[lld] ELF: Introduce --shuffle-padding flag. (PR #117653)
Peter Smith via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 26 05:23:29 PST 2024
================
@@ -1444,6 +1444,38 @@ static void finalizeSynthetic(Ctx &ctx, SyntheticSection *sec) {
}
}
+static bool canInsertPadding(OutputSection *sec) {
+ StringRef s = sec->name;
+ return s == ".bss" || s == ".data" || s == ".data.rel.ro" || s == ".rodata" ||
+ s.starts_with(".text");
+}
+
+static void shufflePadding(Ctx &ctx) {
+ std::mt19937 g(*ctx.arg.shufflePadding);
+ PhdrEntry *curPtLoad = nullptr;
+ for (OutputSection *os : ctx.outputSections) {
+ if (!canInsertPadding(os))
+ continue;
+ for (SectionCommand *bc : os->commands) {
+ if (auto *isd = dyn_cast<InputSectionDescription>(bc)) {
+ SmallVector<InputSection *, 0> tmp;
+ if (os->ptLoad != curPtLoad) {
----------------
smithp35 wrote:
I think it will be worth checking to see if there is an explicit address expression associated with the output section before doing this. The implication being that someone could want the OutputSection at a specific address and adding padding could cause problems. Not likely a problem for an Linux style executable or shared object, but possible for linker scripts.
It may also be worth a quick check to see if there are any symbol assignments within the OutputSection.
```
MYOS : { MYOS_start = . ; *(.text.myos) ; MYOS_end = .; }
```
I think you are likely to be aiming at the default no linker script case where this isn't likely to happen. Could be worth just disabling ShufflePadding if anything other than InputSectionDescriptions are found?
https://github.com/llvm/llvm-project/pull/117653
More information about the llvm-commits
mailing list