1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-20 01:18:53 +03:00

Attempt to demangle as a Rust symbol before as C++

https://github.com/rui314/mold/issues/371
This commit is contained in:
Rui Ueyama 2022-07-04 20:42:05 +08:00
parent 808f1240ac
commit 22e1bba58e
3 changed files with 29 additions and 16 deletions

View File

@ -7,23 +7,34 @@
namespace mold {
std::string_view demangle(std::string_view name) {
if (name.starts_with("_Z")) {
static thread_local char *buf;
if (buf)
free(buf);
static thread_local char *buf;
if (buf)
free(buf);
int status;
buf = abi::__cxa_demangle(std::string(name).c_str(), nullptr, nullptr,
&status);
if (status == 0)
return buf;
buf = rust_demangle(std::string(name).c_str(), 0);
if (buf)
return buf;
}
// Try to demangle as a Rust symbol.
buf = rust_demangle(std::string(name).c_str(), 0);
if (buf)
return buf;
// Try to demangle as a C++ symbol.
if (std::optional<std::string_view> s = cpp_demangle(name))
return *s;
return name;
}
std::optional<std::string_view> cpp_demangle(std::string_view name) {
static thread_local char *buf;
static thread_local size_t buflen;
if (name.starts_with("_Z")) {
int status;
char *p = abi::__cxa_demangle(std::string(name).c_str(), buf, &buflen, &status);
if (status == 0) {
buf = p;
return p;
}
}
return {};
}
} // namespace mold

View File

@ -1092,8 +1092,9 @@ void apply_version_script(Context<E> &ctx) {
}
if (!cpp_matcher.empty())
if (std::optional<u16> ver = cpp_matcher.find(demangle(name)))
sym->ver_idx = *ver;
if (std::optional<std::string_view> s = cpp_demangle(name))
if (std::optional<u16> ver = cpp_matcher.find(*s))
sym->ver_idx = *ver;
}
});
}

1
mold.h
View File

@ -554,6 +554,7 @@ std::filesystem::path to_abs_path(std::filesystem::path path);
//
std::string_view demangle(std::string_view name);
std::optional<std::string_view> cpp_demangle(std::string_view name);
//
// compress.cc