linking asm and c with ld
Posted: Tue Aug 10, 2004 6:51 am
Thanks to the wonderful help I recieved here, I now have a working boot sector, and loader and can successfuly get into protected mode, which is a big step considering I only just started looking into OS developement properly a few days ago.
But enough with the rambling. I face another (incredibly newbish) problem with using ld.
Firstly, I have this code, which is jumped to by the loader right after entering protected mode, kernel.asm:
Now, here is the C code, kernel_c.c:
Simple enough, yes?
Now my problem is trying to compile the thing.
First I assemble the kernel.asm file to an object file:
Then I try to compile the C code to an object file:
Now I try to link them:
I copied this from an example. Just as a "side" question. What exactly does "0xFF800000" do? Do I replace this with the location I loaded my kernel.asm file to (DATA_SEL:0x2000) or does this number not particularly matter? (I have the A20 line enabled, and am in P-Mode with a flat data and code segment but don't have paging turned on [YET!], if any of that's relevant)
Anyway now the main problem. I can get so far as having 2 object files, but they won't link. I get this error:
"ld: PE operations on non PE file."
I read somewhere that this was a problem with a certain version of ld, so I tried a few other versions. Other versions just gave the same thing, or actually caused a general protection fault and crashed ("ld has encountered a problem and needs to close").
I also read somewhere to try "aout" instead of "coff", but then ld complained about unknown file formats, so back to "coff" i went.
I'm guessing it's something pretty newbish I'm doing, but I've searched all over the web, read the documentation, and read other examples, and I just can't see what's wrong.
(As a side note, I'd rather do things simply from the command line like this, than use a linker script, if that can be avoided)
Thanks in advance.
But enough with the rambling. I face another (incredibly newbish) problem with using ld.
Firstly, I have this code, which is jumped to by the loader right after entering protected mode, kernel.asm:
Code: Select all
[bits 32]
extern _c_main
; Jump into the main kernel
call _c_main
; Halt the CPU
cli
hlt
Code: Select all
char message[]= "C Kernel loaded.";
int c_main ()
{
char *source = message;
char *destination = (char *)0xB8000;
while (*source)
{
*destination++ = *source++;
*destination++ = 7;
}
return 0;
}
Now my problem is trying to compile the thing.
First I assemble the kernel.asm file to an object file:
Code: Select all
nasm -f coff kernel.asm -o kernel.o
Code: Select all
gcc -O3 -c kernel_c.c -o kernel_c.o
Code: Select all
ld -Ttext 0xFF800000 --oformat binary -o kernel.bin kernel.o kernel_c.o
Anyway now the main problem. I can get so far as having 2 object files, but they won't link. I get this error:
"ld: PE operations on non PE file."
I read somewhere that this was a problem with a certain version of ld, so I tried a few other versions. Other versions just gave the same thing, or actually caused a general protection fault and crashed ("ld has encountered a problem and needs to close").
I also read somewhere to try "aout" instead of "coff", but then ld complained about unknown file formats, so back to "coff" i went.
I'm guessing it's something pretty newbish I'm doing, but I've searched all over the web, read the documentation, and read other examples, and I just can't see what's wrong.
(As a side note, I'd rather do things simply from the command line like this, than use a linker script, if that can be avoided)
Thanks in advance.