Skip to content

Commit 2f1ab82

Browse files
vtjnashgiordano
authored andcommitted
[JITLink] Fix i686 R_386_32 and other relocation values (llvm#111091)
Fix R_386_32 and other relocations by correcting Addend computations. (cherry picked from commit 5716f83)
1 parent df73ee6 commit 2f1ab82

File tree

7 files changed

+60
-44
lines changed

7 files changed

+60
-44
lines changed

llvm/include/llvm/ExecutionEngine/JITLink/i386.h

+9-24
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,8 @@ enum EdgeKind_i386 : Edge::Kind {
3939
/// Represents a data/control flow instruction using PC-relative addressing
4040
/// to a target.
4141
///
42-
/// The fixup expression for this kind includes an implicit offset to account
43-
/// for the PC (unlike the Delta edges) so that a PCRel32 with a target
44-
/// T and addend zero is a call/branch to the start (offset zero) of T.
45-
///
4642
/// Fixup expression:
47-
/// Fixup <- Target - (Fixup + 4) + Addend : int32
43+
/// Fixup <- Target - Fixup + Addend : int32
4844
///
4945
/// Errors:
5046
/// - The result of the fixup expression must fit into an int32, otherwise
@@ -68,12 +64,8 @@ enum EdgeKind_i386 : Edge::Kind {
6864
/// Represents a data/control flow instruction using PC-relative addressing
6965
/// to a target.
7066
///
71-
/// The fixup expression for this kind includes an implicit offset to account
72-
/// for the PC (unlike the Delta edges) so that a PCRel16 with a target
73-
/// T and addend zero is a call/branch to the start (offset zero) of T.
74-
///
7567
/// Fixup expression:
76-
/// Fixup <- Target - (Fixup + 4) + Addend : int16
68+
/// Fixup <- Target - Fixup + Addend : int16
7769
///
7870
/// Errors:
7971
/// - The result of the fixup expression must fit into an int16, otherwise
@@ -86,7 +78,7 @@ enum EdgeKind_i386 : Edge::Kind {
8678
/// Delta from the fixup to the target.
8779
///
8880
/// Fixup expression:
89-
/// Fixup <- Target - Fixup + Addend : int64
81+
/// Fixup <- Target - Fixup + Addend : int32
9082
///
9183
/// Errors:
9284
/// - The result of the fixup expression must fit into an int32, otherwise
@@ -130,12 +122,8 @@ enum EdgeKind_i386 : Edge::Kind {
130122
/// Represents a PC-relative call or branch to a target. This can be used to
131123
/// identify, record, and/or patch call sites.
132124
///
133-
/// The fixup expression for this kind includes an implicit offset to account
134-
/// for the PC (unlike the Delta edges) so that a Branch32PCRel with a target
135-
/// T and addend zero is a call/branch to the start (offset zero) of T.
136-
///
137125
/// Fixup expression:
138-
/// Fixup <- Target - (Fixup + 4) + Addend : int32
126+
/// Fixup <- Target - Fixup + Addend : int32
139127
///
140128
/// Errors:
141129
/// - The result of the fixup expression must fit into an int32, otherwise
@@ -164,7 +152,7 @@ enum EdgeKind_i386 : Edge::Kind {
164152
/// target may be recorded to allow manipulation at runtime.
165153
///
166154
/// Fixup expression:
167-
/// Fixup <- Target - Fixup + Addend - 4 : int32
155+
/// Fixup <- Target - Fixup + Addend : int32
168156
///
169157
/// Errors:
170158
/// - The result of the fixup expression must fit into an int32, otherwise
@@ -180,7 +168,7 @@ enum EdgeKind_i386 : Edge::Kind {
180168
/// is within range of the fixup location.
181169
///
182170
/// Fixup expression:
183-
/// Fixup <- Target - Fixup + Addend - 4: int32
171+
/// Fixup <- Target - Fixup + Addend : int32
184172
///
185173
/// Errors:
186174
/// - The result of the fixup expression must fit into an int32, otherwise
@@ -215,8 +203,7 @@ inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E,
215203
}
216204

217205
case i386::PCRel32: {
218-
int32_t Value =
219-
E.getTarget().getAddress() - (FixupAddress + 4) + E.getAddend();
206+
int32_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
220207
*(little32_t *)FixupPtr = Value;
221208
break;
222209
}
@@ -231,8 +218,7 @@ inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E,
231218
}
232219

233220
case i386::PCRel16: {
234-
int32_t Value =
235-
E.getTarget().getAddress() - (FixupAddress + 4) + E.getAddend();
221+
int32_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
236222
if (LLVM_LIKELY(isInt<16>(Value)))
237223
*(little16_t *)FixupPtr = Value;
238224
else
@@ -257,8 +243,7 @@ inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E,
257243
case i386::BranchPCRel32:
258244
case i386::BranchPCRel32ToPtrJumpStub:
259245
case i386::BranchPCRel32ToPtrJumpStubBypassable: {
260-
int32_t Value =
261-
E.getTarget().getAddress() - (FixupAddress + 4) + E.getAddend();
246+
int32_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
262247
*(little32_t *)FixupPtr = Value;
263248
break;
264249
}

llvm/lib/ExecutionEngine/JITLink/ELF_i386.cpp

+17-3
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,29 @@ class ELFLinkGraphBuilder_i386 : public ELFLinkGraphBuilder<ELFT> {
186186
int64_t Addend = 0;
187187

188188
switch (*Kind) {
189-
case i386::EdgeKind_i386::Delta32: {
189+
case i386::EdgeKind_i386::None:
190+
break;
191+
case i386::EdgeKind_i386::Pointer32:
192+
case i386::EdgeKind_i386::PCRel32:
193+
case i386::EdgeKind_i386::RequestGOTAndTransformToDelta32FromGOT:
194+
case i386::EdgeKind_i386::Delta32:
195+
case i386::EdgeKind_i386::Delta32FromGOT:
196+
case i386::EdgeKind_i386::BranchPCRel32:
197+
case i386::EdgeKind_i386::BranchPCRel32ToPtrJumpStub:
198+
case i386::EdgeKind_i386::BranchPCRel32ToPtrJumpStubBypassable: {
190199
const char *FixupContent = BlockToFix.getContent().data() +
191200
(FixupAddress - BlockToFix.getAddress());
192-
Addend = *(const support::ulittle32_t *)FixupContent;
201+
Addend = *(const support::little32_t *)FixupContent;
193202
break;
194203
}
195-
default:
204+
case i386::EdgeKind_i386::Pointer16:
205+
case i386::EdgeKind_i386::PCRel16: {
206+
const char *FixupContent = BlockToFix.getContent().data() +
207+
(FixupAddress - BlockToFix.getAddress());
208+
Addend = *(const support::little16_t *)FixupContent;
196209
break;
197210
}
211+
}
198212

199213
Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
200214
Edge GE(*Kind, Offset, *GraphSymbol, Addend);

llvm/test/ExecutionEngine/JITLink/i386/ELF_i386_absolute_relocations_16.s

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@ main:
2222
.type bar,@function
2323
bar:
2424
retw $external_data
25-
.size bar, .-bar
25+
.size bar, .-bar
26+
27+
# jitlink-check: decode_operand(baz, 0) = external_data + 23
28+
.globl baz
29+
.align 2, 0x90
30+
.type baz,@function
31+
baz:
32+
retw $external_data+23
33+
.size baz, .-baz

llvm/test/ExecutionEngine/JITLink/i386/ELF_i386_absolute_relocations_32.s

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,25 @@
77
# Test ELF 32 bit absolute relocations
88

99
.text
10-
.globl main
10+
.globl main
1111
.p2align 4, 0x90
1212
.type main,@function
13-
main:
13+
main:
1414
retl
1515
.size main, .-main
1616

1717
# jitlink-check: decode_operand(foo, 0) = external_data
18-
.globl foo
18+
.globl foo
1919
.p2align 4, 0x90
2020
.type foo,@function
2121
foo:
2222
movl external_data, %eax
23-
.size foo, .-foo
23+
.size foo, .-foo
24+
25+
# jitlink-check: decode_operand(bar, 0) = external_data + 4000
26+
.globl bar
27+
.p2align 4, 0x90
28+
.type bar,@function
29+
bar:
30+
movl external_data + 4000, %eax
31+
.size bar, .-bar

llvm/test/ExecutionEngine/JITLink/i386/ELF_i386_pc_relative_relocations_32.s

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,18 @@ foo:
3333

3434

3535
# Tests PC relative relocation for negative offset from PC
36-
# jitlink-check: decode_operand(baz, 0) = fooz - next_pc(baz)
36+
# jitlink-check: decode_operand(baz, 0) = fooz - next_pc(baz) + 1
3737
.globl fooz
3838
.p2align 4
3939
.type fooz,@function
4040
fooz:
41+
nop
4142
retl
4243
.size fooz, .-fooz
4344

4445
.globl baz
4546
.p2align 4
4647
.type baz,@function
4748
baz:
48-
calll fooz
49-
.size baz, .-baz
49+
calll fooz+1
50+
.size baz, .-baz

llvm/test/ExecutionEngine/JITLink/i386/ELF_i386_small_pic_relocations_got.s

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,29 @@ main:
1919
# Test GOT32 handling.
2020
#
2121
# We want to check both the offset to the GOT entry and its contents.
22-
# jitlink-check: decode_operand(test_got, 4) = got_addr(elf_sm_pic_reloc_got.o, named_data1) - _GLOBAL_OFFSET_TABLE_
22+
# jitlink-check: decode_operand(test_got, 4) = got_addr(elf_sm_pic_reloc_got.o, named_data1) - _GLOBAL_OFFSET_TABLE_ + 42
2323
# jitlink-check: *{4}(got_addr(elf_sm_pic_reloc_got.o, named_data1)) = named_data1
2424
#
25-
# jitlink-check: decode_operand(test_got+6, 4) = got_addr(elf_sm_pic_reloc_got.o, named_data2) - _GLOBAL_OFFSET_TABLE_
25+
# jitlink-check: decode_operand(test_got+6, 4) = got_addr(elf_sm_pic_reloc_got.o, named_data2) - _GLOBAL_OFFSET_TABLE_ + 5
2626
# jitlink-check: *{4}(got_addr(elf_sm_pic_reloc_got.o, named_data2)) = named_data2
2727

2828
.globl test_got
2929
.p2align 4, 0x90
3030
.type test_got,@function
3131
test_got:
32-
leal named_data1@GOT, %eax
33-
leal named_data2@GOT, %eax
32+
leal named_data1@GOT+42, %eax
33+
leal named_data2@GOT+5, %eax
3434
.size test_got, .-test_got
3535

3636

3737

3838
# Test GOTOFF64 handling.
39-
# jitlink-check: decode_operand(test_gotoff, 1) = named_func - _GLOBAL_OFFSET_TABLE_
39+
# jitlink-check: decode_operand(test_gotoff, 1) = named_func - _GLOBAL_OFFSET_TABLE_ + 99
4040
.globl test_gotoff
4141
.p2align 4, 0x90
4242
.type test_gotoff,@function
4343
test_gotoff:
44-
mov $named_func@GOTOFF, %eax
44+
mov $named_func@GOTOFF+99, %eax
4545
.size test_gotoff, .-test_gotoff
4646

4747

llvm/test/ExecutionEngine/JITLink/i386/ELF_i386_small_pic_relocations_plt.s

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ main:
2727
# for position independent code, first, as there may be future use-cases
2828
# where we would want to disable the optimization.
2929
#
30-
# jitlink-check: decode_operand(test_call_extern_plt, 0) = external_func - next_pc(test_call_extern_plt)
30+
# jitlink-check: decode_operand(test_call_extern_plt, 0) = external_func - next_pc(test_call_extern_plt) + 53
3131
# jitlink-check: *{4}(got_addr(elf_sm_pic_reloc_plt.o, external_func))= external_func
3232
.globl test_call_extern_plt
3333
.p2align 4, 0x90
3434
.type test_call_extern_plt,@function
3535
test_call_extern_plt:
36-
call external_func@plt
36+
call external_func@plt + 53
3737

38-
.size test_call_extern_plt, .-test_call_extern_plt
38+
.size test_call_extern_plt, .-test_call_extern_plt

0 commit comments

Comments
 (0)