Skip to content

[lld/COFF] Demangle symbol name in discarded section relocation error message #119726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2024

Conversation

nico
Copy link
Contributor

@nico nico commented Dec 12, 2024

A little unrelated polish thing I noticed in https://crbug.com/383531486

@llvmbot
Copy link
Member

llvmbot commented Dec 12, 2024

@llvm/pr-subscribers-lld-coff
@llvm/pr-subscribers-platform-windows

@llvm/pr-subscribers-lld

Author: Nico Weber (nico)

Changes

A little unrelated polish thing I noticed in https://crbug.com/383531486


Full diff: https://github.com/llvm/llvm-project/pull/119726.diff

4 Files Affected:

  • (modified) lld/COFF/Chunks.cpp (+4-3)
  • (modified) lld/COFF/Symbols.cpp (+2-2)
  • (modified) lld/COFF/Symbols.h (+4)
  • (modified) lld/test/COFF/reloc-discarded.s (+8-3)
diff --git a/lld/COFF/Chunks.cpp b/lld/COFF/Chunks.cpp
index 23fab0e66bb67f..c3a8b2f91999bc 100644
--- a/lld/COFF/Chunks.cpp
+++ b/lld/COFF/Chunks.cpp
@@ -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 =
diff --git a/lld/COFF/Symbols.cpp b/lld/COFF/Symbols.cpp
index 7de2c3829d1b0d..35c421d599ff55 100644
--- a/lld/COFF/Symbols.cpp
+++ b/lld/COFF/Symbols.cpp
@@ -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;
diff --git a/lld/COFF/Symbols.h b/lld/COFF/Symbols.h
index 3b367a38c889f4..465d4df52c6300 100644
--- a/lld/COFF/Symbols.h
+++ b/lld/COFF/Symbols.h
@@ -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
diff --git a/lld/test/COFF/reloc-discarded.s b/lld/test/COFF/reloc-discarded.s
index 378804b623128c..667df0faeb5a89 100644
--- a/lld/test/COFF/reloc-discarded.s
+++ b/lld/test/COFF/reloc-discarded.s
@@ -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
@@ -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

@nico nico merged commit d73ef97 into llvm:main Dec 14, 2024
8 checks passed
@nico nico deleted the cofferr branch December 14, 2024 00:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants