how to link binary bootcode with C object file

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
drnono
Posts: 23
Joined: Sun Aug 09, 2015 11:54 am

how to link binary bootcode with C object file

Post by drnono »

how do I link a binary format assembled with nasm with the object code from C.

I've compiled the i686-elf-gcc cross compiler.

1. nasm boot.asm

2. i686-elf-gcc -c kernel.c -o kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra

3. i686-elf-gcc -T linker.ld -o myos -ffreestanding -O2 -nostdlib boot.o kernel.o -lgcc

When I try to link I get:
boot.o: file not recognized: File format not recognized

linker.ld:

Code: Select all

ENTRY(kmain)

STARTUP(boot.o)
OUTPUT_FORMAT("binary")

SECTIONS
{
        . = 1M;
        .text BLOCK(4K) : ALIGN(4K)
        {
                *(.text)
        }
        .rodata BLOCK(4K) : ALIGN(4K)
        {
                *(.rodata)
        }
        .data BLOCK(4K) : ALIGN(4K)
        {
                *(.data)
        }
        .bss BLOCK(4K) : ALIGN(4K)
        {
                *(.COMMON)
                *(.bss)
        }
}
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: how to link binary bootcode with C object file

Post by kzinti »

Code: Select all

STARTUP(boot.o)
This can't be right (but I don't think that's the problem you are asking about).

In STARTUP(), you want to specify the entry point of your program. This is usually a symbol (name) of the function. Typically one uses _start.
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: how to link binary bootcode with C object file

Post by alexfru »

drnono wrote: 1. nasm boot.asm
I don't think this produces an object file. Most likely a raw binary, which you can't link with anything in a normal way.
drnono
Posts: 23
Joined: Sun Aug 09, 2015 11:54 am

Re: how to link binary bootcode with C object file

Post by drnono »

alexfru wrote:
drnono wrote: 1. nasm boot.asm
I don't think this produces an object file. Most likely a raw binary, which you can't link with anything in a normal way.
It is a raw binary. I tried nasm -felf boot.asm but it doesn't work. So, I'm thinking I have to get them to link or try to get gcc to compile to raw binary either.
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: how to link binary bootcode with C object file

Post by MichaelPetch »

Are you building a mulitboot executable to be used with GRUB or are you building a bootloader that runs in real mode that loads your kernel? I ask because your linker script outputs binary but the contents suggest you might be doing multiboot. Your line nasm boot.asm would build a BIN file. Maybe you meant nasm -f elf32 boot.asm -o boot.o. If doing multiboot you don't want to output as binary in your linker script either. If we saw your boot.asm we might be able to tell what your end goal is.
drnono
Posts: 23
Joined: Sun Aug 09, 2015 11:54 am

Re: how to link binary bootcode with C object file

Post by drnono »

I'm not doing anything with multiboot.
I want to have the kernel on second sector of a disk loaded by the bootloader and then jump or pass to running C code.

I've done this before a while ago but I can't remember how exactly. I'm pretty sure I used the sample linker scripts, but it seems to me now that it would be simpler to just have raw C compiled pasted instead.
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: how to link binary bootcode with C object file

Post by MichaelPetch »

The linker script you are using appears to be for multiboot usage. Maybe what you did previous was build your boot.asm into a bootsector (binary file) and built your kernel separately into a separate binary and then placed them into a disk image with something like DD?
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: how to link binary bootcode with C object file

Post by MichaelPetch »

Linking them together isn't straight forward, so not likely what you had done previously. As an example of how you could do it the more complex way, I slapped together an example as a proof of concept. The kernel disk reading routine is a hack for demonstration purposes; it uses the fast method of turning on A20 for demo purposes. It has a boot.asm that enters into protected mode, zeroes the BSS section, then transfers to a kernel entry point called kmain. It should display MDP in the upper left with white on magenta.

You can find the sample code here: http://www.capp-sysware.com/misc/osdev/linkedboot/

Edit: Had some time this morning to update the code a bit. The kernel loading is less hackish.
Post Reply