| Michael J. Spencer | 9ff4be2 | 2012-12-08 00:47:36 +0000 | [diff] [blame] | 1 | //===- lib/Driver/Driver.cpp - Linker Driver Emulator ---------------------===// |
| 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 "lld/Driver/Driver.h" |
| Rui Ueyama | a5f2816 | 2015-01-06 23:06:49 +0000 | [diff] [blame] | 11 | #include "lld/Core/ArchiveLibraryFile.h" |
| 12 | #include "lld/Core/File.h" |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 13 | #include "lld/Core/Instrumentation.h" |
| Shankar Easwaran | 2b67fca | 2014-10-18 05:33:55 +0000 | [diff] [blame] | 14 | #include "lld/Core/LLVM.h" |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 15 | #include "lld/Core/Parallel.h" |
| Shankar Easwaran | 2b67fca | 2014-10-18 05:33:55 +0000 | [diff] [blame] | 16 | #include "lld/Core/PassManager.h" |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 17 | #include "lld/Core/Resolver.h" |
| Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 18 | #include "lld/Passes/RoundTripNativePass.h" |
| 19 | #include "lld/Passes/RoundTripYAMLPass.h" |
| Shankar Easwaran | 2b67fca | 2014-10-18 05:33:55 +0000 | [diff] [blame] | 20 | #include "lld/ReaderWriter/Reader.h" |
| 21 | #include "lld/ReaderWriter/Writer.h" |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringExtras.h" |
| 23 | #include "llvm/ADT/StringSwitch.h" |
| 24 | #include "llvm/Option/Arg.h" |
| 25 | #include "llvm/Support/CommandLine.h" |
| 26 | #include "llvm/Support/FileSystem.h" |
| 27 | #include "llvm/Support/Path.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
| Rui Ueyama | 74cc8e1 | 2013-10-08 23:01:52 +0000 | [diff] [blame] | 29 | #include <mutex> |
| 30 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 31 | namespace lld { |
| 32 | |
| Rui Ueyama | a5f2816 | 2015-01-06 23:06:49 +0000 | [diff] [blame] | 33 | FileVector makeErrorFile(StringRef path, std::error_code ec) { |
| 34 | std::vector<std::unique_ptr<File>> result; |
| 35 | result.push_back(llvm::make_unique<ErrorFile>(path, ec)); |
| 36 | return result; |
| 37 | } |
| 38 | |
| 39 | FileVector parseMemberFiles(FileVector &files) { |
| 40 | std::vector<std::unique_ptr<File>> members; |
| 41 | for (std::unique_ptr<File> &file : files) { |
| 42 | if (auto *archive = dyn_cast<ArchiveLibraryFile>(file.get())) { |
| 43 | if (std::error_code ec = archive->parseAllMembers(members)) |
| 44 | return makeErrorFile(file->path(), ec); |
| 45 | } else { |
| 46 | members.push_back(std::move(file)); |
| 47 | } |
| 48 | } |
| 49 | return members; |
| 50 | } |
| 51 | |
| Rui Ueyama | df230b2 | 2015-01-15 04:34:31 +0000 | [diff] [blame] | 52 | FileVector loadFile(LinkingContext &ctx, StringRef path, bool wholeArchive) { |
| Rui Ueyama | a5f2816 | 2015-01-06 23:06:49 +0000 | [diff] [blame] | 53 | ErrorOr<std::unique_ptr<MemoryBuffer>> mb |
| 54 | = MemoryBuffer::getFileOrSTDIN(path); |
| 55 | if (std::error_code ec = mb.getError()) |
| 56 | return makeErrorFile(path, ec); |
| 57 | std::vector<std::unique_ptr<File>> files; |
| Rui Ueyama | df230b2 | 2015-01-15 04:34:31 +0000 | [diff] [blame] | 58 | if (std::error_code ec = ctx.registry().loadFile(std::move(mb.get()), files)) |
| Rui Ueyama | a5f2816 | 2015-01-06 23:06:49 +0000 | [diff] [blame] | 59 | return makeErrorFile(path, ec); |
| 60 | if (wholeArchive) |
| 61 | return parseMemberFiles(files); |
| 62 | return files; |
| 63 | } |
| 64 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 65 | /// This is where the link is actually performed. |
| Shankar Easwaran | 7f1146c | 2013-10-08 15:43:48 +0000 | [diff] [blame] | 66 | bool Driver::link(LinkingContext &context, raw_ostream &diagnostics) { |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 67 | // Honor -mllvm |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 68 | if (!context.llvmOptions().empty()) { |
| 69 | unsigned numArgs = context.llvmOptions().size(); |
| 70 | const char **args = new const char *[numArgs + 2]; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 71 | args[0] = "lld (LLVM option parsing)"; |
| 72 | for (unsigned i = 0; i != numArgs; ++i) |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 73 | args[i + 1] = context.llvmOptions()[i]; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 74 | args[numArgs + 1] = 0; |
| 75 | llvm::cl::ParseCommandLineOptions(numArgs + 1, args); |
| 76 | } |
| Rui Ueyama | 8bd093b | 2014-04-04 00:14:04 +0000 | [diff] [blame] | 77 | InputGraph &inputGraph = context.getInputGraph(); |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 78 | if (!inputGraph.size()) |
| Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 79 | return false; |
| Rui Ueyama | cfb2534 | 2015-01-14 00:21:34 +0000 | [diff] [blame] | 80 | inputGraph.normalize(); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 81 | |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 82 | bool fail = false; |
| 83 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 84 | // Read inputs |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 85 | ScopedTask readTask(getDefaultDomain(), "Read Args"); |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 86 | TaskGroup tg; |
| Rui Ueyama | 74cc8e1 | 2013-10-08 23:01:52 +0000 | [diff] [blame] | 87 | std::mutex diagnosticsMutex; |
| Rui Ueyama | 69fcde8 | 2014-04-03 20:47:50 +0000 | [diff] [blame] | 88 | for (std::unique_ptr<InputElement> &ie : inputGraph.inputElements()) { |
| Rui Ueyama | a1490d6 | 2013-10-11 22:02:58 +0000 | [diff] [blame] | 89 | tg.spawn([&] { |
| Rui Ueyama | 74cc8e1 | 2013-10-08 23:01:52 +0000 | [diff] [blame] | 90 | // Writes to the same output stream is not guaranteed to be thread-safe. |
| 91 | // We buffer the diagnostics output to a separate string-backed output |
| 92 | // stream, acquire the lock, and then print it out. |
| 93 | std::string buf; |
| 94 | llvm::raw_string_ostream stream(buf); |
| 95 | |
| Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 96 | if (std::error_code ec = ie->parse(context, stream)) { |
| Simon Atanasyan | a64f34b | 2014-04-30 19:04:01 +0000 | [diff] [blame] | 97 | if (FileNode *fileNode = dyn_cast<FileNode>(ie.get())) |
| 98 | stream << fileNode->errStr(ec) << "\n"; |
| Simon Atanasyan | a64f34b | 2014-04-30 19:04:01 +0000 | [diff] [blame] | 99 | else |
| 100 | llvm_unreachable("Unknown type of input element"); |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 101 | fail = true; |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 102 | } |
| Rui Ueyama | 74cc8e1 | 2013-10-08 23:01:52 +0000 | [diff] [blame] | 103 | |
| 104 | stream.flush(); |
| 105 | if (!buf.empty()) { |
| 106 | std::lock_guard<std::mutex> lock(diagnosticsMutex); |
| 107 | diagnostics << buf; |
| 108 | } |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 109 | }); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 110 | } |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 111 | tg.sync(); |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 112 | readTask.end(); |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 113 | |
| 114 | if (fail) |
| Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 115 | return false; |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 116 | |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 117 | InputGraph::FileVectorT internalFiles; |
| 118 | context.createInternalFiles(internalFiles); |
| Rui Ueyama | 00eb257 | 2014-12-10 00:33:00 +0000 | [diff] [blame] | 119 | for (auto i = internalFiles.rbegin(), e = internalFiles.rend(); i != e; ++i) { |
| 120 | context.getInputGraph().addInputElementFront( |
| 121 | llvm::make_unique<SimpleFileNode>("internal", std::move(*i))); |
| 122 | } |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 123 | |
| 124 | // Give target a chance to add files. |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 125 | InputGraph::FileVectorT implicitFiles; |
| 126 | context.createImplicitFiles(implicitFiles); |
| Rui Ueyama | 00eb257 | 2014-12-10 00:33:00 +0000 | [diff] [blame] | 127 | for (auto i = implicitFiles.rbegin(), e = implicitFiles.rend(); i != e; ++i) { |
| 128 | context.getInputGraph().addInputElementFront( |
| 129 | llvm::make_unique<SimpleFileNode>("implicit", std::move(*i))); |
| 130 | } |
| 131 | |
| 132 | // Give target a chance to sort the input files. |
| 133 | // Mach-O uses this chance to move all object files before library files. |
| 134 | context.maybeSortInputFiles(); |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 135 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 136 | // Do core linking. |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 137 | ScopedTask resolveTask(getDefaultDomain(), "Resolve"); |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 138 | Resolver resolver(context); |
| Rui Ueyama | f1e2668 | 2013-10-11 06:16:35 +0000 | [diff] [blame] | 139 | if (!resolver.resolve()) |
| 140 | return false; |
| Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 141 | std::unique_ptr<MutableFile> merged = resolver.resultFile(); |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 142 | resolveTask.end(); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 143 | |
| 144 | // Run passes on linked atoms. |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 145 | ScopedTask passTask(getDefaultDomain(), "Passes"); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 146 | PassManager pm; |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 147 | context.addPasses(pm); |
| Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 148 | |
| Rui Ueyama | aa5b4dc | 2013-11-01 21:05:42 +0000 | [diff] [blame] | 149 | #ifndef NDEBUG |
| Shankar Easwaran | 2895527 | 2014-12-01 01:04:11 +0000 | [diff] [blame] | 150 | if (context.runRoundTripPass()) { |
| Rui Ueyama | bc6b52e | 2014-03-20 02:49:33 +0000 | [diff] [blame] | 151 | pm.add(std::unique_ptr<Pass>(new RoundTripYAMLPass(context))); |
| 152 | pm.add(std::unique_ptr<Pass>(new RoundTripNativePass(context))); |
| 153 | } |
| Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 154 | #endif |
| 155 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 156 | pm.runOnFile(merged); |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 157 | passTask.end(); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 158 | |
| 159 | // Give linked atoms to Writer to generate output file. |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 160 | ScopedTask writeTask(getDefaultDomain(), "Write"); |
| Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 161 | if (std::error_code ec = context.writeFile(*merged)) { |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 162 | diagnostics << "Failed to write file '" << context.outputPath() |
| Rafael Espindola | 66c0a65 | 2013-07-15 23:55:07 +0000 | [diff] [blame] | 163 | << "': " << ec.message() << "\n"; |
| Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 164 | return false; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 167 | return true; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 170 | } // namespace |