X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FLinker%2FLinkModules.cpp;h=001db21555ccddc841f0ad1982afc4838c7120a9;hb=58b1ac76d470eb5faa7e98feae97c4906d4d146e;hp=f4ebe6b10c015a953a4137603c45e7ae44969f20;hpb=832b2a9cd8870211bf2d347d7b435beacbb06c8d;p=oota-llvm.git diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index f4ebe6b10c0..001db21555c 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -1,4 +1,4 @@ -//===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===// + //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===// // // The LLVM Compiler Infrastructure // @@ -415,7 +415,7 @@ static void ForceRenaming(GlobalValue *GV, const std::string &Name) { // If there is a conflict, rename the conflict. if (GlobalValue *ConflictGV = cast_or_null(ST.lookup(Name))) { - assert(ConflictGV->hasInternalLinkage() && + assert(ConflictGV->hasLocalLinkage() && "Not conflicting with a static global, should link instead!"); GV->takeName(ConflictGV); ConflictGV->setName(Name); // This will cause ConflictGV to get renamed @@ -444,7 +444,7 @@ static void CopyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) { static bool GetLinkageResult(GlobalValue *Dest, const GlobalValue *Src, GlobalValue::LinkageTypes <, bool &LinkFromSrc, std::string *Err) { - assert((!Dest || !Src->hasInternalLinkage()) && + assert((!Dest || !Src->hasLocalLinkage()) && "If Src has internal linkage, Dest shouldn't be set!"); if (!Dest) { // Linking something to nothing. @@ -477,7 +477,7 @@ static bool GetLinkageResult(GlobalValue *Dest, const GlobalValue *Src, "': can only link appending global with another appending global!"); LinkFromSrc = true; // Special cased. LT = Src->getLinkage(); - } else if (Src->isWeakForLinker()) { + } else if (Src->mayBeOverridden()) { // At this point we know that Dest has LinkOnce, External*, Weak, Common, // or DLL* linkage. if ((Dest->hasLinkOnceLinkage() && @@ -489,7 +489,7 @@ static bool GetLinkageResult(GlobalValue *Dest, const GlobalValue *Src, LinkFromSrc = false; LT = Dest->getLinkage(); } - } else if (Dest->isWeakForLinker()) { + } else if (Dest->mayBeOverridden()) { // At this point we know that Src has External* or DLL* linkage. if (Src->hasExternalWeakLinkage()) { LinkFromSrc = false; @@ -536,13 +536,13 @@ static bool LinkGlobals(Module *Dest, const Module *Src, // Check to see if may have to link the global with the global, alias or // function. - if (SGV->hasName() && !SGV->hasInternalLinkage()) + if (SGV->hasName() && !SGV->hasLocalLinkage()) DGV = cast_or_null(DestSymTab.lookup(SGV->getNameStart(), SGV->getNameEnd())); // If we found a global with the same name in the dest module, but it has // internal linkage, we are really not doing any linkage here. - if (DGV && DGV->hasInternalLinkage()) + if (DGV && DGV->hasLocalLinkage()) DGV = 0; // If types don't agree due to opaque types, try to resolve them. @@ -573,7 +573,7 @@ static bool LinkGlobals(Module *Dest, const Module *Src, // 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->hasInternalLinkage() && NewDGV->getName() != SGV->getName()) + if (!NewDGV->hasLocalLinkage() && NewDGV->getName() != SGV->getName()) ForceRenaming(NewDGV, SGV->getName()); // Make sure to remember this mapping. @@ -643,7 +643,7 @@ static bool LinkGlobals(Module *Dest, const Module *Src, // If the symbol table renamed the global, but it is an externally visible // symbol, DGV must be an existing global with internal linkage. Rename. - if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage()) + if (NewDGV->getName() != SGV->getName() && !NewDGV->hasLocalLinkage()) ForceRenaming(NewDGV, SGV->getName()); // Inherit const as appropriate. @@ -662,12 +662,15 @@ static bool LinkGlobals(Module *Dest, const Module *Src, if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant()) DGVar->setConstant(true); - // 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 (isa(DGV) && !SGV->isDeclaration()) - return Error(Err, "Global-Alias Collision on '" + SGV->getName() + - "': symbol multiple defined"); + // SGV is global, but DGV is alias. + if (isa(DGV)) { + // The only valid mappings are: + // - SGV is external declaration, which is effectively a no-op. + // - SGV is weak, when we just need to throw SGV out. + if (!SGV->isDeclaration() && !SGV->mayBeOverridden()) + return Error(Err, "Global-Alias Collision on '" + SGV->getName() + + "': symbol multiple defined"); + } // Set calculated linkage DGV->setLinkage(NewLinkage); @@ -684,10 +687,12 @@ CalculateAliasLinkage(const GlobalValue *SGV, const GlobalValue *DGV) { return GlobalValue::ExternalLinkage; else if (SGV->hasWeakLinkage() || DGV->hasWeakLinkage()) return GlobalValue::WeakLinkage; - else { - assert(SGV->hasInternalLinkage() && DGV->hasInternalLinkage() && - "Unexpected linkage type"); + else if (SGV->hasInternalLinkage() && DGV->hasInternalLinkage()) return GlobalValue::InternalLinkage; + else { + assert (SGV->hasPrivateLinkage() && DGV->hasPrivateLinkage() && + "Unexpected linkage type"); + return GlobalValue::PrivateLinkage; } } @@ -712,7 +717,7 @@ static bool LinkAlias(Module *Dest, const Module *Src, GlobalValue* DGV = NULL; // Try to find something 'similar' to SGA in destination module. - if (!DGV && !SGA->hasInternalLinkage()) { + if (!DGV && !SGA->hasLocalLinkage()) { DGV = Dest->getNamedAlias(SGA->getName()); // If types don't agree due to opaque types, try to resolve them. @@ -720,7 +725,7 @@ static bool LinkAlias(Module *Dest, const Module *Src, RecursiveResolveTypes(SGA->getType(), DGV->getType()); } - if (!DGV && !SGA->hasInternalLinkage()) { + if (!DGV && !SGA->hasLocalLinkage()) { DGV = Dest->getGlobalVariable(SGA->getName()); // If types don't agree due to opaque types, try to resolve them. @@ -728,7 +733,7 @@ static bool LinkAlias(Module *Dest, const Module *Src, RecursiveResolveTypes(SGA->getType(), DGV->getType()); } - if (!DGV && !SGA->hasInternalLinkage()) { + if (!DGV && !SGA->hasLocalLinkage()) { DGV = Dest->getFunction(SGA->getName()); // If types don't agree due to opaque types, try to resolve them. @@ -737,7 +742,7 @@ static bool LinkAlias(Module *Dest, const Module *Src, } // No linking to be performed on internal stuff. - if (DGV && DGV->hasInternalLinkage()) + if (DGV && DGV->hasLocalLinkage()) DGV = NULL; if (GlobalAlias *DGA = dyn_cast_or_null(DGV)) { @@ -757,7 +762,7 @@ static bool LinkAlias(Module *Dest, const Module *Src, } else if (GlobalVariable *DGVar = dyn_cast_or_null(DGV)) { // The only allowed way is to link alias with external declaration or weak // symbol.. - if (DGVar->isDeclaration() || DGVar->isWeakForLinker()) { + if (DGVar->isDeclaration() || DGVar->mayBeOverridden()) { // But only if aliasee is global too... if (!isa(DAliasee)) return Error(Err, "Global-Alias Collision on '" + SGA->getName() + @@ -786,7 +791,7 @@ static bool LinkAlias(Module *Dest, const Module *Src, } else if (Function *DF = dyn_cast_or_null(DGV)) { // The only allowed way is to link alias with external declaration or weak // symbol... - if (DF->isDeclaration() || DF->isWeakForLinker()) { + if (DF->isDeclaration() || DF->mayBeOverridden()) { // But only if aliasee is function too... if (!isa(DAliasee)) return Error(Err, "Function-Alias Collision on '" + SGA->getName() + @@ -828,7 +833,7 @@ static bool LinkAlias(Module *Dest, const Module *Src, // If the symbol table renamed the alias, but it is an externally visible // symbol, DGA must be an global value with internal linkage. Rename it. if (NewGA->getName() != SGA->getName() && - !NewGA->hasInternalLinkage()) + !NewGA->hasLocalLinkage()) ForceRenaming(NewGA, SGA->getName()); // Remember this mapping so uses in the source module get remapped @@ -854,28 +859,39 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src, // Figure out what the initializer looks like in the dest module... Constant *SInit = cast(RemapOperand(SGV->getInitializer(), ValueMap)); - - GlobalVariable *DGV = - cast(ValueMap[SGV]->stripPointerCasts()); - if (DGV->hasInitializer()) { - if (SGV->hasExternalLinkage()) { - if (DGV->getInitializer() != SInit) - return Error(Err, "Global Variable Collision on '" + SGV->getName() + - "': global variables have different initializers"); - } else if (DGV->isWeakForLinker()) { - // Nothing is required, mapped values will take the new global - // automatically. - } else if (SGV->isWeakForLinker()) { - // Nothing is required, mapped values will take the new global - // automatically. - } else if (DGV->hasAppendingLinkage()) { - assert(0 && "Appending linkage unimplemented!"); + // Grab destination global variable or alias. + GlobalValue *DGV = cast(ValueMap[SGV]->stripPointerCasts()); + + // If dest if global variable, check that initializers match. + if (GlobalVariable *DGVar = dyn_cast(DGV)) { + if (DGVar->hasInitializer()) { + if (SGV->hasExternalLinkage()) { + if (DGVar->getInitializer() != SInit) + return Error(Err, "Global Variable Collision on '" + + SGV->getName() + + "': global variables have different initializers"); + } else if (DGVar->mayBeOverridden()) { + // Nothing is required, mapped values will take the new global + // automatically. + } else if (SGV->mayBeOverridden()) { + // Nothing is required, mapped values will take the new global + // automatically. + } else if (DGVar->hasAppendingLinkage()) { + assert(0 && "Appending linkage unimplemented!"); + } else { + assert(0 && "Unknown linkage!"); + } } else { - assert(0 && "Unknown linkage!"); + // Copy the initializer over now... + DGVar->setInitializer(SInit); } } else { - // Copy the initializer over now... - DGV->setInitializer(SInit); + // Destination is alias, the only valid situation is when source is + // weak. Also, note, that we already checked linkage in LinkGlobals(), + // thus we assert here. + // FIXME: Should we weaken this assumption, 'dereference' alias and + // check for initializer of aliasee? + assert(SGV->mayBeOverridden()); } } } @@ -898,13 +914,13 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src, // Check to see if may have to link the function with the global, alias or // function. - if (SF->hasName() && !SF->hasInternalLinkage()) + if (SF->hasName() && !SF->hasLocalLinkage()) DGV = cast_or_null(DestSymTab.lookup(SF->getNameStart(), SF->getNameEnd())); // If we found a global with the same name in the dest module, but it has // internal linkage, we are really not doing any linkage here. - if (DGV && DGV->hasInternalLinkage()) + if (DGV && DGV->hasLocalLinkage()) DGV = 0; // If types don't agree due to opaque types, try to resolve them. @@ -929,7 +945,7 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src, // If the LLVM runtime renamed the function, but it is an externally // visible symbol, DF must be an existing function with internal linkage. // Rename it. - if (!NewDF->hasInternalLinkage() && NewDF->getName() != SF->getName()) + if (!NewDF->hasLocalLinkage() && NewDF->getName() != SF->getName()) ForceRenaming(NewDF, SF->getName()); // ... and remember this mapping... @@ -968,7 +984,7 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src, // If the symbol table renamed the function, but it is an externally // visible symbol, DF must be an existing function with internal // linkage. Rename it. - if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage()) + if (NewDF->getName() != SF->getName() && !NewDF->hasLocalLinkage()) ForceRenaming(NewDF, SF->getName()); // Remember this mapping so uses in the source module get remapped @@ -984,7 +1000,7 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src, // The only valid mappings are: // - SF is external declaration, which is effectively a no-op. // - SF is weak, when we just need to throw SF out. - if (!SF->isDeclaration()) + if (!SF->isDeclaration() && !SF->mayBeOverridden()) return Error(Err, "Function-Alias Collision on '" + SF->getName() + "': symbol multiple defined"); } @@ -1169,7 +1185,7 @@ static bool LinkAppendingVars(Module *M, static bool ResolveAliases(Module *Dest) { for (Module::alias_iterator I = Dest->alias_begin(), E = Dest->alias_end(); I != E; ++I) - if (const GlobalValue *GV = I->resolveAliasedGlobal(/*traverseWeak*/ false)) + if (const GlobalValue *GV = I->resolveAliasedGlobal()) if (GV != I && !GV->isDeclaration()) I->replaceAllUsesWith(const_cast(GV));