/* 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; }
/** Attaches a previously unattached PE Section to the PE Section Table. This method complements * SgAsmPESection::init_from_section_table. This method initializes the section table from the section while * init_from_section_table() initializes the section from the section table. */ void SgAsmPESectionTable::add_section(SgAsmPESection *section) { ROSE_ASSERT(section!=NULL); ROSE_ASSERT(section->get_file()==get_file()); ROSE_ASSERT(section->get_header()==get_header()); ROSE_ASSERT(section->get_section_entry()==NULL); /* must not be in the section table yet */ SgAsmPEFileHeader *fhdr = dynamic_cast<SgAsmPEFileHeader*>(get_header()); ROSE_ASSERT(fhdr!=NULL); /* Assign an ID if there isn't one yet. */ if (section->get_id()<0) { SgAsmGenericSectionList *seclist = fhdr->get_sections();; int max_id=0; /*assume zero is used so we start at one*/ for (size_t i=0; i<seclist->get_sections().size(); i++) { SgAsmGenericSection *s = seclist->get_sections()[i]; max_id = std::max(max_id, s->get_id()); } section->set_id(max_id+1); } /* Create a new section table entry. */ SgAsmPESectionTableEntry *entry = new SgAsmPESectionTableEntry; entry->update_from_section(section); section->set_section_entry(entry); }
/* Writes the section table back to disk. */ void SgAsmPESectionTable::unparse(std::ostream &f) const { SgAsmPEFileHeader *fhdr = dynamic_cast<SgAsmPEFileHeader*>(get_header()); ROSE_ASSERT(fhdr != NULL); SgAsmGenericSectionPtrList sections = fhdr->get_sections()->get_sections(); for (size_t i = 0; i < sections.size(); i++) { if (sections[i]->get_id()>=0) { SgAsmPESection *section = isSgAsmPESection(sections[i]); ROSE_ASSERT(section!=NULL); /* Write the table entry */ ROSE_ASSERT(section->get_id() > 0); /*ID's are 1-origin in PE*/ size_t slot = section->get_id() - 1; SgAsmPESectionTableEntry *shdr = section->get_section_entry(); SgAsmPESectionTableEntry::PESectionTableEntry_disk disk; shdr->encode(&disk); write(f, slot*sizeof(disk), sizeof disk, &disk); } } }