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*]} {
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.
38 # Current restrictions:
39 # - only supports one source file
40 # - cannot be run on remote hosts
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."
48 error "Remote hosts are not supported."
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]
60 set result [gdb_compile $source_file $object_file object $options]
61 if { "$result" != "" } {
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"
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"
81 set result [gdb_compile $object_file $executable executable {nodebug}]
82 if { "$result" != "" } {
91 # All the variables in this namespace are private to the
92 # implementation. Also, any procedure whose name starts with "_" is
93 # private as well. Do not use these.
95 # Exported functions are documented at their definition.
97 # In addition to the hand-written functions documented below, this
98 # module automatically generates a function for each DWARF tag. For
99 # most tags, two forms are made: a full name, and one with the
100 # "DW_TAG_" prefix stripped. For example, you can use either
101 # 'DW_TAG_compile_unit' or 'compile_unit' interchangeably.
103 # There are two exceptions to this rule: DW_TAG_variable and
104 # DW_TAG_namespace. For these, the full name must always be used,
105 # as the short name conflicts with Tcl builtins. (Should future
106 # versions of Tcl or DWARF add more conflicts, this list will grow.
107 # If you want to be safe you should always use the full names.)
109 # Each tag procedure is defined like:
111 # proc DW_TAG_mumble {{attrs {}} {children {}}} { ... }
113 # ATTRS is an optional list of attributes.
114 # It is run through 'subst' in the caller's context before processing.
116 # Each attribute in the list has one of two forms:
118 # 2. { NAME VALUE FORM }
120 # In each case, NAME is the attribute's name.
121 # This can either be the full name, like 'DW_AT_name', or a shortened
122 # name, like 'name'. These are fully equivalent.
124 # If FORM is given, it should name a DW_FORM_ constant.
125 # This can either be the short form, like 'DW_FORM_addr', or a
126 # shortened version, like 'addr'. If the form is given, VALUE
127 # is its value; see below. In some cases, additional processing
128 # is done; for example, DW_FORM_strp manages the .debug_str
129 # section automatically.
131 # If FORM is 'SPECIAL_expr', then VALUE is treated as a location
132 # expression. The effective form is then DW_FORM_block, and VALUE
133 # is passed to the (internal) '_location' proc to be translated.
134 # This proc implements a miniature DW_OP_ assembler.
136 # If FORM is not given, it is guessed:
137 # * If VALUE starts with the "@" character, the rest of VALUE is
138 # looked up as a DWARF constant, and DW_FORM_sdata is used. For
139 # example, '@DW_LANG_c89' could be used.
140 # * If VALUE starts with the ":" character, then it is a label
141 # reference. The rest of VALUE is taken to be the name of a label,
142 # and DW_FORM_ref4 is used. See 'new_label' and 'define_label'.
143 # * Otherwise, VALUE is taken to be a string and DW_FORM_string is
145 # More form-guessing functionality may be added.
147 # CHILDREN is just Tcl code that can be used to define child DIEs. It
148 # is evaluated in the caller's context.
150 # Currently this code is missing nice support for CFA handling, and
151 # probably other things as well.
153 namespace eval Dwarf {
154 # True if the module has been initialized.
155 variable _initialized 0
157 # Constants from dwarf2.h.
161 # DW_FORM short names.
166 # The current output file.
167 variable _output_file
169 # Note: The _cu_ values here also apply to type units (TUs).
170 # Think of a TU as a special kind of CU.
175 # The current CU's base label.
178 # The current CU's version.
181 # The current CU's address size.
182 variable _cu_addr_size
183 # The current CU's offset size.
184 variable _cu_offset_size
186 # Label generation number.
189 # The deferred output array. The index is the section name; the
190 # contents hold the data for that section.
191 variable _deferred_output
193 # If empty, we should write directly to the output file.
194 # Otherwise, this is the name of a section to write to.
197 # The abbrev section. Typically .debug_abbrev but can be .debug_abbrev.dwo
199 variable _abbrev_section
201 # The next available abbrev number in the current CU's abbrev
205 # The string table for this assembly. The key is the string; the
206 # value is the label for that string.
209 proc _process_one_constant {name value} {
215 set _constants($name) $value
217 if {![regexp "^DW_(\[A-Z\]+)_(\[A-Za-z0-9_\]+)$" $name \
218 ignore prefix name2]} {
219 error "non-matching name: $name"
222 if {$name2 == "lo_user" || $name2 == "hi_user"} {
226 # We only try to shorten some very common things.
228 switch -exact -- $prefix {
230 # Create two procedures for the tag. These call
231 # _handle_DW_TAG with the full tag name baked in; this
232 # does all the actual work.
233 proc $name {{attrs {}} {children {}}} \
234 "_handle_DW_TAG $name \$attrs \$children"
236 # Filter out ones that are known to clash.
237 if {$name2 == "variable" || $name2 == "namespace"} {
238 set name2 "tag_$name2"
241 if {[info commands $name2] != {}} {
242 error "duplicate proc name: from $name"
245 proc $name2 {{attrs {}} {children {}}} \
246 "_handle_DW_TAG $name \$attrs \$children"
250 set _AT($name2) $name
254 set _FORM($name2) $name
258 set _OP($name2) $name
267 proc _read_constants {} {
268 global srcdir hex decimal
271 # DWARF name-matching regexp.
272 set dwrx "DW_\[a-zA-Z0-9_\]+"
276 set fd [open [file join $srcdir .. .. include dwarf2.h]]
279 if {[regexp -- "^${ws}($dwrx)${ws}=${ws}($hex|$decimal),?$" \
280 $line ignore name value ignore2]} {
281 _process_one_constant $name $value
286 set fd [open [file join $srcdir .. .. include dwarf2.def]]
290 "^DW_\[A-Z_\]+${ws}\\(($dwrx),${ws}($hex|$decimal)\\)$" \
291 $line ignore name value ignore2]} {
292 _process_one_constant $name $value
297 set _constants(SPECIAL_expr) $_constants(DW_FORM_block)
300 proc _quote {string} {
302 return "\"${string}\\0\""
305 proc _nz_quote {string} {
306 # For now, no quoting is done.
307 return "\"${string}\""
310 proc _handle_DW_FORM {form value} {
311 switch -exact -- $form {
313 _op .ascii [_quote $value]
316 DW_FORM_flag_present {
317 # We don't need to emit anything.
326 variable _cu_offset_size
328 variable _cu_addr_size
330 if {$_cu_version == 2} {
331 set size $_cu_addr_size
333 set size $_cu_offset_size
336 _op .${size}byte $value
355 variable _cu_addr_size
357 _op .${_cu_addr_size}byte $value
373 variable _cu_offset_size
375 if {![info exists _strings($value)]} {
376 set _strings($value) [new_label strp]
377 _defer_output .debug_string {
378 define_label $_strings($value)
379 _op .ascii [_quote $value]
383 _op .${_cu_offset_size}byte $_strings($value) "strp: $value"
387 set l1 [new_label "expr_start"]
388 set l2 [new_label "expr_end"]
389 _op .uleb128 "$l2 - $l1" "expression"
396 set len [string length $value]
398 error "DW_FORM_block1 length too long"
401 _op .ascii [_nz_quote $value]
414 DW_FORM_GNU_addr_index -
415 DW_FORM_GNU_str_index -
416 DW_FORM_GNU_ref_alt -
417 DW_FORM_GNU_strp_alt -
420 error "unhandled form $form"
425 proc _guess_form {value varname} {
426 upvar $varname new_value
428 switch -exact -- [string range $value 0 0] {
430 # Constant reference.
433 set new_value $_constants([string range $value 1 end])
442 set new_value "[string range $value 1 end] - $_cu_label"
448 return DW_FORM_string
453 # Map NAME to its canonical form.
454 proc _map_name {name ary} {
457 if {[info exists ${ary}($name)]} {
458 set name [set ${ary}($name)]
464 proc _handle_DW_TAG {tag_name {attrs {}} {children {}}} {
465 variable _abbrev_section
469 set has_children [expr {[string length $children] > 0}]
470 set my_abbrev [incr _abbrev_num]
472 # We somewhat wastefully emit a new abbrev entry for each tag.
473 # There's no reason for this other than laziness.
474 _defer_output $_abbrev_section {
475 _op .uleb128 $my_abbrev "Abbrev start"
476 _op .uleb128 $_constants($tag_name) $tag_name
477 _op .byte $has_children "has_children"
480 _op .uleb128 $my_abbrev "Abbrev ($tag_name)"
482 foreach attr $attrs {
483 set attr_name [_map_name [lindex $attr 0] _AT]
484 set attr_value [uplevel 2 [list subst [lindex $attr 1]]]
485 if {[llength $attr] > 2} {
486 set attr_form [lindex $attr 2]
488 set attr_form [_guess_form $attr_value attr_value]
490 set attr_form [_map_name $attr_form _FORM]
492 _handle_DW_FORM $attr_form $attr_value
494 _defer_output $_abbrev_section {
495 _op .uleb128 $_constants($attr_name) $attr_name
496 _op .uleb128 $_constants($attr_form) $attr_form
500 _defer_output $_abbrev_section {
502 _op .byte 0x0 Terminator
503 _op .byte 0x0 Terminator
509 # Terminate children.
510 _op .byte 0x0 "Terminate children"
514 proc _emit {string} {
515 variable _output_file
517 variable _deferred_output
520 puts $_output_file $string
522 append _deferred_output($_defer) ${string}\n
526 proc _section {name {flags ""} {type ""}} {
527 if {$flags == "" && $type == ""} {
528 _emit " .section $name"
529 } elseif {$type == ""} {
530 _emit " .section $name, \"$flags\""
532 _emit " .section $name, \"$flags\", %$type"
536 # SECTION_SPEC is a list of arguments to _section.
537 proc _defer_output {section_spec body} {
539 variable _deferred_output
541 set old_defer $_defer
542 set _defer [lindex $section_spec 0]
544 if {![info exists _deferred_output($_defer)]} {
545 set _deferred_output($_defer) ""
546 eval _section $section_spec
551 set _defer $old_defer
554 proc _defer_to_string {body} {
556 variable _deferred_output
558 set old_defer $_defer
561 set _deferred_output($_defer) ""
565 set result $_deferred_output($_defer)
566 unset _deferred_output($_defer)
568 set _defer $old_defer
572 proc _write_deferred_output {} {
573 variable _output_file
574 variable _deferred_output
576 foreach section [array names _deferred_output] {
577 # The data already has a newline.
578 puts -nonewline $_output_file $_deferred_output($section)
582 unset _deferred_output
585 proc _op {name value {comment ""}} {
586 set text " ${name} ${value}"
587 if {$comment != ""} {
588 # Try to make stuff line up nicely.
589 while {[string length $text] < 40} {
592 append text "/* ${comment} */"
597 proc _compute_label {name} {
601 # Return a name suitable for use as a label. If BASE_NAME is
602 # specified, it is incorporated into the label name; this is to
603 # make debugging the generated assembler easier. If BASE_NAME is
604 # not specified a generic default is used. This proc does not
605 # define the label; see 'define_label'. 'new_label' attempts to
606 # ensure that label names are unique.
607 proc new_label {{base_name label}} {
610 return [_compute_label ${base_name}[incr _label_num]]
613 # Define a label named NAME. Ordinarily, NAME comes from a call
614 # to 'new_label', but this is not required.
615 proc define_label {name} {
619 # Declare a global label. This is typically used to refer to
620 # labels defined in other files, for example a function defined in
628 # A higher-level interface to label handling.
630 # ARGS is a list of label descriptors. Each one is either a
631 # single element, or a list of two elements -- a name and some
632 # text. For each descriptor, 'new_label' is invoked. If the list
633 # form is used, the second element in the list is passed as an
634 # argument. The label name is used to define a variable in the
635 # enclosing scope; this can be used to refer to the label later.
636 # The label name is also used to define a new proc whose name is
637 # the label name plus a trailing ":". This proc takes a body as
638 # an argument and can be used to define the label at that point;
639 # then the body, if any, is evaluated in the caller's context.
643 # declare_labels int_label
644 # something { ... $int_label } ;# refer to the label
645 # int_label: constant { ... } ;# define the label
646 proc declare_labels {args} {
648 set name [lindex $arg 0]
649 set text [lindex $arg 1]
651 upvar $name label_var
653 set label_var [new_label]
655 set label_var [new_label $text]
658 proc ${name}: {args} [format {
665 # This is a miniature assembler for location expressions. It is
666 # suitable for use in the attributes to a DIE. Its output is
667 # prefixed with "=" to make it automatically use DW_FORM_block.
668 # BODY is split by lines, and each line is taken to be a list.
669 # (FIXME should use 'info complete' here.)
670 # Each list's first element is the opcode, either short or long
671 # forms are accepted.
672 # FIXME argument handling
674 proc _location {body} {
677 variable _cu_addr_size
678 variable _cu_offset_size
680 foreach line [split $body \n] {
681 if {[lindex $line 0] == ""} {
684 set opcode [_map_name [lindex $line 0] _OP]
685 _op .byte $_constants($opcode) $opcode
687 switch -exact -- $opcode {
689 _op .${_cu_addr_size}byte [lindex $line 1]
694 _op .byte [lindex $line 1]
699 _op .2byte [lindex $line 1]
704 _op .4byte [lindex $line 1]
709 _op .8byte [lindex $line 1]
713 _op .uleb128 [lindex $line 1]
716 _op .sleb128 [lindex $line 1]
720 _op .uleb128 [lindex $line 1]
723 DW_OP_GNU_implicit_pointer {
724 if {[llength $line] != 3} {
725 error "usage: DW_OP_GNU_implicit_pointer LABEL OFFSET"
728 # Here label is a section offset.
729 set label [lindex $line 1]
730 _op .${_cu_offset_size}byte $label
731 _op .sleb128 [lindex $line 2]
735 if {[llength $line] != 2} {
736 error "usage: DW_OP_deref_size SIZE"
739 _op .byte [lindex $line 1]
743 if {[llength $line] > 1} {
744 error "Unimplemented: operands in location for $opcode"
752 # OPTIONS is a list with an even number of elements containing
753 # option-name and option-value pairs.
754 # Current options are:
755 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
756 # default = 0 (32-bit)
757 # version n - DWARF version number to emit
759 # addr_size n - the size of addresses, 32, 64, or default
761 # fission 0|1 - boolean indicating if generating Fission debug info
763 # BODY is Tcl code that emits the DIEs which make up the body of
764 # the CU. It is evaluated in the caller's context.
765 proc cu {options body} {
767 variable _abbrev_section
771 variable _cu_addr_size
772 variable _cu_offset_size
774 # Establish the defaults.
777 set _cu_addr_size default
779 set section ".debug_info"
780 set _abbrev_section ".debug_abbrev"
782 foreach { name value } $options {
783 switch -exact -- $name {
784 is_64 { set is_64 $value }
785 version { set _cu_version $value }
786 addr_size { set _cu_addr_size $value }
787 fission { set fission $value }
788 default { error "unknown option $name" }
791 if {$_cu_addr_size == "default"} {
792 if {[is_64_target]} {
798 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
800 set section ".debug_info.dwo"
801 set _abbrev_section ".debug_abbrev.dwo"
806 set cu_num [incr _cu_count]
807 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
810 set _cu_label [_compute_label "cu${cu_num}_begin"]
811 set start_label [_compute_label "cu${cu_num}_start"]
812 set end_label [_compute_label "cu${cu_num}_end"]
814 define_label $_cu_label
816 _op .4byte 0xffffffff
817 _op .8byte "$end_label - $start_label"
819 _op .4byte "$end_label - $start_label"
821 define_label $start_label
822 _op .2byte $_cu_version Version
823 _op .4byte $my_abbrevs Abbrevs
824 _op .byte $_cu_addr_size "Pointer size"
826 _defer_output $_abbrev_section {
827 define_label $my_abbrevs
832 _defer_output $_abbrev_section {
833 # Emit the terminator.
834 _op .byte 0x0 Terminator
835 _op .byte 0x0 Terminator
838 define_label $end_label
842 # OPTIONS is a list with an even number of elements containing
843 # option-name and option-value pairs.
844 # Current options are:
845 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
846 # default = 0 (32-bit)
847 # version n - DWARF version number to emit
849 # addr_size n - the size of addresses, 32, 64, or default
851 # fission 0|1 - boolean indicating if generating Fission debug info
853 # SIGNATURE is the 64-bit signature of the type.
854 # TYPE_LABEL is the label of the type defined by this TU,
855 # or "" if there is no type (i.e., type stubs in Fission).
856 # BODY is Tcl code that emits the DIEs which make up the body of
857 # the TU. It is evaluated in the caller's context.
858 proc tu {options signature type_label body} {
860 variable _abbrev_section
864 variable _cu_addr_size
865 variable _cu_offset_size
867 # Establish the defaults.
870 set _cu_addr_size default
872 set section ".debug_types"
873 set _abbrev_section ".debug_abbrev"
875 foreach { name value } $options {
876 switch -exact -- $name {
877 is_64 { set is_64 $value }
878 version { set _cu_version $value }
879 addr_size { set _cu_addr_size $value }
880 fission { set fission $value }
881 default { error "unknown option $name" }
884 if {$_cu_addr_size == "default"} {
885 if {[is_64_target]} {
891 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
893 set section ".debug_types.dwo"
894 set _abbrev_section ".debug_abbrev.dwo"
899 set cu_num [incr _cu_count]
900 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
903 set _cu_label [_compute_label "cu${cu_num}_begin"]
904 set start_label [_compute_label "cu${cu_num}_start"]
905 set end_label [_compute_label "cu${cu_num}_end"]
907 define_label $_cu_label
909 _op .4byte 0xffffffff
910 _op .8byte "$end_label - $start_label"
912 _op .4byte "$end_label - $start_label"
914 define_label $start_label
915 _op .2byte $_cu_version Version
916 _op .4byte $my_abbrevs Abbrevs
917 _op .byte $_cu_addr_size "Pointer size"
918 _op .8byte $signature Signature
919 if { $type_label != "" } {
920 uplevel declare_labels $type_label
921 upvar $type_label my_type_label
923 _op .8byte "$my_type_label - $_cu_label"
925 _op .4byte "$my_type_label - $_cu_label"
935 _defer_output $_abbrev_section {
936 define_label $my_abbrevs
941 _defer_output $_abbrev_section {
942 # Emit the terminator.
943 _op .byte 0x0 Terminator
944 _op .byte 0x0 Terminator
947 define_label $end_label
950 proc _empty_array {name} {
951 upvar $name the_array
953 catch {unset the_array}
958 # Emit a .gnu_debugaltlink section with the given file name and
959 # build-id. The buildid should be represented as a hexadecimal
960 # string, like "ffeeddcc".
961 proc gnu_debugaltlink {filename buildid} {
962 _defer_output .gnu_debugaltlink {
963 _op .ascii [_quote $filename]
964 foreach {a b} [split $buildid {}] {
970 proc _note {type name hexdata} {
971 set namelen [expr [string length $name] + 1]
976 _op .4byte [expr [string length $hexdata] / 2]
980 _op .ascii [_quote $name]
983 set total [expr {($namelen + (1 << $align) - 1) & (-1 << $align)}]
984 for {set i $namelen} {$i < $total} {incr i} {
988 foreach {a b} [split $hexdata {}] {
993 # Emit a note section holding the given build-id.
994 proc build_id {buildid} {
995 _defer_output {.note.gnu.build-id a note} {
997 set NT_GNU_BUILD_ID 3
999 _note $NT_GNU_BUILD_ID GNU $buildid
1003 # The top-level interface to the DWARF assembler.
1004 # FILENAME is the name of the file where the generated assembly
1006 # BODY is Tcl code to emit the assembly. It is evaluated via
1007 # "eval" -- not uplevel as you might expect, because it is
1008 # important to run the body in the Dwarf namespace.
1010 # A typical invocation is something like:
1011 # Dwarf::assemble $file {
1021 proc assemble {filename body} {
1022 variable _initialized
1023 variable _output_file
1024 variable _deferred_output
1030 if {!$_initialized} {
1035 set _output_file [open $filename w]
1037 _empty_array _deferred_output
1040 _empty_array _strings
1042 # Not "uplevel" here, because we want to evaluate in this
1043 # namespace. This is somewhat bad because it means we can't
1044 # readily refer to outer variables.
1047 _write_deferred_output
1049 catch {close $_output_file}