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