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