2018-07-20 19:27:48 +02:00
|
|
|
//===-- llvm-undname.cpp - Microsoft ABI name undecorator
|
|
|
|
//------------------===//
|
|
|
|
//
|
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
|
2018-07-20 19:27:48 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This utility works like the windows undname utility. It converts mangled
|
|
|
|
// Microsoft symbol names into pretty C/C++ human-readable names.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Demangle/Demangle.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2019-04-16 14:51:40 +02:00
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2018-07-20 19:27:48 +02:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2019-04-16 14:51:40 +02:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2018-07-20 19:27:48 +02:00
|
|
|
#include "llvm/Support/Process.h"
|
2018-11-11 23:11:47 +01:00
|
|
|
#include "llvm/Support/WithColor.h"
|
2018-07-20 19:27:48 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2021-07-21 12:03:05 +02:00
|
|
|
cl::OptionCategory UndNameCategory("UndName Options");
|
|
|
|
|
2018-08-01 20:33:04 +02:00
|
|
|
cl::opt<bool> DumpBackReferences("backrefs", cl::Optional,
|
|
|
|
cl::desc("dump backreferences"), cl::Hidden,
|
2021-07-21 12:03:05 +02:00
|
|
|
cl::init(false), cl::cat(UndNameCategory));
|
2019-10-15 10:29:56 +02:00
|
|
|
cl::opt<bool> NoAccessSpecifier("no-access-specifier", cl::Optional,
|
|
|
|
cl::desc("skip access specifiers"), cl::Hidden,
|
2021-07-21 12:03:05 +02:00
|
|
|
cl::init(false), cl::cat(UndNameCategory));
|
2019-10-15 10:29:56 +02:00
|
|
|
cl::opt<bool> NoCallingConvention("no-calling-convention", cl::Optional,
|
|
|
|
cl::desc("skip calling convention"),
|
2021-07-21 12:03:05 +02:00
|
|
|
cl::Hidden, cl::init(false),
|
|
|
|
cl::cat(UndNameCategory));
|
2019-10-15 10:29:56 +02:00
|
|
|
cl::opt<bool> NoReturnType("no-return-type", cl::Optional,
|
|
|
|
cl::desc("skip return types"), cl::Hidden,
|
2021-07-21 12:03:05 +02:00
|
|
|
cl::init(false), cl::cat(UndNameCategory));
|
2019-10-15 10:29:56 +02:00
|
|
|
cl::opt<bool> NoMemberType("no-member-type", cl::Optional,
|
|
|
|
cl::desc("skip member types"), cl::Hidden,
|
2021-07-21 12:03:05 +02:00
|
|
|
cl::init(false), cl::cat(UndNameCategory));
|
2019-04-16 14:51:40 +02:00
|
|
|
cl::opt<std::string> RawFile("raw-file", cl::Optional,
|
2021-07-21 12:03:05 +02:00
|
|
|
cl::desc("for fuzzer data"), cl::Hidden,
|
|
|
|
cl::cat(UndNameCategory));
|
2020-05-19 01:57:30 +02:00
|
|
|
cl::opt<bool> WarnTrailing("warn-trailing", cl::Optional,
|
|
|
|
cl::desc("warn on trailing characters"), cl::Hidden,
|
2021-07-21 12:03:05 +02:00
|
|
|
cl::init(false), cl::cat(UndNameCategory));
|
2018-07-20 19:27:48 +02:00
|
|
|
cl::list<std::string> Symbols(cl::Positional, cl::desc("<input symbols>"),
|
2021-07-21 12:03:05 +02:00
|
|
|
cl::ZeroOrMore, cl::cat(UndNameCategory));
|
2018-07-20 19:27:48 +02:00
|
|
|
|
2019-04-16 14:51:40 +02:00
|
|
|
static bool msDemangle(const std::string &S) {
|
2018-07-20 19:27:48 +02:00
|
|
|
int Status;
|
2018-08-01 20:33:04 +02:00
|
|
|
MSDemangleFlags Flags = MSDF_None;
|
|
|
|
if (DumpBackReferences)
|
|
|
|
Flags = MSDemangleFlags(Flags | MSDF_DumpBackrefs);
|
2019-10-15 10:29:56 +02:00
|
|
|
if (NoAccessSpecifier)
|
|
|
|
Flags = MSDemangleFlags(Flags | MSDF_NoAccessSpecifier);
|
|
|
|
if (NoCallingConvention)
|
|
|
|
Flags = MSDemangleFlags(Flags | MSDF_NoCallingConvention);
|
|
|
|
if (NoReturnType)
|
|
|
|
Flags = MSDemangleFlags(Flags | MSDF_NoReturnType);
|
|
|
|
if (NoMemberType)
|
|
|
|
Flags = MSDemangleFlags(Flags | MSDF_NoMemberType);
|
2018-08-01 20:33:04 +02:00
|
|
|
|
2020-05-19 01:57:30 +02:00
|
|
|
size_t NRead;
|
2018-08-01 20:33:04 +02:00
|
|
|
char *ResultBuf =
|
2020-05-19 01:57:30 +02:00
|
|
|
microsoftDemangle(S.c_str(), &NRead, nullptr, nullptr, &Status, Flags);
|
2018-07-20 19:27:48 +02:00
|
|
|
if (Status == llvm::demangle_success) {
|
|
|
|
outs() << ResultBuf << "\n";
|
|
|
|
outs().flush();
|
2020-05-19 01:57:30 +02:00
|
|
|
if (WarnTrailing && NRead < S.size())
|
|
|
|
WithColor::warning() << "trailing characters: " << S.c_str() + NRead
|
|
|
|
<< "\n";
|
2018-07-20 19:27:48 +02:00
|
|
|
} else {
|
2018-11-11 23:11:47 +01:00
|
|
|
WithColor::error() << "Invalid mangled name\n";
|
2018-07-20 19:27:48 +02:00
|
|
|
}
|
|
|
|
std::free(ResultBuf);
|
2019-04-16 14:51:40 +02:00
|
|
|
return Status == llvm::demangle_success;
|
2018-07-20 22:48:36 +02:00
|
|
|
}
|
2018-07-20 19:27:48 +02:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
InitLLVM X(argc, argv);
|
|
|
|
|
2021-07-21 12:03:05 +02:00
|
|
|
cl::HideUnrelatedOptions({&UndNameCategory, &getColorCategory()});
|
2018-07-20 19:27:48 +02:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm-undname\n");
|
|
|
|
|
2019-04-16 14:51:40 +02:00
|
|
|
if (!RawFile.empty()) {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(RawFile);
|
|
|
|
if (std::error_code EC = FileOrErr.getError()) {
|
|
|
|
WithColor::error() << "Could not open input file \'" << RawFile
|
|
|
|
<< "\': " << EC.message() << '\n';
|
|
|
|
return 1;
|
|
|
|
}
|
2020-01-28 20:23:46 +01:00
|
|
|
return msDemangle(std::string(FileOrErr->get()->getBuffer())) ? 0 : 1;
|
2019-04-16 14:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Success = true;
|
2018-07-20 19:27:48 +02:00
|
|
|
if (Symbols.empty()) {
|
|
|
|
while (true) {
|
|
|
|
std::string LineStr;
|
|
|
|
std::getline(std::cin, LineStr);
|
|
|
|
if (std::cin.eof())
|
|
|
|
break;
|
|
|
|
|
|
|
|
StringRef Line(LineStr);
|
|
|
|
Line = Line.trim();
|
|
|
|
if (Line.empty() || Line.startswith("#") || Line.startswith(";"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If the user is manually typing in these decorated names, don't echo
|
|
|
|
// them to the terminal a second time. If they're coming from redirected
|
|
|
|
// input, however, then we should display the input line so that the
|
|
|
|
// mangled and demangled name can be easily correlated in the output.
|
2018-07-21 17:39:05 +02:00
|
|
|
if (!sys::Process::StandardInIsUserInput()) {
|
2018-07-20 19:27:48 +02:00
|
|
|
outs() << Line << "\n";
|
2018-07-21 17:39:05 +02:00
|
|
|
outs().flush();
|
|
|
|
}
|
2020-01-28 20:23:46 +01:00
|
|
|
if (!msDemangle(std::string(Line)))
|
2019-04-16 14:51:40 +02:00
|
|
|
Success = false;
|
2018-07-20 19:27:48 +02:00
|
|
|
outs() << "\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (StringRef S : Symbols) {
|
|
|
|
outs() << S << "\n";
|
2018-07-21 17:39:05 +02:00
|
|
|
outs().flush();
|
2020-01-28 20:23:46 +01:00
|
|
|
if (!msDemangle(std::string(S)))
|
2019-04-16 14:51:40 +02:00
|
|
|
Success = false;
|
2018-07-20 19:27:48 +02:00
|
|
|
outs() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 14:51:40 +02:00
|
|
|
return Success ? 0 : 1;
|
2018-07-20 22:48:36 +02:00
|
|
|
}
|