//===----------------------------------------------------------------------===//
#include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
+#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/CodeGen/Analysis.h"
#include "llvm/CodeGen/CommandFlags.h"
+#include "llvm/IR/Constants.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/Transforms/Utils/GlobalStatus.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
+#include "llvm/Transforms/Utils/ValueMapper.h"
#include <list>
#include <plugin-api.h>
#include <system_error>
return LDPS_OK;
}
+static const GlobalObject *getBaseObject(const GlobalValue &GV) {
+ if (auto *GA = dyn_cast<GlobalAlias>(&GV))
+ return GA->getBaseObject();
+ return cast<GlobalObject>(&GV);
+}
+
/// 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.
static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
int *claimed) {
LLVMContext Context;
- std::unique_ptr<MemoryBuffer> buffer;
+ MemoryBufferRef BufferRef;
+ std::unique_ptr<MemoryBuffer> Buffer;
if (get_view) {
const void *view;
if (get_view(file->handle, &view) != LDPS_OK) {
message(LDPL_ERROR, "Failed to get a view of %s", file->name);
return LDPS_ERR;
}
- buffer.reset(MemoryBuffer::getMemBuffer(
- StringRef((char *)view, file->filesize), "", false));
+ BufferRef = MemoryBufferRef(StringRef((const char *)view, file->filesize), "");
} else {
int64_t offset = 0;
// Gold has found what might be IR part-way inside of a file, such as
message(LDPL_ERROR, EC.message().c_str());
return LDPS_ERR;
}
- buffer = std::move(BufferOrErr.get());
+ Buffer = std::move(BufferOrErr.get());
+ BufferRef = Buffer->getMemBufferRef();
}
- ErrorOr<object::IRObjectFile *> ObjOrErr =
- object::IRObjectFile::createIRObjectFile(buffer->getMemBufferRef(),
- Context);
+ ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
+ object::IRObjectFile::createIRObjectFile(BufferRef, Context);
std::error_code EC = ObjOrErr.getError();
- if (EC == BitcodeError::InvalidBitcodeSignature)
+ if (EC == BitcodeError::InvalidBitcodeSignature ||
+ EC == object::object_error::invalid_file_type ||
+ EC == object::object_error::bitcode_section_not_found)
return LDPS_OK;
*claimed = 1;
EC.message().c_str());
return LDPS_ERR;
}
- std::unique_ptr<object::IRObjectFile> Obj(ObjOrErr.get());
+ std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr);
Modules.resize(Modules.size() + 1);
claimed_file &cf = Modules.back();
sym.size = 0;
sym.comdat_key = nullptr;
- if (GV && (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()))
- sym.comdat_key = sym.name;
+ 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);
+ }
sym.resolution = LDPR_UNKNOWN;
}
return LDPS_OK;
}
-static void keepGlobalValue(GlobalValue &GV) {
+static void keepGlobalValue(GlobalValue &GV,
+ std::vector<GlobalAlias *> &KeptAliases) {
assert(!GV.hasLocalLinkage());
+ if (auto *GA = dyn_cast<GlobalAlias>(&GV))
+ KeptAliases.push_back(GA);
+
switch (GV.getLinkage()) {
default:
break;
assert(!GV.isDiscardableIfUnused());
}
-static bool isDeclaration(const GlobalValue &V) {
- if (V.hasAvailableExternallyLinkage())
- return true;
-
- if (V.isMaterializable())
- return false;
-
- return V.isDeclaration();
-}
-
static void internalize(GlobalValue &GV) {
- if (isDeclaration(GV))
+ if (GV.isDeclarationForLinker())
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();
+ F->setComdat(nullptr); // Should deleteBody do this?
return;
}
if (auto *Var = dyn_cast<GlobalVariable>(&GV)) {
Var->setInitializer(nullptr);
- Var->setLinkage(GlobalValue::ExternalLinkage);
+ Var->setLinkage(
+ GlobalValue::ExternalLinkage); // Should setInitializer do this?
+ Var->setComdat(nullptr); // and this?
return;
}
/*Initializer*/ nullptr);
Var->takeName(&Alias);
Alias.replaceAllUsesWith(Var);
+ Alias.eraseFromParent();
}
static const char *getResolutionName(ld_plugin_symbol_resolution R) {
case LDPR_PREVAILING_DEF_IRONLY_EXP:
return "PREVAILING_DEF_IRONLY_EXP";
}
+ llvm_unreachable("Unknown resolution");
+}
+
+static GlobalObject *makeInternalReplacement(GlobalObject *GO) {
+ Module *M = GO->getParent();
+ GlobalObject *Ret;
+ if (auto *F = dyn_cast<Function>(GO)) {
+ if (F->isMaterializable()) {
+ if (F->materialize())
+ message(LDPL_FATAL, "LLVM gold plugin has failed to read a function");
+
+ }
+
+ auto *NewF = Function::Create(F->getFunctionType(), F->getLinkage(),
+ F->getName(), M);
+
+ ValueToValueMapTy VM;
+ Function::arg_iterator NewI = NewF->arg_begin();
+ for (auto &Arg : F->args()) {
+ NewI->setName(Arg.getName());
+ VM[&Arg] = NewI;
+ ++NewI;
+ }
+
+ NewF->getBasicBlockList().splice(NewF->end(), F->getBasicBlockList());
+ for (auto &BB : *NewF) {
+ for (auto &Inst : BB)
+ RemapInstruction(&Inst, VM, RF_IgnoreMissingEntries);
+ }
+
+ Ret = NewF;
+ F->deleteBody();
+ } else {
+ auto *Var = cast<GlobalVariable>(GO);
+ Ret = new GlobalVariable(
+ *M, Var->getType()->getElementType(), Var->isConstant(),
+ Var->getLinkage(), Var->getInitializer(), Var->getName(),
+ nullptr, Var->getThreadLocalMode(), Var->getType()->getAddressSpace(),
+ Var->isExternallyInitialized());
+ Var->setInitializer(nullptr);
+ }
+ Ret->copyAttributesFrom(GO);
+ Ret->setLinkage(GlobalValue::InternalLinkage);
+ Ret->setComdat(GO->getComdat());
+
+ return Ret;
+}
+
+namespace {
+class LocalValueMaterializer : public ValueMaterializer {
+ DenseSet<GlobalValue *> &Dropped;
+
+public:
+ LocalValueMaterializer(DenseSet<GlobalValue *> &Dropped) : Dropped(Dropped) {}
+ Value *materializeValueFor(Value *V) override;
+};
+}
+
+Value *LocalValueMaterializer::materializeValueFor(Value *V) {
+ auto *GV = dyn_cast<GlobalValue>(V);
+ if (!GV)
+ return nullptr;
+ if (!Dropped.count(GV))
+ return nullptr;
+ assert(!isa<GlobalAlias>(GV) && "Found alias point to weak alias.");
+ return makeInternalReplacement(cast<GlobalObject>(GV));
+}
+
+static Constant *mapConstantToLocalCopy(Constant *C, ValueToValueMapTy &VM,
+ LocalValueMaterializer *Materializer) {
+ return MapValue(C, VM, RF_IgnoreMissingEntries, nullptr, Materializer);
}
static std::unique_ptr<Module>
if (get_view(F.handle, &View) != LDPS_OK)
message(LDPL_FATAL, "Failed to get a view of file");
- std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(
- StringRef((char *)View, File.filesize), "", false));
+ llvm::ErrorOr<MemoryBufferRef> MBOrErr =
+ object::IRObjectFile::findBitcodeInMemBuffer(
+ MemoryBufferRef(StringRef((const char *)View, File.filesize), ""));
+ if (std::error_code EC = MBOrErr.getError())
+ message(LDPL_FATAL, "Could not read bitcode from file : %s",
+ EC.message().c_str());
+
+ std::unique_ptr<MemoryBuffer> Buffer =
+ MemoryBuffer::getMemBuffer(MBOrErr->getBuffer(), "", false);
if (release_input_file(F.handle) != LDPS_OK)
message(LDPL_FATAL, "Failed to release file information");
- ErrorOr<Module *> MOrErr = getLazyBitcodeModule(Buffer.get(), Context);
+ ErrorOr<Module *> MOrErr = getLazyBitcodeModule(std::move(Buffer), Context);
if (std::error_code EC = MOrErr.getError())
message(LDPL_FATAL, "Could not read bitcode from file : %s",
EC.message().c_str());
- Buffer.release();
std::unique_ptr<Module> M(MOrErr.get());
SmallPtrSet<GlobalValue *, 8> Used;
collectUsedGlobalVariables(*M, Used, /*CompilerUsed*/ false);
- std::vector<GlobalValue *> Drop;
+ DenseSet<GlobalValue *> Drop;
+ std::vector<GlobalAlias *> KeptAliases;
for (ld_plugin_symbol &Sym : F.syms) {
ld_plugin_symbol_resolution Resolution =
(ld_plugin_symbol_resolution)Sym.resolution;
if (!GV)
continue; // Asm symbol.
+ if (Resolution != LDPR_PREVAILING_DEF_IRONLY && GV->hasCommonLinkage()) {
+ // 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.
+ // We still have to keep track of LDPR_PREVAILING_DEF_IRONLY so we
+ // internalize once the IR linker has done its job.
+ continue;
+ }
+
switch (Resolution) {
case LDPR_UNKNOWN:
llvm_unreachable("Unexpected resolution");
case LDPR_RESOLVED_EXEC:
case LDPR_RESOLVED_DYN:
case LDPR_UNDEF:
- assert(isDeclaration(*GV));
+ assert(GV->isDeclarationForLinker());
break;
case LDPR_PREVAILING_DEF_IRONLY: {
+ keepGlobalValue(*GV, KeptAliases);
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.
- keepGlobalValue(*GV);
Internalize.insert(Sym.name);
}
break;
}
case LDPR_PREVAILING_DEF:
- keepGlobalValue(*GV);
+ keepGlobalValue(*GV, KeptAliases);
break;
- case LDPR_PREEMPTED_REG:
case LDPR_PREEMPTED_IR:
- Drop.push_back(GV);
+ // 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())
+ Maybe.erase(Sym.name);
+
+ // fall-through
+ case LDPR_PREEMPTED_REG:
+ Drop.insert(GV);
break;
case LDPR_PREVAILING_DEF_IRONLY_EXP: {
// copy will be LDPR_PREEMPTED_IR.
if (GV->hasLinkOnceODRLinkage())
Maybe.insert(Sym.name);
- keepGlobalValue(*GV);
+ keepGlobalValue(*GV, KeptAliases);
break;
}
}
free(Sym.name);
+ free(Sym.comdat_key);
Sym.name = nullptr;
Sym.comdat_key = nullptr;
}
- if (!Drop.empty()) {
- // This is horrible. Given how lazy loading is implemented, dropping
- // the body while there is a materializer present doesn't work, the
- // linker will just read the body back.
- M->materializeAllPermanently();
- for (auto *GV : Drop)
- drop(*GV);
+ 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);
+ if (Aliasee != Replacement)
+ GA->setAliasee(Replacement);
}
+ for (auto *GV : Drop)
+ drop(*GV);
+
return M;
}
runLTOPasses(M, *TM);
PassManager CodeGenPasses;
- CodeGenPasses.add(new DataLayoutPass(&M));
+ CodeGenPasses.add(new DataLayoutPass());
SmallString<128> Filename;
int FD;
M->setTargetTriple(DefaultTriple);
}
- std::string ErrMsg;
- if (L.linkInModule(M.get(), &ErrMsg))
- message(LDPL_FATAL, "Failed to link module: %s", ErrMsg.c_str());
+ if (L.linkInModule(M.get()))
+ message(LDPL_FATAL, "Failed to link module");
}
for (const auto &Name : Internalize) {
else
path = output_name + ".bc";
{
- std::string Error;
- raw_fd_ostream OS(path.c_str(), Error, sys::fs::OpenFlags::F_None);
- if (!Error.empty())
+ 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.");
WriteBitcodeToFile(L.getModule(), OS);
}
if (!options::generate_api_file) {
Ret = allSymbolsReadHook(nullptr);
} else {
- std::string Error;
- raw_fd_ostream ApiFile("apifile.txt", Error, sys::fs::F_None);
- if (!Error.empty())
+ std::error_code EC;
+ raw_fd_ostream ApiFile("apifile.txt", EC, sys::fs::F_None);
+ if (EC)
message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s",
- Error.c_str());
+ EC.message().c_str());
Ret = allSymbolsReadHook(&ApiFile);
}