blob: b48617f1096b71c3bdf5706aff83037ac977be44 [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 "llvm-c/lto.h"
Ivan Krasin5021af52011-09-12 21:47:50 +000017#include "llvm/ADT/OwningPtr.h"
Chandler Carruth07baed52014-01-13 08:04:33 +000018#include "llvm/ADT/StringSet.h"
Michael J. Spencerab425d82010-11-29 18:47:54 +000019#include "llvm/Support/Errno.h"
Rafael Espindola55ab87f2013-06-17 18:38:18 +000020#include "llvm/Support/FileSystem.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000021#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencerab425d82010-11-29 18:47:54 +000022#include "llvm/Support/Path.h"
23#include "llvm/Support/Program.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000024#include "llvm/Support/ToolOutputFile.h"
25#include "llvm/Support/system_error.h"
Torok Edwina488ee0e2009-02-04 21:00:02 +000026#include <cerrno>
Nick Lewyckyfb643e42009-02-03 07:13:24 +000027#include <cstdlib>
28#include <cstring>
Nick Lewycky338d07e2009-02-22 22:15:44 +000029#include <fstream>
Nick Lewyckyfb643e42009-02-03 07:13:24 +000030#include <list>
Chandler Carruth07baed52014-01-13 08:04:33 +000031#include <plugin-api.h>
Nick Lewyckyfb643e42009-02-03 07:13:24 +000032#include <vector>
33
Michael J. Spencer06f52232011-01-20 05:43:16 +000034// Support Windows/MinGW crazyness.
35#ifdef _WIN32
36# include <io.h>
37# define lseek _lseek
38# define read _read
39#endif
40
Nick Lewyckyfb643e42009-02-03 07:13:24 +000041using namespace llvm;
42
43namespace {
44 ld_plugin_status discard_message(int level, const char *format, ...) {
45 // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
46 // callback in the transfer vector. This should never be called.
47 abort();
48 }
49
50 ld_plugin_add_symbols add_symbols = NULL;
51 ld_plugin_get_symbols get_symbols = NULL;
52 ld_plugin_add_input_file add_input_file = NULL;
Rafael Espindola82976402010-06-18 19:18:58 +000053 ld_plugin_add_input_library add_input_library = NULL;
Rafael Espindolaef498152010-06-23 20:20:59 +000054 ld_plugin_set_extra_library_path set_extra_library_path = NULL;
Rafael Espindolaece7c9c2011-04-07 21:11:00 +000055 ld_plugin_get_view get_view = NULL;
Nick Lewyckyfb643e42009-02-03 07:13:24 +000056 ld_plugin_message message = discard_message;
57
58 int api_version = 0;
59 int gold_version = 0;
60
61 struct claimed_file {
Nick Lewyckyfb643e42009-02-03 07:13:24 +000062 void *handle;
63 std::vector<ld_plugin_symbol> syms;
64 };
65
66 lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
Rafael Espindola8fb957e2010-06-03 21:11:20 +000067 std::string output_name = "";
Nick Lewyckyfb643e42009-02-03 07:13:24 +000068 std::list<claimed_file> Modules;
Rafael Espindola55ab87f2013-06-17 18:38:18 +000069 std::vector<std::string> Cleanup;
Rafael Espindola9ef90d52011-02-20 18:28:29 +000070 lto_code_gen_t code_gen = NULL;
Rafael Espindola282a4702013-10-31 20:51:58 +000071 StringSet<> CannotBeHidden;
Nick Lewyckyfb643e42009-02-03 07:13:24 +000072}
73
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000074namespace options {
Rafael Espindola8fb957e2010-06-03 21:11:20 +000075 enum generate_bc { BC_NO, BC_ALSO, BC_ONLY };
Dan Gohmanebb4ae02010-04-16 00:42:57 +000076 static bool generate_api_file = false;
Rafael Espindola8fb957e2010-06-03 21:11:20 +000077 static generate_bc generate_bc_file = BC_NO;
Rafael Espindolaba3398b2010-05-13 13:39:31 +000078 static std::string bc_path;
Shuxin Yang1826ae22013-08-12 21:07:31 +000079 static std::string obj_path;
Rafael Espindolaef498152010-06-23 20:20:59 +000080 static std::string extra_library_path;
Rafael Espindola4ef89f52010-08-09 21:09:46 +000081 static std::string triple;
Rafael Espindolaccab1dd2010-08-11 00:15:13 +000082 static std::string mcpu;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000083 // Additional options to pass into the code generator.
Nick Lewycky0ac5e222010-06-03 17:10:17 +000084 // Note: This array will contain all plugin options which are not claimed
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000085 // as plugin exclusive to pass to the code generator.
Nick Lewycky0ac5e222010-06-03 17:10:17 +000086 // For example, "generate-api-file" and "as"options are for the plugin
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000087 // use only and will not be passed.
Dan Gohmanebb4ae02010-04-16 00:42:57 +000088 static std::vector<std::string> extra;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000089
Rafael Espindolac4dca3a2010-06-07 16:45:22 +000090 static void process_plugin_option(const char* opt_)
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000091 {
Rafael Espindolac4dca3a2010-06-07 16:45:22 +000092 if (opt_ == NULL)
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000093 return;
Rafael Espindolac4dca3a2010-06-07 16:45:22 +000094 llvm::StringRef opt = opt_;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000095
Rafael Espindolac4dca3a2010-06-07 16:45:22 +000096 if (opt == "generate-api-file") {
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000097 generate_api_file = true;
Rafael Espindolaccab1dd2010-08-11 00:15:13 +000098 } else if (opt.startswith("mcpu=")) {
99 mcpu = opt.substr(strlen("mcpu="));
Rafael Espindolaef498152010-06-23 20:20:59 +0000100 } else if (opt.startswith("extra-library-path=")) {
101 extra_library_path = opt.substr(strlen("extra_library_path="));
Rafael Espindola148c3282010-08-10 16:32:15 +0000102 } else if (opt.startswith("mtriple=")) {
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000103 triple = opt.substr(strlen("mtriple="));
Shuxin Yang1826ae22013-08-12 21:07:31 +0000104 } else if (opt.startswith("obj-path=")) {
105 obj_path = opt.substr(strlen("obj-path="));
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000106 } else if (opt == "emit-llvm") {
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000107 generate_bc_file = BC_ONLY;
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000108 } else if (opt == "also-emit-llvm") {
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000109 generate_bc_file = BC_ALSO;
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000110 } else if (opt.startswith("also-emit-llvm=")) {
111 llvm::StringRef path = opt.substr(strlen("also-emit-llvm="));
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000112 generate_bc_file = BC_ALSO;
Nick Lewyckyca4180c2010-06-03 17:13:23 +0000113 if (!bc_path.empty()) {
Rafael Espindolaba3398b2010-05-13 13:39:31 +0000114 (*message)(LDPL_WARNING, "Path to the output IL file specified twice. "
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000115 "Discarding %s", opt_);
Rafael Espindolaba3398b2010-05-13 13:39:31 +0000116 } else {
117 bc_path = path;
118 }
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000119 } else {
120 // Save this option to pass to the code generator.
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000121 extra.push_back(opt);
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000122 }
123 }
124}
125
Dan Gohmanebb4ae02010-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 Lewyckyfb643e42009-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 Lewyckyfb643e42009-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 Espindola8fb957e2010-06-03 21:11:20 +0000149 case LDPT_OUTPUT_NAME:
150 output_name = tv->tv_u.tv_string;
151 break;
Nick Lewyckyfb643e42009-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
Rafael Espindola76e376d2014-02-10 20:38:38 +0000156 case LDPO_PIE: // position independent executable
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000157 output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
158 break;
159 case LDPO_EXEC: // .exe
160 output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
161 break;
162 default:
163 (*message)(LDPL_ERROR, "Unknown output file type %d",
164 tv->tv_u.tv_val);
165 return LDPS_ERR;
166 }
167 // TODO: add an option to disable PIC.
168 //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
169 break;
170 case LDPT_OPTION:
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000171 options::process_plugin_option(tv->tv_u.tv_string);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000172 break;
173 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
174 ld_plugin_register_claim_file callback;
175 callback = tv->tv_u.tv_register_claim_file;
176
177 if ((*callback)(claim_file_hook) != LDPS_OK)
178 return LDPS_ERR;
179
180 registeredClaimFile = true;
181 } break;
182 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
183 ld_plugin_register_all_symbols_read callback;
184 callback = tv->tv_u.tv_register_all_symbols_read;
185
186 if ((*callback)(all_symbols_read_hook) != LDPS_OK)
187 return LDPS_ERR;
Rafael Espindola9ef90d52011-02-20 18:28:29 +0000188
189 code_gen = lto_codegen_create();
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000190 } break;
191 case LDPT_REGISTER_CLEANUP_HOOK: {
192 ld_plugin_register_cleanup callback;
193 callback = tv->tv_u.tv_register_cleanup;
194
195 if ((*callback)(cleanup_hook) != LDPS_OK)
196 return LDPS_ERR;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000197 } break;
198 case LDPT_ADD_SYMBOLS:
199 add_symbols = tv->tv_u.tv_add_symbols;
200 break;
Rafael Espindolacda29112013-10-03 18:29:09 +0000201 case LDPT_GET_SYMBOLS_V2:
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000202 get_symbols = tv->tv_u.tv_get_symbols;
203 break;
204 case LDPT_ADD_INPUT_FILE:
205 add_input_file = tv->tv_u.tv_add_input_file;
206 break;
Rafael Espindola82976402010-06-18 19:18:58 +0000207 case LDPT_ADD_INPUT_LIBRARY:
208 add_input_library = tv->tv_u.tv_add_input_file;
209 break;
Rafael Espindolaef498152010-06-23 20:20:59 +0000210 case LDPT_SET_EXTRA_LIBRARY_PATH:
211 set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
212 break;
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000213 case LDPT_GET_VIEW:
214 get_view = tv->tv_u.tv_get_view;
215 break;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000216 case LDPT_MESSAGE:
217 message = tv->tv_u.tv_message;
218 break;
219 default:
220 break;
221 }
222 }
223
Rafael Espindolae08484d2009-02-18 08:30:15 +0000224 if (!registeredClaimFile) {
Rafael Espindola6add6182009-02-18 17:49:06 +0000225 (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
226 return LDPS_ERR;
227 }
Rafael Espindolae08484d2009-02-18 08:30:15 +0000228 if (!add_symbols) {
Rafael Espindola6add6182009-02-18 17:49:06 +0000229 (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
230 return LDPS_ERR;
231 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000232
233 return LDPS_OK;
234}
235
236/// claim_file_hook - called by gold to see whether this file is one that
237/// our plugin can handle. We'll try to open it and register all the symbols
238/// with add_symbol if possible.
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000239static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
240 int *claimed) {
Rafael Espindola56e41f72011-02-08 22:40:47 +0000241 lto_module_t M;
Ivan Krasin5021af52011-09-12 21:47:50 +0000242 const void *view;
243 OwningPtr<MemoryBuffer> buffer;
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000244 if (get_view) {
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000245 if (get_view(file->handle, &view) != LDPS_OK) {
246 (*message)(LDPL_ERROR, "Failed to get a view of %s", file->name);
247 return LDPS_ERR;
248 }
Ivan Krasin5021af52011-09-12 21:47:50 +0000249 } else {
Ivan Krasin639222d2011-09-15 23:13:00 +0000250 int64_t offset = 0;
Nick Lewycky8691c472009-02-05 04:14:23 +0000251 // Gold has found what might be IR part-way inside of a file, such as
252 // an .a archive.
Ivan Krasin5021af52011-09-12 21:47:50 +0000253 if (file->offset) {
254 offset = file->offset;
255 }
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +0000256 if (error_code ec = MemoryBuffer::getOpenFileSlice(
257 file->fd, file->name, buffer, file->filesize, offset)) {
Ivan Krasin5021af52011-09-12 21:47:50 +0000258 (*message)(LDPL_ERROR, ec.message().c_str());
259 return LDPS_ERR;
260 }
261 view = buffer->getBufferStart();
Rafael Espindola56e41f72011-02-08 22:40:47 +0000262 }
Ivan Krasin5021af52011-09-12 21:47:50 +0000263
264 if (!lto_module_is_object_file_in_memory(view, file->filesize))
265 return LDPS_OK;
266
267 M = lto_module_create_from_memory(view, file->filesize);
Ivan Krasind5f2d8c2011-09-09 00:14:04 +0000268 if (!M) {
269 if (const char* msg = lto_get_error_message()) {
Ivan Krasin5021af52011-09-12 21:47:50 +0000270 (*message)(LDPL_ERROR,
271 "LLVM gold plugin has failed to create LTO module: %s",
272 msg);
Ivan Krasind5f2d8c2011-09-09 00:14:04 +0000273 return LDPS_ERR;
274 }
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000275 return LDPS_OK;
Ivan Krasind5f2d8c2011-09-09 00:14:04 +0000276 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000277
278 *claimed = 1;
279 Modules.resize(Modules.size() + 1);
280 claimed_file &cf = Modules.back();
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000281
282 if (!options::triple.empty())
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000283 lto_module_set_target_triple(M, options::triple.c_str());
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000284
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000285 cf.handle = file->handle;
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000286 unsigned sym_count = lto_module_get_num_symbols(M);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000287 cf.syms.reserve(sym_count);
288
289 for (unsigned i = 0; i != sym_count; ++i) {
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000290 lto_symbol_attributes attrs = lto_module_get_symbol_attribute(M, i);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000291 if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
292 continue;
293
294 cf.syms.push_back(ld_plugin_symbol());
295 ld_plugin_symbol &sym = cf.syms.back();
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000296 sym.name = const_cast<char *>(lto_module_get_symbol_name(M, i));
Rafael Espindola9ef90d52011-02-20 18:28:29 +0000297 sym.name = strdup(sym.name);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000298 sym.version = NULL;
299
300 int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
Rafael Espindola282a4702013-10-31 20:51:58 +0000301 bool CanBeHidden = scope == LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
302 if (!CanBeHidden)
303 CannotBeHidden.insert(sym.name);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000304 switch (scope) {
305 case LTO_SYMBOL_SCOPE_HIDDEN:
306 sym.visibility = LDPV_HIDDEN;
307 break;
308 case LTO_SYMBOL_SCOPE_PROTECTED:
309 sym.visibility = LDPV_PROTECTED;
310 break;
311 case 0: // extern
312 case LTO_SYMBOL_SCOPE_DEFAULT:
Rafael Espindola282a4702013-10-31 20:51:58 +0000313 case LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN:
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000314 sym.visibility = LDPV_DEFAULT;
315 break;
316 default:
317 (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
318 return LDPS_ERR;
319 }
320
321 int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
Rafael Espindola70d80152011-02-14 22:23:49 +0000322 sym.comdat_key = NULL;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000323 switch (definition) {
324 case LTO_SYMBOL_DEFINITION_REGULAR:
325 sym.def = LDPK_DEF;
326 break;
327 case LTO_SYMBOL_DEFINITION_UNDEFINED:
328 sym.def = LDPK_UNDEF;
329 break;
330 case LTO_SYMBOL_DEFINITION_TENTATIVE:
331 sym.def = LDPK_COMMON;
332 break;
333 case LTO_SYMBOL_DEFINITION_WEAK:
Rafael Espindola70d80152011-02-14 22:23:49 +0000334 sym.comdat_key = sym.name;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000335 sym.def = LDPK_WEAKDEF;
336 break;
Rafael Espindola56548522009-04-24 16:55:21 +0000337 case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
338 sym.def = LDPK_WEAKUNDEF;
339 break;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000340 default:
341 (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition);
342 return LDPS_ERR;
343 }
344
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000345 sym.size = 0;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000346
347 sym.resolution = LDPR_UNKNOWN;
348 }
349
350 cf.syms.reserve(cf.syms.size());
351
352 if (!cf.syms.empty()) {
353 if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
354 (*message)(LDPL_ERROR, "Unable to add symbols!");
355 return LDPS_ERR;
356 }
357 }
358
Rafael Espindolad578b692013-10-18 19:32:06 +0000359 if (code_gen) {
360 if (lto_codegen_add_module(code_gen, M)) {
361 (*message)(LDPL_ERROR, "Error linking module: %s",
362 lto_get_error_message());
363 return LDPS_ERR;
364 }
365 }
Rafael Espindola9ef90d52011-02-20 18:28:29 +0000366
367 lto_module_dispose(M);
368
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000369 return LDPS_OK;
370}
371
Rafael Espindola282a4702013-10-31 20:51:58 +0000372static bool mustPreserve(const claimed_file &F, int i) {
373 if (F.syms[i].resolution == LDPR_PREVAILING_DEF)
374 return true;
375 if (F.syms[i].resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
376 return CannotBeHidden.count(F.syms[i].name);
377 return false;
378}
379
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000380/// all_symbols_read_hook - gold informs us that all symbols have been read.
381/// At this point, we use get_symbols to see if any of our definitions have
382/// been overridden by a native object file. Then, perform optimization and
383/// codegen.
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000384static ld_plugin_status all_symbols_read_hook(void) {
Nick Lewycky338d07e2009-02-22 22:15:44 +0000385 std::ofstream api_file;
Rafael Espindola9ef90d52011-02-20 18:28:29 +0000386 assert(code_gen);
387
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000388 if (options::generate_api_file) {
Nick Lewycky338d07e2009-02-22 22:15:44 +0000389 api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
390 if (!api_file.is_open()) {
391 (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing.");
392 abort();
393 }
394 }
395
Rafael Espindola00121822010-06-03 14:45:44 +0000396 for (std::list<claimed_file>::iterator I = Modules.begin(),
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000397 E = Modules.end(); I != E; ++I) {
Nick Lewycky1fcd0f12011-07-26 08:40:36 +0000398 if (I->syms.empty())
399 continue;
Rafael Espindola00121822010-06-03 14:45:44 +0000400 (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
401 for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
Rafael Espindola282a4702013-10-31 20:51:58 +0000402 if (mustPreserve(*I, i)) {
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000403 lto_codegen_add_must_preserve_symbol(code_gen, I->syms[i].name);
Nick Lewycky338d07e2009-02-22 22:15:44 +0000404
Rafael Espindola00121822010-06-03 14:45:44 +0000405 if (options::generate_api_file)
406 api_file << I->syms[i].name << "\n";
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000407 }
408 }
Rafael Espindola77b6d012010-06-14 21:20:52 +0000409 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000410
Rafael Espindola77b6d012010-06-14 21:20:52 +0000411 if (options::generate_api_file)
412 api_file.close();
Nick Lewycky338d07e2009-02-22 22:15:44 +0000413
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000414 lto_codegen_set_pic_model(code_gen, output_type);
415 lto_codegen_set_debug_model(code_gen, LTO_DEBUG_MODEL_DWARF);
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000416 if (!options::mcpu.empty())
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000417 lto_codegen_set_cpu(code_gen, options::mcpu.c_str());
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000418
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000419 // Pass through extra options to the code generator.
420 if (!options::extra.empty()) {
421 for (std::vector<std::string>::iterator it = options::extra.begin();
422 it != options::extra.end(); ++it) {
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000423 lto_codegen_debug_options(code_gen, (*it).c_str());
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000424 }
425 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000426
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000427 if (options::generate_bc_file != options::BC_NO) {
428 std::string path;
429 if (options::generate_bc_file == options::BC_ONLY)
430 path = output_name;
431 else if (!options::bc_path.empty())
432 path = options::bc_path;
433 else
434 path = output_name + ".bc";
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000435 bool err = lto_codegen_write_merged_modules(code_gen, path.c_str());
Rafael Espindolaba3398b2010-05-13 13:39:31 +0000436 if (err)
437 (*message)(LDPL_FATAL, "Failed to write the output file.");
Rafael Espindolafb7a0b82013-10-03 00:07:30 +0000438 if (options::generate_bc_file == options::BC_ONLY) {
439 lto_codegen_dispose(code_gen);
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000440 exit(0);
Rafael Espindolafb7a0b82013-10-03 00:07:30 +0000441 }
Rafael Espindolaba3398b2010-05-13 13:39:31 +0000442 }
Rafael Espindola143fc3b2013-10-16 12:47:04 +0000443
444 std::string ObjPath;
445 {
446 const char *Temp;
447 if (lto_codegen_compile_to_file(code_gen, &Temp)) {
448 (*message)(LDPL_ERROR, "Could not produce a combined object file\n");
449 }
450 ObjPath = Temp;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000451 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000452
Rafael Espindolad4f34d82011-02-19 21:49:57 +0000453 lto_codegen_dispose(code_gen);
Rafael Espindola9ef90d52011-02-20 18:28:29 +0000454 for (std::list<claimed_file>::iterator I = Modules.begin(),
455 E = Modules.end(); I != E; ++I) {
456 for (unsigned i = 0; i != I->syms.size(); ++i) {
457 ld_plugin_symbol &sym = I->syms[i];
458 free(sym.name);
459 }
460 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000461
Rafael Espindola143fc3b2013-10-16 12:47:04 +0000462 if ((*add_input_file)(ObjPath.c_str()) != LDPS_OK) {
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000463 (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
Rafael Espindola143fc3b2013-10-16 12:47:04 +0000464 (*message)(LDPL_ERROR, "File left behind in: %s", ObjPath.c_str());
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000465 return LDPS_ERR;
466 }
467
Rafael Espindolaef498152010-06-23 20:20:59 +0000468 if (!options::extra_library_path.empty() &&
469 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) {
470 (*message)(LDPL_ERROR, "Unable to set the extra library path.");
471 return LDPS_ERR;
472 }
473
Shuxin Yang1826ae22013-08-12 21:07:31 +0000474 if (options::obj_path.empty())
Rafael Espindola143fc3b2013-10-16 12:47:04 +0000475 Cleanup.push_back(ObjPath);
Shuxin Yang1826ae22013-08-12 21:07:31 +0000476
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000477 return LDPS_OK;
478}
479
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000480static ld_plugin_status cleanup_hook(void) {
Rafael Espindola55ab87f2013-06-17 18:38:18 +0000481 for (int i = 0, e = Cleanup.size(); i != e; ++i) {
Shuxin Yang1826ae22013-08-12 21:07:31 +0000482 error_code EC = sys::fs::remove(Cleanup[i]);
Rafael Espindola55ab87f2013-06-17 18:38:18 +0000483 if (EC)
Shuxin Yang1826ae22013-08-12 21:07:31 +0000484 (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),
Rafael Espindolad9a56082013-06-17 22:24:06 +0000485 EC.message().c_str());
Rafael Espindola55ab87f2013-06-17 18:38:18 +0000486 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000487
488 return LDPS_OK;
489}