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.



651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/macho/headers.rb', line 651

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)



628
629
630
# File 'lib/macho/headers.rb', line 628

def cpusubtype
  @cpusubtype
end

#cputypeInteger (readonly)



625
626
627
# File 'lib/macho/headers.rb', line 625

def cputype
  @cputype
end

#filetypeInteger (readonly)



631
632
633
# File 'lib/macho/headers.rb', line 631

def filetype
  @filetype
end

#flagsInteger (readonly)



640
641
642
# File 'lib/macho/headers.rb', line 640

def flags
  @flags
end

#magicInteger (readonly)



622
623
624
# File 'lib/macho/headers.rb', line 622

def magic
  @magic
end

#ncmdsInteger (readonly)



634
635
636
# File 'lib/macho/headers.rb', line 634

def ncmds
  @ncmds
end

#sizeofcmdsInteger (readonly)



637
638
639
# File 'lib/macho/headers.rb', line 637

def sizeofcmds
  @sizeofcmds
end

Instance Method Details

#alignmentInteger



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

def alignment
  magic32? ? 4 : 8
end

#bundle?Boolean



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

def bundle?
  filetype == Headers::MH_BUNDLE
end

#core?Boolean



693
694
695
# File 'lib/macho/headers.rb', line 693

def core?
  filetype == Headers::MH_CORE
end

#dsym?Boolean



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

def dsym?
  filetype == Headers::MH_DSYM
end

#dylib?Boolean



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

def dylib?
  filetype == Headers::MH_DYLIB
end

#dylinker?Boolean



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

def dylinker?
  filetype == Headers::MH_DYLINKER
end

#executable?Boolean



683
684
685
# File 'lib/macho/headers.rb', line 683

def executable?
  filetype == Headers::MH_EXECUTE
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)


669
670
671
672
673
674
675
# File 'lib/macho/headers.rb', line 669

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

  return false if flag.nil?

  flags & flag == flag
end

#fvmlib?Boolean



688
689
690
# File 'lib/macho/headers.rb', line 688

def fvmlib?
  filetype == Headers::MH_FVMLIB
end

#kext?Boolean



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

def kext?
  filetype == Headers::MH_KEXT_BUNDLE
end

#magic32?Boolean



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

def magic32?
  Utils.magic32?(magic)
end

#magic64?Boolean



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

def magic64?
  Utils.magic64?(magic)
end

#object?Boolean



678
679
680
# File 'lib/macho/headers.rb', line 678

def object?
  filetype == Headers::MH_OBJECT
end

#preload?Boolean



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

def preload?
  filetype == Headers::MH_PRELOAD
end

#to_hHash



743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# File 'lib/macho/headers.rb', line 743

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