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