diff --git a/lld/MachO/Symbols.cpp b/lld/MachO/Symbols.cpp --- a/lld/MachO/Symbols.cpp +++ b/lld/MachO/Symbols.cpp @@ -28,12 +28,21 @@ 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 --- 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 --- 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 --- 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 --- 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"?