blob: e959d9566b297ec76190970c774df025d53d2a58 [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.
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000238 if (lseek(file->fd, file->offset, SEEK_SET) == -1) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000239 (*message)(LDPL_ERROR,
Nick Lewyckyfc55def2010-06-03 17:10:17 +0000240 "Failed to seek to archive member of %s at offset %d: %s\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000241 file->name,
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +0000242 file->offset, sys::StrError(errno).c_str());
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000243 return LDPS_ERR;
244 }
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000245 void *buf = malloc(file->filesize);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000246 if (!buf) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000247 (*message)(LDPL_ERROR,
Nick Lewyckyfc55def2010-06-03 17:10:17 +0000248 "Failed to allocate buffer for archive member of size: %d\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000249 file->filesize);
250 return LDPS_ERR;
251 }
252 if (read(file->fd, buf, file->filesize) != file->filesize) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000253 (*message)(LDPL_ERROR,
254 "Failed to read archive member of %s at offset %d: %s\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000255 file->name,
Nick Lewycky0df91b22009-02-07 03:15:01 +0000256 file->offset,
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +0000257 sys::StrError(errno).c_str());
Nick Lewycky0df91b22009-02-07 03:15:01 +0000258 free(buf);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000259 return LDPS_ERR;
260 }
Nick Lewycky0df91b22009-02-07 03:15:01 +0000261 if (!lto_module_is_object_file_in_memory(buf, file->filesize)) {
262 free(buf);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000263 return LDPS_OK;
Nick Lewycky0df91b22009-02-07 03:15:01 +0000264 }
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000265 M = lto_module_create_from_memory(buf, file->filesize);
Rafael Espindola37d42f82011-02-19 21:49:57 +0000266 if (!M) {
267 (*message)(LDPL_ERROR, "Failed to create LLVM module: %s",
268 lto_get_error_message());
269 return LDPS_ERR;
270 }
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000271 free(buf);
272 } else {
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000273 lseek(file->fd, 0, SEEK_SET);
Rafael Espindola35358e02011-02-27 20:15:37 +0000274 M = lto_module_create_from_fd(file->fd, file->name, file->filesize);
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000275 if (!M)
276 return LDPS_OK;
277 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000278
279 *claimed = 1;
280 Modules.resize(Modules.size() + 1);
281 claimed_file &cf = Modules.back();
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000282
283 if (!options::triple.empty())
Rafael Espindola37d42f82011-02-19 21:49:57 +0000284 lto_module_set_target_triple(M, options::triple.c_str());
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000285
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000286 cf.handle = file->handle;
Rafael Espindola37d42f82011-02-19 21:49:57 +0000287 unsigned sym_count = lto_module_get_num_symbols(M);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000288 cf.syms.reserve(sym_count);
289
290 for (unsigned i = 0; i != sym_count; ++i) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000291 lto_symbol_attributes attrs = lto_module_get_symbol_attribute(M, i);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000292 if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
293 continue;
294
295 cf.syms.push_back(ld_plugin_symbol());
296 ld_plugin_symbol &sym = cf.syms.back();
Rafael Espindola37d42f82011-02-19 21:49:57 +0000297 sym.name = const_cast<char *>(lto_module_get_symbol_name(M, i));
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000298 sym.name = strdup(sym.name);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000299 sym.version = NULL;
300
301 int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
302 switch (scope) {
303 case LTO_SYMBOL_SCOPE_HIDDEN:
304 sym.visibility = LDPV_HIDDEN;
305 break;
306 case LTO_SYMBOL_SCOPE_PROTECTED:
307 sym.visibility = LDPV_PROTECTED;
308 break;
309 case 0: // extern
310 case LTO_SYMBOL_SCOPE_DEFAULT:
311 sym.visibility = LDPV_DEFAULT;
312 break;
313 default:
314 (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
315 return LDPS_ERR;
316 }
317
318 int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000319 sym.comdat_key = NULL;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000320 switch (definition) {
321 case LTO_SYMBOL_DEFINITION_REGULAR:
322 sym.def = LDPK_DEF;
323 break;
324 case LTO_SYMBOL_DEFINITION_UNDEFINED:
325 sym.def = LDPK_UNDEF;
326 break;
327 case LTO_SYMBOL_DEFINITION_TENTATIVE:
328 sym.def = LDPK_COMMON;
329 break;
330 case LTO_SYMBOL_DEFINITION_WEAK:
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000331 sym.comdat_key = sym.name;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000332 sym.def = LDPK_WEAKDEF;
333 break;
Rafael Espindola7431af02009-04-24 16:55:21 +0000334 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
335 sym.def = LDPK_WEAKUNDEF;
336 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000337 default:
338 (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition);
339 return LDPS_ERR;
340 }
341
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000342 sym.size = 0;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000343
344 sym.resolution = LDPR_UNKNOWN;
345 }
346
347 cf.syms.reserve(cf.syms.size());
348
349 if (!cf.syms.empty()) {
350 if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
351 (*message)(LDPL_ERROR, "Unable to add symbols!");
352 return LDPS_ERR;
353 }
354 }
355
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000356 if (code_gen)
357 lto_codegen_add_module(code_gen, M);
358
359 lto_module_dispose(M);
360
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000361 return LDPS_OK;
362}
363
364/// all_symbols_read_hook - gold informs us that all symbols have been read.
365/// At this point, we use get_symbols to see if any of our definitions have
366/// been overridden by a native object file. Then, perform optimization and
367/// codegen.
Dan Gohmanf6920032010-04-16 00:42:57 +0000368static ld_plugin_status all_symbols_read_hook(void) {
Nick Lewyckyca428622009-02-22 22:15:44 +0000369 std::ofstream api_file;
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000370 assert(code_gen);
371
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000372 if (options::generate_api_file) {
Nick Lewyckyca428622009-02-22 22:15:44 +0000373 api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
374 if (!api_file.is_open()) {
375 (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing.");
376 abort();
377 }
378 }
379
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000380 // If we don't preserve any symbols, libLTO will assume that all symbols are
381 // needed. Keep all symbols unless we're producing a final executable.
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000382 bool anySymbolsPreserved = false;
383 for (std::list<claimed_file>::iterator I = Modules.begin(),
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000384 E = Modules.end(); I != E; ++I) {
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000385 (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
386 for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
387 if (I->syms[i].resolution == LDPR_PREVAILING_DEF) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000388 lto_codegen_add_must_preserve_symbol(code_gen, I->syms[i].name);
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000389 anySymbolsPreserved = true;
Nick Lewyckyca428622009-02-22 22:15:44 +0000390
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000391 if (options::generate_api_file)
392 api_file << I->syms[i].name << "\n";
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000393 }
394 }
Rafael Espindola38a979b2010-06-14 21:20:52 +0000395 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000396
Rafael Espindola38a979b2010-06-14 21:20:52 +0000397 if (options::generate_api_file)
398 api_file.close();
Nick Lewyckyca428622009-02-22 22:15:44 +0000399
Rafael Espindola38a979b2010-06-14 21:20:52 +0000400 if (!anySymbolsPreserved) {
401 // All of the IL is unnecessary!
Rafael Espindola37d42f82011-02-19 21:49:57 +0000402 lto_codegen_dispose(code_gen);
Rafael Espindola38a979b2010-06-14 21:20:52 +0000403 return LDPS_OK;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000404 }
405
Rafael Espindola37d42f82011-02-19 21:49:57 +0000406 lto_codegen_set_pic_model(code_gen, output_type);
407 lto_codegen_set_debug_model(code_gen, LTO_DEBUG_MODEL_DWARF);
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000408 if (!options::mcpu.empty())
Rafael Espindola37d42f82011-02-19 21:49:57 +0000409 lto_codegen_set_cpu(code_gen, options::mcpu.c_str());
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000410
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000411 // Pass through extra options to the code generator.
412 if (!options::extra.empty()) {
413 for (std::vector<std::string>::iterator it = options::extra.begin();
414 it != options::extra.end(); ++it) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000415 lto_codegen_debug_options(code_gen, (*it).c_str());
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000416 }
417 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000418
Rafael Espindolac4b55612010-06-03 21:11:20 +0000419 if (options::generate_bc_file != options::BC_NO) {
420 std::string path;
421 if (options::generate_bc_file == options::BC_ONLY)
422 path = output_name;
423 else if (!options::bc_path.empty())
424 path = options::bc_path;
425 else
426 path = output_name + ".bc";
Rafael Espindola37d42f82011-02-19 21:49:57 +0000427 bool err = lto_codegen_write_merged_modules(code_gen, path.c_str());
Rafael Espindola62bacd62010-05-13 13:39:31 +0000428 if (err)
429 (*message)(LDPL_FATAL, "Failed to write the output file.");
Rafael Espindolac4b55612010-06-03 21:11:20 +0000430 if (options::generate_bc_file == options::BC_ONLY)
431 exit(0);
Rafael Espindola62bacd62010-05-13 13:39:31 +0000432 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000433 size_t bufsize = 0;
Rafael Espindola37d42f82011-02-19 21:49:57 +0000434 const char *buffer = static_cast<const char *>(lto_codegen_compile(code_gen,
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000435 &bufsize));
436
437 std::string ErrMsg;
438
Rafael Espindola5a287d72011-02-16 11:19:44 +0000439 const char *objPath;
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000440 sys::Path uniqueObjPath("/tmp/llvmgold.o");
Rafael Espindola5a287d72011-02-16 11:19:44 +0000441 if (!options::obj_path.empty()) {
442 objPath = options::obj_path.c_str();
443 } else {
Rafael Espindola5a287d72011-02-16 11:19:44 +0000444 if (uniqueObjPath.createTemporaryFileOnDisk(true, &ErrMsg)) {
445 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
446 return LDPS_ERR;
447 }
448 objPath = uniqueObjPath.c_str();
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000449 }
Rafael Espindola5a287d72011-02-16 11:19:44 +0000450 tool_output_file objFile(objPath, ErrMsg,
451 raw_fd_ostream::F_Binary);
452 if (!ErrMsg.empty()) {
453 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
454 return LDPS_ERR;
455 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000456
Nick Lewycky59b4d2a2010-09-02 05:44:31 +0000457 objFile.os().write(buffer, bufsize);
458 objFile.os().close();
459 if (objFile.os().has_error()) {
Dan Gohmanf2914012010-08-20 16:59:15 +0000460 (*message)(LDPL_ERROR, "Error writing output file '%s'",
Rafael Espindola5a287d72011-02-16 11:19:44 +0000461 objPath);
Nick Lewycky59b4d2a2010-09-02 05:44:31 +0000462 objFile.os().clear_error();
Dan Gohmanf2914012010-08-20 16:59:15 +0000463 return LDPS_ERR;
464 }
465 objFile.keep();
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000466
Rafael Espindola37d42f82011-02-19 21:49:57 +0000467 lto_codegen_dispose(code_gen);
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000468 for (std::list<claimed_file>::iterator I = Modules.begin(),
469 E = Modules.end(); I != E; ++I) {
470 for (unsigned i = 0; i != I->syms.size(); ++i) {
471 ld_plugin_symbol &sym = I->syms[i];
472 free(sym.name);
473 }
474 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000475
Rafael Espindola5a287d72011-02-16 11:19:44 +0000476 if ((*add_input_file)(objPath) != LDPS_OK) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000477 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
Rafael Espindola5a287d72011-02-16 11:19:44 +0000478 (*message)(LDPL_ERROR, "File left behind in: %s", objPath);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000479 return LDPS_ERR;
480 }
481
Rafael Espindola11f403c2010-06-23 20:20:59 +0000482 if (!options::extra_library_path.empty() &&
483 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) {
484 (*message)(LDPL_ERROR, "Unable to set the extra library path.");
485 return LDPS_ERR;
486 }
487
Rafael Espindola5a287d72011-02-16 11:19:44 +0000488 if (options::obj_path.empty())
489 Cleanup.push_back(sys::Path(objPath));
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000490
491 return LDPS_OK;
492}
493
Dan Gohmanf6920032010-04-16 00:42:57 +0000494static ld_plugin_status cleanup_hook(void) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000495 std::string ErrMsg;
496
497 for (int i = 0, e = Cleanup.size(); i != e; ++i)
498 if (Cleanup[i].eraseFromDisk(false, &ErrMsg))
499 (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),
500 ErrMsg.c_str());
501
502 return LDPS_OK;
503}