module MachO::Tools

A collection of convenient methods for common operations on Mach-O and Fat binaries.

Public Class Methods

add_rpath(filename, new_path, options = {}) click to toggle source

Add a runtime path to a Mach-O or Fat binary, overwriting the source file. @param filename [String] the Mach-O or Fat binary being modified @param new_path [String] the new runtime path @param options [Hash] @option options [Boolean] :strict (true) whether or not to fail loudly

with an exception if the change cannot be performed

@return [void]

# File lib/macho/tools.rb, line 71
def self.add_rpath(filename, new_path, options = {})
  file = MachO.open(filename)

  file.add_rpath(new_path, options)
  file.write!
end
change_dylib_id(filename, new_id, options = {}) click to toggle source

Changes the dylib ID of a Mach-O or Fat binary, overwriting the source

file.

@param filename [String] the Mach-O or Fat binary being modified @param new_id [String] the new dylib ID for the binary @param options [Hash] @option options [Boolean] :strict (true) whether or not to fail loudly

with an exception if the change cannot be performed

@return [void]

# File lib/macho/tools.rb, line 23
def self.change_dylib_id(filename, new_id, options = {})
  file = MachO.open(filename)

  file.change_dylib_id(new_id, options)
  file.write!
end
change_install_name(filename, old_name, new_name, options = {}) click to toggle source

Changes a shared library install name in a Mach-O or Fat binary,

overwriting the source file.

@param filename [String] the Mach-O or Fat binary being modified @param old_name [String] the old shared library name @param new_name [String] the new shared library name @param options [Hash] @option options [Boolean] :strict (true) whether or not to fail loudly

with an exception if the change cannot be performed

@return [void]

# File lib/macho/tools.rb, line 39
def self.change_install_name(filename, old_name, new_name, options = {})
  file = MachO.open(filename)

  file.change_install_name(old_name, new_name, options)
  file.write!
end
change_rpath(filename, old_path, new_path, options = {}) click to toggle source

Changes a runtime path in a Mach-O or Fat binary, overwriting the source

file.

@param filename [String] the Mach-O or Fat binary being modified @param old_path [String] the old runtime path @param new_path [String] the new runtime path @param options [Hash] @option options [Boolean] :strict (true) whether or not to fail loudly

with an exception if the change cannot be performed

@option options [Boolean] :uniq (false) whether or not to change duplicate

rpaths simultaneously

@return [void]

# File lib/macho/tools.rb, line 57
def self.change_rpath(filename, old_path, new_path, options = {})
  file = MachO.open(filename)

  file.change_rpath(old_path, new_path, options)
  file.write!
end
delete_rpath(filename, old_path, options = {}) click to toggle source

Delete a runtime path from a Mach-O or Fat binary, overwriting the source

file.

@param filename [String] the Mach-O or Fat binary being modified @param old_path [String] the old runtime path @param options [Hash] @option options [Boolean] :strict (true) whether or not to fail loudly

with an exception if the change cannot be performed

@option options [Boolean] :uniq (false) whether or not to delete duplicate

rpaths simultaneously

@return [void]

# File lib/macho/tools.rb, line 88
def self.delete_rpath(filename, old_path, options = {})
  file = MachO.open(filename)

  file.delete_rpath(old_path, options)
  file.write!
end
dylibs(filename) click to toggle source

@param filename [String] the Mach-O or Fat binary being read @return [Array<String>] an array of all dylibs linked to the binary

# File lib/macho/tools.rb, line 9
def self.dylibs(filename)
  file = MachO.open(filename)

  file.linked_dylibs
end
merge_machos(filename, *files, fat64: false) click to toggle source

Merge multiple Mach-Os into one universal (Fat) binary. @param filename [String] the fat binary to create @param files [Array<String>] the files to merge @param fat64 [Boolean] whether to use {Headers::FatArch64}s to represent each slice @return [void]

# File lib/macho/tools.rb, line 100
def self.merge_machos(filename, *files, fat64: false)
  machos = files.map do |file|
    macho = MachO.open(file)
    case macho
    when MachO::MachOFile
      macho
    else
      macho.machos
    end
  end.flatten

  fat_macho = MachO::FatFile.new_from_machos(*machos, :fat64 => fat64)
  fat_macho.write(filename)
end