[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

llvm-svn: 355938
This commit is contained in:
James Henderson 2019-03-12 17:00:25 +00:00
parent 149bc099f6
commit 9bc817a0ae
10 changed files with 74 additions and 43 deletions

View File

@ -20,7 +20,6 @@ Sections:
Info: .text
Relocations:
- Offset: 0x0000000000000000
Symbol: ''
Type: R_X86_64_NONE
Symbols:
Global:

View 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:

View 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

View File

@ -27,3 +27,6 @@ Sections:
- Offset: 0
Symbol: main
Type: 0xFF
Symbols:
Global:
- Name: main

View File

@ -21,7 +21,6 @@ Sections:
Info: .text
Relocations:
- Offset: 0x000000000000000C
Symbol: ''
Type: R_ARM_V4BX
- Name: .data
Type: SHT_PROGBITS

View File

@ -50,3 +50,6 @@ Sections:
- Offset: 0x0000000000000008
Symbol: x
Type: R_X86_64_64
Symbols:
Global:
- Name: x

View 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:

View File

@ -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

View File

@ -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

View 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;