Prevent alias from pointing to weak aliases.
authorRafael Espindola <rafael.espindola@gmail.com>
Thu, 27 Mar 2014 15:26:56 +0000 (15:26 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Thu, 27 Mar 2014 15:26:56 +0000 (15:26 +0000)
This adds back r204781.

Original message:

Aliases are just another name for a position in a file. As such, the
regular symbol resolutions are not applied. For example, given

define void @my_func() {
  ret void
}
@my_alias = alias weak void ()* @my_func
@my_alias2 = alias void ()* @my_alias

We produce without this patch:

        .weak   my_alias
my_alias = my_func
        .globl  my_alias2
my_alias2 = my_alias

That is, in the resulting ELF file my_alias, my_func and my_alias are
just 3 names pointing to offset 0 of .text. That is *not* the
semantics of IR linking. For example, linking in a

@my_alias = alias void ()* @other_func

would require the strong my_alias to override the weak one and
my_alias2 would end up pointing to other_func.

There is no way to represent that with aliases being just another
name, so the best solution seems to be to just disallow it, converting
a miscompile into an error.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204934 91177308-0d34-0410-b5e6-96231b3b80d8

18 files changed:
docs/LangRef.rst
include/llvm/IR/GlobalAlias.h
lib/CodeGen/SelectionDAG/SelectionDAG.cpp
lib/ExecutionEngine/JIT/JITEmitter.cpp
lib/IR/Globals.cpp
lib/IR/Verifier.cpp
lib/Target/PowerPC/PPCAsmPrinter.cpp
lib/Target/PowerPC/PPCFastISel.cpp
lib/Target/PowerPC/PPCISelDAGToDAG.cpp
lib/Target/TargetMachine.cpp
lib/Target/X86/X86FastISel.cpp
lib/Target/X86/X86ISelLowering.cpp
lib/Target/XCore/XCoreISelLowering.cpp
lib/Transforms/Instrumentation/DataFlowSanitizer.cpp
test/CodeGen/X86/dllexport-x86_64.ll
test/CodeGen/X86/dllexport.ll
test/Feature/aliases.ll
test/Transforms/GlobalOpt/alias-resolve.ll

index adf3266b02962109b274d0e0913a224983d38fb2..91692ad41edcf57cd4202467d79a213c9c956187 100644 (file)
@@ -681,6 +681,10 @@ the aliasee.
 
 The aliasee must be a definition.
 
+Aliases are not allowed to point to aliases with linkages that can be
+overridden. Since they are only a second name, the possibility of the
+intermediate alias being overridden cannot be represented in an object file.
+
 .. _namedmetadatastructure:
 
 Named Metadata
index 81777af38696e69d8da3285098d29f7ad4bbd282..2ca481a515f93d773595888f8f0b4bdaa5055b21 100644 (file)
@@ -64,23 +64,15 @@ public:
   Constant *getAliasee() {
     return getOperand(0);
   }
-  /// getAliasedGlobal() - Aliasee can be either global or bitcast of
-  /// global. This method retrives the global for both aliasee flavours.
+
+  /// This method tries to ultimately resolve the alias by going through the
+  /// aliasing chain and trying to find the very last global. Returns NULL if a
+  /// cycle was found.
   GlobalValue *getAliasedGlobal();
   const GlobalValue *getAliasedGlobal() const {
     return const_cast<GlobalAlias *>(this)->getAliasedGlobal();
   }
 
-  /// resolveAliasedGlobal() - This method tries to ultimately resolve the alias
-  /// by going through the aliasing chain and trying to find the very last
-  /// global. Returns NULL if a cycle was found. If stopOnWeak is false, then
-  /// the whole chain aliasing chain is traversed, otherwise - only strong
-  /// aliases.
-  GlobalValue *resolveAliasedGlobal(bool stopOnWeak = true);
-  const GlobalValue *resolveAliasedGlobal(bool stopOnWeak = true) const {
-    return const_cast<GlobalAlias *>(this)->resolveAliasedGlobal(stopOnWeak);
-  }
-
   static bool isValidLinkage(LinkageTypes L) {
     return isExternalLinkage(L) || isLocalLinkage(L) ||
       isWeakLinkage(L) || isLinkOnceLinkage(L);
index 61961d8ca2d5540c0c2575b379fbb367f8f4472d..52806235fd2c46aad4d754f9713589fcddcfb9b5 100644 (file)
@@ -1172,7 +1172,7 @@ SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
   if (!GVar) {
     // If GV is an alias then use the aliasee for determining thread-localness.
     if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
-      GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
+      GVar = dyn_cast_or_null<GlobalVariable>(GA->getAliasedGlobal());
   }
 
   unsigned Opc;
index 4b2f012eafb25ee7bdb8c39c7a4153ea03f0017a..9d215ec96aee7263a59f39411680e5931858d0dc 100644 (file)
@@ -688,7 +688,7 @@ void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference,
     return TheJIT->getOrEmitGlobalVariable(GV);
 
   if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
-    return TheJIT->getPointerToGlobal(GA->resolveAliasedGlobal(false));
+    return TheJIT->getPointerToGlobal(GA->getAliasedGlobal());
 
   // If we have already compiled the function, return a pointer to its body.
   Function *F = cast<Function>(V);
index 11152d5d6c21c9a941a62a192e0d36414f5abe00..f338dd76aaa8ac2dd2a4a0e68bc94d5d02299ce9 100644 (file)
@@ -236,10 +236,10 @@ void GlobalAlias::setAliasee(Constant *Aliasee) {
   setOperand(0, Aliasee);
 }
 
-GlobalValue *GlobalAlias::getAliasedGlobal() {
-  Constant *C = getAliasee();
-  if (C == 0) return 0;
-  
+static GlobalValue *getAliaseeGV(GlobalAlias *GA) {
+  Constant *C = GA->getAliasee();
+  assert(C && "Must alias something");
+
   if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
     return GV;
 
@@ -248,30 +248,23 @@ GlobalValue *GlobalAlias::getAliasedGlobal() {
           CE->getOpcode() == Instruction::AddrSpaceCast ||
           CE->getOpcode() == Instruction::GetElementPtr) &&
          "Unsupported aliasee");
-  
+
   return cast<GlobalValue>(CE->getOperand(0));
 }
 
-GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) {
+GlobalValue *GlobalAlias::getAliasedGlobal() {
   SmallPtrSet<GlobalValue*, 3> Visited;
 
-  // Check if we need to stop early.
-  if (stopOnWeak && mayBeOverridden())
-    return this;
-
-  GlobalValue *GV = getAliasedGlobal();
-  Visited.insert(GV);
-
-  // Iterate over aliasing chain, stopping on weak alias if necessary.
-  while (GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) {
-    if (stopOnWeak && GA->mayBeOverridden())
-      break;
-
-    GV = GA->getAliasedGlobal();
+  GlobalAlias *GA = this;
 
+  for (;;) {
+    GlobalValue *GV = getAliaseeGV(GA);
     if (!Visited.insert(GV))
       return 0;
-  }
 
-  return GV;
+    // Iterate over aliasing chain.
+    GA = dyn_cast<GlobalAlias>(GV);
+    if (!GA)
+      return GV;
+  }
 }
index 50c6ae204e93549f699e621aaa8bfa2e36010846..f5c8ac623b135ecfe590158f6aa6e58cc0cfb8a7 100644 (file)
@@ -502,10 +502,14 @@ void Verifier::visitGlobalAlias(const GlobalAlias &GA) {
     }
   }
   Assert1(!GV->isDeclaration(), "Alias must point to a definition", &GA);
+  if (const GlobalAlias *GAAliasee = dyn_cast<GlobalAlias>(GV)) {
+    Assert1(!GAAliasee->mayBeOverridden(), "Alias cannot point to a weak alias",
+            &GA);
+  }
 
-  const GlobalValue* Resolved = GA.resolveAliasedGlobal(/*stopOnWeak*/ false);
-  Assert1(Resolved,
-          "Aliasing chain should end with function or global variable", &GA);
+  const GlobalValue *AG = GA.getAliasedGlobal();
+  Assert1(AG, "Aliasing chain should end with function or global variable",
+          &GA);
 
   visitGlobalValue(GA);
 }
index daf90c86ef92c0df896d36e6b66990cabb21ec59..9ce8ea911cf0ee64a4cadb5a4d2ac316838a4efc 100644 (file)
@@ -381,8 +381,8 @@ void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
     if (MO.isGlobal()) {
       const GlobalValue *GValue = MO.getGlobal();
       const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
-      const GlobalValue *RealGValue = GAlias ?
-        GAlias->resolveAliasedGlobal(false) : GValue;
+      const GlobalValue *RealGValue =
+          GAlias ? GAlias->getAliasedGlobal() : GValue;
       MOSymbol = getSymbol(RealGValue);
       const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
       IsExternal = GVar && !GVar->hasInitializer();
@@ -428,8 +428,8 @@ void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
     else if (MO.isGlobal()) {
       const GlobalValue *GValue = MO.getGlobal();
       const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
-      const GlobalValue *RealGValue = GAlias ?
-        GAlias->resolveAliasedGlobal(false) : GValue;
+      const GlobalValue *RealGValue =
+          GAlias ? GAlias->getAliasedGlobal() : GValue;
       MOSymbol = getSymbol(RealGValue);
       const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
     
@@ -463,8 +463,8 @@ void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
     if (MO.isGlobal()) {
       const GlobalValue *GValue = MO.getGlobal();
       const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
-      const GlobalValue *RealGValue = GAlias ?
-        GAlias->resolveAliasedGlobal(false) : GValue;
+      const GlobalValue *RealGValue =
+          GAlias ? GAlias->getAliasedGlobal() : GValue;
       MOSymbol = getSymbol(RealGValue);
       const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
       IsExternal = GVar && !GVar->hasInitializer();
index 4753160e0913c3e2f0a73bd5ed7522729060274c..dd45683032ab7cfc5e1d9101eb0a602134df3292 100644 (file)
@@ -1863,7 +1863,7 @@ unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
   if (!GVar) {
     // If GV is an alias, use the aliasee for determining thread-locality.
     if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
-      GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
+      GVar = dyn_cast_or_null<GlobalVariable>(GA->getAliasedGlobal());
   }
 
   // FIXME: We don't yet handle the complexity of TLS.
index d216026d2e7eafa6be518f6df3b4b601a734fcf0..b8a042503d2e412f3aa90636e4788583adc9643f 100644 (file)
@@ -1463,8 +1463,8 @@ SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
     if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
       const GlobalValue *GValue = G->getGlobal();
       const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
-      const GlobalValue *RealGValue = GAlias ?
-        GAlias->resolveAliasedGlobal(false) : GValue;
+      const GlobalValue *RealGValue =
+          GAlias ? GAlias->getAliasedGlobal() : GValue;
       const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
       assert((GVar || isa<Function>(RealGValue)) &&
              "Unexpected global value subclass!");
index f8a58ec2922c26d627f26ea582ee23fbe3382a33..fe3c870c980a16aebdbf9c635230c270857f08dc 100644 (file)
@@ -129,7 +129,7 @@ TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
   // If GV is an alias then use the aliasee for determining
   // thread-localness.
   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
-    GV = GA->resolveAliasedGlobal(false);
+    GV = GA->getAliasedGlobal();
   const GlobalVariable *Var = cast<GlobalVariable>(GV);
 
   bool isLocal = Var->hasLocalLinkage();
index c3e002082dd09f284fec7ed31e50ccacac4e7d17..9c57f0d1f195125e01349621a97bb4cc90751051 100644 (file)
@@ -363,7 +363,7 @@ bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
     // it works...).
     if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
       if (const GlobalVariable *GVar =
-            dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
+              dyn_cast_or_null<GlobalVariable>(GA->getAliasedGlobal()))
         if (GVar->isThreadLocal())
           return false;
 
index ec923ba7aa276f6fac573b04725bbcf570bacc35..630fba01519497c0323ab222ee1fd1ad59cd6bbe 100644 (file)
@@ -8509,7 +8509,7 @@ X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
     // If GV is an alias then use the aliasee for determining
     // thread-localness.
     if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
-      GV = GA->resolveAliasedGlobal(false);
+      GV = GA->getAliasedGlobal();
     SDLoc dl(GA);
     SDValue Chain = DAG.getEntryNode();
 
index 079e886457bcf0059abf12fa24df97b110e5ee19..1b74013ac18bbf0f2e2b7415dc008b66828cd101 100644 (file)
@@ -277,7 +277,7 @@ getGlobalAddressWrapper(SDValue GA, const GlobalValue *GV,
   const GlobalValue *UnderlyingGV = GV;
   // If GV is an alias then use the aliasee to determine the wrapper type
   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
-    UnderlyingGV = GA->resolveAliasedGlobal();
+    UnderlyingGV = GA->getAliasedGlobal();
   if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(UnderlyingGV)) {
     if (  ( GVar->isConstant() &&
             UnderlyingGV->isLocalLinkage(GV->getLinkage()) )
index 0249df1c6d8378d9d5f23d33072de966c0a9a197..df1549d405ce4e6259146547246edb27c63f91e7 100644 (file)
@@ -554,8 +554,7 @@ bool DataFlowSanitizer::runOnModule(Module &M) {
     ++i;
     // Don't stop on weak.  We assume people aren't playing games with the
     // instrumentedness of overridden weak aliases.
-    if (Function *F = dyn_cast<Function>(
-            GA->resolveAliasedGlobal(/*stopOnWeak=*/false))) {
+    if (Function *F = dyn_cast<Function>(GA->getAliasedGlobal())) {
       bool GAInst = isInstrumented(GA), FInst = isInstrumented(F);
       if (GAInst && FInst) {
         addGlobalNamePrefix(GA);
index 1fadfec2543f9926c3956eb26bc42f5a68481ff2..a38c2d8f8abe1f0a2da41fa8a89ade561ade5d12 100644 (file)
@@ -65,7 +65,7 @@ define weak_odr dllexport void @weak1() {
 @alias2 = dllexport alias void()* @f1
 
 ; CHECK: .globl alias3
-; CHECK: alias3 = alias
+; CHECK: alias3 = notExported
 @alias3 = dllexport alias void()* @alias
 
 ; CHECK: .weak weak_alias
index 7f0dadb6a2ee79d93f71f3f05a6ece5aa682e271..1b34d238693941541acd07833a49b24d47e7af43 100644 (file)
@@ -80,7 +80,7 @@ define weak_odr dllexport void @weak1() {
 @alias2 = dllexport alias void()* @f1
 
 ; CHECK: .globl _alias3
-; CHECK: _alias3 = _alias
+; CHECK: _alias3 = _notExported
 @alias3 = dllexport alias void()* @alias
 
 ; CHECK: .weak _weak_alias
index 7ffa5f2601361a21057c5dd434918852ccd71c39..7fe9d0b374ec59e658f1132f21ab81a6d9203b10 100644 (file)
@@ -14,7 +14,7 @@
 define i32 @foo_f() {
   ret i32 0
 }
-@bar_f = alias weak %FunTy* @foo_f
+@bar_f = alias weak_odr %FunTy* @foo_f
 @bar_ff = alias i32()* @bar_f
 
 @bar_i = alias internal i32* @bar
index 64e3d88af8aa721d1d9f18bab91d8fb85fe4a06d..2d5a956d14b1266467f49f7f005478dafe9ba4b0 100644 (file)
@@ -1,14 +1,17 @@
 ; RUN: opt < %s -globalopt -S | FileCheck %s
 
 @foo1 = alias void ()* @foo2
-; CHECK: @foo1 = alias void ()* @foo2
+; CHECK: @foo1 = alias void ()* @bar2
 
-@foo2 = alias weak void()* @bar1
-; CHECK: @foo2 = alias weak void ()* @bar2
+@foo2 = alias void()* @bar1
+; CHECK: @foo2 = alias void ()* @bar2
 
 @bar1  = alias void ()* @bar2
 ; CHECK: @bar1 = alias void ()* @bar2
 
+@weak1 = alias weak void ()* @bar2
+; CHECK: @weak1 = alias weak void ()* @bar2
+
 define void @bar2() {
   ret void
 }
@@ -17,14 +20,16 @@ define void @bar2() {
 define void @baz() {
 entry:
          call void @foo1()
-; CHECK: call void @foo2()
+; CHECK: call void @bar2()
 
          call void @foo2()
-; CHECK: call void @foo2()
+; CHECK: call void @bar2()
 
          call void @bar1()
 ; CHECK: call void @bar2()
 
+         call void @weak1()
+; CHECK: call void @weak1()
          ret void
 }