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 | |
Dylan Noblesmith | 9e5b178 | 2011-12-22 23:04:07 +0000 | [diff] [blame] | 15 | #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseSet.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringSet.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/TargetLibraryInfo.h" |
NAKAMURA Takumi | b0a5283 | 2015-02-02 05:47:30 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/TargetTransformInfo.h" |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/ReaderWriter.h" |
| 21 | #include "llvm/CodeGen/Analysis.h" |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/CommandFlags.h" |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/ParallelCG.h" |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 24 | #include "llvm/IR/AutoUpgrade.h" |
Rafael Espindola | 890db27 | 2014-09-09 20:08:22 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Constants.h" |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DiagnosticInfo.h" |
| 27 | #include "llvm/IR/DiagnosticPrinter.h" |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 28 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 29 | #include "llvm/IR/LegacyPassManager.h" |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Module.h" |
| 31 | #include "llvm/IR/Verifier.h" |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 32 | #include "llvm/Linker/IRMover.h" |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 33 | #include "llvm/MC/SubtargetFeature.h" |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 34 | #include "llvm/Object/FunctionIndexObjectFile.h" |
Teresa Johnson | b13dbd6 | 2015-12-09 19:45:55 +0000 | [diff] [blame] | 35 | #include "llvm/Object/IRObjectFile.h" |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Host.h" |
Rafael Espindola | 947bdb6 | 2014-11-25 20:52:49 +0000 | [diff] [blame] | 37 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MemoryBuffer.h" |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 39 | #include "llvm/Support/TargetRegistry.h" |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 40 | #include "llvm/Support/TargetSelect.h" |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 41 | #include "llvm/Support/ThreadPool.h" |
Teresa Johnson | b13dbd6 | 2015-12-09 19:45:55 +0000 | [diff] [blame] | 42 | #include "llvm/Support/raw_ostream.h" |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 43 | #include "llvm/Support/thread.h" |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 44 | #include "llvm/Transforms/IPO.h" |
| 45 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 46 | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 47 | #include "llvm/Transforms/Utils/GlobalStatus.h" |
| 48 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 49 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 50 | #include <list> |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 51 | #include <plugin-api.h> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 52 | #include <system_error> |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 53 | #include <vector> |
| 54 | |
Sylvestre Ledru | 5399979 | 2014-02-11 17:30:18 +0000 | [diff] [blame] | 55 | // FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and |
| 56 | // Precise and Debian Wheezy (binutils 2.23 is required) |
Evgeniy Stepanov | 330c5a6 | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 57 | #define LDPO_PIE 3 |
| 58 | |
| 59 | #define LDPT_GET_SYMBOLS_V3 28 |
Sylvestre Ledru | 5399979 | 2014-02-11 17:30:18 +0000 | [diff] [blame] | 60 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 61 | using namespace llvm; |
| 62 | |
Teresa Johnson | cb15b73 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 63 | static ld_plugin_status discard_message(int level, const char *format, ...) { |
| 64 | // Die loudly. Recent versions of Gold pass ld_plugin_message as the first |
| 65 | // callback in the transfer vector. This should never be called. |
| 66 | abort(); |
| 67 | } |
| 68 | |
| 69 | static ld_plugin_release_input_file release_input_file = nullptr; |
| 70 | static ld_plugin_get_input_file get_input_file = nullptr; |
| 71 | static ld_plugin_message message = discard_message; |
| 72 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 73 | namespace { |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 74 | struct claimed_file { |
| 75 | void *handle; |
| 76 | std::vector<ld_plugin_symbol> syms; |
| 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 { |
| 104 | bool IsLinkonceOdr = true; |
| 105 | bool UnnamedAddr = true; |
| 106 | GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; |
| 107 | bool CommonInternal = false; |
| 108 | bool UseCommon = false; |
| 109 | unsigned CommonSize = 0; |
| 110 | unsigned CommonAlign = 0; |
| 111 | claimed_file *CommonFile = nullptr; |
| 112 | }; |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 113 | |
| 114 | /// Class to own information used by a task or during its cleanup for a |
| 115 | /// ThinLTO backend instantiation. |
| 116 | class ThinLTOTaskInfo { |
| 117 | /// The input file holding the module bitcode read by the ThinLTO task. |
| 118 | PluginInputFile InputFile; |
| 119 | |
| 120 | /// The output stream the task will codegen into. |
| 121 | std::unique_ptr<raw_fd_ostream> OS; |
| 122 | |
| 123 | /// The file name corresponding to the output stream, used during cleanup. |
| 124 | std::string Filename; |
| 125 | |
| 126 | /// Flag indicating whether the output file is a temp file that must be |
| 127 | /// added to the cleanup list during cleanup. |
| 128 | bool TempOutFile; |
| 129 | |
| 130 | public: |
| 131 | ThinLTOTaskInfo(PluginInputFile InputFile, std::unique_ptr<raw_fd_ostream> OS, |
| 132 | std::string Filename, bool TempOutFile) |
| 133 | : InputFile(std::move(InputFile)), OS(std::move(OS)), Filename(Filename), |
| 134 | TempOutFile(TempOutFile) {} |
| 135 | |
| 136 | /// Performs task related cleanup activities that must be done |
| 137 | /// single-threaded (i.e. call backs to gold). |
| 138 | void cleanup(); |
| 139 | }; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 140 | } |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 141 | |
Rafael Espindola | 176e664 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 142 | static ld_plugin_add_symbols add_symbols = nullptr; |
| 143 | static ld_plugin_get_symbols get_symbols = nullptr; |
| 144 | static ld_plugin_add_input_file add_input_file = nullptr; |
| 145 | static ld_plugin_set_extra_library_path set_extra_library_path = nullptr; |
| 146 | static ld_plugin_get_view get_view = nullptr; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 147 | static Reloc::Model RelocationModel = Reloc::Default; |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 148 | static std::string output_name = ""; |
| 149 | static std::list<claimed_file> Modules; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 150 | static StringMap<ResolutionInfo> ResInfo; |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 151 | static std::vector<std::string> Cleanup; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 152 | static llvm::TargetOptions TargetOpts; |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 153 | static std::string DefaultTriple = sys::getDefaultTargetTriple(); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 154 | |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 155 | namespace options { |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 156 | enum OutputType { |
| 157 | OT_NORMAL, |
| 158 | OT_DISABLE, |
| 159 | OT_BC_ONLY, |
| 160 | OT_SAVE_TEMPS |
| 161 | }; |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 162 | static bool generate_api_file = false; |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 163 | static OutputType TheOutputType = OT_NORMAL; |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 164 | static unsigned OptLevel = 2; |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 165 | // Default parallelism of 0 used to indicate that user did not specify. |
| 166 | // Actual parallelism default value depends on implementation. |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 167 | // Currently, code generation defaults to no parallelism, whereas |
| 168 | // ThinLTO uses the hardware_concurrency as the default. |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 169 | static unsigned Parallelism = 0; |
Teresa Johnson | 8c8fe5a | 2015-09-16 18:06:45 +0000 | [diff] [blame] | 170 | #ifdef NDEBUG |
| 171 | static bool DisableVerify = true; |
| 172 | #else |
| 173 | static bool DisableVerify = false; |
| 174 | #endif |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 175 | static std::string obj_path; |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 176 | static std::string extra_library_path; |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 177 | static std::string triple; |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 178 | static std::string mcpu; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 179 | // When the thinlto plugin option is specified, only read the function |
| 180 | // the information from intermediate files and write a combined |
| 181 | // global index for the ThinLTO backends. |
| 182 | static bool thinlto = false; |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 183 | // If false, all ThinLTO backend compilations through code gen are performed |
| 184 | // using multiple threads in the gold-plugin, before handing control back to |
| 185 | // gold. If true, exit after creating the combined index, the assuming is |
| 186 | // that the build system will launch the backend processes. |
| 187 | static bool thinlto_index_only = false; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 188 | // Additional options to pass into the code generator. |
Nick Lewycky | 0ac5e22 | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 189 | // Note: This array will contain all plugin options which are not claimed |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 190 | // as plugin exclusive to pass to the code generator. |
Nick Lewycky | 0ac5e22 | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 191 | // For example, "generate-api-file" and "as"options are for the plugin |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 192 | // use only and will not be passed. |
Rafael Espindola | 125b924 | 2014-07-29 19:17:44 +0000 | [diff] [blame] | 193 | static std::vector<const char *> extra; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 194 | |
Nick Lewycky | 7282dd7 | 2015-08-05 21:16:02 +0000 | [diff] [blame] | 195 | static void process_plugin_option(const char *opt_) |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 196 | { |
Rafael Espindola | 176e664 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 197 | if (opt_ == nullptr) |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 198 | return; |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 199 | llvm::StringRef opt = opt_; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 200 | |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 201 | if (opt == "generate-api-file") { |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 202 | generate_api_file = true; |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 203 | } else if (opt.startswith("mcpu=")) { |
| 204 | mcpu = opt.substr(strlen("mcpu=")); |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 205 | } else if (opt.startswith("extra-library-path=")) { |
| 206 | extra_library_path = opt.substr(strlen("extra_library_path=")); |
Rafael Espindola | 148c328 | 2010-08-10 16:32:15 +0000 | [diff] [blame] | 207 | } else if (opt.startswith("mtriple=")) { |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 208 | triple = opt.substr(strlen("mtriple=")); |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 209 | } else if (opt.startswith("obj-path=")) { |
| 210 | obj_path = opt.substr(strlen("obj-path=")); |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 211 | } else if (opt == "emit-llvm") { |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 212 | TheOutputType = OT_BC_ONLY; |
Rafael Espindola | 4a3b6cf | 2014-10-29 23:54:45 +0000 | [diff] [blame] | 213 | } else if (opt == "save-temps") { |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 214 | TheOutputType = OT_SAVE_TEMPS; |
| 215 | } else if (opt == "disable-output") { |
| 216 | TheOutputType = OT_DISABLE; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 217 | } else if (opt == "thinlto") { |
| 218 | thinlto = true; |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 219 | } else if (opt == "thinlto-index-only") { |
| 220 | thinlto_index_only = true; |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 221 | } else if (opt.size() == 2 && opt[0] == 'O') { |
| 222 | if (opt[1] < '0' || opt[1] > '3') |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 223 | message(LDPL_FATAL, "Optimization level must be between 0 and 3"); |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 224 | OptLevel = opt[1] - '0'; |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 225 | } else if (opt.startswith("jobs=")) { |
| 226 | if (StringRef(opt_ + 5).getAsInteger(10, Parallelism)) |
| 227 | message(LDPL_FATAL, "Invalid parallelism level: %s", opt_ + 5); |
Teresa Johnson | 8c8fe5a | 2015-09-16 18:06:45 +0000 | [diff] [blame] | 228 | } else if (opt == "disable-verify") { |
| 229 | DisableVerify = true; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 230 | } else { |
| 231 | // Save this option to pass to the code generator. |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 232 | // ParseCommandLineOptions() expects argv[0] to be program name. Lazily |
| 233 | // add that. |
| 234 | if (extra.empty()) |
| 235 | extra.push_back("LLVMgold"); |
| 236 | |
Rafael Espindola | 125b924 | 2014-07-29 19:17:44 +0000 | [diff] [blame] | 237 | extra.push_back(opt_); |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 242 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 243 | int *claimed); |
| 244 | static ld_plugin_status all_symbols_read_hook(void); |
| 245 | static ld_plugin_status cleanup_hook(void); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 246 | |
| 247 | extern "C" ld_plugin_status onload(ld_plugin_tv *tv); |
| 248 | ld_plugin_status onload(ld_plugin_tv *tv) { |
Peter Collingbourne | 1505c0a | 2014-07-03 23:28:03 +0000 | [diff] [blame] | 249 | InitializeAllTargetInfos(); |
| 250 | InitializeAllTargets(); |
| 251 | InitializeAllTargetMCs(); |
| 252 | InitializeAllAsmParsers(); |
| 253 | InitializeAllAsmPrinters(); |
| 254 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 255 | // We're given a pointer to the first transfer vector. We read through them |
| 256 | // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values |
| 257 | // contain pointers to functions that we need to call to register our own |
| 258 | // hooks. The others are addresses of functions we can use to call into gold |
| 259 | // for services. |
| 260 | |
| 261 | bool registeredClaimFile = false; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 262 | bool RegisteredAllSymbolsRead = false; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 263 | |
| 264 | for (; tv->tv_tag != LDPT_NULL; ++tv) { |
Evgeniy Stepanov | 330c5a6 | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 265 | // Cast tv_tag to int to allow values not in "enum ld_plugin_tag", like, for |
| 266 | // example, LDPT_GET_SYMBOLS_V3 when building against an older plugin-api.h |
| 267 | // header. |
| 268 | switch (static_cast<int>(tv->tv_tag)) { |
| 269 | case LDPT_OUTPUT_NAME: |
| 270 | output_name = tv->tv_u.tv_string; |
| 271 | break; |
| 272 | case LDPT_LINKER_OUTPUT: |
| 273 | switch (tv->tv_u.tv_val) { |
| 274 | case LDPO_REL: // .o |
| 275 | case LDPO_DYN: // .so |
| 276 | case LDPO_PIE: // position independent executable |
| 277 | RelocationModel = Reloc::PIC_; |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 278 | break; |
Evgeniy Stepanov | 330c5a6 | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 279 | case LDPO_EXEC: // .exe |
| 280 | RelocationModel = Reloc::Static; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 281 | break; |
| 282 | default: |
Evgeniy Stepanov | 330c5a6 | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 283 | message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val); |
| 284 | return LDPS_ERR; |
| 285 | } |
| 286 | break; |
| 287 | case LDPT_OPTION: |
| 288 | options::process_plugin_option(tv->tv_u.tv_string); |
| 289 | break; |
| 290 | case LDPT_REGISTER_CLAIM_FILE_HOOK: { |
| 291 | ld_plugin_register_claim_file callback; |
| 292 | callback = tv->tv_u.tv_register_claim_file; |
| 293 | |
| 294 | if (callback(claim_file_hook) != LDPS_OK) |
| 295 | return LDPS_ERR; |
| 296 | |
| 297 | registeredClaimFile = true; |
| 298 | } break; |
| 299 | case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: { |
| 300 | ld_plugin_register_all_symbols_read callback; |
| 301 | callback = tv->tv_u.tv_register_all_symbols_read; |
| 302 | |
| 303 | if (callback(all_symbols_read_hook) != LDPS_OK) |
| 304 | return LDPS_ERR; |
| 305 | |
| 306 | RegisteredAllSymbolsRead = true; |
| 307 | } break; |
| 308 | case LDPT_REGISTER_CLEANUP_HOOK: { |
| 309 | ld_plugin_register_cleanup callback; |
| 310 | callback = tv->tv_u.tv_register_cleanup; |
| 311 | |
| 312 | if (callback(cleanup_hook) != LDPS_OK) |
| 313 | return LDPS_ERR; |
| 314 | } break; |
| 315 | case LDPT_GET_INPUT_FILE: |
| 316 | get_input_file = tv->tv_u.tv_get_input_file; |
| 317 | break; |
| 318 | case LDPT_RELEASE_INPUT_FILE: |
| 319 | release_input_file = tv->tv_u.tv_release_input_file; |
| 320 | break; |
| 321 | case LDPT_ADD_SYMBOLS: |
| 322 | add_symbols = tv->tv_u.tv_add_symbols; |
| 323 | break; |
| 324 | case LDPT_GET_SYMBOLS_V2: |
| 325 | // Do not override get_symbols_v3 with get_symbols_v2. |
| 326 | if (!get_symbols) |
| 327 | get_symbols = tv->tv_u.tv_get_symbols; |
| 328 | break; |
| 329 | case LDPT_GET_SYMBOLS_V3: |
| 330 | get_symbols = tv->tv_u.tv_get_symbols; |
| 331 | break; |
| 332 | case LDPT_ADD_INPUT_FILE: |
| 333 | add_input_file = tv->tv_u.tv_add_input_file; |
| 334 | break; |
| 335 | case LDPT_SET_EXTRA_LIBRARY_PATH: |
| 336 | set_extra_library_path = tv->tv_u.tv_set_extra_library_path; |
| 337 | break; |
| 338 | case LDPT_GET_VIEW: |
| 339 | get_view = tv->tv_u.tv_get_view; |
| 340 | break; |
| 341 | case LDPT_MESSAGE: |
| 342 | message = tv->tv_u.tv_message; |
| 343 | break; |
| 344 | default: |
| 345 | break; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
Rafael Espindola | e08484d | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 349 | if (!registeredClaimFile) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 350 | message(LDPL_ERROR, "register_claim_file not passed to LLVMgold."); |
Rafael Espindola | 6add618 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 351 | return LDPS_ERR; |
| 352 | } |
Rafael Espindola | e08484d | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 353 | if (!add_symbols) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 354 | message(LDPL_ERROR, "add_symbols not passed to LLVMgold."); |
Rafael Espindola | 6add618 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 355 | return LDPS_ERR; |
| 356 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 357 | |
Rafael Espindola | a0d30a9 | 2014-06-19 22:20:07 +0000 | [diff] [blame] | 358 | if (!RegisteredAllSymbolsRead) |
| 359 | return LDPS_OK; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 360 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 361 | if (!get_input_file) { |
| 362 | message(LDPL_ERROR, "get_input_file not passed to LLVMgold."); |
| 363 | return LDPS_ERR; |
Rafael Espindola | c273aac | 2014-06-19 22:54:47 +0000 | [diff] [blame] | 364 | } |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 365 | if (!release_input_file) { |
| 366 | message(LDPL_ERROR, "relesase_input_file not passed to LLVMgold."); |
| 367 | return LDPS_ERR; |
Tom Roeder | b508119 | 2014-06-26 20:43:27 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 370 | return LDPS_OK; |
| 371 | } |
| 372 | |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 373 | static const GlobalObject *getBaseObject(const GlobalValue &GV) { |
| 374 | if (auto *GA = dyn_cast<GlobalAlias>(&GV)) |
| 375 | return GA->getBaseObject(); |
| 376 | return cast<GlobalObject>(&GV); |
| 377 | } |
| 378 | |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 379 | static bool shouldSkip(uint32_t Symflags) { |
| 380 | if (!(Symflags & object::BasicSymbolRef::SF_Global)) |
| 381 | return true; |
| 382 | if (Symflags & object::BasicSymbolRef::SF_FormatSpecific) |
| 383 | return true; |
| 384 | return false; |
| 385 | } |
| 386 | |
NAKAMURA Takumi | b13e63c | 2015-11-19 10:43:44 +0000 | [diff] [blame] | 387 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 388 | if (const auto *BDI = dyn_cast<BitcodeDiagnosticInfo>(&DI)) { |
| 389 | std::error_code EC = BDI->getError(); |
| 390 | if (EC == BitcodeError::InvalidBitcodeSignature) |
| 391 | return; |
| 392 | } |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 393 | |
| 394 | std::string ErrStorage; |
| 395 | { |
| 396 | raw_string_ostream OS(ErrStorage); |
| 397 | DiagnosticPrinterRawOStream DP(OS); |
| 398 | DI.print(DP); |
| 399 | } |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 400 | ld_plugin_level Level; |
| 401 | switch (DI.getSeverity()) { |
| 402 | case DS_Error: |
| 403 | message(LDPL_FATAL, "LLVM gold plugin has failed to create LTO module: %s", |
| 404 | ErrStorage.c_str()); |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 405 | case DS_Warning: |
| 406 | Level = LDPL_WARNING; |
| 407 | break; |
| 408 | case DS_Note: |
Rafael Espindola | f3f1854 | 2015-03-04 18:51:45 +0000 | [diff] [blame] | 409 | case DS_Remark: |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 410 | Level = LDPL_INFO; |
| 411 | break; |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 412 | } |
| 413 | message(Level, "LLVM gold plugin: %s", ErrStorage.c_str()); |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 414 | } |
| 415 | |
NAKAMURA Takumi | b13e63c | 2015-11-19 10:43:44 +0000 | [diff] [blame] | 416 | static void diagnosticHandlerForContext(const DiagnosticInfo &DI, |
| 417 | void *Context) { |
| 418 | diagnosticHandler(DI); |
| 419 | } |
| 420 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 421 | static GlobalValue::VisibilityTypes |
| 422 | getMinVisibility(GlobalValue::VisibilityTypes A, |
| 423 | GlobalValue::VisibilityTypes B) { |
| 424 | if (A == GlobalValue::HiddenVisibility) |
| 425 | return A; |
| 426 | if (B == GlobalValue::HiddenVisibility) |
| 427 | return B; |
| 428 | if (A == GlobalValue::ProtectedVisibility) |
| 429 | return A; |
| 430 | return B; |
| 431 | } |
| 432 | |
Rafael Espindola | e54d821 | 2014-07-06 14:31:22 +0000 | [diff] [blame] | 433 | /// Called by gold to see whether this file is one that our plugin can handle. |
| 434 | /// We'll try to open it and register all the symbols with add_symbol if |
| 435 | /// possible. |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 436 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 437 | int *claimed) { |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 438 | LLVMContext Context; |
Rafael Espindola | eeec8e6 | 2014-08-27 20:25:55 +0000 | [diff] [blame] | 439 | MemoryBufferRef BufferRef; |
| 440 | std::unique_ptr<MemoryBuffer> Buffer; |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 441 | if (get_view) { |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 442 | const void *view; |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 443 | if (get_view(file->handle, &view) != LDPS_OK) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 444 | message(LDPL_ERROR, "Failed to get a view of %s", file->name); |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 445 | return LDPS_ERR; |
| 446 | } |
Nick Lewycky | 7282dd7 | 2015-08-05 21:16:02 +0000 | [diff] [blame] | 447 | BufferRef = |
| 448 | MemoryBufferRef(StringRef((const char *)view, file->filesize), ""); |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 449 | } else { |
Ivan Krasin | 639222d | 2011-09-15 23:13:00 +0000 | [diff] [blame] | 450 | int64_t offset = 0; |
Nick Lewycky | 8691c47 | 2009-02-05 04:14:23 +0000 | [diff] [blame] | 451 | // Gold has found what might be IR part-way inside of a file, such as |
| 452 | // an .a archive. |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 453 | if (file->offset) { |
| 454 | offset = file->offset; |
| 455 | } |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 456 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 457 | MemoryBuffer::getOpenFileSlice(file->fd, file->name, file->filesize, |
| 458 | offset); |
| 459 | if (std::error_code EC = BufferOrErr.getError()) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 460 | message(LDPL_ERROR, EC.message().c_str()); |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 461 | return LDPS_ERR; |
| 462 | } |
Rafael Espindola | eeec8e6 | 2014-08-27 20:25:55 +0000 | [diff] [blame] | 463 | Buffer = std::move(BufferOrErr.get()); |
| 464 | BufferRef = Buffer->getMemBufferRef(); |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 465 | } |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 466 | |
NAKAMURA Takumi | b13e63c | 2015-11-19 10:43:44 +0000 | [diff] [blame] | 467 | Context.setDiagnosticHandler(diagnosticHandlerForContext); |
David Blaikie | 10a27df | 2014-09-03 17:59:23 +0000 | [diff] [blame] | 468 | ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr = |
Rafael Espindola | 5dec7ea | 2014-12-09 20:36:13 +0000 | [diff] [blame] | 469 | object::IRObjectFile::create(BufferRef, Context); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 470 | std::error_code EC = ObjOrErr.getError(); |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 471 | if (EC == object::object_error::invalid_file_type || |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 472 | EC == object::object_error::bitcode_section_not_found) |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 473 | return LDPS_OK; |
| 474 | |
Rafael Espindola | 6c472e5 | 2014-07-29 20:46:19 +0000 | [diff] [blame] | 475 | *claimed = 1; |
| 476 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 477 | if (EC) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 478 | message(LDPL_ERROR, "LLVM gold plugin has failed to create LTO module: %s", |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 479 | EC.message().c_str()); |
Rafael Espindola | 6c472e5 | 2014-07-29 20:46:19 +0000 | [diff] [blame] | 480 | return LDPS_ERR; |
Ivan Krasin | d5f2d8c | 2011-09-09 00:14:04 +0000 | [diff] [blame] | 481 | } |
David Blaikie | 10a27df | 2014-09-03 17:59:23 +0000 | [diff] [blame] | 482 | std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 483 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 484 | Modules.resize(Modules.size() + 1); |
| 485 | claimed_file &cf = Modules.back(); |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 486 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 487 | cf.handle = file->handle; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 488 | |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 489 | // If we are doing ThinLTO compilation, don't need to process the symbols. |
| 490 | // Later we simply build a combined index file after all files are claimed. |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 491 | if (options::thinlto && options::thinlto_index_only) |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 492 | return LDPS_OK; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 493 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 494 | for (auto &Sym : Obj->symbols()) { |
| 495 | uint32_t Symflags = Sym.getFlags(); |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 496 | if (shouldSkip(Symflags)) |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 497 | continue; |
| 498 | |
| 499 | cf.syms.push_back(ld_plugin_symbol()); |
| 500 | ld_plugin_symbol &sym = cf.syms.back(); |
Rafael Espindola | 176e664 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 501 | sym.version = nullptr; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 502 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 503 | SmallString<64> Name; |
| 504 | { |
| 505 | raw_svector_ostream OS(Name); |
| 506 | Sym.printName(OS); |
| 507 | } |
| 508 | sym.name = strdup(Name.c_str()); |
| 509 | |
| 510 | const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl()); |
| 511 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 512 | ResolutionInfo &Res = ResInfo[sym.name]; |
| 513 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 514 | sym.visibility = LDPV_DEFAULT; |
| 515 | if (GV) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 516 | Res.UnnamedAddr &= GV->hasUnnamedAddr(); |
| 517 | Res.IsLinkonceOdr &= GV->hasLinkOnceLinkage(); |
| 518 | if (GV->hasCommonLinkage()) { |
| 519 | Res.CommonAlign = std::max(Res.CommonAlign, GV->getAlignment()); |
| 520 | const DataLayout &DL = GV->getParent()->getDataLayout(); |
| 521 | uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType()); |
| 522 | if (Size >= Res.CommonSize) { |
| 523 | Res.CommonSize = Size; |
| 524 | Res.CommonFile = &cf; |
| 525 | } |
| 526 | } |
| 527 | Res.Visibility = getMinVisibility(Res.Visibility, GV->getVisibility()); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 528 | switch (GV->getVisibility()) { |
| 529 | case GlobalValue::DefaultVisibility: |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 530 | sym.visibility = LDPV_DEFAULT; |
| 531 | break; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 532 | case GlobalValue::HiddenVisibility: |
| 533 | sym.visibility = LDPV_HIDDEN; |
| 534 | break; |
| 535 | case GlobalValue::ProtectedVisibility: |
| 536 | sym.visibility = LDPV_PROTECTED; |
| 537 | break; |
| 538 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 541 | if (Symflags & object::BasicSymbolRef::SF_Undefined) { |
| 542 | sym.def = LDPK_UNDEF; |
| 543 | if (GV && GV->hasExternalWeakLinkage()) |
Rafael Espindola | 5654852 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 544 | sym.def = LDPK_WEAKUNDEF; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 545 | } else { |
| 546 | sym.def = LDPK_DEF; |
| 547 | if (GV) { |
| 548 | assert(!GV->hasExternalWeakLinkage() && |
| 549 | !GV->hasAvailableExternallyLinkage() && "Not a declaration!"); |
| 550 | if (GV->hasCommonLinkage()) |
| 551 | sym.def = LDPK_COMMON; |
| 552 | else if (GV->isWeakForLinker()) |
| 553 | sym.def = LDPK_WEAKDEF; |
| 554 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 557 | sym.size = 0; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 558 | sym.comdat_key = nullptr; |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 559 | if (GV) { |
| 560 | const GlobalObject *Base = getBaseObject(*GV); |
| 561 | if (!Base) |
| 562 | message(LDPL_FATAL, "Unable to determine comdat of alias!"); |
| 563 | const Comdat *C = Base->getComdat(); |
| 564 | if (C) |
| 565 | sym.comdat_key = strdup(C->getName().str().c_str()); |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 566 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 567 | |
| 568 | sym.resolution = LDPR_UNKNOWN; |
| 569 | } |
| 570 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 571 | if (!cf.syms.empty()) { |
Nick Lewycky | 7282dd7 | 2015-08-05 21:16:02 +0000 | [diff] [blame] | 572 | 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] | 573 | message(LDPL_ERROR, "Unable to add symbols!"); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 574 | return LDPS_ERR; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | return LDPS_OK; |
| 579 | } |
| 580 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 581 | static void internalize(GlobalValue &GV) { |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 582 | if (GV.isDeclarationForLinker()) |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 583 | return; // We get here if there is a matching asm definition. |
| 584 | if (!GV.hasLocalLinkage()) |
| 585 | GV.setLinkage(GlobalValue::InternalLinkage); |
| 586 | } |
| 587 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 588 | static const char *getResolutionName(ld_plugin_symbol_resolution R) { |
| 589 | switch (R) { |
| 590 | case LDPR_UNKNOWN: |
| 591 | return "UNKNOWN"; |
| 592 | case LDPR_UNDEF: |
| 593 | return "UNDEF"; |
| 594 | case LDPR_PREVAILING_DEF: |
| 595 | return "PREVAILING_DEF"; |
| 596 | case LDPR_PREVAILING_DEF_IRONLY: |
| 597 | return "PREVAILING_DEF_IRONLY"; |
| 598 | case LDPR_PREEMPTED_REG: |
| 599 | return "PREEMPTED_REG"; |
| 600 | case LDPR_PREEMPTED_IR: |
| 601 | return "PREEMPTED_IR"; |
| 602 | case LDPR_RESOLVED_IR: |
| 603 | return "RESOLVED_IR"; |
| 604 | case LDPR_RESOLVED_EXEC: |
| 605 | return "RESOLVED_EXEC"; |
| 606 | case LDPR_RESOLVED_DYN: |
| 607 | return "RESOLVED_DYN"; |
| 608 | case LDPR_PREVAILING_DEF_IRONLY_EXP: |
| 609 | return "PREVAILING_DEF_IRONLY_EXP"; |
| 610 | } |
Rafael Espindola | 2754dbb | 2014-10-10 00:48:13 +0000 | [diff] [blame] | 611 | llvm_unreachable("Unknown resolution"); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Rafael Espindola | 538c9a8 | 2014-12-23 18:18:37 +0000 | [diff] [blame] | 614 | static void freeSymName(ld_plugin_symbol &Sym) { |
| 615 | free(Sym.name); |
| 616 | free(Sym.comdat_key); |
| 617 | Sym.name = nullptr; |
| 618 | Sym.comdat_key = nullptr; |
| 619 | } |
| 620 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 621 | /// Helper to get a file's symbols and a view into it via gold callbacks. |
| 622 | static const void *getSymbolsAndView(claimed_file &F) { |
Benjamin Kramer | 39988a0 | 2016-03-08 14:02:46 +0000 | [diff] [blame] | 623 | 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] | 624 | if (status == LDPS_NO_SYMS) |
| 625 | return nullptr; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 626 | |
Evgeniy Stepanov | 330c5a6 | 2016-03-04 00:23:29 +0000 | [diff] [blame] | 627 | if (status != LDPS_OK) |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 628 | message(LDPL_FATAL, "Failed to get symbol information"); |
| 629 | |
| 630 | const void *View; |
| 631 | if (get_view(F.handle, &View) != LDPS_OK) |
| 632 | message(LDPL_FATAL, "Failed to get a view of file"); |
| 633 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 634 | return View; |
| 635 | } |
| 636 | |
| 637 | static std::unique_ptr<FunctionInfoIndex> |
| 638 | getFunctionIndexForFile(claimed_file &F, ld_plugin_input_file &Info) { |
| 639 | const void *View = getSymbolsAndView(F); |
| 640 | |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 641 | MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize), |
| 642 | Info.name); |
Teresa Johnson | 6290dbc | 2015-11-21 21:55:48 +0000 | [diff] [blame] | 643 | |
| 644 | // Don't bother trying to build an index if there is no summary information |
| 645 | // in this bitcode file. |
| 646 | if (!object::FunctionIndexObjectFile::hasFunctionSummaryInMemBuffer( |
| 647 | BufferRef, diagnosticHandler)) |
| 648 | return std::unique_ptr<FunctionInfoIndex>(nullptr); |
| 649 | |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 650 | ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr = |
NAKAMURA Takumi | b13e63c | 2015-11-19 10:43:44 +0000 | [diff] [blame] | 651 | object::FunctionIndexObjectFile::create(BufferRef, diagnosticHandler); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 652 | |
| 653 | if (std::error_code EC = ObjOrErr.getError()) |
| 654 | message(LDPL_FATAL, "Could not read function index bitcode from file : %s", |
| 655 | EC.message().c_str()); |
| 656 | |
| 657 | object::FunctionIndexObjectFile &Obj = **ObjOrErr; |
| 658 | |
| 659 | return Obj.takeIndex(); |
| 660 | } |
| 661 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 662 | static std::unique_ptr<Module> |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 663 | getModuleForFile(LLVMContext &Context, claimed_file &F, const void *View, |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 664 | ld_plugin_input_file &Info, raw_fd_ostream *ApiFile, |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 665 | StringSet<> &Internalize, StringSet<> &Maybe, |
| 666 | std::vector<GlobalValue *> &Keep) { |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 667 | MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize), |
| 668 | Info.name); |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 669 | ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr = |
Rafael Espindola | 5dec7ea | 2014-12-09 20:36:13 +0000 | [diff] [blame] | 670 | object::IRObjectFile::create(BufferRef, Context); |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 671 | |
| 672 | if (std::error_code EC = ObjOrErr.getError()) |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 673 | message(LDPL_FATAL, "Could not read bitcode from file : %s", |
| 674 | EC.message().c_str()); |
| 675 | |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 676 | object::IRObjectFile &Obj = **ObjOrErr; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 677 | |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 678 | Module &M = Obj.getModule(); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 679 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 680 | M.materializeMetadata(); |
| 681 | UpgradeDebugInfo(M); |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 682 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 683 | SmallPtrSet<GlobalValue *, 8> Used; |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 684 | collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 685 | |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 686 | unsigned SymNum = 0; |
| 687 | for (auto &ObjSym : Obj.symbols()) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 688 | GlobalValue *GV = Obj.getSymbolGV(ObjSym.getRawDataRefImpl()); |
| 689 | if (GV && GV->hasAppendingLinkage()) |
| 690 | Keep.push_back(GV); |
| 691 | |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 692 | if (shouldSkip(ObjSym.getFlags())) |
| 693 | continue; |
| 694 | ld_plugin_symbol &Sym = F.syms[SymNum]; |
| 695 | ++SymNum; |
| 696 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 697 | ld_plugin_symbol_resolution Resolution = |
| 698 | (ld_plugin_symbol_resolution)Sym.resolution; |
| 699 | |
| 700 | if (options::generate_api_file) |
| 701 | *ApiFile << Sym.name << ' ' << getResolutionName(Resolution) << '\n'; |
| 702 | |
Rafael Espindola | 538c9a8 | 2014-12-23 18:18:37 +0000 | [diff] [blame] | 703 | if (!GV) { |
| 704 | freeSymName(Sym); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 705 | continue; // Asm symbol. |
Rafael Espindola | 538c9a8 | 2014-12-23 18:18:37 +0000 | [diff] [blame] | 706 | } |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 707 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 708 | ResolutionInfo &Res = ResInfo[Sym.name]; |
| 709 | if (Resolution == LDPR_PREVAILING_DEF_IRONLY_EXP && !Res.IsLinkonceOdr) |
| 710 | Resolution = LDPR_PREVAILING_DEF; |
| 711 | |
Teresa Johnson | a17f2cd | 2016-03-04 17:48:35 +0000 | [diff] [blame] | 712 | // In ThinLTO mode change all prevailing resolutions to LDPR_PREVAILING_DEF. |
| 713 | // For ThinLTO the IR files are compiled through the backend independently, |
| 714 | // so we need to ensure that any prevailing linkonce copy will be emitted |
| 715 | // into the object file by making it weak. Additionally, we can skip the |
| 716 | // IRONLY handling for internalization, which isn't performed in ThinLTO |
| 717 | // mode currently anyway. |
| 718 | if (options::thinlto && (Resolution == LDPR_PREVAILING_DEF_IRONLY_EXP || |
| 719 | Resolution == LDPR_PREVAILING_DEF_IRONLY)) |
| 720 | Resolution = LDPR_PREVAILING_DEF; |
| 721 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 722 | GV->setUnnamedAddr(Res.UnnamedAddr); |
| 723 | GV->setVisibility(Res.Visibility); |
| 724 | |
| 725 | // Override gold's resolution for common symbols. We want the largest |
| 726 | // one to win. |
| 727 | if (GV->hasCommonLinkage()) { |
| 728 | cast<GlobalVariable>(GV)->setAlignment(Res.CommonAlign); |
| 729 | if (Resolution == LDPR_PREVAILING_DEF_IRONLY) |
| 730 | Res.CommonInternal = true; |
| 731 | |
| 732 | if (Resolution == LDPR_PREVAILING_DEF_IRONLY || |
| 733 | Resolution == LDPR_PREVAILING_DEF) |
| 734 | Res.UseCommon = true; |
| 735 | |
| 736 | if (Res.CommonFile == &F && Res.UseCommon) { |
| 737 | if (Res.CommonInternal) |
| 738 | Resolution = LDPR_PREVAILING_DEF_IRONLY; |
| 739 | else |
| 740 | Resolution = LDPR_PREVAILING_DEF; |
| 741 | } else { |
| 742 | Resolution = LDPR_PREEMPTED_IR; |
| 743 | } |
Rafael Espindola | 890db27 | 2014-09-09 20:08:22 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 746 | switch (Resolution) { |
| 747 | case LDPR_UNKNOWN: |
| 748 | llvm_unreachable("Unexpected resolution"); |
| 749 | |
| 750 | case LDPR_RESOLVED_IR: |
| 751 | case LDPR_RESOLVED_EXEC: |
| 752 | case LDPR_RESOLVED_DYN: |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 753 | case LDPR_PREEMPTED_IR: |
| 754 | case LDPR_PREEMPTED_REG: |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 755 | break; |
| 756 | |
Rafael Espindola | 5ca7fa1 | 2015-01-14 13:53:50 +0000 | [diff] [blame] | 757 | case LDPR_UNDEF: |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 758 | if (!GV->isDeclarationForLinker()) |
Rafael Espindola | 0fd9e5f | 2015-01-14 19:43:32 +0000 | [diff] [blame] | 759 | assert(GV->hasComdat()); |
Rafael Espindola | 5ca7fa1 | 2015-01-14 13:53:50 +0000 | [diff] [blame] | 760 | break; |
| 761 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 762 | case LDPR_PREVAILING_DEF_IRONLY: { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 763 | Keep.push_back(GV); |
| 764 | // The IR linker has to be able to map this value to a declaration, |
| 765 | // so we can only internalize after linking. |
| 766 | if (!Used.count(GV)) |
Rafael Espindola | a4f104b | 2014-12-09 16:50:57 +0000 | [diff] [blame] | 767 | Internalize.insert(GV->getName()); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 768 | break; |
| 769 | } |
| 770 | |
| 771 | case LDPR_PREVAILING_DEF: |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 772 | Keep.push_back(GV); |
| 773 | // There is a non IR use, so we have to force optimizations to keep this. |
| 774 | switch (GV->getLinkage()) { |
| 775 | default: |
| 776 | break; |
| 777 | case GlobalValue::LinkOnceAnyLinkage: |
| 778 | GV->setLinkage(GlobalValue::WeakAnyLinkage); |
| 779 | break; |
| 780 | case GlobalValue::LinkOnceODRLinkage: |
| 781 | GV->setLinkage(GlobalValue::WeakODRLinkage); |
| 782 | break; |
| 783 | } |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 784 | break; |
| 785 | |
| 786 | case LDPR_PREVAILING_DEF_IRONLY_EXP: { |
| 787 | // We can only check for address uses after we merge the modules. The |
| 788 | // reason is that this GV might have a copy in another module |
| 789 | // and in that module the address might be significant, but that |
| 790 | // copy will be LDPR_PREEMPTED_IR. |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 791 | Maybe.insert(GV->getName()); |
| 792 | Keep.push_back(GV); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 793 | break; |
| 794 | } |
| 795 | } |
| 796 | |
Rafael Espindola | 538c9a8 | 2014-12-23 18:18:37 +0000 | [diff] [blame] | 797 | freeSymName(Sym); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 800 | return Obj.takeModule(); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Duncan P. N. Exon Smith | e406c84 | 2015-04-15 00:13:51 +0000 | [diff] [blame] | 803 | static void saveBCFile(StringRef Path, Module &M) { |
Rafael Espindola | 4a3b6cf | 2014-10-29 23:54:45 +0000 | [diff] [blame] | 804 | std::error_code EC; |
| 805 | raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None); |
| 806 | if (EC) |
| 807 | message(LDPL_FATAL, "Failed to write the output file."); |
Rafael Espindola | 2405298 | 2015-12-22 23:45:49 +0000 | [diff] [blame] | 808 | WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ false); |
Rafael Espindola | 4a3b6cf | 2014-10-29 23:54:45 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 811 | static void recordFile(std::string Filename, bool TempOutFile) { |
| 812 | if (add_input_file(Filename.c_str()) != LDPS_OK) |
| 813 | message(LDPL_FATAL, |
| 814 | "Unable to add .o file to the link. File left behind in: %s", |
| 815 | Filename.c_str()); |
| 816 | if (TempOutFile) |
| 817 | Cleanup.push_back(Filename.c_str()); |
| 818 | } |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 819 | |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 820 | void ThinLTOTaskInfo::cleanup() { |
| 821 | // Close the output file descriptor before we pass it to gold. |
| 822 | OS->close(); |
| 823 | |
| 824 | recordFile(Filename, TempOutFile); |
| 825 | } |
| 826 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 827 | namespace { |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 828 | /// Class to manage optimization and code generation for a module, possibly |
| 829 | /// in a thread (ThinLTO). |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 830 | class CodeGen { |
| 831 | /// The module for which this will generate code. |
| 832 | std::unique_ptr<llvm::Module> M; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 833 | |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 834 | /// The output stream to generate code into. |
| 835 | raw_fd_ostream *OS; |
| 836 | |
| 837 | /// The task ID when this was invoked in a thread (ThinLTO). |
| 838 | int TaskID; |
| 839 | |
| 840 | /// The function index for ThinLTO tasks. |
| 841 | const FunctionInfoIndex *CombinedIndex; |
| 842 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 843 | /// The target machine for generating code for this module. |
| 844 | std::unique_ptr<TargetMachine> TM; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 845 | |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 846 | /// Filename to use as base when save-temps is enabled, used to get |
| 847 | /// a unique and identifiable save-temps output file for each ThinLTO backend. |
| 848 | std::string SaveTempsFilename; |
| 849 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 850 | public: |
| 851 | /// Constructor used by full LTO. |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 852 | CodeGen(std::unique_ptr<llvm::Module> M) |
| 853 | : M(std::move(M)), OS(nullptr), TaskID(-1), CombinedIndex(nullptr) { |
| 854 | initTargetMachine(); |
| 855 | } |
| 856 | /// Constructor used by ThinLTO. |
| 857 | CodeGen(std::unique_ptr<llvm::Module> M, raw_fd_ostream *OS, int TaskID, |
| 858 | const FunctionInfoIndex *CombinedIndex, std::string Filename) |
| 859 | : M(std::move(M)), OS(OS), TaskID(TaskID), CombinedIndex(CombinedIndex), |
| 860 | SaveTempsFilename(Filename) { |
| 861 | assert(options::thinlto == !!CombinedIndex && |
| 862 | "Expected function index iff performing ThinLTO"); |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 863 | initTargetMachine(); |
| 864 | } |
| 865 | |
| 866 | /// Invoke LTO passes and the code generator for the module. |
| 867 | void runAll(); |
| 868 | |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 869 | /// Invoke the actual code generation to emit Module's object to file. |
| 870 | void runCodegenPasses(); |
| 871 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 872 | private: |
| 873 | /// Create a target machine for the module. Must be unique for each |
| 874 | /// module/task. |
| 875 | void initTargetMachine(); |
| 876 | |
| 877 | /// Run all LTO passes on the module. |
| 878 | void runLTOPasses(); |
| 879 | |
| 880 | /// Sets up output files necessary to perform optional multi-threaded |
| 881 | /// split code generation, and invokes the code generation implementation. |
| 882 | void runSplitCodeGen(); |
| 883 | }; |
| 884 | } |
| 885 | |
| 886 | static SubtargetFeatures getFeatures(Triple &TheTriple) { |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 887 | SubtargetFeatures Features; |
| 888 | Features.getDefaultSubtargetFeatures(TheTriple); |
| 889 | for (const std::string &A : MAttrs) |
| 890 | Features.AddFeature(A); |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 891 | return Features; |
| 892 | } |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 893 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 894 | static CodeGenOpt::Level getCGOptLevel() { |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 895 | CodeGenOpt::Level CGOptLevel; |
| 896 | switch (options::OptLevel) { |
| 897 | case 0: |
| 898 | CGOptLevel = CodeGenOpt::None; |
| 899 | break; |
| 900 | case 1: |
| 901 | CGOptLevel = CodeGenOpt::Less; |
| 902 | break; |
| 903 | case 2: |
| 904 | CGOptLevel = CodeGenOpt::Default; |
| 905 | break; |
| 906 | case 3: |
| 907 | CGOptLevel = CodeGenOpt::Aggressive; |
| 908 | break; |
| 909 | } |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 910 | return CGOptLevel; |
| 911 | } |
| 912 | |
| 913 | void CodeGen::initTargetMachine() { |
| 914 | const std::string &TripleStr = M->getTargetTriple(); |
| 915 | Triple TheTriple(TripleStr); |
| 916 | |
| 917 | std::string ErrMsg; |
| 918 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg); |
| 919 | if (!TheTarget) |
| 920 | message(LDPL_FATAL, "Target not found: %s", ErrMsg.c_str()); |
| 921 | |
| 922 | SubtargetFeatures Features = getFeatures(TheTriple); |
| 923 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
| 924 | CodeGenOpt::Level CGOptLevel = getCGOptLevel(); |
| 925 | |
| 926 | TM.reset(TheTarget->createTargetMachine( |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 927 | TripleStr, options::mcpu, Features.getString(), Options, RelocationModel, |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 928 | CodeModel::Default, CGOptLevel)); |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 929 | } |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 930 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 931 | void CodeGen::runLTOPasses() { |
| 932 | M->setDataLayout(TM->createDataLayout()); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 933 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 934 | legacy::PassManager passes; |
| 935 | passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis())); |
| 936 | |
| 937 | PassManagerBuilder PMB; |
| 938 | PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple())); |
| 939 | PMB.Inliner = createFunctionInliningPass(); |
| 940 | // Unconditionally verify input since it is not verified before this |
| 941 | // point and has unknown origin. |
| 942 | PMB.VerifyInput = true; |
| 943 | PMB.VerifyOutput = !options::DisableVerify; |
| 944 | PMB.LoopVectorize = true; |
| 945 | PMB.SLPVectorize = true; |
| 946 | PMB.OptLevel = options::OptLevel; |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 947 | PMB.FunctionIndex = CombinedIndex; |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 948 | PMB.populateLTOPassManager(passes); |
| 949 | passes.run(*M); |
| 950 | } |
| 951 | |
| 952 | /// Open a file and return the new file descriptor given a base input |
| 953 | /// file name, a flag indicating whether a temp file should be generated, |
| 954 | /// and an optional task id. The new filename generated is |
| 955 | /// returned in \p NewFilename. |
| 956 | static int openOutputFile(SmallString<128> InFilename, bool TempOutFile, |
| 957 | SmallString<128> &NewFilename, int TaskID = -1) { |
| 958 | int FD; |
| 959 | if (TempOutFile) { |
| 960 | std::error_code EC = |
| 961 | sys::fs::createTemporaryFile("lto-llvm", "o", FD, NewFilename); |
| 962 | if (EC) |
| 963 | message(LDPL_FATAL, "Could not create temporary file: %s", |
| 964 | EC.message().c_str()); |
| 965 | } else { |
| 966 | NewFilename = InFilename; |
| 967 | if (TaskID >= 0) |
| 968 | NewFilename += utostr(TaskID); |
| 969 | std::error_code EC = |
| 970 | sys::fs::openFileForWrite(NewFilename, FD, sys::fs::F_None); |
| 971 | if (EC) |
| 972 | message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str()); |
| 973 | } |
| 974 | return FD; |
| 975 | } |
| 976 | |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 977 | void CodeGen::runCodegenPasses() { |
| 978 | assert(OS && "Output stream must be set before emitting to file"); |
| 979 | legacy::PassManager CodeGenPasses; |
| 980 | if (TM->addPassesToEmitFile(CodeGenPasses, *OS, |
| 981 | TargetMachine::CGFT_ObjectFile)) |
| 982 | report_fatal_error("Failed to setup codegen"); |
| 983 | CodeGenPasses.run(*M); |
| 984 | } |
| 985 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 986 | void CodeGen::runSplitCodeGen() { |
| 987 | const std::string &TripleStr = M->getTargetTriple(); |
| 988 | Triple TheTriple(TripleStr); |
| 989 | |
| 990 | SubtargetFeatures Features = getFeatures(TheTriple); |
| 991 | |
| 992 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
| 993 | CodeGenOpt::Level CGOptLevel = getCGOptLevel(); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 994 | |
| 995 | SmallString<128> Filename; |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 996 | // Note that openOutputFile will append a unique ID for each task |
Rafael Espindola | 92200d2 | 2015-06-15 13:36:27 +0000 | [diff] [blame] | 997 | if (!options::obj_path.empty()) |
| 998 | Filename = options::obj_path; |
| 999 | else if (options::TheOutputType == options::OT_SAVE_TEMPS) |
| 1000 | Filename = output_name + ".o"; |
| 1001 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 1002 | // Note that the default parallelism is 1 instead of the |
| 1003 | // hardware_concurrency, as there are behavioral differences between |
| 1004 | // parallelism levels (e.g. symbol ordering will be different, and some uses |
| 1005 | // of inline asm currently have issues with parallelism >1). |
| 1006 | unsigned int MaxThreads = options::Parallelism ? options::Parallelism : 1; |
| 1007 | |
| 1008 | std::vector<SmallString<128>> Filenames(MaxThreads); |
Rafael Espindola | 92200d2 | 2015-06-15 13:36:27 +0000 | [diff] [blame] | 1009 | bool TempOutFile = Filename.empty(); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 1010 | { |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 1011 | // Open a file descriptor for each backend task. This is done in a block |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 1012 | // so that the output file descriptors are closed before gold opens them. |
| 1013 | std::list<llvm::raw_fd_ostream> OSs; |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 1014 | std::vector<llvm::raw_pwrite_stream *> OSPtrs(MaxThreads); |
| 1015 | for (unsigned I = 0; I != MaxThreads; ++I) { |
Teresa Johnson | 3b8f612 | 2016-03-04 18:16:00 +0000 | [diff] [blame] | 1016 | int FD = openOutputFile(Filename, TempOutFile, Filenames[I], |
| 1017 | // Only append ID if there are multiple tasks. |
| 1018 | MaxThreads > 1 ? I : -1); |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 1019 | OSs.emplace_back(FD, true); |
| 1020 | OSPtrs[I] = &OSs.back(); |
| 1021 | } |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 1022 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 1023 | // Run backend tasks. |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 1024 | splitCodeGen(std::move(M), OSPtrs, options::mcpu, Features.getString(), |
| 1025 | Options, RelocationModel, CodeModel::Default, CGOptLevel); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 1028 | for (auto &Filename : Filenames) |
| 1029 | recordFile(Filename.c_str(), TempOutFile); |
| 1030 | } |
| 1031 | |
| 1032 | void CodeGen::runAll() { |
| 1033 | runLTOPasses(); |
| 1034 | |
| 1035 | if (options::TheOutputType == options::OT_SAVE_TEMPS) { |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 1036 | std::string OptFilename = output_name; |
| 1037 | // If the CodeGen client provided a filename, use it. Always expect |
| 1038 | // a provided filename if we are in a task (i.e. ThinLTO backend). |
| 1039 | assert(!SaveTempsFilename.empty() || TaskID == -1); |
| 1040 | if (!SaveTempsFilename.empty()) |
| 1041 | OptFilename = SaveTempsFilename; |
| 1042 | saveBCFile(OptFilename + ".opt.bc", *M); |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 1043 | } |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 1044 | |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 1045 | // If we are already in a thread (i.e. ThinLTO), just perform |
| 1046 | // codegen passes directly. |
| 1047 | if (TaskID >= 0) |
| 1048 | runCodegenPasses(); |
| 1049 | // Otherwise attempt split code gen. |
| 1050 | else |
| 1051 | runSplitCodeGen(); |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /// Links the module in \p View from file \p F into the combined module |
| 1055 | /// saved in the IRMover \p L. Returns true on error, false on success. |
| 1056 | static bool linkInModule(LLVMContext &Context, IRMover &L, claimed_file &F, |
| 1057 | const void *View, ld_plugin_input_file &File, |
| 1058 | raw_fd_ostream *ApiFile, StringSet<> &Internalize, |
| 1059 | StringSet<> &Maybe) { |
| 1060 | std::vector<GlobalValue *> Keep; |
| 1061 | std::unique_ptr<Module> M = getModuleForFile(Context, F, View, File, ApiFile, |
| 1062 | Internalize, Maybe, Keep); |
| 1063 | if (!M.get()) |
| 1064 | return false; |
| 1065 | if (!options::triple.empty()) |
| 1066 | M->setTargetTriple(options::triple.c_str()); |
| 1067 | else if (M->getTargetTriple().empty()) { |
| 1068 | M->setTargetTriple(DefaultTriple); |
| 1069 | } |
| 1070 | |
| 1071 | if (L.move(std::move(M), Keep, [](GlobalValue &, IRMover::ValueAdder) {})) |
| 1072 | return true; |
| 1073 | return false; |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 1076 | /// Perform the ThinLTO backend on a single module, invoking the LTO and codegen |
| 1077 | /// pipelines. |
| 1078 | static void thinLTOBackendTask(claimed_file &F, const void *View, |
| 1079 | ld_plugin_input_file &File, |
| 1080 | raw_fd_ostream *ApiFile, |
| 1081 | const FunctionInfoIndex &CombinedIndex, |
| 1082 | raw_fd_ostream *OS, unsigned TaskID) { |
| 1083 | // Need to use a separate context for each task |
| 1084 | LLVMContext Context; |
| 1085 | Context.setDiagnosticHandler(diagnosticHandlerForContext, nullptr, true); |
| 1086 | |
| 1087 | std::unique_ptr<llvm::Module> NewModule(new llvm::Module(File.name, Context)); |
| 1088 | IRMover L(*NewModule.get()); |
| 1089 | |
| 1090 | StringSet<> Dummy; |
| 1091 | if (linkInModule(Context, L, F, View, File, ApiFile, Dummy, Dummy)) |
| 1092 | message(LDPL_FATAL, "Failed to rename module for ThinLTO"); |
Mehdi Amini | d2d9896 | 2016-03-09 01:55:15 +0000 | [diff] [blame^] | 1093 | if (renameModuleForThinLTO(*NewModule, CombinedIndex)) |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 1094 | message(LDPL_FATAL, "Failed to rename module for ThinLTO"); |
| 1095 | |
| 1096 | CodeGen codeGen(std::move(NewModule), OS, TaskID, &CombinedIndex, File.name); |
| 1097 | codeGen.runAll(); |
| 1098 | } |
| 1099 | |
| 1100 | /// Launch each module's backend pipeline in a separate task in a thread pool. |
| 1101 | static void thinLTOBackends(raw_fd_ostream *ApiFile, |
| 1102 | const FunctionInfoIndex &CombinedIndex) { |
| 1103 | unsigned TaskCount = 0; |
| 1104 | std::vector<ThinLTOTaskInfo> Tasks; |
| 1105 | Tasks.reserve(Modules.size()); |
| 1106 | unsigned int MaxThreads = options::Parallelism |
| 1107 | ? options::Parallelism |
| 1108 | : thread::hardware_concurrency(); |
| 1109 | |
| 1110 | // Create ThreadPool in nested scope so that threads will be joined |
| 1111 | // on destruction. |
| 1112 | { |
| 1113 | ThreadPool ThinLTOThreadPool(MaxThreads); |
| 1114 | for (claimed_file &F : Modules) { |
| 1115 | // Do all the gold callbacks in the main thread, since gold is not thread |
| 1116 | // safe by default. |
| 1117 | PluginInputFile InputFile(F.handle); |
| 1118 | const void *View = getSymbolsAndView(F); |
| 1119 | |
| 1120 | SmallString<128> Filename; |
| 1121 | if (!options::obj_path.empty()) |
| 1122 | // Note that openOutputFile will append a unique ID for each task |
| 1123 | Filename = options::obj_path; |
| 1124 | else if (options::TheOutputType == options::OT_SAVE_TEMPS) { |
| 1125 | // Use the input file name so that we get a unique and identifiable |
| 1126 | // output file for each ThinLTO backend task. |
| 1127 | Filename = InputFile.file().name; |
| 1128 | Filename += ".thinlto.o"; |
| 1129 | } |
| 1130 | bool TempOutFile = Filename.empty(); |
| 1131 | |
| 1132 | SmallString<128> NewFilename; |
| 1133 | int FD = openOutputFile(Filename, TempOutFile, NewFilename, |
| 1134 | // Only append the TaskID if we will use the |
| 1135 | // non-unique obj_path. |
| 1136 | !options::obj_path.empty() ? TaskCount : -1); |
| 1137 | TaskCount++; |
| 1138 | std::unique_ptr<raw_fd_ostream> OS = |
| 1139 | llvm::make_unique<raw_fd_ostream>(FD, true); |
| 1140 | |
| 1141 | // Enqueue the task |
| 1142 | ThinLTOThreadPool.async(thinLTOBackendTask, std::ref(F), View, |
| 1143 | std::ref(InputFile.file()), ApiFile, |
| 1144 | std::ref(CombinedIndex), OS.get(), TaskCount); |
| 1145 | |
| 1146 | // Record the information needed by the task or during its cleanup |
| 1147 | // to a ThinLTOTaskInfo instance. For information needed by the task |
| 1148 | // the unique_ptr ownership is transferred to the ThinLTOTaskInfo. |
| 1149 | Tasks.emplace_back(std::move(InputFile), std::move(OS), |
| 1150 | NewFilename.c_str(), TempOutFile); |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | for (auto &Task : Tasks) |
| 1155 | Task.cleanup(); |
| 1156 | } |
| 1157 | |
Rafael Espindola | b639329 | 2014-07-30 01:23:45 +0000 | [diff] [blame] | 1158 | /// gold informs us that all symbols have been read. At this point, we use |
| 1159 | /// get_symbols to see if any of our definitions have been overridden by a |
| 1160 | /// native object file. Then, perform optimization and codegen. |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 1161 | static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) { |
| 1162 | if (Modules.empty()) |
| 1163 | return LDPS_OK; |
Rafael Espindola | 9ef90d5 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 1164 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 1165 | if (unsigned NumOpts = options::extra.size()) |
| 1166 | cl::ParseCommandLineOptions(NumOpts, &options::extra[0]); |
| 1167 | |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 1168 | // If we are doing ThinLTO compilation, simply build the combined |
| 1169 | // function index/summary and emit it. We don't need to parse the modules |
| 1170 | // and link them in this case. |
| 1171 | if (options::thinlto) { |
Teresa Johnson | 8af8d4e | 2015-10-19 15:23:03 +0000 | [diff] [blame] | 1172 | FunctionInfoIndex CombinedIndex; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 1173 | uint64_t NextModuleId = 0; |
| 1174 | for (claimed_file &F : Modules) { |
Teresa Johnson | cb15b73 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 1175 | PluginInputFile InputFile(F.handle); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 1176 | |
| 1177 | std::unique_ptr<FunctionInfoIndex> Index = |
Teresa Johnson | cb15b73 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 1178 | getFunctionIndexForFile(F, InputFile.file()); |
Teresa Johnson | 6290dbc | 2015-11-21 21:55:48 +0000 | [diff] [blame] | 1179 | |
| 1180 | // Skip files without a function summary. |
Teresa Johnson | 9d5b71b | 2015-12-10 16:11:23 +0000 | [diff] [blame] | 1181 | if (Index) |
| 1182 | CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | std::error_code EC; |
| 1186 | raw_fd_ostream OS(output_name + ".thinlto.bc", EC, |
| 1187 | sys::fs::OpenFlags::F_None); |
| 1188 | if (EC) |
| 1189 | message(LDPL_FATAL, "Unable to open %s.thinlto.bc for writing: %s", |
| 1190 | output_name.data(), EC.message().c_str()); |
Teresa Johnson | 3da931f | 2015-10-19 19:06:06 +0000 | [diff] [blame] | 1191 | WriteFunctionSummaryToFile(CombinedIndex, OS); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 1192 | OS.close(); |
| 1193 | |
Teresa Johnson | 7cffaf3 | 2016-03-04 17:06:02 +0000 | [diff] [blame] | 1194 | if (options::thinlto_index_only) { |
| 1195 | cleanup_hook(); |
| 1196 | exit(0); |
| 1197 | } |
| 1198 | |
| 1199 | thinLTOBackends(ApiFile, CombinedIndex); |
| 1200 | return LDPS_OK; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Teresa Johnson | af9e931 | 2015-12-09 19:49:40 +0000 | [diff] [blame] | 1203 | LLVMContext Context; |
| 1204 | Context.setDiagnosticHandler(diagnosticHandlerForContext, nullptr, true); |
| 1205 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 1206 | std::unique_ptr<Module> Combined(new Module("ld-temp.o", Context)); |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 1207 | IRMover L(*Combined); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 1208 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 1209 | StringSet<> Internalize; |
| 1210 | StringSet<> Maybe; |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 1211 | for (claimed_file &F : Modules) { |
Teresa Johnson | cb15b73 | 2015-12-16 16:34:06 +0000 | [diff] [blame] | 1212 | PluginInputFile InputFile(F.handle); |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 1213 | const void *View = getSymbolsAndView(F); |
| 1214 | if (linkInModule(Context, L, F, View, InputFile.file(), ApiFile, |
| 1215 | Internalize, Maybe)) |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 1216 | message(LDPL_FATAL, "Failed to link module"); |
Rafael Espindola | 77b6d01 | 2010-06-14 21:20:52 +0000 | [diff] [blame] | 1217 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 1218 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 1219 | for (const auto &Name : Internalize) { |
| 1220 | GlobalValue *GV = Combined->getNamedValue(Name.first()); |
| 1221 | if (GV) |
| 1222 | internalize(*GV); |
| 1223 | } |
| 1224 | |
| 1225 | for (const auto &Name : Maybe) { |
| 1226 | GlobalValue *GV = Combined->getNamedValue(Name.first()); |
| 1227 | if (!GV) |
| 1228 | continue; |
| 1229 | GV->setLinkage(GlobalValue::LinkOnceODRLinkage); |
| 1230 | if (canBeOmittedFromSymbolTable(GV)) |
| 1231 | internalize(*GV); |
| 1232 | } |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 1233 | |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 1234 | if (options::TheOutputType == options::OT_DISABLE) |
| 1235 | return LDPS_OK; |
| 1236 | |
| 1237 | if (options::TheOutputType != options::OT_NORMAL) { |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 1238 | std::string path; |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 1239 | if (options::TheOutputType == options::OT_BC_ONLY) |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 1240 | path = output_name; |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 1241 | else |
| 1242 | path = output_name + ".bc"; |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 1243 | saveBCFile(path, *Combined); |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 1244 | if (options::TheOutputType == options::OT_BC_ONLY) |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 1245 | return LDPS_OK; |
Rafael Espindola | ba3398b | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 1246 | } |
Rafael Espindola | 143fc3b | 2013-10-16 12:47:04 +0000 | [diff] [blame] | 1247 | |
Teresa Johnson | a9f6555 | 2016-03-04 16:36:06 +0000 | [diff] [blame] | 1248 | CodeGen codeGen(std::move(Combined)); |
| 1249 | codeGen.runAll(); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 1250 | |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 1251 | if (!options::extra_library_path.empty() && |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 1252 | set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) |
| 1253 | message(LDPL_FATAL, "Unable to set the extra library path."); |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 1254 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 1255 | return LDPS_OK; |
| 1256 | } |
| 1257 | |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 1258 | static ld_plugin_status all_symbols_read_hook(void) { |
| 1259 | ld_plugin_status Ret; |
| 1260 | if (!options::generate_api_file) { |
| 1261 | Ret = allSymbolsReadHook(nullptr); |
| 1262 | } else { |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 1263 | std::error_code EC; |
| 1264 | raw_fd_ostream ApiFile("apifile.txt", EC, sys::fs::F_None); |
| 1265 | if (EC) |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 1266 | message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s", |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 1267 | EC.message().c_str()); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 1268 | Ret = allSymbolsReadHook(&ApiFile); |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Rafael Espindola | 947bdb6 | 2014-11-25 20:52:49 +0000 | [diff] [blame] | 1271 | llvm_shutdown(); |
| 1272 | |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 1273 | if (options::TheOutputType == options::OT_BC_ONLY || |
Michael Kuperstein | a07d9b9 | 2015-02-12 18:21:50 +0000 | [diff] [blame] | 1274 | options::TheOutputType == options::OT_DISABLE) { |
| 1275 | if (options::TheOutputType == options::OT_DISABLE) |
| 1276 | // Remove the output file here since ld.bfd creates the output file |
| 1277 | // early. |
| 1278 | sys::fs::remove(output_name); |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 1279 | exit(0); |
Michael Kuperstein | a07d9b9 | 2015-02-12 18:21:50 +0000 | [diff] [blame] | 1280 | } |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 1281 | |
| 1282 | return Ret; |
| 1283 | } |
| 1284 | |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 1285 | static ld_plugin_status cleanup_hook(void) { |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 1286 | for (std::string &Name : Cleanup) { |
| 1287 | std::error_code EC = sys::fs::remove(Name); |
Rafael Espindola | 55ab87f | 2013-06-17 18:38:18 +0000 | [diff] [blame] | 1288 | if (EC) |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 1289 | message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(), |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 1290 | EC.message().c_str()); |
Rafael Espindola | 55ab87f | 2013-06-17 18:38:18 +0000 | [diff] [blame] | 1291 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 1292 | |
| 1293 | return LDPS_OK; |
| 1294 | } |