Linker error under Linux

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Mike

Linker error under Linux

Post by Mike »

I'm just trying to compile a simple kernel under Linux. I'm getting a linker error:

Code: Select all

[mike@pc-00065 kernel3]$ gcc -c kernel.c -o kernel.o
[mike@pc-00065 kernel3]$ nasm -f aout k_entry.asm -o k_entry.o
[mike@pc-00065 kernel3]$ ld -T kernel.ld  k_entry.o kernel.o
k_entry.o(.text+0x16):k_entry.o: undefined reference to `_k_main'
I'm sure it's something silly. Any help appreciated.

Thanks

Part of the kernel entry asm is:

Code: Select all

[BITS 32]

[global start]
[extern _k_main] ; this is in the c file

start:
  ; stop using bootloader GDT, and load new GDT
  lgdt [gdt_ptr]

  mov ax,LINEAR_DATA_SEL
  mov ds,ax
  mov es,ax
  mov ss,ax
  mov fs,ax
  mov gs,ax

  call _k_main

  jmp $ ; crash
And part of the kernel.c file sis:

Code: Select all


void k_main(void) // like main
{
   k_clear_screen();

   k_printf("Welcome to Simplified OS.", 0);
   update_cursor(1, 0);
   k_printf("Right now the kernel is loaded at 1MB physical,/nbut mapped at FF800000 hex.", 3);
   update_cursor(5, 0);

   k_printf("Remapping the PIC...", 6);
   update_cursor(7,0);
   remap_pics(0x20, 0x28);   // irq 0 20h irq 8 28h

   k_printf("PIC remapped starting at 20 hex.", 7);
   update_cursor(8,0);
};
Mike

Re:Linker error under Linux

Post by Mike »

Argh, solved it. I was compiling a C file, so the C compiler doesn't add an _ to the beginning of exported symbols. D'oh!!! ;D
Mike

Re:Linker error under Linux

Post by Mike »

Ok, another problem. This is my build script:

Code: Select all

#! /bin/bash

gcc -c kernel.c -o kernel.o
nasm -f elf k_entry.asm -o k_entry.o
ld -T kernel.ld k_entry.o kernel.o
mv a.out kernel.bin
The ld command gives the error: File size limit exceeded

I only get this if I use -f elf. -f a,out works fine. However, grub then gives error 13 - unsupported file format, I think.

Thanks
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Linker error under Linux

Post by Solar »

Without seeing your linker script, and the kernel.asm you gave rather obviously being only a quick rip-off what's really in there, it's hard to tell why your file size limit should be exceeded.

I'd point you to "the a.out kludge" in the GRUB documentation which would show you how to make GRUB boot an a.out image, but the ld error message points to an error somewhere in your setup, and the a.out kludge would merely cover that one up...
Every good solution is obvious once you've found it.
Post Reply