Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 1 | //===- Driver.cpp ---------------------------------------------------------===// |
| 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 | #include "Config.h" |
| 11 | #include "Driver.h" |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 12 | #include "Error.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 13 | #include "InputFiles.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 14 | #include "SymbolTable.h" |
Rui Ueyama | 685c41c | 2015-08-05 23:43:53 +0000 | [diff] [blame] | 15 | #include "Symbols.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 16 | #include "Writer.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Optional.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
Rui Ueyama | 3ee0fe4 | 2015-05-31 03:55:46 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringSwitch.h" |
Peter Collingbourne | bd1cb79 | 2015-06-09 21:52:48 +0000 | [diff] [blame] | 20 | #include "llvm/LibDriver/LibDriver.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 21 | #include "llvm/Option/Arg.h" |
| 22 | #include "llvm/Option/ArgList.h" |
| 23 | #include "llvm/Option/Option.h" |
| 24 | #include "llvm/Support/CommandLine.h" |
| 25 | #include "llvm/Support/Debug.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Process.h" |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 28 | #include "llvm/Support/TargetSelect.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 31 | #include <memory> |
| 32 | |
| 33 | using namespace llvm; |
Rui Ueyama | 84936e0 | 2015-07-07 23:39:18 +0000 | [diff] [blame] | 34 | using namespace llvm::COFF; |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 35 | using llvm::sys::Process; |
Peter Collingbourne | baf5f87 | 2015-06-26 19:20:09 +0000 | [diff] [blame] | 36 | using llvm::sys::fs::OpenFlags; |
Rui Ueyama | 711cd2d | 2015-05-31 21:17:10 +0000 | [diff] [blame] | 37 | using llvm::sys::fs::file_magic; |
| 38 | using llvm::sys::fs::identify_magic; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 39 | |
Rui Ueyama | 3500f66 | 2015-05-28 20:30:06 +0000 | [diff] [blame] | 40 | namespace lld { |
| 41 | namespace coff { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 42 | |
Rui Ueyama | 3500f66 | 2015-05-28 20:30:06 +0000 | [diff] [blame] | 43 | Configuration *Config; |
Rui Ueyama | a9cbbf8 | 2015-05-31 19:17:09 +0000 | [diff] [blame] | 44 | LinkerDriver *Driver; |
| 45 | |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 46 | void link(llvm::ArrayRef<const char *> Args) { |
Rui Ueyama | 570752c | 2015-08-18 09:13:25 +0000 | [diff] [blame] | 47 | Configuration C; |
| 48 | LinkerDriver D; |
| 49 | Config = &C; |
| 50 | Driver = &D; |
David Blaikie | b2b1c7c | 2015-06-21 06:32:10 +0000 | [diff] [blame] | 51 | return Driver->link(Args); |
Rui Ueyama | a9cbbf8 | 2015-05-31 19:17:09 +0000 | [diff] [blame] | 52 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 53 | |
Rui Ueyama | ad66098 | 2015-06-07 00:20:32 +0000 | [diff] [blame] | 54 | // Drop directory components and replace extension with ".exe". |
| 55 | static std::string getOutputPath(StringRef Path) { |
| 56 | auto P = Path.find_last_of("\\/"); |
| 57 | StringRef S = (P == StringRef::npos) ? Path : Path.substr(P + 1); |
| 58 | return (S.substr(0, S.rfind('.')) + ".exe").str(); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Rui Ueyama | d7c2f58 | 2015-05-31 21:04:56 +0000 | [diff] [blame] | 61 | // Opens a file. Path has to be resolved already. |
| 62 | // Newly created memory buffers are owned by this driver. |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 63 | MemoryBufferRef LinkerDriver::openFile(StringRef Path) { |
Rui Ueyama | d7c2f58 | 2015-05-31 21:04:56 +0000 | [diff] [blame] | 64 | auto MBOrErr = MemoryBuffer::getFile(Path); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 65 | error(MBOrErr, Twine("Could not open ") + Path); |
| 66 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
Rui Ueyama | d7c2f58 | 2015-05-31 21:04:56 +0000 | [diff] [blame] | 67 | MemoryBufferRef MBRef = MB->getMemBufferRef(); |
| 68 | OwningMBs.push_back(std::move(MB)); // take ownership |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 69 | return MBRef; |
| 70 | } |
Rui Ueyama | 711cd2d | 2015-05-31 21:17:10 +0000 | [diff] [blame] | 71 | |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 72 | static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) { |
Rui Ueyama | 711cd2d | 2015-05-31 21:17:10 +0000 | [diff] [blame] | 73 | // File type is detected by contents, not by file extension. |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 74 | file_magic Magic = identify_magic(MB.getBuffer()); |
Rui Ueyama | 711cd2d | 2015-05-31 21:17:10 +0000 | [diff] [blame] | 75 | if (Magic == file_magic::archive) |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 76 | return std::unique_ptr<InputFile>(new ArchiveFile(MB)); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 77 | if (Magic == file_magic::bitcode) |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 78 | return std::unique_ptr<InputFile>(new BitcodeFile(MB)); |
Rui Ueyama | ad66098 | 2015-06-07 00:20:32 +0000 | [diff] [blame] | 79 | if (Config->OutputFile == "") |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 80 | Config->OutputFile = getOutputPath(MB.getBufferIdentifier()); |
| 81 | return std::unique_ptr<InputFile>(new ObjectFile(MB)); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 84 | // Parses .drectve section contents and returns a list of files |
| 85 | // specified by /defaultlib. |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 86 | void LinkerDriver::parseDirectives(StringRef S) { |
| 87 | llvm::opt::InputArgList Args = Parser.parse(S); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 88 | |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 89 | for (auto *Arg : Args) { |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 90 | switch (Arg->getOption().getID()) { |
| 91 | case OPT_alternatename: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 92 | parseAlternateName(Arg->getValue()); |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 93 | break; |
| 94 | case OPT_defaultlib: |
| 95 | if (Optional<StringRef> Path = findLib(Arg->getValue())) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 96 | MemoryBufferRef MB = openFile(*Path); |
| 97 | Symtab.addFile(createFile(MB)); |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 98 | } |
| 99 | break; |
| 100 | case OPT_export: { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 101 | Export E = parseExport(Arg->getValue()); |
| 102 | if (Config->Machine == I386 && E.ExtName.startswith("_")) |
| 103 | E.ExtName = E.ExtName.substr(1); |
| 104 | Config->Exports.push_back(E); |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 105 | break; |
| 106 | } |
| 107 | case OPT_failifmismatch: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 108 | checkFailIfMismatch(Arg->getValue()); |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 109 | break; |
Rui Ueyama | 08d5e18 | 2015-06-18 23:20:11 +0000 | [diff] [blame] | 110 | case OPT_incl: |
Rui Ueyama | 32f8e1c | 2015-06-26 03:44:00 +0000 | [diff] [blame] | 111 | addUndefined(Arg->getValue()); |
Rui Ueyama | 08d5e18 | 2015-06-18 23:20:11 +0000 | [diff] [blame] | 112 | break; |
Rui Ueyama | ce86c99 | 2015-06-18 23:22:39 +0000 | [diff] [blame] | 113 | case OPT_merge: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 114 | parseMerge(Arg->getValue()); |
Rui Ueyama | ce86c99 | 2015-06-18 23:22:39 +0000 | [diff] [blame] | 115 | break; |
| 116 | case OPT_nodefaultlib: |
| 117 | Config->NoDefaultLibs.insert(doFindLib(Arg->getValue())); |
| 118 | break; |
Rui Ueyama | 3c4737d | 2015-08-11 16:46:08 +0000 | [diff] [blame] | 119 | case OPT_editandcontinue: |
Rui Ueyama | 43238317 | 2015-07-29 21:01:15 +0000 | [diff] [blame] | 120 | case OPT_throwingnew: |
Rui Ueyama | 4668263 | 2015-07-29 20:29:15 +0000 | [diff] [blame] | 121 | break; |
Rui Ueyama | 562daa8 | 2015-06-18 21:50:38 +0000 | [diff] [blame] | 122 | default: |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 123 | error(Twine(Arg->getSpelling()) + " is not allowed in .drectve"); |
Rui Ueyama | d7c2f58 | 2015-05-31 21:04:56 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 128 | // Find file from search paths. You can omit ".obj", this function takes |
| 129 | // care of that. Note that the returned path is not guaranteed to exist. |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 130 | StringRef LinkerDriver::doFindFile(StringRef Filename) { |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 131 | bool hasPathSep = (Filename.find_first_of("/\\") != StringRef::npos); |
| 132 | if (hasPathSep) |
| 133 | return Filename; |
| 134 | bool hasExt = (Filename.find('.') != StringRef::npos); |
| 135 | for (StringRef Dir : SearchPaths) { |
| 136 | SmallString<128> Path = Dir; |
| 137 | llvm::sys::path::append(Path, Filename); |
| 138 | if (llvm::sys::fs::exists(Path.str())) |
| 139 | return Alloc.save(Path.str()); |
| 140 | if (!hasExt) { |
| 141 | Path.append(".obj"); |
| 142 | if (llvm::sys::fs::exists(Path.str())) |
| 143 | return Alloc.save(Path.str()); |
| 144 | } |
| 145 | } |
| 146 | return Filename; |
| 147 | } |
| 148 | |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 149 | // Resolves a file path. This never returns the same path |
| 150 | // (in that case, it returns None). |
| 151 | Optional<StringRef> LinkerDriver::findFile(StringRef Filename) { |
| 152 | StringRef Path = doFindFile(Filename); |
| 153 | bool Seen = !VisitedFiles.insert(Path.lower()).second; |
| 154 | if (Seen) |
| 155 | return None; |
| 156 | return Path; |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 159 | // Find library file from search path. |
| 160 | StringRef LinkerDriver::doFindLib(StringRef Filename) { |
| 161 | // Add ".lib" to Filename if that has no file extension. |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 162 | bool hasExt = (Filename.find('.') != StringRef::npos); |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 163 | if (!hasExt) |
| 164 | Filename = Alloc.save(Filename + ".lib"); |
| 165 | return doFindFile(Filename); |
| 166 | } |
| 167 | |
| 168 | // Resolves a library path. /nodefaultlib options are taken into |
| 169 | // consideration. This never returns the same path (in that case, |
| 170 | // it returns None). |
| 171 | Optional<StringRef> LinkerDriver::findLib(StringRef Filename) { |
| 172 | if (Config->NoDefaultLibAll) |
| 173 | return None; |
| 174 | StringRef Path = doFindLib(Filename); |
| 175 | if (Config->NoDefaultLibs.count(Path)) |
| 176 | return None; |
| 177 | bool Seen = !VisitedFiles.insert(Path.lower()).second; |
| 178 | if (Seen) |
| 179 | return None; |
| 180 | return Path; |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // Parses LIB environment which contains a list of search paths. |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 184 | void LinkerDriver::addLibSearchPaths() { |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 185 | Optional<std::string> EnvOpt = Process::GetEnv("LIB"); |
| 186 | if (!EnvOpt.hasValue()) |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 187 | return; |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 188 | StringRef Env = Alloc.save(*EnvOpt); |
| 189 | while (!Env.empty()) { |
| 190 | StringRef Path; |
| 191 | std::tie(Path, Env) = Env.split(';'); |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 192 | SearchPaths.push_back(Path); |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 193 | } |
Rui Ueyama | 54b71da | 2015-05-31 19:17:12 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Rui Ueyama | 6bf638e | 2015-07-02 00:04:14 +0000 | [diff] [blame] | 196 | Undefined *LinkerDriver::addUndefined(StringRef Name) { |
| 197 | Undefined *U = Symtab.addUndefined(Name); |
Rui Ueyama | 18f8d2c | 2015-07-02 00:21:08 +0000 | [diff] [blame] | 198 | Config->GCRoot.insert(U); |
Rui Ueyama | 6bf638e | 2015-07-02 00:04:14 +0000 | [diff] [blame] | 199 | return U; |
Rui Ueyama | 32f8e1c | 2015-06-26 03:44:00 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Rui Ueyama | 7c3e23f | 2015-07-09 01:25:49 +0000 | [diff] [blame] | 202 | // Symbol names are mangled by appending "_" prefix on x86. |
| 203 | StringRef LinkerDriver::mangle(StringRef Sym) { |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 204 | assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN); |
| 205 | if (Config->Machine == I386) |
Rui Ueyama | 7c3e23f | 2015-07-09 01:25:49 +0000 | [diff] [blame] | 206 | return Alloc.save("_" + Sym); |
| 207 | return Sym; |
| 208 | } |
| 209 | |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 210 | // Windows specific -- find default entry point name. |
| 211 | StringRef LinkerDriver::findDefaultEntry() { |
| 212 | // User-defined main functions and their corresponding entry points. |
| 213 | static const char *Entries[][2] = { |
| 214 | {"main", "mainCRTStartup"}, |
| 215 | {"wmain", "wmainCRTStartup"}, |
| 216 | {"WinMain", "WinMainCRTStartup"}, |
| 217 | {"wWinMain", "wWinMainCRTStartup"}, |
| 218 | }; |
| 219 | for (auto E : Entries) { |
Rui Ueyama | a50387f | 2015-07-14 02:58:13 +0000 | [diff] [blame] | 220 | StringRef Entry = Symtab.findMangle(mangle(E[0])); |
| 221 | if (!Entry.empty() && !isa<Undefined>(Symtab.find(Entry)->Body)) |
Rui Ueyama | 7c3e23f | 2015-07-09 01:25:49 +0000 | [diff] [blame] | 222 | return mangle(E[1]); |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 223 | } |
| 224 | return ""; |
| 225 | } |
| 226 | |
| 227 | WindowsSubsystem LinkerDriver::inferSubsystem() { |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 228 | if (Config->DLL) |
| 229 | return IMAGE_SUBSYSTEM_WINDOWS_GUI; |
Rui Ueyama | 611add2 | 2015-08-08 00:23:37 +0000 | [diff] [blame] | 230 | if (Symtab.findUnderscore("main") || Symtab.findUnderscore("wmain")) |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 231 | return IMAGE_SUBSYSTEM_WINDOWS_CUI; |
Rui Ueyama | 611add2 | 2015-08-08 00:23:37 +0000 | [diff] [blame] | 232 | if (Symtab.findUnderscore("WinMain") || Symtab.findUnderscore("wWinMain")) |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 233 | return IMAGE_SUBSYSTEM_WINDOWS_GUI; |
| 234 | return IMAGE_SUBSYSTEM_UNKNOWN; |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Rui Ueyama | 5c437cd | 2015-07-25 21:42:33 +0000 | [diff] [blame] | 237 | static uint64_t getDefaultImageBase() { |
| 238 | if (Config->is64()) |
| 239 | return Config->DLL ? 0x180000000 : 0x140000000; |
| 240 | return Config->DLL ? 0x10000000 : 0x400000; |
| 241 | } |
| 242 | |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 243 | void LinkerDriver::link(llvm::ArrayRef<const char *> ArgsArr) { |
Rui Ueyama | 27e470a | 2015-08-09 20:45:17 +0000 | [diff] [blame] | 244 | // If the first command line argument is "/lib", link.exe acts like lib.exe. |
| 245 | // We call our own implementation of lib.exe that understands bitcode files. |
| 246 | if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) { |
| 247 | if (llvm::libDriverMain(ArgsArr.slice(1)) != 0) |
| 248 | error("lib failed"); |
| 249 | return; |
| 250 | } |
| 251 | |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 252 | // Needed for LTO. |
| 253 | llvm::InitializeAllTargetInfos(); |
| 254 | llvm::InitializeAllTargets(); |
| 255 | llvm::InitializeAllTargetMCs(); |
| 256 | llvm::InitializeAllAsmParsers(); |
| 257 | llvm::InitializeAllAsmPrinters(); |
| 258 | llvm::InitializeAllDisassemblers(); |
| 259 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 260 | // Parse command line options. |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 261 | llvm::opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1)); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 262 | |
Rui Ueyama | 5c72643 | 2015-05-29 16:11:52 +0000 | [diff] [blame] | 263 | // Handle /help |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 264 | if (Args.hasArg(OPT_help)) { |
David Blaikie | b2b1c7c | 2015-06-21 06:32:10 +0000 | [diff] [blame] | 265 | printHelp(ArgsArr[0]); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 266 | return; |
Rui Ueyama | 5c72643 | 2015-05-29 16:11:52 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 269 | if (Args.filtered_begin(OPT_INPUT) == Args.filtered_end()) |
| 270 | error("no input files."); |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 271 | |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 272 | // Construct search path list. |
| 273 | SearchPaths.push_back(""); |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 274 | for (auto *Arg : Args.filtered(OPT_libpath)) |
Rui Ueyama | f00df0a | 2015-06-19 22:39:48 +0000 | [diff] [blame] | 275 | SearchPaths.push_back(Arg->getValue()); |
| 276 | addLibSearchPaths(); |
| 277 | |
Rui Ueyama | ad66098 | 2015-06-07 00:20:32 +0000 | [diff] [blame] | 278 | // Handle /out |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 279 | if (auto *Arg = Args.getLastArg(OPT_out)) |
Rui Ueyama | ad66098 | 2015-06-07 00:20:32 +0000 | [diff] [blame] | 280 | Config->OutputFile = Arg->getValue(); |
| 281 | |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 282 | // Handle /verbose |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 283 | if (Args.hasArg(OPT_verbose)) |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 284 | Config->Verbose = true; |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 285 | |
Rui Ueyama | 95925fd | 2015-06-28 19:35:15 +0000 | [diff] [blame] | 286 | // Handle /force or /force:unresolved |
| 287 | if (Args.hasArg(OPT_force) || Args.hasArg(OPT_force_unresolved)) |
| 288 | Config->Force = true; |
| 289 | |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 290 | // Handle /debug |
| 291 | if (Args.hasArg(OPT_debug)) |
| 292 | Config->Debug = true; |
| 293 | |
Rui Ueyama | a8b6045 | 2015-06-28 19:56:30 +0000 | [diff] [blame] | 294 | // Handle /noentry |
| 295 | if (Args.hasArg(OPT_noentry)) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 296 | if (!Args.hasArg(OPT_dll)) |
| 297 | error("/noentry must be specified with /dll"); |
Rui Ueyama | a8b6045 | 2015-06-28 19:56:30 +0000 | [diff] [blame] | 298 | Config->NoEntry = true; |
| 299 | } |
| 300 | |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 301 | // Handle /dll |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 302 | if (Args.hasArg(OPT_dll)) { |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 303 | Config->DLL = true; |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 304 | Config->ManifestID = 2; |
| 305 | } |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 306 | |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 307 | // Handle /fixed |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 308 | if (Args.hasArg(OPT_fixed)) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 309 | if (Args.hasArg(OPT_dynamicbase)) |
| 310 | error("/fixed must not be specified with /dynamicbase"); |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 311 | Config->Relocatable = false; |
Rui Ueyama | 6592ff8 | 2015-06-16 23:13:00 +0000 | [diff] [blame] | 312 | Config->DynamicBase = false; |
| 313 | } |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 314 | |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 315 | // Handle /machine |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 316 | if (auto *Arg = Args.getLastArg(OPT_machine)) |
| 317 | Config->Machine = getMachineType(Arg->getValue()); |
Rui Ueyama | 3d3e6fb | 2015-05-29 16:06:00 +0000 | [diff] [blame] | 318 | |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 319 | // Handle /nodefaultlib:<filename> |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 320 | for (auto *Arg : Args.filtered(OPT_nodefaultlib)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 321 | Config->NoDefaultLibs.insert(doFindLib(Arg->getValue())); |
| 322 | |
| 323 | // Handle /nodefaultlib |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 324 | if (Args.hasArg(OPT_nodefaultlib_all)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 325 | Config->NoDefaultLibAll = true; |
| 326 | |
Rui Ueyama | 804a8b6 | 2015-05-29 16:18:15 +0000 | [diff] [blame] | 327 | // Handle /base |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 328 | if (auto *Arg = Args.getLastArg(OPT_base)) |
| 329 | parseNumbers(Arg->getValue(), &Config->ImageBase); |
Rui Ueyama | b41b7e5 | 2015-05-29 16:21:11 +0000 | [diff] [blame] | 330 | |
| 331 | // Handle /stack |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 332 | if (auto *Arg = Args.getLastArg(OPT_stack)) |
| 333 | parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit); |
Rui Ueyama | 804a8b6 | 2015-05-29 16:18:15 +0000 | [diff] [blame] | 334 | |
Rui Ueyama | c377e9a | 2015-05-29 16:23:40 +0000 | [diff] [blame] | 335 | // Handle /heap |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 336 | if (auto *Arg = Args.getLastArg(OPT_heap)) |
| 337 | parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit); |
Rui Ueyama | c377e9a | 2015-05-29 16:23:40 +0000 | [diff] [blame] | 338 | |
Rui Ueyama | b9dcdb5 | 2015-05-29 16:28:29 +0000 | [diff] [blame] | 339 | // Handle /version |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 340 | if (auto *Arg = Args.getLastArg(OPT_version)) |
| 341 | parseVersion(Arg->getValue(), &Config->MajorImageVersion, |
| 342 | &Config->MinorImageVersion); |
Rui Ueyama | b9dcdb5 | 2015-05-29 16:28:29 +0000 | [diff] [blame] | 343 | |
Rui Ueyama | 15cc47e | 2015-05-29 16:34:31 +0000 | [diff] [blame] | 344 | // Handle /subsystem |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 345 | if (auto *Arg = Args.getLastArg(OPT_subsystem)) |
| 346 | parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion, |
| 347 | &Config->MinorOSVersion); |
Rui Ueyama | 15cc47e | 2015-05-29 16:34:31 +0000 | [diff] [blame] | 348 | |
Rui Ueyama | 2edb35a | 2015-06-18 19:09:30 +0000 | [diff] [blame] | 349 | // Handle /alternatename |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 350 | for (auto *Arg : Args.filtered(OPT_alternatename)) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 351 | parseAlternateName(Arg->getValue()); |
Rui Ueyama | 2edb35a | 2015-06-18 19:09:30 +0000 | [diff] [blame] | 352 | |
Rui Ueyama | 08d5e18 | 2015-06-18 23:20:11 +0000 | [diff] [blame] | 353 | // Handle /include |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 354 | for (auto *Arg : Args.filtered(OPT_incl)) |
Rui Ueyama | 32f8e1c | 2015-06-26 03:44:00 +0000 | [diff] [blame] | 355 | addUndefined(Arg->getValue()); |
Rui Ueyama | 08d5e18 | 2015-06-18 23:20:11 +0000 | [diff] [blame] | 356 | |
Rui Ueyama | b95188c | 2015-06-18 20:27:09 +0000 | [diff] [blame] | 357 | // Handle /implib |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 358 | if (auto *Arg = Args.getLastArg(OPT_implib)) |
Rui Ueyama | b95188c | 2015-06-18 20:27:09 +0000 | [diff] [blame] | 359 | Config->Implib = Arg->getValue(); |
| 360 | |
Rui Ueyama | e2cbfea | 2015-06-07 03:17:42 +0000 | [diff] [blame] | 361 | // Handle /opt |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 362 | for (auto *Arg : Args.filtered(OPT_opt)) { |
Rui Ueyama | e2cbfea | 2015-06-07 03:17:42 +0000 | [diff] [blame] | 363 | std::string S = StringRef(Arg->getValue()).lower(); |
| 364 | if (S == "noref") { |
| 365 | Config->DoGC = false; |
| 366 | continue; |
| 367 | } |
Rui Ueyama | f799ede | 2015-06-25 23:26:58 +0000 | [diff] [blame] | 368 | if (S == "lldicf") { |
Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 369 | Config->ICF = true; |
| 370 | continue; |
| 371 | } |
Peter Collingbourne | 526ff15 | 2015-08-14 04:47:07 +0000 | [diff] [blame] | 372 | if (StringRef(S).startswith("lldlto=")) { |
| 373 | StringRef OptLevel = StringRef(S).substr(7); |
| 374 | if (OptLevel.getAsInteger(10, Config->LTOOptLevel) || |
Peter Collingbourne | df5783b | 2015-08-28 22:16:09 +0000 | [diff] [blame] | 375 | Config->LTOOptLevel > 3) |
Peter Collingbourne | 526ff15 | 2015-08-14 04:47:07 +0000 | [diff] [blame] | 376 | error("/opt:lldlto: invalid optimization level: " + OptLevel); |
Peter Collingbourne | df5783b | 2015-08-28 22:16:09 +0000 | [diff] [blame] | 377 | continue; |
| 378 | } |
| 379 | if (StringRef(S).startswith("lldltojobs=")) { |
| 380 | StringRef Jobs = StringRef(S).substr(11); |
| 381 | if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0) |
| 382 | error("/opt:lldltojobs: invalid job count: " + Jobs); |
Peter Collingbourne | 526ff15 | 2015-08-14 04:47:07 +0000 | [diff] [blame] | 383 | continue; |
| 384 | } |
Rui Ueyama | e2cbfea | 2015-06-07 03:17:42 +0000 | [diff] [blame] | 385 | if (S != "ref" && S != "icf" && S != "noicf" && |
| 386 | S != "lbr" && S != "nolbr" && |
| 387 | !StringRef(S).startswith("icf=")) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 388 | error(Twine("/opt: unknown option: ") + S); |
Rui Ueyama | e2cbfea | 2015-06-07 03:17:42 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Rui Ueyama | 8854d8a | 2015-06-04 19:21:24 +0000 | [diff] [blame] | 392 | // Handle /failifmismatch |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 393 | for (auto *Arg : Args.filtered(OPT_failifmismatch)) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 394 | checkFailIfMismatch(Arg->getValue()); |
Rui Ueyama | 8854d8a | 2015-06-04 19:21:24 +0000 | [diff] [blame] | 395 | |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 396 | // Handle /merge |
| 397 | for (auto *Arg : Args.filtered(OPT_merge)) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 398 | parseMerge(Arg->getValue()); |
Rui Ueyama | 6600eb1 | 2015-07-04 23:37:32 +0000 | [diff] [blame] | 399 | |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 400 | // Handle /manifest |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 401 | if (auto *Arg = Args.getLastArg(OPT_manifest_colon)) |
| 402 | parseManifest(Arg->getValue()); |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 403 | |
| 404 | // Handle /manifestuac |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 405 | if (auto *Arg = Args.getLastArg(OPT_manifestuac)) |
| 406 | parseManifestUAC(Arg->getValue()); |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 407 | |
| 408 | // Handle /manifestdependency |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 409 | if (auto *Arg = Args.getLastArg(OPT_manifestdependency)) |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 410 | Config->ManifestDependency = Arg->getValue(); |
| 411 | |
| 412 | // Handle /manifestfile |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 413 | if (auto *Arg = Args.getLastArg(OPT_manifestfile)) |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 414 | Config->ManifestFile = Arg->getValue(); |
| 415 | |
Rui Ueyama | 6592ff8 | 2015-06-16 23:13:00 +0000 | [diff] [blame] | 416 | // Handle miscellaneous boolean flags. |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 417 | if (Args.hasArg(OPT_allowbind_no)) |
| 418 | Config->AllowBind = false; |
| 419 | if (Args.hasArg(OPT_allowisolation_no)) |
| 420 | Config->AllowIsolation = false; |
| 421 | if (Args.hasArg(OPT_dynamicbase_no)) |
| 422 | Config->DynamicBase = false; |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 423 | if (Args.hasArg(OPT_nxcompat_no)) |
| 424 | Config->NxCompat = false; |
| 425 | if (Args.hasArg(OPT_tsaware_no)) |
| 426 | Config->TerminalServerAware = false; |
Rui Ueyama | 6592ff8 | 2015-06-16 23:13:00 +0000 | [diff] [blame] | 427 | |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 428 | // Create a list of input files. Files can be given as arguments |
| 429 | // for /defaultlib option. |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 430 | std::vector<StringRef> Paths; |
| 431 | std::vector<MemoryBufferRef> MBs; |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 432 | for (auto *Arg : Args.filtered(OPT_INPUT)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 433 | if (Optional<StringRef> Path = findFile(Arg->getValue())) |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 434 | Paths.push_back(*Path); |
David Blaikie | 6521ed9 | 2015-06-22 22:06:52 +0000 | [diff] [blame] | 435 | for (auto *Arg : Args.filtered(OPT_defaultlib)) |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 436 | if (Optional<StringRef> Path = findLib(Arg->getValue())) |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 437 | Paths.push_back(*Path); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 438 | for (StringRef Path : Paths) |
| 439 | MBs.push_back(openFile(Path)); |
Rui Ueyama | d21b00b | 2015-05-31 19:17:14 +0000 | [diff] [blame] | 440 | |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 441 | // Windows specific -- Create a resource file containing a manifest file. |
| 442 | if (Config->Manifest == Configuration::Embed) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 443 | std::unique_ptr<MemoryBuffer> MB = createManifestRes(); |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 444 | MBs.push_back(MB->getMemBufferRef()); |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 445 | OwningMBs.push_back(std::move(MB)); // take ownership |
| 446 | } |
| 447 | |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 448 | // Windows specific -- Input files can be Windows resource files (.res files). |
| 449 | // We invoke cvtres.exe to convert resource files to a regular COFF file |
| 450 | // then link the result file normally. |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 451 | std::vector<MemoryBufferRef> Resources; |
Rui Ueyama | 77731b4 | 2015-06-26 23:59:13 +0000 | [diff] [blame] | 452 | auto NotResource = [](MemoryBufferRef MB) { |
| 453 | return identify_magic(MB.getBuffer()) != file_magic::windows_resource; |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 454 | }; |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 455 | auto It = std::stable_partition(MBs.begin(), MBs.end(), NotResource); |
| 456 | if (It != MBs.end()) { |
| 457 | Resources.insert(Resources.end(), It, MBs.end()); |
| 458 | MBs.erase(It, MBs.end()); |
Rui Ueyama | 2bf6a12 | 2015-06-14 21:50:50 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Rui Ueyama | 85225b0 | 2015-07-02 03:15:15 +0000 | [diff] [blame] | 461 | // Read all input files given via the command line. Note that step() |
| 462 | // doesn't read files that are specified by directive sections. |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 463 | for (MemoryBufferRef MB : MBs) |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 464 | Symtab.addFile(createFile(MB)); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 465 | Symtab.step(); |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 466 | |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 467 | // Determine machine type and check if all object files are |
| 468 | // for the same CPU type. Note that this needs to be done before |
| 469 | // any call to mangle(). |
| 470 | for (std::unique_ptr<InputFile> &File : Symtab.getFiles()) { |
| 471 | MachineTypes MT = File->getMachineType(); |
| 472 | if (MT == IMAGE_FILE_MACHINE_UNKNOWN) |
| 473 | continue; |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 474 | if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) { |
| 475 | Config->Machine = MT; |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 476 | continue; |
| 477 | } |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 478 | if (Config->Machine != MT) |
| 479 | error(Twine(File->getShortName()) + ": machine type " + machineToStr(MT) + |
| 480 | " conflicts with " + machineToStr(Config->Machine)); |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 481 | } |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 482 | if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) { |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 483 | llvm::errs() << "warning: /machine is not specified. x64 is assumed.\n"; |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 484 | Config->Machine = AMD64; |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | // Windows specific -- Convert Windows resource files to a COFF file. |
| 488 | if (!Resources.empty()) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 489 | std::unique_ptr<MemoryBuffer> MB = convertResToCOFF(Resources); |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 490 | Symtab.addFile(createFile(MB->getMemBufferRef())); |
| 491 | OwningMBs.push_back(std::move(MB)); // take ownership |
| 492 | } |
| 493 | |
Rui Ueyama | 4d54534 | 2015-07-28 03:12:00 +0000 | [diff] [blame] | 494 | // Handle /largeaddressaware |
| 495 | if (Config->is64() || Args.hasArg(OPT_largeaddressaware)) |
| 496 | Config->LargeAddressAware = true; |
| 497 | |
Rui Ueyama | d68e211 | 2015-07-28 03:15:57 +0000 | [diff] [blame] | 498 | // Handle /highentropyva |
| 499 | if (Config->is64() && !Args.hasArg(OPT_highentropyva_no)) |
| 500 | Config->HighEntropyVA = true; |
| 501 | |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 502 | // Handle /entry and /dll |
| 503 | if (auto *Arg = Args.getLastArg(OPT_entry)) { |
| 504 | Config->Entry = addUndefined(mangle(Arg->getValue())); |
| 505 | } else if (Args.hasArg(OPT_dll) && !Config->NoEntry) { |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 506 | StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12" |
| 507 | : "_DllMainCRTStartup"; |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 508 | Config->Entry = addUndefined(S); |
| 509 | } else if (!Config->NoEntry) { |
| 510 | // Windows specific -- If entry point name is not given, we need to |
| 511 | // infer that from user-defined entry name. |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 512 | StringRef S = findDefaultEntry(); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 513 | if (S.empty()) |
| 514 | error("entry point must be defined"); |
Rui Ueyama | 6bf638e | 2015-07-02 00:04:14 +0000 | [diff] [blame] | 515 | Config->Entry = addUndefined(S); |
Rui Ueyama | 85225b0 | 2015-07-02 03:15:15 +0000 | [diff] [blame] | 516 | if (Config->Verbose) |
| 517 | llvm::outs() << "Entry name inferred: " << S << "\n"; |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 520 | // Handle /export |
| 521 | for (auto *Arg : Args.filtered(OPT_export)) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 522 | Export E = parseExport(Arg->getValue()); |
| 523 | if (Config->Machine == I386 && !E.Name.startswith("_@?")) |
| 524 | E.Name = mangle(E.Name); |
| 525 | Config->Exports.push_back(E); |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | // Handle /def |
| 529 | if (auto *Arg = Args.getLastArg(OPT_deffile)) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 530 | MemoryBufferRef MB = openFile(Arg->getValue()); |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 531 | // parseModuleDefs mutates Config object. |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 532 | parseModuleDefs(MB, &Alloc); |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Rui Ueyama | 6d24908 | 2015-07-13 22:31:45 +0000 | [diff] [blame] | 535 | // Handle /delayload |
| 536 | for (auto *Arg : Args.filtered(OPT_delayload)) { |
| 537 | Config->DelayLoads.insert(StringRef(Arg->getValue()).lower()); |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 538 | if (Config->Machine == I386) { |
Rui Ueyama | 6d24908 | 2015-07-13 22:31:45 +0000 | [diff] [blame] | 539 | Config->DelayLoadHelper = addUndefined("___delayLoadHelper2@8"); |
Rui Ueyama | 35ccb0f | 2015-07-25 00:20:06 +0000 | [diff] [blame] | 540 | } else { |
| 541 | Config->DelayLoadHelper = addUndefined("__delayLoadHelper2"); |
Rui Ueyama | 6d24908 | 2015-07-13 22:31:45 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
Rui Ueyama | 5c437cd | 2015-07-25 21:42:33 +0000 | [diff] [blame] | 545 | // Set default image base if /base is not given. |
| 546 | if (Config->ImageBase == uint64_t(-1)) |
| 547 | Config->ImageBase = getDefaultImageBase(); |
| 548 | |
Rui Ueyama | 3cb895c | 2015-07-24 22:58:44 +0000 | [diff] [blame] | 549 | Symtab.addRelative(mangle("__ImageBase"), 0); |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 550 | if (Config->Machine == I386) { |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 +0000 | [diff] [blame] | 551 | Config->SEHTable = Symtab.addRelative("___safe_se_handler_table", 0); |
| 552 | Config->SEHCount = Symtab.addAbsolute("___safe_se_handler_count", 0); |
| 553 | } |
Rui Ueyama | bbdec4f | 2015-07-09 22:51:41 +0000 | [diff] [blame] | 554 | |
Rui Ueyama | 107db55 | 2015-08-09 21:01:06 +0000 | [diff] [blame] | 555 | // We do not support /guard:cf (control flow protection) yet. |
| 556 | // Define CFG symbols anyway so that we can link MSVC 2015 CRT. |
| 557 | Symtab.addAbsolute(mangle("__guard_fids_table"), 0); |
| 558 | Symtab.addAbsolute(mangle("__guard_fids_count"), 0); |
| 559 | Symtab.addAbsolute(mangle("__guard_flags"), 0x100); |
| 560 | |
Rui Ueyama | ea533cd | 2015-07-09 19:54:13 +0000 | [diff] [blame] | 561 | // Read as much files as we can from directives sections. |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 562 | Symtab.run(); |
Rui Ueyama | 85225b0 | 2015-07-02 03:15:15 +0000 | [diff] [blame] | 563 | |
| 564 | // Resolve auxiliary symbols until we get a convergence. |
Rui Ueyama | 573bf7d | 2015-06-19 21:12:48 +0000 | [diff] [blame] | 565 | // (Trying to resolve a symbol may trigger a Lazy symbol to load a new file. |
| 566 | // A new file may contain a directive section to add new command line options. |
| 567 | // That's why we have to repeat until converge.) |
| 568 | for (;;) { |
Rui Ueyama | 6bf638e | 2015-07-02 00:04:14 +0000 | [diff] [blame] | 569 | // Windows specific -- if entry point is not found, |
| 570 | // search for its mangled names. |
| 571 | if (Config->Entry) |
| 572 | Symtab.mangleMaybe(Config->Entry); |
| 573 | |
Rui Ueyama | 573bf7d | 2015-06-19 21:12:48 +0000 | [diff] [blame] | 574 | // Windows specific -- Make sure we resolve all dllexported symbols. |
Rui Ueyama | 6bf638e | 2015-07-02 00:04:14 +0000 | [diff] [blame] | 575 | for (Export &E : Config->Exports) { |
| 576 | E.Sym = addUndefined(E.Name); |
| 577 | Symtab.mangleMaybe(E.Sym); |
| 578 | } |
Rui Ueyama | 573bf7d | 2015-06-19 21:12:48 +0000 | [diff] [blame] | 579 | |
| 580 | // Add weak aliases. Weak aliases is a mechanism to give remaining |
| 581 | // undefined symbols final chance to be resolved successfully. |
Rui Ueyama | 3d4c69c | 2015-07-02 02:38:59 +0000 | [diff] [blame] | 582 | for (auto Pair : Config->AlternateNames) { |
| 583 | StringRef From = Pair.first; |
| 584 | StringRef To = Pair.second; |
Rui Ueyama | 458d744 | 2015-07-02 03:59:04 +0000 | [diff] [blame] | 585 | Symbol *Sym = Symtab.find(From); |
Rui Ueyama | 3d4c69c | 2015-07-02 02:38:59 +0000 | [diff] [blame] | 586 | if (!Sym) |
| 587 | continue; |
Rui Ueyama | 183f53f | 2015-07-06 17:45:22 +0000 | [diff] [blame] | 588 | if (auto *U = dyn_cast<Undefined>(Sym->Body)) |
Rui Ueyama | 3d4c69c | 2015-07-02 02:38:59 +0000 | [diff] [blame] | 589 | if (!U->WeakAlias) |
| 590 | U->WeakAlias = Symtab.addUndefined(To); |
Rui Ueyama | 2edb35a | 2015-06-18 19:09:30 +0000 | [diff] [blame] | 591 | } |
Rui Ueyama | 573bf7d | 2015-06-19 21:12:48 +0000 | [diff] [blame] | 592 | |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 +0000 | [diff] [blame] | 593 | // Windows specific -- if __load_config_used can be resolved, resolve it. |
Rui Ueyama | 8ebdc8c | 2015-08-07 22:43:53 +0000 | [diff] [blame] | 594 | if (Symtab.findUnderscore("_load_config_used")) |
| 595 | addUndefined(mangle("_load_config_used")); |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 +0000 | [diff] [blame] | 596 | |
Rui Ueyama | 3d4c69c | 2015-07-02 02:38:59 +0000 | [diff] [blame] | 597 | if (Symtab.queueEmpty()) |
| 598 | break; |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 599 | Symtab.run(); |
Rui Ueyama | 2edb35a | 2015-06-18 19:09:30 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Peter Collingbourne | df5783b | 2015-08-28 22:16:09 +0000 | [diff] [blame] | 602 | // Do LTO by compiling bitcode input files to a set of native COFF files then |
| 603 | // link those files. |
| 604 | Symtab.addCombinedLTOObjects(); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 605 | |
Peter Collingbourne | 2612a32 | 2015-07-04 05:28:41 +0000 | [diff] [blame] | 606 | // Make sure we have resolved all symbols. |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 607 | Symtab.reportRemainingUndefines(/*Resolve=*/true); |
Peter Collingbourne | 2612a32 | 2015-07-04 05:28:41 +0000 | [diff] [blame] | 608 | |
Rui Ueyama | 3ee0fe4 | 2015-05-31 03:55:46 +0000 | [diff] [blame] | 609 | // Windows specific -- if no /subsystem is given, we need to infer |
| 610 | // that from entry point name. |
| 611 | if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) { |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 612 | Config->Subsystem = inferSubsystem(); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 613 | if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) |
| 614 | error("subsystem must be defined"); |
Rui Ueyama | 3ee0fe4 | 2015-05-31 03:55:46 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Rui Ueyama | ff88d5a | 2015-07-29 20:25:40 +0000 | [diff] [blame] | 617 | // Handle /safeseh. |
| 618 | if (Args.hasArg(OPT_safeseh)) { |
| 619 | for (ObjectFile *File : Symtab.ObjectFiles) { |
| 620 | if (File->SEHCompat) |
| 621 | continue; |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 622 | error(Twine("/safeseh: ") + File->getName() + |
| 623 | " is not compatible with SEH"); |
Rui Ueyama | ff88d5a | 2015-07-29 20:25:40 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
Rui Ueyama | 151d862 | 2015-06-17 20:40:43 +0000 | [diff] [blame] | 627 | // Windows specific -- when we are creating a .dll file, we also |
| 628 | // need to create a .lib file. |
Rui Ueyama | 8765fba | 2015-07-15 22:21:08 +0000 | [diff] [blame] | 629 | if (!Config->Exports.empty()) { |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 630 | fixupExports(); |
| 631 | writeImportLibrary(); |
Rui Ueyama | 8765fba | 2015-07-15 22:21:08 +0000 | [diff] [blame] | 632 | assignExportOrdinals(); |
| 633 | } |
Rui Ueyama | 97dff9e | 2015-06-17 00:16:33 +0000 | [diff] [blame] | 634 | |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 635 | // Windows specific -- Create a side-by-side manifest file. |
| 636 | if (Config->Manifest == Configuration::SideBySide) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 637 | createSideBySideManifest(); |
Rui Ueyama | 24c5fd0 | 2015-06-18 00:12:42 +0000 | [diff] [blame] | 638 | |
Rui Ueyama | 0fc26d2 | 2015-06-29 14:27:12 +0000 | [diff] [blame] | 639 | // Create a dummy PDB file to satisfy build sytem rules. |
| 640 | if (auto *Arg = Args.getLastArg(OPT_pdb)) |
| 641 | touchFile(Arg->getValue()); |
| 642 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 643 | // Write the result. |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 644 | writeResult(&Symtab); |
Peter Collingbourne | be54955 | 2015-06-26 18:58:24 +0000 | [diff] [blame] | 645 | |
Rui Ueyama | 016414f | 2015-06-28 20:07:08 +0000 | [diff] [blame] | 646 | // Create a symbol map file containing symbol VAs and their names |
| 647 | // to help debugging. |
Peter Collingbourne | be54955 | 2015-06-26 18:58:24 +0000 | [diff] [blame] | 648 | if (auto *Arg = Args.getLastArg(OPT_lldmap)) { |
| 649 | std::error_code EC; |
Peter Collingbourne | baf5f87 | 2015-06-26 19:20:09 +0000 | [diff] [blame] | 650 | llvm::raw_fd_ostream Out(Arg->getValue(), EC, OpenFlags::F_Text); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 651 | error(EC, "Could not create the symbol map"); |
Peter Collingbourne | be54955 | 2015-06-26 18:58:24 +0000 | [diff] [blame] | 652 | Symtab.printMap(Out); |
| 653 | } |
Rui Ueyama | a51ce71 | 2015-07-03 05:31:35 +0000 | [diff] [blame] | 654 | // Call exit to avoid calling destructors. |
| 655 | exit(0); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 658 | } // namespace coff |
| 659 | } // namespace lld |