Debian Codegraph @

debug/dwarf/line.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use debug::image;
5 use format::elf;
6 use io;
7 use memio;
8
9 def MIN_LINE_VERSION: u16 = 2;
10 def MAX_LINE_VERSION: u16 = 3;
11
12 // Boolean flags for the line number state machine
13 export type line_flag = enum uint {
14 NONE = 0,
15 IS_STMT = 1 << 0,
16 BASIC_BLOCK = 1 << 1,
17 END_SEQUENCE = 1 << 2,
18 PROLOGUE_END = 1 << 3,
19 EPILOGUE_BEGIN = 1 << 4,
20 };
21
22 // Line number program state
23 export type line_state = struct {
24 vm_loc: u64,
25 addr: uintptr,
26 op_index: uint,
27 file: uint,
28 line: uint,
29 column: uint,
30 flags: line_flag,
31 isa: uint,
32 discriminator: uint,
33 };
34
35 // A file with associated line numbers.
36 export type line_file = struct {
37 name: str,
38 dir: u64,
39 mtime: u64,
40 length: u64,
41 };
42
43 // Header information for a .debug_line program.
44 export type line_header = struct {
45 min_instr_length: u8,
46 max_ops_per_instr: u8,
47 default_isstmt: bool,
48 line_base: i8,
49 line_range: u8,
50 opcode_base: u8,
51 opcode_lengths: []u8,
52 dirs: []str,
53 files: []line_file,
54 };
55
56 // Line number program
57 export type line_program = struct {
58 mem: *memio::stream,
59 rd: *table_reader,
60 state: line_state,
61 head: line_header,
62 };
63
64 // Initializes a new line number state machine to run the line number program at
65 // the specified offset in .debug_line.
66 //
67 // Use [[line_step]] to step the state machine, and pass the result to
68 // [[line_program_finish]] to free resources associated with the state machine
69 // when done using it.
70 export fn exec_line_program(
71 image: *image::image,
72 offs: u64,
73 ) (line_program | void | io::error | nomem) = {
74 const sec = match (image::section_byname(image, ".debug_line")) {
75 case let sec: *elf::section64 =>
76 yield sec;
77 case null =>
78 return;
79 };
80 const memrd = alloc(image::section_reader(image, sec))?;
81 io::seek(memrd, offs: io::off, io::whence::SET)?;
82
83 const rd = alloc(new_table_reader(memrd, true)? as table_reader);
84 if (rd is nomem) free(memrd);
85 const rd = rd?;
86
87 let ok = false;
88 defer if (!ok) {
89 free(memrd);
90 free(rd);
91 };
92
93 // Read program header
94 const ver = read_uhalf(rd)!;
95 assert(ver >= MIN_LINE_VERSION && ver <= MAX_LINE_VERSION,
96 "debug::dwarf: unsupported .debug_line version");
97
98 let head = line_header { ... };
99 defer if (!ok) {
100 free(head.opcode_lengths);
101 free(head.dirs);
102 free(head.files);
103 };
104
105 const head_len = read_secword(rd)?;
106 head.min_instr_length = read_ubyte(rd)?;
107 head.max_ops_per_instr = 1; // Non-VLIW architectures only
108 head.default_isstmt = read_ubyte(rd)? != 0;
109 head.line_base = read_sbyte(rd)?;
110 head.line_range = read_ubyte(rd)?;
111 head.opcode_base = read_ubyte(rd)?;
112
113 // Opcode lengths
114 for (let i = 0u8; i < head.opcode_base - 1; i += 1) {
115 const op = read_ubyte(rd)?;
116 append(head.opcode_lengths, op)?;
117 };
118
119 // Directories
120 for (true) {
121 const dir = read_string(rd)?;
122 if (len(dir) == 0) {
123 break;
124 };
125 append(head.dirs, dir)?;
126 };
127
128 // Files
129 for (true) {
130 const name = read_string(rd)?;
131 if (len(name) == 0) {
132 break;
133 };
134 const dir = read_uleb128(rd)?;
135 const mtime = read_uleb128(rd)?;
136 const length = read_uleb128(rd)?;
137 append(head.files, line_file {
138 name = name,
139 dir = dir,
140 mtime = mtime,
141 length = length,
142 })?;
143 };
144
145 ok = true;
146
147 let prog = line_program {
148 mem = memrd,
149 rd = rd,
150 state = line_state { ... },
151 head = head,
152 };
153 line_prog_reset(&prog);
154 return prog;
155 };
156
157 fn line_prog_reset(prog: *line_program) void = {
158 const head = &prog.head;
159 prog.state = line_state {
160 vm_loc = 0,
161 addr = 0,
162 op_index = 0,
163 file = 1,
164 line = 1,
165 column = 0,
166 flags = if (head.default_isstmt) line_flag::IS_STMT else 0,
167 isa = 0,
168 discriminator = 0,
169 };
170 };
171
172 // Frees resources associated with a [[line_program]].
173 export fn line_program_finish(prog: *line_program) void = {
174 free(prog.mem);
175 free(prog.rd);
176 free(prog.head.opcode_lengths);
177 free(prog.head.dirs);
178 free(prog.head.files);
179 };
180
181 // Runs the line number state machine until the next COPY instruction.
182 export fn line_next(
183 prog: *line_program,
184 ) (line_state | io::EOF | io::error | nomem) = {
185 for (true) {
186 match (line_step(prog)?) {
187 case let state: line_state =>
188 return state;
189 case io::EOF =>
190 return io::EOF;
191 case void => continue;
192 };
193 };
194 };
195
196 // Step the line number state machine. Returns the current line_state on a copy
197 // or end-of-sequence instruction, [[io::EOF]] at the end of the file, or void
198 // otherwise.
199 export fn line_step(
200 prog: *line_program,
201 ) (line_state | void | io::EOF | io::error | nomem) = {
202 let state = &prog.state;
203 if (read_iseof(prog.rd)) {
204 return io::EOF;
205 };
206 state.vm_loc = read_tell(prog.rd);
207
208 const opcode = read_ubyte(prog.rd)?;
209 if (opcode == 0) {
210 // Extended opcode
211 const length = read_uleb128(prog.rd)?;
212 const opcode = read_ubyte(prog.rd)?;
213 switch (opcode) {
214 case DW_LNE_end_sequence =>
215 let copy = *state;
216 line_prog_reset(prog);
217 return copy;
218 case DW_LNE_set_address =>
219 state.addr = read_ulong(prog.rd)?: uintptr;
220 case DW_LNE_define_file =>
221 const name = read_string(prog.rd)?;
222 const dir = read_uleb128(prog.rd)?;
223 const mtime = read_uleb128(prog.rd)?;
224 const length = read_uleb128(prog.rd)?;
225 append(prog.head.files, line_file {
226 name = name,
227 dir = dir,
228 mtime = mtime,
229 length = length,
230 })?;
231 state.file = len(prog.head.files): uint;
232 case DW_LNE_set_discriminator =>
233 state.discriminator = read_uleb128(prog.rd)?: uint;
234 case =>
235 // Unknown opcode, skip
236 read_slice(prog.rd, length - 1)?;
237 };
238 } else if (opcode < prog.head.opcode_base) {
239 // Special opcode
240 switch (opcode) {
241 case DW_LNS_copy =>
242 let copy = *state;
243 state.discriminator = 0;
244 state.flags &= ~(
245 line_flag::BASIC_BLOCK |
246 line_flag::PROLOGUE_END |
247 line_flag::EPILOGUE_BEGIN);
248 return copy;
249 case DW_LNS_advance_pc =>
250 const op_adv = read_uleb128(prog.rd)?;
251 state.addr += (prog.head.min_instr_length * op_adv): uintptr;
252 case DW_LNS_advance_line =>
253 const line = state.line: i64;
254 const offs = read_sleb128(prog.rd)?;
255 line += offs;
256 state.line = line: uint;
257 case DW_LNS_set_file =>
258 state.file = read_uleb128(prog.rd)?: uint;
259 case DW_LNS_set_column =>
260 state.column = read_uleb128(prog.rd)?: uint;
261 case DW_LNS_negate_stmt =>
262 state.flags ^= line_flag::IS_STMT;
263 case DW_LNS_set_basic_block =>
264 state.flags |= line_flag::BASIC_BLOCK;
265 case DW_LNS_const_add_pc =>
266 const opcode = 255 - prog.head.opcode_base;
267 const op_adv = opcode / prog.head.line_range;
268 state.addr += (prog.head.min_instr_length * op_adv): uintptr;
269 case DW_LNS_fixed_advance_pc =>
270 state.addr += read_uhalf(prog.rd)?: uintptr;
271 state.op_index = 0;
272 case DW_LNS_set_prologue_end =>
273 state.flags |= line_flag::PROLOGUE_END;
274 case DW_LNS_set_epilogue_begin =>
275 state.flags |= line_flag::EPILOGUE_BEGIN;
276 case DW_LNS_isa =>
277 state.isa = read_uleb128(prog.rd)?: uint;
278 case =>
279 // Unknown opcode, skip
280 const length = prog.head.opcode_lengths[opcode - 1];
281 for (length != 0; length -= 1) {
282 read_uleb128(prog.rd)?;
283 };
284 };
285 } else {
286 const opcode = opcode - prog.head.opcode_base;
287 const op_adv = opcode / prog.head.line_range;
288 state.addr += (prog.head.min_instr_length * op_adv): uintptr;
289 let line = state.line: int;
290 line += prog.head.line_base: int +
291 opcode: int % prog.head.line_range: int;
292 state.line = line: uint;
293 };
294 };