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 |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 16 | #include "llvm-c/lto.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringSet.h" |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/CommandFlags.h" |
| 19 | #include "llvm/LTO/LTOCodeGenerator.h" |
| 20 | #include "llvm/LTO/LTOModule.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 22 | #include "llvm/Support/TargetSelect.h" |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 23 | #include <list> |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 24 | #include <plugin-api.h> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 25 | #include <system_error> |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
Sylvestre Ledru | 5399979 | 2014-02-11 17:30:18 +0000 | [diff] [blame] | 28 | #ifndef LDPO_PIE |
| 29 | // FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and |
| 30 | // Precise and Debian Wheezy (binutils 2.23 is required) |
| 31 | # define LDPO_PIE 3 |
| 32 | #endif |
| 33 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
| 36 | namespace { |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 37 | struct claimed_file { |
| 38 | void *handle; |
| 39 | std::vector<ld_plugin_symbol> syms; |
| 40 | }; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 41 | } |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 42 | |
| 43 | static ld_plugin_status discard_message(int level, const char *format, ...) { |
| 44 | // Die loudly. Recent versions of Gold pass ld_plugin_message as the first |
| 45 | // callback in the transfer vector. This should never be called. |
| 46 | abort(); |
| 47 | } |
| 48 | |
Rafael Espindola | 176e664 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 49 | static ld_plugin_add_symbols add_symbols = nullptr; |
| 50 | static ld_plugin_get_symbols get_symbols = nullptr; |
| 51 | static ld_plugin_add_input_file add_input_file = nullptr; |
| 52 | static ld_plugin_set_extra_library_path set_extra_library_path = nullptr; |
| 53 | static ld_plugin_get_view get_view = nullptr; |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 54 | static ld_plugin_message message = discard_message; |
| 55 | static lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC; |
| 56 | static std::string output_name = ""; |
| 57 | static std::list<claimed_file> Modules; |
| 58 | static std::vector<std::string> Cleanup; |
| 59 | static LTOCodeGenerator *CodeGen = nullptr; |
| 60 | static StringSet<> CannotBeHidden; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 61 | static llvm::TargetOptions TargetOpts; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 62 | |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 63 | namespace options { |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 64 | enum generate_bc { BC_NO, BC_ALSO, BC_ONLY }; |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 65 | static bool generate_api_file = false; |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 66 | static generate_bc generate_bc_file = BC_NO; |
Rafael Espindola | ba3398b | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 67 | static std::string bc_path; |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 68 | static std::string obj_path; |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 69 | static std::string extra_library_path; |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 70 | static std::string triple; |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 71 | static std::string mcpu; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 72 | // Additional options to pass into the code generator. |
Nick Lewycky | 0ac5e22 | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 73 | // Note: This array will contain all plugin options which are not claimed |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 74 | // as plugin exclusive to pass to the code generator. |
Nick Lewycky | 0ac5e22 | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 75 | // For example, "generate-api-file" and "as"options are for the plugin |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 76 | // use only and will not be passed. |
Rafael Espindola | 125b924 | 2014-07-29 19:17:44 +0000 | [diff] [blame] | 77 | static std::vector<const char *> extra; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 78 | |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 79 | static void process_plugin_option(const char* opt_) |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 80 | { |
Rafael Espindola | 176e664 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 81 | if (opt_ == nullptr) |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 82 | return; |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 83 | llvm::StringRef opt = opt_; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 84 | |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 85 | if (opt == "generate-api-file") { |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 86 | generate_api_file = true; |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 87 | } else if (opt.startswith("mcpu=")) { |
| 88 | mcpu = opt.substr(strlen("mcpu=")); |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 89 | } else if (opt.startswith("extra-library-path=")) { |
| 90 | extra_library_path = opt.substr(strlen("extra_library_path=")); |
Rafael Espindola | 148c328 | 2010-08-10 16:32:15 +0000 | [diff] [blame] | 91 | } else if (opt.startswith("mtriple=")) { |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 92 | triple = opt.substr(strlen("mtriple=")); |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 93 | } else if (opt.startswith("obj-path=")) { |
| 94 | obj_path = opt.substr(strlen("obj-path=")); |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 95 | } else if (opt == "emit-llvm") { |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 96 | generate_bc_file = BC_ONLY; |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 97 | } else if (opt == "also-emit-llvm") { |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 98 | generate_bc_file = BC_ALSO; |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 99 | } else if (opt.startswith("also-emit-llvm=")) { |
| 100 | llvm::StringRef path = opt.substr(strlen("also-emit-llvm=")); |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 101 | generate_bc_file = BC_ALSO; |
Nick Lewycky | ca4180c | 2010-06-03 17:13:23 +0000 | [diff] [blame] | 102 | if (!bc_path.empty()) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 103 | message(LDPL_WARNING, "Path to the output IL file specified twice. " |
| 104 | "Discarding %s", |
| 105 | opt_); |
Rafael Espindola | ba3398b | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 106 | } else { |
| 107 | bc_path = path; |
| 108 | } |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 109 | } else { |
| 110 | // Save this option to pass to the code generator. |
Rafael Espindola | 125b924 | 2014-07-29 19:17:44 +0000 | [diff] [blame] | 111 | extra.push_back(opt_); |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 116 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 117 | int *claimed); |
| 118 | static ld_plugin_status all_symbols_read_hook(void); |
| 119 | static ld_plugin_status cleanup_hook(void); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 120 | |
| 121 | extern "C" ld_plugin_status onload(ld_plugin_tv *tv); |
| 122 | ld_plugin_status onload(ld_plugin_tv *tv) { |
Peter Collingbourne | 1505c0a | 2014-07-03 23:28:03 +0000 | [diff] [blame] | 123 | InitializeAllTargetInfos(); |
| 124 | InitializeAllTargets(); |
| 125 | InitializeAllTargetMCs(); |
| 126 | InitializeAllAsmParsers(); |
| 127 | InitializeAllAsmPrinters(); |
| 128 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 129 | // We're given a pointer to the first transfer vector. We read through them |
| 130 | // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values |
| 131 | // contain pointers to functions that we need to call to register our own |
| 132 | // hooks. The others are addresses of functions we can use to call into gold |
| 133 | // for services. |
| 134 | |
| 135 | bool registeredClaimFile = false; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 136 | bool RegisteredAllSymbolsRead = false; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 137 | |
| 138 | for (; tv->tv_tag != LDPT_NULL; ++tv) { |
| 139 | switch (tv->tv_tag) { |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 140 | case LDPT_OUTPUT_NAME: |
| 141 | output_name = tv->tv_u.tv_string; |
| 142 | break; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 143 | case LDPT_LINKER_OUTPUT: |
| 144 | switch (tv->tv_u.tv_val) { |
| 145 | case LDPO_REL: // .o |
| 146 | case LDPO_DYN: // .so |
Rafael Espindola | 76e376d | 2014-02-10 20:38:38 +0000 | [diff] [blame] | 147 | case LDPO_PIE: // position independent executable |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 148 | output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC; |
| 149 | break; |
| 150 | case LDPO_EXEC: // .exe |
| 151 | output_type = LTO_CODEGEN_PIC_MODEL_STATIC; |
| 152 | break; |
| 153 | default: |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 154 | 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] | 155 | return LDPS_ERR; |
| 156 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 157 | break; |
| 158 | case LDPT_OPTION: |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 159 | options::process_plugin_option(tv->tv_u.tv_string); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 160 | break; |
| 161 | case LDPT_REGISTER_CLAIM_FILE_HOOK: { |
| 162 | ld_plugin_register_claim_file callback; |
| 163 | callback = tv->tv_u.tv_register_claim_file; |
| 164 | |
Rafael Espindola | 54f82b7 | 2014-07-30 01:36:32 +0000 | [diff] [blame] | 165 | if (callback(claim_file_hook) != LDPS_OK) |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 166 | return LDPS_ERR; |
| 167 | |
| 168 | registeredClaimFile = true; |
| 169 | } break; |
| 170 | case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: { |
| 171 | ld_plugin_register_all_symbols_read callback; |
| 172 | callback = tv->tv_u.tv_register_all_symbols_read; |
| 173 | |
Rafael Espindola | 54f82b7 | 2014-07-30 01:36:32 +0000 | [diff] [blame] | 174 | if (callback(all_symbols_read_hook) != LDPS_OK) |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 175 | return LDPS_ERR; |
Rafael Espindola | 9ef90d5 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 176 | |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 177 | RegisteredAllSymbolsRead = true; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 178 | } break; |
| 179 | case LDPT_REGISTER_CLEANUP_HOOK: { |
| 180 | ld_plugin_register_cleanup callback; |
| 181 | callback = tv->tv_u.tv_register_cleanup; |
| 182 | |
Rafael Espindola | 54f82b7 | 2014-07-30 01:36:32 +0000 | [diff] [blame] | 183 | if (callback(cleanup_hook) != LDPS_OK) |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 184 | return LDPS_ERR; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 185 | } break; |
| 186 | case LDPT_ADD_SYMBOLS: |
| 187 | add_symbols = tv->tv_u.tv_add_symbols; |
| 188 | break; |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 189 | case LDPT_GET_SYMBOLS_V2: |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 190 | get_symbols = tv->tv_u.tv_get_symbols; |
| 191 | break; |
| 192 | case LDPT_ADD_INPUT_FILE: |
| 193 | add_input_file = tv->tv_u.tv_add_input_file; |
| 194 | break; |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 195 | case LDPT_SET_EXTRA_LIBRARY_PATH: |
| 196 | set_extra_library_path = tv->tv_u.tv_set_extra_library_path; |
| 197 | break; |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 198 | case LDPT_GET_VIEW: |
| 199 | get_view = tv->tv_u.tv_get_view; |
| 200 | break; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 201 | case LDPT_MESSAGE: |
| 202 | message = tv->tv_u.tv_message; |
| 203 | break; |
| 204 | default: |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
Rafael Espindola | e08484d | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 209 | if (!registeredClaimFile) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 210 | message(LDPL_ERROR, "register_claim_file not passed to LLVMgold."); |
Rafael Espindola | 6add618 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 211 | return LDPS_ERR; |
| 212 | } |
Rafael Espindola | e08484d | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 213 | if (!add_symbols) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 214 | message(LDPL_ERROR, "add_symbols not passed to LLVMgold."); |
Rafael Espindola | 6add618 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 215 | return LDPS_ERR; |
| 216 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 217 | |
Rafael Espindola | a0d30a9 | 2014-06-19 22:20:07 +0000 | [diff] [blame] | 218 | if (!RegisteredAllSymbolsRead) |
| 219 | return LDPS_OK; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 220 | |
Rafael Espindola | a0d30a9 | 2014-06-19 22:20:07 +0000 | [diff] [blame] | 221 | CodeGen = new LTOCodeGenerator(); |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 222 | |
Rafael Espindola | c273aac | 2014-06-19 22:54:47 +0000 | [diff] [blame] | 223 | // Pass through extra options to the code generator. |
| 224 | if (!options::extra.empty()) { |
Rafael Espindola | 125b924 | 2014-07-29 19:17:44 +0000 | [diff] [blame] | 225 | for (const char *Opt : options::extra) |
| 226 | CodeGen->setCodeGenDebugOptions(Opt); |
Rafael Espindola | c273aac | 2014-06-19 22:54:47 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | CodeGen->parseCodeGenDebugOptions(); |
Tom Roeder | b508119 | 2014-06-26 20:43:27 +0000 | [diff] [blame] | 230 | if (MAttrs.size()) { |
| 231 | std::string Attrs; |
| 232 | for (unsigned I = 0; I < MAttrs.size(); ++I) { |
| 233 | if (I > 0) |
| 234 | Attrs.append(","); |
| 235 | Attrs.append(MAttrs[I]); |
| 236 | } |
| 237 | CodeGen->setAttr(Attrs.c_str()); |
| 238 | } |
| 239 | |
Rafael Espindola | c273aac | 2014-06-19 22:54:47 +0000 | [diff] [blame] | 240 | TargetOpts = InitTargetOptionsFromCodeGenFlags(); |
| 241 | CodeGen->setTargetOptions(TargetOpts); |
| 242 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 243 | return LDPS_OK; |
| 244 | } |
| 245 | |
Rafael Espindola | e54d821 | 2014-07-06 14:31:22 +0000 | [diff] [blame] | 246 | /// Called by gold to see whether this file is one that our plugin can handle. |
| 247 | /// We'll try to open it and register all the symbols with add_symbol if |
| 248 | /// possible. |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 249 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 250 | int *claimed) { |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 251 | const void *view; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 252 | std::unique_ptr<MemoryBuffer> buffer; |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 253 | if (get_view) { |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 254 | if (get_view(file->handle, &view) != LDPS_OK) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 255 | message(LDPL_ERROR, "Failed to get a view of %s", file->name); |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 256 | return LDPS_ERR; |
| 257 | } |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 258 | } else { |
Ivan Krasin | 639222d | 2011-09-15 23:13:00 +0000 | [diff] [blame] | 259 | int64_t offset = 0; |
Nick Lewycky | 8691c47 | 2009-02-05 04:14:23 +0000 | [diff] [blame] | 260 | // Gold has found what might be IR part-way inside of a file, such as |
| 261 | // an .a archive. |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 262 | if (file->offset) { |
| 263 | offset = file->offset; |
| 264 | } |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 265 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 266 | MemoryBuffer::getOpenFileSlice(file->fd, file->name, file->filesize, |
| 267 | offset); |
| 268 | if (std::error_code EC = BufferOrErr.getError()) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 269 | message(LDPL_ERROR, EC.message().c_str()); |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 270 | return LDPS_ERR; |
| 271 | } |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 272 | buffer = std::move(BufferOrErr.get()); |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 273 | view = buffer->getBufferStart(); |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 274 | } |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 275 | |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 276 | if (!LTOModule::isBitcodeFile(view, file->filesize)) |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 277 | return LDPS_OK; |
| 278 | |
Rafael Espindola | 6c472e5 | 2014-07-29 20:46:19 +0000 | [diff] [blame] | 279 | *claimed = 1; |
| 280 | |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 281 | std::string Error; |
Rafael Espindola | e54d821 | 2014-07-06 14:31:22 +0000 | [diff] [blame] | 282 | LTOModule *M = |
| 283 | LTOModule::createFromBuffer(view, file->filesize, TargetOpts, Error); |
Ivan Krasin | d5f2d8c | 2011-09-09 00:14:04 +0000 | [diff] [blame] | 284 | if (!M) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 285 | message(LDPL_ERROR, "LLVM gold plugin has failed to create LTO module: %s", |
| 286 | Error.c_str()); |
Rafael Espindola | 6c472e5 | 2014-07-29 20:46:19 +0000 | [diff] [blame] | 287 | return LDPS_ERR; |
Ivan Krasin | d5f2d8c | 2011-09-09 00:14:04 +0000 | [diff] [blame] | 288 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 289 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 290 | Modules.resize(Modules.size() + 1); |
| 291 | claimed_file &cf = Modules.back(); |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 292 | |
| 293 | if (!options::triple.empty()) |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 294 | M->setTargetTriple(options::triple.c_str()); |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 295 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 296 | cf.handle = file->handle; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 297 | unsigned sym_count = M->getSymbolCount(); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 298 | cf.syms.reserve(sym_count); |
| 299 | |
| 300 | for (unsigned i = 0; i != sym_count; ++i) { |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 301 | lto_symbol_attributes attrs = M->getSymbolAttributes(i); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 302 | if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL) |
| 303 | continue; |
| 304 | |
| 305 | cf.syms.push_back(ld_plugin_symbol()); |
| 306 | ld_plugin_symbol &sym = cf.syms.back(); |
Rafael Espindola | b201bfc | 2014-06-19 22:33:23 +0000 | [diff] [blame] | 307 | sym.name = strdup(M->getSymbolName(i)); |
Rafael Espindola | 176e664 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 308 | sym.version = nullptr; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 309 | |
| 310 | int scope = attrs & LTO_SYMBOL_SCOPE_MASK; |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 311 | bool CanBeHidden = scope == LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN; |
| 312 | if (!CanBeHidden) |
| 313 | CannotBeHidden.insert(sym.name); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 314 | switch (scope) { |
| 315 | case LTO_SYMBOL_SCOPE_HIDDEN: |
| 316 | sym.visibility = LDPV_HIDDEN; |
| 317 | break; |
| 318 | case LTO_SYMBOL_SCOPE_PROTECTED: |
| 319 | sym.visibility = LDPV_PROTECTED; |
| 320 | break; |
| 321 | case 0: // extern |
| 322 | case LTO_SYMBOL_SCOPE_DEFAULT: |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 323 | case LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN: |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 324 | sym.visibility = LDPV_DEFAULT; |
| 325 | break; |
| 326 | default: |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 327 | message(LDPL_ERROR, "Unknown scope attribute: %d", scope); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 328 | return LDPS_ERR; |
| 329 | } |
| 330 | |
| 331 | int definition = attrs & LTO_SYMBOL_DEFINITION_MASK; |
Rafael Espindola | 176e664 | 2014-07-29 21:46:05 +0000 | [diff] [blame] | 332 | sym.comdat_key = nullptr; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 333 | switch (definition) { |
| 334 | case LTO_SYMBOL_DEFINITION_REGULAR: |
| 335 | sym.def = LDPK_DEF; |
| 336 | break; |
| 337 | case LTO_SYMBOL_DEFINITION_UNDEFINED: |
| 338 | sym.def = LDPK_UNDEF; |
| 339 | break; |
| 340 | case LTO_SYMBOL_DEFINITION_TENTATIVE: |
| 341 | sym.def = LDPK_COMMON; |
| 342 | break; |
| 343 | case LTO_SYMBOL_DEFINITION_WEAK: |
Rafael Espindola | 70d8015 | 2011-02-14 22:23:49 +0000 | [diff] [blame] | 344 | sym.comdat_key = sym.name; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 345 | sym.def = LDPK_WEAKDEF; |
| 346 | break; |
Rafael Espindola | 5654852 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 347 | case LTO_SYMBOL_DEFINITION_WEAKUNDEF: |
| 348 | sym.def = LDPK_WEAKUNDEF; |
| 349 | break; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 350 | default: |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 351 | message(LDPL_ERROR, "Unknown definition attribute: %d", definition); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 352 | return LDPS_ERR; |
| 353 | } |
| 354 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 355 | sym.size = 0; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 356 | |
| 357 | sym.resolution = LDPR_UNKNOWN; |
| 358 | } |
| 359 | |
| 360 | cf.syms.reserve(cf.syms.size()); |
| 361 | |
| 362 | if (!cf.syms.empty()) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 363 | if (add_symbols(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) { |
| 364 | message(LDPL_ERROR, "Unable to add symbols!"); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 365 | return LDPS_ERR; |
| 366 | } |
| 367 | } |
| 368 | |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 369 | if (CodeGen) { |
| 370 | std::string Error; |
| 371 | if (!CodeGen->addModule(M, Error)) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 372 | message(LDPL_ERROR, "Error linking module: %s", Error.c_str()); |
Rafael Espindola | d578b69 | 2013-10-18 19:32:06 +0000 | [diff] [blame] | 373 | return LDPS_ERR; |
| 374 | } |
| 375 | } |
Rafael Espindola | 9ef90d5 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 376 | |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 377 | delete M; |
Rafael Espindola | 9ef90d5 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 378 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 379 | return LDPS_OK; |
| 380 | } |
| 381 | |
Rafael Espindola | 0c54419 | 2014-07-31 19:32:04 +0000 | [diff] [blame] | 382 | static bool mustPreserve(ld_plugin_symbol &Sym) { |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 383 | if (Sym.resolution == LDPR_PREVAILING_DEF) |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 384 | return true; |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 385 | if (Sym.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP) |
| 386 | return CannotBeHidden.count(Sym.name); |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 387 | return false; |
| 388 | } |
| 389 | |
Rafael Espindola | b639329 | 2014-07-30 01:23:45 +0000 | [diff] [blame] | 390 | /// gold informs us that all symbols have been read. At this point, we use |
| 391 | /// get_symbols to see if any of our definitions have been overridden by a |
| 392 | /// native object file. Then, perform optimization and codegen. |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame^] | 393 | static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *apiFile) { |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 394 | assert(CodeGen); |
Rafael Espindola | 9ef90d5 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 395 | |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 396 | for (claimed_file &F : Modules) { |
| 397 | if (F.syms.empty()) |
Nick Lewycky | 1fcd0f1 | 2011-07-26 08:40:36 +0000 | [diff] [blame] | 398 | continue; |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 399 | get_symbols(F.handle, F.syms.size(), &F.syms[0]); |
| 400 | for (ld_plugin_symbol &Sym : F.syms) { |
Rafael Espindola | 0c54419 | 2014-07-31 19:32:04 +0000 | [diff] [blame] | 401 | if (mustPreserve(Sym)) { |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 402 | CodeGen->addMustPreserveSymbol(Sym.name); |
Nick Lewycky | 338d07e | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 403 | |
Rafael Espindola | 0012182 | 2010-06-03 14:45:44 +0000 | [diff] [blame] | 404 | if (options::generate_api_file) |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame^] | 405 | (*apiFile) << Sym.name << "\n"; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
Rafael Espindola | 77b6d01 | 2010-06-14 21:20:52 +0000 | [diff] [blame] | 408 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 409 | |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 410 | CodeGen->setCodePICModel(output_type); |
| 411 | CodeGen->setDebugInfo(LTO_DEBUG_MODEL_DWARF); |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 412 | if (!options::mcpu.empty()) |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 413 | CodeGen->setCpu(options::mcpu.c_str()); |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 414 | |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 415 | if (options::generate_bc_file != options::BC_NO) { |
| 416 | std::string path; |
| 417 | if (options::generate_bc_file == options::BC_ONLY) |
| 418 | path = output_name; |
| 419 | else if (!options::bc_path.empty()) |
| 420 | path = options::bc_path; |
| 421 | else |
| 422 | path = output_name + ".bc"; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 423 | std::string Error; |
| 424 | if (!CodeGen->writeMergedModules(path.c_str(), Error)) |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 425 | message(LDPL_FATAL, "Failed to write the output file."); |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame^] | 426 | if (options::generate_bc_file == options::BC_ONLY) |
| 427 | return LDPS_OK; |
Rafael Espindola | ba3398b | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 428 | } |
Rafael Espindola | 143fc3b | 2013-10-16 12:47:04 +0000 | [diff] [blame] | 429 | |
| 430 | std::string ObjPath; |
| 431 | { |
| 432 | const char *Temp; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 433 | std::string Error; |
| 434 | if (!CodeGen->compile_to_file(&Temp, /*DisableOpt*/ false, /*DisableInline*/ |
| 435 | false, /*DisableGVNLoadPRE*/ false, Error)) |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 436 | message(LDPL_ERROR, "Could not produce a combined object file\n"); |
Rafael Espindola | 143fc3b | 2013-10-16 12:47:04 +0000 | [diff] [blame] | 437 | ObjPath = Temp; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 438 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 439 | |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 440 | for (claimed_file &F : Modules) { |
| 441 | for (ld_plugin_symbol &Sym : F.syms) |
| 442 | free(Sym.name); |
Rafael Espindola | 9ef90d5 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 443 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 444 | |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 445 | if (add_input_file(ObjPath.c_str()) != LDPS_OK) { |
| 446 | message(LDPL_ERROR, "Unable to add .o file to the link."); |
| 447 | message(LDPL_ERROR, "File left behind in: %s", ObjPath.c_str()); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 448 | return LDPS_ERR; |
| 449 | } |
| 450 | |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 451 | if (!options::extra_library_path.empty() && |
| 452 | set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) { |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 453 | message(LDPL_ERROR, "Unable to set the extra library path."); |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 454 | return LDPS_ERR; |
| 455 | } |
| 456 | |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 457 | if (options::obj_path.empty()) |
Rafael Espindola | 143fc3b | 2013-10-16 12:47:04 +0000 | [diff] [blame] | 458 | Cleanup.push_back(ObjPath); |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 459 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 460 | return LDPS_OK; |
| 461 | } |
| 462 | |
Rafael Espindola | 55b3254 | 2014-08-11 19:06:54 +0000 | [diff] [blame^] | 463 | static ld_plugin_status all_symbols_read_hook(void) { |
| 464 | ld_plugin_status Ret; |
| 465 | if (!options::generate_api_file) { |
| 466 | Ret = allSymbolsReadHook(nullptr); |
| 467 | } else { |
| 468 | std::string Error; |
| 469 | raw_fd_ostream apiFile("apifile.txt", Error, sys::fs::F_None); |
| 470 | if (!Error.empty()) |
| 471 | message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s", |
| 472 | Error.c_str()); |
| 473 | Ret = allSymbolsReadHook(&apiFile); |
| 474 | } |
| 475 | |
| 476 | delete CodeGen; |
| 477 | |
| 478 | if (options::generate_bc_file == options::BC_ONLY) |
| 479 | exit(0); |
| 480 | |
| 481 | return Ret; |
| 482 | } |
| 483 | |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 484 | static ld_plugin_status cleanup_hook(void) { |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 485 | for (std::string &Name : Cleanup) { |
| 486 | std::error_code EC = sys::fs::remove(Name); |
Rafael Espindola | 55ab87f | 2013-06-17 18:38:18 +0000 | [diff] [blame] | 487 | if (EC) |
Rafael Espindola | d2aac57 | 2014-07-30 01:52:40 +0000 | [diff] [blame] | 488 | message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(), |
Rafael Espindola | 5ad21fa | 2014-07-30 00:38:58 +0000 | [diff] [blame] | 489 | EC.message().c_str()); |
Rafael Espindola | 55ab87f | 2013-06-17 18:38:18 +0000 | [diff] [blame] | 490 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 491 | |
| 492 | return LDPS_OK; |
| 493 | } |