Nick Lewycky | 3e62b2d | 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 | 8ffb6b1 | 2017-02-02 17:33:53 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Statistic.h" |
Teresa Johnson | a547919 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 16 | #include "llvm/Bitcode/BitcodeReader.h" |
| 17 | #include "llvm/Bitcode/BitcodeWriter.h" |
David Blaikie | 461bf52 | 2018-04-11 18:49:37 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/CommandFlags.inc" |
Mehdi Amini | f6071e1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 19 | #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H |
Rafael Espindola | 19f3868 | 2014-09-09 20:08:22 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Constants.h" |
Rafael Espindola | 68016e0 | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DiagnosticPrinter.h" |
Teresa Johnson | 57b248a | 2016-08-24 15:11:47 +0000 | [diff] [blame] | 22 | #include "llvm/LTO/Caching.h" |
Teresa Johnson | bb83f89 | 2016-05-26 01:46:41 +0000 | [diff] [blame] | 23 | #include "llvm/LTO/LTO.h" |
Peter Collingbourne | 14ecedf | 2017-03-28 23:35:34 +0000 | [diff] [blame] | 24 | #include "llvm/Object/Error.h" |
Yi Kong | 74ae606 | 2017-09-18 23:24:55 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CachePruning.h" |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Peter Collingbourne | 14ecedf | 2017-03-28 23:35:34 +0000 | [diff] [blame] | 27 | #include "llvm/Support/FileSystem.h" |
Rafael Espindola | 2fbbd7a | 2014-11-25 20:52:49 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MemoryBuffer.h" |
Teresa Johnson | a486381 | 2016-05-17 14:45:30 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Path.h" |
Rafael Espindola | 0b5745a | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 31 | #include "llvm/Support/TargetSelect.h" |
Teresa Johnson | a63df07 | 2015-12-09 19:45:55 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 33 | #include <list> |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 34 | #include <map> |
Chandler Carruth | 9f20a4c | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 35 | #include <plugin-api.h> |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 36 | #include <string> |
Rafael Espindola | d5132f9 | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 37 | #include <system_error> |
Benjamin Kramer | 14aae01 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 38 | #include <utility> |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 39 | #include <vector> |
| 40 | |
Sylvestre Ledru | 6a089db | 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 | f0c747b | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 43 | #define LDPO_PIE 3 |
| 44 | |
| 45 | #define LDPT_GET_SYMBOLS_V3 28 |
Sylvestre Ledru | 6a089db | 2014-02-11 17:30:18 +0000 | [diff] [blame] | 46 | |
Teresa Johnson | ead8765 | 2018-03-14 13:26:18 +0000 | [diff] [blame] | 47 | // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum |
| 48 | // required version. |
| 49 | #define LDPT_GET_WRAP_SYMBOLS 32 |
| 50 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 51 | using namespace llvm; |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 52 | using namespace lto; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 53 | |
Teresa Johnson | ead8765 | 2018-03-14 13:26:18 +0000 | [diff] [blame] | 54 | // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum |
| 55 | // required version. |
| 56 | typedef enum ld_plugin_status (*ld_plugin_get_wrap_symbols)( |
| 57 | uint64_t *num_symbols, const char ***wrap_symbol_list); |
| 58 | |
Teresa Johnson | 2c50df6 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 59 | static ld_plugin_status discard_message(int level, const char *format, ...) { |
| 60 | // Die loudly. Recent versions of Gold pass ld_plugin_message as the first |
| 61 | // callback in the transfer vector. This should never be called. |
| 62 | abort(); |
| 63 | } |
| 64 | |
| 65 | static ld_plugin_release_input_file release_input_file = nullptr; |
| 66 | static ld_plugin_get_input_file get_input_file = nullptr; |
| 67 | static ld_plugin_message message = discard_message; |
Teresa Johnson | ead8765 | 2018-03-14 13:26:18 +0000 | [diff] [blame] | 68 | static ld_plugin_get_wrap_symbols get_wrap_symbols = nullptr; |
Teresa Johnson | 2c50df6 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 69 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 70 | namespace { |
Rafael Espindola | 8483dac | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 71 | struct claimed_file { |
| 72 | void *handle; |
Teresa Johnson | bb83f89 | 2016-05-26 01:46:41 +0000 | [diff] [blame] | 73 | void *leader_handle; |
Rafael Espindola | 8483dac | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 74 | std::vector<ld_plugin_symbol> syms; |
Teresa Johnson | bb83f89 | 2016-05-26 01:46:41 +0000 | [diff] [blame] | 75 | off_t filesize; |
| 76 | std::string name; |
Rafael Espindola | 8483dac | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 77 | }; |
Rafael Espindola | da51320 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 78 | |
Teresa Johnson | 2c50df6 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 79 | /// RAII wrapper to manage opening and releasing of a ld_plugin_input_file. |
| 80 | struct PluginInputFile { |
Teresa Johnson | 5af13d7 | 2015-12-16 21:37:48 +0000 | [diff] [blame] | 81 | void *Handle; |
Teresa Johnson | 2209e67 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 82 | std::unique_ptr<ld_plugin_input_file> File; |
Teresa Johnson | 2c50df6 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 83 | |
Teresa Johnson | 5af13d7 | 2015-12-16 21:37:48 +0000 | [diff] [blame] | 84 | PluginInputFile(void *Handle) : Handle(Handle) { |
Teresa Johnson | 2209e67 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 85 | File = llvm::make_unique<ld_plugin_input_file>(); |
| 86 | if (get_input_file(Handle, File.get()) != LDPS_OK) |
Teresa Johnson | 2c50df6 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 87 | message(LDPL_FATAL, "Failed to get file information"); |
| 88 | } |
| 89 | ~PluginInputFile() { |
Teresa Johnson | 2209e67 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 90 | // File would have been reset to nullptr if we moved this object |
| 91 | // to a new owner. |
| 92 | if (File) |
| 93 | if (release_input_file(Handle) != LDPS_OK) |
| 94 | message(LDPL_FATAL, "Failed to release file information"); |
Teresa Johnson | 2c50df6 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 95 | } |
Teresa Johnson | 2209e67 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 96 | |
| 97 | ld_plugin_input_file &file() { return *File; } |
| 98 | |
| 99 | PluginInputFile(PluginInputFile &&RHS) = default; |
| 100 | PluginInputFile &operator=(PluginInputFile &&RHS) = default; |
Teresa Johnson | 2c50df6 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Rafael Espindola | da51320 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 103 | struct ResolutionInfo { |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 104 | bool CanOmitFromDynSym = true; |
| 105 | bool DefaultVisibility = true; |
Teresa Johnson | ead8765 | 2018-03-14 13:26:18 +0000 | [diff] [blame] | 106 | bool CanInline = true; |
| 107 | bool IsUsedInRegularObj = false; |
Rafael Espindola | da51320 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 108 | }; |
Teresa Johnson | 2209e67 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 109 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 110 | } |
Rafael Espindola | 8483dac | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 111 | |
Rafael Espindola | 3c08473 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 112 | static ld_plugin_add_symbols add_symbols = nullptr; |
| 113 | static ld_plugin_get_symbols get_symbols = nullptr; |
| 114 | static ld_plugin_add_input_file add_input_file = nullptr; |
| 115 | static ld_plugin_set_extra_library_path set_extra_library_path = nullptr; |
| 116 | static ld_plugin_get_view get_view = nullptr; |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 117 | static bool IsExecutable = false; |
Bill Wendling | 0a4671b | 2018-07-12 20:35:58 +0000 | [diff] [blame] | 118 | static bool SplitSections = true; |
Evgeniy Stepanov | 49f70cc | 2017-05-22 21:11:35 +0000 | [diff] [blame] | 119 | static Optional<Reloc::Model> RelocationModel = None; |
Rafael Espindola | 8483dac | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 120 | static std::string output_name = ""; |
| 121 | static std::list<claimed_file> Modules; |
Teresa Johnson | bb83f89 | 2016-05-26 01:46:41 +0000 | [diff] [blame] | 122 | static DenseMap<int, void *> FDToLeaderHandle; |
Rafael Espindola | da51320 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 123 | static StringMap<ResolutionInfo> ResInfo; |
Rafael Espindola | 8483dac | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 124 | static std::vector<std::string> Cleanup; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 125 | |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 126 | namespace options { |
Rafael Espindola | f1ca1d4 | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 127 | enum OutputType { |
| 128 | OT_NORMAL, |
| 129 | OT_DISABLE, |
| 130 | OT_BC_ONLY, |
Teresa Johnson | 02e82c3 | 2019-01-02 23:48:00 +0000 | [diff] [blame] | 131 | OT_ASM_ONLY, |
Rafael Espindola | f1ca1d4 | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 132 | OT_SAVE_TEMPS |
| 133 | }; |
Rafael Espindola | f1ca1d4 | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 134 | static OutputType TheOutputType = OT_NORMAL; |
Peter Collingbourne | 416d8ec | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 135 | static unsigned OptLevel = 2; |
Teresa Johnson | 0794359 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 136 | // Default parallelism of 0 used to indicate that user did not specify. |
| 137 | // Actual parallelism default value depends on implementation. |
Teresa Johnson | 740d871 | 2016-10-19 17:35:01 +0000 | [diff] [blame] | 138 | // Currently only affects ThinLTO, where the default is |
| 139 | // llvm::heavyweight_hardware_concurrency. |
Teresa Johnson | 0794359 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 140 | static unsigned Parallelism = 0; |
Teresa Johnson | 468ae9f | 2016-09-23 20:35:19 +0000 | [diff] [blame] | 141 | // Default regular LTO codegen parallelism (number of partitions). |
| 142 | static unsigned ParallelCodeGenParallelismLevel = 1; |
Teresa Johnson | 15c79f6 | 2015-09-16 18:06:45 +0000 | [diff] [blame] | 143 | #ifdef NDEBUG |
| 144 | static bool DisableVerify = true; |
| 145 | #else |
| 146 | static bool DisableVerify = false; |
| 147 | #endif |
Shuxin Yang | cfaa636 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 148 | static std::string obj_path; |
Rafael Espindola | 11f403c | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 149 | static std::string extra_library_path; |
Rafael Espindola | cbb170d | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 150 | static std::string triple; |
Rafael Espindola | 2d643ef | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 151 | static std::string mcpu; |
Teresa Johnson | b97baa5 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 152 | // When the thinlto plugin option is specified, only read the function |
| 153 | // the information from intermediate files and write a combined |
| 154 | // global index for the ThinLTO backends. |
| 155 | static bool thinlto = false; |
Teresa Johnson | 2209e67 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 156 | // If false, all ThinLTO backend compilations through code gen are performed |
| 157 | // using multiple threads in the gold-plugin, before handing control back to |
Teresa Johnson | 27af205 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 158 | // gold. If true, write individual backend index files which reflect |
| 159 | // the import decisions, and exit afterwards. The assumption is |
Teresa Johnson | 2209e67 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 160 | // that the build system will launch the backend processes. |
| 161 | static bool thinlto_index_only = false; |
Teresa Johnson | 9493a84 | 2016-07-22 18:20:22 +0000 | [diff] [blame] | 162 | // If non-empty, holds the name of a file in which to write the list of |
| 163 | // oject files gold selected for inclusion in the link after symbol |
| 164 | // resolution (i.e. they had selected symbols). This will only be non-empty |
| 165 | // in the thinlto_index_only case. It is used to identify files, which may |
| 166 | // have originally been within archive libraries specified via |
| 167 | // --start-lib/--end-lib pairs, that should be included in the final |
| 168 | // native link process (since intervening function importing and inlining |
| 169 | // may change the symbol resolution detected in the final link and which |
| 170 | // files to include out of --start-lib/--end-lib libraries as a result). |
| 171 | static std::string thinlto_linked_objects_file; |
Teresa Johnson | 4b05ce2 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 172 | // If true, when generating individual index files for distributed backends, |
| 173 | // also generate a "${bitcodefile}.imports" file at the same location for each |
| 174 | // bitcode file, listing the files it imports from in plain text. This is to |
| 175 | // support distributed build file staging. |
| 176 | static bool thinlto_emit_imports_files = false; |
Teresa Johnson | a486381 | 2016-05-17 14:45:30 +0000 | [diff] [blame] | 177 | // Option to control where files for a distributed backend (the individual |
| 178 | // index files and optional imports files) are created. |
| 179 | // If specified, expects a string of the form "oldprefix:newprefix", and |
| 180 | // instead of generating these files in the same directory path as the |
| 181 | // corresponding bitcode file, will use a path formed by replacing the |
| 182 | // bitcode file's path prefix matching oldprefix with newprefix. |
| 183 | static std::string thinlto_prefix_replace; |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 184 | // Option to control the name of modules encoded in the individual index |
| 185 | // files for a distributed backend. This enables the use of minimized |
| 186 | // bitcode files for the thin link, assuming the name of the full bitcode |
| 187 | // file used in the backend differs just in some part of the file suffix. |
| 188 | // If specified, expects a string of the form "oldsuffix:newsuffix". |
| 189 | static std::string thinlto_object_suffix_replace; |
Teresa Johnson | 57b248a | 2016-08-24 15:11:47 +0000 | [diff] [blame] | 190 | // Optional path to a directory for caching ThinLTO objects. |
| 191 | static std::string cache_dir; |
Yi Kong | 74ae606 | 2017-09-18 23:24:55 +0000 | [diff] [blame] | 192 | // Optional pruning policy for ThinLTO caches. |
| 193 | static std::string cache_policy; |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 194 | // Additional options to pass into the code generator. |
Nick Lewycky | fc55def | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 195 | // Note: This array will contain all plugin options which are not claimed |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 196 | // as plugin exclusive to pass to the code generator. |
Rafael Espindola | d7a5d94 | 2014-07-29 19:17:44 +0000 | [diff] [blame] | 197 | static std::vector<const char *> extra; |
Dehao Chen | 7e43600 | 2016-12-16 16:48:46 +0000 | [diff] [blame] | 198 | // Sample profile file path |
| 199 | static std::string sample_profile; |
Sean Fertile | abd8a85 | 2017-10-05 01:48:42 +0000 | [diff] [blame] | 200 | // New pass manager |
| 201 | static bool new_pass_manager = false; |
Teresa Johnson | 65834ee | 2018-04-05 03:16:57 +0000 | [diff] [blame] | 202 | // Debug new pass manager |
| 203 | static bool debug_pass_manager = false; |
Yunlian Jiang | edd00e4 | 2018-04-13 05:03:28 +0000 | [diff] [blame] | 204 | // Directory to store the .dwo files. |
| 205 | static std::string dwo_dir; |
Florian Hahn | a2fbfc9 | 2018-04-20 10:18:36 +0000 | [diff] [blame] | 206 | /// Statistics output filename. |
| 207 | static std::string stats_file; |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 208 | |
Teresa Johnson | 48ba666 | 2018-04-18 13:25:23 +0000 | [diff] [blame] | 209 | // Optimization remarks filename and hotness options |
| 210 | static std::string OptRemarksFilename; |
| 211 | static bool OptRemarksWithHotness = false; |
| 212 | |
Nick Lewycky | 3987976 | 2015-08-05 21:16:02 +0000 | [diff] [blame] | 213 | static void process_plugin_option(const char *opt_) |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 214 | { |
Rafael Espindola | 3c08473 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 215 | if (opt_ == nullptr) |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 216 | return; |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 217 | llvm::StringRef opt = opt_; |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 218 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 219 | if (opt.startswith("mcpu=")) { |
Rafael Espindola | 2d643ef | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 220 | mcpu = opt.substr(strlen("mcpu=")); |
Rafael Espindola | 11f403c | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 221 | } else if (opt.startswith("extra-library-path=")) { |
| 222 | extra_library_path = opt.substr(strlen("extra_library_path=")); |
Rafael Espindola | 15af387 | 2010-08-10 16:32:15 +0000 | [diff] [blame] | 223 | } else if (opt.startswith("mtriple=")) { |
Rafael Espindola | cbb170d | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 224 | triple = opt.substr(strlen("mtriple=")); |
Shuxin Yang | cfaa636 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 225 | } else if (opt.startswith("obj-path=")) { |
| 226 | obj_path = opt.substr(strlen("obj-path=")); |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 227 | } else if (opt == "emit-llvm") { |
Rafael Espindola | f1ca1d4 | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 228 | TheOutputType = OT_BC_ONLY; |
Rafael Espindola | 25016f9 | 2014-10-29 23:54:45 +0000 | [diff] [blame] | 229 | } else if (opt == "save-temps") { |
Rafael Espindola | f1ca1d4 | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 230 | TheOutputType = OT_SAVE_TEMPS; |
| 231 | } else if (opt == "disable-output") { |
| 232 | TheOutputType = OT_DISABLE; |
Teresa Johnson | 02e82c3 | 2019-01-02 23:48:00 +0000 | [diff] [blame] | 233 | } else if (opt == "emit-asm") { |
| 234 | TheOutputType = OT_ASM_ONLY; |
Teresa Johnson | b97baa5 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 235 | } else if (opt == "thinlto") { |
| 236 | thinlto = true; |
Teresa Johnson | 2209e67 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 237 | } else if (opt == "thinlto-index-only") { |
| 238 | thinlto_index_only = true; |
Teresa Johnson | 9493a84 | 2016-07-22 18:20:22 +0000 | [diff] [blame] | 239 | } else if (opt.startswith("thinlto-index-only=")) { |
| 240 | thinlto_index_only = true; |
| 241 | thinlto_linked_objects_file = opt.substr(strlen("thinlto-index-only=")); |
Teresa Johnson | 4b05ce2 | 2016-05-10 15:54:09 +0000 | [diff] [blame] | 242 | } else if (opt == "thinlto-emit-imports-files") { |
| 243 | thinlto_emit_imports_files = true; |
Teresa Johnson | a486381 | 2016-05-17 14:45:30 +0000 | [diff] [blame] | 244 | } else if (opt.startswith("thinlto-prefix-replace=")) { |
| 245 | thinlto_prefix_replace = opt.substr(strlen("thinlto-prefix-replace=")); |
Benjamin Kramer | bf60ce0 | 2016-11-30 10:01:11 +0000 | [diff] [blame] | 246 | if (thinlto_prefix_replace.find(';') == std::string::npos) |
Reid Kleckner | 4e35fa5 | 2016-05-17 18:43:22 +0000 | [diff] [blame] | 247 | message(LDPL_FATAL, "thinlto-prefix-replace expects 'old;new' format"); |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 248 | } else if (opt.startswith("thinlto-object-suffix-replace=")) { |
| 249 | thinlto_object_suffix_replace = |
| 250 | opt.substr(strlen("thinlto-object-suffix-replace=")); |
| 251 | if (thinlto_object_suffix_replace.find(';') == std::string::npos) |
| 252 | message(LDPL_FATAL, |
| 253 | "thinlto-object-suffix-replace expects 'old;new' format"); |
Teresa Johnson | 57b248a | 2016-08-24 15:11:47 +0000 | [diff] [blame] | 254 | } else if (opt.startswith("cache-dir=")) { |
| 255 | cache_dir = opt.substr(strlen("cache-dir=")); |
Yi Kong | 74ae606 | 2017-09-18 23:24:55 +0000 | [diff] [blame] | 256 | } else if (opt.startswith("cache-policy=")) { |
| 257 | cache_policy = opt.substr(strlen("cache-policy=")); |
Peter Collingbourne | 416d8ec | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 258 | } else if (opt.size() == 2 && opt[0] == 'O') { |
| 259 | if (opt[1] < '0' || opt[1] > '3') |
Peter Collingbourne | 0a1d37b | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 260 | message(LDPL_FATAL, "Optimization level must be between 0 and 3"); |
Peter Collingbourne | 416d8ec | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 261 | OptLevel = opt[1] - '0'; |
Peter Collingbourne | 0a1d37b | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 262 | } else if (opt.startswith("jobs=")) { |
| 263 | if (StringRef(opt_ + 5).getAsInteger(10, Parallelism)) |
| 264 | message(LDPL_FATAL, "Invalid parallelism level: %s", opt_ + 5); |
Teresa Johnson | 468ae9f | 2016-09-23 20:35:19 +0000 | [diff] [blame] | 265 | } else if (opt.startswith("lto-partitions=")) { |
| 266 | if (opt.substr(strlen("lto-partitions=")) |
| 267 | .getAsInteger(10, ParallelCodeGenParallelismLevel)) |
| 268 | message(LDPL_FATAL, "Invalid codegen partition level: %s", opt_ + 5); |
Teresa Johnson | 15c79f6 | 2015-09-16 18:06:45 +0000 | [diff] [blame] | 269 | } else if (opt == "disable-verify") { |
| 270 | DisableVerify = true; |
Dehao Chen | 7e43600 | 2016-12-16 16:48:46 +0000 | [diff] [blame] | 271 | } else if (opt.startswith("sample-profile=")) { |
| 272 | sample_profile= opt.substr(strlen("sample-profile=")); |
Sean Fertile | abd8a85 | 2017-10-05 01:48:42 +0000 | [diff] [blame] | 273 | } else if (opt == "new-pass-manager") { |
| 274 | new_pass_manager = true; |
Teresa Johnson | 65834ee | 2018-04-05 03:16:57 +0000 | [diff] [blame] | 275 | } else if (opt == "debug-pass-manager") { |
| 276 | debug_pass_manager = true; |
Yunlian Jiang | edd00e4 | 2018-04-13 05:03:28 +0000 | [diff] [blame] | 277 | } else if (opt.startswith("dwo_dir=")) { |
| 278 | dwo_dir = opt.substr(strlen("dwo_dir=")); |
Teresa Johnson | 48ba666 | 2018-04-18 13:25:23 +0000 | [diff] [blame] | 279 | } else if (opt.startswith("opt-remarks-filename=")) { |
| 280 | OptRemarksFilename = opt.substr(strlen("opt-remarks-filename=")); |
| 281 | } else if (opt == "opt-remarks-with-hotness") { |
| 282 | OptRemarksWithHotness = true; |
Florian Hahn | a2fbfc9 | 2018-04-20 10:18:36 +0000 | [diff] [blame] | 283 | } else if (opt.startswith("stats-file=")) { |
| 284 | stats_file = opt.substr(strlen("stats-file=")); |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 285 | } else { |
| 286 | // Save this option to pass to the code generator. |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 287 | // ParseCommandLineOptions() expects argv[0] to be program name. Lazily |
| 288 | // add that. |
| 289 | if (extra.empty()) |
| 290 | extra.push_back("LLVMgold"); |
| 291 | |
Rafael Espindola | d7a5d94 | 2014-07-29 19:17:44 +0000 | [diff] [blame] | 292 | extra.push_back(opt_); |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
Dan Gohman | f692003 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 297 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 298 | int *claimed); |
| 299 | static ld_plugin_status all_symbols_read_hook(void); |
| 300 | static ld_plugin_status cleanup_hook(void); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 301 | |
| 302 | extern "C" ld_plugin_status onload(ld_plugin_tv *tv); |
| 303 | ld_plugin_status onload(ld_plugin_tv *tv) { |
Peter Collingbourne | 59349ad | 2014-07-03 23:28:03 +0000 | [diff] [blame] | 304 | InitializeAllTargetInfos(); |
| 305 | InitializeAllTargets(); |
| 306 | InitializeAllTargetMCs(); |
| 307 | InitializeAllAsmParsers(); |
| 308 | InitializeAllAsmPrinters(); |
| 309 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 310 | // We're given a pointer to the first transfer vector. We read through them |
| 311 | // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values |
| 312 | // contain pointers to functions that we need to call to register our own |
| 313 | // hooks. The others are addresses of functions we can use to call into gold |
| 314 | // for services. |
| 315 | |
| 316 | bool registeredClaimFile = false; |
Rafael Espindola | 0b5745a | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 317 | bool RegisteredAllSymbolsRead = false; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 318 | |
| 319 | for (; tv->tv_tag != LDPT_NULL; ++tv) { |
Evgeniy Stepanov | f0c747b | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 320 | // Cast tv_tag to int to allow values not in "enum ld_plugin_tag", like, for |
| 321 | // example, LDPT_GET_SYMBOLS_V3 when building against an older plugin-api.h |
| 322 | // header. |
| 323 | switch (static_cast<int>(tv->tv_tag)) { |
| 324 | case LDPT_OUTPUT_NAME: |
| 325 | output_name = tv->tv_u.tv_string; |
| 326 | break; |
| 327 | case LDPT_LINKER_OUTPUT: |
| 328 | switch (tv->tv_u.tv_val) { |
| 329 | case LDPO_REL: // .o |
Evgeniy Stepanov | 49f70cc | 2017-05-22 21:11:35 +0000 | [diff] [blame] | 330 | IsExecutable = false; |
Bill Wendling | 0a4671b | 2018-07-12 20:35:58 +0000 | [diff] [blame] | 331 | SplitSections = false; |
Evgeniy Stepanov | 49f70cc | 2017-05-22 21:11:35 +0000 | [diff] [blame] | 332 | break; |
Evgeniy Stepanov | f0c747b | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 333 | case LDPO_DYN: // .so |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 334 | IsExecutable = false; |
| 335 | RelocationModel = Reloc::PIC_; |
| 336 | break; |
Evgeniy Stepanov | f0c747b | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 337 | case LDPO_PIE: // position independent executable |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 338 | IsExecutable = true; |
Evgeniy Stepanov | f0c747b | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 339 | RelocationModel = Reloc::PIC_; |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 340 | break; |
Evgeniy Stepanov | f0c747b | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 341 | case LDPO_EXEC: // .exe |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 342 | IsExecutable = true; |
Evgeniy Stepanov | f0c747b | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 343 | RelocationModel = Reloc::Static; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 344 | break; |
| 345 | default: |
Evgeniy Stepanov | f0c747b | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 346 | message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val); |
| 347 | return LDPS_ERR; |
| 348 | } |
| 349 | break; |
| 350 | case LDPT_OPTION: |
| 351 | options::process_plugin_option(tv->tv_u.tv_string); |
| 352 | break; |
| 353 | case LDPT_REGISTER_CLAIM_FILE_HOOK: { |
| 354 | ld_plugin_register_claim_file callback; |
| 355 | callback = tv->tv_u.tv_register_claim_file; |
| 356 | |
| 357 | if (callback(claim_file_hook) != LDPS_OK) |
| 358 | return LDPS_ERR; |
| 359 | |
| 360 | registeredClaimFile = true; |
| 361 | } break; |
| 362 | case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: { |
| 363 | ld_plugin_register_all_symbols_read callback; |
| 364 | callback = tv->tv_u.tv_register_all_symbols_read; |
| 365 | |
| 366 | if (callback(all_symbols_read_hook) != LDPS_OK) |
| 367 | return LDPS_ERR; |
| 368 | |
| 369 | RegisteredAllSymbolsRead = true; |
| 370 | } break; |
| 371 | case LDPT_REGISTER_CLEANUP_HOOK: { |
| 372 | ld_plugin_register_cleanup callback; |
| 373 | callback = tv->tv_u.tv_register_cleanup; |
| 374 | |
| 375 | if (callback(cleanup_hook) != LDPS_OK) |
| 376 | return LDPS_ERR; |
| 377 | } break; |
| 378 | case LDPT_GET_INPUT_FILE: |
| 379 | get_input_file = tv->tv_u.tv_get_input_file; |
| 380 | break; |
| 381 | case LDPT_RELEASE_INPUT_FILE: |
| 382 | release_input_file = tv->tv_u.tv_release_input_file; |
| 383 | break; |
| 384 | case LDPT_ADD_SYMBOLS: |
| 385 | add_symbols = tv->tv_u.tv_add_symbols; |
| 386 | break; |
| 387 | case LDPT_GET_SYMBOLS_V2: |
| 388 | // Do not override get_symbols_v3 with get_symbols_v2. |
| 389 | if (!get_symbols) |
| 390 | get_symbols = tv->tv_u.tv_get_symbols; |
| 391 | break; |
| 392 | case LDPT_GET_SYMBOLS_V3: |
| 393 | get_symbols = tv->tv_u.tv_get_symbols; |
| 394 | break; |
| 395 | case LDPT_ADD_INPUT_FILE: |
| 396 | add_input_file = tv->tv_u.tv_add_input_file; |
| 397 | break; |
| 398 | case LDPT_SET_EXTRA_LIBRARY_PATH: |
| 399 | set_extra_library_path = tv->tv_u.tv_set_extra_library_path; |
| 400 | break; |
| 401 | case LDPT_GET_VIEW: |
| 402 | get_view = tv->tv_u.tv_get_view; |
| 403 | break; |
| 404 | case LDPT_MESSAGE: |
| 405 | message = tv->tv_u.tv_message; |
| 406 | break; |
Teresa Johnson | ead8765 | 2018-03-14 13:26:18 +0000 | [diff] [blame] | 407 | case LDPT_GET_WRAP_SYMBOLS: |
| 408 | // FIXME: When binutils 2.31 (containing gold 1.16) is the minimum |
| 409 | // required version, this should be changed to: |
| 410 | // get_wrap_symbols = tv->tv_u.tv_get_wrap_symbols; |
| 411 | get_wrap_symbols = |
Teresa Johnson | 64c4f71 | 2018-03-14 14:00:57 +0000 | [diff] [blame] | 412 | (ld_plugin_get_wrap_symbols)tv->tv_u.tv_message; |
Teresa Johnson | ead8765 | 2018-03-14 13:26:18 +0000 | [diff] [blame] | 413 | break; |
Evgeniy Stepanov | f0c747b | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 414 | default: |
| 415 | break; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Rafael Espindola | 98c507e | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 419 | if (!registeredClaimFile) { |
Rafael Espindola | 601bb09 | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 420 | message(LDPL_ERROR, "register_claim_file not passed to LLVMgold."); |
Rafael Espindola | 6210a94 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 421 | return LDPS_ERR; |
| 422 | } |
Rafael Espindola | 98c507e | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 423 | if (!add_symbols) { |
Rafael Espindola | 601bb09 | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 424 | message(LDPL_ERROR, "add_symbols not passed to LLVMgold."); |
Rafael Espindola | 6210a94 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 425 | return LDPS_ERR; |
| 426 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 427 | |
Rafael Espindola | 474f223 | 2014-06-19 22:20:07 +0000 | [diff] [blame] | 428 | if (!RegisteredAllSymbolsRead) |
| 429 | return LDPS_OK; |
Rafael Espindola | 0b5745a | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 430 | |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 431 | if (!get_input_file) { |
| 432 | message(LDPL_ERROR, "get_input_file not passed to LLVMgold."); |
| 433 | return LDPS_ERR; |
Rafael Espindola | f38fdf1 | 2014-06-19 22:54:47 +0000 | [diff] [blame] | 434 | } |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 435 | if (!release_input_file) { |
Marianne Mailhot-Sarrasin | d9529c5 | 2016-03-30 12:20:53 +0000 | [diff] [blame] | 436 | message(LDPL_ERROR, "release_input_file not passed to LLVMgold."); |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 437 | return LDPS_ERR; |
Tom Roeder | 9ad9c96 | 2014-06-26 20:43:27 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 440 | return LDPS_OK; |
| 441 | } |
| 442 | |
NAKAMURA Takumi | fed4c6e | 2015-11-19 10:43:44 +0000 | [diff] [blame] | 443 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
Rafael Espindola | 68016e0 | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 444 | std::string ErrStorage; |
| 445 | { |
| 446 | raw_string_ostream OS(ErrStorage); |
| 447 | DiagnosticPrinterRawOStream DP(OS); |
| 448 | DI.print(DP); |
| 449 | } |
Rafael Espindola | de83324 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 450 | ld_plugin_level Level; |
| 451 | switch (DI.getSeverity()) { |
| 452 | case DS_Error: |
Mandeep Singh Grang | badd06e | 2018-11-01 23:34:12 +0000 | [diff] [blame] | 453 | Level = LDPL_FATAL; |
| 454 | break; |
Rafael Espindola | de83324 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 455 | case DS_Warning: |
| 456 | Level = LDPL_WARNING; |
| 457 | break; |
| 458 | case DS_Note: |
Rafael Espindola | c90e7f7 | 2015-03-04 18:51:45 +0000 | [diff] [blame] | 459 | case DS_Remark: |
Rafael Espindola | de83324 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 460 | Level = LDPL_INFO; |
| 461 | break; |
Rafael Espindola | de83324 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 462 | } |
| 463 | message(Level, "LLVM gold plugin: %s", ErrStorage.c_str()); |
Rafael Espindola | 68016e0 | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 466 | static void check(Error E, std::string Msg = "LLVM gold plugin") { |
Mehdi Amini | 3619728 | 2016-11-11 06:04:30 +0000 | [diff] [blame] | 467 | handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) -> Error { |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 468 | message(LDPL_FATAL, "%s: %s", Msg.c_str(), EIB.message().c_str()); |
| 469 | return Error::success(); |
| 470 | }); |
NAKAMURA Takumi | fed4c6e | 2015-11-19 10:43:44 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 473 | template <typename T> static T check(Expected<T> E) { |
| 474 | if (E) |
| 475 | return std::move(*E); |
| 476 | check(E.takeError()); |
| 477 | return T(); |
Rafael Espindola | da51320 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Rafael Espindola | 04e43e6 | 2014-07-06 14:31:22 +0000 | [diff] [blame] | 480 | /// Called by gold to see whether this file is one that our plugin can handle. |
| 481 | /// We'll try to open it and register all the symbols with add_symbol if |
| 482 | /// possible. |
Dan Gohman | f692003 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 483 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 484 | int *claimed) { |
Rafael Espindola | b5e9bf0 | 2014-08-27 20:25:55 +0000 | [diff] [blame] | 485 | MemoryBufferRef BufferRef; |
| 486 | std::unique_ptr<MemoryBuffer> Buffer; |
Rafael Espindola | e578252 | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 487 | if (get_view) { |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 488 | const void *view; |
Rafael Espindola | e578252 | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 489 | if (get_view(file->handle, &view) != LDPS_OK) { |
Rafael Espindola | 601bb09 | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 490 | message(LDPL_ERROR, "Failed to get a view of %s", file->name); |
Rafael Espindola | e578252 | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 491 | return LDPS_ERR; |
| 492 | } |
Nick Lewycky | 3987976 | 2015-08-05 21:16:02 +0000 | [diff] [blame] | 493 | BufferRef = |
| 494 | MemoryBufferRef(StringRef((const char *)view, file->filesize), ""); |
Ivan Krasin | ccb7c90 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 495 | } else { |
Ivan Krasin | 71280b5 | 2011-09-15 23:13:00 +0000 | [diff] [blame] | 496 | int64_t offset = 0; |
Nick Lewycky | c1da886 | 2009-02-05 04:14:23 +0000 | [diff] [blame] | 497 | // Gold has found what might be IR part-way inside of a file, such as |
| 498 | // an .a archive. |
Ivan Krasin | ccb7c90 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 499 | if (file->offset) { |
| 500 | offset = file->offset; |
| 501 | } |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 502 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 503 | MemoryBuffer::getOpenFileSlice(file->fd, file->name, file->filesize, |
| 504 | offset); |
| 505 | if (std::error_code EC = BufferOrErr.getError()) { |
Rafael Espindola | 601bb09 | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 506 | message(LDPL_ERROR, EC.message().c_str()); |
Ivan Krasin | ccb7c90 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 507 | return LDPS_ERR; |
| 508 | } |
Rafael Espindola | b5e9bf0 | 2014-08-27 20:25:55 +0000 | [diff] [blame] | 509 | Buffer = std::move(BufferOrErr.get()); |
| 510 | BufferRef = Buffer->getMemBufferRef(); |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 511 | } |
Ivan Krasin | ccb7c90 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 512 | |
Rafael Espindola | df7568e | 2014-07-29 20:46:19 +0000 | [diff] [blame] | 513 | *claimed = 1; |
| 514 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 515 | Expected<std::unique_ptr<InputFile>> ObjOrErr = InputFile::create(BufferRef); |
| 516 | if (!ObjOrErr) { |
| 517 | handleAllErrors(ObjOrErr.takeError(), [&](const ErrorInfoBase &EI) { |
| 518 | std::error_code EC = EI.convertToErrorCode(); |
| 519 | if (EC == object::object_error::invalid_file_type || |
| 520 | EC == object::object_error::bitcode_section_not_found) |
| 521 | *claimed = 0; |
| 522 | else |
Peter Collingbourne | 957cbf8 | 2017-03-31 04:47:07 +0000 | [diff] [blame] | 523 | message(LDPL_FATAL, |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 524 | "LLVM gold plugin has failed to create LTO module: %s", |
| 525 | EI.message().c_str()); |
| 526 | }); |
| 527 | |
| 528 | return *claimed ? LDPS_ERR : LDPS_OK; |
Ivan Krasin | c170f5f | 2011-09-09 00:14:04 +0000 | [diff] [blame] | 529 | } |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 530 | |
| 531 | std::unique_ptr<InputFile> Obj = std::move(*ObjOrErr); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 532 | |
Dehao Chen | 9c353a8 | 2017-07-10 15:31:53 +0000 | [diff] [blame] | 533 | Modules.emplace_back(); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 534 | claimed_file &cf = Modules.back(); |
Rafael Espindola | cbb170d | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 535 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 536 | cf.handle = file->handle; |
Teresa Johnson | bb83f89 | 2016-05-26 01:46:41 +0000 | [diff] [blame] | 537 | // Keep track of the first handle for each file descriptor, since there are |
| 538 | // multiple in the case of an archive. This is used later in the case of |
| 539 | // ThinLTO parallel backends to ensure that each file is only opened and |
| 540 | // released once. |
| 541 | auto LeaderHandle = |
| 542 | FDToLeaderHandle.insert(std::make_pair(file->fd, file->handle)).first; |
| 543 | cf.leader_handle = LeaderHandle->second; |
| 544 | // Save the filesize since for parallel ThinLTO backends we can only |
| 545 | // invoke get_input_file once per archive (only for the leader handle). |
| 546 | cf.filesize = file->filesize; |
| 547 | // In the case of an archive library, all but the first member must have a |
| 548 | // non-zero offset, which we can append to the file name to obtain a |
| 549 | // unique name. |
| 550 | cf.name = file->name; |
| 551 | if (file->offset) |
| 552 | cf.name += ".llvm." + std::to_string(file->offset) + "." + |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 553 | sys::path::filename(Obj->getSourceFileName()).str(); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 554 | |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 555 | for (auto &Sym : Obj->symbols()) { |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 556 | cf.syms.push_back(ld_plugin_symbol()); |
| 557 | ld_plugin_symbol &sym = cf.syms.back(); |
Rafael Espindola | 3c08473 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 558 | sym.version = nullptr; |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 559 | StringRef Name = Sym.getName(); |
| 560 | sym.name = strdup(Name.str().c_str()); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 561 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 562 | ResolutionInfo &Res = ResInfo[Name]; |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 563 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 564 | Res.CanOmitFromDynSym &= Sym.canBeOmittedFromSymbolTable(); |
Rafael Espindola | da51320 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 565 | |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 566 | sym.visibility = LDPV_DEFAULT; |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 567 | GlobalValue::VisibilityTypes Vis = Sym.getVisibility(); |
| 568 | if (Vis != GlobalValue::DefaultVisibility) |
| 569 | Res.DefaultVisibility = false; |
| 570 | switch (Vis) { |
| 571 | case GlobalValue::DefaultVisibility: |
| 572 | break; |
| 573 | case GlobalValue::HiddenVisibility: |
| 574 | sym.visibility = LDPV_HIDDEN; |
| 575 | break; |
| 576 | case GlobalValue::ProtectedVisibility: |
| 577 | sym.visibility = LDPV_PROTECTED; |
| 578 | break; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Peter Collingbourne | aba00ee | 2017-03-28 22:31:35 +0000 | [diff] [blame] | 581 | if (Sym.isUndefined()) { |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 582 | sym.def = LDPK_UNDEF; |
Peter Collingbourne | aba00ee | 2017-03-28 22:31:35 +0000 | [diff] [blame] | 583 | if (Sym.isWeak()) |
Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 584 | sym.def = LDPK_WEAKUNDEF; |
Peter Collingbourne | aba00ee | 2017-03-28 22:31:35 +0000 | [diff] [blame] | 585 | } else if (Sym.isCommon()) |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 586 | sym.def = LDPK_COMMON; |
Peter Collingbourne | aba00ee | 2017-03-28 22:31:35 +0000 | [diff] [blame] | 587 | else if (Sym.isWeak()) |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 588 | sym.def = LDPK_WEAKDEF; |
| 589 | else |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 590 | sym.def = LDPK_DEF; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 591 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 592 | sym.size = 0; |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 593 | sym.comdat_key = nullptr; |
Peter Collingbourne | 957cbf8 | 2017-03-31 04:47:07 +0000 | [diff] [blame] | 594 | int CI = Sym.getComdatIndex(); |
Rafael Espindola | 050bf6c | 2016-10-25 12:02:03 +0000 | [diff] [blame] | 595 | if (CI != -1) { |
| 596 | StringRef C = Obj->getComdatTable()[CI]; |
Rafael Espindola | 58121fb | 2016-10-17 18:51:02 +0000 | [diff] [blame] | 597 | sym.comdat_key = strdup(C.str().c_str()); |
Rafael Espindola | 050bf6c | 2016-10-25 12:02:03 +0000 | [diff] [blame] | 598 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 599 | |
| 600 | sym.resolution = LDPR_UNKNOWN; |
| 601 | } |
| 602 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 603 | if (!cf.syms.empty()) { |
Nick Lewycky | 3987976 | 2015-08-05 21:16:02 +0000 | [diff] [blame] | 604 | if (add_symbols(cf.handle, cf.syms.size(), cf.syms.data()) != LDPS_OK) { |
Rafael Espindola | 601bb09 | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 605 | message(LDPL_ERROR, "Unable to add symbols!"); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 606 | return LDPS_ERR; |
| 607 | } |
| 608 | } |
| 609 | |
Teresa Johnson | ead8765 | 2018-03-14 13:26:18 +0000 | [diff] [blame] | 610 | // Handle any --wrap options passed to gold, which are than passed |
| 611 | // along to the plugin. |
| 612 | if (get_wrap_symbols) { |
| 613 | const char **wrap_symbols; |
| 614 | uint64_t count = 0; |
| 615 | if (get_wrap_symbols(&count, &wrap_symbols) != LDPS_OK) { |
| 616 | message(LDPL_ERROR, "Unable to get wrap symbols!"); |
| 617 | return LDPS_ERR; |
| 618 | } |
| 619 | for (uint64_t i = 0; i < count; i++) { |
| 620 | StringRef Name = wrap_symbols[i]; |
| 621 | ResolutionInfo &Res = ResInfo[Name]; |
| 622 | ResolutionInfo &WrapRes = ResInfo["__wrap_" + Name.str()]; |
| 623 | ResolutionInfo &RealRes = ResInfo["__real_" + Name.str()]; |
| 624 | // Tell LTO not to inline symbols that will be overwritten. |
| 625 | Res.CanInline = false; |
| 626 | RealRes.CanInline = false; |
| 627 | // Tell LTO not to eliminate symbols that will be used after renaming. |
| 628 | Res.IsUsedInRegularObj = true; |
| 629 | WrapRes.IsUsedInRegularObj = true; |
| 630 | } |
| 631 | } |
| 632 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 633 | return LDPS_OK; |
| 634 | } |
| 635 | |
Rafael Espindola | 3d1c71a | 2014-12-23 18:18:37 +0000 | [diff] [blame] | 636 | static void freeSymName(ld_plugin_symbol &Sym) { |
| 637 | free(Sym.name); |
| 638 | free(Sym.comdat_key); |
| 639 | Sym.name = nullptr; |
| 640 | Sym.comdat_key = nullptr; |
| 641 | } |
| 642 | |
Teresa Johnson | 0794359 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 643 | /// Helper to get a file's symbols and a view into it via gold callbacks. |
| 644 | static const void *getSymbolsAndView(claimed_file &F) { |
Benjamin Kramer | b644c0a | 2016-03-08 14:02:46 +0000 | [diff] [blame] | 645 | ld_plugin_status status = get_symbols(F.handle, F.syms.size(), F.syms.data()); |
Evgeniy Stepanov | f0c747b | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 646 | if (status == LDPS_NO_SYMS) |
| 647 | return nullptr; |
Teresa Johnson | b97baa5 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 648 | |
Evgeniy Stepanov | f0c747b | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 649 | if (status != LDPS_OK) |
Teresa Johnson | b97baa5 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 650 | message(LDPL_FATAL, "Failed to get symbol information"); |
| 651 | |
| 652 | const void *View; |
| 653 | if (get_view(F.handle, &View) != LDPS_OK) |
| 654 | message(LDPL_FATAL, "Failed to get a view of file"); |
| 655 | |
Teresa Johnson | 0794359 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 656 | return View; |
| 657 | } |
| 658 | |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 659 | /// Parse the thinlto-object-suffix-replace option into the \p OldSuffix and |
| 660 | /// \p NewSuffix strings, if it was specified. |
| 661 | static void getThinLTOOldAndNewSuffix(std::string &OldSuffix, |
| 662 | std::string &NewSuffix) { |
| 663 | assert(options::thinlto_object_suffix_replace.empty() || |
| 664 | options::thinlto_object_suffix_replace.find(";") != StringRef::npos); |
| 665 | StringRef SuffixReplace = options::thinlto_object_suffix_replace; |
Benjamin Kramer | e5d9cb5 | 2017-08-10 19:28:00 +0000 | [diff] [blame] | 666 | std::tie(OldSuffix, NewSuffix) = SuffixReplace.split(';'); |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /// Given the original \p Path to an output file, replace any filename |
| 670 | /// suffix matching \p OldSuffix with \p NewSuffix. |
Benjamin Kramer | e5d9cb5 | 2017-08-10 19:28:00 +0000 | [diff] [blame] | 671 | static std::string getThinLTOObjectFileName(StringRef Path, StringRef OldSuffix, |
| 672 | StringRef NewSuffix) { |
Fangrui Song | 1f25117 | 2018-08-22 02:11:36 +0000 | [diff] [blame] | 673 | if (Path.consume_back(OldSuffix)) |
| 674 | return (Path + NewSuffix).str(); |
| 675 | return Path; |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Teresa Johnson | a63ab3c9 | 2017-07-25 19:42:32 +0000 | [diff] [blame] | 678 | // Returns true if S is valid as a C language identifier. |
| 679 | static bool isValidCIdentifier(StringRef S) { |
George Rimar | c7c50af | 2017-10-04 11:00:30 +0000 | [diff] [blame] | 680 | return !S.empty() && (isAlpha(S[0]) || S[0] == '_') && |
| 681 | std::all_of(S.begin() + 1, S.end(), |
| 682 | [](char C) { return C == '_' || isAlnum(C); }); |
Teresa Johnson | a63ab3c9 | 2017-07-25 19:42:32 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Eugene Leviant | 72044ef | 2017-12-15 09:18:21 +0000 | [diff] [blame] | 685 | static bool isUndefined(ld_plugin_symbol &Sym) { |
| 686 | return Sym.def == LDPK_UNDEF || Sym.def == LDPK_WEAKUNDEF; |
| 687 | } |
| 688 | |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 689 | static void addModule(LTO &Lto, claimed_file &F, const void *View, |
| 690 | StringRef Filename) { |
| 691 | MemoryBufferRef BufferRef(StringRef((const char *)View, F.filesize), |
| 692 | Filename); |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 693 | Expected<std::unique_ptr<InputFile>> ObjOrErr = InputFile::create(BufferRef); |
Teresa Johnson | f1f6063 | 2015-11-21 21:55:48 +0000 | [diff] [blame] | 694 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 695 | if (!ObjOrErr) |
Peter Collingbourne | 394be6c | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 696 | message(LDPL_FATAL, "Could not read bitcode from file : %s", |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 697 | toString(ObjOrErr.takeError()).c_str()); |
Peter Collingbourne | 394be6c | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 698 | |
Rafael Espindola | 4f20ed1 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 699 | unsigned SymNum = 0; |
Teresa Johnson | a63ab3c9 | 2017-07-25 19:42:32 +0000 | [diff] [blame] | 700 | std::unique_ptr<InputFile> Input = std::move(ObjOrErr.get()); |
| 701 | auto InputFileSyms = Input->symbols(); |
| 702 | assert(InputFileSyms.size() == F.syms.size()); |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 703 | std::vector<SymbolResolution> Resols(F.syms.size()); |
Peter Collingbourne | 6f025c1 | 2016-09-14 02:55:16 +0000 | [diff] [blame] | 704 | for (ld_plugin_symbol &Sym : F.syms) { |
Teresa Johnson | a63ab3c9 | 2017-07-25 19:42:32 +0000 | [diff] [blame] | 705 | const InputFile::Symbol &InpSym = InputFileSyms[SymNum]; |
Peter Collingbourne | 6f025c1 | 2016-09-14 02:55:16 +0000 | [diff] [blame] | 706 | SymbolResolution &R = Resols[SymNum++]; |
Rafael Espindola | 4f20ed1 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 707 | |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 708 | ld_plugin_symbol_resolution Resolution = |
| 709 | (ld_plugin_symbol_resolution)Sym.resolution; |
| 710 | |
Rafael Espindola | da51320 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 711 | ResolutionInfo &Res = ResInfo[Sym.name]; |
Rafael Espindola | 19f3868 | 2014-09-09 20:08:22 +0000 | [diff] [blame] | 712 | |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 713 | switch (Resolution) { |
| 714 | case LDPR_UNKNOWN: |
| 715 | llvm_unreachable("Unexpected resolution"); |
| 716 | |
| 717 | case LDPR_RESOLVED_IR: |
| 718 | case LDPR_RESOLVED_EXEC: |
| 719 | case LDPR_RESOLVED_DYN: |
Rafael Espindola | da51320 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 720 | case LDPR_PREEMPTED_IR: |
| 721 | case LDPR_PREEMPTED_REG: |
Rafael Espindola | 5f92811 | 2015-01-14 13:53:50 +0000 | [diff] [blame] | 722 | case LDPR_UNDEF: |
Rafael Espindola | 5f92811 | 2015-01-14 13:53:50 +0000 | [diff] [blame] | 723 | break; |
| 724 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 725 | case LDPR_PREVAILING_DEF_IRONLY: |
Eugene Leviant | 72044ef | 2017-12-15 09:18:21 +0000 | [diff] [blame] | 726 | R.Prevailing = !isUndefined(Sym); |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 727 | break; |
Teresa Johnson | 9fd9770 | 2016-08-11 12:56:40 +0000 | [diff] [blame] | 728 | |
Teresa Johnson | 6f0ab85 | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 729 | case LDPR_PREVAILING_DEF: |
Eugene Leviant | 72044ef | 2017-12-15 09:18:21 +0000 | [diff] [blame] | 730 | R.Prevailing = !isUndefined(Sym); |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 731 | R.VisibleToRegularObj = true; |
Teresa Johnson | 6f0ab85 | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 732 | break; |
| 733 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 734 | case LDPR_PREVAILING_DEF_IRONLY_EXP: |
Eugene Leviant | 72044ef | 2017-12-15 09:18:21 +0000 | [diff] [blame] | 735 | R.Prevailing = !isUndefined(Sym); |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 736 | if (!Res.CanOmitFromDynSym) |
| 737 | R.VisibleToRegularObj = true; |
Teresa Johnson | 6f0ab85 | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 738 | break; |
| 739 | } |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 740 | |
Teresa Johnson | a63ab3c9 | 2017-07-25 19:42:32 +0000 | [diff] [blame] | 741 | // If the symbol has a C identifier section name, we need to mark |
| 742 | // it as visible to a regular object so that LTO will keep it around |
| 743 | // to ensure the linker generates special __start_<secname> and |
| 744 | // __stop_<secname> symbols which may be used elsewhere. |
| 745 | if (isValidCIdentifier(InpSym.getSectionName())) |
| 746 | R.VisibleToRegularObj = true; |
| 747 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 748 | if (Resolution != LDPR_RESOLVED_DYN && Resolution != LDPR_UNDEF && |
| 749 | (IsExecutable || !Res.DefaultVisibility)) |
| 750 | R.FinalDefinitionInLinkageUnit = true; |
| 751 | |
Teresa Johnson | ead8765 | 2018-03-14 13:26:18 +0000 | [diff] [blame] | 752 | if (!Res.CanInline) |
| 753 | R.LinkerRedefined = true; |
| 754 | |
| 755 | if (Res.IsUsedInRegularObj) |
| 756 | R.VisibleToRegularObj = true; |
| 757 | |
Rafael Espindola | 3d1c71a | 2014-12-23 18:18:37 +0000 | [diff] [blame] | 758 | freeSymName(Sym); |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Teresa Johnson | a63ab3c9 | 2017-07-25 19:42:32 +0000 | [diff] [blame] | 761 | check(Lto.add(std::move(Input), Resols), |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 762 | std::string("Failed to link module ") + F.name); |
Rafael Espindola | 25016f9 | 2014-10-29 23:54:45 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Benjamin Kramer | e5d9cb5 | 2017-08-10 19:28:00 +0000 | [diff] [blame] | 765 | static void recordFile(const std::string &Filename, bool TempOutFile) { |
Teresa Johnson | 0794359 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 766 | if (add_input_file(Filename.c_str()) != LDPS_OK) |
| 767 | message(LDPL_FATAL, |
| 768 | "Unable to add .o file to the link. File left behind in: %s", |
| 769 | Filename.c_str()); |
| 770 | if (TempOutFile) |
Benjamin Kramer | e5d9cb5 | 2017-08-10 19:28:00 +0000 | [diff] [blame] | 771 | Cleanup.push_back(Filename); |
Teresa Johnson | 0794359 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 772 | } |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 773 | |
Mehdi Amini | bba5e18 | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 774 | /// Return the desired output filename given a base input name, a flag |
| 775 | /// indicating whether a temp file should be generated, and an optional task id. |
| 776 | /// The new filename generated is returned in \p NewFilename. |
Benjamin Kramer | e5d9cb5 | 2017-08-10 19:28:00 +0000 | [diff] [blame] | 777 | static int getOutputFileName(StringRef InFilename, bool TempOutFile, |
Benjamin Kramer | 01823a6 | 2017-08-10 17:38:41 +0000 | [diff] [blame] | 778 | SmallString<128> &NewFilename, int TaskID) { |
| 779 | int FD = -1; |
Teresa Johnson | 0794359 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 780 | if (TempOutFile) { |
| 781 | std::error_code EC = |
Benjamin Kramer | 01823a6 | 2017-08-10 17:38:41 +0000 | [diff] [blame] | 782 | sys::fs::createTemporaryFile("lto-llvm", "o", FD, NewFilename); |
Teresa Johnson | 0794359 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 783 | if (EC) |
| 784 | message(LDPL_FATAL, "Could not create temporary file: %s", |
| 785 | EC.message().c_str()); |
| 786 | } else { |
| 787 | NewFilename = InFilename; |
Peter Collingbourne | 289792d | 2017-01-26 02:07:05 +0000 | [diff] [blame] | 788 | if (TaskID > 0) |
Teresa Johnson | 0794359 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 789 | NewFilename += utostr(TaskID); |
Benjamin Kramer | 01823a6 | 2017-08-10 17:38:41 +0000 | [diff] [blame] | 790 | std::error_code EC = |
Zachary Turner | b99ddea | 2018-06-07 20:37:22 +0000 | [diff] [blame] | 791 | sys::fs::openFileForWrite(NewFilename, FD, sys::fs::CD_CreateAlways); |
Benjamin Kramer | 01823a6 | 2017-08-10 17:38:41 +0000 | [diff] [blame] | 792 | if (EC) |
| 793 | message(LDPL_FATAL, "Could not open file %s: %s", NewFilename.c_str(), |
| 794 | EC.message().c_str()); |
Teresa Johnson | 0794359 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 795 | } |
Benjamin Kramer | 01823a6 | 2017-08-10 17:38:41 +0000 | [diff] [blame] | 796 | return FD; |
Teresa Johnson | 0794359 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Peter Collingbourne | 7edf726 | 2018-04-06 21:14:33 +0000 | [diff] [blame] | 799 | static CodeGenOpt::Level getCGOptLevel() { |
| 800 | switch (options::OptLevel) { |
| 801 | case 0: |
| 802 | return CodeGenOpt::None; |
| 803 | case 1: |
| 804 | return CodeGenOpt::Less; |
| 805 | case 2: |
| 806 | return CodeGenOpt::Default; |
| 807 | case 3: |
| 808 | return CodeGenOpt::Aggressive; |
| 809 | } |
| 810 | llvm_unreachable("Invalid optimization level"); |
| 811 | } |
| 812 | |
Teresa Johnson | a486381 | 2016-05-17 14:45:30 +0000 | [diff] [blame] | 813 | /// Parse the thinlto_prefix_replace option into the \p OldPrefix and |
| 814 | /// \p NewPrefix strings, if it was specified. |
| 815 | static void getThinLTOOldAndNewPrefix(std::string &OldPrefix, |
| 816 | std::string &NewPrefix) { |
| 817 | StringRef PrefixReplace = options::thinlto_prefix_replace; |
Reid Kleckner | 4e35fa5 | 2016-05-17 18:43:22 +0000 | [diff] [blame] | 818 | assert(PrefixReplace.empty() || PrefixReplace.find(";") != StringRef::npos); |
Benjamin Kramer | e5d9cb5 | 2017-08-10 19:28:00 +0000 | [diff] [blame] | 819 | std::tie(OldPrefix, NewPrefix) = PrefixReplace.split(';'); |
Teresa Johnson | a486381 | 2016-05-17 14:45:30 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Vitaly Buka | 55523de | 2018-02-22 19:06:15 +0000 | [diff] [blame] | 822 | /// Creates instance of LTO. |
| 823 | /// OnIndexWrite is callback to let caller know when LTO writes index files. |
| 824 | /// LinkedObjectsFile is an output stream to write the list of object files for |
| 825 | /// the final ThinLTO linking. Can be nullptr. |
| 826 | static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite, |
| 827 | raw_fd_ostream *LinkedObjectsFile) { |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 828 | Config Conf; |
| 829 | ThinBackend Backend; |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 830 | |
| 831 | Conf.CPU = options::mcpu; |
| 832 | Conf.Options = InitTargetOptionsFromCodeGenFlags(); |
| 833 | |
| 834 | // Disable the new X86 relax relocations since gold might not support them. |
| 835 | // FIXME: Check the gold version or add a new option to enable them. |
| 836 | Conf.Options.RelaxELFRelocations = false; |
| 837 | |
Bill Wendling | 0a4671b | 2018-07-12 20:35:58 +0000 | [diff] [blame] | 838 | // Toggle function/data sections. |
Nick Desaulniers | 0fca1c8 | 2018-12-05 17:46:24 +0000 | [diff] [blame] | 839 | if (FunctionSections.getNumOccurrences() == 0) |
| 840 | Conf.Options.FunctionSections = SplitSections; |
| 841 | if (DataSections.getNumOccurrences() == 0) |
| 842 | Conf.Options.DataSections = SplitSections; |
Davide Italiano | 67d8465 | 2017-07-25 23:32:50 +0000 | [diff] [blame] | 843 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 844 | Conf.MAttrs = MAttrs; |
Evgeniy Stepanov | 49f70cc | 2017-05-22 21:11:35 +0000 | [diff] [blame] | 845 | Conf.RelocModel = RelocationModel; |
Bill Wendling | 6c00876 | 2018-06-13 05:53:59 +0000 | [diff] [blame] | 846 | Conf.CodeModel = getCodeModel(); |
Peter Collingbourne | 7edf726 | 2018-04-06 21:14:33 +0000 | [diff] [blame] | 847 | Conf.CGOptLevel = getCGOptLevel(); |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 848 | Conf.DisableVerify = options::DisableVerify; |
| 849 | Conf.OptLevel = options::OptLevel; |
Teresa Johnson | 468ae9f | 2016-09-23 20:35:19 +0000 | [diff] [blame] | 850 | if (options::Parallelism) |
| 851 | Backend = createInProcessThinBackend(options::Parallelism); |
Teresa Johnson | 6f0ab85 | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 852 | if (options::thinlto_index_only) { |
Teresa Johnson | 6f0ab85 | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 853 | std::string OldPrefix, NewPrefix; |
| 854 | getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix); |
Vitaly Buka | 55523de | 2018-02-22 19:06:15 +0000 | [diff] [blame] | 855 | Backend = createWriteIndexesThinBackend(OldPrefix, NewPrefix, |
| 856 | options::thinlto_emit_imports_files, |
| 857 | LinkedObjectsFile, OnIndexWrite); |
Teresa Johnson | 27af205 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 860 | Conf.OverrideTriple = options::triple; |
| 861 | Conf.DefaultTriple = sys::getDefaultTargetTriple(); |
| 862 | |
| 863 | Conf.DiagHandler = diagnosticHandler; |
| 864 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 865 | switch (options::TheOutputType) { |
| 866 | case options::OT_NORMAL: |
| 867 | break; |
| 868 | |
| 869 | case options::OT_DISABLE: |
Mehdi Amini | b41bec0 | 2016-08-22 16:41:58 +0000 | [diff] [blame] | 870 | Conf.PreOptModuleHook = [](size_t Task, const Module &M) { return false; }; |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 871 | break; |
| 872 | |
| 873 | case options::OT_BC_ONLY: |
Mehdi Amini | b41bec0 | 2016-08-22 16:41:58 +0000 | [diff] [blame] | 874 | Conf.PostInternalizeModuleHook = [](size_t Task, const Module &M) { |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 875 | std::error_code EC; |
| 876 | raw_fd_ostream OS(output_name, EC, sys::fs::OpenFlags::F_None); |
| 877 | if (EC) |
| 878 | message(LDPL_FATAL, "Failed to write the output file."); |
Rafael Espindola | 06d6207 | 2018-02-14 19:11:32 +0000 | [diff] [blame] | 879 | WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ false); |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 880 | return false; |
| 881 | }; |
| 882 | break; |
| 883 | |
| 884 | case options::OT_SAVE_TEMPS: |
Mehdi Amini | a53e50c | 2016-08-18 00:12:33 +0000 | [diff] [blame] | 885 | check(Conf.addSaveTemps(output_name + ".", |
| 886 | /* UseInputModulePath */ true)); |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 887 | break; |
Teresa Johnson | 02e82c3 | 2019-01-02 23:48:00 +0000 | [diff] [blame] | 888 | case options::OT_ASM_ONLY: |
| 889 | Conf.CGFileType = TargetMachine::CGFT_AssemblyFile; |
| 890 | break; |
Teresa Johnson | 6f0ab85 | 2016-08-11 13:03:56 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Dehao Chen | 7e43600 | 2016-12-16 16:48:46 +0000 | [diff] [blame] | 893 | if (!options::sample_profile.empty()) |
| 894 | Conf.SampleProfile = options::sample_profile; |
| 895 | |
Yunlian Jiang | edd00e4 | 2018-04-13 05:03:28 +0000 | [diff] [blame] | 896 | Conf.DwoDir = options::dwo_dir; |
| 897 | |
Teresa Johnson | 48ba666 | 2018-04-18 13:25:23 +0000 | [diff] [blame] | 898 | // Set up optimization remarks handling. |
| 899 | Conf.RemarksFilename = options::OptRemarksFilename; |
| 900 | Conf.RemarksWithHotness = options::OptRemarksWithHotness; |
| 901 | |
Sean Fertile | abd8a85 | 2017-10-05 01:48:42 +0000 | [diff] [blame] | 902 | // Use new pass manager if set in driver |
| 903 | Conf.UseNewPM = options::new_pass_manager; |
Teresa Johnson | 65834ee | 2018-04-05 03:16:57 +0000 | [diff] [blame] | 904 | // Debug new pass manager if requested |
| 905 | Conf.DebugPassManager = options::debug_pass_manager; |
Sean Fertile | abd8a85 | 2017-10-05 01:48:42 +0000 | [diff] [blame] | 906 | |
Florian Hahn | a2fbfc9 | 2018-04-20 10:18:36 +0000 | [diff] [blame] | 907 | Conf.StatsFile = options::stats_file; |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 908 | return llvm::make_unique<LTO>(std::move(Conf), Backend, |
Teresa Johnson | 468ae9f | 2016-09-23 20:35:19 +0000 | [diff] [blame] | 909 | options::ParallelCodeGenParallelismLevel); |
Teresa Johnson | 27af205 | 2016-05-10 13:48:23 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Teresa Johnson | b81a1e9 | 2016-09-21 19:12:05 +0000 | [diff] [blame] | 912 | // Write empty files that may be expected by a distributed build |
| 913 | // system when invoked with thinlto_index_only. This is invoked when |
| 914 | // the linker has decided not to include the given module in the |
| 915 | // final link. Frequently the distributed build system will want to |
| 916 | // confirm that all expected outputs are created based on all of the |
| 917 | // modules provided to the linker. |
Vitaly Buka | 2749eb2 | 2018-02-16 23:38:22 +0000 | [diff] [blame] | 918 | // If SkipModule is true then .thinlto.bc should contain just |
| 919 | // SkipModuleByDistributedBackend flag which requests distributed backend |
| 920 | // to skip the compilation of the corresponding module and produce an empty |
| 921 | // object file. |
Vitaly Buka | 23f8193 | 2018-01-30 21:19:26 +0000 | [diff] [blame] | 922 | static void writeEmptyDistributedBuildOutputs(const std::string &ModulePath, |
| 923 | const std::string &OldPrefix, |
Vitaly Buka | 2749eb2 | 2018-02-16 23:38:22 +0000 | [diff] [blame] | 924 | const std::string &NewPrefix, |
| 925 | bool SkipModule) { |
Teresa Johnson | b81a1e9 | 2016-09-21 19:12:05 +0000 | [diff] [blame] | 926 | std::string NewModulePath = |
| 927 | getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix); |
| 928 | std::error_code EC; |
| 929 | { |
| 930 | raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC, |
| 931 | sys::fs::OpenFlags::F_None); |
| 932 | if (EC) |
| 933 | message(LDPL_FATAL, "Failed to write '%s': %s", |
| 934 | (NewModulePath + ".thinlto.bc").c_str(), EC.message().c_str()); |
Vitaly Buka | 2749eb2 | 2018-02-16 23:38:22 +0000 | [diff] [blame] | 935 | |
| 936 | if (SkipModule) { |
Teresa Johnson | e07c260 | 2018-06-06 22:22:01 +0000 | [diff] [blame] | 937 | ModuleSummaryIndex Index(/*HaveGVs*/ false); |
Vitaly Buka | 2749eb2 | 2018-02-16 23:38:22 +0000 | [diff] [blame] | 938 | Index.setSkipModuleByDistributedBackend(); |
| 939 | WriteIndexToFile(Index, OS, nullptr); |
| 940 | } |
Teresa Johnson | b81a1e9 | 2016-09-21 19:12:05 +0000 | [diff] [blame] | 941 | } |
| 942 | if (options::thinlto_emit_imports_files) { |
| 943 | raw_fd_ostream OS(NewModulePath + ".imports", EC, |
| 944 | sys::fs::OpenFlags::F_None); |
| 945 | if (EC) |
| 946 | message(LDPL_FATAL, "Failed to write '%s': %s", |
| 947 | (NewModulePath + ".imports").c_str(), EC.message().c_str()); |
| 948 | } |
| 949 | } |
| 950 | |
Vitaly Buka | 55523de | 2018-02-22 19:06:15 +0000 | [diff] [blame] | 951 | // Creates and returns output stream with a list of object files for final |
| 952 | // linking of distributed ThinLTO. |
| 953 | static std::unique_ptr<raw_fd_ostream> CreateLinkedObjectsFile() { |
| 954 | if (options::thinlto_linked_objects_file.empty()) |
| 955 | return nullptr; |
| 956 | assert(options::thinlto_index_only); |
| 957 | std::error_code EC; |
| 958 | auto LinkedObjectsFile = llvm::make_unique<raw_fd_ostream>( |
| 959 | options::thinlto_linked_objects_file, EC, sys::fs::OpenFlags::F_None); |
| 960 | if (EC) |
| 961 | message(LDPL_FATAL, "Failed to create '%s': %s", |
| 962 | options::thinlto_linked_objects_file.c_str(), EC.message().c_str()); |
| 963 | return LinkedObjectsFile; |
| 964 | } |
| 965 | |
Vitaly Buka | f662ded | 2018-02-22 19:06:05 +0000 | [diff] [blame] | 966 | /// Runs LTO and return a list of pairs <FileName, IsTemporary>. |
| 967 | static std::vector<std::pair<SmallString<128>, bool>> runLTO() { |
Teresa Johnson | f4b6540 | 2016-08-20 01:24:07 +0000 | [diff] [blame] | 968 | // Map to own RAII objects that manage the file opening and releasing |
| 969 | // interfaces with gold. This is needed only for ThinLTO mode, since |
| 970 | // unlike regular LTO, where addModule will result in the opened file |
| 971 | // being merged into a new combined module, we need to keep these files open |
| 972 | // through Lto->run(). |
| 973 | DenseMap<void *, std::unique_ptr<PluginInputFile>> HandleToInputFile; |
| 974 | |
Vitaly Buka | 23f8193 | 2018-01-30 21:19:26 +0000 | [diff] [blame] | 975 | // Owns string objects and tells if index file was already created. |
| 976 | StringMap<bool> ObjectToIndexFileState; |
| 977 | |
Vitaly Buka | 55523de | 2018-02-22 19:06:15 +0000 | [diff] [blame] | 978 | std::unique_ptr<raw_fd_ostream> LinkedObjects = CreateLinkedObjectsFile(); |
| 979 | std::unique_ptr<LTO> Lto = createLTO( |
| 980 | [&ObjectToIndexFileState](const std::string &Identifier) { |
Vitaly Buka | 23f8193 | 2018-01-30 21:19:26 +0000 | [diff] [blame] | 981 | ObjectToIndexFileState[Identifier] = true; |
Vitaly Buka | 55523de | 2018-02-22 19:06:15 +0000 | [diff] [blame] | 982 | }, |
| 983 | LinkedObjects.get()); |
Teresa Johnson | b97baa5 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 984 | |
Teresa Johnson | b81a1e9 | 2016-09-21 19:12:05 +0000 | [diff] [blame] | 985 | std::string OldPrefix, NewPrefix; |
| 986 | if (options::thinlto_index_only) |
| 987 | getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix); |
| 988 | |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 989 | std::string OldSuffix, NewSuffix; |
| 990 | getThinLTOOldAndNewSuffix(OldSuffix, NewSuffix); |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 991 | |
Rafael Espindola | 562fd7a | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 992 | for (claimed_file &F : Modules) { |
Teresa Johnson | f4b6540 | 2016-08-20 01:24:07 +0000 | [diff] [blame] | 993 | if (options::thinlto && !HandleToInputFile.count(F.leader_handle)) |
| 994 | HandleToInputFile.insert(std::make_pair( |
| 995 | F.leader_handle, llvm::make_unique<PluginInputFile>(F.handle))); |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 996 | // In case we are thin linking with a minimized bitcode file, ensure |
| 997 | // the module paths encoded in the index reflect where the backends |
| 998 | // will locate the full bitcode files for compiling/importing. |
| 999 | std::string Identifier = |
| 1000 | getThinLTOObjectFileName(F.name, OldSuffix, NewSuffix); |
Vitaly Buka | 23f8193 | 2018-01-30 21:19:26 +0000 | [diff] [blame] | 1001 | auto ObjFilename = ObjectToIndexFileState.insert({Identifier, false}); |
Teresa Johnson | 08d0e94 | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 1002 | assert(ObjFilename.second); |
Vitaly Buka | 23f8193 | 2018-01-30 21:19:26 +0000 | [diff] [blame] | 1003 | if (const void *View = getSymbolsAndView(F)) |
| 1004 | addModule(*Lto, F, View, ObjFilename.first->first()); |
Vitaly Buka | 2749eb2 | 2018-02-16 23:38:22 +0000 | [diff] [blame] | 1005 | else if (options::thinlto_index_only) { |
| 1006 | ObjFilename.first->second = true; |
| 1007 | writeEmptyDistributedBuildOutputs(Identifier, OldPrefix, NewPrefix, |
| 1008 | /* SkipModule */ true); |
| 1009 | } |
Rafael Espindola | 38a979b | 2010-06-14 21:20:52 +0000 | [diff] [blame] | 1010 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 1011 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 1012 | SmallString<128> Filename; |
Mehdi Amini | bba5e18 | 2016-08-17 06:23:09 +0000 | [diff] [blame] | 1013 | // Note that getOutputFileName will append a unique ID for each task |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 1014 | if (!options::obj_path.empty()) |
| 1015 | Filename = options::obj_path; |
| 1016 | else if (options::TheOutputType == options::OT_SAVE_TEMPS) |
| 1017 | Filename = output_name + ".o"; |
Teresa Johnson | 02e82c3 | 2019-01-02 23:48:00 +0000 | [diff] [blame] | 1018 | else if (options::TheOutputType == options::OT_ASM_ONLY) |
| 1019 | Filename = output_name; |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 1020 | bool SaveTemps = !Filename.empty(); |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 1021 | |
Peter Collingbourne | 289792d | 2017-01-26 02:07:05 +0000 | [diff] [blame] | 1022 | size_t MaxTasks = Lto->getMaxTasks(); |
Vitaly Buka | f662ded | 2018-02-22 19:06:05 +0000 | [diff] [blame] | 1023 | std::vector<std::pair<SmallString<128>, bool>> Files(MaxTasks); |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 1024 | |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 1025 | auto AddStream = |
| 1026 | [&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> { |
Vitaly Buka | f662ded | 2018-02-22 19:06:05 +0000 | [diff] [blame] | 1027 | Files[Task].second = !SaveTemps; |
Vitaly Buka | 2749eb2 | 2018-02-16 23:38:22 +0000 | [diff] [blame] | 1028 | int FD = getOutputFileName(Filename, /* TempOutFile */ !SaveTemps, |
Vitaly Buka | f662ded | 2018-02-22 19:06:05 +0000 | [diff] [blame] | 1029 | Files[Task].first, Task); |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 1030 | return llvm::make_unique<lto::NativeObjectStream>( |
| 1031 | llvm::make_unique<llvm::raw_fd_ostream>(FD, true)); |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 1032 | }; |
| 1033 | |
Teresa Johnson | 761281e | 2018-02-20 20:21:53 +0000 | [diff] [blame] | 1034 | auto AddBuffer = [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) { |
Teresa Johnson | f4aea13 | 2018-02-20 19:51:30 +0000 | [diff] [blame] | 1035 | *AddStream(Task)->OS << MB->getBuffer(); |
Peter Collingbourne | ec179d7 | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 1036 | }; |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 1037 | |
| 1038 | NativeObjectCache Cache; |
| 1039 | if (!options::cache_dir.empty()) |
Peter Collingbourne | ec179d7 | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 1040 | Cache = check(localCache(options::cache_dir, AddBuffer)); |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 1041 | |
| 1042 | check(Lto->run(AddStream, Cache)); |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 1043 | |
Vitaly Buka | 23f8193 | 2018-01-30 21:19:26 +0000 | [diff] [blame] | 1044 | // Write empty output files that may be expected by the distributed build |
| 1045 | // system. |
| 1046 | if (options::thinlto_index_only) |
| 1047 | for (auto &Identifier : ObjectToIndexFileState) |
| 1048 | if (!Identifier.getValue()) |
| 1049 | writeEmptyDistributedBuildOutputs(Identifier.getKey(), OldPrefix, |
Vitaly Buka | 2749eb2 | 2018-02-16 23:38:22 +0000 | [diff] [blame] | 1050 | NewPrefix, /* SkipModule */ false); |
Vitaly Buka | 23f8193 | 2018-01-30 21:19:26 +0000 | [diff] [blame] | 1051 | |
Vitaly Buka | f662ded | 2018-02-22 19:06:05 +0000 | [diff] [blame] | 1052 | return Files; |
| 1053 | } |
| 1054 | |
| 1055 | /// gold informs us that all symbols have been read. At this point, we use |
| 1056 | /// get_symbols to see if any of our definitions have been overridden by a |
| 1057 | /// native object file. Then, perform optimization and codegen. |
| 1058 | static ld_plugin_status allSymbolsReadHook() { |
| 1059 | if (Modules.empty()) |
| 1060 | return LDPS_OK; |
| 1061 | |
| 1062 | if (unsigned NumOpts = options::extra.size()) |
| 1063 | cl::ParseCommandLineOptions(NumOpts, &options::extra[0]); |
| 1064 | |
| 1065 | std::vector<std::pair<SmallString<128>, bool>> Files = runLTO(); |
| 1066 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 1067 | if (options::TheOutputType == options::OT_DISABLE || |
Teresa Johnson | 02e82c3 | 2019-01-02 23:48:00 +0000 | [diff] [blame] | 1068 | options::TheOutputType == options::OT_BC_ONLY || |
| 1069 | options::TheOutputType == options::OT_ASM_ONLY) |
Rafael Espindola | f1ca1d4 | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 1070 | return LDPS_OK; |
| 1071 | |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 1072 | if (options::thinlto_index_only) { |
Teresa Johnson | 3e0bad6 | 2018-04-19 16:55:13 +0000 | [diff] [blame] | 1073 | llvm_shutdown(); |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 1074 | cleanup_hook(); |
| 1075 | exit(0); |
Rafael Espindola | 62bacd6 | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 1076 | } |
Rafael Espindola | 648a2e6 | 2013-10-16 12:47:04 +0000 | [diff] [blame] | 1077 | |
Vitaly Buka | f662ded | 2018-02-22 19:06:05 +0000 | [diff] [blame] | 1078 | for (const auto &F : Files) |
| 1079 | if (!F.first.empty()) |
| 1080 | recordFile(F.first.str(), F.second); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 1081 | |
Rafael Espindola | 11f403c | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 1082 | if (!options::extra_library_path.empty() && |
Rafael Espindola | 3fd7e37 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 1083 | set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) |
| 1084 | message(LDPL_FATAL, "Unable to set the extra library path."); |
Shuxin Yang | cfaa636 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 1085 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 1086 | return LDPS_OK; |
| 1087 | } |
| 1088 | |
Rafael Espindola | b2d2f283 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 1089 | static ld_plugin_status all_symbols_read_hook(void) { |
Teresa Johnson | 2d320e5 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 1090 | ld_plugin_status Ret = allSymbolsReadHook(); |
Rafael Espindola | 2fbbd7a | 2014-11-25 20:52:49 +0000 | [diff] [blame] | 1091 | llvm_shutdown(); |
| 1092 | |
Rafael Espindola | f1ca1d4 | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 1093 | if (options::TheOutputType == options::OT_BC_ONLY || |
Teresa Johnson | 02e82c3 | 2019-01-02 23:48:00 +0000 | [diff] [blame] | 1094 | options::TheOutputType == options::OT_ASM_ONLY || |
Michael Kuperstein | 17a113d | 2015-02-12 18:21:50 +0000 | [diff] [blame] | 1095 | options::TheOutputType == options::OT_DISABLE) { |
Davide Italiano | 443693f | 2016-03-20 20:12:33 +0000 | [diff] [blame] | 1096 | if (options::TheOutputType == options::OT_DISABLE) { |
Michael Kuperstein | 17a113d | 2015-02-12 18:21:50 +0000 | [diff] [blame] | 1097 | // Remove the output file here since ld.bfd creates the output file |
| 1098 | // early. |
Davide Italiano | 443693f | 2016-03-20 20:12:33 +0000 | [diff] [blame] | 1099 | std::error_code EC = sys::fs::remove(output_name); |
| 1100 | if (EC) |
| 1101 | message(LDPL_ERROR, "Failed to delete '%s': %s", output_name.c_str(), |
| 1102 | EC.message().c_str()); |
| 1103 | } |
Rafael Espindola | b2d2f283 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 1104 | exit(0); |
Michael Kuperstein | 17a113d | 2015-02-12 18:21:50 +0000 | [diff] [blame] | 1105 | } |
Rafael Espindola | b2d2f283 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 1106 | |
| 1107 | return Ret; |
| 1108 | } |
| 1109 | |
Dan Gohman | f692003 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 1110 | static ld_plugin_status cleanup_hook(void) { |
Rafael Espindola | 562fd7a | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 1111 | for (std::string &Name : Cleanup) { |
| 1112 | std::error_code EC = sys::fs::remove(Name); |
Rafael Espindola | b339411 | 2013-06-17 18:38:18 +0000 | [diff] [blame] | 1113 | if (EC) |
Rafael Espindola | 562fd7a | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 1114 | message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(), |
Rafael Espindola | 601bb09 | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 1115 | EC.message().c_str()); |
Rafael Espindola | b339411 | 2013-06-17 18:38:18 +0000 | [diff] [blame] | 1116 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 1117 | |
Yi Kong | 74ae606 | 2017-09-18 23:24:55 +0000 | [diff] [blame] | 1118 | // Prune cache |
Teresa Johnson | 327fc51 | 2018-02-22 20:57:05 +0000 | [diff] [blame] | 1119 | if (!options::cache_dir.empty()) { |
Yi Kong | 74ae606 | 2017-09-18 23:24:55 +0000 | [diff] [blame] | 1120 | CachePruningPolicy policy = check(parseCachePruningPolicy(options::cache_policy)); |
| 1121 | pruneCache(options::cache_dir, policy); |
| 1122 | } |
| 1123 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 1124 | return LDPS_OK; |
| 1125 | } |