Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 1 | //===-- gold-plugin.cpp - Plugin to gold for Link Time Optimization ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This is a gold plugin for LLVM. It provides an LLVM implementation of the |
| 11 | // interface described in http://gcc.gnu.org/wiki/whopr/driver . |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 15 | #include "llvm/Bitcode/ReaderWriter.h" |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/CommandFlags.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 17 | #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H |
Rafael Espindola | 890db27 | 2014-09-09 20:08:22 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Constants.h" |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DiagnosticPrinter.h" |
Teresa Johnson | 683abe7 | 2016-05-26 01:46:41 +0000 | [diff] [blame] | 20 | #include "llvm/LTO/LTO.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
Rafael Espindola | 947bdb6 | 2014-11-25 20:52:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Path.h" |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 25 | #include "llvm/Support/TargetSelect.h" |
Teresa Johnson | b13dbd6 | 2015-12-09 19:45:55 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 27 | #include <list> |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 28 | #include <map> |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 29 | #include <plugin-api.h> |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 30 | #include <string> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 31 | #include <system_error> |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 32 | #include <utility> |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 33 | #include <vector> |
| 34 | |
Sylvestre Ledru | 5399979 | 2014-02-11 17:30:18 +0000 | [diff] [blame] | 35 | // FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and |
| 36 | // Precise and Debian Wheezy (binutils 2.23 is required) |
Evgeniy Stepanov | 330c5a6 | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 37 | #define LDPO_PIE 3 |
| 38 | |
| 39 | #define LDPT_GET_SYMBOLS_V3 28 |
Sylvestre Ledru | 5399979 | 2014-02-11 17:30:18 +0000 | [diff] [blame] | 40 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 41 | using namespace llvm; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 42 | using namespace lto; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 43 | |
Teresa Johnson | cb15b73 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 44 | static ld_plugin_status discard_message(int level, const char *format, ...) { |
| 45 | // Die loudly. Recent versions of Gold pass ld_plugin_message as the first |
| 46 | // callback in the transfer vector. This should never be called. |
| 47 | abort(); |
| 48 | } |
| 49 | |
| 50 | static ld_plugin_release_input_file release_input_file = nullptr; |
| 51 | static ld_plugin_get_input_file get_input_file = nullptr; |
| 52 | static ld_plugin_message message = discard_message; |
| 53 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 54 | namespace { |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 55 | struct claimed_file { |
| 56 | void *handle; |
Teresa Johnson | 683abe7 | 2016-05-26 01:46:41 +0000 | [diff] [blame] | 57 | void *leader_handle; |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 58 | std::vector<ld_plugin_symbol> syms; |
Teresa Johnson | 683abe7 | 2016-05-26 01:46:41 +0000 | [diff] [blame] | 59 | off_t filesize; |
| 60 | std::string name; |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 61 | }; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 62 | |
Teresa Johnson | cb15b73 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 63 | /// RAII wrapper to manage opening and releasing of a ld_plugin_input_file. |
| 64 | struct PluginInputFile { |
Teresa Johnson | 031bed2 | 2015-12-16 21:37:48 +0000 | [diff] [blame] | 65 | void *Handle; |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 66 | std::unique_ptr<ld_plugin_input_file> File; |
Teresa Johnson | cb15b73 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 67 | |
Teresa Johnson | 031bed2 | 2015-12-16 21:37:48 +0000 | [diff] [blame] | 68 | PluginInputFile(void *Handle) : Handle(Handle) { |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 69 | File = llvm::make_unique<ld_plugin_input_file>(); |
| 70 | if (get_input_file(Handle, File.get()) != LDPS_OK) |
Teresa Johnson | cb15b73 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 71 | message(LDPL_FATAL, "Failed to get file information"); |
| 72 | } |
| 73 | ~PluginInputFile() { |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 74 | // File would have been reset to nullptr if we moved this object |
| 75 | // to a new owner. |
| 76 | if (File) |
| 77 | if (release_input_file(Handle) != LDPS_OK) |
| 78 | message(LDPL_FATAL, "Failed to release file information"); |
Teresa Johnson | cb15b73 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 79 | } |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 80 | |
| 81 | ld_plugin_input_file &file() { return *File; } |
| 82 | |
| 83 | PluginInputFile(PluginInputFile &&RHS) = default; |
| 84 | PluginInputFile &operator=(PluginInputFile &&RHS) = default; |
Teresa Johnson | cb15b73 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 87 | struct ResolutionInfo { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 88 | bool CanOmitFromDynSym = true; |
| 89 | bool DefaultVisibility = true; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 90 | }; |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 91 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 92 | struct CommonResolution { |
| 93 | bool Prevailing = false; |
| 94 | bool VisibleToRegularObj = false; |
| 95 | uint64_t Size = 0; |
| 96 | unsigned Align = 0; |
Teresa Johnson | cbf684e | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 97 | }; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 98 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 99 | } |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 100 | |
Rafael Espindola | 176e664 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 101 | static ld_plugin_add_symbols add_symbols = nullptr; |
| 102 | static ld_plugin_get_symbols get_symbols = nullptr; |
| 103 | static ld_plugin_add_input_file add_input_file = nullptr; |
| 104 | static ld_plugin_set_extra_library_path set_extra_library_path = nullptr; |
| 105 | static ld_plugin_get_view get_view = nullptr; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 106 | static bool IsExecutable = false; |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 107 | static Optional<Reloc::Model> RelocationModel; |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 108 | static std::string output_name = ""; |
| 109 | static std::list<claimed_file> Modules; |
Teresa Johnson | 683abe7 | 2016-05-26 01:46:41 +0000 | [diff] [blame] | 110 | static DenseMap<int, void *> FDToLeaderHandle; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 111 | static StringMap<ResolutionInfo> ResInfo; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 112 | static std::map<std::string, CommonResolution> Commons; |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 113 | static std::vector<std::string> Cleanup; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 114 | static llvm::TargetOptions TargetOpts; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 115 | static size_t MaxTasks; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 116 | |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 117 | namespace options { |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 118 | enum OutputType { |
| 119 | OT_NORMAL, |
| 120 | OT_DISABLE, |
| 121 | OT_BC_ONLY, |
| 122 | OT_SAVE_TEMPS |
| 123 | }; |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 124 | static OutputType TheOutputType = OT_NORMAL; |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 125 | static unsigned OptLevel = 2; |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 126 | // Default parallelism of 0 used to indicate that user did not specify. |
| 127 | // Actual parallelism default value depends on implementation. |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 128 | // Currently, code generation defaults to no parallelism, whereas |
| 129 | // ThinLTO uses the hardware_concurrency as the default. |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 130 | static unsigned Parallelism = 0; |
Teresa Johnson | 8c8fe5a | 2015-09-16 18:06:45 +0000 | [diff] [blame] | 131 | #ifdef NDEBUG |
| 132 | static bool DisableVerify = true; |
| 133 | #else |
| 134 | static bool DisableVerify = false; |
| 135 | #endif |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 136 | static std::string obj_path; |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 137 | static std::string extra_library_path; |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 138 | static std::string triple; |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 139 | static std::string mcpu; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 140 | // When the thinlto plugin option is specified, only read the function |
| 141 | // the information from intermediate files and write a combined |
| 142 | // global index for the ThinLTO backends. |
| 143 | static bool thinlto = false; |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 144 | // If false, all ThinLTO backend compilations through code gen are performed |
| 145 | // using multiple threads in the gold-plugin, before handing control back to |
Teresa Johnson | 84174c3 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 146 | // gold. If true, write individual backend index files which reflect |
| 147 | // the import decisions, and exit afterwards. The assumption is |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 148 | // that the build system will launch the backend processes. |
| 149 | static bool thinlto_index_only = false; |
Teresa Johnson | 1e2708c | 2016-07-22 18:20:22 +0000 | [diff] [blame] | 150 | // If non-empty, holds the name of a file in which to write the list of |
| 151 | // oject files gold selected for inclusion in the link after symbol |
| 152 | // resolution (i.e. they had selected symbols). This will only be non-empty |
| 153 | // in the thinlto_index_only case. It is used to identify files, which may |
| 154 | // have originally been within archive libraries specified via |
| 155 | // --start-lib/--end-lib pairs, that should be included in the final |
| 156 | // native link process (since intervening function importing and inlining |
| 157 | // may change the symbol resolution detected in the final link and which |
| 158 | // files to include out of --start-lib/--end-lib libraries as a result). |
| 159 | static std::string thinlto_linked_objects_file; |
Teresa Johnson | 8570fe4 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 160 | // If true, when generating individual index files for distributed backends, |
| 161 | // also generate a "${bitcodefile}.imports" file at the same location for each |
| 162 | // bitcode file, listing the files it imports from in plain text. This is to |
| 163 | // support distributed build file staging. |
| 164 | static bool thinlto_emit_imports_files = false; |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame] | 165 | // Option to control where files for a distributed backend (the individual |
| 166 | // index files and optional imports files) are created. |
| 167 | // If specified, expects a string of the form "oldprefix:newprefix", and |
| 168 | // instead of generating these files in the same directory path as the |
| 169 | // corresponding bitcode file, will use a path formed by replacing the |
| 170 | // bitcode file's path prefix matching oldprefix with newprefix. |
| 171 | static std::string thinlto_prefix_replace; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 172 | // Additional options to pass into the code generator. |
Nick Lewycky | 0ac5e22 | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 173 | // Note: This array will contain all plugin options which are not claimed |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 174 | // as plugin exclusive to pass to the code generator. |
Rafael Espindola | 125b924 | 2014-07-29 19:17:44 +0000 | [diff] [blame] | 175 | static std::vector<const char *> extra; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 176 | |
Nick Lewycky | 7282dd7 | 2015-08-05 21:16:02 +0000 | [diff] [blame] | 177 | static void process_plugin_option(const char *opt_) |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 178 | { |
Rafael Espindola | 176e664 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 179 | if (opt_ == nullptr) |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 180 | return; |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 181 | llvm::StringRef opt = opt_; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 182 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 183 | if (opt.startswith("mcpu=")) { |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 184 | mcpu = opt.substr(strlen("mcpu=")); |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 185 | } else if (opt.startswith("extra-library-path=")) { |
| 186 | extra_library_path = opt.substr(strlen("extra_library_path=")); |
Rafael Espindola | 148c328 | 2010-08-10 16:32:15 +0000 | [diff] [blame] | 187 | } else if (opt.startswith("mtriple=")) { |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 188 | triple = opt.substr(strlen("mtriple=")); |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 189 | } else if (opt.startswith("obj-path=")) { |
| 190 | obj_path = opt.substr(strlen("obj-path=")); |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 191 | } else if (opt == "emit-llvm") { |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 192 | TheOutputType = OT_BC_ONLY; |
Rafael Espindola | 4a3b6cf | 2014-10-29 23:54:45 +0000 | [diff] [blame] | 193 | } else if (opt == "save-temps") { |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 194 | TheOutputType = OT_SAVE_TEMPS; |
| 195 | } else if (opt == "disable-output") { |
| 196 | TheOutputType = OT_DISABLE; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 197 | } else if (opt == "thinlto") { |
| 198 | thinlto = true; |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 199 | } else if (opt == "thinlto-index-only") { |
| 200 | thinlto_index_only = true; |
Teresa Johnson | 1e2708c | 2016-07-22 18:20:22 +0000 | [diff] [blame] | 201 | } else if (opt.startswith("thinlto-index-only=")) { |
| 202 | thinlto_index_only = true; |
| 203 | thinlto_linked_objects_file = opt.substr(strlen("thinlto-index-only=")); |
Teresa Johnson | 8570fe4 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 204 | } else if (opt == "thinlto-emit-imports-files") { |
| 205 | thinlto_emit_imports_files = true; |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame] | 206 | } else if (opt.startswith("thinlto-prefix-replace=")) { |
| 207 | thinlto_prefix_replace = opt.substr(strlen("thinlto-prefix-replace=")); |
Reid Kleckner | 8e96c3e | 2016-05-17 18:43:22 +0000 | [diff] [blame] | 208 | if (thinlto_prefix_replace.find(";") == std::string::npos) |
| 209 | message(LDPL_FATAL, "thinlto-prefix-replace expects 'old;new' format"); |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 210 | } else if (opt.size() == 2 && opt[0] == 'O') { |
| 211 | if (opt[1] < '0' || opt[1] > '3') |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 212 | message(LDPL_FATAL, "Optimization level must be between 0 and 3"); |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 213 | OptLevel = opt[1] - '0'; |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 214 | } else if (opt.startswith("jobs=")) { |
| 215 | if (StringRef(opt_ + 5).getAsInteger(10, Parallelism)) |
| 216 | message(LDPL_FATAL, "Invalid parallelism level: %s", opt_ + 5); |
Teresa Johnson | 8c8fe5a | 2015-09-16 18:06:45 +0000 | [diff] [blame] | 217 | } else if (opt == "disable-verify") { |
| 218 | DisableVerify = true; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 219 | } else { |
| 220 | // Save this option to pass to the code generator. |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 221 | // ParseCommandLineOptions() expects argv[0] to be program name. Lazily |
| 222 | // add that. |
| 223 | if (extra.empty()) |
| 224 | extra.push_back("LLVMgold"); |
| 225 | |
Rafael Espindola | 125b924 | 2014-07-29 19:17:44 +0000 | [diff] [blame] | 226 | extra.push_back(opt_); |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 231 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 232 | int *claimed); |
| 233 | static ld_plugin_status all_symbols_read_hook(void); |
| 234 | static ld_plugin_status cleanup_hook(void); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 235 | |
| 236 | extern "C" ld_plugin_status onload(ld_plugin_tv *tv); |
| 237 | ld_plugin_status onload(ld_plugin_tv *tv) { |
Peter Collingbourne | 1505c0a | 2014-07-03 23:28:03 +0000 | [diff] [blame] | 238 | InitializeAllTargetInfos(); |
| 239 | InitializeAllTargets(); |
| 240 | InitializeAllTargetMCs(); |
| 241 | InitializeAllAsmParsers(); |
| 242 | InitializeAllAsmPrinters(); |
| 243 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 244 | // We're given a pointer to the first transfer vector. We read through them |
| 245 | // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values |
| 246 | // contain pointers to functions that we need to call to register our own |
| 247 | // hooks. The others are addresses of functions we can use to call into gold |
| 248 | // for services. |
| 249 | |
| 250 | bool registeredClaimFile = false; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 251 | bool RegisteredAllSymbolsRead = false; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 252 | |
| 253 | for (; tv->tv_tag != LDPT_NULL; ++tv) { |
Evgeniy Stepanov | 330c5a6 | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 254 | // Cast tv_tag to int to allow values not in "enum ld_plugin_tag", like, for |
| 255 | // example, LDPT_GET_SYMBOLS_V3 when building against an older plugin-api.h |
| 256 | // header. |
| 257 | switch (static_cast<int>(tv->tv_tag)) { |
| 258 | case LDPT_OUTPUT_NAME: |
| 259 | output_name = tv->tv_u.tv_string; |
| 260 | break; |
| 261 | case LDPT_LINKER_OUTPUT: |
| 262 | switch (tv->tv_u.tv_val) { |
| 263 | case LDPO_REL: // .o |
| 264 | case LDPO_DYN: // .so |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 265 | IsExecutable = false; |
| 266 | RelocationModel = Reloc::PIC_; |
| 267 | break; |
Evgeniy Stepanov | 330c5a6 | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 268 | case LDPO_PIE: // position independent executable |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 269 | IsExecutable = true; |
Evgeniy Stepanov | 330c5a6 | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 270 | RelocationModel = Reloc::PIC_; |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 271 | break; |
Evgeniy Stepanov | 330c5a6 | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 272 | case LDPO_EXEC: // .exe |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 273 | IsExecutable = true; |
Evgeniy Stepanov | 330c5a6 | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 274 | RelocationModel = Reloc::Static; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 275 | break; |
| 276 | default: |
Evgeniy Stepanov | 330c5a6 | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 277 | message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val); |
| 278 | return LDPS_ERR; |
| 279 | } |
| 280 | break; |
| 281 | case LDPT_OPTION: |
| 282 | options::process_plugin_option(tv->tv_u.tv_string); |
| 283 | break; |
| 284 | case LDPT_REGISTER_CLAIM_FILE_HOOK: { |
| 285 | ld_plugin_register_claim_file callback; |
| 286 | callback = tv->tv_u.tv_register_claim_file; |
| 287 | |
| 288 | if (callback(claim_file_hook) != LDPS_OK) |
| 289 | return LDPS_ERR; |
| 290 | |
| 291 | registeredClaimFile = true; |
| 292 | } break; |
| 293 | case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: { |
| 294 | ld_plugin_register_all_symbols_read callback; |
| 295 | callback = tv->tv_u.tv_register_all_symbols_read; |
| 296 | |
| 297 | if (callback(all_symbols_read_hook) != LDPS_OK) |
| 298 | return LDPS_ERR; |
| 299 | |
| 300 | RegisteredAllSymbolsRead = true; |
| 301 | } break; |
| 302 | case LDPT_REGISTER_CLEANUP_HOOK: { |
| 303 | ld_plugin_register_cleanup callback; |
| 304 | callback = tv->tv_u.tv_register_cleanup; |
| 305 | |
| 306 | if (callback(cleanup_hook) != LDPS_OK) |
| 307 | return LDPS_ERR; |
| 308 | } break; |
| 309 | case LDPT_GET_INPUT_FILE: |
| 310 | get_input_file = tv->tv_u.tv_get_input_file; |
| 311 | break; |
| 312 | case LDPT_RELEASE_INPUT_FILE: |
| 313 | release_input_file = tv->tv_u.tv_release_input_file; |
| 314 | break; |
| 315 | case LDPT_ADD_SYMBOLS: |
| 316 | add_symbols = tv->tv_u.tv_add_symbols; |
| 317 | break; |
| 318 | case LDPT_GET_SYMBOLS_V2: |
| 319 | // Do not override get_symbols_v3 with get_symbols_v2. |
| 320 | if (!get_symbols) |
| 321 | get_symbols = tv->tv_u.tv_get_symbols; |
| 322 | break; |
| 323 | case LDPT_GET_SYMBOLS_V3: |
| 324 | get_symbols = tv->tv_u.tv_get_symbols; |
| 325 | break; |
| 326 | case LDPT_ADD_INPUT_FILE: |
| 327 | add_input_file = tv->tv_u.tv_add_input_file; |
| 328 | break; |
| 329 | case LDPT_SET_EXTRA_LIBRARY_PATH: |
| 330 | set_extra_library_path = tv->tv_u.tv_set_extra_library_path; |
| 331 | break; |
| 332 | case LDPT_GET_VIEW: |
| 333 | get_view = tv->tv_u.tv_get_view; |
| 334 | break; |
| 335 | case LDPT_MESSAGE: |
| 336 | message = tv->tv_u.tv_message; |
| 337 | break; |
| 338 | default: |
| 339 | break; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Rafael Espindola | e08484d | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 343 | if (!registeredClaimFile) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 344 | message(LDPL_ERROR, "register_claim_file not passed to LLVMgold."); |
Rafael Espindola | 6add618 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 345 | return LDPS_ERR; |
| 346 | } |
Rafael Espindola | e08484d | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 347 | if (!add_symbols) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 348 | message(LDPL_ERROR, "add_symbols not passed to LLVMgold."); |
Rafael Espindola | 6add618 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 349 | return LDPS_ERR; |
| 350 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 351 | |
Rafael Espindola | a0d30a9 | 2014-06-19 22:20:07 +0000 | [diff] [blame] | 352 | if (!RegisteredAllSymbolsRead) |
| 353 | return LDPS_OK; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 354 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 355 | if (!get_input_file) { |
| 356 | message(LDPL_ERROR, "get_input_file not passed to LLVMgold."); |
| 357 | return LDPS_ERR; |
Rafael Espindola | c273aac | 2014-06-19 22:54:47 +0000 | [diff] [blame] | 358 | } |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 359 | if (!release_input_file) { |
Marianne Mailhot-Sarrasin | a5a750e | 2016-03-30 12:20:53 +0000 | [diff] [blame] | 360 | message(LDPL_ERROR, "release_input_file not passed to LLVMgold."); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 361 | return LDPS_ERR; |
Tom Roeder | b508119 | 2014-06-26 20:43:27 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 364 | return LDPS_OK; |
| 365 | } |
| 366 | |
NAKAMURA Takumi | b13e63c | 2015-11-19 10:43:44 +0000 | [diff] [blame] | 367 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 368 | if (const auto *BDI = dyn_cast<BitcodeDiagnosticInfo>(&DI)) { |
| 369 | std::error_code EC = BDI->getError(); |
| 370 | if (EC == BitcodeError::InvalidBitcodeSignature) |
| 371 | return; |
| 372 | } |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 373 | |
| 374 | std::string ErrStorage; |
| 375 | { |
| 376 | raw_string_ostream OS(ErrStorage); |
| 377 | DiagnosticPrinterRawOStream DP(OS); |
| 378 | DI.print(DP); |
| 379 | } |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 380 | ld_plugin_level Level; |
| 381 | switch (DI.getSeverity()) { |
| 382 | case DS_Error: |
| 383 | message(LDPL_FATAL, "LLVM gold plugin has failed to create LTO module: %s", |
| 384 | ErrStorage.c_str()); |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 385 | case DS_Warning: |
| 386 | Level = LDPL_WARNING; |
| 387 | break; |
| 388 | case DS_Note: |
Rafael Espindola | f3f1854 | 2015-03-04 18:51:45 +0000 | [diff] [blame] | 389 | case DS_Remark: |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 390 | Level = LDPL_INFO; |
| 391 | break; |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 392 | } |
| 393 | message(Level, "LLVM gold plugin: %s", ErrStorage.c_str()); |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 396 | static void check(Error E, std::string Msg = "LLVM gold plugin") { |
| 397 | handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { |
| 398 | message(LDPL_FATAL, "%s: %s", Msg.c_str(), EIB.message().c_str()); |
| 399 | return Error::success(); |
| 400 | }); |
NAKAMURA Takumi | b13e63c | 2015-11-19 10:43:44 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 403 | template <typename T> static T check(Expected<T> E) { |
| 404 | if (E) |
| 405 | return std::move(*E); |
| 406 | check(E.takeError()); |
| 407 | return T(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Rafael Espindola | e54d821 | 2014-07-06 14:31:22 +0000 | [diff] [blame] | 410 | /// Called by gold to see whether this file is one that our plugin can handle. |
| 411 | /// We'll try to open it and register all the symbols with add_symbol if |
| 412 | /// possible. |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 413 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 414 | int *claimed) { |
Rafael Espindola | eeec8e6 | 2014-08-27 20:25:55 +0000 | [diff] [blame] | 415 | MemoryBufferRef BufferRef; |
| 416 | std::unique_ptr<MemoryBuffer> Buffer; |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 417 | if (get_view) { |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 418 | const void *view; |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 419 | if (get_view(file->handle, &view) != LDPS_OK) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 420 | message(LDPL_ERROR, "Failed to get a view of %s", file->name); |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 421 | return LDPS_ERR; |
| 422 | } |
Nick Lewycky | 7282dd7 | 2015-08-05 21:16:02 +0000 | [diff] [blame] | 423 | BufferRef = |
| 424 | MemoryBufferRef(StringRef((const char *)view, file->filesize), ""); |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 425 | } else { |
Ivan Krasin | 639222d | 2011-09-15 23:13:00 +0000 | [diff] [blame] | 426 | int64_t offset = 0; |
Nick Lewycky | 8691c47 | 2009-02-05 04:14:23 +0000 | [diff] [blame] | 427 | // Gold has found what might be IR part-way inside of a file, such as |
| 428 | // an .a archive. |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 429 | if (file->offset) { |
| 430 | offset = file->offset; |
| 431 | } |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 432 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 433 | MemoryBuffer::getOpenFileSlice(file->fd, file->name, file->filesize, |
| 434 | offset); |
| 435 | if (std::error_code EC = BufferOrErr.getError()) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 436 | message(LDPL_ERROR, EC.message().c_str()); |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 437 | return LDPS_ERR; |
| 438 | } |
Rafael Espindola | eeec8e6 | 2014-08-27 20:25:55 +0000 | [diff] [blame] | 439 | Buffer = std::move(BufferOrErr.get()); |
| 440 | BufferRef = Buffer->getMemBufferRef(); |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 441 | } |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 442 | |
Rafael Espindola | 6c472e5 | 2014-07-29 20:46:19 +0000 | [diff] [blame] | 443 | *claimed = 1; |
| 444 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 445 | Expected<std::unique_ptr<InputFile>> ObjOrErr = InputFile::create(BufferRef); |
| 446 | if (!ObjOrErr) { |
| 447 | handleAllErrors(ObjOrErr.takeError(), [&](const ErrorInfoBase &EI) { |
| 448 | std::error_code EC = EI.convertToErrorCode(); |
| 449 | if (EC == object::object_error::invalid_file_type || |
| 450 | EC == object::object_error::bitcode_section_not_found) |
| 451 | *claimed = 0; |
| 452 | else |
| 453 | message(LDPL_ERROR, |
| 454 | "LLVM gold plugin has failed to create LTO module: %s", |
| 455 | EI.message().c_str()); |
| 456 | }); |
| 457 | |
| 458 | return *claimed ? LDPS_ERR : LDPS_OK; |
Ivan Krasin | d5f2d8c | 2011-09-09 00:14:04 +0000 | [diff] [blame] | 459 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 460 | |
| 461 | std::unique_ptr<InputFile> Obj = std::move(*ObjOrErr); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 462 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 463 | Modules.resize(Modules.size() + 1); |
| 464 | claimed_file &cf = Modules.back(); |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 465 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 466 | cf.handle = file->handle; |
Teresa Johnson | 683abe7 | 2016-05-26 01:46:41 +0000 | [diff] [blame] | 467 | // Keep track of the first handle for each file descriptor, since there are |
| 468 | // multiple in the case of an archive. This is used later in the case of |
| 469 | // ThinLTO parallel backends to ensure that each file is only opened and |
| 470 | // released once. |
| 471 | auto LeaderHandle = |
| 472 | FDToLeaderHandle.insert(std::make_pair(file->fd, file->handle)).first; |
| 473 | cf.leader_handle = LeaderHandle->second; |
| 474 | // Save the filesize since for parallel ThinLTO backends we can only |
| 475 | // invoke get_input_file once per archive (only for the leader handle). |
| 476 | cf.filesize = file->filesize; |
| 477 | // In the case of an archive library, all but the first member must have a |
| 478 | // non-zero offset, which we can append to the file name to obtain a |
| 479 | // unique name. |
| 480 | cf.name = file->name; |
| 481 | if (file->offset) |
| 482 | cf.name += ".llvm." + std::to_string(file->offset) + "." + |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 483 | sys::path::filename(Obj->getSourceFileName()).str(); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 484 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 485 | for (auto &Sym : Obj->symbols()) { |
| 486 | uint32_t Symflags = Sym.getFlags(); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 487 | |
| 488 | cf.syms.push_back(ld_plugin_symbol()); |
| 489 | ld_plugin_symbol &sym = cf.syms.back(); |
Rafael Espindola | 176e664 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 490 | sym.version = nullptr; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 491 | StringRef Name = Sym.getName(); |
| 492 | sym.name = strdup(Name.str().c_str()); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 493 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 494 | ResolutionInfo &Res = ResInfo[Name]; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 495 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 496 | Res.CanOmitFromDynSym &= Sym.canBeOmittedFromSymbolTable(); |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 497 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 498 | sym.visibility = LDPV_DEFAULT; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 499 | GlobalValue::VisibilityTypes Vis = Sym.getVisibility(); |
| 500 | if (Vis != GlobalValue::DefaultVisibility) |
| 501 | Res.DefaultVisibility = false; |
| 502 | switch (Vis) { |
| 503 | case GlobalValue::DefaultVisibility: |
| 504 | break; |
| 505 | case GlobalValue::HiddenVisibility: |
| 506 | sym.visibility = LDPV_HIDDEN; |
| 507 | break; |
| 508 | case GlobalValue::ProtectedVisibility: |
| 509 | sym.visibility = LDPV_PROTECTED; |
| 510 | break; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 513 | if (Symflags & object::BasicSymbolRef::SF_Undefined) { |
| 514 | sym.def = LDPK_UNDEF; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 515 | if (Symflags & object::BasicSymbolRef::SF_Weak) |
Rafael Espindola | 5654852 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 516 | sym.def = LDPK_WEAKUNDEF; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 517 | } else if (Symflags & object::BasicSymbolRef::SF_Common) |
| 518 | sym.def = LDPK_COMMON; |
| 519 | else if (Symflags & object::BasicSymbolRef::SF_Weak) |
| 520 | sym.def = LDPK_WEAKDEF; |
| 521 | else |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 522 | sym.def = LDPK_DEF; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 523 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 524 | sym.size = 0; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 525 | sym.comdat_key = nullptr; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 526 | const Comdat *C = check(Sym.getComdat()); |
| 527 | if (C) |
| 528 | sym.comdat_key = strdup(C->getName().str().c_str()); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 529 | |
| 530 | sym.resolution = LDPR_UNKNOWN; |
| 531 | } |
| 532 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 533 | if (!cf.syms.empty()) { |
Nick Lewycky | 7282dd7 | 2015-08-05 21:16:02 +0000 | [diff] [blame] | 534 | if (add_symbols(cf.handle, cf.syms.size(), cf.syms.data()) != LDPS_OK) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 535 | message(LDPL_ERROR, "Unable to add symbols!"); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 536 | return LDPS_ERR; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | return LDPS_OK; |
| 541 | } |
| 542 | |
Rafael Espindola | 538c9a8 | 2014-12-23 18:18:37 +0000 | [diff] [blame] | 543 | static void freeSymName(ld_plugin_symbol &Sym) { |
| 544 | free(Sym.name); |
| 545 | free(Sym.comdat_key); |
| 546 | Sym.name = nullptr; |
| 547 | Sym.comdat_key = nullptr; |
| 548 | } |
| 549 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 550 | /// Helper to get a file's symbols and a view into it via gold callbacks. |
| 551 | static const void *getSymbolsAndView(claimed_file &F) { |
Benjamin Kramer | 39988a0 | 2016-03-08 14:02:46 +0000 | [diff] [blame] | 552 | ld_plugin_status status = get_symbols(F.handle, F.syms.size(), F.syms.data()); |
Evgeniy Stepanov | 330c5a6 | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 553 | if (status == LDPS_NO_SYMS) |
| 554 | return nullptr; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 555 | |
Evgeniy Stepanov | 330c5a6 | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 556 | if (status != LDPS_OK) |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 557 | message(LDPL_FATAL, "Failed to get symbol information"); |
| 558 | |
| 559 | const void *View; |
| 560 | if (get_view(F.handle, &View) != LDPS_OK) |
| 561 | message(LDPL_FATAL, "Failed to get a view of file"); |
| 562 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 563 | return View; |
| 564 | } |
| 565 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 566 | static void addModule(LTO &Lto, claimed_file &F, const void *View) { |
Teresa Johnson | 683abe7 | 2016-05-26 01:46:41 +0000 | [diff] [blame] | 567 | MemoryBufferRef BufferRef(StringRef((const char *)View, F.filesize), F.name); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 568 | Expected<std::unique_ptr<InputFile>> ObjOrErr = InputFile::create(BufferRef); |
Teresa Johnson | 6290dbc | 2015-11-21 21:55:48 +0000 | [diff] [blame] | 569 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 570 | if (!ObjOrErr) |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 571 | message(LDPL_FATAL, "Could not read bitcode from file : %s", |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 572 | toString(ObjOrErr.takeError()).c_str()); |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 573 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 574 | InputFile &Obj = **ObjOrErr; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 575 | |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 576 | unsigned SymNum = 0; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 577 | std::vector<SymbolResolution> Resols(F.syms.size()); |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 578 | for (auto &ObjSym : Obj.symbols()) { |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 579 | ld_plugin_symbol &Sym = F.syms[SymNum]; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 580 | SymbolResolution &R = Resols[SymNum]; |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 581 | ++SymNum; |
| 582 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 583 | ld_plugin_symbol_resolution Resolution = |
| 584 | (ld_plugin_symbol_resolution)Sym.resolution; |
| 585 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 586 | ResolutionInfo &Res = ResInfo[Sym.name]; |
Rafael Espindola | 890db27 | 2014-09-09 20:08:22 +0000 | [diff] [blame] | 587 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 588 | switch (Resolution) { |
| 589 | case LDPR_UNKNOWN: |
| 590 | llvm_unreachable("Unexpected resolution"); |
| 591 | |
| 592 | case LDPR_RESOLVED_IR: |
| 593 | case LDPR_RESOLVED_EXEC: |
| 594 | case LDPR_RESOLVED_DYN: |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 595 | case LDPR_PREEMPTED_IR: |
| 596 | case LDPR_PREEMPTED_REG: |
Rafael Espindola | 5ca7fa1 | 2015-01-14 13:53:50 +0000 | [diff] [blame] | 597 | case LDPR_UNDEF: |
Rafael Espindola | 5ca7fa1 | 2015-01-14 13:53:50 +0000 | [diff] [blame] | 598 | break; |
| 599 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 600 | case LDPR_PREVAILING_DEF_IRONLY: |
| 601 | R.Prevailing = true; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 602 | break; |
Teresa Johnson | f99573b | 2016-08-11 12:56:40 +0000 | [diff] [blame] | 603 | |
Teresa Johnson | cbf684e | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 604 | case LDPR_PREVAILING_DEF: |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 605 | R.Prevailing = true; |
| 606 | R.VisibleToRegularObj = true; |
Teresa Johnson | cbf684e | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 607 | break; |
| 608 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 609 | case LDPR_PREVAILING_DEF_IRONLY_EXP: |
| 610 | R.Prevailing = true; |
| 611 | if (!Res.CanOmitFromDynSym) |
| 612 | R.VisibleToRegularObj = true; |
Teresa Johnson | cbf684e | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 613 | break; |
| 614 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 615 | |
| 616 | if (Resolution != LDPR_RESOLVED_DYN && Resolution != LDPR_UNDEF && |
| 617 | (IsExecutable || !Res.DefaultVisibility)) |
| 618 | R.FinalDefinitionInLinkageUnit = true; |
| 619 | |
| 620 | if (ObjSym.getFlags() & object::BasicSymbolRef::SF_Common) { |
| 621 | // We ignore gold's resolution for common symbols. A common symbol with |
| 622 | // the correct size and alignment is added to the module by the pre-opt |
| 623 | // module hook if any common symbol prevailed. |
| 624 | CommonResolution &CommonRes = Commons[ObjSym.getIRName()]; |
| 625 | if (R.Prevailing) { |
| 626 | CommonRes.Prevailing = true; |
| 627 | CommonRes.VisibleToRegularObj = R.VisibleToRegularObj; |
| 628 | } |
Saleem Abdulrasool | 98541b0 | 2016-08-14 05:07:20 +0000 | [diff] [blame] | 629 | CommonRes.Size = std::max(CommonRes.Size, ObjSym.getCommonSize()); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 630 | CommonRes.Align = std::max(CommonRes.Align, ObjSym.getCommonAlignment()); |
| 631 | R.Prevailing = false; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Rafael Espindola | 538c9a8 | 2014-12-23 18:18:37 +0000 | [diff] [blame] | 634 | freeSymName(Sym); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 637 | check(Lto.add(std::move(*ObjOrErr), Resols), |
| 638 | std::string("Failed to link module ") + F.name); |
Rafael Espindola | 4a3b6cf | 2014-10-29 23:54:45 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 641 | static void recordFile(std::string Filename, bool TempOutFile) { |
| 642 | if (add_input_file(Filename.c_str()) != LDPS_OK) |
| 643 | message(LDPL_FATAL, |
| 644 | "Unable to add .o file to the link. File left behind in: %s", |
| 645 | Filename.c_str()); |
| 646 | if (TempOutFile) |
| 647 | Cleanup.push_back(Filename.c_str()); |
| 648 | } |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 649 | |
Mehdi Amini | 970800e | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 650 | /// Return the desired output filename given a base input name, a flag |
| 651 | /// indicating whether a temp file should be generated, and an optional task id. |
| 652 | /// The new filename generated is returned in \p NewFilename. |
| 653 | static void getOutputFileName(SmallString<128> InFilename, bool TempOutFile, |
| 654 | SmallString<128> &NewFilename, int TaskID = -1) { |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 655 | if (TempOutFile) { |
| 656 | std::error_code EC = |
Mehdi Amini | 970800e | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 657 | sys::fs::createTemporaryFile("lto-llvm", "o", NewFilename); |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 658 | if (EC) |
| 659 | message(LDPL_FATAL, "Could not create temporary file: %s", |
| 660 | EC.message().c_str()); |
| 661 | } else { |
| 662 | NewFilename = InFilename; |
| 663 | if (TaskID >= 0) |
| 664 | NewFilename += utostr(TaskID); |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 665 | } |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 668 | /// Add all required common symbols to M, which is expected to be the first |
| 669 | /// combined module. |
| 670 | static void addCommons(Module &M) { |
| 671 | for (auto &I : Commons) { |
| 672 | if (!I.second.Prevailing) |
Teresa Johnson | cbf684e | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 673 | continue; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 674 | ArrayType *Ty = |
| 675 | ArrayType::get(Type::getInt8Ty(M.getContext()), I.second.Size); |
| 676 | GlobalVariable *OldGV = M.getNamedGlobal(I.first); |
| 677 | auto *GV = new GlobalVariable(M, Ty, false, GlobalValue::CommonLinkage, |
| 678 | ConstantAggregateZero::get(Ty), ""); |
| 679 | GV->setAlignment(I.second.Align); |
| 680 | if (OldGV) { |
| 681 | OldGV->replaceAllUsesWith(ConstantExpr::getBitCast(GV, OldGV->getType())); |
| 682 | GV->takeName(OldGV); |
| 683 | OldGV->eraseFromParent(); |
| 684 | } else { |
| 685 | GV->setName(I.first); |
Teresa Johnson | cbf684e | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 686 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 687 | // We may only internalize commons if there is a single LTO task because |
| 688 | // other native object files may require the common. |
| 689 | if (MaxTasks == 1 && !I.second.VisibleToRegularObj) |
| 690 | GV->setLinkage(GlobalValue::InternalLinkage); |
Teresa Johnson | f99573b | 2016-08-11 12:56:40 +0000 | [diff] [blame] | 691 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 692 | } |
Teresa Johnson | cbf684e | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 693 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 694 | static CodeGenOpt::Level getCGOptLevel() { |
| 695 | switch (options::OptLevel) { |
| 696 | case 0: |
| 697 | return CodeGenOpt::None; |
| 698 | case 1: |
| 699 | return CodeGenOpt::Less; |
| 700 | case 2: |
| 701 | return CodeGenOpt::Default; |
| 702 | case 3: |
| 703 | return CodeGenOpt::Aggressive; |
| 704 | } |
| 705 | llvm_unreachable("Invalid optimization level"); |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame] | 708 | /// Parse the thinlto_prefix_replace option into the \p OldPrefix and |
| 709 | /// \p NewPrefix strings, if it was specified. |
| 710 | static void getThinLTOOldAndNewPrefix(std::string &OldPrefix, |
| 711 | std::string &NewPrefix) { |
| 712 | StringRef PrefixReplace = options::thinlto_prefix_replace; |
Reid Kleckner | 8e96c3e | 2016-05-17 18:43:22 +0000 | [diff] [blame] | 713 | assert(PrefixReplace.empty() || PrefixReplace.find(";") != StringRef::npos); |
| 714 | std::pair<StringRef, StringRef> Split = PrefixReplace.split(";"); |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame] | 715 | OldPrefix = Split.first.str(); |
| 716 | NewPrefix = Split.second.str(); |
| 717 | } |
| 718 | |
Mehdi Amini | 970800e | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 719 | namespace { |
| 720 | // Define the LTOOutput handling |
| 721 | class LTOOutput : public lto::NativeObjectOutput { |
| 722 | StringRef Path; |
| 723 | |
| 724 | public: |
| 725 | LTOOutput(StringRef Path) : Path(Path) {} |
| 726 | // Open the filename \p Path and allocate a stream. |
| 727 | std::unique_ptr<raw_pwrite_stream> getStream() override { |
| 728 | int FD; |
| 729 | std::error_code EC = sys::fs::openFileForWrite(Path, FD, sys::fs::F_None); |
| 730 | if (EC) |
| 731 | message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str()); |
| 732 | return llvm::make_unique<llvm::raw_fd_ostream>(FD, true); |
| 733 | } |
| 734 | }; |
| 735 | } |
| 736 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 737 | static std::unique_ptr<LTO> createLTO() { |
| 738 | Config Conf; |
| 739 | ThinBackend Backend; |
| 740 | unsigned ParallelCodeGenParallelismLevel = 1; |
| 741 | |
| 742 | Conf.CPU = options::mcpu; |
| 743 | Conf.Options = InitTargetOptionsFromCodeGenFlags(); |
| 744 | |
| 745 | // Disable the new X86 relax relocations since gold might not support them. |
| 746 | // FIXME: Check the gold version or add a new option to enable them. |
| 747 | Conf.Options.RelaxELFRelocations = false; |
| 748 | |
| 749 | Conf.MAttrs = MAttrs; |
| 750 | Conf.RelocModel = *RelocationModel; |
| 751 | Conf.CGOptLevel = getCGOptLevel(); |
| 752 | Conf.DisableVerify = options::DisableVerify; |
| 753 | Conf.OptLevel = options::OptLevel; |
| 754 | if (options::Parallelism) { |
| 755 | if (options::thinlto) |
| 756 | Backend = createInProcessThinBackend(options::Parallelism); |
| 757 | else |
| 758 | ParallelCodeGenParallelismLevel = options::Parallelism; |
Teresa Johnson | bbd10b4 | 2016-05-17 14:45:30 +0000 | [diff] [blame] | 759 | } |
Teresa Johnson | cbf684e | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 760 | if (options::thinlto_index_only) { |
Teresa Johnson | cbf684e | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 761 | std::string OldPrefix, NewPrefix; |
| 762 | getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 763 | Backend = createWriteIndexesThinBackend( |
| 764 | OldPrefix, NewPrefix, options::thinlto_emit_imports_files, |
| 765 | options::thinlto_linked_objects_file); |
Teresa Johnson | 84174c3 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 768 | Conf.OverrideTriple = options::triple; |
| 769 | Conf.DefaultTriple = sys::getDefaultTargetTriple(); |
| 770 | |
| 771 | Conf.DiagHandler = diagnosticHandler; |
| 772 | |
| 773 | Conf.PreOptModuleHook = [](size_t Task, Module &M) { |
| 774 | if (Task == 0) |
| 775 | addCommons(M); |
| 776 | return true; |
| 777 | }; |
| 778 | |
| 779 | switch (options::TheOutputType) { |
| 780 | case options::OT_NORMAL: |
| 781 | break; |
| 782 | |
| 783 | case options::OT_DISABLE: |
| 784 | Conf.PreOptModuleHook = [](size_t Task, Module &M) { return false; }; |
| 785 | break; |
| 786 | |
| 787 | case options::OT_BC_ONLY: |
| 788 | Conf.PostInternalizeModuleHook = [](size_t Task, Module &M) { |
| 789 | std::error_code EC; |
| 790 | raw_fd_ostream OS(output_name, EC, sys::fs::OpenFlags::F_None); |
| 791 | if (EC) |
| 792 | message(LDPL_FATAL, "Failed to write the output file."); |
| 793 | WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ false); |
| 794 | return false; |
| 795 | }; |
| 796 | break; |
| 797 | |
| 798 | case options::OT_SAVE_TEMPS: |
Mehdi Amini | eccffad | 2016-08-18 00:12:33 +0000 | [diff] [blame^] | 799 | check(Conf.addSaveTemps(output_name + ".", |
| 800 | /* UseInputModulePath */ true)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 801 | break; |
Teresa Johnson | cbf684e | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 804 | return llvm::make_unique<LTO>(std::move(Conf), Backend, |
| 805 | ParallelCodeGenParallelismLevel); |
Teresa Johnson | 84174c3 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Rafael Espindola | b639329 | 2014-07-30 01:23:45 +0000 | [diff] [blame] | 808 | /// gold informs us that all symbols have been read. At this point, we use |
| 809 | /// get_symbols to see if any of our definitions have been overridden by a |
| 810 | /// native object file. Then, perform optimization and codegen. |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 811 | static ld_plugin_status allSymbolsReadHook() { |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 812 | if (Modules.empty()) |
| 813 | return LDPS_OK; |
Rafael Espindola | 9ef90d5 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 814 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 815 | if (unsigned NumOpts = options::extra.size()) |
| 816 | cl::ParseCommandLineOptions(NumOpts, &options::extra[0]); |
| 817 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 818 | std::unique_ptr<LTO> Lto = createLTO(); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 819 | |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 820 | for (claimed_file &F : Modules) { |
Teresa Johnson | cb15b73 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 821 | PluginInputFile InputFile(F.handle); |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 822 | const void *View = getSymbolsAndView(F); |
Evgeniy Stepanov | 4dc3c8d | 2016-03-11 00:51:57 +0000 | [diff] [blame] | 823 | if (!View) |
| 824 | continue; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 825 | addModule(*Lto, F, View); |
Rafael Espindola | 77b6d01 | 2010-06-14 21:20:52 +0000 | [diff] [blame] | 826 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 827 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 828 | SmallString<128> Filename; |
Mehdi Amini | 970800e | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 829 | // Note that getOutputFileName will append a unique ID for each task |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 830 | if (!options::obj_path.empty()) |
| 831 | Filename = options::obj_path; |
| 832 | else if (options::TheOutputType == options::OT_SAVE_TEMPS) |
| 833 | Filename = output_name + ".o"; |
| 834 | bool SaveTemps = !Filename.empty(); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 835 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 836 | MaxTasks = Lto->getMaxTasks(); |
| 837 | std::vector<uintptr_t> IsTemporary(MaxTasks); |
| 838 | std::vector<SmallString<128>> Filenames(MaxTasks); |
| 839 | |
Mehdi Amini | 970800e | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 840 | auto AddOutput = [&](size_t Task) { |
| 841 | auto &OutputName = Filenames[Task]; |
| 842 | getOutputFileName(Filename, /*TempOutFile=*/!SaveTemps, OutputName, |
| 843 | MaxTasks > 1 ? Task : -1); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 844 | IsTemporary[Task] = !SaveTemps; |
Mehdi Amini | 970800e | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 845 | return llvm::make_unique<LTOOutput>(OutputName); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 846 | }; |
| 847 | |
Mehdi Amini | 970800e | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 848 | check(Lto->run(AddOutput)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 849 | |
| 850 | if (options::TheOutputType == options::OT_DISABLE || |
| 851 | options::TheOutputType == options::OT_BC_ONLY) |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 852 | return LDPS_OK; |
| 853 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 854 | if (options::thinlto_index_only) { |
| 855 | cleanup_hook(); |
| 856 | exit(0); |
Rafael Espindola | ba3398b | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 857 | } |
Rafael Espindola | 143fc3b | 2013-10-16 12:47:04 +0000 | [diff] [blame] | 858 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 859 | for (unsigned I = 0; I != MaxTasks; ++I) |
| 860 | if (!Filenames[I].empty()) |
| 861 | recordFile(Filenames[I].str(), IsTemporary[I]); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 862 | |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 863 | if (!options::extra_library_path.empty() && |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 864 | set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) |
| 865 | message(LDPL_FATAL, "Unable to set the extra library path."); |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 866 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 867 | return LDPS_OK; |
| 868 | } |
| 869 | |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 870 | static ld_plugin_status all_symbols_read_hook(void) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 871 | ld_plugin_status Ret = allSymbolsReadHook(); |
Rafael Espindola | 947bdb6 | 2014-11-25 20:52:49 +0000 | [diff] [blame] | 872 | llvm_shutdown(); |
| 873 | |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 874 | if (options::TheOutputType == options::OT_BC_ONLY || |
Michael Kuperstein | a07d9b9 | 2015-02-12 18:21:50 +0000 | [diff] [blame] | 875 | options::TheOutputType == options::OT_DISABLE) { |
Davide Italiano | 289a43e | 2016-03-20 20:12:33 +0000 | [diff] [blame] | 876 | if (options::TheOutputType == options::OT_DISABLE) { |
Michael Kuperstein | a07d9b9 | 2015-02-12 18:21:50 +0000 | [diff] [blame] | 877 | // Remove the output file here since ld.bfd creates the output file |
| 878 | // early. |
Davide Italiano | 289a43e | 2016-03-20 20:12:33 +0000 | [diff] [blame] | 879 | std::error_code EC = sys::fs::remove(output_name); |
| 880 | if (EC) |
| 881 | message(LDPL_ERROR, "Failed to delete '%s': %s", output_name.c_str(), |
| 882 | EC.message().c_str()); |
| 883 | } |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 884 | exit(0); |
Michael Kuperstein | a07d9b9 | 2015-02-12 18:21:50 +0000 | [diff] [blame] | 885 | } |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 886 | |
| 887 | return Ret; |
| 888 | } |
| 889 | |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 890 | static ld_plugin_status cleanup_hook(void) { |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 891 | for (std::string &Name : Cleanup) { |
| 892 | std::error_code EC = sys::fs::remove(Name); |
Rafael Espindola | 55ab87f | 2013-06-17 18:38:18 +0000 | [diff] [blame] | 893 | if (EC) |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 894 | message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(), |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 895 | EC.message().c_str()); |
Rafael Espindola | 55ab87f | 2013-06-17 18:38:18 +0000 | [diff] [blame] | 896 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 897 | |
| 898 | return LDPS_OK; |
| 899 | } |