blob: a9e01781a1bcbd2d8e76d791e1b15007f1714ea2 [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 Espindola6c809922010-06-07 16:45:22 +000076 static std::string as_path;
Rafael Espindola98197e52010-08-10 18:55:09 +000077 static std::vector<std::string> as_args;
Rafael Espindoladd76f182010-06-18 19:18:58 +000078 static std::vector<std::string> pass_through;
Rafael Espindola11f403c2010-06-23 20:20:59 +000079 static std::string extra_library_path;
Rafael Espindolacbb170d2010-08-09 21:09:46 +000080 static std::string triple;
Rafael Espindola2d643ef2010-08-11 00:15:13 +000081 static std::string mcpu;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000082 // Additional options to pass into the code generator.
Nick Lewyckyfc55def2010-06-03 17:10:17 +000083 // Note: This array will contain all plugin options which are not claimed
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000084 // as plugin exclusive to pass to the code generator.
Nick Lewyckyfc55def2010-06-03 17:10:17 +000085 // For example, "generate-api-file" and "as"options are for the plugin
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000086 // use only and will not be passed.
Dan Gohmanf6920032010-04-16 00:42:57 +000087 static std::vector<std::string> extra;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000088
Rafael Espindola6c809922010-06-07 16:45:22 +000089 static void process_plugin_option(const char* opt_)
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000090 {
Rafael Espindola6c809922010-06-07 16:45:22 +000091 if (opt_ == NULL)
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000092 return;
Rafael Espindola6c809922010-06-07 16:45:22 +000093 llvm::StringRef opt = opt_;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000094
Rafael Espindola6c809922010-06-07 16:45:22 +000095 if (opt == "generate-api-file") {
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000096 generate_api_file = true;
Rafael Espindola2d643ef2010-08-11 00:15:13 +000097 } else if (opt.startswith("mcpu=")) {
98 mcpu = opt.substr(strlen("mcpu="));
Rafael Espindola6c809922010-06-07 16:45:22 +000099 } else if (opt.startswith("as=")) {
100 if (!as_path.empty()) {
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000101 (*message)(LDPL_WARNING, "Path to as specified twice. "
Rafael Espindola6c809922010-06-07 16:45:22 +0000102 "Discarding %s", opt_);
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000103 } else {
Rafael Espindola6c809922010-06-07 16:45:22 +0000104 as_path = opt.substr(strlen("as="));
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000105 }
Rafael Espindola98197e52010-08-10 18:55:09 +0000106 } else if (opt.startswith("as-arg=")) {
107 llvm::StringRef item = opt.substr(strlen("as-arg="));
108 as_args.push_back(item.str());
Rafael Espindola11f403c2010-06-23 20:20:59 +0000109 } else if (opt.startswith("extra-library-path=")) {
110 extra_library_path = opt.substr(strlen("extra_library_path="));
Rafael Espindoladd76f182010-06-18 19:18:58 +0000111 } else if (opt.startswith("pass-through=")) {
112 llvm::StringRef item = opt.substr(strlen("pass-through="));
113 pass_through.push_back(item.str());
Rafael Espindola15af3872010-08-10 16:32:15 +0000114 } else if (opt.startswith("mtriple=")) {
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000115 triple = opt.substr(strlen("mtriple="));
Rafael Espindola5a287d72011-02-16 11:19:44 +0000116 } else if (opt.startswith("obj-path=")) {
117 obj_path = opt.substr(strlen("obj-path="));
Rafael Espindola6c809922010-06-07 16:45:22 +0000118 } else if (opt == "emit-llvm") {
Rafael Espindolac4b55612010-06-03 21:11:20 +0000119 generate_bc_file = BC_ONLY;
Rafael Espindola6c809922010-06-07 16:45:22 +0000120 } else if (opt == "also-emit-llvm") {
Rafael Espindolac4b55612010-06-03 21:11:20 +0000121 generate_bc_file = BC_ALSO;
Rafael Espindola6c809922010-06-07 16:45:22 +0000122 } else if (opt.startswith("also-emit-llvm=")) {
123 llvm::StringRef path = opt.substr(strlen("also-emit-llvm="));
Rafael Espindolac4b55612010-06-03 21:11:20 +0000124 generate_bc_file = BC_ALSO;
Nick Lewycky662f7382010-06-03 17:13:23 +0000125 if (!bc_path.empty()) {
Rafael Espindola62bacd62010-05-13 13:39:31 +0000126 (*message)(LDPL_WARNING, "Path to the output IL file specified twice. "
Rafael Espindola6c809922010-06-07 16:45:22 +0000127 "Discarding %s", opt_);
Rafael Espindola62bacd62010-05-13 13:39:31 +0000128 } else {
129 bc_path = path;
130 }
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000131 } else {
132 // Save this option to pass to the code generator.
Rafael Espindola6c809922010-06-07 16:45:22 +0000133 extra.push_back(opt);
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000134 }
135 }
136}
137
Dan Gohmanf6920032010-04-16 00:42:57 +0000138static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
139 int *claimed);
140static ld_plugin_status all_symbols_read_hook(void);
141static ld_plugin_status cleanup_hook(void);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000142
143extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
144ld_plugin_status onload(ld_plugin_tv *tv) {
145 // We're given a pointer to the first transfer vector. We read through them
146 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
147 // contain pointers to functions that we need to call to register our own
148 // hooks. The others are addresses of functions we can use to call into gold
149 // for services.
150
151 bool registeredClaimFile = false;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000152
153 for (; tv->tv_tag != LDPT_NULL; ++tv) {
154 switch (tv->tv_tag) {
155 case LDPT_API_VERSION:
156 api_version = tv->tv_u.tv_val;
157 break;
158 case LDPT_GOLD_VERSION: // major * 100 + minor
159 gold_version = tv->tv_u.tv_val;
160 break;
Rafael Espindolac4b55612010-06-03 21:11:20 +0000161 case LDPT_OUTPUT_NAME:
162 output_name = tv->tv_u.tv_string;
163 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000164 case LDPT_LINKER_OUTPUT:
165 switch (tv->tv_u.tv_val) {
166 case LDPO_REL: // .o
167 case LDPO_DYN: // .so
168 output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
169 break;
170 case LDPO_EXEC: // .exe
171 output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
172 break;
173 default:
174 (*message)(LDPL_ERROR, "Unknown output file type %d",
175 tv->tv_u.tv_val);
176 return LDPS_ERR;
177 }
178 // TODO: add an option to disable PIC.
179 //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
180 break;
181 case LDPT_OPTION:
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000182 options::process_plugin_option(tv->tv_u.tv_string);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000183 break;
184 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
185 ld_plugin_register_claim_file callback;
186 callback = tv->tv_u.tv_register_claim_file;
187
188 if ((*callback)(claim_file_hook) != LDPS_OK)
189 return LDPS_ERR;
190
191 registeredClaimFile = true;
192 } break;
193 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
194 ld_plugin_register_all_symbols_read callback;
195 callback = tv->tv_u.tv_register_all_symbols_read;
196
197 if ((*callback)(all_symbols_read_hook) != LDPS_OK)
198 return LDPS_ERR;
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000199
200 code_gen = lto_codegen_create();
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000201 } break;
202 case LDPT_REGISTER_CLEANUP_HOOK: {
203 ld_plugin_register_cleanup callback;
204 callback = tv->tv_u.tv_register_cleanup;
205
206 if ((*callback)(cleanup_hook) != LDPS_OK)
207 return LDPS_ERR;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000208 } break;
209 case LDPT_ADD_SYMBOLS:
210 add_symbols = tv->tv_u.tv_add_symbols;
211 break;
212 case LDPT_GET_SYMBOLS:
213 get_symbols = tv->tv_u.tv_get_symbols;
214 break;
215 case LDPT_ADD_INPUT_FILE:
216 add_input_file = tv->tv_u.tv_add_input_file;
217 break;
Rafael Espindoladd76f182010-06-18 19:18:58 +0000218 case LDPT_ADD_INPUT_LIBRARY:
219 add_input_library = tv->tv_u.tv_add_input_file;
220 break;
Rafael Espindola11f403c2010-06-23 20:20:59 +0000221 case LDPT_SET_EXTRA_LIBRARY_PATH:
222 set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
223 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000224 case LDPT_MESSAGE:
225 message = tv->tv_u.tv_message;
226 break;
227 default:
228 break;
229 }
230 }
231
Rafael Espindola98c507e2009-02-18 08:30:15 +0000232 if (!registeredClaimFile) {
Rafael Espindola6210a942009-02-18 17:49:06 +0000233 (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
234 return LDPS_ERR;
235 }
Rafael Espindola98c507e2009-02-18 08:30:15 +0000236 if (!add_symbols) {
Rafael Espindola6210a942009-02-18 17:49:06 +0000237 (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
238 return LDPS_ERR;
239 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000240
241 return LDPS_OK;
242}
243
244/// claim_file_hook - called by gold to see whether this file is one that
245/// our plugin can handle. We'll try to open it and register all the symbols
246/// with add_symbol if possible.
Dan Gohmanf6920032010-04-16 00:42:57 +0000247static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
248 int *claimed) {
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000249 lto_module_t M;
250
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000251 if (file->offset) {
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.
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000254 if (lseek(file->fd, file->offset, SEEK_SET) == -1) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000255 (*message)(LDPL_ERROR,
Nick Lewyckyfc55def2010-06-03 17:10:17 +0000256 "Failed to seek to archive member of %s at offset %d: %s\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000257 file->name,
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +0000258 file->offset, sys::StrError(errno).c_str());
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000259 return LDPS_ERR;
260 }
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000261 void *buf = malloc(file->filesize);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000262 if (!buf) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000263 (*message)(LDPL_ERROR,
Nick Lewyckyfc55def2010-06-03 17:10:17 +0000264 "Failed to allocate buffer for archive member of size: %d\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000265 file->filesize);
266 return LDPS_ERR;
267 }
268 if (read(file->fd, buf, file->filesize) != file->filesize) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000269 (*message)(LDPL_ERROR,
270 "Failed to read archive member of %s at offset %d: %s\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000271 file->name,
Nick Lewycky0df91b22009-02-07 03:15:01 +0000272 file->offset,
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +0000273 sys::StrError(errno).c_str());
Nick Lewycky0df91b22009-02-07 03:15:01 +0000274 free(buf);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000275 return LDPS_ERR;
276 }
Nick Lewycky0df91b22009-02-07 03:15:01 +0000277 if (!lto_module_is_object_file_in_memory(buf, file->filesize)) {
278 free(buf);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000279 return LDPS_OK;
Nick Lewycky0df91b22009-02-07 03:15:01 +0000280 }
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000281 M = lto_module_create_from_memory(buf, file->filesize);
Rafael Espindola37d42f82011-02-19 21:49:57 +0000282 if (!M) {
283 (*message)(LDPL_ERROR, "Failed to create LLVM module: %s",
284 lto_get_error_message());
285 return LDPS_ERR;
286 }
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000287 free(buf);
288 } else {
289 // FIXME: We should not need to pass -1 as the file size, but there
290 // is a bug in BFD that causes it to pass 0 to us. Remove this once
291 // that is fixed.
292 off_t size = file->filesize ? file->filesize : -1;
293
294 // FIXME: We should not need to reset the position in the file, but there
295 // is a bug in BFD. Remove this once that is fixed.
296 off_t old_pos = lseek(file->fd, 0, SEEK_CUR);
297
298 lseek(file->fd, 0, SEEK_SET);
299 M = lto_module_create_from_fd(file->fd, file->name, size);
300
301 lseek(file->fd, old_pos, SEEK_SET);
302 if (!M)
303 return LDPS_OK;
304 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000305
306 *claimed = 1;
307 Modules.resize(Modules.size() + 1);
308 claimed_file &cf = Modules.back();
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000309
310 if (!options::triple.empty())
Rafael Espindola37d42f82011-02-19 21:49:57 +0000311 lto_module_set_target_triple(M, options::triple.c_str());
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000312
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000313 cf.handle = file->handle;
Rafael Espindola37d42f82011-02-19 21:49:57 +0000314 unsigned sym_count = lto_module_get_num_symbols(M);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000315 cf.syms.reserve(sym_count);
316
317 for (unsigned i = 0; i != sym_count; ++i) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000318 lto_symbol_attributes attrs = lto_module_get_symbol_attribute(M, i);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000319 if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
320 continue;
321
322 cf.syms.push_back(ld_plugin_symbol());
323 ld_plugin_symbol &sym = cf.syms.back();
Rafael Espindola37d42f82011-02-19 21:49:57 +0000324 sym.name = const_cast<char *>(lto_module_get_symbol_name(M, i));
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000325 sym.name = strdup(sym.name);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000326 sym.version = NULL;
327
328 int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
329 switch (scope) {
330 case LTO_SYMBOL_SCOPE_HIDDEN:
331 sym.visibility = LDPV_HIDDEN;
332 break;
333 case LTO_SYMBOL_SCOPE_PROTECTED:
334 sym.visibility = LDPV_PROTECTED;
335 break;
336 case 0: // extern
337 case LTO_SYMBOL_SCOPE_DEFAULT:
338 sym.visibility = LDPV_DEFAULT;
339 break;
340 default:
341 (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
342 return LDPS_ERR;
343 }
344
345 int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000346 sym.comdat_key = NULL;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000347 switch (definition) {
348 case LTO_SYMBOL_DEFINITION_REGULAR:
349 sym.def = LDPK_DEF;
350 break;
351 case LTO_SYMBOL_DEFINITION_UNDEFINED:
352 sym.def = LDPK_UNDEF;
353 break;
354 case LTO_SYMBOL_DEFINITION_TENTATIVE:
355 sym.def = LDPK_COMMON;
356 break;
357 case LTO_SYMBOL_DEFINITION_WEAK:
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000358 sym.comdat_key = sym.name;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000359 sym.def = LDPK_WEAKDEF;
360 break;
Rafael Espindola7431af02009-04-24 16:55:21 +0000361 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
362 sym.def = LDPK_WEAKUNDEF;
363 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000364 default:
365 (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition);
366 return LDPS_ERR;
367 }
368
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000369 sym.size = 0;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000370
371 sym.resolution = LDPR_UNKNOWN;
372 }
373
374 cf.syms.reserve(cf.syms.size());
375
376 if (!cf.syms.empty()) {
377 if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
378 (*message)(LDPL_ERROR, "Unable to add symbols!");
379 return LDPS_ERR;
380 }
381 }
382
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000383 if (code_gen)
384 lto_codegen_add_module(code_gen, M);
385
386 lto_module_dispose(M);
387
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000388 return LDPS_OK;
389}
390
391/// all_symbols_read_hook - gold informs us that all symbols have been read.
392/// At this point, we use get_symbols to see if any of our definitions have
393/// been overridden by a native object file. Then, perform optimization and
394/// codegen.
Dan Gohmanf6920032010-04-16 00:42:57 +0000395static ld_plugin_status all_symbols_read_hook(void) {
Nick Lewyckyca428622009-02-22 22:15:44 +0000396 std::ofstream api_file;
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000397 assert(code_gen);
398
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000399 if (options::generate_api_file) {
Nick Lewyckyca428622009-02-22 22:15:44 +0000400 api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
401 if (!api_file.is_open()) {
402 (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing.");
403 abort();
404 }
405 }
406
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000407 // If we don't preserve any symbols, libLTO will assume that all symbols are
408 // needed. Keep all symbols unless we're producing a final executable.
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000409 bool anySymbolsPreserved = false;
410 for (std::list<claimed_file>::iterator I = Modules.begin(),
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000411 E = Modules.end(); I != E; ++I) {
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000412 (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
413 for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
414 if (I->syms[i].resolution == LDPR_PREVAILING_DEF) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000415 lto_codegen_add_must_preserve_symbol(code_gen, I->syms[i].name);
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000416 anySymbolsPreserved = true;
Nick Lewyckyca428622009-02-22 22:15:44 +0000417
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000418 if (options::generate_api_file)
419 api_file << I->syms[i].name << "\n";
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000420 }
421 }
Rafael Espindola38a979b2010-06-14 21:20:52 +0000422 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000423
Rafael Espindola38a979b2010-06-14 21:20:52 +0000424 if (options::generate_api_file)
425 api_file.close();
Nick Lewyckyca428622009-02-22 22:15:44 +0000426
Rafael Espindola38a979b2010-06-14 21:20:52 +0000427 if (!anySymbolsPreserved) {
428 // All of the IL is unnecessary!
Rafael Espindola37d42f82011-02-19 21:49:57 +0000429 lto_codegen_dispose(code_gen);
Rafael Espindola38a979b2010-06-14 21:20:52 +0000430 return LDPS_OK;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000431 }
432
Rafael Espindola37d42f82011-02-19 21:49:57 +0000433 lto_codegen_set_pic_model(code_gen, output_type);
434 lto_codegen_set_debug_model(code_gen, LTO_DEBUG_MODEL_DWARF);
Rafael Espindola6c809922010-06-07 16:45:22 +0000435 if (!options::as_path.empty()) {
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000436 sys::Path p = sys::Program::FindProgramByName(options::as_path);
Rafael Espindola37d42f82011-02-19 21:49:57 +0000437 lto_codegen_set_assembler_path(code_gen, p.c_str());
Rafael Espindola42de34f2009-06-15 10:14:18 +0000438 }
Rafael Espindola98197e52010-08-10 18:55:09 +0000439 if (!options::as_args.empty()) {
440 std::vector<const char *> as_args_p;
441 for (std::vector<std::string>::iterator I = options::as_args.begin(),
442 E = options::as_args.end(); I != E; ++I) {
443 as_args_p.push_back(I->c_str());
444 }
Rafael Espindola37d42f82011-02-19 21:49:57 +0000445 lto_codegen_set_assembler_args(code_gen, &as_args_p[0], as_args_p.size());
Rafael Espindola98197e52010-08-10 18:55:09 +0000446 }
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000447 if (!options::mcpu.empty())
Rafael Espindola37d42f82011-02-19 21:49:57 +0000448 lto_codegen_set_cpu(code_gen, options::mcpu.c_str());
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000449
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000450 // Pass through extra options to the code generator.
451 if (!options::extra.empty()) {
452 for (std::vector<std::string>::iterator it = options::extra.begin();
453 it != options::extra.end(); ++it) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000454 lto_codegen_debug_options(code_gen, (*it).c_str());
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000455 }
456 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000457
Rafael Espindolac4b55612010-06-03 21:11:20 +0000458 if (options::generate_bc_file != options::BC_NO) {
459 std::string path;
460 if (options::generate_bc_file == options::BC_ONLY)
461 path = output_name;
462 else if (!options::bc_path.empty())
463 path = options::bc_path;
464 else
465 path = output_name + ".bc";
Rafael Espindola37d42f82011-02-19 21:49:57 +0000466 bool err = lto_codegen_write_merged_modules(code_gen, path.c_str());
Rafael Espindola62bacd62010-05-13 13:39:31 +0000467 if (err)
468 (*message)(LDPL_FATAL, "Failed to write the output file.");
Rafael Espindolac4b55612010-06-03 21:11:20 +0000469 if (options::generate_bc_file == options::BC_ONLY)
470 exit(0);
Rafael Espindola62bacd62010-05-13 13:39:31 +0000471 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000472 size_t bufsize = 0;
Rafael Espindola37d42f82011-02-19 21:49:57 +0000473 const char *buffer = static_cast<const char *>(lto_codegen_compile(code_gen,
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000474 &bufsize));
475
476 std::string ErrMsg;
477
Rafael Espindola5a287d72011-02-16 11:19:44 +0000478 const char *objPath;
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000479 sys::Path uniqueObjPath("/tmp/llvmgold.o");
Rafael Espindola5a287d72011-02-16 11:19:44 +0000480 if (!options::obj_path.empty()) {
481 objPath = options::obj_path.c_str();
482 } else {
Rafael Espindola5a287d72011-02-16 11:19:44 +0000483 if (uniqueObjPath.createTemporaryFileOnDisk(true, &ErrMsg)) {
484 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
485 return LDPS_ERR;
486 }
487 objPath = uniqueObjPath.c_str();
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000488 }
Rafael Espindola5a287d72011-02-16 11:19:44 +0000489 tool_output_file objFile(objPath, ErrMsg,
490 raw_fd_ostream::F_Binary);
491 if (!ErrMsg.empty()) {
492 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
493 return LDPS_ERR;
494 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000495
Nick Lewycky59b4d2a2010-09-02 05:44:31 +0000496 objFile.os().write(buffer, bufsize);
497 objFile.os().close();
498 if (objFile.os().has_error()) {
Dan Gohmanf2914012010-08-20 16:59:15 +0000499 (*message)(LDPL_ERROR, "Error writing output file '%s'",
Rafael Espindola5a287d72011-02-16 11:19:44 +0000500 objPath);
Nick Lewycky59b4d2a2010-09-02 05:44:31 +0000501 objFile.os().clear_error();
Dan Gohmanf2914012010-08-20 16:59:15 +0000502 return LDPS_ERR;
503 }
504 objFile.keep();
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000505
Rafael Espindola37d42f82011-02-19 21:49:57 +0000506 lto_codegen_dispose(code_gen);
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000507 for (std::list<claimed_file>::iterator I = Modules.begin(),
508 E = Modules.end(); I != E; ++I) {
509 for (unsigned i = 0; i != I->syms.size(); ++i) {
510 ld_plugin_symbol &sym = I->syms[i];
511 free(sym.name);
512 }
513 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000514
Rafael Espindola5a287d72011-02-16 11:19:44 +0000515 if ((*add_input_file)(objPath) != LDPS_OK) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000516 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
Rafael Espindola5a287d72011-02-16 11:19:44 +0000517 (*message)(LDPL_ERROR, "File left behind in: %s", objPath);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000518 return LDPS_ERR;
519 }
520
Rafael Espindola11f403c2010-06-23 20:20:59 +0000521 if (!options::extra_library_path.empty() &&
522 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) {
523 (*message)(LDPL_ERROR, "Unable to set the extra library path.");
524 return LDPS_ERR;
525 }
526
Rafael Espindoladd76f182010-06-18 19:18:58 +0000527 for (std::vector<std::string>::iterator i = options::pass_through.begin(),
528 e = options::pass_through.end();
529 i != e; ++i) {
530 std::string &item = *i;
Rafael Espindolabebb6402010-06-21 02:23:12 +0000531 const char *item_p = item.c_str();
Rafael Espindoladd76f182010-06-18 19:18:58 +0000532 if (llvm::StringRef(item).startswith("-l")) {
533 if (add_input_library(item_p + 2) != LDPS_OK) {
534 (*message)(LDPL_ERROR, "Unable to add library to the link.");
535 return LDPS_ERR;
536 }
537 } else {
538 if (add_input_file(item_p) != LDPS_OK) {
539 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
540 return LDPS_ERR;
541 }
542 }
543 }
544
Rafael Espindola5a287d72011-02-16 11:19:44 +0000545 if (options::obj_path.empty())
546 Cleanup.push_back(sys::Path(objPath));
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000547
548 return LDPS_OK;
549}
550
Dan Gohmanf6920032010-04-16 00:42:57 +0000551static ld_plugin_status cleanup_hook(void) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000552 std::string ErrMsg;
553
554 for (int i = 0, e = Cleanup.size(); i != e; ++i)
555 if (Cleanup[i].eraseFromDisk(false, &ErrMsg))
556 (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),
557 ErrMsg.c_str());
558
559 return LDPS_OK;
560}