Class: MachO::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=2a16L=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.



389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/macho/load_commands.rb', line 389

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

Instance Attribute Details

#fileoffFixnum (readonly)

Returns the file offset of the segment.

Returns:

  • (Fixnum)

    the file offset of the segment



363
364
365
# File 'lib/macho/load_commands.rb', line 363

def fileoff
  @fileoff
end

#filesizeFixnum (readonly)

Returns the amount to map from the file.

Returns:

  • (Fixnum)

    the amount to map from the file



366
367
368
# File 'lib/macho/load_commands.rb', line 366

def filesize
  @filesize
end

#flagsFixnum (readonly)

Returns any flags associated with the segment.

Returns:

  • (Fixnum)

    any flags associated with the segment



378
379
380
# File 'lib/macho/load_commands.rb', line 378

def flags
  @flags
end

#initprotFixnum (readonly)

Returns the initial VM protection.

Returns:

  • (Fixnum)

    the initial VM protection



372
373
374
# File 'lib/macho/load_commands.rb', line 372

def initprot
  @initprot
end

#maxprotFixnum (readonly)

Returns the maximum VM protection.

Returns:

  • (Fixnum)

    the maximum VM protection



369
370
371
# File 'lib/macho/load_commands.rb', line 369

def maxprot
  @maxprot
end

#nsectsFixnum (readonly)

Returns the number of sections in the segment.

Returns:

  • (Fixnum)

    the number of sections in the segment



375
376
377
# File 'lib/macho/load_commands.rb', line 375

def nsects
  @nsects
end

#segnameString (readonly)

Returns the name of the segment.

Returns:

  • (String)

    the name of the segment



354
355
356
# File 'lib/macho/load_commands.rb', line 354

def segname
  @segname
end

#vmaddrFixnum (readonly)

Returns the memory address of the segment.

Returns:

  • (Fixnum)

    the memory address of the segment



357
358
359
# File 'lib/macho/load_commands.rb', line 357

def vmaddr
  @vmaddr
end

#vmsizeFixnum (readonly)

Returns the memory size of the segment.

Returns:

  • (Fixnum)

    the memory size of the segment



360
361
362
# File 'lib/macho/load_commands.rb', line 360

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



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

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

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

All sections referenced within this segment.

Returns:



406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/macho/load_commands.rb', line 406

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

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