gdb/testsuite: make DWARF assembler's ranges' "base" and "range" procs
[deliverable/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 }
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 $srcdir/$subdir/$srcfile.
225 #
226 # - MACRO_AT_func { FUNC }
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, if the attribute name has a default form (f.i. DW_FORM_addr for
251 # DW_AT_low_pc), then that one is used.
252 # * Otherwise, an error is reported. Either specify a form explicitly, or
253 # add a default for the the attribute name in _default_form.
254 #
255 # CHILDREN is just Tcl code that can be used to define child DIEs. It
256 # is evaluated in the caller's context.
257 #
258 # Currently this code is missing nice support for CFA handling, and
259 # probably other things as well.
260
261 namespace eval Dwarf {
262 # True if the module has been initialized.
263 variable _initialized 0
264
265 # Constants from dwarf2.h.
266 variable _constants
267 # DW_AT short names.
268 variable _AT
269 # DW_FORM short names.
270 variable _FORM
271 # DW_OP short names.
272 variable _OP
273
274 # The current output file.
275 variable _output_file
276
277 # Note: The _cu_ values here also apply to type units (TUs).
278 # Think of a TU as a special kind of CU.
279
280 # Current CU count.
281 variable _cu_count
282
283 # The current CU's base label.
284 variable _cu_label
285
286 # The current CU's version.
287 variable _cu_version
288
289 # The current CU's address size.
290 variable _cu_addr_size
291 # The current CU's offset size.
292 variable _cu_offset_size
293
294 # Label generation number.
295 variable _label_num
296
297 # The deferred output array. The index is the section name; the
298 # contents hold the data for that section.
299 variable _deferred_output
300
301 # If empty, we should write directly to the output file.
302 # Otherwise, this is the name of a section to write to.
303 variable _defer
304
305 # The abbrev section. Typically .debug_abbrev but can be .debug_abbrev.dwo
306 # for Fission.
307 variable _abbrev_section
308
309 # The next available abbrev number in the current CU's abbrev
310 # table.
311 variable _abbrev_num
312
313 # The string table for this assembly. The key is the string; the
314 # value is the label for that string.
315 variable _strings
316
317 # Current .debug_line unit count.
318 variable _line_count
319
320 # Whether a file_name entry was seen.
321 variable _line_saw_file
322
323 # Whether a line table program has been seen.
324 variable _line_saw_program
325
326 # A Label for line table header generation.
327 variable _line_header_end_label
328
329 # The address size for debug ranges section.
330 variable _debug_ranges_64_bit
331
332 proc _process_one_constant {name value} {
333 variable _constants
334 variable _AT
335 variable _FORM
336 variable _OP
337
338 set _constants($name) $value
339
340 if {![regexp "^DW_(\[A-Z\]+)_(\[A-Za-z0-9_\]+)$" $name \
341 ignore prefix name2]} {
342 error "non-matching name: $name"
343 }
344
345 if {$name2 == "lo_user" || $name2 == "hi_user"} {
346 return
347 }
348
349 # We only try to shorten some very common things.
350 # FIXME: CFA?
351 switch -exact -- $prefix {
352 TAG {
353 # Create two procedures for the tag. These call
354 # _handle_DW_TAG with the full tag name baked in; this
355 # does all the actual work.
356 proc $name {{attrs {}} {children {}}} \
357 "_handle_DW_TAG $name \$attrs \$children"
358
359 # Filter out ones that are known to clash.
360 if {$name2 == "variable" || $name2 == "namespace"} {
361 set name2 "tag_$name2"
362 }
363
364 if {[info commands $name2] != {}} {
365 error "duplicate proc name: from $name"
366 }
367
368 proc $name2 {{attrs {}} {children {}}} \
369 "_handle_DW_TAG $name \$attrs \$children"
370 }
371
372 AT {
373 set _AT($name2) $name
374 }
375
376 FORM {
377 set _FORM($name2) $name
378 }
379
380 OP {
381 set _OP($name2) $name
382 }
383
384 default {
385 return
386 }
387 }
388 }
389
390 proc _read_constants {} {
391 global srcdir hex decimal
392 variable _constants
393
394 # DWARF name-matching regexp.
395 set dwrx "DW_\[a-zA-Z0-9_\]+"
396 # Whitespace regexp.
397 set ws "\[ \t\]+"
398
399 set fd [open [file join $srcdir .. .. include dwarf2.h]]
400 while {![eof $fd]} {
401 set line [gets $fd]
402 if {[regexp -- "^${ws}($dwrx)${ws}=${ws}($hex|$decimal),?$" \
403 $line ignore name value ignore2]} {
404 _process_one_constant $name $value
405 }
406 }
407 close $fd
408
409 set fd [open [file join $srcdir .. .. include dwarf2.def]]
410 while {![eof $fd]} {
411 set line [gets $fd]
412 if {[regexp -- \
413 "^DW_\[A-Z_\]+${ws}\\(($dwrx),${ws}($hex|$decimal)\\)$" \
414 $line ignore name value ignore2]} {
415 _process_one_constant $name $value
416 }
417 }
418 close $fd
419
420 set _constants(SPECIAL_expr) $_constants(DW_FORM_block)
421 }
422
423 proc _quote {string} {
424 # FIXME
425 return "\"${string}\\0\""
426 }
427
428 proc _nz_quote {string} {
429 # For now, no quoting is done.
430 return "\"${string}\""
431 }
432
433 proc _handle_DW_FORM {form value} {
434 switch -exact -- $form {
435 DW_FORM_string {
436 _op .ascii [_quote $value]
437 }
438
439 DW_FORM_flag_present {
440 # We don't need to emit anything.
441 }
442
443 DW_FORM_data4 -
444 DW_FORM_ref4 {
445 _op .4byte $value
446 }
447
448 DW_FORM_ref_addr {
449 variable _cu_offset_size
450 variable _cu_version
451 variable _cu_addr_size
452
453 if {$_cu_version == 2} {
454 set size $_cu_addr_size
455 } else {
456 set size $_cu_offset_size
457 }
458
459 _op .${size}byte $value
460 }
461
462 DW_FORM_sec_offset {
463 variable _cu_offset_size
464 _op .${_cu_offset_size}byte $value
465 }
466
467 DW_FORM_ref1 -
468 DW_FORM_flag -
469 DW_FORM_data1 {
470 _op .byte $value
471 }
472
473 DW_FORM_sdata {
474 _op .sleb128 $value
475 }
476
477 DW_FORM_ref_udata -
478 DW_FORM_udata {
479 _op .uleb128 $value
480 }
481
482 DW_FORM_addr {
483 variable _cu_addr_size
484
485 _op .${_cu_addr_size}byte $value
486 }
487
488 DW_FORM_data2 -
489 DW_FORM_ref2 {
490 _op .2byte $value
491 }
492
493 DW_FORM_data8 -
494 DW_FORM_ref8 -
495 DW_FORM_ref_sig8 {
496 _op .8byte $value
497 }
498
499 DW_FORM_data16 {
500 _op .8byte $value
501 }
502
503 DW_FORM_strp {
504 variable _strings
505 variable _cu_offset_size
506
507 if {![info exists _strings($value)]} {
508 set _strings($value) [new_label strp]
509 _defer_output .debug_string {
510 define_label $_strings($value)
511 _op .ascii [_quote $value]
512 }
513 }
514
515 _op .${_cu_offset_size}byte $_strings($value) "strp: $value"
516 }
517
518 SPECIAL_expr {
519 set l1 [new_label "expr_start"]
520 set l2 [new_label "expr_end"]
521 _op .uleb128 "$l2 - $l1" "expression"
522 define_label $l1
523 _location $value
524 define_label $l2
525 }
526
527 DW_FORM_block1 {
528 set len [string length $value]
529 if {$len > 255} {
530 error "DW_FORM_block1 length too long"
531 }
532 _op .byte $len
533 _op .ascii [_nz_quote $value]
534 }
535
536 DW_FORM_block2 -
537 DW_FORM_block4 -
538
539 DW_FORM_block -
540
541 DW_FORM_ref2 -
542 DW_FORM_indirect -
543 DW_FORM_exprloc -
544
545 DW_FORM_strx -
546 DW_FORM_strx1 -
547 DW_FORM_strx2 -
548 DW_FORM_strx3 -
549 DW_FORM_strx4 -
550
551 DW_FORM_GNU_addr_index -
552 DW_FORM_GNU_str_index -
553 DW_FORM_GNU_ref_alt -
554 DW_FORM_GNU_strp_alt -
555
556 default {
557 error "unhandled form $form"
558 }
559 }
560 }
561
562 proc _guess_form {value varname} {
563 upvar $varname new_value
564
565 switch -exact -- [string range $value 0 0] {
566 @ {
567 # Constant reference.
568 variable _constants
569
570 set new_value $_constants([string range $value 1 end])
571 # Just the simplest.
572 return DW_FORM_sdata
573 }
574
575 : {
576 # Label reference.
577 variable _cu_label
578
579 set new_value "[string range $value 1 end] - $_cu_label"
580
581 return DW_FORM_ref4
582 }
583
584 % {
585 # Label reference, an offset from .debug_info.
586 set new_value "[string range $value 1 end]"
587
588 return DW_FORM_ref_addr
589 }
590
591 default {
592 return ""
593 }
594 }
595 }
596
597 proc _default_form { attr } {
598 switch -exact -- $attr {
599 DW_AT_low_pc {
600 return DW_FORM_addr
601 }
602 DW_AT_producer -
603 DW_AT_comp_dir -
604 DW_AT_linkage_name -
605 DW_AT_MIPS_linkage_name -
606 DW_AT_name {
607 return DW_FORM_string
608 }
609 }
610 return ""
611 }
612
613 # Map NAME to its canonical form.
614 proc _map_name {name ary} {
615 variable $ary
616
617 if {[info exists ${ary}($name)]} {
618 set name [set ${ary}($name)]
619 }
620
621 return $name
622 }
623
624 proc _handle_attribute { attr_name attr_value attr_form } {
625 variable _abbrev_section
626 variable _constants
627
628 _handle_DW_FORM $attr_form $attr_value
629
630 _defer_output $_abbrev_section {
631 _op .uleb128 $_constants($attr_name) $attr_name
632 _op .uleb128 $_constants($attr_form) $attr_form
633 }
634 }
635
636 # Handle macro attribute MACRO_AT_range.
637
638 proc _handle_macro_at_range { attr_value } {
639 if {[llength $attr_value] != 1} {
640 error "usage: MACRO_AT_range { func }"
641 }
642
643 set func [lindex $attr_value 0]
644 global srcdir subdir srcfile
645 set src ${srcdir}/${subdir}/${srcfile}
646 set result [function_range $func $src]
647
648 _handle_attribute DW_AT_low_pc [lindex $result 0] \
649 DW_FORM_addr
650 _handle_attribute DW_AT_high_pc \
651 "[lindex $result 0] + [lindex $result 1]" DW_FORM_addr
652 }
653
654 # Handle macro attribute MACRO_AT_func.
655
656 proc _handle_macro_at_func { attr_value } {
657 if {[llength $attr_value] != 1} {
658 error "usage: MACRO_AT_func { func file }"
659 }
660 _handle_attribute DW_AT_name [lindex $attr_value 0] DW_FORM_string
661 _handle_macro_at_range $attr_value
662 }
663
664 proc _handle_DW_TAG {tag_name {attrs {}} {children {}}} {
665 variable _abbrev_section
666 variable _abbrev_num
667 variable _constants
668
669 set has_children [expr {[string length $children] > 0}]
670 set my_abbrev [incr _abbrev_num]
671
672 # We somewhat wastefully emit a new abbrev entry for each tag.
673 # There's no reason for this other than laziness.
674 _defer_output $_abbrev_section {
675 _op .uleb128 $my_abbrev "Abbrev start"
676 _op .uleb128 $_constants($tag_name) $tag_name
677 _op .byte $has_children "has_children"
678 }
679
680 _op .uleb128 $my_abbrev "Abbrev ($tag_name)"
681
682 foreach attr $attrs {
683 set attr_name [_map_name [lindex $attr 0] _AT]
684
685 # When the length of ATTR is greater than 2, the last
686 # element of the list must be a form. The second through
687 # the penultimate elements are joined together and
688 # evaluated using subst. This allows constructs such as
689 # [gdb_target_symbol foo] to be used.
690
691 if {[llength $attr] > 2} {
692 set attr_value [uplevel 2 [list subst [join [lrange $attr 1 end-1]]]]
693 } else {
694 set attr_value [uplevel 2 [list subst [lindex $attr 1]]]
695 }
696
697 if { [string equal "MACRO_AT_func" $attr_name] } {
698 _handle_macro_at_func $attr_value
699 } elseif { [string equal "MACRO_AT_range" $attr_name] } {
700 _handle_macro_at_range $attr_value
701 } else {
702 if {[llength $attr] > 2} {
703 set attr_form [uplevel 2 [list subst [lindex $attr end]]]
704
705 if { [string index $attr_value 0] == ":" } {
706 # It is a label, get its value.
707 _guess_form $attr_value attr_value
708 }
709 } else {
710 set attr_form [_guess_form $attr_value attr_value]
711 if { $attr_form eq "" } {
712 set attr_form [_default_form $attr_name]
713 }
714 if { $attr_form eq "" } {
715 error "No form for $attr_name $attr_value"
716 }
717 }
718 set attr_form [_map_name $attr_form _FORM]
719
720 _handle_attribute $attr_name $attr_value $attr_form
721 }
722 }
723
724 _defer_output $_abbrev_section {
725 # Terminator.
726 _op .byte 0x0 "DW_AT - Terminator"
727 _op .byte 0x0 "DW_FORM - Terminator"
728 }
729
730 if {$has_children} {
731 uplevel 2 $children
732
733 # Terminate children.
734 _op .byte 0x0 "Terminate children"
735 }
736 }
737
738 proc _emit {string} {
739 variable _output_file
740 variable _defer
741 variable _deferred_output
742
743 if {$_defer == ""} {
744 puts $_output_file $string
745 } else {
746 append _deferred_output($_defer) ${string}\n
747 }
748 }
749
750 proc _section {name {flags ""} {type ""}} {
751 if {$flags == "" && $type == ""} {
752 _emit " .section $name"
753 } elseif {$type == ""} {
754 _emit " .section $name, \"$flags\""
755 } else {
756 _emit " .section $name, \"$flags\", %$type"
757 }
758 }
759
760 # SECTION_SPEC is a list of arguments to _section.
761 proc _defer_output {section_spec body} {
762 variable _defer
763 variable _deferred_output
764
765 set old_defer $_defer
766 set _defer [lindex $section_spec 0]
767
768 if {![info exists _deferred_output($_defer)]} {
769 set _deferred_output($_defer) ""
770 eval _section $section_spec
771 }
772
773 uplevel $body
774
775 set _defer $old_defer
776 }
777
778 proc _defer_to_string {body} {
779 variable _defer
780 variable _deferred_output
781
782 set old_defer $_defer
783 set _defer temp
784
785 set _deferred_output($_defer) ""
786
787 uplevel $body
788
789 set result $_deferred_output($_defer)
790 unset _deferred_output($_defer)
791
792 set _defer $old_defer
793 return $result
794 }
795
796 proc _write_deferred_output {} {
797 variable _output_file
798 variable _deferred_output
799
800 foreach section [array names _deferred_output] {
801 # The data already has a newline.
802 puts -nonewline $_output_file $_deferred_output($section)
803 }
804
805 # Save some memory.
806 unset _deferred_output
807 }
808
809 proc _op {name value {comment ""}} {
810 set text " ${name} ${value}"
811 if {$comment != ""} {
812 # Try to make stuff line up nicely.
813 while {[string length $text] < 40} {
814 append text " "
815 }
816 append text "/* ${comment} */"
817 }
818 _emit $text
819 }
820
821 proc _compute_label {name} {
822 return ".L${name}"
823 }
824
825 # Return a name suitable for use as a label. If BASE_NAME is
826 # specified, it is incorporated into the label name; this is to
827 # make debugging the generated assembler easier. If BASE_NAME is
828 # not specified a generic default is used. This proc does not
829 # define the label; see 'define_label'. 'new_label' attempts to
830 # ensure that label names are unique.
831 proc new_label {{base_name label}} {
832 variable _label_num
833
834 return [_compute_label ${base_name}[incr _label_num]]
835 }
836
837 # Define a label named NAME. Ordinarily, NAME comes from a call
838 # to 'new_label', but this is not required.
839 proc define_label {name} {
840 _emit "${name}:"
841 }
842
843 # A higher-level interface to label handling.
844 #
845 # ARGS is a list of label descriptors. Each one is either a
846 # single element, or a list of two elements -- a name and some
847 # text. For each descriptor, 'new_label' is invoked. If the list
848 # form is used, the second element in the list is passed as an
849 # argument. The label name is used to define a variable in the
850 # enclosing scope; this can be used to refer to the label later.
851 # The label name is also used to define a new proc whose name is
852 # the label name plus a trailing ":". This proc takes a body as
853 # an argument and can be used to define the label at that point;
854 # then the body, if any, is evaluated in the caller's context.
855 #
856 # For example:
857 #
858 # declare_labels int_label
859 # something { ... $int_label } ;# refer to the label
860 # int_label: constant { ... } ;# define the label
861 proc declare_labels {args} {
862 foreach arg $args {
863 set name [lindex $arg 0]
864 set text [lindex $arg 1]
865
866 upvar $name label_var
867 if {$text == ""} {
868 set label_var [new_label]
869 } else {
870 set label_var [new_label $text]
871 }
872
873 proc ${name}: {args} [format {
874 define_label %s
875 uplevel $args
876 } $label_var]
877 }
878 }
879
880 # This is a miniature assembler for location expressions. It is
881 # suitable for use in the attributes to a DIE. Its output is
882 # prefixed with "=" to make it automatically use DW_FORM_block.
883 # BODY is split by lines, and each line is taken to be a list.
884 # (FIXME should use 'info complete' here.)
885 # Each list's first element is the opcode, either short or long
886 # forms are accepted.
887 # FIXME argument handling
888 # FIXME move docs
889 proc _location {body} {
890 variable _constants
891 variable _cu_label
892 variable _cu_version
893 variable _cu_addr_size
894 variable _cu_offset_size
895
896 foreach line [split $body \n] {
897 # Ignore blank lines, and allow embedded comments.
898 if {[lindex $line 0] == "" || [regexp -- {^[ \t]*#} $line]} {
899 continue
900 }
901 set opcode [_map_name [lindex $line 0] _OP]
902 _op .byte $_constants($opcode) $opcode
903
904 switch -exact -- $opcode {
905 DW_OP_addr {
906 _op .${_cu_addr_size}byte [lindex $line 1]
907 }
908
909 DW_OP_regx {
910 _op .uleb128 [lindex $line 1]
911 }
912
913 DW_OP_pick -
914 DW_OP_const1u -
915 DW_OP_const1s {
916 _op .byte [lindex $line 1]
917 }
918
919 DW_OP_const2u -
920 DW_OP_const2s {
921 _op .2byte [lindex $line 1]
922 }
923
924 DW_OP_const4u -
925 DW_OP_const4s {
926 _op .4byte [lindex $line 1]
927 }
928
929 DW_OP_const8u -
930 DW_OP_const8s {
931 _op .8byte [lindex $line 1]
932 }
933
934 DW_OP_constu {
935 _op .uleb128 [lindex $line 1]
936 }
937 DW_OP_consts {
938 _op .sleb128 [lindex $line 1]
939 }
940
941 DW_OP_plus_uconst {
942 _op .uleb128 [lindex $line 1]
943 }
944
945 DW_OP_piece {
946 _op .uleb128 [lindex $line 1]
947 }
948
949 DW_OP_bit_piece {
950 _op .uleb128 [lindex $line 1]
951 _op .uleb128 [lindex $line 2]
952 }
953
954 DW_OP_skip -
955 DW_OP_bra {
956 _op .2byte [lindex $line 1]
957 }
958
959 DW_OP_implicit_value {
960 set l1 [new_label "value_start"]
961 set l2 [new_label "value_end"]
962 _op .uleb128 "$l2 - $l1"
963 define_label $l1
964 foreach value [lrange $line 1 end] {
965 switch -regexp -- $value {
966 {^0x[[:xdigit:]]{1,2}$} {_op .byte $value}
967 {^0x[[:xdigit:]]{4}$} {_op .2byte $value}
968 {^0x[[:xdigit:]]{8}$} {_op .4byte $value}
969 {^0x[[:xdigit:]]{16}$} {_op .8byte $value}
970 default {
971 error "bad value '$value' in DW_OP_implicit_value"
972 }
973 }
974 }
975 define_label $l2
976 }
977
978 DW_OP_implicit_pointer -
979 DW_OP_GNU_implicit_pointer {
980 if {[llength $line] != 3} {
981 error "usage: $opcode LABEL OFFSET"
982 }
983
984 # Here label is a section offset.
985 set label [lindex $line 1]
986 if { $_cu_version == 2 } {
987 _op .${_cu_addr_size}byte $label
988 } else {
989 _op .${_cu_offset_size}byte $label
990 }
991 _op .sleb128 [lindex $line 2]
992 }
993
994 DW_OP_GNU_variable_value {
995 if {[llength $line] != 2} {
996 error "usage: $opcode LABEL"
997 }
998
999 # Here label is a section offset.
1000 set label [lindex $line 1]
1001 if { $_cu_version == 2 } {
1002 _op .${_cu_addr_size}byte $label
1003 } else {
1004 _op .${_cu_offset_size}byte $label
1005 }
1006 }
1007
1008 DW_OP_deref_size {
1009 if {[llength $line] != 2} {
1010 error "usage: DW_OP_deref_size SIZE"
1011 }
1012
1013 _op .byte [lindex $line 1]
1014 }
1015
1016 DW_OP_bregx {
1017 _op .uleb128 [lindex $line 1]
1018 _op .sleb128 [lindex $line 2]
1019 }
1020
1021 default {
1022 if {[llength $line] > 1} {
1023 error "Unimplemented: operands in location for $opcode"
1024 }
1025 }
1026 }
1027 }
1028 }
1029
1030 # Emit a DWARF CU.
1031 # OPTIONS is a list with an even number of elements containing
1032 # option-name and option-value pairs.
1033 # Current options are:
1034 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1035 # default = 0 (32-bit)
1036 # version n - DWARF version number to emit
1037 # default = 4
1038 # addr_size n - the size of addresses in bytes: 4, 8, or default
1039 # default = default
1040 # fission 0|1 - boolean indicating if generating Fission debug info
1041 # default = 0
1042 # BODY is Tcl code that emits the DIEs which make up the body of
1043 # the CU. It is evaluated in the caller's context.
1044 proc cu {options body} {
1045 variable _cu_count
1046 variable _abbrev_section
1047 variable _abbrev_num
1048 variable _cu_label
1049 variable _cu_version
1050 variable _cu_addr_size
1051 variable _cu_offset_size
1052
1053 # Establish the defaults.
1054 set is_64 0
1055 set _cu_version 4
1056 set _cu_addr_size default
1057 set fission 0
1058 set section ".debug_info"
1059 set _abbrev_section ".debug_abbrev"
1060
1061 foreach { name value } $options {
1062 set value [uplevel 1 "subst \"$value\""]
1063 switch -exact -- $name {
1064 is_64 { set is_64 $value }
1065 version { set _cu_version $value }
1066 addr_size { set _cu_addr_size $value }
1067 fission { set fission $value }
1068 default { error "unknown option $name" }
1069 }
1070 }
1071 if {$_cu_addr_size == "default"} {
1072 if {[is_64_target]} {
1073 set _cu_addr_size 8
1074 } else {
1075 set _cu_addr_size 4
1076 }
1077 }
1078 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
1079 if { $fission } {
1080 set section ".debug_info.dwo"
1081 set _abbrev_section ".debug_abbrev.dwo"
1082 }
1083
1084 _section $section
1085
1086 set cu_num [incr _cu_count]
1087 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
1088 set _abbrev_num 1
1089
1090 set _cu_label [_compute_label "cu${cu_num}_begin"]
1091 set start_label [_compute_label "cu${cu_num}_start"]
1092 set end_label [_compute_label "cu${cu_num}_end"]
1093
1094 define_label $_cu_label
1095 if {$is_64} {
1096 _op .4byte 0xffffffff
1097 _op .8byte "$end_label - $start_label"
1098 } else {
1099 _op .4byte "$end_label - $start_label"
1100 }
1101 define_label $start_label
1102 _op .2byte $_cu_version Version
1103 _op .${_cu_offset_size}byte $my_abbrevs Abbrevs
1104 _op .byte $_cu_addr_size "Pointer size"
1105
1106 _defer_output $_abbrev_section {
1107 define_label $my_abbrevs
1108 }
1109
1110 uplevel $body
1111
1112 _defer_output $_abbrev_section {
1113 # Emit the terminator.
1114 _op .byte 0x0 "Abbrev end - Terminator"
1115 }
1116
1117 define_label $end_label
1118 }
1119
1120 # Emit a DWARF TU.
1121 # OPTIONS is a list with an even number of elements containing
1122 # option-name and option-value pairs.
1123 # Current options are:
1124 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1125 # default = 0 (32-bit)
1126 # version n - DWARF version number to emit
1127 # default = 4
1128 # addr_size n - the size of addresses in bytes: 4, 8, or default
1129 # default = default
1130 # fission 0|1 - boolean indicating if generating Fission debug info
1131 # default = 0
1132 # SIGNATURE is the 64-bit signature of the type.
1133 # TYPE_LABEL is the label of the type defined by this TU,
1134 # or "" if there is no type (i.e., type stubs in Fission).
1135 # BODY is Tcl code that emits the DIEs which make up the body of
1136 # the TU. It is evaluated in the caller's context.
1137 proc tu {options signature type_label body} {
1138 variable _cu_count
1139 variable _abbrev_section
1140 variable _abbrev_num
1141 variable _cu_label
1142 variable _cu_version
1143 variable _cu_addr_size
1144 variable _cu_offset_size
1145
1146 # Establish the defaults.
1147 set is_64 0
1148 set _cu_version 4
1149 set _cu_addr_size default
1150 set fission 0
1151 set section ".debug_types"
1152 set _abbrev_section ".debug_abbrev"
1153
1154 foreach { name value } $options {
1155 switch -exact -- $name {
1156 is_64 { set is_64 $value }
1157 version { set _cu_version $value }
1158 addr_size { set _cu_addr_size $value }
1159 fission { set fission $value }
1160 default { error "unknown option $name" }
1161 }
1162 }
1163 if {$_cu_addr_size == "default"} {
1164 if {[is_64_target]} {
1165 set _cu_addr_size 8
1166 } else {
1167 set _cu_addr_size 4
1168 }
1169 }
1170 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
1171 if { $fission } {
1172 set section ".debug_types.dwo"
1173 set _abbrev_section ".debug_abbrev.dwo"
1174 }
1175
1176 _section $section
1177
1178 set cu_num [incr _cu_count]
1179 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
1180 set _abbrev_num 1
1181
1182 set _cu_label [_compute_label "cu${cu_num}_begin"]
1183 set start_label [_compute_label "cu${cu_num}_start"]
1184 set end_label [_compute_label "cu${cu_num}_end"]
1185
1186 define_label $_cu_label
1187 if {$is_64} {
1188 _op .4byte 0xffffffff
1189 _op .8byte "$end_label - $start_label"
1190 } else {
1191 _op .4byte "$end_label - $start_label"
1192 }
1193 define_label $start_label
1194 _op .2byte $_cu_version Version
1195 _op .${_cu_offset_size}byte $my_abbrevs Abbrevs
1196 _op .byte $_cu_addr_size "Pointer size"
1197 _op .8byte $signature Signature
1198 if { $type_label != "" } {
1199 uplevel declare_labels $type_label
1200 upvar $type_label my_type_label
1201 if {$is_64} {
1202 _op .8byte "$my_type_label - $_cu_label"
1203 } else {
1204 _op .4byte "$my_type_label - $_cu_label"
1205 }
1206 } else {
1207 if {$is_64} {
1208 _op .8byte 0
1209 } else {
1210 _op .4byte 0
1211 }
1212 }
1213
1214 _defer_output $_abbrev_section {
1215 define_label $my_abbrevs
1216 }
1217
1218 uplevel $body
1219
1220 _defer_output $_abbrev_section {
1221 # Emit the terminator.
1222 _op .byte 0x0 "Abbrev end - Terminator"
1223 }
1224
1225 define_label $end_label
1226 }
1227
1228 # Emit a DWARF .debug_ranges unit.
1229 # OPTIONS is a list with an even number of elements containing
1230 # option-name and option-value pairs.
1231 # Current options are:
1232 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1233 # default = 0 (32-bit)
1234 #
1235 # BODY is Tcl code that emits the content of the .debug_ranges
1236 # unit, it is evaluated in the caller's context.
1237 proc ranges {options body} {
1238 variable _debug_ranges_64_bit
1239
1240 foreach { name value } $options {
1241 switch -exact -- $name {
1242 is_64 { set _debug_ranges_64_bit [subst $value] }
1243 default { error "unknown option $name" }
1244 }
1245 }
1246
1247 set section ".debug_ranges"
1248 _section $section
1249
1250 proc sequence { body } {
1251 variable _debug_ranges_64_bit
1252
1253 # Emit the sequence of addresses.
1254
1255 proc base { addr } {
1256 variable _debug_ranges_64_bit
1257
1258 if { $_debug_ranges_64_bit } then {
1259 _op .8byte 0xffffffffffffffff "Base Marker"
1260 _op .8byte $addr "Base Address"
1261 } else {
1262 _op .4byte 0xffffffff "Base Marker"
1263 _op .4byte $addr "Base Address"
1264 }
1265 }
1266
1267 proc range { start end } {
1268 variable _debug_ranges_64_bit
1269
1270 if { $_debug_ranges_64_bit } then {
1271 _op .8byte $start "Start Address"
1272 _op .8byte $end "End Address"
1273 } else {
1274 _op .4byte $start "Start Address"
1275 _op .4byte $end "End Address"
1276 }
1277 }
1278
1279 uplevel $body
1280
1281 # End of the sequence.
1282 if { $_debug_ranges_64_bit } then {
1283 _op .8byte 0x0 "End of Sequence Marker (Part 1)"
1284 _op .8byte 0x0 "End of Sequence Marker (Part 2)"
1285 } else {
1286 _op .4byte 0x0 "End of Sequence Marker (Part 1)"
1287 _op .4byte 0x0 "End of Sequence Marker (Part 2)"
1288 }
1289 }
1290
1291 uplevel $body
1292 }
1293
1294
1295 # Emit a DWARF .debug_line unit.
1296 # OPTIONS is a list with an even number of elements containing
1297 # option-name and option-value pairs.
1298 # Current options are:
1299 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1300 # default = 0 (32-bit)
1301 # version n - DWARF version number to emit
1302 # default = 4
1303 # addr_size n - the size of addresses in bytes: 4, 8, or default
1304 # default = default
1305 #
1306 # LABEL is the label of the current unit (which is probably
1307 # referenced by a DW_AT_stmt_list), or "" if there is no such
1308 # label.
1309 #
1310 # BODY is Tcl code that emits the parts which make up the body of
1311 # the line unit. It is evaluated in the caller's context. The
1312 # following commands are available for the BODY section:
1313 #
1314 # include_dir "dirname" -- adds a new include directory
1315 #
1316 # file_name "file.c" idx -- adds a new file name. IDX is a
1317 # 1-based index referencing an include directory or 0 for
1318 # current directory.
1319
1320 proc lines {options label body} {
1321 variable _line_count
1322 variable _line_saw_file
1323 variable _line_saw_program
1324 variable _line_header_end_label
1325
1326 # Establish the defaults.
1327 set is_64 0
1328 set _unit_version 4
1329 set _unit_addr_size default
1330 set _line_saw_program 0
1331 set _line_saw_file 0
1332 set _default_is_stmt 1
1333
1334 foreach { name value } $options {
1335 switch -exact -- $name {
1336 is_64 { set is_64 $value }
1337 version { set _unit_version $value }
1338 addr_size { set _unit_addr_size $value }
1339 default_is_stmt { set _default_is_stmt $value }
1340 default { error "unknown option $name" }
1341 }
1342 }
1343 if {$_unit_addr_size == "default"} {
1344 if {[is_64_target]} {
1345 set _unit_addr_size 8
1346 } else {
1347 set _unit_addr_size 4
1348 }
1349 }
1350
1351 set unit_num [incr _line_count]
1352
1353 set section ".debug_line"
1354 _section $section
1355
1356 if { "$label" != "" } {
1357 # Define the user-provided label at this point.
1358 $label:
1359 }
1360
1361 set unit_len_label [_compute_label "line${_line_count}_start"]
1362 set unit_end_label [_compute_label "line${_line_count}_end"]
1363 set header_len_label [_compute_label "line${_line_count}_header_start"]
1364 set _line_header_end_label [_compute_label "line${_line_count}_header_end"]
1365
1366 if {$is_64} {
1367 _op .4byte 0xffffffff
1368 _op .8byte "$unit_end_label - $unit_len_label" "unit_length"
1369 } else {
1370 _op .4byte "$unit_end_label - $unit_len_label" "unit_length"
1371 }
1372
1373 define_label $unit_len_label
1374
1375 _op .2byte $_unit_version version
1376
1377 if {$is_64} {
1378 _op .8byte "$_line_header_end_label - $header_len_label" "header_length"
1379 } else {
1380 _op .4byte "$_line_header_end_label - $header_len_label" "header_length"
1381 }
1382
1383 define_label $header_len_label
1384
1385 _op .byte 1 "minimum_instruction_length"
1386 _op .byte $_default_is_stmt "default_is_stmt"
1387 _op .byte 1 "line_base"
1388 _op .byte 1 "line_range"
1389 _op .byte 10 "opcode_base"
1390
1391 # The standard_opcode_lengths table. The number of arguments
1392 # for each of the standard opcodes. Generating 9 entries here
1393 # matches the use of 10 in the opcode_base above. These 9
1394 # entries match the 9 standard opcodes for DWARF2, making use
1395 # of only 9 should be fine, even if we are generating DWARF3
1396 # or DWARF4.
1397 _op .byte 0 "standard opcode 1"
1398 _op .byte 1 "standard opcode 2"
1399 _op .byte 1 "standard opcode 3"
1400 _op .byte 1 "standard opcode 4"
1401 _op .byte 1 "standard opcode 5"
1402 _op .byte 0 "standard opcode 6"
1403 _op .byte 0 "standard opcode 7"
1404 _op .byte 0 "standard opcode 8"
1405 _op .byte 1 "standard opcode 9"
1406
1407 proc include_dir {dirname} {
1408 _op .ascii [_quote $dirname]
1409 }
1410
1411 proc file_name {filename diridx} {
1412 variable _line_saw_file
1413 if "! $_line_saw_file" {
1414 # Terminate the dir list.
1415 _op .byte 0 "Terminator."
1416 set _line_saw_file 1
1417 }
1418
1419 _op .ascii [_quote $filename]
1420 _op .sleb128 $diridx
1421 _op .sleb128 0 "mtime"
1422 _op .sleb128 0 "length"
1423 }
1424
1425 proc program {statements} {
1426 variable _line_saw_program
1427 variable _line_header_end_label
1428 variable _line
1429
1430 set _line 1
1431
1432 if "! $_line_saw_program" {
1433 # Terminate the file list.
1434 _op .byte 0 "Terminator."
1435 define_label $_line_header_end_label
1436 set _line_saw_program 1
1437 }
1438
1439 proc DW_LNE_set_address {addr} {
1440 _op .byte 0
1441 set start [new_label "set_address_start"]
1442 set end [new_label "set_address_end"]
1443 _op .uleb128 "${end} - ${start}"
1444 define_label ${start}
1445 _op .byte 2
1446 if {[is_64_target]} {
1447 _op .8byte ${addr}
1448 } else {
1449 _op .4byte ${addr}
1450 }
1451 define_label ${end}
1452 }
1453
1454 proc DW_LNE_end_sequence {} {
1455 variable _line
1456 _op .byte 0
1457 _op .uleb128 1
1458 _op .byte 1
1459 set _line 1
1460 }
1461
1462 proc DW_LNE_user { len opcode } {
1463 set DW_LNE_lo_usr 0x80
1464 set DW_LNE_hi_usr 0xff
1465 if { $DW_LNE_lo_usr <= $opcode
1466 && $opcode <= $DW_LNE_hi_usr } {
1467 _op .byte 0
1468 _op .uleb128 $len
1469 _op .byte $opcode
1470 for {set i 1} {$i < $len} {incr i} {
1471 _op .byte 0
1472 }
1473 } else {
1474 error "unknown vendor specific extended opcode: $opcode"
1475 }
1476 }
1477
1478 proc DW_LNS_copy {} {
1479 _op .byte 1
1480 }
1481
1482 proc DW_LNS_negate_stmt {} {
1483 _op .byte 6
1484 }
1485
1486 proc DW_LNS_advance_pc {offset} {
1487 _op .byte 2
1488 _op .uleb128 ${offset}
1489 }
1490
1491 proc DW_LNS_advance_line {offset} {
1492 variable _line
1493 _op .byte 3
1494 _op .sleb128 ${offset}
1495 set _line [expr $_line + $offset]
1496 }
1497
1498 # A pseudo line number program instruction, that can be used instead
1499 # of DW_LNS_advance_line. Rather than writing:
1500 # {DW_LNS_advance_line [expr $line1 - 1]}
1501 # {DW_LNS_advance_line [expr $line2 - $line1]}
1502 # {DW_LNS_advance_line [expr $line3 - $line2]}
1503 # we can just write:
1504 # {line $line1}
1505 # {line $line2}
1506 # {line $line3}
1507 proc line {line} {
1508 variable _line
1509 set offset [expr $line - $_line]
1510 DW_LNS_advance_line $offset
1511 }
1512
1513 proc DW_LNS_set_file {num} {
1514 _op .byte 4
1515 _op .sleb128 ${num}
1516 }
1517
1518 foreach statement $statements {
1519 uplevel 1 $statement
1520 }
1521 }
1522
1523 uplevel $body
1524
1525 rename include_dir ""
1526 rename file_name ""
1527
1528 # Terminate dir list if we saw no files.
1529 if "! $_line_saw_file" {
1530 _op .byte 0 "Terminator."
1531 }
1532
1533 # Terminate the file list.
1534 if "! $_line_saw_program" {
1535 _op .byte 0 "Terminator."
1536 define_label $_line_header_end_label
1537 }
1538
1539 define_label $unit_end_label
1540 }
1541
1542 proc _empty_array {name} {
1543 upvar $name the_array
1544
1545 catch {unset the_array}
1546 set the_array(_) {}
1547 unset the_array(_)
1548 }
1549
1550 # Emit a .gnu_debugaltlink section with the given file name and
1551 # build-id. The buildid should be represented as a hexadecimal
1552 # string, like "ffeeddcc".
1553 proc gnu_debugaltlink {filename buildid} {
1554 _defer_output .gnu_debugaltlink {
1555 _op .ascii [_quote $filename]
1556 foreach {a b} [split $buildid {}] {
1557 _op .byte 0x$a$b
1558 }
1559 }
1560 }
1561
1562 proc _note {type name hexdata} {
1563 set namelen [expr [string length $name] + 1]
1564
1565 # Name size.
1566 _op .4byte $namelen
1567 # Data size.
1568 _op .4byte [expr [string length $hexdata] / 2]
1569 # Type.
1570 _op .4byte $type
1571 # The name.
1572 _op .ascii [_quote $name]
1573 # Alignment.
1574 set align 2
1575 set total [expr {($namelen + (1 << $align) - 1) & -(1 << $align)}]
1576 for {set i $namelen} {$i < $total} {incr i} {
1577 _op .byte 0
1578 }
1579 # The data.
1580 foreach {a b} [split $hexdata {}] {
1581 _op .byte 0x$a$b
1582 }
1583 }
1584
1585 # Emit a note section holding the given build-id.
1586 proc build_id {buildid} {
1587 _defer_output {.note.gnu.build-id a note} {
1588 # From elf/common.h.
1589 set NT_GNU_BUILD_ID 3
1590
1591 _note $NT_GNU_BUILD_ID GNU $buildid
1592 }
1593 }
1594
1595 # The top-level interface to the DWARF assembler.
1596 # FILENAME is the name of the file where the generated assembly
1597 # code is written.
1598 # BODY is Tcl code to emit the assembly. It is evaluated via
1599 # "eval" -- not uplevel as you might expect, because it is
1600 # important to run the body in the Dwarf namespace.
1601 #
1602 # A typical invocation is something like:
1603 # Dwarf::assemble $file {
1604 # cu 0 2 8 {
1605 # compile_unit {
1606 # ...
1607 # }
1608 # }
1609 # cu 0 2 8 {
1610 # ...
1611 # }
1612 # }
1613 proc assemble {filename body} {
1614 variable _initialized
1615 variable _output_file
1616 variable _deferred_output
1617 variable _defer
1618 variable _label_num
1619 variable _strings
1620 variable _cu_count
1621 variable _line_count
1622 variable _line_saw_file
1623 variable _line_saw_program
1624 variable _line_header_end_label
1625 variable _debug_ranges_64_bit
1626
1627 if {!$_initialized} {
1628 _read_constants
1629 set _initialized 1
1630 }
1631
1632 set _output_file [open $filename w]
1633 set _cu_count 0
1634 _empty_array _deferred_output
1635 set _defer ""
1636 set _label_num 0
1637 _empty_array _strings
1638
1639 set _line_count 0
1640 set _line_saw_file 0
1641 set _line_saw_program 0
1642 set _debug_ranges_64_bit [is_64_target]
1643
1644 # Not "uplevel" here, because we want to evaluate in this
1645 # namespace. This is somewhat bad because it means we can't
1646 # readily refer to outer variables.
1647 eval $body
1648
1649 _write_deferred_output
1650
1651 catch {close $_output_file}
1652 set _output_file {}
1653 }
1654 }
This page took 0.0635520000000001 seconds and 4 git commands to generate.