Debian Codegraph @

tests/test_dwarf.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import functools
5 import logging
6 import operator
7 import os.path
8 import re
9 import tempfile
10
11 from _drgn_util.elf import PT, SHF, SHT, STB, STT
12 import drgn
13 from drgn import (
14 AbsenceReason,
15 FaultError,
16 FindObjectFlags,
17 Language,
18 Object,
19 ObjectNotFoundError,
20 Program,
21 ProgramFlags,
22 Qualifiers,
23 TypeEnumerator,
24 TypeMember,
25 TypeParameter,
26 TypeTemplateParameter,
27 alignof,
28 )
29 from tests import (
30 DEFAULT_LANGUAGE,
31 MockMemorySegment,
32 TestCase,
33 add_mock_memory_segments,
34 identical,
35 )
36 import tests.assembler as assembler
37 from tests.dwarf import (
38 DW_AT,
39 DW_ATE,
40 DW_END,
41 DW_FORM,
42 DW_INL,
43 DW_LANG,
44 DW_OP,
45 DW_TAG,
46 DW_UT,
47 )
48 from tests.dwarfwriter import (
49 DwarfAttrib,
50 DwarfDie,
51 DwarfLabel,
52 DwarfUnit,
53 compile_dwarf,
54 create_dwarf_file,
55 create_dwarf_lnp,
56 )
57 from tests.elfwriter import ElfSection, ElfSymbol
58
59 bool_die = DwarfDie(
60 DW_TAG.base_type,
61 (
62 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 1),
63 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.boolean),
64 DwarfAttrib(DW_AT.name, DW_FORM.string, "_Bool"),
65 ),
66 )
67 char_die = DwarfDie(
68 DW_TAG.base_type,
69 (
70 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 1),
71 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.signed_char),
72 DwarfAttrib(DW_AT.name, DW_FORM.string, "char"),
73 ),
74 )
75 signed_char_die = DwarfDie(
76 DW_TAG.base_type,
77 (
78 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 1),
79 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.signed_char),
80 DwarfAttrib(DW_AT.name, DW_FORM.string, "signed char"),
81 ),
82 )
83 unsigned_char_die = DwarfDie(
84 DW_TAG.base_type,
85 (
86 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 1),
87 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.unsigned_char),
88 DwarfAttrib(DW_AT.name, DW_FORM.string, "unsigned char"),
89 ),
90 )
91 utf8_char_die = DwarfDie(
92 DW_TAG.base_type,
93 (
94 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 1),
95 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.UTF),
96 DwarfAttrib(DW_AT.name, DW_FORM.string, "char8_t"),
97 ),
98 )
99 utf16_char_die = DwarfDie(
100 DW_TAG.base_type,
101 (
102 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 2),
103 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.UTF),
104 DwarfAttrib(DW_AT.name, DW_FORM.string, "char16_t"),
105 ),
106 )
107 utf32_char_die = DwarfDie(
108 DW_TAG.base_type,
109 (
110 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
111 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.UTF),
112 DwarfAttrib(DW_AT.name, DW_FORM.string, "char32_t"),
113 ),
114 )
115 short_die = DwarfDie(
116 DW_TAG.base_type,
117 (
118 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 2),
119 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.signed),
120 DwarfAttrib(DW_AT.name, DW_FORM.string, "short"),
121 ),
122 )
123 unsigned_short_die = DwarfDie(
124 DW_TAG.base_type,
125 (
126 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 2),
127 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.unsigned),
128 DwarfAttrib(DW_AT.name, DW_FORM.string, "unsigned short"),
129 ),
130 )
131 int_die = DwarfDie(
132 DW_TAG.base_type,
133 (
134 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
135 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.signed),
136 DwarfAttrib(DW_AT.name, DW_FORM.string, "int"),
137 ),
138 )
139 unsigned_int_die = DwarfDie(
140 DW_TAG.base_type,
141 (
142 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
143 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.unsigned),
144 DwarfAttrib(DW_AT.name, DW_FORM.string, "unsigned int"),
145 ),
146 )
147 long_die = DwarfDie(
148 DW_TAG.base_type,
149 (
150 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
151 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.signed),
152 DwarfAttrib(DW_AT.name, DW_FORM.string, "long"),
153 ),
154 )
155 unsigned_long_die = DwarfDie(
156 DW_TAG.base_type,
157 (
158 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
159 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.unsigned),
160 DwarfAttrib(DW_AT.name, DW_FORM.string, "unsigned long"),
161 ),
162 )
163 long_long_die = DwarfDie(
164 DW_TAG.base_type,
165 (
166 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
167 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.signed),
168 DwarfAttrib(DW_AT.name, DW_FORM.string, "long long"),
169 ),
170 )
171 float_die = DwarfDie(
172 DW_TAG.base_type,
173 (
174 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
175 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.float),
176 DwarfAttrib(DW_AT.name, DW_FORM.string, "float"),
177 ),
178 )
179 double_die = DwarfDie(
180 DW_TAG.base_type,
181 (
182 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
183 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.float),
184 DwarfAttrib(DW_AT.name, DW_FORM.string, "double"),
185 ),
186 )
187 long_double_die = DwarfDie(
188 DW_TAG.base_type,
189 (
190 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 16),
191 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.float),
192 DwarfAttrib(DW_AT.name, DW_FORM.string, "long double"),
193 ),
194 )
195 unsigned_long_long_die = DwarfDie(
196 DW_TAG.base_type,
197 (
198 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
199 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.unsigned),
200 DwarfAttrib(DW_AT.name, DW_FORM.string, "unsigned long long"),
201 ),
202 )
203
204
205 labeled_char_die = (DwarfLabel("char_die"), char_die)
206 labeled_int_die = (DwarfLabel("int_die"), int_die)
207 labeled_unsigned_int_die = (DwarfLabel("unsigned_int_die"), unsigned_int_die)
208 labeled_float_die = (DwarfLabel("float_die"), float_die)
209
210
211 def add_extra_dwarf(prog, path, address_range=None, supplementary_path=None):
212 module = prog.extra_module(path, create=True)
213 if address_range is not None:
214 module.address_range = address_range
215 module.try_file(path, force=True)
216 if module.debug_file_status == drgn.ModuleFileStatus.WANT_SUPPLEMENTARY:
217 module.try_file(supplementary_path)
218 else:
219 assert supplementary_path is None
220 assert not module.wants_debug_file()
221
222
223 def dwarf_program(
224 *args, segments=None, address_range=None, gnu_debugaltlink=None, **kwds
225 ):
226 prog = Program()
227 with tempfile.NamedTemporaryFile() as f:
228 f.write(create_dwarf_file(*args, gnu_debugaltlink=gnu_debugaltlink, **kwds))
229 f.flush()
230 add_extra_dwarf(
231 prog,
232 f.name,
233 address_range=address_range,
234 supplementary_path=(
235 None if gnu_debugaltlink is None else gnu_debugaltlink[0]
236 ),
237 )
238
239 if segments is not None:
240 add_mock_memory_segments(prog, segments)
241 return prog
242
243
244 def wrap_test_type_dies(*dies):
245 return (
246 DwarfDie(
247 DW_TAG.typedef,
248 (
249 DwarfAttrib(DW_AT.name, DW_FORM.string, "TEST"),
250 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "test_type_die"),
251 ),
252 ),
253 DwarfLabel("test_type_die"),
254 ) + dies
255
256
257 elfutils_version = tuple(int(x) for x in drgn._elfutils_version.split(".")[:2])
258
259
260 def with_and_without_dw_form_indirect(f):
261 @functools.wraps(f)
262 def wrapper(self):
263 with self.subTest():
264 f(self, False)
265 # elfutils does not support DW_FORM_indirect properly before commit
266 # d63b26b8d21f ("libdw: handle DW_FORM_indirect when reading
267 # attributes").
268 if elfutils_version >= (0, 184):
269 with self.subTest(msg="with DW_FORM_indirect"):
270 f(self, True)
271
272 return wrapper
273
274
275 class TestInvalidDwarf(TestCase):
276 def test_name_out_of_bounds(self):
277 with self.assertRaisesRegex(Exception, "name is out of bounds"):
278 "foo" in dwarf_program(
279 DwarfDie(
280 DW_TAG.base_type,
281 (
282 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.signed),
283 DwarfAttrib(DW_AT.name, DW_FORM.strp, 0xDEADBEEF),
284 ),
285 )
286 )
287
288 def test_sibling_out_of_bounds(self):
289 with self.assertRaisesRegex(Exception, "DW_AT_sibling is out of bounds"):
290 "foo" in dwarf_program(
291 DwarfDie(
292 DW_TAG.base_type,
293 (
294 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.signed),
295 DwarfAttrib(DW_AT.sibling, DW_FORM.ref4, 0xDEADBEEF),
296 ),
297 )
298 )
299
300 def test_sibling_points_backwards(self):
301 with self.assertRaisesRegex(Exception, "DW_AT_sibling points backwards"):
302 "foo" in dwarf_program(
303 DwarfDie(
304 DW_TAG.base_type,
305 (
306 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.signed),
307 DwarfAttrib(DW_AT.sibling, DW_FORM.ref1, 0),
308 ),
309 )
310 )
311
312
313 class TestTypes(TestCase):
314 def test_unknown_tag(self):
315 prog = dwarf_program(wrap_test_type_dies(DwarfDie(0x9999, ())))
316 self.assertRaisesRegex(
317 Exception, "unknown DWARF type tag 0x9999", prog.type, "TEST"
318 )
319
320 def test_base_type_missing_byte_size(self):
321 prog = dwarf_program(
322 wrap_test_type_dies(
323 DwarfDie(
324 DW_TAG.base_type,
325 (
326 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.signed),
327 DwarfAttrib(DW_AT.name, DW_FORM.string, "bad egg"),
328 ),
329 )
330 )
331 )
332 self.assertRaisesRegex(
333 Exception,
334 "DW_TAG_base_type has missing or invalid DW_AT_byte_size",
335 prog.type,
336 "TEST",
337 )
338
339 def test_base_type_missing_encoding(self):
340 prog = dwarf_program(
341 wrap_test_type_dies(
342 DwarfDie(
343 DW_TAG.base_type,
344 (
345 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
346 DwarfAttrib(DW_AT.name, DW_FORM.string, "bad egg"),
347 ),
348 )
349 )
350 )
351 self.assertRaisesRegex(
352 Exception,
353 "DW_TAG_base_type has missing or invalid DW_AT_encoding",
354 prog.type,
355 "TEST",
356 )
357
358 def test_base_type_missing_name(self):
359 prog = dwarf_program(
360 wrap_test_type_dies(
361 DwarfDie(
362 DW_TAG.base_type,
363 (
364 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
365 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.signed),
366 ),
367 )
368 )
369 )
370 self.assertRaisesRegex(
371 Exception,
372 "DW_TAG_base_type has missing or invalid DW_AT_name",
373 prog.type,
374 "TEST",
375 )
376
377 def test_unknown_base_type_encoding(self):
378 prog = dwarf_program(
379 wrap_test_type_dies(
380 DwarfDie(
381 DW_TAG.base_type,
382 (
383 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
384 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, 99),
385 DwarfAttrib(DW_AT.name, DW_FORM.string, "magic int"),
386 ),
387 )
388 )
389 )
390 self.assertRaisesRegex(Exception, "unknown DWARF encoding", prog.type, "TEST")
391
392 def test_reference_forms(self):
393 for form in (
394 DW_FORM.ref1,
395 DW_FORM.ref2,
396 DW_FORM.ref4,
397 DW_FORM.ref8,
398 DW_FORM.ref_udata,
399 DW_FORM.ref_addr,
400 ):
401 with self.subTest(form=form):
402 prog = dwarf_program(
403 (
404 *labeled_int_die,
405 DwarfDie(
406 DW_TAG.typedef,
407 (
408 DwarfAttrib(DW_AT.name, DW_FORM.string, "TEST"),
409 DwarfAttrib(DW_AT.type, form, "int_die"),
410 ),
411 ),
412 )
413 )
414 self.assertIdentical(
415 prog.type("TEST").type, prog.int_type("int", 4, True)
416 )
417
418 def test_int_type_byteorder(self):
419 prog = dwarf_program(
420 wrap_test_type_dies(
421 DwarfDie(
422 DW_TAG.base_type,
423 (
424 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
425 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.signed),
426 DwarfAttrib(DW_AT.name, DW_FORM.string, "int"),
427 DwarfAttrib(DW_AT.endianity, DW_FORM.data1, DW_END.big),
428 ),
429 )
430 )
431 )
432 self.assertIdentical(
433 prog.type("TEST").type, prog.int_type("int", 4, True, "big")
434 )
435
436 def test_bool_type_byteorder(self):
437 prog = dwarf_program(
438 wrap_test_type_dies(
439 DwarfDie(
440 DW_TAG.base_type,
441 (
442 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 1),
443 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.boolean),
444 DwarfAttrib(DW_AT.name, DW_FORM.string, "_Bool"),
445 DwarfAttrib(DW_AT.endianity, DW_FORM.data1, DW_END.big),
446 ),
447 )
448 )
449 )
450 self.assertIdentical(prog.type("TEST").type, prog.bool_type("_Bool", 1, "big"))
451
452 def test_float_type_byteorder(self):
453 prog = dwarf_program(
454 wrap_test_type_dies(
455 DwarfDie(
456 DW_TAG.base_type,
457 (
458 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
459 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.float),
460 DwarfAttrib(DW_AT.name, DW_FORM.string, "float"),
461 DwarfAttrib(DW_AT.endianity, DW_FORM.data1, DW_END.big),
462 ),
463 )
464 )
465 )
466 self.assertIdentical(prog.type("TEST").type, prog.float_type("float", 4, "big"))
467
468 def test_byteorder_by_name(self):
469 # The only producer that uses DW_AT_endianity that I could find is GCC
470 # for the scalar_storage_order type attribute (see
471 # https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html). It
472 # always places the standard DIE before the DIE with DW_AT_endianity,
473 # which luckily guarantees that we'll use the standard one when doing a
474 # name lookup.
475 prog = dwarf_program(
476 wrap_test_type_dies(
477 DwarfDie(
478 DW_TAG.base_type,
479 (
480 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
481 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.signed),
482 DwarfAttrib(DW_AT.name, DW_FORM.string, "int"),
483 ),
484 ),
485 DwarfDie(
486 DW_TAG.base_type,
487 (
488 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
489 DwarfAttrib(DW_AT.encoding, DW_FORM.data1, DW_ATE.signed),
490 DwarfAttrib(DW_AT.name, DW_FORM.string, "int"),
491 DwarfAttrib(DW_AT.endianity, DW_FORM.data1, DW_END.big),
492 ),
493 ),
494 )
495 )
496 self.assertIdentical(prog.type("int"), prog.int_type("int", 4, True, "little"))
497
498 @with_and_without_dw_form_indirect
499 def test_qualifier(self, use_dw_form_indirect):
500 prog = dwarf_program(
501 wrap_test_type_dies(
502 DwarfDie(
503 DW_TAG.const_type,
504 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
505 ),
506 *labeled_int_die,
507 ),
508 use_dw_form_indirect=use_dw_form_indirect,
509 )
510 self.assertIdentical(
511 prog.type("TEST").type,
512 prog.int_type("int", 4, True, qualifiers=Qualifiers.CONST),
513 )
514
515 def test_multiple_qualifiers(self):
516 prog = dwarf_program(
517 wrap_test_type_dies(
518 DwarfDie(
519 DW_TAG.const_type,
520 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "restrict_die"),),
521 ),
522 DwarfLabel("restrict_die"),
523 DwarfDie(
524 DW_TAG.restrict_type,
525 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "volatile_die"),),
526 ),
527 DwarfLabel("volatile_die"),
528 DwarfDie(
529 DW_TAG.volatile_type,
530 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "atomic_die"),),
531 ),
532 DwarfLabel("atomic_die"),
533 DwarfDie(
534 DW_TAG.atomic_type,
535 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
536 ),
537 *labeled_int_die,
538 )
539 )
540 self.assertIdentical(
541 prog.type("TEST").type,
542 prog.int_type(
543 "int",
544 4,
545 True,
546 qualifiers=Qualifiers.CONST
547 | Qualifiers.RESTRICT
548 | Qualifiers.VOLATILE
549 | Qualifiers.ATOMIC,
550 ),
551 )
552
553 def test_qualifier_void(self):
554 prog = dwarf_program(wrap_test_type_dies(DwarfDie(DW_TAG.const_type, ())))
555 self.assertIdentical(
556 prog.type("TEST").type, prog.void_type(qualifiers=Qualifiers.CONST)
557 )
558
559 def test_multiple_qualifiers_void(self):
560 prog = dwarf_program(
561 wrap_test_type_dies(
562 DwarfDie(
563 DW_TAG.const_type,
564 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "restrict_die"),),
565 ),
566 DwarfLabel("restrict_die"),
567 DwarfDie(
568 DW_TAG.restrict_type,
569 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "volatile_die"),),
570 ),
571 DwarfLabel("volatile_die"),
572 DwarfDie(
573 DW_TAG.volatile_type,
574 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "atomic_die"),),
575 ),
576 DwarfLabel("atomic_die"),
577 DwarfDie(DW_TAG.atomic_type, ()),
578 )
579 )
580 self.assertIdentical(
581 prog.type("TEST").type,
582 prog.void_type(
583 qualifiers=Qualifiers.CONST
584 | Qualifiers.RESTRICT
585 | Qualifiers.VOLATILE
586 | Qualifiers.ATOMIC,
587 ),
588 )
589
590 def test_struct(self):
591 prog = dwarf_program(
592 wrap_test_type_dies(
593 DwarfDie(
594 DW_TAG.structure_type,
595 (
596 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
597 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
598 ),
599 (
600 DwarfDie(
601 DW_TAG.member,
602 (
603 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
604 DwarfAttrib(
605 DW_AT.data_member_location, DW_FORM.data1, 0
606 ),
607 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
608 ),
609 ),
610 DwarfDie(
611 DW_TAG.member,
612 (
613 DwarfAttrib(DW_AT.name, DW_FORM.string, "y"),
614 DwarfAttrib(
615 DW_AT.data_member_location, DW_FORM.data1, 4
616 ),
617 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
618 ),
619 ),
620 ),
621 ),
622 *labeled_int_die,
623 )
624 )
625 self.assertIdentical(
626 prog.type("TEST").type,
627 prog.struct_type(
628 "point",
629 8,
630 (
631 TypeMember(prog.int_type("int", 4, True), "x", 0),
632 TypeMember(prog.int_type("int", 4, True), "y", 32),
633 ),
634 ),
635 )
636
637 def test_struct_anonymous(self):
638 prog = dwarf_program(
639 wrap_test_type_dies(
640 DwarfDie(
641 DW_TAG.structure_type,
642 (DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),),
643 (
644 DwarfDie(
645 DW_TAG.member,
646 (
647 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
648 DwarfAttrib(
649 DW_AT.data_member_location, DW_FORM.data1, 0
650 ),
651 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
652 ),
653 ),
654 DwarfDie(
655 DW_TAG.member,
656 (
657 DwarfAttrib(DW_AT.name, DW_FORM.string, "y"),
658 DwarfAttrib(
659 DW_AT.data_member_location, DW_FORM.data1, 4
660 ),
661 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
662 ),
663 ),
664 ),
665 ),
666 *labeled_int_die,
667 )
668 )
669 self.assertIdentical(
670 prog.type("TEST").type,
671 prog.struct_type(
672 None,
673 8,
674 (
675 TypeMember(prog.int_type("int", 4, True), "x", 0),
676 TypeMember(prog.int_type("int", 4, True), "y", 32),
677 ),
678 ),
679 )
680
681 def test_struct_no_members(self):
682 prog = dwarf_program(
683 wrap_test_type_dies(
684 DwarfDie(
685 DW_TAG.structure_type,
686 (DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 0),),
687 )
688 )
689 )
690 self.assertIdentical(prog.type("TEST").type, prog.struct_type(None, 0, ()))
691
692 def test_struct_incomplete(self):
693 prog = dwarf_program(
694 wrap_test_type_dies(
695 DwarfDie(
696 DW_TAG.structure_type,
697 (
698 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
699 DwarfAttrib(DW_AT.declaration, DW_FORM.flag_present, True),
700 ),
701 )
702 )
703 )
704 self.assertIdentical(prog.type("TEST").type, prog.struct_type("point"))
705
706 def test_struct_unnamed_member(self):
707 prog = dwarf_program(
708 wrap_test_type_dies(
709 DwarfDie(
710 DW_TAG.structure_type,
711 (
712 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
713 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
714 ),
715 (
716 DwarfDie(
717 DW_TAG.member,
718 (
719 DwarfAttrib(
720 DW_AT.data_member_location, DW_FORM.data1, 0
721 ),
722 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
723 ),
724 ),
725 DwarfDie(
726 DW_TAG.member,
727 (
728 DwarfAttrib(DW_AT.name, DW_FORM.string, "y"),
729 DwarfAttrib(
730 DW_AT.data_member_location, DW_FORM.data1, 4
731 ),
732 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
733 ),
734 ),
735 ),
736 ),
737 *labeled_int_die,
738 )
739 )
740 self.assertIdentical(
741 prog.type("TEST").type,
742 prog.struct_type(
743 "point",
744 8,
745 (
746 TypeMember(prog.int_type("int", 4, True), None),
747 TypeMember(prog.int_type("int", 4, True), "y", 32),
748 ),
749 ),
750 )
751
752 def test_struct_member_missing_type(self):
753 prog = dwarf_program(
754 wrap_test_type_dies(
755 DwarfDie(
756 DW_TAG.structure_type,
757 (DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),),
758 (
759 DwarfDie(
760 DW_TAG.member,
761 (
762 DwarfAttrib(
763 DW_AT.data_member_location, DW_FORM.data1, 0
764 ),
765 ),
766 ),
767 ),
768 )
769 )
770 )
771 with self.assertRaisesRegex(Exception, "DW_TAG_member is missing DW_AT_type"):
772 prog.type("TEST").type.members[0].type
773
774 def test_struct_member_invalid_type(self):
775 prog = dwarf_program(
776 wrap_test_type_dies(
777 DwarfDie(
778 DW_TAG.structure_type,
779 (DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),),
780 (
781 DwarfDie(
782 DW_TAG.member,
783 (
784 DwarfAttrib(
785 DW_AT.data_member_location, DW_FORM.data1, 0
786 ),
787 DwarfAttrib(DW_AT.type, DW_FORM.string, "foo"),
788 ),
789 ),
790 ),
791 )
792 )
793 )
794 with self.assertRaisesRegex(Exception, "DW_TAG_member has invalid DW_AT_type"):
795 prog.type("TEST").type.members[0].type
796
797 def test_struct_member_invalid_location(self):
798 prog = dwarf_program(
799 wrap_test_type_dies(
800 DwarfDie(
801 DW_TAG.structure_type,
802 (DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),),
803 (
804 DwarfDie(
805 DW_TAG.member,
806 (
807 DwarfAttrib(
808 DW_AT.data_member_location,
809 DW_FORM.string,
810 "foo",
811 ),
812 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
813 ),
814 ),
815 ),
816 ),
817 *labeled_int_die,
818 )
819 )
820 self.assertRaisesRegex(
821 Exception,
822 "DW_TAG_member has invalid DW_AT_data_member_location",
823 prog.type,
824 "TEST",
825 )
826
827 def test_struct_missing_size(self):
828 prog = dwarf_program(wrap_test_type_dies(DwarfDie(DW_TAG.structure_type, ())))
829 self.assertRaisesRegex(
830 Exception,
831 "DW_TAG_structure_type has missing or invalid DW_AT_byte_size",
832 prog.type,
833 "TEST",
834 )
835
836 def test_incomplete_to_complete(self):
837 for version in (4, 5):
838 with self.subTest(version=version):
839 prog = dwarf_program(
840 wrap_test_type_dies(
841 DwarfDie(
842 DW_TAG.pointer_type,
843 (
844 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
845 DwarfAttrib(
846 DW_AT.type, DW_FORM.ref4, "incomplete_struct_die"
847 ),
848 ),
849 ),
850 DwarfLabel("incomplete_struct_die"),
851 DwarfDie(
852 DW_TAG.structure_type,
853 (
854 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
855 DwarfAttrib(
856 DW_AT.declaration, DW_FORM.flag_present, True
857 ),
858 ),
859 ),
860 DwarfDie(
861 DW_TAG.structure_type,
862 (
863 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
864 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
865 DwarfAttrib(DW_AT.decl_file, DW_FORM.udata, "foo.c"),
866 ),
867 (
868 DwarfDie(
869 DW_TAG.member,
870 (
871 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
872 DwarfAttrib(
873 DW_AT.data_member_location, DW_FORM.data1, 0
874 ),
875 DwarfAttrib(
876 DW_AT.type, DW_FORM.ref4, "int_die"
877 ),
878 ),
879 ),
880 DwarfDie(
881 DW_TAG.member,
882 (
883 DwarfAttrib(DW_AT.name, DW_FORM.string, "y"),
884 DwarfAttrib(
885 DW_AT.data_member_location, DW_FORM.data1, 4
886 ),
887 DwarfAttrib(
888 DW_AT.type, DW_FORM.ref4, "int_die"
889 ),
890 ),
891 ),
892 ),
893 ),
894 *labeled_int_die,
895 ),
896 version=version,
897 )
898 self.assertIdentical(
899 prog.type("TEST").type,
900 prog.pointer_type(
901 prog.struct_type(
902 "point",
903 8,
904 (
905 TypeMember(prog.int_type("int", 4, True), "x"),
906 TypeMember(prog.int_type("int", 4, True), "y", 32),
907 ),
908 )
909 ),
910 )
911
912 def test_incomplete_to_complete_namespace(self):
913 for version in (4, 5):
914 with self.subTest(version=version):
915 prog = dwarf_program(
916 wrap_test_type_dies(
917 DwarfDie(
918 DW_TAG.pointer_type,
919 (
920 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
921 DwarfAttrib(
922 DW_AT.type, DW_FORM.ref4, "incomplete_struct_die"
923 ),
924 ),
925 ),
926 DwarfDie(
927 DW_TAG.namespace,
928 (DwarfAttrib(DW_AT.name, DW_FORM.string, "Math"),),
929 (
930 DwarfLabel("incomplete_struct_die"),
931 DwarfDie(
932 DW_TAG.structure_type,
933 (
934 DwarfAttrib(
935 DW_AT.name, DW_FORM.string, "point"
936 ),
937 DwarfAttrib(
938 DW_AT.declaration,
939 DW_FORM.flag_present,
940 True,
941 ),
942 ),
943 ),
944 ),
945 ),
946 DwarfDie(
947 DW_TAG.namespace,
948 (DwarfAttrib(DW_AT.name, DW_FORM.string, "Math"),),
949 (
950 DwarfDie(
951 DW_TAG.structure_type,
952 (
953 DwarfAttrib(
954 DW_AT.name, DW_FORM.string, "point"
955 ),
956 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
957 DwarfAttrib(
958 DW_AT.decl_file, DW_FORM.udata, "foo.c"
959 ),
960 ),
961 (
962 DwarfDie(
963 DW_TAG.member,
964 (
965 DwarfAttrib(
966 DW_AT.name, DW_FORM.string, "x"
967 ),
968 DwarfAttrib(
969 DW_AT.data_member_location,
970 DW_FORM.data1,
971 0,
972 ),
973 DwarfAttrib(
974 DW_AT.type, DW_FORM.ref4, "int_die"
975 ),
976 ),
977 ),
978 DwarfDie(
979 DW_TAG.member,
980 (
981 DwarfAttrib(
982 DW_AT.name, DW_FORM.string, "y"
983 ),
984 DwarfAttrib(
985 DW_AT.data_member_location,
986 DW_FORM.data1,
987 4,
988 ),
989 DwarfAttrib(
990 DW_AT.type, DW_FORM.ref4, "int_die"
991 ),
992 ),
993 ),
994 ),
995 ),
996 ),
997 ),
998 *labeled_int_die,
999 # Incorrect structure we should not access
1000 DwarfDie(
1001 DW_TAG.structure_type,
1002 (
1003 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
1004 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1005 DwarfAttrib(DW_AT.decl_file, DW_FORM.udata, "wrong.c"),
1006 ),
1007 ),
1008 ),
1009 lang=DW_LANG.C_plus_plus,
1010 version=version,
1011 )
1012 self.assertIdentical(
1013 prog.type("TEST").type,
1014 prog.pointer_type(
1015 prog.struct_type(
1016 "point",
1017 8,
1018 (
1019 TypeMember(
1020 prog.int_type(
1021 "int", 4, True, language=Language.CPP
1022 ),
1023 "x",
1024 ),
1025 TypeMember(
1026 prog.int_type(
1027 "int", 4, True, language=Language.CPP
1028 ),
1029 "y",
1030 32,
1031 ),
1032 ),
1033 language=Language.CPP,
1034 ),
1035 language=Language.CPP,
1036 ),
1037 )
1038
1039 def test_incomplete_to_complete_specification(self):
1040 for version in (4, 5):
1041 with self.subTest(version=version):
1042 prog = dwarf_program(
1043 wrap_test_type_dies(
1044 DwarfDie(
1045 DW_TAG.pointer_type,
1046 (
1047 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1048 DwarfAttrib(
1049 DW_AT.type, DW_FORM.ref4, "incomplete_struct_die"
1050 ),
1051 ),
1052 ),
1053 DwarfLabel("incomplete_struct_die"),
1054 DwarfDie(
1055 DW_TAG.structure_type,
1056 (
1057 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
1058 DwarfAttrib(
1059 DW_AT.declaration, DW_FORM.flag_present, True
1060 ),
1061 ),
1062 ),
1063 DwarfDie(
1064 DW_TAG.structure_type,
1065 (
1066 DwarfAttrib(
1067 DW_AT.specification,
1068 DW_FORM.ref4,
1069 "incomplete_struct_die",
1070 ),
1071 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1072 ),
1073 (
1074 DwarfDie(
1075 DW_TAG.member,
1076 (
1077 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
1078 DwarfAttrib(
1079 DW_AT.data_member_location, DW_FORM.data1, 0
1080 ),
1081 DwarfAttrib(
1082 DW_AT.type, DW_FORM.ref4, "int_die"
1083 ),
1084 ),
1085 ),
1086 DwarfDie(
1087 DW_TAG.member,
1088 (
1089 DwarfAttrib(DW_AT.name, DW_FORM.string, "y"),
1090 DwarfAttrib(
1091 DW_AT.data_member_location, DW_FORM.data1, 4
1092 ),
1093 DwarfAttrib(
1094 DW_AT.type, DW_FORM.ref4, "int_die"
1095 ),
1096 ),
1097 ),
1098 ),
1099 ),
1100 *labeled_int_die,
1101 ),
1102 version=version,
1103 )
1104 self.assertIdentical(
1105 prog.type("TEST").type,
1106 prog.pointer_type(
1107 prog.struct_type(
1108 "point",
1109 8,
1110 (
1111 TypeMember(prog.int_type("int", 4, True), "x"),
1112 TypeMember(prog.int_type("int", 4, True), "y", 32),
1113 ),
1114 )
1115 ),
1116 )
1117
1118 def test_incomplete_to_complete_nested(self):
1119 prog = dwarf_program(
1120 wrap_test_type_dies(
1121 DwarfDie(
1122 DW_TAG.pointer_type,
1123 (
1124 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1125 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "incomplete_class_die"),
1126 ),
1127 ),
1128 DwarfDie(
1129 DW_TAG.class_type,
1130 (
1131 DwarfAttrib(DW_AT.name, DW_FORM.string, "Foo"),
1132 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 0),
1133 ),
1134 (
1135 DwarfLabel("incomplete_class_die"),
1136 DwarfDie(
1137 DW_TAG.class_type,
1138 (
1139 DwarfAttrib(DW_AT.name, DW_FORM.string, "Bar"),
1140 DwarfAttrib(
1141 DW_AT.declaration, DW_FORM.flag_present, True
1142 ),
1143 ),
1144 ),
1145 DwarfDie(
1146 DW_TAG.class_type,
1147 (
1148 DwarfAttrib(DW_AT.name, DW_FORM.string, "Bar"),
1149 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 0),
1150 ),
1151 ),
1152 ),
1153 ),
1154 DwarfDie(
1155 DW_TAG.class_type,
1156 (
1157 DwarfAttrib(DW_AT.name, DW_FORM.string, "Bar"),
1158 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 1),
1159 ),
1160 ),
1161 DwarfDie(
1162 DW_TAG.subprogram,
1163 (DwarfAttrib(DW_AT.name, DW_FORM.string, "main"),),
1164 ),
1165 ),
1166 lang=DW_LANG.C_plus_plus,
1167 )
1168 self.assertIdentical(
1169 prog.type("TEST").type.type,
1170 prog.class_type("Bar", 0, ()),
1171 )
1172
1173 def test_incomplete_to_complete_nested_specification(self):
1174 prog = dwarf_program(
1175 wrap_test_type_dies(
1176 DwarfDie(
1177 DW_TAG.pointer_type,
1178 (
1179 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1180 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "incomplete_class_die"),
1181 ),
1182 ),
1183 DwarfDie(
1184 DW_TAG.class_type,
1185 (
1186 DwarfAttrib(DW_AT.name, DW_FORM.string, "Foo"),
1187 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 0),
1188 ),
1189 (
1190 DwarfLabel("incomplete_class_die"),
1191 DwarfDie(
1192 DW_TAG.class_type,
1193 (
1194 DwarfAttrib(DW_AT.name, DW_FORM.string, "Bar"),
1195 DwarfAttrib(
1196 DW_AT.declaration, DW_FORM.flag_present, True
1197 ),
1198 ),
1199 ),
1200 DwarfDie(
1201 DW_TAG.class_type,
1202 (
1203 DwarfAttrib(
1204 DW_AT.specification,
1205 DW_FORM.ref4,
1206 "incomplete_class_die",
1207 ),
1208 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 0),
1209 ),
1210 ),
1211 ),
1212 ),
1213 DwarfDie(
1214 DW_TAG.class_type,
1215 (
1216 DwarfAttrib(DW_AT.name, DW_FORM.string, "Bar"),
1217 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 1),
1218 ),
1219 ),
1220 DwarfDie(
1221 DW_TAG.subprogram,
1222 (DwarfAttrib(DW_AT.name, DW_FORM.string, "main"),),
1223 ),
1224 ),
1225 lang=DW_LANG.C_plus_plus,
1226 )
1227 self.assertIdentical(
1228 prog.type("TEST").type.type,
1229 prog.class_type("Bar", 0, ()),
1230 )
1231
1232 def test_filename(self):
1233 dies = (
1234 DwarfDie(
1235 DW_TAG.structure_type,
1236 [
1237 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
1238 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1239 DwarfAttrib(DW_AT.decl_file, DW_FORM.udata, "foo.c"),
1240 ],
1241 (
1242 DwarfDie(
1243 DW_TAG.member,
1244 (
1245 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
1246 DwarfAttrib(DW_AT.data_member_location, DW_FORM.data1, 0),
1247 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1248 ),
1249 ),
1250 DwarfDie(
1251 DW_TAG.member,
1252 (
1253 DwarfAttrib(DW_AT.name, DW_FORM.string, "y"),
1254 DwarfAttrib(DW_AT.data_member_location, DW_FORM.data1, 4),
1255 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1256 ),
1257 ),
1258 ),
1259 ),
1260 DwarfDie(
1261 DW_TAG.structure_type,
1262 [
1263 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
1264 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1265 DwarfAttrib(DW_AT.decl_file, DW_FORM.udata, "bar/baz.c"),
1266 ],
1267 (
1268 DwarfDie(
1269 DW_TAG.member,
1270 (
1271 DwarfAttrib(DW_AT.name, DW_FORM.string, "a"),
1272 DwarfAttrib(DW_AT.data_member_location, DW_FORM.data1, 0),
1273 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1274 ),
1275 ),
1276 DwarfDie(
1277 DW_TAG.member,
1278 (
1279 DwarfAttrib(DW_AT.name, DW_FORM.string, "b"),
1280 DwarfAttrib(DW_AT.data_member_location, DW_FORM.data1, 4),
1281 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1282 ),
1283 ),
1284 ),
1285 ),
1286 *labeled_int_die,
1287 )
1288
1289 def point_type(prog):
1290 return prog.struct_type(
1291 "point",
1292 8,
1293 (
1294 TypeMember(prog.int_type("int", 4, True), "x"),
1295 TypeMember(prog.int_type("int", 4, True), "y", 32),
1296 ),
1297 )
1298
1299 def other_point_type(prog):
1300 return prog.struct_type(
1301 "point",
1302 8,
1303 (
1304 TypeMember(prog.int_type("int", 4, True), "a"),
1305 TypeMember(prog.int_type("int", 4, True), "b", 32),
1306 ),
1307 )
1308
1309 prog = dwarf_program(dies)
1310 for dir in ["", "src", "usr/src", "/usr/src"]:
1311 with self.subTest(dir=dir):
1312 self.assertIdentical(
1313 prog.type("struct point", os.path.join(dir, "foo.c")),
1314 point_type(prog),
1315 )
1316 for dir in ["", "bar", "src/bar", "usr/src/bar", "/usr/src/bar"]:
1317 with self.subTest(dir=dir):
1318 self.assertIdentical(
1319 prog.type("struct point", os.path.join(dir, "baz.c")),
1320 other_point_type(prog),
1321 )
1322
1323 dies[0].attribs[-1] = DwarfAttrib(DW_AT.decl_file, DW_FORM.udata, "xy/foo.h")
1324 dies[1].attribs[-1] = DwarfAttrib(
1325 DW_AT.decl_file, DW_FORM.udata, "/usr/include/ab/foo.h"
1326 )
1327 prog = dwarf_program(dies)
1328 for dir in ["xy", "src/xy", "usr/src/xy", "/usr/src/xy"]:
1329 with self.subTest(dir=dir):
1330 self.assertIdentical(
1331 prog.type("struct point", os.path.join(dir, "foo.h")),
1332 point_type(prog),
1333 )
1334 for dir in ["ab", "include/ab", "usr/include/ab", "/usr/include/ab"]:
1335 with self.subTest(dir=dir):
1336 self.assertIdentical(
1337 prog.type("struct point", os.path.join(dir, "foo.h")),
1338 other_point_type(prog),
1339 )
1340 for filename in [None, "foo.h"]:
1341 with self.subTest(filename=filename):
1342 t = prog.type("struct point", filename)
1343 self.assertTrue(
1344 identical(t, point_type(prog))
1345 or identical(t, other_point_type(prog))
1346 )
1347
1348 def test_bit_field_data_bit_offset(self):
1349 dies = (
1350 DwarfDie(
1351 DW_TAG.structure_type,
1352 (
1353 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
1354 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1355 ),
1356 (
1357 DwarfDie(
1358 DW_TAG.member,
1359 (
1360 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
1361 DwarfAttrib(DW_AT.data_member_location, DW_FORM.data1, 0),
1362 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1363 ),
1364 ),
1365 DwarfDie(
1366 DW_TAG.member,
1367 (
1368 DwarfAttrib(DW_AT.name, DW_FORM.string, "y"),
1369 DwarfAttrib(DW_AT.bit_size, DW_FORM.data1, 12),
1370 DwarfAttrib(DW_AT.data_bit_offset, DW_FORM.data1, 32),
1371 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1372 ),
1373 ),
1374 DwarfDie(
1375 DW_TAG.member,
1376 (
1377 DwarfAttrib(DW_AT.name, DW_FORM.string, "z"),
1378 DwarfAttrib(DW_AT.bit_size, DW_FORM.data1, 20),
1379 DwarfAttrib(DW_AT.data_bit_offset, DW_FORM.data1, 44),
1380 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1381 ),
1382 ),
1383 ),
1384 ),
1385 *labeled_int_die,
1386 )
1387
1388 for little_endian in [True, False]:
1389 prog = dwarf_program(
1390 wrap_test_type_dies(*dies), little_endian=little_endian
1391 )
1392 self.assertIdentical(
1393 prog.type("TEST").type,
1394 prog.struct_type(
1395 "point",
1396 8,
1397 [
1398 TypeMember(prog.int_type("int", 4, True), "x", 0),
1399 TypeMember(
1400 Object(
1401 prog, prog.int_type("int", 4, True), bit_field_size=12
1402 ),
1403 "y",
1404 32,
1405 ),
1406 TypeMember(
1407 Object(
1408 prog, prog.int_type("int", 4, True), bit_field_size=20
1409 ),
1410 "z",
1411 44,
1412 ),
1413 ],
1414 ),
1415 )
1416
1417 def test_bit_field_bit_offset_big_endian(self):
1418 prog = dwarf_program(
1419 wrap_test_type_dies(
1420 DwarfDie(
1421 DW_TAG.structure_type,
1422 (
1423 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
1424 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1425 ),
1426 (
1427 DwarfDie(
1428 DW_TAG.member,
1429 (
1430 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
1431 DwarfAttrib(
1432 DW_AT.data_member_location, DW_FORM.data1, 0
1433 ),
1434 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1435 ),
1436 ),
1437 DwarfDie(
1438 DW_TAG.member,
1439 (
1440 DwarfAttrib(DW_AT.name, DW_FORM.string, "y"),
1441 DwarfAttrib(DW_AT.bit_size, DW_FORM.data1, 12),
1442 DwarfAttrib(DW_AT.bit_offset, DW_FORM.data1, 32),
1443 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1444 ),
1445 ),
1446 DwarfDie(
1447 DW_TAG.member,
1448 (
1449 DwarfAttrib(DW_AT.name, DW_FORM.string, "z"),
1450 DwarfAttrib(DW_AT.bit_size, DW_FORM.data1, 20),
1451 DwarfAttrib(DW_AT.bit_offset, DW_FORM.data1, 44),
1452 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1453 ),
1454 ),
1455 ),
1456 ),
1457 *labeled_int_die,
1458 ),
1459 little_endian=False,
1460 )
1461 self.assertIdentical(
1462 prog.type("TEST").type,
1463 prog.struct_type(
1464 "point",
1465 8,
1466 [
1467 TypeMember(prog.int_type("int", 4, True), "x", 0),
1468 TypeMember(
1469 Object(prog, prog.int_type("int", 4, True), bit_field_size=12),
1470 "y",
1471 32,
1472 ),
1473 TypeMember(
1474 Object(prog, prog.int_type("int", 4, True), bit_field_size=20),
1475 "z",
1476 44,
1477 ),
1478 ],
1479 ),
1480 )
1481
1482 def test_bit_field_data_member_location_and_bit_offset_big_endian(self):
1483 prog = dwarf_program(
1484 wrap_test_type_dies(
1485 DwarfDie(
1486 DW_TAG.structure_type,
1487 (
1488 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
1489 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1490 ),
1491 (
1492 DwarfDie(
1493 DW_TAG.member,
1494 (
1495 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
1496 DwarfAttrib(
1497 DW_AT.data_member_location, DW_FORM.data1, 0
1498 ),
1499 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1500 ),
1501 ),
1502 DwarfDie(
1503 DW_TAG.member,
1504 (
1505 DwarfAttrib(DW_AT.name, DW_FORM.string, "y"),
1506 DwarfAttrib(DW_AT.bit_size, DW_FORM.data1, 12),
1507 DwarfAttrib(
1508 DW_AT.data_member_location, DW_FORM.data1, 4
1509 ),
1510 DwarfAttrib(DW_AT.bit_offset, DW_FORM.data1, 0),
1511 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1512 ),
1513 ),
1514 DwarfDie(
1515 DW_TAG.member,
1516 (
1517 DwarfAttrib(DW_AT.name, DW_FORM.string, "z"),
1518 DwarfAttrib(DW_AT.bit_size, DW_FORM.data1, 20),
1519 DwarfAttrib(
1520 DW_AT.data_member_location, DW_FORM.data1, 4
1521 ),
1522 DwarfAttrib(DW_AT.bit_offset, DW_FORM.data1, 12),
1523 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1524 ),
1525 ),
1526 ),
1527 ),
1528 *labeled_int_die,
1529 ),
1530 little_endian=False,
1531 )
1532 self.assertIdentical(
1533 prog.type("TEST").type,
1534 prog.struct_type(
1535 "point",
1536 8,
1537 [
1538 TypeMember(prog.int_type("int", 4, True), "x", 0),
1539 TypeMember(
1540 Object(prog, prog.int_type("int", 4, True), bit_field_size=12),
1541 "y",
1542 32,
1543 ),
1544 TypeMember(
1545 Object(prog, prog.int_type("int", 4, True), bit_field_size=20),
1546 "z",
1547 44,
1548 ),
1549 ],
1550 ),
1551 )
1552
1553 def test_bit_field_data_member_location_and_bit_offset_little_endian(self):
1554 prog = dwarf_program(
1555 wrap_test_type_dies(
1556 DwarfDie(
1557 DW_TAG.structure_type,
1558 (
1559 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
1560 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1561 ),
1562 (
1563 DwarfDie(
1564 DW_TAG.member,
1565 (
1566 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
1567 DwarfAttrib(
1568 DW_AT.data_member_location, DW_FORM.data1, 0
1569 ),
1570 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1571 ),
1572 ),
1573 DwarfDie(
1574 DW_TAG.member,
1575 (
1576 DwarfAttrib(DW_AT.name, DW_FORM.string, "y"),
1577 DwarfAttrib(DW_AT.bit_size, DW_FORM.data1, 12),
1578 DwarfAttrib(
1579 DW_AT.data_member_location, DW_FORM.data1, 4
1580 ),
1581 DwarfAttrib(DW_AT.bit_offset, DW_FORM.data1, 20),
1582 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1583 ),
1584 ),
1585 DwarfDie(
1586 DW_TAG.member,
1587 (
1588 DwarfAttrib(DW_AT.name, DW_FORM.string, "z"),
1589 DwarfAttrib(DW_AT.bit_size, DW_FORM.data1, 20),
1590 DwarfAttrib(
1591 DW_AT.data_member_location, DW_FORM.data1, 4
1592 ),
1593 DwarfAttrib(DW_AT.bit_offset, DW_FORM.data1, 0),
1594 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1595 ),
1596 ),
1597 ),
1598 ),
1599 *labeled_int_die,
1600 )
1601 )
1602 self.assertIdentical(
1603 prog.type("TEST").type,
1604 prog.struct_type(
1605 "point",
1606 8,
1607 [
1608 TypeMember(prog.int_type("int", 4, True), "x", 0),
1609 TypeMember(
1610 Object(prog, prog.int_type("int", 4, True), bit_field_size=12),
1611 "y",
1612 32,
1613 ),
1614 TypeMember(
1615 Object(prog, prog.int_type("int", 4, True), bit_field_size=20),
1616 "z",
1617 44,
1618 ),
1619 ],
1620 ),
1621 )
1622
1623 def test_bit_field_data_member_location_and_bit_offset_with_byte_size_little_endian(
1624 self,
1625 ):
1626 prog = dwarf_program(
1627 wrap_test_type_dies(
1628 DwarfDie(
1629 DW_TAG.structure_type,
1630 (
1631 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
1632 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1633 ),
1634 (
1635 DwarfDie(
1636 DW_TAG.member,
1637 (
1638 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
1639 DwarfAttrib(
1640 DW_AT.data_member_location, DW_FORM.data1, 0
1641 ),
1642 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1643 ),
1644 ),
1645 DwarfDie(
1646 DW_TAG.member,
1647 (
1648 DwarfAttrib(DW_AT.name, DW_FORM.string, "y"),
1649 DwarfAttrib(DW_AT.bit_size, DW_FORM.data1, 12),
1650 DwarfAttrib(
1651 DW_AT.data_member_location, DW_FORM.data1, 4
1652 ),
1653 DwarfAttrib(DW_AT.bit_offset, DW_FORM.data1, 20),
1654 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
1655 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1656 ),
1657 ),
1658 DwarfDie(
1659 DW_TAG.member,
1660 (
1661 DwarfAttrib(DW_AT.name, DW_FORM.string, "z"),
1662 DwarfAttrib(DW_AT.bit_size, DW_FORM.data1, 20),
1663 DwarfAttrib(
1664 DW_AT.data_member_location, DW_FORM.data1, 4
1665 ),
1666 DwarfAttrib(DW_AT.bit_offset, DW_FORM.data1, 0),
1667 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
1668 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1669 ),
1670 ),
1671 ),
1672 ),
1673 *labeled_int_die,
1674 )
1675 )
1676 self.assertIdentical(
1677 prog.type("TEST").type,
1678 prog.struct_type(
1679 "point",
1680 8,
1681 [
1682 TypeMember(prog.int_type("int", 4, True), "x", 0),
1683 TypeMember(
1684 Object(prog, prog.int_type("int", 4, True), bit_field_size=12),
1685 "y",
1686 32,
1687 ),
1688 TypeMember(
1689 Object(prog, prog.int_type("int", 4, True), bit_field_size=20),
1690 "z",
1691 44,
1692 ),
1693 ],
1694 ),
1695 )
1696
1697 def test_union(self):
1698 prog = dwarf_program(
1699 wrap_test_type_dies(
1700 DwarfDie(
1701 DW_TAG.union_type,
1702 (
1703 DwarfAttrib(DW_AT.name, DW_FORM.string, "option"),
1704 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
1705 ),
1706 (
1707 DwarfDie(
1708 DW_TAG.member,
1709 (
1710 DwarfAttrib(DW_AT.name, DW_FORM.string, "i"),
1711 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1712 ),
1713 ),
1714 DwarfDie(
1715 DW_TAG.member,
1716 (
1717 DwarfAttrib(DW_AT.name, DW_FORM.string, "f"),
1718 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "float_die"),
1719 ),
1720 ),
1721 ),
1722 ),
1723 *labeled_int_die,
1724 *labeled_float_die,
1725 )
1726 )
1727 self.assertIdentical(
1728 prog.type("TEST").type,
1729 prog.union_type(
1730 "option",
1731 4,
1732 (
1733 TypeMember(prog.int_type("int", 4, True), "i"),
1734 TypeMember(prog.float_type("float", 4), "f"),
1735 ),
1736 ),
1737 )
1738
1739 def test_class(self):
1740 prog = dwarf_program(
1741 wrap_test_type_dies(
1742 DwarfDie(
1743 DW_TAG.class_type,
1744 (
1745 DwarfAttrib(DW_AT.name, DW_FORM.string, "coord"),
1746 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 12),
1747 ),
1748 (
1749 DwarfDie(
1750 DW_TAG.member,
1751 (
1752 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
1753 DwarfAttrib(
1754 DW_AT.data_member_location, DW_FORM.data1, 0
1755 ),
1756 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1757 ),
1758 ),
1759 DwarfDie(
1760 DW_TAG.member,
1761 (
1762 DwarfAttrib(DW_AT.name, DW_FORM.string, "y"),
1763 DwarfAttrib(
1764 DW_AT.data_member_location, DW_FORM.data1, 4
1765 ),
1766 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1767 ),
1768 ),
1769 DwarfDie(
1770 DW_TAG.member,
1771 (
1772 DwarfAttrib(DW_AT.name, DW_FORM.string, "z"),
1773 DwarfAttrib(
1774 DW_AT.data_member_location, DW_FORM.data1, 8
1775 ),
1776 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1777 ),
1778 ),
1779 ),
1780 ),
1781 *labeled_int_die,
1782 )
1783 )
1784 self.assertIdentical(
1785 prog.type("TEST").type,
1786 prog.class_type(
1787 "coord",
1788 12,
1789 (
1790 TypeMember(prog.int_type("int", 4, True), "x", 0),
1791 TypeMember(prog.int_type("int", 4, True), "y", 32),
1792 TypeMember(prog.int_type("int", 4, True), "z", 64),
1793 ),
1794 ),
1795 )
1796
1797 def test_class_template(self):
1798 prog = dwarf_program(
1799 wrap_test_type_dies(
1800 DwarfDie(
1801 DW_TAG.class_type,
1802 (
1803 DwarfAttrib(DW_AT.name, DW_FORM.string, "Array"),
1804 DwarfAttrib(DW_AT.declaration, DW_FORM.flag_present, True),
1805 ),
1806 (
1807 DwarfDie(
1808 DW_TAG.template_type_parameter,
1809 (
1810 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
1811 DwarfAttrib(DW_AT.name, DW_FORM.string, "T"),
1812 ),
1813 ),
1814 DwarfDie(
1815 DW_TAG.template_value_parameter,
1816 (
1817 DwarfAttrib(
1818 DW_AT.type, DW_FORM.ref4, "unsigned_int_die"
1819 ),
1820 DwarfAttrib(DW_AT.name, DW_FORM.string, "N"),
1821 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 2),
1822 ),
1823 ),
1824 ),
1825 ),
1826 *labeled_int_die,
1827 *labeled_unsigned_int_die,
1828 )
1829 )
1830 self.assertIdentical(
1831 prog.type("TEST").type,
1832 prog.class_type(
1833 "Array",
1834 template_parameters=(
1835 TypeTemplateParameter(prog.int_type("int", 4, True), "T"),
1836 TypeTemplateParameter(
1837 Object(prog, prog.int_type("unsigned int", 4, False), 2), "N"
1838 ),
1839 ),
1840 ),
1841 )
1842
1843 def test_template_value_parameter_missing_value(self):
1844 with self.assertRaisesRegex(
1845 Exception, "DW_AT_template_value_parameter is missing value"
1846 ):
1847 dwarf_program(
1848 wrap_test_type_dies(
1849 DwarfDie(
1850 DW_TAG.class_type,
1851 (DwarfAttrib(DW_AT.declaration, DW_FORM.flag_present, True),),
1852 (
1853 DwarfDie(
1854 DW_TAG.template_value_parameter,
1855 (
1856 DwarfAttrib(
1857 DW_AT.type, DW_FORM.ref4, "unsigned_int_die"
1858 ),
1859 DwarfAttrib(DW_AT.name, DW_FORM.string, "N"),
1860 ),
1861 ),
1862 ),
1863 ),
1864 *labeled_unsigned_int_die,
1865 )
1866 ).type("TEST").type.template_parameters[0].argument
1867
1868 def test_class_template_parameter_pack(self):
1869 prog = dwarf_program(
1870 wrap_test_type_dies(
1871 DwarfDie(
1872 DW_TAG.class_type,
1873 (
1874 DwarfAttrib(DW_AT.name, DW_FORM.string, "ParamPack<int, 123>"),
1875 DwarfAttrib(DW_AT.declaration, DW_FORM.flag_present, True),
1876 ),
1877 (
1878 DwarfDie(
1879 DW_TAG.GNU_template_parameter_pack,
1880 (DwarfAttrib(DW_AT.name, DW_FORM.string, "Params"),),
1881 (
1882 DwarfDie(
1883 DW_TAG.template_type_parameter,
1884 (
1885 DwarfAttrib(
1886 DW_AT.type, DW_FORM.ref4, "int_die"
1887 ),
1888 DwarfAttrib(DW_AT.name, DW_FORM.string, "T"),
1889 ),
1890 ),
1891 char_die, # Unexpected die - should be ignored
1892 DwarfDie(
1893 DW_TAG.template_value_parameter,
1894 (
1895 DwarfAttrib(
1896 DW_AT.type, DW_FORM.ref4, "unsigned_int_die"
1897 ),
1898 DwarfAttrib(DW_AT.name, DW_FORM.string, "N"),
1899 DwarfAttrib(
1900 DW_AT.const_value, DW_FORM.data1, 2
1901 ),
1902 ),
1903 ),
1904 ),
1905 ),
1906 ),
1907 ),
1908 *labeled_int_die,
1909 *labeled_unsigned_int_die,
1910 )
1911 )
1912 self.assertIdentical(
1913 prog.type("TEST").type,
1914 prog.class_type(
1915 "ParamPack<int, 123>",
1916 template_parameters=(
1917 TypeTemplateParameter(prog.int_type("int", 4, True), "T"),
1918 TypeTemplateParameter(
1919 Object(prog, prog.int_type("unsigned int", 4, False), 2), "N"
1920 ),
1921 ),
1922 ),
1923 )
1924
1925 def test_lazy_cycle(self):
1926 prog = dwarf_program(
1927 wrap_test_type_dies(
1928 DwarfLabel("struct_die"),
1929 DwarfDie(
1930 DW_TAG.structure_type,
1931 (
1932 DwarfAttrib(DW_AT.name, DW_FORM.string, "foo"),
1933 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1934 ),
1935 (
1936 DwarfDie(
1937 DW_TAG.member,
1938 (
1939 DwarfAttrib(DW_AT.name, DW_FORM.string, "next"),
1940 DwarfAttrib(
1941 DW_AT.data_member_location, DW_FORM.data1, 0
1942 ),
1943 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "pointer_die"),
1944 ),
1945 ),
1946 ),
1947 ),
1948 DwarfLabel("pointer_die"),
1949 DwarfDie(
1950 DW_TAG.pointer_type,
1951 (
1952 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1953 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "struct_die"),
1954 ),
1955 ),
1956 )
1957 )
1958 type_ = prog.struct_type(
1959 "foo", 8, (TypeMember(lambda: prog.pointer_type(type_), "next"),)
1960 )
1961 self.assertIdentical(prog.type("TEST").type, type_)
1962
1963 def test_infinite_cycle(self):
1964 prog = dwarf_program(
1965 wrap_test_type_dies(
1966 DwarfLabel("pointer_die"),
1967 DwarfDie(
1968 DW_TAG.pointer_type,
1969 (
1970 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
1971 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "pointer_die"),
1972 ),
1973 ),
1974 )
1975 )
1976 self.assertRaisesRegex(Exception, "maximum.*depth exceeded", prog.type, "TEST")
1977
1978 def test_enum(self):
1979 prog = dwarf_program(
1980 wrap_test_type_dies(
1981 DwarfDie(
1982 DW_TAG.enumeration_type,
1983 (
1984 DwarfAttrib(DW_AT.name, DW_FORM.string, "color"),
1985 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "unsigned_int_die"),
1986 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
1987 ),
1988 (
1989 DwarfDie(
1990 DW_TAG.enumerator,
1991 (
1992 DwarfAttrib(DW_AT.name, DW_FORM.string, "RED"),
1993 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 0),
1994 ),
1995 ),
1996 DwarfDie(
1997 DW_TAG.enumerator,
1998 (
1999 DwarfAttrib(DW_AT.name, DW_FORM.string, "GREEN"),
2000 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 1),
2001 ),
2002 ),
2003 DwarfDie(
2004 DW_TAG.enumerator,
2005 (
2006 DwarfAttrib(DW_AT.name, DW_FORM.string, "BLUE"),
2007 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 2),
2008 ),
2009 ),
2010 ),
2011 ),
2012 *labeled_unsigned_int_die,
2013 )
2014 )
2015 self.assertIdentical(
2016 prog.type("TEST").type,
2017 prog.enum_type(
2018 "color",
2019 prog.int_type("unsigned int", 4, False),
2020 (
2021 TypeEnumerator("RED", 0),
2022 TypeEnumerator("GREEN", 1),
2023 TypeEnumerator("BLUE", 2),
2024 ),
2025 ),
2026 )
2027
2028 def test_enum_typedef(self):
2029 prog = dwarf_program(
2030 wrap_test_type_dies(
2031 DwarfDie(
2032 DW_TAG.enumeration_type,
2033 (
2034 DwarfAttrib(DW_AT.name, DW_FORM.string, "color"),
2035 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "typedef_die"),
2036 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
2037 ),
2038 (
2039 DwarfDie(
2040 DW_TAG.enumerator,
2041 (
2042 DwarfAttrib(DW_AT.name, DW_FORM.string, "RED"),
2043 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 0),
2044 ),
2045 ),
2046 DwarfDie(
2047 DW_TAG.enumerator,
2048 (
2049 DwarfAttrib(DW_AT.name, DW_FORM.string, "GREEN"),
2050 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 1),
2051 ),
2052 ),
2053 DwarfDie(
2054 DW_TAG.enumerator,
2055 (
2056 DwarfAttrib(DW_AT.name, DW_FORM.string, "BLUE"),
2057 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 2),
2058 ),
2059 ),
2060 ),
2061 ),
2062 DwarfLabel("typedef_die"),
2063 DwarfDie(
2064 DW_TAG.typedef,
2065 (
2066 DwarfAttrib(DW_AT.name, DW_FORM.string, "__uint32_t"),
2067 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "unsigned_int_die"),
2068 ),
2069 ),
2070 *labeled_unsigned_int_die,
2071 )
2072 )
2073 self.assertIdentical(
2074 prog.type("TEST").type,
2075 prog.enum_type(
2076 "color",
2077 prog.int_type("unsigned int", 4, False),
2078 (
2079 TypeEnumerator("RED", 0),
2080 TypeEnumerator("GREEN", 1),
2081 TypeEnumerator("BLUE", 2),
2082 ),
2083 ),
2084 )
2085
2086 def test_enum_anonymous(self):
2087 prog = dwarf_program(
2088 wrap_test_type_dies(
2089 DwarfDie(
2090 DW_TAG.enumeration_type,
2091 (
2092 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "unsigned_int_die"),
2093 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
2094 ),
2095 (
2096 DwarfDie(
2097 DW_TAG.enumerator,
2098 (
2099 DwarfAttrib(DW_AT.name, DW_FORM.string, "RED"),
2100 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 0),
2101 ),
2102 ),
2103 DwarfDie(
2104 DW_TAG.enumerator,
2105 (
2106 DwarfAttrib(DW_AT.name, DW_FORM.string, "GREEN"),
2107 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 1),
2108 ),
2109 ),
2110 DwarfDie(
2111 DW_TAG.enumerator,
2112 (
2113 DwarfAttrib(DW_AT.name, DW_FORM.string, "BLUE"),
2114 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 2),
2115 ),
2116 ),
2117 ),
2118 ),
2119 *labeled_unsigned_int_die,
2120 )
2121 )
2122 self.assertIdentical(
2123 prog.type("TEST").type,
2124 prog.enum_type(
2125 None,
2126 prog.int_type("unsigned int", 4, False),
2127 (
2128 TypeEnumerator("RED", 0),
2129 TypeEnumerator("GREEN", 1),
2130 TypeEnumerator("BLUE", 2),
2131 ),
2132 ),
2133 )
2134
2135 def test_enum_no_enumerators(self):
2136 prog = dwarf_program(
2137 wrap_test_type_dies(
2138 DwarfDie(
2139 DW_TAG.enumeration_type,
2140 (
2141 DwarfAttrib(DW_AT.name, DW_FORM.string, "color"),
2142 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "unsigned_int_die"),
2143 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
2144 ),
2145 ),
2146 *labeled_unsigned_int_die,
2147 )
2148 )
2149 self.assertIdentical(
2150 prog.type("TEST").type,
2151 prog.enum_type("color", prog.int_type("unsigned int", 4, False), ()),
2152 )
2153
2154 def test_enum_incomplete(self):
2155 prog = dwarf_program(
2156 wrap_test_type_dies(
2157 DwarfDie(
2158 DW_TAG.enumeration_type,
2159 (
2160 DwarfAttrib(DW_AT.name, DW_FORM.string, "color"),
2161 DwarfAttrib(DW_AT.declaration, DW_FORM.flag_present, True),
2162 ),
2163 )
2164 )
2165 )
2166 self.assertIdentical(prog.type("TEST").type, prog.enum_type("color"))
2167
2168 def test_enum_old_gcc(self):
2169 # GCC < 5.1
2170 prog = dwarf_program(
2171 wrap_test_type_dies(
2172 DwarfDie(
2173 DW_TAG.enumeration_type,
2174 (
2175 DwarfAttrib(DW_AT.name, DW_FORM.string, "color"),
2176 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
2177 ),
2178 (
2179 DwarfDie(
2180 DW_TAG.enumerator,
2181 (
2182 DwarfAttrib(DW_AT.name, DW_FORM.string, "RED"),
2183 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 0),
2184 ),
2185 ),
2186 DwarfDie(
2187 DW_TAG.enumerator,
2188 (
2189 DwarfAttrib(DW_AT.name, DW_FORM.string, "GREEN"),
2190 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 1),
2191 ),
2192 ),
2193 DwarfDie(
2194 DW_TAG.enumerator,
2195 (
2196 DwarfAttrib(DW_AT.name, DW_FORM.string, "BLUE"),
2197 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 2),
2198 ),
2199 ),
2200 ),
2201 )
2202 )
2203 )
2204 self.assertIdentical(
2205 prog.type("TEST").type,
2206 prog.enum_type(
2207 "color",
2208 prog.int_type("<unknown>", 4, False),
2209 (
2210 TypeEnumerator("RED", 0),
2211 TypeEnumerator("GREEN", 1),
2212 TypeEnumerator("BLUE", 2),
2213 ),
2214 ),
2215 )
2216
2217 def test_enum_old_gcc_signed(self):
2218 # GCC < 5.1
2219 prog = dwarf_program(
2220 wrap_test_type_dies(
2221 DwarfDie(
2222 DW_TAG.enumeration_type,
2223 (
2224 DwarfAttrib(DW_AT.name, DW_FORM.string, "color"),
2225 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
2226 ),
2227 (
2228 DwarfDie(
2229 DW_TAG.enumerator,
2230 (
2231 DwarfAttrib(DW_AT.name, DW_FORM.string, "RED"),
2232 DwarfAttrib(DW_AT.const_value, DW_FORM.sdata, 0),
2233 ),
2234 ),
2235 DwarfDie(
2236 DW_TAG.enumerator,
2237 (
2238 DwarfAttrib(DW_AT.name, DW_FORM.string, "GREEN"),
2239 DwarfAttrib(DW_AT.const_value, DW_FORM.sdata, -1),
2240 ),
2241 ),
2242 DwarfDie(
2243 DW_TAG.enumerator,
2244 (
2245 DwarfAttrib(DW_AT.name, DW_FORM.string, "BLUE"),
2246 DwarfAttrib(DW_AT.const_value, DW_FORM.sdata, -2),
2247 ),
2248 ),
2249 ),
2250 )
2251 )
2252 )
2253 self.assertIdentical(
2254 prog.type("TEST").type,
2255 prog.enum_type(
2256 "color",
2257 prog.int_type("<unknown>", 4, True),
2258 (
2259 TypeEnumerator("RED", 0),
2260 TypeEnumerator("GREEN", -1),
2261 TypeEnumerator("BLUE", -2),
2262 ),
2263 ),
2264 )
2265
2266 def test_enum_compatible_type_not_integer(self):
2267 prog = dwarf_program(
2268 wrap_test_type_dies(
2269 DwarfDie(
2270 DW_TAG.enumeration_type,
2271 (
2272 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "float_die"),
2273 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
2274 ),
2275 ),
2276 *labeled_float_die,
2277 )
2278 )
2279 self.assertRaisesRegex(
2280 Exception,
2281 "DW_AT_type of DW_TAG_enumeration_type is not an integer type",
2282 prog.type,
2283 "TEST",
2284 )
2285
2286 def test_enum_missing_compatible_type_and_byte_size(self):
2287 prog = dwarf_program(wrap_test_type_dies(DwarfDie(DW_TAG.enumeration_type, ())))
2288 self.assertRaisesRegex(
2289 Exception,
2290 "DW_TAG_enumeration_type has missing or invalid DW_AT_byte_size",
2291 prog.type,
2292 "TEST",
2293 )
2294
2295 def test_enum_enumerator_missing_name(self):
2296 prog = dwarf_program(
2297 wrap_test_type_dies(
2298 DwarfDie(
2299 DW_TAG.enumeration_type,
2300 (
2301 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "unsigned_int_die"),
2302 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
2303 ),
2304 (
2305 DwarfDie(
2306 DW_TAG.enumerator,
2307 (DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 0),),
2308 ),
2309 ),
2310 ),
2311 *labeled_unsigned_int_die,
2312 )
2313 )
2314 self.assertRaisesRegex(
2315 Exception,
2316 "DW_TAG_enumerator has missing or invalid DW_AT_name",
2317 prog.type,
2318 "TEST",
2319 )
2320
2321 def test_enum_enumerator_missing_const_value(self):
2322 prog = dwarf_program(
2323 wrap_test_type_dies(
2324 DwarfDie(
2325 DW_TAG.enumeration_type,
2326 (
2327 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "unsigned_int_die"),
2328 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
2329 ),
2330 (
2331 DwarfDie(
2332 DW_TAG.enumerator,
2333 (DwarfAttrib(DW_AT.name, DW_FORM.string, "FOO"),),
2334 ),
2335 ),
2336 ),
2337 *labeled_unsigned_int_die,
2338 )
2339 )
2340 self.assertRaisesRegex(
2341 Exception,
2342 "DW_TAG_enumerator is missing DW_AT_const_value",
2343 prog.type,
2344 "TEST",
2345 )
2346
2347 def test_enum_enumerator_invalid_const_value(self):
2348 prog = dwarf_program(
2349 wrap_test_type_dies(
2350 DwarfDie(
2351 DW_TAG.enumeration_type,
2352 (
2353 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "unsigned_int_die"),
2354 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
2355 ),
2356 (
2357 DwarfDie(
2358 DW_TAG.enumerator,
2359 (
2360 DwarfAttrib(DW_AT.name, DW_FORM.string, "FOO"),
2361 DwarfAttrib(DW_AT.const_value, DW_FORM.string, "FOO"),
2362 ),
2363 ),
2364 ),
2365 ),
2366 *labeled_unsigned_int_die,
2367 )
2368 )
2369 self.assertRaisesRegex(
2370 Exception,
2371 "DW_TAG_enumerator has invalid DW_AT_const_value",
2372 prog.type,
2373 "TEST",
2374 )
2375
2376 def test_tagged_by_name(self):
2377 prog = dwarf_program(
2378 (
2379 *labeled_int_die,
2380 *labeled_unsigned_int_die,
2381 *labeled_float_die,
2382 DwarfDie(
2383 DW_TAG.structure_type,
2384 [
2385 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
2386 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
2387 ],
2388 [
2389 DwarfDie(
2390 DW_TAG.member,
2391 [
2392 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
2393 DwarfAttrib(
2394 DW_AT.data_member_location, DW_FORM.data1, 0
2395 ),
2396 DwarfAttrib(
2397 DW_AT.type,
2398 DW_FORM.ref4,
2399 "int_die",
2400 ),
2401 ],
2402 ),
2403 DwarfDie(
2404 DW_TAG.member,
2405 [
2406 DwarfAttrib(DW_AT.name, DW_FORM.string, "y"),
2407 DwarfAttrib(
2408 DW_AT.data_member_location, DW_FORM.data1, 4
2409 ),
2410 DwarfAttrib(
2411 DW_AT.type,
2412 DW_FORM.ref4,
2413 "int_die",
2414 ),
2415 ],
2416 ),
2417 ],
2418 ),
2419 DwarfDie(
2420 DW_TAG.union_type,
2421 [
2422 DwarfAttrib(DW_AT.name, DW_FORM.string, "option"),
2423 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
2424 ],
2425 [
2426 DwarfDie(
2427 DW_TAG.member,
2428 [
2429 DwarfAttrib(DW_AT.name, DW_FORM.string, "i"),
2430 DwarfAttrib(
2431 DW_AT.type,
2432 DW_FORM.ref4,
2433 "int_die",
2434 ),
2435 ],
2436 ),
2437 DwarfDie(
2438 DW_TAG.member,
2439 [
2440 DwarfAttrib(DW_AT.name, DW_FORM.string, "f"),
2441 DwarfAttrib(
2442 DW_AT.type,
2443 DW_FORM.ref4,
2444 "float_die",
2445 ),
2446 ],
2447 ),
2448 ],
2449 ),
2450 DwarfDie(
2451 DW_TAG.enumeration_type,
2452 [
2453 DwarfAttrib(DW_AT.name, DW_FORM.string, "color"),
2454 DwarfAttrib(
2455 DW_AT.type,
2456 DW_FORM.ref4,
2457 "unsigned_int_die",
2458 ),
2459 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
2460 ],
2461 [
2462 DwarfDie(
2463 DW_TAG.enumerator,
2464 [
2465 DwarfAttrib(DW_AT.name, DW_FORM.string, "RED"),
2466 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 0),
2467 ],
2468 ),
2469 DwarfDie(
2470 DW_TAG.enumerator,
2471 [
2472 DwarfAttrib(DW_AT.name, DW_FORM.string, "GREEN"),
2473 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 1),
2474 ],
2475 ),
2476 DwarfDie(
2477 DW_TAG.enumerator,
2478 [
2479 DwarfAttrib(DW_AT.name, DW_FORM.string, "BLUE"),
2480 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 2),
2481 ],
2482 ),
2483 ],
2484 ),
2485 )
2486 )
2487
2488 self.assertIdentical(
2489 prog.type("struct point"),
2490 prog.struct_type(
2491 "point",
2492 8,
2493 (
2494 TypeMember(prog.int_type("int", 4, True), "x", 0),
2495 TypeMember(prog.int_type("int", 4, True), "y", 32),
2496 ),
2497 ),
2498 )
2499 self.assertRaisesRegex(LookupError, "could not find", prog.type, "union point")
2500 self.assertIdentical(
2501 prog.type("union option"),
2502 prog.union_type(
2503 "option",
2504 4,
2505 (
2506 TypeMember(prog.int_type("int", 4, True), "i"),
2507 TypeMember(prog.float_type("float", 4), "f"),
2508 ),
2509 ),
2510 )
2511 self.assertRaisesRegex(
2512 LookupError, "could not find", prog.type, "struct option"
2513 )
2514 self.assertIdentical(
2515 prog.type("enum color"),
2516 prog.enum_type(
2517 "color",
2518 prog.int_type("unsigned int", 4, False),
2519 (
2520 TypeEnumerator("RED", 0),
2521 TypeEnumerator("GREEN", 1),
2522 TypeEnumerator("BLUE", 2),
2523 ),
2524 ),
2525 )
2526 self.assertRaisesRegex(LookupError, "could not find", prog.type, "struct color")
2527
2528 def test_typedef(self):
2529 prog = dwarf_program(
2530 wrap_test_type_dies(
2531 DwarfDie(
2532 DW_TAG.typedef,
2533 (
2534 DwarfAttrib(DW_AT.name, DW_FORM.string, "INT"),
2535 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
2536 ),
2537 ),
2538 *labeled_int_die,
2539 )
2540 )
2541 self.assertIdentical(
2542 prog.type("TEST").type,
2543 prog.typedef_type("INT", prog.int_type("int", 4, True)),
2544 )
2545
2546 def test_typedef_missing_name(self):
2547 prog = dwarf_program(
2548 wrap_test_type_dies(
2549 DwarfDie(
2550 DW_TAG.typedef, (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),)
2551 ),
2552 *labeled_int_die,
2553 )
2554 )
2555 self.assertRaisesRegex(
2556 Exception,
2557 "DW_TAG_typedef has missing or invalid DW_AT_name",
2558 prog.type,
2559 "TEST",
2560 )
2561
2562 def test_typedef_void(self):
2563 prog = dwarf_program(
2564 wrap_test_type_dies(
2565 DwarfDie(
2566 DW_TAG.typedef, (DwarfAttrib(DW_AT.name, DW_FORM.string, "VOID"),)
2567 )
2568 )
2569 )
2570 self.assertIdentical(
2571 prog.type("TEST").type, prog.typedef_type("VOID", prog.void_type())
2572 )
2573
2574 def test_typedef_by_name(self):
2575 prog = dwarf_program(
2576 (
2577 DwarfDie(
2578 DW_TAG.typedef,
2579 (
2580 DwarfAttrib(DW_AT.name, DW_FORM.string, "pid_t"),
2581 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
2582 ),
2583 ),
2584 *labeled_int_die,
2585 )
2586 )
2587 self.assertIdentical(
2588 prog.type("pid_t"),
2589 prog.typedef_type("pid_t", prog.int_type("int", 4, True)),
2590 )
2591
2592 def test_pointer(self):
2593 prog = dwarf_program(
2594 wrap_test_type_dies(
2595 DwarfDie(
2596 DW_TAG.pointer_type,
2597 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
2598 ),
2599 *labeled_int_die,
2600 )
2601 )
2602 self.assertIdentical(
2603 prog.type("TEST").type, prog.pointer_type(prog.int_type("int", 4, True))
2604 )
2605 self.assertIdentical(
2606 prog.type("TEST").type, prog.pointer_type(prog.int_type("int", 4, True), 8)
2607 )
2608
2609 def test_pointer_explicit_size(self):
2610 prog = dwarf_program(
2611 wrap_test_type_dies(
2612 DwarfDie(
2613 DW_TAG.pointer_type,
2614 (
2615 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
2616 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
2617 ),
2618 ),
2619 *labeled_int_die,
2620 )
2621 )
2622 self.assertIdentical(
2623 prog.type("TEST").type, prog.pointer_type(prog.int_type("int", 4, True), 4)
2624 )
2625
2626 def test_pointer_void(self):
2627 prog = dwarf_program(wrap_test_type_dies(DwarfDie(DW_TAG.pointer_type, ())))
2628 self.assertIdentical(
2629 prog.type("TEST").type, prog.pointer_type(prog.void_type())
2630 )
2631
2632 def test_array(self):
2633 prog = dwarf_program(
2634 wrap_test_type_dies(
2635 DwarfDie(
2636 DW_TAG.array_type,
2637 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
2638 (
2639 DwarfDie(
2640 DW_TAG.subrange_type,
2641 (DwarfAttrib(DW_AT.upper_bound, DW_FORM.data1, 1),),
2642 ),
2643 ),
2644 ),
2645 *labeled_int_die,
2646 )
2647 )
2648 self.assertIdentical(
2649 prog.type("TEST").type, prog.array_type(prog.int_type("int", 4, True), 2)
2650 )
2651
2652 def test_array_two_dimensional(self):
2653 prog = dwarf_program(
2654 wrap_test_type_dies(
2655 DwarfDie(
2656 DW_TAG.array_type,
2657 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
2658 (
2659 DwarfDie(
2660 DW_TAG.subrange_type,
2661 (DwarfAttrib(DW_AT.upper_bound, DW_FORM.data1, 1),),
2662 ),
2663 DwarfDie(
2664 DW_TAG.subrange_type,
2665 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 3),),
2666 ),
2667 ),
2668 ),
2669 *labeled_int_die,
2670 )
2671 )
2672 self.assertIdentical(
2673 prog.type("TEST").type,
2674 prog.array_type(prog.array_type(prog.int_type("int", 4, True), 3), 2),
2675 )
2676
2677 def test_array_three_dimensional(self):
2678 prog = dwarf_program(
2679 wrap_test_type_dies(
2680 DwarfDie(
2681 DW_TAG.array_type,
2682 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
2683 (
2684 DwarfDie(
2685 DW_TAG.subrange_type,
2686 (DwarfAttrib(DW_AT.upper_bound, DW_FORM.data1, 1),),
2687 ),
2688 DwarfDie(
2689 DW_TAG.subrange_type,
2690 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 3),),
2691 ),
2692 DwarfDie(
2693 DW_TAG.subrange_type,
2694 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 4),),
2695 ),
2696 ),
2697 ),
2698 *labeled_int_die,
2699 )
2700 )
2701 self.assertIdentical(
2702 prog.type("TEST").type,
2703 prog.array_type(
2704 prog.array_type(prog.array_type(prog.int_type("int", 4, True), 4), 3), 2
2705 ),
2706 )
2707
2708 def test_array_missing_type(self):
2709 prog = dwarf_program(
2710 wrap_test_type_dies(
2711 DwarfDie(
2712 DW_TAG.array_type,
2713 (),
2714 (
2715 DwarfDie(
2716 DW_TAG.subrange_type,
2717 (DwarfAttrib(DW_AT.upper_bound, DW_FORM.data1, 1),),
2718 ),
2719 ),
2720 ),
2721 int_die,
2722 )
2723 )
2724 self.assertRaisesRegex(
2725 Exception, "DW_TAG_array_type is missing DW_AT_type", prog.type, "TEST"
2726 )
2727
2728 def test_array_zero_length_count(self):
2729 prog = dwarf_program(
2730 wrap_test_type_dies(
2731 DwarfDie(
2732 DW_TAG.array_type,
2733 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
2734 (
2735 DwarfDie(
2736 DW_TAG.subrange_type,
2737 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 0),),
2738 ),
2739 ),
2740 ),
2741 *labeled_int_die,
2742 )
2743 )
2744 self.assertIdentical(
2745 prog.type("TEST").type, prog.array_type(prog.int_type("int", 4, True), 0)
2746 )
2747
2748 def test_array_zero_length_upper_bound(self):
2749 prog = dwarf_program(
2750 wrap_test_type_dies(
2751 DwarfDie(
2752 DW_TAG.array_type,
2753 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
2754 (
2755 DwarfDie(
2756 DW_TAG.subrange_type,
2757 (DwarfAttrib(DW_AT.upper_bound, DW_FORM.sdata, -1),),
2758 ),
2759 ),
2760 ),
2761 *labeled_int_die,
2762 )
2763 )
2764 self.assertIdentical(
2765 prog.type("TEST").type, prog.array_type(prog.int_type("int", 4, True), 0)
2766 )
2767
2768 def test_array_zero_length_upper_bound_cpp(self):
2769 prog = dwarf_program(
2770 wrap_test_type_dies(
2771 DwarfDie(
2772 DW_TAG.array_type,
2773 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
2774 (
2775 DwarfDie(
2776 DW_TAG.subrange_type,
2777 (
2778 DwarfAttrib(
2779 DW_AT.upper_bound,
2780 DW_FORM.data8,
2781 18446744073709551615,
2782 ),
2783 ),
2784 ),
2785 ),
2786 ),
2787 *labeled_int_die,
2788 )
2789 )
2790 self.assertIdentical(
2791 prog.type("TEST").type, prog.array_type(prog.int_type("int", 4, True), 0)
2792 )
2793
2794 def test_incomplete_array_no_subrange(self):
2795 prog = dwarf_program(
2796 wrap_test_type_dies(
2797 DwarfDie(
2798 DW_TAG.array_type,
2799 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
2800 ),
2801 *labeled_int_die,
2802 )
2803 )
2804 self.assertIdentical(
2805 prog.type("TEST").type, prog.array_type(prog.int_type("int", 4, True))
2806 )
2807
2808 def test_incomplete_array_empty_subrange(self):
2809 prog = dwarf_program(
2810 wrap_test_type_dies(
2811 DwarfDie(
2812 DW_TAG.array_type,
2813 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
2814 (DwarfDie(DW_TAG.subrange_type, ()),),
2815 ),
2816 *labeled_int_die,
2817 )
2818 )
2819 self.assertIdentical(
2820 prog.type("TEST").type, prog.array_type(prog.int_type("int", 4, True))
2821 )
2822
2823 def test_incomplete_array_of_array(self):
2824 prog = dwarf_program(
2825 wrap_test_type_dies(
2826 # int [3][]
2827 DwarfDie(
2828 DW_TAG.array_type,
2829 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
2830 (
2831 DwarfDie(DW_TAG.subrange_type, ()),
2832 DwarfDie(
2833 DW_TAG.subrange_type,
2834 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 3),),
2835 ),
2836 ),
2837 ),
2838 *labeled_int_die,
2839 )
2840 )
2841 self.assertIdentical(
2842 prog.type("TEST").type,
2843 prog.array_type(prog.array_type(prog.int_type("int", 4, True), 3)),
2844 )
2845
2846 def test_array_of_zero_length_array(self):
2847 prog = dwarf_program(
2848 wrap_test_type_dies(
2849 # int [3][0]
2850 DwarfDie(
2851 DW_TAG.array_type,
2852 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
2853 (
2854 DwarfDie(
2855 DW_TAG.subrange_type,
2856 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 3),),
2857 ),
2858 DwarfDie(
2859 DW_TAG.subrange_type,
2860 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 0),),
2861 ),
2862 ),
2863 ),
2864 *labeled_int_die,
2865 )
2866 )
2867 self.assertIdentical(
2868 prog.type("TEST").type,
2869 prog.array_type(prog.array_type(prog.int_type("int", 4, True), 0), 3),
2870 )
2871
2872 def test_array_of_zero_length_array_old_gcc(self):
2873 # GCC < 9.0
2874 prog = dwarf_program(
2875 wrap_test_type_dies(
2876 # int [3][0]
2877 DwarfDie(
2878 DW_TAG.array_type,
2879 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
2880 (
2881 DwarfDie(
2882 DW_TAG.subrange_type,
2883 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 3),),
2884 ),
2885 DwarfDie(DW_TAG.subrange_type, ()),
2886 ),
2887 ),
2888 *labeled_int_die,
2889 )
2890 )
2891 self.assertIdentical(
2892 prog.type("TEST").type,
2893 prog.array_type(prog.array_type(prog.int_type("int", 4, True), 0), 3),
2894 )
2895
2896 def test_array_of_zero_length_array_typedef(self):
2897 prog = dwarf_program(
2898 wrap_test_type_dies(
2899 # ZARRAY [3]
2900 DwarfDie(
2901 DW_TAG.array_type,
2902 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "typedef_die"),),
2903 (
2904 DwarfDie(
2905 DW_TAG.subrange_type,
2906 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 3),),
2907 ),
2908 ),
2909 ),
2910 # typedef int ZARRAY[0];
2911 DwarfLabel("typedef_die"),
2912 DwarfDie(
2913 DW_TAG.typedef,
2914 (
2915 DwarfAttrib(DW_AT.name, DW_FORM.string, "ZARRAY"),
2916 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
2917 ),
2918 ),
2919 DwarfLabel("array_die"),
2920 DwarfDie(
2921 DW_TAG.array_type,
2922 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
2923 (
2924 DwarfDie(
2925 DW_TAG.subrange_type,
2926 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 0),),
2927 ),
2928 ),
2929 ),
2930 *labeled_int_die,
2931 )
2932 )
2933 self.assertIdentical(
2934 prog.type("TEST").type,
2935 prog.array_type(
2936 prog.typedef_type(
2937 "ZARRAY", prog.array_type(prog.int_type("int", 4, True), 0)
2938 ),
2939 3,
2940 ),
2941 )
2942
2943 def test_array_of_zero_length_array_typedef_old_gcc(self):
2944 # GCC actually squashes arrays of typedef arrays into one array type,
2945 # but let's handle it like GCC < 9.0 anyways.
2946 prog = dwarf_program(
2947 wrap_test_type_dies(
2948 # ZARRAY [3]
2949 DwarfDie(
2950 DW_TAG.array_type,
2951 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "typedef_die"),),
2952 (
2953 DwarfDie(
2954 DW_TAG.subrange_type,
2955 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 3),),
2956 ),
2957 ),
2958 ),
2959 # typedef int ZARRAY[0];
2960 DwarfLabel("typedef_die"),
2961 DwarfDie(
2962 DW_TAG.typedef,
2963 (
2964 DwarfAttrib(DW_AT.name, DW_FORM.string, "ZARRAY"),
2965 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
2966 ),
2967 ),
2968 DwarfLabel("array_die"),
2969 DwarfDie(
2970 DW_TAG.array_type,
2971 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
2972 (DwarfDie(DW_TAG.subrange_type, ()),),
2973 ),
2974 *labeled_int_die,
2975 )
2976 )
2977 self.assertIdentical(
2978 prog.type("TEST").type,
2979 prog.array_type(
2980 prog.typedef_type(
2981 "ZARRAY", prog.array_type(prog.int_type("int", 4, True), 0)
2982 ),
2983 3,
2984 ),
2985 )
2986
2987 def test_flexible_array_member(self):
2988 # struct {
2989 # int i;
2990 # int a[];
2991 # };
2992 prog = dwarf_program(
2993 wrap_test_type_dies(
2994 DwarfDie(
2995 DW_TAG.structure_type,
2996 (DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),),
2997 (
2998 DwarfDie(
2999 DW_TAG.member,
3000 (
3001 DwarfAttrib(DW_AT.name, DW_FORM.string, "i"),
3002 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
3003 ),
3004 ),
3005 DwarfDie(
3006 DW_TAG.member,
3007 (
3008 DwarfAttrib(DW_AT.name, DW_FORM.string, "a"),
3009 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
3010 DwarfAttrib(
3011 DW_AT.data_member_location, DW_FORM.data1, 4
3012 ),
3013 ),
3014 ),
3015 ),
3016 ),
3017 DwarfLabel("array_die"),
3018 DwarfDie(
3019 DW_TAG.array_type,
3020 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3021 ),
3022 *labeled_int_die,
3023 )
3024 )
3025 self.assertIdentical(
3026 prog.type("TEST").type,
3027 prog.struct_type(
3028 None,
3029 4,
3030 (
3031 TypeMember(prog.int_type("int", 4, True), "i"),
3032 TypeMember(prog.array_type(prog.int_type("int", 4, True)), "a", 32),
3033 ),
3034 ),
3035 )
3036
3037 def test_typedef_flexible_array_member(self):
3038 prog = dwarf_program(
3039 wrap_test_type_dies(
3040 # struct {
3041 # int i;
3042 # FARRAY a;
3043 # };
3044 DwarfDie(
3045 DW_TAG.structure_type,
3046 (DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),),
3047 (
3048 DwarfDie(
3049 DW_TAG.member,
3050 (
3051 DwarfAttrib(DW_AT.name, DW_FORM.string, "i"),
3052 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
3053 ),
3054 ),
3055 DwarfDie(
3056 DW_TAG.member,
3057 (
3058 DwarfAttrib(DW_AT.name, DW_FORM.string, "a"),
3059 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "typedef_die"),
3060 DwarfAttrib(
3061 DW_AT.data_member_location, DW_FORM.data1, 4
3062 ),
3063 ),
3064 ),
3065 ),
3066 ),
3067 # typedef int FARRAY[];
3068 DwarfLabel("typedef_die"),
3069 DwarfDie(
3070 DW_TAG.typedef,
3071 (
3072 DwarfAttrib(DW_AT.name, DW_FORM.string, "FARRAY"),
3073 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
3074 ),
3075 ),
3076 DwarfLabel("array_die"),
3077 DwarfDie(
3078 DW_TAG.array_type,
3079 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3080 ),
3081 *labeled_int_die,
3082 )
3083 )
3084 self.assertIdentical(
3085 prog.type("TEST").type,
3086 prog.struct_type(
3087 None,
3088 4,
3089 (
3090 TypeMember(prog.int_type("int", 4, True), "i"),
3091 TypeMember(
3092 prog.typedef_type(
3093 "FARRAY", prog.array_type(prog.int_type("int", 4, True))
3094 ),
3095 "a",
3096 32,
3097 ),
3098 ),
3099 ),
3100 )
3101
3102 def test_zero_length_array_only_member(self):
3103 # struct {
3104 # int a[0];
3105 # };
3106 prog = dwarf_program(
3107 wrap_test_type_dies(
3108 DwarfDie(
3109 DW_TAG.structure_type,
3110 (DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),),
3111 (
3112 DwarfDie(
3113 DW_TAG.member,
3114 (
3115 DwarfAttrib(DW_AT.name, DW_FORM.string, "a"),
3116 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
3117 ),
3118 ),
3119 ),
3120 ),
3121 DwarfLabel("array_die"),
3122 DwarfDie(
3123 DW_TAG.array_type,
3124 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3125 (
3126 DwarfDie(
3127 DW_TAG.subrange_type,
3128 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 0),),
3129 ),
3130 ),
3131 ),
3132 *labeled_int_die,
3133 )
3134 )
3135 self.assertIdentical(
3136 prog.type("TEST").type,
3137 prog.struct_type(
3138 None,
3139 4,
3140 (TypeMember(prog.array_type(prog.int_type("int", 4, True), 0), "a"),),
3141 ),
3142 )
3143
3144 def test_zero_length_array_only_member_old_gcc(self):
3145 # GCC < 9.0.
3146 # struct {
3147 # int a[0];
3148 # };
3149 prog = dwarf_program(
3150 wrap_test_type_dies(
3151 DwarfDie(
3152 DW_TAG.structure_type,
3153 (DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),),
3154 (
3155 DwarfDie(
3156 DW_TAG.member,
3157 (
3158 DwarfAttrib(DW_AT.name, DW_FORM.string, "a"),
3159 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
3160 ),
3161 ),
3162 ),
3163 ),
3164 DwarfLabel("array_die"),
3165 DwarfDie(
3166 DW_TAG.array_type,
3167 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3168 (DwarfDie(DW_TAG.subrange_type, ()),),
3169 ),
3170 *labeled_int_die,
3171 )
3172 )
3173 self.assertIdentical(
3174 prog.type("TEST").type,
3175 prog.struct_type(
3176 None,
3177 4,
3178 (TypeMember(prog.array_type(prog.int_type("int", 4, True), 0), "a"),),
3179 ),
3180 )
3181
3182 def test_qualified_zero_length_array_only_member_old_gcc(self):
3183 # GCC < 9.0.
3184 # struct {
3185 # const int a[0];
3186 # };
3187 prog = dwarf_program(
3188 wrap_test_type_dies(
3189 DwarfDie(
3190 DW_TAG.structure_type,
3191 (DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),),
3192 (
3193 DwarfDie(
3194 DW_TAG.member,
3195 (
3196 DwarfAttrib(DW_AT.name, DW_FORM.string, "a"),
3197 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "const_die"),
3198 ),
3199 ),
3200 ),
3201 ),
3202 DwarfLabel("const_die"),
3203 DwarfDie(
3204 DW_TAG.const_type,
3205 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),),
3206 ),
3207 DwarfLabel("array_die"),
3208 DwarfDie(
3209 DW_TAG.array_type,
3210 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3211 (DwarfDie(DW_TAG.subrange_type, ()),),
3212 ),
3213 *labeled_int_die,
3214 )
3215 )
3216 self.assertIdentical(
3217 prog.type("TEST").type,
3218 prog.struct_type(
3219 None,
3220 4,
3221 (
3222 TypeMember(
3223 prog.array_type(
3224 prog.int_type("int", 4, True),
3225 0,
3226 qualifiers=Qualifiers.CONST,
3227 ),
3228 "a",
3229 ),
3230 ),
3231 ),
3232 )
3233
3234 def test_typedef_zero_length_array_only_member(self):
3235 prog = dwarf_program(
3236 wrap_test_type_dies(
3237 DwarfDie(
3238 # struct foo {
3239 # ZARRAY a;
3240 # };
3241 DW_TAG.structure_type,
3242 (
3243 DwarfAttrib(DW_AT.name, DW_FORM.string, "foo"),
3244 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
3245 ),
3246 (
3247 DwarfDie(
3248 DW_TAG.member,
3249 (
3250 DwarfAttrib(DW_AT.name, DW_FORM.string, "a"),
3251 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "typedef_die"),
3252 ),
3253 ),
3254 ),
3255 ),
3256 # typedef int ZARRAY[0];
3257 DwarfLabel("typedef_die"),
3258 DwarfDie(
3259 DW_TAG.typedef,
3260 (
3261 DwarfAttrib(DW_AT.name, DW_FORM.string, "ZARRAY"),
3262 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
3263 ),
3264 (
3265 DwarfDie(
3266 DW_TAG.subrange_type,
3267 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 0),),
3268 ),
3269 ),
3270 ),
3271 DwarfLabel("array_die"),
3272 DwarfDie(
3273 DW_TAG.array_type,
3274 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3275 ),
3276 *labeled_int_die,
3277 )
3278 )
3279 self.assertIdentical(
3280 prog.type("TEST").type,
3281 prog.struct_type(
3282 "foo",
3283 4,
3284 (
3285 TypeMember(
3286 prog.typedef_type(
3287 "ZARRAY", prog.array_type(prog.int_type("int", 4, True), 0)
3288 ),
3289 "a",
3290 ),
3291 ),
3292 ),
3293 )
3294
3295 def test_typedef_zero_length_array_only_member_old_gcc(self):
3296 # GCC < 9.0.
3297 dies = (
3298 DwarfDie(
3299 # struct foo {
3300 # ZARRAY a;
3301 # };
3302 DW_TAG.structure_type,
3303 (
3304 DwarfAttrib(DW_AT.name, DW_FORM.string, "foo"),
3305 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
3306 ),
3307 (
3308 DwarfDie(
3309 DW_TAG.member,
3310 (
3311 DwarfAttrib(DW_AT.name, DW_FORM.string, "a"),
3312 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "typedef_die"),
3313 ),
3314 ),
3315 ),
3316 ),
3317 # typedef int ZARRAY[0];
3318 DwarfLabel("typedef_die"),
3319 DwarfDie(
3320 DW_TAG.typedef,
3321 (
3322 DwarfAttrib(DW_AT.name, DW_FORM.string, "ZARRAY"),
3323 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
3324 ),
3325 ),
3326 DwarfLabel("array_die"),
3327 DwarfDie(
3328 DW_TAG.array_type, (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),)
3329 ),
3330 *labeled_int_die,
3331 )
3332
3333 prog = dwarf_program(wrap_test_type_dies(*dies))
3334 self.assertIdentical(
3335 prog.type("TEST").type,
3336 prog.struct_type(
3337 "foo",
3338 4,
3339 (
3340 TypeMember(
3341 prog.typedef_type(
3342 "ZARRAY", prog.array_type(prog.int_type("int", 4, True), 0)
3343 ),
3344 "a",
3345 ),
3346 ),
3347 ),
3348 )
3349 # Although the ZARRAY type must be a zero-length array in the context
3350 # of the structure, it could still be an incomplete array if used
3351 # elsewhere.
3352 self.assertIdentical(
3353 prog.type("ZARRAY"),
3354 prog.typedef_type("ZARRAY", prog.array_type(prog.int_type("int", 4, True))),
3355 )
3356
3357 # Make sure it still works if we parse the array type first.
3358 prog = dwarf_program(wrap_test_type_dies(*dies))
3359 self.assertIdentical(
3360 prog.type("ZARRAY"),
3361 prog.typedef_type("ZARRAY", prog.array_type(prog.int_type("int", 4, True))),
3362 )
3363 self.assertIdentical(
3364 prog.type("TEST").type,
3365 prog.struct_type(
3366 "foo",
3367 4,
3368 (
3369 TypeMember(
3370 prog.typedef_type(
3371 "ZARRAY", prog.array_type(prog.int_type("int", 4, True), 0)
3372 ),
3373 "a",
3374 ),
3375 ),
3376 ),
3377 )
3378
3379 def test_zero_length_array_not_last_member(self):
3380 # struct {
3381 # int a[0];
3382 # int i;
3383 # };
3384 prog = dwarf_program(
3385 wrap_test_type_dies(
3386 DwarfDie(
3387 DW_TAG.structure_type,
3388 (DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),),
3389 (
3390 DwarfDie(
3391 DW_TAG.member,
3392 (
3393 DwarfAttrib(DW_AT.name, DW_FORM.string, "a"),
3394 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
3395 ),
3396 ),
3397 DwarfDie(
3398 DW_TAG.member,
3399 (
3400 DwarfAttrib(DW_AT.name, DW_FORM.string, "i"),
3401 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
3402 ),
3403 ),
3404 ),
3405 ),
3406 DwarfLabel("array_die"),
3407 DwarfDie(
3408 DW_TAG.array_type,
3409 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3410 (
3411 DwarfDie(
3412 DW_TAG.subrange_type,
3413 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 0),),
3414 ),
3415 ),
3416 ),
3417 *labeled_int_die,
3418 )
3419 )
3420 self.assertIdentical(
3421 prog.type("TEST").type,
3422 prog.struct_type(
3423 None,
3424 4,
3425 (
3426 TypeMember(prog.array_type(prog.int_type("int", 4, True), 0), "a"),
3427 TypeMember(prog.int_type("int", 4, True), "i"),
3428 ),
3429 ),
3430 )
3431
3432 def test_zero_length_array_not_last_member_old_gcc(self):
3433 # GCC < 9.0.
3434 # struct {
3435 # int a[0];
3436 # int i;
3437 # };
3438 prog = dwarf_program(
3439 wrap_test_type_dies(
3440 DwarfDie(
3441 DW_TAG.structure_type,
3442 (DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),),
3443 (
3444 DwarfDie(
3445 DW_TAG.member,
3446 (
3447 DwarfAttrib(DW_AT.name, DW_FORM.string, "a"),
3448 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
3449 ),
3450 ),
3451 DwarfDie(
3452 DW_TAG.member,
3453 (
3454 DwarfAttrib(DW_AT.name, DW_FORM.string, "i"),
3455 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
3456 ),
3457 ),
3458 ),
3459 ),
3460 DwarfLabel("array_die"),
3461 DwarfDie(
3462 DW_TAG.array_type,
3463 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3464 (DwarfDie(DW_TAG.subrange_type, ()),),
3465 ),
3466 *labeled_int_die,
3467 )
3468 )
3469 self.assertIdentical(
3470 prog.type("TEST").type,
3471 prog.struct_type(
3472 None,
3473 4,
3474 (
3475 TypeMember(prog.array_type(prog.int_type("int", 4, True), 0), "a"),
3476 TypeMember(prog.int_type("int", 4, True), "i"),
3477 ),
3478 ),
3479 )
3480
3481 def test_zero_length_array_in_union(self):
3482 # union {
3483 # int i;
3484 # int a[0];
3485 # };
3486 prog = dwarf_program(
3487 wrap_test_type_dies(
3488 DwarfDie(
3489 DW_TAG.union_type,
3490 (DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),),
3491 (
3492 DwarfDie(
3493 DW_TAG.member,
3494 (
3495 DwarfAttrib(DW_AT.name, DW_FORM.string, "i"),
3496 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
3497 ),
3498 ),
3499 DwarfDie(
3500 DW_TAG.member,
3501 (
3502 DwarfAttrib(DW_AT.name, DW_FORM.string, "a"),
3503 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
3504 ),
3505 ),
3506 ),
3507 ),
3508 DwarfLabel("array_die"),
3509 DwarfDie(
3510 DW_TAG.array_type,
3511 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3512 (
3513 DwarfDie(
3514 DW_TAG.subrange_type,
3515 (DwarfAttrib(DW_AT.count, DW_FORM.data1, 0),),
3516 ),
3517 ),
3518 ),
3519 *labeled_int_die,
3520 )
3521 )
3522 self.assertIdentical(
3523 prog.type("TEST").type,
3524 prog.union_type(
3525 None,
3526 4,
3527 (
3528 TypeMember(prog.int_type("int", 4, True), "i"),
3529 TypeMember(prog.array_type(prog.int_type("int", 4, True), 0), "a"),
3530 ),
3531 ),
3532 )
3533
3534 def test_zero_length_array_in_union_old_gcc(self):
3535 # GCC < 9.0.
3536 # union {
3537 # int i;
3538 # int a[0];
3539 # };
3540 prog = dwarf_program(
3541 wrap_test_type_dies(
3542 DwarfDie(
3543 DW_TAG.union_type,
3544 (DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),),
3545 (
3546 DwarfDie(
3547 DW_TAG.member,
3548 (
3549 DwarfAttrib(DW_AT.name, DW_FORM.string, "i"),
3550 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
3551 ),
3552 ),
3553 DwarfDie(
3554 DW_TAG.member,
3555 (
3556 DwarfAttrib(DW_AT.name, DW_FORM.string, "a"),
3557 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
3558 ),
3559 ),
3560 ),
3561 ),
3562 DwarfLabel("array_die"),
3563 DwarfDie(
3564 DW_TAG.array_type,
3565 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3566 (DwarfDie(DW_TAG.subrange_type, ()),),
3567 ),
3568 *labeled_int_die,
3569 )
3570 )
3571 self.assertIdentical(
3572 prog.type("TEST").type,
3573 prog.union_type(
3574 None,
3575 4,
3576 (
3577 TypeMember(prog.int_type("int", 4, True), "i"),
3578 TypeMember(prog.array_type(prog.int_type("int", 4, True), 0), "a"),
3579 ),
3580 ),
3581 )
3582
3583 def test_pointer_size(self):
3584 prog = dwarf_program((int_die,), bits=32)
3585 self.assertIdentical(
3586 prog.type("int *"), prog.pointer_type(prog.int_type("int", 4, True), 4)
3587 )
3588
3589 def test_function_no_parameters(self):
3590 # int foo(void)
3591 prog = dwarf_program(
3592 wrap_test_type_dies(
3593 DwarfDie(
3594 DW_TAG.subroutine_type,
3595 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3596 ),
3597 *labeled_int_die,
3598 )
3599 )
3600 self.assertIdentical(
3601 prog.type("TEST").type,
3602 prog.function_type(prog.int_type("int", 4, True), (), False),
3603 )
3604
3605 def test_function_void_return(self):
3606 # void foo(void)
3607 prog = dwarf_program(wrap_test_type_dies(DwarfDie(DW_TAG.subroutine_type, ())))
3608 self.assertIdentical(
3609 prog.type("TEST").type, prog.function_type(prog.void_type(), (), False)
3610 )
3611
3612 def test_function_unnamed_parameter(self):
3613 # int foo(char)
3614 prog = dwarf_program(
3615 wrap_test_type_dies(
3616 DwarfDie(
3617 DW_TAG.subroutine_type,
3618 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3619 (
3620 DwarfDie(
3621 DW_TAG.formal_parameter,
3622 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "char_die"),),
3623 ),
3624 ),
3625 ),
3626 *labeled_int_die,
3627 *labeled_char_die,
3628 )
3629 )
3630 self.assertIdentical(
3631 prog.type("TEST").type,
3632 prog.function_type(
3633 prog.int_type("int", 4, True),
3634 (TypeParameter(prog.int_type("char", 1, True)),),
3635 False,
3636 ),
3637 )
3638
3639 def test_function_named_parameter(self):
3640 # int foo(char c)
3641 prog = dwarf_program(
3642 wrap_test_type_dies(
3643 DwarfDie(
3644 DW_TAG.subroutine_type,
3645 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3646 (
3647 DwarfDie(
3648 DW_TAG.formal_parameter,
3649 (
3650 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "char_die"),
3651 DwarfAttrib(DW_AT.name, DW_FORM.string, "c"),
3652 ),
3653 ),
3654 ),
3655 ),
3656 *labeled_int_die,
3657 *labeled_char_die,
3658 )
3659 )
3660 self.assertIdentical(
3661 prog.type("TEST").type,
3662 prog.function_type(
3663 prog.int_type("int", 4, True),
3664 (TypeParameter(prog.int_type("char", 1, True), "c"),),
3665 False,
3666 ),
3667 )
3668
3669 def test_utf_chars(self):
3670 # char8_t foo(char16_t a, char32_t b)
3671 prog = dwarf_program(
3672 wrap_test_type_dies(
3673 DwarfDie(
3674 DW_TAG.subroutine_type,
3675 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "utf8_char_die"),),
3676 (
3677 DwarfDie(
3678 DW_TAG.formal_parameter,
3679 (
3680 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "utf16_char_die"),
3681 DwarfAttrib(DW_AT.name, DW_FORM.string, "a"),
3682 ),
3683 ),
3684 DwarfDie(
3685 DW_TAG.formal_parameter,
3686 (
3687 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "utf32_char_die"),
3688 DwarfAttrib(DW_AT.name, DW_FORM.string, "b"),
3689 ),
3690 ),
3691 ),
3692 ),
3693 DwarfLabel("utf8_char_die"),
3694 utf8_char_die,
3695 DwarfLabel("utf16_char_die"),
3696 utf16_char_die,
3697 DwarfLabel("utf32_char_die"),
3698 utf32_char_die,
3699 )
3700 )
3701 self.assertIdentical(
3702 prog.type("TEST").type,
3703 prog.function_type(
3704 prog.int_type("char8_t", 1, False),
3705 (
3706 TypeParameter(prog.int_type("char16_t", 2, False), "a"),
3707 TypeParameter(prog.int_type("char32_t", 4, False), "b"),
3708 ),
3709 False,
3710 ),
3711 )
3712
3713 def test_function_unspecified_parameters(self):
3714 # int foo()
3715 prog = dwarf_program(
3716 wrap_test_type_dies(
3717 DwarfDie(
3718 DW_TAG.subroutine_type,
3719 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3720 (DwarfDie(DW_TAG.unspecified_parameters, ()),),
3721 ),
3722 *labeled_int_die,
3723 )
3724 )
3725 self.assertIdentical(
3726 prog.type("TEST").type,
3727 prog.function_type(prog.int_type("int", 4, True), (), True),
3728 )
3729
3730 def test_function_variadic(self):
3731 # int foo(char, ...)
3732 prog = dwarf_program(
3733 wrap_test_type_dies(
3734 DwarfDie(
3735 DW_TAG.subroutine_type,
3736 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3737 (
3738 DwarfDie(
3739 DW_TAG.formal_parameter,
3740 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "char_die"),),
3741 ),
3742 DwarfDie(DW_TAG.unspecified_parameters, ()),
3743 ),
3744 ),
3745 *labeled_int_die,
3746 *labeled_char_die,
3747 )
3748 )
3749 self.assertIdentical(
3750 prog.type("TEST").type,
3751 prog.function_type(
3752 prog.int_type("int", 4, True),
3753 (TypeParameter(prog.int_type("char", 1, True)),),
3754 True,
3755 ),
3756 )
3757
3758 def test_function_incomplete_array_parameter(self):
3759 # void foo(int [])
3760 # Note that in C, this is equivalent to void foo(int *), so GCC and
3761 # Clang emit the DWARF for the latter.
3762 prog = dwarf_program(
3763 wrap_test_type_dies(
3764 DwarfDie(
3765 DW_TAG.subroutine_type,
3766 (),
3767 (
3768 DwarfDie(
3769 DW_TAG.formal_parameter,
3770 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),),
3771 ),
3772 ),
3773 ),
3774 DwarfLabel("array_die"),
3775 DwarfDie(
3776 DW_TAG.array_type,
3777 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3778 ),
3779 *labeled_int_die,
3780 )
3781 )
3782 self.assertIdentical(
3783 prog.type("TEST").type,
3784 prog.function_type(
3785 prog.void_type(),
3786 (TypeParameter(prog.array_type(prog.int_type("int", 4, True))),),
3787 False,
3788 ),
3789 )
3790
3791 def test_function_template(self):
3792 prog = dwarf_program(
3793 wrap_test_type_dies(
3794 DwarfDie(
3795 DW_TAG.subroutine_type,
3796 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3797 (
3798 DwarfDie(DW_TAG.unspecified_parameters, ()),
3799 DwarfDie(
3800 DW_TAG.template_type_parameter,
3801 (DwarfAttrib(DW_AT.name, DW_FORM.string, "T"),),
3802 ),
3803 DwarfDie(
3804 DW_TAG.template_value_parameter,
3805 (
3806 DwarfAttrib(
3807 DW_AT.type, DW_FORM.ref4, "unsigned_int_die"
3808 ),
3809 DwarfAttrib(DW_AT.name, DW_FORM.string, "N"),
3810 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 2),
3811 ),
3812 ),
3813 ),
3814 ),
3815 *labeled_int_die,
3816 *labeled_unsigned_int_die,
3817 )
3818 )
3819 self.assertIdentical(
3820 prog.type("TEST").type,
3821 prog.function_type(
3822 prog.int_type("int", 4, True),
3823 (),
3824 is_variadic=True,
3825 template_parameters=(
3826 TypeTemplateParameter(prog.void_type(), "T"),
3827 TypeTemplateParameter(
3828 Object(prog, prog.int_type("unsigned int", 4, False), 2), "N"
3829 ),
3830 ),
3831 ),
3832 )
3833
3834 def test_function_template_parameter_pack(self):
3835 prog = dwarf_program(
3836 wrap_test_type_dies(
3837 DwarfDie(
3838 DW_TAG.subroutine_type,
3839 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
3840 (
3841 DwarfDie(
3842 DW_TAG.GNU_template_parameter_pack,
3843 (DwarfAttrib(DW_AT.name, DW_FORM.string, "Params"),),
3844 (
3845 DwarfDie(
3846 DW_TAG.template_type_parameter,
3847 (
3848 DwarfAttrib(
3849 DW_AT.type, DW_FORM.ref4, "int_die"
3850 ),
3851 DwarfAttrib(DW_AT.name, DW_FORM.string, "T"),
3852 ),
3853 ),
3854 char_die, # Unexpected die - should be ignored
3855 DwarfDie(
3856 DW_TAG.template_value_parameter,
3857 (
3858 DwarfAttrib(
3859 DW_AT.type, DW_FORM.ref4, "unsigned_int_die"
3860 ),
3861 DwarfAttrib(DW_AT.name, DW_FORM.string, "N"),
3862 DwarfAttrib(
3863 DW_AT.const_value, DW_FORM.data1, 2
3864 ),
3865 ),
3866 ),
3867 ),
3868 ),
3869 ),
3870 ),
3871 *labeled_int_die,
3872 *labeled_unsigned_int_die,
3873 )
3874 )
3875 self.assertIdentical(
3876 prog.type("TEST").type,
3877 prog.function_type(
3878 prog.int_type("int", 4, True),
3879 (),
3880 is_variadic=False,
3881 template_parameters=(
3882 TypeTemplateParameter(prog.int_type("int", 4, True), "T"),
3883 TypeTemplateParameter(
3884 Object(prog, prog.int_type("unsigned int", 4, False), 2), "N"
3885 ),
3886 ),
3887 ),
3888 )
3889
3890 def test_language(self):
3891 for name, lang in DW_LANG.__members__.items():
3892 if re.fullmatch("C[0-9]*", name):
3893 prog = dwarf_program(wrap_test_type_dies(int_die), lang=lang)
3894 self.assertIdentical(
3895 prog.type("TEST").type,
3896 prog.int_type("int", 4, True, language=Language.C),
3897 )
3898 prog = dwarf_program(wrap_test_type_dies(int_die), lang=DW_LANG.BLISS)
3899 self.assertIdentical(
3900 prog.type("TEST").type,
3901 prog.int_type("int", 4, True, language=DEFAULT_LANGUAGE),
3902 )
3903
3904 def test_base_type_unit(self):
3905 for version in (4, 5):
3906 with self.subTest(version=version):
3907 prog = dwarf_program(
3908 (
3909 DwarfUnit(
3910 DW_UT.compile,
3911 DwarfDie(
3912 DW_TAG.compile_unit,
3913 (),
3914 (
3915 DwarfLabel("signature_die"),
3916 DwarfDie(
3917 DW_TAG.base_type,
3918 (
3919 DwarfAttrib(
3920 DW_AT.signature,
3921 DW_FORM.ref_sig8,
3922 0xDEADBEEF,
3923 ),
3924 ),
3925 ),
3926 DwarfDie(
3927 DW_TAG.typedef,
3928 (
3929 DwarfAttrib(
3930 DW_AT.name, DW_FORM.string, "TEST"
3931 ),
3932 DwarfAttrib(
3933 DW_AT.type,
3934 DW_FORM.ref4,
3935 "signature_die",
3936 ),
3937 ),
3938 ),
3939 ),
3940 ),
3941 ),
3942 DwarfUnit(
3943 DW_UT.type,
3944 DwarfDie(
3945 DW_TAG.type_unit,
3946 (),
3947 labeled_int_die,
3948 ),
3949 type_signature=0xDEADBEEF,
3950 type_offset="int_die",
3951 ),
3952 ),
3953 version=version,
3954 )
3955 self.assertIdentical(
3956 prog.type("TEST").type, prog.int_type("int", 4, True)
3957 )
3958 self.assertIdentical(prog.type("int"), prog.type("TEST").type)
3959
3960 def test_struct_type_unit(self):
3961 for version in (4, 5):
3962 with self.subTest(version=version):
3963 prog = dwarf_program(
3964 (
3965 DwarfUnit(
3966 DW_UT.compile,
3967 DwarfDie(
3968 DW_TAG.compile_unit,
3969 (),
3970 (
3971 DwarfLabel("signature_die"),
3972 DwarfDie(
3973 DW_TAG.structure_type,
3974 (
3975 DwarfAttrib(
3976 DW_AT.signature,
3977 DW_FORM.ref_sig8,
3978 0xDEADBEEF,
3979 ),
3980 ),
3981 ),
3982 DwarfDie(
3983 DW_TAG.typedef,
3984 (
3985 DwarfAttrib(
3986 DW_AT.name, DW_FORM.string, "TEST"
3987 ),
3988 DwarfAttrib(
3989 DW_AT.type,
3990 DW_FORM.ref4,
3991 "signature_die",
3992 ),
3993 ),
3994 ),
3995 ),
3996 ),
3997 ),
3998 DwarfUnit(
3999 DW_UT.type,
4000 DwarfDie(
4001 DW_TAG.type_unit,
4002 (),
4003 (
4004 DwarfLabel("struct_die"),
4005 DwarfDie(
4006 DW_TAG.structure_type,
4007 (
4008 DwarfAttrib(
4009 DW_AT.name, DW_FORM.string, "point"
4010 ),
4011 DwarfAttrib(
4012 DW_AT.byte_size, DW_FORM.data1, 8
4013 ),
4014 ),
4015 (
4016 DwarfDie(
4017 DW_TAG.member,
4018 (
4019 DwarfAttrib(
4020 DW_AT.name, DW_FORM.string, "x"
4021 ),
4022 DwarfAttrib(
4023 DW_AT.data_member_location,
4024 DW_FORM.data1,
4025 0,
4026 ),
4027 DwarfAttrib(
4028 DW_AT.type,
4029 DW_FORM.ref4,
4030 "int_die",
4031 ),
4032 ),
4033 ),
4034 DwarfDie(
4035 DW_TAG.member,
4036 (
4037 DwarfAttrib(
4038 DW_AT.name, DW_FORM.string, "y"
4039 ),
4040 DwarfAttrib(
4041 DW_AT.data_member_location,
4042 DW_FORM.data1,
4043 4,
4044 ),
4045 DwarfAttrib(
4046 DW_AT.type,
4047 DW_FORM.ref4,
4048 "int_die",
4049 ),
4050 ),
4051 ),
4052 ),
4053 ),
4054 *labeled_int_die,
4055 ),
4056 ),
4057 type_signature=0xDEADBEEF,
4058 type_offset="struct_die",
4059 ),
4060 ),
4061 version=version,
4062 )
4063
4064 self.assertIdentical(
4065 prog.type("TEST").type,
4066 prog.struct_type(
4067 "point",
4068 8,
4069 (
4070 TypeMember(prog.int_type("int", 4, True), "x", 0),
4071 TypeMember(prog.int_type("int", 4, True), "y", 32),
4072 ),
4073 ),
4074 )
4075 self.assertIdentical(prog.type("struct point"), prog.type("TEST").type)
4076
4077 def test_namespaces(self):
4078 def make_composite_die(tag: DW_TAG, name: str) -> DwarfDie:
4079 return DwarfDie(
4080 tag,
4081 (
4082 DwarfAttrib(DW_AT.name, DW_FORM.string, name),
4083 DwarfAttrib(
4084 DW_AT.byte_size,
4085 DW_FORM.data1,
4086 4 if tag == DW_TAG.union_type else 8,
4087 ),
4088 DwarfAttrib(DW_AT.decl_file, DW_FORM.udata, "foo.c"),
4089 ),
4090 (
4091 DwarfDie(
4092 DW_TAG.member,
4093 (
4094 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
4095 DwarfAttrib(
4096 DW_AT.data_member_location,
4097 DW_FORM.data1,
4098 0,
4099 ),
4100 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4101 ),
4102 ),
4103 DwarfDie(
4104 DW_TAG.member,
4105 (
4106 DwarfAttrib(DW_AT.name, DW_FORM.string, "y"),
4107 DwarfAttrib(
4108 DW_AT.data_member_location,
4109 DW_FORM.data1,
4110 0 if tag == DW_TAG.union_type else 4,
4111 ),
4112 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "float_die"),
4113 ),
4114 ),
4115 ),
4116 )
4117
4118 dies = (
4119 *labeled_int_die,
4120 *labeled_float_die,
4121 DwarfDie(
4122 DW_TAG.namespace,
4123 (DwarfAttrib(DW_AT.name, DW_FORM.string, "moho"),),
4124 (
4125 DwarfDie(
4126 DW_TAG.namespace,
4127 (DwarfAttrib(DW_AT.name, DW_FORM.string, "eve"),),
4128 (
4129 DwarfDie(
4130 DW_TAG.namespace,
4131 (DwarfAttrib(DW_AT.name, DW_FORM.string, "kerbin"),),
4132 (
4133 DwarfDie(
4134 DW_TAG.typedef,
4135 (
4136 DwarfAttrib(
4137 DW_AT.name,
4138 DW_FORM.string,
4139 "TEST_TYPEDEF",
4140 ),
4141 DwarfAttrib(
4142 DW_AT.type, DW_FORM.ref4, "int_die"
4143 ),
4144 ),
4145 ),
4146 make_composite_die(
4147 DW_TAG.structure_type, "TEST_STRUCT"
4148 ),
4149 make_composite_die(DW_TAG.class_type, "TEST_CLASS"),
4150 make_composite_die(DW_TAG.union_type, "TEST_UNION"),
4151 ),
4152 ),
4153 ),
4154 ),
4155 ),
4156 ),
4157 )
4158
4159 prog = dwarf_program(dies, lang=DW_LANG.C_plus_plus)
4160 # Language is not set automatically when there is no DIE for `main`
4161 prog.language = Language.CPP
4162
4163 self.assertIdentical(
4164 prog.type("moho::eve::kerbin::TEST_TYPEDEF"),
4165 prog.typedef_type("TEST_TYPEDEF", prog.int_type("int", 4, True)),
4166 )
4167 self.assertIdentical(
4168 prog.type("struct moho::eve::kerbin::TEST_STRUCT"),
4169 prog.struct_type(
4170 "TEST_STRUCT",
4171 8,
4172 (
4173 TypeMember(prog.int_type("int", 4, True), "x", 0),
4174 TypeMember(prog.float_type("float", 4), "y", 32),
4175 ),
4176 ),
4177 )
4178 self.assertIdentical(
4179 prog.type("class moho::eve::kerbin::TEST_CLASS"),
4180 prog.class_type(
4181 "TEST_CLASS",
4182 8,
4183 (
4184 TypeMember(prog.int_type("int", 4, True), "x", 0),
4185 TypeMember(prog.float_type("float", 4), "y", 32),
4186 ),
4187 ),
4188 )
4189 self.assertIdentical(
4190 prog.type("union moho::eve::kerbin::TEST_UNION"),
4191 prog.union_type(
4192 "TEST_UNION",
4193 4,
4194 (
4195 TypeMember(prog.int_type("int", 4, True), "x", 0),
4196 TypeMember(prog.float_type("float", 4), "y", 0),
4197 ),
4198 ),
4199 )
4200
4201 def test_explicit_global_namespace(self):
4202 prog = dwarf_program(
4203 (
4204 *labeled_int_die,
4205 DwarfDie(
4206 DW_TAG.typedef,
4207 (
4208 DwarfAttrib(
4209 DW_AT.name,
4210 DW_FORM.string,
4211 "TEST_TYPEDEF_GLOBAL",
4212 ),
4213 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4214 ),
4215 ),
4216 ),
4217 lang=DW_LANG.C_plus_plus,
4218 )
4219 # Language is not set automatically when there is no DIE for `main`
4220 prog.language = Language.CPP
4221
4222 self.assertIdentical(
4223 prog.type("TEST_TYPEDEF_GLOBAL"), prog.type("::TEST_TYPEDEF_GLOBAL")
4224 )
4225
4226 def test_template_in_namespace(self):
4227 dies = (
4228 *labeled_int_die,
4229 *labeled_unsigned_int_die,
4230 DwarfDie(
4231 DW_TAG.namespace,
4232 (DwarfAttrib(DW_AT.name, DW_FORM.string, "containers"),),
4233 (
4234 DwarfLabel("typedef_die"),
4235 DwarfDie(
4236 DW_TAG.typedef,
4237 (
4238 DwarfAttrib(
4239 DW_AT.name,
4240 DW_FORM.string,
4241 "MyTypedef",
4242 ),
4243 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "unsigned_int_die"),
4244 ),
4245 ),
4246 DwarfDie(
4247 DW_TAG.class_type,
4248 (
4249 DwarfAttrib(
4250 DW_AT.name,
4251 DW_FORM.string,
4252 "Pair<int, containers::MyTypedef>",
4253 ),
4254 DwarfAttrib(
4255 DW_AT.byte_size,
4256 DW_FORM.data1,
4257 8,
4258 ),
4259 ),
4260 (
4261 DwarfDie(
4262 DW_TAG.template_type_parameter,
4263 (
4264 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4265 DwarfAttrib(DW_AT.name, DW_FORM.string, "T"),
4266 ),
4267 ),
4268 DwarfDie(
4269 DW_TAG.template_type_parameter,
4270 (
4271 DwarfAttrib(
4272 DW_AT.type, DW_FORM.ref4, "typedef_die"
4273 ),
4274 DwarfAttrib(DW_AT.name, DW_FORM.string, "V"),
4275 ),
4276 ),
4277 DwarfDie(
4278 DW_TAG.member,
4279 (
4280 DwarfAttrib(DW_AT.name, DW_FORM.string, "first"),
4281 DwarfAttrib(
4282 DW_AT.data_member_location,
4283 DW_FORM.data1,
4284 0,
4285 ),
4286 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4287 ),
4288 ),
4289 DwarfDie(
4290 DW_TAG.member,
4291 (
4292 DwarfAttrib(DW_AT.name, DW_FORM.string, "second"),
4293 DwarfAttrib(
4294 DW_AT.data_member_location,
4295 DW_FORM.data1,
4296 4,
4297 ),
4298 DwarfAttrib(
4299 DW_AT.type, DW_FORM.ref4, "typedef_die"
4300 ),
4301 ),
4302 ),
4303 ),
4304 ),
4305 ),
4306 ),
4307 )
4308
4309 prog = dwarf_program(dies, lang=DW_LANG.C_plus_plus)
4310 # Language is not set automatically when there is no DIE for `main`
4311 prog.language = Language.CPP
4312
4313 self.assertIdentical(
4314 prog.type("class containers::Pair<int, containers::MyTypedef>"),
4315 prog.class_type(
4316 "Pair<int, containers::MyTypedef>",
4317 8,
4318 members=(
4319 TypeMember(prog.int_type("int", 4, True), "first", 0),
4320 TypeMember(prog.type("containers::MyTypedef"), "second", 32),
4321 ),
4322 template_parameters=(
4323 TypeTemplateParameter(prog.int_type("int", 4, True), "T"),
4324 TypeTemplateParameter(prog.type("containers::MyTypedef"), "V"),
4325 ),
4326 ),
4327 )
4328
4329 def test_cpp_compound_type_specifiers(self):
4330 for keyword, tag in (
4331 ("struct", DW_TAG.structure_type),
4332 ("union", DW_TAG.union_type),
4333 ("class", DW_TAG.class_type),
4334 ):
4335 with self.subTest(keyword=keyword):
4336 prog = dwarf_program(
4337 (
4338 DwarfDie(
4339 tag,
4340 (
4341 DwarfAttrib(DW_AT.name, DW_FORM.string, "Foo"),
4342 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
4343 ),
4344 (
4345 DwarfDie(
4346 DW_TAG.member,
4347 (
4348 DwarfAttrib(DW_AT.name, DW_FORM.string, "bar"),
4349 DwarfAttrib(
4350 DW_AT.data_member_location, DW_FORM.data1, 0
4351 ),
4352 DwarfAttrib(
4353 DW_AT.type, DW_FORM.ref4, "int_die"
4354 ),
4355 ),
4356 ),
4357 ),
4358 ),
4359 *labeled_int_die,
4360 ),
4361 )
4362 self.assertRaises(LookupError, prog.type, "Foo")
4363 prog.language = Language.CPP
4364 self.assertIdentical(prog.type("Foo"), prog.type(keyword + " Foo"))
4365
4366 def test_cpp_enum_specifier(self):
4367 prog = dwarf_program(
4368 (
4369 DwarfDie(
4370 DW_TAG.enumeration_type,
4371 (
4372 DwarfAttrib(DW_AT.name, DW_FORM.string, "Foo"),
4373 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4374 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
4375 ),
4376 (
4377 DwarfDie(
4378 DW_TAG.enumerator,
4379 (
4380 DwarfAttrib(DW_AT.name, DW_FORM.string, "BAR"),
4381 DwarfAttrib(DW_AT.const_value, DW_FORM.data2, 1337),
4382 ),
4383 ),
4384 ),
4385 ),
4386 *labeled_int_die,
4387 ),
4388 )
4389 self.assertRaises(LookupError, prog.type, "Foo")
4390 prog.language = Language.CPP
4391 self.assertIdentical(prog.type("Foo"), prog.type("enum Foo"))
4392
4393 def test_cpp_typedef(self):
4394 prog = dwarf_program(
4395 (
4396 DwarfDie(
4397 DW_TAG.typedef,
4398 (
4399 DwarfAttrib(DW_AT.name, DW_FORM.string, "INT"),
4400 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4401 ),
4402 ),
4403 *labeled_int_die,
4404 DwarfDie(
4405 DW_TAG.subprogram,
4406 (DwarfAttrib(DW_AT.name, DW_FORM.string, "main"),),
4407 ),
4408 ),
4409 lang=DW_LANG.C_plus_plus,
4410 )
4411 self.assertIdentical(
4412 prog.type("INT"),
4413 prog.typedef_type("INT", prog.int_type("int", 4, True)),
4414 )
4415
4416 def test_alignment(self):
4417 prog = dwarf_program(
4418 wrap_test_type_dies(
4419 DwarfDie(
4420 DW_TAG.structure_type,
4421 (
4422 DwarfAttrib(DW_AT.name, DW_FORM.string, "cacheline"),
4423 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 64),
4424 DwarfAttrib(DW_AT.alignment, DW_FORM.data1, 64),
4425 ),
4426 (
4427 DwarfDie(
4428 DW_TAG.member,
4429 (
4430 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
4431 DwarfAttrib(
4432 DW_AT.data_member_location, DW_FORM.data1, 0
4433 ),
4434 DwarfAttrib(DW_AT.alignment, DW_FORM.data1, 64),
4435 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4436 ),
4437 ),
4438 ),
4439 ),
4440 *labeled_int_die,
4441 )
4442 )
4443 self.assertEqual(alignof(prog.type("TEST")), 64)
4444
4445
4446 class TestObjects(TestCase):
4447 def test_constant_signed_enum(self):
4448 prog = dwarf_program(
4449 (
4450 *labeled_int_die,
4451 DwarfDie(
4452 DW_TAG.enumeration_type,
4453 (
4454 DwarfAttrib(DW_AT.name, DW_FORM.string, "color"),
4455 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4456 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
4457 ),
4458 (
4459 DwarfDie(
4460 DW_TAG.enumerator,
4461 (
4462 DwarfAttrib(DW_AT.name, DW_FORM.string, "RED"),
4463 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 0),
4464 ),
4465 ),
4466 DwarfDie(
4467 DW_TAG.enumerator,
4468 (
4469 DwarfAttrib(DW_AT.name, DW_FORM.string, "GREEN"),
4470 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 1),
4471 ),
4472 ),
4473 DwarfDie(
4474 DW_TAG.enumerator,
4475 (
4476 DwarfAttrib(DW_AT.name, DW_FORM.string, "BLUE"),
4477 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 2),
4478 ),
4479 ),
4480 ),
4481 ),
4482 DwarfDie(
4483 DW_TAG.variable,
4484 (
4485 DwarfAttrib(DW_AT.name, DW_FORM.string, "RED"),
4486 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4487 DwarfAttrib(
4488 DW_AT.location,
4489 DW_FORM.exprloc,
4490 b"\x03\x04\x03\x02\x01\xff\xff\xff\xff",
4491 ),
4492 ),
4493 ),
4494 )
4495 )
4496 type_ = prog.enum_type(
4497 "color",
4498 prog.int_type("int", 4, True),
4499 (
4500 TypeEnumerator("RED", 0),
4501 TypeEnumerator("GREEN", 1),
4502 TypeEnumerator("BLUE", 2),
4503 ),
4504 )
4505 self.assertIdentical(
4506 prog.object("RED", FindObjectFlags.CONSTANT), Object(prog, type_, value=0)
4507 )
4508 self.assertIdentical(prog["BLUE"], Object(prog, type_, value=2))
4509
4510 def test_constant_unsigned_enum(self):
4511 prog = dwarf_program(
4512 (
4513 *labeled_unsigned_int_die,
4514 DwarfDie(
4515 DW_TAG.enumeration_type,
4516 (
4517 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "unsigned_int_die"),
4518 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
4519 ),
4520 (
4521 DwarfDie(
4522 DW_TAG.enumerator,
4523 (
4524 DwarfAttrib(DW_AT.name, DW_FORM.string, "FLAG"),
4525 DwarfAttrib(DW_AT.const_value, DW_FORM.data2, 1 << 12),
4526 ),
4527 ),
4528 ),
4529 ),
4530 )
4531 )
4532 self.assertIdentical(
4533 prog["FLAG"],
4534 Object(
4535 prog,
4536 prog.enum_type(
4537 None,
4538 prog.int_type("unsigned int", 4, False),
4539 (TypeEnumerator("FLAG", 4096),),
4540 ),
4541 4096,
4542 ),
4543 )
4544
4545 def test_function(self):
4546 prog = dwarf_program(
4547 wrap_test_type_dies(
4548 *labeled_int_die,
4549 DwarfDie(
4550 DW_TAG.subprogram,
4551 (
4552 DwarfAttrib(DW_AT.name, DW_FORM.string, "abs"),
4553 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4554 DwarfAttrib(DW_AT.low_pc, DW_FORM.addr, 0x7FC3EB9B1C30),
4555 ),
4556 (
4557 DwarfDie(
4558 DW_TAG.formal_parameter,
4559 (
4560 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
4561 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4562 ),
4563 ),
4564 ),
4565 ),
4566 )
4567 )
4568 self.assertIdentical(
4569 prog["abs"],
4570 Object(
4571 prog,
4572 prog.function_type(
4573 prog.int_type("int", 4, True),
4574 (TypeParameter(prog.int_type("int", 4, True), "x"),),
4575 False,
4576 ),
4577 address=0x7FC3EB9B1C30,
4578 ),
4579 )
4580 self.assertIdentical(prog.object("abs", FindObjectFlags.FUNCTION), prog["abs"])
4581 self.assertRaisesRegex(
4582 ObjectNotFoundError,
4583 "could not find variable",
4584 prog.object,
4585 "abs",
4586 FindObjectFlags.VARIABLE,
4587 )
4588
4589 def test_function_no_address(self):
4590 prog = dwarf_program(
4591 wrap_test_type_dies(
4592 DwarfDie(
4593 DW_TAG.subprogram,
4594 (DwarfAttrib(DW_AT.name, DW_FORM.string, "abort"),),
4595 )
4596 )
4597 )
4598 self.assertIdentical(
4599 prog.object("abort"),
4600 Object(
4601 prog,
4602 prog.function_type(prog.void_type(), ()),
4603 absence_reason=AbsenceReason.OPTIMIZED_OUT,
4604 ),
4605 )
4606
4607 def test_function_concrete_out_of_line_instance(self):
4608 prog = dwarf_program(
4609 (
4610 *labeled_int_die,
4611 DwarfLabel("abstract_instance_root"),
4612 DwarfDie(
4613 DW_TAG.subprogram,
4614 (
4615 DwarfAttrib(DW_AT.name, DW_FORM.string, "abs"),
4616 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4617 DwarfAttrib(DW_AT.inline, DW_FORM.data1, DW_INL.inlined),
4618 ),
4619 (
4620 DwarfLabel("abstract_instance_parameter"),
4621 DwarfDie(
4622 DW_TAG.formal_parameter,
4623 (
4624 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
4625 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4626 ),
4627 ),
4628 ),
4629 ),
4630 DwarfDie(
4631 DW_TAG.subprogram,
4632 (
4633 DwarfAttrib(
4634 DW_AT.abstract_origin,
4635 DW_FORM.ref4,
4636 "abstract_instance_root",
4637 ),
4638 DwarfAttrib(DW_AT.low_pc, DW_FORM.addr, 0x7FC3EB9B1C30),
4639 ),
4640 (
4641 DwarfDie(
4642 DW_TAG.formal_parameter,
4643 (
4644 DwarfAttrib(
4645 DW_AT.abstract_origin,
4646 DW_FORM.ref4,
4647 "abstract_instance_parameter",
4648 ),
4649 ),
4650 ),
4651 ),
4652 ),
4653 )
4654 )
4655 self.assertIdentical(
4656 prog["abs"],
4657 Object(
4658 prog,
4659 prog.function_type(
4660 prog.int_type("int", 4, True),
4661 (TypeParameter(prog.int_type("int", 4, True), "x"),),
4662 False,
4663 ),
4664 address=0x7FC3EB9B1C30,
4665 ),
4666 )
4667
4668 def test_member_function_specification(self):
4669 prog = dwarf_program(
4670 (
4671 DwarfDie(
4672 DW_TAG.class_type,
4673 (
4674 DwarfAttrib(DW_AT.name, DW_FORM.string, "Foo"),
4675 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 0),
4676 ),
4677 (
4678 DwarfLabel("declaration"),
4679 DwarfDie(
4680 DW_TAG.subprogram,
4681 (
4682 DwarfAttrib(DW_AT.name, DW_FORM.string, "bar"),
4683 DwarfAttrib(
4684 DW_AT.declaration, DW_FORM.flag_present, True
4685 ),
4686 ),
4687 ),
4688 ),
4689 ),
4690 # This is how GCC and Clang do it: the declaration is in the
4691 # correct scope, and the definition is at the top level.
4692 DwarfDie(
4693 DW_TAG.subprogram,
4694 (
4695 DwarfAttrib(DW_AT.specification, DW_FORM.ref4, "declaration"),
4696 DwarfAttrib(DW_AT.low_pc, DW_FORM.addr, 0x7FC3EB9B1C30),
4697 ),
4698 ),
4699 DwarfDie(
4700 DW_TAG.subprogram,
4701 (DwarfAttrib(DW_AT.name, DW_FORM.string, "main"),),
4702 ),
4703 ),
4704 lang=DW_LANG.C_plus_plus,
4705 )
4706 self.assertIdentical(
4707 prog["Foo::bar"],
4708 Object(
4709 prog,
4710 prog.function_type(
4711 prog.void_type(),
4712 (),
4713 ),
4714 address=0x7FC3EB9B1C30,
4715 ),
4716 )
4717 self.assertNotIn("bar", prog)
4718
4719 def test_member_function_concrete_out_of_line_instance(self):
4720 prog = dwarf_program(
4721 (
4722 DwarfDie(
4723 DW_TAG.class_type,
4724 (
4725 DwarfAttrib(DW_AT.name, DW_FORM.string, "Foo"),
4726 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 0),
4727 ),
4728 (
4729 DwarfLabel("declaration"),
4730 DwarfDie(
4731 DW_TAG.subprogram,
4732 (
4733 DwarfAttrib(DW_AT.name, DW_FORM.string, "bar"),
4734 DwarfAttrib(
4735 DW_AT.declaration, DW_FORM.flag_present, True
4736 ),
4737 ),
4738 ),
4739 ),
4740 ),
4741 DwarfLabel("abstract_instance_root"),
4742 DwarfDie(
4743 DW_TAG.subprogram,
4744 (
4745 DwarfAttrib(DW_AT.specification, DW_FORM.ref4, "declaration"),
4746 DwarfAttrib(
4747 DW_AT.inline, DW_FORM.data1, DW_INL.declared_inlined
4748 ),
4749 ),
4750 ),
4751 DwarfDie(
4752 DW_TAG.subprogram,
4753 (
4754 DwarfAttrib(
4755 DW_AT.specification, DW_FORM.ref4, "abstract_instance_root"
4756 ),
4757 DwarfAttrib(DW_AT.low_pc, DW_FORM.addr, 0x7FC3EB9B1C30),
4758 ),
4759 ),
4760 DwarfDie(
4761 DW_TAG.subprogram,
4762 (DwarfAttrib(DW_AT.name, DW_FORM.string, "main"),),
4763 ),
4764 ),
4765 lang=DW_LANG.C_plus_plus,
4766 )
4767 self.assertIdentical(
4768 prog["Foo::bar"],
4769 Object(
4770 prog,
4771 prog.function_type(
4772 prog.void_type(),
4773 (),
4774 ),
4775 address=0x7FC3EB9B1C30,
4776 ),
4777 )
4778 self.assertNotIn("bar", prog)
4779
4780 def test_variable(self):
4781 prog = dwarf_program(
4782 wrap_test_type_dies(
4783 *labeled_int_die,
4784 DwarfDie(
4785 DW_TAG.variable,
4786 (
4787 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
4788 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4789 DwarfAttrib(
4790 DW_AT.location,
4791 DW_FORM.exprloc,
4792 b"\x03\x04\x03\x02\x01\xff\xff\xff\xff",
4793 ),
4794 ),
4795 ),
4796 )
4797 )
4798 self.assertIdentical(
4799 prog["x"],
4800 Object(prog, prog.int_type("int", 4, True), address=0xFFFFFFFF01020304),
4801 )
4802 self.assertIdentical(prog.object("x", FindObjectFlags.VARIABLE), prog["x"])
4803 self.assertRaisesRegex(
4804 ObjectNotFoundError,
4805 "could not find constant",
4806 prog.object,
4807 "x",
4808 FindObjectFlags.CONSTANT,
4809 )
4810
4811 def test_zero_size_variable(self):
4812 prog = dwarf_program(
4813 wrap_test_type_dies(
4814 *labeled_int_die,
4815 DwarfLabel("array_die"),
4816 DwarfDie(
4817 DW_TAG.array_type,
4818 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
4819 ),
4820 DwarfDie(
4821 DW_TAG.variable,
4822 (
4823 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
4824 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
4825 DwarfAttrib(
4826 DW_AT.location,
4827 DW_FORM.exprloc,
4828 b"\x03\x04\x03\x02\x01\xff\xff\xff\xff",
4829 ),
4830 ),
4831 ),
4832 )
4833 )
4834 self.assertIdentical(
4835 prog["x"],
4836 Object(
4837 prog,
4838 prog.array_type(prog.int_type("int", 4, True)),
4839 address=0xFFFFFFFF01020304,
4840 ),
4841 )
4842
4843 def test_variable_no_address(self):
4844 prog = dwarf_program(
4845 wrap_test_type_dies(
4846 *labeled_int_die,
4847 DwarfDie(
4848 DW_TAG.variable,
4849 (
4850 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
4851 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4852 ),
4853 ),
4854 )
4855 )
4856 self.assertIdentical(
4857 prog.object("x"),
4858 Object(prog, "int", absence_reason=AbsenceReason.OPTIMIZED_OUT),
4859 )
4860
4861 def test_variable_expr_empty(self):
4862 prog = dwarf_program(
4863 wrap_test_type_dies(
4864 *labeled_int_die,
4865 DwarfDie(
4866 DW_TAG.variable,
4867 (
4868 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
4869 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4870 DwarfAttrib(DW_AT.location, DW_FORM.exprloc, b""),
4871 ),
4872 ),
4873 )
4874 )
4875 self.assertIdentical(
4876 prog.object("x"),
4877 Object(prog, "int", absence_reason=AbsenceReason.OPTIMIZED_OUT),
4878 )
4879
4880 def test_variable_expr_bit_piece(self):
4881 prog = dwarf_program(
4882 wrap_test_type_dies(
4883 *labeled_int_die,
4884 DwarfDie(
4885 DW_TAG.variable,
4886 (
4887 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
4888 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4889 DwarfAttrib(
4890 DW_AT.location,
4891 DW_FORM.exprloc,
4892 assembler.assemble(
4893 assembler.U8(DW_OP.addr),
4894 assembler.U64(0xFFFFFFFF01020304),
4895 assembler.U8(DW_OP.bit_piece),
4896 assembler.ULEB128(32),
4897 assembler.ULEB128(4),
4898 ),
4899 ),
4900 ),
4901 ),
4902 ),
4903 )
4904 self.assertIdentical(
4905 prog.object("x"),
4906 Object(prog, "int", address=0xFFFFFFFF01020304, bit_offset=4),
4907 )
4908
4909 def test_variable_expr_implicit_value(self):
4910 for little_endian in (True, False):
4911 with self.subTest(little_endian=little_endian):
4912 prog = dwarf_program(
4913 wrap_test_type_dies(
4914 *labeled_int_die,
4915 DwarfDie(
4916 DW_TAG.variable,
4917 (
4918 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
4919 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4920 DwarfAttrib(
4921 DW_AT.location,
4922 DW_FORM.exprloc,
4923 assembler.assemble(
4924 assembler.U8(DW_OP.implicit_value),
4925 assembler.ULEB128(4),
4926 assembler.U32(0x12345678),
4927 little_endian=little_endian,
4928 ),
4929 ),
4930 ),
4931 ),
4932 ),
4933 little_endian=little_endian,
4934 )
4935 self.assertIdentical(prog.object("x"), Object(prog, "int", 0x12345678))
4936
4937 def test_variable_expr_implicit_value_pieces(self):
4938 for little_endian in (True, False):
4939 with self.subTest(little_endian=little_endian):
4940 prog = dwarf_program(
4941 wrap_test_type_dies(
4942 *labeled_int_die,
4943 DwarfDie(
4944 DW_TAG.variable,
4945 (
4946 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
4947 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4948 DwarfAttrib(
4949 DW_AT.location,
4950 DW_FORM.exprloc,
4951 assembler.assemble(
4952 assembler.U8(DW_OP.implicit_value),
4953 assembler.ULEB128(2),
4954 assembler.U16(
4955 0x5678 if little_endian else 0x1234
4956 ),
4957 assembler.U8(DW_OP.piece),
4958 assembler.ULEB128(2),
4959 assembler.U8(DW_OP.implicit_value),
4960 assembler.ULEB128(2),
4961 assembler.U16(
4962 0x1234 if little_endian else 0x5678
4963 ),
4964 assembler.U8(DW_OP.piece),
4965 assembler.ULEB128(2),
4966 little_endian=little_endian,
4967 ),
4968 ),
4969 ),
4970 ),
4971 ),
4972 little_endian=little_endian,
4973 )
4974 self.assertIdentical(prog.object("x"), Object(prog, "int", 0x12345678))
4975
4976 def test_variable_expr_implicit_value_pieces_too_large(self):
4977 for little_endian in (True, False):
4978 with self.subTest(little_endian=little_endian):
4979 prog = dwarf_program(
4980 wrap_test_type_dies(
4981 *labeled_int_die,
4982 DwarfDie(
4983 DW_TAG.variable,
4984 (
4985 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
4986 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
4987 DwarfAttrib(
4988 DW_AT.location,
4989 DW_FORM.exprloc,
4990 assembler.assemble(
4991 assembler.U8(DW_OP.implicit_value),
4992 assembler.ULEB128(2),
4993 assembler.U16(
4994 0x5678 if little_endian else 0x1234
4995 ),
4996 assembler.U8(DW_OP.piece),
4997 assembler.ULEB128(2),
4998 assembler.U8(DW_OP.implicit_value),
4999 assembler.ULEB128(4),
5000 assembler.U32(
5001 0x1234 if little_endian else 0x5678
5002 ),
5003 assembler.U8(DW_OP.piece),
5004 # Piece size is larger than remaining size of object.
5005 assembler.ULEB128(4),
5006 assembler.U8(DW_OP.implicit_value),
5007 assembler.ULEB128(4),
5008 assembler.U32(0),
5009 # There is nothing remaining in the object.
5010 assembler.U8(DW_OP.piece),
5011 assembler.ULEB128(4),
5012 little_endian=little_endian,
5013 ),
5014 ),
5015 ),
5016 ),
5017 ),
5018 little_endian=little_endian,
5019 )
5020 self.assertIdentical(prog.object("x"), Object(prog, "int", 0x12345678))
5021
5022 def test_variable_expr_implicit_value_too_small(self):
5023 for little_endian in (True, False):
5024 with self.subTest(little_endian=little_endian):
5025 prog = dwarf_program(
5026 wrap_test_type_dies(
5027 *labeled_int_die,
5028 DwarfDie(
5029 DW_TAG.variable,
5030 (
5031 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5032 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5033 DwarfAttrib(
5034 DW_AT.location,
5035 DW_FORM.exprloc,
5036 assembler.assemble(
5037 assembler.U8(DW_OP.implicit_value),
5038 assembler.ULEB128(1),
5039 assembler.U8(0x99),
5040 little_endian=little_endian,
5041 ),
5042 ),
5043 ),
5044 ),
5045 ),
5046 little_endian=little_endian,
5047 )
5048 self.assertIdentical(prog.object("x"), Object(prog, "int", 0x99))
5049
5050 def test_variable_expr_implicit_value_bit_pieces(self):
5051 for little_endian in (True, False):
5052 with self.subTest(little_endian=little_endian):
5053 prog = dwarf_program(
5054 wrap_test_type_dies(
5055 *labeled_int_die,
5056 DwarfDie(
5057 DW_TAG.variable,
5058 (
5059 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5060 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5061 DwarfAttrib(
5062 DW_AT.location,
5063 DW_FORM.exprloc,
5064 assembler.assemble(
5065 assembler.U8(DW_OP.implicit_value),
5066 assembler.ULEB128(1),
5067 assembler.U8(0x8F if little_endian else 0x1F),
5068 assembler.U8(DW_OP.bit_piece),
5069 assembler.ULEB128(4),
5070 assembler.ULEB128(4),
5071 assembler.U8(DW_OP.implicit_value),
5072 assembler.ULEB128(4),
5073 assembler.U32(
5074 0x1234567 if little_endian else 0x2345678
5075 ),
5076 assembler.U8(DW_OP.bit_piece),
5077 assembler.ULEB128(28),
5078 assembler.ULEB128(0),
5079 little_endian=little_endian,
5080 ),
5081 ),
5082 ),
5083 ),
5084 ),
5085 little_endian=little_endian,
5086 )
5087 self.assertIdentical(prog.object("x"), Object(prog, "int", 0x12345678))
5088
5089 def test_variable_expr_implicit_value_piece_empty(self):
5090 prog = dwarf_program(
5091 wrap_test_type_dies(
5092 *labeled_int_die,
5093 DwarfDie(
5094 DW_TAG.variable,
5095 (
5096 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5097 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5098 DwarfAttrib(
5099 DW_AT.location,
5100 DW_FORM.exprloc,
5101 assembler.assemble(
5102 assembler.U8(DW_OP.implicit_value),
5103 assembler.ULEB128(2),
5104 assembler.U16(0),
5105 assembler.U8(DW_OP.piece),
5106 assembler.ULEB128(2),
5107 assembler.U8(DW_OP.piece),
5108 assembler.ULEB128(2),
5109 ),
5110 ),
5111 ),
5112 ),
5113 ),
5114 )
5115 self.assertIdentical(
5116 prog.object("x"),
5117 Object(prog, "int", absence_reason=AbsenceReason.OPTIMIZED_OUT),
5118 )
5119
5120 def test_variable_expr_stack_value(self):
5121 for little_endian in (True, False):
5122 with self.subTest(little_endian=little_endian):
5123 prog = dwarf_program(
5124 wrap_test_type_dies(
5125 *labeled_int_die,
5126 DwarfDie(
5127 DW_TAG.variable,
5128 (
5129 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5130 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5131 DwarfAttrib(
5132 DW_AT.location,
5133 DW_FORM.exprloc,
5134 assembler.assemble(
5135 assembler.U8(DW_OP.lit31),
5136 assembler.U8(DW_OP.stack_value),
5137 little_endian=little_endian,
5138 ),
5139 ),
5140 ),
5141 ),
5142 ),
5143 little_endian=little_endian,
5144 )
5145 self.assertIdentical(prog.object("x"), Object(prog, "int", 31))
5146
5147 def test_variable_expr_stack_value_pieces(self):
5148 for little_endian in (True, False):
5149 with self.subTest(little_endian=little_endian):
5150 prog = dwarf_program(
5151 wrap_test_type_dies(
5152 *labeled_int_die,
5153 DwarfDie(
5154 DW_TAG.variable,
5155 (
5156 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5157 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5158 DwarfAttrib(
5159 DW_AT.location,
5160 DW_FORM.exprloc,
5161 assembler.assemble(
5162 assembler.U8(
5163 DW_OP.lit2 if little_endian else DW_OP.lit1
5164 ),
5165 assembler.U8(DW_OP.stack_value),
5166 assembler.U8(DW_OP.piece),
5167 assembler.ULEB128(3 if little_endian else 1),
5168 assembler.U8(
5169 DW_OP.lit1 if little_endian else DW_OP.lit2
5170 ),
5171 assembler.U8(DW_OP.stack_value),
5172 assembler.U8(DW_OP.piece),
5173 assembler.ULEB128(1 if little_endian else 3),
5174 little_endian=little_endian,
5175 ),
5176 ),
5177 ),
5178 ),
5179 ),
5180 little_endian=little_endian,
5181 )
5182 self.assertIdentical(prog.object("x"), Object(prog, "int", 0x1000002))
5183
5184 def test_variable_expr_stack_value_bit_pieces(self):
5185 for little_endian in (True, False):
5186 with self.subTest(little_endian=little_endian):
5187 prog = dwarf_program(
5188 wrap_test_type_dies(
5189 *labeled_int_die,
5190 DwarfDie(
5191 DW_TAG.variable,
5192 (
5193 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5194 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5195 DwarfAttrib(
5196 DW_AT.location,
5197 DW_FORM.exprloc,
5198 assembler.assemble(
5199 assembler.U8(
5200 DW_OP.lit2 if little_endian else DW_OP.lit31
5201 ),
5202 assembler.U8(DW_OP.stack_value),
5203 assembler.U8(DW_OP.bit_piece),
5204 assembler.ULEB128(4 if little_endian else 28),
5205 assembler.ULEB128(0 if little_endian else 4),
5206 assembler.U8(
5207 DW_OP.lit31 if little_endian else DW_OP.lit2
5208 ),
5209 assembler.U8(DW_OP.stack_value),
5210 assembler.U8(DW_OP.bit_piece),
5211 assembler.ULEB128(28 if little_endian else 4),
5212 assembler.ULEB128(4 if little_endian else 0),
5213 little_endian=little_endian,
5214 ),
5215 ),
5216 ),
5217 ),
5218 ),
5219 little_endian=little_endian,
5220 )
5221 self.assertIdentical(prog.object("x"), Object(prog, "int", 0x12))
5222
5223 def test_variable_expr_stack_value_piece_empty(self):
5224 prog = dwarf_program(
5225 wrap_test_type_dies(
5226 *labeled_int_die,
5227 DwarfDie(
5228 DW_TAG.variable,
5229 (
5230 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5231 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5232 DwarfAttrib(
5233 DW_AT.location,
5234 DW_FORM.exprloc,
5235 assembler.assemble(
5236 assembler.U8(DW_OP.lit1),
5237 assembler.U8(DW_OP.stack_value),
5238 assembler.U8(DW_OP.piece),
5239 assembler.ULEB128(2),
5240 assembler.U8(DW_OP.piece),
5241 assembler.ULEB128(2),
5242 ),
5243 ),
5244 ),
5245 ),
5246 ),
5247 )
5248 self.assertIdentical(
5249 prog.object("x"),
5250 Object(prog, "int", absence_reason=AbsenceReason.OPTIMIZED_OUT),
5251 )
5252
5253 def test_variable_expr_contiguous_piece_addresses(self):
5254 prog = dwarf_program(
5255 wrap_test_type_dies(
5256 *labeled_int_die,
5257 DwarfDie(
5258 DW_TAG.variable,
5259 (
5260 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5261 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5262 DwarfAttrib(
5263 DW_AT.location,
5264 DW_FORM.exprloc,
5265 assembler.assemble(
5266 assembler.U8(DW_OP.addr),
5267 assembler.U64(0xFFFF0000),
5268 assembler.U8(DW_OP.piece),
5269 assembler.ULEB128(2),
5270 assembler.U8(DW_OP.addr),
5271 assembler.U64(0xFFFF0002),
5272 assembler.U8(DW_OP.piece),
5273 assembler.ULEB128(2),
5274 ),
5275 ),
5276 ),
5277 ),
5278 ),
5279 )
5280 self.assertIdentical(prog.object("x"), Object(prog, "int", address=0xFFFF0000))
5281
5282 def test_variable_expr_contiguous_bit_piece_addresses(self):
5283 for bit_offset in (0, 1):
5284 with self.subTest(bit_offset=bit_offset):
5285 prog = dwarf_program(
5286 wrap_test_type_dies(
5287 *labeled_int_die,
5288 DwarfDie(
5289 DW_TAG.variable,
5290 (
5291 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5292 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5293 DwarfAttrib(
5294 DW_AT.location,
5295 DW_FORM.exprloc,
5296 assembler.assemble(
5297 assembler.U8(DW_OP.addr),
5298 assembler.U64(0xFFFF0000),
5299 assembler.U8(DW_OP.bit_piece),
5300 assembler.ULEB128(10),
5301 assembler.ULEB128(bit_offset),
5302 assembler.U8(DW_OP.addr),
5303 assembler.U64(0xFFFF0001),
5304 assembler.U8(DW_OP.bit_piece),
5305 assembler.ULEB128(22),
5306 assembler.ULEB128(bit_offset + 2),
5307 ),
5308 ),
5309 ),
5310 ),
5311 ),
5312 )
5313 self.assertIdentical(
5314 prog.object("x"),
5315 Object(prog, "int", address=0xFFFF0000, bit_offset=bit_offset),
5316 )
5317
5318 def test_variable_expr_non_contiguous_piece_addresses(self):
5319 for little_endian in (True, False):
5320 with self.subTest(little_endian=little_endian):
5321 prog = dwarf_program(
5322 wrap_test_type_dies(
5323 *labeled_int_die,
5324 DwarfDie(
5325 DW_TAG.variable,
5326 (
5327 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5328 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5329 DwarfAttrib(
5330 DW_AT.location,
5331 DW_FORM.exprloc,
5332 assembler.assemble(
5333 assembler.U8(DW_OP.addr),
5334 assembler.U64(0xFFFF0002),
5335 assembler.U8(DW_OP.piece),
5336 assembler.ULEB128(2),
5337 assembler.U8(DW_OP.addr),
5338 assembler.U64(0xFFFF0000),
5339 assembler.U8(DW_OP.piece),
5340 assembler.ULEB128(2),
5341 little_endian=little_endian,
5342 ),
5343 ),
5344 ),
5345 ),
5346 ),
5347 little_endian=little_endian,
5348 segments=[
5349 MockMemorySegment(
5350 (0x12345678).to_bytes(
5351 4, "little" if little_endian else "big"
5352 ),
5353 0xFFFF0000,
5354 )
5355 ],
5356 )
5357 self.assertIdentical(prog.object("x"), Object(prog, "int", 0x56781234))
5358
5359 def test_variable_expr_non_contiguous_piece_addresses_too_large(self):
5360 for little_endian in (True, False):
5361 with self.subTest(little_endian=little_endian):
5362 prog = dwarf_program(
5363 wrap_test_type_dies(
5364 *labeled_int_die,
5365 DwarfDie(
5366 DW_TAG.variable,
5367 (
5368 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5369 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5370 DwarfAttrib(
5371 DW_AT.location,
5372 DW_FORM.exprloc,
5373 assembler.assemble(
5374 assembler.U8(DW_OP.addr),
5375 assembler.U64(0xFFFF0002),
5376 assembler.U8(DW_OP.piece),
5377 assembler.ULEB128(2),
5378 assembler.U8(DW_OP.addr),
5379 assembler.U64(0xFFFF0000),
5380 assembler.U8(DW_OP.piece),
5381 assembler.ULEB128(256),
5382 little_endian=little_endian,
5383 ),
5384 ),
5385 ),
5386 ),
5387 ),
5388 little_endian=little_endian,
5389 segments=[
5390 MockMemorySegment(
5391 (0x12345678).to_bytes(
5392 4, "little" if little_endian else "big"
5393 ),
5394 0xFFFF0000,
5395 )
5396 ],
5397 )
5398 self.assertIdentical(prog.object("x"), Object(prog, "int", 0x56781234))
5399
5400 def test_variable_expr_non_contiguous_bit_piece_addresses(self):
5401 for little_endian in (True, False):
5402 with self.subTest(little_endian=little_endian):
5403 prog = dwarf_program(
5404 wrap_test_type_dies(
5405 *labeled_int_die,
5406 DwarfDie(
5407 DW_TAG.variable,
5408 (
5409 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5410 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5411 DwarfAttrib(
5412 DW_AT.location,
5413 DW_FORM.exprloc,
5414 assembler.assemble(
5415 assembler.U8(DW_OP.addr),
5416 assembler.U64(0xFFFF0000),
5417 assembler.U8(DW_OP.bit_piece),
5418 assembler.ULEB128(4),
5419 assembler.ULEB128(0),
5420 assembler.U8(DW_OP.addr),
5421 assembler.U64(0xFFFF0000),
5422 assembler.U8(DW_OP.bit_piece),
5423 assembler.ULEB128(28),
5424 assembler.ULEB128(5),
5425 little_endian=little_endian,
5426 ),
5427 ),
5428 ),
5429 ),
5430 ),
5431 little_endian=little_endian,
5432 segments=[
5433 MockMemorySegment(
5434 (
5435 (0x2468ACE8).to_bytes(5, "little")
5436 if little_endian
5437 else (0x111A2B3C00).to_bytes(5, "big")
5438 ),
5439 0xFFFF0000,
5440 )
5441 ],
5442 )
5443 self.assertIdentical(prog.object("x"), Object(prog, "int", 0x12345678))
5444
5445 def test_variable_expr_empty_piece_non_contiguous_address(self):
5446 prog = dwarf_program(
5447 wrap_test_type_dies(
5448 *labeled_int_die,
5449 DwarfDie(
5450 DW_TAG.variable,
5451 (
5452 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5453 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5454 DwarfAttrib(
5455 DW_AT.location,
5456 DW_FORM.exprloc,
5457 assembler.assemble(
5458 assembler.U8(DW_OP.addr),
5459 assembler.U64(0xFFFF0000),
5460 assembler.U8(DW_OP.piece),
5461 assembler.ULEB128(2),
5462 # This piece is not contiguous with
5463 # the previous one, but it is zero
5464 # bits so it should be ignored.
5465 assembler.U8(DW_OP.addr),
5466 assembler.U64(0xEEEE0000),
5467 assembler.U8(DW_OP.piece),
5468 assembler.ULEB128(0),
5469 assembler.U8(DW_OP.addr),
5470 assembler.U64(0xFFFF0002),
5471 assembler.U8(DW_OP.piece),
5472 assembler.ULEB128(2),
5473 ),
5474 ),
5475 ),
5476 ),
5477 ),
5478 )
5479 self.assertIdentical(prog.object("x"), Object(prog, "int", address=0xFFFF0000))
5480
5481 def test_variable_expr_previous_empty_piece_non_contiguous_address(self):
5482 prog = dwarf_program(
5483 wrap_test_type_dies(
5484 *labeled_int_die,
5485 DwarfDie(
5486 DW_TAG.variable,
5487 (
5488 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5489 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5490 DwarfAttrib(
5491 DW_AT.location,
5492 DW_FORM.exprloc,
5493 assembler.assemble(
5494 assembler.U8(DW_OP.addr),
5495 assembler.U64(0xEEEE0000),
5496 assembler.U8(DW_OP.piece),
5497 assembler.ULEB128(0),
5498 # This piece is not contiguous with
5499 # the previous one, but the
5500 # previous one was zero bits so it
5501 # should be ignored.
5502 assembler.U8(DW_OP.addr),
5503 assembler.U64(0xFFFF0000),
5504 assembler.U8(DW_OP.piece),
5505 assembler.ULEB128(4),
5506 ),
5507 ),
5508 ),
5509 ),
5510 ),
5511 )
5512 self.assertIdentical(prog.object("x"), Object(prog, "int", address=0xFFFF0000))
5513
5514 def test_variable_expr_address_empty_piece(self):
5515 prog = dwarf_program(
5516 wrap_test_type_dies(
5517 *labeled_int_die,
5518 DwarfDie(
5519 DW_TAG.variable,
5520 (
5521 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5522 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5523 DwarfAttrib(
5524 DW_AT.location,
5525 DW_FORM.exprloc,
5526 assembler.assemble(
5527 assembler.U8(DW_OP.addr),
5528 assembler.U64(0xEEEE0000),
5529 assembler.U8(DW_OP.piece),
5530 assembler.ULEB128(0),
5531 ),
5532 ),
5533 ),
5534 ),
5535 ),
5536 )
5537 self.assertIdentical(
5538 prog.object("x"),
5539 Object(prog, "int", absence_reason=AbsenceReason.OPTIMIZED_OUT),
5540 )
5541
5542 def test_variable_expr_absent_empty_piece(self):
5543 prog = dwarf_program(
5544 wrap_test_type_dies(
5545 *labeled_int_die,
5546 DwarfDie(
5547 DW_TAG.variable,
5548 (
5549 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5550 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5551 DwarfAttrib(
5552 DW_AT.location,
5553 DW_FORM.exprloc,
5554 assembler.assemble(
5555 assembler.U8(DW_OP.piece),
5556 assembler.ULEB128(0),
5557 ),
5558 ),
5559 ),
5560 ),
5561 ),
5562 )
5563 self.assertIdentical(
5564 prog.object("x"),
5565 Object(prog, "int", absence_reason=AbsenceReason.OPTIMIZED_OUT),
5566 )
5567
5568 def test_variable_expr_unknown(self):
5569 prog = dwarf_program(
5570 wrap_test_type_dies(
5571 *labeled_int_die,
5572 DwarfDie(
5573 DW_TAG.variable,
5574 (
5575 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5576 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5577 DwarfAttrib(DW_AT.location, DW_FORM.exprloc, b"\xdf"),
5578 ),
5579 ),
5580 )
5581 )
5582 self.assertIdentical(
5583 prog.object("x"),
5584 Object(prog, "int", absence_reason=AbsenceReason.NOT_IMPLEMENTED),
5585 )
5586
5587 def test_variable_expr_unknown_after_location(self):
5588 prog = dwarf_program(
5589 wrap_test_type_dies(
5590 *labeled_int_die,
5591 DwarfDie(
5592 DW_TAG.variable,
5593 (
5594 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5595 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
5596 DwarfAttrib(
5597 DW_AT.location,
5598 DW_FORM.exprloc,
5599 assembler.assemble(
5600 assembler.U8(DW_OP.implicit_value),
5601 assembler.ULEB128(4),
5602 assembler.U32(0),
5603 assembler.U8(0xDF),
5604 ),
5605 ),
5606 ),
5607 ),
5608 )
5609 )
5610 self.assertIdentical(
5611 prog.object("x"),
5612 Object(prog, "int", absence_reason=AbsenceReason.NOT_IMPLEMENTED),
5613 )
5614
5615 def _eval_dwarf_expr(self, ops, **kwds):
5616 assemble_kwds = {
5617 key: value for key, value in kwds.items() if key == "little_endian"
5618 }
5619 return dwarf_program(
5620 wrap_test_type_dies(
5621 DwarfLabel("unsigned_long_long_die"),
5622 unsigned_long_long_die,
5623 DwarfDie(
5624 DW_TAG.variable,
5625 (
5626 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
5627 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "unsigned_long_long_die"),
5628 DwarfAttrib(
5629 DW_AT.location,
5630 DW_FORM.exprloc,
5631 assembler.assemble(
5632 *ops,
5633 assembler.U8(DW_OP.stack_value),
5634 **assemble_kwds,
5635 ),
5636 ),
5637 ),
5638 ),
5639 ),
5640 **kwds,
5641 )["x"].value_()
5642
5643 def _assert_dwarf_expr_eval(self, ops, expected, **kwds):
5644 self.assertEqual(self._eval_dwarf_expr(ops, **kwds), expected)
5645
5646 def _assert_dwarf_expr_stack_underflow(self, ops, **kwds):
5647 with self.assertRaisesRegex(Exception, "stack underflow"):
5648 self._eval_dwarf_expr(ops, **kwds)
5649
5650 def test_variable_expr_op_lit(self):
5651 for i in range(32):
5652 with self.subTest(i=i):
5653 self._assert_dwarf_expr_eval([assembler.U8(DW_OP.lit0 + i)], i)
5654
5655 def test_variable_expr_op_addr(self):
5656 with self.subTest(bits=64):
5657 self._assert_dwarf_expr_eval(
5658 [assembler.U8(DW_OP.addr), assembler.U64(2**64 - 1)],
5659 2**64 - 1,
5660 bits=64,
5661 )
5662 with self.subTest(bits=32):
5663 self._assert_dwarf_expr_eval(
5664 [assembler.U8(DW_OP.addr), assembler.U32(2**32 - 1)],
5665 2**32 - 1,
5666 bits=32,
5667 )
5668
5669 def test_variable_expr_op_constu(self):
5670 for bits in (64, 32):
5671 for size in (1, 2, 4, 8):
5672 op_name = f"const{size}u"
5673 with self.subTest(bits=bits, op=op_name):
5674 op = getattr(DW_OP, op_name)
5675 type_ = getattr(assembler, f"U{size * 8}")
5676 self._assert_dwarf_expr_eval(
5677 [assembler.U8(op), type_(2 ** (size * 8) - 1)],
5678 (2 ** (size * 8) - 1) & (2**bits - 1),
5679 bits=bits,
5680 )
5681 with self.subTest(bits=bits, op="constu"):
5682 self._assert_dwarf_expr_eval(
5683 [assembler.U8(DW_OP.constu), assembler.ULEB128(0x123456789)],
5684 0x123456789 & (2**bits - 1),
5685 bits=bits,
5686 )
5687
5688 # These are really tests of our ULEB128 parsing.
5689 def test_variable_expr_op_constu_max(self):
5690 self._assert_dwarf_expr_eval(
5691 [assembler.U8(DW_OP.constu), b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01"],
5692 2**64 - 1,
5693 )
5694
5695 def test_variable_expr_op_constu_non_canonical(self):
5696 self._assert_dwarf_expr_eval(
5697 [
5698 assembler.U8(DW_OP.constu),
5699 b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\x81\x00",
5700 ],
5701 2**64 - 1,
5702 )
5703 self._assert_dwarf_expr_eval(
5704 [
5705 assembler.U8(DW_OP.constu),
5706 b"\xfb\x80\x80\x80\x80\x80\x80\x80\x80\x80\x00",
5707 ],
5708 123,
5709 )
5710
5711 def test_variable_expr_op_constu_overflow(self):
5712 self.assertRaisesRegex(
5713 Exception,
5714 "overflow",
5715 self._eval_dwarf_expr,
5716 [assembler.U8(DW_OP.constu), b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\x02"],
5717 )
5718 self.assertRaisesRegex(
5719 Exception,
5720 "overflow",
5721 self._eval_dwarf_expr,
5722 [
5723 assembler.U8(DW_OP.constu),
5724 b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\x82\x00",
5725 ],
5726 )
5727 self.assertRaisesRegex(
5728 Exception,
5729 "overflow",
5730 self._eval_dwarf_expr,
5731 [
5732 assembler.U8(DW_OP.constu),
5733 b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\x81\x01",
5734 ],
5735 )
5736 self.assertRaisesRegex(
5737 Exception,
5738 "overflow",
5739 self._eval_dwarf_expr,
5740 [
5741 assembler.U8(DW_OP.constu),
5742 b"\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01",
5743 ],
5744 )
5745
5746 def test_variable_expr_op_consts(self):
5747 for bits in (64, 32):
5748 for size in (1, 2, 4, 8):
5749 op_name = f"const{size}s"
5750 with self.subTest(bits=bits, op=op_name):
5751 op = getattr(DW_OP, op_name)
5752 type_ = getattr(assembler, f"S{size * 8}")
5753 self._assert_dwarf_expr_eval(
5754 [assembler.U8(op), type_(-1)],
5755 -1 & (2**bits - 1),
5756 bits=bits,
5757 )
5758 with self.subTest(bits=bits, op="consts"):
5759 self._assert_dwarf_expr_eval(
5760 [assembler.U8(DW_OP.consts), assembler.SLEB128(-0x123456789)],
5761 -0x123456789 & (2**bits - 1),
5762 bits=bits,
5763 )
5764
5765 # These are really tests of our SLEB128 parsing.
5766 def test_variable_expr_op_consts_max(self):
5767 # Maximum positive value.
5768 self._assert_dwarf_expr_eval(
5769 [assembler.U8(DW_OP.consts), b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00"],
5770 2**63 - 1,
5771 )
5772 # Maximum negative value.
5773 self._assert_dwarf_expr_eval(
5774 [assembler.U8(DW_OP.consts), b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\x7f"],
5775 -1 & (2**64 - 1),
5776 )
5777 # Minimum negative value.
5778 self._assert_dwarf_expr_eval(
5779 [assembler.U8(DW_OP.consts), b"\x80\x80\x80\x80\x80\x80\x80\x80\x80\x7f"],
5780 -(2**63) & (2**64 - 1),
5781 )
5782
5783 def test_variable_expr_op_consts_non_canonical(self):
5784 self._assert_dwarf_expr_eval(
5785 [
5786 assembler.U8(DW_OP.consts),
5787 b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\x80\x00",
5788 ],
5789 2**63 - 1,
5790 )
5791 self._assert_dwarf_expr_eval(
5792 [
5793 assembler.U8(DW_OP.consts),
5794 b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x7f",
5795 ],
5796 -1 & (2**64 - 1),
5797 )
5798 self._assert_dwarf_expr_eval(
5799 [
5800 assembler.U8(DW_OP.consts),
5801 b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x7f",
5802 ],
5803 -1 & (2**64 - 1),
5804 )
5805 self._assert_dwarf_expr_eval(
5806 [
5807 assembler.U8(DW_OP.consts),
5808 b"\x80\x80\x80\x80\x80\x80\x80\x80\x80\xff\x7f",
5809 ],
5810 -(2**63) & (2**64 - 1),
5811 )
5812 self._assert_dwarf_expr_eval(
5813 [
5814 assembler.U8(DW_OP.consts),
5815 b"\x80\x80\x80\x80\x80\x80\x80\x80\x80\xff\xff\x7f",
5816 ],
5817 -(2**63) & (2**64 - 1),
5818 )
5819 self._assert_dwarf_expr_eval(
5820 [
5821 assembler.U8(DW_OP.consts),
5822 b"\xfb\x80\x80\x80\x80\x80\x80\x80\x80\x80\x00",
5823 ],
5824 123,
5825 )
5826
5827 def test_variable_expr_op_consts_overflow(self):
5828 self.assertRaisesRegex(
5829 Exception,
5830 "overflow",
5831 self._eval_dwarf_expr,
5832 [assembler.U8(DW_OP.consts), b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01"],
5833 )
5834 self.assertRaisesRegex(
5835 Exception,
5836 "overflow",
5837 self._eval_dwarf_expr,
5838 [
5839 assembler.U8(DW_OP.consts),
5840 b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01",
5841 ],
5842 )
5843 self.assertRaisesRegex(
5844 Exception,
5845 "overflow",
5846 self._eval_dwarf_expr,
5847 [
5848 assembler.U8(DW_OP.consts),
5849 b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\x81\x00",
5850 ],
5851 )
5852 self.assertRaisesRegex(
5853 Exception,
5854 "overflow",
5855 self._eval_dwarf_expr,
5856 [
5857 assembler.U8(DW_OP.consts),
5858 b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\x80\x01",
5859 ],
5860 )
5861 self.assertRaisesRegex(
5862 Exception,
5863 "overflow",
5864 self._eval_dwarf_expr,
5865 [
5866 assembler.U8(DW_OP.consts),
5867 b"\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01",
5868 ],
5869 )
5870 self.assertRaisesRegex(
5871 Exception,
5872 "overflow",
5873 self._eval_dwarf_expr,
5874 [
5875 assembler.U8(DW_OP.consts),
5876 b"\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x7f",
5877 ],
5878 )
5879
5880 def test_variable_expr_op_dup(self):
5881 self._assert_dwarf_expr_eval(
5882 [
5883 assembler.U8(DW_OP.lit1),
5884 assembler.U8(DW_OP.dup),
5885 assembler.U8(DW_OP.plus),
5886 ],
5887 2,
5888 )
5889
5890 def test_variable_expr_op_drop(self):
5891 self._assert_dwarf_expr_eval(
5892 [
5893 assembler.U8(DW_OP.lit1),
5894 assembler.U8(DW_OP.lit2),
5895 assembler.U8(DW_OP.drop),
5896 assembler.U8(DW_OP.lit3),
5897 assembler.U8(DW_OP.plus),
5898 ],
5899 4,
5900 )
5901
5902 def test_variable_expr_op_pick(self):
5903 for i, value in enumerate((30, 20, 10)):
5904 with self.subTest(i=i):
5905 self._assert_dwarf_expr_eval(
5906 [
5907 assembler.U8(DW_OP.lit10),
5908 assembler.U8(DW_OP.lit20),
5909 assembler.U8(DW_OP.lit30),
5910 assembler.U8(DW_OP.pick),
5911 assembler.U8(i),
5912 ],
5913 value,
5914 )
5915
5916 def test_variable_expr_op_pick_underflow(self):
5917 for i in (3, 255):
5918 with self.subTest(i=i):
5919 self._assert_dwarf_expr_stack_underflow(
5920 [
5921 assembler.U8(DW_OP.lit10),
5922 assembler.U8(DW_OP.lit20),
5923 assembler.U8(DW_OP.lit30),
5924 assembler.U8(DW_OP.pick),
5925 assembler.U8(i),
5926 ]
5927 )
5928
5929 def test_variable_expr_op_over(self):
5930 self._assert_dwarf_expr_eval(
5931 [
5932 assembler.U8(DW_OP.lit10),
5933 assembler.U8(DW_OP.lit20),
5934 assembler.U8(DW_OP.over),
5935 ],
5936 10,
5937 )
5938 self._assert_dwarf_expr_eval(
5939 [
5940 assembler.U8(DW_OP.lit10),
5941 assembler.U8(DW_OP.lit20),
5942 assembler.U8(DW_OP.lit30),
5943 assembler.U8(DW_OP.over),
5944 ],
5945 20,
5946 )
5947
5948 def test_variable_expr_op_swap(self):
5949 self._assert_dwarf_expr_eval(
5950 [
5951 assembler.U8(DW_OP.lit3),
5952 assembler.U8(DW_OP.lit5),
5953 assembler.U8(DW_OP.swap),
5954 assembler.U8(DW_OP.minus),
5955 ],
5956 2,
5957 )
5958
5959 def test_variable_expr_op_rot(self):
5960 for i, value in enumerate((5, 3, 7, 1)):
5961 self._assert_dwarf_expr_eval(
5962 [
5963 assembler.U8(DW_OP.lit1),
5964 assembler.U8(DW_OP.lit3),
5965 assembler.U8(DW_OP.lit5),
5966 assembler.U8(DW_OP.lit7),
5967 assembler.U8(DW_OP.rot),
5968 assembler.U8(DW_OP.pick),
5969 assembler.U8(i),
5970 ],
5971 value,
5972 )
5973
5974 def test_variable_expr_op_deref(self):
5975 for bits in (64, 32):
5976 for little_endian in (True, False):
5977 with self.subTest(bits=bits, little_endian=little_endian):
5978 self._assert_dwarf_expr_eval(
5979 [
5980 assembler.U8(DW_OP.addr),
5981 (assembler.U64 if bits == 64 else assembler.U32)(
5982 0xFFFF0000
5983 ),
5984 assembler.U8(DW_OP.deref),
5985 ],
5986 0x12345678,
5987 bits=bits,
5988 little_endian=little_endian,
5989 segments=[
5990 MockMemorySegment(
5991 (0x12345678).to_bytes(
5992 bits // 8, "little" if little_endian else "big"
5993 ),
5994 0xFFFF0000,
5995 )
5996 ],
5997 )
5998
5999 def test_variable_expr_op_deref_fault(self):
6000 with self.assertRaises(FaultError):
6001 self._eval_dwarf_expr(
6002 [
6003 assembler.U8(DW_OP.addr),
6004 assembler.U64(0xFFFF0000),
6005 assembler.U8(DW_OP.deref),
6006 ]
6007 )
6008
6009 def test_variable_expr_op_deref_size(self):
6010 for bits in (64, 32):
6011 for little_endian in (True, False):
6012 with self.subTest(bits=bits, little_endian=little_endian):
6013 self._assert_dwarf_expr_eval(
6014 [
6015 assembler.U8(DW_OP.addr),
6016 (assembler.U64 if bits == 64 else assembler.U32)(
6017 0xFFFF0000
6018 ),
6019 assembler.U8(DW_OP.deref_size),
6020 assembler.U8(2),
6021 ],
6022 0x1337,
6023 bits=bits,
6024 little_endian=little_endian,
6025 segments=[
6026 MockMemorySegment(
6027 (0x1337).to_bytes(
6028 2, "little" if little_endian else "big"
6029 ),
6030 0xFFFF0000,
6031 )
6032 ],
6033 )
6034
6035 def test_variable_expr_op_deref_size_fault(self):
6036 with self.assertRaises(FaultError):
6037 self._eval_dwarf_expr(
6038 [
6039 assembler.U8(DW_OP.addr),
6040 assembler.U64(0xFFFF0000),
6041 assembler.U8(DW_OP.deref_size),
6042 assembler.U8(1),
6043 ]
6044 )
6045
6046 def test_variable_expr_stack_underflow(self):
6047 for case in [
6048 (DW_OP.dup, 1),
6049 (DW_OP.drop, 1),
6050 (DW_OP.over, 2),
6051 (DW_OP.swap, 2),
6052 (DW_OP.rot, 3),
6053 (DW_OP.deref, 1),
6054 (DW_OP.deref_size, 1, assembler.U8(1)),
6055 (DW_OP.abs, 1),
6056 (DW_OP.and_, 2),
6057 (DW_OP.div, 2),
6058 (DW_OP.minus, 2),
6059 (DW_OP.mod, 2),
6060 (DW_OP.mul, 2),
6061 (DW_OP.neg, 1),
6062 (DW_OP.not_, 1),
6063 (DW_OP.or_, 2),
6064 (DW_OP.plus, 2),
6065 (DW_OP.plus_uconst, 1, assembler.ULEB128(1)),
6066 (DW_OP.shl, 2),
6067 (DW_OP.shr, 2),
6068 (DW_OP.shra, 2),
6069 (DW_OP.xor, 2),
6070 (DW_OP.le, 2),
6071 (DW_OP.ge, 2),
6072 (DW_OP.eq, 2),
6073 (DW_OP.lt, 2),
6074 (DW_OP.gt, 2),
6075 (DW_OP.ne, 2),
6076 (DW_OP.bra, 1, assembler.S16(1)),
6077 ]:
6078 op = case[0]
6079 min_entries = case[1]
6080 extra_args = case[2:]
6081 with self.subTest(op=op):
6082 for i in range(min_entries):
6083 self._assert_dwarf_expr_stack_underflow(
6084 [assembler.U8(DW_OP.lit1)] * i + [assembler.U8(op), *extra_args]
6085 )
6086
6087 def test_variable_expr_op_abs(self):
6088 for bits in (64, 32):
6089 with self.subTest(bits=bits):
6090 self._assert_dwarf_expr_eval(
6091 [
6092 assembler.U8(DW_OP.const1s),
6093 assembler.S8(-9),
6094 assembler.U8(DW_OP.abs),
6095 ],
6096 9,
6097 bits=bits,
6098 )
6099
6100 def test_variable_expr_op_and(self):
6101 for bits in (64, 32):
6102 with self.subTest(bits=bits):
6103 self._assert_dwarf_expr_eval(
6104 [
6105 assembler.U8(DW_OP.lit3),
6106 assembler.U8(DW_OP.lit5),
6107 assembler.U8(DW_OP.and_),
6108 ],
6109 1,
6110 bits=bits,
6111 )
6112
6113 def test_variable_expr_op_div(self):
6114 for bits in (64, 32):
6115 with self.subTest(bits=bits):
6116 self._assert_dwarf_expr_eval(
6117 [
6118 assembler.U8(DW_OP.lit5),
6119 assembler.U8(DW_OP.lit2),
6120 assembler.U8(DW_OP.div),
6121 ],
6122 2,
6123 bits=bits,
6124 )
6125 self._assert_dwarf_expr_eval(
6126 [
6127 assembler.U8(DW_OP.lit0),
6128 assembler.U8(DW_OP.lit3),
6129 assembler.U8(DW_OP.div),
6130 ],
6131 0,
6132 bits=bits,
6133 )
6134 # The DWARF 5 specification doesn't specify how signed division
6135 # should be rounded. We assume truncation towards zero like C.
6136 self._assert_dwarf_expr_eval(
6137 [
6138 assembler.U8(DW_OP.const1s),
6139 assembler.S8(-5),
6140 assembler.U8(DW_OP.lit2),
6141 assembler.U8(DW_OP.div),
6142 ],
6143 -2 & (2**bits - 1),
6144 bits=bits,
6145 )
6146
6147 def test_variable_expr_op_div_by_zero(self):
6148 with self.assertRaisesRegex(Exception, "division by zero"):
6149 self._eval_dwarf_expr(
6150 [
6151 assembler.U8(DW_OP.lit1),
6152 assembler.U8(DW_OP.lit0),
6153 assembler.U8(DW_OP.div),
6154 ]
6155 )
6156
6157 def test_variable_expr_op_minus(self):
6158 for bits in (64, 32):
6159 with self.subTest(bits=bits):
6160 self._assert_dwarf_expr_eval(
6161 [
6162 assembler.U8(DW_OP.lit5),
6163 assembler.U8(DW_OP.lit2),
6164 assembler.U8(DW_OP.minus),
6165 ],
6166 3,
6167 bits=bits,
6168 )
6169 self._assert_dwarf_expr_eval(
6170 [
6171 assembler.U8(DW_OP.lit2),
6172 assembler.U8(DW_OP.lit5),
6173 assembler.U8(DW_OP.minus),
6174 ],
6175 -3 & (2**bits - 1),
6176 bits=bits,
6177 )
6178
6179 def test_variable_expr_op_mod(self):
6180 for bits in (64, 32):
6181 with self.subTest(bits=bits):
6182 self._assert_dwarf_expr_eval(
6183 [
6184 assembler.U8(DW_OP.lit5),
6185 assembler.U8(DW_OP.lit2),
6186 assembler.U8(DW_OP.mod),
6187 ],
6188 1,
6189 bits=bits,
6190 )
6191 self._assert_dwarf_expr_eval(
6192 [
6193 assembler.U8(DW_OP.lit0),
6194 assembler.U8(DW_OP.lit3),
6195 assembler.U8(DW_OP.mod),
6196 ],
6197 0,
6198 bits=bits,
6199 )
6200 # Although DW_OP_div is signed, DW_OP_mod is unsigned.
6201 self._assert_dwarf_expr_eval(
6202 [
6203 assembler.U8(DW_OP.const1s),
6204 assembler.S8(-5),
6205 assembler.U8(DW_OP.lit2),
6206 assembler.U8(DW_OP.mod),
6207 ],
6208 1,
6209 bits=bits,
6210 )
6211
6212 def test_variable_expr_op_mod_by_zero(self):
6213 with self.assertRaisesRegex(Exception, "modulo by zero"):
6214 self._eval_dwarf_expr(
6215 [
6216 assembler.U8(DW_OP.lit1),
6217 assembler.U8(DW_OP.lit0),
6218 assembler.U8(DW_OP.mod),
6219 ]
6220 )
6221
6222 def test_variable_expr_op_mul(self):
6223 for bits in (64, 32):
6224 with self.subTest(bits=bits):
6225 self._assert_dwarf_expr_eval(
6226 [
6227 assembler.U8(DW_OP.lit5),
6228 assembler.U8(DW_OP.lit2),
6229 assembler.U8(DW_OP.mul),
6230 ],
6231 10,
6232 bits=bits,
6233 )
6234 self._assert_dwarf_expr_eval(
6235 [
6236 assembler.U8(DW_OP.const1s),
6237 assembler.S8(-5),
6238 assembler.U8(DW_OP.lit2),
6239 assembler.U8(DW_OP.mul),
6240 ],
6241 ((-5 & (2**bits - 1)) * 2) & (2**bits - 1),
6242 bits=bits,
6243 )
6244
6245 def test_variable_expr_op_neg(self):
6246 for bits in (64, 32):
6247 with self.subTest(bits=bits):
6248 self._assert_dwarf_expr_eval(
6249 [
6250 assembler.U8(DW_OP.lit7),
6251 assembler.U8(DW_OP.neg),
6252 ],
6253 -7 & (2**bits - 1),
6254 bits=bits,
6255 )
6256 self._assert_dwarf_expr_eval(
6257 [
6258 assembler.U8(DW_OP.const1s),
6259 assembler.S8(-7),
6260 assembler.U8(DW_OP.neg),
6261 ],
6262 7,
6263 bits=bits,
6264 )
6265
6266 def test_variable_expr_op_not(self):
6267 for bits in (64, 32):
6268 with self.subTest(bits=bits):
6269 self._assert_dwarf_expr_eval(
6270 [
6271 assembler.U8(DW_OP.lit0),
6272 assembler.U8(DW_OP.not_),
6273 ],
6274 2**bits - 1,
6275 bits=bits,
6276 )
6277 self._assert_dwarf_expr_eval(
6278 [
6279 assembler.U8(DW_OP.lit31),
6280 assembler.U8(DW_OP.not_),
6281 ],
6282 ~31 & (2**bits - 1),
6283 bits=bits,
6284 )
6285
6286 def test_variable_expr_op_or(self):
6287 for bits in (64, 32):
6288 with self.subTest(bits=bits):
6289 self._assert_dwarf_expr_eval(
6290 [
6291 assembler.U8(DW_OP.lit3),
6292 assembler.U8(DW_OP.lit5),
6293 assembler.U8(DW_OP.or_),
6294 ],
6295 7,
6296 bits=bits,
6297 )
6298
6299 def test_variable_expr_op_plus(self):
6300 for bits in (64, 32):
6301 with self.subTest(bits=bits):
6302 self._assert_dwarf_expr_eval(
6303 [
6304 assembler.U8(DW_OP.lit6),
6305 assembler.U8(DW_OP.lit7),
6306 assembler.U8(DW_OP.plus),
6307 ],
6308 13,
6309 bits=bits,
6310 )
6311 self._assert_dwarf_expr_eval(
6312 [
6313 assembler.S8(DW_OP.const1s),
6314 assembler.S8(-3),
6315 assembler.U8(DW_OP.lit5),
6316 assembler.U8(DW_OP.plus),
6317 ],
6318 2,
6319 bits=bits,
6320 )
6321
6322 def test_variable_expr_op_plus_uconst(self):
6323 for bits in (64, 32):
6324 with self.subTest(bits=bits):
6325 self._assert_dwarf_expr_eval(
6326 [
6327 assembler.U8(DW_OP.lit6),
6328 assembler.U8(DW_OP.plus_uconst),
6329 assembler.ULEB128(7),
6330 ],
6331 13,
6332 bits=bits,
6333 )
6334 self._assert_dwarf_expr_eval(
6335 [
6336 assembler.S8(DW_OP.const1s),
6337 assembler.S8(-3),
6338 assembler.U8(DW_OP.plus_uconst),
6339 assembler.ULEB128(5),
6340 ],
6341 2,
6342 bits=bits,
6343 )
6344
6345 def test_variable_expr_op_shl(self):
6346 for bits in (64, 32):
6347 with self.subTest(bits=bits):
6348 self._assert_dwarf_expr_eval(
6349 [
6350 assembler.U8(DW_OP.lit3),
6351 assembler.U8(DW_OP.lit4),
6352 assembler.U8(DW_OP.shl),
6353 ],
6354 48,
6355 bits=bits,
6356 )
6357 self._assert_dwarf_expr_eval(
6358 [
6359 assembler.U8(DW_OP.constu),
6360 assembler.ULEB128(2 ** (bits - 2)),
6361 assembler.U8(DW_OP.lit1),
6362 assembler.U8(DW_OP.shl),
6363 ],
6364 2 ** (bits - 1),
6365 bits=bits,
6366 )
6367 # The DWARF specification doesn't define the behavior of
6368 # shifting by a number of bits larger than the width of the
6369 # type. We evaluate it to zero.
6370 self._assert_dwarf_expr_eval(
6371 [
6372 assembler.U8(DW_OP.lit3),
6373 assembler.U8(DW_OP.const1u),
6374 assembler.U8(bits),
6375 assembler.U8(DW_OP.shl),
6376 ],
6377 0,
6378 bits=bits,
6379 )
6380
6381 def test_variable_expr_op_shr(self):
6382 for bits in (64, 32):
6383 with self.subTest(bits=bits):
6384 self._assert_dwarf_expr_eval(
6385 [
6386 assembler.U8(DW_OP.const1u),
6387 assembler.U8(48),
6388 assembler.U8(DW_OP.lit4),
6389 assembler.U8(DW_OP.shr),
6390 ],
6391 3,
6392 bits=bits,
6393 )
6394 self._assert_dwarf_expr_eval(
6395 [
6396 assembler.U8(DW_OP.constu),
6397 assembler.ULEB128(2 ** (bits - 1)),
6398 assembler.U8(DW_OP.lit1),
6399 assembler.U8(DW_OP.shr),
6400 ],
6401 2 ** (bits - 2),
6402 bits=bits,
6403 )
6404 self._assert_dwarf_expr_eval(
6405 [
6406 assembler.U8(DW_OP.const1s),
6407 assembler.S8(-1),
6408 assembler.U8(DW_OP.const1u),
6409 assembler.U8(bits),
6410 assembler.U8(DW_OP.shr),
6411 ],
6412 0,
6413 bits=bits,
6414 )
6415
6416 def test_variable_expr_op_shra(self):
6417 for bits in (64, 32):
6418 with self.subTest(bits=bits):
6419 self._assert_dwarf_expr_eval(
6420 [
6421 assembler.U8(DW_OP.const1u),
6422 assembler.U8(48),
6423 assembler.U8(DW_OP.lit4),
6424 assembler.U8(DW_OP.shra),
6425 ],
6426 3,
6427 bits=bits,
6428 )
6429 self._assert_dwarf_expr_eval(
6430 [
6431 assembler.U8(DW_OP.const1s),
6432 assembler.S8(-48),
6433 assembler.U8(DW_OP.lit4),
6434 assembler.U8(DW_OP.shra),
6435 ],
6436 -3 & (2**bits - 1),
6437 bits=bits,
6438 )
6439 self._assert_dwarf_expr_eval(
6440 [
6441 assembler.U8(DW_OP.constu),
6442 assembler.ULEB128(2 ** (bits - 1)),
6443 assembler.U8(DW_OP.lit1),
6444 assembler.U8(DW_OP.shra),
6445 ],
6446 2 ** (bits - 2) + 2 ** (bits - 1),
6447 bits=bits,
6448 )
6449 self._assert_dwarf_expr_eval(
6450 [
6451 assembler.U8(DW_OP.const1s),
6452 assembler.S8(-2),
6453 assembler.U8(DW_OP.const1u),
6454 assembler.U8(bits),
6455 assembler.U8(DW_OP.shra),
6456 ],
6457 -1 & (2**bits - 1),
6458 bits=bits,
6459 )
6460
6461 def test_variable_expr_op_xor(self):
6462 for bits in (64, 32):
6463 with self.subTest(bits=bits):
6464 self._assert_dwarf_expr_eval(
6465 [
6466 assembler.U8(DW_OP.lit3),
6467 assembler.U8(DW_OP.lit5),
6468 assembler.U8(DW_OP.xor),
6469 ],
6470 6,
6471 bits=bits,
6472 )
6473
6474 def test_variable_expr_relational(self):
6475 for op, py_op in [
6476 (DW_OP.le, operator.le),
6477 (DW_OP.ge, operator.ge),
6478 (DW_OP.eq, operator.eq),
6479 (DW_OP.lt, operator.lt),
6480 (DW_OP.gt, operator.gt),
6481 (DW_OP.ne, operator.ne),
6482 ]:
6483 for bits in (64, 32):
6484 for val1, val2 in [
6485 (3, 5),
6486 (3, -5),
6487 (-3, 5),
6488 (-3, -5),
6489 (5, 5),
6490 (5, -5),
6491 (-5, 5),
6492 (-5, -5),
6493 (6, 5),
6494 (6, -5),
6495 (-6, 5),
6496 (-6, -5),
6497 ]:
6498 with self.subTest(bits=bits, val1=val1, val2=val2):
6499 self._assert_dwarf_expr_eval(
6500 [
6501 assembler.U8(DW_OP.const1s),
6502 assembler.S8(val1),
6503 assembler.U8(DW_OP.const1s),
6504 assembler.S8(val2),
6505 assembler.U8(op),
6506 ],
6507 int(py_op(val1, val2)),
6508 )
6509
6510 def test_variable_expr_op_skip(self):
6511 self._assert_dwarf_expr_eval(
6512 [
6513 assembler.U8(DW_OP.skip),
6514 assembler.S16(3),
6515 assembler.U8(DW_OP.lit0),
6516 assembler.U8(DW_OP.lit0),
6517 assembler.U8(DW_OP.div),
6518 assembler.U8(DW_OP.lit20),
6519 ],
6520 20,
6521 )
6522 self._assert_dwarf_expr_eval(
6523 [
6524 assembler.U8(DW_OP.lit1),
6525 assembler.U8(DW_OP.skip),
6526 assembler.S16(4),
6527 assembler.U8(DW_OP.lit3),
6528 assembler.U8(DW_OP.skip),
6529 assembler.S16(4),
6530 assembler.U8(DW_OP.lit2),
6531 assembler.U8(DW_OP.skip),
6532 assembler.S16(-8),
6533 ],
6534 3,
6535 )
6536
6537 def test_variable_expr_op_skip_infinite(self):
6538 with self.assertRaisesRegex(Exception, "too many operations"):
6539 self._eval_dwarf_expr([assembler.U8(DW_OP.skip), assembler.S16(-3)])
6540
6541 def test_variable_expr_op_skip_out_of_bounds(self):
6542 with self.assertRaisesRegex(Exception, "out of bounds"):
6543 self._eval_dwarf_expr(
6544 [
6545 assembler.U8(DW_OP.skip),
6546 # 1 extra for for the DW_OP_stack_value added by
6547 # _eval_dwarf_expr().
6548 assembler.U16(3),
6549 assembler.U8(DW_OP.nop),
6550 ],
6551 )
6552
6553 def test_variable_expr_op_bra(self):
6554 self._assert_dwarf_expr_eval(
6555 [
6556 assembler.U8(DW_OP.lit31),
6557 assembler.U8(DW_OP.bra),
6558 assembler.S16(3),
6559 assembler.U8(DW_OP.lit0),
6560 assembler.U8(DW_OP.lit0),
6561 assembler.U8(DW_OP.div),
6562 assembler.U8(DW_OP.lit20),
6563 ],
6564 20,
6565 )
6566 self._assert_dwarf_expr_eval(
6567 [
6568 assembler.U8(DW_OP.lit1),
6569 assembler.U8(DW_OP.lit0),
6570 assembler.U8(DW_OP.bra),
6571 assembler.S16(1),
6572 assembler.U8(DW_OP.lit2),
6573 ],
6574 2,
6575 )
6576 # More complicated expression implementing something like this:
6577 # i = 0
6578 # x = 0
6579 # do {
6580 # x += 2;
6581 # i += 1;
6582 # while (i <= 5);
6583 # return x;
6584 self._assert_dwarf_expr_eval(
6585 [
6586 assembler.U8(DW_OP.lit0),
6587 assembler.U8(DW_OP.lit0),
6588 assembler.U8(DW_OP.plus_uconst),
6589 assembler.ULEB128(2),
6590 assembler.U8(DW_OP.swap),
6591 assembler.U8(DW_OP.plus_uconst),
6592 assembler.ULEB128(1),
6593 assembler.U8(DW_OP.swap),
6594 assembler.U8(DW_OP.over),
6595 assembler.U8(DW_OP.lit5),
6596 assembler.U8(DW_OP.lt),
6597 assembler.U8(DW_OP.bra),
6598 assembler.S16(-12),
6599 ],
6600 10,
6601 )
6602
6603 def test_variable_expr_op_bra_out_of_bounds(self):
6604 with self.assertRaisesRegex(Exception, "out of bounds"):
6605 self._eval_dwarf_expr(
6606 [
6607 assembler.U8(DW_OP.lit1),
6608 assembler.U8(DW_OP.bra),
6609 # 1 extra for for the DW_OP_stack_value added by
6610 # _eval_dwarf_expr().
6611 assembler.U16(3),
6612 assembler.U8(DW_OP.nop),
6613 ],
6614 )
6615 self._assert_dwarf_expr_eval(
6616 [
6617 assembler.U8(DW_OP.lit0),
6618 assembler.U8(DW_OP.bra),
6619 assembler.U16(3),
6620 assembler.U8(DW_OP.lit2),
6621 ],
6622 2,
6623 )
6624
6625 def test_variable_expr_op_nop(self):
6626 self._assert_dwarf_expr_eval(
6627 [
6628 assembler.U8(DW_OP.nop),
6629 assembler.U8(DW_OP.nop),
6630 assembler.U8(DW_OP.lit25),
6631 assembler.U8(DW_OP.nop),
6632 assembler.U8(DW_OP.nop),
6633 ],
6634 25,
6635 )
6636
6637 def test_variable_const_signed(self):
6638 for form in (
6639 DW_FORM.data1,
6640 DW_FORM.data2,
6641 DW_FORM.data4,
6642 DW_FORM.data8,
6643 DW_FORM.sdata,
6644 ):
6645 prog = dwarf_program(
6646 wrap_test_type_dies(
6647 *labeled_int_die,
6648 DwarfDie(
6649 DW_TAG.variable,
6650 (
6651 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
6652 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
6653 DwarfAttrib(DW_AT.const_value, form, 1),
6654 ),
6655 ),
6656 )
6657 )
6658 self.assertIdentical(
6659 prog["x"], Object(prog, prog.int_type("int", 4, True), 1)
6660 )
6661
6662 def test_variable_const_unsigned(self):
6663 for form in (
6664 DW_FORM.data1,
6665 DW_FORM.data2,
6666 DW_FORM.data4,
6667 DW_FORM.data8,
6668 DW_FORM.udata,
6669 ):
6670 prog = dwarf_program(
6671 wrap_test_type_dies(
6672 *labeled_unsigned_int_die,
6673 DwarfDie(
6674 DW_TAG.variable,
6675 (
6676 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
6677 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "unsigned_int_die"),
6678 DwarfAttrib(DW_AT.const_value, form, 1),
6679 ),
6680 ),
6681 )
6682 )
6683 self.assertIdentical(
6684 prog["x"], Object(prog, prog.int_type("unsigned int", 4, False), 1)
6685 )
6686
6687 def test_variable_const_block(self):
6688 prog = dwarf_program(
6689 wrap_test_type_dies(
6690 *labeled_int_die,
6691 DwarfLabel("array_die"),
6692 DwarfDie(
6693 DW_TAG.array_type,
6694 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
6695 (
6696 DwarfDie(
6697 DW_TAG.subrange_type,
6698 (DwarfAttrib(DW_AT.upper_bound, DW_FORM.data1, 1),),
6699 ),
6700 ),
6701 ),
6702 DwarfDie(
6703 DW_TAG.variable,
6704 (
6705 DwarfAttrib(DW_AT.name, DW_FORM.string, "p"),
6706 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
6707 DwarfAttrib(
6708 DW_AT.const_value,
6709 DW_FORM.block1,
6710 b"\x01\x00\x00\x00\x02\x00\x00\x00",
6711 ),
6712 ),
6713 ),
6714 )
6715 )
6716 self.assertIdentical(
6717 prog["p"],
6718 Object(prog, prog.array_type(prog.int_type("int", 4, True), 2), [1, 2]),
6719 )
6720
6721 def test_variable_const_block_too_small(self):
6722 prog = dwarf_program(
6723 wrap_test_type_dies(
6724 *labeled_int_die,
6725 DwarfLabel("array_die"),
6726 DwarfDie(
6727 DW_TAG.array_type,
6728 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
6729 (
6730 DwarfDie(
6731 DW_TAG.subrange_type,
6732 (DwarfAttrib(DW_AT.upper_bound, DW_FORM.data1, 1),),
6733 ),
6734 ),
6735 ),
6736 DwarfDie(
6737 DW_TAG.variable,
6738 (
6739 DwarfAttrib(DW_AT.name, DW_FORM.string, "p"),
6740 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "array_die"),
6741 DwarfAttrib(
6742 DW_AT.const_value,
6743 DW_FORM.block,
6744 b"\x01\x00\x00\x00\x02\x00\x00",
6745 ),
6746 ),
6747 ),
6748 )
6749 )
6750 self.assertRaisesRegex(Exception, "too small", prog.variable, "p")
6751
6752 @with_and_without_dw_form_indirect
6753 def test_specification(self, use_dw_form_indirect):
6754 prog = dwarf_program(
6755 wrap_test_type_dies(
6756 *labeled_int_die,
6757 DwarfLabel("declaration_die"),
6758 DwarfDie(
6759 DW_TAG.variable,
6760 (
6761 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
6762 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
6763 DwarfAttrib(DW_AT.declaration, DW_FORM.flag_present, True),
6764 ),
6765 ),
6766 DwarfDie(
6767 DW_TAG.variable,
6768 (
6769 DwarfAttrib(
6770 DW_AT.specification, DW_FORM.ref4, "declaration_die"
6771 ),
6772 DwarfAttrib(
6773 DW_AT.location,
6774 DW_FORM.exprloc,
6775 b"\x03\x04\x03\x02\x01\xff\xff\xff\xff",
6776 ),
6777 ),
6778 ),
6779 ),
6780 use_dw_form_indirect=use_dw_form_indirect,
6781 )
6782
6783 self.assertIdentical(
6784 prog["x"],
6785 Object(prog, prog.int_type("int", 4, True), address=0xFFFFFFFF01020304),
6786 )
6787
6788 @with_and_without_dw_form_indirect
6789 def test_namespace_reverse_specification(self, use_dw_form_indirect):
6790 """Test specification inside namespace while declaration is outside of it."""
6791 prog = dwarf_program(
6792 (
6793 *labeled_int_die,
6794 DwarfDie(
6795 DW_TAG.namespace,
6796 [
6797 DwarfAttrib(DW_AT.name, DW_FORM.string, "moho"),
6798 DwarfAttrib(DW_AT.sibling, DW_FORM.ref4, "declaration_die"),
6799 ],
6800 [
6801 DwarfDie(
6802 DW_TAG.variable,
6803 (
6804 DwarfAttrib(
6805 DW_AT.specification, DW_FORM.ref4, "declaration_die"
6806 ),
6807 DwarfAttrib(
6808 DW_AT.location,
6809 DW_FORM.exprloc,
6810 b"\x03\x04\x03\x02\x01\xff\xff\xff\xff",
6811 ),
6812 ),
6813 )
6814 ],
6815 ),
6816 DwarfLabel("declaration_die"),
6817 DwarfDie(
6818 DW_TAG.variable,
6819 (
6820 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
6821 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
6822 DwarfAttrib(DW_AT.declaration, DW_FORM.flag_present, True),
6823 ),
6824 ),
6825 ),
6826 use_dw_form_indirect=use_dw_form_indirect,
6827 )
6828
6829 self.assertIdentical(
6830 prog["x"],
6831 Object(prog, prog.int_type("int", 4, True), address=0xFFFFFFFF01020304),
6832 )
6833
6834 def test_not_found(self):
6835 prog = dwarf_program(int_die)
6836 self.assertRaisesRegex(ObjectNotFoundError, "could not find", prog.object, "y")
6837
6838
6839 class TestScopes(TestCase):
6840 def test_global_namespace(self):
6841 prog = dwarf_program(
6842 (
6843 *labeled_int_die,
6844 DwarfDie(
6845 DW_TAG.variable,
6846 (
6847 DwarfAttrib(DW_AT.name, DW_FORM.string, "target"),
6848 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
6849 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 123),
6850 ),
6851 ),
6852 )
6853 )
6854 self.assertIdentical(
6855 prog["::target"], Object(prog, prog.int_type("int", 4, True), 123)
6856 )
6857 self.assertIdentical(prog["::target"], prog["target"])
6858
6859 def test_namespaces_single(self):
6860 prog = dwarf_program(
6861 (
6862 *labeled_int_die,
6863 DwarfDie(
6864 DW_TAG.namespace,
6865 (DwarfAttrib(DW_AT.name, DW_FORM.string, "moho"),),
6866 (
6867 DwarfDie(
6868 DW_TAG.variable,
6869 (
6870 DwarfAttrib(DW_AT.name, DW_FORM.string, "target"),
6871 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
6872 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 123),
6873 ),
6874 ),
6875 ),
6876 ),
6877 )
6878 )
6879 self.assertIdentical(
6880 prog["moho::target"], Object(prog, prog.int_type("int", 4, True), 123)
6881 )
6882
6883 def test_namespaces_gcc(self):
6884 prog = dwarf_program(
6885 (
6886 *labeled_int_die,
6887 DwarfDie(
6888 DW_TAG.namespace,
6889 (DwarfAttrib(DW_AT.name, DW_FORM.string, "moho"),),
6890 (
6891 DwarfDie(
6892 DW_TAG.variable,
6893 (
6894 DwarfAttrib(DW_AT.name, DW_FORM.string, "target"),
6895 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
6896 DwarfAttrib(DW_AT.const_value, DW_FORM.data1, 123),
6897 ),
6898 ),
6899 ),
6900 ),
6901 )
6902 )
6903 self.assertIdentical(
6904 prog["moho::target"], Object(prog, prog.int_type("int", 4, True), 123)
6905 )
6906
6907 def test_namespaces_nested(self):
6908 prog = dwarf_program(
6909 (
6910 *labeled_int_die,
6911 DwarfDie(
6912 DW_TAG.namespace,
6913 (DwarfAttrib(DW_AT.name, DW_FORM.string, "moho"),),
6914 (
6915 DwarfDie(
6916 DW_TAG.namespace,
6917 (DwarfAttrib(DW_AT.name, DW_FORM.string, "eve"),),
6918 (
6919 DwarfDie(
6920 DW_TAG.namespace,
6921 (
6922 DwarfAttrib(
6923 DW_AT.name, DW_FORM.string, "kerbin"
6924 ),
6925 ),
6926 (
6927 DwarfDie(
6928 DW_TAG.variable,
6929 (
6930 DwarfAttrib(
6931 DW_AT.name, DW_FORM.string, "minmus"
6932 ),
6933 DwarfAttrib(
6934 DW_AT.type, DW_FORM.ref4, "int_die"
6935 ),
6936 DwarfAttrib(
6937 DW_AT.const_value, DW_FORM.data1, 47
6938 ),
6939 ),
6940 ),
6941 ),
6942 ),
6943 ),
6944 ),
6945 ),
6946 ),
6947 )
6948 )
6949 self.assertIdentical(
6950 prog["moho::eve::kerbin::minmus"],
6951 Object(prog, prog.int_type("int", 4, True), 47),
6952 )
6953
6954 def test_nested_classes(self):
6955 for kind, tag in (
6956 ("class", DW_TAG.class_type),
6957 ("struct", DW_TAG.structure_type),
6958 ("union", DW_TAG.union_type),
6959 ):
6960 with self.subTest(kind=kind):
6961 prog = dwarf_program(
6962 (
6963 DwarfDie(
6964 tag,
6965 (
6966 DwarfAttrib(DW_AT.name, DW_FORM.string, "Foo"),
6967 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 0),
6968 ),
6969 (
6970 DwarfDie(
6971 tag,
6972 (
6973 DwarfAttrib(DW_AT.name, DW_FORM.string, "Bar"),
6974 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 0),
6975 ),
6976 ),
6977 ),
6978 ),
6979 DwarfDie(
6980 DW_TAG.subprogram,
6981 (DwarfAttrib(DW_AT.name, DW_FORM.string, "main"),),
6982 ),
6983 ),
6984 lang=DW_LANG.C_plus_plus,
6985 )
6986 self.assertIdentical(
6987 prog.type(kind + " Foo::Bar"),
6988 getattr(prog, kind + "_type")("Bar", 0, ()),
6989 )
6990
6991
6992 class TestProgram(TestCase):
6993 def test_language(self):
6994 dies = (
6995 DwarfDie(
6996 DW_TAG.subprogram,
6997 (
6998 DwarfAttrib(DW_AT.name, DW_FORM.string, "main"),
6999 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
7000 DwarfAttrib(DW_AT.low_pc, DW_FORM.addr, 0x7FC3EB9B1C30),
7001 ),
7002 ),
7003 *labeled_int_die,
7004 )
7005 self.assertEqual(dwarf_program(()).language, DEFAULT_LANGUAGE)
7006 self.assertEqual(dwarf_program(dies).language, DEFAULT_LANGUAGE)
7007 self.assertEqual(dwarf_program(dies, lang=DW_LANG.C).language, Language.C)
7008 self.assertEqual(
7009 dwarf_program(dies, lang=DW_LANG.BLISS).language, DEFAULT_LANGUAGE
7010 )
7011 self.assertEqual(
7012 dwarf_program(dies, lang=DW_LANG.C_plus_plus_14).language, Language.CPP
7013 )
7014
7015 self.assertEqual(
7016 dwarf_program(dies, lang=DW_LANG.C_plus_plus).object("main").type_.language,
7017 Language.CPP,
7018 )
7019
7020 def test_reference_counting(self):
7021 # Test that we keep the appropriate objects alive even if we don't have
7022 # an explicit reference (e.g., from a temporary variable).
7023 dies = (
7024 *labeled_int_die,
7025 DwarfDie(
7026 DW_TAG.variable,
7027 [
7028 DwarfAttrib(DW_AT.name, DW_FORM.string, "x"),
7029 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
7030 DwarfAttrib(
7031 DW_AT.location,
7032 DW_FORM.exprloc,
7033 b"\x03\x04\x03\x02\x01\xff\xff\xff\xff",
7034 ),
7035 ],
7036 ),
7037 )
7038 self.assertEqual(dwarf_program(dies)["x"].address_, 0xFFFFFFFF01020304)
7039 self.assertEqual(
7040 dwarf_program(dies)["x"].prog_["x"].address_, 0xFFFFFFFF01020304
7041 )
7042 self.assertFalse(dwarf_program(dies)["x"].prog_.flags & ProgramFlags.IS_LIVE)
7043 self.assertEqual(dwarf_program(dies)["x"].type_.name, "int")
7044
7045 def test_reference_counting_type_member(self):
7046 dies = (
7047 DwarfDie(
7048 DW_TAG.structure_type,
7049 (
7050 DwarfAttrib(DW_AT.name, DW_FORM.string, "foo"),
7051 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
7052 ),
7053 (
7054 DwarfDie(
7055 DW_TAG.member,
7056 (
7057 DwarfAttrib(DW_AT.name, DW_FORM.string, "bar"),
7058 DwarfAttrib(DW_AT.data_member_location, DW_FORM.data1, 0),
7059 DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),
7060 ),
7061 ),
7062 ),
7063 ),
7064 *labeled_int_die,
7065 )
7066 self.assertIsNotNone(repr(dwarf_program(dies).type("struct foo").members[0]))
7067
7068 def test_reference_counting_type_parameter(self):
7069 dies = wrap_test_type_dies(
7070 DwarfDie(
7071 DW_TAG.subroutine_type,
7072 (),
7073 (
7074 DwarfDie(
7075 DW_TAG.formal_parameter,
7076 (DwarfAttrib(DW_AT.type, DW_FORM.ref4, "int_die"),),
7077 ),
7078 ),
7079 ),
7080 *labeled_int_die,
7081 )
7082 self.assertIsNotNone(repr(dwarf_program(dies).type("TEST").type.parameters[0]))
7083
7084
7085 class TestCompressedDebugSections(TestCase):
7086 def test_zlib_gnu(self):
7087 prog = dwarf_program(wrap_test_type_dies(int_die), compress="zlib-gnu")
7088 self.assertIdentical(prog.type("TEST").type, prog.int_type("int", 4, True))
7089
7090 def test_zlib_gabi(self):
7091 prog = dwarf_program(wrap_test_type_dies(int_die), compress="zlib-gabi")
7092 self.assertIdentical(prog.type("TEST").type, prog.int_type("int", 4, True))
7093
7094
7095 class TestSplitDwarf(TestCase):
7096 def test_dwo4(self):
7097 prog = Program()
7098 with tempfile.TemporaryDirectory() as temp_dir:
7099 with open(os.path.join(temp_dir, "split.dwo"), "wb") as f:
7100 f.write(
7101 create_dwarf_file(
7102 DwarfUnit(
7103 DW_UT.split_compile,
7104 DwarfDie(
7105 DW_TAG.compile_unit,
7106 (
7107 DwarfAttrib(
7108 DW_AT.GNU_dwo_id,
7109 DW_FORM.data8,
7110 0xDDEEAADDBBEEFFFF,
7111 ),
7112 ),
7113 wrap_test_type_dies(int_die),
7114 ),
7115 ),
7116 split="dwo",
7117 )
7118 )
7119 with open(os.path.join(temp_dir, "skeleton"), "wb") as f:
7120 f.write(
7121 create_dwarf_file(
7122 DwarfUnit(
7123 DW_UT.skeleton,
7124 DwarfDie(
7125 DW_TAG.compile_unit,
7126 (
7127 DwarfAttrib(
7128 DW_AT.GNU_dwo_name, DW_FORM.string, "split.dwo"
7129 ),
7130 DwarfAttrib(
7131 DW_AT.GNU_dwo_id,
7132 DW_FORM.data8,
7133 0xDDEEAADDBBEEFFFF,
7134 ),
7135 ),
7136 ),
7137 )
7138 )
7139 )
7140 add_extra_dwarf(prog, f.name)
7141 self.assertIdentical(prog.type("TEST").type, prog.int_type("int", 4, True))
7142
7143 def test_dwo4_not_found(self):
7144 prog = Program()
7145 with tempfile.TemporaryDirectory() as temp_dir:
7146 with open(os.path.join(temp_dir, "skeleton"), "wb") as f:
7147 f.write(
7148 create_dwarf_file(
7149 DwarfUnit(
7150 DW_UT.skeleton,
7151 DwarfDie(
7152 DW_TAG.compile_unit,
7153 (
7154 DwarfAttrib(
7155 DW_AT.GNU_dwo_name, DW_FORM.string, "split.dwo"
7156 ),
7157 DwarfAttrib(
7158 DW_AT.GNU_dwo_id,
7159 DW_FORM.data8,
7160 0xDDEEAADDBBEEFFFF,
7161 ),
7162 ),
7163 ),
7164 )
7165 )
7166 )
7167 with self.assertLogs(logging.getLogger("drgn"), "WARNING") as log:
7168 add_extra_dwarf(prog, f.name)
7169 # Force debug info to be indexed.
7170 try:
7171 prog["foo"]
7172 except KeyError:
7173 pass
7174 self.assertTrue(
7175 any(
7176 "split DWARF file split.dwo not found" in output
7177 for output in log.output
7178 )
7179 )
7180
7181 def test_dwo4_id_mismatch(self):
7182 prog = Program()
7183 with tempfile.TemporaryDirectory() as temp_dir:
7184 with open(os.path.join(temp_dir, "split.dwo"), "wb") as f:
7185 f.write(
7186 create_dwarf_file(
7187 DwarfUnit(
7188 DW_UT.split_compile,
7189 DwarfDie(
7190 DW_TAG.compile_unit,
7191 (
7192 DwarfAttrib(
7193 DW_AT.GNU_dwo_id,
7194 DW_FORM.data8,
7195 0xBBBBBBBB00000000,
7196 ),
7197 ),
7198 ),
7199 ),
7200 split="dwo",
7201 )
7202 )
7203 with open(os.path.join(temp_dir, "skeleton"), "wb") as f:
7204 f.write(
7205 create_dwarf_file(
7206 DwarfUnit(
7207 DW_UT.skeleton,
7208 DwarfDie(
7209 DW_TAG.compile_unit,
7210 (
7211 DwarfAttrib(
7212 DW_AT.GNU_dwo_name, DW_FORM.string, "split.dwo"
7213 ),
7214 DwarfAttrib(
7215 DW_AT.GNU_dwo_id,
7216 DW_FORM.data8,
7217 0xDDEEAADDBBEEFFFF,
7218 ),
7219 ),
7220 ),
7221 )
7222 )
7223 )
7224 with self.assertLogs(logging.getLogger("drgn"), "WARNING") as log:
7225 add_extra_dwarf(prog, f.name)
7226 # Force debug info to be indexed.
7227 try:
7228 prog["foo"]
7229 except KeyError:
7230 pass
7231 self.assertTrue(
7232 any(
7233 "split DWARF file split.dwo not found" in output
7234 for output in log.output
7235 )
7236 )
7237
7238 def test_dwo5(self):
7239 prog = Program()
7240 with tempfile.TemporaryDirectory() as temp_dir:
7241 with open(os.path.join(temp_dir, "split.dwo"), "wb") as f:
7242 f.write(
7243 create_dwarf_file(
7244 DwarfUnit(
7245 DW_UT.split_compile,
7246 DwarfDie(
7247 DW_TAG.compile_unit,
7248 (),
7249 wrap_test_type_dies(int_die),
7250 ),
7251 dwo_id=0xDDEEAADDBBEEFFFF,
7252 ),
7253 version=5,
7254 split="dwo",
7255 )
7256 )
7257 with open(os.path.join(temp_dir, "skeleton"), "wb") as f:
7258 f.write(
7259 create_dwarf_file(
7260 DwarfUnit(
7261 DW_UT.skeleton,
7262 DwarfDie(
7263 DW_TAG.compile_unit,
7264 (
7265 DwarfAttrib(
7266 DW_AT.dwo_name, DW_FORM.string, "split.dwo"
7267 ),
7268 ),
7269 ),
7270 dwo_id=0xDDEEAADDBBEEFFFF,
7271 ),
7272 version=5,
7273 )
7274 )
7275 add_extra_dwarf(prog, f.name)
7276 self.assertIdentical(prog.type("TEST").type, prog.int_type("int", 4, True))
7277
7278 def test_dwo5_not_found(self):
7279 prog = Program()
7280 with tempfile.TemporaryDirectory() as temp_dir:
7281 with open(os.path.join(temp_dir, "skeleton"), "wb") as f:
7282 f.write(
7283 create_dwarf_file(
7284 DwarfUnit(
7285 DW_UT.skeleton,
7286 DwarfDie(
7287 DW_TAG.compile_unit,
7288 (
7289 DwarfAttrib(
7290 DW_AT.dwo_name, DW_FORM.string, "split.dwo"
7291 ),
7292 ),
7293 ),
7294 dwo_id=0xDDEEAADDBBEEFFFF,
7295 ),
7296 version=5,
7297 )
7298 )
7299 with self.assertLogs(logging.getLogger("drgn"), "WARNING") as log:
7300 add_extra_dwarf(prog, f.name)
7301 # Force debug info to be indexed.
7302 try:
7303 prog["foo"]
7304 except KeyError:
7305 pass
7306 self.assertTrue(
7307 any(
7308 "split DWARF file split.dwo not found" in output
7309 for output in log.output
7310 )
7311 )
7312
7313 def test_dwo5_id_mismatch(self):
7314 prog = Program()
7315 with tempfile.TemporaryDirectory() as temp_dir:
7316 with open(os.path.join(temp_dir, "split.dwo"), "wb") as f:
7317 f.write(
7318 create_dwarf_file(
7319 DwarfUnit(
7320 DW_UT.split_compile,
7321 DwarfDie(
7322 DW_TAG.compile_unit,
7323 (),
7324 wrap_test_type_dies(int_die),
7325 ),
7326 dwo_id=0xBBBBBBBB00000000,
7327 ),
7328 version=5,
7329 split="dwo",
7330 )
7331 )
7332 with open(os.path.join(temp_dir, "skeleton"), "wb") as f:
7333 f.write(
7334 create_dwarf_file(
7335 DwarfUnit(
7336 DW_UT.skeleton,
7337 DwarfDie(
7338 DW_TAG.compile_unit,
7339 (
7340 DwarfAttrib(
7341 DW_AT.dwo_name, DW_FORM.string, "split.dwo"
7342 ),
7343 ),
7344 ),
7345 dwo_id=0xDDEEAADDBBEEFFFF,
7346 ),
7347 version=5,
7348 )
7349 )
7350 with self.assertLogs(logging.getLogger("drgn"), "WARNING") as log:
7351 add_extra_dwarf(prog, f.name)
7352 # Force debug info to be indexed.
7353 try:
7354 prog["foo"]
7355 except KeyError:
7356 pass
7357 self.assertTrue(
7358 any(
7359 "split DWARF file split.dwo not found" in output
7360 for output in log.output
7361 )
7362 )
7363
7364
7365 class TestImportedUnit(TestCase):
7366 alt_build_id = b"\xfe\xdc\xba\x98\x76\x54\x32\x10"
7367
7368 def test_global(self):
7369 prog = dwarf_program(
7370 (
7371 DwarfUnit(
7372 DW_UT.compile,
7373 DwarfDie(
7374 DW_TAG.compile_unit,
7375 (),
7376 (
7377 DwarfDie(
7378 DW_TAG.imported_unit,
7379 (
7380 DwarfAttrib(
7381 DW_AT.import_,
7382 DW_FORM.ref_addr,
7383 "partial_unit",
7384 ),
7385 ),
7386 ),
7387 ),
7388 ),
7389 ),
7390 DwarfUnit(
7391 DW_UT.partial,
7392 DwarfDie(
7393 DW_TAG.partial_unit,
7394 (),
7395 wrap_test_type_dies(int_die),
7396 ),
7397 die_label="partial_unit",
7398 ),
7399 ),
7400 )
7401 self.assertIdentical(prog.type("TEST").type, prog.int_type("int", 4, True))
7402
7403 def test_global_alt(self):
7404 with tempfile.NamedTemporaryFile() as alt_f:
7405 alt_dwarf = compile_dwarf(
7406 (
7407 DwarfUnit(
7408 DW_UT.partial,
7409 DwarfDie(
7410 DW_TAG.partial_unit,
7411 (),
7412 wrap_test_type_dies(int_die),
7413 ),
7414 die_label="alt_unit",
7415 ),
7416 ),
7417 build_id=self.alt_build_id,
7418 )
7419 alt_f.write(alt_dwarf.data)
7420 alt_f.flush()
7421
7422 prog = dwarf_program(
7423 (
7424 DwarfUnit(
7425 DW_UT.compile,
7426 DwarfDie(
7427 DW_TAG.compile_unit,
7428 (),
7429 (
7430 DwarfDie(
7431 DW_TAG.imported_unit,
7432 (
7433 DwarfAttrib(
7434 DW_AT.import_,
7435 DW_FORM.GNU_ref_alt,
7436 alt_dwarf.labels["alt_unit"],
7437 ),
7438 ),
7439 ),
7440 ),
7441 ),
7442 ),
7443 ),
7444 gnu_debugaltlink=(alt_f.name, self.alt_build_id),
7445 )
7446 self.assertIdentical(prog.type("TEST").type, prog.int_type("int", 4, True))
7447
7448 def test_global_nested(self):
7449 prog = dwarf_program(
7450 (
7451 DwarfUnit(
7452 DW_UT.compile,
7453 DwarfDie(
7454 DW_TAG.compile_unit,
7455 (),
7456 (
7457 DwarfDie(
7458 DW_TAG.imported_unit,
7459 (
7460 DwarfAttrib(
7461 DW_AT.import_,
7462 DW_FORM.ref_addr,
7463 "partial_unit",
7464 ),
7465 ),
7466 ),
7467 ),
7468 ),
7469 ),
7470 DwarfUnit(
7471 DW_UT.partial,
7472 DwarfDie(
7473 DW_TAG.partial_unit,
7474 (),
7475 (
7476 DwarfDie(
7477 DW_TAG.imported_unit,
7478 (
7479 DwarfAttrib(
7480 DW_AT.import_, DW_FORM.ref_addr, "partial_unit2"
7481 ),
7482 ),
7483 ),
7484 ),
7485 ),
7486 die_label="partial_unit",
7487 ),
7488 DwarfUnit(
7489 DW_UT.partial,
7490 DwarfDie(
7491 DW_TAG.partial_unit,
7492 (),
7493 wrap_test_type_dies(int_die),
7494 ),
7495 die_label="partial_unit2",
7496 ),
7497 ),
7498 )
7499 self.assertIdentical(prog.type("TEST").type, prog.int_type("int", 4, True))
7500
7501 def test_global_nested_alt(self):
7502 with tempfile.NamedTemporaryFile() as alt_f:
7503 alt_dwarf = compile_dwarf(
7504 (
7505 DwarfUnit(
7506 DW_UT.partial,
7507 DwarfDie(
7508 DW_TAG.partial_unit,
7509 (),
7510 (
7511 DwarfDie(
7512 DW_TAG.imported_unit,
7513 (
7514 DwarfAttrib(
7515 DW_AT.import_, DW_FORM.ref_addr, "alt_unit2"
7516 ),
7517 ),
7518 ),
7519 ),
7520 ),
7521 die_label="alt_unit",
7522 ),
7523 DwarfUnit(
7524 DW_UT.partial,
7525 DwarfDie(
7526 DW_TAG.partial_unit,
7527 (),
7528 wrap_test_type_dies(int_die),
7529 ),
7530 die_label="alt_unit2",
7531 ),
7532 ),
7533 build_id=self.alt_build_id,
7534 )
7535 alt_f.write(alt_dwarf.data)
7536 alt_f.flush()
7537
7538 prog = dwarf_program(
7539 (
7540 DwarfUnit(
7541 DW_UT.compile,
7542 DwarfDie(
7543 DW_TAG.compile_unit,
7544 (),
7545 (
7546 DwarfDie(
7547 DW_TAG.imported_unit,
7548 (
7549 DwarfAttrib(
7550 DW_AT.import_,
7551 DW_FORM.ref_addr,
7552 "partial_unit",
7553 ),
7554 ),
7555 ),
7556 ),
7557 ),
7558 ),
7559 DwarfUnit(
7560 DW_UT.partial,
7561 DwarfDie(
7562 DW_TAG.partial_unit,
7563 (),
7564 (
7565 DwarfDie(
7566 DW_TAG.imported_unit,
7567 (
7568 DwarfAttrib(
7569 DW_AT.import_,
7570 DW_FORM.GNU_ref_alt,
7571 alt_dwarf.labels["alt_unit"],
7572 ),
7573 ),
7574 ),
7575 ),
7576 ),
7577 die_label="partial_unit",
7578 ),
7579 ),
7580 gnu_debugaltlink=(alt_f.name, self.alt_build_id),
7581 )
7582 self.assertIdentical(prog.type("TEST").type, prog.int_type("int", 4, True))
7583
7584 def test_enumeration_type(self):
7585 prog = dwarf_program(
7586 (
7587 DwarfUnit(
7588 DW_UT.compile,
7589 DwarfDie(
7590 DW_TAG.compile_unit,
7591 (),
7592 (
7593 DwarfDie(
7594 DW_TAG.imported_unit,
7595 (
7596 DwarfAttrib(
7597 DW_AT.import_,
7598 DW_FORM.ref_addr,
7599 "partial_unit",
7600 ),
7601 ),
7602 ),
7603 ),
7604 ),
7605 ),
7606 DwarfUnit(
7607 DW_UT.partial,
7608 DwarfDie(
7609 DW_TAG.partial_unit,
7610 (),
7611 wrap_test_type_dies(
7612 DwarfDie(
7613 DW_TAG.enumeration_type,
7614 (
7615 DwarfAttrib(DW_AT.name, DW_FORM.string, "color"),
7616 DwarfAttrib(
7617 DW_AT.type, DW_FORM.ref4, "unsigned_int_die"
7618 ),
7619 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 4),
7620 ),
7621 (
7622 DwarfDie(
7623 DW_TAG.enumerator,
7624 (
7625 DwarfAttrib(
7626 DW_AT.name, DW_FORM.string, "RED"
7627 ),
7628 DwarfAttrib(
7629 DW_AT.const_value, DW_FORM.data1, 0
7630 ),
7631 ),
7632 ),
7633 DwarfDie(
7634 DW_TAG.enumerator,
7635 (
7636 DwarfAttrib(
7637 DW_AT.name, DW_FORM.string, "GREEN"
7638 ),
7639 DwarfAttrib(
7640 DW_AT.const_value, DW_FORM.data1, 1
7641 ),
7642 ),
7643 ),
7644 DwarfDie(
7645 DW_TAG.enumerator,
7646 (
7647 DwarfAttrib(
7648 DW_AT.name, DW_FORM.string, "BLUE"
7649 ),
7650 DwarfAttrib(
7651 DW_AT.const_value, DW_FORM.data1, 2
7652 ),
7653 ),
7654 ),
7655 ),
7656 ),
7657 *labeled_unsigned_int_die,
7658 ),
7659 ),
7660 die_label="partial_unit",
7661 ),
7662 ),
7663 )
7664 self.assertIdentical(
7665 prog.type("TEST").type,
7666 prog.enum_type(
7667 "color",
7668 prog.int_type("unsigned int", 4, False),
7669 (
7670 TypeEnumerator("RED", 0),
7671 TypeEnumerator("GREEN", 1),
7672 TypeEnumerator("BLUE", 2),
7673 ),
7674 ),
7675 )
7676
7677 def test_namespace(self):
7678 prog = dwarf_program(
7679 (
7680 DwarfUnit(
7681 DW_UT.partial,
7682 DwarfDie(
7683 DW_TAG.partial_unit,
7684 (
7685 # TODO: in practice, partial units don't seem to
7686 # have a language set, and it's supposed to be
7687 # inherited from the unit that imports it. We don't
7688 # handle that yet.
7689 DwarfAttrib(
7690 DW_AT.language,
7691 DW_FORM.data1,
7692 DW_LANG.C_plus_plus,
7693 ),
7694 ),
7695 (
7696 *labeled_int_die,
7697 DwarfDie(
7698 DW_TAG.namespace,
7699 (DwarfAttrib(DW_AT.name, DW_FORM.string, "foo"),),
7700 (
7701 DwarfDie(
7702 DW_TAG.typedef,
7703 (
7704 DwarfAttrib(
7705 DW_AT.name, DW_FORM.string, "TEST"
7706 ),
7707 DwarfAttrib(
7708 DW_AT.type, DW_FORM.ref4, "int_die"
7709 ),
7710 ),
7711 ),
7712 ),
7713 ),
7714 ),
7715 ),
7716 die_label="partial_unit",
7717 ),
7718 DwarfUnit(
7719 DW_UT.compile,
7720 DwarfDie(
7721 DW_TAG.compile_unit,
7722 (
7723 DwarfAttrib(
7724 DW_AT.language,
7725 DW_FORM.data1,
7726 DW_LANG.C_plus_plus,
7727 ),
7728 ),
7729 (
7730 DwarfDie(
7731 DW_TAG.imported_unit,
7732 (
7733 DwarfAttrib(
7734 DW_AT.import_,
7735 DW_FORM.ref_addr,
7736 "partial_unit",
7737 ),
7738 ),
7739 ),
7740 DwarfDie(
7741 DW_TAG.subprogram,
7742 (DwarfAttrib(DW_AT.name, DW_FORM.string, "main"),),
7743 ),
7744 ),
7745 ),
7746 ),
7747 ),
7748 )
7749 self.assertIdentical(prog.type("foo::TEST").type, prog.int_type("int", 4, True))
7750
7751 def test_namespace_alt(self):
7752 with tempfile.NamedTemporaryFile() as alt_f:
7753 alt_dwarf = compile_dwarf(
7754 (
7755 DwarfUnit(
7756 DW_UT.partial,
7757 DwarfDie(
7758 DW_TAG.partial_unit,
7759 (
7760 # See above re: language in partial units.
7761 DwarfAttrib(
7762 DW_AT.language,
7763 DW_FORM.data1,
7764 DW_LANG.C_plus_plus,
7765 ),
7766 ),
7767 (
7768 *labeled_int_die,
7769 DwarfDie(
7770 DW_TAG.namespace,
7771 (DwarfAttrib(DW_AT.name, DW_FORM.string, "foo"),),
7772 (
7773 DwarfDie(
7774 DW_TAG.typedef,
7775 (
7776 DwarfAttrib(
7777 DW_AT.name, DW_FORM.string, "TEST"
7778 ),
7779 DwarfAttrib(
7780 DW_AT.type, DW_FORM.ref4, "int_die"
7781 ),
7782 ),
7783 ),
7784 ),
7785 ),
7786 ),
7787 ),
7788 die_label="alt_unit",
7789 ),
7790 ),
7791 build_id=self.alt_build_id,
7792 )
7793 alt_f.write(alt_dwarf.data)
7794 alt_f.flush()
7795
7796 prog = dwarf_program(
7797 (
7798 DwarfUnit(
7799 DW_UT.compile,
7800 DwarfDie(
7801 DW_TAG.compile_unit,
7802 (
7803 DwarfAttrib(
7804 DW_AT.language,
7805 DW_FORM.data1,
7806 DW_LANG.C_plus_plus,
7807 ),
7808 ),
7809 (
7810 DwarfDie(
7811 DW_TAG.imported_unit,
7812 (
7813 DwarfAttrib(
7814 DW_AT.import_,
7815 DW_FORM.GNU_ref_alt,
7816 alt_dwarf.labels["alt_unit"],
7817 ),
7818 ),
7819 ),
7820 DwarfDie(
7821 DW_TAG.subprogram,
7822 (DwarfAttrib(DW_AT.name, DW_FORM.string, "main"),),
7823 ),
7824 ),
7825 ),
7826 ),
7827 ),
7828 gnu_debugaltlink=(alt_f.name, self.alt_build_id),
7829 )
7830 self.assertIdentical(
7831 prog.type("foo::TEST").type, prog.int_type("int", 4, True)
7832 )
7833
7834 def test_specification_imported(self):
7835 # DW_AT_specification in an imported unit referring to a
7836 # DW_AT_declaration DIE in a normal CU.
7837 prog = dwarf_program(
7838 (
7839 DwarfUnit(
7840 DW_UT.compile,
7841 DwarfDie(
7842 DW_TAG.compile_unit,
7843 (),
7844 wrap_test_type_dies(
7845 DwarfDie(
7846 DW_TAG.pointer_type,
7847 (
7848 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
7849 DwarfAttrib(
7850 DW_AT.type,
7851 DW_FORM.ref4,
7852 "incomplete_struct_die",
7853 ),
7854 ),
7855 ),
7856 DwarfLabel("incomplete_struct_die"),
7857 DwarfDie(
7858 DW_TAG.structure_type,
7859 (
7860 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
7861 DwarfAttrib(
7862 DW_AT.declaration,
7863 DW_FORM.flag_present,
7864 True,
7865 ),
7866 ),
7867 ),
7868 DwarfDie(
7869 DW_TAG.imported_unit,
7870 (
7871 DwarfAttrib(
7872 DW_AT.import_,
7873 DW_FORM.ref_addr,
7874 "partial_unit",
7875 ),
7876 ),
7877 ),
7878 ),
7879 ),
7880 ),
7881 DwarfUnit(
7882 DW_UT.partial,
7883 DwarfDie(
7884 DW_TAG.partial_unit,
7885 (),
7886 (
7887 DwarfDie(
7888 DW_TAG.structure_type,
7889 (
7890 DwarfAttrib(
7891 DW_AT.specification,
7892 DW_FORM.ref_addr,
7893 "incomplete_struct_die",
7894 ),
7895 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
7896 ),
7897 (
7898 DwarfDie(
7899 DW_TAG.member,
7900 (
7901 DwarfAttrib(
7902 DW_AT.name, DW_FORM.string, "x"
7903 ),
7904 DwarfAttrib(
7905 DW_AT.data_member_location,
7906 DW_FORM.data1,
7907 0,
7908 ),
7909 DwarfAttrib(
7910 DW_AT.type,
7911 DW_FORM.ref4,
7912 "int_die",
7913 ),
7914 ),
7915 ),
7916 DwarfDie(
7917 DW_TAG.member,
7918 (
7919 DwarfAttrib(
7920 DW_AT.name, DW_FORM.string, "y"
7921 ),
7922 DwarfAttrib(
7923 DW_AT.data_member_location,
7924 DW_FORM.data1,
7925 4,
7926 ),
7927 DwarfAttrib(
7928 DW_AT.type,
7929 DW_FORM.ref4,
7930 "int_die",
7931 ),
7932 ),
7933 ),
7934 ),
7935 ),
7936 *labeled_int_die,
7937 ),
7938 ),
7939 die_label="partial_unit",
7940 ),
7941 ),
7942 )
7943 self.assertIdentical(
7944 prog.type("TEST").type,
7945 prog.pointer_type(
7946 prog.struct_type(
7947 "point",
7948 8,
7949 (
7950 TypeMember(prog.int_type("int", 4, True), "x"),
7951 TypeMember(prog.int_type("int", 4, True), "y", 32),
7952 ),
7953 )
7954 ),
7955 )
7956
7957 def test_declaration_and_specification_imported(self):
7958 # DW_AT_specification in an imported unit referring to a
7959 # DW_AT_declaration DIE in the same imported unit.
7960 prog = dwarf_program(
7961 (
7962 DwarfUnit(
7963 DW_UT.compile,
7964 DwarfDie(
7965 DW_TAG.compile_unit,
7966 (),
7967 (
7968 DwarfDie(
7969 DW_TAG.imported_unit,
7970 (
7971 DwarfAttrib(
7972 DW_AT.import_,
7973 DW_FORM.ref_addr,
7974 "partial_unit",
7975 ),
7976 ),
7977 ),
7978 ),
7979 ),
7980 ),
7981 DwarfUnit(
7982 DW_UT.partial,
7983 DwarfDie(
7984 DW_TAG.partial_unit,
7985 (),
7986 wrap_test_type_dies(
7987 DwarfDie(
7988 DW_TAG.pointer_type,
7989 (
7990 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
7991 DwarfAttrib(
7992 DW_AT.type,
7993 DW_FORM.ref4,
7994 "incomplete_struct_die",
7995 ),
7996 ),
7997 ),
7998 DwarfLabel("incomplete_struct_die"),
7999 DwarfDie(
8000 DW_TAG.structure_type,
8001 (
8002 DwarfAttrib(DW_AT.name, DW_FORM.string, "point"),
8003 DwarfAttrib(
8004 DW_AT.declaration,
8005 DW_FORM.flag_present,
8006 True,
8007 ),
8008 ),
8009 ),
8010 DwarfDie(
8011 DW_TAG.structure_type,
8012 (
8013 DwarfAttrib(
8014 DW_AT.specification,
8015 DW_FORM.ref4,
8016 "incomplete_struct_die",
8017 ),
8018 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
8019 ),
8020 (
8021 DwarfDie(
8022 DW_TAG.member,
8023 (
8024 DwarfAttrib(
8025 DW_AT.name, DW_FORM.string, "x"
8026 ),
8027 DwarfAttrib(
8028 DW_AT.data_member_location,
8029 DW_FORM.data1,
8030 0,
8031 ),
8032 DwarfAttrib(
8033 DW_AT.type,
8034 DW_FORM.ref4,
8035 "int_die",
8036 ),
8037 ),
8038 ),
8039 DwarfDie(
8040 DW_TAG.member,
8041 (
8042 DwarfAttrib(
8043 DW_AT.name, DW_FORM.string, "y"
8044 ),
8045 DwarfAttrib(
8046 DW_AT.data_member_location,
8047 DW_FORM.data1,
8048 4,
8049 ),
8050 DwarfAttrib(
8051 DW_AT.type,
8052 DW_FORM.ref4,
8053 "int_die",
8054 ),
8055 ),
8056 ),
8057 ),
8058 ),
8059 *labeled_int_die,
8060 ),
8061 ),
8062 die_label="partial_unit",
8063 ),
8064 ),
8065 )
8066 self.assertIdentical(
8067 prog.type("TEST").type,
8068 prog.pointer_type(
8069 prog.struct_type(
8070 "point",
8071 8,
8072 (
8073 TypeMember(prog.int_type("int", 4, True), "x"),
8074 TypeMember(prog.int_type("int", 4, True), "y", 32),
8075 ),
8076 )
8077 ),
8078 )
8079
8080 def test_declaration_and_specification_alt(self):
8081 # DW_AT_specification in an imported unit from a .gnu_debugaltlink file
8082 # referring to a DW_AT_declaration DIE in the same imported unit.
8083 with tempfile.NamedTemporaryFile() as alt_f:
8084 alt_dwarf = compile_dwarf(
8085 (
8086 DwarfUnit(
8087 DW_UT.partial,
8088 DwarfDie(
8089 DW_TAG.partial_unit,
8090 (),
8091 wrap_test_type_dies(
8092 DwarfDie(
8093 DW_TAG.pointer_type,
8094 (
8095 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
8096 DwarfAttrib(
8097 DW_AT.type,
8098 DW_FORM.ref4,
8099 "incomplete_struct_die",
8100 ),
8101 ),
8102 ),
8103 DwarfLabel("incomplete_struct_die"),
8104 DwarfDie(
8105 DW_TAG.structure_type,
8106 (
8107 DwarfAttrib(
8108 DW_AT.name, DW_FORM.string, "point"
8109 ),
8110 DwarfAttrib(
8111 DW_AT.declaration,
8112 DW_FORM.flag_present,
8113 True,
8114 ),
8115 ),
8116 ),
8117 DwarfDie(
8118 DW_TAG.structure_type,
8119 (
8120 DwarfAttrib(
8121 DW_AT.specification,
8122 DW_FORM.ref4,
8123 "incomplete_struct_die",
8124 ),
8125 DwarfAttrib(DW_AT.byte_size, DW_FORM.data1, 8),
8126 ),
8127 (
8128 DwarfDie(
8129 DW_TAG.member,
8130 (
8131 DwarfAttrib(
8132 DW_AT.name, DW_FORM.string, "x"
8133 ),
8134 DwarfAttrib(
8135 DW_AT.data_member_location,
8136 DW_FORM.data1,
8137 0,
8138 ),
8139 DwarfAttrib(
8140 DW_AT.type, DW_FORM.ref4, "int_die"
8141 ),
8142 ),
8143 ),
8144 DwarfDie(
8145 DW_TAG.member,
8146 (
8147 DwarfAttrib(
8148 DW_AT.name, DW_FORM.string, "y"
8149 ),
8150 DwarfAttrib(
8151 DW_AT.data_member_location,
8152 DW_FORM.data1,
8153 4,
8154 ),
8155 DwarfAttrib(
8156 DW_AT.type, DW_FORM.ref4, "int_die"
8157 ),
8158 ),
8159 ),
8160 ),
8161 ),
8162 *labeled_int_die,
8163 ),
8164 ),
8165 die_label="alt_unit",
8166 ),
8167 ),
8168 build_id=self.alt_build_id,
8169 )
8170 alt_f.write(alt_dwarf.data)
8171 alt_f.flush()
8172
8173 prog = dwarf_program(
8174 (
8175 DwarfUnit(
8176 DW_UT.compile,
8177 DwarfDie(
8178 DW_TAG.compile_unit,
8179 (),
8180 (
8181 DwarfDie(
8182 DW_TAG.imported_unit,
8183 (
8184 DwarfAttrib(
8185 DW_AT.import_,
8186 DW_FORM.GNU_ref_alt,
8187 alt_dwarf.labels["alt_unit"],
8188 ),
8189 ),
8190 ),
8191 ),
8192 ),
8193 ),
8194 ),
8195 gnu_debugaltlink=(alt_f.name, self.alt_build_id),
8196 )
8197 self.assertIdentical(
8198 prog.type("TEST").type,
8199 prog.pointer_type(
8200 prog.struct_type(
8201 "point",
8202 8,
8203 (
8204 TypeMember(prog.int_type("int", 4, True), "x"),
8205 TypeMember(prog.int_type("int", 4, True), "y", 32),
8206 ),
8207 )
8208 ),
8209 )
8210
8211 def test_function_alt(self):
8212 with tempfile.NamedTemporaryFile() as alt_f:
8213 alt_dwarf = compile_dwarf(
8214 (
8215 DwarfUnit(
8216 DW_UT.partial,
8217 DwarfDie(
8218 DW_TAG.partial_unit,
8219 (),
8220 (
8221 *labeled_int_die,
8222 # DWP puts a subprogram DIE without
8223 # DW_AT_low_pc/DW_AT_ranges in the
8224 # supplementary file and another subprogram DIE
8225 # that references it with DW_AT_abstract_origin
8226 # in the main debug file.
8227 DwarfLabel("abstract_instance_root"),
8228 DwarfDie(
8229 DW_TAG.subprogram,
8230 (
8231 DwarfAttrib(DW_AT.name, DW_FORM.string, "abs"),
8232 DwarfAttrib(
8233 DW_AT.type, DW_FORM.ref4, "int_die"
8234 ),
8235 ),
8236 (
8237 DwarfLabel("abstract_instance_parameter"),
8238 DwarfDie(
8239 DW_TAG.formal_parameter,
8240 (
8241 DwarfAttrib(
8242 DW_AT.name, DW_FORM.string, "x"
8243 ),
8244 DwarfAttrib(
8245 DW_AT.type, DW_FORM.ref4, "int_die"
8246 ),
8247 ),
8248 ),
8249 ),
8250 ),
8251 ),
8252 ),
8253 die_label="alt_unit",
8254 ),
8255 ),
8256 build_id=self.alt_build_id,
8257 )
8258 alt_f.write(alt_dwarf.data)
8259 alt_f.flush()
8260
8261 prog = dwarf_program(
8262 (
8263 DwarfUnit(
8264 DW_UT.compile,
8265 DwarfDie(
8266 DW_TAG.compile_unit,
8267 (),
8268 (
8269 DwarfDie(
8270 DW_TAG.imported_unit,
8271 (
8272 DwarfAttrib(
8273 DW_AT.import_,
8274 DW_FORM.GNU_ref_alt,
8275 alt_dwarf.labels["alt_unit"],
8276 ),
8277 ),
8278 ),
8279 DwarfDie(
8280 DW_TAG.subprogram,
8281 (
8282 DwarfAttrib(
8283 DW_AT.abstract_origin,
8284 DW_FORM.GNU_ref_alt,
8285 alt_dwarf.labels["abstract_instance_root"],
8286 ),
8287 DwarfAttrib(
8288 DW_AT.low_pc,
8289 DW_FORM.addr,
8290 0x7FC3EB9B1C30,
8291 ),
8292 ),
8293 (
8294 DwarfDie(
8295 DW_TAG.formal_parameter,
8296 (
8297 DwarfAttrib(
8298 DW_AT.abstract_origin,
8299 DW_FORM.GNU_ref_alt,
8300 alt_dwarf.labels[
8301 "abstract_instance_parameter"
8302 ],
8303 ),
8304 ),
8305 ),
8306 ),
8307 ),
8308 ),
8309 ),
8310 ),
8311 ),
8312 gnu_debugaltlink=(alt_f.name, self.alt_build_id),
8313 )
8314 self.assertIdentical(
8315 prog["abs"],
8316 Object(
8317 prog,
8318 prog.function_type(
8319 prog.int_type("int", 4, True),
8320 (TypeParameter(prog.int_type("int", 4, True), "x"),),
8321 False,
8322 ),
8323 address=0x7FC3EB9B1C30,
8324 ),
8325 )
8326
8327 def test_unused_partial_unit(self):
8328 prog = dwarf_program(
8329 (
8330 DwarfUnit(
8331 DW_UT.compile,
8332 DwarfDie(
8333 DW_TAG.compile_unit,
8334 (),
8335 (
8336 DwarfDie(
8337 DW_TAG.typedef,
8338 (DwarfAttrib(DW_AT.name, DW_FORM.string, "TEST"),),
8339 ),
8340 ),
8341 ),
8342 ),
8343 DwarfUnit(
8344 DW_UT.partial,
8345 DwarfDie(
8346 DW_TAG.partial_unit,
8347 (),
8348 (
8349 DwarfDie(
8350 DW_TAG.typedef,
8351 (DwarfAttrib(DW_AT.name, DW_FORM.string, "UNUSED"),),
8352 ),
8353 ),
8354 ),
8355 ),
8356 ),
8357 )
8358 self.assertIdentical(prog.type("TEST").type, prog.void_type())
8359 self.assertRaises(LookupError, prog.type, "UNUSED")
8360
8361 def test_unused_partial_unit_alt(self):
8362 with tempfile.NamedTemporaryFile() as alt_f:
8363 alt_dwarf = compile_dwarf(
8364 (
8365 DwarfUnit(
8366 DW_UT.partial,
8367 DwarfDie(
8368 DW_TAG.partial_unit,
8369 (),
8370 (
8371 DwarfDie(
8372 DW_TAG.typedef,
8373 (DwarfAttrib(DW_AT.name, DW_FORM.string, "TEST"),),
8374 ),
8375 ),
8376 ),
8377 die_label="alt_unit",
8378 ),
8379 DwarfUnit(
8380 DW_UT.partial,
8381 DwarfDie(
8382 DW_TAG.partial_unit,
8383 (),
8384 (
8385 DwarfDie(
8386 DW_TAG.typedef,
8387 (
8388 DwarfAttrib(
8389 DW_AT.name, DW_FORM.string, "UNUSED"
8390 ),
8391 ),
8392 ),
8393 ),
8394 ),
8395 ),
8396 ),
8397 build_id=self.alt_build_id,
8398 )
8399 alt_f.write(alt_dwarf.data)
8400 alt_f.flush()
8401
8402 prog = dwarf_program(
8403 (
8404 DwarfUnit(
8405 DW_UT.compile,
8406 DwarfDie(
8407 DW_TAG.compile_unit,
8408 (),
8409 (
8410 DwarfDie(
8411 DW_TAG.imported_unit,
8412 (
8413 DwarfAttrib(
8414 DW_AT.import_,
8415 DW_FORM.GNU_ref_alt,
8416 alt_dwarf.labels["alt_unit"],
8417 ),
8418 ),
8419 ),
8420 ),
8421 ),
8422 ),
8423 ),
8424 gnu_debugaltlink=(alt_f.name, self.alt_build_id),
8425 )
8426 self.assertIdentical(prog.type("TEST").type, prog.void_type())
8427 self.assertRaises(LookupError, prog.type, "UNUSED")
8428
8429 def test_imported_unit_with_children(self):
8430 # DW_TAG_imported_unit shouldn't have children. Test that we ignore the
8431 # children properly and continue where we left off.
8432 prog = dwarf_program(
8433 (
8434 DwarfUnit(
8435 DW_UT.compile,
8436 DwarfDie(
8437 DW_TAG.compile_unit,
8438 (),
8439 (
8440 DwarfDie(
8441 DW_TAG.imported_unit,
8442 (
8443 DwarfAttrib(
8444 DW_AT.import_,
8445 DW_FORM.ref_addr,
8446 "partial_unit",
8447 ),
8448 ),
8449 (
8450 DwarfDie(
8451 DW_TAG.typedef,
8452 (
8453 DwarfAttrib(
8454 DW_AT.name, DW_FORM.string, "UNUSED"
8455 ),
8456 ),
8457 ),
8458 ),
8459 ),
8460 *labeled_unsigned_int_die,
8461 DwarfDie(
8462 DW_TAG.typedef,
8463 (
8464 DwarfAttrib(DW_AT.name, DW_FORM.string, "TEST2"),
8465 DwarfAttrib(
8466 DW_AT.type, DW_FORM.ref4, "unsigned_int_die"
8467 ),
8468 ),
8469 ),
8470 ),
8471 ),
8472 ),
8473 DwarfUnit(
8474 DW_UT.partial,
8475 DwarfDie(
8476 DW_TAG.partial_unit,
8477 (),
8478 wrap_test_type_dies(int_die),
8479 ),
8480 die_label="partial_unit",
8481 ),
8482 ),
8483 )
8484 self.assertIdentical(prog.type("TEST").type, prog.int_type("int", 4, True))
8485 self.assertIdentical(
8486 prog.type("TEST2").type, prog.int_type("unsigned int", 4, False)
8487 )
8488 self.assertRaises(LookupError, prog.type, "UNUSED")
8489
8490 def test_imported_unit_with_sibling(self):
8491 prog = dwarf_program(
8492 (
8493 DwarfUnit(
8494 DW_UT.compile,
8495 DwarfDie(
8496 DW_TAG.compile_unit,
8497 (),
8498 (
8499 DwarfDie(
8500 DW_TAG.imported_unit,
8501 (
8502 DwarfAttrib(
8503 DW_AT.import_,
8504 DW_FORM.ref_addr,
8505 "partial_unit",
8506 ),
8507 DwarfAttrib(
8508 DW_AT.sibling,
8509 DW_FORM.ref4,
8510 "TEST2_die",
8511 ),
8512 ),
8513 ),
8514 DwarfLabel("TEST2_die"),
8515 DwarfDie(
8516 DW_TAG.typedef,
8517 (
8518 DwarfAttrib(DW_AT.name, DW_FORM.string, "TEST2"),
8519 DwarfAttrib(
8520 DW_AT.type, DW_FORM.ref4, "unsigned_int_die"
8521 ),
8522 ),
8523 ),
8524 *labeled_unsigned_int_die,
8525 ),
8526 ),
8527 ),
8528 DwarfUnit(
8529 DW_UT.partial,
8530 DwarfDie(
8531 DW_TAG.partial_unit,
8532 (),
8533 wrap_test_type_dies(int_die),
8534 ),
8535 die_label="partial_unit",
8536 ),
8537 ),
8538 )
8539 self.assertIdentical(prog.type("TEST").type, prog.int_type("int", 4, True))
8540 self.assertIdentical(
8541 prog.type("TEST2").type, prog.int_type("unsigned int", 4, False)
8542 )
8543
8544 def test_top_level_imported_unit_with_children(self):
8545 prog = dwarf_program(
8546 (
8547 DwarfUnit(
8548 DW_UT.compile,
8549 DwarfDie(
8550 DW_TAG.imported_unit,
8551 (
8552 DwarfAttrib(
8553 DW_AT.import_,
8554 DW_FORM.ref_addr,
8555 "partial_unit",
8556 ),
8557 ),
8558 (
8559 DwarfDie(
8560 DW_TAG.typedef,
8561 (DwarfAttrib(DW_AT.name, DW_FORM.string, "foo"),),
8562 ),
8563 ),
8564 ),
8565 ),
8566 DwarfUnit(
8567 DW_UT.partial,
8568 DwarfDie(
8569 DW_TAG.partial_unit,
8570 (),
8571 (
8572 DwarfDie(
8573 DW_TAG.typedef,
8574 (DwarfAttrib(DW_AT.name, DW_FORM.string, "UNUSED"),),
8575 ),
8576 ),
8577 ),
8578 die_label="partial_unit",
8579 ),
8580 ),
8581 allow_any_unit_die=True,
8582 )
8583 self.assertRaises(LookupError, prog.type, "UNUSED")
8584
8585 def test_top_level_imported_unit(self):
8586 prog = dwarf_program(
8587 (
8588 DwarfUnit(
8589 DW_UT.compile,
8590 DwarfDie(
8591 DW_TAG.imported_unit,
8592 (
8593 DwarfAttrib(
8594 DW_AT.import_,
8595 DW_FORM.ref_addr,
8596 "partial_unit",
8597 ),
8598 ),
8599 ),
8600 ),
8601 DwarfUnit(
8602 DW_UT.partial,
8603 DwarfDie(
8604 DW_TAG.partial_unit,
8605 (),
8606 (
8607 DwarfDie(
8608 DW_TAG.typedef,
8609 (DwarfAttrib(DW_AT.name, DW_FORM.string, "UNUSED"),),
8610 ),
8611 ),
8612 ),
8613 die_label="partial_unit",
8614 ),
8615 ),
8616 allow_any_unit_die=True,
8617 )
8618 self.assertRaises(LookupError, prog.type, "UNUSED")
8619
8620 def test_missing_import(self):
8621 with self.assertRaisesRegex(
8622 Exception, "DW_TAG_imported_unit is missing DW_AT_import"
8623 ):
8624 "foo" in dwarf_program(
8625 (
8626 DwarfUnit(
8627 DW_UT.compile,
8628 DwarfDie(
8629 DW_TAG.compile_unit,
8630 (),
8631 (DwarfDie(DW_TAG.imported_unit),),
8632 ),
8633 ),
8634 ),
8635 )
8636
8637 def test_out_of_bounds(self):
8638 with self.assertRaisesRegex(Exception, "reference is out of bounds"):
8639 "foo" in dwarf_program(
8640 (
8641 DwarfUnit(
8642 DW_UT.compile,
8643 DwarfDie(
8644 DW_TAG.compile_unit,
8645 (),
8646 (
8647 DwarfDie(
8648 DW_TAG.imported_unit,
8649 (
8650 DwarfAttrib(
8651 DW_AT.import_,
8652 DW_FORM.ref4,
8653 0x100000,
8654 ),
8655 ),
8656 ),
8657 ),
8658 ),
8659 ),
8660 ),
8661 )
8662
8663 def test_out_of_bounds_alt(self):
8664 with tempfile.NamedTemporaryFile() as alt_f:
8665 alt_dwarf = compile_dwarf(
8666 (),
8667 build_id=self.alt_build_id,
8668 )
8669 alt_f.write(alt_dwarf.data)
8670 alt_f.flush()
8671
8672 with self.assertRaisesRegex(Exception, "reference is out of bounds"):
8673 "foo" in dwarf_program(
8674 (
8675 DwarfUnit(
8676 DW_UT.compile,
8677 DwarfDie(
8678 DW_TAG.compile_unit,
8679 (),
8680 (
8681 DwarfDie(
8682 DW_TAG.imported_unit,
8683 (
8684 DwarfAttrib(
8685 DW_AT.import_,
8686 DW_FORM.GNU_ref_alt,
8687 0x100000,
8688 ),
8689 ),
8690 ),
8691 ),
8692 ),
8693 ),
8694 ),
8695 gnu_debugaltlink=(alt_f.name, self.alt_build_id),
8696 )
8697
8698 def test_cycle(self):
8699 with self.assertRaisesRegex(
8700 Exception, "maximum DWARF imported unit depth exceeded"
8701 ):
8702 "foo" in dwarf_program(
8703 (
8704 DwarfUnit(
8705 DW_UT.compile,
8706 DwarfDie(
8707 DW_TAG.compile_unit,
8708 (),
8709 (
8710 DwarfDie(
8711 DW_TAG.imported_unit,
8712 (
8713 DwarfAttrib(
8714 DW_AT.import_,
8715 DW_FORM.ref4,
8716 "cycle_unit",
8717 ),
8718 ),
8719 ),
8720 ),
8721 ),
8722 die_label="cycle_unit",
8723 ),
8724 ),
8725 )
8726
8727 def test_cycle_alt(self):
8728 with tempfile.NamedTemporaryFile() as alt_f:
8729 alt_dwarf = compile_dwarf(
8730 (
8731 DwarfUnit(
8732 DW_UT.compile,
8733 DwarfDie(
8734 DW_TAG.partial_unit,
8735 (),
8736 (
8737 DwarfDie(
8738 DW_TAG.imported_unit,
8739 (
8740 DwarfAttrib(
8741 DW_AT.import_,
8742 DW_FORM.ref4,
8743 "cycle_unit",
8744 ),
8745 ),
8746 ),
8747 ),
8748 ),
8749 die_label="cycle_unit",
8750 ),
8751 ),
8752 build_id=self.alt_build_id,
8753 )
8754 alt_f.write(alt_dwarf.data)
8755 alt_f.flush()
8756
8757 with self.assertRaisesRegex(
8758 Exception, "maximum DWARF imported unit depth exceeded"
8759 ):
8760 "foo" in dwarf_program(
8761 (
8762 DwarfUnit(
8763 DW_UT.compile,
8764 DwarfDie(
8765 DW_TAG.compile_unit,
8766 (),
8767 (
8768 DwarfDie(
8769 DW_TAG.imported_unit,
8770 (
8771 DwarfAttrib(
8772 DW_AT.import_,
8773 DW_FORM.GNU_ref_alt,
8774 alt_dwarf.labels["cycle_unit"],
8775 ),
8776 ),
8777 ),
8778 ),
8779 ),
8780 ),
8781 ),
8782 gnu_debugaltlink=(alt_f.name, self.alt_build_id),
8783 )
8784
8785
8786 class TestSourceLocation(TestCase):
8787 @staticmethod
8788 def dwarf_lnp_program(*, dies=(), lnp, low_pc, high_pc, **kwds):
8789 return dwarf_program(
8790 DwarfUnit(
8791 DW_UT.compile,
8792 die=DwarfDie(
8793 DW_TAG.compile_unit,
8794 (
8795 DwarfAttrib(DW_AT.low_pc, DW_FORM.addr, low_pc),
8796 DwarfAttrib(DW_AT.high_pc, DW_FORM.addr, high_pc),
8797 ),
8798 dies,
8799 ),
8800 lnp=lnp,
8801 ),
8802 sections=(
8803 ElfSection(
8804 name=".text",
8805 sh_type=SHT.PROGBITS,
8806 p_type=PT.LOAD,
8807 vaddr=low_pc,
8808 memsz=high_pc - low_pc,
8809 sh_flags=SHF.ALLOC,
8810 ),
8811 ),
8812 address_range=(low_pc, high_pc),
8813 **kwds,
8814 )
8815
8816 def test_inlined(self):
8817 locs = self.dwarf_lnp_program(
8818 dies=(
8819 DwarfDie(
8820 DW_TAG.subprogram,
8821 (
8822 DwarfAttrib(DW_AT.name, DW_FORM.string, "caller"),
8823 DwarfAttrib(DW_AT.low_pc, DW_FORM.addr, 0x100),
8824 DwarfAttrib(DW_AT.high_pc, DW_FORM.addr, 0x108),
8825 ),
8826 (
8827 DwarfDie(
8828 DW_TAG.inlined_subroutine,
8829 (
8830 DwarfAttrib(
8831 DW_AT.abstract_origin, DW_FORM.ref4, "callee_die"
8832 ),
8833 DwarfAttrib(DW_AT.low_pc, DW_FORM.addr, 0x104),
8834 DwarfAttrib(DW_AT.high_pc, DW_FORM.addr, 0x108),
8835 DwarfAttrib(DW_AT.call_file, DW_FORM.udata, "main.c"),
8836 DwarfAttrib(DW_AT.call_line, DW_FORM.udata, 2),
8837 DwarfAttrib(DW_AT.call_column, DW_FORM.udata, 2),
8838 ),
8839 ),
8840 ),
8841 ),
8842 DwarfLabel("callee_die"),
8843 DwarfDie(
8844 DW_TAG.subprogram,
8845 (DwarfAttrib(DW_AT.name, DW_FORM.string, "callee"),),
8846 ),
8847 ),
8848 lnp=create_dwarf_lnp(
8849 (
8850 (0x100, "main.c", 1, 1),
8851 (0x104, "main.c", 6, 4),
8852 (0x108, "main.c", 10, 1),
8853 (0x10C, None, None, None),
8854 ),
8855 comp_dir="/usr/src",
8856 file_name="main.c",
8857 ),
8858 low_pc=0x100,
8859 high_pc=0x10C,
8860 ).source_location(0x104)
8861 self.assertEqual(len(locs), 2)
8862 self.assertEqual(tuple(locs[0]), ("/usr/src/main.c", 6, 4))
8863 self.assertEqual(locs[0].name(), "callee")
8864 self.assertEqual(tuple(locs[1]), ("/usr/src/main.c", 2, 2))
8865 self.assertEqual(locs[1].name(), "caller")
8866
8867 def test_assembly(self):
8868 prog = self.dwarf_lnp_program(
8869 lnp=create_dwarf_lnp(
8870 (
8871 (0x100, "main.S", 2, 1),
8872 (0x104, "main.S", 4, 1),
8873 (0x108, "main.S", 3, 1),
8874 (0x10C, None, None, None),
8875 ),
8876 comp_dir="/usr/src",
8877 file_name="main.S",
8878 ),
8879 low_pc=0x100,
8880 high_pc=0x10C,
8881 symbols=(
8882 ElfSymbol(
8883 name="asm_func",
8884 value=0x100,
8885 size=0xC,
8886 type=STT.FUNC,
8887 binding=STB.GLOBAL,
8888 shindex=1,
8889 ),
8890 ),
8891 )
8892
8893 with self.subTest("address"):
8894 locs = prog.source_location(0x104)
8895 self.assertEqual(len(locs), 1)
8896 self.assertEqual(tuple(locs[0]), ("/usr/src/main.S", 4, 1))
8897 self.assertEqual(locs[0].name(), "asm_func")
8898
8899 with self.subTest("address_str"):
8900 locs = prog.source_location("0x104")
8901 self.assertEqual(len(locs), 1)
8902 self.assertEqual(tuple(locs[0]), ("/usr/src/main.S", 4, 1))
8903 self.assertEqual(locs[0].name(), "asm_func")
8904
8905 with self.subTest("symbol_name"):
8906 locs = prog.source_location("asm_func")
8907 self.assertEqual(len(locs), 1)
8908 self.assertEqual(tuple(locs[0]), ("/usr/src/main.S", 2, 1))
8909 self.assertEqual(locs[0].name(), "asm_func")
8910
8911 with self.subTest("symbol_name+offset"):
8912 locs = prog.source_location("asm_func+0x8")
8913 self.assertEqual(len(locs), 1)
8914 self.assertEqual(tuple(locs[0]), ("/usr/src/main.S", 3, 1))
8915 self.assertEqual(locs[0].name(), "asm_func")
8916
8917 def test_module_not_found(self):
8918 self.assertRaisesRegex(
8919 LookupError,
8920 "source code location not found",
8921 dwarf_program(()).source_location,
8922 0x100,
8923 )
8924
8925 def test_cu_not_found(self):
8926 self.assertRaisesRegex(
8927 LookupError,
8928 "source code location not found",
8929 dwarf_program(
8930 (),
8931 address_range=(0x100, 0x10C),
8932 ).source_location,
8933 0x100,
8934 )