1 //===-- llvm-tli-checker.cpp - Compare TargetLibraryInfo to SDK libraries -===// 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/SmallString.h" 10 #include "llvm/ADT/StringMap.h" 11 #include "llvm/ADT/Triple.h" 12 #include "llvm/Analysis/TargetLibraryInfo.h" 13 #include "llvm/Config/llvm-config.h" 14 #include "llvm/Demangle/Demangle.h" 15 #include "llvm/Object/Archive.h" 16 #include "llvm/Object/ELFObjectFile.h" 17 #include "llvm/Option/ArgList.h" 18 #include "llvm/Option/Option.h" 19 #include "llvm/Support/FileSystem.h" 20 #include "llvm/Support/InitLLVM.h" 21 #include "llvm/Support/Path.h" 22 #include "llvm/Support/WithColor.h" 23 24 using namespace llvm; 25 using namespace llvm::object; 26 27 // Command-line option boilerplate. 28 namespace { 29 enum ID { 30 OPT_INVALID = 0, // This is not an option ID. 31 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ 32 HELPTEXT, METAVAR, VALUES) \ 33 OPT_##ID, 34 #include "Opts.inc" 35 #undef OPTION 36 }; 37 38 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; 39 #include "Opts.inc" 40 #undef PREFIX 41 42 static const opt::OptTable::Info InfoTable[] = { 43 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ 44 HELPTEXT, METAVAR, VALUES) \ 45 { \ 46 PREFIX, NAME, HELPTEXT, \ 47 METAVAR, OPT_##ID, opt::Option::KIND##Class, \ 48 PARAM, FLAGS, OPT_##GROUP, \ 49 OPT_##ALIAS, ALIASARGS, VALUES}, 50 #include "Opts.inc" 51 #undef OPTION 52 }; 53 54 class TLICheckerOptTable : public opt::OptTable { 55 public: 56 TLICheckerOptTable() : OptTable(InfoTable) {} 57 }; 58 } // end anonymous namespace 59 60 // We have three levels of reporting. 61 enum class ReportKind { 62 Error, // For argument parsing errors. 63 Summary, // Report counts but not details. 64 Discrepancy, // Report where TLI and the library differ. 65 Full // Report for every known-to-TLI function. 66 }; 67 68 // Most of the ObjectFile interfaces return an Expected<T>, so make it easy 69 // to ignore errors. 70 template <typename T> 71 static T unwrapIgnoreError(Expected<T> E, T Default = T()) { 72 if (E) 73 return std::move(*E); 74 // Sink the error and return a nothing value. 75 consumeError(E.takeError()); 76 return Default; 77 } 78 79 static void fail(const Twine &Message) { 80 WithColor::error() << Message << '\n'; 81 exit(EXIT_FAILURE); 82 } 83 84 // Some problem occurred with an archive member; complain and continue. 85 static void reportArchiveChildIssue(const object::Archive::Child &C, int Index, 86 StringRef ArchiveFilename) { 87 // First get the member name. 88 std::string ChildName; 89 Expected<StringRef> NameOrErr = C.getName(); 90 if (NameOrErr) 91 ChildName = std::string(NameOrErr.get()); 92 else { 93 // Ignore the name-fetch error, just report the index. 94 consumeError(NameOrErr.takeError()); 95 ChildName = "<file index: " + std::to_string(Index) + ">"; 96 } 97 98 WithColor::warning() << ArchiveFilename << "(" << ChildName 99 << "): member is not usable\n"; 100 } 101 102 // Return Name, and if Name is mangled, append "aka" and the demangled name. 103 static std::string getPrintableName(StringRef Name) { 104 std::string OutputName = "'"; 105 OutputName += Name; 106 OutputName += "'"; 107 std::string DemangledName(demangle(Name.str())); 108 if (Name != DemangledName) { 109 OutputName += " aka "; 110 OutputName += DemangledName; 111 } 112 return OutputName; 113 } 114 115 // Store all the names that TargetLibraryInfo knows about; the bool indicates 116 // whether TLI has it marked as "available" for the target of interest. 117 // This is a vector to preserve the sorted order for better reporting. 118 struct TLINameList : std::vector<std::pair<StringRef, bool>> { 119 // Record all the TLI info in the vector. 120 void initialize(StringRef TargetTriple); 121 // Print out what we found. 122 void dump(); 123 }; 124 static TLINameList TLINames; 125 126 void TLINameList::initialize(StringRef TargetTriple) { 127 Triple T(TargetTriple); 128 TargetLibraryInfoImpl TLII(T); 129 TargetLibraryInfo TLI(TLII); 130 131 reserve(LibFunc::NumLibFuncs); 132 size_t NumAvailable = 0; 133 for (unsigned FI = 0; FI != LibFunc::NumLibFuncs; ++FI) { 134 LibFunc LF = (LibFunc)FI; 135 bool Available = TLI.has(LF); 136 // getName returns names only for available funcs. 137 TLII.setAvailable(LF); 138 emplace_back(TLI.getName(LF), Available); 139 if (Available) 140 ++NumAvailable; 141 } 142 outs() << "TLI knows " << LibFunc::NumLibFuncs << " symbols, " << NumAvailable 143 << " available for '" << TargetTriple << "'\n"; 144 } 145 146 void TLINameList::dump() { 147 // Assume this gets called after initialize(), so we have the above line of 148 // output as a header. So, for example, no need to repeat the triple. 149 for (auto &TLIName : TLINames) { 150 outs() << (TLIName.second ? " " : "not ") 151 << "available: " << getPrintableName(TLIName.first) << '\n'; 152 } 153 } 154 155 // Store all the exported symbol names we found in the input libraries. 156 // We use a map to get hashed lookup speed; the bool is meaningless. 157 class SDKNameMap : public StringMap<bool> { 158 void populateFromObject(ObjectFile *O); 159 void populateFromArchive(Archive *A); 160 161 public: 162 void populateFromFile(StringRef LibDir, StringRef LibName); 163 }; 164 static SDKNameMap SDKNames; 165 166 // Given an ObjectFile, extract the global function symbols. 167 void SDKNameMap::populateFromObject(ObjectFile *O) { 168 // FIXME: Support other formats. 169 if (!O->isELF()) { 170 WithColor::warning() << O->getFileName() 171 << ": only ELF-format files are supported\n"; 172 return; 173 } 174 const auto *ELF = cast<ELFObjectFileBase>(O); 175 176 for (auto &S : ELF->getDynamicSymbolIterators()) { 177 // We want only defined global function symbols. 178 SymbolRef::Type Type = unwrapIgnoreError(S.getType()); 179 uint32_t Flags = unwrapIgnoreError(S.getFlags()); 180 section_iterator Section = unwrapIgnoreError(S.getSection(), 181 /*Default=*/O->section_end()); 182 StringRef Name = unwrapIgnoreError(S.getName()); 183 if (Type == SymbolRef::ST_Function && (Flags & SymbolRef::SF_Global) && 184 Section != O->section_end()) 185 insert({Name, true}); 186 } 187 } 188 189 // Unpack an archive and populate from the component object files. 190 // This roughly imitates dumpArchive() from llvm-objdump.cpp. 191 void SDKNameMap::populateFromArchive(Archive *A) { 192 Error Err = Error::success(); 193 int Index = -1; 194 for (auto &C : A->children(Err)) { 195 ++Index; 196 Expected<std::unique_ptr<object::Binary>> ChildOrErr = C.getAsBinary(); 197 if (!ChildOrErr) { 198 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) { 199 // Issue a generic warning. 200 consumeError(std::move(E)); 201 reportArchiveChildIssue(C, Index, A->getFileName()); 202 } 203 continue; 204 } 205 if (ObjectFile *O = dyn_cast<ObjectFile>(&*ChildOrErr.get())) 206 populateFromObject(O); 207 // Ignore non-object archive members. 208 } 209 if (Err) 210 WithColor::defaultErrorHandler(std::move(Err)); 211 } 212 213 // Unpack a library file and extract the global function names. 214 void SDKNameMap::populateFromFile(StringRef LibDir, StringRef LibName) { 215 // Pick an arbitrary but reasonable default size. 216 SmallString<255> Filepath(LibDir); 217 sys::path::append(Filepath, LibName); 218 if (!sys::fs::exists(Filepath)) { 219 WithColor::warning() << StringRef(Filepath) << ": not found\n"; 220 return; 221 } 222 outs() << "\nLooking for symbols in '" << StringRef(Filepath) << "'\n"; 223 auto ExpectedBinary = createBinary(Filepath); 224 if (!ExpectedBinary) { 225 // FIXME: Report this better. 226 WithColor::defaultWarningHandler(ExpectedBinary.takeError()); 227 return; 228 } 229 OwningBinary<Binary> OBinary = std::move(*ExpectedBinary); 230 Binary &Binary = *OBinary.getBinary(); 231 size_t Precount = size(); 232 if (Archive *A = dyn_cast<Archive>(&Binary)) 233 populateFromArchive(A); 234 else if (ObjectFile *O = dyn_cast<ObjectFile>(&Binary)) 235 populateFromObject(O); 236 else { 237 WithColor::warning() << StringRef(Filepath) 238 << ": not an archive or object file\n"; 239 return; 240 } 241 if (Precount == size()) 242 WithColor::warning() << StringRef(Filepath) << ": no symbols found\n"; 243 else 244 outs() << "Found " << size() - Precount << " global function symbols in '" 245 << StringRef(Filepath) << "'\n"; 246 } 247 248 int main(int argc, char *argv[]) { 249 InitLLVM X(argc, argv); 250 BumpPtrAllocator A; 251 StringSaver Saver(A); 252 TLICheckerOptTable Tbl; 253 opt::InputArgList Args = Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, 254 [&](StringRef Msg) { fail(Msg); }); 255 256 if (Args.hasArg(OPT_help)) { 257 std::string Usage(argv[0]); 258 Usage += " [options] library-file [library-file...]"; 259 Tbl.printHelp(outs(), Usage.c_str(), 260 "LLVM TargetLibraryInfo versus SDK checker"); 261 outs() << "\nPass @FILE as argument to read options or library names from " 262 "FILE.\n"; 263 return 0; 264 } 265 266 TLINames.initialize(Args.getLastArgValue(OPT_triple_EQ)); 267 268 // --dump-tli doesn't require any input files. 269 if (Args.hasArg(OPT_dump_tli)) { 270 TLINames.dump(); 271 return 0; 272 } 273 274 std::vector<std::string> LibList = Args.getAllArgValues(OPT_INPUT); 275 if (LibList.empty()) 276 fail("no input files\n"); 277 StringRef LibDir = Args.getLastArgValue(OPT_libdir_EQ); 278 bool SeparateMode = Args.hasArg(OPT_separate); 279 280 ReportKind ReportLevel = 281 SeparateMode ? ReportKind::Summary : ReportKind::Discrepancy; 282 if (const opt::Arg *A = Args.getLastArg(OPT_report_EQ)) { 283 ReportLevel = StringSwitch<ReportKind>(A->getValue()) 284 .Case("summary", ReportKind::Summary) 285 .Case("discrepancy", ReportKind::Discrepancy) 286 .Case("full", ReportKind::Full) 287 .Default(ReportKind::Error); 288 if (ReportLevel == ReportKind::Error) 289 fail(Twine("invalid option for --report: ", StringRef(A->getValue()))); 290 } 291 292 for (size_t I = 0; I < LibList.size(); ++I) { 293 // In SeparateMode we report on input libraries individually; otherwise 294 // we do one big combined search. Reading to the end of LibList here 295 // will cause the outer while loop to terminate cleanly. 296 if (SeparateMode) { 297 SDKNames.clear(); 298 SDKNames.populateFromFile(LibDir, LibList[I]); 299 if (SDKNames.empty()) 300 continue; 301 } else { 302 do 303 SDKNames.populateFromFile(LibDir, LibList[I]); 304 while (++I < LibList.size()); 305 if (SDKNames.empty()) { 306 WithColor::error() << "NO symbols found!\n"; 307 break; 308 } 309 outs() << "Found a grand total of " << SDKNames.size() 310 << " library symbols\n"; 311 } 312 unsigned TLIdoesSDKdoesnt = 0; 313 unsigned TLIdoesntSDKdoes = 0; 314 unsigned TLIandSDKboth = 0; 315 unsigned TLIandSDKneither = 0; 316 for (auto &TLIName : TLINames) { 317 bool TLIHas = TLIName.second; 318 bool SDKHas = SDKNames.count(TLIName.first) == 1; 319 int Which = int(TLIHas) * 2 + int(SDKHas); 320 switch (Which) { 321 case 0: ++TLIandSDKneither; break; 322 case 1: ++TLIdoesntSDKdoes; break; 323 case 2: ++TLIdoesSDKdoesnt; break; 324 case 3: ++TLIandSDKboth; break; 325 } 326 // If the results match, report only if user requested a full report. 327 ReportKind Threshold = 328 TLIHas == SDKHas ? ReportKind::Full : ReportKind::Discrepancy; 329 if (Threshold <= ReportLevel) { 330 constexpr char YesNo[2][4] = {"no ", "yes"}; 331 constexpr char Indicator[4][3] = {"!!", ">>", "<<", "=="}; 332 outs() << Indicator[Which] << " TLI " << YesNo[TLIHas] << " SDK " 333 << YesNo[SDKHas] << ": " << getPrintableName(TLIName.first) 334 << '\n'; 335 } 336 } 337 338 assert(TLIandSDKboth + TLIandSDKneither + TLIdoesSDKdoesnt + 339 TLIdoesntSDKdoes == 340 LibFunc::NumLibFuncs); 341 outs() << "<< Total TLI yes SDK no: " << TLIdoesSDKdoesnt 342 << "\n>> Total TLI no SDK yes: " << TLIdoesntSDKdoes 343 << "\n== Total TLI yes SDK yes: " << TLIandSDKboth; 344 if (TLIandSDKboth == 0) { 345 outs() << " *** NO TLI SYMBOLS FOUND"; 346 if (SeparateMode) 347 outs() << " in '" << LibList[I] << "'"; 348 } 349 outs() << '\n'; 350 351 if (!SeparateMode) { 352 if (TLIdoesSDKdoesnt == 0 && TLIdoesntSDKdoes == 0) 353 outs() << "PASS: LLVM TLI matched SDK libraries successfully.\n"; 354 else 355 outs() << "FAIL: LLVM TLI doesn't match SDK libraries.\n"; 356 } 357 } 358 } 359