[llvm-dev] How to get the address of a global variable in LLVM?

Jonathan Roelofs via llvm-dev llvm-dev at lists.llvm.org
Fri Apr 28 14:42:25 PDT 2017



On 4/28/17 3:38 PM, Dipanjan Das via llvm-dev wrote:
>
> I am a little confused. Can anyone explain me in short? The definition
> of `func` is as below:
>
>    void func(int var_addr) {
> printf("[instr]: Global variable at address 0x%x\n", var_addr);
>    }
>
> I want to insert a call to `func` just before the store instruction with
> the address `x` of global variable `g` as parameter.

Tim is saying that your function should be:

   void func(void *var_addr) ...

and that in llvm you want to refer to it with type `void func(i8*)`.

I only suggested the load because I was confused about what you wanted 
(assumed you wanted the value, because of what you had picked for the 
function signature).


Jon

>
>
> On 28 April 2017 at 14:34, Tim Northover <t.p.northover at gmail.com
> <mailto:t.p.northover at gmail.com>> wrote:
>
>     On 28 April 2017 at 14:32, Jonathan Roelofs via llvm-dev
>     <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote:
>     > You need a load instruction since your function takes an i32, not an
>     > i32*.... A global's value in llvm is its address, not the numeric
>     data it
>     > contains.
>
>     I suspect he actually wants to make his function take an "i8*" and
>     bitcast his pointer to that type before calling it. [*]
>
>     Tim.
>
>     [*] Keeping it as an i32/i64 and using ptrtoint instead of bitcast
>     would also work, but be less portable and elegant.
>
>
>
>
> --
>
> Thanks & Regards,
>
> Dipanjan
>
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>

-- 
Jon Roelofs
jonathan at codesourcery.com
CodeSourcery / Mentor Embedded / Siemens


More information about the llvm-dev mailing list