From ba9cf30e670669c42c36ced062a429dcde9ae5e3 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 3 Nov 2022 15:41:14 -0400 Subject: [PATCH] Clean up linker script symbols --- src/hello/hello.d | 9 +-- src/hulk/header.d | 8 +-- src/hulk/hippo.d | 2 +- src/hulk/hulk.d | 4 +- src/hulk/hulk.ld | 18 +++++- src/hulk/hurl.d | 6 +- src/hulk/linker_addresses.d | 110 ++++++++++++++++++++++++++++++++++-- 7 files changed, 133 insertions(+), 24 deletions(-) diff --git a/src/hello/hello.d b/src/hello/hello.d index 1bd2bc8..f134d04 100644 --- a/src/hello/hello.d +++ b/src/hello/hello.d @@ -13,7 +13,9 @@ import hulk.memory; import ldc.llvmasm; __gshared EFI_SYSTEM_TABLE * st; +/** HULK binary start address, 4KB-aligned. */ extern extern(C) __gshared ubyte _hulk_bin_start; +/** HULK binary end address, 4KB-aligned. */ extern extern(C) __gshared ubyte _hulk_bin_end; private align(4096) __gshared ubyte[1024 * 1024] scratch_buffer; /** Index to the memory map region being used to allocate page tables. */ @@ -39,14 +41,9 @@ private ulong hulk_bin_size() return cast(ulong)&_hulk_bin_end - cast(ulong)&_hulk_bin_start; } -private ulong hulk_total_size() -{ - return cast(ulong)hulk_header().total_size; -} - private ulong hulk_bss_size() { - return hulk_total_size() - hulk_bin_size(); + return cast(ulong)hulk_header().hulk_bss_size; } private ulong hulk_stack_size() diff --git a/src/hulk/header.d b/src/hulk/header.d index 1d1e2f1..6293f2e 100644 --- a/src/hulk/header.d +++ b/src/hulk/header.d @@ -11,14 +11,14 @@ import hulk.bootinfo; struct HulkHeader { /** - * Total size of memory to be mapped. + * HULK BSS size. + * + * The OS loader must map memory to a zeroed range of this size. * - * The OS loader must ensure that virtual memory is mapped for this entire - * size. * This value is really an integer, but is stored as a pointer because it * is provided by the linker and LDC does not like us to cast it. */ - void * total_size; + void * hulk_bss_size; /** Entry point. */ void * entry; diff --git a/src/hulk/hippo.d b/src/hulk/hippo.d index d730e40..98f4cec 100644 --- a/src/hulk/hippo.d +++ b/src/hulk/hippo.d @@ -54,7 +54,7 @@ struct hippo { size_t usable_memory; ulong[2][4] reserved = [ - [header.bootinfo.hulk_phys, cast(ulong)header.total_size - LinkerAddresses.hulk_bss_size], + [header.bootinfo.hulk_phys, LinkerAddresses.hulk_binary_size], [header.bootinfo.bss_phys, LinkerAddresses.hulk_bss_size], [header.bootinfo.stack_phys, header.stack_size], [cast(ulong)header.bootinfo.fb.buffer, header.bootinfo.fb.height * header.bootinfo.fb.stride * 4u], diff --git a/src/hulk/hulk.d b/src/hulk/hulk.d index 0762198..f23f0bc 100644 --- a/src/hulk/hulk.d +++ b/src/hulk/hulk.d @@ -21,11 +21,11 @@ import hulk.pic; import hulk.acpi; import hulk.apic; -extern extern(C) __gshared ubyte _hulk_total_size; +extern extern(C) __gshared ubyte _hulk_bss_size; @(ldc.attributes.section(".hulk_header")) private __gshared HulkHeader hulk_header = { - &_hulk_total_size, /* total_size */ + &_hulk_bss_size, /* hulk_bss_size */ &hulk_start, /* entry */ 16u * 1024u, /* stack_size */ HULK_VIRTUAL_BASE_ADDRESS, /* virt_base */ diff --git a/src/hulk/hulk.ld b/src/hulk/hulk.ld index c994277..4f13a8d 100644 --- a/src/hulk/hulk.ld +++ b/src/hulk/hulk.ld @@ -2,6 +2,7 @@ SECTIONS { . = 0xFFFF800000000000; _hulk_mem_start = .; + _hulk_header_start = .; .hulk_header : { @@ -9,6 +10,7 @@ SECTIONS } . = ALIGN(4K); + _hulk_header_end = .; _hulk_text_start = .; .text : @@ -18,6 +20,7 @@ SECTIONS . = ALIGN(4K); _hulk_text_end = .; + _hulk_rodata_start = .; .rodata : { @@ -26,12 +29,17 @@ SECTIONS . = ALIGN(4K); + _hulk_rodata_end = .; + _hulk_data_start = .; + .data : { *(.data) } . = ALIGN(4K); + _hulk_data_end = .; + _hulk_binary_size = . - _hulk_mem_start; _hulk_bss_start = .; .bss : @@ -41,7 +49,13 @@ SECTIONS } . = ALIGN(4K); - _hulk_bss_size = . - _hulk_bss_start; + _hulk_bss_end = .; _hulk_mem_end = .; - _hulk_total_size = _hulk_mem_end - _hulk_mem_start; + + _hulk_header_size = _hulk_header_end - _hulk_header_start; + _hulk_text_size = _hulk_text_end - _hulk_text_start; + _hulk_rodata_size = _hulk_rodata_end - _hulk_rodata_start; + _hulk_data_size = _hulk_data_end - _hulk_data_start; + _hulk_bss_size = _hulk_bss_end - _hulk_bss_start; + _hulk_mem_size = _hulk_mem_end - _hulk_mem_start; } diff --git a/src/hulk/hurl.d b/src/hulk/hurl.d index 7509280..9f86a4d 100644 --- a/src/hulk/hurl.d +++ b/src/hulk/hurl.d @@ -36,7 +36,7 @@ struct hurl /* Turn on NXE (no execute enable) flag in the EFER MSR. */ wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_NXE); m_pt_base = allocate_pt(); - size_t hulk_bin_phys_size = cast(ulong)header.total_size - LinkerAddresses.hulk_bss_size; + size_t hulk_binary_size = LinkerAddresses.hulk_binary_size; /* Identity map all physical RAM. */ map_range(0u, 0u, @@ -45,10 +45,10 @@ struct hurl /* Map HULK binary region. */ map_range(HULK_VIRTUAL_BASE_ADDRESS, header.bootinfo.hulk_phys, - hulk_bin_phys_size, + hulk_binary_size, PT_WRITABLE); /* Map HULK BSS region. */ - map_range(HULK_VIRTUAL_BASE_ADDRESS + hulk_bin_phys_size, + map_range(HULK_VIRTUAL_BASE_ADDRESS + hulk_binary_size, header.bootinfo.bss_phys, LinkerAddresses.hulk_bss_size, PT_WRITABLE | PT_NO_EXECUTE); diff --git a/src/hulk/linker_addresses.d b/src/hulk/linker_addresses.d index 715b6fb..7996acf 100644 --- a/src/hulk/linker_addresses.d +++ b/src/hulk/linker_addresses.d @@ -3,39 +3,137 @@ */ module hulk.linker_addresses; -private extern extern(C) __gshared ubyte _hulk_mem_start; +private extern extern(C) __gshared ubyte _hulk_header_start; +private extern extern(C) __gshared ubyte _hulk_header_end; +private extern extern(C) __gshared ubyte _hulk_header_size; + +private extern extern(C) __gshared ubyte _hulk_text_start; +private extern extern(C) __gshared ubyte _hulk_text_end; +private extern extern(C) __gshared ubyte _hulk_text_size; + +private extern extern(C) __gshared ubyte _hulk_rodata_start; +private extern extern(C) __gshared ubyte _hulk_rodata_end; +private extern extern(C) __gshared ubyte _hulk_rodata_size; + +private extern extern(C) __gshared ubyte _hulk_data_start; +private extern extern(C) __gshared ubyte _hulk_data_end; +private extern extern(C) __gshared ubyte _hulk_data_size; + private extern extern(C) __gshared ubyte _hulk_bss_start; +private extern extern(C) __gshared ubyte _hulk_bss_end; private extern extern(C) __gshared ubyte _hulk_bss_size; + +/* hulk_binary includes header, text, rodata, and data */ +private extern extern(C) __gshared ubyte _hulk_binary_size; + +/* hulk_mem includes header, text, rodata, data, and bss */ +private extern extern(C) __gshared ubyte _hulk_mem_start; private extern extern(C) __gshared ubyte _hulk_mem_end; -private extern extern(C) __gshared ubyte _hulk_total_size; +private extern extern(C) __gshared ubyte _hulk_mem_size; /** * This struct provides access to linker-defined symbols. */ public struct LinkerAddresses { - public static @property ulong hulk_mem_start() + public static @property ulong hulk_header_start() { - return cast(ulong)&_hulk_mem_start; + return cast(ulong)&_hulk_header_start; } + public static @property ulong hulk_header_end() + { + return cast(ulong)&_hulk_header_end; + } + + public static @property ulong hulk_header_size() + { + return cast(ulong)&_hulk_header_size; + } + + + public static @property ulong hulk_text_start() + { + return cast(ulong)&_hulk_text_start; + } + + public static @property ulong hulk_text_end() + { + return cast(ulong)&_hulk_text_end; + } + + public static @property ulong hulk_text_size() + { + return cast(ulong)&_hulk_text_size; + } + + + public static @property ulong hulk_rodata_start() + { + return cast(ulong)&_hulk_rodata_start; + } + + public static @property ulong hulk_rodata_end() + { + return cast(ulong)&_hulk_rodata_end; + } + + public static @property ulong hulk_rodata_size() + { + return cast(ulong)&_hulk_rodata_size; + } + + + public static @property ulong hulk_data_start() + { + return cast(ulong)&_hulk_data_start; + } + + public static @property ulong hulk_data_end() + { + return cast(ulong)&_hulk_data_end; + } + + public static @property ulong hulk_data_size() + { + return cast(ulong)&_hulk_data_size; + } + + public static @property ulong hulk_bss_start() { return cast(ulong)&_hulk_bss_start; } + public static @property ulong hulk_bss_end() + { + return cast(ulong)&_hulk_bss_end; + } + public static @property ulong hulk_bss_size() { return cast(ulong)&_hulk_bss_size; } + + public static @property ulong hulk_binary_size() + { + return cast(ulong)&_hulk_binary_size; + } + + + public static @property ulong hulk_mem_start() + { + return cast(ulong)&_hulk_mem_start; + } + public static @property ulong hulk_mem_end() { return cast(ulong)&_hulk_mem_end; } - public static @property ulong hulk_total_size() + public static @property ulong hulk_mem_size() { - return cast(ulong)&_hulk_total_size; + return cast(ulong)&_hulk_mem_size; } }