[lld] ELF: Introduce --shuffle-padding flag. (PR #117653)

via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 27 16:04:30 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) {
----------------
pcc wrote:

In the case you mention it won't necessarily be the case that start/stop symbols will break things. The distance between `MYOS_start` and `MYOS_end` will increase but a normal program shouldn't be able to notice this (modulo things like flash size limitations) just like it shouldn't be able to notice that the compiler was replaced with one that generates slightly larger code. Silently disabling the feature could lead to confusion so I'd prefer not to do that given that the user is opting into the feature with a flag.

https://github.com/llvm/llvm-project/pull/117653


More information about the llvm-commits mailing list