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" |
| 32 | #include "llvm/Linker/Linker.h" |
| 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 | b13dbd6 | 2015-12-09 19:45:55 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 42 | #include "llvm/Transforms/IPO.h" |
| 43 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| 44 | #include "llvm/Transforms/Utils/GlobalStatus.h" |
| 45 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 46 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 47 | #include <list> |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 48 | #include <plugin-api.h> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 49 | #include <system_error> |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 50 | #include <vector> |
| 51 | |
Sylvestre Ledru | 5399979 | 2014-02-11 17:30:18 +0000 | [diff] [blame] | 52 | #ifndef LDPO_PIE |
| 53 | // FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and |
| 54 | // Precise and Debian Wheezy (binutils 2.23 is required) |
| 55 | # define LDPO_PIE 3 |
| 56 | #endif |
| 57 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 58 | using namespace llvm; |
| 59 | |
| 60 | namespace { |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 61 | struct claimed_file { |
| 62 | void *handle; |
| 63 | std::vector<ld_plugin_symbol> syms; |
| 64 | }; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 65 | } |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 66 | |
| 67 | static ld_plugin_status discard_message(int level, const char *format, ...) { |
| 68 | // Die loudly. Recent versions of Gold pass ld_plugin_message as the first |
| 69 | // callback in the transfer vector. This should never be called. |
| 70 | abort(); |
| 71 | } |
| 72 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 73 | static ld_plugin_get_input_file get_input_file = nullptr; |
| 74 | static ld_plugin_release_input_file release_input_file = nullptr; |
Rafael Espindola | 176e664 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 75 | static ld_plugin_add_symbols add_symbols = nullptr; |
| 76 | static ld_plugin_get_symbols get_symbols = nullptr; |
| 77 | static ld_plugin_add_input_file add_input_file = nullptr; |
| 78 | static ld_plugin_set_extra_library_path set_extra_library_path = nullptr; |
| 79 | static ld_plugin_get_view get_view = nullptr; |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 80 | static ld_plugin_message message = discard_message; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 81 | static Reloc::Model RelocationModel = Reloc::Default; |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 82 | static std::string output_name = ""; |
| 83 | static std::list<claimed_file> Modules; |
| 84 | static std::vector<std::string> Cleanup; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 85 | static llvm::TargetOptions TargetOpts; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 86 | |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 87 | namespace options { |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 88 | enum OutputType { |
| 89 | OT_NORMAL, |
| 90 | OT_DISABLE, |
| 91 | OT_BC_ONLY, |
| 92 | OT_SAVE_TEMPS |
| 93 | }; |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 94 | static bool generate_api_file = false; |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 95 | static OutputType TheOutputType = OT_NORMAL; |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 96 | static unsigned OptLevel = 2; |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 97 | static unsigned Parallelism = 1; |
Teresa Johnson | 8c8fe5a | 2015-09-16 18:06:45 +0000 | [diff] [blame] | 98 | #ifdef NDEBUG |
| 99 | static bool DisableVerify = true; |
| 100 | #else |
| 101 | static bool DisableVerify = false; |
| 102 | #endif |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 103 | static std::string obj_path; |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 104 | static std::string extra_library_path; |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 105 | static std::string triple; |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 106 | static std::string mcpu; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 107 | // When the thinlto plugin option is specified, only read the function |
| 108 | // the information from intermediate files and write a combined |
| 109 | // global index for the ThinLTO backends. |
| 110 | static bool thinlto = false; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 111 | // Additional options to pass into the code generator. |
Nick Lewycky | 0ac5e22 | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 112 | // Note: This array will contain all plugin options which are not claimed |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 113 | // as plugin exclusive to pass to the code generator. |
Nick Lewycky | 0ac5e22 | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 114 | // For example, "generate-api-file" and "as"options are for the plugin |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 115 | // use only and will not be passed. |
Rafael Espindola | 125b924 | 2014-07-29 19:17:44 +0000 | [diff] [blame] | 116 | static std::vector<const char *> extra; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 117 | |
Nick Lewycky | 7282dd7 | 2015-08-05 21:16:02 +0000 | [diff] [blame] | 118 | static void process_plugin_option(const char *opt_) |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 119 | { |
Rafael Espindola | 176e664 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 120 | if (opt_ == nullptr) |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 121 | return; |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 122 | llvm::StringRef opt = opt_; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 123 | |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 124 | if (opt == "generate-api-file") { |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 125 | generate_api_file = true; |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 126 | } else if (opt.startswith("mcpu=")) { |
| 127 | mcpu = opt.substr(strlen("mcpu=")); |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 128 | } else if (opt.startswith("extra-library-path=")) { |
| 129 | extra_library_path = opt.substr(strlen("extra_library_path=")); |
Rafael Espindola | 148c328 | 2010-08-10 16:32:15 +0000 | [diff] [blame] | 130 | } else if (opt.startswith("mtriple=")) { |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 131 | triple = opt.substr(strlen("mtriple=")); |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 132 | } else if (opt.startswith("obj-path=")) { |
| 133 | obj_path = opt.substr(strlen("obj-path=")); |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 134 | } else if (opt == "emit-llvm") { |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 135 | TheOutputType = OT_BC_ONLY; |
Rafael Espindola | 4a3b6cf | 2014-10-29 23:54:45 +0000 | [diff] [blame] | 136 | } else if (opt == "save-temps") { |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 137 | TheOutputType = OT_SAVE_TEMPS; |
| 138 | } else if (opt == "disable-output") { |
| 139 | TheOutputType = OT_DISABLE; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 140 | } else if (opt == "thinlto") { |
| 141 | thinlto = true; |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 142 | } else if (opt.size() == 2 && opt[0] == 'O') { |
| 143 | if (opt[1] < '0' || opt[1] > '3') |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 144 | message(LDPL_FATAL, "Optimization level must be between 0 and 3"); |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 145 | OptLevel = opt[1] - '0'; |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 146 | } else if (opt.startswith("jobs=")) { |
| 147 | if (StringRef(opt_ + 5).getAsInteger(10, Parallelism)) |
| 148 | message(LDPL_FATAL, "Invalid parallelism level: %s", opt_ + 5); |
Teresa Johnson | 8c8fe5a | 2015-09-16 18:06:45 +0000 | [diff] [blame] | 149 | } else if (opt == "disable-verify") { |
| 150 | DisableVerify = true; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 151 | } else { |
| 152 | // Save this option to pass to the code generator. |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 153 | // ParseCommandLineOptions() expects argv[0] to be program name. Lazily |
| 154 | // add that. |
| 155 | if (extra.empty()) |
| 156 | extra.push_back("LLVMgold"); |
| 157 | |
Rafael Espindola | 125b924 | 2014-07-29 19:17:44 +0000 | [diff] [blame] | 158 | extra.push_back(opt_); |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 163 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 164 | int *claimed); |
| 165 | static ld_plugin_status all_symbols_read_hook(void); |
| 166 | static ld_plugin_status cleanup_hook(void); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 167 | |
| 168 | extern "C" ld_plugin_status onload(ld_plugin_tv *tv); |
| 169 | ld_plugin_status onload(ld_plugin_tv *tv) { |
Peter Collingbourne | 1505c0a | 2014-07-03 23:28:03 +0000 | [diff] [blame] | 170 | InitializeAllTargetInfos(); |
| 171 | InitializeAllTargets(); |
| 172 | InitializeAllTargetMCs(); |
| 173 | InitializeAllAsmParsers(); |
| 174 | InitializeAllAsmPrinters(); |
| 175 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 176 | // We're given a pointer to the first transfer vector. We read through them |
| 177 | // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values |
| 178 | // contain pointers to functions that we need to call to register our own |
| 179 | // hooks. The others are addresses of functions we can use to call into gold |
| 180 | // for services. |
| 181 | |
| 182 | bool registeredClaimFile = false; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 183 | bool RegisteredAllSymbolsRead = false; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 184 | |
| 185 | for (; tv->tv_tag != LDPT_NULL; ++tv) { |
| 186 | switch (tv->tv_tag) { |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 187 | case LDPT_OUTPUT_NAME: |
| 188 | output_name = tv->tv_u.tv_string; |
| 189 | break; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 190 | case LDPT_LINKER_OUTPUT: |
| 191 | switch (tv->tv_u.tv_val) { |
| 192 | case LDPO_REL: // .o |
| 193 | case LDPO_DYN: // .so |
Rafael Espindola | 76e376d | 2014-02-10 20:38:38 +0000 | [diff] [blame] | 194 | case LDPO_PIE: // position independent executable |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 195 | RelocationModel = Reloc::PIC_; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 196 | break; |
| 197 | case LDPO_EXEC: // .exe |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 198 | RelocationModel = Reloc::Static; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 199 | break; |
| 200 | default: |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 201 | message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 202 | return LDPS_ERR; |
| 203 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 204 | break; |
| 205 | case LDPT_OPTION: |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 206 | options::process_plugin_option(tv->tv_u.tv_string); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 207 | break; |
| 208 | case LDPT_REGISTER_CLAIM_FILE_HOOK: { |
| 209 | ld_plugin_register_claim_file callback; |
| 210 | callback = tv->tv_u.tv_register_claim_file; |
| 211 | |
Rafael Espindola | 54f82b7 | 2014-07-30 01:36:32 +0000 | [diff] [blame] | 212 | if (callback(claim_file_hook) != LDPS_OK) |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 213 | return LDPS_ERR; |
| 214 | |
| 215 | registeredClaimFile = true; |
| 216 | } break; |
| 217 | case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: { |
| 218 | ld_plugin_register_all_symbols_read callback; |
| 219 | callback = tv->tv_u.tv_register_all_symbols_read; |
| 220 | |
Rafael Espindola | 54f82b7 | 2014-07-30 01:36:32 +0000 | [diff] [blame] | 221 | if (callback(all_symbols_read_hook) != LDPS_OK) |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 222 | return LDPS_ERR; |
Rafael Espindola | 9ef90d5 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 223 | |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 224 | RegisteredAllSymbolsRead = true; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 225 | } break; |
| 226 | case LDPT_REGISTER_CLEANUP_HOOK: { |
| 227 | ld_plugin_register_cleanup callback; |
| 228 | callback = tv->tv_u.tv_register_cleanup; |
| 229 | |
Rafael Espindola | 54f82b7 | 2014-07-30 01:36:32 +0000 | [diff] [blame] | 230 | if (callback(cleanup_hook) != LDPS_OK) |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 231 | return LDPS_ERR; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 232 | } break; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 233 | case LDPT_GET_INPUT_FILE: |
| 234 | get_input_file = tv->tv_u.tv_get_input_file; |
| 235 | break; |
| 236 | case LDPT_RELEASE_INPUT_FILE: |
| 237 | release_input_file = tv->tv_u.tv_release_input_file; |
| 238 | break; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 239 | case LDPT_ADD_SYMBOLS: |
| 240 | add_symbols = tv->tv_u.tv_add_symbols; |
| 241 | break; |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 242 | case LDPT_GET_SYMBOLS_V2: |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 243 | get_symbols = tv->tv_u.tv_get_symbols; |
| 244 | break; |
| 245 | case LDPT_ADD_INPUT_FILE: |
| 246 | add_input_file = tv->tv_u.tv_add_input_file; |
| 247 | break; |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 248 | case LDPT_SET_EXTRA_LIBRARY_PATH: |
| 249 | set_extra_library_path = tv->tv_u.tv_set_extra_library_path; |
| 250 | break; |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 251 | case LDPT_GET_VIEW: |
| 252 | get_view = tv->tv_u.tv_get_view; |
| 253 | break; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 254 | case LDPT_MESSAGE: |
| 255 | message = tv->tv_u.tv_message; |
| 256 | break; |
| 257 | default: |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
Rafael Espindola | e08484d | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 262 | if (!registeredClaimFile) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 263 | message(LDPL_ERROR, "register_claim_file not passed to LLVMgold."); |
Rafael Espindola | 6add618 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 264 | return LDPS_ERR; |
| 265 | } |
Rafael Espindola | e08484d | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 266 | if (!add_symbols) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 267 | message(LDPL_ERROR, "add_symbols not passed to LLVMgold."); |
Rafael Espindola | 6add618 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 268 | return LDPS_ERR; |
| 269 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 270 | |
Rafael Espindola | a0d30a9 | 2014-06-19 22:20:07 +0000 | [diff] [blame] | 271 | if (!RegisteredAllSymbolsRead) |
| 272 | return LDPS_OK; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 273 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 274 | if (!get_input_file) { |
| 275 | message(LDPL_ERROR, "get_input_file not passed to LLVMgold."); |
| 276 | return LDPS_ERR; |
Rafael Espindola | c273aac | 2014-06-19 22:54:47 +0000 | [diff] [blame] | 277 | } |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 278 | if (!release_input_file) { |
| 279 | message(LDPL_ERROR, "relesase_input_file not passed to LLVMgold."); |
| 280 | return LDPS_ERR; |
Tom Roeder | b508119 | 2014-06-26 20:43:27 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 283 | return LDPS_OK; |
| 284 | } |
| 285 | |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 286 | static const GlobalObject *getBaseObject(const GlobalValue &GV) { |
| 287 | if (auto *GA = dyn_cast<GlobalAlias>(&GV)) |
| 288 | return GA->getBaseObject(); |
| 289 | return cast<GlobalObject>(&GV); |
| 290 | } |
| 291 | |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 292 | static bool shouldSkip(uint32_t Symflags) { |
| 293 | if (!(Symflags & object::BasicSymbolRef::SF_Global)) |
| 294 | return true; |
| 295 | if (Symflags & object::BasicSymbolRef::SF_FormatSpecific) |
| 296 | return true; |
| 297 | return false; |
| 298 | } |
| 299 | |
NAKAMURA Takumi | b13e63c | 2015-11-19 10:43:44 +0000 | [diff] [blame] | 300 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 301 | if (const auto *BDI = dyn_cast<BitcodeDiagnosticInfo>(&DI)) { |
| 302 | std::error_code EC = BDI->getError(); |
| 303 | if (EC == BitcodeError::InvalidBitcodeSignature) |
| 304 | return; |
| 305 | } |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 306 | |
| 307 | std::string ErrStorage; |
| 308 | { |
| 309 | raw_string_ostream OS(ErrStorage); |
| 310 | DiagnosticPrinterRawOStream DP(OS); |
| 311 | DI.print(DP); |
| 312 | } |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 313 | ld_plugin_level Level; |
| 314 | switch (DI.getSeverity()) { |
| 315 | case DS_Error: |
| 316 | message(LDPL_FATAL, "LLVM gold plugin has failed to create LTO module: %s", |
| 317 | ErrStorage.c_str()); |
| 318 | llvm_unreachable("Fatal doesn't return."); |
| 319 | case DS_Warning: |
| 320 | Level = LDPL_WARNING; |
| 321 | break; |
| 322 | case DS_Note: |
Rafael Espindola | f3f1854 | 2015-03-04 18:51:45 +0000 | [diff] [blame] | 323 | case DS_Remark: |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 324 | Level = LDPL_INFO; |
| 325 | break; |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 326 | } |
| 327 | message(Level, "LLVM gold plugin: %s", ErrStorage.c_str()); |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 328 | } |
| 329 | |
NAKAMURA Takumi | b13e63c | 2015-11-19 10:43:44 +0000 | [diff] [blame] | 330 | static void diagnosticHandlerForContext(const DiagnosticInfo &DI, |
| 331 | void *Context) { |
| 332 | diagnosticHandler(DI); |
| 333 | } |
| 334 | |
Rafael Espindola | e54d821 | 2014-07-06 14:31:22 +0000 | [diff] [blame] | 335 | /// Called by gold to see whether this file is one that our plugin can handle. |
| 336 | /// We'll try to open it and register all the symbols with add_symbol if |
| 337 | /// possible. |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 338 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 339 | int *claimed) { |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 340 | LLVMContext Context; |
Rafael Espindola | eeec8e6 | 2014-08-27 20:25:55 +0000 | [diff] [blame] | 341 | MemoryBufferRef BufferRef; |
| 342 | std::unique_ptr<MemoryBuffer> Buffer; |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 343 | if (get_view) { |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 344 | const void *view; |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 345 | if (get_view(file->handle, &view) != LDPS_OK) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 346 | message(LDPL_ERROR, "Failed to get a view of %s", file->name); |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 347 | return LDPS_ERR; |
| 348 | } |
Nick Lewycky | 7282dd7 | 2015-08-05 21:16:02 +0000 | [diff] [blame] | 349 | BufferRef = |
| 350 | MemoryBufferRef(StringRef((const char *)view, file->filesize), ""); |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 351 | } else { |
Ivan Krasin | 639222d | 2011-09-15 23:13:00 +0000 | [diff] [blame] | 352 | int64_t offset = 0; |
Nick Lewycky | 8691c47 | 2009-02-05 04:14:23 +0000 | [diff] [blame] | 353 | // Gold has found what might be IR part-way inside of a file, such as |
| 354 | // an .a archive. |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 355 | if (file->offset) { |
| 356 | offset = file->offset; |
| 357 | } |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 358 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 359 | MemoryBuffer::getOpenFileSlice(file->fd, file->name, file->filesize, |
| 360 | offset); |
| 361 | if (std::error_code EC = BufferOrErr.getError()) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 362 | message(LDPL_ERROR, EC.message().c_str()); |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 363 | return LDPS_ERR; |
| 364 | } |
Rafael Espindola | eeec8e6 | 2014-08-27 20:25:55 +0000 | [diff] [blame] | 365 | Buffer = std::move(BufferOrErr.get()); |
| 366 | BufferRef = Buffer->getMemBufferRef(); |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 367 | } |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 368 | |
NAKAMURA Takumi | b13e63c | 2015-11-19 10:43:44 +0000 | [diff] [blame] | 369 | Context.setDiagnosticHandler(diagnosticHandlerForContext); |
David Blaikie | 10a27df | 2014-09-03 17:59:23 +0000 | [diff] [blame] | 370 | ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr = |
Rafael Espindola | 5dec7ea | 2014-12-09 20:36:13 +0000 | [diff] [blame] | 371 | object::IRObjectFile::create(BufferRef, Context); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 372 | std::error_code EC = ObjOrErr.getError(); |
Rafael Espindola | d0b23be | 2015-01-10 00:07:30 +0000 | [diff] [blame] | 373 | if (EC == object::object_error::invalid_file_type || |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 374 | EC == object::object_error::bitcode_section_not_found) |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 375 | return LDPS_OK; |
| 376 | |
Rafael Espindola | 6c472e5 | 2014-07-29 20:46:19 +0000 | [diff] [blame] | 377 | *claimed = 1; |
| 378 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 379 | if (EC) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 380 | 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] | 381 | EC.message().c_str()); |
Rafael Espindola | 6c472e5 | 2014-07-29 20:46:19 +0000 | [diff] [blame] | 382 | return LDPS_ERR; |
Ivan Krasin | d5f2d8c | 2011-09-09 00:14:04 +0000 | [diff] [blame] | 383 | } |
David Blaikie | 10a27df | 2014-09-03 17:59:23 +0000 | [diff] [blame] | 384 | std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 385 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 386 | Modules.resize(Modules.size() + 1); |
| 387 | claimed_file &cf = Modules.back(); |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 388 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 389 | cf.handle = file->handle; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 390 | |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 391 | // If we are doing ThinLTO compilation, don't need to process the symbols. |
| 392 | // Later we simply build a combined index file after all files are claimed. |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 393 | if (options::thinlto) |
| 394 | return LDPS_OK; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 395 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 396 | for (auto &Sym : Obj->symbols()) { |
| 397 | uint32_t Symflags = Sym.getFlags(); |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 398 | if (shouldSkip(Symflags)) |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 399 | continue; |
| 400 | |
| 401 | cf.syms.push_back(ld_plugin_symbol()); |
| 402 | ld_plugin_symbol &sym = cf.syms.back(); |
Rafael Espindola | 176e664 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 403 | sym.version = nullptr; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 404 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 405 | SmallString<64> Name; |
| 406 | { |
| 407 | raw_svector_ostream OS(Name); |
| 408 | Sym.printName(OS); |
| 409 | } |
| 410 | sym.name = strdup(Name.c_str()); |
| 411 | |
| 412 | const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl()); |
| 413 | |
| 414 | sym.visibility = LDPV_DEFAULT; |
| 415 | if (GV) { |
| 416 | switch (GV->getVisibility()) { |
| 417 | case GlobalValue::DefaultVisibility: |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 418 | sym.visibility = LDPV_DEFAULT; |
| 419 | break; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 420 | case GlobalValue::HiddenVisibility: |
| 421 | sym.visibility = LDPV_HIDDEN; |
| 422 | break; |
| 423 | case GlobalValue::ProtectedVisibility: |
| 424 | sym.visibility = LDPV_PROTECTED; |
| 425 | break; |
| 426 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 429 | if (Symflags & object::BasicSymbolRef::SF_Undefined) { |
| 430 | sym.def = LDPK_UNDEF; |
| 431 | if (GV && GV->hasExternalWeakLinkage()) |
Rafael Espindola | 5654852 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 432 | sym.def = LDPK_WEAKUNDEF; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 433 | } else { |
| 434 | sym.def = LDPK_DEF; |
| 435 | if (GV) { |
| 436 | assert(!GV->hasExternalWeakLinkage() && |
| 437 | !GV->hasAvailableExternallyLinkage() && "Not a declaration!"); |
| 438 | if (GV->hasCommonLinkage()) |
| 439 | sym.def = LDPK_COMMON; |
| 440 | else if (GV->isWeakForLinker()) |
| 441 | sym.def = LDPK_WEAKDEF; |
| 442 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 445 | sym.size = 0; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 446 | sym.comdat_key = nullptr; |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 447 | if (GV) { |
| 448 | const GlobalObject *Base = getBaseObject(*GV); |
| 449 | if (!Base) |
| 450 | message(LDPL_FATAL, "Unable to determine comdat of alias!"); |
| 451 | const Comdat *C = Base->getComdat(); |
| 452 | if (C) |
| 453 | sym.comdat_key = strdup(C->getName().str().c_str()); |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 454 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 455 | |
| 456 | sym.resolution = LDPR_UNKNOWN; |
| 457 | } |
| 458 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 459 | if (!cf.syms.empty()) { |
Nick Lewycky | 7282dd7 | 2015-08-05 21:16:02 +0000 | [diff] [blame] | 460 | 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] | 461 | message(LDPL_ERROR, "Unable to add symbols!"); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 462 | return LDPS_ERR; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | return LDPS_OK; |
| 467 | } |
| 468 | |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 469 | static void keepGlobalValue(GlobalValue &GV, |
| 470 | std::vector<GlobalAlias *> &KeptAliases) { |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 471 | assert(!GV.hasLocalLinkage()); |
| 472 | |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 473 | if (auto *GA = dyn_cast<GlobalAlias>(&GV)) |
| 474 | KeptAliases.push_back(GA); |
| 475 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 476 | switch (GV.getLinkage()) { |
| 477 | default: |
| 478 | break; |
| 479 | case GlobalValue::LinkOnceAnyLinkage: |
| 480 | GV.setLinkage(GlobalValue::WeakAnyLinkage); |
| 481 | break; |
| 482 | case GlobalValue::LinkOnceODRLinkage: |
| 483 | GV.setLinkage(GlobalValue::WeakODRLinkage); |
| 484 | break; |
| 485 | } |
| 486 | |
| 487 | assert(!GV.isDiscardableIfUnused()); |
| 488 | } |
| 489 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 490 | static void internalize(GlobalValue &GV) { |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 491 | if (GV.isDeclarationForLinker()) |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 492 | return; // We get here if there is a matching asm definition. |
| 493 | if (!GV.hasLocalLinkage()) |
| 494 | GV.setLinkage(GlobalValue::InternalLinkage); |
| 495 | } |
| 496 | |
| 497 | static void drop(GlobalValue &GV) { |
| 498 | if (auto *F = dyn_cast<Function>(&GV)) { |
| 499 | F->deleteBody(); |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 500 | F->setComdat(nullptr); // Should deleteBody do this? |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 501 | return; |
| 502 | } |
| 503 | |
| 504 | if (auto *Var = dyn_cast<GlobalVariable>(&GV)) { |
| 505 | Var->setInitializer(nullptr); |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 506 | Var->setLinkage( |
| 507 | GlobalValue::ExternalLinkage); // Should setInitializer do this? |
| 508 | Var->setComdat(nullptr); // and this? |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 509 | return; |
| 510 | } |
| 511 | |
| 512 | auto &Alias = cast<GlobalAlias>(GV); |
| 513 | Module &M = *Alias.getParent(); |
| 514 | PointerType &Ty = *cast<PointerType>(Alias.getType()); |
| 515 | GlobalValue::LinkageTypes L = Alias.getLinkage(); |
| 516 | auto *Var = |
| 517 | new GlobalVariable(M, Ty.getElementType(), /*isConstant*/ false, L, |
| 518 | /*Initializer*/ nullptr); |
| 519 | Var->takeName(&Alias); |
| 520 | Alias.replaceAllUsesWith(Var); |
Rafael Espindola | 71143ed | 2014-09-10 19:39:41 +0000 | [diff] [blame] | 521 | Alias.eraseFromParent(); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | static const char *getResolutionName(ld_plugin_symbol_resolution R) { |
| 525 | switch (R) { |
| 526 | case LDPR_UNKNOWN: |
| 527 | return "UNKNOWN"; |
| 528 | case LDPR_UNDEF: |
| 529 | return "UNDEF"; |
| 530 | case LDPR_PREVAILING_DEF: |
| 531 | return "PREVAILING_DEF"; |
| 532 | case LDPR_PREVAILING_DEF_IRONLY: |
| 533 | return "PREVAILING_DEF_IRONLY"; |
| 534 | case LDPR_PREEMPTED_REG: |
| 535 | return "PREEMPTED_REG"; |
| 536 | case LDPR_PREEMPTED_IR: |
| 537 | return "PREEMPTED_IR"; |
| 538 | case LDPR_RESOLVED_IR: |
| 539 | return "RESOLVED_IR"; |
| 540 | case LDPR_RESOLVED_EXEC: |
| 541 | return "RESOLVED_EXEC"; |
| 542 | case LDPR_RESOLVED_DYN: |
| 543 | return "RESOLVED_DYN"; |
| 544 | case LDPR_PREVAILING_DEF_IRONLY_EXP: |
| 545 | return "PREVAILING_DEF_IRONLY_EXP"; |
| 546 | } |
Rafael Espindola | 2754dbb | 2014-10-10 00:48:13 +0000 | [diff] [blame] | 547 | llvm_unreachable("Unknown resolution"); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 550 | namespace { |
Manuel Klimek | 100f40c | 2015-10-20 08:21:01 +0000 | [diff] [blame] | 551 | class LocalValueMaterializer final : public ValueMaterializer { |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 552 | DenseSet<GlobalValue *> &Dropped; |
Rafael Espindola | 4d47f7f | 2014-12-10 00:09:35 +0000 | [diff] [blame] | 553 | DenseMap<GlobalObject *, GlobalObject *> LocalVersions; |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 554 | |
| 555 | public: |
| 556 | LocalValueMaterializer(DenseSet<GlobalValue *> &Dropped) : Dropped(Dropped) {} |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 557 | Value *materializeDeclFor(Value *V) override; |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 558 | }; |
| 559 | } |
| 560 | |
Rafael Espindola | 19b5238 | 2015-11-27 20:28:19 +0000 | [diff] [blame] | 561 | Value *LocalValueMaterializer::materializeDeclFor(Value *V) { |
Rafael Espindola | 4d47f7f | 2014-12-10 00:09:35 +0000 | [diff] [blame] | 562 | auto *GO = dyn_cast<GlobalObject>(V); |
| 563 | if (!GO) |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 564 | return nullptr; |
Rafael Espindola | 4d47f7f | 2014-12-10 00:09:35 +0000 | [diff] [blame] | 565 | |
| 566 | auto I = LocalVersions.find(GO); |
| 567 | if (I != LocalVersions.end()) |
| 568 | return I->second; |
| 569 | |
| 570 | if (!Dropped.count(GO)) |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 571 | return nullptr; |
Rafael Espindola | 4d47f7f | 2014-12-10 00:09:35 +0000 | [diff] [blame] | 572 | |
| 573 | Module &M = *GO->getParent(); |
| 574 | GlobalValue::LinkageTypes L = GO->getLinkage(); |
| 575 | GlobalObject *Declaration; |
| 576 | if (auto *F = dyn_cast<Function>(GO)) { |
| 577 | Declaration = Function::Create(F->getFunctionType(), L, "", &M); |
| 578 | } else { |
| 579 | auto *Var = cast<GlobalVariable>(GO); |
| 580 | Declaration = new GlobalVariable(M, Var->getType()->getElementType(), |
| 581 | Var->isConstant(), L, |
| 582 | /*Initializer*/ nullptr); |
| 583 | } |
| 584 | Declaration->takeName(GO); |
| 585 | Declaration->copyAttributesFrom(GO); |
| 586 | |
| 587 | GO->setLinkage(GlobalValue::InternalLinkage); |
| 588 | GO->setName(Declaration->getName()); |
| 589 | Dropped.erase(GO); |
| 590 | GO->replaceAllUsesWith(Declaration); |
| 591 | |
| 592 | LocalVersions[Declaration] = GO; |
| 593 | |
| 594 | return GO; |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | static Constant *mapConstantToLocalCopy(Constant *C, ValueToValueMapTy &VM, |
| 598 | LocalValueMaterializer *Materializer) { |
| 599 | return MapValue(C, VM, RF_IgnoreMissingEntries, nullptr, Materializer); |
| 600 | } |
| 601 | |
Rafael Espindola | 538c9a8 | 2014-12-23 18:18:37 +0000 | [diff] [blame] | 602 | static void freeSymName(ld_plugin_symbol &Sym) { |
| 603 | free(Sym.name); |
| 604 | free(Sym.comdat_key); |
| 605 | Sym.name = nullptr; |
| 606 | Sym.comdat_key = nullptr; |
| 607 | } |
| 608 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 609 | static std::unique_ptr<FunctionInfoIndex> |
Mehdi Amini | 0027c1d | 2015-11-19 15:42:34 +0000 | [diff] [blame] | 610 | getFunctionIndexForFile(claimed_file &F, ld_plugin_input_file &Info) { |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 611 | |
| 612 | if (get_symbols(F.handle, F.syms.size(), &F.syms[0]) != LDPS_OK) |
| 613 | message(LDPL_FATAL, "Failed to get symbol information"); |
| 614 | |
| 615 | const void *View; |
| 616 | if (get_view(F.handle, &View) != LDPS_OK) |
| 617 | message(LDPL_FATAL, "Failed to get a view of file"); |
| 618 | |
| 619 | MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize), |
| 620 | Info.name); |
Teresa Johnson | 6290dbc | 2015-11-21 21:55:48 +0000 | [diff] [blame] | 621 | |
| 622 | // Don't bother trying to build an index if there is no summary information |
| 623 | // in this bitcode file. |
| 624 | if (!object::FunctionIndexObjectFile::hasFunctionSummaryInMemBuffer( |
| 625 | BufferRef, diagnosticHandler)) |
| 626 | return std::unique_ptr<FunctionInfoIndex>(nullptr); |
| 627 | |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 628 | ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr = |
NAKAMURA Takumi | b13e63c | 2015-11-19 10:43:44 +0000 | [diff] [blame] | 629 | object::FunctionIndexObjectFile::create(BufferRef, diagnosticHandler); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 630 | |
| 631 | if (std::error_code EC = ObjOrErr.getError()) |
| 632 | message(LDPL_FATAL, "Could not read function index bitcode from file : %s", |
| 633 | EC.message().c_str()); |
| 634 | |
| 635 | object::FunctionIndexObjectFile &Obj = **ObjOrErr; |
| 636 | |
| 637 | return Obj.takeIndex(); |
| 638 | } |
| 639 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 640 | static std::unique_ptr<Module> |
Jan Wen Voung | c11b45a | 2015-02-11 16:12:50 +0000 | [diff] [blame] | 641 | getModuleForFile(LLVMContext &Context, claimed_file &F, |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 642 | ld_plugin_input_file &Info, raw_fd_ostream *ApiFile, |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 643 | StringSet<> &Internalize, StringSet<> &Maybe) { |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 644 | |
Nick Lewycky | 7282dd7 | 2015-08-05 21:16:02 +0000 | [diff] [blame] | 645 | if (get_symbols(F.handle, F.syms.size(), F.syms.data()) != LDPS_OK) |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 646 | message(LDPL_FATAL, "Failed to get symbol information"); |
| 647 | |
| 648 | const void *View; |
| 649 | if (get_view(F.handle, &View) != LDPS_OK) |
| 650 | message(LDPL_FATAL, "Failed to get a view of file"); |
| 651 | |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 652 | MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize), |
| 653 | Info.name); |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 654 | ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr = |
Rafael Espindola | 5dec7ea | 2014-12-09 20:36:13 +0000 | [diff] [blame] | 655 | object::IRObjectFile::create(BufferRef, Context); |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 656 | |
| 657 | if (std::error_code EC = ObjOrErr.getError()) |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 658 | message(LDPL_FATAL, "Could not read bitcode from file : %s", |
| 659 | EC.message().c_str()); |
| 660 | |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 661 | object::IRObjectFile &Obj = **ObjOrErr; |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 662 | |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 663 | Module &M = Obj.getModule(); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 664 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 665 | M.materializeMetadata(); |
| 666 | UpgradeDebugInfo(M); |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 667 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 668 | SmallPtrSet<GlobalValue *, 8> Used; |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 669 | collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 670 | |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 671 | DenseSet<GlobalValue *> Drop; |
| 672 | std::vector<GlobalAlias *> KeptAliases; |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 673 | |
| 674 | unsigned SymNum = 0; |
| 675 | for (auto &ObjSym : Obj.symbols()) { |
| 676 | if (shouldSkip(ObjSym.getFlags())) |
| 677 | continue; |
| 678 | ld_plugin_symbol &Sym = F.syms[SymNum]; |
| 679 | ++SymNum; |
| 680 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 681 | ld_plugin_symbol_resolution Resolution = |
| 682 | (ld_plugin_symbol_resolution)Sym.resolution; |
| 683 | |
| 684 | if (options::generate_api_file) |
| 685 | *ApiFile << Sym.name << ' ' << getResolutionName(Resolution) << '\n'; |
| 686 | |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 687 | GlobalValue *GV = Obj.getSymbolGV(ObjSym.getRawDataRefImpl()); |
Rafael Espindola | 538c9a8 | 2014-12-23 18:18:37 +0000 | [diff] [blame] | 688 | if (!GV) { |
| 689 | freeSymName(Sym); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 690 | continue; // Asm symbol. |
Rafael Espindola | 538c9a8 | 2014-12-23 18:18:37 +0000 | [diff] [blame] | 691 | } |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 692 | |
Rafael Espindola | 51bd8ee | 2014-09-17 20:41:13 +0000 | [diff] [blame] | 693 | if (Resolution != LDPR_PREVAILING_DEF_IRONLY && GV->hasCommonLinkage()) { |
Rafael Espindola | 890db27 | 2014-09-09 20:08:22 +0000 | [diff] [blame] | 694 | // Common linkage is special. There is no single symbol that wins the |
| 695 | // resolution. Instead we have to collect the maximum alignment and size. |
| 696 | // The IR linker does that for us if we just pass it every common GV. |
Rafael Espindola | 51bd8ee | 2014-09-17 20:41:13 +0000 | [diff] [blame] | 697 | // We still have to keep track of LDPR_PREVAILING_DEF_IRONLY so we |
| 698 | // internalize once the IR linker has done its job. |
Rafael Espindola | 538c9a8 | 2014-12-23 18:18:37 +0000 | [diff] [blame] | 699 | freeSymName(Sym); |
Rafael Espindola | 890db27 | 2014-09-09 20:08:22 +0000 | [diff] [blame] | 700 | continue; |
| 701 | } |
| 702 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 703 | switch (Resolution) { |
| 704 | case LDPR_UNKNOWN: |
| 705 | llvm_unreachable("Unexpected resolution"); |
| 706 | |
| 707 | case LDPR_RESOLVED_IR: |
| 708 | case LDPR_RESOLVED_EXEC: |
| 709 | case LDPR_RESOLVED_DYN: |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 710 | assert(GV->isDeclarationForLinker()); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 711 | break; |
| 712 | |
Rafael Espindola | 5ca7fa1 | 2015-01-14 13:53:50 +0000 | [diff] [blame] | 713 | case LDPR_UNDEF: |
Rafael Espindola | 9e3e53f | 2015-01-14 20:08:46 +0000 | [diff] [blame] | 714 | if (!GV->isDeclarationForLinker()) { |
Rafael Espindola | 0fd9e5f | 2015-01-14 19:43:32 +0000 | [diff] [blame] | 715 | assert(GV->hasComdat()); |
| 716 | Drop.insert(GV); |
| 717 | } |
Rafael Espindola | 5ca7fa1 | 2015-01-14 13:53:50 +0000 | [diff] [blame] | 718 | break; |
| 719 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 720 | case LDPR_PREVAILING_DEF_IRONLY: { |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 721 | keepGlobalValue(*GV, KeptAliases); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 722 | if (!Used.count(GV)) { |
| 723 | // Since we use the regular lib/Linker, we cannot just internalize GV |
| 724 | // now or it will not be copied to the merged module. Instead we force |
| 725 | // it to be copied and then internalize it. |
Rafael Espindola | a4f104b | 2014-12-09 16:50:57 +0000 | [diff] [blame] | 726 | Internalize.insert(GV->getName()); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 727 | } |
| 728 | break; |
| 729 | } |
| 730 | |
| 731 | case LDPR_PREVAILING_DEF: |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 732 | keepGlobalValue(*GV, KeptAliases); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 733 | break; |
| 734 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 735 | case LDPR_PREEMPTED_IR: |
Rafael Espindola | dfc7ed7 | 2014-10-07 04:06:13 +0000 | [diff] [blame] | 736 | // Gold might have selected a linkonce_odr and preempted a weak_odr. |
| 737 | // In that case we have to make sure we don't end up internalizing it. |
| 738 | if (!GV->isDiscardableIfUnused()) |
Rafael Espindola | a4f104b | 2014-12-09 16:50:57 +0000 | [diff] [blame] | 739 | Maybe.erase(GV->getName()); |
Rafael Espindola | dfc7ed7 | 2014-10-07 04:06:13 +0000 | [diff] [blame] | 740 | |
| 741 | // fall-through |
| 742 | case LDPR_PREEMPTED_REG: |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 743 | Drop.insert(GV); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 744 | break; |
| 745 | |
| 746 | case LDPR_PREVAILING_DEF_IRONLY_EXP: { |
| 747 | // We can only check for address uses after we merge the modules. The |
| 748 | // reason is that this GV might have a copy in another module |
| 749 | // and in that module the address might be significant, but that |
| 750 | // copy will be LDPR_PREEMPTED_IR. |
| 751 | if (GV->hasLinkOnceODRLinkage()) |
Rafael Espindola | a4f104b | 2014-12-09 16:50:57 +0000 | [diff] [blame] | 752 | Maybe.insert(GV->getName()); |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 753 | keepGlobalValue(*GV, KeptAliases); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 754 | break; |
| 755 | } |
| 756 | } |
| 757 | |
Rafael Espindola | 538c9a8 | 2014-12-23 18:18:37 +0000 | [diff] [blame] | 758 | freeSymName(Sym); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 761 | ValueToValueMapTy VM; |
| 762 | LocalValueMaterializer Materializer(Drop); |
| 763 | for (GlobalAlias *GA : KeptAliases) { |
| 764 | // Gold told us to keep GA. It is possible that a GV usied in the aliasee |
| 765 | // expression is being dropped. If that is the case, that GV must be copied. |
| 766 | Constant *Aliasee = GA->getAliasee(); |
| 767 | Constant *Replacement = mapConstantToLocalCopy(Aliasee, VM, &Materializer); |
Rafael Espindola | 4d47f7f | 2014-12-10 00:09:35 +0000 | [diff] [blame] | 768 | GA->setAliasee(Replacement); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Rafael Espindola | f7ecb11 | 2014-08-22 23:26:10 +0000 | [diff] [blame] | 771 | for (auto *GV : Drop) |
| 772 | drop(*GV); |
| 773 | |
Rafael Espindola | 527e846 | 2014-12-09 16:13:59 +0000 | [diff] [blame] | 774 | return Obj.takeModule(); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 775 | } |
| 776 | |
NAKAMURA Takumi | b0a5283 | 2015-02-02 05:47:30 +0000 | [diff] [blame] | 777 | static void runLTOPasses(Module &M, TargetMachine &TM) { |
Chandler Carruth | df47bb9 | 2015-07-24 17:23:09 +0000 | [diff] [blame] | 778 | M.setDataLayout(TM.createDataLayout()); |
Rafael Espindola | 85d8509 | 2015-02-21 00:13:15 +0000 | [diff] [blame] | 779 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 780 | legacy::PassManager passes; |
NAKAMURA Takumi | b0a5283 | 2015-02-02 05:47:30 +0000 | [diff] [blame] | 781 | passes.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis())); |
| 782 | |
Chandler Carruth | 5700b37 | 2015-02-13 21:10:58 +0000 | [diff] [blame] | 783 | PassManagerBuilder PMB; |
Sylvestre Ledru | 450f97d | 2015-01-24 13:59:08 +0000 | [diff] [blame] | 784 | PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM.getTargetTriple())); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 785 | PMB.Inliner = createFunctionInliningPass(); |
Teresa Johnson | 8c8fe5a | 2015-09-16 18:06:45 +0000 | [diff] [blame] | 786 | // Unconditionally verify input since it is not verified before this |
| 787 | // point and has unknown origin. |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 788 | PMB.VerifyInput = true; |
Teresa Johnson | 8c8fe5a | 2015-09-16 18:06:45 +0000 | [diff] [blame] | 789 | PMB.VerifyOutput = !options::DisableVerify; |
Rafael Espindola | 8391dbd | 2014-10-30 00:11:24 +0000 | [diff] [blame] | 790 | PMB.LoopVectorize = true; |
Rafael Espindola | 919fb53 | 2014-10-30 00:38:54 +0000 | [diff] [blame] | 791 | PMB.SLPVectorize = true; |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 792 | PMB.OptLevel = options::OptLevel; |
Alexey Samsonov | 5ce2448 | 2015-01-30 19:14:04 +0000 | [diff] [blame] | 793 | PMB.populateLTOPassManager(passes); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 794 | passes.run(M); |
| 795 | } |
| 796 | |
Duncan P. N. Exon Smith | e406c84 | 2015-04-15 00:13:51 +0000 | [diff] [blame] | 797 | static void saveBCFile(StringRef Path, Module &M) { |
Rafael Espindola | 4a3b6cf | 2014-10-29 23:54:45 +0000 | [diff] [blame] | 798 | std::error_code EC; |
| 799 | raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None); |
| 800 | if (EC) |
| 801 | message(LDPL_FATAL, "Failed to write the output file."); |
Duncan P. N. Exon Smith | a052ed6 | 2015-04-15 00:10:50 +0000 | [diff] [blame] | 802 | WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true); |
Rafael Espindola | 4a3b6cf | 2014-10-29 23:54:45 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 805 | static void codegen(std::unique_ptr<Module> M) { |
| 806 | const std::string &TripleStr = M->getTargetTriple(); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 807 | Triple TheTriple(TripleStr); |
| 808 | |
| 809 | std::string ErrMsg; |
| 810 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg); |
| 811 | if (!TheTarget) |
| 812 | message(LDPL_FATAL, "Target not found: %s", ErrMsg.c_str()); |
| 813 | |
| 814 | if (unsigned NumOpts = options::extra.size()) |
| 815 | cl::ParseCommandLineOptions(NumOpts, &options::extra[0]); |
| 816 | |
| 817 | SubtargetFeatures Features; |
| 818 | Features.getDefaultSubtargetFeatures(TheTriple); |
| 819 | for (const std::string &A : MAttrs) |
| 820 | Features.AddFeature(A); |
| 821 | |
| 822 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 823 | CodeGenOpt::Level CGOptLevel; |
| 824 | switch (options::OptLevel) { |
| 825 | case 0: |
| 826 | CGOptLevel = CodeGenOpt::None; |
| 827 | break; |
| 828 | case 1: |
| 829 | CGOptLevel = CodeGenOpt::Less; |
| 830 | break; |
| 831 | case 2: |
| 832 | CGOptLevel = CodeGenOpt::Default; |
| 833 | break; |
| 834 | case 3: |
| 835 | CGOptLevel = CodeGenOpt::Aggressive; |
| 836 | break; |
| 837 | } |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 838 | std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine( |
| 839 | TripleStr, options::mcpu, Features.getString(), Options, RelocationModel, |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 840 | CodeModel::Default, CGOptLevel)); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 841 | |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 842 | runLTOPasses(*M, *TM); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 843 | |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 844 | if (options::TheOutputType == options::OT_SAVE_TEMPS) |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 845 | saveBCFile(output_name + ".opt.bc", *M); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 846 | |
| 847 | SmallString<128> Filename; |
Rafael Espindola | 92200d2 | 2015-06-15 13:36:27 +0000 | [diff] [blame] | 848 | if (!options::obj_path.empty()) |
| 849 | Filename = options::obj_path; |
| 850 | else if (options::TheOutputType == options::OT_SAVE_TEMPS) |
| 851 | Filename = output_name + ".o"; |
| 852 | |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 853 | std::vector<SmallString<128>> Filenames(options::Parallelism); |
Rafael Espindola | 92200d2 | 2015-06-15 13:36:27 +0000 | [diff] [blame] | 854 | bool TempOutFile = Filename.empty(); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 855 | { |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 856 | // Open a file descriptor for each backend thread. This is done in a block |
| 857 | // so that the output file descriptors are closed before gold opens them. |
| 858 | std::list<llvm::raw_fd_ostream> OSs; |
| 859 | std::vector<llvm::raw_pwrite_stream *> OSPtrs(options::Parallelism); |
| 860 | for (unsigned I = 0; I != options::Parallelism; ++I) { |
| 861 | int FD; |
| 862 | if (TempOutFile) { |
| 863 | std::error_code EC = |
| 864 | sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filenames[I]); |
| 865 | if (EC) |
| 866 | message(LDPL_FATAL, "Could not create temporary file: %s", |
| 867 | EC.message().c_str()); |
| 868 | } else { |
| 869 | Filenames[I] = Filename; |
| 870 | if (options::Parallelism != 1) |
| 871 | Filenames[I] += utostr(I); |
| 872 | std::error_code EC = |
| 873 | sys::fs::openFileForWrite(Filenames[I], FD, sys::fs::F_None); |
| 874 | if (EC) |
| 875 | message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str()); |
| 876 | } |
| 877 | OSs.emplace_back(FD, true); |
| 878 | OSPtrs[I] = &OSs.back(); |
| 879 | } |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 880 | |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 881 | // Run backend threads. |
| 882 | splitCodeGen(std::move(M), OSPtrs, options::mcpu, Features.getString(), |
| 883 | Options, RelocationModel, CodeModel::Default, CGOptLevel); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 886 | for (auto &Filename : Filenames) { |
| 887 | if (add_input_file(Filename.c_str()) != LDPS_OK) |
| 888 | message(LDPL_FATAL, |
| 889 | "Unable to add .o file to the link. File left behind in: %s", |
| 890 | Filename.c_str()); |
| 891 | if (TempOutFile) |
| 892 | Cleanup.push_back(Filename.c_str()); |
| 893 | } |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Rafael Espindola | b639329 | 2014-07-30 01:23:45 +0000 | [diff] [blame] | 896 | /// gold informs us that all symbols have been read. At this point, we use |
| 897 | /// get_symbols to see if any of our definitions have been overridden by a |
| 898 | /// native object file. Then, perform optimization and codegen. |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 899 | static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) { |
| 900 | if (Modules.empty()) |
| 901 | return LDPS_OK; |
Rafael Espindola | 9ef90d5 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 902 | |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 903 | // If we are doing ThinLTO compilation, simply build the combined |
| 904 | // function index/summary and emit it. We don't need to parse the modules |
| 905 | // and link them in this case. |
| 906 | if (options::thinlto) { |
Teresa Johnson | 8af8d4e | 2015-10-19 15:23:03 +0000 | [diff] [blame] | 907 | FunctionInfoIndex CombinedIndex; |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 908 | uint64_t NextModuleId = 0; |
| 909 | for (claimed_file &F : Modules) { |
| 910 | ld_plugin_input_file File; |
| 911 | if (get_input_file(F.handle, &File) != LDPS_OK) |
| 912 | message(LDPL_FATAL, "Failed to get file information"); |
| 913 | |
| 914 | std::unique_ptr<FunctionInfoIndex> Index = |
Mehdi Amini | 0027c1d | 2015-11-19 15:42:34 +0000 | [diff] [blame] | 915 | getFunctionIndexForFile(F, File); |
Teresa Johnson | 6290dbc | 2015-11-21 21:55:48 +0000 | [diff] [blame] | 916 | |
| 917 | // Skip files without a function summary. |
| 918 | if (!Index) |
| 919 | continue; |
| 920 | |
Teresa Johnson | 8af8d4e | 2015-10-19 15:23:03 +0000 | [diff] [blame] | 921 | CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId); |
Teresa Johnson | db51357 | 2015-12-09 21:11:42 +0000 | [diff] [blame] | 922 | |
| 923 | if (release_input_file(F.handle) != LDPS_OK) |
| 924 | message(LDPL_FATAL, "Failed to release file information"); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | std::error_code EC; |
| 928 | raw_fd_ostream OS(output_name + ".thinlto.bc", EC, |
| 929 | sys::fs::OpenFlags::F_None); |
| 930 | if (EC) |
| 931 | message(LDPL_FATAL, "Unable to open %s.thinlto.bc for writing: %s", |
| 932 | output_name.data(), EC.message().c_str()); |
Teresa Johnson | 3da931f | 2015-10-19 19:06:06 +0000 | [diff] [blame] | 933 | WriteFunctionSummaryToFile(CombinedIndex, OS); |
Teresa Johnson | 403a787 | 2015-10-04 14:33:43 +0000 | [diff] [blame] | 934 | OS.close(); |
| 935 | |
| 936 | cleanup_hook(); |
| 937 | exit(0); |
| 938 | } |
| 939 | |
Teresa Johnson | af9e931 | 2015-12-09 19:49:40 +0000 | [diff] [blame] | 940 | LLVMContext Context; |
| 941 | Context.setDiagnosticHandler(diagnosticHandlerForContext, nullptr, true); |
| 942 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 943 | std::unique_ptr<Module> Combined(new Module("ld-temp.o", Context)); |
Rafael Espindola | f49a38f | 2015-12-04 22:08:53 +0000 | [diff] [blame] | 944 | Linker L(*Combined, diagnosticHandler); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 945 | |
| 946 | std::string DefaultTriple = sys::getDefaultTargetTriple(); |
| 947 | |
| 948 | StringSet<> Internalize; |
| 949 | StringSet<> Maybe; |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 950 | for (claimed_file &F : Modules) { |
Jan Wen Voung | c11b45a | 2015-02-11 16:12:50 +0000 | [diff] [blame] | 951 | ld_plugin_input_file File; |
| 952 | if (get_input_file(F.handle, &File) != LDPS_OK) |
| 953 | message(LDPL_FATAL, "Failed to get file information"); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 954 | std::unique_ptr<Module> M = |
Rafael Espindola | 503f883 | 2015-03-02 19:08:03 +0000 | [diff] [blame] | 955 | getModuleForFile(Context, F, File, ApiFile, Internalize, Maybe); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 956 | if (!options::triple.empty()) |
| 957 | M->setTargetTriple(options::triple.c_str()); |
| 958 | else if (M->getTargetTriple().empty()) { |
| 959 | M->setTargetTriple(DefaultTriple); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 960 | } |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 961 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 962 | if (L.linkInModule(*M)) |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 963 | message(LDPL_FATAL, "Failed to link module"); |
Jan Wen Voung | c11b45a | 2015-02-11 16:12:50 +0000 | [diff] [blame] | 964 | if (release_input_file(F.handle) != LDPS_OK) |
| 965 | message(LDPL_FATAL, "Failed to release file information"); |
Rafael Espindola | 77b6d01 | 2010-06-14 21:20:52 +0000 | [diff] [blame] | 966 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 967 | |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 968 | for (const auto &Name : Internalize) { |
| 969 | GlobalValue *GV = Combined->getNamedValue(Name.first()); |
| 970 | if (GV) |
| 971 | internalize(*GV); |
| 972 | } |
| 973 | |
| 974 | for (const auto &Name : Maybe) { |
| 975 | GlobalValue *GV = Combined->getNamedValue(Name.first()); |
| 976 | if (!GV) |
| 977 | continue; |
| 978 | GV->setLinkage(GlobalValue::LinkOnceODRLinkage); |
| 979 | if (canBeOmittedFromSymbolTable(GV)) |
| 980 | internalize(*GV); |
| 981 | } |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 982 | |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 983 | if (options::TheOutputType == options::OT_DISABLE) |
| 984 | return LDPS_OK; |
| 985 | |
| 986 | if (options::TheOutputType != options::OT_NORMAL) { |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 987 | std::string path; |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 988 | if (options::TheOutputType == options::OT_BC_ONLY) |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 989 | path = output_name; |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 990 | else |
| 991 | path = output_name + ".bc"; |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 992 | saveBCFile(path, *Combined); |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 993 | if (options::TheOutputType == options::OT_BC_ONLY) |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 994 | return LDPS_OK; |
Rafael Espindola | ba3398b | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 995 | } |
Rafael Espindola | 143fc3b | 2013-10-16 12:47:04 +0000 | [diff] [blame] | 996 | |
Peter Collingbourne | 87202a4 | 2015-09-01 20:40:22 +0000 | [diff] [blame] | 997 | codegen(std::move(Combined)); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 998 | |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 999 | if (!options::extra_library_path.empty() && |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 1000 | set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) |
| 1001 | message(LDPL_FATAL, "Unable to set the extra library path."); |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 1002 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 1003 | return LDPS_OK; |
| 1004 | } |
| 1005 | |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 1006 | static ld_plugin_status all_symbols_read_hook(void) { |
| 1007 | ld_plugin_status Ret; |
| 1008 | if (!options::generate_api_file) { |
| 1009 | Ret = allSymbolsReadHook(nullptr); |
| 1010 | } else { |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 1011 | std::error_code EC; |
| 1012 | raw_fd_ostream ApiFile("apifile.txt", EC, sys::fs::F_None); |
| 1013 | if (EC) |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 1014 | message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s", |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 1015 | EC.message().c_str()); |
Rafael Espindola | 33466a7 | 2014-08-21 20:28:55 +0000 | [diff] [blame] | 1016 | Ret = allSymbolsReadHook(&ApiFile); |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Rafael Espindola | 947bdb6 | 2014-11-25 20:52:49 +0000 | [diff] [blame] | 1019 | llvm_shutdown(); |
| 1020 | |
Rafael Espindola | 6953a3a | 2014-11-24 21:18:14 +0000 | [diff] [blame] | 1021 | if (options::TheOutputType == options::OT_BC_ONLY || |
Michael Kuperstein | a07d9b9 | 2015-02-12 18:21:50 +0000 | [diff] [blame] | 1022 | options::TheOutputType == options::OT_DISABLE) { |
| 1023 | if (options::TheOutputType == options::OT_DISABLE) |
| 1024 | // Remove the output file here since ld.bfd creates the output file |
| 1025 | // early. |
| 1026 | sys::fs::remove(output_name); |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 1027 | exit(0); |
Michael Kuperstein | a07d9b9 | 2015-02-12 18:21:50 +0000 | [diff] [blame] | 1028 | } |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame] | 1029 | |
| 1030 | return Ret; |
| 1031 | } |
| 1032 | |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 1033 | static ld_plugin_status cleanup_hook(void) { |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 1034 | for (std::string &Name : Cleanup) { |
| 1035 | std::error_code EC = sys::fs::remove(Name); |
Rafael Espindola | 55ab87f | 2013-06-17 18:38:18 +0000 | [diff] [blame] | 1036 | if (EC) |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 1037 | message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(), |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 1038 | EC.message().c_str()); |
Rafael Espindola | 55ab87f | 2013-06-17 18:38:18 +0000 | [diff] [blame] | 1039 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 1040 | |
| 1041 | return LDPS_OK; |
| 1042 | } |