diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -101,9 +101,9 @@ } for (StringRef v : - {".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss", - ".gcc_except_table", ".init_array", ".fini_array", ".tbss", ".tdata", - ".ARM.exidx", ".ARM.extab", ".ctors", ".dtors"}) + {".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss", ".ldata", + ".lrodata", ".lbss", ".gcc_except_table", ".init_array", ".fini_array", + ".tbss", ".tdata", ".ARM.exidx", ".ARM.extab", ".ctors", ".dtors"}) if (isSectionPrefix(v, s->name)) return v; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -830,10 +830,11 @@ RF_NOT_ALLOC = 1 << 26, RF_PARTITION = 1 << 18, // Partition number (8 bits) RF_NOT_SPECIAL = 1 << 17, - RF_WRITE = 1 << 13, - RF_EXEC_WRITE = 1 << 12, - RF_EXEC = 1 << 11, - RF_RODATA = 1 << 10, + RF_WRITE = 1 << 16, + RF_EXEC_WRITE = 1 << 15, + RF_EXEC = 1 << 14, + RF_RODATA = 1 << 13, + RF_LARGE = 1 << 12, RF_NOT_RELRO = 1 << 9, RF_NOT_TLS = 1 << 8, RF_BSS = 1 << 7, @@ -891,6 +892,9 @@ // .dynstr and .dynsym can be away from .text. if (osec.type == SHT_PROGBITS) rank |= RF_RODATA; + // Among PROGBITS sections, place .rodata closer to .text. + if (!(osec.flags & SHF_X86_64_LARGE && config->emachine == EM_X86_64)) + rank |= RF_LARGE; } else if (isExec) { rank |= isWrite ? RF_EXEC_WRITE : RF_EXEC; } else { @@ -901,6 +905,10 @@ rank |= RF_NOT_TLS; if (!isRelroSection(&osec)) rank |= RF_NOT_RELRO; + // Place .ldata and .lbss after .bss. Making .bss closer to .text alleviates + // relocation overflow pressure. + if (osec.flags & SHF_X86_64_LARGE && config->emachine == EM_X86_64) + rank |= RF_LARGE; } // Within TLS sections, or within other RelRo sections, or within non-RelRo diff --git a/lld/test/ELF/x86-64-section-layout.s b/lld/test/ELF/x86-64-section-layout.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/x86-64-section-layout.s @@ -0,0 +1,53 @@ +# REQUIRES: x86 +## Test the placement of .lrodata, .lbss, .ldata, and their -fdata-sections variants. +## See also section-layout.s. + +# RUN: llvm-mc -filetype=obj -triple=x86_64 --defsym=BSS=1 %s -o %t.o +# RUN: ld.lld --section-start=.note=0x200300 %t.o -o %t +# RUN: llvm-readelf -S %t | FileCheck %s + +# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t1.o +# RUN: ld.lld --section-start=.note=0x200300 %t1.o -o %t1 +# RUN: llvm-readelf -S %t1 | FileCheck %s --check-prefix=CHECK1 + +# 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 +# CHECK-NEXT: .lrodata PROGBITS 0000000000200301 000301 000002 00 Al 0 0 1 +# CHECK-NEXT: .rodata PROGBITS 0000000000200303 000303 000001 00 A 0 0 1 +# CHECK-NEXT: .text PROGBITS 0000000000201304 000304 000000 00 AX 0 0 4 +# CHECK-NEXT: .tdata PROGBITS 0000000000202304 000304 000001 00 WAT 0 0 1 +# CHECK-NEXT: .tbss NOBITS 0000000000202305 000305 000002 00 WAT 0 0 1 +# CHECK-NEXT: .data PROGBITS 0000000000203305 000305 000001 00 WA 0 0 1 +# CHECK-NEXT: .bss NOBITS 0000000000203306 000306 000001 00 WA 0 0 1 +# CHECK-NEXT: .ldata PROGBITS 0000000000203307 000307 000002 00 WAl 0 0 1 +# CHECK-NEXT: .ldata2 PROGBITS 0000000000203309 000309 000001 00 WAl 0 0 1 +# CHECK-NEXT: .lbss NOBITS 000000000020330a 00030a 000002 00 WAl 0 0 1 +# CHECK-NEXT: .comment PROGBITS 0000000000000000 00030a {{.*}} 01 MS 0 0 1 + +# CHECK1: .data PROGBITS 0000000000203305 000305 000001 00 WA 0 0 1 +# CHECK1-NEXT: .ldata PROGBITS 0000000000203306 000306 000002 00 WAl 0 0 1 +# CHECK1-NEXT: .ldata2 PROGBITS 0000000000203308 000308 000001 00 WAl 0 0 1 +# CHECK1-NEXT: .comment PROGBITS 0000000000000000 000309 000013 01 MS 0 0 1 + +.section .note,"a",@note; .space 1 +.section .rodata,"a",@progbits; .space 1 +.section .data,"aw",@progbits; .space 1 +.ifdef BSS +.section .bss,"aw",@nobits; .space 1 +.endif +.section .tdata,"awT",@progbits; .space 1 +.section .tbss,"awT",@nobits; .space 1 +.section .tbss.1,"awT",@nobits; .space 1 + +.section .lrodata,"al"; .space 1 +.section .lrodata.1,"al"; .space 1 +.section .ldata,"awl"; .space 1 +## Input .ldata.rel.ro sections are placed in the output .ldata section. +.section .ldata.rel.ro,"awl"; .space 1 +.ifdef BSS +.section .lbss,"awl",@nobits; .space 1 +## Input .lbss.rel.ro sections are placed in the output .lbss section. +.section .lbss.rel.ro,"awl",@nobits; .space 1 +.endif +.section .ldata2,"awl"; .space 1