]> granicus.if.org Git - llvm/commitdiff
[yaml2obj]Allow explicit symbol indexes in relocations and emit error for bad names
authorJames Henderson <jh7370@my.bristol.ac.uk>
Tue, 12 Mar 2019 17:00:25 +0000 (17:00 +0000)
committerJames Henderson <jh7370@my.bristol.ac.uk>
Tue, 12 Mar 2019 17:00:25 +0000 (17:00 +0000)
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

test/ExecutionEngine/RuntimeDyld/X86/ELF_x86-64_none.yaml
test/Object/AMDGPU/elf64-relocs.yaml
test/Object/Lanai/yaml2obj-elf-lanai-rel.yaml
test/Object/X86/yaml-elf-x86-rel-broken.yaml
test/Object/yaml2obj-elf-rel-noref.yaml
test/tools/llvm-objdump/X86/disasm-zeroes-relocations.test
test/tools/sanstats/elf.test
test/tools/yaml2obj/relocation-explicit-symbol-index.yaml [new file with mode: 0644]
test/tools/yaml2obj/relocation-missing-symbol.yaml [new file with mode: 0644]
tools/yaml2obj/yaml2elf.cpp

index 7732c7f1ab98404829a224f24454f9d44bd7ed76..ed4b243fae1311ff00f2fdb0a5a82b5af5f28ffe 100644 (file)
@@ -20,7 +20,6 @@ Sections:
     Info:            .text
     Relocations:
       - Offset:          0x0000000000000000
-        Symbol:          ''
         Type:            R_X86_64_NONE
 Symbols:
   Global:
index bc6c2b89d5f5cbefcfac38f85a1a5be858b2d77f..d2c45e447ff3b3749ff3366575aa1fa48ed19de2 100644 (file)
@@ -38,43 +38,30 @@ Sections:
     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:
index df81a5b8bde04dfe56f7d35b2af50ac9c8c48b33..ec2a19d741dd95d8dee471358187f5a7a702a10c 100644 (file)
@@ -35,22 +35,16 @@ Sections:
         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
 
 
index edd5dbce1236db45134024ae1b81cd42b00d865c..b9afd768e0b95bea8d05ec08a7d337343c128c54 100644 (file)
@@ -27,3 +27,6 @@ Sections:
       - Offset:          0
         Symbol:          main
         Type:            0xFF
+Symbols:
+  Global:
+    - Name: main
index 8eb6b0dca3b69e04688060397e3c4a8478b05308..cf4324b67de458c9d2d67282da9ef358fed61034 100644 (file)
@@ -21,7 +21,6 @@ Sections:
     Info:            .text
     Relocations:     
       - Offset:          0x000000000000000C
-        Symbol:          ''
         Type:            R_ARM_V4BX
   - Name:            .data
     Type:            SHT_PROGBITS
index 7bdc7fd7f346e7f036cd4008728200888ca2d8ab..ad8b025ab50dcaf1ec2e6dd4e6ad676b33aff6e2 100644 (file)
@@ -50,3 +50,6 @@ Sections:
       - Offset:          0x0000000000000008
         Symbol:          x
         Type:            R_X86_64_64
+Symbols:
+  Global:
+    - Name: x
index 54878a1d526b8f40c436c23a96ede79bedad05e0..67e6e4ae1c19f9596b26a600cd73adef00a134d1 100644 (file)
@@ -85,46 +85,34 @@ Sections:
     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 @@ Sections:
     Info:            .debug_pubnames
     Relocations:     
       - Offset:          0x0000000000000006
-        Symbol:          ''
         Type:            R_X86_64_32
   - Name:            .comment
     Type:            SHT_PROGBITS
@@ -165,14 +152,11 @@ Sections:
     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 @@ Sections:
     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 (file)
index 0000000..ac16643
--- /dev/null
@@ -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 (file)
index 0000000..7edbf18
--- /dev/null
@@ -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
index 6bae48bba40e7f7dbf2f872a0513e980ddfbbe54..df4bdb7d2dd8455f58fa249ba44a02905ea9e508 100644 (file)
@@ -530,11 +530,14 @@ ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
 
   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;