Skip to content

Commit ec6ae99

Browse files
committed
[LLD][AArch64] Detach Landing Pad creation from Thunk creation
Move Landing Pad Creation to a new function that checks each thunk every pass to see if it needs a landing pad. This permits a thunk to be created without needing a landing pad, but later needing one due to drifting out of direct branch range and requiring an indirect branch. We record all the Thunks created so far in a new vector rather than trying to iterate over the DenseMap as we need a deterministic order of adding LandingPadThunks due to the short branch fall through. We cannot use normalizeExistingThunk() either as that only iterates through live thunks. Fixes: https://crbug.com/377438309 Original PR: #108989 Sending without a new test case to fix existing test. A new regression test will come in a separate PR as coming up with a small enough reproducer for this case is non-trivial.
1 parent fa5a10d commit ec6ae99

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

lld/ELF/Relocations.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,31 @@ bool ThunkCreator::normalizeExistingThunk(Relocation &rel, uint64_t src) {
22932293
return false;
22942294
}
22952295

2296+
// When indirect branches are restricted, such as AArch64 BTI Thunks may need
2297+
// to target a linker generated landing pad instead of the target. This needs
2298+
// to be done once per pass as the need for a BTI thunk is dependent whether
2299+
// a thunk is short or long. We iterate over all the thunks to make sure we
2300+
// catch thunks that have been created but are no longer live. Non-live thunks
2301+
// are not reachable via normalizeExistingThunk() but are still written.
2302+
bool ThunkCreator::addSyntheticLandingPads() {
2303+
bool addressesChanged = false;
2304+
for (Thunk *t : allThunks) {
2305+
if (t->needsSyntheticLandingPad()) {
2306+
Thunk *lpt;
2307+
bool isNew;
2308+
auto &dr = cast<Defined>(t->destination);
2309+
std::tie(lpt, isNew) = getSyntheticLandingPad(dr, t->addend);
2310+
if (isNew) {
2311+
addressesChanged = true;
2312+
ThunkSection *ts = getISThunkSec(cast<InputSection>(dr.section));
2313+
ts->addThunk(lpt);
2314+
}
2315+
t->landingPad = lpt->getThunkTargetSym();
2316+
}
2317+
}
2318+
return addressesChanged;
2319+
}
2320+
22962321
// Process all relocations from the InputSections that have been assigned
22972322
// to InputSectionDescriptions and redirect through Thunks if needed. The
22982323
// function should be called iteratively until it returns false.
@@ -2326,6 +2351,9 @@ bool ThunkCreator::createThunks(uint32_t pass,
23262351
if (pass == 0 && ctx.target->getThunkSectionSpacing())
23272352
createInitialThunkSections(outputSections);
23282353

2354+
if (ctx.arg.emachine == EM_AARCH64)
2355+
addressesChanged = addSyntheticLandingPads();
2356+
23292357
// Create all the Thunks and insert them into synthetic ThunkSections. The
23302358
// ThunkSections are later inserted back into InputSectionDescriptions.
23312359
// We separate the creation of ThunkSections from the insertion of the
@@ -2338,8 +2366,9 @@ bool ThunkCreator::createThunks(uint32_t pass,
23382366
uint64_t src = isec->getVA(rel.offset);
23392367

23402368
// If we are a relocation to an existing Thunk, check if it is
2341-
// still in range. If not then Rel will be altered to point to its
2342-
// original target so another Thunk can be generated.
2369+
// still in range, or if it needs a landing pad created. If not
2370+
// range then Rel will be altered to point to its original target
2371+
// so another Thunk can be generated.
23432372
if (pass > 0 && normalizeExistingThunk(rel, src))
23442373
continue;
23452374

@@ -2360,20 +2389,7 @@ bool ThunkCreator::createThunks(uint32_t pass,
23602389
ts = getISDThunkSec(os, isec, isd, rel, src);
23612390
ts->addThunk(t);
23622391
thunks[t->getThunkTargetSym()] = t;
2363-
2364-
// When indirect branches are restricted, such as AArch64 BTI
2365-
// Thunks may need to target a linker generated landing pad
2366-
// instead of the target.
2367-
if (t->needsSyntheticLandingPad()) {
2368-
Thunk *lpt;
2369-
auto &dr = cast<Defined>(t->destination);
2370-
std::tie(lpt, isNew) = getSyntheticLandingPad(dr, t->addend);
2371-
if (isNew) {
2372-
ts = getISThunkSec(cast<InputSection>(dr.section));
2373-
ts->addThunk(lpt);
2374-
}
2375-
t->landingPad = lpt->getThunkTargetSym();
2376-
}
2392+
allThunks.push_back(t);
23772393
}
23782394

23792395
// Redirect relocation to Thunk, we never go via the PLT to a Thunk

lld/ELF/Relocations.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ class ThunkCreator {
183183

184184
bool normalizeExistingThunk(Relocation &rel, uint64_t src);
185185

186+
bool addSyntheticLandingPads();
187+
186188
Ctx &ctx;
187189

188190
// Record all the available Thunks for a (Symbol, addend) pair, where Symbol
@@ -216,6 +218,9 @@ class ThunkCreator {
216218
Thunk *>
217219
landingPadsBySectionAndAddend;
218220

221+
// All the nonLandingPad thunks that have been created, in order of creation.
222+
std::vector<Thunk *> allThunks;
223+
219224
// The number of completed passes of createThunks this permits us
220225
// to do one time initialization on Pass 0 and put a limit on the
221226
// number of times it can be called to prevent infinite loops.

0 commit comments

Comments
 (0)