From fe1b353fe68e0c9b3b48c0fb0da08cea3d564a14 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 14 Aug 2024 22:21:39 +0200 Subject: [PATCH] Reduced number of load commands --- src/arch/arm64/Registers.go | 5 ++- src/os/linux/elf/ELF.go | 2 +- src/os/mac/macho/Header.go | 2 + src/os/mac/macho/MachO.go | 79 ++++++++++++++--------------------- src/os/mac/macho/Segment64.go | 2 + src/os/mac/macho/Thread.go | 3 ++ 6 files changed, 43 insertions(+), 50 deletions(-) diff --git a/src/arch/arm64/Registers.go b/src/arch/arm64/Registers.go index cf8c5db..088e2b3 100644 --- a/src/arch/arm64/Registers.go +++ b/src/arch/arm64/Registers.go @@ -32,8 +32,9 @@ const ( X26 X27 X28 - X29 - X30 + FP // Frame pointer + LR // Link register + SP // Stack pointer ) var SyscallInputRegisters = []cpu.Register{X8, X0, X1, X2, X3, X4, X5} diff --git a/src/os/linux/elf/ELF.go b/src/os/linux/elf/ELF.go index 87818dc..fb8b517 100644 --- a/src/os/linux/elf/ELF.go +++ b/src/os/linux/elf/ELF.go @@ -45,7 +45,7 @@ func New(code []byte, data []byte) *ELF { Size: HeaderSize, ProgramHeaderEntrySize: ProgramHeaderSize, ProgramHeaderEntryCount: 2, - SectionHeaderEntrySize: SectionHeaderSize, + SectionHeaderEntrySize: 0, SectionHeaderEntryCount: 0, SectionNameStringTableIndex: 0, }, diff --git a/src/os/mac/macho/Header.go b/src/os/mac/macho/Header.go index f7798be..21c0c54 100644 --- a/src/os/mac/macho/Header.go +++ b/src/os/mac/macho/Header.go @@ -1,5 +1,7 @@ package macho +const HeaderSize = 32 + // Header contains general information. type Header struct { Magic uint32 diff --git a/src/os/mac/macho/MachO.go b/src/os/mac/macho/MachO.go index d48281c..a96e37b 100644 --- a/src/os/mac/macho/MachO.go +++ b/src/os/mac/macho/MachO.go @@ -24,9 +24,9 @@ func New(code []byte, data []byte) *MachO { Architecture: CPU_X86_64, MicroArchitecture: 3 | 0x80000000, Type: TypeExecute, - NumCommands: 5, - SizeCommands: 72*4 + 184, - Flags: FlagNoUndefs, + NumCommands: 4, + SizeCommands: Segment64Size*3 + ThreadSize, + Flags: FlagNoUndefs | FlagNoHeapExecution, Reserved: 0, }, Code: code, @@ -52,33 +52,19 @@ func (m *MachO) Write(writer io.Writer) { InitProt: 0, }) - codePadding := common.Padding(32+m.Header.SizeCommands, config.Align) + codePadding := common.Padding(HeaderSize+m.Header.SizeCommands, config.Align) codeEnd := uint64(config.CodeOffset + len(m.Code)) dataPadding := common.Padding(codeEnd, config.Align) dataStart := codeEnd + dataPadding - binary.Write(writer, binary.LittleEndian, &Segment64{ - LoadCommand: LcSegment64, - Length: 72, - Name: [16]byte{'_', '_', 'H', 'E', 'A', 'D'}, - Address: config.BaseAddress, - SizeInMemory: config.CodeOffset, - Offset: 0, - SizeInFile: config.CodeOffset, - NumSections: 0, - Flag: 0, - MaxProt: ProtReadable | ProtExecutable, - InitProt: ProtReadable | ProtExecutable, - }) - binary.Write(writer, binary.LittleEndian, &Segment64{ LoadCommand: LcSegment64, Length: 72, Name: [16]byte{'_', '_', 'T', 'E', 'X', 'T'}, - Address: config.BaseAddress + config.CodeOffset, - SizeInMemory: uint64(len(m.Code)), - Offset: config.CodeOffset, - SizeInFile: uint64(len(m.Code)), + Address: config.BaseAddress, + SizeInMemory: config.CodeOffset + uint64(len(m.Code)), + Offset: 0, + SizeInFile: config.CodeOffset + uint64(len(m.Code)), NumSections: 0, Flag: 0, MaxProt: ProtReadable | ProtExecutable, @@ -103,31 +89,30 @@ func (m *MachO) Write(writer io.Writer) { LoadCommand: LcUnixthread, Len: 184, Type: 0x4, - }) - - binary.Write(writer, binary.LittleEndian, []uint32{ - 42, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - config.BaseAddress + config.CodeOffset, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, + Data: [43]uint32{ + 42, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + config.BaseAddress + config.CodeOffset, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + }, }) writer.Write(bytes.Repeat([]byte{0}, int(codePadding))) diff --git a/src/os/mac/macho/Segment64.go b/src/os/mac/macho/Segment64.go index 382e18f..1f611f0 100644 --- a/src/os/mac/macho/Segment64.go +++ b/src/os/mac/macho/Segment64.go @@ -1,5 +1,7 @@ package macho +const Segment64Size = 72 + // Segment64 is a segment load command. type Segment64 struct { LoadCommand diff --git a/src/os/mac/macho/Thread.go b/src/os/mac/macho/Thread.go index 69f9053..004c196 100644 --- a/src/os/mac/macho/Thread.go +++ b/src/os/mac/macho/Thread.go @@ -1,8 +1,11 @@ package macho +const ThreadSize = 184 + // Thread is a thread state load command. type Thread struct { LoadCommand Len uint32 Type uint32 + Data [43]uint32 }