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