[llvm-commits] [llvm] r114109 - in /llvm/trunk: lib/MC/ELFObjectWriter.cpp test/MC/ELF/empty.s

Rafael Espindola rafael.espindola at gmail.com
Thu Sep 16 12:46:31 PDT 2010


Author: rafael
Date: Thu Sep 16 14:46:31 2010
New Revision: 114109

URL: http://llvm.org/viewvc/llvm-project?rev=114109&view=rev
Log:
Print the address of sections as 0 and create the metadata sections in the
same order as gnu as.

Modified:
    llvm/trunk/lib/MC/ELFObjectWriter.cpp
    llvm/trunk/test/MC/ELF/empty.s

Modified: llvm/trunk/lib/MC/ELFObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/ELFObjectWriter.cpp?rev=114109&r1=114108&r2=114109&view=diff
==============================================================================
--- llvm/trunk/lib/MC/ELFObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/ELFObjectWriter.cpp Thu Sep 16 14:46:31 2010
@@ -205,7 +205,8 @@
                      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::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 @@
     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<const MCSectionELF&>(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 @@
   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 @@
 
     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(),

Modified: llvm/trunk/test/MC/ELF/empty.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/empty.s?rev=114109&r1=114108&r2=114109&view=diff
==============================================================================
--- llvm/trunk/test/MC/ELF/empty.s (original)
+++ llvm/trunk/test/MC/ELF/empty.s Thu Sep 16 14:46:31 2010
@@ -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)





More information about the llvm-commits mailing list