2009-02-03 07:13:24 +00:00
|
|
|
//===-- gold-plugin.cpp - Plugin to gold for Link Time Optimization ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is a gold plugin for LLVM. It provides an LLVM implementation of the
|
|
|
|
// interface described in http://gcc.gnu.org/wiki/whopr/driver .
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-12-22 23:04:07 +00:00
|
|
|
#include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
|
2014-08-22 23:26:10 +00:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2014-01-13 08:04:33 +00:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2015-01-15 02:16:27 +00:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2015-02-02 05:47:30 +00:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2014-08-21 20:28:55 +00:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
|
|
|
#include "llvm/CodeGen/Analysis.h"
|
2014-06-19 21:14:13 +00:00
|
|
|
#include "llvm/CodeGen/CommandFlags.h"
|
2015-03-30 21:36:43 +00:00
|
|
|
#include "llvm/IR/AutoUpgrade.h"
|
2014-09-09 20:08:22 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
2015-01-10 00:07:30 +00:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2014-08-21 20:28:55 +00:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2015-02-13 10:01:29 +00:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2014-08-21 20:28:55 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Verifier.h"
|
|
|
|
#include "llvm/Linker/Linker.h"
|
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
|
|
|
#include "llvm/Object/IRObjectFile.h"
|
2015-04-09 21:06:08 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-08-21 20:28:55 +00:00
|
|
|
#include "llvm/Support/Host.h"
|
2014-11-25 20:52:49 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-08-21 20:28:55 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2014-06-19 21:14:13 +00:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2014-08-21 20:28:55 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
|
|
|
#include "llvm/Transforms/Utils/GlobalStatus.h"
|
|
|
|
#include "llvm/Transforms/Utils/ModuleUtils.h"
|
2014-08-22 23:26:10 +00:00
|
|
|
#include "llvm/Transforms/Utils/ValueMapper.h"
|
2009-02-03 07:13:24 +00:00
|
|
|
#include <list>
|
2014-01-13 08:04:33 +00:00
|
|
|
#include <plugin-api.h>
|
2014-06-12 17:38:55 +00:00
|
|
|
#include <system_error>
|
2009-02-03 07:13:24 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-02-11 17:30:18 +00:00
|
|
|
#ifndef LDPO_PIE
|
|
|
|
// FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and
|
|
|
|
// Precise and Debian Wheezy (binutils 2.23 is required)
|
|
|
|
# define LDPO_PIE 3
|
|
|
|
#endif
|
|
|
|
|
2009-02-03 07:13:24 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2014-06-20 01:37:35 +00:00
|
|
|
struct claimed_file {
|
|
|
|
void *handle;
|
|
|
|
std::vector<ld_plugin_symbol> syms;
|
|
|
|
};
|
|
|
|
}
|
2009-02-03 07:13:24 +00:00
|
|
|
|
2014-06-20 01:37:35 +00:00
|
|
|
static ld_plugin_status discard_message(int level, const char *format, ...) {
|
|
|
|
// Die loudly. Recent versions of Gold pass ld_plugin_message as the first
|
|
|
|
// callback in the transfer vector. This should never be called.
|
|
|
|
abort();
|
2009-02-03 07:13:24 +00:00
|
|
|
}
|
2014-06-20 01:37:35 +00:00
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
static ld_plugin_get_input_file get_input_file = nullptr;
|
|
|
|
static ld_plugin_release_input_file release_input_file = nullptr;
|
2014-07-29 21:46:05 +00:00
|
|
|
static ld_plugin_add_symbols add_symbols = nullptr;
|
|
|
|
static ld_plugin_get_symbols get_symbols = nullptr;
|
|
|
|
static ld_plugin_add_input_file add_input_file = nullptr;
|
|
|
|
static ld_plugin_set_extra_library_path set_extra_library_path = nullptr;
|
|
|
|
static ld_plugin_get_view get_view = nullptr;
|
2014-06-20 01:37:35 +00:00
|
|
|
static ld_plugin_message message = discard_message;
|
2014-08-21 20:28:55 +00:00
|
|
|
static Reloc::Model RelocationModel = Reloc::Default;
|
2014-06-20 01:37:35 +00:00
|
|
|
static std::string output_name = "";
|
|
|
|
static std::list<claimed_file> Modules;
|
|
|
|
static std::vector<std::string> Cleanup;
|
2014-06-19 21:14:13 +00:00
|
|
|
static llvm::TargetOptions TargetOpts;
|
2009-02-03 07:13:24 +00:00
|
|
|
|
2009-10-28 18:55:55 +00:00
|
|
|
namespace options {
|
2014-11-24 21:18:14 +00:00
|
|
|
enum OutputType {
|
|
|
|
OT_NORMAL,
|
|
|
|
OT_DISABLE,
|
|
|
|
OT_BC_ONLY,
|
|
|
|
OT_SAVE_TEMPS
|
|
|
|
};
|
2010-04-16 00:42:57 +00:00
|
|
|
static bool generate_api_file = false;
|
2014-11-24 21:18:14 +00:00
|
|
|
static OutputType TheOutputType = OT_NORMAL;
|
2015-03-19 22:01:00 +00:00
|
|
|
static unsigned OptLevel = 2;
|
2013-08-12 21:07:31 +00:00
|
|
|
static std::string obj_path;
|
2010-06-23 20:20:59 +00:00
|
|
|
static std::string extra_library_path;
|
2010-08-09 21:09:46 +00:00
|
|
|
static std::string triple;
|
2010-08-11 00:15:13 +00:00
|
|
|
static std::string mcpu;
|
2009-10-28 18:55:55 +00:00
|
|
|
// Additional options to pass into the code generator.
|
2010-06-03 17:10:17 +00:00
|
|
|
// Note: This array will contain all plugin options which are not claimed
|
2009-10-28 18:55:55 +00:00
|
|
|
// as plugin exclusive to pass to the code generator.
|
2010-06-03 17:10:17 +00:00
|
|
|
// For example, "generate-api-file" and "as"options are for the plugin
|
2009-10-28 18:55:55 +00:00
|
|
|
// use only and will not be passed.
|
2014-07-29 19:17:44 +00:00
|
|
|
static std::vector<const char *> extra;
|
2009-10-28 18:55:55 +00:00
|
|
|
|
2010-06-07 16:45:22 +00:00
|
|
|
static void process_plugin_option(const char* opt_)
|
2009-10-28 18:55:55 +00:00
|
|
|
{
|
2014-07-29 21:46:05 +00:00
|
|
|
if (opt_ == nullptr)
|
2009-10-28 18:55:55 +00:00
|
|
|
return;
|
2010-06-07 16:45:22 +00:00
|
|
|
llvm::StringRef opt = opt_;
|
2009-10-28 18:55:55 +00:00
|
|
|
|
2010-06-07 16:45:22 +00:00
|
|
|
if (opt == "generate-api-file") {
|
2009-10-28 18:55:55 +00:00
|
|
|
generate_api_file = true;
|
2010-08-11 00:15:13 +00:00
|
|
|
} else if (opt.startswith("mcpu=")) {
|
|
|
|
mcpu = opt.substr(strlen("mcpu="));
|
2010-06-23 20:20:59 +00:00
|
|
|
} else if (opt.startswith("extra-library-path=")) {
|
|
|
|
extra_library_path = opt.substr(strlen("extra_library_path="));
|
2010-08-10 16:32:15 +00:00
|
|
|
} else if (opt.startswith("mtriple=")) {
|
2010-08-09 21:09:46 +00:00
|
|
|
triple = opt.substr(strlen("mtriple="));
|
2013-08-12 21:07:31 +00:00
|
|
|
} else if (opt.startswith("obj-path=")) {
|
|
|
|
obj_path = opt.substr(strlen("obj-path="));
|
2010-06-07 16:45:22 +00:00
|
|
|
} else if (opt == "emit-llvm") {
|
2014-11-24 21:18:14 +00:00
|
|
|
TheOutputType = OT_BC_ONLY;
|
2014-10-29 23:54:45 +00:00
|
|
|
} else if (opt == "save-temps") {
|
2014-11-24 21:18:14 +00:00
|
|
|
TheOutputType = OT_SAVE_TEMPS;
|
|
|
|
} else if (opt == "disable-output") {
|
|
|
|
TheOutputType = OT_DISABLE;
|
2015-03-19 22:01:00 +00:00
|
|
|
} else if (opt.size() == 2 && opt[0] == 'O') {
|
|
|
|
if (opt[1] < '0' || opt[1] > '3')
|
|
|
|
report_fatal_error("Optimization level must be between 0 and 3");
|
|
|
|
OptLevel = opt[1] - '0';
|
2009-10-28 18:55:55 +00:00
|
|
|
} else {
|
|
|
|
// Save this option to pass to the code generator.
|
2014-08-21 20:28:55 +00:00
|
|
|
// ParseCommandLineOptions() expects argv[0] to be program name. Lazily
|
|
|
|
// add that.
|
|
|
|
if (extra.empty())
|
|
|
|
extra.push_back("LLVMgold");
|
|
|
|
|
2014-07-29 19:17:44 +00:00
|
|
|
extra.push_back(opt_);
|
2009-10-28 18:55:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-16 00:42:57 +00:00
|
|
|
static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
|
|
|
|
int *claimed);
|
|
|
|
static ld_plugin_status all_symbols_read_hook(void);
|
|
|
|
static ld_plugin_status cleanup_hook(void);
|
2009-02-03 07:13:24 +00:00
|
|
|
|
|
|
|
extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
|
|
|
|
ld_plugin_status onload(ld_plugin_tv *tv) {
|
2014-07-03 23:28:03 +00:00
|
|
|
InitializeAllTargetInfos();
|
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllAsmParsers();
|
|
|
|
InitializeAllAsmPrinters();
|
|
|
|
|
2009-02-03 07:13:24 +00:00
|
|
|
// We're given a pointer to the first transfer vector. We read through them
|
|
|
|
// until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
|
|
|
|
// contain pointers to functions that we need to call to register our own
|
|
|
|
// hooks. The others are addresses of functions we can use to call into gold
|
|
|
|
// for services.
|
|
|
|
|
|
|
|
bool registeredClaimFile = false;
|
2014-06-19 21:14:13 +00:00
|
|
|
bool RegisteredAllSymbolsRead = false;
|
2009-02-03 07:13:24 +00:00
|
|
|
|
|
|
|
for (; tv->tv_tag != LDPT_NULL; ++tv) {
|
|
|
|
switch (tv->tv_tag) {
|
2010-06-03 21:11:20 +00:00
|
|
|
case LDPT_OUTPUT_NAME:
|
|
|
|
output_name = tv->tv_u.tv_string;
|
|
|
|
break;
|
2009-02-03 07:13:24 +00:00
|
|
|
case LDPT_LINKER_OUTPUT:
|
|
|
|
switch (tv->tv_u.tv_val) {
|
|
|
|
case LDPO_REL: // .o
|
|
|
|
case LDPO_DYN: // .so
|
2014-02-10 20:38:38 +00:00
|
|
|
case LDPO_PIE: // position independent executable
|
2014-08-21 20:28:55 +00:00
|
|
|
RelocationModel = Reloc::PIC_;
|
2009-02-03 07:13:24 +00:00
|
|
|
break;
|
|
|
|
case LDPO_EXEC: // .exe
|
2014-08-21 20:28:55 +00:00
|
|
|
RelocationModel = Reloc::Static;
|
2009-02-03 07:13:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-07-30 00:38:58 +00:00
|
|
|
message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val);
|
2009-02-03 07:13:24 +00:00
|
|
|
return LDPS_ERR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LDPT_OPTION:
|
2009-10-28 18:55:55 +00:00
|
|
|
options::process_plugin_option(tv->tv_u.tv_string);
|
2009-02-03 07:13:24 +00:00
|
|
|
break;
|
|
|
|
case LDPT_REGISTER_CLAIM_FILE_HOOK: {
|
|
|
|
ld_plugin_register_claim_file callback;
|
|
|
|
callback = tv->tv_u.tv_register_claim_file;
|
|
|
|
|
2014-07-30 01:36:32 +00:00
|
|
|
if (callback(claim_file_hook) != LDPS_OK)
|
2009-02-03 07:13:24 +00:00
|
|
|
return LDPS_ERR;
|
|
|
|
|
|
|
|
registeredClaimFile = true;
|
|
|
|
} break;
|
|
|
|
case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
|
|
|
|
ld_plugin_register_all_symbols_read callback;
|
|
|
|
callback = tv->tv_u.tv_register_all_symbols_read;
|
|
|
|
|
2014-07-30 01:36:32 +00:00
|
|
|
if (callback(all_symbols_read_hook) != LDPS_OK)
|
2009-02-03 07:13:24 +00:00
|
|
|
return LDPS_ERR;
|
2011-02-20 18:28:29 +00:00
|
|
|
|
2014-06-19 21:14:13 +00:00
|
|
|
RegisteredAllSymbolsRead = true;
|
2009-02-03 07:13:24 +00:00
|
|
|
} break;
|
|
|
|
case LDPT_REGISTER_CLEANUP_HOOK: {
|
|
|
|
ld_plugin_register_cleanup callback;
|
|
|
|
callback = tv->tv_u.tv_register_cleanup;
|
|
|
|
|
2014-07-30 01:36:32 +00:00
|
|
|
if (callback(cleanup_hook) != LDPS_OK)
|
2009-02-03 07:13:24 +00:00
|
|
|
return LDPS_ERR;
|
|
|
|
} break;
|
2014-08-21 20:28:55 +00:00
|
|
|
case LDPT_GET_INPUT_FILE:
|
|
|
|
get_input_file = tv->tv_u.tv_get_input_file;
|
|
|
|
break;
|
|
|
|
case LDPT_RELEASE_INPUT_FILE:
|
|
|
|
release_input_file = tv->tv_u.tv_release_input_file;
|
|
|
|
break;
|
2009-02-03 07:13:24 +00:00
|
|
|
case LDPT_ADD_SYMBOLS:
|
|
|
|
add_symbols = tv->tv_u.tv_add_symbols;
|
|
|
|
break;
|
2013-10-03 18:29:09 +00:00
|
|
|
case LDPT_GET_SYMBOLS_V2:
|
2009-02-03 07:13:24 +00:00
|
|
|
get_symbols = tv->tv_u.tv_get_symbols;
|
|
|
|
break;
|
|
|
|
case LDPT_ADD_INPUT_FILE:
|
|
|
|
add_input_file = tv->tv_u.tv_add_input_file;
|
|
|
|
break;
|
2010-06-23 20:20:59 +00:00
|
|
|
case LDPT_SET_EXTRA_LIBRARY_PATH:
|
|
|
|
set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
|
|
|
|
break;
|
2011-04-07 21:11:00 +00:00
|
|
|
case LDPT_GET_VIEW:
|
|
|
|
get_view = tv->tv_u.tv_get_view;
|
|
|
|
break;
|
2009-02-03 07:13:24 +00:00
|
|
|
case LDPT_MESSAGE:
|
|
|
|
message = tv->tv_u.tv_message;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-18 08:30:15 +00:00
|
|
|
if (!registeredClaimFile) {
|
2014-07-30 00:38:58 +00:00
|
|
|
message(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
|
2009-02-18 17:49:06 +00:00
|
|
|
return LDPS_ERR;
|
|
|
|
}
|
2009-02-18 08:30:15 +00:00
|
|
|
if (!add_symbols) {
|
2014-07-30 00:38:58 +00:00
|
|
|
message(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
|
2009-02-18 17:49:06 +00:00
|
|
|
return LDPS_ERR;
|
|
|
|
}
|
2009-02-03 07:13:24 +00:00
|
|
|
|
2014-06-19 22:20:07 +00:00
|
|
|
if (!RegisteredAllSymbolsRead)
|
|
|
|
return LDPS_OK;
|
2014-06-19 21:14:13 +00:00
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
if (!get_input_file) {
|
|
|
|
message(LDPL_ERROR, "get_input_file not passed to LLVMgold.");
|
|
|
|
return LDPS_ERR;
|
2014-06-19 22:54:47 +00:00
|
|
|
}
|
2014-08-21 20:28:55 +00:00
|
|
|
if (!release_input_file) {
|
|
|
|
message(LDPL_ERROR, "relesase_input_file not passed to LLVMgold.");
|
|
|
|
return LDPS_ERR;
|
2014-06-26 20:43:27 +00:00
|
|
|
}
|
|
|
|
|
2009-02-03 07:13:24 +00:00
|
|
|
return LDPS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-22 23:26:10 +00:00
|
|
|
static const GlobalObject *getBaseObject(const GlobalValue &GV) {
|
|
|
|
if (auto *GA = dyn_cast<GlobalAlias>(&GV))
|
|
|
|
return GA->getBaseObject();
|
|
|
|
return cast<GlobalObject>(&GV);
|
|
|
|
}
|
|
|
|
|
2014-12-09 16:13:59 +00:00
|
|
|
static bool shouldSkip(uint32_t Symflags) {
|
|
|
|
if (!(Symflags & object::BasicSymbolRef::SF_Global))
|
|
|
|
return true;
|
|
|
|
if (Symflags & object::BasicSymbolRef::SF_FormatSpecific)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-10 00:07:30 +00:00
|
|
|
static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
|
2015-03-02 19:08:03 +00:00
|
|
|
if (const auto *BDI = dyn_cast<BitcodeDiagnosticInfo>(&DI)) {
|
|
|
|
std::error_code EC = BDI->getError();
|
|
|
|
if (EC == BitcodeError::InvalidBitcodeSignature)
|
|
|
|
return;
|
|
|
|
}
|
2015-01-10 00:07:30 +00:00
|
|
|
|
|
|
|
std::string ErrStorage;
|
|
|
|
{
|
|
|
|
raw_string_ostream OS(ErrStorage);
|
|
|
|
DiagnosticPrinterRawOStream DP(OS);
|
|
|
|
DI.print(DP);
|
|
|
|
}
|
2015-03-02 19:08:03 +00:00
|
|
|
ld_plugin_level Level;
|
|
|
|
switch (DI.getSeverity()) {
|
|
|
|
case DS_Error:
|
|
|
|
message(LDPL_FATAL, "LLVM gold plugin has failed to create LTO module: %s",
|
|
|
|
ErrStorage.c_str());
|
|
|
|
llvm_unreachable("Fatal doesn't return.");
|
|
|
|
case DS_Warning:
|
|
|
|
Level = LDPL_WARNING;
|
|
|
|
break;
|
|
|
|
case DS_Note:
|
2015-03-04 18:51:45 +00:00
|
|
|
case DS_Remark:
|
2015-03-02 19:08:03 +00:00
|
|
|
Level = LDPL_INFO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
message(Level, "LLVM gold plugin: %s", ErrStorage.c_str());
|
2015-01-10 00:07:30 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 14:31:22 +00:00
|
|
|
/// Called by gold to see whether this file is one that our plugin can handle.
|
|
|
|
/// We'll try to open it and register all the symbols with add_symbol if
|
|
|
|
/// possible.
|
2010-04-16 00:42:57 +00:00
|
|
|
static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
|
|
|
|
int *claimed) {
|
2014-08-21 20:28:55 +00:00
|
|
|
LLVMContext Context;
|
2014-08-27 20:25:55 +00:00
|
|
|
MemoryBufferRef BufferRef;
|
|
|
|
std::unique_ptr<MemoryBuffer> Buffer;
|
2011-04-07 21:11:00 +00:00
|
|
|
if (get_view) {
|
2014-08-21 20:28:55 +00:00
|
|
|
const void *view;
|
2011-04-07 21:11:00 +00:00
|
|
|
if (get_view(file->handle, &view) != LDPS_OK) {
|
2014-07-30 00:38:58 +00:00
|
|
|
message(LDPL_ERROR, "Failed to get a view of %s", file->name);
|
2011-04-07 21:11:00 +00:00
|
|
|
return LDPS_ERR;
|
|
|
|
}
|
2014-10-22 02:23:31 +00:00
|
|
|
BufferRef = MemoryBufferRef(StringRef((const char *)view, file->filesize), "");
|
2011-09-12 21:47:50 +00:00
|
|
|
} else {
|
2011-09-15 23:13:00 +00:00
|
|
|
int64_t offset = 0;
|
2009-02-05 04:14:23 +00:00
|
|
|
// Gold has found what might be IR part-way inside of a file, such as
|
|
|
|
// an .a archive.
|
2011-09-12 21:47:50 +00:00
|
|
|
if (file->offset) {
|
|
|
|
offset = file->offset;
|
|
|
|
}
|
2014-07-06 17:43:13 +00:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
|
|
|
|
MemoryBuffer::getOpenFileSlice(file->fd, file->name, file->filesize,
|
|
|
|
offset);
|
|
|
|
if (std::error_code EC = BufferOrErr.getError()) {
|
2014-07-30 00:38:58 +00:00
|
|
|
message(LDPL_ERROR, EC.message().c_str());
|
2011-09-12 21:47:50 +00:00
|
|
|
return LDPS_ERR;
|
|
|
|
}
|
2014-08-27 20:25:55 +00:00
|
|
|
Buffer = std::move(BufferOrErr.get());
|
|
|
|
BufferRef = Buffer->getMemBufferRef();
|
2011-02-08 22:40:47 +00:00
|
|
|
}
|
2011-09-12 21:47:50 +00:00
|
|
|
|
2015-01-10 00:07:30 +00:00
|
|
|
Context.setDiagnosticHandler(diagnosticHandler);
|
2014-09-03 17:59:23 +00:00
|
|
|
ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
|
2014-12-09 20:36:13 +00:00
|
|
|
object::IRObjectFile::create(BufferRef, Context);
|
2014-08-21 20:28:55 +00:00
|
|
|
std::error_code EC = ObjOrErr.getError();
|
2015-01-10 00:07:30 +00:00
|
|
|
if (EC == object::object_error::invalid_file_type ||
|
2014-09-18 21:28:49 +00:00
|
|
|
EC == object::object_error::bitcode_section_not_found)
|
2011-09-12 21:47:50 +00:00
|
|
|
return LDPS_OK;
|
|
|
|
|
2014-07-29 20:46:19 +00:00
|
|
|
*claimed = 1;
|
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
if (EC) {
|
2014-07-30 00:38:58 +00:00
|
|
|
message(LDPL_ERROR, "LLVM gold plugin has failed to create LTO module: %s",
|
2014-08-21 20:28:55 +00:00
|
|
|
EC.message().c_str());
|
2014-07-29 20:46:19 +00:00
|
|
|
return LDPS_ERR;
|
2011-09-09 00:14:04 +00:00
|
|
|
}
|
2014-09-03 17:59:23 +00:00
|
|
|
std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr);
|
2009-02-03 07:13:24 +00:00
|
|
|
|
|
|
|
Modules.resize(Modules.size() + 1);
|
|
|
|
claimed_file &cf = Modules.back();
|
2010-08-09 21:09:46 +00:00
|
|
|
|
2009-02-03 07:13:24 +00:00
|
|
|
cf.handle = file->handle;
|
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
for (auto &Sym : Obj->symbols()) {
|
|
|
|
uint32_t Symflags = Sym.getFlags();
|
2014-12-09 16:13:59 +00:00
|
|
|
if (shouldSkip(Symflags))
|
2009-02-03 07:13:24 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
cf.syms.push_back(ld_plugin_symbol());
|
|
|
|
ld_plugin_symbol &sym = cf.syms.back();
|
2014-07-29 21:46:05 +00:00
|
|
|
sym.version = nullptr;
|
2009-02-03 07:13:24 +00:00
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
SmallString<64> Name;
|
|
|
|
{
|
|
|
|
raw_svector_ostream OS(Name);
|
|
|
|
Sym.printName(OS);
|
|
|
|
}
|
|
|
|
sym.name = strdup(Name.c_str());
|
|
|
|
|
|
|
|
const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
|
|
|
|
|
|
|
|
sym.visibility = LDPV_DEFAULT;
|
|
|
|
if (GV) {
|
|
|
|
switch (GV->getVisibility()) {
|
|
|
|
case GlobalValue::DefaultVisibility:
|
|
|
|
sym.visibility = LDPV_DEFAULT;
|
|
|
|
break;
|
|
|
|
case GlobalValue::HiddenVisibility:
|
2009-02-03 07:13:24 +00:00
|
|
|
sym.visibility = LDPV_HIDDEN;
|
|
|
|
break;
|
2014-08-21 20:28:55 +00:00
|
|
|
case GlobalValue::ProtectedVisibility:
|
2009-02-03 07:13:24 +00:00
|
|
|
sym.visibility = LDPV_PROTECTED;
|
|
|
|
break;
|
2014-08-21 20:28:55 +00:00
|
|
|
}
|
2009-02-03 07:13:24 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
if (Symflags & object::BasicSymbolRef::SF_Undefined) {
|
|
|
|
sym.def = LDPK_UNDEF;
|
|
|
|
if (GV && GV->hasExternalWeakLinkage())
|
2009-04-24 16:55:21 +00:00
|
|
|
sym.def = LDPK_WEAKUNDEF;
|
2014-08-21 20:28:55 +00:00
|
|
|
} else {
|
|
|
|
sym.def = LDPK_DEF;
|
|
|
|
if (GV) {
|
|
|
|
assert(!GV->hasExternalWeakLinkage() &&
|
|
|
|
!GV->hasAvailableExternallyLinkage() && "Not a declaration!");
|
|
|
|
if (GV->hasCommonLinkage())
|
|
|
|
sym.def = LDPK_COMMON;
|
|
|
|
else if (GV->isWeakForLinker())
|
|
|
|
sym.def = LDPK_WEAKDEF;
|
|
|
|
}
|
2009-02-03 07:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sym.size = 0;
|
2014-08-21 20:28:55 +00:00
|
|
|
sym.comdat_key = nullptr;
|
2014-08-22 23:26:10 +00:00
|
|
|
if (GV) {
|
|
|
|
const GlobalObject *Base = getBaseObject(*GV);
|
|
|
|
if (!Base)
|
|
|
|
message(LDPL_FATAL, "Unable to determine comdat of alias!");
|
|
|
|
const Comdat *C = Base->getComdat();
|
|
|
|
if (C)
|
|
|
|
sym.comdat_key = strdup(C->getName().str().c_str());
|
|
|
|
else if (Base->hasWeakLinkage() || Base->hasLinkOnceLinkage())
|
|
|
|
sym.comdat_key = strdup(sym.name);
|
|
|
|
}
|
2009-02-03 07:13:24 +00:00
|
|
|
|
|
|
|
sym.resolution = LDPR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cf.syms.empty()) {
|
2014-07-30 00:38:58 +00:00
|
|
|
if (add_symbols(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
|
|
|
|
message(LDPL_ERROR, "Unable to add symbols!");
|
2009-02-03 07:13:24 +00:00
|
|
|
return LDPS_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
return LDPS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-22 23:26:10 +00:00
|
|
|
static void keepGlobalValue(GlobalValue &GV,
|
|
|
|
std::vector<GlobalAlias *> &KeptAliases) {
|
2014-08-21 20:28:55 +00:00
|
|
|
assert(!GV.hasLocalLinkage());
|
|
|
|
|
2014-08-22 23:26:10 +00:00
|
|
|
if (auto *GA = dyn_cast<GlobalAlias>(&GV))
|
|
|
|
KeptAliases.push_back(GA);
|
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
switch (GV.getLinkage()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case GlobalValue::LinkOnceAnyLinkage:
|
|
|
|
GV.setLinkage(GlobalValue::WeakAnyLinkage);
|
|
|
|
break;
|
|
|
|
case GlobalValue::LinkOnceODRLinkage:
|
|
|
|
GV.setLinkage(GlobalValue::WeakODRLinkage);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!GV.isDiscardableIfUnused());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void internalize(GlobalValue &GV) {
|
2014-10-24 18:13:04 +00:00
|
|
|
if (GV.isDeclarationForLinker())
|
2014-08-21 20:28:55 +00:00
|
|
|
return; // We get here if there is a matching asm definition.
|
|
|
|
if (!GV.hasLocalLinkage())
|
|
|
|
GV.setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void drop(GlobalValue &GV) {
|
|
|
|
if (auto *F = dyn_cast<Function>(&GV)) {
|
|
|
|
F->deleteBody();
|
2014-08-22 23:26:10 +00:00
|
|
|
F->setComdat(nullptr); // Should deleteBody do this?
|
2014-08-21 20:28:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto *Var = dyn_cast<GlobalVariable>(&GV)) {
|
|
|
|
Var->setInitializer(nullptr);
|
2014-08-22 23:26:10 +00:00
|
|
|
Var->setLinkage(
|
|
|
|
GlobalValue::ExternalLinkage); // Should setInitializer do this?
|
|
|
|
Var->setComdat(nullptr); // and this?
|
2014-08-21 20:28:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &Alias = cast<GlobalAlias>(GV);
|
|
|
|
Module &M = *Alias.getParent();
|
|
|
|
PointerType &Ty = *cast<PointerType>(Alias.getType());
|
|
|
|
GlobalValue::LinkageTypes L = Alias.getLinkage();
|
|
|
|
auto *Var =
|
|
|
|
new GlobalVariable(M, Ty.getElementType(), /*isConstant*/ false, L,
|
|
|
|
/*Initializer*/ nullptr);
|
|
|
|
Var->takeName(&Alias);
|
|
|
|
Alias.replaceAllUsesWith(Var);
|
2014-09-10 19:39:41 +00:00
|
|
|
Alias.eraseFromParent();
|
2014-08-21 20:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *getResolutionName(ld_plugin_symbol_resolution R) {
|
|
|
|
switch (R) {
|
|
|
|
case LDPR_UNKNOWN:
|
|
|
|
return "UNKNOWN";
|
|
|
|
case LDPR_UNDEF:
|
|
|
|
return "UNDEF";
|
|
|
|
case LDPR_PREVAILING_DEF:
|
|
|
|
return "PREVAILING_DEF";
|
|
|
|
case LDPR_PREVAILING_DEF_IRONLY:
|
|
|
|
return "PREVAILING_DEF_IRONLY";
|
|
|
|
case LDPR_PREEMPTED_REG:
|
|
|
|
return "PREEMPTED_REG";
|
|
|
|
case LDPR_PREEMPTED_IR:
|
|
|
|
return "PREEMPTED_IR";
|
|
|
|
case LDPR_RESOLVED_IR:
|
|
|
|
return "RESOLVED_IR";
|
|
|
|
case LDPR_RESOLVED_EXEC:
|
|
|
|
return "RESOLVED_EXEC";
|
|
|
|
case LDPR_RESOLVED_DYN:
|
|
|
|
return "RESOLVED_DYN";
|
|
|
|
case LDPR_PREVAILING_DEF_IRONLY_EXP:
|
|
|
|
return "PREVAILING_DEF_IRONLY_EXP";
|
|
|
|
}
|
2014-10-10 00:48:13 +00:00
|
|
|
llvm_unreachable("Unknown resolution");
|
2014-08-21 20:28:55 +00:00
|
|
|
}
|
|
|
|
|
2014-08-22 23:26:10 +00:00
|
|
|
namespace {
|
|
|
|
class LocalValueMaterializer : public ValueMaterializer {
|
|
|
|
DenseSet<GlobalValue *> &Dropped;
|
2014-12-10 00:09:35 +00:00
|
|
|
DenseMap<GlobalObject *, GlobalObject *> LocalVersions;
|
2014-08-22 23:26:10 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
LocalValueMaterializer(DenseSet<GlobalValue *> &Dropped) : Dropped(Dropped) {}
|
|
|
|
Value *materializeValueFor(Value *V) override;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *LocalValueMaterializer::materializeValueFor(Value *V) {
|
2014-12-10 00:09:35 +00:00
|
|
|
auto *GO = dyn_cast<GlobalObject>(V);
|
|
|
|
if (!GO)
|
2014-08-22 23:26:10 +00:00
|
|
|
return nullptr;
|
2014-12-10 00:09:35 +00:00
|
|
|
|
|
|
|
auto I = LocalVersions.find(GO);
|
|
|
|
if (I != LocalVersions.end())
|
|
|
|
return I->second;
|
|
|
|
|
|
|
|
if (!Dropped.count(GO))
|
2014-08-22 23:26:10 +00:00
|
|
|
return nullptr;
|
2014-12-10 00:09:35 +00:00
|
|
|
|
|
|
|
Module &M = *GO->getParent();
|
|
|
|
GlobalValue::LinkageTypes L = GO->getLinkage();
|
|
|
|
GlobalObject *Declaration;
|
|
|
|
if (auto *F = dyn_cast<Function>(GO)) {
|
|
|
|
Declaration = Function::Create(F->getFunctionType(), L, "", &M);
|
|
|
|
} else {
|
|
|
|
auto *Var = cast<GlobalVariable>(GO);
|
|
|
|
Declaration = new GlobalVariable(M, Var->getType()->getElementType(),
|
|
|
|
Var->isConstant(), L,
|
|
|
|
/*Initializer*/ nullptr);
|
|
|
|
}
|
|
|
|
Declaration->takeName(GO);
|
|
|
|
Declaration->copyAttributesFrom(GO);
|
|
|
|
|
|
|
|
GO->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
GO->setName(Declaration->getName());
|
|
|
|
Dropped.erase(GO);
|
|
|
|
GO->replaceAllUsesWith(Declaration);
|
|
|
|
|
|
|
|
LocalVersions[Declaration] = GO;
|
|
|
|
|
|
|
|
return GO;
|
2014-08-22 23:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Constant *mapConstantToLocalCopy(Constant *C, ValueToValueMapTy &VM,
|
|
|
|
LocalValueMaterializer *Materializer) {
|
|
|
|
return MapValue(C, VM, RF_IgnoreMissingEntries, nullptr, Materializer);
|
|
|
|
}
|
|
|
|
|
2014-12-23 18:18:37 +00:00
|
|
|
static void freeSymName(ld_plugin_symbol &Sym) {
|
|
|
|
free(Sym.name);
|
|
|
|
free(Sym.comdat_key);
|
|
|
|
Sym.name = nullptr;
|
|
|
|
Sym.comdat_key = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
static std::unique_ptr<Module>
|
2015-02-11 16:12:50 +00:00
|
|
|
getModuleForFile(LLVMContext &Context, claimed_file &F,
|
2015-03-02 19:08:03 +00:00
|
|
|
ld_plugin_input_file &Info, raw_fd_ostream *ApiFile,
|
2014-08-21 20:28:55 +00:00
|
|
|
StringSet<> &Internalize, StringSet<> &Maybe) {
|
|
|
|
|
|
|
|
if (get_symbols(F.handle, F.syms.size(), &F.syms[0]) != LDPS_OK)
|
|
|
|
message(LDPL_FATAL, "Failed to get symbol information");
|
|
|
|
|
|
|
|
const void *View;
|
|
|
|
if (get_view(F.handle, &View) != LDPS_OK)
|
|
|
|
message(LDPL_FATAL, "Failed to get a view of file");
|
|
|
|
|
2015-03-02 19:08:03 +00:00
|
|
|
MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize),
|
|
|
|
Info.name);
|
2014-12-09 16:13:59 +00:00
|
|
|
ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
|
2014-12-09 20:36:13 +00:00
|
|
|
object::IRObjectFile::create(BufferRef, Context);
|
2014-12-09 16:13:59 +00:00
|
|
|
|
|
|
|
if (std::error_code EC = ObjOrErr.getError())
|
2014-09-18 21:28:49 +00:00
|
|
|
message(LDPL_FATAL, "Could not read bitcode from file : %s",
|
|
|
|
EC.message().c_str());
|
|
|
|
|
2014-12-09 16:13:59 +00:00
|
|
|
object::IRObjectFile &Obj = **ObjOrErr;
|
2014-08-21 20:28:55 +00:00
|
|
|
|
2014-12-09 16:13:59 +00:00
|
|
|
Module &M = Obj.getModule();
|
2014-08-21 20:28:55 +00:00
|
|
|
|
2015-03-30 21:36:43 +00:00
|
|
|
M.materializeMetadata();
|
|
|
|
UpgradeDebugInfo(M);
|
2015-03-02 19:08:03 +00:00
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
SmallPtrSet<GlobalValue *, 8> Used;
|
2014-12-09 16:13:59 +00:00
|
|
|
collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
|
2014-08-21 20:28:55 +00:00
|
|
|
|
2014-08-22 23:26:10 +00:00
|
|
|
DenseSet<GlobalValue *> Drop;
|
|
|
|
std::vector<GlobalAlias *> KeptAliases;
|
2014-12-09 16:13:59 +00:00
|
|
|
|
|
|
|
unsigned SymNum = 0;
|
|
|
|
for (auto &ObjSym : Obj.symbols()) {
|
|
|
|
if (shouldSkip(ObjSym.getFlags()))
|
|
|
|
continue;
|
|
|
|
ld_plugin_symbol &Sym = F.syms[SymNum];
|
|
|
|
++SymNum;
|
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
ld_plugin_symbol_resolution Resolution =
|
|
|
|
(ld_plugin_symbol_resolution)Sym.resolution;
|
|
|
|
|
|
|
|
if (options::generate_api_file)
|
|
|
|
*ApiFile << Sym.name << ' ' << getResolutionName(Resolution) << '\n';
|
|
|
|
|
2014-12-09 16:13:59 +00:00
|
|
|
GlobalValue *GV = Obj.getSymbolGV(ObjSym.getRawDataRefImpl());
|
2014-12-23 18:18:37 +00:00
|
|
|
if (!GV) {
|
|
|
|
freeSymName(Sym);
|
2014-08-21 20:28:55 +00:00
|
|
|
continue; // Asm symbol.
|
2014-12-23 18:18:37 +00:00
|
|
|
}
|
2014-08-21 20:28:55 +00:00
|
|
|
|
2014-09-17 20:41:13 +00:00
|
|
|
if (Resolution != LDPR_PREVAILING_DEF_IRONLY && GV->hasCommonLinkage()) {
|
2014-09-09 20:08:22 +00:00
|
|
|
// Common linkage is special. There is no single symbol that wins the
|
|
|
|
// resolution. Instead we have to collect the maximum alignment and size.
|
|
|
|
// The IR linker does that for us if we just pass it every common GV.
|
2014-09-17 20:41:13 +00:00
|
|
|
// We still have to keep track of LDPR_PREVAILING_DEF_IRONLY so we
|
|
|
|
// internalize once the IR linker has done its job.
|
2014-12-23 18:18:37 +00:00
|
|
|
freeSymName(Sym);
|
2014-09-09 20:08:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
switch (Resolution) {
|
|
|
|
case LDPR_UNKNOWN:
|
|
|
|
llvm_unreachable("Unexpected resolution");
|
|
|
|
|
|
|
|
case LDPR_RESOLVED_IR:
|
|
|
|
case LDPR_RESOLVED_EXEC:
|
|
|
|
case LDPR_RESOLVED_DYN:
|
2014-10-24 18:13:04 +00:00
|
|
|
assert(GV->isDeclarationForLinker());
|
2014-08-21 20:28:55 +00:00
|
|
|
break;
|
|
|
|
|
2015-01-14 13:53:50 +00:00
|
|
|
case LDPR_UNDEF:
|
2015-01-14 20:08:46 +00:00
|
|
|
if (!GV->isDeclarationForLinker()) {
|
2015-01-14 19:43:32 +00:00
|
|
|
assert(GV->hasComdat());
|
|
|
|
Drop.insert(GV);
|
|
|
|
}
|
2015-01-14 13:53:50 +00:00
|
|
|
break;
|
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
case LDPR_PREVAILING_DEF_IRONLY: {
|
2014-08-22 23:26:10 +00:00
|
|
|
keepGlobalValue(*GV, KeptAliases);
|
2014-08-21 20:28:55 +00:00
|
|
|
if (!Used.count(GV)) {
|
|
|
|
// Since we use the regular lib/Linker, we cannot just internalize GV
|
|
|
|
// now or it will not be copied to the merged module. Instead we force
|
|
|
|
// it to be copied and then internalize it.
|
2014-12-09 16:50:57 +00:00
|
|
|
Internalize.insert(GV->getName());
|
2014-08-21 20:28:55 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-10-18 19:32:06 +00:00
|
|
|
}
|
2014-08-21 20:28:55 +00:00
|
|
|
|
|
|
|
case LDPR_PREVAILING_DEF:
|
2014-08-22 23:26:10 +00:00
|
|
|
keepGlobalValue(*GV, KeptAliases);
|
2014-08-21 20:28:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LDPR_PREEMPTED_IR:
|
2014-10-07 04:06:13 +00:00
|
|
|
// Gold might have selected a linkonce_odr and preempted a weak_odr.
|
|
|
|
// In that case we have to make sure we don't end up internalizing it.
|
|
|
|
if (!GV->isDiscardableIfUnused())
|
2014-12-09 16:50:57 +00:00
|
|
|
Maybe.erase(GV->getName());
|
2014-10-07 04:06:13 +00:00
|
|
|
|
|
|
|
// fall-through
|
|
|
|
case LDPR_PREEMPTED_REG:
|
2014-08-22 23:26:10 +00:00
|
|
|
Drop.insert(GV);
|
2014-08-21 20:28:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LDPR_PREVAILING_DEF_IRONLY_EXP: {
|
|
|
|
// We can only check for address uses after we merge the modules. The
|
|
|
|
// reason is that this GV might have a copy in another module
|
|
|
|
// and in that module the address might be significant, but that
|
|
|
|
// copy will be LDPR_PREEMPTED_IR.
|
|
|
|
if (GV->hasLinkOnceODRLinkage())
|
2014-12-09 16:50:57 +00:00
|
|
|
Maybe.insert(GV->getName());
|
2014-08-22 23:26:10 +00:00
|
|
|
keepGlobalValue(*GV, KeptAliases);
|
2014-08-21 20:28:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 18:18:37 +00:00
|
|
|
freeSymName(Sym);
|
2013-10-18 19:32:06 +00:00
|
|
|
}
|
2011-02-20 18:28:29 +00:00
|
|
|
|
2014-08-22 23:26:10 +00:00
|
|
|
ValueToValueMapTy VM;
|
|
|
|
LocalValueMaterializer Materializer(Drop);
|
|
|
|
for (GlobalAlias *GA : KeptAliases) {
|
|
|
|
// Gold told us to keep GA. It is possible that a GV usied in the aliasee
|
|
|
|
// expression is being dropped. If that is the case, that GV must be copied.
|
|
|
|
Constant *Aliasee = GA->getAliasee();
|
|
|
|
Constant *Replacement = mapConstantToLocalCopy(Aliasee, VM, &Materializer);
|
2014-12-10 00:09:35 +00:00
|
|
|
GA->setAliasee(Replacement);
|
2014-08-21 20:28:55 +00:00
|
|
|
}
|
2011-02-20 18:28:29 +00:00
|
|
|
|
2014-08-22 23:26:10 +00:00
|
|
|
for (auto *GV : Drop)
|
|
|
|
drop(*GV);
|
|
|
|
|
2014-12-09 16:13:59 +00:00
|
|
|
return Obj.takeModule();
|
2009-02-03 07:13:24 +00:00
|
|
|
}
|
|
|
|
|
2015-02-02 05:47:30 +00:00
|
|
|
static void runLTOPasses(Module &M, TargetMachine &TM) {
|
2015-02-21 00:13:15 +00:00
|
|
|
if (const DataLayout *DL = TM.getDataLayout())
|
2015-03-04 19:15:29 +00:00
|
|
|
M.setDataLayout(*DL);
|
2015-02-21 00:13:15 +00:00
|
|
|
|
2015-02-13 10:01:29 +00:00
|
|
|
legacy::PassManager passes;
|
2015-02-02 05:47:30 +00:00
|
|
|
passes.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
|
|
|
|
|
2015-02-13 21:10:58 +00:00
|
|
|
PassManagerBuilder PMB;
|
2015-01-24 13:59:08 +00:00
|
|
|
PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM.getTargetTriple()));
|
2014-08-21 20:28:55 +00:00
|
|
|
PMB.Inliner = createFunctionInliningPass();
|
|
|
|
PMB.VerifyInput = true;
|
|
|
|
PMB.VerifyOutput = true;
|
2014-10-30 00:11:24 +00:00
|
|
|
PMB.LoopVectorize = true;
|
2014-10-30 00:38:54 +00:00
|
|
|
PMB.SLPVectorize = true;
|
2015-03-19 22:01:00 +00:00
|
|
|
PMB.OptLevel = options::OptLevel;
|
2015-01-30 19:14:04 +00:00
|
|
|
PMB.populateLTOPassManager(passes);
|
2014-08-21 20:28:55 +00:00
|
|
|
passes.run(M);
|
|
|
|
}
|
|
|
|
|
2015-04-15 00:13:51 +00:00
|
|
|
static void saveBCFile(StringRef Path, Module &M) {
|
2014-10-29 23:54:45 +00:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
|
|
|
|
if (EC)
|
|
|
|
message(LDPL_FATAL, "Failed to write the output file.");
|
2015-04-15 00:10:50 +00:00
|
|
|
WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
|
2014-10-29 23:54:45 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
static void codegen(Module &M) {
|
|
|
|
const std::string &TripleStr = M.getTargetTriple();
|
|
|
|
Triple TheTriple(TripleStr);
|
|
|
|
|
|
|
|
std::string ErrMsg;
|
|
|
|
const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
|
|
|
|
if (!TheTarget)
|
|
|
|
message(LDPL_FATAL, "Target not found: %s", ErrMsg.c_str());
|
|
|
|
|
|
|
|
if (unsigned NumOpts = options::extra.size())
|
|
|
|
cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
|
|
|
|
|
|
|
|
SubtargetFeatures Features;
|
|
|
|
Features.getDefaultSubtargetFeatures(TheTriple);
|
|
|
|
for (const std::string &A : MAttrs)
|
|
|
|
Features.AddFeature(A);
|
|
|
|
|
|
|
|
TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
|
2015-03-19 22:01:00 +00:00
|
|
|
CodeGenOpt::Level CGOptLevel;
|
|
|
|
switch (options::OptLevel) {
|
|
|
|
case 0:
|
|
|
|
CGOptLevel = CodeGenOpt::None;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
CGOptLevel = CodeGenOpt::Less;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
CGOptLevel = CodeGenOpt::Default;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
CGOptLevel = CodeGenOpt::Aggressive;
|
|
|
|
break;
|
|
|
|
}
|
2014-08-21 20:28:55 +00:00
|
|
|
std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
|
|
|
|
TripleStr, options::mcpu, Features.getString(), Options, RelocationModel,
|
2015-03-19 22:01:00 +00:00
|
|
|
CodeModel::Default, CGOptLevel));
|
2014-08-21 20:28:55 +00:00
|
|
|
|
|
|
|
runLTOPasses(M, *TM);
|
|
|
|
|
2014-11-24 21:18:14 +00:00
|
|
|
if (options::TheOutputType == options::OT_SAVE_TEMPS)
|
2014-10-29 23:54:45 +00:00
|
|
|
saveBCFile(output_name + ".opt.bc", M);
|
|
|
|
|
2015-02-13 10:01:29 +00:00
|
|
|
legacy::PassManager CodeGenPasses;
|
2014-08-21 20:28:55 +00:00
|
|
|
|
|
|
|
SmallString<128> Filename;
|
|
|
|
int FD;
|
|
|
|
if (options::obj_path.empty()) {
|
|
|
|
std::error_code EC =
|
|
|
|
sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
|
|
|
|
if (EC)
|
2014-11-19 22:39:21 +00:00
|
|
|
message(LDPL_FATAL, "Could not create temporary file: %s",
|
2014-08-21 20:28:55 +00:00
|
|
|
EC.message().c_str());
|
|
|
|
} else {
|
|
|
|
Filename = options::obj_path;
|
|
|
|
std::error_code EC =
|
|
|
|
sys::fs::openFileForWrite(Filename.c_str(), FD, sys::fs::F_None);
|
|
|
|
if (EC)
|
|
|
|
message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
raw_fd_ostream OS(FD, true);
|
|
|
|
|
2015-04-09 21:06:08 +00:00
|
|
|
if (TM->addPassesToEmitFile(CodeGenPasses, OS,
|
2014-08-21 20:28:55 +00:00
|
|
|
TargetMachine::CGFT_ObjectFile))
|
|
|
|
message(LDPL_FATAL, "Failed to setup codegen");
|
|
|
|
CodeGenPasses.run(M);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (add_input_file(Filename.c_str()) != LDPS_OK)
|
|
|
|
message(LDPL_FATAL,
|
|
|
|
"Unable to add .o file to the link. File left behind in: %s",
|
|
|
|
Filename.c_str());
|
|
|
|
|
|
|
|
if (options::obj_path.empty())
|
|
|
|
Cleanup.push_back(Filename.c_str());
|
2013-10-31 20:51:58 +00:00
|
|
|
}
|
|
|
|
|
2014-07-30 01:23:45 +00:00
|
|
|
/// gold informs us that all symbols have been read. At this point, we use
|
|
|
|
/// get_symbols to see if any of our definitions have been overridden by a
|
|
|
|
/// native object file. Then, perform optimization and codegen.
|
2014-08-21 20:28:55 +00:00
|
|
|
static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
|
|
|
|
if (Modules.empty())
|
|
|
|
return LDPS_OK;
|
2011-02-20 18:28:29 +00:00
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
LLVMContext Context;
|
2015-03-04 18:51:45 +00:00
|
|
|
Context.setDiagnosticHandler(diagnosticHandler, nullptr, true);
|
2015-03-02 19:08:03 +00:00
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
std::unique_ptr<Module> Combined(new Module("ld-temp.o", Context));
|
|
|
|
Linker L(Combined.get());
|
2010-06-03 14:45:44 +00:00
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
std::string DefaultTriple = sys::getDefaultTargetTriple();
|
|
|
|
|
|
|
|
StringSet<> Internalize;
|
|
|
|
StringSet<> Maybe;
|
|
|
|
for (claimed_file &F : Modules) {
|
2015-02-11 16:12:50 +00:00
|
|
|
ld_plugin_input_file File;
|
|
|
|
if (get_input_file(F.handle, &File) != LDPS_OK)
|
|
|
|
message(LDPL_FATAL, "Failed to get file information");
|
2014-08-21 20:28:55 +00:00
|
|
|
std::unique_ptr<Module> M =
|
2015-03-02 19:08:03 +00:00
|
|
|
getModuleForFile(Context, F, File, ApiFile, Internalize, Maybe);
|
2014-08-21 20:28:55 +00:00
|
|
|
if (!options::triple.empty())
|
|
|
|
M->setTargetTriple(options::triple.c_str());
|
|
|
|
else if (M->getTargetTriple().empty()) {
|
|
|
|
M->setTargetTriple(DefaultTriple);
|
2009-02-03 07:13:24 +00:00
|
|
|
}
|
2014-08-21 20:28:55 +00:00
|
|
|
|
2014-10-25 04:06:10 +00:00
|
|
|
if (L.linkInModule(M.get()))
|
|
|
|
message(LDPL_FATAL, "Failed to link module");
|
2015-02-11 16:12:50 +00:00
|
|
|
if (release_input_file(F.handle) != LDPS_OK)
|
|
|
|
message(LDPL_FATAL, "Failed to release file information");
|
2014-08-21 20:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &Name : Internalize) {
|
|
|
|
GlobalValue *GV = Combined->getNamedValue(Name.first());
|
|
|
|
if (GV)
|
|
|
|
internalize(*GV);
|
2010-06-14 21:20:52 +00:00
|
|
|
}
|
2009-02-03 07:13:24 +00:00
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
for (const auto &Name : Maybe) {
|
|
|
|
GlobalValue *GV = Combined->getNamedValue(Name.first());
|
|
|
|
if (!GV)
|
|
|
|
continue;
|
|
|
|
GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
|
|
|
|
if (canBeOmittedFromSymbolTable(GV))
|
|
|
|
internalize(*GV);
|
|
|
|
}
|
2010-08-11 00:15:13 +00:00
|
|
|
|
2014-11-24 21:18:14 +00:00
|
|
|
if (options::TheOutputType == options::OT_DISABLE)
|
|
|
|
return LDPS_OK;
|
|
|
|
|
|
|
|
if (options::TheOutputType != options::OT_NORMAL) {
|
2010-06-03 21:11:20 +00:00
|
|
|
std::string path;
|
2014-11-24 21:18:14 +00:00
|
|
|
if (options::TheOutputType == options::OT_BC_ONLY)
|
2010-06-03 21:11:20 +00:00
|
|
|
path = output_name;
|
|
|
|
else
|
|
|
|
path = output_name + ".bc";
|
2014-10-29 23:54:45 +00:00
|
|
|
saveBCFile(path, *L.getModule());
|
2014-11-24 21:18:14 +00:00
|
|
|
if (options::TheOutputType == options::OT_BC_ONLY)
|
2014-08-11 19:06:54 +00:00
|
|
|
return LDPS_OK;
|
2010-05-13 13:39:31 +00:00
|
|
|
}
|
2013-10-16 12:47:04 +00:00
|
|
|
|
2014-08-21 20:28:55 +00:00
|
|
|
codegen(*L.getModule());
|
2009-02-03 07:13:24 +00:00
|
|
|
|
2010-06-23 20:20:59 +00:00
|
|
|
if (!options::extra_library_path.empty() &&
|
2014-08-21 20:28:55 +00:00
|
|
|
set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK)
|
|
|
|
message(LDPL_FATAL, "Unable to set the extra library path.");
|
2013-08-12 21:07:31 +00:00
|
|
|
|
2009-02-03 07:13:24 +00:00
|
|
|
return LDPS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-11 19:06:54 +00:00
|
|
|
static ld_plugin_status all_symbols_read_hook(void) {
|
|
|
|
ld_plugin_status Ret;
|
|
|
|
if (!options::generate_api_file) {
|
|
|
|
Ret = allSymbolsReadHook(nullptr);
|
|
|
|
} else {
|
2014-08-25 18:16:47 +00:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream ApiFile("apifile.txt", EC, sys::fs::F_None);
|
|
|
|
if (EC)
|
2014-08-11 19:06:54 +00:00
|
|
|
message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s",
|
2014-08-25 18:16:47 +00:00
|
|
|
EC.message().c_str());
|
2014-08-21 20:28:55 +00:00
|
|
|
Ret = allSymbolsReadHook(&ApiFile);
|
2014-08-11 19:06:54 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 20:52:49 +00:00
|
|
|
llvm_shutdown();
|
|
|
|
|
2014-11-24 21:18:14 +00:00
|
|
|
if (options::TheOutputType == options::OT_BC_ONLY ||
|
2015-02-12 18:21:50 +00:00
|
|
|
options::TheOutputType == options::OT_DISABLE) {
|
|
|
|
if (options::TheOutputType == options::OT_DISABLE)
|
|
|
|
// Remove the output file here since ld.bfd creates the output file
|
|
|
|
// early.
|
|
|
|
sys::fs::remove(output_name);
|
2014-08-11 19:06:54 +00:00
|
|
|
exit(0);
|
2015-02-12 18:21:50 +00:00
|
|
|
}
|
2014-08-11 19:06:54 +00:00
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2010-04-16 00:42:57 +00:00
|
|
|
static ld_plugin_status cleanup_hook(void) {
|
2014-07-30 01:52:40 +00:00
|
|
|
for (std::string &Name : Cleanup) {
|
|
|
|
std::error_code EC = sys::fs::remove(Name);
|
2013-06-17 18:38:18 +00:00
|
|
|
if (EC)
|
2014-07-30 01:52:40 +00:00
|
|
|
message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(),
|
2014-07-30 00:38:58 +00:00
|
|
|
EC.message().c_str());
|
2013-06-17 18:38:18 +00:00
|
|
|
}
|
2009-02-03 07:13:24 +00:00
|
|
|
|
|
|
|
return LDPS_OK;
|
|
|
|
}
|