Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust demangle #112

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@
[submodule "third_party/demumble"]
path = third_party/demumble
url = https://github.com/nico/demumble.git
[submodule "third_party/rustc-demangle"]
path = third_party/rustc-demangle
url = https://github.com/zokier/rustc-demangle.git
branch = capi
23 changes: 22 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ project (Bloaty)
# Options we define for users.
option(BLOATY_ENABLE_ASAN "Enable address sanitizer." OFF)
option(BLOATY_ENABLE_UBSAN "Enable undefined behavior sanitizer." OFF)
option(BLOATY_ENABLE_RUSTC_DEMANGLE "Use rustc-demangle to demangle Rust symbols" OFF)

# Set default build type.
if(NOT CMAKE_BUILD_TYPE)
Expand Down Expand Up @@ -36,6 +37,9 @@ include_directories(third_party/protobuf/src)
include_directories(.)
include_directories(src)
include_directories(third_party/abseil-cpp)
if(BLOATY_ENABLE_RUSTC_DEMANGLE)
include_directories(third_party/rustc-demangle/rustc-demangle-capi/include)
endif()
include_directories("${CMAKE_CURRENT_BINARY_DIR}/src")

# Baseline build flags.
Expand Down Expand Up @@ -67,6 +71,21 @@ if(BLOATY_ENABLE_UBSAN)
set(CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fsanitize=undefined")
endif()

if(BLOATY_ENABLE_RUSTC_DEMANGLE)
set(BLOATY_RUSTC_DEMANGLE_LIB_FILE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/rustc-demangle/target/release/librustc_demangle.a)
add_definitions(-DBLOATY_ENABLE_RUSTC_DEMANGLE)
add_custom_command(
OUTPUT ${BLOATY_RUSTC_DEMANGLE_LIB_FILE}
COMMAND cargo build --manifest-path ${CMAKE_CURRENT_SOURCE_DIR}/third_party/rustc-demangle/Cargo.toml --all --release
)
# add_custom_target(rustc_demangle_target DEPENDS ${BLOATY_RUSTC_DEMANGLE_LIB_FILE})
add_library(rustc_demangle STATIC IMPORTED GLOBAL)
add_dependencies(rustc_demangle ${BLOATY_RUSTC_DEMANGLE_LIB_FILE})
set_target_properties(rustc_demangle PROPERTIES
IMPORTED_LOCATION ${BLOATY_RUSTC_DEMANGLE_LIB_FILE}
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/third_party/rustc-demangle/rustc-demangle-capi/include)
endif()

if(DEFINED ENV{CXXFLAGS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}")
endif()
Expand Down Expand Up @@ -106,7 +125,9 @@ add_library(libbloaty
)

set(LIBBLOATY_LIBS libbloaty libprotoc re2 capstone-static)

if(BLOATY_ENABLE_RUSTC_DEMANGLE)
set(LIBBLOATY_LIBS "${LIBBLOATY_LIBS}" rustc_demangle dl)
endif()

if(DEFINED ENV{LIB_FUZZING_ENGINE})
message("LIB_FUZZING_ENGINE set, building fuzz_target instead of Bloaty")
Expand Down
21 changes: 21 additions & 0 deletions src/bloaty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ static void Throw(const char *str, int line) {
#define THROW(msg) Throw(msg, __LINE__)
#define THROWF(...) Throw(absl::Substitute(__VA_ARGS__).c_str(), __LINE__)

#ifdef BLOATY_ENABLE_RUSTC_DEMANGLE
#include "rustc_demangle.h"
#else
int rustc_demangle(const char *mangled, char *out, size_t out_size) {
THROW("rustc-demangle not enabled");
return 0;
}
#endif

namespace bloaty {

// Use a global since we would have to plumb it through so many call-stacks
Expand Down Expand Up @@ -98,6 +107,7 @@ constexpr DataSourceDefinition data_sources[] = {
"symbols from symbol table (configure demangling with --demangle)"},
{DataSource::kRawSymbols, "rawsymbols", "unmangled symbols"},
{DataSource::kFullSymbols, "fullsymbols", "full demangled symbols"},
{DataSource::kRustSymbols, "rustsymbols", "rust demangled symbols"},
{DataSource::kShortSymbols, "shortsymbols", "short demangled symbols"},
};

Expand Down Expand Up @@ -249,6 +259,13 @@ std::string ItaniumDemangle(string_view symbol, DataSource source) {
} else {
return std::string(symbol);
}
} else if (source == DataSource::kRustSymbols) {
char demangled[1024];
if (::rustc_demangle(demangle_from.data(), demangled, sizeof(demangled))) {
return std::string(demangled);
} else {
return std::string(symbol);
}
} else if (source == DataSource::kFullSymbols) {
char* demangled =
__cxa_demangle(demangle_from.data(), NULL, NULL, NULL);
Expand Down Expand Up @@ -1203,6 +1220,8 @@ class Bloaty {
return DataSource::kRawSymbols;
case Options::DEMANGLE_SHORT:
return DataSource::kShortSymbols;
case Options::DEMANGLE_RUST:
return DataSource::kRustSymbols;
case Options::DEMANGLE_FULL:
return DataSource::kFullSymbols;
default:
Expand Down Expand Up @@ -1783,6 +1802,8 @@ bool DoParseOptions(bool skip_unknown, int* argc, char** argv[],
options->set_demangle(Options::DEMANGLE_SHORT);
} else if (option == "full") {
options->set_demangle(Options::DEMANGLE_FULL);
} else if (option == "rust") {
options->set_demangle(Options::DEMANGLE_RUST);
} else {
THROWF("unknown value for --demangle: $0", option);
}
Expand Down
1 change: 1 addition & 0 deletions src/bloaty.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ enum class DataSource {

kRawSymbols,
kFullSymbols,
kRustSymbols,
kShortSymbols
};

Expand Down
1 change: 1 addition & 0 deletions src/bloaty.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ message Options {
DEMANGLE_SHORT = 0;
DEMANGLE_FULL = 1;
DEMANGLE_NONE = 2;
DEMANGLE_RUST = 3;
}
optional Demangle demangle = 5 [default = DEMANGLE_SHORT];

Expand Down
1 change: 1 addition & 0 deletions src/elf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,7 @@ class ElfObjectFile : public ObjectFile {
case DataSource::kRawSymbols:
case DataSource::kShortSymbols:
case DataSource::kFullSymbols:
case DataSource::kRustSymbols:
ReadELFSymbols(debug_file().file_data(), sink, nullptr, false);
break;
case DataSource::kArchiveMembers:
Expand Down
1 change: 1 addition & 0 deletions third_party/rustc-demangle
Submodule rustc-demangle added at 249546