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