llvm.org GIT mirror llvm / 33845aa
Prevent alias from pointing to weak aliases. 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@204781 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
18 changed file(s) with 62 addition(s) and 65 deletion(s). Raw diff Collapse all Expand all
679679 the aliasee.
680680
681681 The aliasee must be a definition.
682
683 Aliases are not allowed to point to aliases with linkages that can be
684 overridden. Since they are only a second name, the possibility of the
685 intermediate alias being overridden cannot be represented in an object file.
682686
683687 .. _namedmetadatastructure:
684688
6363 Constant *getAliasee() {
6464 return getOperand(0);
6565 }
66 /// getAliasedGlobal() - Aliasee can be either global or bitcast of
67 /// global. This method retrives the global for both aliasee flavours.
66
67 /// This method tries to ultimately resolve the alias by going through the
68 /// aliasing chain and trying to find the very last global. Returns NULL if a
69 /// cycle was found.
6870 GlobalValue *getAliasedGlobal();
6971 const GlobalValue *getAliasedGlobal() const {
7072 return const_cast(this)->getAliasedGlobal();
71 }
72
73 /// resolveAliasedGlobal() - This method tries to ultimately resolve the alias
74 /// by going through the aliasing chain and trying to find the very last
75 /// global. Returns NULL if a cycle was found. If stopOnWeak is false, then
76 /// the whole chain aliasing chain is traversed, otherwise - only strong
77 /// aliases.
78 GlobalValue *resolveAliasedGlobal(bool stopOnWeak = true);
79 const GlobalValue *resolveAliasedGlobal(bool stopOnWeak = true) const {
80 return const_cast(this)->resolveAliasedGlobal(stopOnWeak);
8173 }
8274
8375 static bool isValidLinkage(LinkageTypes L) {
11711171 if (!GVar) {
11721172 // If GV is an alias then use the aliasee for determining thread-localness.
11731173 if (const GlobalAlias *GA = dyn_cast(GV))
1174 GVar = dyn_cast_or_null(GA->resolveAliasedGlobal(false));
1174 GVar = dyn_cast_or_null(GA->getAliasedGlobal());
11751175 }
11761176
11771177 unsigned Opc;
687687 return TheJIT->getOrEmitGlobalVariable(GV);
688688
689689 if (GlobalAlias *GA = dyn_cast(V))
690 return TheJIT->getPointerToGlobal(GA->resolveAliasedGlobal(false));
690 return TheJIT->getPointerToGlobal(GA->getAliasedGlobal());
691691
692692 // If we have already compiled the function, return a pointer to its body.
693693 Function *F = cast(V);
235235 setOperand(0, Aliasee);
236236 }
237237
238 GlobalValue *GlobalAlias::getAliasedGlobal() {
239 Constant *C = getAliasee();
240 if (C == 0) return 0;
241
238 static GlobalValue *getAliaseeGV(GlobalAlias *GA) {
239 Constant *C = GA->getAliasee();
240 assert(C && "Must alias something");
241
242242 if (GlobalValue *GV = dyn_cast(C))
243243 return GV;
244244
247247 CE->getOpcode() == Instruction::AddrSpaceCast ||
248248 CE->getOpcode() == Instruction::GetElementPtr) &&
249249 "Unsupported aliasee");
250
250
251251 return cast(CE->getOperand(0));
252252 }
253253
254 GlobalValue *GlobalAlias::resolveAliasedGlobal(bool stopOnWeak) {
254 GlobalValue *GlobalAlias::getAliasedGlobal() {
255255 SmallPtrSet Visited;
256256
257 // Check if we need to stop early.
258 if (stopOnWeak && mayBeOverridden())
259 return this;
260
261 GlobalValue *GV = getAliasedGlobal();
262 Visited.insert(GV);
263
264 // Iterate over aliasing chain, stopping on weak alias if necessary.
265 while (GlobalAlias *GA = dyn_cast(GV)) {
266 if (stopOnWeak && GA->mayBeOverridden())
267 break;
268
269 GV = GA->getAliasedGlobal();
270
257 GlobalAlias *GA = this;
258
259 for (;;) {
260 GlobalValue *GV = getAliaseeGV(GA);
271261 if (!Visited.insert(GV))
272262 return 0;
273 }
274
275 return GV;
276 }
263
264 // Iterate over aliasing chain.
265 GA = dyn_cast(GV);
266 if (!GA)
267 return GV;
268 }
269 }
501501 }
502502 }
503503 Assert1(!GV->isDeclaration(), "Alias must point to a definition", &GA);
504
505 const GlobalValue* Resolved = GA.resolveAliasedGlobal(/*stopOnWeak*/ false);
506 Assert1(Resolved,
507 "Aliasing chain should end with function or global variable", &GA);
504 if (const GlobalAlias *GAAliasee = dyn_cast(GV)) {
505 Assert1(!GAAliasee->mayBeOverridden(), "Alias cannot point to a weak alias",
506 &GA);
507 }
508
509 const GlobalValue *AG = GA.getAliasedGlobal();
510 Assert1(AG, "Aliasing chain should end with function or global variable",
511 &GA);
508512
509513 visitGlobalValue(GA);
510514 }
380380 if (MO.isGlobal()) {
381381 const GlobalValue *GValue = MO.getGlobal();
382382 const GlobalAlias *GAlias = dyn_cast(GValue);
383 const GlobalValue *RealGValue = GAlias ?
384 GAlias->resolveAliasedGlobal(false) : GValue;
383 const GlobalValue *RealGValue =
384 GAlias ? GAlias->getAliasedGlobal() : GValue;
385385 MOSymbol = getSymbol(RealGValue);
386386 const GlobalVariable *GVar = dyn_cast(RealGValue);
387387 IsExternal = GVar && !GVar->hasInitializer();
427427 else if (MO.isGlobal()) {
428428 const GlobalValue *GValue = MO.getGlobal();
429429 const GlobalAlias *GAlias = dyn_cast(GValue);
430 const GlobalValue *RealGValue = GAlias ?
431 GAlias->resolveAliasedGlobal(false) : GValue;
430 const GlobalValue *RealGValue =
431 GAlias ? GAlias->getAliasedGlobal() : GValue;
432432 MOSymbol = getSymbol(RealGValue);
433433 const GlobalVariable *GVar = dyn_cast(RealGValue);
434434
462462 if (MO.isGlobal()) {
463463 const GlobalValue *GValue = MO.getGlobal();
464464 const GlobalAlias *GAlias = dyn_cast(GValue);
465 const GlobalValue *RealGValue = GAlias ?
466 GAlias->resolveAliasedGlobal(false) : GValue;
465 const GlobalValue *RealGValue =
466 GAlias ? GAlias->getAliasedGlobal() : GValue;
467467 MOSymbol = getSymbol(RealGValue);
468468 const GlobalVariable *GVar = dyn_cast(RealGValue);
469469 IsExternal = GVar && !GVar->hasInitializer();
18621862 if (!GVar) {
18631863 // If GV is an alias, use the aliasee for determining thread-locality.
18641864 if (const GlobalAlias *GA = dyn_cast(GV))
1865 GVar = dyn_cast_or_null(GA->resolveAliasedGlobal(false));
1865 GVar = dyn_cast_or_null(GA->getAliasedGlobal());
18661866 }
18671867
18681868 // FIXME: We don't yet handle the complexity of TLS.
14121412 if (GlobalAddressSDNode *G = dyn_cast(GA)) {
14131413 const GlobalValue *GValue = G->getGlobal();
14141414 const GlobalAlias *GAlias = dyn_cast(GValue);
1415 const GlobalValue *RealGValue = GAlias ?
1416 GAlias->resolveAliasedGlobal(false) : GValue;
1415 const GlobalValue *RealGValue =
1416 GAlias ? GAlias->getAliasedGlobal() : GValue;
14171417 const GlobalVariable *GVar = dyn_cast(RealGValue);
14181418 assert((GVar || isa(RealGValue)) &&
14191419 "Unexpected global value subclass!");
128128 // If GV is an alias then use the aliasee for determining
129129 // thread-localness.
130130 if (const GlobalAlias *GA = dyn_cast(GV))
131 GV = GA->resolveAliasedGlobal(false);
131 GV = GA->getAliasedGlobal();
132132 const GlobalVariable *Var = cast(GV);
133133
134134 bool isLocal = Var->hasLocalLinkage();
362362 // it works...).
363363 if (const GlobalAlias *GA = dyn_cast(GV))
364364 if (const GlobalVariable *GVar =
365 dyn_cast_or_null(GA->resolveAliasedGlobal(false)))
365 dyn_cast_or_null(GA->getAliasedGlobal()))
366366 if (GVar->isThreadLocal())
367367 return false;
368368
85088508 // If GV is an alias then use the aliasee for determining
85098509 // thread-localness.
85108510 if (const GlobalAlias *GA = dyn_cast(GV))
8511 GV = GA->resolveAliasedGlobal(false);
8511 GV = GA->getAliasedGlobal();
85128512 SDLoc dl(GA);
85138513 SDValue Chain = DAG.getEntryNode();
85148514
276276 const GlobalValue *UnderlyingGV = GV;
277277 // If GV is an alias then use the aliasee to determine the wrapper type
278278 if (const GlobalAlias *GA = dyn_cast(GV))
279 UnderlyingGV = GA->resolveAliasedGlobal();
279 UnderlyingGV = GA->getAliasedGlobal();
280280 if (const GlobalVariable *GVar = dyn_cast(UnderlyingGV)) {
281281 if ( ( GVar->isConstant() &&
282282 UnderlyingGV->isLocalLinkage(GV->getLinkage()) )
553553 ++i;
554554 // Don't stop on weak. We assume people aren't playing games with the
555555 // instrumentedness of overridden weak aliases.
556 if (Function *F = dyn_cast(
557 GA->resolveAliasedGlobal(/*stopOnWeak=*/false))) {
556 if (Function *F = dyn_cast(GA->getAliasedGlobal())) {
558557 bool GAInst = isInstrumented(GA), FInst = isInstrumented(F);
559558 if (GAInst && FInst) {
560559 addGlobalNamePrefix(GA);
6464 @alias2 = dllexport alias void()* @f1
6565
6666 ; CHECK: .globl alias3
67 ; CHECK: alias3 = alias
67 ; CHECK: alias3 = notExported
6868 @alias3 = dllexport alias void()* @alias
6969
7070 ; CHECK: .weak weak_alias
7979 @alias2 = dllexport alias void()* @f1
8080
8181 ; CHECK: .globl _alias3
82 ; CHECK: _alias3 = _alias
82 ; CHECK: _alias3 = _notExported
8383 @alias3 = dllexport alias void()* @alias
8484
8585 ; CHECK: .weak _weak_alias
1313 define i32 @foo_f() {
1414 ret i32 0
1515 }
16 @bar_f = alias weak %FunTy* @foo_f
16 @bar_f = alias weak_odr %FunTy* @foo_f
1717 @bar_ff = alias i32()* @bar_f
1818
1919 @bar_i = alias internal i32* @bar
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
11
22 @foo1 = alias void ()* @foo2
3 ; CHECK: @foo1 = alias void ()* @foo2
3 ; CHECK: @foo1 = alias void ()* @bar2
44
5 @foo2 = alias weak void()* @bar1
6 ; CHECK: @foo2 = alias weak void ()* @bar2
5 @foo2 = alias void()* @bar1
6 ; CHECK: @foo2 = alias void ()* @bar2
77
88 @bar1 = alias void ()* @bar2
99 ; CHECK: @bar1 = alias void ()* @bar2
10
11 @weak1 = alias weak void ()* @bar2
12 ; CHECK: @weak1 = alias weak void ()* @bar2
1013
1114 define void @bar2() {
1215 ret void
1619 define void @baz() {
1720 entry:
1821 call void @foo1()
19 ; CHECK: call void @foo2()
22 ; CHECK: call void @bar2()
2023
2124 call void @foo2()
22 ; CHECK: call void @foo2()
25 ; CHECK: call void @bar2()
2326
2427 call void @bar1()
2528 ; CHECK: call void @bar2()
2629
30 call void @weak1()
31 ; CHECK: call void @weak1()
2732 ret void
2833 }
2934