[lld] r245008 - ELF: Create a symbol table.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 13 19:42:20 PDT 2015


Author: rafael
Date: Thu Aug 13 21:42:20 2015
New Revision: 245008

URL: http://llvm.org/viewvc/llvm-project?rev=245008&view=rev
Log:
ELF: Create a symbol table.

For now it is empty. I will add the symbols in a followup patch.

Added:
    lld/trunk/test/elf2/symbols.s
Modified:
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/elf2/basic.s
    lld/trunk/test/elf2/basic32.s
    lld/trunk/test/elf2/basic32be.s
    lld/trunk/test/elf2/basic64be.s
    lld/trunk/test/elf2/string-table.s

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=245008&r1=245007&r2=245008&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Aug 13 21:42:20 2015
@@ -100,6 +100,25 @@ public:
   }
 };
 
+template <class ELFT>
+class SymbolTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
+public:
+  SymbolTableSection()
+      : OutputSectionBase<ELFT::Is64Bits>(".symtab", SHT_SYMTAB, 0) {
+    typedef OutputSectionBase<ELFT::Is64Bits> Base;
+    typename Base::HeaderT &Header = this->Header;
+
+    // For now the only local symbol is going to be the one at index 0
+    Header.sh_info = 1;
+
+    Header.sh_entsize = sizeof(typename ELFFile<ELFT>::Elf_Sym);
+    Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
+  }
+  void setStringTableIndex(uint32_t Index) { this->Header.sh_link = Index; }
+
+  void writeTo(uint8_t *Buf) override;
+};
+
 // The writer writes a SymbolTable result to a file.
 template <class ELFT> class Writer {
 public:
@@ -124,6 +143,8 @@ private:
   uintX_t SizeOfHeaders;
   uintX_t SectionHeaderOff;
 
+  SymbolTableSection<ELFT> SymbolTable;
+
   unsigned StringTableIndex;
   StringTableSection<ELFT::Is64Bits> StringTable;
 
@@ -180,6 +201,8 @@ void StringTableSection<Is64Bits>::write
   memcpy(Buf, Data.data(), Data.size());
 }
 
+template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {}
+
 template <bool Is64Bits>
 template <endianness E>
 void OutputSectionBase<Is64Bits>::writeHeaderTo(
@@ -261,8 +284,10 @@ template <class ELFT> void Writer<ELFT>:
   std::stable_sort(OutputSections.begin(), OutputSections.end(),
                    compSec<ELFT::Is64Bits>);
 
+  OutputSections.push_back(&SymbolTable);
   OutputSections.push_back(&StringTable);
   StringTableIndex = OutputSections.size();
+  SymbolTable.setStringTableIndex(StringTableIndex);
 
   for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) {
     StringTable.add(Sec->getName());

Modified: lld/trunk/test/elf2/basic.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/basic.s?rev=245008&r1=245007&r2=245008&view=diff
==============================================================================
--- lld/trunk/test/elf2/basic.s (original)
+++ lld/trunk/test/elf2/basic.s Thu Aug 13 21:42:20 2015
@@ -25,15 +25,15 @@ _start:
 # CHECK-NEXT:   Version: 1
 # CHECK-NEXT:   Entry: 0x401000
 # CHECK-NEXT:   ProgramHeaderOffset: 0x40
-# CHECK-NEXT:   SectionHeaderOffset: 0x1030
+# CHECK-NEXT:   SectionHeaderOffset: 0x1038
 # CHECK-NEXT:   Flags [ (0x0)
 # CHECK-NEXT:   ]
 # CHECK-NEXT:   HeaderSize: 64
 # CHECK-NEXT:   ProgramHeaderEntrySize: 56
 # CHECK-NEXT:   ProgramHeaderCount: 1
 # CHECK-NEXT:   SectionHeaderEntrySize: 64
-# CHECK-NEXT:   SectionHeaderCount: 5
-# CHECK-NEXT:   StringTableSectionIndex: 4
+# CHECK-NEXT:   SectionHeaderCount: 6
+# CHECK-NEXT:   StringTableSectionIndex: 5
 # CHECK-NEXT: }
 # CHECK-NEXT: Sections [
 # CHECK-NEXT:   Section {
@@ -100,13 +100,27 @@ _start:
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Section {
 # CHECK-NEXT:     Index: 4
+# CHECK-NEXT:     Name: .symtab
+# CHECK-NEXT:     Type: SHT_SYMTAB (0x2)
+# CHECK-NEXT:     Flags [ (0x0)
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Address: 0x0
+# CHECK-NEXT:     Offset: 0x1010
+# CHECK-NEXT:     Size: 0
+# CHECK-NEXT:     Link: 5
+# CHECK-NEXT:     Info: 1
+# CHECK-NEXT:     AddressAlignment: 8
+# CHECK-NEXT:     EntrySize: 24
+# CHECK-NEXT:  }
+# CHECK-NEXT:  Section {
+# CHECK-NEXT:    Index: 5
 # CHECK-NEXT:     Name: .strtab (12)
 # CHECK-NEXT:     Type: SHT_STRTAB (0x3)
 # CHECK-NEXT:     Flags [ (0x0)
 # CHECK-NEXT:     ]
 # CHECK-NEXT:     Address: 0x0
 # CHECK-NEXT:     Offset: 0x1010
-# CHECK-NEXT:     Size: 26
+# CHECK-NEXT:     Size: 34
 # CHECK-NEXT:     Link: 0
 # CHECK-NEXT:     Info: 0
 # CHECK-NEXT:     AddressAlignment: 1
@@ -119,8 +133,8 @@ _start:
 # CHECK-NEXT:     Offset: 0x0
 # CHECK-NEXT:     VirtualAddress: 0x400000
 # CHECK-NEXT:     PhysicalAddress: 0x400000
-# CHECK-NEXT:     FileSize: 4464
-# CHECK-NEXT:     MemSize: 4464
+# CHECK-NEXT:     FileSize: 4536
+# CHECK-NEXT:     MemSize: 4536
 # CHECK-NEXT:     Flags [ (0x5)
 # CHECK-NEXT:       PF_R (0x4)
 # CHECK-NEXT:       PF_X (0x1)

Modified: lld/trunk/test/elf2/basic32.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/basic32.s?rev=245008&r1=245007&r2=245008&view=diff
==============================================================================
--- lld/trunk/test/elf2/basic32.s (original)
+++ lld/trunk/test/elf2/basic32.s Thu Aug 13 21:42:20 2015
@@ -25,15 +25,15 @@ _start:
 # CHECK-NEXT:   Version: 1
 # CHECK-NEXT:   Entry: 0x401000
 # CHECK-NEXT:   ProgramHeaderOffset: 0x34
-# CHECK-NEXT:   SectionHeaderOffset: 0x1028
+# CHECK-NEXT:   SectionHeaderOffset: 0x1030
 # CHECK-NEXT:   Flags [ (0x0)
 # CHECK-NEXT:   ]
 # CHECK-NEXT:   HeaderSize: 52
 # CHECK-NEXT:   ProgramHeaderEntrySize: 32
 # CHECK-NEXT:   ProgramHeaderCount: 1
 # CHECK-NEXT:   SectionHeaderEntrySize: 40
-# CHECK-NEXT:   SectionHeaderCount: 5
-# CHECK-NEXT:   StringTableSectionIndex: 4
+# CHECK-NEXT:   SectionHeaderCount: 6
+# CHECK-NEXT:   StringTableSectionIndex: 5
 # CHECK-NEXT: }
 # CHECK-NEXT: Sections [
 # CHECK-NEXT:   Section {
@@ -100,13 +100,27 @@ _start:
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Section {
 # CHECK-NEXT:     Index: 4
+# CHECK-NEXT:     Name: .symtab
+# CHECK-NEXT:     Type: SHT_SYMTAB
+# CHECK-NEXT:     Flags [
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Address: 0x0
+# CHECK-NEXT:     Offset: 0x100C
+# CHECK-NEXT:     Size: 0
+# CHECK-NEXT:     Link: 5
+# CHECK-NEXT:     Info: 1
+# CHECK-NEXT:     AddressAlignment: 4
+# CHECK-NEXT:     EntrySize: 16
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Section {
+# CHECK-NEXT:     Index: 5
 # CHECK-NEXT:     Name: .strtab
 # CHECK-NEXT:     Type: SHT_STRTAB (0x3)
 # CHECK-NEXT:     Flags [ (0x0)
 # CHECK-NEXT:     ]
 # CHECK-NEXT:     Address: 0x0
 # CHECK-NEXT:     Offset: 0x100C
-# CHECK-NEXT:     Size: 26
+# CHECK-NEXT:     Size: 34
 # CHECK-NEXT:     Link: 0
 # CHECK-NEXT:     Info: 0
 # CHECK-NEXT:     AddressAlignment: 1
@@ -119,8 +133,8 @@ _start:
 # CHECK-NEXT:     Offset: 0x0
 # CHECK-NEXT:     VirtualAddress: 0x400000
 # CHECK-NEXT:     PhysicalAddress: 0x400000
-# CHECK-NEXT:     FileSize: 4336
-# CHECK-NEXT:     MemSize: 4336
+# CHECK-NEXT:     FileSize: 4384
+# CHECK-NEXT:     MemSize: 4384
 # CHECK-NEXT:     Flags [ (0x5)
 # CHECK-NEXT:       PF_R (0x4)
 # CHECK-NEXT:       PF_X (0x1)

Modified: lld/trunk/test/elf2/basic32be.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/basic32be.s?rev=245008&r1=245007&r2=245008&view=diff
==============================================================================
--- lld/trunk/test/elf2/basic32be.s (original)
+++ lld/trunk/test/elf2/basic32be.s Thu Aug 13 21:42:20 2015
@@ -25,15 +25,15 @@ _start:
 # CHECK-NEXT:   Version: 1
 # CHECK-NEXT:   Entry: 0x401000
 # CHECK-NEXT:   ProgramHeaderOffset: 0x34
-# CHECK-NEXT:   SectionHeaderOffset: 0x1028
+# CHECK-NEXT:   SectionHeaderOffset: 0x1030
 # CHECK-NEXT:   Flags [ (0x0)
 # CHECK-NEXT:   ]
 # CHECK-NEXT:   HeaderSize: 52
 # CHECK-NEXT:   ProgramHeaderEntrySize: 32
 # CHECK-NEXT:   ProgramHeaderCount: 1
 # CHECK-NEXT:   SectionHeaderEntrySize: 40
-# CHECK-NEXT:   SectionHeaderCount: 5
-# CHECK-NEXT:    StringTableSectionIndex: 4
+# CHECK-NEXT:   SectionHeaderCount: 6
+# CHECK-NEXT:    StringTableSectionIndex: 5
 # CHECK-NEXT: }
 # CHECK-NEXT: Sections [
 # CHECK-NEXT:   Section {
@@ -100,13 +100,27 @@ _start:
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Section {
 # CHECK-NEXT:     Index: 4
+# CHECK-NEXT:     Name: .symtab
+# CHECK-NEXT:     Type: SHT_SYMTAB
+# CHECK-NEXT:     Flags [
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Address: 0x0
+# CHECK-NEXT:     Offset: 0x100C
+# CHECK-NEXT:     Size: 0
+# CHECK-NEXT:     Link: 5
+# CHECK-NEXT:     Info: 1
+# CHECK-NEXT:     AddressAlignment: 4
+# CHECK-NEXT:     EntrySize: 16
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Section {
+# CHECK-NEXT:     Index: 5
 # CHECK-NEXT:     Name: .strtab
 # CHECK-NEXT:     Type: SHT_STRTAB (0x3)
 # CHECK-NEXT:     Flags [ (0x0)
 # CHECK-NEXT:     ]
 # CHECK-NEXT:     Address: 0x0
 # CHECK-NEXT:     Offset: 0x100C
-# CHECK-NEXT:     Size: 26
+# CHECK-NEXT:     Size: 34
 # CHECK-NEXT:     Link: 0
 # CHECK-NEXT:     Info: 0
 # CHECK-NEXT:     AddressAlignment: 1
@@ -119,8 +133,8 @@ _start:
 # CHECK-NEXT:     Offset: 0x0
 # CHECK-NEXT:     VirtualAddress: 0x400000
 # CHECK-NEXT:     PhysicalAddress: 0x400000
-# CHECK-NEXT:     FileSize: 4336
-# CHECK-NEXT:     MemSize: 4336
+# CHECK-NEXT:     FileSize: 4384
+# CHECK-NEXT:     MemSize: 4384
 # CHECK-NEXT:     Flags [ (0x5)
 # CHECK-NEXT:       PF_R (0x4)
 # CHECK-NEXT:       PF_X (0x1)

Modified: lld/trunk/test/elf2/basic64be.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/basic64be.s?rev=245008&r1=245007&r2=245008&view=diff
==============================================================================
--- lld/trunk/test/elf2/basic64be.s (original)
+++ lld/trunk/test/elf2/basic64be.s Thu Aug 13 21:42:20 2015
@@ -30,15 +30,15 @@ _start:
 # CHECK-NEXT:   Version: 1
 # CHECK-NEXT:   Entry: 0x401000
 # CHECK-NEXT:   ProgramHeaderOffset: 0x40
-# CHECK-NEXT:   SectionHeaderOffset: 0x1048
+# CHECK-NEXT:   SectionHeaderOffset: 0x1050
 # CHECK-NEXT:   Flags [ (0x0)
 # CHECK-NEXT:   ]
 # CHECK-NEXT:   HeaderSize: 64
 # CHECK-NEXT:   ProgramHeaderEntrySize: 56
 # CHECK-NEXT:   ProgramHeaderCount: 1
 # CHECK-NEXT:   SectionHeaderEntrySize: 64
-# CHECK-NEXT:   SectionHeaderCount: 6
-# CHECK-NEXT:    StringTableSectionIndex: 5
+# CHECK-NEXT:   SectionHeaderCount: 7
+# CHECK-NEXT:    StringTableSectionIndex: 6
 # CHECK-NEXT: }
 # CHECK-NEXT: Sections [
 # CHECK-NEXT:   Section {
@@ -121,13 +121,27 @@ _start:
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Section {
 # CHECK-NEXT:     Index: 5
+# CHECK-NEXT:     Name: .symtab
+# CHECK-NEXT:     Type: SHT_SYMTAB
+# CHECK-NEXT:     Flags [
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Address: 0x0
+# CHECK-NEXT:     Offset: 0x1024
+# CHECK-NEXT:     Size: 0
+# CHECK-NEXT:     Link: 6
+# CHECK-NEXT:     Info: 1
+# CHECK-NEXT:     AddressAlignment: 8
+# CHECK-NEXT:     EntrySize: 24
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Section {
+# CHECK-NEXT:     Index: 6
 # CHECK-NEXT:     Name: .strtab
 # CHECK-NEXT:     Type: SHT_STRTAB (0x3)
 # CHECK-NEXT:     Flags [ (0x0)
 # CHECK-NEXT:     ]
 # CHECK-NEXT:     Address: 0x0
 # CHECK-NEXT:     Offset: 0x1024
-# CHECK-NEXT:     Size: 31
+# CHECK-NEXT:     Size: 39
 # CHECK-NEXT:     Link: 0
 # CHECK-NEXT:     Info: 0
 # CHECK-NEXT:     AddressAlignment: 1
@@ -140,8 +154,8 @@ _start:
 # CHECK-NEXT:     Offset: 0x0
 # CHECK-NEXT:     VirtualAddress: 0x400000
 # CHECK-NEXT:     PhysicalAddress: 0x400000
-# CHECK-NEXT:     FileSize: 4552
-# CHECK-NEXT:     MemSize: 4552
+# CHECK-NEXT:     FileSize: 4624
+# CHECK-NEXT:     MemSize: 4624
 # CHECK-NEXT:     Flags [ (0x5)
 # CHECK-NEXT:       PF_R (0x4)
 # CHECK-NEXT:       PF_X (0x1)

Modified: lld/trunk/test/elf2/string-table.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/string-table.s?rev=245008&r1=245007&r2=245008&view=diff
==============================================================================
--- lld/trunk/test/elf2/string-table.s (original)
+++ lld/trunk/test/elf2/string-table.s Thu Aug 13 21:42:20 2015
@@ -52,14 +52,14 @@ _start:
 // CHECK-NEXT:   ]
 // CHECK-NEXT:   Address: 0x0
 // CHECK-NEXT:   Offset:
-// CHECK-NEXT:   Size: 33
+// CHECK-NEXT:   Size: 41
 // CHECK-NEXT:   Link: 0
 // CHECK-NEXT:   Info: 0
 // CHECK-NEXT:   AddressAlignment: 1
 // CHECK-NEXT:   EntrySize: 0
 // CHECK-NEXT:   SectionData (
 // CHECK-NEXT:     0000: 002E7465 7874002E 62737300 666F6F62  |..text..bss.foob|
-// CHECK-NEXT:     0010: 6172002E 73747274 6162002E 64617461  |ar..strtab..data|
-// CHECK-NEXT:     0020: 00                                   |.|
+// CHECK-NEXT:     0010: 6172002E 73747274 6162002E 73796D74  |ar..strtab..symt|
+// CHECK-NEXT:     0020: 6162002E 64617461 00                 |ab..data.|
 // CHECK-NEXT:   )
 // CHECK-NEXT: }

Added: lld/trunk/test/elf2/symbols.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/symbols.s?rev=245008&view=auto
==============================================================================
--- lld/trunk/test/elf2/symbols.s (added)
+++ lld/trunk/test/elf2/symbols.s Thu Aug 13 21:42:20 2015
@@ -0,0 +1,10 @@
+// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+// RUN: lld -flavor gnu2 %t -o %t2
+// RUN: llvm-readobj -symbols %t2 | FileCheck %s
+// REQUIRES: x86
+
+.globl _start;
+_start:
+
+// CHECK: Symbols [
+// CHECK-NEXT: ]




More information about the llvm-commits mailing list