- if (!DGV) {
- // No linking to be performed, simply create an identical version of the
- // symbol over in the dest module... the initializer will be filled in
- // later by LinkGlobalInits...
- GlobalVariable *NewDGV =
- new GlobalVariable(SGV->getType()->getElementType(),
- SGV->isConstant(), SGV->getLinkage(), /*init*/0,
- SGV->getName(), Dest, false,
- SGV->getType()->getAddressSpace());
- // Propagate alignment, visibility and section info.
- CopyGVAttributes(NewDGV, SGV);
-
- // If the LLVM runtime renamed the global, but it is an externally visible
- // symbol, DGV must be an existing global with internal linkage. Rename
- // it.
- if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage())
- ForceRenaming(NewDGV, SGV->getName());
-
- // Make sure to remember this mapping...
- ValueMap[SGV] = NewDGV;
-
- if (SGV->hasAppendingLinkage())
- // Keep track that this is an appending variable...
- AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
- } else if (DGV->hasAppendingLinkage()) {
- // No linking is performed yet. Just insert a new copy of the global, and
- // keep track of the fact that it is an appending variable in the
- // AppendingVars map. The name is cleared out so that no linkage is
- // performed.
- GlobalVariable *NewDGV =
- new GlobalVariable(SGV->getType()->getElementType(),
- SGV->isConstant(), SGV->getLinkage(), /*init*/0,
- "", Dest, false,
- SGV->getType()->getAddressSpace());
-
- // Set alignment allowing CopyGVAttributes merge it with alignment of SGV.
- NewDGV->setAlignment(DGV->getAlignment());
- // Propagate alignment, section and visibility info.
- CopyGVAttributes(NewDGV, SGV);
-
- // Make sure to remember this mapping...
- ValueMap[SGV] = NewDGV;
-
- // Keep track that this is an appending variable...
- AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
- } else if (GlobalAlias *DGA = dyn_cast<GlobalAlias>(DGV)) {
- // SGV is global, but DGV is alias. The only valid mapping is when SGV is
- // external declaration, which is effectively a no-op. Also make sure
- // linkage calculation was correct.
- if (SGV->isDeclaration() && !LinkFromSrc) {
- // Make sure to remember this mapping...
- ValueMap[SGV] = DGA;
- } else
- return Error(Err, "Global-Alias Collision on '" + SGV->getName() +
- "': symbol multiple defined");
- } else if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV)) {
- // Otherwise, perform the global-global mapping as instructed by
- // GetLinkageResult.
- if (LinkFromSrc) {
- // Propagate alignment, section, and visibility info.
- CopyGVAttributes(DGVar, SGV);
-
- // If the types don't match, and if we are to link from the source, nuke
- // DGV and create a new one of the appropriate type.
- if (SGV->getType() != DGVar->getType()) {
- GlobalVariable *NewDGV =
- new GlobalVariable(SGV->getType()->getElementType(),
- DGVar->isConstant(), DGVar->getLinkage(),
- /*init*/0, DGVar->getName(), Dest, false,
- SGV->getType()->getAddressSpace());
- CopyGVAttributes(NewDGV, DGVar);
- DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV,
- DGVar->getType()));
- // DGVar will conflict with NewDGV because they both had the same
- // name. We must erase this now so ForceRenaming doesn't assert
- // because DGV might not have internal linkage.
- DGVar->eraseFromParent();
-
- // If the symbol table renamed the global, but it is an externally
- // visible symbol, DGV must be an existing global with internal
- // linkage. Rename it.
- if (NewDGV->getName() != SGV->getName() &&
- !NewDGV->hasInternalLinkage())
- ForceRenaming(NewDGV, SGV->getName());
-
- DGVar = NewDGV;
- }
-
- // Inherit const as appropriate
- DGVar->setConstant(SGV->isConstant());
-
- // Set initializer to zero, so we can link the stuff later
- DGVar->setInitializer(0);
- } else {
- // Special case for const propagation