1//===-- llvm-undname.cpp - Microsoft ABI name undecorator
2//------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9//
10// This utility works like the windows undname utility. It converts mangled
11// Microsoft symbol names into pretty C/C++ human-readable names.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ADT/StringRef.h"
16#include "llvm/Demangle/Demangle.h"
17#include "llvm/Support/CommandLine.h"
18#include "llvm/Support/ErrorOr.h"
19#include "llvm/Support/InitLLVM.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/Process.h"
22#include "llvm/Support/WithColor.h"
23#include "llvm/Support/raw_ostream.h"
24#include <cstdio>
25#include <cstring>
26#include <iostream>
27#include <string>
28
29using namespace llvm;
30
31static cl::OptionCategory UndNameCategory("UndName Options");
32
33static cl::opt<bool> DumpBackReferences("backrefs",
34 cl::desc("dump backreferences"),
35 cl::Hidden, cl::init(Val: false),
36 cl::cat(UndNameCategory));
37static cl::opt<bool> NoAccessSpecifier("no-access-specifier",
38 cl::desc("skip access specifiers"),
39 cl::Hidden, cl::init(Val: false),
40 cl::cat(UndNameCategory));
41static cl::opt<bool> NoCallingConvention("no-calling-convention",
42 cl::desc("skip calling convention"),
43 cl::Hidden, cl::init(Val: false),
44 cl::cat(UndNameCategory));
45static cl::opt<bool> NoReturnType("no-return-type",
46 cl::desc("skip return types"), cl::Hidden,
47 cl::init(Val: false), cl::cat(UndNameCategory));
48static cl::opt<bool> NoMemberType("no-member-type",
49 cl::desc("skip member types"), cl::Hidden,
50 cl::init(Val: false), cl::cat(UndNameCategory));
51static cl::opt<bool> NoVariableType("no-variable-type",
52 cl::desc("skip variable types"), cl::Hidden,
53 cl::init(Val: false), cl::cat(UndNameCategory));
54static cl::opt<std::string> RawFile("raw-file", cl::desc("for fuzzer data"),
55 cl::Hidden, cl::cat(UndNameCategory));
56static cl::opt<bool> WarnTrailing("warn-trailing",
57 cl::desc("warn on trailing characters"),
58 cl::Hidden, cl::init(Val: false),
59 cl::cat(UndNameCategory));
60static cl::list<std::string> Symbols(cl::Positional,
61 cl::desc("<input symbols>"),
62 cl::cat(UndNameCategory));
63
64static bool msDemangle(const std::string &S) {
65 int Status;
66 MSDemangleFlags Flags = MSDF_None;
67 if (DumpBackReferences)
68 Flags = MSDemangleFlags(Flags | MSDF_DumpBackrefs);
69 if (NoAccessSpecifier)
70 Flags = MSDemangleFlags(Flags | MSDF_NoAccessSpecifier);
71 if (NoCallingConvention)
72 Flags = MSDemangleFlags(Flags | MSDF_NoCallingConvention);
73 if (NoReturnType)
74 Flags = MSDemangleFlags(Flags | MSDF_NoReturnType);
75 if (NoMemberType)
76 Flags = MSDemangleFlags(Flags | MSDF_NoMemberType);
77 if (NoVariableType)
78 Flags = MSDemangleFlags(Flags | MSDF_NoVariableType);
79
80 size_t NRead;
81 char *ResultBuf = microsoftDemangle(mangled_name: S, n_read: &NRead, status: &Status, Flags);
82 if (Status == llvm::demangle_success) {
83 outs() << ResultBuf << "\n";
84 outs().flush();
85 if (WarnTrailing && NRead < S.size())
86 WithColor::warning() << "trailing characters: " << S.c_str() + NRead
87 << "\n";
88 } else {
89 WithColor::error() << "Invalid mangled name\n";
90 }
91 std::free(ptr: ResultBuf);
92 return Status == llvm::demangle_success;
93}
94
95int main(int argc, char **argv) {
96 InitLLVM X(argc, argv);
97
98 cl::HideUnrelatedOptions(Categories: {&UndNameCategory, &getColorCategory()});
99 cl::ParseCommandLineOptions(argc, argv, Overview: "llvm-undname\n");
100
101 if (!RawFile.empty()) {
102 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
103 MemoryBuffer::getFileOrSTDIN(Filename: RawFile);
104 if (std::error_code EC = FileOrErr.getError()) {
105 WithColor::error() << "Could not open input file \'" << RawFile
106 << "\': " << EC.message() << '\n';
107 return 1;
108 }
109 return msDemangle(S: std::string(FileOrErr->get()->getBuffer())) ? 0 : 1;
110 }
111
112 bool Success = true;
113 if (Symbols.empty()) {
114 while (true) {
115 std::string LineStr;
116 std::getline(is&: std::cin, str&: LineStr);
117 if (std::cin.eof())
118 break;
119
120 StringRef Line(LineStr);
121 Line = Line.trim();
122 if (Line.empty() || Line.starts_with(Prefix: "#") || Line.starts_with(Prefix: ";"))
123 continue;
124
125 // If the user is manually typing in these decorated names, don't echo
126 // them to the terminal a second time. If they're coming from redirected
127 // input, however, then we should display the input line so that the
128 // mangled and demangled name can be easily correlated in the output.
129 if (!sys::Process::StandardInIsUserInput()) {
130 outs() << Line << "\n";
131 outs().flush();
132 }
133 if (!msDemangle(S: std::string(Line)))
134 Success = false;
135 outs() << "\n";
136 }
137 } else {
138 for (StringRef S : Symbols) {
139 outs() << S << "\n";
140 outs().flush();
141 if (!msDemangle(S: std::string(S)))
142 Success = false;
143 outs() << "\n";
144 }
145 }
146
147 return Success ? 0 : 1;
148}
149