From f0d43bbee5f03094981ca8e841393bad035defc0 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 9 Jul 2025 00:09:28 +0200 Subject: [PATCH] Removed unnecessary Mach-O segment --- src/macho/Arch.go | 4 ++-- src/macho/Constants.go | 3 ++- src/macho/MachO.go | 30 +++++------------------------- 3 files changed, 9 insertions(+), 28 deletions(-) diff --git a/src/macho/Arch.go b/src/macho/Arch.go index 24f00f6..144b315 100644 --- a/src/macho/Arch.go +++ b/src/macho/Arch.go @@ -6,9 +6,9 @@ import "git.urbach.dev/cli/q/src/build" func Arch(arch build.Arch) (CPU, uint32) { switch arch { case build.ARM: - return CPU_ARM_64, CPU_SUBTYPE_ARM64_ALL | 0x80000000 + return CPU_ARM_64, CPU_SUBTYPE_ARM64_ALL case build.X86: - return CPU_X86_64, CPU_SUBTYPE_X86_64_ALL | 0x80000000 + return CPU_X86_64, CPU_SUBTYPE_X86_64_ALL default: return 0, 0 } diff --git a/src/macho/Constants.go b/src/macho/Constants.go index be1a635..096b8e3 100644 --- a/src/macho/Constants.go +++ b/src/macho/Constants.go @@ -2,6 +2,7 @@ package macho const ( BaseAddress = 0x1000000 - SizeCommands = Segment64Size*4 + DyldInfoCommandSize + MainSize + DylinkerCommandSize + len(LinkerString) + DylibCommandSize + len(LibSystemString) + NumCommands = 7 + SizeCommands = Segment64Size*3 + DyldInfoCommandSize + MainSize + DylinkerCommandSize + len(LinkerString) + DylibCommandSize + len(LibSystemString) HeaderEnd = HeaderSize + SizeCommands ) \ No newline at end of file diff --git a/src/macho/MachO.go b/src/macho/MachO.go index 3b6f7af..a9c5aa5 100644 --- a/src/macho/MachO.go +++ b/src/macho/MachO.go @@ -14,7 +14,6 @@ type MachO struct { PageZero Segment64 CodeHeader Segment64 DataHeader Segment64 - ImportsHeader Segment64 MainHeader Main InfoHeader DyldInfoCommand LinkerHeader DylinkerCommand @@ -23,10 +22,9 @@ type MachO struct { // Write writes the Mach-O format to the given writer. func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes []byte) { - x := exe.New(HeaderEnd, b.FileAlign(), b.MemoryAlign(), b.Congruent(), codeBytes, dataBytes, nil) + x := exe.New(HeaderEnd, b.FileAlign(), b.MemoryAlign(), b.Congruent(), codeBytes, dataBytes) code := x.Sections[0] data := x.Sections[1] - imports := x.Sections[2] arch, microArch := Arch(b.Arch) m := &MachO{ @@ -35,9 +33,9 @@ func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes [] Architecture: arch, MicroArchitecture: microArch, Type: TypeExecute, - NumCommands: 8, + NumCommands: NumCommands, SizeCommands: uint32(SizeCommands), - Flags: FlagNoUndefs | FlagPIE | FlagNoHeapExecution, + Flags: FlagNoUndefs | FlagDyldLink | FlagTwoLevel | FlagPIE | FlagNoHeapExecution, Reserved: 0, }, PageZero: Segment64{ @@ -79,19 +77,6 @@ func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes [] MaxProt: ProtReadable, InitProt: ProtReadable, }, - ImportsHeader: Segment64{ - LoadCommand: LcSegment64, - Length: Segment64Size, - Name: [16]byte{'_', '_', 'L', 'I', 'N', 'K', 'E', 'D', 'I', 'T'}, - Address: uint64(BaseAddress + imports.MemoryOffset), - SizeInMemory: uint64(len(imports.Bytes)), - Offset: uint64(imports.FileOffset), - SizeInFile: uint64(len(imports.Bytes)), - NumSections: 0, - Flag: 0, - MaxProt: ProtReadable, - InitProt: ProtReadable, - }, MainHeader: Main{ LoadCommand: LcMain, Length: MainSize, @@ -99,10 +84,8 @@ func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes [] StackSize: 0, }, InfoHeader: DyldInfoCommand{ - LoadCommand: LcDyldInfoOnly, - Length: DyldInfoCommandSize, - RebaseOffset: uint32(imports.FileOffset), - RebaseSize: uint32(len(imports.Bytes)), + LoadCommand: LcDyldInfoOnly, + Length: DyldInfoCommandSize, }, LinkerHeader: DylinkerCommand{ LoadCommand: LcLoadDylinker, @@ -120,7 +103,6 @@ func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes [] 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.ImportsHeader) binary.Write(writer, binary.LittleEndian, &m.MainHeader) binary.Write(writer, binary.LittleEndian, &m.InfoHeader) binary.Write(writer, binary.LittleEndian, &m.LinkerHeader) @@ -131,6 +113,4 @@ func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes [] writer.Write(code.Bytes) writer.Seek(int64(data.Padding), io.SeekCurrent) writer.Write(data.Bytes) - writer.Seek(int64(imports.Padding), io.SeekCurrent) - writer.Write(imports.Bytes) } \ No newline at end of file