Class: ELFTools::Structs::Fields

Inherits:
Object
  • Object
show all
Defined in:
lib/elftools/structs.rb

Overview

What a structure the file records at an offset holds, read without building the structure.

Reading a table of structures costs a structure for every entry of it otherwise, and setting up the fields of one is most of what it costs. #struct builds one for whatever wants the structure itself, which is what assigning to a field takes, and what is assigned is answered with from then on.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, stream, offset, elf_class:, endian:) ⇒ Fields

Returns a new instance of Fields.

Parameters:

  • klass (Class)

    The structure class.

  • stream (#pos=, #read)

    The streaming object.

  • offset (Integer)

    The file offset the structure is recorded at.

  • elf_class (Integer)

    32 or 64.

  • endian (:little, :big)

    The endianness the file records it in.

Raises:

  • (EOFError)

    If the file does not reach that far.



289
290
291
292
293
294
295
296
# File 'lib/elftools/structs.rb', line 289

def initialize(klass, stream, offset, elf_class:, endian:)
  @klass = klass
  @stream = stream
  @offset = offset
  @elf_class = elf_class
  @endian = endian
  @fields = unpack
end

Instance Attribute Details

#elf_classInteger (readonly)

Returns 32 or 64.

Returns:

  • (Integer)

    32 or 64.



256
257
258
# File 'lib/elftools/structs.rb', line 256

def elf_class
  @elf_class
end

#endian:little, :big (readonly)

Returns The endianness the file records it in.

Returns:

  • (:little, :big)

    The endianness the file records it in.



257
258
259
# File 'lib/elftools/structs.rb', line 257

def endian
  @endian
end

Class Method Details

.from(klass, fields, elf_class:, endian:, offset:) ⇒ ELFTools::Structs::Fields

Fields nothing in the file records, for what a file states some other way than by recording a structure of it.

There is nothing to read and nothing to patch, so a structure is only built if something asks for one, and is built from these.

Examples:

Fields.from(ELF_Rel, { r_offset: 0x1000 }, elf_class: 64, endian: :little, offset: 0x40)

Parameters:

  • klass (Class)

    The structure class.

  • fields (Hash{Symbol => Integer})

    What each field is to record.

  • elf_class (Integer)

    32 or 64.

  • endian (:little, :big)

    The endianness the file records it in.

  • offset (Integer)

    Where in the file this came from.

Returns:



279
280
281
# File 'lib/elftools/structs.rb', line 279

def self.from(klass, fields, elf_class:, endian:, offset:)
  allocate.tap { |made| made.send(:made_of, klass, fields, elf_class, endian, offset) }
end

.of(struct) ⇒ ELFTools::Structs::Fields

What a structure already built holds, for a caller that has one.

Parameters:

Returns:



262
263
264
# File 'lib/elftools/structs.rb', line 262

def self.of(struct)
  allocate.tap { |fields| fields.send(:built_from, struct) }
end

Instance Method Details

#[](name) ⇒ Integer

What one field of the structure records.

The structure answers once there is one, so that a field assigned to reads back as it was assigned.

Examples:

fields[:st_value]
#=> 4198864

Parameters:

  • name (Symbol)

    The name of the field.

Returns:

  • (Integer)

    The value.



307
308
309
310
311
# File 'lib/elftools/structs.rb', line 307

def [](name)
  return @struct[name].to_i if @struct

  @fields[name]
end

#[]=(name, value) ⇒ void

This method returns an undefined value.

Assigns to one field.

The structure takes the assignment wherever the file records one, so that ELFFile#patches reports it. Fields the file records no structure of take it themselves, there being nothing to patch.

Parameters:

  • name (Symbol)

    The name of the field.

  • value (Integer)

    What it is to record.



321
322
323
324
325
326
327
# File 'lib/elftools/structs.rb', line 321

def []=(name, value)
  if @struct.nil? && @stream.nil?
    @fields[name] = value
  else
    struct[name] = value
  end
end

#structELFTools::Structs::ELFStruct

The structure itself, read from where the file records it, or built from these fields where it records none.

The same one however often it is asked for, so that assigning to a field of it is not forgotten.

Returns:



335
336
337
# File 'lib/elftools/structs.rb', line 335

def struct
  @struct ||= @stream.nil? ? build : read
end