blob: 68c9d1a6f6e4d7335332abd6931c0bc38d844098 [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"
Rafael Espindola0d68b4c2015-03-30 21:36:43 +000023#include "llvm/IR/AutoUpgrade.h"
Rafael Espindola890db272014-09-09 20:08:22 +000024#include "llvm/IR/Constants.h"
Rafael Espindolad0b23be2015-01-10 00:07:30 +000025#include "llvm/IR/DiagnosticInfo.h"
26#include "llvm/IR/DiagnosticPrinter.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000027#include "llvm/IR/LLVMContext.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000028#include "llvm/IR/LegacyPassManager.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000029#include "llvm/IR/Module.h"
30#include "llvm/IR/Verifier.h"
31#include "llvm/Linker/Linker.h"
32#include "llvm/MC/SubtargetFeature.h"
33#include "llvm/Object/IRObjectFile.h"
Rafael Espindola5682ce22015-04-09 21:06:08 +000034#include "llvm/Support/raw_ostream.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000035#include "llvm/Support/Host.h"
Rafael Espindola947bdb62014-11-25 20:52:49 +000036#include "llvm/Support/ManagedStatic.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000037#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000038#include "llvm/Support/TargetRegistry.h"
Rafael Espindola6b244b12014-06-19 21:14:13 +000039#include "llvm/Support/TargetSelect.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000040#include "llvm/Transforms/IPO.h"
41#include "llvm/Transforms/IPO/PassManagerBuilder.h"
42#include "llvm/Transforms/Utils/GlobalStatus.h"
43#include "llvm/Transforms/Utils/ModuleUtils.h"
Rafael Espindolaf7ecb112014-08-22 23:26:10 +000044#include "llvm/Transforms/Utils/ValueMapper.h"
Nick Lewyckyfb643e42009-02-03 07:13:24 +000045#include <list>
Chandler Carruth07baed52014-01-13 08:04:33 +000046#include <plugin-api.h>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000047#include <system_error>
Nick Lewyckyfb643e42009-02-03 07:13:24 +000048#include <vector>
49
Sylvestre Ledru53999792014-02-11 17:30:18 +000050#ifndef LDPO_PIE
51// FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and
52// Precise and Debian Wheezy (binutils 2.23 is required)
53# define LDPO_PIE 3
54#endif
55
Nick Lewyckyfb643e42009-02-03 07:13:24 +000056using namespace llvm;
57
58namespace {
Rafael Espindolabfb8b912014-06-20 01:37:35 +000059struct claimed_file {
60 void *handle;
61 std::vector<ld_plugin_symbol> syms;
62};
Nick Lewyckyfb643e42009-02-03 07:13:24 +000063}
Rafael Espindolabfb8b912014-06-20 01:37:35 +000064
65static ld_plugin_status discard_message(int level, const char *format, ...) {
66 // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
67 // callback in the transfer vector. This should never be called.
68 abort();
69}
70
Rafael Espindola33466a72014-08-21 20:28:55 +000071static ld_plugin_get_input_file get_input_file = nullptr;
72static ld_plugin_release_input_file release_input_file = nullptr;
Rafael Espindola176e6642014-07-29 21:46:05 +000073static ld_plugin_add_symbols add_symbols = nullptr;
74static ld_plugin_get_symbols get_symbols = nullptr;
75static ld_plugin_add_input_file add_input_file = nullptr;
76static ld_plugin_set_extra_library_path set_extra_library_path = nullptr;
77static ld_plugin_get_view get_view = nullptr;
Rafael Espindolabfb8b912014-06-20 01:37:35 +000078static ld_plugin_message message = discard_message;
Rafael Espindola33466a72014-08-21 20:28:55 +000079static Reloc::Model RelocationModel = Reloc::Default;
Rafael Espindolabfb8b912014-06-20 01:37:35 +000080static std::string output_name = "";
81static std::list<claimed_file> Modules;
82static std::vector<std::string> Cleanup;
Rafael Espindola6b244b12014-06-19 21:14:13 +000083static llvm::TargetOptions TargetOpts;
Nick Lewyckyfb643e42009-02-03 07:13:24 +000084
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000085namespace options {
Rafael Espindola6953a3a2014-11-24 21:18:14 +000086 enum OutputType {
87 OT_NORMAL,
88 OT_DISABLE,
89 OT_BC_ONLY,
90 OT_SAVE_TEMPS
91 };
Dan Gohmanebb4ae02010-04-16 00:42:57 +000092 static bool generate_api_file = false;
Rafael Espindola6953a3a2014-11-24 21:18:14 +000093 static OutputType TheOutputType = OT_NORMAL;
Peter Collingbourne070843d2015-03-19 22:01:00 +000094 static unsigned OptLevel = 2;
Shuxin Yang1826ae22013-08-12 21:07:31 +000095 static std::string obj_path;
Rafael Espindolaef498152010-06-23 20:20:59 +000096 static std::string extra_library_path;
Rafael Espindola4ef89f52010-08-09 21:09:46 +000097 static std::string triple;
Rafael Espindolaccab1dd2010-08-11 00:15:13 +000098 static std::string mcpu;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +000099 // Additional options to pass into the code generator.
Nick Lewycky0ac5e222010-06-03 17:10:17 +0000100 // Note: This array will contain all plugin options which are not claimed
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000101 // as plugin exclusive to pass to the code generator.
Nick Lewycky0ac5e222010-06-03 17:10:17 +0000102 // For example, "generate-api-file" and "as"options are for the plugin
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000103 // use only and will not be passed.
Rafael Espindola125b9242014-07-29 19:17:44 +0000104 static std::vector<const char *> extra;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000105
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000106 static void process_plugin_option(const char* opt_)
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000107 {
Rafael Espindola176e6642014-07-29 21:46:05 +0000108 if (opt_ == nullptr)
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000109 return;
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000110 llvm::StringRef opt = opt_;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000111
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000112 if (opt == "generate-api-file") {
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000113 generate_api_file = true;
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000114 } else if (opt.startswith("mcpu=")) {
115 mcpu = opt.substr(strlen("mcpu="));
Rafael Espindolaef498152010-06-23 20:20:59 +0000116 } else if (opt.startswith("extra-library-path=")) {
117 extra_library_path = opt.substr(strlen("extra_library_path="));
Rafael Espindola148c3282010-08-10 16:32:15 +0000118 } else if (opt.startswith("mtriple=")) {
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000119 triple = opt.substr(strlen("mtriple="));
Shuxin Yang1826ae22013-08-12 21:07:31 +0000120 } else if (opt.startswith("obj-path=")) {
121 obj_path = opt.substr(strlen("obj-path="));
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000122 } else if (opt == "emit-llvm") {
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000123 TheOutputType = OT_BC_ONLY;
Rafael Espindola4a3b6cf2014-10-29 23:54:45 +0000124 } else if (opt == "save-temps") {
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000125 TheOutputType = OT_SAVE_TEMPS;
126 } else if (opt == "disable-output") {
127 TheOutputType = OT_DISABLE;
Peter Collingbourne070843d2015-03-19 22:01:00 +0000128 } else if (opt.size() == 2 && opt[0] == 'O') {
129 if (opt[1] < '0' || opt[1] > '3')
130 report_fatal_error("Optimization level must be between 0 and 3");
131 OptLevel = opt[1] - '0';
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000132 } else {
133 // Save this option to pass to the code generator.
Rafael Espindola33466a72014-08-21 20:28:55 +0000134 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily
135 // add that.
136 if (extra.empty())
137 extra.push_back("LLVMgold");
138
Rafael Espindola125b9242014-07-29 19:17:44 +0000139 extra.push_back(opt_);
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000140 }
141 }
142}
143
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000144static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
145 int *claimed);
146static ld_plugin_status all_symbols_read_hook(void);
147static ld_plugin_status cleanup_hook(void);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000148
149extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
150ld_plugin_status onload(ld_plugin_tv *tv) {
Peter Collingbourne1505c0a2014-07-03 23:28:03 +0000151 InitializeAllTargetInfos();
152 InitializeAllTargets();
153 InitializeAllTargetMCs();
154 InitializeAllAsmParsers();
155 InitializeAllAsmPrinters();
156
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000157 // We're given a pointer to the first transfer vector. We read through them
158 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
159 // contain pointers to functions that we need to call to register our own
160 // hooks. The others are addresses of functions we can use to call into gold
161 // for services.
162
163 bool registeredClaimFile = false;
Rafael Espindola6b244b12014-06-19 21:14:13 +0000164 bool RegisteredAllSymbolsRead = false;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000165
166 for (; tv->tv_tag != LDPT_NULL; ++tv) {
167 switch (tv->tv_tag) {
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000168 case LDPT_OUTPUT_NAME:
169 output_name = tv->tv_u.tv_string;
170 break;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000171 case LDPT_LINKER_OUTPUT:
172 switch (tv->tv_u.tv_val) {
173 case LDPO_REL: // .o
174 case LDPO_DYN: // .so
Rafael Espindola76e376d2014-02-10 20:38:38 +0000175 case LDPO_PIE: // position independent executable
Rafael Espindola33466a72014-08-21 20:28:55 +0000176 RelocationModel = Reloc::PIC_;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000177 break;
178 case LDPO_EXEC: // .exe
Rafael Espindola33466a72014-08-21 20:28:55 +0000179 RelocationModel = Reloc::Static;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000180 break;
181 default:
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000182 message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000183 return LDPS_ERR;
184 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000185 break;
186 case LDPT_OPTION:
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000187 options::process_plugin_option(tv->tv_u.tv_string);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000188 break;
189 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
190 ld_plugin_register_claim_file callback;
191 callback = tv->tv_u.tv_register_claim_file;
192
Rafael Espindola54f82b72014-07-30 01:36:32 +0000193 if (callback(claim_file_hook) != LDPS_OK)
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000194 return LDPS_ERR;
195
196 registeredClaimFile = true;
197 } break;
198 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
199 ld_plugin_register_all_symbols_read callback;
200 callback = tv->tv_u.tv_register_all_symbols_read;
201
Rafael Espindola54f82b72014-07-30 01:36:32 +0000202 if (callback(all_symbols_read_hook) != LDPS_OK)
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000203 return LDPS_ERR;
Rafael Espindola9ef90d52011-02-20 18:28:29 +0000204
Rafael Espindola6b244b12014-06-19 21:14:13 +0000205 RegisteredAllSymbolsRead = true;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000206 } break;
207 case LDPT_REGISTER_CLEANUP_HOOK: {
208 ld_plugin_register_cleanup callback;
209 callback = tv->tv_u.tv_register_cleanup;
210
Rafael Espindola54f82b72014-07-30 01:36:32 +0000211 if (callback(cleanup_hook) != LDPS_OK)
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000212 return LDPS_ERR;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000213 } break;
Rafael Espindola33466a72014-08-21 20:28:55 +0000214 case LDPT_GET_INPUT_FILE:
215 get_input_file = tv->tv_u.tv_get_input_file;
216 break;
217 case LDPT_RELEASE_INPUT_FILE:
218 release_input_file = tv->tv_u.tv_release_input_file;
219 break;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000220 case LDPT_ADD_SYMBOLS:
221 add_symbols = tv->tv_u.tv_add_symbols;
222 break;
Rafael Espindolacda29112013-10-03 18:29:09 +0000223 case LDPT_GET_SYMBOLS_V2:
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000224 get_symbols = tv->tv_u.tv_get_symbols;
225 break;
226 case LDPT_ADD_INPUT_FILE:
227 add_input_file = tv->tv_u.tv_add_input_file;
228 break;
Rafael Espindolaef498152010-06-23 20:20:59 +0000229 case LDPT_SET_EXTRA_LIBRARY_PATH:
230 set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
231 break;
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000232 case LDPT_GET_VIEW:
233 get_view = tv->tv_u.tv_get_view;
234 break;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000235 case LDPT_MESSAGE:
236 message = tv->tv_u.tv_message;
237 break;
238 default:
239 break;
240 }
241 }
242
Rafael Espindolae08484d2009-02-18 08:30:15 +0000243 if (!registeredClaimFile) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000244 message(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
Rafael Espindola6add6182009-02-18 17:49:06 +0000245 return LDPS_ERR;
246 }
Rafael Espindolae08484d2009-02-18 08:30:15 +0000247 if (!add_symbols) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000248 message(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
Rafael Espindola6add6182009-02-18 17:49:06 +0000249 return LDPS_ERR;
250 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000251
Rafael Espindolaa0d30a92014-06-19 22:20:07 +0000252 if (!RegisteredAllSymbolsRead)
253 return LDPS_OK;
Rafael Espindola6b244b12014-06-19 21:14:13 +0000254
Rafael Espindola33466a72014-08-21 20:28:55 +0000255 if (!get_input_file) {
256 message(LDPL_ERROR, "get_input_file not passed to LLVMgold.");
257 return LDPS_ERR;
Rafael Espindolac273aac2014-06-19 22:54:47 +0000258 }
Rafael Espindola33466a72014-08-21 20:28:55 +0000259 if (!release_input_file) {
260 message(LDPL_ERROR, "relesase_input_file not passed to LLVMgold.");
261 return LDPS_ERR;
Tom Roederb5081192014-06-26 20:43:27 +0000262 }
263
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000264 return LDPS_OK;
265}
266
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000267static const GlobalObject *getBaseObject(const GlobalValue &GV) {
268 if (auto *GA = dyn_cast<GlobalAlias>(&GV))
269 return GA->getBaseObject();
270 return cast<GlobalObject>(&GV);
271}
272
Rafael Espindola527e8462014-12-09 16:13:59 +0000273static bool shouldSkip(uint32_t Symflags) {
274 if (!(Symflags & object::BasicSymbolRef::SF_Global))
275 return true;
276 if (Symflags & object::BasicSymbolRef::SF_FormatSpecific)
277 return true;
278 return false;
279}
280
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000281static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
Rafael Espindola503f8832015-03-02 19:08:03 +0000282 if (const auto *BDI = dyn_cast<BitcodeDiagnosticInfo>(&DI)) {
283 std::error_code EC = BDI->getError();
284 if (EC == BitcodeError::InvalidBitcodeSignature)
285 return;
286 }
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000287
288 std::string ErrStorage;
289 {
290 raw_string_ostream OS(ErrStorage);
291 DiagnosticPrinterRawOStream DP(OS);
292 DI.print(DP);
293 }
Rafael Espindola503f8832015-03-02 19:08:03 +0000294 ld_plugin_level Level;
295 switch (DI.getSeverity()) {
296 case DS_Error:
297 message(LDPL_FATAL, "LLVM gold plugin has failed to create LTO module: %s",
298 ErrStorage.c_str());
299 llvm_unreachable("Fatal doesn't return.");
300 case DS_Warning:
301 Level = LDPL_WARNING;
302 break;
303 case DS_Note:
Rafael Espindolaf3f18542015-03-04 18:51:45 +0000304 case DS_Remark:
Rafael Espindola503f8832015-03-02 19:08:03 +0000305 Level = LDPL_INFO;
306 break;
Rafael Espindola503f8832015-03-02 19:08:03 +0000307 }
308 message(Level, "LLVM gold plugin: %s", ErrStorage.c_str());
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000309}
310
Rafael Espindolae54d8212014-07-06 14:31:22 +0000311/// Called by gold to see whether this file is one that our plugin can handle.
312/// We'll try to open it and register all the symbols with add_symbol if
313/// possible.
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000314static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
315 int *claimed) {
Rafael Espindola33466a72014-08-21 20:28:55 +0000316 LLVMContext Context;
Rafael Espindolaeeec8e62014-08-27 20:25:55 +0000317 MemoryBufferRef BufferRef;
318 std::unique_ptr<MemoryBuffer> Buffer;
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000319 if (get_view) {
Rafael Espindola33466a72014-08-21 20:28:55 +0000320 const void *view;
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000321 if (get_view(file->handle, &view) != LDPS_OK) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000322 message(LDPL_ERROR, "Failed to get a view of %s", file->name);
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000323 return LDPS_ERR;
324 }
Rafael Espindolab0fc4cf2014-10-22 02:23:31 +0000325 BufferRef = MemoryBufferRef(StringRef((const char *)view, file->filesize), "");
Ivan Krasin5021af52011-09-12 21:47:50 +0000326 } else {
Ivan Krasin639222d2011-09-15 23:13:00 +0000327 int64_t offset = 0;
Nick Lewycky8691c472009-02-05 04:14:23 +0000328 // Gold has found what might be IR part-way inside of a file, such as
329 // an .a archive.
Ivan Krasin5021af52011-09-12 21:47:50 +0000330 if (file->offset) {
331 offset = file->offset;
332 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000333 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
334 MemoryBuffer::getOpenFileSlice(file->fd, file->name, file->filesize,
335 offset);
336 if (std::error_code EC = BufferOrErr.getError()) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000337 message(LDPL_ERROR, EC.message().c_str());
Ivan Krasin5021af52011-09-12 21:47:50 +0000338 return LDPS_ERR;
339 }
Rafael Espindolaeeec8e62014-08-27 20:25:55 +0000340 Buffer = std::move(BufferOrErr.get());
341 BufferRef = Buffer->getMemBufferRef();
Rafael Espindola56e41f72011-02-08 22:40:47 +0000342 }
Ivan Krasin5021af52011-09-12 21:47:50 +0000343
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000344 Context.setDiagnosticHandler(diagnosticHandler);
David Blaikie10a27df2014-09-03 17:59:23 +0000345 ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
Rafael Espindola5dec7ea2014-12-09 20:36:13 +0000346 object::IRObjectFile::create(BufferRef, Context);
Rafael Espindola33466a72014-08-21 20:28:55 +0000347 std::error_code EC = ObjOrErr.getError();
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000348 if (EC == object::object_error::invalid_file_type ||
Peter Collingbourne10039c02014-09-18 21:28:49 +0000349 EC == object::object_error::bitcode_section_not_found)
Ivan Krasin5021af52011-09-12 21:47:50 +0000350 return LDPS_OK;
351
Rafael Espindola6c472e52014-07-29 20:46:19 +0000352 *claimed = 1;
353
Rafael Espindola33466a72014-08-21 20:28:55 +0000354 if (EC) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000355 message(LDPL_ERROR, "LLVM gold plugin has failed to create LTO module: %s",
Rafael Espindola33466a72014-08-21 20:28:55 +0000356 EC.message().c_str());
Rafael Espindola6c472e52014-07-29 20:46:19 +0000357 return LDPS_ERR;
Ivan Krasind5f2d8c2011-09-09 00:14:04 +0000358 }
David Blaikie10a27df2014-09-03 17:59:23 +0000359 std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000360
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000361 Modules.resize(Modules.size() + 1);
362 claimed_file &cf = Modules.back();
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000363
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000364 cf.handle = file->handle;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000365
Rafael Espindola33466a72014-08-21 20:28:55 +0000366 for (auto &Sym : Obj->symbols()) {
367 uint32_t Symflags = Sym.getFlags();
Rafael Espindola527e8462014-12-09 16:13:59 +0000368 if (shouldSkip(Symflags))
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000369 continue;
370
371 cf.syms.push_back(ld_plugin_symbol());
372 ld_plugin_symbol &sym = cf.syms.back();
Rafael Espindola176e6642014-07-29 21:46:05 +0000373 sym.version = nullptr;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000374
Rafael Espindola33466a72014-08-21 20:28:55 +0000375 SmallString<64> Name;
376 {
377 raw_svector_ostream OS(Name);
378 Sym.printName(OS);
379 }
380 sym.name = strdup(Name.c_str());
381
382 const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
383
384 sym.visibility = LDPV_DEFAULT;
385 if (GV) {
386 switch (GV->getVisibility()) {
387 case GlobalValue::DefaultVisibility:
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000388 sym.visibility = LDPV_DEFAULT;
389 break;
Rafael Espindola33466a72014-08-21 20:28:55 +0000390 case GlobalValue::HiddenVisibility:
391 sym.visibility = LDPV_HIDDEN;
392 break;
393 case GlobalValue::ProtectedVisibility:
394 sym.visibility = LDPV_PROTECTED;
395 break;
396 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000397 }
398
Rafael Espindola33466a72014-08-21 20:28:55 +0000399 if (Symflags & object::BasicSymbolRef::SF_Undefined) {
400 sym.def = LDPK_UNDEF;
401 if (GV && GV->hasExternalWeakLinkage())
Rafael Espindola56548522009-04-24 16:55:21 +0000402 sym.def = LDPK_WEAKUNDEF;
Rafael Espindola33466a72014-08-21 20:28:55 +0000403 } else {
404 sym.def = LDPK_DEF;
405 if (GV) {
406 assert(!GV->hasExternalWeakLinkage() &&
407 !GV->hasAvailableExternallyLinkage() && "Not a declaration!");
408 if (GV->hasCommonLinkage())
409 sym.def = LDPK_COMMON;
410 else if (GV->isWeakForLinker())
411 sym.def = LDPK_WEAKDEF;
412 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000413 }
414
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000415 sym.size = 0;
Rafael Espindola33466a72014-08-21 20:28:55 +0000416 sym.comdat_key = nullptr;
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000417 if (GV) {
418 const GlobalObject *Base = getBaseObject(*GV);
419 if (!Base)
420 message(LDPL_FATAL, "Unable to determine comdat of alias!");
421 const Comdat *C = Base->getComdat();
422 if (C)
423 sym.comdat_key = strdup(C->getName().str().c_str());
424 else if (Base->hasWeakLinkage() || Base->hasLinkOnceLinkage())
425 sym.comdat_key = strdup(sym.name);
426 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000427
428 sym.resolution = LDPR_UNKNOWN;
429 }
430
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000431 if (!cf.syms.empty()) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000432 if (add_symbols(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
433 message(LDPL_ERROR, "Unable to add symbols!");
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000434 return LDPS_ERR;
435 }
436 }
437
438 return LDPS_OK;
439}
440
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000441static void keepGlobalValue(GlobalValue &GV,
442 std::vector<GlobalAlias *> &KeptAliases) {
Rafael Espindola33466a72014-08-21 20:28:55 +0000443 assert(!GV.hasLocalLinkage());
444
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000445 if (auto *GA = dyn_cast<GlobalAlias>(&GV))
446 KeptAliases.push_back(GA);
447
Rafael Espindola33466a72014-08-21 20:28:55 +0000448 switch (GV.getLinkage()) {
449 default:
450 break;
451 case GlobalValue::LinkOnceAnyLinkage:
452 GV.setLinkage(GlobalValue::WeakAnyLinkage);
453 break;
454 case GlobalValue::LinkOnceODRLinkage:
455 GV.setLinkage(GlobalValue::WeakODRLinkage);
456 break;
457 }
458
459 assert(!GV.isDiscardableIfUnused());
460}
461
Rafael Espindola33466a72014-08-21 20:28:55 +0000462static void internalize(GlobalValue &GV) {
Rafael Espindolad4bcefc2014-10-24 18:13:04 +0000463 if (GV.isDeclarationForLinker())
Rafael Espindola33466a72014-08-21 20:28:55 +0000464 return; // We get here if there is a matching asm definition.
465 if (!GV.hasLocalLinkage())
466 GV.setLinkage(GlobalValue::InternalLinkage);
467}
468
469static void drop(GlobalValue &GV) {
470 if (auto *F = dyn_cast<Function>(&GV)) {
471 F->deleteBody();
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000472 F->setComdat(nullptr); // Should deleteBody do this?
Rafael Espindola33466a72014-08-21 20:28:55 +0000473 return;
474 }
475
476 if (auto *Var = dyn_cast<GlobalVariable>(&GV)) {
477 Var->setInitializer(nullptr);
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000478 Var->setLinkage(
479 GlobalValue::ExternalLinkage); // Should setInitializer do this?
480 Var->setComdat(nullptr); // and this?
Rafael Espindola33466a72014-08-21 20:28:55 +0000481 return;
482 }
483
484 auto &Alias = cast<GlobalAlias>(GV);
485 Module &M = *Alias.getParent();
486 PointerType &Ty = *cast<PointerType>(Alias.getType());
487 GlobalValue::LinkageTypes L = Alias.getLinkage();
488 auto *Var =
489 new GlobalVariable(M, Ty.getElementType(), /*isConstant*/ false, L,
490 /*Initializer*/ nullptr);
491 Var->takeName(&Alias);
492 Alias.replaceAllUsesWith(Var);
Rafael Espindola71143ed2014-09-10 19:39:41 +0000493 Alias.eraseFromParent();
Rafael Espindola33466a72014-08-21 20:28:55 +0000494}
495
496static const char *getResolutionName(ld_plugin_symbol_resolution R) {
497 switch (R) {
498 case LDPR_UNKNOWN:
499 return "UNKNOWN";
500 case LDPR_UNDEF:
501 return "UNDEF";
502 case LDPR_PREVAILING_DEF:
503 return "PREVAILING_DEF";
504 case LDPR_PREVAILING_DEF_IRONLY:
505 return "PREVAILING_DEF_IRONLY";
506 case LDPR_PREEMPTED_REG:
507 return "PREEMPTED_REG";
508 case LDPR_PREEMPTED_IR:
509 return "PREEMPTED_IR";
510 case LDPR_RESOLVED_IR:
511 return "RESOLVED_IR";
512 case LDPR_RESOLVED_EXEC:
513 return "RESOLVED_EXEC";
514 case LDPR_RESOLVED_DYN:
515 return "RESOLVED_DYN";
516 case LDPR_PREVAILING_DEF_IRONLY_EXP:
517 return "PREVAILING_DEF_IRONLY_EXP";
518 }
Rafael Espindola2754dbb2014-10-10 00:48:13 +0000519 llvm_unreachable("Unknown resolution");
Rafael Espindola33466a72014-08-21 20:28:55 +0000520}
521
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000522namespace {
523class LocalValueMaterializer : public ValueMaterializer {
524 DenseSet<GlobalValue *> &Dropped;
Rafael Espindola4d47f7f2014-12-10 00:09:35 +0000525 DenseMap<GlobalObject *, GlobalObject *> LocalVersions;
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000526
527public:
528 LocalValueMaterializer(DenseSet<GlobalValue *> &Dropped) : Dropped(Dropped) {}
529 Value *materializeValueFor(Value *V) override;
530};
531}
532
533Value *LocalValueMaterializer::materializeValueFor(Value *V) {
Rafael Espindola4d47f7f2014-12-10 00:09:35 +0000534 auto *GO = dyn_cast<GlobalObject>(V);
535 if (!GO)
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000536 return nullptr;
Rafael Espindola4d47f7f2014-12-10 00:09:35 +0000537
538 auto I = LocalVersions.find(GO);
539 if (I != LocalVersions.end())
540 return I->second;
541
542 if (!Dropped.count(GO))
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000543 return nullptr;
Rafael Espindola4d47f7f2014-12-10 00:09:35 +0000544
545 Module &M = *GO->getParent();
546 GlobalValue::LinkageTypes L = GO->getLinkage();
547 GlobalObject *Declaration;
548 if (auto *F = dyn_cast<Function>(GO)) {
549 Declaration = Function::Create(F->getFunctionType(), L, "", &M);
550 } else {
551 auto *Var = cast<GlobalVariable>(GO);
552 Declaration = new GlobalVariable(M, Var->getType()->getElementType(),
553 Var->isConstant(), L,
554 /*Initializer*/ nullptr);
555 }
556 Declaration->takeName(GO);
557 Declaration->copyAttributesFrom(GO);
558
559 GO->setLinkage(GlobalValue::InternalLinkage);
560 GO->setName(Declaration->getName());
561 Dropped.erase(GO);
562 GO->replaceAllUsesWith(Declaration);
563
564 LocalVersions[Declaration] = GO;
565
566 return GO;
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000567}
568
569static Constant *mapConstantToLocalCopy(Constant *C, ValueToValueMapTy &VM,
570 LocalValueMaterializer *Materializer) {
571 return MapValue(C, VM, RF_IgnoreMissingEntries, nullptr, Materializer);
572}
573
Rafael Espindola538c9a82014-12-23 18:18:37 +0000574static void freeSymName(ld_plugin_symbol &Sym) {
575 free(Sym.name);
576 free(Sym.comdat_key);
577 Sym.name = nullptr;
578 Sym.comdat_key = nullptr;
579}
580
Rafael Espindola33466a72014-08-21 20:28:55 +0000581static std::unique_ptr<Module>
Jan Wen Voungc11b45a2015-02-11 16:12:50 +0000582getModuleForFile(LLVMContext &Context, claimed_file &F,
Rafael Espindola503f8832015-03-02 19:08:03 +0000583 ld_plugin_input_file &Info, raw_fd_ostream *ApiFile,
Rafael Espindola33466a72014-08-21 20:28:55 +0000584 StringSet<> &Internalize, StringSet<> &Maybe) {
Rafael Espindola33466a72014-08-21 20:28:55 +0000585
586 if (get_symbols(F.handle, F.syms.size(), &F.syms[0]) != LDPS_OK)
587 message(LDPL_FATAL, "Failed to get symbol information");
588
589 const void *View;
590 if (get_view(F.handle, &View) != LDPS_OK)
591 message(LDPL_FATAL, "Failed to get a view of file");
592
Rafael Espindola503f8832015-03-02 19:08:03 +0000593 MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize),
594 Info.name);
Rafael Espindola527e8462014-12-09 16:13:59 +0000595 ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
Rafael Espindola5dec7ea2014-12-09 20:36:13 +0000596 object::IRObjectFile::create(BufferRef, Context);
Rafael Espindola527e8462014-12-09 16:13:59 +0000597
598 if (std::error_code EC = ObjOrErr.getError())
Peter Collingbourne10039c02014-09-18 21:28:49 +0000599 message(LDPL_FATAL, "Could not read bitcode from file : %s",
600 EC.message().c_str());
601
Rafael Espindola527e8462014-12-09 16:13:59 +0000602 object::IRObjectFile &Obj = **ObjOrErr;
Rafael Espindola33466a72014-08-21 20:28:55 +0000603
Rafael Espindola527e8462014-12-09 16:13:59 +0000604 Module &M = Obj.getModule();
Rafael Espindola33466a72014-08-21 20:28:55 +0000605
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000606 M.materializeMetadata();
607 UpgradeDebugInfo(M);
Rafael Espindola503f8832015-03-02 19:08:03 +0000608
Rafael Espindola33466a72014-08-21 20:28:55 +0000609 SmallPtrSet<GlobalValue *, 8> Used;
Rafael Espindola527e8462014-12-09 16:13:59 +0000610 collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
Rafael Espindola33466a72014-08-21 20:28:55 +0000611
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000612 DenseSet<GlobalValue *> Drop;
613 std::vector<GlobalAlias *> KeptAliases;
Rafael Espindola527e8462014-12-09 16:13:59 +0000614
615 unsigned SymNum = 0;
616 for (auto &ObjSym : Obj.symbols()) {
617 if (shouldSkip(ObjSym.getFlags()))
618 continue;
619 ld_plugin_symbol &Sym = F.syms[SymNum];
620 ++SymNum;
621
Rafael Espindola33466a72014-08-21 20:28:55 +0000622 ld_plugin_symbol_resolution Resolution =
623 (ld_plugin_symbol_resolution)Sym.resolution;
624
625 if (options::generate_api_file)
626 *ApiFile << Sym.name << ' ' << getResolutionName(Resolution) << '\n';
627
Rafael Espindola527e8462014-12-09 16:13:59 +0000628 GlobalValue *GV = Obj.getSymbolGV(ObjSym.getRawDataRefImpl());
Rafael Espindola538c9a82014-12-23 18:18:37 +0000629 if (!GV) {
630 freeSymName(Sym);
Rafael Espindola33466a72014-08-21 20:28:55 +0000631 continue; // Asm symbol.
Rafael Espindola538c9a82014-12-23 18:18:37 +0000632 }
Rafael Espindola33466a72014-08-21 20:28:55 +0000633
Rafael Espindola51bd8ee2014-09-17 20:41:13 +0000634 if (Resolution != LDPR_PREVAILING_DEF_IRONLY && GV->hasCommonLinkage()) {
Rafael Espindola890db272014-09-09 20:08:22 +0000635 // Common linkage is special. There is no single symbol that wins the
636 // resolution. Instead we have to collect the maximum alignment and size.
637 // The IR linker does that for us if we just pass it every common GV.
Rafael Espindola51bd8ee2014-09-17 20:41:13 +0000638 // We still have to keep track of LDPR_PREVAILING_DEF_IRONLY so we
639 // internalize once the IR linker has done its job.
Rafael Espindola538c9a82014-12-23 18:18:37 +0000640 freeSymName(Sym);
Rafael Espindola890db272014-09-09 20:08:22 +0000641 continue;
642 }
643
Rafael Espindola33466a72014-08-21 20:28:55 +0000644 switch (Resolution) {
645 case LDPR_UNKNOWN:
646 llvm_unreachable("Unexpected resolution");
647
648 case LDPR_RESOLVED_IR:
649 case LDPR_RESOLVED_EXEC:
650 case LDPR_RESOLVED_DYN:
Rafael Espindolad4bcefc2014-10-24 18:13:04 +0000651 assert(GV->isDeclarationForLinker());
Rafael Espindola33466a72014-08-21 20:28:55 +0000652 break;
653
Rafael Espindola5ca7fa12015-01-14 13:53:50 +0000654 case LDPR_UNDEF:
Rafael Espindola9e3e53f2015-01-14 20:08:46 +0000655 if (!GV->isDeclarationForLinker()) {
Rafael Espindola0fd9e5f2015-01-14 19:43:32 +0000656 assert(GV->hasComdat());
657 Drop.insert(GV);
658 }
Rafael Espindola5ca7fa12015-01-14 13:53:50 +0000659 break;
660
Rafael Espindola33466a72014-08-21 20:28:55 +0000661 case LDPR_PREVAILING_DEF_IRONLY: {
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000662 keepGlobalValue(*GV, KeptAliases);
Rafael Espindola33466a72014-08-21 20:28:55 +0000663 if (!Used.count(GV)) {
664 // Since we use the regular lib/Linker, we cannot just internalize GV
665 // now or it will not be copied to the merged module. Instead we force
666 // it to be copied and then internalize it.
Rafael Espindolaa4f104b2014-12-09 16:50:57 +0000667 Internalize.insert(GV->getName());
Rafael Espindola33466a72014-08-21 20:28:55 +0000668 }
669 break;
670 }
671
672 case LDPR_PREVAILING_DEF:
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000673 keepGlobalValue(*GV, KeptAliases);
Rafael Espindola33466a72014-08-21 20:28:55 +0000674 break;
675
Rafael Espindola33466a72014-08-21 20:28:55 +0000676 case LDPR_PREEMPTED_IR:
Rafael Espindoladfc7ed72014-10-07 04:06:13 +0000677 // Gold might have selected a linkonce_odr and preempted a weak_odr.
678 // In that case we have to make sure we don't end up internalizing it.
679 if (!GV->isDiscardableIfUnused())
Rafael Espindolaa4f104b2014-12-09 16:50:57 +0000680 Maybe.erase(GV->getName());
Rafael Espindoladfc7ed72014-10-07 04:06:13 +0000681
682 // fall-through
683 case LDPR_PREEMPTED_REG:
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000684 Drop.insert(GV);
Rafael Espindola33466a72014-08-21 20:28:55 +0000685 break;
686
687 case LDPR_PREVAILING_DEF_IRONLY_EXP: {
688 // We can only check for address uses after we merge the modules. The
689 // reason is that this GV might have a copy in another module
690 // and in that module the address might be significant, but that
691 // copy will be LDPR_PREEMPTED_IR.
692 if (GV->hasLinkOnceODRLinkage())
Rafael Espindolaa4f104b2014-12-09 16:50:57 +0000693 Maybe.insert(GV->getName());
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000694 keepGlobalValue(*GV, KeptAliases);
Rafael Espindola33466a72014-08-21 20:28:55 +0000695 break;
696 }
697 }
698
Rafael Espindola538c9a82014-12-23 18:18:37 +0000699 freeSymName(Sym);
Rafael Espindola33466a72014-08-21 20:28:55 +0000700 }
701
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000702 ValueToValueMapTy VM;
703 LocalValueMaterializer Materializer(Drop);
704 for (GlobalAlias *GA : KeptAliases) {
705 // Gold told us to keep GA. It is possible that a GV usied in the aliasee
706 // expression is being dropped. If that is the case, that GV must be copied.
707 Constant *Aliasee = GA->getAliasee();
708 Constant *Replacement = mapConstantToLocalCopy(Aliasee, VM, &Materializer);
Rafael Espindola4d47f7f2014-12-10 00:09:35 +0000709 GA->setAliasee(Replacement);
Rafael Espindola33466a72014-08-21 20:28:55 +0000710 }
711
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000712 for (auto *GV : Drop)
713 drop(*GV);
714
Rafael Espindola527e8462014-12-09 16:13:59 +0000715 return Obj.takeModule();
Rafael Espindola33466a72014-08-21 20:28:55 +0000716}
717
NAKAMURA Takumib0a52832015-02-02 05:47:30 +0000718static void runLTOPasses(Module &M, TargetMachine &TM) {
Rafael Espindola85d85092015-02-21 00:13:15 +0000719 if (const DataLayout *DL = TM.getDataLayout())
Rafael Espindola265ffbe2015-03-04 19:15:29 +0000720 M.setDataLayout(*DL);
Rafael Espindola85d85092015-02-21 00:13:15 +0000721
Chandler Carruth30d69c22015-02-13 10:01:29 +0000722 legacy::PassManager passes;
NAKAMURA Takumib0a52832015-02-02 05:47:30 +0000723 passes.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
724
Chandler Carruth5700b372015-02-13 21:10:58 +0000725 PassManagerBuilder PMB;
Sylvestre Ledru450f97d2015-01-24 13:59:08 +0000726 PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM.getTargetTriple()));
Rafael Espindola33466a72014-08-21 20:28:55 +0000727 PMB.Inliner = createFunctionInliningPass();
728 PMB.VerifyInput = true;
729 PMB.VerifyOutput = true;
Rafael Espindola8391dbd2014-10-30 00:11:24 +0000730 PMB.LoopVectorize = true;
Rafael Espindola919fb532014-10-30 00:38:54 +0000731 PMB.SLPVectorize = true;
Peter Collingbourne070843d2015-03-19 22:01:00 +0000732 PMB.OptLevel = options::OptLevel;
Alexey Samsonov5ce24482015-01-30 19:14:04 +0000733 PMB.populateLTOPassManager(passes);
Rafael Espindola33466a72014-08-21 20:28:55 +0000734 passes.run(M);
735}
736
Duncan P. N. Exon Smithe406c842015-04-15 00:13:51 +0000737static void saveBCFile(StringRef Path, Module &M) {
Rafael Espindola4a3b6cf2014-10-29 23:54:45 +0000738 std::error_code EC;
739 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
740 if (EC)
741 message(LDPL_FATAL, "Failed to write the output file.");
Duncan P. N. Exon Smitha052ed62015-04-15 00:10:50 +0000742 WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
Rafael Espindola4a3b6cf2014-10-29 23:54:45 +0000743}
744
Rafael Espindola33466a72014-08-21 20:28:55 +0000745static void codegen(Module &M) {
746 const std::string &TripleStr = M.getTargetTriple();
747 Triple TheTriple(TripleStr);
748
749 std::string ErrMsg;
750 const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
751 if (!TheTarget)
752 message(LDPL_FATAL, "Target not found: %s", ErrMsg.c_str());
753
754 if (unsigned NumOpts = options::extra.size())
755 cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
756
757 SubtargetFeatures Features;
758 Features.getDefaultSubtargetFeatures(TheTriple);
759 for (const std::string &A : MAttrs)
760 Features.AddFeature(A);
761
762 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne070843d2015-03-19 22:01:00 +0000763 CodeGenOpt::Level CGOptLevel;
764 switch (options::OptLevel) {
765 case 0:
766 CGOptLevel = CodeGenOpt::None;
767 break;
768 case 1:
769 CGOptLevel = CodeGenOpt::Less;
770 break;
771 case 2:
772 CGOptLevel = CodeGenOpt::Default;
773 break;
774 case 3:
775 CGOptLevel = CodeGenOpt::Aggressive;
776 break;
777 }
Rafael Espindola33466a72014-08-21 20:28:55 +0000778 std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
779 TripleStr, options::mcpu, Features.getString(), Options, RelocationModel,
Peter Collingbourne070843d2015-03-19 22:01:00 +0000780 CodeModel::Default, CGOptLevel));
Rafael Espindola33466a72014-08-21 20:28:55 +0000781
782 runLTOPasses(M, *TM);
783
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000784 if (options::TheOutputType == options::OT_SAVE_TEMPS)
Rafael Espindola4a3b6cf2014-10-29 23:54:45 +0000785 saveBCFile(output_name + ".opt.bc", M);
786
Chandler Carruth30d69c22015-02-13 10:01:29 +0000787 legacy::PassManager CodeGenPasses;
Rafael Espindola33466a72014-08-21 20:28:55 +0000788
789 SmallString<128> Filename;
Rafael Espindola92200d22015-06-15 13:36:27 +0000790 if (!options::obj_path.empty())
791 Filename = options::obj_path;
792 else if (options::TheOutputType == options::OT_SAVE_TEMPS)
793 Filename = output_name + ".o";
794
Rafael Espindola33466a72014-08-21 20:28:55 +0000795 int FD;
Rafael Espindola92200d22015-06-15 13:36:27 +0000796 bool TempOutFile = Filename.empty();
797 if (TempOutFile) {
Rafael Espindola33466a72014-08-21 20:28:55 +0000798 std::error_code EC =
799 sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
800 if (EC)
Duncan P. N. Exon Smithf6ab4702014-11-19 22:39:21 +0000801 message(LDPL_FATAL, "Could not create temporary file: %s",
Rafael Espindola33466a72014-08-21 20:28:55 +0000802 EC.message().c_str());
803 } else {
Rafael Espindola33466a72014-08-21 20:28:55 +0000804 std::error_code EC =
805 sys::fs::openFileForWrite(Filename.c_str(), FD, sys::fs::F_None);
806 if (EC)
807 message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str());
808 }
809
810 {
811 raw_fd_ostream OS(FD, true);
Rafael Espindola33466a72014-08-21 20:28:55 +0000812
Rafael Espindola5682ce22015-04-09 21:06:08 +0000813 if (TM->addPassesToEmitFile(CodeGenPasses, OS,
Rafael Espindola33466a72014-08-21 20:28:55 +0000814 TargetMachine::CGFT_ObjectFile))
815 message(LDPL_FATAL, "Failed to setup codegen");
816 CodeGenPasses.run(M);
817 }
818
819 if (add_input_file(Filename.c_str()) != LDPS_OK)
820 message(LDPL_FATAL,
821 "Unable to add .o file to the link. File left behind in: %s",
822 Filename.c_str());
823
Rafael Espindola92200d22015-06-15 13:36:27 +0000824 if (TempOutFile)
Rafael Espindola33466a72014-08-21 20:28:55 +0000825 Cleanup.push_back(Filename.c_str());
Rafael Espindola282a4702013-10-31 20:51:58 +0000826}
827
Rafael Espindolab6393292014-07-30 01:23:45 +0000828/// gold informs us that all symbols have been read. At this point, we use
829/// get_symbols to see if any of our definitions have been overridden by a
830/// native object file. Then, perform optimization and codegen.
Rafael Espindola33466a72014-08-21 20:28:55 +0000831static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
832 if (Modules.empty())
833 return LDPS_OK;
Rafael Espindola9ef90d52011-02-20 18:28:29 +0000834
Rafael Espindola33466a72014-08-21 20:28:55 +0000835 LLVMContext Context;
Rafael Espindolaf3f18542015-03-04 18:51:45 +0000836 Context.setDiagnosticHandler(diagnosticHandler, nullptr, true);
Rafael Espindola503f8832015-03-02 19:08:03 +0000837
Rafael Espindola33466a72014-08-21 20:28:55 +0000838 std::unique_ptr<Module> Combined(new Module("ld-temp.o", Context));
839 Linker L(Combined.get());
840
841 std::string DefaultTriple = sys::getDefaultTargetTriple();
842
843 StringSet<> Internalize;
844 StringSet<> Maybe;
Rafael Espindolad2aac572014-07-30 01:52:40 +0000845 for (claimed_file &F : Modules) {
Jan Wen Voungc11b45a2015-02-11 16:12:50 +0000846 ld_plugin_input_file File;
847 if (get_input_file(F.handle, &File) != LDPS_OK)
848 message(LDPL_FATAL, "Failed to get file information");
Rafael Espindola33466a72014-08-21 20:28:55 +0000849 std::unique_ptr<Module> M =
Rafael Espindola503f8832015-03-02 19:08:03 +0000850 getModuleForFile(Context, F, File, ApiFile, Internalize, Maybe);
Rafael Espindola33466a72014-08-21 20:28:55 +0000851 if (!options::triple.empty())
852 M->setTargetTriple(options::triple.c_str());
853 else if (M->getTargetTriple().empty()) {
854 M->setTargetTriple(DefaultTriple);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000855 }
Rafael Espindola33466a72014-08-21 20:28:55 +0000856
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000857 if (L.linkInModule(M.get()))
858 message(LDPL_FATAL, "Failed to link module");
Jan Wen Voungc11b45a2015-02-11 16:12:50 +0000859 if (release_input_file(F.handle) != LDPS_OK)
860 message(LDPL_FATAL, "Failed to release file information");
Rafael Espindola77b6d012010-06-14 21:20:52 +0000861 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000862
Rafael Espindola33466a72014-08-21 20:28:55 +0000863 for (const auto &Name : Internalize) {
864 GlobalValue *GV = Combined->getNamedValue(Name.first());
865 if (GV)
866 internalize(*GV);
867 }
868
869 for (const auto &Name : Maybe) {
870 GlobalValue *GV = Combined->getNamedValue(Name.first());
871 if (!GV)
872 continue;
873 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
874 if (canBeOmittedFromSymbolTable(GV))
875 internalize(*GV);
876 }
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000877
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000878 if (options::TheOutputType == options::OT_DISABLE)
879 return LDPS_OK;
880
881 if (options::TheOutputType != options::OT_NORMAL) {
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000882 std::string path;
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000883 if (options::TheOutputType == options::OT_BC_ONLY)
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000884 path = output_name;
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000885 else
886 path = output_name + ".bc";
Rafael Espindola4a3b6cf2014-10-29 23:54:45 +0000887 saveBCFile(path, *L.getModule());
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000888 if (options::TheOutputType == options::OT_BC_ONLY)
Rafael Espindola55b32542014-08-11 19:06:54 +0000889 return LDPS_OK;
Rafael Espindolaba3398b2010-05-13 13:39:31 +0000890 }
Rafael Espindola143fc3b2013-10-16 12:47:04 +0000891
Rafael Espindola33466a72014-08-21 20:28:55 +0000892 codegen(*L.getModule());
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000893
Rafael Espindolaef498152010-06-23 20:20:59 +0000894 if (!options::extra_library_path.empty() &&
Rafael Espindola33466a72014-08-21 20:28:55 +0000895 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK)
896 message(LDPL_FATAL, "Unable to set the extra library path.");
Shuxin Yang1826ae22013-08-12 21:07:31 +0000897
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000898 return LDPS_OK;
899}
900
Rafael Espindola55b32542014-08-11 19:06:54 +0000901static ld_plugin_status all_symbols_read_hook(void) {
902 ld_plugin_status Ret;
903 if (!options::generate_api_file) {
904 Ret = allSymbolsReadHook(nullptr);
905 } else {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000906 std::error_code EC;
907 raw_fd_ostream ApiFile("apifile.txt", EC, sys::fs::F_None);
908 if (EC)
Rafael Espindola55b32542014-08-11 19:06:54 +0000909 message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s",
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000910 EC.message().c_str());
Rafael Espindola33466a72014-08-21 20:28:55 +0000911 Ret = allSymbolsReadHook(&ApiFile);
Rafael Espindola55b32542014-08-11 19:06:54 +0000912 }
913
Rafael Espindola947bdb62014-11-25 20:52:49 +0000914 llvm_shutdown();
915
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000916 if (options::TheOutputType == options::OT_BC_ONLY ||
Michael Kupersteina07d9b92015-02-12 18:21:50 +0000917 options::TheOutputType == options::OT_DISABLE) {
918 if (options::TheOutputType == options::OT_DISABLE)
919 // Remove the output file here since ld.bfd creates the output file
920 // early.
921 sys::fs::remove(output_name);
Rafael Espindola55b32542014-08-11 19:06:54 +0000922 exit(0);
Michael Kupersteina07d9b92015-02-12 18:21:50 +0000923 }
Rafael Espindola55b32542014-08-11 19:06:54 +0000924
925 return Ret;
926}
927
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000928static ld_plugin_status cleanup_hook(void) {
Rafael Espindolad2aac572014-07-30 01:52:40 +0000929 for (std::string &Name : Cleanup) {
930 std::error_code EC = sys::fs::remove(Name);
Rafael Espindola55ab87f2013-06-17 18:38:18 +0000931 if (EC)
Rafael Espindolad2aac572014-07-30 01:52:40 +0000932 message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(),
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000933 EC.message().c_str());
Rafael Espindola55ab87f2013-06-17 18:38:18 +0000934 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000935
936 return LDPS_OK;
937}