patchelf 1.5.0 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1e44d5adb39d260d124ebc5231362a134e0719cf2f4a379ddab0d4a7343db66
4
- data.tar.gz: a1cdf1d41641b728e205de2559c86eb4fb7a98f09d6a1882123310edf6701bcf
3
+ metadata.gz: f11434b437c5d8623134c6c1fe78332b8cbc1251bfd1b0549a560fef7160cf1e
4
+ data.tar.gz: 742bade914a5ad4e8b1523ad3d1aea9b2a801f89365e087cbc2d7fed0bfe4cba
5
5
  SHA512:
6
- metadata.gz: 432fd63c969cd337644e4360dbc110a0fae0aaba480174ea3162ddab17814fea9eceeff266cc2f98c571b06830f4ff94ac148b762c49d6fe2135cb692a212a29
7
- data.tar.gz: 9d7b9769ae80913cbc621801f6caee0e1e61ccbc32514302f892e7ecbff3c8439ae89557c1bb3b10d27e21732fe1e7a36bf8be12b8296237a3f6dd39a6b76c3b
6
+ metadata.gz: a476877469a6480b277ac318e703eb8d5a5c373b47b095b5c215327207e52b024fc8c38093ef3974fb022f206078c4ae20548d97d861bcc3bcae3e5a0b6727c6
7
+ data.tar.gz: 4acd3a4e025dec84cefe1b45d09f6aa5425618a12ac3f5bb94a60a3aa9f04a9e5fba68cba8f988be6866a615744ae1f03c3a944812a12513bb4612ade87aa721
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 david942j
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,9 +1,10 @@
1
+ [![Downloads](https://img.shields.io/endpoint?url=https://gem-badge-h3lg.onrender.com/downloads/patchelf)](https://rubygems.org/gems/patchelf)
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/patchelf.svg)](https://badge.fury.io/rb/patchelf)
1
4
  [![Build Status](https://github.com/david942j/patchelf.rb/workflows/build/badge.svg)](https://github.com/david942j/patchelf.rb/actions)
2
- [![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=david942j/patchelf.rb)](https://dependabot.com)
3
- [![Code Climate](https://codeclimate.com/github/david942j/patchelf.rb/badges/gpa.svg)](https://codeclimate.com/github/david942j/patchelf.rb)
4
- [![Issue Count](https://codeclimate.com/github/david942j/patchelf.rb/badges/issue_count.svg)](https://codeclimate.com/github/david942j/patchelf.rb)
5
- [![Test Coverage](https://codeclimate.com/github/david942j/patchelf.rb/badges/coverage.svg)](https://codeclimate.com/github/david942j/patchelf.rb/coverage)
6
- [![Inline docs](https://inch-ci.org/github/david942j/patchelf.rb.svg?branch=master)](https://inch-ci.org/github/david942j/patchelf.rb)
5
+ [![Maintainability](https://qlty.sh/gh/david942j/projects/patchelf.rb/maintainability.svg)](https://qlty.sh/gh/david942j/projects/patchelf.rb)
6
+ [![Code Coverage](https://qlty.sh/gh/david942j/projects/patchelf.rb/coverage.svg)](https://qlty.sh/gh/david942j/projects/patchelf.rb)
7
+ [![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](https://www.rubydoc.info/github/david942j/patchelf.rb/master)
7
8
  [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](http://choosealicense.com/licenses/mit/)
8
9
 
9
10
  # patchelf.rb
@@ -5,6 +5,7 @@ require 'elftools/elf_file'
5
5
  require 'elftools/structs'
6
6
  require 'elftools/util'
7
7
  require 'fileutils'
8
+ require 'objspace'
8
9
 
9
10
  require 'patchelf/helper'
10
11
 
@@ -65,6 +66,9 @@ module PatchELF
65
66
  @elf = ELFTools::ELFFile.new(f)
66
67
  @buffer = StringIO.new(f.tap(&:rewind).read) # StringIO makes easier to work with Bindata
67
68
 
69
+ # Ensure file is closed when the {AltSaver} object is garbage collected.
70
+ ObjectSpace.define_finalizer(self, Helper.close_file_proc(f))
71
+
68
72
  @ehdr = @elf.header
69
73
  @endian = @elf.endian
70
74
  @elf_class = @elf.elf_class
@@ -451,10 +455,10 @@ module PatchELF
451
455
 
452
456
  begin
453
457
  new_index = new_section_idx(old_shndx)
454
- next unless new_index
455
458
  rescue ArgumentError
456
459
  Logger.warn "entry #{entry} in symbol table refers to a non existing section, skipping"
457
460
  end
461
+ next unless new_index
458
462
 
459
463
  sym[pack[:st_shndx]] = new_index
460
464
 
@@ -561,11 +565,12 @@ module PatchELF
561
565
  if needed_space > start_offset
562
566
  needed_space += seg_num_bytes # new load segment is required
563
567
 
564
- needed_pages = Helper.alignup(needed_space - start_offset, page_size) / page_size
568
+ extra_bytes = needed_space - start_offset
569
+ needed_pages = Helper.alignup(extra_bytes, page_size) / page_size
565
570
  Logger.debug "needed pages is #{needed_pages}"
566
571
  raise PatchError, 'virtual address space underrun' if needed_pages * page_size > first_page
567
572
 
568
- shift_file(needed_pages, start_offset)
573
+ shift_file(needed_pages, start_offset, extra_bytes)
569
574
 
570
575
  first_page -= needed_pages * page_size
571
576
  start_offset += needed_pages * page_size
@@ -574,7 +579,7 @@ module PatchELF
574
579
 
575
580
  cur_off = ehdr.num_bytes + (@segments.count * seg_num_bytes)
576
581
  Logger.debug "clearing first #{start_offset - cur_off} bytes"
577
- with_buf_at(cur_off) { |buf| buf.fill("\x00", (start_offset - cur_off)) }
582
+ with_buf_at(cur_off) { |buf| buf.fill("\x00", start_offset - cur_off) }
578
583
 
579
584
  cur_off = write_replaced_sections cur_off, first_page, 0
580
585
  raise PatchError, "cur_off(#{cur_off}) != needed_space" if cur_off != needed_space
@@ -776,7 +781,7 @@ module PatchELF
776
781
  end
777
782
  # rubocop:enable Metrics/PerceivedComplexity
778
783
 
779
- def shift_file(extra_pages, start_offset)
784
+ def shift_file(extra_pages, start_offset, extra_bytes)
780
785
  raise PatchError, "start_offset(#{start_offset}) < ehdr.num_bytes" if start_offset < ehdr.num_bytes
781
786
 
782
787
  oldsz = @buffer.size
@@ -799,8 +804,8 @@ module PatchELF
799
804
  p_offset: split_phdr.p_offset - split_shift - shift,
800
805
  p_vaddr: split_phdr.p_vaddr - split_shift - shift,
801
806
  p_paddr: split_phdr.p_paddr - split_shift - shift,
802
- p_filesz: split_shift + shift,
803
- p_memsz: split_shift + shift,
807
+ p_filesz: split_shift + extra_bytes,
808
+ p_memsz: split_shift + extra_bytes,
804
809
  p_flags: ELFTools::Constants::PF_R | ELFTools::Constants::PF_W,
805
810
  p_align: page_size
806
811
  )
data/lib/patchelf/cli.rb CHANGED
@@ -31,7 +31,7 @@ module PatchELF
31
31
  needed: []
32
32
  }
33
33
  return $stdout.puts "PatchELF Version #{PatchELF::VERSION}" if argv.include?('--version')
34
- return $stdout.puts option_parser unless parse(argv)
34
+ return $stdout.puts option_parser unless parse?(argv)
35
35
 
36
36
  # Now the options are (hopefully) valid, let's process the ELF file.
37
37
  begin
@@ -71,7 +71,7 @@ module PatchELF
71
71
  end
72
72
  end
73
73
 
74
- def parse(argv)
74
+ def parse?(argv)
75
75
  remain = option_parser.permute(argv)
76
76
  return false if remain.first.nil?
77
77
 
@@ -78,7 +78,14 @@ module PatchELF
78
78
  # alignup(0x10, 0x8)
79
79
  # #=> 16
80
80
  def alignup(val, align = page_size)
81
- (val & (align - 1)).zero? ? val : (aligndown(val, align) + align)
81
+ val.nobits?(align - 1) ? val : (aligndown(val, align) + align)
82
+ end
83
+
84
+ # @param [File?] file
85
+ # @return [Proc]
86
+ # A proc that closes the file if it's open.
87
+ def close_file_proc(file)
88
+ proc { file.close if file && !file.closed? }
82
89
  end
83
90
  end
84
91
  end
data/lib/patchelf/mm.rb CHANGED
@@ -49,7 +49,7 @@ module PatchELF
49
49
  abnormal_elf('No LOAD segment found, not an executable.') if load_segments.empty?
50
50
  # TODO: Handle only one LOAD. (be careful if memsz > filesz)
51
51
 
52
- fgap_method || mgap_method || new_load_method
52
+ fgap_method? || mgap_method? || new_load_method
53
53
  end
54
54
 
55
55
  # Query if extended.
@@ -72,25 +72,25 @@ module PatchELF
72
72
 
73
73
  private
74
74
 
75
- def fgap_method
75
+ def fgap_method?
76
76
  idx = find_gap { |prv, nxt| nxt.file_head - prv.file_tail }
77
77
  return false if idx.nil?
78
78
 
79
79
  loads = load_segments
80
80
  # prefer extend backwardly
81
- return extend_backward(loads[idx - 1]) if writable?(loads[idx - 1])
81
+ return extend_backward?(loads[idx - 1]) if writable?(loads[idx - 1])
82
82
 
83
- extend_forward(loads[idx])
83
+ extend_forward?(loads[idx])
84
84
  end
85
85
 
86
- def extend_backward(seg, size = @request_size)
86
+ def extend_backward?(seg, size = @request_size)
87
87
  invoke_callbacks(seg, seg.file_tail)
88
88
  seg.header.p_filesz += size
89
89
  seg.header.p_memsz += size
90
90
  true
91
91
  end
92
92
 
93
- def extend_forward(seg, size = @request_size)
93
+ def extend_forward?(seg, size = @request_size)
94
94
  seg.header.p_offset -= size
95
95
  seg.header.p_vaddr -= size
96
96
  seg.header.p_filesz += size
@@ -99,7 +99,7 @@ module PatchELF
99
99
  true
100
100
  end
101
101
 
102
- def mgap_method
102
+ def mgap_method?
103
103
  # | 1 | | 2 |
104
104
  # | 1 | | 2 |
105
105
  #=>
@@ -113,10 +113,10 @@ module PatchELF
113
113
  @extend_size = PatchELF::Helper.alignup(@request_size)
114
114
  shift_attributes
115
115
  # prefer backward than forward
116
- return extend_backward(loads[idx - 1]) if writable?(loads[idx - 1])
116
+ return extend_backward?(loads[idx - 1]) if writable?(loads[idx - 1])
117
117
 
118
118
  # NOTE: loads[idx].file_head has been changed in shift_attributes
119
- extend_forward(loads[idx], @extend_size)
119
+ extend_forward?(loads[idx], @extend_size)
120
120
  end
121
121
 
122
122
  def find_gap(check_sz: true)
@@ -2,8 +2,10 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'elftools/elf_file'
5
+ require 'objspace'
5
6
 
6
7
  require 'patchelf/exceptions'
8
+ require 'patchelf/helper'
7
9
  require 'patchelf/logger'
8
10
  require 'patchelf/saver'
9
11
 
@@ -27,13 +29,17 @@ module PatchELF
27
29
  # :silent = ignore the errors
28
30
  def initialize(filename, on_error: :log, logging: true)
29
31
  @in_file = filename
30
- @elf = ELFTools::ELFFile.new(File.open(filename))
32
+ f = File.open(filename)
33
+ @elf = ELFTools::ELFFile.new(f)
31
34
  @set = {}
32
35
  @rpath_sym = :runpath
33
36
  @on_error = logging ? on_error : :exception
34
37
 
35
38
  on_error_syms = %i[exception log silent]
36
39
  raise ArgumentError, "on_error must be one of #{on_error_syms}" unless on_error_syms.include?(@on_error)
40
+
41
+ # Ensure file is closed when the {Patcher} object is garbage collected.
42
+ ObjectSpace.define_finalizer(self, Helper.close_file_proc(f))
37
43
  end
38
44
 
39
45
  # @return [String?]
@@ -5,10 +5,15 @@ require 'elftools/elf_file'
5
5
  require 'elftools/structs'
6
6
  require 'elftools/util'
7
7
  require 'fileutils'
8
+ require 'objspace'
8
9
 
10
+ require 'patchelf/helper'
9
11
  require 'patchelf/mm'
10
12
 
11
13
  module PatchELF
14
+ # To mark a not-using tag
15
+ IGNORE = ELFTools::Constants::DT_LOOS
16
+
12
17
  # Internal use only.
13
18
  #
14
19
  # For {Patcher} to do patching things and save to file.
@@ -27,10 +32,14 @@ module PatchELF
27
32
  @set = set
28
33
  # [{Integer => String}]
29
34
  @inline_patch = {}
30
- @elf = ELFTools::ELFFile.new(File.open(in_file))
35
+ f = File.open(in_file)
36
+ @elf = ELFTools::ELFFile.new(f)
31
37
  @mm = PatchELF::MM.new(@elf)
32
38
  @strtab_extend_requests = []
33
39
  @append_dyn = []
40
+
41
+ # Ensure file is closed when the {Saver} object is garbage collected.
42
+ ObjectSpace.define_finalizer(self, Helper.close_file_proc(f))
34
43
  end
35
44
 
36
45
  # @return [void]
@@ -116,8 +125,6 @@ module PatchELF
116
125
  end
117
126
  end
118
127
 
119
- # To mark a not-using tag
120
- IGNORE = ELFTools::Constants::DT_LOOS
121
128
  def patch_needed
122
129
  original_needs = dynamic.tags_by_type(:needed)
123
130
  @set[:needed].uniq!
@@ -2,5 +2,5 @@
2
2
 
3
3
  module PatchELF
4
4
  # Current gem version.
5
- VERSION = '1.5.0'.freeze
5
+ VERSION = '1.5.2'.freeze
6
6
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patchelf
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - david942j
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-02-17 00:00:00.000000000 Z
10
+ date: 2025-11-01 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: elftools
@@ -24,6 +23,20 @@ dependencies:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
25
  version: '1.3'
26
+ - !ruby/object:Gem::Dependency
27
+ name: logger
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1'
27
40
  - !ruby/object:Gem::Dependency
28
41
  name: rake
29
42
  requirement: !ruby/object:Gem::Requirement
@@ -118,6 +131,7 @@ executables:
118
131
  extensions: []
119
132
  extra_rdoc_files: []
120
133
  files:
134
+ - LICENSE
121
135
  - README.md
122
136
  - bin/patchelf.rb
123
137
  - lib/patchelf.rb
@@ -135,7 +149,6 @@ licenses:
135
149
  - MIT
136
150
  metadata:
137
151
  rubygems_mfa_required: 'true'
138
- post_install_message:
139
152
  rdoc_options: []
140
153
  require_paths:
141
154
  - lib
@@ -150,8 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
163
  - !ruby/object:Gem::Version
151
164
  version: '0'
152
165
  requirements: []
153
- rubygems_version: 3.5.3
154
- signing_key:
166
+ rubygems_version: 3.6.2
155
167
  specification_version: 4
156
168
  summary: patchelf
157
169
  test_files: []