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