1 # Copyright 2010-2020 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 $options]
82 if { "$result" != "" } {
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:
96 # asm ("main_label: .globl main_label");
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:
104 # asm ("func_start: .globl func_start");
105 # static void func (void) {}
106 # asm ("func_end: .globl func_end");
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
112 # asm ("func_start: .globl func_start");
113 # asm ("func_end: .globl func_end");
114 # static void func (void) {}
117 proc function_range { func src } {
118 global decimal gdb_prompt
120 set exe [standard_temp_file func_addr[pid].x]
122 gdb_compile $src $exe executable {debug}
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)
138 # Compute the function 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)
148 # Compute the size of the last instruction.
149 if { $func_length == 0 } then {
150 set func_pattern "$func"
152 set func_pattern "$func\\+$func_length"
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)
160 set func_length [expr $func_length + $end - $start]
164 return [list "${func}_label - $func_label_offset" $func_length]
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.
173 # Exported functions are documented at their definition.
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.
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.)
187 # Each tag procedure is defined like:
189 # proc DW_TAG_mumble {{attrs {}} {children {}}} { ... }
191 # ATTRS is an optional list of attributes.
192 # It is run through 'subst' in the caller's context before processing.
194 # Each attribute in the list has one of two forms:
196 # 2. { NAME VALUE FORM }
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.
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:
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.
210 # - MACRO_AT_func { FUNC FILE }
211 # It is substituted by DW_AT_name with FUNC and MACRO_AT_range.
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.
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.
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 # * If VALUE starts with the "%" character, then it is a label
233 # reference too, but DW_FORM_ref_addr is used.
234 # * Otherwise, VALUE is taken to be a string and DW_FORM_string is
235 # used. In order to prevent bugs where a numeric value is given but
236 # no form is specified, it is an error if the value looks like a number
237 # (using Tcl's "string is integer") and no form is provided.
238 # More form-guessing functionality may be added.
240 # CHILDREN is just Tcl code that can be used to define child DIEs. It
241 # is evaluated in the caller's context.
243 # Currently this code is missing nice support for CFA handling, and
244 # probably other things as well.
246 namespace eval Dwarf {
247 # True if the module has been initialized.
248 variable _initialized 0
250 # Constants from dwarf2.h.
254 # DW_FORM short names.
259 # The current output file.
260 variable _output_file
262 # Note: The _cu_ values here also apply to type units (TUs).
263 # Think of a TU as a special kind of CU.
268 # The current CU's base label.
271 # The current CU's version.
274 # The current CU's address size.
275 variable _cu_addr_size
276 # The current CU's offset size.
277 variable _cu_offset_size
279 # Label generation number.
282 # The deferred output array. The index is the section name; the
283 # contents hold the data for that section.
284 variable _deferred_output
286 # If empty, we should write directly to the output file.
287 # Otherwise, this is the name of a section to write to.
290 # The abbrev section. Typically .debug_abbrev but can be .debug_abbrev.dwo
292 variable _abbrev_section
294 # The next available abbrev number in the current CU's abbrev
298 # The string table for this assembly. The key is the string; the
299 # value is the label for that string.
302 # Current .debug_line unit count.
305 # Whether a file_name entry was seen.
306 variable _line_saw_file
308 # Whether a line table program has been seen.
309 variable _line_saw_program
311 # A Label for line table header generation.
312 variable _line_header_end_label
314 # The address size for debug ranges section.
315 variable _debug_ranges_64_bit
317 proc _process_one_constant {name value} {
323 set _constants($name) $value
325 if {![regexp "^DW_(\[A-Z\]+)_(\[A-Za-z0-9_\]+)$" $name \
326 ignore prefix name2]} {
327 error "non-matching name: $name"
330 if {$name2 == "lo_user" || $name2 == "hi_user"} {
334 # We only try to shorten some very common things.
336 switch -exact -- $prefix {
338 # Create two procedures for the tag. These call
339 # _handle_DW_TAG with the full tag name baked in; this
340 # does all the actual work.
341 proc $name {{attrs {}} {children {}}} \
342 "_handle_DW_TAG $name \$attrs \$children"
344 # Filter out ones that are known to clash.
345 if {$name2 == "variable" || $name2 == "namespace"} {
346 set name2 "tag_$name2"
349 if {[info commands $name2] != {}} {
350 error "duplicate proc name: from $name"
353 proc $name2 {{attrs {}} {children {}}} \
354 "_handle_DW_TAG $name \$attrs \$children"
358 set _AT($name2) $name
362 set _FORM($name2) $name
366 set _OP($name2) $name
375 proc _read_constants {} {
376 global srcdir hex decimal
379 # DWARF name-matching regexp.
380 set dwrx "DW_\[a-zA-Z0-9_\]+"
384 set fd [open [file join $srcdir .. .. include dwarf2.h]]
387 if {[regexp -- "^${ws}($dwrx)${ws}=${ws}($hex|$decimal),?$" \
388 $line ignore name value ignore2]} {
389 _process_one_constant $name $value
394 set fd [open [file join $srcdir .. .. include dwarf2.def]]
398 "^DW_\[A-Z_\]+${ws}\\(($dwrx),${ws}($hex|$decimal)\\)$" \
399 $line ignore name value ignore2]} {
400 _process_one_constant $name $value
405 set _constants(SPECIAL_expr) $_constants(DW_FORM_block)
408 proc _quote {string} {
410 return "\"${string}\\0\""
413 proc _nz_quote {string} {
414 # For now, no quoting is done.
415 return "\"${string}\""
418 proc _handle_DW_FORM {form value} {
419 switch -exact -- $form {
421 _op .ascii [_quote $value]
424 DW_FORM_flag_present {
425 # We don't need to emit anything.
434 variable _cu_offset_size
436 variable _cu_addr_size
438 if {$_cu_version == 2} {
439 set size $_cu_addr_size
441 set size $_cu_offset_size
444 _op .${size}byte $value
448 variable _cu_offset_size
449 _op .${_cu_offset_size}byte $value
468 variable _cu_addr_size
470 _op .${_cu_addr_size}byte $value
490 variable _cu_offset_size
492 if {![info exists _strings($value)]} {
493 set _strings($value) [new_label strp]
494 _defer_output .debug_string {
495 define_label $_strings($value)
496 _op .ascii [_quote $value]
500 _op .${_cu_offset_size}byte $_strings($value) "strp: $value"
504 set l1 [new_label "expr_start"]
505 set l2 [new_label "expr_end"]
506 _op .uleb128 "$l2 - $l1" "expression"
513 set len [string length $value]
515 error "DW_FORM_block1 length too long"
518 _op .ascii [_nz_quote $value]
536 DW_FORM_GNU_addr_index -
537 DW_FORM_GNU_str_index -
538 DW_FORM_GNU_ref_alt -
539 DW_FORM_GNU_strp_alt -
542 error "unhandled form $form"
547 proc _guess_form {value varname} {
548 upvar $varname new_value
550 switch -exact -- [string range $value 0 0] {
552 # Constant reference.
555 set new_value $_constants([string range $value 1 end])
564 set new_value "[string range $value 1 end] - $_cu_label"
570 # Label reference, an offset from .debug_info.
571 set new_value "[string range $value 1 end]"
573 return DW_FORM_ref_addr
577 return DW_FORM_string
582 # Map NAME to its canonical form.
583 proc _map_name {name ary} {
586 if {[info exists ${ary}($name)]} {
587 set name [set ${ary}($name)]
593 proc _handle_attribute { attr_name attr_value attr_form } {
594 variable _abbrev_section
597 _handle_DW_FORM $attr_form $attr_value
599 _defer_output $_abbrev_section {
600 _op .uleb128 $_constants($attr_name) $attr_name
601 _op .uleb128 $_constants($attr_form) $attr_form
605 # Handle macro attribute MACRO_AT_range.
607 proc _handle_macro_at_range { attr_value } {
608 if {[llength $attr_value] != 2} {
609 error "usage: MACRO_AT_range { func file }"
612 set func [lindex $attr_value 0]
613 set src [lindex $attr_value 1]
614 set result [function_range $func $src]
616 _handle_attribute DW_AT_low_pc [lindex $result 0] \
618 _handle_attribute DW_AT_high_pc \
619 "[lindex $result 0] + [lindex $result 1]" DW_FORM_addr
622 # Handle macro attribute MACRO_AT_func.
624 proc _handle_macro_at_func { attr_value } {
625 if {[llength $attr_value] != 2} {
626 error "usage: MACRO_AT_func { func file }"
628 _handle_attribute DW_AT_name [lindex $attr_value 0] DW_FORM_string
629 _handle_macro_at_range $attr_value
632 proc _handle_DW_TAG {tag_name {attrs {}} {children {}}} {
633 variable _abbrev_section
637 set has_children [expr {[string length $children] > 0}]
638 set my_abbrev [incr _abbrev_num]
640 # We somewhat wastefully emit a new abbrev entry for each tag.
641 # There's no reason for this other than laziness.
642 _defer_output $_abbrev_section {
643 _op .uleb128 $my_abbrev "Abbrev start"
644 _op .uleb128 $_constants($tag_name) $tag_name
645 _op .byte $has_children "has_children"
648 _op .uleb128 $my_abbrev "Abbrev ($tag_name)"
650 foreach attr $attrs {
651 set attr_name [_map_name [lindex $attr 0] _AT]
653 # When the length of ATTR is greater than 2, the last
654 # element of the list must be a form. The second through
655 # the penultimate elements are joined together and
656 # evaluated using subst. This allows constructs such as
657 # [gdb_target_symbol foo] to be used.
659 if {[llength $attr] > 2} {
660 set attr_value [uplevel 2 [list subst [join [lrange $attr 1 end-1]]]]
662 set attr_value [uplevel 2 [list subst [lindex $attr 1]]]
665 if { [string equal "MACRO_AT_func" $attr_name] } {
666 _handle_macro_at_func $attr_value
667 } elseif { [string equal "MACRO_AT_range" $attr_name] } {
668 _handle_macro_at_range $attr_value
670 if {[llength $attr] > 2} {
671 set attr_form [uplevel 2 [list subst [lindex $attr end]]]
673 if { [string index $attr_value 0] == ":" } {
674 # It is a label, get its value.
675 _guess_form $attr_value attr_value
678 # If the value looks like an integer, a form is required.
679 if [string is integer $attr_value] {
680 error "Integer value requires a form"
682 set attr_form [_guess_form $attr_value attr_value]
684 set attr_form [_map_name $attr_form _FORM]
686 _handle_attribute $attr_name $attr_value $attr_form
690 _defer_output $_abbrev_section {
692 _op .byte 0x0 Terminator
693 _op .byte 0x0 Terminator
699 # Terminate children.
700 _op .byte 0x0 "Terminate children"
704 proc _emit {string} {
705 variable _output_file
707 variable _deferred_output
710 puts $_output_file $string
712 append _deferred_output($_defer) ${string}\n
716 proc _section {name {flags ""} {type ""}} {
717 if {$flags == "" && $type == ""} {
718 _emit " .section $name"
719 } elseif {$type == ""} {
720 _emit " .section $name, \"$flags\""
722 _emit " .section $name, \"$flags\", %$type"
726 # SECTION_SPEC is a list of arguments to _section.
727 proc _defer_output {section_spec body} {
729 variable _deferred_output
731 set old_defer $_defer
732 set _defer [lindex $section_spec 0]
734 if {![info exists _deferred_output($_defer)]} {
735 set _deferred_output($_defer) ""
736 eval _section $section_spec
741 set _defer $old_defer
744 proc _defer_to_string {body} {
746 variable _deferred_output
748 set old_defer $_defer
751 set _deferred_output($_defer) ""
755 set result $_deferred_output($_defer)
756 unset _deferred_output($_defer)
758 set _defer $old_defer
762 proc _write_deferred_output {} {
763 variable _output_file
764 variable _deferred_output
766 foreach section [array names _deferred_output] {
767 # The data already has a newline.
768 puts -nonewline $_output_file $_deferred_output($section)
772 unset _deferred_output
775 proc _op {name value {comment ""}} {
776 set text " ${name} ${value}"
777 if {$comment != ""} {
778 # Try to make stuff line up nicely.
779 while {[string length $text] < 40} {
782 append text "/* ${comment} */"
787 proc _compute_label {name} {
791 # Return a name suitable for use as a label. If BASE_NAME is
792 # specified, it is incorporated into the label name; this is to
793 # make debugging the generated assembler easier. If BASE_NAME is
794 # not specified a generic default is used. This proc does not
795 # define the label; see 'define_label'. 'new_label' attempts to
796 # ensure that label names are unique.
797 proc new_label {{base_name label}} {
800 return [_compute_label ${base_name}[incr _label_num]]
803 # Define a label named NAME. Ordinarily, NAME comes from a call
804 # to 'new_label', but this is not required.
805 proc define_label {name} {
809 # Declare a global label. This is typically used to refer to
810 # labels defined in other files, for example a function defined in
818 # A higher-level interface to label handling.
820 # ARGS is a list of label descriptors. Each one is either a
821 # single element, or a list of two elements -- a name and some
822 # text. For each descriptor, 'new_label' is invoked. If the list
823 # form is used, the second element in the list is passed as an
824 # argument. The label name is used to define a variable in the
825 # enclosing scope; this can be used to refer to the label later.
826 # The label name is also used to define a new proc whose name is
827 # the label name plus a trailing ":". This proc takes a body as
828 # an argument and can be used to define the label at that point;
829 # then the body, if any, is evaluated in the caller's context.
833 # declare_labels int_label
834 # something { ... $int_label } ;# refer to the label
835 # int_label: constant { ... } ;# define the label
836 proc declare_labels {args} {
838 set name [lindex $arg 0]
839 set text [lindex $arg 1]
841 upvar $name label_var
843 set label_var [new_label]
845 set label_var [new_label $text]
848 proc ${name}: {args} [format {
855 # This is a miniature assembler for location expressions. It is
856 # suitable for use in the attributes to a DIE. Its output is
857 # prefixed with "=" to make it automatically use DW_FORM_block.
858 # BODY is split by lines, and each line is taken to be a list.
859 # (FIXME should use 'info complete' here.)
860 # Each list's first element is the opcode, either short or long
861 # forms are accepted.
862 # FIXME argument handling
864 proc _location {body} {
868 variable _cu_addr_size
869 variable _cu_offset_size
871 foreach line [split $body \n] {
872 # Ignore blank lines, and allow embedded comments.
873 if {[lindex $line 0] == "" || [regexp -- {^[ \t]*#} $line]} {
876 set opcode [_map_name [lindex $line 0] _OP]
877 _op .byte $_constants($opcode) $opcode
879 switch -exact -- $opcode {
881 _op .${_cu_addr_size}byte [lindex $line 1]
885 _op .uleb128 [lindex $line 1]
891 _op .byte [lindex $line 1]
896 _op .2byte [lindex $line 1]
901 _op .4byte [lindex $line 1]
906 _op .8byte [lindex $line 1]
910 _op .uleb128 [lindex $line 1]
913 _op .sleb128 [lindex $line 1]
917 _op .uleb128 [lindex $line 1]
921 _op .uleb128 [lindex $line 1]
925 _op .uleb128 [lindex $line 1]
926 _op .uleb128 [lindex $line 2]
931 _op .2byte [lindex $line 1]
934 DW_OP_implicit_value {
935 set l1 [new_label "value_start"]
936 set l2 [new_label "value_end"]
937 _op .uleb128 "$l2 - $l1"
939 foreach value [lrange $line 1 end] {
940 switch -regexp -- $value {
941 {^0x[[:xdigit:]]{1,2}$} {_op .byte $value}
942 {^0x[[:xdigit:]]{4}$} {_op .2byte $value}
943 {^0x[[:xdigit:]]{8}$} {_op .4byte $value}
944 {^0x[[:xdigit:]]{16}$} {_op .8byte $value}
946 error "bad value '$value' in DW_OP_implicit_value"
953 DW_OP_implicit_pointer -
954 DW_OP_GNU_implicit_pointer {
955 if {[llength $line] != 3} {
956 error "usage: $opcode LABEL OFFSET"
959 # Here label is a section offset.
960 set label [lindex $line 1]
961 if { $_cu_version == 2 } {
962 _op .${_cu_addr_size}byte $label
964 _op .${_cu_offset_size}byte $label
966 _op .sleb128 [lindex $line 2]
969 DW_OP_GNU_variable_value {
970 if {[llength $line] != 2} {
971 error "usage: $opcode LABEL"
974 # Here label is a section offset.
975 set label [lindex $line 1]
976 if { $_cu_version == 2 } {
977 _op .${_cu_addr_size}byte $label
979 _op .${_cu_offset_size}byte $label
984 if {[llength $line] != 2} {
985 error "usage: DW_OP_deref_size SIZE"
988 _op .byte [lindex $line 1]
992 _op .uleb128 [lindex $line 1]
993 _op .sleb128 [lindex $line 2]
997 if {[llength $line] > 1} {
998 error "Unimplemented: operands in location for $opcode"
1006 # OPTIONS is a list with an even number of elements containing
1007 # option-name and option-value pairs.
1008 # Current options are:
1009 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1010 # default = 0 (32-bit)
1011 # version n - DWARF version number to emit
1013 # addr_size n - the size of addresses, 32, 64, or default
1015 # fission 0|1 - boolean indicating if generating Fission debug info
1017 # BODY is Tcl code that emits the DIEs which make up the body of
1018 # the CU. It is evaluated in the caller's context.
1019 proc cu {options body} {
1021 variable _abbrev_section
1022 variable _abbrev_num
1024 variable _cu_version
1025 variable _cu_addr_size
1026 variable _cu_offset_size
1028 # Establish the defaults.
1031 set _cu_addr_size default
1033 set section ".debug_info"
1034 set _abbrev_section ".debug_abbrev"
1036 foreach { name value } $options {
1037 set value [uplevel 1 "subst \"$value\""]
1038 switch -exact -- $name {
1039 is_64 { set is_64 $value }
1040 version { set _cu_version $value }
1041 addr_size { set _cu_addr_size $value }
1042 fission { set fission $value }
1043 default { error "unknown option $name" }
1046 if {$_cu_addr_size == "default"} {
1047 if {[is_64_target]} {
1053 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
1055 set section ".debug_info.dwo"
1056 set _abbrev_section ".debug_abbrev.dwo"
1061 set cu_num [incr _cu_count]
1062 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
1065 set _cu_label [_compute_label "cu${cu_num}_begin"]
1066 set start_label [_compute_label "cu${cu_num}_start"]
1067 set end_label [_compute_label "cu${cu_num}_end"]
1069 define_label $_cu_label
1071 _op .4byte 0xffffffff
1072 _op .8byte "$end_label - $start_label"
1074 _op .4byte "$end_label - $start_label"
1076 define_label $start_label
1077 _op .2byte $_cu_version Version
1078 _op .${_cu_offset_size}byte $my_abbrevs Abbrevs
1079 _op .byte $_cu_addr_size "Pointer size"
1081 _defer_output $_abbrev_section {
1082 define_label $my_abbrevs
1087 _defer_output $_abbrev_section {
1088 # Emit the terminator.
1089 _op .byte 0x0 Terminator
1090 _op .byte 0x0 Terminator
1093 define_label $end_label
1097 # OPTIONS is a list with an even number of elements containing
1098 # option-name and option-value pairs.
1099 # Current options are:
1100 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1101 # default = 0 (32-bit)
1102 # version n - DWARF version number to emit
1104 # addr_size n - the size of addresses, 32, 64, or default
1106 # fission 0|1 - boolean indicating if generating Fission debug info
1108 # SIGNATURE is the 64-bit signature of the type.
1109 # TYPE_LABEL is the label of the type defined by this TU,
1110 # or "" if there is no type (i.e., type stubs in Fission).
1111 # BODY is Tcl code that emits the DIEs which make up the body of
1112 # the TU. It is evaluated in the caller's context.
1113 proc tu {options signature type_label body} {
1115 variable _abbrev_section
1116 variable _abbrev_num
1118 variable _cu_version
1119 variable _cu_addr_size
1120 variable _cu_offset_size
1122 # Establish the defaults.
1125 set _cu_addr_size default
1127 set section ".debug_types"
1128 set _abbrev_section ".debug_abbrev"
1130 foreach { name value } $options {
1131 switch -exact -- $name {
1132 is_64 { set is_64 $value }
1133 version { set _cu_version $value }
1134 addr_size { set _cu_addr_size $value }
1135 fission { set fission $value }
1136 default { error "unknown option $name" }
1139 if {$_cu_addr_size == "default"} {
1140 if {[is_64_target]} {
1146 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
1148 set section ".debug_types.dwo"
1149 set _abbrev_section ".debug_abbrev.dwo"
1154 set cu_num [incr _cu_count]
1155 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
1158 set _cu_label [_compute_label "cu${cu_num}_begin"]
1159 set start_label [_compute_label "cu${cu_num}_start"]
1160 set end_label [_compute_label "cu${cu_num}_end"]
1162 define_label $_cu_label
1164 _op .4byte 0xffffffff
1165 _op .8byte "$end_label - $start_label"
1167 _op .4byte "$end_label - $start_label"
1169 define_label $start_label
1170 _op .2byte $_cu_version Version
1171 _op .${_cu_offset_size}byte $my_abbrevs Abbrevs
1172 _op .byte $_cu_addr_size "Pointer size"
1173 _op .8byte $signature Signature
1174 if { $type_label != "" } {
1175 uplevel declare_labels $type_label
1176 upvar $type_label my_type_label
1178 _op .8byte "$my_type_label - $_cu_label"
1180 _op .4byte "$my_type_label - $_cu_label"
1190 _defer_output $_abbrev_section {
1191 define_label $my_abbrevs
1196 _defer_output $_abbrev_section {
1197 # Emit the terminator.
1198 _op .byte 0x0 Terminator
1199 _op .byte 0x0 Terminator
1202 define_label $end_label
1205 # Emit a DWARF .debug_ranges unit.
1206 # OPTIONS is a list with an even number of elements containing
1207 # option-name and option-value pairs.
1208 # Current options are:
1209 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1210 # default = 0 (32-bit)
1212 # BODY is Tcl code that emits the content of the .debug_ranges
1213 # unit, it is evaluated in the caller's context.
1214 proc ranges {options body} {
1215 variable _debug_ranges_64_bit
1217 foreach { name value } $options {
1218 switch -exact -- $name {
1219 is_64 { set _debug_ranges_64_bit [subst $value] }
1220 default { error "unknown option $name" }
1224 set section ".debug_ranges"
1227 proc sequence {{ranges {}}} {
1228 variable _debug_ranges_64_bit
1230 # Emit the sequence of addresses.
1232 foreach range $ranges {
1233 set range [uplevel 1 "subst \"$range\""]
1234 set type [lindex $range 0]
1235 switch -exact -- $type {
1237 set base [lrange $range 1 end]
1239 if { $_debug_ranges_64_bit } then {
1240 _op .8byte 0xffffffffffffffff "Base Marker"
1241 _op .8byte $base "Base Address"
1243 _op .4byte 0xffffffff "Base Marker"
1244 _op .4byte $base "Base Address"
1248 set start [lindex $range 1]
1249 set end [lrange $range 2 end]
1251 if { $_debug_ranges_64_bit } then {
1252 _op .8byte $start "Start Address"
1253 _op .8byte $end "End Address"
1255 _op .4byte $start "Start Address"
1256 _op .4byte $end "End Address"
1259 default { error "unknown range type: $type " }
1263 # End of the sequence.
1264 if { $_debug_ranges_64_bit } then {
1265 _op .8byte 0x0 "End of Sequence Marker (Part 1)"
1266 _op .8byte 0x0 "End of Sequence Marker (Part 2)"
1268 _op .4byte 0x0 "End of Sequence Marker (Part 1)"
1269 _op .4byte 0x0 "End of Sequence Marker (Part 2)"
1277 # Emit a DWARF .debug_line unit.
1278 # OPTIONS is a list with an even number of elements containing
1279 # option-name and option-value pairs.
1280 # Current options are:
1281 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1282 # default = 0 (32-bit)
1283 # version n - DWARF version number to emit
1285 # addr_size n - the size of addresses, 32, 64, or default
1288 # LABEL is the label of the current unit (which is probably
1289 # referenced by a DW_AT_stmt_list), or "" if there is no such
1292 # BODY is Tcl code that emits the parts which make up the body of
1293 # the line unit. It is evaluated in the caller's context. The
1294 # following commands are available for the BODY section:
1296 # include_dir "dirname" -- adds a new include directory
1298 # file_name "file.c" idx -- adds a new file name. IDX is a
1299 # 1-based index referencing an include directory or 0 for
1300 # current directory.
1302 proc lines {options label body} {
1303 variable _line_count
1304 variable _line_saw_file
1305 variable _line_saw_program
1306 variable _line_header_end_label
1308 # Establish the defaults.
1311 set _unit_addr_size default
1312 set _line_saw_program 0
1313 set _line_saw_file 0
1314 set _default_is_stmt 1
1316 foreach { name value } $options {
1317 switch -exact -- $name {
1318 is_64 { set is_64 $value }
1319 version { set _unit_version $value }
1320 addr_size { set _unit_addr_size $value }
1321 default_is_stmt { set _default_is_stmt $value }
1322 default { error "unknown option $name" }
1325 if {$_unit_addr_size == "default"} {
1326 if {[is_64_target]} {
1327 set _unit_addr_size 8
1329 set _unit_addr_size 4
1333 set unit_num [incr _line_count]
1335 set section ".debug_line"
1338 if { "$label" != "" } {
1339 # Define the user-provided label at this point.
1343 set unit_len_label [_compute_label "line${_line_count}_start"]
1344 set unit_end_label [_compute_label "line${_line_count}_end"]
1345 set header_len_label [_compute_label "line${_line_count}_header_start"]
1346 set _line_header_end_label [_compute_label "line${_line_count}_header_end"]
1349 _op .4byte 0xffffffff
1350 _op .8byte "$unit_end_label - $unit_len_label" "unit_length"
1352 _op .4byte "$unit_end_label - $unit_len_label" "unit_length"
1355 define_label $unit_len_label
1357 _op .2byte $_unit_version version
1360 _op .8byte "$_line_header_end_label - $header_len_label" "header_length"
1362 _op .4byte "$_line_header_end_label - $header_len_label" "header_length"
1365 define_label $header_len_label
1367 _op .byte 1 "minimum_instruction_length"
1368 _op .byte $_default_is_stmt "default_is_stmt"
1369 _op .byte 1 "line_base"
1370 _op .byte 1 "line_range"
1371 _op .byte 10 "opcode_base"
1373 # The standard_opcode_lengths table. The number of arguments
1374 # for each of the standard opcodes. Generating 9 entries here
1375 # matches the use of 10 in the opcode_base above. These 9
1376 # entries match the 9 standard opcodes for DWARF2, making use
1377 # of only 9 should be fine, even if we are generating DWARF3
1379 _op .byte 0 "standard opcode 1"
1380 _op .byte 1 "standard opcode 2"
1381 _op .byte 1 "standard opcode 3"
1382 _op .byte 1 "standard opcode 4"
1383 _op .byte 1 "standard opcode 5"
1384 _op .byte 0 "standard opcode 6"
1385 _op .byte 0 "standard opcode 7"
1386 _op .byte 0 "standard opcode 8"
1387 _op .byte 1 "standard opcode 9"
1389 proc include_dir {dirname} {
1390 _op .ascii [_quote $dirname]
1393 proc file_name {filename diridx} {
1394 variable _line_saw_file
1395 if "! $_line_saw_file" {
1396 # Terminate the dir list.
1397 _op .byte 0 "Terminator."
1398 set _line_saw_file 1
1401 _op .ascii [_quote $filename]
1402 _op .sleb128 $diridx
1403 _op .sleb128 0 "mtime"
1404 _op .sleb128 0 "length"
1407 proc program {statements} {
1408 variable _line_saw_program
1409 variable _line_header_end_label
1411 if "! $_line_saw_program" {
1412 # Terminate the file list.
1413 _op .byte 0 "Terminator."
1414 define_label $_line_header_end_label
1415 set _line_saw_program 1
1418 proc DW_LNE_set_address {addr} {
1420 set start [new_label "set_address_start"]
1421 set end [new_label "set_address_end"]
1422 _op .uleb128 "${end} - ${start}"
1423 define_label ${start}
1425 if {[is_64_target]} {
1433 proc DW_LNE_end_sequence {} {
1439 proc DW_LNS_copy {} {
1443 proc DW_LNS_negate_stmt {} {
1447 proc DW_LNS_advance_pc {offset} {
1449 _op .uleb128 ${offset}
1452 proc DW_LNS_advance_line {offset} {
1454 _op .sleb128 ${offset}
1457 foreach statement $statements {
1458 uplevel 1 $statement
1464 rename include_dir ""
1467 # Terminate dir list if we saw no files.
1468 if "! $_line_saw_file" {
1469 _op .byte 0 "Terminator."
1472 # Terminate the file list.
1473 if "! $_line_saw_program" {
1474 _op .byte 0 "Terminator."
1475 define_label $_line_header_end_label
1478 define_label $unit_end_label
1481 proc _empty_array {name} {
1482 upvar $name the_array
1484 catch {unset the_array}
1489 # Emit a .gnu_debugaltlink section with the given file name and
1490 # build-id. The buildid should be represented as a hexadecimal
1491 # string, like "ffeeddcc".
1492 proc gnu_debugaltlink {filename buildid} {
1493 _defer_output .gnu_debugaltlink {
1494 _op .ascii [_quote $filename]
1495 foreach {a b} [split $buildid {}] {
1501 proc _note {type name hexdata} {
1502 set namelen [expr [string length $name] + 1]
1507 _op .4byte [expr [string length $hexdata] / 2]
1511 _op .ascii [_quote $name]
1514 set total [expr {($namelen + (1 << $align) - 1) & -(1 << $align)}]
1515 for {set i $namelen} {$i < $total} {incr i} {
1519 foreach {a b} [split $hexdata {}] {
1524 # Emit a note section holding the given build-id.
1525 proc build_id {buildid} {
1526 _defer_output {.note.gnu.build-id a note} {
1527 # From elf/common.h.
1528 set NT_GNU_BUILD_ID 3
1530 _note $NT_GNU_BUILD_ID GNU $buildid
1534 # The top-level interface to the DWARF assembler.
1535 # FILENAME is the name of the file where the generated assembly
1537 # BODY is Tcl code to emit the assembly. It is evaluated via
1538 # "eval" -- not uplevel as you might expect, because it is
1539 # important to run the body in the Dwarf namespace.
1541 # A typical invocation is something like:
1542 # Dwarf::assemble $file {
1552 proc assemble {filename body} {
1553 variable _initialized
1554 variable _output_file
1555 variable _deferred_output
1560 variable _line_count
1561 variable _line_saw_file
1562 variable _line_saw_program
1563 variable _line_header_end_label
1564 variable _debug_ranges_64_bit
1566 if {!$_initialized} {
1571 set _output_file [open $filename w]
1573 _empty_array _deferred_output
1576 _empty_array _strings
1579 set _line_saw_file 0
1580 set _line_saw_program 0
1581 set _debug_ranges_64_bit [is_64_target]
1583 # Not "uplevel" here, because we want to evaluate in this
1584 # namespace. This is somewhat bad because it means we can't
1585 # readily refer to outer variables.
1588 _write_deferred_output
1590 catch {close $_output_file}