Inline Assembly Function to load GDT Error

Programming, for all ages and all languages.
Post Reply
LindusSystem
Member
Member
Posts: 63
Joined: Sat Apr 28, 2012 9:41 am
Location: Earth -> Asia

Inline Assembly Function to load GDT Error

Post 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.
Anyone has a idea of making a ntfs bootsector?if yes PM me , plz.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Inline Assembly Function to load GDT Error

Post by Combuster »

Define gdtp instead of just declaring it extern?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
LindusSystem
Member
Member
Posts: 63
Joined: Sat Apr 28, 2012 9:41 am
Location: Earth -> Asia

Re: Inline Assembly Function to load GDT Error

Post 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.
Anyone has a idea of making a ntfs bootsector?if yes PM me , plz.
LindusSystem
Member
Member
Posts: 63
Joined: Sat Apr 28, 2012 9:41 am
Location: Earth -> Asia

Re: Inline Assembly Function to load GDT Error

Post 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
Anyone has a idea of making a ntfs bootsector?if yes PM me , plz.
Post Reply