OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Mar 29, 2024 9:58 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: GNU Toolchain
PostPosted: Wed Nov 18, 2015 12:17 pm 
Offline

Joined: Wed Nov 18, 2015 11:56 am
Posts: 22
My current toolchain is the standard GCC. As I understand, compiling using the default compiler is *very* bad. I was following this tutorial as a base, and my current kernel boots just fine under QEMU using `qemu-system-i386 -kernel kernel.bin -serial file:/dev/stdout`. It works just as well in the form of a CD using grub2.

How can I put right what once went wrong?


Top
 Profile  
 
 Post subject: Re: GNU Toolchain
PostPosted: Wed Nov 18, 2015 2:45 pm 
Offline

Joined: Fri Sep 25, 2015 5:30 am
Posts: 22
A cross-compiler is used to make sure that you use the right target. This means that it makes sure you compile for i386, and that you don't use the libraries of your host OS. It is possible to build a kernel without a cross-compiler (see http://www.jamesmolloy.co.uk/tutorial_html/, http://www.osdever.net/tutorials/view/brans-kernel-development-tutorial and http://wiki.osdev.org/Why_do_I_need_a_Cross_Compiler), but it makes everything more dangerous, and normally requires extra commands, and you need to re-implement the standard include files, such as stdint.h. It is interesting to note that the tutorial you used also doesn't pass these options. I think that the main reason it works was because your code didn't do anything that required the standard c library or other libraries linux provides. You should not assume this is always the case, so it would be the best to switch to a cross-compiler. It would also be good to do the bare-bones tutorial on the wiki instead, because it does basically the same as this tutorial, but with a cross-compiler and the right options.


Top
 Profile  
 
 Post subject: Re: GNU Toolchain
PostPosted: Wed Nov 18, 2015 5:00 pm 
Offline
Member
Member

Joined: Wed Sep 07, 2011 3:34 pm
Posts: 112
The pre-built toolchain on your development system was really intended for building userland applications to be run within the operating system on your development system itself, as opposed to a bare-metal kernel running (potentially) on a different architecture. But if your development system is the same architecture as the system you want your OS to run on, and you write your build scripts to make sure you're not including or linking with anything from the operating system or intended for userland applications, it will work fine.

The reason many tutorials start with "build a cross-compiler" is because sooner or later you'll want to do some kind of compiling/linking that the pre-built toolchain doesn't support, so you might as well get it out of the way. Plus it makes sure you've got your head in the right place for "bare-metal" development and you're not still thinking in userland application terms.

That said, if you are targeting the same architecture as your development system, your cross-compiler toolchain will look very similar to the pre-built toolchain.


Top
 Profile  
 
 Post subject: Re: GNU Toolchain
PostPosted: Thu Nov 19, 2015 8:50 am 
Offline

Joined: Wed Nov 18, 2015 11:56 am
Posts: 22
So the tutorial I posted works because the operating system being compiled on the same arch for which it was written? So an ARM OS would not compile properly, correct?


Top
 Profile  
 
 Post subject: Re: GNU Toolchain
PostPosted: Thu Nov 19, 2015 8:59 am 
Offline

Joined: Wed Nov 18, 2015 11:56 am
Posts: 22
You said that it would involve reimplementing stdint.h, but I thought stdint.h was implementation dependent. Does the standard not specify the minimum size of bytes and not the max?


Top
 Profile  
 
 Post subject: Re: GNU Toolchain
PostPosted: Thu Nov 19, 2015 10:25 am 
Offline
Member
Member

Joined: Wed Sep 07, 2011 3:34 pm
Posts: 112
hwg wrote:
So the tutorial I posted works because the operating system being compiled on the same arch for which it was written? So an ARM OS would not compile properly, correct?

Correct.

hwg wrote:
You said that it would involve reimplementing stdint.h, but I thought stdint.h was implementation dependent. Does the standard not specify the minimum size of bytes and not the max?

stdint.h is specific to the toolchain and the architecture. If you're using the host's toolchain and you're targeting the host's architecture, it's safe to use the host's stdint.h.

Honestly I think the wiki article on cross-compilers is a little extreme (it goes on and on about shorter command-lines, for example). So long as you set your CFLAGS and LDFLAGS appropriately to ensure the compiler won't link against anything from the host system, you're fine. Some headers from the host system may still be useful, others (that refer to libraries pre-built for the host OS that you can't easily recompile for your OS) won't be.

In my opinion the downsides of using the host's toolchains are:
  • Developers new to "bare-metal" development will get confused about what they're really compiling and targeting.
  • If/when you eventually target a different architecture, or other developers with incompatible host toolchains get involved in your OS, you'll have refactor your build scripts.

So a cross-compiler is a good idea in general, but it's not "dangerous" to start with the host's toolchain.


Top
 Profile  
 
 Post subject: Re: GNU Toolchain
PostPosted: Thu Nov 19, 2015 2:59 pm 
Offline

Joined: Fri Sep 25, 2015 5:30 am
Posts: 22
Reimplementing stdint.h needs to be done when using james molloy's tutorial, because he turns of the standard headers the compiler gives you, something that isn't necessary. Because of this you need to redefine stuff like uint32_t


Top
 Profile  
 
 Post subject: Re: GNU Toolchain
PostPosted: Mon Nov 23, 2015 8:22 am 
Offline

Joined: Wed Nov 18, 2015 11:56 am
Posts: 22
How much existing code would have to be changed?
Currently, the OS is writing to 0xB8000 to put text on the screen.


Top
 Profile  
 
 Post subject: Re: GNU Toolchain
PostPosted: Mon Nov 23, 2015 3:17 pm 
Offline

Joined: Fri Sep 25, 2015 5:30 am
Posts: 22
From what I can see most of the code is or does the same as the bare bones tutorial, so it is mainly changing the compiler and the options


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 54 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group