From df7e43df7f823da7e3d4932ebfd5146c835bbc28 Mon Sep 17 00:00:00 2001 From: mintsuki Date: Wed, 20 Apr 2022 04:00:00 +0200 Subject: [PATCH] efi: Adjust for new reduced-gnu-efi --- common/GNUmakefile | 12 +- common/gensyms.sh | 4 +- common/lib/trace.s2.c | 4 +- common/linker_uefi32.ld.in | 218 +++++++++++++++++++++---------------- common/linker_uefi64.ld.in | 207 +++++++++++++++++++++-------------- common/mm/pmm.s2.c | 8 +- 6 files changed, 268 insertions(+), 185 deletions(-) diff --git a/common/GNUmakefile b/common/GNUmakefile index afab553a..3d36a52f 100644 --- a/common/GNUmakefile +++ b/common/GNUmakefile @@ -219,11 +219,11 @@ $(call MKESCAPE,$(BUILDDIR))/stage2.bin: $(call MKESCAPE,$(BUILDDIR))/limine.sys $(call MKESCAPE,$(BUILDDIR))/stage2.map.o: $(call MKESCAPE,$(BUILDDIR))/limine_stage2only.elf cd '$(call SHESCAPE,$(BUILDDIR))' && \ - '$(call SHESCAPE,$(SRCDIR))/gensyms.sh' '$(call SHESCAPE,$<)' stage2 32 + '$(call SHESCAPE,$(SRCDIR))/gensyms.sh' '$(call SHESCAPE,$<)' stage2 32 '\.text' $(call MKESCAPE,$(BUILDDIR))/full.map.o: $(call MKESCAPE,$(BUILDDIR))/limine_nomap.elf cd '$(call SHESCAPE,$(BUILDDIR))' && \ - '$(call SHESCAPE,$(SRCDIR))/gensyms.sh' '$(call SHESCAPE,$<)' full 32 + '$(call SHESCAPE,$(SRCDIR))/gensyms.sh' '$(call SHESCAPE,$<)' full 32 '\.text' $(call MKESCAPE,$(BUILDDIR))/limine.sys: $(call MKESCAPE,$(BUILDDIR))/limine.elf $(LIMINE_OBJCOPY) -O binary '$(call SHESCAPE,$<)' '$(call SHESCAPE,$@)' @@ -279,10 +279,10 @@ ifeq ($(TARGET), uefi64) $(call MKESCAPE,$(BUILDDIR))/full.map.o: $(call MKESCAPE,$(BUILDDIR))/limine_nomap.elf cd '$(call SHESCAPE,$(BUILDDIR))' && \ - '$(call SHESCAPE,$(SRCDIR))/gensyms.sh' '$(call SHESCAPE,$<)' full 64 + '$(call SHESCAPE,$(SRCDIR))/gensyms.sh' '$(call SHESCAPE,$<)' full 64 '\.sect' $(call MKESCAPE,$(BUILDDIR))/BOOTX64.EFI: $(call MKESCAPE,$(BUILDDIR))/limine.elf - $(LIMINE_OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc -j .sbat --target efi-app-x86_64 '$(call SHESCAPE,$<)' '$(call SHESCAPE,$@)' + $(LIMINE_OBJCOPY) -O binary '$(call SHESCAPE,$<)' '$(call SHESCAPE,$@)' $(call MKESCAPE,$(BUILDDIR))/reduced-gnu-efi/gnuefi/crt0-efi-x86_64.o: reduced-gnu-efi true @@ -326,10 +326,10 @@ ifeq ($(TARGET), uefi32) $(call MKESCAPE,$(BUILDDIR))/full.map.o: $(call MKESCAPE,$(BUILDDIR))/limine_nomap.elf cd '$(call SHESCAPE,$(BUILDDIR))' && \ - '$(call SHESCAPE,$(SRCDIR))/gensyms.sh' '$(call SHESCAPE,$<)' full 32 + '$(call SHESCAPE,$(SRCDIR))/gensyms.sh' '$(call SHESCAPE,$<)' full 32 '\.sect' $(call MKESCAPE,$(BUILDDIR))/BOOTIA32.EFI: $(call MKESCAPE,$(BUILDDIR))/limine.elf - $(LIMINE_OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc -j .sbat --target efi-app-ia32 '$(call SHESCAPE,$<)' '$(call SHESCAPE,$@)' + $(LIMINE_OBJCOPY) -O binary '$(call SHESCAPE,$<)' '$(call SHESCAPE,$@)' $(call MKESCAPE,$(BUILDDIR))/reduced-gnu-efi/gnuefi/crt0-efi-ia32.o: reduced-gnu-efi true diff --git a/common/gensyms.sh b/common/gensyms.sh index 047522cd..70a4b28b 100755 --- a/common/gensyms.sh +++ b/common/gensyms.sh @@ -27,8 +27,8 @@ TMP3=$(mktemp) TMP4=$(mktemp) "$LIMINE_OBJDUMP" -t "$1" | ( "$SED" '/[[:<:]]d[[:>:]]/d' 2>/dev/null || "$SED" '/\bd\b/d' ) | sort > "$TMP1" -"$GREP" "\.text" < "$TMP1" | cut -d' ' -f1 > "$TMP2" -"$GREP" "\.text" < "$TMP1" | "$AWK" 'NF{ print $NF }' > "$TMP3" +"$GREP" "$4" < "$TMP1" | cut -d' ' -f1 > "$TMP2" +"$GREP" "$4" < "$TMP1" | "$AWK" 'NF{ print $NF }' > "$TMP3" echo "section .$2_map" > "$TMP4" echo "global $2_map" >> "$TMP4" diff --git a/common/lib/trace.s2.c b/common/lib/trace.s2.c index 77a09996..dd60615a 100644 --- a/common/lib/trace.s2.c +++ b/common/lib/trace.s2.c @@ -11,7 +11,7 @@ #if bios == 1 extern symbol stage2_map; #elif uefi == 1 -extern symbol ImageBase; +extern symbol __image_base; #endif extern symbol full_map; @@ -27,7 +27,7 @@ static char *trace_address(size_t *off, size_t addr) { #elif uefi == 1 limine_map = full_map; - addr -= (size_t)ImageBase; + addr -= (size_t)__image_base; #endif uintptr_t prev_addr = 0; diff --git a/common/linker_uefi32.ld.in b/common/linker_uefi32.ld.in index 9944b043..863c3ad1 100644 --- a/common/linker_uefi32.ld.in +++ b/common/linker_uefi32.ld.in @@ -1,107 +1,143 @@ -/* The following code originates from gnu-efi */ - -OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") +OUTPUT_FORMAT(elf32-i386) OUTPUT_ARCH(i386) ENTRY(_start) + +PHDRS +{ + null PT_NULL FLAGS(0) ; + segm PT_LOAD FLAGS(7) ; + dynamic PT_DYNAMIC FLAGS(7) ; +} + SECTIONS { - . = 0; - ImageBase = .; - /* .hash and/or .gnu.hash MUST come first! */ - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - . = ALIGN(4096); - .text : - { - _text = .; - *(.text) - *(.text.*) - *(.gnu.linkonce.t.*) - . = ALIGN(16); - } - _etext = .; - _text_size = . - _text; - . = ALIGN(4096); - .sdata : - { - _data = .; - *(.got.plt) - *(.got) - *(.srodata) - *(.sdata) - *(.sbss) - *(.scommon) - } - . = ALIGN(4096); - .data : - { - *(.rodata*) - *(.got.plt) - *(.got) + . = 0; + __image_base = .; + __image_size = __image_end - __image_base; + + .sect : { + *(.pe_header) + + . = ALIGN(0x1000); + + __text_start = .; + __text_size = __text_end - __text_start; + *(.text .text.*) + . = ALIGN(0x1000); + __text_end = .; + + __reloc_start = .; + __reloc_size = __reloc_end - __reloc_start; + *(.reloc) + . = ALIGN(0x1000); + __reloc_end = .; + + __sbat_start = .; + __sbat_size = __sbat_end - __sbat_start; + *(.sbat) + . = ALIGN(0x1000); + __sbat_end = .; + + __data_start = .; + __data_size = __data_end - __data_start; + *(.rodata .rodata.*) + #ifdef LINKER_NOMAP full_map = .; #else *(.full_map) #endif + *(.no_unwind) + data_begin = .; - *(.data) - *(.data1) - *(.data.*) - *(.sdata) - /* the EFI loader doesn't seem to like a .bss section, so we stick - it all into .data: */ - *(.sbss) - *(.scommon) - *(.dynbss) - *(.bss) - *(COMMON) + *(.data .data.*) + *(.bss .bss.*) + *(COMMON) data_end = .; - } - .note.gnu.build-id : { *(.note.gnu.build-id) } + . = ALIGN(0x1000); + } :segm - . = ALIGN(4096); - .dynamic : { *(.dynamic) } - . = ALIGN(4096); - .rel : - { - *(.rel.data) - *(.rel.data.*) - *(.rel.got) - *(.rel.stab) - *(.data.rel.ro.local) - *(.data.rel.local) - *(.data.rel.ro) - *(.data.rel*) - } - _edata = .; - _data_size = . - _etext; + .rel : { + *(.rel .rel.*) + . = ALIGN(0x1000); + } :segm - . = ALIGN(4096); - .sbat : - { - _sbat = .; - *(.sbat) - *(.sbat.*) - } - _esbat = .; - _sbat_size = . - _sbat; + .got : { + *(.got .got.*) + . = ALIGN(0x1000); + } :segm - . = ALIGN(4096); - .reloc : /* This is the PECOFF .reloc section! */ - { - *(.reloc) - } - . = ALIGN(4096); - .dynsym : { *(.dynsym) } - . = ALIGN(4096); - .dynstr : { *(.dynstr) } - . = ALIGN(4096); - /DISCARD/ : - { - *(.rel.reloc) - *(.eh_frame) - *(.note.GNU-stack) - } - .comment 0 : { *(.comment) } + .dynamic : { + __dynamic = .; + *(.dynamic) + . = ALIGN(0x1000); + } :segm :dynamic + + __data_end = .; + __image_end = .; + + .symtab 0 : { + *(.symtab) + } :null + + .strtab 0 : { + *(.strtab) + } :null + + .shstrtab 0 : { + *(.shstrtab) + } :null + + .debug_aranges 0 : { + *(.debug_aranges) + } :null + + .debug_pubnames 0 : { + *(.debug_pubnames) + } :null + + .debug_info 0 : { + *(.debug_info) + } :null + + .debug_abbrev 0 : { + *(.debug_abbrev) + } :null + + .debug_line 0 : { + *(.debug_line) + } :null + + .debug_loclists 0 : { + *(.debug_loclists) + } :null + + .debug_rnglists 0 : { + *(.debug_rnglists) + } :null + + .debug_frame 0 : { + *(.debug_frame) + } :null + + .debug_loc 0 : { + *(.debug_loc) + } :null + + .debug_ranges 0 : { + *(.debug_ranges) + } :null + + .debug_str 0 : { + *(.debug_str) + } :null + + .debug_line_str 0 : { + *(.debug_line_str) + } :null + + /DISCARD/ : { + *(*) + } } diff --git a/common/linker_uefi64.ld.in b/common/linker_uefi64.ld.in index a2a10bad..8a1eabf7 100644 --- a/common/linker_uefi64.ld.in +++ b/common/linker_uefi64.ld.in @@ -1,96 +1,143 @@ -/* The following code originates from gnu-efi */ - -OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", "elf64-x86-64") +OUTPUT_FORMAT(elf64-x86-64) OUTPUT_ARCH(i386:x86-64) ENTRY(_start) + +PHDRS +{ + null PT_NULL FLAGS(0) ; + segm PT_LOAD FLAGS(7) ; + dynamic PT_DYNAMIC FLAGS(7) ; +} + SECTIONS { - . = 0; - ImageBase = .; - /* .hash and/or .gnu.hash MUST come first! */ - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - . = ALIGN(4096); - .eh_frame : - { - *(.eh_frame) - } - . = ALIGN(4096); - .text : - { - _text = .; - *(.text) - *(.text.*) - *(.gnu.linkonce.t.*) - . = ALIGN(16); - } - _etext = .; - _text_size = . - _text; - . = ALIGN(4096); - .reloc : - { - *(.reloc) - } - . = ALIGN(4096); - .data : - { - _data = .; - *(.rodata*) - *(.got.plt) - *(.got) + . = 0; + __image_base = .; + __image_size = __image_end - __image_base; + + .sect : { + *(.pe_header) + + . = ALIGN(0x1000); + + __text_start = .; + __text_size = __text_end - __text_start; + *(.text .text.*) + . = ALIGN(0x1000); + __text_end = .; + + __reloc_start = .; + __reloc_size = __reloc_end - __reloc_start; + *(.reloc) + . = ALIGN(0x1000); + __reloc_end = .; + + __sbat_start = .; + __sbat_size = __sbat_end - __sbat_start; + *(.sbat) + . = ALIGN(0x1000); + __sbat_end = .; + + __data_start = .; + __data_size = __data_end - __data_start; + *(.rodata .rodata.*) + #ifdef LINKER_NOMAP full_map = .; #else *(.full_map) #endif + *(.no_unwind) + data_begin = .; - *(.data*) - *(.sdata) - /* the EFI loader doesn't seem to like a .bss section, so we stick - it all into .data: */ - *(.sbss) - *(.scommon) - *(.dynbss) - *(.bss) - *(COMMON) + *(.data .data.*) + *(.bss .bss.*) + *(COMMON) data_end = .; - *(.rel.local) - } - .note.gnu.build-id : { *(.note.gnu.build-id) } + . = ALIGN(0x1000); + } :segm - _edata = .; - _data_size = . - _etext; + .rela : { + *(.rela .rela.*) + . = ALIGN(0x1000); + } :segm - . = ALIGN(4096); - .sbat : - { - _sbat = .; - *(.sbat) - *(.sbat.*) - } - _esbat = .; - _sbat_size = . - _sbat; + .got : { + *(.got .got.*) + . = ALIGN(0x1000); + } :segm - . = ALIGN(4096); - .dynamic : { *(.dynamic) } - . = ALIGN(4096); - .rela : - { - *(.rela.data*) - *(.rela.got) - *(.rela.stab) - } - . = ALIGN(4096); - .dynsym : { *(.dynsym) } - . = ALIGN(4096); - .dynstr : { *(.dynstr) } - . = ALIGN(4096); - .ignored.reloc : - { - *(.rela.reloc) - *(.eh_frame) - *(.note.GNU-stack) - } - .comment 0 : { *(.comment) } + .dynamic : { + __dynamic = .; + *(.dynamic) + . = ALIGN(0x1000); + } :segm :dynamic + + __data_end = .; + __image_end = .; + + .symtab 0 : { + *(.symtab) + } :null + + .strtab 0 : { + *(.strtab) + } :null + + .shstrtab 0 : { + *(.shstrtab) + } :null + + .debug_aranges 0 : { + *(.debug_aranges) + } :null + + .debug_pubnames 0 : { + *(.debug_pubnames) + } :null + + .debug_info 0 : { + *(.debug_info) + } :null + + .debug_abbrev 0 : { + *(.debug_abbrev) + } :null + + .debug_line 0 : { + *(.debug_line) + } :null + + .debug_loclists 0 : { + *(.debug_loclists) + } :null + + .debug_rnglists 0 : { + *(.debug_rnglists) + } :null + + .debug_frame 0 : { + *(.debug_frame) + } :null + + .debug_loc 0 : { + *(.debug_loc) + } :null + + .debug_ranges 0 : { + *(.debug_ranges) + } :null + + .debug_str 0 : { + *(.debug_str) + } :null + + .debug_line_str 0 : { + *(.debug_line_str) + } :null + + /DISCARD/ : { + *(*) + } } diff --git a/common/mm/pmm.s2.c b/common/mm/pmm.s2.c index d2ade3ef..6edcb449 100644 --- a/common/mm/pmm.s2.c +++ b/common/mm/pmm.s2.c @@ -279,8 +279,8 @@ void init_memmap(void) { #endif #if uefi == 1 -extern symbol ImageBase; -extern symbol _edata; +extern symbol __image_base; +extern symbol __image_end; void init_memmap(void) { EFI_STATUS status; @@ -388,10 +388,10 @@ void init_memmap(void) { memcpy(untouched_memmap, memmap, memmap_entries * sizeof(struct e820_entry_t)); untouched_memmap_entries = memmap_entries; - size_t bootloader_size = ALIGN_UP((uintptr_t)_edata - (uintptr_t)ImageBase, 4096); + size_t bootloader_size = ALIGN_UP((uintptr_t)__image_end - (uintptr_t)__image_base, 4096); // Allocate bootloader itself - memmap_alloc_range((uintptr_t)ImageBase, bootloader_size, + memmap_alloc_range((uintptr_t)__image_base, bootloader_size, MEMMAP_BOOTLOADER_RECLAIMABLE, false, true, false, true); sanitise_entries(memmap, &memmap_entries, false);