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