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