Skip to content

Commit 5d26959

Browse files
committed
[Object] Implement relocation resolver for COFF ARM/ARM64
Adding testscases for this via llvm-dwarfdump. Also add testcases for the existing resolver support for X86. Differential Revision: https://reviews.llvm.org/D67340 llvm-svn: 371515
1 parent 1eda21e commit 5d26959

File tree

5 files changed

+647
-2
lines changed

5 files changed

+647
-2
lines changed

llvm/lib/Object/RelocationResolver.cpp

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,47 @@ static uint64_t resolveCOFFX86_64(RelocationRef R, uint64_t S, uint64_t A) {
426426
}
427427
}
428428

429+
static bool supportsCOFFARM(uint64_t Type) {
430+
switch (Type) {
431+
case COFF::IMAGE_REL_ARM_SECREL:
432+
case COFF::IMAGE_REL_ARM_ADDR32:
433+
return true;
434+
default:
435+
return false;
436+
}
437+
}
438+
439+
static uint64_t resolveCOFFARM(RelocationRef R, uint64_t S, uint64_t A) {
440+
switch (R.getType()) {
441+
case COFF::IMAGE_REL_ARM_SECREL:
442+
case COFF::IMAGE_REL_ARM_ADDR32:
443+
return (S + A) & 0xFFFFFFFF;
444+
default:
445+
llvm_unreachable("Invalid relocation type");
446+
}
447+
}
448+
449+
static bool supportsCOFFARM64(uint64_t Type) {
450+
switch (Type) {
451+
case COFF::IMAGE_REL_ARM64_SECREL:
452+
case COFF::IMAGE_REL_ARM64_ADDR64:
453+
return true;
454+
default:
455+
return false;
456+
}
457+
}
458+
459+
static uint64_t resolveCOFFARM64(RelocationRef R, uint64_t S, uint64_t A) {
460+
switch (R.getType()) {
461+
case COFF::IMAGE_REL_ARM64_SECREL:
462+
return (S + A) & 0xFFFFFFFF;
463+
case COFF::IMAGE_REL_ARM64_ADDR64:
464+
return S + A;
465+
default:
466+
llvm_unreachable("Invalid relocation type");
467+
}
468+
}
469+
429470
static bool supportsMachOX86_64(uint64_t Type) {
430471
return Type == MachO::X86_64_RELOC_UNSIGNED;
431472
}
@@ -478,9 +519,19 @@ static uint64_t resolveWasm32(RelocationRef R, uint64_t S, uint64_t A) {
478519
std::pair<bool (*)(uint64_t), RelocationResolver>
479520
getRelocationResolver(const ObjectFile &Obj) {
480521
if (Obj.isCOFF()) {
481-
if (Obj.getBytesInAddress() == 8)
522+
switch (Obj.getArch()) {
523+
case Triple::x86_64:
482524
return {supportsCOFFX86_64, resolveCOFFX86_64};
483-
return {supportsCOFFX86, resolveCOFFX86};
525+
case Triple::x86:
526+
return {supportsCOFFX86, resolveCOFFX86};
527+
case Triple::arm:
528+
case Triple::thumb:
529+
return {supportsCOFFARM, resolveCOFFARM};
530+
case Triple::aarch64:
531+
return {supportsCOFFARM64, resolveCOFFARM64};
532+
default:
533+
return {nullptr, nullptr};
534+
}
484535
} else if (Obj.isELF()) {
485536
if (Obj.getBytesInAddress() == 8) {
486537
switch (Obj.getArch()) {
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# RUN: yaml2obj %s | llvm-dwarfdump - | FileCheck %s
2+
# CHECK: DW_TAG_compile_unit
3+
# CHECK-NEXT: DW_AT_producer ("producer string")
4+
# CHECK-NEXT: DW_AT_language (DW_LANG_C99)
5+
# CHECK-NEXT: DW_AT_name ("test.c")
6+
# CHECK-NEXT: DW_AT_comp_dir ("/path/to/src")
7+
--- !COFF
8+
header:
9+
Machine: IMAGE_FILE_MACHINE_ARMNT
10+
Characteristics: [ ]
11+
sections:
12+
- Name: .text
13+
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_PURGEABLE, IMAGE_SCN_MEM_16BIT, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
14+
Alignment: 4
15+
SectionData: ''
16+
- Name: .data
17+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
18+
Alignment: 4
19+
SectionData: ''
20+
- Name: .bss
21+
Characteristics: [ IMAGE_SCN_CNT_UNINITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
22+
Alignment: 4
23+
SectionData: ''
24+
SizeOfRawData: 0
25+
- Name: .debug_str
26+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
27+
Alignment: 1
28+
SectionData: 70726F647563657220737472696E6700746573742E63002F706174682F746F2F73726300
29+
- Name: .debug_abbrev
30+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
31+
Alignment: 1
32+
SectionData: 011101250E1305030E1B0E000000
33+
- Name: .debug_info
34+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
35+
Alignment: 1
36+
SectionData: 170000000400000000000401000000000C00000000000000000000
37+
Relocations:
38+
- VirtualAddress: 6
39+
SymbolName: .debug_abbrev
40+
Type: IMAGE_REL_ARM_SECREL
41+
- VirtualAddress: 12
42+
SymbolName: info_string0
43+
Type: IMAGE_REL_ARM_SECREL
44+
- VirtualAddress: 18
45+
SymbolName: info_string1
46+
Type: IMAGE_REL_ARM_SECREL
47+
- VirtualAddress: 22
48+
SymbolName: info_string2
49+
Type: IMAGE_REL_ARM_SECREL
50+
symbols:
51+
- Name: .text
52+
Value: 0
53+
SectionNumber: 1
54+
SimpleType: IMAGE_SYM_TYPE_NULL
55+
ComplexType: IMAGE_SYM_DTYPE_NULL
56+
StorageClass: IMAGE_SYM_CLASS_STATIC
57+
SectionDefinition:
58+
Length: 0
59+
NumberOfRelocations: 0
60+
NumberOfLinenumbers: 0
61+
CheckSum: 0
62+
Number: 1
63+
- Name: .data
64+
Value: 0
65+
SectionNumber: 2
66+
SimpleType: IMAGE_SYM_TYPE_NULL
67+
ComplexType: IMAGE_SYM_DTYPE_NULL
68+
StorageClass: IMAGE_SYM_CLASS_STATIC
69+
SectionDefinition:
70+
Length: 0
71+
NumberOfRelocations: 0
72+
NumberOfLinenumbers: 0
73+
CheckSum: 0
74+
Number: 2
75+
- Name: .bss
76+
Value: 0
77+
SectionNumber: 3
78+
SimpleType: IMAGE_SYM_TYPE_NULL
79+
ComplexType: IMAGE_SYM_DTYPE_NULL
80+
StorageClass: IMAGE_SYM_CLASS_STATIC
81+
SectionDefinition:
82+
Length: 0
83+
NumberOfRelocations: 0
84+
NumberOfLinenumbers: 0
85+
CheckSum: 0
86+
Number: 3
87+
- Name: .debug_str
88+
Value: 0
89+
SectionNumber: 4
90+
SimpleType: IMAGE_SYM_TYPE_NULL
91+
ComplexType: IMAGE_SYM_DTYPE_NULL
92+
StorageClass: IMAGE_SYM_CLASS_STATIC
93+
SectionDefinition:
94+
Length: 36
95+
NumberOfRelocations: 0
96+
NumberOfLinenumbers: 0
97+
CheckSum: 3440120581
98+
Number: 4
99+
- Name: .debug_abbrev
100+
Value: 0
101+
SectionNumber: 5
102+
SimpleType: IMAGE_SYM_TYPE_NULL
103+
ComplexType: IMAGE_SYM_DTYPE_NULL
104+
StorageClass: IMAGE_SYM_CLASS_STATIC
105+
SectionDefinition:
106+
Length: 14
107+
NumberOfRelocations: 0
108+
NumberOfLinenumbers: 0
109+
CheckSum: 2811829245
110+
Number: 5
111+
- Name: .debug_info
112+
Value: 0
113+
SectionNumber: 6
114+
SimpleType: IMAGE_SYM_TYPE_NULL
115+
ComplexType: IMAGE_SYM_DTYPE_NULL
116+
StorageClass: IMAGE_SYM_CLASS_STATIC
117+
SectionDefinition:
118+
Length: 27
119+
NumberOfRelocations: 4
120+
NumberOfLinenumbers: 0
121+
CheckSum: 1535600857
122+
Number: 6
123+
- Name: info_string0
124+
Value: 0
125+
SectionNumber: 4
126+
SimpleType: IMAGE_SYM_TYPE_NULL
127+
ComplexType: IMAGE_SYM_DTYPE_NULL
128+
StorageClass: IMAGE_SYM_CLASS_STATIC
129+
- Name: info_string1
130+
Value: 16
131+
SectionNumber: 4
132+
SimpleType: IMAGE_SYM_TYPE_NULL
133+
ComplexType: IMAGE_SYM_DTYPE_NULL
134+
StorageClass: IMAGE_SYM_CLASS_STATIC
135+
- Name: info_string2
136+
Value: 23
137+
SectionNumber: 4
138+
SimpleType: IMAGE_SYM_TYPE_NULL
139+
ComplexType: IMAGE_SYM_DTYPE_NULL
140+
StorageClass: IMAGE_SYM_CLASS_STATIC
141+
...
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# RUN: yaml2obj %s | llvm-dwarfdump - | FileCheck %s
2+
# CHECK: DW_TAG_compile_unit
3+
# CHECK-NEXT: DW_AT_producer ("producer string")
4+
# CHECK-NEXT: DW_AT_language (DW_LANG_C99)
5+
# CHECK-NEXT: DW_AT_name ("test.c")
6+
# CHECK-NEXT: DW_AT_comp_dir ("/path/to/src")
7+
--- !COFF
8+
header:
9+
Machine: IMAGE_FILE_MACHINE_ARM64
10+
Characteristics: [ ]
11+
sections:
12+
- Name: .text
13+
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
14+
Alignment: 4
15+
SectionData: ''
16+
- Name: .data
17+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
18+
Alignment: 4
19+
SectionData: ''
20+
- Name: .bss
21+
Characteristics: [ IMAGE_SCN_CNT_UNINITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
22+
Alignment: 4
23+
SectionData: ''
24+
SizeOfRawData: 0
25+
- Name: .debug_str
26+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
27+
Alignment: 1
28+
SectionData: 70726F647563657220737472696E6700746573742E63002F706174682F746F2F73726300
29+
- Name: .debug_abbrev
30+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
31+
Alignment: 1
32+
SectionData: 011101250E1305030E1B0E000000
33+
- Name: .debug_info
34+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
35+
Alignment: 1
36+
SectionData: 170000000400000000000401000000000C00000000000000000000
37+
Relocations:
38+
- VirtualAddress: 6
39+
SymbolName: .debug_abbrev
40+
Type: IMAGE_REL_ARM64_SECREL
41+
- VirtualAddress: 12
42+
SymbolName: info_string0
43+
Type: IMAGE_REL_ARM64_SECREL
44+
- VirtualAddress: 18
45+
SymbolName: info_string1
46+
Type: IMAGE_REL_ARM64_SECREL
47+
- VirtualAddress: 22
48+
SymbolName: info_string2
49+
Type: IMAGE_REL_ARM64_SECREL
50+
symbols:
51+
- Name: .text
52+
Value: 0
53+
SectionNumber: 1
54+
SimpleType: IMAGE_SYM_TYPE_NULL
55+
ComplexType: IMAGE_SYM_DTYPE_NULL
56+
StorageClass: IMAGE_SYM_CLASS_STATIC
57+
SectionDefinition:
58+
Length: 0
59+
NumberOfRelocations: 0
60+
NumberOfLinenumbers: 0
61+
CheckSum: 0
62+
Number: 1
63+
- Name: .data
64+
Value: 0
65+
SectionNumber: 2
66+
SimpleType: IMAGE_SYM_TYPE_NULL
67+
ComplexType: IMAGE_SYM_DTYPE_NULL
68+
StorageClass: IMAGE_SYM_CLASS_STATIC
69+
SectionDefinition:
70+
Length: 0
71+
NumberOfRelocations: 0
72+
NumberOfLinenumbers: 0
73+
CheckSum: 0
74+
Number: 2
75+
- Name: .bss
76+
Value: 0
77+
SectionNumber: 3
78+
SimpleType: IMAGE_SYM_TYPE_NULL
79+
ComplexType: IMAGE_SYM_DTYPE_NULL
80+
StorageClass: IMAGE_SYM_CLASS_STATIC
81+
SectionDefinition:
82+
Length: 0
83+
NumberOfRelocations: 0
84+
NumberOfLinenumbers: 0
85+
CheckSum: 0
86+
Number: 3
87+
- Name: .debug_str
88+
Value: 0
89+
SectionNumber: 4
90+
SimpleType: IMAGE_SYM_TYPE_NULL
91+
ComplexType: IMAGE_SYM_DTYPE_NULL
92+
StorageClass: IMAGE_SYM_CLASS_STATIC
93+
SectionDefinition:
94+
Length: 36
95+
NumberOfRelocations: 0
96+
NumberOfLinenumbers: 0
97+
CheckSum: 3440120581
98+
Number: 4
99+
- Name: .debug_abbrev
100+
Value: 0
101+
SectionNumber: 5
102+
SimpleType: IMAGE_SYM_TYPE_NULL
103+
ComplexType: IMAGE_SYM_DTYPE_NULL
104+
StorageClass: IMAGE_SYM_CLASS_STATIC
105+
SectionDefinition:
106+
Length: 14
107+
NumberOfRelocations: 0
108+
NumberOfLinenumbers: 0
109+
CheckSum: 2811829245
110+
Number: 5
111+
- Name: .debug_info
112+
Value: 0
113+
SectionNumber: 6
114+
SimpleType: IMAGE_SYM_TYPE_NULL
115+
ComplexType: IMAGE_SYM_DTYPE_NULL
116+
StorageClass: IMAGE_SYM_CLASS_STATIC
117+
SectionDefinition:
118+
Length: 27
119+
NumberOfRelocations: 4
120+
NumberOfLinenumbers: 0
121+
CheckSum: 1535600857
122+
Number: 6
123+
- Name: info_string0
124+
Value: 0
125+
SectionNumber: 4
126+
SimpleType: IMAGE_SYM_TYPE_NULL
127+
ComplexType: IMAGE_SYM_DTYPE_NULL
128+
StorageClass: IMAGE_SYM_CLASS_STATIC
129+
- Name: info_string1
130+
Value: 16
131+
SectionNumber: 4
132+
SimpleType: IMAGE_SYM_TYPE_NULL
133+
ComplexType: IMAGE_SYM_DTYPE_NULL
134+
StorageClass: IMAGE_SYM_CLASS_STATIC
135+
- Name: info_string2
136+
Value: 23
137+
SectionNumber: 4
138+
SimpleType: IMAGE_SYM_TYPE_NULL
139+
ComplexType: IMAGE_SYM_DTYPE_NULL
140+
StorageClass: IMAGE_SYM_CLASS_STATIC
141+
...

0 commit comments

Comments
 (0)