Categories · Hello World Operating System · Operating Systems · Work

Steps to build your Hello World Operating System

 

Step one :- Retrieving the Source Code.

Create a directory say “src”  and change into it  using the following commands.

# Create a directory from which we will build the toolchain.
mkdir -p $HOME/src
cd $HOME/src

Then download the source code using the following commands into the src directory

# Download the needed packages we will need, as well as their dependencies.
wget http://ftp.gnu.org/gnu/binutils/binutils-2.23.1.tar.bz2
wget http://ftp.gnu.org/gnu/gcc/gcc-4.8.2/gcc-4.8.2.tar.bz2
wget http://ftp.gnu.org/gnu/gmp/gmp-5.0.5.tar.xz
wget http://mpfr.loria.fr/mpfr-3.0.1/mpfr-3.0.1.tar.xz
wget http://www.multiprecision.org/mpc/download/mpc-1.0.1.tar.gz

Step two :- Extracting from the Source Code.

Tar it or extract the files from the downloaded source code using the below commands.

# Extract the archives in the current directory.
tar xvf binutils-2.23.1.tar.bz2
tar xvf gcc-4.8.2.tar.bz2 
tar xvf gmp-5.0.5.tar.xz
tar xvf mpfr-3.0.1.tar.xz
tar xvf mpc-1.0.1.tar.gz

Step three :- Setting Up Symbolic Links in the GCC.

Your system may already have the required packages installed for gmp, mpfr, and mpc, but we’ll build our own version here to be safe. GCC already contains the build scripts to do this, so we simply need to tell GCC to use our versions, which is done by setting up symbolic links in the GCC directory as given below :-

cd gcc-4.8.2
ln -s ../gmp-5.0.5 gmp
ln -s ../mpc-1.0.1 mpc
ln -s ../mpfr-3.0.1 mpfr
cd ..

Step four :- Set the Path .

We’ll need to add the destination directory to our PATH environmental variable, as it allows the later steps to automatically find our cross toolchain.  We can add the following line to to the ./profile file in our $HOME.

export PATH="$HOME/opt/cross/bin:$PATH"

Step five :- Configure the Binutils .

Note that we build binutils in another directory than its source directory and the first step is to create a new directory and configure it as using the following commands.

# Delete the build directory if it already exists.
rm -rfv binutils-build
mkdir binutils-build
cd binutils-build

../binutils-2.23.1/configure --prefix=$HOME/opt/cross --target=i386-elf \
                             --disable-nls
  • The option –prefix tells the configure script where we want our toolchain to be installed.
  • The –target option tells the configure script which platform that our toolchain should target, that is, produce binaries for. Note that the resulting toolchain runs on your local system and is 32-bit if you have 32-bit, and 64-bit if you have 64-bit. However, it produces 32-bit binaries in any case. That is the point of cross-compilation: the system on which the compiler is run is different from the platform it produces binaries for.
  • The –disable-nls option simply tells that we don’t want native language support, which makes a smaller toolchain and all the error messages are in english.

 

Step six :- Build the Binutils.

Use the following command to build the binutils.

make

Step seven :- Install the Binutils.

Use the following command to install the binutils.

make install

Step eight :- Configure the Cross Compiler.

We create a new directory under src and configure the gcc using the following commands.

rm -rfv gcc-build
mkdir gcc-build
cd gcc-build
../gcc-4.8.2/configure --prefix=$HOME/opt/cross --target=i386-elf \
                       --enable-languages=c,c++ --disable-nls --without-headers
  • The –prefix option tells the configure script where to install the cross compiler,
  • the –target option tells it what platform it should produce binaries for,
  • the –disable-nls option disables native language support.
  • The –enable-languages tells what languages that the compiler will support. If you omit this, you will build all the support languages in GCC which will take quite a while and you don’t need them.You can remove the c++ from the command line if you wish, but C++ is a language suitable for operating systems development.
  • The –without-headers option tells the compiler that there is no C standard library for the target system. After all, we haven’t made one yet!

Step nine :- Build the Cross Compiler.

Let’s now build the cross compiler using the following commands. Note that we are using the lib gcc standard library.

make all-gcc # build gcc
make all-libgcc # install gcc support library

Step ten :- Install the Cross Compiler.

make all-libgcc # install gcc support library
make install-libgcc # install gcc support library 

Step eleven :-Making your own operating system.

We need to create a new directory say “myos” and then write the boot.s , kernel.c and  linker.ld . The link to these files can be found here .

Step twelve :-Compiling the boot.s ,kernel.c and linker.ld files.

  • To compile the boot.s file use the following commad.
      i386-elf-as boot.s -o boot.o
  • To compile the kernel.c  file use the following commad.
      i386-elf-gcc -c kernel.c -o kernel.o -std=gnu99 -ffreestanding -O2 -Wall \
             -Wextra -nostdlib -nostartfiles -nodefaultlibs
  • To compile your linker.ld file , use the following command.
    i386-elf-gcc -T linker.ld -o myos.bin -ffreestanding -O2 -Wall -Wextra \
                 -nostdlib -nostartfiles -nodefaultlibs boot.o kernel.o  -lgcc

We are using a few new options here:-

  1. -std=gnu99 uses the C99 standard plus the various GNU extensions supported by GCC.
  2. -ffreestanding tells the compiler that we are using the Freestanding version of C and that it should not assume that a standard library is available.
  3. -nostdlib removes the dependency on the C standard library (which isn’t even available) that is automatically added to the command line.
  4. -nostartfiles tells the compiler not to embed a tiny file containing the _start function, as the normal _start function is not usable for booting an operating system.
  5. -nodefaultlibs disables various libraries that are included by default.
  6. -lgcc links against the libgcc that we built . GCC will emit calls to this library if it needs reusable code, for instance to do division of 64-bit integers on 32-bit platforms.
  7. The -T option tells the compiler to use the linker script linker.ld.

 

Step thirteen :-Writing the Makefile

Refer in this article.

Step fourteen :- Configuring the Bootloader.

Refer  Step 3 part b) in this article.

Step fifteen :- Installing the grub-mkrescue and xorriso if not present.

You’ll need to install them from your repositories if you don’t have it yet.

Step sixteen :-Running Make ISO

You will now be able to run “make iso” in your myos directory and end up with a bootable operating system.

Congratulations! 🙂

Step seventeen :-Testing Your Operating Sytem

You can test your OS using any virtual machines. Here I have used a Qemu. You can run the following command after installing Qemu .

qemu-system-i386 -cdrom myos.iso

It will show a menu provided by the bootloader. Simply select “myos” and if all goes well, you should see the happy words “Amma says , Hello, Kernel World!” followed by some mysterious character.

You can refer here for the documentation part of it.

Hope this helped you.