Skip to content

Commit d73ef97

Browse files
authored
[lld/COFF] Demangle symbol name in discarded section relocation error message (#119726)
1 parent f22cff7 commit d73ef97

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

lld/COFF/Chunks.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,14 @@ static void maybeReportRelocationToDiscarded(const SectionChunk *fromChunk,
369369
// Get the name of the symbol. If it's null, it was discarded early, so we
370370
// have to go back to the object file.
371371
ObjFile *file = fromChunk->file;
372-
StringRef name;
372+
std::string name;
373373
if (sym) {
374-
name = sym->getName();
374+
name = toString(file->ctx, *sym);
375375
} else {
376376
COFFSymbolRef coffSym =
377377
check(file->getCOFFObj()->getSymbol(rel.SymbolTableIndex));
378-
name = check(file->getCOFFObj()->getSymbolName(coffSym));
378+
name = maybeDemangleSymbol(
379+
file->ctx, check(file->getCOFFObj()->getSymbolName(coffSym)));
379380
}
380381

381382
std::vector<std::string> symbolLocations =

lld/COFF/Symbols.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ static_assert(sizeof(SymbolUnion) <= 48,
2828
"symbols should be optimized for memory usage");
2929

3030
// Returns a symbol name for an error message.
31-
static std::string maybeDemangleSymbol(const COFFLinkerContext &ctx,
32-
StringRef symName) {
31+
std::string maybeDemangleSymbol(const COFFLinkerContext &ctx,
32+
StringRef symName) {
3333
if (ctx.config.demangle) {
3434
std::string prefix;
3535
StringRef prefixless = symName;

lld/COFF/Symbols.h

+4
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,10 @@ std::string toString(const coff::COFFLinkerContext &ctx, coff::Symbol &b);
533533
std::string toCOFFString(const coff::COFFLinkerContext &ctx,
534534
const llvm::object::Archive::Symbol &b);
535535

536+
// Returns a symbol name for an error message.
537+
std::string maybeDemangleSymbol(const coff::COFFLinkerContext &ctx,
538+
StringRef symName);
539+
536540
} // namespace lld
537541

538542
#endif

lld/test/COFF/reloc-discarded.s

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88

99
# RUN: not lld-link -entry:main -nodefaultlib %t1.obj %t2.obj -out:%t.exe -opt:ref 2>&1 | FileCheck %s
1010
# RUN: not lld-link -entry:main -nodefaultlib %t1.obj %t2.obj -out:%t.exe -opt:noref 2>&1 | FileCheck %s
11+
# RUN: not lld-link -entry:main -nodefaultlib %t1.obj %t2.obj -out:%t.exe -demangle:no 2>&1 \
12+
# RUN: | FileCheck --check-prefix=NODEMANGLE %s
1113

12-
# CHECK: error: relocation against symbol in discarded section: assoc_global
14+
# CHECK: error: relocation against symbol in discarded section: int __cdecl assoc_global(void)
1315
# CHECK: >>> referenced by {{.*}}reloc-discarded{{.*}}.obj:(main)
1416

17+
# NODEMANGLE: error: relocation against symbol in discarded section: ?assoc_global@@YAHXZ
18+
# NODEMANGLE: >>> referenced by {{.*}}reloc-discarded{{.*}}.obj:(main)
19+
1520
.section .bss,"bw",discard,main_global
1621
.globl main_global
1722
.p2align 2
@@ -20,12 +25,12 @@ main_global:
2025

2126
.section .CRT$XCU,"dr",associative,main_global
2227
.p2align 3
23-
assoc_global:
28+
"?assoc_global@@YAHXZ":
2429
.quad main_global
2530

2631
.text
2732
.globl main
2833
main:
29-
movq assoc_global(%rip), %rax
34+
movq "?assoc_global@@YAHXZ"(%rip), %rax
3035
movl (%rax), %eax
3136
retq

0 commit comments

Comments
 (0)