-void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
- MCAsmLayout &Layout,
- SectionIndexMapTy &SectionIndexMap,
- const RelMapTy &RelMap) {
- MCContext &Ctx = Asm.getContext();
- MCDataFragment *F;
-
- unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
-
- // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
- const MCSectionELF *ShstrtabSection =
- Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
- SectionKind::getReadOnly());
- MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
- ShstrtabSD.setAlignment(1);
-
- const MCSectionELF *SymtabSection =
- Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
- SectionKind::getReadOnly(),
- EntrySize, "");
- MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
- SymtabSD.setAlignment(is64Bit() ? 8 : 4);
-
- MCSectionData *SymtabShndxSD = NULL;
-
- if (NeedsSymtabShndx) {
- const MCSectionELF *SymtabShndxSection =
- Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
- SectionKind::getReadOnly(), 4, "");
- SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
- SymtabShndxSD->setAlignment(4);
- }
-
- const MCSectionELF *StrtabSection;
- StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
- SectionKind::getReadOnly());
- MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
- StrtabSD.setAlignment(1);
-
- ComputeIndexMap(Asm, SectionIndexMap, RelMap);
-
- ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
- SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
- StringTableIndex = SectionIndexMap.lookup(StrtabSection);
-
- // Symbol table
- F = new MCDataFragment(&SymtabSD);
- MCDataFragment *ShndxF = NULL;
- if (NeedsSymtabShndx) {
- ShndxF = new MCDataFragment(SymtabShndxSD);
- }
- WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
-
- F = new MCDataFragment(&StrtabSD);
- F->getContents().append(StringTable.begin(), StringTable.end());
-
- F = new MCDataFragment(&ShstrtabSD);
-
- std::vector<const MCSectionELF*> Sections;
- for (MCAssembler::const_iterator it = Asm.begin(),
- ie = Asm.end(); it != ie; ++it) {
- const MCSectionELF &Section =
- static_cast<const MCSectionELF&>(it->getSection());
- Sections.push_back(&Section);
- }
- array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
-
- // Section header string table.
- //
- // The first entry of a string table holds a null character so skip
- // section 0.
- uint64_t Index = 1;
- F->getContents().push_back('\x00');
-
- for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
- const MCSectionELF &Section = *Sections[I];
-
- StringRef Name = Section.getSectionName();
- if (I != 0) {
- StringRef PreviousName = Sections[I - 1]->getSectionName();
- if (PreviousName.endswith(Name)) {
- SectionStringTableIndex[&Section] = Index - Name.size() - 1;
- continue;
- }
- }
- // Remember the index into the string table so we can write it
- // into the sh_name field of the section header table.
- SectionStringTableIndex[&Section] = Index;
-
- Index += Name.size() + 1;
- F->getContents().append(Name.begin(), Name.end());
- F->getContents().push_back('\x00');
- }
-}
-
-void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
- MCAsmLayout &Layout,
- GroupMapTy &GroupMap,
- RevGroupMapTy &RevGroupMap,
- SectionIndexMapTy &SectionIndexMap,
- const RelMapTy &RelMap) {
- // Create the .note.GNU-stack section if needed.
- MCContext &Ctx = Asm.getContext();
- if (Asm.getNoExecStack()) {
- const MCSectionELF *GnuStackSection =
- Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
- SectionKind::getReadOnly());
- Asm.getOrCreateSectionData(*GnuStackSection);
- }
-
- // Build the groups
- for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
- it != ie; ++it) {
- const MCSectionELF &Section =
- static_cast<const MCSectionELF&>(it->getSection());
- if (!(Section.getFlags() & ELF::SHF_GROUP))
- continue;
-
- const MCSymbol *SignatureSymbol = Section.getGroup();
- Asm.getOrCreateSymbolData(*SignatureSymbol);
- const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
- if (!Group) {
- Group = Ctx.CreateELFGroupSection();
- MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
- Data.setAlignment(4);
- MCDataFragment *F = new MCDataFragment(&Data);
- String32(*F, ELF::GRP_COMDAT);
- }
- GroupMap[Group] = SignatureSymbol;
- }
-
- ComputeIndexMap(Asm, SectionIndexMap, RelMap);
-
- // Add sections to the groups
- for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
- it != ie; ++it) {
- const MCSectionELF &Section =
- static_cast<const MCSectionELF&>(it->getSection());
- if (!(Section.getFlags() & ELF::SHF_GROUP))
- continue;
- const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
- MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
- // FIXME: we could use the previous fragment
- MCDataFragment *F = new MCDataFragment(&Data);
- unsigned Index = SectionIndexMap.lookup(&Section);
- String32(*F, Index);
- }