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 { |
| 59 | lto_module_t M; |
| 60 | void *handle; |
| 61 | std::vector<ld_plugin_symbol> syms; |
| 62 | }; |
| 63 | |
| 64 | lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC; |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 65 | std::string output_name = ""; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 66 | std::list<claimed_file> Modules; |
| 67 | std::vector<sys::Path> Cleanup; |
| 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 | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 75 | static std::string as_path; |
Rafael Espindola | 98197e5 | 2010-08-10 18:55:09 +0000 | [diff] [blame] | 76 | static std::vector<std::string> as_args; |
Rafael Espindola | dd76f18 | 2010-06-18 19:18:58 +0000 | [diff] [blame] | 77 | static std::vector<std::string> pass_through; |
Rafael Espindola | 11f403c | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 78 | static std::string extra_library_path; |
Rafael Espindola | cbb170d | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 79 | static std::string triple; |
Rafael Espindola | 2d643ef | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 80 | static std::string mcpu; |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 81 | // Additional options to pass into the code generator. |
Nick Lewycky | fc55def | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 82 | // Note: This array will contain all plugin options which are not claimed |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 83 | // as plugin exclusive to pass to the code generator. |
Nick Lewycky | fc55def | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 84 | // For example, "generate-api-file" and "as"options are for the plugin |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 85 | // use only and will not be passed. |
Dan Gohman | f692003 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 86 | static std::vector<std::string> extra; |
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 | static void process_plugin_option(const char* opt_) |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 89 | { |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 90 | if (opt_ == NULL) |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 91 | return; |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 92 | llvm::StringRef opt = opt_; |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 93 | |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 94 | if (opt == "generate-api-file") { |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 95 | generate_api_file = true; |
Rafael Espindola | 2d643ef | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 96 | } else if (opt.startswith("mcpu=")) { |
| 97 | mcpu = opt.substr(strlen("mcpu=")); |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 98 | } else if (opt.startswith("as=")) { |
| 99 | if (!as_path.empty()) { |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 100 | (*message)(LDPL_WARNING, "Path to as specified twice. " |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 101 | "Discarding %s", opt_); |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 102 | } else { |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 103 | as_path = opt.substr(strlen("as=")); |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 104 | } |
Rafael Espindola | 98197e5 | 2010-08-10 18:55:09 +0000 | [diff] [blame] | 105 | } else if (opt.startswith("as-arg=")) { |
| 106 | llvm::StringRef item = opt.substr(strlen("as-arg=")); |
| 107 | as_args.push_back(item.str()); |
Rafael Espindola | 11f403c | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 108 | } else if (opt.startswith("extra-library-path=")) { |
| 109 | extra_library_path = opt.substr(strlen("extra_library_path=")); |
Rafael Espindola | dd76f18 | 2010-06-18 19:18:58 +0000 | [diff] [blame] | 110 | } else if (opt.startswith("pass-through=")) { |
| 111 | llvm::StringRef item = opt.substr(strlen("pass-through=")); |
| 112 | pass_through.push_back(item.str()); |
Rafael Espindola | 15af387 | 2010-08-10 16:32:15 +0000 | [diff] [blame] | 113 | } else if (opt.startswith("mtriple=")) { |
Rafael Espindola | cbb170d | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 114 | triple = opt.substr(strlen("mtriple=")); |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 115 | } else if (opt == "emit-llvm") { |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 116 | generate_bc_file = BC_ONLY; |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 117 | } else if (opt == "also-emit-llvm") { |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 118 | generate_bc_file = BC_ALSO; |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 119 | } else if (opt.startswith("also-emit-llvm=")) { |
| 120 | llvm::StringRef path = opt.substr(strlen("also-emit-llvm=")); |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 121 | generate_bc_file = BC_ALSO; |
Nick Lewycky | 662f738 | 2010-06-03 17:13:23 +0000 | [diff] [blame] | 122 | if (!bc_path.empty()) { |
Rafael Espindola | 62bacd6 | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 123 | (*message)(LDPL_WARNING, "Path to the output IL file specified twice. " |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 124 | "Discarding %s", opt_); |
Rafael Espindola | 62bacd6 | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 125 | } else { |
| 126 | bc_path = path; |
| 127 | } |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 128 | } else { |
| 129 | // Save this option to pass to the code generator. |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 130 | extra.push_back(opt); |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
Dan Gohman | f692003 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 135 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 136 | int *claimed); |
| 137 | static ld_plugin_status all_symbols_read_hook(void); |
| 138 | static ld_plugin_status cleanup_hook(void); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 139 | |
| 140 | extern "C" ld_plugin_status onload(ld_plugin_tv *tv); |
| 141 | ld_plugin_status onload(ld_plugin_tv *tv) { |
| 142 | // We're given a pointer to the first transfer vector. We read through them |
| 143 | // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values |
| 144 | // contain pointers to functions that we need to call to register our own |
| 145 | // hooks. The others are addresses of functions we can use to call into gold |
| 146 | // for services. |
| 147 | |
| 148 | bool registeredClaimFile = false; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 149 | |
| 150 | for (; tv->tv_tag != LDPT_NULL; ++tv) { |
| 151 | switch (tv->tv_tag) { |
| 152 | case LDPT_API_VERSION: |
| 153 | api_version = tv->tv_u.tv_val; |
| 154 | break; |
| 155 | case LDPT_GOLD_VERSION: // major * 100 + minor |
| 156 | gold_version = tv->tv_u.tv_val; |
| 157 | break; |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 158 | case LDPT_OUTPUT_NAME: |
| 159 | output_name = tv->tv_u.tv_string; |
| 160 | break; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 161 | case LDPT_LINKER_OUTPUT: |
| 162 | switch (tv->tv_u.tv_val) { |
| 163 | case LDPO_REL: // .o |
| 164 | case LDPO_DYN: // .so |
| 165 | output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC; |
| 166 | break; |
| 167 | case LDPO_EXEC: // .exe |
| 168 | output_type = LTO_CODEGEN_PIC_MODEL_STATIC; |
| 169 | break; |
| 170 | default: |
| 171 | (*message)(LDPL_ERROR, "Unknown output file type %d", |
| 172 | tv->tv_u.tv_val); |
| 173 | return LDPS_ERR; |
| 174 | } |
| 175 | // TODO: add an option to disable PIC. |
| 176 | //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC; |
| 177 | break; |
| 178 | case LDPT_OPTION: |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 179 | options::process_plugin_option(tv->tv_u.tv_string); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 180 | break; |
| 181 | case LDPT_REGISTER_CLAIM_FILE_HOOK: { |
| 182 | ld_plugin_register_claim_file callback; |
| 183 | callback = tv->tv_u.tv_register_claim_file; |
| 184 | |
| 185 | if ((*callback)(claim_file_hook) != LDPS_OK) |
| 186 | return LDPS_ERR; |
| 187 | |
| 188 | registeredClaimFile = true; |
| 189 | } break; |
| 190 | case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: { |
| 191 | ld_plugin_register_all_symbols_read callback; |
| 192 | callback = tv->tv_u.tv_register_all_symbols_read; |
| 193 | |
| 194 | if ((*callback)(all_symbols_read_hook) != LDPS_OK) |
| 195 | return LDPS_ERR; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 196 | } break; |
| 197 | case LDPT_REGISTER_CLEANUP_HOOK: { |
| 198 | ld_plugin_register_cleanup callback; |
| 199 | callback = tv->tv_u.tv_register_cleanup; |
| 200 | |
| 201 | if ((*callback)(cleanup_hook) != LDPS_OK) |
| 202 | return LDPS_ERR; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 203 | } break; |
| 204 | case LDPT_ADD_SYMBOLS: |
| 205 | add_symbols = tv->tv_u.tv_add_symbols; |
| 206 | break; |
| 207 | case LDPT_GET_SYMBOLS: |
| 208 | get_symbols = tv->tv_u.tv_get_symbols; |
| 209 | break; |
| 210 | case LDPT_ADD_INPUT_FILE: |
| 211 | add_input_file = tv->tv_u.tv_add_input_file; |
| 212 | break; |
Rafael Espindola | dd76f18 | 2010-06-18 19:18:58 +0000 | [diff] [blame] | 213 | case LDPT_ADD_INPUT_LIBRARY: |
| 214 | add_input_library = tv->tv_u.tv_add_input_file; |
| 215 | break; |
Rafael Espindola | 11f403c | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 216 | case LDPT_SET_EXTRA_LIBRARY_PATH: |
| 217 | set_extra_library_path = tv->tv_u.tv_set_extra_library_path; |
| 218 | break; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 219 | case LDPT_MESSAGE: |
| 220 | message = tv->tv_u.tv_message; |
| 221 | break; |
| 222 | default: |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | |
Rafael Espindola | 98c507e | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 227 | if (!registeredClaimFile) { |
Rafael Espindola | 6210a94 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 228 | (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold."); |
| 229 | return LDPS_ERR; |
| 230 | } |
Rafael Espindola | 98c507e | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 231 | if (!add_symbols) { |
Rafael Espindola | 6210a94 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 232 | (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold."); |
| 233 | return LDPS_ERR; |
| 234 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 235 | |
| 236 | return LDPS_OK; |
| 237 | } |
| 238 | |
| 239 | /// claim_file_hook - called by gold to see whether this file is one that |
| 240 | /// our plugin can handle. We'll try to open it and register all the symbols |
| 241 | /// with add_symbol if possible. |
Dan Gohman | f692003 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 242 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 243 | int *claimed) { |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame^] | 244 | lto_module_t M; |
| 245 | |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 246 | if (file->offset) { |
Nick Lewycky | c1da886 | 2009-02-05 04:14:23 +0000 | [diff] [blame] | 247 | // Gold has found what might be IR part-way inside of a file, such as |
| 248 | // an .a archive. |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 249 | if (lseek(file->fd, file->offset, SEEK_SET) == -1) { |
Nick Lewycky | 0df91b2 | 2009-02-07 03:15:01 +0000 | [diff] [blame] | 250 | (*message)(LDPL_ERROR, |
Nick Lewycky | fc55def | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 251 | "Failed to seek to archive member of %s at offset %d: %s\n", |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 252 | file->name, |
Jeffrey Yasskin | ed1c0ff | 2009-07-01 18:11:20 +0000 | [diff] [blame] | 253 | file->offset, sys::StrError(errno).c_str()); |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 254 | return LDPS_ERR; |
| 255 | } |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame^] | 256 | void *buf = malloc(file->filesize); |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 257 | if (!buf) { |
Nick Lewycky | 0df91b2 | 2009-02-07 03:15:01 +0000 | [diff] [blame] | 258 | (*message)(LDPL_ERROR, |
Nick Lewycky | fc55def | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 259 | "Failed to allocate buffer for archive member of size: %d\n", |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 260 | file->filesize); |
| 261 | return LDPS_ERR; |
| 262 | } |
| 263 | if (read(file->fd, buf, file->filesize) != file->filesize) { |
Nick Lewycky | 0df91b2 | 2009-02-07 03:15:01 +0000 | [diff] [blame] | 264 | (*message)(LDPL_ERROR, |
| 265 | "Failed to read archive member of %s at offset %d: %s\n", |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 266 | file->name, |
Nick Lewycky | 0df91b2 | 2009-02-07 03:15:01 +0000 | [diff] [blame] | 267 | file->offset, |
Jeffrey Yasskin | ed1c0ff | 2009-07-01 18:11:20 +0000 | [diff] [blame] | 268 | sys::StrError(errno).c_str()); |
Nick Lewycky | 0df91b2 | 2009-02-07 03:15:01 +0000 | [diff] [blame] | 269 | free(buf); |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 270 | return LDPS_ERR; |
| 271 | } |
Nick Lewycky | 0df91b2 | 2009-02-07 03:15:01 +0000 | [diff] [blame] | 272 | if (!lto_module_is_object_file_in_memory(buf, file->filesize)) { |
| 273 | free(buf); |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 274 | return LDPS_OK; |
Nick Lewycky | 0df91b2 | 2009-02-07 03:15:01 +0000 | [diff] [blame] | 275 | } |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame^] | 276 | M = lto_module_create_from_memory(buf, file->filesize); |
| 277 | free(buf); |
| 278 | } else { |
| 279 | // FIXME: We should not need to pass -1 as the file size, but there |
| 280 | // is a bug in BFD that causes it to pass 0 to us. Remove this once |
| 281 | // that is fixed. |
| 282 | off_t size = file->filesize ? file->filesize : -1; |
| 283 | |
| 284 | // FIXME: We should not need to reset the position in the file, but there |
| 285 | // is a bug in BFD. Remove this once that is fixed. |
| 286 | off_t old_pos = lseek(file->fd, 0, SEEK_CUR); |
| 287 | |
| 288 | lseek(file->fd, 0, SEEK_SET); |
| 289 | M = lto_module_create_from_fd(file->fd, file->name, size); |
| 290 | |
| 291 | lseek(file->fd, old_pos, SEEK_SET); |
| 292 | if (!M) |
| 293 | return LDPS_OK; |
| 294 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 295 | |
| 296 | *claimed = 1; |
| 297 | Modules.resize(Modules.size() + 1); |
| 298 | claimed_file &cf = Modules.back(); |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame^] | 299 | cf.M = M; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 300 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 301 | if (!cf.M) { |
| 302 | (*message)(LDPL_ERROR, "Failed to create LLVM module: %s", |
| 303 | lto_get_error_message()); |
| 304 | return LDPS_ERR; |
| 305 | } |
Rafael Espindola | cbb170d | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 306 | |
| 307 | if (!options::triple.empty()) |
| 308 | lto_module_set_target_triple(cf.M, options::triple.c_str()); |
| 309 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 310 | cf.handle = file->handle; |
| 311 | unsigned sym_count = lto_module_get_num_symbols(cf.M); |
| 312 | cf.syms.reserve(sym_count); |
| 313 | |
| 314 | for (unsigned i = 0; i != sym_count; ++i) { |
| 315 | lto_symbol_attributes attrs = lto_module_get_symbol_attribute(cf.M, i); |
| 316 | if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL) |
| 317 | continue; |
| 318 | |
| 319 | cf.syms.push_back(ld_plugin_symbol()); |
| 320 | ld_plugin_symbol &sym = cf.syms.back(); |
| 321 | sym.name = const_cast<char *>(lto_module_get_symbol_name(cf.M, i)); |
| 322 | sym.version = NULL; |
| 323 | |
| 324 | int scope = attrs & LTO_SYMBOL_SCOPE_MASK; |
| 325 | switch (scope) { |
| 326 | case LTO_SYMBOL_SCOPE_HIDDEN: |
| 327 | sym.visibility = LDPV_HIDDEN; |
| 328 | break; |
| 329 | case LTO_SYMBOL_SCOPE_PROTECTED: |
| 330 | sym.visibility = LDPV_PROTECTED; |
| 331 | break; |
| 332 | case 0: // extern |
| 333 | case LTO_SYMBOL_SCOPE_DEFAULT: |
| 334 | sym.visibility = LDPV_DEFAULT; |
| 335 | break; |
| 336 | default: |
| 337 | (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope); |
| 338 | return LDPS_ERR; |
| 339 | } |
| 340 | |
| 341 | int definition = attrs & LTO_SYMBOL_DEFINITION_MASK; |
| 342 | switch (definition) { |
| 343 | case LTO_SYMBOL_DEFINITION_REGULAR: |
| 344 | sym.def = LDPK_DEF; |
| 345 | break; |
| 346 | case LTO_SYMBOL_DEFINITION_UNDEFINED: |
| 347 | sym.def = LDPK_UNDEF; |
| 348 | break; |
| 349 | case LTO_SYMBOL_DEFINITION_TENTATIVE: |
| 350 | sym.def = LDPK_COMMON; |
| 351 | break; |
| 352 | case LTO_SYMBOL_DEFINITION_WEAK: |
| 353 | sym.def = LDPK_WEAKDEF; |
| 354 | break; |
Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 355 | case LTO_SYMBOL_DEFINITION_WEAKUNDEF: |
| 356 | sym.def = LDPK_WEAKUNDEF; |
| 357 | break; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 358 | default: |
| 359 | (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition); |
| 360 | return LDPS_ERR; |
| 361 | } |
| 362 | |
| 363 | // LLVM never emits COMDAT. |
| 364 | sym.size = 0; |
| 365 | sym.comdat_key = NULL; |
| 366 | |
| 367 | sym.resolution = LDPR_UNKNOWN; |
| 368 | } |
| 369 | |
| 370 | cf.syms.reserve(cf.syms.size()); |
| 371 | |
| 372 | if (!cf.syms.empty()) { |
| 373 | if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) { |
| 374 | (*message)(LDPL_ERROR, "Unable to add symbols!"); |
| 375 | return LDPS_ERR; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | return LDPS_OK; |
| 380 | } |
| 381 | |
| 382 | /// all_symbols_read_hook - gold informs us that all symbols have been read. |
| 383 | /// At this point, we use get_symbols to see if any of our definitions have |
| 384 | /// been overridden by a native object file. Then, perform optimization and |
| 385 | /// codegen. |
Dan Gohman | f692003 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 386 | static ld_plugin_status all_symbols_read_hook(void) { |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 387 | lto_code_gen_t cg = lto_codegen_create(); |
| 388 | |
| 389 | for (std::list<claimed_file>::iterator I = Modules.begin(), |
| 390 | E = Modules.end(); I != E; ++I) |
| 391 | lto_codegen_add_module(cg, I->M); |
| 392 | |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 393 | std::ofstream api_file; |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 394 | if (options::generate_api_file) { |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 395 | api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc); |
| 396 | if (!api_file.is_open()) { |
| 397 | (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing."); |
| 398 | abort(); |
| 399 | } |
| 400 | } |
| 401 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 402 | // If we don't preserve any symbols, libLTO will assume that all symbols are |
| 403 | // needed. Keep all symbols unless we're producing a final executable. |
Rafael Espindola | c72f8e9 | 2010-06-03 14:45:44 +0000 | [diff] [blame] | 404 | bool anySymbolsPreserved = false; |
| 405 | for (std::list<claimed_file>::iterator I = Modules.begin(), |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 406 | E = Modules.end(); I != E; ++I) { |
Rafael Espindola | c72f8e9 | 2010-06-03 14:45:44 +0000 | [diff] [blame] | 407 | (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]); |
| 408 | for (unsigned i = 0, e = I->syms.size(); i != e; i++) { |
| 409 | if (I->syms[i].resolution == LDPR_PREVAILING_DEF) { |
| 410 | lto_codegen_add_must_preserve_symbol(cg, I->syms[i].name); |
| 411 | anySymbolsPreserved = true; |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 412 | |
Rafael Espindola | c72f8e9 | 2010-06-03 14:45:44 +0000 | [diff] [blame] | 413 | if (options::generate_api_file) |
| 414 | api_file << I->syms[i].name << "\n"; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
Rafael Espindola | 38a979b | 2010-06-14 21:20:52 +0000 | [diff] [blame] | 417 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 418 | |
Rafael Espindola | 38a979b | 2010-06-14 21:20:52 +0000 | [diff] [blame] | 419 | if (options::generate_api_file) |
| 420 | api_file.close(); |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 421 | |
Rafael Espindola | 38a979b | 2010-06-14 21:20:52 +0000 | [diff] [blame] | 422 | if (!anySymbolsPreserved) { |
| 423 | // All of the IL is unnecessary! |
| 424 | lto_codegen_dispose(cg); |
| 425 | return LDPS_OK; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | lto_codegen_set_pic_model(cg, output_type); |
| 429 | lto_codegen_set_debug_model(cg, LTO_DEBUG_MODEL_DWARF); |
Rafael Espindola | 6c80992 | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 430 | if (!options::as_path.empty()) { |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 431 | sys::Path p = sys::Program::FindProgramByName(options::as_path); |
Rafael Espindola | 42de34f | 2009-06-15 10:14:18 +0000 | [diff] [blame] | 432 | lto_codegen_set_assembler_path(cg, p.c_str()); |
| 433 | } |
Rafael Espindola | 98197e5 | 2010-08-10 18:55:09 +0000 | [diff] [blame] | 434 | if (!options::as_args.empty()) { |
| 435 | std::vector<const char *> as_args_p; |
| 436 | for (std::vector<std::string>::iterator I = options::as_args.begin(), |
| 437 | E = options::as_args.end(); I != E; ++I) { |
| 438 | as_args_p.push_back(I->c_str()); |
| 439 | } |
| 440 | lto_codegen_set_assembler_args(cg, &as_args_p[0], as_args_p.size()); |
| 441 | } |
Rafael Espindola | 2d643ef | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 442 | if (!options::mcpu.empty()) |
| 443 | lto_codegen_set_cpu(cg, options::mcpu.c_str()); |
| 444 | |
Viktor Kutuzov | 5c00b4a | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 445 | // Pass through extra options to the code generator. |
| 446 | if (!options::extra.empty()) { |
| 447 | for (std::vector<std::string>::iterator it = options::extra.begin(); |
| 448 | it != options::extra.end(); ++it) { |
| 449 | lto_codegen_debug_options(cg, (*it).c_str()); |
| 450 | } |
| 451 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 452 | |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 453 | |
| 454 | if (options::generate_bc_file != options::BC_NO) { |
| 455 | std::string path; |
| 456 | if (options::generate_bc_file == options::BC_ONLY) |
| 457 | path = output_name; |
| 458 | else if (!options::bc_path.empty()) |
| 459 | path = options::bc_path; |
| 460 | else |
| 461 | path = output_name + ".bc"; |
| 462 | bool err = lto_codegen_write_merged_modules(cg, path.c_str()); |
Rafael Espindola | 62bacd6 | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 463 | if (err) |
| 464 | (*message)(LDPL_FATAL, "Failed to write the output file."); |
Rafael Espindola | c4b5561 | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 465 | if (options::generate_bc_file == options::BC_ONLY) |
| 466 | exit(0); |
Rafael Espindola | 62bacd6 | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 467 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 468 | size_t bufsize = 0; |
| 469 | const char *buffer = static_cast<const char *>(lto_codegen_compile(cg, |
| 470 | &bufsize)); |
| 471 | |
| 472 | std::string ErrMsg; |
| 473 | |
| 474 | sys::Path uniqueObjPath("/tmp/llvmgold.o"); |
| 475 | if (uniqueObjPath.createTemporaryFileOnDisk(true, &ErrMsg)) { |
| 476 | (*message)(LDPL_ERROR, "%s", ErrMsg.c_str()); |
| 477 | return LDPS_ERR; |
| 478 | } |
Dan Gohman | f291401 | 2010-08-20 16:59:15 +0000 | [diff] [blame] | 479 | tool_output_file objFile(uniqueObjPath.c_str(), ErrMsg, |
| 480 | raw_fd_ostream::F_Binary); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 481 | if (!ErrMsg.empty()) { |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 482 | (*message)(LDPL_ERROR, "%s", ErrMsg.c_str()); |
| 483 | return LDPS_ERR; |
| 484 | } |
| 485 | |
Nick Lewycky | 59b4d2a | 2010-09-02 05:44:31 +0000 | [diff] [blame] | 486 | objFile.os().write(buffer, bufsize); |
| 487 | objFile.os().close(); |
| 488 | if (objFile.os().has_error()) { |
Dan Gohman | f291401 | 2010-08-20 16:59:15 +0000 | [diff] [blame] | 489 | (*message)(LDPL_ERROR, "Error writing output file '%s'", |
| 490 | uniqueObjPath.c_str()); |
Nick Lewycky | 59b4d2a | 2010-09-02 05:44:31 +0000 | [diff] [blame] | 491 | objFile.os().clear_error(); |
Dan Gohman | f291401 | 2010-08-20 16:59:15 +0000 | [diff] [blame] | 492 | return LDPS_ERR; |
| 493 | } |
| 494 | objFile.keep(); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 495 | |
| 496 | lto_codegen_dispose(cg); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 497 | |
Rafael Espindola | bebb640 | 2010-06-21 02:23:12 +0000 | [diff] [blame] | 498 | if ((*add_input_file)(uniqueObjPath.c_str()) != LDPS_OK) { |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 499 | (*message)(LDPL_ERROR, "Unable to add .o file to the link."); |
| 500 | (*message)(LDPL_ERROR, "File left behind in: %s", uniqueObjPath.c_str()); |
| 501 | return LDPS_ERR; |
| 502 | } |
| 503 | |
Rafael Espindola | 11f403c | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 504 | if (!options::extra_library_path.empty() && |
| 505 | set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) { |
| 506 | (*message)(LDPL_ERROR, "Unable to set the extra library path."); |
| 507 | return LDPS_ERR; |
| 508 | } |
| 509 | |
Rafael Espindola | dd76f18 | 2010-06-18 19:18:58 +0000 | [diff] [blame] | 510 | for (std::vector<std::string>::iterator i = options::pass_through.begin(), |
| 511 | e = options::pass_through.end(); |
| 512 | i != e; ++i) { |
| 513 | std::string &item = *i; |
Rafael Espindola | bebb640 | 2010-06-21 02:23:12 +0000 | [diff] [blame] | 514 | const char *item_p = item.c_str(); |
Rafael Espindola | dd76f18 | 2010-06-18 19:18:58 +0000 | [diff] [blame] | 515 | if (llvm::StringRef(item).startswith("-l")) { |
| 516 | if (add_input_library(item_p + 2) != LDPS_OK) { |
| 517 | (*message)(LDPL_ERROR, "Unable to add library to the link."); |
| 518 | return LDPS_ERR; |
| 519 | } |
| 520 | } else { |
| 521 | if (add_input_file(item_p) != LDPS_OK) { |
| 522 | (*message)(LDPL_ERROR, "Unable to add .o file to the link."); |
| 523 | return LDPS_ERR; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 528 | Cleanup.push_back(uniqueObjPath); |
| 529 | |
| 530 | return LDPS_OK; |
| 531 | } |
| 532 | |
Dan Gohman | f692003 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 533 | static ld_plugin_status cleanup_hook(void) { |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 534 | std::string ErrMsg; |
| 535 | |
| 536 | for (int i = 0, e = Cleanup.size(); i != e; ++i) |
| 537 | if (Cleanup[i].eraseFromDisk(false, &ErrMsg)) |
| 538 | (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(), |
| 539 | ErrMsg.c_str()); |
| 540 | |
| 541 | return LDPS_OK; |
| 542 | } |