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