Bare Metal Programming

Bare Metal Programming 開發指的是在沒有作業系統的環境下來開發程式,最直接的影響是在沒有作業系統的環境一些常見的 library 提供的功能將會無法直接使用。

main.s

main.s 之後會被 cross compiler 編譯成 main.o

1
2
3
4
.section ".text"
_start:
  wfe
  b _start

wfe: Wait for event, enter to low-power standby state

linker.ld

Linker script 將 main.o.text section 放置到指定的記憶體位置 (0x80000) 後

SECTIONS
{
  . = 0x80000;
  .text :
  {
      *(.text)
  }
}

Makefile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
TOOLCHAIN_PREFIX = aarch64-linux-gnu-
CC = $(TOOLCHAIN_PREFIX)gcc
LD = $(TOOLCHAIN_PREFIX)ld
OBJCPY = $(TOOLCHAIN_PREFIX)objcopy

CFLAGS = -Wall

.PHONY: all clean run

all: kernel8.img clean

main.o: main.s
	$(CC) $(CFLAGS) -c main.s -o main.o

kernel8.img: main.o
	$(LD) -T linker.ld -o kernel8.elf main.o
	$(OBJCPY) -O binary kernel8.elf kernel8.img

clean:
	rm -f *.o

run:
	qemu-system-aarch64 -M raspi3 -kernel kernel8.img -display none -d in_asm

Build Image

$ make

Run

$ make run

延伸閱讀

Linker Script初探 - GNU Linker Ld手冊略讀

from Source to Binary: How GNU Toolchain Works

comments powered by Disqus