elftools 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d224322bce7543153f1df63cc56b4c41c57825527f484fd6ff8893c85e939496
4
- data.tar.gz: 6ff32495c229e034faabcd026eb47cf05fcdc7d737430075cdb0feb24548ef6b
3
+ metadata.gz: 37252d63c99fd7133d5abc0fc0e8c8685d3197840eaad1ddee1ba83b36e5dee7
4
+ data.tar.gz: 7c092256e8b730ba2e58d8dc6f5000024cf88ee8229f71727c5194a358d07bf8
5
5
  SHA512:
6
- metadata.gz: 46d10fef9a58529af0a3838357f222f009a2b6e466a0a588933eccb182a8df4f785643d2f32909158e14bafb8dce026d3237afe2e5089ffe69a2f1a545a294e7
7
- data.tar.gz: 6f4e7af4e07511edc99b0ef49cae2617edddb1a19097082674435a87dcbd471ebcff412525e8a862f1cec5206c1034911985863b7e66ba60cd47b650c099c559
6
+ metadata.gz: 03ca86ad044a3ed27c1b635f1ad4170a5906f8f3b41b5162934b16784475a3ce3b731b9734665b72803cb293f6eeb8e90cfc703a5b77adebc0b625842d7b3a95
7
+ data.tar.gz: 44a0470006720e81dfc7096b9cbb076120100db888603cd7107091372b3277d2a0f806089812505d86a0f58f44148e477906289cc49689d21d0ec57950ffee1e
@@ -64,6 +64,18 @@ module ELFTools
64
64
  each_tags.find { |tag| tag.header.d_tag == type }
65
65
  end
66
66
 
67
+ # Get tags of specific type.
68
+ # @param [Integer, Symbol, String] type
69
+ # Constant value, symbol, or string of type
70
+ # is acceptable. See examples for more information.
71
+ # @return [Array<ELFTools::Dynamic::Tag>] The desired tags.
72
+ #
73
+ # @see #tag_by_type
74
+ def tags_by_type(type)
75
+ type = Util.to_constant(Constants::DT, type)
76
+ each_tags.select { |tag| tag.header.d_tag == type }
77
+ end
78
+
67
79
  # Get the +n+-th tag.
68
80
  #
69
81
  # Tags are lazy loaded.
@@ -282,18 +282,15 @@ module ELFTools
282
282
  #
283
283
  # This method should work no matter ELF is a PIE or not.
284
284
  # This method refers from (actually equals to) binutils/readelf.c#offset_from_vma.
285
- # @param [Integer] vma The address need query.
286
- # @return [Integer] Offset related to file.
285
+ # @param [Integer] vma The virtual address to be queried.
286
+ # @return [Integer] Related file offset.
287
287
  # @example
288
288
  # elf = ELFTools::ELFFile.new(File.open('/bin/cat'))
289
289
  # elf.offset_from_vma(0x401337)
290
290
  # #=> 4919 # 0x1337
291
291
  def offset_from_vma(vma, size = 0)
292
292
  segments_by_type(:load) do |seg|
293
- if vma >= (seg.header.p_vaddr & -seg.header.p_align) &&
294
- vma + size <= seg.header.p_vaddr + seg.header.p_filesz
295
- return vma - seg.header.p_vaddr + seg.header.p_offset
296
- end
293
+ return seg.vma_to_offset(vma) if seg.vma_in?(vma, size)
297
294
  end
298
295
  end
299
296
 
@@ -0,0 +1,89 @@
1
+ require 'elftools/segments/segment'
2
+
3
+ module ELFTools
4
+ module Segments
5
+ # For DT_LOAD segment.
6
+ # Able to query between file offset and virtual memory address.
7
+ class LoadSegment < Segment
8
+ # Returns the start of this segment.
9
+ # @return [Integer]
10
+ # The file offset.
11
+ def file_head
12
+ header.p_offset.to_i
13
+ end
14
+
15
+ # Returns size in file.
16
+ # @return [Integer]
17
+ # The size.
18
+ def size
19
+ header.p_filesz.to_i
20
+ end
21
+
22
+ # Returns the end of this segment.
23
+ # @return [Integer]
24
+ # The file offset.
25
+ def file_tail
26
+ file_head + size
27
+ end
28
+
29
+ # Returns the start virtual address of this segment.
30
+ # @return [Integer]
31
+ # The vma.
32
+ def mem_head
33
+ header.p_vaddr.to_i
34
+ end
35
+
36
+ # Returns size in memory.
37
+ # @return [Integer]
38
+ # The size.
39
+ def mem_size
40
+ header.p_memsz.to_i
41
+ end
42
+
43
+ # Returns the end virtual address of this segment.
44
+ # @return [Integer]
45
+ # The vma.
46
+ def mem_tail
47
+ mem_head + mem_size
48
+ end
49
+
50
+ # Query if the given file offset located in this segment.
51
+ # @param [Integer] offset
52
+ # File offset.
53
+ # @param [Integer] size
54
+ # Size.
55
+ # @return [Boolean]
56
+ def offset_in?(offset, size = 0)
57
+ file_head <= offset && offset + size < file_tail
58
+ end
59
+
60
+ # Convert file offset into virtual memory address.
61
+ # @param [Integer] offset
62
+ # File offset.
63
+ # @return [Integer]
64
+ def offset_to_vma(offset)
65
+ # XXX: What if file_head is not aligned with p_vaddr (which is invalid according to ELF spec)?
66
+ offset - file_head + header.p_vaddr
67
+ end
68
+
69
+ # Query if the given virtual memory address located in this segment.
70
+ # @param [Integer] vma
71
+ # Virtual memory address.
72
+ # @param [Integer] size
73
+ # Size.
74
+ # @return [Boolean]
75
+ def vma_in?(vma, size = 0)
76
+ vma >= (header.p_vaddr & -header.p_align) &&
77
+ vma + size <= mem_tail
78
+ end
79
+
80
+ # Convert virtual memory address into file offset.
81
+ # @param [Integer] vma
82
+ # Virtual memory address.
83
+ # @return [Integer]
84
+ def vma_to_offset(vma)
85
+ vma - header.p_vaddr + header.p_offset
86
+ end
87
+ end
88
+ end
89
+ end
@@ -4,6 +4,7 @@ require 'elftools/segments/segment'
4
4
 
5
5
  require 'elftools/segments/dynamic_segment'
6
6
  require 'elftools/segments/interp_segment'
7
+ require 'elftools/segments/load_segment'
7
8
  require 'elftools/segments/note_segment'
8
9
 
9
10
  module ELFTools
@@ -20,6 +21,7 @@ module ELFTools
20
21
  klass = case header.p_type
21
22
  when Constants::PT_DYNAMIC then DynamicSegment
22
23
  when Constants::PT_INTERP then InterpSegment
24
+ when Constants::PT_LOAD then LoadSegment
23
25
  when Constants::PT_NOTE then NoteSegment
24
26
  else Segment
25
27
  end
@@ -1,4 +1,4 @@
1
1
  module ELFTools
2
2
  # Current gem version
3
- VERSION = '1.0.2'.freeze
3
+ VERSION = '1.1.0'.freeze
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elftools
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - david942j
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-25 00:00:00.000000000 Z
11
+ date: 2019-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bindata
@@ -135,6 +135,7 @@ files:
135
135
  - lib/elftools/sections/sym_tab_section.rb
136
136
  - lib/elftools/segments/dynamic_segment.rb
137
137
  - lib/elftools/segments/interp_segment.rb
138
+ - lib/elftools/segments/load_segment.rb
138
139
  - lib/elftools/segments/note_segment.rb
139
140
  - lib/elftools/segments/segment.rb
140
141
  - lib/elftools/segments/segments.rb