Skip to content

Commit

Permalink
Attempt to demangle as a Rust symbol before as C++
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Jul 4, 2022
1 parent 808f124 commit 22e1bba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
39 changes: 25 additions & 14 deletions demangle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions elf/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
}
Expand Down
1 change: 1 addition & 0 deletions mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 22e1bba

Please sign in to comment.