Merge pull request #4285 from woodruffw/ruby-macho-1.2.0

vendor: Update ruby-macho to 1.2.0
This commit is contained in:
William Woodruff 2018-06-06 10:48:56 -04:00 committed by GitHub
commit e78158a4f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 254 additions and 165 deletions

View File

@ -3,7 +3,7 @@ Vendored Dependencies
* [plist](https://github.com/patsplat/plist), version 3.3.0
* [ruby-macho](https://github.com/Homebrew/ruby-macho), version 1.1.0
* [ruby-macho](https://github.com/Homebrew/ruby-macho), version 1.2.0
* [backports](https://github.com/marcandre/backports), version 3.8.0
@ -35,7 +35,7 @@ Vendored Dependencies
### ruby-macho
> The MIT License
> Copyright (c) 2015, 2016, 2017 William Woodruff <william @ tuffbizz.com>
> Copyright (c) 2015, 2016, 2017, 2018 William Woodruff <william @ yossarian.net>
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal

View File

@ -12,7 +12,7 @@ require "#{File.dirname(__FILE__)}/macho/tools"
# The primary namespace for ruby-macho.
module MachO
# release version
VERSION = "1.1.0".freeze
VERSION = "1.2.0".freeze
# Opens the given filename as a MachOFile or FatFile, depending on its magic.
# @param filename [String] the file being opened

View File

@ -10,7 +10,7 @@ module MachO
# Raised when a Mach-O file modification fails but can be recovered when
# operating on multiple Mach-O slices of a fat binary in non-strict mode.
class RecoverableModificationError < ModificationError
# @return [Fixnum, nil] The index of the Mach-O slice of a fat binary for
# @return [Integer, nil] The index of the Mach-O slice of a fat binary for
# which modification failed or `nil` if not a fat binary. This is used to
# make the error message more useful.
attr_accessor :macho_slice
@ -40,7 +40,7 @@ module MachO
# Raised when a file's magic bytes are not valid Mach-O magic.
class MagicError < NotAMachOError
# @param num [Fixnum] the unknown number
# @param num [Integer] the unknown number
def initialize(num)
super "Unrecognized Mach-O magic: 0x#{"%02x" % num}"
end
@ -69,7 +69,7 @@ module MachO
# Raised when the CPU type is unknown.
class CPUTypeError < MachOError
# @param cputype [Fixnum] the unknown CPU type
# @param cputype [Integer] the unknown CPU type
def initialize(cputype)
super "Unrecognized CPU type: 0x#{"%08x" % cputype}"
end
@ -77,8 +77,8 @@ module MachO
# Raised when the CPU type/sub-type pair is unknown.
class CPUSubtypeError < MachOError
# @param cputype [Fixnum] the CPU type of the unknown pair
# @param cpusubtype [Fixnum] the CPU sub-type of the unknown pair
# @param cputype [Integer] the CPU type of the unknown pair
# @param cpusubtype [Integer] the CPU sub-type of the unknown pair
def initialize(cputype, cpusubtype)
super "Unrecognized CPU sub-type: 0x#{"%08x" % cpusubtype}" \
" (for CPU type: 0x#{"%08x" % cputype})"
@ -87,7 +87,7 @@ module MachO
# Raised when a mach-o file's filetype field is unknown.
class FiletypeError < MachOError
# @param num [Fixnum] the unknown number
# @param num [Integer] the unknown number
def initialize(num)
super "Unrecognized Mach-O filetype code: 0x#{"%02x" % num}"
end
@ -95,7 +95,7 @@ module MachO
# Raised when an unknown load command is encountered.
class LoadCommandError < MachOError
# @param num [Fixnum] the unknown number
# @param num [Integer] the unknown number
def initialize(num)
super "Unrecognized Mach-O load command: 0x#{"%02x" % num}"
end
@ -113,8 +113,8 @@ module MachO
# is wrong.
class LoadCommandCreationArityError < MachOError
# @param cmd_sym [Symbol] the load command's symbol
# @param expected_arity [Fixnum] the number of arguments expected
# @param actual_arity [Fixnum] the number of arguments received
# @param expected_arity [Integer] the number of arguments expected
# @param actual_arity [Integer] the number of arguments received
def initialize(cmd_sym, expected_arity, actual_arity)
super "Expected #{expected_arity} arguments for #{cmd_sym} creation," \
" got #{actual_arity}"
@ -140,7 +140,7 @@ module MachO
# Raised when a change at an offset is not valid.
class OffsetInsertionError < ModificationError
# @param offset [Fixnum] the invalid offset
# @param offset [Integer] the invalid offset
def initialize(offset)
super "Insertion at offset #{offset} is not valid"
end

View File

@ -261,11 +261,8 @@ module MachO
# @raise [MachOError] if the instance was initialized without a file
# @note Overwrites all data in the file!
def write!
if filename.nil?
raise MachOError, "cannot write to a default file when initialized from a binary string"
else
File.open(@filename, "wb") { |f| f.write(@raw_data) }
end
raise MachOError, "no initial file to write to" if filename.nil?
File.open(@filename, "wb") { |f| f.write(@raw_data) }
end
private

View File

@ -450,10 +450,10 @@ module MachO
# Fat binary header structure
# @see MachO::FatArch
class FatHeader < MachOStructure
# @return [Fixnum] the magic number of the header (and file)
# @return [Integer] the magic number of the header (and file)
attr_reader :magic
# @return [Fixnum] the number of fat architecture structures following the header
# @return [Integer] the number of fat architecture structures following the header
attr_reader :nfat_arch
# always big-endian
@ -481,19 +481,19 @@ module MachO
# these, representing one or more internal Mach-O blobs.
# @see MachO::Headers::FatHeader
class FatArch < MachOStructure
# @return [Fixnum] the CPU type of the Mach-O
# @return [Integer] the CPU type of the Mach-O
attr_reader :cputype
# @return [Fixnum] the CPU subtype of the Mach-O
# @return [Integer] the CPU subtype of the Mach-O
attr_reader :cpusubtype
# @return [Fixnum] the file offset to the beginning of the Mach-O data
# @return [Integer] the file offset to the beginning of the Mach-O data
attr_reader :offset
# @return [Fixnum] the size, in bytes, of the Mach-O data
# @return [Integer] the size, in bytes, of the Mach-O data
attr_reader :size
# @return [Fixnum] the alignment, as a power of 2
# @return [Integer] the alignment, as a power of 2
attr_reader :align
# always big-endian
@ -522,25 +522,25 @@ module MachO
# 32-bit Mach-O file header structure
class MachHeader < MachOStructure
# @return [Fixnum] the magic number
# @return [Integer] the magic number
attr_reader :magic
# @return [Fixnum] the CPU type of the Mach-O
# @return [Integer] the CPU type of the Mach-O
attr_reader :cputype
# @return [Fixnum] the CPU subtype of the Mach-O
# @return [Integer] the CPU subtype of the Mach-O
attr_reader :cpusubtype
# @return [Fixnum] the file type of the Mach-O
# @return [Integer] the file type of the Mach-O
attr_reader :filetype
# @return [Fixnum] the number of load commands in the Mach-O
# @return [Integer] the number of load commands in the Mach-O
attr_reader :ncmds
# @return [Fixnum] the size of all load commands, in bytes, in the Mach-O
# @return [Integer] the size of all load commands, in bytes, in the Mach-O
attr_reader :sizeofcmds
# @return [Fixnum] the header flags associated with the Mach-O
# @return [Integer] the header flags associated with the Mach-O
attr_reader :flags
# @see MachOStructure::FORMAT
@ -635,7 +635,7 @@ module MachO
Utils.magic64?(magic)
end
# @return [Fixnum] the file's internal alignment
# @return [Integer] the file's internal alignment
def alignment
magic32? ? 4 : 8
end

View File

@ -58,6 +58,8 @@ module MachO
0x2e => :LC_LINKER_OPTIMIZATION_HINT,
0x2f => :LC_VERSION_MIN_TVOS,
0x30 => :LC_VERSION_MIN_WATCHOS,
0x31 => :LC_NOTE,
0x32 => :LC_BUILD_VERSION,
}.freeze
# association of symbol representations to load command constants
@ -66,20 +68,20 @@ module MachO
# load commands responsible for loading dylibs
# @api private
DYLIB_LOAD_COMMANDS = [
:LC_LOAD_DYLIB,
:LC_LOAD_WEAK_DYLIB,
:LC_REEXPORT_DYLIB,
:LC_LAZY_LOAD_DYLIB,
:LC_LOAD_UPWARD_DYLIB,
DYLIB_LOAD_COMMANDS = %i[
LC_LOAD_DYLIB
LC_LOAD_WEAK_DYLIB
LC_REEXPORT_DYLIB
LC_LAZY_LOAD_DYLIB
LC_LOAD_UPWARD_DYLIB
].freeze
# load commands that can be created manually via {LoadCommand.create}
# @api private
CREATABLE_LOAD_COMMANDS = DYLIB_LOAD_COMMANDS + [
:LC_ID_DYLIB,
:LC_RPATH,
:LC_LOAD_DYLINKER,
CREATABLE_LOAD_COMMANDS = DYLIB_LOAD_COMMANDS + %i[
LC_ID_DYLIB
LC_RPATH
LC_LOAD_DYLINKER
].freeze
# association of load command symbols to string representations of classes
@ -141,6 +143,8 @@ module MachO
:LC_LINKER_OPTIMIZATION_HINT => "LinkeditDataCommand",
:LC_VERSION_MIN_TVOS => "VersionMinCommand",
:LC_VERSION_MIN_WATCHOS => "VersionMinCommand",
:LC_NOTE => "LoadCommand",
:LC_BUILD_VERSION => "BuildVersionCommand",
}.freeze
# association of segment name symbols to names
@ -173,10 +177,10 @@ module MachO
# @return [MachO::MachOView] the raw view associated with the load command
attr_reader :view
# @return [Fixnum] the load command's identifying number
# @return [Integer] the load command's identifying number
attr_reader :cmd
# @return [Fixnum] the size of the load command, in bytes
# @return [Integer] the size of the load command, in bytes
attr_reader :cmdsize
# @see MachOStructure::FORMAT
@ -216,8 +220,8 @@ module MachO
end
# @param view [MachO::MachOView] the load command's raw view
# @param cmd [Fixnum] the load command's identifying number
# @param cmdsize [Fixnum] the size of the load command in bytes
# @param cmd [Integer] the load command's identifying number
# @param cmdsize [Integer] the size of the load command in bytes
# @api private
def initialize(view, cmd, cmdsize)
@view = view
@ -241,7 +245,7 @@ module MachO
[cmd, SIZEOF].pack(format)
end
# @return [Fixnum] the load command's offset in the source file
# @return [Integer] the load command's offset in the source file
# @deprecated use {#view} instead
def offset
view.offset
@ -267,7 +271,7 @@ module MachO
# explicit operations on the raw Mach-O data.
class LCStr
# @param lc [LoadCommand] the load command
# @param lc_str [Fixnum, String] the offset to the beginning of the
# @param lc_str [Integer, String] the offset to the beginning of the
# string, or the string itself if not being initialized with a view.
# @raise [MachO::LCStrMalformedError] if the string is malformed
# @todo devise a solution such that the `lc_str` parameter is not
@ -295,7 +299,7 @@ module MachO
@string
end
# @return [Fixnum] the offset to the beginning of the string in the
# @return [Integer] the offset to the beginning of the string in the
# load command
def to_i
@string_offset
@ -308,7 +312,7 @@ module MachO
# @return [Symbol] the endianness of the serialized load command
attr_reader :endianness
# @return [Fixnum] the constant alignment value used to pad the
# @return [Integer] the constant alignment value used to pad the
# serialized load command
attr_reader :alignment
@ -320,7 +324,7 @@ module MachO
end
# @param endianness [Symbol] the endianness of the context
# @param alignment [Fixnum] the alignment of the context
# @param alignment [Integer] the alignment of the context
# @api private
def initialize(endianness, alignment)
@endianness = endianness
@ -333,7 +337,7 @@ module MachO
# identifying an object produced by static link editor. Corresponds to
# LC_UUID.
class UUIDCommand < LoadCommand
# @return [Array<Fixnum>] the UUID
# @return [Array<Integer>] the UUID
attr_reader :uuid
# @see MachOStructure::FORMAT
@ -368,28 +372,28 @@ module MachO
# @return [String] the name of the segment
attr_reader :segname
# @return [Fixnum] the memory address of the segment
# @return [Integer] the memory address of the segment
attr_reader :vmaddr
# @return [Fixnum] the memory size of the segment
# @return [Integer] the memory size of the segment
attr_reader :vmsize
# @return [Fixnum] the file offset of the segment
# @return [Integer] the file offset of the segment
attr_reader :fileoff
# @return [Fixnum] the amount to map from the file
# @return [Integer] the amount to map from the file
attr_reader :filesize
# @return [Fixnum] the maximum VM protection
# @return [Integer] the maximum VM protection
attr_reader :maxprot
# @return [Fixnum] the initial VM protection
# @return [Integer] the initial VM protection
attr_reader :initprot
# @return [Fixnum] the number of sections in the segment
# @return [Integer] the number of sections in the segment
attr_reader :nsects
# @return [Fixnum] any flags associated with the segment
# @return [Integer] any flags associated with the segment
attr_reader :flags
# @see MachOStructure::FORMAT
@ -466,13 +470,13 @@ module MachO
# name as an LCStr
attr_reader :name
# @return [Fixnum] the library's build time stamp
# @return [Integer] the library's build time stamp
attr_reader :timestamp
# @return [Fixnum] the library's current version number
# @return [Integer] the library's current version number
attr_reader :current_version
# @return [Fixnum] the library's compatibility version number
# @return [Integer] the library's compatibility version number
attr_reader :compatibility_version
# @see MachOStructure::FORMAT
@ -551,10 +555,10 @@ module MachO
# name as an LCStr
attr_reader :name
# @return [Fixnum] the number of modules in the library
# @return [Integer] the number of modules in the library
attr_reader :nmodules
# @return [Fixnum] a bit vector of linked modules
# @return [Integer] a bit vector of linked modules
attr_reader :linked_modules
# @see MachOStructure::FORMAT
@ -591,10 +595,10 @@ module MachO
# initialization routine and an index into the module table for the module
# that defines the routine. Corresponds to LC_ROUTINES.
class RoutinesCommand < LoadCommand
# @return [Fixnum] the address of the initialization routine
# @return [Integer] the address of the initialization routine
attr_reader :init_address
# @return [Fixnum] the index into the module table that the init routine
# @return [Integer] the index into the module table that the init routine
# is defined in
attr_reader :init_module
@ -739,10 +743,10 @@ module MachO
# A load command containing the offsets and sizes of the link-edit 4.3BSD
# "stab" style symbol table information. Corresponds to LC_SYMTAB.
class SymtabCommand < LoadCommand
# @return [Fixnum] the symbol table's offset
# @return [Integer] the symbol table's offset
attr_reader :symoff
# @return [Fixnum] the number of symbol table entries
# @return [Integer] the number of symbol table entries
attr_reader :nsyms
# @return the string table's offset
@ -772,58 +776,58 @@ module MachO
# A load command containing symbolic information needed to support data
# structures used by the dynamic link editor. Corresponds to LC_DYSYMTAB.
class DysymtabCommand < LoadCommand
# @return [Fixnum] the index to local symbols
# @return [Integer] the index to local symbols
attr_reader :ilocalsym
# @return [Fixnum] the number of local symbols
# @return [Integer] the number of local symbols
attr_reader :nlocalsym
# @return [Fixnum] the index to externally defined symbols
# @return [Integer] the index to externally defined symbols
attr_reader :iextdefsym
# @return [Fixnum] the number of externally defined symbols
# @return [Integer] the number of externally defined symbols
attr_reader :nextdefsym
# @return [Fixnum] the index to undefined symbols
# @return [Integer] the index to undefined symbols
attr_reader :iundefsym
# @return [Fixnum] the number of undefined symbols
# @return [Integer] the number of undefined symbols
attr_reader :nundefsym
# @return [Fixnum] the file offset to the table of contents
# @return [Integer] the file offset to the table of contents
attr_reader :tocoff
# @return [Fixnum] the number of entries in the table of contents
# @return [Integer] the number of entries in the table of contents
attr_reader :ntoc
# @return [Fixnum] the file offset to the module table
# @return [Integer] the file offset to the module table
attr_reader :modtaboff
# @return [Fixnum] the number of entries in the module table
# @return [Integer] the number of entries in the module table
attr_reader :nmodtab
# @return [Fixnum] the file offset to the referenced symbol table
# @return [Integer] the file offset to the referenced symbol table
attr_reader :extrefsymoff
# @return [Fixnum] the number of entries in the referenced symbol table
# @return [Integer] the number of entries in the referenced symbol table
attr_reader :nextrefsyms
# @return [Fixnum] the file offset to the indirect symbol table
# @return [Integer] the file offset to the indirect symbol table
attr_reader :indirectsymoff
# @return [Fixnum] the number of entries in the indirect symbol table
# @return [Integer] the number of entries in the indirect symbol table
attr_reader :nindirectsyms
# @return [Fixnum] the file offset to the external relocation entries
# @return [Integer] the file offset to the external relocation entries
attr_reader :extreloff
# @return [Fixnum] the number of external relocation entries
# @return [Integer] the number of external relocation entries
attr_reader :nextrel
# @return [Fixnum] the file offset to the local relocation entries
# @return [Integer] the file offset to the local relocation entries
attr_reader :locreloff
# @return [Fixnum] the number of local relocation entries
# @return [Integer] the number of local relocation entries
attr_reader :nlocrel
# @see MachOStructure::FORMAT
@ -865,10 +869,10 @@ module MachO
# A load command containing the offset and number of hints in the two-level
# namespace lookup hints table. Corresponds to LC_TWOLEVEL_HINTS.
class TwolevelHintsCommand < LoadCommand
# @return [Fixnum] the offset to the hint table
# @return [Integer] the offset to the hint table
attr_reader :htoffset
# @return [Fixnum] the number of hints in the hint table
# @return [Integer] the number of hints in the hint table
attr_reader :nhints
# @return [TwolevelHintsTable]
@ -898,8 +902,8 @@ module MachO
attr_reader :hints
# @param view [MachO::MachOView] the view into the current Mach-O
# @param htoffset [Fixnum] the offset of the hints table
# @param nhints [Fixnum] the number of two-level hints in the table
# @param htoffset [Integer] the offset of the hints table
# @param nhints [Integer] the number of two-level hints in the table
# @api private
def initialize(view, htoffset, nhints)
format = Utils.specialize_format("L=#{nhints}", view.endianness)
@ -911,13 +915,13 @@ module MachO
# An individual two-level namespace lookup hint.
class TwolevelHint
# @return [Fixnum] the index into the sub-images
# @return [Integer] the index into the sub-images
attr_reader :isub_image
# @return [Fixnum] the index into the table of contents
# @return [Integer] the index into the table of contents
attr_reader :itoc
# @param blob [Fixnum] the 32-bit number containing the lookup hint
# @param blob [Integer] the 32-bit number containing the lookup hint
# @api private
def initialize(blob)
@isub_image = blob >> 24
@ -930,7 +934,7 @@ module MachO
# A load command containing the value of the original checksum for prebound
# files, or zero. Corresponds to LC_PREBIND_CKSUM.
class PrebindCksumCommand < LoadCommand
# @return [Fixnum] the checksum or 0
# @return [Integer] the checksum or 0
attr_reader :cksum
# @see MachOStructure::FORMAT
@ -987,10 +991,10 @@ module MachO
# LC_SEGMENT_SPLIT_INFO, LC_FUNCTION_STARTS, LC_DATA_IN_CODE,
# LC_DYLIB_CODE_SIGN_DRS, and LC_LINKER_OPTIMIZATION_HINT.
class LinkeditDataCommand < LoadCommand
# @return [Fixnum] offset to the data in the __LINKEDIT segment
# @return [Integer] offset to the data in the __LINKEDIT segment
attr_reader :dataoff
# @return [Fixnum] size of the data in the __LINKEDIT segment
# @return [Integer] size of the data in the __LINKEDIT segment
attr_reader :datasize
# @see MachOStructure::FORMAT
@ -1012,13 +1016,13 @@ module MachO
# A load command representing the offset to and size of an encrypted
# segment. Corresponds to LC_ENCRYPTION_INFO.
class EncryptionInfoCommand < LoadCommand
# @return [Fixnum] the offset to the encrypted segment
# @return [Integer] the offset to the encrypted segment
attr_reader :cryptoff
# @return [Fixnum] the size of the encrypted segment
# @return [Integer] the size of the encrypted segment
attr_reader :cryptsize
# @return [Fixnum] the encryption system, or 0 if not encrypted yet
# @return [Integer] the encryption system, or 0 if not encrypted yet
attr_reader :cryptid
# @see MachOStructure::FORMAT
@ -1041,16 +1045,16 @@ module MachO
# A load command representing the offset to and size of an encrypted
# segment. Corresponds to LC_ENCRYPTION_INFO_64.
class EncryptionInfoCommand64 < LoadCommand
# @return [Fixnum] the offset to the encrypted segment
# @return [Integer] the offset to the encrypted segment
attr_reader :cryptoff
# @return [Fixnum] the size of the encrypted segment
# @return [Integer] the size of the encrypted segment
attr_reader :cryptsize
# @return [Fixnum] the encryption system, or 0 if not encrypted yet
# @return [Integer] the encryption system, or 0 if not encrypted yet
attr_reader :cryptid
# @return [Fixnum] 64-bit padding value
# @return [Integer] 64-bit padding value
attr_reader :pad
# @see MachOStructure::FORMAT
@ -1075,10 +1079,10 @@ module MachO
# was built to run. Corresponds to LC_VERSION_MIN_MACOSX and
# LC_VERSION_MIN_IPHONEOS.
class VersionMinCommand < LoadCommand
# @return [Fixnum] the version X.Y.Z packed as x16.y8.z8
# @return [Integer] the version X.Y.Z packed as x16.y8.z8
attr_reader :version
# @return [Fixnum] the SDK version X.Y.Z packed as x16.y8.z8
# @return [Integer] the SDK version X.Y.Z packed as x16.y8.z8
attr_reader :sdk
# @see MachOStructure::FORMAT
@ -1119,38 +1123,129 @@ module MachO
end
end
# A load command containing the minimum OS version on which
# the binary was built for its platform.
# Corresponds to LC_BUILD_VERSION.
class BuildVersionCommand < LoadCommand
# @return [Integer]
attr_reader :platform
# @return [Integer] the minimum OS version X.Y.Z packed as x16.y8.z8
attr_reader :minos
# @return [Integer] the SDK version X.Y.Z packed as x16.y8.z8
attr_reader :sdk
# @return [ToolEntries] tool entries
attr_reader :tool_entries
# @see MachOStructure::FORMAT
# @api private
FORMAT = "L=6".freeze
# @see MachOStructure::SIZEOF
# @api private
SIZEOF = 24
# @api private
def initialize(view, cmd, cmdsize, platform, minos, sdk, ntools)
super(view, cmd, cmdsize)
@platform = platform
@minos = minos
@sdk = sdk
@tool_entries = ToolEntries.new(view, ntools)
end
# A representation of the tool versions exposed
# by a {BuildVersionCommand} (`LC_BUILD_VERSION`).
class ToolEntries
# @return [Array<Tool>] all tools
attr_reader :tools
# @param view [MachO::MachOView] the view into the current Mach-O
# @param ntools [Integer] the number of tools
# @api private
def initialize(view, ntools)
format = Utils.specialize_format("L=#{ntools * 2}", view.endianness)
raw_table = view.raw_data[view.offset + 24, ntools * 8]
blobs = raw_table.unpack(format).each_slice(2).to_a
@tools = blobs.map { |b| Tool.new(*b) }
end
# An individual tool.
class Tool
# @return [Integer] the enum for the tool
attr_reader :tool
# @return [Integer] the tool's version number
attr_reader :version
# @param tool 32-bit integer
# # @param version 32-bit integer
# @api private
def initialize(tool, version)
@tool = tool
@version = version
end
end
end
# A string representation of the binary's minimum OS version.
# @return [String] a string representing the minimum OS version.
def minos_string
binary = "%032b" % minos
segs = [
binary[0..15], binary[16..23], binary[24..31]
].map { |s| s.to_i(2) }
segs.join(".")
end
# A string representation of the binary's SDK version.
# @return [String] a string representing the SDK version.
def sdk_string
binary = "%032b" % sdk
segs = [
binary[0..15], binary[16..23], binary[24..31]
].map { |s| s.to_i(2) }
segs.join(".")
end
end
# A load command containing the file offsets and sizes of the new
# compressed form of the information dyld needs to load the image.
# Corresponds to LC_DYLD_INFO and LC_DYLD_INFO_ONLY.
class DyldInfoCommand < LoadCommand
# @return [Fixnum] the file offset to the rebase information
# @return [Integer] the file offset to the rebase information
attr_reader :rebase_off
# @return [Fixnum] the size of the rebase information
# @return [Integer] the size of the rebase information
attr_reader :rebase_size
# @return [Fixnum] the file offset to the binding information
# @return [Integer] the file offset to the binding information
attr_reader :bind_off
# @return [Fixnum] the size of the binding information
# @return [Integer] the size of the binding information
attr_reader :bind_size
# @return [Fixnum] the file offset to the weak binding information
# @return [Integer] the file offset to the weak binding information
attr_reader :weak_bind_off
# @return [Fixnum] the size of the weak binding information
# @return [Integer] the size of the weak binding information
attr_reader :weak_bind_size
# @return [Fixnum] the file offset to the lazy binding information
# @return [Integer] the file offset to the lazy binding information
attr_reader :lazy_bind_off
# @return [Fixnum] the size of the lazy binding information
# @return [Integer] the size of the lazy binding information
attr_reader :lazy_bind_size
# @return [Fixnum] the file offset to the export information
# @return [Integer] the file offset to the export information
attr_reader :export_off
# @return [Fixnum] the size of the export information
# @return [Integer] the size of the export information
attr_reader :export_size
# @see MachOStructure::FORMAT
@ -1182,7 +1277,7 @@ module MachO
# A load command containing linker options embedded in object files.
# Corresponds to LC_LINKER_OPTION.
class LinkerOptionCommand < LoadCommand
# @return [Fixnum] the number of strings
# @return [Integer] the number of strings
attr_reader :count
# @see MachOStructure::FORMAT
@ -1202,10 +1297,10 @@ module MachO
# A load command specifying the offset of main(). Corresponds to LC_MAIN.
class EntryPointCommand < LoadCommand
# @return [Fixnum] the file (__TEXT) offset of main()
# @return [Integer] the file (__TEXT) offset of main()
attr_reader :entryoff
# @return [Fixnum] if not 0, the initial stack size.
# @return [Integer] if not 0, the initial stack size.
attr_reader :stacksize
# @see MachOStructure::FORMAT
@ -1227,7 +1322,7 @@ module MachO
# A load command specifying the version of the sources used to build the
# binary. Corresponds to LC_SOURCE_VERSION.
class SourceVersionCommand < LoadCommand
# @return [Fixnum] the version packed as a24.b10.c10.d10.e10
# @return [Integer] the version packed as a24.b10.c10.d10.e10
attr_reader :version
# @see MachOStructure::FORMAT
@ -1260,10 +1355,10 @@ module MachO
# An obsolete load command containing the offset and size of the (GNU style)
# symbol table information. Corresponds to LC_SYMSEG.
class SymsegCommand < LoadCommand
# @return [Fixnum] the offset to the symbol segment
# @return [Integer] the offset to the symbol segment
attr_reader :offset
# @return [Fixnum] the size of the symbol segment in bytes
# @return [Integer] the size of the symbol segment in bytes
attr_reader :size
# @see MachOStructure::FORMAT
@ -1301,7 +1396,7 @@ module MachO
# @return [LCStr] the pathname of the file being loaded
attr_reader :name
# @return [Fixnum] the virtual address being loaded at
# @return [Integer] the virtual address being loaded at
attr_reader :header_addr
# @see MachOStructure::FORMAT
@ -1325,10 +1420,10 @@ module MachO
# @return [LCStr] the library's target pathname
attr_reader :name
# @return [Fixnum] the library's minor version number
# @return [Integer] the library's minor version number
attr_reader :minor_version
# @return [Fixnum] the library's header address
# @return [Integer] the library's header address
attr_reader :header_addr
# @see MachOStructure::FORMAT

View File

@ -134,7 +134,7 @@ module MachO
alias [] command
# Inserts a load command at the given offset.
# @param offset [Fixnum] the offset to insert at
# @param offset [Integer] the offset to insert at
# @param lc [LoadCommands::LoadCommand] the load command to insert
# @param options [Hash]
# @option options [Boolean] :repopulate (true) whether or not to repopulate
@ -404,11 +404,8 @@ module MachO
# @raise [MachOError] if the instance was initialized without a file
# @note Overwrites all data in the file!
def write!
if @filename.nil?
raise MachOError, "cannot write to a default file when initialized from a binary string"
else
File.open(@filename, "wb") { |f| f.write(@raw_data) }
end
raise MachOError, "no initial file to write to" if @filename.nil?
File.open(@filename, "wb") { |f| f.write(@raw_data) }
end
private
@ -434,7 +431,7 @@ module MachO
end
# Read just the file's magic number and check its validity.
# @return [Fixnum] the magic
# @return [Integer] the magic
# @raise [MagicError] if the magic is not valid Mach-O magic
# @raise [FatBinaryError] if the magic is for a Fat file
# @api private
@ -450,7 +447,7 @@ module MachO
end
# Check the file's CPU type.
# @param cputype [Fixnum] the CPU type
# @param cputype [Integer] the CPU type
# @raise [CPUTypeError] if the CPU type is unknown
# @api private
def check_cputype(cputype)
@ -458,7 +455,7 @@ module MachO
end
# Check the file's CPU type/subtype pair.
# @param cpusubtype [Fixnum] the CPU subtype
# @param cpusubtype [Integer] the CPU subtype
# @raise [CPUSubtypeError] if the CPU sub-type is unknown
# @api private
def check_cpusubtype(cputype, cpusubtype)
@ -467,7 +464,7 @@ module MachO
end
# Check the file's type.
# @param filetype [Fixnum] the file type
# @param filetype [Integer] the file type
# @raise [FiletypeError] if the file type is unknown
# @api private
def check_filetype(filetype)
@ -503,7 +500,7 @@ module MachO
end
# The low file offset (offset to first section data).
# @return [Fixnum] the offset
# @return [Integer] the offset
# @api private
def low_fileoff
offset = @raw_data.size
@ -523,7 +520,7 @@ module MachO
end
# Updates the number of load commands in the raw data.
# @param ncmds [Fixnum] the new number of commands
# @param ncmds [Integer] the new number of commands
# @return [void]
# @api private
def update_ncmds(ncmds)
@ -533,7 +530,7 @@ module MachO
end
# Updates the size of all load commands in the raw data.
# @param size [Fixnum] the new size, in bytes
# @param size [Integer] the new size, in bytes
# @return [void]
# @api private
def update_sizeofcmds(size)

View File

@ -76,25 +76,25 @@ module MachO
# pad bytes
attr_reader :segname
# @return [Fixnum] the memory address of the section
# @return [Integer] the memory address of the section
attr_reader :addr
# @return [Fixnum] the size, in bytes, of the section
# @return [Integer] the size, in bytes, of the section
attr_reader :size
# @return [Fixnum] the file offset of the section
# @return [Integer] the file offset of the section
attr_reader :offset
# @return [Fixnum] the section alignment (power of 2) of the section
# @return [Integer] the section alignment (power of 2) of the section
attr_reader :align
# @return [Fixnum] the file offset of the section's relocation entries
# @return [Integer] the file offset of the section's relocation entries
attr_reader :reloff
# @return [Fixnum] the number of relocation entries
# @return [Integer] the number of relocation entries
attr_reader :nreloc
# @return [Fixnum] flags for type and attributes of the section
# @return [Integer] flags for type and attributes of the section
attr_reader :flags
# @return [void] reserved (for offset or index)

View File

@ -8,11 +8,11 @@ module MachO
FORMAT = "".freeze
# The size of the data structure, in bytes.
# @return [Fixnum] the size, in bytes
# @return [Integer] the size, in bytes
# @api private
SIZEOF = 0
# @return [Fixnum] the size, in bytes, of the represented structure.
# @return [Integer] the size, in bytes, of the represented structure.
def self.bytesize
self::SIZEOF
end

View File

@ -88,7 +88,7 @@ module MachO
# Merge multiple Mach-Os into one universal (Fat) binary.
# @param filename [String] the fat binary to create
# @param files [Array<MachO::MachOFile, MachO::FatFile>] the files to merge
# @param files [Array<String>] the files to merge
# @return [void]
def self.merge_machos(filename, *files)
machos = files.map do |file|

View File

@ -2,9 +2,9 @@ module MachO
# A collection of utility functions used throughout ruby-macho.
module Utils
# Rounds a value to the next multiple of the given round.
# @param value [Fixnum] the number being rounded
# @param round [Fixnum] the number being rounded with
# @return [Fixnum] the rounded value
# @param value [Integer] the number being rounded
# @param round [Integer] the number being rounded with
# @return [Integer] the rounded value
# @see http://www.opensource.apple.com/source/cctools/cctools-870/libstuff/rnd.c
def self.round(value, round)
round -= 1
@ -15,9 +15,9 @@ module MachO
# Returns the number of bytes needed to pad the given size to the given
# alignment.
# @param size [Fixnum] the unpadded size
# @param alignment [Fixnum] the number to alignment the size with
# @return [Fixnum] the number of pad bytes required
# @param size [Integer] the unpadded size
# @param alignment [Integer] the number to alignment the size with
# @return [Integer] the number of pad bytes required
def self.padding_for(size, alignment)
round(size, alignment) - size
end
@ -33,9 +33,9 @@ module MachO
end
# Packs tagged strings into an aligned payload.
# @param fixed_offset [Fixnum] the baseline offset for the first packed
# @param fixed_offset [Integer] the baseline offset for the first packed
# string
# @param alignment [Fixnum] the alignment value to use for packing
# @param alignment [Integer] the alignment value to use for packing
# @param strings [Hash] the labeled strings to pack
# @return [Array<String, Hash>] the packed string and labeled offsets
def self.pack_strings(fixed_offset, alignment, strings = {})
@ -55,42 +55,42 @@ module MachO
end
# Compares the given number to valid Mach-O magic numbers.
# @param num [Fixnum] the number being checked
# @param num [Integer] the number being checked
# @return [Boolean] whether `num` is a valid Mach-O magic number
def self.magic?(num)
Headers::MH_MAGICS.key?(num)
end
# Compares the given number to valid Fat magic numbers.
# @param num [Fixnum] the number being checked
# @param num [Integer] the number being checked
# @return [Boolean] whether `num` is a valid Fat magic number
def self.fat_magic?(num)
num == Headers::FAT_MAGIC
end
# Compares the given number to valid 32-bit Mach-O magic numbers.
# @param num [Fixnum] the number being checked
# @param num [Integer] the number being checked
# @return [Boolean] whether `num` is a valid 32-bit magic number
def self.magic32?(num)
num == Headers::MH_MAGIC || num == Headers::MH_CIGAM
end
# Compares the given number to valid 64-bit Mach-O magic numbers.
# @param num [Fixnum] the number being checked
# @param num [Integer] the number being checked
# @return [Boolean] whether `num` is a valid 64-bit magic number
def self.magic64?(num)
num == Headers::MH_MAGIC_64 || num == Headers::MH_CIGAM_64
end
# Compares the given number to valid little-endian magic numbers.
# @param num [Fixnum] the number being checked
# @param num [Integer] the number being checked
# @return [Boolean] whether `num` is a valid little-endian magic number
def self.little_magic?(num)
num == Headers::MH_CIGAM || num == Headers::MH_CIGAM_64
end
# Compares the given number to valid big-endian magic numbers.
# @param num [Fixnum] the number being checked
# @param num [Integer] the number being checked
# @return [Boolean] whether `num` is a valid big-endian magic number
def self.big_magic?(num)
num == Headers::MH_CIGAM || num == Headers::MH_CIGAM_64

View File

@ -7,13 +7,13 @@ module MachO
# @return [Symbol] the endianness of the data (`:big` or `:little`)
attr_reader :endianness
# @return [Fixnum] the offset of the relevant data (in {#raw_data})
# @return [Integer] the offset of the relevant data (in {#raw_data})
attr_reader :offset
# Creates a new MachOView.
# @param raw_data [String] the raw Mach-O data
# @param endianness [Symbol] the endianness of the data
# @param offset [Fixnum] the offset of the relevant data
# @param offset [Integer] the offset of the relevant data
def initialize(raw_data, endianness, offset)
@raw_data = raw_data
@endianness = endianness