| 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" |
| 13 | #include "lld/Core/InputFiles.h" |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 14 | #include "lld/Core/Instrumentation.h" |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 15 | #include "lld/Core/PassManager.h" |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 16 | #include "lld/Core/Parallel.h" |
| 17 | #include "lld/Core/Resolver.h" |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 18 | #include "lld/ReaderWriter/Reader.h" |
| 19 | #include "lld/ReaderWriter/Writer.h" |
| Michael J. Spencer | 9ff4be2 | 2012-12-08 00:47:36 +0000 | [diff] [blame] | 20 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
| 22 | #include "llvm/ADT/StringSwitch.h" |
| 23 | #include "llvm/Option/Arg.h" |
| 24 | #include "llvm/Support/CommandLine.h" |
| 25 | #include "llvm/Support/FileSystem.h" |
| 26 | #include "llvm/Support/Path.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| Michael J. Spencer | 9ff4be2 | 2012-12-08 00:47:36 +0000 | [diff] [blame] | 28 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 29 | namespace lld { |
| 30 | |
| 31 | /// This is where the link is actually performed. |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame^] | 32 | bool Driver::link(const LinkingContext &context, raw_ostream &diagnostics) { |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 33 | // Honor -mllvm |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame^] | 34 | if (!context.llvmOptions().empty()) { |
| 35 | unsigned numArgs = context.llvmOptions().size(); |
| 36 | const char **args = new const char *[numArgs + 2]; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 37 | args[0] = "lld (LLVM option parsing)"; |
| 38 | for (unsigned i = 0; i != numArgs; ++i) |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame^] | 39 | args[i + 1] = context.llvmOptions()[i]; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 40 | args[numArgs + 1] = 0; |
| 41 | llvm::cl::ParseCommandLineOptions(numArgs + 1, args); |
| 42 | } |
| 43 | |
| 44 | // Read inputs |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 45 | ScopedTask readTask(getDefaultDomain(), "Read Args"); |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame^] | 46 | std::vector<std::vector<std::unique_ptr<File>> > files( |
| 47 | context.inputFiles().size()); |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 48 | size_t index = 0; |
| 49 | std::atomic<bool> fail(false); |
| 50 | TaskGroup tg; |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame^] | 51 | for (const auto &input : context.inputFiles()) { |
| 52 | if (context.logInputFiles()) |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 53 | llvm::outs() << input.getPath() << "\n"; |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 54 | |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame^] | 55 | tg.spawn([ &, index]{ |
| 56 | if (error_code ec = context.readFile(input.getPath(), files[index])) { |
| 57 | diagnostics << "Failed to read file: " << input.getPath() << ": " |
| 58 | << ec.message() << "\n"; |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 59 | fail = true; |
| 60 | return; |
| 61 | } |
| 62 | }); |
| 63 | ++index; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 64 | } |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 65 | tg.sync(); |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 66 | readTask.end(); |
| Michael J. Spencer | e5b8fe1 | 2013-05-28 18:37:39 +0000 | [diff] [blame] | 67 | |
| 68 | if (fail) |
| 69 | return true; |
| 70 | |
| 71 | InputFiles inputs; |
| 72 | for (auto &f : files) |
| 73 | inputs.appendFiles(f); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 74 | |
| 75 | // Give target a chance to add files. |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame^] | 76 | context.addImplicitFiles(inputs); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 77 | |
| 78 | // assign an ordinal to each file so sort() can preserve command line order |
| 79 | inputs.assignFileOrdinals(); |
| 80 | |
| 81 | // Do core linking. |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 82 | ScopedTask resolveTask(getDefaultDomain(), "Resolve"); |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame^] | 83 | Resolver resolver(context, inputs); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 84 | if (resolver.resolve()) { |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame^] | 85 | if (!context.allowRemainingUndefines()) |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 86 | return true; |
| 87 | } |
| 88 | MutableFile &merged = resolver.resultFile(); |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 89 | resolveTask.end(); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 90 | |
| 91 | // Run passes on linked atoms. |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 92 | ScopedTask passTask(getDefaultDomain(), "Passes"); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 93 | PassManager pm; |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame^] | 94 | context.addPasses(pm); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 95 | pm.runOnFile(merged); |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 96 | passTask.end(); |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 97 | |
| 98 | // Give linked atoms to Writer to generate output file. |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 99 | ScopedTask writeTask(getDefaultDomain(), "Write"); |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame^] | 100 | if (error_code ec = context.writeFile(merged)) { |
| 101 | diagnostics << "Failed to write file '" << context.outputPath() |
| Rafael Espindola | 66c0a65 | 2013-07-15 23:55:07 +0000 | [diff] [blame] | 102 | << "': " << ec.message() << "\n"; |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | return false; |
| 107 | } |
| 108 | |
| Nick Kledzik | c314b46 | 2013-04-04 18:59:24 +0000 | [diff] [blame] | 109 | } // namespace |