Index: ELF/SyntheticSections.cpp =================================================================== --- ELF/SyntheticSections.cpp +++ ELF/SyntheticSections.cpp @@ -1957,6 +1957,11 @@ } } + // Don't allow the section to shrink; otherwise the size of the section can + // oscillate infinitely. Trailing 1s do not decode to more relocations. + if (relrRelocs.size() < oldSize) + relrRelocs.resize(oldSize, Elf_Relr(1)); + return relrRelocs.size() != oldSize; } Index: test/ELF/pack-dyn-relocs-relr-loop.s =================================================================== --- /dev/null +++ test/ELF/pack-dyn-relocs-relr-loop.s @@ -0,0 +1,41 @@ +# REQUIRES: aarch64 +# RUN: llvm-mc -filetype=obj -triple=aarch64 %s -o %t.o +# RUN: ld.lld -pie --pack-dyn-relocs=relr -z max-page-size=4096 %t.o -o %t +# RUN: llvm-readobj -r %t | FileCheck --check-prefix=RELR %s +# RUN: llvm-readobj -r --raw-relr %t | FileCheck --check-prefix=RAW %s + +## This test makes sure we don't shrink .relr.dyn, +## otherwise it size may oscillate between two numbers. + +## The test is very sensitive to the exact section sizes and offsets, +## make sure .data is located at a page boundary. + +# RELR: .relr.dyn { +# RELR-NEXT: 0x2F40 R_AARCH64_RELATIVE - 0x0 +# RELR-NEXT: 0x2F48 R_AARCH64_RELATIVE - 0x0 +# RELR-NEXT: 0x3000 R_AARCH64_RELATIVE - 0x0 +# RELR-NEXT: } + +# RAW: 0x2F40 +# RAW-NEXT: 0x1000003 +## Trailing 0x1 does not decode any relocation. +# RAW-NEXT: 0x1 + +.section .data.rel.ro +.align 3 +.space 0xce0 +foo: +## Encoded by the first word of .relr.dyn +.quad foo + +## Encoded by the second word of .relr.dyn +.quad foo + +.section .data +.align 3 +bar: +## If .data is at 0x3000, the relocation will be encoded by the second word. +## If we shrink .relr.dyn, the end of .dynamic will be at 0x2ff8 and .data +## will be at 0x3ff8, this relocation will be encoded by the third word, +## which will cause .relr.dyn to expand again. +.quad bar