[llvm] r355938 - [yaml2obj]Allow explicit symbol indexes in relocations and emit error for bad names
James Henderson via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 12 10:00:25 PDT 2019
Author: jhenderson
Date: Tue Mar 12 10:00:25 2019
New Revision: 355938
URL: http://llvm.org/viewvc/llvm-project?rev=355938&view=rev
Log:
[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
Added:
llvm/trunk/test/tools/yaml2obj/relocation-explicit-symbol-index.yaml
llvm/trunk/test/tools/yaml2obj/relocation-missing-symbol.yaml
Modified:
llvm/trunk/test/ExecutionEngine/RuntimeDyld/X86/ELF_x86-64_none.yaml
llvm/trunk/test/Object/AMDGPU/elf64-relocs.yaml
llvm/trunk/test/Object/Lanai/yaml2obj-elf-lanai-rel.yaml
llvm/trunk/test/Object/X86/yaml-elf-x86-rel-broken.yaml
llvm/trunk/test/Object/yaml2obj-elf-rel-noref.yaml
llvm/trunk/test/tools/llvm-objdump/X86/disasm-zeroes-relocations.test
llvm/trunk/test/tools/sanstats/elf.test
llvm/trunk/tools/yaml2obj/yaml2elf.cpp
Modified: llvm/trunk/test/ExecutionEngine/RuntimeDyld/X86/ELF_x86-64_none.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/ExecutionEngine/RuntimeDyld/X86/ELF_x86-64_none.yaml?rev=355938&r1=355937&r2=355938&view=diff
==============================================================================
--- llvm/trunk/test/ExecutionEngine/RuntimeDyld/X86/ELF_x86-64_none.yaml (original)
+++ llvm/trunk/test/ExecutionEngine/RuntimeDyld/X86/ELF_x86-64_none.yaml Tue Mar 12 10:00:25 2019
@@ -20,7 +20,6 @@ Sections:
Info: .text
Relocations:
- Offset: 0x0000000000000000
- Symbol: ''
Type: R_X86_64_NONE
Symbols:
Global:
Modified: llvm/trunk/test/Object/AMDGPU/elf64-relocs.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/AMDGPU/elf64-relocs.yaml?rev=355938&r1=355937&r2=355938&view=diff
==============================================================================
--- llvm/trunk/test/Object/AMDGPU/elf64-relocs.yaml (original)
+++ llvm/trunk/test/Object/AMDGPU/elf64-relocs.yaml Tue Mar 12 10:00:25 2019
@@ -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:
Modified: llvm/trunk/test/Object/Lanai/yaml2obj-elf-lanai-rel.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/Lanai/yaml2obj-elf-lanai-rel.yaml?rev=355938&r1=355937&r2=355938&view=diff
==============================================================================
--- llvm/trunk/test/Object/Lanai/yaml2obj-elf-lanai-rel.yaml (original)
+++ llvm/trunk/test/Object/Lanai/yaml2obj-elf-lanai-rel.yaml Tue Mar 12 10:00:25 2019
@@ -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
Modified: llvm/trunk/test/Object/X86/yaml-elf-x86-rel-broken.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/X86/yaml-elf-x86-rel-broken.yaml?rev=355938&r1=355937&r2=355938&view=diff
==============================================================================
--- llvm/trunk/test/Object/X86/yaml-elf-x86-rel-broken.yaml (original)
+++ llvm/trunk/test/Object/X86/yaml-elf-x86-rel-broken.yaml Tue Mar 12 10:00:25 2019
@@ -27,3 +27,6 @@ Sections:
- Offset: 0
Symbol: main
Type: 0xFF
+Symbols:
+ Global:
+ - Name: main
Modified: llvm/trunk/test/Object/yaml2obj-elf-rel-noref.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/yaml2obj-elf-rel-noref.yaml?rev=355938&r1=355937&r2=355938&view=diff
==============================================================================
--- llvm/trunk/test/Object/yaml2obj-elf-rel-noref.yaml (original)
+++ llvm/trunk/test/Object/yaml2obj-elf-rel-noref.yaml Tue Mar 12 10:00:25 2019
@@ -21,7 +21,6 @@ Sections:
Info: .text
Relocations:
- Offset: 0x000000000000000C
- Symbol: ''
Type: R_ARM_V4BX
- Name: .data
Type: SHT_PROGBITS
Modified: llvm/trunk/test/tools/llvm-objdump/X86/disasm-zeroes-relocations.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/X86/disasm-zeroes-relocations.test?rev=355938&r1=355937&r2=355938&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/X86/disasm-zeroes-relocations.test (original)
+++ llvm/trunk/test/tools/llvm-objdump/X86/disasm-zeroes-relocations.test Tue Mar 12 10:00:25 2019
@@ -50,3 +50,6 @@ Sections:
- Offset: 0x0000000000000008
Symbol: x
Type: R_X86_64_64
+Symbols:
+ Global:
+ - Name: x
Modified: llvm/trunk/test/tools/sanstats/elf.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/sanstats/elf.test?rev=355938&r1=355937&r2=355938&view=diff
==============================================================================
--- llvm/trunk/test/tools/sanstats/elf.test (original)
+++ llvm/trunk/test/tools/sanstats/elf.test Tue Mar 12 10:00:25 2019
@@ -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:
Added: llvm/trunk/test/tools/yaml2obj/relocation-explicit-symbol-index.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/yaml2obj/relocation-explicit-symbol-index.yaml?rev=355938&view=auto
==============================================================================
--- llvm/trunk/test/tools/yaml2obj/relocation-explicit-symbol-index.yaml (added)
+++ llvm/trunk/test/tools/yaml2obj/relocation-explicit-symbol-index.yaml Tue Mar 12 10:00:25 2019
@@ -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
Added: llvm/trunk/test/tools/yaml2obj/relocation-missing-symbol.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/yaml2obj/relocation-missing-symbol.yaml?rev=355938&view=auto
==============================================================================
--- llvm/trunk/test/tools/yaml2obj/relocation-missing-symbol.yaml (added)
+++ llvm/trunk/test/tools/yaml2obj/relocation-missing-symbol.yaml Tue Mar 12 10:00:25 2019
@@ -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
Modified: llvm/trunk/tools/yaml2obj/yaml2elf.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/yaml2obj/yaml2elf.cpp?rev=355938&r1=355937&r2=355938&view=diff
==============================================================================
--- llvm/trunk/tools/yaml2obj/yaml2elf.cpp (original)
+++ llvm/trunk/tools/yaml2obj/yaml2elf.cpp Tue Mar 12 10:00:25 2019
@@ -530,11 +530,14 @@ ELFState<ELFT>::writeSectionContent(Elf_
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;
More information about the llvm-commits
mailing list