From 2323505be7e3fbc99b4d0c2838a870b244da06ad Mon Sep 17 00:00:00 2001 From: Suguru Saito Date: Tue, 14 Dec 2021 01:01:31 +0900 Subject: [PATCH] Add linkerscript (experimental) --- x1/linkerscript/Makefile | 31 ++++++++++++++++++++++++ x1/linkerscript/crt0.s | 41 ++++++++++++++++++++++++++++++++ x1/linkerscript/main.c | 4 ++++ x1/linkerscript/x1.ld | 51 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 x1/linkerscript/Makefile create mode 100644 x1/linkerscript/crt0.s create mode 100644 x1/linkerscript/main.c create mode 100644 x1/linkerscript/x1.ld diff --git a/x1/linkerscript/Makefile b/x1/linkerscript/Makefile new file mode 100644 index 0000000..a107a2c --- /dev/null +++ b/x1/linkerscript/Makefile @@ -0,0 +1,31 @@ +CROSS_COMPILE := arm-linux-gnueabihf- +AS := $(CROSS_COMPILE)as +CC := $(CROSS_COMPILE)gcc +LD := $(CROSS_COMPILE)ld +OBJCOPY := $(CROSS_COMPILE)objcopy + +TARGET := AppMain +OBJS := crt0.o main.o + +ASFLAGS := -W +CFLAGS := -Wall -ffreestanding -fomit-frame-pointer -Os +LDFLAGS := -T x1.ld + +.PHONY: +all: $(TARGET).bin + +.PHONY: +clean: + -rm -f $(TARGET).bin $(TARGET).elf $(OBJS) + +%.o: %.s + $(AS) $(ASFLAGS) $< -o $@ + +%.o: %.c + $(CC) -c $(CFLAGS) $< + +$(TARGET).elf: $(OBJS) + $(LD) $(LDFLAGS) $^ -o $@ + +$(TARGET).bin: $(TARGET).elf + $(OBJCOPY) -O binary $< $@ diff --git a/x1/linkerscript/crt0.s b/x1/linkerscript/crt0.s new file mode 100644 index 0000000..c31569b --- /dev/null +++ b/x1/linkerscript/crt0.s @@ -0,0 +1,41 @@ +.text +.arm +.align 4 + +.global _start +_start: + stmdb sp!, {r4, lr} + /* store context */ + movs r4, r0 + + /* Clear the .bss section */ + bl .L_clean_bss + + /* restore context */ + adr r0, [.L_context] + str r4, [r0] + ldr r0, [r0] + + /* call user's main */ + bl main + + ldmia sp!, {r4, pc} + +.L_context: +.long 0 + +/* + r0 = start address + r1 = end address + tmp: r2 +*/ +.L_clean_bss: + ldr r0, =__bss_start + ldr r1, =__bss_end + sub r1, r1, #4 + mov r2, #0 +.L_clean_bss_loop: + cmp r0, r1 + strlt r2, [r0], #4 + blt .L_clean_bss_loop + bx lr diff --git a/x1/linkerscript/main.c b/x1/linkerscript/main.c new file mode 100644 index 0000000..9fbdf5d --- /dev/null +++ b/x1/linkerscript/main.c @@ -0,0 +1,4 @@ +int main(void *arg) +{ + return 0; +} diff --git a/x1/linkerscript/x1.ld b/x1/linkerscript/x1.ld new file mode 100644 index 0000000..b9a1a4f --- /dev/null +++ b/x1/linkerscript/x1.ld @@ -0,0 +1,51 @@ +OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm") +OUTPUT_ARCH(arm) +ENTRY(_start) + +MEMORY { + RAM (xrw): ORIGIN = 0x90000000, LENGTH = 15M +} + +SECTIONS { + .text : { + *(.text) + *(.text*) + *(.rodata) + *(.rodata*) + *(.glue_7) + *(.glue_7t) + + KEEP (*(.init)) + KEEP (*(.fini)) + + . = ALIGN(4); + } > RAM + + .data : { + *(.data) + *(.data*) + . = ALIGN(4); + } > RAM + + .bss : { + __bss_start = .; + *(.bss) + *(COMMON) + . = ALIGN(4); + __bss_end = .; + } > RAM + + /DISCARD/ : { + *(.discard) + *(.discard.*) + *(.comment) + } + + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .ARM.attributes 0 : { *(.ARM.attributes) } +}