class Elf::Section

Constants

Abs
Common
FlagsToChars
OsSpecific
ProcSpecific
Reserved
SunW

Sun-specific range, subset of OS-specific range

SunWIgnore
Undef

Reserved sections’ indexes

XIndex

Attributes

addr[R]
addralign[R]
entsize[R]
file[R]
index[R]
info[R]
offset[R]
size[R]
to_i[R]
type[R]

Public Class Methods

new(elf, index, sectdata, type) click to toggle source
# File lib/elf/section.rb, line 115
def initialize(elf, index, sectdata, type)
  @file =      elf
  @index =     index
  @type =      type
  @name =      sectdata[:name_idx]
  @flags_val = sectdata[:flags_val]
  @addr =      sectdata[:addr]
  @offset =    sectdata[:offset]
  @size =      sectdata[:size]
  @link =      sectdata[:link]
  @info =      sectdata[:info]
  @addralign = sectdata[:addralign]
  @entsize =   sectdata[:entsize]

  @numentries = @size/@entsize unless @entsize == 0
end
read(elf, index, sectdata) click to toggle source

Create a new Section object reading the section’s header from the file. This function assumes that the elf file is aligned ad the start of a section header, and returns the file moved at the start of the next header.

# File lib/elf/section.rb, line 57
def Section.read(elf, index, sectdata)
  begin
    if Type::ProcSpecific.include?(sectdata[:type_id])
      begin
        case elf.machine
        when Elf::Machine::ARM
          type = Type::ProcARM[sectdata[:type_id]]
        else
          type = Type[sectdata[:type_id]]
        end
      rescue Value::OutOfBound
        type = Type[sectdata[:type_id]]
      end
    elsif Type::OsSpecific.include?(sectdata[:type_id])
      begin
        # Unfortunately, even though OS ABIs are well-defined for both
        # GNU/Linux and Solaris, they don't seem to get used at all.
        #
        # For this reason, instead of basing ourselves on (just) the
        # OS ABI, the name of the section is used to identify the type
        # of section to use

        # Don't set the name if there is no string table loaded
        name = elf.string_table ? elf.string_table[sectdata[:name_idx]] : ""
        if elf.abi == Elf::OsAbi::Solaris or
            name =~ /^\.SUNW_/
          type = Type::SunW[sectdata[:type_id]]
        elsif elf.abi == Elf::OsAbi::Linux or
            name =~ /^\.gnu\./
          type = Type::GNU[sectdata[:type_id]]
        else
          type = Type[sectdata[:type_id]]
        end
      rescue Value::OutOfBound
        type = Type[sectdata[:type_id]]
      end
    else
      type = Type[sectdata[:type_id]]
    end
    type = nil if Type.is_a? Value::Unknown
  rescue Value::OutOfBound
    type = nil
  end

  raise UnknownType.new(sectdata[:type_id],
                        elf.string_table ? elf.string_table[sectdata[:name_idx]] : sectdata[:name_idx]
                        ) if type.nil?

  if Type::Class[type]
    return Type::Class[type].new(elf, index, sectdata, type)
  else
    return Section.new(elf, index, sectdata, type)
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/elf/section.rb, line 132
def ==(other)
  # For the sake of retrocompatibility and code readability,
  # accept these two types as a valid (albeit false) comparison.
  return false if other.nil? or other.is_a? Integer

  raise TypeError.new("wrong argument type #{other.class} (expected Elf::Section)") unless
    other.is_a? Section

  other.file == @file and other.addr == @addr
end
flags() click to toggle source

Return a set of flag items, easier to check for single elements.

# File lib/elf/section.rb, line 202
def flags
  return @flags if @flags

  @flags = Set.new
  Flags.each do |flag|
    flags.add(flag) if (@flags_val & flag.val) == flag.val
  end

  @flags
end
flags_i() click to toggle source
# File lib/elf/section.rb, line 239
def flags_i
  @flags_val
end
flags_s() click to toggle source
# File lib/elf/section.rb, line 226
def flags_s
  return @flags_s if @flags_s

  @flags_s = FlagsToChars.collect { |flag, char|
    flags.include?(flag) ? char : ""
  }.join

  @flags_s << 'o' if (@flags_val & Flags::MaskOS) != 0
  @flags_s << 'p' if (@flags_val & Flags::MaskProc) != 0

  return @flags_s
end
load() click to toggle source
# File lib/elf/section.rb, line 166
def load
  oldpos = @file.tell
  @file.seek(@offset, IO::SEEK_SET)

  load_internal

  @file.seek(oldpos, IO::SEEK_SET)
end
name() click to toggle source
# File lib/elf/section.rb, line 143
def name
  # We didn't read the name in form of string yet;
  # Check if the file has loaded a string table yet
  if @name.is_a? Integer and @file.string_table
    @name = @file.string_table[@name]
  end

  @name
end
Also aliased as: to_s
summary() click to toggle source
# File lib/elf/section.rb, line 175
def summary
  $stdout.puts "#{name}\t\t#{@type}\t#{@flags_val}\t#{@addr}\t#{@offset}\t#{@size}\t#{@link}\t#{@info}\t#{@addralign}\t#{@entsize}"
end
to_s()

Alias to_s to name so that using this in a string will report the name

Alias for: name