Update copyright year range in all GDB files.
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / dwarf.exp
CommitLineData
b811d2c2 1# Copyright 2010-2020 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 569 % {
456ba0fa
TV
570 # Label reference, an offset from .debug_info.
571 set new_value "[string range $value 1 end]"
f13a9a0c
YQ
572
573 return DW_FORM_ref_addr
574 }
575
1d24041a
TT
576 default {
577 return DW_FORM_string
578 }
579 }
580 }
581
582 # Map NAME to its canonical form.
583 proc _map_name {name ary} {
584 variable $ary
585
586 if {[info exists ${ary}($name)]} {
587 set name [set ${ary}($name)]
588 }
589
590 return $name
591 }
592
02ad9cf1
YQ
593 proc _handle_attribute { attr_name attr_value attr_form } {
594 variable _abbrev_section
595 variable _constants
596
597 _handle_DW_FORM $attr_form $attr_value
598
599 _defer_output $_abbrev_section {
600 _op .uleb128 $_constants($attr_name) $attr_name
601 _op .uleb128 $_constants($attr_form) $attr_form
602 }
603 }
604
876c4df9
YQ
605 # Handle macro attribute MACRO_AT_range.
606
607 proc _handle_macro_at_range { attr_value } {
608 if {[llength $attr_value] != 2} {
609 error "usage: MACRO_AT_range { func file }"
610 }
611
612 set func [lindex $attr_value 0]
613 set src [lindex $attr_value 1]
614 set result [function_range $func $src]
615
616 _handle_attribute DW_AT_low_pc [lindex $result 0] \
617 DW_FORM_addr
618 _handle_attribute DW_AT_high_pc \
619 "[lindex $result 0] + [lindex $result 1]" DW_FORM_addr
620 }
621
622 # Handle macro attribute MACRO_AT_func.
623
624 proc _handle_macro_at_func { attr_value } {
625 if {[llength $attr_value] != 2} {
626 error "usage: MACRO_AT_func { func file }"
627 }
628 _handle_attribute DW_AT_name [lindex $attr_value 0] DW_FORM_string
629 _handle_macro_at_range $attr_value
630 }
631
1d24041a 632 proc _handle_DW_TAG {tag_name {attrs {}} {children {}}} {
6c9e2db4 633 variable _abbrev_section
1d24041a
TT
634 variable _abbrev_num
635 variable _constants
636
637 set has_children [expr {[string length $children] > 0}]
638 set my_abbrev [incr _abbrev_num]
639
640 # We somewhat wastefully emit a new abbrev entry for each tag.
641 # There's no reason for this other than laziness.
6c9e2db4 642 _defer_output $_abbrev_section {
1d24041a
TT
643 _op .uleb128 $my_abbrev "Abbrev start"
644 _op .uleb128 $_constants($tag_name) $tag_name
645 _op .byte $has_children "has_children"
646 }
647
648 _op .uleb128 $my_abbrev "Abbrev ($tag_name)"
649
650 foreach attr $attrs {
651 set attr_name [_map_name [lindex $attr 0] _AT]
2223449a
KB
652
653 # When the length of ATTR is greater than 2, the last
654 # element of the list must be a form. The second through
655 # the penultimate elements are joined together and
656 # evaluated using subst. This allows constructs such as
657 # [gdb_target_symbol foo] to be used.
658
659 if {[llength $attr] > 2} {
660 set attr_value [uplevel 2 [list subst [join [lrange $attr 1 end-1]]]]
661 } else {
662 set attr_value [uplevel 2 [list subst [lindex $attr 1]]]
663 }
876c4df9
YQ
664
665 if { [string equal "MACRO_AT_func" $attr_name] } {
666 _handle_macro_at_func $attr_value
667 } elseif { [string equal "MACRO_AT_range" $attr_name] } {
668 _handle_macro_at_range $attr_value
1d24041a 669 } else {
876c4df9 670 if {[llength $attr] > 2} {
f13a9a0c
YQ
671 set attr_form [uplevel 2 [list subst [lindex $attr end]]]
672
673 if { [string index $attr_value 0] == ":" } {
674 # It is a label, get its value.
675 _guess_form $attr_value attr_value
676 }
876c4df9 677 } else {
f2e0d4b4
DE
678 # If the value looks like an integer, a form is required.
679 if [string is integer $attr_value] {
680 error "Integer value requires a form"
681 }
876c4df9
YQ
682 set attr_form [_guess_form $attr_value attr_value]
683 }
684 set attr_form [_map_name $attr_form _FORM]
1d24041a 685
876c4df9
YQ
686 _handle_attribute $attr_name $attr_value $attr_form
687 }
1d24041a
TT
688 }
689
6c9e2db4 690 _defer_output $_abbrev_section {
1d24041a
TT
691 # Terminator.
692 _op .byte 0x0 Terminator
693 _op .byte 0x0 Terminator
694 }
695
696 if {$has_children} {
697 uplevel 2 $children
698
699 # Terminate children.
700 _op .byte 0x0 "Terminate children"
701 }
702 }
703
704 proc _emit {string} {
705 variable _output_file
706 variable _defer
707 variable _deferred_output
708
709 if {$_defer == ""} {
710 puts $_output_file $string
711 } else {
712 append _deferred_output($_defer) ${string}\n
713 }
714 }
715
dc294be5
TT
716 proc _section {name {flags ""} {type ""}} {
717 if {$flags == "" && $type == ""} {
718 _emit " .section $name"
719 } elseif {$type == ""} {
720 _emit " .section $name, \"$flags\""
721 } else {
722 _emit " .section $name, \"$flags\", %$type"
723 }
1d24041a
TT
724 }
725
dc294be5
TT
726 # SECTION_SPEC is a list of arguments to _section.
727 proc _defer_output {section_spec body} {
1d24041a
TT
728 variable _defer
729 variable _deferred_output
730
731 set old_defer $_defer
dc294be5 732 set _defer [lindex $section_spec 0]
1d24041a
TT
733
734 if {![info exists _deferred_output($_defer)]} {
735 set _deferred_output($_defer) ""
dc294be5 736 eval _section $section_spec
1d24041a
TT
737 }
738
739 uplevel $body
740
741 set _defer $old_defer
742 }
743
744 proc _defer_to_string {body} {
745 variable _defer
746 variable _deferred_output
747
748 set old_defer $_defer
749 set _defer temp
750
751 set _deferred_output($_defer) ""
752
753 uplevel $body
754
755 set result $_deferred_output($_defer)
756 unset _deferred_output($_defer)
757
758 set _defer $old_defer
759 return $result
760 }
761
762 proc _write_deferred_output {} {
763 variable _output_file
764 variable _deferred_output
765
766 foreach section [array names _deferred_output] {
767 # The data already has a newline.
768 puts -nonewline $_output_file $_deferred_output($section)
769 }
770
771 # Save some memory.
772 unset _deferred_output
773 }
774
775 proc _op {name value {comment ""}} {
776 set text " ${name} ${value}"
777 if {$comment != ""} {
778 # Try to make stuff line up nicely.
779 while {[string length $text] < 40} {
780 append text " "
781 }
782 append text "/* ${comment} */"
783 }
784 _emit $text
785 }
786
787 proc _compute_label {name} {
788 return ".L${name}"
789 }
790
791 # Return a name suitable for use as a label. If BASE_NAME is
792 # specified, it is incorporated into the label name; this is to
793 # make debugging the generated assembler easier. If BASE_NAME is
794 # not specified a generic default is used. This proc does not
795 # define the label; see 'define_label'. 'new_label' attempts to
796 # ensure that label names are unique.
797 proc new_label {{base_name label}} {
798 variable _label_num
799
800 return [_compute_label ${base_name}[incr _label_num]]
801 }
802
803 # Define a label named NAME. Ordinarily, NAME comes from a call
804 # to 'new_label', but this is not required.
805 proc define_label {name} {
806 _emit "${name}:"
807 }
808
809 # Declare a global label. This is typically used to refer to
810 # labels defined in other files, for example a function defined in
811 # a .c file.
812 proc extern {args} {
813 foreach name $args {
814 _op .global $name
815 }
816 }
817
818 # A higher-level interface to label handling.
819 #
820 # ARGS is a list of label descriptors. Each one is either a
821 # single element, or a list of two elements -- a name and some
822 # text. For each descriptor, 'new_label' is invoked. If the list
823 # form is used, the second element in the list is passed as an
824 # argument. The label name is used to define a variable in the
825 # enclosing scope; this can be used to refer to the label later.
826 # The label name is also used to define a new proc whose name is
827 # the label name plus a trailing ":". This proc takes a body as
828 # an argument and can be used to define the label at that point;
829 # then the body, if any, is evaluated in the caller's context.
830 #
831 # For example:
832 #
833 # declare_labels int_label
834 # something { ... $int_label } ;# refer to the label
835 # int_label: constant { ... } ;# define the label
836 proc declare_labels {args} {
837 foreach arg $args {
838 set name [lindex $arg 0]
839 set text [lindex $arg 1]
840
841 upvar $name label_var
842 if {$text == ""} {
843 set label_var [new_label]
844 } else {
845 set label_var [new_label $text]
846 }
847
848 proc ${name}: {args} [format {
849 define_label %s
850 uplevel $args
851 } $label_var]
852 }
853 }
854
855 # This is a miniature assembler for location expressions. It is
856 # suitable for use in the attributes to a DIE. Its output is
857 # prefixed with "=" to make it automatically use DW_FORM_block.
858 # BODY is split by lines, and each line is taken to be a list.
859 # (FIXME should use 'info complete' here.)
860 # Each list's first element is the opcode, either short or long
861 # forms are accepted.
862 # FIXME argument handling
863 # FIXME move docs
864 proc _location {body} {
865 variable _constants
b6807d98 866 variable _cu_label
5ac95241 867 variable _cu_version
b6807d98 868 variable _cu_addr_size
5bd1ef56 869 variable _cu_offset_size
1d24041a
TT
870
871 foreach line [split $body \n] {
4ff709eb
TT
872 # Ignore blank lines, and allow embedded comments.
873 if {[lindex $line 0] == "" || [regexp -- {^[ \t]*#} $line]} {
1d24041a
TT
874 continue
875 }
876 set opcode [_map_name [lindex $line 0] _OP]
877 _op .byte $_constants($opcode) $opcode
878
879 switch -exact -- $opcode {
880 DW_OP_addr {
1d24041a
TT
881 _op .${_cu_addr_size}byte [lindex $line 1]
882 }
883
0fde2c53
DE
884 DW_OP_regx {
885 _op .uleb128 [lindex $line 1]
886 }
887
4ff709eb 888 DW_OP_pick -
1d24041a
TT
889 DW_OP_const1u -
890 DW_OP_const1s {
891 _op .byte [lindex $line 1]
892 }
893
894 DW_OP_const2u -
895 DW_OP_const2s {
896 _op .2byte [lindex $line 1]
897 }
898
899 DW_OP_const4u -
900 DW_OP_const4s {
901 _op .4byte [lindex $line 1]
902 }
903
904 DW_OP_const8u -
905 DW_OP_const8s {
906 _op .8byte [lindex $line 1]
907 }
908
909 DW_OP_constu {
910 _op .uleb128 [lindex $line 1]
911 }
912 DW_OP_consts {
913 _op .sleb128 [lindex $line 1]
914 }
915
16b5a7cb
AB
916 DW_OP_plus_uconst {
917 _op .uleb128 [lindex $line 1]
918 }
919
5bd1ef56
TT
920 DW_OP_piece {
921 _op .uleb128 [lindex $line 1]
922 }
923
16b5a7cb
AB
924 DW_OP_bit_piece {
925 _op .uleb128 [lindex $line 1]
926 _op .uleb128 [lindex $line 2]
927 }
928
4ff709eb
TT
929 DW_OP_skip -
930 DW_OP_bra {
931 _op .2byte [lindex $line 1]
932 }
933
f13a9a0c
YQ
934 DW_OP_implicit_value {
935 set l1 [new_label "value_start"]
936 set l2 [new_label "value_end"]
937 _op .uleb128 "$l2 - $l1"
938 define_label $l1
939 foreach value [lrange $line 1 end] {
940 switch -regexp -- $value {
941 {^0x[[:xdigit:]]{1,2}$} {_op .byte $value}
942 {^0x[[:xdigit:]]{4}$} {_op .2byte $value}
943 {^0x[[:xdigit:]]{8}$} {_op .4byte $value}
944 {^0x[[:xdigit:]]{16}$} {_op .8byte $value}
945 default {
946 error "bad value '$value' in DW_OP_implicit_value"
947 }
948 }
949 }
950 define_label $l2
951 }
952
7942e96e 953 DW_OP_implicit_pointer -
b6807d98
TT
954 DW_OP_GNU_implicit_pointer {
955 if {[llength $line] != 3} {
7942e96e 956 error "usage: $opcode LABEL OFFSET"
b6807d98
TT
957 }
958
959 # Here label is a section offset.
960 set label [lindex $line 1]
5ac95241
YQ
961 if { $_cu_version == 2 } {
962 _op .${_cu_addr_size}byte $label
963 } else {
964 _op .${_cu_offset_size}byte $label
965 }
b6807d98
TT
966 _op .sleb128 [lindex $line 2]
967 }
968
ae3a7c47
KB
969 DW_OP_GNU_variable_value {
970 if {[llength $line] != 2} {
971 error "usage: $opcode LABEL"
972 }
973
974 # Here label is a section offset.
975 set label [lindex $line 1]
976 if { $_cu_version == 2 } {
977 _op .${_cu_addr_size}byte $label
978 } else {
979 _op .${_cu_offset_size}byte $label
980 }
981 }
982
b39a8faf
YQ
983 DW_OP_deref_size {
984 if {[llength $line] != 2} {
985 error "usage: DW_OP_deref_size SIZE"
986 }
987
988 _op .byte [lindex $line 1]
989 }
990
5f3ff4f8
JK
991 DW_OP_bregx {
992 _op .uleb128 [lindex $line 1]
993 _op .sleb128 [lindex $line 2]
994 }
995
1d24041a
TT
996 default {
997 if {[llength $line] > 1} {
998 error "Unimplemented: operands in location for $opcode"
999 }
1000 }
1001 }
1002 }
1003 }
1004
1005 # Emit a DWARF CU.
6c9e2db4
DE
1006 # OPTIONS is a list with an even number of elements containing
1007 # option-name and option-value pairs.
1008 # Current options are:
1009 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1010 # default = 0 (32-bit)
1011 # version n - DWARF version number to emit
1012 # default = 4
e630b974
TT
1013 # addr_size n - the size of addresses, 32, 64, or default
1014 # default = default
6c9e2db4
DE
1015 # fission 0|1 - boolean indicating if generating Fission debug info
1016 # default = 0
1d24041a
TT
1017 # BODY is Tcl code that emits the DIEs which make up the body of
1018 # the CU. It is evaluated in the caller's context.
6c9e2db4 1019 proc cu {options body} {
1d24041a 1020 variable _cu_count
6c9e2db4 1021 variable _abbrev_section
1d24041a
TT
1022 variable _abbrev_num
1023 variable _cu_label
1024 variable _cu_version
1025 variable _cu_addr_size
1026 variable _cu_offset_size
1027
6c9e2db4
DE
1028 # Establish the defaults.
1029 set is_64 0
1030 set _cu_version 4
e630b974 1031 set _cu_addr_size default
6c9e2db4
DE
1032 set fission 0
1033 set section ".debug_info"
1034 set _abbrev_section ".debug_abbrev"
1035
1036 foreach { name value } $options {
f13a9a0c 1037 set value [uplevel 1 "subst \"$value\""]
6c9e2db4
DE
1038 switch -exact -- $name {
1039 is_64 { set is_64 $value }
1040 version { set _cu_version $value }
1041 addr_size { set _cu_addr_size $value }
1042 fission { set fission $value }
1043 default { error "unknown option $name" }
1044 }
1045 }
e630b974
TT
1046 if {$_cu_addr_size == "default"} {
1047 if {[is_64_target]} {
1048 set _cu_addr_size 8
1049 } else {
1050 set _cu_addr_size 4
1051 }
1052 }
6c9e2db4
DE
1053 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
1054 if { $fission } {
1055 set section ".debug_info.dwo"
1056 set _abbrev_section ".debug_abbrev.dwo"
1d24041a 1057 }
1d24041a 1058
6c9e2db4 1059 _section $section
1d24041a
TT
1060
1061 set cu_num [incr _cu_count]
1062 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
1063 set _abbrev_num 1
1064
1065 set _cu_label [_compute_label "cu${cu_num}_begin"]
1066 set start_label [_compute_label "cu${cu_num}_start"]
1067 set end_label [_compute_label "cu${cu_num}_end"]
28d2bfb9 1068
1d24041a
TT
1069 define_label $_cu_label
1070 if {$is_64} {
1071 _op .4byte 0xffffffff
1072 _op .8byte "$end_label - $start_label"
1073 } else {
1074 _op .4byte "$end_label - $start_label"
1075 }
1076 define_label $start_label
6c9e2db4 1077 _op .2byte $_cu_version Version
41c77605 1078 _op .${_cu_offset_size}byte $my_abbrevs Abbrevs
6c9e2db4 1079 _op .byte $_cu_addr_size "Pointer size"
1d24041a 1080
6c9e2db4 1081 _defer_output $_abbrev_section {
1d24041a
TT
1082 define_label $my_abbrevs
1083 }
1084
1085 uplevel $body
1086
6c9e2db4 1087 _defer_output $_abbrev_section {
1d24041a
TT
1088 # Emit the terminator.
1089 _op .byte 0x0 Terminator
1090 _op .byte 0x0 Terminator
1091 }
1092
1093 define_label $end_label
1094 }
1095
4f22ed5c 1096 # Emit a DWARF TU.
6c9e2db4
DE
1097 # OPTIONS is a list with an even number of elements containing
1098 # option-name and option-value pairs.
1099 # Current options are:
1100 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1101 # default = 0 (32-bit)
1102 # version n - DWARF version number to emit
1103 # default = 4
e630b974
TT
1104 # addr_size n - the size of addresses, 32, 64, or default
1105 # default = default
6c9e2db4
DE
1106 # fission 0|1 - boolean indicating if generating Fission debug info
1107 # default = 0
4f22ed5c 1108 # SIGNATURE is the 64-bit signature of the type.
6c9e2db4
DE
1109 # TYPE_LABEL is the label of the type defined by this TU,
1110 # or "" if there is no type (i.e., type stubs in Fission).
4f22ed5c 1111 # BODY is Tcl code that emits the DIEs which make up the body of
6c9e2db4
DE
1112 # the TU. It is evaluated in the caller's context.
1113 proc tu {options signature type_label body} {
4f22ed5c 1114 variable _cu_count
6c9e2db4 1115 variable _abbrev_section
4f22ed5c
DE
1116 variable _abbrev_num
1117 variable _cu_label
1118 variable _cu_version
1119 variable _cu_addr_size
1120 variable _cu_offset_size
1121
6c9e2db4
DE
1122 # Establish the defaults.
1123 set is_64 0
1124 set _cu_version 4
e630b974 1125 set _cu_addr_size default
6c9e2db4
DE
1126 set fission 0
1127 set section ".debug_types"
1128 set _abbrev_section ".debug_abbrev"
1129
1130 foreach { name value } $options {
1131 switch -exact -- $name {
1132 is_64 { set is_64 $value }
1133 version { set _cu_version $value }
1134 addr_size { set _cu_addr_size $value }
1135 fission { set fission $value }
1136 default { error "unknown option $name" }
1137 }
1138 }
e630b974
TT
1139 if {$_cu_addr_size == "default"} {
1140 if {[is_64_target]} {
1141 set _cu_addr_size 8
1142 } else {
1143 set _cu_addr_size 4
1144 }
1145 }
6c9e2db4
DE
1146 set _cu_offset_size [expr { $is_64 ? 8 : 4 }]
1147 if { $fission } {
1148 set section ".debug_types.dwo"
1149 set _abbrev_section ".debug_abbrev.dwo"
4f22ed5c 1150 }
4f22ed5c 1151
6c9e2db4 1152 _section $section
4f22ed5c
DE
1153
1154 set cu_num [incr _cu_count]
1155 set my_abbrevs [_compute_label "abbrev${cu_num}_begin"]
1156 set _abbrev_num 1
1157
1158 set _cu_label [_compute_label "cu${cu_num}_begin"]
1159 set start_label [_compute_label "cu${cu_num}_start"]
1160 set end_label [_compute_label "cu${cu_num}_end"]
1161
1162 define_label $_cu_label
1163 if {$is_64} {
1164 _op .4byte 0xffffffff
1165 _op .8byte "$end_label - $start_label"
1166 } else {
1167 _op .4byte "$end_label - $start_label"
1168 }
1169 define_label $start_label
6c9e2db4 1170 _op .2byte $_cu_version Version
41c77605 1171 _op .${_cu_offset_size}byte $my_abbrevs Abbrevs
6c9e2db4 1172 _op .byte $_cu_addr_size "Pointer size"
4f22ed5c 1173 _op .8byte $signature Signature
6c9e2db4
DE
1174 if { $type_label != "" } {
1175 uplevel declare_labels $type_label
1176 upvar $type_label my_type_label
1177 if {$is_64} {
1178 _op .8byte "$my_type_label - $_cu_label"
1179 } else {
1180 _op .4byte "$my_type_label - $_cu_label"
1181 }
4f22ed5c 1182 } else {
6c9e2db4
DE
1183 if {$is_64} {
1184 _op .8byte 0
1185 } else {
1186 _op .4byte 0
1187 }
4f22ed5c
DE
1188 }
1189
6c9e2db4 1190 _defer_output $_abbrev_section {
4f22ed5c
DE
1191 define_label $my_abbrevs
1192 }
1193
1194 uplevel $body
1195
6c9e2db4 1196 _defer_output $_abbrev_section {
4f22ed5c
DE
1197 # Emit the terminator.
1198 _op .byte 0x0 Terminator
1199 _op .byte 0x0 Terminator
1200 }
1201
1202 define_label $end_label
1203 }
1204
28d2bfb9
AB
1205 # Emit a DWARF .debug_ranges unit.
1206 # OPTIONS is a list with an even number of elements containing
1207 # option-name and option-value pairs.
1208 # Current options are:
1209 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1210 # default = 0 (32-bit)
1211 #
1212 # BODY is Tcl code that emits the content of the .debug_ranges
1213 # unit, it is evaluated in the caller's context.
1214 proc ranges {options body} {
1215 variable _debug_ranges_64_bit
1216
1217 foreach { name value } $options {
1218 switch -exact -- $name {
1219 is_64 { set _debug_ranges_64_bit [subst $value] }
1220 default { error "unknown option $name" }
1221 }
1222 }
1223
1224 set section ".debug_ranges"
1225 _section $section
1226
1227 proc sequence {{ranges {}}} {
1228 variable _debug_ranges_64_bit
1229
1230 # Emit the sequence of addresses.
1231 set base ""
1232 foreach range $ranges {
1233 set range [uplevel 1 "subst \"$range\""]
1234 set type [lindex $range 0]
1235 switch -exact -- $type {
1236 base {
1237 set base [lrange $range 1 end]
1238
1239 if { $_debug_ranges_64_bit } then {
1240 _op .8byte 0xffffffffffffffff "Base Marker"
1241 _op .8byte $base "Base Address"
1242 } else {
1243 _op .4byte 0xffffffff "Base Marker"
1244 _op .4byte $base "Base Address"
1245 }
1246 }
1247 range {
1248 set start [lindex $range 1]
1249 set end [lrange $range 2 end]
1250
1251 if { $_debug_ranges_64_bit } then {
1252 _op .8byte $start "Start Address"
1253 _op .8byte $end "End Address"
1254 } else {
1255 _op .4byte $start "Start Address"
1256 _op .4byte $end "End Address"
1257 }
1258 }
1259 default { error "unknown range type: $type " }
1260 }
1261 }
1262
1263 # End of the sequence.
1264 if { $_debug_ranges_64_bit } then {
1265 _op .8byte 0x0 "End of Sequence Marker (Part 1)"
1266 _op .8byte 0x0 "End of Sequence Marker (Part 2)"
1267 } else {
1268 _op .4byte 0x0 "End of Sequence Marker (Part 1)"
1269 _op .4byte 0x0 "End of Sequence Marker (Part 2)"
1270 }
1271 }
1272
1273 uplevel $body
1274 }
1275
1276
6ef37366
PM
1277 # Emit a DWARF .debug_line unit.
1278 # OPTIONS is a list with an even number of elements containing
1279 # option-name and option-value pairs.
1280 # Current options are:
1281 # is_64 0|1 - boolean indicating if you want to emit 64-bit DWARF
1282 # default = 0 (32-bit)
1283 # version n - DWARF version number to emit
1284 # default = 4
1285 # addr_size n - the size of addresses, 32, 64, or default
1286 # default = default
1287 #
1288 # LABEL is the label of the current unit (which is probably
1289 # referenced by a DW_AT_stmt_list), or "" if there is no such
1290 # label.
1291 #
1292 # BODY is Tcl code that emits the parts which make up the body of
1293 # the line unit. It is evaluated in the caller's context. The
1294 # following commands are available for the BODY section:
1295 #
1296 # include_dir "dirname" -- adds a new include directory
1297 #
1298 # file_name "file.c" idx -- adds a new file name. IDX is a
1299 # 1-based index referencing an include directory or 0 for
1300 # current directory.
1301
1302 proc lines {options label body} {
1303 variable _line_count
1304 variable _line_saw_file
28d2bfb9
AB
1305 variable _line_saw_program
1306 variable _line_header_end_label
6ef37366
PM
1307
1308 # Establish the defaults.
1309 set is_64 0
1310 set _unit_version 4
1311 set _unit_addr_size default
1312
1313 foreach { name value } $options {
1314 switch -exact -- $name {
1315 is_64 { set is_64 $value }
1316 version { set _unit_version $value }
1317 addr_size { set _unit_addr_size $value }
1318 default { error "unknown option $name" }
1319 }
1320 }
1321 if {$_unit_addr_size == "default"} {
1322 if {[is_64_target]} {
1323 set _unit_addr_size 8
1324 } else {
1325 set _unit_addr_size 4
1326 }
1327 }
1328
1329 set unit_num [incr _line_count]
1330
1331 set section ".debug_line"
1332 _section $section
1333
1334 if { "$label" != "" } {
1335 # Define the user-provided label at this point.
1336 $label:
1337 }
1338
1339 set unit_len_label [_compute_label "line${_line_count}_start"]
1340 set unit_end_label [_compute_label "line${_line_count}_end"]
1341 set header_len_label [_compute_label "line${_line_count}_header_start"]
28d2bfb9 1342 set _line_header_end_label [_compute_label "line${_line_count}_header_end"]
6ef37366
PM
1343
1344 if {$is_64} {
1345 _op .4byte 0xffffffff
1346 _op .8byte "$unit_end_label - $unit_len_label" "unit_length"
1347 } else {
1348 _op .4byte "$unit_end_label - $unit_len_label" "unit_length"
1349 }
1350
1351 define_label $unit_len_label
1352
1353 _op .2byte $_unit_version version
1354
1355 if {$is_64} {
28d2bfb9 1356 _op .8byte "$_line_header_end_label - $header_len_label" "header_length"
6ef37366 1357 } else {
28d2bfb9 1358 _op .4byte "$_line_header_end_label - $header_len_label" "header_length"
6ef37366
PM
1359 }
1360
1361 define_label $header_len_label
1362
1363 _op .byte 1 "minimum_instruction_length"
28d2bfb9 1364 _op .byte 1 "default_is_stmt"
6ef37366
PM
1365 _op .byte 1 "line_base"
1366 _op .byte 1 "line_range"
28d2bfb9
AB
1367 _op .byte 10 "opcode_base"
1368
1369 # The standard_opcode_lengths table. The number of arguments
1370 # for each of the standard opcodes. Generating 9 entries here
1371 # matches the use of 10 in the opcode_base above. These 9
1372 # entries match the 9 standard opcodes for DWARF2, making use
1373 # of only 9 should be fine, even if we are generating DWARF3
1374 # or DWARF4.
1375 _op .byte 0 "standard opcode 1"
1376 _op .byte 1 "standard opcode 2"
1377 _op .byte 1 "standard opcode 3"
1378 _op .byte 1 "standard opcode 4"
1379 _op .byte 1 "standard opcode 5"
1380 _op .byte 0 "standard opcode 6"
1381 _op .byte 0 "standard opcode 7"
1382 _op .byte 0 "standard opcode 8"
1383 _op .byte 1 "standard opcode 9"
6ef37366
PM
1384
1385 proc include_dir {dirname} {
1386 _op .ascii [_quote $dirname]
1387 }
1388
1389 proc file_name {filename diridx} {
1390 variable _line_saw_file
1391 if "! $_line_saw_file" {
1392 # Terminate the dir list.
1393 _op .byte 0 "Terminator."
1394 set _line_saw_file 1
1395 }
1396
1397 _op .ascii [_quote $filename]
1398 _op .sleb128 $diridx
1399 _op .sleb128 0 "mtime"
1400 _op .sleb128 0 "length"
1401 }
1402
28d2bfb9
AB
1403 proc program {statements} {
1404 variable _line_saw_program
1405 variable _line_header_end_label
1406
1407 if "! $_line_saw_program" {
1408 # Terminate the file list.
1409 _op .byte 0 "Terminator."
1410 define_label $_line_header_end_label
1411 set _line_saw_program 1
1412 }
1413
1414 proc DW_LNE_set_address {addr} {
1415 _op .byte 0
1416 set start [new_label "set_address_start"]
1417 set end [new_label "set_address_end"]
1418 _op .uleb128 "${end} - ${start}"
1419 define_label ${start}
1420 _op .byte 2
1421 if {[is_64_target]} {
1422 _op .8byte ${addr}
1423 } else {
1424 _op .4byte ${addr}
1425 }
1426 define_label ${end}
1427 }
1428
1429 proc DW_LNE_end_sequence {} {
1430 _op .byte 0
1431 _op .uleb128 1
1432 _op .byte 1
1433 }
1434
1435 proc DW_LNS_copy {} {
1436 _op .byte 1
1437 }
1438
1439 proc DW_LNS_advance_pc {offset} {
1440 _op .byte 2
1441 _op .uleb128 ${offset}
1442 }
1443
1444 proc DW_LNS_advance_line {offset} {
1445 _op .byte 3
1446 _op .sleb128 ${offset}
1447 }
1448
1449 foreach statement $statements {
1450 uplevel 1 $statement
1451 }
1452 }
1453
6ef37366
PM
1454 uplevel $body
1455
1456 rename include_dir ""
1457 rename file_name ""
1458
1459 # Terminate dir list if we saw no files.
1460 if "! $_line_saw_file" {
1461 _op .byte 0 "Terminator."
1462 }
1463
1464 # Terminate the file list.
28d2bfb9
AB
1465 if "! $_line_saw_program" {
1466 _op .byte 0 "Terminator."
1467 define_label $_line_header_end_label
1468 }
6ef37366 1469
6ef37366
PM
1470 define_label $unit_end_label
1471 }
1472
1d24041a
TT
1473 proc _empty_array {name} {
1474 upvar $name the_array
1475
1476 catch {unset the_array}
1477 set the_array(_) {}
1478 unset the_array(_)
1479 }
1480
dc294be5
TT
1481 # Emit a .gnu_debugaltlink section with the given file name and
1482 # build-id. The buildid should be represented as a hexadecimal
1483 # string, like "ffeeddcc".
1484 proc gnu_debugaltlink {filename buildid} {
1485 _defer_output .gnu_debugaltlink {
1486 _op .ascii [_quote $filename]
1487 foreach {a b} [split $buildid {}] {
1488 _op .byte 0x$a$b
1489 }
1490 }
1491 }
1492
1493 proc _note {type name hexdata} {
1494 set namelen [expr [string length $name] + 1]
1495
1496 # Name size.
1497 _op .4byte $namelen
1498 # Data size.
1499 _op .4byte [expr [string length $hexdata] / 2]
1500 # Type.
1501 _op .4byte $type
1502 # The name.
1503 _op .ascii [_quote $name]
1504 # Alignment.
1505 set align 2
340c2830 1506 set total [expr {($namelen + (1 << $align) - 1) & -(1 << $align)}]
dc294be5
TT
1507 for {set i $namelen} {$i < $total} {incr i} {
1508 _op .byte 0
1509 }
1510 # The data.
1511 foreach {a b} [split $hexdata {}] {
1512 _op .byte 0x$a$b
1513 }
1514 }
1515
1516 # Emit a note section holding the given build-id.
1517 proc build_id {buildid} {
1518 _defer_output {.note.gnu.build-id a note} {
1519 # From elf/common.h.
1520 set NT_GNU_BUILD_ID 3
1521
1522 _note $NT_GNU_BUILD_ID GNU $buildid
1523 }
1524 }
1525
1d24041a
TT
1526 # The top-level interface to the DWARF assembler.
1527 # FILENAME is the name of the file where the generated assembly
1528 # code is written.
1529 # BODY is Tcl code to emit the assembly. It is evaluated via
1530 # "eval" -- not uplevel as you might expect, because it is
1531 # important to run the body in the Dwarf namespace.
1532 #
1533 # A typical invocation is something like:
1534 # Dwarf::assemble $file {
1535 # cu 0 2 8 {
1536 # compile_unit {
1537 # ...
1538 # }
1539 # }
1540 # cu 0 2 8 {
1541 # ...
1542 # }
1543 # }
1544 proc assemble {filename body} {
1545 variable _initialized
1546 variable _output_file
1547 variable _deferred_output
1548 variable _defer
1549 variable _label_num
1550 variable _strings
d65f0a9c 1551 variable _cu_count
6ef37366
PM
1552 variable _line_count
1553 variable _line_saw_file
28d2bfb9
AB
1554 variable _line_saw_program
1555 variable _line_header_end_label
1556 variable _debug_ranges_64_bit
1d24041a
TT
1557
1558 if {!$_initialized} {
1559 _read_constants
1560 set _initialized 1
1561 }
1562
1563 set _output_file [open $filename w]
1564 set _cu_count 0
1565 _empty_array _deferred_output
1566 set _defer ""
1567 set _label_num 0
1568 _empty_array _strings
1569
6ef37366
PM
1570 set _line_count 0
1571 set _line_saw_file 0
28d2bfb9
AB
1572 set _line_saw_program 0
1573 set _debug_ranges_64_bit [is_64_target]
6ef37366 1574
1d24041a
TT
1575 # Not "uplevel" here, because we want to evaluate in this
1576 # namespace. This is somewhat bad because it means we can't
1577 # readily refer to outer variables.
1578 eval $body
1579
1580 _write_deferred_output
1581
1582 catch {close $_output_file}
1583 set _output_file {}
1584 }
1585}
This page took 1.065456 seconds and 4 git commands to generate.