Class: MachO::MachOFile

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

Overview

Represents a Mach-O file, which contains a header and load commands as well as binary executable instructions. Mach-O binaries are architecture specific.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ MachOFile

TODO:

document all the exceptions propagated here

Creates a new FatFile from the given filename.

Parameters:

  • filename (String)

    the Mach-O file to load from

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
# File 'lib/macho/macho_file.rb', line 27

def initialize(filename)
	raise ArgumentError.new("filename must be a String") unless filename.is_a? String

	@filename = filename
	@raw_data = open(@filename, "rb") { |f| f.read }
	@header = get_mach_header
	@load_commands = get_load_commands
end

Instance Attribute Details

#headerMachO::MachHeader, MachO::MachHeader64 (readonly)

Returns:



9
10
11
# File 'lib/macho/macho_file.rb', line 9

def header
  @header
end

#load_commandsArray<MachO::LoadCommand> (readonly)

Returns an array of the file’s load commands.

Returns:



12
13
14
# File 'lib/macho/macho_file.rb', line 12

def load_commands
  @load_commands
end

Class Method Details

.new_from_bin(bin) ⇒ MachO::MachOFile

Creates a new MachOFile instance from a binary string.

Parameters:

  • bin (String)

    a binary string containing raw Mach-O data

Returns:



17
18
19
20
21
22
# File 'lib/macho/macho_file.rb', line 17

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

	instance
end

Instance Method Details

#bundle?Boolean

Returns true if the Mach-O is of type ‘MH_BUNDLE`, false otherwise.

Returns:

  • (Boolean)

    true if the Mach-O is of type ‘MH_BUNDLE`, false otherwise



71
72
73
# File 'lib/macho/macho_file.rb', line 71

def bundle?
	header[:filetype] == MH_BUNDLE
end

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

TODO:

refactor

This method returns an undefined value.

Raises:



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/macho/macho_file.rb', line 258

def change_install_name(old_name, new_name)
	idx = linked_dylibs.index(old_name)
	raise DylibUnknownError.new(old_name) if idx.nil?

	# this is a bit of a hack - since there is a 1-1 ordered association
	# between linked_dylibs and command('LC_LOAD_DYLIB'), we can use
	# their indices interchangeably to avoid having to loop.
	dylib_cmd = command('LC_LOAD_DYLIB')[idx]

	if magic32?
		cmd_round = 4
	else
		cmd_round = 8
	end

	new_sizeofcmds = header[:sizeofcmds]
	old_install_name = old_name.dup
	new_install_name = new_name.dup

	old_pad = MachO.round(old_install_name.size, cmd_round) - old_install_name.size
	new_pad = MachO.round(new_install_name.size, cmd_round) - new_install_name.size

	old_install_name << "\x00" * old_pad
	new_install_name << "\x00" * new_pad

	new_size = DylibCommand.bytesize + new_install_name.size
	new_sizeofcmds += new_size - dylib_cmd.cmdsize

	low_fileoff = 2**64

	segments.each do |seg|
		sections(seg).each do |sect|
			if sect.size != 0 && !sect.flag?(S_ZEROFILL) &&
					!sect.flag?(S_THREAD_LOCAL_ZEROFILL) &&
					sect.offset < low_fileoff

				low_fileoff = sect.offset
			end
		end
	end

	if new_sizeofcmds + header.bytesize > low_fileoff
		raise HeaderPadError.new(@filename)
	end

	set_sizeofcmds(new_sizeofcmds)

	@raw_data[dylib_cmd.offset + 4, 4] = [new_size].pack("V")

	@raw_data.slice!(dylib_cmd.offset + dylib_cmd.name...dylib_cmd.offset + dylib_cmd.class.bytesize + old_install_name.size)

	@raw_data.insert(dylib_cmd.offset + dylib_cmd.name, new_install_name)

	null_pad = old_install_name.size - new_install_name.size

	if null_pad < 0
		@raw_data.slice!(new_sizeofcmds + header.bytesize, null_pad.abs)
	else
		@raw_data.insert(new_sizeofcmds + header.bytesize, "\x00" * null_pad)
	end

	header = get_mach_header
	load_commands = get_load_commands
end

#command(name) ⇒ Array<MachO::LoadCommand> Also known as: []

All load commands of a given name.

Examples:

file.command("LC_LOAD_DYLIB")
file["LC_LOAD_DYLIB"]

Returns:



120
121
122
# File 'lib/macho/macho_file.rb', line 120

def command(name)
	load_commands.select { |lc| lc.to_s == name }
end

#cpusubtypeString

Returns a string representation of the Mach-O’s CPU subtype.

Returns:

  • (String)

    a string representation of the Mach-O’s CPU subtype



96
97
98
# File 'lib/macho/macho_file.rb', line 96

def cpusubtype
	CPU_SUBTYPES[header[:cpusubtype]]
end

#cputypeString

Returns a string representation of the Mach-O’s CPU type.

Returns:

  • (String)

    a string representation of the Mach-O’s CPU type



91
92
93
# File 'lib/macho/macho_file.rb', line 91

def cputype
	CPU_TYPES[header[:cputype]]
end

#dylib?Boolean

Returns true if the Mach-O is of type ‘MH_DYLIB`, false otherwise.

Returns:

  • (Boolean)

    true if the Mach-O is of type ‘MH_DYLIB`, false otherwise



66
67
68
# File 'lib/macho/macho_file.rb', line 66

def dylib?
	header[:filetype] == MH_DYLIB
end

#dylib_idString

The Mach-O’s dylib ID, or ‘nil` if not a dylib.

Examples:

file.dylib_id # => 'libBar.dylib'

Returns:

  • (String)

    the Mach-O’s dylib ID



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/macho/macho_file.rb', line 141

def dylib_id
	if !dylib?
		return nil
	end

	dylib_id_cmd = command('LC_ID_DYLIB').first

	cmdsize = dylib_id_cmd.cmdsize
	offset = dylib_id_cmd.offset
	stroffset = dylib_id_cmd.name

	dylib_id = @raw_data.slice(offset + stroffset...offset + cmdsize).unpack("Z*").first

	dylib_id.delete("\x00")
end

#dylib_id=(new_id) ⇒ void

TODO:

refactor

This method returns an undefined value.

Changes the Mach-O’s dylib ID to ‘new_id`. Does nothing if not a dylib.

Examples:

file.dylib_id = "libFoo.dylib"


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/macho/macho_file.rb', line 162

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

	if !dylib?
		return nil
	end

	if magic32?
		cmd_round = 4
	else
		cmd_round = 8
	end

	new_sizeofcmds = header[:sizeofcmds]
	dylib_id_cmd = command('LC_ID_DYLIB').first
	old_id = dylib_id
	new_id = new_id.dup

	new_pad = MachO.round(new_id.size, cmd_round) - new_id.size
	old_pad = MachO.round(old_id.size, cmd_round) - old_id.size

	# pad the old and new IDs with null bytes to meet command bounds
	old_id << "\x00" * old_pad
	new_id << "\x00" * new_pad

	# calculate the new size of the DylibCommand and sizeofcmds in MH
	new_size = DylibCommand.bytesize + new_id.size
	new_sizeofcmds += new_size - dylib_id_cmd.cmdsize

	# calculate the low file offset (offset to first section data)
	low_fileoff = 2**64 # ULLONGMAX

	segments.each do |seg|
		sections(seg).each do |sect|
			if sect.size != 0 && !sect.flag?(S_ZEROFILL) &&
					!sect.flag?(S_THREAD_LOCAL_ZEROFILL) &&
					sect.offset < low_fileoff

				low_fileoff = sect.offset
			end
		end
	end

	if new_sizeofcmds + header.bytesize > low_fileoff
		raise HeaderPadError.new(@filename)
	end

	# update sizeofcmds in mach_header
	set_sizeofcmds(new_sizeofcmds)

	# update cmdsize in the dylib_command
	@raw_data[dylib_id_cmd.offset + 4, 4] = [new_size].pack("V")

	# delete the old id
	@raw_data.slice!(dylib_id_cmd.offset + dylib_id_cmd.name...dylib_id_cmd.offset + dylib_id_cmd.class.bytesize + old_id.size)

	# insert the new id
	@raw_data.insert(dylib_id_cmd.offset + dylib_id_cmd.name, new_id)

	# pad/unpad after new_sizeofcmds until offsets are corrected
	null_pad = old_id.size - new_id.size

	if null_pad < 0
		@raw_data.slice!(new_sizeofcmds + header.bytesize, null_pad.abs)
	else
		@raw_data.insert(new_sizeofcmds + header.bytesize, "\x00" * null_pad)
	end

	# synchronize fields with the raw data
	header = get_mach_header
	load_commands = get_load_commands
end

#executable?Boolean

Returns true if the Mach-O is of type ‘MH_EXECUTE`, false otherwise.

Returns:

  • (Boolean)

    true if the Mach-O is of type ‘MH_EXECUTE`, false otherwise



61
62
63
# File 'lib/macho/macho_file.rb', line 61

def executable?
	header[:filetype] == MH_EXECUTE
end

#filetypeString

Returns a string representation of the Mach-O’s filetype.

Returns:

  • (String)

    a string representation of the Mach-O’s filetype



86
87
88
# File 'lib/macho/macho_file.rb', line 86

def filetype
	MH_FILETYPES[header[:filetype]]
end

#flagsFixnum

Returns execution flags set by the linker.

Returns:

  • (Fixnum)

    execution flags set by the linker



111
112
113
# File 'lib/macho/macho_file.rb', line 111

def flags
	header[:flags]
end

#initialize_from_bin(bin) ⇒ Object



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

def initialize_from_bin(bin)
	@filename = nil
	@raw_data = bin
	@header = get_mach_header
	@load_commands = get_load_commands
end

#linked_dylibsArray<String>

All shared libraries linked to the Mach-O.

Returns:

  • (Array<String>)

    an array of all shared libraries



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/macho/macho_file.rb', line 239

def linked_dylibs
	dylibs = []
	dylib_cmds = command('LC_LOAD_DYLIB')

	dylib_cmds.each do |dylib_cmd|
		cmdsize = dylib_cmd.cmdsize
		offset = dylib_cmd.offset
		stroffset = dylib_cmd.name

		dylib = @raw_data.slice(offset + stroffset...offset + cmdsize).unpack("Z*").first

		dylibs << dylib
	end

	dylibs
end

#magicFixnum

Returns the Mach-O’s magic number.

Returns:

  • (Fixnum)

    the Mach-O’s magic number



76
77
78
# File 'lib/macho/macho_file.rb', line 76

def magic
	header[:magic]
end

#magic32?Boolean

Returns true if the Mach-O has 32-bit magic, false otherwise.

Returns:

  • (Boolean)

    true if the Mach-O has 32-bit magic, false otherwise



51
52
53
# File 'lib/macho/macho_file.rb', line 51

def magic32?
	MachO.magic32?(header[:magic])
end

#magic64?Boolean

Returns true if the Mach-O has 64-bit magic, false otherwise.

Returns:

  • (Boolean)

    true if the Mach-O has 64-bit magic, false otherwise



56
57
58
# File 'lib/macho/macho_file.rb', line 56

def magic64?
	MachO.magic64?(header[:magic])
end

#magic_stringString

Returns a string representation of the Mach-O’s magic number.

Returns:

  • (String)

    a string representation of the Mach-O’s magic number



81
82
83
# File 'lib/macho/macho_file.rb', line 81

def magic_string
	MH_MAGICS[header[:magic]]
end

#ncmdsFixnum

Returns the number of load commands in the Mach-O’s header.

Returns:

  • (Fixnum)

    the number of load commands in the Mach-O’s header



101
102
103
# File 'lib/macho/macho_file.rb', line 101

def ncmds
	header[:ncmds]
end

#sections(segment) ⇒ Array<MachO::Section>, Array<MachO::Section64>

All sections of the segment ‘segment`.

Returns:



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/macho/macho_file.rb', line 328

def sections(segment)
	sections = []

	if !segment.is_a?(SegmentCommand) && !segment.is_a?(SegmentCommand64)
		raise ArgumentError.new("not a valid segment")
	end

	if segment.nsects.zero?
		return sections
	end

	offset = segment.offset + segment.class.bytesize

	segment.nsects.times do
		if segment.is_a? SegmentCommand
			sections << Section.new_from_bin(@raw_data.slice(offset, Section.bytesize))
			offset += Section.bytesize
		else
			sections << Section64.new_from_bin(@raw_data.slice(offset, Section64.bytesize))
			offset += Section64.bytesize
		end
	end

	sections
end

#segmentsArray<MachO::SegmentCommand>, Array<MachO::SegmentCommand64>

All segment load commands in the Mach-O.

Returns:



129
130
131
132
133
134
135
# File 'lib/macho/macho_file.rb', line 129

def segments
	if magic32?
		command("LC_SEGMENT")
	else
		command("LC_SEGMENT_64")
	end
end

#serializeString

The file’s raw Mach-O data.

Returns:

  • (String)

    the raw Mach-O data



46
47
48
# File 'lib/macho/macho_file.rb', line 46

def serialize
	@raw_data
end

#sizeofcmdsFixnum

Returns the size of all load commands, in bytes.

Returns:

  • (Fixnum)

    the size of all load commands, in bytes



106
107
108
# File 'lib/macho/macho_file.rb', line 106

def sizeofcmds
	header[:sizeofcmds]
end

#write(filename) ⇒ void

This method returns an undefined value.

Write all Mach-O data to the given filename.

Parameters:

  • filename (String)

    the file to write to



357
358
359
# File 'lib/macho/macho_file.rb', line 357

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

#write!void

This method returns an undefined value.

Write all Mach-O data to the file used to initialize the instance.

Raises:

  • (MachOError)

    if the instance was created from a binary string



364
365
366
367
368
369
370
# File 'lib/macho/macho_file.rb', line 364

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