blob: 7aa8c9109f64070206f8588885d0e5302908be35 [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
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000020#include "llvm/Support/ToolOutputFile.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000021#include "llvm/Support/Errno.h"
22#include "llvm/Support/Path.h"
23#include "llvm/Support/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
Michael J. Spencer8b1659e2011-01-20 05:43:16 +000032// Support Windows/MinGW crazyness.
33#ifdef _WIN32
34# include <io.h>
35# define lseek _lseek
36# define read _read
37#endif
38
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000039using namespace llvm;
40
41namespace {
42 ld_plugin_status discard_message(int level, const char *format, ...) {
43 // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
44 // callback in the transfer vector. This should never be called.
45 abort();
46 }
47
48 ld_plugin_add_symbols add_symbols = NULL;
49 ld_plugin_get_symbols get_symbols = NULL;
50 ld_plugin_add_input_file add_input_file = NULL;
Rafael Espindoladd76f182010-06-18 19:18:58 +000051 ld_plugin_add_input_library add_input_library = NULL;
Rafael Espindola11f403c2010-06-23 20:20:59 +000052 ld_plugin_set_extra_library_path set_extra_library_path = NULL;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000053 ld_plugin_message message = discard_message;
54
55 int api_version = 0;
56 int gold_version = 0;
57
58 struct claimed_file {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000059 void *handle;
60 std::vector<ld_plugin_symbol> syms;
61 };
62
63 lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
Rafael Espindolac4b55612010-06-03 21:11:20 +000064 std::string output_name = "";
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000065 std::list<claimed_file> Modules;
66 std::vector<sys::Path> Cleanup;
Rafael Espindola8e04fc32011-02-20 18:28:29 +000067 lto_code_gen_t code_gen = NULL;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000068}
69
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000070namespace options {
Rafael Espindolac4b55612010-06-03 21:11:20 +000071 enum generate_bc { BC_NO, BC_ALSO, BC_ONLY };
Dan Gohmanf6920032010-04-16 00:42:57 +000072 static bool generate_api_file = false;
Rafael Espindolac4b55612010-06-03 21:11:20 +000073 static generate_bc generate_bc_file = BC_NO;
Rafael Espindola62bacd62010-05-13 13:39:31 +000074 static std::string bc_path;
Rafael Espindola5a287d72011-02-16 11:19:44 +000075 static std::string obj_path;
Rafael Espindola11f403c2010-06-23 20:20:59 +000076 static std::string extra_library_path;
Rafael Espindolacbb170d2010-08-09 21:09:46 +000077 static std::string triple;
Rafael Espindola2d643ef2010-08-11 00:15:13 +000078 static std::string mcpu;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000079 // Additional options to pass into the code generator.
Nick Lewyckyfc55def2010-06-03 17:10:17 +000080 // Note: This array will contain all plugin options which are not claimed
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000081 // as plugin exclusive to pass to the code generator.
Nick Lewyckyfc55def2010-06-03 17:10:17 +000082 // For example, "generate-api-file" and "as"options are for the plugin
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000083 // use only and will not be passed.
Dan Gohmanf6920032010-04-16 00:42:57 +000084 static std::vector<std::string> extra;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000085
Rafael Espindola6c809922010-06-07 16:45:22 +000086 static void process_plugin_option(const char* opt_)
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000087 {
Rafael Espindola6c809922010-06-07 16:45:22 +000088 if (opt_ == NULL)
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000089 return;
Rafael Espindola6c809922010-06-07 16:45:22 +000090 llvm::StringRef opt = opt_;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000091
Rafael Espindola6c809922010-06-07 16:45:22 +000092 if (opt == "generate-api-file") {
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000093 generate_api_file = true;
Rafael Espindola2d643ef2010-08-11 00:15:13 +000094 } else if (opt.startswith("mcpu=")) {
95 mcpu = opt.substr(strlen("mcpu="));
Rafael Espindola11f403c2010-06-23 20:20:59 +000096 } else if (opt.startswith("extra-library-path=")) {
97 extra_library_path = opt.substr(strlen("extra_library_path="));
Rafael Espindola15af3872010-08-10 16:32:15 +000098 } else if (opt.startswith("mtriple=")) {
Rafael Espindolacbb170d2010-08-09 21:09:46 +000099 triple = opt.substr(strlen("mtriple="));
Rafael Espindola5a287d72011-02-16 11:19:44 +0000100 } else if (opt.startswith("obj-path=")) {
101 obj_path = opt.substr(strlen("obj-path="));
Rafael Espindola6c809922010-06-07 16:45:22 +0000102 } else if (opt == "emit-llvm") {
Rafael Espindolac4b55612010-06-03 21:11:20 +0000103 generate_bc_file = BC_ONLY;
Rafael Espindola6c809922010-06-07 16:45:22 +0000104 } else if (opt == "also-emit-llvm") {
Rafael Espindolac4b55612010-06-03 21:11:20 +0000105 generate_bc_file = BC_ALSO;
Rafael Espindola6c809922010-06-07 16:45:22 +0000106 } else if (opt.startswith("also-emit-llvm=")) {
107 llvm::StringRef path = opt.substr(strlen("also-emit-llvm="));
Rafael Espindolac4b55612010-06-03 21:11:20 +0000108 generate_bc_file = BC_ALSO;
Nick Lewycky662f7382010-06-03 17:13:23 +0000109 if (!bc_path.empty()) {
Rafael Espindola62bacd62010-05-13 13:39:31 +0000110 (*message)(LDPL_WARNING, "Path to the output IL file specified twice. "
Rafael Espindola6c809922010-06-07 16:45:22 +0000111 "Discarding %s", opt_);
Rafael Espindola62bacd62010-05-13 13:39:31 +0000112 } else {
113 bc_path = path;
114 }
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000115 } else {
116 // Save this option to pass to the code generator.
Rafael Espindola6c809922010-06-07 16:45:22 +0000117 extra.push_back(opt);
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000118 }
119 }
120}
121
Dan Gohmanf6920032010-04-16 00:42:57 +0000122static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
123 int *claimed);
124static ld_plugin_status all_symbols_read_hook(void);
125static ld_plugin_status cleanup_hook(void);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000126
127extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
128ld_plugin_status onload(ld_plugin_tv *tv) {
129 // We're given a pointer to the first transfer vector. We read through them
130 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
131 // contain pointers to functions that we need to call to register our own
132 // hooks. The others are addresses of functions we can use to call into gold
133 // for services.
134
135 bool registeredClaimFile = false;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000136
137 for (; tv->tv_tag != LDPT_NULL; ++tv) {
138 switch (tv->tv_tag) {
139 case LDPT_API_VERSION:
140 api_version = tv->tv_u.tv_val;
141 break;
142 case LDPT_GOLD_VERSION: // major * 100 + minor
143 gold_version = tv->tv_u.tv_val;
144 break;
Rafael Espindolac4b55612010-06-03 21:11:20 +0000145 case LDPT_OUTPUT_NAME:
146 output_name = tv->tv_u.tv_string;
147 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000148 case LDPT_LINKER_OUTPUT:
149 switch (tv->tv_u.tv_val) {
150 case LDPO_REL: // .o
151 case LDPO_DYN: // .so
152 output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
153 break;
154 case LDPO_EXEC: // .exe
155 output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
156 break;
157 default:
158 (*message)(LDPL_ERROR, "Unknown output file type %d",
159 tv->tv_u.tv_val);
160 return LDPS_ERR;
161 }
162 // TODO: add an option to disable PIC.
163 //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
164 break;
165 case LDPT_OPTION:
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000166 options::process_plugin_option(tv->tv_u.tv_string);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000167 break;
168 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
169 ld_plugin_register_claim_file callback;
170 callback = tv->tv_u.tv_register_claim_file;
171
172 if ((*callback)(claim_file_hook) != LDPS_OK)
173 return LDPS_ERR;
174
175 registeredClaimFile = true;
176 } break;
177 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
178 ld_plugin_register_all_symbols_read callback;
179 callback = tv->tv_u.tv_register_all_symbols_read;
180
181 if ((*callback)(all_symbols_read_hook) != LDPS_OK)
182 return LDPS_ERR;
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000183
184 code_gen = lto_codegen_create();
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000185 } break;
186 case LDPT_REGISTER_CLEANUP_HOOK: {
187 ld_plugin_register_cleanup callback;
188 callback = tv->tv_u.tv_register_cleanup;
189
190 if ((*callback)(cleanup_hook) != LDPS_OK)
191 return LDPS_ERR;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000192 } break;
193 case LDPT_ADD_SYMBOLS:
194 add_symbols = tv->tv_u.tv_add_symbols;
195 break;
196 case LDPT_GET_SYMBOLS:
197 get_symbols = tv->tv_u.tv_get_symbols;
198 break;
199 case LDPT_ADD_INPUT_FILE:
200 add_input_file = tv->tv_u.tv_add_input_file;
201 break;
Rafael Espindoladd76f182010-06-18 19:18:58 +0000202 case LDPT_ADD_INPUT_LIBRARY:
203 add_input_library = tv->tv_u.tv_add_input_file;
204 break;
Rafael Espindola11f403c2010-06-23 20:20:59 +0000205 case LDPT_SET_EXTRA_LIBRARY_PATH:
206 set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
207 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000208 case LDPT_MESSAGE:
209 message = tv->tv_u.tv_message;
210 break;
211 default:
212 break;
213 }
214 }
215
Rafael Espindola98c507e2009-02-18 08:30:15 +0000216 if (!registeredClaimFile) {
Rafael Espindola6210a942009-02-18 17:49:06 +0000217 (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
218 return LDPS_ERR;
219 }
Rafael Espindola98c507e2009-02-18 08:30:15 +0000220 if (!add_symbols) {
Rafael Espindola6210a942009-02-18 17:49:06 +0000221 (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
222 return LDPS_ERR;
223 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000224
225 return LDPS_OK;
226}
227
228/// claim_file_hook - called by gold to see whether this file is one that
229/// our plugin can handle. We'll try to open it and register all the symbols
230/// with add_symbol if possible.
Dan Gohmanf6920032010-04-16 00:42:57 +0000231static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
232 int *claimed) {
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000233 lto_module_t M;
234
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000235 if (file->offset) {
Nick Lewyckyc1da8862009-02-05 04:14:23 +0000236 // Gold has found what might be IR part-way inside of a file, such as
237 // an .a archive.
Rafael Espindolaf21b1052011-03-17 00:36:11 +0000238 M = lto_module_create_from_fd_at_offset(file->fd, file->name, -1,
239 file->filesize, file->offset);
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000240 } else {
Rafael Espindola35358e02011-02-27 20:15:37 +0000241 M = lto_module_create_from_fd(file->fd, file->name, file->filesize);
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000242 }
Rafael Espindolaf21b1052011-03-17 00:36:11 +0000243 if (!M)
244 return LDPS_OK;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000245
246 *claimed = 1;
247 Modules.resize(Modules.size() + 1);
248 claimed_file &cf = Modules.back();
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000249
250 if (!options::triple.empty())
Rafael Espindola37d42f82011-02-19 21:49:57 +0000251 lto_module_set_target_triple(M, options::triple.c_str());
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000252
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000253 cf.handle = file->handle;
Rafael Espindola37d42f82011-02-19 21:49:57 +0000254 unsigned sym_count = lto_module_get_num_symbols(M);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000255 cf.syms.reserve(sym_count);
256
257 for (unsigned i = 0; i != sym_count; ++i) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000258 lto_symbol_attributes attrs = lto_module_get_symbol_attribute(M, i);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000259 if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
260 continue;
261
262 cf.syms.push_back(ld_plugin_symbol());
263 ld_plugin_symbol &sym = cf.syms.back();
Rafael Espindola37d42f82011-02-19 21:49:57 +0000264 sym.name = const_cast<char *>(lto_module_get_symbol_name(M, i));
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000265 sym.name = strdup(sym.name);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000266 sym.version = NULL;
267
268 int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
269 switch (scope) {
270 case LTO_SYMBOL_SCOPE_HIDDEN:
271 sym.visibility = LDPV_HIDDEN;
272 break;
273 case LTO_SYMBOL_SCOPE_PROTECTED:
274 sym.visibility = LDPV_PROTECTED;
275 break;
276 case 0: // extern
277 case LTO_SYMBOL_SCOPE_DEFAULT:
278 sym.visibility = LDPV_DEFAULT;
279 break;
280 default:
281 (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
282 return LDPS_ERR;
283 }
284
285 int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000286 sym.comdat_key = NULL;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000287 switch (definition) {
288 case LTO_SYMBOL_DEFINITION_REGULAR:
289 sym.def = LDPK_DEF;
290 break;
291 case LTO_SYMBOL_DEFINITION_UNDEFINED:
292 sym.def = LDPK_UNDEF;
293 break;
294 case LTO_SYMBOL_DEFINITION_TENTATIVE:
295 sym.def = LDPK_COMMON;
296 break;
297 case LTO_SYMBOL_DEFINITION_WEAK:
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000298 sym.comdat_key = sym.name;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000299 sym.def = LDPK_WEAKDEF;
300 break;
Rafael Espindola7431af02009-04-24 16:55:21 +0000301 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
302 sym.def = LDPK_WEAKUNDEF;
303 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000304 default:
305 (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition);
306 return LDPS_ERR;
307 }
308
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000309 sym.size = 0;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000310
311 sym.resolution = LDPR_UNKNOWN;
312 }
313
314 cf.syms.reserve(cf.syms.size());
315
316 if (!cf.syms.empty()) {
317 if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
318 (*message)(LDPL_ERROR, "Unable to add symbols!");
319 return LDPS_ERR;
320 }
321 }
322
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000323 if (code_gen)
324 lto_codegen_add_module(code_gen, M);
325
326 lto_module_dispose(M);
327
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000328 return LDPS_OK;
329}
330
331/// all_symbols_read_hook - gold informs us that all symbols have been read.
332/// At this point, we use get_symbols to see if any of our definitions have
333/// been overridden by a native object file. Then, perform optimization and
334/// codegen.
Dan Gohmanf6920032010-04-16 00:42:57 +0000335static ld_plugin_status all_symbols_read_hook(void) {
Nick Lewyckyca428622009-02-22 22:15:44 +0000336 std::ofstream api_file;
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000337 assert(code_gen);
338
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000339 if (options::generate_api_file) {
Nick Lewyckyca428622009-02-22 22:15:44 +0000340 api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
341 if (!api_file.is_open()) {
342 (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing.");
343 abort();
344 }
345 }
346
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000347 // If we don't preserve any symbols, libLTO will assume that all symbols are
348 // needed. Keep all symbols unless we're producing a final executable.
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000349 bool anySymbolsPreserved = false;
350 for (std::list<claimed_file>::iterator I = Modules.begin(),
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000351 E = Modules.end(); I != E; ++I) {
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000352 (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
353 for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
354 if (I->syms[i].resolution == LDPR_PREVAILING_DEF) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000355 lto_codegen_add_must_preserve_symbol(code_gen, I->syms[i].name);
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000356 anySymbolsPreserved = true;
Nick Lewyckyca428622009-02-22 22:15:44 +0000357
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000358 if (options::generate_api_file)
359 api_file << I->syms[i].name << "\n";
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000360 }
361 }
Rafael Espindola38a979b2010-06-14 21:20:52 +0000362 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000363
Rafael Espindola38a979b2010-06-14 21:20:52 +0000364 if (options::generate_api_file)
365 api_file.close();
Nick Lewyckyca428622009-02-22 22:15:44 +0000366
Rafael Espindola38a979b2010-06-14 21:20:52 +0000367 if (!anySymbolsPreserved) {
368 // All of the IL is unnecessary!
Rafael Espindola37d42f82011-02-19 21:49:57 +0000369 lto_codegen_dispose(code_gen);
Rafael Espindola38a979b2010-06-14 21:20:52 +0000370 return LDPS_OK;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000371 }
372
Rafael Espindola37d42f82011-02-19 21:49:57 +0000373 lto_codegen_set_pic_model(code_gen, output_type);
374 lto_codegen_set_debug_model(code_gen, LTO_DEBUG_MODEL_DWARF);
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000375 if (!options::mcpu.empty())
Rafael Espindola37d42f82011-02-19 21:49:57 +0000376 lto_codegen_set_cpu(code_gen, options::mcpu.c_str());
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000377
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000378 // Pass through extra options to the code generator.
379 if (!options::extra.empty()) {
380 for (std::vector<std::string>::iterator it = options::extra.begin();
381 it != options::extra.end(); ++it) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000382 lto_codegen_debug_options(code_gen, (*it).c_str());
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000383 }
384 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000385
Rafael Espindolac4b55612010-06-03 21:11:20 +0000386 if (options::generate_bc_file != options::BC_NO) {
387 std::string path;
388 if (options::generate_bc_file == options::BC_ONLY)
389 path = output_name;
390 else if (!options::bc_path.empty())
391 path = options::bc_path;
392 else
393 path = output_name + ".bc";
Rafael Espindola37d42f82011-02-19 21:49:57 +0000394 bool err = lto_codegen_write_merged_modules(code_gen, path.c_str());
Rafael Espindola62bacd62010-05-13 13:39:31 +0000395 if (err)
396 (*message)(LDPL_FATAL, "Failed to write the output file.");
Rafael Espindolac4b55612010-06-03 21:11:20 +0000397 if (options::generate_bc_file == options::BC_ONLY)
398 exit(0);
Rafael Espindola62bacd62010-05-13 13:39:31 +0000399 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000400 size_t bufsize = 0;
Rafael Espindola37d42f82011-02-19 21:49:57 +0000401 const char *buffer = static_cast<const char *>(lto_codegen_compile(code_gen,
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000402 &bufsize));
403
404 std::string ErrMsg;
405
Rafael Espindola5a287d72011-02-16 11:19:44 +0000406 const char *objPath;
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000407 sys::Path uniqueObjPath("/tmp/llvmgold.o");
Rafael Espindola5a287d72011-02-16 11:19:44 +0000408 if (!options::obj_path.empty()) {
409 objPath = options::obj_path.c_str();
410 } else {
Rafael Espindola5a287d72011-02-16 11:19:44 +0000411 if (uniqueObjPath.createTemporaryFileOnDisk(true, &ErrMsg)) {
412 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
413 return LDPS_ERR;
414 }
415 objPath = uniqueObjPath.c_str();
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000416 }
Rafael Espindola5a287d72011-02-16 11:19:44 +0000417 tool_output_file objFile(objPath, ErrMsg,
418 raw_fd_ostream::F_Binary);
419 if (!ErrMsg.empty()) {
420 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
421 return LDPS_ERR;
422 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000423
Nick Lewycky59b4d2a2010-09-02 05:44:31 +0000424 objFile.os().write(buffer, bufsize);
425 objFile.os().close();
426 if (objFile.os().has_error()) {
Dan Gohmanf2914012010-08-20 16:59:15 +0000427 (*message)(LDPL_ERROR, "Error writing output file '%s'",
Rafael Espindola5a287d72011-02-16 11:19:44 +0000428 objPath);
Nick Lewycky59b4d2a2010-09-02 05:44:31 +0000429 objFile.os().clear_error();
Dan Gohmanf2914012010-08-20 16:59:15 +0000430 return LDPS_ERR;
431 }
432 objFile.keep();
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000433
Rafael Espindola37d42f82011-02-19 21:49:57 +0000434 lto_codegen_dispose(code_gen);
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000435 for (std::list<claimed_file>::iterator I = Modules.begin(),
436 E = Modules.end(); I != E; ++I) {
437 for (unsigned i = 0; i != I->syms.size(); ++i) {
438 ld_plugin_symbol &sym = I->syms[i];
439 free(sym.name);
440 }
441 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000442
Rafael Espindola5a287d72011-02-16 11:19:44 +0000443 if ((*add_input_file)(objPath) != LDPS_OK) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000444 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
Rafael Espindola5a287d72011-02-16 11:19:44 +0000445 (*message)(LDPL_ERROR, "File left behind in: %s", objPath);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000446 return LDPS_ERR;
447 }
448
Rafael Espindola11f403c2010-06-23 20:20:59 +0000449 if (!options::extra_library_path.empty() &&
450 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) {
451 (*message)(LDPL_ERROR, "Unable to set the extra library path.");
452 return LDPS_ERR;
453 }
454
Rafael Espindola5a287d72011-02-16 11:19:44 +0000455 if (options::obj_path.empty())
456 Cleanup.push_back(sys::Path(objPath));
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000457
458 return LDPS_OK;
459}
460
Dan Gohmanf6920032010-04-16 00:42:57 +0000461static ld_plugin_status cleanup_hook(void) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000462 std::string ErrMsg;
463
464 for (int i = 0, e = Cleanup.size(); i != e; ++i)
465 if (Cleanup[i].eraseFromDisk(false, &ErrMsg))
466 (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),
467 ErrMsg.c_str());
468
469 return LDPS_OK;
470}