当前位置: 首页>>代码示例>>C++>>正文


C++ pe_base::update_number_of_sections方法代码示例

本文整理汇总了C++中pe_base::update_number_of_sections方法的典型用法代码示例。如果您正苦于以下问题:C++ pe_base::update_number_of_sections方法的具体用法?C++ pe_base::update_number_of_sections怎么用?C++ pe_base::update_number_of_sections使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pe_base的用法示例。


在下文中一共展示了pe_base::update_number_of_sections方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: rebuild_pe

//Rebuilds PE image headers
//If strip_dos_header is true, DOS headers partially will be used for PE headers
//If change_size_of_headers == true, SizeOfHeaders will be recalculated automatically
//If save_bound_import == true, existing bound import directory will be saved correctly (because some compilers and bind.exe put it to PE headers)
void rebuild_pe(pe_base& pe, image_dos_header& dos_header, bool strip_dos_header, bool change_size_of_headers, bool save_bound_import)
{
    dos_header = pe.get_dos_header();

    if(strip_dos_header)
    {
        //Strip stub overlay
        pe.strip_stub_overlay();
        //BaseOfCode NT Headers field now overlaps
        //e_lfanew field, so we're acrually setting
        //e_lfanew with this call
        pe.set_base_of_code(8 * sizeof(uint16_t));
    }
    else
    {
        //Set start of PE headers
        dos_header.e_lfanew = sizeof(image_dos_header)
            + pe_utils::align_up(static_cast<uint32_t>(pe.get_stub_overlay().size()), sizeof(uint32_t));
    }

    section_list& sections = pe.get_image_sections();

    //Calculate pointer to section data
    size_t ptr_to_section_data = (strip_dos_header ? 8 * sizeof(uint16_t) : sizeof(image_dos_header)) + pe.get_sizeof_nt_header()
        + pe_utils::align_up(pe.get_stub_overlay().size(), sizeof(uint32_t))
        - sizeof(image_data_directory) * (image_numberof_directory_entries - pe.get_number_of_rvas_and_sizes())
        + sections.size() * sizeof(image_section_header);

    if(save_bound_import && pe.has_bound_import())
    {
        //It will be aligned to DWORD, because we're aligning to DWORD everything above it
        pe.set_directory_rva(image_directory_entry_bound_import, static_cast<uint32_t>(ptr_to_section_data));
        ptr_to_section_data += pe.get_directory_size(image_directory_entry_bound_import);    
    }
    
    ptr_to_section_data = pe_utils::align_up(ptr_to_section_data, pe.get_file_alignment());

    //Set size of headers and size of optional header
    if(change_size_of_headers)
    {
        if(!pe.get_image_sections().empty())
        {
            if(static_cast<uint32_t>(ptr_to_section_data) > (*sections.begin()).get_virtual_address())
                throw pe_exception("Headers of PE file are too long. Try to strip STUB or don't build bound import", pe_exception::cannot_rebuild_image);
        }

        pe.set_size_of_headers(static_cast<uint32_t>(ptr_to_section_data));
    }

    //Set number of sections in PE header
    pe.update_number_of_sections();

    pe.update_image_size();

    pe.set_size_of_optional_header(static_cast<uint16_t>(pe.get_sizeof_opt_headers()
        - sizeof(image_data_directory) * (image_numberof_directory_entries - pe.get_number_of_rvas_and_sizes())));

    //Recalculate pointer to raw data according to section list
    for(section_list::iterator it = sections.begin(); it != sections.end(); ++it)
    {
        //Save section headers PointerToRawData
        if((*it).get_size_of_raw_data())
        {
            (*it).set_pointer_to_raw_data(static_cast<uint32_t>(ptr_to_section_data));
            ptr_to_section_data += (*it).get_aligned_raw_size(pe.get_file_alignment());
        }
    }
}
开发者ID:apriorit,项目名称:portable-executable-library,代码行数:72,代码来源:pe_rebuilder.cpp


注:本文中的pe_base::update_number_of_sections方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。