-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile
More file actions
69 lines (51 loc) · 1.45 KB
/
Copy pathmakefile
File metadata and controls
69 lines (51 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
OUT := out
all: os-image
KERNEL_OFFSET := 0x1000
CC = gcc
CFLAGS = -fno-pie -m32 -ffreestanding -g -c
# Source files
SOURCEDIR = kernel/
C_SOURCES := $(shell find $(SOURCEDIR) -name '*.c' -type f -printf "%f\n")
ASM_SOURCES := $(shell find $(SOURCEDIR) -name '*.asm' -type f -printf "%f\n"| grep -v kernel_start)
C_OBJECTS = $(addprefix ${OUT}/,$(C_SOURCES:%.c=%.o))
ASM_OBJECTS = $(addprefix ${OUT}/,$(ASM_SOURCES:%.asm=%.o))
# $@ is the target file
# $^ substituted with all deps
# $< first arg
${OUT}:
mkdir $@
# Kernel binary
${OUT}/kernel.bin: ${OUT}/kernel.elf
objcopy -O binary $< $@
${OUT}/kernel.elf: ${OUT}/kernel_start.o ${C_OBJECTS} ${ASM_OBJECTS}
ld -m elf_i386 -o $@ -T link.ld $^
# Boot binary
${OUT}/boot.bin: boot/*.asm | ${OUT}
nasm boot/boot.asm -i boot/ -f bin -o $@
# Match alls
${OUT}/%.o: kernel/%.asm | ${OUT}
nasm $< -f elf -o $@
${OUT}/%.o: kernel/%.c | ${OUT}
$(CC) $(CFLAGS) $< -o $@
# OS Image
BINS := boot.bin kernel.bin
os-image: $(BINS:%=${OUT}/%)
cat $^ > $@
run: os-image
qemu-system-x86_64 os-image
debug: os-image ${OUT}/kernel.elf
qemu-system-i386 os-image -s -S &
gdb \
-ex 'set disassembly-flavor intel' \
-ex 'target remote localhost:1234' \
-ex 'symbol-file ${OUT}/kernel.elf' \
-ex 'break main' \
-ex 'layout src' \
-ex 'continue'
pkill qemu
clean:
rm -rf out/ *.bin *.o *.dis os-image
# Disassemble related stuffs
kernel.dis: out/kernel.bin
ndisasm -b 32 $< > kernel.dis
disassemble: kernel.dis