This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Private Function
fnselectMachOSlice(
all_mapped_memory: []align(std.heap.page_size_min) constu8,
arch: std.Target.Cpu.Arch,
) Error![]constu8 {
// In most cases, the file we just mapped is a Mach-O binary. However, it could be a "universal// binary": a simple file format which contains Mach-O binaries for multiple targets. For// instance, `/usr/lib/dyld` is currently distributed as a universal binary containing images// for both ARM64 macOS and x86_64 macOS.if (all_mapped_memory.len < 4) returnerror.InvalidMachO;
constmagic = std.mem.readInt(u32, all_mapped_memory.ptr[0..4], .little);
// The contents of a Mach-O file, which may or may not be the whole of `all_mapped_memory`.constmapped_macho = switch (magic) {
macho.MH_MAGIC_64 => all_mapped_memory,
macho.FAT_CIGAM => mapped_macho: {
// This is the universal binary format (aka a "fat binary").varfat_r: Io.Reader = .fixed(all_mapped_memory);
consthdr = fat_r.takeStruct(macho.fat_header, .big) catch |err| switch (err) {
error.ReadFailed => unreachable,
error.EndOfStream => returnerror.InvalidMachO,
};
constwant_cpu_type = switch (arch) {
.x86_64 => macho.CPU_TYPE_X86_64,
.aarch64 => macho.CPU_TYPE_ARM64,
else => unreachable,
};
for (0..hdr.nfat_arch) |_| {
constfat_arch = fat_r.takeStruct(macho.fat_arch, .big) catch |err| switch (err) {
error.ReadFailed => unreachable,
error.EndOfStream => returnerror.InvalidMachO,
};
if (fat_arch.cputype != want_cpu_type) continue;
if (fat_arch.offset + fat_arch.size > all_mapped_memory.len) returnerror.InvalidMachO;
break :mapped_machoall_mapped_memory[fat_arch.offset..][0..fat_arch.size];
}
// `arch` was not present in the fat binary.returnerror.MissingDebugInfo;
},
// Even on modern 64-bit targets, this format doesn't seem to be too extensively used. It// will be fairly easy to add support here if necessary; it's very similar to above.macho.FAT_CIGAM_64 => returnerror.UnsupportedDebugInfo,
else => returnerror.InvalidMachO,
};
returnmapped_macho;
}