Class: MachO::Headers::MachHeader

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

Overview

32-bit Mach-O file header structure

Direct Known Subclasses

MachHeader64

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=7"
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.

28

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from MachOStructure

bytesize, new_from_bin

Constructor Details

#initialize(magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds, flags) ⇒ MachHeader

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 MachHeader.



672
673
674
675
676
677
678
679
680
681
682
683
684
# File 'lib/macho/headers.rb', line 672

def initialize(magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds,
               flags)
  super()
  @magic = magic
  @cputype = cputype
  # For now we're not interested in additional capability bits also to be
  # found in the `cpusubtype` field. We only care about the CPU sub-type.
  @cpusubtype = cpusubtype & ~CPU_SUBTYPE_MASK
  @filetype = filetype
  @ncmds = ncmds
  @sizeofcmds = sizeofcmds
  @flags = flags
end

Instance Attribute Details

#cpusubtypeInteger (readonly)

Returns the CPU subtype of the Mach-O.

Returns:

  • (Integer)

    the CPU subtype of the Mach-O



649
650
651
# File 'lib/macho/headers.rb', line 649

def cpusubtype
  @cpusubtype
end

#cputypeInteger (readonly)

Returns the CPU type of the Mach-O.

Returns:

  • (Integer)

    the CPU type of the Mach-O



646
647
648
# File 'lib/macho/headers.rb', line 646

def cputype
  @cputype
end

#filetypeInteger (readonly)

Returns the file type of the Mach-O.

Returns:

  • (Integer)

    the file type of the Mach-O



652
653
654
# File 'lib/macho/headers.rb', line 652

def filetype
  @filetype
end

#flagsInteger (readonly)

Returns the header flags associated with the Mach-O.

Returns:

  • (Integer)

    the header flags associated with the Mach-O



661
662
663
# File 'lib/macho/headers.rb', line 661

def flags
  @flags
end

#magicInteger (readonly)

Returns the magic number.

Returns:

  • (Integer)

    the magic number



643
644
645
# File 'lib/macho/headers.rb', line 643

def magic
  @magic
end

#ncmdsInteger (readonly)

Returns the number of load commands in the Mach-O.

Returns:

  • (Integer)

    the number of load commands in the Mach-O



655
656
657
# File 'lib/macho/headers.rb', line 655

def ncmds
  @ncmds
end

#sizeofcmdsInteger (readonly)

Returns the size of all load commands, in bytes, in the Mach-O.

Returns:

  • (Integer)

    the size of all load commands, in bytes, in the Mach-O



658
659
660
# File 'lib/macho/headers.rb', line 658

def sizeofcmds
  @sizeofcmds
end

Instance Method Details

#alignmentInteger

Returns the file's internal alignment.

Returns:

  • (Integer)

    the file's internal alignment



764
765
766
# File 'lib/macho/headers.rb', line 764

def alignment
  magic32? ? 4 : 8
end

#bundle?Boolean

Returns whether or not the file is of type MH_BUNDLE.

Returns:

  • (Boolean)

    whether or not the file is of type MH_BUNDLE



734
735
736
# File 'lib/macho/headers.rb', line 734

def bundle?
  filetype == Headers::MH_BUNDLE
end

#core?Boolean

Returns whether or not the file is of type MH_CORE.

Returns:

  • (Boolean)

    whether or not the file is of type MH_CORE



714
715
716
# File 'lib/macho/headers.rb', line 714

def core?
  filetype == Headers::MH_CORE
end

#dsym?Boolean

Returns whether or not the file is of type MH_DSYM.

Returns:

  • (Boolean)

    whether or not the file is of type MH_DSYM



739
740
741
# File 'lib/macho/headers.rb', line 739

def dsym?
  filetype == Headers::MH_DSYM
end

#dylib?Boolean

Returns whether or not the file is of type MH_DYLIB.

Returns:

  • (Boolean)

    whether or not the file is of type MH_DYLIB



724
725
726
# File 'lib/macho/headers.rb', line 724

def dylib?
  filetype == Headers::MH_DYLIB
end

#dylinker?Boolean

Returns whether or not the file is of type MH_DYLINKER.

Returns:

  • (Boolean)

    whether or not the file is of type MH_DYLINKER



729
730
731
# File 'lib/macho/headers.rb', line 729

def dylinker?
  filetype == Headers::MH_DYLINKER
end

#executable?Boolean

Returns whether or not the file is of type MH_EXECUTE.

Returns:

  • (Boolean)

    whether or not the file is of type MH_EXECUTE



704
705
706
# File 'lib/macho/headers.rb', line 704

def executable?
  filetype == Headers::MH_EXECUTE
end

#fileset?Boolean

Returns whether or not the file is of type MH_FILESET.

Returns:

  • (Boolean)

    whether or not the file is of type MH_FILESET



749
750
751
# File 'lib/macho/headers.rb', line 749

def fileset?
  filetype == Headers::MH_FILESET
end

#flag?(flag) ⇒ Boolean

Returns true if flag is present in the header's flag section.

Examples:

puts "this mach-o has position-independent execution" if header.flag?(:MH_PIE)

Parameters:

  • flag (Symbol)

    a mach header flag symbol

Returns:

  • (Boolean)

    true if flag is present in the header's flag section



690
691
692
693
694
695
696
# File 'lib/macho/headers.rb', line 690

def flag?(flag)
  flag = MH_FLAGS[flag]

  return false if flag.nil?

  flags & flag == flag
end

#fvmlib?Boolean

Returns whether or not the file is of type MH_FVMLIB.

Returns:

  • (Boolean)

    whether or not the file is of type MH_FVMLIB



709
710
711
# File 'lib/macho/headers.rb', line 709

def fvmlib?
  filetype == Headers::MH_FVMLIB
end

#kext?Boolean

Returns whether or not the file is of type MH_KEXT_BUNDLE.

Returns:

  • (Boolean)

    whether or not the file is of type MH_KEXT_BUNDLE



744
745
746
# File 'lib/macho/headers.rb', line 744

def kext?
  filetype == Headers::MH_KEXT_BUNDLE
end

#magic32?Boolean

Returns true if the Mach-O has 32-bit magic, false otherwise.

Returns:

  • (Boolean)

    true if the Mach-O has 32-bit magic, false otherwise



754
755
756
# File 'lib/macho/headers.rb', line 754

def magic32?
  Utils.magic32?(magic)
end

#magic64?Boolean

Returns true if the Mach-O has 64-bit magic, false otherwise.

Returns:

  • (Boolean)

    true if the Mach-O has 64-bit magic, false otherwise



759
760
761
# File 'lib/macho/headers.rb', line 759

def magic64?
  Utils.magic64?(magic)
end

#object?Boolean

Returns whether or not the file is of type MH_OBJECT.

Returns:

  • (Boolean)

    whether or not the file is of type MH_OBJECT



699
700
701
# File 'lib/macho/headers.rb', line 699

def object?
  filetype == Headers::MH_OBJECT
end

#preload?Boolean

Returns whether or not the file is of type MH_PRELOAD.

Returns:

  • (Boolean)

    whether or not the file is of type MH_PRELOAD



719
720
721
# File 'lib/macho/headers.rb', line 719

def preload?
  filetype == Headers::MH_PRELOAD
end

#to_hHash

Returns a hash representation of this MachO::Headers::MachHeader.

Returns:



769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
# File 'lib/macho/headers.rb', line 769

def to_h
  {
    "magic" => magic,
    "magic_sym" => MH_MAGICS[magic],
    "cputype" => cputype,
    "cputype_sym" => CPU_TYPES[cputype],
    "cpusubtype" => cpusubtype,
    "cpusubtype_sym" => CPU_SUBTYPES[cputype][cpusubtype],
    "filetype" => filetype,
    "filetype_sym" => MH_FILETYPES[filetype],
    "ncmds" => ncmds,
    "sizeofcmds" => sizeofcmds,
    "flags" => flags,
    "alignment" => alignment,
  }.merge super
end