Skip to content

Commit 875c10a

Browse files
committed
Inject a "global pointers array".
This change adds (to the AOT LLVM module) an array containing the addresses of all global variables in the module. We can use this to find the addresses of global variables (by index) without leaning on dlsym(3).
1 parent c15739a commit 875c10a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

llvm/lib/YkIR/YkIRWriter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,21 @@ class YkIRWriter {
751751
serialiseGlobal(G);
752752
}
753753

754+
// Now that we've finished serialising globals, add a global (immutable, for
755+
// now) array to the LLVM module containing pointers to all the global
756+
// variables. We will use this to find the addresses of globals at runtime.
757+
// The indices of the array correspond with `GlobalDeclIdx`s in the AOT IR.
758+
vector<llvm::Constant *> GlobalsAsConsts;
759+
for (llvm::GlobalVariable *G : Globals) {
760+
GlobalsAsConsts.push_back(cast<llvm::Constant>(G));
761+
}
762+
ArrayType *GlobalsArrayTy =
763+
ArrayType::get(PointerType::get(M.getContext(), 0), Globals.size());
764+
GlobalVariable *GlobalsArray = new GlobalVariable(
765+
M, GlobalsArrayTy, true, GlobalValue::LinkageTypes::ExternalLinkage,
766+
ConstantArray::get(GlobalsArrayTy, GlobalsAsConsts));
767+
GlobalsArray->setName("__yk_globalvar_ptrs");
768+
754769
// num_types:
755770
OutStreamer.emitSizeT(Types.size());
756771
// types:

0 commit comments

Comments
 (0)