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