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