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