Index: llvm/lib/Linker/IRMover.cpp =================================================================== --- llvm/lib/Linker/IRMover.cpp +++ llvm/lib/Linker/IRMover.cpp @@ -868,7 +868,7 @@ } uint64_t DstNumElements = 0; - if (DstGV) { + if (DstGV && !DstGV->isDeclaration()) { ArrayType *DstTy = cast(DstGV->getValueType()); DstNumElements = DstTy->getNumElements(); @@ -928,9 +928,9 @@ Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType())); - Mapper.scheduleMapAppendingVariable(*NG, - DstGV ? DstGV->getInitializer() : nullptr, - IsOldStructor, SrcElements); + Mapper.scheduleMapAppendingVariable( + *NG, DstGV && !DstGV->isDeclaration() ? DstGV->getInitializer() : nullptr, + IsOldStructor, SrcElements); // Replace any uses of the two global variables with uses of the new // global. @@ -983,7 +983,6 @@ DGV = nullptr; // Handle the ultra special appending linkage case first. - assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage()); if (SGV->hasAppendingLinkage()) return linkAppendingVarProto(cast_or_null(DGV), cast(SGV)); Index: llvm/test/Linker/Inputs/appending-global.ll =================================================================== --- /dev/null +++ llvm/test/Linker/Inputs/appending-global.ll @@ -0,0 +1 @@ +@var = appending global [1 x i8* ] undef Index: llvm/test/Linker/appending-global-proto.ll =================================================================== --- /dev/null +++ llvm/test/Linker/appending-global-proto.ll @@ -0,0 +1,11 @@ +; RUN: llvm-link %s %p/Inputs/appending-global.ll -S -o - | FileCheck %s +; RUN: llvm-link %p/Inputs/appending-global.ll %s -S -o - | FileCheck %s + +; Checks that we can link global variable with appending linkage with the +; existing external declaration. + +; CHECK-DAG: @var = appending global [1 x i8*] undef +; CHECK-DAG: @use = global [1 x i8*] [i8* bitcast ([1 x i8*]* @var to i8*)] + +@var = external global i8* +@use = global [1 x i8*] [i8* bitcast (i8** @var to i8*)]