Skip to content

Commit 5071b09

Browse files
author
Tarcisio Fischer
committed
Avoid _ret symbol and apply suggested documentation improvements
1 parent bdffde2 commit 5071b09

9 files changed

+80
-78
lines changed

lld/ELF/AArch64ErrataFix.cpp

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ static uint64_t scanCortexA53Errata843419(InputSection *isec, uint64_t &off,
370370

371371
class elf::Patch843419Section final : public SyntheticSection {
372372
public:
373-
Patch843419Section(Ctx &, InputSection *p, uint64_t off);
373+
Patch843419Section(Ctx &, InputSection *p, uint64_t off, Symbol* patcheeCodeSym);
374374

375375
void writeTo(uint8_t *buf) override;
376376

@@ -388,11 +388,9 @@ class elf::Patch843419Section final : public SyntheticSection {
388388
uint64_t patcheeOffset;
389389
// A label for the start of the Patch that we can use as a relocation target.
390390
Symbol *patchSym;
391-
// A label for the return location.
392-
Symbol *retSym;
393391
};
394392

395-
Patch843419Section::Patch843419Section(Ctx &ctx, InputSection *p, uint64_t off)
393+
Patch843419Section::Patch843419Section(Ctx &ctx, InputSection *p, uint64_t off, Symbol* patcheeCodeSym)
396394
: SyntheticSection(ctx, ".text.patch", SHT_PROGBITS,
397395
SHF_ALLOC | SHF_EXECINSTR, 4),
398396
patchee(p), patcheeOffset(off) {
@@ -401,12 +399,8 @@ Patch843419Section::Patch843419Section(Ctx &ctx, InputSection *p, uint64_t off)
401399
ctx, ctx.saver.save("__CortexA53843419_" + utohexstr(getLDSTAddr())),
402400
STT_FUNC, 0, getSize(), *this);
403401
addSyntheticLocal(ctx, ctx.saver.save("$x"), STT_NOTYPE, 0, 0, *this);
404-
retSym = addSyntheticLocal(
405-
ctx, ctx.saver.save("__CortexA53843419_" + utohexstr(getLDSTAddr()) + "_ret"),
406-
STT_FUNC, off + 4, 4, *p);
407-
408-
// Relocation must be created as soon as possible, so it'll be picked up.
409-
addReloc({R_PC, R_AARCH64_JUMP26, 4, 0, retSym});
402+
int64_t retToPatcheeSymOffset = (getLDSTAddr() - p->getVA(dyn_cast<Defined>(patcheeCodeSym)->value)) + 4;
403+
addReloc({R_PC, R_AARCH64_JUMP26, 4, retToPatcheeSymOffset, patcheeCodeSym});
410404
}
411405

412406
uint64_t Patch843419Section::getLDSTAddr() const {
@@ -418,10 +412,6 @@ void Patch843419Section::writeTo(uint8_t *buf) {
418412
// patchee Section.
419413
write32le(buf, read32le(patchee->content().begin() + patcheeOffset));
420414

421-
// Note: The jump back was configured in this classe's constructor, and
422-
// will be filled by the relocation. Adding the relocation here would be
423-
// too late.
424-
425415
// Apply relocations
426416
ctx.target->relocateAlloc(*this, buf);
427417
}
@@ -462,7 +452,7 @@ void AArch64Err843419Patcher::init() {
462452
// the same type. For example we must remove the redundant $d.1 from $x.0
463453
// $d.0 $d.1 $x.1.
464454
for (auto &kv : sectionMap) {
465-
std::vector<const Defined *> &mapSyms = kv.second;
455+
std::vector<Defined *> &mapSyms = kv.second;
466456
llvm::stable_sort(mapSyms, [](const Defined *a, const Defined *b) {
467457
return a->value < b->value;
468458
});
@@ -536,7 +526,8 @@ void AArch64Err843419Patcher::insertPatches(
536526
// Patches that we need to insert.
537527
static void implementPatch(Ctx &ctx, uint64_t adrpAddr, uint64_t patcheeOffset,
538528
InputSection *isec,
539-
std::vector<Patch843419Section *> &patches) {
529+
std::vector<Patch843419Section *> &patches,
530+
Symbol* patcheeCodeSym) {
540531
// There may be a relocation at the same offset that we are patching. There
541532
// are four cases that we need to consider.
542533
// Case 1: R_AARCH64_JUMP26 branch relocation. We have already patched this
@@ -561,7 +552,7 @@ static void implementPatch(Ctx &ctx, uint64_t adrpAddr, uint64_t patcheeOffset,
561552
Log(ctx) << "detected cortex-a53-843419 erratum sequence starting at " <<
562553
utohexstr(adrpAddr) << " in unpatched output.";
563554

564-
auto *ps = make<Patch843419Section>(ctx, isec, patcheeOffset);
555+
auto *ps = make<Patch843419Section>(ctx, isec, patcheeOffset, patcheeCodeSym);
565556
patches.push_back(ps);
566557

567558
auto makeRelToPatch = [](uint64_t offset, Symbol *patchSym) {
@@ -591,7 +582,7 @@ AArch64Err843419Patcher::patchInputSectionDescription(
591582
// mapping symbols of the same type. Our range of executable instructions to
592583
// scan is therefore [codeSym->value, dataSym->value) or [codeSym->value,
593584
// section size).
594-
std::vector<const Defined *> &mapSyms = sectionMap[isec];
585+
std::vector<Defined *> &mapSyms = sectionMap[isec];
595586

596587
auto codeSym = mapSyms.begin();
597588
while (codeSym != mapSyms.end()) {
@@ -604,7 +595,7 @@ AArch64Err843419Patcher::patchInputSectionDescription(
604595
uint64_t startAddr = isec->getVA(off);
605596
if (uint64_t patcheeOffset =
606597
scanCortexA53Errata843419(isec, off, limit))
607-
implementPatch(ctx, startAddr, patcheeOffset, isec, patches);
598+
implementPatch(ctx, startAddr, patcheeOffset, isec, patches, dyn_cast<Symbol>(*codeSym));
608599
}
609600
if (dataSym == mapSyms.end())
610601
break;

lld/ELF/AArch64ErrataFix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class AArch64Err843419Patcher {
3939
// A cache of the mapping symbols defined by the InputSection sorted in order
4040
// of ascending value with redundant symbols removed. These describe
4141
// the ranges of code and data in an executable InputSection.
42-
llvm::DenseMap<InputSection *, std::vector<const Defined *>> sectionMap;
42+
llvm::DenseMap<InputSection *, std::vector<Defined *>> sectionMap;
4343

4444
bool initialized = false;
4545
};

lld/ELF/Arch/AArch64.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ bool elf::isAArch64BTILandingPad(Ctx &ctx, Symbol &s, int64_t a) {
4949
return true;
5050
const uint8_t *buf = isec->content().begin();
5151
// Synthetic sections may have a size but empty data - Assume that they won't contain a landing pad
52-
if (buf == nullptr && dyn_cast<SyntheticSection>(isec) != nullptr) {
52+
if (buf == nullptr && isa<SyntheticSection>(isec))
5353
return false;
54-
}
5554

5655
const uint32_t instr = read32le(buf + off);
5756
// All BTI instructions are HINT instructions which all have same encoding

lld/ELF/Relocations.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,6 +2010,12 @@ ThunkSection *ThunkCreator::addThunkSection(OutputSection *os,
20102010
// 2.) The InputSectionDescription is larger than 4 KiB. This will prevent
20112011
// any assertion failures that an InputSectionDescription is < 4 KiB
20122012
// in size.
2013+
//
2014+
// isPrefix is a ThunkSection explicitly inserted before its target
2015+
// section. We suppress the rounding up of the size of these ThunkSections
2016+
// as unlike normal ThunkSections, they are small in size, but when BTI is
2017+
// enabled very frequent. This can bloat code-size and push the errata
2018+
// patches out of branch range.
20132019
uint64_t isdSize = isd->sections.back()->outSecOff +
20142020
isd->sections.back()->getSize() -
20152021
isd->sections.front()->outSecOff;

lld/test/ELF/aarch64-cortex-a53-843419-address.s

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
// CHECK-NEXT: ff8: d0000020 adrp x0, 0x6000
4242
// CHECK-NEXT: ffc: f9400021 ldr x1, [x1]
4343
// CHECK-NEXT: 1000: 14000ff9 b 0x4fe4
44-
// CHECK: <__CortexA53843419_1000_ret>:
4544
// CHECK-NEXT: 1004: d65f03c0 ret
4645
.section .text.01, "ax", %progbits
4746
.balign 4096
@@ -64,7 +63,6 @@ $x.999:
6463
// CHECK-NEXT: 1ffc: b0000020 adrp x0, 0x6000
6564
// CHECK-NEXT: 2000: bd400021 ldr s1, [x1]
6665
// CHECK-NEXT: 2004: 14000bfa b 0x4fec
67-
// CHECK: <__CortexA53843419_2004_ret>:
6866
// CHECK-NEXT: 2008: d65f03c0 ret
6967
.globl t3_ffc_ldrsimd
7068
.type t3_ffc_ldrsimd, %function
@@ -102,7 +100,6 @@ t3_ff8_ldralldata:
102100
// CHECK-NEXT: 3ff8: f0000000 adrp x0, 0x6000
103101
// CHECK-NEXT: 3ffc: f9400021 ldr x1, [x1]
104102
// CHECK-NEXT: 4000: 140003fd b 0x4ff4
105-
// CHECK: <__CortexA53843419_4000_ret>:
106103
// CHECK-NEXT: 4004: d65f03c0 ret
107104
.space 4096 - 12
108105
.globl t3_ffc_ldr
@@ -135,7 +132,6 @@ t3_ff8_ldralldata:
135132
// CHECK-NEXT: 4ffc: d0000000 adrp x0, 0x6000
136133
// CHECK-NEXT: 5000: f9000021 str x1, [x1]
137134
// CHECK-NEXT: 5004: 140003fb b 0x5ff0
138-
// CHECK: <__CortexA53843419_5004_ret>:
139135
// CHECK-NEXT: 5008: d65f03c0 ret
140136

141137
.section .newisd, "ax", %progbits
@@ -161,7 +157,6 @@ t3_ffc_str:
161157
// CHECK-NEXT: 5ff8: b0000000 adrp x0, 0x6000
162158
// CHECK-NEXT: 5ffc: f9000021 str x1, [x1]
163159
// CHECK-NEXT: 6000: 14000003 b 0x600c
164-
// CHECK: <__CortexA53843419_6000_ret>:
165160
// CHECK-NEXT: 6004: d65f03c0 ret
166161

167162
.section .newos, "ax", %progbits

lld/test/ELF/aarch64-cortex-a53-843419-large.s

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ t3_ff8_ldr:
4545
// CHECK3-NEXT: 211ff8: f00400e0 adrp x0, 0x8230000
4646
// CHECK3-NEXT: 211ffc: f9400021 ldr x1, [x1]
4747
// CHECK3-NEXT: 212000: 15800802 b 0x6214008
48-
// CHECK3: <__CortexA53843419_211000_ret>:
4948
// CHECK3-NEXT: 212004: d65f03c0 ret
5049

5150
.section .text.04, "ax", %progbits
@@ -67,7 +66,6 @@ t3_ff8_str:
6766
// CHECK4-NEXT: 4213ff8: b00200e0 adrp x0, 0x8230000
6867
// CHECK4-NEXT: 4213ffc: f9400021 ldr x1, [x1]
6968
// CHECK4-NEXT: 4214000: 14800004 b 0x6214010
70-
// CHECK4: <__CortexA53843419_4213000_ret>:
7169
// CHECK4-NEXT: 4214004: d65f03c0 ret
7270

7371
.section .text.06, "ax", %progbits
@@ -107,7 +105,6 @@ t3_ffc_ldr:
107105
// CHECK7-NEXT: 8211ffc: f00000e0 adrp x0, 0x8230000
108106
// CHECK7-NEXT: 8212000: f9400021 ldr x1, [x1]
109107
// CHECK7-NEXT: 8212004: 14000002 b 0x821200c
110-
// CHECK7: <__CortexA53843419_8212004_ret>:
111108
// CHECK7-NEXT: 8212008: d65f03c0 ret
112109
// CHECK7: <__CortexA53843419_8212004>:
113110
// CHECK7-NEXT: 821200c: f9400000 ldr x0, [x0]

0 commit comments

Comments
 (0)