1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-22 02:20:51 +03:00
mold/symbols.cc

48 lines
1.2 KiB
C++
Raw Normal View History

2021-02-27 06:16:51 +03:00
#include "mold.h"
#include <cxxabi.h>
#include <stdlib.h>
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';
size_t len = sizeof(demangle_buf);
2021-02-27 06:16:51 +03:00
int status;
demangle_buf =
2021-05-20 09:12:46 +03:00
abi::__cxa_demangle(mangled, demangle_buf, &demangle_buf_len, &status);
delete[](mangled);
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-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)
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);
INSTANTIATE(AARCH64);