Fix pr20078.

When linking llvm.global_ctors with the optional third element we have to handle
it specially and only copy the elements whose keys were also copied.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217281 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2014-09-05 21:27:52 +00:00
parent ea388503c4
commit e8b19acded
3 changed files with 46 additions and 5 deletions

View File

@ -1223,14 +1223,34 @@ static void getArrayElements(Constant *C, SmallVectorImpl<Constant*> &Dest) {
void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) {
// Merge the initializer.
SmallVector<Constant*, 16> Elements;
getArrayElements(AVI.DstInit, Elements);
SmallVector<Constant *, 16> DstElements;
getArrayElements(AVI.DstInit, DstElements);
Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap, &ValMaterializer);
getArrayElements(SrcInit, Elements);
SmallVector<Constant *, 16> SrcElements;
getArrayElements(AVI.SrcInit, SrcElements);
ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType());
AVI.NewGV->setInitializer(ConstantArray::get(NewType, Elements));
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_None, &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));
}
/// linkGlobalInits - Update the initializers in the Dest module now that all

View File

@ -0,0 +1,6 @@
@v = weak global i8 1
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @f, i8* @v}]
define weak void @f() {
ret void
}

15
test/Linker/ctors.ll Normal file
View File

@ -0,0 +1,15 @@
; RUN: llvm-link %s %p/Inputs/ctors.ll -S -o - | \
; RUN: FileCheck --check-prefix=ALL --check-prefix=CHECK1 %s
; RUN: llvm-link %p/Inputs/ctors.ll %s -S -o - | \
; RUN: FileCheck --check-prefix=ALL --check-prefix=CHECK2 %s
@v = weak global i8 0
; CHECK1: @v = weak global i8 0
; CHECK2: @v = weak global i8 1
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @f, i8* @v }]
; ALL: @llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @f, i8* @v }]
define weak void @f() {
ret void
}