2016-09-06 21:16:48 +02:00
|
|
|
//===-- llvm-c++filt.cpp --------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-09-06 21:16:48 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-02-11 21:30:53 +01:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2019-11-18 23:51:41 +01:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2016-09-06 21:16:48 +02:00
|
|
|
#include "llvm/Demangle/Demangle.h"
|
2021-07-09 19:10:45 +02:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
2017-01-20 05:25:26 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2019-11-18 23:51:41 +01:00
|
|
|
#include "llvm/Support/Host.h"
|
2018-04-13 20:26:06 +02:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2021-07-09 19:10:45 +02:00
|
|
|
#include "llvm/Support/WithColor.h"
|
2016-09-27 20:50:30 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-11-13 21:43:38 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
2016-09-06 21:16:48 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2021-07-09 19:10:45 +02:00
|
|
|
namespace {
|
|
|
|
enum ID {
|
|
|
|
OPT_INVALID = 0, // This is not an option ID.
|
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
|
|
|
HELPTEXT, METAVAR, VALUES) \
|
|
|
|
OPT_##ID,
|
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTION
|
2017-01-21 03:36:26 +01:00
|
|
|
};
|
2021-07-09 19:10:45 +02:00
|
|
|
|
|
|
|
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
|
|
|
|
#include "Opts.inc"
|
|
|
|
#undef PREFIX
|
|
|
|
|
|
|
|
const opt::OptTable::Info InfoTable[] = {
|
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
|
|
|
HELPTEXT, METAVAR, VALUES) \
|
|
|
|
{ \
|
|
|
|
PREFIX, NAME, HELPTEXT, \
|
|
|
|
METAVAR, OPT_##ID, opt::Option::KIND##Class, \
|
|
|
|
PARAM, FLAGS, OPT_##GROUP, \
|
|
|
|
OPT_##ALIAS, ALIASARGS, VALUES},
|
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
class CxxfiltOptTable : public opt::OptTable {
|
|
|
|
public:
|
|
|
|
CxxfiltOptTable() : OptTable(InfoTable) { setGroupedShortOptions(true); }
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static bool StripUnderscore;
|
|
|
|
static bool Types;
|
|
|
|
|
|
|
|
static StringRef ToolName;
|
|
|
|
|
|
|
|
static void error(const Twine &Message) {
|
|
|
|
WithColor::error(errs(), ToolName) << Message << '\n';
|
|
|
|
exit(1);
|
2019-11-18 23:51:41 +01:00
|
|
|
}
|
|
|
|
|
2019-12-16 18:48:00 +01:00
|
|
|
static std::string demangle(const std::string &Mangled) {
|
2016-11-13 21:43:38 +01:00
|
|
|
int Status;
|
2019-12-16 18:48:00 +01:00
|
|
|
std::string Prefix;
|
2017-01-22 18:41:10 +01:00
|
|
|
|
2019-05-09 17:58:16 +02:00
|
|
|
const char *DecoratedStr = Mangled.c_str();
|
2021-07-09 19:10:45 +02:00
|
|
|
if (StripUnderscore)
|
2019-05-09 17:58:16 +02:00
|
|
|
if (DecoratedStr[0] == '_')
|
|
|
|
++DecoratedStr;
|
|
|
|
size_t DecoratedLength = strlen(DecoratedStr);
|
2017-01-22 18:41:10 +01:00
|
|
|
|
|
|
|
char *Undecorated = nullptr;
|
|
|
|
|
2019-05-09 17:58:16 +02:00
|
|
|
if (Types ||
|
|
|
|
((DecoratedLength >= 2 && strncmp(DecoratedStr, "_Z", 2) == 0) ||
|
|
|
|
(DecoratedLength >= 4 && strncmp(DecoratedStr, "___Z", 4) == 0)))
|
|
|
|
Undecorated = itaniumDemangle(DecoratedStr, nullptr, nullptr, &Status);
|
2017-01-22 18:41:10 +01:00
|
|
|
|
2017-03-22 22:15:19 +01:00
|
|
|
if (!Undecorated &&
|
2019-05-09 17:58:16 +02:00
|
|
|
(DecoratedLength > 6 && strncmp(DecoratedStr, "__imp_", 6) == 0)) {
|
2019-12-16 18:48:00 +01:00
|
|
|
Prefix = "import thunk for ";
|
2019-05-09 17:58:16 +02:00
|
|
|
Undecorated = itaniumDemangle(DecoratedStr + 6, nullptr, nullptr, &Status);
|
2017-03-22 22:15:19 +01:00
|
|
|
}
|
|
|
|
|
2021-05-04 01:41:30 +02:00
|
|
|
if (!Undecorated &&
|
|
|
|
(DecoratedLength >= 2 && strncmp(DecoratedStr, "_R", 2) == 0)) {
|
|
|
|
Undecorated = rustDemangle(DecoratedStr, nullptr, nullptr, &Status);
|
|
|
|
}
|
|
|
|
|
2019-12-16 18:48:00 +01:00
|
|
|
std::string Result(Undecorated ? Prefix + Undecorated : Mangled);
|
2017-01-22 18:41:10 +01:00
|
|
|
free(Undecorated);
|
2019-02-11 21:30:53 +01:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2019-02-27 22:39:11 +01:00
|
|
|
// Split 'Source' on any character that fails to pass 'IsLegalChar'. The
|
|
|
|
// returned vector consists of pairs where 'first' is the delimited word, and
|
|
|
|
// 'second' are the delimiters following that word.
|
|
|
|
static void SplitStringDelims(
|
|
|
|
StringRef Source,
|
|
|
|
SmallVectorImpl<std::pair<StringRef, StringRef>> &OutFragments,
|
|
|
|
function_ref<bool(char)> IsLegalChar) {
|
|
|
|
// The beginning of the input string.
|
|
|
|
const auto Head = Source.begin();
|
|
|
|
|
|
|
|
// Obtain any leading delimiters.
|
|
|
|
auto Start = std::find_if(Head, Source.end(), IsLegalChar);
|
|
|
|
if (Start != Head)
|
|
|
|
OutFragments.push_back({"", Source.slice(0, Start - Head)});
|
|
|
|
|
|
|
|
// Capture each word and the delimiters following that word.
|
|
|
|
while (Start != Source.end()) {
|
|
|
|
Start = std::find_if(Start, Source.end(), IsLegalChar);
|
|
|
|
auto End = std::find_if_not(Start, Source.end(), IsLegalChar);
|
|
|
|
auto DEnd = std::find_if(End, Source.end(), IsLegalChar);
|
|
|
|
OutFragments.push_back({Source.slice(Start - Head, End - Head),
|
|
|
|
Source.slice(End - Head, DEnd - Head)});
|
|
|
|
Start = DEnd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This returns true if 'C' is a character that can show up in an
|
|
|
|
// Itanium-mangled string.
|
|
|
|
static bool IsLegalItaniumChar(char C) {
|
|
|
|
// Itanium CXX ABI [External Names]p5.1.1:
|
|
|
|
// '$' and '.' in mangled names are reserved for private implementations.
|
|
|
|
return isalnum(C) || C == '.' || C == '$' || C == '_';
|
|
|
|
}
|
|
|
|
|
2019-02-11 21:30:53 +01:00
|
|
|
// If 'Split' is true, then 'Mangled' is broken into individual words and each
|
|
|
|
// word is demangled. Otherwise, the entire string is treated as a single
|
|
|
|
// mangled item. The result is output to 'OS'.
|
|
|
|
static void demangleLine(llvm::raw_ostream &OS, StringRef Mangled, bool Split) {
|
|
|
|
std::string Result;
|
|
|
|
if (Split) {
|
2019-02-27 22:39:11 +01:00
|
|
|
SmallVector<std::pair<StringRef, StringRef>, 16> Words;
|
|
|
|
SplitStringDelims(Mangled, Words, IsLegalItaniumChar);
|
|
|
|
for (const auto &Word : Words)
|
2020-01-28 20:23:46 +01:00
|
|
|
Result += ::demangle(std::string(Word.first)) + Word.second.str();
|
2019-02-11 21:30:53 +01:00
|
|
|
} else
|
2020-01-28 20:23:46 +01:00
|
|
|
Result = ::demangle(std::string(Mangled));
|
2019-02-11 21:30:53 +01:00
|
|
|
OS << Result << '\n';
|
|
|
|
OS.flush();
|
2016-11-13 21:43:38 +01:00
|
|
|
}
|
|
|
|
|
2016-09-06 21:16:48 +02:00
|
|
|
int main(int argc, char **argv) {
|
2018-04-13 20:26:06 +02:00
|
|
|
InitLLVM X(argc, argv);
|
2021-07-09 19:10:45 +02:00
|
|
|
BumpPtrAllocator A;
|
|
|
|
StringSaver Saver(A);
|
|
|
|
CxxfiltOptTable Tbl;
|
|
|
|
ToolName = argv[0];
|
|
|
|
opt::InputArgList Args = Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver,
|
|
|
|
[&](StringRef Msg) { error(Msg); });
|
|
|
|
if (Args.hasArg(OPT_help)) {
|
|
|
|
Tbl.printHelp(outs(),
|
|
|
|
(Twine(ToolName) + " [options] <mangled>").str().c_str(),
|
|
|
|
"LLVM symbol undecoration tool");
|
|
|
|
// TODO Replace this with OptTable API once it adds extrahelp support.
|
|
|
|
outs() << "\nPass @FILE as argument to read options from FILE.\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (Args.hasArg(OPT_version)) {
|
|
|
|
outs() << ToolName << '\n';
|
|
|
|
cl::PrintVersionMessage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The default value depends on the default triple. Mach-O has symbols
|
|
|
|
// prefixed with "_", so strip by default.
|
|
|
|
if (opt::Arg *A =
|
|
|
|
Args.getLastArg(OPT_strip_underscore, OPT_no_strip_underscore))
|
|
|
|
StripUnderscore = A->getOption().matches(OPT_strip_underscore);
|
|
|
|
else
|
|
|
|
StripUnderscore = Triple(sys::getProcessTriple()).isOSBinFormatMachO();
|
2017-01-20 06:27:09 +01:00
|
|
|
|
2021-07-09 19:10:45 +02:00
|
|
|
Types = Args.hasArg(OPT_types);
|
2017-01-20 05:25:26 +01:00
|
|
|
|
2021-07-09 19:10:45 +02:00
|
|
|
std::vector<std::string> Decorated = Args.getAllArgValues(OPT_INPUT);
|
2017-01-20 05:25:26 +01:00
|
|
|
if (Decorated.empty())
|
2016-11-13 21:43:38 +01:00
|
|
|
for (std::string Mangled; std::getline(std::cin, Mangled);)
|
2019-02-11 21:30:53 +01:00
|
|
|
demangleLine(llvm::outs(), Mangled, true);
|
2016-11-13 21:43:38 +01:00
|
|
|
else
|
2017-01-20 05:25:26 +01:00
|
|
|
for (const auto &Symbol : Decorated)
|
2019-02-11 21:30:53 +01:00
|
|
|
demangleLine(llvm::outs(), Symbol, false);
|
2016-11-13 21:43:38 +01:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2016-09-06 21:16:48 +02:00
|
|
|
}
|