Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
minrt 0.6.0: overhaul init/fini/exit behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarLambda committed Feb 5, 2023
1 parent e2ac164 commit 565650e
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ make_dist() {
[ ! -d "$DIST" ] && mkdir "$DIST" || rm "$DIST"/*

(PROJECT=libseven VERSION=0.17.0 make_dist)
(PROJECT=minrt VERSION=0.5.1 make_dist)
(PROJECT=minrt VERSION=0.6.0 make_dist)
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project('sdk-seven', 'c',
version: '0.17.1',
version: '0.18.0',
license: 'Zlib',
meson_version: '>=0.55.0',
default_options: ['warning_level=2', 'c_std=c99'])
Expand Down
14 changes: 10 additions & 4 deletions minrt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ set(CMAKE_INCLUDE_FLAG_ASM "-Wa,-I")

add_library(minrt STATIC
src/gba/crt0.s
src/gba/exit.c
src/gba/fini.c
src/gba/init.c
src/gba/ram.c
src/gba/rom_header.s
)
Expand All @@ -24,14 +27,17 @@ target_compile_options(minrt PRIVATE

target_link_options(minrt INTERFACE
-mthumb
-specs=lib/nocrt0.specs
-L${CMAKE_CURRENT_LIST_DIR}/lib
-specs=${CMAKE_CURRENT_LIST_DIR}lib/nocrt0.specs
-L${CMAKE_CURRENT_LIST_DIR}/lib/ldscripts
-Trom.mem
-Tgba.x
)

add_library(minrt_multiboot STATIC
src/gba/crt0.s
src/gba/exit.c
src/gba/fini.c
src/gba/init.c
src/gba/ram.c
src/gba/multiboot_header.s
)
Expand All @@ -45,8 +51,8 @@ target_compile_options(minrt_multiboot PRIVATE

target_link_options(minrt_multiboot INTERFACE
-mthumb
-specs=lib/nocrt0.specs
-L${CMAKE_CURRENT_LIST_DIR}/lib
-specs=${CMAKE_CURRENT_LIST_DIR}lib/nocrt0.specs
-L${CMAKE_CURRENT_LIST_DIR}/lib/ldscripts
-Tmultiboot.mem
-Tgba.x
)
Expand Down
10 changes: 0 additions & 10 deletions minrt/lib/ldscripts/gba.x
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,6 @@ SECTIONS

/* Static initialization and finalization */

.init :
{
KEEP(*(SORT_NONE(.init)))
} >LOAD_REGION

.fini :
{
KEEP(*(SORT_NONE(.fini)))
} >LOAD_REGION

.preinit_array :
{
PROVIDE_HIDDEN(__preinit_array_start = .);
Expand Down
16 changes: 6 additions & 10 deletions minrt/meson.build
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
version = '0.5.1'
version = '0.6.0'

sources = [
'src/gba/rom_header.s',
'src/gba/crt0.s',
'src/gba/ram.c',
]

sources_mb = [
'src/gba/multiboot_header.s',
'src/gba/crt0.s',
'src/gba/exit.c',
'src/gba/fini.c',
'src/gba/init.c',
'src/gba/ram.c',
]

Expand All @@ -20,13 +16,13 @@ link_args = ['-specs=' + lib / 'nocrt0.specs', '-L' + lib / 'ldscripts']

minrt = static_library(
'minrt_rom',
sources,
sources + ['src/gba/rom_header.s'],
include_directories: includes + ['src/gba'],
install: true)

minrt_mb = static_library(
'minrt_mb',
sources_mb,
sources + ['src/gba/multiboot_header.s'],
include_directories: includes + ['src/gba'],
install: true)

Expand Down
7 changes: 3 additions & 4 deletions minrt/src/gba/crt0.s
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ _start:
ldr r2, =__bss_len

@ Initialization
bl __libc_init_array
bl __minrt_init_array

@ main(0, NULL,NULL)
movs r0, #0 @ argc
Expand All @@ -126,9 +126,8 @@ pool: .pool

.section .text._exit,"ax",%progbits
_exit:
@ Disable IRQs and halt
ldr r3, =REG_IME
strh r3, [r3]
ldr r1, =REG_IME
str r1, [r1]
b .

.section .noinit,"aw",%nobits
Expand Down
17 changes: 17 additions & 0 deletions minrt/src/gba/exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdlib.h>
#include <unistd.h>

extern void exit(int code)
{
void __call_exitprocs(int, void*) __attribute__((weak));
void __minrt_fini_array(void);

if (__call_exitprocs)
{
__call_exitprocs(code, NULL);
}

__minrt_fini_array();

_exit(code);
}
17 changes: 17 additions & 0 deletions minrt/src/gba/fini.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stddef.h>

extern void (*__fini_array_start []) (void) __attribute__((weak));
extern void (*__fini_array_end []) (void) __attribute__((weak));

extern void __minrt_fini_array(void)
{
size_t count;
size_t i;

count = __fini_array_end - __fini_array_start;

for (i = count; i > 0; i--)
{
__fini_array_start[i-1]();
}
}
26 changes: 26 additions & 0 deletions minrt/src/gba/init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stddef.h>

extern void (*__preinit_array_start[]) (void) __attribute__((weak));
extern void (*__preinit_array_end[]) (void) __attribute__((weak));
extern void (*__init_array_start[]) (void) __attribute__((weak));
extern void (*__init_array_end[]) (void) __attribute__((weak));

extern void __minrt_init_array(void)
{
size_t count;
size_t i;

count = __preinit_array_end - __preinit_array_start;

for (i = 0; i < count; i++)
{
__preinit_array_start[i]();
}

count = __init_array_end - __init_array_start;

for (i = 0; i < count; i++)
{
__init_array_start[i]();
}
}

0 comments on commit 565650e

Please sign in to comment.