gdb.dwarf2: Define and use gdb_target_symbol for symbol prefixes
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / dwarf.exp
1 # Copyright 2010-2015 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 {nodebug}]
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 } {
118 global decimal gdb_prompt
119
120 set exe [standard_temp_file func_addr[pid].x]
121
122 gdb_compile $src $exe executable {debug}
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 # A DWARF assembler.
168 #
169 # All the variables in this namespace are private to the
170 # implementation. Also, any procedure whose name starts with "_" is
171 # private as well. Do not use these.
172 #
173 # Exported functions are documented at their definition.
174 #
175 # In addition to the hand-written functions documented below, this
176 # module automatically generates a function for each DWARF tag. For
177 # most tags, two forms are made: a full name, and one with the
178 # "DW_TAG_" prefix stripped. For example, you can use either
179 # 'DW_TAG_compile_unit' or 'compile_unit' interchangeably.
180 #
181 # There are two exceptions to this rule: DW_TAG_variable and
182 # DW_TAG_namespace. For these, the full name must always be used,
183 # as the short name conflicts with Tcl builtins. (Should future
184 # versions of Tcl or DWARF add more conflicts, this list will grow.
185 # If you want to be safe you should always use the full names.)
186 #
187 # Each tag procedure is defined like:
188 #
189 # proc DW_TAG_mumble {{attrs {}} {children {}}} { ... }
190 #
191 # ATTRS is an optional list of attributes.
192 # It is run through 'subst' in the caller's context before processing.
193 #
194 # Each attribute in the list has one of two forms:
195 # 1. { NAME VALUE }
196 # 2. { NAME VALUE FORM }
197 #
198 # In each case, NAME is the attribute's name.
199 # This can either be the full name, like 'DW_AT_name', or a shortened
200 # name, like 'name'. These are fully equivalent.
201 #
202 # Besides DWARF standard attributes, assembler supports 'macro' attribute
203 # which will be substituted by one or more standard or macro attributes.
204 # supported macro attributes are:
205 #
206 # - MACRO_AT_range { FUNC FILE }
207 # It is substituted by DW_AT_low_pc and DW_AT_high_pc with the start and
208 # end address of function FUNC in file FILE.
209 #
210 # - MACRO_AT_func { FUNC FILE }
211 # It is substituted by DW_AT_name with FUNC and MACRO_AT_range.
212 #
213 # If FORM is given, it should name a DW_FORM_ constant.
214 # This can either be the short form, like 'DW_FORM_addr', or a
215 # shortened version, like 'addr'. If the form is given, VALUE
216 # is its value; see below. In some cases, additional processing
217 # is done; for example, DW_FORM_strp manages the .debug_str
218 # section automatically.
219 #
220 # If FORM is 'SPECIAL_expr', then VALUE is treated as a location
221 # expression. The effective form is then DW_FORM_block, and VALUE
222 # is passed to the (internal) '_location' proc to be translated.
223 # This proc implements a miniature DW_OP_ assembler.
224 #
225 # If FORM is not given, it is guessed:
226 # * If VALUE starts with the "@" character, the rest of VALUE is
227 # looked up as a DWARF constant, and DW_FORM_sdata is used. For
228 # example, '@DW_LANG_c89' could be used.
229 # * If VALUE starts with the ":" character, then it is a label
230 # reference. The rest of VALUE is taken to be the name of a label,
231 # and DW_FORM_ref4 is used. See 'new_label' and 'define_label'.
232 # * Otherwise, VALUE is taken to be a string and DW_FORM_string is
233 # used. In order to prevent bugs where a numeric value is given but
234 # no form is specified, it is an error if the value looks like a number
235 # (using Tcl's "string is integer") and no form is provided.
236 # More form-guessing functionality may be added.
237 #
238 # CHILDREN is just Tcl code that can be used to define child DIEs. It
239 # is evaluated in the caller's context.
240 #
241 # Currently this code is missing nice support for CFA handling, and
242 # probably other things as well.
243
244 namespace eval Dwarf {
245 # True if the module has been initialized.
246 variable _initialized 0
247
248 # Constants from dwarf2.h.
249 variable _constants
250 # DW_AT short names.
251 variable _AT
252 # DW_FORM short names.
253 variable _FORM
254 # DW_OP short names.
255 variable _OP
256
257 # The current output file.
258 variable _output_file
259
260 # Note: The _cu_ values here also apply to type units (TUs).
261 # Think of a TU as a special kind of CU.
262
263 # Current CU count.
264 variable _cu_count
265
266 # The current CU's base label.
267 variable _cu_label
268
269 # The current CU's version.
270 variable _cu_version
271
272 # The current CU's address size.
273 variable _cu_addr_size
274 # The current CU's offset size.
275 variable _cu_offset_size
276
277 # Label generation number.
278 variable _label_num
279
280 # The deferred output array. The index is the section name; the
281 # contents hold the data for that section.
282 variable _deferred_output
283
284 # If empty, we should write directly to the output file.
285 # Otherwise, this is the name of a section to write to.
286 variable _defer
287
288 # The abbrev section. Typically .debug_abbrev but can be .debug_abbrev.dwo
289 # for Fission.
290 variable _abbrev_section
291
292 # The next available abbrev number in the current CU's abbrev
293 # table.
294 variable _abbrev_num
295
296 # The string table for this assembly. The key is the string; the
297 # value is the label for that string.
298 variable _strings
299
300 # Current .debug_line unit count.
301 variable _line_count
302
303 # Whether a file_name entry was seen.
304 variable _line_saw_file
305
306 proc _process_one_constant {name value} {
307 variable _constants
308 variable _AT
309 variable _FORM
310 variable _OP
311
312 set _constants($name) $value
313
314 if {![regexp "^DW_(\[A-Z\]+)_(\[A-Za-z0-9_\]+)$" $name \
315 ignore prefix name2]} {
316 error "non-matching name: $name"
317 }
318
319 if {$name2 == "lo_user" || $name2 == "hi_user"} {
320 return
321 }
322
323 # We only try to shorten some very common things.
324 # FIXME: CFA?
325 switch -exact -- $prefix {
326 TAG {
327 # Create two procedures for the tag. These call
328 # _handle_DW_TAG with the full tag name baked in; this
329 # does all the actual work.
330 proc $name {{attrs {}} {children {}}} \
331 "_handle_DW_TAG $name \$attrs \$children"
332
333 # Filter out ones that are known to clash.
334 if {$name2 == "variable" || $name2 == "namespace"} {
335 set name2 "tag_$name2"
336 }
337
338 if {[info commands $name2] != {}} {
339 error "duplicate proc name: from $name"
340 }
341
342 proc $name2 {{attrs {}} {children {}}} \
343 "_handle_DW_TAG $name \$attrs \$children"
344 }
345
346 AT {
347 set _AT($name2) $name
348 }
349
350 FORM {
351 set _FORM($name2) $name
352 }
353
354 OP {
355 set _OP($name2) $name
356 }
357
358 default {
359 return
360 }
361 }
362 }
363
364 proc _read_constants {} {
365 global srcdir hex decimal
366 variable _constants
367
368 # DWARF name-matching regexp.
369 set dwrx "DW_\[a-zA-Z0-9_\]+"
370 # Whitespace regexp.
371 set ws "\[ \t\]+"
372
373 set fd [open [file join $srcdir .. .. include dwarf2.h]]
374 while {![eof $fd]} {
375 set line [gets $fd]
376 if {[regexp -- "^${ws}($dwrx)${ws}=${ws}($hex|$decimal),?$" \
377 $line ignore name value ignore2]} {
378 _process_one_constant $name $value
379 }
380 }
381 close $fd
382
383 set fd [open [file join $srcdir .. .. include dwarf2.def]]
384 while {![eof $fd]} {
385 set line [gets $fd]
386 if {[regexp -- \
387 "^DW_\[A-Z_\]+${ws}\\(($dwrx),${ws}($hex|$decimal)\\)$" \
388 $line ignore name value ignore2]} {
389 _process_one_constant $name $value
390 }
391 }
392 close $fd
393
394 set _constants(SPECIAL_expr) $_constants(DW_FORM_block)
395 }
396
397 proc _quote {string} {
398 # FIXME
399 return "\"${string}\\0\""
400 }
401
402 proc _nz_quote {string} {
403 # For now, no quoting is done.
404 return "\"${string}\""
405 }
406
407 proc _handle_DW_FORM {form value} {
408 switch -exact -- $form {
409 DW_FORM_string {
410 _op .ascii [_quote $value]
411 }
412
413 DW_FORM_flag_present {
414 # We don't need to emit anything.
415 }
416
417 DW_FORM_data4 -
418 DW_FORM_ref4 {
419 _op .4byte $value
420 }
421
422 DW_FORM_ref_addr {
423 variable _cu_offset_size
424 variable _cu_version
425 variable _cu_addr_size
426
427 if {$_cu_version == 2} {
428 set size $_cu_addr_size
429 } else {
430 set size $_cu_offset_size
431 }
432
433 _op .${size}byte $value
434 }
435
436 DW_FORM_sec_offset {
437 variable _cu_offset_size
438 _op .${_cu_offset_size}byte $value
439 }
440
441 DW_FORM_ref1 -
442 DW_FORM_flag -
443 DW_FORM_data1 {
444 _op .byte $value
445 }
446
447 DW_FORM_sdata {
448 _op .sleb128 $value
449 }
450
451 DW_FORM_ref_udata -
452 DW_FORM_udata {
453 _op .uleb128 $value
454 }
455
456 DW_FORM_addr {
457 variable _cu_addr_size
458
459 _op .${_cu_addr_size}byte $value
460 }
461
462 DW_FORM_data2 -
463 DW_FORM_ref2 {
464 _op .2byte $value
465 }
466
467 DW_FORM_data8 -
468 DW_FORM_ref8 -
469 DW_FORM_ref_sig8 {
470 _op .8byte $value
471 }
472
473 DW_FORM_strp {
474 variable _strings
475 variable _cu_offset_size
476
477 if {![info exists _strings($value)]} {
478 set _strings($value) [new_label strp]
479 _defer_output .debug_string {
480 define_label $_strings($value)
481 _op .ascii [_quote $value]
482 }
483 }
484
485 _op .${_cu_offset_size}byte $_strings($value) "strp: $value"
486 }
487
488 SPECIAL_expr {
489 set l1 [new_label "expr_start"]
490 set l2 [new_label "expr_end"]
491 _op .uleb128 "$l2 - $l1" "expression"
492 define_label $l1
493 _location $value
494 define_label $l2
495 }
496
497 DW_FORM_block1 {
498 set len [string length $value]
499 if {$len > 255} {
500 error "DW_FORM_block1 length too long"
501 }
502 _op .byte $len
503 _op .ascii [_nz_quote $value]
504 }
505
506 DW_FORM_block2 -
507 DW_FORM_block4 -
508
509 DW_FORM_block -
510
511 DW_FORM_ref2 -
512 DW_FORM_indirect -
513 DW_FORM_exprloc -
514
515 DW_FORM_GNU_addr_index -
516 DW_FORM_GNU_str_index -
517 DW_FORM_GNU_ref_alt -
518 DW_FORM_GNU_strp_alt -
519
520 default {
521 error "unhandled form $form"
522 }
523 }
524 }
525
526 proc _guess_form {value varname} {
527 upvar $varname new_value
528
529 switch -exact -- [string range $value 0 0] {
530 @ {
531 # Constant reference.
532 variable _constants
533
534 set new_value $_constants([string range $value 1 end])
535 # Just the simplest.
536 return DW_FORM_sdata
537 }
538
539 : {
540 # Label reference.
541 variable _cu_label
542
543 set new_value "[string range $value 1 end] - $_cu_label"
544
545 return DW_FORM_ref4
546 }
547
548 default {
549 return DW_FORM_string
550 }
551 }
552 }
553
554 # Map NAME to its canonical form.
555 proc _map_name {name ary} {
556 variable $ary
557
558 if {[info exists ${ary}($name)]} {
559 set name [set ${ary}($name)]
560 }
561
562 return $name
563 }
564
565 proc _handle_attribute { attr_name attr_value attr_form } {
566 variable _abbrev_section
567 variable _constants
568
569 _handle_DW_FORM $attr_form $attr_value
570
571 _defer_output $_abbrev_section {
572 _op .uleb128 $_constants($attr_name) $attr_name
573 _op .uleb128 $_constants($attr_form) $attr_form
574 }
575 }
576
577 # Handle macro attribute MACRO_AT_range.
578
579 proc _handle_macro_at_range { attr_value } {
580 if {[llength $attr_value] != 2} {
581 error "usage: MACRO_AT_range { func file }"
582 }
583
584 set func [lindex $attr_value 0]
585 set src [lindex $attr_value 1]
586 set result [function_range $func $src]
587
588 _handle_attribute DW_AT_low_pc [lindex $result 0] \
589 DW_FORM_addr
590 _handle_attribute DW_AT_high_pc \
591 "[lindex $result 0] + [lindex $result 1]" DW_FORM_addr
592 }
593
594 # Handle macro attribute MACRO_AT_func.
595
596 proc _handle_macro_at_func { attr_value } {
597 if {[llength $attr_value] != 2} {
598 error "usage: MACRO_AT_func { func file }"
599 }
600 _handle_attribute DW_AT_name [lindex $attr_value 0] DW_FORM_string
601 _handle_macro_at_range $attr_value
602 }
603
604 proc _handle_DW_TAG {tag_name {attrs {}} {children {}}} {
605 variable _abbrev_section
606 variable _abbrev_num
607 variable _constants
608
609 set has_children [expr {[string length $children] > 0}]
610 set my_abbrev [incr _abbrev_num]
611
612 # We somewhat wastefully emit a new abbrev entry for each tag.
613 # There's no reason for this other than laziness.
614 _defer_output $_abbrev_section {
615 _op .uleb128 $my_abbrev "Abbrev start"
616 _op .uleb128 $_constants($tag_name) $tag_name
617 _op .byte $has_children "has_children"
618 }
619
620 _op .uleb128 $my_abbrev "Abbrev ($tag_name)"
621
622 foreach attr $attrs {
623 set attr_name [_map_name [lindex $attr 0] _AT]
624
625 # When the length of ATTR is greater than 2, the last
626 # element of the list must be a form. The second through
627 # the penultimate elements are joined together and
628 # evaluated using subst. This allows constructs such as
629 # [gdb_target_symbol foo] to be used.
630
631 if {[llength $attr] > 2} {
632 set attr_value [uplevel 2 [list subst [join [lrange $attr 1 end-1]]]]
633 } else {
634 set attr_value [uplevel 2 [list subst [lindex $attr 1]]]
635 }
636
637 if { [string equal "MACRO_AT_func" $attr_name] } {
638 _handle_macro_at_func $attr_value
639 } elseif { [string equal "MACRO_AT_range" $attr_name] } {
640 _handle_macro_at_range $attr_value
641 } else {
642 if {[llength $attr] > 2} {
643 set attr_form [lindex $attr end]
644 } else {
645 # If the value looks like an integer, a form is required.
646 if [string is integer $attr_value] {
647 error "Integer value requires a form"
648 }
649 set attr_form [_guess_form $attr_value attr_value]
650 }
651 set attr_form [_map_name $attr_form _FORM]
652
653 _handle_attribute $attr_name $attr_value $attr_form
654 }
655 }
656
657 _defer_output $_abbrev_section {
658 # Terminator.
659 _op .byte 0x0 Terminator
660 _op .byte 0x0 Terminator
661 }
662
663 if {$has_children} {
664 uplevel 2 $children
665
666 # Terminate children.
667 _op .byte 0x0 "Terminate children"
668 }
669 }
670
671 proc _emit {string} {
672 variable _output_file
673 variable _defer
674 variable _deferred_output
675
676 if {$_defer == ""} {
677 puts $_output_file $string
678 } else {
679 append _deferred_output($_defer) ${string}\n
680 }
681 }
682
683 proc _section {name {flags ""} {type ""}} {
684 if {$flags == "" && $type == ""} {
685 _emit " .section $name"
686 } elseif {$type == ""} {
687 _emit " .section $name, \"$flags\""
688 } else {
689 _emit " .section $name, \"$flags\", %$type"
690 }
691 }
692
693 # SECTION_SPEC is a list of arguments to _section.
694 proc _defer_output {section_spec body} {
695 variable _defer
696 variable _deferred_output
697
698 set old_defer $_defer
699 set _defer [lindex $section_spec 0]
700
701 if {![info exists _deferred_output($_defer)]} {
702 set _deferred_output($_defer) ""
703 eval _section $section_spec
704 }
705
706 uplevel $body
707
708 set _defer $old_defer
709 }
710
711 proc _defer_to_string {body} {
712 variable _defer
713 variable _deferred_output
714
715 set old_defer $_defer
716 set _defer temp
717
718 set _deferred_output($_defer) ""
719
720 uplevel $body
721
722 set result $_deferred_output($_defer)
723 unset _deferred_output($_defer)
724
725 set _defer $old_defer
726 return $result
727 }
728
729 proc _write_deferred_output {} {
730 variable _output_file
731 variable _deferred_output
732
733 foreach section [array names _deferred_output] {
734 # The data already has a newline.
735 puts -nonewline $_output_file $_deferred_output($section)
736 }
737
738 # Save some memory.
739 unset _deferred_output
740 }
741
742 proc _op {name value {comment ""}} {
743 set text " ${name} ${value}"
744 if {$comment != ""} {
745 # Try to make stuff line up nicely.
746 while {[string length $text] < 40} {
747 append text " "
748 }
749 append text "/* ${comment} */"
750 }
751 _emit $text
752 }
753
754 proc _compute_label {name} {
755 return ".L${name}"
756 }
757
758 # Return a name suitable for use as a label. If BASE_NAME is
759 # specified, it is incorporated into the label name; this is to
760 # make debugging the generated assembler easier. If BASE_NAME is
761 # not specified a generic default is used. This proc does not
762 # define the label; see 'define_label'. 'new_label' attempts to
763 # ensure that label names are unique.
764 proc new_label {{base_name label}} {
765 variable _label_num
766
767 return [_compute_label ${base_name}[incr _label_num]]
768 }
769
770 # Define a label named NAME. Ordinarily, NAME comes from a call
771 # to 'new_label', but this is not required.
772 proc define_label {name} {
773 _emit "${name}:"
774 }
775
776 # Declare a global label. This is typically used to refer to
777 # labels defined in other files, for example a function defined in
778 # a .c file.
779 proc extern {args} {
780 foreach name $args {
781 _op .global $name
782 }
783 }
784
785 # A higher-level interface to label handling.
786 #
787 # ARGS is a list of label descriptors. Each one is either a
788 # single element, or a list of two elements -- a name and some
789 # text. For each descriptor, 'new_label' is invoked. If the list
790 # form is used, the second element in the list is passed as an
791 # argument. The label name is used to define a variable in the
792 # enclosing scope; this can be used to refer to the label later.
793 # The label name is also used to define a new proc whose name is
794 # the label name plus a trailing ":". This proc takes a body as
795 # an argument and can be used to define the label at that point;
796 # then the body, if any, is evaluated in the caller's context.
797 #
798 # For example:
799 #
800 # declare_labels int_label
801 # something { ... $int_label } ;# refer to the label
802 # int_label: constant { ... } ;# define the label
803 proc declare_labels {args} {
804 foreach arg $args {
805 set name [lindex $arg 0]
806 set text [lindex $arg 1]
807
808 upvar $name label_var
809 if {$text == ""} {
810 set label_var [new_label]
811 } else {
812 set label_var [new_label $text]
813 }
814
815 proc ${name}: {args} [format {
816 define_label %s
817 uplevel $args
818 } $label_var]
819 }
820 }
821
822 # This is a miniature assembler for location expressions. It is
823 # suitable for use in the attributes to a DIE. Its output is
824 # prefixed with "=" to make it automatically use DW_FORM_block.
825 # BODY is split by lines, and each line is taken to be a list.
826 # (FIXME should use 'info complete' here.)
827 # Each list's first element is the opcode, either short or long
828 # forms are accepted.
829 # FIXME argument handling
830 # FIXME move docs
831 proc _location {body} {
832 variable _constants
833 variable _cu_label
834 variable _cu_addr_size
835 variable _cu_offset_size
836
837 foreach line [split $body \n] {
838 # Ignore blank lines, and allow embedded comments.
839 if {[lindex $line 0] == "" || [regexp -- {^[ \t]*#} $line]} {
840 continue
841 }
842 set opcode [_map_name [lindex $line 0] _OP]
843 _op .byte $_constants($opcode) $opcode
844
845 switch -exact -- $opcode {
846 DW_OP_addr {
847 _op .${_cu_addr_size}byte [lindex $line 1]
848 }
849
850 DW_OP_regx {
851 _op .uleb128 [lindex $line 1]
852 }
853
854 DW_OP_pick -
855 DW_OP_const1u -
856 DW_OP_const1s {
857 _op .byte [lindex $line 1]
858 }
859
860 DW_OP_const2u -
861 DW_OP_const2s {
862 _op .2byte [lindex $line 1]
863 }
864
865 DW_OP_const4u -
866 DW_OP_const4s {
867 _op .4byte [lindex $line 1]
868 }
869
870 DW_OP_const8u -
871 DW_OP_const8s {
872 _op .8byte [lindex $line 1]
873 }
874
875 DW_OP_constu {
876 _op .uleb128 [lindex $line 1]
877 }
878 DW_OP_consts {
879 _op .sleb128 [lindex $line 1]
880 }
881
882 DW_OP_plus_uconst {
883 _op .uleb128 [lindex $line 1]
884 }
885
886 DW_OP_piece {
887 _op .uleb128 [lindex $line 1]
888 }
889
890 DW_OP_bit_piece {
891 _op .uleb128 [lindex $line 1]
892 _op .uleb128 [lindex $line 2]
893 }
894
895 DW_OP_skip -
896 DW_OP_bra {
897 _op .2byte [lindex $line 1]
898 }
899
900 DW_OP_GNU_implicit_pointer {
901 if {[llength $line] != 3} {
902 error "usage: DW_OP_GNU_implicit_pointer LABEL OFFSET"
903 }
904
905 # Here label is a section offset.
906 set label [lindex $line 1]
907 _op .${_cu_offset_size}byte $label
908 _op .sleb128 [lindex $line 2]
909 }
910
911 DW_OP_deref_size {
912 if {[llength $line] != 2} {
913 error "usage: DW_OP_deref_size SIZE"
914 }
915
916 _op .byte [lindex $line 1]
917 }
918
919 DW_OP_bregx {
920 _op .uleb128 [lindex $line 1]
921 _op .sleb128 [lindex $line 2]
922 }
923
924 default {
925 if {[llength $line] > 1} {
926 error "Unimplemented: operands in location for $opcode"
927 }
928 }
929 }
930 }
931 }
932
933 # Emit a DWARF CU.
934 # OPTIONS is a list with an even number of elements containing
935 # option-name and option-value pairs.
936 # Current options are:
937 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
938 # default = 0 (32-bit)
939 # version n - DWARF version number to emit
940 # default = 4
941 # addr_size n - the size of addresses, 32, 64, or default
942 # default = default
943 # fission 0|1 - boolean indicating if generating Fission debug info
944 # default = 0
945 # BODY is Tcl code that emits the DIEs which make up the body of
946 # the CU. It is evaluated in the caller's context.
947 proc cu {options body} {
948 variable _cu_count
949 variable _abbrev_section
950 variable _abbrev_num
951 variable _cu_label
952 variable _cu_version
953 variable _cu_addr_size
954 variable _cu_offset_size
955
956 # Establish the defaults.
957 set is_64 0
958 set _cu_version 4
959 set _cu_addr_size default
960 set fission 0
961 set section ".debug_info"
962 set _abbrev_section ".debug_abbrev"
963
964 foreach { name value } $options {
965 switch -exact -- $name {
966 is_64 { set is_64 $value }
967 version { set _cu_version $value }
968 addr_size { set _cu_addr_size $value }
969 fission { set fission $value }
970 default { error "unknown option $name" }
971 }
972 }
973 if {$_cu_addr_size == "default"} {
974 if {[is_64_target]} {
975 set _cu_addr_size 8
976 } else {
977 set _cu_addr_size 4
978 }
979 }
980 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
981 if { $fission } {
982 set section ".debug_info.dwo"
983 set _abbrev_section ".debug_abbrev.dwo"
984 }
985
986 _section $section
987
988 set cu_num [incr _cu_count]
989 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
990 set _abbrev_num 1
991
992 set _cu_label [_compute_label "cu${cu_num}_begin"]
993 set start_label [_compute_label "cu${cu_num}_start"]
994 set end_label [_compute_label "cu${cu_num}_end"]
995
996 define_label $_cu_label
997 if {$is_64} {
998 _op .4byte 0xffffffff
999 _op .8byte "$end_label - $start_label"
1000 } else {
1001 _op .4byte "$end_label - $start_label"
1002 }
1003 define_label $start_label
1004 _op .2byte $_cu_version Version
1005 _op .${_cu_offset_size}byte $my_abbrevs Abbrevs
1006 _op .byte $_cu_addr_size "Pointer size"
1007
1008 _defer_output $_abbrev_section {
1009 define_label $my_abbrevs
1010 }
1011
1012 uplevel $body
1013
1014 _defer_output $_abbrev_section {
1015 # Emit the terminator.
1016 _op .byte 0x0 Terminator
1017 _op .byte 0x0 Terminator
1018 }
1019
1020 define_label $end_label
1021 }
1022
1023 # Emit a DWARF TU.
1024 # OPTIONS is a list with an even number of elements containing
1025 # option-name and option-value pairs.
1026 # Current options are:
1027 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1028 # default = 0 (32-bit)
1029 # version n - DWARF version number to emit
1030 # default = 4
1031 # addr_size n - the size of addresses, 32, 64, or default
1032 # default = default
1033 # fission 0|1 - boolean indicating if generating Fission debug info
1034 # default = 0
1035 # SIGNATURE is the 64-bit signature of the type.
1036 # TYPE_LABEL is the label of the type defined by this TU,
1037 # or "" if there is no type (i.e., type stubs in Fission).
1038 # BODY is Tcl code that emits the DIEs which make up the body of
1039 # the TU. It is evaluated in the caller's context.
1040 proc tu {options signature type_label body} {
1041 variable _cu_count
1042 variable _abbrev_section
1043 variable _abbrev_num
1044 variable _cu_label
1045 variable _cu_version
1046 variable _cu_addr_size
1047 variable _cu_offset_size
1048
1049 # Establish the defaults.
1050 set is_64 0
1051 set _cu_version 4
1052 set _cu_addr_size default
1053 set fission 0
1054 set section ".debug_types"
1055 set _abbrev_section ".debug_abbrev"
1056
1057 foreach { name value } $options {
1058 switch -exact -- $name {
1059 is_64 { set is_64 $value }
1060 version { set _cu_version $value }
1061 addr_size { set _cu_addr_size $value }
1062 fission { set fission $value }
1063 default { error "unknown option $name" }
1064 }
1065 }
1066 if {$_cu_addr_size == "default"} {
1067 if {[is_64_target]} {
1068 set _cu_addr_size 8
1069 } else {
1070 set _cu_addr_size 4
1071 }
1072 }
1073 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
1074 if { $fission } {
1075 set section ".debug_types.dwo"
1076 set _abbrev_section ".debug_abbrev.dwo"
1077 }
1078
1079 _section $section
1080
1081 set cu_num [incr _cu_count]
1082 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
1083 set _abbrev_num 1
1084
1085 set _cu_label [_compute_label "cu${cu_num}_begin"]
1086 set start_label [_compute_label "cu${cu_num}_start"]
1087 set end_label [_compute_label "cu${cu_num}_end"]
1088
1089 define_label $_cu_label
1090 if {$is_64} {
1091 _op .4byte 0xffffffff
1092 _op .8byte "$end_label - $start_label"
1093 } else {
1094 _op .4byte "$end_label - $start_label"
1095 }
1096 define_label $start_label
1097 _op .2byte $_cu_version Version
1098 _op .${_cu_offset_size}byte $my_abbrevs Abbrevs
1099 _op .byte $_cu_addr_size "Pointer size"
1100 _op .8byte $signature Signature
1101 if { $type_label != "" } {
1102 uplevel declare_labels $type_label
1103 upvar $type_label my_type_label
1104 if {$is_64} {
1105 _op .8byte "$my_type_label - $_cu_label"
1106 } else {
1107 _op .4byte "$my_type_label - $_cu_label"
1108 }
1109 } else {
1110 if {$is_64} {
1111 _op .8byte 0
1112 } else {
1113 _op .4byte 0
1114 }
1115 }
1116
1117 _defer_output $_abbrev_section {
1118 define_label $my_abbrevs
1119 }
1120
1121 uplevel $body
1122
1123 _defer_output $_abbrev_section {
1124 # Emit the terminator.
1125 _op .byte 0x0 Terminator
1126 _op .byte 0x0 Terminator
1127 }
1128
1129 define_label $end_label
1130 }
1131
1132 # Emit a DWARF .debug_line unit.
1133 # OPTIONS is a list with an even number of elements containing
1134 # option-name and option-value pairs.
1135 # Current options are:
1136 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1137 # default = 0 (32-bit)
1138 # version n - DWARF version number to emit
1139 # default = 4
1140 # addr_size n - the size of addresses, 32, 64, or default
1141 # default = default
1142 #
1143 # LABEL is the label of the current unit (which is probably
1144 # referenced by a DW_AT_stmt_list), or "" if there is no such
1145 # label.
1146 #
1147 # BODY is Tcl code that emits the parts which make up the body of
1148 # the line unit. It is evaluated in the caller's context. The
1149 # following commands are available for the BODY section:
1150 #
1151 # include_dir "dirname" -- adds a new include directory
1152 #
1153 # file_name "file.c" idx -- adds a new file name. IDX is a
1154 # 1-based index referencing an include directory or 0 for
1155 # current directory.
1156
1157 proc lines {options label body} {
1158 variable _line_count
1159 variable _line_saw_file
1160
1161 # Establish the defaults.
1162 set is_64 0
1163 set _unit_version 4
1164 set _unit_addr_size default
1165
1166 foreach { name value } $options {
1167 switch -exact -- $name {
1168 is_64 { set is_64 $value }
1169 version { set _unit_version $value }
1170 addr_size { set _unit_addr_size $value }
1171 default { error "unknown option $name" }
1172 }
1173 }
1174 if {$_unit_addr_size == "default"} {
1175 if {[is_64_target]} {
1176 set _unit_addr_size 8
1177 } else {
1178 set _unit_addr_size 4
1179 }
1180 }
1181
1182 set unit_num [incr _line_count]
1183
1184 set section ".debug_line"
1185 _section $section
1186
1187 if { "$label" != "" } {
1188 # Define the user-provided label at this point.
1189 $label:
1190 }
1191
1192 set unit_len_label [_compute_label "line${_line_count}_start"]
1193 set unit_end_label [_compute_label "line${_line_count}_end"]
1194 set header_len_label [_compute_label "line${_line_count}_header_start"]
1195 set header_end_label [_compute_label "line${_line_count}_header_end"]
1196
1197 if {$is_64} {
1198 _op .4byte 0xffffffff
1199 _op .8byte "$unit_end_label - $unit_len_label" "unit_length"
1200 } else {
1201 _op .4byte "$unit_end_label - $unit_len_label" "unit_length"
1202 }
1203
1204 define_label $unit_len_label
1205
1206 _op .2byte $_unit_version version
1207
1208 if {$is_64} {
1209 _op .8byte "$header_end_label - $header_len_label" "header_length"
1210 } else {
1211 _op .4byte "$header_end_label - $header_len_label" "header_length"
1212 }
1213
1214 define_label $header_len_label
1215
1216 _op .byte 1 "minimum_instruction_length"
1217 _op .byte 0 "default_is_stmt"
1218 _op .byte 1 "line_base"
1219 _op .byte 1 "line_range"
1220 _op .byte 1 "opcode_base"
1221 # Since we emit opcode_base==1, we skip
1222 # standard_opcode_length table altogether.
1223
1224 proc include_dir {dirname} {
1225 _op .ascii [_quote $dirname]
1226 }
1227
1228 proc file_name {filename diridx} {
1229 variable _line_saw_file
1230 if "! $_line_saw_file" {
1231 # Terminate the dir list.
1232 _op .byte 0 "Terminator."
1233 set _line_saw_file 1
1234 }
1235
1236 _op .ascii [_quote $filename]
1237 _op .sleb128 $diridx
1238 _op .sleb128 0 "mtime"
1239 _op .sleb128 0 "length"
1240 }
1241
1242 uplevel $body
1243
1244 rename include_dir ""
1245 rename file_name ""
1246
1247 # Terminate dir list if we saw no files.
1248 if "! $_line_saw_file" {
1249 _op .byte 0 "Terminator."
1250 }
1251
1252 # Terminate the file list.
1253 _op .byte 0 "Terminator."
1254
1255 define_label $header_end_label
1256 define_label $unit_end_label
1257 }
1258
1259 proc _empty_array {name} {
1260 upvar $name the_array
1261
1262 catch {unset the_array}
1263 set the_array(_) {}
1264 unset the_array(_)
1265 }
1266
1267 # Emit a .gnu_debugaltlink section with the given file name and
1268 # build-id. The buildid should be represented as a hexadecimal
1269 # string, like "ffeeddcc".
1270 proc gnu_debugaltlink {filename buildid} {
1271 _defer_output .gnu_debugaltlink {
1272 _op .ascii [_quote $filename]
1273 foreach {a b} [split $buildid {}] {
1274 _op .byte 0x$a$b
1275 }
1276 }
1277 }
1278
1279 proc _note {type name hexdata} {
1280 set namelen [expr [string length $name] + 1]
1281
1282 # Name size.
1283 _op .4byte $namelen
1284 # Data size.
1285 _op .4byte [expr [string length $hexdata] / 2]
1286 # Type.
1287 _op .4byte $type
1288 # The name.
1289 _op .ascii [_quote $name]
1290 # Alignment.
1291 set align 2
1292 set total [expr {($namelen + (1 << $align) - 1) & (-1 << $align)}]
1293 for {set i $namelen} {$i < $total} {incr i} {
1294 _op .byte 0
1295 }
1296 # The data.
1297 foreach {a b} [split $hexdata {}] {
1298 _op .byte 0x$a$b
1299 }
1300 }
1301
1302 # Emit a note section holding the given build-id.
1303 proc build_id {buildid} {
1304 _defer_output {.note.gnu.build-id a note} {
1305 # From elf/common.h.
1306 set NT_GNU_BUILD_ID 3
1307
1308 _note $NT_GNU_BUILD_ID GNU $buildid
1309 }
1310 }
1311
1312 # The top-level interface to the DWARF assembler.
1313 # FILENAME is the name of the file where the generated assembly
1314 # code is written.
1315 # BODY is Tcl code to emit the assembly. It is evaluated via
1316 # "eval" -- not uplevel as you might expect, because it is
1317 # important to run the body in the Dwarf namespace.
1318 #
1319 # A typical invocation is something like:
1320 # Dwarf::assemble $file {
1321 # cu 0 2 8 {
1322 # compile_unit {
1323 # ...
1324 # }
1325 # }
1326 # cu 0 2 8 {
1327 # ...
1328 # }
1329 # }
1330 proc assemble {filename body} {
1331 variable _initialized
1332 variable _output_file
1333 variable _deferred_output
1334 variable _defer
1335 variable _label_num
1336 variable _strings
1337 variable _cu_count
1338 variable _line_count
1339 variable _line_saw_file
1340
1341 if {!$_initialized} {
1342 _read_constants
1343 set _initialized 1
1344 }
1345
1346 set _output_file [open $filename w]
1347 set _cu_count 0
1348 _empty_array _deferred_output
1349 set _defer ""
1350 set _label_num 0
1351 _empty_array _strings
1352
1353 set _line_count 0
1354 set _line_saw_file 0
1355
1356 # Not "uplevel" here, because we want to evaluate in this
1357 # namespace. This is somewhat bad because it means we can't
1358 # readily refer to outer variables.
1359 eval $body
1360
1361 _write_deferred_output
1362
1363 catch {close $_output_file}
1364 set _output_file {}
1365 }
1366 }
This page took 0.05872 seconds and 4 git commands to generate.