compile simple os on linux

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
majorek
Posts: 5
Joined: Mon Jul 07, 2008 10:10 am

compile simple os on linux

Post 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 ?
whowhatwhere
Member
Member
Posts: 199
Joined: Sat Jun 28, 2008 6:44 pm

Re: compile simple os on linux

Post 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.
Last edited by whowhatwhere on Mon Jul 07, 2008 10:43 am, edited 2 times in total.
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: compile simple os on linux

Post 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
majorek
Posts: 5
Joined: Mon Jul 07, 2008 10:10 am

Re: compile simple os on linux

Post 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 ?
eax
Member
Member
Posts: 57
Joined: Mon Jun 23, 2008 6:45 am

Re: compile simple os on linux

Post 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
Last edited by eax on Mon Jul 07, 2008 12:32 pm, edited 1 time in total.
User avatar
Adek336
Member
Member
Posts: 129
Joined: Thu May 12, 2005 11:00 pm
Location: Kabaty, Warszawa
Contact:

Re: compile simple os on linux

Post 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).
majorek
Posts: 5
Joined: Mon Jul 07, 2008 10:10 am

Re: compile simple os on linux

Post 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 ;/
User avatar
Adek336
Member
Member
Posts: 129
Joined: Thu May 12, 2005 11:00 pm
Location: Kabaty, Warszawa
Contact:

Re: compile simple os on linux

Post by Adek336 »

ok great :D you had to remove a OUTPUTTARGET(i586-coff) statement, right?
majorek
Posts: 5
Joined: Mon Jul 07, 2008 10:10 am

Re: compile simple os on linux

Post by majorek »

ok great :D you had to remove a OUTPUTTARGET(i586-coff) statement, right?
where is it ? :roll:
User avatar
Adek336
Member
Member
Posts: 129
Joined: Thu May 12, 2005 11:00 pm
Location: Kabaty, Warszawa
Contact:

Re: compile simple os on linux

Post 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?
eax
Member
Member
Posts: 57
Joined: Mon Jun 23, 2008 6:45 am

Re: compile simple os on linux

Post 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
majorek
Posts: 5
Joined: Mon Jul 07, 2008 10:10 am

Re: compile simple os on linux

Post by majorek »

file size is the same ;/
eax
Member
Member
Posts: 57
Joined: Mon Jun 23, 2008 6:45 am

Re: compile simple os on linux

Post 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 :)
Post Reply