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