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_COFF_DRIVER_H 10 #define LLD_COFF_DRIVER_H 11 12 #include "Config.h" 13 #include "SymbolTable.h" 14 #include "lld/Common/LLVM.h" 15 #include "lld/Common/Reproduce.h" 16 #include "llvm/ADT/Optional.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/ADT/StringSet.h" 19 #include "llvm/Object/Archive.h" 20 #include "llvm/Object/COFF.h" 21 #include "llvm/Option/Arg.h" 22 #include "llvm/Option/ArgList.h" 23 #include "llvm/Support/FileSystem.h" 24 #include "llvm/Support/TarWriter.h" 25 #include <memory> 26 #include <set> 27 #include <vector> 28 29 namespace lld { 30 namespace coff { 31 32 class LinkerDriver; 33 extern LinkerDriver *driver; 34 35 using llvm::COFF::MachineTypes; 36 using llvm::COFF::WindowsSubsystem; 37 using llvm::Optional; 38 39 class COFFOptTable : public llvm::opt::OptTable { 40 public: 41 COFFOptTable(); 42 }; 43 44 // Constructing the option table is expensive. Use a global table to avoid doing 45 // it more than once. 46 extern COFFOptTable optTable; 47 48 // The result of parsing the .drective section. The /export: and /include: 49 // options are handled separately because they reference symbols, and the number 50 // of symbols can be quite large. The LLVM Option library will perform at least 51 // one memory allocation per argument, and that is prohibitively slow for 52 // parsing directives. 53 struct ParsedDirectives { 54 std::vector<StringRef> exports; 55 std::vector<StringRef> includes; 56 llvm::opt::InputArgList args; 57 }; 58 59 class ArgParser { 60 public: 61 // Parses command line options. 62 llvm::opt::InputArgList parse(llvm::ArrayRef<const char *> args); 63 64 // Tokenizes a given string and then parses as command line options. 65 llvm::opt::InputArgList parse(StringRef s) { return parse(tokenize(s)); } 66 67 // Tokenizes a given string and then parses as command line options in 68 // .drectve section. /EXPORT options are returned in second element 69 // to be processed in fastpath. 70 ParsedDirectives parseDirectives(StringRef s); 71 72 private: 73 // Concatenate LINK environment variable. 74 void addLINK(SmallVector<const char *, 256> &argv); 75 76 std::vector<const char *> tokenize(StringRef s); 77 }; 78 79 class LinkerDriver { 80 public: 81 void link(llvm::ArrayRef<const char *> args); 82 83 // Used by the resolver to parse .drectve section contents. 84 void parseDirectives(InputFile *file); 85 86 // Used by ArchiveFile to enqueue members. 87 void enqueueArchiveMember(const Archive::Child &c, const Archive::Symbol &sym, 88 StringRef parentName); 89 90 void enqueuePDB(StringRef Path) { enqueuePath(Path, false, false); } 91 92 MemoryBufferRef takeBuffer(std::unique_ptr<MemoryBuffer> mb); 93 94 void enqueuePath(StringRef path, bool wholeArchive, bool lazy); 95 96 private: 97 std::unique_ptr<llvm::TarWriter> tar; // for /linkrepro 98 99 // Opens a file. Path has to be resolved already. 100 MemoryBufferRef openFile(StringRef path); 101 102 // Searches a file from search paths. 103 Optional<StringRef> findFile(StringRef filename); 104 Optional<StringRef> findLib(StringRef filename); 105 StringRef doFindFile(StringRef filename); 106 StringRef doFindLib(StringRef filename); 107 StringRef doFindLibMinGW(StringRef filename); 108 109 // Parses LIB environment which contains a list of search paths. 110 void addLibSearchPaths(); 111 112 // Library search path. The first element is always "" (current directory). 113 std::vector<StringRef> searchPaths; 114 115 // Convert resource files and potentially merge input resource object 116 // trees into one resource tree. 117 void convertResources(); 118 119 void maybeExportMinGWSymbols(const llvm::opt::InputArgList &args); 120 121 // We don't want to add the same file more than once. 122 // Files are uniquified by their filesystem and file number. 123 std::set<llvm::sys::fs::UniqueID> visitedFiles; 124 125 std::set<std::string> visitedLibs; 126 127 Symbol *addUndefined(StringRef sym); 128 129 StringRef mangleMaybe(Symbol *s); 130 131 // Windows specific -- "main" is not the only main function in Windows. 132 // You can choose one from these four -- {w,}{WinMain,main}. 133 // There are four different entry point functions for them, 134 // {w,}{WinMain,main}CRTStartup, respectively. The linker needs to 135 // choose the right one depending on which "main" function is defined. 136 // This function looks up the symbol table and resolve corresponding 137 // entry point name. 138 StringRef findDefaultEntry(); 139 WindowsSubsystem inferSubsystem(); 140 141 void addBuffer(std::unique_ptr<MemoryBuffer> mb, bool wholeArchive, 142 bool lazy); 143 void addArchiveBuffer(MemoryBufferRef mbref, StringRef symName, 144 StringRef parentName, uint64_t offsetInArchive); 145 146 void enqueueTask(std::function<void()> task); 147 bool run(); 148 149 std::list<std::function<void()>> taskQueue; 150 std::vector<StringRef> filePaths; 151 std::vector<MemoryBufferRef> resources; 152 153 llvm::StringSet<> directivesExports; 154 }; 155 156 // Functions below this line are defined in DriverUtils.cpp. 157 158 void printHelp(const char *argv0); 159 160 // Parses a string in the form of "<integer>[,<integer>]". 161 void parseNumbers(StringRef arg, uint64_t *addr, uint64_t *size = nullptr); 162 163 void parseGuard(StringRef arg); 164 165 // Parses a string in the form of "<integer>[.<integer>]". 166 // Minor's default value is 0. 167 void parseVersion(StringRef arg, uint32_t *major, uint32_t *minor); 168 169 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]". 170 void parseSubsystem(StringRef arg, WindowsSubsystem *sys, uint32_t *major, 171 uint32_t *minor); 172 173 void parseAlternateName(StringRef); 174 void parseMerge(StringRef); 175 void parseSection(StringRef); 176 void parseAligncomm(StringRef); 177 178 // Parses a string in the form of "[:<integer>]" 179 void parseFunctionPadMin(llvm::opt::Arg *a, llvm::COFF::MachineTypes machine); 180 181 // Parses a string in the form of "EMBED[,=<integer>]|NO". 182 void parseManifest(StringRef arg); 183 184 // Parses a string in the form of "level=<string>|uiAccess=<string>" 185 void parseManifestUAC(StringRef arg); 186 187 // Parses a string in the form of "cd|net[,(cd|net)]*" 188 void parseSwaprun(StringRef arg); 189 190 // Create a resource file containing a manifest XML. 191 std::unique_ptr<MemoryBuffer> createManifestRes(); 192 void createSideBySideManifest(); 193 194 // Used for dllexported symbols. 195 Export parseExport(StringRef arg); 196 void fixupExports(); 197 void assignExportOrdinals(); 198 199 // Parses a string in the form of "key=value" and check 200 // if value matches previous values for the key. 201 // This feature used in the directive section to reject 202 // incompatible objects. 203 void checkFailIfMismatch(StringRef arg, InputFile *source); 204 205 // Convert Windows resource files (.res files) to a .obj file. 206 MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> mbs, 207 ArrayRef<ObjFile *> objs); 208 209 void runMSVCLinker(std::string rsp, ArrayRef<StringRef> objects); 210 211 // Create enum with OPT_xxx values for each option in Options.td 212 enum { 213 OPT_INVALID = 0, 214 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID, 215 #include "Options.inc" 216 #undef OPTION 217 }; 218 219 } // namespace coff 220 } // namespace lld 221 222 #endif 223