1 //===- Driver.h -------------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_COFF_DRIVER_H
10 #define LLD_COFF_DRIVER_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"
33 extern LinkerDriver *driver;
35 using llvm::COFF::MachineTypes;
36 using llvm::COFF::WindowsSubsystem;
39 class COFFOptTable : public llvm::opt::OptTable {
46 // Concatenate LINK environment variable and given arguments and parse them.
47 llvm::opt::InputArgList parseLINK(std::vector<const char *> args);
49 // Tokenizes a given string and then parses as command line options.
50 llvm::opt::InputArgList parse(StringRef s) { return parse(tokenize(s)); }
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);
59 // Parses command line options.
60 llvm::opt::InputArgList parse(llvm::ArrayRef<const char *> args);
62 std::vector<const char *> tokenize(StringRef s);
69 void link(llvm::ArrayRef<const char *> args);
71 // Used by the resolver to parse .drectve section contents.
72 void parseDirectives(InputFile *file);
74 // Used by ArchiveFile to enqueue members.
75 void enqueueArchiveMember(const Archive::Child &c, const Archive::Symbol &sym,
76 StringRef parentName);
78 MemoryBufferRef takeBuffer(std::unique_ptr<MemoryBuffer> mb);
80 void enqueuePath(StringRef path, bool wholeArchive);
83 std::unique_ptr<llvm::TarWriter> tar; // for /linkrepro
85 // Opens a file. Path has to be resolved already.
86 MemoryBufferRef openFile(StringRef path);
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);
95 // Parses LIB environment which contains a list of search paths.
96 void addLibSearchPaths();
98 // Library search path. The first element is always "" (current directory).
99 std::vector<StringRef> searchPaths;
101 void maybeExportMinGWSymbols(const llvm::opt::InputArgList &args);
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;
107 std::set<std::string> visitedLibs;
109 Symbol *addUndefined(StringRef sym);
111 StringRef mangleMaybe(Symbol *s);
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
120 StringRef findDefaultEntry();
121 WindowsSubsystem inferSubsystem();
123 void addBuffer(std::unique_ptr<MemoryBuffer> mb, bool wholeArchive);
124 void addArchiveBuffer(MemoryBufferRef mbref, StringRef symName,
125 StringRef parentName, uint64_t offsetInArchive);
127 void enqueueTask(std::function<void()> task);
130 std::list<std::function<void()>> taskQueue;
131 std::vector<StringRef> filePaths;
132 std::vector<MemoryBufferRef> resources;
134 llvm::StringSet<> directivesExports;
137 // Functions below this line are defined in DriverUtils.cpp.
139 void printHelp(const char *argv0);
141 // Parses a string in the form of "<integer>[,<integer>]".
142 void parseNumbers(StringRef arg, uint64_t *addr, uint64_t *size = nullptr);
144 void parseGuard(StringRef arg);
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);
150 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
151 void parseSubsystem(StringRef arg, WindowsSubsystem *sys, uint32_t *major,
154 void parseAlternateName(StringRef);
155 void parseMerge(StringRef);
156 void parseSection(StringRef);
157 void parseAligncomm(StringRef);
159 // Parses a string in the form of "[:<integer>]"
160 void parseFunctionPadMin(llvm::opt::Arg *a, llvm::COFF::MachineTypes machine);
162 // Parses a string in the form of "EMBED[,=<integer>]|NO".
163 void parseManifest(StringRef arg);
165 // Parses a string in the form of "level=<string>|uiAccess=<string>"
166 void parseManifestUAC(StringRef arg);
168 // Parses a string in the form of "cd|net[,(cd|net)]*"
169 void parseSwaprun(StringRef arg);
171 // Create a resource file containing a manifest XML.
172 std::unique_ptr<MemoryBuffer> createManifestRes();
173 void createSideBySideManifest();
175 // Used for dllexported symbols.
176 Export parseExport(StringRef arg);
178 void assignExportOrdinals();
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);
186 // Convert Windows resource files (.res files) to a .obj file.
187 MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> mbs);
189 void runMSVCLinker(std::string rsp, ArrayRef<StringRef> objects);
191 // Create enum with OPT_xxx values for each option in Options.td
194 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
195 #include "Options.inc"