Learn My Old Code

Read the code I read serveral months ago.

Learn My Old Code

Code are all in my old repo DoKernel

This post is a review for improving my next repo dotkernel. there are 2 reusable files.

config/config.mk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
DIR_PATH=/home/doot/sysmaking/dokernel

TOOLS_PREFIX = /usr/local/cross/bin/i686-elf-

CC = ${TOOLS_PREFIX}gcc
CXX = ${TOOLS_PREFIX}g++
LD = ${TOOLS_PREIFX}ld
AS = nasm
OBJCOPY = ${TOOLS_PREIFX}objcopy

BIN = img.bin


AS_FLAGS = -f elf32 -F dwarf -g
LINK_FLAGS = -ffreestanding -nostdlib -g
CC_FLAGS = -std=gnu11 -ffreestanding -m32 -masm=intel -Wall -Wextra -Wno-address-of-packed-member -g -O0

OBJCOPY_GENBIN_FLAGS = -O binary -S
OBJCOPY_GENSYM_FLAGS = --only-keep-debug


CLEAN_CMD = find . | grep -e "\.o" -e "\.elf" -e "\.bin" -e "\.debug" | xargs rm
  1. assembler flags : -f elf32 for output format, -F dwarf for debug infomation format, -g for enable debug infomation
  2. linker flags : -ffreestanding means that standard library may not exst or enable the freestanding environment, -nostdlib tells linker not to link startup files.
  3. compiler flags : -std=gun11, -ffreestanding, -m32 means generating code for 32bit abi, -masm=intel choices the intel syntax, -Wall, -Wextra, -Wno-address-of-packed-member disable some warning.
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
SECTIONS
{
/* jump to here to run */
. = 0x7C00;
.text :
{
boot.o(.text)
}

.data :
{
boot.o(.data)
boot.o(.rodata)
}

allsize = ABSOLUTE(.);
.mbrcheck :
{
. = 0x7C00 + 512 - allsize - 2;
boot.o(.mbrcheck)

}

. = 0x9000;
.text.1 : AT(0x7c00 + 0x200)
{
boot.o(.text.1)
. = ALIGN(0x200) - 1;
BYTE(0xcc)
}

}

It seems that there are no hightlighting scheme for link script, I just use C.

  1. . = 0x7c00, assign the VMA and MBR is here to run.
  2. allsize = ABSOLUTE(.), we can get the size of code and data above.
  3. AT(0x7c00 + 0x200), assign the LMA of the section