#How to get address of a symbol defined in assembly at comptime?

1 messages · Page 1 of 1 (latest)

iron lichen
#

Hi,

For context I'm playing with mini-os and xen and I try to implement something in Zig just to learn and fun.
I have a small piece of asm where I defined a symbol hypercall_page:

asm volatile (
   \\.globl hypercall_page
   \\    .align 4096
   \\hypercall_page:
   \\    .fill 4096,1,0

This code is in my kmain.zig file and it builds correctly and I can see the symbol hypercall_page at 0x13000 in the ELF file. So far so good.
The issue is that I want to use this variable at comptime in my kmain.zig to generate an ELF note using the address of this hypercall_page. So I added

extern const hypercall_page: usize;

But when I try to pass this constant to my function that generates ELF note at comptime I have the following error:

src/kmain.zig:36:41: error: unable to resolve comptime value
    elfNote.genXenLong(.hypercall_page, hypercall_page);
                                        ^~~~~~~~~~~~~~
src/kmain.zig:36:41: note: parameter is comptime

I tried to add comptime instead of const before the hypercall_page but it is not the solution.
Any hint to solve this problem?

PS: the code for more information is here (it is really for fun and I have no other goal that trying stuff... and failing 🙂 ) : https://github.com/gthvn1/mini-zos/blob/1-add-elfnote/src/kmain.zig

vital rain
#

the value of an extern isn't known at comptime - you need to make the parameter not be comptime

iron lichen
# vital rain the value of an extern isn't known at comptime - you need to make the parameter ...

Damned... So I cannot generate an ELF note that contains the address of a symbol... This address is known because I can see the symbol and its address when I'm using readelf -s <mybin> so without running the code. The problem is that It is required by Xen to have a ELF note with the address of the page that will contain "hypercalls".
So maybe I need to use the linker to force the hypercall_page to be at 0x13000 for example or right before the .text maybe.... This should be doable I think.

#

and so I can generate my ELF note during the compilation with 0x13000.

#

In fact the best solution I think if we cannot get the value at comptime is to generate the ELF note directly in assembly as it is done with mini-os....

#

and thanks @vital rain for the confirmation that I cannot get the symbol a comptime 👍

vital rain
#

note that the address of a symbol is not known until link time, which is after compile time

#

also, I was specifically referring to the value of the symbol, not the address, since you passed it without &hypercall_page

iron lichen
#

oh yes right for link time. So it makes sense that symbol is not know at comptime... thx for this enlightenment.
Finally I did this:

    asm volatile (
        \\
        \\    .pushsection .note.Xen
        \\    .align 4
        \\    .long 2f - 1f
        \\    .long 4f - 3f
        \\    .long 2
        \\1:.asciz #Xen
        \\2:.align 4
        \\3:.quad hypercall_page
        \\4:.align 4
        \\    .popsection
        \\
        \\.globl shared_info, hypercall_page
        \\    .align 4096
        \\shared_info:
        \\    .fill 4096,1,0
        \\hypercall_page:
        \\    .fill 4096,1,0
    );