/** Pre-unparsing adjustments */ bool SgAsmElfNoteSection::reallocate() { bool reallocated = SgAsmElfSection::reallocate(); /* How much space is needed by the notes? */ rose_addr_t need = 0; for (size_t i=0; i<p_entries->get_entries().size(); i++) { SgAsmElfNoteEntry *ent = p_entries->get_entries()[i]; need += ent->calculate_size(); } /* Adjust the section/segment size */ if (need < get_size()) { if (is_mapped()) { ROSE_ASSERT(get_mapped_size()==get_size()); set_mapped_size(need); } set_size(need); reallocated = true; } else if (need > get_size()) { get_file()->shift_extend(this, 0, need-get_size(), SgAsmGenericFile::ADDRSP_ALL, SgAsmGenericFile::ELASTIC_HOLE); reallocated = true; } return reallocated; }
/** Pre-unparsing updates */ bool SgAsmElfSegmentTable::reallocate() { bool reallocated = false; /* Resize based on word size from ELF File Header */ size_t opt_size, nentries; rose_addr_t need = calculate_sizes(NULL, NULL, &opt_size, &nentries); if (need < get_size()) { if (is_mapped()) { ROSE_ASSERT(get_mapped_size()==get_size()); set_mapped_size(need); } set_size(need); reallocated = true; } else if (need > get_size()) { get_file()->shift_extend(this, 0, need-get_size(), SgAsmGenericFile::ADDRSP_ALL, SgAsmGenericFile::ELASTIC_HOLE); reallocated = true; } /* Update data members in the ELF File Header. No need to return true for these changes. */ SgAsmElfFileHeader *fhdr = dynamic_cast<SgAsmElfFileHeader*>(get_header()); fhdr->set_phextrasz(opt_size); fhdr->set_e_phnum(nentries); return reallocated; }
/* Pre-unparsing updates */ bool SgAsmPESectionTable::reallocate() { bool reallocated = false; /* Resize based on section having largest ID */ SgAsmPEFileHeader *fhdr = dynamic_cast<SgAsmPEFileHeader*>(get_header()); ROSE_ASSERT(fhdr != NULL); SgAsmGenericSectionPtrList sections = fhdr->get_sections()->get_sections(); int max_id = 0; for (size_t i=0; i<sections.size(); i++) { max_id = std::max(max_id, sections[i]->get_id()); } size_t nsections = max_id; /*PE section IDs are 1-origin*/ size_t need = nsections * sizeof(SgAsmPESectionTableEntry::PESectionTableEntry_disk); if (need < get_size()) { if (is_mapped()) { ROSE_ASSERT(get_mapped_size()==get_size()); set_mapped_size(need); } set_size(need); reallocated = true; } else if (need > get_size()) { get_file()->shift_extend(this, 0, need-get_size(), SgAsmGenericFile::ADDRSP_ALL, SgAsmGenericFile::ELASTIC_HOLE); reallocated = true; } return reallocated; }
bool SgAsmElfSection::reallocate() { bool reallocated = false; SgAsmElfSectionTableEntry *sechdr = get_section_entry(); SgAsmElfSegmentTableEntry *seghdr = get_segment_entry(); /* Change section size if this section was defined in the ELF Section Table */ if (sechdr!=NULL) { rose_addr_t need = calculate_sizes(NULL, NULL, NULL, NULL); if (need < get_size()) { if (is_mapped()) { ROSE_ASSERT(get_mapped_size()==get_size()); set_mapped_size(need); } set_size(need); reallocated = true; } else if (need > get_size()) { get_file()->shift_extend(this, 0, need-get_size(), SgAsmGenericFile::ADDRSP_ALL, SgAsmGenericFile::ELASTIC_HOLE); reallocated = true; } } /* Update entry in the ELF Section Table and/or ELF Segment Table */ if (sechdr) sechdr->update_from_section(this); if (seghdr) seghdr->update_from_section(this); return reallocated; }
/* Change size of PE header based on word size */ bool SgAsmPEFileHeader::reallocate() { bool reallocated = SgAsmGenericHeader::reallocate(); /* Resize if necessary */ rose_addr_t need = sizeof(PEFileHeader_disk); if (4==get_word_size()) { need += sizeof(PE32OptHeader_disk); } else if (8==get_word_size()) { need += sizeof(PE64OptHeader_disk); } else { throw FormatError("unsupported PE word size"); } need += p_rvasize_pairs->get_pairs().size() * sizeof(SgAsmPERVASizePair::RVASizePair_disk); if (need<get_size()) { if (is_mapped()) { ROSE_ASSERT(get_mapped_size()==get_size()); set_mapped_size(need); } set_size(need); reallocated = true; } else if (need>get_size()) { get_file()->shift_extend(this, 0, need-get_size(), SgAsmGenericFile::ADDRSP_ALL, SgAsmGenericFile::ELASTIC_HOLE); reallocated = true; } /* Make sure the RVA/Size pairs at the end of the header are consistent with the sections to which they point. Reallocate() * has already been called recursively for the sections. */ update_rvasize_pairs(); /* Make sure header is consistent with sections. Reallocate() has already been called recursively for the sections. * Count the number of sections in the table and update the header's e_nsections member. */ if (p_section_table) { ROSE_ASSERT(p_section_table->get_header()==this); SgAsmGenericSectionList *all = get_sections(); p_e_nsections = 0; for (size_t i=0; i<all->get_sections().size(); i++) { SgAsmPESection *pesec = dynamic_cast<SgAsmPESection*>(all->get_sections()[i]); if (pesec && pesec->get_section_entry()!=NULL) p_e_nsections++; } rose_addr_t header_size = ALIGN_UP(p_section_table->get_offset() + p_section_table->get_size(), p_e_file_align>0 ? p_e_file_align : 1); #if 1 /* The PE Specification regarding e_header_size (known as "SizeOfHeader" on page 14 of "Microsoft Portable Executable * and Common Object File Format Specification: Revision 8.1 February 15, 2008" is not always followed. We recompute * it here as being the minimum RVA from all the sections defined in the PE Section Table, but not smaller * than the value according to the specification. This alternate value is kept if it's already in the parse tree, * otherwise we use the correct value. (RPM 2008-10-21) */ rose_addr_t min_offset = 0; for (size_t i=0, nfound=0; i<all->get_sections().size(); i++) { SgAsmPESection *pesec = dynamic_cast<SgAsmPESection*>(all->get_sections()[i]); if (pesec && pesec->get_section_entry()!=NULL) { if (0==nfound++) { min_offset = pesec->get_offset(); } else { min_offset = std::min(min_offset, pesec->get_offset() ); } } } rose_addr_t header_size2 = std::max(header_size, min_offset); if (p_e_header_size==header_size2) header_size = header_size2; /* If the original header size was zero then don't change that--leave it at zero. Some tiny executables have a zero * value here and as a result, since this is near the end of the NT Optional Header, they can truncate the file and * the loader will fill the optional header with zeros when reading. (RPM 2008-11-11) */ if (p_e_header_size==0) header_size = 0; #endif p_e_header_size = header_size; } /* The size of the optional header. If there's a section table then we use its offset to calculate the optional header * size in order to be compatible with the PE loader. Otherwise use the actual optional header size. */ if (p_section_table) { ROSE_ASSERT(p_section_table->get_offset() >= get_offset() + sizeof(PEFileHeader_disk)); p_e_nt_hdr_size = p_section_table->get_offset() - (get_offset() + sizeof(PEFileHeader_disk)); } else if (4==get_word_size()) { p_e_nt_hdr_size = sizeof(PE32OptHeader_disk); } else if (8==get_word_size()) { p_e_nt_hdr_size = sizeof(PE64OptHeader_disk); } else { throw FormatError("invalid PE word size"); } /* Update COFF symbol table related data members in the file header */ if (get_coff_symtab()) { ROSE_ASSERT(get_coff_symtab()->get_header()==this); set_e_coff_symtab(get_coff_symtab()->get_offset()); set_e_coff_nsyms(get_coff_symtab()->get_nslots()); } /* Update some additional header fields */ set_e_num_rvasize_pairs(get_rvasize_pairs()->get_pairs().size()); set_e_opt_magic(4==get_word_size() ? 0x010b : 0x020b); set_e_lmajor((get_exec_format()->get_version() >> 16) & 0xffff); set_e_lminor(get_exec_format()->get_version() & 0xffff); /* Adjust the COFF Header's e_nt_hdr_size to accommodate the NT Optional Header in such a way that EXEs from tinype.com * don't change (i.e., don't increase e_nt_hdr_size if the bytes beyond it are zero anyway, and if they aren't then adjust * it as little as possible. The RVA/Size pairs are considered to be part of the NT Optional Header. */ size_t oh_size = p_rvasize_pairs->get_pairs().size() * sizeof(SgAsmPERVASizePair::RVASizePair_disk); size_t rvasize_offset; /*offset with respect to "oh" buffer allocated below*/ if (4==get_word_size()) { oh_size += sizeof(PE32OptHeader_disk); } else if (8==get_word_size()) { oh_size += sizeof(PE64OptHeader_disk); } else { throw FormatError("unsupported PE word size"); } unsigned char *oh = new unsigned char[oh_size]; if (4==get_word_size()) { encode((PE32OptHeader_disk*)oh); rvasize_offset = sizeof(PE32OptHeader_disk); } else if (8==get_word_size()) { encode((PE64OptHeader_disk*)oh); rvasize_offset = sizeof(PE64OptHeader_disk); } else { delete[] oh; throw FormatError("unsupported PE word size"); } while (oh_size>p_e_nt_hdr_size) { if (0!=oh[oh_size-1]) break; --oh_size; } set_e_nt_hdr_size(oh_size); return reallocated; }
/** Update prior to unparsing */ bool SgAsmElfFileHeader::reallocate() { /* Reallocate superclass. This also calls reallocate() for all the sections associated with this ELF File Header. */ bool reallocated = SgAsmGenericHeader::reallocate(); /* Resize header based on current word size */ rose_addr_t need; if (4==get_word_size()) { need = sizeof(Elf32FileHeader_disk); } else if (8==get_word_size()) { need = sizeof(Elf64FileHeader_disk); } else { throw FormatError("unsupported ELF word size"); } if (need < get_size()) { if (is_mapped()) { ROSE_ASSERT(get_mapped_size()==get_size()); set_mapped_size(need); } set_size(need); reallocated = true; } else if (need > get_size()) { get_file()->shift_extend(this, 0, need-get_size(), SgAsmGenericFile::ADDRSP_ALL, SgAsmGenericFile::ELASTIC_HOLE); reallocated = true; } /* Update ELF-specific file class data member from generic data. */ switch(get_word_size()) { case 4: p_e_ident_file_class = 1; break; case 8: p_e_ident_file_class = 2; break; default: ROSE_ASSERT(!"invalid word size"); break; } /* Byte order. According to the spec, valid values are 1 (little-endian) and 2 (big-endian). However, we've seen cases * where a value of zero is used to indicate "native" order (loader assumes words are in the order of the machine on which * the loader is running, and the ROSE ELF parser determines the order by looking at other fields in the header). Any * original value other than 1 or 2 will be written to the new output; otherwise we choose 1 or 2 based on the currently * defined byte order. */ if (p_e_ident_data_encoding==1 || p_e_ident_data_encoding==2) { p_e_ident_data_encoding = ByteOrder::ORDER_LSB==get_sex() ? 1 : 2; } /* Update ELF-specific file type from generic data. */ switch (p_exec_format->get_purpose()) { case PURPOSE_UNSPECIFIED: case PURPOSE_PROC_SPECIFIC: case PURPOSE_OS_SPECIFIC: case PURPOSE_OTHER: /* keep as is */ break; case PURPOSE_LIBRARY: if (p_e_type==1 || p_e_type==3) { /* keep as is */ } else { p_e_type = 1; } break; case PURPOSE_EXECUTABLE: p_e_type = 2; break; case PURPOSE_CORE_DUMP: p_e_type = 4; } /* Update ELF machine type. */ p_e_machine = isa_to_machine(get_isa()); /* The ELF header stores its own size */ p_e_ehsize = get_size(); return reallocated; }
/** Returns the memory extent for a mapped section. If the section is not mapped then offset and size will be zero */ Extent SgAsmGenericSection::get_mapped_preferred_extent() const { ROSE_ASSERT(this != NULL); return Extent(get_mapped_preferred_rva(), get_mapped_size()); }
/** Returns whether section desires to be mapped to memory */ bool SgAsmGenericSection::is_mapped() const { return (get_mapped_preferred_rva()!=0 || get_mapped_size()!=0 || get_mapped_rperm() || get_mapped_wperm() || get_mapped_xperm()); }