link error

Programming, for all ages and all languages.
Post Reply
derektriley
Posts: 2
Joined: Sat Feb 01, 2014 10:18 pm

link error

Post by derektriley »

I'm following Bran's Kernel Development tutorials and I am on the the GDT. I am getting a link error that I haven't been able to fix and have been searching for a bit. Any help will be very much appreciated. Thanks for your time.

Commands issued by my Makefile:

Code: Select all

i586-elf-gcc -c scrn.c -o scrn.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra -I.
i586-elf-gcc -c kernel.c -o kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra -I.
i586-elf-as boot.s -o boot.o
i586-elf-gcc -c gdt.c -o gdt.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra -I.
i586-elf-gcc -T linker.ld -o dtros.bin -ffreestanding -O2 -nostdlib scrn.o kernel.o boot.o gdt.o -lgcc
boot.o: In function `_gdt_flush':
(.text+0x11): undefined reference to `_gp'
gdt.o: In function `gdt_install':
gdt.c:(.text+0xfe): undefined reference to `gdt_flush'
collect2: ld returned 1 exit status
make: *** [dtros.bin] Error 1
Here is my asm code:

Code: Select all

# This will set up our new segment registers. We need to do
# something special in order to set CS. We do what is called a
# far jump. A jump that includes a segment as well as an offset.
# This is declared in C as 'extern void gdt_flush();'
.section .text
.global _gdt_flush     # Allows the C code to link to this
.type _gdt_flush, @function
.extern _gp
_gdt_flush:
    lgdt (_gp)        # Load the GDT with our '_gp' which is a special pointer
    mov $0x10, %ax       # 0x10 is the offset in the GDT to our data segment
    mov %ax, %ds
    mov %ax, %es
    mov %ax, %fs
    mov %ax, %gs 
    mov %ax, %ss
	ljmp $0x8,$flush2   # 0x08 is the offset to our code segment: Far jump!
flush2:
    ret               # Returns back to the C code!
In gdt.c I have:

Code: Select all

/* Our GDT, with 3 entries, and finally our special GDT pointer */
struct gdt_entry gdt[3];
struct gdt_ptr gp;

/* This will be a function in start.asm. We use this to properly
*  reload the new segment registers */
extern void gdt_flush();
as discussed in the tutorial.
User avatar
xenos
Member
Member
Posts: 1118
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: link error

Post by xenos »

Since you use a proper ELF cross compiler (which is good) you don't need to (in fact must not) prepend underscores to the symbol names in your ASM file, so it should be gp and gdt_flush instead of _gp and _gdt_flush.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
derektriley
Posts: 2
Joined: Sat Feb 01, 2014 10:18 pm

Re: link error

Post by derektriley »

Thank you very much.
Post Reply