// / Contains the declaration of FatHeader and FatArch // / These structs are always written using Big-Endian, // / as documented in the mach-o/fat.h // / This file also has a CreateFat function to generate // / Fat file from a list of MachoContext package fat import ( "io" "os" "sort" log "github.com/sirupsen/logrus" . "ios-wrapper/pkg/ios" macho "ios-wrapper/pkg/ios/macho" ) // / Get the alignment for the Mach-O in Fat binary // / The returned value is the multiplier of 2 func GetAlignment(h *macho.Header) uint32 { switch h.Cputype() { case CPU_TYPE_ARM, CPU_TYPE_ARM64: return 0xe // log2(0x4000) case CPU_TYPE_POWERPC, CPU_TYPE_POWERPC64, CPU_TYPE_I386, CPU_TYPE_X86_64: return 0xc // log2(0x1000) default: return 0xd // log2(0x2000) } } func MachosToFatArchs(machos []*macho.MachoContext) []*FatArch { var fa []*FatArch for _, m := range machos { fa = append(fa, &FatArch{ Cputype: m.Header().Cputype(), Cpusubtype: m.Header().Cpusubtype(), Size: m.FileSize(), Offset: 0, Align: GetAlignment(m.Header()), }) } return fa } // / Create a Fat binary from MachoContext // / Convert MachoContext to FatArch // / Calculate the alignment, now using basic calculation // / because iOS always has valid archs ARM // / // / Sort the Fat arch as per the cctools/lipo // / Calculate the offset with each Mach-O // / // / Write the FatHeader // / Write the FatArchs // / Write the Mach-Os func CreateFat(machos []*macho.MachoContext, outfilename string) error { archs := MachosToFatArchs(machos) sort.SliceStable(archs, func(i, j int) bool { if archs[i].Cputype == archs[j].Cputype { return archs[i].Cpusubtype < archs[i].Cpusubtype } if archs[i].Cputype == CPU_TYPE_ARM64 { return false } if archs[j].Cputype == CPU_TYPE_ARM64 { return true } return archs[i].Cpusubtype < archs[j].Cpusubtype }) // calculate the offset for each FatArch offset := uint32(8) // size of fat header, bytes // offset to the first macho offset += uint32(len(archs)) * 5 // size of fat_arch, bytes for _, arch := range archs { offset = uint32(OffsetRounder(uint64(offset), 1<