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 class ArgParser { 45 public: 46 // Concatenate LINK environment variable and given arguments and parse them. 47 llvm::opt::InputArgList parseLINK(std::vector<const char *> args); 48 49 // Tokenizes a given string and then parses as command line options. 50 llvm::opt::InputArgList parse(StringRef s) { return parse(tokenize(s)); } 51 52 // Tokenizes a given string and then parses as command line options in 53 // .drectve section. /EXPORT options are returned in second element 54 // to be processed in fastpath. 55 std::pair<llvm::opt::InputArgList, std::vector<StringRef>> 56 parseDirectives(StringRef s); 57 58 private: 59 // Parses command line options. 60 llvm::opt::InputArgList parse(llvm::ArrayRef<const char *> args); 61 62 std::vector<const char *> tokenize(StringRef s); 63 64 COFFOptTable table; 65 }; 66 67 class LinkerDriver { 68 public: 69 void link(llvm::ArrayRef<const char *> args); 70 71 // Used by the resolver to parse .drectve section contents. 72 void parseDirectives(InputFile *file); 73 74 // Used by ArchiveFile to enqueue members. 75 void enqueueArchiveMember(const Archive::Child &c, const Archive::Symbol &sym, 76 StringRef parentName); 77 78 MemoryBufferRef takeBuffer(std::unique_ptr<MemoryBuffer> mb); 79 80 void enqueuePath(StringRef path, bool wholeArchive); 81 82 private: 83 std::unique_ptr<llvm::TarWriter> tar; // for /linkrepro 84 85 // Opens a file. Path has to be resolved already. 86 MemoryBufferRef openFile(StringRef path); 87 88 // Searches a file from search paths. 89 Optional<StringRef> findFile(StringRef filename); 90 Optional<StringRef> findLib(StringRef filename); 91 StringRef doFindFile(StringRef filename); 92 StringRef doFindLib(StringRef filename); 93 StringRef doFindLibMinGW(StringRef filename); 94 95 // Parses LIB environment which contains a list of search paths. 96 void addLibSearchPaths(); 97 98 // Library search path. The first element is always "" (current directory). 99 std::vector<StringRef> searchPaths; 100 101 void maybeExportMinGWSymbols(const llvm::opt::InputArgList &args); 102 103 // We don't want to add the same file more than once. 104 // Files are uniquified by their filesystem and file number. 105 std::set<llvm::sys::fs::UniqueID> visitedFiles; 106 107 std::set<std::string> visitedLibs; 108 109 Symbol *addUndefined(StringRef sym); 110 111 StringRef mangleMaybe(Symbol *s); 112 113 // Windows specific -- "main" is not the only main function in Windows. 114 // You can choose one from these four -- {w,}{WinMain,main}. 115 // There are four different entry point functions for them, 116 // {w,}{WinMain,main}CRTStartup, respectively. The linker needs to 117 // choose the right one depending on which "main" function is defined. 118 // This function looks up the symbol table and resolve corresponding 119 // entry point name. 120 StringRef findDefaultEntry(); 121 WindowsSubsystem inferSubsystem(); 122 123 void addBuffer(std::unique_ptr<MemoryBuffer> mb, bool wholeArchive); 124 void addArchiveBuffer(MemoryBufferRef mbref, StringRef symName, 125 StringRef parentName, uint64_t offsetInArchive); 126 127 void enqueueTask(std::function<void()> task); 128 bool run(); 129 130 std::list<std::function<void()>> taskQueue; 131 std::vector<StringRef> filePaths; 132 std::vector<MemoryBufferRef> resources; 133 134 llvm::StringSet<> directivesExports; 135 }; 136 137 // Functions below this line are defined in DriverUtils.cpp. 138 139 void printHelp(const char *argv0); 140 141 // Parses a string in the form of "<integer>[,<integer>]". 142 void parseNumbers(StringRef arg, uint64_t *addr, uint64_t *size = nullptr); 143 144 void parseGuard(StringRef arg); 145 146 // Parses a string in the form of "<integer>[.<integer>]". 147 // Minor's default value is 0. 148 void parseVersion(StringRef arg, uint32_t *major, uint32_t *minor); 149 150 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]". 151 void parseSubsystem(StringRef arg, WindowsSubsystem *sys, uint32_t *major, 152 uint32_t *minor); 153 154 void parseAlternateName(StringRef); 155 void parseMerge(StringRef); 156 void parseSection(StringRef); 157 void parseAligncomm(StringRef); 158 159 // Parses a string in the form of "[:<integer>]" 160 void parseFunctionPadMin(llvm::opt::Arg *a, llvm::COFF::MachineTypes machine); 161 162 // Parses a string in the form of "EMBED[,=<integer>]|NO". 163 void parseManifest(StringRef arg); 164 165 // Parses a string in the form of "level=<string>|uiAccess=<string>" 166 void parseManifestUAC(StringRef arg); 167 168 // Parses a string in the form of "cd|net[,(cd|net)]*" 169 void parseSwaprun(StringRef arg); 170 171 // Create a resource file containing a manifest XML. 172 std::unique_ptr<MemoryBuffer> createManifestRes(); 173 void createSideBySideManifest(); 174 175 // Used for dllexported symbols. 176 Export parseExport(StringRef arg); 177 void fixupExports(); 178 void assignExportOrdinals(); 179 180 // Parses a string in the form of "key=value" and check 181 // if value matches previous values for the key. 182 // This feature used in the directive section to reject 183 // incompatible objects. 184 void checkFailIfMismatch(StringRef arg, InputFile *source); 185 186 // Convert Windows resource files (.res files) to a .obj file. 187 MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> mbs); 188 189 void runMSVCLinker(std::string rsp, ArrayRef<StringRef> objects); 190 191 // Create enum with OPT_xxx values for each option in Options.td 192 enum { 193 OPT_INVALID = 0, 194 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID, 195 #include "Options.inc" 196 #undef OPTION 197 }; 198 199 } // namespace coff 200 } // namespace lld 201 202 #endif 203