class MachO::LoadCommands::LoadCommand
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.
Public Class Methods
Creates a new (viewless) command corresponding to the symbol provided @param cmd_sym [Symbol] the symbol of the load command being created @param args [Array] the arguments for the load command being created
# File lib/macho/load_commands.rb, line 241 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 # macOS 15 introduces a new dylib load command that adds a flags field to the end. # It uses the same commands with it dynamically being created if the dylib has a flags field if klass == DylibUseCommand && (args[1] != DYLIB_USE_MARKER || args.size <= DylibCommand.min_args - 3) klass = DylibCommand klass_arity = klass.min_args - 3 end raise LoadCommandCreationArityError.new(cmd_sym, klass_arity, args.size) if klass_arity > args.size klass.new(nil, cmd, nil, *args) end
Instantiates a new LoadCommand
given a view into its origin Mach-O @param view [MachO::MachOView] the load command’s raw view @return [LoadCommand] the new load command @api private
# File lib/macho/load_commands.rb, line 231 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
Public Instance Methods
@return [Integer] the load command’s offset in the source file @deprecated use {#view} instead
# File lib/macho/load_commands.rb, line 281 def offset view.offset end
@return [Boolean] whether the load command can be serialized
# File lib/macho/load_commands.rb, line 263 def serializable? CREATABLE_LOAD_COMMANDS.include?(LOAD_COMMANDS[cmd]) end
@param context [SerializationContext] the context
to serialize into
@return [String, nil] the serialized fields of the load command, or nil
if the load command can't be serialized
@api private
# File lib/macho/load_commands.rb, line 272 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
@return [Hash] a hash representation of this load command @note Children should override this to include additional information.
# File lib/macho/load_commands.rb, line 301 def to_h { "view" => view.to_h, "cmd" => cmd, "cmdsize" => cmdsize, "type" => type, }.merge super end
@return [String] a string representation of the load command’s
identifying number
# File lib/macho/load_commands.rb, line 295 def to_s type.to_s end
@return [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
# File lib/macho/load_commands.rb, line 287 def type LOAD_COMMANDS[cmd] end