This commit is contained in:
parent
2bd5b81ffb
commit
f0d43bbee5
3 changed files with 9 additions and 28 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
)
|
|
@ -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,
|
||||
|
@ -101,8 +86,6 @@ func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes []
|
|||
InfoHeader: DyldInfoCommand{
|
||||
LoadCommand: LcDyldInfoOnly,
|
||||
Length: DyldInfoCommandSize,
|
||||
RebaseOffset: uint32(imports.FileOffset),
|
||||
RebaseSize: uint32(len(imports.Bytes)),
|
||||
},
|
||||
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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue