blob: 2d0f5bd3af3ad762590c54c1774806de29cb26c8 [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;
Rafael Espindoladd76f182010-06-18 19:18:58 +000044 ld_plugin_add_input_library add_input_library = NULL;
Rafael Espindola11f403c2010-06-23 20:20:59 +000045 ld_plugin_set_extra_library_path set_extra_library_path = NULL;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000046 ld_plugin_message message = discard_message;
47
48 int api_version = 0;
49 int gold_version = 0;
50
51 struct claimed_file {
52 lto_module_t M;
53 void *handle;
54 std::vector<ld_plugin_symbol> syms;
55 };
56
57 lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
Rafael Espindolac4b55612010-06-03 21:11:20 +000058 std::string output_name = "";
Nick Lewycky3e62b2d2009-02-03 07:13:24 +000059 std::list<claimed_file> Modules;
60 std::vector<sys::Path> Cleanup;
61}
62
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000063namespace options {
Rafael Espindolac4b55612010-06-03 21:11:20 +000064 enum generate_bc { BC_NO, BC_ALSO, BC_ONLY };
Dan Gohmanf6920032010-04-16 00:42:57 +000065 static bool generate_api_file = false;
Rafael Espindolac4b55612010-06-03 21:11:20 +000066 static generate_bc generate_bc_file = BC_NO;
Rafael Espindola62bacd62010-05-13 13:39:31 +000067 static std::string bc_path;
Rafael Espindola6c809922010-06-07 16:45:22 +000068 static std::string as_path;
Rafael Espindoladd76f182010-06-18 19:18:58 +000069 static std::vector<std::string> pass_through;
Rafael Espindola11f403c2010-06-23 20:20:59 +000070 static std::string extra_library_path;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000071 // Additional options to pass into the code generator.
Nick Lewyckyfc55def2010-06-03 17:10:17 +000072 // Note: This array will contain all plugin options which are not claimed
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000073 // as plugin exclusive to pass to the code generator.
Nick Lewyckyfc55def2010-06-03 17:10:17 +000074 // For example, "generate-api-file" and "as"options are for the plugin
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000075 // use only and will not be passed.
Dan Gohmanf6920032010-04-16 00:42:57 +000076 static std::vector<std::string> extra;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000077
Rafael Espindola6c809922010-06-07 16:45:22 +000078 static void process_plugin_option(const char* opt_)
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000079 {
Rafael Espindola6c809922010-06-07 16:45:22 +000080 if (opt_ == NULL)
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000081 return;
Rafael Espindola6c809922010-06-07 16:45:22 +000082 llvm::StringRef opt = opt_;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000083
Rafael Espindola6c809922010-06-07 16:45:22 +000084 if (opt == "generate-api-file") {
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000085 generate_api_file = true;
Rafael Espindola6c809922010-06-07 16:45:22 +000086 } else if (opt.startswith("as=")) {
87 if (!as_path.empty()) {
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000088 (*message)(LDPL_WARNING, "Path to as specified twice. "
Rafael Espindola6c809922010-06-07 16:45:22 +000089 "Discarding %s", opt_);
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000090 } else {
Rafael Espindola6c809922010-06-07 16:45:22 +000091 as_path = opt.substr(strlen("as="));
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000092 }
Rafael Espindola11f403c2010-06-23 20:20:59 +000093 } else if (opt.startswith("extra-library-path=")) {
94 extra_library_path = opt.substr(strlen("extra_library_path="));
Rafael Espindoladd76f182010-06-18 19:18:58 +000095 } else if (opt.startswith("pass-through=")) {
96 llvm::StringRef item = opt.substr(strlen("pass-through="));
97 pass_through.push_back(item.str());
Rafael Espindola6c809922010-06-07 16:45:22 +000098 } else if (opt == "emit-llvm") {
Rafael Espindolac4b55612010-06-03 21:11:20 +000099 generate_bc_file = BC_ONLY;
Rafael Espindola6c809922010-06-07 16:45:22 +0000100 } else if (opt == "also-emit-llvm") {
Rafael Espindolac4b55612010-06-03 21:11:20 +0000101 generate_bc_file = BC_ALSO;
Rafael Espindola6c809922010-06-07 16:45:22 +0000102 } else if (opt.startswith("also-emit-llvm=")) {
103 llvm::StringRef path = opt.substr(strlen("also-emit-llvm="));
Rafael Espindolac4b55612010-06-03 21:11:20 +0000104 generate_bc_file = BC_ALSO;
Nick Lewycky662f7382010-06-03 17:13:23 +0000105 if (!bc_path.empty()) {
Rafael Espindola62bacd62010-05-13 13:39:31 +0000106 (*message)(LDPL_WARNING, "Path to the output IL file specified twice. "
Rafael Espindola6c809922010-06-07 16:45:22 +0000107 "Discarding %s", opt_);
Rafael Espindola62bacd62010-05-13 13:39:31 +0000108 } else {
109 bc_path = path;
110 }
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000111 } else {
112 // Save this option to pass to the code generator.
Rafael Espindola6c809922010-06-07 16:45:22 +0000113 extra.push_back(opt);
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000114 }
115 }
116}
117
Dan Gohmanf6920032010-04-16 00:42:57 +0000118static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
119 int *claimed);
120static ld_plugin_status all_symbols_read_hook(void);
121static ld_plugin_status cleanup_hook(void);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000122
123extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
124ld_plugin_status onload(ld_plugin_tv *tv) {
125 // We're given a pointer to the first transfer vector. We read through them
126 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
127 // contain pointers to functions that we need to call to register our own
128 // hooks. The others are addresses of functions we can use to call into gold
129 // for services.
130
131 bool registeredClaimFile = false;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000132
133 for (; tv->tv_tag != LDPT_NULL; ++tv) {
134 switch (tv->tv_tag) {
135 case LDPT_API_VERSION:
136 api_version = tv->tv_u.tv_val;
137 break;
138 case LDPT_GOLD_VERSION: // major * 100 + minor
139 gold_version = tv->tv_u.tv_val;
140 break;
Rafael Espindolac4b55612010-06-03 21:11:20 +0000141 case LDPT_OUTPUT_NAME:
142 output_name = tv->tv_u.tv_string;
143 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000144 case LDPT_LINKER_OUTPUT:
145 switch (tv->tv_u.tv_val) {
146 case LDPO_REL: // .o
147 case LDPO_DYN: // .so
148 output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
149 break;
150 case LDPO_EXEC: // .exe
151 output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
152 break;
153 default:
154 (*message)(LDPL_ERROR, "Unknown output file type %d",
155 tv->tv_u.tv_val);
156 return LDPS_ERR;
157 }
158 // TODO: add an option to disable PIC.
159 //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
160 break;
161 case LDPT_OPTION:
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000162 options::process_plugin_option(tv->tv_u.tv_string);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000163 break;
164 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
165 ld_plugin_register_claim_file callback;
166 callback = tv->tv_u.tv_register_claim_file;
167
168 if ((*callback)(claim_file_hook) != LDPS_OK)
169 return LDPS_ERR;
170
171 registeredClaimFile = true;
172 } break;
173 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
174 ld_plugin_register_all_symbols_read callback;
175 callback = tv->tv_u.tv_register_all_symbols_read;
176
177 if ((*callback)(all_symbols_read_hook) != LDPS_OK)
178 return LDPS_ERR;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000179 } break;
180 case LDPT_REGISTER_CLEANUP_HOOK: {
181 ld_plugin_register_cleanup callback;
182 callback = tv->tv_u.tv_register_cleanup;
183
184 if ((*callback)(cleanup_hook) != LDPS_OK)
185 return LDPS_ERR;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000186 } break;
187 case LDPT_ADD_SYMBOLS:
188 add_symbols = tv->tv_u.tv_add_symbols;
189 break;
190 case LDPT_GET_SYMBOLS:
191 get_symbols = tv->tv_u.tv_get_symbols;
192 break;
193 case LDPT_ADD_INPUT_FILE:
194 add_input_file = tv->tv_u.tv_add_input_file;
195 break;
Rafael Espindoladd76f182010-06-18 19:18:58 +0000196 case LDPT_ADD_INPUT_LIBRARY:
197 add_input_library = tv->tv_u.tv_add_input_file;
198 break;
Rafael Espindola11f403c2010-06-23 20:20:59 +0000199 case LDPT_SET_EXTRA_LIBRARY_PATH:
200 set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
201 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000202 case LDPT_MESSAGE:
203 message = tv->tv_u.tv_message;
204 break;
205 default:
206 break;
207 }
208 }
209
Rafael Espindola98c507e2009-02-18 08:30:15 +0000210 if (!registeredClaimFile) {
Rafael Espindola6210a942009-02-18 17:49:06 +0000211 (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
212 return LDPS_ERR;
213 }
Rafael Espindola98c507e2009-02-18 08:30:15 +0000214 if (!add_symbols) {
Rafael Espindola6210a942009-02-18 17:49:06 +0000215 (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
216 return LDPS_ERR;
217 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000218
219 return LDPS_OK;
220}
221
222/// claim_file_hook - called by gold to see whether this file is one that
223/// our plugin can handle. We'll try to open it and register all the symbols
224/// with add_symbol if possible.
Dan Gohmanf6920032010-04-16 00:42:57 +0000225static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
226 int *claimed) {
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000227 void *buf = NULL;
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000228 if (file->offset) {
Nick Lewyckyc1da8862009-02-05 04:14:23 +0000229 // Gold has found what might be IR part-way inside of a file, such as
230 // an .a archive.
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000231 if (lseek(file->fd, file->offset, SEEK_SET) == -1) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000232 (*message)(LDPL_ERROR,
Nick Lewyckyfc55def2010-06-03 17:10:17 +0000233 "Failed to seek to archive member of %s at offset %d: %s\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000234 file->name,
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +0000235 file->offset, sys::StrError(errno).c_str());
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000236 return LDPS_ERR;
237 }
238 buf = malloc(file->filesize);
239 if (!buf) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000240 (*message)(LDPL_ERROR,
Nick Lewyckyfc55def2010-06-03 17:10:17 +0000241 "Failed to allocate buffer for archive member of size: %d\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000242 file->filesize);
243 return LDPS_ERR;
244 }
245 if (read(file->fd, buf, file->filesize) != file->filesize) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000246 (*message)(LDPL_ERROR,
247 "Failed to read archive member of %s at offset %d: %s\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000248 file->name,
Nick Lewycky0df91b22009-02-07 03:15:01 +0000249 file->offset,
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +0000250 sys::StrError(errno).c_str());
Nick Lewycky0df91b22009-02-07 03:15:01 +0000251 free(buf);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000252 return LDPS_ERR;
253 }
Nick Lewycky0df91b22009-02-07 03:15:01 +0000254 if (!lto_module_is_object_file_in_memory(buf, file->filesize)) {
255 free(buf);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000256 return LDPS_OK;
Nick Lewycky0df91b22009-02-07 03:15:01 +0000257 }
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000258 } else if (!lto_module_is_object_file(file->name))
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000259 return LDPS_OK;
260
261 *claimed = 1;
262 Modules.resize(Modules.size() + 1);
263 claimed_file &cf = Modules.back();
264
Nick Lewyckyc1da8862009-02-05 04:14:23 +0000265 cf.M = buf ? lto_module_create_from_memory(buf, file->filesize) :
266 lto_module_create(file->name);
Nick Lewyckyea97aa62009-02-06 01:58:34 +0000267 free(buf);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000268 if (!cf.M) {
269 (*message)(LDPL_ERROR, "Failed to create LLVM module: %s",
270 lto_get_error_message());
271 return LDPS_ERR;
272 }
273 cf.handle = file->handle;
274 unsigned sym_count = lto_module_get_num_symbols(cf.M);
275 cf.syms.reserve(sym_count);
276
277 for (unsigned i = 0; i != sym_count; ++i) {
278 lto_symbol_attributes attrs = lto_module_get_symbol_attribute(cf.M, i);
279 if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
280 continue;
281
282 cf.syms.push_back(ld_plugin_symbol());
283 ld_plugin_symbol &sym = cf.syms.back();
284 sym.name = const_cast<char *>(lto_module_get_symbol_name(cf.M, i));
285 sym.version = NULL;
286
287 int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
288 switch (scope) {
289 case LTO_SYMBOL_SCOPE_HIDDEN:
290 sym.visibility = LDPV_HIDDEN;
291 break;
292 case LTO_SYMBOL_SCOPE_PROTECTED:
293 sym.visibility = LDPV_PROTECTED;
294 break;
295 case 0: // extern
296 case LTO_SYMBOL_SCOPE_DEFAULT:
297 sym.visibility = LDPV_DEFAULT;
298 break;
299 default:
300 (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
301 return LDPS_ERR;
302 }
303
304 int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
305 switch (definition) {
306 case LTO_SYMBOL_DEFINITION_REGULAR:
307 sym.def = LDPK_DEF;
308 break;
309 case LTO_SYMBOL_DEFINITION_UNDEFINED:
310 sym.def = LDPK_UNDEF;
311 break;
312 case LTO_SYMBOL_DEFINITION_TENTATIVE:
313 sym.def = LDPK_COMMON;
314 break;
315 case LTO_SYMBOL_DEFINITION_WEAK:
316 sym.def = LDPK_WEAKDEF;
317 break;
Rafael Espindola7431af02009-04-24 16:55:21 +0000318 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
319 sym.def = LDPK_WEAKUNDEF;
320 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000321 default:
322 (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition);
323 return LDPS_ERR;
324 }
325
326 // LLVM never emits COMDAT.
327 sym.size = 0;
328 sym.comdat_key = NULL;
329
330 sym.resolution = LDPR_UNKNOWN;
331 }
332
333 cf.syms.reserve(cf.syms.size());
334
335 if (!cf.syms.empty()) {
336 if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
337 (*message)(LDPL_ERROR, "Unable to add symbols!");
338 return LDPS_ERR;
339 }
340 }
341
342 return LDPS_OK;
343}
344
345/// all_symbols_read_hook - gold informs us that all symbols have been read.
346/// At this point, we use get_symbols to see if any of our definitions have
347/// been overridden by a native object file. Then, perform optimization and
348/// codegen.
Dan Gohmanf6920032010-04-16 00:42:57 +0000349static ld_plugin_status all_symbols_read_hook(void) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000350 lto_code_gen_t cg = lto_codegen_create();
351
352 for (std::list<claimed_file>::iterator I = Modules.begin(),
353 E = Modules.end(); I != E; ++I)
354 lto_codegen_add_module(cg, I->M);
355
Nick Lewyckyca428622009-02-22 22:15:44 +0000356 std::ofstream api_file;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000357 if (options::generate_api_file) {
Nick Lewyckyca428622009-02-22 22:15:44 +0000358 api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
359 if (!api_file.is_open()) {
360 (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing.");
361 abort();
362 }
363 }
364
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000365 // If we don't preserve any symbols, libLTO will assume that all symbols are
366 // needed. Keep all symbols unless we're producing a final executable.
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000367 bool anySymbolsPreserved = false;
368 for (std::list<claimed_file>::iterator I = Modules.begin(),
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000369 E = Modules.end(); I != E; ++I) {
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000370 (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
371 for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
372 if (I->syms[i].resolution == LDPR_PREVAILING_DEF) {
373 lto_codegen_add_must_preserve_symbol(cg, I->syms[i].name);
374 anySymbolsPreserved = true;
Nick Lewyckyca428622009-02-22 22:15:44 +0000375
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000376 if (options::generate_api_file)
377 api_file << I->syms[i].name << "\n";
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000378 }
379 }
Rafael Espindola38a979b2010-06-14 21:20:52 +0000380 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000381
Rafael Espindola38a979b2010-06-14 21:20:52 +0000382 if (options::generate_api_file)
383 api_file.close();
Nick Lewyckyca428622009-02-22 22:15:44 +0000384
Rafael Espindola38a979b2010-06-14 21:20:52 +0000385 if (!anySymbolsPreserved) {
386 // All of the IL is unnecessary!
387 lto_codegen_dispose(cg);
388 return LDPS_OK;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000389 }
390
391 lto_codegen_set_pic_model(cg, output_type);
392 lto_codegen_set_debug_model(cg, LTO_DEBUG_MODEL_DWARF);
Rafael Espindola6c809922010-06-07 16:45:22 +0000393 if (!options::as_path.empty()) {
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000394 sys::Path p = sys::Program::FindProgramByName(options::as_path);
Rafael Espindola42de34f2009-06-15 10:14:18 +0000395 lto_codegen_set_assembler_path(cg, p.c_str());
396 }
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000397 // Pass through extra options to the code generator.
398 if (!options::extra.empty()) {
399 for (std::vector<std::string>::iterator it = options::extra.begin();
400 it != options::extra.end(); ++it) {
401 lto_codegen_debug_options(cg, (*it).c_str());
402 }
403 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000404
Rafael Espindolac4b55612010-06-03 21:11:20 +0000405
406 if (options::generate_bc_file != options::BC_NO) {
407 std::string path;
408 if (options::generate_bc_file == options::BC_ONLY)
409 path = output_name;
410 else if (!options::bc_path.empty())
411 path = options::bc_path;
412 else
413 path = output_name + ".bc";
414 bool err = lto_codegen_write_merged_modules(cg, path.c_str());
Rafael Espindola62bacd62010-05-13 13:39:31 +0000415 if (err)
416 (*message)(LDPL_FATAL, "Failed to write the output file.");
Rafael Espindolac4b55612010-06-03 21:11:20 +0000417 if (options::generate_bc_file == options::BC_ONLY)
418 exit(0);
Rafael Espindola62bacd62010-05-13 13:39:31 +0000419 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000420 size_t bufsize = 0;
421 const char *buffer = static_cast<const char *>(lto_codegen_compile(cg,
422 &bufsize));
423
424 std::string ErrMsg;
425
426 sys::Path uniqueObjPath("/tmp/llvmgold.o");
427 if (uniqueObjPath.createTemporaryFileOnDisk(true, &ErrMsg)) {
428 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
429 return LDPS_ERR;
430 }
Nick Lewycky221aec62010-06-07 21:42:19 +0000431 raw_fd_ostream objFile(uniqueObjPath.c_str(), ErrMsg,
432 raw_fd_ostream::F_Binary);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000433 if (!ErrMsg.empty()) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000434 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
435 return LDPS_ERR;
436 }
437
Nick Lewycky221aec62010-06-07 21:42:19 +0000438 objFile.write(buffer, bufsize);
439 objFile.close();
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000440
441 lto_codegen_dispose(cg);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000442
Rafael Espindolabebb6402010-06-21 02:23:12 +0000443 if ((*add_input_file)(uniqueObjPath.c_str()) != LDPS_OK) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000444 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
445 (*message)(LDPL_ERROR, "File left behind in: %s", uniqueObjPath.c_str());
446 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 Espindoladd76f182010-06-18 19:18:58 +0000455 for (std::vector<std::string>::iterator i = options::pass_through.begin(),
456 e = options::pass_through.end();
457 i != e; ++i) {
458 std::string &item = *i;
Rafael Espindolabebb6402010-06-21 02:23:12 +0000459 const char *item_p = item.c_str();
Rafael Espindoladd76f182010-06-18 19:18:58 +0000460 if (llvm::StringRef(item).startswith("-l")) {
461 if (add_input_library(item_p + 2) != LDPS_OK) {
462 (*message)(LDPL_ERROR, "Unable to add library to the link.");
463 return LDPS_ERR;
464 }
465 } else {
466 if (add_input_file(item_p) != LDPS_OK) {
467 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
468 return LDPS_ERR;
469 }
470 }
471 }
472
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000473 Cleanup.push_back(uniqueObjPath);
474
475 return LDPS_OK;
476}
477
Dan Gohmanf6920032010-04-16 00:42:57 +0000478static ld_plugin_status cleanup_hook(void) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000479 std::string ErrMsg;
480
481 for (int i = 0, e = Cleanup.size(); i != e; ++i)
482 if (Cleanup[i].eraseFromDisk(false, &ErrMsg))
483 (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),
484 ErrMsg.c_str());
485
486 return LDPS_OK;
487}