Class: MachO::LoadCommands::LoadCommand

Inherits:
MachOStructure show all
Defined in:
lib/macho/load_commands.rb

Overview

The top-level Mach-O load command structure.

This is the most generic load command -- only the type ID and size are represented. Used when a more specific class isn't available or isn't implemented.

Defined Under Namespace

Classes: LCStr, SerializationContext

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MachOStructure

bytesize, format, #initialize

Constructor Details

This class inherits a constructor from MachO::MachOStructure

Class Method Details

.create(cmd_sym, *args) ⇒ Object

Creates a new (viewless) command corresponding to the symbol provided

Parameters:

  • cmd_sym (Symbol)

    the symbol of the load command being created

  • args (Array)

    the arguments for the load command being created

Raises:



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/macho/load_commands.rb', line 225

def self.create(cmd_sym, *args)
  raise LoadCommandNotCreatableError, cmd_sym unless CREATABLE_LOAD_COMMANDS.include?(cmd_sym)

  klass = LoadCommands.const_get LC_STRUCTURES[cmd_sym]
  cmd = LOAD_COMMAND_CONSTANTS[cmd_sym]

  # cmd will be filled in, view and cmdsize will be left unpopulated
  klass_arity = klass.min_args - 3

  raise LoadCommandCreationArityError.new(cmd_sym, klass_arity, args.size) if klass_arity > args.size

  klass.new(nil, cmd, nil, *args)
end

.new_from_bin(view) ⇒ LoadCommand

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.

Instantiates a new LoadCommand given a view into its origin Mach-O

Parameters:

Returns:



215
216
217
218
219
220
# File 'lib/macho/load_commands.rb', line 215

def self.new_from_bin(view)
  bin = view.raw_data.slice(view.offset, bytesize)
  format = Utils.specialize_format(self.format, view.endianness)

  new(view, *bin.unpack(format))
end

Instance Method Details

#cmdInteger

Returns the load command's type ID.

Returns:

  • (Integer)

    the load command's type ID



206
# File 'lib/macho/load_commands.rb', line 206

field :cmd, :uint32

#cmdsizeInteger

Returns the size of the load command, in bytes.

Returns:

  • (Integer)

    the size of the load command, in bytes



209
# File 'lib/macho/load_commands.rb', line 209

field :cmdsize, :uint32

#offsetInteger

Deprecated.

use #view instead

Returns the load command's offset in the source file.

Returns:

  • (Integer)

    the load command's offset in the source file



258
259
260
# File 'lib/macho/load_commands.rb', line 258

def offset
  view.offset
end

#serializable?Boolean

Returns whether the load command can be serialized.

Returns:

  • (Boolean)

    whether the load command can be serialized



240
241
242
# File 'lib/macho/load_commands.rb', line 240

def serializable?
  CREATABLE_LOAD_COMMANDS.include?(LOAD_COMMANDS[cmd])
end

#serialize(context) ⇒ String?

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.

Returns the serialized fields of the load command, or nil if the load command can't be serialized.

Parameters:

Returns:

  • (String, nil)

    the serialized fields of the load command, or nil if the load command can't be serialized

Raises:



249
250
251
252
253
254
# File 'lib/macho/load_commands.rb', line 249

def serialize(context)
  raise LoadCommandNotSerializableError, LOAD_COMMANDS[cmd] unless serializable?

  format = Utils.specialize_format(self.class.format, context.endianness)
  [cmd, self.class.bytesize].pack(format)
end

#to_hHash

Note:

Children should override this to include additional information.

Returns a hash representation of this load command.

Returns:

  • (Hash)

    a hash representation of this load command



278
279
280
281
282
283
284
285
# File 'lib/macho/load_commands.rb', line 278

def to_h
  {
    "view" => view.to_h,
    "cmd" => cmd,
    "cmdsize" => cmdsize,
    "type" => type,
  }.merge super
end

#to_sString

Returns a string representation of the load command's identifying number.

Returns:

  • (String)

    a string representation of the load command's identifying number



272
273
274
# File 'lib/macho/load_commands.rb', line 272

def to_s
  type.to_s
end

#typeSymbol? Also known as: to_sym

Returns a symbol representation of the load command's type ID, or nil if the ID doesn't correspond to a known load command class.

Returns:

  • (Symbol, nil)

    a symbol representation of the load command's type ID, or nil if the ID doesn't correspond to a known load command class



264
265
266
# File 'lib/macho/load_commands.rb', line 264

def type
  LOAD_COMMANDS[cmd]
end

#viewMachO::MachOView?

Returns the raw view associated with the load command, or nil if the load command was created via create.

Returns:

  • (MachO::MachOView, nil)

    the raw view associated with the load command, or nil if the load command was created via create.



203
# File 'lib/macho/load_commands.rb', line 203

field :view, :view