Class: MachO::LoadCommands::SegmentCommand

Inherits:
LoadCommand show all
Defined in:
lib/macho/load_commands.rb

Overview

A load command indicating that part of this file is to be mapped into the task's address space. Corresponds to LC_SEGMENT.

Direct Known Subclasses

SegmentCommand64

Instance Method Summary collapse

Methods inherited from LoadCommand

#cmd, #cmdsize, create, new_from_bin, #offset, #serializable?, #serialize, #to_s, #type, #view

Methods inherited from MachOStructure

bytesize, format, #initialize, new_from_bin

Constructor Details

This class inherits a constructor from MachO::MachOStructure

Instance Method Details

#fileoffInteger

Returns the file offset of the segment.

Returns:

  • (Integer)

    the file offset of the segment



444
# File 'lib/macho/load_commands.rb', line 444

field :fileoff, :uint32

#filesizeInteger

Returns the amount to map from the file.

Returns:

  • (Integer)

    the amount to map from the file



447
# File 'lib/macho/load_commands.rb', line 447

field :filesize, :uint32

#flag?(flag) ⇒ Boolean

Returns true if flag is present in the segment's flag field.

Examples:

puts "this segment relocated in/to it" if sect.flag?(:SG_NORELOC)

Parameters:

  • flag (Symbol)

    a segment flag symbol

Returns:

  • (Boolean)

    true if flag is present in the segment's flag field



485
486
487
488
489
490
491
# File 'lib/macho/load_commands.rb', line 485

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

  return false if flag.nil?

  flags & flag == flag
end

#flagsInteger

Returns any flags associated with the segment.

Returns:

  • (Integer)

    any flags associated with the segment



459
# File 'lib/macho/load_commands.rb', line 459

field :flags, :uint32

#guess_alignInteger

Note:

See guess_align in cctools/misc/lipo.c

Guesses the alignment of the segment.

Returns:

  • (Integer)

    the guessed alignment, as a power of 2



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/macho/load_commands.rb', line 496

def guess_align
  return Sections::MAX_SECT_ALIGN if vmaddr.zero?

  align = 0
  segalign = 1

  while segalign.nobits?(vmaddr)
    segalign <<= 1
    align += 1
  end

  return 2 if align < 2
  return Sections::MAX_SECT_ALIGN if align > Sections::MAX_SECT_ALIGN

  align
end

#initprotInteger

Returns the initial VM protection.

Returns:

  • (Integer)

    the initial VM protection



453
# File 'lib/macho/load_commands.rb', line 453

field :initprot, :int32

#maxprotInteger

Returns the maximum VM protection.

Returns:

  • (Integer)

    the maximum VM protection



450
# File 'lib/macho/load_commands.rb', line 450

field :maxprot, :int32

#nsectsInteger

Returns the number of sections in the segment.

Returns:

  • (Integer)

    the number of sections in the segment



456
# File 'lib/macho/load_commands.rb', line 456

field :nsects, :uint32

#sectionsArray<MachO::Sections::Section>, Array<MachO::Sections::Section64>

All sections referenced within this segment.

Returns:



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/macho/load_commands.rb', line 464

def sections
  klass = case self
  when SegmentCommand64
    MachO::Sections::Section64
  when SegmentCommand
    MachO::Sections::Section
  end

  offset = view.offset + self.class.bytesize
  length = nsects * klass.bytesize

  bins = view.raw_data[offset, length]
  bins.unpack("a#{klass.bytesize}" * nsects).map do |bin|
    klass.new_from_bin(view.endianness, bin)
  end
end

#segnameString

Returns the name of the segment.

Returns:

  • (String)

    the name of the segment



435
# File 'lib/macho/load_commands.rb', line 435

field :segname, :string, :padding => :null, :size => 16, :to_s => true

#to_hHash

Returns a hash representation of this MachO::LoadCommands::SegmentCommand.

Returns:



514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/macho/load_commands.rb', line 514

def to_h
  {
    "segname" => segname,
    "vmaddr" => vmaddr,
    "vmsize" => vmsize,
    "fileoff" => fileoff,
    "filesize" => filesize,
    "maxprot" => maxprot,
    "initprot" => initprot,
    "nsects" => nsects,
    "flags" => flags,
    "sections" => sections.map(&:to_h),
  }.merge super
end

#vmaddrInteger

Returns the memory address of the segment.

Returns:

  • (Integer)

    the memory address of the segment



438
# File 'lib/macho/load_commands.rb', line 438

field :vmaddr, :uint32

#vmsizeInteger

Returns the memory size of the segment.

Returns:

  • (Integer)

    the memory size of the segment



441
# File 'lib/macho/load_commands.rb', line 441

field :vmsize, :uint32