xref: /llvm-project/llvm/tools/llvm-readtapi/llvm-readtapi.cpp (revision 5bcd91058ee4855804780c4ae35ac87ed45a4b58)
1 //===-- llvm-readtapi.cpp - tapi file reader and transformer -----*- C++-*-===//
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 // This file defines the command-line driver for llvm-readtapi.
10 //
11 //===----------------------------------------------------------------------===//
12 #include "DiffEngine.h"
13 #include "llvm/BinaryFormat/Magic.h"
14 #include "llvm/Option/Arg.h"
15 #include "llvm/Option/ArgList.h"
16 #include "llvm/Option/Option.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/Error.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/InitLLVM.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/TextAPI/DylibReader.h"
25 #include "llvm/TextAPI/TextAPIError.h"
26 #include "llvm/TextAPI/TextAPIReader.h"
27 #include "llvm/TextAPI/TextAPIWriter.h"
28 #include "llvm/TextAPI/Utils.h"
29 #include <cstdlib>
30 
31 #if !defined(_MSC_VER) && !defined(__MINGW32__)
32 #include <unistd.h>
33 #endif
34 
35 using namespace llvm;
36 using namespace MachO;
37 using namespace object;
38 
39 namespace {
40 using namespace llvm::opt;
41 enum ID {
42   OPT_INVALID = 0, // This is not an option ID.
43 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
44 #include "TapiOpts.inc"
45 #undef OPTION
46 };
47 
48 #define PREFIX(NAME, VALUE)                                                    \
49   static constexpr StringLiteral NAME##_init[] = VALUE;                        \
50   static constexpr ArrayRef<StringLiteral> NAME(NAME##_init,                   \
51                                                 std::size(NAME##_init) - 1);
52 #include "TapiOpts.inc"
53 #undef PREFIX
54 
55 static constexpr opt::OptTable::Info InfoTable[] = {
56 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
57 #include "TapiOpts.inc"
58 #undef OPTION
59 };
60 
61 class TAPIOptTable : public opt::GenericOptTable {
62 public:
63   TAPIOptTable() : opt::GenericOptTable(InfoTable) {
64     setGroupedShortOptions(true);
65   }
66 };
67 
68 struct StubOptions {
69   bool DeleteInput = false;
70   bool DeletePrivate = false;
71   bool TraceLibs = false;
72 };
73 
74 struct Context {
75   std::vector<std::string> Inputs;
76   StubOptions StubOpt;
77   std::unique_ptr<llvm::raw_fd_stream> OutStream;
78   FileType WriteFT = FileType::TBD_V5;
79   bool Compact = false;
80   Architecture Arch = AK_unknown;
81 };
82 
83 // Use unique exit code to differentiate failures not directly caused from
84 // TextAPI operations. This is used for wrapping `compare` operations in
85 // automation and scripting.
86 const int NON_TAPI_EXIT_CODE = 2;
87 const std::string TOOLNAME = "llvm-readtapi";
88 ExitOnError ExitOnErr;
89 } // anonymous namespace
90 
91 // Handle error reporting in cases where `ExitOnError` is not used.
92 static void reportError(Twine Message, int ExitCode = EXIT_FAILURE) {
93   errs() << TOOLNAME << ": error: " << Message << "\n";
94   errs().flush();
95   exit(ExitCode);
96 }
97 
98 // Handle warnings.
99 static void reportWarning(Twine Message) {
100   errs() << TOOLNAME << ": warning: " << Message << "\n";
101 }
102 
103 /// Get what the symlink points to.
104 /// This is a no-op on windows as it references POSIX level apis.
105 static void read_link(const Twine &Path, SmallVectorImpl<char> &Output) {
106 #if !defined(_MSC_VER) && !defined(__MINGW32__)
107   Output.clear();
108   if (Path.isTriviallyEmpty())
109     return;
110 
111   SmallString<PATH_MAX> Storage;
112   auto P = Path.toNullTerminatedStringRef(Storage);
113   SmallString<PATH_MAX> Result;
114   ssize_t Len;
115   if ((Len = ::readlink(P.data(), Result.data(), PATH_MAX)) == -1)
116     reportError("unable to read symlink: " + Path);
117   Result.resize_for_overwrite(Len);
118   Output.swap(Result);
119 #else
120   reportError("unable to read symlink on windows: " + Path);
121 #endif
122 }
123 
124 static std::unique_ptr<InterfaceFile>
125 getInterfaceFile(const StringRef Filename, bool ResetBanner = true) {
126   ExitOnErr.setBanner(TOOLNAME + ": error: '" + Filename.str() + "' ");
127   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
128       MemoryBuffer::getFile(Filename);
129   if (BufferOrErr.getError())
130     ExitOnErr(errorCodeToError(BufferOrErr.getError()));
131   auto Buffer = std::move(*BufferOrErr);
132 
133   std::unique_ptr<InterfaceFile> IF;
134   switch (identify_magic(Buffer->getBuffer())) {
135   case file_magic::macho_dynamically_linked_shared_lib:
136     LLVM_FALLTHROUGH;
137   case file_magic::macho_dynamically_linked_shared_lib_stub:
138     LLVM_FALLTHROUGH;
139   case file_magic::macho_universal_binary:
140     IF = ExitOnErr(DylibReader::get(Buffer->getMemBufferRef()));
141     break;
142   case file_magic::tapi_file:
143     IF = ExitOnErr(TextAPIReader::get(Buffer->getMemBufferRef()));
144     break;
145   default:
146     reportError(Filename + ": unsupported file type");
147   }
148 
149   if (ResetBanner)
150     ExitOnErr.setBanner(TOOLNAME + ": error: ");
151   return IF;
152 }
153 
154 static bool handleCompareAction(const Context &Ctx) {
155   if (Ctx.Inputs.size() != 2)
156     reportError("compare only supports two input files",
157                 /*ExitCode=*/NON_TAPI_EXIT_CODE);
158 
159   // Override default exit code.
160   ExitOnErr = ExitOnError(TOOLNAME + ": error: ",
161                           /*DefaultErrorExitCode=*/NON_TAPI_EXIT_CODE);
162   auto LeftIF = getInterfaceFile(Ctx.Inputs.front());
163   auto RightIF = getInterfaceFile(Ctx.Inputs.at(1));
164 
165   raw_ostream &OS = Ctx.OutStream ? *Ctx.OutStream : outs();
166   return DiffEngine(LeftIF.get(), RightIF.get()).compareFiles(OS);
167 }
168 
169 static bool handleWriteAction(const Context &Ctx,
170                               std::unique_ptr<InterfaceFile> Out = nullptr) {
171   if (!Out) {
172     if (Ctx.Inputs.size() != 1)
173       reportError("write only supports one input file");
174     Out = getInterfaceFile(Ctx.Inputs.front());
175   }
176   raw_ostream &OS = Ctx.OutStream ? *Ctx.OutStream : outs();
177   ExitOnErr(TextAPIWriter::writeToStream(OS, *Out, Ctx.WriteFT, Ctx.Compact));
178   return EXIT_SUCCESS;
179 }
180 
181 static bool handleMergeAction(const Context &Ctx) {
182   if (Ctx.Inputs.size() < 2)
183     reportError("merge requires at least two input files");
184 
185   std::unique_ptr<InterfaceFile> Out;
186   for (StringRef FileName : Ctx.Inputs) {
187     auto IF = getInterfaceFile(FileName);
188     // On the first iteration copy the input file and skip merge.
189     if (!Out) {
190       Out = std::move(IF);
191       continue;
192     }
193     Out = ExitOnErr(Out->merge(IF.get()));
194   }
195   return handleWriteAction(Ctx, std::move(Out));
196 }
197 
198 static void stubifyImpl(std::unique_ptr<InterfaceFile> IF, Context &Ctx) {
199   // TODO: Add inlining and magic merge support.
200   if (Ctx.OutStream == nullptr) {
201     std::error_code EC;
202     SmallString<PATH_MAX> OutputLoc = IF->getPath();
203     replace_extension(OutputLoc, ".tbd");
204     Ctx.OutStream = std::make_unique<llvm::raw_fd_stream>(OutputLoc, EC);
205     if (EC)
206       reportError("opening file '" + OutputLoc + ": " + EC.message());
207   }
208 
209   handleWriteAction(Ctx, std::move(IF));
210   // Clear out output stream after file has been written incase more files are
211   // stubifed.
212   Ctx.OutStream = nullptr;
213 }
214 
215 static void stubifyDirectory(const StringRef InputPath, Context &Ctx) {
216   assert(InputPath.back() != '/' && "Unexpected / at end of input path.");
217   StringMap<std::vector<SymLink>> SymLinks;
218   StringMap<std::unique_ptr<InterfaceFile>> Dylibs;
219   StringMap<std::string> OriginalNames;
220   std::set<std::pair<std::string, bool>> LibsToDelete;
221 
222   std::error_code EC;
223   for (sys::fs::recursive_directory_iterator IT(InputPath, EC), IE; IT != IE;
224        IT.increment(EC)) {
225     if (EC == std::errc::no_such_file_or_directory) {
226       reportWarning(IT->path() + ": " + EC.message());
227       continue;
228     }
229     if (EC)
230       reportError(IT->path() + ": " + EC.message());
231 
232     // Skip header directories (include/Headers/PrivateHeaders) and module
233     // files.
234     StringRef Path = IT->path();
235     if (Path.ends_with("/include") || Path.ends_with("/Headers") ||
236         Path.ends_with("/PrivateHeaders") || Path.ends_with("/Modules") ||
237         Path.ends_with(".map") || Path.ends_with(".modulemap")) {
238       IT.no_push();
239       continue;
240     }
241 
242     // Check if the entry is a symlink. We don't follow symlinks but we record
243     // their content.
244     bool IsSymLink;
245     if (auto EC = sys::fs::is_symlink_file(Path, IsSymLink))
246       reportError(Path + ": " + EC.message());
247 
248     if (IsSymLink) {
249       IT.no_push();
250 
251       bool ShouldSkip;
252       auto SymLinkEC = shouldSkipSymLink(Path, ShouldSkip);
253 
254       // If symlink is broken, for some reason, we should continue
255       // trying to repair it before quitting.
256       if (!SymLinkEC && ShouldSkip)
257         continue;
258 
259       if (Ctx.StubOpt.DeletePrivate &&
260           isPrivateLibrary(Path.drop_front(InputPath.size()), true)) {
261         LibsToDelete.emplace(Path, false);
262         continue;
263       }
264 
265       SmallString<PATH_MAX> SymPath;
266       read_link(Path, SymPath);
267       // Sometimes there are broken symlinks that are absolute paths, which are
268       // invalid during build time, but would be correct during runtime. In the
269       // case of an absolute path we should check first if the path exists with
270       // the known locations as prefix.
271       SmallString<PATH_MAX> LinkSrc = Path;
272       SmallString<PATH_MAX> LinkTarget;
273       if (sys::path::is_absolute(SymPath)) {
274         LinkTarget = InputPath;
275         sys::path::append(LinkTarget, SymPath);
276 
277         // TODO: Investigate supporting a file manager for file system accesses.
278         if (sys::fs::exists(LinkTarget)) {
279           // Convert the absolute path to an relative path.
280           if (auto ec = MachO::make_relative(LinkSrc, LinkTarget, SymPath))
281             reportError(LinkTarget + ": " + EC.message());
282         } else if (!sys::fs::exists(SymPath)) {
283           reportWarning("ignoring broken symlink: " + Path);
284           continue;
285         } else {
286           LinkTarget = SymPath;
287         }
288       } else {
289         LinkTarget = LinkSrc;
290         sys::path::remove_filename(LinkTarget);
291         sys::path::append(LinkTarget, SymPath);
292       }
293 
294       // For Apple SDKs, the symlink src is guaranteed to be a canonical path
295       // because we don't follow symlinks when scanning. The symlink target is
296       // constructed from the symlink path and needs to be canonicalized.
297       if (auto ec = sys::fs::real_path(Twine(LinkTarget), LinkTarget)) {
298         reportWarning(LinkTarget + ": " + ec.message());
299         continue;
300       }
301 
302       auto itr = SymLinks.insert({LinkTarget.c_str(), std::vector<SymLink>()});
303       itr.first->second.emplace_back(LinkSrc.str(), std::string(SymPath.str()));
304 
305       continue;
306     }
307 
308     bool IsDirectory = false;
309     if (auto EC = sys::fs::is_directory(Path, IsDirectory))
310       reportError(Path + ": " + EC.message());
311     if (IsDirectory)
312       continue;
313 
314     if (Ctx.StubOpt.DeletePrivate &&
315         isPrivateLibrary(Path.drop_front(InputPath.size()))) {
316       IT.no_push();
317       LibsToDelete.emplace(Path, false);
318       continue;
319     }
320     auto IF = getInterfaceFile(Path);
321     if (Ctx.StubOpt.TraceLibs)
322       errs() << Path << "\n";
323 
324     // Normalize path for map lookup by removing the extension.
325     SmallString<PATH_MAX> NormalizedPath(Path);
326     replace_extension(NormalizedPath, "");
327 
328     if ((IF->getFileType() == FileType::MachO_DynamicLibrary) ||
329         (IF->getFileType() == FileType::MachO_DynamicLibrary_Stub)) {
330       OriginalNames[NormalizedPath.c_str()] = IF->getPath();
331 
332       // Don't add this MachO dynamic library because we already have a
333       // text-based stub recorded for this path.
334       if (Dylibs.count(NormalizedPath.c_str()))
335         continue;
336     }
337 
338     Dylibs[NormalizedPath.c_str()] = std::move(IF);
339   }
340 
341   for (auto &Lib : Dylibs) {
342     auto &Dylib = Lib.second;
343     // Get the original file name.
344     SmallString<PATH_MAX> NormalizedPath(Dylib->getPath());
345     stubifyImpl(std::move(Dylib), Ctx);
346 
347     replace_extension(NormalizedPath, "");
348     auto Found = OriginalNames.find(NormalizedPath.c_str());
349     if (Found == OriginalNames.end())
350       continue;
351 
352     if (Ctx.StubOpt.DeleteInput)
353       LibsToDelete.emplace(Found->second, true);
354 
355     // Don't allow for more than 20 levels of symlinks when searching for
356     // libraries to stubify.
357     StringRef LibToCheck = Found->second;
358     for (int i = 0; i < 20; ++i) {
359       auto LinkIt = SymLinks.find(LibToCheck.str());
360       if (LinkIt != SymLinks.end()) {
361         for (auto &SymInfo : LinkIt->second) {
362           SmallString<PATH_MAX> LinkSrc(SymInfo.SrcPath);
363           SmallString<PATH_MAX> LinkTarget(SymInfo.LinkContent);
364           replace_extension(LinkSrc, "tbd");
365           replace_extension(LinkTarget, "tbd");
366 
367           if (auto EC = sys::fs::remove(LinkSrc))
368             reportError(LinkSrc + " : " + EC.message());
369 
370           if (auto EC = sys::fs::create_link(LinkTarget, LinkSrc))
371             reportError(LinkTarget + " : " + EC.message());
372 
373           if (Ctx.StubOpt.DeleteInput)
374             LibsToDelete.emplace(SymInfo.SrcPath, true);
375 
376           LibToCheck = SymInfo.SrcPath;
377         }
378       } else
379         break;
380     }
381   }
382 
383   // Recursively delete the directories. This will abort when they are not empty
384   // or we reach the root of the SDK.
385   for (const auto &[LibPath, IsInput] : LibsToDelete) {
386     if (!IsInput && SymLinks.count(LibPath))
387       continue;
388 
389     if (auto EC = sys::fs::remove(LibPath))
390       reportError(LibPath + " : " + EC.message());
391 
392     std::error_code EC;
393     auto Dir = sys::path::parent_path(LibPath);
394     do {
395       EC = sys::fs::remove(Dir);
396       Dir = sys::path::parent_path(Dir);
397       if (!Dir.starts_with(InputPath))
398         break;
399     } while (!EC);
400   }
401 }
402 
403 static bool handleStubifyAction(Context &Ctx) {
404   if (Ctx.Inputs.empty())
405     reportError("stubify requires at least one input file");
406 
407   if ((Ctx.Inputs.size() > 1) && (Ctx.OutStream != nullptr))
408     reportError("cannot write multiple inputs into single output file");
409 
410   for (StringRef PathName : Ctx.Inputs) {
411     bool IsDirectory = false;
412     if (auto EC = sys::fs::is_directory(PathName, IsDirectory))
413       reportError(PathName + ": " + EC.message());
414 
415     if (IsDirectory) {
416       if (Ctx.OutStream != nullptr)
417         reportError("cannot stubify directory'" + PathName +
418                     "' into single output file");
419       stubifyDirectory(PathName, Ctx);
420       continue;
421     }
422 
423     stubifyImpl(getInterfaceFile(PathName), Ctx);
424     if (Ctx.StubOpt.DeleteInput)
425       if (auto ec = sys::fs::remove(PathName))
426         reportError("deleting file '" + PathName + ": " + ec.message());
427   }
428   return EXIT_SUCCESS;
429 }
430 
431 using IFOperation =
432     std::function<llvm::Expected<std::unique_ptr<InterfaceFile>>(
433         const llvm::MachO::InterfaceFile &, Architecture)>;
434 static bool handleSingleFileAction(const Context &Ctx, const StringRef Action,
435                                    IFOperation act) {
436   if (Ctx.Inputs.size() != 1)
437     reportError(Action + " only supports one input file");
438   if (Ctx.Arch == AK_unknown)
439     reportError(Action + " requires -arch <arch>");
440 
441   auto IF = getInterfaceFile(Ctx.Inputs.front(), /*ResetBanner=*/false);
442   auto OutIF = act(*IF, Ctx.Arch);
443   if (!OutIF)
444     ExitOnErr(OutIF.takeError());
445 
446   return handleWriteAction(Ctx, std::move(*OutIF));
447 }
448 
449 static void setStubOptions(opt::InputArgList &Args, StubOptions &Opt) {
450   Opt.DeleteInput = Args.hasArg(OPT_delete_input);
451   Opt.DeletePrivate = Args.hasArg(OPT_delete_private_libraries);
452   Opt.TraceLibs = Args.hasArg(OPT_t);
453 }
454 
455 int main(int Argc, char **Argv) {
456   InitLLVM X(Argc, Argv);
457   BumpPtrAllocator A;
458   StringSaver Saver(A);
459   TAPIOptTable Tbl;
460   Context Ctx;
461   ExitOnErr.setBanner(TOOLNAME + ": error:");
462   opt::InputArgList Args = Tbl.parseArgs(
463       Argc, Argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) { reportError(Msg); });
464   if (Args.hasArg(OPT_help)) {
465     Tbl.printHelp(outs(),
466                   "USAGE: llvm-readtapi <command> [-arch <architecture> "
467                   "<options>]* <inputs> [-o "
468                   "<output>]*",
469                   "LLVM TAPI file reader and transformer");
470     return EXIT_SUCCESS;
471   }
472 
473   if (Args.hasArg(OPT_version)) {
474     cl::PrintVersionMessage();
475     return EXIT_SUCCESS;
476   }
477 
478   for (opt::Arg *A : Args.filtered(OPT_INPUT))
479     Ctx.Inputs.push_back(A->getValue());
480 
481   if (opt::Arg *A = Args.getLastArg(OPT_output_EQ)) {
482     std::string OutputLoc = std::move(A->getValue());
483     std::error_code EC;
484     Ctx.OutStream = std::make_unique<llvm::raw_fd_stream>(OutputLoc, EC);
485     if (EC)
486       reportError("error opening the file '" + OutputLoc + EC.message(),
487                   NON_TAPI_EXIT_CODE);
488   }
489 
490   Ctx.Compact = Args.hasArg(OPT_compact);
491 
492   if (opt::Arg *A = Args.getLastArg(OPT_filetype_EQ)) {
493     StringRef FT = A->getValue();
494     Ctx.WriteFT = TextAPIWriter::parseFileType(FT);
495     if (Ctx.WriteFT < FileType::TBD_V3)
496       reportError("deprecated filetype '" + FT + "' is not supported to write");
497     if (Ctx.WriteFT == FileType::Invalid)
498       reportError("unsupported filetype '" + FT + "'");
499   }
500 
501   if (opt::Arg *A = Args.getLastArg(OPT_arch_EQ)) {
502     StringRef Arch = A->getValue();
503     Ctx.Arch = getArchitectureFromName(Arch);
504     if (Ctx.Arch == AK_unknown)
505       reportError("unsupported architecture '" + Arch);
506   }
507   // Handle top level and exclusive operation.
508   SmallVector<opt::Arg *, 1> ActionArgs(Args.filtered(OPT_action_group));
509 
510   if (ActionArgs.empty())
511     // If no action specified, write out tapi file in requested format.
512     return handleWriteAction(Ctx);
513 
514   if (ActionArgs.size() > 1) {
515     std::string Buf;
516     raw_string_ostream OS(Buf);
517     OS << "only one of the following actions can be specified:";
518     for (auto *Arg : ActionArgs)
519       OS << " " << Arg->getSpelling();
520     reportError(OS.str());
521   }
522 
523   switch (ActionArgs.front()->getOption().getID()) {
524   case OPT_compare:
525     return handleCompareAction(Ctx);
526   case OPT_merge:
527     return handleMergeAction(Ctx);
528   case OPT_extract:
529     return handleSingleFileAction(Ctx, "extract", &InterfaceFile::extract);
530   case OPT_remove:
531     return handleSingleFileAction(Ctx, "remove", &InterfaceFile::remove);
532   case OPT_stubify:
533     setStubOptions(Args, Ctx.StubOpt);
534     return handleStubifyAction(Ctx);
535   }
536 
537   return EXIT_SUCCESS;
538 }
539