1 # Copyright 2010-2021 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 powerpc-*-eabi*]} {
31 # Use 'objcopy --extract-dwo to extract DWO information from
32 # OBJECT_FILE and place it into DWO_FILE.
34 # Return 0 on success, otherwise, return -1.
35 proc extract_dwo_information { object_file dwo_file } {
36 set objcopy [gdb_find_objcopy]
37 set command "$objcopy --extract-dwo $object_file $dwo_file"
38 verbose -log "Executing $command"
39 set result [catch "exec $command" output]
40 verbose -log "objcopy --extract-dwo output: $output"
47 # Use 'objcopy --strip-dwo to remove DWO information from
50 # Return 0 on success, otherwise, return -1.
51 proc strip_dwo_information { filename } {
52 set objcopy [gdb_find_objcopy]
53 set command "$objcopy --strip-dwo $filename"
54 verbose -log "Executing $command"
55 set result [catch "exec $command" output]
56 verbose -log "objcopy --strip-dwo output: $output"
63 # Build an executable, with the debug information split out into a
66 # This function is based on build_executable_from_specs in
67 # lib/gdb.exp, but with threading support, and rust support removed.
69 # TESTNAME is the name of the test; this is passed to 'untested' if
72 # EXECUTABLE is the executable to create, this can be an absolute
73 # path, or a relative path, in which case the EXECUTABLE will be
74 # created in the standard output directory.
76 # OPTIONS is passed to the final link, using gdb_compile. If OPTIONS
77 # contains any option that indicates threads is required, of if the
78 # option rust is included, then this function will return failure.
80 # ARGS is a series of lists. Each list is a spec for one source file
81 # that will be compiled to make EXECUTABLE. Each spec in ARGS has the
85 # [ SOURCE OPTIONS OBJFILE ]
87 # Where SOURCE is the path to the source file to compile. This can be
88 # absolute, or relative to the standard global ${subdir}/${srcdir}/
91 # OPTIONS are the options to use when compiling SOURCE into an object
94 # OBJFILE is optional, if present this is the name of the object file
95 # to create for SOURCE. If this is not provided then a suitable name
96 # will be auto-generated.
98 # If OPTIONS contains the option 'split-dwo' then the debug
99 # information is extracted from the object file created by compiling
100 # SOURCE and placed into a file with a dwo extension. The name of
101 # this file is generated based on the name of the object file that was
102 # created (with the .o replaced with .dwo).
103 proc build_executable_and_dwo_files { testname executable options args } {
107 if { ! [regexp "^/" "$executable"] } then {
108 set binfile [standard_output_file $executable]
110 set binfile $executable
114 if { [lsearch -exact $options "c++"] >= 0 } {
115 set info_options "c++"
117 if [get_compiler_info ${info_options}] {
122 if {[lsearch -regexp $options \
123 {^(pthreads|shlib|shlib_pthreads|openmp)$}] != -1} {
124 # Currently don't support compiling thread based tests here.
125 # If this is required then look to build_executable_from_specs
129 if {[lsearch -exact $options rust] != -1} {
130 # Currently don't support compiling rust tests here. If this
131 # is required then look to build_executable_from_specs for
136 # Must be run on local host due to use of objcopy.
137 if [is_remote host] {
144 if {[llength $spec] < 2} {
145 error "invalid spec length"
149 verbose -log "APB: SPEC: $spec"
151 set s [lindex $spec 0]
152 set local_options [lindex $spec 1]
154 if { ! [regexp "^/" "$s"] } then {
155 set s "$srcdir/$subdir/$s"
158 if {[llength $spec] > 2} {
159 set objfile [lindex $spec 2]
161 set objfile "${binfile}${i}.o"
165 if { [$func "${s}" "${objfile}" object $local_options] != "" } {
170 lappend objects "$objfile"
172 if {[lsearch -exact $local_options "split-dwo"] >= 0} {
173 # Split out the DWO file.
174 set dwo_file "[file rootname ${objfile}].dwo"
176 if { [extract_dwo_information $objfile $dwo_file] == -1 } {
181 if { [strip_dwo_information $objfile] == -1 } {
188 verbose -log "APB: OBJECTS = $objects"
190 set ret [$func $objects "${binfile}" executable $options]
199 # Return a list of expressions about function FUNC's address and length.
200 # The first expression is the address of function FUNC, and the second
201 # one is FUNC's length. SRC is the source file having function FUNC.
202 # An internal label ${func}_label must be defined inside FUNC:
206 # asm ("main_label: .globl main_label");
210 # This label is needed to compute the start address of function FUNC.
211 # If the compiler is gcc, we can do the following to get function start
212 # and end address too:
214 # asm ("func_start: .globl func_start");
215 # static void func (void) {}
216 # asm ("func_end: .globl func_end");
218 # however, this isn't portable, because other compilers, such as clang,
219 # may not guarantee the order of global asms and function. The code
222 # asm ("func_start: .globl func_start");
223 # asm ("func_end: .globl func_end");
224 # static void func (void) {}
227 proc function_range { func src {options {debug}} } {
228 global decimal gdb_prompt
230 set exe [standard_temp_file func_addr[pid].x]
232 gdb_compile $src $exe executable $options
238 # Compute the label offset, and we can get the function start address
239 # by "${func}_label - $func_label_offset".
240 set func_label_offset ""
241 set test "p ${func}_label - ${func}"
242 gdb_test_multiple $test $test {
243 -re ".* = ($decimal)\r\n$gdb_prompt $" {
244 set func_label_offset $expect_out(1,string)
248 # Compute the function length.
251 set test "disassemble $func"
252 gdb_test_multiple $test $test {
253 -re ".*$hex <\\+($decimal)>:\[^\r\n\]+\r\nEnd of assembler dump\.\r\n$gdb_prompt $" {
254 set func_length $expect_out(1,string)
258 # Compute the size of the last instruction.
259 if { $func_length == 0 } then {
260 set func_pattern "$func"
262 set func_pattern "$func\\+$func_length"
264 set test "x/2i $func+$func_length"
265 gdb_test_multiple $test $test {
266 -re ".*($hex) <$func_pattern>:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
267 set start $expect_out(1,string)
268 set end $expect_out(2,string)
270 set func_length [expr $func_length + $end - $start]
274 return [list "${func}_label - $func_label_offset" $func_length]
277 # Extract the start, length, and end for function called NAME and
278 # create suitable variables in the callers scope.
279 proc get_func_info { name {options {debug}} } {
280 global srcdir subdir srcfile
282 upvar 1 "${name}_start" func_start
283 upvar 1 "${name}_len" func_len
284 upvar 1 "${name}_end" func_end
286 lassign [function_range ${name} \
287 [list ${srcdir}/${subdir}/$srcfile] \
290 set func_end "$func_start + $func_len"
295 # All the variables in this namespace are private to the
296 # implementation. Also, any procedure whose name starts with "_" is
297 # private as well. Do not use these.
299 # Exported functions are documented at their definition.
301 # In addition to the hand-written functions documented below, this
302 # module automatically generates a function for each DWARF tag. For
303 # most tags, two forms are made: a full name, and one with the
304 # "DW_TAG_" prefix stripped. For example, you can use either
305 # 'DW_TAG_compile_unit' or 'compile_unit' interchangeably.
307 # There are two exceptions to this rule: DW_TAG_variable and
308 # DW_TAG_namespace. For these, the full name must always be used,
309 # as the short name conflicts with Tcl builtins. (Should future
310 # versions of Tcl or DWARF add more conflicts, this list will grow.
311 # If you want to be safe you should always use the full names.)
313 # Each tag procedure is defined like:
315 # proc DW_TAG_mumble {{attrs {}} {children {}}} { ... }
317 # ATTRS is an optional list of attributes.
318 # It is run through 'subst' in the caller's context before processing.
320 # Each attribute in the list has one of two forms:
322 # 2. { NAME VALUE FORM }
324 # In each case, NAME is the attribute's name.
325 # This can either be the full name, like 'DW_AT_name', or a shortened
326 # name, like 'name'. These are fully equivalent.
328 # Besides DWARF standard attributes, assembler supports 'macro' attribute
329 # which will be substituted by one or more standard or macro attributes.
330 # supported macro attributes are:
332 # - MACRO_AT_range { FUNC }
333 # It is substituted by DW_AT_low_pc and DW_AT_high_pc with the start and
334 # end address of function FUNC in file $srcdir/$subdir/$srcfile.
336 # - MACRO_AT_func { FUNC }
337 # It is substituted by DW_AT_name with FUNC and MACRO_AT_range.
339 # If FORM is given, it should name a DW_FORM_ constant.
340 # This can either be the short form, like 'DW_FORM_addr', or a
341 # shortened version, like 'addr'. If the form is given, VALUE
342 # is its value; see below. In some cases, additional processing
343 # is done; for example, DW_FORM_strp manages the .debug_str
344 # section automatically.
346 # If FORM is 'SPECIAL_expr', then VALUE is treated as a location
347 # expression. The effective form is then DW_FORM_block or DW_FORM_exprloc
348 # for DWARF version >= 4, and VALUE is passed to the (internal)
349 # '_location' proc to be translated.
350 # This proc implements a miniature DW_OP_ assembler.
352 # If FORM is not given, it is guessed:
353 # * If VALUE starts with the "@" character, the rest of VALUE is
354 # looked up as a DWARF constant, and DW_FORM_sdata is used. For
355 # example, '@DW_LANG_c89' could be used.
356 # * If VALUE starts with the ":" character, then it is a label
357 # reference. The rest of VALUE is taken to be the name of a label,
358 # and DW_FORM_ref4 is used. See 'new_label' and 'define_label'.
359 # * If VALUE starts with the "%" character, then it is a label
360 # reference too, but DW_FORM_ref_addr is used.
361 # * Otherwise, if the attribute name has a default form (f.i. DW_FORM_addr for
362 # DW_AT_low_pc), then that one is used.
363 # * Otherwise, an error is reported. Either specify a form explicitly, or
364 # add a default for the the attribute name in _default_form.
366 # CHILDREN is just Tcl code that can be used to define child DIEs. It
367 # is evaluated in the caller's context.
369 # Currently this code is missing nice support for CFA handling, and
370 # probably other things as well.
372 namespace eval Dwarf {
373 # True if the module has been initialized.
374 variable _initialized 0
376 # Constants from dwarf2.h.
380 # DW_FORM short names.
385 # The current output file.
386 variable _output_file
388 # Note: The _cu_ values here also apply to type units (TUs).
389 # Think of a TU as a special kind of CU.
394 # The current CU's base label.
397 # The current CU's version.
400 # The current CU's address size.
401 variable _cu_addr_size
402 # The current CU's offset size.
403 variable _cu_offset_size
405 # Label generation number.
408 # The deferred output array. The index is the section name; the
409 # contents hold the data for that section.
410 variable _deferred_output
412 # If empty, we should write directly to the output file.
413 # Otherwise, this is the name of a section to write to.
416 # The abbrev section. Typically .debug_abbrev but can be .debug_abbrev.dwo
418 variable _abbrev_section
420 # The next available abbrev number in the current CU's abbrev
424 # The string table for this assembly. The key is the string; the
425 # value is the label for that string.
428 # Current .debug_line unit count.
431 # Whether a file_name entry was seen.
432 variable _line_saw_file
434 # Whether a line table program has been seen.
435 variable _line_saw_program
437 # A Label for line table header generation.
438 variable _line_header_end_label
440 # The address size for debug ranges section.
441 variable _debug_ranges_64_bit
443 # The index into the .debug_addr section (used for fission
445 variable _debug_addr_index
447 # Flag, true if the current CU is contains fission information,
449 variable _cu_is_fission
451 proc _process_one_constant {name value} {
457 set _constants($name) $value
459 if {![regexp "^DW_(\[A-Z\]+)_(\[A-Za-z0-9_\]+)$" $name \
460 ignore prefix name2]} {
461 error "non-matching name: $name"
464 if {$name2 == "lo_user" || $name2 == "hi_user"} {
468 # We only try to shorten some very common things.
470 switch -exact -- $prefix {
472 # Create two procedures for the tag. These call
473 # _handle_DW_TAG with the full tag name baked in; this
474 # does all the actual work.
475 proc $name {{attrs {}} {children {}}} \
476 "_handle_DW_TAG $name \$attrs \$children"
478 # Filter out ones that are known to clash.
479 if {$name2 == "variable" || $name2 == "namespace"} {
480 set name2 "tag_$name2"
483 if {[info commands $name2] != {}} {
484 error "duplicate proc name: from $name"
487 proc $name2 {{attrs {}} {children {}}} \
488 "_handle_DW_TAG $name \$attrs \$children"
492 set _AT($name2) $name
496 set _FORM($name2) $name
500 set _OP($name2) $name
509 proc _read_constants {} {
510 global srcdir hex decimal
512 # DWARF name-matching regexp.
513 set dwrx "DW_\[a-zA-Z0-9_\]+"
517 set fd [open [file join $srcdir .. .. include dwarf2.h]]
520 if {[regexp -- "^${ws}($dwrx)${ws}=${ws}($hex|$decimal),?$" \
521 $line ignore name value ignore2]} {
522 _process_one_constant $name $value
527 set fd [open [file join $srcdir .. .. include dwarf2.def]]
531 "^DW_\[A-Z_\]+${ws}\\(($dwrx),${ws}($hex|$decimal)\\)$" \
532 $line ignore name value ignore2]} {
533 _process_one_constant $name $value
539 proc _quote {string} {
541 return "\"${string}\\0\""
544 proc _nz_quote {string} {
545 # For now, no quoting is done.
546 return "\"${string}\""
549 proc _handle_DW_FORM {form value} {
550 switch -exact -- $form {
552 _op .ascii [_quote $value]
555 DW_FORM_flag_present {
556 # We don't need to emit anything.
565 variable _cu_offset_size
567 variable _cu_addr_size
569 if {$_cu_version == 2} {
570 set size $_cu_addr_size
572 set size $_cu_offset_size
575 _op .${size}byte $value
578 DW_FORM_GNU_ref_alt -
579 DW_FORM_GNU_strp_alt -
581 variable _cu_offset_size
582 _op .${_cu_offset_size}byte $value
603 variable _cu_addr_size
605 _op .${_cu_addr_size}byte $value
608 DW_FORM_GNU_addr_index {
609 variable _debug_addr_index
610 variable _cu_addr_size
612 _op .uleb128 ${_debug_addr_index}
613 incr _debug_addr_index
615 _defer_output .debug_addr {
616 _op .${_cu_addr_size}byte $value
637 variable _cu_offset_size
639 if {![info exists _strings($value)]} {
640 set _strings($value) [new_label strp]
641 _defer_output .debug_str {
642 define_label $_strings($value)
643 _op .ascii [_quote $value]
647 _op .${_cu_offset_size}byte $_strings($value) "strp: $value"
652 variable _cu_addr_size
653 variable _cu_offset_size
655 set l1 [new_label "expr_start"]
656 set l2 [new_label "expr_end"]
657 _op .uleb128 "$l2 - $l1" "expression"
659 _location $value $_cu_version $_cu_addr_size $_cu_offset_size
664 set len [string length $value]
666 error "DW_FORM_block1 length too long"
669 _op .ascii [_nz_quote $value]
687 DW_FORM_GNU_str_index -
690 error "unhandled form $form"
695 proc _guess_form {value varname} {
696 upvar $varname new_value
698 switch -exact -- [string range $value 0 0] {
700 # Constant reference.
703 set new_value $_constants([string range $value 1 end])
712 set new_value "[string range $value 1 end] - $_cu_label"
718 # Label reference, an offset from .debug_info.
719 set new_value "[string range $value 1 end]"
721 return DW_FORM_ref_addr
730 proc _default_form { attr } {
731 switch -exact -- $attr {
738 DW_AT_MIPS_linkage_name -
740 return DW_FORM_string
742 DW_AT_GNU_addr_base {
743 return DW_FORM_sec_offset
749 # Map NAME to its canonical form.
750 proc _map_name {name ary} {
753 if {[info exists ${ary}($name)]} {
754 set name [set ${ary}($name)]
760 proc _handle_attribute { attr_name attr_value attr_form } {
761 variable _abbrev_section
765 _handle_DW_FORM $attr_form $attr_value
767 _defer_output $_abbrev_section {
768 if { $attr_form eq "SPECIAL_expr" } {
769 if { $_cu_version < 4 } {
770 set attr_form_comment "DW_FORM_block"
772 set attr_form_comment "DW_FORM_exprloc"
775 set attr_form_comment $attr_form
777 _op .uleb128 $_constants($attr_name) $attr_name
778 _op .uleb128 $_constants($attr_form) $attr_form_comment
782 # Handle macro attribute MACRO_AT_range.
784 proc _handle_macro_at_range { attr_value } {
785 variable _cu_is_fission
787 if {[llength $attr_value] != 1} {
788 error "usage: MACRO_AT_range { func }"
791 set func [lindex $attr_value 0]
792 global srcdir subdir srcfile
793 set src ${srcdir}/${subdir}/${srcfile}
794 set result [function_range $func $src]
796 set form DW_FORM_addr
797 if { $_cu_is_fission } {
798 set form DW_FORM_GNU_addr_index
801 _handle_attribute DW_AT_low_pc [lindex $result 0] $form
802 _handle_attribute DW_AT_high_pc \
803 "[lindex $result 0] + [lindex $result 1]" $form
806 # Handle macro attribute MACRO_AT_func.
808 proc _handle_macro_at_func { attr_value } {
809 if {[llength $attr_value] != 1} {
810 error "usage: MACRO_AT_func { func file }"
812 _handle_attribute DW_AT_name [lindex $attr_value 0] DW_FORM_string
813 _handle_macro_at_range $attr_value
816 proc _handle_DW_TAG {tag_name {attrs {}} {children {}}} {
817 variable _abbrev_section
821 set has_children [expr {[string length $children] > 0}]
822 set my_abbrev [incr _abbrev_num]
824 # We somewhat wastefully emit a new abbrev entry for each tag.
825 # There's no reason for this other than laziness.
826 _defer_output $_abbrev_section {
827 _op .uleb128 $my_abbrev "Abbrev start"
828 _op .uleb128 $_constants($tag_name) $tag_name
829 _op .byte $has_children "has_children"
832 _op .uleb128 $my_abbrev "Abbrev ($tag_name)"
834 foreach attr $attrs {
835 set attr_name [_map_name [lindex $attr 0] _AT]
837 # When the length of ATTR is greater than 2, the last
838 # element of the list must be a form. The second through
839 # the penultimate elements are joined together and
840 # evaluated using subst. This allows constructs such as
841 # [gdb_target_symbol foo] to be used.
843 if {[llength $attr] > 2} {
844 set attr_value [uplevel 2 [list subst [join [lrange $attr 1 end-1]]]]
846 set attr_value [uplevel 2 [list subst [lindex $attr 1]]]
849 if { [string equal "MACRO_AT_func" $attr_name] } {
850 _handle_macro_at_func $attr_value
851 } elseif { [string equal "MACRO_AT_range" $attr_name] } {
852 _handle_macro_at_range $attr_value
854 if {[llength $attr] > 2} {
855 set attr_form [uplevel 2 [list subst [lindex $attr end]]]
857 if { [string index $attr_value 0] == ":" } {
858 # It is a label, get its value.
859 _guess_form $attr_value attr_value
862 set attr_form [_guess_form $attr_value attr_value]
863 if { $attr_form eq "" } {
864 set attr_form [_default_form $attr_name]
866 if { $attr_form eq "" } {
867 error "No form for $attr_name $attr_value"
870 set attr_form [_map_name $attr_form _FORM]
872 _handle_attribute $attr_name $attr_value $attr_form
876 _defer_output $_abbrev_section {
878 _op .byte 0x0 "DW_AT - Terminator"
879 _op .byte 0x0 "DW_FORM - Terminator"
885 # Terminate children.
886 _op .byte 0x0 "Terminate children"
890 proc _emit {string} {
891 variable _output_file
893 variable _deferred_output
896 puts $_output_file $string
898 append _deferred_output($_defer) ${string}\n
902 proc _section {name {flags ""} {type ""}} {
903 if {$flags == "" && $type == ""} {
904 _emit " .section $name"
905 } elseif {$type == ""} {
906 _emit " .section $name, \"$flags\""
908 _emit " .section $name, \"$flags\", %$type"
912 # SECTION_SPEC is a list of arguments to _section.
913 proc _defer_output {section_spec body} {
915 variable _deferred_output
917 set old_defer $_defer
918 set _defer [lindex $section_spec 0]
920 if {![info exists _deferred_output($_defer)]} {
921 set _deferred_output($_defer) ""
922 eval _section $section_spec
927 set _defer $old_defer
930 proc _defer_to_string {body} {
932 variable _deferred_output
934 set old_defer $_defer
937 set _deferred_output($_defer) ""
941 set result $_deferred_output($_defer)
942 unset _deferred_output($_defer)
944 set _defer $old_defer
948 proc _write_deferred_output {} {
949 variable _output_file
950 variable _deferred_output
952 foreach section [array names _deferred_output] {
953 # The data already has a newline.
954 puts -nonewline $_output_file $_deferred_output($section)
958 unset _deferred_output
961 proc _op {name value {comment ""}} {
962 set text " ${name} ${value}"
963 if {$comment != ""} {
964 # Try to make stuff line up nicely.
965 while {[string length $text] < 40} {
968 append text "/* ${comment} */"
973 proc _compute_label {name} {
977 # Return a name suitable for use as a label. If BASE_NAME is
978 # specified, it is incorporated into the label name; this is to
979 # make debugging the generated assembler easier. If BASE_NAME is
980 # not specified a generic default is used. This proc does not
981 # define the label; see 'define_label'. 'new_label' attempts to
982 # ensure that label names are unique.
983 proc new_label {{base_name label}} {
986 return [_compute_label ${base_name}[incr _label_num]]
989 # Define a label named NAME. Ordinarily, NAME comes from a call
990 # to 'new_label', but this is not required.
991 proc define_label {name} {
995 # A higher-level interface to label handling.
997 # ARGS is a list of label descriptors. Each one is either a
998 # single element, or a list of two elements -- a name and some
999 # text. For each descriptor, 'new_label' is invoked. If the list
1000 # form is used, the second element in the list is passed as an
1001 # argument. The label name is used to define a variable in the
1002 # enclosing scope; this can be used to refer to the label later.
1003 # The label name is also used to define a new proc whose name is
1004 # the label name plus a trailing ":". This proc takes a body as
1005 # an argument and can be used to define the label at that point;
1006 # then the body, if any, is evaluated in the caller's context.
1010 # declare_labels int_label
1011 # something { ... $int_label } ;# refer to the label
1012 # int_label: constant { ... } ;# define the label
1013 proc declare_labels {args} {
1015 set name [lindex $arg 0]
1016 set text [lindex $arg 1]
1018 if { $text == "" } {
1022 upvar $name label_var
1023 set label_var [new_label $text]
1025 proc ${name}: {args} [format {
1032 # Assign elements from LINE to the elements of an array named
1033 # "argvec" in the caller scope. The keys used are named in ARGS.
1034 # If the wrong number of elements appear in LINE, error.
1035 proc _get_args {line op args} {
1036 if {[llength $line] != [llength $args] + 1} {
1037 error "usage: $op [string toupper $args]"
1041 foreach var $args value [lreplace $line 0 0] {
1042 set argvec($var) $value
1046 # This is a miniature assembler for location expressions. It is
1047 # suitable for use in the attributes to a DIE. Its output is
1048 # prefixed with "=" to make it automatically use DW_FORM_block.
1050 # BODY is split by lines, and each line is taken to be a list.
1052 # DWARF_VERSION is the DWARF version for the section where the location
1053 # description is found.
1055 # ADDR_SIZE is the length in bytes (4 or 8) of an address on the target
1056 # machine (typically found in the header of the section where the location
1057 # description is found).
1059 # OFFSET_SIZE is the length in bytes (4 or 8) of an offset into a DWARF
1060 # section. This typically depends on whether 32-bit or 64-bit DWARF is
1061 # used, as indicated in the header of the section where the location
1062 # description is found.
1064 # (FIXME should use 'info complete' here.)
1065 # Each list's first element is the opcode, either short or long
1066 # forms are accepted.
1067 # FIXME argument handling
1069 proc _location { body dwarf_version addr_size offset_size } {
1072 foreach line [split $body \n] {
1073 # Ignore blank lines, and allow embedded comments.
1074 if {[lindex $line 0] == "" || [regexp -- {^[ \t]*#} $line]} {
1077 set opcode [_map_name [lindex $line 0] _OP]
1078 _op .byte $_constants($opcode) $opcode
1080 array unset argvec *
1081 switch -exact -- $opcode {
1083 _get_args $line $opcode size
1084 _op .${addr_size}byte $argvec(size)
1087 DW_OP_GNU_addr_index {
1088 variable _debug_addr_index
1089 variable _cu_addr_size
1091 _op .uleb128 ${_debug_addr_index}
1092 incr _debug_addr_index
1094 _defer_output .debug_addr {
1095 _op .${_cu_addr_size}byte [lindex $line 1]
1100 _get_args $line $opcode register
1101 _op .uleb128 $argvec(register)
1107 _get_args $line $opcode const
1108 _op .byte $argvec(const)
1113 _get_args $line $opcode const
1114 _op .2byte $argvec(const)
1119 _get_args $line $opcode const
1120 _op .4byte $argvec(const)
1125 _get_args $line $opcode const
1126 _op .8byte $argvec(const)
1130 _get_args $line $opcode const
1131 _op .uleb128 $argvec(const)
1134 _get_args $line $opcode const
1135 _op .sleb128 $argvec(const)
1139 _get_args $line $opcode const
1140 _op .uleb128 $argvec(const)
1144 _get_args $line $opcode size
1145 _op .uleb128 $argvec(size)
1149 _get_args $line $opcode size offset
1150 _op .uleb128 $argvec(size)
1151 _op .uleb128 $argvec(offset)
1156 _get_args $line $opcode label
1157 _op .2byte $argvec(label)
1160 DW_OP_implicit_value {
1161 set l1 [new_label "value_start"]
1162 set l2 [new_label "value_end"]
1163 _op .uleb128 "$l2 - $l1"
1165 foreach value [lrange $line 1 end] {
1166 switch -regexp -- $value {
1167 {^0x[[:xdigit:]]{1,2}$} {_op .byte $value}
1168 {^0x[[:xdigit:]]{4}$} {_op .2byte $value}
1169 {^0x[[:xdigit:]]{8}$} {_op .4byte $value}
1170 {^0x[[:xdigit:]]{16}$} {_op .8byte $value}
1172 error "bad value '$value' in DW_OP_implicit_value"
1179 DW_OP_implicit_pointer -
1180 DW_OP_GNU_implicit_pointer {
1181 _get_args $line $opcode label offset
1183 # Here label is a section offset.
1184 if { $dwarf_version == 2 } {
1185 _op .${addr_size}byte $argvec(label)
1187 _op .${offset_size}byte $argvec(label)
1189 _op .sleb128 $argvec(offset)
1192 DW_OP_GNU_variable_value {
1193 _get_args $line $opcode label
1195 # Here label is a section offset.
1196 if { $dwarf_version == 2 } {
1197 _op .${addr_size}byte $argvec(label)
1199 _op .${offset_size}byte $argvec(label)
1204 _get_args $line $opcode size
1205 _op .byte $argvec(size)
1209 _get_args $line $opcode register offset
1210 _op .uleb128 $argvec(register)
1211 _op .sleb128 $argvec(offset)
1215 if {[llength $line] > 1} {
1216 error "Unimplemented: operands in location for $opcode"
1223 # Return a label that references the current position in the
1224 # .debug_addr table. When a user is creating split DWARF they
1225 # will define two CUs, the first will be the split DWARF content,
1226 # and the second will be the non-split stub CU. The split DWARF
1227 # CU fills in the .debug_addr section, but the non-split CU
1228 # includes a reference to the start of the section. The label
1229 # returned by this proc provides that reference.
1230 proc debug_addr_label {} {
1231 variable _debug_addr_index
1233 set lbl [new_label "debug_addr_idx_${_debug_addr_index}_"]
1234 _defer_output .debug_addr {
1241 # OPTIONS is a list with an even number of elements containing
1242 # option-name and option-value pairs.
1243 # Current options are:
1244 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1245 # default = 0 (32-bit)
1246 # version n - DWARF version number to emit
1248 # addr_size n - the size of addresses in bytes: 4, 8, or default
1250 # fission 0|1 - boolean indicating if generating Fission debug info
1252 # BODY is Tcl code that emits the DIEs which make up the body of
1253 # the CU. It is evaluated in the caller's context.
1254 proc cu {options body} {
1257 variable _abbrev_section
1258 variable _abbrev_num
1260 variable _cu_version
1261 variable _cu_addr_size
1262 variable _cu_offset_size
1263 variable _cu_is_fission
1265 # Establish the defaults.
1268 set _cu_addr_size default
1269 set _cu_is_fission 0
1270 set section ".debug_info"
1271 set _abbrev_section ".debug_abbrev"
1273 foreach { name value } $options {
1274 set value [uplevel 1 "subst \"$value\""]
1275 switch -exact -- $name {
1276 is_64 { set is_64 $value }
1277 version { set _cu_version $value }
1278 addr_size { set _cu_addr_size $value }
1279 fission { set _cu_is_fission $value }
1280 default { error "unknown option $name" }
1283 if {$_cu_addr_size == "default"} {
1284 if {[is_64_target]} {
1290 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
1291 if { $_cu_is_fission } {
1292 set section ".debug_info.dwo"
1293 set _abbrev_section ".debug_abbrev.dwo"
1296 if {$_cu_version < 4} {
1297 set _constants(SPECIAL_expr) $_constants(DW_FORM_block)
1299 set _constants(SPECIAL_expr) $_constants(DW_FORM_exprloc)
1304 set cu_num [incr _cu_count]
1305 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
1308 set _cu_label [_compute_label "cu${cu_num}_begin"]
1309 set start_label [_compute_label "cu${cu_num}_start"]
1310 set end_label [_compute_label "cu${cu_num}_end"]
1312 define_label $_cu_label
1314 _op .4byte 0xffffffff
1315 _op .8byte "$end_label - $start_label"
1317 _op .4byte "$end_label - $start_label"
1319 define_label $start_label
1320 _op .2byte $_cu_version Version
1322 # The CU header for DWARF 4 and 5 are slightly different.
1323 if { $_cu_version == 5 } {
1324 _op .byte 0x1 "DW_UT_compile"
1325 _op .byte $_cu_addr_size "Pointer size"
1326 _op .${_cu_offset_size}byte $my_abbrevs Abbrevs
1328 _op .${_cu_offset_size}byte $my_abbrevs Abbrevs
1329 _op .byte $_cu_addr_size "Pointer size"
1332 _defer_output $_abbrev_section {
1333 define_label $my_abbrevs
1338 _defer_output $_abbrev_section {
1339 # Emit the terminator.
1340 _op .byte 0x0 "Abbrev end - Terminator"
1343 define_label $end_label
1347 # OPTIONS is a list with an even number of elements containing
1348 # option-name and option-value pairs.
1349 # Current options are:
1350 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1351 # default = 0 (32-bit)
1352 # version n - DWARF version number to emit
1354 # addr_size n - the size of addresses in bytes: 4, 8, or default
1356 # fission 0|1 - boolean indicating if generating Fission debug info
1358 # SIGNATURE is the 64-bit signature of the type.
1359 # TYPE_LABEL is the label of the type defined by this TU,
1360 # or "" if there is no type (i.e., type stubs in Fission).
1361 # BODY is Tcl code that emits the DIEs which make up the body of
1362 # the TU. It is evaluated in the caller's context.
1363 proc tu {options signature type_label body} {
1365 variable _abbrev_section
1366 variable _abbrev_num
1368 variable _cu_version
1369 variable _cu_addr_size
1370 variable _cu_offset_size
1371 variable _cu_is_fission
1373 # Establish the defaults.
1376 set _cu_addr_size default
1377 set _cu_is_fission 0
1378 set section ".debug_types"
1379 set _abbrev_section ".debug_abbrev"
1381 foreach { name value } $options {
1382 switch -exact -- $name {
1383 is_64 { set is_64 $value }
1384 version { set _cu_version $value }
1385 addr_size { set _cu_addr_size $value }
1386 fission { set _cu_is_fission $value }
1387 default { error "unknown option $name" }
1390 if {$_cu_addr_size == "default"} {
1391 if {[is_64_target]} {
1397 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
1398 if { $_cu_is_fission } {
1399 set section ".debug_types.dwo"
1400 set _abbrev_section ".debug_abbrev.dwo"
1405 set cu_num [incr _cu_count]
1406 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
1409 set _cu_label [_compute_label "cu${cu_num}_begin"]
1410 set start_label [_compute_label "cu${cu_num}_start"]
1411 set end_label [_compute_label "cu${cu_num}_end"]
1413 define_label $_cu_label
1415 _op .4byte 0xffffffff
1416 _op .8byte "$end_label - $start_label"
1418 _op .4byte "$end_label - $start_label"
1420 define_label $start_label
1421 _op .2byte $_cu_version Version
1422 _op .${_cu_offset_size}byte $my_abbrevs Abbrevs
1423 _op .byte $_cu_addr_size "Pointer size"
1424 _op .8byte $signature Signature
1425 if { $type_label != "" } {
1426 uplevel declare_labels $type_label
1427 upvar $type_label my_type_label
1429 _op .8byte "$my_type_label - $_cu_label"
1431 _op .4byte "$my_type_label - $_cu_label"
1441 _defer_output $_abbrev_section {
1442 define_label $my_abbrevs
1447 _defer_output $_abbrev_section {
1448 # Emit the terminator.
1449 _op .byte 0x0 "Abbrev end - Terminator"
1452 define_label $end_label
1455 # Emit a DWARF .debug_ranges unit.
1456 # OPTIONS is a list with an even number of elements containing
1457 # option-name and option-value pairs.
1458 # Current options are:
1459 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1460 # default = 0 (32-bit)
1462 # BODY is Tcl code that emits the content of the .debug_ranges
1463 # unit, it is evaluated in the caller's context.
1464 proc ranges {options body} {
1465 variable _debug_ranges_64_bit
1467 foreach { name value } $options {
1468 switch -exact -- $name {
1469 is_64 { set _debug_ranges_64_bit [subst $value] }
1470 default { error "unknown option $name" }
1474 set section ".debug_ranges"
1477 proc sequence { body } {
1478 variable _debug_ranges_64_bit
1480 # Emit the sequence of addresses.
1482 proc base { addr } {
1483 variable _debug_ranges_64_bit
1485 if { $_debug_ranges_64_bit } then {
1486 _op .8byte 0xffffffffffffffff "Base Marker"
1487 _op .8byte $addr "Base Address"
1489 _op .4byte 0xffffffff "Base Marker"
1490 _op .4byte $addr "Base Address"
1494 proc range { start end } {
1495 variable _debug_ranges_64_bit
1497 if { $_debug_ranges_64_bit } then {
1498 _op .8byte $start "Start Address"
1499 _op .8byte $end "End Address"
1501 _op .4byte $start "Start Address"
1502 _op .4byte $end "End Address"
1508 # End of the sequence.
1509 if { $_debug_ranges_64_bit } then {
1510 _op .8byte 0x0 "End of Sequence Marker (Part 1)"
1511 _op .8byte 0x0 "End of Sequence Marker (Part 2)"
1513 _op .4byte 0x0 "End of Sequence Marker (Part 1)"
1514 _op .4byte 0x0 "End of Sequence Marker (Part 2)"
1521 # Emit a DWARF .debug_rnglists section.
1523 # The target address size is based on the current target's address size.
1525 # There is one mandatory positional argument, BODY, which must be Tcl code
1526 # that emits the content of the section. It is evaluated in the caller's
1529 # The following option can be used:
1531 # - -is-64 true|false: Whether to use 64-bit DWARF instead of 32-bit DWARF.
1532 # The default is 32-bit.
1534 proc rnglists { args } {
1535 variable _debug_rnglists_addr_size
1536 variable _debug_rnglists_offset_size
1537 variable _debug_rnglists_is_64_dwarf
1539 parse_args {{"is-64" "false"}}
1541 if { [llength $args] != 1 } {
1542 error "rnglists proc expects one positional argument (body)"
1548 set _debug_rnglists_addr_size 8
1550 set _debug_rnglists_addr_size 4
1554 set _debug_rnglists_offset_size 8
1555 set _debug_rnglists_is_64_dwarf true
1557 set _debug_rnglists_offset_size 4
1558 set _debug_rnglists_is_64_dwarf false
1561 _section ".debug_rnglists"
1563 # Count of tables in the section.
1564 variable _debug_rnglists_table_count 0
1566 # Compute the label name for list at index LIST_IDX, for the current
1569 proc _compute_list_label { list_idx } {
1570 variable _debug_rnglists_table_count
1572 return ".Lrnglists_table_${_debug_rnglists_table_count}_list_${list_idx}"
1575 # Generate one table (header + offset array + range lists).
1577 # Accepts one positional argument, BODY. BODY may call the LIST_
1578 # procedure to generate rnglists.
1580 # The -post-header-label option can be used to define a label just after
1581 # the header of the table. This is the label that a DW_AT_rnglists_base
1582 # attribute will usually refer to.
1584 # The `-with-offset-array true|false` option can be used to control
1585 # whether the headers of the location list tables have an array of
1586 # offset. The default is true.
1588 proc table { args } {
1589 variable _debug_rnglists_table_count
1590 variable _debug_rnglists_addr_size
1591 variable _debug_rnglists_offset_size
1592 variable _debug_rnglists_is_64_dwarf
1595 {post-header-label ""}
1596 {with-offset-array true}
1599 if { [llength $args] != 1 } {
1600 error "table proc expects one positional argument (body)"
1605 # Generate one range list.
1607 # BODY may call the various procs defined below to generate list entries.
1608 # They correspond to the range list entry kinds described in section 2.17.3
1609 # of the DWARF 5 spec.
1611 # To define a label pointing to the beginning of the list, use
1612 # the conventional way of declaring and defining labels:
1614 # declare_labels the_list
1620 proc list_ { body } {
1621 variable _debug_rnglists_list_count
1623 # Define a label for this list. It is used to build the offset
1625 set list_label [_compute_list_label $_debug_rnglists_list_count]
1626 define_label $list_label
1628 # Emit a DW_RLE_start_end entry.
1630 proc start_end { start end } {
1631 variable _debug_rnglists_addr_size
1633 _op .byte 0x06 "DW_RLE_start_end"
1634 _op .${_debug_rnglists_addr_size}byte $start "start"
1635 _op .${_debug_rnglists_addr_size}byte $end "end"
1641 _op .byte 0x00 "DW_RLE_end_of_list"
1643 incr _debug_rnglists_list_count
1646 # Count of lists in the table.
1647 variable _debug_rnglists_list_count 0
1649 # Generate the lists ops first, because we need to know how many
1650 # lists there are to generate the header and offset table.
1651 set lists_ops [_defer_to_string {
1655 set post_unit_len_label \
1656 [_compute_label "rnglists_table_${_debug_rnglists_table_count}_post_unit_len"]
1657 set post_header_label \
1658 [_compute_label "rnglists_table_${_debug_rnglists_table_count}_post_header"]
1659 set table_end_label \
1660 [_compute_label "rnglists_table_${_debug_rnglists_table_count}_end"]
1662 # Emit the table header.
1663 if { $_debug_rnglists_is_64_dwarf } {
1664 _op .4byte 0xffffffff "unit length 1/2"
1665 _op .8byte "$table_end_label - $post_unit_len_label" "unit length 2/2"
1667 _op .4byte "$table_end_label - $post_unit_len_label" "unit length"
1670 define_label $post_unit_len_label
1672 _op .2byte 5 "dwarf version"
1673 _op .byte $_debug_rnglists_addr_size "address size"
1674 _op .byte 0 "segment selector size"
1676 if { ${with-offset-array} } {
1677 _op .4byte "$_debug_rnglists_list_count" "offset entry count"
1679 _op .4byte 0 "offset entry count"
1682 define_label $post_header_label
1684 # Define the user post-header label, if provided.
1685 if { ${post-header-label} != "" } {
1686 define_label ${post-header-label}
1689 # Emit the offset array.
1690 if { ${with-offset-array} } {
1691 for {set list_idx 0} {$list_idx < $_debug_rnglists_list_count} {incr list_idx} {
1692 set list_label [_compute_list_label $list_idx]
1693 _op .${_debug_rnglists_offset_size}byte "$list_label - $post_header_label" "offset of list $list_idx"
1697 # Emit the actual list data.
1700 define_label $table_end_label
1702 incr _debug_rnglists_table_count
1708 # Emit a DWARF .debug_loclists section.
1710 # The target address size is based on the current target's address size.
1712 # There is one mandatory positional argument, BODY, which must be Tcl code
1713 # that emits the content of the section. It is evaluated in the caller's
1716 # The following option can be used:
1718 # - -is-64 true|false: Whether to use 64-bit DWARF instead of 32-bit DWARF.
1719 # The default is 32-bit.
1721 proc loclists { args } {
1722 variable _debug_loclists_addr_size
1723 variable _debug_loclists_offset_size
1724 variable _debug_loclists_is_64_dwarf
1726 parse_args {{"is-64" "false"}}
1728 if { [llength $args] != 1 } {
1729 error "loclists proc expects one positional argument (body)"
1735 set _debug_loclists_addr_size 8
1737 set _debug_loclists_addr_size 4
1741 set _debug_loclists_offset_size 8
1742 set _debug_loclists_is_64_dwarf true
1744 set _debug_loclists_offset_size 4
1745 set _debug_loclists_is_64_dwarf false
1748 _section ".debug_loclists"
1750 # Count of tables in the section.
1751 variable _debug_loclists_table_count 0
1753 # Compute the label name for list at index LIST_IDX, for the current
1756 proc _compute_list_label { list_idx } {
1757 variable _debug_loclists_table_count
1759 return ".Lloclists_table_${_debug_loclists_table_count}_list_${list_idx}"
1762 # Generate one table (header + offset array + location lists).
1764 # Accepts one position argument, BODY. BODY may call the LIST_
1765 # procedure to generate loclists.
1767 # The -post-header-label option can be used to define a label just after the
1768 # header of the table. This is the label that a DW_AT_loclists_base
1769 # attribute will usually refer to.
1771 # The `-with-offset-array true|false` option can be used to control
1772 # whether the headers of the location list tables have an array of
1773 # offset. The default is true.
1775 proc table { args } {
1776 variable _debug_loclists_table_count
1777 variable _debug_loclists_addr_size
1778 variable _debug_loclists_offset_size
1779 variable _debug_loclists_is_64_dwarf
1782 {post-header-label ""}
1783 {with-offset-array true}
1786 if { [llength $args] != 1 } {
1787 error "table proc expects one positional argument (body)"
1792 # Generate one location list.
1794 # BODY may call the various procs defined below to generate list
1795 # entries. They correspond to the location list entry kinds
1796 # described in section 2.6.2 of the DWARF 5 spec.
1798 # To define a label pointing to the beginning of the list, use
1799 # the conventional way of declaring and defining labels:
1801 # declare_labels the_list
1807 proc list_ { body } {
1808 variable _debug_loclists_list_count
1810 # Count the location descriptions in this list.
1811 variable _debug_loclists_locdesc_count 0
1813 # Define a label for this list. It is used to build the offset
1815 set list_label [_compute_list_label $_debug_loclists_list_count]
1816 define_label $list_label
1818 # Emit a DW_LLE_start_length entry.
1820 proc start_length { start length locdesc } {
1821 variable _debug_loclists_is_64_dwarf
1822 variable _debug_loclists_addr_size
1823 variable _debug_loclists_offset_size
1824 variable _debug_loclists_table_count
1825 variable _debug_loclists_list_count
1826 variable _debug_loclists_locdesc_count
1828 _op .byte 0x08 "DW_LLE_start_length"
1830 # Start and end of the address range.
1831 _op .${_debug_loclists_addr_size}byte $start "start"
1832 _op .uleb128 $length "length"
1834 # Length of location description.
1835 set locdesc_start_label ".Lloclists_table_${_debug_loclists_table_count}_list_${_debug_loclists_list_count}_locdesc_${_debug_loclists_locdesc_count}_start"
1836 set locdesc_end_label ".Lloclists_table_${_debug_loclists_table_count}_list_${_debug_loclists_list_count}_locdesc_${_debug_loclists_locdesc_count}_end"
1837 _op .uleb128 "$locdesc_end_label - $locdesc_start_label" "locdesc length"
1839 define_label $locdesc_start_label
1841 _location $locdesc $dwarf_version $_debug_loclists_addr_size $_debug_loclists_offset_size
1842 define_label $locdesc_end_label
1844 incr _debug_loclists_locdesc_count
1850 _op .byte 0x00 "DW_LLE_end_of_list"
1852 incr _debug_loclists_list_count
1855 # Count of lists in the table.
1856 variable _debug_loclists_list_count 0
1858 # Generate the lists ops first, because we need to know how many
1859 # lists there are to generate the header and offset table.
1860 set lists_ops [_defer_to_string {
1864 set post_unit_len_label \
1865 [_compute_label "loclists_table_${_debug_loclists_table_count}_post_unit_len"]
1866 set post_header_label \
1867 [_compute_label "loclists_table_${_debug_loclists_table_count}_post_header"]
1868 set table_end_label \
1869 [_compute_label "loclists_table_${_debug_loclists_table_count}_end"]
1871 # Emit the table header.
1872 if { $_debug_loclists_is_64_dwarf } {
1873 _op .4byte 0xffffffff "unit length 1/2"
1874 _op .8byte "$table_end_label - $post_unit_len_label" "unit length 2/2"
1876 _op .4byte "$table_end_label - $post_unit_len_label" "unit length"
1879 define_label $post_unit_len_label
1881 _op .2byte 5 "DWARF version"
1882 _op .byte $_debug_loclists_addr_size "address size"
1883 _op .byte 0 "segment selector size"
1885 if { ${with-offset-array} } {
1886 _op .4byte "$_debug_loclists_list_count" "offset entry count"
1888 _op .4byte 0 "offset entry count"
1891 define_label $post_header_label
1893 # Define the user post-header label, if provided.
1894 if { ${post-header-label} != "" } {
1895 define_label ${post-header-label}
1898 # Emit the offset array.
1899 if { ${with-offset-array} } {
1900 for {set list_idx 0} {$list_idx < $_debug_loclists_list_count} {incr list_idx} {
1901 set list_label [_compute_list_label $list_idx]
1902 _op .${_debug_loclists_offset_size}byte "$list_label - $post_header_label" "offset of list $list_idx"
1906 # Emit the actual list data.
1909 define_label $table_end_label
1911 incr _debug_loclists_table_count
1917 # Emit a DWARF .debug_line unit.
1918 # OPTIONS is a list with an even number of elements containing
1919 # option-name and option-value pairs.
1920 # Current options are:
1921 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1922 # default = 0 (32-bit)
1923 # version n - DWARF version number to emit
1925 # addr_size n - the size of addresses in bytes: 4, 8, or default
1928 # LABEL is the label of the current unit (which is probably
1929 # referenced by a DW_AT_stmt_list), or "" if there is no such
1932 # BODY is Tcl code that emits the parts which make up the body of
1933 # the line unit. It is evaluated in the caller's context. The
1934 # following commands are available for the BODY section:
1936 # include_dir "dirname" -- adds a new include directory
1938 # file_name "file.c" idx -- adds a new file name. IDX is a
1939 # 1-based index referencing an include directory or 0 for
1940 # current directory.
1942 proc lines {options label body} {
1943 variable _line_count
1944 variable _line_saw_file
1945 variable _line_saw_program
1946 variable _line_header_end_label
1948 # Establish the defaults.
1951 set _unit_addr_size default
1952 set _line_saw_program 0
1953 set _line_saw_file 0
1954 set _default_is_stmt 1
1956 foreach { name value } $options {
1957 switch -exact -- $name {
1958 is_64 { set is_64 $value }
1959 version { set _unit_version $value }
1960 addr_size { set _unit_addr_size $value }
1961 default_is_stmt { set _default_is_stmt $value }
1962 default { error "unknown option $name" }
1965 if {$_unit_addr_size == "default"} {
1966 if {[is_64_target]} {
1967 set _unit_addr_size 8
1969 set _unit_addr_size 4
1973 set unit_num [incr _line_count]
1975 set section ".debug_line"
1978 if { "$label" != "" } {
1979 # Define the user-provided label at this point.
1983 set unit_len_label [_compute_label "line${_line_count}_start"]
1984 set unit_end_label [_compute_label "line${_line_count}_end"]
1985 set header_len_label [_compute_label "line${_line_count}_header_start"]
1986 set _line_header_end_label [_compute_label "line${_line_count}_header_end"]
1989 _op .4byte 0xffffffff
1990 _op .8byte "$unit_end_label - $unit_len_label" "unit_length"
1992 _op .4byte "$unit_end_label - $unit_len_label" "unit_length"
1995 define_label $unit_len_label
1997 _op .2byte $_unit_version version
2000 _op .8byte "$_line_header_end_label - $header_len_label" "header_length"
2002 _op .4byte "$_line_header_end_label - $header_len_label" "header_length"
2005 define_label $header_len_label
2007 _op .byte 1 "minimum_instruction_length"
2008 _op .byte $_default_is_stmt "default_is_stmt"
2009 _op .byte 1 "line_base"
2010 _op .byte 1 "line_range"
2011 _op .byte 10 "opcode_base"
2013 # The standard_opcode_lengths table. The number of arguments
2014 # for each of the standard opcodes. Generating 9 entries here
2015 # matches the use of 10 in the opcode_base above. These 9
2016 # entries match the 9 standard opcodes for DWARF2, making use
2017 # of only 9 should be fine, even if we are generating DWARF3
2019 _op .byte 0 "standard opcode 1"
2020 _op .byte 1 "standard opcode 2"
2021 _op .byte 1 "standard opcode 3"
2022 _op .byte 1 "standard opcode 4"
2023 _op .byte 1 "standard opcode 5"
2024 _op .byte 0 "standard opcode 6"
2025 _op .byte 0 "standard opcode 7"
2026 _op .byte 0 "standard opcode 8"
2027 _op .byte 1 "standard opcode 9"
2029 proc include_dir {dirname} {
2030 _op .ascii [_quote $dirname]
2033 proc file_name {filename diridx} {
2034 variable _line_saw_file
2035 if "! $_line_saw_file" {
2036 # Terminate the dir list.
2037 _op .byte 0 "Terminator."
2038 set _line_saw_file 1
2041 _op .ascii [_quote $filename]
2042 _op .sleb128 $diridx
2043 _op .sleb128 0 "mtime"
2044 _op .sleb128 0 "length"
2047 proc program {statements} {
2048 variable _line_saw_program
2049 variable _line_header_end_label
2054 if "! $_line_saw_program" {
2055 # Terminate the file list.
2056 _op .byte 0 "Terminator."
2057 define_label $_line_header_end_label
2058 set _line_saw_program 1
2061 proc DW_LNE_set_address {addr} {
2063 set start [new_label "set_address_start"]
2064 set end [new_label "set_address_end"]
2065 _op .uleb128 "${end} - ${start}"
2066 define_label ${start}
2068 if {[is_64_target]} {
2076 proc DW_LNE_end_sequence {} {
2084 proc DW_LNE_user { len opcode } {
2085 set DW_LNE_lo_usr 0x80
2086 set DW_LNE_hi_usr 0xff
2087 if { $DW_LNE_lo_usr <= $opcode
2088 && $opcode <= $DW_LNE_hi_usr } {
2092 for {set i 1} {$i < $len} {incr i} {
2096 error "unknown vendor specific extended opcode: $opcode"
2100 proc DW_LNS_copy {} {
2104 proc DW_LNS_negate_stmt {} {
2108 proc DW_LNS_advance_pc {offset} {
2110 _op .uleb128 ${offset}
2113 proc DW_LNS_advance_line {offset} {
2116 _op .sleb128 ${offset}
2117 set _line [expr $_line + $offset]
2120 # A pseudo line number program instruction, that can be used instead
2121 # of DW_LNS_advance_line. Rather than writing:
2122 # {DW_LNS_advance_line [expr $line1 - 1]}
2123 # {DW_LNS_advance_line [expr $line2 - $line1]}
2124 # {DW_LNS_advance_line [expr $line3 - $line2]}
2125 # we can just write:
2131 set offset [expr $line - $_line]
2132 DW_LNS_advance_line $offset
2135 proc DW_LNS_set_file {num} {
2140 foreach statement $statements {
2141 uplevel 1 $statement
2147 rename include_dir ""
2150 # Terminate dir list if we saw no files.
2151 if "! $_line_saw_file" {
2152 _op .byte 0 "Terminator."
2155 # Terminate the file list.
2156 if "! $_line_saw_program" {
2157 _op .byte 0 "Terminator."
2158 define_label $_line_header_end_label
2161 define_label $unit_end_label
2164 proc _empty_array {name} {
2165 upvar $name the_array
2167 catch {unset the_array}
2172 # Emit a .gnu_debugaltlink section with the given file name and
2173 # build-id. The buildid should be represented as a hexadecimal
2174 # string, like "ffeeddcc".
2175 proc gnu_debugaltlink {filename buildid} {
2176 _defer_output .gnu_debugaltlink {
2177 _op .ascii [_quote $filename]
2178 foreach {a b} [split $buildid {}] {
2184 proc _note {type name hexdata} {
2185 set namelen [expr [string length $name] + 1]
2190 _op .4byte [expr [string length $hexdata] / 2]
2194 _op .ascii [_quote $name]
2197 set total [expr {($namelen + (1 << $align) - 1) & -(1 << $align)}]
2198 for {set i $namelen} {$i < $total} {incr i} {
2202 foreach {a b} [split $hexdata {}] {
2207 # Emit a note section holding the given build-id.
2208 proc build_id {buildid} {
2209 _defer_output {.note.gnu.build-id a note} {
2210 # From elf/common.h.
2211 set NT_GNU_BUILD_ID 3
2213 _note $NT_GNU_BUILD_ID GNU $buildid
2217 # The top-level interface to the DWARF assembler.
2218 # FILENAME is the name of the file where the generated assembly
2220 # BODY is Tcl code to emit the assembly. It is evaluated via
2221 # "eval" -- not uplevel as you might expect, because it is
2222 # important to run the body in the Dwarf namespace.
2224 # A typical invocation is something like:
2225 # Dwarf::assemble $file {
2235 proc assemble {filename body} {
2236 variable _initialized
2237 variable _output_file
2238 variable _deferred_output
2243 variable _line_count
2244 variable _line_saw_file
2245 variable _line_saw_program
2246 variable _line_header_end_label
2247 variable _debug_ranges_64_bit
2248 variable _debug_addr_index
2250 if {!$_initialized} {
2255 set _output_file [open $filename w]
2257 _empty_array _deferred_output
2260 _empty_array _strings
2263 set _line_saw_file 0
2264 set _line_saw_program 0
2265 set _debug_ranges_64_bit [is_64_target]
2267 set _debug_addr_index 0
2269 # Not "uplevel" here, because we want to evaluate in this
2270 # namespace. This is somewhat bad because it means we can't
2271 # readily refer to outer variables.
2274 _write_deferred_output
2276 catch {close $_output_file}