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 | |
| 15 | #include "plugin-api.h" |
| 16 | |
| 17 | #include "llvm-c/lto.h" |
| 18 | |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | #include "llvm/System/Path.h" |
| 21 | |
Torok Edwin | 6cbbdfd | 2009-02-04 21:00:02 +0000 | [diff] [blame] | 22 | #include <cerrno> |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 23 | #include <cstdlib> |
| 24 | #include <cstring> |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame^] | 25 | #include <fstream> |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 26 | #include <list> |
| 27 | #include <vector> |
| 28 | |
| 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
| 32 | ld_plugin_status discard_message(int level, const char *format, ...) { |
| 33 | // Die loudly. Recent versions of Gold pass ld_plugin_message as the first |
| 34 | // callback in the transfer vector. This should never be called. |
| 35 | abort(); |
| 36 | } |
| 37 | |
| 38 | ld_plugin_add_symbols add_symbols = NULL; |
| 39 | ld_plugin_get_symbols get_symbols = NULL; |
| 40 | ld_plugin_add_input_file add_input_file = NULL; |
| 41 | ld_plugin_message message = discard_message; |
| 42 | |
| 43 | int api_version = 0; |
| 44 | int gold_version = 0; |
| 45 | |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame^] | 46 | bool generate_api_file = false; |
| 47 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 48 | struct claimed_file { |
| 49 | lto_module_t M; |
| 50 | void *handle; |
| 51 | std::vector<ld_plugin_symbol> syms; |
| 52 | }; |
| 53 | |
| 54 | lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC; |
| 55 | std::list<claimed_file> Modules; |
| 56 | std::vector<sys::Path> Cleanup; |
| 57 | } |
| 58 | |
| 59 | ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 60 | int *claimed); |
| 61 | ld_plugin_status all_symbols_read_hook(void); |
| 62 | ld_plugin_status cleanup_hook(void); |
| 63 | |
| 64 | extern "C" ld_plugin_status onload(ld_plugin_tv *tv); |
| 65 | ld_plugin_status onload(ld_plugin_tv *tv) { |
| 66 | // We're given a pointer to the first transfer vector. We read through them |
| 67 | // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values |
| 68 | // contain pointers to functions that we need to call to register our own |
| 69 | // hooks. The others are addresses of functions we can use to call into gold |
| 70 | // for services. |
| 71 | |
| 72 | bool registeredClaimFile = false; |
| 73 | bool registeredAllSymbolsRead = false; |
| 74 | bool registeredCleanup = false; |
| 75 | |
| 76 | for (; tv->tv_tag != LDPT_NULL; ++tv) { |
| 77 | switch (tv->tv_tag) { |
| 78 | case LDPT_API_VERSION: |
| 79 | api_version = tv->tv_u.tv_val; |
| 80 | break; |
| 81 | case LDPT_GOLD_VERSION: // major * 100 + minor |
| 82 | gold_version = tv->tv_u.tv_val; |
| 83 | break; |
| 84 | case LDPT_LINKER_OUTPUT: |
| 85 | switch (tv->tv_u.tv_val) { |
| 86 | case LDPO_REL: // .o |
| 87 | case LDPO_DYN: // .so |
| 88 | output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC; |
| 89 | break; |
| 90 | case LDPO_EXEC: // .exe |
| 91 | output_type = LTO_CODEGEN_PIC_MODEL_STATIC; |
| 92 | break; |
| 93 | default: |
| 94 | (*message)(LDPL_ERROR, "Unknown output file type %d", |
| 95 | tv->tv_u.tv_val); |
| 96 | return LDPS_ERR; |
| 97 | } |
| 98 | // TODO: add an option to disable PIC. |
| 99 | //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC; |
| 100 | break; |
| 101 | case LDPT_OPTION: |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame^] | 102 | if (strcmp("generate-api-file", tv->tv_u.tv_string) == 0) { |
| 103 | generate_api_file = true; |
| 104 | } else { |
| 105 | (*message)(LDPL_WARNING, "Ignoring flag %s", tv->tv_u.tv_string); |
| 106 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 107 | break; |
| 108 | case LDPT_REGISTER_CLAIM_FILE_HOOK: { |
| 109 | ld_plugin_register_claim_file callback; |
| 110 | callback = tv->tv_u.tv_register_claim_file; |
| 111 | |
| 112 | if ((*callback)(claim_file_hook) != LDPS_OK) |
| 113 | return LDPS_ERR; |
| 114 | |
| 115 | registeredClaimFile = true; |
| 116 | } break; |
| 117 | case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: { |
| 118 | ld_plugin_register_all_symbols_read callback; |
| 119 | callback = tv->tv_u.tv_register_all_symbols_read; |
| 120 | |
| 121 | if ((*callback)(all_symbols_read_hook) != LDPS_OK) |
| 122 | return LDPS_ERR; |
| 123 | |
| 124 | registeredAllSymbolsRead = true; |
| 125 | } break; |
| 126 | case LDPT_REGISTER_CLEANUP_HOOK: { |
| 127 | ld_plugin_register_cleanup callback; |
| 128 | callback = tv->tv_u.tv_register_cleanup; |
| 129 | |
| 130 | if ((*callback)(cleanup_hook) != LDPS_OK) |
| 131 | return LDPS_ERR; |
| 132 | |
| 133 | registeredCleanup = true; |
| 134 | } break; |
| 135 | case LDPT_ADD_SYMBOLS: |
| 136 | add_symbols = tv->tv_u.tv_add_symbols; |
| 137 | break; |
| 138 | case LDPT_GET_SYMBOLS: |
| 139 | get_symbols = tv->tv_u.tv_get_symbols; |
| 140 | break; |
| 141 | case LDPT_ADD_INPUT_FILE: |
| 142 | add_input_file = tv->tv_u.tv_add_input_file; |
| 143 | break; |
| 144 | case LDPT_MESSAGE: |
| 145 | message = tv->tv_u.tv_message; |
| 146 | break; |
| 147 | default: |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
Rafael Espindola | 98c507e | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 152 | if (!registeredClaimFile) { |
Rafael Espindola | 6210a94 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 153 | (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold."); |
| 154 | return LDPS_ERR; |
| 155 | } |
Rafael Espindola | 98c507e | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 156 | if (!add_symbols) { |
Rafael Espindola | 6210a94 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 157 | (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold."); |
| 158 | return LDPS_ERR; |
| 159 | } |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 160 | |
| 161 | return LDPS_OK; |
| 162 | } |
| 163 | |
| 164 | /// claim_file_hook - called by gold to see whether this file is one that |
| 165 | /// our plugin can handle. We'll try to open it and register all the symbols |
| 166 | /// with add_symbol if possible. |
| 167 | ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 168 | int *claimed) { |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 169 | void *buf = NULL; |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 170 | if (file->offset) { |
Nick Lewycky | c1da886 | 2009-02-05 04:14:23 +0000 | [diff] [blame] | 171 | // Gold has found what might be IR part-way inside of a file, such as |
| 172 | // an .a archive. |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 173 | if (lseek(file->fd, file->offset, SEEK_SET) == -1) { |
Nick Lewycky | 0df91b2 | 2009-02-07 03:15:01 +0000 | [diff] [blame] | 174 | (*message)(LDPL_ERROR, |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 175 | "Failed to seek to archive member of %s at offset %d: %s\n", |
| 176 | file->name, |
| 177 | file->offset, strerror(errno)); |
| 178 | return LDPS_ERR; |
| 179 | } |
| 180 | buf = malloc(file->filesize); |
| 181 | if (!buf) { |
Nick Lewycky | 0df91b2 | 2009-02-07 03:15:01 +0000 | [diff] [blame] | 182 | (*message)(LDPL_ERROR, |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 183 | "Failed to allocate buffer for archive member of size: %d\n", |
| 184 | file->filesize); |
| 185 | return LDPS_ERR; |
| 186 | } |
| 187 | if (read(file->fd, buf, file->filesize) != file->filesize) { |
Nick Lewycky | 0df91b2 | 2009-02-07 03:15:01 +0000 | [diff] [blame] | 188 | (*message)(LDPL_ERROR, |
| 189 | "Failed to read archive member of %s at offset %d: %s\n", |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 190 | file->name, |
Nick Lewycky | 0df91b2 | 2009-02-07 03:15:01 +0000 | [diff] [blame] | 191 | file->offset, |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 192 | strerror(errno)); |
Nick Lewycky | 0df91b2 | 2009-02-07 03:15:01 +0000 | [diff] [blame] | 193 | free(buf); |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 194 | return LDPS_ERR; |
| 195 | } |
Nick Lewycky | 0df91b2 | 2009-02-07 03:15:01 +0000 | [diff] [blame] | 196 | if (!lto_module_is_object_file_in_memory(buf, file->filesize)) { |
| 197 | free(buf); |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 198 | return LDPS_OK; |
Nick Lewycky | 0df91b2 | 2009-02-07 03:15:01 +0000 | [diff] [blame] | 199 | } |
Torok Edwin | 3e5a0d8 | 2009-02-04 17:39:30 +0000 | [diff] [blame] | 200 | } else if (!lto_module_is_object_file(file->name)) |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 201 | return LDPS_OK; |
| 202 | |
| 203 | *claimed = 1; |
| 204 | Modules.resize(Modules.size() + 1); |
| 205 | claimed_file &cf = Modules.back(); |
| 206 | |
Nick Lewycky | c1da886 | 2009-02-05 04:14:23 +0000 | [diff] [blame] | 207 | cf.M = buf ? lto_module_create_from_memory(buf, file->filesize) : |
| 208 | lto_module_create(file->name); |
Nick Lewycky | ea97aa6 | 2009-02-06 01:58:34 +0000 | [diff] [blame] | 209 | free(buf); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 210 | if (!cf.M) { |
| 211 | (*message)(LDPL_ERROR, "Failed to create LLVM module: %s", |
| 212 | lto_get_error_message()); |
| 213 | return LDPS_ERR; |
| 214 | } |
| 215 | cf.handle = file->handle; |
| 216 | unsigned sym_count = lto_module_get_num_symbols(cf.M); |
| 217 | cf.syms.reserve(sym_count); |
| 218 | |
| 219 | for (unsigned i = 0; i != sym_count; ++i) { |
| 220 | lto_symbol_attributes attrs = lto_module_get_symbol_attribute(cf.M, i); |
| 221 | if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL) |
| 222 | continue; |
| 223 | |
| 224 | cf.syms.push_back(ld_plugin_symbol()); |
| 225 | ld_plugin_symbol &sym = cf.syms.back(); |
| 226 | sym.name = const_cast<char *>(lto_module_get_symbol_name(cf.M, i)); |
| 227 | sym.version = NULL; |
| 228 | |
| 229 | int scope = attrs & LTO_SYMBOL_SCOPE_MASK; |
| 230 | switch (scope) { |
| 231 | case LTO_SYMBOL_SCOPE_HIDDEN: |
| 232 | sym.visibility = LDPV_HIDDEN; |
| 233 | break; |
| 234 | case LTO_SYMBOL_SCOPE_PROTECTED: |
| 235 | sym.visibility = LDPV_PROTECTED; |
| 236 | break; |
| 237 | case 0: // extern |
| 238 | case LTO_SYMBOL_SCOPE_DEFAULT: |
| 239 | sym.visibility = LDPV_DEFAULT; |
| 240 | break; |
| 241 | default: |
| 242 | (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope); |
| 243 | return LDPS_ERR; |
| 244 | } |
| 245 | |
| 246 | int definition = attrs & LTO_SYMBOL_DEFINITION_MASK; |
| 247 | switch (definition) { |
| 248 | case LTO_SYMBOL_DEFINITION_REGULAR: |
| 249 | sym.def = LDPK_DEF; |
| 250 | break; |
| 251 | case LTO_SYMBOL_DEFINITION_UNDEFINED: |
| 252 | sym.def = LDPK_UNDEF; |
| 253 | break; |
| 254 | case LTO_SYMBOL_DEFINITION_TENTATIVE: |
| 255 | sym.def = LDPK_COMMON; |
| 256 | break; |
| 257 | case LTO_SYMBOL_DEFINITION_WEAK: |
| 258 | sym.def = LDPK_WEAKDEF; |
| 259 | break; |
| 260 | default: |
| 261 | (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition); |
| 262 | return LDPS_ERR; |
| 263 | } |
| 264 | |
| 265 | // LLVM never emits COMDAT. |
| 266 | sym.size = 0; |
| 267 | sym.comdat_key = NULL; |
| 268 | |
| 269 | sym.resolution = LDPR_UNKNOWN; |
| 270 | } |
| 271 | |
| 272 | cf.syms.reserve(cf.syms.size()); |
| 273 | |
| 274 | if (!cf.syms.empty()) { |
| 275 | if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) { |
| 276 | (*message)(LDPL_ERROR, "Unable to add symbols!"); |
| 277 | return LDPS_ERR; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return LDPS_OK; |
| 282 | } |
| 283 | |
| 284 | /// all_symbols_read_hook - gold informs us that all symbols have been read. |
| 285 | /// At this point, we use get_symbols to see if any of our definitions have |
| 286 | /// been overridden by a native object file. Then, perform optimization and |
| 287 | /// codegen. |
| 288 | ld_plugin_status all_symbols_read_hook(void) { |
| 289 | lto_code_gen_t cg = lto_codegen_create(); |
| 290 | |
| 291 | for (std::list<claimed_file>::iterator I = Modules.begin(), |
| 292 | E = Modules.end(); I != E; ++I) |
| 293 | lto_codegen_add_module(cg, I->M); |
| 294 | |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame^] | 295 | std::ofstream api_file; |
| 296 | if (generate_api_file) { |
| 297 | api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc); |
| 298 | if (!api_file.is_open()) { |
| 299 | (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing."); |
| 300 | abort(); |
| 301 | } |
| 302 | } |
| 303 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 304 | // If we don't preserve any symbols, libLTO will assume that all symbols are |
| 305 | // needed. Keep all symbols unless we're producing a final executable. |
| 306 | if (output_type == LTO_CODEGEN_PIC_MODEL_STATIC) { |
| 307 | bool anySymbolsPreserved = false; |
| 308 | for (std::list<claimed_file>::iterator I = Modules.begin(), |
| 309 | E = Modules.end(); I != E; ++I) { |
| 310 | (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]); |
| 311 | for (unsigned i = 0, e = I->syms.size(); i != e; i++) { |
Nick Lewycky | e0afaa3 | 2009-02-15 22:49:17 +0000 | [diff] [blame] | 312 | if (I->syms[i].resolution == LDPR_PREVAILING_DEF || |
| 313 | (I->syms[i].def == LDPK_COMMON && |
| 314 | I->syms[i].resolution == LDPR_RESOLVED_IR)) { |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 315 | lto_codegen_add_must_preserve_symbol(cg, I->syms[i].name); |
| 316 | anySymbolsPreserved = true; |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame^] | 317 | |
| 318 | if (generate_api_file) |
| 319 | api_file << I->syms[i].name << "\n"; |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
Nick Lewycky | ca42862 | 2009-02-22 22:15:44 +0000 | [diff] [blame^] | 324 | if (generate_api_file) |
| 325 | api_file.close(); |
| 326 | |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 327 | if (!anySymbolsPreserved) { |
| 328 | // This entire file is unnecessary! |
| 329 | lto_codegen_dispose(cg); |
| 330 | return LDPS_OK; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | lto_codegen_set_pic_model(cg, output_type); |
| 335 | lto_codegen_set_debug_model(cg, LTO_DEBUG_MODEL_DWARF); |
| 336 | |
| 337 | size_t bufsize = 0; |
| 338 | const char *buffer = static_cast<const char *>(lto_codegen_compile(cg, |
| 339 | &bufsize)); |
| 340 | |
| 341 | std::string ErrMsg; |
| 342 | |
| 343 | sys::Path uniqueObjPath("/tmp/llvmgold.o"); |
| 344 | if (uniqueObjPath.createTemporaryFileOnDisk(true, &ErrMsg)) { |
| 345 | (*message)(LDPL_ERROR, "%s", ErrMsg.c_str()); |
| 346 | return LDPS_ERR; |
| 347 | } |
| 348 | raw_fd_ostream *objFile = new raw_fd_ostream(uniqueObjPath.c_str(), true, |
| 349 | ErrMsg); |
| 350 | if (!ErrMsg.empty()) { |
| 351 | delete objFile; |
| 352 | (*message)(LDPL_ERROR, "%s", ErrMsg.c_str()); |
| 353 | return LDPS_ERR; |
| 354 | } |
| 355 | |
| 356 | objFile->write(buffer, bufsize); |
| 357 | objFile->close(); |
| 358 | |
| 359 | lto_codegen_dispose(cg); |
Nick Lewycky | 3e62b2d | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 360 | |
| 361 | if ((*add_input_file)(const_cast<char*>(uniqueObjPath.c_str())) != LDPS_OK) { |
| 362 | (*message)(LDPL_ERROR, "Unable to add .o file to the link."); |
| 363 | (*message)(LDPL_ERROR, "File left behind in: %s", uniqueObjPath.c_str()); |
| 364 | return LDPS_ERR; |
| 365 | } |
| 366 | |
| 367 | Cleanup.push_back(uniqueObjPath); |
| 368 | |
| 369 | return LDPS_OK; |
| 370 | } |
| 371 | |
| 372 | ld_plugin_status cleanup_hook(void) { |
| 373 | std::string ErrMsg; |
| 374 | |
| 375 | for (int i = 0, e = Cleanup.size(); i != e; ++i) |
| 376 | if (Cleanup[i].eraseFromDisk(false, &ErrMsg)) |
| 377 | (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(), |
| 378 | ErrMsg.c_str()); |
| 379 | |
| 380 | return LDPS_OK; |
| 381 | } |