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 "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 + // ALLOC attribute, whereas sections that only have SHT_STRTAB but + // without ALLOC is placed at the end. All "Sec" reaching here has + // "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