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
X27
X28
X29
X30
FP // Frame pointer
LR // Link register
SP // Stack pointer
)
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,
ProgramHeaderEntrySize: ProgramHeaderSize,
ProgramHeaderEntryCount: 2,
SectionHeaderEntrySize: SectionHeaderSize,
SectionHeaderEntrySize: 0,
SectionHeaderEntryCount: 0,
SectionNameStringTableIndex: 0,
},

View File

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

View File

@ -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)))

View File

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

View File

@ -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
}