Cross Compilier problem

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.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Cross Compilier problem

Post by neon »

Hey everyone,

After some issues with setting up my OSs kernel, I have decided to give a cross compilier a shot.

I am using GCC, running in Cygwin on a WinXP SP2 machine.

Does the cross compilier run in a script (Like a batch program)? I created a *.sh file for Cygwin, containing a simple "dir" command.

It seems like my Cygwin batch files are not effecting anything within
Cygwin (Current directory never changes), which is making it impossible
for me to develope a cross compilier.

I could execute a "dir" command directly within Cygwin, but not within
the batch file!

I know its probably something simple, but I do not have much exp with
Linux command line.

Does anyone have any suggestions?

Thanks!
User avatar
ucosty
Member
Member
Posts: 271
Joined: Tue Aug 08, 2006 7:43 am
Location: Sydney, Australia

Post by ucosty »

I can only suggest, at this stage, that you read the following document:

http://www.osdev.org/wiki/GCC_Cross-Compiler

It is a comprehensive guide to compiling a cross-compiler on cygwin.
The cake is a lie | rackbits.com
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

Thats the tutorial that I am having problems with...
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

neon wrote:Thats the tutorial that I am having problems with...
It looks pretty strait forward.. :?
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

It is, Im just wondering if I could put it in a script (Simular to Win32 batch files), and if so, how?

The tutorial doesnt explain that anywhere..
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:

Post by Combuster »

configure is a script. If shell scripts are causing trouble, consider removing and reinstalling cygwin. It wouldn't be the first time that worked :roll:
"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
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

I know its something simple, but what do you mean by 'configure'?

Im currently trying to get it to work with DJGPP (Cygwin has been giving me alot of problems)
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

Im setting up a 2 stage bootloader (to fix a previous issue with C routines)

I seem to be having a small issue with my linker script though. My
bootloader enters pmode fine, then I attempt to jump to my 2nd stage
loader:

Code: Select all

jmp 08h:1000h
Keep in mind, I load my kernel at 0x1000, and Im using NASM with
DJGPP. Also, The bootloader is in pmode.

My 2nd stage is in another source file (Stage2.asm):

Code: Select all

[BITS 32]

extern _main
global _Stage2

_Stage2:
	hlt
	call _main
How would I insure _Stage2 is loaded at 0x1000? My linker (ld)
is giving me the error "Cannot find entry symbol '_Stage2' "

Here is the linker script Im using:

Code: Select all

ENTRY(_Stage2)
OUTPUT_FORMAT("binary") 
SECTIONS 
{ 
    .text 0x1000 : 
    { 
        code = .; _code = .; __code = .; 
        *(.text) 
        . = ALIGN(4096); 
    } 

    .data : 
    { 
        data = .; _data = .; __data = .; 
        *(.data) 
        . = ALIGN(4096); 
    } 


    .bss : 
    { 
        bss = .; _bss = .; __bss = .; 
        *(.bss) 
        . = ALIGN(4096); 
    } 

    end = .; _end = .; __end = .; 
}
Im not worried about the .rodata section yet, until Im able to jump to
my C kernel's main() routine.

Here is the command line..

Code: Select all

nasm -f bin BOOT.asm -o BOOT.bin
nasm -f elf Stage2.asm -o Stage2.o

// Compile main C kernel
gcc -ffreestanding -nostdlib -nostartfiles -fno-builtin -c -o Main.o Main.c

// Link the C kernel with 2nd stage loader
ld Link.ld -c KERNEL.o Main.o Stage2.o

objcopy -R .note -R .comment -S -O binary KERNEL.o KERNEL.bin

makeboot Floppy.img BOOT.bin KERNEL.o
Does anyone know what could cause this? The exact error I get is

Code: Select all

c:/djgpp/bin/ld.exe: warning: cannot find entry symbol _Stage2; defaulting to 000018
(Okay its a warning, not an error--Its still a big problem though)

Thanks alot for any help!
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

makeboot Floppy.img BOOT.bin KERNEL.o
...Found the problem :oops:

It seems to work now. Lets see if I could fix the C string issue next.. :D

Im going to try going with DJGPP for now, and see what happens.

Thanks!
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

It is calling main() just fine right now, but I am still getting that warning
(Weird)

Unfortanly, Im back with a problem (That I was hoping to solve). I am
unable to define any routine before main.

This works just fine (In my kernel):

Code: Select all

void main () {

	__asm ("hlt");
}
As soon as I define a routine before main, my kernel triple faults:

Code: Select all

void foo () {}

void main () {

	__asm ("hlt");
}
What am I doing wrong?

This is one of the issues that stopped my OS development before..

I want to add on that defining a routine after main works. It only
triple faults when it is before main.
Last edited by neon on Sun Mar 18, 2007 6:23 pm, edited 1 time in total.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

Here is the output from Bosch:

Code: Select all


00000003740i[BIOS ] $Revision: 1.166 $ $Date: 2006/08/11 17:34:12 $
00000317563i[KBD  ] reset-disable command received
00000318691i[PIDE ] new BM-DMA address: 0xc000
00000450000i[WGUI ] dimension update x=720 y=400 fontheight=16 fontwidth=9 bpp=8
00004462070i[CPU0 ] LOCK prefix unallowed (op1=0x53, attr=0x0, mod=0x0, nnn=0)
00004462070e[CPU0 ] interrupt(): gate descriptor is not valid sys seg
00004462070e[CPU0 ] interrupt(): gate descriptor is not valid sys seg
00004462070i[CPU0 ] protected mode
00004462070i[CPU0 ] CS.d_b = 32 bit
00004462070i[CPU0 ] SS.d_b = 32 bit
00004462070i[CPU0 ] | EAX=00000000  EBX=00001000  ECX=00000002  EDX=000003f2
00004462070i[CPU0 ] | ESP=00090000  EBP=00000000  ESI=00007cbc  EDI=0000ffde
00004462070i[CPU0 ] | IOPL=0 id vip vif ac vm RF nt of df if tf SF zf af pf cf
00004462070i[CPU0 ] | SEG selector     base    limit G D
00004462070i[CPU0 ] | SEG sltr(index|ti|rpl)     base    limit G D
00004462070i[CPU0 ] |  CS:0008( 0001| 0|  0) 00000000 000fffff 1 1
00004462070i[CPU0 ] |  DS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00004462070i[CPU0 ] |  SS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00004462070i[CPU0 ] |  ES:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00004462070i[CPU0 ] |  FS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00004462070i[CPU0 ] |  GS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00004462070i[CPU0 ] | EIP=00000003 (00000003)
00004462070i[CPU0 ] | CR0=0x00000011 CR1=0 CR2=0x00000000
00004462070i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00004462070i[CPU0 ] >> lock push ebx : F053
00004462070e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
I am suspected why IP would be 3 though..
User avatar
ucosty
Member
Member
Posts: 271
Joined: Tue Aug 08, 2006 7:43 am
Location: Sydney, Australia

Post by ucosty »

I (think) the warning is being caused by the underscore. The GCC cross compiler doesn't prefix underscores to the beginning of function names. That warning is telling you it can't find the function you are calling and is defaulting.
The cake is a lie | rackbits.com
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

I tried it with and without underscores...

Code: Select all

stage2
_stage2
__stage2
___stage2
____stage2   -I actually tried this :)
Still get the same warning though...

I wonder, how can my kernel still boot up (and run) successfully if it
is being loaded at the wrong address? o.0

Any ideas?

My second stage loader is loaded at 0x1000..

Of course, I am still unable to define routines before main (Triple fault).
Mabey _main is being loaded at 0x1000? (Dont see how though..)

Code: Select all

c:/djgpp/bin/ld.exe: warning: cannot find entry symbol Stage2; defaulting to 000018f8
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Okay, from what I read, your problem isn't what you think it is...

When LD links and can't find the symbol, it sets a default value for the entry point. Usually it's correct, but sometimes it's not (triple fault would happen then).

The warning says that it can't find Stage2, NOT stage2. IIRC, C is case-sensitive, which means that Stage2 is very different from stage2. Try renaming, in your C file, stage2 to Stage2, or in your linker script (or whatever you use) change Stage2 to stage2.

Try my idea, prove me wrong, please :D.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

Actually, I written it wrong in my post.. (Sorry)

I did use Stage2 and stage2 varants, and still doesnt work.

I should let you know, _Stage2 is defined in an Assembly file, not
a C source file. Mabey Im linking it wrong?

Here is Stage2.asm:

Code: Select all

; We should be in pmode, so insure code is 32bit
[BITS 32]

extern _main
global _Stage2

; --------------------------------- Second stage loader
_Stage2:

; might need to setup stack here

; Give control to kernel
	call	_main
	cli	; Should never get here
	hlt
My bootloader goes into pmode, sets up the stack, and jumps
to 08h:1000h (Where it loads my 2nd stage at)

This is when _Stage2 (Supposed) to take over, and call _main()

Heres my command line:

Code: Select all

nasm -f bin BOOT.asm -o BOOT.bin 
nasm -f elf Stage2.asm -o Stage2.o   <<<< _Stage2 links here

// Compile main C kernel 
gcc -ffreestanding -nostdlib -nostartfiles -fno-builtin -c -o Main.o Main.c 

// Link the C kernel with 2nd stage loader 
ld Link.ld -c KERNEL.o Main.o Stage2.o 

objcopy -R .note -R .comment -S -O binary KERNEL.o KERNEL.bin 

makeboot Floppy.img BOOT.bin KERNEL.o 
Do you see any problem in the way I link? (Or something else?)
I tried both COFF and ELF formats when linking, but same problem..

Thanks for the help!
Post Reply