OSDN Git Service

[yaml2obj][ELF] Make symbol table top-level key.
authorSean Silva <silvas@purdue.edu>
Sat, 22 Jun 2013 01:38:00 +0000 (01:38 +0000)
committerSean Silva <silvas@purdue.edu>
Sat, 22 Jun 2013 01:38:00 +0000 (01:38 +0000)
Although in reality the symbol table in ELF resides in a section, the
standard requires that there be no more than one SHT_SYMTAB. To enforce
this constraint, it is cleaner to group all the symbols under a
top-level `Symbols` key on the object file.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@184627 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Object/ELFYAML.h
lib/Object/ELFYAML.cpp
test/Object/yaml2obj-elf-symbol-LocalGlobalWeak.yaml
test/Object/yaml2obj-elf-symbol-basic.yaml
tools/yaml2obj/yaml2elf.cpp

index b6c7556..fca965f 100644 (file)
@@ -72,12 +72,15 @@ struct Section {
   object::yaml::BinaryRef Content;
   StringRef Link;
   llvm::yaml::Hex64 AddressAlign;
-  // For SHT_SYMTAB; should be empty otherwise.
-  LocalGlobalWeakSymbols Symbols;
 };
 struct Object {
   FileHeader Header;
   std::vector<Section> Sections;
+  // Although in reality the symbols reside in a section, it is a lot
+  // cleaner and nicer if we read them from the YAML as a separate
+  // top-level key, which automatically ensures that invariants like there
+  // being a single SHT_SYMTAB section are upheld.
+  LocalGlobalWeakSymbols Symbols;
 };
 
 } // end namespace ELFYAML
index 107333c..a331302 100644 (file)
@@ -244,7 +244,8 @@ void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
   ECase(SHT_NULL)
   ECase(SHT_PROGBITS)
-  ECase(SHT_SYMTAB)
+  // No SHT_SYMTAB. Use the top-level `Symbols` key instead.
+  // FIXME: Issue a diagnostic with this information.
   ECase(SHT_STRTAB)
   ECase(SHT_RELA)
   ECase(SHT_HASH)
@@ -326,17 +327,12 @@ void MappingTraits<ELFYAML::Section>::mapping(IO &IO,
   IO.mapOptional("Content", Section.Content);
   IO.mapOptional("Link", Section.Link);
   IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
-  // TODO: Error if `Type` is SHT_SYMTAB and this is not present, or if
-  // `Type` is *not* SHT_SYMTAB and this *is* present. (By SHT_SYMTAB I
-  // also mean SHT_DYNSYM, but for simplicity right now we just do
-  // SHT_SYMTAB). Want to be able to share the predicate with consumers of
-  // this structure.
-  IO.mapOptional("Symbols", Section.Symbols);
 }
 
 void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) {
   IO.mapRequired("FileHeader", Object.Header);
   IO.mapOptional("Sections", Object.Sections);
+  IO.mapOptional("Symbols", Object.Symbols);
 }
 
 } // end namespace yaml
index 44479dc..3c4e830 100644 (file)
@@ -10,21 +10,19 @@ Sections:
     Type: SHT_PROGBITS
     Flags: [ SHF_ALLOC, SHF_WRITE ]
     Content: "DEADBEEF"
-  - Name: .symtab
-    Type: SHT_SYMTAB
-    Symbols:
-      Local:
-        - Name: local_symbol
-          Type: STT_OBJECT
-          Section: .data
-      Global:
-        - Name: global_symbol
-          Type: STT_OBJECT
-          Section: .data
-      Weak:
-        - Name: weak_symbol
-          Type: STT_OBJECT
-          Section: .data
+Symbols:
+  Local:
+    - Name: local_symbol
+      Type: STT_OBJECT
+      Section: .data
+  Global:
+    - Name: global_symbol
+      Type: STT_OBJECT
+      Section: .data
+  Weak:
+    - Name: weak_symbol
+      Type: STT_OBJECT
+      Section: .data
 
 # CHECK: Symbol {
 # CHECK:   Name:  (0)
index d54e2f0..3fb9b17 100644 (file)
@@ -16,16 +16,14 @@ Sections:
                       # This YAML file is a valid relocatable object that,
                       # when linked and run on x86_64, will go into an
                       # infloop.
-  - Name: .symtab
-    Type: SHT_SYMTAB
-    Symbols:
-      Global:
-        - Name: main
-          Type: STT_FUNC
-          Section: .text
-          Value: 0x1
-          Size: 2
-        - Name: undefined_symbol
+Symbols:
+  Global:
+    - Name: main
+      Type: STT_FUNC
+      Section: .text
+      Value: 0x1
+      Size: 2
+    - Name: undefined_symbol
 
 # CHECK:      Symbols [
 # CHECK-NEXT:   Symbol {
index 82ff787..61252a4 100644 (file)
@@ -216,8 +216,7 @@ static void handleSymtabSectionHeader(
     typename object::ELFObjectFile<ELFT>::Elf_Shdr &SHeader) {
 
   typedef typename object::ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
-  // TODO: Ensure that a manually specified `Link` field is diagnosed as an
-  // error for SHT_SYMTAB.
+  SHeader.sh_type = ELF::SHT_SYMTAB;
   SHeader.sh_link = State.getDotStrTabSecNo();
   // One greater than symbol table index of the last local symbol.
   SHeader.sh_info = Symbols.Local.size() + 1;
@@ -272,11 +271,12 @@ static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
   // Immediately following the ELF header.
   Header.e_shoff = sizeof(Header);
   const std::vector<ELFYAML::Section> &Sections = Doc.Sections;
-  // "+ 3" for
+  // "+ 4" for
   // - SHT_NULL entry (placed first, i.e. 0'th entry)
+  // - symbol table (.symtab) (placed third to last)
   // - string table (.strtab) (placed second to last)
   // - section header string table. (placed last)
-  Header.e_shnum = Sections.size() + 3;
+  Header.e_shnum = Sections.size() + 4;
   // Place section header string table last.
   Header.e_shstrndx = Header.e_shnum - 1;
   const unsigned DotStrtabSecNo = Header.e_shnum - 2;
@@ -334,14 +334,16 @@ static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
     SHeader.sh_info = 0;
     SHeader.sh_addralign = Sec.AddressAlign;
     SHeader.sh_entsize = 0;
-    // XXX: Really ugly right now. Should not be writing to `CBA` above
-    // (and setting sh_offset and sh_size) when going through this branch
-    // here.
-    if (Sec.Type == ELFYAML::ELF_SHT(SHT_SYMTAB))
-      handleSymtabSectionHeader<ELFT>(Sec.Symbols, State, SHeader);
     SHeaders.push_back(SHeader);
   }
 
+  // .symtab section.
+  Elf_Shdr SymtabSHeader;
+  zero(SymtabSHeader);
+  SymtabSHeader.sh_name = SHStrTab.addString(StringRef(".symtab"));
+  handleSymtabSectionHeader<ELFT>(Doc.Symbols, State, SymtabSHeader);
+  SHeaders.push_back(SymtabSHeader);
+
   // .strtab string table header.
   Elf_Shdr DotStrTabSHeader;
   zero(DotStrTabSHeader);