compiling c kernel and linking with asm...

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
Mtype2
Posts: 13
Joined: Tue Sep 16, 2014 6:17 am

compiling c kernel and linking with asm...

Post by Mtype2 »

Hello im currently trying to link my kernel loader written in asm to my C kernel...
heres some code:

Code: Select all

[BITS 32]
extern _sys_entry
global x32
x32:
	mov		ax, data		; set data segments to data selector (0x10)
	mov		ds, ax
	mov		fs, ax
	mov		es, ax
	mov		gs, ax
	mov		esp, 90000h	
	call 	_sys_entry
	hlt


times 512-($-$$) db 0

Code: Select all

void sys_entry(void)
{
	writeLine("Kernel Loaded");
}
the problem is when i link them together ld outputs a bin file with the linked code with MZ and dos headers...
i want ld tomake a flat binary without any headers just an normal bin as nasm will ouput..
thanks for any help.

make.bat:

Code: Select all

call nasm source/BOOTZ.asm -f bin -o bin/BOOTZ.bin
call cd bin
call gcc -ffreestanding -fno-builtin -nostdlib -c C:\Users\Mark\Desktop\OS\source\kernel\*.c
call cd..

call nasm -f elf source/CORELD.asm -o bin/CORELD.o

call ld -Ttext 0x1000 -o bin/CORE.bin bin/*.o
PearOs
Member
Member
Posts: 194
Joined: Mon Apr 08, 2013 3:03 pm
Location: Usually at my keyboard!

Re: compiling c kernel and linking with asm...

Post by PearOs »

Mtype2 wrote:Hello im currently trying to link my kernel loader written in asm to my C kernel...
heres some code:

Code: Select all

[BITS 32]
extern _sys_entry
global x32
x32:
	mov		ax, data		; set data segments to data selector (0x10)
	mov		ds, ax
	mov		fs, ax
	mov		es, ax
	mov		gs, ax
	mov		esp, 90000h	
	call 	_sys_entry
	hlt


times 512-($-$$) db 0

Code: Select all

void sys_entry(void)
{
	writeLine("Kernel Loaded");
}
the problem is when i link them together ld outputs a bin file with the linked code with MZ and dos headers...
i want ld tomake a flat binary without any headers just an normal bin as nasm will ouput..
thanks for any help.

make.bat:

Code: Select all

call nasm source/BOOTZ.asm -f bin -o bin/BOOTZ.bin
call cd bin
call gcc -ffreestanding -fno-builtin -nostdlib -c C:\Users\Mark\Desktop\OS\source\kernel\*.c
call cd..

call nasm -f elf source/CORELD.asm -o bin/CORELD.o

call ld -Ttext 0x1000 -o bin/CORE.bin bin/*.o
"call nasm -f elf source/CORELD.asm -o bin/CORELD.o"

Shouldn't that say "nasm -f bin"? You told it to use ELF that's why you are seeing the headers. Unless you wanted elf...

- Matt
Mtype2
Posts: 13
Joined: Tue Sep 16, 2014 6:17 am

Re: compiling c kernel and linking with asm...

Post by Mtype2 »

i have already tried that...ld says that... File format not recognized.
maybe you misunderstood me...

im compiling CORELD.asm into an object file.
after its compiled i feed it to ld inorder to link it with kernel.o and terminal.o
but unfortunately i get the MZ header and an ugly formatted bin file that messes all my adresses.
PearOs
Member
Member
Posts: 194
Joined: Mon Apr 08, 2013 3:03 pm
Location: Usually at my keyboard!

Re: compiling c kernel and linking with asm...

Post by PearOs »

Mtype2 wrote:i have already tried that...ld says that... File format not recognized.
maybe you misunderstood me...

im compiling CORELD.asm into an object file.
after its compiled i feed it to ld inorder to link it with kernel.o and terminal.o
but unfortunately i get the MZ header and an ugly formatted bin file that messes all my adresses.
Perhaps I did, but lets take a look at something.

"call nasm -f elf source/CORELD.asm -o bin/CORELD.o"
Here you compile CORELD.asm to CORELD.o in the bin directory as an "ELF" Binary.

"call ld -Ttext 0x1000 -o bin/CORE.bin bin/*.o"
Here you use LD which I assume is a linking tool of some sort which uses a wild card in the directory "bin" with card ".o". What file lies in there? Oh just the ELF binary file "CORELD.o". That lies your issue, if you are getting headers that are messing up your addresses, then either two things are happening:

1. You don't want the elf format and because you are using it you are getting headers.
or
2. You want the elf format and I am misunderstanding this altogether and there is a problem somewhere else.

Now I'm not 100% what you are trying to accomplish thus this is hard for me to help solve your problem, also considering I don't use LD so perhaps maybe someone else knows whats wrong. Either way good luck.
Cheers
Mtype2
Posts: 13
Joined: Tue Sep 16, 2014 6:17 am

Re: compiling c kernel and linking with asm...

Post by Mtype2 »

:D ...
the *.o means "input all the objects in the specified directory."
in the bin folder i have CORELD.O TERMINAL.O KERNEL.O
so when i'm calling LD it links all the three files just fine... i can confirm that by looking at the linked bin file with a hex editor and suddenly finding a string which i declared in KERNEL.O
so.. the linking process goes as supposed (kinda)...

however, the problem is that i get mz headers,gcc coments,and some other irrelevant stuff inside my final bin file,so when its loaded i cant jump to it cuz the offsets are messed(or the mz code isnt readable... :/ )[its loaded by stage0 from the disk to 0x1000]

whats the correct way of linking the kernel with the loader?
are there any alternatives? with examples please
thanks :D
PearOs
Member
Member
Posts: 194
Joined: Mon Apr 08, 2013 3:03 pm
Location: Usually at my keyboard!

Re: compiling c kernel and linking with asm...

Post by PearOs »

Mtype2 wrote::D ...
the *.o means "input all the objects in the specified directory."
in the bin folder i have CORELD.O TERMINAL.O KERNEL.O
so when i'm calling LD it links all the three files just fine... i can confirm that by looking at the linked bin file with a hex editor and suddenly finding a string which i declared in KERNEL.O
so.. the linking process goes as supposed (kinda)...

however, the problem is that i get mz headers,gcc coments,and some other irrelevant stuff inside my final bin file,so when its loaded i cant jump to it cuz the offsets are messed(or the mz code isnt readable... :/ )[its loaded by stage0 from the disk to 0x1000]

whats the correct way of linking the kernel with the loader?
are there any alternatives? with examples please
thanks :D
Ok so we are both on the same page :D From what I've read, never done this, but you need to strip the unwanted headers from the input files.. I forget what the tool is, but that's what you'll need to do. Sorry if that's an unhelpful answer, but at least it will get you in the right direction.

- Matt
Mtype2
Posts: 13
Joined: Tue Sep 16, 2014 6:17 am

Re: compiling c kernel and linking with asm...

Post by Mtype2 »

Anybody? it seems to be funny to stuck at linking stage when its the only barrier between you and programming something decent in the OS :?
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: compiling c kernel and linking with asm...

Post by iansjack »

Use the "--oformat=binary" swtch in the ld command.
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: compiling c kernel and linking with asm...

Post by Combuster »

Posting Checklist. I don't trust that ld. I certainly don't trust the place where you got the linker command from. Positioned binaries should always be made using a linker script.
"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 ]
Mtype2
Posts: 13
Joined: Tue Sep 16, 2014 6:17 am

Re: compiling c kernel and linking with asm...

Post by Mtype2 »

OK. i have succesfully made a binary from the loader and the kernel and the boot0 jumps to the loader with no problems... untill i call my _sys_entry c func from pmode... i get the following error:

write virtual checks:no write acess to seg...
add byte ptr ds:[eax] , al:0000

and a BXVGA panic !?

whats wrong?

build:

Code: Select all

call nasm source/BOOTZ.asm -f bin -o bin/BOOTZ.bin
call cd bin
call gcc -nostdlib -m32 -c C:\Users\Mark\Desktop\OS\source\kernel\*.c
call cd..

call nasm -f elf source/CORELD.asm -o bin/CORELD.o

call ld -Ttext 0x0000 -mi386pe -o bin\CORE2.bin bin\CORELD.o bin\Terminal.o bin\Kernel.o

call objcopy -O binary bin\CORE2.bin bin\CORE.bin
call dd if=bin\BOOTZ.bin of=image\OS.img bs=512 count=1
call dd if=bin\CORE.bin of=image\OS.img bs=512 seek=1
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: compiling c kernel and linking with asm...

Post by iansjack »

The error message seems fairly clear but, without seeing your GDT and IDT or your writeLine function, it's just guesswork. Ckeck your segment selectors. And I would advise you to learn how to use a debugger to help you trace errors.

You are at a fairly simple stage at the moment and if you don't learn how to deal with simple errors by yourself you are going to be really lost when things get more complicated.
Last edited by iansjack on Thu Sep 18, 2014 5:57 am, edited 1 time in total.
Octocontrabass
Member
Member
Posts: 5590
Joined: Mon Mar 25, 2013 7:01 pm

Re: compiling c kernel and linking with asm...

Post by Octocontrabass »

Mtype2 wrote:whats wrong?

Code: Select all

call gcc
Did you read the posting checklist yet? We can't help you properly until you set up a real cross-compiler.

The error message from Bochs suggests a problem with your bootloader. Did you finish fixing it yet?
mallard
Member
Member
Posts: 280
Joined: Tue May 13, 2014 3:02 am
Location: Private, UK

Re: compiling c kernel and linking with asm...

Post by mallard »

BTW, you do realise you don't have to put "call" in front of everything in a batch file, right? The only time it's needed is when you're running another batch file...
Image
Mtype2
Posts: 13
Joined: Tue Sep 16, 2014 6:17 am

Re: compiling c kernel and linking with asm...

Post by Mtype2 »

Code: Select all

set32bit:
    mov si,string4
	call echo
	mov ah,0x00
    int 16h
    mov ah,0x00
	mov al,13h
	int 0x10	
	mov eax, cr0 
	or  eax, 1     ; set PE (Protection Enable) bit in CR0 (Control Register 0)
	mov cr0, eax
	jmp DWORD code:x32+0x10000

[BITS 32]
extern _sys_entry
global x32
x32:
	
	mov		ax, data		; set data segments to data selector (0x10)
	mov		ds, ax
	mov		fs, ax
	mov		es, ax
	mov		gs, ax
	jmp		DWORD _sys_entry
	hlt


times 512-($-$$) db 0
thats the loader code... the gdt is fine cuz it works as expected without the jmp sysentry...
registers are:
CS=0008
DS=0010
SS=1000//CANT FIX IT
ES=0010
FS=0010
GS=0010

as you can see im offsetting 0x10000 to each segment and the gdt
Mtype2
Posts: 13
Joined: Tue Sep 16, 2014 6:17 am

Re: compiling c kernel and linking with asm...

Post by Mtype2 »

oh..lol... i actually didnt read enough sectors...my image is 21kb in size and i read only 2 sectors..changed it to 45 and voilla..it works... gonna buy some greygoose and celebrate :D thanks for help anyway
Post Reply