From a1815dd7b99355cf9da16f36d57a7ca861b26d56 Mon Sep 17 00:00:00 2001 From: yellows8 Date: Sat, 16 Sep 2017 12:55:18 -0400 Subject: [PATCH] Use custom funcs for handling init_array/fini_array. Updated crt0. --- crt0/switch_crt0.s | 3 +- nx/source/system/sysinit.s | 82 +++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/crt0/switch_crt0.s b/crt0/switch_crt0.s index 62425e8b..8a942ee0 100644 --- a/crt0/switch_crt0.s +++ b/crt0/switch_crt0.s @@ -1,5 +1,5 @@ .section ".crt0","ax" -.global _start, _sysexit, main +.global _start _start: bl startup @@ -40,6 +40,7 @@ got_loop: subs x1, x1, #8 bne got_loop + mov x0, x28 ldr x3, =initSystem add x3, x3, x28 blr x3 diff --git a/nx/source/system/sysinit.s b/nx/source/system/sysinit.s index d6d62ad2..72a6f833 100644 --- a/nx/source/system/sysinit.s +++ b/nx/source/system/sysinit.s @@ -1,12 +1,15 @@ + .text .global initSystem .type initSystem, %function initSystem: stp x29, x30, [sp, #-16]! + adr x1, __nx_binarybase + str x0, [x1] bl __libnx_init bl __appInit - //bl __libc_init_array //Currently broken since this doesn't use .got. + bl __nx_libc_init_array ldp x29, x30, [sp], #16 ret @@ -14,8 +17,83 @@ initSystem: .type __nx_exit, %function __nx_exit: - //bl __libc_fini_array//See above. + bl __nx_libc_fini_array bl __appExit b __libnx_exit +__nx_libc_init_array: + stp x29, x30, [sp, #-16]! + stp x21, x22, [sp, #-16]! + stp x19, x20, [sp, #-16]! + adr x3, __nx_binarybase + ldr x20, [x3] + + ldr x0, =__preinit_array_start + ldr x1, =__preinit_array_end + sub x1, x1, x0 + add x21, x0, x20 + lsr x19, x1, #3 + cbz x19, __nx_libc_init_array_end0 + + __nx_libc_init_array_lp0: + ldr x3, [x21], #8 + sub x19, x19, #1 + add x3, x3, x20 + blr x3 + cbnz x19, __nx_libc_init_array_lp0 + +__nx_libc_init_array_end0: + bl _init + + ldr x0, =__init_array_start + ldr x1, =__init_array_end + sub x1, x1, x0 + add x21, x0, x20 + lsr x19, x1, #3 + cbz x19, __nx_libc_init_array_end1 + + __nx_libc_init_array_lp1: + ldr x3, [x21], #8 + sub x19, x19, #1 + add x3, x3, x20 + blr x3 + cbnz x19, __nx_libc_init_array_lp1 + +__nx_libc_init_array_end1: + ldp x19, x20, [sp], #16 + ldp x21, x22, [sp], #16 + ldp x29, x30, [sp], #16 + ret + +__nx_libc_fini_array: + stp x29, x30, [sp, #-16]! + stp x21, x22, [sp, #-16]! + stp x19, x20, [sp, #-16]! + adr x3, __nx_binarybase + ldr x20, [x3] + + ldr x0, =__fini_array_start + ldr x1, =__fini_array_end + sub x1, x1, x0 + add x21, x0, x20 + lsr x19, x1, #3 + cbz x19, __nx_libc_fini_array_end + + __nx_libc_fini_array_lp: + sub x19, x19, #1 + ldr x3, [x21, x19, lsl #3] + add x3, x3, x20 + blr x3 + cbnz x19, __nx_libc_fini_array_lp + +__nx_libc_fini_array_end: + ldp x19, x20, [sp], #16 + ldp x21, x22, [sp], #16 + ldp x29, x30, [sp], #16 + ret + + .data +__nx_binarybase: + .dword 0 +