Class: PatchELF::AltSaver

Inherits:
Object
  • Object
show all
Defined in:
lib/patchelf/alt_saver.rb

Overview

Internal use only. alternative to Saver, that aims to be byte to byte equivalent with NixOS/patchelf.

DISCLAIMER: This differs from Saver in number of ways. No lazy reading, inconsistent use of existing internal API(e.g: manual reading of data instead of calling section.data)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(in_file, out_file, set) ⇒ AltSaver

Instantiate a PatchELF::AltSaver object. the params passed are the same as the ones passed to Saver

Parameters:

  • in_file (String)
  • out_file (String)
  • set ({Symbol => String, Array})


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/patchelf/alt_saver.rb', line 55

def initialize(in_file, out_file, set)
  @in_file = in_file
  @out_file = out_file
  @set = set

  f = File.open(in_file, 'rb')
  # the +@buffer+ and +@elf+ both could work on same +StringIO+ stream,
  # the updating of @buffer in place blocks us from looking up old values.
  # TODO: cache the values needed later, use same stream for +@buffer+ and +@elf+.
  # also be sure to update the stream offset passed to Segments::Segment.
  @elf = ELFTools::ELFFile.new(f)
  @buffer = StringIO.new(f.tap(&:rewind).read) # StringIO makes easier to work with Bindata

  @ehdr = @elf.header
  @endian = @elf.endian
  @elf_class = @elf.elf_class

  @segments = @elf.segments # usage similar to phdrs
  @sections = @elf.sections # usage similar to shdrs
  update_section_idx!

  # {String => String}
  # section name to its data mapping
  @replaced_sections = {}
  @section_alignment = ehdr.e_phoff.num_bytes

  # using the same environment flag as patchelf, makes it easier for debugging
  Logger.level = ::Logger.const_get(ENV['PATCHELF_DEBUG'] ? :DEBUG : :WARN)
end

Instance Attribute Details

#in_fileString (readonly)

Returns Input filename.

Returns:

  • (String)

    Input filename.



47
48
49
# File 'lib/patchelf/alt_saver.rb', line 47

def in_file
  @in_file
end

#out_fileString (readonly)

Returns Output filename.

Returns:

  • (String)

    Output filename.



48
49
50
# File 'lib/patchelf/alt_saver.rb', line 48

def out_file
  @out_file
end

Instance Method Details

#save!void

This method returns an undefined value.



86
87
88
89
90
91
92
93
94
# File 'lib/patchelf/alt_saver.rb', line 86

def save!
  @set.each { |mtd, val| send(:"modify_#{mtd}") if val }
  rewrite_sections

  FileUtils.cp(in_file, out_file) if out_file != in_file
  patch_out
  # Let output file have the same permission as input.
  FileUtils.chmod(File.stat(in_file).mode, out_file)
end