Skip to content

Commit 219b873

Browse files
committed
[IR] Allow uses of llvm.global_ctors and llvm.global_dtors
With PAuth enabled, signed function pointers in init/fini arrays are going to be represented with `ptrauth` constants. To support address discrimination for such signed pointers, we need to fill the storage address with `getelementptr` referencing the array (`llvm.global_ctors` or `llvm.global_dtors`) itself. Such uses of these special arrays were previously disallowed since `appendToGlobal{C|D}tors` did not update uses after construction of a new array. This patch implements such update logic. This will be tested in a follow-up patch introducing PAuth support for function pointers in init/fini arrays since such signed pointers with address discrimination are currently the only entities which have such uses needed to be updated.
1 parent 17e51d5 commit 219b873

File tree

3 files changed

+10
-22
lines changed

3 files changed

+10
-22
lines changed

llvm/lib/IR/Verifier.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,8 +850,6 @@ void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
850850
GV.getName() == "llvm.global_dtors")) {
851851
Check(!GV.hasInitializer() || GV.hasAppendingLinkage(),
852852
"invalid linkage for intrinsic global variable", &GV);
853-
Check(GV.materialized_use_empty(),
854-
"invalid uses of intrinsic global variable", &GV);
855853

856854
// Don't worry about emitting an error for it not being an array,
857855
// visitGlobalValue will complain on appending non-array.

llvm/lib/Transforms/Utils/ModuleUtils.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ static void appendToGlobalArray(StringRef ArrayName, Module &M, Function *F,
3535
// to the list.
3636
SmallVector<Constant *, 16> CurrentCtors;
3737
StructType *EltTy;
38-
if (GlobalVariable *GVCtor = M.getNamedGlobal(ArrayName)) {
38+
GlobalVariable *GVCtor = M.getNamedGlobal(ArrayName);
39+
if (GVCtor) {
3940
EltTy = cast<StructType>(GVCtor->getValueType()->getArrayElementType());
4041
if (Constant *Init = GVCtor->getInitializer()) {
4142
unsigned n = Init->getNumOperands();
4243
CurrentCtors.reserve(n + 1);
4344
for (unsigned i = 0; i != n; ++i)
4445
CurrentCtors.push_back(cast<Constant>(Init->getOperand(i)));
4546
}
46-
GVCtor->eraseFromParent();
47+
GVCtor->removeFromParent();
4748
} else {
4849
EltTy = StructType::get(IRB.getInt32Ty(),
4950
PointerType::get(FnTy, F->getAddressSpace()),
@@ -67,8 +68,13 @@ static void appendToGlobalArray(StringRef ArrayName, Module &M, Function *F,
6768

6869
// Create the new global variable and replace all uses of
6970
// the old global variable with the new one.
70-
(void)new GlobalVariable(M, NewInit->getType(), false,
71-
GlobalValue::AppendingLinkage, NewInit, ArrayName);
71+
auto *NewGVCtor =
72+
new GlobalVariable(M, NewInit->getType(), false,
73+
GlobalValue::AppendingLinkage, NewInit, ArrayName);
74+
if (GVCtor)
75+
for (Use &U : make_early_inc_range(GVCtor->uses()))
76+
U.set(NewGVCtor);
77+
delete GVCtor;
7278
}
7379

7480
void llvm::appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data) {

llvm/test/Verifier/global-ctors-dtors-uses.ll

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)