blob: 950c7435ec28c0d64aeba413b3453d66c6f4ca18 [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 Espindoladd76f182010-06-18 19:18:58 +000076 static std::vector<std::string> pass_through;
Rafael Espindola11f403c2010-06-23 20:20:59 +000077 static std::string extra_library_path;
Rafael Espindolacbb170d2010-08-09 21:09:46 +000078 static std::string triple;
Rafael Espindola2d643ef2010-08-11 00:15:13 +000079 static std::string mcpu;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000080 // Additional options to pass into the code generator.
Nick Lewyckyfc55def2010-06-03 17:10:17 +000081 // Note: This array will contain all plugin options which are not claimed
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000082 // as plugin exclusive to pass to the code generator.
Nick Lewyckyfc55def2010-06-03 17:10:17 +000083 // For example, "generate-api-file" and "as"options are for the plugin
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000084 // use only and will not be passed.
Dan Gohmanf6920032010-04-16 00:42:57 +000085 static std::vector<std::string> extra;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000086
Rafael Espindola6c809922010-06-07 16:45:22 +000087 static void process_plugin_option(const char* opt_)
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000088 {
Rafael Espindola6c809922010-06-07 16:45:22 +000089 if (opt_ == NULL)
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000090 return;
Rafael Espindola6c809922010-06-07 16:45:22 +000091 llvm::StringRef opt = opt_;
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000092
Rafael Espindola6c809922010-06-07 16:45:22 +000093 if (opt == "generate-api-file") {
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +000094 generate_api_file = true;
Rafael Espindola2d643ef2010-08-11 00:15:13 +000095 } else if (opt.startswith("mcpu=")) {
96 mcpu = opt.substr(strlen("mcpu="));
Rafael Espindola11f403c2010-06-23 20:20:59 +000097 } else if (opt.startswith("extra-library-path=")) {
98 extra_library_path = opt.substr(strlen("extra_library_path="));
Rafael Espindoladd76f182010-06-18 19:18:58 +000099 } else if (opt.startswith("pass-through=")) {
100 llvm::StringRef item = opt.substr(strlen("pass-through="));
101 pass_through.push_back(item.str());
Rafael Espindola15af3872010-08-10 16:32:15 +0000102 } else if (opt.startswith("mtriple=")) {
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000103 triple = opt.substr(strlen("mtriple="));
Rafael Espindola5a287d72011-02-16 11:19:44 +0000104 } else if (opt.startswith("obj-path=")) {
105 obj_path = opt.substr(strlen("obj-path="));
Rafael Espindola6c809922010-06-07 16:45:22 +0000106 } else if (opt == "emit-llvm") {
Rafael Espindolac4b55612010-06-03 21:11:20 +0000107 generate_bc_file = BC_ONLY;
Rafael Espindola6c809922010-06-07 16:45:22 +0000108 } else if (opt == "also-emit-llvm") {
Rafael Espindolac4b55612010-06-03 21:11:20 +0000109 generate_bc_file = BC_ALSO;
Rafael Espindola6c809922010-06-07 16:45:22 +0000110 } else if (opt.startswith("also-emit-llvm=")) {
111 llvm::StringRef path = opt.substr(strlen("also-emit-llvm="));
Rafael Espindolac4b55612010-06-03 21:11:20 +0000112 generate_bc_file = BC_ALSO;
Nick Lewycky662f7382010-06-03 17:13:23 +0000113 if (!bc_path.empty()) {
Rafael Espindola62bacd62010-05-13 13:39:31 +0000114 (*message)(LDPL_WARNING, "Path to the output IL file specified twice. "
Rafael Espindola6c809922010-06-07 16:45:22 +0000115 "Discarding %s", opt_);
Rafael Espindola62bacd62010-05-13 13:39:31 +0000116 } else {
117 bc_path = path;
118 }
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000119 } else {
120 // Save this option to pass to the code generator.
Rafael Espindola6c809922010-06-07 16:45:22 +0000121 extra.push_back(opt);
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000122 }
123 }
124}
125
Dan Gohmanf6920032010-04-16 00:42:57 +0000126static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
127 int *claimed);
128static ld_plugin_status all_symbols_read_hook(void);
129static ld_plugin_status cleanup_hook(void);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000130
131extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
132ld_plugin_status onload(ld_plugin_tv *tv) {
133 // We're given a pointer to the first transfer vector. We read through them
134 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
135 // contain pointers to functions that we need to call to register our own
136 // hooks. The others are addresses of functions we can use to call into gold
137 // for services.
138
139 bool registeredClaimFile = false;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000140
141 for (; tv->tv_tag != LDPT_NULL; ++tv) {
142 switch (tv->tv_tag) {
143 case LDPT_API_VERSION:
144 api_version = tv->tv_u.tv_val;
145 break;
146 case LDPT_GOLD_VERSION: // major * 100 + minor
147 gold_version = tv->tv_u.tv_val;
148 break;
Rafael Espindolac4b55612010-06-03 21:11:20 +0000149 case LDPT_OUTPUT_NAME:
150 output_name = tv->tv_u.tv_string;
151 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000152 case LDPT_LINKER_OUTPUT:
153 switch (tv->tv_u.tv_val) {
154 case LDPO_REL: // .o
155 case LDPO_DYN: // .so
156 output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
157 break;
158 case LDPO_EXEC: // .exe
159 output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
160 break;
161 default:
162 (*message)(LDPL_ERROR, "Unknown output file type %d",
163 tv->tv_u.tv_val);
164 return LDPS_ERR;
165 }
166 // TODO: add an option to disable PIC.
167 //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
168 break;
169 case LDPT_OPTION:
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000170 options::process_plugin_option(tv->tv_u.tv_string);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000171 break;
172 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
173 ld_plugin_register_claim_file callback;
174 callback = tv->tv_u.tv_register_claim_file;
175
176 if ((*callback)(claim_file_hook) != LDPS_OK)
177 return LDPS_ERR;
178
179 registeredClaimFile = true;
180 } break;
181 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
182 ld_plugin_register_all_symbols_read callback;
183 callback = tv->tv_u.tv_register_all_symbols_read;
184
185 if ((*callback)(all_symbols_read_hook) != LDPS_OK)
186 return LDPS_ERR;
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000187
188 code_gen = lto_codegen_create();
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000189 } break;
190 case LDPT_REGISTER_CLEANUP_HOOK: {
191 ld_plugin_register_cleanup callback;
192 callback = tv->tv_u.tv_register_cleanup;
193
194 if ((*callback)(cleanup_hook) != LDPS_OK)
195 return LDPS_ERR;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000196 } break;
197 case LDPT_ADD_SYMBOLS:
198 add_symbols = tv->tv_u.tv_add_symbols;
199 break;
200 case LDPT_GET_SYMBOLS:
201 get_symbols = tv->tv_u.tv_get_symbols;
202 break;
203 case LDPT_ADD_INPUT_FILE:
204 add_input_file = tv->tv_u.tv_add_input_file;
205 break;
Rafael Espindoladd76f182010-06-18 19:18:58 +0000206 case LDPT_ADD_INPUT_LIBRARY:
207 add_input_library = tv->tv_u.tv_add_input_file;
208 break;
Rafael Espindola11f403c2010-06-23 20:20:59 +0000209 case LDPT_SET_EXTRA_LIBRARY_PATH:
210 set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
211 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000212 case LDPT_MESSAGE:
213 message = tv->tv_u.tv_message;
214 break;
215 default:
216 break;
217 }
218 }
219
Rafael Espindola98c507e2009-02-18 08:30:15 +0000220 if (!registeredClaimFile) {
Rafael Espindola6210a942009-02-18 17:49:06 +0000221 (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
222 return LDPS_ERR;
223 }
Rafael Espindola98c507e2009-02-18 08:30:15 +0000224 if (!add_symbols) {
Rafael Espindola6210a942009-02-18 17:49:06 +0000225 (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
226 return LDPS_ERR;
227 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000228
229 return LDPS_OK;
230}
231
232/// claim_file_hook - called by gold to see whether this file is one that
233/// our plugin can handle. We'll try to open it and register all the symbols
234/// with add_symbol if possible.
Dan Gohmanf6920032010-04-16 00:42:57 +0000235static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
236 int *claimed) {
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000237 lto_module_t M;
238
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000239 if (file->offset) {
Nick Lewyckyc1da8862009-02-05 04:14:23 +0000240 // Gold has found what might be IR part-way inside of a file, such as
241 // an .a archive.
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000242 if (lseek(file->fd, file->offset, SEEK_SET) == -1) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000243 (*message)(LDPL_ERROR,
Nick Lewyckyfc55def2010-06-03 17:10:17 +0000244 "Failed to seek to archive member of %s at offset %d: %s\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000245 file->name,
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +0000246 file->offset, sys::StrError(errno).c_str());
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000247 return LDPS_ERR;
248 }
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000249 void *buf = malloc(file->filesize);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000250 if (!buf) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000251 (*message)(LDPL_ERROR,
Nick Lewyckyfc55def2010-06-03 17:10:17 +0000252 "Failed to allocate buffer for archive member of size: %d\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000253 file->filesize);
254 return LDPS_ERR;
255 }
256 if (read(file->fd, buf, file->filesize) != file->filesize) {
Nick Lewycky0df91b22009-02-07 03:15:01 +0000257 (*message)(LDPL_ERROR,
258 "Failed to read archive member of %s at offset %d: %s\n",
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000259 file->name,
Nick Lewycky0df91b22009-02-07 03:15:01 +0000260 file->offset,
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +0000261 sys::StrError(errno).c_str());
Nick Lewycky0df91b22009-02-07 03:15:01 +0000262 free(buf);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000263 return LDPS_ERR;
264 }
Nick Lewycky0df91b22009-02-07 03:15:01 +0000265 if (!lto_module_is_object_file_in_memory(buf, file->filesize)) {
266 free(buf);
Torok Edwin3e5a0d82009-02-04 17:39:30 +0000267 return LDPS_OK;
Nick Lewycky0df91b22009-02-07 03:15:01 +0000268 }
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000269 M = lto_module_create_from_memory(buf, file->filesize);
Rafael Espindola37d42f82011-02-19 21:49:57 +0000270 if (!M) {
271 (*message)(LDPL_ERROR, "Failed to create LLVM module: %s",
272 lto_get_error_message());
273 return LDPS_ERR;
274 }
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000275 free(buf);
276 } else {
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000277 lseek(file->fd, 0, SEEK_SET);
Rafael Espindola35358e02011-02-27 20:15:37 +0000278 M = lto_module_create_from_fd(file->fd, file->name, file->filesize);
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000279 if (!M)
280 return LDPS_OK;
281 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000282
283 *claimed = 1;
284 Modules.resize(Modules.size() + 1);
285 claimed_file &cf = Modules.back();
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000286
287 if (!options::triple.empty())
Rafael Espindola37d42f82011-02-19 21:49:57 +0000288 lto_module_set_target_triple(M, options::triple.c_str());
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000289
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000290 cf.handle = file->handle;
Rafael Espindola37d42f82011-02-19 21:49:57 +0000291 unsigned sym_count = lto_module_get_num_symbols(M);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000292 cf.syms.reserve(sym_count);
293
294 for (unsigned i = 0; i != sym_count; ++i) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000295 lto_symbol_attributes attrs = lto_module_get_symbol_attribute(M, i);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000296 if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
297 continue;
298
299 cf.syms.push_back(ld_plugin_symbol());
300 ld_plugin_symbol &sym = cf.syms.back();
Rafael Espindola37d42f82011-02-19 21:49:57 +0000301 sym.name = const_cast<char *>(lto_module_get_symbol_name(M, i));
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000302 sym.name = strdup(sym.name);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000303 sym.version = NULL;
304
305 int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
306 switch (scope) {
307 case LTO_SYMBOL_SCOPE_HIDDEN:
308 sym.visibility = LDPV_HIDDEN;
309 break;
310 case LTO_SYMBOL_SCOPE_PROTECTED:
311 sym.visibility = LDPV_PROTECTED;
312 break;
313 case 0: // extern
314 case LTO_SYMBOL_SCOPE_DEFAULT:
315 sym.visibility = LDPV_DEFAULT;
316 break;
317 default:
318 (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
319 return LDPS_ERR;
320 }
321
322 int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000323 sym.comdat_key = NULL;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000324 switch (definition) {
325 case LTO_SYMBOL_DEFINITION_REGULAR:
326 sym.def = LDPK_DEF;
327 break;
328 case LTO_SYMBOL_DEFINITION_UNDEFINED:
329 sym.def = LDPK_UNDEF;
330 break;
331 case LTO_SYMBOL_DEFINITION_TENTATIVE:
332 sym.def = LDPK_COMMON;
333 break;
334 case LTO_SYMBOL_DEFINITION_WEAK:
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000335 sym.comdat_key = sym.name;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000336 sym.def = LDPK_WEAKDEF;
337 break;
Rafael Espindola7431af02009-04-24 16:55:21 +0000338 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
339 sym.def = LDPK_WEAKUNDEF;
340 break;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000341 default:
342 (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition);
343 return LDPS_ERR;
344 }
345
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000346 sym.size = 0;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000347
348 sym.resolution = LDPR_UNKNOWN;
349 }
350
351 cf.syms.reserve(cf.syms.size());
352
353 if (!cf.syms.empty()) {
354 if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
355 (*message)(LDPL_ERROR, "Unable to add symbols!");
356 return LDPS_ERR;
357 }
358 }
359
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000360 if (code_gen)
361 lto_codegen_add_module(code_gen, M);
362
363 lto_module_dispose(M);
364
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000365 return LDPS_OK;
366}
367
368/// all_symbols_read_hook - gold informs us that all symbols have been read.
369/// At this point, we use get_symbols to see if any of our definitions have
370/// been overridden by a native object file. Then, perform optimization and
371/// codegen.
Dan Gohmanf6920032010-04-16 00:42:57 +0000372static ld_plugin_status all_symbols_read_hook(void) {
Nick Lewyckyca428622009-02-22 22:15:44 +0000373 std::ofstream api_file;
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000374 assert(code_gen);
375
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000376 if (options::generate_api_file) {
Nick Lewyckyca428622009-02-22 22:15:44 +0000377 api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
378 if (!api_file.is_open()) {
379 (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing.");
380 abort();
381 }
382 }
383
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000384 // If we don't preserve any symbols, libLTO will assume that all symbols are
385 // needed. Keep all symbols unless we're producing a final executable.
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000386 bool anySymbolsPreserved = false;
387 for (std::list<claimed_file>::iterator I = Modules.begin(),
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000388 E = Modules.end(); I != E; ++I) {
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000389 (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
390 for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
391 if (I->syms[i].resolution == LDPR_PREVAILING_DEF) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000392 lto_codegen_add_must_preserve_symbol(code_gen, I->syms[i].name);
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000393 anySymbolsPreserved = true;
Nick Lewyckyca428622009-02-22 22:15:44 +0000394
Rafael Espindolac72f8e92010-06-03 14:45:44 +0000395 if (options::generate_api_file)
396 api_file << I->syms[i].name << "\n";
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000397 }
398 }
Rafael Espindola38a979b2010-06-14 21:20:52 +0000399 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000400
Rafael Espindola38a979b2010-06-14 21:20:52 +0000401 if (options::generate_api_file)
402 api_file.close();
Nick Lewyckyca428622009-02-22 22:15:44 +0000403
Rafael Espindola38a979b2010-06-14 21:20:52 +0000404 if (!anySymbolsPreserved) {
405 // All of the IL is unnecessary!
Rafael Espindola37d42f82011-02-19 21:49:57 +0000406 lto_codegen_dispose(code_gen);
Rafael Espindola38a979b2010-06-14 21:20:52 +0000407 return LDPS_OK;
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000408 }
409
Rafael Espindola37d42f82011-02-19 21:49:57 +0000410 lto_codegen_set_pic_model(code_gen, output_type);
411 lto_codegen_set_debug_model(code_gen, LTO_DEBUG_MODEL_DWARF);
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000412 if (!options::mcpu.empty())
Rafael Espindola37d42f82011-02-19 21:49:57 +0000413 lto_codegen_set_cpu(code_gen, options::mcpu.c_str());
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000414
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000415 // Pass through extra options to the code generator.
416 if (!options::extra.empty()) {
417 for (std::vector<std::string>::iterator it = options::extra.begin();
418 it != options::extra.end(); ++it) {
Rafael Espindola37d42f82011-02-19 21:49:57 +0000419 lto_codegen_debug_options(code_gen, (*it).c_str());
Viktor Kutuzov5c00b4a2009-10-28 18:55:55 +0000420 }
421 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000422
Rafael Espindolac4b55612010-06-03 21:11:20 +0000423 if (options::generate_bc_file != options::BC_NO) {
424 std::string path;
425 if (options::generate_bc_file == options::BC_ONLY)
426 path = output_name;
427 else if (!options::bc_path.empty())
428 path = options::bc_path;
429 else
430 path = output_name + ".bc";
Rafael Espindola37d42f82011-02-19 21:49:57 +0000431 bool err = lto_codegen_write_merged_modules(code_gen, path.c_str());
Rafael Espindola62bacd62010-05-13 13:39:31 +0000432 if (err)
433 (*message)(LDPL_FATAL, "Failed to write the output file.");
Rafael Espindolac4b55612010-06-03 21:11:20 +0000434 if (options::generate_bc_file == options::BC_ONLY)
435 exit(0);
Rafael Espindola62bacd62010-05-13 13:39:31 +0000436 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000437 size_t bufsize = 0;
Rafael Espindola37d42f82011-02-19 21:49:57 +0000438 const char *buffer = static_cast<const char *>(lto_codegen_compile(code_gen,
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000439 &bufsize));
440
441 std::string ErrMsg;
442
Rafael Espindola5a287d72011-02-16 11:19:44 +0000443 const char *objPath;
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000444 sys::Path uniqueObjPath("/tmp/llvmgold.o");
Rafael Espindola5a287d72011-02-16 11:19:44 +0000445 if (!options::obj_path.empty()) {
446 objPath = options::obj_path.c_str();
447 } else {
Rafael Espindola5a287d72011-02-16 11:19:44 +0000448 if (uniqueObjPath.createTemporaryFileOnDisk(true, &ErrMsg)) {
449 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
450 return LDPS_ERR;
451 }
452 objPath = uniqueObjPath.c_str();
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000453 }
Rafael Espindola5a287d72011-02-16 11:19:44 +0000454 tool_output_file objFile(objPath, ErrMsg,
455 raw_fd_ostream::F_Binary);
456 if (!ErrMsg.empty()) {
457 (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
458 return LDPS_ERR;
459 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000460
Nick Lewycky59b4d2a2010-09-02 05:44:31 +0000461 objFile.os().write(buffer, bufsize);
462 objFile.os().close();
463 if (objFile.os().has_error()) {
Dan Gohmanf2914012010-08-20 16:59:15 +0000464 (*message)(LDPL_ERROR, "Error writing output file '%s'",
Rafael Espindola5a287d72011-02-16 11:19:44 +0000465 objPath);
Nick Lewycky59b4d2a2010-09-02 05:44:31 +0000466 objFile.os().clear_error();
Dan Gohmanf2914012010-08-20 16:59:15 +0000467 return LDPS_ERR;
468 }
469 objFile.keep();
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000470
Rafael Espindola37d42f82011-02-19 21:49:57 +0000471 lto_codegen_dispose(code_gen);
Rafael Espindola8e04fc32011-02-20 18:28:29 +0000472 for (std::list<claimed_file>::iterator I = Modules.begin(),
473 E = Modules.end(); I != E; ++I) {
474 for (unsigned i = 0; i != I->syms.size(); ++i) {
475 ld_plugin_symbol &sym = I->syms[i];
476 free(sym.name);
477 }
478 }
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000479
Rafael Espindola5a287d72011-02-16 11:19:44 +0000480 if ((*add_input_file)(objPath) != LDPS_OK) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000481 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
Rafael Espindola5a287d72011-02-16 11:19:44 +0000482 (*message)(LDPL_ERROR, "File left behind in: %s", objPath);
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000483 return LDPS_ERR;
484 }
485
Rafael Espindola11f403c2010-06-23 20:20:59 +0000486 if (!options::extra_library_path.empty() &&
487 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) {
488 (*message)(LDPL_ERROR, "Unable to set the extra library path.");
489 return LDPS_ERR;
490 }
491
Rafael Espindoladd76f182010-06-18 19:18:58 +0000492 for (std::vector<std::string>::iterator i = options::pass_through.begin(),
493 e = options::pass_through.end();
494 i != e; ++i) {
495 std::string &item = *i;
Rafael Espindolabebb6402010-06-21 02:23:12 +0000496 const char *item_p = item.c_str();
Rafael Espindoladd76f182010-06-18 19:18:58 +0000497 if (llvm::StringRef(item).startswith("-l")) {
498 if (add_input_library(item_p + 2) != LDPS_OK) {
499 (*message)(LDPL_ERROR, "Unable to add library to the link.");
500 return LDPS_ERR;
501 }
502 } else {
503 if (add_input_file(item_p) != LDPS_OK) {
504 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
505 return LDPS_ERR;
506 }
507 }
508 }
509
Rafael Espindola5a287d72011-02-16 11:19:44 +0000510 if (options::obj_path.empty())
511 Cleanup.push_back(sys::Path(objPath));
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000512
513 return LDPS_OK;
514}
515
Dan Gohmanf6920032010-04-16 00:42:57 +0000516static ld_plugin_status cleanup_hook(void) {
Nick Lewycky3e62b2d2009-02-03 07:13:24 +0000517 std::string ErrMsg;
518
519 for (int i = 0, e = Cleanup.size(); i != e; ++i)
520 if (Cleanup[i].eraseFromDisk(false, &ErrMsg))
521 (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),
522 ErrMsg.c_str());
523
524 return LDPS_OK;
525}