Linking C and ASM files

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
Faust
Posts: 3
Joined: Mon Jan 17, 2011 10:43 pm

Linking C and ASM files

Post by Faust »

I have been studying theory for a while to make my own OS, so now I am at the very beggining of this.

I have made my own bootloader, wich boots from a CD correctly. Then, it loads a program off the disk and jump to it.

My problem is that I can run a program written in asm, but once it comes to C, Im not able to compile/assemble/link everything in the right way. I always end up with some error, eg. ld:cannot perform PE operations on non-PE file.
So, my question is, could someone explain me clearly how to do this ?

I am working under Windows with Cygwin.

boot.asm

Code: Select all

[ORG 0]
	
            jmp 07C0h:start     ; Goto segment 07C0
	
    start:
            ; Update the segment registers
            mov ax, cs
            mov ds, ax
            mov es, ax

    reset:                      ; Reset the floppy drive
            mov ax, 0           ;
            mov dl, 0           ; Drive=0 (=A)
            int 13h             ;
            jc reset            ; ERROR => reset again

    read:
            mov ax, 1000h       ; ES:BX = 1000:0000
            mov es, ax          ;
            mov bx, 0           ;

            mov ah, 2           ; Load disk data to ES:BX
			mov dl, 0           ; Drive=0
			mov dh, 0           ; Head=0
			mov ch, 0           ; Cylinder=0
			mov cl, 2           ; Sector=2
            mov al, 5           ; Load 5 sectors
            
            int 13h             ; Read!

            jc read             ; ERROR => Try again

	program:
            jmp 1000h:0000      ; Jump to the program

    times 510-($-$$) db 0
    dw 0AA55h
loader.asm

Code: Select all

global loader           
extern _kmain            ; kmain from kernel.c
 
section .text
align 4
 
	; reserve initial kernel stack space
	STACKSIZE equ 0x4000                  ; that's 16k.
		
	loader:
			mov esp, stack+STACKSIZE           ; set up the stack
			
			mov al, 'R'
            mov ah, 0Eh     ; Print AL
            mov bx, 7
            int 10h
			
			push eax                           ; pass Multiboot magic number
			push ebx                           ; pass Multiboot info structure
			call  _kmain                       ; call kernel proper
			   
			cli
	hang:
			hlt                                ; halt machine should kernel return
			jmp   hang
 
section .bss
align 4
stack:
resb STACKSIZE                     ; reserve 16k stack on a doubleword boundary
kernel.c

Code: Select all

void kmain( void* mbd, unsigned int magic )
{
   unsigned char *videoram = (unsigned char *) 0xb8000;
   videoram[0] = 65;
   videoram[1] = 0x07;
}
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Linking C and ASM files

Post by xenos »

Try using a GCC Cross-Compiler - it takes a bit of time to compile it, but once you set up a cross compile toolchain, it saves you from a lot of trouble.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
Muneer
Member
Member
Posts: 104
Joined: Tue Nov 02, 2010 2:05 am
Location: India

Re: Linking C and ASM files

Post by Muneer »

If you ever change from cygwin to djgpp(which I use), although everyone else here suggests a GCC cross compiler, and if you fail to set it up, then this is the way to go
http://forum.osdev.org/viewtopic.php?f= ... 71#p186871
Even the smallest person could change the course of the future - Lord Of The Rings.

In the end all that matters is what you have done - Alexander.

Even after a decade oh god those still gives me the shivers.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Linking C and ASM files

Post by Combuster »

Do I really have to follow you with paintspray?

DJGPP is for DOS, it's old, has a ton of known issues, and is about the most inferior solution you can try on a windows machine. That means you are absolutely not helping the OP with any of this.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: Linking C and ASM files

Post by Chandra »

Faust wrote:My problem is that I can run a program written in asm, but once it comes to C, Im not able to compile/assemble/link everything in the right way. I always end up with some error, eg. ld:cannot perform PE operations on non-PE file.
What output format are you using for Nasm and Cygwin? Can you post your script file?
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Linking C and ASM files

Post by Combuster »

cannot perform PE operations on non-PE file
A frequently asked question, solved by using the cross-compiler linked previously instead of trying to get a linker to do something it was not configured to do: OS development is not windows development.

Edit: now also listed in the official FAQ, I have seen the message pop up way too often even though it is mentioned from the starting articles on the wiki.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
b.zaar
Member
Member
Posts: 294
Joined: Wed May 21, 2008 4:33 am
Location: Mars MTC +6:00
Contact:

Re: Linking C and ASM files

Post by b.zaar »

I found I had a few troubles with cygwin even with a cross compiler. One of them was ld not linking more than 5 externs to an elf32 object file.
I'm now using andLinux (ubuntu 9.04) with a cross compiler. It works a lot faster than cygwin as well.
"God! Not Unix" - Richard Stallman

Website: venom Dev
OS project: venom OS
Hexadecimal Editor: hexed
Faust
Posts: 3
Joined: Mon Jan 17, 2011 10:43 pm

Re: Linking C and ASM files

Post by Faust »

So, I made a cross-compiler, but it' still not working.
Well, at least I don't get the cannot perform Pe operations.... so my i586-elf-ld links properly ( I think )

I made a little test to check where was the error, maybe someone can figure out what it is.

boot.asm : a bootloader that loads the program starting on the 2nd sector. This works fine.
loader.asm : a program that calls the kernel. For the test, it only shows 'R' on the screen.

First, I compile boot.asm with nasm. Nothing to report, no error.
Then I compile a first version of loader.asm with nasm, wich is directly a binary file.
Everything is fine. Shows 'R'.
Then, a second version of loader.asm, compiled with nasm, output as ELF, and linked with i586-elf-ld, output as binary.
Now, it doesn't show anything.

Opening these two versions on an HEX editor, I noticed they were slightly differents. Quite similar, but there is a '66' added before some instructions, wich is, I read somewhere, an indicator about the length of the register in the next instruction(32/64). So maybe it's something about that.

( I don't expect you to understand this, just to show... )
Working :
00000000: 66 bc 18 40 00 00 b0 52 b4 0e bb 07 00 cd 10 66
00000010: 50 66 53 fa f4 e9 fc ff
Not working :
00000000: bc 00 d0 04 08 b0 52 b4 0e 66 bb 07 00 cd 10 50
00000010: 53 fa f4 e9 fa ff ff

If you have any idea how to solve this, any suggestion, I would appreciate. :)
Tosi
Member
Member
Posts: 255
Joined: Tue Jun 15, 2010 9:27 am
Location: Flyover State, United States
Contact:

Re: Linking C and ASM files

Post by Tosi »

The linker is adding 16-bit override prefixes because it is targeting a 32-bit host. And I don't think ELF supports real mode code either. Just use a flat binary for the second stage loader, and only use ELF for the kernel proper, once you have entered protected mode.
Faust
Posts: 3
Joined: Mon Jan 17, 2011 10:43 pm

Re: Linking C and ASM files

Post by Faust »

Problem is flat binary doesn't support external references, so I can't link second stage loader and C file.
Should I enter protected mode during the bootload program ?
User avatar
b.zaar
Member
Member
Posts: 294
Joined: Wed May 21, 2008 4:33 am
Location: Mars MTC +6:00
Contact:

Re: Linking C and ASM files

Post by b.zaar »

Could just need a 'bits 16' directive in the asm code.
"God! Not Unix" - Richard Stallman

Website: venom Dev
OS project: venom OS
Hexadecimal Editor: hexed
Post Reply