Page 1 of 1
GNU Toolchain
Posted: Wed Nov 18, 2015 12:17 pm
by hwg
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?
Re: GNU Toolchain
Posted: Wed Nov 18, 2015 2:45 pm
by kenod
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/b ... t-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.
Re: GNU Toolchain
Posted: Wed Nov 18, 2015 5:00 pm
by intx13
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.
Re: GNU Toolchain
Posted: Thu Nov 19, 2015 8:50 am
by hwg
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?
Re: GNU Toolchain
Posted: Thu Nov 19, 2015 8:59 am
by hwg
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?
Re: GNU Toolchain
Posted: Thu Nov 19, 2015 10:25 am
by intx13
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.
Re: GNU Toolchain
Posted: Thu Nov 19, 2015 2:59 pm
by kenod
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
Re: GNU Toolchain
Posted: Mon Nov 23, 2015 8:22 am
by hwg
How much existing code would have to be changed?
Currently, the OS is writing to 0xB8000 to put text on the screen.
Re: GNU Toolchain
Posted: Mon Nov 23, 2015 3:17 pm
by kenod
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