blob: 40f5fd60865324e7d68a2975842ba7ef7fad2199 [file] [log] [blame]
Nick Lewyckyfb643e42009-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
Dylan Noblesmith9e5b1782011-12-22 23:04:07 +000015#include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
Nick Lewyckyfb643e42009-02-03 07:13:24 +000016#include "plugin-api.h"
Nick Lewyckyfb643e42009-02-03 07:13:24 +000017#include "llvm-c/lto.h"
Ivan Krasin5021af52011-09-12 21:47:50 +000018#include "llvm/ADT/OwningPtr.h"
Michael J. Spencerab425d82010-11-29 18:47:54 +000019#include "llvm/Support/Errno.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000020#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencerab425d82010-11-29 18:47:54 +000021#include "llvm/Support/Path.h"
22#include "llvm/Support/Program.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000023#include "llvm/Support/ToolOutputFile.h"
24#include "llvm/Support/system_error.h"
Torok Edwina488ee0e2009-02-04 21:00:02 +000025#include <cerrno>
Nick Lewyckyfb643e42009-02-03 07:13:24 +000026#include <cstdlib>
27#include <cstring>
Nick Lewycky338d07e2009-02-22 22:15:44 +000028#include <fstream>
Nick Lewyckyfb643e42009-02-03 07:13:24 +000029#include <list>
30#include <vector>
31
Michael J. Spencer06f52232011-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 Lewyckyfb643e42009-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 Espindola82976402010-06-18 19:18:58 +000051 ld_plugin_add_input_library add_input_library = NULL;
Rafael Espindolaef498152010-06-23 20:20:59 +000052 ld_plugin_set_extra_library_path set_extra_library_path = NULL;
Rafael Espindolaece7c9c2011-04-07 21:11:00 +000053 ld_plugin_get_view get_view = NULL;
Nick Lewyckyfb643e42009-02-03 07:13:24 +000054 ld_plugin_message message = discard_message;
55
56 int api_version = 0;
57 int gold_version = 0;
58
59 struct claimed_file {
Nick Lewyckyfb643e42009-02-03 07:13:24 +000060 void *handle;
61 std::vector<ld_plugin_symbol> syms;
62 };
63
64 lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
Rafael Espindola8fb957e2010-06-03 21:11:20 +000065 std::string output_name = "";
Nick Lewyckyfb643e42009-02-03 07:13:24 +000066 std::list<claimed_file> Modules;
67 std::vector<sys::Path> Cleanup;
Rafael Espindola9ef90d52011-02-20 18:28:29 +000068 lto_code_gen_t code_gen = NULL;
Nick Lewyckyfb643e42009-02-03 07:13:24 +000069}
70
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000071namespace options {
Rafael Espindola8fb957e2010-06-03 21:11:20 +000072 enum generate_bc { BC_NO, BC_ALSO, BC_ONLY };
Dan Gohmanebb4ae02010-04-16 00:42:57 +000073 static bool generate_api_file = false;
Rafael Espindola8fb957e2010-06-03 21:11:20 +000074 static generate_bc generate_bc_file = BC_NO;
Rafael Espindolaba3398b2010-05-13 13:39:31 +000075 static std::string bc_path;
Rafael Espindola6b6ed5d2011-02-16 11:19:44 +000076 static std::string obj_path;
Rafael Espindolaef498152010-06-23 20:20:59 +000077 static std::string extra_library_path;
Rafael Espindola4ef89f52010-08-09 21:09:46 +000078 static std::string triple;
Rafael Espindolaccab1dd2010-08-11 00:15:13 +000079 static std::string mcpu;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000080 // Additional options to pass into the code generator.
Nick Lewycky0ac5e222010-06-03 17:10:17 +000081 // Note: This array will contain all plugin options which are not claimed
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000082 // as plugin exclusive to pass to the code generator.
Nick Lewycky0ac5e222010-06-03 17:10:17 +000083 // For example, "generate-api-file" and "as"options are for the plugin
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000084 // use only and will not be passed.
Dan Gohmanebb4ae02010-04-16 00:42:57 +000085 static std::vector<std::string> extra;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000086
Rafael Espindolac4dca3a2010-06-07 16:45:22 +000087 static void process_plugin_option(const char* opt_)
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000088 {
Rafael Espindolac4dca3a2010-06-07 16:45:22 +000089 if (opt_ == NULL)
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000090 return;
Rafael Espindolac4dca3a2010-06-07 16:45:22 +000091 llvm::StringRef opt = opt_;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000092
Rafael Espindolac4dca3a2010-06-07 16:45:22 +000093 if (opt == "generate-api-file") {
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000094 generate_api_file = true;
Rafael Espindolaccab1dd2010-08-11 00:15:13 +000095 } else if (opt.startswith("mcpu=")) {
96 mcpu = opt.substr(strlen("mcpu="));
Rafael Espindolaef498152010-06-23 20:20:59 +000097 } else if (opt.startswith("extra-library-path=")) {
98 extra_library_path = opt.substr(strlen("extra_library_path="));
Rafael Espindola148c3282010-08-10 16:32:15 +000099 } else if (opt.startswith("mtriple=")) {
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000100 triple = opt.substr(strlen("mtriple="));
Rafael Espindola6b6ed5d2011-02-16 11:19:44 +0000101 } else if (opt.startswith("obj-path=")) {
102 obj_path = opt.substr(strlen("obj-path="));
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000103 } else if (opt == "emit-llvm") {
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000104 generate_bc_file = BC_ONLY;
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000105 } else if (opt == "also-emit-llvm") {
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000106 generate_bc_file = BC_ALSO;
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000107 } else if (opt.startswith("also-emit-llvm=")) {
108 llvm::StringRef path = opt.substr(strlen("also-emit-llvm="));
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000109 generate_bc_file = BC_ALSO;
Nick Lewyckyca4180c2010-06-03 17:13:23 +0000110 if (!bc_path.empty()) {
Rafael Espindolaba3398b2010-05-13 13:39:31 +0000111 (*message)(LDPL_WARNING, "Path to the output IL file specified twice. "
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000112 "Discarding %s", opt_);
Rafael Espindolaba3398b2010-05-13 13:39:31 +0000113 } else {
114 bc_path = path;
115 }
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000116 } else {
117 // Save this option to pass to the code generator.
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000118 extra.push_back(opt);
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000119 }
120 }
121}
122
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000123static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
124 int *claimed);
125static ld_plugin_status all_symbols_read_hook(void);
126static ld_plugin_status cleanup_hook(void);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000127
128extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
129ld_plugin_status onload(ld_plugin_tv *tv) {
130 // We're given a pointer to the first transfer vector. We read through them
131 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
132 // contain pointers to functions that we need to call to register our own
133 // hooks. The others are addresses of functions we can use to call into gold
134 // for services.
135
136 bool registeredClaimFile = false;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000137
138 for (; tv->tv_tag != LDPT_NULL; ++tv) {
139 switch (tv->tv_tag) {
140 case LDPT_API_VERSION:
141 api_version = tv->tv_u.tv_val;
142 break;
143 case LDPT_GOLD_VERSION: // major * 100 + minor
144 gold_version = tv->tv_u.tv_val;
145 break;
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000146 case LDPT_OUTPUT_NAME:
147 output_name = tv->tv_u.tv_string;
148 break;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000149 case LDPT_LINKER_OUTPUT:
150 switch (tv->tv_u.tv_val) {
151 case LDPO_REL: // .o
152 case LDPO_DYN: // .so
Rafael Espindola9aa1fcc2012-06-13 13:30:24 +0000153 // FIXME: Replace 3 with LDPO_PIE once that is in a released binutils.
154 case 3: // position independent executable
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000155 output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
156 break;
157 case LDPO_EXEC: // .exe
158 output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
159 break;
160 default:
161 (*message)(LDPL_ERROR, "Unknown output file type %d",
162 tv->tv_u.tv_val);
163 return LDPS_ERR;
164 }
165 // TODO: add an option to disable PIC.
166 //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
167 break;
168 case LDPT_OPTION:
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000169 options::process_plugin_option(tv->tv_u.tv_string);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000170 break;
171 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
172 ld_plugin_register_claim_file callback;
173 callback = tv->tv_u.tv_register_claim_file;
174
175 if ((*callback)(claim_file_hook) != LDPS_OK)
176 return LDPS_ERR;
177
178 registeredClaimFile = true;
179 } break;
180 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
181 ld_plugin_register_all_symbols_read callback;
182 callback = tv->tv_u.tv_register_all_symbols_read;
183
184 if ((*callback)(all_symbols_read_hook) != LDPS_OK)
185 return LDPS_ERR;
Rafael Espindola9ef90d52011-02-20 18:28:29 +0000186
187 code_gen = lto_codegen_create();
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000188 } break;
189 case LDPT_REGISTER_CLEANUP_HOOK: {
190 ld_plugin_register_cleanup callback;
191 callback = tv->tv_u.tv_register_cleanup;
192
193 if ((*callback)(cleanup_hook) != LDPS_OK)
194 return LDPS_ERR;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000195 } break;
196 case LDPT_ADD_SYMBOLS:
197 add_symbols = tv->tv_u.tv_add_symbols;
198 break;
199 case LDPT_GET_SYMBOLS:
200 get_symbols = tv->tv_u.tv_get_symbols;
201 break;
202 case LDPT_ADD_INPUT_FILE:
203 add_input_file = tv->tv_u.tv_add_input_file;
204 break;
Rafael Espindola82976402010-06-18 19:18:58 +0000205 case LDPT_ADD_INPUT_LIBRARY:
206 add_input_library = tv->tv_u.tv_add_input_file;
207 break;
Rafael Espindolaef498152010-06-23 20:20:59 +0000208 case LDPT_SET_EXTRA_LIBRARY_PATH:
209 set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
210 break;
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000211 case LDPT_GET_VIEW:
212 get_view = tv->tv_u.tv_get_view;
213 break;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000214 case LDPT_MESSAGE:
215 message = tv->tv_u.tv_message;
216 break;
217 default:
218 break;
219 }
220 }
221
Rafael Espindolae08484d2009-02-18 08:30:15 +0000222 if (!registeredClaimFile) {
Rafael Espindola6add6182009-02-18 17:49:06 +0000223 (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
224 return LDPS_ERR;
225 }
Rafael Espindolae08484d2009-02-18 08:30:15 +0000226 if (!add_symbols) {
Rafael Espindola6add6182009-02-18 17:49:06 +0000227 (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
228 return LDPS_ERR;
229 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000230
231 return LDPS_OK;
232}
233
234/// claim_file_hook - called by gold to see whether this file is one that
235/// our plugin can handle. We'll try to open it and register all the symbols
236/// with add_symbol if possible.
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000237static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
238 int *claimed) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000239 lto_module_t M;
Ivan Krasin5021af52011-09-12 21:47:50 +0000240 const void *view;
241 OwningPtr<MemoryBuffer> buffer;
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000242 if (get_view) {
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000243 if (get_view(file->handle, &view) != LDPS_OK) {
244 (*message)(LDPL_ERROR, "Failed to get a view of %s", file->name);
245 return LDPS_ERR;
246 }
Ivan Krasin5021af52011-09-12 21:47:50 +0000247 } else {
Ivan Krasin639222d2011-09-15 23:13:00 +0000248 int64_t offset = 0;
Nick Lewycky8691c472009-02-05 04:14:23 +0000249 // Gold has found what might be IR part-way inside of a file, such as
250 // an .a archive.
Ivan Krasin5021af52011-09-12 21:47:50 +0000251 if (file->offset) {
252 offset = file->offset;
253 }
254 if (error_code ec =
255 MemoryBuffer::getOpenFile(file->fd, file->name, buffer, file->filesize,
256 -1, offset, false)) {
257 (*message)(LDPL_ERROR, ec.message().c_str());
258 return LDPS_ERR;
259 }
260 view = buffer->getBufferStart();
Rafael Espindola56e41f72011-02-08 22:40:47 +0000261 }
Ivan Krasin5021af52011-09-12 21:47:50 +0000262
263 if (!lto_module_is_object_file_in_memory(view, file->filesize))
264 return LDPS_OK;
265
266 M = lto_module_create_from_memory(view, file->filesize);
Ivan Krasind5f2d8c2011-09-09 00:14:04 +0000267 if (!M) {
268 if (const char* msg = lto_get_error_message()) {
Ivan Krasin5021af52011-09-12 21:47:50 +0000269 (*message)(LDPL_ERROR,
270 "LLVM gold plugin has failed to create LTO module: %s",
271 msg);
Ivan Krasind5f2d8c2011-09-09 00:14:04 +0000272 return LDPS_ERR;
273 }
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000274 return LDPS_OK;
Ivan Krasind5f2d8c2011-09-09 00:14:04 +0000275 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000276
277 *claimed = 1;
278 Modules.resize(Modules.size() + 1);
279 claimed_file &cf = Modules.back();
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000280
281 if (!options::triple.empty())
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000282 lto_module_set_target_triple(M, options::triple.c_str());
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000283
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000284 cf.handle = file->handle;
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000285 unsigned sym_count = lto_module_get_num_symbols(M);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000286 cf.syms.reserve(sym_count);
287
288 for (unsigned i = 0; i != sym_count; ++i) {
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000289 lto_symbol_attributes attrs = lto_module_get_symbol_attribute(M, i);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000290 if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
291 continue;
292
293 cf.syms.push_back(ld_plugin_symbol());
294 ld_plugin_symbol &sym = cf.syms.back();
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000295 sym.name = const_cast<char *>(lto_module_get_symbol_name(M, i));
Rafael Espindola9ef90d52011-02-20 18:28:29 +0000296 sym.name = strdup(sym.name);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000297 sym.version = NULL;
298
299 int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
300 switch (scope) {
301 case LTO_SYMBOL_SCOPE_HIDDEN:
302 sym.visibility = LDPV_HIDDEN;
303 break;
304 case LTO_SYMBOL_SCOPE_PROTECTED:
305 sym.visibility = LDPV_PROTECTED;
306 break;
307 case 0: // extern
308 case LTO_SYMBOL_SCOPE_DEFAULT:
309 sym.visibility = LDPV_DEFAULT;
310 break;
311 default:
312 (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
313 return LDPS_ERR;
314 }
315
316 int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
Rafael Espindola70d80152011-02-14 22:23:49 +0000317 sym.comdat_key = NULL;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000318 switch (definition) {
319 case LTO_SYMBOL_DEFINITION_REGULAR:
320 sym.def = LDPK_DEF;
321 break;
322 case LTO_SYMBOL_DEFINITION_UNDEFINED:
323 sym.def = LDPK_UNDEF;
324 break;
325 case LTO_SYMBOL_DEFINITION_TENTATIVE:
326 sym.def = LDPK_COMMON;
327 break;
328 case LTO_SYMBOL_DEFINITION_WEAK:
Rafael Espindola70d80152011-02-14 22:23:49 +0000329 sym.comdat_key = sym.name;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000330 sym.def = LDPK_WEAKDEF;
331 break;
Rafael Espindola56548522009-04-24 16:55:21 +0000332 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
333 sym.def = LDPK_WEAKUNDEF;
334 break;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000335 default:
336 (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition);
337 return LDPS_ERR;
338 }
339
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000340 sym.size = 0;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000341
342 sym.resolution = LDPR_UNKNOWN;
343 }
344
345 cf.syms.reserve(cf.syms.size());
346
347 if (!cf.syms.empty()) {
348 if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
349 (*message)(LDPL_ERROR, "Unable to add symbols!");
350 return LDPS_ERR;
351 }
352 }
353
Rafael Espindola9ef90d52011-02-20 18:28:29 +0000354 if (code_gen)
355 lto_codegen_add_module(code_gen, M);
356
357 lto_module_dispose(M);
358
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000359 return LDPS_OK;
360}
361
362/// all_symbols_read_hook - gold informs us that all symbols have been read.
363/// At this point, we use get_symbols to see if any of our definitions have
364/// been overridden by a native object file. Then, perform optimization and
365/// codegen.
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000366static ld_plugin_status all_symbols_read_hook(void) {
Nick Lewycky338d07e2009-02-22 22:15:44 +0000367 std::ofstream api_file;
Rafael Espindola9ef90d52011-02-20 18:28:29 +0000368 assert(code_gen);
369
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000370 if (options::generate_api_file) {
Nick Lewycky338d07e2009-02-22 22:15:44 +0000371 api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
372 if (!api_file.is_open()) {
373 (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing.");
374 abort();
375 }
376 }
377
Rafael Espindola00121822010-06-03 14:45:44 +0000378 for (std::list<claimed_file>::iterator I = Modules.begin(),
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000379 E = Modules.end(); I != E; ++I) {
Nick Lewycky1fcd0f12011-07-26 08:40:36 +0000380 if (I->syms.empty())
381 continue;
Rafael Espindola00121822010-06-03 14:45:44 +0000382 (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
383 for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
384 if (I->syms[i].resolution == LDPR_PREVAILING_DEF) {
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000385 lto_codegen_add_must_preserve_symbol(code_gen, I->syms[i].name);
Nick Lewycky338d07e2009-02-22 22:15:44 +0000386
Rafael Espindola00121822010-06-03 14:45:44 +0000387 if (options::generate_api_file)
388 api_file << I->syms[i].name << "\n";
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000389 }
390 }
Rafael Espindola77b6d012010-06-14 21:20:52 +0000391 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000392
Rafael Espindola77b6d012010-06-14 21:20:52 +0000393 if (options::generate_api_file)
394 api_file.close();
Nick Lewycky338d07e2009-02-22 22:15:44 +0000395
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000396 lto_codegen_set_pic_model(code_gen, output_type);
397 lto_codegen_set_debug_model(code_gen, LTO_DEBUG_MODEL_DWARF);
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000398 if (!options::mcpu.empty())
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000399 lto_codegen_set_cpu(code_gen, options::mcpu.c_str());
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000400
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000401 // Pass through extra options to the code generator.
402 if (!options::extra.empty()) {
403 for (std::vector<std::string>::iterator it = options::extra.begin();
404 it != options::extra.end(); ++it) {
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000405 lto_codegen_debug_options(code_gen, (*it).c_str());
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000406 }
407 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000408
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000409 if (options::generate_bc_file != options::BC_NO) {
410 std::string path;
411 if (options::generate_bc_file == options::BC_ONLY)
412 path = output_name;
413 else if (!options::bc_path.empty())
414 path = options::bc_path;
415 else
416 path = output_name + ".bc";
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000417 bool err = lto_codegen_write_merged_modules(code_gen, path.c_str());
Rafael Espindolaba3398b2010-05-13 13:39:31 +0000418 if (err)
419 (*message)(LDPL_FATAL, "Failed to write the output file.");
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000420 if (options::generate_bc_file == options::BC_ONLY)
421 exit(0);
Rafael Espindolaba3398b2010-05-13 13:39:31 +0000422 }
Rafael Espindola6b6ed5d2011-02-16 11:19:44 +0000423 const char *objPath;
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000424 if (lto_codegen_compile_to_file(code_gen, &objPath)) {
425 (*message)(LDPL_ERROR, "Could not produce a combined object file\n");
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000426 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000427
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000428 lto_codegen_dispose(code_gen);
Rafael Espindola9ef90d52011-02-20 18:28:29 +0000429 for (std::list<claimed_file>::iterator I = Modules.begin(),
430 E = Modules.end(); I != E; ++I) {
431 for (unsigned i = 0; i != I->syms.size(); ++i) {
432 ld_plugin_symbol &sym = I->syms[i];
433 free(sym.name);
434 }
435 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000436
Rafael Espindola6b6ed5d2011-02-16 11:19:44 +0000437 if ((*add_input_file)(objPath) != LDPS_OK) {
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000438 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
Rafael Espindola6b6ed5d2011-02-16 11:19:44 +0000439 (*message)(LDPL_ERROR, "File left behind in: %s", objPath);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000440 return LDPS_ERR;
441 }
442
Rafael Espindolaef498152010-06-23 20:20:59 +0000443 if (!options::extra_library_path.empty() &&
444 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) {
445 (*message)(LDPL_ERROR, "Unable to set the extra library path.");
446 return LDPS_ERR;
447 }
448
Rafael Espindola6b6ed5d2011-02-16 11:19:44 +0000449 if (options::obj_path.empty())
450 Cleanup.push_back(sys::Path(objPath));
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000451
452 return LDPS_OK;
453}
454
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000455static ld_plugin_status cleanup_hook(void) {
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000456 std::string ErrMsg;
457
458 for (int i = 0, e = Cleanup.size(); i != e; ++i)
459 if (Cleanup[i].eraseFromDisk(false, &ErrMsg))
460 (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),
461 ErrMsg.c_str());
462
463 return LDPS_OK;
464}