ubuntu - About the code of makefile compilation script and linker script ld when compiling in Linux
漂亮男人
漂亮男人 2017-06-05 11:10:05
0
1
761

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: ——————————————————————————————

!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

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

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

  • kernel.ld -- link script written for kernel format

    */

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) }

}

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

漂亮男人
漂亮男人

reply all(1)
漂亮男人

You don’t need to know assembly for this. You need to learn Makefile and ld script.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template