blob: e34f773579eb9eb9179fffe17516d8ce5999afd2 [file] [log] [blame]
Nick Lewycky3e62b2d2009-02-03 07:13:24 +00001//===-- 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 Sands09b5d902009-10-22 16:03:32 +000015#include "llvm/Config/config.h"
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000016#include "plugin-api.h"
17
18#include "llvm-c/lto.h"
19
20#include "llvm/Support/raw_ostream.h"
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +000021#include "llvm/System/Errno.h"
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000022#include "llvm/System/Path.h"
Rafael Espindola42de34f2009-06-15 10:14:18 +000023#include "llvm/System/Program.h"
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000024
Torok Edwin6cbbdfd2009-02-04 21:00:02 +000025#include <cerrno>
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000026#include <cstdlib>
27#include <cstring>
Nick Lewyckyca428622009-02-22 22:15:44 +000028#include <fstream>
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000029#include <list>
30#include <vector>
31
32using namespace llvm;
33
34namespace {
35 ld_plugin_status discard_message(int level, const char *format, ...) {
36 // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
37 // callback in the transfer vector. This should never be called.
38 abort();
39 }
40
41 ld_plugin_add_symbols add_symbols = NULL;
42 ld_plugin_get_symbols get_symbols = NULL;
43 ld_plugin_add_input_file add_input_file = NULL;
44 ld_plugin_message message = discard_message;
45
46 int api_version = 0;
47 int gold_version = 0;
48
49 struct claimed_file {
50 lto_module_t M;
51 void *handle;
52 std::vector<ld_plugin_symbol> syms;
53 };
54
55 lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
Rafael Espindolac4b55612010-06-03 21:11:20 +000056 std::string output_name = "";
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000057 std::list<claimed_file> Modules;
58 std::vector<sys::Path> Cleanup;
59}
60
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000061namespace options {
Rafael Espindolac4b55612010-06-03 21:11:20 +000062 enum generate_bc { BC_NO, BC_ALSO, BC_ONLY };
Dan Gohmanf6920032010-04-16 00:42:57 +000063 static bool generate_api_file = false;
Rafael Espindolac4b55612010-06-03 21:11:20 +000064 static generate_bc generate_bc_file = BC_NO;
Rafael Espindola62bacd62010-05-13 13:39:31 +000065 static std::string bc_path;
Dan Gohmanf6920032010-04-16 00:42:57 +000066 static const char *as_path = NULL;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000067 // Additional options to pass into the code generator.
Nick Lewyckyfc55def2010-06-03 17:10:17 +000068 // Note: This array will contain all plugin options which are not claimed
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000069 // as plugin exclusive to pass to the code generator.
Nick Lewyckyfc55def2010-06-03 17:10:17 +000070 // For example, "generate-api-file" and "as"options are for the plugin
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000071 // use only and will not be passed.
Dan Gohmanf6920032010-04-16 00:42:57 +000072 static std::vector<std::string> extra;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000073
Dan Gohmanf6920032010-04-16 00:42:57 +000074 static void process_plugin_option(const char* opt)
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000075 {
76 if (opt == NULL)
77 return;
78
79 if (strcmp("generate-api-file", opt) == 0) {
80 generate_api_file = true;
81 } else if (strncmp("as=", opt, 3) == 0) {
82 if (as_path) {
83 (*message)(LDPL_WARNING, "Path to as specified twice. "
84 "Discarding %s", opt);
85 } else {
86 as_path = strdup(opt + 3);
87 }
Rafael Espindolac4b55612010-06-03 21:11:20 +000088 } else if (strcmp("emit-llvm", opt) == 0) {
89 generate_bc_file = BC_ONLY;
90 } else if (strcmp("also-emit-llvm", opt) == 0) {
91 generate_bc_file = BC_ALSO;
Nick Lewyckyfc55def2010-06-03 17:10:17 +000092 } else if (llvm::StringRef(opt).startswith("also-emit-llvm=")) {
Rafael Espindola62bacd62010-05-13 13:39:31 +000093 const char *path = opt + strlen("also-emit-llvm=");
Rafael Espindolac4b55612010-06-03 21:11:20 +000094 generate_bc_file = BC_ALSO;
Nick Lewycky662f7382010-06-03 17:13:23 +000095 if (!bc_path.empty()) {
Rafael Espindola62bacd62010-05-13 13:39:31 +000096 (*message)(LDPL_WARNING, "Path to the output IL file specified twice. "
97 "Discarding %s", opt);
98 } else {
99 bc_path = path;
100 }
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000101 } else {
102 // Save this option to pass to the code generator.
103 extra.push_back(std::string(opt));
104 }
105 }
106}
107
Dan Gohmanf6920032010-04-16 00:42:57 +0000108static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
109 int *claimed);
110static ld_plugin_status all_symbols_read_hook(void);
111static ld_plugin_status cleanup_hook(void);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000112
113extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
114ld_plugin_status onload(ld_plugin_tv *tv) {
115 // We're given a pointer to the first transfer vector. We read through them
116 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
117 // contain pointers to functions that we need to call to register our own
118 // hooks. The others are addresses of functions we can use to call into gold
119 // for services.
120
121 bool registeredClaimFile = false;
122 bool registeredAllSymbolsRead = false;
123 bool registeredCleanup = false;
124
125 for (; tv->tv_tag != LDPT_NULL; ++tv) {
126 switch (tv->tv_tag) {
127 case LDPT_API_VERSION:
128 api_version = tv->tv_u.tv_val;
129 break;
130 case LDPT_GOLD_VERSION: // major * 100 + minor
131 gold_version = tv->tv_u.tv_val;
132 break;
Rafael Espindolac4b55612010-06-03 21:11:20 +0000133 case LDPT_OUTPUT_NAME:
134 output_name = tv->tv_u.tv_string;
135 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000136 case LDPT_LINKER_OUTPUT:
137 switch (tv->tv_u.tv_val) {
138 case LDPO_REL: // .o
139 case LDPO_DYN: // .so
140 output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
141 break;
142 case LDPO_EXEC: // .exe
143 output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
144 break;
145 default:
146 (*message)(LDPL_ERROR, "Unknown output file type %d",
147 tv->tv_u.tv_val);
148 return LDPS_ERR;
149 }
150 // TODO: add an option to disable PIC.
151 //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
152 break;
153 case LDPT_OPTION:
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000154 options::process_plugin_option(tv->tv_u.tv_string);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000155 break;
156 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
157 ld_plugin_register_claim_file callback;
158 callback = tv->tv_u.tv_register_claim_file;
159
160 if ((*callback)(claim_file_hook) != LDPS_OK)
161 return LDPS_ERR;
162
163 registeredClaimFile = true;
164 } break;
165 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
166 ld_plugin_register_all_symbols_read callback;
167 callback = tv->tv_u.tv_register_all_symbols_read;
168
169 if ((*callback)(all_symbols_read_hook) != LDPS_OK)
170 return LDPS_ERR;
171
172 registeredAllSymbolsRead = true;
173 } break;
174 case LDPT_REGISTER_CLEANUP_HOOK: {
175 ld_plugin_register_cleanup callback;
176 callback = tv->tv_u.tv_register_cleanup;
177
178 if ((*callback)(cleanup_hook) != LDPS_OK)
179 return LDPS_ERR;
180
181 registeredCleanup = true;
182 } break;
183 case LDPT_ADD_SYMBOLS:
184 add_symbols = tv->tv_u.tv_add_symbols;
185 break;
186 case LDPT_GET_SYMBOLS:
187 get_symbols = tv->tv_u.tv_get_symbols;
188 break;
189 case LDPT_ADD_INPUT_FILE:
190 add_input_file = tv->tv_u.tv_add_input_file;
191 break;
192 case LDPT_MESSAGE:
193 message = tv->tv_u.tv_message;
194 break;
195 default:
196 break;
197 }
198 }
199
Rafael Espindola98c507e2009-02-18 08:30:15 +0000200 if (!registeredClaimFile) {
Rafael Espindola6210a942009-02-18 17:49:06 +0000201 (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
202 return LDPS_ERR;
203 }
Rafael Espindola98c507e2009-02-18 08:30:15 +0000204 if (!add_symbols) {
Rafael Espindola6210a942009-02-18 17:49:06 +0000205 (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
206 return LDPS_ERR;
207 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000208
209 return LDPS_OK;
210}
211
212/// claim_file_hook - called by gold to see whether this file is one that
213/// our plugin can handle. We'll try to open it and register all the symbols
214/// with add_symbol if possible.
Dan Gohmanf6920032010-04-16 00:42:57 +0000215static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
216 int *claimed) {
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000217 void *buf = NULL;
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000218 if (file->offset) {
Nick Lewyckyc1da8862009-02-05 04:14:23 +0000219 // Gold has found what might be IR part-way inside of a file, such as
220 // an .a archive.
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000221 if (lseek(file->fd, file->offset, SEEK_SET) == -1) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000222 (*message)(LDPL_ERROR,
Nick Lewyckyfc55def2010-06-03 17:10:17 +0000223 "Failed to seek to archive member of %s at offset %d: %s\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000224 file->name,
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +0000225 file->offset, sys::StrError(errno).c_str());
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000226 return LDPS_ERR;
227 }
228 buf = malloc(file->filesize);
229 if (!buf) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000230 (*message)(LDPL_ERROR,
Nick Lewyckyfc55def2010-06-03 17:10:17 +0000231 "Failed to allocate buffer for archive member of size: %d\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000232 file->filesize);
233 return LDPS_ERR;
234 }
235 if (read(file->fd, buf, file->filesize) != file->filesize) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000236 (*message)(LDPL_ERROR,
237 "Failed to read archive member of %s at offset %d: %s\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000238 file->name,
Nick Lewycky0df91b22009-02-07 03:15:01 +0000239 file->offset,
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +0000240 sys::StrError(errno).c_str());
Nick Lewycky0df91b22009-02-07 03:15:01 +0000241 free(buf);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000242 return LDPS_ERR;
243 }
Nick Lewycky0df91b22009-02-07 03:15:01 +0000244 if (!lto_module_is_object_file_in_memory(buf, file->filesize)) {
245 free(buf);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000246 return LDPS_OK;
Nick Lewycky0df91b22009-02-07 03:15:01 +0000247 }
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000248 } else if (!lto_module_is_object_file(file->name))
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000249 return LDPS_OK;
250
251 *claimed = 1;
252 Modules.resize(Modules.size() + 1);
253 claimed_file &cf = Modules.back();
254
Nick Lewyckyc1da8862009-02-05 04:14:23 +0000255 cf.M = buf ? lto_module_create_from_memory(buf, file->filesize) :
256 lto_module_create(file->name);
Nick Lewyckyea97aa62009-02-06 01:58:34 +0000257 free(buf);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000258 if (!cf.M) {
259 (*message)(LDPL_ERROR, "Failed to create LLVM module: %s",
260 lto_get_error_message());
261 return LDPS_ERR;
262 }
263 cf.handle = file->handle;
264 unsigned sym_count = lto_module_get_num_symbols(cf.M);
265 cf.syms.reserve(sym_count);
266
267 for (unsigned i = 0; i != sym_count; ++i) {
268 lto_symbol_attributes attrs = lto_module_get_symbol_attribute(cf.M, i);
269 if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
270 continue;
271
272 cf.syms.push_back(ld_plugin_symbol());
273 ld_plugin_symbol &sym = cf.syms.back();
274 sym.name = const_cast<char *>(lto_module_get_symbol_name(cf.M, i));
275 sym.version = NULL;
276
277 int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
278 switch (scope) {
279 case LTO_SYMBOL_SCOPE_HIDDEN:
280 sym.visibility = LDPV_HIDDEN;
281 break;
282 case LTO_SYMBOL_SCOPE_PROTECTED:
283 sym.visibility = LDPV_PROTECTED;
284 break;
285 case 0: // extern
286 case LTO_SYMBOL_SCOPE_DEFAULT:
287 sym.visibility = LDPV_DEFAULT;
288 break;
289 default:
290 (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
291 return LDPS_ERR;
292 }
293
294 int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
295 switch (definition) {
296 case LTO_SYMBOL_DEFINITION_REGULAR:
297 sym.def = LDPK_DEF;
298 break;
299 case LTO_SYMBOL_DEFINITION_UNDEFINED:
300 sym.def = LDPK_UNDEF;
301 break;
302 case LTO_SYMBOL_DEFINITION_TENTATIVE:
303 sym.def = LDPK_COMMON;
304 break;
305 case LTO_SYMBOL_DEFINITION_WEAK:
306 sym.def = LDPK_WEAKDEF;
307 break;
Rafael Espindola7431af02009-04-24 16:55:21 +0000308 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
309 sym.def = LDPK_WEAKUNDEF;
310 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000311 default:
312 (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition);
313 return LDPS_ERR;
314 }
315
316 // LLVM never emits COMDAT.
317 sym.size = 0;
318 sym.comdat_key = NULL;
319
320 sym.resolution = LDPR_UNKNOWN;
321 }
322
323 cf.syms.reserve(cf.syms.size());
324
325 if (!cf.syms.empty()) {
326 if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
327 (*message)(LDPL_ERROR, "Unable to add symbols!");
328 return LDPS_ERR;
329 }
330 }
331
332 return LDPS_OK;
333}
334
335/// all_symbols_read_hook - gold informs us that all symbols have been read.
336/// At this point, we use get_symbols to see if any of our definitions have
337/// been overridden by a native object file. Then, perform optimization and
338/// codegen.
Dan Gohmanf6920032010-04-16 00:42:57 +0000339static ld_plugin_status all_symbols_read_hook(void) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000340 lto_code_gen_t cg = lto_codegen_create();
341
342 for (std::list<claimed_file>::iterator I = Modules.begin(),
343 E = Modules.end(); I != E; ++I)
344 lto_codegen_add_module(cg, I->M);
345
Nick Lewyckyca428622009-02-22 22:15:44 +0000346 std::ofstream api_file;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000347 if (options::generate_api_file) {
Nick Lewyckyca428622009-02-22 22:15:44 +0000348 api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
349 if (!api_file.is_open()) {
350 (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing.");
351 abort();
352 }
353 }
354
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000355 // If we don't preserve any symbols, libLTO will assume that all symbols are
356 // needed. Keep all symbols unless we're producing a final executable.
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000357 bool anySymbolsPreserved = false;
358 for (std::list<claimed_file>::iterator I = Modules.begin(),
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000359 E = Modules.end(); I != E; ++I) {
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000360 (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
361 for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
362 if (I->syms[i].resolution == LDPR_PREVAILING_DEF) {
363 lto_codegen_add_must_preserve_symbol(cg, I->syms[i].name);
364 anySymbolsPreserved = true;
Nick Lewyckyca428622009-02-22 22:15:44 +0000365
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000366 if (options::generate_api_file)
367 api_file << I->syms[i].name << "\n";
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000368 }
369 }
370
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000371 if (options::generate_api_file)
Nick Lewyckyca428622009-02-22 22:15:44 +0000372 api_file.close();
373
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000374 if (!anySymbolsPreserved) {
375 // This entire file is unnecessary!
376 lto_codegen_dispose(cg);
377 return LDPS_OK;
378 }
379 }
380
381 lto_codegen_set_pic_model(cg, output_type);
382 lto_codegen_set_debug_model(cg, LTO_DEBUG_MODEL_DWARF);
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000383 if (options::as_path) {
384 sys::Path p = sys::Program::FindProgramByName(options::as_path);
Rafael Espindola42de34f2009-06-15 10:14:18 +0000385 lto_codegen_set_assembler_path(cg, p.c_str());
386 }
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000387 // Pass through extra options to the code generator.
388 if (!options::extra.empty()) {
389 for (std::vector<std::string>::iterator it = options::extra.begin();
390 it != options::extra.end(); ++it) {
391 lto_codegen_debug_options(cg, (*it).c_str());
392 }
393 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000394
Rafael Espindolac4b55612010-06-03 21:11:20 +0000395
396 if (options::generate_bc_file != options::BC_NO) {
397 std::string path;
398 if (options::generate_bc_file == options::BC_ONLY)
399 path = output_name;
400 else if (!options::bc_path.empty())
401 path = options::bc_path;
402 else
403 path = output_name + ".bc";
404 bool err = lto_codegen_write_merged_modules(cg, path.c_str());
Rafael Espindola62bacd62010-05-13 13:39:31 +0000405 if (err)
406 (*message)(LDPL_FATAL, "Failed to write the output file.");
Rafael Espindolac4b55612010-06-03 21:11:20 +0000407 if (options::generate_bc_file == options::BC_ONLY)
408 exit(0);
Rafael Espindola62bacd62010-05-13 13:39:31 +0000409 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000410 size_t bufsize = 0;
411 const char *buffer = static_cast<const char *>(lto_codegen_compile(cg,
412 &bufsize));
413
414 std::string ErrMsg;
415
416 sys::Path uniqueObjPath("/tmp/llvmgold.o");
417 if (uniqueObjPath.createTemporaryFileOnDisk(true, &ErrMsg)) {
418 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
419 return LDPS_ERR;
420 }
Nick Lewycky4b67bc12010-04-16 04:32:20 +0000421 raw_fd_ostream *objFile =
Chris Lattner17e9edc2009-08-23 02:51:22 +0000422 new raw_fd_ostream(uniqueObjPath.c_str(), ErrMsg,
Dan Gohmanbaa26392009-08-25 15:34:52 +0000423 raw_fd_ostream::F_Binary);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000424 if (!ErrMsg.empty()) {
425 delete objFile;
426 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
427 return LDPS_ERR;
428 }
429
430 objFile->write(buffer, bufsize);
431 objFile->close();
432
433 lto_codegen_dispose(cg);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000434
435 if ((*add_input_file)(const_cast<char*>(uniqueObjPath.c_str())) != LDPS_OK) {
436 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
437 (*message)(LDPL_ERROR, "File left behind in: %s", uniqueObjPath.c_str());
438 return LDPS_ERR;
439 }
440
441 Cleanup.push_back(uniqueObjPath);
442
443 return LDPS_OK;
444}
445
Dan Gohmanf6920032010-04-16 00:42:57 +0000446static ld_plugin_status cleanup_hook(void) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000447 std::string ErrMsg;
448
449 for (int i = 0, e = Cleanup.size(); i != e; ++i)
450 if (Cleanup[i].eraseFromDisk(false, &ErrMsg))
451 (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),
452 ErrMsg.c_str());
453
454 return LDPS_OK;
455}