Debian Codegraph @

tests/dwarfwriter.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import os.path
5 from typing import Any, Dict, NamedTuple, Optional, Sequence, Union
6 import zlib
7
8 from _drgn_util.elf import ET, SHF, SHT
9 from tests.assembler import _append_sleb128, _append_uleb128
10 from tests.dwarf import DW_AT, DW_FORM, DW_LNCT, DW_LNE, DW_LNS, DW_TAG, DW_UT
11 from tests.elfwriter import ElfSection, create_elf_file
12
13
14 class DwarfAttrib(NamedTuple):
15 name: DW_AT
16 form: DW_FORM
17 value: Any
18
19
20 class DwarfLabel(NamedTuple):
21 name: str
22
23
24 class DwarfDie(NamedTuple):
25 tag: DW_TAG
26 attribs: Sequence[DwarfAttrib] = ()
27 children: Sequence[Union["DwarfDie", DwarfLabel]] = ()
28
29
30 class DwarfLnp(NamedTuple):
31 instructions: Sequence[Sequence[Any]]
32 comp_dir: str
33 file_names: Sequence[str]
34 default_is_stmt: bool = True
35
36
37 class DwarfUnit(NamedTuple):
38 type: DW_UT
39 die: DwarfDie
40 die_label: Optional[str] = None
41 dwo_id: Optional[int] = None
42 type_signature: Optional[int] = None
43 type_offset: Optional[str] = None
44 lnp: Optional[DwarfLnp] = None
45
46
47 def create_dwarf_lnp(rows, comp_dir: str, file_name: str) -> DwarfLnp:
48 instructions = []
49 file_names = {file_name: 1}
50 current_pc = 0
51 current_file = 1
52 current_line = 1
53 current_column = 0
54 for pc, file_name, line, column in rows:
55 if pc > current_pc:
56 instructions.append((DW_LNS.advance_pc, pc - current_pc))
57 else:
58 assert pc == 0 and current_pc == 0
59
60 if file_name is None:
61 assert line is None
62 assert column is None
63 instructions.append((DW_LNE.end_sequence,))
64 else:
65 file = file_names.setdefault(file_name, len(file_names) + 1)
66 if file != current_file:
67 instructions.append((DW_LNS.set_file, file))
68 if line != current_line:
69 instructions.append((DW_LNS.advance_line, line - current_line))
70 if column != current_column:
71 instructions.append((DW_LNS.set_column, column))
72 instructions.append((DW_LNS.copy,))
73 current_pc = pc
74 current_file = file
75 current_line = line
76 current_column = column
77 assert instructions[-1] == (DW_LNE.end_sequence,)
78
79 return DwarfLnp(
80 instructions=instructions, comp_dir=comp_dir, file_names=list(file_names)
81 )
82
83
84 def _compile_debug_abbrev(units, use_dw_form_indirect):
85 buf = bytearray()
86 code = 1
87
88 def aux(die):
89 if isinstance(die, DwarfLabel):
90 return
91 nonlocal code
92 _append_uleb128(buf, code)
93 code += 1
94 _append_uleb128(buf, die.tag)
95 buf.append(bool(die.children))
96 for attrib in die.attribs:
97 _append_uleb128(buf, attrib.name)
98 _append_uleb128(
99 buf, DW_FORM.indirect if use_dw_form_indirect else attrib.form
100 )
101 buf.append(0)
102 buf.append(0)
103 if die.children:
104 for child in die.children:
105 aux(child)
106
107 for unit in units:
108 aux(unit.die)
109 buf.append(0)
110 return buf
111
112
113 def _compile_debug_info(
114 units, little_endian, bits, version, file_names, use_dw_form_indirect
115 ):
116 offset_size = 4 # We only emit the 32-bit format for now.
117 byteorder = "little" if little_endian else "big"
118 labels = {}
119 references = []
120 unit_references = []
121 code = 1
122
123 def aux(buf, die, depth):
124 if isinstance(die, DwarfLabel):
125 if die.name in labels:
126 raise ValueError(f"duplicate label {die.name!r}")
127 labels[die.name] = len(buf)
128 return
129
130 nonlocal code
131 _append_uleb128(buf, code)
132 code += 1
133 for attrib in die.attribs:
134 if use_dw_form_indirect:
135 _append_uleb128(buf, attrib.form)
136 if attrib.name == DW_AT.decl_file or attrib.name == DW_AT.call_file:
137 value = file_names[attrib.value]
138 else:
139 value = attrib.value
140 if attrib.form == DW_FORM.addr:
141 buf.extend(value.to_bytes(bits // 8, byteorder))
142 elif attrib.form == DW_FORM.data1:
143 buf.append(value)
144 elif attrib.form == DW_FORM.data2:
145 buf.extend(value.to_bytes(2, byteorder))
146 elif attrib.form in (DW_FORM.data4, DW_FORM.ref_sup4):
147 buf.extend(value.to_bytes(4, byteorder))
148 elif attrib.form in (DW_FORM.data8, DW_FORM.ref_sup8):
149 buf.extend(value.to_bytes(8, byteorder))
150 elif attrib.form == DW_FORM.udata:
151 _append_uleb128(buf, value)
152 elif attrib.form == DW_FORM.sdata:
153 _append_sleb128(buf, value)
154 elif attrib.form == DW_FORM.block:
155 _append_uleb128(buf, len(value))
156 buf.extend(value)
157 elif attrib.form == DW_FORM.block1:
158 buf.append(len(value))
159 buf.extend(value)
160 elif attrib.form in (DW_FORM.strp, DW_FORM.GNU_ref_alt):
161 buf.extend(value.to_bytes(offset_size, byteorder))
162 elif attrib.form == DW_FORM.string:
163 buf.extend(value.encode())
164 buf.append(0)
165 elif attrib.form == DW_FORM.ref1:
166 if isinstance(value, str):
167 unit_references.append((len(buf), 1, value))
168 buf.append(0)
169 else:
170 buf.extend(value.to_bytes(1, byteorder))
171 elif attrib.form == DW_FORM.ref2:
172 if isinstance(value, str):
173 unit_references.append((len(buf), 2, value))
174 buf.extend(bytes(2))
175 else:
176 buf.extend(value.to_bytes(2, byteorder))
177 elif attrib.form == DW_FORM.ref4:
178 if isinstance(value, str):
179 unit_references.append((len(buf), 4, value))
180 buf.extend(bytes(4))
181 else:
182 buf.extend(value.to_bytes(4, byteorder))
183 elif attrib.form == DW_FORM.ref8:
184 if isinstance(value, str):
185 unit_references.append((len(buf), 8, value))
186 buf.extend(bytes(8))
187 else:
188 buf.extend(value.to_bytes(8, byteorder))
189 elif attrib.form == DW_FORM.ref_udata:
190 if isinstance(value, str):
191 assert (
192 value in labels
193 ), "DW_FORM_ref_udata can only be used for backreferences"
194 _append_uleb128(buf, labels[value] - unit_offset)
195 else:
196 _append_uleb128(buf, value)
197 elif attrib.form == DW_FORM.ref_addr:
198 if isinstance(value, str):
199 references.append((len(buf), offset_size, value))
200 buf.extend(bytes(offset_size))
201 else:
202 buf.extend(value.to_bytes(offset_size, byteorder))
203 elif attrib.form == DW_FORM.ref_sig8:
204 buf.extend(value.to_bytes(8, byteorder))
205 elif attrib.form == DW_FORM.sec_offset:
206 buf.extend(bytes(offset_size))
207 elif attrib.form == DW_FORM.flag_present:
208 pass
209 elif attrib.form == DW_FORM.exprloc:
210 _append_uleb128(buf, len(value))
211 buf.extend(value)
212 else:
213 assert False, attrib.form
214 if die.children:
215 for child in die.children:
216 aux(buf, child, depth + 1)
217 buf.append(0)
218
219 debug_info = bytearray()
220 debug_types = bytearray()
221 for unit in units:
222 unit_references.clear()
223 if version == 4 and unit.type in (DW_UT.type, DW_UT.split_type):
224 buf = debug_types
225 else:
226 buf = debug_info
227 unit_offset = len(buf)
228 buf.extend(b"\0\0\0\0") # unit_length
229 buf.extend(version.to_bytes(2, byteorder)) # version
230 if version >= 5:
231 buf.append(unit.type) # unit_type
232 buf.append(bits // 8) # address_size
233 buf.extend((0).to_bytes(4, byteorder)) # debug_abbrev_offset
234 if version < 5:
235 buf.append(bits // 8) # address_size
236
237 if version >= 5 and unit.type in (DW_UT.skeleton, DW_UT.split_compile):
238 buf.extend(unit.dwo_id.to_bytes(8, byteorder)) # dwo_id
239 else:
240 assert unit.dwo_id is None
241 if unit.type in (DW_UT.type, DW_UT.split_type):
242 buf.extend(unit.type_signature.to_bytes(8, byteorder)) # type_signature
243 unit_references.append((len(buf), offset_size, unit.type_offset))
244 buf.extend(bytes(offset_size)) # type_offset
245 else:
246 assert unit.type_signature is None
247 assert unit.type_offset is None
248
249 if unit.die_label is not None:
250 aux(buf, DwarfLabel(unit.die_label), 0)
251 aux(buf, unit.die, 0)
252
253 unit_length = len(buf) - unit_offset - 4
254 buf[unit_offset : unit_offset + 4] = unit_length.to_bytes(4, byteorder)
255
256 for offset, size, label in unit_references:
257 die_offset = labels[label] - unit_offset
258 buf[offset : offset + size] = die_offset.to_bytes(size, byteorder)
259
260 for offset, size, label in references:
261 buf[offset : offset + size] = labels[label].to_bytes(size, byteorder)
262
263 return debug_info, debug_types, labels
264
265
266 def _compile_debug_line(units, little_endian, bits, version):
267 byteorder = "little" if little_endian else "big"
268
269 if not units:
270 units = [DwarfUnit(DW_UT.compile, DwarfDie(DW_TAG.compile_unit, []))]
271
272 buf = bytearray()
273 for unit in units:
274 lnp = unit.lnp
275 if lnp is None:
276 lnp = DwarfLnp(
277 instructions=(),
278 comp_dir="/usr/src",
279 file_names=("main.c",),
280 )
281
282 unit.die.attribs.append(
283 DwarfAttrib(DW_AT.stmt_list, DW_FORM.sec_offset, len(buf))
284 )
285 if unit.type in (DW_UT.compile, DW_UT.partial, DW_UT.skeleton):
286 unit.die.attribs.append(
287 DwarfAttrib(DW_AT.name, DW_FORM.string, lnp.file_names[0])
288 )
289 unit.die.attribs.append(
290 DwarfAttrib(DW_AT.comp_dir, DW_FORM.string, lnp.comp_dir)
291 )
292
293 unit_length_start = len(buf)
294 buf.extend(b"\0\0\0\0") # unit_length
295 unit_length_end = len(buf)
296 buf.extend(version.to_bytes(2, byteorder)) # version
297 if version >= 5:
298 buf.append(bits // 8) # address_size
299 buf.append(0) # segment_selector_size
300 header_length_start = len(buf)
301 buf.extend(b"\0\0\0\0") # header_length
302 header_length_end = len(buf)
303 buf.append(1) # minimum_instruction_length
304 buf.append(1) # maximum_operations_per_instruction
305 buf.append(lnp.default_is_stmt) # default_is_stmt
306 buf.append(1) # line_base
307 buf.append(1) # line_range
308 buf.append(13) # opcode_base
309 buf.extend([0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1]) # standard_opcode_lengths
310 if version >= 5:
311 buf.append(1) # directory_entry_format_count
312 # directory_entry_format
313 _append_uleb128(buf, DW_LNCT.path)
314 _append_uleb128(buf, DW_FORM.string)
315
316 directories = {lnp.comp_dir: 0}
317 file_names = {}
318 file_name_entries = []
319
320 for file_name in lnp.file_names:
321 assert file_name not in file_names
322
323 file_names[file_name] = len(file_names) + 1
324
325 dirname, basename = os.path.split(file_name)
326 if dirname:
327 directory_index = directories.setdefault(dirname, len(directories))
328 else:
329 directory_index = 0
330 file_name_entries.append((basename, directory_index))
331
332 def collect_file_names(die):
333 if isinstance(die, DwarfLabel):
334 return
335 for attrib in die.attribs:
336 if attrib.name not in (DW_AT.decl_file, DW_AT.call_file):
337 continue
338
339 if attrib.value in file_names:
340 continue
341
342 file_names[attrib.value] = len(file_names) + 1
343
344 dirname, basename = os.path.split(attrib.value)
345 if dirname:
346 directory_index = directories.setdefault(dirname, len(directories))
347 else:
348 directory_index = 0
349 file_name_entries.append((basename, directory_index))
350 for child in die.children:
351 collect_file_names(child)
352
353 collect_file_names(unit.die)
354
355 if version >= 5:
356 _append_uleb128(buf, len(directories)) # directories_count
357
358 # directories (or include_directories in version <= 4)
359 for directory, index in directories.items():
360 if index > 0 or version >= 5:
361 buf.extend(directory.encode("ascii"))
362 buf.append(0)
363 if version < 5:
364 buf.append(0)
365
366 if version >= 5:
367 buf.append(2) # file_name_entry_format_count
368 # file_name_entry_format
369 _append_uleb128(buf, DW_LNCT.path)
370 _append_uleb128(buf, DW_FORM.string)
371 _append_uleb128(buf, DW_LNCT.directory_index)
372 _append_uleb128(buf, DW_FORM.udata)
373
374 if version >= 5:
375 file_name_entries.insert(0, file_name_entries[0])
376 _append_uleb128(buf, len(file_names)) # file_names_count
377
378 # file_names
379 for path, directory_index in file_name_entries:
380 # path
381 buf.extend(path.encode("ascii"))
382 buf.append(0)
383 _append_uleb128(buf, directory_index) # directory_index
384 if version < 5:
385 _append_uleb128(buf, 0) # mtime
386 _append_uleb128(buf, 0) # size
387
388 if version < 5:
389 buf.append(0)
390
391 buf[header_length_start:header_length_end] = (
392 len(buf) - header_length_end
393 ).to_bytes(header_length_end - header_length_start, byteorder)
394
395 for instruction in lnp.instructions:
396 opcode = instruction[0]
397 buf.append(opcode)
398 if opcode in {DW_LNS.copy, DW_LNE.end_sequence}:
399 assert len(instruction) == 1
400 elif opcode == DW_LNS.advance_pc:
401 assert len(instruction) == 2
402 _append_uleb128(buf, instruction[1])
403 elif opcode == DW_LNS.advance_line:
404 assert len(instruction) == 2
405 _append_sleb128(buf, instruction[1])
406 elif opcode == DW_LNS.set_file:
407 assert len(instruction) == 2
408 _append_uleb128(buf, instruction[1])
409 elif opcode == DW_LNS.set_column:
410 assert len(instruction) == 2
411 _append_uleb128(buf, instruction[1])
412 else:
413 assert False, opcode
414
415 buf[unit_length_start:unit_length_end] = (len(buf) - unit_length_end).to_bytes(
416 unit_length_end - unit_length_start, byteorder
417 )
418 return buf, file_names
419
420
421 _UNIT_TAGS = frozenset({DW_TAG.type_unit, DW_TAG.compile_unit, DW_TAG.partial_unit})
422
423
424 class DwarfResult(NamedTuple):
425 data: bytes
426 labels: Dict[str, int]
427
428
429 def compile_dwarf(
430 units_or_dies,
431 *,
432 version=4,
433 lang=None,
434 use_dw_form_indirect=False,
435 compress=None,
436 split=None,
437 sections=(),
438 little_endian=True,
439 bits=64,
440 allow_any_unit_die=False,
441 **kwargs,
442 ):
443 assert compress in (None, "zlib-gnu", "zlib-gabi")
444 assert split in (None, "dwo")
445
446 if isinstance(units_or_dies, (DwarfDie, DwarfUnit)):
447 units_or_dies = (units_or_dies,)
448 if not units_or_dies or isinstance(units_or_dies[0], DwarfUnit):
449 units = units_or_dies
450 else:
451 assert all(isinstance(die, (DwarfDie, DwarfLabel)) for die in units_or_dies)
452 assert all(
453 not isinstance(die, DwarfDie) or die.tag not in _UNIT_TAGS
454 for die in units_or_dies
455 )
456 units = (
457 DwarfUnit(DW_UT.compile, DwarfDie(DW_TAG.compile_unit, (), units_or_dies)),
458 )
459 assert all(isinstance(unit, DwarfUnit) for unit in units)
460 assert allow_any_unit_die or all(unit.die.tag in _UNIT_TAGS for unit in units)
461
462 unit_attribs = []
463 if lang is not None:
464 unit_attribs.append(DwarfAttrib(DW_AT.language, DW_FORM.data1, lang))
465
466 units = [
467 unit._replace(
468 die=unit.die._replace(attribs=list(unit.die.attribs) + unit_attribs)
469 )
470 for unit in units
471 ]
472
473 # TODO: line number information for a split file is in the skeleton file.
474 # We don't have any test cases yet that use line number information from a
475 # split file, but when we do, we'll have to add a way to include the split
476 # file's line number information in the skeleton file.
477 if split:
478 file_names = {}
479 else:
480 debug_line, file_names = _compile_debug_line(
481 units, little_endian, bits, version
482 )
483
484 debug_info, debug_types, labels = _compile_debug_info(
485 units, little_endian, bits, version, file_names, use_dw_form_indirect
486 )
487
488 def debug_section(name, data):
489 assert name.startswith(".debug")
490 if compress == "zlib-gnu":
491 name = ".z" + name[1:]
492 compressed_data = bytearray(b"ZLIB")
493 compressed_data.extend(len(data).to_bytes(8, "big"))
494 compressed_data.extend(zlib.compress(data))
495 data = compressed_data
496 if split:
497 name += ".dwo"
498 return ElfSection(
499 name=name,
500 sh_type=SHT.PROGBITS,
501 data=data,
502 sh_flags=SHF.COMPRESSED if compress == "zlib-gabi" else SHF(0),
503 )
504 return name
505
506 dwarf_sections = [
507 debug_section(
508 ".debug_abbrev", _compile_debug_abbrev(units, use_dw_form_indirect)
509 ),
510 debug_section(".debug_info", debug_info),
511 debug_section(".debug_str", b"\0"),
512 ]
513 if not split:
514 dwarf_sections.append(debug_section(".debug_line", debug_line))
515 if debug_types:
516 dwarf_sections.append(debug_section(".debug_types", debug_types))
517
518 return DwarfResult(
519 data=create_elf_file(
520 ET.EXEC,
521 sections=[*sections, *dwarf_sections],
522 little_endian=little_endian,
523 bits=bits,
524 **kwargs,
525 ),
526 labels=labels,
527 )
528
529
530 def create_dwarf_file(*args, **kwargs):
531 return compile_dwarf(*args, **kwargs).data