[lld] ELF: Introduce --randomize-section-padding option. (PR #117653)

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 11 18:28:12 PST 2024


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

>From 75ad826539205c52e1d9502938bb814ce914853b Mon Sep 17 00:00:00 2001
From: Peter Collingbourne <peter at pcc.me.uk>
Date: Mon, 25 Nov 2024 16:43:33 -0800
Subject: [PATCH 1/6] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
 =?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.6-beta.1
---
 lld/ELF/Config.h               |  1 +
 lld/ELF/Driver.cpp             |  2 ++
 lld/ELF/Options.td             |  3 +++
 lld/ELF/OutputSections.h       |  4 ++--
 lld/ELF/SyntheticSections.cpp  | 15 +++++++++++++++
 lld/ELF/SyntheticSections.h    |  9 +++++++++
 lld/ELF/Writer.cpp             | 34 ++++++++++++++++++++++++++++++++++
 lld/docs/ld.lld.1              |  9 +++++++++
 lld/test/ELF/shuffle-padding.s | 29 +++++++++++++++++++++++++++++
 9 files changed, 104 insertions(+), 2 deletions(-)
 create mode 100644 lld/test/ELF/shuffle-padding.s

diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index a2836733c2715e..ed6bce405d1664 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -329,6 +329,7 @@ struct Config {
   bool relrPackDynRelocs = false;
   llvm::DenseSet<llvm::StringRef> saveTempsArgs;
   llvm::SmallVector<std::pair<llvm::GlobPattern, uint32_t>, 0> shuffleSections;
+  std::optional<uint64_t> shufflePadding;
   bool singleRoRx;
   bool shared;
   bool symbolic;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index bc4b967ccbbbb4..d8bcbe4a2d19d8 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -1410,6 +1410,8 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
   ctx.arg.searchPaths = args::getStrings(args, OPT_library_path);
   ctx.arg.sectionStartMap = getSectionStartMap(ctx, args);
   ctx.arg.shared = args.hasArg(OPT_shared);
+  if (args.hasArg(OPT_shuffle_padding))
+    ctx.arg.shufflePadding = args::getInteger(args, OPT_shuffle_padding, 0);
   ctx.arg.singleRoRx = !args.hasFlag(OPT_rosegment, OPT_no_rosegment, true);
   ctx.arg.soName = args.getLastArgValue(OPT_soname);
   ctx.arg.sortSection = getSortSection(ctx, args);
diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td
index ebe77204264210..44ac0ee43a8502 100644
--- a/lld/ELF/Options.td
+++ b/lld/ELF/Options.td
@@ -434,6 +434,9 @@ defm section_start: Eq<"section-start", "Set address of section">,
 
 def shared: F<"shared">, HelpText<"Build a shared object">;
 
+def shuffle_padding: JJ<"shuffle-padding=">,
+  HelpText<"Randomly insert padding between input sections using given seed">;
+
 defm soname: Eq<"soname", "Set DT_SONAME">;
 
 defm sort_section:
diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h
index 67191392d1dbe7..3ab36a21ce488d 100644
--- a/lld/ELF/OutputSections.h
+++ b/lld/ELF/OutputSections.h
@@ -124,14 +124,14 @@ class OutputSection final : public SectionBase {
   void sortInitFini();
   void sortCtorsDtors();
 
+  std::array<uint8_t, 4> getFiller(Ctx &);
+
   // Used for implementation of --compress-debug-sections and
   // --compress-sections.
   CompressedData compressed;
 
 private:
   SmallVector<InputSection *, 0> storage;
-
-  std::array<uint8_t, 4> getFiller(Ctx &);
 };
 
 struct OutputDesc final : SectionCommand {
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 21fe2a25fa1bd2..70eca0e58036e0 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -2753,6 +2753,21 @@ RelroPaddingSection::RelroPaddingSection(Ctx &ctx)
     : SyntheticSection(ctx, ".relro_padding", SHT_NOBITS, SHF_ALLOC | SHF_WRITE,
                        1) {}
 
+ShufflePaddingSection::ShufflePaddingSection(Ctx &ctx, uint64_t size,
+                                             OutputSection *parent)
+    : SyntheticSection(ctx, ".shuffle_padding", SHF_ALLOC, SHT_PROGBITS, 1),
+      size(size) {
+  this->parent = parent;
+}
+
+void ShufflePaddingSection::writeTo(uint8_t *buf) {
+  std::array<uint8_t, 4> filler = getParent()->getFiller(ctx);
+  uint8_t *end = buf + size;
+  for (; buf + 4 <= end; buf += 4)
+    memcpy(buf, &filler[0], 4);
+  memcpy(buf, &filler[0], end - buf);
+}
+
 // The string hash function for .gdb_index.
 static uint32_t computeGdbHash(StringRef s) {
   uint32_t h = 0;
diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h
index 4b643e86335510..177a0337607da8 100644
--- a/lld/ELF/SyntheticSections.h
+++ b/lld/ELF/SyntheticSections.h
@@ -796,6 +796,15 @@ class RelroPaddingSection final : public SyntheticSection {
   void writeTo(uint8_t *buf) override {}
 };
 
+class ShufflePaddingSection final : public SyntheticSection {
+  uint64_t size;
+
+public:
+  ShufflePaddingSection(Ctx &ctx, uint64_t size, OutputSection *parent);
+  size_t getSize() const override { return size; }
+  void writeTo(uint8_t *buf) override;
+};
+
 // Used by the merged DWARF32 .debug_names (a per-module index). If we
 // move to DWARF64, most of this data will need to be re-sized.
 class DebugNamesBaseSection : public SyntheticSection {
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index a7fbdc07907044..3c1ab3234801d9 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1444,6 +1444,37 @@ 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) {
+          tmp.push_back(
+              make<ShufflePaddingSection>(ctx, g() % ctx.arg.maxPageSize, os));
+          curPtLoad = os->ptLoad;
+        }
+        for (InputSection *isec : isd->sections) {
+          if (g() < (1<<28))
+            tmp.push_back(make<ShufflePaddingSection>(ctx, isec->addralign, os));
+          tmp.push_back(isec);
+        }
+        isd->sections = std::move(tmp);
+      }
+    }
+  }
+}
+
 // We need to generate and finalize the content that depends on the address of
 // InputSections. As the generation of the content may also alter InputSection
 // addresses we must converge to a fixed point. We do that here. See the comment
@@ -1470,6 +1501,9 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
   if (ctx.arg.emachine == EM_HEXAGON)
     hexagonTLSSymbolUpdate(ctx);
 
+  if (ctx.arg.shufflePadding)
+    shufflePadding(ctx);
+
   uint32_t pass = 0, assignPasses = 0;
   for (;;) {
     bool changed = ctx.target->needsThunks
diff --git a/lld/docs/ld.lld.1 b/lld/docs/ld.lld.1
index b22cb362837715..5d3d4164622166 100644
--- a/lld/docs/ld.lld.1
+++ b/lld/docs/ld.lld.1
@@ -577,6 +577,15 @@ were concatenated in the order they appeared on the command line.
 Set address of section.
 .It Fl -shared , Fl -Bsharable
 Build a shared object.
+.It Fl -shuffle-padding Ns = Ns Ar seed
+Randomly insert padding between input sections using the given seed.
+Padding is inserted into output sections with names matching the following patterns:
+.Cm .bss ,
+.Cm .data ,
+.Cm .data.rel.ro ,
+.Cm .rodata
+and
+.Cm .text* .
 .It Fl -shuffle-sections Ns = Ns Ar seed
 Shuffle matched sections using the given seed before mapping them to the output sections.
 If -1, reverse the section order. If 0, use a random seed.
diff --git a/lld/test/ELF/shuffle-padding.s b/lld/test/ELF/shuffle-padding.s
new file mode 100644
index 00000000000000..f816100ffba1be
--- /dev/null
+++ b/lld/test/ELF/shuffle-padding.s
@@ -0,0 +1,29 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+
+# RUN: ld.lld %t.o -o %t.out
+# RUN: llvm-readelf -x .rodata %t.out | FileCheck %s
+# CHECK: Hex dump of section '.rodata':
+# CHECK-NEXT: 0x00201120 010203
+
+## --shuffle-padding= inserts segment offset padding and pre-section padding.
+# RUN: ld.lld --shuffle-padding=1 %t.o -o %t.out
+# RUN: llvm-readelf -x .rodata %t.out | FileCheck --check-prefix=PAD1 %s
+# PAD1: Hex dump of section '.rodata':
+# PAD1-NEXT: 0x00201548 0102cc03
+
+## Size of segment offset padding and location of pre-section padding is
+## dependent on the seed.
+# RUN: ld.lld --shuffle-padding=2 %t.o -o %t.out
+# RUN: llvm-readelf -x .rodata %t.out | FileCheck --check-prefix=PAD2 %s
+# PAD2: Hex dump of section '.rodata':
+# PAD2-NEXT: 0x00201dc8 cc010203
+
+.section .rodata.a,"ax"
+.byte 1
+
+.section .rodata.b,"ax"
+.byte 2
+
+.section .rodata.c,"ax"
+.byte 3

>From c420199d1ce7b6e065924aabb4f1a3edc0b45d14 Mon Sep 17 00:00:00 2001
From: Peter Collingbourne <peter at pcc.me.uk>
Date: Mon, 25 Nov 2024 16:51:54 -0800
Subject: [PATCH 2/6] format

Created using spr 1.3.6-beta.1
---
 lld/ELF/Writer.cpp | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 3c1ab3234801d9..7591b5c0ff24d2 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1445,9 +1445,9 @@ 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");
+  StringRef s = sec->name;
+  return s == ".bss" || s == ".data" || s == ".data.rel.ro" || s == ".rodata" ||
+         s.starts_with(".text");
 }
 
 static void shufflePadding(Ctx &ctx) {
@@ -1465,8 +1465,9 @@ static void shufflePadding(Ctx &ctx) {
           curPtLoad = os->ptLoad;
         }
         for (InputSection *isec : isd->sections) {
-          if (g() < (1<<28))
-            tmp.push_back(make<ShufflePaddingSection>(ctx, isec->addralign, os));
+          if (g() < (1 << 28))
+            tmp.push_back(
+                make<ShufflePaddingSection>(ctx, isec->addralign, os));
           tmp.push_back(isec);
         }
         isd->sections = std::move(tmp);

>From 667e7bad3979d6e527f63e6ee3e9803073e0c6ed Mon Sep 17 00:00:00 2001
From: Peter Collingbourne <peter at pcc.me.uk>
Date: Wed, 27 Nov 2024 16:07:21 -0800
Subject: [PATCH 3/6] Add tests

Created using spr 1.3.6-beta.1
---
 lld/test/ELF/shuffle-padding-bss.s            | 23 ++++++++++++
 lld/test/ELF/shuffle-padding-data.s           | 36 +++++++++++++++++++
 ...fle-padding.s => shuffle-padding-rodata.s} | 19 ++++++----
 lld/test/ELF/shuffle-padding-text.s           | 26 ++++++++++++++
 4 files changed, 98 insertions(+), 6 deletions(-)
 create mode 100644 lld/test/ELF/shuffle-padding-bss.s
 create mode 100644 lld/test/ELF/shuffle-padding-data.s
 rename lld/test/ELF/{shuffle-padding.s => shuffle-padding-rodata.s} (62%)
 create mode 100644 lld/test/ELF/shuffle-padding-text.s

diff --git a/lld/test/ELF/shuffle-padding-bss.s b/lld/test/ELF/shuffle-padding-bss.s
new file mode 100644
index 00000000000000..f56916785fb791
--- /dev/null
+++ b/lld/test/ELF/shuffle-padding-bss.s
@@ -0,0 +1,23 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+
+## --shuffle-padding= inserts segment offset padding and pre-section padding,
+## and does not affect .bss flags.
+# RUN: ld.lld --shuffle-padding=1 %t.o -o %t.out
+# RUN: llvm-readelf -sS %t.out | FileCheck --check-prefix=HEADER %s
+# HEADER: .bss              NOBITS          0000000000202580 000580 000f90 00  WA  0   0  1
+# HEADER: 1: 000000000020350c     0 NOTYPE  LOCAL  DEFAULT     2 a
+# HEADER: 2: 000000000020350e     0 NOTYPE  LOCAL  DEFAULT     2 b
+# HEADER: 3: 000000000020350f     0 NOTYPE  LOCAL  DEFAULT     2 c
+
+.section .bss.a,"a", at nobits
+a:
+.zero 1
+
+.section .bss.b,"a", at nobits
+b:
+.zero 1
+
+.section .bss.c,"a", at nobits
+c:
+.zero 1
diff --git a/lld/test/ELF/shuffle-padding-data.s b/lld/test/ELF/shuffle-padding-data.s
new file mode 100644
index 00000000000000..431c79214dd791
--- /dev/null
+++ b/lld/test/ELF/shuffle-padding-data.s
@@ -0,0 +1,36 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+
+# RUN: ld.lld %t.o -o %t.out
+# RUN: llvm-readelf -x .data %t.out | FileCheck %s
+# CHECK: Hex dump of section '.data':
+# CHECK-NEXT: 0x00202158 010203
+
+## --shuffle-padding= inserts segment offset padding and pre-section padding.
+# RUN: ld.lld --shuffle-padding=1 %t.o -o %t.out
+# RUN: llvm-readelf -x .data %t.out | FileCheck --check-prefix=PAD1 %s
+# PAD1: Hex dump of section '.data':
+# PAD1: 0x00203500 00000000 00000000 00000000 01000203
+
+## --shuffle-padding=  does not affect .rodata flags.
+# RUN: llvm-readelf -S %t.out | FileCheck --check-prefix=HEADER %s
+# HEADER: .data             PROGBITS        0000000000202580 000580 000f90 00  WA  0   0  1
+
+## Size of segment offset padding and location of pre-section padding is
+## dependent on the seed.
+# RUN: ld.lld --shuffle-padding=2 %t.o -o %t.out
+# RUN: llvm-readelf -x .data %t.out | FileCheck --check-prefix=PAD2 %s
+# PAD2: Hex dump of section '.data':
+# PAD2: 0x002037e0 00000000 00000000 00000000 00010203
+
+.section .data.a,"aw", at progbits
+a:
+.byte 1
+
+.section .data.b,"aw", at progbits
+b:
+.byte 2
+
+.section .data.c,"aw", at progbits
+c:
+.byte 3
diff --git a/lld/test/ELF/shuffle-padding.s b/lld/test/ELF/shuffle-padding-rodata.s
similarity index 62%
rename from lld/test/ELF/shuffle-padding.s
rename to lld/test/ELF/shuffle-padding-rodata.s
index f816100ffba1be..35f18fcc12c10c 100644
--- a/lld/test/ELF/shuffle-padding.s
+++ b/lld/test/ELF/shuffle-padding-rodata.s
@@ -4,26 +4,33 @@
 # RUN: ld.lld %t.o -o %t.out
 # RUN: llvm-readelf -x .rodata %t.out | FileCheck %s
 # CHECK: Hex dump of section '.rodata':
-# CHECK-NEXT: 0x00201120 010203
+# CHECK-NEXT: 0x00200120 010203
 
 ## --shuffle-padding= inserts segment offset padding and pre-section padding.
 # RUN: ld.lld --shuffle-padding=1 %t.o -o %t.out
 # RUN: llvm-readelf -x .rodata %t.out | FileCheck --check-prefix=PAD1 %s
 # PAD1: Hex dump of section '.rodata':
-# PAD1-NEXT: 0x00201548 0102cc03
+# PAD1: 0x00200540 00000000 00010203
+
+## --shuffle-padding=  does not affect .rodata flags.
+# RUN: llvm-readelf -S %t.out | FileCheck --check-prefix=HEADER %s
+# HEADER: .rodata           PROGBITS        0000000000200120 000120 000428 00   A  0   0  1
 
 ## Size of segment offset padding and location of pre-section padding is
 ## dependent on the seed.
 # RUN: ld.lld --shuffle-padding=2 %t.o -o %t.out
 # RUN: llvm-readelf -x .rodata %t.out | FileCheck --check-prefix=PAD2 %s
 # PAD2: Hex dump of section '.rodata':
-# PAD2-NEXT: 0x00201dc8 cc010203
+# PAD2: 0x00200dc0 00000000 00000000 01000203
 
-.section .rodata.a,"ax"
+.section .rodata.a,"a", at progbits
+a:
 .byte 1
 
-.section .rodata.b,"ax"
+.section .rodata.b,"a", at progbits
+b:
 .byte 2
 
-.section .rodata.c,"ax"
+.section .rodata.c,"a", at progbits
+c:
 .byte 3
diff --git a/lld/test/ELF/shuffle-padding-text.s b/lld/test/ELF/shuffle-padding-text.s
new file mode 100644
index 00000000000000..5f9cafe49fa289
--- /dev/null
+++ b/lld/test/ELF/shuffle-padding-text.s
@@ -0,0 +1,26 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+
+# RUN: ld.lld %t.o -o %t.out
+# RUN: llvm-readelf -x .text %t.out | FileCheck %s
+# CHECK: Hex dump of section '.text':
+# CHECK-NEXT: 0x00201120 010203
+
+## --shuffle-padding= inserts segment offset padding and pre-section padding.
+# RUN: ld.lld --shuffle-padding=1 %t.o -o %t.out
+# RUN: llvm-readelf -x .text %t.out | FileCheck --check-prefix=PAD1 %s
+# PAD1: Hex dump of section '.text':
+# PAD1: 0x00201540 cccccccc cccccccc 0102cc03
+
+## --shuffle-padding=  does not affect .text flags.
+# RUN: llvm-readelf -S %t.out | FileCheck --check-prefix=HEADER %s
+# HEADER: .text             PROGBITS        0000000000201120 000120 00042c 00  AX  0   0  4
+
+.section .text.a,"ax"
+.byte 1
+
+.section .text.b,"ax"
+.byte 2
+
+.section .text.c,"ax"
+.byte 3

>From 678959e8307f45b929841547ba62799bf8f21591 Mon Sep 17 00:00:00 2001
From: Peter Collingbourne <peter at pcc.me.uk>
Date: Wed, 4 Dec 2024 21:11:14 -0800
Subject: [PATCH 4/6] Address review comments

Created using spr 1.3.6-beta.1
---
 lld/ELF/Config.h                         |  2 +-
 lld/ELF/Driver.cpp                       |  5 +-
 lld/ELF/Options.td                       |  2 +-
 lld/ELF/SyntheticSections.cpp            |  8 +--
 lld/ELF/SyntheticSections.h              |  4 +-
 lld/ELF/Writer.cpp                       | 17 ++---
 lld/docs/ld.lld.1                        | 18 ++---
 lld/test/ELF/randomize-section-padding.s | 88 ++++++++++++++++++++++++
 lld/test/ELF/shuffle-padding-bss.s       | 23 -------
 lld/test/ELF/shuffle-padding-data.s      | 36 ----------
 lld/test/ELF/shuffle-padding-rodata.s    | 36 ----------
 lld/test/ELF/shuffle-padding-text.s      | 26 -------
 12 files changed, 117 insertions(+), 148 deletions(-)
 create mode 100644 lld/test/ELF/randomize-section-padding.s
 delete mode 100644 lld/test/ELF/shuffle-padding-bss.s
 delete mode 100644 lld/test/ELF/shuffle-padding-data.s
 delete mode 100644 lld/test/ELF/shuffle-padding-rodata.s
 delete mode 100644 lld/test/ELF/shuffle-padding-text.s

diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index ed6bce405d1664..5b6b332cd597df 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -320,6 +320,7 @@ struct Config {
   bool printGcSections;
   bool printIcfSections;
   bool printMemoryUsage;
+  std::optional<uint64_t> randomizeSectionPadding;
   bool rejectMismatch;
   bool relax;
   bool relaxGP;
@@ -329,7 +330,6 @@ struct Config {
   bool relrPackDynRelocs = false;
   llvm::DenseSet<llvm::StringRef> saveTempsArgs;
   llvm::SmallVector<std::pair<llvm::GlobPattern, uint32_t>, 0> shuffleSections;
-  std::optional<uint64_t> shufflePadding;
   bool singleRoRx;
   bool shared;
   bool symbolic;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index d8bcbe4a2d19d8..a207ff9325454d 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -1410,8 +1410,9 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
   ctx.arg.searchPaths = args::getStrings(args, OPT_library_path);
   ctx.arg.sectionStartMap = getSectionStartMap(ctx, args);
   ctx.arg.shared = args.hasArg(OPT_shared);
-  if (args.hasArg(OPT_shuffle_padding))
-    ctx.arg.shufflePadding = args::getInteger(args, OPT_shuffle_padding, 0);
+  if (args.hasArg(OPT_randomize_section_padding))
+    ctx.arg.randomizeSectionPadding =
+        args::getInteger(args, OPT_randomize_section_padding, 0);
   ctx.arg.singleRoRx = !args.hasFlag(OPT_rosegment, OPT_no_rosegment, true);
   ctx.arg.soName = args.getLastArgValue(OPT_soname);
   ctx.arg.sortSection = getSortSection(ctx, args);
diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td
index 44ac0ee43a8502..42f9a6533541ba 100644
--- a/lld/ELF/Options.td
+++ b/lld/ELF/Options.td
@@ -434,7 +434,7 @@ defm section_start: Eq<"section-start", "Set address of section">,
 
 def shared: F<"shared">, HelpText<"Build a shared object">;
 
-def shuffle_padding: JJ<"shuffle-padding=">,
+def randomize_section_padding: JJ<"randomize-section-padding=">,
   HelpText<"Randomly insert padding between input sections using given seed">;
 
 defm soname: Eq<"soname", "Set DT_SONAME">;
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 70eca0e58036e0..2282138dbad16a 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -2753,14 +2753,14 @@ RelroPaddingSection::RelroPaddingSection(Ctx &ctx)
     : SyntheticSection(ctx, ".relro_padding", SHT_NOBITS, SHF_ALLOC | SHF_WRITE,
                        1) {}
 
-ShufflePaddingSection::ShufflePaddingSection(Ctx &ctx, uint64_t size,
-                                             OutputSection *parent)
-    : SyntheticSection(ctx, ".shuffle_padding", SHF_ALLOC, SHT_PROGBITS, 1),
+RandomizePaddingSection::RandomizePaddingSection(Ctx &ctx, uint64_t size,
+                                                 OutputSection *parent)
+    : SyntheticSection(ctx, ".randomize_padding", SHT_PROGBITS, SHF_ALLOC, 1),
       size(size) {
   this->parent = parent;
 }
 
-void ShufflePaddingSection::writeTo(uint8_t *buf) {
+void RandomizePaddingSection::writeTo(uint8_t *buf) {
   std::array<uint8_t, 4> filler = getParent()->getFiller(ctx);
   uint8_t *end = buf + size;
   for (; buf + 4 <= end; buf += 4)
diff --git a/lld/ELF/SyntheticSections.h b/lld/ELF/SyntheticSections.h
index 177a0337607da8..132513cbd3b796 100644
--- a/lld/ELF/SyntheticSections.h
+++ b/lld/ELF/SyntheticSections.h
@@ -796,11 +796,11 @@ class RelroPaddingSection final : public SyntheticSection {
   void writeTo(uint8_t *buf) override {}
 };
 
-class ShufflePaddingSection final : public SyntheticSection {
+class RandomizePaddingSection final : public SyntheticSection {
   uint64_t size;
 
 public:
-  ShufflePaddingSection(Ctx &ctx, uint64_t size, OutputSection *parent);
+  RandomizePaddingSection(Ctx &ctx, uint64_t size, OutputSection *parent);
   size_t getSize() const override { return size; }
   void writeTo(uint8_t *buf) override;
 };
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 7591b5c0ff24d2..2cb04e6f290116 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1450,8 +1450,8 @@ static bool canInsertPadding(OutputSection *sec) {
          s.starts_with(".text");
 }
 
-static void shufflePadding(Ctx &ctx) {
-  std::mt19937 g(*ctx.arg.shufflePadding);
+static void randomizeSectionPadding(Ctx &ctx) {
+  std::mt19937 g(*ctx.arg.randomizeSectionPadding);
   PhdrEntry *curPtLoad = nullptr;
   for (OutputSection *os : ctx.outputSections) {
     if (!canInsertPadding(os))
@@ -1460,14 +1460,15 @@ static void shufflePadding(Ctx &ctx) {
       if (auto *isd = dyn_cast<InputSectionDescription>(bc)) {
         SmallVector<InputSection *, 0> tmp;
         if (os->ptLoad != curPtLoad) {
-          tmp.push_back(
-              make<ShufflePaddingSection>(ctx, g() % ctx.arg.maxPageSize, os));
+          tmp.push_back(make<RandomizePaddingSection>(
+              ctx, g() % ctx.arg.maxPageSize, os));
           curPtLoad = os->ptLoad;
         }
         for (InputSection *isec : isd->sections) {
-          if (g() < (1 << 28))
+          // Probability of inserting padding is 1 in 16.
+          if (g() % 16 == 0)
             tmp.push_back(
-                make<ShufflePaddingSection>(ctx, isec->addralign, os));
+                make<RandomizePaddingSection>(ctx, isec->addralign, os));
           tmp.push_back(isec);
         }
         isd->sections = std::move(tmp);
@@ -1502,8 +1503,8 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
   if (ctx.arg.emachine == EM_HEXAGON)
     hexagonTLSSymbolUpdate(ctx);
 
-  if (ctx.arg.shufflePadding)
-    shufflePadding(ctx);
+  if (ctx.arg.randomizeSectionPadding)
+    randomizeSectionPadding(ctx);
 
   uint32_t pass = 0, assignPasses = 0;
   for (;;) {
diff --git a/lld/docs/ld.lld.1 b/lld/docs/ld.lld.1
index 5d3d4164622166..403da7c01d5e02 100644
--- a/lld/docs/ld.lld.1
+++ b/lld/docs/ld.lld.1
@@ -529,6 +529,15 @@ and
 .It Fl -pop-state
 Restore the states saved by
 .Fl -push-state.
+.It Fl -randomize-section-padding Ns = Ns Ar seed
+Randomly insert padding between input sections using the given seed.
+Padding is inserted into output sections with names matching the following patterns:
+.Cm .bss ,
+.Cm .data ,
+.Cm .data.rel.ro ,
+.Cm .rodata
+and
+.Cm .text* .
 .It Fl --relax-gp
 Enable global pointer relaxation for RISC-V.
 .It Fl -relocatable , Fl r
@@ -577,15 +586,6 @@ were concatenated in the order they appeared on the command line.
 Set address of section.
 .It Fl -shared , Fl -Bsharable
 Build a shared object.
-.It Fl -shuffle-padding Ns = Ns Ar seed
-Randomly insert padding between input sections using the given seed.
-Padding is inserted into output sections with names matching the following patterns:
-.Cm .bss ,
-.Cm .data ,
-.Cm .data.rel.ro ,
-.Cm .rodata
-and
-.Cm .text* .
 .It Fl -shuffle-sections Ns = Ns Ar seed
 Shuffle matched sections using the given seed before mapping them to the output sections.
 If -1, reverse the section order. If 0, use a random seed.
diff --git a/lld/test/ELF/randomize-section-padding.s b/lld/test/ELF/randomize-section-padding.s
new file mode 100644
index 00000000000000..6d56ad5129b19e
--- /dev/null
+++ b/lld/test/ELF/randomize-section-padding.s
@@ -0,0 +1,88 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+
+## --randomize-section-padding= inserts segment offset padding and pre-section
+## padding, and does not affect flags. Segment offset padding is only inserted
+## when PT_LOAD changes, as shown by .bss size (.data and .bss share a PT_LOAD).
+
+# RUN: ld.lld --randomize-section-padding=6 %t.o -o %t.out
+# RUN: llvm-readelf -sS -x .rodata -x .text -x .data %t.out | FileCheck --check-prefix=PAD6 %s
+
+# PAD6: .rodata           PROGBITS        0000000000200158 000158 000b8d 00   A  0   0  1
+# PAD6: .text             PROGBITS        0000000000201ce8 000ce8 000270 00  AX  0   0  4
+# PAD6: .data             PROGBITS        0000000000202f58 000f58 000941 00  WA  0   0  1
+# PAD6: .bss              NOBITS          0000000000203899 001899 000003 00  WA  0   0  1
+
+# PAD6: 0000000000203899     0 NOTYPE  LOCAL  DEFAULT     4 a
+# PAD6: 000000000020389a     0 NOTYPE  LOCAL  DEFAULT     4 b
+# PAD6: 000000000020389b     0 NOTYPE  LOCAL  DEFAULT     4 c
+
+# PAD6: Hex dump of section '.rodata':
+# PAD6: 0x00200cd8 00000000 00000000 00000102 03
+# PAD6: Hex dump of section '.text':
+# PAD6: 0x00201f48 cccccccc cccccccc cccccccc 0405cc06
+# PAD6: Hex dump of section '.data':
+# PAD6: 0x00203888 00000000 00000000 00000000 00000708
+# PAD6: 0x00203898 09
+
+## Size of segment offset padding and location of pre-section padding is
+## dependent on the seed.
+
+# RUN: ld.lld --randomize-section-padding=46 %t.o -o %t.out
+# RUN: llvm-readelf -sS -x .rodata -x .text -x .data %t.out | FileCheck --check-prefix=PAD46 %s
+
+# PAD46: .rodata           PROGBITS        0000000000200158 000158 000cc0 00   A  0   0  1
+# PAD46: .text             PROGBITS        0000000000201e18 000e18 0009bf 00  AX  0   0  4
+# PAD46: .data             PROGBITS        00000000002037d7 0017d7 000540 00  WA  0   0  1
+# PAD46: .bss              NOBITS          0000000000203d17 001d17 000004 00  WA  0   0  1
+
+# PAD46: 0000000000203d17     0 NOTYPE  LOCAL  DEFAULT     4 a
+# PAD46: 0000000000203d18     0 NOTYPE  LOCAL  DEFAULT     4 b
+# PAD46: 0000000000203d1a     0 NOTYPE  LOCAL  DEFAULT     4 c
+
+# PAD46: Hex dump of section '.rodata':
+# PAD46: 0x00200e08 00000000 00000000 00000000 00010203
+# PAD46: Hex dump of section '.text':
+# PAD46: 0x002027c8 cccccccc cccccccc cccccccc 040506
+# PAD46: Hex dump of section '.data':
+# PAD46: 0x00203d07 00000000 00000000 00000000 07000809
+
+.section .rodata.a,"a", at progbits
+.byte 1
+
+.section .rodata.b,"a", at progbits
+.byte 2
+
+.section .rodata.c,"a", at progbits
+.byte 3
+
+.section .text.a,"ax", at progbits
+.byte 4
+
+.section .text.b,"ax", at progbits
+.byte 5
+
+.section .text.c,"ax", at progbits
+.byte 6
+
+.section .data.a,"aw", at progbits
+.byte 7
+
+.section .data.b,"aw", at progbits
+.byte 8
+
+.section .data.c,"aw", at progbits
+.byte 9
+
+.section .bss.a,"a", at nobits
+a:
+.zero 1
+
+.section .bss.b,"a", at nobits
+b:
+.zero 1
+
+.section .bss.c,"a", at nobits
+c:
+.zero 1
+
diff --git a/lld/test/ELF/shuffle-padding-bss.s b/lld/test/ELF/shuffle-padding-bss.s
deleted file mode 100644
index f56916785fb791..00000000000000
--- a/lld/test/ELF/shuffle-padding-bss.s
+++ /dev/null
@@ -1,23 +0,0 @@
-# REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
-
-## --shuffle-padding= inserts segment offset padding and pre-section padding,
-## and does not affect .bss flags.
-# RUN: ld.lld --shuffle-padding=1 %t.o -o %t.out
-# RUN: llvm-readelf -sS %t.out | FileCheck --check-prefix=HEADER %s
-# HEADER: .bss              NOBITS          0000000000202580 000580 000f90 00  WA  0   0  1
-# HEADER: 1: 000000000020350c     0 NOTYPE  LOCAL  DEFAULT     2 a
-# HEADER: 2: 000000000020350e     0 NOTYPE  LOCAL  DEFAULT     2 b
-# HEADER: 3: 000000000020350f     0 NOTYPE  LOCAL  DEFAULT     2 c
-
-.section .bss.a,"a", at nobits
-a:
-.zero 1
-
-.section .bss.b,"a", at nobits
-b:
-.zero 1
-
-.section .bss.c,"a", at nobits
-c:
-.zero 1
diff --git a/lld/test/ELF/shuffle-padding-data.s b/lld/test/ELF/shuffle-padding-data.s
deleted file mode 100644
index 431c79214dd791..00000000000000
--- a/lld/test/ELF/shuffle-padding-data.s
+++ /dev/null
@@ -1,36 +0,0 @@
-# REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
-
-# RUN: ld.lld %t.o -o %t.out
-# RUN: llvm-readelf -x .data %t.out | FileCheck %s
-# CHECK: Hex dump of section '.data':
-# CHECK-NEXT: 0x00202158 010203
-
-## --shuffle-padding= inserts segment offset padding and pre-section padding.
-# RUN: ld.lld --shuffle-padding=1 %t.o -o %t.out
-# RUN: llvm-readelf -x .data %t.out | FileCheck --check-prefix=PAD1 %s
-# PAD1: Hex dump of section '.data':
-# PAD1: 0x00203500 00000000 00000000 00000000 01000203
-
-## --shuffle-padding=  does not affect .rodata flags.
-# RUN: llvm-readelf -S %t.out | FileCheck --check-prefix=HEADER %s
-# HEADER: .data             PROGBITS        0000000000202580 000580 000f90 00  WA  0   0  1
-
-## Size of segment offset padding and location of pre-section padding is
-## dependent on the seed.
-# RUN: ld.lld --shuffle-padding=2 %t.o -o %t.out
-# RUN: llvm-readelf -x .data %t.out | FileCheck --check-prefix=PAD2 %s
-# PAD2: Hex dump of section '.data':
-# PAD2: 0x002037e0 00000000 00000000 00000000 00010203
-
-.section .data.a,"aw", at progbits
-a:
-.byte 1
-
-.section .data.b,"aw", at progbits
-b:
-.byte 2
-
-.section .data.c,"aw", at progbits
-c:
-.byte 3
diff --git a/lld/test/ELF/shuffle-padding-rodata.s b/lld/test/ELF/shuffle-padding-rodata.s
deleted file mode 100644
index 35f18fcc12c10c..00000000000000
--- a/lld/test/ELF/shuffle-padding-rodata.s
+++ /dev/null
@@ -1,36 +0,0 @@
-# REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
-
-# RUN: ld.lld %t.o -o %t.out
-# RUN: llvm-readelf -x .rodata %t.out | FileCheck %s
-# CHECK: Hex dump of section '.rodata':
-# CHECK-NEXT: 0x00200120 010203
-
-## --shuffle-padding= inserts segment offset padding and pre-section padding.
-# RUN: ld.lld --shuffle-padding=1 %t.o -o %t.out
-# RUN: llvm-readelf -x .rodata %t.out | FileCheck --check-prefix=PAD1 %s
-# PAD1: Hex dump of section '.rodata':
-# PAD1: 0x00200540 00000000 00010203
-
-## --shuffle-padding=  does not affect .rodata flags.
-# RUN: llvm-readelf -S %t.out | FileCheck --check-prefix=HEADER %s
-# HEADER: .rodata           PROGBITS        0000000000200120 000120 000428 00   A  0   0  1
-
-## Size of segment offset padding and location of pre-section padding is
-## dependent on the seed.
-# RUN: ld.lld --shuffle-padding=2 %t.o -o %t.out
-# RUN: llvm-readelf -x .rodata %t.out | FileCheck --check-prefix=PAD2 %s
-# PAD2: Hex dump of section '.rodata':
-# PAD2: 0x00200dc0 00000000 00000000 01000203
-
-.section .rodata.a,"a", at progbits
-a:
-.byte 1
-
-.section .rodata.b,"a", at progbits
-b:
-.byte 2
-
-.section .rodata.c,"a", at progbits
-c:
-.byte 3
diff --git a/lld/test/ELF/shuffle-padding-text.s b/lld/test/ELF/shuffle-padding-text.s
deleted file mode 100644
index 5f9cafe49fa289..00000000000000
--- a/lld/test/ELF/shuffle-padding-text.s
+++ /dev/null
@@ -1,26 +0,0 @@
-# REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
-
-# RUN: ld.lld %t.o -o %t.out
-# RUN: llvm-readelf -x .text %t.out | FileCheck %s
-# CHECK: Hex dump of section '.text':
-# CHECK-NEXT: 0x00201120 010203
-
-## --shuffle-padding= inserts segment offset padding and pre-section padding.
-# RUN: ld.lld --shuffle-padding=1 %t.o -o %t.out
-# RUN: llvm-readelf -x .text %t.out | FileCheck --check-prefix=PAD1 %s
-# PAD1: Hex dump of section '.text':
-# PAD1: 0x00201540 cccccccc cccccccc 0102cc03
-
-## --shuffle-padding=  does not affect .text flags.
-# RUN: llvm-readelf -S %t.out | FileCheck --check-prefix=HEADER %s
-# HEADER: .text             PROGBITS        0000000000201120 000120 00042c 00  AX  0   0  4
-
-.section .text.a,"ax"
-.byte 1
-
-.section .text.b,"ax"
-.byte 2
-
-.section .text.c,"ax"
-.byte 3

>From a02fa3704e2172502bcc63a702fb8f647cab74a6 Mon Sep 17 00:00:00 2001
From: Peter Collingbourne <peter at pcc.me.uk>
Date: Thu, 5 Dec 2024 15:00:13 -0800
Subject: [PATCH 5/6] Add linker script test

Created using spr 1.3.6-beta.1
---
 ...dding.s => randomize-section-padding.test} | 47 +++++++++++++++++--
 1 file changed, 42 insertions(+), 5 deletions(-)
 rename lld/test/ELF/{randomize-section-padding.s => randomize-section-padding.test} (55%)

diff --git a/lld/test/ELF/randomize-section-padding.s b/lld/test/ELF/randomize-section-padding.test
similarity index 55%
rename from lld/test/ELF/randomize-section-padding.s
rename to lld/test/ELF/randomize-section-padding.test
index 6d56ad5129b19e..b44ddf15ce2fff 100644
--- a/lld/test/ELF/randomize-section-padding.s
+++ b/lld/test/ELF/randomize-section-padding.test
@@ -1,12 +1,13 @@
 # REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+# RUN: split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/a.s -o %t/a.o
 
 ## --randomize-section-padding= inserts segment offset padding and pre-section
 ## padding, and does not affect flags. Segment offset padding is only inserted
 ## when PT_LOAD changes, as shown by .bss size (.data and .bss share a PT_LOAD).
 
-# RUN: ld.lld --randomize-section-padding=6 %t.o -o %t.out
-# RUN: llvm-readelf -sS -x .rodata -x .text -x .data %t.out | FileCheck --check-prefix=PAD6 %s
+# RUN: ld.lld --randomize-section-padding=6 %t/a.o -o %t/a.out
+# RUN: llvm-readelf -sS -x .rodata -x .text -x .data %t/a.out | FileCheck --check-prefix=PAD6 %s
 
 # PAD6: .rodata           PROGBITS        0000000000200158 000158 000b8d 00   A  0   0  1
 # PAD6: .text             PROGBITS        0000000000201ce8 000ce8 000270 00  AX  0   0  4
@@ -28,8 +29,8 @@
 ## Size of segment offset padding and location of pre-section padding is
 ## dependent on the seed.
 
-# RUN: ld.lld --randomize-section-padding=46 %t.o -o %t.out
-# RUN: llvm-readelf -sS -x .rodata -x .text -x .data %t.out | FileCheck --check-prefix=PAD46 %s
+# RUN: ld.lld --randomize-section-padding=46 %t/a.o -o %t/a.out
+# RUN: llvm-readelf -sS -x .rodata -x .text -x .data %t/a.out | FileCheck --check-prefix=PAD46 %s
 
 # PAD46: .rodata           PROGBITS        0000000000200158 000158 000cc0 00   A  0   0  1
 # PAD46: .text             PROGBITS        0000000000201e18 000e18 0009bf 00  AX  0   0  4
@@ -47,6 +48,31 @@
 # PAD46: Hex dump of section '.data':
 # PAD46: 0x00203d07 00000000 00000000 00000000 07000809
 
+## When there are multiple InputSectionDescriptions for an output section,
+## segment offset padding is inserted in the first InputSectionDescription.
+
+# RUN: ld.lld --randomize-section-padding=46 %t/a.o %t/a.lds -o %t/a.out
+
+# RUN: llvm-readelf -sS -x .rodata -x .text -x .data %t/a.out | FileCheck --check-prefix=PAD46-LDS %s
+
+# PAD46-LDS: .rodata           PROGBITS        0000000000000158 000158 000cc0 00   A  0   0  1
+# PAD46-LDS: .text             PROGBITS        0000000000001000 001000 0009c0 00  AX  0   0  4
+# PAD46-LDS: .data             PROGBITS        0000000000002000 002000 000540 00  WA  0   0  1
+# PAD46-LDS: .bss              NOBITS          0000000000002540 002540 000004 00  WA  0   0  1
+
+# PAD46-LDS: 0000000000002543     0 NOTYPE  LOCAL  DEFAULT     4 a
+# PAD46-LDS: 0000000000002541     0 NOTYPE  LOCAL  DEFAULT     4 b
+# PAD46-LDS: 0000000000002540     0 NOTYPE  LOCAL  DEFAULT     4 c
+
+# PAD46-LDS: Hex dump of section '.rodata':
+# PAD46-LDS: 0x00000e08 00000000 00000000 00000000 00030201 ................
+# PAD46-LDS: Hex dump of section '.text':
+# PAD46-LDS: 0x000019b0 cccccccc cccccccc cccc0605 04cccccc ................
+# PAD46-LDS: Hex dump of section '.data':
+# PAD46-LDS: 0x00002530 00000000 00000000 00000000 09000807 ................
+
+#--- a.s
+
 .section .rodata.a,"a", at progbits
 .byte 1
 
@@ -86,3 +112,14 @@ b:
 c:
 .zero 1
 
+#--- a.lds
+
+SECTIONS {
+  . = SIZEOF_HEADERS;
+  .rodata : { *(.rodata.c) *(.rodata.b) *(.rodata.a) }
+  . = ALIGN(CONSTANT(MAXPAGESIZE));
+  .text : { *(.text.c) *(.text.b) *(.text.a) }
+  . = ALIGN(CONSTANT(MAXPAGESIZE));
+  .data : { *(.data.c) *(.data.b) *(.data.a) }
+  .bss : { *(.bss.c) *(.bss.b) *(.bss.a) }
+}

>From cd4829720d5ea128ffebe6641497b2546f8aa16f Mon Sep 17 00:00:00 2001
From: Peter Collingbourne <peter at pcc.me.uk>
Date: Wed, 11 Dec 2024 18:27:55 -0800
Subject: [PATCH 6/6] Address review comments

Created using spr 1.3.6-beta.1
---
 lld/ELF/Options.td                          |  2 +-
 lld/ELF/Writer.cpp                          |  3 ++-
 lld/docs/ld.lld.1                           |  2 +-
 lld/test/ELF/randomize-section-padding.test | 24 ++++++++++-----------
 4 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td
index 42f9a6533541ba..c31875305952fb 100644
--- a/lld/ELF/Options.td
+++ b/lld/ELF/Options.td
@@ -435,7 +435,7 @@ defm section_start: Eq<"section-start", "Set address of section">,
 def shared: F<"shared">, HelpText<"Build a shared object">;
 
 def randomize_section_padding: JJ<"randomize-section-padding=">,
-  HelpText<"Randomly insert padding between input sections using given seed">;
+  HelpText<"Randomly insert padding between input sections and at the start of each segment using given seed">;
 
 defm soname: Eq<"soname", "Set DT_SONAME">;
 
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 2cb04e6f290116..d7d00e6e58aa85 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1446,7 +1446,8 @@ 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" ||
+  return s == ".bss" || s == ".data" || s == ".data.rel.ro" || s == ".lbss" ||
+         s == ".ldata" || s == ".lrodata" || s == ".ltext" || s == ".rodata" ||
          s.starts_with(".text");
 }
 
diff --git a/lld/docs/ld.lld.1 b/lld/docs/ld.lld.1
index 403da7c01d5e02..32d7d928ff94ec 100644
--- a/lld/docs/ld.lld.1
+++ b/lld/docs/ld.lld.1
@@ -530,7 +530,7 @@ and
 Restore the states saved by
 .Fl -push-state.
 .It Fl -randomize-section-padding Ns = Ns Ar seed
-Randomly insert padding between input sections using the given seed.
+Randomly insert padding between input sections and at the start of each segment using the given seed.
 Padding is inserted into output sections with names matching the following patterns:
 .Cm .bss ,
 .Cm .data ,
diff --git a/lld/test/ELF/randomize-section-padding.test b/lld/test/ELF/randomize-section-padding.test
index b44ddf15ce2fff..af8e4f14981cd1 100644
--- a/lld/test/ELF/randomize-section-padding.test
+++ b/lld/test/ELF/randomize-section-padding.test
@@ -9,10 +9,10 @@
 # RUN: ld.lld --randomize-section-padding=6 %t/a.o -o %t/a.out
 # RUN: llvm-readelf -sS -x .rodata -x .text -x .data %t/a.out | FileCheck --check-prefix=PAD6 %s
 
-# PAD6: .rodata           PROGBITS        0000000000200158 000158 000b8d 00   A  0   0  1
-# PAD6: .text             PROGBITS        0000000000201ce8 000ce8 000270 00  AX  0   0  4
-# PAD6: .data             PROGBITS        0000000000202f58 000f58 000941 00  WA  0   0  1
-# PAD6: .bss              NOBITS          0000000000203899 001899 000003 00  WA  0   0  1
+# PAD6:      .rodata           PROGBITS        0000000000200158 000158 000b8d 00   A  0   0  1
+# PAD6-NEXT: .text             PROGBITS        0000000000201ce8 000ce8 000270 00  AX  0   0  4
+# PAD6-NEXT: .data             PROGBITS        0000000000202f58 000f58 000941 00  WA  0   0  1
+# PAD6-NEXT: .bss              NOBITS          0000000000203899 001899 000003 00  WA  0   0  1
 
 # PAD6: 0000000000203899     0 NOTYPE  LOCAL  DEFAULT     4 a
 # PAD6: 000000000020389a     0 NOTYPE  LOCAL  DEFAULT     4 b
@@ -32,10 +32,10 @@
 # RUN: ld.lld --randomize-section-padding=46 %t/a.o -o %t/a.out
 # RUN: llvm-readelf -sS -x .rodata -x .text -x .data %t/a.out | FileCheck --check-prefix=PAD46 %s
 
-# PAD46: .rodata           PROGBITS        0000000000200158 000158 000cc0 00   A  0   0  1
-# PAD46: .text             PROGBITS        0000000000201e18 000e18 0009bf 00  AX  0   0  4
-# PAD46: .data             PROGBITS        00000000002037d7 0017d7 000540 00  WA  0   0  1
-# PAD46: .bss              NOBITS          0000000000203d17 001d17 000004 00  WA  0   0  1
+# PAD46:      .rodata           PROGBITS        0000000000200158 000158 000cc0 00   A  0   0  1
+# PAD46-NEXT: .text             PROGBITS        0000000000201e18 000e18 0009bf 00  AX  0   0  4
+# PAD46-NEXT: .data             PROGBITS        00000000002037d7 0017d7 000540 00  WA  0   0  1
+# PAD46-NEXT: .bss              NOBITS          0000000000203d17 001d17 000004 00  WA  0   0  1
 
 # PAD46: 0000000000203d17     0 NOTYPE  LOCAL  DEFAULT     4 a
 # PAD46: 0000000000203d18     0 NOTYPE  LOCAL  DEFAULT     4 b
@@ -55,10 +55,10 @@
 
 # RUN: llvm-readelf -sS -x .rodata -x .text -x .data %t/a.out | FileCheck --check-prefix=PAD46-LDS %s
 
-# PAD46-LDS: .rodata           PROGBITS        0000000000000158 000158 000cc0 00   A  0   0  1
-# PAD46-LDS: .text             PROGBITS        0000000000001000 001000 0009c0 00  AX  0   0  4
-# PAD46-LDS: .data             PROGBITS        0000000000002000 002000 000540 00  WA  0   0  1
-# PAD46-LDS: .bss              NOBITS          0000000000002540 002540 000004 00  WA  0   0  1
+# PAD46-LDS:      .rodata           PROGBITS        0000000000000158 000158 000cc0 00   A  0   0  1
+# PAD46-LDS-NEXT: .text             PROGBITS        0000000000001000 001000 0009c0 00  AX  0   0  4
+# PAD46-LDS-NEXT: .data             PROGBITS        0000000000002000 002000 000540 00  WA  0   0  1
+# PAD46-LDS-NEXT: .bss              NOBITS          0000000000002540 002540 000004 00  WA  0   0  1
 
 # PAD46-LDS: 0000000000002543     0 NOTYPE  LOCAL  DEFAULT     4 a
 # PAD46-LDS: 0000000000002541     0 NOTYPE  LOCAL  DEFAULT     4 b



More information about the llvm-commits mailing list