Reputation: 11170
I'm trying to link to files - a c file containing a main function and an asm file that simply jumps to the main.
I have mingw installed. My files:
//kernel.c
void some_function(){
}
void main(){
char* video_memory = (char*) 0xb8000;
*video_memory = 'X';
some_function();
}
;kernel_entry.asm
[bits 32]
[extern main]
call main
jmp $
The commands I call to build:
gcc -ffreestanding -c kernel.c -o kernel.o
nasm kernel_entry.asm -f elf -o kernel_entry.o
ld -o kernel.bin -Ttext 0x1000 kernel_entry.o kernel.o
The errors I'm getting:
kernel_entry.o:(.text+0x1): undefined reference to `main'
kernel.o:kernel.c:(.text+0xf): undefined reference to `__main'
EDIT:
Which commands work:
ld -r -o kernel.out -Ttext 0x1000 kernel.o
objcopy -O binary -j .text kernel.out kernel.bin
When I try to run ld with -r i get an error:
ld: Relocatable linking with relocations from format elf32-i386 (kernel_entry.o)
to format pe-i386 (kernel.bin) is not supported
EDIT2: I had the best results when using these commands:
gcc -ffreestanding -c kernel.c -o kernel.o
nasm kernel_entry.asm -f win32 -o kernel_entry.o
ld -r -o kernel.out -Ttext 0x1000 kernel_entry.o kernel.o
objcopy -O binary -j .text kernel.out kernel.bin
The files linked succesfully, but when running, the main never gets called. Also tried with coff format, it also links, but Bochs keeps restarting.
Upvotes: 0
Views: 3829
Reputation: 22074
The first error is is because in C a function is named _name
, so you can not call main
as such, you must call _main
. In TASM you can set the calling convention, so the assmebler automatically can call the right function, I don't know if there is such a feature for nasm as well.
The second problem is probably, because you are calling the linker directly. In this case you must specify the C startup module, which is normally added by the compiler to the linker options. Usually I think this is a file named something like crt0
. If you write your own startup code, you must provide this yourself. This module sets up the environment for C from the OS specific entry point. I guess this is missing in your project.
http://en.wikipedia.org/wiki/Crt0
Upvotes: 2