From e449090df06d987ca7115029f97cdaedc4f3e816 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Tue, 27 Jun 2023 18:13:47 +0000 Subject: [PATCH] m68k: improve build process This includes several improvements to the link flags, link script, common makefile, etc.. Previously bss initialization in m68k start was incorrect. m68k can now call static initializers if necessary. --- common.mk | 6 +-- m68k/common.mk | 10 ++--- m68k/handlers.c | 17 +++++--- m68k/m68k.lds | 31 +++++++++++--- m68k/start.cpp | 23 ++++++++-- m68k/vectors.s | 112 ++++++++++++++++++++++++------------------------ sh2.lds | 11 ++++- 7 files changed, 128 insertions(+), 82 deletions(-) diff --git a/common.mk b/common.mk index a882670..f9e5c5b 100644 --- a/common.mk +++ b/common.mk @@ -2,13 +2,13 @@ LIB ?= . OPT ?= -Og AARCH = --isa=sh2 --big -AFLAGS = -g -gdwarf-4 +AFLAGS = -g -gdwarf-4 --fatal-warnings CARCH = -m2 -mb CFLAGS += -falign-functions=4 -ffunction-sections -fdata-sections -fshort-enums -ffreestanding -nostdlib CFLAGS += -Wall -Werror -Wfatal-errors -Wno-error=unused-variable -g -gdwarf-4 $(OPT) -LDFLAGS = --gc-sections --no-warn-rwx-segment --print-memory-usage --entry=_start -CXXFLAGS = -std=c++17 -fno-exceptions -fno-non-call-exceptions -fno-rtti -fno-threadsafe-statics +LDFLAGS = --gc-sections --print-gc-sections --no-warn-rwx-segment --print-memory-usage --entry=_start --orphan-handling=error +CXXFLAGS = -std=c++20 -fno-exceptions -fno-non-call-exceptions -fno-rtti -fno-threadsafe-statics TARGET = sh2-none-elf- CC = $(TARGET)gcc diff --git a/m68k/common.mk b/m68k/common.mk index 02c93d7..9f83b7e 100644 --- a/m68k/common.mk +++ b/m68k/common.mk @@ -4,13 +4,13 @@ OPT ?= -Og ARCH = -march=68000 -mcpu=68000 AARCH = $(ARCH) -AFLAGS = -g -gdwarf-4 +AFLAGS = -g -gdwarf-4 --fatal-warnings CARCH = $(ARCH) -mtune=68000 CFLAGS += -falign-functions=4 -ffunction-sections -fdata-sections -fshort-enums -ffreestanding -nostdlib CFLAGS += -Wall -Werror -Wfatal-errors -Wno-error=unused-variable -g -gdwarf-4 $(OPT) -LDFLAGS = --gc-sections --no-warn-rwx-segment --print-memory-usage --entry=start -CXXFLAGS = -fno-exceptions -fno-rtti +LDFLAGS = --gc-sections --print-gc-sections --no-warn-rwx-segment --print-memory-usage --entry=start --orphan-handling=error +CXXFLAGS = -std=c++20 -fno-exceptions -fno-non-call-exceptions -fno-rtti -fno-threadsafe-statics TARGET = m68k-none-elf- CC = $(TARGET)gcc @@ -41,8 +41,8 @@ all: main.bin %.o: %.cpp $(CXX) $(CFLAGS) $(CXXFLAGS) $(CARCH) -c $< -o $@ -%.elf: - $(LD) $(LDFLAGS) -T $(LIB)/m68k/m68k.lds $(LIB)/m68k/vectors.o $(LIB)/m68k/start.o $(LIB)/m68k/handlers.o $^ -o $@ +%.elf: $(LIB)/m68k/vectors.o $(LIB)/m68k/start.o $(LIB)/m68k/handlers.o + $(LD) $(LDFLAGS) -T $(LIB)/m68k/m68k.lds $^ -o $@ %.bin: %.elf $(OBJCOPY) -O binary $< $@ diff --git a/m68k/handlers.c b/m68k/handlers.c index 831e190..3cf5528 100644 --- a/m68k/handlers.c +++ b/m68k/handlers.c @@ -1,14 +1,17 @@ void auto_vector_1(void) __attribute__ ((weak, interrupt_handler)); -void auto_vector_1(void) { return; } +void auto_vector_1(void) { while (1); } void auto_vector_2(void) __attribute__ ((weak, interrupt_handler)); -void auto_vector_2(void) { return; } +void auto_vector_2(void) { while (1); } void auto_vector_3(void) __attribute__ ((weak, interrupt_handler)); -void auto_vector_3(void) { return; } +void auto_vector_3(void) { while (1); } void auto_vector_4(void) __attribute__ ((weak, interrupt_handler)); -void auto_vector_4(void) { return; } +void auto_vector_4(void) { while (1); } void auto_vector_5(void) __attribute__ ((weak, interrupt_handler)); -void auto_vector_5(void) { return; } +void auto_vector_5(void) { while (1); } void auto_vector_6(void) __attribute__ ((weak, interrupt_handler)); -void auto_vector_6(void) { return; } +void auto_vector_6(void) { while (1); } void auto_vector_7(void) __attribute__ ((weak, interrupt_handler)); -void auto_vector_7(void) { return; } +void auto_vector_7(void) { while (1); } + +void exception(void) __attribute__ ((weak, interrupt_handler, noreturn)); +void exception(void) { while (1); } diff --git a/m68k/m68k.lds b/m68k/m68k.lds index ba4f290..7a022a5 100644 --- a/m68k/m68k.lds +++ b/m68k/m68k.lds @@ -6,33 +6,52 @@ MEMORY } SECTIONS { - .text : + . = 0x00000000; + + .text ALIGN(4) : SUBALIGN(4) { - . = 0x000000; - KEEP(*(.vectors)) - KEEP(*(.text.start)) + KEEP(*(.vectors*)) + KEEP(*(.text.start)) *(.text) - *(.text.*) + *(.text.*) } > sound_ram .data ALIGN(4) : SUBALIGN(4) { *(.data) - *(.data.*) + *(.data.*) } > sound_ram .rodata ALIGN(4) : SUBALIGN(4) { *(.rodata) + *(.rodata.*) + } > sound_ram + + .ctors ALIGN(4) : SUBALIGN(4) + { + KEEP(*(.ctors)) + KEEP(*(.ctors.*)) } > sound_ram .bss ALIGN(4) (NOLOAD) : SUBALIGN(4) { *(.bss) + *(.bss.*) } > sound_ram + /DISCARD/ : + { + *(.debug*) + *(.comment*) + *(.rela*) + } + __bss_link_start = ADDR(.bss); __bss_link_end = ADDR(.bss) + SIZEOF(.bss); + + __ctors_link_start = ADDR(.ctors); + __ctors_link_end = ADDR(.ctors) + SIZEOF(.ctors); } scsp = 0x000000; diff --git a/m68k/start.cpp b/m68k/start.cpp index d36bd51..7f2a869 100644 --- a/m68k/start.cpp +++ b/m68k/start.cpp @@ -1,18 +1,33 @@ #include -extern uint32_t * __bss_link_start; -extern uint32_t * __bss_link_end; +extern "C" uint32_t __bss_link_start __asm("__bss_link_start"); +extern "C" uint32_t __bss_link_end __asm("__bss_link_end"); + +typedef void(*init_t)(void); + +extern "C" uint32_t __ctors_link_start __asm("__ctors_link_start"); +extern "C" uint32_t __ctors_link_end __asm("__ctors_link_end"); + extern void main(); extern "C" void start(void) { - uint32_t * start = __bss_link_start; - uint32_t * end = __bss_link_end; + uint32_t * start; + uint32_t * end; + + start = &__bss_link_start; + end = &__bss_link_end; while (start < end) { *start++ = 0; } + start = &__ctors_link_start; + end = &__ctors_link_end; + while (start < end) { + (reinterpret_cast(*start++))(); + } + main(); while (1); diff --git a/m68k/vectors.s b/m68k/vectors.s index a225749..7193337 100644 --- a/m68k/vectors.s +++ b/m68k/vectors.s @@ -2,29 +2,29 @@ .long 0x7fffc /* Reset - initial stack pointer */ .long start /* Reset - initial program counter */ - .long start /* Bus error */ - .long start /* Address error */ - .long start /* Illegal command */ - .long start /* Divide by zero */ - .long start /* CHK exception */ - .long start /* TRAPV exception */ - .long start /* Privilege violation */ - .long start /* Trace */ - .long start /* Line 1010 emulator */ - .long start /* Line 1111 emulator */ - .long start /* reserved 12 */ - .long start /* reserved 13 */ - .long start /* reserved 14 */ - .long start /* Uninitialized interrupt vector */ - .long start /* reserved 16 */ - .long start /* reserved 17 */ - .long start /* reserved 18 */ - .long start /* reserved 19 */ - .long start /* reserved 20 */ - .long start /* reserved 21 */ - .long start /* reserved 22 */ - .long start /* reserved 23 */ - .long start /* Spurious interrupt */ + .long exception /* Bus error */ + .long exception /* Address error */ + .long exception /* Illegal command */ + .long exception /* Divide by zero */ + .long exception /* CHK exception */ + .long exception /* TRAPV exception */ + .long exception /* Privilege violation */ + .long exception /* Trace */ + .long exception /* Line 1010 emulator */ + .long exception /* Line 1111 emulator */ + .long exception /* reserved 12 */ + .long exception /* reserved 13 */ + .long exception /* reserved 14 */ + .long exception /* Uninitialized interrupt vector */ + .long exception /* reserved 16 */ + .long exception /* reserved 17 */ + .long exception /* reserved 18 */ + .long exception /* reserved 19 */ + .long exception /* reserved 20 */ + .long exception /* reserved 21 */ + .long exception /* reserved 22 */ + .long exception /* reserved 23 */ + .long exception /* Spurious interrupt */ .long auto_vector_1 /* Auto vector level 1 interrupt */ .long auto_vector_2 /* Auto vector level 2 interrupt */ .long auto_vector_3 /* Auto vector level 3 interrupt */ @@ -32,37 +32,37 @@ .long auto_vector_5 /* Auto vector level 5 interrupt */ .long auto_vector_6 /* Auto vector level 6 interrupt */ .long auto_vector_7 /* Auto vector level 7 interrupt */ - .long start /* Trap #0 vector */ - .long start /* Trap #1 vector */ - .long start /* Trap #2 vector */ - .long start /* Trap #3 vector */ - .long start /* Trap #4 vector */ - .long start /* Trap #5 vector */ - .long start /* Trap #6 vector */ - .long start /* Trap #7 vector */ - .long start /* Trap #8 vector */ - .long start /* Trap #9 vector */ - .long start /* Trap #10 vector */ - .long start /* Trap #11 vector */ - .long start /* Trap #12 vector */ - .long start /* Trap #13 vector */ - .long start /* Trap #14 vector */ - .long start /* Trap #15 vector */ - .long start /* reserved 48 */ - .long start /* reserved 49 */ - .long start /* reserved 50 */ - .long start /* reserved 51 */ - .long start /* reserved 52 */ - .long start /* reserved 53 */ - .long start /* reserved 54 */ - .long start /* reserved 55 */ - .long start /* reserved 56 */ - .long start /* reserved 57 */ - .long start /* reserved 58 */ - .long start /* reserved 59 */ - .long start /* reserved 60 */ - .long start /* reserved 61 */ - .long start /* reserved 62 */ - .long start /* reserved 63 */ + .long exception /* Trap #0 vector */ + .long exception /* Trap #1 vector */ + .long exception /* Trap #2 vector */ + .long exception /* Trap #3 vector */ + .long exception /* Trap #4 vector */ + .long exception /* Trap #5 vector */ + .long exception /* Trap #6 vector */ + .long exception /* Trap #7 vector */ + .long exception /* Trap #8 vector */ + .long exception /* Trap #9 vector */ + .long exception /* Trap #10 vector */ + .long exception /* Trap #11 vector */ + .long exception /* Trap #12 vector */ + .long exception /* Trap #13 vector */ + .long exception /* Trap #14 vector */ + .long exception /* Trap #15 vector */ + .long exception /* reserved 48 */ + .long exception /* reserved 49 */ + .long exception /* reserved 50 */ + .long exception /* reserved 51 */ + .long exception /* reserved 52 */ + .long exception /* reserved 53 */ + .long exception /* reserved 54 */ + .long exception /* reserved 55 */ + .long exception /* reserved 56 */ + .long exception /* reserved 57 */ + .long exception /* reserved 58 */ + .long exception /* reserved 59 */ + .long exception /* reserved 60 */ + .long exception /* reserved 61 */ + .long exception /* reserved 62 */ + .long exception /* reserved 63 */ - .align 0x400, 0xee + .fill 0x300, 1, 0xee diff --git a/sh2.lds b/sh2.lds index 54386eb..46f9a8c 100644 --- a/sh2.lds +++ b/sh2.lds @@ -2,7 +2,7 @@ OUTPUT_FORMAT("elf32-sh", "elf32-sh", "elf32-sh") OUTPUT_ARCH(sh) MEMORY { - ipl_rom : ORIGIN = 0x00000000, LENGTH = 512K + ipl_rom : ORIGIN = 0x00000000, LENGTH = 512K work_ram_l : ORIGIN = 0x00200000, LENGTH = 1M work_ram_h : ORIGIN = 0x06000000, LENGTH = 1M } @@ -26,6 +26,7 @@ SECTIONS .rodata ALIGN(4) : SUBALIGN(4) { *(.rodata) + *(.rodata.*) } > work_ram_h .ctors ALIGN(4) : SUBALIGN(4) @@ -37,6 +38,7 @@ SECTIONS .bss ALIGN(4) (NOLOAD) : SUBALIGN(4) { *(.bss) + *(.bss.*) } > work_ram_h .bss.work_ram_l ALIGN(4) (NOLOAD) : SUBALIGN(4) @@ -44,6 +46,13 @@ SECTIONS *(.bss.work_ram_l) } > work_ram_l + /DISCARD/ : + { + *(.debug*) + *(.comment*) + *(.rela*) + } + __bss_link_start = ADDR(.bss); __bss_link_end = ADDR(.bss) + SIZEOF(.bss);