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