diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -73,6 +73,15 @@ return None; } +static std::string getDefinedLocation(const Symbol &sym) { + std::string msg = "\n>>> defined in "; + if (sym.file) + msg += toString(sym.file); + else if (Optional loc = getLinkerScriptLocation(sym)) + msg += *loc; + return msg; +} + // Construct a message in the following format. // // >>> defined in /home/alice/src/foo.o @@ -80,19 +89,30 @@ // >>> /home/alice/src/bar.o:(.text+0x1) static std::string getLocation(InputSectionBase &s, const Symbol &sym, uint64_t off) { - std::string msg = "\n>>> defined in "; - if (sym.file) - msg += toString(sym.file); - else if (Optional loc = getLinkerScriptLocation(sym)) - msg += *loc; - - msg += "\n>>> referenced by "; + std::string msg = getDefinedLocation(sym) + "\n>>> referenced by "; std::string src = s.getSrcMsg(sym, off); if (!src.empty()) msg += src + "\n>>> "; return msg + s.getObjMsg(off); } +void reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v, + int64_t min, uint64_t max) { + ErrorPlace errPlace = getErrorPlace(loc); + std::string hint; + if (rel.sym && !rel.sym->isLocal()) + hint = "; references " + lld::toString(*rel.sym) + + getDefinedLocation(*rel.sym); + + if (errPlace.isec && errPlace.isec->name.startswith(".debug")) + hint += "; consider recompiling with -fdebug-types-section to reduce size " + "of debug sections"; + + errorOrWarn(errPlace.loc + "relocation " + lld::toString(rel.type) + + " out of range: " + v.str() + " is not in [" + Twine(min).str() + + ", " + Twine(max).str() + "]" + hint); +} + namespace { // Build a bitmask with one bit set for each RelExpr. // diff --git a/lld/ELF/Target.h b/lld/ELF/Target.h --- a/lld/ELF/Target.h +++ b/lld/ELF/Target.h @@ -204,18 +204,8 @@ template bool isMipsPIC(const Defined *sym); -static inline void reportRangeError(uint8_t *loc, const Relocation &rel, - const Twine &v, int64_t min, uint64_t max) { - ErrorPlace errPlace = getErrorPlace(loc); - StringRef hint; - if (errPlace.isec && errPlace.isec->name.startswith(".debug")) - hint = "; consider recompiling with -fdebug-types-section to reduce size " - "of debug sections"; - - errorOrWarn(errPlace.loc + "relocation " + lld::toString(rel.type) + - " out of range: " + v.str() + " is not in [" + Twine(min).str() + - ", " + Twine(max).str() + "]" + hint); -} +void reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v, + int64_t min, uint64_t max); // Make sure that V can be represented as an N bit signed integer. inline void checkInt(uint8_t *loc, int64_t v, int n, const Relocation &rel) { diff --git a/lld/test/ELF/aarch64-abs16.s b/lld/test/ELF/aarch64-abs16.s --- a/lld/test/ELF/aarch64-abs16.s +++ b/lld/test/ELF/aarch64-abs16.s @@ -21,7 +21,7 @@ // CHECK-NEXT: 220158 ffff0080 // RUN: not ld.lld %t.o %t255.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=OVERFLOW1 -// OVERFLOW1: relocation R_AARCH64_ABS16 out of range: -32769 is not in [-32768, 65535] +// OVERFLOW1: relocation R_AARCH64_ABS16 out of range: -32769 is not in [-32768, 65535]; references foo // RUN: not ld.lld %t.o %t257.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=OVERFLOW2 -// OVERFLOW2: relocation R_AARCH64_ABS16 out of range: 65536 is not in [-32768, 65535] +// OVERFLOW2: relocation R_AARCH64_ABS16 out of range: 65536 is not in [-32768, 65535]; references foo diff --git a/lld/test/ELF/aarch64-abs32.s b/lld/test/ELF/aarch64-abs32.s --- a/lld/test/ELF/aarch64-abs32.s +++ b/lld/test/ELF/aarch64-abs32.s @@ -21,7 +21,7 @@ // CHECK-NEXT: 220158 ffffffff 00000080 // RUN: not ld.lld %t.o %t255.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=OVERFLOW1 -// OVERFLOW1: relocation R_AARCH64_ABS32 out of range: -2147483649 is not in [-2147483648, 4294967295] +// OVERFLOW1: relocation R_AARCH64_ABS32 out of range: -2147483649 is not in [-2147483648, 4294967295]; references foo // RUN: not ld.lld %t.o %t257.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=OVERFLOW2 -// OVERFLOW2: relocation R_AARCH64_ABS32 out of range: 4294967296 is not in [-2147483648, 4294967295] +// OVERFLOW2: relocation R_AARCH64_ABS32 out of range: 4294967296 is not in [-2147483648, 4294967295]; references foo diff --git a/lld/test/ELF/aarch64-prel16.s b/lld/test/ELF/aarch64-prel16.s --- a/lld/test/ELF/aarch64-prel16.s +++ b/lld/test/ELF/aarch64-prel16.s @@ -25,7 +25,7 @@ // CHECK-NEXT: 202158 ffff0080 // RUN: not ld.lld -z max-page-size=4096 %t.o %t255.o -o %t2 2>&1 | FileCheck %s --check-prefix=OVERFLOW1 -// OVERFLOW1: relocation R_AARCH64_PREL16 out of range: -32769 is not in [-32768, 65535] +// OVERFLOW1: relocation R_AARCH64_PREL16 out of range: -32769 is not in [-32768, 65535]; references foo // RUN: not ld.lld -z max-page-size=4096 %t.o %t257.o -o %t2 2>&1 | FileCheck %s --check-prefix=OVERFLOW2 -// OVERFLOW2: relocation R_AARCH64_PREL16 out of range: 65536 is not in [-32768, 65535] +// OVERFLOW2: relocation R_AARCH64_PREL16 out of range: 65536 is not in [-32768, 65535]; references foo diff --git a/lld/test/ELF/aarch64-prel32.s b/lld/test/ELF/aarch64-prel32.s --- a/lld/test/ELF/aarch64-prel32.s +++ b/lld/test/ELF/aarch64-prel32.s @@ -25,7 +25,7 @@ // CHECK-NEXT: 202158 ffffffff 00000080 // RUN: not ld.lld -z max-page-size=4096 %t.o %t255.o -o %t2 2>&1 | FileCheck %s --check-prefix=OVERFLOW1 -// OVERFLOW1: relocation R_AARCH64_PREL32 out of range: -2147483649 is not in [-2147483648, 4294967295] +// OVERFLOW1: relocation R_AARCH64_PREL32 out of range: -2147483649 is not in [-2147483648, 4294967295]; references foo // RUN: not ld.lld -z max-page-size=4096 %t.o %t257.o -o %t2 2>&1 | FileCheck %s --check-prefix=OVERFLOW2 -// OVERFLOW2: relocation R_AARCH64_PREL32 out of range: 4294967296 is not in [-2147483648, 4294967295] +// OVERFLOW2: relocation R_AARCH64_PREL32 out of range: 4294967296 is not in [-2147483648, 4294967295]; references foo diff --git a/lld/test/ELF/hexagon-jump-error.s b/lld/test/ELF/hexagon-jump-error.s --- a/lld/test/ELF/hexagon-jump-error.s +++ b/lld/test/ELF/hexagon-jump-error.s @@ -12,19 +12,19 @@ .section b9, "ax" 1: -# CHECK-NEXT: relocation R_HEX_B13_PCREL out of range: 16388 is not in [-16384, 16383] +# CHECK: relocation R_HEX_B13_PCREL out of range: 16388 is not in [-16384, 16383] if (r0==#0) jump:t #1f .space (1<<14) .section b13, "ax" 1: -# CHECK-NEXT: relocation R_HEX_B15_PCREL out of range: 65540 is not in [-65536, 65535] +# CHECK: relocation R_HEX_B15_PCREL out of range: 65540 is not in [-65536, 65535] if (p0) jump #1f .space (1<<16) .section b15, "ax" 1: -# CHECK-NEXT: relocation R_HEX_B22_PCREL out of range: 8388612 is not in [-2097152, 2097151] +# CHECK: relocation R_HEX_B22_PCREL out of range: 8388612 is not in [-2097152, 2097151] jump #1f .space (1<<23) .section b22, "ax" diff --git a/lld/test/ELF/i386-reloc-16.s b/lld/test/ELF/i386-reloc-16.s --- a/lld/test/ELF/i386-reloc-16.s +++ b/lld/test/ELF/i386-reloc-16.s @@ -7,11 +7,11 @@ # RUN: not ld.lld %t.o --defsym=a=-1 --defsym=b=0 -o /dev/null 2>&1 | \ # RUN: FileCheck --check-prefix=OVERFLOW1 %s -# OVERFLOW1: relocation R_386_16 out of range: -32769 is not in [-32768, 65535] +# OVERFLOW1: relocation R_386_16 out of range: -32769 is not in [-32768, 65535]; references a # RUN: not ld.lld %t.o --defsym=a=0 --defsym=b=32769 -o /dev/null 2>&1 | \ # RUN: FileCheck --check-prefix=OVERFLOW2 %s -# OVERFLOW2: relocation R_386_16 out of range: 65536 is not in [-32768, 65535] +# OVERFLOW2: relocation R_386_16 out of range: 65536 is not in [-32768, 65535]; references b .code16 .global _start diff --git a/lld/test/ELF/i386-reloc-8.s b/lld/test/ELF/i386-reloc-8.s --- a/lld/test/ELF/i386-reloc-8.s +++ b/lld/test/ELF/i386-reloc-8.s @@ -7,11 +7,11 @@ # RUN: not ld.lld %t.o --defsym=a=-1 --defsym=b=0 -o /dev/null 2>&1 | \ # RUN: FileCheck --check-prefix=OVERFLOW1 %s -# OVERFLOW1: relocation R_386_8 out of range: -129 is not in [-128, 255] +# OVERFLOW1: relocation R_386_8 out of range: -129 is not in [-128, 255]; references a # RUN: not ld.lld %t.o --defsym=a=0 --defsym=b=129 -o /dev/null 2>&1 | \ # RUN: FileCheck --check-prefix=OVERFLOW2 %s -# OVERFLOW2: relocation R_386_8 out of range: 256 is not in [-128, 255] +# OVERFLOW2: relocation R_386_8 out of range: 256 is not in [-128, 255]; references b .code16 .globl _start diff --git a/lld/test/ELF/i386-reloc-range.s b/lld/test/ELF/i386-reloc-range.s --- a/lld/test/ELF/i386-reloc-range.s +++ b/lld/test/ELF/i386-reloc-range.s @@ -17,7 +17,7 @@ // RUN: not ld.lld -Ttext 0x200 %t.o %t2.o -o /dev/null 2>&1 | FileCheck --check-prefix=ERR %s -// ERR: {{.*}}:(.text+0x1): relocation R_386_PC16 out of range: 65536 is not in [-65536, 65535] +// ERR: {{.*}}:(.text+0x1): relocation R_386_PC16 out of range: 65536 is not in [-65536, 65535]; references foo .global _start _start: diff --git a/lld/test/ELF/ppc64-reloc-addr.s b/lld/test/ELF/ppc64-reloc-addr.s --- a/lld/test/ELF/ppc64-reloc-addr.s +++ b/lld/test/ELF/ppc64-reloc-addr.s @@ -9,12 +9,12 @@ # CHECK: 0x{{[0-9a-f]+}} ffff0080 ffffffff 00000080 # RUN: not ld.lld %t.o %t255.o -o /dev/null 2>&1 | FileCheck --check-prefix=OVERFLOW1 %s -# OVERFLOW1: relocation R_PPC64_ADDR16 out of range: -32769 is not in [-32768, 65535] -# OVERFLOW1: relocation R_PPC64_ADDR32 out of range: -2147483649 is not in [-2147483648, 4294967295] +# OVERFLOW1: relocation R_PPC64_ADDR16 out of range: -32769 is not in [-32768, 65535]; references foo +# OVERFLOW1: relocation R_PPC64_ADDR32 out of range: -2147483649 is not in [-2147483648, 4294967295]; references foo # RUN: not ld.lld %t.o %t257.o -o /dev/null 2>&1 | FileCheck --check-prefix=OVERFLOW2 %s -# OVERFLOW2: relocation R_PPC64_ADDR16 out of range: 65536 is not in [-32768, 65535] -# OVERFLOW2: relocation R_PPC64_ADDR32 out of range: 4294967296 is not in [-2147483648, 4294967295] +# OVERFLOW2: relocation R_PPC64_ADDR16 out of range: 65536 is not in [-32768, 65535]; references foo +# OVERFLOW2: relocation R_PPC64_ADDR32 out of range: 4294967296 is not in [-2147483648, 4294967295]; references foo .globl _start _start: diff --git a/lld/test/ELF/riscv-branch.s b/lld/test/ELF/riscv-branch.s --- a/lld/test/ELF/riscv-branch.s +++ b/lld/test/ELF/riscv-branch.s @@ -19,8 +19,8 @@ # RUN: not ld.lld %t.rv32.o --defsym foo=_start+0x1000 --defsym bar=_start+4-0x1002 -o %t 2>&1 | FileCheck --check-prefix=ERROR-RANGE %s # RUN: not ld.lld %t.rv64.o --defsym foo=_start+0x1000 --defsym bar=_start+4-0x1002 -o %t 2>&1 | FileCheck --check-prefix=ERROR-RANGE %s -# ERROR-RANGE: relocation R_RISCV_BRANCH out of range: 2048 is not in [-2048, 2047] -# ERROR-RANGE-NEXT: relocation R_RISCV_BRANCH out of range: -2049 is not in [-2048, 2047] +# ERROR-RANGE: relocation R_RISCV_BRANCH out of range: 2048 is not in [-2048, 2047]; references foo +# ERROR-RANGE: relocation R_RISCV_BRANCH out of range: -2049 is not in [-2048, 2047]; references bar # RUN: not ld.lld %t.rv32.o --defsym foo=_start+1 --defsym bar=_start-1 -o %t 2>&1 | FileCheck --check-prefix=ERROR-ALIGN %s # RUN: not ld.lld %t.rv64.o --defsym foo=_start+1 --defsym bar=_start-1 -o %t 2>&1 | FileCheck --check-prefix=ERROR-ALIGN %s diff --git a/lld/test/ELF/riscv-call.s b/lld/test/ELF/riscv-call.s --- a/lld/test/ELF/riscv-call.s +++ b/lld/test/ELF/riscv-call.s @@ -22,9 +22,10 @@ # LIMITS-NEXT: e7 80 00 80 jalr -2048(ra) # RUN: ld.lld %t.rv32.o --defsym foo=_start+0x7ffff800 --defsym bar=_start+8-0x80000801 -o %t -# RUN: not ld.lld %t.rv64.o --defsym foo=_start+0x7ffff800 --defsym bar=_start+8-0x80000801 -o %t 2>&1 | FileCheck --check-prefix=ERROR %s -# ERROR: relocation R_RISCV_CALL out of range: 524288 is not in [-524288, 524287] -# ERROR-NEXT: relocation R_RISCV_CALL out of range: -524289 is not in [-524288, 524287] +# RUN: not ld.lld %t.rv64.o --defsym foo=_start+0x7ffff800 --defsym bar=_start+8-0x80000801 -o %t 2>&1 | \ +# RUN: FileCheck --check-prefix=ERROR %s +# ERROR: relocation R_RISCV_CALL out of range: 524288 is not in [-524288, 524287]; references foo +# ERROR: relocation R_RISCV_CALL out of range: -524289 is not in [-524288, 524287]; references bar .global _start _start: diff --git a/lld/test/ELF/riscv-hi20-lo12.s b/lld/test/ELF/riscv-hi20-lo12.s --- a/lld/test/ELF/riscv-hi20-lo12.s +++ b/lld/test/ELF/riscv-hi20-lo12.s @@ -26,8 +26,8 @@ # LIMITS-NEXT: 23 a0 b5 80 sw a1, -2048(a1) # RUN: not ld.lld %t.rv64.o --defsym foo=0x7ffff800 --defsym bar=0xffffffff7ffff7ff -o %t 2>&1 | FileCheck --check-prefix ERROR %s -# ERROR: relocation R_RISCV_HI20 out of range: 524288 is not in [-524288, 524287] -# ERROR-NEXT: relocation R_RISCV_HI20 out of range: -524289 is not in [-524288, 524287] +# ERROR: relocation R_RISCV_HI20 out of range: 524288 is not in [-524288, 524287]; references foo +# ERROR: relocation R_RISCV_HI20 out of range: -524289 is not in [-524288, 524287]; references bar .global _start diff --git a/lld/test/ELF/riscv-jal.s b/lld/test/ELF/riscv-jal.s --- a/lld/test/ELF/riscv-jal.s +++ b/lld/test/ELF/riscv-jal.s @@ -19,13 +19,13 @@ # RUN: not ld.lld %t.rv32.o --defsym foo=_start+0x100000 --defsym bar=_start+4-0x100002 -o %t 2>&1 | FileCheck --check-prefix=ERROR-RANGE %s # RUN: not ld.lld %t.rv64.o --defsym foo=_start+0x100000 --defsym bar=_start+4-0x100002 -o %t 2>&1 | FileCheck --check-prefix=ERROR-RANGE %s -# ERROR-RANGE: relocation R_RISCV_JAL out of range: 524288 is not in [-524288, 524287] -# ERROR-RANGE-NEXT: relocation R_RISCV_JAL out of range: -524289 is not in [-524288, 524287] +# ERROR-RANGE: relocation R_RISCV_JAL out of range: 524288 is not in [-524288, 524287]; references foo +# ERROR-RANGE: relocation R_RISCV_JAL out of range: -524289 is not in [-524288, 524287]; references bar # RUN: not ld.lld %t.rv32.o --defsym foo=_start+1 --defsym bar=_start+4+3 -o %t 2>&1 | FileCheck --check-prefix=ERROR-ALIGN %s # RUN: not ld.lld %t.rv64.o --defsym foo=_start+1 --defsym bar=_start+4+3 -o %t 2>&1 | FileCheck --check-prefix=ERROR-ALIGN %s -# ERROR-ALIGN: improper alignment for relocation R_RISCV_JAL: 0x1 is not aligned to 2 bytes -# ERROR-ALIGN-NEXT: improper alignment for relocation R_RISCV_JAL: 0x3 is not aligned to 2 bytes +# ERROR-ALIGN: improper alignment for relocation R_RISCV_JAL: 0x1 is not aligned to 2 bytes +# ERROR-ALIGN: improper alignment for relocation R_RISCV_JAL: 0x3 is not aligned to 2 bytes .global _start diff --git a/lld/test/ELF/riscv-pcrel-hilo.s b/lld/test/ELF/riscv-pcrel-hilo.s --- a/lld/test/ELF/riscv-pcrel-hilo.s +++ b/lld/test/ELF/riscv-pcrel-hilo.s @@ -31,8 +31,8 @@ # RUN: ld.lld %t.rv32.o --defsym foo=_start+0x7ffff800 --defsym bar=_start+12-0x80000801 -o %t # RUN: not ld.lld %t.rv64.o --defsym foo=_start+0x7ffff800 --defsym bar=_start+12-0x80000801 -o %t 2>&1 | FileCheck --check-prefix=ERROR %s -# ERROR: relocation R_RISCV_PCREL_HI20 out of range: 524288 is not in [-524288, 524287] -# ERROR-NEXT: relocation R_RISCV_PCREL_HI20 out of range: -524289 is not in [-524288, 524287] +# ERROR: relocation R_RISCV_PCREL_HI20 out of range: 524288 is not in [-524288, 524287]; references foo +# ERROR: relocation R_RISCV_PCREL_HI20 out of range: -524289 is not in [-524288, 524287]; references bar .global _start _start: diff --git a/lld/test/ELF/x86-64-reloc-error.s b/lld/test/ELF/x86-64-reloc-error.s --- a/lld/test/ELF/x86-64-reloc-error.s +++ b/lld/test/ELF/x86-64-reloc-error.s @@ -1,10 +1,12 @@ // REQUIRES: x86 -// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/x86-64-reloc-error.s -o %tabs +// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/x86-64-reloc-error.s -o %t1.o // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t -// RUN: not ld.lld -shared %tabs %t -o /dev/null 2>&1 | FileCheck %s +// RUN: not ld.lld -shared %t1.o %t -o /dev/null 2>&1 | FileCheck %s movl $big, %edx movq $foo - 0x1000000000000, %rdx -# CHECK: {{.*}}:(.text+0x1): relocation R_X86_64_32 out of range: 68719476736 is not in [0, 4294967295] -# CHECK: {{.*}}:(.text+0x8): relocation R_X86_64_32S out of range: -281474976710656 is not in [-2147483648, 2147483647] +# CHECK: {{.*}}:(.text+0x1): relocation R_X86_64_32 out of range: 68719476736 is not in [0, 4294967295]; references big +# CHECK-NEXT: >>> defined in {{.*}}1.o +# CHECK: {{.*}}:(.text+0x8): relocation R_X86_64_32S out of range: -281474976710656 is not in [-2147483648, 2147483647]; references foo +# CHECK-NEXT: >>> defined in {{.*}}1.o diff --git a/lld/test/ELF/x86-64-reloc-error2.s b/lld/test/ELF/x86-64-reloc-error2.s --- a/lld/test/ELF/x86-64-reloc-error2.s +++ b/lld/test/ELF/x86-64-reloc-error2.s @@ -4,7 +4,8 @@ ## Check we are able to find a function symbol that encloses ## a given location when reporting error messages. -# CHECK: {{.*}}.o:(function func: .text.func+0x3): relocation R_X86_64_32S out of range: -281474974609120 is not in [-2147483648, 2147483647] +# CHECK: {{.*}}.o:(function func: .text.func+0x3): relocation R_X86_64_32S out of range: -281474974609120 is not in [-2147483648, 2147483647]; references func +# CHECK-NEXT: >>> defined in {{.*}}.o # This mergeable section will be garbage collected. We had a crash issue in that case. Test it. .section .rodata.str1,"aMS",@progbits,1