OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 18, 2024 4:41 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: how to link binary bootcode with C object file
PostPosted: Mon Nov 19, 2018 1:39 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: how to link binary bootcode with C object file
PostPosted: Mon Nov 19, 2018 2:29 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
Code:
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.

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: how to link binary bootcode with C object file
PostPosted: Mon Nov 19, 2018 2:36 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
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.


Top
 Profile  
 
 Post subject: Re: how to link binary bootcode with C object file
PostPosted: Mon Nov 19, 2018 2:43 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: how to link binary bootcode with C object file
PostPosted: Mon Nov 19, 2018 2:47 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 692
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.


Top
 Profile  
 
 Post subject: Re: how to link binary bootcode with C object file
PostPosted: Mon Nov 19, 2018 3:08 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: how to link binary bootcode with C object file
PostPosted: Mon Nov 19, 2018 3:20 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 692
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?


Top
 Profile  
 
 Post subject: Re: how to link binary bootcode with C object file
PostPosted: Mon Nov 19, 2018 3:56 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 692
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 335 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group