Class: MachO::FatFile

Inherits:
Object
  • Object
show all
Defined in:
lib/macho/fat_file.rb

Overview

Represents a "Fat" file, which contains a header, a listing of available architectures, and one or more Mach-O binaries.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ FatFile

Creates a new FatFile from the given filename.

Parameters:

  • filename (String)

    the fat file to load from

Raises:

  • (ArgumentError)

    if the given file does not exist



32
33
34
35
36
37
38
39
40
# File 'lib/macho/fat_file.rb', line 32

def initialize(filename)
  raise ArgumentError.new("#{filename}: no such file") unless File.file?(filename)

  @filename = filename
  @raw_data = File.open(@filename, "rb") { |f| f.read }
  @header = get_fat_header
  @fat_archs = get_fat_archs
  @machos = get_machos
end

Instance Attribute Details

#fat_archsArray<MachO::FatArch> (readonly)

Returns an array of fat architectures.

Returns:



14
15
16
# File 'lib/macho/fat_file.rb', line 14

def fat_archs
  @fat_archs
end

#filenameString

Returns the filename loaded from, or nil if loaded from a binary string.

Returns:

  • (String)

    the filename loaded from, or nil if loaded from a binary string



8
9
10
# File 'lib/macho/fat_file.rb', line 8

def filename
  @filename
end

#headerMachO::FatHeader (readonly)

Returns the file's header.

Returns:



11
12
13
# File 'lib/macho/fat_file.rb', line 11

def header
  @header
end

#machosArray<MachO::MachOFile> (readonly)

Returns an array of Mach-O binaries.

Returns:



17
18
19
# File 'lib/macho/fat_file.rb', line 17

def machos
  @machos
end

Class Method Details

.new_from_bin(bin) ⇒ MachO::FatFile

Creates a new FatFile instance from a binary string.

Parameters:

  • bin (String)

    a binary string containing raw Mach-O data

Returns:



22
23
24
25
26
27
# File 'lib/macho/fat_file.rb', line 22

def self.new_from_bin(bin)
  instance = allocate
  instance.initialize_from_bin(bin)

  instance
end

Instance Method Details

#bundle?Boolean

Returns true if the file is of type MH_BUNDLE, false otherwise.

Returns:

  • (Boolean)

    true if the file is of type MH_BUNDLE, false otherwise



93
94
95
# File 'lib/macho/fat_file.rb', line 93

def bundle?
  machos.first.bundle?
end

#change_install_name(old_name, new_name) ⇒ Object Also known as: change_dylib

TODO:

incomplete

Changes all dependent shared library install names from old_name to new_name. In a fat file, this changes install names in all internal Mach-Os.

Examples:

file.change_install_name('/usr/lib/libFoo.dylib', '/usr/lib/libBar.dylib')

Parameters:

  • old_name (String)

    the shared library name being changed

  • new_name (String)

    the new name



169
170
171
172
173
174
175
# File 'lib/macho/fat_file.rb', line 169

def change_install_name(old_name, new_name)
  machos.each do |macho|
    macho.change_install_name(old_name, new_name)
  end

  synchronize_raw_data
end

#core?Boolean

Returns true if the file is of type MH_CORE, false otherwise.

Returns:

  • (Boolean)

    true if the file is of type MH_CORE, false otherwise



73
74
75
# File 'lib/macho/fat_file.rb', line 73

def core?
  machos.first.core?
end

#dsym?Boolean

Returns true if the file is of type MH_DSYM, false otherwise.

Returns:

  • (Boolean)

    true if the file is of type MH_DSYM, false otherwise



98
99
100
# File 'lib/macho/fat_file.rb', line 98

def dsym?
  machos.first.dsym?
end

#dylib?Boolean

Returns true if the file is of type MH_DYLIB, false otherwise.

Returns:

  • (Boolean)

    true if the file is of type MH_DYLIB, false otherwise



83
84
85
# File 'lib/macho/fat_file.rb', line 83

def dylib?
  machos.first.dylib?
end

#dylib_idString?

The file's dylib ID. If the file is not a dylib, returns nil.

Examples:

file.dylib_id # => 'libBar.dylib'

Returns:

  • (String, nil)

    the file's dylib ID



127
128
129
# File 'lib/macho/fat_file.rb', line 127

def dylib_id
  machos.first.dylib_id
end

#dylib_id=(new_id) ⇒ void

This method returns an undefined value.

Changes the file's dylib ID to new_id. If the file is not a dylib, does nothing.

Examples:

file.dylib_id = 'libFoo.dylib'

Parameters:

  • new_id (String)

    the new dylib ID

Raises:

  • (ArgumentError)

    if new_id is not a String



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/macho/fat_file.rb', line 137

def dylib_id=(new_id)
  if !new_id.is_a?(String)
    raise ArgumentError.new("argument must be a String")
  end

  if !machos.all?(&:dylib?)
    return nil
  end

  machos.each do |macho|
    macho.dylib_id = new_id
  end

  synchronize_raw_data
end

#dylinker?Boolean

Returns true if the file is of type MH_DYLINKER, false otherwise.

Returns:

  • (Boolean)

    true if the file is of type MH_DYLINKER, false otherwise



88
89
90
# File 'lib/macho/fat_file.rb', line 88

def dylinker?
  machos.first.dylinker?
end

#executable?Boolean

Returns true if the file is of type MH_EXECUTE, false otherwise.

Returns:

  • (Boolean)

    true if the file is of type MH_EXECUTE, false otherwise



63
64
65
# File 'lib/macho/fat_file.rb', line 63

def executable?
  machos.first.executable?
end

#extract(cputype) ⇒ MachO::MachOFile?

Extract a Mach-O with the given CPU type from the file.

Examples:

file.extract(:i386) # => MachO::MachOFile

Parameters:

  • cputype (Symbol)

    the CPU type of the Mach-O being extracted

Returns:

  • (MachO::MachOFile, nil)

    the extracted Mach-O or nil if no Mach-O has the given CPU type



184
185
186
# File 'lib/macho/fat_file.rb', line 184

def extract(cputype)
  machos.select { |macho| macho.cputype == cputype }.first
end

#filetypeString

The file's type. Assumed to be the same for every Mach-O within.

Returns:

  • (String)

    the filetype



119
120
121
# File 'lib/macho/fat_file.rb', line 119

def filetype
  machos.first.filetype
end

#fvmlib?Boolean

Returns true if the file is of type MH_FVMLIB, false otherwise.

Returns:

  • (Boolean)

    true if the file is of type MH_FVMLIB, false otherwise



68
69
70
# File 'lib/macho/fat_file.rb', line 68

def fvmlib?
  machos.first.fvmlib?
end

#initialize_from_bin(bin) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
46
47
48
49
# File 'lib/macho/fat_file.rb', line 43

def initialize_from_bin(bin)
  @filename = nil
  @raw_data = bin
  @header = get_fat_header
  @fat_archs = get_fat_archs
  @machos = get_machos
end

#kext?Boolean

Returns true if the file is of type MH_KEXT_BUNDLE, false otherwise.

Returns:

  • (Boolean)

    true if the file is of type MH_KEXT_BUNDLE, false otherwise



103
104
105
# File 'lib/macho/fat_file.rb', line 103

def kext?
  machos.first.kext?
end

#linked_dylibsArray<String>

All shared libraries linked to the file's Mach-Os.

Returns:

  • (Array<String>)

    an array of all shared libraries



155
156
157
158
159
160
# File 'lib/macho/fat_file.rb', line 155

def linked_dylibs
  # Individual architectures in a fat binary can link to different subsets
  # of libraries, but at this point we want to have the full picture, i.e.
  # the union of all libraries used by all architectures.
  machos.map(&:linked_dylibs).flatten.uniq
end

#magicFixnum

Returns the file's magic number.

Returns:

  • (Fixnum)

    the file's magic number



108
109
110
# File 'lib/macho/fat_file.rb', line 108

def magic
  header.magic
end

#magic_stringString

Returns a string representation of the file's magic number.

Returns:

  • (String)

    a string representation of the file's magic number



113
114
115
# File 'lib/macho/fat_file.rb', line 113

def magic_string
  MH_MAGICS[magic]
end

#object?Boolean

Returns true if the file is of type MH_OBJECT, false otherwise.

Returns:

  • (Boolean)

    true if the file is of type MH_OBJECT, false otherwise



58
59
60
# File 'lib/macho/fat_file.rb', line 58

def object?
  machos.first.object?
end

#preload?Boolean

Returns true if the file is of type MH_PRELOAD, false otherwise.

Returns:

  • (Boolean)

    true if the file is of type MH_PRELOAD, false otherwise



78
79
80
# File 'lib/macho/fat_file.rb', line 78

def preload?
  machos.first.preload?
end

#serializeString

The file's raw fat data.

Returns:

  • (String)

    the raw fat data



53
54
55
# File 'lib/macho/fat_file.rb', line 53

def serialize
  @raw_data
end

#write(filename) ⇒ Object

Write all (fat) data to the given filename.

Parameters:

  • filename (String)

    the file to write to



190
191
192
# File 'lib/macho/fat_file.rb', line 190

def write(filename)
  File.open(filename, "wb") { |f| f.write(@raw_data) }
end

#write!void

Note:

Overwrites all data in the file!

This method returns an undefined value.

Write all (fat) data to the file used to initialize the instance.

Raises:



198
199
200
201
202
203
204
# File 'lib/macho/fat_file.rb', line 198

def write!
  if filename.nil?
    raise MachOError.new("cannot write to a default file when initialized from a binary string")
  else
    File.open(@filename, "wb") { |f| f.write(@raw_data) }
  end
end