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



577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/macho/headers.rb', line 577

def initialize(magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds,
               flags)
  @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)



554
555
556
# File 'lib/macho/headers.rb', line 554

def cpusubtype
  @cpusubtype
end

#cputypeInteger (readonly)



551
552
553
# File 'lib/macho/headers.rb', line 551

def cputype
  @cputype
end

#filetypeInteger (readonly)



557
558
559
# File 'lib/macho/headers.rb', line 557

def filetype
  @filetype
end

#flagsInteger (readonly)



566
567
568
# File 'lib/macho/headers.rb', line 566

def flags
  @flags
end

#magicInteger (readonly)



548
549
550
# File 'lib/macho/headers.rb', line 548

def magic
  @magic
end

#ncmdsInteger (readonly)



560
561
562
# File 'lib/macho/headers.rb', line 560

def ncmds
  @ncmds
end

#sizeofcmdsInteger (readonly)



563
564
565
# File 'lib/macho/headers.rb', line 563

def sizeofcmds
  @sizeofcmds
end

Instance Method Details

#alignmentInteger



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

def alignment
  magic32? ? 4 : 8
end

#bundle?Boolean



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

def bundle?
  filetype == Headers::MH_BUNDLE
end

#core?Boolean



616
617
618
# File 'lib/macho/headers.rb', line 616

def core?
  filetype == Headers::MH_CORE
end

#dsym?Boolean



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

def dsym?
  filetype == Headers::MH_DSYM
end

#dylib?Boolean



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

def dylib?
  filetype == Headers::MH_DYLIB
end

#dylinker?Boolean



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

def dylinker?
  filetype == Headers::MH_DYLINKER
end

#executable?Boolean



606
607
608
# File 'lib/macho/headers.rb', line 606

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)


594
595
596
597
598
# File 'lib/macho/headers.rb', line 594

def flag?(flag)
  flag = MH_FLAGS[flag]
  return false if flag.nil?
  flags & flag == flag
end

#fvmlib?Boolean



611
612
613
# File 'lib/macho/headers.rb', line 611

def fvmlib?
  filetype == Headers::MH_FVMLIB
end

#kext?Boolean



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

def kext?
  filetype == Headers::MH_KEXT_BUNDLE
end

#magic32?Boolean



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

def magic32?
  Utils.magic32?(magic)
end

#magic64?Boolean



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

def magic64?
  Utils.magic64?(magic)
end

#object?Boolean



601
602
603
# File 'lib/macho/headers.rb', line 601

def object?
  filetype == Headers::MH_OBJECT
end

#preload?Boolean



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

def preload?
  filetype == Headers::MH_PRELOAD
end

#to_hHash



666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# File 'lib/macho/headers.rb', line 666

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