]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/Driver.h
Merge lld trunk r321017 to contrib/llvm/tools/lld.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / COFF / Driver.h
1 //===- Driver.h -------------------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLD_COFF_DRIVER_H
11 #define LLD_COFF_DRIVER_H
12
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/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/TarWriter.h"
24 #include <memory>
25 #include <set>
26 #include <vector>
27
28 namespace lld {
29 namespace coff {
30
31 class LinkerDriver;
32 extern LinkerDriver *Driver;
33
34 using llvm::COFF::MachineTypes;
35 using llvm::COFF::WindowsSubsystem;
36 using llvm::Optional;
37
38 // Implemented in MarkLive.cpp.
39 void markLive(ArrayRef<Chunk *> Chunks);
40
41 // Implemented in ICF.cpp.
42 void doICF(ArrayRef<Chunk *> Chunks);
43
44 class COFFOptTable : public llvm::opt::OptTable {
45 public:
46   COFFOptTable();
47 };
48
49 class ArgParser {
50 public:
51   // Concatenate LINK environment variable and given arguments and parse them.
52   llvm::opt::InputArgList parseLINK(std::vector<const char *> Args);
53
54   // Tokenizes a given string and then parses as command line options.
55   llvm::opt::InputArgList parse(StringRef S) { return parse(tokenize(S)); }
56
57 private:
58   // Parses command line options.
59   llvm::opt::InputArgList parse(llvm::ArrayRef<const char *> Args);
60
61   std::vector<const char *> tokenize(StringRef S);
62
63   COFFOptTable Table;
64 };
65
66 class LinkerDriver {
67 public:
68   void link(llvm::ArrayRef<const char *> Args);
69
70   // Used by the resolver to parse .drectve section contents.
71   void parseDirectives(StringRef S);
72
73   // Used by ArchiveFile to enqueue members.
74   void enqueueArchiveMember(const Archive::Child &C, StringRef SymName,
75                             StringRef ParentName);
76
77   MemoryBufferRef takeBuffer(std::unique_ptr<MemoryBuffer> MB);
78
79 private:
80   std::unique_ptr<llvm::TarWriter> Tar; // for /linkrepro
81
82   // Opens a file. Path has to be resolved already.
83   MemoryBufferRef openFile(StringRef Path);
84
85   // Searches a file from search paths.
86   Optional<StringRef> findFile(StringRef Filename);
87   Optional<StringRef> findLib(StringRef Filename);
88   StringRef doFindFile(StringRef Filename);
89   StringRef doFindLib(StringRef Filename);
90
91   // Parses LIB environment which contains a list of search paths.
92   void addLibSearchPaths();
93
94   // Library search path. The first element is always "" (current directory).
95   std::vector<StringRef> SearchPaths;
96   std::set<std::string> VisitedFiles;
97   std::set<std::string> VisitedLibs;
98
99   Symbol *addUndefined(StringRef Sym);
100   StringRef mangle(StringRef Sym);
101
102   // Windows specific -- "main" is not the only main function in Windows.
103   // You can choose one from these four -- {w,}{WinMain,main}.
104   // There are four different entry point functions for them,
105   // {w,}{WinMain,main}CRTStartup, respectively. The linker needs to
106   // choose the right one depending on which "main" function is defined.
107   // This function looks up the symbol table and resolve corresponding
108   // entry point name.
109   StringRef findDefaultEntry();
110   WindowsSubsystem inferSubsystem();
111
112   void invokeMSVC(llvm::opt::InputArgList &Args);
113
114   void addBuffer(std::unique_ptr<MemoryBuffer> MB, bool WholeArchive);
115   void addArchiveBuffer(MemoryBufferRef MBRef, StringRef SymName,
116                         StringRef ParentName);
117
118   void enqueuePath(StringRef Path, bool WholeArchive);
119
120   void enqueueTask(std::function<void()> Task);
121   bool run();
122
123   std::list<std::function<void()>> TaskQueue;
124   std::vector<StringRef> FilePaths;
125   std::vector<MemoryBufferRef> Resources;
126 };
127
128 // Functions below this line are defined in DriverUtils.cpp.
129
130 void printHelp(const char *Argv0);
131
132 // For /machine option.
133 MachineTypes getMachineType(StringRef Arg);
134 StringRef machineToStr(MachineTypes MT);
135
136 // Parses a string in the form of "<integer>[,<integer>]".
137 void parseNumbers(StringRef Arg, uint64_t *Addr, uint64_t *Size = nullptr);
138
139 // Parses a string in the form of "<integer>[.<integer>]".
140 // Minor's default value is 0.
141 void parseVersion(StringRef Arg, uint32_t *Major, uint32_t *Minor);
142
143 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
144 void parseSubsystem(StringRef Arg, WindowsSubsystem *Sys, uint32_t *Major,
145                     uint32_t *Minor);
146
147 void parseAlternateName(StringRef);
148 void parseMerge(StringRef);
149 void parseSection(StringRef);
150 void parseAligncomm(StringRef);
151
152 // Parses a string in the form of "EMBED[,=<integer>]|NO".
153 void parseManifest(StringRef Arg);
154
155 // Parses a string in the form of "level=<string>|uiAccess=<string>"
156 void parseManifestUAC(StringRef Arg);
157
158 // Create a resource file containing a manifest XML.
159 std::unique_ptr<MemoryBuffer> createManifestRes();
160 void createSideBySideManifest();
161
162 // Used for dllexported symbols.
163 Export parseExport(StringRef Arg);
164 void fixupExports();
165 void assignExportOrdinals();
166
167 // Parses a string in the form of "key=value" and check
168 // if value matches previous values for the key.
169 // This feature used in the directive section to reject
170 // incompatible objects.
171 void checkFailIfMismatch(StringRef Arg);
172
173 // Convert Windows resource files (.res files) to a .obj file.
174 MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> MBs);
175
176 void runMSVCLinker(std::string Rsp, ArrayRef<StringRef> Objects);
177
178 // Create enum with OPT_xxx values for each option in Options.td
179 enum {
180   OPT_INVALID = 0,
181 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
182 #include "Options.inc"
183 #undef OPTION
184 };
185
186 } // namespace coff
187 } // namespace lld
188
189 #endif