Class: MachO::LoadCommand

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

Overview

Mach-O load command structure This is the most generic load command - only cmd ID and size are represented, and no actual data. Used when a more specific class isn't available/implemented.

Defined Under Namespace

Classes: LCStr, SerializationContext

Constant Summary collapse

FORMAT =

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

"L=2".freeze
SIZEOF =

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

8

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MachOStructure

bytesize

Constructor Details

#initialize(view, cmd, cmdsize) ⇒ 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.

Returns a new instance of LoadCommand.

Parameters:

  • view (MachO::MachOView)

    the load command's raw view

  • cmd (Fixnum)

    the load command's identifying number

  • cmdsize (Fixnum)

    the size of the load command in bytes



213
214
215
216
217
# File 'lib/macho/load_commands.rb', line 213

def initialize(view, cmd, cmdsize)
  @view = view
  @cmd = cmd
  @cmdsize = cmdsize
end

Instance Attribute Details

#cmdFixnum (readonly)

Returns the load command's identifying number.

Returns:

  • (Fixnum)

    the load command's identifying number



168
169
170
# File 'lib/macho/load_commands.rb', line 168

def cmd
  @cmd
end

#cmdsizeFixnum (readonly)

Returns the size of the load command, in bytes.

Returns:

  • (Fixnum)

    the size of the load command, in bytes



171
172
173
# File 'lib/macho/load_commands.rb', line 171

def cmdsize
  @cmdsize
end

#viewMachO::MachOView (readonly)

Returns the raw view associated with the load command.

Returns:



165
166
167
# File 'lib/macho/load_commands.rb', line 165

def view
  @view
end

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:



195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/macho/load_commands.rb', line 195

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

  klass = MachO.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.instance_method(:initialize).arity - 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) ⇒ MachO::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:



185
186
187
188
189
190
# File 'lib/macho/load_commands.rb', line 185

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

#offsetFixnum

Deprecated.

use #view instead

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

Returns:

  • (Fixnum)

    the load command's offset in the source file



237
238
239
# File 'lib/macho/load_commands.rb', line 237

def offset
  view.offset
end

#serializable?Boolean

Returns true if the load command can be serialized, false otherwise.

Returns:

  • (Boolean)

    true if the load command can be serialized, false otherwise



220
221
222
# File 'lib/macho/load_commands.rb', line 220

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:



229
230
231
232
233
# File 'lib/macho/load_commands.rb', line 229

def serialize(context)
  raise LoadCommandNotSerializableError, LOAD_COMMANDS[cmd] unless serializable?
  format = Utils.specialize_format(FORMAT, context.endianness)
  [cmd, SIZEOF].pack(format)
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



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

def to_s
  type.to_s
end

#typeSymbol Also known as: to_sym

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

Returns:

  • (Symbol)

    a symbol representation of the load command's identifying number



242
243
244
# File 'lib/macho/load_commands.rb', line 242

def type
  LOAD_COMMANDS[cmd]
end