Index: lld/ELF/Writer.cpp =================================================================== --- lld/ELF/Writer.cpp +++ lld/ELF/Writer.cpp @@ -2276,7 +2276,7 @@ Default = Out::elfHeader; auto define = [=](StringRef start, StringRef end, OutputSection *os) { - if (os) { + if (os && getFirstInputSection(os)) { addOptionalRegular(start, os, 0); addOptionalRegular(end, os, -1); } else { Index: lld/test/ELF/linkerscript/preinit-array-empty.test =================================================================== --- /dev/null +++ lld/test/ELF/linkerscript/preinit-array-empty.test @@ -0,0 +1,24 @@ +# REQUIRES: x86 +## PR52534: https://bugs.llvm.org/show_bug.cgi?id=52534 +## Link should succeed without causing an out of range relocation error. + +# RUN: split-file %s %t +# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/t.s -o %t.o +# RUN: ld.lld --script %t/t.script %t.o -o %t.out --image-base=0x80000000 + +# RUN: llvm-readelf -s %t.out | FileCheck %s +# CHECK: [[#%x,ADDR:]] 0 NOTYPE LOCAL HIDDEN [[#]] __preinit_array_start +# CHECK-NEXT: [[#ADDR]] 0 NOTYPE LOCAL HIDDEN [[#]] __preinit_array_end +# CHECK-NEXT: [[#ADDR]] 0 NOTYPE GLOBAL DEFAULT [[#]] _start + +#--- t.s +.global _start +_start: + movq __preinit_array_start@GOTPCREL(%rip),%rax + movq __preinit_array_end@GOTPCREL(%rip),%rax + +#--- t.script +SECTIONS { + .text : { *(.text); } + .preinit_array : { *(.preinit_array); } +}