1 # Copyright 2010-2013 Free Software Foundation, Inc.
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.
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.
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/>.
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*]} {
34 # All the variables in this namespace are private to the
35 # implementation. Also, any procedure whose name starts with "_" is
36 # private as well. Do not use these.
38 # Exported functions are documented at their definition.
40 # In addition to the hand-written functions documented below, this
41 # module automatically generates a function for each DWARF tag. For
42 # most tags, two forms are made: a full name, and one with the
43 # "DW_TAG_" prefix stripped. For example, you can use either
44 # 'DW_TAG_compile_unit' or 'compile_unit' interchangeably.
46 # There are two exceptions to this rule: DW_TAG_variable and
47 # DW_TAG_namespace. For these, the full name must always be used,
48 # as the short name conflicts with Tcl builtins. (Should future
49 # versions of Tcl or DWARF add more conflicts, this list will grow.
50 # If you want to be safe you should always use the full names.)
52 # Each tag procedure is defined like:
54 # proc DW_TAG_mumble {{attrs {}} {children {}}} { ... }
56 # ATTRS is an optional list of attributes.
57 # It is run through 'subst' in the caller's context before processing.
59 # Each attribute in the list has one of two forms:
61 # 2. { NAME VALUE FORM }
63 # In each case, NAME is the attribute's name.
64 # This can either be the full name, like 'DW_AT_name', or a shortened
65 # name, like 'name'. These are fully equivalent.
67 # If FORM is given, it should name a DW_FORM_ constant.
68 # This can either be the short form, like 'DW_FORM_addr', or a
69 # shortened version, like 'addr'. If the form is given, VALUE
70 # is its value; see below. In some cases, additional processing
71 # is done; for example, DW_FORM_strp manages the .debug_str
72 # section automatically.
74 # If FORM is 'SPECIAL_expr', then VALUE is treated as a location
75 # expression. The effective form is then DW_FORM_block, and VALUE
76 # is passed to the (internal) '_location' proc to be translated.
77 # This proc implements a miniature DW_OP_ assembler.
79 # If FORM is not given, it is guessed:
80 # * If VALUE starts with the "@" character, the rest of VALUE is
81 # looked up as a DWARF constant, and DW_FORM_sdata is used. For
82 # example, '@DW_LANG_c89' could be used.
83 # * If VALUE starts with the ":" character, then it is a label
84 # reference. The rest of VALUE is taken to be the name of a label,
85 # and DW_FORM_ref4 is used. See 'new_label' and 'define_label'.
86 # * Otherwise, VALUE is taken to be a string and DW_FORM_string is
88 # More form-guessing functionality may be added.
90 # CHILDREN is just Tcl code that can be used to define child DIEs. It
91 # is evaluated in the caller's context.
93 # Currently this code is missing nice support for CFA handling, and
94 # probably other things as well.
96 namespace eval Dwarf {
97 # True if the module has been initialized.
98 variable _initialized 0
100 # Constants from dwarf2.h.
104 # DW_FORM short names.
109 # The current output file.
110 variable _output_file
112 # Note: The _cu_ values here also apply to type units (TUs).
113 # Think of a TU as a special kind of CU.
118 # The current CU's base label.
121 # The current CU's version.
124 # The current CU's address size.
125 variable _cu_addr_size
126 # The current CU's offset size.
127 variable _cu_offset_size
129 # Label generation number.
132 # The deferred output array. The index is the section name; the
133 # contents hold the data for that section.
134 variable _deferred_output
136 # If empty, we should write directly to the output file.
137 # Otherwise, this is the name of a section to write to.
140 # The abbrev section. Typically .debug_abbrev but can be .debug_abbrev.dwo
142 variable _abbrev_section
144 # The next available abbrev number in the current CU's abbrev
148 # The string table for this assembly. The key is the string; the
149 # value is the label for that string.
152 proc _process_one_constant {name value} {
158 set _constants($name) $value
160 if {![regexp "^DW_(\[A-Z\]+)_(\[A-Za-z0-9_\]+)$" $name \
161 ignore prefix name2]} {
162 error "non-matching name: $name"
165 if {$name2 == "lo_user" || $name2 == "hi_user"} {
169 # We only try to shorten some very common things.
171 switch -exact -- $prefix {
173 # Create two procedures for the tag. These call
174 # _handle_DW_TAG with the full tag name baked in; this
175 # does all the actual work.
176 proc $name {{attrs {}} {children {}}} \
177 "_handle_DW_TAG $name \$attrs \$children"
179 # Filter out ones that are known to clash.
180 if {$name2 == "variable" || $name2 == "namespace"} {
181 set name2 "tag_$name2"
184 if {[info commands $name2] != {}} {
185 error "duplicate proc name: from $name"
188 proc $name2 {{attrs {}} {children {}}} \
189 "_handle_DW_TAG $name \$attrs \$children"
193 set _AT($name2) $name
197 set _FORM($name2) $name
201 set _OP($name2) $name
210 proc _read_constants {} {
211 global srcdir hex decimal
214 # DWARF name-matching regexp.
215 set dwrx "DW_\[a-zA-Z0-9_\]+"
219 set fd [open [file join $srcdir .. .. include dwarf2.h]]
222 if {[regexp -- "^${ws}($dwrx)${ws}=${ws}($hex|$decimal),?$" \
223 $line ignore name value ignore2]} {
224 _process_one_constant $name $value
229 set fd [open [file join $srcdir .. .. include dwarf2.def]]
233 "^DW_\[A-Z_\]+${ws}\\(($dwrx),${ws}($hex|$decimal)\\)$" \
234 $line ignore name value ignore2]} {
235 _process_one_constant $name $value
240 set _constants(SPECIAL_expr) $_constants(DW_FORM_block)
243 proc _quote {string} {
245 return "\"${string}\\0\""
248 proc _nz_quote {string} {
249 # For now, no quoting is done.
250 return "\"${string}\""
253 proc _handle_DW_FORM {form value} {
254 switch -exact -- $form {
256 _op .ascii [_quote $value]
259 DW_FORM_flag_present {
260 # We don't need to emit anything.
269 variable _cu_offset_size
271 variable _cu_addr_size
273 if {$_cu_version == 2} {
274 set size $_cu_addr_size
276 set size $_cu_offset_size
279 _op .${size}byte $value
298 variable _cu_addr_size
300 _op .${_cu_addr_size}byte $value
316 variable _cu_offset_size
318 if {![info exists _strings($value)]} {
319 set _strings($value) [new_label strp]
320 _defer_output .debug_string {
321 define_label $_strings($value)
322 _op .ascii [_quote $value]
326 _op .${_cu_offset_size}byte $_strings($value) "strp: $value"
330 set l1 [new_label "expr_start"]
331 set l2 [new_label "expr_end"]
332 _op .uleb128 "$l2 - $l1" "expression"
339 set len [string length $value]
341 error "DW_FORM_block1 length too long"
344 _op .ascii [_nz_quote $value]
357 DW_FORM_GNU_addr_index -
358 DW_FORM_GNU_str_index -
359 DW_FORM_GNU_ref_alt -
360 DW_FORM_GNU_strp_alt -
363 error "unhandled form $form"
368 proc _guess_form {value varname} {
369 upvar $varname new_value
371 switch -exact -- [string range $value 0 0] {
373 # Constant reference.
376 set new_value $_constants([string range $value 1 end])
385 set new_value "[string range $value 1 end] - $_cu_label"
391 return DW_FORM_string
396 # Map NAME to its canonical form.
397 proc _map_name {name ary} {
400 if {[info exists ${ary}($name)]} {
401 set name [set ${ary}($name)]
407 proc _handle_DW_TAG {tag_name {attrs {}} {children {}}} {
408 variable _abbrev_section
412 set has_children [expr {[string length $children] > 0}]
413 set my_abbrev [incr _abbrev_num]
415 # We somewhat wastefully emit a new abbrev entry for each tag.
416 # There's no reason for this other than laziness.
417 _defer_output $_abbrev_section {
418 _op .uleb128 $my_abbrev "Abbrev start"
419 _op .uleb128 $_constants($tag_name) $tag_name
420 _op .byte $has_children "has_children"
423 _op .uleb128 $my_abbrev "Abbrev ($tag_name)"
425 foreach attr $attrs {
426 set attr_name [_map_name [lindex $attr 0] _AT]
427 set attr_value [uplevel 2 [list subst [lindex $attr 1]]]
428 if {[llength $attr] > 2} {
429 set attr_form [lindex $attr 2]
431 set attr_form [_guess_form $attr_value attr_value]
433 set attr_form [_map_name $attr_form _FORM]
435 _handle_DW_FORM $attr_form $attr_value
437 _defer_output $_abbrev_section {
438 _op .uleb128 $_constants($attr_name) $attr_name
439 _op .uleb128 $_constants($attr_form) $attr_form
443 _defer_output $_abbrev_section {
445 _op .byte 0x0 Terminator
446 _op .byte 0x0 Terminator
452 # Terminate children.
453 _op .byte 0x0 "Terminate children"
457 proc _emit {string} {
458 variable _output_file
460 variable _deferred_output
463 puts $_output_file $string
465 append _deferred_output($_defer) ${string}\n
469 proc _section {name} {
470 _emit " .section $name"
473 proc _defer_output {section body} {
475 variable _deferred_output
477 set old_defer $_defer
480 if {![info exists _deferred_output($_defer)]} {
481 set _deferred_output($_defer) ""
487 set _defer $old_defer
490 proc _defer_to_string {body} {
492 variable _deferred_output
494 set old_defer $_defer
497 set _deferred_output($_defer) ""
501 set result $_deferred_output($_defer)
502 unset _deferred_output($_defer)
504 set _defer $old_defer
508 proc _write_deferred_output {} {
509 variable _output_file
510 variable _deferred_output
512 foreach section [array names _deferred_output] {
513 # The data already has a newline.
514 puts -nonewline $_output_file $_deferred_output($section)
518 unset _deferred_output
521 proc _op {name value {comment ""}} {
522 set text " ${name} ${value}"
523 if {$comment != ""} {
524 # Try to make stuff line up nicely.
525 while {[string length $text] < 40} {
528 append text "/* ${comment} */"
533 proc _compute_label {name} {
537 # Return a name suitable for use as a label. If BASE_NAME is
538 # specified, it is incorporated into the label name; this is to
539 # make debugging the generated assembler easier. If BASE_NAME is
540 # not specified a generic default is used. This proc does not
541 # define the label; see 'define_label'. 'new_label' attempts to
542 # ensure that label names are unique.
543 proc new_label {{base_name label}} {
546 return [_compute_label ${base_name}[incr _label_num]]
549 # Define a label named NAME. Ordinarily, NAME comes from a call
550 # to 'new_label', but this is not required.
551 proc define_label {name} {
555 # Declare a global label. This is typically used to refer to
556 # labels defined in other files, for example a function defined in
564 # A higher-level interface to label handling.
566 # ARGS is a list of label descriptors. Each one is either a
567 # single element, or a list of two elements -- a name and some
568 # text. For each descriptor, 'new_label' is invoked. If the list
569 # form is used, the second element in the list is passed as an
570 # argument. The label name is used to define a variable in the
571 # enclosing scope; this can be used to refer to the label later.
572 # The label name is also used to define a new proc whose name is
573 # the label name plus a trailing ":". This proc takes a body as
574 # an argument and can be used to define the label at that point;
575 # then the body, if any, is evaluated in the caller's context.
579 # declare_labels int_label
580 # something { ... $int_label } ;# refer to the label
581 # int_label: constant { ... } ;# define the label
582 proc declare_labels {args} {
584 set name [lindex $arg 0]
585 set text [lindex $arg 1]
587 upvar $name label_var
589 set label_var [new_label]
591 set label_var [new_label $text]
594 proc ${name}: {args} [format {
601 # This is a miniature assembler for location expressions. It is
602 # suitable for use in the attributes to a DIE. Its output is
603 # prefixed with "=" to make it automatically use DW_FORM_block.
604 # BODY is split by lines, and each line is taken to be a list.
605 # (FIXME should use 'info complete' here.)
606 # Each list's first element is the opcode, either short or long
607 # forms are accepted.
608 # FIXME argument handling
610 proc _location {body} {
613 variable _cu_addr_size
614 variable _cu_offset_size
616 foreach line [split $body \n] {
617 if {[lindex $line 0] == ""} {
620 set opcode [_map_name [lindex $line 0] _OP]
621 _op .byte $_constants($opcode) $opcode
623 switch -exact -- $opcode {
625 _op .${_cu_addr_size}byte [lindex $line 1]
630 _op .byte [lindex $line 1]
635 _op .2byte [lindex $line 1]
640 _op .4byte [lindex $line 1]
645 _op .8byte [lindex $line 1]
649 _op .uleb128 [lindex $line 1]
652 _op .sleb128 [lindex $line 1]
656 _op .uleb128 [lindex $line 1]
659 DW_OP_GNU_implicit_pointer {
660 if {[llength $line] != 3} {
661 error "usage: DW_OP_GNU_implicit_pointer LABEL OFFSET"
664 # Here label is a section offset.
665 set label [lindex $line 1]
666 _op .${_cu_offset_size}byte $label
667 _op .sleb128 [lindex $line 2]
671 if {[llength $line] != 2} {
672 error "usage: DW_OP_deref_size SIZE"
675 _op .byte [lindex $line 1]
679 if {[llength $line] > 1} {
680 error "Unimplemented: operands in location for $opcode"
688 # OPTIONS is a list with an even number of elements containing
689 # option-name and option-value pairs.
690 # Current options are:
691 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
692 # default = 0 (32-bit)
693 # version n - DWARF version number to emit
695 # addr_size n - the size of addresses, 32, 64, or default
697 # fission 0|1 - boolean indicating if generating Fission debug info
699 # BODY is Tcl code that emits the DIEs which make up the body of
700 # the CU. It is evaluated in the caller's context.
701 proc cu {options body} {
703 variable _abbrev_section
707 variable _cu_addr_size
708 variable _cu_offset_size
710 # Establish the defaults.
713 set _cu_addr_size default
715 set section ".debug_info"
716 set _abbrev_section ".debug_abbrev"
718 foreach { name value } $options {
719 switch -exact -- $name {
720 is_64 { set is_64 $value }
721 version { set _cu_version $value }
722 addr_size { set _cu_addr_size $value }
723 fission { set fission $value }
724 default { error "unknown option $name" }
727 if {$_cu_addr_size == "default"} {
728 if {[is_64_target]} {
734 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
736 set section ".debug_info.dwo"
737 set _abbrev_section ".debug_abbrev.dwo"
742 set cu_num [incr _cu_count]
743 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
746 set _cu_label [_compute_label "cu${cu_num}_begin"]
747 set start_label [_compute_label "cu${cu_num}_start"]
748 set end_label [_compute_label "cu${cu_num}_end"]
750 define_label $_cu_label
752 _op .4byte 0xffffffff
753 _op .8byte "$end_label - $start_label"
755 _op .4byte "$end_label - $start_label"
757 define_label $start_label
758 _op .2byte $_cu_version Version
759 _op .4byte $my_abbrevs Abbrevs
760 _op .byte $_cu_addr_size "Pointer size"
762 _defer_output $_abbrev_section {
763 define_label $my_abbrevs
768 _defer_output $_abbrev_section {
769 # Emit the terminator.
770 _op .byte 0x0 Terminator
771 _op .byte 0x0 Terminator
774 define_label $end_label
778 # OPTIONS is a list with an even number of elements containing
779 # option-name and option-value pairs.
780 # Current options are:
781 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
782 # default = 0 (32-bit)
783 # version n - DWARF version number to emit
785 # addr_size n - the size of addresses, 32, 64, or default
787 # fission 0|1 - boolean indicating if generating Fission debug info
789 # SIGNATURE is the 64-bit signature of the type.
790 # TYPE_LABEL is the label of the type defined by this TU,
791 # or "" if there is no type (i.e., type stubs in Fission).
792 # BODY is Tcl code that emits the DIEs which make up the body of
793 # the TU. It is evaluated in the caller's context.
794 proc tu {options signature type_label body} {
796 variable _abbrev_section
800 variable _cu_addr_size
801 variable _cu_offset_size
803 # Establish the defaults.
806 set _cu_addr_size default
808 set section ".debug_types"
809 set _abbrev_section ".debug_abbrev"
811 foreach { name value } $options {
812 switch -exact -- $name {
813 is_64 { set is_64 $value }
814 version { set _cu_version $value }
815 addr_size { set _cu_addr_size $value }
816 fission { set fission $value }
817 default { error "unknown option $name" }
820 if {$_cu_addr_size == "default"} {
821 if {[is_64_target]} {
827 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
829 set section ".debug_types.dwo"
830 set _abbrev_section ".debug_abbrev.dwo"
835 set cu_num [incr _cu_count]
836 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
839 set _cu_label [_compute_label "cu${cu_num}_begin"]
840 set start_label [_compute_label "cu${cu_num}_start"]
841 set end_label [_compute_label "cu${cu_num}_end"]
843 define_label $_cu_label
845 _op .4byte 0xffffffff
846 _op .8byte "$end_label - $start_label"
848 _op .4byte "$end_label - $start_label"
850 define_label $start_label
851 _op .2byte $_cu_version Version
852 _op .4byte $my_abbrevs Abbrevs
853 _op .byte $_cu_addr_size "Pointer size"
854 _op .8byte $signature Signature
855 if { $type_label != "" } {
856 uplevel declare_labels $type_label
857 upvar $type_label my_type_label
859 _op .8byte "$my_type_label - $_cu_label"
861 _op .4byte "$my_type_label - $_cu_label"
871 _defer_output $_abbrev_section {
872 define_label $my_abbrevs
877 _defer_output $_abbrev_section {
878 # Emit the terminator.
879 _op .byte 0x0 Terminator
880 _op .byte 0x0 Terminator
883 define_label $end_label
886 proc _empty_array {name} {
887 upvar $name the_array
889 catch {unset the_array}
894 # The top-level interface to the DWARF assembler.
895 # FILENAME is the name of the file where the generated assembly
897 # BODY is Tcl code to emit the assembly. It is evaluated via
898 # "eval" -- not uplevel as you might expect, because it is
899 # important to run the body in the Dwarf namespace.
901 # A typical invocation is something like:
902 # Dwarf::assemble $file {
912 proc assemble {filename body} {
913 variable _initialized
914 variable _output_file
915 variable _deferred_output
921 if {!$_initialized} {
926 set _output_file [open $filename w]
928 _empty_array _deferred_output
931 _empty_array _strings
933 # Not "uplevel" here, because we want to evaluate in this
934 # namespace. This is somewhat bad because it means we can't
935 # readily refer to outer variables.
938 _write_deferred_output
940 catch {close $_output_file}