Module: MachO::Tools

Defined in:
lib/macho/tools.rb

Overview

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

Class Method Summary collapse

Class Method Details

.add_rpath(filename, new_path, options = {}) ⇒ void

This method returns an undefined value.

Add a runtime path to a Mach-O or Fat binary, overwriting the source file.

Parameters:

  • filename (String)

    the Mach-O or Fat binary being modified

  • new_path (String)

    the new runtime path

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :strict (Boolean) — default: true

    whether or not to fail loudly with an exception if the change cannot be performed



67
68
69
70
71
72
# File 'lib/macho/tools.rb', line 67

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 = {}) ⇒ void

This method returns an undefined value.

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

Parameters:

  • filename (String)

    the Mach-O or Fat binary being modified

  • new_id (String)

    the new dylib ID for the binary

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :strict (Boolean) — default: true

    whether or not to fail loudly with an exception if the change cannot be performed



21
22
23
24
25
26
# File 'lib/macho/tools.rb', line 21

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 = {}) ⇒ void

This method returns an undefined value.

Changes a shared library install name in a Mach-O or Fat binary, overwriting the source file.

Parameters:

  • filename (String)

    the Mach-O or Fat binary being modified

  • old_name (String)

    the old shared library name

  • new_name (String)

    the new shared library name

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :strict (Boolean) — default: true

    whether or not to fail loudly with an exception if the change cannot be performed



37
38
39
40
41
42
# File 'lib/macho/tools.rb', line 37

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 = {}) ⇒ void

This method returns an undefined value.

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

Parameters:

  • filename (String)

    the Mach-O or Fat binary being modified

  • old_path (String)

    the old runtime path

  • new_path (String)

    the new runtime path

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :strict (Boolean) — default: true

    whether or not to fail loudly with an exception if the change cannot be performed



53
54
55
56
57
58
# File 'lib/macho/tools.rb', line 53

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 = {}) ⇒ void

This method returns an undefined value.

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

Parameters:

  • filename (String)

    the Mach-O or Fat binary being modified

  • old_path (String)

    the old runtime path

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :strict (Boolean) — default: true

    whether or not to fail loudly with an exception if the change cannot be performed



82
83
84
85
86
87
# File 'lib/macho/tools.rb', line 82

def self.delete_rpath(filename, old_path, options = {})
  file = MachO.open(filename)

  file.delete_rpath(old_path, options)
  file.write!
end

.dylibs(filename) ⇒ Array<String>

Returns an array of all dylibs linked to the binary.

Parameters:

  • filename (String)

    the Mach-O or Fat binary being read

Returns:

  • (Array<String>)

    an array of all dylibs linked to the binary



7
8
9
10
11
# File 'lib/macho/tools.rb', line 7

def self.dylibs(filename)
  file = MachO.open(filename)

  file.linked_dylibs
end

.merge_machos(filename, *files, fat64: false) ⇒ void

This method returns an undefined value.

Merge multiple Mach-Os into one universal (Fat) binary.

Parameters:

  • filename (String)

    the fat binary to create

  • files (Array<String>)

    the files to merge

  • fat64 (Boolean) (defaults to: false)

    whether to use Headers::FatArch64s to represent each slice



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/macho/tools.rb', line 94

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