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
|
//! A thin wrapper around `Dwarf` which handles loading debug information from an ELF file. Load the
//! info with `load`, then directly access the `dwarf` field before finally `deinit`ing.
dwarf: Dwarf,
/// If we encounter a `.eh_frame` section while loading the ELF module, it is stored here and may be
/// used with `Dwarf.Unwind` for call stack unwinding.
eh_frame: ?UnwindSection,
/// If we encounter a `.debug_frame` section while loading the ELF module, it is stored here and may
/// be used with `Dwarf.Unwind` for call stack unwinding.
debug_frame: ?UnwindSection,
/// The memory-mapped ELF file, which is referenced by `dwarf`. This field is here only so that
/// this memory can be unmapped by `ElfModule.deinit`.
mapped_file: []align(std.heap.page_size_min) const u8,
/// Sometimes, debug info is stored separately to the main ELF file. In that case, `mapped_file`
/// is the mapped ELF binary, and `mapped_debug_file` is the mapped debug info file. Both must
/// be unmapped by `ElfModule.deinit`.
mapped_debug_file: ?[]align(std.heap.page_size_min) const u8,
pub const UnwindSection = struct {
vaddr: u64,
bytes: []const u8,
owned: bool,
};
pub fn deinit(em: *ElfModule, gpa: Allocator) void {
em.dwarf.deinit(gpa);
std.posix.munmap(em.mapped_file);
if (em.mapped_debug_file) |m| std.posix.munmap(m);
if (em.eh_frame) |s| if (s.owned) gpa.free(s.bytes);
if (em.debug_frame) |s| if (s.owned) gpa.free(s.bytes);
}
pub const LoadError = error{
InvalidDebugInfo,
MissingDebugInfo,
InvalidElfMagic,
InvalidElfVersion,
InvalidElfEndian,
/// TODO: implement this and then remove this error code
UnimplementedDwarfForeignEndian,
/// The debug info may be valid but this implementation uses memory
/// mapping which limits things to usize. If the target debug info is
/// 64-bit and host is 32-bit, there may be debug info that is not
/// supportable using this method.
Overflow,
PermissionDenied,
LockedMemoryLimitExceeded,
MemoryMappingNotSupported,
} || Allocator.Error || std.fs.File.OpenError || Dwarf.OpenError;
/// Reads debug info from an ELF file given its path.
///
/// If the required sections aren't present but a reference to external debug
/// info is, then this this function will recurse to attempt to load the debug
/// sections from an external file.
pub fn load(
gpa: Allocator,
elf_file_path: Path,
build_id: ?[]const u8,
expected_crc: ?u32,
parent_sections: ?*Dwarf.SectionArray,
parent_mapped_mem: ?[]align(std.heap.page_size_min) const u8,
) LoadError!ElfModule {
const mapped_mem: []align(std.heap.page_size_min) const u8 = mapped: {
const elf_file = try elf_file_path.root_dir.handle.openFile(elf_file_path.sub_path, .{});
defer elf_file.close();
const file_len = std.math.cast(
usize,
elf_file.getEndPos() catch return Dwarf.bad(),
) orelse return error.Overflow;
break :mapped std.posix.mmap(
null,
file_len,
std.posix.PROT.READ,
.{ .TYPE = .SHARED },
elf_file.handle,
0,
) catch |err| switch (err) {
error.MappingAlreadyExists => unreachable,
else => |e| return e,
};
};
errdefer std.posix.munmap(mapped_mem);
if (expected_crc) |crc| if (crc != std.hash.crc.Crc32.hash(mapped_mem)) return error.InvalidDebugInfo;
const hdr: *const elf.Ehdr = @ptrCast(&mapped_mem[0]);
if (!mem.eql(u8, hdr.e_ident[0..4], elf.MAGIC)) return error.InvalidElfMagic;
if (hdr.e_ident[elf.EI_VERSION] != 1) return error.InvalidElfVersion;
const endian: std.builtin.Endian = switch (hdr.e_ident[elf.EI_DATA]) {
elf.ELFDATA2LSB => .little,
elf.ELFDATA2MSB => .big,
else => return error.InvalidElfEndian,
};
if (endian != native_endian) return error.UnimplementedDwarfForeignEndian;
const shoff = hdr.e_shoff;
const str_section_off = std.math.cast(
usize,
shoff + @as(u64, hdr.e_shentsize) * @as(u64, hdr.e_shstrndx),
) orelse return error.Overflow;
const str_shdr: *const elf.Shdr = @ptrCast(@alignCast(mapped_mem[str_section_off..]));
const header_strings = mapped_mem[str_shdr.sh_offset..][0..str_shdr.sh_size];
const shdrs = @as(
[*]const elf.Shdr,
@ptrCast(@alignCast(&mapped_mem[shoff])),
)[0..hdr.e_shnum];
var sections: Dwarf.SectionArray = @splat(null);
// Combine section list. This takes ownership over any owned sections from the parent scope.
if (parent_sections) |ps| {
for (ps, §ions) |*parent, *section_elem| {
if (parent.*) |*p| {
section_elem.* = p.*;
p.owned = false;
}
}
}
errdefer for (sections) |opt_section| if (opt_section) |s| if (s.owned) gpa.free(s.data);
var eh_frame_section: ?UnwindSection = null;
errdefer if (eh_frame_section) |s| if (s.owned) gpa.free(s.bytes);
var debug_frame_section: ?UnwindSection = null;
errdefer if (debug_frame_section) |s| if (s.owned) gpa.free(s.bytes);
var separate_debug_filename: ?[]const u8 = null;
var separate_debug_crc: ?u32 = null;
for (shdrs) |*shdr| {
if (shdr.sh_type == elf.SHT_NULL or shdr.sh_type == elf.SHT_NOBITS) continue;
const name = mem.sliceTo(header_strings[shdr.sh_name..], 0);
if (mem.eql(u8, name, ".gnu_debuglink")) {
if (mapped_mem.len < shdr.sh_offset + shdr.sh_size) return error.InvalidDebugInfo;
const gnu_debuglink = mapped_mem[@intCast(shdr.sh_offset)..][0..@intCast(shdr.sh_size)];
const debug_filename = mem.sliceTo(@as([*:0]const u8, @ptrCast(gnu_debuglink.ptr)), 0);
const crc_offset = mem.alignForward(usize, debug_filename.len + 1, 4);
const crc_bytes = gnu_debuglink[crc_offset..][0..4];
separate_debug_crc = mem.readInt(u32, crc_bytes, endian);
separate_debug_filename = debug_filename;
continue;
}
const section_id: union(enum) {
dwarf: Dwarf.Section.Id,
eh_frame,
debug_frame,
} = s: {
inline for (@typeInfo(Dwarf.Section.Id).@"enum".fields) |s| {
if (mem.eql(u8, "." ++ s.name, name)) {
break :s .{ .dwarf = @enumFromInt(s.value) };
}
}
if (mem.eql(u8, ".eh_frame", name)) break :s .eh_frame;
if (mem.eql(u8, ".debug_frame", name)) break :s .debug_frame;
continue;
};
switch (section_id) {
.dwarf => |i| if (sections[@intFromEnum(i)] != null) continue,
.eh_frame => if (eh_frame_section != null) continue,
.debug_frame => if (debug_frame_section != null) continue,
}
if (mapped_mem.len < shdr.sh_offset + shdr.sh_size) return error.InvalidDebugInfo;
const raw_section_bytes = mapped_mem[@intCast(shdr.sh_offset)..][0..@intCast(shdr.sh_size)];
const section_bytes: []const u8, const section_owned: bool = section: {
if ((shdr.sh_flags & elf.SHF_COMPRESSED) == 0) {
break :section .{ raw_section_bytes, false };
}
var section_reader: Reader = .fixed(raw_section_bytes);
const chdr = section_reader.takeStruct(elf.Chdr, endian) catch continue;
if (chdr.ch_type != .ZLIB) continue;
var decompress: std.compress.flate.Decompress = .init(§ion_reader, .zlib, &.{});
var decompressed_section: ArrayList(u8) = .empty;
defer decompressed_section.deinit(gpa);
decompress.reader.appendRemainingUnlimited(gpa, &decompressed_section) catch {
Dwarf.invalidDebugInfoDetected();
continue;
};
if (chdr.ch_size != decompressed_section.items.len) {
Dwarf.invalidDebugInfoDetected();
continue;
}
break :section .{ try decompressed_section.toOwnedSlice(gpa), true };
};
switch (section_id) {
.dwarf => |id| sections[@intFromEnum(id)] = .{
.data = section_bytes,
.owned = section_owned,
},
.eh_frame => eh_frame_section = .{
.vaddr = shdr.sh_addr,
.bytes = section_bytes,
.owned = section_owned,
},
.debug_frame => debug_frame_section = .{
.vaddr = shdr.sh_addr,
.bytes = section_bytes,
.owned = section_owned,
},
}
}
const missing_debug_info =
sections[@intFromEnum(Dwarf.Section.Id.debug_info)] == null or
sections[@intFromEnum(Dwarf.Section.Id.debug_abbrev)] == null or
sections[@intFromEnum(Dwarf.Section.Id.debug_str)] == null or
sections[@intFromEnum(Dwarf.Section.Id.debug_line)] == null;
// Attempt to load debug info from an external file
// See: https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
if (missing_debug_info) {
// Only allow one level of debug info nesting
if (parent_mapped_mem) |_| {
return error.MissingDebugInfo;
}
// $XDG_CACHE_HOME/debuginfod_client/<buildid>/debuginfo
// This only opportunisticly tries to load from the debuginfod cache, but doesn't try to populate it.
// One can manually run `debuginfod-find debuginfo PATH` to download the symbols
debuginfod: {
const id = build_id orelse break :debuginfod;
switch (builtin.os.tag) {
.wasi, .windows => break :debuginfod,
else => {},
}
const id_dir_path: []u8 = p: {
if (std.posix.getenv("DEBUGINFOD_CACHE_PATH")) |path| {
break :p try std.fmt.allocPrint(gpa, "{s}/{x}", .{ path, id });
}
if (std.posix.getenv("XDG_CACHE_HOME")) |cache_path| {
if (cache_path.len > 0) {
break :p try std.fmt.allocPrint(gpa, "{s}/debuginfod_client/{x}", .{ cache_path, id });
}
}
if (std.posix.getenv("HOME")) |home_path| {
break :p try std.fmt.allocPrint(gpa, "{s}/.cache/debuginfod_client/{x}", .{ home_path, id });
}
break :debuginfod;
};
defer gpa.free(id_dir_path);
if (!std.fs.path.isAbsolute(id_dir_path)) break :debuginfod;
var id_dir = std.fs.openDirAbsolute(id_dir_path, .{}) catch break :debuginfod;
defer id_dir.close();
return load(gpa, .{
.root_dir = .{ .path = id_dir_path, .handle = id_dir },
.sub_path = "debuginfo",
}, null, separate_debug_crc, §ions, mapped_mem) catch break :debuginfod;
}
const global_debug_directories = [_][]const u8{
"/usr/lib/debug",
};
// <global debug directory>/.build-id/<2-character id prefix>/<id remainder>.debug
if (build_id) |id| blk: {
if (id.len < 3) break :blk;
// Either md5 (16 bytes) or sha1 (20 bytes) are used here in practice
const extension = ".debug";
var id_prefix_buf: [2]u8 = undefined;
var filename_buf: [38 + extension.len]u8 = undefined;
_ = std.fmt.bufPrint(&id_prefix_buf, "{x}", .{id[0..1]}) catch unreachable;
const filename = std.fmt.bufPrint(&filename_buf, "{x}" ++ extension, .{id[1..]}) catch break :blk;
for (global_debug_directories) |global_directory| {
const path: Path = .{
.root_dir = .cwd(),
.sub_path = try std.fs.path.join(gpa, &.{
global_directory, ".build-id", &id_prefix_buf, filename,
}),
};
defer gpa.free(path.sub_path);
return load(gpa, path, null, separate_debug_crc, §ions, mapped_mem) catch continue;
}
}
// use the path from .gnu_debuglink, in the same search order as gdb
separate: {
const separate_filename = separate_debug_filename orelse break :separate;
if (mem.eql(u8, std.fs.path.basename(elf_file_path.sub_path), separate_filename))
return error.MissingDebugInfo;
exe_dir: {
const exe_dir_path = try std.fs.path.resolve(gpa, &.{
elf_file_path.root_dir.path orelse ".",
std.fs.path.dirname(elf_file_path.sub_path) orelse ".",
});
defer gpa.free(exe_dir_path);
var exe_dir = std.fs.openDirAbsolute(exe_dir_path, .{}) catch break :exe_dir;
defer exe_dir.close();
// <exe_dir>/<gnu_debuglink>
if (load(
gpa,
.{
.root_dir = .{ .path = exe_dir_path, .handle = exe_dir },
.sub_path = separate_filename,
},
null,
separate_debug_crc,
§ions,
mapped_mem,
)) |em| {
return em;
} else |_| {}
// <exe_dir>/.debug/<gnu_debuglink>
const path: Path = .{
.root_dir = .{ .path = exe_dir_path, .handle = exe_dir },
.sub_path = try std.fs.path.join(gpa, &.{ ".debug", separate_filename }),
};
defer gpa.free(path.sub_path);
if (load(gpa, path, null, separate_debug_crc, §ions, mapped_mem)) |em| {
return em;
} else |_| {}
}
var cwd_buf: [std.fs.max_path_bytes]u8 = undefined;
const cwd_path = std.posix.realpath(".", &cwd_buf) catch break :separate;
// <global debug directory>/<absolute folder of current binary>/<gnu_debuglink>
for (global_debug_directories) |global_directory| {
const path: Path = .{
.root_dir = .cwd(),
.sub_path = try std.fs.path.join(gpa, &.{ global_directory, cwd_path, separate_filename }),
};
defer gpa.free(path.sub_path);
if (load(gpa, path, null, separate_debug_crc, §ions, mapped_mem)) |em| {
return em;
} else |_| {}
}
}
return error.MissingDebugInfo;
}
var dwarf: Dwarf = .{ .sections = sections };
try dwarf.open(gpa, endian);
return .{
.dwarf = dwarf,
.eh_frame = eh_frame_section,
.debug_frame = debug_frame_section,
.mapped_file = parent_mapped_mem orelse mapped_mem,
.mapped_debug_file = if (parent_mapped_mem != null) mapped_mem else null,
};
}
const std = @import("../../std.zig");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const Dwarf = std.debug.Dwarf;
const Path = std.Build.Cache.Path;
const Reader = std.Io.Reader;
const mem = std.mem;
const elf = std.elf;
const builtin = @import("builtin");
const native_endian = builtin.cpu.arch.endian();
const ElfModule = @This();
|