blob: 9c17da6a4cb6e4a83e0f0c3cc633523b9c114aa5 [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
Dylan Noblesmith94214062011-12-22 23:04:07 +000015#include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000016#include "plugin-api.h"
17
18#include "llvm-c/lto.h"
19
Ivan Krasinccb7c902011-09-12 21:47:50 +000020#include "llvm/ADT/OwningPtr.h"
21#include "llvm/Support/system_error.h"
22#include "llvm/Support/MemoryBuffer.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000023#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000024#include "llvm/Support/Errno.h"
25#include "llvm/Support/Path.h"
26#include "llvm/Support/Program.h"
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000027
Torok Edwin6cbbdfd2009-02-04 21:00:02 +000028#include <cerrno>
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000029#include <cstdlib>
30#include <cstring>
Nick Lewyckyca428622009-02-22 22:15:44 +000031#include <fstream>
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000032#include <list>
33#include <vector>
34
Michael J. Spencer8b1659e2011-01-20 05:43:16 +000035// Support Windows/MinGW crazyness.
36#ifdef _WIN32
37# include <io.h>
38# define lseek _lseek
39# define read _read
40#endif
41
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000042using namespace llvm;
43
44namespace {
45 ld_plugin_status discard_message(int level, const char *format, ...) {
46 // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
47 // callback in the transfer vector. This should never be called.
48 abort();
49 }
50
51 ld_plugin_add_symbols add_symbols = NULL;
52 ld_plugin_get_symbols get_symbols = NULL;
53 ld_plugin_add_input_file add_input_file = NULL;
Rafael Espindoladd76f182010-06-18 19:18:58 +000054 ld_plugin_add_input_library add_input_library = NULL;
Rafael Espindola11f403c2010-06-23 20:20:59 +000055 ld_plugin_set_extra_library_path set_extra_library_path = NULL;
Rafael Espindolae5782522011-04-07 21:11:00 +000056 ld_plugin_get_view get_view = NULL;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000057 ld_plugin_message message = discard_message;
58
59 int api_version = 0;
60 int gold_version = 0;
61
62 struct claimed_file {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000063 void *handle;
64 std::vector<ld_plugin_symbol> syms;
65 };
66
67 lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
Rafael Espindolac4b55612010-06-03 21:11:20 +000068 std::string output_name = "";
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000069 std::list<claimed_file> Modules;
70 std::vector<sys::Path> Cleanup;
Rafael Espindola8e04fc32011-02-20 18:28:29 +000071 lto_code_gen_t code_gen = NULL;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000072}
73
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000074namespace options {
Rafael Espindolac4b55612010-06-03 21:11:20 +000075 enum generate_bc { BC_NO, BC_ALSO, BC_ONLY };
Dan Gohmanf6920032010-04-16 00:42:57 +000076 static bool generate_api_file = false;
Rafael Espindolac4b55612010-06-03 21:11:20 +000077 static generate_bc generate_bc_file = BC_NO;
Rafael Espindola62bacd62010-05-13 13:39:31 +000078 static std::string bc_path;
Rafael Espindola5a287d72011-02-16 11:19:44 +000079 static std::string obj_path;
Rafael Espindola11f403c2010-06-23 20:20:59 +000080 static std::string extra_library_path;
Rafael Espindolacbb170d2010-08-09 21:09:46 +000081 static std::string triple;
Rafael Espindola2d643ef2010-08-11 00:15:13 +000082 static std::string mcpu;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000083 // Additional options to pass into the code generator.
Nick Lewyckyfc55def2010-06-03 17:10:17 +000084 // Note: This array will contain all plugin options which are not claimed
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000085 // as plugin exclusive to pass to the code generator.
Nick Lewyckyfc55def2010-06-03 17:10:17 +000086 // For example, "generate-api-file" and "as"options are for the plugin
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000087 // use only and will not be passed.
Dan Gohmanf6920032010-04-16 00:42:57 +000088 static std::vector<std::string> extra;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000089
Rafael Espindola6c809922010-06-07 16:45:22 +000090 static void process_plugin_option(const char* opt_)
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000091 {
Rafael Espindola6c809922010-06-07 16:45:22 +000092 if (opt_ == NULL)
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000093 return;
Rafael Espindola6c809922010-06-07 16:45:22 +000094 llvm::StringRef opt = opt_;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000095
Rafael Espindola6c809922010-06-07 16:45:22 +000096 if (opt == "generate-api-file") {
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000097 generate_api_file = true;
Rafael Espindola2d643ef2010-08-11 00:15:13 +000098 } else if (opt.startswith("mcpu=")) {
99 mcpu = opt.substr(strlen("mcpu="));
Rafael Espindola11f403c2010-06-23 20:20:59 +0000100 } else if (opt.startswith("extra-library-path=")) {
101 extra_library_path = opt.substr(strlen("extra_library_path="));
Rafael Espindola15af3872010-08-10 16:32:15 +0000102 } else if (opt.startswith("mtriple=")) {
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000103 triple = opt.substr(strlen("mtriple="));
Rafael Espindola5a287d72011-02-16 11:19:44 +0000104 } else if (opt.startswith("obj-path=")) {
105 obj_path = opt.substr(strlen("obj-path="));
Rafael Espindola6c809922010-06-07 16:45:22 +0000106 } else if (opt == "emit-llvm") {
Rafael Espindolac4b55612010-06-03 21:11:20 +0000107 generate_bc_file = BC_ONLY;
Rafael Espindola6c809922010-06-07 16:45:22 +0000108 } else if (opt == "also-emit-llvm") {
Rafael Espindolac4b55612010-06-03 21:11:20 +0000109 generate_bc_file = BC_ALSO;
Rafael Espindola6c809922010-06-07 16:45:22 +0000110 } else if (opt.startswith("also-emit-llvm=")) {
111 llvm::StringRef path = opt.substr(strlen("also-emit-llvm="));
Rafael Espindolac4b55612010-06-03 21:11:20 +0000112 generate_bc_file = BC_ALSO;
Nick Lewycky662f7382010-06-03 17:13:23 +0000113 if (!bc_path.empty()) {
Rafael Espindola62bacd62010-05-13 13:39:31 +0000114 (*message)(LDPL_WARNING, "Path to the output IL file specified twice. "
Rafael Espindola6c809922010-06-07 16:45:22 +0000115 "Discarding %s", opt_);
Rafael Espindola62bacd62010-05-13 13:39:31 +0000116 } else {
117 bc_path = path;
118 }
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000119 } else {
120 // Save this option to pass to the code generator.
Rafael Espindola6c809922010-06-07 16:45:22 +0000121 extra.push_back(opt);
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000122 }
123 }
124}
125
Dan Gohmanf6920032010-04-16 00:42:57 +0000126static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
127 int *claimed);
128static ld_plugin_status all_symbols_read_hook(void);
129static ld_plugin_status cleanup_hook(void);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000130
131extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
132ld_plugin_status onload(ld_plugin_tv *tv) {
133 // We're given a pointer to the first transfer vector. We read through them
134 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
135 // contain pointers to functions that we need to call to register our own
136 // hooks. The others are addresses of functions we can use to call into gold
137 // for services.
138
139 bool registeredClaimFile = false;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000140
141 for (; tv->tv_tag != LDPT_NULL; ++tv) {
142 switch (tv->tv_tag) {
143 case LDPT_API_VERSION:
144 api_version = tv->tv_u.tv_val;
145 break;
146 case LDPT_GOLD_VERSION: // major * 100 + minor
147 gold_version = tv->tv_u.tv_val;
148 break;
Rafael Espindolac4b55612010-06-03 21:11:20 +0000149 case LDPT_OUTPUT_NAME:
150 output_name = tv->tv_u.tv_string;
151 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000152 case LDPT_LINKER_OUTPUT:
153 switch (tv->tv_u.tv_val) {
154 case LDPO_REL: // .o
155 case LDPO_DYN: // .so
Rafael Espindola4f817682012-06-13 13:30:24 +0000156 // FIXME: Replace 3 with LDPO_PIE once that is in a released binutils.
157 case 3: // position independent executable
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000158 output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
159 break;
160 case LDPO_EXEC: // .exe
161 output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
162 break;
163 default:
164 (*message)(LDPL_ERROR, "Unknown output file type %d",
165 tv->tv_u.tv_val);
166 return LDPS_ERR;
167 }
168 // TODO: add an option to disable PIC.
169 //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
170 break;
171 case LDPT_OPTION:
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000172 options::process_plugin_option(tv->tv_u.tv_string);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000173 break;
174 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
175 ld_plugin_register_claim_file callback;
176 callback = tv->tv_u.tv_register_claim_file;
177
178 if ((*callback)(claim_file_hook) != LDPS_OK)
179 return LDPS_ERR;
180
181 registeredClaimFile = true;
182 } break;
183 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
184 ld_plugin_register_all_symbols_read callback;
185 callback = tv->tv_u.tv_register_all_symbols_read;
186
187 if ((*callback)(all_symbols_read_hook) != LDPS_OK)
188 return LDPS_ERR;
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000189
190 code_gen = lto_codegen_create();
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000191 } break;
192 case LDPT_REGISTER_CLEANUP_HOOK: {
193 ld_plugin_register_cleanup callback;
194 callback = tv->tv_u.tv_register_cleanup;
195
196 if ((*callback)(cleanup_hook) != LDPS_OK)
197 return LDPS_ERR;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000198 } break;
199 case LDPT_ADD_SYMBOLS:
200 add_symbols = tv->tv_u.tv_add_symbols;
201 break;
202 case LDPT_GET_SYMBOLS:
203 get_symbols = tv->tv_u.tv_get_symbols;
204 break;
205 case LDPT_ADD_INPUT_FILE:
206 add_input_file = tv->tv_u.tv_add_input_file;
207 break;
Rafael Espindoladd76f182010-06-18 19:18:58 +0000208 case LDPT_ADD_INPUT_LIBRARY:
209 add_input_library = tv->tv_u.tv_add_input_file;
210 break;
Rafael Espindola11f403c2010-06-23 20:20:59 +0000211 case LDPT_SET_EXTRA_LIBRARY_PATH:
212 set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
213 break;
Rafael Espindolae5782522011-04-07 21:11:00 +0000214 case LDPT_GET_VIEW:
215 get_view = tv->tv_u.tv_get_view;
216 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000217 case LDPT_MESSAGE:
218 message = tv->tv_u.tv_message;
219 break;
220 default:
221 break;
222 }
223 }
224
Rafael Espindola98c507e2009-02-18 08:30:15 +0000225 if (!registeredClaimFile) {
Rafael Espindola6210a942009-02-18 17:49:06 +0000226 (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
227 return LDPS_ERR;
228 }
Rafael Espindola98c507e2009-02-18 08:30:15 +0000229 if (!add_symbols) {
Rafael Espindola6210a942009-02-18 17:49:06 +0000230 (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
231 return LDPS_ERR;
232 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000233
234 return LDPS_OK;
235}
236
237/// claim_file_hook - called by gold to see whether this file is one that
238/// our plugin can handle. We'll try to open it and register all the symbols
239/// with add_symbol if possible.
Dan Gohmanf6920032010-04-16 00:42:57 +0000240static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
241 int *claimed) {
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000242 lto_module_t M;
Ivan Krasinccb7c902011-09-12 21:47:50 +0000243 const void *view;
244 OwningPtr<MemoryBuffer> buffer;
Rafael Espindolae5782522011-04-07 21:11:00 +0000245 if (get_view) {
Rafael Espindolae5782522011-04-07 21:11:00 +0000246 if (get_view(file->handle, &view) != LDPS_OK) {
247 (*message)(LDPL_ERROR, "Failed to get a view of %s", file->name);
248 return LDPS_ERR;
249 }
Ivan Krasinccb7c902011-09-12 21:47:50 +0000250 } else {
Ivan Krasin71280b52011-09-15 23:13:00 +0000251 int64_t offset = 0;
Nick Lewyckyc1da8862009-02-05 04:14:23 +0000252 // Gold has found what might be IR part-way inside of a file, such as
253 // an .a archive.
Ivan Krasinccb7c902011-09-12 21:47:50 +0000254 if (file->offset) {
255 offset = file->offset;
256 }
257 if (error_code ec =
258 MemoryBuffer::getOpenFile(file->fd, file->name, buffer, file->filesize,
259 -1, offset, false)) {
260 (*message)(LDPL_ERROR, ec.message().c_str());
261 return LDPS_ERR;
262 }
263 view = buffer->getBufferStart();
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000264 }
Ivan Krasinccb7c902011-09-12 21:47:50 +0000265
266 if (!lto_module_is_object_file_in_memory(view, file->filesize))
267 return LDPS_OK;
268
269 M = lto_module_create_from_memory(view, file->filesize);
Ivan Krasinc170f5f2011-09-09 00:14:04 +0000270 if (!M) {
271 if (const char* msg = lto_get_error_message()) {
Ivan Krasinccb7c902011-09-12 21:47:50 +0000272 (*message)(LDPL_ERROR,
273 "LLVM gold plugin has failed to create LTO module: %s",
274 msg);
Ivan Krasinc170f5f2011-09-09 00:14:04 +0000275 return LDPS_ERR;
276 }
Rafael Espindolaf21b1052011-03-17 00:36:11 +0000277 return LDPS_OK;
Ivan Krasinc170f5f2011-09-09 00:14:04 +0000278 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000279
280 *claimed = 1;
281 Modules.resize(Modules.size() + 1);
282 claimed_file &cf = Modules.back();
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000283
284 if (!options::triple.empty())
Rafael Espindola37d42f82011-02-19 21:49:57 +0000285 lto_module_set_target_triple(M, options::triple.c_str());
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000286
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000287 cf.handle = file->handle;
Rafael Espindola37d42f82011-02-19 21:49:57 +0000288 unsigned sym_count = lto_module_get_num_symbols(M);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000289 cf.syms.reserve(sym_count);
290
291 for (unsigned i = 0; i != sym_count; ++i) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000292 lto_symbol_attributes attrs = lto_module_get_symbol_attribute(M, i);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000293 if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
294 continue;
295
296 cf.syms.push_back(ld_plugin_symbol());
297 ld_plugin_symbol &sym = cf.syms.back();
Rafael Espindola37d42f82011-02-19 21:49:57 +0000298 sym.name = const_cast<char *>(lto_module_get_symbol_name(M, i));
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000299 sym.name = strdup(sym.name);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000300 sym.version = NULL;
301
302 int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
303 switch (scope) {
304 case LTO_SYMBOL_SCOPE_HIDDEN:
305 sym.visibility = LDPV_HIDDEN;
306 break;
307 case LTO_SYMBOL_SCOPE_PROTECTED:
308 sym.visibility = LDPV_PROTECTED;
309 break;
310 case 0: // extern
311 case LTO_SYMBOL_SCOPE_DEFAULT:
312 sym.visibility = LDPV_DEFAULT;
313 break;
314 default:
315 (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
316 return LDPS_ERR;
317 }
318
319 int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000320 sym.comdat_key = NULL;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000321 switch (definition) {
322 case LTO_SYMBOL_DEFINITION_REGULAR:
323 sym.def = LDPK_DEF;
324 break;
325 case LTO_SYMBOL_DEFINITION_UNDEFINED:
326 sym.def = LDPK_UNDEF;
327 break;
328 case LTO_SYMBOL_DEFINITION_TENTATIVE:
329 sym.def = LDPK_COMMON;
330 break;
331 case LTO_SYMBOL_DEFINITION_WEAK:
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000332 sym.comdat_key = sym.name;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000333 sym.def = LDPK_WEAKDEF;
334 break;
Rafael Espindola7431af02009-04-24 16:55:21 +0000335 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
336 sym.def = LDPK_WEAKUNDEF;
337 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000338 default:
339 (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition);
340 return LDPS_ERR;
341 }
342
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000343 sym.size = 0;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000344
345 sym.resolution = LDPR_UNKNOWN;
346 }
347
348 cf.syms.reserve(cf.syms.size());
349
350 if (!cf.syms.empty()) {
351 if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
352 (*message)(LDPL_ERROR, "Unable to add symbols!");
353 return LDPS_ERR;
354 }
355 }
356
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000357 if (code_gen)
358 lto_codegen_add_module(code_gen, M);
359
360 lto_module_dispose(M);
361
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000362 return LDPS_OK;
363}
364
365/// all_symbols_read_hook - gold informs us that all symbols have been read.
366/// At this point, we use get_symbols to see if any of our definitions have
367/// been overridden by a native object file. Then, perform optimization and
368/// codegen.
Dan Gohmanf6920032010-04-16 00:42:57 +0000369static ld_plugin_status all_symbols_read_hook(void) {
Nick Lewyckyca428622009-02-22 22:15:44 +0000370 std::ofstream api_file;
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000371 assert(code_gen);
372
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000373 if (options::generate_api_file) {
Nick Lewyckyca428622009-02-22 22:15:44 +0000374 api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
375 if (!api_file.is_open()) {
376 (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing.");
377 abort();
378 }
379 }
380
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000381 // If we don't preserve any symbols, libLTO will assume that all symbols are
382 // needed. Keep all symbols unless we're producing a final executable.
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000383 bool anySymbolsPreserved = false;
384 for (std::list<claimed_file>::iterator I = Modules.begin(),
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000385 E = Modules.end(); I != E; ++I) {
Nick Lewycky5e0ac582011-07-26 08:40:36 +0000386 if (I->syms.empty())
387 continue;
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000388 (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
389 for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
390 if (I->syms[i].resolution == LDPR_PREVAILING_DEF) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000391 lto_codegen_add_must_preserve_symbol(code_gen, I->syms[i].name);
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000392 anySymbolsPreserved = true;
Nick Lewyckyca428622009-02-22 22:15:44 +0000393
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000394 if (options::generate_api_file)
395 api_file << I->syms[i].name << "\n";
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000396 }
397 }
Rafael Espindola38a979b2010-06-14 21:20:52 +0000398 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000399
Rafael Espindola38a979b2010-06-14 21:20:52 +0000400 if (options::generate_api_file)
401 api_file.close();
Nick Lewyckyca428622009-02-22 22:15:44 +0000402
Rafael Espindola38a979b2010-06-14 21:20:52 +0000403 if (!anySymbolsPreserved) {
404 // All of the IL is unnecessary!
Rafael Espindola37d42f82011-02-19 21:49:57 +0000405 lto_codegen_dispose(code_gen);
Rafael Espindola38a979b2010-06-14 21:20:52 +0000406 return LDPS_OK;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000407 }
408
Rafael Espindola37d42f82011-02-19 21:49:57 +0000409 lto_codegen_set_pic_model(code_gen, output_type);
410 lto_codegen_set_debug_model(code_gen, LTO_DEBUG_MODEL_DWARF);
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000411 if (!options::mcpu.empty())
Rafael Espindola37d42f82011-02-19 21:49:57 +0000412 lto_codegen_set_cpu(code_gen, options::mcpu.c_str());
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000413
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000414 // Pass through extra options to the code generator.
415 if (!options::extra.empty()) {
416 for (std::vector<std::string>::iterator it = options::extra.begin();
417 it != options::extra.end(); ++it) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000418 lto_codegen_debug_options(code_gen, (*it).c_str());
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000419 }
420 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000421
Rafael Espindolac4b55612010-06-03 21:11:20 +0000422 if (options::generate_bc_file != options::BC_NO) {
423 std::string path;
424 if (options::generate_bc_file == options::BC_ONLY)
425 path = output_name;
426 else if (!options::bc_path.empty())
427 path = options::bc_path;
428 else
429 path = output_name + ".bc";
Rafael Espindola37d42f82011-02-19 21:49:57 +0000430 bool err = lto_codegen_write_merged_modules(code_gen, path.c_str());
Rafael Espindola62bacd62010-05-13 13:39:31 +0000431 if (err)
432 (*message)(LDPL_FATAL, "Failed to write the output file.");
Rafael Espindolac4b55612010-06-03 21:11:20 +0000433 if (options::generate_bc_file == options::BC_ONLY)
434 exit(0);
Rafael Espindola62bacd62010-05-13 13:39:31 +0000435 }
Rafael Espindola5a287d72011-02-16 11:19:44 +0000436 const char *objPath;
Rafael Espindola6421a882011-03-22 20:57:13 +0000437 if (lto_codegen_compile_to_file(code_gen, &objPath)) {
438 (*message)(LDPL_ERROR, "Could not produce a combined object file\n");
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000439 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000440
Rafael Espindola37d42f82011-02-19 21:49:57 +0000441 lto_codegen_dispose(code_gen);
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000442 for (std::list<claimed_file>::iterator I = Modules.begin(),
443 E = Modules.end(); I != E; ++I) {
444 for (unsigned i = 0; i != I->syms.size(); ++i) {
445 ld_plugin_symbol &sym = I->syms[i];
446 free(sym.name);
447 }
448 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000449
Rafael Espindola5a287d72011-02-16 11:19:44 +0000450 if ((*add_input_file)(objPath) != LDPS_OK) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000451 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
Rafael Espindola5a287d72011-02-16 11:19:44 +0000452 (*message)(LDPL_ERROR, "File left behind in: %s", objPath);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000453 return LDPS_ERR;
454 }
455
Rafael Espindola11f403c2010-06-23 20:20:59 +0000456 if (!options::extra_library_path.empty() &&
457 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) {
458 (*message)(LDPL_ERROR, "Unable to set the extra library path.");
459 return LDPS_ERR;
460 }
461
Rafael Espindola5a287d72011-02-16 11:19:44 +0000462 if (options::obj_path.empty())
463 Cleanup.push_back(sys::Path(objPath));
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000464
465 return LDPS_OK;
466}
467
Dan Gohmanf6920032010-04-16 00:42:57 +0000468static ld_plugin_status cleanup_hook(void) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000469 std::string ErrMsg;
470
471 for (int i = 0, e = Cleanup.size(); i != e; ++i)
472 if (Cleanup[i].eraseFromDisk(false, &ErrMsg))
473 (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),
474 ErrMsg.c_str());
475
476 return LDPS_OK;
477}