Recently I was reading a tutorial on implementing a simple x86 operating system. Because I am not very familiar with the Linux system, when I saw that when configuring the development environment, I needed to write the script files, Makefile and linker ld used in development. It was really interesting. I don’t understand, and I don’t know how to deal with these codes. I hope the seniors can tell me what assembly language knowledge and Linux operations I need to learn to understand.
This is the makefile code: ——————————————————————————————
C_OBJECTS = $(patsubst %.c, %.o, $(C_SOURCES))
S_SOURCES = $(shell find . - name "*.s")
S_OBJECTS = $(patsubst %.s, %.o, $(S_SOURCES))
LD = ld
ASM = nasm
LD_FLAGS = -T scripts/kernel.ld -m elf_i386 -nostdlib
ASM_FLAGS = -f elf -g -F stabs
.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:cleanclean:
$(RM) $(S_OBJECTS) $(C_OBJECTS) hx_kernel
.PHONY:update_imageupdate_image:
sudo mount floppy.img /mnt/kernel
sudo cp hx_kernel /mnt/kernel/hx_kernel
sleep 1
sudo umount /mnt/kernel
.PHONY:mount_imagemount_image:
sudo mount floppy.img /mnt/kernel
.PHONY:umount_imageumount_image:
sudo umount /mnt/kernel
.PHONY:qemuqemu:
qemu -fda floppy.img -boot a
.PHONY:bochsbochs:
bochs -f tools/bochsrc.txt
.PHONY:debugdebug:
qemu -S -s -fda floppy.img -boot a &
sleep 1
cgdb -x tools/gdbinit
————————————————————————————
This is the linker code: ——————————————————————————————
/*
*/
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) }
}——————————————————————————
You don’t need to know assembly for this. You need to learn Makefile and ld script.