blob: 46b1717fca690e62c170e0d1026a8c2d55aa544d [file] [log] [blame]
Nick Lewyckyc64a3bc2009-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
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
Edwin Török27599342009-02-04 21:00:02 +000022#include <cerrno>
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +000023#include <cstdlib>
24#include <cstring>
Nick Lewyckyb9c19732009-02-22 22:15:44 +000025#include <fstream>
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +000026#include <list>
27#include <vector>
28
29using namespace llvm;
30
31namespace {
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 Lewyckyb9c19732009-02-22 22:15:44 +000046 bool generate_api_file = false;
Nick Lewyckyef973202009-04-30 15:24:09 +000047 const char *gcc_path = NULL;
Nick Lewycky4c47e352009-06-07 00:50:45 +000048 const char *as_path = NULL;
Nick Lewyckyb9c19732009-02-22 22:15:44 +000049
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +000050 struct claimed_file {
51 lto_module_t M;
52 void *handle;
53 std::vector<ld_plugin_symbol> syms;
54 };
55
56 lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
57 std::list<claimed_file> Modules;
58 std::vector<sys::Path> Cleanup;
59}
60
61ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
62 int *claimed);
63ld_plugin_status all_symbols_read_hook(void);
64ld_plugin_status cleanup_hook(void);
65
66extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
67ld_plugin_status onload(ld_plugin_tv *tv) {
68 // We're given a pointer to the first transfer vector. We read through them
69 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
70 // contain pointers to functions that we need to call to register our own
71 // hooks. The others are addresses of functions we can use to call into gold
72 // for services.
73
74 bool registeredClaimFile = false;
75 bool registeredAllSymbolsRead = false;
76 bool registeredCleanup = false;
77
78 for (; tv->tv_tag != LDPT_NULL; ++tv) {
79 switch (tv->tv_tag) {
80 case LDPT_API_VERSION:
81 api_version = tv->tv_u.tv_val;
82 break;
83 case LDPT_GOLD_VERSION: // major * 100 + minor
84 gold_version = tv->tv_u.tv_val;
85 break;
86 case LDPT_LINKER_OUTPUT:
87 switch (tv->tv_u.tv_val) {
88 case LDPO_REL: // .o
89 case LDPO_DYN: // .so
90 output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
91 break;
92 case LDPO_EXEC: // .exe
93 output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
94 break;
95 default:
96 (*message)(LDPL_ERROR, "Unknown output file type %d",
97 tv->tv_u.tv_val);
98 return LDPS_ERR;
99 }
100 // TODO: add an option to disable PIC.
101 //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
102 break;
103 case LDPT_OPTION:
Nick Lewyckyb9c19732009-02-22 22:15:44 +0000104 if (strcmp("generate-api-file", tv->tv_u.tv_string) == 0) {
105 generate_api_file = true;
Nick Lewyckyef973202009-04-30 15:24:09 +0000106 } else if (strncmp("gcc=", tv->tv_u.tv_string, 4) == 0) {
107 if (gcc_path) {
108 (*message)(LDPL_WARNING, "Path to gcc specified twice. "
109 "Discarding %s", tv->tv_u.tv_string);
110 } else {
111 gcc_path = strdup(tv->tv_u.tv_string + 4);
112 }
Nick Lewycky4c47e352009-06-07 00:50:45 +0000113 } else if (strncmp("as=", tv->tv_u.tv_string, 3) == 0) {
114 if (as_path) {
115 (*message)(LDPL_WARNING, "Path to as specified twice. "
116 "Discarding %s", tv->tv_u.tv_string);
117 } else {
118 as_path = strdup(tv->tv_u.tv_string + 3);
119 }
Nick Lewyckyb9c19732009-02-22 22:15:44 +0000120 } else {
121 (*message)(LDPL_WARNING, "Ignoring flag %s", tv->tv_u.tv_string);
122 }
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000123 break;
124 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
125 ld_plugin_register_claim_file callback;
126 callback = tv->tv_u.tv_register_claim_file;
127
128 if ((*callback)(claim_file_hook) != LDPS_OK)
129 return LDPS_ERR;
130
131 registeredClaimFile = true;
132 } break;
133 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
134 ld_plugin_register_all_symbols_read callback;
135 callback = tv->tv_u.tv_register_all_symbols_read;
136
137 if ((*callback)(all_symbols_read_hook) != LDPS_OK)
138 return LDPS_ERR;
139
140 registeredAllSymbolsRead = true;
141 } break;
142 case LDPT_REGISTER_CLEANUP_HOOK: {
143 ld_plugin_register_cleanup callback;
144 callback = tv->tv_u.tv_register_cleanup;
145
146 if ((*callback)(cleanup_hook) != LDPS_OK)
147 return LDPS_ERR;
148
149 registeredCleanup = true;
150 } break;
151 case LDPT_ADD_SYMBOLS:
152 add_symbols = tv->tv_u.tv_add_symbols;
153 break;
154 case LDPT_GET_SYMBOLS:
155 get_symbols = tv->tv_u.tv_get_symbols;
156 break;
157 case LDPT_ADD_INPUT_FILE:
158 add_input_file = tv->tv_u.tv_add_input_file;
159 break;
160 case LDPT_MESSAGE:
161 message = tv->tv_u.tv_message;
162 break;
163 default:
164 break;
165 }
166 }
167
Rafael Espindoladfaa0bd2009-02-18 08:30:15 +0000168 if (!registeredClaimFile) {
Rafael Espindola842e2232009-02-18 17:49:06 +0000169 (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
170 return LDPS_ERR;
171 }
Rafael Espindoladfaa0bd2009-02-18 08:30:15 +0000172 if (!add_symbols) {
Rafael Espindola842e2232009-02-18 17:49:06 +0000173 (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
174 return LDPS_ERR;
175 }
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000176
177 return LDPS_OK;
178}
179
180/// claim_file_hook - called by gold to see whether this file is one that
181/// our plugin can handle. We'll try to open it and register all the symbols
182/// with add_symbol if possible.
183ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
184 int *claimed) {
Edwin Török8b5a38f2009-02-04 17:39:30 +0000185 void *buf = NULL;
Edwin Török8b5a38f2009-02-04 17:39:30 +0000186 if (file->offset) {
Nick Lewycky0d22bd72009-02-05 04:14:23 +0000187 // Gold has found what might be IR part-way inside of a file, such as
188 // an .a archive.
Edwin Török8b5a38f2009-02-04 17:39:30 +0000189 if (lseek(file->fd, file->offset, SEEK_SET) == -1) {
Nick Lewyckydca9a842009-02-07 03:15:01 +0000190 (*message)(LDPL_ERROR,
Edwin Török8b5a38f2009-02-04 17:39:30 +0000191 "Failed to seek to archive member of %s at offset %d: %s\n",
192 file->name,
193 file->offset, strerror(errno));
194 return LDPS_ERR;
195 }
196 buf = malloc(file->filesize);
197 if (!buf) {
Nick Lewyckydca9a842009-02-07 03:15:01 +0000198 (*message)(LDPL_ERROR,
Edwin Török8b5a38f2009-02-04 17:39:30 +0000199 "Failed to allocate buffer for archive member of size: %d\n",
200 file->filesize);
201 return LDPS_ERR;
202 }
203 if (read(file->fd, buf, file->filesize) != file->filesize) {
Nick Lewyckydca9a842009-02-07 03:15:01 +0000204 (*message)(LDPL_ERROR,
205 "Failed to read archive member of %s at offset %d: %s\n",
Edwin Török8b5a38f2009-02-04 17:39:30 +0000206 file->name,
Nick Lewyckydca9a842009-02-07 03:15:01 +0000207 file->offset,
Edwin Török8b5a38f2009-02-04 17:39:30 +0000208 strerror(errno));
Nick Lewyckydca9a842009-02-07 03:15:01 +0000209 free(buf);
Edwin Török8b5a38f2009-02-04 17:39:30 +0000210 return LDPS_ERR;
211 }
Nick Lewyckydca9a842009-02-07 03:15:01 +0000212 if (!lto_module_is_object_file_in_memory(buf, file->filesize)) {
213 free(buf);
Edwin Török8b5a38f2009-02-04 17:39:30 +0000214 return LDPS_OK;
Nick Lewyckydca9a842009-02-07 03:15:01 +0000215 }
Edwin Török8b5a38f2009-02-04 17:39:30 +0000216 } else if (!lto_module_is_object_file(file->name))
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000217 return LDPS_OK;
218
219 *claimed = 1;
220 Modules.resize(Modules.size() + 1);
221 claimed_file &cf = Modules.back();
222
Nick Lewycky0d22bd72009-02-05 04:14:23 +0000223 cf.M = buf ? lto_module_create_from_memory(buf, file->filesize) :
224 lto_module_create(file->name);
Nick Lewycky5d8327c2009-02-06 01:58:34 +0000225 free(buf);
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000226 if (!cf.M) {
227 (*message)(LDPL_ERROR, "Failed to create LLVM module: %s",
228 lto_get_error_message());
229 return LDPS_ERR;
230 }
231 cf.handle = file->handle;
232 unsigned sym_count = lto_module_get_num_symbols(cf.M);
233 cf.syms.reserve(sym_count);
234
235 for (unsigned i = 0; i != sym_count; ++i) {
236 lto_symbol_attributes attrs = lto_module_get_symbol_attribute(cf.M, i);
237 if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
238 continue;
239
240 cf.syms.push_back(ld_plugin_symbol());
241 ld_plugin_symbol &sym = cf.syms.back();
242 sym.name = const_cast<char *>(lto_module_get_symbol_name(cf.M, i));
243 sym.version = NULL;
244
245 int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
246 switch (scope) {
247 case LTO_SYMBOL_SCOPE_HIDDEN:
248 sym.visibility = LDPV_HIDDEN;
249 break;
250 case LTO_SYMBOL_SCOPE_PROTECTED:
251 sym.visibility = LDPV_PROTECTED;
252 break;
253 case 0: // extern
254 case LTO_SYMBOL_SCOPE_DEFAULT:
255 sym.visibility = LDPV_DEFAULT;
256 break;
257 default:
258 (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
259 return LDPS_ERR;
260 }
261
262 int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
263 switch (definition) {
264 case LTO_SYMBOL_DEFINITION_REGULAR:
265 sym.def = LDPK_DEF;
266 break;
267 case LTO_SYMBOL_DEFINITION_UNDEFINED:
268 sym.def = LDPK_UNDEF;
269 break;
270 case LTO_SYMBOL_DEFINITION_TENTATIVE:
271 sym.def = LDPK_COMMON;
272 break;
273 case LTO_SYMBOL_DEFINITION_WEAK:
274 sym.def = LDPK_WEAKDEF;
275 break;
Rafael Espindolab4d596f2009-04-24 16:55:21 +0000276 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
277 sym.def = LDPK_WEAKUNDEF;
278 break;
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000279 default:
280 (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition);
281 return LDPS_ERR;
282 }
283
284 // LLVM never emits COMDAT.
285 sym.size = 0;
286 sym.comdat_key = NULL;
287
288 sym.resolution = LDPR_UNKNOWN;
289 }
290
291 cf.syms.reserve(cf.syms.size());
292
293 if (!cf.syms.empty()) {
294 if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
295 (*message)(LDPL_ERROR, "Unable to add symbols!");
296 return LDPS_ERR;
297 }
298 }
299
300 return LDPS_OK;
301}
302
303/// all_symbols_read_hook - gold informs us that all symbols have been read.
304/// At this point, we use get_symbols to see if any of our definitions have
305/// been overridden by a native object file. Then, perform optimization and
306/// codegen.
307ld_plugin_status all_symbols_read_hook(void) {
308 lto_code_gen_t cg = lto_codegen_create();
309
310 for (std::list<claimed_file>::iterator I = Modules.begin(),
311 E = Modules.end(); I != E; ++I)
312 lto_codegen_add_module(cg, I->M);
313
Nick Lewyckyb9c19732009-02-22 22:15:44 +0000314 std::ofstream api_file;
315 if (generate_api_file) {
316 api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
317 if (!api_file.is_open()) {
318 (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing.");
319 abort();
320 }
321 }
322
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000323 // If we don't preserve any symbols, libLTO will assume that all symbols are
324 // needed. Keep all symbols unless we're producing a final executable.
325 if (output_type == LTO_CODEGEN_PIC_MODEL_STATIC) {
326 bool anySymbolsPreserved = false;
327 for (std::list<claimed_file>::iterator I = Modules.begin(),
328 E = Modules.end(); I != E; ++I) {
329 (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
330 for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
Nick Lewycky8660c3b2009-02-15 22:49:17 +0000331 if (I->syms[i].resolution == LDPR_PREVAILING_DEF ||
332 (I->syms[i].def == LDPK_COMMON &&
333 I->syms[i].resolution == LDPR_RESOLVED_IR)) {
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000334 lto_codegen_add_must_preserve_symbol(cg, I->syms[i].name);
335 anySymbolsPreserved = true;
Nick Lewyckyb9c19732009-02-22 22:15:44 +0000336
337 if (generate_api_file)
338 api_file << I->syms[i].name << "\n";
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000339 }
340 }
341 }
342
Nick Lewyckyb9c19732009-02-22 22:15:44 +0000343 if (generate_api_file)
344 api_file.close();
345
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000346 if (!anySymbolsPreserved) {
347 // This entire file is unnecessary!
348 lto_codegen_dispose(cg);
349 return LDPS_OK;
350 }
351 }
352
353 lto_codegen_set_pic_model(cg, output_type);
354 lto_codegen_set_debug_model(cg, LTO_DEBUG_MODEL_DWARF);
Nick Lewyckyef973202009-04-30 15:24:09 +0000355 if (gcc_path)
356 lto_codegen_set_gcc_path(cg, gcc_path);
Nick Lewycky4c47e352009-06-07 00:50:45 +0000357 if (as_path)
358 lto_codegen_set_assembler_path(cg, as_path);
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000359
360 size_t bufsize = 0;
361 const char *buffer = static_cast<const char *>(lto_codegen_compile(cg,
362 &bufsize));
363
364 std::string ErrMsg;
365
366 sys::Path uniqueObjPath("/tmp/llvmgold.o");
367 if (uniqueObjPath.createTemporaryFileOnDisk(true, &ErrMsg)) {
368 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
369 return LDPS_ERR;
370 }
371 raw_fd_ostream *objFile = new raw_fd_ostream(uniqueObjPath.c_str(), true,
372 ErrMsg);
373 if (!ErrMsg.empty()) {
374 delete objFile;
375 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
376 return LDPS_ERR;
377 }
378
379 objFile->write(buffer, bufsize);
380 objFile->close();
381
382 lto_codegen_dispose(cg);
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000383
384 if ((*add_input_file)(const_cast<char*>(uniqueObjPath.c_str())) != LDPS_OK) {
385 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
386 (*message)(LDPL_ERROR, "File left behind in: %s", uniqueObjPath.c_str());
387 return LDPS_ERR;
388 }
389
390 Cleanup.push_back(uniqueObjPath);
391
392 return LDPS_OK;
393}
394
395ld_plugin_status cleanup_hook(void) {
396 std::string ErrMsg;
397
398 for (int i = 0, e = Cleanup.size(); i != e; ++i)
399 if (Cleanup[i].eraseFromDisk(false, &ErrMsg))
400 (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),
401 ErrMsg.c_str());
402
403 return LDPS_OK;
404}