Skip to content
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

Bad symbol name in the error: undefined symbol error message #1159

Closed
marxin opened this issue Nov 29, 2023 · 3 comments
Closed

Bad symbol name in the error: undefined symbol error message #1159

marxin opened this issue Nov 29, 2023 · 3 comments

Comments

@marxin
Copy link
Sponsor Contributor

marxin commented Nov 29, 2023

I noticed the following strange behavior:

namespace ns {
	void version();
};

int main()
{
  ns::version();

	return 0;
}
$ marxin@marxinbox:/tmp> g++ main.cpp -fuse-ld=mold -Wl,--version
collect2 version 13.2.1 20230912 [revision b96e66fd4ef3e36983969fb8cdd1956f551a074b]
$ g++ main.cpp -fuse-ld=mold
mold: error: undefined symbol: ns::versionv
>>> referenced by main.cpp
>>>               /tmp/ccdckvbd.o:(main)
collect2: error: ld returned 1 exit status
$ nm a-main.o
0000000000000000 T main
                 U _ZN2ns7versionEv
$ nm -C a-main.o
0000000000000000 T main
                 U ns::version()

Note the superfluous v character and compared to the usual message, the parenthesis are missing:

mold: error: undefined symbol: foo()
@rui314
Copy link
Owner

rui314 commented Nov 29, 2023

_ZN2ns7versionEv is valid as a Rust symbol of ns::versionv and C++ symbol of ns::version(). It is inherently ambiguous. Let me try to find a way to find if an object file is created by the Rust compiler.

@marxin
Copy link
Sponsor Contributor Author

marxin commented Nov 29, 2023

That's funny!

Well, one might probably rely on:

readelf -p .comment x.o

String dump of section '.comment':
  [     1]  rustc version 1.74.0 (79e9716c9 2023-11-13)

@rui314
Copy link
Owner

rui314 commented Nov 30, 2023

Yeah, it seems like reading the contents of .comment is the best way to identify whether an object file was created by rustc or not.

@rui314 rui314 closed this as completed in ea9864b Nov 30, 2023
VitalyAnkh pushed a commit to VitalyAnkh/mold that referenced this issue Dec 23, 2023
Previously, we always tried to demangle a symbol name as a Rust
symbol first and then attempted to demangle it as a C++ symbol.
This resulted in an incorrect demangled result, as the Rust legacy
mangling scheme is not distinguishable from C++.

This patch fix the issue by adding the "is_rust_obj" flag to the
ObjectFile. We try to demangle a symbol as a Rust one before as a C++
only if the flag is true.

Fixes rui314#1159
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants