-
- assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
- "Global must either be external or have an initializer!");
-
- bool SGExtern = SGV->isExternal();
- bool DGExtern = DGV ? DGV->isExternal() : false;
-
- if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) {
- // 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);
-
- // 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.insert(std::make_pair(SGV, NewDGV));
- if (SGV->hasAppendingLinkage())
- // Keep track that this is an appending variable...
- AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
-
- } else if (SGV->isExternal()) {
- // If SGV is external or if both SGV & DGV are external.. Just link the
- // external globals, we aren't adding anything.
- ValueMap.insert(std::make_pair(SGV, DGV));
-
- // Inherit 'const' information.
- if (SGV->isConstant()) DGV->setConstant(true);
-
- } else if (DGV->isExternal()) { // If DGV is external but SGV is not...
- ValueMap.insert(std::make_pair(SGV, DGV));
- DGV->setLinkage(SGV->getLinkage()); // Inherit linkage!
-
- if (DGV->isConstant() && !SGV->isConstant())
- return Error(Err, "Linking globals named '" + SGV->getName() +
- "': declaration is const but definition is not!");
-
- // Inherit 'const' information.
- if (SGV->isConstant()) DGV->setConstant(true);
-
- } else if (SGV->hasWeakLinkage() || SGV->hasLinkOnceLinkage()) {
- // At this point we know that DGV has LinkOnce, Appending, Weak, or
- // External linkage. If DGV is Appending, this is an error.
- if (DGV->hasAppendingLinkage())
- return Error(Err, "Linking globals named '" + SGV->getName() +
- "' with 'weak' and 'appending' linkage is not allowed!");
-
- if (SGV->isConstant() != DGV->isConstant())
- return Error(Err, "Global Variable Collision on '" +
- ToStr(SGV->getType(), Src) + " %" + SGV->getName() +
- "' - Global variables differ in const'ness");
-
- // Otherwise, just perform the link.
- ValueMap.insert(std::make_pair(SGV, DGV));
-
- // Linkonce+Weak = Weak
- if (DGV->hasLinkOnceLinkage() && SGV->hasWeakLinkage())
- DGV->setLinkage(SGV->getLinkage());
-
- } else if (DGV->hasWeakLinkage() || DGV->hasLinkOnceLinkage()) {
- // At this point we know that SGV has LinkOnce, Appending, or External
- // linkage. If SGV is Appending, this is an error.
- if (SGV->hasAppendingLinkage())
- return Error(Err, "Linking globals named '" + SGV->getName() +
- " ' with 'weak' and 'appending' linkage is not allowed!");
-
- if (SGV->isConstant() != DGV->isConstant())
- return Error(Err, "Global Variable Collision on '" +
- ToStr(SGV->getType(), Src) + " %" + SGV->getName() +
- "' - Global variables differ in const'ness");
-
- if (!SGV->hasLinkOnceLinkage())
- DGV->setLinkage(SGV->getLinkage()); // Inherit linkage!
- ValueMap.insert(std::make_pair(SGV, DGV));
-
- } else if (SGV->getLinkage() != DGV->getLinkage()) {
- return Error(Err, "Global variables named '" + SGV->getName() +
- "' have different linkage specifiers!");
- // Inherit 'const' information.
- if (SGV->isConstant()) DGV->setConstant(true);
-
- } else if (SGV->hasExternalLinkage()) {
- // Allow linking two exactly identical external global variables...
- if (SGV->isConstant() != DGV->isConstant())
- return Error(Err, "Global Variable Collision on '" +
- ToStr(SGV->getType(), Src) + " %" + SGV->getName() +
- "' - Global variables differ in const'ness");
-
- if (SGV->getInitializer() != DGV->getInitializer())
- return Error(Err, "Global Variable Collision on '" +
- ToStr(SGV->getType(), Src) + " %" + SGV->getName() +
- "' - External linkage globals have different initializers");
-
- ValueMap.insert(std::make_pair(SGV, DGV));
- } else if (SGV->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);
-
- // Make sure to remember this mapping...
- ValueMap.insert(std::make_pair(SGV, NewDGV));
-
- // Keep track that this is an appending variable...
- AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));