llvm.org GIT mirror llvm / 5f9ffd1
[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 James Henderson 7 months ago
10 changed file(s) with 74 addition(s) and 43 deletion(s). Raw diff Collapse all Expand all
1919 Info: .text
2020 Relocations:
2121 - Offset: 0x0000000000000000
22 Symbol: ''
2322 Type: R_X86_64_NONE
2423 Symbols:
2524 Global:
3737 AddressAlign: 0x08
3838 Relocations:
3939 - Offset: 0x0
40 Symbol: s0
4140 Type: R_AMDGPU_NONE
4241 - Offset: 0x2
43 Symbol: s1
4442 Type: R_AMDGPU_ABS32_LO
4543 - Offset: 0x4
46 Symbol: s2
4744 Type: R_AMDGPU_ABS32_HI
4845 - Offset: 0x6
49 Symbol: s3
5046 Type: R_AMDGPU_ABS64
5147 - Offset: 0x8
52 Symbol: s4
5348 Type: R_AMDGPU_REL32
5449 - Offset: 0x10
55 Symbol: s5
5650 Type: R_AMDGPU_REL64
5751 - Offset: 0x12
58 Symbol: s6
5952 Type: R_AMDGPU_ABS32
6053 - Offset: 0x14
61 Symbol: s7
6254 Type: R_AMDGPU_GOTPCREL
6355 - Offset: 0x16
64 Symbol: s8
6556 Type: R_AMDGPU_GOTPCREL32_LO
6657 - Offset: 0x18
67 Symbol: s9
6858 Type: R_AMDGPU_GOTPCREL32_HI
6959 - Offset: 0x20
70 Symbol: s10
7160 Type: R_AMDGPU_REL32_LO
7261 - Offset: 0x22
73 Symbol: s11
7462 Type: R_AMDGPU_REL32_HI
7563 - Offset: 0x24
76 Symbol: s12
7764 Type: R_AMDGPU_RELATIVE64
7865
7966 Symbols:
3434 Symbol: main
3535 Type: R_LANAI_32
3636 - Offset: 0x4
37 Symbol: a
3837 Type: R_LANAI_NONE
3938 - Offset: 0x8
40 Symbol: b
4139 Type: R_LANAI_21
4240 - Offset: 0xC
43 Symbol: c
4441 Type: R_LANAI_21_F
4542 - Offset: 0x10
46 Symbol: d
4743 Type: R_LANAI_25
4844 - Offset: 0x14
49 Symbol: e
5045 Type: R_LANAI_HI16
5146 - Offset: 0x18
52 Symbol: f
5347 Type: R_LANAI_LO16
5448
5549
2626 - Offset: 0
2727 Symbol: main
2828 Type: 0xFF
29 Symbols:
30 Global:
31 - Name: main
2020 Info: .text
2121 Relocations:
2222 - Offset: 0x000000000000000C
23 Symbol: ''
2423 Type: R_ARM_V4BX
2524 - Name: .data
2625 Type: SHT_PROGBITS
4949 - Offset: 0x0000000000000008
5050 Symbol: x
5151 Type: R_X86_64_64
52 Symbols:
53 Global:
54 - Name: x
8484 Info: .debug_info
8585 Relocations:
8686 - Offset: 0x0000000000000006
87 Symbol: ''
8887 Type: R_X86_64_32
8988 - Offset: 0x000000000000000C
90 Symbol: ''
9189 Type: R_X86_64_32
9290 - Offset: 0x0000000000000012
93 Symbol: ''
9491 Type: R_X86_64_32
9592 Addend: 55
9693 - Offset: 0x0000000000000016
97 Symbol: ''
9894 Type: R_X86_64_32
9995 - Offset: 0x000000000000001A
100 Symbol: ''
10196 Type: R_X86_64_32
10297 Addend: 59
10398 - Offset: 0x000000000000001E
104 Symbol: ''
10599 Type: R_X86_64_64
106100 - Offset: 0x000000000000002B
107 Symbol: ''
108101 Type: R_X86_64_64
109102 - Offset: 0x0000000000000039
110 Symbol: ''
111103 Type: R_X86_64_32
112104 Addend: 64
113105 - Offset: 0x0000000000000040
114 Symbol: ''
115106 Type: R_X86_64_64
116107 Addend: 16
117108 - Offset: 0x000000000000004E
118 Symbol: ''
119109 Type: R_X86_64_32
120110 Addend: 67
121111 - Offset: 0x0000000000000055
122 Symbol: ''
123112 Type: R_X86_64_64
124113 Addend: 32
125114 - Offset: 0x0000000000000063
126 Symbol: ''
127115 Type: R_X86_64_32
128116 Addend: 70
129117 - Name: .debug_ranges
141129 Info: .debug_pubnames
142130 Relocations:
143131 - Offset: 0x0000000000000006
144 Symbol: ''
145132 Type: R_X86_64_32
146133 - Name: .comment
147134 Type: SHT_PROGBITS
164151 Info: .eh_frame
165152 Relocations:
166153 - Offset: 0x0000000000000020
167 Symbol: ''
168154 Type: R_X86_64_PC32
169155 - Offset: 0x000000000000003C
170 Symbol: ''
171156 Type: R_X86_64_PC32
172157 Addend: 16
173158 - Offset: 0x0000000000000058
174 Symbol: ''
175159 Type: R_X86_64_PC32
176160 Addend: 32
177161 - Name: .debug_line
185169 Info: .debug_line
186170 Relocations:
187171 - Offset: 0x0000000000000027
188 Symbol: ''
189172 Type: R_X86_64_64
190173 Symbols:
191174 Local:
0 # Test that a relocation's symbol can be an integer.
1 # RUN: yaml2obj %s > %t
2 # LLVM tools (both llvm-readobj and llvm-objdump) reject relocations with
3 # invalid symbol indexes, so inspect the hex contents instead.
4 # RUN: llvm-readobj -x .rela.text %t | FileCheck %s
5
6 # CHECK: Hex dump of section '.rela.text':
7 # CHECK-NEXT: 0x00000000 00000000 00000000 02000000 42000000
8 # Symbol index 0x42 --^
9 # CHECK-NEXT: 0x00000010 00000000 00000000 00000000 00000000
10 # CHECK-NEXT: 0x00000020 02000000 01000000 00000000 00000000
11 # Symbol index 0x01 --^
12
13 --- !ELF
14 FileHeader:
15 Class: ELFCLASS64
16 Data: ELFDATA2LSB
17 Type: ET_REL
18 Machine: EM_X86_64
19 Sections:
20 - Name: .text
21 Type: SHT_PROGBITS
22 - Name: .rela.text
23 Type: SHT_RELA
24 Info: .text
25 Link: .symtab
26 Relocations:
27 - Type: R_X86_64_PC32
28 Offset: 0
29 Symbol: 0x42
30 - Type: R_X86_64_PC32
31 Offset: 0
32 Symbol: 0x1
33 Symbols:
34 Global:
35 - Name: foo
0 # Show that yaml2obj rejects a symbol reference from a relocation if the symbol
1 # does not exist.
2
3 # RUN: not yaml2obj %s -o %t 2>&1 | FileCheck %s
4
5 # CHECK: Unknown symbol referenced: 'does_not_exist' at YAML section '.rela.text'
6
7 --- !ELF
8 FileHeader:
9 Class: ELFCLASS64
10 Data: ELFDATA2LSB
11 Type: ET_REL
12 Machine: EM_X86_64
13 Sections:
14 - Name: .text
15 Type: SHT_PROGBITS
16 - Name: .rela.text
17 Type: SHT_RELA
18 Info: .text
19 Link: .symtab
20 Relocations:
21 - Type: R_X86_64_PC32
22 Offset: 0
23 Symbol: does_not_exist
529529
530530 for (const auto &Rel : Section.Relocations) {
531531 unsigned SymIdx = 0;
532 // Some special relocation, R_ARM_v4BX for instance, does not have
533 // an external reference. So it ignores the return value of lookup()
534 // here.
535 if (Rel.Symbol)
536 SymN2I.lookup(*Rel.Symbol, SymIdx);
532 // If a relocation references a symbol, try to look one up in the symbol
533 // table. If it is not there, treat the value as a symbol index.
534 if (Rel.Symbol && SymN2I.lookup(*Rel.Symbol, SymIdx) &&
535 !to_integer(*Rel.Symbol, SymIdx)) {
536 WithColor::error() << "Unknown symbol referenced: '" << *Rel.Symbol
537 << "' at YAML section '" << Section.Name << "'.\n";
538 return false;
539 }
537540
538541 if (IsRela) {
539542 Elf_Rela REntry;