Linking floppy bootloader

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Even
Posts: 9
Joined: Tue Mar 20, 2012 6:01 pm
Location: Warsaw, Poland

Linking floppy bootloader

Post 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
Last edited by Even on Wed Mar 21, 2012 4:57 am, edited 1 time in total.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re: Linking floppy bootloader

Post by bubach »

512mb? Besides, you should never use C in a bootsector. Try learning how to do the same thing with Assembly.
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
Even
Posts: 9
Joined: Tue Mar 20, 2012 6:01 pm
Location: Warsaw, Poland

Re: Linking floppy bootloader

Post 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).
Last edited by Even on Wed Mar 21, 2012 5:21 am, edited 1 time in total.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Linking floppy bootloader

Post 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.
Every good solution is obvious once you've found it.
User avatar
Even
Posts: 9
Joined: Tue Mar 20, 2012 6:01 pm
Location: Warsaw, Poland

Re: Linking floppy bootloader

Post 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.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Linking floppy bootloader

Post 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.
User avatar
Even
Posts: 9
Joined: Tue Mar 20, 2012 6:01 pm
Location: Warsaw, Poland

Re: Linking floppy bootloader

Post 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!
Post Reply