Skip to content

Commit c3d979d

Browse files
quic-akaryakiandrom3da
authored andcommitted
Update
1 parent 6ceb3a1 commit c3d979d

File tree

4 files changed

+72
-73
lines changed

4 files changed

+72
-73
lines changed

lld/ELF/Relocations.cpp

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,18 +2054,46 @@ void ThunkCreator::mergeThunks(ArrayRef<OutputSection *> outputSections) {
20542054
});
20552055
}
20562056

2057-
static int64_t getPCBias(Ctx &ctx, RelType type) {
2058-
if (ctx.arg.emachine != EM_ARM)
2059-
return 0;
2060-
switch (type) {
2061-
case R_ARM_THM_JUMP19:
2062-
case R_ARM_THM_JUMP24:
2063-
case R_ARM_THM_CALL:
2064-
return 4;
2065-
default:
2066-
return 8;
2057+
static const uint32_t HEXAGON_MASK_END_PACKET = 3 << 14;
2058+
static const uint32_t HEXAGON_END_OF_PACKET = 3 << 14;
2059+
static const uint32_t HEXAGON_END_OF_DUPLEX = 0 << 14;
2060+
2061+
// Return the distance between the packet start and the instruction in the
2062+
// relocation.
2063+
static int getHexagonPacketOffset(const InputSection &isec,
2064+
const Relocation &rel) {
2065+
const ArrayRef<uint8_t> SectContents = isec.content();
2066+
2067+
// Search back as many as 3 instructions.
2068+
for (unsigned i = 0;; i++) {
2069+
if (i == 3 || rel.offset < (i + 1) * 4)
2070+
return i * 4;
2071+
uint32_t instWord = 0;
2072+
const ArrayRef<uint8_t> InstWordContents =
2073+
SectContents.drop_front(rel.offset - (i + 1) * 4);
2074+
::memcpy(&instWord, InstWordContents.data(), sizeof(instWord));
2075+
if (((instWord & HEXAGON_MASK_END_PACKET) == HEXAGON_END_OF_PACKET) ||
2076+
((instWord & HEXAGON_MASK_END_PACKET) == HEXAGON_END_OF_DUPLEX))
2077+
return i * 4;
20672078
}
20682079
}
2080+
static int64_t getPCBias(Ctx &ctx, const InputSection &isec,
2081+
const Relocation &rel) {
2082+
if (ctx.arg.emachine == EM_ARM) {
2083+
switch (rel.type) {
2084+
case R_ARM_THM_JUMP19:
2085+
case R_ARM_THM_JUMP24:
2086+
case R_ARM_THM_CALL:
2087+
return 4;
2088+
default:
2089+
return 8;
2090+
}
2091+
}
2092+
if (ctx.arg.emachine == EM_HEXAGON) {
2093+
return -getHexagonPacketOffset(isec, rel);
2094+
}
2095+
return 0;
2096+
}
20692097

20702098
// Find or create a ThunkSection within the InputSectionDescription (ISD) that
20712099
// is in range of Src. An ISD maps to a range of InputSections described by a
@@ -2076,7 +2104,7 @@ ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *os,
20762104
const Relocation &rel,
20772105
uint64_t src) {
20782106
// See the comment in getThunk for -pcBias below.
2079-
const int64_t pcBias = getPCBias(ctx, rel.type);
2107+
const int64_t pcBias = getPCBias(ctx, *isec, rel);
20802108
for (std::pair<ThunkSection *, uint32_t> tp : isd->thunkSections) {
20812109
ThunkSection *ts = tp.first;
20822110
uint64_t tsBase = os->addr + ts->outSecOff - pcBias;
@@ -2237,7 +2265,7 @@ std::pair<Thunk *, bool> ThunkCreator::getThunk(InputSection *isec,
22372265
// out in the relocation addend. We compensate for the PC bias so that
22382266
// an Arm and Thumb relocation to the same destination get the same keyAddend,
22392267
// which is usually 0.
2240-
const int64_t pcBias = getPCBias(ctx, rel.type);
2268+
const int64_t pcBias = getPCBias(ctx, *isec, rel);
22412269
const int64_t keyAddend = rel.addend + pcBias;
22422270

22432271
// We use a ((section, offset), addend) pair to find the thunk position if
@@ -2383,7 +2411,7 @@ bool ThunkCreator::createThunks(uint32_t pass,
23832411
// STT_SECTION + non-zero addend, clear the addend after
23842412
// redirection.
23852413
if (ctx.arg.emachine != EM_MIPS)
2386-
rel.addend = -getPCBias(ctx, rel.type);
2414+
rel.addend = -getPCBias(ctx, *isec, rel);
23872415
}
23882416

23892417
for (auto &p : isd->thunkSections)

lld/ELF/Thunks.cpp

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -351,42 +351,16 @@ class AVRThunk : public Thunk {
351351
void addSymbols(ThunkSection &isec) override;
352352
};
353353

354-
static const uint32_t MASK_END_PACKET = 3 << 14;
355-
static const uint32_t END_OF_PACKET = 3 << 14;
356-
static const uint32_t END_OF_DUPLEX = 0 << 14;
357-
358-
static std::optional<int32_t> getRealAddend(const InputSection &isec,
359-
const Relocation &rel) {
360-
const ArrayRef<uint8_t> SectContents = isec.content();
361-
if (SectContents.size() < sizeof(int32_t))
362-
// FIXME: assert? emit a diagnostic?
363-
return std::nullopt;
364-
int32_t offset = rel.offset;
365-
366-
// Search as many as 4 instructions:
367-
for (int i = 0; i < 4; i++) {
368-
uint32_t instWord = 0;
369-
const ArrayRef<uint8_t> InstWordContents = SectContents.drop_front(offset);
370-
::memcpy(&instWord, InstWordContents.data(), sizeof(instWord));
371-
if (((instWord & MASK_END_PACKET) == END_OF_PACKET) ||
372-
((instWord & MASK_END_PACKET) == END_OF_DUPLEX))
373-
break;
374-
offset += sizeof(instWord);
375-
}
376-
return offset - rel.offset;
377-
}
378354
// Hexagon CPUs need thunks for <<FIXME TBD>>
379355
// when their destination is out of range [0, 0x_?].
380356
class HexagonThunk : public Thunk {
381357
public:
382358
HexagonThunk(Ctx &ctx, const InputSection &isec, Relocation &rel,
383359
Symbol &dest)
384-
: Thunk(ctx, dest, 0), RealAddend(getRealAddend(isec, rel)),
385-
RelOffset(rel.offset) {
360+
: Thunk(ctx, dest, 0), RelOffset(rel.offset) {
386361
alignment = 4;
387362
}
388-
std::optional<int32_t> RealAddend;
389-
int32_t RelOffset;
363+
uint32_t RelOffset;
390364
uint32_t size() override { return ctx.arg.isPic ? 12 : 8; }
391365
void writeTo(uint8_t *buf) override;
392366
void addSymbols(ThunkSection &isec) override;

lld/test/ELF/hexagon-thunks-packets.s

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,25 @@
1414
# CHECK: Disassembly of section .text:
1515

1616
# CHECK-NONPIC: 000200b4 <__trampoline_for_myfn_a_from_.text.thunk>:
17-
# CHECK-NONPIC: { immext(#0x800040)
18-
# CHECK-NONPIC: jump 0x820118 }
19-
# CHECK-NONPIC: 000200bc <__trampoline_for_myfn_a_from_.text.thunk>:
20-
# CHECK-NONPIC: { immext(#0x800040)
21-
# CHECK-NONPIC: jump 0x820118 }
17+
# CHECK-NONPIC: { immext(#0x1000040)
18+
# CHECK-NONPIC: jump 0x1020110 }
2219

2320
# CHECK-PIC: 00010150 <__trampoline_for_myfn_a_from_.text.thunk>:
24-
# CHECK-PIC: { immext(#0x800040)
25-
# CHECK-PIC: r14 = add(pc,##0x80006c) }
21+
# CHECK-PIC: { immext(#0x1000040)
22+
# CHECK-PIC: r14 = add(pc,##0x1000060) }
2623
# CHECK-PIC: { jumpr r14 }
2724

28-
# CHECK-NONPIC: 000200c4 <myfn_b>:
25+
# CHECK-NONPIC: 000200bc <myfn_b>:
2926
# CHECK-NONPIC: { jumpr r31 }
30-
# CHECK-PIC: 00010168 <myfn_b>:
27+
# CHECK-PIC: 0001015c <myfn_b>:
3128
# CHECK-PIC: { jumpr r31 }
3229
.globl myfn_b
3330
.type myfn_b, @function
3431
myfn_b:
3532
jumpr r31
3633
.size myfn_b, .-myfn_b
3734

38-
# CHECK-PIC: 0001016c <main>:
35+
# CHECK-PIC: 00010160 <main>:
3936
.globl main
4037
.type main, @function
4138
main:
@@ -48,13 +45,13 @@ main:
4845
# CHECK-PIC: call 0x10150
4946
# CHECK-NONPIC: call 0x200b4
5047
call myfn_b
51-
# CHECK-PIC: call 0x10168
52-
# CHECK-NONPIC: call 0x200c4
48+
# CHECK-PIC: call 0x1015c
49+
# CHECK-NONPIC: call 0x200bc
5350

5451
{ r2 = add(r0, r1)
5552
if (p0) call #myfn_b
5653
if (!p0) call #myfn_a }
57-
# CHECK-PIC: { if (p0) call 0x10168
54+
# CHECK-PIC: { if (p0) call 0x1015c
5855
# CHECK-PIC: if (!p0) call 0x10150
5956
# CHECK-NONPIC: { if (p0) call 0x200bc
6057
# CHECK-NONPIC: if (!p0) call 0x200b4
@@ -82,7 +79,7 @@ main:
8279
r4 = r5
8380
if (r0 <= #0) jump:t #myfn_a
8481
p1 = cmp.eq(r0, #0); if (p1.new) jump:nt #myfn_a }
85-
# CHECK-NONPIC: { if (r0==#0) jump:t 0x200b4
82+
# CHECK-NONPIC: { if (r0<=#0) jump:t 0x200b4
8683
# CHECK-NONPIC: p1 = cmp.eq(r0,#0x0); if (p1.new) jump:nt 0x200b4
8784
# CHECK-PIC: { if (r0<=#0) jump:t 0x10150
8885
# CHECK-PIC: p1 = cmp.eq(r0,#0x0); if (p1.new) jump:nt 0x10150
@@ -93,13 +90,13 @@ main:
9390
# CHECK-PIC: { r0 = #0x0 ; jump 0x10150 }
9491
# CHECK-NONPIC: { r0 = #0x0 ; jump 0x200b4 }
9592
{r0 = #0; jump #myfn_b}
96-
# CHECK-PIC: { r0 = #0x0 ; jump 0x10168 }
97-
# CHECK-NONPIC: { r0 = #0x0 ; jump 0x200c4 }
93+
# CHECK-PIC: { r0 = #0x0 ; jump 0x1015c }
94+
# CHECK-NONPIC: { r0 = #0x0 ; jump 0x200bc }
9895
jumpr r31
9996
.size main, .-main
10097

10198
.section .text.foo
102-
.skip 0x800000
99+
.skip 0x1000000
103100

104101
.globl myfn_a
105102
.type myfn_a, @function
@@ -108,15 +105,15 @@ myfn_a:
108105
jumpr r31
109106
.size myfn_a, .-myfn_a
110107

111-
# CHECK-NONPIC: 00820118 <myfn_a>:
112-
# CHECK-NONPIC: { r0 = #0x0 ; jump 0x820120 }
108+
# CHECK-NONPIC: 01020110 <myfn_a>:
109+
# CHECK-NONPIC: { r0 = #0x0 ; jump 0x1020118 }
113110
# CHECK-NONPIC: { jumpr r31 }
114111

115-
# CHECK-NONPIC: 00820120 <__trampoline_for_myfn_b_from_.text.thunk>:
116-
# CHECK-NONPIC: { immext(#0xff7fff80)
117-
# CHECK-NONPIC: jump 0x200c4 }
112+
# CHECK-NONPIC: 01020118 <__trampoline_for_myfn_b_from_.text.thunk>:
113+
# CHECK-NONPIC: { immext(#0xfeffff80)
114+
# CHECK-NONPIC: jump 0x200bc }
118115

119-
# CHECK-PIC: 008101c4 <__trampoline_for_myfn_b_from_.text.thunk>:
120-
# CHECK-PIC: { immext(#0xff7fff80)
121-
# CHECK-PIC: r14 = add(pc,##0xff7fffa4) } // fixme??
116+
# CHECK-PIC: 010101b8 <__trampoline_for_myfn_b_from_.text.thunk>:
117+
# CHECK-PIC: { immext(#0xfeffff80)
118+
# CHECK-PIC: r14 = add(pc,##0xfeffffa4) }
122119
# CHECK-PIC: { jumpr r14 }

lld/test/ELF/hexagon-thunks.s

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ main:
1515
jumpr r31
1616
.size main, .-main
1717

18-
.org 0x800000
18+
.org 0x1000000
1919

2020
.globl myfn
2121
.type myfn, @function
@@ -26,11 +26,11 @@ myfn:
2626
# CHECK: Disassembly of section .text:
2727

2828
# CHECK-NONPIC: 000200b4 <__trampoline_for_myfn_from_.text.thunk>:
29-
# CHECK-NONPIC: { immext(#0x800000)
30-
# CHECK-NONPIC: jump 0x8200bc }
29+
# CHECK-NONPIC: { immext(#0x1000000)
30+
# CHECK-NONPIC: jump 0x10200bc }
3131
# CHECK-PIC: 00010150 <__trampoline_for_myfn_from_.text.thunk>:
32-
# CHECK-PIC: { immext(#0x800000)
33-
# CHECK-PIC: r14 = add(pc,##0x80000c) }
32+
# CHECK-PIC: { immext(#0x1000000)
33+
# CHECK-PIC: r14 = add(pc,##0x100000c) }
3434
# CHECK-PIC: { jumpr r14 }
3535

3636
# CHECK-NONPIC: 000200bc <main>:
@@ -39,6 +39,6 @@ myfn:
3939
# CHECK-PIC: call 0x10150
4040
# CHECK: jumpr r31
4141

42-
# CHECK-NONPIC: 008200bc <myfn>:
43-
# CHECK-PIC: 0081015c <myfn>:
42+
# CHECK-NONPIC: 010200bc <myfn>:
43+
# CHECK-PIC: 0101015c <myfn>:
4444
# CHECK: jumpr r31

0 commit comments

Comments
 (0)