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) {
|
func Arch(arch build.Arch) (CPU, uint32) {
|
||||||
switch arch {
|
switch arch {
|
||||||
case build.ARM:
|
case build.ARM:
|
||||||
return CPU_ARM_64, CPU_SUBTYPE_ARM64_ALL | 0x80000000
|
return CPU_ARM_64, CPU_SUBTYPE_ARM64_ALL
|
||||||
case build.X86:
|
case build.X86:
|
||||||
return CPU_X86_64, CPU_SUBTYPE_X86_64_ALL | 0x80000000
|
return CPU_X86_64, CPU_SUBTYPE_X86_64_ALL
|
||||||
default:
|
default:
|
||||||
return 0, 0
|
return 0, 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package macho
|
||||||
|
|
||||||
const (
|
const (
|
||||||
BaseAddress = 0x1000000
|
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
|
HeaderEnd = HeaderSize + SizeCommands
|
||||||
)
|
)
|
|
@ -14,7 +14,6 @@ type MachO struct {
|
||||||
PageZero Segment64
|
PageZero Segment64
|
||||||
CodeHeader Segment64
|
CodeHeader Segment64
|
||||||
DataHeader Segment64
|
DataHeader Segment64
|
||||||
ImportsHeader Segment64
|
|
||||||
MainHeader Main
|
MainHeader Main
|
||||||
InfoHeader DyldInfoCommand
|
InfoHeader DyldInfoCommand
|
||||||
LinkerHeader DylinkerCommand
|
LinkerHeader DylinkerCommand
|
||||||
|
@ -23,10 +22,9 @@ type MachO struct {
|
||||||
|
|
||||||
// Write writes the Mach-O format to the given writer.
|
// Write writes the Mach-O format to the given writer.
|
||||||
func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes []byte) {
|
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]
|
code := x.Sections[0]
|
||||||
data := x.Sections[1]
|
data := x.Sections[1]
|
||||||
imports := x.Sections[2]
|
|
||||||
arch, microArch := Arch(b.Arch)
|
arch, microArch := Arch(b.Arch)
|
||||||
|
|
||||||
m := &MachO{
|
m := &MachO{
|
||||||
|
@ -35,9 +33,9 @@ func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes []
|
||||||
Architecture: arch,
|
Architecture: arch,
|
||||||
MicroArchitecture: microArch,
|
MicroArchitecture: microArch,
|
||||||
Type: TypeExecute,
|
Type: TypeExecute,
|
||||||
NumCommands: 8,
|
NumCommands: NumCommands,
|
||||||
SizeCommands: uint32(SizeCommands),
|
SizeCommands: uint32(SizeCommands),
|
||||||
Flags: FlagNoUndefs | FlagPIE | FlagNoHeapExecution,
|
Flags: FlagNoUndefs | FlagDyldLink | FlagTwoLevel | FlagPIE | FlagNoHeapExecution,
|
||||||
Reserved: 0,
|
Reserved: 0,
|
||||||
},
|
},
|
||||||
PageZero: Segment64{
|
PageZero: Segment64{
|
||||||
|
@ -79,19 +77,6 @@ func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes []
|
||||||
MaxProt: ProtReadable,
|
MaxProt: ProtReadable,
|
||||||
InitProt: 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{
|
MainHeader: Main{
|
||||||
LoadCommand: LcMain,
|
LoadCommand: LcMain,
|
||||||
Length: MainSize,
|
Length: MainSize,
|
||||||
|
@ -99,10 +84,8 @@ func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes []
|
||||||
StackSize: 0,
|
StackSize: 0,
|
||||||
},
|
},
|
||||||
InfoHeader: DyldInfoCommand{
|
InfoHeader: DyldInfoCommand{
|
||||||
LoadCommand: LcDyldInfoOnly,
|
LoadCommand: LcDyldInfoOnly,
|
||||||
Length: DyldInfoCommandSize,
|
Length: DyldInfoCommandSize,
|
||||||
RebaseOffset: uint32(imports.FileOffset),
|
|
||||||
RebaseSize: uint32(len(imports.Bytes)),
|
|
||||||
},
|
},
|
||||||
LinkerHeader: DylinkerCommand{
|
LinkerHeader: DylinkerCommand{
|
||||||
LoadCommand: LcLoadDylinker,
|
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.PageZero)
|
||||||
binary.Write(writer, binary.LittleEndian, &m.CodeHeader)
|
binary.Write(writer, binary.LittleEndian, &m.CodeHeader)
|
||||||
binary.Write(writer, binary.LittleEndian, &m.DataHeader)
|
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.MainHeader)
|
||||||
binary.Write(writer, binary.LittleEndian, &m.InfoHeader)
|
binary.Write(writer, binary.LittleEndian, &m.InfoHeader)
|
||||||
binary.Write(writer, binary.LittleEndian, &m.LinkerHeader)
|
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.Write(code.Bytes)
|
||||||
writer.Seek(int64(data.Padding), io.SeekCurrent)
|
writer.Seek(int64(data.Padding), io.SeekCurrent)
|
||||||
writer.Write(data.Bytes)
|
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