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/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"
32 extern LinkerDriver
*Driver
;
34 using llvm::COFF::MachineTypes
;
35 using llvm::COFF::WindowsSubsystem
;
38 // Implemented in MarkLive.cpp.
39 void markLive(const std::vector
<Chunk
*> &Chunks
);
41 // Implemented in ICF.cpp.
42 void doICF(const std::vector
<Chunk
*> &Chunks
);
46 // Parses command line options.
47 llvm::opt::InputArgList
parse(llvm::ArrayRef
<const char *> Args
);
49 // Concatenate LINK environment varirable and given arguments and parse them.
50 llvm::opt::InputArgList
parseLINK(std::vector
<const char *> Args
);
52 // Tokenizes a given string and then parses as command line options.
53 llvm::opt::InputArgList
parse(StringRef S
) { return parse(tokenize(S
)); }
56 std::vector
<const char *> tokenize(StringRef S
);
58 std::vector
<const char *> replaceResponseFiles(std::vector
<const char *>);
63 LinkerDriver() { coff::Symtab
= &Symtab
; }
64 void link(llvm::ArrayRef
<const char *> Args
);
66 // Used by the resolver to parse .drectve section contents.
67 void parseDirectives(StringRef S
);
69 // Used by ArchiveFile to enqueue members.
70 void enqueueArchiveMember(const Archive::Child
&C
, StringRef SymName
,
71 StringRef ParentName
);
77 std::unique_ptr
<llvm::TarWriter
> Tar
; // for /linkrepro
79 // Opens a file. Path has to be resolved already.
80 MemoryBufferRef
openFile(StringRef Path
);
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
);
88 // Parses LIB environment which contains a list of search paths.
89 void addLibSearchPaths();
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
;
96 SymbolBody
*addUndefined(StringRef Sym
);
97 StringRef
mangle(StringRef Sym
);
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
106 StringRef
findDefaultEntry();
107 WindowsSubsystem
inferSubsystem();
109 void invokeMSVC(llvm::opt::InputArgList
&Args
);
111 MemoryBufferRef
takeBuffer(std::unique_ptr
<MemoryBuffer
> MB
);
112 void addBuffer(std::unique_ptr
<MemoryBuffer
> MB
);
113 void addArchiveBuffer(MemoryBufferRef MBRef
, StringRef SymName
,
114 StringRef ParentName
);
116 void enqueuePath(StringRef Path
);
118 void enqueueTask(std::function
<void()> Task
);
121 std::list
<std::function
<void()>> TaskQueue
;
122 std::vector
<StringRef
> FilePaths
;
123 std::vector
<MemoryBufferRef
> Resources
;
126 // Functions below this line are defined in DriverUtils.cpp.
128 void printHelp(const char *Argv0
);
130 // For /machine option.
131 MachineTypes
getMachineType(StringRef Arg
);
132 StringRef
machineToStr(MachineTypes MT
);
134 // Parses a string in the form of "<integer>[,<integer>]".
135 void parseNumbers(StringRef Arg
, uint64_t *Addr
, uint64_t *Size
= nullptr);
137 // Parses a string in the form of "<integer>[.<integer>]".
138 // Minor's default value is 0.
139 void parseVersion(StringRef Arg
, uint32_t *Major
, uint32_t *Minor
);
141 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
142 void parseSubsystem(StringRef Arg
, WindowsSubsystem
*Sys
, uint32_t *Major
,
145 void parseAlternateName(StringRef
);
146 void parseMerge(StringRef
);
147 void parseSection(StringRef
);
149 // Parses a string in the form of "EMBED[,=<integer>]|NO".
150 void parseManifest(StringRef Arg
);
152 // Parses a string in the form of "level=<string>|uiAccess=<string>"
153 void parseManifestUAC(StringRef Arg
);
155 // Create a resource file containing a manifest XML.
156 std::unique_ptr
<MemoryBuffer
> createManifestRes();
157 void createSideBySideManifest();
159 // Used for dllexported symbols.
160 Export
parseExport(StringRef Arg
);
162 void assignExportOrdinals();
164 // Parses a string in the form of "key=value" and check
165 // if value matches previous values for the key.
166 // This feature used in the directive section to reject
167 // incompatible objects.
168 void checkFailIfMismatch(StringRef Arg
);
170 // Convert Windows resource files (.res files) to a .obj file
172 std::unique_ptr
<MemoryBuffer
>
173 convertResToCOFF(const std::vector
<MemoryBufferRef
> &MBs
);
175 void runMSVCLinker(std::string Rsp
, ArrayRef
<StringRef
> Objects
);
177 // Create enum with OPT_xxx values for each option in Options.td
180 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
181 #include "Options.inc"