]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/lib/dwarf.exp
gdb/testsuite: Move helper function into lib/dwarf.exp
[thirdparty/binutils-gdb.git] / gdb / testsuite / lib / dwarf.exp
1 # Copyright 2010-2020 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # Return true if the target supports DWARF-2 and uses gas.
17 # For now pick a sampling of likely targets.
18 proc dwarf2_support {} {
19 if {[istarget *-*-linux*]
20 || [istarget *-*-gnu*]
21 || [istarget *-*-elf*]
22 || [istarget *-*-openbsd*]
23 || [istarget arm*-*-eabi*]
24 || [istarget arm*-*-symbianelf*]
25 || [istarget powerpc-*-eabi*]} {
26 return 1
27 }
28
29 return 0
30 }
31
32 # Build an executable from a fission-based .S file.
33 # This handles the extra work of splitting the .o into non-dwo and dwo
34 # pieces, making sure the .dwo is available if we're using cc-with-tweaks.sh
35 # to build a .dwp file.
36 # The arguments and results are the same as for build_executable.
37 #
38 # Current restrictions:
39 # - only supports one source file
40 # - cannot be run on remote hosts
41
42 proc build_executable_from_fission_assembler { testname executable sources options } {
43 verbose -log "build_executable_from_fission_assembler $testname $executable $sources $options"
44 if { [llength $sources] != 1 } {
45 error "Only one source file supported."
46 }
47 if [is_remote host] {
48 error "Remote hosts are not supported."
49 }
50
51 global srcdir subdir
52 set source_file ${srcdir}/${subdir}/${sources}
53 set root_name [file rootname [file tail $source_file]]
54 set output_base [standard_output_file $root_name]
55 set object_file ${output_base}.o
56 set dwo_file ${output_base}.dwo
57 set object_options "object $options"
58 set objcopy [gdb_find_objcopy]
59
60 set result [gdb_compile $source_file $object_file object $options]
61 if { "$result" != "" } {
62 return -1
63 }
64
65 set command "$objcopy --extract-dwo $object_file $dwo_file"
66 verbose -log "Executing $command"
67 set result [catch "exec $command" output]
68 verbose -log "objcopy --extract-dwo output: $output"
69 if { $result == 1 } {
70 return -1
71 }
72
73 set command "$objcopy --strip-dwo $object_file"
74 verbose -log "Executing $command"
75 set result [catch "exec $command" output]
76 verbose -log "objcopy --strip-dwo output: $output"
77 if { $result == 1 } {
78 return -1
79 }
80
81 set result [gdb_compile $object_file $executable executable $options]
82 if { "$result" != "" } {
83 return -1
84 }
85
86 return 0
87 }
88
89 # Return a list of expressions about function FUNC's address and length.
90 # The first expression is the address of function FUNC, and the second
91 # one is FUNC's length. SRC is the source file having function FUNC.
92 # An internal label ${func}_label must be defined inside FUNC:
93 #
94 # int main (void)
95 # {
96 # asm ("main_label: .globl main_label");
97 # return 0;
98 # }
99 #
100 # This label is needed to compute the start address of function FUNC.
101 # If the compiler is gcc, we can do the following to get function start
102 # and end address too:
103 #
104 # asm ("func_start: .globl func_start");
105 # static void func (void) {}
106 # asm ("func_end: .globl func_end");
107 #
108 # however, this isn't portable, because other compilers, such as clang,
109 # may not guarantee the order of global asms and function. The code
110 # becomes:
111 #
112 # asm ("func_start: .globl func_start");
113 # asm ("func_end: .globl func_end");
114 # static void func (void) {}
115 #
116
117 proc function_range { func src {options {debug}} } {
118 global decimal gdb_prompt
119
120 set exe [standard_temp_file func_addr[pid].x]
121
122 gdb_compile $src $exe executable $options
123
124 gdb_exit
125 gdb_start
126 gdb_load "$exe"
127
128 # Compute the label offset, and we can get the function start address
129 # by "${func}_label - $func_label_offset".
130 set func_label_offset ""
131 set test "p ${func}_label - ${func}"
132 gdb_test_multiple $test $test {
133 -re ".* = ($decimal)\r\n$gdb_prompt $" {
134 set func_label_offset $expect_out(1,string)
135 }
136 }
137
138 # Compute the function length.
139 global hex
140 set func_length ""
141 set test "disassemble $func"
142 gdb_test_multiple $test $test {
143 -re ".*$hex <\\+($decimal)>:\[^\r\n\]+\r\nEnd of assembler dump\.\r\n$gdb_prompt $" {
144 set func_length $expect_out(1,string)
145 }
146 }
147
148 # Compute the size of the last instruction.
149 if { $func_length == 0 } then {
150 set func_pattern "$func"
151 } else {
152 set func_pattern "$func\\+$func_length"
153 }
154 set test "x/2i $func+$func_length"
155 gdb_test_multiple $test $test {
156 -re ".*($hex) <$func_pattern>:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
157 set start $expect_out(1,string)
158 set end $expect_out(2,string)
159
160 set func_length [expr $func_length + $end - $start]
161 }
162 }
163
164 return [list "${func}_label - $func_label_offset" $func_length]
165 }
166
167 # Extract the start, length, and end for function called NAME and
168 # create suitable variables in the callers scope.
169 proc get_func_info { name {options {debug}} } {
170 global srcdir subdir srcfile
171
172 upvar 1 "${name}_start" func_start
173 upvar 1 "${name}_len" func_len
174 upvar 1 "${name}_end" func_end
175
176 lassign [function_range ${name} \
177 [list ${srcdir}/${subdir}/$srcfile] \
178 ${options}] \
179 func_start func_len
180 set func_end "$func_start + $func_len"
181 }
182
183 # A DWARF assembler.
184 #
185 # All the variables in this namespace are private to the
186 # implementation. Also, any procedure whose name starts with "_" is
187 # private as well. Do not use these.
188 #
189 # Exported functions are documented at their definition.
190 #
191 # In addition to the hand-written functions documented below, this
192 # module automatically generates a function for each DWARF tag. For
193 # most tags, two forms are made: a full name, and one with the
194 # "DW_TAG_" prefix stripped. For example, you can use either
195 # 'DW_TAG_compile_unit' or 'compile_unit' interchangeably.
196 #
197 # There are two exceptions to this rule: DW_TAG_variable and
198 # DW_TAG_namespace. For these, the full name must always be used,
199 # as the short name conflicts with Tcl builtins. (Should future
200 # versions of Tcl or DWARF add more conflicts, this list will grow.
201 # If you want to be safe you should always use the full names.)
202 #
203 # Each tag procedure is defined like:
204 #
205 # proc DW_TAG_mumble {{attrs {}} {children {}}} { ... }
206 #
207 # ATTRS is an optional list of attributes.
208 # It is run through 'subst' in the caller's context before processing.
209 #
210 # Each attribute in the list has one of two forms:
211 # 1. { NAME VALUE }
212 # 2. { NAME VALUE FORM }
213 #
214 # In each case, NAME is the attribute's name.
215 # This can either be the full name, like 'DW_AT_name', or a shortened
216 # name, like 'name'. These are fully equivalent.
217 #
218 # Besides DWARF standard attributes, assembler supports 'macro' attribute
219 # which will be substituted by one or more standard or macro attributes.
220 # supported macro attributes are:
221 #
222 # - MACRO_AT_range { FUNC FILE }
223 # It is substituted by DW_AT_low_pc and DW_AT_high_pc with the start and
224 # end address of function FUNC in file FILE.
225 #
226 # - MACRO_AT_func { FUNC FILE }
227 # It is substituted by DW_AT_name with FUNC and MACRO_AT_range.
228 #
229 # If FORM is given, it should name a DW_FORM_ constant.
230 # This can either be the short form, like 'DW_FORM_addr', or a
231 # shortened version, like 'addr'. If the form is given, VALUE
232 # is its value; see below. In some cases, additional processing
233 # is done; for example, DW_FORM_strp manages the .debug_str
234 # section automatically.
235 #
236 # If FORM is 'SPECIAL_expr', then VALUE is treated as a location
237 # expression. The effective form is then DW_FORM_block, and VALUE
238 # is passed to the (internal) '_location' proc to be translated.
239 # This proc implements a miniature DW_OP_ assembler.
240 #
241 # If FORM is not given, it is guessed:
242 # * If VALUE starts with the "@" character, the rest of VALUE is
243 # looked up as a DWARF constant, and DW_FORM_sdata is used. For
244 # example, '@DW_LANG_c89' could be used.
245 # * If VALUE starts with the ":" character, then it is a label
246 # reference. The rest of VALUE is taken to be the name of a label,
247 # and DW_FORM_ref4 is used. See 'new_label' and 'define_label'.
248 # * If VALUE starts with the "%" character, then it is a label
249 # reference too, but DW_FORM_ref_addr is used.
250 # * Otherwise, VALUE is taken to be a string and DW_FORM_string is
251 # used. In order to prevent bugs where a numeric value is given but
252 # no form is specified, it is an error if the value looks like a number
253 # (using Tcl's "string is integer") and no form is provided.
254 # More form-guessing functionality may be added.
255 #
256 # CHILDREN is just Tcl code that can be used to define child DIEs. It
257 # is evaluated in the caller's context.
258 #
259 # Currently this code is missing nice support for CFA handling, and
260 # probably other things as well.
261
262 namespace eval Dwarf {
263 # True if the module has been initialized.
264 variable _initialized 0
265
266 # Constants from dwarf2.h.
267 variable _constants
268 # DW_AT short names.
269 variable _AT
270 # DW_FORM short names.
271 variable _FORM
272 # DW_OP short names.
273 variable _OP
274
275 # The current output file.
276 variable _output_file
277
278 # Note: The _cu_ values here also apply to type units (TUs).
279 # Think of a TU as a special kind of CU.
280
281 # Current CU count.
282 variable _cu_count
283
284 # The current CU's base label.
285 variable _cu_label
286
287 # The current CU's version.
288 variable _cu_version
289
290 # The current CU's address size.
291 variable _cu_addr_size
292 # The current CU's offset size.
293 variable _cu_offset_size
294
295 # Label generation number.
296 variable _label_num
297
298 # The deferred output array. The index is the section name; the
299 # contents hold the data for that section.
300 variable _deferred_output
301
302 # If empty, we should write directly to the output file.
303 # Otherwise, this is the name of a section to write to.
304 variable _defer
305
306 # The abbrev section. Typically .debug_abbrev but can be .debug_abbrev.dwo
307 # for Fission.
308 variable _abbrev_section
309
310 # The next available abbrev number in the current CU's abbrev
311 # table.
312 variable _abbrev_num
313
314 # The string table for this assembly. The key is the string; the
315 # value is the label for that string.
316 variable _strings
317
318 # Current .debug_line unit count.
319 variable _line_count
320
321 # Whether a file_name entry was seen.
322 variable _line_saw_file
323
324 # Whether a line table program has been seen.
325 variable _line_saw_program
326
327 # A Label for line table header generation.
328 variable _line_header_end_label
329
330 # The address size for debug ranges section.
331 variable _debug_ranges_64_bit
332
333 proc _process_one_constant {name value} {
334 variable _constants
335 variable _AT
336 variable _FORM
337 variable _OP
338
339 set _constants($name) $value
340
341 if {![regexp "^DW_(\[A-Z\]+)_(\[A-Za-z0-9_\]+)$" $name \
342 ignore prefix name2]} {
343 error "non-matching name: $name"
344 }
345
346 if {$name2 == "lo_user" || $name2 == "hi_user"} {
347 return
348 }
349
350 # We only try to shorten some very common things.
351 # FIXME: CFA?
352 switch -exact -- $prefix {
353 TAG {
354 # Create two procedures for the tag. These call
355 # _handle_DW_TAG with the full tag name baked in; this
356 # does all the actual work.
357 proc $name {{attrs {}} {children {}}} \
358 "_handle_DW_TAG $name \$attrs \$children"
359
360 # Filter out ones that are known to clash.
361 if {$name2 == "variable" || $name2 == "namespace"} {
362 set name2 "tag_$name2"
363 }
364
365 if {[info commands $name2] != {}} {
366 error "duplicate proc name: from $name"
367 }
368
369 proc $name2 {{attrs {}} {children {}}} \
370 "_handle_DW_TAG $name \$attrs \$children"
371 }
372
373 AT {
374 set _AT($name2) $name
375 }
376
377 FORM {
378 set _FORM($name2) $name
379 }
380
381 OP {
382 set _OP($name2) $name
383 }
384
385 default {
386 return
387 }
388 }
389 }
390
391 proc _read_constants {} {
392 global srcdir hex decimal
393 variable _constants
394
395 # DWARF name-matching regexp.
396 set dwrx "DW_\[a-zA-Z0-9_\]+"
397 # Whitespace regexp.
398 set ws "\[ \t\]+"
399
400 set fd [open [file join $srcdir .. .. include dwarf2.h]]
401 while {![eof $fd]} {
402 set line [gets $fd]
403 if {[regexp -- "^${ws}($dwrx)${ws}=${ws}($hex|$decimal),?$" \
404 $line ignore name value ignore2]} {
405 _process_one_constant $name $value
406 }
407 }
408 close $fd
409
410 set fd [open [file join $srcdir .. .. include dwarf2.def]]
411 while {![eof $fd]} {
412 set line [gets $fd]
413 if {[regexp -- \
414 "^DW_\[A-Z_\]+${ws}\\(($dwrx),${ws}($hex|$decimal)\\)$" \
415 $line ignore name value ignore2]} {
416 _process_one_constant $name $value
417 }
418 }
419 close $fd
420
421 set _constants(SPECIAL_expr) $_constants(DW_FORM_block)
422 }
423
424 proc _quote {string} {
425 # FIXME
426 return "\"${string}\\0\""
427 }
428
429 proc _nz_quote {string} {
430 # For now, no quoting is done.
431 return "\"${string}\""
432 }
433
434 proc _handle_DW_FORM {form value} {
435 switch -exact -- $form {
436 DW_FORM_string {
437 _op .ascii [_quote $value]
438 }
439
440 DW_FORM_flag_present {
441 # We don't need to emit anything.
442 }
443
444 DW_FORM_data4 -
445 DW_FORM_ref4 {
446 _op .4byte $value
447 }
448
449 DW_FORM_ref_addr {
450 variable _cu_offset_size
451 variable _cu_version
452 variable _cu_addr_size
453
454 if {$_cu_version == 2} {
455 set size $_cu_addr_size
456 } else {
457 set size $_cu_offset_size
458 }
459
460 _op .${size}byte $value
461 }
462
463 DW_FORM_sec_offset {
464 variable _cu_offset_size
465 _op .${_cu_offset_size}byte $value
466 }
467
468 DW_FORM_ref1 -
469 DW_FORM_flag -
470 DW_FORM_data1 {
471 _op .byte $value
472 }
473
474 DW_FORM_sdata {
475 _op .sleb128 $value
476 }
477
478 DW_FORM_ref_udata -
479 DW_FORM_udata {
480 _op .uleb128 $value
481 }
482
483 DW_FORM_addr {
484 variable _cu_addr_size
485
486 _op .${_cu_addr_size}byte $value
487 }
488
489 DW_FORM_data2 -
490 DW_FORM_ref2 {
491 _op .2byte $value
492 }
493
494 DW_FORM_data8 -
495 DW_FORM_ref8 -
496 DW_FORM_ref_sig8 {
497 _op .8byte $value
498 }
499
500 DW_FORM_data16 {
501 _op .8byte $value
502 }
503
504 DW_FORM_strp {
505 variable _strings
506 variable _cu_offset_size
507
508 if {![info exists _strings($value)]} {
509 set _strings($value) [new_label strp]
510 _defer_output .debug_string {
511 define_label $_strings($value)
512 _op .ascii [_quote $value]
513 }
514 }
515
516 _op .${_cu_offset_size}byte $_strings($value) "strp: $value"
517 }
518
519 SPECIAL_expr {
520 set l1 [new_label "expr_start"]
521 set l2 [new_label "expr_end"]
522 _op .uleb128 "$l2 - $l1" "expression"
523 define_label $l1
524 _location $value
525 define_label $l2
526 }
527
528 DW_FORM_block1 {
529 set len [string length $value]
530 if {$len > 255} {
531 error "DW_FORM_block1 length too long"
532 }
533 _op .byte $len
534 _op .ascii [_nz_quote $value]
535 }
536
537 DW_FORM_block2 -
538 DW_FORM_block4 -
539
540 DW_FORM_block -
541
542 DW_FORM_ref2 -
543 DW_FORM_indirect -
544 DW_FORM_exprloc -
545
546 DW_FORM_strx -
547 DW_FORM_strx1 -
548 DW_FORM_strx2 -
549 DW_FORM_strx3 -
550 DW_FORM_strx4 -
551
552 DW_FORM_GNU_addr_index -
553 DW_FORM_GNU_str_index -
554 DW_FORM_GNU_ref_alt -
555 DW_FORM_GNU_strp_alt -
556
557 default {
558 error "unhandled form $form"
559 }
560 }
561 }
562
563 proc _guess_form {value varname} {
564 upvar $varname new_value
565
566 switch -exact -- [string range $value 0 0] {
567 @ {
568 # Constant reference.
569 variable _constants
570
571 set new_value $_constants([string range $value 1 end])
572 # Just the simplest.
573 return DW_FORM_sdata
574 }
575
576 : {
577 # Label reference.
578 variable _cu_label
579
580 set new_value "[string range $value 1 end] - $_cu_label"
581
582 return DW_FORM_ref4
583 }
584
585 % {
586 # Label reference, an offset from .debug_info.
587 set new_value "[string range $value 1 end]"
588
589 return DW_FORM_ref_addr
590 }
591
592 default {
593 return DW_FORM_string
594 }
595 }
596 }
597
598 # Map NAME to its canonical form.
599 proc _map_name {name ary} {
600 variable $ary
601
602 if {[info exists ${ary}($name)]} {
603 set name [set ${ary}($name)]
604 }
605
606 return $name
607 }
608
609 proc _handle_attribute { attr_name attr_value attr_form } {
610 variable _abbrev_section
611 variable _constants
612
613 _handle_DW_FORM $attr_form $attr_value
614
615 _defer_output $_abbrev_section {
616 _op .uleb128 $_constants($attr_name) $attr_name
617 _op .uleb128 $_constants($attr_form) $attr_form
618 }
619 }
620
621 # Handle macro attribute MACRO_AT_range.
622
623 proc _handle_macro_at_range { attr_value } {
624 if {[llength $attr_value] != 2} {
625 error "usage: MACRO_AT_range { func file }"
626 }
627
628 set func [lindex $attr_value 0]
629 set src [lindex $attr_value 1]
630 set result [function_range $func $src]
631
632 _handle_attribute DW_AT_low_pc [lindex $result 0] \
633 DW_FORM_addr
634 _handle_attribute DW_AT_high_pc \
635 "[lindex $result 0] + [lindex $result 1]" DW_FORM_addr
636 }
637
638 # Handle macro attribute MACRO_AT_func.
639
640 proc _handle_macro_at_func { attr_value } {
641 if {[llength $attr_value] != 2} {
642 error "usage: MACRO_AT_func { func file }"
643 }
644 _handle_attribute DW_AT_name [lindex $attr_value 0] DW_FORM_string
645 _handle_macro_at_range $attr_value
646 }
647
648 proc _handle_DW_TAG {tag_name {attrs {}} {children {}}} {
649 variable _abbrev_section
650 variable _abbrev_num
651 variable _constants
652
653 set has_children [expr {[string length $children] > 0}]
654 set my_abbrev [incr _abbrev_num]
655
656 # We somewhat wastefully emit a new abbrev entry for each tag.
657 # There's no reason for this other than laziness.
658 _defer_output $_abbrev_section {
659 _op .uleb128 $my_abbrev "Abbrev start"
660 _op .uleb128 $_constants($tag_name) $tag_name
661 _op .byte $has_children "has_children"
662 }
663
664 _op .uleb128 $my_abbrev "Abbrev ($tag_name)"
665
666 foreach attr $attrs {
667 set attr_name [_map_name [lindex $attr 0] _AT]
668
669 # When the length of ATTR is greater than 2, the last
670 # element of the list must be a form. The second through
671 # the penultimate elements are joined together and
672 # evaluated using subst. This allows constructs such as
673 # [gdb_target_symbol foo] to be used.
674
675 if {[llength $attr] > 2} {
676 set attr_value [uplevel 2 [list subst [join [lrange $attr 1 end-1]]]]
677 } else {
678 set attr_value [uplevel 2 [list subst [lindex $attr 1]]]
679 }
680
681 if { [string equal "MACRO_AT_func" $attr_name] } {
682 _handle_macro_at_func $attr_value
683 } elseif { [string equal "MACRO_AT_range" $attr_name] } {
684 _handle_macro_at_range $attr_value
685 } else {
686 if {[llength $attr] > 2} {
687 set attr_form [uplevel 2 [list subst [lindex $attr end]]]
688
689 if { [string index $attr_value 0] == ":" } {
690 # It is a label, get its value.
691 _guess_form $attr_value attr_value
692 }
693 } else {
694 # If the value looks like an integer, a form is required.
695 if [string is integer $attr_value] {
696 error "Integer value requires a form"
697 }
698 set attr_form [_guess_form $attr_value attr_value]
699 }
700 set attr_form [_map_name $attr_form _FORM]
701
702 _handle_attribute $attr_name $attr_value $attr_form
703 }
704 }
705
706 _defer_output $_abbrev_section {
707 # Terminator.
708 _op .byte 0x0 Terminator
709 _op .byte 0x0 Terminator
710 }
711
712 if {$has_children} {
713 uplevel 2 $children
714
715 # Terminate children.
716 _op .byte 0x0 "Terminate children"
717 }
718 }
719
720 proc _emit {string} {
721 variable _output_file
722 variable _defer
723 variable _deferred_output
724
725 if {$_defer == ""} {
726 puts $_output_file $string
727 } else {
728 append _deferred_output($_defer) ${string}\n
729 }
730 }
731
732 proc _section {name {flags ""} {type ""}} {
733 if {$flags == "" && $type == ""} {
734 _emit " .section $name"
735 } elseif {$type == ""} {
736 _emit " .section $name, \"$flags\""
737 } else {
738 _emit " .section $name, \"$flags\", %$type"
739 }
740 }
741
742 # SECTION_SPEC is a list of arguments to _section.
743 proc _defer_output {section_spec body} {
744 variable _defer
745 variable _deferred_output
746
747 set old_defer $_defer
748 set _defer [lindex $section_spec 0]
749
750 if {![info exists _deferred_output($_defer)]} {
751 set _deferred_output($_defer) ""
752 eval _section $section_spec
753 }
754
755 uplevel $body
756
757 set _defer $old_defer
758 }
759
760 proc _defer_to_string {body} {
761 variable _defer
762 variable _deferred_output
763
764 set old_defer $_defer
765 set _defer temp
766
767 set _deferred_output($_defer) ""
768
769 uplevel $body
770
771 set result $_deferred_output($_defer)
772 unset _deferred_output($_defer)
773
774 set _defer $old_defer
775 return $result
776 }
777
778 proc _write_deferred_output {} {
779 variable _output_file
780 variable _deferred_output
781
782 foreach section [array names _deferred_output] {
783 # The data already has a newline.
784 puts -nonewline $_output_file $_deferred_output($section)
785 }
786
787 # Save some memory.
788 unset _deferred_output
789 }
790
791 proc _op {name value {comment ""}} {
792 set text " ${name} ${value}"
793 if {$comment != ""} {
794 # Try to make stuff line up nicely.
795 while {[string length $text] < 40} {
796 append text " "
797 }
798 append text "/* ${comment} */"
799 }
800 _emit $text
801 }
802
803 proc _compute_label {name} {
804 return ".L${name}"
805 }
806
807 # Return a name suitable for use as a label. If BASE_NAME is
808 # specified, it is incorporated into the label name; this is to
809 # make debugging the generated assembler easier. If BASE_NAME is
810 # not specified a generic default is used. This proc does not
811 # define the label; see 'define_label'. 'new_label' attempts to
812 # ensure that label names are unique.
813 proc new_label {{base_name label}} {
814 variable _label_num
815
816 return [_compute_label ${base_name}[incr _label_num]]
817 }
818
819 # Define a label named NAME. Ordinarily, NAME comes from a call
820 # to 'new_label', but this is not required.
821 proc define_label {name} {
822 _emit "${name}:"
823 }
824
825 # Declare a global label. This is typically used to refer to
826 # labels defined in other files, for example a function defined in
827 # a .c file.
828 proc extern {args} {
829 foreach name $args {
830 _op .global $name
831 }
832 }
833
834 # A higher-level interface to label handling.
835 #
836 # ARGS is a list of label descriptors. Each one is either a
837 # single element, or a list of two elements -- a name and some
838 # text. For each descriptor, 'new_label' is invoked. If the list
839 # form is used, the second element in the list is passed as an
840 # argument. The label name is used to define a variable in the
841 # enclosing scope; this can be used to refer to the label later.
842 # The label name is also used to define a new proc whose name is
843 # the label name plus a trailing ":". This proc takes a body as
844 # an argument and can be used to define the label at that point;
845 # then the body, if any, is evaluated in the caller's context.
846 #
847 # For example:
848 #
849 # declare_labels int_label
850 # something { ... $int_label } ;# refer to the label
851 # int_label: constant { ... } ;# define the label
852 proc declare_labels {args} {
853 foreach arg $args {
854 set name [lindex $arg 0]
855 set text [lindex $arg 1]
856
857 upvar $name label_var
858 if {$text == ""} {
859 set label_var [new_label]
860 } else {
861 set label_var [new_label $text]
862 }
863
864 proc ${name}: {args} [format {
865 define_label %s
866 uplevel $args
867 } $label_var]
868 }
869 }
870
871 # This is a miniature assembler for location expressions. It is
872 # suitable for use in the attributes to a DIE. Its output is
873 # prefixed with "=" to make it automatically use DW_FORM_block.
874 # BODY is split by lines, and each line is taken to be a list.
875 # (FIXME should use 'info complete' here.)
876 # Each list's first element is the opcode, either short or long
877 # forms are accepted.
878 # FIXME argument handling
879 # FIXME move docs
880 proc _location {body} {
881 variable _constants
882 variable _cu_label
883 variable _cu_version
884 variable _cu_addr_size
885 variable _cu_offset_size
886
887 foreach line [split $body \n] {
888 # Ignore blank lines, and allow embedded comments.
889 if {[lindex $line 0] == "" || [regexp -- {^[ \t]*#} $line]} {
890 continue
891 }
892 set opcode [_map_name [lindex $line 0] _OP]
893 _op .byte $_constants($opcode) $opcode
894
895 switch -exact -- $opcode {
896 DW_OP_addr {
897 _op .${_cu_addr_size}byte [lindex $line 1]
898 }
899
900 DW_OP_regx {
901 _op .uleb128 [lindex $line 1]
902 }
903
904 DW_OP_pick -
905 DW_OP_const1u -
906 DW_OP_const1s {
907 _op .byte [lindex $line 1]
908 }
909
910 DW_OP_const2u -
911 DW_OP_const2s {
912 _op .2byte [lindex $line 1]
913 }
914
915 DW_OP_const4u -
916 DW_OP_const4s {
917 _op .4byte [lindex $line 1]
918 }
919
920 DW_OP_const8u -
921 DW_OP_const8s {
922 _op .8byte [lindex $line 1]
923 }
924
925 DW_OP_constu {
926 _op .uleb128 [lindex $line 1]
927 }
928 DW_OP_consts {
929 _op .sleb128 [lindex $line 1]
930 }
931
932 DW_OP_plus_uconst {
933 _op .uleb128 [lindex $line 1]
934 }
935
936 DW_OP_piece {
937 _op .uleb128 [lindex $line 1]
938 }
939
940 DW_OP_bit_piece {
941 _op .uleb128 [lindex $line 1]
942 _op .uleb128 [lindex $line 2]
943 }
944
945 DW_OP_skip -
946 DW_OP_bra {
947 _op .2byte [lindex $line 1]
948 }
949
950 DW_OP_implicit_value {
951 set l1 [new_label "value_start"]
952 set l2 [new_label "value_end"]
953 _op .uleb128 "$l2 - $l1"
954 define_label $l1
955 foreach value [lrange $line 1 end] {
956 switch -regexp -- $value {
957 {^0x[[:xdigit:]]{1,2}$} {_op .byte $value}
958 {^0x[[:xdigit:]]{4}$} {_op .2byte $value}
959 {^0x[[:xdigit:]]{8}$} {_op .4byte $value}
960 {^0x[[:xdigit:]]{16}$} {_op .8byte $value}
961 default {
962 error "bad value '$value' in DW_OP_implicit_value"
963 }
964 }
965 }
966 define_label $l2
967 }
968
969 DW_OP_implicit_pointer -
970 DW_OP_GNU_implicit_pointer {
971 if {[llength $line] != 3} {
972 error "usage: $opcode LABEL OFFSET"
973 }
974
975 # Here label is a section offset.
976 set label [lindex $line 1]
977 if { $_cu_version == 2 } {
978 _op .${_cu_addr_size}byte $label
979 } else {
980 _op .${_cu_offset_size}byte $label
981 }
982 _op .sleb128 [lindex $line 2]
983 }
984
985 DW_OP_GNU_variable_value {
986 if {[llength $line] != 2} {
987 error "usage: $opcode LABEL"
988 }
989
990 # Here label is a section offset.
991 set label [lindex $line 1]
992 if { $_cu_version == 2 } {
993 _op .${_cu_addr_size}byte $label
994 } else {
995 _op .${_cu_offset_size}byte $label
996 }
997 }
998
999 DW_OP_deref_size {
1000 if {[llength $line] != 2} {
1001 error "usage: DW_OP_deref_size SIZE"
1002 }
1003
1004 _op .byte [lindex $line 1]
1005 }
1006
1007 DW_OP_bregx {
1008 _op .uleb128 [lindex $line 1]
1009 _op .sleb128 [lindex $line 2]
1010 }
1011
1012 default {
1013 if {[llength $line] > 1} {
1014 error "Unimplemented: operands in location for $opcode"
1015 }
1016 }
1017 }
1018 }
1019 }
1020
1021 # Emit a DWARF CU.
1022 # OPTIONS is a list with an even number of elements containing
1023 # option-name and option-value pairs.
1024 # Current options are:
1025 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1026 # default = 0 (32-bit)
1027 # version n - DWARF version number to emit
1028 # default = 4
1029 # addr_size n - the size of addresses, 32, 64, or default
1030 # default = default
1031 # fission 0|1 - boolean indicating if generating Fission debug info
1032 # default = 0
1033 # BODY is Tcl code that emits the DIEs which make up the body of
1034 # the CU. It is evaluated in the caller's context.
1035 proc cu {options body} {
1036 variable _cu_count
1037 variable _abbrev_section
1038 variable _abbrev_num
1039 variable _cu_label
1040 variable _cu_version
1041 variable _cu_addr_size
1042 variable _cu_offset_size
1043
1044 # Establish the defaults.
1045 set is_64 0
1046 set _cu_version 4
1047 set _cu_addr_size default
1048 set fission 0
1049 set section ".debug_info"
1050 set _abbrev_section ".debug_abbrev"
1051
1052 foreach { name value } $options {
1053 set value [uplevel 1 "subst \"$value\""]
1054 switch -exact -- $name {
1055 is_64 { set is_64 $value }
1056 version { set _cu_version $value }
1057 addr_size { set _cu_addr_size $value }
1058 fission { set fission $value }
1059 default { error "unknown option $name" }
1060 }
1061 }
1062 if {$_cu_addr_size == "default"} {
1063 if {[is_64_target]} {
1064 set _cu_addr_size 8
1065 } else {
1066 set _cu_addr_size 4
1067 }
1068 }
1069 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
1070 if { $fission } {
1071 set section ".debug_info.dwo"
1072 set _abbrev_section ".debug_abbrev.dwo"
1073 }
1074
1075 _section $section
1076
1077 set cu_num [incr _cu_count]
1078 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
1079 set _abbrev_num 1
1080
1081 set _cu_label [_compute_label "cu${cu_num}_begin"]
1082 set start_label [_compute_label "cu${cu_num}_start"]
1083 set end_label [_compute_label "cu${cu_num}_end"]
1084
1085 define_label $_cu_label
1086 if {$is_64} {
1087 _op .4byte 0xffffffff
1088 _op .8byte "$end_label - $start_label"
1089 } else {
1090 _op .4byte "$end_label - $start_label"
1091 }
1092 define_label $start_label
1093 _op .2byte $_cu_version Version
1094 _op .${_cu_offset_size}byte $my_abbrevs Abbrevs
1095 _op .byte $_cu_addr_size "Pointer size"
1096
1097 _defer_output $_abbrev_section {
1098 define_label $my_abbrevs
1099 }
1100
1101 uplevel $body
1102
1103 _defer_output $_abbrev_section {
1104 # Emit the terminator.
1105 _op .byte 0x0 Terminator
1106 _op .byte 0x0 Terminator
1107 }
1108
1109 define_label $end_label
1110 }
1111
1112 # Emit a DWARF TU.
1113 # OPTIONS is a list with an even number of elements containing
1114 # option-name and option-value pairs.
1115 # Current options are:
1116 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1117 # default = 0 (32-bit)
1118 # version n - DWARF version number to emit
1119 # default = 4
1120 # addr_size n - the size of addresses, 32, 64, or default
1121 # default = default
1122 # fission 0|1 - boolean indicating if generating Fission debug info
1123 # default = 0
1124 # SIGNATURE is the 64-bit signature of the type.
1125 # TYPE_LABEL is the label of the type defined by this TU,
1126 # or "" if there is no type (i.e., type stubs in Fission).
1127 # BODY is Tcl code that emits the DIEs which make up the body of
1128 # the TU. It is evaluated in the caller's context.
1129 proc tu {options signature type_label body} {
1130 variable _cu_count
1131 variable _abbrev_section
1132 variable _abbrev_num
1133 variable _cu_label
1134 variable _cu_version
1135 variable _cu_addr_size
1136 variable _cu_offset_size
1137
1138 # Establish the defaults.
1139 set is_64 0
1140 set _cu_version 4
1141 set _cu_addr_size default
1142 set fission 0
1143 set section ".debug_types"
1144 set _abbrev_section ".debug_abbrev"
1145
1146 foreach { name value } $options {
1147 switch -exact -- $name {
1148 is_64 { set is_64 $value }
1149 version { set _cu_version $value }
1150 addr_size { set _cu_addr_size $value }
1151 fission { set fission $value }
1152 default { error "unknown option $name" }
1153 }
1154 }
1155 if {$_cu_addr_size == "default"} {
1156 if {[is_64_target]} {
1157 set _cu_addr_size 8
1158 } else {
1159 set _cu_addr_size 4
1160 }
1161 }
1162 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
1163 if { $fission } {
1164 set section ".debug_types.dwo"
1165 set _abbrev_section ".debug_abbrev.dwo"
1166 }
1167
1168 _section $section
1169
1170 set cu_num [incr _cu_count]
1171 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
1172 set _abbrev_num 1
1173
1174 set _cu_label [_compute_label "cu${cu_num}_begin"]
1175 set start_label [_compute_label "cu${cu_num}_start"]
1176 set end_label [_compute_label "cu${cu_num}_end"]
1177
1178 define_label $_cu_label
1179 if {$is_64} {
1180 _op .4byte 0xffffffff
1181 _op .8byte "$end_label - $start_label"
1182 } else {
1183 _op .4byte "$end_label - $start_label"
1184 }
1185 define_label $start_label
1186 _op .2byte $_cu_version Version
1187 _op .${_cu_offset_size}byte $my_abbrevs Abbrevs
1188 _op .byte $_cu_addr_size "Pointer size"
1189 _op .8byte $signature Signature
1190 if { $type_label != "" } {
1191 uplevel declare_labels $type_label
1192 upvar $type_label my_type_label
1193 if {$is_64} {
1194 _op .8byte "$my_type_label - $_cu_label"
1195 } else {
1196 _op .4byte "$my_type_label - $_cu_label"
1197 }
1198 } else {
1199 if {$is_64} {
1200 _op .8byte 0
1201 } else {
1202 _op .4byte 0
1203 }
1204 }
1205
1206 _defer_output $_abbrev_section {
1207 define_label $my_abbrevs
1208 }
1209
1210 uplevel $body
1211
1212 _defer_output $_abbrev_section {
1213 # Emit the terminator.
1214 _op .byte 0x0 Terminator
1215 _op .byte 0x0 Terminator
1216 }
1217
1218 define_label $end_label
1219 }
1220
1221 # Emit a DWARF .debug_ranges unit.
1222 # OPTIONS is a list with an even number of elements containing
1223 # option-name and option-value pairs.
1224 # Current options are:
1225 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1226 # default = 0 (32-bit)
1227 #
1228 # BODY is Tcl code that emits the content of the .debug_ranges
1229 # unit, it is evaluated in the caller's context.
1230 proc ranges {options body} {
1231 variable _debug_ranges_64_bit
1232
1233 foreach { name value } $options {
1234 switch -exact -- $name {
1235 is_64 { set _debug_ranges_64_bit [subst $value] }
1236 default { error "unknown option $name" }
1237 }
1238 }
1239
1240 set section ".debug_ranges"
1241 _section $section
1242
1243 proc sequence {{ranges {}}} {
1244 variable _debug_ranges_64_bit
1245
1246 # Emit the sequence of addresses.
1247 set base ""
1248 foreach range $ranges {
1249 set range [uplevel 1 "subst \"$range\""]
1250 set type [lindex $range 0]
1251 switch -exact -- $type {
1252 base {
1253 set base [lrange $range 1 end]
1254
1255 if { $_debug_ranges_64_bit } then {
1256 _op .8byte 0xffffffffffffffff "Base Marker"
1257 _op .8byte $base "Base Address"
1258 } else {
1259 _op .4byte 0xffffffff "Base Marker"
1260 _op .4byte $base "Base Address"
1261 }
1262 }
1263 range {
1264 set start [lindex $range 1]
1265 set end [lrange $range 2 end]
1266
1267 if { $_debug_ranges_64_bit } then {
1268 _op .8byte $start "Start Address"
1269 _op .8byte $end "End Address"
1270 } else {
1271 _op .4byte $start "Start Address"
1272 _op .4byte $end "End Address"
1273 }
1274 }
1275 default { error "unknown range type: $type " }
1276 }
1277 }
1278
1279 # End of the sequence.
1280 if { $_debug_ranges_64_bit } then {
1281 _op .8byte 0x0 "End of Sequence Marker (Part 1)"
1282 _op .8byte 0x0 "End of Sequence Marker (Part 2)"
1283 } else {
1284 _op .4byte 0x0 "End of Sequence Marker (Part 1)"
1285 _op .4byte 0x0 "End of Sequence Marker (Part 2)"
1286 }
1287 }
1288
1289 uplevel $body
1290 }
1291
1292
1293 # Emit a DWARF .debug_line unit.
1294 # OPTIONS is a list with an even number of elements containing
1295 # option-name and option-value pairs.
1296 # Current options are:
1297 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1298 # default = 0 (32-bit)
1299 # version n - DWARF version number to emit
1300 # default = 4
1301 # addr_size n - the size of addresses, 32, 64, or default
1302 # default = default
1303 #
1304 # LABEL is the label of the current unit (which is probably
1305 # referenced by a DW_AT_stmt_list), or "" if there is no such
1306 # label.
1307 #
1308 # BODY is Tcl code that emits the parts which make up the body of
1309 # the line unit. It is evaluated in the caller's context. The
1310 # following commands are available for the BODY section:
1311 #
1312 # include_dir "dirname" -- adds a new include directory
1313 #
1314 # file_name "file.c" idx -- adds a new file name. IDX is a
1315 # 1-based index referencing an include directory or 0 for
1316 # current directory.
1317
1318 proc lines {options label body} {
1319 variable _line_count
1320 variable _line_saw_file
1321 variable _line_saw_program
1322 variable _line_header_end_label
1323
1324 # Establish the defaults.
1325 set is_64 0
1326 set _unit_version 4
1327 set _unit_addr_size default
1328 set _line_saw_program 0
1329 set _line_saw_file 0
1330 set _default_is_stmt 1
1331
1332 foreach { name value } $options {
1333 switch -exact -- $name {
1334 is_64 { set is_64 $value }
1335 version { set _unit_version $value }
1336 addr_size { set _unit_addr_size $value }
1337 default_is_stmt { set _default_is_stmt $value }
1338 default { error "unknown option $name" }
1339 }
1340 }
1341 if {$_unit_addr_size == "default"} {
1342 if {[is_64_target]} {
1343 set _unit_addr_size 8
1344 } else {
1345 set _unit_addr_size 4
1346 }
1347 }
1348
1349 set unit_num [incr _line_count]
1350
1351 set section ".debug_line"
1352 _section $section
1353
1354 if { "$label" != "" } {
1355 # Define the user-provided label at this point.
1356 $label:
1357 }
1358
1359 set unit_len_label [_compute_label "line${_line_count}_start"]
1360 set unit_end_label [_compute_label "line${_line_count}_end"]
1361 set header_len_label [_compute_label "line${_line_count}_header_start"]
1362 set _line_header_end_label [_compute_label "line${_line_count}_header_end"]
1363
1364 if {$is_64} {
1365 _op .4byte 0xffffffff
1366 _op .8byte "$unit_end_label - $unit_len_label" "unit_length"
1367 } else {
1368 _op .4byte "$unit_end_label - $unit_len_label" "unit_length"
1369 }
1370
1371 define_label $unit_len_label
1372
1373 _op .2byte $_unit_version version
1374
1375 if {$is_64} {
1376 _op .8byte "$_line_header_end_label - $header_len_label" "header_length"
1377 } else {
1378 _op .4byte "$_line_header_end_label - $header_len_label" "header_length"
1379 }
1380
1381 define_label $header_len_label
1382
1383 _op .byte 1 "minimum_instruction_length"
1384 _op .byte $_default_is_stmt "default_is_stmt"
1385 _op .byte 1 "line_base"
1386 _op .byte 1 "line_range"
1387 _op .byte 10 "opcode_base"
1388
1389 # The standard_opcode_lengths table. The number of arguments
1390 # for each of the standard opcodes. Generating 9 entries here
1391 # matches the use of 10 in the opcode_base above. These 9
1392 # entries match the 9 standard opcodes for DWARF2, making use
1393 # of only 9 should be fine, even if we are generating DWARF3
1394 # or DWARF4.
1395 _op .byte 0 "standard opcode 1"
1396 _op .byte 1 "standard opcode 2"
1397 _op .byte 1 "standard opcode 3"
1398 _op .byte 1 "standard opcode 4"
1399 _op .byte 1 "standard opcode 5"
1400 _op .byte 0 "standard opcode 6"
1401 _op .byte 0 "standard opcode 7"
1402 _op .byte 0 "standard opcode 8"
1403 _op .byte 1 "standard opcode 9"
1404
1405 proc include_dir {dirname} {
1406 _op .ascii [_quote $dirname]
1407 }
1408
1409 proc file_name {filename diridx} {
1410 variable _line_saw_file
1411 if "! $_line_saw_file" {
1412 # Terminate the dir list.
1413 _op .byte 0 "Terminator."
1414 set _line_saw_file 1
1415 }
1416
1417 _op .ascii [_quote $filename]
1418 _op .sleb128 $diridx
1419 _op .sleb128 0 "mtime"
1420 _op .sleb128 0 "length"
1421 }
1422
1423 proc program {statements} {
1424 variable _line_saw_program
1425 variable _line_header_end_label
1426
1427 if "! $_line_saw_program" {
1428 # Terminate the file list.
1429 _op .byte 0 "Terminator."
1430 define_label $_line_header_end_label
1431 set _line_saw_program 1
1432 }
1433
1434 proc DW_LNE_set_address {addr} {
1435 _op .byte 0
1436 set start [new_label "set_address_start"]
1437 set end [new_label "set_address_end"]
1438 _op .uleb128 "${end} - ${start}"
1439 define_label ${start}
1440 _op .byte 2
1441 if {[is_64_target]} {
1442 _op .8byte ${addr}
1443 } else {
1444 _op .4byte ${addr}
1445 }
1446 define_label ${end}
1447 }
1448
1449 proc DW_LNE_end_sequence {} {
1450 _op .byte 0
1451 _op .uleb128 1
1452 _op .byte 1
1453 }
1454
1455 proc DW_LNS_copy {} {
1456 _op .byte 1
1457 }
1458
1459 proc DW_LNS_negate_stmt {} {
1460 _op .byte 6
1461 }
1462
1463 proc DW_LNS_advance_pc {offset} {
1464 _op .byte 2
1465 _op .uleb128 ${offset}
1466 }
1467
1468 proc DW_LNS_advance_line {offset} {
1469 _op .byte 3
1470 _op .sleb128 ${offset}
1471 }
1472
1473 proc DW_LNS_set_file {num} {
1474 _op .byte 4
1475 _op .sleb128 ${num}
1476 }
1477
1478 foreach statement $statements {
1479 uplevel 1 $statement
1480 }
1481 }
1482
1483 uplevel $body
1484
1485 rename include_dir ""
1486 rename file_name ""
1487
1488 # Terminate dir list if we saw no files.
1489 if "! $_line_saw_file" {
1490 _op .byte 0 "Terminator."
1491 }
1492
1493 # Terminate the file list.
1494 if "! $_line_saw_program" {
1495 _op .byte 0 "Terminator."
1496 define_label $_line_header_end_label
1497 }
1498
1499 define_label $unit_end_label
1500 }
1501
1502 proc _empty_array {name} {
1503 upvar $name the_array
1504
1505 catch {unset the_array}
1506 set the_array(_) {}
1507 unset the_array(_)
1508 }
1509
1510 # Emit a .gnu_debugaltlink section with the given file name and
1511 # build-id. The buildid should be represented as a hexadecimal
1512 # string, like "ffeeddcc".
1513 proc gnu_debugaltlink {filename buildid} {
1514 _defer_output .gnu_debugaltlink {
1515 _op .ascii [_quote $filename]
1516 foreach {a b} [split $buildid {}] {
1517 _op .byte 0x$a$b
1518 }
1519 }
1520 }
1521
1522 proc _note {type name hexdata} {
1523 set namelen [expr [string length $name] + 1]
1524
1525 # Name size.
1526 _op .4byte $namelen
1527 # Data size.
1528 _op .4byte [expr [string length $hexdata] / 2]
1529 # Type.
1530 _op .4byte $type
1531 # The name.
1532 _op .ascii [_quote $name]
1533 # Alignment.
1534 set align 2
1535 set total [expr {($namelen + (1 << $align) - 1) & -(1 << $align)}]
1536 for {set i $namelen} {$i < $total} {incr i} {
1537 _op .byte 0
1538 }
1539 # The data.
1540 foreach {a b} [split $hexdata {}] {
1541 _op .byte 0x$a$b
1542 }
1543 }
1544
1545 # Emit a note section holding the given build-id.
1546 proc build_id {buildid} {
1547 _defer_output {.note.gnu.build-id a note} {
1548 # From elf/common.h.
1549 set NT_GNU_BUILD_ID 3
1550
1551 _note $NT_GNU_BUILD_ID GNU $buildid
1552 }
1553 }
1554
1555 # The top-level interface to the DWARF assembler.
1556 # FILENAME is the name of the file where the generated assembly
1557 # code is written.
1558 # BODY is Tcl code to emit the assembly. It is evaluated via
1559 # "eval" -- not uplevel as you might expect, because it is
1560 # important to run the body in the Dwarf namespace.
1561 #
1562 # A typical invocation is something like:
1563 # Dwarf::assemble $file {
1564 # cu 0 2 8 {
1565 # compile_unit {
1566 # ...
1567 # }
1568 # }
1569 # cu 0 2 8 {
1570 # ...
1571 # }
1572 # }
1573 proc assemble {filename body} {
1574 variable _initialized
1575 variable _output_file
1576 variable _deferred_output
1577 variable _defer
1578 variable _label_num
1579 variable _strings
1580 variable _cu_count
1581 variable _line_count
1582 variable _line_saw_file
1583 variable _line_saw_program
1584 variable _line_header_end_label
1585 variable _debug_ranges_64_bit
1586
1587 if {!$_initialized} {
1588 _read_constants
1589 set _initialized 1
1590 }
1591
1592 set _output_file [open $filename w]
1593 set _cu_count 0
1594 _empty_array _deferred_output
1595 set _defer ""
1596 set _label_num 0
1597 _empty_array _strings
1598
1599 set _line_count 0
1600 set _line_saw_file 0
1601 set _line_saw_program 0
1602 set _debug_ranges_64_bit [is_64_target]
1603
1604 # Not "uplevel" here, because we want to evaluate in this
1605 # namespace. This is somewhat bad because it means we can't
1606 # readily refer to outer variables.
1607 eval $body
1608
1609 _write_deferred_output
1610
1611 catch {close $_output_file}
1612 set _output_file {}
1613 }
1614 }