Thread overview
[Help] all __gshared vairable address is 0.
Aug 25, 2019
lili
Aug 25, 2019
kinke
Aug 29, 2019
lili
Aug 28, 2019
XavierAP
August 25, 2019
Hi:
    I'm writing a OS kernel in D. I meet a problem that all bss variables's address is same
    and is 0.

   see code below:

extern(C) extern __gshared ulong  p4_table; //defined in a boot.s file
__gshared int xx= void;
abstract final class Vga
{
    void init()
    {
        clearScreen();
        Vga.println(cast(ulong)p4_table); //0
        Vga.println(cast(ulong)&xx); //0
    }
but i see the link map file p4_table and xx has different address.

                0x0000000000104000                __bss = .
 *(.bss)
 .bss           0x0000000000104000    0x15000 boot.o
                0x0000000000104000                p4_table
                0x0000000000105000                p3_table
                0x0000000000106000                p2_table
                0x0000000000108000                idt_addr
 *(.bss.*)
 .bss.xx        0x0000000000119000        0x4 vga.o
                0x0000000000119000                xx

 and when i call asm_fn in d the return value of asm_fn is return 0x106000 not 0.
 so why did it occur.


[global asm_fn]
asm_fn:
	mov rax, p2_table
	ret

src  https://github.com/lilijreey/OS-D


August 25, 2019
On Sunday, 25 August 2019 at 12:12:51 UTC, lili wrote:
> I'm writing a OS kernel in D.

Are you aware of https://github.com/PowerNex/PowerNex, a similar project?

> and when i call asm_fn in d the return value of asm_fn is return 0x106000 not 0.
>  so why did it occur.

I'd suggest writing the same function in D and comparing the produced assembly. Use `-output-s` to generate textual assembly files (*.s) directly with LDC.
I guess you need to play around with `-relocation-model`.
August 28, 2019
On Sunday, 25 August 2019 at 12:12:51 UTC, lili wrote:
> Hi:
>     I'm writing a OS kernel in D. I meet a problem that all bss variables's address is same
>     and is 0.
>
>    see code below:
>
> extern(C) extern __gshared ulong  p4_table; //defined in a boot.s file
> __gshared int xx= void;
> abstract final class Vga
> {
>     void init()
>     {
>         clearScreen();
>         Vga.println(cast(ulong)p4_table); //0
>         Vga.println(cast(ulong)&xx); //0
>     }
> but i see the link map file p4_table and xx has different address.

Welp... I don't know. But in any case in principle you should use the portable alias size_t for pointers:

https://dlang.org/spec/type.html#size_t
August 29, 2019
On Sunday, 25 August 2019 at 12:57:58 UTC, kinke wrote:
> On Sunday, 25 August 2019 at 12:12:51 UTC, lili wrote:
>> I'm writing a OS kernel in D.
>
> Are you aware of https://github.com/PowerNex/PowerNex, a similar project?
>
>> and when i call asm_fn in d the return value of asm_fn is return 0x106000 not 0.
>>  so why did it occur.
>
> I'd suggest writing the same function in D and comparing the produced assembly. Use `-output-s` to generate textual assembly files (*.s) directly with LDC.
> I guess you need to play around with `-relocation-model`.

You are right, the problem was fixed when I set -relocation-mode=static. Thanks very much.