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