[lld] 062c660 - [lld/mac] With -demangle, strip leading _ from non-mangled names
Nico Weber via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 4 13:47:17 PDT 2022
Author: Nico Weber
Date: 2022-10-04T16:47:07-04:00
New Revision: 062c660dbfa81a742440cec49446fe81fc7d0114
URL: https://github.com/llvm/llvm-project/commit/062c660dbfa81a742440cec49446fe81fc7d0114
DIFF: https://github.com/llvm/llvm-project/commit/062c660dbfa81a742440cec49446fe81fc7d0114.diff
LOG: [lld/mac] With -demangle, strip leading _ from non-mangled names
For
void f();
int main() { f(); }
`lld -demangle` now produces
ld64.lld: error: undefined symbol: f
>>> referenced by path/to/main.o:(symbol main+0x8)
instead of
ld64.lld: error: undefined symbol: _f
>>> referenced by path/to/main.o:(symbol _main+0x8)
previously. (Without `-demangle`, it still prints `_f` and `_main`.)
This does *not* match ld64's behavior, but it does match e.g. lld/COFF's
behaviour.
This is arguably easier to understand: clang prepends symbol names with `_`
on macOS, so it seems friendly if the linker removes it again in its
diagnostics. It also makes the `extern "C"` insertion diagnostics we added
recently look more self-consistent.
Differential Revision: https://reviews.llvm.org/D135189
Added:
Modified:
lld/MachO/Symbols.cpp
lld/test/MachO/invalid/duplicate-symbol.s
lld/test/MachO/invalid/undefined-symbol.s
lld/test/MachO/undef-suggest-extern-c.s
lld/test/MachO/undef-suggest-extern-c2.s
Removed:
################################################################################
diff --git a/lld/MachO/Symbols.cpp b/lld/MachO/Symbols.cpp
index acfc2d6dcd397..a3553769d6f1d 100644
--- a/lld/MachO/Symbols.cpp
+++ b/lld/MachO/Symbols.cpp
@@ -28,12 +28,21 @@ static_assert(sizeof(void *) != 8 || sizeof(Defined) == 88,
static_assert(sizeof(SymbolUnion) == sizeof(Defined),
"Defined should be the largest Symbol kind");
+// Returns a symbol name for an error message.
+static std::string maybeDemangleSymbol(StringRef symName) {
+ if (config->demangle) {
+ symName.consume_front("_");
+ return demangle(symName, true);
+ }
+ return std::string(symName);
+}
+
std::string lld::toString(const Symbol &sym) {
- return demangle(sym.getName(), config->demangle);
+ return maybeDemangleSymbol(sym.getName());
}
std::string lld::toMachOString(const object::Archive::Symbol &b) {
- return demangle(b.getName(), config->demangle);
+ return maybeDemangleSymbol(b.getName());
}
uint64_t Symbol::getStubVA() const { return in.stubs->getVA(stubsIndex); }
diff --git a/lld/test/MachO/invalid/duplicate-symbol.s b/lld/test/MachO/invalid/duplicate-symbol.s
index 28495baea5cb7..e469835e43044 100644
--- a/lld/test/MachO/invalid/duplicate-symbol.s
+++ b/lld/test/MachO/invalid/duplicate-symbol.s
@@ -1,8 +1,8 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t-dup.o
-# RUN: not %lld -dylib -o /dev/null %t-dup.o %t.o 2>&1 | FileCheck %s -DNAME=_ZN1a1bL3fooE -DFILE_1=%t-dup.o -DFILE_2=%t.o
-# RUN: not %lld -dylib -o /dev/null %t.o %t.o 2>&1 | FileCheck %s -DNAME=_ZN1a1bL3fooE -DFILE_1=%t.o -DFILE_2=%t.o
+# RUN: not %lld -dylib -o /dev/null %t-dup.o %t.o 2>&1 | FileCheck %s -DNAME=__ZN1a1bL3fooE -DFILE_1=%t-dup.o -DFILE_2=%t.o
+# RUN: not %lld -dylib -o /dev/null %t.o %t.o 2>&1 | FileCheck %s -DNAME=__ZN1a1bL3fooE -DFILE_1=%t.o -DFILE_2=%t.o
# RUN: not %lld -dylib -demangle -o /dev/null %t-dup.o %t.o 2>&1 | FileCheck %s -DNAME="a::b::foo" -DFILE_1=%t-dup.o -DFILE_2=%t.o
# RUN: not %lld -dylib -demangle -o /dev/null %t.o %t.o 2>&1 | FileCheck %s -DNAME="a::b::foo" -DFILE_1=%t.o -DFILE_2=%t.o
@@ -12,6 +12,6 @@
# CHECK-NEXT: >>> defined in [[FILE_2]]
.text
-.global _ZN1a1bL3fooE
-_ZN1a1bL3fooE:
+.global __ZN1a1bL3fooE
+__ZN1a1bL3fooE:
ret
diff --git a/lld/test/MachO/invalid/undefined-symbol.s b/lld/test/MachO/invalid/undefined-symbol.s
index 9da4a7326011b..dc0a6a1cb7042 100644
--- a/lld/test/MachO/invalid/undefined-symbol.s
+++ b/lld/test/MachO/invalid/undefined-symbol.s
@@ -6,15 +6,15 @@
# RUN: not %lld --icf=all -o /dev/null %t/main.o 2>&1 | \
# RUN: FileCheck %s -DSYM=__ZN3foo3barEi -DLOC='%t/main.o:(symbol _main+0x1)'
# RUN: not %lld -demangle --icf=all -o /dev/null %t/main.o 2>&1 | \
-# RUN: FileCheck %s -DSYM='foo::bar(int)' -DLOC='%t/main.o:(symbol _main+0x1)'
+# RUN: FileCheck %s -DSYM='foo::bar(int)' -DLOC='%t/main.o:(symbol main+0x1)'
# RUN: not %lld -o /dev/null %t/main.o %t/foo.a 2>&1 | \
# RUN: FileCheck %s -DSYM=_bar -DLOC='%t/foo.a(foo.o):(symbol __ZN3foo3barEi+0x1)'
# RUN: not %lld -demangle -o /dev/null %t/main.o %t/foo.a 2>&1 | \
-# RUN: FileCheck %s -DSYM=_bar -DLOC='%t/foo.a(foo.o):(symbol foo::bar(int)+0x1)'
+# RUN: FileCheck %s -DSYM=bar -DLOC='%t/foo.a(foo.o):(symbol foo::bar(int)+0x1)'
# RUN: not %lld -o /dev/null %t/main.o -force_load %t/foo.a 2>&1 | \
# RUN: FileCheck %s -DSYM=_bar -DLOC='%t/foo.a(foo.o):(symbol __ZN3foo3barEi+0x1)'
# RUN: not %lld -demangle -o /dev/null %t/main.o -force_load %t/foo.a 2>&1 | \
-# RUN: FileCheck %s -DSYM=_bar -DLOC='%t/foo.a(foo.o):(symbol foo::bar(int)+0x1)'
+# RUN: FileCheck %s -DSYM=bar -DLOC='%t/foo.a(foo.o):(symbol foo::bar(int)+0x1)'
# CHECK: error: undefined symbol: [[SYM]]
# CHECK-NEXT: >>> referenced by [[LOC]]
diff --git a/lld/test/MachO/undef-suggest-extern-c.s b/lld/test/MachO/undef-suggest-extern-c.s
index 10bca0fbc1682..8c53c8e602cad 100644
--- a/lld/test/MachO/undef-suggest-extern-c.s
+++ b/lld/test/MachO/undef-suggest-extern-c.s
@@ -8,7 +8,7 @@
# CHECK: error: undefined symbol: foo(int)
# CHECK-NEXT: >>> referenced by {{.*}}
-# CHECK-NEXT: >>> did you mean: extern "C" _foo
+# CHECK-NEXT: >>> did you mean: extern "C" foo
## Don't suggest for nested names like F::foo() and foo::foo().
# RUN: echo 'call __ZN1F3fooEv; call __ZN3fooC1Ev' | llvm-mc -filetype=obj -triple=x86_64-apple-macos - -o %t2.o
diff --git a/lld/test/MachO/undef-suggest-extern-c2.s b/lld/test/MachO/undef-suggest-extern-c2.s
index afdfbcfa415f6..152fe3877e2dd 100644
--- a/lld/test/MachO/undef-suggest-extern-c2.s
+++ b/lld/test/MachO/undef-suggest-extern-c2.s
@@ -10,7 +10,7 @@
# RUN: echo '__Z3fooi: call _foo' | llvm-mc -filetype=obj -triple=x86_64-apple-macos - -o %t2.o
# RUN: not %lld %t2.o -demangle -o /dev/null 2>&1 | FileCheck %s
-# CHECK: error: undefined symbol: _foo
+# CHECK: error: undefined symbol: foo
# CHECK-NEXT: >>> referenced by {{.*}}
# CHECK-NEXT: >>> did you mean to declare foo(int) as extern "C"?
More information about the llvm-commits
mailing list