Module: ELFTools::Util::ClassMethods

Included in:
ELFTools::Util
Defined in:
lib/elftools/util.rb

Overview

Class methods.

Instance Method Summary collapse

Instance Method Details

#align(num, bit) ⇒ Integer

Round up the number to be multiple of 2**bit.

Examples:

align(10, 1) #=> 10
align(10, 2) #=> 12
align(10, 3) #=> 16
align(10, 4) #=> 16
align(10, 5) #=> 32

Parameters:

  • num (Integer)

    Number to be rounded-up.

  • bit (Integer)

    How many bit to be aligned.

Returns:

  • (Integer)

    See examples.



19
20
21
22
23
24
# File 'lib/elftools/util.rb', line 19

def align(num, bit)
  n = 2**bit
  return num if (num % n).zero?

  (num + n) & ~(n - 1)
end

#cstring(stream, offset) ⇒ String

Read from stream until reach a null-byte.

Examples:

Util.cstring(File.open('/bin/cat'), 0)
#=> "\x7FELF\x02\x01\x01"

Parameters:

  • stream (#pos=, #read)

    Streaming object

  • offset (Integer)

    Start from here.

Returns:

  • (String)

    Result string will never contain null byte.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/elftools/util.rb', line 61

def cstring(stream, offset)
  stream.pos = offset
  # read until "\x00"
  ret = ''
  loop do
    c = stream.read(1)
    return nil if c.nil? # reach EOF
    break if c == "\x00"

    ret += c
  end
  ret
end

#select_by_type(enum, type) ⇒ Array<Object>

Select objects from enumerator with .type property equals to type.

Different from naive Array#select is this method will yield block whenever find a desired object.

This method is used to simplify the same logic in methods ELFFile#sections_by_type, ELFFile#segments_by_type, etc.

Parameters:

  • enum (Enumerator)

    An enumerator for further select.

  • type (Object)

    The type you want.

Returns:

  • (Array<Object>)

    The return value will be objects in enum with attribute .type equals to type.



88
89
90
91
92
93
94
95
# File 'lib/elftools/util.rb', line 88

def select_by_type(enum, type)
  enum.select do |obj|
    if obj.type == type
      yield obj if block_given?
      true
    end
  end
end

#to_constant(mod, val) ⇒ Integer

Fetch the correct value from module mod.

See ELFFile#segment_by_type for how to use this method.

Parameters:

  • mod (Module)

    The module defined constant numbers.

  • val (Integer, Symbol, String)

    Desired value.

Returns:

  • (Integer)

    Currently this method always return a value from Constants.

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/elftools/util.rb', line 36

def to_constant(mod, val)
  # Ignore the outest name.
  module_name = mod.name.sub('ELFTools::', '')
  # if val is an integer, check if exists in mod
  if val.is_a?(Integer)
    return val if mod.constants.any? { |c| mod.const_get(c) == val }

    raise ArgumentError, "No constants in #{module_name} is #{val}"
  end
  val = val.to_s.upcase
  prefix = module_name.split('::')[-1]
  val = "#{prefix}_#{val}" unless val.start_with?(prefix)
  val = val.to_sym
  raise ArgumentError, "No constants in #{module_name} named \"#{val}\"" unless mod.const_defined?(val)

  mod.const_get(val)
end