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