OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 3:06 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: [Help] Linking GCC C and ELF ASM.
PostPosted: Sat Jan 01, 2011 12:43 am 
Offline

Joined: Sat Jan 01, 2011 12:34 am
Posts: 14
Hey guys! I was just wondering if someone could help me on how to go about linking GCC C Objects and ELF ASM objects in flat binary.

I currently have a custom bootloader(Yes i know its crap, but it gets the job done):

NASM:
Code:
; boot1.asm   stand alone program for floppy boot sector
; Compiled using            nasm -f bin boot1.asm
; Written to floppy with    dd if=boot1 of=/dev/fd0

; Boot record is loaded at 0000:7C00,
   ORG 7C00h
; load message address into SI register:
   LEA SI,[msg]
; screen function:
   MOV AH,0Eh
print:  MOV AL,[SI]         
   CMP AL,0         
   JZ done      ; zero byte at end of string
   INT 10h      ; write character to screen.   
        INC SI         
   JMP print

; wait for 'any key':
done:   ;MOV AH,0       
       ;INT 16h      ; waits for key press
         ; AL is ASCII code or zero
         ; AH is keyboard code
;===================================
; load the kernel at 0800h:0000h
; 10 sectors starting at:
;   cylinder: 0
;   sector: 2
;   head: 0

; BIOS passes drive number in dl,
; so it's not changed:

mov     ah, 02h ; read function.
mov     al, 10  ; sectors to read.
mov     ch, 0   ; cylinder.
mov     cl, 2   ; sector.
mov     dh, 0   ; head.
; dl not changed! - drive number.

; es:bx points to receiving
;  data buffer:
mov     bx, 0800h   
mov     es, bx
mov     bx, 0

; read!
int     13h

jmp     0800h:0000h

; store magic value at 0040h:0072h to reboot:
;      0000h - cold boot.
;      1234h - warm boot.
   ;MOV  AX,0040h
   ;MOV  DS,AX
   ;MOV  word[0072h],0000h   ; cold boot.
   ;JMP  0FFFFh:0000h    ; reboot!

msg    DB  'Welcome, I have control of the computer.',13,10
   DB  'Press any key to reboot.',13,10
   DB  '(after removing the floppy)',13,10,0
; end boot1
TIMES 510 - ($ - $$) db 0   ;Fill the rest of sector with 0
DW 0xAA55         ;Add boot signature at the end of bootloader



Basically it reads the code starting at Sector 2 and goes to sector 12. The code that it reads is the binary file which is compiled from these two sources:

NASM:
Code:
; boot1.asm   stand alone program for floppy boot sector
; Compiled using            nasm -f bin boot1.asm
; Written to floppy with    dd if=boot1 of=/dev/fd0
global loader
[BITS 32]
extern _kmain
section .text
; Boot record is loaded at 0000:7C00,
   ;ORG 0000h
; load message address into SI register:
   LEA SI,[msg]
; screen function:
   MOV AH,0Eh
print:  MOV AL,[SI]         
   CMP AL,0         
   JZ done      ; zero byte at end of string
   INT 10h      ; write character to screen.   
        INC SI         
   JMP print

; wait for 'any key':
done:   ;MOV AH,0       
       ;INT 16h      ; waits for key press
         ; AL is ASCII code or zero
         ; AH is keyboard code
call _kmain


; store magic value at 0040h:0072h to reboot:
;      0000h - cold boot.
;      1234h - warm boot.
   MOV  AX,0040h
   MOV  DS,AX
   MOV  word[0072h],0000h   ; cold boot.
   JMP  0FFFFh:0000h    ; reboot!

msg    DB  'Welcome, I(k2) have control of the computer.',13,10
   DB  'Press any key to reboot.',13,10
   DB  '(after removing the floppy)',13,10,0
; end boot1

TIMES 512 - ($ - $$) db 0   ;Fill the rest of sector with 0


GCC C:
Code:
void k_clear_screen();

void kmain()
{

   /* Print a letter to screen to see everything is working: */
   unsigned char *videoram = (unsigned char *) 0xb8000;
   videoram[0] = 65; /* character 'A' */
   videoram[1] = 0x07; /* forground, background color. */

   /* Write your kernel here. */
   k_clear_screen();
}


void k_clear_screen() // clear the entire text screen
{
   char *vidmem = (char *) 0xb8000;
   unsigned int i=0;
   while(i < (80*25*2))
   {
      vidmem[i]=' ';
      i++;
      vidmem[i]= 0x07;
      i++;
   };
};


Ok so once the bootloader jumps to 0800h(Where the GCC and NASM binary file is)it should execute the code. Unfortuantely it is not. I think it is not doing this because i am not linking the GCC object and the NASM Object properly.

To compile the Kernel(2nd NASM code) i used:
nasm -f elf -0 k2.o k2.asm

To compile the GCC (C) code i used:
gcc -o kernel2.o -c kernel2.c -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs

They both compiled sucessfully!

Now i linked them using:
ld -o k2.bin k2.o kernel2.o

They linked without any errors! So i said cool! So i loaded my bootload onto my floppy, and the k2.bin after it. The bootloader loaded up and displayed it text. Next the letter A should have appeared and the screen should have cleared. Unfortunately neither happened. I know for a fact the code is being loaded and jumped to in the right part of the memory. I think its just not linking properly. Does anyone have an exmaple of how to link these two files together right (Link them so they come out in the same format as a NASM binary file) so it can work properly?

Also i have tried having the bootloader jump to a kernel written in just ASM compiled into binary and it works.

Would it be easier to take all assembly out of the kernel, and just compile the C source to an object, then Link it to flat binary? If i did that how would i call the function kmain from my bootloader?

Thanks!


Top
 Profile  
 
 Post subject: Re: [Help] Linking GCC C and ELF ASM.
PostPosted: Sat Jan 01, 2011 1:30 am 
Offline
Member
Member

Joined: Tue Apr 13, 2010 8:00 pm
Posts: 285
1: They're called ldscripts. Any custom link job needs one. There are uncountably many resources on how to write them, find one. In short, your problem is caused by ld not knowing where your C code is going to be in memory, and is assuming it will be at the default address for executables in your OS.

2: Don't use a custom bootloader. Just don't. There's a select few who can say they use a custom boot loader without being immediately discredited, neither you nor I are among them.


Top
 Profile  
 
 Post subject: Re: [Help] Linking GCC C and ELF ASM.
PostPosted: Sat Jan 01, 2011 3:56 am 
Offline
Member
Member
User avatar

Joined: Thu Aug 11, 2005 11:00 pm
Posts: 1110
Location: Tartu, Estonia
I don't see any point in your code where you switch the CPU from Real Mode (the old 16 bit mode used by DOS, in which the CPU starts) to Protected Mode (the 32 bit mode used by modern operating systems). GCC produces 32 bit code which won't work if you are still in real mode. Similarly, using [BITS 32] tells NASM to produce 32 bit code, which won't work in Real Mode either.

If you want to execute 32 bit code, you either need to switch to Protected Mode at some place in your boot sector or kernel image, or use a bootloader such as GRUB which does this job for you and starts your kernel in Protected Mode. But be aware of the fact that BIOS interrupts won't work in Protected Mode, they are written in 16 bit Real Mode code.

This might help you: Bare Bones

_________________
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS


Top
 Profile  
 
 Post subject: Re: [Help] Linking GCC C and ELF ASM.
PostPosted: Sat Jan 01, 2011 11:38 am 
Offline

Joined: Sat Jan 01, 2011 12:34 am
Posts: 14
Thanks for the responces... but everytime i try the bar bones tutorial i get everything working and then in GRUB i get ERROR 13.


Top
 Profile  
 
 Post subject: Re: [Help] Linking GCC C and ELF ASM.
PostPosted: Sat Jan 01, 2011 1:30 pm 
Offline
Member
Member

Joined: Tue Apr 13, 2010 8:00 pm
Posts: 285
What is grub error 13, and what have you tried to fix it?


Top
 Profile  
 
 Post subject: Re: [Help] Linking GCC C and ELF ASM.
PostPosted: Sat Jan 01, 2011 2:18 pm 
Offline
Member
Member
User avatar

Joined: Thu Aug 11, 2005 11:00 pm
Posts: 1110
Location: Tartu, Estonia
Grub Error 13

The most common GRUB error encountered by OS developers...

_________________
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS


Top
 Profile  
 
 Post subject: Re: [Help] Linking GCC C and ELF ASM.
PostPosted: Sat Jan 01, 2011 8:56 pm 
Offline
Member
Member

Joined: Tue Apr 13, 2010 8:00 pm
Posts: 285
The point was to have op do the leg work.


Top
 Profile  
 
 Post subject: Re: [Help] Linking GCC C and ELF ASM.
PostPosted: Sat Jan 01, 2011 10:12 pm 
Offline
Member
Member

Joined: Wed Oct 18, 2006 10:43 pm
Posts: 490
Location: Kansas City, KS, USA
TylerH wrote:
The point was to have op do the leg work.

You must be new here. Everyone knows the OP never does the leg work. That's why we have all these already answered questions and "please do my homework for me" posts.


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], mrjbom and 68 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