Index: lld/ELF/SyntheticSections.cpp =================================================================== --- lld/ELF/SyntheticSections.cpp +++ lld/ELF/SyntheticSections.cpp @@ -1764,28 +1764,34 @@ for (size_t I = 0, E = Offsets.size(); I < E;) { // Add a leading relocation. RelrRelocs.push_back(Elf_Relr(Offsets[I])); + uint64_t Base = Offsets[I] + Wordsize; ++I; - // Find foldable relocations to create a bitmap. - uint64_t Bitmap = 0; - for (size_t J = I; J < E; ++J) { - uint64_t Delta = Offsets[J] - Offsets[I]; + // Find foldable relocations to construct bitmaps. + while (I < E) { + uint64_t Bitmap = 0; - // If it is too far, it cannot be folded. - if (Delta >= NBits * Wordsize) - break; + while (I < E) { + uint64_t Delta = Offsets[I] - Base; - // If it is not a multiple of wordsize away, it cannot be folded. - if (Delta % Wordsize) - break; + // If it is too far, it cannot be folded. + if (Delta >= NBits * Wordsize) + break; - // Fold it. - Bitmap |= 1ULL << (Delta / Wordsize); - } + // If it is not a multiple of wordsize away, it cannot be folded. + if (Delta % Wordsize) + break; + + // Fold it. + Bitmap |= 1ULL << (Delta / Wordsize); + ++I; + } + + if (!Bitmap) + break; - if (Bitmap) { RelrRelocs.push_back(Elf_Relr((Bitmap << 1) | 1)); - I += NBits; + Base += NBits * Wordsize; } } Index: lld/test/ELF/pack-dyn-relocs2.s =================================================================== --- /dev/null +++ lld/test/ELF/pack-dyn-relocs2.s @@ -0,0 +1,85 @@ +// REQUIRES: arm, aarch64 + +// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %p/Inputs/arm-shared.s -o %t.so.o +// RUN: ld.lld -shared %t.so.o -o %t.so + +// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o +// RUN: ld.lld -pie --pack-dyn-relocs=relr %t.o %t.so -o %t.exe +// RUN: llvm-readobj -relocations %t.exe | FileCheck %s + +// CHECK: Section (5) .relr.dyn { +// CHECK-NEXT: 0x1000 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1004 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1008 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x100C R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1010 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1014 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1018 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x101C R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1020 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1024 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1028 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x102C R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1030 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1034 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1038 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x103C R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1040 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1044 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1048 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x104C R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1050 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1054 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1058 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x105C R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1060 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1064 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1068 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x106C R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1070 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1074 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1078 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x107C R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1080 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: 0x1084 R_ARM_RELATIVE - 0x0 +// CHECK-NEXT: } + +// RUN: llvm-readobj -s -dynamic-table %t.exe | FileCheck --check-prefix=HEADER %s +// HEADER: 0x00000023 RELRSZ 0xC + +.data +.align 2 +.dc.a __ehdr_start + 1 +.dc.a __ehdr_start + 2 +.dc.a __ehdr_start + 3 +.dc.a __ehdr_start + 4 +.dc.a __ehdr_start + 5 +.dc.a __ehdr_start + 6 +.dc.a __ehdr_start + 7 +.dc.a __ehdr_start + 9 +.dc.a __ehdr_start + 10 +.dc.a __ehdr_start + 11 +.dc.a __ehdr_start + 12 +.dc.a __ehdr_start + 13 +.dc.a __ehdr_start + 14 +.dc.a __ehdr_start + 15 +.dc.a __ehdr_start + 16 +.dc.a __ehdr_start + 17 +.dc.a __ehdr_start + 18 +.dc.a __ehdr_start + 19 +.dc.a __ehdr_start + 20 +.dc.a __ehdr_start + 21 +.dc.a __ehdr_start + 22 +.dc.a __ehdr_start + 23 +.dc.a __ehdr_start + 24 +.dc.a __ehdr_start + 25 +.dc.a __ehdr_start + 26 +.dc.a __ehdr_start + 27 +.dc.a __ehdr_start + 28 +.dc.a __ehdr_start + 29 +.dc.a __ehdr_start + 30 +.dc.a __ehdr_start + 31 +.dc.a __ehdr_start + 32 +.dc.a __ehdr_start + 33 +.dc.a __ehdr_start + 34 +.dc.a __ehdr_start + 35