[lld] Fix lld crash wrt generated thunks growing away from the patched code (PR #170495)
Peter Smith via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 5 09:30:44 PST 2025
================
@@ -597,7 +595,7 @@ AArch64Err843419Patcher::patchInputSectionDescription(
uint64_t startAddr = isec->getVA(off);
if (uint64_t patcheeOffset =
scanCortexA53Errata843419(isec, off, limit))
- implementPatch(ctx, startAddr, patcheeOffset, isec, patches);
+ implementPatch(ctx, startAddr, patcheeOffset, isec, patches, dyn_cast<Symbol>(*codeSym));
----------------
smithp35 wrote:
It looks like you are using a mapping symbol like $x as the relocation target. Somewhat annoyingly the ABI says that it is an error for a relocation to target a mapping symbol (https://github.com/ARM-software/abi-aa/blob/main/aaelf64/aaelf64.rst#565mapping-symbols)
> The mapping symbols are defined in the [Mapping symbols table](https://github.com/ARM-software/abi-aa/blob/main/aaelf64/aaelf64.rst#mapping-symbols-table). It is an error for a relocation to reference a mapping symbol. Two forms of mapping symbol are supported:
Normally the linker output won't contain these relocations so no-one would be able to tell. There's an option called `-emit-relocs` which is used by BOLT to do binary analysis that will output the relocations. It is possible that these relocations to mapping symbols will lead to errors in other tools.
This is shame as using mapping symbols would be convenient.
I recommend when constructing the `sectionMap` we also have a
```
llvm::DenseMap<InputSection *, std::vector<Defined *>> sectionSymbolMap;
```
When iterating through the local symbols to construct section Map, we can construct a sectionSymbolMap too.
```
// Collect mapping symbols and Section symbols for every executable InputSection.
for (ELFFileBase *file : ctx.objectFiles) {
for (Symbol *b : file->getLocalSymbols()) {
auto *def = dyn_cast<Defined>(b);
if (!def)
continue;
if (!def->isSection && !isCodeMapSymbol(def) && !isDataMapSymbol(def))
continue;
if (auto *sec = dyn_cast_or_null<InputSection>(def->section)) {
if (def->isSection())
sectionSymbolMap[sec].push_back(def);
else if (sec->flags & SHF_EXECINSTR)
sectionMap[sec].push_back(def);
}
}
}
```
In patchInputSectionDescription we can then do
```
Defined *sectionSymbol = sectionSymbolMap[isec];
```
Which can be passed to implementPatch instead of codeSym.
While I don't know of any code-generator that doesn't add a Section Symbol for each allocatable section, it is not required by the ELF specification. If `sectionSymbolMap[isec]` returns `nullptr` we can call addSyntheticLocal() with a symbol type of STT_SECTION, offset of 0, size 0, and the empty string "" as a name to generate one. Something like:
```
sectionSymbol = addSyntheticLocal("", STT_SECTION, 0, 0, isec);
```
As an aside it looks like isDataMapSymbol() isn't used for anything significant. It looks like it can be removed.
https://github.com/llvm/llvm-project/pull/170495
More information about the llvm-commits
mailing list