1 //===- Driver.h -------------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_COFF_DRIVER_H
11 #define LLD_COFF_DRIVER_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/ADT/StringSet.h"
20 #include "llvm/Object/Archive.h"
21 #include "llvm/Object/COFF.h"
22 #include "llvm/Option/Arg.h"
23 #include "llvm/Option/ArgList.h"
24 #include "llvm/Support/TarWriter.h"
33 extern LinkerDriver *Driver;
35 using llvm::COFF::MachineTypes;
36 using llvm::COFF::WindowsSubsystem;
39 // Implemented in MarkLive.cpp.
40 void markLive(ArrayRef<Chunk *> Chunks);
42 // Implemented in ICF.cpp.
43 void doICF(ArrayRef<Chunk *> Chunks);
45 class COFFOptTable : public llvm::opt::OptTable {
52 // Concatenate LINK environment variable and given arguments and parse them.
53 llvm::opt::InputArgList parseLINK(std::vector<const char *> Args);
55 // Tokenizes a given string and then parses as command line options.
56 llvm::opt::InputArgList parse(StringRef S) { return parse(tokenize(S)); }
58 // Tokenizes a given string and then parses as command line options in
60 llvm::opt::InputArgList parseDirectives(StringRef S);
63 // Parses command line options.
64 llvm::opt::InputArgList parse(llvm::ArrayRef<const char *> Args);
66 std::vector<const char *> tokenize(StringRef S);
73 void link(llvm::ArrayRef<const char *> Args);
75 // Used by the resolver to parse .drectve section contents.
76 void parseDirectives(StringRef S);
78 // Used by ArchiveFile to enqueue members.
79 void enqueueArchiveMember(const Archive::Child &C, StringRef SymName,
80 StringRef ParentName);
82 MemoryBufferRef takeBuffer(std::unique_ptr<MemoryBuffer> MB);
85 std::unique_ptr<llvm::TarWriter> Tar; // for /linkrepro
87 // Opens a file. Path has to be resolved already.
88 MemoryBufferRef openFile(StringRef Path);
90 // Searches a file from search paths.
91 Optional<StringRef> findFile(StringRef Filename);
92 Optional<StringRef> findLib(StringRef Filename);
93 StringRef doFindFile(StringRef Filename);
94 StringRef doFindLib(StringRef Filename);
96 // Parses LIB environment which contains a list of search paths.
97 void addLibSearchPaths();
99 // Library search path. The first element is always "" (current directory).
100 std::vector<StringRef> SearchPaths;
101 std::set<std::string> VisitedFiles;
102 std::set<std::string> VisitedLibs;
104 Symbol *addUndefined(StringRef Sym);
105 StringRef mangle(StringRef Sym);
107 // Windows specific -- "main" is not the only main function in Windows.
108 // You can choose one from these four -- {w,}{WinMain,main}.
109 // There are four different entry point functions for them,
110 // {w,}{WinMain,main}CRTStartup, respectively. The linker needs to
111 // choose the right one depending on which "main" function is defined.
112 // This function looks up the symbol table and resolve corresponding
114 StringRef findDefaultEntry();
115 WindowsSubsystem inferSubsystem();
117 void invokeMSVC(llvm::opt::InputArgList &Args);
119 void addBuffer(std::unique_ptr<MemoryBuffer> MB, bool WholeArchive);
120 void addArchiveBuffer(MemoryBufferRef MBRef, StringRef SymName,
121 StringRef ParentName);
123 void enqueuePath(StringRef Path, bool WholeArchive);
125 void enqueueTask(std::function<void()> Task);
128 std::list<std::function<void()>> TaskQueue;
129 std::vector<StringRef> FilePaths;
130 std::vector<MemoryBufferRef> Resources;
132 llvm::StringSet<> DirectivesExports;
135 // Functions below this line are defined in DriverUtils.cpp.
137 void printHelp(const char *Argv0);
139 // For /machine option.
140 MachineTypes getMachineType(StringRef Arg);
141 StringRef machineToStr(MachineTypes MT);
143 // Parses a string in the form of "<integer>[,<integer>]".
144 void parseNumbers(StringRef Arg, uint64_t *Addr, uint64_t *Size = nullptr);
146 // Parses a string in the form of "<integer>[.<integer>]".
147 // Minor's default value is 0.
148 void parseVersion(StringRef Arg, uint32_t *Major, uint32_t *Minor);
150 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
151 void parseSubsystem(StringRef Arg, WindowsSubsystem *Sys, uint32_t *Major,
154 void parseAlternateName(StringRef);
155 void parseMerge(StringRef);
156 void parseSection(StringRef);
157 void parseAligncomm(StringRef);
159 // Parses a string in the form of "EMBED[,=<integer>]|NO".
160 void parseManifest(StringRef Arg);
162 // Parses a string in the form of "level=<string>|uiAccess=<string>"
163 void parseManifestUAC(StringRef Arg);
165 // Create a resource file containing a manifest XML.
166 std::unique_ptr<MemoryBuffer> createManifestRes();
167 void createSideBySideManifest();
169 // Used for dllexported symbols.
170 Export parseExport(StringRef Arg);
172 void assignExportOrdinals();
174 // Parses a string in the form of "key=value" and check
175 // if value matches previous values for the key.
176 // This feature used in the directive section to reject
177 // incompatible objects.
178 void checkFailIfMismatch(StringRef Arg);
180 // Convert Windows resource files (.res files) to a .obj file.
181 MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> MBs);
183 void runMSVCLinker(std::string Rsp, ArrayRef<StringRef> Objects);
185 // Create enum with OPT_xxx values for each option in Options.td
188 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
189 #include "Options.inc"