Module: MachO

Defined in:
lib/macho.rb,
lib/macho/view.rb,
lib/macho/tools.rb,
lib/macho/utils.rb,
lib/macho/headers.rb,
lib/macho/fat_file.rb,
lib/macho/sections.rb,
lib/macho/structure.rb,
lib/macho/exceptions.rb,
lib/macho/macho_file.rb,
lib/macho/load_commands.rb

Overview

The primary namespace for ruby-macho.

Defined Under Namespace

Modules: Headers, LoadCommands, Sections, Tools, Utils Classes: CPUSubtypeError, CPUTypeError, CodeSigningError, DylibIdMissingError, DylibUnknownError, FatArchOffsetOverflowError, FatBinaryError, FatFile, FiletypeError, HeaderPadError, JavaClassFileError, LCStrMalformedError, LoadCommandCreationArityError, LoadCommandError, LoadCommandNotCreatableError, LoadCommandNotSerializableError, MachOBinaryError, MachOError, MachOFile, MachOStructure, MachOView, MagicError, ModificationError, NotAMachOError, OffsetInsertionError, RecoverableModificationError, RpathExistsError, RpathUnknownError, TruncatedFileError, UnimplementedError

Constant Summary collapse

VERSION =

release version

"2.5.1"

Class Method Summary collapse

Class Method Details

.codesign!(filename) ⇒ void

This method returns an undefined value.

Signs the dylib using an ad-hoc identity. Necessary after making any changes to a dylib, since otherwise changing a signed file invalidates its signature.

Parameters:

  • filename (String)

    the file being opened

Raises:



51
52
53
54
55
56
57
58
59
60
# File 'lib/macho.rb', line 51

def self.codesign!(filename)
  raise ArgumentError, "codesign binary is not available on Linux" if RUBY_PLATFORM !~ /darwin/
  raise ArgumentError, "#{filename}: no such file" unless File.file?(filename)

  _, _, status = Open3.capture3("codesign", "--sign", "-", "--force",
                                "--preserve-metadata=entitlements,requirements,flags,runtime",
                                filename)

  raise CodeSigningError, "#{filename}: signing failed!" unless status.success?
end

.open(filename) ⇒ MachOFile, FatFile

Opens the given filename as a MachOFile or FatFile, depending on its magic.

Parameters:

  • filename (String)

    the file being opened

Returns:

  • (MachOFile)

    if the file is a Mach-O

  • (FatFile)

    if the file is a Fat file

Raises:

  • (ArgumentError)

    if the given file does not exist

  • (TruncatedFileError)

    if the file is too small to have a valid header

  • (MagicError)

    if the file's magic is not valid Mach-O magic



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/macho.rb', line 28

def self.open(filename)
  raise ArgumentError, "#{filename}: no such file" unless File.file?(filename)
  raise TruncatedFileError unless File.stat(filename).size >= 4

  magic = File.open(filename, "rb") { |f| f.read(4) }.unpack1("N")

  if Utils.fat_magic?(magic)
    file = FatFile.new(filename)
  elsif Utils.magic?(magic)
    file = MachOFile.new(filename)
  else
    raise MagicError, magic
  end

  file
end