Skip to content

Commit 22e1bba

Browse files
committed
Attempt to demangle as a Rust symbol before as C++
#371
1 parent 808f124 commit 22e1bba

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

demangle.cc

+25-14
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,34 @@
77
namespace mold {
88

99
std::string_view demangle(std::string_view name) {
10-
if (name.starts_with("_Z")) {
11-
static thread_local char *buf;
12-
if (buf)
13-
free(buf);
10+
static thread_local char *buf;
11+
if (buf)
12+
free(buf);
1413

15-
int status;
16-
buf = abi::__cxa_demangle(std::string(name).c_str(), nullptr, nullptr,
17-
&status);
18-
if (status == 0)
19-
return buf;
20-
21-
buf = rust_demangle(std::string(name).c_str(), 0);
22-
if (buf)
23-
return buf;
24-
}
14+
// Try to demangle as a Rust symbol.
15+
buf = rust_demangle(std::string(name).c_str(), 0);
16+
if (buf)
17+
return buf;
2518

19+
// Try to demangle as a C++ symbol.
20+
if (std::optional<std::string_view> s = cpp_demangle(name))
21+
return *s;
2622
return name;
2723
}
2824

25+
std::optional<std::string_view> cpp_demangle(std::string_view name) {
26+
static thread_local char *buf;
27+
static thread_local size_t buflen;
28+
29+
if (name.starts_with("_Z")) {
30+
int status;
31+
char *p = abi::__cxa_demangle(std::string(name).c_str(), buf, &buflen, &status);
32+
if (status == 0) {
33+
buf = p;
34+
return p;
35+
}
36+
}
37+
return {};
38+
}
39+
2940
} // namespace mold

elf/passes.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -1092,8 +1092,9 @@ void apply_version_script(Context<E> &ctx) {
10921092
}
10931093

10941094
if (!cpp_matcher.empty())
1095-
if (std::optional<u16> ver = cpp_matcher.find(demangle(name)))
1096-
sym->ver_idx = *ver;
1095+
if (std::optional<std::string_view> s = cpp_demangle(name))
1096+
if (std::optional<u16> ver = cpp_matcher.find(*s))
1097+
sym->ver_idx = *ver;
10971098
}
10981099
});
10991100
}

mold.h

+1
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ std::filesystem::path to_abs_path(std::filesystem::path path);
554554
//
555555

556556
std::string_view demangle(std::string_view name);
557+
std::optional<std::string_view> cpp_demangle(std::string_view name);
557558

558559
//
559560
// compress.cc

0 commit comments

Comments
 (0)