macOS Mach-O multiarch ("fat") binary: JavaScript parsing library

This is a simple container format that encapsulates multiple Mach-O files, each generally for a different architecture. XNU can execute these files just like single-arch Mach-Os and will pick the appropriate entry.

KS implementation details

License: CC0-1.0

This page hosts a formal specification of macOS Mach-O multiarch ("fat") binary using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for JavaScript generated by Kaitai Struct depends on the JavaScript runtime library. You have to install it before you can parse data.

The JavaScript runtime library is available at npm:

npm install kaitai-struct

Code

See the usage examples in the JavaScript notes.

Parse structure from an ArrayBuffer:

var arrayBuffer = ...;
var data = new MachOFat(new KaitaiStream(arrayBuffer));

After that, one can get various attributes from the structure by accessing fields or properties like:

data.magic // => get magic

JavaScript source code to parse macOS Mach-O multiarch ("fat") binary

MachOFat.js

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(['kaitai-struct/KaitaiStream', './MachO'], factory);
  } else if (typeof module === 'object' && module.exports) {
    module.exports = factory(require('kaitai-struct/KaitaiStream'), require('./MachO'));
  } else {
    root.MachOFat = factory(root.KaitaiStream, root.MachO);
  }
}(typeof self !== 'undefined' ? self : this, function (KaitaiStream, MachO) {
/**
 * This is a simple container format that encapsulates multiple Mach-O files,
 * each generally for a different architecture. XNU can execute these files just
 * like single-arch Mach-Os and will pick the appropriate entry.
 * @see {@link https://opensource.apple.com/source/xnu/xnu-7195.121.3/EXTERNAL_HEADERS/mach-o/fat.h.auto.html|Source}
 */

var MachOFat = (function() {
  function MachOFat(_io, _parent, _root) {
    this._io = _io;
    this._parent = _parent;
    this._root = _root || this;

    this._read();
  }
  MachOFat.prototype._read = function() {
    this.magic = this._io.readBytes(4);
    if (!((KaitaiStream.byteArrayCompare(this.magic, [202, 254, 186, 190]) == 0))) {
      throw new KaitaiStream.ValidationNotEqualError([202, 254, 186, 190], this.magic, this._io, "/seq/0");
    }
    this.numFatArch = this._io.readU4be();
    this.fatArchs = [];
    for (var i = 0; i < this.numFatArch; i++) {
      this.fatArchs.push(new FatArch(this._io, this, this._root));
    }
  }

  var FatArch = MachOFat.FatArch = (function() {
    function FatArch(_io, _parent, _root) {
      this._io = _io;
      this._parent = _parent;
      this._root = _root || this;

      this._read();
    }
    FatArch.prototype._read = function() {
      this.cpuType = this._io.readU4be();
      this.cpuSubtype = this._io.readU4be();
      this.ofsObject = this._io.readU4be();
      this.lenObject = this._io.readU4be();
      this.align = this._io.readU4be();
    }
    Object.defineProperty(FatArch.prototype, 'object', {
      get: function() {
        if (this._m_object !== undefined)
          return this._m_object;
        var _pos = this._io.pos;
        this._io.seek(this.ofsObject);
        this._raw__m_object = this._io.readBytes(this.lenObject);
        var _io__raw__m_object = new KaitaiStream(this._raw__m_object);
        this._m_object = new MachO(_io__raw__m_object, this, null);
        this._io.seek(_pos);
        return this._m_object;
      }
    });

    return FatArch;
  })();

  return MachOFat;
})();
return MachOFat;
}));