1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
debug_info: []const u8,
debug_abbrev: []const u8,
debug_str: []const u8,
pub fn getCompileUnitIterator(self: DwarfInfo) CompileUnitIterator {
return .{ .ctx = self };
}
const CompileUnitIterator = struct {
ctx: DwarfInfo,
pos: usize = 0,
pub fn next(self: *CompileUnitIterator) !?CompileUnit {
if (self.pos >= self.ctx.debug_info.len) return null;
var stream = std.io.fixedBufferStream(self.ctx.debug_info[self.pos..]);
var creader = std.io.countingReader(stream.reader());
const reader = creader.reader();
const cuh = try CompileUnit.Header.read(reader);
const total_length = cuh.length + @as(u64, if (cuh.is_64bit) @sizeOf(u64) else @sizeOf(u32));
const offset = math.cast(usize, creader.bytes_read) orelse return error.Overflow;
const cu = CompileUnit{
.cuh = cuh,
.debug_info_off = self.pos + offset,
};
self.pos += (math.cast(usize, total_length) orelse return error.Overflow);
return cu;
}
};
pub fn genSubprogramLookupByName(
self: DwarfInfo,
compile_unit: CompileUnit,
abbrev_lookup: AbbrevLookupTable,
lookup: *SubprogramLookupByName,
) !void {
var abbrev_it = compile_unit.getAbbrevEntryIterator(self);
while (try abbrev_it.next(abbrev_lookup)) |entry| switch (entry.tag) {
dwarf.TAG.subprogram => {
var attr_it = entry.getAttributeIterator(self, compile_unit.cuh);
var name: ?[]const u8 = null;
var low_pc: ?u64 = null;
var high_pc: ?u64 = null;
while (try attr_it.next()) |attr| switch (attr.name) {
dwarf.AT.name => if (attr.getString(self, compile_unit.cuh)) |str| {
name = str;
},
dwarf.AT.low_pc => {
if (attr.getAddr(self, compile_unit.cuh)) |addr| {
low_pc = addr;
}
if (try attr.getConstant(self)) |constant| {
low_pc = @as(u64, @intCast(constant));
}
},
dwarf.AT.high_pc => {
if (attr.getAddr(self, compile_unit.cuh)) |addr| {
high_pc = addr;
}
if (try attr.getConstant(self)) |constant| {
high_pc = @as(u64, @intCast(constant));
}
},
else => {},
};
if (name == null or low_pc == null or high_pc == null) continue;
try lookup.putNoClobber(name.?, .{ .addr = low_pc.?, .size = high_pc.? });
},
else => {},
};
}
pub fn genAbbrevLookupByKind(self: DwarfInfo, off: usize, lookup: *AbbrevLookupTable) !void {
const data = self.debug_abbrev[off..];
var stream = std.io.fixedBufferStream(data);
var creader = std.io.countingReader(stream.reader());
const reader = creader.reader();
while (true) {
const kind = try leb.readULEB128(u64, reader);
if (kind == 0) break;
const pos = math.cast(usize, creader.bytes_read) orelse return error.Overflow;
_ = try leb.readULEB128(u64, reader); // TAG
_ = try reader.readByte(); // CHILDREN
while (true) {
const name = try leb.readULEB128(u64, reader);
const form = try leb.readULEB128(u64, reader);
if (name == 0 and form == 0) break;
}
const next_pos = math.cast(usize, creader.bytes_read) orelse return error.Overflow;
try lookup.putNoClobber(kind, .{
.pos = pos,
.len = next_pos - pos - 2,
});
}
}
pub const CompileUnit = struct {
cuh: Header,
debug_info_off: usize,
pub const Header = struct {
is_64bit: bool,
length: u64,
version: u16,
debug_abbrev_offset: u64,
address_size: u8,
fn read(reader: anytype) !Header {
var length: u64 = try reader.readIntLittle(u32);
const is_64bit = length == 0xffffffff;
if (is_64bit) {
length = try reader.readIntLittle(u64);
}
const version = try reader.readIntLittle(u16);
const debug_abbrev_offset = if (is_64bit)
try reader.readIntLittle(u64)
else
try reader.readIntLittle(u32);
const address_size = try reader.readIntLittle(u8);
return Header{
.is_64bit = is_64bit,
.length = length,
.version = version,
.debug_abbrev_offset = debug_abbrev_offset,
.address_size = address_size,
};
}
};
inline fn getDebugInfo(self: CompileUnit, ctx: DwarfInfo) []const u8 {
return ctx.debug_info[self.debug_info_off..][0..self.cuh.length];
}
pub fn getAbbrevEntryIterator(self: CompileUnit, ctx: DwarfInfo) AbbrevEntryIterator {
return .{ .cu = self, .ctx = ctx };
}
};
const AbbrevEntryIterator = struct {
cu: CompileUnit,
ctx: DwarfInfo,
pos: usize = 0,
pub fn next(self: *AbbrevEntryIterator, lookup: AbbrevLookupTable) !?AbbrevEntry {
if (self.pos + self.cu.debug_info_off >= self.ctx.debug_info.len) return null;
const debug_info = self.ctx.debug_info[self.pos + self.cu.debug_info_off ..];
var stream = std.io.fixedBufferStream(debug_info);
var creader = std.io.countingReader(stream.reader());
const reader = creader.reader();
const kind = try leb.readULEB128(u64, reader);
self.pos += (math.cast(usize, creader.bytes_read) orelse return error.Overflow);
if (kind == 0) {
return AbbrevEntry.null();
}
const abbrev_pos = lookup.get(kind) orelse return null;
const len = try findAbbrevEntrySize(
self.ctx,
abbrev_pos.pos,
abbrev_pos.len,
self.pos + self.cu.debug_info_off,
self.cu.cuh,
);
const entry = try getAbbrevEntry(
self.ctx,
abbrev_pos.pos,
abbrev_pos.len,
self.pos + self.cu.debug_info_off,
len,
);
self.pos += len;
return entry;
}
};
pub const AbbrevEntry = struct {
tag: u64,
children: u8,
debug_abbrev_off: usize,
debug_abbrev_len: usize,
debug_info_off: usize,
debug_info_len: usize,
fn @"null"() AbbrevEntry {
return .{
.tag = 0,
.children = dwarf.CHILDREN.no,
.debug_abbrev_off = 0,
.debug_abbrev_len = 0,
.debug_info_off = 0,
.debug_info_len = 0,
};
}
pub fn hasChildren(self: AbbrevEntry) bool {
return self.children == dwarf.CHILDREN.yes;
}
inline fn getDebugInfo(self: AbbrevEntry, ctx: DwarfInfo) []const u8 {
return ctx.debug_info[self.debug_info_off..][0..self.debug_info_len];
}
inline fn getDebugAbbrev(self: AbbrevEntry, ctx: DwarfInfo) []const u8 {
return ctx.debug_abbrev[self.debug_abbrev_off..][0..self.debug_abbrev_len];
}
pub fn getAttributeIterator(self: AbbrevEntry, ctx: DwarfInfo, cuh: CompileUnit.Header) AttributeIterator {
return .{ .entry = self, .ctx = ctx, .cuh = cuh };
}
};
pub const Attribute = struct {
name: u64,
form: u64,
debug_info_off: usize,
debug_info_len: usize,
inline fn getDebugInfo(self: Attribute, ctx: DwarfInfo) []const u8 {
return ctx.debug_info[self.debug_info_off..][0..self.debug_info_len];
}
pub fn getString(self: Attribute, ctx: DwarfInfo, cuh: CompileUnit.Header) ?[]const u8 {
const debug_info = self.getDebugInfo(ctx);
switch (self.form) {
dwarf.FORM.string => {
return mem.sliceTo(@as([*:0]const u8, @ptrCast(debug_info.ptr)), 0);
},
dwarf.FORM.strp => {
const off = if (cuh.is_64bit)
mem.readIntLittle(u64, debug_info[0..8])
else
mem.readIntLittle(u32, debug_info[0..4]);
return ctx.getString(off);
},
else => return null,
}
}
pub fn getConstant(self: Attribute, ctx: DwarfInfo) !?i128 {
const debug_info = self.getDebugInfo(ctx);
var stream = std.io.fixedBufferStream(debug_info);
const reader = stream.reader();
return switch (self.form) {
dwarf.FORM.data1 => debug_info[0],
dwarf.FORM.data2 => mem.readIntLittle(u16, debug_info[0..2]),
dwarf.FORM.data4 => mem.readIntLittle(u32, debug_info[0..4]),
dwarf.FORM.data8 => mem.readIntLittle(u64, debug_info[0..8]),
dwarf.FORM.udata => try leb.readULEB128(u64, reader),
dwarf.FORM.sdata => try leb.readILEB128(i64, reader),
else => null,
};
}
pub fn getAddr(self: Attribute, ctx: DwarfInfo, cuh: CompileUnit.Header) ?u64 {
if (self.form != dwarf.FORM.addr) return null;
const debug_info = self.getDebugInfo(ctx);
return switch (cuh.address_size) {
1 => debug_info[0],
2 => mem.readIntLittle(u16, debug_info[0..2]),
4 => mem.readIntLittle(u32, debug_info[0..4]),
8 => mem.readIntLittle(u64, debug_info[0..8]),
else => unreachable,
};
}
};
const AttributeIterator = struct {
entry: AbbrevEntry,
ctx: DwarfInfo,
cuh: CompileUnit.Header,
debug_abbrev_pos: usize = 0,
debug_info_pos: usize = 0,
pub fn next(self: *AttributeIterator) !?Attribute {
const debug_abbrev = self.entry.getDebugAbbrev(self.ctx);
if (self.debug_abbrev_pos >= debug_abbrev.len) return null;
var stream = std.io.fixedBufferStream(debug_abbrev[self.debug_abbrev_pos..]);
var creader = std.io.countingReader(stream.reader());
const reader = creader.reader();
const name = try leb.readULEB128(u64, reader);
const form = try leb.readULEB128(u64, reader);
self.debug_abbrev_pos += (math.cast(usize, creader.bytes_read) orelse return error.Overflow);
const len = try findFormSize(
self.ctx,
form,
self.debug_info_pos + self.entry.debug_info_off,
self.cuh,
);
const attr = Attribute{
.name = name,
.form = form,
.debug_info_off = self.debug_info_pos + self.entry.debug_info_off,
.debug_info_len = len,
};
self.debug_info_pos += len;
return attr;
}
};
fn getAbbrevEntry(self: DwarfInfo, da_off: usize, da_len: usize, di_off: usize, di_len: usize) !AbbrevEntry {
const debug_abbrev = self.debug_abbrev[da_off..][0..da_len];
var stream = std.io.fixedBufferStream(debug_abbrev);
var creader = std.io.countingReader(stream.reader());
const reader = creader.reader();
const tag = try leb.readULEB128(u64, reader);
const children = switch (tag) {
std.dwarf.TAG.const_type,
std.dwarf.TAG.packed_type,
std.dwarf.TAG.pointer_type,
std.dwarf.TAG.reference_type,
std.dwarf.TAG.restrict_type,
std.dwarf.TAG.rvalue_reference_type,
std.dwarf.TAG.shared_type,
std.dwarf.TAG.volatile_type,
=> if (creader.bytes_read == da_len) std.dwarf.CHILDREN.no else try reader.readByte(),
else => try reader.readByte(),
};
const pos = math.cast(usize, creader.bytes_read) orelse return error.Overflow;
return AbbrevEntry{
.tag = tag,
.children = children,
.debug_abbrev_off = pos + da_off,
.debug_abbrev_len = da_len - pos,
.debug_info_off = di_off,
.debug_info_len = di_len,
};
}
fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Header) !usize {
const debug_info = self.debug_info[di_off..];
var stream = std.io.fixedBufferStream(debug_info);
var creader = std.io.countingReader(stream.reader());
const reader = creader.reader();
switch (form) {
dwarf.FORM.strp,
dwarf.FORM.sec_offset,
dwarf.FORM.ref_addr,
=> return if (cuh.is_64bit) @sizeOf(u64) else @sizeOf(u32),
dwarf.FORM.addr => return cuh.address_size,
dwarf.FORM.block1,
dwarf.FORM.block2,
dwarf.FORM.block4,
dwarf.FORM.block,
=> {
const len: u64 = switch (form) {
dwarf.FORM.block1 => try reader.readIntLittle(u8),
dwarf.FORM.block2 => try reader.readIntLittle(u16),
dwarf.FORM.block4 => try reader.readIntLittle(u32),
dwarf.FORM.block => try leb.readULEB128(u64, reader),
else => unreachable,
};
var i: u64 = 0;
while (i < len) : (i += 1) {
_ = try reader.readByte();
}
return math.cast(usize, creader.bytes_read) orelse error.Overflow;
},
dwarf.FORM.exprloc => {
const expr_len = try leb.readULEB128(u64, reader);
var i: u64 = 0;
while (i < expr_len) : (i += 1) {
_ = try reader.readByte();
}
return math.cast(usize, creader.bytes_read) orelse error.Overflow;
},
dwarf.FORM.flag_present => return 0,
dwarf.FORM.data1,
dwarf.FORM.ref1,
dwarf.FORM.flag,
=> return @sizeOf(u8),
dwarf.FORM.data2,
dwarf.FORM.ref2,
=> return @sizeOf(u16),
dwarf.FORM.data4,
dwarf.FORM.ref4,
=> return @sizeOf(u32),
dwarf.FORM.data8,
dwarf.FORM.ref8,
dwarf.FORM.ref_sig8,
=> return @sizeOf(u64),
dwarf.FORM.udata,
dwarf.FORM.ref_udata,
=> {
_ = try leb.readULEB128(u64, reader);
return math.cast(usize, creader.bytes_read) orelse error.Overflow;
},
dwarf.FORM.sdata => {
_ = try leb.readILEB128(i64, reader);
return math.cast(usize, creader.bytes_read) orelse error.Overflow;
},
dwarf.FORM.string => {
var count: usize = 0;
while (true) {
const byte = try reader.readByte();
count += 1;
if (byte == 0x0) break;
}
return count;
},
else => {
// TODO figure out how to handle this
log.debug("unhandled DW_FORM_* value with identifier {x}", .{form});
return error.UnhandledDwFormValue;
},
}
}
fn findAbbrevEntrySize(self: DwarfInfo, da_off: usize, da_len: usize, di_off: usize, cuh: CompileUnit.Header) !usize {
const debug_abbrev = self.debug_abbrev[da_off..][0..da_len];
var stream = std.io.fixedBufferStream(debug_abbrev);
var creader = std.io.countingReader(stream.reader());
const reader = creader.reader();
const tag = try leb.readULEB128(u64, reader);
switch (tag) {
std.dwarf.TAG.const_type,
std.dwarf.TAG.packed_type,
std.dwarf.TAG.pointer_type,
std.dwarf.TAG.reference_type,
std.dwarf.TAG.restrict_type,
std.dwarf.TAG.rvalue_reference_type,
std.dwarf.TAG.shared_type,
std.dwarf.TAG.volatile_type,
=> if (creader.bytes_read != da_len) {
_ = try reader.readByte();
},
else => _ = try reader.readByte(),
}
var len: usize = 0;
while (creader.bytes_read < debug_abbrev.len) {
_ = try leb.readULEB128(u64, reader);
const form = try leb.readULEB128(u64, reader);
const form_len = try self.findFormSize(form, di_off + len, cuh);
len += form_len;
}
return len;
}
fn getString(self: DwarfInfo, off: u64) []const u8 {
assert(off < self.debug_str.len);
return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.debug_str.ptr + @as(usize, @intCast(off)))), 0);
}
const DwarfInfo = @This();
const std = @import("std");
const assert = std.debug.assert;
const dwarf = std.dwarf;
const leb = std.leb;
const log = std.log.scoped(.macho);
const math = std.math;
const mem = std.mem;
const Allocator = mem.Allocator;
pub const AbbrevLookupTable = std.AutoHashMap(u64, struct { pos: usize, len: usize });
pub const SubprogramLookupByName = std.StringHashMap(struct { addr: u64, size: u64 });
|