diff options
38 files changed, 856 insertions, 605 deletions
@@ -8,7 +8,9 @@ build.ninja cmake_install.cmake qtcsettings.cmake -*.xpm +x86_64/ + +*.epm *.app *.com diff --git a/Private/CRT/__cxxkit_defines.h b/Private/CRT/__cxxkit_defines.h index 46110369..9726bf18 100644 --- a/Private/CRT/__cxxkit_defines.h +++ b/Private/CRT/__cxxkit_defines.h @@ -3,7 +3,7 @@ * * hCore * Copyright 2024 Mahrouss Logic, all rights reserved. - * + * * ======================================================== */ @@ -20,69 +20,71 @@ typedef int ssize_t; typedef size_t ptrdiff_t; typedef size_t uintptr_t; -typedef void* voidptr_t; -typedef void* any_t; -typedef char* caddr_t; +typedef void *voidptr_t; +typedef void *any_t; +typedef char *caddr_t; #ifndef NULL #define NULL ((voidptr_t)0) #endif // !null #ifdef __GNUC__ -# include <CRT/__cxxkit_alloca.h> -# define __cxxkit_alloca(sz) __cxxkit_alloca_gcc(sz) +#include <CRT/__cxxkit_alloca.h> +#define __cxxkit_alloca(sz) __cxxkit_alloca_gcc(sz) #elif defined(__HISYS__) -# define __alloca(SZ) __cxxkit_alloca(SZ) +#define __alloca(SZ) __cxxkit_alloca(SZ) -# define __deref(ptr) (*(PTR)) +#define __deref(ptr) (*(PTR)) -# define __libexport __lib(export) -# define __libimport __lib(import) +#define __libexport __lib(export) +#define __libimport __lib(import) #ifdef __cplusplus -# define __init_decl() extern "C" { -# define __fini_decl() }; +#define __init_decl() \ + extern "C" \ + { +#define __fini_decl() \ + } \ + ; #else -# define __init_decl() -# define __fini_decl() +#define __init_decl() +#define __fini_decl() #endif #endif #if __has_builtin(__builtin_alloca) -# define alloca(sz) __builtin_alloca(sz) +#define alloca(sz) __builtin_alloca(sz) #ifdef __alloca -# undef __alloca +#undef __alloca #endif #define __alloca alloca #else -# warning alloca not detected +#warning alloca not detected #endif typedef long long off_t; typedef unsigned long long uoff_t; -typedef union -{ - struct - { - unsigned int mantisa : 23; - unsigned int exponent : 8; - unsigned int sign : 1; - }; - - float f; +typedef union { + struct + { + unsigned int mantissa : 23; + unsigned int exponent : 8; + unsigned int sign : 1; + }; + + float f; } __attribute__((packed)) float_cast_t; -typedef union -{ - struct +typedef union { + struct { - unsigned long int mantisa : 52; - unsigned int exponent : 11; - unsigned int sign : 1; + unsigned long long int mantissa : 52; + unsigned int exponent : 11; + unsigned int sign : 1; }; - + double f; } __attribute__((packed)) double_cast_t; diff --git a/Private/NewBoot/Source/MPT/.hgkeep b/Private/EFIKit/.gitkeep index e69de29b..e69de29b 100644 --- a/Private/NewBoot/Source/MPT/.hgkeep +++ b/Private/EFIKit/.gitkeep diff --git a/Private/Linker/AMD64.ld b/Private/Linker/AMD64.ld index 81e87dc6..f75c7533 100644 --- a/Private/Linker/AMD64.ld +++ b/Private/Linker/AMD64.ld @@ -10,7 +10,7 @@ PHDRS { } SECTIONS { - . = 1M; + . = 0x10000000; .text : { *(.text .text.*) diff --git a/Private/NewBoot/BootKit/Boot.hpp b/Private/NewBoot/BootKit/Boot.hpp index 7323737e..6df2e08f 100644 --- a/Private/NewBoot/BootKit/Boot.hpp +++ b/Private/NewBoot/BootKit/Boot.hpp @@ -13,13 +13,13 @@ using namespace hCore; -typedef void* PEFImage; +typedef void *PEFImage; enum { - kSegmentCode = 2, - kSegmentData = 4, - kSegmentBss = 6, + kSegmentCode = 2, + kSegmentData = 4, + kSegmentBss = 6, }; /** @@ -28,29 +28,20 @@ enum */ class BKTextWriter final { - volatile UInt16* fWhere{ nullptr }; + volatile UInt16 *fWhere{nullptr}; -public: - void WriteString(const char* c, - unsigned char forecolour, - unsigned char backcolour, - int x, - int y); + public: + void WriteString(const char *c, unsigned char forecolour, unsigned char backcolour, int x, int y); - void WriteCharacter(char c, - unsigned char forecolour, - unsigned char backcolour, - int x, - int y); + void WriteCharacter(char c, unsigned char forecolour, unsigned char backcolour, int x, int y); -public: + public: BKTextWriter() = default; ~BKTextWriter() = default; -public: - BKTextWriter& operator=(const BKTextWriter&) = default; - BKTextWriter(const BKTextWriter&) = default; - + public: + BKTextWriter &operator=(const BKTextWriter &) = default; + BKTextWriter(const BKTextWriter &) = default; }; enum @@ -73,5 +64,6 @@ enum kWhite, }; -#define BK_START_KERNEL (0x00080000) +#define BK_START_KERNEL (0x10000000) +long long int BStrLen(const char *ptr); diff --git a/Private/NewBoot/Source/HEL/AMD64/ATA.cxx b/Private/NewBoot/Source/HEL/AMD64/ATA.cxx deleted file mode 100644 index 7bcd304a..00000000 --- a/Private/NewBoot/Source/HEL/AMD64/ATA.cxx +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright 2024 Mahrouss Logic, all rights reserved - */ - -#include "ATA.hxx" -#include <ArchKit/Arch.hpp> - -using namespace hCore::HAL; - -static Boolean kATADetected = false; - -void IDESelect(UInt8 Bus, Boolean isMaster) -{ - if (Bus == ATA_PRIMARY) - out8(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, - isMaster ? ATA_PRIMARY_SEL : ATA_SECONDARY_SEL); - else - out8(ATA_SECONDARY_IO + ATA_REG_HDDEVSEL, - isMaster ? ATA_PRIMARY_SEL : ATA_SECONDARY_SEL); -} - -Boolean ATAInitDriver(UInt8 Bus, UInt8 Drive) -{ - UInt16 IO = (Bus == ATA_PRIMARY) ? ATA_PRIMARY_IO : ATA_SECONDARY_IO; - - IDESelect(Bus, Drive); - - out8(IO + ATA_REG_SEC_COUNT0, 0); - out8(IO + ATA_REG_LBA0, 0); - out8(IO + ATA_REG_LBA1, 0); - out8(IO + ATA_REG_LBA2, 0); - - out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); - - UInt8 status = in8(IO + ATA_REG_STATUS); - - if (status) - { - while ((status = in8(IO + ATA_REG_STATUS) & ATA_SR_BSY)) - ; - - if (status & ATA_REG_ERROR) - { - return false; - } - - kATADetected = true; - return status; - } - - return false; -} - -void ATAWait(UInt16 IO) -{ - for (int i = 0; i < 4000; i++) - in8(IO + ATA_REG_ALT_STATUS); -} - -void IDEPoll(UInt16 IO) { ATAWait(IO); } - -UInt16 ATAReadLba(UInt32 lba, UInt8 bus, Boolean master) -{ - UInt16 IO = bus; - IDESelect(IO, master ? ATA_MASTER : ATA_SLAVE); - - out8(IO + ATA_REG_LBA5, (UInt8)(lba >> 24) & 0xF); - - out8(IO + ATA_REG_SEC_COUNT0, lba / 512); - - out8(IO + ATA_REG_LBA0, (UInt8)lba); - out8(IO + ATA_REG_LBA1, (UInt8)(lba >> 8)); - out8(IO + ATA_REG_LBA2, (UInt8)(lba >> 16)); - - out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); - - IDEPoll(IO); - - UInt16 data = in16(IO + ATA_REG_DATA); - - while ((in8(ATA_COMMAND(IO))) & 0x88) - ATAWait(IO); - - return data; -} - -Void ATAWriteLba(UInt16 Byte, UInt32 lba, UInt8 bus, Boolean master) -{ - UInt16 IO = bus; - IDESelect(IO, master ? ATA_MASTER : ATA_SLAVE); - - out8(IO + ATA_REG_LBA5, (UInt8)(lba >> 24) & 0xF); - - out8(IO + ATA_REG_SEC_COUNT0, lba / 512); - - out8(IO + ATA_REG_LBA0, (UInt8)lba); - out8(IO + ATA_REG_LBA1, (UInt8)(lba >> 8)); - out8(IO + ATA_REG_LBA2, (UInt8)(lba >> 16)); - - out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); // TODO: support DMA - - IDEPoll(IO); - - out32(IO + ATA_REG_DATA, Byte); - - while ((in8(ATA_COMMAND(IO))) & 0x88) - ATAWait(IO); -} - -Boolean ATAIsDetected(Void) { return kATADetected; } - -extern "C" void _start(void) -{ - ATAInitDriver(ATA_PRIMARY, true); - ATAInitDriver(ATA_PRIMARY, false); - - ATAInitDriver(ATA_SECONDARY, true); - ATAInitDriver(ATA_SECONDARY, false); -} - -extern "C" void out8(UInt16 port, UInt8 value) -{ - asm volatile("outb %%al, %1" : : "a"(value), "Nd"(port) : "memory"); -} - -extern "C" void out16(UInt16 port, UInt16 value) -{ - asm volatile("outw %%ax, %1" : : "a"(value), "Nd"(port) : "memory"); -} - -extern "C" void out32(UInt16 port, UInt32 value) -{ - asm volatile("outl %%eax, %1" : : "a"(value), "Nd"(port) : "memory"); -} - -extern "C" UInt8 in8(UInt16 port) -{ - UInt8 value = 0UL; - asm volatile("inb %1, %%al" : "=a"(value) : "Nd"(port) : "memory"); - - return value; -} - -extern "C" UInt16 in16(UInt16 port) -{ - UInt16 value = 0UL; - asm volatile("inw %1, %%ax" : "=a"(value) : "Nd"(port) : "memory"); - - return value; -} - -extern "C" UInt32 in32(UInt16 port) -{ - UInt32 value = 0UL; - asm volatile("inl %1, %%eax" : "=a"(value) : "Nd"(port) : "memory"); - - return value; -} - -extern "C" int init_ata_mpt(void) -{ - for (int i = 0; i < 255; ++i) - { - if (ATAInitDriver(i, ATA_MASTER)) - { - ATAInitDriver(i, ATA_SLAVE); - return 1; - } - } - - return 0; -}
\ No newline at end of file diff --git a/Private/NewBoot/Source/HEL/AMD64/ATA.hxx b/Private/NewBoot/Source/HEL/AMD64/ATA.hxx deleted file mode 100644 index 09cc4007..00000000 --- a/Private/NewBoot/Source/HEL/AMD64/ATA.hxx +++ /dev/null @@ -1,106 +0,0 @@ -#pragma once - -#include <NewKit/Defines.hpp> - -using namespace hCore; - -// Status register -#define ATA_SR_BSY 0x80 -#define ATA_SR_DRDY 0x40 -#define ATA_SR_DF 0x20 -#define ATA_SR_DSC 0x10 -#define ATA_SR_DRQ 0x08 -#define ATA_SR_CORR 0x04 -#define ATA_SR_IDX 0x02 -#define ATA_SR_ERR 0x01 - -// Error register -#define ATA_ER_BBK 0x80 -#define ATA_ER_UNC 0x40 -#define ATA_ER_MC 0x20 -#define ATA_ER_IDNF 0x10 -#define ATA_ER_MCR 0x08 -#define ATA_ER_ABRT 0x04 -#define ATA_ER_TK0NF 0x02 -#define ATA_ER_AMNF 0x01 - -#define ATA_CMD_READ_PIO 0x20 -#define ATA_CMD_READ_PIO_EXT 0x24 -#define ATA_CMD_READ_DMA 0xC8 -#define ATA_CMD_READ_DMA_EXT 0x25 -#define ATA_CMD_WRITE_PIO 0x30 -#define ATA_CMD_WRITE_PIO_EXT 0x34 -#define ATA_CMD_WRITE_DMA 0xCA -#define ATA_CMD_WRITE_DMA_EXT 0x35 -#define ATA_CMD_CACHE_FLUSH 0xE7 -#define ATA_CMD_CACHE_FLUSH_EXT 0xEA -#define ATA_CMD_PACKET 0xA0 -#define ATA_CMD_IDENTIFY_PACKET 0xA1 -#define ATA_CMD_IDENTIFY 0xEC - -#define ATA_IDENT_DEVICE_TYPE 0 -#define ATA_IDENT_CYLINDERS 2 -#define ATA_IDENT_HEADS 6 -#define ATA_IDENT_SECTORS 12 -#define ATA_IDENT_SERIAL 20 -#define ATA_IDENT_MODEL 54 -#define ATA_IDENT_CAPABILITIES 98 -#define ATA_IDENT_FIELDVALID 106 -#define ATA_IDENT_MAX_LBA 120 -#define ATA_IDENT_COMMANDSETS 164 -#define ATA_IDENT_MAX_LBA_EXT 200 - - -#define ATA_MASTER 0x00 -#define ATA_SLAVE 0x01 - -// Register -#define ATA_REG_DATA 0x00 -#define ATA_REG_ERROR 0x01 -#define ATA_REG_FEATURES 0x01 -#define ATA_REG_SEC_COUNT0 0x02 -#define ATA_REG_LBA0 0x03 -#define ATA_REG_LBA1 0x04 -#define ATA_REG_LBA2 0x05 -#define ATA_REG_HDDEVSEL 0x06 -#define ATA_REG_COMMAND 0x07 -#define ATA_REG_STATUS 0x07 -#define ATA_REG_SEC_COUNT1 0x08 -#define ATA_REG_LBA3 0x09 -#define ATA_REG_LBA4 0x0A -#define ATA_REG_LBA5 0x0B -#define ATA_REG_CONTROL 0x0C -#define ATA_REG_ALT_STATUS 0x0C -#define ATA_REG_DEV_ADDRESS 0x0D - -#define ATA_PRIMARY_IO 0x1F0 -#define ATA_SECONDARY_IO 0x170 -#define ATA_PRIMARY_DCR_AS 0x3F6 -#define ATA_SECONDARY_DCR_AS 0x376 - -// Irq -#define ATA_PRIMARY_IRQ 14 -#define ATA_SECONDARY_IRQ 15 - -// Channels -#define ATA_PRIMARY 0x00 -#define ATA_SECONDARY 0x01 - -// IO Direction -#define ATA_READ 0x00 -#define ATA_WRITE 0x013 - -#define ATA_PRIMARY_SEL 0xA0 -#define ATA_SECONDARY_SEL 0xB0 - -// ATA Helpers -#define ATA_ADDRESS1(x) (x + 3) -#define ATA_ADDRESS2(x) (x + 4) -#define ATA_ADDRESS3(x) (x + 5) -#define ATA_COMMAND(x) (x + 7) - -Boolean ATAInitDriver(UInt8 bus, UInt8 drive); -Void ATAWait(UInt16 IO); -UInt16 ATAReadLba(UInt32 lba, UInt8 bus, Boolean master); -Void ATAWriteLba(UInt16 byte, UInt32 lba, UInt8 bus, Boolean master); -Boolean ATAIsDetected(Void);
\ No newline at end of file diff --git a/Private/NewBoot/Source/HEL/AMD64/BIOSAllocApi.inc b/Private/NewBoot/Source/HEL/AMD64/BIOSAllocApi.inc index 5b007434..551cf0f6 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BIOSAllocApi.inc +++ b/Private/NewBoot/Source/HEL/AMD64/BIOSAllocApi.inc @@ -13,7 +13,7 @@ __LoadGDT: lgdt [ebp - 8] ret -;; This memory resides in the BIOS Memory Region BMR (0x9000-0x9000 + 4096) +;; This memory resides in the BIOS Memory Region BMR (0x9000-(0x9000 + 0x1000)) ;; It contains bootloader data. __BIOSAlloc: @@ -37,4 +37,4 @@ __BIOSAllocDone: __bios_lookup_table: resb 8096 * 4 -__end_bios_lookup_table:
\ No newline at end of file +__end_bios_lookup_table: diff --git a/Private/NewBoot/Source/HEL/AMD64/makefile b/Private/NewBoot/Source/HEL/AMD64/makefile deleted file mode 100644 index 576bb12b..00000000 --- a/Private/NewBoot/Source/HEL/AMD64/makefile +++ /dev/null @@ -1,37 +0,0 @@ -ASM = nasm - -# The kernel entrypoint -ENTRY = NBRuntimeZero - -# Where the text segment is. -TEXT = 0x7c00 - -# we want a flat binary -FMT = binary - -ASMFLAGS = -f elf64 - -KERNEL = hBoot.bin -ATAMOD = ATA.bin - -LD = x86_64-elf-ld -CC = x86_64-elf-gcc -CCFLAGS = -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__hCore__ -I./ -I../../../ -I$(HOME)/hCore/ - -.PHONY: build-crt0-bios -build-crt0-bios: - $(CC) $(CCFLAGS) BIOSRuntime.cxx - $(CC) $(CCFLAGS) ATA.cxx - $(CC) $(CCFLAGS) Processor.cxx - $(ASM) $(ASMFLAGS) BIOSRuntime0.asm - - $(LD) --oformat $(FMT) ATA.o -o $(ATAMOD) - $(LD) -e $(ENTRY) -Ttext $(TEXT) --oformat $(FMT) BIOSRuntime.o BIOSRuntime0.o -o $(KERNEL) - -.PHONY: all -all: build-crt0-bios - @echo "Done (CRT-0)" - -.PHONY: clean -clean: - rm -f $(wildcard *.o) $(wildcard *.bin)
\ No newline at end of file diff --git a/Private/NewBoot/Source/MPT/API.hxx b/Private/NewBoot/Source/MPT/API.hxx deleted file mode 100644 index 5958f1c3..00000000 --- a/Private/NewBoot/Source/MPT/API.hxx +++ /dev/null @@ -1,17 +0,0 @@ -/* - * ======================================================== - * - * NewBoot - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#pragma once - -#ifdef __MPT_NEED_ATA_SUPPORT -extern "C" int init_ata_mpt(void); -#endif // __MPT_NEED_ATA_SUPPORT - -#include "Detail.hxx" -#include "MPT.hxx" diff --git a/Private/NewBoot/Source/MPT/Detail.hxx b/Private/NewBoot/Source/MPT/Detail.hxx deleted file mode 100644 index 420387d1..00000000 --- a/Private/NewBoot/Source/MPT/Detail.hxx +++ /dev/null @@ -1,45 +0,0 @@ -/* - * ======================================================== - * - * NewBoot - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#pragma once - -namespace detail -{ -inline void hang(); -inline void panic(const char *msg, const char *expr) -{ -} - -inline void assert_expr(bool expr, const char *str_expr) -{ - if (!expr) - { - detail::panic("assertion failed!", str_expr); - detail::hang(); - } -} - -inline void hang() -{ - while (1) - { - } -} - -enum -{ - okay = 1, - failed = 0, -}; -} // namespace detail - -#ifdef assert -#undef assert -#define assert(expr) detail::assert_expr(expr, #expr) -#endif // ifdef assert diff --git a/Private/NewBoot/Source/MPT/MPT.hxx b/Private/NewBoot/Source/MPT/MPT.hxx deleted file mode 100644 index 9ac79cd1..00000000 --- a/Private/NewBoot/Source/MPT/MPT.hxx +++ /dev/null @@ -1,30 +0,0 @@ -/* - * ======================================================== - * - * NewBoot - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#pragma once - -/// @brief 255 size partition header. -/// we use that to gather information about this hard drive. - -struct MasterPartitionTable final -{ - char fPartName[32]; - int fPartType; - int fPartSectorSz; - int fPartSectorCnt; - char fReserved[211]; -}; - -enum -{ - kPartEfi = 0x10, - kPartEpm = 0x11, - kPartEbr = 0x12, -}; - diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index 75993507..25eb7983 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -1,16 +1,16 @@ # (C) Mahrouss Logic, 2024, all rights reserved. -CC_GNU=x86_64-elf-g++ -LD_GNU=x86_64-elf-ld -OBJCOPY_GNU=x86_64-elf-objcopy +CC_GNU=x86_64-w64-mingw32-g++ +LD_GNU=x86_64-w64-mingw32-ld + FLAG_GNU=-I../ -I../../ -I../../efiSDK/inc -I./ -c -fPIC -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__hCore__ -I./ -I$(HOME)/ .PHONY: arch-amd64 arch-amd64: $(CC_GNU) $(FLAG_GNU) -D__DBG__ HEL/AMD64/*.cxx - $(LD_GNU) *.o -e Main -shared -o HCORELDR.ELF - $(OBJCOPY_GNU) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --target efi-app-x86_64 --subsystem=10 HCORELDR.ELF HCORELDR.EFI + $(LD_GNU) *.o --script=../../efiSDK/gnuefi/coff_x86_64_efi.lds ../../efiSDK/x86_64/gnuefi/crt0-efi-x86_64.o \ + --subsystem=10 ../../efiSDK/x86_64/gnuefi/reloc_x86_64.o -L../../efiSDK/x86_64/gnuefi/ -lgnuefi -o HCORELDR.EXE .PHONY: clean clean: - rm -f *.o + rm -f *.o *.EFI *.EXE diff --git a/Private/efiSDK/COMPILE-MINGW.sh b/Private/efiSDK/COMPILE-MINGW.sh new file mode 100755 index 00000000..540cbf1b --- /dev/null +++ b/Private/efiSDK/COMPILE-MINGW.sh @@ -0,0 +1,3 @@ +#! /bin/sh + +prefix=x86_64-w64-mingw32- ARCH=x86_64 make diff --git a/Private/efiSDK/ChangeLog b/Private/efiSDK/ChangeLog index b30ba252..6a2944b9 100644 --- a/Private/efiSDK/ChangeLog +++ b/Private/efiSDK/ChangeLog @@ -1220,7 +1220,7 @@ Date: Tue Jun 25 08:47:03 2013 -0400 * gnuefi/elf_ia32_efi.lds: Drop unneeded ".rel.reloc" section. - * gnuefi/crt0-efi-ia32.S (_start): Exit if _relocate() returns with + * gnuefi/crt0-efi-ia32.S (__main): Exit if _relocate() returns with non-zero exit status. * inc/ia32/efibind.h [__GNUC__]: Force 8-byte alignment for 64-bit @@ -1271,7 +1271,7 @@ Date: Tue Jun 25 08:47:03 2013 -0400 * gnuefi/elf_ia64_efi.lds: Add .sdata section for small data and put __gp in the "middle" of it. - * gnuefi/crt0-efi-ia64.S (_start): Use movl/add to load + * gnuefi/crt0-efi-ia64.S (__main): Use movl/add to load gp-relative addresses that could be out of the range of the addl offset. * gnuefi/reloc_ia64.S (_relocate): Ditto. diff --git a/Private/efiSDK/README.gnuefi b/Private/efiSDK/README.gnuefi index 512698c2..73e1cdcc 100644 --- a/Private/efiSDK/README.gnuefi +++ b/Private/efiSDK/README.gnuefi @@ -324,7 +324,7 @@ found in gnuefi/reloc_ia64.S. The self-relocator operates as follows: the startup code invokes it right after EFI has handed off control to the EFI binary at symbol -"_start". Upon activation, the self-relocator searches the .dynamic +"__main". Upon activation, the self-relocator searches the .dynamic section (whose starting address is given by symbol _DYNAMIC) for the dynamic relocation information, which can be found in the DT_REL, DT_RELSZ, and DT_RELENT entries of the dynamic table (DT_RELA, @@ -367,11 +367,11 @@ in gnuefi/crt0-efi-ia64.S crafts one manually by with the code: .section .plabel, "a" _start_plabel: - data8 _start + data8 __main data8 __gp -this places the procedure label for entry point _start in a section -called ".plabel". Now, the only problem is that _start and __gp need +this places the procedure label for entry point __main in a section +called ".plabel". Now, the only problem is that __main and __gp need to be relocated _before_ EFI hands control over to the EFI binary. Fortunately, PE32+ defines a section called ".reloc" that can achieve this. Thus, in addition to manually crafting the function descriptor, diff --git a/Private/efiSDK/apps/trivial.S b/Private/efiSDK/apps/trivial.S index 3ec65623..6e4f369d 100644 --- a/Private/efiSDK/apps/trivial.S +++ b/Private/efiSDK/apps/trivial.S @@ -1,8 +1,8 @@ .text .align 4 - .globl _start -_start: + .globl __main +__main: #if 0 pushl %ebp movl %esp,%ebp diff --git a/Private/efiSDK/gnuefi/coff_x86_64_efi.lds b/Private/efiSDK/gnuefi/coff_x86_64_efi.lds new file mode 100644 index 00000000..2473553b --- /dev/null +++ b/Private/efiSDK/gnuefi/coff_x86_64_efi.lds @@ -0,0 +1,113 @@ +/* Same as elf_x86_64_fbsd_efi.lds, except for OUTPUT_FORMAT below - KEEP IN SYNC */ + +OUTPUT_FORMAT("pei-x86-64") +OUTPUT_ARCH(i386:x86-64) +ENTRY(__main) +SECTIONS +{ + . = 0; + ImageBase = .; + /* .hash and/or .gnu.hash MUST come first! */ + .hash : { *(.hash) } + .gnu.hash : { *(.gnu.hash) } + . = ALIGN(4096); + .eh_frame : { *(.eh_frame) } + .gcc_except_table : { *(.gcc_except_table*) } + . = ALIGN(4096); + .text : + { + _text = .; + *(.text) + *(.text.*) + *(.gnu.linkonce.t.*) + *(.plt) + . = ALIGN(16); + } + _etext = .; + _text_size = _etext - _text; + . = ALIGN(4096); + .reloc : + { + KEEP (*(.reloc)) + } + + . = ALIGN(4096); + .data : + { + _data = .; + *(.got.plt) + *(.got) + *(.data*) + *(.sdata) + + /* + * Note that these aren't the using the GNU "CONSTRUCTOR" output section + * command, so they don't start with a size. Because of p2align and the + * end/END definitions, and the fact that they're mergeable, they can also + * have NULLs which aren't guaranteed to be at the end. + */ + . = ALIGN(16); + __init_array_start = .; + *(SORT(.init_array.*)) + *(.init_array) + __init_array_end = .; + . = ALIGN(16); + __CTOR_LIST__ = .; + *(SORT(.ctors.*)) + *(.ctors) + __CTOR_END__ = .; + . = ALIGN(16); + __DTOR_LIST__ = .; + *(SORT(.dtors.*)) + *(.dtors) + __DTOR_END__ = .; + . = ALIGN(16); + __fini_array_start = .; + *(SORT(.fini_array.*)) + *(.fini_array) + __fini_array_end = .; + + /* the EFI loader doesn't seem to like a .bss section, so we stick + it all into .data: */ + *(.sbss) + *(.scommon) + *(.dynbss) + *(.bss*) + *(COMMON) + *(.rel.local) + } + .note.gnu.build-id : { *(.note.gnu.build-id) } + + _edata = .; + _data_size = _edata - _etext; + . = ALIGN(4096); + .dynamic : { *(.dynamic) } + . = ALIGN(4096); + .rela : + { + *(.rela.text*) + *(.rela.data*) + *(.rela.got) + *(.rela.dyn) + *(.rela.stab) + *(.rela.init_array*) + *(.rela.fini_array*) + *(.rela.ctors*) + *(.rela.dtors*) + } + . = ALIGN(4096); + .rela.plt : { *(.rela.plt) } + . = ALIGN(4096); + .rodata : { *(.rodata*) } + . = ALIGN(4096); + .dynsym : { *(.dynsym) } + . = ALIGN(4096); + .dynstr : { *(.dynstr) } + . = ALIGN(4096); + .ignored.reloc : + { + *(.rela.reloc) + *(.note.GNU-stack) + } + .comment 0 : { *(.comment) } +} diff --git a/Private/efiSDK/gnuefi/crt0-efi-aarch64.S b/Private/efiSDK/gnuefi/crt0-efi-aarch64.S index 236b5444..41489c75 100644 --- a/Private/efiSDK/gnuefi/crt0-efi-aarch64.S +++ b/Private/efiSDK/gnuefi/crt0-efi-aarch64.S @@ -19,9 +19,9 @@ .text .align 12 - .globl _start - .type _start,%function -_start: + .globl __main + .type __main,%function +__main: stp x29, x30, [sp, #-32]! mov x29, sp diff --git a/Private/efiSDK/gnuefi/crt0-efi-arm.S b/Private/efiSDK/gnuefi/crt0-efi-arm.S index d84d43cf..56ecd2bb 100644 --- a/Private/efiSDK/gnuefi/crt0-efi-arm.S +++ b/Private/efiSDK/gnuefi/crt0-efi-arm.S @@ -45,11 +45,11 @@ optional_header: .2byte 0x10b // PE32+ format .byte 0x02 // MajorLinkerVersion .byte 0x14 // MinorLinkerVersion - .4byte _etext - _start // SizeOfCode + .4byte _etext - __main // SizeOfCode .4byte _alldata_size - ImageBase // SizeOfInitializedData .4byte 0 // SizeOfUninitializedData - .4byte _start - ImageBase // AddressOfEntryPoint - .4byte _start - ImageBase // BaseOfCode + .4byte __main - ImageBase // AddressOfEntryPoint + .4byte __main - ImageBase // BaseOfCode .4byte _reloc - ImageBase // BaseOfData extra_header_fields: @@ -67,7 +67,7 @@ extra_header_fields: .4byte _image_end - ImageBase // SizeOfImage // Everything before the kernel image is considered part of the header - .4byte _start - ImageBase // SizeOfHeaders + .4byte __main - ImageBase // SizeOfHeaders .4byte 0 // CheckSum .2byte EFI_SUBSYSTEM // Subsystem .2byte 0 // DllCharacteristics @@ -100,10 +100,10 @@ extra_header_fields: section_table: .ascii ".text\0\0\0" - .4byte _evtext - _start // VirtualSize - .4byte _start - ImageBase // VirtualAddress - .4byte _etext - _start // SizeOfRawData - .4byte _start - ImageBase // PointerToRawData + .4byte _evtext - __main // VirtualSize + .4byte __main - ImageBase // VirtualAddress + .4byte _etext - __main // SizeOfRawData + .4byte __main - ImageBase // PointerToRawData .4byte 0 // PointerToRelocations (0 for executables) .4byte 0 // PointerToLineNumbers (0 for executables) .2byte 0 // NumberOfRelocations (0 for executables) @@ -149,9 +149,9 @@ section_table: .4byte 0x40000040 // Characteristics (section flags) .balign 256 -.globl _start -.type _start,%function -_start: +.globl __main +.type __main,%function +__main: stmfd sp!, {r0-r2, lr} mov r2, r0 diff --git a/Private/efiSDK/gnuefi/crt0-efi-ia32.S b/Private/efiSDK/gnuefi/crt0-efi-ia32.S index e7023ab5..8ada9ab2 100644 --- a/Private/efiSDK/gnuefi/crt0-efi-ia32.S +++ b/Private/efiSDK/gnuefi/crt0-efi-ia32.S @@ -36,9 +36,9 @@ .text .align 4 - .globl _start - .type _start,%function -_start: + .globl __main + .type __main,%function +__main: pushl %ebp movl %esp,%ebp diff --git a/Private/efiSDK/gnuefi/crt0-efi-ia64.S b/Private/efiSDK/gnuefi/crt0-efi-ia64.S index 30714d34..e22f2674 100644 --- a/Private/efiSDK/gnuefi/crt0-efi-ia64.S +++ b/Private/efiSDK/gnuefi/crt0-efi-ia64.S @@ -37,8 +37,8 @@ .psr lsb .lsb - .proc _start -_start: + .proc __main +__main: alloc loc0=ar.pfs,2,2,2,0 mov loc1=rp movl out0=@gprel(ImageBase) // out0 <- ImageBase (ldbase) @@ -64,7 +64,7 @@ _start: ;; br.ret.sptk.few rp - .endp _start + .endp __main // PE32+ wants a PLABEL, not the code address of the entry point: @@ -73,7 +73,7 @@ _start: .global _start_plabel .section .plabel, "a" _start_plabel: - data8 _start + data8 __main data8 __gp // hand-craft a .reloc section for the plabel: diff --git a/Private/efiSDK/gnuefi/crt0-efi-loongarch64.S b/Private/efiSDK/gnuefi/crt0-efi-loongarch64.S index 97e32651..d86236c4 100644 --- a/Private/efiSDK/gnuefi/crt0-efi-loongarch64.S +++ b/Private/efiSDK/gnuefi/crt0-efi-loongarch64.S @@ -48,11 +48,11 @@ optional_header: .2byte 0x20b // PE32+ format .byte 0x02 // MajorLinkerVersion .byte 0x14 // MinorLinkerVersion - .4byte _edata - _start // SizeOfCode + .4byte _edata - __main // SizeOfCode .4byte 0 // SizeOfInitializedData .4byte 0 // SizeOfUninitializedData - .4byte _start - ImageBase // AddressOfEntryPoint - .4byte _start - ImageBase // BaseOfCode + .4byte __main - ImageBase // AddressOfEntryPoint + .4byte __main - ImageBase // BaseOfCode extra_header_fields: .8byte 0 // ImageBase @@ -69,7 +69,7 @@ extra_header_fields: .4byte _edata - ImageBase // SizeOfImage // Everything before the kernel image is considered part of the header - .4byte _start - ImageBase // SizeOfHeaders + .4byte __main - ImageBase // SizeOfHeaders .4byte 0 // CheckSum .2byte EFI_SUBSYSTEM // Subsystem .2byte 0 // DllCharacteristics @@ -123,10 +123,10 @@ section_table: .byte 0 .byte 0 .byte 0 // end of 0 padding of section name - .4byte _edata - _start // VirtualSize - .4byte _start - ImageBase // VirtualAddress - .4byte _edata - _start // SizeOfRawData - .4byte _start - ImageBase // PointerToRawData + .4byte _edata - __main // VirtualSize + .4byte __main - ImageBase // VirtualAddress + .4byte _edata - __main // SizeOfRawData + .4byte __main - ImageBase // PointerToRawData .4byte 0 // PointerToRelocations (0 for executables) .4byte 0 // PointerToLineNumbers (0 for executables) @@ -136,9 +136,9 @@ section_table: .align 4 - .globl _start - .type _start, @function -_start: + .globl __main + .type __main, @function +__main: addi.d $sp, $sp, -24 st.d $ra, $sp, 0 st.d $a0, $sp, 8 @@ -158,4 +158,4 @@ _start: 0: ld.d $ra, $sp, 0 addi.d $sp, $sp, 24 jr $ra - .end _start + .end __main diff --git a/Private/efiSDK/gnuefi/crt0-efi-mips64el.S b/Private/efiSDK/gnuefi/crt0-efi-mips64el.S index 334c3086..06dbf061 100644 --- a/Private/efiSDK/gnuefi/crt0-efi-mips64el.S +++ b/Private/efiSDK/gnuefi/crt0-efi-mips64el.S @@ -45,11 +45,11 @@ optional_header: .2byte 0x20b // PE32+ format .byte 0x02 // MajorLinkerVersion .byte 0x14 // MinorLinkerVersion - .4byte _edata - _start // SizeOfCode + .4byte _edata - __main // SizeOfCode .4byte 0 // SizeOfInitializedData .4byte 0 // SizeOfUninitializedData - .4byte _start - ImageBase // AddressOfEntryPoint - .4byte _start - ImageBase // BaseOfCode + .4byte __main - ImageBase // AddressOfEntryPoint + .4byte __main - ImageBase // BaseOfCode extra_header_fields: .8byte 0 // ImageBase @@ -66,7 +66,7 @@ extra_header_fields: .4byte _edata - ImageBase // SizeOfImage // Everything before the kernel image is considered part of the header - .4byte _start - ImageBase // SizeOfHeaders + .4byte __main - ImageBase // SizeOfHeaders .4byte 0 // CheckSum .2byte EFI_SUBSYSTEM // Subsystem .2byte 0 // DllCharacteristics @@ -120,10 +120,10 @@ section_table: .byte 0 .byte 0 .byte 0 // end of 0 padding of section name - .4byte _edata - _start // VirtualSize - .4byte _start - ImageBase // VirtualAddress - .4byte _edata - _start // SizeOfRawData - .4byte _start - ImageBase // PointerToRawData + .4byte _edata - __main // VirtualSize + .4byte __main - ImageBase // VirtualAddress + .4byte _edata - __main // SizeOfRawData + .4byte __main - ImageBase // PointerToRawData .4byte 0 // PointerToRelocations (0 for executables) .4byte 0 // PointerToLineNumbers (0 for executables) @@ -135,10 +135,10 @@ section_table: .set noreorder .align 4 - .globl _start - .ent _start - .type _start, @function -_start: + .globl __main + .ent __main + .type __main, @function +__main: daddiu $sp, -32 sd $ra, ($sp) @@ -182,7 +182,7 @@ _pc: // a0: ImageHandle ld $a0, 16($sp) - // call _start + // call __main dla $t9, _entry jalr $t9 // a1: SystemTable @@ -193,7 +193,7 @@ _pc: ld $ra, ($sp) jr $ra daddiu $sp, 32 - .end _start + .end __main .set pop diff --git a/Private/efiSDK/gnuefi/crt0-efi-riscv64.S b/Private/efiSDK/gnuefi/crt0-efi-riscv64.S index 712ed03f..7fcfbda9 100644 --- a/Private/efiSDK/gnuefi/crt0-efi-riscv64.S +++ b/Private/efiSDK/gnuefi/crt0-efi-riscv64.S @@ -51,7 +51,7 @@ optional_header: .4byte _text_size - ImageBase // SizeOfCode .4byte _alldata_size - ImageBase // SizeOfInitializedData .4byte 0 // SizeOfUninitializedData - .4byte _start - ImageBase // AddressOfEntryPoint + .4byte __main - ImageBase // AddressOfEntryPoint .4byte _text - ImageBase // BaseOfCode extra_header_fields: @@ -151,9 +151,9 @@ section_table: .4byte 0x40000040 // Characteristics (section flags) .text - .globl _start - .type _start,%function -_start: + .globl __main + .type __main,%function +__main: addi sp, sp, -24 sd a0, 0(sp) sd a1, 8(sp) diff --git a/Private/efiSDK/gnuefi/crt0-efi-x86_64.S b/Private/efiSDK/gnuefi/crt0-efi-x86_64.S index b1f599fc..206c71e4 100644 --- a/Private/efiSDK/gnuefi/crt0-efi-x86_64.S +++ b/Private/efiSDK/gnuefi/crt0-efi-x86_64.S @@ -34,12 +34,13 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + .text .align 4 - .globl _start - .type _start,%function -_start: + .globl __main + +__main: subq $8, %rsp pushq %rcx pushq %rdx @@ -60,11 +61,11 @@ _start: call _entry addq $8, %rsp -.exit: +.exit: ret // hand-craft a dummy .reloc section so EFI knows it's a relocatable executable: - + .data dummy: .4byte 0 diff --git a/Private/efiSDK/gnuefi/elf_aarch64_efi.lds b/Private/efiSDK/gnuefi/elf_aarch64_efi.lds index 65b584d9..81f47ee2 100644 --- a/Private/efiSDK/gnuefi/elf_aarch64_efi.lds +++ b/Private/efiSDK/gnuefi/elf_aarch64_efi.lds @@ -1,6 +1,6 @@ OUTPUT_FORMAT("elf64-littleaarch64", "elf64-littleaarch64", "elf64-littleaarch64") OUTPUT_ARCH(aarch64) -ENTRY(_start) +ENTRY(__main) SECTIONS { . = 0; diff --git a/Private/efiSDK/gnuefi/elf_arm_efi.lds b/Private/efiSDK/gnuefi/elf_arm_efi.lds index ec09eb0f..050cf59a 100644 --- a/Private/efiSDK/gnuefi/elf_arm_efi.lds +++ b/Private/efiSDK/gnuefi/elf_arm_efi.lds @@ -1,6 +1,6 @@ OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm") OUTPUT_ARCH(arm) -ENTRY(_start) +ENTRY(__main) SECTIONS { .text 0 : { diff --git a/Private/efiSDK/gnuefi/elf_ia32_efi.lds b/Private/efiSDK/gnuefi/elf_ia32_efi.lds index 13614540..3d8284c8 100644 --- a/Private/efiSDK/gnuefi/elf_ia32_efi.lds +++ b/Private/efiSDK/gnuefi/elf_ia32_efi.lds @@ -1,6 +1,6 @@ OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") OUTPUT_ARCH(i386) -ENTRY(_start) +ENTRY(__main) SECTIONS { . = 0; diff --git a/Private/efiSDK/gnuefi/elf_ia32_fbsd_efi.lds b/Private/efiSDK/gnuefi/elf_ia32_fbsd_efi.lds index 650000cb..fed4a9e7 100644 --- a/Private/efiSDK/gnuefi/elf_ia32_fbsd_efi.lds +++ b/Private/efiSDK/gnuefi/elf_ia32_fbsd_efi.lds @@ -1,6 +1,6 @@ OUTPUT_FORMAT("elf32-i386-freebsd", "elf32-i386-freebsd", "elf32-i386-freebsd") OUTPUT_ARCH(i386) -ENTRY(_start) +ENTRY(__main) SECTIONS { . = 0; diff --git a/Private/efiSDK/gnuefi/elf_loongarch64_efi.lds b/Private/efiSDK/gnuefi/elf_loongarch64_efi.lds index d9de3bdf..8bddef75 100644 --- a/Private/efiSDK/gnuefi/elf_loongarch64_efi.lds +++ b/Private/efiSDK/gnuefi/elf_loongarch64_efi.lds @@ -1,6 +1,6 @@ OUTPUT_FORMAT("elf64-loongarch", "elf64-loongarch", "elf64-loongarch") OUTPUT_ARCH(loongarch) -ENTRY(_start) +ENTRY(__main) SECTIONS { .text 0x0 : { diff --git a/Private/efiSDK/gnuefi/elf_mips64el_efi.lds b/Private/efiSDK/gnuefi/elf_mips64el_efi.lds index a0d1a5b6..0c99d142 100644 --- a/Private/efiSDK/gnuefi/elf_mips64el_efi.lds +++ b/Private/efiSDK/gnuefi/elf_mips64el_efi.lds @@ -1,6 +1,6 @@ OUTPUT_FORMAT("elf64-tradlittlemips", "elf64-tradbigmips", "elf64-tradlittlemips") OUTPUT_ARCH(mips) -ENTRY(_start) +ENTRY(__main) SECTIONS { .text 0x0 : { diff --git a/Private/efiSDK/gnuefi/elf_riscv64_efi.lds b/Private/efiSDK/gnuefi/elf_riscv64_efi.lds index ac7055a5..e5a8d95a 100644 --- a/Private/efiSDK/gnuefi/elf_riscv64_efi.lds +++ b/Private/efiSDK/gnuefi/elf_riscv64_efi.lds @@ -2,7 +2,7 @@ OUTPUT_FORMAT("elf64-littleriscv", "elf64-littleriscv", "elf64-littleriscv") OUTPUT_ARCH(riscv) -ENTRY(_start) +ENTRY(__main) SECTIONS { .text 0 : { diff --git a/Private/efiSDK/gnuefi/elf_x86_64_efi.lds b/Private/efiSDK/gnuefi/elf_x86_64_efi.lds index e9f395e5..877532fe 100644 --- a/Private/efiSDK/gnuefi/elf_x86_64_efi.lds +++ b/Private/efiSDK/gnuefi/elf_x86_64_efi.lds @@ -1,7 +1,7 @@ /* Same as elf_x86_64_fbsd_efi.lds, except for OUTPUT_FORMAT below - KEEP IN SYNC */ OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", "elf64-x86-64") OUTPUT_ARCH(i386:x86-64) -ENTRY(_start) +ENTRY(__main) SECTIONS { . = 0; diff --git a/Private/efiSDK/gnuefi/elf_x86_64_fbsd_efi.lds b/Private/efiSDK/gnuefi/elf_x86_64_fbsd_efi.lds index a0dc6c0a..a2699935 100644 --- a/Private/efiSDK/gnuefi/elf_x86_64_fbsd_efi.lds +++ b/Private/efiSDK/gnuefi/elf_x86_64_fbsd_efi.lds @@ -1,7 +1,7 @@ /* Same as elf_x86_64_efi.lds, except for OUTPUT_FORMAT below - KEEP IN SYNC */ OUTPUT_FORMAT("elf64-x86-64-freebsd", "elf64-x86-64-freebsd", "elf64-x86-64-freebsd") OUTPUT_ARCH(i386:x86-64) -ENTRY(_start) +ENTRY(__main) SECTIONS { . = 0; diff --git a/Private/efiSDK/gnuefi/reloc_x86_64.c b/Private/efiSDK/gnuefi/reloc_x86_64.c index 04b75b29..e18aac29 100644 --- a/Private/efiSDK/gnuefi/reloc_x86_64.c +++ b/Private/efiSDK/gnuefi/reloc_x86_64.c @@ -1,8 +1,8 @@ /* reloc_x86_64.c - position independent x86_64 ELF shared object relocator Copyright (C) 1999 Hewlett-Packard Co. - Contributed by David Mosberger <davidm@hpl.hp.com>. + Contributed by David Mosberger <davidm@hpl.hp.com>. Copyright (C) 2005 Intel Co. - Contributed by Fenghua Yu <fenghua.yu@intel.com>. + Contributed by Fenghua Yu <fenghua.yu@intel.com>. All rights reserved. @@ -40,59 +40,58 @@ #include <elf.h> -EFI_STATUS _relocate (long ldbase, Elf64_Dyn *dyn, - EFI_HANDLE image EFI_UNUSED, - EFI_SYSTEM_TABLE *systab EFI_UNUSED) +EFI_STATUS _relocate(long ldbase, Elf64_Dyn *dyn, EFI_HANDLE image EFI_UNUSED, EFI_SYSTEM_TABLE *systab EFI_UNUSED) { - long relsz = 0, relent = 0; - Elf64_Rel *rel = 0; - unsigned long *addr; - int i; - - for (i = 0; dyn[i].d_tag != DT_NULL; ++i) { - switch (dyn[i].d_tag) { - case DT_RELA: - rel = (Elf64_Rel*) - ((unsigned long)dyn[i].d_un.d_ptr - + ldbase); - break; - - case DT_RELASZ: - relsz = dyn[i].d_un.d_val; - break; - - case DT_RELAENT: - relent = dyn[i].d_un.d_val; - break; - - default: - break; - } - } - - if (!rel && relent == 0) - return EFI_SUCCESS; - - if (!rel || relent == 0) - return EFI_LOAD_ERROR; - - while (relsz > 0) { - /* apply the relocs */ - switch (ELF64_R_TYPE (rel->r_info)) { - case R_X86_64_NONE: - break; - - case R_X86_64_RELATIVE: - addr = (unsigned long *) - (ldbase + rel->r_offset); - *addr += ldbase; - break; - - default: - break; - } - rel = (Elf64_Rel*) ((char *) rel + relent); - relsz -= relent; - } - return EFI_SUCCESS; + long relsz = 0, relent = 0; + Elf64_Rel *rel = 0; + unsigned long *addr; + int i; + + for (i = 0; dyn[i].d_tag != DT_NULL; ++i) + { + switch (dyn[i].d_tag) + { + case DT_RELA: + rel = (Elf64_Rel *)((unsigned long long int)dyn[i].d_un.d_ptr + ldbase); + break; + + case DT_RELASZ: + relsz = dyn[i].d_un.d_val; + break; + + case DT_RELAENT: + relent = dyn[i].d_un.d_val; + break; + + default: + break; + } + } + + if (!rel && relent == 0) + return EFI_SUCCESS; + + if (!rel || relent == 0) + return EFI_LOAD_ERROR; + + while (relsz > 0) + { + /* apply the relocs */ + switch (ELF64_R_TYPE(rel->r_info)) + { + case R_X86_64_NONE: + break; + + case R_X86_64_RELATIVE: + addr = (unsigned long *)(ldbase + rel->r_offset); + *addr += ldbase; + break; + + default: + break; + } + rel = (Elf64_Rel *)((char *)rel + relent); + relsz -= relent; + } + return EFI_SUCCESS; } diff --git a/Private/efiSDK/inc/elf.h b/Private/efiSDK/inc/elf.h new file mode 100644 index 00000000..edf82196 --- /dev/null +++ b/Private/efiSDK/inc/elf.h @@ -0,0 +1,546 @@ +/** +@brief Freestanding ELF header. +*/ + +#pragma once + +/* 32-bit ELF base types. */ +typedef unsigned Elf32_Addr; +typedef unsigned short Elf32_Half; +typedef unsigned Elf32_Off; +typedef unsigned Elf32_Sword; +typedef unsigned Elf32_Word; + +/* 64-bit ELF base types. */ +typedef unsigned long long int Elf64_Addr; +typedef unsigned short Elf64_Half; +typedef short Elf64_SHalf; +typedef unsigned long long int Elf64_Off; +typedef int Elf64_Sword; +typedef unsigned int Elf64_Word; +typedef unsigned long long int Elf64_Xword; +typedef long long int Elf64_Sxword; + +/* These constants are for the segment types stored in the image headers */ +#define PT_NULL 0 +#define PT_LOAD 1 +#define PT_DYNAMIC 2 +#define PT_INTERP 3 +#define PT_NOTE 4 +#define PT_SHLIB 5 +#define PT_PHDR 6 +#define PT_TLS 7 /* Thread local storage segment */ +#define PT_LOOS 0x60000000 /* OS-specific */ +#define PT_HIOS 0x6fffffff /* OS-specific */ +#define PT_LOPROC 0x70000000 +#define PT_HIPROC 0x7fffffff +#define PT_GNU_EH_FRAME (PT_LOOS + 0x474e550) +#define PT_GNU_STACK (PT_LOOS + 0x474e551) +#define PT_GNU_RELRO (PT_LOOS + 0x474e552) +#define PT_GNU_PROPERTY (PT_LOOS + 0x474e553) + +/* ARM MTE memory tag segment type */ +#define PT_AARCH64_MEMTAG_MTE (PT_LOPROC + 0x2) + +/* + * Extended Numbering + * + * If the real number of program header table entries is larger than + * or equal to PN_XNUM(0xffff), it is set to sh_info field of the + * section header at index 0, and PN_XNUM is set to e_phnum + * field. Otherwise, the section header at index 0 is zero + * initialized, if it exists. + * + * Specifications are available in: + * + * - Oracle: Linker and Libraries. + * Part No: 817–1984–19, August 2011. + * https://docs.oracle.com/cd/E18752_01/pdf/817-1984.pdf + * + * - System V ABI AMD64 Architecture Processor Supplement + * Draft Version 0.99.4, + * January 13, 2010. + * http://www.cs.washington.edu/education/courses/cse351/12wi/supp-docs/abi.pdf + */ +#define PN_XNUM 0xffff + +/* These constants define the different elf file types */ +#define ET_NONE 0 +#define ET_REL 1 +#define ET_EXEC 2 +#define ET_DYN 3 +#define ET_CORE 4 +#define ET_LOPROC 0xff00 +#define ET_HIPROC 0xffff + +/* This is the info that is needed to parse the dynamic section of the file */ +#define DT_NULL 0 +#define DT_NEEDED 1 +#define DT_PLTRELSZ 2 +#define DT_PLTGOT 3 +#define DT_HASH 4 +#define DT_STRTAB 5 +#define DT_SYMTAB 6 +#define DT_RELA 7 +#define DT_RELASZ 8 +#define DT_RELAENT 9 +#define DT_STRSZ 10 +#define DT_SYMENT 11 +#define DT_INIT 12 +#define DT_FINI 13 +#define DT_SONAME 14 +#define DT_RPATH 15 +#define DT_SYMBOLIC 16 +#define DT_REL 17 +#define DT_RELSZ 18 +#define DT_RELENT 19 +#define DT_PLTREL 20 +#define DT_DEBUG 21 +#define DT_TEXTREL 22 +#define DT_JMPREL 23 +#define DT_ENCODING 32 +#define OLD_DT_LOOS 0x60000000 +#define DT_LOOS 0x6000000d +#define DT_HIOS 0x6ffff000 +#define DT_VALRNGLO 0x6ffffd00 +#define DT_VALRNGHI 0x6ffffdff +#define DT_ADDRRNGLO 0x6ffffe00 +#define DT_ADDRRNGHI 0x6ffffeff +#define DT_VERSYM 0x6ffffff0 +#define DT_RELACOUNT 0x6ffffff9 +#define DT_RELCOUNT 0x6ffffffa +#define DT_FLAGS_1 0x6ffffffb +#define DT_VERDEF 0x6ffffffc +#define DT_VERDEFNUM 0x6ffffffd +#define DT_VERNEED 0x6ffffffe +#define DT_VERNEEDNUM 0x6fffffff +#define OLD_DT_HIOS 0x6fffffff +#define DT_LOPROC 0x70000000 +#define DT_HIPROC 0x7fffffff + +/* This info is needed when parsing the symbol table */ +#define STB_LOCAL 0 +#define STB_GLOBAL 1 +#define STB_WEAK 2 + +#define STT_NOTYPE 0 +#define STT_OBJECT 1 +#define STT_FUNC 2 +#define STT_SECTION 3 +#define STT_FILE 4 +#define STT_COMMON 5 +#define STT_TLS 6 + +#define ELF_ST_BIND(x) ((x) >> 4) +#define ELF_ST_TYPE(x) ((x) & 0xf) +#define ELF32_ST_BIND(x) ELF_ST_BIND(x) +#define ELF32_ST_TYPE(x) ELF_ST_TYPE(x) +#define ELF64_ST_BIND(x) ELF_ST_BIND(x) +#define ELF64_ST_TYPE(x) ELF_ST_TYPE(x) + +typedef struct +{ + Elf32_Sword d_tag; + union { + Elf32_Sword d_val; + Elf32_Addr d_ptr; + } d_un; +} Elf32_Dyn; + +typedef struct +{ + Elf64_Sxword d_tag; /* entry tag value */ + union { + Elf64_Xword d_val; + Elf64_Addr d_ptr; + } d_un; +} Elf64_Dyn; + +/* The following are used with relocations */ +#define ELF32_R_SYM(x) ((x) >> 8) +#define ELF32_R_TYPE(x) ((x) & 0xff) + +#define ELF64_R_SYM(i) ((i) >> 32) +#define ELF64_R_TYPE(i) ((i) & 0xffffffff) + +typedef struct elf32_rel +{ + Elf32_Addr r_offset; + Elf32_Word r_info; +} Elf32_Rel; + +typedef struct elf64_rel +{ + Elf64_Addr r_offset; /* Location at which to apply the action */ + Elf64_Xword r_info; /* index and type of relocation */ +} Elf64_Rel; + +typedef struct elf32_rela +{ + Elf32_Addr r_offset; + Elf32_Word r_info; + Elf32_Sword r_addend; +} Elf32_Rela; + +typedef struct elf64_rela +{ + Elf64_Addr r_offset; /* Location at which to apply the action */ + Elf64_Xword r_info; /* index and type of relocation */ + Elf64_Sxword r_addend; /* Constant addend used to compute value */ +} Elf64_Rela; + +typedef struct elf32_sym +{ + Elf32_Word st_name; + Elf32_Addr st_value; + Elf32_Word st_size; + unsigned char st_info; + unsigned char st_other; + Elf32_Half st_shndx; +} Elf32_Sym; + +typedef struct elf64_sym +{ + Elf64_Word st_name; /* Symbol name, index in string tbl */ + unsigned char st_info; /* Type and binding attributes */ + unsigned char st_other; /* No defined meaning, 0 */ + Elf64_Half st_shndx; /* Associated section index */ + Elf64_Addr st_value; /* Value of the symbol */ + Elf64_Xword st_size; /* Associated symbol size */ +} Elf64_Sym; + +#define EI_NIDENT 16 + +typedef struct elf32_hdr +{ + unsigned char e_ident[EI_NIDENT]; + Elf32_Half e_type; + Elf32_Half e_machine; + Elf32_Word e_version; + Elf32_Addr e_entry; /* Entry point */ + Elf32_Off e_phoff; + Elf32_Off e_shoff; + Elf32_Word e_flags; + Elf32_Half e_ehsize; + Elf32_Half e_phentsize; + Elf32_Half e_phnum; + Elf32_Half e_shentsize; + Elf32_Half e_shnum; + Elf32_Half e_shstrndx; +} Elf32_Ehdr; + +typedef struct elf64_hdr +{ + unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */ + Elf64_Half e_type; + Elf64_Half e_machine; + Elf64_Word e_version; + Elf64_Addr e_entry; /* Entry point virtual address */ + Elf64_Off e_phoff; /* Program header table file offset */ + Elf64_Off e_shoff; /* Section header table file offset */ + Elf64_Word e_flags; + Elf64_Half e_ehsize; + Elf64_Half e_phentsize; + Elf64_Half e_phnum; + Elf64_Half e_shentsize; + Elf64_Half e_shnum; + Elf64_Half e_shstrndx; +} Elf64_Ehdr; + +/* These constants define the permissions on sections in the program + header, p_flags. */ +#define PF_R 0x4 +#define PF_W 0x2 +#define PF_X 0x1 + +typedef struct elf32_phdr +{ + Elf32_Word p_type; + Elf32_Off p_offset; + Elf32_Addr p_vaddr; + Elf32_Addr p_paddr; + Elf32_Word p_filesz; + Elf32_Word p_memsz; + Elf32_Word p_flags; + Elf32_Word p_align; +} Elf32_Phdr; + +typedef struct elf64_phdr +{ + Elf64_Word p_type; + Elf64_Word p_flags; + Elf64_Off p_offset; /* Segment file offset */ + Elf64_Addr p_vaddr; /* Segment virtual address */ + Elf64_Addr p_paddr; /* Segment physical address */ + Elf64_Xword p_filesz; /* Segment size in file */ + Elf64_Xword p_memsz; /* Segment size in memory */ + Elf64_Xword p_align; /* Segment alignment, file & memory */ +} Elf64_Phdr; + +/* sh_type */ +#define SHT_NULL 0 +#define SHT_PROGBITS 1 +#define SHT_SYMTAB 2 +#define SHT_STRTAB 3 +#define SHT_RELA 4 +#define SHT_HASH 5 +#define SHT_DYNAMIC 6 +#define SHT_NOTE 7 +#define SHT_NOBITS 8 +#define SHT_REL 9 +#define SHT_SHLIB 10 +#define SHT_DYNSYM 11 +#define SHT_NUM 12 +#define SHT_LOPROC 0x70000000 +#define SHT_HIPROC 0x7fffffff +#define SHT_LOUSER 0x80000000 +#define SHT_HIUSER 0xffffffff + +/* sh_flags */ +#define SHF_WRITE 0x1 +#define SHF_ALLOC 0x2 +#define SHF_EXECINSTR 0x4 +#define SHF_RELA_LIVEPATCH 0x00100000 +#define SHF_RO_AFTER_INIT 0x00200000 +#define SHF_MASKPROC 0xf0000000 + +/* special section indexes */ +#define SHN_UNDEF 0 +#define SHN_LORESERVE 0xff00 +#define SHN_LOPROC 0xff00 +#define SHN_HIPROC 0xff1f +#define SHN_LIVEPATCH 0xff20 +#define SHN_ABS 0xfff1 +#define SHN_COMMON 0xfff2 +#define SHN_HIRESERVE 0xffff + +typedef struct elf32_shdr +{ + Elf32_Word sh_name; + Elf32_Word sh_type; + Elf32_Word sh_flags; + Elf32_Addr sh_addr; + Elf32_Off sh_offset; + Elf32_Word sh_size; + Elf32_Word sh_link; + Elf32_Word sh_info; + Elf32_Word sh_addralign; + Elf32_Word sh_entsize; +} Elf32_Shdr; + +typedef struct elf64_shdr +{ + Elf64_Word sh_name; /* Section name, index in string tbl */ + Elf64_Word sh_type; /* Type of section */ + Elf64_Xword sh_flags; /* Miscellaneous section attributes */ + Elf64_Addr sh_addr; /* Section virtual addr at execution */ + Elf64_Off sh_offset; /* Section file offset */ + Elf64_Xword sh_size; /* Size of section in bytes */ + Elf64_Word sh_link; /* Index of another section */ + Elf64_Word sh_info; /* Additional section information */ + Elf64_Xword sh_addralign; /* Section alignment */ + Elf64_Xword sh_entsize; /* Entry size if section holds table */ +} Elf64_Shdr; + +#define EI_MAG0 0 /* e_ident[] indexes */ +#define EI_MAG1 1 +#define EI_MAG2 2 +#define EI_MAG3 3 +#define EI_CLASS 4 +#define EI_DATA 5 +#define EI_VERSION 6 +#define EI_OSABI 7 +#define EI_PAD 8 + +#define ELFMAG0 0x7f /* EI_MAG */ +#define ELFMAG1 'E' +#define ELFMAG2 'L' +#define ELFMAG3 'F' +#define ELFMAG "\177ELF" +#define SELFMAG 4 + +#define ELFCLASSNONE 0 /* EI_CLASS */ +#define ELFCLASS32 1 +#define ELFCLASS64 2 +#define ELFCLASSNUM 3 + +#define ELFDATANONE 0 /* e_ident[EI_DATA] */ +#define ELFDATA2LSB 1 +#define ELFDATA2MSB 2 + +#define EV_NONE 0 /* e_version, EI_VERSION */ +#define EV_CURRENT 1 +#define EV_NUM 2 + +#define ELFOSABI_NONE 0 +#define ELFOSABI_LINUX 3 + +#ifndef ELF_OSABI +#define ELF_OSABI ELFOSABI_NONE +#endif + +/* + * Notes used in ET_CORE. Architectures export some of the arch register sets + * using the corresponding note types via the PTRACE_GETREGSET and + * PTRACE_SETREGSET requests. + * The note name for these types is "LINUX", except NT_PRFPREG that is named + * "CORE". + */ +#define NT_PRSTATUS 1 +#define NT_PRFPREG 2 +#define NT_PRPSINFO 3 +#define NT_TASKSTRUCT 4 +#define NT_AUXV 6 +/* + * Note to userspace developers: size of NT_SIGINFO note may increase + * in the future to accomodate more fields, don't assume it is fixed! + */ +#define NT_SIGINFO 0x53494749 +#define NT_FILE 0x46494c45 +#define NT_PRXFPREG 0x46e62b7f /* copied from gdb5.1/include/elf/common.h */ +#define NT_PPC_VMX 0x100 /* PowerPC Altivec/VMX registers */ +#define NT_PPC_SPE 0x101 /* PowerPC SPE/EVR registers */ +#define NT_PPC_VSX 0x102 /* PowerPC VSX registers */ +#define NT_PPC_TAR 0x103 /* Target Address Register */ +#define NT_PPC_PPR 0x104 /* Program Priority Register */ +#define NT_PPC_DSCR 0x105 /* Data Stream Control Register */ +#define NT_PPC_EBB 0x106 /* Event Based Branch Registers */ +#define NT_PPC_PMU 0x107 /* Performance Monitor Registers */ +#define NT_PPC_TM_CGPR 0x108 /* TM checkpointed GPR Registers */ +#define NT_PPC_TM_CFPR 0x109 /* TM checkpointed FPR Registers */ +#define NT_PPC_TM_CVMX 0x10a /* TM checkpointed VMX Registers */ +#define NT_PPC_TM_CVSX 0x10b /* TM checkpointed VSX Registers */ +#define NT_PPC_TM_SPR 0x10c /* TM Special Purpose Registers */ +#define NT_PPC_TM_CTAR 0x10d /* TM checkpointed Target Address Register */ +#define NT_PPC_TM_CPPR 0x10e /* TM checkpointed Program Priority Register */ +#define NT_PPC_TM_CDSCR 0x10f /* TM checkpointed Data Stream Control Register */ +#define NT_PPC_PKEY 0x110 /* Memory Protection Keys registers */ +#define NT_PPC_DEXCR 0x111 /* PowerPC DEXCR registers */ +#define NT_PPC_HASHKEYR 0x112 /* PowerPC HASHKEYR register */ +#define NT_386_TLS 0x200 /* i386 TLS slots (struct user_desc) */ +#define NT_386_IOPERM 0x201 /* x86 io permission bitmap (1=deny) */ +#define NT_X86_XSTATE 0x202 /* x86 extended state using xsave */ +/* Old binutils treats 0x203 as a CET state */ +#define NT_X86_SHSTK 0x204 /* x86 SHSTK state */ +#define NT_S390_HIGH_GPRS 0x300 /* s390 upper register halves */ +#define NT_S390_TIMER 0x301 /* s390 timer register */ +#define NT_S390_TODCMP 0x302 /* s390 TOD clock comparator register */ +#define NT_S390_TODPREG 0x303 /* s390 TOD programmable register */ +#define NT_S390_CTRS 0x304 /* s390 control registers */ +#define NT_S390_PREFIX 0x305 /* s390 prefix register */ +#define NT_S390_LAST_BREAK 0x306 /* s390 breaking event address */ +#define NT_S390_SYSTEM_CALL 0x307 /* s390 system call restart data */ +#define NT_S390_TDB 0x308 /* s390 transaction diagnostic block */ +#define NT_S390_VXRS_LOW 0x309 /* s390 vector registers 0-15 upper half */ +#define NT_S390_VXRS_HIGH 0x30a /* s390 vector registers 16-31 */ +#define NT_S390_GS_CB 0x30b /* s390 guarded storage registers */ +#define NT_S390_GS_BC 0x30c /* s390 guarded storage broadcast control block */ +#define NT_S390_RI_CB 0x30d /* s390 runtime instrumentation */ +#define NT_S390_PV_CPU_DATA 0x30e /* s390 protvirt cpu dump data */ +#define NT_ARM_VFP 0x400 /* ARM VFP/NEON registers */ +#define NT_ARM_TLS 0x401 /* ARM TLS register */ +#define NT_ARM_HW_BREAK 0x402 /* ARM hardware breakpoint registers */ +#define NT_ARM_HW_WATCH 0x403 /* ARM hardware watchpoint registers */ +#define NT_ARM_SYSTEM_CALL 0x404 /* ARM system call number */ +#define NT_ARM_SVE 0x405 /* ARM Scalable Vector Extension registers */ +#define NT_ARM_PAC_MASK 0x406 /* ARM pointer authentication code masks */ +#define NT_ARM_PACA_KEYS 0x407 /* ARM pointer authentication address keys */ +#define NT_ARM_PACG_KEYS 0x408 /* ARM pointer authentication generic key */ +#define NT_ARM_TAGGED_ADDR_CTRL 0x409 /* arm64 tagged address control (prctl()) */ +#define NT_ARM_PAC_ENABLED_KEYS 0x40a /* arm64 ptr auth enabled keys (prctl()) */ +#define NT_ARM_SSVE 0x40b /* ARM Streaming SVE registers */ +#define NT_ARM_ZA 0x40c /* ARM SME ZA registers */ +#define NT_ARM_ZT 0x40d /* ARM SME ZT registers */ +#define NT_ARC_V2 0x600 /* ARCv2 accumulator/extra registers */ +#define NT_VMCOREDD 0x700 /* Vmcore Device Dump Note */ +#define NT_MIPS_DSP 0x800 /* MIPS DSP ASE registers */ +#define NT_MIPS_FP_MODE 0x801 /* MIPS floating-point mode */ +#define NT_MIPS_MSA 0x802 /* MIPS SIMD registers */ +#define NT_RISCV_CSR 0x900 /* RISC-V Control and Status Registers */ +#define NT_RISCV_VECTOR 0x901 /* RISC-V vector registers */ +#define NT_LOONGARCH_CPUCFG 0xa00 /* LoongArch CPU config registers */ +#define NT_LOONGARCH_CSR 0xa01 /* LoongArch control and status registers */ +#define NT_LOONGARCH_LSX 0xa02 /* LoongArch Loongson SIMD Extension registers */ +#define NT_LOONGARCH_LASX 0xa03 /* LoongArch Loongson Advanced SIMD Extension registers */ +#define NT_LOONGARCH_LBT 0xa04 /* LoongArch Loongson Binary Translation registers */ +#define NT_LOONGARCH_HW_BREAK 0xa05 /* LoongArch hardware breakpoint registers */ +#define NT_LOONGARCH_HW_WATCH 0xa06 /* LoongArch hardware watchpoint registers */ + +/* Note types with note name "GNU" */ +#define NT_GNU_PROPERTY_TYPE_0 5 + +/* Note header in a PT_NOTE section */ +typedef struct elf32_note +{ + Elf32_Word n_namesz; /* Name size */ + Elf32_Word n_descsz; /* Content size */ + Elf32_Word n_type; /* Content type */ +} Elf32_Nhdr; + +/* Note header in a PT_NOTE section */ +typedef struct elf64_note +{ + Elf64_Word n_namesz; /* Name size */ + Elf64_Word n_descsz; /* Content size */ + Elf64_Word n_type; /* Content type */ +} Elf64_Nhdr; + +/* .note.gnu.property types for EM_AARCH64: */ +#define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000 + +/* Bits for GNU_PROPERTY_AARCH64_FEATURE_1_BTI */ +#define GNU_PROPERTY_AARCH64_FEATURE_1_BTI (1U << 0) + +#ifdef __i386__ + +#define R_386_NONE 0 +#define R_386_32 1 +#define R_386_PC32 2 +#define R_386_GOT32 3 +#define R_386_PLT32 4 +#define R_386_COPY 5 +#define R_386_GLOB_DAT 6 +#define R_386_JMP_SLOT 7 +#define R_386_RELATIVE 8 +#define R_386_GOTOFF 9 +#define R_386_GOTPC 10 +#define R_386_NUM 11 + +/* + * These are used to set parameters in the core dumps. + */ +#define ELF_CLASS ELFCLASS32 +#define ELF_DATA ELFDATA2LSB +#define ELF_ARCH EM_386 + +#else + +/* x86-64 relocation types */ +#define R_X86_64_NONE 0 /* No reloc */ +#define R_X86_64_64 1 /* Direct 64 bit */ +#define R_X86_64_PC32 2 /* PC relative 32 bit signed */ +#define R_X86_64_GOT32 3 /* 32 bit GOT entry */ +#define R_X86_64_PLT32 4 /* 32 bit PLT address */ +#define R_X86_64_COPY 5 /* Copy symbol at runtime */ +#define R_X86_64_GLOB_DAT 6 /* Create GOT entry */ +#define R_X86_64_JUMP_SLOT 7 /* Create PLT entry */ +#define R_X86_64_RELATIVE 8 /* Adjust by program base */ +#define R_X86_64_GOTPCREL \ + 9 /* 32 bit signed pc relative \ + offset to GOT */ +#define R_X86_64_32 10 /* Direct 32 bit zero extended */ +#define R_X86_64_32S 11 /* Direct 32 bit sign extended */ +#define R_X86_64_16 12 /* Direct 16 bit zero extended */ +#define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */ +#define R_X86_64_8 14 /* Direct 8 bit sign extended */ +#define R_X86_64_PC8 15 /* 8 bit sign extended pc relative */ +#define R_X86_64_PC64 24 /* Place relative 64-bit signed */ + +/* + * These are used to set parameters in the core dumps. + */ +#define ELF_CLASS ELFCLASS64 +#define ELF_DATA ELFDATA2LSB +#define ELF_ARCH EM_X86_64 + +#endif diff --git a/Private/efiSDK/lib/ctors.S b/Private/efiSDK/lib/ctors.S index 8aa36dcc..6afdf176 100644 --- a/Private/efiSDK/lib/ctors.S +++ b/Private/efiSDK/lib/ctors.S @@ -8,7 +8,9 @@ * end/END definitions, and the fact that they're mergeable, they can also * have NULLs which aren't guaranteed to be at the end. */ - .section .init_array,"aw",%init_array + +#if defined(__ELF__) && defined(__linux__) +.section .init_array,"aw",%init_array .p2align 3, 0 .globl __init_array_start __init_array_start: @@ -38,7 +40,5 @@ __fini_array_start: __fini_array_end: .p2align 3, 0 -#if defined(__ELF__) && defined(__linux__) .section .note.GNU-stack,"",%progbits #endif - |
