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