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


@echo compile code file $ <  ...
 $(CC) $(C_FLAGS) $< -o $@


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


@echo link kernel file  ...
 $(LD) $(LD_FLAGS) $(S_OBJECTS) $(C_OBJECTS) -o hx_kernel


$(RM) $(S_OBJECTS) $(C_OBJECTS) hx_kernel


sudo mount floppy.img /mnt/kernel
 sudo cp hx_kernel /mnt/kernel/hx_kernel
 sleep 1
 sudo umount /mnt/kernel


sudo mount floppy.img /mnt/kernel


sudo umount /mnt/kernel


qemu -fda floppy.img -boot a


bochs -f tools/bochsrc.txt


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


/* Segment Start Position */
 . = 0x100000;
 .text :
 . = ALIGN(4096);
 .data :
 . = ALIGN(4096);
 .bss :
 . = ALIGN(4096);
 .stab :
 . = ALIGN(4096);
 .stabstr :
 . = ALIGN(4096);
 /DISCARD/ : { *(.comment) *(.eh_frame) }



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