1 //===-- gold-plugin.cpp - Plugin to gold for Link Time Optimization ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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 .
13 //===----------------------------------------------------------------------===//
15 #include "plugin-api.h"
17 #include "llvm-c/lto.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/System/Path.h"
31 ld_plugin_status discard_message(int level, const char *format, ...) {
32 // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
33 // callback in the transfer vector. This should never be called.
37 ld_plugin_add_symbols add_symbols = NULL;
38 ld_plugin_get_symbols get_symbols = NULL;
39 ld_plugin_add_input_file add_input_file = NULL;
40 ld_plugin_message message = discard_message;
48 std::vector<ld_plugin_symbol> syms;
51 lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
52 std::list<claimed_file> Modules;
53 std::vector<sys::Path> Cleanup;
56 ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
58 ld_plugin_status all_symbols_read_hook(void);
59 ld_plugin_status cleanup_hook(void);
61 extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
62 ld_plugin_status onload(ld_plugin_tv *tv) {
63 // We're given a pointer to the first transfer vector. We read through them
64 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
65 // contain pointers to functions that we need to call to register our own
66 // hooks. The others are addresses of functions we can use to call into gold
69 bool registeredClaimFile = false;
70 bool registeredAllSymbolsRead = false;
71 bool registeredCleanup = false;
73 for (; tv->tv_tag != LDPT_NULL; ++tv) {
75 case LDPT_API_VERSION:
76 api_version = tv->tv_u.tv_val;
78 case LDPT_GOLD_VERSION: // major * 100 + minor
79 gold_version = tv->tv_u.tv_val;
81 case LDPT_LINKER_OUTPUT:
82 switch (tv->tv_u.tv_val) {
85 output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
87 case LDPO_EXEC: // .exe
88 output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
91 (*message)(LDPL_ERROR, "Unknown output file type %d",
95 // TODO: add an option to disable PIC.
96 //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
99 (*message)(LDPL_WARNING, "Ignoring flag %s", tv->tv_u.tv_string);
101 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
102 ld_plugin_register_claim_file callback;
103 callback = tv->tv_u.tv_register_claim_file;
105 if ((*callback)(claim_file_hook) != LDPS_OK)
108 registeredClaimFile = true;
110 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
111 ld_plugin_register_all_symbols_read callback;
112 callback = tv->tv_u.tv_register_all_symbols_read;
114 if ((*callback)(all_symbols_read_hook) != LDPS_OK)
117 registeredAllSymbolsRead = true;
119 case LDPT_REGISTER_CLEANUP_HOOK: {
120 ld_plugin_register_cleanup callback;
121 callback = tv->tv_u.tv_register_cleanup;
123 if ((*callback)(cleanup_hook) != LDPS_OK)
126 registeredCleanup = true;
128 case LDPT_ADD_SYMBOLS:
129 add_symbols = tv->tv_u.tv_add_symbols;
131 case LDPT_GET_SYMBOLS:
132 get_symbols = tv->tv_u.tv_get_symbols;
134 case LDPT_ADD_INPUT_FILE:
135 add_input_file = tv->tv_u.tv_add_input_file;
138 message = tv->tv_u.tv_message;
145 if (!registeredClaimFile) {
146 (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
150 (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
157 /// claim_file_hook - called by gold to see whether this file is one that
158 /// our plugin can handle. We'll try to open it and register all the symbols
159 /// with add_symbol if possible.
160 ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
164 // Gold has found what might be IR part-way inside of a file, such as
166 if (lseek(file->fd, file->offset, SEEK_SET) == -1) {
167 (*message)(LDPL_ERROR,
168 "Failed to seek to archive member of %s at offset %d: %s\n",
170 file->offset, strerror(errno));
173 buf = malloc(file->filesize);
175 (*message)(LDPL_ERROR,
176 "Failed to allocate buffer for archive member of size: %d\n",
180 if (read(file->fd, buf, file->filesize) != file->filesize) {
181 (*message)(LDPL_ERROR,
182 "Failed to read archive member of %s at offset %d: %s\n",
189 if (!lto_module_is_object_file_in_memory(buf, file->filesize)) {
193 } else if (!lto_module_is_object_file(file->name))
197 Modules.resize(Modules.size() + 1);
198 claimed_file &cf = Modules.back();
200 cf.M = buf ? lto_module_create_from_memory(buf, file->filesize) :
201 lto_module_create(file->name);
204 (*message)(LDPL_ERROR, "Failed to create LLVM module: %s",
205 lto_get_error_message());
208 cf.handle = file->handle;
209 unsigned sym_count = lto_module_get_num_symbols(cf.M);
210 cf.syms.reserve(sym_count);
212 for (unsigned i = 0; i != sym_count; ++i) {
213 lto_symbol_attributes attrs = lto_module_get_symbol_attribute(cf.M, i);
214 if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
217 cf.syms.push_back(ld_plugin_symbol());
218 ld_plugin_symbol &sym = cf.syms.back();
219 sym.name = const_cast<char *>(lto_module_get_symbol_name(cf.M, i));
222 int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
224 case LTO_SYMBOL_SCOPE_HIDDEN:
225 sym.visibility = LDPV_HIDDEN;
227 case LTO_SYMBOL_SCOPE_PROTECTED:
228 sym.visibility = LDPV_PROTECTED;
231 case LTO_SYMBOL_SCOPE_DEFAULT:
232 sym.visibility = LDPV_DEFAULT;
235 (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
239 int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
240 switch (definition) {
241 case LTO_SYMBOL_DEFINITION_REGULAR:
244 case LTO_SYMBOL_DEFINITION_UNDEFINED:
245 sym.def = LDPK_UNDEF;
247 case LTO_SYMBOL_DEFINITION_TENTATIVE:
248 sym.def = LDPK_COMMON;
250 case LTO_SYMBOL_DEFINITION_WEAK:
251 sym.def = LDPK_WEAKDEF;
254 (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition);
258 // LLVM never emits COMDAT.
260 sym.comdat_key = NULL;
262 sym.resolution = LDPR_UNKNOWN;
265 cf.syms.reserve(cf.syms.size());
267 if (!cf.syms.empty()) {
268 if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
269 (*message)(LDPL_ERROR, "Unable to add symbols!");
277 /// all_symbols_read_hook - gold informs us that all symbols have been read.
278 /// At this point, we use get_symbols to see if any of our definitions have
279 /// been overridden by a native object file. Then, perform optimization and
281 ld_plugin_status all_symbols_read_hook(void) {
282 lto_code_gen_t cg = lto_codegen_create();
284 for (std::list<claimed_file>::iterator I = Modules.begin(),
285 E = Modules.end(); I != E; ++I)
286 lto_codegen_add_module(cg, I->M);
288 // If we don't preserve any symbols, libLTO will assume that all symbols are
289 // needed. Keep all symbols unless we're producing a final executable.
290 if (output_type == LTO_CODEGEN_PIC_MODEL_STATIC) {
291 bool anySymbolsPreserved = false;
292 for (std::list<claimed_file>::iterator I = Modules.begin(),
293 E = Modules.end(); I != E; ++I) {
294 (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
295 for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
296 if (I->syms[i].resolution == LDPR_PREVAILING_DEF ||
297 (I->syms[i].def == LDPK_COMMON &&
298 I->syms[i].resolution == LDPR_RESOLVED_IR)) {
299 lto_codegen_add_must_preserve_symbol(cg, I->syms[i].name);
300 anySymbolsPreserved = true;
305 if (!anySymbolsPreserved) {
306 // This entire file is unnecessary!
307 lto_codegen_dispose(cg);
312 lto_codegen_set_pic_model(cg, output_type);
313 lto_codegen_set_debug_model(cg, LTO_DEBUG_MODEL_DWARF);
316 const char *buffer = static_cast<const char *>(lto_codegen_compile(cg,
321 sys::Path uniqueObjPath("/tmp/llvmgold.o");
322 if (uniqueObjPath.createTemporaryFileOnDisk(true, &ErrMsg)) {
323 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
326 raw_fd_ostream *objFile = new raw_fd_ostream(uniqueObjPath.c_str(), true,
328 if (!ErrMsg.empty()) {
330 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
334 objFile->write(buffer, bufsize);
337 lto_codegen_dispose(cg);
339 if ((*add_input_file)(const_cast<char*>(uniqueObjPath.c_str())) != LDPS_OK) {
340 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
341 (*message)(LDPL_ERROR, "File left behind in: %s", uniqueObjPath.c_str());
345 Cleanup.push_back(uniqueObjPath);
350 ld_plugin_status cleanup_hook(void) {
353 for (int i = 0, e = Cleanup.size(); i != e; ++i)
354 if (Cleanup[i].eraseFromDisk(false, &ErrMsg))
355 (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),