Page 1 of 1

compile simple os on linux

Posted: Mon Jul 07, 2008 10:14 am
by majorek
Hi!
I have a simple OS , which is wrote to compile with nasm and DJGPP , now i want to compile it on linux with nasm and gcc

Here are sources: http://binboy.sphere.pl/down.php?idmk=36

when I compile it on linux i have:

Code: Select all

   1.  wojtek@wojtek-desktop:~/Pulpit/doc3_files$ make
   2. nasm start.asm -o start.o -f coff
   3. cc -O2 -fomit-frame-pointer -I. -nostdinc -fno-builtin  -c -o main.o main.c
   4. cc -O2 -fomit-frame-pointer -I. -nostdinc -fno-builtin  -c -o intr.o intr.c
   5. cc -O2 -fomit-frame-pointer -I. -nostdinc -fno-builtin  -c -o cons.o cons.c
   6. nasm irqroute.asm -o irqroute.o -f coff
   7. cc -O2 -fomit-frame-pointer -I. -nostdinc -fno-builtin  -c -o kbd.o kbd.c
   8. cc -O2 -fomit-frame-pointer -I. -nostdinc -fno-builtin  -c -o sched.o sched.c
   9. nasm exc.asm -o exc.o -f coff
  10. cc -O2 -fomit-frame-pointer -I. -nostdinc -fno-builtin  -c -o traps.o traps.c
  11. ld -Tkernel.ld -o kernel.bin start.o main.o intr.o cons.o irqroute.o kbd.o sched.o exc.o traps.o
  12. start.o: file not recognized: File format not recognized
  13. make: *** [kernel.bin] Błąd 1
  14. wojtek@wojtek-desktop:~/Pulpit/doc3_files$

Linux don't uses coff files, so in makefile I chanded:

nasm $*.asm -o $*.o -f coff

now is :

nasm $*.asm -o $*.o -f elf

But is still don't works

Code: Select all

   1.  wojtek@wojtek-desktop:~/Pulpit/doc3_files$ make
   2. nasm start.asm -o start.o -f elf
   3. cc -O2 -fomit-frame-pointer -I. -nostdinc -fno-builtin  -c -o main.o main.c
   4. cc -O2 -fomit-frame-pointer -I. -nostdinc -fno-builtin  -c -o intr.o intr.c
   5. cc -O2 -fomit-frame-pointer -I. -nostdinc -fno-builtin  -c -o cons.o cons.c
   6. nasm irqroute.asm -o irqroute.o -f elf
   7. cc -O2 -fomit-frame-pointer -I. -nostdinc -fno-builtin  -c -o kbd.o kbd.c
   8. cc -O2 -fomit-frame-pointer -I. -nostdinc -fno-builtin  -c -o sched.o sched.c
   9. nasm exc.asm -o exc.o -f elf
  10. cc -O2 -fomit-frame-pointer -I. -nostdinc -fno-builtin  -c -o traps.o traps.c
  11. ld -Tkernel.ld -o kernel.bin start.o main.o intr.o cons.o irqroute.o kbd.o sched.o exc.o traps.o
  12. start.o: In function `_start.1':
  13. start.asm:(.text+0x5f): undefined reference to `_start_kernel'
  14. start.o: In function `_irq1':
  15. start.asm:(.text+0x7a): undefined reference to `_do_irq1'
  16. start.o: In function `_irq0':
  17. start.asm:(.text+0x9a): undefined reference to `_do_irq0'
  18. main.o: In function `start_kernel':
  19. main.c:(.text+0x6a): undefined reference to `irq0'
  20. intr.o: In function `set_intr_gate':
  21. intr.c:(.text+0x1a): undefined reference to `idt'
  22. intr.c:(.text+0x21): undefined reference to `idt'
  23. intr.o: In function `set_trap_gate':
  24. intr.c:(.text+0x4a): undefined reference to `idt'
  25. intr.c:(.text+0x51): undefined reference to `idt'
  26. intr.o: In function `set_system_gate':
  27. intr.c:(.text+0x7a): undefined reference to `idt'
  28. intr.o:intr.c:(.text+0x81): more undefined references to `idt' follow
  29. kbd.o: In function `kbd_init':
  30. kbd.c:(.text+0x47): undefined reference to `irq1'
  31. exc.o: In function `exception_table':
  32. exc.asm:(.text+0x0): undefined reference to `_do_exc0'
  33. exc.asm:(.text+0x4): undefined reference to `_do_exc1'
  34. exc.asm:(.text+0x8): undefined reference to `_do_exc2'
  35. exc.asm:(.text+0xc): undefined reference to `_do_exc3'
  36. exc.asm:(.text+0x10): undefined reference to `_do_exc4'
  37. exc.asm:(.text+0x14): undefined reference to `_do_exc5'
  38. exc.asm:(.text+0x18): undefined reference to `_do_exc6'
  39. exc.asm:(.text+0x1c): undefined reference to `_do_exc7'
  40. exc.asm:(.text+0x20): undefined reference to `_do_exc8'
  41. exc.asm:(.text+0x24): undefined reference to `_do_exc9'
  42. exc.asm:(.text+0x28): undefined reference to `_do_exc10'
  43. exc.asm:(.text+0x2c): undefined reference to `_do_exc11'
  44. exc.asm:(.text+0x30): undefined reference to `_do_exc12'
  45. exc.asm:(.text+0x34): undefined reference to `_do_exc13'
  46. exc.asm:(.text+0x38): undefined reference to `_do_exc14'
  47. traps.o: In function `trap_init':
  48. traps.c:(.text+0x7): undefined reference to `exc0'
  49. traps.c:(.text+0x1b): undefined reference to `exc1'
  50. traps.c:(.text+0x2f): undefined reference to `exc2'
  51. traps.c:(.text+0x43): undefined reference to `exc3'
  52. traps.c:(.text+0x57): undefined reference to `exc4'
  53. traps.c:(.text+0x6b): undefined reference to `exc5'
  54. traps.c:(.text+0x7f): undefined reference to `exc6'
  55. traps.c:(.text+0x93): undefined reference to `exc7'
  56. traps.c:(.text+0xa7): undefined reference to `exc8'
  57. traps.c:(.text+0xbb): undefined reference to `exc9'
  58. traps.c:(.text+0xcf): undefined reference to `exc10'
  59. traps.c:(.text+0xe3): undefined reference to `exc11'
  60. traps.c:(.text+0xf7): undefined reference to `exc12'
  61. traps.c:(.text+0x10b): undefined reference to `exc13'
  62. traps.c:(.text+0x11f): undefined reference to `exc14'
  63. make: *** [kernel.bin] Błąd 1
  64. wojtek@wojtek-desktop:~/Pulpit/doc3_files$

How to do this ?

Re: compile simple os on linux

Posted: Mon Jul 07, 2008 10:37 am
by whowhatwhere
Some windows compilers prepend an underscore "_" to function names when switching between ASM and C/C++. You need to either add or remove leading underscores to each function name in your sources for it to compile.

All global functions in *.asm (like _start_kernel) need to have the prepended underscore removed. (_start_kernel) becomes (start_kernel).

Hope this helps. =)

EDIT: I haven't looked at your code at all, but I know people moving away from *broken* compilers have issues which can be resolved by the above procedures.

Re: compile simple os on linux

Posted: Mon Jul 07, 2008 10:40 am
by suthers
You should define all the asm functions you want to call from asm as global.... otherwise your C code won't be able to link to them...
(You probably have other problems, but start with that to diminish the number of errors...)
Jules

Re: compile simple os on linux

Posted: Mon Jul 07, 2008 11:38 am
by majorek
Thank's , I have changed all names, but now , when I run this OS in grub, I have an 13 error: not supported file format ;/

I think, because I have changed nasm output file format from coff to elf,...but linux don't supported coff file system ;/ , any idea's ?

Re: compile simple os on linux

Posted: Mon Jul 07, 2008 12:29 pm
by eax
Im new myself so I can only be of limited help but I got that error when the assembler was assembling in one format and the linker another

heres my files but I dont do GCC and ld I do i586-gcc and i586-ld because I compile on linux for windows, so I do it the opposite from you I think :) Still I had that exact same error you had but dont get it now, heres my link.ld linker which performas compilation and linking, you probably want like someone say the leading underscores flag on or of, see my link.ld

#link.ld START
OUTPUT_FORMAT("elf32-i386")

ENTRY(start)

phys = 0x00100000;

SECTIONS

{

.text phys : AT(phys) {

code = .;

*(.text)

*(.rodata)

. = ALIGN(4096);

}

.data : AT(phys + (data - code))

{

data = .;

*(.data)

. = ALIGN(4096);

}

.bss : AT(phys + (bss - code))

{

bss = .;

*(.bss)

. = ALIGN(4096);

}

end = .;

}

#link.ld END

Ok heres my makefile

#makefile START
cross:
@echo Now assembling, compiling, and linking your kernel

nasm -f elf -o start.o start.asm
#rem Remember this spot here: We will add 'i586-elf-gcc' commands here to compile C sources

i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o main.o main.c

i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o scrn.o scrn.c

i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o gdt.o gdt.c

i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o idt.o idt.c

i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o isrs.o isrs.c

i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o irq.o irq.c

i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o timer.o timer.c

i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o kb.o kb.c
i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fleading-underscore -I./include -c -o kbintst.o kbintst.c

#rem This links all your files. Remember that as you add *.o files, you need to

#rem add them after start.o. If you don't add them at all, they won't be in your kernel!

i586-elf-ld -T link.ld -o kernel.bin start.o main.o scrn.o gdt.o idt.o isrs.o irq.o timer.o kb.o kbintst.o

echo Cleaning up object files...

rm *.o
rm *.c~
@echo Done
#makefile END

The :CROSS part can be called whatever you want is just a label so I can do make cross and it all builds

BTW I also get unsupported executable format if I have the format to "Binary" in my linker file and compile on linux, actually if you did it like I have I THINK you should be able to compile on both but Im unsure if you would need to build a cross compiler ? Anyway I put this in just to give you some ideas but other will be able to steer you in the right direction but I had to faff about for a while as well to get rid of that erro 13, its quite a common error

Re: compile simple os on linux

Posted: Mon Jul 07, 2008 12:29 pm
by Adek336
Cześć,

elf is just fine, I load my mboot compliant kernel with Grub.

Code: Select all

mboot:
 dd 0x1BADB002	; Sygnatura
 dd 0x10001 ; Flagi dla bootloadera
 dd -(0x1BADB002+0x10001) ; suma kontrolna nag��wka
 dd mboot ; Pozycja nag��wka w pliku
 dd code
 dd bss
 dd end
 dd _start
The mboot header should be 4-byte aligned; your header is at the top of start.asm so I think this is not the case.
Another issue, it has to be in the first 8kb of the file. You may want to add a "GLOBAL mboot" statement and then run

Code: Select all

nm kernel.bin
to see if mboot is at the first 8kb (probably yes).

Re: compile simple os on linux

Posted: Mon Jul 07, 2008 12:42 pm
by majorek
Problem was in link.ld
previously, link.ld was for coff files :)

present link.ld :

Code: Select all

ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
thank's :)

EDIT:

But now kernel.bin is too big , previously was 8 kb, now is 23 kb ;/

Re: compile simple os on linux

Posted: Mon Jul 07, 2008 12:53 pm
by Adek336
ok great :D you had to remove a OUTPUTTARGET(i586-coff) statement, right?

Re: compile simple os on linux

Posted: Mon Jul 07, 2008 1:09 pm
by majorek
ok great :D you had to remove a OUTPUTTARGET(i586-coff) statement, right?
where is it ? :roll:

Re: compile simple os on linux

Posted: Mon Jul 07, 2008 1:20 pm
by Adek336
i guess it isn't :D you said you changed your linker script so that it would generate another output format so I thought that it was something like that. What was your change to the linker script?

Re: compile simple os on linux

Posted: Mon Jul 07, 2008 3:36 pm
by eax
if you are still having problems with file size being to big try adding this to your linker

OUTPUT_FORMAT("elf32-i386")

that elf32-i386 in the linker file isnt a reference to my cross compiler lines

Re: compile simple os on linux

Posted: Tue Jul 08, 2008 12:02 am
by majorek
file size is the same ;/

Re: compile simple os on linux

Posted: Wed Jul 09, 2008 9:43 am
by eax
You know what you could try?..........its just I think I may have had the same issues, build a cross compiler on linux for windows , thats what I did and I think that should give you the best of both worlds and could well sort your size problem as thinking back I think I had that as well. Im going to look out the link which if you follow the first part to the letter , you should have a cross compiler that you can use an identical makefile to my own. So get brans tutorial after the cross compiler is built download all brans source in a zip file , build the source, if that compiles fine great, next try your own code by using a similiar makefile to the one I showed on this post , if you dont get the size error its all good and it was something to do with the way the compiler was handling things, if your own code fails and brans does not then its something to do with your code. However I think you will find building a cross compiler will sort your issues , see link below:


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

When you get Step 1 steps all complete and read Step 2 - C Library STOP , you don't want to do step 2. I was actually surprised how easy it was to setup but thats in no small part to a really well written wiki article. I did have some errors with binutils but that was because I missed one of the steps , when I rectified this everything worked

Good luck :)