Page 1 of 1

going from asm file to c/c++ file

Posted: Sun Mar 22, 2009 2:05 pm
by mostermand
Hi I am just starting in OSDeving.
I am trying to make a flat binary file by linking an asm file and a cpp file with my cross compiler using a custom link script from "Bran's Kernel Development Tutorial".

I have a kernel_start.asm that jumps to extern _main which is defined in kernel.cpp.

I am getting the following error when trying to link it.

kernel_start.asm:(.text+0x1): undefined reference to `_main'

If anyone could help me in this matter I would be most grapefull(and grape tastes good)

Re: going from asm file to c/c++ file

Posted: Sun Mar 22, 2009 2:18 pm
by gzaloprgm
Try removing the underscore (_) from extern _main and call _main.

Re: going from asm file to c/c++ file

Posted: Sun Mar 22, 2009 2:42 pm
by mostermand
funny before I read your reply I came to the same conclusion by disassemblying my object files but it still didn't work then I found out I was passing my arguments wrong to the linker now the problem is that even though the linker script specifies "binary" format I get an elf file

Re: going from asm file to c/c++ file

Posted: Sun Mar 22, 2009 3:04 pm
by AJ
Hi,

Have you taken account of C++ name mangling? IIRC a standards-compiant main function name does not normally get mangled, but if it's not compilant the name could get mangled anyway. Try defining main as 'extern "C" ' rather than just 'extern'.

As for the linker producing elf output instead of a flat binary, can we see your compiler and linker commands, along with the linker script?

Cheers,
Adam

Re: going from asm file to c/c++ file

Posted: Mon Mar 23, 2009 6:35 am
by mostermand
The name mangling is no longer a problem I figured it out as for the the linker commands and script here it is:

i586-elf-ld -t link.ld -o kernel.bin Kernel.o kernel_start.o

linker-script:

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
.text phys : AT(phys) {
code = .;
*(.text)
*(.rodata)
. = ALIGN(4096);
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
. = ALIGN(4096);
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
. = ALIGN(4096);
}
end = .;
}

Re: going from asm file to c/c++ file

Posted: Mon Mar 23, 2009 9:37 am
by Creature
I'm not sure, but, shouldn't rodata be in the data section? Doesn't the 'text' section define executable and read-only code whilst the 'data' section defines 'read/write, but non-executable code'?

Re: going from asm file to c/c++ file

Posted: Mon Mar 23, 2009 11:46 am
by mostermand
I'll check it out