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 "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"
30 extern LinkerDriver *Driver;
32 using llvm::COFF::MachineTypes;
33 using llvm::COFF::WindowsSubsystem;
37 // Implemented in MarkLive.cpp.
38 void markLive(const std::vector<Chunk *> &Chunks);
40 // Implemented in ICF.cpp.
41 void doICF(const std::vector<Chunk *> &Chunks);
45 ArgParser() : Alloc(AllocAux) {}
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(llvm::ArrayRef<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 *>);
60 llvm::BumpPtrAllocator AllocAux;
61 llvm::StringSaver Alloc;
66 LinkerDriver() : Alloc(AllocAux) {}
67 void link(llvm::ArrayRef<const char *> Args);
69 // Used by the resolver to parse .drectve section contents.
70 void parseDirectives(StringRef S);
73 llvm::BumpPtrAllocator AllocAux;
74 llvm::StringSaver Alloc;
78 // Opens a file. Path has to be resolved already.
79 MemoryBufferRef openFile(StringRef Path);
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);
87 // Parses LIB environment which contains a list of search paths.
88 void addLibSearchPaths();
90 // Library search path. The first element is always "" (current directory).
91 std::vector<StringRef> SearchPaths;
92 std::set<std::string> VisitedFiles;
94 Undefined *addUndefined(StringRef Sym);
95 StringRef mangle(StringRef Sym);
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
104 StringRef findDefaultEntry();
105 WindowsSubsystem inferSubsystem();
107 // Driver is the owner of all opened files.
108 // InputFiles have MemoryBufferRefs to them.
109 std::vector<std::unique_ptr<MemoryBuffer>> OwningMBs;
112 void parseModuleDefs(MemoryBufferRef MB, llvm::StringSaver *Alloc);
113 void writeImportLibrary();
115 // Functions below this line are defined in DriverUtils.cpp.
117 void printHelp(const char *Argv0);
119 // For /machine option.
120 MachineTypes getMachineType(StringRef Arg);
121 StringRef machineToStr(MachineTypes MT);
123 // Parses a string in the form of "<integer>[,<integer>]".
124 void parseNumbers(StringRef Arg, uint64_t *Addr, uint64_t *Size = nullptr);
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);
130 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
131 void parseSubsystem(StringRef Arg, WindowsSubsystem *Sys, uint32_t *Major,
134 void parseAlternateName(StringRef);
135 void parseMerge(StringRef);
136 void parseSection(StringRef);
138 // Parses a string in the form of "EMBED[,=<integer>]|NO".
139 void parseManifest(StringRef Arg);
141 // Parses a string in the form of "level=<string>|uiAccess=<string>"
142 void parseManifestUAC(StringRef Arg);
144 // Create a resource file containing a manifest XML.
145 std::unique_ptr<MemoryBuffer> createManifestRes();
146 void createSideBySideManifest();
148 // Used for dllexported symbols.
149 Export parseExport(StringRef Arg);
151 void assignExportOrdinals();
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);
159 // Convert Windows resource files (.res files) to a .obj file
161 std::unique_ptr<MemoryBuffer>
162 convertResToCOFF(const std::vector<MemoryBufferRef> &MBs);
164 void createPDB(StringRef Path);
166 // Create enum with OPT_xxx values for each option in Options.td
169 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11) OPT_##ID,
170 #include "Options.inc"