blob: a974d81d323f7e28531d071bf6a066fe2ee0cf09 [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>
25#include <list>
26#include <vector>
27
28using namespace llvm;
29
30namespace {
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.
34 abort();
35 }
36
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;
41
42 int api_version = 0;
43 int gold_version = 0;
44
45 struct claimed_file {
46 lto_module_t M;
47 void *handle;
48 std::vector<ld_plugin_symbol> syms;
49 };
50
51 lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
52 std::list<claimed_file> Modules;
53 std::vector<sys::Path> Cleanup;
54}
55
56ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
57 int *claimed);
58ld_plugin_status all_symbols_read_hook(void);
59ld_plugin_status cleanup_hook(void);
60
61extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
62ld_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
67 // for services.
68
69 bool registeredClaimFile = false;
70 bool registeredAllSymbolsRead = false;
71 bool registeredCleanup = false;
72
73 for (; tv->tv_tag != LDPT_NULL; ++tv) {
74 switch (tv->tv_tag) {
75 case LDPT_API_VERSION:
76 api_version = tv->tv_u.tv_val;
77 break;
78 case LDPT_GOLD_VERSION: // major * 100 + minor
79 gold_version = tv->tv_u.tv_val;
80 break;
81 case LDPT_LINKER_OUTPUT:
82 switch (tv->tv_u.tv_val) {
83 case LDPO_REL: // .o
84 case LDPO_DYN: // .so
85 output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
86 break;
87 case LDPO_EXEC: // .exe
88 output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
89 break;
90 default:
91 (*message)(LDPL_ERROR, "Unknown output file type %d",
92 tv->tv_u.tv_val);
93 return LDPS_ERR;
94 }
95 // TODO: add an option to disable PIC.
96 //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
97 break;
98 case LDPT_OPTION:
99 (*message)(LDPL_WARNING, "Ignoring flag %s", tv->tv_u.tv_string);
100 break;
101 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
102 ld_plugin_register_claim_file callback;
103 callback = tv->tv_u.tv_register_claim_file;
104
105 if ((*callback)(claim_file_hook) != LDPS_OK)
106 return LDPS_ERR;
107
108 registeredClaimFile = true;
109 } break;
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;
113
114 if ((*callback)(all_symbols_read_hook) != LDPS_OK)
115 return LDPS_ERR;
116
117 registeredAllSymbolsRead = true;
118 } break;
119 case LDPT_REGISTER_CLEANUP_HOOK: {
120 ld_plugin_register_cleanup callback;
121 callback = tv->tv_u.tv_register_cleanup;
122
123 if ((*callback)(cleanup_hook) != LDPS_OK)
124 return LDPS_ERR;
125
126 registeredCleanup = true;
127 } break;
128 case LDPT_ADD_SYMBOLS:
129 add_symbols = tv->tv_u.tv_add_symbols;
130 break;
131 case LDPT_GET_SYMBOLS:
132 get_symbols = tv->tv_u.tv_get_symbols;
133 break;
134 case LDPT_ADD_INPUT_FILE:
135 add_input_file = tv->tv_u.tv_add_input_file;
136 break;
137 case LDPT_MESSAGE:
138 message = tv->tv_u.tv_message;
139 break;
140 default:
141 break;
142 }
143 }
144
145 if (!registeredClaimFile || !registeredAllSymbolsRead || !registeredCleanup ||
146 !add_symbols || !get_symbols || !add_input_file) {
147 (*message)(LDPL_ERROR, "Not all hooks registered for LLVMgold.");
148 return LDPS_ERR;
149 }
150
151 return LDPS_OK;
152}
153
154/// claim_file_hook - called by gold to see whether this file is one that
155/// our plugin can handle. We'll try to open it and register all the symbols
156/// with add_symbol if possible.
157ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
158 int *claimed) {
Edwin Török8b5a38f2009-02-04 17:39:30 +0000159 void *buf = NULL;
Edwin Török8b5a38f2009-02-04 17:39:30 +0000160 if (file->offset) {
Nick Lewycky0d22bd72009-02-05 04:14:23 +0000161 // Gold has found what might be IR part-way inside of a file, such as
162 // an .a archive.
Edwin Török8b5a38f2009-02-04 17:39:30 +0000163 if (lseek(file->fd, file->offset, SEEK_SET) == -1) {
Nick Lewyckydca9a842009-02-07 03:15:01 +0000164 (*message)(LDPL_ERROR,
Edwin Török8b5a38f2009-02-04 17:39:30 +0000165 "Failed to seek to archive member of %s at offset %d: %s\n",
166 file->name,
167 file->offset, strerror(errno));
168 return LDPS_ERR;
169 }
170 buf = malloc(file->filesize);
171 if (!buf) {
Nick Lewyckydca9a842009-02-07 03:15:01 +0000172 (*message)(LDPL_ERROR,
Edwin Török8b5a38f2009-02-04 17:39:30 +0000173 "Failed to allocate buffer for archive member of size: %d\n",
174 file->filesize);
175 return LDPS_ERR;
176 }
177 if (read(file->fd, buf, file->filesize) != file->filesize) {
Nick Lewyckydca9a842009-02-07 03:15:01 +0000178 (*message)(LDPL_ERROR,
179 "Failed to read archive member of %s at offset %d: %s\n",
Edwin Török8b5a38f2009-02-04 17:39:30 +0000180 file->name,
Nick Lewyckydca9a842009-02-07 03:15:01 +0000181 file->offset,
Edwin Török8b5a38f2009-02-04 17:39:30 +0000182 strerror(errno));
Nick Lewyckydca9a842009-02-07 03:15:01 +0000183 free(buf);
Edwin Török8b5a38f2009-02-04 17:39:30 +0000184 return LDPS_ERR;
185 }
Nick Lewyckydca9a842009-02-07 03:15:01 +0000186 if (!lto_module_is_object_file_in_memory(buf, file->filesize)) {
187 free(buf);
Edwin Török8b5a38f2009-02-04 17:39:30 +0000188 return LDPS_OK;
Nick Lewyckydca9a842009-02-07 03:15:01 +0000189 }
Edwin Török8b5a38f2009-02-04 17:39:30 +0000190 } else if (!lto_module_is_object_file(file->name))
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000191 return LDPS_OK;
192
193 *claimed = 1;
194 Modules.resize(Modules.size() + 1);
195 claimed_file &cf = Modules.back();
196
Nick Lewycky0d22bd72009-02-05 04:14:23 +0000197 cf.M = buf ? lto_module_create_from_memory(buf, file->filesize) :
198 lto_module_create(file->name);
Nick Lewycky5d8327c2009-02-06 01:58:34 +0000199 free(buf);
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000200 if (!cf.M) {
201 (*message)(LDPL_ERROR, "Failed to create LLVM module: %s",
202 lto_get_error_message());
203 return LDPS_ERR;
204 }
205 cf.handle = file->handle;
206 unsigned sym_count = lto_module_get_num_symbols(cf.M);
207 cf.syms.reserve(sym_count);
208
209 for (unsigned i = 0; i != sym_count; ++i) {
210 lto_symbol_attributes attrs = lto_module_get_symbol_attribute(cf.M, i);
211 if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
212 continue;
213
214 cf.syms.push_back(ld_plugin_symbol());
215 ld_plugin_symbol &sym = cf.syms.back();
216 sym.name = const_cast<char *>(lto_module_get_symbol_name(cf.M, i));
217 sym.version = NULL;
218
219 int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
220 switch (scope) {
221 case LTO_SYMBOL_SCOPE_HIDDEN:
222 sym.visibility = LDPV_HIDDEN;
223 break;
224 case LTO_SYMBOL_SCOPE_PROTECTED:
225 sym.visibility = LDPV_PROTECTED;
226 break;
227 case 0: // extern
228 case LTO_SYMBOL_SCOPE_DEFAULT:
229 sym.visibility = LDPV_DEFAULT;
230 break;
231 default:
232 (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
233 return LDPS_ERR;
234 }
235
236 int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
237 switch (definition) {
238 case LTO_SYMBOL_DEFINITION_REGULAR:
239 sym.def = LDPK_DEF;
240 break;
241 case LTO_SYMBOL_DEFINITION_UNDEFINED:
242 sym.def = LDPK_UNDEF;
243 break;
244 case LTO_SYMBOL_DEFINITION_TENTATIVE:
245 sym.def = LDPK_COMMON;
246 break;
247 case LTO_SYMBOL_DEFINITION_WEAK:
248 sym.def = LDPK_WEAKDEF;
249 break;
250 default:
251 (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition);
252 return LDPS_ERR;
253 }
254
255 // LLVM never emits COMDAT.
256 sym.size = 0;
257 sym.comdat_key = NULL;
258
259 sym.resolution = LDPR_UNKNOWN;
260 }
261
262 cf.syms.reserve(cf.syms.size());
263
264 if (!cf.syms.empty()) {
265 if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
266 (*message)(LDPL_ERROR, "Unable to add symbols!");
267 return LDPS_ERR;
268 }
269 }
270
271 return LDPS_OK;
272}
273
274/// all_symbols_read_hook - gold informs us that all symbols have been read.
275/// At this point, we use get_symbols to see if any of our definitions have
276/// been overridden by a native object file. Then, perform optimization and
277/// codegen.
278ld_plugin_status all_symbols_read_hook(void) {
279 lto_code_gen_t cg = lto_codegen_create();
280
281 for (std::list<claimed_file>::iterator I = Modules.begin(),
282 E = Modules.end(); I != E; ++I)
283 lto_codegen_add_module(cg, I->M);
284
285 // If we don't preserve any symbols, libLTO will assume that all symbols are
286 // needed. Keep all symbols unless we're producing a final executable.
287 if (output_type == LTO_CODEGEN_PIC_MODEL_STATIC) {
288 bool anySymbolsPreserved = false;
289 for (std::list<claimed_file>::iterator I = Modules.begin(),
290 E = Modules.end(); I != E; ++I) {
291 (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
292 for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000293 if (I->syms[i].resolution == LDPR_PREVAILING_DEF) {
294 lto_codegen_add_must_preserve_symbol(cg, I->syms[i].name);
295 anySymbolsPreserved = true;
296 }
297 }
298 }
299
300 if (!anySymbolsPreserved) {
301 // This entire file is unnecessary!
302 lto_codegen_dispose(cg);
303 return LDPS_OK;
304 }
305 }
306
307 lto_codegen_set_pic_model(cg, output_type);
308 lto_codegen_set_debug_model(cg, LTO_DEBUG_MODEL_DWARF);
309
310 size_t bufsize = 0;
311 const char *buffer = static_cast<const char *>(lto_codegen_compile(cg,
312 &bufsize));
313
314 std::string ErrMsg;
315
316 sys::Path uniqueObjPath("/tmp/llvmgold.o");
317 if (uniqueObjPath.createTemporaryFileOnDisk(true, &ErrMsg)) {
318 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
319 return LDPS_ERR;
320 }
321 raw_fd_ostream *objFile = new raw_fd_ostream(uniqueObjPath.c_str(), true,
322 ErrMsg);
323 if (!ErrMsg.empty()) {
324 delete objFile;
325 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
326 return LDPS_ERR;
327 }
328
329 objFile->write(buffer, bufsize);
330 objFile->close();
331
332 lto_codegen_dispose(cg);
Nick Lewyckyc64a3bc2009-02-03 07:13:24 +0000333
334 if ((*add_input_file)(const_cast<char*>(uniqueObjPath.c_str())) != LDPS_OK) {
335 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
336 (*message)(LDPL_ERROR, "File left behind in: %s", uniqueObjPath.c_str());
337 return LDPS_ERR;
338 }
339
340 Cleanup.push_back(uniqueObjPath);
341
342 return LDPS_OK;
343}
344
345ld_plugin_status cleanup_hook(void) {
346 std::string ErrMsg;
347
348 for (int i = 0, e = Cleanup.size(); i != e; ++i)
349 if (Cleanup[i].eraseFromDisk(false, &ErrMsg))
350 (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),
351 ErrMsg.c_str());
352
353 return LDPS_OK;
354}