Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -717,6 +717,16 @@ if (!(Sec->Flags & SHF_ALLOC)) return Rank | RF_NOT_ALLOC; + // Place .dynsym and .dynstr at the beginning of SHF_ALLOC + // sections. We want to do this to mitigate the possibility that + // huge .dynsym and .dynstr sections placed between text sections + // cause relocation overflow. Note: .dynstr has SHT_STRTAB type and + // SHF_ALLOC attribute, whereas sections that only have SHT_STRTAB + // but without SHF_ALLOC is placed at the end. All "Sec" reaching + // here has SHF_ALLOC bit set. + if (Sec->Type == SHT_DYNSYM || Sec->Type == SHT_STRTAB) + return Rank; + // Sort sections based on their access permission in the following // order: R, RX, RWX, RW. This order is based on the following // considerations: Index: test/ELF/aarch64-copy.s =================================================================== --- test/ELF/aarch64-copy.s +++ test/ELF/aarch64-copy.s @@ -90,4 +90,4 @@ // RODATA: Contents of section .rodata: // S(z) = 0x40014 -// RODATA-NEXT: 101c8 14000400 +// RODATA-NEXT: 10228 14000400 Index: test/ELF/arm-copy.s =================================================================== --- test/ELF/arm-copy.s +++ test/ELF/arm-copy.s @@ -78,4 +78,4 @@ // RODATA: Contents of section .rodata: // S(z) = 0x13004 -// RODATA-NEXT: 10114 04300100 +// RODATA-NEXT: 10144 04300100 Index: test/ELF/dynsec-at-beginning.s =================================================================== --- /dev/null +++ test/ELF/dynsec-at-beginning.s @@ -0,0 +1,16 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t + +# RUN: ld.lld --hash-style=gnu -o %t1 %t -shared +# RUN: llvm-readobj -elf-output-style=GNU -s %t1 | FileCheck %s + +# Dynamic symbol and dynamic strtab sections are at the beginning of +# SHF_ALLOC sections. +# CHECK: .dynsym {{.*}} A +# CHECK-NEXT: .dynstr {{.*}} A +# CHECK-NEXT: .hash {{.*}} A +# CHECK-NEXT: .text {{.*}} AX +# CHECK-NEXT: foo {{.*}} WA + +.section foo, "aw" +.byte 0