Stream: cranelift

Topic: write to a global value


view this post on Zulip Cregg (Jul 01 2024 at 13:44):

I would like to create and then write to a global value and am wondering how to achieve this. Do I need to get a pointer to it and then use the store instruction? If so, how do I get a pointer to it? Is there a different instruction that I need to use? I can create and read from the global value just fine but am totally lost on how to write to it so any help would be greatly appreciated!

view this post on Zulip bjorn3 (Jul 01 2024 at 14:54):

If you are using cranelift-module (or one of it's implementors like cranelift-jit or cranelift-object), the global_value instruction will return a pointer to the global variable. You can then read and write through this pointer.

view this post on Zulip Cregg (Jul 01 2024 at 15:00):

When I try that and then print out the pointer it prints 0 and attempting to write to it segfaults

view this post on Zulip bjorn3 (Jul 01 2024 at 15:01):

Are you using cranelift-jit or cranelift-object? How did you get the GlobalValue you passed to the global_value instruction?

view this post on Zulip Cregg (Jul 01 2024 at 15:01):

I'm using cranelift object and i produced the global value using the declare data method on object module

view this post on Zulip bjorn3 (Jul 01 2024 at 15:03):

Is the global variable something you import from another library or do you define it in the same object file?

view this post on Zulip bjorn3 (Jul 01 2024 at 15:03):

And which Linkage did you specify when declaring?

view this post on Zulip Cregg (Jul 01 2024 at 15:12):

I am trying to define it by writing to it and I declared it as linkage::local

view this post on Zulip bjorn3 (Jul 01 2024 at 15:14):

You need to define any global variable you aren't importing from another library using define_data.

view this post on Zulip Cregg (Jul 01 2024 at 15:15):

alright thanks
ill give that a try in a moment


Last updated: Nov 22 2024 at 16:03 UTC