Class: MachO::FatFile
- Inherits:
-
Object
- Object
- MachO::FatFile
- 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
-
#fat_archs ⇒ Array<MachO::FatArch>
readonly
An array of fat architectures.
-
#filename ⇒ String
The filename loaded from, or nil if loaded from a binary string.
-
#header ⇒ MachO::FatHeader
readonly
The file's header.
-
#machos ⇒ Array<MachO::MachOFile>
readonly
An array of Mach-O binaries.
Class Method Summary collapse
-
.new_from_bin(bin) ⇒ MachO::FatFile
Creates a new FatFile instance from a binary string.
Instance Method Summary collapse
-
#bundle? ⇒ Boolean
True if the file is of type
MH_BUNDLE, false otherwise. -
#change_install_name(old_name, new_name) ⇒ Object
(also: #change_dylib)
Changes all dependent shared library install names from
old_nametonew_name. -
#core? ⇒ Boolean
True if the file is of type
MH_CORE, false otherwise. -
#dsym? ⇒ Boolean
True if the file is of type
MH_DSYM, false otherwise. -
#dylib? ⇒ Boolean
True if the file is of type
MH_DYLIB, false otherwise. -
#dylib_id ⇒ String?
The file's dylib ID.
-
#dylib_id=(new_id) ⇒ void
Changes the file's dylib ID to
new_id. -
#dylinker? ⇒ Boolean
True if the file is of type
MH_DYLINKER, false otherwise. -
#executable? ⇒ Boolean
True if the file is of type
MH_EXECUTE, false otherwise. -
#extract(cputype) ⇒ MachO::MachOFile?
Extract a Mach-O with the given CPU type from the file.
-
#filetype ⇒ String
The file's type.
-
#fvmlib? ⇒ Boolean
True if the file is of type
MH_FVMLIB, false otherwise. -
#initialize(filename) ⇒ FatFile
constructor
Creates a new FatFile from the given filename.
- #initialize_from_bin(bin) ⇒ Object private
-
#kext? ⇒ Boolean
True if the file is of type
MH_KEXT_BUNDLE, false otherwise. -
#linked_dylibs ⇒ Array<String>
All shared libraries linked to the file's Mach-Os.
-
#magic ⇒ Fixnum
The file's magic number.
-
#magic_string ⇒ String
A string representation of the file's magic number.
-
#object? ⇒ Boolean
True if the file is of type
MH_OBJECT, false otherwise. -
#preload? ⇒ Boolean
True if the file is of type
MH_PRELOAD, false otherwise. -
#serialize ⇒ String
The file's raw fat data.
-
#write(filename) ⇒ Object
Write all (fat) data to the given filename.
-
#write! ⇒ void
Write all (fat) data to the file used to initialize the instance.
Constructor Details
#initialize(filename) ⇒ FatFile
Creates a new FatFile from the given filename.
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_archs ⇒ Array<MachO::FatArch> (readonly)
Returns an array of fat architectures.
14 15 16 |
# File 'lib/macho/fat_file.rb', line 14 def fat_archs @fat_archs end |
#filename ⇒ String
Returns 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 |
#header ⇒ MachO::FatHeader (readonly)
Returns the file's header.
11 12 13 |
# File 'lib/macho/fat_file.rb', line 11 def header @header end |
#machos ⇒ Array<MachO::MachOFile> (readonly)
Returns an array of Mach-O binaries.
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.
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.
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
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.
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.
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.
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.
83 84 85 |
# File 'lib/macho/fat_file.rb', line 83 def dylib? machos.first.dylib? end |
#dylib_id ⇒ String?
The file's dylib ID. If the file is not a dylib, returns nil.
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.
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.
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.
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.
184 185 186 |
# File 'lib/macho/fat_file.rb', line 184 def extract(cputype) machos.select { |macho| macho.cputype == cputype }.first end |
#filetype ⇒ String
The file's type. Assumed to be the same for every Mach-O within.
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.
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.
103 104 105 |
# File 'lib/macho/fat_file.rb', line 103 def kext? machos.first.kext? end |
#linked_dylibs ⇒ Array<String>
All shared libraries linked to the file's Mach-Os.
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 |
#magic ⇒ Fixnum
Returns the file's magic number.
108 109 110 |
# File 'lib/macho/fat_file.rb', line 108 def magic header.magic end |
#magic_string ⇒ String
Returns 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.
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.
78 79 80 |
# File 'lib/macho/fat_file.rb', line 78 def preload? machos.first.preload? end |
#serialize ⇒ String
The file's 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.
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
Overwrites all data in the file!
This method returns an undefined value.
Write all (fat) data to the file used to initialize the instance.
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 |