Skip to content

Commit

Permalink
[lld/COFF] Demangle symbol name in discarded section relocation error…
Browse files Browse the repository at this point in the history
… message (#119726)
  • Loading branch information
nico authored Dec 14, 2024
1 parent f22cff7 commit d73ef97
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
7 changes: 4 additions & 3 deletions lld/COFF/Chunks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,14 @@ static void maybeReportRelocationToDiscarded(const SectionChunk *fromChunk,
// Get the name of the symbol. If it's null, it was discarded early, so we
// have to go back to the object file.
ObjFile *file = fromChunk->file;
StringRef name;
std::string name;
if (sym) {
name = sym->getName();
name = toString(file->ctx, *sym);
} else {
COFFSymbolRef coffSym =
check(file->getCOFFObj()->getSymbol(rel.SymbolTableIndex));
name = check(file->getCOFFObj()->getSymbolName(coffSym));
name = maybeDemangleSymbol(
file->ctx, check(file->getCOFFObj()->getSymbolName(coffSym)));
}

std::vector<std::string> symbolLocations =
Expand Down
4 changes: 2 additions & 2 deletions lld/COFF/Symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ static_assert(sizeof(SymbolUnion) <= 48,
"symbols should be optimized for memory usage");

// Returns a symbol name for an error message.
static std::string maybeDemangleSymbol(const COFFLinkerContext &ctx,
StringRef symName) {
std::string maybeDemangleSymbol(const COFFLinkerContext &ctx,
StringRef symName) {
if (ctx.config.demangle) {
std::string prefix;
StringRef prefixless = symName;
Expand Down
4 changes: 4 additions & 0 deletions lld/COFF/Symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,10 @@ std::string toString(const coff::COFFLinkerContext &ctx, coff::Symbol &b);
std::string toCOFFString(const coff::COFFLinkerContext &ctx,
const llvm::object::Archive::Symbol &b);

// Returns a symbol name for an error message.
std::string maybeDemangleSymbol(const coff::COFFLinkerContext &ctx,
StringRef symName);

} // namespace lld

#endif
11 changes: 8 additions & 3 deletions lld/test/COFF/reloc-discarded.s
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@

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

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

# NODEMANGLE: error: relocation against symbol in discarded section: ?assoc_global@@YAHXZ
# NODEMANGLE: >>> referenced by {{.*}}reloc-discarded{{.*}}.obj:(main)

.section .bss,"bw",discard,main_global
.globl main_global
.p2align 2
Expand All @@ -20,12 +25,12 @@ main_global:

.section .CRT$XCU,"dr",associative,main_global
.p2align 3
assoc_global:
"?assoc_global@@YAHXZ":
.quad main_global

.text
.globl main
main:
movq assoc_global(%rip), %rax
movq "?assoc_global@@YAHXZ"(%rip), %rax
movl (%rax), %eax
retq

0 comments on commit d73ef97

Please sign in to comment.