Debian Codegraph @

debug/dwarf/info.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use errors;
5 use debug::image;
6 use format::elf;
7 use io;
8 use memio;
9
10 def INFO_VERSION: u16 = 4;
11
12 export type debug_info_reader = struct {
13 image: *image::image,
14 abbrev: abbrev_table,
15 strings: (string_table | void),
16 mem: *memio::stream,
17 rd: *table_reader,
18 };
19
20 // Reads the debug info from a DWARF image. Returns a [[debug_info_reader]],
21 // call [[debug_info_next]] to retrieve the next DIE.
22 //
23 // Pass the return value to [[debug_info_finish]] after you're done with it.
24 export fn read_debug_info(
25 image: *image::image,
26 offs: u64,
27 ) (debug_info_reader | void | io::error | nomem) = {
28 const sec = match (image::section_byname(image, ".debug_info")) {
29 case let sec: *elf::section64 =>
30 yield sec;
31 case null =>
32 return;
33 };
34
35 let ok = false;
36
37 const memrd = alloc(image::section_reader(image, sec))?;
38 defer if (!ok) free(memrd);
39
40 io::seek(memrd, offs: io::off, io::whence::SET)?;
41
42 const rd = match (new_table_reader(memrd, true)) {
43 case let rd: table_reader =>
44 yield alloc(rd)?;
45 case io::EOF =>
46 return;
47 };
48 defer if (!ok) free(rd);
49
50 const ver = read_uhalf(rd)!;
51 const abbrev_offs = read_secword(rd)!;
52 const asize = read_ubyte(rd)!;
53 assert(ver <= INFO_VERSION, "debug::dwarf: unsupported .debug_info version");
54 assert(asize == 8, "debug::dwarf: unsupported address size in .debug_info");
55
56 const abbrevs = match (load_abbrevs(image, abbrev_offs)?) {
57 case let tab: abbrev_table =>
58 yield tab;
59 case void =>
60 return;
61 };
62
63 ok = true;
64 return debug_info_reader {
65 image = image,
66 abbrev = abbrevs,
67 strings = load_strings(image)?,
68 mem = memrd,
69 rd = rd,
70 };
71 };
72
73 // Returns the next debug info [[entry]] (DIE) from a [[debug_info_reader]].
74 // Pass the return value to [[entry_finish]] when done.
75 export fn debug_info_next(di: *debug_info_reader) (entry | io::EOF | nomem) = {
76 if (read_iseof(di.rd)) {
77 return io::EOF;
78 };
79
80 let code = read_uleb128(di.rd)!;
81 for (code == 0) {
82 if (read_iseof(di.rd)) {
83 return io::EOF;
84 };
85 code = read_uleb128(di.rd)!;
86 };
87
88 const ref = get_abbrev(&di.abbrev, code);
89 assert(ref != null, "debug::dwarf: unknown abbreviated tag");
90
91 match (read_die(di, di.rd, ref as *abbrev)) {
92 case io::error => abort();
93 case nomem => return nomem;
94 case let ent: entry =>
95 return ent;
96 };
97 };
98
99 // Frees resources associated with a [[debug_info_reader]].
100 export fn debug_info_finish(di: *debug_info_reader) void = {
101 free(di.mem);
102 free(di.rd);
103 };
104
105 // A debug entry.
106 export type entry = struct {
107 tag: u32,
108 children: bool,
109 fields: []field,
110 };
111
112 // Frees resources associated with an [[entry]].
113 export fn entry_finish(ent: *entry) void = {
114 free(ent.fields);
115 };
116
117 // A debug [[entry]] field.
118 export type field = struct {
119 attr: u32,
120 form: u32,
121 union {
122 address: uintptr,
123 block: []u8,
124 constant: u64,
125 string: const str,
126 flag: bool,
127 reference: u64,
128 exprloc: []u8,
129 ptr: u64,
130 },
131 };
132
133 fn read_die(
134 ir: *debug_info_reader,
135 rd: *table_reader,
136 abbrev: *abbrev,
137 ) (entry | io::error) = {
138 let fields: []field = [];
139 let ok = false;
140 defer if (!ok) {
141 fields = [];
142 free(fields);
143 };
144
145 for (const abf &.. abbrev.fields) {
146 let field = field {
147 attr = abf.attr,
148 form = abf.form,
149 ...
150 };
151 let form = abf.form;
152 for (form == DW_FORM_indirect) {
153 form = read_uleb128(rd)?: u32;
154 };
155
156 // NOTE: Only supports up to DWARF 4 forms for now
157 switch (form) {
158 case DW_FORM_addr =>
159 field.address = read_ulong(rd)?: uintptr;
160 case DW_FORM_block =>
161 field.block = read_slice(rd, read_uleb128(rd)?)?;
162 case DW_FORM_block1 =>
163 field.block = read_slice(rd, read_ubyte(rd)?)?;
164 case DW_FORM_block2 =>
165 field.block = read_slice(rd, read_uhalf(rd)?)?;
166 case DW_FORM_block4 =>
167 field.block = read_slice(rd, read_uword(rd)?)?;
168 case DW_FORM_data1 =>
169 field.constant = read_ubyte(rd)?;
170 case DW_FORM_data2 =>
171 field.constant = read_uhalf(rd)?;
172 case DW_FORM_data4 =>
173 field.constant = read_uword(rd)?;
174 case DW_FORM_data8 =>
175 field.constant = read_ulong(rd)?;
176 case DW_FORM_udata =>
177 field.constant = read_uleb128(rd)?;
178 case DW_FORM_sdata =>
179 field.constant = read_sleb128(rd)?: u64;
180 case DW_FORM_string =>
181 field.string = read_string(rd)?;
182 case DW_FORM_strp =>
183 // TODO: Look up in .debug_strings
184 const offs = read_secword(rd)?;
185 match (ir.strings) {
186 case let tab: string_table =>
187 field.string = get_strp(&tab, offs);
188 case void =>
189 field.string = "(unknown)";
190 };
191 case DW_FORM_flag =>
192 field.flag = read_ubyte(rd)? != 0;
193 case DW_FORM_flag_present =>
194 field.flag = true;
195 case DW_FORM_ref_addr =>
196 field.reference = read_secword(rd)?;
197 case DW_FORM_ref1 =>
198 field.reference = read_ubyte(rd)?;
199 case DW_FORM_ref2 =>
200 field.reference = read_uhalf(rd)?;
201 case DW_FORM_ref4 =>
202 field.reference = read_uword(rd)?;
203 case DW_FORM_ref8 =>
204 field.reference = read_ulong(rd)?;
205 case DW_FORM_ref_udata =>
206 field.reference = read_uleb128(rd)?;
207 case DW_FORM_ref_sig8 =>
208 field.reference = read_ulong(rd)?;
209 case DW_FORM_sec_offset =>
210 field.reference = read_secword(rd)?;
211 case DW_FORM_exprloc =>
212 field.exprloc = read_slice(rd, read_uleb128(rd)?)?;
213 case DW_FORM_indirect => abort();
214 case => return errors::unsupported;
215 };
216
217 append(fields, field)?;
218 };
219
220 ok = true;
221 return entry {
222 tag = abbrev.tag,
223 children = abbrev.has_children,
224 fields = fields,
225 };
226 };