Code for Compile-time makefile Compile Script and Linker Script ld in linux

  linux, question

Recently, I am reading a tutorial on implementing a simple x86 operating system, because I am not very familiar with linux system. when I see the configuration of the development environment, I need to write the script file, Makefile and linker ld used in the development. I really don’t understand it, and I don’t know how to deal with these codes. I hope my predecessors will tell me.Which assembly language knowledge and linux operation are needed to understand?

This is the makefile code:
——————————————————————————————

! 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 compile code file $ <  ...
 $(CC) $(C_FLAGS) $< -o $@

.s.o:

@echo compile assembly file $ <  ...
 $(ASM) $(ASM_FLAGS) $<

link:

@echo link kernel file  ...
 $(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

——————————————————————————————

This is the linker code:
——————————————————————————————
/*

  • Kernel.ld — link script written for kernel format
    */

ENTRY(start)
SECTIONS
{

/* Segment Start Position */
 
 . = 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) }

}

————————————————————————————

This doesn’t need to know compilation. You need to studyMakefileAndld script.