Page 2 of 3

Re:building OS from scratch (HELP ME .......)

Posted: Thu Dec 18, 2003 8:19 am
by Pype.Clicker
neuRopac wrote: sir ,
thank you for your quick reply ....
but i tried to understand what u've said,but in vain . . . . . . . .::)
could u pls explain me that. i'm really tensed.
waiting for the earliest reply from you . . . . . . . . ::)
okay ... entering tutor mode.

You need to know that there are many formats for executable files, like COFF, ELF, AOUT, OBJ, PE, etc. all those are standards and informations about them are available on wotsit.

GRUB knows the ELF format and can load it correctly, but it doesn't know about other file formats. However, it does understand flat binary files that have the so-called "Multiboot header" included (look at the GRUB documentation if you need), so by asking -faout instead of -felf, you're actually generating a .bin file that GRUB will not understand correctly.

From there, you have two options:
- either you're working under windows, which uses PE/COFF as a native executable format, and your compiler/linker does not know anything about ELF formats, in which case you need to download another compiler toolchain that *will* support it. Don't ask where, i dunno.

- either you're working under linux, which uses ELF as native executable format (at least since kernel 2.2). However, the 'elf' target itself is not valid under linux. There are many variants of ELF, depending on whether you're on a 32 or 64 bits equipment or whether you're on big-endian or little-endian CPU. As there are chances you're on an Intel Pentium based system, "elf32-i386" should be the target you must use.

hope i made myself clearer this time ...

Re:building OS from scratch (HELP ME .......)

Posted: Fri Dec 19, 2003 12:06 am
by neuRopac
okay ... entering tutor mode.

You need to know that there are many formats for executable files, like COFF, ELF, AOUT, OBJ, PE, etc. all those are standards and informations about them are available on wotsit.

GRUB knows the ELF format and can load it correctly, but it doesn't know about other file formats. However, it does understand flat binary files that have the so-called "Multiboot header" included (look at the GRUB documentation if you need), so by asking -faout instead of -felf, you're actually generating a .bin file that GRUB will not understand correctly.

From there, you have two options:
- either you're working under windows, which uses PE/COFF as a native executable format, and your compiler/linker does not know anything about ELF formats, in which case you need to download another compiler toolchain that *will* support it. Don't ask where, i dunno.

- either you're working under linux, which uses ELF as native executable format (at least since kernel 2.2). However, the 'elf' target itself is not valid under linux. There are many variants of ELF, depending on whether you're on a 32 or 64 bits equipment or whether you're on big-endian or little-endian CPU. As there are chances you're on an Intel Pentium based system, "elf32-i386" should be the target you must use.

hope i made myself clearer this time ...

thankyou sir,

i understood the things.

now let me tell u what i did ?

my files are :

kernel_start.asm //kernel startup file which calls the kernel's main function

i created an object file of the above using nasm as

#nasm -f elf kernel_strat.asm -o ks.o

kernel.c //this is the kernel c file

i created an object file of the above using gcc as

#gcc -c kernel.c -o kernel.o

in linker script file named 'link.ld' i marked output format as 'elf32-i386'(OUTPUT_FORMAT("elf32-i386"))

and i linked those two object files using ld as follows...

#ld -N -T link.ld -o kernel.elf ks.o kernel.o

i got kernel.elf sizing 5.2 kb

i copied it into directory named kernel in the floppy and i tried to load it using grub...

unfortunately i got the same error

error 13:invalid or unsupported file format

please check the error and please do try to help me out.
=---------------8<-----------------
kernel_start.asm
////////////////
[BITS 32]
[global start]
[extern k_main] ;

start:
call k_main

cli ;
hlt ;
-------8<-----------------
kernel.c
////////////////////////////
#define WHITE_TXT 0x07 // white on black text

void k_clear_screen();
unsigned int k_printf(char *message, unsigned int line);


k_main() // like main in a normal C program
{
k_clear_screen();
k_printf("Hi!\nHow's this for a starter OS?", 0);
};

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

unsigned int k_printf(char *message, unsigned int line) // the message and then the line #
{
char *vidmem = (char *) 0xb8000;
unsigned int i=0;

i=(line*80*2);

while(*message!=0)
{
if(*message=='\n') // check for a new line
{
line++;
i=(line*80*2);
*message++;
} else {
vidmem=*message;
*message++;
i++;
vidmem=WHITE_TXT;
i++;
};
};

return(1);
};
----------8<--------------

Re:building OS from scratch (HELP ME .......)

Posted: Fri Dec 19, 2003 12:23 am
by neuRopac
ooppppsssssssss....

i forgot to add the linker script file.....now here it is
---------------------------------------------

link.ld
///////////////////////////////


OUTPUT_FORMAT("elf32-i386")
ENTRY(start)
SECTIONS
{
.text 0x100000 : {
code = .; _code = .; __code = .;
*(.text)
. = ALIGN(4096);
}
.data : {
data = .; _data = .; __data = .;
*(.data)
. = ALIGN(4096);
}
.bss :
{
bss = .; _bss = .; __bss = .;
*(.bss)
. = ALIGN(4096);
}
end = .; _end = .; __end = .;
}
--------------------------------------------------


please keep helping...

Re:building OS from scratch (HELP ME .......)

Posted: Fri Dec 19, 2003 5:05 am
by Pype.Clicker
could you tell me what OS and compiler (gcc --version) you use to compile ? And could you also pack your files in a ZIP that you attach ?...
I've never played much with grub-elf-loading, so i need your files to make sure what i'm gonna say is correct ...

Re:building OS from scratch (HELP ME .......)

Posted: Fri Dec 19, 2003 6:15 am
by neuRopac
sir ,

thank you...

i am using linux version 9.0 and i am using
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)


i created my simple c kernel...as

#nasm -f elf kernel_start.asm -o ks.o
#gcc -c kernel.c -o kernel.o
#ld -N -T link.ld -o kernel.elf ks.o kernel.o
ld: warning: cannot find entry symbol entry; defaulting to 000a0000
#
i was adviced not to consider the above warning

and i tried to boot using grub....


when i specified virtual address in linker script file as
virt=0xc0000000;

i got error as follows ....

error 28:selected item cannot fit into memory

and i changed my virt address in linker script file
as virt=0xa0000;

and i tried to load kernel

i got error as

Error 7: Loading below 1MB is not supported

i am not getting to find the appropriate addrees where i am trying
to load my kernel

please help me in this regard..........
and i have attached files u asked


[attachment deleted by admin]

Re:building OS from scratch (HELP ME .......)

Posted: Fri Dec 19, 2003 8:38 am
by Pype.Clicker
neuRopac wrote: ld: warning: cannot find entry symbol entry; defaulting to 000a0000
Your 'ENTRY(entry)' line in link.ld is wrong. You should be using ENTRY(start), as this is your actual entry point in kernel_start.asm
error 28:selected item cannot fit into memory
Hmm ... surprising... i'll try it at home tonight to see if i can reproduce the problem with a corrected linker script.
Error 7: Loading below 1MB is not supported
Actually, GRUB is not able to load kernels under 1MB barrier (don't ask why. I just read it can't in the docs) I don't think the problem comes from your vaddr, but more from the physical address

By the way, you're lucky the multiboot header comes at first, provided that you made nothing special in your linker to make it so.

Question for G? (Gnu Grub Gurus): is multiboot header required when loading ELF kernels ? I thought multiboot header was for binary formats only ...

Re:building OS from scratch (HELP ME .......)

Posted: Fri Dec 19, 2003 9:13 am
by frank
Question for G? (Gnu Grub Gurus): is multiboot header required when loading ELF kernels ? I thought multiboot header was for binary formats only ...
It's also required for ELF kernels. Oterwise grub will give an error.
Afaik you need to have an multiboot header with any format, but I'm not sure of that.

Re:building OS from scratch (HELP ME .......)

Posted: Fri Dec 19, 2003 9:55 am
by Tim
Pype.Clicker wrote:
error 28:selected item cannot fit into memory
Hmm ... surprising... i'll try it at home tonight to see if i can reproduce the problem with a corrected linker script.
This is normal. GRUB looks at the virtual address and treats it as a physical address. If you had more than 3GB installed, this would work.

Re:building OS from scratch (HELP ME .......)

Posted: Fri Dec 19, 2003 10:21 am
by Pype.Clicker
So how do we define the virtual address that GRUB should use ?

Re:building OS from scratch (HELP ME .......)

Posted: Fri Dec 19, 2003 11:49 pm
by neuRopac
even after changing the ENTRY(entry) to ENTRY(start),
i'm getting the same error . . ..

this time i'm not getting the Warning this time, but the GRUB says the same error..

help me out...
:( :(

Re:building OS from scratch (HELP ME .......)

Posted: Sat Dec 20, 2003 4:54 am
by Tim
Pype.Clicker wrote:So how do we define the virtual address that GRUB should use ?
I don't think you can when using ELF. I think you've got to keep your virtual address within the range of physical memory. Can any ELF experts confirm or deny this?

Re:building OS from scratch (HELP ME .......)

Posted: Sat Dec 20, 2003 5:01 am
by neuRopac
what i think is the kernel is being detected( b'coz the GRUB shows no error like file unrecocgonised.)

and i also feel that the kernel is loaded (b'coz i don't get any errors which i mentioned in the previous

posts).when i type the command boot at the GRUB prompt the print function is supposed to be executed

( if at all the kernel is loaded ) but it is not. immediately the system is rebooted.

please please help me out.

i 've been trying to solve this out for the past two days.

i'm really restless.

seeking some really graceful help from GURUS. ???

Re:building OS from scratch (HELP ME .......)

Posted: Sat Dec 20, 2003 6:09 am
by BI lazy
hmmm ... I suggest to tell the linker:

virt = 0xc0000000
phys = 0x100000

then I think GRUB is able to put the image at the right place in memory: at 0x100000.

I build an elf32 image for my kernel. It needs the multiboot header - but not such things like the a_out kludge.

hope this helps


stay safe

Re:building OS from scratch (HELP ME .......)

Posted: Sat Dec 20, 2003 6:53 am
by neuRopac
hai BI lazy,

i already specified
virt = 0xc0000000
phys = 0x100000

and i made elf32 kernel file ...

but i got error;;

selected item cannot fit into memory......
then i simpliy specified the load address as -Ttext=0x100000 in ld command
as 'os-dever.net tutorial-using GRUB' instructed,i got no error and i got some message as follows

grub>kernel /kernel/kernel.elf

multiboot-elf <0x100000:0x121:...><(some numbers-i forget that)>

entry=0x100000 shtab=somenumber...



but no error......

i thin kernel was being loaded

and i typed boot command

system gets rebooted.......

what can i do further........

Re:building OS from scratch (HELP ME .......)

Posted: Sat Dec 20, 2003 10:09 am
by BI lazy
well ... why do you say -Ttext=0x... if you have a linker script at hands?

Other thing: do you have paging set up?

Without that, you'd need to do some corrections to all the adresses in your program, to render them from virtual adresses to physical adresses.

So in your case, I'd also set virt=0x100000.

hth ... and do some research yourself, gosh.