| 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" |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 11 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 12 | #include "lld/Core/LLVM.h" |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 13 | #include "lld/Core/Instrumentation.h" |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 14 | #include "lld/Core/PassManager.h" |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 15 | #include "lld/Core/Parallel.h" |
| 16 | #include "lld/Core/Resolver.h" |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 17 | #include "lld/ReaderWriter/Reader.h" |
| 18 | #include "lld/ReaderWriter/Writer.h" |
| Michael J. Spencer | 9ff4be2 | 2012-12-08 00:47:36 +0000 | [diff] [blame] | 19 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
| 21 | #include "llvm/ADT/StringSwitch.h" |
| 22 | #include "llvm/Option/Arg.h" |
| 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/FileSystem.h" |
| 25 | #include "llvm/Support/Path.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| Michael J. Spencer | 9ff4be2 | 2012-12-08 00:47:36 +0000 | [diff] [blame] | 27 | |
| Rui Ueyama | 74cc8e1 | 2013-10-08 23:01:52 +0000 | [diff] [blame] | 28 | #include <mutex> |
| 29 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 30 | namespace lld { |
| 31 | |
| 32 | /// This is where the link is actually performed. |
| Shankar Easwaran | 7f1146c | 2013-10-08 15:43:48 +0000 | [diff] [blame] | 33 | bool Driver::link(LinkingContext &context, raw_ostream &diagnostics) { |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 34 | // Honor -mllvm |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 35 | if (!context.llvmOptions().empty()) { |
| 36 | unsigned numArgs = context.llvmOptions().size(); |
| 37 | const char **args = new const char *[numArgs + 2]; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 38 | args[0] = "lld (LLVM option parsing)"; |
| 39 | for (unsigned i = 0; i != numArgs; ++i) |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 40 | args[i + 1] = context.llvmOptions()[i]; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 41 | args[numArgs + 1] = 0; |
| 42 | llvm::cl::ParseCommandLineOptions(numArgs + 1, args); |
| 43 | } |
| Shankar Easwaran | e44104b | 2013-08-21 22:57:10 +0000 | [diff] [blame] | 44 | InputGraph &inputGraph = context.inputGraph(); |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 45 | if (!inputGraph.size()) |
| Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 46 | return false; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 47 | |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 48 | bool fail = false; |
| 49 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 50 | // Read inputs |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 51 | ScopedTask readTask(getDefaultDomain(), "Read Args"); |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 52 | TaskGroup tg; |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 53 | int index = 0; |
| Rui Ueyama | 74cc8e1 | 2013-10-08 23:01:52 +0000 | [diff] [blame] | 54 | std::mutex diagnosticsMutex; |
| Shankar Easwaran | d26c8e3 | 2013-08-31 05:27:38 +0000 | [diff] [blame] | 55 | for (auto &ie : inputGraph.inputElements()) { |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 56 | tg.spawn([&, index] { |
| Rui Ueyama | 74cc8e1 | 2013-10-08 23:01:52 +0000 | [diff] [blame] | 57 | // Writes to the same output stream is not guaranteed to be thread-safe. |
| 58 | // We buffer the diagnostics output to a separate string-backed output |
| 59 | // stream, acquire the lock, and then print it out. |
| 60 | std::string buf; |
| 61 | llvm::raw_string_ostream stream(buf); |
| 62 | |
| 63 | if (error_code ec = ie->parse(context, stream)) { |
| 64 | FileNode *fileNode = llvm::dyn_cast<FileNode>(ie.get()); |
| 65 | stream << fileNode->errStr(ec) << "\n"; |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 66 | fail = true; |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 67 | } |
| Rui Ueyama | 74cc8e1 | 2013-10-08 23:01:52 +0000 | [diff] [blame] | 68 | |
| 69 | stream.flush(); |
| 70 | if (!buf.empty()) { |
| 71 | std::lock_guard<std::mutex> lock(diagnosticsMutex); |
| 72 | diagnostics << buf; |
| 73 | } |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 74 | }); |
| 75 | ++index; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 76 | } |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 77 | tg.sync(); |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 78 | readTask.end(); |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 79 | |
| 80 | if (fail) |
| Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 81 | return false; |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 82 | |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 83 | std::unique_ptr<SimpleFileNode> fileNode( |
| 84 | new SimpleFileNode("Internal Files")); |
| Shankar Easwaran | d26c8e3 | 2013-08-31 05:27:38 +0000 | [diff] [blame] | 85 | |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 86 | InputGraph::FileVectorT internalFiles; |
| 87 | context.createInternalFiles(internalFiles); |
| Shankar Easwaran | d26c8e3 | 2013-08-31 05:27:38 +0000 | [diff] [blame] | 88 | |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 89 | if (internalFiles.size()) { |
| 90 | fileNode->appendInputFiles(std::move(internalFiles)); |
| 91 | } |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 92 | |
| 93 | // Give target a chance to add files. |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 94 | InputGraph::FileVectorT implicitFiles; |
| 95 | context.createImplicitFiles(implicitFiles); |
| 96 | if (implicitFiles.size()) { |
| 97 | fileNode->appendInputFiles(std::move(implicitFiles)); |
| 98 | } |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 99 | |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 100 | context.inputGraph().insertOneElementAt(std::move(fileNode), |
| 101 | InputGraph::Position::BEGIN); |
| 102 | |
| 103 | context.inputGraph().assignOrdinals(); |
| 104 | |
| 105 | context.inputGraph().doPostProcess(); |
| 106 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 107 | // Do core linking. |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 108 | ScopedTask resolveTask(getDefaultDomain(), "Resolve"); |
| Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 109 | Resolver resolver(context); |
| Rui Ueyama | ee36604 | 2013-10-11 06:16:33 +0000 | [diff] [blame^] | 110 | if (!resolver.resolve()) { |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 111 | if (!context.allowRemainingUndefines()) |
| Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 112 | return false; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 113 | } |
| 114 | MutableFile &merged = resolver.resultFile(); |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 115 | resolveTask.end(); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 116 | |
| 117 | // Run passes on linked atoms. |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 118 | ScopedTask passTask(getDefaultDomain(), "Passes"); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 119 | PassManager pm; |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 120 | context.addPasses(pm); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 121 | pm.runOnFile(merged); |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 122 | passTask.end(); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 123 | |
| 124 | // Give linked atoms to Writer to generate output file. |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 125 | ScopedTask writeTask(getDefaultDomain(), "Write"); |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 126 | if (error_code ec = context.writeFile(merged)) { |
| 127 | diagnostics << "Failed to write file '" << context.outputPath() |
| Rafael Espindola | 66c0a65 | 2013-07-15 23:55:07 +0000 | [diff] [blame] | 128 | << "': " << ec.message() << "\n"; |
| Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 129 | return false; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| Rui Ueyama | 8db1edd | 2013-09-24 23:26:34 +0000 | [diff] [blame] | 132 | return true; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 135 | } // namespace |