Skip to content

Commit

Permalink
[lld/mac] With -demangle, strip leading _ from non-mangled names
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nico committed Oct 4, 2022
1 parent 2e91a5f commit 062c660
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
13 changes: 11 additions & 2 deletions lld/MachO/Symbols.cpp
Expand Up @@ -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); }
Expand Down
8 changes: 4 additions & 4 deletions 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
Expand All @@ -12,6 +12,6 @@
# CHECK-NEXT: >>> defined in [[FILE_2]]

.text
.global _ZN1a1bL3fooE
_ZN1a1bL3fooE:
.global __ZN1a1bL3fooE
__ZN1a1bL3fooE:
ret
6 changes: 3 additions & 3 deletions lld/test/MachO/invalid/undefined-symbol.s
Expand Up @@ -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]]

Expand Down
2 changes: 1 addition & 1 deletion lld/test/MachO/undef-suggest-extern-c.s
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lld/test/MachO/undef-suggest-extern-c2.s
Expand Up @@ -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"?

Expand Down

0 comments on commit 062c660

Please sign in to comment.