From d10621e832c50b29dc57e1dd09cc2afa8abeb001 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Tue, 17 Dec 2019 15:02:59 -0800 Subject: [PATCH] mesosphere: refactor Elf vs Elf64 distinction --- .../include/mesosphere/init/kern_init_elf.hpp | 20 +++++++++++++------ .../{arch/arm64 => }/init/kern_init_elf64.hpp | 10 +--------- .../kern_init_elf.cpp} | 18 ++++++++--------- .../kernel_ldr/source/kern_init_loader.cpp | 6 +++--- mesosphere/kernel_ldr/source/start.s | 8 ++++---- 5 files changed, 31 insertions(+), 31 deletions(-) rename libraries/libmesosphere/include/mesosphere/{arch/arm64 => }/init/kern_init_elf64.hpp (93%) rename libraries/libmesosphere/source/{arch/arm64/init/kern_init_elf64.cpp => init/kern_init_elf.cpp} (78%) diff --git a/libraries/libmesosphere/include/mesosphere/init/kern_init_elf.hpp b/libraries/libmesosphere/include/mesosphere/init/kern_init_elf.hpp index 2ee1e001a..1e226ccac 100644 --- a/libraries/libmesosphere/include/mesosphere/init/kern_init_elf.hpp +++ b/libraries/libmesosphere/include/mesosphere/init/kern_init_elf.hpp @@ -17,17 +17,25 @@ #include #ifdef ATMOSPHERE_ARCH_ARM64 - - #include "../arch/arm64/init/kern_init_elf64.hpp" - + #include "kern_init_elf64.hpp" #else - #error "Unknown Architecture" - #endif namespace ams::kern::init::Elf { - /* TODO: Anything we want inside this namespace? */ + #ifdef ATMOSPHERE_ARCH_ARM64 + using namespace ams::kern::init::Elf::Elf64; + + enum RelocationType { + R_ARCHITECTURE_RELATIVE = 0x403, /* Real name R_AARCH64_RELATIVE */ + }; + #else + #error "Unknown Architecture" + #endif + + /* API to apply relocations or call init array. */ + void ApplyRelocations(uintptr_t base_address, const Dyn *dynamic); + void CallInitArrayFuncs(uintptr_t init_array_start, uintptr_t init_array_end); } \ No newline at end of file diff --git a/libraries/libmesosphere/include/mesosphere/arch/arm64/init/kern_init_elf64.hpp b/libraries/libmesosphere/include/mesosphere/init/kern_init_elf64.hpp similarity index 93% rename from libraries/libmesosphere/include/mesosphere/arch/arm64/init/kern_init_elf64.hpp rename to libraries/libmesosphere/include/mesosphere/init/kern_init_elf64.hpp index 33ef192fe..84a0b2545 100644 --- a/libraries/libmesosphere/include/mesosphere/arch/arm64/init/kern_init_elf64.hpp +++ b/libraries/libmesosphere/include/mesosphere/init/kern_init_elf64.hpp @@ -125,12 +125,4 @@ namespace ams::kern::init::Elf::Elf64 { DT_RELCOUNT = 0x6ffffffa }; - enum RelocationType { - R_AARCH64_RELATIVE = 0x403, - }; - - /* API to apply relocations or call init array. */ - void ApplyRelocations(uintptr_t base_address, const Dyn *dynamic); - void CallInitArrayFuncs(uintptr_t init_array_start, uintptr_t init_array_end); - -} \ No newline at end of file +} diff --git a/libraries/libmesosphere/source/arch/arm64/init/kern_init_elf64.cpp b/libraries/libmesosphere/source/init/kern_init_elf.cpp similarity index 78% rename from libraries/libmesosphere/source/arch/arm64/init/kern_init_elf64.cpp rename to libraries/libmesosphere/source/init/kern_init_elf.cpp index 6f846763b..1eb6df6ae 100644 --- a/libraries/libmesosphere/source/arch/arm64/init/kern_init_elf64.cpp +++ b/libraries/libmesosphere/source/init/kern_init_elf.cpp @@ -15,7 +15,7 @@ */ #include -namespace ams::kern::init::Elf::Elf64 { +namespace ams::kern::init::Elf { /* API to apply relocations or call init array. */ void ApplyRelocations(uintptr_t base_address, const Dyn *dynamic) { @@ -52,25 +52,25 @@ namespace ams::kern::init::Elf::Elf64 { /* Apply all Rel relocations */ for (size_t i = 0; i < rel_count; i++) { - const auto &rel = *reinterpret_cast(dyn_rel + rel_ent * i); + const auto &rel = *reinterpret_cast(dyn_rel + rel_ent * i); - /* Only allow R_AARCH64_RELATIVE relocations. */ - while (rel.GetType() != R_AARCH64_RELATIVE) { /* ... */ } + /* Only allow architecture-specific relocations. */ + while (rel.GetType() != R_ARCHITECTURE_RELATIVE) { /* ... */ } /* Apply the relocation. */ - Elf64::Addr *target_address = reinterpret_cast(base_address + rel.GetOffset()); + Elf::Addr *target_address = reinterpret_cast(base_address + rel.GetOffset()); *target_address += base_address; } /* Apply all Rela relocations. */ for (size_t i = 0; i < rela_count; i++) { - const auto &rela = *reinterpret_cast(dyn_rela + rela_ent * i); + const auto &rela = *reinterpret_cast(dyn_rela + rela_ent * i); - /* Only allow R_AARCH64_RELATIVE relocations. */ - while (rela.GetType() != R_AARCH64_RELATIVE) { /* ... */ } + /* Only allow architecture-specific relocations. */ + while (rela.GetType() != R_ARCHITECTURE_RELATIVE) { /* ... */ } /* Apply the relocation. */ - Elf64::Addr *target_address = reinterpret_cast(base_address + rela.GetOffset()); + Elf::Addr *target_address = reinterpret_cast(base_address + rela.GetOffset()); *target_address = base_address + rela.GetAddend(); } } diff --git a/mesosphere/kernel_ldr/source/kern_init_loader.cpp b/mesosphere/kernel_ldr/source/kern_init_loader.cpp index f42e80f3b..7b7d5c562 100644 --- a/mesosphere/kernel_ldr/source/kern_init_loader.cpp +++ b/mesosphere/kernel_ldr/source/kern_init_loader.cpp @@ -320,14 +320,14 @@ namespace ams::kern::init::loader { std::memset(GetVoidPointer(virtual_base_address + bss_offset), 0, bss_end_offset - bss_offset); /* Apply relocations to the kernel. */ - const Elf::Elf64::Dyn *kernel_dynamic = reinterpret_cast(GetInteger(virtual_base_address) + dynamic_offset); - Elf::Elf64::ApplyRelocations(GetInteger(virtual_base_address), kernel_dynamic); + const Elf::Dyn *kernel_dynamic = reinterpret_cast(GetInteger(virtual_base_address) + dynamic_offset); + Elf::ApplyRelocations(GetInteger(virtual_base_address), kernel_dynamic); /* Reprotect .rodata as R-- */ ttbr1_table.Reprotect(virtual_base_address + ro_offset, ro_end_offset - ro_offset, KernelRwDataAttribute, KernelRoDataAttribute); /* Call the kernel's init array functions. */ - Elf::Elf64::CallInitArrayFuncs(GetInteger(virtual_base_address) + init_array_offset, GetInteger(virtual_base_address) + init_array_end_offset); + Elf::CallInitArrayFuncs(GetInteger(virtual_base_address) + init_array_offset, GetInteger(virtual_base_address) + init_array_end_offset); /* Return the difference between the random virtual base and the physical base. */ return GetInteger(virtual_base_address) - base_address; diff --git a/mesosphere/kernel_ldr/source/start.s b/mesosphere/kernel_ldr/source/start.s index f8aed3d78..3812c3ed3 100644 --- a/mesosphere/kernel_ldr/source/start.s +++ b/mesosphere/kernel_ldr/source/start.s @@ -48,17 +48,17 @@ _start: ldr x1, [x1, #0x18] /* .dynamic. */ add x1, x0, x1 - /* branch to ams::kern::init::Elf::Elf64::ApplyRelocations(uintptr_t, const ams::kern::init::Elf::Elf64::Dyn *); */ - bl _ZN3ams4kern4init3Elf5Elf6416ApplyRelocationsEmPKNS3_3DynE + /* branch to ams::kern::init::Elf::ApplyRelocations(uintptr_t, const ams::kern::init::Elf::Elf64::Dyn *); */ + bl _ZN3ams4kern4init3Elf16ApplyRelocationsEmPKNS2_5Elf643DynE - /* branch to ams::kern::init::Elf::Elf64::CallInitArrayFuncs(uintptr_t, uintptr_t) */ + /* branch to ams::kern::init::Elf::CallInitArrayFuncs(uintptr_t, uintptr_t) */ adr x2, _start adr x1, __external_references ldr x0, [x1, #0x20] /* init_array_start */ ldr x1, [x1, #0x28] /* init_array_end */ add x0, x0, x2 add x1, x1, x2 - bl _ZN3ams4kern4init3Elf5Elf6418CallInitArrayFuncsEmm + bl _ZN3ams4kern4init3Elf18CallInitArrayFuncsEmm /* Setup system registers, for detection of errors during init later. */ msr tpidr_el1, xzr /* Clear TPIDR_EL1 */