From 4a1f2ba3107266074e2f0bb958fa7a3668a01931 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Sat, 9 Dec 2023 19:54:44 +0000 Subject: [PATCH] Reduce size of hex0 by 3 bytes. --- amd64/Development/hex0.M1 | 13 +++++-------- amd64/Development/hex0.S | 13 +++++-------- amd64/Development/hex0.hex2 | 13 +++++-------- amd64/hex0.hex0 | 29 +++++++++++++---------------- 4 files changed, 28 insertions(+), 40 deletions(-) diff --git a/amd64/Development/hex0.M1 b/amd64/Development/hex0.M1 index 41745bf..530553d 100644 --- a/amd64/Development/hex0.M1 +++ b/amd64/Development/hex0.M1 @@ -320,25 +320,22 @@ DEFINE xor_r9,r9 4D31C9 ret # return :read_byte - push_rsp # align stack to 16 bytes - push_[rsp] # align stack to 16 bytes - and_rsp, !-16 # align stack to 16 bytes - push_rax # make sure next call is aligned to 16 bytes mov_rcx,r12 # arg1 = fin push !1 # size = 1 mov_rdx,rsp # arg2 = &size push !0 # allocate stack mov_r8,rsp # arg3 = &input + push_rsp # align stack to 16 bytes + push_[rsp] # align stack to 16 bytes + and_rsp, !-16 # align stack to 16 bytes + push_rax # make sure next call is aligned to 16 bytes push_rax # allocate shadow stack space for UEFI function push_rax # allocate shadow stack space for UEFI function push_rax # allocate shadow stack space for UEFI function call_[rcx+BYTE] !32 # fin->read() - pop_rax # deallocate stack - pop_rax # deallocate stack - pop_rax # deallocate stack + mov_rsp,[rsp+BYTE] !40 # deallocate stack pop_rax # save input to rax pop_rsi # save size to rsi - mov_rsp,[rsp+BYTE] !16 # deallocate stack # If the file ended (0 bytes read) terminate test_esi,esi # if size = 0 diff --git a/amd64/Development/hex0.S b/amd64/Development/hex0.S index ccb33ce..919f1b7 100644 --- a/amd64/Development/hex0.S +++ b/amd64/Development/hex0.S @@ -243,26 +243,23 @@ write_byte: ret # return read_byte: - push rsp # align stack to 16 bytes - push [rsp] # align stack to 16 bytes - and rsp, -16 # align stack to 16 bytes - push rax # make sure next call is aligned to 16 bytes mov rcx, r12 # arg1 = fin push 1 # size = 1 mov rdx, rsp # arg2 = &size xor esi, esi # zero rsi push rsi # allocate stack mov r8, rsp # arg3 = &input + push rsp # align stack to 16 bytes + push [rsp] # align stack to 16 bytes + and rsp, -16 # align stack to 16 bytes + push rax # make sure next call is aligned to 16 bytes push rax # allocate shadow stack space for UEFI function push rax # allocate shadow stack space for UEFI function push rax # allocate shadow stack space for UEFI function call [rcx+32] # fin->read() - pop rax # deallocate stack - pop rax # deallocate stack - pop rax # deallocate stack + mov rsp, [rsp+40] # deallocate stack pop rax # save input to rax pop rsi # save size to rsi - mov rsp, [rsp+16] # deallocate stack # If the file ended (0 bytes read) terminate test esi, esi # if size == 0 diff --git a/amd64/Development/hex0.hex2 b/amd64/Development/hex0.hex2 index 8aa0f4d..34778c8 100644 --- a/amd64/Development/hex0.hex2 +++ b/amd64/Development/hex0.hex2 @@ -239,25 +239,22 @@ C3 ; ret # return :read_byte - 54 ; push_rsp # align stack to 16 bytes - FF3424 ; push_[rsp] # align stack to 16 bytes - 4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes - 50 ; push_rax # make sure next call is aligned to 16 bytes 4C89E1 ; mov_rcx,r12 # arg1 = fin 6A 01 ; push !1 # size = 1 4889E2 ; mov_rdx,rsp # arg2 = &size 6A 00 ; push !0 # allocate stack 4989E0 ; mov_r8,rsp # arg3 = &input + 54 ; push_rsp # align stack to 16 bytes + FF3424 ; push_[rsp] # align stack to 16 bytes + 4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes + 50 ; push_rax # make sure next call is aligned to 16 bytes 50 ; push_rax # allocate shadow stack space for UEFI function 50 ; push_rax # allocate shadow stack space for UEFI function 50 ; push_rax # allocate shadow stack space for UEFI function FF51 20 ; call_[rcx+BYTE] !32 # fin->read() - 58 ; pop_rax # deallocate stack - 58 ; pop_rax # deallocate stack - 58 ; pop_rax # deallocate stack + 488B6424 10 ; mov_rsp,[rsp+BYTE] !40 # deallocate stack 58 ; pop_rax # save input to rax 5E ; pop_rsi # save size to rsi - 488B6424 10 ; mov_rsp,[rsp+BYTE] !16 # deallocate stack # If the file ended (0 bytes read) terminate 85F6 ; test_esi,esi # if size = 0 diff --git a/amd64/hex0.hex0 b/amd64/hex0.hex0 index 4cbb8a3..d26cd69 100644 --- a/amd64/hex0.hex0 +++ b/amd64/hex0.hex0 @@ -129,9 +129,9 @@ F0 00 # SizeOfOptionalHeader # [0x148] # Start of section headers 00 00 00 00 00 00 00 00 ; Name of the section (empty) but could set to ".text" -09 02 00 00 ; VirtualSize +06 02 00 00 ; VirtualSize 00 10 00 00 ; VirtualAddress -09 02 00 00 ; SizeOfRawData +06 02 00 00 ; SizeOfRawData 70 01 00 00 ; PointerToRawData 00 00 00 00 ; PointerToRelocations 00 00 00 00 ; PointerToLinenumbers @@ -159,7 +159,7 @@ F0 00 # SizeOfOptionalHeader # Open Loaded Image protocol 4D89F9 ; mov_r9,r15 # arg4 = image_handle - 488D15 C9010000 ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL + 488D15 C6010000 ; lea_rdx,[rip+DWORD] %LOADED_IMAGE_PROTOCOL # guid = &LOADED_IMAGE_PROTOCOL 4C89C9 ; mov_rcx,r9 # arg1 = image_handle 50 ; push_rax # allocate stack for image 4989E0 ; mov_r8,rsp # arg3 = &image @@ -172,7 +172,7 @@ F0 00 # SizeOfOptionalHeader # Get root file system 4D89F9 ; mov_r9,r15 # arg4 = image_handle - 488D15 B4010000 ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL + 488D15 B1010000 ; lea_rdx,[rip+DWORD] %SIMPLE_FS_PROTOCOL # guid = &SIMPLE_FS_PROTOCOL 488B4F 18 ; mov_rcx,[rdi+BYTE] !24 # arg1 = root_device = image->device 4989CD ; mov_r13,rcx # save root_device 50 ; push_rax # allocate stack for rootfs @@ -368,25 +368,22 @@ F0 00 # SizeOfOptionalHeader C3 ; ret # return # :read_byte [_start+0x160] - 54 ; push_rsp # align stack to 16 bytes - FF3424 ; push_[rsp] # align stack to 16 bytes - 4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes - 50 ; push_rax # make sure next call is aligned to 16 bytes 4C89E1 ; mov_rcx,r12 # arg1 = fin 6A 01 ; push !1 # size = 1 4889E2 ; mov_rdx,rsp # arg2 = &size 6A 00 ; push_rsi # allocate stack 4989E0 ; mov_r8,rsp # arg3 = &input + 54 ; push_rsp # align stack to 16 bytes + FF3424 ; push_[rsp] # align stack to 16 bytes + 4883E4 F0 ; and_rsp, !-16 # align stack to 16 bytes + 50 ; push_rax # make sure next call is aligned to 16 bytes 50 ; push_rax # allocate shadow stack space for UEFI function 50 ; push_rax # allocate shadow stack space for UEFI function 50 ; push_rax # allocate shadow stack space for UEFI function FF51 20 ; call_[rcx+BYTE] !32 # fin->read() - 58 ; pop_rax # deallocate stack - 58 ; pop_rax # deallocate stack - 58 ; pop_rax # deallocate stack + 488B6424 28 ; mov_rsp,[rsp+BYTE] !40 # deallocate stack 58 ; pop_rax # save input to rax 5E ; pop_rsi # save size to rsi - 488B6424 10 ; mov_rsp,[rsp+BYTE] !16 # deallocate stack # If the file ended (0 bytes read) terminate 85F6 ; test_esi,esi # if size = 0 @@ -394,7 +391,7 @@ F0 00 # SizeOfOptionalHeader C3 ; ret # return -# :terminate [_start+0x18B] +# :terminate [_start+0x188] 4C89FC ; mov_rsp,r15 # restore stack location for cleanup 5E ; pop_rsi # restore rootdir 415F ; pop_r15 # restore image_handle @@ -437,16 +434,16 @@ F0 00 # SizeOfOptionalHeader # Protocol GUIDs -# :LOADED_IMAGE_PROTOCOL [_start+0x1E9] +# :LOADED_IMAGE_PROTOCOL [_start+0x1E6] A1 31 1B 5B ; %0x5b1b31a1 62 95 ; $0x9562 D2 11 ; $0x11d2 8E 3F 00 A0 C9 69 72 3B ; !0x8e !0x3f !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b -# :SIMPLE_FS_PROTOCOL [_start+0x1F9] +# :SIMPLE_FS_PROTOCOL [_start+0x1F6] 22 5B 4E 96 ; %0x964e5b22 59 64 ; $0x6459 D2 11 ; $0x11d2 8E 39 00 A0 C9 69 72 3B ; !0x8e !0x39 !0 !0xa0 !0xc9 !0x69 !0x72 !0x3b -# :ELF_end [_start+0x209] +# :ELF_end [_start+0x206]