Page 1 of 1

Inline Assembly Function to load GDT Error

Posted: Thu May 17, 2012 10:41 am
by LindusSystem
One crap error which I could not solve of my friend.
Code goes like this:

Code: Select all

void init_gdt()
{
    gdtp.limit = (sizeof(struct gdt_gate) * 5) - 1;
    gdtp.base = &gdt_entries;
    gdt_set_entry(0, 0, 0, NULL_SEGMENT, 0);
    gdt_set_entry(1, 0,SEGMENT_LIMIT, KRNL_CODE_SEGMENT, 0xCF);
    gdt_set_entry(2, 0,SEGMENT_LIMIT, KRNL_DATA_SEGMENT, 0xCF);
    gdt_set_entry(3, 0,SEGMENT_LIMIT, USER_CODE_SEGMENT, 0xCF);
    gdt_set_entry(4, 0,SEGMENT_LIMIT, USER_DATA_SEGMENT, 0xCF);
    __asm__ __volatile__("lgdt (%0) ": :"p" ((&gdtp)));
}
The build script is like this

Code: Select all

nasm -f aout boot.asm -o output/boot.o
gcc -Wall -O  -finline-functions -fno-builtin -I./include -c -o output/hal/init.o hal/x86/init.c
gcc -Wall -O  -finline-functions -fno-builtin -I./include -c -o output/hal/gdt.o hal/x86/descriptors/gdt.c

ld -T link.ld -o krnl32.exe  output\boot.o output\hal\init.o output\hal\gdt.o
And the error which the linker gives is
output/hal/gdt.o<.text+0x157>:gdt.c: undefined reference to '$_gdtp'
Any idea of the causing problem.

Re: Inline Assembly Function to load GDT Error

Posted: Thu May 17, 2012 10:48 am
by Combuster
Define gdtp instead of just declaring it extern?

Re: Inline Assembly Function to load GDT Error

Posted: Thu May 17, 2012 10:52 am
by LindusSystem
I have declared gdtp as struct descriptor_pointer gdtp;

And the structure look like this:

struct descriptor_pointer
{
u16int limit;
u32int base;
} __attribute__((packed));

EDIT:The underscores are added by GCC automatically.

Re: Inline Assembly Function to load GDT Error

Posted: Thu May 17, 2012 11:01 am
by LindusSystem
Thank you,but I made a modifications to your code and made it like this and it works!

Code: Select all

__asm__ __volatile__("lgdt %0":: "m"(gdtp));
Thanks Combuster and berkus for your consideration