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

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=2Z16L=4l=2L=2".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.

56

Instance Attribute Summary collapse

Attributes inherited from LoadCommand

#cmd, #cmdsize, #view

Instance Method Summary collapse

Methods inherited from LoadCommand

create, new_from_bin, #offset, #serializable?, #serialize, #to_s, #type

Methods inherited from MachOStructure

bytesize, new_from_bin

Constructor Details

#initialize(view, cmd, cmdsize, segname, vmaddr, vmsize, fileoff, filesize, maxprot, initprot, nsects, flags) ⇒ SegmentCommand

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



439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/macho/load_commands.rb', line 439

def initialize(view, cmd, cmdsize, segname, vmaddr, vmsize, fileoff,
               filesize, maxprot, initprot, nsects, flags)
  super(view, cmd, cmdsize)
  @segname = segname
  @vmaddr = vmaddr
  @vmsize = vmsize
  @fileoff = fileoff
  @filesize = filesize
  @maxprot = maxprot
  @initprot = initprot
  @nsects = nsects
  @flags = flags
end

Instance Attribute Details

#fileoffInteger (readonly)

Returns the file offset of the segment.

Returns:

  • (Integer)

    the file offset of the segment



413
414
415
# File 'lib/macho/load_commands.rb', line 413

def fileoff
  @fileoff
end

#filesizeInteger (readonly)

Returns the amount to map from the file.

Returns:

  • (Integer)

    the amount to map from the file



416
417
418
# File 'lib/macho/load_commands.rb', line 416

def filesize
  @filesize
end

#flagsInteger (readonly)

Returns any flags associated with the segment.

Returns:

  • (Integer)

    any flags associated with the segment



428
429
430
# File 'lib/macho/load_commands.rb', line 428

def flags
  @flags
end

#initprotInteger (readonly)

Returns the initial VM protection.

Returns:

  • (Integer)

    the initial VM protection



422
423
424
# File 'lib/macho/load_commands.rb', line 422

def initprot
  @initprot
end

#maxprotInteger (readonly)

Returns the maximum VM protection.

Returns:

  • (Integer)

    the maximum VM protection



419
420
421
# File 'lib/macho/load_commands.rb', line 419

def maxprot
  @maxprot
end

#nsectsInteger (readonly)

Returns the number of sections in the segment.

Returns:

  • (Integer)

    the number of sections in the segment



425
426
427
# File 'lib/macho/load_commands.rb', line 425

def nsects
  @nsects
end

#segnameString (readonly)

Returns the name of the segment.

Returns:

  • (String)

    the name of the segment



404
405
406
# File 'lib/macho/load_commands.rb', line 404

def segname
  @segname
end

#vmaddrInteger (readonly)

Returns the memory address of the segment.

Returns:

  • (Integer)

    the memory address of the segment



407
408
409
# File 'lib/macho/load_commands.rb', line 407

def vmaddr
  @vmaddr
end

#vmsizeInteger (readonly)

Returns the memory size of the segment.

Returns:

  • (Integer)

    the memory size of the segment



410
411
412
# File 'lib/macho/load_commands.rb', line 410

def vmsize
  @vmsize
end

Instance Method Details

#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



477
478
479
480
481
482
483
# File 'lib/macho/load_commands.rb', line 477

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

  return false if flag.nil?

  flags & flag == flag
end

#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



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/macho/load_commands.rb', line 488

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

  align = 0
  segalign = 1

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

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

  align
end

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

All sections referenced within this segment.

Returns:



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/macho/load_commands.rb', line 456

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

#to_hHash

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

Returns:



506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/macho/load_commands.rb', line 506

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