@@ -808,11 +808,12 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
808808 cache_gbl = nothing
809809 end
810810
811- gv_to_value = Dict {String, Any } ()
811+ gv_to_value = Dict {String, Ptr{Cvoid} } ()
812812
813+ # The caller is responsible for initializing global variables that
814+ # point to global values or bindings with their address in memory.
815+ # For Julia < v"1.13" to enable relocation we strip out the initializers here.
813816 if VERSION >= v " 1.13.0-DEV.623"
814- # Since Julia 1.13, the caller is responsible for initializing global variables that
815- # point to global values or bindings with their address in memory.
816817 num_gvars = Ref {Csize_t} (0 )
817818 @ccall jl_get_llvm_gvs (native_code:: Ptr{Cvoid} , num_gvars:: Ptr{Csize_t} ,
818819 C_NULL :: Ptr{Cvoid} ):: Nothing
@@ -826,12 +827,7 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
826827
827828 for (gv_ref, init) in zip (gvs, inits)
828829 gv = GlobalVariable (gv_ref)
829- val = const_inttoptr (ConstantInt (Int64 (init)), LLVM. PointerType ())
830- initializer! (gv, val)
831-
832- # TODO : jl_binding_t?
833- @show LLVM. name (gv), val
834- gv_to_value[LLVM. name (gv)] = Base. unsafe_pointer_to_objref (val)
830+ gv_to_value[LLVM. name (gv)] = init
835831 end
836832 else
837833 # Prior to this version of Julia we only had access to the values that the global variables
@@ -870,11 +866,15 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
870866 end
871867 ptr = reinterpret (Ptr{Cvoid}, convert (UInt, init))
872868 if ptr in gvalues
873- gv_to_value[LLVM. name (gv)] = Base . unsafe_pointer_to_objref ( ptr)
869+ gv_to_value[LLVM. name (gv)] = ptr
874870 end
871+ LLVM. initializer! (gv, nothing )
875872 end
876873 @assert length (gv_to_value) == length (gvalues)
877874 end
875+ # It's valid to call Base.unsafe_pointer_to_objref on values(gv_to_value),
876+ # but we may not be able to "easily" obtain the pointer back later.
877+ # (Types, etc, disallow Base.pointer_from_objref on them.)
878878
879879 if VERSION >= v " 1.13.0-DEV.1120"
880880 # on sufficiently recent versions of Julia, we can query the CIs compiled.
0 commit comments