Nick Lewycky | 3e62b2d | 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 | |
Duncan Sands | 09b5d90 | 2009-10-22 16:03:32 +0000 | [diff] [blame] | 15 | #include "llvm/Config/config.h" |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 16 | #include "plugin-api.h" |
| 17 | |
| 18 | #include "llvm-c/lto.h" |
| 19 | |
Dan Gohman | e4f1a9b | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ToolOutputFile.h" |
Michael J. Spencer | 3cc52ea | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Errno.h" |
| 22 | #include "llvm/Support/Path.h" |
| 23 | #include "llvm/Support/Program.h" |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 24 | |
Torok Edwin | 6cbbdfd | 2009-02-04 21:00:02 +0000 | [diff] [blame] | 25 | #include <cerrno> |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 26 | #include <cstdlib> |
| 27 | #include <cstring> |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 28 | #include <fstream> |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 29 | #include <list> |
| 30 | #include <vector> |
| 31 | |
Michael J. Spencer | 8b1659e | 2011-01-20 05:43:16 +0000 | [diff] [blame] | 32 | // Support Windows/MinGW crazyness. |
| 33 | #ifdef _WIN32 |
| 34 | # include <io.h> |
| 35 | # define lseek _lseek |
| 36 | # define read _read |
| 37 | #endif |
| 38 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
| 41 | namespace { |
| 42 | ld_plugin_status discard_message(int level, const char *format, ...) { |
| 43 | // Die loudly. Recent versions of Gold pass ld_plugin_message as the first |
| 44 | // callback in the transfer vector. This should never be called. |
| 45 | abort(); |
| 46 | } |
| 47 | |
| 48 | ld_plugin_add_symbols add_symbols = NULL; |
| 49 | ld_plugin_get_symbols get_symbols = NULL; |
| 50 | ld_plugin_add_input_file add_input_file = NULL; |
Rafael Espindola | dd76f18 | 2010-06-18 19:18:58 +0000 | [diff] [blame] | 51 | ld_plugin_add_input_library add_input_library = NULL; |
Rafael Espindola | 11f403c | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 52 | ld_plugin_set_extra_library_path set_extra_library_path = NULL; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 53 | ld_plugin_message message = discard_message; |
| 54 | |
| 55 | int api_version = 0; |
| 56 | int gold_version = 0; |
| 57 | |
| 58 | struct claimed_file { |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 59 | void *handle; |
| 60 | std::vector<ld_plugin_symbol> syms; |
| 61 | }; |
| 62 | |
| 63 | lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC; |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 64 | std::string output_name = ""; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 65 | std::list<claimed_file> Modules; |
| 66 | std::vector<sys::Path> Cleanup; |
Rafael Espindola | 8e04fc3 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 67 | lto_code_gen_t code_gen = NULL; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 70 | namespace options { |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 71 | enum generate_bc { BC_NO, BC_ALSO, BC_ONLY }; |
Dan Gohman | f692003 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 72 | static bool generate_api_file = false; |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 73 | static generate_bc generate_bc_file = BC_NO; |
Rafael Espindola | 62bacd6 | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 74 | static std::string bc_path; |
Rafael Espindola | 5a287d7 | 2011-02-16 11:19:44 +0000 | [diff] [blame] | 75 | static std::string obj_path; |
Rafael Espindola | 11f403c | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 76 | static std::string extra_library_path; |
Rafael Espindola | cbb170d | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 77 | static std::string triple; |
Rafael Espindola | 2d643ef | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 78 | static std::string mcpu; |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 79 | // Additional options to pass into the code generator. |
Nick Lewycky | fc55def | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 80 | // Note: This array will contain all plugin options which are not claimed |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 81 | // as plugin exclusive to pass to the code generator. |
Nick Lewycky | fc55def | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 82 | // For example, "generate-api-file" and "as"options are for the plugin |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 83 | // use only and will not be passed. |
Dan Gohman | f692003 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 84 | static std::vector<std::string> extra; |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 85 | |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 86 | static void process_plugin_option(const char* opt_) |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 87 | { |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 88 | if (opt_ == NULL) |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 89 | return; |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 90 | llvm::StringRef opt = opt_; |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 91 | |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 92 | if (opt == "generate-api-file") { |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 93 | generate_api_file = true; |
Rafael Espindola | 2d643ef | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 94 | } else if (opt.startswith("mcpu=")) { |
| 95 | mcpu = opt.substr(strlen("mcpu=")); |
Rafael Espindola | 11f403c | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 96 | } else if (opt.startswith("extra-library-path=")) { |
| 97 | extra_library_path = opt.substr(strlen("extra_library_path=")); |
Rafael Espindola | 15af387 | 2010-08-10 16:32:15 +0000 | [diff] [blame] | 98 | } else if (opt.startswith("mtriple=")) { |
Rafael Espindola | cbb170d | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 99 | triple = opt.substr(strlen("mtriple=")); |
Rafael Espindola | 5a287d7 | 2011-02-16 11:19:44 +0000 | [diff] [blame] | 100 | } else if (opt.startswith("obj-path=")) { |
| 101 | obj_path = opt.substr(strlen("obj-path=")); |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 102 | } else if (opt == "emit-llvm") { |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 103 | generate_bc_file = BC_ONLY; |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 104 | } else if (opt == "also-emit-llvm") { |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 105 | generate_bc_file = BC_ALSO; |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 106 | } else if (opt.startswith("also-emit-llvm=")) { |
| 107 | llvm::StringRef path = opt.substr(strlen("also-emit-llvm=")); |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 108 | generate_bc_file = BC_ALSO; |
Nick Lewycky | 662f738 | 2010-06-03 17:13:23 +0000 | [diff] [blame] | 109 | if (!bc_path.empty()) { |
Rafael Espindola | 62bacd6 | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 110 | (*message)(LDPL_WARNING, "Path to the output IL file specified twice. " |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 111 | "Discarding %s", opt_); |
Rafael Espindola | 62bacd6 | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 112 | } else { |
| 113 | bc_path = path; |
| 114 | } |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 115 | } else { |
| 116 | // Save this option to pass to the code generator. |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 117 | extra.push_back(opt); |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
Dan Gohman | f692003 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 122 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 123 | int *claimed); |
| 124 | static ld_plugin_status all_symbols_read_hook(void); |
| 125 | static ld_plugin_status cleanup_hook(void); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 126 | |
| 127 | extern "C" ld_plugin_status onload(ld_plugin_tv *tv); |
| 128 | ld_plugin_status onload(ld_plugin_tv *tv) { |
| 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; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 136 | |
| 137 | for (; tv->tv_tag != LDPT_NULL; ++tv) { |
| 138 | switch (tv->tv_tag) { |
| 139 | case LDPT_API_VERSION: |
| 140 | api_version = tv->tv_u.tv_val; |
| 141 | break; |
| 142 | case LDPT_GOLD_VERSION: // major * 100 + minor |
| 143 | gold_version = tv->tv_u.tv_val; |
| 144 | break; |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 145 | case LDPT_OUTPUT_NAME: |
| 146 | output_name = tv->tv_u.tv_string; |
| 147 | break; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 148 | case LDPT_LINKER_OUTPUT: |
| 149 | switch (tv->tv_u.tv_val) { |
| 150 | case LDPO_REL: // .o |
| 151 | case LDPO_DYN: // .so |
| 152 | output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC; |
| 153 | break; |
| 154 | case LDPO_EXEC: // .exe |
| 155 | output_type = LTO_CODEGEN_PIC_MODEL_STATIC; |
| 156 | break; |
| 157 | default: |
| 158 | (*message)(LDPL_ERROR, "Unknown output file type %d", |
| 159 | tv->tv_u.tv_val); |
| 160 | return LDPS_ERR; |
| 161 | } |
| 162 | // TODO: add an option to disable PIC. |
| 163 | //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC; |
| 164 | break; |
| 165 | case LDPT_OPTION: |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 166 | options::process_plugin_option(tv->tv_u.tv_string); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 167 | break; |
| 168 | case LDPT_REGISTER_CLAIM_FILE_HOOK: { |
| 169 | ld_plugin_register_claim_file callback; |
| 170 | callback = tv->tv_u.tv_register_claim_file; |
| 171 | |
| 172 | if ((*callback)(claim_file_hook) != LDPS_OK) |
| 173 | return LDPS_ERR; |
| 174 | |
| 175 | registeredClaimFile = true; |
| 176 | } break; |
| 177 | case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: { |
| 178 | ld_plugin_register_all_symbols_read callback; |
| 179 | callback = tv->tv_u.tv_register_all_symbols_read; |
| 180 | |
| 181 | if ((*callback)(all_symbols_read_hook) != LDPS_OK) |
| 182 | return LDPS_ERR; |
Rafael Espindola | 8e04fc3 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 183 | |
| 184 | code_gen = lto_codegen_create(); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 185 | } break; |
| 186 | case LDPT_REGISTER_CLEANUP_HOOK: { |
| 187 | ld_plugin_register_cleanup callback; |
| 188 | callback = tv->tv_u.tv_register_cleanup; |
| 189 | |
| 190 | if ((*callback)(cleanup_hook) != LDPS_OK) |
| 191 | return LDPS_ERR; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 192 | } break; |
| 193 | case LDPT_ADD_SYMBOLS: |
| 194 | add_symbols = tv->tv_u.tv_add_symbols; |
| 195 | break; |
| 196 | case LDPT_GET_SYMBOLS: |
| 197 | get_symbols = tv->tv_u.tv_get_symbols; |
| 198 | break; |
| 199 | case LDPT_ADD_INPUT_FILE: |
| 200 | add_input_file = tv->tv_u.tv_add_input_file; |
| 201 | break; |
Rafael Espindola | dd76f18 | 2010-06-18 19:18:58 +0000 | [diff] [blame] | 202 | case LDPT_ADD_INPUT_LIBRARY: |
| 203 | add_input_library = tv->tv_u.tv_add_input_file; |
| 204 | break; |
Rafael Espindola | 11f403c | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 205 | case LDPT_SET_EXTRA_LIBRARY_PATH: |
| 206 | set_extra_library_path = tv->tv_u.tv_set_extra_library_path; |
| 207 | break; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 208 | case LDPT_MESSAGE: |
| 209 | message = tv->tv_u.tv_message; |
| 210 | break; |
| 211 | default: |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | |
Rafael Espindola | 98c507e | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 216 | if (!registeredClaimFile) { |
Rafael Espindola | 6210a94 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 217 | (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold."); |
| 218 | return LDPS_ERR; |
| 219 | } |
Rafael Espindola | 98c507e | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 220 | if (!add_symbols) { |
Rafael Espindola | 6210a94 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 221 | (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold."); |
| 222 | return LDPS_ERR; |
| 223 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 224 | |
| 225 | return LDPS_OK; |
| 226 | } |
| 227 | |
| 228 | /// claim_file_hook - called by gold to see whether this file is one that |
| 229 | /// our plugin can handle. We'll try to open it and register all the symbols |
| 230 | /// with add_symbol if possible. |
Dan Gohman | f692003 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 231 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 232 | int *claimed) { |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 233 | lto_module_t M; |
| 234 | |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 235 | if (file->offset) { |
Nick Lewycky | c1da886 | 2009-02-05 04:14:23 +0000 | [diff] [blame] | 236 | // Gold has found what might be IR part-way inside of a file, such as |
| 237 | // an .a archive. |
Rafael Espindola | f21b105 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 238 | M = lto_module_create_from_fd_at_offset(file->fd, file->name, -1, |
| 239 | file->filesize, file->offset); |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 240 | } else { |
Rafael Espindola | 35358e0 | 2011-02-27 20:15:37 +0000 | [diff] [blame] | 241 | M = lto_module_create_from_fd(file->fd, file->name, file->filesize); |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 242 | } |
Rafael Espindola | f21b105 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 243 | if (!M) |
| 244 | return LDPS_OK; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 245 | |
| 246 | *claimed = 1; |
| 247 | Modules.resize(Modules.size() + 1); |
| 248 | claimed_file &cf = Modules.back(); |
Rafael Espindola | cbb170d | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 249 | |
| 250 | if (!options::triple.empty()) |
Rafael Espindola | 37d42f8 | 2011-02-19 21:49:57 +0000 | [diff] [blame] | 251 | lto_module_set_target_triple(M, options::triple.c_str()); |
Rafael Espindola | cbb170d | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 252 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 253 | cf.handle = file->handle; |
Rafael Espindola | 37d42f8 | 2011-02-19 21:49:57 +0000 | [diff] [blame] | 254 | unsigned sym_count = lto_module_get_num_symbols(M); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 255 | cf.syms.reserve(sym_count); |
| 256 | |
| 257 | for (unsigned i = 0; i != sym_count; ++i) { |
Rafael Espindola | 37d42f8 | 2011-02-19 21:49:57 +0000 | [diff] [blame] | 258 | lto_symbol_attributes attrs = lto_module_get_symbol_attribute(M, i); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 259 | if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL) |
| 260 | continue; |
| 261 | |
| 262 | cf.syms.push_back(ld_plugin_symbol()); |
| 263 | ld_plugin_symbol &sym = cf.syms.back(); |
Rafael Espindola | 37d42f8 | 2011-02-19 21:49:57 +0000 | [diff] [blame] | 264 | sym.name = const_cast<char *>(lto_module_get_symbol_name(M, i)); |
Rafael Espindola | 8e04fc3 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 265 | sym.name = strdup(sym.name); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 266 | sym.version = NULL; |
| 267 | |
| 268 | int scope = attrs & LTO_SYMBOL_SCOPE_MASK; |
| 269 | switch (scope) { |
| 270 | case LTO_SYMBOL_SCOPE_HIDDEN: |
| 271 | sym.visibility = LDPV_HIDDEN; |
| 272 | break; |
| 273 | case LTO_SYMBOL_SCOPE_PROTECTED: |
| 274 | sym.visibility = LDPV_PROTECTED; |
| 275 | break; |
| 276 | case 0: // extern |
| 277 | case LTO_SYMBOL_SCOPE_DEFAULT: |
| 278 | sym.visibility = LDPV_DEFAULT; |
| 279 | break; |
| 280 | default: |
| 281 | (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope); |
| 282 | return LDPS_ERR; |
| 283 | } |
| 284 | |
| 285 | int definition = attrs & LTO_SYMBOL_DEFINITION_MASK; |
Rafael Espindola | 5d618ef | 2011-02-14 22:23:49 +0000 | [diff] [blame] | 286 | sym.comdat_key = NULL; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 287 | switch (definition) { |
| 288 | case LTO_SYMBOL_DEFINITION_REGULAR: |
| 289 | sym.def = LDPK_DEF; |
| 290 | break; |
| 291 | case LTO_SYMBOL_DEFINITION_UNDEFINED: |
| 292 | sym.def = LDPK_UNDEF; |
| 293 | break; |
| 294 | case LTO_SYMBOL_DEFINITION_TENTATIVE: |
| 295 | sym.def = LDPK_COMMON; |
| 296 | break; |
| 297 | case LTO_SYMBOL_DEFINITION_WEAK: |
Rafael Espindola | 5d618ef | 2011-02-14 22:23:49 +0000 | [diff] [blame] | 298 | sym.comdat_key = sym.name; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 299 | sym.def = LDPK_WEAKDEF; |
| 300 | break; |
Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 301 | case LTO_SYMBOL_DEFINITION_WEAKUNDEF: |
| 302 | sym.def = LDPK_WEAKUNDEF; |
| 303 | break; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 304 | default: |
| 305 | (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition); |
| 306 | return LDPS_ERR; |
| 307 | } |
| 308 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 309 | sym.size = 0; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 310 | |
| 311 | sym.resolution = LDPR_UNKNOWN; |
| 312 | } |
| 313 | |
| 314 | cf.syms.reserve(cf.syms.size()); |
| 315 | |
| 316 | if (!cf.syms.empty()) { |
| 317 | if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) { |
| 318 | (*message)(LDPL_ERROR, "Unable to add symbols!"); |
| 319 | return LDPS_ERR; |
| 320 | } |
| 321 | } |
| 322 | |
Rafael Espindola | 8e04fc3 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 323 | if (code_gen) |
| 324 | lto_codegen_add_module(code_gen, M); |
| 325 | |
| 326 | lto_module_dispose(M); |
| 327 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 328 | return LDPS_OK; |
| 329 | } |
| 330 | |
| 331 | /// all_symbols_read_hook - gold informs us that all symbols have been read. |
| 332 | /// At this point, we use get_symbols to see if any of our definitions have |
| 333 | /// been overridden by a native object file. Then, perform optimization and |
| 334 | /// codegen. |
Dan Gohman | f692003 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 335 | static ld_plugin_status all_symbols_read_hook(void) { |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 336 | std::ofstream api_file; |
Rafael Espindola | 8e04fc3 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 337 | assert(code_gen); |
| 338 | |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 339 | if (options::generate_api_file) { |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 340 | api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc); |
| 341 | if (!api_file.is_open()) { |
| 342 | (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing."); |
| 343 | abort(); |
| 344 | } |
| 345 | } |
| 346 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 347 | // If we don't preserve any symbols, libLTO will assume that all symbols are |
| 348 | // needed. Keep all symbols unless we're producing a final executable. |
Rafael Espindola | c72f8e9 | 2010-06-03 14:45:44 +0000 | [diff] [blame] | 349 | bool anySymbolsPreserved = false; |
| 350 | for (std::list<claimed_file>::iterator I = Modules.begin(), |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 351 | E = Modules.end(); I != E; ++I) { |
Rafael Espindola | c72f8e9 | 2010-06-03 14:45:44 +0000 | [diff] [blame] | 352 | (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]); |
| 353 | for (unsigned i = 0, e = I->syms.size(); i != e; i++) { |
| 354 | if (I->syms[i].resolution == LDPR_PREVAILING_DEF) { |
Rafael Espindola | 37d42f8 | 2011-02-19 21:49:57 +0000 | [diff] [blame] | 355 | lto_codegen_add_must_preserve_symbol(code_gen, I->syms[i].name); |
Rafael Espindola | c72f8e9 | 2010-06-03 14:45:44 +0000 | [diff] [blame] | 356 | anySymbolsPreserved = true; |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 357 | |
Rafael Espindola | c72f8e9 | 2010-06-03 14:45:44 +0000 | [diff] [blame] | 358 | if (options::generate_api_file) |
| 359 | api_file << I->syms[i].name << "\n"; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
Rafael Espindola | 38a979b | 2010-06-14 21:20:52 +0000 | [diff] [blame] | 362 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 363 | |
Rafael Espindola | 38a979b | 2010-06-14 21:20:52 +0000 | [diff] [blame] | 364 | if (options::generate_api_file) |
| 365 | api_file.close(); |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 366 | |
Rafael Espindola | 38a979b | 2010-06-14 21:20:52 +0000 | [diff] [blame] | 367 | if (!anySymbolsPreserved) { |
| 368 | // All of the IL is unnecessary! |
Rafael Espindola | 37d42f8 | 2011-02-19 21:49:57 +0000 | [diff] [blame] | 369 | lto_codegen_dispose(code_gen); |
Rafael Espindola | 38a979b | 2010-06-14 21:20:52 +0000 | [diff] [blame] | 370 | return LDPS_OK; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Rafael Espindola | 37d42f8 | 2011-02-19 21:49:57 +0000 | [diff] [blame] | 373 | lto_codegen_set_pic_model(code_gen, output_type); |
| 374 | lto_codegen_set_debug_model(code_gen, LTO_DEBUG_MODEL_DWARF); |
Rafael Espindola | 2d643ef | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 375 | if (!options::mcpu.empty()) |
Rafael Espindola | 37d42f8 | 2011-02-19 21:49:57 +0000 | [diff] [blame] | 376 | lto_codegen_set_cpu(code_gen, options::mcpu.c_str()); |
Rafael Espindola | 2d643ef | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 377 | |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 378 | // Pass through extra options to the code generator. |
| 379 | if (!options::extra.empty()) { |
| 380 | for (std::vector<std::string>::iterator it = options::extra.begin(); |
| 381 | it != options::extra.end(); ++it) { |
Rafael Espindola | 37d42f8 | 2011-02-19 21:49:57 +0000 | [diff] [blame] | 382 | lto_codegen_debug_options(code_gen, (*it).c_str()); |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 385 | |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 386 | if (options::generate_bc_file != options::BC_NO) { |
| 387 | std::string path; |
| 388 | if (options::generate_bc_file == options::BC_ONLY) |
| 389 | path = output_name; |
| 390 | else if (!options::bc_path.empty()) |
| 391 | path = options::bc_path; |
| 392 | else |
| 393 | path = output_name + ".bc"; |
Rafael Espindola | 37d42f8 | 2011-02-19 21:49:57 +0000 | [diff] [blame] | 394 | bool err = lto_codegen_write_merged_modules(code_gen, path.c_str()); |
Rafael Espindola | 62bacd6 | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 395 | if (err) |
| 396 | (*message)(LDPL_FATAL, "Failed to write the output file."); |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 397 | if (options::generate_bc_file == options::BC_ONLY) |
| 398 | exit(0); |
Rafael Espindola | 62bacd6 | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 399 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 400 | size_t bufsize = 0; |
Rafael Espindola | 5a287d7 | 2011-02-16 11:19:44 +0000 | [diff] [blame] | 401 | const char *objPath; |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame^] | 402 | if (lto_codegen_compile_to_file(code_gen, &objPath)) { |
| 403 | (*message)(LDPL_ERROR, "Could not produce a combined object file\n"); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 404 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 405 | |
Rafael Espindola | 37d42f8 | 2011-02-19 21:49:57 +0000 | [diff] [blame] | 406 | lto_codegen_dispose(code_gen); |
Rafael Espindola | 8e04fc3 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 407 | for (std::list<claimed_file>::iterator I = Modules.begin(), |
| 408 | E = Modules.end(); I != E; ++I) { |
| 409 | for (unsigned i = 0; i != I->syms.size(); ++i) { |
| 410 | ld_plugin_symbol &sym = I->syms[i]; |
| 411 | free(sym.name); |
| 412 | } |
| 413 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 414 | |
Rafael Espindola | 5a287d7 | 2011-02-16 11:19:44 +0000 | [diff] [blame] | 415 | if ((*add_input_file)(objPath) != LDPS_OK) { |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 416 | (*message)(LDPL_ERROR, "Unable to add .o file to the link."); |
Rafael Espindola | 5a287d7 | 2011-02-16 11:19:44 +0000 | [diff] [blame] | 417 | (*message)(LDPL_ERROR, "File left behind in: %s", objPath); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 418 | return LDPS_ERR; |
| 419 | } |
| 420 | |
Rafael Espindola | 11f403c | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 421 | if (!options::extra_library_path.empty() && |
| 422 | set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) { |
| 423 | (*message)(LDPL_ERROR, "Unable to set the extra library path."); |
| 424 | return LDPS_ERR; |
| 425 | } |
| 426 | |
Rafael Espindola | 5a287d7 | 2011-02-16 11:19:44 +0000 | [diff] [blame] | 427 | if (options::obj_path.empty()) |
| 428 | Cleanup.push_back(sys::Path(objPath)); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 429 | |
| 430 | return LDPS_OK; |
| 431 | } |
| 432 | |
Dan Gohman | f692003 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 433 | static ld_plugin_status cleanup_hook(void) { |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 434 | std::string ErrMsg; |
| 435 | |
| 436 | for (int i = 0, e = Cleanup.size(); i != e; ++i) |
| 437 | if (Cleanup[i].eraseFromDisk(false, &ErrMsg)) |
| 438 | (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(), |
| 439 | ErrMsg.c_str()); |
| 440 | |
| 441 | return LDPS_OK; |
| 442 | } |