xref: /freebsd/contrib/llvm-project/lld/COFF/Driver.h (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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 extern std::unique_ptr<class 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   LinkerDriver(COFFLinkerContext &c) : ctx(c) {}
82 
83   void linkerMain(llvm::ArrayRef<const char *> args);
84 
85   // Used by the resolver to parse .drectve section contents.
86   void parseDirectives(InputFile *file);
87 
88   // Used by ArchiveFile to enqueue members.
89   void enqueueArchiveMember(const Archive::Child &c, const Archive::Symbol &sym,
90                             StringRef parentName);
91 
92   void enqueuePDB(StringRef Path) { enqueuePath(Path, false, false); }
93 
94   MemoryBufferRef takeBuffer(std::unique_ptr<MemoryBuffer> mb);
95 
96   void enqueuePath(StringRef path, bool wholeArchive, bool lazy);
97 
98   std::unique_ptr<llvm::TarWriter> tar; // for /linkrepro
99 
100 private:
101   // Searches a file from search paths.
102   Optional<StringRef> findFile(StringRef filename);
103   Optional<StringRef> findLib(StringRef filename);
104   StringRef doFindFile(StringRef filename);
105   StringRef doFindLib(StringRef filename);
106   StringRef doFindLibMinGW(StringRef filename);
107 
108   bool findUnderscoreMangle(StringRef sym);
109 
110   // Parses LIB environment which contains a list of search paths.
111   void addLibSearchPaths();
112 
113   // Library search path. The first element is always "" (current directory).
114   std::vector<StringRef> searchPaths;
115 
116   // Convert resource files and potentially merge input resource object
117   // trees into one resource tree.
118   void convertResources();
119 
120   void maybeExportMinGWSymbols(const llvm::opt::InputArgList &args);
121 
122   // We don't want to add the same file more than once.
123   // Files are uniquified by their filesystem and file number.
124   std::set<llvm::sys::fs::UniqueID> visitedFiles;
125 
126   std::set<std::string> visitedLibs;
127 
128   Symbol *addUndefined(StringRef sym);
129 
130   StringRef mangleMaybe(Symbol *s);
131 
132   // Windows specific -- "main" is not the only main function in Windows.
133   // You can choose one from these four -- {w,}{WinMain,main}.
134   // There are four different entry point functions for them,
135   // {w,}{WinMain,main}CRTStartup, respectively. The linker needs to
136   // choose the right one depending on which "main" function is defined.
137   // This function looks up the symbol table and resolve corresponding
138   // entry point name.
139   StringRef findDefaultEntry();
140   WindowsSubsystem inferSubsystem();
141 
142   void addBuffer(std::unique_ptr<MemoryBuffer> mb, bool wholeArchive,
143                  bool lazy);
144   void addArchiveBuffer(MemoryBufferRef mbref, StringRef symName,
145                         StringRef parentName, uint64_t offsetInArchive);
146 
147   void enqueueTask(std::function<void()> task);
148   bool run();
149 
150   std::list<std::function<void()>> taskQueue;
151   std::vector<StringRef> filePaths;
152   std::vector<MemoryBufferRef> resources;
153 
154   llvm::StringSet<> directivesExports;
155 
156   COFFLinkerContext &ctx;
157 };
158 
159 // Functions below this line are defined in DriverUtils.cpp.
160 
161 void printHelp(const char *argv0);
162 
163 // Parses a string in the form of "<integer>[,<integer>]".
164 void parseNumbers(StringRef arg, uint64_t *addr, uint64_t *size = nullptr);
165 
166 void parseGuard(StringRef arg);
167 
168 // Parses a string in the form of "<integer>[.<integer>]".
169 // Minor's default value is 0.
170 void parseVersion(StringRef arg, uint32_t *major, uint32_t *minor);
171 
172 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
173 void parseSubsystem(StringRef arg, WindowsSubsystem *sys, uint32_t *major,
174                     uint32_t *minor, bool *gotVersion = nullptr);
175 
176 void parseAlternateName(StringRef);
177 void parseMerge(StringRef);
178 void parsePDBPageSize(StringRef);
179 void parseSection(StringRef);
180 void parseAligncomm(StringRef);
181 
182 // Parses a string in the form of "[:<integer>]"
183 void parseFunctionPadMin(llvm::opt::Arg *a, llvm::COFF::MachineTypes machine);
184 
185 // Parses a string in the form of "EMBED[,=<integer>]|NO".
186 void parseManifest(StringRef arg);
187 
188 // Parses a string in the form of "level=<string>|uiAccess=<string>"
189 void parseManifestUAC(StringRef arg);
190 
191 // Parses a string in the form of "cd|net[,(cd|net)]*"
192 void parseSwaprun(StringRef arg);
193 
194 // Create a resource file containing a manifest XML.
195 std::unique_ptr<MemoryBuffer> createManifestRes();
196 void createSideBySideManifest();
197 
198 // Used for dllexported symbols.
199 Export parseExport(StringRef arg);
200 void fixupExports();
201 void assignExportOrdinals();
202 
203 // Parses a string in the form of "key=value" and check
204 // if value matches previous values for the key.
205 // This feature used in the directive section to reject
206 // incompatible objects.
207 void checkFailIfMismatch(StringRef arg, InputFile *source);
208 
209 // Convert Windows resource files (.res files) to a .obj file.
210 MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> mbs,
211                                  ArrayRef<ObjFile *> objs);
212 
213 // Create enum with OPT_xxx values for each option in Options.td
214 enum {
215   OPT_INVALID = 0,
216 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
217 #include "Options.inc"
218 #undef OPTION
219 };
220 
221 } // namespace coff
222 } // namespace lld
223 
224 #endif
225