1 //===- Driver.h -------------------------------------------------*- 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 #ifndef LLD_MACHO_DRIVER_H
10 #define LLD_MACHO_DRIVER_H
11 
12 #include "lld/Common/LLVM.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/SetVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Option/OptTable.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 
19 #include <set>
20 #include <type_traits>
21 
22 namespace llvm {
23 namespace MachO {
24 class InterfaceFile;
25 enum class PlatformKind : unsigned;
26 } // namespace MachO
27 } // namespace llvm
28 
29 namespace lld {
30 namespace macho {
31 
32 class DylibFile;
33 class InputFile;
34 
35 class MachOOptTable : public llvm::opt::OptTable {
36 public:
37   MachOOptTable();
38   llvm::opt::InputArgList parse(ArrayRef<const char *> argv);
39   void printHelp(const char *argv0, bool showHidden) const;
40 };
41 
42 // Create enum with OPT_xxx values for each option in Options.td
43 enum {
44   OPT_INVALID = 0,
45 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
46 #include "Options.inc"
47 #undef OPTION
48 };
49 
50 void parseLCLinkerOption(InputFile *, unsigned argc, StringRef data);
51 
52 std::string createResponseFile(const llvm::opt::InputArgList &args);
53 
54 // Check for both libfoo.dylib and libfoo.tbd (in that order).
55 llvm::Optional<std::string> resolveDylibPath(llvm::StringRef path);
56 
57 DylibFile *loadDylib(llvm::MemoryBufferRef mbref, DylibFile *umbrella = nullptr,
58                      bool isBundleLoader = false);
59 
60 // Search for all possible combinations of `{root}/{name}.{extension}`.
61 // If \p extensions are not specified, then just search for `{root}/{name}`.
62 llvm::Optional<llvm::StringRef>
63 findPathCombination(const llvm::Twine &name,
64                     const std::vector<llvm::StringRef> &roots,
65                     ArrayRef<llvm::StringRef> extensions = {""});
66 
67 // If -syslibroot is specified, absolute paths to non-object files may be
68 // rerooted.
69 llvm::StringRef rerootPath(llvm::StringRef path);
70 
71 llvm::Optional<InputFile *> loadArchiveMember(MemoryBufferRef, uint32_t modTime,
72                                               StringRef archiveName,
73                                               bool objCOnly,
74                                               uint64_t offsetInArchive);
75 
76 uint32_t getModTime(llvm::StringRef path);
77 
78 void printArchiveMemberLoad(StringRef reason, const InputFile *);
79 
80 // Map simulator platforms to their underlying device platform.
81 llvm::MachO::PlatformKind removeSimulator(llvm::MachO::PlatformKind platform);
82 
83 // Helper class to export dependency info.
84 class DependencyTracker {
85 public:
86   explicit DependencyTracker(llvm::StringRef path);
87 
88   // Adds the given path to the set of not-found files.
logFileNotFound(const Twine & path)89   inline void logFileNotFound(const Twine &path) {
90     if (active)
91       notFounds.insert(path.str());
92   }
93 
94   // Writes the dependencies to specified path.
95   // The content is sorted by its Op Code, then within each section,
96   // alphabetical order.
97   void write(llvm::StringRef version,
98              const llvm::SetVector<InputFile *> &inputs,
99              llvm::StringRef output);
100 
101 private:
102   enum DepOpCode : uint8_t {
103     // Denotes the linker version.
104     Version = 0x00,
105     // Denotes the input files.
106     Input = 0x10,
107     // Denotes the files that do not exist(?)
108     NotFound = 0x11,
109     // Denotes the output files.
110     Output = 0x40,
111   };
112 
113   const llvm::StringRef path;
114   bool active;
115 
116   // The paths need to be alphabetically ordered.
117   // We need to own the paths because some of them are temporarily
118   // constructed.
119   std::set<std::string> notFounds;
120 };
121 
122 extern DependencyTracker *depTracker;
123 
124 } // namespace macho
125 } // namespace lld
126 
127 #endif
128