diff --git a/src/macho/Constants.go b/src/macho/Constants.go index 096b8e3..a24abf6 100644 --- a/src/macho/Constants.go +++ b/src/macho/Constants.go @@ -3,6 +3,6 @@ package macho const ( BaseAddress = 0x1000000 NumCommands = 7 - SizeCommands = Segment64Size*3 + DyldInfoCommandSize + MainSize + DylinkerCommandSize + len(LinkerString) + DylibCommandSize + len(LibSystemString) + SizeCommands = Segment64Size*3 + Section64Size + DyldInfoCommandSize + MainSize + DylinkerCommandSize + len(LinkerString) + DylibCommandSize + len(LibSystemString) HeaderEnd = HeaderSize + SizeCommands ) \ No newline at end of file diff --git a/src/macho/Header.go b/src/macho/Header.go index 1d9b9cc..d8a9e82 100644 --- a/src/macho/Header.go +++ b/src/macho/Header.go @@ -11,5 +11,5 @@ type Header struct { NumCommands uint32 SizeCommands uint32 Flags HeaderFlags - Reserved uint32 + _ uint32 } \ No newline at end of file diff --git a/src/macho/MachO.go b/src/macho/MachO.go index a9c5aa5..4c2a69d 100644 --- a/src/macho/MachO.go +++ b/src/macho/MachO.go @@ -12,8 +12,9 @@ import ( type MachO struct { Header PageZero Segment64 - CodeHeader Segment64 - DataHeader Segment64 + CodeSegment Segment64 + CodeSection Section64 + DataSegment Segment64 MainHeader Main InfoHeader DyldInfoCommand LinkerHeader DylinkerCommand @@ -36,7 +37,6 @@ func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes [] NumCommands: NumCommands, SizeCommands: uint32(SizeCommands), Flags: FlagNoUndefs | FlagDyldLink | FlagTwoLevel | FlagPIE | FlagNoHeapExecution, - Reserved: 0, }, PageZero: Segment64{ LoadCommand: LcSegment64, @@ -51,20 +51,29 @@ func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes [] MaxProt: 0, InitProt: 0, }, - CodeHeader: Segment64{ + CodeSegment: Segment64{ LoadCommand: LcSegment64, - Length: Segment64Size, + Length: Segment64Size + Section64Size, Name: [16]byte{'_', '_', 'T', 'E', 'X', 'T'}, Address: uint64(BaseAddress), SizeInMemory: uint64(code.MemoryOffset + len(code.Bytes)), Offset: 0, SizeInFile: uint64(code.FileOffset + len(code.Bytes)), - NumSections: 0, + NumSections: 1, Flag: 0, MaxProt: ProtReadable | ProtExecutable, InitProt: ProtReadable | ProtExecutable, }, - DataHeader: Segment64{ + CodeSection: Section64{ + SectionName: [16]byte{'_', '_', 't', 'e', 'x', 't'}, + SegmentName: [16]byte{'_', '_', 'T', 'E', 'X', 'T'}, + Address: uint64(BaseAddress + code.MemoryOffset), + Size: uint64(len(code.Bytes)), + Offset: uint32(code.FileOffset), + Align: 6, + Flags: FlagPureInstructions, + }, + DataSegment: Segment64{ LoadCommand: LcSegment64, Length: Segment64Size, Name: [16]byte{'_', '_', 'D', 'A', 'T', 'A', '_', 'C', 'O', 'N', 'S', 'T'}, @@ -101,8 +110,9 @@ func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes [] binary.Write(writer, binary.LittleEndian, &m.Header) binary.Write(writer, binary.LittleEndian, &m.PageZero) - binary.Write(writer, binary.LittleEndian, &m.CodeHeader) - binary.Write(writer, binary.LittleEndian, &m.DataHeader) + binary.Write(writer, binary.LittleEndian, &m.CodeSegment) + binary.Write(writer, binary.LittleEndian, &m.CodeSection) + binary.Write(writer, binary.LittleEndian, &m.DataSegment) binary.Write(writer, binary.LittleEndian, &m.MainHeader) binary.Write(writer, binary.LittleEndian, &m.InfoHeader) binary.Write(writer, binary.LittleEndian, &m.LinkerHeader) diff --git a/src/macho/Section64.go b/src/macho/Section64.go new file mode 100644 index 0000000..fcc1a9c --- /dev/null +++ b/src/macho/Section64.go @@ -0,0 +1,19 @@ +package macho + +const Section64Size = 80 + +// Section64 is one of multiple sections in a segment. +type Section64 struct { + SectionName [16]byte + SegmentName [16]byte + Address uint64 + Size uint64 + Offset uint32 + Align uint32 + RelocationOffset uint32 + NumRelocations uint32 + Flags SectionFlags + _ uint32 + _ uint32 + _ uint32 +} \ No newline at end of file diff --git a/src/macho/SectionFlags.go b/src/macho/SectionFlags.go new file mode 100644 index 0000000..c52210c --- /dev/null +++ b/src/macho/SectionFlags.go @@ -0,0 +1,7 @@ +package macho + +type SectionFlags uint32 + +const ( + FlagPureInstructions SectionFlags = 0x80000000 +) \ No newline at end of file