xref: /freebsd/contrib/llvm-project/lld/COFF/Driver.h (revision c697fb7f)
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   // Parses command line options.
47   llvm::opt::InputArgList parse(llvm::ArrayRef<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   // Concatenate LINK environment variable.
60   void addLINK(SmallVector<const char *, 256> &argv);
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, bool lazy);
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   // Convert resource files and potentially merge input resource object
102   // trees into one resource tree.
103   void convertResources();
104 
105   void maybeExportMinGWSymbols(const llvm::opt::InputArgList &args);
106 
107   // We don't want to add the same file more than once.
108   // Files are uniquified by their filesystem and file number.
109   std::set<llvm::sys::fs::UniqueID> visitedFiles;
110 
111   std::set<std::string> visitedLibs;
112 
113   Symbol *addUndefined(StringRef sym);
114 
115   StringRef mangleMaybe(Symbol *s);
116 
117   // Windows specific -- "main" is not the only main function in Windows.
118   // You can choose one from these four -- {w,}{WinMain,main}.
119   // There are four different entry point functions for them,
120   // {w,}{WinMain,main}CRTStartup, respectively. The linker needs to
121   // choose the right one depending on which "main" function is defined.
122   // This function looks up the symbol table and resolve corresponding
123   // entry point name.
124   StringRef findDefaultEntry();
125   WindowsSubsystem inferSubsystem();
126 
127   void addBuffer(std::unique_ptr<MemoryBuffer> mb, bool wholeArchive,
128                  bool lazy);
129   void addArchiveBuffer(MemoryBufferRef mbref, StringRef symName,
130                         StringRef parentName, uint64_t offsetInArchive);
131 
132   void enqueueTask(std::function<void()> task);
133   bool run();
134 
135   std::list<std::function<void()>> taskQueue;
136   std::vector<StringRef> filePaths;
137   std::vector<MemoryBufferRef> resources;
138 
139   llvm::StringSet<> directivesExports;
140 };
141 
142 // Functions below this line are defined in DriverUtils.cpp.
143 
144 void printHelp(const char *argv0);
145 
146 // Parses a string in the form of "<integer>[,<integer>]".
147 void parseNumbers(StringRef arg, uint64_t *addr, uint64_t *size = nullptr);
148 
149 void parseGuard(StringRef arg);
150 
151 // Parses a string in the form of "<integer>[.<integer>]".
152 // Minor's default value is 0.
153 void parseVersion(StringRef arg, uint32_t *major, uint32_t *minor);
154 
155 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
156 void parseSubsystem(StringRef arg, WindowsSubsystem *sys, uint32_t *major,
157                     uint32_t *minor);
158 
159 void parseAlternateName(StringRef);
160 void parseMerge(StringRef);
161 void parseSection(StringRef);
162 void parseAligncomm(StringRef);
163 
164 // Parses a string in the form of "[:<integer>]"
165 void parseFunctionPadMin(llvm::opt::Arg *a, llvm::COFF::MachineTypes machine);
166 
167 // Parses a string in the form of "EMBED[,=<integer>]|NO".
168 void parseManifest(StringRef arg);
169 
170 // Parses a string in the form of "level=<string>|uiAccess=<string>"
171 void parseManifestUAC(StringRef arg);
172 
173 // Parses a string in the form of "cd|net[,(cd|net)]*"
174 void parseSwaprun(StringRef arg);
175 
176 // Create a resource file containing a manifest XML.
177 std::unique_ptr<MemoryBuffer> createManifestRes();
178 void createSideBySideManifest();
179 
180 // Used for dllexported symbols.
181 Export parseExport(StringRef arg);
182 void fixupExports();
183 void assignExportOrdinals();
184 
185 // Parses a string in the form of "key=value" and check
186 // if value matches previous values for the key.
187 // This feature used in the directive section to reject
188 // incompatible objects.
189 void checkFailIfMismatch(StringRef arg, InputFile *source);
190 
191 // Convert Windows resource files (.res files) to a .obj file.
192 MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> mbs,
193                                  ArrayRef<ObjFile *> objs);
194 
195 void runMSVCLinker(std::string rsp, ArrayRef<StringRef> objects);
196 
197 // Create enum with OPT_xxx values for each option in Options.td
198 enum {
199   OPT_INVALID = 0,
200 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
201 #include "Options.inc"
202 #undef OPTION
203 };
204 
205 } // namespace coff
206 } // namespace lld
207 
208 #endif
209