blob: 0e1db1bf2eaa9a89e7939fd9603e3afb08e3054a [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;
56 std::list<claimed_file> Modules;
57 std::vector<sys::Path> Cleanup;
58}
59
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000060namespace options {
61 bool generate_api_file = false;
62 const char *as_path = NULL;
63 // Additional options to pass into the code generator.
64 // Note: This array will contain all plugin options which are not claimed
65 // as plugin exclusive to pass to the code generator.
66 // For example, "generate-api-file" and "as"options are for the plugin
67 // use only and will not be passed.
68 std::vector<std::string> extra;
69
70 void process_plugin_option(const char* opt)
71 {
72 if (opt == NULL)
73 return;
74
75 if (strcmp("generate-api-file", opt) == 0) {
76 generate_api_file = true;
77 } else if (strncmp("as=", opt, 3) == 0) {
78 if (as_path) {
79 (*message)(LDPL_WARNING, "Path to as specified twice. "
80 "Discarding %s", opt);
81 } else {
82 as_path = strdup(opt + 3);
83 }
84 } else {
85 // Save this option to pass to the code generator.
86 extra.push_back(std::string(opt));
87 }
88 }
89}
90
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000091ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
92 int *claimed);
93ld_plugin_status all_symbols_read_hook(void);
94ld_plugin_status cleanup_hook(void);
95
96extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
97ld_plugin_status onload(ld_plugin_tv *tv) {
98 // We're given a pointer to the first transfer vector. We read through them
99 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
100 // contain pointers to functions that we need to call to register our own
101 // hooks. The others are addresses of functions we can use to call into gold
102 // for services.
103
104 bool registeredClaimFile = false;
105 bool registeredAllSymbolsRead = false;
106 bool registeredCleanup = false;
107
108 for (; tv->tv_tag != LDPT_NULL; ++tv) {
109 switch (tv->tv_tag) {
110 case LDPT_API_VERSION:
111 api_version = tv->tv_u.tv_val;
112 break;
113 case LDPT_GOLD_VERSION: // major * 100 + minor
114 gold_version = tv->tv_u.tv_val;
115 break;
116 case LDPT_LINKER_OUTPUT:
117 switch (tv->tv_u.tv_val) {
118 case LDPO_REL: // .o
119 case LDPO_DYN: // .so
120 output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
121 break;
122 case LDPO_EXEC: // .exe
123 output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
124 break;
125 default:
126 (*message)(LDPL_ERROR, "Unknown output file type %d",
127 tv->tv_u.tv_val);
128 return LDPS_ERR;
129 }
130 // TODO: add an option to disable PIC.
131 //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
132 break;
133 case LDPT_OPTION:
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000134 options::process_plugin_option(tv->tv_u.tv_string);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000135 break;
136 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
137 ld_plugin_register_claim_file callback;
138 callback = tv->tv_u.tv_register_claim_file;
139
140 if ((*callback)(claim_file_hook) != LDPS_OK)
141 return LDPS_ERR;
142
143 registeredClaimFile = true;
144 } break;
145 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
146 ld_plugin_register_all_symbols_read callback;
147 callback = tv->tv_u.tv_register_all_symbols_read;
148
149 if ((*callback)(all_symbols_read_hook) != LDPS_OK)
150 return LDPS_ERR;
151
152 registeredAllSymbolsRead = true;
153 } break;
154 case LDPT_REGISTER_CLEANUP_HOOK: {
155 ld_plugin_register_cleanup callback;
156 callback = tv->tv_u.tv_register_cleanup;
157
158 if ((*callback)(cleanup_hook) != LDPS_OK)
159 return LDPS_ERR;
160
161 registeredCleanup = true;
162 } break;
163 case LDPT_ADD_SYMBOLS:
164 add_symbols = tv->tv_u.tv_add_symbols;
165 break;
166 case LDPT_GET_SYMBOLS:
167 get_symbols = tv->tv_u.tv_get_symbols;
168 break;
169 case LDPT_ADD_INPUT_FILE:
170 add_input_file = tv->tv_u.tv_add_input_file;
171 break;
172 case LDPT_MESSAGE:
173 message = tv->tv_u.tv_message;
174 break;
175 default:
176 break;
177 }
178 }
179
Rafael Espindola98c507e2009-02-18 08:30:15 +0000180 if (!registeredClaimFile) {
Rafael Espindola6210a942009-02-18 17:49:06 +0000181 (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
182 return LDPS_ERR;
183 }
Rafael Espindola98c507e2009-02-18 08:30:15 +0000184 if (!add_symbols) {
Rafael Espindola6210a942009-02-18 17:49:06 +0000185 (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
186 return LDPS_ERR;
187 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000188
189 return LDPS_OK;
190}
191
192/// claim_file_hook - called by gold to see whether this file is one that
193/// our plugin can handle. We'll try to open it and register all the symbols
194/// with add_symbol if possible.
195ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
196 int *claimed) {
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000197 void *buf = NULL;
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000198 if (file->offset) {
Nick Lewyckyc1da8862009-02-05 04:14:23 +0000199 // Gold has found what might be IR part-way inside of a file, such as
200 // an .a archive.
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000201 if (lseek(file->fd, file->offset, SEEK_SET) == -1) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000202 (*message)(LDPL_ERROR,
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000203 "Failed to seek to archive member of %s at offset %d: %s\n",
204 file->name,
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +0000205 file->offset, sys::StrError(errno).c_str());
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000206 return LDPS_ERR;
207 }
208 buf = malloc(file->filesize);
209 if (!buf) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000210 (*message)(LDPL_ERROR,
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000211 "Failed to allocate buffer for archive member of size: %d\n",
212 file->filesize);
213 return LDPS_ERR;
214 }
215 if (read(file->fd, buf, file->filesize) != file->filesize) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000216 (*message)(LDPL_ERROR,
217 "Failed to read archive member of %s at offset %d: %s\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000218 file->name,
Nick Lewycky0df91b22009-02-07 03:15:01 +0000219 file->offset,
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +0000220 sys::StrError(errno).c_str());
Nick Lewycky0df91b22009-02-07 03:15:01 +0000221 free(buf);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000222 return LDPS_ERR;
223 }
Nick Lewycky0df91b22009-02-07 03:15:01 +0000224 if (!lto_module_is_object_file_in_memory(buf, file->filesize)) {
225 free(buf);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000226 return LDPS_OK;
Nick Lewycky0df91b22009-02-07 03:15:01 +0000227 }
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000228 } else if (!lto_module_is_object_file(file->name))
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000229 return LDPS_OK;
230
231 *claimed = 1;
232 Modules.resize(Modules.size() + 1);
233 claimed_file &cf = Modules.back();
234
Nick Lewyckyc1da8862009-02-05 04:14:23 +0000235 cf.M = buf ? lto_module_create_from_memory(buf, file->filesize) :
236 lto_module_create(file->name);
Nick Lewyckyea97aa62009-02-06 01:58:34 +0000237 free(buf);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000238 if (!cf.M) {
239 (*message)(LDPL_ERROR, "Failed to create LLVM module: %s",
240 lto_get_error_message());
241 return LDPS_ERR;
242 }
243 cf.handle = file->handle;
244 unsigned sym_count = lto_module_get_num_symbols(cf.M);
245 cf.syms.reserve(sym_count);
246
247 for (unsigned i = 0; i != sym_count; ++i) {
248 lto_symbol_attributes attrs = lto_module_get_symbol_attribute(cf.M, i);
249 if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
250 continue;
251
252 cf.syms.push_back(ld_plugin_symbol());
253 ld_plugin_symbol &sym = cf.syms.back();
254 sym.name = const_cast<char *>(lto_module_get_symbol_name(cf.M, i));
255 sym.version = NULL;
256
257 int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
258 switch (scope) {
259 case LTO_SYMBOL_SCOPE_HIDDEN:
260 sym.visibility = LDPV_HIDDEN;
261 break;
262 case LTO_SYMBOL_SCOPE_PROTECTED:
263 sym.visibility = LDPV_PROTECTED;
264 break;
265 case 0: // extern
266 case LTO_SYMBOL_SCOPE_DEFAULT:
267 sym.visibility = LDPV_DEFAULT;
268 break;
269 default:
270 (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
271 return LDPS_ERR;
272 }
273
274 int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
275 switch (definition) {
276 case LTO_SYMBOL_DEFINITION_REGULAR:
277 sym.def = LDPK_DEF;
278 break;
279 case LTO_SYMBOL_DEFINITION_UNDEFINED:
280 sym.def = LDPK_UNDEF;
281 break;
282 case LTO_SYMBOL_DEFINITION_TENTATIVE:
283 sym.def = LDPK_COMMON;
284 break;
285 case LTO_SYMBOL_DEFINITION_WEAK:
286 sym.def = LDPK_WEAKDEF;
287 break;
Rafael Espindola7431af02009-04-24 16:55:21 +0000288 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
289 sym.def = LDPK_WEAKUNDEF;
290 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000291 default:
292 (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition);
293 return LDPS_ERR;
294 }
295
296 // LLVM never emits COMDAT.
297 sym.size = 0;
298 sym.comdat_key = NULL;
299
300 sym.resolution = LDPR_UNKNOWN;
301 }
302
303 cf.syms.reserve(cf.syms.size());
304
305 if (!cf.syms.empty()) {
306 if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
307 (*message)(LDPL_ERROR, "Unable to add symbols!");
308 return LDPS_ERR;
309 }
310 }
311
312 return LDPS_OK;
313}
314
315/// all_symbols_read_hook - gold informs us that all symbols have been read.
316/// At this point, we use get_symbols to see if any of our definitions have
317/// been overridden by a native object file. Then, perform optimization and
318/// codegen.
319ld_plugin_status all_symbols_read_hook(void) {
320 lto_code_gen_t cg = lto_codegen_create();
321
322 for (std::list<claimed_file>::iterator I = Modules.begin(),
323 E = Modules.end(); I != E; ++I)
324 lto_codegen_add_module(cg, I->M);
325
Nick Lewyckyca428622009-02-22 22:15:44 +0000326 std::ofstream api_file;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000327 if (options::generate_api_file) {
Nick Lewyckyca428622009-02-22 22:15:44 +0000328 api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
329 if (!api_file.is_open()) {
330 (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing.");
331 abort();
332 }
333 }
334
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000335 // If we don't preserve any symbols, libLTO will assume that all symbols are
336 // needed. Keep all symbols unless we're producing a final executable.
337 if (output_type == LTO_CODEGEN_PIC_MODEL_STATIC) {
338 bool anySymbolsPreserved = false;
339 for (std::list<claimed_file>::iterator I = Modules.begin(),
340 E = Modules.end(); I != E; ++I) {
341 (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
342 for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
Nick Lewyckye0afaa32009-02-15 22:49:17 +0000343 if (I->syms[i].resolution == LDPR_PREVAILING_DEF ||
344 (I->syms[i].def == LDPK_COMMON &&
345 I->syms[i].resolution == LDPR_RESOLVED_IR)) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000346 lto_codegen_add_must_preserve_symbol(cg, I->syms[i].name);
347 anySymbolsPreserved = true;
Nick Lewyckyca428622009-02-22 22:15:44 +0000348
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000349 if (options::generate_api_file)
Nick Lewyckyca428622009-02-22 22:15:44 +0000350 api_file << I->syms[i].name << "\n";
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000351 }
352 }
353 }
354
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000355 if (options::generate_api_file)
Nick Lewyckyca428622009-02-22 22:15:44 +0000356 api_file.close();
357
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000358 if (!anySymbolsPreserved) {
359 // This entire file is unnecessary!
360 lto_codegen_dispose(cg);
361 return LDPS_OK;
362 }
363 }
364
365 lto_codegen_set_pic_model(cg, output_type);
366 lto_codegen_set_debug_model(cg, LTO_DEBUG_MODEL_DWARF);
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000367 if (options::as_path) {
368 sys::Path p = sys::Program::FindProgramByName(options::as_path);
Rafael Espindola42de34f2009-06-15 10:14:18 +0000369 lto_codegen_set_assembler_path(cg, p.c_str());
370 }
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000371 // Pass through extra options to the code generator.
372 if (!options::extra.empty()) {
373 for (std::vector<std::string>::iterator it = options::extra.begin();
374 it != options::extra.end(); ++it) {
375 lto_codegen_debug_options(cg, (*it).c_str());
376 }
377 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000378
379 size_t bufsize = 0;
380 const char *buffer = static_cast<const char *>(lto_codegen_compile(cg,
381 &bufsize));
382
383 std::string ErrMsg;
384
385 sys::Path uniqueObjPath("/tmp/llvmgold.o");
386 if (uniqueObjPath.createTemporaryFileOnDisk(true, &ErrMsg)) {
387 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
388 return LDPS_ERR;
389 }
Chris Lattner17e9edc2009-08-23 02:51:22 +0000390 raw_fd_ostream *objFile =
391 new raw_fd_ostream(uniqueObjPath.c_str(), ErrMsg,
Dan Gohmanbaa26392009-08-25 15:34:52 +0000392 raw_fd_ostream::F_Binary);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000393 if (!ErrMsg.empty()) {
394 delete objFile;
395 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
396 return LDPS_ERR;
397 }
398
399 objFile->write(buffer, bufsize);
400 objFile->close();
401
402 lto_codegen_dispose(cg);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000403
404 if ((*add_input_file)(const_cast<char*>(uniqueObjPath.c_str())) != LDPS_OK) {
405 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
406 (*message)(LDPL_ERROR, "File left behind in: %s", uniqueObjPath.c_str());
407 return LDPS_ERR;
408 }
409
410 Cleanup.push_back(uniqueObjPath);
411
412 return LDPS_OK;
413}
414
415ld_plugin_status cleanup_hook(void) {
416 std::string ErrMsg;
417
418 for (int i = 0, e = Cleanup.size(); i != e; ++i)
419 if (Cleanup[i].eraseFromDisk(false, &ErrMsg))
420 (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),
421 ErrMsg.c_str());
422
423 return LDPS_OK;
424}