/** Pre-unparsing updates */ bool SgAsmElfSectionTable::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_shextrasz(opt_size); fhdr->set_e_shnum(nentries); return reallocated; }
/** Attaches a previously unattached ELF Section to the section table. If @p section is an ELF String Section * (SgAsmElfStringSection) that contains an ELF String Table (SgAsmElfStringTable) and the ELF Section Table has no * associated string table then the @p section will be used as the string table to hold the section names. * * This method complements SgAsmElfSection::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. * * Returns the new section table entry linked into the AST. */ SgAsmElfSectionTableEntry * SgAsmElfSectionTable::add_section(SgAsmElfSection *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 */ SgAsmElfFileHeader *fhdr = dynamic_cast<SgAsmElfFileHeader*>(get_header()); ROSE_ASSERT(fhdr!=NULL); /* Assign an ID if there isn't one yet */ if (section->get_id()<0) { int id = fhdr->get_e_shnum(); fhdr->set_e_shnum(id+1); section->set_id(id); } /* If the supplied section is a string table and the ELF Section Table doesn't have a string table associated with it yet, * then use the supplied section as the string table to hold the names of the sections. When this happens, all sections * that are already defined in the ELF Section Table should have their names moved into the new string table. */ SgAsmElfStringSection *strsec = NULL; if (fhdr->get_e_shstrndx()==0) { strsec = dynamic_cast<SgAsmElfStringSection*>(section); if (strsec) { fhdr->set_e_shstrndx(section->get_id()); SgAsmGenericSectionList *all = fhdr->get_sections(); for (size_t i=0; i<all->get_sections().size(); i++) { SgAsmElfSection *s = dynamic_cast<SgAsmElfSection*>(all->get_sections()[i]); if (s && s->get_id()>=0 && s->get_section_entry()!=NULL) { s->allocate_name_to_storage(strsec); } } } } else { strsec = dynamic_cast<SgAsmElfStringSection*>(fhdr->get_section_by_id(fhdr->get_e_shstrndx())); ROSE_ASSERT(strsec!=NULL); } /* Make sure the name is in the correct string table */ if (strsec) section->allocate_name_to_storage(strsec); /* Create a new section table entry. */ SgAsmElfSectionTableEntry *shdr = new SgAsmElfSectionTableEntry; shdr->update_from_section(section); section->set_section_entry(shdr); return shdr; }
/** Non-parsing constructor for an ELF Section Table */ void SgAsmElfSectionTable::ctor() { /* There can be only one ELF Section Table */ SgAsmElfFileHeader *fhdr = dynamic_cast<SgAsmElfFileHeader*>(get_header()); ROSE_ASSERT(fhdr); ROSE_ASSERT(fhdr->get_section_table()==NULL); set_synthesized(true); /* the section table isn't really a section itself */ set_name(new SgAsmBasicString("ELF Section Table")); set_purpose(SP_HEADER); /* Every section table has a first entry that's all zeros. We don't declare that section here (see parse()) but we do set * the section count in the header in order to reserve that first slot. */ if (fhdr->get_e_shnum()<1) fhdr->set_e_shnum(1); fhdr->set_section_table(this); }