Skip to content

Commit 678959e

Browse files
committed
Address review comments
Created using spr 1.3.6-beta.1
1 parent 667e7ba commit 678959e

File tree

12 files changed

+117
-148
lines changed

12 files changed

+117
-148
lines changed

lld/ELF/Config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ struct Config {
320320
bool printGcSections;
321321
bool printIcfSections;
322322
bool printMemoryUsage;
323+
std::optional<uint64_t> randomizeSectionPadding;
323324
bool rejectMismatch;
324325
bool relax;
325326
bool relaxGP;
@@ -329,7 +330,6 @@ struct Config {
329330
bool relrPackDynRelocs = false;
330331
llvm::DenseSet<llvm::StringRef> saveTempsArgs;
331332
llvm::SmallVector<std::pair<llvm::GlobPattern, uint32_t>, 0> shuffleSections;
332-
std::optional<uint64_t> shufflePadding;
333333
bool singleRoRx;
334334
bool shared;
335335
bool symbolic;

lld/ELF/Driver.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,8 +1410,9 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
14101410
ctx.arg.searchPaths = args::getStrings(args, OPT_library_path);
14111411
ctx.arg.sectionStartMap = getSectionStartMap(ctx, args);
14121412
ctx.arg.shared = args.hasArg(OPT_shared);
1413-
if (args.hasArg(OPT_shuffle_padding))
1414-
ctx.arg.shufflePadding = args::getInteger(args, OPT_shuffle_padding, 0);
1413+
if (args.hasArg(OPT_randomize_section_padding))
1414+
ctx.arg.randomizeSectionPadding =
1415+
args::getInteger(args, OPT_randomize_section_padding, 0);
14151416
ctx.arg.singleRoRx = !args.hasFlag(OPT_rosegment, OPT_no_rosegment, true);
14161417
ctx.arg.soName = args.getLastArgValue(OPT_soname);
14171418
ctx.arg.sortSection = getSortSection(ctx, args);

lld/ELF/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ defm section_start: Eq<"section-start", "Set address of section">,
434434

435435
def shared: F<"shared">, HelpText<"Build a shared object">;
436436

437-
def shuffle_padding: JJ<"shuffle-padding=">,
437+
def randomize_section_padding: JJ<"randomize-section-padding=">,
438438
HelpText<"Randomly insert padding between input sections using given seed">;
439439

440440
defm soname: Eq<"soname", "Set DT_SONAME">;

lld/ELF/SyntheticSections.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2753,14 +2753,14 @@ RelroPaddingSection::RelroPaddingSection(Ctx &ctx)
27532753
: SyntheticSection(ctx, ".relro_padding", SHT_NOBITS, SHF_ALLOC | SHF_WRITE,
27542754
1) {}
27552755

2756-
ShufflePaddingSection::ShufflePaddingSection(Ctx &ctx, uint64_t size,
2757-
OutputSection *parent)
2758-
: SyntheticSection(ctx, ".shuffle_padding", SHF_ALLOC, SHT_PROGBITS, 1),
2756+
RandomizePaddingSection::RandomizePaddingSection(Ctx &ctx, uint64_t size,
2757+
OutputSection *parent)
2758+
: SyntheticSection(ctx, ".randomize_padding", SHT_PROGBITS, SHF_ALLOC, 1),
27592759
size(size) {
27602760
this->parent = parent;
27612761
}
27622762

2763-
void ShufflePaddingSection::writeTo(uint8_t *buf) {
2763+
void RandomizePaddingSection::writeTo(uint8_t *buf) {
27642764
std::array<uint8_t, 4> filler = getParent()->getFiller(ctx);
27652765
uint8_t *end = buf + size;
27662766
for (; buf + 4 <= end; buf += 4)

lld/ELF/SyntheticSections.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,11 +796,11 @@ class RelroPaddingSection final : public SyntheticSection {
796796
void writeTo(uint8_t *buf) override {}
797797
};
798798

799-
class ShufflePaddingSection final : public SyntheticSection {
799+
class RandomizePaddingSection final : public SyntheticSection {
800800
uint64_t size;
801801

802802
public:
803-
ShufflePaddingSection(Ctx &ctx, uint64_t size, OutputSection *parent);
803+
RandomizePaddingSection(Ctx &ctx, uint64_t size, OutputSection *parent);
804804
size_t getSize() const override { return size; }
805805
void writeTo(uint8_t *buf) override;
806806
};

lld/ELF/Writer.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,8 +1450,8 @@ static bool canInsertPadding(OutputSection *sec) {
14501450
s.starts_with(".text");
14511451
}
14521452

1453-
static void shufflePadding(Ctx &ctx) {
1454-
std::mt19937 g(*ctx.arg.shufflePadding);
1453+
static void randomizeSectionPadding(Ctx &ctx) {
1454+
std::mt19937 g(*ctx.arg.randomizeSectionPadding);
14551455
PhdrEntry *curPtLoad = nullptr;
14561456
for (OutputSection *os : ctx.outputSections) {
14571457
if (!canInsertPadding(os))
@@ -1460,14 +1460,15 @@ static void shufflePadding(Ctx &ctx) {
14601460
if (auto *isd = dyn_cast<InputSectionDescription>(bc)) {
14611461
SmallVector<InputSection *, 0> tmp;
14621462
if (os->ptLoad != curPtLoad) {
1463-
tmp.push_back(
1464-
make<ShufflePaddingSection>(ctx, g() % ctx.arg.maxPageSize, os));
1463+
tmp.push_back(make<RandomizePaddingSection>(
1464+
ctx, g() % ctx.arg.maxPageSize, os));
14651465
curPtLoad = os->ptLoad;
14661466
}
14671467
for (InputSection *isec : isd->sections) {
1468-
if (g() < (1 << 28))
1468+
// Probability of inserting padding is 1 in 16.
1469+
if (g() % 16 == 0)
14691470
tmp.push_back(
1470-
make<ShufflePaddingSection>(ctx, isec->addralign, os));
1471+
make<RandomizePaddingSection>(ctx, isec->addralign, os));
14711472
tmp.push_back(isec);
14721473
}
14731474
isd->sections = std::move(tmp);
@@ -1502,8 +1503,8 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
15021503
if (ctx.arg.emachine == EM_HEXAGON)
15031504
hexagonTLSSymbolUpdate(ctx);
15041505

1505-
if (ctx.arg.shufflePadding)
1506-
shufflePadding(ctx);
1506+
if (ctx.arg.randomizeSectionPadding)
1507+
randomizeSectionPadding(ctx);
15071508

15081509
uint32_t pass = 0, assignPasses = 0;
15091510
for (;;) {

lld/docs/ld.lld.1

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,15 @@ and
529529
.It Fl -pop-state
530530
Restore the states saved by
531531
.Fl -push-state.
532+
.It Fl -randomize-section-padding Ns = Ns Ar seed
533+
Randomly insert padding between input sections using the given seed.
534+
Padding is inserted into output sections with names matching the following patterns:
535+
.Cm .bss ,
536+
.Cm .data ,
537+
.Cm .data.rel.ro ,
538+
.Cm .rodata
539+
and
540+
.Cm .text* .
532541
.It Fl --relax-gp
533542
Enable global pointer relaxation for RISC-V.
534543
.It Fl -relocatable , Fl r
@@ -577,15 +586,6 @@ were concatenated in the order they appeared on the command line.
577586
Set address of section.
578587
.It Fl -shared , Fl -Bsharable
579588
Build a shared object.
580-
.It Fl -shuffle-padding Ns = Ns Ar seed
581-
Randomly insert padding between input sections using the given seed.
582-
Padding is inserted into output sections with names matching the following patterns:
583-
.Cm .bss ,
584-
.Cm .data ,
585-
.Cm .data.rel.ro ,
586-
.Cm .rodata
587-
and
588-
.Cm .text* .
589589
.It Fl -shuffle-sections Ns = Ns Ar seed
590590
Shuffle matched sections using the given seed before mapping them to the output sections.
591591
If -1, reverse the section order. If 0, use a random seed.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
3+
4+
## --randomize-section-padding= inserts segment offset padding and pre-section
5+
## padding, and does not affect flags. Segment offset padding is only inserted
6+
## when PT_LOAD changes, as shown by .bss size (.data and .bss share a PT_LOAD).
7+
8+
# RUN: ld.lld --randomize-section-padding=6 %t.o -o %t.out
9+
# RUN: llvm-readelf -sS -x .rodata -x .text -x .data %t.out | FileCheck --check-prefix=PAD6 %s
10+
11+
# PAD6: .rodata PROGBITS 0000000000200158 000158 000b8d 00 A 0 0 1
12+
# PAD6: .text PROGBITS 0000000000201ce8 000ce8 000270 00 AX 0 0 4
13+
# PAD6: .data PROGBITS 0000000000202f58 000f58 000941 00 WA 0 0 1
14+
# PAD6: .bss NOBITS 0000000000203899 001899 000003 00 WA 0 0 1
15+
16+
# PAD6: 0000000000203899 0 NOTYPE LOCAL DEFAULT 4 a
17+
# PAD6: 000000000020389a 0 NOTYPE LOCAL DEFAULT 4 b
18+
# PAD6: 000000000020389b 0 NOTYPE LOCAL DEFAULT 4 c
19+
20+
# PAD6: Hex dump of section '.rodata':
21+
# PAD6: 0x00200cd8 00000000 00000000 00000102 03
22+
# PAD6: Hex dump of section '.text':
23+
# PAD6: 0x00201f48 cccccccc cccccccc cccccccc 0405cc06
24+
# PAD6: Hex dump of section '.data':
25+
# PAD6: 0x00203888 00000000 00000000 00000000 00000708
26+
# PAD6: 0x00203898 09
27+
28+
## Size of segment offset padding and location of pre-section padding is
29+
## dependent on the seed.
30+
31+
# RUN: ld.lld --randomize-section-padding=46 %t.o -o %t.out
32+
# RUN: llvm-readelf -sS -x .rodata -x .text -x .data %t.out | FileCheck --check-prefix=PAD46 %s
33+
34+
# PAD46: .rodata PROGBITS 0000000000200158 000158 000cc0 00 A 0 0 1
35+
# PAD46: .text PROGBITS 0000000000201e18 000e18 0009bf 00 AX 0 0 4
36+
# PAD46: .data PROGBITS 00000000002037d7 0017d7 000540 00 WA 0 0 1
37+
# PAD46: .bss NOBITS 0000000000203d17 001d17 000004 00 WA 0 0 1
38+
39+
# PAD46: 0000000000203d17 0 NOTYPE LOCAL DEFAULT 4 a
40+
# PAD46: 0000000000203d18 0 NOTYPE LOCAL DEFAULT 4 b
41+
# PAD46: 0000000000203d1a 0 NOTYPE LOCAL DEFAULT 4 c
42+
43+
# PAD46: Hex dump of section '.rodata':
44+
# PAD46: 0x00200e08 00000000 00000000 00000000 00010203
45+
# PAD46: Hex dump of section '.text':
46+
# PAD46: 0x002027c8 cccccccc cccccccc cccccccc 040506
47+
# PAD46: Hex dump of section '.data':
48+
# PAD46: 0x00203d07 00000000 00000000 00000000 07000809
49+
50+
.section .rodata.a,"a",@progbits
51+
.byte 1
52+
53+
.section .rodata.b,"a",@progbits
54+
.byte 2
55+
56+
.section .rodata.c,"a",@progbits
57+
.byte 3
58+
59+
.section .text.a,"ax",@progbits
60+
.byte 4
61+
62+
.section .text.b,"ax",@progbits
63+
.byte 5
64+
65+
.section .text.c,"ax",@progbits
66+
.byte 6
67+
68+
.section .data.a,"aw",@progbits
69+
.byte 7
70+
71+
.section .data.b,"aw",@progbits
72+
.byte 8
73+
74+
.section .data.c,"aw",@progbits
75+
.byte 9
76+
77+
.section .bss.a,"a",@nobits
78+
a:
79+
.zero 1
80+
81+
.section .bss.b,"a",@nobits
82+
b:
83+
.zero 1
84+
85+
.section .bss.c,"a",@nobits
86+
c:
87+
.zero 1
88+

lld/test/ELF/shuffle-padding-bss.s

Lines changed: 0 additions & 23 deletions
This file was deleted.

lld/test/ELF/shuffle-padding-data.s

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)