Home | History | Annotate | Line # | Download | only in lib
dwarf.exp revision 1.8
      1  1.8  christos # Copyright 2010-2019 Free Software Foundation, Inc.
      2  1.1  christos 
      3  1.1  christos # This program is free software; you can redistribute it and/or modify
      4  1.1  christos # it under the terms of the GNU General Public License as published by
      5  1.1  christos # the Free Software Foundation; either version 3 of the License, or
      6  1.1  christos # (at your option) any later version.
      7  1.1  christos #
      8  1.1  christos # This program is distributed in the hope that it will be useful,
      9  1.1  christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  1.1  christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  1.1  christos # GNU General Public License for more details.
     12  1.1  christos #
     13  1.1  christos # You should have received a copy of the GNU General Public License
     14  1.1  christos # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15  1.1  christos 
     16  1.1  christos # Return true if the target supports DWARF-2 and uses gas.
     17  1.1  christos # For now pick a sampling of likely targets.
     18  1.1  christos proc dwarf2_support {} {
     19  1.1  christos     if {[istarget *-*-linux*]
     20  1.1  christos 	|| [istarget *-*-gnu*]
     21  1.1  christos 	|| [istarget *-*-elf*]
     22  1.1  christos 	|| [istarget *-*-openbsd*]
     23  1.1  christos 	|| [istarget arm*-*-eabi*]
     24  1.1  christos 	|| [istarget arm*-*-symbianelf*]
     25  1.1  christos 	|| [istarget powerpc-*-eabi*]} {
     26  1.1  christos 	return 1
     27  1.1  christos     }
     28  1.1  christos 
     29  1.1  christos     return 0
     30  1.1  christos }
     31  1.1  christos 
     32  1.1  christos # Build an executable from a fission-based .S file.
     33  1.1  christos # This handles the extra work of splitting the .o into non-dwo and dwo
     34  1.1  christos # pieces, making sure the .dwo is available if we're using cc-with-tweaks.sh
     35  1.1  christos # to build a .dwp file.
     36  1.1  christos # The arguments and results are the same as for build_executable.
     37  1.1  christos #
     38  1.1  christos # Current restrictions:
     39  1.1  christos # - only supports one source file
     40  1.1  christos # - cannot be run on remote hosts
     41  1.1  christos 
     42  1.1  christos proc build_executable_from_fission_assembler { testname executable sources options } {
     43  1.1  christos     verbose -log "build_executable_from_fission_assembler $testname $executable $sources $options"
     44  1.1  christos     if { [llength $sources] != 1 } {
     45  1.1  christos 	error "Only one source file supported."
     46  1.1  christos     }
     47  1.1  christos     if [is_remote host] {
     48  1.1  christos 	error "Remote hosts are not supported."
     49  1.1  christos     }
     50  1.1  christos 
     51  1.1  christos     global srcdir subdir
     52  1.1  christos     set source_file ${srcdir}/${subdir}/${sources}
     53  1.1  christos     set root_name [file rootname [file tail $source_file]]
     54  1.1  christos     set output_base [standard_output_file $root_name]
     55  1.1  christos     set object_file ${output_base}.o
     56  1.1  christos     set dwo_file ${output_base}.dwo
     57  1.1  christos     set object_options "object $options"
     58  1.1  christos     set objcopy [gdb_find_objcopy]
     59  1.1  christos 
     60  1.1  christos     set result [gdb_compile $source_file $object_file object $options]
     61  1.1  christos     if { "$result" != "" } {
     62  1.1  christos 	return -1
     63  1.1  christos     }
     64  1.1  christos 
     65  1.1  christos     set command "$objcopy --extract-dwo $object_file $dwo_file"
     66  1.1  christos     verbose -log "Executing $command"
     67  1.1  christos     set result [catch "exec $command" output]
     68  1.1  christos     verbose -log "objcopy --extract-dwo output: $output"
     69  1.1  christos     if { $result == 1 } {
     70  1.1  christos 	return -1
     71  1.1  christos     }
     72  1.1  christos 
     73  1.1  christos     set command "$objcopy --strip-dwo $object_file"
     74  1.1  christos     verbose -log "Executing $command"
     75  1.1  christos     set result [catch "exec $command" output]
     76  1.1  christos     verbose -log "objcopy --strip-dwo output: $output"
     77  1.1  christos     if { $result == 1 } {
     78  1.1  christos 	return -1
     79  1.1  christos     }
     80  1.1  christos 
     81  1.6  christos     set result [gdb_compile $object_file $executable executable $options]
     82  1.1  christos     if { "$result" != "" } {
     83  1.1  christos 	return -1
     84  1.1  christos     }
     85  1.1  christos 
     86  1.1  christos     return 0
     87  1.1  christos }
     88  1.1  christos 
     89  1.3  christos # Return a list of expressions about function FUNC's address and length.
     90  1.3  christos # The first expression is the address of function FUNC, and the second
     91  1.3  christos # one is FUNC's length.  SRC is the source file having function FUNC.
     92  1.3  christos # An internal label ${func}_label must be defined inside FUNC:
     93  1.3  christos #
     94  1.3  christos #  int main (void)
     95  1.3  christos #  {
     96  1.3  christos #    asm ("main_label: .globl main_label");
     97  1.3  christos #    return 0;
     98  1.3  christos #  }
     99  1.3  christos #
    100  1.3  christos # This label is needed to compute the start address of function FUNC.
    101  1.3  christos # If the compiler is gcc, we can do the following to get function start
    102  1.3  christos # and end address too:
    103  1.3  christos #
    104  1.3  christos # asm ("func_start: .globl func_start");
    105  1.3  christos # static void func (void) {}
    106  1.3  christos # asm ("func_end: .globl func_end");
    107  1.3  christos #
    108  1.3  christos # however, this isn't portable, because other compilers, such as clang,
    109  1.3  christos # may not guarantee the order of global asms and function.  The code
    110  1.3  christos # becomes:
    111  1.3  christos #
    112  1.3  christos # asm ("func_start: .globl func_start");
    113  1.3  christos # asm ("func_end: .globl func_end");
    114  1.3  christos # static void func (void) {}
    115  1.3  christos #
    116  1.3  christos 
    117  1.3  christos proc function_range { func src } {
    118  1.3  christos     global decimal gdb_prompt
    119  1.3  christos 
    120  1.3  christos     set exe [standard_temp_file func_addr[pid].x]
    121  1.3  christos 
    122  1.3  christos     gdb_compile $src $exe executable {debug}
    123  1.3  christos 
    124  1.3  christos     gdb_exit
    125  1.3  christos     gdb_start
    126  1.3  christos     gdb_load "$exe"
    127  1.3  christos 
    128  1.3  christos     # Compute the label offset, and we can get the function start address
    129  1.3  christos     # by "${func}_label - $func_label_offset".
    130  1.3  christos     set func_label_offset ""
    131  1.3  christos     set test "p ${func}_label - ${func}"
    132  1.3  christos     gdb_test_multiple $test $test {
    133  1.3  christos 	-re ".* = ($decimal)\r\n$gdb_prompt $" {
    134  1.3  christos 	    set func_label_offset $expect_out(1,string)
    135  1.3  christos 	}
    136  1.3  christos     }
    137  1.3  christos 
    138  1.3  christos     # Compute the function length.
    139  1.3  christos     global hex
    140  1.3  christos     set func_length ""
    141  1.3  christos     set test "disassemble $func"
    142  1.3  christos     gdb_test_multiple $test $test {
    143  1.3  christos 	-re ".*$hex <\\+($decimal)>:\[^\r\n\]+\r\nEnd of assembler dump\.\r\n$gdb_prompt $" {
    144  1.3  christos 	    set func_length $expect_out(1,string)
    145  1.3  christos 	}
    146  1.3  christos     }
    147  1.3  christos 
    148  1.3  christos     # Compute the size of the last instruction.
    149  1.5  christos     if { $func_length == 0 } then {
    150  1.5  christos 	set func_pattern "$func"
    151  1.5  christos     } else {
    152  1.5  christos 	set func_pattern "$func\\+$func_length"
    153  1.5  christos     }
    154  1.3  christos     set test "x/2i $func+$func_length"
    155  1.3  christos     gdb_test_multiple $test $test {
    156  1.5  christos 	-re ".*($hex) <$func_pattern>:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
    157  1.3  christos 	    set start $expect_out(1,string)
    158  1.3  christos 	    set end $expect_out(2,string)
    159  1.3  christos 
    160  1.3  christos 	    set func_length [expr $func_length + $end - $start]
    161  1.3  christos 	}
    162  1.3  christos     }
    163  1.3  christos 
    164  1.3  christos     return [list "${func}_label - $func_label_offset" $func_length]
    165  1.3  christos }
    166  1.3  christos 
    167  1.1  christos # A DWARF assembler.
    168  1.1  christos #
    169  1.1  christos # All the variables in this namespace are private to the
    170  1.1  christos # implementation.  Also, any procedure whose name starts with "_" is
    171  1.1  christos # private as well.  Do not use these.
    172  1.1  christos #
    173  1.1  christos # Exported functions are documented at their definition.
    174  1.1  christos #
    175  1.1  christos # In addition to the hand-written functions documented below, this
    176  1.1  christos # module automatically generates a function for each DWARF tag.  For
    177  1.1  christos # most tags, two forms are made: a full name, and one with the
    178  1.1  christos # "DW_TAG_" prefix stripped.  For example, you can use either
    179  1.1  christos # 'DW_TAG_compile_unit' or 'compile_unit' interchangeably.
    180  1.1  christos #
    181  1.1  christos # There are two exceptions to this rule: DW_TAG_variable and
    182  1.1  christos # DW_TAG_namespace.  For these, the full name must always be used,
    183  1.1  christos # as the short name conflicts with Tcl builtins.  (Should future
    184  1.1  christos # versions of Tcl or DWARF add more conflicts, this list will grow.
    185  1.1  christos # If you want to be safe you should always use the full names.)
    186  1.1  christos #
    187  1.1  christos # Each tag procedure is defined like:
    188  1.1  christos #
    189  1.1  christos # proc DW_TAG_mumble {{attrs {}} {children {}}} { ... }
    190  1.1  christos #
    191  1.1  christos # ATTRS is an optional list of attributes.
    192  1.1  christos # It is run through 'subst' in the caller's context before processing.
    193  1.1  christos #
    194  1.1  christos # Each attribute in the list has one of two forms:
    195  1.1  christos #   1. { NAME VALUE }
    196  1.1  christos #   2. { NAME VALUE FORM }
    197  1.1  christos #
    198  1.1  christos # In each case, NAME is the attribute's name.
    199  1.1  christos # This can either be the full name, like 'DW_AT_name', or a shortened
    200  1.1  christos # name, like 'name'.  These are fully equivalent.
    201  1.1  christos #
    202  1.3  christos # Besides DWARF standard attributes, assembler supports 'macro' attribute
    203  1.3  christos # which will be substituted by one or more standard or macro attributes.
    204  1.3  christos # supported macro attributes are:
    205  1.3  christos #
    206  1.3  christos #  - MACRO_AT_range { FUNC FILE }
    207  1.3  christos #  It is substituted by DW_AT_low_pc and DW_AT_high_pc with the start and
    208  1.3  christos #  end address of function FUNC in file FILE.
    209  1.3  christos #
    210  1.3  christos #  - MACRO_AT_func { FUNC FILE }
    211  1.3  christos #  It is substituted by DW_AT_name with FUNC and MACRO_AT_range.
    212  1.3  christos #
    213  1.1  christos # If FORM is given, it should name a DW_FORM_ constant.
    214  1.1  christos # This can either be the short form, like 'DW_FORM_addr', or a
    215  1.1  christos # shortened version, like 'addr'.  If the form is given, VALUE
    216  1.1  christos # is its value; see below.  In some cases, additional processing
    217  1.1  christos # is done; for example, DW_FORM_strp manages the .debug_str
    218  1.1  christos # section automatically.
    219  1.1  christos #
    220  1.1  christos # If FORM is 'SPECIAL_expr', then VALUE is treated as a location
    221  1.1  christos # expression.  The effective form is then DW_FORM_block, and VALUE
    222  1.1  christos # is passed to the (internal) '_location' proc to be translated.
    223  1.1  christos # This proc implements a miniature DW_OP_ assembler.
    224  1.1  christos #
    225  1.1  christos # If FORM is not given, it is guessed:
    226  1.1  christos # * If VALUE starts with the "@" character, the rest of VALUE is
    227  1.1  christos #   looked up as a DWARF constant, and DW_FORM_sdata is used.  For
    228  1.1  christos #   example, '@DW_LANG_c89' could be used.
    229  1.1  christos # * If VALUE starts with the ":" character, then it is a label
    230  1.1  christos #   reference.  The rest of VALUE is taken to be the name of a label,
    231  1.1  christos #   and DW_FORM_ref4 is used.  See 'new_label' and 'define_label'.
    232  1.7  christos # * If VALUE starts with the "%" character, then it is a label
    233  1.7  christos #   reference too, but DW_FORM_ref_addr is used.
    234  1.1  christos # * Otherwise, VALUE is taken to be a string and DW_FORM_string is
    235  1.3  christos #   used.  In order to prevent bugs where a numeric value is given but
    236  1.3  christos #   no form is specified, it is an error if the value looks like a number
    237  1.3  christos #   (using Tcl's "string is integer") and no form is provided.
    238  1.1  christos # More form-guessing functionality may be added.
    239  1.1  christos #
    240  1.1  christos # CHILDREN is just Tcl code that can be used to define child DIEs.  It
    241  1.1  christos # is evaluated in the caller's context.
    242  1.1  christos #
    243  1.1  christos # Currently this code is missing nice support for CFA handling, and
    244  1.1  christos # probably other things as well.
    245  1.1  christos 
    246  1.1  christos namespace eval Dwarf {
    247  1.1  christos     # True if the module has been initialized.
    248  1.1  christos     variable _initialized 0
    249  1.1  christos 
    250  1.1  christos     # Constants from dwarf2.h.
    251  1.1  christos     variable _constants
    252  1.1  christos     # DW_AT short names.
    253  1.1  christos     variable _AT
    254  1.1  christos     # DW_FORM short names.
    255  1.1  christos     variable _FORM
    256  1.1  christos     # DW_OP short names.
    257  1.1  christos     variable _OP
    258  1.1  christos 
    259  1.1  christos     # The current output file.
    260  1.1  christos     variable _output_file
    261  1.1  christos 
    262  1.1  christos     # Note: The _cu_ values here also apply to type units (TUs).
    263  1.1  christos     # Think of a TU as a special kind of CU.
    264  1.1  christos 
    265  1.1  christos     # Current CU count.
    266  1.1  christos     variable _cu_count
    267  1.1  christos 
    268  1.1  christos     # The current CU's base label.
    269  1.1  christos     variable _cu_label
    270  1.1  christos 
    271  1.1  christos     # The current CU's version.
    272  1.1  christos     variable _cu_version
    273  1.1  christos 
    274  1.1  christos     # The current CU's address size.
    275  1.1  christos     variable _cu_addr_size
    276  1.1  christos     # The current CU's offset size.
    277  1.1  christos     variable _cu_offset_size
    278  1.1  christos 
    279  1.1  christos     # Label generation number.
    280  1.1  christos     variable _label_num
    281  1.1  christos 
    282  1.1  christos     # The deferred output array.  The index is the section name; the
    283  1.1  christos     # contents hold the data for that section.
    284  1.1  christos     variable _deferred_output
    285  1.1  christos 
    286  1.1  christos     # If empty, we should write directly to the output file.
    287  1.1  christos     # Otherwise, this is the name of a section to write to.
    288  1.1  christos     variable _defer
    289  1.1  christos 
    290  1.1  christos     # The abbrev section.  Typically .debug_abbrev but can be .debug_abbrev.dwo
    291  1.1  christos     # for Fission.
    292  1.1  christos     variable _abbrev_section
    293  1.1  christos 
    294  1.1  christos     # The next available abbrev number in the current CU's abbrev
    295  1.1  christos     # table.
    296  1.1  christos     variable _abbrev_num
    297  1.1  christos 
    298  1.1  christos     # The string table for this assembly.  The key is the string; the
    299  1.1  christos     # value is the label for that string.
    300  1.1  christos     variable _strings
    301  1.1  christos 
    302  1.5  christos     # Current .debug_line unit count.
    303  1.5  christos     variable _line_count
    304  1.5  christos 
    305  1.5  christos     # Whether a file_name entry was seen.
    306  1.5  christos     variable _line_saw_file
    307  1.5  christos 
    308  1.6  christos     # Whether a line table program has been seen.
    309  1.6  christos     variable _line_saw_program
    310  1.6  christos 
    311  1.6  christos     # A Label for line table header generation.
    312  1.6  christos     variable _line_header_end_label
    313  1.6  christos 
    314  1.6  christos     # The address size for debug ranges section.
    315  1.6  christos     variable _debug_ranges_64_bit
    316  1.6  christos 
    317  1.1  christos     proc _process_one_constant {name value} {
    318  1.1  christos 	variable _constants
    319  1.1  christos 	variable _AT
    320  1.1  christos 	variable _FORM
    321  1.1  christos 	variable _OP
    322  1.1  christos 
    323  1.1  christos 	set _constants($name) $value
    324  1.1  christos 
    325  1.1  christos 	if {![regexp "^DW_(\[A-Z\]+)_(\[A-Za-z0-9_\]+)$" $name \
    326  1.1  christos 		  ignore prefix name2]} {
    327  1.1  christos 	    error "non-matching name: $name"
    328  1.1  christos 	}
    329  1.1  christos 
    330  1.1  christos 	if {$name2 == "lo_user" || $name2 == "hi_user"} {
    331  1.1  christos 	    return
    332  1.1  christos 	}
    333  1.1  christos 
    334  1.1  christos 	# We only try to shorten some very common things.
    335  1.1  christos 	# FIXME: CFA?
    336  1.1  christos 	switch -exact -- $prefix {
    337  1.1  christos 	    TAG {
    338  1.1  christos 		# Create two procedures for the tag.  These call
    339  1.1  christos 		# _handle_DW_TAG with the full tag name baked in; this
    340  1.1  christos 		# does all the actual work.
    341  1.1  christos 		proc $name {{attrs {}} {children {}}} \
    342  1.1  christos 		    "_handle_DW_TAG $name \$attrs \$children"
    343  1.1  christos 
    344  1.1  christos 		# Filter out ones that are known to clash.
    345  1.1  christos 		if {$name2 == "variable" || $name2 == "namespace"} {
    346  1.1  christos 		    set name2 "tag_$name2"
    347  1.1  christos 		}
    348  1.1  christos 
    349  1.1  christos 		if {[info commands $name2] != {}} {
    350  1.1  christos 		    error "duplicate proc name: from $name"
    351  1.1  christos 		}
    352  1.1  christos 
    353  1.1  christos 		proc $name2 {{attrs {}} {children {}}} \
    354  1.1  christos 		    "_handle_DW_TAG $name \$attrs \$children"
    355  1.1  christos 	    }
    356  1.1  christos 
    357  1.1  christos 	    AT {
    358  1.1  christos 		set _AT($name2) $name
    359  1.1  christos 	    }
    360  1.1  christos 
    361  1.1  christos 	    FORM {
    362  1.1  christos 		set _FORM($name2) $name
    363  1.1  christos 	    }
    364  1.1  christos 
    365  1.1  christos 	    OP {
    366  1.1  christos 		set _OP($name2) $name
    367  1.1  christos 	    }
    368  1.1  christos 
    369  1.1  christos 	    default {
    370  1.1  christos 		return
    371  1.1  christos 	    }
    372  1.1  christos 	}
    373  1.1  christos     }
    374  1.1  christos 
    375  1.1  christos     proc _read_constants {} {
    376  1.1  christos 	global srcdir hex decimal
    377  1.1  christos 	variable _constants
    378  1.1  christos 
    379  1.1  christos 	# DWARF name-matching regexp.
    380  1.1  christos 	set dwrx "DW_\[a-zA-Z0-9_\]+"
    381  1.1  christos 	# Whitespace regexp.
    382  1.1  christos 	set ws "\[ \t\]+"
    383  1.1  christos 
    384  1.1  christos 	set fd [open [file join $srcdir .. .. include dwarf2.h]]
    385  1.1  christos 	while {![eof $fd]} {
    386  1.1  christos 	    set line [gets $fd]
    387  1.1  christos 	    if {[regexp -- "^${ws}($dwrx)${ws}=${ws}($hex|$decimal),?$" \
    388  1.1  christos 		     $line ignore name value ignore2]} {
    389  1.1  christos 		_process_one_constant $name $value
    390  1.1  christos 	    }
    391  1.1  christos 	}
    392  1.1  christos 	close $fd
    393  1.1  christos 
    394  1.1  christos 	set fd [open [file join $srcdir .. .. include dwarf2.def]]
    395  1.1  christos 	while {![eof $fd]} {
    396  1.1  christos 	    set line [gets $fd]
    397  1.1  christos 	    if {[regexp -- \
    398  1.1  christos 		     "^DW_\[A-Z_\]+${ws}\\(($dwrx),${ws}($hex|$decimal)\\)$" \
    399  1.1  christos 		     $line ignore name value ignore2]} {
    400  1.1  christos 		_process_one_constant $name $value
    401  1.1  christos 	    }
    402  1.1  christos 	}
    403  1.1  christos 	close $fd
    404  1.1  christos 
    405  1.1  christos 	set _constants(SPECIAL_expr) $_constants(DW_FORM_block)
    406  1.1  christos     }
    407  1.1  christos 
    408  1.1  christos     proc _quote {string} {
    409  1.1  christos 	# FIXME
    410  1.1  christos 	return "\"${string}\\0\""
    411  1.1  christos     }
    412  1.1  christos 
    413  1.1  christos     proc _nz_quote {string} {
    414  1.1  christos 	# For now, no quoting is done.
    415  1.1  christos 	return "\"${string}\""
    416  1.1  christos     }
    417  1.1  christos 
    418  1.1  christos     proc _handle_DW_FORM {form value} {
    419  1.1  christos 	switch -exact -- $form {
    420  1.1  christos 	    DW_FORM_string  {
    421  1.1  christos 		_op .ascii [_quote $value]
    422  1.1  christos 	    }
    423  1.1  christos 
    424  1.1  christos 	    DW_FORM_flag_present {
    425  1.1  christos 		# We don't need to emit anything.
    426  1.1  christos 	    }
    427  1.1  christos 
    428  1.1  christos 	    DW_FORM_data4 -
    429  1.1  christos 	    DW_FORM_ref4 {
    430  1.1  christos 		_op .4byte $value
    431  1.1  christos 	    }
    432  1.1  christos 
    433  1.1  christos 	    DW_FORM_ref_addr {
    434  1.1  christos 		variable _cu_offset_size
    435  1.1  christos 		variable _cu_version
    436  1.1  christos 		variable _cu_addr_size
    437  1.1  christos 
    438  1.1  christos 		if {$_cu_version == 2} {
    439  1.1  christos 		    set size $_cu_addr_size
    440  1.1  christos 		} else {
    441  1.1  christos 		    set size $_cu_offset_size
    442  1.1  christos 		}
    443  1.1  christos 
    444  1.1  christos 		_op .${size}byte $value
    445  1.1  christos 	    }
    446  1.1  christos 
    447  1.5  christos 	    DW_FORM_sec_offset {
    448  1.5  christos 		variable _cu_offset_size
    449  1.5  christos 		_op .${_cu_offset_size}byte $value
    450  1.5  christos 	    }
    451  1.5  christos 
    452  1.1  christos 	    DW_FORM_ref1 -
    453  1.1  christos 	    DW_FORM_flag -
    454  1.1  christos 	    DW_FORM_data1 {
    455  1.1  christos 		_op .byte $value
    456  1.1  christos 	    }
    457  1.1  christos 
    458  1.1  christos 	    DW_FORM_sdata {
    459  1.1  christos 		_op .sleb128 $value
    460  1.1  christos 	    }
    461  1.1  christos 
    462  1.1  christos 	    DW_FORM_ref_udata -
    463  1.1  christos 	    DW_FORM_udata {
    464  1.1  christos 		_op .uleb128 $value
    465  1.1  christos 	    }
    466  1.1  christos 
    467  1.1  christos 	    DW_FORM_addr {
    468  1.1  christos 		variable _cu_addr_size
    469  1.1  christos 
    470  1.1  christos 		_op .${_cu_addr_size}byte $value
    471  1.1  christos 	    }
    472  1.1  christos 
    473  1.1  christos 	    DW_FORM_data2 -
    474  1.1  christos 	    DW_FORM_ref2 {
    475  1.1  christos 		_op .2byte $value
    476  1.1  christos 	    }
    477  1.1  christos 
    478  1.1  christos 	    DW_FORM_data8 -
    479  1.1  christos 	    DW_FORM_ref8 -
    480  1.1  christos 	    DW_FORM_ref_sig8 {
    481  1.1  christos 		_op .8byte $value
    482  1.1  christos 	    }
    483  1.1  christos 
    484  1.7  christos 	    DW_FORM_data16 {
    485  1.7  christos 		_op .8byte $value
    486  1.7  christos 	    }
    487  1.7  christos 
    488  1.1  christos 	    DW_FORM_strp {
    489  1.1  christos 		variable _strings
    490  1.1  christos 		variable _cu_offset_size
    491  1.1  christos 
    492  1.1  christos 		if {![info exists _strings($value)]} {
    493  1.1  christos 		    set _strings($value) [new_label strp]
    494  1.1  christos 		    _defer_output .debug_string {
    495  1.1  christos 			define_label $_strings($value)
    496  1.1  christos 			_op .ascii [_quote $value]
    497  1.1  christos 		    }
    498  1.1  christos 		}
    499  1.1  christos 
    500  1.1  christos 		_op .${_cu_offset_size}byte $_strings($value) "strp: $value"
    501  1.1  christos 	    }
    502  1.1  christos 
    503  1.1  christos 	    SPECIAL_expr {
    504  1.1  christos 		set l1 [new_label "expr_start"]
    505  1.1  christos 		set l2 [new_label "expr_end"]
    506  1.1  christos 		_op .uleb128 "$l2 - $l1" "expression"
    507  1.1  christos 		define_label $l1
    508  1.1  christos 		_location $value
    509  1.1  christos 		define_label $l2
    510  1.1  christos 	    }
    511  1.1  christos 
    512  1.1  christos 	    DW_FORM_block1 {
    513  1.1  christos 		set len [string length $value]
    514  1.1  christos 		if {$len > 255} {
    515  1.1  christos 		    error "DW_FORM_block1 length too long"
    516  1.1  christos 		}
    517  1.1  christos 		_op .byte $len
    518  1.1  christos 		_op .ascii [_nz_quote $value]
    519  1.1  christos 	    }
    520  1.1  christos 
    521  1.1  christos 	    DW_FORM_block2 -
    522  1.1  christos 	    DW_FORM_block4 -
    523  1.1  christos 
    524  1.1  christos 	    DW_FORM_block -
    525  1.1  christos 
    526  1.1  christos 	    DW_FORM_ref2 -
    527  1.1  christos 	    DW_FORM_indirect -
    528  1.1  christos 	    DW_FORM_exprloc -
    529  1.1  christos 
    530  1.1  christos 	    DW_FORM_GNU_addr_index -
    531  1.1  christos 	    DW_FORM_GNU_str_index -
    532  1.1  christos 	    DW_FORM_GNU_ref_alt -
    533  1.1  christos 	    DW_FORM_GNU_strp_alt -
    534  1.1  christos 
    535  1.1  christos 	    default {
    536  1.1  christos 		error "unhandled form $form"
    537  1.1  christos 	    }
    538  1.1  christos 	}
    539  1.1  christos     }
    540  1.1  christos 
    541  1.1  christos     proc _guess_form {value varname} {
    542  1.1  christos 	upvar $varname new_value
    543  1.1  christos 
    544  1.1  christos 	switch -exact -- [string range $value 0 0] {
    545  1.1  christos 	    @ {
    546  1.1  christos 		# Constant reference.
    547  1.1  christos 		variable _constants
    548  1.1  christos 
    549  1.1  christos 		set new_value $_constants([string range $value 1 end])
    550  1.1  christos 		# Just the simplest.
    551  1.1  christos 		return DW_FORM_sdata
    552  1.1  christos 	    }
    553  1.1  christos 
    554  1.1  christos 	    : {
    555  1.1  christos 		# Label reference.
    556  1.1  christos 		variable _cu_label
    557  1.1  christos 
    558  1.1  christos 		set new_value "[string range $value 1 end] - $_cu_label"
    559  1.1  christos 
    560  1.1  christos 		return DW_FORM_ref4
    561  1.1  christos 	    }
    562  1.1  christos 
    563  1.7  christos 	    % {
    564  1.7  christos 		# Label reference, an offset from .debug_info.  Assuming
    565  1.7  christos 		# .Lcu1_begin is on .debug_info.
    566  1.7  christos 		set cu1_label [_compute_label "cu1_begin"]
    567  1.7  christos 		set new_value "[string range $value 1 end] - $cu1_label"
    568  1.7  christos 
    569  1.7  christos 		return DW_FORM_ref_addr
    570  1.7  christos 	    }
    571  1.7  christos 
    572  1.1  christos 	    default {
    573  1.1  christos 		return DW_FORM_string
    574  1.1  christos 	    }
    575  1.1  christos 	}
    576  1.1  christos     }
    577  1.1  christos 
    578  1.1  christos     # Map NAME to its canonical form.
    579  1.1  christos     proc _map_name {name ary} {
    580  1.1  christos 	variable $ary
    581  1.1  christos 
    582  1.1  christos 	if {[info exists ${ary}($name)]} {
    583  1.1  christos 	    set name [set ${ary}($name)]
    584  1.1  christos 	}
    585  1.1  christos 
    586  1.1  christos 	return $name
    587  1.1  christos     }
    588  1.1  christos 
    589  1.3  christos     proc _handle_attribute { attr_name attr_value attr_form } {
    590  1.3  christos 	variable _abbrev_section
    591  1.3  christos 	variable _constants
    592  1.3  christos 
    593  1.3  christos 	_handle_DW_FORM $attr_form $attr_value
    594  1.3  christos 
    595  1.3  christos 	_defer_output $_abbrev_section {
    596  1.3  christos 	    _op .uleb128 $_constants($attr_name) $attr_name
    597  1.3  christos 	    _op .uleb128 $_constants($attr_form) $attr_form
    598  1.3  christos 	}
    599  1.3  christos     }
    600  1.3  christos 
    601  1.3  christos     # Handle macro attribute MACRO_AT_range.
    602  1.3  christos 
    603  1.3  christos     proc _handle_macro_at_range { attr_value } {
    604  1.3  christos 	if {[llength $attr_value] != 2} {
    605  1.3  christos 	    error "usage: MACRO_AT_range { func file }"
    606  1.3  christos 	}
    607  1.3  christos 
    608  1.3  christos 	set func [lindex $attr_value 0]
    609  1.3  christos 	set src [lindex $attr_value 1]
    610  1.3  christos 	set result [function_range $func $src]
    611  1.3  christos 
    612  1.3  christos 	_handle_attribute DW_AT_low_pc [lindex $result 0] \
    613  1.3  christos 	    DW_FORM_addr
    614  1.3  christos 	_handle_attribute DW_AT_high_pc \
    615  1.3  christos 	    "[lindex $result 0] + [lindex $result 1]" DW_FORM_addr
    616  1.3  christos     }
    617  1.3  christos 
    618  1.3  christos     # Handle macro attribute MACRO_AT_func.
    619  1.3  christos 
    620  1.3  christos     proc _handle_macro_at_func { attr_value } {
    621  1.3  christos 	if {[llength $attr_value] != 2} {
    622  1.3  christos 	    error "usage: MACRO_AT_func { func file }"
    623  1.3  christos 	}
    624  1.3  christos 	_handle_attribute DW_AT_name [lindex $attr_value 0] DW_FORM_string
    625  1.3  christos 	_handle_macro_at_range $attr_value
    626  1.3  christos     }
    627  1.3  christos 
    628  1.1  christos     proc _handle_DW_TAG {tag_name {attrs {}} {children {}}} {
    629  1.1  christos 	variable _abbrev_section
    630  1.1  christos 	variable _abbrev_num
    631  1.1  christos 	variable _constants
    632  1.1  christos 
    633  1.1  christos 	set has_children [expr {[string length $children] > 0}]
    634  1.1  christos 	set my_abbrev [incr _abbrev_num]
    635  1.1  christos 
    636  1.1  christos 	# We somewhat wastefully emit a new abbrev entry for each tag.
    637  1.1  christos 	# There's no reason for this other than laziness.
    638  1.1  christos 	_defer_output $_abbrev_section {
    639  1.1  christos 	    _op .uleb128 $my_abbrev "Abbrev start"
    640  1.1  christos 	    _op .uleb128 $_constants($tag_name) $tag_name
    641  1.1  christos 	    _op .byte $has_children "has_children"
    642  1.1  christos 	}
    643  1.1  christos 
    644  1.1  christos 	_op .uleb128 $my_abbrev "Abbrev ($tag_name)"
    645  1.1  christos 
    646  1.1  christos 	foreach attr $attrs {
    647  1.1  christos 	    set attr_name [_map_name [lindex $attr 0] _AT]
    648  1.6  christos 
    649  1.6  christos 	    # When the length of ATTR is greater than 2, the last
    650  1.6  christos 	    # element of the list must be a form.  The second through
    651  1.6  christos 	    # the penultimate elements are joined together and
    652  1.6  christos 	    # evaluated using subst.  This allows constructs such as
    653  1.6  christos 	    # [gdb_target_symbol foo] to be used.
    654  1.6  christos 
    655  1.6  christos 	    if {[llength $attr] > 2} {
    656  1.6  christos 	        set attr_value [uplevel 2 [list subst [join [lrange $attr 1 end-1]]]]
    657  1.6  christos 	    } else {
    658  1.6  christos 	        set attr_value [uplevel 2 [list subst [lindex $attr 1]]]
    659  1.6  christos 	    }
    660  1.3  christos 
    661  1.3  christos 	    if { [string equal "MACRO_AT_func" $attr_name] } {
    662  1.3  christos 		_handle_macro_at_func $attr_value
    663  1.3  christos 	    } elseif { [string equal "MACRO_AT_range" $attr_name] } {
    664  1.3  christos 		_handle_macro_at_range $attr_value
    665  1.1  christos 	    } else {
    666  1.3  christos 		if {[llength $attr] > 2} {
    667  1.7  christos 		    set attr_form [uplevel 2 [list subst [lindex $attr end]]]
    668  1.7  christos 
    669  1.7  christos 		    if { [string index $attr_value 0] == ":" } {
    670  1.7  christos 			# It is a label, get its value.
    671  1.7  christos 			_guess_form $attr_value attr_value
    672  1.7  christos 		    }
    673  1.3  christos 		} else {
    674  1.3  christos 		    # If the value looks like an integer, a form is required.
    675  1.3  christos 		    if [string is integer $attr_value] {
    676  1.3  christos 			error "Integer value requires a form"
    677  1.3  christos 		    }
    678  1.3  christos 		    set attr_form [_guess_form $attr_value attr_value]
    679  1.3  christos 		}
    680  1.3  christos 		set attr_form [_map_name $attr_form _FORM]
    681  1.1  christos 
    682  1.3  christos 		_handle_attribute $attr_name $attr_value $attr_form
    683  1.1  christos 	    }
    684  1.1  christos 	}
    685  1.1  christos 
    686  1.1  christos 	_defer_output $_abbrev_section {
    687  1.1  christos 	    # Terminator.
    688  1.1  christos 	    _op .byte 0x0 Terminator
    689  1.1  christos 	    _op .byte 0x0 Terminator
    690  1.1  christos 	}
    691  1.1  christos 
    692  1.1  christos 	if {$has_children} {
    693  1.1  christos 	    uplevel 2 $children
    694  1.1  christos 
    695  1.1  christos 	    # Terminate children.
    696  1.1  christos 	    _op .byte 0x0 "Terminate children"
    697  1.1  christos 	}
    698  1.1  christos     }
    699  1.1  christos 
    700  1.1  christos     proc _emit {string} {
    701  1.1  christos 	variable _output_file
    702  1.1  christos 	variable _defer
    703  1.1  christos 	variable _deferred_output
    704  1.1  christos 
    705  1.1  christos 	if {$_defer == ""} {
    706  1.1  christos 	    puts $_output_file $string
    707  1.1  christos 	} else {
    708  1.1  christos 	    append _deferred_output($_defer) ${string}\n
    709  1.1  christos 	}
    710  1.1  christos     }
    711  1.1  christos 
    712  1.1  christos     proc _section {name {flags ""} {type ""}} {
    713  1.1  christos 	if {$flags == "" && $type == ""} {
    714  1.1  christos 	    _emit "        .section $name"
    715  1.1  christos 	} elseif {$type == ""} {
    716  1.1  christos 	    _emit "        .section $name, \"$flags\""
    717  1.1  christos 	} else {
    718  1.1  christos 	    _emit "        .section $name, \"$flags\", %$type"
    719  1.1  christos 	}
    720  1.1  christos     }
    721  1.1  christos 
    722  1.1  christos     # SECTION_SPEC is a list of arguments to _section.
    723  1.1  christos     proc _defer_output {section_spec body} {
    724  1.1  christos 	variable _defer
    725  1.1  christos 	variable _deferred_output
    726  1.1  christos 
    727  1.1  christos 	set old_defer $_defer
    728  1.1  christos 	set _defer [lindex $section_spec 0]
    729  1.1  christos 
    730  1.1  christos 	if {![info exists _deferred_output($_defer)]} {
    731  1.1  christos 	    set _deferred_output($_defer) ""
    732  1.1  christos 	    eval _section $section_spec
    733  1.1  christos 	}
    734  1.1  christos 
    735  1.1  christos 	uplevel $body
    736  1.1  christos 
    737  1.1  christos 	set _defer $old_defer
    738  1.1  christos     }
    739  1.1  christos 
    740  1.1  christos     proc _defer_to_string {body} {
    741  1.1  christos 	variable _defer
    742  1.1  christos 	variable _deferred_output
    743  1.1  christos 
    744  1.1  christos 	set old_defer $_defer
    745  1.1  christos 	set _defer temp
    746  1.1  christos 
    747  1.1  christos 	set _deferred_output($_defer) ""
    748  1.1  christos 
    749  1.1  christos 	uplevel $body
    750  1.1  christos 
    751  1.1  christos 	set result $_deferred_output($_defer)
    752  1.1  christos 	unset _deferred_output($_defer)
    753  1.1  christos 
    754  1.1  christos 	set _defer $old_defer
    755  1.1  christos 	return $result
    756  1.1  christos     }
    757  1.1  christos 
    758  1.1  christos     proc _write_deferred_output {} {
    759  1.1  christos 	variable _output_file
    760  1.1  christos 	variable _deferred_output
    761  1.1  christos 
    762  1.1  christos 	foreach section [array names _deferred_output] {
    763  1.1  christos 	    # The data already has a newline.
    764  1.1  christos 	    puts -nonewline $_output_file $_deferred_output($section)
    765  1.1  christos 	}
    766  1.1  christos 
    767  1.1  christos 	# Save some memory.
    768  1.1  christos 	unset _deferred_output
    769  1.1  christos     }
    770  1.1  christos 
    771  1.1  christos     proc _op {name value {comment ""}} {
    772  1.1  christos 	set text "        ${name}        ${value}"
    773  1.1  christos 	if {$comment != ""} {
    774  1.1  christos 	    # Try to make stuff line up nicely.
    775  1.1  christos 	    while {[string length $text] < 40} {
    776  1.1  christos 		append text " "
    777  1.1  christos 	    }
    778  1.1  christos 	    append text "/* ${comment} */"
    779  1.1  christos 	}
    780  1.1  christos 	_emit $text
    781  1.1  christos     }
    782  1.1  christos 
    783  1.1  christos     proc _compute_label {name} {
    784  1.1  christos 	return ".L${name}"
    785  1.1  christos     }
    786  1.1  christos 
    787  1.1  christos     # Return a name suitable for use as a label.  If BASE_NAME is
    788  1.1  christos     # specified, it is incorporated into the label name; this is to
    789  1.1  christos     # make debugging the generated assembler easier.  If BASE_NAME is
    790  1.1  christos     # not specified a generic default is used.  This proc does not
    791  1.1  christos     # define the label; see 'define_label'.  'new_label' attempts to
    792  1.1  christos     # ensure that label names are unique.
    793  1.1  christos     proc new_label {{base_name label}} {
    794  1.1  christos 	variable _label_num
    795  1.1  christos 
    796  1.1  christos 	return [_compute_label ${base_name}[incr _label_num]]
    797  1.1  christos     }
    798  1.1  christos 
    799  1.1  christos     # Define a label named NAME.  Ordinarily, NAME comes from a call
    800  1.1  christos     # to 'new_label', but this is not required.
    801  1.1  christos     proc define_label {name} {
    802  1.1  christos 	_emit "${name}:"
    803  1.1  christos     }
    804  1.1  christos 
    805  1.1  christos     # Declare a global label.  This is typically used to refer to
    806  1.1  christos     # labels defined in other files, for example a function defined in
    807  1.1  christos     # a .c file.
    808  1.1  christos     proc extern {args} {
    809  1.1  christos 	foreach name $args {
    810  1.1  christos 	    _op .global $name
    811  1.1  christos 	}
    812  1.1  christos     }
    813  1.1  christos 
    814  1.1  christos     # A higher-level interface to label handling.
    815  1.1  christos     #
    816  1.1  christos     # ARGS is a list of label descriptors.  Each one is either a
    817  1.1  christos     # single element, or a list of two elements -- a name and some
    818  1.1  christos     # text.  For each descriptor, 'new_label' is invoked.  If the list
    819  1.1  christos     # form is used, the second element in the list is passed as an
    820  1.1  christos     # argument.  The label name is used to define a variable in the
    821  1.1  christos     # enclosing scope; this can be used to refer to the label later.
    822  1.1  christos     # The label name is also used to define a new proc whose name is
    823  1.1  christos     # the label name plus a trailing ":".  This proc takes a body as
    824  1.1  christos     # an argument and can be used to define the label at that point;
    825  1.1  christos     # then the body, if any, is evaluated in the caller's context.
    826  1.1  christos     #
    827  1.1  christos     # For example:
    828  1.1  christos     #
    829  1.1  christos     # declare_labels int_label
    830  1.1  christos     # something { ... $int_label }   ;# refer to the label
    831  1.1  christos     # int_label: constant { ... }    ;# define the label
    832  1.1  christos     proc declare_labels {args} {
    833  1.1  christos 	foreach arg $args {
    834  1.1  christos 	    set name [lindex $arg 0]
    835  1.1  christos 	    set text [lindex $arg 1]
    836  1.1  christos 
    837  1.1  christos 	    upvar $name label_var
    838  1.1  christos 	    if {$text == ""} {
    839  1.1  christos 		set label_var [new_label]
    840  1.1  christos 	    } else {
    841  1.1  christos 		set label_var [new_label $text]
    842  1.1  christos 	    }
    843  1.1  christos 
    844  1.1  christos 	    proc ${name}: {args} [format {
    845  1.1  christos 		define_label %s
    846  1.1  christos 		uplevel $args
    847  1.1  christos 	    } $label_var]
    848  1.1  christos 	}
    849  1.1  christos     }
    850  1.1  christos 
    851  1.1  christos     # This is a miniature assembler for location expressions.  It is
    852  1.1  christos     # suitable for use in the attributes to a DIE.  Its output is
    853  1.1  christos     # prefixed with "=" to make it automatically use DW_FORM_block.
    854  1.1  christos     # BODY is split by lines, and each line is taken to be a list.
    855  1.1  christos     # (FIXME should use 'info complete' here.)
    856  1.1  christos     # Each list's first element is the opcode, either short or long
    857  1.1  christos     # forms are accepted.
    858  1.1  christos     # FIXME argument handling
    859  1.1  christos     # FIXME move docs
    860  1.1  christos     proc _location {body} {
    861  1.1  christos 	variable _constants
    862  1.1  christos 	variable _cu_label
    863  1.7  christos 	variable _cu_version
    864  1.1  christos 	variable _cu_addr_size
    865  1.1  christos 	variable _cu_offset_size
    866  1.1  christos 
    867  1.1  christos 	foreach line [split $body \n] {
    868  1.3  christos 	    # Ignore blank lines, and allow embedded comments.
    869  1.3  christos 	    if {[lindex $line 0] == "" || [regexp -- {^[ \t]*#} $line]} {
    870  1.1  christos 		continue
    871  1.1  christos 	    }
    872  1.1  christos 	    set opcode [_map_name [lindex $line 0] _OP]
    873  1.1  christos 	    _op .byte $_constants($opcode) $opcode
    874  1.1  christos 
    875  1.1  christos 	    switch -exact -- $opcode {
    876  1.1  christos 		DW_OP_addr {
    877  1.1  christos 		    _op .${_cu_addr_size}byte [lindex $line 1]
    878  1.1  christos 		}
    879  1.1  christos 
    880  1.6  christos 		DW_OP_regx {
    881  1.6  christos 		    _op .uleb128 [lindex $line 1]
    882  1.6  christos 		}
    883  1.6  christos 
    884  1.3  christos 		DW_OP_pick -
    885  1.1  christos 		DW_OP_const1u -
    886  1.1  christos 		DW_OP_const1s {
    887  1.1  christos 		    _op .byte [lindex $line 1]
    888  1.1  christos 		}
    889  1.1  christos 
    890  1.1  christos 		DW_OP_const2u -
    891  1.1  christos 		DW_OP_const2s {
    892  1.1  christos 		    _op .2byte [lindex $line 1]
    893  1.1  christos 		}
    894  1.1  christos 
    895  1.1  christos 		DW_OP_const4u -
    896  1.1  christos 		DW_OP_const4s {
    897  1.1  christos 		    _op .4byte [lindex $line 1]
    898  1.1  christos 		}
    899  1.1  christos 
    900  1.1  christos 		DW_OP_const8u -
    901  1.1  christos 		DW_OP_const8s {
    902  1.1  christos 		    _op .8byte [lindex $line 1]
    903  1.1  christos 		}
    904  1.1  christos 
    905  1.1  christos 		DW_OP_constu {
    906  1.1  christos 		    _op .uleb128 [lindex $line 1]
    907  1.1  christos 		}
    908  1.1  christos 		DW_OP_consts {
    909  1.1  christos 		    _op .sleb128 [lindex $line 1]
    910  1.1  christos 		}
    911  1.1  christos 
    912  1.1  christos 		DW_OP_plus_uconst {
    913  1.1  christos 		    _op .uleb128 [lindex $line 1]
    914  1.1  christos 		}
    915  1.1  christos 
    916  1.1  christos 		DW_OP_piece {
    917  1.1  christos 		    _op .uleb128 [lindex $line 1]
    918  1.1  christos 		}
    919  1.1  christos 
    920  1.1  christos 		DW_OP_bit_piece {
    921  1.1  christos 		    _op .uleb128 [lindex $line 1]
    922  1.1  christos 		    _op .uleb128 [lindex $line 2]
    923  1.1  christos 		}
    924  1.1  christos 
    925  1.3  christos 		DW_OP_skip -
    926  1.3  christos 		DW_OP_bra {
    927  1.3  christos 		    _op .2byte [lindex $line 1]
    928  1.3  christos 		}
    929  1.3  christos 
    930  1.7  christos 		DW_OP_implicit_value {
    931  1.7  christos 		    set l1 [new_label "value_start"]
    932  1.7  christos 		    set l2 [new_label "value_end"]
    933  1.7  christos 		    _op .uleb128 "$l2 - $l1"
    934  1.7  christos 		    define_label $l1
    935  1.7  christos 		    foreach value [lrange $line 1 end] {
    936  1.7  christos 			switch -regexp -- $value {
    937  1.7  christos 			    {^0x[[:xdigit:]]{1,2}$} {_op .byte $value}
    938  1.7  christos 			    {^0x[[:xdigit:]]{4}$} {_op .2byte $value}
    939  1.7  christos 			    {^0x[[:xdigit:]]{8}$} {_op .4byte $value}
    940  1.7  christos 			    {^0x[[:xdigit:]]{16}$} {_op .8byte $value}
    941  1.7  christos 			    default {
    942  1.7  christos 				error "bad value '$value' in DW_OP_implicit_value"
    943  1.7  christos 			    }
    944  1.7  christos 			}
    945  1.7  christos 		    }
    946  1.7  christos 		    define_label $l2
    947  1.7  christos 		}
    948  1.7  christos 
    949  1.7  christos 		DW_OP_implicit_pointer -
    950  1.1  christos 		DW_OP_GNU_implicit_pointer {
    951  1.1  christos 		    if {[llength $line] != 3} {
    952  1.7  christos 			error "usage: $opcode LABEL OFFSET"
    953  1.1  christos 		    }
    954  1.1  christos 
    955  1.1  christos 		    # Here label is a section offset.
    956  1.1  christos 		    set label [lindex $line 1]
    957  1.7  christos 		    if { $_cu_version == 2 } {
    958  1.7  christos 			_op .${_cu_addr_size}byte $label
    959  1.7  christos 		    } else {
    960  1.7  christos 			_op .${_cu_offset_size}byte $label
    961  1.7  christos 		    }
    962  1.1  christos 		    _op .sleb128 [lindex $line 2]
    963  1.1  christos 		}
    964  1.1  christos 
    965  1.8  christos 		DW_OP_GNU_variable_value {
    966  1.8  christos 		    if {[llength $line] != 2} {
    967  1.8  christos 			error "usage: $opcode LABEL"
    968  1.8  christos 		    }
    969  1.8  christos 
    970  1.8  christos 		    # Here label is a section offset.
    971  1.8  christos 		    set label [lindex $line 1]
    972  1.8  christos 		    if { $_cu_version == 2 } {
    973  1.8  christos 			_op .${_cu_addr_size}byte $label
    974  1.8  christos 		    } else {
    975  1.8  christos 			_op .${_cu_offset_size}byte $label
    976  1.8  christos 		    }
    977  1.8  christos 		}
    978  1.8  christos 
    979  1.1  christos 		DW_OP_deref_size {
    980  1.1  christos 		    if {[llength $line] != 2} {
    981  1.1  christos 			error "usage: DW_OP_deref_size SIZE"
    982  1.1  christos 		    }
    983  1.1  christos 
    984  1.1  christos 		    _op .byte [lindex $line 1]
    985  1.1  christos 		}
    986  1.1  christos 
    987  1.6  christos 		DW_OP_bregx {
    988  1.6  christos 		    _op .uleb128 [lindex $line 1]
    989  1.6  christos 		    _op .sleb128 [lindex $line 2]
    990  1.6  christos 		}
    991  1.6  christos 
    992  1.1  christos 		default {
    993  1.1  christos 		    if {[llength $line] > 1} {
    994  1.1  christos 			error "Unimplemented: operands in location for $opcode"
    995  1.1  christos 		    }
    996  1.1  christos 		}
    997  1.1  christos 	    }
    998  1.1  christos 	}
    999  1.1  christos     }
   1000  1.1  christos 
   1001  1.1  christos     # Emit a DWARF CU.
   1002  1.1  christos     # OPTIONS is a list with an even number of elements containing
   1003  1.1  christos     # option-name and option-value pairs.
   1004  1.1  christos     # Current options are:
   1005  1.1  christos     # is_64 0|1    - boolean indicating if you want to emit 64-bit DWARF
   1006  1.1  christos     #                default = 0 (32-bit)
   1007  1.1  christos     # version n    - DWARF version number to emit
   1008  1.1  christos     #                default = 4
   1009  1.1  christos     # addr_size n  - the size of addresses, 32, 64, or default
   1010  1.1  christos     #                default = default
   1011  1.1  christos     # fission 0|1  - boolean indicating if generating Fission debug info
   1012  1.1  christos     #                default = 0
   1013  1.1  christos     # BODY is Tcl code that emits the DIEs which make up the body of
   1014  1.1  christos     # the CU.  It is evaluated in the caller's context.
   1015  1.1  christos     proc cu {options body} {
   1016  1.1  christos 	variable _cu_count
   1017  1.1  christos 	variable _abbrev_section
   1018  1.1  christos 	variable _abbrev_num
   1019  1.1  christos 	variable _cu_label
   1020  1.1  christos 	variable _cu_version
   1021  1.1  christos 	variable _cu_addr_size
   1022  1.1  christos 	variable _cu_offset_size
   1023  1.1  christos 
   1024  1.1  christos 	# Establish the defaults.
   1025  1.1  christos 	set is_64 0
   1026  1.1  christos 	set _cu_version 4
   1027  1.1  christos 	set _cu_addr_size default
   1028  1.1  christos 	set fission 0
   1029  1.1  christos 	set section ".debug_info"
   1030  1.1  christos 	set _abbrev_section ".debug_abbrev"
   1031  1.1  christos 
   1032  1.1  christos 	foreach { name value } $options {
   1033  1.7  christos 	    set value [uplevel 1 "subst \"$value\""]
   1034  1.1  christos 	    switch -exact -- $name {
   1035  1.1  christos 		is_64 { set is_64 $value }
   1036  1.1  christos 		version { set _cu_version $value }
   1037  1.1  christos 		addr_size { set _cu_addr_size $value }
   1038  1.1  christos 		fission { set fission $value }
   1039  1.1  christos 		default { error "unknown option $name" }
   1040  1.1  christos 	    }
   1041  1.1  christos 	}
   1042  1.1  christos 	if {$_cu_addr_size == "default"} {
   1043  1.1  christos 	    if {[is_64_target]} {
   1044  1.1  christos 		set _cu_addr_size 8
   1045  1.1  christos 	    } else {
   1046  1.1  christos 		set _cu_addr_size 4
   1047  1.1  christos 	    }
   1048  1.1  christos 	}
   1049  1.1  christos 	set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
   1050  1.1  christos 	if { $fission } {
   1051  1.1  christos 	    set section ".debug_info.dwo"
   1052  1.1  christos 	    set _abbrev_section ".debug_abbrev.dwo"
   1053  1.1  christos 	}
   1054  1.1  christos 
   1055  1.1  christos 	_section $section
   1056  1.1  christos 
   1057  1.1  christos 	set cu_num [incr _cu_count]
   1058  1.1  christos 	set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
   1059  1.1  christos 	set _abbrev_num 1
   1060  1.1  christos 
   1061  1.1  christos 	set _cu_label [_compute_label "cu${cu_num}_begin"]
   1062  1.1  christos 	set start_label [_compute_label "cu${cu_num}_start"]
   1063  1.1  christos 	set end_label [_compute_label "cu${cu_num}_end"]
   1064  1.6  christos 
   1065  1.1  christos 	define_label $_cu_label
   1066  1.1  christos 	if {$is_64} {
   1067  1.1  christos 	    _op .4byte 0xffffffff
   1068  1.1  christos 	    _op .8byte "$end_label - $start_label"
   1069  1.1  christos 	} else {
   1070  1.1  christos 	    _op .4byte "$end_label - $start_label"
   1071  1.1  christos 	}
   1072  1.1  christos 	define_label $start_label
   1073  1.1  christos 	_op .2byte $_cu_version Version
   1074  1.3  christos 	_op .${_cu_offset_size}byte $my_abbrevs Abbrevs
   1075  1.1  christos 	_op .byte $_cu_addr_size "Pointer size"
   1076  1.1  christos 
   1077  1.1  christos 	_defer_output $_abbrev_section {
   1078  1.1  christos 	    define_label $my_abbrevs
   1079  1.1  christos 	}
   1080  1.1  christos 
   1081  1.1  christos 	uplevel $body
   1082  1.1  christos 
   1083  1.1  christos 	_defer_output $_abbrev_section {
   1084  1.1  christos 	    # Emit the terminator.
   1085  1.1  christos 	    _op .byte 0x0 Terminator
   1086  1.1  christos 	    _op .byte 0x0 Terminator
   1087  1.1  christos 	}
   1088  1.1  christos 
   1089  1.1  christos 	define_label $end_label
   1090  1.1  christos     }
   1091  1.1  christos 
   1092  1.1  christos     # Emit a DWARF TU.
   1093  1.1  christos     # OPTIONS is a list with an even number of elements containing
   1094  1.1  christos     # option-name and option-value pairs.
   1095  1.1  christos     # Current options are:
   1096  1.1  christos     # is_64 0|1    - boolean indicating if you want to emit 64-bit DWARF
   1097  1.1  christos     #                default = 0 (32-bit)
   1098  1.1  christos     # version n    - DWARF version number to emit
   1099  1.1  christos     #                default = 4
   1100  1.1  christos     # addr_size n  - the size of addresses, 32, 64, or default
   1101  1.1  christos     #                default = default
   1102  1.1  christos     # fission 0|1  - boolean indicating if generating Fission debug info
   1103  1.1  christos     #                default = 0
   1104  1.1  christos     # SIGNATURE is the 64-bit signature of the type.
   1105  1.1  christos     # TYPE_LABEL is the label of the type defined by this TU,
   1106  1.1  christos     # or "" if there is no type (i.e., type stubs in Fission).
   1107  1.1  christos     # BODY is Tcl code that emits the DIEs which make up the body of
   1108  1.1  christos     # the TU.  It is evaluated in the caller's context.
   1109  1.1  christos     proc tu {options signature type_label body} {
   1110  1.1  christos 	variable _cu_count
   1111  1.1  christos 	variable _abbrev_section
   1112  1.1  christos 	variable _abbrev_num
   1113  1.1  christos 	variable _cu_label
   1114  1.1  christos 	variable _cu_version
   1115  1.1  christos 	variable _cu_addr_size
   1116  1.1  christos 	variable _cu_offset_size
   1117  1.1  christos 
   1118  1.1  christos 	# Establish the defaults.
   1119  1.1  christos 	set is_64 0
   1120  1.1  christos 	set _cu_version 4
   1121  1.1  christos 	set _cu_addr_size default
   1122  1.1  christos 	set fission 0
   1123  1.1  christos 	set section ".debug_types"
   1124  1.1  christos 	set _abbrev_section ".debug_abbrev"
   1125  1.1  christos 
   1126  1.1  christos 	foreach { name value } $options {
   1127  1.1  christos 	    switch -exact -- $name {
   1128  1.1  christos 		is_64 { set is_64 $value }
   1129  1.1  christos 		version { set _cu_version $value }
   1130  1.1  christos 		addr_size { set _cu_addr_size $value }
   1131  1.1  christos 		fission { set fission $value }
   1132  1.1  christos 		default { error "unknown option $name" }
   1133  1.1  christos 	    }
   1134  1.1  christos 	}
   1135  1.1  christos 	if {$_cu_addr_size == "default"} {
   1136  1.1  christos 	    if {[is_64_target]} {
   1137  1.1  christos 		set _cu_addr_size 8
   1138  1.1  christos 	    } else {
   1139  1.1  christos 		set _cu_addr_size 4
   1140  1.1  christos 	    }
   1141  1.1  christos 	}
   1142  1.1  christos 	set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
   1143  1.1  christos 	if { $fission } {
   1144  1.1  christos 	    set section ".debug_types.dwo"
   1145  1.1  christos 	    set _abbrev_section ".debug_abbrev.dwo"
   1146  1.1  christos 	}
   1147  1.1  christos 
   1148  1.1  christos 	_section $section
   1149  1.1  christos 
   1150  1.1  christos 	set cu_num [incr _cu_count]
   1151  1.1  christos 	set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
   1152  1.1  christos 	set _abbrev_num 1
   1153  1.1  christos 
   1154  1.1  christos 	set _cu_label [_compute_label "cu${cu_num}_begin"]
   1155  1.1  christos 	set start_label [_compute_label "cu${cu_num}_start"]
   1156  1.1  christos 	set end_label [_compute_label "cu${cu_num}_end"]
   1157  1.1  christos 
   1158  1.1  christos 	define_label $_cu_label
   1159  1.1  christos 	if {$is_64} {
   1160  1.1  christos 	    _op .4byte 0xffffffff
   1161  1.1  christos 	    _op .8byte "$end_label - $start_label"
   1162  1.1  christos 	} else {
   1163  1.1  christos 	    _op .4byte "$end_label - $start_label"
   1164  1.1  christos 	}
   1165  1.1  christos 	define_label $start_label
   1166  1.1  christos 	_op .2byte $_cu_version Version
   1167  1.3  christos 	_op .${_cu_offset_size}byte $my_abbrevs Abbrevs
   1168  1.1  christos 	_op .byte $_cu_addr_size "Pointer size"
   1169  1.1  christos 	_op .8byte $signature Signature
   1170  1.1  christos 	if { $type_label != "" } {
   1171  1.1  christos 	    uplevel declare_labels $type_label
   1172  1.1  christos 	    upvar $type_label my_type_label
   1173  1.1  christos 	    if {$is_64} {
   1174  1.1  christos 		_op .8byte "$my_type_label - $_cu_label"
   1175  1.1  christos 	    } else {
   1176  1.1  christos 		_op .4byte "$my_type_label - $_cu_label"
   1177  1.1  christos 	    }
   1178  1.1  christos 	} else {
   1179  1.1  christos 	    if {$is_64} {
   1180  1.1  christos 		_op .8byte 0
   1181  1.1  christos 	    } else {
   1182  1.1  christos 		_op .4byte 0
   1183  1.1  christos 	    }
   1184  1.1  christos 	}
   1185  1.1  christos 
   1186  1.1  christos 	_defer_output $_abbrev_section {
   1187  1.1  christos 	    define_label $my_abbrevs
   1188  1.1  christos 	}
   1189  1.1  christos 
   1190  1.1  christos 	uplevel $body
   1191  1.1  christos 
   1192  1.1  christos 	_defer_output $_abbrev_section {
   1193  1.1  christos 	    # Emit the terminator.
   1194  1.1  christos 	    _op .byte 0x0 Terminator
   1195  1.1  christos 	    _op .byte 0x0 Terminator
   1196  1.1  christos 	}
   1197  1.1  christos 
   1198  1.1  christos 	define_label $end_label
   1199  1.1  christos     }
   1200  1.1  christos 
   1201  1.6  christos     # Emit a DWARF .debug_ranges unit.
   1202  1.6  christos     # OPTIONS is a list with an even number of elements containing
   1203  1.6  christos     # option-name and option-value pairs.
   1204  1.6  christos     # Current options are:
   1205  1.6  christos     # is_64 0|1    - boolean indicating if you want to emit 64-bit DWARF
   1206  1.6  christos     #                default = 0 (32-bit)
   1207  1.6  christos     #
   1208  1.6  christos     # BODY is Tcl code that emits the content of the .debug_ranges
   1209  1.6  christos     # unit, it is evaluated in the caller's context.
   1210  1.6  christos     proc ranges {options body} {
   1211  1.6  christos 	variable _debug_ranges_64_bit
   1212  1.6  christos 
   1213  1.6  christos 	foreach { name value } $options {
   1214  1.6  christos 	    switch -exact -- $name {
   1215  1.6  christos 		is_64 { set _debug_ranges_64_bit [subst $value] }
   1216  1.6  christos 		default { error "unknown option $name" }
   1217  1.6  christos 	    }
   1218  1.6  christos 	}
   1219  1.6  christos 
   1220  1.6  christos 	set section ".debug_ranges"
   1221  1.6  christos 	_section $section
   1222  1.6  christos 
   1223  1.6  christos 	proc sequence {{ranges {}}} {
   1224  1.6  christos 	    variable _debug_ranges_64_bit
   1225  1.6  christos 
   1226  1.6  christos 	    # Emit the sequence of addresses.
   1227  1.6  christos 	    set base ""
   1228  1.6  christos 	    foreach range $ranges {
   1229  1.6  christos 		set range [uplevel 1 "subst \"$range\""]
   1230  1.6  christos 		set type [lindex $range 0]
   1231  1.6  christos 		switch -exact -- $type {
   1232  1.6  christos 		    base {
   1233  1.6  christos 			set base [lrange $range 1 end]
   1234  1.6  christos 
   1235  1.6  christos 			if { $_debug_ranges_64_bit } then {
   1236  1.6  christos 			    _op .8byte 0xffffffffffffffff "Base Marker"
   1237  1.6  christos 			    _op .8byte $base "Base Address"
   1238  1.6  christos 			} else {
   1239  1.6  christos 			    _op .4byte 0xffffffff "Base Marker"
   1240  1.6  christos 			    _op .4byte $base "Base Address"
   1241  1.6  christos 			}
   1242  1.6  christos 		    }
   1243  1.6  christos 		    range {
   1244  1.6  christos 			set start [lindex $range 1]
   1245  1.6  christos 			set end [lrange $range 2 end]
   1246  1.6  christos 
   1247  1.6  christos 			if { $_debug_ranges_64_bit } then {
   1248  1.6  christos 			    _op .8byte $start "Start Address"
   1249  1.6  christos 			    _op .8byte $end "End Address"
   1250  1.6  christos 			} else {
   1251  1.6  christos 			    _op .4byte $start "Start Address"
   1252  1.6  christos 			    _op .4byte $end "End Address"
   1253  1.6  christos 			}
   1254  1.6  christos 		    }
   1255  1.6  christos 		    default { error "unknown range type: $type " }
   1256  1.6  christos 		}
   1257  1.6  christos 	    }
   1258  1.6  christos 
   1259  1.6  christos 	    # End of the sequence.
   1260  1.6  christos 	    if { $_debug_ranges_64_bit } then {
   1261  1.6  christos 		_op .8byte 0x0 "End of Sequence Marker (Part 1)"
   1262  1.6  christos 		_op .8byte 0x0 "End of Sequence Marker (Part 2)"
   1263  1.6  christos 	    } else {
   1264  1.6  christos 		_op .4byte 0x0 "End of Sequence Marker (Part 1)"
   1265  1.6  christos 		_op .4byte 0x0 "End of Sequence Marker (Part 2)"
   1266  1.6  christos 	    }
   1267  1.6  christos 	}
   1268  1.6  christos 
   1269  1.6  christos 	uplevel $body
   1270  1.6  christos     }
   1271  1.6  christos 
   1272  1.6  christos 
   1273  1.5  christos     # Emit a DWARF .debug_line unit.
   1274  1.5  christos     # OPTIONS is a list with an even number of elements containing
   1275  1.5  christos     # option-name and option-value pairs.
   1276  1.5  christos     # Current options are:
   1277  1.5  christos     # is_64 0|1    - boolean indicating if you want to emit 64-bit DWARF
   1278  1.5  christos     #                default = 0 (32-bit)
   1279  1.5  christos     # version n    - DWARF version number to emit
   1280  1.5  christos     #                default = 4
   1281  1.5  christos     # addr_size n  - the size of addresses, 32, 64, or default
   1282  1.5  christos     #                default = default
   1283  1.5  christos     #
   1284  1.5  christos     # LABEL is the label of the current unit (which is probably
   1285  1.5  christos     # referenced by a DW_AT_stmt_list), or "" if there is no such
   1286  1.5  christos     # label.
   1287  1.5  christos     #
   1288  1.5  christos     # BODY is Tcl code that emits the parts which make up the body of
   1289  1.5  christos     # the line unit.  It is evaluated in the caller's context.  The
   1290  1.5  christos     # following commands are available for the BODY section:
   1291  1.5  christos     #
   1292  1.5  christos     #   include_dir "dirname" -- adds a new include directory
   1293  1.5  christos     #
   1294  1.5  christos     #   file_name "file.c" idx -- adds a new file name.  IDX is a
   1295  1.5  christos     #   1-based index referencing an include directory or 0 for
   1296  1.5  christos     #   current directory.
   1297  1.5  christos 
   1298  1.5  christos     proc lines {options label body} {
   1299  1.5  christos 	variable _line_count
   1300  1.5  christos 	variable _line_saw_file
   1301  1.6  christos 	variable _line_saw_program
   1302  1.6  christos 	variable _line_header_end_label
   1303  1.5  christos 
   1304  1.5  christos 	# Establish the defaults.
   1305  1.5  christos 	set is_64 0
   1306  1.5  christos 	set _unit_version 4
   1307  1.5  christos 	set _unit_addr_size default
   1308  1.5  christos 
   1309  1.5  christos 	foreach { name value } $options {
   1310  1.5  christos 	    switch -exact -- $name {
   1311  1.5  christos 		is_64 { set is_64 $value }
   1312  1.5  christos 		version { set _unit_version $value }
   1313  1.5  christos 		addr_size { set _unit_addr_size $value }
   1314  1.5  christos 		default { error "unknown option $name" }
   1315  1.5  christos 	    }
   1316  1.5  christos 	}
   1317  1.5  christos 	if {$_unit_addr_size == "default"} {
   1318  1.5  christos 	    if {[is_64_target]} {
   1319  1.5  christos 		set _unit_addr_size 8
   1320  1.5  christos 	    } else {
   1321  1.5  christos 		set _unit_addr_size 4
   1322  1.5  christos 	    }
   1323  1.5  christos 	}
   1324  1.5  christos 
   1325  1.5  christos 	set unit_num [incr _line_count]
   1326  1.5  christos 
   1327  1.5  christos 	set section ".debug_line"
   1328  1.5  christos 	_section $section
   1329  1.5  christos 
   1330  1.5  christos 	if { "$label" != "" } {
   1331  1.5  christos 	    # Define the user-provided label at this point.
   1332  1.5  christos 	    $label:
   1333  1.5  christos 	}
   1334  1.5  christos 
   1335  1.5  christos 	set unit_len_label [_compute_label "line${_line_count}_start"]
   1336  1.5  christos 	set unit_end_label [_compute_label "line${_line_count}_end"]
   1337  1.5  christos 	set header_len_label [_compute_label "line${_line_count}_header_start"]
   1338  1.6  christos 	set _line_header_end_label [_compute_label "line${_line_count}_header_end"]
   1339  1.5  christos 
   1340  1.5  christos 	if {$is_64} {
   1341  1.5  christos 	    _op .4byte 0xffffffff
   1342  1.5  christos 	    _op .8byte "$unit_end_label - $unit_len_label" "unit_length"
   1343  1.5  christos 	} else {
   1344  1.5  christos 	    _op .4byte "$unit_end_label - $unit_len_label" "unit_length"
   1345  1.5  christos 	}
   1346  1.5  christos 
   1347  1.5  christos 	define_label $unit_len_label
   1348  1.5  christos 
   1349  1.5  christos 	_op .2byte $_unit_version version
   1350  1.5  christos 
   1351  1.5  christos 	if {$is_64} {
   1352  1.6  christos 	    _op .8byte "$_line_header_end_label - $header_len_label" "header_length"
   1353  1.5  christos 	} else {
   1354  1.6  christos 	    _op .4byte "$_line_header_end_label - $header_len_label" "header_length"
   1355  1.5  christos 	}
   1356  1.5  christos 
   1357  1.5  christos 	define_label $header_len_label
   1358  1.5  christos 
   1359  1.5  christos 	_op .byte 1 "minimum_instruction_length"
   1360  1.6  christos 	_op .byte 1 "default_is_stmt"
   1361  1.5  christos 	_op .byte 1 "line_base"
   1362  1.5  christos 	_op .byte 1 "line_range"
   1363  1.6  christos 	_op .byte 10 "opcode_base"
   1364  1.6  christos 
   1365  1.6  christos 	# The standard_opcode_lengths table.  The number of arguments
   1366  1.6  christos 	# for each of the standard opcodes.  Generating 9 entries here
   1367  1.6  christos 	# matches the use of 10 in the opcode_base above.  These 9
   1368  1.6  christos 	# entries match the 9 standard opcodes for DWARF2, making use
   1369  1.6  christos 	# of only 9 should be fine, even if we are generating DWARF3
   1370  1.6  christos 	# or DWARF4.
   1371  1.6  christos 	_op .byte 0 "standard opcode 1"
   1372  1.6  christos 	_op .byte 1 "standard opcode 2"
   1373  1.6  christos 	_op .byte 1 "standard opcode 3"
   1374  1.6  christos 	_op .byte 1 "standard opcode 4"
   1375  1.6  christos 	_op .byte 1 "standard opcode 5"
   1376  1.6  christos 	_op .byte 0 "standard opcode 6"
   1377  1.6  christos 	_op .byte 0 "standard opcode 7"
   1378  1.6  christos 	_op .byte 0 "standard opcode 8"
   1379  1.6  christos 	_op .byte 1 "standard opcode 9"
   1380  1.5  christos 
   1381  1.5  christos 	proc include_dir {dirname} {
   1382  1.5  christos 	    _op .ascii [_quote $dirname]
   1383  1.5  christos 	}
   1384  1.5  christos 
   1385  1.5  christos 	proc file_name {filename diridx} {
   1386  1.5  christos 	    variable _line_saw_file
   1387  1.5  christos 	    if "! $_line_saw_file" {
   1388  1.5  christos 		# Terminate the dir list.
   1389  1.5  christos 		_op .byte 0 "Terminator."
   1390  1.5  christos 		set _line_saw_file 1
   1391  1.5  christos 	    }
   1392  1.5  christos 
   1393  1.5  christos 	    _op .ascii [_quote $filename]
   1394  1.5  christos 	    _op .sleb128 $diridx
   1395  1.5  christos 	    _op .sleb128 0 "mtime"
   1396  1.5  christos 	    _op .sleb128 0 "length"
   1397  1.5  christos 	}
   1398  1.5  christos 
   1399  1.6  christos 	proc program {statements} {
   1400  1.6  christos 	    variable _line_saw_program
   1401  1.6  christos 	    variable _line_header_end_label
   1402  1.6  christos 
   1403  1.6  christos 	    if "! $_line_saw_program" {
   1404  1.6  christos 		# Terminate the file list.
   1405  1.6  christos 		_op .byte 0 "Terminator."
   1406  1.6  christos 		define_label $_line_header_end_label
   1407  1.6  christos 		set _line_saw_program 1
   1408  1.6  christos 	    }
   1409  1.6  christos 
   1410  1.6  christos 	    proc DW_LNE_set_address {addr} {
   1411  1.6  christos 		_op .byte 0
   1412  1.6  christos 		set start [new_label "set_address_start"]
   1413  1.6  christos 		set end [new_label "set_address_end"]
   1414  1.6  christos 		_op .uleb128 "${end} - ${start}"
   1415  1.6  christos 		define_label ${start}
   1416  1.6  christos 		_op .byte 2
   1417  1.6  christos 		if {[is_64_target]} {
   1418  1.6  christos 		    _op .8byte ${addr}
   1419  1.6  christos 		} else {
   1420  1.6  christos 		    _op .4byte ${addr}
   1421  1.6  christos 		}
   1422  1.6  christos 		define_label ${end}
   1423  1.6  christos 	    }
   1424  1.6  christos 
   1425  1.6  christos 	    proc DW_LNE_end_sequence {} {
   1426  1.6  christos 		_op .byte 0
   1427  1.6  christos 		_op .uleb128 1
   1428  1.6  christos 		_op .byte 1
   1429  1.6  christos 	    }
   1430  1.6  christos 
   1431  1.6  christos 	    proc DW_LNS_copy {} {
   1432  1.6  christos 		_op .byte 1
   1433  1.6  christos 	    }
   1434  1.6  christos 
   1435  1.6  christos 	    proc DW_LNS_advance_pc {offset} {
   1436  1.6  christos 		_op .byte 2
   1437  1.6  christos 		_op .uleb128 ${offset}
   1438  1.6  christos 	    }
   1439  1.6  christos 
   1440  1.6  christos 	    proc DW_LNS_advance_line {offset} {
   1441  1.6  christos 		_op .byte 3
   1442  1.6  christos 		_op .sleb128 ${offset}
   1443  1.6  christos 	    }
   1444  1.6  christos 
   1445  1.6  christos 	    foreach statement $statements {
   1446  1.6  christos 		uplevel 1 $statement
   1447  1.6  christos 	    }
   1448  1.6  christos 	}
   1449  1.6  christos 
   1450  1.5  christos 	uplevel $body
   1451  1.5  christos 
   1452  1.5  christos 	rename include_dir ""
   1453  1.5  christos 	rename file_name ""
   1454  1.5  christos 
   1455  1.5  christos 	# Terminate dir list if we saw no files.
   1456  1.5  christos 	if "! $_line_saw_file" {
   1457  1.5  christos 	    _op .byte 0 "Terminator."
   1458  1.5  christos 	}
   1459  1.5  christos 
   1460  1.5  christos 	# Terminate the file list.
   1461  1.6  christos 	if "! $_line_saw_program" {
   1462  1.6  christos 	    _op .byte 0 "Terminator."
   1463  1.6  christos 	    define_label $_line_header_end_label
   1464  1.6  christos 	}
   1465  1.5  christos 
   1466  1.5  christos 	define_label $unit_end_label
   1467  1.5  christos     }
   1468  1.5  christos 
   1469  1.1  christos     proc _empty_array {name} {
   1470  1.1  christos 	upvar $name the_array
   1471  1.1  christos 
   1472  1.1  christos 	catch {unset the_array}
   1473  1.1  christos 	set the_array(_) {}
   1474  1.1  christos 	unset the_array(_)
   1475  1.1  christos     }
   1476  1.1  christos 
   1477  1.1  christos     # Emit a .gnu_debugaltlink section with the given file name and
   1478  1.1  christos     # build-id.  The buildid should be represented as a hexadecimal
   1479  1.1  christos     # string, like "ffeeddcc".
   1480  1.1  christos     proc gnu_debugaltlink {filename buildid} {
   1481  1.1  christos 	_defer_output .gnu_debugaltlink {
   1482  1.1  christos 	    _op .ascii [_quote $filename]
   1483  1.1  christos 	    foreach {a b} [split $buildid {}] {
   1484  1.1  christos 		_op .byte 0x$a$b
   1485  1.1  christos 	    }
   1486  1.1  christos 	}
   1487  1.1  christos     }
   1488  1.1  christos 
   1489  1.1  christos     proc _note {type name hexdata} {
   1490  1.1  christos 	set namelen [expr [string length $name] + 1]
   1491  1.1  christos 
   1492  1.1  christos 	# Name size.
   1493  1.1  christos 	_op .4byte $namelen
   1494  1.1  christos 	# Data size.
   1495  1.1  christos 	_op .4byte [expr [string length $hexdata] / 2]
   1496  1.1  christos 	# Type.
   1497  1.1  christos 	_op .4byte $type
   1498  1.1  christos 	# The name.
   1499  1.1  christos 	_op .ascii [_quote $name]
   1500  1.1  christos 	# Alignment.
   1501  1.1  christos 	set align 2
   1502  1.6  christos 	set total [expr {($namelen + (1 << $align) - 1) & -(1 << $align)}]
   1503  1.1  christos 	for {set i $namelen} {$i < $total} {incr i} {
   1504  1.1  christos 	    _op .byte 0
   1505  1.1  christos 	}
   1506  1.1  christos 	# The data.
   1507  1.1  christos 	foreach {a b} [split $hexdata {}] {
   1508  1.1  christos 	    _op .byte 0x$a$b
   1509  1.1  christos 	}
   1510  1.1  christos     }
   1511  1.1  christos 
   1512  1.1  christos     # Emit a note section holding the given build-id.
   1513  1.1  christos     proc build_id {buildid} {
   1514  1.1  christos 	_defer_output {.note.gnu.build-id a note} {
   1515  1.1  christos 	    # From elf/common.h.
   1516  1.1  christos 	    set NT_GNU_BUILD_ID 3
   1517  1.1  christos 
   1518  1.1  christos 	    _note $NT_GNU_BUILD_ID GNU $buildid
   1519  1.1  christos 	}
   1520  1.1  christos     }
   1521  1.1  christos 
   1522  1.1  christos     # The top-level interface to the DWARF assembler.
   1523  1.1  christos     # FILENAME is the name of the file where the generated assembly
   1524  1.1  christos     # code is written.
   1525  1.1  christos     # BODY is Tcl code to emit the assembly.  It is evaluated via
   1526  1.1  christos     # "eval" -- not uplevel as you might expect, because it is
   1527  1.1  christos     # important to run the body in the Dwarf namespace.
   1528  1.1  christos     #
   1529  1.1  christos     # A typical invocation is something like:
   1530  1.1  christos     #    Dwarf::assemble $file {
   1531  1.1  christos     #        cu 0 2 8 {
   1532  1.1  christos     #            compile_unit {
   1533  1.1  christos     #            ...
   1534  1.1  christos     #            }
   1535  1.1  christos     #        }
   1536  1.1  christos     #        cu 0 2 8 {
   1537  1.1  christos     #        ...
   1538  1.1  christos     #        }
   1539  1.1  christos     #    }
   1540  1.1  christos     proc assemble {filename body} {
   1541  1.1  christos 	variable _initialized
   1542  1.1  christos 	variable _output_file
   1543  1.1  christos 	variable _deferred_output
   1544  1.1  christos 	variable _defer
   1545  1.1  christos 	variable _label_num
   1546  1.1  christos 	variable _strings
   1547  1.1  christos 	variable _cu_count
   1548  1.5  christos 	variable _line_count
   1549  1.5  christos 	variable _line_saw_file
   1550  1.6  christos 	variable _line_saw_program
   1551  1.6  christos 	variable _line_header_end_label
   1552  1.6  christos 	variable _debug_ranges_64_bit
   1553  1.1  christos 
   1554  1.1  christos 	if {!$_initialized} {
   1555  1.1  christos 	    _read_constants
   1556  1.1  christos 	    set _initialized 1
   1557  1.1  christos 	}
   1558  1.1  christos 
   1559  1.1  christos 	set _output_file [open $filename w]
   1560  1.1  christos 	set _cu_count 0
   1561  1.1  christos 	_empty_array _deferred_output
   1562  1.1  christos 	set _defer ""
   1563  1.1  christos 	set _label_num 0
   1564  1.1  christos 	_empty_array _strings
   1565  1.1  christos 
   1566  1.5  christos 	set _line_count 0
   1567  1.5  christos 	set _line_saw_file 0
   1568  1.6  christos 	set _line_saw_program 0
   1569  1.6  christos 	set _debug_ranges_64_bit [is_64_target]
   1570  1.5  christos 
   1571  1.1  christos 	# Not "uplevel" here, because we want to evaluate in this
   1572  1.1  christos 	# namespace.  This is somewhat bad because it means we can't
   1573  1.1  christos 	# readily refer to outer variables.
   1574  1.1  christos 	eval $body
   1575  1.1  christos 
   1576  1.1  christos 	_write_deferred_output
   1577  1.1  christos 
   1578  1.1  christos 	catch {close $_output_file}
   1579  1.1  christos 	set _output_file {}
   1580  1.1  christos     }
   1581  1.1  christos }
   1582