From 875c10a4c600c0546f929adaa5f68f47236dfccb Mon Sep 17 00:00:00 2001 From: Edd Barrett Date: Mon, 18 Mar 2024 18:35:18 +0000 Subject: [PATCH] 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). --- llvm/lib/YkIR/YkIRWriter.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/llvm/lib/YkIR/YkIRWriter.cpp b/llvm/lib/YkIR/YkIRWriter.cpp index bae14be7f3d888..0ad6a255928d98 100644 --- a/llvm/lib/YkIR/YkIRWriter.cpp +++ b/llvm/lib/YkIR/YkIRWriter.cpp @@ -751,6 +751,21 @@ class YkIRWriter { serialiseGlobal(G); } + // Now that we've finished serialising globals, add a global (immutable, for + // now) array to the LLVM module containing pointers to all the global + // variables. We will use this to find the addresses of globals at runtime. + // The indices of the array correspond with `GlobalDeclIdx`s in the AOT IR. + vector GlobalsAsConsts; + for (llvm::GlobalVariable *G : Globals) { + GlobalsAsConsts.push_back(cast(G)); + } + ArrayType *GlobalsArrayTy = + ArrayType::get(PointerType::get(M.getContext(), 0), Globals.size()); + GlobalVariable *GlobalsArray = new GlobalVariable( + M, GlobalsArrayTy, true, GlobalValue::LinkageTypes::ExternalLinkage, + ConstantArray::get(GlobalsArrayTy, GlobalsAsConsts)); + GlobalsArray->setName("__yk_globalvar_ptrs"); + // num_types: OutStreamer.emitSizeT(Types.size()); // types: