#include "llvm/Linker/Linker.h"
#include "llvm-c/Linker.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/Triple.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/TypeFinder.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include <cctype>
#include <tuple>
using namespace llvm;
namespace {
class TypeMapTy : public ValueMapTypeRemapper {
DenseMap<Type*, Type*> MappedTypes;
SmallVector<Type*, 16> SpeculativeTypes;
SmallVector<StructType*, 16> SpeculativeDstOpaqueTypes;
SmallVector<StructType*, 16> SrcDefinitionsToResolve;
SmallPtrSet<StructType*, 16> DstResolvedOpaqueTypes;
public:
TypeMapTy(Linker::IdentifiedStructTypeSet &DstStructTypesSet)
: DstStructTypesSet(DstStructTypesSet) {}
Linker::IdentifiedStructTypeSet &DstStructTypesSet;
void addTypeMapping(Type *DstTy, Type *SrcTy);
void linkDefinedTypeBodies();
Type *get(Type *SrcTy);
Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);
void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);
FunctionType *get(FunctionType *T) {
return cast<FunctionType>(get((Type *)T));
}
void dump() const {
for (auto &Pair : MappedTypes) {
dbgs() << "TypeMap: ";
Pair.first->print(dbgs());
dbgs() << " => ";
Pair.second->print(dbgs());
dbgs() << '\n';
}
}
private:
Type *remapType(Type *SrcTy) override { return get(SrcTy); }
bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
};
}
void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
assert(SpeculativeTypes.empty());
assert(SpeculativeDstOpaqueTypes.empty());
if (!areTypesIsomorphic(DstTy, SrcTy)) {
for (Type *Ty : SpeculativeTypes)
MappedTypes.erase(Ty);
SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -
SpeculativeDstOpaqueTypes.size());
for (StructType *Ty : SpeculativeDstOpaqueTypes)
DstResolvedOpaqueTypes.erase(Ty);
} else {
for (Type *Ty : SpeculativeTypes)
if (auto *STy = dyn_cast<StructType>(Ty))
if (STy->hasName())
STy->setName("");
}
SpeculativeTypes.clear();
SpeculativeDstOpaqueTypes.clear();
}
bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
if (DstTy->getTypeID() != SrcTy->getTypeID())
return false;
Type *&Entry = MappedTypes[SrcTy];
if (Entry)
return Entry == DstTy;
if (DstTy == SrcTy) {
Entry = DstTy;
return true;
}
if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
if (SSTy->isOpaque()) {
Entry = DstTy;
SpeculativeTypes.push_back(SrcTy);
return true;
}
if (cast<StructType>(DstTy)->isOpaque()) {
if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
return false;
SrcDefinitionsToResolve.push_back(SSTy);
SpeculativeTypes.push_back(SrcTy);
SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));
Entry = DstTy;
return true;
}
}
if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
return false;
if (isa<IntegerType>(DstTy))
return false; if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
return false;
} else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
return false;
} else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
StructType *SSTy = cast<StructType>(SrcTy);
if (DSTy->isLiteral() != SSTy->isLiteral() ||
DSTy->isPacked() != SSTy->isPacked())
return false;
} else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
return false;
} else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements())
return false;
}
Entry = DstTy;
SpeculativeTypes.push_back(SrcTy);
for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)
if (!areTypesIsomorphic(DstTy->getContainedType(I),
SrcTy->getContainedType(I)))
return false;
return true;
}
void TypeMapTy::linkDefinedTypeBodies() {
SmallVector<Type*, 16> Elements;
for (StructType *SrcSTy : SrcDefinitionsToResolve) {
StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
assert(DstSTy->isOpaque());
Elements.resize(SrcSTy->getNumElements());
for (unsigned I = 0, E = Elements.size(); I != E; ++I)
Elements[I] = get(SrcSTy->getElementType(I));
DstSTy->setBody(Elements, SrcSTy->isPacked());
DstStructTypesSet.switchToNonOpaque(DstSTy);
}
SrcDefinitionsToResolve.clear();
DstResolvedOpaqueTypes.clear();
}
void TypeMapTy::finishType(StructType *DTy, StructType *STy,
ArrayRef<Type *> ETypes) {
DTy->setBody(ETypes, STy->isPacked());
if (STy->hasName()) {
SmallString<16> TmpName = STy->getName();
STy->setName("");
DTy->setName(TmpName);
}
DstStructTypesSet.addNonOpaque(DTy);
}
Type *TypeMapTy::get(Type *Ty) {
SmallPtrSet<StructType *, 8> Visited;
return get(Ty, Visited);
}
Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) {
Type **Entry = &MappedTypes[Ty];
if (*Entry)
return *Entry;
bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral();
#ifndef NDEBUG
if (!IsUniqued) {
for (auto &Pair : MappedTypes) {
assert(!(Pair.first != Ty && Pair.second == Ty) &&
"mapping to a source type");
}
}
#endif
if (!IsUniqued && !Visited.insert(cast<StructType>(Ty)).second) {
StructType *DTy = StructType::create(Ty->getContext());
return *Entry = DTy;
}
SmallVector<Type *, 4> ElementTypes;
if (Ty->getNumContainedTypes() == 0 && IsUniqued)
return *Entry = Ty;
bool AnyChange = false;
ElementTypes.resize(Ty->getNumContainedTypes());
for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
ElementTypes[I] = get(Ty->getContainedType(I), Visited);
AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
}
Entry = &MappedTypes[Ty];
if (*Entry) {
if (auto *DTy = dyn_cast<StructType>(*Entry)) {
if (DTy->isOpaque()) {
auto *STy = cast<StructType>(Ty);
finishType(DTy, STy, ElementTypes);
}
}
return *Entry;
}
if (!AnyChange && IsUniqued)
return *Entry = Ty;
switch (Ty->getTypeID()) {
default:
llvm_unreachable("unknown derived type to remap");
case Type::ArrayTyID:
return *Entry = ArrayType::get(ElementTypes[0],
cast<ArrayType>(Ty)->getNumElements());
case Type::VectorTyID:
return *Entry = VectorType::get(ElementTypes[0],
cast<VectorType>(Ty)->getNumElements());
case Type::PointerTyID:
return *Entry = PointerType::get(ElementTypes[0],
cast<PointerType>(Ty)->getAddressSpace());
case Type::FunctionTyID:
return *Entry = FunctionType::get(ElementTypes[0],
makeArrayRef(ElementTypes).slice(1),
cast<FunctionType>(Ty)->isVarArg());
case Type::StructTyID: {
auto *STy = cast<StructType>(Ty);
bool IsPacked = STy->isPacked();
if (IsUniqued)
return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
if (STy->isOpaque()) {
DstStructTypesSet.addOpaque(STy);
return *Entry = Ty;
}
if (StructType *OldT =
DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
STy->setName("");
return *Entry = OldT;
}
if (!AnyChange) {
DstStructTypesSet.addNonOpaque(STy);
return *Entry = Ty;
}
StructType *DTy = StructType::create(Ty->getContext());
finishType(DTy, STy, ElementTypes);
return *Entry = DTy;
}
}
}
namespace {
class ModuleLinker;
class ValueMaterializerTy final : public ValueMaterializer {
TypeMapTy &TypeMap;
Module *DstM;
std::vector<GlobalValue *> &LazilyLinkGlobalValues;
ModuleLinker *ModLinker;
public:
ValueMaterializerTy(TypeMapTy &TypeMap, Module *DstM,
std::vector<GlobalValue *> &LazilyLinkGlobalValues,
ModuleLinker *ModLinker)
: ValueMaterializer(), TypeMap(TypeMap), DstM(DstM),
LazilyLinkGlobalValues(LazilyLinkGlobalValues), ModLinker(ModLinker) {}
Value *materializeValueFor(Value *V) override;
};
class LinkDiagnosticInfo : public DiagnosticInfo {
const Twine &Msg;
public:
LinkDiagnosticInfo(DiagnosticSeverity Severity, const Twine &Msg);
void print(DiagnosticPrinter &DP) const override;
};
LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,
const Twine &Msg)
: DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
class ModuleLinker {
Module *DstM, *SrcM;
TypeMapTy TypeMap;
ValueMaterializerTy ValMaterializer;
ValueToValueMapTy ValueMap;
struct AppendingVarInfo {
GlobalVariable *NewGV; const Constant *DstInit; const Constant *SrcInit; };
std::vector<AppendingVarInfo> AppendingVars;
SmallPtrSet<const Value *, 16> DoNotLinkFromSource;
std::vector<GlobalValue *> LazilyLinkGlobalValues;
DiagnosticHandlerFunction DiagnosticHandler;
unsigned Flags;
FunctionInfoIndex *ImportIndex;
Function *ImportFunction;
bool HasExportedFunctions;
bool DoneLinkingBodies;
public:
ModuleLinker(Module *dstM, Linker::IdentifiedStructTypeSet &Set, Module *srcM,
DiagnosticHandlerFunction DiagnosticHandler, unsigned Flags,
FunctionInfoIndex *Index = nullptr,
Function *FuncToImport = nullptr)
: DstM(dstM), SrcM(srcM), TypeMap(Set),
ValMaterializer(TypeMap, DstM, LazilyLinkGlobalValues, this),
DiagnosticHandler(DiagnosticHandler), Flags(Flags), ImportIndex(Index),
ImportFunction(FuncToImport), HasExportedFunctions(false),
DoneLinkingBodies(false) {
assert((ImportIndex || !ImportFunction) &&
"Expect a FunctionInfoIndex when importing");
if (ImportIndex && !ImportFunction)
HasExportedFunctions = ImportIndex->hasExportedFunctions(SrcM);
}
bool run();
bool shouldOverrideFromSrc() { return Flags & Linker::OverrideFromSrc; }
bool shouldLinkOnlyNeeded() { return Flags & Linker::LinkOnlyNeeded; }
bool shouldInternalizeLinkedSymbols() {
return Flags & Linker::InternalizeLinkedSymbols;
}
GlobalValue *copyGlobalValueProto(TypeMapTy &TypeMap, const GlobalValue *SGV,
const GlobalValue *DGV = nullptr);
bool doPromoteLocalToGlobal(const GlobalValue *SGV);
bool doneLinkingBodies() { return DoneLinkingBodies; }
private:
bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest,
const GlobalValue &Src);
bool emitError(const Twine &Message) {
DiagnosticHandler(LinkDiagnosticInfo(DS_Error, Message));
return true;
}
void emitWarning(const Twine &Message) {
DiagnosticHandler(LinkDiagnosticInfo(DS_Warning, Message));
}
bool getComdatLeader(Module *M, StringRef ComdatName,
const GlobalVariable *&GVar);
bool computeResultingSelectionKind(StringRef ComdatName,
Comdat::SelectionKind Src,
Comdat::SelectionKind Dst,
Comdat::SelectionKind &Result,
bool &LinkFromSrc);
std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>>
ComdatsChosen;
bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK,
bool &LinkFromSrc);
GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
return nullptr;
GlobalValue *DGV = DstM->getNamedValue(SrcGV->getName());
if (!DGV)
return nullptr;
if (DGV->hasLocalLinkage())
return nullptr;
return DGV;
}
void computeTypeMapping();
void upgradeMismatchedGlobalArray(StringRef Name);
void upgradeMismatchedGlobals();
bool linkAppendingVarProto(GlobalVariable *DstGV,
const GlobalVariable *SrcGV);
bool linkGlobalValueProto(GlobalValue *GV);
bool linkModuleFlagsMetadata();
void linkAppendingVarInit(const AppendingVarInfo &AVI);
void linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src);
bool linkFunctionBody(Function &Dst, Function &Src);
void linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src);
bool linkGlobalValueBody(GlobalValue &Src);
GlobalVariable *copyGlobalVariableProto(TypeMapTy &TypeMap,
const GlobalVariable *SGVar);
Function *copyFunctionProto(TypeMapTy &TypeMap, const Function *SF);
GlobalValue *copyGlobalAliasProto(TypeMapTy &TypeMap, const GlobalAlias *SGA);
bool isPerformingImport() { return ImportFunction != nullptr; }
bool isModuleExporting() { return HasExportedFunctions; }
bool doImportAsDefinition(const GlobalValue *SGV);
std::string getName(const GlobalValue *SGV);
GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV);
void copyGVAttributes(GlobalValue *NewGV, const GlobalValue *SrcGV);
void setVisibility(GlobalValue *NewGV, const GlobalValue *SGV,
const GlobalValue *DGV = nullptr);
void linkNamedMDNodes();
};
}
static void forceRenaming(GlobalValue *GV, StringRef Name) {
if (GV->hasLocalLinkage() || GV->getName() == Name)
return;
Module *M = GV->getParent();
if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
GV->takeName(ConflictGV);
ConflictGV->setName(Name); assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
} else {
GV->setName(Name); }
}
void ModuleLinker::copyGVAttributes(GlobalValue *NewGV,
const GlobalValue *SrcGV) {
auto *GA = dyn_cast<GlobalAlias>(SrcGV);
if (GA && !dyn_cast<GlobalAlias>(NewGV)) {
assert(isPerformingImport() &&
(GA->hasWeakAnyLinkage() ||
!doImportAsDefinition(GA->getBaseObject())));
NewGV->copyAttributesFrom(GA->getBaseObject());
} else
NewGV->copyAttributesFrom(SrcGV);
forceRenaming(NewGV, getName(SrcGV));
}
static bool isLessConstraining(GlobalValue::VisibilityTypes a,
GlobalValue::VisibilityTypes b) {
if (a == GlobalValue::HiddenVisibility)
return false;
if (b == GlobalValue::HiddenVisibility)
return true;
if (a == GlobalValue::ProtectedVisibility)
return false;
if (b == GlobalValue::ProtectedVisibility)
return true;
return false;
}
bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) {
if (!isPerformingImport())
return false;
if (dyn_cast<GlobalVariable>(SGV) && !SGV->isDeclaration())
return true;
auto *SF = dyn_cast<Function>(SGV);
if (SF && SF == ImportFunction)
return true;
return false;
}
bool ModuleLinker::doPromoteLocalToGlobal(const GlobalValue *SGV) {
assert(SGV->hasLocalLinkage());
if (!isPerformingImport() && !isModuleExporting())
return false;
auto *GVar = dyn_cast<GlobalVariable>(SGV);
if (GVar && GVar->isConstant() && GVar->hasUnnamedAddr())
return false;
return true;
}
std::string ModuleLinker::getName(const GlobalValue *SGV) {
if (SGV->hasLocalLinkage() &&
(doPromoteLocalToGlobal(SGV) || isPerformingImport()))
return FunctionInfoIndex::getGlobalNameForLocal(
SGV->getName(),
ImportIndex->getModuleId(SGV->getParent()->getModuleIdentifier()));
return SGV->getName();
}
GlobalValue::LinkageTypes ModuleLinker::getLinkage(const GlobalValue *SGV) {
if (isModuleExporting()) {
if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV))
return GlobalValue::ExternalLinkage;
return SGV->getLinkage();
}
if (!isPerformingImport())
return SGV->getLinkage();
switch (SGV->getLinkage()) {
case GlobalValue::ExternalLinkage:
if (doImportAsDefinition(SGV))
return GlobalValue::AvailableExternallyLinkage;
return SGV->getLinkage();
case GlobalValue::AvailableExternallyLinkage:
if (!doImportAsDefinition(SGV))
return GlobalValue::ExternalLinkage;
return SGV->getLinkage();
case GlobalValue::LinkOnceAnyLinkage:
case GlobalValue::LinkOnceODRLinkage:
return SGV->getLinkage();
case GlobalValue::WeakAnyLinkage:
assert(!doImportAsDefinition(SGV));
return GlobalValue::ExternalWeakLinkage;
case GlobalValue::WeakODRLinkage:
if (doImportAsDefinition(SGV))
return GlobalValue::AvailableExternallyLinkage;
else
return GlobalValue::ExternalLinkage;
case GlobalValue::AppendingLinkage:
assert(false && "Cannot import appending linkage variable");
case GlobalValue::InternalLinkage:
case GlobalValue::PrivateLinkage:
if (doPromoteLocalToGlobal(SGV)) {
if (doImportAsDefinition(SGV))
return GlobalValue::AvailableExternallyLinkage;
else
return GlobalValue::ExternalLinkage;
}
return SGV->getLinkage();
case GlobalValue::ExternalWeakLinkage:
assert(!doImportAsDefinition(SGV));
return SGV->getLinkage();
case GlobalValue::CommonLinkage:
return SGV->getLinkage();
}
llvm_unreachable("unknown linkage type");
}
GlobalVariable *
ModuleLinker::copyGlobalVariableProto(TypeMapTy &TypeMap,
const GlobalVariable *SGVar) {
GlobalVariable *NewDGV = new GlobalVariable(
*DstM, TypeMap.get(SGVar->getType()->getElementType()),
SGVar->isConstant(), getLinkage(SGVar), nullptr, getName(SGVar),
nullptr, SGVar->getThreadLocalMode(),
SGVar->getType()->getAddressSpace());
return NewDGV;
}
Function *ModuleLinker::copyFunctionProto(TypeMapTy &TypeMap,
const Function *SF) {
return Function::Create(TypeMap.get(SF->getFunctionType()), getLinkage(SF),
getName(SF), DstM);
}
GlobalValue *ModuleLinker::copyGlobalAliasProto(TypeMapTy &TypeMap,
const GlobalAlias *SGA) {
if (isPerformingImport() && (SGA->hasWeakAnyLinkage() ||
!doImportAsDefinition(SGA->getBaseObject()))) {
const GlobalValue *GVal = SGA->getBaseObject();
GlobalValue *NewGV;
if (auto *GVar = dyn_cast<GlobalVariable>(GVal))
NewGV = copyGlobalVariableProto(TypeMap, GVar);
else {
auto *F = dyn_cast<Function>(GVal);
assert(F);
NewGV = copyFunctionProto(TypeMap, F);
}
if (SGA->hasWeakAnyLinkage())
NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
else
NewGV->setLinkage(GlobalValue::ExternalLinkage);
DoNotLinkFromSource.insert(SGA);
return NewGV;
}
auto *Ty = TypeMap.get(SGA->getValueType());
return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
getLinkage(SGA), getName(SGA), DstM);
}
void ModuleLinker::setVisibility(GlobalValue *NewGV, const GlobalValue *SGV,
const GlobalValue *DGV) {
GlobalValue::VisibilityTypes Visibility = SGV->getVisibility();
if (DGV)
Visibility = isLessConstraining(Visibility, DGV->getVisibility())
? DGV->getVisibility()
: Visibility;
if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV))
Visibility = GlobalValue::HiddenVisibility;
NewGV->setVisibility(Visibility);
}
GlobalValue *ModuleLinker::copyGlobalValueProto(TypeMapTy &TypeMap,
const GlobalValue *SGV,
const GlobalValue *DGV) {
GlobalValue *NewGV;
if (auto *SGVar = dyn_cast<GlobalVariable>(SGV))
NewGV = copyGlobalVariableProto(TypeMap, SGVar);
else if (auto *SF = dyn_cast<Function>(SGV))
NewGV = copyFunctionProto(TypeMap, SF);
else
NewGV = copyGlobalAliasProto(TypeMap, cast<GlobalAlias>(SGV));
copyGVAttributes(NewGV, SGV);
setVisibility(NewGV, SGV, DGV);
return NewGV;
}
Value *ValueMaterializerTy::materializeValueFor(Value *V) {
auto *SGV = dyn_cast<GlobalValue>(V);
if (!SGV)
return nullptr;
if (ModLinker->doneLinkingBodies())
return nullptr;
GlobalValue *DGV = ModLinker->copyGlobalValueProto(TypeMap, SGV);
if (Comdat *SC = SGV->getComdat()) {
if (auto *DGO = dyn_cast<GlobalObject>(DGV)) {
Comdat *DC = DstM->getOrInsertComdat(SC->getName());
DGO->setComdat(DC);
}
}
LazilyLinkGlobalValues.push_back(SGV);
return DGV;
}
bool ModuleLinker::getComdatLeader(Module *M, StringRef ComdatName,
const GlobalVariable *&GVar) {
const GlobalValue *GVal = M->getNamedValue(ComdatName);
if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) {
GVal = GA->getBaseObject();
if (!GVal)
return emitError("Linking COMDATs named '" + ComdatName +
"': COMDAT key involves incomputable alias size.");
}
GVar = dyn_cast_or_null<GlobalVariable>(GVal);
if (!GVar)
return emitError(
"Linking COMDATs named '" + ComdatName +
"': GlobalVariable required for data dependent selection!");
return false;
}
bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
Comdat::SelectionKind Src,
Comdat::SelectionKind Dst,
Comdat::SelectionKind &Result,
bool &LinkFromSrc) {
bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any ||
Dst == Comdat::SelectionKind::Largest;
bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any ||
Src == Comdat::SelectionKind::Largest;
if (DstAnyOrLargest && SrcAnyOrLargest) {
if (Dst == Comdat::SelectionKind::Largest ||
Src == Comdat::SelectionKind::Largest)
Result = Comdat::SelectionKind::Largest;
else
Result = Comdat::SelectionKind::Any;
} else if (Src == Dst) {
Result = Dst;
} else {
return emitError("Linking COMDATs named '" + ComdatName +
"': invalid selection kinds!");
}
switch (Result) {
case Comdat::SelectionKind::Any:
LinkFromSrc = false;
break;
case Comdat::SelectionKind::NoDuplicates:
return emitError("Linking COMDATs named '" + ComdatName +
"': noduplicates has been violated!");
case Comdat::SelectionKind::ExactMatch:
case Comdat::SelectionKind::Largest:
case Comdat::SelectionKind::SameSize: {
const GlobalVariable *DstGV;
const GlobalVariable *SrcGV;
if (getComdatLeader(DstM, ComdatName, DstGV) ||
getComdatLeader(SrcM, ComdatName, SrcGV))
return true;
const DataLayout &DstDL = DstM->getDataLayout();
const DataLayout &SrcDL = SrcM->getDataLayout();
uint64_t DstSize =
DstDL.getTypeAllocSize(DstGV->getType()->getPointerElementType());
uint64_t SrcSize =
SrcDL.getTypeAllocSize(SrcGV->getType()->getPointerElementType());
if (Result == Comdat::SelectionKind::ExactMatch) {
if (SrcGV->getInitializer() != DstGV->getInitializer())
return emitError("Linking COMDATs named '" + ComdatName +
"': ExactMatch violated!");
LinkFromSrc = false;
} else if (Result == Comdat::SelectionKind::Largest) {
LinkFromSrc = SrcSize > DstSize;
} else if (Result == Comdat::SelectionKind::SameSize) {
if (SrcSize != DstSize)
return emitError("Linking COMDATs named '" + ComdatName +
"': SameSize violated!");
LinkFromSrc = false;
} else {
llvm_unreachable("unknown selection kind");
}
break;
}
}
return false;
}
bool ModuleLinker::getComdatResult(const Comdat *SrcC,
Comdat::SelectionKind &Result,
bool &LinkFromSrc) {
Comdat::SelectionKind SSK = SrcC->getSelectionKind();
StringRef ComdatName = SrcC->getName();
Module::ComdatSymTabType &ComdatSymTab = DstM->getComdatSymbolTable();
Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName);
if (DstCI == ComdatSymTab.end()) {
LinkFromSrc = true;
Result = SSK;
return false;
}
const Comdat *DstC = &DstCI->second;
Comdat::SelectionKind DSK = DstC->getSelectionKind();
return computeResultingSelectionKind(ComdatName, SSK, DSK, Result,
LinkFromSrc);
}
bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
const GlobalValue &Dest,
const GlobalValue &Src) {
if (shouldOverrideFromSrc()) {
LinkFromSrc = true;
return false;
}
if (Src.hasAppendingLinkage()) {
assert(!isPerformingImport());
LinkFromSrc = true;
return false;
}
bool SrcIsDeclaration = Src.isDeclarationForLinker();
bool DestIsDeclaration = Dest.isDeclarationForLinker();
if (isPerformingImport()) {
if (isa<Function>(&Src)) {
LinkFromSrc = (&Src == ImportFunction);
return false;
}
if (isa<GlobalAlias>(&Src) && !DestIsDeclaration) {
assert(isa<GlobalAlias>(&Dest));
LinkFromSrc = false;
return false;
}
}
if (SrcIsDeclaration) {
if (Src.hasDLLImportStorageClass()) {
LinkFromSrc = DestIsDeclaration;
return false;
}
LinkFromSrc = Dest.hasExternalWeakLinkage();
return false;
}
if (DestIsDeclaration) {
LinkFromSrc = true;
return false;
}
if (Src.hasCommonLinkage()) {
if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) {
LinkFromSrc = true;
return false;
}
if (!Dest.hasCommonLinkage()) {
LinkFromSrc = false;
return false;
}
const DataLayout &DL = Dest.getParent()->getDataLayout();
uint64_t DestSize = DL.getTypeAllocSize(Dest.getType()->getElementType());
uint64_t SrcSize = DL.getTypeAllocSize(Src.getType()->getElementType());
LinkFromSrc = SrcSize > DestSize;
return false;
}
if (Src.isWeakForLinker()) {
assert(!Dest.hasExternalWeakLinkage());
assert(!Dest.hasAvailableExternallyLinkage());
if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) {
LinkFromSrc = true;
return false;
}
LinkFromSrc = false;
return false;
}
if (Dest.isWeakForLinker()) {
assert(Src.hasExternalLinkage());
LinkFromSrc = true;
return false;
}
assert(!Src.hasExternalWeakLinkage());
assert(!Dest.hasExternalWeakLinkage());
assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() &&
"Unexpected linkage type!");
return emitError("Linking globals named '" + Src.getName() +
"': symbol multiply defined!");
}
void ModuleLinker::computeTypeMapping() {
for (GlobalValue &SGV : SrcM->globals()) {
GlobalValue *DGV = getLinkedToGlobal(&SGV);
if (!DGV)
continue;
if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
continue;
}
ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType());
ArrayType *SAT = cast<ArrayType>(SGV.getType()->getElementType());
TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
}
for (GlobalValue &SGV : *SrcM) {
if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
}
for (GlobalValue &SGV : SrcM->aliases()) {
if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
}
std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes();
for (StructType *ST : Types) {
if (!ST->hasName())
continue;
size_t DotPos = ST->getName().rfind('.');
if (DotPos == 0 || DotPos == StringRef::npos ||
ST->getName().back() == '.' ||
!isdigit(static_cast<unsigned char>(ST->getName()[DotPos + 1])))
continue;
StructType *DST = DstM->getTypeByName(ST->getName().substr(0, DotPos));
if (!DST)
continue;
if (TypeMap.DstStructTypesSet.hasType(DST))
TypeMap.addTypeMapping(DST, ST);
}
TypeMap.linkDefinedTypeBodies();
}
static void upgradeGlobalArray(GlobalVariable *GV) {
ArrayType *ATy = cast<ArrayType>(GV->getType()->getElementType());
StructType *OldTy = cast<StructType>(ATy->getElementType());
assert(OldTy->getNumElements() == 2 && "Expected to upgrade from 2 elements");
PointerType *VoidPtrTy = Type::getInt8Ty(GV->getContext())->getPointerTo();
Type *Tys[3] = {OldTy->getElementType(0), OldTy->getElementType(1),
VoidPtrTy};
StructType *NewTy = StructType::get(GV->getContext(), Tys, false);
Constant *OldInitC = GV->getInitializer();
ConstantArray *OldInit = dyn_cast<ConstantArray>(OldInitC);
if (!OldInit && !isa<ConstantAggregateZero>(OldInitC))
return;
std::vector<Constant *> Initializers;
if (OldInit && OldInit->getNumOperands()) {
Value *Null = Constant::getNullValue(VoidPtrTy);
for (Use &U : OldInit->operands()) {
ConstantStruct *Init = cast<ConstantStruct>(U.get());
Initializers.push_back(ConstantStruct::get(
NewTy, Init->getOperand(0), Init->getOperand(1), Null, nullptr));
}
}
assert(Initializers.size() == ATy->getNumElements() &&
"Failed to copy all array elements");
ATy = ArrayType::get(NewTy, Initializers.size());
Constant *NewInit = ConstantArray::get(ATy, Initializers);
GlobalVariable *NewGV = new GlobalVariable(
*GV->getParent(), ATy, GV->isConstant(), GV->getLinkage(), NewInit, "",
GV, GV->getThreadLocalMode(), GV->getType()->getAddressSpace(),
GV->isExternallyInitialized());
NewGV->copyAttributesFrom(GV);
NewGV->takeName(GV);
assert(GV->use_empty() && "program cannot use initializer list");
GV->eraseFromParent();
}
void ModuleLinker::upgradeMismatchedGlobalArray(StringRef Name) {
auto *DstGV = dyn_cast_or_null<GlobalVariable>(DstM->getNamedValue(Name));
if (!DstGV)
return;
auto *SrcGV = dyn_cast_or_null<GlobalVariable>(SrcM->getNamedValue(Name));
if (!SrcGV)
return;
auto *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
auto *SrcTy =
cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
if (DstTy == SrcTy)
return;
auto *DstEltTy = cast<StructType>(DstTy->getElementType());
auto *SrcEltTy = cast<StructType>(SrcTy->getElementType());
if (DstEltTy->getNumElements() == 2 && SrcEltTy->getNumElements() == 3) {
upgradeGlobalArray(DstGV);
return;
}
if (DstEltTy->getNumElements() == 3 && SrcEltTy->getNumElements() == 2)
upgradeGlobalArray(SrcGV);
}
void ModuleLinker::upgradeMismatchedGlobals() {
upgradeMismatchedGlobalArray("llvm.global_ctors");
upgradeMismatchedGlobalArray("llvm.global_dtors");
}
bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV,
const GlobalVariable *SrcGV) {
if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
return emitError("Linking globals named '" + SrcGV->getName() +
"': can only link appending global with another appending global!");
ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
ArrayType *SrcTy =
cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
Type *EltTy = DstTy->getElementType();
if (EltTy != SrcTy->getElementType())
return emitError("Appending variables with different element types!");
if (DstGV->isConstant() != SrcGV->isConstant())
return emitError("Appending variables linked with different const'ness!");
if (DstGV->getAlignment() != SrcGV->getAlignment())
return emitError(
"Appending variables with different alignment need to be linked!");
if (DstGV->getVisibility() != SrcGV->getVisibility())
return emitError(
"Appending variables with different visibility need to be linked!");
if (DstGV->hasUnnamedAddr() != SrcGV->hasUnnamedAddr())
return emitError(
"Appending variables with different unnamed_addr need to be linked!");
if (StringRef(DstGV->getSection()) != SrcGV->getSection())
return emitError(
"Appending variables with different section name need to be linked!");
uint64_t NewSize = DstTy->getNumElements() + SrcTy->getNumElements();
ArrayType *NewType = ArrayType::get(EltTy, NewSize);
GlobalVariable *NG =
new GlobalVariable(*DstGV->getParent(), NewType, SrcGV->isConstant(),
DstGV->getLinkage(), nullptr, "", DstGV,
DstGV->getThreadLocalMode(),
DstGV->getType()->getAddressSpace());
copyGVAttributes(NG, DstGV);
AppendingVarInfo AVI;
AVI.NewGV = NG;
AVI.DstInit = DstGV->getInitializer();
AVI.SrcInit = SrcGV->getInitializer();
AppendingVars.push_back(AVI);
ValueMap[SrcGV] = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
DstGV->eraseFromParent();
DoNotLinkFromSource.insert(SrcGV);
return false;
}
bool ModuleLinker::linkGlobalValueProto(GlobalValue *SGV) {
GlobalValue *DGV = getLinkedToGlobal(SGV);
assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage());
if (SGV->hasAppendingLinkage() && isPerformingImport()) {
DoNotLinkFromSource.insert(SGV);
return false;
}
if (DGV && DGV->hasAppendingLinkage())
return linkAppendingVarProto(cast<GlobalVariable>(DGV),
cast<GlobalVariable>(SGV));
bool LinkFromSrc = true;
Comdat *C = nullptr;
bool HasUnnamedAddr = SGV->hasUnnamedAddr();
if (const Comdat *SC = SGV->getComdat()) {
Comdat::SelectionKind SK;
std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
C = DstM->getOrInsertComdat(SC->getName());
C->setSelectionKind(SK);
} else if (DGV) {
if (shouldLinkFromSource(LinkFromSrc, *DGV, *SGV))
return true;
}
if (!LinkFromSrc) {
DoNotLinkFromSource.insert(SGV);
if (DGV)
ValueMap[SGV] =
ConstantExpr::getBitCast(DGV, TypeMap.get(SGV->getType()));
}
if (DGV)
HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
if (!LinkFromSrc && !DGV)
return false;
GlobalValue *NewGV;
if (!LinkFromSrc) {
NewGV = DGV;
setVisibility(NewGV, SGV, DGV);
} else {
if (!DGV && !shouldOverrideFromSrc() && SGV != ImportFunction &&
(SGV->hasLocalLinkage() || SGV->hasLinkOnceLinkage() ||
SGV->hasAvailableExternallyLinkage())) {
DoNotLinkFromSource.insert(SGV);
return false;
}
if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration())) {
DoNotLinkFromSource.insert(SGV);
return false;
}
NewGV = copyGlobalValueProto(TypeMap, SGV, DGV);
}
NewGV->setUnnamedAddr(HasUnnamedAddr);
if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
if (C)
NewGO->setComdat(C);
if (DGV && DGV->hasCommonLinkage() && SGV->hasCommonLinkage())
NewGO->setAlignment(std::max(DGV->getAlignment(), SGV->getAlignment()));
}
if (auto *NewGVar = dyn_cast<GlobalVariable>(NewGV)) {
auto *DGVar = dyn_cast_or_null<GlobalVariable>(DGV);
auto *SGVar = dyn_cast<GlobalVariable>(SGV);
if (DGVar && SGVar && DGVar->isDeclaration() && SGVar->isDeclaration() &&
(!DGVar->isConstant() || !SGVar->isConstant()))
NewGVar->setConstant(false);
}
if (NewGV != DGV) {
if (DGV) {
DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, DGV->getType()));
DGV->eraseFromParent();
}
ValueMap[SGV] = NewGV;
}
return false;
}
static void getArrayElements(const Constant *C,
SmallVectorImpl<Constant *> &Dest) {
unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
for (unsigned i = 0; i != NumElements; ++i)
Dest.push_back(C->getAggregateElement(i));
}
void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) {
SmallVector<Constant *, 16> DstElements;
getArrayElements(AVI.DstInit, DstElements);
SmallVector<Constant *, 16> SrcElements;
getArrayElements(AVI.SrcInit, SrcElements);
ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType());
StringRef Name = AVI.NewGV->getName();
bool IsNewStructor =
(Name == "llvm.global_ctors" || Name == "llvm.global_dtors") &&
cast<StructType>(NewType->getElementType())->getNumElements() == 3;
for (auto *V : SrcElements) {
if (IsNewStructor) {
Constant *Key = V->getAggregateElement(2);
if (DoNotLinkFromSource.count(Key))
continue;
}
DstElements.push_back(
MapValue(V, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer));
}
if (IsNewStructor) {
NewType = ArrayType::get(NewType->getElementType(), DstElements.size());
AVI.NewGV->mutateType(PointerType::get(NewType, 0));
}
AVI.NewGV->setInitializer(ConstantArray::get(NewType, DstElements));
}
void ModuleLinker::linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src) {
Dst.setInitializer(MapValue(Src.getInitializer(), ValueMap,
RF_MoveDistinctMDs, &TypeMap, &ValMaterializer));
}
bool ModuleLinker::linkFunctionBody(Function &Dst, Function &Src) {
assert(Dst.isDeclaration() && !Src.isDeclaration());
if (std::error_code EC = Src.materialize())
return emitError(EC.message());
if (Src.hasPrefixData())
Dst.setPrefixData(MapValue(Src.getPrefixData(), ValueMap,
RF_MoveDistinctMDs, &TypeMap, &ValMaterializer));
if (Src.hasPrologueData())
Dst.setPrologueData(MapValue(Src.getPrologueData(), ValueMap,
RF_MoveDistinctMDs, &TypeMap,
&ValMaterializer));
if (Src.hasPersonalityFn())
Dst.setPersonalityFn(MapValue(Src.getPersonalityFn(), ValueMap,
RF_MoveDistinctMDs, &TypeMap,
&ValMaterializer));
Function::arg_iterator DI = Dst.arg_begin();
for (Argument &Arg : Src.args()) {
DI->setName(Arg.getName());
ValueMap[&Arg] = &*DI;
++DI;
}
SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
Src.getAllMetadata(MDs);
for (const auto &I : MDs)
Dst.setMetadata(I.first, MapMetadata(I.second, ValueMap, RF_MoveDistinctMDs,
&TypeMap, &ValMaterializer));
Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
for (BasicBlock &BB : Dst)
for (Instruction &I : BB)
RemapInstruction(&I, ValueMap,
RF_IgnoreMissingEntries | RF_MoveDistinctMDs, &TypeMap,
&ValMaterializer);
for (Argument &Arg : Src.args())
ValueMap.erase(&Arg);
Src.dematerialize();
return false;
}
void ModuleLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) {
Constant *Aliasee = Src.getAliasee();
Constant *Val = MapValue(Aliasee, ValueMap, RF_MoveDistinctMDs, &TypeMap,
&ValMaterializer);
Dst.setAliasee(Val);
}
bool ModuleLinker::linkGlobalValueBody(GlobalValue &Src) {
Value *Dst = ValueMap[&Src];
assert(Dst);
if (shouldInternalizeLinkedSymbols())
if (auto *DGV = dyn_cast<GlobalValue>(Dst))
DGV->setLinkage(GlobalValue::InternalLinkage);
if (auto *F = dyn_cast<Function>(&Src))
return linkFunctionBody(cast<Function>(*Dst), *F);
if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {
linkGlobalInit(cast<GlobalVariable>(*Dst), *GVar);
return false;
}
linkAliasBody(cast<GlobalAlias>(*Dst), cast<GlobalAlias>(Src));
return false;
}
void ModuleLinker::linkNamedMDNodes() {
const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
for (const NamedMDNode &NMD : SrcM->named_metadata()) {
if (&NMD == SrcModFlags)
continue;
NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(NMD.getName());
for (const MDNode *op : NMD.operands())
DestNMD->addOperand(MapMetadata(op, ValueMap, RF_MoveDistinctMDs,
&TypeMap, &ValMaterializer));
}
}
bool ModuleLinker::linkModuleFlagsMetadata() {
const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
if (!SrcModFlags) return false;
NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata();
if (DstModFlags->getNumOperands() == 0) {
for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
DstModFlags->addOperand(SrcModFlags->getOperand(I));
return false;
}
DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;
SmallSetVector<MDNode*, 16> Requirements;
for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
MDNode *Op = DstModFlags->getOperand(I);
ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0));
MDString *ID = cast<MDString>(Op->getOperand(1));
if (Behavior->getZExtValue() == Module::Require) {
Requirements.insert(cast<MDNode>(Op->getOperand(2)));
} else {
Flags[ID] = std::make_pair(Op, I);
}
}
bool HasErr = false;
for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
MDNode *SrcOp = SrcModFlags->getOperand(I);
ConstantInt *SrcBehavior =
mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
MDString *ID = cast<MDString>(SrcOp->getOperand(1));
MDNode *DstOp;
unsigned DstIndex;
std::tie(DstOp, DstIndex) = Flags.lookup(ID);
unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
if (SrcBehaviorValue == Module::Require) {
if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
DstModFlags->addOperand(SrcOp);
}
continue;
}
if (!DstOp) {
Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands());
DstModFlags->addOperand(SrcOp);
continue;
}
ConstantInt *DstBehavior =
mdconst::extract<ConstantInt>(DstOp->getOperand(0));
unsigned DstBehaviorValue = DstBehavior->getZExtValue();
if (DstBehaviorValue == Module::Override) {
if (SrcBehaviorValue == Module::Override &&
SrcOp->getOperand(2) != DstOp->getOperand(2)) {
HasErr |= emitError("linking module flags '" + ID->getString() +
"': IDs have conflicting override values");
}
continue;
} else if (SrcBehaviorValue == Module::Override) {
DstModFlags->setOperand(DstIndex, SrcOp);
Flags[ID].first = SrcOp;
continue;
}
if (SrcBehaviorValue != DstBehaviorValue) {
HasErr |= emitError("linking module flags '" + ID->getString() +
"': IDs have conflicting behaviors");
continue;
}
auto replaceDstValue = [&](MDNode *New) {
Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
MDNode *Flag = MDNode::get(DstM->getContext(), FlagOps);
DstModFlags->setOperand(DstIndex, Flag);
Flags[ID].first = Flag;
};
switch (SrcBehaviorValue) {
case Module::Require:
case Module::Override: llvm_unreachable("not possible");
case Module::Error: {
if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
HasErr |= emitError("linking module flags '" + ID->getString() +
"': IDs have conflicting values");
}
continue;
}
case Module::Warning: {
if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
emitWarning("linking module flags '" + ID->getString() +
"': IDs have conflicting values");
}
continue;
}
case Module::Append: {
MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
SmallVector<Metadata *, 8> MDs;
MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands());
MDs.append(DstValue->op_begin(), DstValue->op_end());
MDs.append(SrcValue->op_begin(), SrcValue->op_end());
replaceDstValue(MDNode::get(DstM->getContext(), MDs));
break;
}
case Module::AppendUnique: {
SmallSetVector<Metadata *, 16> Elts;
MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
Elts.insert(DstValue->op_begin(), DstValue->op_end());
Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
replaceDstValue(MDNode::get(DstM->getContext(),
makeArrayRef(Elts.begin(), Elts.end())));
break;
}
}
}
for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
MDNode *Requirement = Requirements[I];
MDString *Flag = cast<MDString>(Requirement->getOperand(0));
Metadata *ReqValue = Requirement->getOperand(1);
MDNode *Op = Flags[Flag].first;
if (!Op || Op->getOperand(2) != ReqValue) {
HasErr |= emitError("linking module flags '" + Flag->getString() +
"': does not have the required value");
continue;
}
}
return HasErr;
}
static bool triplesMatch(const Triple &T0, const Triple &T1) {
if (T0.getVendor() == Triple::Apple)
return T0.getArch() == T1.getArch() &&
T0.getSubArch() == T1.getSubArch() &&
T0.getVendor() == T1.getVendor() &&
T0.getOS() == T1.getOS();
return T0 == T1;
}
static std::string mergeTriples(const Triple &SrcTriple, const Triple &DstTriple) {
if (SrcTriple.getVendor() == Triple::Apple)
if (DstTriple.isOSVersionLT(SrcTriple))
return SrcTriple.str();
return DstTriple.str();
}
bool ModuleLinker::run() {
assert(DstM && "Null destination module");
assert(SrcM && "Null source module");
if (DstM->getDataLayout().isDefault())
DstM->setDataLayout(SrcM->getDataLayout());
if (SrcM->getDataLayout() != DstM->getDataLayout()) {
emitWarning("Linking two modules of different data layouts: '" +
SrcM->getModuleIdentifier() + "' is '" +
SrcM->getDataLayoutStr() + "' whereas '" +
DstM->getModuleIdentifier() + "' is '" +
DstM->getDataLayoutStr() + "'\n");
}
if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
DstM->setTargetTriple(SrcM->getTargetTriple());
Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM->getTargetTriple());
if (!SrcM->getTargetTriple().empty() && !triplesMatch(SrcTriple, DstTriple))
emitWarning("Linking two modules of different target triples: " +
SrcM->getModuleIdentifier() + "' is '" +
SrcM->getTargetTriple() + "' whereas '" +
DstM->getModuleIdentifier() + "' is '" +
DstM->getTargetTriple() + "'\n");
DstM->setTargetTriple(mergeTriples(SrcTriple, DstTriple));
if (!SrcM->getModuleInlineAsm().empty()) {
if (DstM->getModuleInlineAsm().empty())
DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm());
else
DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+"\n"+
SrcM->getModuleInlineAsm());
}
computeTypeMapping();
ComdatsChosen.clear();
for (const auto &SMEC : SrcM->getComdatSymbolTable()) {
const Comdat &C = SMEC.getValue();
if (ComdatsChosen.count(&C))
continue;
Comdat::SelectionKind SK;
bool LinkFromSrc;
if (getComdatResult(&C, SK, LinkFromSrc))
return true;
ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc);
}
upgradeMismatchedGlobals();
for (GlobalVariable &GV : SrcM->globals())
if (linkGlobalValueProto(&GV))
return true;
for (Function &F :*SrcM)
if (linkGlobalValueProto(&F))
return true;
for (GlobalAlias &GA : SrcM->aliases())
if (linkGlobalValueProto(&GA))
return true;
for (const AppendingVarInfo &AppendingVar : AppendingVars)
linkAppendingVarInit(AppendingVar);
for (const auto &Entry : DstM->getComdatSymbolTable()) {
const Comdat &C = Entry.getValue();
if (C.getSelectionKind() == Comdat::Any)
continue;
const GlobalValue *GV = SrcM->getNamedValue(C.getName());
if (GV)
MapValue(GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
}
for (Function &SF : *SrcM) {
if (SF.isDeclaration())
continue;
if (DoNotLinkFromSource.count(&SF))
continue;
if (isPerformingImport() && &SF != ImportFunction)
continue;
if (linkGlobalValueBody(SF))
return true;
}
for (GlobalAlias &Src : SrcM->aliases()) {
if (DoNotLinkFromSource.count(&Src))
continue;
linkGlobalValueBody(Src);
}
for (GlobalVariable &Src : SrcM->globals()) {
if (!Src.hasInitializer() || DoNotLinkFromSource.count(&Src))
continue;
linkGlobalValueBody(Src);
}
while (!LazilyLinkGlobalValues.empty()) {
GlobalValue *SGV = LazilyLinkGlobalValues.back();
LazilyLinkGlobalValues.pop_back();
if (isPerformingImport() && !doImportAsDefinition(SGV))
continue;
if (shouldLinkOnlyNeeded() && SGV->isDeclaration())
continue;
assert(!SGV->isDeclaration() && "users should not pass down decls");
if (linkGlobalValueBody(*SGV))
return true;
}
DoneLinkingBodies = true;
linkNamedMDNodes();
if (linkModuleFlagsMetadata())
return true;
return false;
}
Linker::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P)
: ETypes(E), IsPacked(P) {}
Linker::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)
: ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
bool Linker::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {
if (IsPacked != That.IsPacked)
return false;
if (ETypes != That.ETypes)
return false;
return true;
}
bool Linker::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {
return !this->operator==(That);
}
StructType *Linker::StructTypeKeyInfo::getEmptyKey() {
return DenseMapInfo<StructType *>::getEmptyKey();
}
StructType *Linker::StructTypeKeyInfo::getTombstoneKey() {
return DenseMapInfo<StructType *>::getTombstoneKey();
}
unsigned Linker::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),
Key.IsPacked);
}
unsigned Linker::StructTypeKeyInfo::getHashValue(const StructType *ST) {
return getHashValue(KeyTy(ST));
}
bool Linker::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
const StructType *RHS) {
if (RHS == getEmptyKey() || RHS == getTombstoneKey())
return false;
return LHS == KeyTy(RHS);
}
bool Linker::StructTypeKeyInfo::isEqual(const StructType *LHS,
const StructType *RHS) {
if (RHS == getEmptyKey())
return LHS == getEmptyKey();
if (RHS == getTombstoneKey())
return LHS == getTombstoneKey();
return KeyTy(LHS) == KeyTy(RHS);
}
void Linker::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {
assert(!Ty->isOpaque());
NonOpaqueStructTypes.insert(Ty);
}
void Linker::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) {
assert(!Ty->isOpaque());
NonOpaqueStructTypes.insert(Ty);
bool Removed = OpaqueStructTypes.erase(Ty);
(void)Removed;
assert(Removed);
}
void Linker::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {
assert(Ty->isOpaque());
OpaqueStructTypes.insert(Ty);
}
StructType *
Linker::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes,
bool IsPacked) {
Linker::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);
auto I = NonOpaqueStructTypes.find_as(Key);
if (I == NonOpaqueStructTypes.end())
return nullptr;
return *I;
}
bool Linker::IdentifiedStructTypeSet::hasType(StructType *Ty) {
if (Ty->isOpaque())
return OpaqueStructTypes.count(Ty);
auto I = NonOpaqueStructTypes.find(Ty);
if (I == NonOpaqueStructTypes.end())
return false;
return *I == Ty;
}
void Linker::init(Module *M, DiagnosticHandlerFunction DiagnosticHandler) {
this->Composite = M;
this->DiagnosticHandler = DiagnosticHandler;
TypeFinder StructTypes;
StructTypes.run(*M, true);
for (StructType *Ty : StructTypes) {
if (Ty->isOpaque())
IdentifiedStructTypes.addOpaque(Ty);
else
IdentifiedStructTypes.addNonOpaque(Ty);
}
}
Linker::Linker(Module *M, DiagnosticHandlerFunction DiagnosticHandler) {
init(M, DiagnosticHandler);
}
Linker::Linker(Module *M) {
init(M, [this](const DiagnosticInfo &DI) {
Composite->getContext().diagnose(DI);
});
}
void Linker::deleteModule() {
delete Composite;
Composite = nullptr;
}
bool Linker::linkInModule(Module *Src, unsigned Flags, FunctionInfoIndex *Index,
Function *FuncToImport) {
ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src,
DiagnosticHandler, Flags, Index, FuncToImport);
bool RetCode = TheLinker.run();
Composite->dropTriviallyDeadConstantArrays();
return RetCode;
}
void Linker::setModule(Module *Dst) {
init(Dst, DiagnosticHandler);
}
bool Linker::LinkModules(Module *Dest, Module *Src,
DiagnosticHandlerFunction DiagnosticHandler,
unsigned Flags) {
Linker L(Dest, DiagnosticHandler);
return L.linkInModule(Src, Flags);
}
bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Flags) {
Linker L(Dest);
return L.linkInModule(Src, Flags);
}
LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
LLVMLinkerMode Unused, char **OutMessages) {
Module *D = unwrap(Dest);
std::string Message;
raw_string_ostream Stream(Message);
DiagnosticPrinterRawOStream DP(Stream);
LLVMBool Result = Linker::LinkModules(
D, unwrap(Src), [&](const DiagnosticInfo &DI) { DI.print(DP); });
if (OutMessages && Result) {
Stream.flush();
*OutMessages = strdup(Message.c_str());
}
return Result;
}