blob: 678e6e6707da62b35e0bdfc4dda1216b02c3b5e3 [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
Rafael Espindolaf7ecb112014-08-22 23:26:10 +000016#include "llvm/ADT/DenseSet.h"
Chandler Carruth07baed52014-01-13 08:04:33 +000017#include "llvm/ADT/StringSet.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000018#include "llvm/Analysis/TargetLibraryInfo.h"
NAKAMURA Takumib0a52832015-02-02 05:47:30 +000019#include "llvm/Analysis/TargetTransformInfo.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000020#include "llvm/Bitcode/ReaderWriter.h"
21#include "llvm/CodeGen/Analysis.h"
Rafael Espindola6b244b12014-06-19 21:14:13 +000022#include "llvm/CodeGen/CommandFlags.h"
Peter Collingbourne87202a42015-09-01 20:40:22 +000023#include "llvm/CodeGen/ParallelCG.h"
Rafael Espindola0d68b4c2015-03-30 21:36:43 +000024#include "llvm/IR/AutoUpgrade.h"
Rafael Espindola890db272014-09-09 20:08:22 +000025#include "llvm/IR/Constants.h"
Rafael Espindolad0b23be2015-01-10 00:07:30 +000026#include "llvm/IR/DiagnosticInfo.h"
27#include "llvm/IR/DiagnosticPrinter.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000028#include "llvm/IR/LLVMContext.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000029#include "llvm/IR/LegacyPassManager.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000030#include "llvm/IR/Module.h"
31#include "llvm/IR/Verifier.h"
Rafael Espindolacaabe222015-12-10 14:19:35 +000032#include "llvm/Linker/IRMover.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000033#include "llvm/MC/SubtargetFeature.h"
Teresa Johnson403a7872015-10-04 14:33:43 +000034#include "llvm/Object/FunctionIndexObjectFile.h"
Teresa Johnsonb13dbd62015-12-09 19:45:55 +000035#include "llvm/Object/IRObjectFile.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000036#include "llvm/Support/Host.h"
Rafael Espindola947bdb62014-11-25 20:52:49 +000037#include "llvm/Support/ManagedStatic.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000038#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000039#include "llvm/Support/TargetRegistry.h"
Rafael Espindola6b244b12014-06-19 21:14:13 +000040#include "llvm/Support/TargetSelect.h"
Teresa Johnson7cffaf32016-03-04 17:06:02 +000041#include "llvm/Support/ThreadPool.h"
Teresa Johnsonb13dbd62015-12-09 19:45:55 +000042#include "llvm/Support/raw_ostream.h"
Teresa Johnson7cffaf32016-03-04 17:06:02 +000043#include "llvm/Support/thread.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000044#include "llvm/Transforms/IPO.h"
45#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Teresa Johnson7cffaf32016-03-04 17:06:02 +000046#include "llvm/Transforms/Utils/FunctionImportUtils.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000047#include "llvm/Transforms/Utils/GlobalStatus.h"
48#include "llvm/Transforms/Utils/ModuleUtils.h"
Rafael Espindolaf7ecb112014-08-22 23:26:10 +000049#include "llvm/Transforms/Utils/ValueMapper.h"
Nick Lewyckyfb643e42009-02-03 07:13:24 +000050#include <list>
Chandler Carruth07baed52014-01-13 08:04:33 +000051#include <plugin-api.h>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000052#include <system_error>
Nick Lewyckyfb643e42009-02-03 07:13:24 +000053#include <vector>
54
Sylvestre Ledru53999792014-02-11 17:30:18 +000055// FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and
56// Precise and Debian Wheezy (binutils 2.23 is required)
Evgeniy Stepanov330c5a62016-03-04 00:23:29 +000057#define LDPO_PIE 3
58
59#define LDPT_GET_SYMBOLS_V3 28
Sylvestre Ledru53999792014-02-11 17:30:18 +000060
Nick Lewyckyfb643e42009-02-03 07:13:24 +000061using namespace llvm;
62
Teresa Johnsoncb15b732015-12-16 16:34:06 +000063static ld_plugin_status discard_message(int level, const char *format, ...) {
64 // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
65 // callback in the transfer vector. This should never be called.
66 abort();
67}
68
69static ld_plugin_release_input_file release_input_file = nullptr;
70static ld_plugin_get_input_file get_input_file = nullptr;
71static ld_plugin_message message = discard_message;
72
Nick Lewyckyfb643e42009-02-03 07:13:24 +000073namespace {
Rafael Espindolabfb8b912014-06-20 01:37:35 +000074struct claimed_file {
75 void *handle;
76 std::vector<ld_plugin_symbol> syms;
77};
Rafael Espindolacaabe222015-12-10 14:19:35 +000078
Teresa Johnsoncb15b732015-12-16 16:34:06 +000079/// RAII wrapper to manage opening and releasing of a ld_plugin_input_file.
80struct PluginInputFile {
Teresa Johnson031bed22015-12-16 21:37:48 +000081 void *Handle;
Teresa Johnson7cffaf32016-03-04 17:06:02 +000082 std::unique_ptr<ld_plugin_input_file> File;
Teresa Johnsoncb15b732015-12-16 16:34:06 +000083
Teresa Johnson031bed22015-12-16 21:37:48 +000084 PluginInputFile(void *Handle) : Handle(Handle) {
Teresa Johnson7cffaf32016-03-04 17:06:02 +000085 File = llvm::make_unique<ld_plugin_input_file>();
86 if (get_input_file(Handle, File.get()) != LDPS_OK)
Teresa Johnsoncb15b732015-12-16 16:34:06 +000087 message(LDPL_FATAL, "Failed to get file information");
88 }
89 ~PluginInputFile() {
Teresa Johnson7cffaf32016-03-04 17:06:02 +000090 // File would have been reset to nullptr if we moved this object
91 // to a new owner.
92 if (File)
93 if (release_input_file(Handle) != LDPS_OK)
94 message(LDPL_FATAL, "Failed to release file information");
Teresa Johnsoncb15b732015-12-16 16:34:06 +000095 }
Teresa Johnson7cffaf32016-03-04 17:06:02 +000096
97 ld_plugin_input_file &file() { return *File; }
98
99 PluginInputFile(PluginInputFile &&RHS) = default;
100 PluginInputFile &operator=(PluginInputFile &&RHS) = default;
Teresa Johnsoncb15b732015-12-16 16:34:06 +0000101};
102
Rafael Espindolacaabe222015-12-10 14:19:35 +0000103struct ResolutionInfo {
104 bool IsLinkonceOdr = true;
105 bool UnnamedAddr = true;
106 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
107 bool CommonInternal = false;
108 bool UseCommon = false;
109 unsigned CommonSize = 0;
110 unsigned CommonAlign = 0;
111 claimed_file *CommonFile = nullptr;
112};
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000113
114/// Class to own information used by a task or during its cleanup for a
115/// ThinLTO backend instantiation.
116class ThinLTOTaskInfo {
117 /// The input file holding the module bitcode read by the ThinLTO task.
118 PluginInputFile InputFile;
119
120 /// The output stream the task will codegen into.
121 std::unique_ptr<raw_fd_ostream> OS;
122
123 /// The file name corresponding to the output stream, used during cleanup.
124 std::string Filename;
125
126 /// Flag indicating whether the output file is a temp file that must be
127 /// added to the cleanup list during cleanup.
128 bool TempOutFile;
129
130public:
131 ThinLTOTaskInfo(PluginInputFile InputFile, std::unique_ptr<raw_fd_ostream> OS,
132 std::string Filename, bool TempOutFile)
133 : InputFile(std::move(InputFile)), OS(std::move(OS)), Filename(Filename),
134 TempOutFile(TempOutFile) {}
135
136 /// Performs task related cleanup activities that must be done
137 /// single-threaded (i.e. call backs to gold).
138 void cleanup();
139};
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000140}
Rafael Espindolabfb8b912014-06-20 01:37:35 +0000141
Rafael Espindola176e6642014-07-29 21:46:05 +0000142static ld_plugin_add_symbols add_symbols = nullptr;
143static ld_plugin_get_symbols get_symbols = nullptr;
144static ld_plugin_add_input_file add_input_file = nullptr;
145static ld_plugin_set_extra_library_path set_extra_library_path = nullptr;
146static ld_plugin_get_view get_view = nullptr;
Rafael Espindola33466a72014-08-21 20:28:55 +0000147static Reloc::Model RelocationModel = Reloc::Default;
Rafael Espindolabfb8b912014-06-20 01:37:35 +0000148static std::string output_name = "";
149static std::list<claimed_file> Modules;
Rafael Espindolacaabe222015-12-10 14:19:35 +0000150static StringMap<ResolutionInfo> ResInfo;
Rafael Espindolabfb8b912014-06-20 01:37:35 +0000151static std::vector<std::string> Cleanup;
Rafael Espindola6b244b12014-06-19 21:14:13 +0000152static llvm::TargetOptions TargetOpts;
Teresa Johnsona9f65552016-03-04 16:36:06 +0000153static std::string DefaultTriple = sys::getDefaultTargetTriple();
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000154
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000155namespace options {
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000156 enum OutputType {
157 OT_NORMAL,
158 OT_DISABLE,
159 OT_BC_ONLY,
160 OT_SAVE_TEMPS
161 };
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000162 static bool generate_api_file = false;
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000163 static OutputType TheOutputType = OT_NORMAL;
Peter Collingbourne070843d2015-03-19 22:01:00 +0000164 static unsigned OptLevel = 2;
Teresa Johnsona9f65552016-03-04 16:36:06 +0000165 // Default parallelism of 0 used to indicate that user did not specify.
166 // Actual parallelism default value depends on implementation.
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000167 // Currently, code generation defaults to no parallelism, whereas
168 // ThinLTO uses the hardware_concurrency as the default.
Teresa Johnsona9f65552016-03-04 16:36:06 +0000169 static unsigned Parallelism = 0;
Teresa Johnson8c8fe5a2015-09-16 18:06:45 +0000170#ifdef NDEBUG
171 static bool DisableVerify = true;
172#else
173 static bool DisableVerify = false;
174#endif
Shuxin Yang1826ae22013-08-12 21:07:31 +0000175 static std::string obj_path;
Rafael Espindolaef498152010-06-23 20:20:59 +0000176 static std::string extra_library_path;
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000177 static std::string triple;
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000178 static std::string mcpu;
Teresa Johnson403a7872015-10-04 14:33:43 +0000179 // When the thinlto plugin option is specified, only read the function
180 // the information from intermediate files and write a combined
181 // global index for the ThinLTO backends.
182 static bool thinlto = false;
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000183 // If false, all ThinLTO backend compilations through code gen are performed
184 // using multiple threads in the gold-plugin, before handing control back to
185 // gold. If true, exit after creating the combined index, the assuming is
186 // that the build system will launch the backend processes.
187 static bool thinlto_index_only = false;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000188 // Additional options to pass into the code generator.
Nick Lewycky0ac5e222010-06-03 17:10:17 +0000189 // Note: This array will contain all plugin options which are not claimed
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000190 // as plugin exclusive to pass to the code generator.
Nick Lewycky0ac5e222010-06-03 17:10:17 +0000191 // For example, "generate-api-file" and "as"options are for the plugin
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000192 // use only and will not be passed.
Rafael Espindola125b9242014-07-29 19:17:44 +0000193 static std::vector<const char *> extra;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000194
Nick Lewycky7282dd72015-08-05 21:16:02 +0000195 static void process_plugin_option(const char *opt_)
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000196 {
Rafael Espindola176e6642014-07-29 21:46:05 +0000197 if (opt_ == nullptr)
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000198 return;
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000199 llvm::StringRef opt = opt_;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000200
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000201 if (opt == "generate-api-file") {
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000202 generate_api_file = true;
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000203 } else if (opt.startswith("mcpu=")) {
204 mcpu = opt.substr(strlen("mcpu="));
Rafael Espindolaef498152010-06-23 20:20:59 +0000205 } else if (opt.startswith("extra-library-path=")) {
206 extra_library_path = opt.substr(strlen("extra_library_path="));
Rafael Espindola148c3282010-08-10 16:32:15 +0000207 } else if (opt.startswith("mtriple=")) {
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000208 triple = opt.substr(strlen("mtriple="));
Shuxin Yang1826ae22013-08-12 21:07:31 +0000209 } else if (opt.startswith("obj-path=")) {
210 obj_path = opt.substr(strlen("obj-path="));
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000211 } else if (opt == "emit-llvm") {
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000212 TheOutputType = OT_BC_ONLY;
Rafael Espindola4a3b6cf2014-10-29 23:54:45 +0000213 } else if (opt == "save-temps") {
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000214 TheOutputType = OT_SAVE_TEMPS;
215 } else if (opt == "disable-output") {
216 TheOutputType = OT_DISABLE;
Teresa Johnson403a7872015-10-04 14:33:43 +0000217 } else if (opt == "thinlto") {
218 thinlto = true;
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000219 } else if (opt == "thinlto-index-only") {
220 thinlto_index_only = true;
Peter Collingbourne070843d2015-03-19 22:01:00 +0000221 } else if (opt.size() == 2 && opt[0] == 'O') {
222 if (opt[1] < '0' || opt[1] > '3')
Peter Collingbourne87202a42015-09-01 20:40:22 +0000223 message(LDPL_FATAL, "Optimization level must be between 0 and 3");
Peter Collingbourne070843d2015-03-19 22:01:00 +0000224 OptLevel = opt[1] - '0';
Peter Collingbourne87202a42015-09-01 20:40:22 +0000225 } else if (opt.startswith("jobs=")) {
226 if (StringRef(opt_ + 5).getAsInteger(10, Parallelism))
227 message(LDPL_FATAL, "Invalid parallelism level: %s", opt_ + 5);
Teresa Johnson8c8fe5a2015-09-16 18:06:45 +0000228 } else if (opt == "disable-verify") {
229 DisableVerify = true;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000230 } else {
231 // Save this option to pass to the code generator.
Rafael Espindola33466a72014-08-21 20:28:55 +0000232 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily
233 // add that.
234 if (extra.empty())
235 extra.push_back("LLVMgold");
236
Rafael Espindola125b9242014-07-29 19:17:44 +0000237 extra.push_back(opt_);
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000238 }
239 }
240}
241
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000242static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
243 int *claimed);
244static ld_plugin_status all_symbols_read_hook(void);
245static ld_plugin_status cleanup_hook(void);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000246
247extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
248ld_plugin_status onload(ld_plugin_tv *tv) {
Peter Collingbourne1505c0a2014-07-03 23:28:03 +0000249 InitializeAllTargetInfos();
250 InitializeAllTargets();
251 InitializeAllTargetMCs();
252 InitializeAllAsmParsers();
253 InitializeAllAsmPrinters();
254
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000255 // We're given a pointer to the first transfer vector. We read through them
256 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
257 // contain pointers to functions that we need to call to register our own
258 // hooks. The others are addresses of functions we can use to call into gold
259 // for services.
260
261 bool registeredClaimFile = false;
Rafael Espindola6b244b12014-06-19 21:14:13 +0000262 bool RegisteredAllSymbolsRead = false;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000263
264 for (; tv->tv_tag != LDPT_NULL; ++tv) {
Evgeniy Stepanov330c5a62016-03-04 00:23:29 +0000265 // Cast tv_tag to int to allow values not in "enum ld_plugin_tag", like, for
266 // example, LDPT_GET_SYMBOLS_V3 when building against an older plugin-api.h
267 // header.
268 switch (static_cast<int>(tv->tv_tag)) {
269 case LDPT_OUTPUT_NAME:
270 output_name = tv->tv_u.tv_string;
271 break;
272 case LDPT_LINKER_OUTPUT:
273 switch (tv->tv_u.tv_val) {
274 case LDPO_REL: // .o
275 case LDPO_DYN: // .so
276 case LDPO_PIE: // position independent executable
277 RelocationModel = Reloc::PIC_;
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000278 break;
Evgeniy Stepanov330c5a62016-03-04 00:23:29 +0000279 case LDPO_EXEC: // .exe
280 RelocationModel = Reloc::Static;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000281 break;
282 default:
Evgeniy Stepanov330c5a62016-03-04 00:23:29 +0000283 message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val);
284 return LDPS_ERR;
285 }
286 break;
287 case LDPT_OPTION:
288 options::process_plugin_option(tv->tv_u.tv_string);
289 break;
290 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
291 ld_plugin_register_claim_file callback;
292 callback = tv->tv_u.tv_register_claim_file;
293
294 if (callback(claim_file_hook) != LDPS_OK)
295 return LDPS_ERR;
296
297 registeredClaimFile = true;
298 } break;
299 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
300 ld_plugin_register_all_symbols_read callback;
301 callback = tv->tv_u.tv_register_all_symbols_read;
302
303 if (callback(all_symbols_read_hook) != LDPS_OK)
304 return LDPS_ERR;
305
306 RegisteredAllSymbolsRead = true;
307 } break;
308 case LDPT_REGISTER_CLEANUP_HOOK: {
309 ld_plugin_register_cleanup callback;
310 callback = tv->tv_u.tv_register_cleanup;
311
312 if (callback(cleanup_hook) != LDPS_OK)
313 return LDPS_ERR;
314 } break;
315 case LDPT_GET_INPUT_FILE:
316 get_input_file = tv->tv_u.tv_get_input_file;
317 break;
318 case LDPT_RELEASE_INPUT_FILE:
319 release_input_file = tv->tv_u.tv_release_input_file;
320 break;
321 case LDPT_ADD_SYMBOLS:
322 add_symbols = tv->tv_u.tv_add_symbols;
323 break;
324 case LDPT_GET_SYMBOLS_V2:
325 // Do not override get_symbols_v3 with get_symbols_v2.
326 if (!get_symbols)
327 get_symbols = tv->tv_u.tv_get_symbols;
328 break;
329 case LDPT_GET_SYMBOLS_V3:
330 get_symbols = tv->tv_u.tv_get_symbols;
331 break;
332 case LDPT_ADD_INPUT_FILE:
333 add_input_file = tv->tv_u.tv_add_input_file;
334 break;
335 case LDPT_SET_EXTRA_LIBRARY_PATH:
336 set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
337 break;
338 case LDPT_GET_VIEW:
339 get_view = tv->tv_u.tv_get_view;
340 break;
341 case LDPT_MESSAGE:
342 message = tv->tv_u.tv_message;
343 break;
344 default:
345 break;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000346 }
347 }
348
Rafael Espindolae08484d2009-02-18 08:30:15 +0000349 if (!registeredClaimFile) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000350 message(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
Rafael Espindola6add6182009-02-18 17:49:06 +0000351 return LDPS_ERR;
352 }
Rafael Espindolae08484d2009-02-18 08:30:15 +0000353 if (!add_symbols) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000354 message(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
Rafael Espindola6add6182009-02-18 17:49:06 +0000355 return LDPS_ERR;
356 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000357
Rafael Espindolaa0d30a92014-06-19 22:20:07 +0000358 if (!RegisteredAllSymbolsRead)
359 return LDPS_OK;
Rafael Espindola6b244b12014-06-19 21:14:13 +0000360
Rafael Espindola33466a72014-08-21 20:28:55 +0000361 if (!get_input_file) {
362 message(LDPL_ERROR, "get_input_file not passed to LLVMgold.");
363 return LDPS_ERR;
Rafael Espindolac273aac2014-06-19 22:54:47 +0000364 }
Rafael Espindola33466a72014-08-21 20:28:55 +0000365 if (!release_input_file) {
366 message(LDPL_ERROR, "relesase_input_file not passed to LLVMgold.");
367 return LDPS_ERR;
Tom Roederb5081192014-06-26 20:43:27 +0000368 }
369
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000370 return LDPS_OK;
371}
372
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000373static const GlobalObject *getBaseObject(const GlobalValue &GV) {
374 if (auto *GA = dyn_cast<GlobalAlias>(&GV))
375 return GA->getBaseObject();
376 return cast<GlobalObject>(&GV);
377}
378
Rafael Espindola527e8462014-12-09 16:13:59 +0000379static bool shouldSkip(uint32_t Symflags) {
380 if (!(Symflags & object::BasicSymbolRef::SF_Global))
381 return true;
382 if (Symflags & object::BasicSymbolRef::SF_FormatSpecific)
383 return true;
384 return false;
385}
386
NAKAMURA Takumib13e63c2015-11-19 10:43:44 +0000387static void diagnosticHandler(const DiagnosticInfo &DI) {
Rafael Espindola503f8832015-03-02 19:08:03 +0000388 if (const auto *BDI = dyn_cast<BitcodeDiagnosticInfo>(&DI)) {
389 std::error_code EC = BDI->getError();
390 if (EC == BitcodeError::InvalidBitcodeSignature)
391 return;
392 }
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000393
394 std::string ErrStorage;
395 {
396 raw_string_ostream OS(ErrStorage);
397 DiagnosticPrinterRawOStream DP(OS);
398 DI.print(DP);
399 }
Rafael Espindola503f8832015-03-02 19:08:03 +0000400 ld_plugin_level Level;
401 switch (DI.getSeverity()) {
402 case DS_Error:
403 message(LDPL_FATAL, "LLVM gold plugin has failed to create LTO module: %s",
404 ErrStorage.c_str());
Rafael Espindola503f8832015-03-02 19:08:03 +0000405 case DS_Warning:
406 Level = LDPL_WARNING;
407 break;
408 case DS_Note:
Rafael Espindolaf3f18542015-03-04 18:51:45 +0000409 case DS_Remark:
Rafael Espindola503f8832015-03-02 19:08:03 +0000410 Level = LDPL_INFO;
411 break;
Rafael Espindola503f8832015-03-02 19:08:03 +0000412 }
413 message(Level, "LLVM gold plugin: %s", ErrStorage.c_str());
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000414}
415
NAKAMURA Takumib13e63c2015-11-19 10:43:44 +0000416static void diagnosticHandlerForContext(const DiagnosticInfo &DI,
417 void *Context) {
418 diagnosticHandler(DI);
419}
420
Rafael Espindolacaabe222015-12-10 14:19:35 +0000421static GlobalValue::VisibilityTypes
422getMinVisibility(GlobalValue::VisibilityTypes A,
423 GlobalValue::VisibilityTypes B) {
424 if (A == GlobalValue::HiddenVisibility)
425 return A;
426 if (B == GlobalValue::HiddenVisibility)
427 return B;
428 if (A == GlobalValue::ProtectedVisibility)
429 return A;
430 return B;
431}
432
Rafael Espindolae54d8212014-07-06 14:31:22 +0000433/// Called by gold to see whether this file is one that our plugin can handle.
434/// We'll try to open it and register all the symbols with add_symbol if
435/// possible.
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000436static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
437 int *claimed) {
Rafael Espindola33466a72014-08-21 20:28:55 +0000438 LLVMContext Context;
Rafael Espindolaeeec8e62014-08-27 20:25:55 +0000439 MemoryBufferRef BufferRef;
440 std::unique_ptr<MemoryBuffer> Buffer;
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000441 if (get_view) {
Rafael Espindola33466a72014-08-21 20:28:55 +0000442 const void *view;
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000443 if (get_view(file->handle, &view) != LDPS_OK) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000444 message(LDPL_ERROR, "Failed to get a view of %s", file->name);
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000445 return LDPS_ERR;
446 }
Nick Lewycky7282dd72015-08-05 21:16:02 +0000447 BufferRef =
448 MemoryBufferRef(StringRef((const char *)view, file->filesize), "");
Ivan Krasin5021af52011-09-12 21:47:50 +0000449 } else {
Ivan Krasin639222d2011-09-15 23:13:00 +0000450 int64_t offset = 0;
Nick Lewycky8691c472009-02-05 04:14:23 +0000451 // Gold has found what might be IR part-way inside of a file, such as
452 // an .a archive.
Ivan Krasin5021af52011-09-12 21:47:50 +0000453 if (file->offset) {
454 offset = file->offset;
455 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000456 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
457 MemoryBuffer::getOpenFileSlice(file->fd, file->name, file->filesize,
458 offset);
459 if (std::error_code EC = BufferOrErr.getError()) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000460 message(LDPL_ERROR, EC.message().c_str());
Ivan Krasin5021af52011-09-12 21:47:50 +0000461 return LDPS_ERR;
462 }
Rafael Espindolaeeec8e62014-08-27 20:25:55 +0000463 Buffer = std::move(BufferOrErr.get());
464 BufferRef = Buffer->getMemBufferRef();
Rafael Espindola56e41f72011-02-08 22:40:47 +0000465 }
Ivan Krasin5021af52011-09-12 21:47:50 +0000466
NAKAMURA Takumib13e63c2015-11-19 10:43:44 +0000467 Context.setDiagnosticHandler(diagnosticHandlerForContext);
David Blaikie10a27df2014-09-03 17:59:23 +0000468 ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
Rafael Espindola5dec7ea2014-12-09 20:36:13 +0000469 object::IRObjectFile::create(BufferRef, Context);
Rafael Espindola33466a72014-08-21 20:28:55 +0000470 std::error_code EC = ObjOrErr.getError();
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000471 if (EC == object::object_error::invalid_file_type ||
Peter Collingbourne10039c02014-09-18 21:28:49 +0000472 EC == object::object_error::bitcode_section_not_found)
Ivan Krasin5021af52011-09-12 21:47:50 +0000473 return LDPS_OK;
474
Rafael Espindola6c472e52014-07-29 20:46:19 +0000475 *claimed = 1;
476
Rafael Espindola33466a72014-08-21 20:28:55 +0000477 if (EC) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000478 message(LDPL_ERROR, "LLVM gold plugin has failed to create LTO module: %s",
Rafael Espindola33466a72014-08-21 20:28:55 +0000479 EC.message().c_str());
Rafael Espindola6c472e52014-07-29 20:46:19 +0000480 return LDPS_ERR;
Ivan Krasind5f2d8c2011-09-09 00:14:04 +0000481 }
David Blaikie10a27df2014-09-03 17:59:23 +0000482 std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000483
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000484 Modules.resize(Modules.size() + 1);
485 claimed_file &cf = Modules.back();
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000486
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000487 cf.handle = file->handle;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000488
Teresa Johnson403a7872015-10-04 14:33:43 +0000489 // If we are doing ThinLTO compilation, don't need to process the symbols.
490 // Later we simply build a combined index file after all files are claimed.
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000491 if (options::thinlto && options::thinlto_index_only)
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000492 return LDPS_OK;
Teresa Johnson403a7872015-10-04 14:33:43 +0000493
Rafael Espindola33466a72014-08-21 20:28:55 +0000494 for (auto &Sym : Obj->symbols()) {
495 uint32_t Symflags = Sym.getFlags();
Rafael Espindola527e8462014-12-09 16:13:59 +0000496 if (shouldSkip(Symflags))
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000497 continue;
498
499 cf.syms.push_back(ld_plugin_symbol());
500 ld_plugin_symbol &sym = cf.syms.back();
Rafael Espindola176e6642014-07-29 21:46:05 +0000501 sym.version = nullptr;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000502
Rafael Espindola33466a72014-08-21 20:28:55 +0000503 SmallString<64> Name;
504 {
505 raw_svector_ostream OS(Name);
506 Sym.printName(OS);
507 }
508 sym.name = strdup(Name.c_str());
509
510 const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
511
Rafael Espindolacaabe222015-12-10 14:19:35 +0000512 ResolutionInfo &Res = ResInfo[sym.name];
513
Rafael Espindola33466a72014-08-21 20:28:55 +0000514 sym.visibility = LDPV_DEFAULT;
515 if (GV) {
Rafael Espindolacaabe222015-12-10 14:19:35 +0000516 Res.UnnamedAddr &= GV->hasUnnamedAddr();
517 Res.IsLinkonceOdr &= GV->hasLinkOnceLinkage();
518 if (GV->hasCommonLinkage()) {
519 Res.CommonAlign = std::max(Res.CommonAlign, GV->getAlignment());
520 const DataLayout &DL = GV->getParent()->getDataLayout();
521 uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
522 if (Size >= Res.CommonSize) {
523 Res.CommonSize = Size;
524 Res.CommonFile = &cf;
525 }
526 }
527 Res.Visibility = getMinVisibility(Res.Visibility, GV->getVisibility());
Rafael Espindola33466a72014-08-21 20:28:55 +0000528 switch (GV->getVisibility()) {
529 case GlobalValue::DefaultVisibility:
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000530 sym.visibility = LDPV_DEFAULT;
531 break;
Rafael Espindola33466a72014-08-21 20:28:55 +0000532 case GlobalValue::HiddenVisibility:
533 sym.visibility = LDPV_HIDDEN;
534 break;
535 case GlobalValue::ProtectedVisibility:
536 sym.visibility = LDPV_PROTECTED;
537 break;
538 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000539 }
540
Rafael Espindola33466a72014-08-21 20:28:55 +0000541 if (Symflags & object::BasicSymbolRef::SF_Undefined) {
542 sym.def = LDPK_UNDEF;
543 if (GV && GV->hasExternalWeakLinkage())
Rafael Espindola56548522009-04-24 16:55:21 +0000544 sym.def = LDPK_WEAKUNDEF;
Rafael Espindola33466a72014-08-21 20:28:55 +0000545 } else {
546 sym.def = LDPK_DEF;
547 if (GV) {
548 assert(!GV->hasExternalWeakLinkage() &&
549 !GV->hasAvailableExternallyLinkage() && "Not a declaration!");
550 if (GV->hasCommonLinkage())
551 sym.def = LDPK_COMMON;
552 else if (GV->isWeakForLinker())
553 sym.def = LDPK_WEAKDEF;
554 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000555 }
556
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000557 sym.size = 0;
Rafael Espindola33466a72014-08-21 20:28:55 +0000558 sym.comdat_key = nullptr;
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000559 if (GV) {
560 const GlobalObject *Base = getBaseObject(*GV);
561 if (!Base)
562 message(LDPL_FATAL, "Unable to determine comdat of alias!");
563 const Comdat *C = Base->getComdat();
564 if (C)
565 sym.comdat_key = strdup(C->getName().str().c_str());
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000566 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000567
568 sym.resolution = LDPR_UNKNOWN;
569 }
570
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000571 if (!cf.syms.empty()) {
Nick Lewycky7282dd72015-08-05 21:16:02 +0000572 if (add_symbols(cf.handle, cf.syms.size(), cf.syms.data()) != LDPS_OK) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000573 message(LDPL_ERROR, "Unable to add symbols!");
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000574 return LDPS_ERR;
575 }
576 }
577
578 return LDPS_OK;
579}
580
Rafael Espindola33466a72014-08-21 20:28:55 +0000581static void internalize(GlobalValue &GV) {
Rafael Espindolad4bcefc2014-10-24 18:13:04 +0000582 if (GV.isDeclarationForLinker())
Rafael Espindola33466a72014-08-21 20:28:55 +0000583 return; // We get here if there is a matching asm definition.
584 if (!GV.hasLocalLinkage())
585 GV.setLinkage(GlobalValue::InternalLinkage);
586}
587
Rafael Espindola33466a72014-08-21 20:28:55 +0000588static const char *getResolutionName(ld_plugin_symbol_resolution R) {
589 switch (R) {
590 case LDPR_UNKNOWN:
591 return "UNKNOWN";
592 case LDPR_UNDEF:
593 return "UNDEF";
594 case LDPR_PREVAILING_DEF:
595 return "PREVAILING_DEF";
596 case LDPR_PREVAILING_DEF_IRONLY:
597 return "PREVAILING_DEF_IRONLY";
598 case LDPR_PREEMPTED_REG:
599 return "PREEMPTED_REG";
600 case LDPR_PREEMPTED_IR:
601 return "PREEMPTED_IR";
602 case LDPR_RESOLVED_IR:
603 return "RESOLVED_IR";
604 case LDPR_RESOLVED_EXEC:
605 return "RESOLVED_EXEC";
606 case LDPR_RESOLVED_DYN:
607 return "RESOLVED_DYN";
608 case LDPR_PREVAILING_DEF_IRONLY_EXP:
609 return "PREVAILING_DEF_IRONLY_EXP";
610 }
Rafael Espindola2754dbb2014-10-10 00:48:13 +0000611 llvm_unreachable("Unknown resolution");
Rafael Espindola33466a72014-08-21 20:28:55 +0000612}
613
Rafael Espindola538c9a82014-12-23 18:18:37 +0000614static void freeSymName(ld_plugin_symbol &Sym) {
615 free(Sym.name);
616 free(Sym.comdat_key);
617 Sym.name = nullptr;
618 Sym.comdat_key = nullptr;
619}
620
Teresa Johnsona9f65552016-03-04 16:36:06 +0000621/// Helper to get a file's symbols and a view into it via gold callbacks.
622static const void *getSymbolsAndView(claimed_file &F) {
Benjamin Kramer39988a02016-03-08 14:02:46 +0000623 ld_plugin_status status = get_symbols(F.handle, F.syms.size(), F.syms.data());
Evgeniy Stepanov330c5a62016-03-04 00:23:29 +0000624 if (status == LDPS_NO_SYMS)
625 return nullptr;
Teresa Johnson403a7872015-10-04 14:33:43 +0000626
Evgeniy Stepanov330c5a62016-03-04 00:23:29 +0000627 if (status != LDPS_OK)
Teresa Johnson403a7872015-10-04 14:33:43 +0000628 message(LDPL_FATAL, "Failed to get symbol information");
629
630 const void *View;
631 if (get_view(F.handle, &View) != LDPS_OK)
632 message(LDPL_FATAL, "Failed to get a view of file");
633
Teresa Johnsona9f65552016-03-04 16:36:06 +0000634 return View;
635}
636
637static std::unique_ptr<FunctionInfoIndex>
638getFunctionIndexForFile(claimed_file &F, ld_plugin_input_file &Info) {
639 const void *View = getSymbolsAndView(F);
640
Teresa Johnson403a7872015-10-04 14:33:43 +0000641 MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize),
642 Info.name);
Teresa Johnson6290dbc2015-11-21 21:55:48 +0000643
644 // Don't bother trying to build an index if there is no summary information
645 // in this bitcode file.
646 if (!object::FunctionIndexObjectFile::hasFunctionSummaryInMemBuffer(
647 BufferRef, diagnosticHandler))
648 return std::unique_ptr<FunctionInfoIndex>(nullptr);
649
Teresa Johnson403a7872015-10-04 14:33:43 +0000650 ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr =
NAKAMURA Takumib13e63c2015-11-19 10:43:44 +0000651 object::FunctionIndexObjectFile::create(BufferRef, diagnosticHandler);
Teresa Johnson403a7872015-10-04 14:33:43 +0000652
653 if (std::error_code EC = ObjOrErr.getError())
654 message(LDPL_FATAL, "Could not read function index bitcode from file : %s",
655 EC.message().c_str());
656
657 object::FunctionIndexObjectFile &Obj = **ObjOrErr;
658
659 return Obj.takeIndex();
660}
661
Rafael Espindola33466a72014-08-21 20:28:55 +0000662static std::unique_ptr<Module>
Teresa Johnsona9f65552016-03-04 16:36:06 +0000663getModuleForFile(LLVMContext &Context, claimed_file &F, const void *View,
Rafael Espindola503f8832015-03-02 19:08:03 +0000664 ld_plugin_input_file &Info, raw_fd_ostream *ApiFile,
Rafael Espindolacaabe222015-12-10 14:19:35 +0000665 StringSet<> &Internalize, StringSet<> &Maybe,
666 std::vector<GlobalValue *> &Keep) {
Rafael Espindola503f8832015-03-02 19:08:03 +0000667 MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize),
668 Info.name);
Rafael Espindola527e8462014-12-09 16:13:59 +0000669 ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
Rafael Espindola5dec7ea2014-12-09 20:36:13 +0000670 object::IRObjectFile::create(BufferRef, Context);
Rafael Espindola527e8462014-12-09 16:13:59 +0000671
672 if (std::error_code EC = ObjOrErr.getError())
Peter Collingbourne10039c02014-09-18 21:28:49 +0000673 message(LDPL_FATAL, "Could not read bitcode from file : %s",
674 EC.message().c_str());
675
Rafael Espindola527e8462014-12-09 16:13:59 +0000676 object::IRObjectFile &Obj = **ObjOrErr;
Rafael Espindola33466a72014-08-21 20:28:55 +0000677
Rafael Espindola527e8462014-12-09 16:13:59 +0000678 Module &M = Obj.getModule();
Rafael Espindola33466a72014-08-21 20:28:55 +0000679
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000680 M.materializeMetadata();
681 UpgradeDebugInfo(M);
Rafael Espindola503f8832015-03-02 19:08:03 +0000682
Rafael Espindola33466a72014-08-21 20:28:55 +0000683 SmallPtrSet<GlobalValue *, 8> Used;
Rafael Espindola527e8462014-12-09 16:13:59 +0000684 collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
Rafael Espindola33466a72014-08-21 20:28:55 +0000685
Rafael Espindola527e8462014-12-09 16:13:59 +0000686 unsigned SymNum = 0;
687 for (auto &ObjSym : Obj.symbols()) {
Rafael Espindolacaabe222015-12-10 14:19:35 +0000688 GlobalValue *GV = Obj.getSymbolGV(ObjSym.getRawDataRefImpl());
689 if (GV && GV->hasAppendingLinkage())
690 Keep.push_back(GV);
691
Rafael Espindola527e8462014-12-09 16:13:59 +0000692 if (shouldSkip(ObjSym.getFlags()))
693 continue;
694 ld_plugin_symbol &Sym = F.syms[SymNum];
695 ++SymNum;
696
Rafael Espindola33466a72014-08-21 20:28:55 +0000697 ld_plugin_symbol_resolution Resolution =
698 (ld_plugin_symbol_resolution)Sym.resolution;
699
700 if (options::generate_api_file)
701 *ApiFile << Sym.name << ' ' << getResolutionName(Resolution) << '\n';
702
Rafael Espindola538c9a82014-12-23 18:18:37 +0000703 if (!GV) {
704 freeSymName(Sym);
Rafael Espindola33466a72014-08-21 20:28:55 +0000705 continue; // Asm symbol.
Rafael Espindola538c9a82014-12-23 18:18:37 +0000706 }
Rafael Espindola33466a72014-08-21 20:28:55 +0000707
Rafael Espindolacaabe222015-12-10 14:19:35 +0000708 ResolutionInfo &Res = ResInfo[Sym.name];
709 if (Resolution == LDPR_PREVAILING_DEF_IRONLY_EXP && !Res.IsLinkonceOdr)
710 Resolution = LDPR_PREVAILING_DEF;
711
Teresa Johnsona17f2cd2016-03-04 17:48:35 +0000712 // In ThinLTO mode change all prevailing resolutions to LDPR_PREVAILING_DEF.
713 // For ThinLTO the IR files are compiled through the backend independently,
714 // so we need to ensure that any prevailing linkonce copy will be emitted
715 // into the object file by making it weak. Additionally, we can skip the
716 // IRONLY handling for internalization, which isn't performed in ThinLTO
717 // mode currently anyway.
718 if (options::thinlto && (Resolution == LDPR_PREVAILING_DEF_IRONLY_EXP ||
719 Resolution == LDPR_PREVAILING_DEF_IRONLY))
720 Resolution = LDPR_PREVAILING_DEF;
721
Rafael Espindolacaabe222015-12-10 14:19:35 +0000722 GV->setUnnamedAddr(Res.UnnamedAddr);
723 GV->setVisibility(Res.Visibility);
724
725 // Override gold's resolution for common symbols. We want the largest
726 // one to win.
727 if (GV->hasCommonLinkage()) {
728 cast<GlobalVariable>(GV)->setAlignment(Res.CommonAlign);
729 if (Resolution == LDPR_PREVAILING_DEF_IRONLY)
730 Res.CommonInternal = true;
731
732 if (Resolution == LDPR_PREVAILING_DEF_IRONLY ||
733 Resolution == LDPR_PREVAILING_DEF)
734 Res.UseCommon = true;
735
736 if (Res.CommonFile == &F && Res.UseCommon) {
737 if (Res.CommonInternal)
738 Resolution = LDPR_PREVAILING_DEF_IRONLY;
739 else
740 Resolution = LDPR_PREVAILING_DEF;
741 } else {
742 Resolution = LDPR_PREEMPTED_IR;
743 }
Rafael Espindola890db272014-09-09 20:08:22 +0000744 }
745
Rafael Espindola33466a72014-08-21 20:28:55 +0000746 switch (Resolution) {
747 case LDPR_UNKNOWN:
748 llvm_unreachable("Unexpected resolution");
749
750 case LDPR_RESOLVED_IR:
751 case LDPR_RESOLVED_EXEC:
752 case LDPR_RESOLVED_DYN:
Rafael Espindolacaabe222015-12-10 14:19:35 +0000753 case LDPR_PREEMPTED_IR:
754 case LDPR_PREEMPTED_REG:
Rafael Espindola33466a72014-08-21 20:28:55 +0000755 break;
756
Rafael Espindola5ca7fa12015-01-14 13:53:50 +0000757 case LDPR_UNDEF:
Rafael Espindolacaabe222015-12-10 14:19:35 +0000758 if (!GV->isDeclarationForLinker())
Rafael Espindola0fd9e5f2015-01-14 19:43:32 +0000759 assert(GV->hasComdat());
Rafael Espindola5ca7fa12015-01-14 13:53:50 +0000760 break;
761
Rafael Espindola33466a72014-08-21 20:28:55 +0000762 case LDPR_PREVAILING_DEF_IRONLY: {
Rafael Espindolacaabe222015-12-10 14:19:35 +0000763 Keep.push_back(GV);
764 // The IR linker has to be able to map this value to a declaration,
765 // so we can only internalize after linking.
766 if (!Used.count(GV))
Rafael Espindolaa4f104b2014-12-09 16:50:57 +0000767 Internalize.insert(GV->getName());
Rafael Espindola33466a72014-08-21 20:28:55 +0000768 break;
769 }
770
771 case LDPR_PREVAILING_DEF:
Rafael Espindolacaabe222015-12-10 14:19:35 +0000772 Keep.push_back(GV);
773 // There is a non IR use, so we have to force optimizations to keep this.
774 switch (GV->getLinkage()) {
775 default:
776 break;
777 case GlobalValue::LinkOnceAnyLinkage:
778 GV->setLinkage(GlobalValue::WeakAnyLinkage);
779 break;
780 case GlobalValue::LinkOnceODRLinkage:
781 GV->setLinkage(GlobalValue::WeakODRLinkage);
782 break;
783 }
Rafael Espindola33466a72014-08-21 20:28:55 +0000784 break;
785
786 case LDPR_PREVAILING_DEF_IRONLY_EXP: {
787 // We can only check for address uses after we merge the modules. The
788 // reason is that this GV might have a copy in another module
789 // and in that module the address might be significant, but that
790 // copy will be LDPR_PREEMPTED_IR.
Rafael Espindolacaabe222015-12-10 14:19:35 +0000791 Maybe.insert(GV->getName());
792 Keep.push_back(GV);
Rafael Espindola33466a72014-08-21 20:28:55 +0000793 break;
794 }
795 }
796
Rafael Espindola538c9a82014-12-23 18:18:37 +0000797 freeSymName(Sym);
Rafael Espindola33466a72014-08-21 20:28:55 +0000798 }
799
Rafael Espindola527e8462014-12-09 16:13:59 +0000800 return Obj.takeModule();
Rafael Espindola33466a72014-08-21 20:28:55 +0000801}
802
Duncan P. N. Exon Smithe406c842015-04-15 00:13:51 +0000803static void saveBCFile(StringRef Path, Module &M) {
Rafael Espindola4a3b6cf2014-10-29 23:54:45 +0000804 std::error_code EC;
805 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
806 if (EC)
807 message(LDPL_FATAL, "Failed to write the output file.");
Rafael Espindola24052982015-12-22 23:45:49 +0000808 WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ false);
Rafael Espindola4a3b6cf2014-10-29 23:54:45 +0000809}
810
Teresa Johnsona9f65552016-03-04 16:36:06 +0000811static void recordFile(std::string Filename, bool TempOutFile) {
812 if (add_input_file(Filename.c_str()) != LDPS_OK)
813 message(LDPL_FATAL,
814 "Unable to add .o file to the link. File left behind in: %s",
815 Filename.c_str());
816 if (TempOutFile)
817 Cleanup.push_back(Filename.c_str());
818}
Rafael Espindola33466a72014-08-21 20:28:55 +0000819
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000820void ThinLTOTaskInfo::cleanup() {
821 // Close the output file descriptor before we pass it to gold.
822 OS->close();
823
824 recordFile(Filename, TempOutFile);
825}
826
Teresa Johnsona9f65552016-03-04 16:36:06 +0000827namespace {
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000828/// Class to manage optimization and code generation for a module, possibly
829/// in a thread (ThinLTO).
Teresa Johnsona9f65552016-03-04 16:36:06 +0000830class CodeGen {
831 /// The module for which this will generate code.
832 std::unique_ptr<llvm::Module> M;
Rafael Espindola33466a72014-08-21 20:28:55 +0000833
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000834 /// The output stream to generate code into.
835 raw_fd_ostream *OS;
836
837 /// The task ID when this was invoked in a thread (ThinLTO).
838 int TaskID;
839
840 /// The function index for ThinLTO tasks.
841 const FunctionInfoIndex *CombinedIndex;
842
Teresa Johnsona9f65552016-03-04 16:36:06 +0000843 /// The target machine for generating code for this module.
844 std::unique_ptr<TargetMachine> TM;
Rafael Espindola33466a72014-08-21 20:28:55 +0000845
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000846 /// Filename to use as base when save-temps is enabled, used to get
847 /// a unique and identifiable save-temps output file for each ThinLTO backend.
848 std::string SaveTempsFilename;
849
Teresa Johnsona9f65552016-03-04 16:36:06 +0000850public:
851 /// Constructor used by full LTO.
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000852 CodeGen(std::unique_ptr<llvm::Module> M)
853 : M(std::move(M)), OS(nullptr), TaskID(-1), CombinedIndex(nullptr) {
854 initTargetMachine();
855 }
856 /// Constructor used by ThinLTO.
857 CodeGen(std::unique_ptr<llvm::Module> M, raw_fd_ostream *OS, int TaskID,
858 const FunctionInfoIndex *CombinedIndex, std::string Filename)
859 : M(std::move(M)), OS(OS), TaskID(TaskID), CombinedIndex(CombinedIndex),
860 SaveTempsFilename(Filename) {
861 assert(options::thinlto == !!CombinedIndex &&
862 "Expected function index iff performing ThinLTO");
Teresa Johnsona9f65552016-03-04 16:36:06 +0000863 initTargetMachine();
864 }
865
866 /// Invoke LTO passes and the code generator for the module.
867 void runAll();
868
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000869 /// Invoke the actual code generation to emit Module's object to file.
870 void runCodegenPasses();
871
Teresa Johnsona9f65552016-03-04 16:36:06 +0000872private:
873 /// Create a target machine for the module. Must be unique for each
874 /// module/task.
875 void initTargetMachine();
876
877 /// Run all LTO passes on the module.
878 void runLTOPasses();
879
880 /// Sets up output files necessary to perform optional multi-threaded
881 /// split code generation, and invokes the code generation implementation.
882 void runSplitCodeGen();
883};
884}
885
886static SubtargetFeatures getFeatures(Triple &TheTriple) {
Rafael Espindola33466a72014-08-21 20:28:55 +0000887 SubtargetFeatures Features;
888 Features.getDefaultSubtargetFeatures(TheTriple);
889 for (const std::string &A : MAttrs)
890 Features.AddFeature(A);
Teresa Johnsona9f65552016-03-04 16:36:06 +0000891 return Features;
892}
Rafael Espindola33466a72014-08-21 20:28:55 +0000893
Teresa Johnsona9f65552016-03-04 16:36:06 +0000894static CodeGenOpt::Level getCGOptLevel() {
Peter Collingbourne070843d2015-03-19 22:01:00 +0000895 CodeGenOpt::Level CGOptLevel;
896 switch (options::OptLevel) {
897 case 0:
898 CGOptLevel = CodeGenOpt::None;
899 break;
900 case 1:
901 CGOptLevel = CodeGenOpt::Less;
902 break;
903 case 2:
904 CGOptLevel = CodeGenOpt::Default;
905 break;
906 case 3:
907 CGOptLevel = CodeGenOpt::Aggressive;
908 break;
909 }
Teresa Johnsona9f65552016-03-04 16:36:06 +0000910 return CGOptLevel;
911}
912
913void CodeGen::initTargetMachine() {
914 const std::string &TripleStr = M->getTargetTriple();
915 Triple TheTriple(TripleStr);
916
917 std::string ErrMsg;
918 const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
919 if (!TheTarget)
920 message(LDPL_FATAL, "Target not found: %s", ErrMsg.c_str());
921
922 SubtargetFeatures Features = getFeatures(TheTriple);
923 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
924 CodeGenOpt::Level CGOptLevel = getCGOptLevel();
925
926 TM.reset(TheTarget->createTargetMachine(
Rafael Espindola33466a72014-08-21 20:28:55 +0000927 TripleStr, options::mcpu, Features.getString(), Options, RelocationModel,
Peter Collingbourne070843d2015-03-19 22:01:00 +0000928 CodeModel::Default, CGOptLevel));
Teresa Johnsona9f65552016-03-04 16:36:06 +0000929}
Rafael Espindola33466a72014-08-21 20:28:55 +0000930
Teresa Johnsona9f65552016-03-04 16:36:06 +0000931void CodeGen::runLTOPasses() {
932 M->setDataLayout(TM->createDataLayout());
Rafael Espindola33466a72014-08-21 20:28:55 +0000933
Teresa Johnsona9f65552016-03-04 16:36:06 +0000934 legacy::PassManager passes;
935 passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
936
937 PassManagerBuilder PMB;
938 PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple()));
939 PMB.Inliner = createFunctionInliningPass();
940 // Unconditionally verify input since it is not verified before this
941 // point and has unknown origin.
942 PMB.VerifyInput = true;
943 PMB.VerifyOutput = !options::DisableVerify;
944 PMB.LoopVectorize = true;
945 PMB.SLPVectorize = true;
946 PMB.OptLevel = options::OptLevel;
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000947 PMB.FunctionIndex = CombinedIndex;
Teresa Johnsona9f65552016-03-04 16:36:06 +0000948 PMB.populateLTOPassManager(passes);
949 passes.run(*M);
950}
951
952/// Open a file and return the new file descriptor given a base input
953/// file name, a flag indicating whether a temp file should be generated,
954/// and an optional task id. The new filename generated is
955/// returned in \p NewFilename.
956static int openOutputFile(SmallString<128> InFilename, bool TempOutFile,
957 SmallString<128> &NewFilename, int TaskID = -1) {
958 int FD;
959 if (TempOutFile) {
960 std::error_code EC =
961 sys::fs::createTemporaryFile("lto-llvm", "o", FD, NewFilename);
962 if (EC)
963 message(LDPL_FATAL, "Could not create temporary file: %s",
964 EC.message().c_str());
965 } else {
966 NewFilename = InFilename;
967 if (TaskID >= 0)
968 NewFilename += utostr(TaskID);
969 std::error_code EC =
970 sys::fs::openFileForWrite(NewFilename, FD, sys::fs::F_None);
971 if (EC)
972 message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str());
973 }
974 return FD;
975}
976
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000977void CodeGen::runCodegenPasses() {
978 assert(OS && "Output stream must be set before emitting to file");
979 legacy::PassManager CodeGenPasses;
980 if (TM->addPassesToEmitFile(CodeGenPasses, *OS,
981 TargetMachine::CGFT_ObjectFile))
982 report_fatal_error("Failed to setup codegen");
983 CodeGenPasses.run(*M);
984}
985
Teresa Johnsona9f65552016-03-04 16:36:06 +0000986void CodeGen::runSplitCodeGen() {
987 const std::string &TripleStr = M->getTargetTriple();
988 Triple TheTriple(TripleStr);
989
990 SubtargetFeatures Features = getFeatures(TheTriple);
991
992 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
993 CodeGenOpt::Level CGOptLevel = getCGOptLevel();
Rafael Espindola33466a72014-08-21 20:28:55 +0000994
995 SmallString<128> Filename;
Teresa Johnsona9f65552016-03-04 16:36:06 +0000996 // Note that openOutputFile will append a unique ID for each task
Rafael Espindola92200d22015-06-15 13:36:27 +0000997 if (!options::obj_path.empty())
998 Filename = options::obj_path;
999 else if (options::TheOutputType == options::OT_SAVE_TEMPS)
1000 Filename = output_name + ".o";
1001
Teresa Johnsona9f65552016-03-04 16:36:06 +00001002 // Note that the default parallelism is 1 instead of the
1003 // hardware_concurrency, as there are behavioral differences between
1004 // parallelism levels (e.g. symbol ordering will be different, and some uses
1005 // of inline asm currently have issues with parallelism >1).
1006 unsigned int MaxThreads = options::Parallelism ? options::Parallelism : 1;
1007
1008 std::vector<SmallString<128>> Filenames(MaxThreads);
Rafael Espindola92200d22015-06-15 13:36:27 +00001009 bool TempOutFile = Filename.empty();
Rafael Espindola33466a72014-08-21 20:28:55 +00001010 {
Teresa Johnsona9f65552016-03-04 16:36:06 +00001011 // Open a file descriptor for each backend task. This is done in a block
Peter Collingbourne87202a42015-09-01 20:40:22 +00001012 // so that the output file descriptors are closed before gold opens them.
1013 std::list<llvm::raw_fd_ostream> OSs;
Teresa Johnsona9f65552016-03-04 16:36:06 +00001014 std::vector<llvm::raw_pwrite_stream *> OSPtrs(MaxThreads);
1015 for (unsigned I = 0; I != MaxThreads; ++I) {
Teresa Johnson3b8f6122016-03-04 18:16:00 +00001016 int FD = openOutputFile(Filename, TempOutFile, Filenames[I],
1017 // Only append ID if there are multiple tasks.
1018 MaxThreads > 1 ? I : -1);
Peter Collingbourne87202a42015-09-01 20:40:22 +00001019 OSs.emplace_back(FD, true);
1020 OSPtrs[I] = &OSs.back();
1021 }
Rafael Espindola33466a72014-08-21 20:28:55 +00001022
Teresa Johnsona9f65552016-03-04 16:36:06 +00001023 // Run backend tasks.
Peter Collingbourne87202a42015-09-01 20:40:22 +00001024 splitCodeGen(std::move(M), OSPtrs, options::mcpu, Features.getString(),
1025 Options, RelocationModel, CodeModel::Default, CGOptLevel);
Rafael Espindola33466a72014-08-21 20:28:55 +00001026 }
1027
Teresa Johnsona9f65552016-03-04 16:36:06 +00001028 for (auto &Filename : Filenames)
1029 recordFile(Filename.c_str(), TempOutFile);
1030}
1031
1032void CodeGen::runAll() {
1033 runLTOPasses();
1034
1035 if (options::TheOutputType == options::OT_SAVE_TEMPS) {
Teresa Johnson7cffaf32016-03-04 17:06:02 +00001036 std::string OptFilename = output_name;
1037 // If the CodeGen client provided a filename, use it. Always expect
1038 // a provided filename if we are in a task (i.e. ThinLTO backend).
1039 assert(!SaveTempsFilename.empty() || TaskID == -1);
1040 if (!SaveTempsFilename.empty())
1041 OptFilename = SaveTempsFilename;
1042 saveBCFile(OptFilename + ".opt.bc", *M);
Peter Collingbourne87202a42015-09-01 20:40:22 +00001043 }
Teresa Johnsona9f65552016-03-04 16:36:06 +00001044
Teresa Johnson7cffaf32016-03-04 17:06:02 +00001045 // If we are already in a thread (i.e. ThinLTO), just perform
1046 // codegen passes directly.
1047 if (TaskID >= 0)
1048 runCodegenPasses();
1049 // Otherwise attempt split code gen.
1050 else
1051 runSplitCodeGen();
Teresa Johnsona9f65552016-03-04 16:36:06 +00001052}
1053
1054/// Links the module in \p View from file \p F into the combined module
1055/// saved in the IRMover \p L. Returns true on error, false on success.
1056static bool linkInModule(LLVMContext &Context, IRMover &L, claimed_file &F,
1057 const void *View, ld_plugin_input_file &File,
1058 raw_fd_ostream *ApiFile, StringSet<> &Internalize,
1059 StringSet<> &Maybe) {
1060 std::vector<GlobalValue *> Keep;
1061 std::unique_ptr<Module> M = getModuleForFile(Context, F, View, File, ApiFile,
1062 Internalize, Maybe, Keep);
1063 if (!M.get())
1064 return false;
1065 if (!options::triple.empty())
1066 M->setTargetTriple(options::triple.c_str());
1067 else if (M->getTargetTriple().empty()) {
1068 M->setTargetTriple(DefaultTriple);
1069 }
1070
1071 if (L.move(std::move(M), Keep, [](GlobalValue &, IRMover::ValueAdder) {}))
1072 return true;
1073 return false;
Rafael Espindola282a4702013-10-31 20:51:58 +00001074}
1075
Teresa Johnson7cffaf32016-03-04 17:06:02 +00001076/// Perform the ThinLTO backend on a single module, invoking the LTO and codegen
1077/// pipelines.
1078static void thinLTOBackendTask(claimed_file &F, const void *View,
1079 ld_plugin_input_file &File,
1080 raw_fd_ostream *ApiFile,
1081 const FunctionInfoIndex &CombinedIndex,
1082 raw_fd_ostream *OS, unsigned TaskID) {
1083 // Need to use a separate context for each task
1084 LLVMContext Context;
1085 Context.setDiagnosticHandler(diagnosticHandlerForContext, nullptr, true);
1086
1087 std::unique_ptr<llvm::Module> NewModule(new llvm::Module(File.name, Context));
1088 IRMover L(*NewModule.get());
1089
1090 StringSet<> Dummy;
1091 if (linkInModule(Context, L, F, View, File, ApiFile, Dummy, Dummy))
1092 message(LDPL_FATAL, "Failed to rename module for ThinLTO");
Mehdi Aminid2d98962016-03-09 01:55:15 +00001093 if (renameModuleForThinLTO(*NewModule, CombinedIndex))
Teresa Johnson7cffaf32016-03-04 17:06:02 +00001094 message(LDPL_FATAL, "Failed to rename module for ThinLTO");
1095
1096 CodeGen codeGen(std::move(NewModule), OS, TaskID, &CombinedIndex, File.name);
1097 codeGen.runAll();
1098}
1099
1100/// Launch each module's backend pipeline in a separate task in a thread pool.
1101static void thinLTOBackends(raw_fd_ostream *ApiFile,
1102 const FunctionInfoIndex &CombinedIndex) {
1103 unsigned TaskCount = 0;
1104 std::vector<ThinLTOTaskInfo> Tasks;
1105 Tasks.reserve(Modules.size());
1106 unsigned int MaxThreads = options::Parallelism
1107 ? options::Parallelism
1108 : thread::hardware_concurrency();
1109
1110 // Create ThreadPool in nested scope so that threads will be joined
1111 // on destruction.
1112 {
1113 ThreadPool ThinLTOThreadPool(MaxThreads);
1114 for (claimed_file &F : Modules) {
1115 // Do all the gold callbacks in the main thread, since gold is not thread
1116 // safe by default.
1117 PluginInputFile InputFile(F.handle);
1118 const void *View = getSymbolsAndView(F);
1119
1120 SmallString<128> Filename;
1121 if (!options::obj_path.empty())
1122 // Note that openOutputFile will append a unique ID for each task
1123 Filename = options::obj_path;
1124 else if (options::TheOutputType == options::OT_SAVE_TEMPS) {
1125 // Use the input file name so that we get a unique and identifiable
1126 // output file for each ThinLTO backend task.
1127 Filename = InputFile.file().name;
1128 Filename += ".thinlto.o";
1129 }
1130 bool TempOutFile = Filename.empty();
1131
1132 SmallString<128> NewFilename;
1133 int FD = openOutputFile(Filename, TempOutFile, NewFilename,
1134 // Only append the TaskID if we will use the
1135 // non-unique obj_path.
1136 !options::obj_path.empty() ? TaskCount : -1);
1137 TaskCount++;
1138 std::unique_ptr<raw_fd_ostream> OS =
1139 llvm::make_unique<raw_fd_ostream>(FD, true);
1140
1141 // Enqueue the task
1142 ThinLTOThreadPool.async(thinLTOBackendTask, std::ref(F), View,
1143 std::ref(InputFile.file()), ApiFile,
1144 std::ref(CombinedIndex), OS.get(), TaskCount);
1145
1146 // Record the information needed by the task or during its cleanup
1147 // to a ThinLTOTaskInfo instance. For information needed by the task
1148 // the unique_ptr ownership is transferred to the ThinLTOTaskInfo.
1149 Tasks.emplace_back(std::move(InputFile), std::move(OS),
1150 NewFilename.c_str(), TempOutFile);
1151 }
1152 }
1153
1154 for (auto &Task : Tasks)
1155 Task.cleanup();
1156}
1157
Rafael Espindolab6393292014-07-30 01:23:45 +00001158/// gold informs us that all symbols have been read. At this point, we use
1159/// get_symbols to see if any of our definitions have been overridden by a
1160/// native object file. Then, perform optimization and codegen.
Rafael Espindola33466a72014-08-21 20:28:55 +00001161static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
1162 if (Modules.empty())
1163 return LDPS_OK;
Rafael Espindola9ef90d52011-02-20 18:28:29 +00001164
Teresa Johnsona9f65552016-03-04 16:36:06 +00001165 if (unsigned NumOpts = options::extra.size())
1166 cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
1167
Teresa Johnson403a7872015-10-04 14:33:43 +00001168 // If we are doing ThinLTO compilation, simply build the combined
1169 // function index/summary and emit it. We don't need to parse the modules
1170 // and link them in this case.
1171 if (options::thinlto) {
Teresa Johnson8af8d4e2015-10-19 15:23:03 +00001172 FunctionInfoIndex CombinedIndex;
Teresa Johnson403a7872015-10-04 14:33:43 +00001173 uint64_t NextModuleId = 0;
1174 for (claimed_file &F : Modules) {
Teresa Johnsoncb15b732015-12-16 16:34:06 +00001175 PluginInputFile InputFile(F.handle);
Teresa Johnson403a7872015-10-04 14:33:43 +00001176
1177 std::unique_ptr<FunctionInfoIndex> Index =
Teresa Johnsoncb15b732015-12-16 16:34:06 +00001178 getFunctionIndexForFile(F, InputFile.file());
Teresa Johnson6290dbc2015-11-21 21:55:48 +00001179
1180 // Skip files without a function summary.
Teresa Johnson9d5b71b2015-12-10 16:11:23 +00001181 if (Index)
1182 CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId);
Teresa Johnson403a7872015-10-04 14:33:43 +00001183 }
1184
1185 std::error_code EC;
1186 raw_fd_ostream OS(output_name + ".thinlto.bc", EC,
1187 sys::fs::OpenFlags::F_None);
1188 if (EC)
1189 message(LDPL_FATAL, "Unable to open %s.thinlto.bc for writing: %s",
1190 output_name.data(), EC.message().c_str());
Teresa Johnson3da931f2015-10-19 19:06:06 +00001191 WriteFunctionSummaryToFile(CombinedIndex, OS);
Teresa Johnson403a7872015-10-04 14:33:43 +00001192 OS.close();
1193
Teresa Johnson7cffaf32016-03-04 17:06:02 +00001194 if (options::thinlto_index_only) {
1195 cleanup_hook();
1196 exit(0);
1197 }
1198
1199 thinLTOBackends(ApiFile, CombinedIndex);
1200 return LDPS_OK;
Teresa Johnson403a7872015-10-04 14:33:43 +00001201 }
1202
Teresa Johnsonaf9e9312015-12-09 19:49:40 +00001203 LLVMContext Context;
1204 Context.setDiagnosticHandler(diagnosticHandlerForContext, nullptr, true);
1205
Rafael Espindola33466a72014-08-21 20:28:55 +00001206 std::unique_ptr<Module> Combined(new Module("ld-temp.o", Context));
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00001207 IRMover L(*Combined);
Rafael Espindola33466a72014-08-21 20:28:55 +00001208
Rafael Espindola33466a72014-08-21 20:28:55 +00001209 StringSet<> Internalize;
1210 StringSet<> Maybe;
Rafael Espindolad2aac572014-07-30 01:52:40 +00001211 for (claimed_file &F : Modules) {
Teresa Johnsoncb15b732015-12-16 16:34:06 +00001212 PluginInputFile InputFile(F.handle);
Teresa Johnsona9f65552016-03-04 16:36:06 +00001213 const void *View = getSymbolsAndView(F);
1214 if (linkInModule(Context, L, F, View, InputFile.file(), ApiFile,
1215 Internalize, Maybe))
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001216 message(LDPL_FATAL, "Failed to link module");
Rafael Espindola77b6d012010-06-14 21:20:52 +00001217 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +00001218
Rafael Espindola33466a72014-08-21 20:28:55 +00001219 for (const auto &Name : Internalize) {
1220 GlobalValue *GV = Combined->getNamedValue(Name.first());
1221 if (GV)
1222 internalize(*GV);
1223 }
1224
1225 for (const auto &Name : Maybe) {
1226 GlobalValue *GV = Combined->getNamedValue(Name.first());
1227 if (!GV)
1228 continue;
1229 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1230 if (canBeOmittedFromSymbolTable(GV))
1231 internalize(*GV);
1232 }
Rafael Espindolaccab1dd2010-08-11 00:15:13 +00001233
Rafael Espindola6953a3a2014-11-24 21:18:14 +00001234 if (options::TheOutputType == options::OT_DISABLE)
1235 return LDPS_OK;
1236
1237 if (options::TheOutputType != options::OT_NORMAL) {
Rafael Espindola8fb957e2010-06-03 21:11:20 +00001238 std::string path;
Rafael Espindola6953a3a2014-11-24 21:18:14 +00001239 if (options::TheOutputType == options::OT_BC_ONLY)
Rafael Espindola8fb957e2010-06-03 21:11:20 +00001240 path = output_name;
Rafael Espindola8fb957e2010-06-03 21:11:20 +00001241 else
1242 path = output_name + ".bc";
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001243 saveBCFile(path, *Combined);
Rafael Espindola6953a3a2014-11-24 21:18:14 +00001244 if (options::TheOutputType == options::OT_BC_ONLY)
Rafael Espindola55b32542014-08-11 19:06:54 +00001245 return LDPS_OK;
Rafael Espindolaba3398b2010-05-13 13:39:31 +00001246 }
Rafael Espindola143fc3b2013-10-16 12:47:04 +00001247
Teresa Johnsona9f65552016-03-04 16:36:06 +00001248 CodeGen codeGen(std::move(Combined));
1249 codeGen.runAll();
Nick Lewyckyfb643e42009-02-03 07:13:24 +00001250
Rafael Espindolaef498152010-06-23 20:20:59 +00001251 if (!options::extra_library_path.empty() &&
Rafael Espindola33466a72014-08-21 20:28:55 +00001252 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK)
1253 message(LDPL_FATAL, "Unable to set the extra library path.");
Shuxin Yang1826ae22013-08-12 21:07:31 +00001254
Nick Lewyckyfb643e42009-02-03 07:13:24 +00001255 return LDPS_OK;
1256}
1257
Rafael Espindola55b32542014-08-11 19:06:54 +00001258static ld_plugin_status all_symbols_read_hook(void) {
1259 ld_plugin_status Ret;
1260 if (!options::generate_api_file) {
1261 Ret = allSymbolsReadHook(nullptr);
1262 } else {
Rafael Espindola3fd1e992014-08-25 18:16:47 +00001263 std::error_code EC;
1264 raw_fd_ostream ApiFile("apifile.txt", EC, sys::fs::F_None);
1265 if (EC)
Rafael Espindola55b32542014-08-11 19:06:54 +00001266 message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s",
Rafael Espindola3fd1e992014-08-25 18:16:47 +00001267 EC.message().c_str());
Rafael Espindola33466a72014-08-21 20:28:55 +00001268 Ret = allSymbolsReadHook(&ApiFile);
Rafael Espindola55b32542014-08-11 19:06:54 +00001269 }
1270
Rafael Espindola947bdb62014-11-25 20:52:49 +00001271 llvm_shutdown();
1272
Rafael Espindola6953a3a2014-11-24 21:18:14 +00001273 if (options::TheOutputType == options::OT_BC_ONLY ||
Michael Kupersteina07d9b92015-02-12 18:21:50 +00001274 options::TheOutputType == options::OT_DISABLE) {
1275 if (options::TheOutputType == options::OT_DISABLE)
1276 // Remove the output file here since ld.bfd creates the output file
1277 // early.
1278 sys::fs::remove(output_name);
Rafael Espindola55b32542014-08-11 19:06:54 +00001279 exit(0);
Michael Kupersteina07d9b92015-02-12 18:21:50 +00001280 }
Rafael Espindola55b32542014-08-11 19:06:54 +00001281
1282 return Ret;
1283}
1284
Dan Gohmanebb4ae02010-04-16 00:42:57 +00001285static ld_plugin_status cleanup_hook(void) {
Rafael Espindolad2aac572014-07-30 01:52:40 +00001286 for (std::string &Name : Cleanup) {
1287 std::error_code EC = sys::fs::remove(Name);
Rafael Espindola55ab87f2013-06-17 18:38:18 +00001288 if (EC)
Rafael Espindolad2aac572014-07-30 01:52:40 +00001289 message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(),
Rafael Espindola5ad21fa2014-07-30 00:38:58 +00001290 EC.message().c_str());
Rafael Espindola55ab87f2013-06-17 18:38:18 +00001291 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +00001292
1293 return LDPS_OK;
1294}