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/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"
32 extern LinkerDriver *Driver;
34 using llvm::COFF::MachineTypes;
35 using llvm::COFF::WindowsSubsystem;
38 // Implemented in MarkLive.cpp.
39 void markLive(ArrayRef<Chunk *> Chunks);
41 // Implemented in ICF.cpp.
42 void doICF(ArrayRef<Chunk *> Chunks);
44 class COFFOptTable : public llvm::opt::OptTable {
51 // Concatenate LINK environment variable and given arguments and parse them.
52 llvm::opt::InputArgList parseLINK(std::vector<const char *> Args);
54 // Tokenizes a given string and then parses as command line options.
55 llvm::opt::InputArgList parse(StringRef S) { return parse(tokenize(S)); }
58 // Parses command line options.
59 llvm::opt::InputArgList parse(llvm::ArrayRef<const char *> Args);
61 std::vector<const char *> tokenize(StringRef S);
68 void link(llvm::ArrayRef<const char *> Args);
70 // Used by the resolver to parse .drectve section contents.
71 void parseDirectives(StringRef S);
73 // Used by ArchiveFile to enqueue members.
74 void enqueueArchiveMember(const Archive::Child &C, StringRef SymName,
75 StringRef ParentName);
77 MemoryBufferRef takeBuffer(std::unique_ptr<MemoryBuffer> MB);
80 std::unique_ptr<llvm::TarWriter> Tar; // for /linkrepro
82 // Opens a file. Path has to be resolved already.
83 MemoryBufferRef openFile(StringRef Path);
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);
91 // Parses LIB environment which contains a list of search paths.
92 void addLibSearchPaths();
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;
99 Symbol *addUndefined(StringRef Sym);
100 StringRef mangle(StringRef Sym);
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
109 StringRef findDefaultEntry();
110 WindowsSubsystem inferSubsystem();
112 void invokeMSVC(llvm::opt::InputArgList &Args);
114 void addBuffer(std::unique_ptr<MemoryBuffer> MB, bool WholeArchive);
115 void addArchiveBuffer(MemoryBufferRef MBRef, StringRef SymName,
116 StringRef ParentName);
118 void enqueuePath(StringRef Path, bool WholeArchive);
120 void enqueueTask(std::function<void()> Task);
123 std::list<std::function<void()>> TaskQueue;
124 std::vector<StringRef> FilePaths;
125 std::vector<MemoryBufferRef> Resources;
128 // Functions below this line are defined in DriverUtils.cpp.
130 void printHelp(const char *Argv0);
132 // For /machine option.
133 MachineTypes getMachineType(StringRef Arg);
134 StringRef machineToStr(MachineTypes MT);
136 // Parses a string in the form of "<integer>[,<integer>]".
137 void parseNumbers(StringRef Arg, uint64_t *Addr, uint64_t *Size = nullptr);
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);
143 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
144 void parseSubsystem(StringRef Arg, WindowsSubsystem *Sys, uint32_t *Major,
147 void parseAlternateName(StringRef);
148 void parseMerge(StringRef);
149 void parseSection(StringRef);
150 void parseAligncomm(StringRef);
152 // Parses a string in the form of "EMBED[,=<integer>]|NO".
153 void parseManifest(StringRef Arg);
155 // Parses a string in the form of "level=<string>|uiAccess=<string>"
156 void parseManifestUAC(StringRef Arg);
158 // Create a resource file containing a manifest XML.
159 std::unique_ptr<MemoryBuffer> createManifestRes();
160 void createSideBySideManifest();
162 // Used for dllexported symbols.
163 Export parseExport(StringRef Arg);
165 void assignExportOrdinals();
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);
173 // Convert Windows resource files (.res files) to a .obj file.
174 MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> MBs);
176 void runMSVCLinker(std::string Rsp, ArrayRef<StringRef> Objects);
178 // Create enum with OPT_xxx values for each option in Options.td
181 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
182 #include "Options.inc"