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

View file

@ -1,7 +1,7 @@
%rename link old_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:
crti%O%s crtbegin%O%s

View file

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

View file

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

View file

@ -1,7 +1,7 @@
%rename link old_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:
crti%O%s crtbegin%O%s

View file

@ -19,8 +19,8 @@
/* Necessary for calculating kernelldr size/base for initial identity mapping */
extern "C" {
extern const u8 __start__[];
extern const u8 __end__[];
extern const u8 __bin_start__[];
extern const u8 __bin_end__[];
}
@ -88,8 +88,8 @@ namespace ams::kern::init::loader {
/* Map in an RWX identity mapping for ourselves. */
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_size = util::AlignUp(reinterpret_cast<uintptr_t>(__end__), PageSize) - kernel_ldr_base;
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>(__bin_end__), PageSize) - kernel_ldr_base;
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. */