Reduced number of load commands

This commit is contained in:
Eduard Urbach 2024-08-14 22:21:39 +02:00
parent 35eeb420e1
commit fe1b353fe6
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
6 changed files with 43 additions and 50 deletions

View File

@ -32,8 +32,9 @@ const (
X26 X26
X27 X27
X28 X28
X29 FP // Frame pointer
X30 LR // Link register
SP // Stack pointer
) )
var SyscallInputRegisters = []cpu.Register{X8, X0, X1, X2, X3, X4, X5} var SyscallInputRegisters = []cpu.Register{X8, X0, X1, X2, X3, X4, X5}

View File

@ -45,7 +45,7 @@ func New(code []byte, data []byte) *ELF {
Size: HeaderSize, Size: HeaderSize,
ProgramHeaderEntrySize: ProgramHeaderSize, ProgramHeaderEntrySize: ProgramHeaderSize,
ProgramHeaderEntryCount: 2, ProgramHeaderEntryCount: 2,
SectionHeaderEntrySize: SectionHeaderSize, SectionHeaderEntrySize: 0,
SectionHeaderEntryCount: 0, SectionHeaderEntryCount: 0,
SectionNameStringTableIndex: 0, SectionNameStringTableIndex: 0,
}, },

View File

@ -1,5 +1,7 @@
package macho package macho
const HeaderSize = 32
// Header contains general information. // Header contains general information.
type Header struct { type Header struct {
Magic uint32 Magic uint32

View File

@ -24,9 +24,9 @@ func New(code []byte, data []byte) *MachO {
Architecture: CPU_X86_64, Architecture: CPU_X86_64,
MicroArchitecture: 3 | 0x80000000, MicroArchitecture: 3 | 0x80000000,
Type: TypeExecute, Type: TypeExecute,
NumCommands: 5, NumCommands: 4,
SizeCommands: 72*4 + 184, SizeCommands: Segment64Size*3 + ThreadSize,
Flags: FlagNoUndefs, Flags: FlagNoUndefs | FlagNoHeapExecution,
Reserved: 0, Reserved: 0,
}, },
Code: code, Code: code,
@ -52,33 +52,19 @@ func (m *MachO) Write(writer io.Writer) {
InitProt: 0, 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)) codeEnd := uint64(config.CodeOffset + len(m.Code))
dataPadding := common.Padding(codeEnd, config.Align) dataPadding := common.Padding(codeEnd, config.Align)
dataStart := codeEnd + dataPadding 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{ binary.Write(writer, binary.LittleEndian, &Segment64{
LoadCommand: LcSegment64, LoadCommand: LcSegment64,
Length: 72, Length: 72,
Name: [16]byte{'_', '_', 'T', 'E', 'X', 'T'}, Name: [16]byte{'_', '_', 'T', 'E', 'X', 'T'},
Address: config.BaseAddress + config.CodeOffset, Address: config.BaseAddress,
SizeInMemory: uint64(len(m.Code)), SizeInMemory: config.CodeOffset + uint64(len(m.Code)),
Offset: config.CodeOffset, Offset: 0,
SizeInFile: uint64(len(m.Code)), SizeInFile: config.CodeOffset + uint64(len(m.Code)),
NumSections: 0, NumSections: 0,
Flag: 0, Flag: 0,
MaxProt: ProtReadable | ProtExecutable, MaxProt: ProtReadable | ProtExecutable,
@ -103,31 +89,30 @@ func (m *MachO) Write(writer io.Writer) {
LoadCommand: LcUnixthread, LoadCommand: LcUnixthread,
Len: 184, Len: 184,
Type: 0x4, Type: 0x4,
}) Data: [43]uint32{
42,
binary.Write(writer, binary.LittleEndian, []uint32{ 0, 0,
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, 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,
config.BaseAddress + config.CodeOffset, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, },
0, 0,
}) })
writer.Write(bytes.Repeat([]byte{0}, int(codePadding))) writer.Write(bytes.Repeat([]byte{0}, int(codePadding)))

View File

@ -1,5 +1,7 @@
package macho package macho
const Segment64Size = 72
// Segment64 is a segment load command. // Segment64 is a segment load command.
type Segment64 struct { type Segment64 struct {
LoadCommand LoadCommand

View File

@ -1,8 +1,11 @@
package macho package macho
const ThreadSize = 184
// Thread is a thread state load command. // Thread is a thread state load command.
type Thread struct { type Thread struct {
LoadCommand LoadCommand
Len uint32 Len uint32
Type uint32 Type uint32
Data [43]uint32
} }