kern/ldr: enable the use of relr for relocations

This commit is contained in:
Michael Scire 2024-09-01 22:27:48 -07:00
parent 423a05a1e9
commit 027e209073
6 changed files with 17 additions and 13 deletions

View File

@ -14,6 +14,7 @@ SECTIONS
/* =========== CODE section =========== */ /* =========== CODE section =========== */
PROVIDE(__start__ = 0x0); PROVIDE(__start__ = 0x0);
. = __start__; . = __start__;
__bin_start__ = .;
__code_start = . ; __code_start = . ;
.start : .start :
@ -159,6 +160,7 @@ SECTIONS
__bss_start__ = .; __bss_start__ = .;
.rela.dyn : { *(.rela.*) } :data .rela.dyn : { *(.rela.*) } :data
.relr.dyn : { *(.relr.*) } :data
.bss ADDR(.rela.dyn) (NOLOAD) : { .bss ADDR(.rela.dyn) (NOLOAD) : {
*(.dynbss) *(.dynbss)
@ -169,6 +171,7 @@ SECTIONS
__bss_end__ = .; __bss_end__ = .;
__bin_end__ = .;
__end__ = ABSOLUTE(.); __end__ = ABSOLUTE(.);
/* ================== /* ==================

View File

@ -1,7 +1,7 @@
%rename link old_link %rename link old_link
*link: *link:
%(old_link) -T %:getenv(ATMOSPHERE_TOPDIR /kernel.ld) -pie --gc-sections -z text -z nodynamic-undefined-weak -nostdlib %(old_link) -T %:getenv(ATMOSPHERE_TOPDIR /kernel.ld) -pie --gc-sections -z text -z nodynamic-undefined-weak -z pack-relative-relocs -nostdlib
*startfile: *startfile:
crti%O%s crtbegin%O%s crti%O%s crtbegin%O%s

View File

@ -15,8 +15,8 @@
*/ */
#include <mesosphere.hpp> #include <mesosphere.hpp>
extern "C" void _start(); extern "C" void __bin_start__();
extern "C" void __end__(); extern "C" void __bin_end__();
namespace ams::kern { namespace ams::kern {
@ -264,8 +264,8 @@ namespace ams::kern::init {
KMemoryLayout::GetPhysicalMemoryRegionTree().InsertDirectly(KernelPhysicalAddressSpaceBase, KernelPhysicalAddressSpaceBase + KernelPhysicalAddressSpaceSize - 1); KMemoryLayout::GetPhysicalMemoryRegionTree().InsertDirectly(KernelPhysicalAddressSpaceBase, KernelPhysicalAddressSpaceBase + KernelPhysicalAddressSpaceSize - 1);
/* Save start and end for ease of use. */ /* Save start and end for ease of use. */
const uintptr_t code_start_virt_addr = reinterpret_cast<uintptr_t>(_start); const uintptr_t code_start_virt_addr = reinterpret_cast<uintptr_t>(__bin_start__);
const uintptr_t code_end_virt_addr = reinterpret_cast<uintptr_t>(__end__); const uintptr_t code_end_virt_addr = reinterpret_cast<uintptr_t>(__bin_end__);
/* Setup the containing kernel region. */ /* Setup the containing kernel region. */
constexpr size_t KernelRegionSize = 1_GB; constexpr size_t KernelRegionSize = 1_GB;

View File

@ -12,6 +12,7 @@ SECTIONS
/* =========== CODE section =========== */ /* =========== CODE section =========== */
PROVIDE(__start__ = 0x0); PROVIDE(__start__ = 0x0);
. = __start__; . = __start__;
__bin_start__ = .;
__code_start = . ; __code_start = . ;
.crt0 : .crt0 :
@ -74,9 +75,8 @@ SECTIONS
.gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } : rodata .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } : rodata
.dynamic : { *(.dynamic) } :krnlldr :dyn .dynamic : { *(.dynamic) } :krnlldr :dyn
.dynsym : { *(.dynsym) } :krnlldr
.dynstr : { *(.dynstr) } :krnlldr
.rela.dyn : { *(.rela.*) } :krnlldr .rela.dyn : { *(.rela.*) } :krnlldr
.relr.dyn : { *(.relr.*) } :krnlldr
.hash : { *(.hash) } :krnlldr .hash : { *(.hash) } :krnlldr
.gnu.hash : { *(.gnu.hash) } :krnlldr .gnu.hash : { *(.gnu.hash) } :krnlldr
.gnu.version : { *(.gnu.version) } :krnlldr .gnu.version : { *(.gnu.version) } :krnlldr
@ -159,6 +159,7 @@ SECTIONS
} :krnlldr } :krnlldr
__bss_end__ = .; __bss_end__ = .;
__bin_end__ = .;
__end__ = ABSOLUTE(.) ; __end__ = ABSOLUTE(.) ;
/* ================== /* ==================
@ -166,7 +167,7 @@ SECTIONS
================== */ ================== */
/* Discard sections that difficult post-processing */ /* Discard sections that difficult post-processing */
/DISCARD/ : { *(.group .comment .note .interp) } /DISCARD/ : { *(.group .comment .note .interp .dynsym .dynstr) }
/* Stabs debugging sections. */ /* Stabs debugging sections. */
.stab 0 : { *(.stab) } .stab 0 : { *(.stab) }

View File

@ -1,7 +1,7 @@
%rename link old_link %rename link old_link
*link: *link:
%(old_link) -T %:getenv(ATMOSPHERE_TOPDIR /kernel_ldr.ld) -pie --gc-sections -z text -z nodynamic-undefined-weak -nostdlib %(old_link) -T %:getenv(ATMOSPHERE_TOPDIR /kernel_ldr.ld) -pie --gc-sections -z text -z nodynamic-undefined-weak -z pack-relative-relocs -nostdlib
*startfile: *startfile:
crti%O%s crtbegin%O%s crti%O%s crtbegin%O%s

View File

@ -19,8 +19,8 @@
/* Necessary for calculating kernelldr size/base for initial identity mapping */ /* Necessary for calculating kernelldr size/base for initial identity mapping */
extern "C" { extern "C" {
extern const u8 __start__[]; extern const u8 __bin_start__[];
extern const u8 __end__[]; extern const u8 __bin_end__[];
} }
@ -88,8 +88,8 @@ namespace ams::kern::init::loader {
/* Map in an RWX identity mapping for ourselves. */ /* Map in an RWX identity mapping for ourselves. */
constexpr PageTableEntry KernelLdrRWXIdentityAttribute(PageTableEntry::Permission_KernelRWX, PageTableEntry::PageAttribute_NormalMemory, PageTableEntry::Shareable_InnerShareable, PageTableEntry::MappingFlag_Mapped); constexpr PageTableEntry KernelLdrRWXIdentityAttribute(PageTableEntry::Permission_KernelRWX, PageTableEntry::PageAttribute_NormalMemory, PageTableEntry::Shareable_InnerShareable, PageTableEntry::MappingFlag_Mapped);
const uintptr_t kernel_ldr_base = util::AlignDown(reinterpret_cast<uintptr_t>(__start__), PageSize); const uintptr_t kernel_ldr_base = util::AlignDown(reinterpret_cast<uintptr_t>(__bin_start__), PageSize);
const uintptr_t kernel_ldr_size = util::AlignUp(reinterpret_cast<uintptr_t>(__end__), PageSize) - kernel_ldr_base; const uintptr_t kernel_ldr_size = util::AlignUp(reinterpret_cast<uintptr_t>(__bin_end__), PageSize) - kernel_ldr_base;
init_pt.Map(kernel_ldr_base, kernel_ldr_size, kernel_ldr_base, KernelRWXIdentityAttribute, allocator, 0); init_pt.Map(kernel_ldr_base, kernel_ldr_size, kernel_ldr_base, KernelRWXIdentityAttribute, allocator, 0);
/* Map in the page table region as RW- for ourselves. */ /* Map in the page table region as RW- for ourselves. */