From e31c140ca8e6cc7025ef308375ea52d3d093b02b Mon Sep 17 00:00:00 2001 From: fincs Date: Wed, 20 Sep 2017 19:52:16 +0200 Subject: [PATCH 1/7] New linkscript including stuff from supplied AArch64 scripts --- buildscripts/lib/switch.ld | 163 +++++++++++++++++++++---------------- 1 file changed, 93 insertions(+), 70 deletions(-) diff --git a/buildscripts/lib/switch.ld b/buildscripts/lib/switch.ld index d96a01d8..e745ad92 100644 --- a/buildscripts/lib/switch.ld +++ b/buildscripts/lib/switch.ld @@ -11,132 +11,155 @@ PHDRS SECTIONS { /* =========== CODE section =========== */ - PROVIDE(__start__ = 0x0); . = __start__; - .text ALIGN(0x1000) : + .crt0 : + { + KEEP (*(.crt0)) + . = ALIGN(8); + } :code + + .init : { - /* .init */ - KEEP( *(.crt0) ) KEEP( *(.init) ) - . = ALIGN(4); + . = ALIGN(8); + } :code - /* .text */ - *(.text) - *(.text.*) - *(.glue_7) - *(.glue_7t) - *(.stub) - *(.gnu.warning) - *(.gnu.linkonce.t*) - . = ALIGN(4); + .plt : + { + *(.plt) + *(.iplt) + . = ALIGN(8); + } :code - /* .fini */ + .text : + { + *(.text.unlikely .text.*_unlikely .text.unlikely.*) + *(.text.exit .text.exit.*) + *(.text.startup .text.startup.*) + *(.text.hot .text.hot.*) + *(.text .stub .text.* .gnu.linkonce.t.*) + . = ALIGN(8); + } :code + + .fini : + { KEEP( *(.fini) ) - . = ALIGN(4); - } : code + . = ALIGN(8); + } :code /* =========== RODATA section =========== */ + . = ALIGN(0x1000); - .rodata ALIGN(0x1000) : + .rodata : { - *(.rodata) - *(.roda) - *(.rodata.*) - *all.rodata*(*) - *(.gnu.linkonce.r*) - SORT(CONSTRUCTORS) - . = ALIGN(4); - } : rodata + *(.rodata .rodata.* .gnu.linkonce.r.*) + . = ALIGN(8); + } :rodata - .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } : rodata - __exidx_start = .; - ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) } : rodata - __exidx_end = .; + .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } :rodata + .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } :rodata + .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } :rodata + .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } : rodata + + .interp : { *(.interp) } :rodata + .note.gnu.build-id : { *(.note.gnu.build-id) } :rodata + .hash : { *(.hash) } :rodata + .gnu.hash : { *(.gnu.hash) } :rodata + .gnu.version : { *(.gnu.version) } :rodata + .gnu.version_d : { *(.gnu.version_d) } :rodata + .gnu.version_r : { *(.gnu.version_r) } :rodata + .dynsym : { *(.dynsym) } :rodata + .dynstr : { *(.dynstr) } :rodata + .rela.dyn : { *(.rela.*) } :rodata /* =========== DATA section =========== */ + . = ALIGN(0x1000); - .data ALIGN(0x1000) : - { - *(.data) - *(.data.*) - *(.gnu.linkonce.d*) - CONSTRUCTORS - . = ALIGN(4); - __got_start__ = .; - *(.got) - __got_end__ = .; - } : data + .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } :data + .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } :data + .gnu_extab : ONLY_IF_RW { *(.gnu_extab*) } : data + .exception_ranges : ONLY_IF_RW { *(.exception_ranges .exception_ranges*) } :data - - .tdata ALIGN(4) : + .tdata ALIGN(8) : { __tdata_lma = .; - *(.tdata) - *(.tdata.*) - *(.gnu.linkonce.td.*) - . = ALIGN(4); + *(.tdata .tdata.* .gnu.linkonce.td.*) + . = ALIGN(8); __tdata_lma_end = .; - } : data + } :data - .tbss ALIGN(4) : + .tbss ALIGN(8) : { - *(.tbss) - *(.tbss.*) - *(.gnu.linkonce.tb.*) - *(.tcommon) - . = ALIGN(4); - } : data + *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) + . = ALIGN(8); + } :data - .preinit_array ALIGN(4) : + .preinit_array ALIGN(8) : { PROVIDE (__preinit_array_start = .); KEEP (*(.preinit_array)) PROVIDE (__preinit_array_end = .); - } : data + } :data - .init_array ALIGN(4) : + .init_array ALIGN(8) : { PROVIDE (__init_array_start = .); KEEP (*(SORT(.init_array.*))) KEEP (*(.init_array)) PROVIDE (__init_array_end = .); - } : data + } :data - .fini_array ALIGN(4) : + .fini_array ALIGN(8) : { PROVIDE (__fini_array_start = .); KEEP (*(.fini_array)) KEEP (*(SORT(.fini_array.*))) PROVIDE (__fini_array_end = .); - } : data + } :data - .ctors ALIGN(4) : + .ctors ALIGN(8) : { KEEP (*crtbegin.o(.ctors)) /* MUST be first -- GCC requires it */ KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors)) KEEP (*(SORT(.ctors.*))) KEEP (*(.ctors)) - } : data + } :data - .dtors ALIGN(4) : + .dtors ALIGN(8) : { KEEP (*crtbegin.o(.dtors)) KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors)) KEEP (*(SORT(.dtors.*))) KEEP (*(.dtors)) - } : data + } :data + + __got_start__ = .; + + .got : { *(.got) *(.igot) } :data + .got.plt : { *(.got.plt) *(.igot.plt) } :data + + __got_end__ = .; + + .data ALIGN(8) : + { + *(.data .data.* .gnu.linkonce.d.*) + SORT(CONSTRUCTORS) + } :data + + .dynamic ALIGN(8) : + { + *(.dynamic) + } :data __bss_start__ = .; - .bss ALIGN(4) : + .bss ALIGN(8) : { *(.dynbss) - *(.bss) - *(.bss.*) - *(.gnu.linkonce.b*) + *(.bss .bss.* .gnu.linkonce.b.*) *(COMMON) - . = ALIGN(4); + . = ALIGN(8); /* Reserve space for the TLS segment of the main thread */ __tls_start = .; From fe79be4a1d66a9fc035192484d395d83ef1c6fe0 Mon Sep 17 00:00:00 2001 From: fincs Date: Wed, 20 Sep 2017 19:52:28 +0200 Subject: [PATCH 2/7] Link with -pie (position independent executable) --- buildscripts/lib/switch.specs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildscripts/lib/switch.specs b/buildscripts/lib/switch.specs index 025f66fa..77d2670f 100644 --- a/buildscripts/lib/switch.specs +++ b/buildscripts/lib/switch.specs @@ -1,7 +1,7 @@ %rename link old_link *link: -%(old_link) -T switch.ld%s -d --gc-sections +%(old_link) -T switch.ld%s -pie --gc-sections *startfile: switch_crt0%O%s crti%O%s crtbegin%O%s From a78a6ecc27e2b370e9647fe787910b5c957104eb Mon Sep 17 00:00:00 2001 From: fincs Date: Wed, 20 Sep 2017 19:53:21 +0200 Subject: [PATCH 3/7] Move -fPIC to ARCH setting in Makefile --- nx/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nx/Makefile b/nx/Makefile index 93569f36..adb01059 100644 --- a/nx/Makefile +++ b/nx/Makefile @@ -32,9 +32,9 @@ INCLUDES := include #--------------------------------------------------------------------------------- # options for code generation #--------------------------------------------------------------------------------- -ARCH := -march=armv8-a +ARCH := -march=armv8-a -fPIC -CFLAGS := -g -Wall -Werror -fPIC \ +CFLAGS := -g -Wall -Werror \ -ffunction-sections \ -fdata-sections \ $(ARCH) \ From 1e0d868b0c5826faf5a37cf5eac7b750efc9c352 Mon Sep 17 00:00:00 2001 From: fincs Date: Wed, 20 Sep 2017 21:28:07 +0200 Subject: [PATCH 4/7] Linkscript: place .dynamic section into its own PT_DYNAMIC segment --- buildscripts/lib/switch.ld | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/buildscripts/lib/switch.ld b/buildscripts/lib/switch.ld index e745ad92..4ddba876 100644 --- a/buildscripts/lib/switch.ld +++ b/buildscripts/lib/switch.ld @@ -6,6 +6,7 @@ PHDRS code PT_LOAD FLAGS(5) /* Read | Execute */; rodata PT_LOAD FLAGS(4) /* Read */; data PT_LOAD FLAGS(6) /* Read | Write */; + dyn PT_DYNAMIC; } SECTIONS @@ -63,6 +64,7 @@ SECTIONS .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } :rodata .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } : rodata + .dynamic : { *(.dynamic) } :rodata :dyn .interp : { *(.interp) } :rodata .note.gnu.build-id : { *(.note.gnu.build-id) } :rodata .hash : { *(.hash) } :rodata @@ -148,11 +150,6 @@ SECTIONS SORT(CONSTRUCTORS) } :data - .dynamic ALIGN(8) : - { - *(.dynamic) - } :data - __bss_start__ = .; .bss ALIGN(8) : { From 7cc1442588fcbbad26889f6379fe672c66634491 Mon Sep 17 00:00:00 2001 From: fincs Date: Wed, 20 Sep 2017 21:29:50 +0200 Subject: [PATCH 5/7] elf2nso: Ignore non-PT_LOAD program headers --- tools/elf2nso.c | 65 ++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/tools/elf2nso.c b/tools/elf2nso.c index 52d305ec..7ec34996 100644 --- a/tools/elf2nso.c +++ b/tools/elf2nso.c @@ -62,8 +62,8 @@ uint8_t* ReadEntireFile(const char* fn, size_t* len_out) { int main(int argc, char* argv[]) { if (argc != 3) { - printf("%s \n", argv[0]); - return 1; + fprintf(stderr, "%s \n", argv[0]); + return EXIT_FAILURE; } NsoHeader nso_hdr; @@ -72,48 +72,57 @@ int main(int argc, char* argv[]) { nso_hdr.Unk3 = 0x3f; if (sizeof(NsoHeader) != 0x100) { - printf("Bad compile environment!\n"); - return 1; + fprintf(stderr, "Bad compile environment!\n"); + return EXIT_FAILURE; } size_t elf_len; uint8_t* elf = ReadEntireFile(argv[1], &elf_len); if (elf == NULL) { - printf("Failed to open input!\n"); - return 1; + fprintf(stderr, "Failed to open input!\n"); + return EXIT_FAILURE; } if (elf_len < sizeof(Elf64_Ehdr)) { - printf("Input file doesn't fit ELF header!\n"); - return 1; + fprintf(stderr, "Input file doesn't fit ELF header!\n"); + return EXIT_FAILURE; } Elf64_Ehdr* hdr = (Elf64_Ehdr*) elf; if (hdr->e_machine != EM_AARCH64) { - printf("Invalid ELF: expected AArch64!\n"); - return 1; + fprintf(stderr, "Invalid ELF: expected AArch64!\n"); + return EXIT_FAILURE; } - Elf64_Off ph_end = hdr->e_phoff + 3 * sizeof(Elf64_Phdr); + Elf64_Off ph_end = hdr->e_phoff + hdr->e_phnum * sizeof(Elf64_Phdr); if (ph_end < hdr->e_phoff || ph_end > elf_len) { - printf("Invalid ELF: phdrs outside file!\n"); - return 1; + fprintf(stderr, "Invalid ELF: phdrs outside file!\n"); + return EXIT_FAILURE; } - if (hdr->e_phnum != 3) { - printf("Invalid ELF: expected 3 phdrs!\n"); - return 1; - } - - Elf64_Phdr* phdr = (Elf64_Phdr*) &elf[hdr->e_phoff]; - size_t i; + Elf64_Phdr* phdrs = (Elf64_Phdr*) &elf[hdr->e_phoff]; + size_t i, j = 0; size_t file_off = sizeof(NsoHeader); uint8_t* comp_buf[3]; int comp_sz[3]; - for (i=0; i<3; i++, phdr++) { + for (i=0; i<3; i++) { + Elf64_Phdr* phdr = NULL; + while (j < hdr->e_phnum) { + Elf64_Phdr* cur = &phdrs[j++]; + if (cur->p_type == PT_LOAD) { + phdr = cur; + break; + } + } + + if (phdr == NULL) { + fprintf(stderr, "Invalid ELF: expected 3 loadable phdrs!\n"); + return EXIT_FAILURE; + } + nso_hdr.Segments[i].FileOff = file_off; nso_hdr.Segments[i].DstOff = phdr->p_vaddr; nso_hdr.Segments[i].DecompSz = phdr->p_filesz; @@ -133,16 +142,16 @@ int main(int argc, char* argv[]) { comp_buf[i] = malloc(comp_max); if (comp_buf[i] == NULL) { - printf("Compressing: Out of memory!\n"); - return 1; + fprintf(stderr, "Compressing: Out of memory!\n"); + return EXIT_FAILURE; } // TODO check p_offset comp_sz[i] = LZ4_compress_default(&elf[phdr->p_offset], comp_buf[i], phdr->p_filesz, comp_max); if (comp_sz[i] < 0) { - printf("Failed to compress!\n"); - return 1; + fprintf(stderr, "Failed to compress!\n"); + return EXIT_FAILURE; } nso_hdr.CompSz[i] = comp_sz[i]; @@ -152,8 +161,8 @@ int main(int argc, char* argv[]) { FILE* out = fopen(argv[2], "wb"); if (out == NULL) { - printf("Failed to open output file!\n"); - return 1; + fprintf(stderr, "Failed to open output file!\n"); + return EXIT_FAILURE; } // TODO check retvals @@ -162,5 +171,5 @@ int main(int argc, char* argv[]) { for (i=0; i<3; i++) fwrite(comp_buf[i], comp_sz[i], 1, out); - return 0; + return EXIT_SUCCESS; } From 6c14d225fddb527b55f9c33264ee1c3925f4f62c Mon Sep 17 00:00:00 2001 From: fincs Date: Thu, 21 Sep 2017 01:11:28 +0200 Subject: [PATCH 6/7] Disallow relocations in read-only segments --- buildscripts/lib/switch.specs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildscripts/lib/switch.specs b/buildscripts/lib/switch.specs index 77d2670f..323475f2 100644 --- a/buildscripts/lib/switch.specs +++ b/buildscripts/lib/switch.specs @@ -1,7 +1,7 @@ %rename link old_link *link: -%(old_link) -T switch.ld%s -pie --gc-sections +%(old_link) -T switch.ld%s -pie --gc-sections -z text *startfile: switch_crt0%O%s crti%O%s crtbegin%O%s From 1630fc16404c56ff41007652f83b8c7c2ab85734 Mon Sep 17 00:00:00 2001 From: fincs Date: Thu, 21 Sep 2017 01:12:11 +0200 Subject: [PATCH 7/7] Implement proper .dynamic section parsing --- crt0/switch_crt0.s | 52 ++++++++------------- nx/source/system/dynamic.c | 37 +++++++++++++++ nx/source/system/sysinit.s | 92 +++----------------------------------- 3 files changed, 61 insertions(+), 120 deletions(-) create mode 100644 nx/source/system/dynamic.c diff --git a/crt0/switch_crt0.s b/crt0/switch_crt0.s index 8a942ee0..ced5c18f 100644 --- a/crt0/switch_crt0.s +++ b/crt0/switch_crt0.s @@ -3,55 +3,39 @@ _start: bl startup - - .word 0x454d4f48 - .word 0x57455242 + .ascii "HOMEBREW" startup: // get aslr base sub x28, x30, #4 // clear .bss - ldr x0, =__bss_start__ - ldr x1, =__bss_end__ + adrp x0, __bss_start__ + adrp x1, __bss_end__ + add x0, x0, #:lo12:__bss_start__ + add x1, x1, #:lo12:__bss_end__ sub x1, x1, x0 // calculate size add x1, x1, #7 // round up to 8 bic x1, x1, #7 - mov x2, #0 - add x0, x0, x28 // relocate ptr bss_loop: - str x2, [x0], #8 + str xzr, [x0], #8 subs x1, x1, #8 bne bss_loop - // relocate .got - ldr x0, =__got_start__ - ldr x1, =__got_end__ - sub x1, x1, x0 // calculate size - add x1, x1, #7 // round up to 8 - bic x1, x1, #7 - add x0, x0, x28 // relocate ptr - -got_loop: - ldr x2, [x0] - add x2, x2, x28 - str x2, [x0], #8 - subs x1, x1, #8 - bne got_loop - + // process .dynamic section mov x0, x28 - ldr x3, =initSystem - add x3, x3, x28 - blr x3 + adrp x1, _DYNAMIC + add x1, x1, #:lo12:_DYNAMIC + bl __nx_dynamic + // initialize system + mov x0, x28 + bl __nx_init + + // call entrypoint mov x0, #0 // argc mov x1, #0 // argv - - ldr x3, =main - add x3, x3, x28 - ldr x2, =__nx_exit - add x2, x2, x28 - mov x30, x2 - br x3 - + adrp x30, __nx_exit + add x30, x30, #:lo12:__nx_exit + b main diff --git a/nx/source/system/dynamic.c b/nx/source/system/dynamic.c new file mode 100644 index 00000000..78f51df4 --- /dev/null +++ b/nx/source/system/dynamic.c @@ -0,0 +1,37 @@ +#include +#include + +void __nx_dynamic(uintptr_t base, const Elf64_Dyn* dyn) +{ + const Elf64_Rela* rela = NULL; + u64 relasz = 0; + + for (; dyn->d_tag != DT_NULL; dyn++) + { + switch (dyn->d_tag) + { + case DT_RELA: + rela = (const Elf64_Rela*)(base + dyn->d_un.d_ptr); + break; + case DT_RELASZ: + relasz = dyn->d_un.d_val / sizeof(Elf64_Rela); + break; + } + } + + if (rela == NULL) + for (;;); // Panic + + for (; relasz--; rela++) + { + switch (ELF64_R_TYPE(rela->r_info)) + { + case R_AARCH64_RELATIVE: + { + u64* ptr = (u64*)(base + rela->r_offset); + *ptr = base + rela->r_addend; + break; + } + } + } +} diff --git a/nx/source/system/sysinit.s b/nx/source/system/sysinit.s index 72a6f833..200c0fb1 100644 --- a/nx/source/system/sysinit.s +++ b/nx/source/system/sysinit.s @@ -1,99 +1,19 @@ .text - .global initSystem - .type initSystem, %function + .global __nx_init + .type __nx_init, %function -initSystem: +__nx_init: stp x29, x30, [sp, #-16]! - adr x1, __nx_binarybase - str x0, [x1] bl __libnx_init - bl __appInit - bl __nx_libc_init_array + bl __libc_init_array ldp x29, x30, [sp], #16 ret .global __nx_exit - .type __nx_exit, %function + .type __nx_exit, %function __nx_exit: - bl __nx_libc_fini_array + bl __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 -