[ELF] Mention symbol name in reportRangeError()

For an out-of-range relocation referencing a non-local symbol, report the symbol name and the object file that defines the symbol. As an example:
```
t.o:(function func: .text.func+0x3): relocation R_X86_64_32S out of range: -281474974609120 is not in [-2147483648, 2147483647]
```
=>
```
t.o:(function func: .text.func+0x3): relocation R_X86_64_32S out of range: -281474974609120 is not in [-2147483648, 2147483647]; references func
>>> defined in t1.o
```

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D73518
This commit is contained in:
Fangrui Song 2020-01-27 17:35:06 -08:00
parent 25b8e96388
commit 4a4ce14eb2
18 changed files with 71 additions and 57 deletions

View File

@ -73,6 +73,15 @@ static Optional<std::string> getLinkerScriptLocation(const Symbol &sym) {
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<std::string> 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 @@ static Optional<std::string> getLinkerScriptLocation(const Symbol &sym) {
// >>> /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<std::string> 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.
//

View File

@ -204,18 +204,8 @@ TargetInfo *getTarget();
template <class ELFT> 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) {

View File

@ -21,7 +21,7 @@ _start:
// 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

View File

@ -21,7 +21,7 @@ _start:
// 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

View File

@ -25,7 +25,7 @@ _start:
// 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

View File

@ -25,7 +25,7 @@ _start:
// 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

View File

@ -12,19 +12,19 @@ _start:
.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"

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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:

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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