1 # Copyright 2010-2015 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" != "" } {
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 # * 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.
238 # CHILDREN is just Tcl code that can be used to define child DIEs. It
239 # is evaluated in the caller's context.
241 # Currently this code is missing nice support for CFA handling, and
242 # probably other things as well.
244 namespace eval Dwarf {
245 # True if the module has been initialized.
246 variable _initialized 0
248 # Constants from dwarf2.h.
252 # DW_FORM short names.
257 # The current output file.
258 variable _output_file
260 # Note: The _cu_ values here also apply to type units (TUs).
261 # Think of a TU as a special kind of CU.
266 # The current CU's base label.
269 # The current CU's version.
272 # The current CU's address size.
273 variable _cu_addr_size
274 # The current CU's offset size.
275 variable _cu_offset_size
277 # Label generation number.
280 # The deferred output array. The index is the section name; the
281 # contents hold the data for that section.
282 variable _deferred_output
284 # If empty, we should write directly to the output file.
285 # Otherwise, this is the name of a section to write to.
288 # The abbrev section. Typically .debug_abbrev but can be .debug_abbrev.dwo
290 variable _abbrev_section
292 # The next available abbrev number in the current CU's abbrev
296 # The string table for this assembly. The key is the string; the
297 # value is the label for that string.
300 # Current .debug_line unit count.
303 # Whether a file_name entry was seen.
304 variable _line_saw_file
306 proc _process_one_constant {name value} {
312 set _constants($name) $value
314 if {![regexp "^DW_(\[A-Z\]+)_(\[A-Za-z0-9_\]+)$" $name \
315 ignore prefix name2]} {
316 error "non-matching name: $name"
319 if {$name2 == "lo_user" || $name2 == "hi_user"} {
323 # We only try to shorten some very common things.
325 switch -exact -- $prefix {
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"
333 # Filter out ones that are known to clash.
334 if {$name2 == "variable" || $name2 == "namespace"} {
335 set name2 "tag_$name2"
338 if {[info commands $name2] != {}} {
339 error "duplicate proc name: from $name"
342 proc $name2 {{attrs {}} {children {}}} \
343 "_handle_DW_TAG $name \$attrs \$children"
347 set _AT($name2) $name
351 set _FORM($name2) $name
355 set _OP($name2) $name
364 proc _read_constants {} {
365 global srcdir hex decimal
368 # DWARF name-matching regexp.
369 set dwrx "DW_\[a-zA-Z0-9_\]+"
373 set fd [open [file join $srcdir .. .. include dwarf2.h]]
376 if {[regexp -- "^${ws}($dwrx)${ws}=${ws}($hex|$decimal),?$" \
377 $line ignore name value ignore2]} {
378 _process_one_constant $name $value
383 set fd [open [file join $srcdir .. .. include dwarf2.def]]
387 "^DW_\[A-Z_\]+${ws}\\(($dwrx),${ws}($hex|$decimal)\\)$" \
388 $line ignore name value ignore2]} {
389 _process_one_constant $name $value
394 set _constants(SPECIAL_expr) $_constants(DW_FORM_block)
397 proc _quote {string} {
399 return "\"${string}\\0\""
402 proc _nz_quote {string} {
403 # For now, no quoting is done.
404 return "\"${string}\""
407 proc _handle_DW_FORM {form value} {
408 switch -exact -- $form {
410 _op .ascii [_quote $value]
413 DW_FORM_flag_present {
414 # We don't need to emit anything.
423 variable _cu_offset_size
425 variable _cu_addr_size
427 if {$_cu_version == 2} {
428 set size $_cu_addr_size
430 set size $_cu_offset_size
433 _op .${size}byte $value
437 variable _cu_offset_size
438 _op .${_cu_offset_size}byte $value
457 variable _cu_addr_size
459 _op .${_cu_addr_size}byte $value
475 variable _cu_offset_size
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]
485 _op .${_cu_offset_size}byte $_strings($value) "strp: $value"
489 set l1 [new_label "expr_start"]
490 set l2 [new_label "expr_end"]
491 _op .uleb128 "$l2 - $l1" "expression"
498 set len [string length $value]
500 error "DW_FORM_block1 length too long"
503 _op .ascii [_nz_quote $value]
515 DW_FORM_GNU_addr_index -
516 DW_FORM_GNU_str_index -
517 DW_FORM_GNU_ref_alt -
518 DW_FORM_GNU_strp_alt -
521 error "unhandled form $form"
526 proc _guess_form {value varname} {
527 upvar $varname new_value
529 switch -exact -- [string range $value 0 0] {
531 # Constant reference.
534 set new_value $_constants([string range $value 1 end])
543 set new_value "[string range $value 1 end] - $_cu_label"
549 return DW_FORM_string
554 # Map NAME to its canonical form.
555 proc _map_name {name ary} {
558 if {[info exists ${ary}($name)]} {
559 set name [set ${ary}($name)]
565 proc _handle_attribute { attr_name attr_value attr_form } {
566 variable _abbrev_section
569 _handle_DW_FORM $attr_form $attr_value
571 _defer_output $_abbrev_section {
572 _op .uleb128 $_constants($attr_name) $attr_name
573 _op .uleb128 $_constants($attr_form) $attr_form
577 # Handle macro attribute MACRO_AT_range.
579 proc _handle_macro_at_range { attr_value } {
580 if {[llength $attr_value] != 2} {
581 error "usage: MACRO_AT_range { func file }"
584 set func [lindex $attr_value 0]
585 set src [lindex $attr_value 1]
586 set result [function_range $func $src]
588 _handle_attribute DW_AT_low_pc [lindex $result 0] \
590 _handle_attribute DW_AT_high_pc \
591 "[lindex $result 0] + [lindex $result 1]" DW_FORM_addr
594 # Handle macro attribute MACRO_AT_func.
596 proc _handle_macro_at_func { attr_value } {
597 if {[llength $attr_value] != 2} {
598 error "usage: MACRO_AT_func { func file }"
600 _handle_attribute DW_AT_name [lindex $attr_value 0] DW_FORM_string
601 _handle_macro_at_range $attr_value
604 proc _handle_DW_TAG {tag_name {attrs {}} {children {}}} {
605 variable _abbrev_section
609 set has_children [expr {[string length $children] > 0}]
610 set my_abbrev [incr _abbrev_num]
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"
620 _op .uleb128 $my_abbrev "Abbrev ($tag_name)"
622 foreach attr $attrs {
623 set attr_name [_map_name [lindex $attr 0] _AT]
624 set attr_value [uplevel 2 [list subst [lindex $attr 1]]]
626 if { [string equal "MACRO_AT_func" $attr_name] } {
627 _handle_macro_at_func $attr_value
628 } elseif { [string equal "MACRO_AT_range" $attr_name] } {
629 _handle_macro_at_range $attr_value
631 if {[llength $attr] > 2} {
632 set attr_form [lindex $attr 2]
634 # If the value looks like an integer, a form is required.
635 if [string is integer $attr_value] {
636 error "Integer value requires a form"
638 set attr_form [_guess_form $attr_value attr_value]
640 set attr_form [_map_name $attr_form _FORM]
642 _handle_attribute $attr_name $attr_value $attr_form
646 _defer_output $_abbrev_section {
648 _op .byte 0x0 Terminator
649 _op .byte 0x0 Terminator
655 # Terminate children.
656 _op .byte 0x0 "Terminate children"
660 proc _emit {string} {
661 variable _output_file
663 variable _deferred_output
666 puts $_output_file $string
668 append _deferred_output($_defer) ${string}\n
672 proc _section {name {flags ""} {type ""}} {
673 if {$flags == "" && $type == ""} {
674 _emit " .section $name"
675 } elseif {$type == ""} {
676 _emit " .section $name, \"$flags\""
678 _emit " .section $name, \"$flags\", %$type"
682 # SECTION_SPEC is a list of arguments to _section.
683 proc _defer_output {section_spec body} {
685 variable _deferred_output
687 set old_defer $_defer
688 set _defer [lindex $section_spec 0]
690 if {![info exists _deferred_output($_defer)]} {
691 set _deferred_output($_defer) ""
692 eval _section $section_spec
697 set _defer $old_defer
700 proc _defer_to_string {body} {
702 variable _deferred_output
704 set old_defer $_defer
707 set _deferred_output($_defer) ""
711 set result $_deferred_output($_defer)
712 unset _deferred_output($_defer)
714 set _defer $old_defer
718 proc _write_deferred_output {} {
719 variable _output_file
720 variable _deferred_output
722 foreach section [array names _deferred_output] {
723 # The data already has a newline.
724 puts -nonewline $_output_file $_deferred_output($section)
728 unset _deferred_output
731 proc _op {name value {comment ""}} {
732 set text " ${name} ${value}"
733 if {$comment != ""} {
734 # Try to make stuff line up nicely.
735 while {[string length $text] < 40} {
738 append text "/* ${comment} */"
743 proc _compute_label {name} {
747 # Return a name suitable for use as a label. If BASE_NAME is
748 # specified, it is incorporated into the label name; this is to
749 # make debugging the generated assembler easier. If BASE_NAME is
750 # not specified a generic default is used. This proc does not
751 # define the label; see 'define_label'. 'new_label' attempts to
752 # ensure that label names are unique.
753 proc new_label {{base_name label}} {
756 return [_compute_label ${base_name}[incr _label_num]]
759 # Define a label named NAME. Ordinarily, NAME comes from a call
760 # to 'new_label', but this is not required.
761 proc define_label {name} {
765 # Declare a global label. This is typically used to refer to
766 # labels defined in other files, for example a function defined in
774 # A higher-level interface to label handling.
776 # ARGS is a list of label descriptors. Each one is either a
777 # single element, or a list of two elements -- a name and some
778 # text. For each descriptor, 'new_label' is invoked. If the list
779 # form is used, the second element in the list is passed as an
780 # argument. The label name is used to define a variable in the
781 # enclosing scope; this can be used to refer to the label later.
782 # The label name is also used to define a new proc whose name is
783 # the label name plus a trailing ":". This proc takes a body as
784 # an argument and can be used to define the label at that point;
785 # then the body, if any, is evaluated in the caller's context.
789 # declare_labels int_label
790 # something { ... $int_label } ;# refer to the label
791 # int_label: constant { ... } ;# define the label
792 proc declare_labels {args} {
794 set name [lindex $arg 0]
795 set text [lindex $arg 1]
797 upvar $name label_var
799 set label_var [new_label]
801 set label_var [new_label $text]
804 proc ${name}: {args} [format {
811 # This is a miniature assembler for location expressions. It is
812 # suitable for use in the attributes to a DIE. Its output is
813 # prefixed with "=" to make it automatically use DW_FORM_block.
814 # BODY is split by lines, and each line is taken to be a list.
815 # (FIXME should use 'info complete' here.)
816 # Each list's first element is the opcode, either short or long
817 # forms are accepted.
818 # FIXME argument handling
820 proc _location {body} {
823 variable _cu_addr_size
824 variable _cu_offset_size
826 foreach line [split $body \n] {
827 # Ignore blank lines, and allow embedded comments.
828 if {[lindex $line 0] == "" || [regexp -- {^[ \t]*#} $line]} {
831 set opcode [_map_name [lindex $line 0] _OP]
832 _op .byte $_constants($opcode) $opcode
834 switch -exact -- $opcode {
836 _op .${_cu_addr_size}byte [lindex $line 1]
842 _op .byte [lindex $line 1]
847 _op .2byte [lindex $line 1]
852 _op .4byte [lindex $line 1]
857 _op .8byte [lindex $line 1]
861 _op .uleb128 [lindex $line 1]
864 _op .sleb128 [lindex $line 1]
868 _op .uleb128 [lindex $line 1]
872 _op .uleb128 [lindex $line 1]
876 _op .uleb128 [lindex $line 1]
877 _op .uleb128 [lindex $line 2]
882 _op .2byte [lindex $line 1]
885 DW_OP_GNU_implicit_pointer {
886 if {[llength $line] != 3} {
887 error "usage: DW_OP_GNU_implicit_pointer LABEL OFFSET"
890 # Here label is a section offset.
891 set label [lindex $line 1]
892 _op .${_cu_offset_size}byte $label
893 _op .sleb128 [lindex $line 2]
897 if {[llength $line] != 2} {
898 error "usage: DW_OP_deref_size SIZE"
901 _op .byte [lindex $line 1]
905 if {[llength $line] > 1} {
906 error "Unimplemented: operands in location for $opcode"
914 # OPTIONS is a list with an even number of elements containing
915 # option-name and option-value pairs.
916 # Current options are:
917 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
918 # default = 0 (32-bit)
919 # version n - DWARF version number to emit
921 # addr_size n - the size of addresses, 32, 64, or default
923 # fission 0|1 - boolean indicating if generating Fission debug info
925 # BODY is Tcl code that emits the DIEs which make up the body of
926 # the CU. It is evaluated in the caller's context.
927 proc cu {options body} {
929 variable _abbrev_section
933 variable _cu_addr_size
934 variable _cu_offset_size
936 # Establish the defaults.
939 set _cu_addr_size default
941 set section ".debug_info"
942 set _abbrev_section ".debug_abbrev"
944 foreach { name value } $options {
945 switch -exact -- $name {
946 is_64 { set is_64 $value }
947 version { set _cu_version $value }
948 addr_size { set _cu_addr_size $value }
949 fission { set fission $value }
950 default { error "unknown option $name" }
953 if {$_cu_addr_size == "default"} {
954 if {[is_64_target]} {
960 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
962 set section ".debug_info.dwo"
963 set _abbrev_section ".debug_abbrev.dwo"
968 set cu_num [incr _cu_count]
969 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
972 set _cu_label [_compute_label "cu${cu_num}_begin"]
973 set start_label [_compute_label "cu${cu_num}_start"]
974 set end_label [_compute_label "cu${cu_num}_end"]
976 define_label $_cu_label
978 _op .4byte 0xffffffff
979 _op .8byte "$end_label - $start_label"
981 _op .4byte "$end_label - $start_label"
983 define_label $start_label
984 _op .2byte $_cu_version Version
985 _op .${_cu_offset_size}byte $my_abbrevs Abbrevs
986 _op .byte $_cu_addr_size "Pointer size"
988 _defer_output $_abbrev_section {
989 define_label $my_abbrevs
994 _defer_output $_abbrev_section {
995 # Emit the terminator.
996 _op .byte 0x0 Terminator
997 _op .byte 0x0 Terminator
1000 define_label $end_label
1004 # OPTIONS is a list with an even number of elements containing
1005 # option-name and option-value pairs.
1006 # Current options are:
1007 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1008 # default = 0 (32-bit)
1009 # version n - DWARF version number to emit
1011 # addr_size n - the size of addresses, 32, 64, or default
1013 # fission 0|1 - boolean indicating if generating Fission debug info
1015 # SIGNATURE is the 64-bit signature of the type.
1016 # TYPE_LABEL is the label of the type defined by this TU,
1017 # or "" if there is no type (i.e., type stubs in Fission).
1018 # BODY is Tcl code that emits the DIEs which make up the body of
1019 # the TU. It is evaluated in the caller's context.
1020 proc tu {options signature type_label body} {
1022 variable _abbrev_section
1023 variable _abbrev_num
1025 variable _cu_version
1026 variable _cu_addr_size
1027 variable _cu_offset_size
1029 # Establish the defaults.
1032 set _cu_addr_size default
1034 set section ".debug_types"
1035 set _abbrev_section ".debug_abbrev"
1037 foreach { name value } $options {
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_types.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"
1080 _op .8byte $signature Signature
1081 if { $type_label != "" } {
1082 uplevel declare_labels $type_label
1083 upvar $type_label my_type_label
1085 _op .8byte "$my_type_label - $_cu_label"
1087 _op .4byte "$my_type_label - $_cu_label"
1097 _defer_output $_abbrev_section {
1098 define_label $my_abbrevs
1103 _defer_output $_abbrev_section {
1104 # Emit the terminator.
1105 _op .byte 0x0 Terminator
1106 _op .byte 0x0 Terminator
1109 define_label $end_label
1112 # Emit a DWARF .debug_line unit.
1113 # OPTIONS is a list with an even number of elements containing
1114 # option-name and option-value pairs.
1115 # Current options are:
1116 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1117 # default = 0 (32-bit)
1118 # version n - DWARF version number to emit
1120 # addr_size n - the size of addresses, 32, 64, or default
1123 # LABEL is the label of the current unit (which is probably
1124 # referenced by a DW_AT_stmt_list), or "" if there is no such
1127 # BODY is Tcl code that emits the parts which make up the body of
1128 # the line unit. It is evaluated in the caller's context. The
1129 # following commands are available for the BODY section:
1131 # include_dir "dirname" -- adds a new include directory
1133 # file_name "file.c" idx -- adds a new file name. IDX is a
1134 # 1-based index referencing an include directory or 0 for
1135 # current directory.
1137 proc lines {options label body} {
1138 variable _line_count
1139 variable _line_saw_file
1141 # Establish the defaults.
1144 set _unit_addr_size default
1146 foreach { name value } $options {
1147 switch -exact -- $name {
1148 is_64 { set is_64 $value }
1149 version { set _unit_version $value }
1150 addr_size { set _unit_addr_size $value }
1151 default { error "unknown option $name" }
1154 if {$_unit_addr_size == "default"} {
1155 if {[is_64_target]} {
1156 set _unit_addr_size 8
1158 set _unit_addr_size 4
1162 set unit_num [incr _line_count]
1164 set section ".debug_line"
1167 if { "$label" != "" } {
1168 # Define the user-provided label at this point.
1172 set unit_len_label [_compute_label "line${_line_count}_start"]
1173 set unit_end_label [_compute_label "line${_line_count}_end"]
1174 set header_len_label [_compute_label "line${_line_count}_header_start"]
1175 set header_end_label [_compute_label "line${_line_count}_header_end"]
1178 _op .4byte 0xffffffff
1179 _op .8byte "$unit_end_label - $unit_len_label" "unit_length"
1181 _op .4byte "$unit_end_label - $unit_len_label" "unit_length"
1184 define_label $unit_len_label
1186 _op .2byte $_unit_version version
1189 _op .8byte "$header_end_label - $header_len_label" "header_length"
1191 _op .4byte "$header_end_label - $header_len_label" "header_length"
1194 define_label $header_len_label
1196 _op .byte 1 "minimum_instruction_length"
1197 _op .byte 0 "default_is_stmt"
1198 _op .byte 1 "line_base"
1199 _op .byte 1 "line_range"
1200 _op .byte 1 "opcode_base"
1201 # Since we emit opcode_base==1, we skip
1202 # standard_opcode_length table altogether.
1204 proc include_dir {dirname} {
1205 _op .ascii [_quote $dirname]
1208 proc file_name {filename diridx} {
1209 variable _line_saw_file
1210 if "! $_line_saw_file" {
1211 # Terminate the dir list.
1212 _op .byte 0 "Terminator."
1213 set _line_saw_file 1
1216 _op .ascii [_quote $filename]
1217 _op .sleb128 $diridx
1218 _op .sleb128 0 "mtime"
1219 _op .sleb128 0 "length"
1224 rename include_dir ""
1227 # Terminate dir list if we saw no files.
1228 if "! $_line_saw_file" {
1229 _op .byte 0 "Terminator."
1232 # Terminate the file list.
1233 _op .byte 0 "Terminator."
1235 define_label $header_end_label
1236 define_label $unit_end_label
1239 proc _empty_array {name} {
1240 upvar $name the_array
1242 catch {unset the_array}
1247 # Emit a .gnu_debugaltlink section with the given file name and
1248 # build-id. The buildid should be represented as a hexadecimal
1249 # string, like "ffeeddcc".
1250 proc gnu_debugaltlink {filename buildid} {
1251 _defer_output .gnu_debugaltlink {
1252 _op .ascii [_quote $filename]
1253 foreach {a b} [split $buildid {}] {
1259 proc _note {type name hexdata} {
1260 set namelen [expr [string length $name] + 1]
1265 _op .4byte [expr [string length $hexdata] / 2]
1269 _op .ascii [_quote $name]
1272 set total [expr {($namelen + (1 << $align) - 1) & (-1 << $align)}]
1273 for {set i $namelen} {$i < $total} {incr i} {
1277 foreach {a b} [split $hexdata {}] {
1282 # Emit a note section holding the given build-id.
1283 proc build_id {buildid} {
1284 _defer_output {.note.gnu.build-id a note} {
1285 # From elf/common.h.
1286 set NT_GNU_BUILD_ID 3
1288 _note $NT_GNU_BUILD_ID GNU $buildid
1292 # The top-level interface to the DWARF assembler.
1293 # FILENAME is the name of the file where the generated assembly
1295 # BODY is Tcl code to emit the assembly. It is evaluated via
1296 # "eval" -- not uplevel as you might expect, because it is
1297 # important to run the body in the Dwarf namespace.
1299 # A typical invocation is something like:
1300 # Dwarf::assemble $file {
1310 proc assemble {filename body} {
1311 variable _initialized
1312 variable _output_file
1313 variable _deferred_output
1318 variable _line_count
1319 variable _line_saw_file
1321 if {!$_initialized} {
1326 set _output_file [open $filename w]
1328 _empty_array _deferred_output
1331 _empty_array _strings
1334 set _line_saw_file 0
1336 # Not "uplevel" here, because we want to evaluate in this
1337 # namespace. This is somewhat bad because it means we can't
1338 # readily refer to outer variables.
1341 _write_deferred_output
1343 catch {close $_output_file}