最近在看一篇实现简单x86操作系统的教程,因为对linux系统不是很熟悉,在看到配置开发环境时,需要编写开发中用到的脚本文件,Makefile和链接器ld,实在看不懂,也不知道该如何处理这些代码,望前辈们告知 需要学习哪些汇编语言知识和linux操作才能看懂。
这是makefile代码:
——————————————————————————————
!Makefile
C_SOURCES = $(shell find . -name "*.c")
C_OBJECTS = $(patsubst %.c, %.o, $(C_SOURCES))
S_SOURCES = $(shell find . -name "*.s")
S_OBJECTS = $(patsubst %.s, %.o, $(S_SOURCES))
CC = gcc
LD = ld
ASM = nasm
C_FLAGS = -c -Wall -m32 -ggdb -gstabs+ -nostdinc -fno-builtin -fno-stack-protector -I include
LD_FLAGS = -T scripts/kernel.ld -m elf_i386 -nostdlib
ASM_FLAGS = -f elf -g -F stabs
all: $(S_OBJECTS) $(C_OBJECTS) link update_image
.c.o:
@echo 编译代码文件 $< ...
$(CC) $(C_FLAGS) $< -o $@
.s.o:
@echo 编译汇编文件 $< ...
$(ASM) $(ASM_FLAGS) $<
link:
@echo 链接内核文件...
$(LD) $(LD_FLAGS) $(S_OBJECTS) $(C_OBJECTS) -o hx_kernel
.PHONY:clean
clean:
$(RM) $(S_OBJECTS) $(C_OBJECTS) hx_kernel
.PHONY:update_image
update_image:
sudo mount floppy.img /mnt/kernel
sudo cp hx_kernel /mnt/kernel/hx_kernel
sleep 1
sudo umount /mnt/kernel
.PHONY:mount_image
mount_image:
sudo mount floppy.img /mnt/kernel
.PHONY:umount_image
umount_image:
sudo umount /mnt/kernel
.PHONY:qemu
qemu:
qemu -fda floppy.img -boot a
.PHONY:bochs
bochs:
bochs -f tools/bochsrc.txt
.PHONY:debug
debug:
qemu -S -s -fda floppy.img -boot a &
sleep 1
cgdb -x tools/gdbinit
——————————————————————————————
这是链接器代码:
——————————————————————————————
/*
kernel.ld -- 针对 kernel 格式所写的链接脚本
*/
ENTRY(start)
SECTIONS
{
/* 段起始位置 */
. = 0x100000;
.text :
{
*(.text)
. = ALIGN(4096);
}
.data :
{
*(.data)
*(.rodata)
. = ALIGN(4096);
}
.bss :
{
*(.bss)
. = ALIGN(4096);
}
.stab :
{
*(.stab)
. = ALIGN(4096);
}
.stabstr :
{
*(.stabstr)
. = ALIGN(4096);
}
/DISCARD/ : { *(.comment) *(.eh_frame) }
}
————————————————————————————