2021-02-27 06:16:51 +03:00
|
|
|
#include "mold.h"
|
|
|
|
|
|
|
|
#include <cxxabi.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-03-27 09:25:45 +03:00
|
|
|
static thread_local char *demangle_buf;
|
|
|
|
static thread_local size_t demangle_buf_len;
|
|
|
|
|
2021-02-28 15:11:11 +03:00
|
|
|
static bool is_mangled_name(std::string_view name) {
|
|
|
|
return name.starts_with("_Z");
|
|
|
|
}
|
|
|
|
|
2021-03-29 14:29:57 +03:00
|
|
|
template <typename E>
|
|
|
|
std::string_view Symbol<E>::get_demangled_name() const {
|
2021-04-12 14:14:37 +03:00
|
|
|
if (is_mangled_name(name())) {
|
2021-05-20 09:12:46 +03:00
|
|
|
char *mangled = new char[name().size() + 1];
|
|
|
|
memcpy(mangled, name().data(), name().size());
|
|
|
|
mangled[name().size()] = '\0';
|
|
|
|
|
2021-03-27 09:25:45 +03:00
|
|
|
size_t len = sizeof(demangle_buf);
|
2021-02-27 06:16:51 +03:00
|
|
|
int status;
|
2021-03-27 09:25:45 +03:00
|
|
|
demangle_buf =
|
2021-05-20 09:12:46 +03:00
|
|
|
abi::__cxa_demangle(mangled, demangle_buf, &demangle_buf_len, &status);
|
|
|
|
delete[](mangled);
|
2021-03-27 09:25:45 +03:00
|
|
|
if (status == 0)
|
|
|
|
return demangle_buf;
|
2021-02-27 06:16:51 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 14:14:37 +03:00
|
|
|
return name();
|
2021-03-27 09:25:45 +03:00
|
|
|
}
|
|
|
|
|
2021-03-29 14:29:57 +03:00
|
|
|
template <typename E>
|
|
|
|
std::ostream &operator<<(std::ostream &out, const Symbol<E> &sym) {
|
2021-03-29 10:48:23 +03:00
|
|
|
if (opt_demangle)
|
2021-03-27 09:25:45 +03:00
|
|
|
out << sym.get_demangled_name();
|
|
|
|
else
|
2021-04-12 14:14:37 +03:00
|
|
|
out << sym.name();
|
2021-02-27 06:16:51 +03:00
|
|
|
return out;
|
|
|
|
}
|
2021-03-29 14:29:57 +03:00
|
|
|
|
2021-04-05 16:19:51 +03:00
|
|
|
#define INSTANTIATE(E) \
|
|
|
|
template class Symbol<E>; \
|
|
|
|
template std::ostream &operator<<(std::ostream &, const Symbol<E> &)
|
2021-03-29 14:29:57 +03:00
|
|
|
|
2021-04-05 16:19:51 +03:00
|
|
|
INSTANTIATE(X86_64);
|
|
|
|
INSTANTIATE(I386);
|
2021-07-03 15:12:08 +03:00
|
|
|
INSTANTIATE(AARCH64);
|