Writing a Simple Operating System — from Scratch

(Jeff_L) #1

CHAPTER 5. WRITING, BUILDING, AND LOADING YOUR


KERNEL 56


It is often useful to specify targets that are not actually real targets, in that they do
not generate files. A common use of such phoney targets is to make acleantarget, so
that when we runmake clean, all of the generated files are deleted from the directory,
leaving only the source files, as in Figure XXX.


clean:
rm *.bin *.o

Cleaning your directory in this way is useful if you’d like to distribute only the
source files to a friend, put the directory under version control, or if you’d like to test
that modifications of your makefile will correctly build all targets from scratch.
Ifmakeis run without a target, the first target in the main file is taken to be the
default, so you often see a phoney target such asallat the top of Makefile as in Figure
XXX.


# Default make target.
all: kernel.bin

# $^ is substituted with all of the target ’s dependancy files
kernel.bin: kernel_entry.o kernel.o
ld -o kernel.bin -Ttext 0x1000 $^ --oformat binary

# $< is the first dependancy and $@ is the target file
kernel.o : kernel.c
gcc -ffreestanding -c $< -o $@

# Same as the above rule.
kernel_entry.o : kernel_entry.asm
nasm $< -f elf -o $@

Note that, by givingkernel.binas a dependency to thealltarget, we ensure that
kernel.binand all of its dependencies are built for the default target.
We can now put all of the commands for building our kernel and the loadable kernel
image into a useful makefile (see Figure XXX), that will allow us to test changes or
corrections to our code in Bochs simply by typingmake run.


all: os-image

# Run bochs to simulate booting of our code.
run: all
bochs

# This is the actual disk image that the computer loads ,
# which is the combination of our compiled bootsector and kernel
os-image: boot_sect.bin kernel.bin
cat $^ > os -image
Free download pdf