Page 1 of 1

Linking problem

Posted: Wed Oct 29, 2014 2:45 pm
by mulatrix
I'm following this tutorial to create my first OS but I have a problem when it comes to link the files.

https://code.google.com/p/onyxkernel/wiki/FirstStep

Code: Select all

gcc -ffreestanding -fno-builtin -nostdlib -c *.c
nasm -f aout loader.asm -o loader.o
ld -Ttext 0x1000 -o kernel.bin loader.o main.o video.o 
The tutorial says to use these commands to compile and link the files. The first two commands are no errors but the third is giving me this error:

Code: Select all

loader.o: file not recognized: File format not recognized 
What does this error mean?

Re: Linking problem

Posted: Wed Oct 29, 2014 2:51 pm
by seuti
Are you using a cross-compiler?

Re: Linking problem

Posted: Wed Oct 29, 2014 2:52 pm
by mulatrix
seuti wrote:Are you using a cross-compiler?
I'm using GCC and Nasm.

Re: Linking problem

Posted: Wed Oct 29, 2014 2:56 pm
by FallenAvatar
mulatrix wrote:
seuti wrote:Are you using a cross-compiler?
I'm using GCC and Nasm.
http://wiki.osdev.org/Why_do_I_need_a_Cross_Compiler%3F

- Monk

Re: Linking problem

Posted: Wed Oct 29, 2014 2:58 pm
by gmoney
Could you post the makefile probably something simple and its good practice. To use a gcc cross compiler vs unix gcc they have the instuctions for building a cross compiler in the wiki

Re: Linking problem

Posted: Wed Oct 29, 2014 3:03 pm
by iansjack
It looks like you are mixing elf object files with a.out ones - not a good idea. I would forget that tutorial; it looks pretty bad and even describes itself as "deprecated". Have a look at the tutorials in the Wiki here instead.

With any tutorial you should try to understand exactly what you are doing rather than just blindly following instructions.

Re: Linking problem

Posted: Wed Oct 29, 2014 4:02 pm
by sortie
Your tutorial is bad. Please follow the Bare Bones tutorial on our wiki. I have personally verified that it is good.

Re: Linking problem

Posted: Thu Oct 30, 2014 5:09 am
by mulatrix
sortie wrote:Your tutorial is bad. Please follow the Bare Bones tutorial on our wiki. I have personally verified that it is good.
But what is wrong: the code or commands?
I liked this tutorial because it is exactly what I want. Enable A20 Gate, entering the protected mode and call the kernel in C.

Re: Linking problem

Posted: Thu Oct 30, 2014 9:35 am
by Combuster
The main problem with most tutorials is that they use quick hacks, assumptions and workarounds that are specific to a certain setup - and in this business, preferably ancient ones you can't even get to work in modern times. This is the main reason why your tools refuse to work - they're not ancient enough to work in the way they used to work.

The basic setup requirements are listed in the Posting Checklist. After that, you'll have a compiler that can do ELF, and the assembler can follow with just one command line change. At that point, you'll probably notice that the tutorial still doesn't compile, and you realize you would have been better off using a known good tutorial from the start.

Re: Linking problem

Posted: Thu Oct 30, 2014 1:38 pm
by sortie
mulatrix wrote:But what is wrong: the code or commands? I liked this tutorial because it is exactly what I want. Enable A20 Gate, entering the protected mode and call the kernel in C.
Both the code and commands are wrong.

There are a number of problems with the tutorial that you followed:
  • The tutorial is marked as deprecated.
  • It is marked as under construction and notes issues with the code.
  • It is from 2008, last edited in 2010. This means it does not contain any recommendations from the last 4-6 years.
  • The author recommends people use other tutorials in the comments.
  • The comments are filled with people having lots of trouble.
  • The tutorial is not community edited,
  • It is hosted on google code, the fact people use that as opposed to superior sites like github, is a point against it in my book.
  • The tutorial was probably written by a newbie (at least at that time).
And all that was without even looking at the code. When you look at the tutorial itself, there are more issues:
  • The code doesn't use size_t and other unsigned variables where they should be used.
  • The putc function is obviously really broken. The pos variable isn't calculated correctly. It also isn't being incremented/used correctly. The x variable measures in bytes, not characters, and y is just entirely broken.
  • The tutorial makes a nonsense remark about operating systems not being able to escape a main function.
  • The kernel entry doesn't set a stack (and thus doesn't ensure it is properly alignment). (Well, the bootloader does do it)
  • It recommends using djgpp, an ancient compiler for DOS, or whatever. (I never looked into it.)
  • A cross-compiler isn't used. (This gets even more troublesome when it recommends using a non-cross gcc on windows.)
  • -nostdlib is passed when compiling the kernel, but it is a link time option to gcc.
  • The ancient a.out format is the output format of nasm for no good reason. Your ld doesn't even accept it.
  • 0x1000 is an awfully low place to load the kernel.
  • The output format of ld is ill-defined and depends on what ld you are using. It is not a flat binary, as the bootloader thinks it is.
  • A linker script is not used.
  • Code generated by gcc is used, but libgcc is not linked with.
  • (There are probably issues with the bootloader, but I never wrote one (I just use an existing one), so I can't tell)
  • The bootloader is too simple to be even remotely useful for anything but a bad hello world kernel.
In short, it's an awful tutorial and a worse base. You should be able to realize this yourself.

If you want to write an operating system, don't write your own bootloader. Use an existing one such as GRUB. Then you can easily write a C kernel, compile it with your cross-compiler, and get to actual work. We already documented this well at the Bare Bones tutorial. It's verified and gives you an actual environment that doesn't blow up when you do anything more sophisticated.

As for your original question:

Code: Select all

loader.o: file not recognized: File format not recognized 
This means that loader.o is in the a.out format and your ld doesn't support it.

You say it enables the A20 line. I don't see that.

It appears that you post from the same IP as dromenox. Are you that person?

Re: Linking problem

Posted: Thu Oct 30, 2014 3:29 pm
by mulatrix
sortie wrote:
mulatrix wrote:But what is wrong: the code or commands? I liked this tutorial because it is exactly what I want. Enable A20 Gate, entering the protected mode and call the kernel in C.
Both the code and commands are wrong.

There are a number of problems with the tutorial that you followed:
  • The tutorial is marked as deprecated.
  • It is marked as under construction and notes issues with the code.
  • It is from 2008, last edited in 2010. This means it does not contain any recommendations from the last 4-6 years.
  • The author recommends people use other tutorials in the comments.
  • The comments are filled with people having lots of trouble.
  • The tutorial is not community edited,
  • It is hosted on google code, the fact people use that as opposed to superior sites like github, is a point against it in my book.
  • The tutorial was probably written by a newbie (at least at that time).
And all that was without even looking at the code. When you look at the tutorial itself, there are more issues:
  • The code doesn't use size_t and other unsigned variables where they should be used.
  • The putc function is obviously really broken. The pos variable isn't calculated correctly. It also isn't being incremented/used correctly. The x variable measures in bytes, not characters, and y is just entirely broken.
  • The tutorial makes a nonsense remark about operating systems not being able to escape a main function.
  • The kernel entry doesn't set a stack (and thus doesn't ensure it is properly alignment). (Well, the bootloader does do it)
  • It recommends using djgpp, an ancient compiler for DOS, or whatever. (I never looked into it.)
  • A cross-compiler isn't used. (This gets even more troublesome when it recommends using a non-cross gcc on windows.)
  • -nostdlib is passed when compiling the kernel, but it is a link time option to gcc.
  • The ancient a.out format is the output format of nasm for no good reason. Your ld doesn't even accept it.
  • 0x1000 is an awfully low place to load the kernel.
  • The output format of ld is ill-defined and depends on what ld you are using. It is not a flat binary, as the bootloader thinks it is.
  • A linker script is not used.
  • Code generated by gcc is used, but libgcc is not linked with.
  • (There are probably issues with the bootloader, but I never wrote one (I just use an existing one), so I can't tell)
  • The bootloader is too simple to be even remotely useful for anything but a bad hello world kernel.
In short, it's an awful tutorial and a worse base. You should be able to realize this yourself.

If you want to write an operating system, don't write your own bootloader. Use an existing one such as GRUB. Then you can easily write a C kernel, compile it with your cross-compiler, and get to actual work. We already documented this well at the Bare Bones tutorial. It's verified and gives you an actual environment that doesn't blow up when you do anything more sophisticated.

As for your original question:

Code: Select all

loader.o: file not recognized: File format not recognized 
This means that loader.o is in the a.out format and your ld doesn't support it.

You say it enables the A20 line. I don't see that.

It appears that you post from the same IP as dromenox. Are you that person?


Yes, it's me. I created another account because I am a noob and I am ashamed of the questions I posted in the old account. But I'll stop asking questions and try to learn. I really wanted to create my bootloader to learn but I'll end up using GRUB.

But thank you for the answer, the tutorial seems very bad now.

Sorry for my bad english, i'm for other country.

Re: Linking problem

Posted: Thu Oct 30, 2014 3:41 pm
by no92
I think most people would forgive you stupid questions, once you've

a) matured and become a more advanced OSDever
or
b) thanked everybody and apologized what you and others think you should for (almost nobody does that)

Of course there's some kind of a limit for silly questions, but if you are not trolling on purpose or if you are a shitty person you won't find yourself exceeding it.

BTW, nobody will care about this topic in half a year, probably even less.

Re: Linking problem

Posted: Thu Oct 30, 2014 8:25 pm
by neon
Hello,

There is no need to be ashamed and I encourage dropping "noob" from your vocabulary. Similarly, there is no stupid question so long as its well written with the intent to gain more knowledge and better understanding. That is, after all, the point of questions. Everyone makes errors so I do not see there being any reason to be ashamed.

I also encourage to never stop asking questions. Question everything - that is the beginning of knowledge and wisdom.

With that said, writing a boot loader can be as simple or complicated as you want it to be. It all depends on your design goals and needs. If you are doing this for educational or research only, do whatever you want to satisfy those goals. Tutorials are a great way to learn and obtain new knowledge - but you need to remember that they are just that - tutorials. Copying and pasting or following them blindly will lead to failure if you do not understand the concepts employed by the articles. This is why my articles are content heavy - it is more important to get the concepts first before diving into code (note though that the provided article is not mine.)

Finally, as noted earlier, all tutorials make assumptions. These assumptions are around the build environments, tool chain, system requirement, host operating system, end design goals. And all tutorials make sacrifices -- simplicity rather than portability and well designed interfaces, pure monolithic rather then proper designed microkernel or monolithic with loadable modules etc, everything dealing with memory management, scheduling, concurrency, etc. This list goes on and on. These sacrifices are due to the infeasibility of implementation for a tutorial. If you limit yourself to a tutorial, you will always hit a dead end.

Follow your own design and architecture goals using tutorials when needed to better understand concepts. This is what you should be doing. It is your OS, after all.

Since you are using GCC and LD already, I would suggest starting with the GCC Cross Compiler if not already done so. Make sure that you understand your chosen tool chain very well. You can write the boot loader in C - however it can be tricky with BIOS firmware (due to the need to drop down to real or virtual 8086 mode.) It is far simpler with EFI since you do not need to worry about the real mode stages