[yaml2obj]Allow explicit symbol indexes in relocations and emit error for bad names

Prior to this change, the "Symbol" field of a relocation would always be
assumed to be a symbol name, and if no such symbol existed, the
relocation would reference index 0. This confused me when I tried to use
a literal symbol index in the field: since "0x1" was not a known symbol
name, the symbol index was set as 0. This change falls back to treating
unknown symbol names as integers, and emits an error if the name is not
found and the string is not an integer.

Note that the Symbol field is optional, so if a relocation doesn't
reference a symbol, it shouldn't be specified. The new error required a
number of test updates.

Reviewed by: grimar, ruiu
Differential Revision: https://reviews.llvm.org/D58510


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@355938 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/ExecutionEngine/RuntimeDyld/X86/ELF_x86-64_none.yaml b/test/ExecutionEngine/RuntimeDyld/X86/ELF_x86-64_none.yaml
index 7732c7f..ed4b243 100644
--- a/test/ExecutionEngine/RuntimeDyld/X86/ELF_x86-64_none.yaml
+++ b/test/ExecutionEngine/RuntimeDyld/X86/ELF_x86-64_none.yaml
@@ -20,7 +20,6 @@
     Info:            .text
     Relocations:
       - Offset:          0x0000000000000000
-        Symbol:          ''
         Type:            R_X86_64_NONE
 Symbols:
   Global:
diff --git a/test/Object/AMDGPU/elf64-relocs.yaml b/test/Object/AMDGPU/elf64-relocs.yaml
index bc6c2b8..d2c45e4 100644
--- a/test/Object/AMDGPU/elf64-relocs.yaml
+++ b/test/Object/AMDGPU/elf64-relocs.yaml
@@ -38,43 +38,30 @@
     AddressAlign:    0x08
     Relocations:
       - Offset:          0x0
-        Symbol:          s0
         Type:            R_AMDGPU_NONE
       - Offset:          0x2
-        Symbol:          s1
         Type:            R_AMDGPU_ABS32_LO
       - Offset:          0x4
-        Symbol:          s2
         Type:            R_AMDGPU_ABS32_HI
       - Offset:          0x6
-        Symbol:          s3
         Type:            R_AMDGPU_ABS64
       - Offset:          0x8
-        Symbol:          s4
         Type:            R_AMDGPU_REL32
       - Offset:          0x10
-        Symbol:          s5
         Type:            R_AMDGPU_REL64
       - Offset:          0x12
-        Symbol:          s6
         Type:            R_AMDGPU_ABS32
       - Offset:          0x14
-        Symbol:          s7
         Type:            R_AMDGPU_GOTPCREL
       - Offset:          0x16
-        Symbol:          s8
         Type:            R_AMDGPU_GOTPCREL32_LO
       - Offset:          0x18
-        Symbol:          s9
         Type:            R_AMDGPU_GOTPCREL32_HI
       - Offset:          0x20
-        Symbol:          s10
         Type:            R_AMDGPU_REL32_LO
       - Offset:          0x22
-        Symbol:          s11
         Type:            R_AMDGPU_REL32_HI
       - Offset:          0x24
-        Symbol:          s12
         Type:            R_AMDGPU_RELATIVE64
 
 Symbols:
diff --git a/test/Object/Lanai/yaml2obj-elf-lanai-rel.yaml b/test/Object/Lanai/yaml2obj-elf-lanai-rel.yaml
index df81a5b..ec2a19d 100644
--- a/test/Object/Lanai/yaml2obj-elf-lanai-rel.yaml
+++ b/test/Object/Lanai/yaml2obj-elf-lanai-rel.yaml
@@ -35,22 +35,16 @@
         Symbol:          main
         Type:            R_LANAI_32
       - Offset:          0x4
-        Symbol:          a
         Type:            R_LANAI_NONE
       - Offset:          0x8
-        Symbol:          b
         Type:            R_LANAI_21
       - Offset:          0xC
-        Symbol:          c
         Type:            R_LANAI_21_F
       - Offset:          0x10
-        Symbol:          d
         Type:            R_LANAI_25
       - Offset:          0x14
-        Symbol:          e
         Type:            R_LANAI_HI16
       - Offset:          0x18
-        Symbol:          f
         Type:            R_LANAI_LO16
 
 
diff --git a/test/Object/X86/yaml-elf-x86-rel-broken.yaml b/test/Object/X86/yaml-elf-x86-rel-broken.yaml
index edd5dbc..b9afd76 100644
--- a/test/Object/X86/yaml-elf-x86-rel-broken.yaml
+++ b/test/Object/X86/yaml-elf-x86-rel-broken.yaml
@@ -27,3 +27,6 @@
       - Offset:          0
         Symbol:          main
         Type:            0xFF
+Symbols:
+  Global:
+    - Name: main
diff --git a/test/Object/yaml2obj-elf-rel-noref.yaml b/test/Object/yaml2obj-elf-rel-noref.yaml
index 8eb6b0d..cf4324b 100644
--- a/test/Object/yaml2obj-elf-rel-noref.yaml
+++ b/test/Object/yaml2obj-elf-rel-noref.yaml
@@ -21,7 +21,6 @@
     Info:            .text
     Relocations:     
       - Offset:          0x000000000000000C
-        Symbol:          ''
         Type:            R_ARM_V4BX
   - Name:            .data
     Type:            SHT_PROGBITS
diff --git a/test/tools/llvm-objdump/X86/disasm-zeroes-relocations.test b/test/tools/llvm-objdump/X86/disasm-zeroes-relocations.test
index 7bdc7fd..ad8b025 100644
--- a/test/tools/llvm-objdump/X86/disasm-zeroes-relocations.test
+++ b/test/tools/llvm-objdump/X86/disasm-zeroes-relocations.test
@@ -50,3 +50,6 @@
       - Offset:          0x0000000000000008
         Symbol:          x
         Type:            R_X86_64_64
+Symbols:
+  Global:
+    - Name: x
diff --git a/test/tools/sanstats/elf.test b/test/tools/sanstats/elf.test
index 54878a1..67e6e4a 100644
--- a/test/tools/sanstats/elf.test
+++ b/test/tools/sanstats/elf.test
@@ -85,46 +85,34 @@
     Info:            .debug_info
     Relocations:     
       - Offset:          0x0000000000000006
-        Symbol:          ''
         Type:            R_X86_64_32
       - Offset:          0x000000000000000C
-        Symbol:          ''
         Type:            R_X86_64_32
       - Offset:          0x0000000000000012
-        Symbol:          ''
         Type:            R_X86_64_32
         Addend:          55
       - Offset:          0x0000000000000016
-        Symbol:          ''
         Type:            R_X86_64_32
       - Offset:          0x000000000000001A
-        Symbol:          ''
         Type:            R_X86_64_32
         Addend:          59
       - Offset:          0x000000000000001E
-        Symbol:          ''
         Type:            R_X86_64_64
       - Offset:          0x000000000000002B
-        Symbol:          ''
         Type:            R_X86_64_64
       - Offset:          0x0000000000000039
-        Symbol:          ''
         Type:            R_X86_64_32
         Addend:          64
       - Offset:          0x0000000000000040
-        Symbol:          ''
         Type:            R_X86_64_64
         Addend:          16
       - Offset:          0x000000000000004E
-        Symbol:          ''
         Type:            R_X86_64_32
         Addend:          67
       - Offset:          0x0000000000000055
-        Symbol:          ''
         Type:            R_X86_64_64
         Addend:          32
       - Offset:          0x0000000000000063
-        Symbol:          ''
         Type:            R_X86_64_32
         Addend:          70
   - Name:            .debug_ranges
@@ -142,7 +130,6 @@
     Info:            .debug_pubnames
     Relocations:     
       - Offset:          0x0000000000000006
-        Symbol:          ''
         Type:            R_X86_64_32
   - Name:            .comment
     Type:            SHT_PROGBITS
@@ -165,14 +152,11 @@
     Info:            .eh_frame
     Relocations:     
       - Offset:          0x0000000000000020
-        Symbol:          ''
         Type:            R_X86_64_PC32
       - Offset:          0x000000000000003C
-        Symbol:          ''
         Type:            R_X86_64_PC32
         Addend:          16
       - Offset:          0x0000000000000058
-        Symbol:          ''
         Type:            R_X86_64_PC32
         Addend:          32
   - Name:            .debug_line
@@ -186,7 +170,6 @@
     Info:            .debug_line
     Relocations:     
       - Offset:          0x0000000000000027
-        Symbol:          ''
         Type:            R_X86_64_64
 Symbols:         
   Local:           
diff --git a/test/tools/yaml2obj/relocation-explicit-symbol-index.yaml b/test/tools/yaml2obj/relocation-explicit-symbol-index.yaml
new file mode 100644
index 0000000..ac16643
--- /dev/null
+++ b/test/tools/yaml2obj/relocation-explicit-symbol-index.yaml
@@ -0,0 +1,36 @@
+# Test that a relocation's symbol can be an integer.
+# RUN: yaml2obj %s > %t
+# LLVM tools (both llvm-readobj and llvm-objdump) reject relocations with
+# invalid symbol indexes, so inspect the hex contents instead.
+# RUN: llvm-readobj -x .rela.text %t | FileCheck %s
+
+# CHECK:      Hex dump of section '.rela.text':
+# CHECK-NEXT: 0x00000000 00000000 00000000 02000000 42000000
+#                               Symbol index 0x42 --^
+# CHECK-NEXT: 0x00000010 00000000 00000000 00000000 00000000
+# CHECK-NEXT: 0x00000020 02000000 01000000 00000000 00000000
+#             Symbol index 0x01 --^
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Sections:
+  - Name: .text
+    Type: SHT_PROGBITS
+  - Name: .rela.text
+    Type: SHT_RELA
+    Info: .text
+    Link: .symtab
+    Relocations:
+      - Type: R_X86_64_PC32
+        Offset: 0
+        Symbol: 0x42
+      - Type: R_X86_64_PC32
+        Offset: 0
+        Symbol: 0x1
+Symbols:
+  Global:
+    - Name: foo
diff --git a/test/tools/yaml2obj/relocation-missing-symbol.yaml b/test/tools/yaml2obj/relocation-missing-symbol.yaml
new file mode 100644
index 0000000..7edbf18
--- /dev/null
+++ b/test/tools/yaml2obj/relocation-missing-symbol.yaml
@@ -0,0 +1,24 @@
+# Show that yaml2obj rejects a symbol reference from a relocation if the symbol
+# does not exist.
+
+# RUN: not yaml2obj %s -o %t 2>&1 | FileCheck %s
+
+# CHECK: Unknown symbol referenced: 'does_not_exist' at YAML section '.rela.text'
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Sections:
+  - Name: .text
+    Type: SHT_PROGBITS
+  - Name: .rela.text
+    Type: SHT_RELA
+    Info: .text
+    Link: .symtab
+    Relocations:
+      - Type: R_X86_64_PC32
+        Offset: 0
+        Symbol: does_not_exist
diff --git a/tools/yaml2obj/yaml2elf.cpp b/tools/yaml2obj/yaml2elf.cpp
index 6bae48b..df4bdb7 100644
--- a/tools/yaml2obj/yaml2elf.cpp
+++ b/tools/yaml2obj/yaml2elf.cpp
@@ -530,11 +530,14 @@
 
   for (const auto &Rel : Section.Relocations) {
     unsigned SymIdx = 0;
-    // Some special relocation, R_ARM_v4BX for instance, does not have
-    // an external reference.  So it ignores the return value of lookup()
-    // here.
-    if (Rel.Symbol)
-      SymN2I.lookup(*Rel.Symbol, SymIdx);
+    // If a relocation references a symbol, try to look one up in the symbol
+    // table. If it is not there, treat the value as a symbol index.
+    if (Rel.Symbol && SymN2I.lookup(*Rel.Symbol, SymIdx) &&
+        !to_integer(*Rel.Symbol, SymIdx)) {
+      WithColor::error() << "Unknown symbol referenced: '" << *Rel.Symbol
+                         << "' at YAML section '" << Section.Name << "'.\n";
+      return false;
+    }
 
     if (IsRela) {
       Elf_Rela REntry;