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