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