Skip to content

[lld/ELF][x86-64] Place large executable sections at the edges of binary #70358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 16 additions & 7 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,15 +653,17 @@ enum RankFlags {
RF_NOT_ADDR_SET = 1 << 27,
RF_NOT_ALLOC = 1 << 26,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This ranking mechanism is a bit difficult to understand, but changing it is out of scope of this change.

RF_PARTITION = 1 << 18, // Partition number (8 bits)
RF_LARGE_EXEC_WRITE = 1 << 16,
RF_LARGE_ALT = 1 << 15,
RF_WRITE = 1 << 14,
RF_EXEC_WRITE = 1 << 13,
RF_EXEC = 1 << 12,
RF_RODATA = 1 << 11,
RF_LARGE = 1 << 10,
RF_NOT_RELRO = 1 << 9,
RF_NOT_TLS = 1 << 8,
RF_BSS = 1 << 7,
RF_LARGE_EXEC = 1 << 10,
RF_LARGE = 1 << 9,
RF_NOT_RELRO = 1 << 8,
RF_NOT_TLS = 1 << 7,
RF_BSS = 1 << 6,
};

unsigned elf::getSectionRank(Ctx &ctx, OutputSection &osec) {
Expand Down Expand Up @@ -691,14 +693,15 @@ unsigned elf::getSectionRank(Ctx &ctx, OutputSection &osec) {
// places.
bool isExec = osec.flags & SHF_EXECINSTR;
bool isWrite = osec.flags & SHF_WRITE;
bool isLarge = osec.flags & SHF_X86_64_LARGE && ctx.arg.emachine == EM_X86_64;

if (!isWrite && !isExec) {
// Among PROGBITS sections, place .lrodata further from .text.
// For -z lrodata-after-bss, place .lrodata after .lbss like GNU ld. This
// layout has one extra PT_LOAD, but alleviates relocation overflow
// pressure for absolute relocations referencing small data from -fno-pic
// relocatable files.
if (osec.flags & SHF_X86_64_LARGE && ctx.arg.emachine == EM_X86_64)
if (isLarge)
rank |= ctx.arg.zLrodataAfterBss ? RF_LARGE_ALT : 0;
else
rank |= ctx.arg.zLrodataAfterBss ? 0 : RF_LARGE;
Expand All @@ -722,7 +725,13 @@ unsigned elf::getSectionRank(Ctx &ctx, OutputSection &osec) {
else
rank |= RF_RODATA;
} else if (isExec) {
rank |= isWrite ? RF_EXEC_WRITE : RF_EXEC;
// Place readonly .ltext before .lrodata and writable .ltext after .lbss to
// keep writable and readonly segments separate.
if (isLarge) {
rank |= isWrite ? RF_LARGE_EXEC_WRITE : RF_LARGE_EXEC;
} else {
rank |= isWrite ? RF_EXEC_WRITE : RF_EXEC;
}
} else {
rank |= RF_WRITE;
// The TLS initialization block needs to be a single contiguous block. Place
Expand All @@ -737,7 +746,7 @@ unsigned elf::getSectionRank(Ctx &ctx, OutputSection &osec) {
// alleviates relocation overflow pressure.
// For -z lrodata-after-bss, place .lbss/.lrodata/.ldata after .bss.
// .bss/.lbss being adjacent reuses the NOBITS size optimization.
if (osec.flags & SHF_X86_64_LARGE && ctx.arg.emachine == EM_X86_64) {
if (isLarge) {
rank |= ctx.arg.zLrodataAfterBss
? (osec.type == SHT_NOBITS ? 1 : RF_LARGE_ALT)
: RF_LARGE;
Expand Down
41 changes: 41 additions & 0 deletions lld/test/ELF/x86-64-section-layout.s
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
# RUN: ld.lld --section-start=.note=0x200300 a.o -z lrodata-after-bss -o a3
# RUN: llvm-readelf -S -l -sX a3 | FileCheck %s --check-prefix=CHECK3

# RUN: llvm-mc -filetype=obj -triple=x86_64 c.s -o c.o
# RUN: ld.lld c.o -o c
# RUN: llvm-readelf -S -l c | FileCheck %s --check-prefix=CHECK4

# CHECK: Name Type Address Off Size ES Flg Lk Inf Al
# CHECK-NEXT: NULL 0000000000000000 000000 000000 00 0 0 0
# CHECK-NEXT: .note NOTE 0000000000200300 000300 000001 00 A 0 0 1
Expand Down Expand Up @@ -116,6 +120,18 @@
# CHECK3-NEXT: 0000000000203307 0 NOTYPE GLOBAL DEFAULT [[#]] (.data) _edata
# CHECK3-NEXT: 0000000000207d0d 0 NOTYPE GLOBAL DEFAULT [[#]] (.ldata2) _end

# CHECK4: .note NOTE
# CHECK4-NEXT: .ltext PROGBITS
# CHECK4-NEXT: .lrodata PROGBITS
# CHECK4-NEXT: .rodata PROGBITS
# CHECK4-NEXT: .text PROGBITS
# CHECK4-NEXT: .data PROGBITS
# CHECK4-NEXT: .bss NOBITS
# CHECK4-NEXT: .ldata PROGBITS
# CHECK4-NEXT: .lbss NOBITS
# CHECK4-NEXT: .ltext_w PROGBITS
# CHECK4-NEXT: .comment PROGBITS

#--- a.s
.globl _start, _etext, _edata, _end
_start:
Expand Down Expand Up @@ -155,3 +171,28 @@ SECTIONS {
.ldata2 : {}
.lbss : { *(.lbss .lbss.*) }
}

#--- c.s
## Test .ltext layout
.section .ltext,"axl",@progbits
.globl f
f:
ret

.section .ltext_w,"awxl",@progbits
.globl g
g:
ret

.section .text,"ax",@progbits
.globl h
h:
ret

.section .note,"a",@note; .space 1
.section .rodata,"a",@progbits; .space 1
.section .data,"aw",@progbits; .space 1
.section .bss,"aw",@nobits; .space 1
.section .lrodata,"al"; .space 1
.section .ldata,"awl"; .space 1
.section .lbss,"awl",@nobits; .space 1
Loading