Page 1 of 1

kernel entry address

Posted: Sun Sep 02, 2007 12:38 pm
by ezome
how does my bootloader find the entry address of my c kernel?


i link with

Code: Select all

LDFLAGS = -nostdlib -e _main -Ttext 0x100000 -L$(LIB_PATH) -Map kernel.map
but the mapfile says this

Code: Select all

Address of section .text set to 0x100000
LOAD kernel32.o
LOAD main.o
LOAD ..\..\lib\/libos.a
LOAD ..\..\lib\/libc.a

.text           0x00100000      0x670
 *(.text)
 .text          0x00100000       0x90 kernel32.o
                0x00100034                kernel32
 .text          0x00100090       0x20 main.o
                0x00100090                main
so the main function is placed at 0x00100090 which is pretty useless
and it moves from time to time when the code changes

how do i make sure the main function stays at 0x00100000?
here it is:

Code: Select all

void kernel32();

int main()
{
  kernel32();
  while(1);
}

Posted: Sun Sep 02, 2007 12:45 pm
by cyr1x
I would use a small stub written in ASM that calls the main function.
Something like:

Code: Select all

bit 32

global start
extern main

start:
call main
And set the entry point to "start" (with ld -> "ENTRY(start)")

Posted: Sun Sep 02, 2007 1:01 pm
by ezome
and how do i link a nasm binary to a c binary?

Posted: Sun Sep 02, 2007 1:15 pm
by jerryleecooper
Why do you want main to return a value? What is a map file? :?:
My advice is, make a main.c file with your main entry function, that will be called by your bootloader, don't put any function before your main function, and link your main.o object first. It's not only an advice, but the thing to do. Don't play with your main function a lot, or else it will break often.

Posted: Sun Sep 02, 2007 1:43 pm
by ezome
yeah you are right. the return value of main is pretty useless
just an old habbit after years of c++

a mapfile just shows you where the functions are placed in your executable

i think the problem is that i link kernel32.c before main.c
as you can see here

Code: Select all

.text           0x00100000      0x670
 *(.text)
 .text          0x00100000       0x90 kernel32.o
                0x00100034                kernel32
 .text          0x00100090       0x20 main.o
                0x00100090                main

Posted: Sun Sep 02, 2007 1:50 pm
by ezome
ok it's fixed

it was just about the linking order
after i renamed main.c my makefile linked it first

Code: Select all

.text           0x00100000      0x670
 *(.text)
 .text          0x00100000       0x20 _main.o
                0x00100000                main
 .text          0x00100020       0x90 kernel32.o
                0x00100054                kernel32