[yaml2obj][obj2yaml] - Do not create a symbol table by default.
authorGeorge Rimar <grimar@accesssoftek.com>
Sun, 20 Oct 2019 14:47:17 +0000 (14:47 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Sun, 20 Oct 2019 14:47:17 +0000 (14:47 +0000)
This patch tries to resolve problems faced in D68943
and uses some of the code written by Konrad Wilhelm Kleine
in that patch.

Previously, yaml2obj tool always created a .symtab section.
This patch changes that. With it we only create it when
have a "Symbols:" tag in the YAML document or when
we need to create it because it is used by another section(s).

obj2yaml follows the new behavior and does not print "Symbols:"
anymore when there is no symbol table.

Differential revision: https://reviews.llvm.org/D69041

llvm-svn: 375361

62 files changed:
llvm/include/llvm/ObjectYAML/ELFYAML.h
llvm/lib/ObjectYAML/ELFEmitter.cpp
llvm/test/Object/invalid.test
llvm/test/Object/objdump-sectionheaders.test
llvm/test/tools/llvm-objcopy/ELF/add-section-remove.test
llvm/test/tools/llvm-objcopy/ELF/add-section.test
llvm/test/tools/llvm-objcopy/ELF/add-symbol.test
llvm/test/tools/llvm-objcopy/ELF/basic-only-section.test
llvm/test/tools/llvm-objcopy/ELF/explicit-keep-remove.test
llvm/test/tools/llvm-objcopy/ELF/explicit-only-section-remove.test
llvm/test/tools/llvm-objcopy/ELF/invalid-e_shoff.test
llvm/test/tools/llvm-objcopy/ELF/keep-only-section.test
llvm/test/tools/llvm-objcopy/ELF/no-strip-all.test
llvm/test/tools/llvm-objcopy/ELF/no-symbol-relocation.test
llvm/test/tools/llvm-objcopy/ELF/null-symbol.test
llvm/test/tools/llvm-objcopy/ELF/only-section-many.test
llvm/test/tools/llvm-objcopy/ELF/preserve-segment-contents-ehdr-phdrs.test
llvm/test/tools/llvm-objcopy/ELF/remove-section.test
llvm/test/tools/llvm-objcopy/ELF/rename-section-multiple.test
llvm/test/tools/llvm-objcopy/ELF/rename-section.test
llvm/test/tools/llvm-objcopy/ELF/segment-shift-section-remove.test
llvm/test/tools/llvm-objcopy/ELF/segment-test-remove-section.test
llvm/test/tools/llvm-objcopy/ELF/shstrtab-optimize.test
llvm/test/tools/llvm-objcopy/ELF/strip-unneeded-remove-debug-keep-link.test
llvm/test/tools/llvm-objcopy/ELF/symtab-error-on-remove-strtab.test
llvm/test/tools/llvm-objcopy/ELF/symtab-link.test
llvm/test/tools/llvm-objcopy/ELF/wildcard-syntax.test
llvm/test/tools/llvm-objdump/full-contents.test
llvm/test/tools/llvm-objdump/relocations-elf.test
llvm/test/tools/llvm-readobj/check-output-order.test
llvm/test/tools/llvm-readobj/elf-dynamic-malformed.test
llvm/test/tools/llvm-readobj/elf-file-headers.test
llvm/test/tools/llvm-readobj/elf-hidden-versym.test
llvm/test/tools/llvm-readobj/elf-invalid-shstrndx.test
llvm/test/tools/llvm-readobj/elf-invalid-versioning.test
llvm/test/tools/llvm-readobj/elf-no-phdrs.test
llvm/test/tools/llvm-readobj/elf-no-relocs.test
llvm/test/tools/llvm-readobj/elf-relr-relocs.test
llvm/test/tools/llvm-readobj/elf-versioninfo.test
llvm/test/tools/llvm-readobj/elf-wrong-shstrtab-type.test
llvm/test/tools/llvm-readobj/reloc-types-elf-aarch64.test
llvm/test/tools/llvm-readobj/reloc-types-elf-arm.test
llvm/test/tools/llvm-readobj/reloc-types-elf-lanai.test
llvm/test/tools/llvm-readobj/reloc-types-elf-mips.test
llvm/test/tools/llvm-readobj/reloc-types-elf-mips64.test
llvm/test/tools/obj2yaml/elf-llvm-addrsig-section.yaml
llvm/test/tools/obj2yaml/elf-no-symtab.yaml [new file with mode: 0644]
llvm/test/tools/obj2yaml/elf-sht-symtab-shndx.yaml
llvm/test/tools/obj2yaml/no-symbol-reloc.test
llvm/test/tools/obj2yaml/section-group.test
llvm/test/tools/yaml2obj/dynamic-symbols.yaml
llvm/test/tools/yaml2obj/elf-comdat-broken-info.yaml
llvm/test/tools/yaml2obj/elf-custom-null-section.yaml
llvm/test/tools/yaml2obj/elf-header-sh-fields.yaml
llvm/test/tools/yaml2obj/elf-sht-symtab-shndx.yaml
llvm/test/tools/yaml2obj/implicit-sections-types.test
llvm/test/tools/yaml2obj/implicit-sections.test
llvm/test/tools/yaml2obj/invalid-symboless-relocation.yaml
llvm/test/tools/yaml2obj/reloc-sec-info.yaml
llvm/test/tools/yaml2obj/symboless-relocation.yaml
llvm/test/tools/yaml2obj/symtab-implicit-sections-flags.yaml
llvm/tools/obj2yaml/elf2yaml.cpp

index ef2b4fb..0898a0e 100644 (file)
@@ -376,7 +376,7 @@ struct Object {
   // 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.
-  std::vector<Symbol> Symbols;
+  Optional<std::vector<Symbol>> Symbols;
   std::vector<Symbol> DynamicSymbols;
 };
 
index f9c31f3..e0faed2 100644 (file)
@@ -200,10 +200,17 @@ template <class ELFT>
 ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH)
     : Doc(D), ErrHandler(EH) {
   StringSet<> DocSections;
-  for (std::unique_ptr<ELFYAML::Section> &D : Doc.Sections)
+  for (std::unique_ptr<ELFYAML::Section> &D : Doc.Sections) {
     if (!D->Name.empty())
       DocSections.insert(D->Name);
 
+    // Some sections wants to link to .symtab by default.
+    // That means we want to create the symbol table for them.
+    if (D->Type == llvm::ELF::SHT_REL || D->Type == llvm::ELF::SHT_RELA)
+      if (!Doc.Symbols && D->Link.empty())
+        Doc.Symbols.emplace();
+  }
+
   // Insert SHT_NULL section implicitly when it is not defined in YAML.
   if (Doc.Sections.empty() || Doc.Sections.front()->Type != ELF::SHT_NULL)
     Doc.Sections.insert(
@@ -211,7 +218,11 @@ ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH)
         std::make_unique<ELFYAML::Section>(
             ELFYAML::Section::SectionKind::RawContent, /*IsImplicit=*/true));
 
-  std::vector<StringRef> ImplicitSections = {".symtab", ".strtab", ".shstrtab"};
+  std::vector<StringRef> ImplicitSections;
+  if (Doc.Symbols)
+    ImplicitSections.push_back(".symtab");
+  ImplicitSections.insert(ImplicitSections.end(), {".strtab", ".shstrtab"});
+
   if (!Doc.DynamicSymbols.empty())
     ImplicitSections.insert(ImplicitSections.end(), {".dynsym", ".dynstr"});
 
@@ -508,7 +519,11 @@ void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
                                              ELFYAML::Section *YAMLSec) {
 
   bool IsStatic = STType == SymtabType::Static;
-  const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols;
+  ArrayRef<ELFYAML::Symbol> Symbols;
+  if (IsStatic && Doc.Symbols)
+    Symbols = *Doc.Symbols;
+  else if (!IsStatic)
+    Symbols = Doc.DynamicSymbols;
 
   ELFYAML::RawContentSection *RawSec =
       dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec);
@@ -1044,14 +1059,16 @@ template <class ELFT> void ELFState<ELFT>::buildSymbolIndexes() {
     }
   };
 
-  Build(Doc.Symbols, SymN2I);
+  if (Doc.Symbols)
+    Build(*Doc.Symbols, SymN2I);
   Build(Doc.DynamicSymbols, DynSymN2I);
 }
 
 template <class ELFT> void ELFState<ELFT>::finalizeStrings() {
   // Add the regular symbol names to .strtab section.
-  for (const ELFYAML::Symbol &Sym : Doc.Symbols)
-    DotStrtab.add(ELFYAML::dropUniqueSuffix(Sym.Name));
+  if (Doc.Symbols)
+    for (const ELFYAML::Symbol &Sym : *Doc.Symbols)
+      DotStrtab.add(ELFYAML::dropUniqueSuffix(Sym.Name));
   DotStrtab.finalize();
 
   // Add the dynamic symbol names to .dynstr section.
index cbf770a..56372bf 100644 (file)
@@ -226,6 +226,7 @@ Sections:
     Type: SHT_SYMTAB_SHNDX
     Entries: [ 0, 1 ]
     Link: .symtab
+Symbols: []
 
 ## Check that llvm-readobj reports an error if the e_phentsize field is broken.
 
@@ -274,8 +275,8 @@ Sections:
     Type:     SHT_RELA
     ShOffset: 0x10000
 
-## Check that llvm-objdump reports an error when .shstrtab has a broken sh_offset
-## so large that sh_offset + sh_size overflows the platform address size type.
+## Check that llvm-objdump reports an error when we try to print symbols and
+## .shstrtab has a broken sh_offset so large that sh_offset + sh_size overflows the platform address size type.
 
 # RUN: yaml2obj %s --docnum=14 -o %t14
 # RUN: not llvm-readobj --symbols %t14 2>&1 | FileCheck -DFILE=%t14 --check-prefix=INVALID-SECTION-SIZE2 %s
@@ -292,6 +293,7 @@ Sections:
   - Name:     .shstrtab
     Type:     SHT_STRTAB
     ShOffset: 0xFFFFFFFF
+Symbols: []
 
 ## Check that llvm-readobj reports an error when trying to dump sections
 ## when the e_shnum field is broken (is greater than the actual number of sections).
@@ -565,7 +567,7 @@ Sections:
 # RUN: yaml2obj --docnum=26 %s -o %t26
 # RUN: not llvm-readobj -h %t26 2>&1 | FileCheck -DFILE=%t26 --check-prefix=INVALID-SEC-NUM1 %s
 
-# INVALID-SEC-NUM1: error: '[[FILE]]': invalid section header table offset (e_shoff = 0x78) or invalid number of sections specified in the first section header's sh_size field (0x3ffffffffffffff)
+# INVALID-SEC-NUM1: error: '[[FILE]]': invalid section header table offset (e_shoff = 0x58) or invalid number of sections specified in the first section header's sh_size field (0x3ffffffffffffff)
 
 --- !ELF
 FileHeader:
index 434d73e..fba4fa7 100644 (file)
@@ -37,6 +37,5 @@ Sections:
   - Name:    .rela.text
     Type:    SHT_RELA
     Address: 0x0000000000000038
-    Link:    .symtab
     Info:    .text
     Relocations:
index fe462db..ad41d74 100644 (file)
@@ -23,11 +23,10 @@ Sections:
     Flags:           [ SHF_ALLOC ]
     Content:        "32323232"
 
-# CHECK: SectionHeaderCount: 7
+# CHECK: SectionHeaderCount: 6
 
 # CHECK: Name: .test1
 # CHECK: Name: .test3
-# CHECK: Name: .symtab
 # CHECK: Name: .strtab
 # CHECK: Name: .shstrtab
 # CHECK: Name: .test2
index 4acbd9a..e930d27 100644 (file)
@@ -24,11 +24,10 @@ Sections:
     Flags:           [ SHF_ALLOC ]
     Content:        "32323232"
 
-# CHECK: SectionHeaderCount: 7
+# CHECK: SectionHeaderCount: 6
 
 # CHECK: Name: .test1
 # CHECK: Name: .test3
-# CHECK: Name: .symtab
 # CHECK: Name: .strtab
 # CHECK: Name: .shstrtab
 # CHECK: Name: .test2
index fd83860..a9002d4 100644 (file)
@@ -58,6 +58,9 @@ ProgramHeaders:
     Align: 0x1000
     Sections:
       - Section: .data
+## TODO (grimar): llvm-objcopy seems produce a broken output without
+## the following line, i.e. when there is no symbol table in the input.
+Symbols: []
 
 # CHECK:      0: 00000000 0 NOTYPE LOCAL DEFAULT UND
 # CHECK-NEXT: 1: 00000001 0 NOTYPE GLOBAL DEFAULT ABS abs1
index e47a97a..f95ea38 100644 (file)
@@ -15,9 +15,7 @@ Sections:
     Type:            SHT_PROGBITS
     Flags:           [ ]
 
-# CHECK: SectionHeaderCount: 5
+# CHECK: SectionHeaderCount: 3
 
 # CHECK:     Name: .test
-# CHECK:     Name: .symtab
-# CHECK:     Name: .strtab
 # CHECK:     Name: .shstrtab
index 98ad3ae..6512afa 100644 (file)
@@ -13,9 +13,8 @@ Sections:
     Type:            SHT_PROGBITS
     Flags:           [ ]
 
-# CHECK: SectionHeaderCount: 5
+# CHECK: SectionHeaderCount: 4
 
 # CHECK:     Name: .test
-# CHECK:     Name: .symtab
 # CHECK:     Name: .strtab
 # CHECK:     Name: .shstrtab
index 8a152e5..5baf845 100644 (file)
@@ -13,9 +13,7 @@ Sections:
     Type:            SHT_PROGBITS
     Flags:           [ ]
 
-# CHECK: SectionHeaderCount: 5
+# CHECK: SectionHeaderCount: 3
 
 # CHECK:     Name: .test
-# CHECK:     Name: .symtab
-# CHECK:     Name: .strtab
 # CHECK:     Name: .shstrtab
index fc2e084..9d50922 100644 (file)
@@ -28,5 +28,6 @@ FileHeader:
 Sections:
   - Name: .foo
     Type: SHT_PROGBITS
+Symbols: []
 
 # CASE2: error: '[[INPUT]]': section header table goes past the end of the file: e_shoff = 0x40000000
index 7ea8468..75b5bbd 100644 (file)
@@ -20,19 +20,15 @@ Sections:
   - Name:            .test3
     Type:            SHT_PROGBITS
 
-# CHECK: SectionHeaderCount: 6
+# CHECK: SectionHeaderCount: 4
 
 # CHECK:     Name: .test
 # CHECK:     Name: .test2
-# CHECK:     Name: .symtab
-# CHECK:     Name: .strtab
 # CHECK:     Name: .shstrtab
 
-# REGEX: SectionHeaderCount: 7
+# REGEX: SectionHeaderCount: 5
 
 # REGEX:     Name: .test
 # REGEX:     Name: .test2
 # REGEX:     Name: .test3
-# REGEX:     Name: .symtab
-# REGEX:     Name: .strtab
 # REGEX:     Name: .shstrtab
index a0158d4..90b4c15 100644 (file)
@@ -37,6 +37,7 @@ Sections:
   - Name:  .alloc
     Type:  SHT_PROGBITS
     Flags: [ SHF_ALLOC ]
+Symbols: []
 
 # ALL: SectionHeaderCount: 3
 # ALL: Name: .alloc
index 9def536..4b13dda 100644 (file)
@@ -17,7 +17,6 @@ Sections:
     Content:         "0000000000000000"
   - Name:            .rel.text
     Type:            SHT_REL
-    Link:            .symtab
     Info:            .text
     Relocations:
       - Offset: 0x1000
index 94f5ab5..b7ac3e8 100644 (file)
@@ -15,6 +15,7 @@ Sections:
     Address:         0x1000
     AddressAlign:    0x0000000000000010
     Size:            8
+Symbols: []
 
 #CHECK: Symbols [
 #CHECK-NEXT:  Symbol {
index 43ccff1..9f1f77d 100644 (file)
@@ -19,10 +19,8 @@ Sections:
     Type:            SHT_PROGBITS
     Flags:           [ ]
 
-# CHECK: SectionHeaderCount: 6
+# CHECK: SectionHeaderCount: 4
 
 # CHECK:     Name: .test1
 # CHECK:     Name: .test2
-# CHECK:     Name: .symtab
-# CHECK:     Name: .strtab
 # CHECK:     Name: .shstrtab
index 8f8b98e..927485a 100644 (file)
@@ -30,6 +30,7 @@ Sections:
   - Name:            .test3
     Type:            SHT_PROGBITS
     Flags:           [ ]
+Symbols: []
 
 # CHECK: SectionHeaderCount: 6
 
index e614f65..c508ac8 100644 (file)
@@ -22,7 +22,7 @@ Sections:
     Flags:           [ SHF_ALLOC ]
     Content:        "37373737"
 
-# CHECK: SectionHeaderCount: 7
+# CHECK: SectionHeaderCount: 6
 
 # CHECK: Name: .test2
 # CHECK: SectionData (
@@ -36,6 +36,5 @@ Sections:
 # CHECK: SectionData (
 # CHECK-NEXT:   0000: 37373737
 # CHECK-NEXT: )
-# CHECK: Name: .symtab
 # CHECK: Name: .strtab
 # CHECK: Name: .shstrtab
index 3829b5b..c8a1eaf 100644 (file)
@@ -16,13 +16,12 @@ Sections:
     Flags:           [ SHF_ALLOC ]
     Content:        "c3c3c3c3"
 
-# CHECK: SectionHeaderCount: 5
+# CHECK: SectionHeaderCount: 4
 
 # CHECK: Name: .bar
 # CHECK: SectionData (
 # CHECK-NEXT:   0000: C3C3C3C3
 # CHECK-NEXT: )
-# CHECK: Name: .symtab
 # CHECK: Name: .strtab
 # CHECK: Name: .shstrtab
 
index cef783c..9a50a10 100644 (file)
@@ -40,6 +40,9 @@ ProgramHeaders:
     PAddr: 0x3000
     Sections:
       - Section: .text3
+## TODO (grimar): without the following line (i.e. without an empty symbol table),
+## llvm-objcopy adds an empty .strtab section. It doesn't look correct.
+Symbols: []
 
 #CHECK: SectionHeaderCount: 4
 
index 60cd5f3..ca83e58 100644 (file)
@@ -42,6 +42,9 @@ ProgramHeaders:
       - Section: .text
       - Section: .text2
       - Section: .text3
+## TODO (grimar): without the following line (i.e. without an empty symbol table),
+## llvm-objcopy adds an empty .strtab section. It doesn't look correct.
+Symbols: []
 
 # Make sure that when we remove a section we overwrite it with zeros
 # DATA: {{^[^[:blank:]]+}} 00 00 00 00
index 24b546c..8b1da21 100644 (file)
@@ -11,7 +11,7 @@
 # CHECK-NEXT: ]
 # CHECK-NEXT: Address:
 # CHECK-NEXT: Offset:
-# CHECK-NEXT: Size: 36
+# CHECK-NEXT: Size: 28
 
 !ELF
 FileHeader:
index b6a72f9..948bfea 100644 (file)
@@ -20,11 +20,10 @@ Sections:
   - Name:            .debugfoo
     Type:            SHT_PROGBITS
 
-# CHECK: There are 6 section headers
+# CHECK: There are 5 section headers
 
 # CHECK:        [ 0]
 # CHECK-NEXT:   [ 1] .text
-# CHECK-NEXT:   [ 2] .symtab
-# CHECK-NEXT:   [ 3] .strtab
-# CHECK-NEXT:   [ 4] .shstrtab
-# CHECK-NEXT:   [ 5] .gnu_debuglink
+# CHECK-NEXT:   [ 2] .strtab
+# CHECK-NEXT:   [ 3] .shstrtab
+# CHECK-NEXT:   [ 4] .gnu_debuglink
index 7c9a460..4aac930 100644 (file)
@@ -9,6 +9,7 @@ FileHeader:
   Data:            ELFDATA2LSB
   Type:            ET_REL
   Machine:         EM_X86_64
+Symbols: []
 
 # ERR1: error: '[[INPUT]]': string table '.strtab' cannot be removed because it is referenced by the symbol table '.symtab'
 # ERR2: error: '[[INPUT]]': string table '.strtab' cannot be removed because it is referenced by the symbol table '.symtab'
index 68b8f78..8297529 100644 (file)
@@ -16,6 +16,7 @@ Sections:
     Link:            .symtab
     Type:            SHT_PROGBITS
     Flags:           [ ]
+Symbols: []
 
 # CHECK:      Name: .foo
 # CHECK-NEXT: Type:
index 0564289..685ba16 100644 (file)
@@ -57,6 +57,7 @@ Sections:
     Type: SHT_PROGBITS
   - Name: .bar
     Type: SHT_PROGBITS
+Symbols: []
 
 ## Use a separate test file with special characters for the following tests.
 
@@ -128,6 +129,7 @@ Sections:
     Type: SHT_PROGBITS
   - Name: .foo
     Type: SHT_PROGBITS
+Symbols: []
 
 # WARN: warning: invalid glob pattern: ][]
 
index e4939f2..29cc9fd 100644 (file)
@@ -64,3 +64,4 @@ Sections:
     Type:            SHT_PROGBITS
     Flags:           [ ]
     Content:         '11112222'
+Symbols: []
index 87fe55c..b79ab9e 100644 (file)
@@ -86,7 +86,6 @@ FileHeader:
 Sections:
 - Name: .rela.foo
   Type: SHT_RELA
-  Link: .symtab
   Info: 0x255
   Relocations:
     - Offset: 0x1
index 7097d7f..36fce3a 100644 (file)
@@ -39,6 +39,7 @@ Sections:
     Entries:
       - Tag: DT_NULL
         Value: 0
+Symbols: []
 ProgramHeaders:
   - Type: PT_DYNAMIC
     Sections:
index afdcbe6..1a31fa3 100644 (file)
@@ -34,6 +34,7 @@ Sections:
     Type:    SHT_DYNAMIC
     Address: 0x1000
     Content: "01234567"
+Symbols: []
 ProgramHeaders:
   - Type: PT_LOAD
     VAddr: 0x1000
@@ -76,6 +77,7 @@ Sections:
         Value: 0
       - Tag:   DT_NULL
         Value: 0
+Symbols: []
 ProgramHeaders:
   - Type: PT_LOAD
     VAddr: 0x1000
@@ -143,6 +145,7 @@ Sections:
         Value: 1
       - Tag:   DT_NULL
         Value: 0
+Symbols: []
 ProgramHeaders:
   - Type: PT_LOAD
     VAddr: 0x1000
@@ -189,6 +192,7 @@ Sections:
         Value: 1
       - Tag:   DT_NULL
         Value: 0x0
+Symbols: []
 ProgramHeaders:
   - Type: PT_LOAD
     VAddr: 0x1000
@@ -231,6 +235,7 @@ Sections:
         Value: 0x1000000
       - Tag:   DT_NULL
         Value: 0x0
+Symbols: []
 ProgramHeaders:
   - Type: PT_LOAD
     VAddr: 0x1000
index ce7711c..1cb3563 100644 (file)
 # I386-NEXT:  Version: 1
 # I386-NEXT:  Entry: 0x0
 # I386-NEXT:  ProgramHeaderOffset: 0x0
-# I386-NEXT:  SectionHeaderOffset: 0x64
+# I386-NEXT:  SectionHeaderOffset: 0x48
 # I386-NEXT:  Flags [ (0x0)
 # I386-NEXT:  ]
 # I386-NEXT:  HeaderSize: 52
 # I386-NEXT:  ProgramHeaderEntrySize: 0
 # I386-NEXT:  ProgramHeaderCount: 0
 # I386-NEXT:  SectionHeaderEntrySize: 40
-# I386-NEXT:  SectionHeaderCount: 4
-# I386-NEXT:  StringTableSectionIndex: 3
+# I386-NEXT:  SectionHeaderCount: 3
+# I386-NEXT:  StringTableSectionIndex: 2
 # I386-NEXT:}
 # I386-NOT:{{.}}
 
@@ -65,15 +65,15 @@ FileHeader:
 # X86-64-NEXT:  Version: 1
 # X86-64-NEXT:  Entry: 0x0
 # X86-64-NEXT:  ProgramHeaderOffset: 0x0
-# X86-64-NEXT:  SectionHeaderOffset: 0x78
+# X86-64-NEXT:  SectionHeaderOffset: 0x58
 # X86-64-NEXT:  Flags [ (0x0)
 # X86-64-NEXT:  ]
 # X86-64-NEXT:  HeaderSize: 64
 # X86-64-NEXT:  ProgramHeaderEntrySize: 0
 # X86-64-NEXT:  ProgramHeaderCount: 0
 # X86-64-NEXT:  SectionHeaderEntrySize: 64
-# X86-64-NEXT:  SectionHeaderCount: 4
-# X86-64-NEXT:  StringTableSectionIndex: 3
+# X86-64-NEXT:  SectionHeaderCount: 3
+# X86-64-NEXT:  StringTableSectionIndex: 2
 # X86-64-NEXT:}
 # X86-64-NOT:{{.}}
 
@@ -116,14 +116,14 @@ FileHeader:
 # LANAI-NEXT:  Version: 1
 # LANAI-NEXT:  Entry: 0x0
 # LANAI-NEXT:  ProgramHeaderOffset: 0x0
-# LANAI-NEXT:  SectionHeaderOffset: 0x64
+# LANAI-NEXT:  SectionHeaderOffset: 0x48
 # LANAI-NEXT:  Flags [ (0x0)
 # LANAI-NEXT:  ]
 # LANAI-NEXT:  HeaderSize: 52
 # LANAI-NEXT:  ProgramHeaderEntrySize: 0
 # LANAI-NEXT:  ProgramHeaderCount: 0
 # LANAI-NEXT:  SectionHeaderEntrySize: 40
-# LANAI-NEXT:  SectionHeaderCount: 4
-# LANAI-NEXT:  StringTableSectionIndex: 3
+# LANAI-NEXT:  SectionHeaderCount: 3
+# LANAI-NEXT:  StringTableSectionIndex: 2
 # LANAI-NEXT:}
 # LANAI-NOT:{{.}}
index b6cf05a..0dcee4a 100644 (file)
@@ -2,7 +2,7 @@
 # RUN: llvm-readelf -V %t | FileCheck %s --check-prefix=HIDDEN
 
 # HIDDEN:      Version symbols section '.gnu.version' contains 2 entries:
-# HIDDEN-NEXT:  Addr: 0000000000200210  Offset: 0x000040  Link: 6 (.dynsym)
+# HIDDEN-NEXT:  Addr: 0000000000200210  Offset: 0x000040  Link: 5 (.dynsym)
 # HIDDEN-NEXT:   000:   0 (*local*)       3h(hiddensym)
 
 --- !ELF
index ba89914..f57f7e8 100644 (file)
@@ -4,7 +4,7 @@
 
 # GNU:      ELF Header:
 # GNU:        Section header string table index: 255
-# GNU-NEXT:   There are 4 section headers, starting at offset 0x78:
+# GNU-NEXT:   There are 3 section headers, starting at offset 0x58:
 # GNU:      Section Headers:
 # GNU-NEXT:  [Nr] Name
 # GNU-EMPTY:
index ae1b2ad..e963d69 100644 (file)
@@ -2,7 +2,7 @@
 # RUN: llvm-readelf -V %t | FileCheck %s --check-prefix=INVALID
 
 # INVALID:      Version symbols section '.gnu.version' contains 2 entries:
-# INVALID-NEXT:  Addr: 0000000000200210  Offset: 0x000040  Link: 6 (.dynsym)
+# INVALID-NEXT:  Addr: 0000000000200210  Offset: 0x000040  Link: 5 (.dynsym)
 # INVALID-NEXT:   000:   0 (*local*)       3 (*invalid*)
 
 --- !ELF
index 4bb0067..f015695 100644 (file)
@@ -14,7 +14,7 @@
 # GNU-EMPTY:
 # GNU-NEXT:  Section to Segment mapping:
 # GNU-NEXT:    Segment Sections...
-# GNU-NEXT:     None   .symtab .strtab .shstrtab
+# GNU-NEXT:     None   .strtab .shstrtab
 
 --- !ELF
 FileHeader:
index df80b90..c632e97 100644 (file)
@@ -41,8 +41,6 @@ Sections:
   - Name: .rela.text
     Type: SHT_RELA
     Info: .text
-    Link: .symtab
   - Name: .rel.text
     Type: SHT_REL
     Info: .text
-    Link: .symtab
index 3badb73..9d3b5de 100644 (file)
@@ -91,6 +91,7 @@ Sections:
     Link:            .symtab
     AddressAlign:    0x0000000000000001
     Content:         600D0100000000000301000000000000000002000000000001050F00000000000900405005700A00
+Symbols: []
 ...
 
 # RUN: yaml2obj -docnum 2 %s \
@@ -169,4 +170,5 @@ Sections:
     Link:            .symtab
     AddressAlign:    0x00000001
     Content:         600D0100030100000000020001050F0009004050
+Symbols: []
 ...
index 46b4343..393889c 100644 (file)
@@ -164,12 +164,12 @@ DynamicSymbols:
 # LLVM-NEXT: ]
 
 # GNU:      Version symbols section '.gnu.version' contains 6 entries:
-# GNU-NEXT:  Addr: 0000000000000000  Offset: 0x000040  Link: 7 (.dynsym)
+# GNU-NEXT:  Addr: 0000000000000000  Offset: 0x000040  Link: 6 (.dynsym)
 # GNU-NEXT:   000:   0 (*local*) 2 (VERSION1) 3 (VERSION2) 4 (v1)
 # GNU-NEXT:   004:   5 (v2)      6 (v3)
 # GNU-EMPTY:
 # GNU-NEXT: Version definition section '.gnu.version_d' contains 3 entries:
-# GNU-NEXT:  Addr: 0000000000000000  Offset: 0x00004c  Link: 8 (.dynstr)
+# GNU-NEXT:  Addr: 0000000000000000  Offset: 0x00004c  Link: 7 (.dynstr)
 # GNU-NEXT:   0x0000: Rev: 1  Flags: none  Index: 2  Cnt: 1  Name: VERSION1
 # GNU-NEXT:   0x001c: Rev: 1  Flags: none  Index: 3  Cnt: 2  Name: VERSION2
 # GNU-NEXT:   0x0038: Parent 1: VERSION1
@@ -177,7 +177,7 @@ DynamicSymbols:
 # GNU-NEXT:   0x0038: Parent 1: VERSION1
 # GNU-EMPTY:
 # GNU-NEXT: Version needs section '.gnu.version_r' contains 2 entries:
-# GNU-NEXT:  Addr: 0000000000000000  Offset: 0x00008c  Link: 8 (.dynstr)
+# GNU-NEXT:  Addr: 0000000000000000  Offset: 0x00008c  Link: 7 (.dynstr)
 # GNU-NEXT:   0x0000: Version: 1  File: verneed1.so.0  Cnt: 2
 # GNU-NEXT:   0x0010:   Name: v1  Flags: none  Version: 4
 # GNU-NEXT:   0x0020:   Name: v2  Flags: none  Version: 5
index 6a600dc..cc40f49 100644 (file)
@@ -15,7 +15,7 @@
 # GNU: Section Headers:
 # GNU:   [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
 # GNU: warning: '[[FILE]]': invalid sh_type for string table section [index 1]: expected SHT_STRTAB, but got SHT_PROGBITS
-# GNU:   [ 1] .shstrtab         PROGBITS        0000000000000000 000040 00001b 00 0   0  0
+# GNU:   [ 1] .shstrtab         PROGBITS        0000000000000000 000040 000013 00 0   0  0
 
 ## Test we report multiple identical warnings (one for each object) when dumping an archive.
 
index f50668c..fdd3b97 100644 (file)
@@ -140,7 +140,6 @@ Sections:
     Content: 00
   - Name:         .rela.text
     Type:         SHT_RELA
-    Link:         .symtab
     AddressAlign: 0x0000000000000008
     EntSize:      0x0000000000000018
     Info:         .text
index 6e29637..ac6bda6 100644 (file)
@@ -149,7 +149,6 @@ Sections:
     Content: 00
   - Name:         .rel.text
     Type:         SHT_REL
-    Link:         .symtab
     AddressAlign: 0x0000000000000004
     EntSize:      0x0000000000000008
     Info:         .text
index 270e2c3..b5804c9 100644 (file)
@@ -26,7 +26,6 @@ Sections:
     Content:      00
   - Name:         .rela.text
     Type:         SHT_RELA
-    Link:         .symtab
     AddressAlign: 0x0000000000000004
     EntSize:      0x000000000000000C
     Info:         .text
index b948a3d..16dfd2f 100644 (file)
@@ -70,7 +70,6 @@ Sections:
     Content: 00
   - Name:         .rel.text
     Type:         SHT_REL
-    Link:         .symtab
     AddressAlign: 0x0000000000000004
     EntSize:      0x0000000000000008
     Info:         .text
index f040642..f1fefb2 100644 (file)
@@ -70,7 +70,6 @@ Sections:
     Content: 00
   - Name:         .rela.text
     Type:         SHT_RELA
-    Link:         .symtab
     AddressAlign: 0x0000000000000008
     EntSize:      0x0000000000000018
     Info:         .text
index 6f21c32..9519d1f 100644 (file)
@@ -62,7 +62,6 @@ Symbols:
 
 # INVALID-ENTRY:      - Name:    .llvm_addrsig
 # INVALID-ENTRY-NEXT:   Type:    SHT_LLVM_ADDRSIG
-# INVALID-ENTRY-NEXT:   Link:    .symtab
 # INVALID-ENTRY-NEXT:   Content: FFFFFFFFFF
 
 --- !ELF
@@ -83,7 +82,6 @@ Sections:
 
 # EMPTY:      - Name:    .llvm_addrsig
 # EMPTY-NEXT:   Type:    SHT_LLVM_ADDRSIG
-# EMPTY-NEXT:   Link:    .symtab
 # EMPTY-NEXT:   Symbols: []
 
 --- !ELF
diff --git a/llvm/test/tools/obj2yaml/elf-no-symtab.yaml b/llvm/test/tools/obj2yaml/elf-no-symtab.yaml
new file mode 100644 (file)
index 0000000..cab5953
--- /dev/null
@@ -0,0 +1,43 @@
+## Check that obj2yaml doesn't create a "Symbols" tag for the objects
+## without a symbol table.
+
+# RUN: yaml2obj --docnum=1 %s -o %t1
+# RUN: obj2yaml %t1 | FileCheck %s --check-prefix=NOSYMTAB
+
+# NOSYMTAB:      --- !ELF
+# NOSYMTAB-NEXT:  FileHeader:
+# NOSYMTAB-NEXT:    Class:           ELFCLASS64
+# NOSYMTAB-NEXT:    Data:            ELFDATA2LSB
+# NOSYMTAB-NEXT:    Type:            ET_DYN
+# NOSYMTAB-NEXT:    Machine:         EM_X86_64
+# NOSYMTAB-NEXT: ...
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_DYN
+  Machine: EM_X86_64
+
+## Check that obj2yaml creates a "Symbols" tag for the objects
+## that have a symbol table.
+
+# RUN: yaml2obj --docnum=2 %s -o %t2
+# RUN: obj2yaml %t2 | FileCheck %s --check-prefix=SYMTAB
+
+# SYMTAB:      --- !ELF
+# SYMTAB-NEXT:  FileHeader:
+# SYMTAB-NEXT:   Class:   ELFCLASS64
+# SYMTAB-NEXT:   Data:    ELFDATA2LSB
+# SYMTAB-NEXT:   Type:    ET_DYN
+# SYMTAB-NEXT:   Machine: EM_X86_64
+# SYMTAB-NEXT:  Symbols: []
+# SYMTAB-NEXT: ...
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_DYN
+  Machine: EM_X86_64
+Symbols: []
index e926019..735ad6e 100644 (file)
@@ -142,6 +142,7 @@ Sections:
     Type:    SHT_SYMTAB_SHNDX
     Entries: [ 0 ]
     Link:    .symtab
+Symbols: []
 
 ## Check that yaml2obj can't dump the object if SHT_SYMTAB_SHNDX is
 ## not associated with a SHT_SYMTAB section (this case is illegal).
index 351aad3..f2f1c38 100644 (file)
@@ -26,6 +26,7 @@
 # CHECK-NEXT:       - Offset: 0x0000000000000004
 # CHECK-NEXT:         Symbol: ''
 # CHECK-NEXT:         Type:   R_X86_64_NONE
+# CHECK-NEXT: Symbols: []
 # CHECK-NEXT: ...
 
 --- !ELF
index cd520cb..111bffd 100644 (file)
@@ -66,5 +66,6 @@ Sections:
     Info: 0xFF
     Members:
       - SectionOrType: GRP_COMDAT
+Symbols: []
 
 # ERR: Error reading file: {{.*}}2.o: unable to get symbol from section [index 2]: invalid symbol index (255)
index 9ecb1c2..7eb58fa 100644 (file)
@@ -52,7 +52,7 @@ DynamicSymbols:
 
 # NUM:      Name: bar
 # NUM:      Section: 
-# NUM-SAME: .symtab (0x2)
+# NUM-SAME: .strtab (0x2)
 
 # NUM: error: '[[FILE]]': invalid section index: 255
 
index 9292136..6f2f7ac 100644 (file)
@@ -12,7 +12,6 @@ FileHeader:
 Sections:
   - Name:              .group
     Type:              SHT_GROUP
-    Link:              .symtab
     Info:              12345
     Members:
       - SectionOrType: GRP_COMDAT
index 90cc6f0..e8cbb4e 100644 (file)
@@ -8,9 +8,8 @@
 # DEFAULT:      Section Headers:
 # DEFAULT-NEXT:  [Nr] Name      Type   Address          Off    Size   ES Flg Lk Inf Al
 # DEFAULT-NEXT:  [ 0]           NULL   0000000000000000 000000 000000 00 0   0  0
-# DEFAULT-NEXT:  [ 1] .symtab   SYMTAB 0000000000000000 000040 000018 18 2   1  8
-# DEFAULT-NEXT:  [ 2] .strtab   STRTAB 0000000000000000 000058 000001 00 0   0  1
-# DEFAULT-NEXT:  [ 3] .shstrtab STRTAB 0000000000000000 000059 00001b 00 0   0  1
+# DEFAULT-NEXT:  [ 1] .strtab   STRTAB 0000000000000000 000040 000001 00 0   0  1
+# DEFAULT-NEXT:  [ 2] .shstrtab STRTAB 0000000000000000 000041 000013 00 0   0  1
 
 --- !ELF
 FileHeader:
@@ -105,9 +104,8 @@ Sections:
 # OTHER-SECTION-NEXT:   [Nr] Name      Type     Address          Off    Size   ES Flg Lk Inf Al
 # OTHER-SECTION-NEXT:   [ 0]           NULL     0000000000000000 000000 000000 00 0   0  0
 # OTHER-SECTION-NEXT:   [ 1] foo       PROGBITS 0000000000000000 000040 000000 00 0   0  0
-# OTHER-SECTION-NEXT:   [ 2] .symtab   SYMTAB   0000000000000000 000040 000018 18 3   1  8
-# OTHER-SECTION-NEXT:   [ 3] .strtab   STRTAB   0000000000000000 000058 000001 00 0   0  1
-# OTHER-SECTION-NEXT:   [ 4] .shstrtab STRTAB   0000000000000000 000059 00001f 00 0   0  1
+# OTHER-SECTION-NEXT:   [ 2] .strtab   STRTAB   0000000000000000 000040 000001 00 0   0  1
+# OTHER-SECTION-NEXT:   [ 3] .shstrtab STRTAB   0000000000000000 000041 000017 00 0   0  1
 
 --- !ELF
 FileHeader:
index 6ae98b2..821b774 100644 (file)
@@ -6,10 +6,10 @@
 # RUN: yaml2obj --docnum=1 %s -o %t1
 # RUN: llvm-readelf --file-headers %t1 | FileCheck %s --check-prefix=DEFAULT
 
-# DEFAULT:   Start of section headers:          120 (bytes into file)
+# DEFAULT:   Start of section headers:          88 (bytes into file)
 # DEFAULT:   Size of section headers:           64 (bytes)
-# DEFAULT:   Number of section headers:         4
-# DEFAULT:   Section header string table index: 3
+# DEFAULT:   Number of section headers:         3
+# DEFAULT:   Section header string table index: 2
 
 --- !ELF
 FileHeader:
index 6e20912..612087a 100644 (file)
@@ -126,4 +126,3 @@ Sections:
     Type:    SHT_SYMTAB_SHNDX
     Entries: [ 0 ]
     EntSize: 2
-    Link:    .symtab
index 8360fa7..2860fa3 100644 (file)
@@ -24,6 +24,8 @@ FileHeader:
   Data:    ELFDATA2LSB
   Type:    ET_DYN
   Machine: EM_X86_64
+## Needed to force the creation of the .symtab.
+Symbols: []
 ## Needed to force the creation of the .dynsym and .dynstr.
 DynamicSymbols:
   - Name:    foo
index 77e9e1c..cde17c2 100644 (file)
@@ -84,3 +84,30 @@ Sections:
   - Name:    .text.foo
     Type:    SHT_PROGBITS
     Address: 0x200
+
+## Check we don't add a symbol table when no "Symbols" key is specified.
+
+# RUN: yaml2obj --docnum=3 %s -o %t3
+# RUN: llvm-readelf -S %t3 | FileCheck /dev/null --implicit-check-not=.symtab
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_DYN
+  Machine: EM_X86_64
+
+## Check we add a symbol table when "Symbols" key is specified.
+
+# RUN: yaml2obj --docnum=4 %s -o %t4
+# RUN: llvm-readelf -S %t4 | FileCheck %s --check-prefix=SYMTAB
+
+# SYMTAB: .symtab
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_DYN
+  Machine: EM_X86_64
+Symbols: []
index 716778e..9ec956f 100644 (file)
@@ -16,7 +16,6 @@ Sections:
     Content:         "00000000"
   - Name:            .rel.text
     Type:            SHT_REL
-    Link:            .symtab
     Info:            .text
     Relocations:
       - Offset: 0x1000
index 801fe02..bbbdc85 100644 (file)
@@ -20,7 +20,6 @@ FileHeader:
 Sections:
   - Name:            .rela.text
     Type:            SHT_RELA
-    Link:            .symtab
     Info:            12345
     Relocations:
 
index 99f7af6..680e03e 100644 (file)
@@ -14,7 +14,6 @@ Sections:
     Content:         "00000000"
   - Name:            .rel.text
     Type:            SHT_REL
-    Link:            .symtab
     Info:            .text
     Relocations:
       - Offset: 0x1000
index 2c17b95..35c3396 100644 (file)
@@ -200,9 +200,13 @@ template <class ELFT> Expected<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
       return TableOrErr.takeError();
     ShndxTable = *TableOrErr;
   }
-  if (SymTab)
-    if (Error E = dumpSymbols(SymTab, Y->Symbols))
+
+  if (SymTab) {
+    Y->Symbols.emplace();
+    if (Error E = dumpSymbols(SymTab, *Y->Symbols))
       return std::move(E);
+  }
+
   if (DynSymTab)
     if (Error E = dumpSymbols(DynSymTab, Y->DynamicSymbols))
       return std::move(E);