Page 1 of 1

Linking floppy bootloader

Posted: Wed Mar 21, 2012 4:28 am
by Even
Hi!
I'm writing a floppy bootloader but I've got a problem. The bootloader consists of two parts: the assembler main function and a C - function. The linked binary file must be 512 bytes long with a 0xAA55 signature at the end, so I use this part of source:

Code: Select all

.fill	510-(.-start), 1, 0
.word	0xaa55
I use AT&T syntax and gas assembler.
When I link program the linker inserts C function after 0xAA55 signature so the binary file becomes larger then 512 bytes. How can I tell linker to insert the compiled C - function to the appointed address, not after the assembler main?

Command line:

Code: Select all

gcc -m32 -c Cfunc.c
as --32 -o  ASMfunc.o ASMfunc.s
ld -m elf_i386 -M -Ttext 0x7c00 --oformat binary -o bootloader.bin ASMfunc.o Cfunc.o
The part of linker output:
.text 0x0000000000007c00 0x200 ASMfunc.o
0x0000000000007c00 ASMfunc
.text 0x0000000000007e00 0x5 Cfunc.o
0x0000000000007e00 Cfunc

Re: Linking floppy bootloader

Posted: Wed Mar 21, 2012 4:40 am
by bubach
512mb? Besides, you should never use C in a bootsector. Try learning how to do the same thing with Assembly.

Re: Linking floppy bootloader

Posted: Wed Mar 21, 2012 5:01 am
by Even
bubach wrote:512mb?
Oh, sorry. My mistake.
bubach wrote:Besides, you should never use C in a bootsector. Try learning how to do the same thing with Assembly.
Why shouldn't I use C in a bootsector? I've switched from real to protected mode and configured GDT (null, code and data descriptors).

Re: Linking floppy bootloader

Posted: Wed Mar 21, 2012 5:11 am
by Solar
Even wrote:Why shouldn't I use C in a bootsector?
Usually people don't use C in a bootsector because the available space is so cramped, there are no guarantees for the size of the compiler output (i.e. your current compiler might work but others might not), and you have to start with ASM anyway, so they don't bother with the bootsector being compiled from two languages.

I wouldn't go as far as saying you "should not". If you get it to work, fine. It's just a somewhat unusual decision.

Re: Linking floppy bootloader

Posted: Wed Mar 21, 2012 5:56 am
by Even
Ok, let's forget about bootloader.

I've got two functions. How can I tell linker to insert the second function code into a specific place in the program? Like parameter "-Ttext org" but for the .text segment of the second function.

Re: Linking floppy bootloader

Posted: Wed Mar 21, 2012 6:03 am
by qw
Use a linker script. You can supply the name of the object files in desired order. An alternative is to locate the functions in different sections and merge the sections in desired order. However, for a boot sector this may be too much trouble.

Re: Linking floppy bootloader

Posted: Wed Mar 21, 2012 3:35 pm
by Even
I've done it! Tested in bochs and it works!

I've even deleted this part of code:

Code: Select all

.fill   510-(.-start), 1, 0
.word   0xaa55
..and written a linker script:

Code: Select all

ENTRY(start)
STARTUP(ASMfunc.o)
INPUT(Cfunc.o)

SECTIONS
{
	.text 0x7c00:
	{
		*(.text);

	}
	.data 0x7dfe:
	{
		SHORT(0xaa55);
	}
}
OUTPUT_FORMAT(binary)
OUTPUT(test.bin)
So the 0xaa55 signature is inserted by linker. When you add some variables to a C - function the script will become more complicated because of more segments.
Command line:

Code: Select all

as --32 -o  ASMfunc.o ASMfunc.s
gcc -m32 -fno-asynchronous-unwind-tables -c Cfunc.c
ld -m elf_i386 -M -T linker_script
Thanks everybody!