From 71859c640f6a36251aca223fd503c58dc314e296 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 16 Sep 2010 19:46:31 +0000 Subject: [PATCH] Print the address of sections as 0 and create the metadata sections in the same order as gnu as. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@114109 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/MC/ELFObjectWriter.cpp | 57 ++++++++++++++++---------------------- test/MC/ELF/empty.s | 36 +++++++++++++++++++++++- 2 files changed, 59 insertions(+), 34 deletions(-) diff --git a/lib/MC/ELFObjectWriter.cpp b/lib/MC/ELFObjectWriter.cpp index feabd6008bf..8295dacd156 100644 --- a/lib/MC/ELFObjectWriter.cpp +++ b/lib/MC/ELFObjectWriter.cpp @@ -205,7 +205,8 @@ namespace { const MCAsmLayout &Layout); void WriteSymbolTable(MCDataFragment *F, const MCAssembler &Asm, - const MCAsmLayout &Layout); + const MCAsmLayout &Layout, + unsigned NumRegularSections); void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, @@ -405,7 +406,8 @@ void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD, void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F, const MCAssembler &Asm, - const MCAsmLayout &Layout) { + const MCAsmLayout &Layout, + unsigned NumRegularSections) { // The string table must be emitted first because we need the index // into the string table for all the symbol names. assert(StringTable.size() && "Missing string table"); @@ -423,20 +425,16 @@ void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F, WriteSymbol(F, MSD, Layout); } - // Write out a symbol table entry for each section. - // leaving out the just added .symtab which is at - // the very end + // Write out a symbol table entry for each regular section. unsigned Index = 1; - for (MCAssembler::const_iterator it = Asm.begin(), - ie = Asm.end(); it != ie; ++it, ++Index) { + for (MCAssembler::const_iterator it = Asm.begin(); + Index <= NumRegularSections; ++it, ++Index) { const MCSectionELF &Section = static_cast(it->getSection()); // Leave out relocations so we don't have indexes within // the relocations messed up if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL) continue; - if (Index == Asm.size()) - continue; WriteSymbolEntry(F, 0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, Index); LastLocalSymbolIndex++; } @@ -785,48 +783,41 @@ void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm, const MCSection *SymtabSection; unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32; + unsigned NumRegularSections = Asm.size(); + + // We construct .shstrtab, .symtab and .strtab is this order to match gnu as. + const MCSection *ShstrtabSection; + ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0, + SectionKind::getReadOnly(), false); + MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection); + ShstrtabSD.setAlignment(1); + ShstrtabIndex = Asm.size(); + SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, SectionKind::getReadOnly(), false, EntrySize); - MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection); - SymtabSD.setAlignment(Is64Bit ? 8 : 4); - F = new MCDataFragment(&SymtabSD); - - // Symbol table - WriteSymbolTable(F, Asm, Layout); - Asm.AddSectionToTheEnd(SymtabSD, Layout); - const MCSection *StrtabSection; StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0, SectionKind::getReadOnly(), false); - MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection); StrtabSD.setAlignment(1); - - // FIXME: This isn't right. If the sections get rearranged this will - // be wrong. We need a proper lookup. StringTableIndex = Asm.size(); + + // Symbol table + F = new MCDataFragment(&SymtabSD); + WriteSymbolTable(F, Asm, Layout, NumRegularSections); + Asm.AddSectionToTheEnd(SymtabSD, Layout); + F = new MCDataFragment(&StrtabSD); F->getContents().append(StringTable.begin(), StringTable.end()); Asm.AddSectionToTheEnd(StrtabSD, Layout); - const MCSection *ShstrtabSection; - ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0, - SectionKind::getReadOnly(), false); - - MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection); - ShstrtabSD.setAlignment(1); - F = new MCDataFragment(&ShstrtabSD); - // FIXME: This isn't right. If the sections get rearranged this will - // be wrong. We need a proper lookup. - ShstrtabIndex = Asm.size(); - // Section header string table. // // The first entry of a string table holds a null character so skip @@ -974,7 +965,7 @@ void ELFObjectWriterImpl::WriteObject(const MCAssembler &Asm, WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()], Section.getType(), Section.getFlags(), - Layout.getSectionAddress(&SD), + 0, SectionOffsetMap.lookup(&SD.getSection()), Layout.getSectionSize(&SD), sh_link, sh_info, SD.getAlignment(), diff --git a/test/MC/ELF/empty.s b/test/MC/ELF/empty.s index ba98a8c527e..62cbb275e08 100644 --- a/test/MC/ELF/empty.s +++ b/test/MC/ELF/empty.s @@ -1,6 +1,7 @@ // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | elf-dump | FileCheck %s -// Test that like gnu as we create text, data and bss by default. +// Test that like gnu as we create text, data and bss by default. Also test +// that shstrtab, symtab and strtab are listed in that order. // CHECK: ('sh_name', 1) # '.text' // CHECK-NEXT: ('sh_type', 1) @@ -34,3 +35,36 @@ // CHECK-NEXT: ('sh_info', 0) // CHECK-NEXT: ('sh_addralign', 4) // CHECK-NEXT: ('sh_entsize', 0) + +// CHECK: ('sh_name', 18) # '.shstrtab' +// CHECK-NEXT: ('sh_type', 3) +// CHECK-NEXT: ('sh_flags', 0) +// CHECK-NEXT: ('sh_addr', 0) +// CHECK-NEXT: ('sh_offset', 64) +// CHECK-NEXT: ('sh_size', 44) +// CHECK-NEXT: ('sh_link', 0) +// CHECK-NEXT: ('sh_info', 0) +// CHECK-NEXT: ('sh_addralign', 1) +// CHECK-NEXT: ('sh_entsize', 0) + +// CHECK: ('sh_name', 28) # '.symtab' +// CHECK-NEXT: ('sh_type', 2) +// CHECK-NEXT: ('sh_flags', 0) +// CHECK-NEXT: ('sh_addr', 0) +// CHECK-NEXT: ('sh_offset', +// CHECK-NEXT: ('sh_size', 96) +// CHECK-NEXT: ('sh_link', 6) +// CHECK-NEXT: ('sh_info', 4) +// CHECK-NEXT: ('sh_addralign', 8) +// CHECK-NEXT: ('sh_entsize', 24) + +// CHECK: ('sh_name', 36) # '.strtab' +// CHECK-NEXT: ('sh_type', 3) +// CHECK-NEXT: ('sh_flags', 0) +// CHECK-NEXT: ('sh_addr', 0) +// CHECK-NEXT: ('sh_offset', +// CHECK-NEXT: ('sh_size', 1) +// CHECK-NEXT: ('sh_link', 0) +// CHECK-NEXT: ('sh_info', 0) +// CHECK-NEXT: ('sh_addralign', 1) +// CHECK-NEXT: ('sh_entsize', 0)