Skip to content

Commit 098b0d1

Browse files
authored
[LLD][AArch64] Detach Landing Pad creation from Thunk creation (#116402)
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 ef92aba commit 098b0d1

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

lld/ELF/Relocations.cpp

+28-14
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,30 @@ 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+
continue;
2307+
Thunk *lpt;
2308+
bool isNew;
2309+
auto &dr = cast<Defined>(t->destination);
2310+
std::tie(lpt, isNew) = getSyntheticLandingPad(dr, t->addend);
2311+
if (isNew) {
2312+
addressesChanged = true;
2313+
getISThunkSec(cast<InputSection>(dr.section))->addThunk(lpt);
2314+
}
2315+
t->landingPad = lpt->getThunkTargetSym();
2316+
}
2317+
return addressesChanged;
2318+
}
2319+
22962320
// Process all relocations from the InputSections that have been assigned
22972321
// to InputSectionDescriptions and redirect through Thunks if needed. The
22982322
// function should be called iteratively until it returns false.
@@ -2326,6 +2350,9 @@ bool ThunkCreator::createThunks(uint32_t pass,
23262350
if (pass == 0 && ctx.target->getThunkSectionSpacing())
23272351
createInitialThunkSections(outputSections);
23282352

2353+
if (ctx.arg.emachine == EM_AARCH64)
2354+
addressesChanged = addSyntheticLandingPads();
2355+
23292356
// Create all the Thunks and insert them into synthetic ThunkSections. The
23302357
// ThunkSections are later inserted back into InputSectionDescriptions.
23312358
// We separate the creation of ThunkSections from the insertion of the
@@ -2360,20 +2387,7 @@ bool ThunkCreator::createThunks(uint32_t pass,
23602387
ts = getISDThunkSec(os, isec, isd, rel, src);
23612388
ts->addThunk(t);
23622389
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-
}
2390+
allThunks.push_back(t);
23772391
}
23782392

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

lld/ELF/Relocations.h

+5
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)