bare-metal-aarch64. step-04 setting up a C environment make使用进阶 filter-out

发布时间 2023-09-01 01:46:39作者: 杨大茄子

https://github.com/rlepigre/bare-metal-aarch64/tree/master/step-04

void kernel_entry(){
  // Go in an infinite loop of "wfe" instructions.
  while(1){
    asm("wfe");
  }
}

  

# Flags passed to GCC.
GCC_FLAGS = -ffreestanding -Wall -Wextra -Werror -pedantic -O0

kernel.o: kernel.c
	${BINROOT}gcc ${GCC_FLAGS} -c $< -o $@

  

BINROOT=/Applications/ArmGNUToolchain/12.3.rel1/aarch64-none-elf/bin/aarch64-none-elf-
OBJS=$(patsubst %.c,%.o,$(wildcard *.c)) $(patsubst %.S,%.o,$(wildcard *.S) )

.PHONY:all
all: kernel8.img $($(OBJS):%.o=%.objdump )
        echo $(OBJS)
        echo $(OBJS:.o=.objdump )
%.objdump:%.o
        ${BINROOT}objdump -d $< > $@

%.o: %.S
        ${BINROOT}as -c $< -o $@

# Flags passed to GCC.
GCC_FLAGS = -ffreestanding -Wall -Wextra -Werror -pedantic -O0

%.o: %.c
        ${BINROOT}gcc ${GCC_FLAGS} -c $< -o $@

kernel8.elf: kernel8.ld $(patsubst %.c,%.o,$(wildcard *.c)) $(patsubst %.S,%.o,$(wildcard *.S) )
        echo $(wildcard *.c)
        echo $(patsubst %.c,%.o,$(wildcard *.c) )
        echo $(filter-out $<,$^)
        ${BINROOT}ld -T $< -o $@ $(filter-out $<,$^)

kernel8.img: kernel8.elf
        ${BINROOT}objcopy -O binary $< $@

.PHONY: run-gdb
run-gdb: kernel8.img
        @echo "[QEMU]    running with $<"
        @echo "(Press Ctrl-A X to exit QEMU.)"
        ${Q}qemu-system-aarch64 -s -S -M raspi3b -nographic -kernel $<

.PHONY: gdb
gdb: kernel8.elf
        @echo "[GDB]     running with $<"
        ${Q}${BINROOT}gdb -ex "target remote :1234" $<


.PHONY: clean
clean:
        -rm -f *.o *.elf *.img *.objdump

  

 $(var1:var2=var3);类似替换功能;(macmini 测试好像不支持;); obj=$(dir:%.c=%.o) 据说能用

能用,第二行那个OBJS裸着,我先前一直写成$(OBJS)所以失败;%去掉也能用,操

 https://www.gnu.org/software/make/manual/html_node/Text-Functions.html

 $(subst from,to,text)

Performs a textual replacement on the text text: each occurrence of from is replaced by to. The result is substituted for the function call. For example,

$(subst ee,EE,feet on the street)

produces the value ‘fEEt on the strEEt’.

$(patsubst pattern,replacement,text)

For example,

$(patsubst %.c,%.o,x.c.c bar.c)

produces the value ‘x.c.o bar.o’.

  

命令前加-是忽略错误,命令前加“@”是只显示该行命令执行的结果输出;

.phony 是阻止make去文件夹中查找跟目标同名的文件的

 

总算把这什么什么段搞清楚了。!!!!

.rodata (which stands for read-only data), 只读数据,存储 全局常量;

.data, 存储 全局已经初始化的变量

.bss (block starting symbol). 未初始化的全局变量

Every global variable that is initialised is either allocated in .rodata, if it is marked as const, or in .data otherwise.

Uninitialised global variables are allocated in the .bss section. 

初始化SP;

 

链接脚本:

ENTRY(_start)

SECTIONS {
  /* Our kernel image will be placed at address 0x80000. */
        . = 0x80000;
  /* It starts with the ".text" segment. */     
        __start=.;
        __text_start=.;
        .text : {
    /* The ".text" segment itself starts with the code from "boot.S". */
    /* The "_start" symbol (at the beginning of "boot.S") is at 0x80000. */
                *(.text.boot)
                *(.text)
        }
        .=ALIGN(4096);
        __text_end=.;
        
        __rodata_start=.;
        .rodata:{
                *(.rodata)
        }
        .=ALIGN(4096);
        __rodata_end=.;

        __data_start=.;
        .data:{
                *(.data)
        }
        . = ALIGN(4096);
        __data_end=.;

        __bss_start=.;
        .bss:{
                *(.bss)
        }
        . = ALIGN(4096);
        __bss_end=.;
        __end=.;
}

 

测试发现,链接脚本里 “=” 前面需要有空格!!!

 

  ARM64 汇编   无条件跳转

 

 

 

Clearing the BSS segment   BSS段清零