worf 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dda7327b73cf8cf78be3bbd8c9db54d2e5212eb654d3850cef505e9ba62aeeb8
4
+ data.tar.gz: 94f66431dbd19de1a609f6fcf4024a6ab250e7b99bf392d6311f5f61a771ec21
5
+ SHA512:
6
+ metadata.gz: 123f7f71318b46d119428f4d97c5740e0e1b393cf308b5aea3bcba374ebf13d5707599c27e2a6e65a6e557134e0bc86375e566ffc5476e6e53a360ac89750ace
7
+ data.tar.gz: b31f0d6de154eeeb8904b865aafdb43dd5f0307287c0c7676d97622056f9285d77202f238f68f03d694695f0390049e2186266abea80bad3f15a1204154a69d2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2021 Aaron Patterson
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # WORF, the DWARF parser
2
+
3
+ WORF is a DWARF parser that is written in Ruby. You can use this library to
4
+ parse DWARF files. I usually use this with Mach-O files or ELF files, but
5
+ as long as you have an IO object that contains DWARF data, WORF will parse it.
6
+
7
+ With DWARF data, you can write some debugging utilities, but as an example
8
+ I'll write a very simple version of [pahole](https://linux.die.net/man/1/pahole),
9
+ a utility that finds holes in structs.
10
+
11
+ ## Example pahole
12
+
13
+ This example only works on macOS. We're going to find structs in Ruby that have
14
+ holes in them (or wasted space).
15
+
16
+ First we'll use [OdinFlex](https://github.com/tenderlove/odinflex) to find Ruby's archive file:
17
+
18
+ ```ruby
19
+ archive = nil
20
+
21
+ File.open(RbConfig.ruby) do |f|
22
+ my_macho = OdinFlex::MachO.new f
23
+ my_macho.each do |section|
24
+ if section.symtab?
25
+ archive = section.nlist.find_all(&:archive?).map(&:archive).uniq.first
26
+ break
27
+ end
28
+ end
29
+ end
30
+ ```
31
+
32
+ Now that we have the archive file, we're going to use OdinFlex again to process
33
+ the AR file which will give us access to all of the Mach-O files stored inside.
34
+ Those Mach-O files also have debugging sections that contain DWARF data, and
35
+ we'll use WORF to parse that data:
36
+
37
+ ```ruby
38
+ File.open(archive) do |f|
39
+ ar = OdinFlex::AR.new f
40
+ ar.each do |object_file|
41
+ next unless object_file.identifier =~ /\.o$/
42
+ p object_file.identifier
43
+
44
+ mach_o = OdinFlex::MachO.new(f)
45
+ debug_abbrev = debug_strs = debug_info = nil
46
+
47
+ mach_o.each do |part|
48
+ if part.section?
49
+ case part.sectname
50
+ when "__debug_abbrev"
51
+ debug_abbrev = WORF::DebugAbbrev.new f, part, mach_o.start_pos
52
+ when "__debug_str"
53
+ debug_strs = WORF::DebugStrings.new f, part, mach_o.start_pos
54
+ when "__debug_info"
55
+ debug_info = WORF::DebugInfo.new f, part, mach_o.start_pos
56
+ end
57
+ end
58
+ end
59
+
60
+ if debug_abbrev && debug_strs && debug_info
61
+ puts "great"
62
+ process_debug_info(debug_abbrev, debug_strs, debug_info)
63
+ ## Now process the DWARF info
64
+ end
65
+ exit
66
+ end
67
+ end
68
+ ```
69
+
70
+ Now we can process the DWARF information and find holes in structs!
71
+
72
+ Ok, I am feeling lazy and don't want to write the rest of this program.
73
+ Check in the examples folder for a full listing.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ file 'lib/worf/constants.rb' => ['lib/worf/constants.yml', 'lib/worf/constants.erb'] do |t|
2
+ require 'psych'
3
+ require 'erb'
4
+ constants = Psych.load_file t.prereqs.first
5
+ erb = ERB.new File.read(t.prereqs[1]), trim_mode: '-'
6
+ File.write t.name, erb.result(binding)
7
+ end
8
+
9
+ require "rake/testtask"
10
+
11
+ Rake::TestTask.new do |t|
12
+ t.libs << "test"
13
+ t.test_files = FileList['test/*_test.rb']
14
+ t.verbose = true
15
+ t.warning = true
16
+ end
17
+
18
+ task :default => 'lib/worf/constants.rb'
data/lib/worf.rb ADDED
@@ -0,0 +1,563 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "worf/constants"
4
+
5
+ module WORF
6
+ module Constants
7
+ TAG_TO_NAME = constants.grep(/TAG/).each_with_object([]) { |c, o|
8
+ v = const_get(c)
9
+ if v < DW_TAG_low_user
10
+ o[const_get(c)] = c
11
+ end
12
+ }
13
+
14
+ def self.tag_for id
15
+ TAG_TO_NAME[id]
16
+ end
17
+
18
+ def self.at_for id
19
+ constants.grep(/_AT_/).find { |c| const_get(c) == id }
20
+ end
21
+
22
+ def self.form_for id
23
+ constants.grep(/_FORM_/).find { |c| const_get(c) == id }
24
+ end
25
+ end
26
+
27
+ class Tag
28
+ attr_reader :index, :type
29
+
30
+ FORM_TO_UNPACK = {
31
+ Constants::DW_FORM_addr => "Q",
32
+ Constants::DW_FORM_strp => "L",
33
+ Constants::DW_FORM_data1 => "C",
34
+ Constants::DW_FORM_data2 => "S",
35
+ Constants::DW_FORM_data4 => "L",
36
+ Constants::DW_FORM_data8 => "Q",
37
+ Constants::DW_FORM_sec_offset => "L",
38
+ Constants::DW_FORM_ref_addr => "L",
39
+ Constants::DW_FORM_ref4 => "L"
40
+ }
41
+
42
+ UNPACK_TO_LEN = {
43
+ "Q" => 8,
44
+ "L" => 4,
45
+ "C" => 1,
46
+ "S" => 2,
47
+ }
48
+
49
+ class FixedWidthTag < Tag
50
+ def initialize index, type, has_children, attr_names, attr_forms, unpack, readlen
51
+ super(index, type, has_children, attr_names, attr_forms)
52
+ @unpack = unpack
53
+ @readlen = readlen
54
+ end
55
+
56
+ def decode io, _
57
+ io.read(@readlen).unpack(@unpack)
58
+ end
59
+ end
60
+
61
+ def self.build index, type, has_children, attr_names, attr_forms
62
+ if attr_forms.all? { |x| FORM_TO_UNPACK.key?(x) }
63
+ packs = attr_forms.map { |x| FORM_TO_UNPACK[x] }
64
+ readlen = packs.map { |p| UNPACK_TO_LEN[p] }.sum
65
+ FixedWidthTag.new index, type, has_children, attr_names, attr_forms, packs.join, readlen
66
+ else
67
+ new index, type, has_children, attr_names, attr_forms
68
+ end
69
+ end
70
+
71
+ def initialize index, type, has_children, attr_names, attr_forms
72
+ @index = index
73
+ @type = type
74
+ @has_children = has_children
75
+ @attr_names = attr_names
76
+ @attr_forms = attr_forms
77
+ end
78
+
79
+ class_eval Constants.constants.grep(/^DW_TAG_(.*)$/) { |match|
80
+ "def #{$1}?; type == Constants::#{match}; end"
81
+ }.join "\n"
82
+
83
+ def has_children?; @has_children; end
84
+
85
+ def user?
86
+ @type > Constants::DW_TAG_low_user
87
+ end
88
+
89
+ def identifier
90
+ Constants.tag_for(@type)
91
+ end
92
+
93
+ def attribute_info name
94
+ i = index_of(name) || return
95
+ yield @attr_forms, i
96
+ end
97
+
98
+ def index_of name
99
+ @attr_names.index(name)
100
+ end
101
+
102
+ def decode io, _
103
+ @attr_forms.map do |type|
104
+ case type
105
+ when Constants::DW_FORM_addr then io.read(8).unpack1("Q")
106
+ when Constants::DW_FORM_strp then io.read(4).unpack1("L")
107
+ when Constants::DW_FORM_data1 then io.read(1).unpack1("C")
108
+ when Constants::DW_FORM_data2 then io.read(2).unpack1("S")
109
+ when Constants::DW_FORM_data4 then io.read(4).unpack1("L")
110
+ when Constants::DW_FORM_data8 then io.read(8).unpack1("Q")
111
+ when Constants::DW_FORM_sec_offset then io.read(4).unpack1("L")
112
+ when Constants::DW_FORM_ref_addr then io.read(4).unpack1("L")
113
+ when Constants::DW_FORM_ref4 then io.read(4).unpack1("L")
114
+ when Constants::DW_FORM_flag_present
115
+ true
116
+ when Constants::DW_FORM_exprloc
117
+ io.read(WORF.unpackULEB128(io))
118
+ when Constants::DW_FORM_string
119
+ str = []
120
+ loop do
121
+ x = io.readbyte
122
+ break if x == 0
123
+ str << x
124
+ end
125
+
126
+ str.pack("C*")
127
+ when Constants::DW_FORM_flag
128
+ io.readbyte
129
+ when Constants::DW_FORM_block1
130
+ io.read io.readbyte
131
+ when Constants::DW_FORM_udata
132
+ WORF.unpackULEB128 io
133
+ when Constants::DW_FORM_sdata
134
+ WORF.unpackSLEB128 io
135
+ else
136
+ raise "Unhandled type: #{Constants.form_for(type)}"
137
+ end
138
+ end
139
+ end
140
+
141
+ def inspect
142
+ names = @attr_names.map { |k| Constants.at_for(k) || :Custom }
143
+ forms = @attr_forms.map { |v| Constants.form_for(v) }
144
+ maxlen = names.map { |x| x.length }.max || 0
145
+
146
+ "[#{@index}] #{Constants.tag_for(@type)} #{@has_children ? "children" : "no children"}\n" +
147
+ names.zip(forms).map { |k,v| " #{k.to_s.ljust(maxlen)} #{v}" }.join("\n")
148
+
149
+ end
150
+ end
151
+
152
+ class DebugStrings
153
+ def initialize io, section, head_pos
154
+ @io = io
155
+ @section = section
156
+ @head_pos = head_pos
157
+ end
158
+
159
+ def string_at offset
160
+ pos = @io.pos
161
+ @io.seek @head_pos + @section.offset + offset, IO::SEEK_SET
162
+ @io.readline("\x00").b.delete("\x00")
163
+ ensure
164
+ @io.seek pos, IO::SEEK_SET
165
+ end
166
+ end
167
+
168
+ class DIE
169
+ include Enumerable
170
+
171
+ attr_reader :tag, :offset, :attributes, :children
172
+
173
+ def initialize tag, offset, attributes, children
174
+ @tag = tag
175
+ @offset = offset
176
+ @attributes = attributes
177
+ @children = children
178
+ end
179
+
180
+ def find_type child
181
+ raise ArgumentError, "DIE doesn't have a type" unless child.type
182
+ children.bsearch { |c_die| child.type <=> c_die.offset }
183
+ end
184
+
185
+ def location
186
+ at Constants::DW_AT_location
187
+ end
188
+
189
+ def low_pc
190
+ at Constants::DW_AT_low_pc
191
+ end
192
+
193
+ def high_pc
194
+ at Constants::DW_AT_high_pc
195
+ end
196
+
197
+ def data_member_location
198
+ at Constants::DW_AT_data_member_location
199
+ end
200
+
201
+ def byte_size
202
+ at Constants::DW_AT_byte_size
203
+ end
204
+
205
+ def type
206
+ at Constants::DW_AT_type
207
+ end
208
+
209
+ def decl_file
210
+ at Constants::DW_AT_decl_file
211
+ end
212
+
213
+ def const_value
214
+ at Constants::DW_AT_const_value
215
+ end
216
+
217
+ def name strings
218
+ tag.attribute_info(Constants::DW_AT_name) do |form, i|
219
+ if form == Constants::DW_FORM_string
220
+ attributes[i]
221
+ else
222
+ strings.string_at(attributes[i])
223
+ end
224
+ end
225
+ end
226
+
227
+ def name_offset
228
+ at Constants::DW_AT_name
229
+ end
230
+
231
+ def each &block
232
+ yield self
233
+ children.each { |child| child.each(&block) }
234
+ end
235
+
236
+ private
237
+
238
+ def at name
239
+ idx = tag.index_of(name)
240
+ idx && attributes[idx]
241
+ end
242
+ end
243
+
244
+ class DebugLine
245
+ class Registers
246
+ attr_accessor :address, :op_index, :file, :line, :column, :is_stmt,
247
+ :basic_block, :end_sequence, :prologue_end, :epilogue_begin,
248
+ :isa, :discriminator
249
+
250
+ def initialize default_is_stmt
251
+ @address = 0
252
+ @op_index = 0
253
+ @file = 1
254
+ @line = 1
255
+ @column = 0
256
+ @is_stmt = default_is_stmt
257
+ @basic_block = false
258
+ @end_sequence = false
259
+ @prologue_end = false
260
+ @epilogue_begin = false
261
+ @isa = 0
262
+ @discriminator = 0
263
+ end
264
+
265
+ def inspect
266
+ sprintf("%#018x %s %s %s", address,
267
+ line.to_s.rjust(6),
268
+ column.to_s.rjust(6),
269
+ file.to_s.rjust(6))
270
+ end
271
+ end
272
+
273
+ FileName = Struct.new(:name, :dir_index, :mod_time, :length)
274
+ Info = Struct.new(:unit_length, :version, :include_directories, :file_names, :matrix)
275
+
276
+ def initialize io, section, head_pos
277
+ @io = io
278
+ @section = section
279
+ @head_pos = head_pos
280
+ end
281
+
282
+ def info
283
+ include_directories = []
284
+ file_names = []
285
+ matrix = []
286
+
287
+ @io.seek @head_pos + @section.offset, IO::SEEK_SET
288
+ last_position = @head_pos + @section.offset + @section.size
289
+ while @io.pos < last_position
290
+ unit_length, dwarf_version = @io.read(6).unpack("LS")
291
+ if dwarf_version != 4
292
+ raise NotImplementedError, "Only DWARF4 rn #{dwarf_version}"
293
+ end
294
+
295
+ # we're just not handling 32 bit
296
+ _, # prologue_length,
297
+ min_inst_length,
298
+ max_ops_per_inst,
299
+ default_is_stmt,
300
+ line_base,
301
+ line_range,
302
+ opcode_base = @io.read(4 + (1 * 6)).unpack("LCCCcCC")
303
+
304
+ # assume address size is 8
305
+ address_size = 8
306
+
307
+ registers = Registers.new(default_is_stmt)
308
+
309
+ @io.read(opcode_base - 1) #standard_opcode_lengths = @io.read(opcode_base - 1).bytes
310
+
311
+ loop do
312
+ str = @io.readline("\0").chomp("\0")
313
+ break if "" == str
314
+ include_directories << str
315
+ end
316
+
317
+ loop do
318
+ fname = @io.readline("\0").chomp("\0")
319
+ break if "" == fname
320
+
321
+ directory_idx = WORF.unpackULEB128 @io
322
+ last_mod = WORF.unpackULEB128 @io
323
+ length = WORF.unpackULEB128 @io
324
+ file_names << FileName.new(fname, directory_idx, last_mod, length)
325
+ end
326
+
327
+ loop do
328
+ code = @io.readbyte
329
+ case code
330
+ when 0 # extended operands
331
+ expected_size = WORF.unpackULEB128 @io
332
+ raise if expected_size == 0
333
+
334
+ cur_pos = @io.pos
335
+ extended_code = @io.readbyte
336
+ case extended_code
337
+ when Constants::DW_LNE_end_sequence
338
+ registers.end_sequence = true
339
+ matrix << registers.dup
340
+ break
341
+ when Constants::DW_LNE_set_address
342
+ registers.address = @io.read(address_size).unpack1("Q")
343
+ registers.op_index = 0
344
+ when Constants::DW_LNE_set_discriminator
345
+ raise
346
+ else
347
+ raise "unknown extednded opcode #{extended_code}"
348
+ end
349
+
350
+ raise unless expected_size == (@io.pos - cur_pos)
351
+ when Constants::DW_LNS_copy
352
+ matrix << registers.dup
353
+ registers.discriminator = 0
354
+ registers.basic_block = false
355
+ registers.prologue_end = false
356
+ registers.epilogue_begin = false
357
+ when Constants::DW_LNS_advance_pc
358
+ code = WORF.unpackULEB128 @io
359
+ registers.address += (code * min_inst_length)
360
+ when Constants::DW_LNS_advance_line
361
+ registers.line += WORF.unpackSLEB128 @io
362
+ when Constants::DW_LNS_set_file
363
+ registers.file = WORF.unpackULEB128 @io
364
+ when Constants::DW_LNS_set_column
365
+ registers.column = WORF.unpackULEB128 @io
366
+ when Constants::DW_LNS_negate_stmt
367
+ registers.is_stmt = !registers.is_stmt
368
+ when Constants::DW_LNS_set_basic_block
369
+ registers.basic_block = true
370
+ when Constants::DW_LNS_const_add_pc
371
+ code = 255
372
+ adjusted_opcode = code - opcode_base
373
+ operation_advance = adjusted_opcode / line_range
374
+ new_address = min_inst_length *
375
+ ((registers.op_index + operation_advance) /
376
+ max_ops_per_inst)
377
+
378
+ new_op_index = (registers.op_index + operation_advance) % max_ops_per_inst
379
+
380
+ registers.address += new_address
381
+ registers.op_index = new_op_index
382
+ when Constants::DW_LNS_fixed_advance_pc
383
+ raise
384
+ when Constants::DW_LNS_set_prologue_end
385
+ registers.prologue_end = true
386
+ when Constants::DW_LNS_set_epilogue_begin
387
+ raise
388
+ when Constants::DW_LNS_set_isa
389
+ raise
390
+ else
391
+ adjusted_opcode = code - opcode_base
392
+ operation_advance = adjusted_opcode / line_range
393
+ new_address = min_inst_length *
394
+ ((registers.op_index + operation_advance) /
395
+ max_ops_per_inst)
396
+
397
+ new_op_index = (registers.op_index + operation_advance) % max_ops_per_inst
398
+
399
+ line_increment = line_base + (adjusted_opcode % line_range)
400
+
401
+ registers.address += new_address
402
+ registers.op_index = new_op_index
403
+ registers.line += line_increment
404
+ matrix << registers.dup
405
+
406
+ registers.basic_block = false
407
+ registers.prologue_end = false
408
+ registers.epilogue_begin = false
409
+ registers.discriminator = 0
410
+ end
411
+ end
412
+ end
413
+
414
+ Info.new unit_length, dwarf_version, include_directories, file_names, matrix
415
+ end
416
+ end
417
+
418
+ CompilationUnit = Struct.new(:unit_length, :version, :debug_abbrev_offset, :address_size, :die)
419
+
420
+ class DebugInfo
421
+ def initialize io, section, head_pos
422
+ @io = io
423
+ @section = section
424
+ @head_pos = head_pos
425
+ end
426
+
427
+ def compile_units tags
428
+ cus = []
429
+ @io.seek @head_pos + @section.offset, IO::SEEK_SET
430
+ while @io.pos < @head_pos + @section.offset + @section.size
431
+ unit_length, dwarf_version = @io.read(6).unpack("LS")
432
+ if dwarf_version != 4
433
+ raise NotImplementedError, "Only DWARF4 rn #{dwarf_version}"
434
+ end
435
+
436
+ debug_abbrev_offset = @io.read(4).unpack1("L")
437
+ address_size = @io.readbyte
438
+ if address_size != 8
439
+ raise NotImplementedError, "only 8 bytes address size supported rn"
440
+ end
441
+ offset = @io.pos - @section.offset
442
+ abbrev_code = WORF.unpackULEB128 @io
443
+ tag = tags[abbrev_code - 1]
444
+ cu = CompilationUnit.new(unit_length,
445
+ dwarf_version,
446
+ debug_abbrev_offset,
447
+ address_size,
448
+ parse_die(@io, tags, tag, offset, address_size))
449
+ cus << cu
450
+ end
451
+ cus
452
+ ensure
453
+ @io.seek @head_pos, IO::SEEK_SET
454
+ end
455
+
456
+ private
457
+
458
+ def read_children io, tags, address_size
459
+ children = []
460
+ loop do
461
+ offset = io.pos - @section.offset
462
+ abbrev_code = WORF.unpackULEB128 io
463
+
464
+ return children if abbrev_code == 0
465
+
466
+ tag = tags.fetch(abbrev_code - 1)
467
+ die = parse_die io, tags, tag, offset, address_size
468
+ children << die
469
+ end
470
+ end
471
+
472
+ NO_CHILDREN = [].freeze
473
+
474
+ def parse_die io, tags, tag, offset, address_size
475
+ attributes = decode tag, address_size, io
476
+
477
+ children = if tag.has_children?
478
+ read_children io, tags, address_size
479
+ else
480
+ NO_CHILDREN
481
+ end
482
+ DIE.new tag, offset - @head_pos, attributes, children
483
+ end
484
+
485
+ def decode tag, address_size, io
486
+ tag.decode io, address_size
487
+ end
488
+ end
489
+
490
+ class DebugAbbrev
491
+ def initialize io, section, head_pos
492
+ @io = io
493
+ @section = section
494
+ @head_pos = head_pos
495
+ end
496
+
497
+ def tags
498
+ @tags ||= begin
499
+ @io.seek @head_pos + @section.offset, IO::SEEK_SET
500
+ tags = []
501
+ loop do
502
+ break if @io.pos + 1 >= @head_pos + @section.offset + @section.size
503
+ tags << read_tag
504
+ end
505
+ tags
506
+ end
507
+ end
508
+
509
+ private
510
+
511
+ def read_tag
512
+ abbreviation_code = WORF.unpackULEB128 @io
513
+ name = WORF.unpackULEB128 @io
514
+ children_p = @io.readbyte == Constants::DW_CHILDREN_yes
515
+ attr_names = []
516
+ attr_forms = []
517
+ loop do
518
+ attr_name = WORF.unpackULEB128 @io
519
+ attr_form = WORF.unpackULEB128 @io
520
+ break if attr_name == 0 && attr_form == 0
521
+
522
+ attr_names << attr_name
523
+ attr_forms << attr_form
524
+ end
525
+ Tag.build abbreviation_code, name, children_p, attr_names, attr_forms
526
+ end
527
+ end
528
+
529
+ def self.unpackULEB128 io
530
+ result = 0
531
+ shift = 0
532
+
533
+ loop do
534
+ byte = io.getbyte
535
+ result |= ((byte & 0x7F) << shift)
536
+ if (byte < 0x80)
537
+ break
538
+ end
539
+ shift += 7
540
+ end
541
+
542
+ result
543
+ end
544
+
545
+ def self.unpackSLEB128 io
546
+ result = 0
547
+ shift = 0
548
+ size = 64
549
+
550
+ loop do
551
+ byte = io.getbyte
552
+ result |= ((byte & 0x7F) << shift)
553
+ shift += 7
554
+ if (byte >> 7) == 0
555
+ if shift < size && (byte & 0x40) != 0
556
+ result |= (~0 << shift)
557
+ end
558
+ break
559
+ end
560
+ end
561
+ result
562
+ end
563
+ end