Skip to content

Commit b13997b

Browse files
committed
[AArch64][PAC] Do not fold ptrauth wrappers corresponding to extern_weak
When parsing IR, we do some optimizations very early - for example, the `constantFoldCompareGlobalToNull` function folds comparisons against null pointer. If the value has `extern_weak` linkage or if it is an llvm.ptrauth wrapper corresponding to a symbol with `extern_weak` linkage, the folding is prevented. When the llvm.ptrauth wrapper was defined after its usage in such condition, its uses (including the use in the condition) were updated when the wrapper definition was found, but before its section was parsed. Since the "llvm.ptrauth" section name indicates that the value is an llvm.ptrauth wrapper, not having it parsed before updating usages made the folding code think that this is not an llvm.ptrauth wrapper, which led to undesired folding. This patch moves the code updating uses of the global value after the code parsing its section name.
1 parent 2a88245 commit b13997b

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,16 +1256,6 @@ bool LLParser::parseGlobal(const std::string &Name, LocTy NameLoc,
12561256
GV->setThreadLocalMode(TLM);
12571257
GV->setUnnamedAddr(UnnamedAddr);
12581258

1259-
if (GVal) {
1260-
if (GVal->getAddressSpace() != AddrSpace)
1261-
return error(
1262-
TyLoc,
1263-
"forward reference and definition of global have different types");
1264-
1265-
GVal->replaceAllUsesWith(GV);
1266-
GVal->eraseFromParent();
1267-
}
1268-
12691259
// parse attributes on the global.
12701260
while (Lex.getKind() == lltok::comma) {
12711261
Lex.Lex();
@@ -1303,6 +1293,16 @@ bool LLParser::parseGlobal(const std::string &Name, LocTy NameLoc,
13031293
}
13041294
}
13051295

1296+
if (GVal) {
1297+
if (GVal->getAddressSpace() != AddrSpace)
1298+
return error(
1299+
TyLoc,
1300+
"forward reference and definition of global have different types");
1301+
1302+
GVal->replaceAllUsesWith(GV);
1303+
GVal->eraseFromParent();
1304+
}
1305+
13061306
AttrBuilder Attrs(M->getContext());
13071307
LocTy BuiltinLoc;
13081308
std::vector<unsigned> FwdRefAttrGrps;

llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-elf-reloc.ll

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ define i8* @test_global_weak() #0 {
106106
ret i8* %tmp0
107107
}
108108

109-
; FIXME: if we define this after function definition, the function body
110-
; transorms to a single call to foo_weak via bl.
111-
@foo_weak.ptrauth.ia.0 = private constant { ptr, i32, i64, i64 } { ptr @foo_weak, i32 0, i64 0, i64 0 }, section "llvm.ptrauth"
112-
113109
define void @test_foo_weak() #0 {
114110
; CHECK-LABEL: test_foo_weak:
115111
; CHECK: // %bb.0:
@@ -152,8 +148,6 @@ define i8* @test_global_strong_def() #0 {
152148

153149
@g_weak = extern_weak global i32
154150

155-
declare extern_weak void @foo_weak() #0
156-
157151
; For ELF, we should specify dso_local explicitly for strong definition, otherwise the symbol would be assumed preemptible, and GOT load would be used
158152
@g_strong_def = dso_local constant i32 42
159153

@@ -234,6 +228,10 @@ declare extern_weak void @foo_weak() #0
234228
; CHECK-NEXT: g_strong_def.ref.da.0:
235229
; CHECK-NEXT: .xword g_strong_def@AUTH(da,0)
236230

231+
declare extern_weak void @foo_weak() #0
232+
233+
@foo_weak.ptrauth.ia.0 = private constant { ptr, i32, i64, i64 } { ptr @foo_weak, i32 0, i64 0, i64 0 }, section "llvm.ptrauth"
234+
237235
@g_strong_def.ptrauth.da.0 = private constant { i8*, i32, i64, i64 } { i8* bitcast (i32* @g_strong_def to i8*), i32 2, i64 0, i64 0 }, section "llvm.ptrauth"
238236

239237
@g_strong_def.ref.da.0 = dso_local constant i8* bitcast ({ i8*, i32, i64, i64 }* @g_strong_def.ptrauth.da.0 to i8*)

0 commit comments

Comments
 (0)