odinflex 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: 7c5b70806e04d651047d322586c99784f4b64146bf80a28aa5b733b135384e71
4
+ data.tar.gz: bef4880fba6965f96e8af257a5495f06d056a1caf14887f93429537b6a3adcc6
5
+ SHA512:
6
+ metadata.gz: 9a5f351cffa64dfdd2e5631172b582e839f6d69f6b18c9f27fbebe0f7ad51e8b8ee56b746ee3975db8c8930b97a8ef72b17af64be4bbee2d7e3d1bd9e6d7cc11
7
+ data.tar.gz: e89f7f5d942a9883c276b31694a625fe705fdd1836557afb623957f8bdd708962b14455de38f21793fde4db58f8fd653cc1f20930edf49b37e90e3e08f0b366f
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "minitest"
4
+ gem "rake"
5
+ gem "worf", git: "https://github.com/tenderlove/worf.git"
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,59 @@
1
+ # OdinFlex
2
+
3
+ This is just a collection of weird tools that I wrote for dealing with some
4
+ binary file formats. There is an AR parser and a MachO parser in here.
5
+
6
+
7
+ ## Examples
8
+
9
+ Find debugging information:
10
+
11
+ ```ruby
12
+ mach_o = OdinFlex::MachO.new(io)
13
+ abbrev = mach_o.find_section "__debug_abbrev"
14
+
15
+ # Do stuff with the debugging information
16
+ ```
17
+
18
+ List all symbols in Ruby:
19
+
20
+ ```ruby
21
+ require "odinflex/mach-o"
22
+
23
+ File.open(RbConfig.ruby) do |f|
24
+ my_macho = OdinFlex::MachO.new f
25
+ my_macho.each do |section|
26
+ if section.symtab?
27
+ section.nlist.each { |symbol|
28
+ p symbol
29
+ }
30
+ end
31
+ end
32
+ end
33
+ ```
34
+
35
+ Find Ruby's archive file, then read the archive:
36
+
37
+ ```ruby
38
+ require "odinflex/mach-o"
39
+ require "odinflex/ar"
40
+
41
+ archive = nil
42
+
43
+ File.open(RbConfig.ruby) do |f|
44
+ my_macho = OdinFlex::MachO.new f
45
+ my_macho.each do |section|
46
+ if section.symtab?
47
+ archive = section.nlist.find_all(&:archive?).map(&:archive).uniq.first
48
+ break
49
+ end
50
+ end
51
+ end
52
+
53
+ File.open(archive) do |f|
54
+ ar = OdinFlex::AR.new f
55
+ ar.each do |object_file|
56
+ p object_file.identifier
57
+ end
58
+ end
59
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "rake/testtask"
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << "test"
5
+ t.test_files = FileList['test/*_test.rb']
6
+ t.verbose = true
7
+ t.warning = true
8
+ end
@@ -0,0 +1,56 @@
1
+ module OdinFlex
2
+ class AR
3
+ include Enumerable
4
+
5
+ HEADER_LENGTH = 16 + # File Identifier
6
+ 12 + # File modification timestamp
7
+ 6 + # Owner ID
8
+ 6 + # Group ID
9
+ 8 + # File Mode
10
+ 10 + # File size in bytes
11
+ 2 # Ending characters
12
+
13
+ AR_HEADER = "!<arch>\n"
14
+
15
+ def self.is_ar? io
16
+ pos = io.pos
17
+ header = io.read(AR_HEADER.length)
18
+ header == AR_HEADER
19
+ ensure
20
+ io.seek pos, IO::SEEK_SET
21
+ end
22
+
23
+ def initialize fd
24
+ @fd = fd
25
+ @pos = fd.pos
26
+ end
27
+
28
+ Info = Struct.new :identifier, :timestamp, :owner, :group, :mode, :size, :pos
29
+
30
+ def each
31
+ @fd.seek @pos, IO::SEEK_SET
32
+ header = @fd.read(AR_HEADER.length)
33
+
34
+ raise "Wrong Header" unless header == AR_HEADER
35
+
36
+ loop do
37
+ break if @fd.eof?
38
+
39
+ identifier, timestamp, owner, group, mode, size, ending =
40
+ @fd.read(HEADER_LENGTH).unpack('A16A12A6A6A8A10A2')
41
+
42
+ raise "wrong ending #{ending}" unless ending.bytes == [0x60, 0x0A]
43
+ pos = @fd.pos
44
+
45
+ # Assume BSD for now
46
+ fname_len = identifier[/\d+$/].to_i
47
+ filename = @fd.read(fname_len).unpack1('A*')
48
+ info = Info.new filename, timestamp, owner, group, mode, size.to_i - fname_len, @fd.pos
49
+
50
+ yield info
51
+
52
+ @fd.seek size.to_i + pos, IO::SEEK_SET
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,560 @@
1
+ module OdinFlex
2
+ class MachO
3
+ HEADER_MAGIC = 0xfeedfacf
4
+
5
+ def self.is_macho? io
6
+ pos = io.pos
7
+ header = io.read(8).unpack1 'L'
8
+ header == HEADER_MAGIC
9
+ ensure
10
+ io.seek pos, IO::SEEK_SET
11
+ end
12
+
13
+ attr_reader :start_pos
14
+
15
+ def initialize fd
16
+ @fd = fd
17
+ @start_pos = @fd.pos
18
+ end
19
+
20
+ module SectionTypes
21
+ def section?; false; end
22
+ def symtab?; false; end
23
+ def segment?; false; end
24
+ def dysymtab?; false; end
25
+ def command?; false; end
26
+ end
27
+
28
+ class Header < Struct.new :magic,
29
+ :cputype,
30
+ :cpusubtype,
31
+ :filetype,
32
+ :ncmds,
33
+ :sizeofcmds,
34
+ :flags,
35
+ :reserved
36
+
37
+ include SectionTypes
38
+
39
+ MH_OBJECT = 0x1
40
+ MH_EXECUTE = 0x2
41
+ MH_FVMLIB = 0x3
42
+ MH_CORE = 0x4
43
+ MH_PRELOAD = 0x5
44
+ MH_DYLIB = 0x6
45
+ MH_DYLINKER = 0x7
46
+ MH_BUNDLE = 0x8
47
+ MH_DYLIB_STUB = 0x9
48
+ MH_DSYM = 0xa
49
+ MH_KEXT_BUNDLE = 0xb
50
+ MH_FILESET = 0xc
51
+
52
+ SIZEOF = 8 * 4 # 8 * (32 bit int)
53
+
54
+ def object_file?
55
+ filetype == MH_OBJECT
56
+ end
57
+
58
+ def executable_file?
59
+ filetype == MH_EXECUTE
60
+ end
61
+
62
+ def dsym_file?
63
+ filetype == MH_DSYM
64
+ end
65
+ end
66
+
67
+ class Command
68
+ include SectionTypes
69
+
70
+ attr_reader :cmd, :size
71
+
72
+ def self.from_offset offset, io
73
+ io.seek offset, IO::SEEK_SET
74
+ cmd, size = io.read(2 * 4).unpack('LL')
75
+ from_io cmd, size, offset, io
76
+ end
77
+
78
+ def initialize cmd, size
79
+ @cmd = cmd
80
+ @size = size
81
+ end
82
+
83
+ def command?; true; end
84
+ end
85
+
86
+ class LC_UNIXTHREAD < Command
87
+ VALUE = 0x04
88
+ SIZE = 16 # uuid
89
+
90
+ def self.from_io cmd, size, offset, io
91
+ new(cmd, size, io.read(SIZE))
92
+ end
93
+
94
+ attr_reader :uuid
95
+
96
+ def initialize cmd, size, uuid
97
+ super(cmd, size)
98
+ @uuid = uuid
99
+ end
100
+ end
101
+
102
+ class LC_FUNCTION_STARTS < Command
103
+ VALUE = 0x26
104
+ SIZE = 4 + # dataoff
105
+ 4 # datasize
106
+
107
+ def self.from_io cmd, size, offset, io
108
+ new(cmd, size, *io.read(SIZE).unpack("LL"))
109
+ end
110
+
111
+ attr_reader :dataoff, :datasize
112
+
113
+ def initialize cmd, size, dataoff, datasize
114
+ super(cmd, size)
115
+ @dataoff = dataoff
116
+ @datasize = datasize
117
+ end
118
+ end
119
+
120
+ class LC_DATA_IN_CODE < LC_FUNCTION_STARTS
121
+ VALUE = 0x29
122
+ end
123
+
124
+ class LC_LOAD_DYLIB < Command
125
+ VALUE = 0xc
126
+
127
+ def self.from_io cmd, size, offset, io
128
+ # `size` is the total segment size including command and length bytes
129
+ # so we need to remove them from the size.
130
+ args = io.read(4 * 4).unpack('LLLL')
131
+ io.seek offset + args.first, IO::SEEK_SET
132
+ name = io.read(size - 8 - (4 * 4)).unpack1('A*')
133
+ new(cmd, size, name, *args)
134
+ end
135
+
136
+ def initialize cmd, size, name, str_offset, timestamp, current_version, compat_version
137
+ super(cmd, size)
138
+ @name = name
139
+ @str_offset = str_offset
140
+ @timestamp = timestamp
141
+ @current_version = current_version
142
+ @compat_version = compat_version
143
+ end
144
+ end
145
+
146
+ class LC_LOAD_DYLINKER < Command
147
+ VALUE = 0xe
148
+
149
+ def self.from_io cmd, size, offset, io
150
+ # `size` is the total segment size including command and length bytes
151
+ # so we need to remove them from the size.
152
+ new(cmd, size, *io.read(size - 8).unpack('LA*'))
153
+ end
154
+
155
+ attr_reader :name
156
+
157
+ def initialize cmd, size, offset, name
158
+ super(cmd, size)
159
+ @offset = offset
160
+ @name = name
161
+ end
162
+ end
163
+
164
+ class LC_VERSION_MIN_MACOSX < Command
165
+ VALUE = 0x24
166
+ SIZE = 16 # uuid
167
+
168
+ def self.from_io cmd, size, offset, io
169
+ new(cmd, size, *io.read(SIZE))
170
+ end
171
+
172
+ attr_reader :uuid
173
+
174
+ def initialize cmd, size, uuid
175
+ super(cmd, size)
176
+ @uuid = uuid
177
+ end
178
+ end
179
+
180
+ class LC_UUID < Command
181
+ VALUE = 0x1b
182
+ SIZE = 16 # uuid
183
+
184
+ def self.from_io cmd, size, offset, io
185
+ new(cmd, size, *io.read(SIZE))
186
+ end
187
+
188
+ attr_reader :uuid
189
+
190
+ def initialize cmd, size, uuid
191
+ super(cmd, size)
192
+ @uuid = uuid
193
+ end
194
+ end
195
+
196
+ class LC_SOURCE_VERSION < Command
197
+ VALUE = 0x2A
198
+ SIZE = 8 # version
199
+
200
+ def self.from_io cmd, size, offset, io
201
+ new(cmd, size, *io.read(SIZE).unpack('Q'))
202
+ end
203
+
204
+ def initialize cmd, size, version
205
+ super(cmd, size)
206
+ @version = version
207
+ end
208
+ end
209
+
210
+ class LC_BUILD_VERSION < Command
211
+ VALUE = 0x32
212
+ SIZE = 4 + # platform
213
+ 4 + # minos
214
+ 4 + # sdk
215
+ 4 # ntools
216
+
217
+ def self.from_io cmd, size, offset, io
218
+ new(cmd, size, *io.read(SIZE).unpack('L4'))
219
+ end
220
+
221
+ attr_reader :platform, :minos, :sdk, :ntools
222
+
223
+ def initialize cmd, size, platform, minos, sdk, ntools
224
+ super(cmd, size)
225
+ @platform = platform
226
+ @minos = minos
227
+ @sdk = sdk
228
+ @ntools = ntools
229
+ end
230
+ end
231
+
232
+ class LC_SYMTAB < Command
233
+ VALUE = 0x2
234
+ SIZE = 4 + # symoff
235
+ 4 + # nsyms
236
+ 4 + # stroff
237
+ 4 # strsize
238
+
239
+ class NList < Struct.new(:name, :index, :type, :sect, :desc, :value)
240
+ using Module.new {
241
+ refine Integer do
242
+ def inspect; sprintf("%#x", self); end
243
+ end
244
+ }
245
+
246
+ # From stab.h
247
+ N_GSYM = 0x20 # global symbol
248
+ N_FNAME = 0x22 # function name
249
+ N_FUN = 0x24 # function
250
+ N_STSYM = 0x26
251
+ N_LCSYM = 0x28
252
+ N_BNSYM = 0x2e
253
+ N_AST = 0x32
254
+ N_OPT = 0x3c
255
+ N_RSYM = 0x40
256
+ N_SLINE = 0x44
257
+ N_ENSYM = 0x4e
258
+ N_SSYM = 0x60
259
+ N_SO = 0x64
260
+ N_OSO = 0x66
261
+
262
+ constants.each { |n|
263
+ define_method(n.to_s.sub(/^N_/, '').downcase + "?") { type == self.class::const_get(n) }
264
+ }
265
+
266
+ # define these constants after the above metaprogramming so they don't get
267
+ # metaprogrammed methods :grimace:
268
+
269
+ # From nlist.h
270
+ N_STAB = 0xe0
271
+ N_PEXT = 0x10
272
+ N_TYPE = 0x0e
273
+ N_EXT = 0x01
274
+
275
+ def stab?; (type & N_STAB) != 0; end
276
+
277
+ def archive?
278
+ oso? && name =~ /[(][^)]*[)]$/
279
+ end
280
+
281
+ def archive
282
+ name[/^.*(?=[(][^)]*[)]$)/]
283
+ end
284
+
285
+ def object
286
+ name[/(?<=[(])[^)]*(?=[)]$)/]
287
+ end
288
+
289
+ def inspect
290
+ start = "#<struct #{self.class} "
291
+ members.inject(start) { |buffer, member|
292
+ buffer + " #{member.to_s}=#{self[member].inspect}"
293
+ } + ">"
294
+ end
295
+ end
296
+
297
+ def self.from_io cmd, size, offset, io
298
+ current = io.pos
299
+ symoff, nsyms, stroff, strsize = *io.read(SIZE).unpack('L4')
300
+
301
+ io.seek stroff, IO::SEEK_SET
302
+ stable = io.read(strsize)
303
+
304
+ io.seek symoff, IO::SEEK_SET
305
+
306
+ nlist = nsyms.times.map do |i|
307
+ index = io.read(4).unpack1 'L'
308
+ x = stable.byteslice(index, stable.bytesize)
309
+ unless x
310
+ return new(cmd, size, symoff, nsyms, stroff, strsize, [])
311
+ end
312
+ name = x.unpack1 "Z*"
313
+ NList.new(name, index, *io.read(1 + 1 + 2 + 8).unpack('CCsQ'))
314
+ end
315
+ new(cmd, size, symoff, nsyms, stroff, strsize, nlist)
316
+ ensure
317
+ io.seek current, IO::SEEK_SET
318
+ end
319
+
320
+ attr_reader :symoff, :nsyms, :stroff, :strsize, :nlist
321
+
322
+ def initialize cmd, size, symoff, nsyms, stroff, strsize, nlist
323
+ super(cmd, size)
324
+ @symoff = symoff
325
+ @nsyms = nsyms
326
+ @stroff = stroff
327
+ @strsize = strsize
328
+ @nlist = nlist
329
+ end
330
+
331
+ def symtab?; true; end
332
+ end
333
+
334
+ class LC_DYSYMTAB < Command
335
+ VALUE = 0xb
336
+ SIZE = 18 * 4
337
+
338
+ def self.from_io cmd, size, offset, io
339
+ new(cmd, size, *io.read(SIZE).unpack('L18'))
340
+ end
341
+
342
+ attr_reader :ilocalsym,
343
+ :nlocalsym,
344
+ :iextdefsym,
345
+ :nextdefsym,
346
+ :iundefsym,
347
+ :nundefsym,
348
+ :tocoff,
349
+ :ntoc,
350
+ :modtaboff,
351
+ :nmodtab,
352
+ :extrefsymoff,
353
+ :nextrefsyms,
354
+ :indirectsymoff,
355
+ :nindirectsyms,
356
+ :extreloff,
357
+ :nextrel,
358
+ :locreloff,
359
+ :nlocrel
360
+
361
+ def initialize cmd,
362
+ size,
363
+ ilocalsym,
364
+ nlocalsym,
365
+ iextdefsym,
366
+ nextdefsym,
367
+ iundefsym,
368
+ nundefsym,
369
+ tocoff,
370
+ ntoc,
371
+ modtaboff,
372
+ nmodtab,
373
+ extrefsymoff,
374
+ nextrefsyms,
375
+ indirectsymoff,
376
+ nindirectsyms,
377
+ extreloff,
378
+ nextrel,
379
+ locreloff,
380
+ nlocrel
381
+ super(cmd, size)
382
+
383
+ @ilocalsym = ilocalsym
384
+ @nlocalsym = nlocalsym
385
+ @iextdefsym = iextdefsym
386
+ @nextdefsym = nextdefsym
387
+ @iundefsym = iundefsym
388
+ @nundefsym = nundefsym
389
+ @tocoff = tocoff
390
+ @ntoc = ntoc
391
+ @modtaboff = modtaboff
392
+ @nmodtab = nmodtab
393
+ @extrefsymoff = extrefsymoff
394
+ @nextrefsyms = nextrefsyms
395
+ @indirectsymoff = indirectsymoff
396
+ @nindirectsyms = nindirectsyms
397
+ @extreloff = extreloff
398
+ @nextrel = nextrel
399
+ @locreloff = locreloff
400
+ @nlocrel = nlocrel
401
+ end
402
+
403
+ def dysymtab?; true; end
404
+ end
405
+
406
+ class LC_SEGMENT_64 < Command
407
+ VALUE = 0x19
408
+ SIZE = 16 + # segname
409
+ 8 + # vmaddr
410
+ 8 + # vmsize
411
+ 8 + # fileoff
412
+ 8 + # filesize
413
+ 4 + # maxprot
414
+ 4 + # initprot
415
+ 4 + # nsects
416
+ 4 # flags
417
+
418
+ def self.from_io cmd, size, offset, io
419
+ new(cmd, size, *io.read(SIZE).unpack('A16Q4L4'))
420
+ end
421
+
422
+ attr_reader :segname, :vmaddr, :vmsize, :fileoff, :filesize, :maxprot, :initprot, :nsects, :flags
423
+
424
+ def initialize cmd, size, segname, vmaddr, vmsize, fileoff, filesize, maxprot, initprot, nsects, flags
425
+ super(cmd, size)
426
+ @segname = segname
427
+ @vmaddr = vmaddr
428
+ @vmsize = vmsize
429
+ @fileoff = fileoff
430
+ @filesize = filesize
431
+ @maxprot = maxprot
432
+ @initprot = initprot
433
+ @nsects = nsects
434
+ @flags = flags
435
+ end
436
+
437
+ def segment?; true; end
438
+ end
439
+
440
+ class Section < Struct.new :io, :start_pos, :sectname, :segname, :addr, :size, :offset, :align, :reloff, :nreloc, :flags, :reserved1, :reserved2, :reserved3
441
+
442
+ include SectionTypes
443
+
444
+ def section?; true; end
445
+
446
+ def as_dwarf
447
+ raise NotImplementedError, "load WORF" unless defined?(::WORF)
448
+
449
+ case sectname
450
+ when "__debug_abbrev"
451
+ WORF::DebugAbbrev.new io, self, start_pos
452
+ when "__debug_info"
453
+ WORF::DebugInfo.new io, self, start_pos
454
+ when "__debug_str"
455
+ WORF::DebugStrings.new io, self, start_pos
456
+ when "__debug_line"
457
+ WORF::DebugLine.new io, self, start_pos
458
+ else
459
+ raise NotImplementedError
460
+ end
461
+ end
462
+ end
463
+
464
+ include Enumerable
465
+
466
+ def executable?
467
+ header.executable_file?
468
+ end
469
+
470
+ def object?
471
+ header.object_file?
472
+ end
473
+
474
+ def dsym?
475
+ header.dsym_file?
476
+ end
477
+
478
+ def each
479
+ h = header
480
+
481
+ yield h
482
+
483
+ @fd.seek @start_pos + Header::SIZEOF, IO::SEEK_SET
484
+
485
+ next_pos = @fd.pos
486
+
487
+ h.ncmds.times do |i|
488
+ @fd.seek next_pos, IO::SEEK_SET
489
+
490
+ cmd, size = @fd.read(2 * 4).unpack('LL')
491
+
492
+ case cmd
493
+ when LC_SEGMENT_64::VALUE
494
+ lc = LC_SEGMENT_64.from_offset(next_pos, @fd)
495
+ yield lc
496
+ lc.nsects.times do
497
+ args = @fd.read(32 + (2 * 8) + (8 * 4)).unpack('A16A16QQL8')
498
+ yield Section.new(@fd, start_pos, *args)
499
+ end
500
+ when LC_FUNCTION_STARTS::VALUE
501
+ yield LC_FUNCTION_STARTS.from_offset(next_pos, @fd)
502
+ when LC_DATA_IN_CODE::VALUE
503
+ yield LC_DATA_IN_CODE.from_offset(next_pos, @fd)
504
+ when LC_BUILD_VERSION::VALUE
505
+ yield LC_BUILD_VERSION.from_offset(next_pos, @fd)
506
+ when LC_LOAD_DYLIB::VALUE
507
+ yield LC_LOAD_DYLIB.from_offset(next_pos, @fd)
508
+ when LC_LOAD_DYLINKER::VALUE
509
+ yield LC_LOAD_DYLINKER.from_offset(next_pos, @fd)
510
+ when LC_SOURCE_VERSION::VALUE
511
+ yield LC_SOURCE_VERSION.from_offset(next_pos, @fd)
512
+ when LC_SYMTAB::VALUE
513
+ yield LC_SYMTAB.from_offset(next_pos, @fd)
514
+ when LC_DYSYMTAB::VALUE
515
+ yield LC_DYSYMTAB.from_offset(next_pos, @fd)
516
+ when LC_UUID::VALUE
517
+ yield LC_UUID.from_offset(next_pos, @fd)
518
+ else
519
+ # Just skip stuff we don't know about
520
+ if $DEBUG
521
+ puts "Unknown command #{cmd}"
522
+ end
523
+ end
524
+
525
+ next_pos += size
526
+ end
527
+ end
528
+
529
+ def find_section name
530
+ find { |thing| thing.section? && thing.sectname == name }
531
+ end
532
+
533
+ def read section
534
+ save_pos do
535
+ @fd.seek @start_pos + section.offset, IO::SEEK_SET
536
+ data = @fd.read(section.size)
537
+ data.bytes.each_slice(16) do |list|
538
+ p list.map { |x| sprintf("%02x", x) }.join ' '
539
+ end
540
+ end
541
+ end
542
+
543
+ def header
544
+ @fd.seek @start_pos, IO::SEEK_SET
545
+ header = Header.new(*@fd.read(Header::SIZEOF).unpack('L8'))
546
+ # I don't want to deal with endianness
547
+ raise 'not supported' unless header.magic == HEADER_MAGIC
548
+ header
549
+ end
550
+
551
+ private
552
+
553
+ def save_pos
554
+ pos = @fd.pos
555
+ yield
556
+ ensure
557
+ @fd.seek pos, IO::SEEK_SET
558
+ end
559
+ end
560
+ end