Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lld/ELF/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ struct Config {
bool zIfuncNoplt;
bool zInitfirst;
bool zInterpose;
bool zKeepDataSectionPrefix;
bool zKeepTextSectionPrefix;
bool zLrodataAfterBss;
bool zNoBtCfi;
Expand Down
2 changes: 2 additions & 0 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,8 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
ctx.arg.zIfuncNoplt = hasZOption(args, "ifunc-noplt");
ctx.arg.zInitfirst = hasZOption(args, "initfirst");
ctx.arg.zInterpose = hasZOption(args, "interpose");
ctx.arg.zKeepDataSectionPrefix = getZFlag(
args, "keep-data-section-prefix", "nokeep-data-section-prefix", false);
ctx.arg.zKeepTextSectionPrefix = getZFlag(
args, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
ctx.arg.zLrodataAfterBss =
Expand Down
18 changes: 17 additions & 1 deletion lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,22 @@ template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
}
}

// Returns true if the section is a data section that's read only and
// relocatable per its section name.
static bool isRelRoDataSection(Ctx &ctx, StringRef SectionName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

secName

if (!secName.consume_front(".data.rel.ro")) return false; then check .hot or .unlikely

This is more efficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

// If -z keep-data-section-prefix is given, '<section>.hot' and
// '<section>.unlikely' is considered a split of '<section>' based on
// hotness. They should share the same section attributes.
if (ctx.arg.zKeepDataSectionPrefix) {
if (SectionName.ends_with(".hot"))
SectionName = SectionName.drop_back(4);
else if (SectionName.ends_with(".unlikely"))
SectionName = SectionName.drop_back(9);
}

return SectionName == ".data.rel.ro";
}

// Today's loaders have a feature to make segments read-only after
// processing dynamic relocations to enhance security. PT_GNU_RELRO
// is defined for that.
Expand Down Expand Up @@ -629,7 +645,7 @@ static bool isRelroSection(Ctx &ctx, const OutputSection *sec) {
// magic section names.
StringRef s = sec->name;

bool abiAgnostic = s == ".data.rel.ro" || s == ".bss.rel.ro" ||
bool abiAgnostic = isRelRoDataSection(ctx, s) || s == ".bss.rel.ro" ||
s == ".ctors" || s == ".dtors" || s == ".jcr" ||
s == ".eh_frame" || s == ".fini_array" ||
s == ".init_array" || s == ".preinit_array";
Expand Down
89 changes: 89 additions & 0 deletions lld/test/ELF/keep-text-section-prefix.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# REQUIRES: x86

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this x86 only? The assembly below is consumed with the x86 linux triple so it shouldn't matter?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use x86 as a placeholder for target-agnostic behavior. llvm-mc depends on llvm/lib/Target/X86, and we use REQUIRES: x86 to prevent test execution when LLVM_TARGETS_TO_BUILD skips x86.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know the reason to use 'x86' before. Thanks for the info!


# RUN: rm -rf %t && split-file %s %t && cd %t

# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux input.s -o a.o
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-triple=x86_64 a.s -o a.o

We make the source file name match the output name and often use short names like .s for convenience.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


# RUN: ld.lld -z keep-data-section-prefix -T linker-script.lds a.o -o a.out
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a.lds. You can also use x.lds if you want to avoid the letter a

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done with x.lds mainly because object file name has 'a' already.

# RUN: llvm-readelf -l a.out | FileCheck --check-prefixes=SEG,LS %s
# RUN: llvm-readelf -S a.out | FileCheck %s --check-prefix=CHECK-LS

# RUN: ld.lld -z keep-data-section-prefix a.o -o b.out
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our convention is like a.o => a, b.o => b. The output file names out out1 out2 are more common.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it. I used out1, out2, out3 in the updated test.

# RUN: llvm-readelf -l b.out | FileCheck --check-prefixes=SEG,PRE %s
# RUN: llvm-readelf -S b.out | FileCheck %s --check-prefix=CHECK-PRE

# RUN: ld.lld a.o -o c.out
# RUN: llvm-readelf -l c.out | FileCheck --check-prefixes=SEG,PRE %s
# RUN: llvm-readelf -S c.out | FileCheck %s --check-prefix=CHECK-PRE

# RUN: not ld.lld -T linker-script.lds a.o -o d.out 2>&1 | FileCheck %s
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omit -o for not ld.lld, since lld will not create the output file anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

# CHECK: error: section: .relro_padding is not contiguous with other relro sections

## The first PW PT_LOAD segment has FileSiz 0x126f (0x1000 + 0x200 + 0x60 + 0xf),
## and its p_offset p_vaddr p_paddr p_filesz should match PT_GNU_RELRO.
# Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
# SEG: LOAD 0x0001c8 0x00000000002011c8 0x00000000002011c8 0x000001 0x000001 R E 0x1000
# SEG-NEXT: LOAD 0x0001c9 0x00000000002021c9 0x00000000002021c9 0x00126f 0x001e37 RW 0x1000
# SEG-NEXT: LOAD 0x001438 0x0000000000204438 0x0000000000204438 0x000001 0x000002 RW 0x1000
# SEG-NEXT: GNU_RELRO 0x0001c9 0x00000000002021c9 0x00000000002021c9 0x00126f 0x001e37 R 0x1
# SEG-NEXT: GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x0

## Input to output mapping per linker script
## .data.rel.ro.split -> .data.rel.ro
## .data.rel.ro -> .data.rel.ro
## .data.rel.ro.hot -> .data.rel.ro.hot
## .data.rel.ro.unlikely -> .data.rel.ro.unlikely
# LS: .text
# LS-NEXT: .data.rel.ro.hot .data.rel.ro .data.rel.ro.unlikely .relro_padding
# LS-NEXT: .data .bss

# [Nr] Name Type Address Off Size
# CHECK-LS: .data.rel.ro.hot PROGBITS 00000000002021c9 0001c9 00000f
# CHECK-LS-NEXT: .data.rel.ro PROGBITS 00000000002021d8 0001d8 000260
# CHECK-LS-NEXT: .data.rel.ro.unlikely PROGBITS 0000000000202438 000438 001000
# CHECK-LS-NEXT: .relro_padding NOBITS 0000000000203438 001438 000bc8
# CHECK-LS-NEXT: .data PROGBITS 0000000000204438 001438 000001
# CHECK-LS-NEXT: .bss NOBITS 0000000000204439 001439 000001

## Linker script is not provided to map data sections.
## So all input sections with prefix .data.rel.ro will map to .data.rel.ro in the output.
# PRE: .text
# PRE-NEXT: .data.rel.ro .relro_padding
# PRE-NEXT: .data .bss

# [Nr] Name Type Address Off Size
# CHECK-PRE: .data.rel.ro PROGBITS 00000000002021c9 0001c9 00126f
# CHECK-PRE-NEXT: .relro_padding NOBITS 0000000000203438 001438 000bc8
# CHECK-PRE-NEXT: .data PROGBITS 0000000000204438 001438 000001
# CHECK-PRE-NEXT: .bss NOBITS 0000000000204439 001439 000001

#--- linker-script.lds
SECTIONS {
.data.rel.ro.hot : { *(.data.rel.ro.hot) }
.data.rel.ro : { .data.rel.ro }
.data.rel.ro.unlikely : { *(.data.rel.ro.unlikely) }
} INSERT AFTER .text


#--- input.s
.globl _start
_start:
ret

.section .data.rel.ro.hot, "aw"
.space 15

.section .data.rel.ro, "aw"
.space 96

.section .data.rel.ro.split,"aw"
.space 512

.section .data.rel.ro.unlikely, "aw"
.space 4096

.section .data, "aw"
.space 1

.section .bss, "aw"
.space 1