1 //===-- llvm-c++filt.cpp --------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ADT/StringExtras.h" 10 #include "llvm/ADT/Triple.h" 11 #include "llvm/Demangle/Demangle.h" 12 #include "llvm/Option/Arg.h" 13 #include "llvm/Option/ArgList.h" 14 #include "llvm/Option/Option.h" 15 #include "llvm/Support/CommandLine.h" 16 #include "llvm/Support/Host.h" 17 #include "llvm/Support/InitLLVM.h" 18 #include "llvm/Support/WithColor.h" 19 #include "llvm/Support/raw_ostream.h" 20 #include <cstdlib> 21 #include <iostream> 22 23 using namespace llvm; 24 25 namespace { 26 enum ID { 27 OPT_INVALID = 0, // This is not an option ID. 28 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ 29 HELPTEXT, METAVAR, VALUES) \ 30 OPT_##ID, 31 #include "Opts.inc" 32 #undef OPTION 33 }; 34 35 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; 36 #include "Opts.inc" 37 #undef PREFIX 38 39 const opt::OptTable::Info InfoTable[] = { 40 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ 41 HELPTEXT, METAVAR, VALUES) \ 42 { \ 43 PREFIX, NAME, HELPTEXT, \ 44 METAVAR, OPT_##ID, opt::Option::KIND##Class, \ 45 PARAM, FLAGS, OPT_##GROUP, \ 46 OPT_##ALIAS, ALIASARGS, VALUES}, 47 #include "Opts.inc" 48 #undef OPTION 49 }; 50 51 class CxxfiltOptTable : public opt::OptTable { 52 public: 53 CxxfiltOptTable() : OptTable(InfoTable) { setGroupedShortOptions(true); } 54 }; 55 } // namespace 56 57 static bool StripUnderscore; 58 static bool Types; 59 60 static StringRef ToolName; 61 62 static void error(const Twine &Message) { 63 WithColor::error(errs(), ToolName) << Message << '\n'; 64 exit(1); 65 } 66 67 static std::string demangle(const std::string &Mangled) { 68 int Status; 69 std::string Prefix; 70 71 const char *DecoratedStr = Mangled.c_str(); 72 if (StripUnderscore) 73 if (DecoratedStr[0] == '_') 74 ++DecoratedStr; 75 size_t DecoratedLength = strlen(DecoratedStr); 76 77 char *Undecorated = nullptr; 78 79 if (Types || 80 ((DecoratedLength >= 2 && strncmp(DecoratedStr, "_Z", 2) == 0) || 81 (DecoratedLength >= 4 && strncmp(DecoratedStr, "___Z", 4) == 0))) 82 Undecorated = itaniumDemangle(DecoratedStr, nullptr, nullptr, &Status); 83 84 if (!Undecorated && 85 (DecoratedLength > 6 && strncmp(DecoratedStr, "__imp_", 6) == 0)) { 86 Prefix = "import thunk for "; 87 Undecorated = itaniumDemangle(DecoratedStr + 6, nullptr, nullptr, &Status); 88 } 89 90 if (!Undecorated && 91 (DecoratedLength >= 2 && strncmp(DecoratedStr, "_R", 2) == 0)) { 92 Undecorated = rustDemangle(DecoratedStr, nullptr, nullptr, &Status); 93 } 94 95 std::string Result(Undecorated ? Prefix + Undecorated : Mangled); 96 free(Undecorated); 97 return Result; 98 } 99 100 // Split 'Source' on any character that fails to pass 'IsLegalChar'. The 101 // returned vector consists of pairs where 'first' is the delimited word, and 102 // 'second' are the delimiters following that word. 103 static void SplitStringDelims( 104 StringRef Source, 105 SmallVectorImpl<std::pair<StringRef, StringRef>> &OutFragments, 106 function_ref<bool(char)> IsLegalChar) { 107 // The beginning of the input string. 108 const auto Head = Source.begin(); 109 110 // Obtain any leading delimiters. 111 auto Start = std::find_if(Head, Source.end(), IsLegalChar); 112 if (Start != Head) 113 OutFragments.push_back({"", Source.slice(0, Start - Head)}); 114 115 // Capture each word and the delimiters following that word. 116 while (Start != Source.end()) { 117 Start = std::find_if(Start, Source.end(), IsLegalChar); 118 auto End = std::find_if_not(Start, Source.end(), IsLegalChar); 119 auto DEnd = std::find_if(End, Source.end(), IsLegalChar); 120 OutFragments.push_back({Source.slice(Start - Head, End - Head), 121 Source.slice(End - Head, DEnd - Head)}); 122 Start = DEnd; 123 } 124 } 125 126 // This returns true if 'C' is a character that can show up in an 127 // Itanium-mangled string. 128 static bool IsLegalItaniumChar(char C) { 129 // Itanium CXX ABI [External Names]p5.1.1: 130 // '$' and '.' in mangled names are reserved for private implementations. 131 return isalnum(C) || C == '.' || C == '$' || C == '_'; 132 } 133 134 // If 'Split' is true, then 'Mangled' is broken into individual words and each 135 // word is demangled. Otherwise, the entire string is treated as a single 136 // mangled item. The result is output to 'OS'. 137 static void demangleLine(llvm::raw_ostream &OS, StringRef Mangled, bool Split) { 138 std::string Result; 139 if (Split) { 140 SmallVector<std::pair<StringRef, StringRef>, 16> Words; 141 SplitStringDelims(Mangled, Words, IsLegalItaniumChar); 142 for (const auto &Word : Words) 143 Result += ::demangle(std::string(Word.first)) + Word.second.str(); 144 } else 145 Result = ::demangle(std::string(Mangled)); 146 OS << Result << '\n'; 147 OS.flush(); 148 } 149 150 int main(int argc, char **argv) { 151 InitLLVM X(argc, argv); 152 BumpPtrAllocator A; 153 StringSaver Saver(A); 154 CxxfiltOptTable Tbl; 155 ToolName = argv[0]; 156 opt::InputArgList Args = Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, 157 [&](StringRef Msg) { error(Msg); }); 158 if (Args.hasArg(OPT_help)) { 159 Tbl.printHelp(outs(), 160 (Twine(ToolName) + " [options] <mangled>").str().c_str(), 161 "LLVM symbol undecoration tool"); 162 // TODO Replace this with OptTable API once it adds extrahelp support. 163 outs() << "\nPass @FILE as argument to read options from FILE.\n"; 164 return 0; 165 } 166 if (Args.hasArg(OPT_version)) { 167 outs() << ToolName << '\n'; 168 cl::PrintVersionMessage(); 169 return 0; 170 } 171 172 // The default value depends on the default triple. Mach-O has symbols 173 // prefixed with "_", so strip by default. 174 if (opt::Arg *A = 175 Args.getLastArg(OPT_strip_underscore, OPT_no_strip_underscore)) 176 StripUnderscore = A->getOption().matches(OPT_strip_underscore); 177 else 178 StripUnderscore = Triple(sys::getProcessTriple()).isOSBinFormatMachO(); 179 180 Types = Args.hasArg(OPT_types); 181 182 std::vector<std::string> Decorated = Args.getAllArgValues(OPT_INPUT); 183 if (Decorated.empty()) 184 for (std::string Mangled; std::getline(std::cin, Mangled);) 185 demangleLine(llvm::outs(), Mangled, true); 186 else 187 for (const auto &Symbol : Decorated) 188 demangleLine(llvm::outs(), Symbol, false); 189 190 return EXIT_SUCCESS; 191 } 192