Home | History | Annotate | Line # | Download | only in lib
dwarf.exp revision 1.3
      1  1.3  christos # Copyright 2010-2015 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.1  christos     set result [gdb_compile $object_file $executable executable {nodebug}]
     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.3  christos     set test "x/2i $func+$func_length"
    150  1.3  christos     gdb_test_multiple $test $test {
    151  1.3  christos 	-re ".*($hex) <$func\\+$func_length>:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
    152  1.3  christos 	    set start $expect_out(1,string)
    153  1.3  christos 	    set end $expect_out(2,string)
    154  1.3  christos 
    155  1.3  christos 	    set func_length [expr $func_length + $end - $start]
    156  1.3  christos 	}
    157  1.3  christos     }
    158  1.3  christos 
    159  1.3  christos     return [list "${func}_label - $func_label_offset" $func_length]
    160  1.3  christos }
    161  1.3  christos 
    162  1.1  christos # A DWARF assembler.
    163  1.1  christos #
    164  1.1  christos # All the variables in this namespace are private to the
    165  1.1  christos # implementation.  Also, any procedure whose name starts with "_" is
    166  1.1  christos # private as well.  Do not use these.
    167  1.1  christos #
    168  1.1  christos # Exported functions are documented at their definition.
    169  1.1  christos #
    170  1.1  christos # In addition to the hand-written functions documented below, this
    171  1.1  christos # module automatically generates a function for each DWARF tag.  For
    172  1.1  christos # most tags, two forms are made: a full name, and one with the
    173  1.1  christos # "DW_TAG_" prefix stripped.  For example, you can use either
    174  1.1  christos # 'DW_TAG_compile_unit' or 'compile_unit' interchangeably.
    175  1.1  christos #
    176  1.1  christos # There are two exceptions to this rule: DW_TAG_variable and
    177  1.1  christos # DW_TAG_namespace.  For these, the full name must always be used,
    178  1.1  christos # as the short name conflicts with Tcl builtins.  (Should future
    179  1.1  christos # versions of Tcl or DWARF add more conflicts, this list will grow.
    180  1.1  christos # If you want to be safe you should always use the full names.)
    181  1.1  christos #
    182  1.1  christos # Each tag procedure is defined like:
    183  1.1  christos #
    184  1.1  christos # proc DW_TAG_mumble {{attrs {}} {children {}}} { ... }
    185  1.1  christos #
    186  1.1  christos # ATTRS is an optional list of attributes.
    187  1.1  christos # It is run through 'subst' in the caller's context before processing.
    188  1.1  christos #
    189  1.1  christos # Each attribute in the list has one of two forms:
    190  1.1  christos #   1. { NAME VALUE }
    191  1.1  christos #   2. { NAME VALUE FORM }
    192  1.1  christos #
    193  1.1  christos # In each case, NAME is the attribute's name.
    194  1.1  christos # This can either be the full name, like 'DW_AT_name', or a shortened
    195  1.1  christos # name, like 'name'.  These are fully equivalent.
    196  1.1  christos #
    197  1.3  christos # Besides DWARF standard attributes, assembler supports 'macro' attribute
    198  1.3  christos # which will be substituted by one or more standard or macro attributes.
    199  1.3  christos # supported macro attributes are:
    200  1.3  christos #
    201  1.3  christos #  - MACRO_AT_range { FUNC FILE }
    202  1.3  christos #  It is substituted by DW_AT_low_pc and DW_AT_high_pc with the start and
    203  1.3  christos #  end address of function FUNC in file FILE.
    204  1.3  christos #
    205  1.3  christos #  - MACRO_AT_func { FUNC FILE }
    206  1.3  christos #  It is substituted by DW_AT_name with FUNC and MACRO_AT_range.
    207  1.3  christos #
    208  1.1  christos # If FORM is given, it should name a DW_FORM_ constant.
    209  1.1  christos # This can either be the short form, like 'DW_FORM_addr', or a
    210  1.1  christos # shortened version, like 'addr'.  If the form is given, VALUE
    211  1.1  christos # is its value; see below.  In some cases, additional processing
    212  1.1  christos # is done; for example, DW_FORM_strp manages the .debug_str
    213  1.1  christos # section automatically.
    214  1.1  christos #
    215  1.1  christos # If FORM is 'SPECIAL_expr', then VALUE is treated as a location
    216  1.1  christos # expression.  The effective form is then DW_FORM_block, and VALUE
    217  1.1  christos # is passed to the (internal) '_location' proc to be translated.
    218  1.1  christos # This proc implements a miniature DW_OP_ assembler.
    219  1.1  christos #
    220  1.1  christos # If FORM is not given, it is guessed:
    221  1.1  christos # * If VALUE starts with the "@" character, the rest of VALUE is
    222  1.1  christos #   looked up as a DWARF constant, and DW_FORM_sdata is used.  For
    223  1.1  christos #   example, '@DW_LANG_c89' could be used.
    224  1.1  christos # * If VALUE starts with the ":" character, then it is a label
    225  1.1  christos #   reference.  The rest of VALUE is taken to be the name of a label,
    226  1.1  christos #   and DW_FORM_ref4 is used.  See 'new_label' and 'define_label'.
    227  1.1  christos # * Otherwise, VALUE is taken to be a string and DW_FORM_string is
    228  1.3  christos #   used.  In order to prevent bugs where a numeric value is given but
    229  1.3  christos #   no form is specified, it is an error if the value looks like a number
    230  1.3  christos #   (using Tcl's "string is integer") and no form is provided.
    231  1.1  christos # More form-guessing functionality may be added.
    232  1.1  christos #
    233  1.1  christos # CHILDREN is just Tcl code that can be used to define child DIEs.  It
    234  1.1  christos # is evaluated in the caller's context.
    235  1.1  christos #
    236  1.1  christos # Currently this code is missing nice support for CFA handling, and
    237  1.1  christos # probably other things as well.
    238  1.1  christos 
    239  1.1  christos namespace eval Dwarf {
    240  1.1  christos     # True if the module has been initialized.
    241  1.1  christos     variable _initialized 0
    242  1.1  christos 
    243  1.1  christos     # Constants from dwarf2.h.
    244  1.1  christos     variable _constants
    245  1.1  christos     # DW_AT short names.
    246  1.1  christos     variable _AT
    247  1.1  christos     # DW_FORM short names.
    248  1.1  christos     variable _FORM
    249  1.1  christos     # DW_OP short names.
    250  1.1  christos     variable _OP
    251  1.1  christos 
    252  1.1  christos     # The current output file.
    253  1.1  christos     variable _output_file
    254  1.1  christos 
    255  1.1  christos     # Note: The _cu_ values here also apply to type units (TUs).
    256  1.1  christos     # Think of a TU as a special kind of CU.
    257  1.1  christos 
    258  1.1  christos     # Current CU count.
    259  1.1  christos     variable _cu_count
    260  1.1  christos 
    261  1.1  christos     # The current CU's base label.
    262  1.1  christos     variable _cu_label
    263  1.1  christos 
    264  1.1  christos     # The current CU's version.
    265  1.1  christos     variable _cu_version
    266  1.1  christos 
    267  1.1  christos     # The current CU's address size.
    268  1.1  christos     variable _cu_addr_size
    269  1.1  christos     # The current CU's offset size.
    270  1.1  christos     variable _cu_offset_size
    271  1.1  christos 
    272  1.1  christos     # Label generation number.
    273  1.1  christos     variable _label_num
    274  1.1  christos 
    275  1.1  christos     # The deferred output array.  The index is the section name; the
    276  1.1  christos     # contents hold the data for that section.
    277  1.1  christos     variable _deferred_output
    278  1.1  christos 
    279  1.1  christos     # If empty, we should write directly to the output file.
    280  1.1  christos     # Otherwise, this is the name of a section to write to.
    281  1.1  christos     variable _defer
    282  1.1  christos 
    283  1.1  christos     # The abbrev section.  Typically .debug_abbrev but can be .debug_abbrev.dwo
    284  1.1  christos     # for Fission.
    285  1.1  christos     variable _abbrev_section
    286  1.1  christos 
    287  1.1  christos     # The next available abbrev number in the current CU's abbrev
    288  1.1  christos     # table.
    289  1.1  christos     variable _abbrev_num
    290  1.1  christos 
    291  1.1  christos     # The string table for this assembly.  The key is the string; the
    292  1.1  christos     # value is the label for that string.
    293  1.1  christos     variable _strings
    294  1.1  christos 
    295  1.1  christos     proc _process_one_constant {name value} {
    296  1.1  christos 	variable _constants
    297  1.1  christos 	variable _AT
    298  1.1  christos 	variable _FORM
    299  1.1  christos 	variable _OP
    300  1.1  christos 
    301  1.1  christos 	set _constants($name) $value
    302  1.1  christos 
    303  1.1  christos 	if {![regexp "^DW_(\[A-Z\]+)_(\[A-Za-z0-9_\]+)$" $name \
    304  1.1  christos 		  ignore prefix name2]} {
    305  1.1  christos 	    error "non-matching name: $name"
    306  1.1  christos 	}
    307  1.1  christos 
    308  1.1  christos 	if {$name2 == "lo_user" || $name2 == "hi_user"} {
    309  1.1  christos 	    return
    310  1.1  christos 	}
    311  1.1  christos 
    312  1.1  christos 	# We only try to shorten some very common things.
    313  1.1  christos 	# FIXME: CFA?
    314  1.1  christos 	switch -exact -- $prefix {
    315  1.1  christos 	    TAG {
    316  1.1  christos 		# Create two procedures for the tag.  These call
    317  1.1  christos 		# _handle_DW_TAG with the full tag name baked in; this
    318  1.1  christos 		# does all the actual work.
    319  1.1  christos 		proc $name {{attrs {}} {children {}}} \
    320  1.1  christos 		    "_handle_DW_TAG $name \$attrs \$children"
    321  1.1  christos 
    322  1.1  christos 		# Filter out ones that are known to clash.
    323  1.1  christos 		if {$name2 == "variable" || $name2 == "namespace"} {
    324  1.1  christos 		    set name2 "tag_$name2"
    325  1.1  christos 		}
    326  1.1  christos 
    327  1.1  christos 		if {[info commands $name2] != {}} {
    328  1.1  christos 		    error "duplicate proc name: from $name"
    329  1.1  christos 		}
    330  1.1  christos 
    331  1.1  christos 		proc $name2 {{attrs {}} {children {}}} \
    332  1.1  christos 		    "_handle_DW_TAG $name \$attrs \$children"
    333  1.1  christos 	    }
    334  1.1  christos 
    335  1.1  christos 	    AT {
    336  1.1  christos 		set _AT($name2) $name
    337  1.1  christos 	    }
    338  1.1  christos 
    339  1.1  christos 	    FORM {
    340  1.1  christos 		set _FORM($name2) $name
    341  1.1  christos 	    }
    342  1.1  christos 
    343  1.1  christos 	    OP {
    344  1.1  christos 		set _OP($name2) $name
    345  1.1  christos 	    }
    346  1.1  christos 
    347  1.1  christos 	    default {
    348  1.1  christos 		return
    349  1.1  christos 	    }
    350  1.1  christos 	}
    351  1.1  christos     }
    352  1.1  christos 
    353  1.1  christos     proc _read_constants {} {
    354  1.1  christos 	global srcdir hex decimal
    355  1.1  christos 	variable _constants
    356  1.1  christos 
    357  1.1  christos 	# DWARF name-matching regexp.
    358  1.1  christos 	set dwrx "DW_\[a-zA-Z0-9_\]+"
    359  1.1  christos 	# Whitespace regexp.
    360  1.1  christos 	set ws "\[ \t\]+"
    361  1.1  christos 
    362  1.1  christos 	set fd [open [file join $srcdir .. .. include dwarf2.h]]
    363  1.1  christos 	while {![eof $fd]} {
    364  1.1  christos 	    set line [gets $fd]
    365  1.1  christos 	    if {[regexp -- "^${ws}($dwrx)${ws}=${ws}($hex|$decimal),?$" \
    366  1.1  christos 		     $line ignore name value ignore2]} {
    367  1.1  christos 		_process_one_constant $name $value
    368  1.1  christos 	    }
    369  1.1  christos 	}
    370  1.1  christos 	close $fd
    371  1.1  christos 
    372  1.1  christos 	set fd [open [file join $srcdir .. .. include dwarf2.def]]
    373  1.1  christos 	while {![eof $fd]} {
    374  1.1  christos 	    set line [gets $fd]
    375  1.1  christos 	    if {[regexp -- \
    376  1.1  christos 		     "^DW_\[A-Z_\]+${ws}\\(($dwrx),${ws}($hex|$decimal)\\)$" \
    377  1.1  christos 		     $line ignore name value ignore2]} {
    378  1.1  christos 		_process_one_constant $name $value
    379  1.1  christos 	    }
    380  1.1  christos 	}
    381  1.1  christos 	close $fd
    382  1.1  christos 
    383  1.1  christos 	set _constants(SPECIAL_expr) $_constants(DW_FORM_block)
    384  1.1  christos     }
    385  1.1  christos 
    386  1.1  christos     proc _quote {string} {
    387  1.1  christos 	# FIXME
    388  1.1  christos 	return "\"${string}\\0\""
    389  1.1  christos     }
    390  1.1  christos 
    391  1.1  christos     proc _nz_quote {string} {
    392  1.1  christos 	# For now, no quoting is done.
    393  1.1  christos 	return "\"${string}\""
    394  1.1  christos     }
    395  1.1  christos 
    396  1.1  christos     proc _handle_DW_FORM {form value} {
    397  1.1  christos 	switch -exact -- $form {
    398  1.1  christos 	    DW_FORM_string  {
    399  1.1  christos 		_op .ascii [_quote $value]
    400  1.1  christos 	    }
    401  1.1  christos 
    402  1.1  christos 	    DW_FORM_flag_present {
    403  1.1  christos 		# We don't need to emit anything.
    404  1.1  christos 	    }
    405  1.1  christos 
    406  1.1  christos 	    DW_FORM_data4 -
    407  1.1  christos 	    DW_FORM_ref4 {
    408  1.1  christos 		_op .4byte $value
    409  1.1  christos 	    }
    410  1.1  christos 
    411  1.1  christos 	    DW_FORM_ref_addr {
    412  1.1  christos 		variable _cu_offset_size
    413  1.1  christos 		variable _cu_version
    414  1.1  christos 		variable _cu_addr_size
    415  1.1  christos 
    416  1.1  christos 		if {$_cu_version == 2} {
    417  1.1  christos 		    set size $_cu_addr_size
    418  1.1  christos 		} else {
    419  1.1  christos 		    set size $_cu_offset_size
    420  1.1  christos 		}
    421  1.1  christos 
    422  1.1  christos 		_op .${size}byte $value
    423  1.1  christos 	    }
    424  1.1  christos 
    425  1.1  christos 	    DW_FORM_ref1 -
    426  1.1  christos 	    DW_FORM_flag -
    427  1.1  christos 	    DW_FORM_data1 {
    428  1.1  christos 		_op .byte $value
    429  1.1  christos 	    }
    430  1.1  christos 
    431  1.1  christos 	    DW_FORM_sdata {
    432  1.1  christos 		_op .sleb128 $value
    433  1.1  christos 	    }
    434  1.1  christos 
    435  1.1  christos 	    DW_FORM_ref_udata -
    436  1.1  christos 	    DW_FORM_udata {
    437  1.1  christos 		_op .uleb128 $value
    438  1.1  christos 	    }
    439  1.1  christos 
    440  1.1  christos 	    DW_FORM_addr {
    441  1.1  christos 		variable _cu_addr_size
    442  1.1  christos 
    443  1.1  christos 		_op .${_cu_addr_size}byte $value
    444  1.1  christos 	    }
    445  1.1  christos 
    446  1.1  christos 	    DW_FORM_data2 -
    447  1.1  christos 	    DW_FORM_ref2 {
    448  1.1  christos 		_op .2byte $value
    449  1.1  christos 	    }
    450  1.1  christos 
    451  1.1  christos 	    DW_FORM_data8 -
    452  1.1  christos 	    DW_FORM_ref8 -
    453  1.1  christos 	    DW_FORM_ref_sig8 {
    454  1.1  christos 		_op .8byte $value
    455  1.1  christos 	    }
    456  1.1  christos 
    457  1.1  christos 	    DW_FORM_strp {
    458  1.1  christos 		variable _strings
    459  1.1  christos 		variable _cu_offset_size
    460  1.1  christos 
    461  1.1  christos 		if {![info exists _strings($value)]} {
    462  1.1  christos 		    set _strings($value) [new_label strp]
    463  1.1  christos 		    _defer_output .debug_string {
    464  1.1  christos 			define_label $_strings($value)
    465  1.1  christos 			_op .ascii [_quote $value]
    466  1.1  christos 		    }
    467  1.1  christos 		}
    468  1.1  christos 
    469  1.1  christos 		_op .${_cu_offset_size}byte $_strings($value) "strp: $value"
    470  1.1  christos 	    }
    471  1.1  christos 
    472  1.1  christos 	    SPECIAL_expr {
    473  1.1  christos 		set l1 [new_label "expr_start"]
    474  1.1  christos 		set l2 [new_label "expr_end"]
    475  1.1  christos 		_op .uleb128 "$l2 - $l1" "expression"
    476  1.1  christos 		define_label $l1
    477  1.1  christos 		_location $value
    478  1.1  christos 		define_label $l2
    479  1.1  christos 	    }
    480  1.1  christos 
    481  1.1  christos 	    DW_FORM_block1 {
    482  1.1  christos 		set len [string length $value]
    483  1.1  christos 		if {$len > 255} {
    484  1.1  christos 		    error "DW_FORM_block1 length too long"
    485  1.1  christos 		}
    486  1.1  christos 		_op .byte $len
    487  1.1  christos 		_op .ascii [_nz_quote $value]
    488  1.1  christos 	    }
    489  1.1  christos 
    490  1.1  christos 	    DW_FORM_block2 -
    491  1.1  christos 	    DW_FORM_block4 -
    492  1.1  christos 
    493  1.1  christos 	    DW_FORM_block -
    494  1.1  christos 
    495  1.1  christos 	    DW_FORM_ref2 -
    496  1.1  christos 	    DW_FORM_indirect -
    497  1.1  christos 	    DW_FORM_sec_offset -
    498  1.1  christos 	    DW_FORM_exprloc -
    499  1.1  christos 
    500  1.1  christos 	    DW_FORM_GNU_addr_index -
    501  1.1  christos 	    DW_FORM_GNU_str_index -
    502  1.1  christos 	    DW_FORM_GNU_ref_alt -
    503  1.1  christos 	    DW_FORM_GNU_strp_alt -
    504  1.1  christos 
    505  1.1  christos 	    default {
    506  1.1  christos 		error "unhandled form $form"
    507  1.1  christos 	    }
    508  1.1  christos 	}
    509  1.1  christos     }
    510  1.1  christos 
    511  1.1  christos     proc _guess_form {value varname} {
    512  1.1  christos 	upvar $varname new_value
    513  1.1  christos 
    514  1.1  christos 	switch -exact -- [string range $value 0 0] {
    515  1.1  christos 	    @ {
    516  1.1  christos 		# Constant reference.
    517  1.1  christos 		variable _constants
    518  1.1  christos 
    519  1.1  christos 		set new_value $_constants([string range $value 1 end])
    520  1.1  christos 		# Just the simplest.
    521  1.1  christos 		return DW_FORM_sdata
    522  1.1  christos 	    }
    523  1.1  christos 
    524  1.1  christos 	    : {
    525  1.1  christos 		# Label reference.
    526  1.1  christos 		variable _cu_label
    527  1.1  christos 
    528  1.1  christos 		set new_value "[string range $value 1 end] - $_cu_label"
    529  1.1  christos 
    530  1.1  christos 		return DW_FORM_ref4
    531  1.1  christos 	    }
    532  1.1  christos 
    533  1.1  christos 	    default {
    534  1.1  christos 		return DW_FORM_string
    535  1.1  christos 	    }
    536  1.1  christos 	}
    537  1.1  christos     }
    538  1.1  christos 
    539  1.1  christos     # Map NAME to its canonical form.
    540  1.1  christos     proc _map_name {name ary} {
    541  1.1  christos 	variable $ary
    542  1.1  christos 
    543  1.1  christos 	if {[info exists ${ary}($name)]} {
    544  1.1  christos 	    set name [set ${ary}($name)]
    545  1.1  christos 	}
    546  1.1  christos 
    547  1.1  christos 	return $name
    548  1.1  christos     }
    549  1.1  christos 
    550  1.3  christos     proc _handle_attribute { attr_name attr_value attr_form } {
    551  1.3  christos 	variable _abbrev_section
    552  1.3  christos 	variable _constants
    553  1.3  christos 
    554  1.3  christos 	_handle_DW_FORM $attr_form $attr_value
    555  1.3  christos 
    556  1.3  christos 	_defer_output $_abbrev_section {
    557  1.3  christos 	    _op .uleb128 $_constants($attr_name) $attr_name
    558  1.3  christos 	    _op .uleb128 $_constants($attr_form) $attr_form
    559  1.3  christos 	}
    560  1.3  christos     }
    561  1.3  christos 
    562  1.3  christos     # Handle macro attribute MACRO_AT_range.
    563  1.3  christos 
    564  1.3  christos     proc _handle_macro_at_range { attr_value } {
    565  1.3  christos 	if {[llength $attr_value] != 2} {
    566  1.3  christos 	    error "usage: MACRO_AT_range { func file }"
    567  1.3  christos 	}
    568  1.3  christos 
    569  1.3  christos 	set func [lindex $attr_value 0]
    570  1.3  christos 	set src [lindex $attr_value 1]
    571  1.3  christos 	set result [function_range $func $src]
    572  1.3  christos 
    573  1.3  christos 	_handle_attribute DW_AT_low_pc [lindex $result 0] \
    574  1.3  christos 	    DW_FORM_addr
    575  1.3  christos 	_handle_attribute DW_AT_high_pc \
    576  1.3  christos 	    "[lindex $result 0] + [lindex $result 1]" DW_FORM_addr
    577  1.3  christos     }
    578  1.3  christos 
    579  1.3  christos     # Handle macro attribute MACRO_AT_func.
    580  1.3  christos 
    581  1.3  christos     proc _handle_macro_at_func { attr_value } {
    582  1.3  christos 	if {[llength $attr_value] != 2} {
    583  1.3  christos 	    error "usage: MACRO_AT_func { func file }"
    584  1.3  christos 	}
    585  1.3  christos 	_handle_attribute DW_AT_name [lindex $attr_value 0] DW_FORM_string
    586  1.3  christos 	_handle_macro_at_range $attr_value
    587  1.3  christos     }
    588  1.3  christos 
    589  1.1  christos     proc _handle_DW_TAG {tag_name {attrs {}} {children {}}} {
    590  1.1  christos 	variable _abbrev_section
    591  1.1  christos 	variable _abbrev_num
    592  1.1  christos 	variable _constants
    593  1.1  christos 
    594  1.1  christos 	set has_children [expr {[string length $children] > 0}]
    595  1.1  christos 	set my_abbrev [incr _abbrev_num]
    596  1.1  christos 
    597  1.1  christos 	# We somewhat wastefully emit a new abbrev entry for each tag.
    598  1.1  christos 	# There's no reason for this other than laziness.
    599  1.1  christos 	_defer_output $_abbrev_section {
    600  1.1  christos 	    _op .uleb128 $my_abbrev "Abbrev start"
    601  1.1  christos 	    _op .uleb128 $_constants($tag_name) $tag_name
    602  1.1  christos 	    _op .byte $has_children "has_children"
    603  1.1  christos 	}
    604  1.1  christos 
    605  1.1  christos 	_op .uleb128 $my_abbrev "Abbrev ($tag_name)"
    606  1.1  christos 
    607  1.1  christos 	foreach attr $attrs {
    608  1.1  christos 	    set attr_name [_map_name [lindex $attr 0] _AT]
    609  1.1  christos 	    set attr_value [uplevel 2 [list subst [lindex $attr 1]]]
    610  1.3  christos 
    611  1.3  christos 	    if { [string equal "MACRO_AT_func" $attr_name] } {
    612  1.3  christos 		_handle_macro_at_func $attr_value
    613  1.3  christos 	    } elseif { [string equal "MACRO_AT_range" $attr_name] } {
    614  1.3  christos 		_handle_macro_at_range $attr_value
    615  1.1  christos 	    } else {
    616  1.3  christos 		if {[llength $attr] > 2} {
    617  1.3  christos 		    set attr_form [lindex $attr 2]
    618  1.3  christos 		} else {
    619  1.3  christos 		    # If the value looks like an integer, a form is required.
    620  1.3  christos 		    if [string is integer $attr_value] {
    621  1.3  christos 			error "Integer value requires a form"
    622  1.3  christos 		    }
    623  1.3  christos 		    set attr_form [_guess_form $attr_value attr_value]
    624  1.3  christos 		}
    625  1.3  christos 		set attr_form [_map_name $attr_form _FORM]
    626  1.1  christos 
    627  1.3  christos 		_handle_attribute $attr_name $attr_value $attr_form
    628  1.1  christos 	    }
    629  1.1  christos 	}
    630  1.1  christos 
    631  1.1  christos 	_defer_output $_abbrev_section {
    632  1.1  christos 	    # Terminator.
    633  1.1  christos 	    _op .byte 0x0 Terminator
    634  1.1  christos 	    _op .byte 0x0 Terminator
    635  1.1  christos 	}
    636  1.1  christos 
    637  1.1  christos 	if {$has_children} {
    638  1.1  christos 	    uplevel 2 $children
    639  1.1  christos 
    640  1.1  christos 	    # Terminate children.
    641  1.1  christos 	    _op .byte 0x0 "Terminate children"
    642  1.1  christos 	}
    643  1.1  christos     }
    644  1.1  christos 
    645  1.1  christos     proc _emit {string} {
    646  1.1  christos 	variable _output_file
    647  1.1  christos 	variable _defer
    648  1.1  christos 	variable _deferred_output
    649  1.1  christos 
    650  1.1  christos 	if {$_defer == ""} {
    651  1.1  christos 	    puts $_output_file $string
    652  1.1  christos 	} else {
    653  1.1  christos 	    append _deferred_output($_defer) ${string}\n
    654  1.1  christos 	}
    655  1.1  christos     }
    656  1.1  christos 
    657  1.1  christos     proc _section {name {flags ""} {type ""}} {
    658  1.1  christos 	if {$flags == "" && $type == ""} {
    659  1.1  christos 	    _emit "        .section $name"
    660  1.1  christos 	} elseif {$type == ""} {
    661  1.1  christos 	    _emit "        .section $name, \"$flags\""
    662  1.1  christos 	} else {
    663  1.1  christos 	    _emit "        .section $name, \"$flags\", %$type"
    664  1.1  christos 	}
    665  1.1  christos     }
    666  1.1  christos 
    667  1.1  christos     # SECTION_SPEC is a list of arguments to _section.
    668  1.1  christos     proc _defer_output {section_spec body} {
    669  1.1  christos 	variable _defer
    670  1.1  christos 	variable _deferred_output
    671  1.1  christos 
    672  1.1  christos 	set old_defer $_defer
    673  1.1  christos 	set _defer [lindex $section_spec 0]
    674  1.1  christos 
    675  1.1  christos 	if {![info exists _deferred_output($_defer)]} {
    676  1.1  christos 	    set _deferred_output($_defer) ""
    677  1.1  christos 	    eval _section $section_spec
    678  1.1  christos 	}
    679  1.1  christos 
    680  1.1  christos 	uplevel $body
    681  1.1  christos 
    682  1.1  christos 	set _defer $old_defer
    683  1.1  christos     }
    684  1.1  christos 
    685  1.1  christos     proc _defer_to_string {body} {
    686  1.1  christos 	variable _defer
    687  1.1  christos 	variable _deferred_output
    688  1.1  christos 
    689  1.1  christos 	set old_defer $_defer
    690  1.1  christos 	set _defer temp
    691  1.1  christos 
    692  1.1  christos 	set _deferred_output($_defer) ""
    693  1.1  christos 
    694  1.1  christos 	uplevel $body
    695  1.1  christos 
    696  1.1  christos 	set result $_deferred_output($_defer)
    697  1.1  christos 	unset _deferred_output($_defer)
    698  1.1  christos 
    699  1.1  christos 	set _defer $old_defer
    700  1.1  christos 	return $result
    701  1.1  christos     }
    702  1.1  christos 
    703  1.1  christos     proc _write_deferred_output {} {
    704  1.1  christos 	variable _output_file
    705  1.1  christos 	variable _deferred_output
    706  1.1  christos 
    707  1.1  christos 	foreach section [array names _deferred_output] {
    708  1.1  christos 	    # The data already has a newline.
    709  1.1  christos 	    puts -nonewline $_output_file $_deferred_output($section)
    710  1.1  christos 	}
    711  1.1  christos 
    712  1.1  christos 	# Save some memory.
    713  1.1  christos 	unset _deferred_output
    714  1.1  christos     }
    715  1.1  christos 
    716  1.1  christos     proc _op {name value {comment ""}} {
    717  1.1  christos 	set text "        ${name}        ${value}"
    718  1.1  christos 	if {$comment != ""} {
    719  1.1  christos 	    # Try to make stuff line up nicely.
    720  1.1  christos 	    while {[string length $text] < 40} {
    721  1.1  christos 		append text " "
    722  1.1  christos 	    }
    723  1.1  christos 	    append text "/* ${comment} */"
    724  1.1  christos 	}
    725  1.1  christos 	_emit $text
    726  1.1  christos     }
    727  1.1  christos 
    728  1.1  christos     proc _compute_label {name} {
    729  1.1  christos 	return ".L${name}"
    730  1.1  christos     }
    731  1.1  christos 
    732  1.1  christos     # Return a name suitable for use as a label.  If BASE_NAME is
    733  1.1  christos     # specified, it is incorporated into the label name; this is to
    734  1.1  christos     # make debugging the generated assembler easier.  If BASE_NAME is
    735  1.1  christos     # not specified a generic default is used.  This proc does not
    736  1.1  christos     # define the label; see 'define_label'.  'new_label' attempts to
    737  1.1  christos     # ensure that label names are unique.
    738  1.1  christos     proc new_label {{base_name label}} {
    739  1.1  christos 	variable _label_num
    740  1.1  christos 
    741  1.1  christos 	return [_compute_label ${base_name}[incr _label_num]]
    742  1.1  christos     }
    743  1.1  christos 
    744  1.1  christos     # Define a label named NAME.  Ordinarily, NAME comes from a call
    745  1.1  christos     # to 'new_label', but this is not required.
    746  1.1  christos     proc define_label {name} {
    747  1.1  christos 	_emit "${name}:"
    748  1.1  christos     }
    749  1.1  christos 
    750  1.1  christos     # Declare a global label.  This is typically used to refer to
    751  1.1  christos     # labels defined in other files, for example a function defined in
    752  1.1  christos     # a .c file.
    753  1.1  christos     proc extern {args} {
    754  1.1  christos 	foreach name $args {
    755  1.1  christos 	    _op .global $name
    756  1.1  christos 	}
    757  1.1  christos     }
    758  1.1  christos 
    759  1.1  christos     # A higher-level interface to label handling.
    760  1.1  christos     #
    761  1.1  christos     # ARGS is a list of label descriptors.  Each one is either a
    762  1.1  christos     # single element, or a list of two elements -- a name and some
    763  1.1  christos     # text.  For each descriptor, 'new_label' is invoked.  If the list
    764  1.1  christos     # form is used, the second element in the list is passed as an
    765  1.1  christos     # argument.  The label name is used to define a variable in the
    766  1.1  christos     # enclosing scope; this can be used to refer to the label later.
    767  1.1  christos     # The label name is also used to define a new proc whose name is
    768  1.1  christos     # the label name plus a trailing ":".  This proc takes a body as
    769  1.1  christos     # an argument and can be used to define the label at that point;
    770  1.1  christos     # then the body, if any, is evaluated in the caller's context.
    771  1.1  christos     #
    772  1.1  christos     # For example:
    773  1.1  christos     #
    774  1.1  christos     # declare_labels int_label
    775  1.1  christos     # something { ... $int_label }   ;# refer to the label
    776  1.1  christos     # int_label: constant { ... }    ;# define the label
    777  1.1  christos     proc declare_labels {args} {
    778  1.1  christos 	foreach arg $args {
    779  1.1  christos 	    set name [lindex $arg 0]
    780  1.1  christos 	    set text [lindex $arg 1]
    781  1.1  christos 
    782  1.1  christos 	    upvar $name label_var
    783  1.1  christos 	    if {$text == ""} {
    784  1.1  christos 		set label_var [new_label]
    785  1.1  christos 	    } else {
    786  1.1  christos 		set label_var [new_label $text]
    787  1.1  christos 	    }
    788  1.1  christos 
    789  1.1  christos 	    proc ${name}: {args} [format {
    790  1.1  christos 		define_label %s
    791  1.1  christos 		uplevel $args
    792  1.1  christos 	    } $label_var]
    793  1.1  christos 	}
    794  1.1  christos     }
    795  1.1  christos 
    796  1.1  christos     # This is a miniature assembler for location expressions.  It is
    797  1.1  christos     # suitable for use in the attributes to a DIE.  Its output is
    798  1.1  christos     # prefixed with "=" to make it automatically use DW_FORM_block.
    799  1.1  christos     # BODY is split by lines, and each line is taken to be a list.
    800  1.1  christos     # (FIXME should use 'info complete' here.)
    801  1.1  christos     # Each list's first element is the opcode, either short or long
    802  1.1  christos     # forms are accepted.
    803  1.1  christos     # FIXME argument handling
    804  1.1  christos     # FIXME move docs
    805  1.1  christos     proc _location {body} {
    806  1.1  christos 	variable _constants
    807  1.1  christos 	variable _cu_label
    808  1.1  christos 	variable _cu_addr_size
    809  1.1  christos 	variable _cu_offset_size
    810  1.1  christos 
    811  1.1  christos 	foreach line [split $body \n] {
    812  1.3  christos 	    # Ignore blank lines, and allow embedded comments.
    813  1.3  christos 	    if {[lindex $line 0] == "" || [regexp -- {^[ \t]*#} $line]} {
    814  1.1  christos 		continue
    815  1.1  christos 	    }
    816  1.1  christos 	    set opcode [_map_name [lindex $line 0] _OP]
    817  1.1  christos 	    _op .byte $_constants($opcode) $opcode
    818  1.1  christos 
    819  1.1  christos 	    switch -exact -- $opcode {
    820  1.1  christos 		DW_OP_addr {
    821  1.1  christos 		    _op .${_cu_addr_size}byte [lindex $line 1]
    822  1.1  christos 		}
    823  1.1  christos 
    824  1.3  christos 		DW_OP_pick -
    825  1.1  christos 		DW_OP_const1u -
    826  1.1  christos 		DW_OP_const1s {
    827  1.1  christos 		    _op .byte [lindex $line 1]
    828  1.1  christos 		}
    829  1.1  christos 
    830  1.1  christos 		DW_OP_const2u -
    831  1.1  christos 		DW_OP_const2s {
    832  1.1  christos 		    _op .2byte [lindex $line 1]
    833  1.1  christos 		}
    834  1.1  christos 
    835  1.1  christos 		DW_OP_const4u -
    836  1.1  christos 		DW_OP_const4s {
    837  1.1  christos 		    _op .4byte [lindex $line 1]
    838  1.1  christos 		}
    839  1.1  christos 
    840  1.1  christos 		DW_OP_const8u -
    841  1.1  christos 		DW_OP_const8s {
    842  1.1  christos 		    _op .8byte [lindex $line 1]
    843  1.1  christos 		}
    844  1.1  christos 
    845  1.1  christos 		DW_OP_constu {
    846  1.1  christos 		    _op .uleb128 [lindex $line 1]
    847  1.1  christos 		}
    848  1.1  christos 		DW_OP_consts {
    849  1.1  christos 		    _op .sleb128 [lindex $line 1]
    850  1.1  christos 		}
    851  1.1  christos 
    852  1.1  christos 		DW_OP_plus_uconst {
    853  1.1  christos 		    _op .uleb128 [lindex $line 1]
    854  1.1  christos 		}
    855  1.1  christos 
    856  1.1  christos 		DW_OP_piece {
    857  1.1  christos 		    _op .uleb128 [lindex $line 1]
    858  1.1  christos 		}
    859  1.1  christos 
    860  1.1  christos 		DW_OP_bit_piece {
    861  1.1  christos 		    _op .uleb128 [lindex $line 1]
    862  1.1  christos 		    _op .uleb128 [lindex $line 2]
    863  1.1  christos 		}
    864  1.1  christos 
    865  1.3  christos 		DW_OP_skip -
    866  1.3  christos 		DW_OP_bra {
    867  1.3  christos 		    _op .2byte [lindex $line 1]
    868  1.3  christos 		}
    869  1.3  christos 
    870  1.1  christos 		DW_OP_GNU_implicit_pointer {
    871  1.1  christos 		    if {[llength $line] != 3} {
    872  1.1  christos 			error "usage: DW_OP_GNU_implicit_pointer LABEL OFFSET"
    873  1.1  christos 		    }
    874  1.1  christos 
    875  1.1  christos 		    # Here label is a section offset.
    876  1.1  christos 		    set label [lindex $line 1]
    877  1.1  christos 		    _op .${_cu_offset_size}byte $label
    878  1.1  christos 		    _op .sleb128 [lindex $line 2]
    879  1.1  christos 		}
    880  1.1  christos 
    881  1.1  christos 		DW_OP_deref_size {
    882  1.1  christos 		    if {[llength $line] != 2} {
    883  1.1  christos 			error "usage: DW_OP_deref_size SIZE"
    884  1.1  christos 		    }
    885  1.1  christos 
    886  1.1  christos 		    _op .byte [lindex $line 1]
    887  1.1  christos 		}
    888  1.1  christos 
    889  1.1  christos 		default {
    890  1.1  christos 		    if {[llength $line] > 1} {
    891  1.1  christos 			error "Unimplemented: operands in location for $opcode"
    892  1.1  christos 		    }
    893  1.1  christos 		}
    894  1.1  christos 	    }
    895  1.1  christos 	}
    896  1.1  christos     }
    897  1.1  christos 
    898  1.1  christos     # Emit a DWARF CU.
    899  1.1  christos     # OPTIONS is a list with an even number of elements containing
    900  1.1  christos     # option-name and option-value pairs.
    901  1.1  christos     # Current options are:
    902  1.1  christos     # is_64 0|1    - boolean indicating if you want to emit 64-bit DWARF
    903  1.1  christos     #                default = 0 (32-bit)
    904  1.1  christos     # version n    - DWARF version number to emit
    905  1.1  christos     #                default = 4
    906  1.1  christos     # addr_size n  - the size of addresses, 32, 64, or default
    907  1.1  christos     #                default = default
    908  1.1  christos     # fission 0|1  - boolean indicating if generating Fission debug info
    909  1.1  christos     #                default = 0
    910  1.1  christos     # BODY is Tcl code that emits the DIEs which make up the body of
    911  1.1  christos     # the CU.  It is evaluated in the caller's context.
    912  1.1  christos     proc cu {options body} {
    913  1.1  christos 	variable _cu_count
    914  1.1  christos 	variable _abbrev_section
    915  1.1  christos 	variable _abbrev_num
    916  1.1  christos 	variable _cu_label
    917  1.1  christos 	variable _cu_version
    918  1.1  christos 	variable _cu_addr_size
    919  1.1  christos 	variable _cu_offset_size
    920  1.1  christos 
    921  1.1  christos 	# Establish the defaults.
    922  1.1  christos 	set is_64 0
    923  1.1  christos 	set _cu_version 4
    924  1.1  christos 	set _cu_addr_size default
    925  1.1  christos 	set fission 0
    926  1.1  christos 	set section ".debug_info"
    927  1.1  christos 	set _abbrev_section ".debug_abbrev"
    928  1.1  christos 
    929  1.1  christos 	foreach { name value } $options {
    930  1.1  christos 	    switch -exact -- $name {
    931  1.1  christos 		is_64 { set is_64 $value }
    932  1.1  christos 		version { set _cu_version $value }
    933  1.1  christos 		addr_size { set _cu_addr_size $value }
    934  1.1  christos 		fission { set fission $value }
    935  1.1  christos 		default { error "unknown option $name" }
    936  1.1  christos 	    }
    937  1.1  christos 	}
    938  1.1  christos 	if {$_cu_addr_size == "default"} {
    939  1.1  christos 	    if {[is_64_target]} {
    940  1.1  christos 		set _cu_addr_size 8
    941  1.1  christos 	    } else {
    942  1.1  christos 		set _cu_addr_size 4
    943  1.1  christos 	    }
    944  1.1  christos 	}
    945  1.1  christos 	set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
    946  1.1  christos 	if { $fission } {
    947  1.1  christos 	    set section ".debug_info.dwo"
    948  1.1  christos 	    set _abbrev_section ".debug_abbrev.dwo"
    949  1.1  christos 	}
    950  1.1  christos 
    951  1.1  christos 	_section $section
    952  1.1  christos 
    953  1.1  christos 	set cu_num [incr _cu_count]
    954  1.1  christos 	set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
    955  1.1  christos 	set _abbrev_num 1
    956  1.1  christos 
    957  1.1  christos 	set _cu_label [_compute_label "cu${cu_num}_begin"]
    958  1.1  christos 	set start_label [_compute_label "cu${cu_num}_start"]
    959  1.1  christos 	set end_label [_compute_label "cu${cu_num}_end"]
    960  1.1  christos 
    961  1.1  christos 	define_label $_cu_label
    962  1.1  christos 	if {$is_64} {
    963  1.1  christos 	    _op .4byte 0xffffffff
    964  1.1  christos 	    _op .8byte "$end_label - $start_label"
    965  1.1  christos 	} else {
    966  1.1  christos 	    _op .4byte "$end_label - $start_label"
    967  1.1  christos 	}
    968  1.1  christos 	define_label $start_label
    969  1.1  christos 	_op .2byte $_cu_version Version
    970  1.3  christos 	_op .${_cu_offset_size}byte $my_abbrevs Abbrevs
    971  1.1  christos 	_op .byte $_cu_addr_size "Pointer size"
    972  1.1  christos 
    973  1.1  christos 	_defer_output $_abbrev_section {
    974  1.1  christos 	    define_label $my_abbrevs
    975  1.1  christos 	}
    976  1.1  christos 
    977  1.1  christos 	uplevel $body
    978  1.1  christos 
    979  1.1  christos 	_defer_output $_abbrev_section {
    980  1.1  christos 	    # Emit the terminator.
    981  1.1  christos 	    _op .byte 0x0 Terminator
    982  1.1  christos 	    _op .byte 0x0 Terminator
    983  1.1  christos 	}
    984  1.1  christos 
    985  1.1  christos 	define_label $end_label
    986  1.1  christos     }
    987  1.1  christos 
    988  1.1  christos     # Emit a DWARF TU.
    989  1.1  christos     # OPTIONS is a list with an even number of elements containing
    990  1.1  christos     # option-name and option-value pairs.
    991  1.1  christos     # Current options are:
    992  1.1  christos     # is_64 0|1    - boolean indicating if you want to emit 64-bit DWARF
    993  1.1  christos     #                default = 0 (32-bit)
    994  1.1  christos     # version n    - DWARF version number to emit
    995  1.1  christos     #                default = 4
    996  1.1  christos     # addr_size n  - the size of addresses, 32, 64, or default
    997  1.1  christos     #                default = default
    998  1.1  christos     # fission 0|1  - boolean indicating if generating Fission debug info
    999  1.1  christos     #                default = 0
   1000  1.1  christos     # SIGNATURE is the 64-bit signature of the type.
   1001  1.1  christos     # TYPE_LABEL is the label of the type defined by this TU,
   1002  1.1  christos     # or "" if there is no type (i.e., type stubs in Fission).
   1003  1.1  christos     # BODY is Tcl code that emits the DIEs which make up the body of
   1004  1.1  christos     # the TU.  It is evaluated in the caller's context.
   1005  1.1  christos     proc tu {options signature type_label body} {
   1006  1.1  christos 	variable _cu_count
   1007  1.1  christos 	variable _abbrev_section
   1008  1.1  christos 	variable _abbrev_num
   1009  1.1  christos 	variable _cu_label
   1010  1.1  christos 	variable _cu_version
   1011  1.1  christos 	variable _cu_addr_size
   1012  1.1  christos 	variable _cu_offset_size
   1013  1.1  christos 
   1014  1.1  christos 	# Establish the defaults.
   1015  1.1  christos 	set is_64 0
   1016  1.1  christos 	set _cu_version 4
   1017  1.1  christos 	set _cu_addr_size default
   1018  1.1  christos 	set fission 0
   1019  1.1  christos 	set section ".debug_types"
   1020  1.1  christos 	set _abbrev_section ".debug_abbrev"
   1021  1.1  christos 
   1022  1.1  christos 	foreach { name value } $options {
   1023  1.1  christos 	    switch -exact -- $name {
   1024  1.1  christos 		is_64 { set is_64 $value }
   1025  1.1  christos 		version { set _cu_version $value }
   1026  1.1  christos 		addr_size { set _cu_addr_size $value }
   1027  1.1  christos 		fission { set fission $value }
   1028  1.1  christos 		default { error "unknown option $name" }
   1029  1.1  christos 	    }
   1030  1.1  christos 	}
   1031  1.1  christos 	if {$_cu_addr_size == "default"} {
   1032  1.1  christos 	    if {[is_64_target]} {
   1033  1.1  christos 		set _cu_addr_size 8
   1034  1.1  christos 	    } else {
   1035  1.1  christos 		set _cu_addr_size 4
   1036  1.1  christos 	    }
   1037  1.1  christos 	}
   1038  1.1  christos 	set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
   1039  1.1  christos 	if { $fission } {
   1040  1.1  christos 	    set section ".debug_types.dwo"
   1041  1.1  christos 	    set _abbrev_section ".debug_abbrev.dwo"
   1042  1.1  christos 	}
   1043  1.1  christos 
   1044  1.1  christos 	_section $section
   1045  1.1  christos 
   1046  1.1  christos 	set cu_num [incr _cu_count]
   1047  1.1  christos 	set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
   1048  1.1  christos 	set _abbrev_num 1
   1049  1.1  christos 
   1050  1.1  christos 	set _cu_label [_compute_label "cu${cu_num}_begin"]
   1051  1.1  christos 	set start_label [_compute_label "cu${cu_num}_start"]
   1052  1.1  christos 	set end_label [_compute_label "cu${cu_num}_end"]
   1053  1.1  christos 
   1054  1.1  christos 	define_label $_cu_label
   1055  1.1  christos 	if {$is_64} {
   1056  1.1  christos 	    _op .4byte 0xffffffff
   1057  1.1  christos 	    _op .8byte "$end_label - $start_label"
   1058  1.1  christos 	} else {
   1059  1.1  christos 	    _op .4byte "$end_label - $start_label"
   1060  1.1  christos 	}
   1061  1.1  christos 	define_label $start_label
   1062  1.1  christos 	_op .2byte $_cu_version Version
   1063  1.3  christos 	_op .${_cu_offset_size}byte $my_abbrevs Abbrevs
   1064  1.1  christos 	_op .byte $_cu_addr_size "Pointer size"
   1065  1.1  christos 	_op .8byte $signature Signature
   1066  1.1  christos 	if { $type_label != "" } {
   1067  1.1  christos 	    uplevel declare_labels $type_label
   1068  1.1  christos 	    upvar $type_label my_type_label
   1069  1.1  christos 	    if {$is_64} {
   1070  1.1  christos 		_op .8byte "$my_type_label - $_cu_label"
   1071  1.1  christos 	    } else {
   1072  1.1  christos 		_op .4byte "$my_type_label - $_cu_label"
   1073  1.1  christos 	    }
   1074  1.1  christos 	} else {
   1075  1.1  christos 	    if {$is_64} {
   1076  1.1  christos 		_op .8byte 0
   1077  1.1  christos 	    } else {
   1078  1.1  christos 		_op .4byte 0
   1079  1.1  christos 	    }
   1080  1.1  christos 	}
   1081  1.1  christos 
   1082  1.1  christos 	_defer_output $_abbrev_section {
   1083  1.1  christos 	    define_label $my_abbrevs
   1084  1.1  christos 	}
   1085  1.1  christos 
   1086  1.1  christos 	uplevel $body
   1087  1.1  christos 
   1088  1.1  christos 	_defer_output $_abbrev_section {
   1089  1.1  christos 	    # Emit the terminator.
   1090  1.1  christos 	    _op .byte 0x0 Terminator
   1091  1.1  christos 	    _op .byte 0x0 Terminator
   1092  1.1  christos 	}
   1093  1.1  christos 
   1094  1.1  christos 	define_label $end_label
   1095  1.1  christos     }
   1096  1.1  christos 
   1097  1.1  christos     proc _empty_array {name} {
   1098  1.1  christos 	upvar $name the_array
   1099  1.1  christos 
   1100  1.1  christos 	catch {unset the_array}
   1101  1.1  christos 	set the_array(_) {}
   1102  1.1  christos 	unset the_array(_)
   1103  1.1  christos     }
   1104  1.1  christos 
   1105  1.1  christos     # Emit a .gnu_debugaltlink section with the given file name and
   1106  1.1  christos     # build-id.  The buildid should be represented as a hexadecimal
   1107  1.1  christos     # string, like "ffeeddcc".
   1108  1.1  christos     proc gnu_debugaltlink {filename buildid} {
   1109  1.1  christos 	_defer_output .gnu_debugaltlink {
   1110  1.1  christos 	    _op .ascii [_quote $filename]
   1111  1.1  christos 	    foreach {a b} [split $buildid {}] {
   1112  1.1  christos 		_op .byte 0x$a$b
   1113  1.1  christos 	    }
   1114  1.1  christos 	}
   1115  1.1  christos     }
   1116  1.1  christos 
   1117  1.1  christos     proc _note {type name hexdata} {
   1118  1.1  christos 	set namelen [expr [string length $name] + 1]
   1119  1.1  christos 
   1120  1.1  christos 	# Name size.
   1121  1.1  christos 	_op .4byte $namelen
   1122  1.1  christos 	# Data size.
   1123  1.1  christos 	_op .4byte [expr [string length $hexdata] / 2]
   1124  1.1  christos 	# Type.
   1125  1.1  christos 	_op .4byte $type
   1126  1.1  christos 	# The name.
   1127  1.1  christos 	_op .ascii [_quote $name]
   1128  1.1  christos 	# Alignment.
   1129  1.1  christos 	set align 2
   1130  1.1  christos 	set total [expr {($namelen + (1 << $align) - 1) & (-1 << $align)}]
   1131  1.1  christos 	for {set i $namelen} {$i < $total} {incr i} {
   1132  1.1  christos 	    _op .byte 0
   1133  1.1  christos 	}
   1134  1.1  christos 	# The data.
   1135  1.1  christos 	foreach {a b} [split $hexdata {}] {
   1136  1.1  christos 	    _op .byte 0x$a$b
   1137  1.1  christos 	}
   1138  1.1  christos     }
   1139  1.1  christos 
   1140  1.1  christos     # Emit a note section holding the given build-id.
   1141  1.1  christos     proc build_id {buildid} {
   1142  1.1  christos 	_defer_output {.note.gnu.build-id a note} {
   1143  1.1  christos 	    # From elf/common.h.
   1144  1.1  christos 	    set NT_GNU_BUILD_ID 3
   1145  1.1  christos 
   1146  1.1  christos 	    _note $NT_GNU_BUILD_ID GNU $buildid
   1147  1.1  christos 	}
   1148  1.1  christos     }
   1149  1.1  christos 
   1150  1.1  christos     # The top-level interface to the DWARF assembler.
   1151  1.1  christos     # FILENAME is the name of the file where the generated assembly
   1152  1.1  christos     # code is written.
   1153  1.1  christos     # BODY is Tcl code to emit the assembly.  It is evaluated via
   1154  1.1  christos     # "eval" -- not uplevel as you might expect, because it is
   1155  1.1  christos     # important to run the body in the Dwarf namespace.
   1156  1.1  christos     #
   1157  1.1  christos     # A typical invocation is something like:
   1158  1.1  christos     #    Dwarf::assemble $file {
   1159  1.1  christos     #        cu 0 2 8 {
   1160  1.1  christos     #            compile_unit {
   1161  1.1  christos     #            ...
   1162  1.1  christos     #            }
   1163  1.1  christos     #        }
   1164  1.1  christos     #        cu 0 2 8 {
   1165  1.1  christos     #        ...
   1166  1.1  christos     #        }
   1167  1.1  christos     #    }
   1168  1.1  christos     proc assemble {filename body} {
   1169  1.1  christos 	variable _initialized
   1170  1.1  christos 	variable _output_file
   1171  1.1  christos 	variable _deferred_output
   1172  1.1  christos 	variable _defer
   1173  1.1  christos 	variable _label_num
   1174  1.1  christos 	variable _strings
   1175  1.1  christos 	variable _cu_count
   1176  1.1  christos 
   1177  1.1  christos 	if {!$_initialized} {
   1178  1.1  christos 	    _read_constants
   1179  1.1  christos 	    set _initialized 1
   1180  1.1  christos 	}
   1181  1.1  christos 
   1182  1.1  christos 	set _output_file [open $filename w]
   1183  1.1  christos 	set _cu_count 0
   1184  1.1  christos 	_empty_array _deferred_output
   1185  1.1  christos 	set _defer ""
   1186  1.1  christos 	set _label_num 0
   1187  1.1  christos 	_empty_array _strings
   1188  1.1  christos 
   1189  1.1  christos 	# Not "uplevel" here, because we want to evaluate in this
   1190  1.1  christos 	# namespace.  This is somewhat bad because it means we can't
   1191  1.1  christos 	# readily refer to outer variables.
   1192  1.1  christos 	eval $body
   1193  1.1  christos 
   1194  1.1  christos 	_write_deferred_output
   1195  1.1  christos 
   1196  1.1  christos 	catch {close $_output_file}
   1197  1.1  christos 	set _output_file {}
   1198  1.1  christos     }
   1199  1.1  christos }
   1200