2 /*--------------------------------------------------------------------*/
3 /*--- Read DWARF1/2/3/4 debug info. readdwarf.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2010 Julian Seward
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 The GNU General Public License is contained in the file COPYING.
31 #if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_l4re)
33 #include "pub_core_basics.h"
34 #include "pub_core_debuginfo.h"
35 #include "pub_core_libcbase.h"
36 #include "pub_core_libcassert.h"
37 #include "pub_core_libcprint.h"
38 #include "pub_core_options.h"
39 #include "pub_core_xarray.h"
40 #include "pub_core_tooliface.h" /* VG_(needs) */
41 #include "priv_misc.h" /* dinfo_zalloc/free/strdup */
42 #include "priv_d3basics.h"
43 #include "priv_tytypes.h"
44 #include "priv_storage.h"
45 #include "priv_readdwarf.h" /* self */
48 /*------------------------------------------------------------*/
50 /*--- Read line number and CFI info from DWARF1, DWARF2 ---*/
51 /*--- and to some extent DWARF3 sections. ---*/
53 /*------------------------------------------------------------*/
55 /*------------------------------------------------------------*/
56 /*--- Expanding arrays of words, for holding file name and ---*/
57 /*--- directory name arrays. ---*/
58 /*------------------------------------------------------------*/
68 static void init_WordArray ( WordArray* wa )
75 static void free_WordArray ( WordArray* wa )
78 vg_assert(wa->tab_size > 0);
79 ML_(dinfo_free)(wa->tab);
84 static void addto_WordArray ( WordArray* wa, Word w )
89 if (0) VG_(printf)("<<ADD %p (new sz = %d) >>\n",
90 (HChar*)w, wa->tab_used+1);
92 if (wa->tab_used < wa->tab_size) {
96 if (0) VG_(printf)("EXPAND ARRAY from %d\n", wa->tab_size);
97 vg_assert(wa->tab_used == wa->tab_size);
98 vg_assert( (wa->tab_size == 0 && wa->tab == NULL)
99 || (wa->tab_size != 0 && wa->tab != NULL) );
100 new_size = wa->tab_size == 0 ? 8 : 2 * wa->tab_size;
101 new_tab = ML_(dinfo_zalloc)("di.aWA.1", new_size * sizeof(Word));
102 vg_assert(new_tab != NULL);
103 for (i = 0; i < wa->tab_used; i++)
104 new_tab[i] = wa->tab[i];
105 wa->tab_size = new_size;
107 ML_(dinfo_free)(wa->tab);
111 vg_assert(wa->tab_used < wa->tab_size);
112 vg_assert(wa->tab_size > 0);
113 wa->tab[wa->tab_used] = w;
117 static Word index_WordArray ( /*OUT*/Bool* inRange, WordArray* wa, Int i )
120 if (i >= 0 && i < wa->tab_used) {
130 /*------------------------------------------------------------*/
131 /*--- Read DWARF2 format line number info. ---*/
132 /*------------------------------------------------------------*/
134 /* Structure holding info extracted from the a .debug_line
140 ULong li_header_length;
141 UChar li_min_insn_length;
142 UChar li_max_ops_per_insn;
143 UChar li_default_is_stmt;
146 UChar li_opcode_base;
150 /* Structure holding additional infos found from a .debug_info
151 * compilation unit block */
154 /* Feel free to add more members here if you need ! */
155 Char* compdir; /* Compilation directory - points to .debug_info */
156 Char* name; /* Main file name - points to .debug_info */
157 ULong stmt_list; /* Offset in .debug_line */
158 Bool dw64; /* 64-bit Dwarf? */
162 /* Line number opcodes. */
163 enum dwarf_line_number_ops
165 DW_LNS_extended_op = 0,
167 DW_LNS_advance_pc = 2,
168 DW_LNS_advance_line = 3,
170 DW_LNS_set_column = 5,
171 DW_LNS_negate_stmt = 6,
172 DW_LNS_set_basic_block = 7,
173 DW_LNS_const_add_pc = 8,
174 DW_LNS_fixed_advance_pc = 9,
176 DW_LNS_set_prologue_end = 10,
177 DW_LNS_set_epilogue_begin = 11,
181 /* Line number extended opcodes. */
182 enum dwarf_line_number_x_ops
184 DW_LNE_end_sequence = 1,
185 DW_LNE_set_address = 2,
186 DW_LNE_define_file = 3,
187 DW_LNE_set_discriminator = 4
192 /* Information for the last statement boundary.
193 * Needed to calculate statement lengths. */
208 /* FIXME: duplicated in readdwarf3.c */
210 ULong read_leb128 ( UChar* data, Int* length_return, Int sign )
217 vg_assert(sign == 0 || sign == 1);
224 result |= ((ULong)(byte & 0x7f)) << shift;
231 if (length_return != NULL)
232 * length_return = num_read;
234 if (sign && (shift < 64) && (byte & 0x40))
235 result |= -(1ULL << shift);
240 /* Small helper functions easier to use
241 * value is returned and the given pointer is
242 * moved past end of leb128 data */
243 /* FIXME: duplicated in readdwarf3.c */
244 static ULong read_leb128U( UChar **data )
247 ULong val = read_leb128( *data, &len, 0 );
252 /* Same for signed data */
253 /* FIXME: duplicated in readdwarf3.c */
254 static Long read_leb128S( UChar **data )
257 ULong val = read_leb128( *data, &len, 1 );
262 /* Read what the DWARF3 spec calls an "initial length field". This
263 uses up either 4 or 12 bytes of the input and produces a 32-bit or
264 64-bit number respectively.
266 Read 32-bit value from p. If it is 0xFFFFFFFF, instead read a
267 64-bit bit value from p+4. This is used in 64-bit dwarf to encode
270 XXX this is a hack: the endianness of the initial length field is
271 specified by the DWARF we're reading. This happens to work only
272 because we don't do cross-arch jitting, hence this code runs on a
273 platform of the same endianness as the DWARF it is reading. Same
274 applies for initial lengths for CIE/FDEs and probably in zillions
275 of other places -- to be precise, exactly the places where
276 binutils/dwarf.c calls byte_get().
278 static ULong read_initial_length_field ( UChar* p_img, /*OUT*/Bool* is64 )
280 UInt w32 = *((UInt*)p_img);
281 if (w32 == 0xFFFFFFFF) {
283 return *((ULong*)(p_img+4));
291 static LineSMR state_machine_regs;
294 void reset_state_machine ( Int is_stmt )
296 if (0) VG_(printf)("smr.a := %p (reset)\n", NULL );
297 state_machine_regs.last_address = 0;
298 state_machine_regs.last_file = 1;
299 state_machine_regs.last_line = 1;
300 state_machine_regs.address = 0;
301 state_machine_regs.file = 1;
302 state_machine_regs.line = 1;
303 state_machine_regs.column = 0;
304 state_machine_regs.is_stmt = is_stmt;
305 state_machine_regs.basic_block = 0;
306 state_machine_regs.end_sequence = 0;
309 /* Look up a directory name, or return NULL if unknown. */
311 Char* lookupDir ( Int filename_index,
312 WordArray* fnidx2dir,
313 WordArray* dirnames )
316 Word diridx, dirname;
318 diridx = index_WordArray( &inRange, fnidx2dir, filename_index );
319 if (!inRange) goto bad;
321 dirname = index_WordArray( &inRange, dirnames, (Int)diridx );
322 if (!inRange) goto bad;
324 return (Char*)dirname;
329 ////////////////////////////////////////////////////////////////////
330 ////////////////////////////////////////////////////////////////////
332 /* Handled an extended line op starting at 'data'. Returns the number
333 of bytes that 'data' should be advanced by. */
335 Word process_extended_line_op( struct _DebugInfo* di,
336 WordArray* filenames,
338 WordArray* fnidx2dir,
339 UChar* data, Int is_stmt)
347 len = read_leb128 (data, & bytes_read, 0);
351 VG_(message)(Vg_UserMsg,
352 "Warning: DWARF2 reader: "
353 "Badly formed extended line op encountered\n");
354 return (Word)bytes_read;
360 if (0) VG_(printf)("dwarf2: ext OPC: %d\n", op_code);
363 case DW_LNE_end_sequence:
364 if (0) VG_(printf)("1001: si->o %#lx, smr.a %#lx\n",
365 di->text_debug_bias, state_machine_regs.address );
366 /* JRS: added for compliance with spec; is pointless due to
367 reset_state_machine below */
368 state_machine_regs.end_sequence = 1;
370 if (state_machine_regs.is_stmt) {
371 if (state_machine_regs.last_address) {
372 Bool inRange = False;
374 = (Char*)index_WordArray( &inRange, filenames,
375 state_machine_regs.last_file);
376 if (!inRange || !filename)
381 lookupDir( state_machine_regs.last_file,
382 fnidx2dir, dirnames ),
383 di->text_debug_bias + state_machine_regs.last_address,
384 di->text_debug_bias + state_machine_regs.address,
385 state_machine_regs.last_line, 0
389 reset_state_machine (is_stmt);
391 VG_(printf)(" Extended opcode %d: End of Sequence\n\n",
395 case DW_LNE_set_address:
396 adr = *((Addr *)data);
397 state_machine_regs.address = adr;
399 VG_(printf)(" Extended opcode %d: set Address to 0x%lx\n",
400 (Int)op_code, (Addr)adr);
403 case DW_LNE_define_file:
405 addto_WordArray( filenames, (Word)ML_(addStr)(di,name,-1) );
406 data += VG_(strlen) ((char *) data) + 1;
407 read_leb128 (data, & bytes_read, 0);
409 read_leb128 (data, & bytes_read, 0);
411 read_leb128 (data, & bytes_read, 0);
413 VG_(printf)(" DWARF2-line: set_address\n");
416 case DW_LNE_set_discriminator:
417 read_leb128 (data, & bytes_read, 0);
423 VG_(printf)("process_extended_line_op:default\n");
430 ////////////////////////////////////////////////////////////////////
431 ////////////////////////////////////////////////////////////////////
433 /* read a .debug_line section block for a compilation unit
435 * Input: - theBlock must point to the start of the block
436 * for the given compilation unit
437 * - ui contains additional info like the compilation dir
440 * Output: - si debug info structures get updated
443 void read_dwarf2_lineblock ( struct _DebugInfo* di,
445 UChar* theBlock, /* IMAGE */
450 UChar* standard_opcodes;
451 UChar* end_of_sequence;
457 UChar* external = theBlock;
458 UChar* data = theBlock;
460 /* filenames is an array of file names harvested from the DWARF2
461 info. Entry [0] is NULL and is never referred to by the state
464 Similarly, dirnames is an array of directory names. Entry [0]
465 is also NULL and denotes "we don't know what the path is", since
466 that is different from "the path is the empty string". Unlike
467 the file name table, the state machine does refer to entry [0],
468 which basically means "." ("the current directory of the
469 compilation", whatever that means, according to the DWARF3
472 fnidx2dir is an array of indexes into the dirnames table.
473 (confused yet?) filenames[] and fnidx2dir[] are indexed
474 together. That is, for some index i in the filename table, then
476 the filename is filenames[i]
477 the directory is dirnames[ fnidx2dir[i] ] */
479 /* Fails due to gcc padding ...
480 vg_assert(sizeof(DWARF2_External_LineInfo)
481 == sizeof(DWARF2_Internal_LineInfo));
484 init_WordArray(&filenames);
485 init_WordArray(&dirnames);
486 init_WordArray(&fnidx2dir);
488 /* DWARF2 starts numbering filename entries at 1, so we need to
489 add a dummy zeroth entry to the table. The zeroth dirnames
490 entry denotes 'current directory of compilation' so we might
491 as well make the fnidx2dir zeroth entry denote that.
493 addto_WordArray( &filenames, (Word)NULL );
496 addto_WordArray( &dirnames, (Word)ML_(addStr)(di, ui->compdir, -1) );
498 addto_WordArray( &dirnames, (Word)ML_(addStr)(di, ".", -1) );
500 addto_WordArray( &fnidx2dir, (Word)0 ); /* compilation dir */
502 info.li_length = read_initial_length_field( external, &is64 );
503 external += is64 ? 12 : 4;
505 VG_(printf)(" Length: %llu\n",
508 /* Check the length of the block. */
509 if (info.li_length > noLargerThan) {
510 ML_(symerr)(di, True,
511 "DWARF line info appears to be corrupt "
512 "- the section is too small");
516 /* Check its version number. */
517 info.li_version = * ((UShort *)external);
520 VG_(printf)(" DWARF Version: %d\n",
521 (Int)info.li_version);
523 if (info.li_version != 2 && info.li_version != 3 && info.li_version != 4) {
524 ML_(symerr)(di, True,
525 "Only DWARF version 2, 3 and 4 line info "
526 "is currently supported.");
530 info.li_header_length = ui->dw64 ? *((ULong*)external)
531 : (ULong)(*((UInt*)external));
532 external += ui->dw64 ? 8 : 4;
534 VG_(printf)(" Prologue Length: %llu\n",
535 info.li_header_length);
537 info.li_min_insn_length = * ((UChar *)external);
540 VG_(printf)(" Minimum Instruction Length: %d\n",
541 (Int)info.li_min_insn_length);
543 /* We only support machines with one opcode per instruction
544 for now. If we ever want to support VLIW machines there is
545 code to handle multiple opcodes per instruction in the
546 patch attached to BZ#233595.
548 if (info.li_version >= 4) {
549 info.li_max_ops_per_insn = * ((UChar *)external);
550 if (info.li_max_ops_per_insn != 1) {
551 ML_(symerr)(di, True,
552 "Invalid Maximum Ops Per Insn in line info.");
557 VG_(printf)(" Maximum Ops Per Insn: %d\n",
558 (Int)info.li_max_ops_per_insn);
560 info.li_max_ops_per_insn = 1;
563 info.li_default_is_stmt = * ((UChar *)external);
566 VG_(printf)(" Initial value of 'is_stmt': %d\n",
567 (Int)info.li_default_is_stmt);
569 /* Josef Weidendorfer (20021021) writes:
571 It seems to me that the Intel Fortran compiler generates bad
572 DWARF2 line info code: It sets "is_stmt" of the state machine in
573 the the line info reader to be always false. Thus, there is
574 never a statement boundary generated and therefore never a
575 instruction range/line number mapping generated for valgrind.
577 Please have a look at the DWARF2 specification, Ch. 6.2
578 (x86.ddj.com/ftp/manuals/tools/dwarf.pdf). Perhaps I understand
579 this wrong, but I don't think so.
581 I just had a look at the GDB DWARF2 reader... They completely
582 ignore "is_stmt" when recording line info ;-) That's the reason
583 "objdump -S" works on files from the the intel fortran compiler.
586 info.li_default_is_stmt = True;
588 /* JRS: changed (UInt*) to (UChar*) */
589 info.li_line_base = * ((UChar *)external);
590 info.li_line_base = (Int)(signed char)info.li_line_base;
593 VG_(printf)(" Line Base: %d\n",
596 info.li_line_range = * ((UChar *)external);
599 VG_(printf)(" Line Range: %d\n",
600 (Int)info.li_line_range);
602 info.li_opcode_base = * ((UChar *)external);
605 VG_(printf)(" Opcode Base: %d\n\n",
606 info.li_opcode_base);
608 if (0) VG_(printf)("dwarf2: line base: %d, range %d, opc base: %d\n",
609 (Int)info.li_line_base,
610 (Int)info.li_line_range,
611 (Int)info.li_opcode_base);
613 end_of_sequence = data + info.li_length
616 reset_state_machine (info.li_default_is_stmt);
618 /* Read the contents of the Opcodes table. */
619 standard_opcodes = external;
620 if (di->ddump_line) {
621 VG_(printf)(" Opcodes:\n");
622 for (i = 1; i < (Int)info.li_opcode_base; i++) {
623 VG_(printf)(" Opcode %d has %d args\n",
624 i, (Int)standard_opcodes[i-1]);
629 /* Read the contents of the Directory table. */
630 data = standard_opcodes + info.li_opcode_base - 1;
633 VG_(printf)(" The Directory Table%s\n",
634 *data == 0 ? " is empty." : ":" );
636 while (* data != 0) {
639 static Char buf[NBUF];
642 VG_(printf)(" %s\n", data);
644 /* If data[0] is '/', then 'data' is an absolute path and we
645 don't mess with it. Otherwise, if we can, construct the
646 'path ui->compdir' ++ "/" ++ 'data'. */
649 /* not an absolute path */
650 && ui->compdir != NULL
651 /* actually got something sensible for compdir */
652 && VG_(strlen)(ui->compdir) + VG_(strlen)(data) + 5/*paranoia*/ < NBUF
653 /* it's short enough to concatenate */)
656 VG_(strcat)(buf, ui->compdir);
657 VG_(strcat)(buf, "/");
658 VG_(strcat)(buf, data);
659 vg_assert(VG_(strlen)(buf) < NBUF);
660 addto_WordArray( &dirnames, (Word)ML_(addStr)(di,buf,-1) );
661 if (0) VG_(printf)("rel path %s\n", buf);
663 /* just use 'data'. */
664 addto_WordArray( &dirnames, (Word)ML_(addStr)(di,data,-1) );
665 if (0) VG_(printf)("abs path %s\n", data);
668 data += VG_(strlen)(data) + 1;
677 ML_(symerr)(di, True,
678 "can't find NUL at end of DWARF2 directory table");
683 /* Read the contents of the File Name table. This produces a bunch
684 of file names, and for each, an index to the corresponding
685 directory name entry. */
686 if (di->ddump_line) {
687 VG_(printf)(" The File Name Table:\n");
688 VG_(printf)(" Entry Dir Time Size Name\n");
692 while (* data != 0) {
694 Int bytes_read, diridx;
695 Int uu_time, uu_size; /* unused, and a guess */
697 data += VG_(strlen) ((Char *) data) + 1;
699 diridx = read_leb128 (data, & bytes_read, 0);
701 uu_time = read_leb128 (data, & bytes_read, 0);
703 uu_size = read_leb128 (data, & bytes_read, 0);
706 addto_WordArray( &filenames, (Word)ML_(addStr)(di,name,-1) );
707 addto_WordArray( &fnidx2dir, (Word)diridx );
708 if (0) VG_(printf)("file %s diridx %d\n", name, diridx );
710 VG_(printf)(" %d\t%d\t%d\t%d\t%s\n",
711 i, diridx, uu_time, uu_size, name);
719 ML_(symerr)(di, True,
720 "can't find NUL at end of DWARF2 file name table");
726 VG_(printf)(" Line Number Statements:\n");
728 /* Now display the statements. */
730 while (data < end_of_sequence) {
738 if (0) VG_(printf)("dwarf2: OPC: %d\n", op_code);
740 if (op_code >= info.li_opcode_base) {
743 op_code -= info.li_opcode_base;
744 adv = (op_code / info.li_line_range)
745 * info.li_min_insn_length;
747 state_machine_regs.address += adv;
749 if (0) VG_(printf)("smr.a += %#x\n", adv );
750 adv = (op_code % info.li_line_range) + info.li_line_base;
751 if (0) VG_(printf)("1002: di->o %#lx, smr.a %#lx\n",
752 di->text_debug_bias, state_machine_regs.address );
753 state_machine_regs.line += adv;
756 VG_(printf)(" Special opcode %d: advance Address by %d "
757 "to 0x%lx and Line by %d to %d\n",
758 (Int)op_code, advAddr, state_machine_regs.address,
759 (Int)adv, (Int)state_machine_regs.line );
761 if (state_machine_regs.is_stmt) {
762 /* only add a statement if there was a previous boundary */
763 if (state_machine_regs.last_address) {
764 Bool inRange = False;
766 = (Char*)index_WordArray( &inRange, &filenames,
767 state_machine_regs.last_file);
768 if (!inRange || !filename)
773 lookupDir( state_machine_regs.last_file,
774 &fnidx2dir, &dirnames ),
775 di->text_debug_bias + state_machine_regs.last_address,
776 di->text_debug_bias + state_machine_regs.address,
777 state_machine_regs.last_line,
781 state_machine_regs.last_address = state_machine_regs.address;
782 state_machine_regs.last_file = state_machine_regs.file;
783 state_machine_regs.last_line = state_machine_regs.line;
788 else { /* ! (op_code >= info.li_opcode_base) */
791 case DW_LNS_extended_op:
792 data += process_extended_line_op (
793 di, &filenames, &dirnames, &fnidx2dir,
794 data, info.li_default_is_stmt);
798 if (0) VG_(printf)("1002: di->o %#lx, smr.a %#lx\n",
799 di->text_debug_bias, state_machine_regs.address );
800 if (state_machine_regs.is_stmt) {
801 /* only add a statement if there was a previous boundary */
802 if (state_machine_regs.last_address) {
803 Bool inRange = False;
805 = (Char*)index_WordArray( &inRange, &filenames,
806 state_machine_regs.last_file );
807 if (!inRange || !filename)
812 lookupDir( state_machine_regs.last_file,
813 &fnidx2dir, &dirnames ),
814 di->text_debug_bias + state_machine_regs.last_address,
815 di->text_debug_bias + state_machine_regs.address,
816 state_machine_regs.last_line,
820 state_machine_regs.last_address = state_machine_regs.address;
821 state_machine_regs.last_file = state_machine_regs.file;
822 state_machine_regs.last_line = state_machine_regs.line;
824 state_machine_regs.basic_block = 0; /* JRS added */
826 VG_(printf)(" Copy\n");
829 case DW_LNS_advance_pc:
830 adv = info.li_min_insn_length
831 * read_leb128 (data, & bytes_read, 0);
833 state_machine_regs.address += adv;
834 if (0) VG_(printf)("smr.a += %#x\n", adv );
836 VG_(printf)(" Advance PC by %d to 0x%lx\n",
837 (Int)adv, state_machine_regs.address);
840 case DW_LNS_advance_line:
841 adv = read_leb128 (data, & bytes_read, 1);
843 state_machine_regs.line += adv;
845 VG_(printf)(" Advance Line by %d to %d\n",
846 (Int)adv, (Int)state_machine_regs.line);
849 case DW_LNS_set_file:
850 adv = read_leb128 (data, & bytes_read, 0);
852 state_machine_regs.file = adv;
854 VG_(printf)(" Set File Name to entry %d in the File Name Table\n",
858 case DW_LNS_set_column:
859 adv = read_leb128 (data, & bytes_read, 0);
861 state_machine_regs.column = adv;
863 VG_(printf)(" Set column to %d\n", (Int)adv);
866 case DW_LNS_negate_stmt:
867 adv = state_machine_regs.is_stmt;
869 state_machine_regs.is_stmt = adv;
871 VG_(printf)(" DWARF2-line: negate_stmt\n");
874 case DW_LNS_set_basic_block:
875 state_machine_regs.basic_block = 1;
877 VG_(printf)(" DWARF2-line: set_basic_block\n");
880 case DW_LNS_const_add_pc:
881 adv = (((255 - info.li_opcode_base) / info.li_line_range)
882 * info.li_min_insn_length);
883 state_machine_regs.address += adv;
884 if (0) VG_(printf)("smr.a += %#x\n", adv );
886 VG_(printf)(" Advance PC by constant %d to 0x%lx\n",
887 (Int)adv, (Addr)state_machine_regs.address);
890 case DW_LNS_fixed_advance_pc:
891 /* XXX: Need something to get 2 bytes */
892 adv = *((UShort *)data);
894 state_machine_regs.address += adv;
895 if (0) VG_(printf)("smr.a += %#x\n", adv );
897 VG_(printf)(" DWARF2-line: fixed_advance_pc\n");
900 case DW_LNS_set_prologue_end:
902 VG_(printf)(" DWARF2-line: set_prologue_end\n");
905 case DW_LNS_set_epilogue_begin:
907 VG_(printf)(" DWARF2-line: set_epilogue_begin\n");
911 /*adv =*/ read_leb128 (data, & bytes_read, 0);
914 VG_(printf)(" DWARF2-line: set_isa\n");
919 for (j = standard_opcodes[op_code - 1]; j > 0 ; --j) {
920 read_leb128 (data, &bytes_read, 0);
924 VG_(printf)(" Unknown opcode %d\n", (Int)op_code);
927 } /* switch (op_code) */
929 } /* if (op_code >= info.li_opcode_base) */
931 } /* while (data < end_of_sequence) */
937 free_WordArray(&filenames);
938 free_WordArray(&dirnames);
939 free_WordArray(&fnidx2dir);
942 ////////////////////////////////////////////////////////////////////
943 ////////////////////////////////////////////////////////////////////
945 /* Return abbrev for given code
946 * Returned pointer points to the tag
948 static UChar* lookup_abbrev( UChar* p, UInt acode )
953 code = read_leb128U( &p );
956 read_leb128U( &p ); /* skip tag */
957 p++; /* skip has_children flag */
959 name = read_leb128U( &p ); /* name */
960 read_leb128U( &p ); /* form */
962 while( name != 0 ); /* until name == form == 0 */
967 /* Read general information for a particular compile unit block in
968 * the .debug_info section.
970 * Input: - unitblock is the start of a compilation
971 * unit block in .debuginfo section
972 * - debugabbrev is start of .debug_abbrev section
973 * - debugstr is start of .debug_str section
975 * Output: Fill members of ui pertaining to the compilation unit:
976 * - ui->name is the name of the compilation unit
977 * - ui->compdir is the compilation unit directory
978 * - ui->stmt_list is the offset in .debug_line section
979 * for the dbginfos of this compilation unit
981 * Note : the output strings are not allocated and point
982 * directly to the memory-mapped section.
985 void read_unitinfo_dwarf2( /*OUT*/UnitInfo* ui,
986 UChar* unitblock_img,
987 UChar* debugabbrev_img,
988 UChar* debugstr_img )
991 ULong atoffs, blklen;
996 UChar* p = unitblock_img;
1000 VG_(memset)( ui, 0, sizeof( UnitInfo ) );
1001 ui->stmt_list = -1LL;
1003 /* Read the compilation unit header in .debug_info section - See p 70 */
1005 /* This block length */
1006 blklen = read_initial_length_field( p, &ui->dw64 );
1007 p += ui->dw64 ? 12 : 4;
1009 /* version should be 2, 3 or 4 */
1010 /* ver = *((UShort*)p); */
1013 /* get offset in abbrev */
1014 atoffs = ui->dw64 ? *((ULong*)p) : (ULong)(*((UInt*)p));
1015 p += ui->dw64 ? 8 : 4;
1021 end_img = unitblock_img
1022 + blklen + (ui->dw64 ? 12 : 4); /* End of this block */
1023 level = 0; /* Level in the abbrev tree */
1024 abbrev_img = debugabbrev_img
1025 + atoffs; /* Abbreviation data for this block */
1027 /* Read the compilation unit entries */
1028 while ( p < end_img ) {
1032 acode = read_leb128U( &p ); /* abbreviation code */
1034 /* NULL entry used for padding - or last child for a sequence
1040 /* Read abbreviation header */
1041 abcode = read_leb128U( &abbrev_img ); /* abbreviation code */
1042 if ( acode != abcode ) {
1043 /* We are in in children list, and must rewind to a
1044 * previously declared abbrev code. This code works but is
1045 * not triggered since we shortcut the parsing once we have
1046 * read the compile_unit block. This should only occur when
1048 abbrev_img = lookup_abbrev( debugabbrev_img + atoffs, acode );
1051 tag = read_leb128U( &abbrev_img );
1052 has_child = *(abbrev_img++) == 1; /* DW_CHILDREN_yes */
1057 /* And loop on entries */
1059 /* Read entry definition */
1061 ULong cval = -1LL; /* Constant value read */
1062 Char *sval = NULL; /* String value read */
1063 name = read_leb128U( &abbrev_img );
1064 form = read_leb128U( &abbrev_img );
1069 /* Attributes encoding explained p 71 */
1070 if ( form == 0x16 /* FORM_indirect */ )
1071 form = read_leb128U( &p );
1072 /* Decode form. For most kinds, Just skip the amount of data since
1073 we don't use it for now */
1074 /* JRS 9 Feb 06: This now handles 64-bit DWARF too. In
1075 64-bit DWARF, lineptr (and loclistptr,macptr,rangelistptr
1076 classes) use FORM_data8, not FORM_data4. Also,
1077 FORM_ref_addr and FORM_strp are 64-bit values, not 32-bit
1079 /* TJH 27 Apr 10: in DWARF 4 lineptr (and loclistptr,macptr,
1080 rangelistptr classes) use FORM_sec_offset which is 64 bits
1081 in 64 bit DWARF and 32 bits in 32 bit DWARF. */
1082 /* JRS 20 Apr 11: LLVM-2.9 encodes DW_AT_stmt_list using
1083 FORM_addr rather than the FORM_data4 that GCC uses. Hence
1084 handle FORM_addr too. */
1086 /* Those cases extract the data properly */
1087 case 0x05: /* FORM_data2 */ cval = *((UShort*)p); p +=2; break;
1088 case 0x06: /* FORM_data4 */ cval = *((UInt*)p);p +=4; break;
1089 case 0x0e: /* FORM_strp */ /* pointer in .debug_str */
1090 /* 2006-01-01: only generate a value if
1091 debugstr is non-NULL (which means that a
1092 debug_str section was found) */
1093 if (debugstr_img && !ui->dw64)
1094 sval = debugstr_img + *((UInt*)p);
1095 if (debugstr_img && ui->dw64)
1096 sval = debugstr_img + *((ULong*)p);
1097 p += ui->dw64 ? 8 : 4;
1099 case 0x08: /* FORM_string */ sval = (Char*)p;
1100 p += VG_(strlen)((Char*)p) + 1; break;
1101 case 0x0b: /* FORM_data1 */ cval = *p; p++; break;
1102 case 0x17: /* FORM_sec_offset */if (ui->dw64) {
1103 cval = *((ULong*)p); p += 8;
1105 cval = *((UInt*)p); p += 4;
1108 case 0x07: /* FORM_data8 */ if (ui->dw64) cval = *((ULong*)p);
1110 /* perhaps should assign
1111 unconditionally to cval? */
1113 /* TODO : Following ones just skip data - implement if you need */
1114 case 0x01: /* FORM_addr */ p += addr_size; break;
1115 case 0x03: /* FORM_block2 */ p += *((UShort*)p) + 2; break;
1116 case 0x04: /* FORM_block4 */ p += *((UInt*)p) + 4; break;
1117 case 0x09: /* FORM_block */ p += read_leb128U( &p ); break;
1118 case 0x0a: /* FORM_block1 */ p += *p + 1; break;
1119 case 0x0c: /* FORM_flag */ p++; break;
1120 case 0x0d: /* FORM_sdata */ read_leb128S( &p ); break;
1121 case 0x0f: /* FORM_udata */ read_leb128U( &p ); break;
1122 case 0x10: /* FORM_ref_addr */ p += ui->dw64 ? 8 : 4; break;
1123 case 0x11: /* FORM_ref1 */ p++; break;
1124 case 0x12: /* FORM_ref2 */ p += 2; break;
1125 case 0x13: /* FORM_ref4 */ p += 4; break;
1126 case 0x14: /* FORM_ref8 */ p += 8; break;
1127 case 0x15: /* FORM_ref_udata */ read_leb128U( &p ); break;
1128 case 0x18: /* FORM_exprloc */ p += read_leb128U( &p ); break;
1129 case 0x19: /* FORM_flag_present */break;
1130 case 0x20: /* FORM_ref_sig8 */ p += 8; break;
1133 VG_(printf)( "### unhandled dwarf2 abbrev form code 0x%x\n", form );
1137 /* Now store the members we need in the UnitInfo structure */
1138 if ( tag == 0x0011 /*TAG_compile_unit*/ ) {
1139 if ( name == 0x03 ) ui->name = sval; /* DW_AT_name */
1140 else if ( name == 0x1b ) ui->compdir = sval; /* DW_AT_compdir */
1141 else if ( name == 0x10 ) ui->stmt_list = cval; /* DW_AT_stmt_list */
1144 /* Shortcut the parsing once we have read the compile_unit block
1145 * That's enough info for us, and we are not gdb ! */
1146 if ( tag == 0x0011 /*TAG_compile_unit*/ )
1148 } /* Loop on each sub block */
1150 /* This test would be valid if we were not shortcutting the parsing
1152 VG_(printf)( "#### Exiting debuginfo block at level %d !!!\n", level );
1157 ////////////////////////////////////////////////////////////////////
1158 ////////////////////////////////////////////////////////////////////
1160 /* Collect the debug info from DWARF3 debugging sections
1161 * of a given module.
1163 * Inputs: given .debug_xxx sections
1164 * Output: update di to contain all the DWARF3 debug infos
1166 void ML_(read_debuginfo_dwarf3)
1167 ( struct _DebugInfo* di,
1168 UChar* debug_info_img, Word debug_info_sz, /* .debug_info */
1169 UChar* debug_abbv_img, Word debug_abbv_sz, /* .debug_abbrev */
1170 UChar* debug_line_img, Word debug_line_sz, /* .debug_line */
1171 UChar* debug_str_img, Word debug_str_sz ) /* .debug_str */
1181 end1_img = debug_info_img + debug_info_sz;
1184 /* Make sure we at least have a header for the first block */
1185 if (debug_info_sz < 4) {
1186 ML_(symerr)( di, True,
1187 "Last block truncated in .debug_info; ignoring" );
1191 /* Iterate on all the blocks we find in .debug_info */
1192 for ( block_img = debug_info_img;
1193 block_img < end1_img - 4;
1194 block_img += blklen + blklen_len ) {
1196 /* Read the compilation unit header in .debug_info section - See
1198 /* This block length */
1199 blklen = read_initial_length_field( block_img, &blklen_is_64 );
1200 blklen_len = blklen_is_64 ? 12 : 4;
1201 if ( block_img + blklen + blklen_len > end1_img ) {
1202 ML_(symerr)( di, True,
1203 "Last block truncated in .debug_info; ignoring" );
1207 /* version should be 2 */
1208 ver = *((UShort*)( block_img + blklen_len ));
1209 if ( ver != 2 && ver != 3 && ver != 4 ) {
1210 ML_(symerr)( di, True,
1211 "Ignoring non-Dwarf2/3/4 block in .debug_info" );
1215 /* Fill ui with offset in .debug_line and compdir */
1217 VG_(printf)( "Reading UnitInfo at 0x%lx.....\n",
1218 block_img - debug_info_img + 0UL );
1219 read_unitinfo_dwarf2( &ui, block_img,
1220 debug_abbv_img, debug_str_img );
1222 VG_(printf)( " => LINES=0x%llx NAME=%s DIR=%s\n",
1223 ui.stmt_list, ui.name, ui.compdir );
1225 /* Ignore blocks with no .debug_line associated block */
1226 if ( ui.stmt_list == -1LL )
1230 VG_(printf)("debug_line_sz %ld, ui.stmt_list %lld %s\n",
1231 debug_line_sz, ui.stmt_list, ui.name );
1232 /* Read the .debug_line block for this compile unit */
1233 read_dwarf2_lineblock(
1234 di, &ui, debug_line_img + ui.stmt_list,
1235 debug_line_sz - ui.stmt_list );
1240 ////////////////////////////////////////////////////////////////////
1241 ////////////////////////////////////////////////////////////////////
1243 /*------------------------------------------------------------*/
1244 /*--- Read DWARF1 format line number info. ---*/
1245 /*------------------------------------------------------------*/
1247 /* DWARF1 appears to be redundant, but nevertheless the Lahey Fortran
1248 compiler generates it.
1251 /* The following three enums (dwarf_tag, dwarf_form, dwarf_attribute)
1252 are taken from the file include/elf/dwarf.h in the GNU gdb-6.0
1253 sources, which are Copyright 1992, 1993, 1995, 1999 Free Software
1254 Foundation, Inc and naturally licensed under the GNU General Public
1255 License version 2 or later.
1258 /* Tag names and codes. */
1261 TAG_padding = 0x0000,
1262 TAG_array_type = 0x0001,
1263 TAG_class_type = 0x0002,
1264 TAG_entry_point = 0x0003,
1265 TAG_enumeration_type = 0x0004,
1266 TAG_formal_parameter = 0x0005,
1267 TAG_global_subroutine = 0x0006,
1268 TAG_global_variable = 0x0007,
1269 /* 0x0008 -- reserved */
1270 /* 0x0009 -- reserved */
1272 TAG_lexical_block = 0x000b,
1273 TAG_local_variable = 0x000c,
1274 TAG_member = 0x000d,
1275 /* 0x000e -- reserved */
1276 TAG_pointer_type = 0x000f,
1277 TAG_reference_type = 0x0010,
1278 TAG_compile_unit = 0x0011,
1279 TAG_string_type = 0x0012,
1280 TAG_structure_type = 0x0013,
1281 TAG_subroutine = 0x0014,
1282 TAG_subroutine_type = 0x0015,
1283 TAG_typedef = 0x0016,
1284 TAG_union_type = 0x0017,
1285 TAG_unspecified_parameters = 0x0018,
1286 TAG_variant = 0x0019,
1287 TAG_common_block = 0x001a,
1288 TAG_common_inclusion = 0x001b,
1289 TAG_inheritance = 0x001c,
1290 TAG_inlined_subroutine = 0x001d,
1291 TAG_module = 0x001e,
1292 TAG_ptr_to_member_type = 0x001f,
1293 TAG_set_type = 0x0020,
1294 TAG_subrange_type = 0x0021,
1295 TAG_with_stmt = 0x0022,
1297 /* GNU extensions */
1299 TAG_format_label = 0x8000, /* for FORTRAN 77 and Fortran 90 */
1300 TAG_namelist = 0x8001, /* For Fortran 90 */
1301 TAG_function_template = 0x8002, /* for C++ */
1302 TAG_class_template = 0x8003 /* for C++ */
1305 /* Form names and codes. */
1318 /* Attribute names and codes. */
1320 enum dwarf_attribute {
1321 AT_sibling = (0x0010|FORM_REF),
1322 AT_location = (0x0020|FORM_BLOCK2),
1323 AT_name = (0x0030|FORM_STRING),
1324 AT_fund_type = (0x0050|FORM_DATA2),
1325 AT_mod_fund_type = (0x0060|FORM_BLOCK2),
1326 AT_user_def_type = (0x0070|FORM_REF),
1327 AT_mod_u_d_type = (0x0080|FORM_BLOCK2),
1328 AT_ordering = (0x0090|FORM_DATA2),
1329 AT_subscr_data = (0x00a0|FORM_BLOCK2),
1330 AT_byte_size = (0x00b0|FORM_DATA4),
1331 AT_bit_offset = (0x00c0|FORM_DATA2),
1332 AT_bit_size = (0x00d0|FORM_DATA4),
1333 /* (0x00e0|FORM_xxxx) -- reserved */
1334 AT_element_list = (0x00f0|FORM_BLOCK4),
1335 AT_stmt_list = (0x0100|FORM_DATA4),
1336 AT_low_pc = (0x0110|FORM_ADDR),
1337 AT_high_pc = (0x0120|FORM_ADDR),
1338 AT_language = (0x0130|FORM_DATA4),
1339 AT_member = (0x0140|FORM_REF),
1340 AT_discr = (0x0150|FORM_REF),
1341 AT_discr_value = (0x0160|FORM_BLOCK2),
1342 /* (0x0170|FORM_xxxx) -- reserved */
1343 /* (0x0180|FORM_xxxx) -- reserved */
1344 AT_string_length = (0x0190|FORM_BLOCK2),
1345 AT_common_reference = (0x01a0|FORM_REF),
1346 AT_comp_dir = (0x01b0|FORM_STRING),
1347 AT_const_value_string = (0x01c0|FORM_STRING),
1348 AT_const_value_data2 = (0x01c0|FORM_DATA2),
1349 AT_const_value_data4 = (0x01c0|FORM_DATA4),
1350 AT_const_value_data8 = (0x01c0|FORM_DATA8),
1351 AT_const_value_block2 = (0x01c0|FORM_BLOCK2),
1352 AT_const_value_block4 = (0x01c0|FORM_BLOCK4),
1353 AT_containing_type = (0x01d0|FORM_REF),
1354 AT_default_value_addr = (0x01e0|FORM_ADDR),
1355 AT_default_value_data2 = (0x01e0|FORM_DATA2),
1356 AT_default_value_data4 = (0x01e0|FORM_DATA4),
1357 AT_default_value_data8 = (0x01e0|FORM_DATA8),
1358 AT_default_value_string = (0x01e0|FORM_STRING),
1359 AT_friends = (0x01f0|FORM_BLOCK2),
1360 AT_inline = (0x0200|FORM_STRING),
1361 AT_is_optional = (0x0210|FORM_STRING),
1362 AT_lower_bound_ref = (0x0220|FORM_REF),
1363 AT_lower_bound_data2 = (0x0220|FORM_DATA2),
1364 AT_lower_bound_data4 = (0x0220|FORM_DATA4),
1365 AT_lower_bound_data8 = (0x0220|FORM_DATA8),
1366 AT_private = (0x0240|FORM_STRING),
1367 AT_producer = (0x0250|FORM_STRING),
1368 AT_program = (0x0230|FORM_STRING),
1369 AT_protected = (0x0260|FORM_STRING),
1370 AT_prototyped = (0x0270|FORM_STRING),
1371 AT_public = (0x0280|FORM_STRING),
1372 AT_pure_virtual = (0x0290|FORM_STRING),
1373 AT_return_addr = (0x02a0|FORM_BLOCK2),
1374 AT_abstract_origin = (0x02b0|FORM_REF),
1375 AT_start_scope = (0x02c0|FORM_DATA4),
1376 AT_stride_size = (0x02e0|FORM_DATA4),
1377 AT_upper_bound_ref = (0x02f0|FORM_REF),
1378 AT_upper_bound_data2 = (0x02f0|FORM_DATA2),
1379 AT_upper_bound_data4 = (0x02f0|FORM_DATA4),
1380 AT_upper_bound_data8 = (0x02f0|FORM_DATA8),
1381 AT_virtual = (0x0300|FORM_STRING),
1383 /* GNU extensions. */
1385 AT_sf_names = (0x8000|FORM_DATA4),
1386 AT_src_info = (0x8010|FORM_DATA4),
1387 AT_mac_info = (0x8020|FORM_DATA4),
1388 AT_src_coords = (0x8030|FORM_DATA4),
1389 AT_body_begin = (0x8040|FORM_ADDR),
1390 AT_body_end = (0x8050|FORM_ADDR)
1393 /* end of enums taken from gdb-6.0 sources */
1395 void ML_(read_debuginfo_dwarf1) (
1396 struct _DebugInfo* di,
1397 UChar* dwarf1d, Int dwarf1d_sz,
1398 UChar* dwarf1l, Int dwarf1l_sz )
1401 Bool stmt_list_found;
1402 Int die_offset, die_szb, at_offset;
1403 UShort die_kind, at_kind;
1405 UChar* src_filename;
1408 VG_(printf)("read_debuginfo_dwarf1 ( %p, %d, %p, %d )\n",
1409 dwarf1d, dwarf1d_sz, dwarf1l, dwarf1l_sz );
1411 /* This loop scans the DIEs. */
1414 if (die_offset >= dwarf1d_sz) break;
1416 die_szb = *(Int*)(dwarf1d + die_offset);
1417 die_kind = *(UShort*)(dwarf1d + die_offset + 4);
1419 /* We're only interested in compile_unit DIEs; ignore others. */
1420 if (die_kind != TAG_compile_unit) {
1421 die_offset += die_szb;
1426 VG_(printf)("compile-unit DIE: offset %d, tag 0x%x, size %d\n",
1427 die_offset, (Int)die_kind, die_szb );
1429 /* We've got a compile_unit DIE starting at (dwarf1d +
1430 die_offset+6). Try and find the AT_name and AT_stmt_list
1431 attributes. Then, finally, we can read the line number info
1432 for this source file. */
1434 /* The next 3 are set as we find the relevant attrs. */
1435 src_filename = NULL;
1436 stmt_list_found = False;
1439 /* This loop scans the Attrs inside compile_unit DIEs. */
1440 at_base = dwarf1d + die_offset + 6;
1443 if (at_offset >= die_szb-6) break;
1445 at_kind = *(UShort*)(at_base + at_offset);
1446 if (0) VG_(printf)("atoffset %d, attag 0x%x\n",
1447 at_offset, (Int)at_kind );
1448 at_offset += 2; /* step over the attribute itself */
1449 /* We have to examine the attribute to figure out its
1455 if (at_kind == AT_stmt_list) {
1456 stmt_list_found = True;
1457 stmt_list = *(Int*)(at_base+at_offset);
1459 at_offset += 4; break;
1462 at_offset += sizeof(void*); break;
1466 /* Zero terminated string, step over it. */
1467 if (at_kind == AT_name)
1468 src_filename = at_base + at_offset;
1469 while (at_offset < die_szb-6 && at_base[at_offset] != 0)
1474 VG_(printf)("Unhandled DWARF-1 attribute 0x%x\n",
1476 VG_(core_panic)("Unhandled DWARF-1 attribute");
1477 } /* switch (at_kind) */
1478 } /* looping over attributes */
1480 /* So, did we find the required stuff for a line number table in
1481 this DIE? If yes, read it. */
1482 if (stmt_list_found /* there is a line number table */
1483 && src_filename != NULL /* we know the source filename */
1487 4 bytes, includes the entire table
1489 unclear (4? 8?), assuming native pointer size here.
1490 Then a sequence of triples
1491 (source line number -- 32 bits
1492 source line column -- 16 bits
1493 address delta -- 32 bits)
1499 UInt prev_line, prev_delta;
1501 curr_filenm = ML_(addStr) ( di, src_filename, -1 );
1502 prev_line = prev_delta = 0;
1504 ptr = dwarf1l + stmt_list;
1505 len = *(Int*)ptr; ptr += sizeof(Int);
1506 base = (Addr)(*(void**)ptr); ptr += sizeof(void*);
1507 len -= (sizeof(Int) + sizeof(void*));
1512 line = *(UInt*)ptr; ptr += sizeof(UInt);
1513 col = *(UShort*)ptr; ptr += sizeof(UShort);
1514 delta = *(UShort*)ptr; ptr += sizeof(UInt);
1515 if (0) VG_(printf)("line %d, col %d, delta %d\n",
1516 line, (Int)col, delta );
1517 len -= (sizeof(UInt) + sizeof(UShort) + sizeof(UInt));
1519 if (delta > 0 && prev_line > 0) {
1520 if (0) VG_(printf) (" %d %d-%d\n",
1521 prev_line, prev_delta, delta-1);
1522 ML_(addLineInfo) ( di, curr_filenm, NULL,
1523 base + prev_delta, base + delta,
1531 /* Move on the the next DIE. */
1532 die_offset += die_szb;
1534 } /* Looping over DIEs */
1539 /*------------------------------------------------------------*/
1540 /*--- Read call-frame info from an .eh_frame section ---*/
1541 /*------------------------------------------------------------*/
1545 The DWARF3 spec, available from http://www.dwarfstd.org/Download.php
1547 This describes how to read CFA data from .debug_frame sections.
1548 So as to maximise everybody's annoyance and confusion, .eh_frame
1549 sections are almost the same as .debug_frame sections, but differ
1550 in a few subtle and ill documented but important aspects.
1552 Generic ELF Specification, sections 7.5 (DWARF Extensions) and 7.6
1553 (Exception Frames), available from
1555 http://www.linux-foundation.org/spec/book/ELF-generic/ELF-generic.html
1557 This really does describe .eh_frame, at least the aspects that
1558 differ from standard DWARF3. It's better than guessing, and
1559 (marginally) more fun than reading the gdb source code.
1565 gdb-6.3/gdb/dwarf2-frame.c
1567 gdb-6.3/gdb/i386-tdep.c:
1569 DWARF2/GCC uses the stack address *before* the function call as a
1570 frame's CFA. [jrs: I presume this means %esp before the call as
1573 JRS: on amd64, the dwarf register numbering is, as per
1574 gdb-6.3/gdb/amd64-tdep.c and also amd64-abi-0.98.pdf:
1577 RAX RDX RCX RBX RSI RDI RBP RSP
1582 16 is the return address (RIP)
1583 "The table defines Return Address to have a register number,
1584 even though the address is stored in 0(%rsp) and not in a
1600 50,51,52,53,54,55 ES,CS,SS,DS,FS,GS
1601 58 FS.BASE (what's that?)
1602 59 GS.BASE (what's that?)
1603 62 TR (task register)
1604 63 LDTR (LDT register)
1606 65 FCW (x87 control word)
1607 66 FSW (x86 status word)
1609 On x86 I cannot find any documentation. It _appears_ to be the
1610 actual instruction encoding, viz:
1613 EAX ECX EDX EBX ESP EBP ESI EDI
1615 8 is the return address (EIP) */
1618 /* Comments re DW_CFA_set_loc, 16 Nov 06.
1621 Someone recently sent me a libcrypto.so.0.9.8 as distributed with
1622 Ubuntu of some flavour, compiled with gcc 4.1.2 on amd64. It
1623 causes V's CF reader to complain a lot:
1625 >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1626 >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1627 >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1628 >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1629 >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:48
1630 >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1632 After chasing this around a bit it seems that the CF bytecode
1633 parser lost sync at a DW_CFA_set_loc, which has a single argument
1634 denoting an address.
1636 As it stands that address is extracted by read_Addr(). On amd64
1637 that just fetches 8 bytes regardless of anything else.
1639 read_encoded_Addr() is more sophisticated. This appears to take
1640 into account some kind of encoding flag. When I replace the uses
1641 of read_Addr by read_encoded_Addr for DW_CFA_set_loc, the
1642 complaints go away, there is no loss of sync, and the parsed CF
1643 instructions are the same as shown by readelf --debug-dump=frames.
1645 So it seems a plausible fix. The problem is I looked in the DWARF3
1646 spec and completely failed to figure out whether or not the arg to
1647 DW_CFA_set_loc is supposed to be encoded in a way suitable for
1648 read_encoded_Addr, nor for that matter any description of what it
1649 is that read_encoded_Addr is really decoding.
1652 The problem is that the encoding is not standard - the eh_frame
1653 section uses the same encoding as the dwarf_frame section except
1654 for a few small changes, and this is one of them. So this is not
1655 something the DWARF standard covers.
1657 There is an augmentation string to indicate what is going on though
1658 so that programs can recognise it.
1660 What we are doing seems to match what gdb 6.5 and libdwarf 20060614
1661 do though. I'm not sure about readelf though.
1663 (later): Well dwarfdump barfs on it:
1665 dwarfdump ERROR: dwarf_get_fde_info_for_reg:
1666 DW_DLE_DF_FRAME_DECODING_ERROR(193) (193)
1668 I've looked at binutils as well now, and the code in readelf agrees
1669 with your patch - ie it treats set_loc as having an encoded address
1670 if there is a zR augmentation indicating an encoding.
1672 Quite why gdb and libdwarf don't understand this is an interesting
1675 Final outcome: all uses of read_Addr were replaced by
1676 read_encoded_Addr. A new type AddressDecodingInfo was added to
1677 make it relatively clean to plumb through the extra info needed by
1681 /* More badness re address encoding, 12 Jan 07.
1683 Most gcc provided CIEs have a "zR" augmentation, which means they
1684 supply their own address encoding, and that works fine. However,
1685 some icc9 supplied CIEs have no augmentation, which means they use
1686 the default_Addr_encoding(). That says to use a machine-word sized
1687 value, literally unmodified.
1689 Since .so's are, in general, relocated when loaded, having absolute
1690 addresses in the CFI data makes no sense when read_encoded_Addr is
1691 used to find the initial location for a FDE. The resulting saga:
1694 > I'm chasing a stack backtrace failure for an amd64 .so which was
1695 > created I believe by icc 9.1. After a while I wound up looking at
1696 > this: (readdwarf.c)
1698 > 5083 tom static UChar default_Addr_encoding ( void )
1700 > 3584 tom switch (sizeof(Addr)) {
1701 > 3584 tom case 4: return DW_EH_PE_udata4;
1702 > 3584 tom case 8: return DW_EH_PE_udata8;
1703 > 3584 tom default: vg_assert(0);
1707 > If a CIE does not have an "augmentation string" (typically "zR") then
1708 > addresses are decoded as described by default_Addr_encoding. If there
1709 > is an 'R' in the augmentation string then the encoding to use
1710 > is specified by the CIE itself, which works fine with GCC compiled code
1711 > since that always appears to specify zR.
1715 > Problem is this .so has no augmentation string and so uses the
1716 > default encoding, viz DW_EH_PE_udata8. That appears to mean
1717 > "read a 64 bit number" and use that as-is (for the starting value
1718 > of the program counter when running the CFA program).
1720 Strictly speaking the default is DW_EH_PE_absptr, but that amounts
1721 to either udata4 or udata8 depending on the platform's pointer size
1722 which is a shortcut I used.
1724 > For this .so that gives nonsense (very small) PCs which are later
1725 > rejected by the sanity check which ensures PC ranges fall inside
1726 > the mapped text segment. It seems like the .so expects to have the
1727 > start VMA of the text segment added on. This would correspond to
1729 > static UChar default_Addr_encoding ( void )
1731 > switch (sizeof(Addr)) {
1732 > case 4: return DW_EH_PE_textrel + DW_EH_PE_udata4;
1733 > case 8: return DW_EH_PE_textrel + DW_EH_PE_udata8;
1734 > default: vg_assert(0);
1738 The problem you're seeing is that you have absolute pointers inside
1739 a shared library, which obviously makes little sense on the face of
1740 things as how would the linker know where the library will be
1743 The answer of course is that it doesn't, so if it points absolute
1744 pointers in the frame unwind data is has to include relocations for
1745 them, and I'm betting that if you look at the relocations in the
1746 library you will there are some for that data.
1748 That is fine of course when ld.so maps the library - it will
1749 relocate the eh_frame data as it maps it (or prelinking will
1750 already have done so) and when the g++ exception code kicks in and
1751 unwinds the stack it will see relocated data.
1753 We of course are mapping the section from the ELF file ourselves
1754 and are not applying the relocations, hence the problem you are
1757 Strictly speaking we should apply the relocations but the cheap
1758 solution is essentially to do what you've done - strictly speaking
1759 you should adjust by the difference between the address the library
1760 was linked for and the address it has been loaded at, but a shared
1761 library will normally be linked for address zero I believe. It's
1762 possible that prelinking might change that though?
1765 That all syncs with what I am seeing.
1767 So what I am inclined to do is:
1769 - Leave default_Addr_encoding as it is
1771 - Change read_encoded_Addr's handling of "case DW_EH_PE_absptr" so
1772 it sets base to, as you say, the difference between the address
1773 the library was linked for and the address it has been loaded at
1774 (== the SegInfo's text_bias)
1776 Does that sound sane? I think it should even handle the prelinked
1781 Hmm. Plausible as it sounds, it doesn't work. It now produces
1782 bogus backtraces for locations inside the (statically linked)
1783 memcheck executable.
1785 Besides, there are a couple of other places where read_encoded_Addr
1786 is used -- one of which is used to establish the length of the
1787 address range covered by the current FDE:
1789 fde_arange = read_encoded_Addr(&nbytes, &adi, data);
1791 and it doesn't seem to make any sense for read_encoded_Addr to add
1792 on the text segment bias in that context. The DWARF3 spec says
1793 that both the initial_location and address_range (length) fields
1794 are encoded the same way ("target address"), so it is unclear at
1795 what stage in the process it would be appropriate to relocate the
1796 former but not the latter.
1798 One unprincipled kludge that does work is the following: just
1799 before handing one of the address range fragments off to
1800 ML_(addDiCfSI) for permanent storage, check its start address. If
1801 that is very low (less than 2 M), and is far below the mapped text
1802 segment, and adding the text bias would move the fragment entirely
1803 inside the mapped text segment, then do so. A kind of kludged
1804 last-minute relocation, if you like.
1806 12 Jan 07: committing said kludge (see kludge_then_addDiCfSI). If
1807 the situation clarifies, it can easily enough be backed out and
1808 replaced by a better fix.
1811 /* --------------- Decls --------------- */
1813 #if defined(VGP_x86_linux)
1816 # define RA_REG_DEFAULT 8
1817 #elif defined(VGP_amd64_linux)
1820 # define RA_REG_DEFAULT 16
1821 #elif defined(VGP_ppc32_linux)
1824 # define RA_REG_DEFAULT 65
1825 #elif defined(VGP_ppc64_linux)
1828 # define RA_REG_DEFAULT 65
1829 #elif defined(VGP_arm_linux)
1832 # define RA_REG_DEFAULT 14 //???
1833 #elif defined(VGP_x86_darwin)
1836 # define RA_REG_DEFAULT 8
1837 #elif defined(VGP_amd64_darwin)
1840 # define RA_REG_DEFAULT 16
1841 #elif defined(VGP_s390x_linux)
1842 # define FP_REG 11 // sometimes s390 has a frame pointer in r11
1843 # define SP_REG 15 // stack is always r15
1844 # define RA_REG_DEFAULT 14 // the return address is in r14
1845 #elif defined(VGP_x86_l4re)
1848 # define RA_REG_DEFAULT 8
1850 # error "Unknown platform"
1853 /* the number of regs we are prepared to unwind */
1854 #if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
1855 # define N_CFI_REGS 72
1857 # define N_CFI_REGS 20
1860 /* Instructions for the automaton */
1861 enum dwarf_cfa_primary_ops
1863 DW_CFA_use_secondary = 0,
1864 DW_CFA_advance_loc = 1,
1869 enum dwarf_cfa_secondary_ops
1872 DW_CFA_set_loc = 0x01,
1873 DW_CFA_advance_loc1 = 0x02,
1874 DW_CFA_advance_loc2 = 0x03,
1875 DW_CFA_advance_loc4 = 0x04,
1876 DW_CFA_offset_extended = 0x05,
1877 DW_CFA_restore_extended = 0x06,
1878 DW_CFA_undefined = 0x07,
1879 DW_CFA_same_value = 0x08,
1880 DW_CFA_register = 0x09,
1881 DW_CFA_remember_state = 0x0a,
1882 DW_CFA_restore_state = 0x0b,
1883 DW_CFA_def_cfa = 0x0c,
1884 DW_CFA_def_cfa_register = 0x0d,
1885 DW_CFA_def_cfa_offset = 0x0e,
1886 DW_CFA_def_cfa_expression = 0x0f, /* DWARF3 only */
1887 DW_CFA_expression = 0x10, /* DWARF3 only */
1888 DW_CFA_offset_extended_sf = 0x11, /* DWARF3 only */
1889 DW_CFA_def_cfa_sf = 0x12, /* DWARF3 only */
1890 DW_CFA_def_cfa_offset_sf = 0x13, /* DWARF3 only */
1891 DW_CFA_val_offset = 0x14, /* DWARF3 only */
1892 DW_CFA_val_offset_sf = 0x15, /* DWARF3 only */
1893 DW_CFA_val_expression = 0x16, /* DWARF3 only */
1894 DW_CFA_lo_user = 0x1c,
1895 DW_CFA_GNU_window_save = 0x2d, /* GNU extension */
1896 DW_CFA_GNU_args_size = 0x2e, /* GNU extension */
1897 DW_CFA_GNU_negative_offset_extended = 0x2f, /* GNU extension */
1898 DW_CFA_hi_user = 0x3f
1901 #define DW_EH_PE_absptr 0x00
1902 #define DW_EH_PE_omit 0xff
1904 #define DW_EH_PE_uleb128 0x01
1905 #define DW_EH_PE_udata2 0x02
1906 #define DW_EH_PE_udata4 0x03
1907 #define DW_EH_PE_udata8 0x04
1908 #define DW_EH_PE_sleb128 0x09
1909 #define DW_EH_PE_sdata2 0x0A
1910 #define DW_EH_PE_sdata4 0x0B
1911 #define DW_EH_PE_sdata8 0x0C
1912 #define DW_EH_PE_signed 0x08
1914 #define DW_EH_PE_pcrel 0x10
1915 #define DW_EH_PE_textrel 0x20
1916 #define DW_EH_PE_datarel 0x30
1917 #define DW_EH_PE_funcrel 0x40
1918 #define DW_EH_PE_aligned 0x50
1920 #define DW_EH_PE_indirect 0x80
1923 /* RegRule and UnwindContext are used temporarily to do the unwinding.
1924 The result is then summarised into a sequence of CfiSIs, if
1925 possible. UnwindContext effectively holds the state of the
1926 abstract machine whilst it is running.
1928 The CFA can either be a signed offset from a register,
1931 CFA = cfa_reg + cfa_off when UnwindContext.cfa_is_regoff==True
1934 When .cfa_is_regoff == True, cfa_expr_id must be zero
1935 When .cfa_is_regoff == False, cfa_reg must be zero
1936 and cfa_off must be zero
1938 RegRule describes, for each register, how to get its
1939 value in the previous frame, where 'cfa' denotes the cfa
1940 for the frame as a whole:
1942 RegRule = RR_Undef -- undefined
1943 | RR_Same -- same as in previous frame
1944 | RR_CFAOff arg -- is at * ( cfa + arg )
1945 | RR_CFAValOff arg -- is ( cfa + arg )
1946 | RR_Reg arg -- is in register 'arg'
1947 | RR_Expr arg -- is at * [[ arg ]]
1948 | RR_ValExpr arg -- is [[ arg ]]
1951 Note that RR_Expr is redundant since the same can be represented
1952 using RR_ValExpr with an explicit dereference (CfiExpr_Deref) at
1953 the outermost level.
1955 All expressions are stored in exprs in the containing
1956 UnwindContext. Since the UnwindContext gets reinitialised for each
1957 new FDE, summarise_context needs to copy out any expressions it
1958 wants to keep into the cfsi_exprs field of the containing SegInfo.
1962 enum { RR_Undef, RR_Same, RR_CFAOff, RR_CFAValOff,
1963 RR_Reg, /*RR_Expr,*/ RR_ValExpr, RR_Arch } tag;
1964 /* meaning: int offset for CFAoff/CFAValOff
1966 expr index for Expr/ValExpr */
1971 static void ppRegRule ( XArray* exprs, RegRule* rrule )
1974 switch (rrule->tag) {
1975 case RR_Undef: VG_(printf)("u "); break;
1976 case RR_Same: VG_(printf)("s "); break;
1977 case RR_CFAOff: VG_(printf)("c%d ", rrule->arg); break;
1978 case RR_CFAValOff: VG_(printf)("v%d ", rrule->arg); break;
1979 case RR_Reg: VG_(printf)("r%d ", rrule->arg); break;
1980 case RR_ValExpr: VG_(printf)("ve{");
1981 ML_(ppCfiExpr)( exprs, rrule->arg );
1984 case RR_Arch: VG_(printf)("a "); break;
1985 default: VG_(core_panic)("ppRegRule");
1990 /* Size of the stack of register unwind rules. This is only
1991 exceedingly rarely used, so a stack of size 1 should actually work
1992 with almost all compiler-generated CFA. */
1993 #define N_RR_STACK 4
1997 /* Read-only fields (set by the CIE) */
2002 /* The rest of these fields can be modifed by
2003 run_CF_instruction. */
2006 /* We need a stack of these in order to handle
2007 DW_CFA_{remember,restore}_state. */
2008 struct UnwindContextState {
2009 /* The CFA entry. This can be either reg+/-offset or an expr. */
2010 Bool cfa_is_regoff; /* True=>is reg+offset; False=>is expr */
2012 Int cfa_off; /* in bytes */
2013 Int cfa_expr_ix; /* index into cfa_exprs */
2014 /* Register unwind rules. */
2015 RegRule reg[N_CFI_REGS];
2018 Int state_sp; /* 0 <= state_sp < N_RR_STACK; points at the
2019 currently-in-use rule set. */
2020 /* array of CfiExpr, shared by reg[] and cfa_expr_ix */
2025 static void ppUnwindContext ( UnwindContext* ctx )
2028 VG_(printf)("0x%llx: ", (ULong)ctx->loc);
2029 for (j = 0; j <= ctx->state_sp; j++) {
2030 struct UnwindContextState* ctxs = &ctx->state[j];
2031 VG_(printf)("%s[%d]={ ", j > 0 ? " " : "", j);
2032 if (ctxs->cfa_is_regoff) {
2033 VG_(printf)("%d(r%d) ", ctxs->cfa_off, ctxs->cfa_reg);
2035 vg_assert(ctx->exprs);
2037 ML_(ppCfiExpr)( ctx->exprs, ctxs->cfa_expr_ix );
2041 for (i = 0; i < N_CFI_REGS; i++)
2042 ppRegRule(ctx->exprs, &ctxs->reg[i]);
2048 static void initUnwindContext ( /*OUT*/UnwindContext* ctx )
2051 VG_(memset)(ctx, 0, sizeof(*ctx));
2052 /* ctx->code_a_f = 0;
2054 ctx->initloc = 0; */
2055 ctx->ra_reg = RA_REG_DEFAULT;
2058 ctx->state_sp = 0; */
2059 for (j = 0; j < N_RR_STACK; j++) {
2060 ctx->state[j].cfa_is_regoff = True;
2061 /* ctx->state[j].cfa_reg = 0;
2062 ctx->state[j].cfa_off = 0;
2063 ctx->state[j].cfa_expr_ix = 0; */
2064 for (i = 0; i < N_CFI_REGS; i++) {
2066 ctx->state[j].reg[i].tag = RR_Undef;
2067 /* ctx->state[j].reg[i].arg = 0; */
2069 # if defined(VGA_arm)
2070 /* All callee-saved registers (or at least the ones we are
2071 summarising for) should start out as RR_Same, on ARM. */
2072 ctx->state[j].reg[11].tag = RR_Same;
2073 /* ctx->state[j].reg[13].tag = RR_Same; */
2074 ctx->state[j].reg[14].tag = RR_Same;
2075 ctx->state[j].reg[12].tag = RR_Same;
2076 ctx->state[j].reg[7].tag = RR_Same;
2077 /* this can't be right though: R12 (IP) isn't callee saved. */
2083 /* A structure which holds information needed by read_encoded_Addr().
2088 UChar* ehframe_image;
2092 AddressDecodingInfo;
2095 /* ------------ Deal with summary-info records ------------ */
2097 static void initCfiSI ( DiCfSI* si )
2099 VG_(memset)(si, 0, sizeof(*si));
2103 /* --------------- Summarisation --------------- */
2107 Int copy_convert_CfiExpr_tree ( XArray* dst,
2108 UnwindContext* srcuc,
2111 /* Summarise ctx into si, if possible. Returns True if successful.
2112 This is taken to be just after ctx's loc advances; hence the
2113 summary is up to but not including the current loc. This works
2114 on both x86 and amd64.
2116 static Bool summarise_context( /*OUT*/DiCfSI* si,
2119 struct _DebugInfo* debuginfo )
2122 struct UnwindContextState* ctxs;
2125 /* Guard against obviously stupid settings of the reg-rule stack
2127 if (ctx->state_sp < 0) { why = 8; goto failed; }
2128 if (ctx->state_sp >= N_RR_STACK) { why = 9; goto failed; }
2129 ctxs = &ctx->state[ctx->state_sp];
2131 /* First, summarise the method for generating the CFA */
2132 if (!ctxs->cfa_is_regoff) {
2133 /* it was set by DW_CFA_def_cfa_expression; try to convert */
2137 dst = debuginfo->cfsi_exprs;
2138 if (src && (VG_(sizeXA)(src) > 0) && (!dst)) {
2139 dst = VG_(newXA)( ML_(dinfo_zalloc), "di.ccCt.1", ML_(dinfo_free),
2142 debuginfo->cfsi_exprs = dst;
2144 conv = copy_convert_CfiExpr_tree
2145 ( dst, ctx, ctxs->cfa_expr_ix );
2146 vg_assert(conv >= -1);
2147 if (conv == -1) { why = 6; goto failed; }
2148 si->cfa_how = CFIC_EXPR;
2150 if (0 && debuginfo->ddump_frames)
2151 ML_(ppCfiExpr)(dst, conv);
2154 if (ctxs->cfa_is_regoff && ctxs->cfa_reg == SP_REG) {
2155 si->cfa_off = ctxs->cfa_off;
2156 # if defined(VGA_x86) || defined(VGA_amd64) || defined(VGA_s390x)
2157 si->cfa_how = CFIC_IA_SPREL;
2158 # elif defined(VGA_arm)
2159 si->cfa_how = CFIC_ARM_R13REL;
2161 si->cfa_how = 0; /* invalid */
2165 if (ctxs->cfa_is_regoff && ctxs->cfa_reg == FP_REG) {
2166 si->cfa_off = ctxs->cfa_off;
2167 # if defined(VGA_x86) || defined(VGA_amd64) || defined(VGA_s390x)
2168 si->cfa_how = CFIC_IA_BPREL;
2169 # elif defined(VGA_arm)
2170 si->cfa_how = CFIC_ARM_R12REL;
2172 si->cfa_how = 0; /* invalid */
2175 # if defined(VGA_arm)
2177 if (ctxs->cfa_is_regoff && ctxs->cfa_reg == 11/*??_REG*/) {
2178 si->cfa_how = CFIC_ARM_R11REL;
2179 si->cfa_off = ctxs->cfa_off;
2182 if (ctxs->cfa_is_regoff && ctxs->cfa_reg == 7/*??_REG*/) {
2183 si->cfa_how = CFIC_ARM_R7REL;
2184 si->cfa_off = ctxs->cfa_off;
2192 # define SUMMARISE_HOW(_how, _off, _ctxreg) \
2193 switch (_ctxreg.tag) { \
2195 _how = CFIR_UNKNOWN; _off = 0; break; \
2197 _how = CFIR_SAME; _off = 0; break; \
2199 _how = CFIR_MEMCFAREL; _off = _ctxreg.arg; break; \
2200 case RR_CFAValOff: \
2201 _how = CFIR_CFAREL; _off = _ctxreg.arg; break; \
2202 case RR_ValExpr: { \
2203 XArray *src, *dst; \
2206 dst = debuginfo->cfsi_exprs; \
2207 if (src && (VG_(sizeXA)(src) > 0) && (!dst)) { \
2208 dst = VG_(newXA)( ML_(dinfo_zalloc), \
2211 sizeof(CfiExpr) ); \
2213 debuginfo->cfsi_exprs = dst; \
2215 conv = copy_convert_CfiExpr_tree \
2216 ( dst, ctx, _ctxreg.arg ); \
2217 vg_assert(conv >= -1); \
2218 if (conv == -1) { why = 7; goto failed; } \
2221 if (0 && debuginfo->ddump_frames) \
2222 ML_(ppCfiExpr)(dst, conv); \
2226 why = 2; goto failed; /* otherwise give up */ \
2229 # if defined(VGA_x86) || defined(VGA_amd64)
2231 /* --- entire tail of this fn specialised for x86/amd64 --- */
2233 SUMMARISE_HOW(si->ra_how, si->ra_off,
2234 ctxs->reg[ctx->ra_reg] );
2235 SUMMARISE_HOW(si->bp_how, si->bp_off,
2236 ctxs->reg[FP_REG] );
2238 /* on x86/amd64, it seems the old %{e,r}sp value before the call is
2239 always the same as the CFA. Therefore ... */
2240 si->sp_how = CFIR_CFAREL;
2243 /* also, gcc says "Undef" for %{e,r}bp when it is unchanged. So
2245 if (ctxs->reg[FP_REG].tag == RR_Undef)
2246 si->bp_how = CFIR_SAME;
2248 /* knock out some obviously stupid cases */
2249 if (si->ra_how == CFIR_SAME)
2250 { why = 3; goto failed; }
2252 /* bogus looking range? Note, we require that the difference is
2253 representable in 32 bits. */
2254 if (loc_start >= ctx->loc)
2255 { why = 4; goto failed; }
2256 if (ctx->loc - loc_start > 10000000 /* let's say */)
2257 { why = 5; goto failed; }
2259 si->base = loc_start + ctx->initloc;
2260 si->len = (UInt)(ctx->loc - loc_start);
2264 # elif defined(VGA_arm)
2266 /* ---- entire tail of this fn specialised for arm ---- */
2268 SUMMARISE_HOW(si->r14_how, si->r14_off,
2271 //SUMMARISE_HOW(si->r13_how, si->r13_off,
2274 SUMMARISE_HOW(si->r12_how, si->r12_off,
2275 ctxs->reg[FP_REG] );
2277 SUMMARISE_HOW(si->r11_how, si->r11_off,
2278 ctxs->reg[11/*FP_REG*/] );
2280 SUMMARISE_HOW(si->r7_how, si->r7_off,
2283 if (ctxs->reg[14/*LR*/].tag == RR_Same
2284 && ctx->ra_reg == 14/*as we expect it always to be*/) {
2285 /* Generate a trivial CfiExpr, which merely says "r14". First
2286 ensure this DebugInfo has a cfsi_expr array in which to park
2288 if (!debuginfo->cfsi_exprs)
2289 debuginfo->cfsi_exprs = VG_(newXA)( ML_(dinfo_zalloc),
2293 si->ra_off = ML_(CfiExpr_CfiReg)( debuginfo->cfsi_exprs,
2295 si->ra_how = CFIR_EXPR;
2297 /* Just summarise it in the normal way */
2298 SUMMARISE_HOW(si->ra_how, si->ra_off,
2299 ctxs->reg[ctx->ra_reg] );
2302 /* on arm, it seems the old r13 (SP) value before the call is
2303 always the same as the CFA. Therefore ... */
2304 si->r13_how = CFIR_CFAREL;
2307 /* bogus looking range? Note, we require that the difference is
2308 representable in 32 bits. */
2309 if (loc_start >= ctx->loc)
2310 { why = 4; goto failed; }
2311 if (ctx->loc - loc_start > 10000000 /* let's say */)
2312 { why = 5; goto failed; }
2314 si->base = loc_start + ctx->initloc;
2315 si->len = (UInt)(ctx->loc - loc_start);
2320 # elif defined(VGA_s390x)
2322 SUMMARISE_HOW(si->ra_how, si->ra_off,
2323 ctxs->reg[ctx->ra_reg] );
2324 SUMMARISE_HOW(si->fp_how, si->fp_off,
2325 ctxs->reg[FP_REG] );
2326 SUMMARISE_HOW(si->sp_how, si->sp_off,
2327 ctxs->reg[SP_REG] );
2329 /* change some defaults to consumable values */
2330 if (si->sp_how == CFIR_UNKNOWN)
2331 si->sp_how = CFIR_SAME;
2333 if (si->fp_how == CFIR_UNKNOWN)
2334 si->fp_how = CFIR_SAME;
2336 if (si->cfa_how == CFIR_UNKNOWN) {
2337 si->cfa_how = CFIC_IA_SPREL;
2340 if (si->ra_how == CFIR_UNKNOWN) {
2341 if (!debuginfo->cfsi_exprs)
2342 debuginfo->cfsi_exprs = VG_(newXA)( ML_(dinfo_zalloc),
2346 si->ra_how = CFIR_EXPR;
2347 si->ra_off = ML_(CfiExpr_CfiReg)( debuginfo->cfsi_exprs,
2351 /* knock out some obviously stupid cases */
2352 if (si->ra_how == CFIR_SAME)
2353 { why = 3; goto failed; }
2355 /* bogus looking range? Note, we require that the difference is
2356 representable in 32 bits. */
2357 if (loc_start >= ctx->loc)
2358 { why = 4; goto failed; }
2359 if (ctx->loc - loc_start > 10000000 /* let's say */)
2360 { why = 5; goto failed; }
2362 si->base = loc_start + ctx->initloc;
2363 si->len = (UInt)(ctx->loc - loc_start);
2369 # elif defined(VGA_ppc32) || defined(VGA_ppc64)
2371 # error "Unknown arch"
2374 # undef SUMMARISE_HOW
2377 if (VG_(clo_verbosity) > 2 || debuginfo->trace_cfi) {
2378 VG_(message)(Vg_DebugMsg,
2379 "summarise_context(loc_start = %#lx)"
2380 ": cannot summarise(why=%d): \n", loc_start, why);
2381 ppUnwindContext(ctx);
2386 /* Copy the tree rooted at srcuc->exprs node srcix to dstxa, on the
2387 way converting any DwReg regs (regs numbered using the Dwarf scheme
2388 defined by each architecture's ABI) into CfiRegs, which are
2389 platform independent. If the conversion isn't possible because
2390 there is no equivalent register, return -1. This has the
2391 undesirable side effect of de-dagifying the input; oh well. */
2392 static Int copy_convert_CfiExpr_tree ( XArray* dstxa,
2393 UnwindContext* srcuc,
2398 XArray* srcxa = srcuc->exprs;
2401 vg_assert(srcix >= 0 && srcix < VG_(sizeXA)(srcxa));
2403 src = VG_(indexXA)( srcxa, srcix );
2406 return ML_(CfiExpr_Undef)( dstxa );
2408 cpA = copy_convert_CfiExpr_tree( dstxa, srcuc, src->Cex.Deref.ixAddr );
2410 return -1; /* propagate failure */
2411 return ML_(CfiExpr_Deref)( dstxa, cpA );
2413 return ML_(CfiExpr_Const)( dstxa, src->Cex.Const.con );
2415 cpL = copy_convert_CfiExpr_tree( dstxa, srcuc, src->Cex.Binop.ixL );
2416 cpR = copy_convert_CfiExpr_tree( dstxa, srcuc, src->Cex.Binop.ixR );
2417 vg_assert(cpL >= -1 && cpR >= -1);
2418 if (cpL == -1 || cpR == -1)
2419 return -1; /* propagate failure */
2420 return ML_(CfiExpr_Binop)( dstxa, src->Cex.Binop.op, cpL, cpR );
2422 /* should not see these in input (are created only by this
2423 conversion step!) */
2424 VG_(core_panic)("copy_convert_CfiExpr_tree: CfiReg in input");
2426 /* This is the only place where the conversion can fail. */
2427 Int dwreg __attribute__((unused));
2428 dwreg = src->Cex.DwReg.reg;
2429 # if defined(VGA_x86) || defined(VGA_amd64)
2430 if (dwreg == SP_REG)
2431 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_SP );
2432 if (dwreg == FP_REG)
2433 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_BP );
2434 if (dwreg == srcuc->ra_reg)
2435 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_IP ); /* correct? */
2436 # elif defined(VGA_arm)
2437 if (dwreg == SP_REG)
2438 return ML_(CfiExpr_CfiReg)( dstxa, Creg_ARM_R13 );
2439 if (dwreg == FP_REG)
2440 return ML_(CfiExpr_CfiReg)( dstxa, Creg_ARM_R12 );
2441 if (dwreg == srcuc->ra_reg)
2442 return ML_(CfiExpr_CfiReg)( dstxa, Creg_ARM_R15 ); /* correct? */
2443 # elif defined(VGA_s390x)
2444 if (dwreg == SP_REG)
2445 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_SP );
2446 if (dwreg == FP_REG)
2447 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_BP );
2448 if (dwreg == srcuc->ra_reg)
2449 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_IP ); /* correct? */
2450 # elif defined(VGA_ppc32) || defined(VGA_ppc64)
2452 # error "Unknown arch"
2454 /* else we must fail - can't represent the reg */
2458 VG_(core_panic)("copy_convert_CfiExpr_tree: default");
2463 static void ppUnwindContext_summary ( UnwindContext* ctx )
2465 struct UnwindContextState* ctxs = &ctx->state[ctx->state_sp];
2467 VG_(printf)("0x%llx-1: ", (ULong)ctx->loc);
2469 if (ctxs->cfa_reg == SP_REG) {
2470 VG_(printf)("SP/CFA=%d+SP ", ctxs->cfa_off);
2472 if (ctxs->cfa_reg == FP_REG) {
2473 VG_(printf)("SP/CFA=%d+FP ", ctxs->cfa_off);
2475 VG_(printf)("SP/CFA=unknown ");
2479 ppRegRule( ctx->exprs, &ctxs->reg[ctx->ra_reg] );
2482 ppRegRule( ctx->exprs, &ctxs->reg[FP_REG] );
2487 /* ------------ Pick apart DWARF2 byte streams ------------ */
2489 static inline Bool host_is_little_endian ( void )
2491 UInt x = 0x76543210;
2492 UChar* p = (UChar*)(&x);
2493 return toBool(*p == 0x10);
2496 static Short read_Short ( UChar* data )
2499 if (host_is_little_endian()) {
2501 | ( ((UInt)data[1]) << 8 );
2504 | ( ((UInt)data[0]) << 8 );
2509 static Int read_Int ( UChar* data )
2512 if (host_is_little_endian()) {
2514 | ( ((UInt)data[1]) << 8 )
2515 | ( ((UInt)data[2]) << 16 )
2516 | ( ((UInt)data[3]) << 24 );
2519 | ( ((UInt)data[2]) << 8 )
2520 | ( ((UInt)data[1]) << 16 )
2521 | ( ((UInt)data[0]) << 24 );
2526 static Long read_Long ( UChar* data )
2529 if (host_is_little_endian()) {
2531 | ( ((ULong)data[1]) << 8 )
2532 | ( ((ULong)data[2]) << 16 )
2533 | ( ((ULong)data[3]) << 24 )
2534 | ( ((ULong)data[4]) << 32 )
2535 | ( ((ULong)data[5]) << 40 )
2536 | ( ((ULong)data[6]) << 48 )
2537 | ( ((ULong)data[7]) << 56 );
2540 | ( ((ULong)data[6]) << 8 )
2541 | ( ((ULong)data[5]) << 16 )
2542 | ( ((ULong)data[4]) << 24 )
2543 | ( ((ULong)data[3]) << 32 )
2544 | ( ((ULong)data[2]) << 40 )
2545 | ( ((ULong)data[1]) << 48 )
2546 | ( ((ULong)data[0]) << 56 );
2551 static UShort read_UShort ( UChar* data )
2554 if (host_is_little_endian()) {
2556 | ( ((UInt)data[1]) << 8 );
2559 | ( ((UInt)data[0]) << 8 );
2564 static UInt read_UInt ( UChar* data )
2567 if (host_is_little_endian()) {
2569 | ( ((UInt)data[1]) << 8 )
2570 | ( ((UInt)data[2]) << 16 )
2571 | ( ((UInt)data[3]) << 24 );
2574 | ( ((UInt)data[2]) << 8 )
2575 | ( ((UInt)data[1]) << 16 )
2576 | ( ((UInt)data[0]) << 24 );
2581 static ULong read_ULong ( UChar* data )
2584 if (host_is_little_endian()) {
2586 | ( ((ULong)data[1]) << 8 )
2587 | ( ((ULong)data[2]) << 16 )
2588 | ( ((ULong)data[3]) << 24 )
2589 | ( ((ULong)data[4]) << 32 )
2590 | ( ((ULong)data[5]) << 40 )
2591 | ( ((ULong)data[6]) << 48 )
2592 | ( ((ULong)data[7]) << 56 );
2595 | ( ((ULong)data[6]) << 8 )
2596 | ( ((ULong)data[5]) << 16 )
2597 | ( ((ULong)data[4]) << 24 )
2598 | ( ((ULong)data[3]) << 32 )
2599 | ( ((ULong)data[2]) << 40 )
2600 | ( ((ULong)data[1]) << 48 )
2601 | ( ((ULong)data[0]) << 56 );
2606 static UChar read_UChar ( UChar* data )
2611 static ULong read_le_u_encoded_literal ( UChar* data, UInt size )
2614 case 8: return (ULong)read_ULong( data );
2615 case 4: return (ULong)read_UInt( data );
2616 case 2: return (ULong)read_UShort( data );
2617 case 1: return (ULong)read_UChar( data );
2618 default: vg_assert(0); /*NOTREACHED*/ return 0;
2622 static Long read_le_s_encoded_literal ( UChar* data, UInt size )
2624 Long s64 = read_le_u_encoded_literal( data, size );
2627 case 4: s64 <<= 32; s64 >>= 32; break;
2628 case 2: s64 <<= 48; s64 >>= 48; break;
2629 case 1: s64 <<= 56; s64 >>= 56; break;
2630 default: vg_assert(0); /*NOTREACHED*/ return 0;
2635 static UChar default_Addr_encoding ( void )
2637 switch (sizeof(Addr)) {
2638 case 4: return DW_EH_PE_udata4;
2639 case 8: return DW_EH_PE_udata8;
2640 default: vg_assert(0);
2644 static UInt size_of_encoded_Addr ( UChar encoding )
2646 if (encoding == DW_EH_PE_omit)
2649 switch (encoding & 0x07) {
2650 case DW_EH_PE_absptr: return sizeof(Addr);
2651 case DW_EH_PE_udata2: return sizeof(UShort);
2652 case DW_EH_PE_udata4: return sizeof(UInt);
2653 case DW_EH_PE_udata8: return sizeof(ULong);
2654 default: vg_assert(0);
2658 static Addr read_encoded_Addr ( /*OUT*/Int* nbytes,
2659 AddressDecodingInfo* adi,
2662 /* Regarding the handling of DW_EH_PE_absptr. DWARF3 says this
2663 denotes an absolute address, hence you would think 'base' is
2664 zero. However, that is nonsensical (unless relocations are to
2665 be applied to the unwind data before reading it, which sounds
2666 unlikely). My interpretation is that DW_EH_PE_absptr indicates
2667 an address relative to where the object was loaded (technically,
2668 relative to its stated load VMA, hence the use of text_bias
2669 rather than text_avma). Hmm, should we use text_bias or
2670 text_avma here? Not sure.
2672 This view appears to be supported by DWARF3 spec sec 7.3
2673 "Executable Objects and Shared Objects":
2675 This requirement makes the debugging information for shared
2676 objects position independent. Virtual addresses in a shared
2677 object may be calculated by adding the offset to the base
2678 address at which the object was attached. This offset is
2679 available in the run-time linker's data structures.
2683 UChar encoding = adi->encoding;
2684 UChar* ehframe_image = adi->ehframe_image;
2685 Addr ehframe_avma = adi->ehframe_avma;
2687 vg_assert((encoding & DW_EH_PE_indirect) == 0);
2691 switch (encoding & 0x70) {
2692 case DW_EH_PE_absptr:
2693 base = adi->text_bias;
2695 case DW_EH_PE_pcrel:
2696 base = ehframe_avma + ( data - ehframe_image );
2698 case DW_EH_PE_datarel:
2700 base = /* data base address */ 0;
2702 case DW_EH_PE_textrel:
2704 base = /* text base address */ 0;
2706 case DW_EH_PE_funcrel:
2709 case DW_EH_PE_aligned:
2711 offset = data - ehframe_image;
2712 if ((offset % sizeof(Addr)) != 0) {
2713 *nbytes = sizeof(Addr) - (offset % sizeof(Addr));
2721 if ((encoding & 0x07) == 0x00)
2722 encoding |= default_Addr_encoding();
2724 switch (encoding & 0x0f) {
2725 case DW_EH_PE_udata2:
2726 *nbytes += sizeof(UShort);
2727 return base + read_UShort(data);
2728 case DW_EH_PE_udata4:
2729 *nbytes += sizeof(UInt);
2730 return base + read_UInt(data);
2731 case DW_EH_PE_udata8:
2732 *nbytes += sizeof(ULong);
2733 return base + read_ULong(data);
2734 case DW_EH_PE_sdata2:
2735 *nbytes += sizeof(Short);
2736 return base + read_Short(data);
2737 case DW_EH_PE_sdata4:
2738 *nbytes += sizeof(Int);
2739 return base + read_Int(data);
2740 case DW_EH_PE_sdata8:
2741 *nbytes += sizeof(Long);
2742 return base + read_Long(data);
2744 vg_assert2(0, "read encoded address %d\n", encoding & 0x0f);
2749 /* ------------ Run/show DWARF3 expressions ---------- */
2751 /* Convert the DWARF3 expression in expr[0 .. exprlen-1] into a dag
2752 (of CfiExprs) stored in ctx->exprs, and return the index in
2753 ctx->exprs of the root node. Or fail in which case return -1. */
2754 /* IMPORTANT: when adding expression forms here, also remember to
2755 add suitable evaluation code in evalCfiExpr in debuginfo.c. */
2756 static Int dwarfexpr_to_dag ( UnwindContext* ctx,
2757 UChar* expr, Int exprlen,
2758 Bool push_cfa_at_start,
2761 # define N_EXPR_STACK 20
2763 # define PUSH(_arg) \
2765 vg_assert(sp >= -1 && sp < N_EXPR_STACK); \
2766 if (sp == N_EXPR_STACK-1) \
2769 stack[sp] = (_arg); \
2772 # define POP(_lval) \
2774 vg_assert(sp >= -1 && sp < N_EXPR_STACK); \
2777 _lval = stack[sp]; \
2788 Int sp; /* # of top element: valid is -1 .. N_EXPR_STACK-1 */
2789 Int stack[N_EXPR_STACK]; /* indices into ctx->exprs */
2790 struct UnwindContextState* ctxs = &ctx->state[ctx->state_sp];
2792 XArray* dst = ctx->exprs;
2793 UChar* limit = expr + exprlen;
2796 vg_assert(exprlen >= 0);
2798 sp = -1; /* empty */
2800 /* Synthesise the CFA as a CfiExpr */
2801 if (push_cfa_at_start) {
2802 if (ctxs->cfa_is_regoff) {
2803 /* cfa is reg +/- offset */
2804 ix = ML_(CfiExpr_Binop)( dst,
2806 ML_(CfiExpr_DwReg)( dst, ctxs->cfa_reg ),
2807 ML_(CfiExpr_Const)( dst, (UWord)(Word)ctxs->cfa_off )
2811 /* CFA is already an expr; use its root node */
2812 PUSH(ctxs->cfa_expr_ix);
2818 vg_assert(sp >= -1 && sp < N_EXPR_STACK);
2821 return -1; /* overrun - something's wrong */
2823 if (expr == limit) {
2824 /* end of expr - return expr on the top of stack. */
2826 return -1; /* stack empty. Bad. */
2831 op = 0; opname = NULL; /* excessively conservative */
2836 case DW_OP_lit0 ... DW_OP_lit31:
2837 /* push: literal 0 .. 31 */
2838 sw = (Word)opcode - (Word)DW_OP_lit0;
2839 vg_assert(sw >= 0 && sw <= 31);
2840 PUSH( ML_(CfiExpr_Const)( dst, (UWord)sw ) );
2842 VG_(printf)("DW_OP_lit%ld", sw);
2845 case DW_OP_breg0 ... DW_OP_breg31:
2846 /* push: reg + sleb128 */
2847 reg = (Int)opcode - (Int)DW_OP_breg0;
2848 vg_assert(reg >= 0 && reg <= 31);
2849 sw = read_leb128S( &expr );
2850 ix = ML_(CfiExpr_Binop)( dst,
2852 ML_(CfiExpr_DwReg)( dst, reg ),
2853 ML_(CfiExpr_Const)( dst, (UWord)sw )
2857 VG_(printf)("DW_OP_breg%d: %ld", reg, sw);
2860 case DW_OP_reg0 ... DW_OP_reg31:
2862 reg = (Int)opcode - (Int)DW_OP_reg0;
2863 vg_assert(reg >= 0 && reg <= 31);
2864 ix = ML_(CfiExpr_DwReg)( dst, reg );
2867 VG_(printf)("DW_OP_reg%d", reg);
2870 case DW_OP_plus_uconst:
2871 uw = read_leb128U( &expr );
2872 PUSH( ML_(CfiExpr_Const)( dst, uw ) );
2875 PUSH( ML_(CfiExpr_Binop)( dst, op, ix2, ix ) );
2877 VG_(printf)("DW_OP_plus_uconst: %lu", uw);
2881 /* push: 32-bit signed immediate */
2882 sw = read_le_s_encoded_literal( expr, 4 );
2884 PUSH( ML_(CfiExpr_Const)( dst, (UWord)sw ) );
2886 VG_(printf)("DW_OP_const4s: %ld", sw);
2890 /* push: 8-bit signed immediate */
2891 sw = read_le_s_encoded_literal( expr, 1 );
2893 PUSH( ML_(CfiExpr_Const)( dst, (UWord)sw ) );
2895 VG_(printf)("DW_OP_const1s: %ld", sw);
2899 op = Cop_Sub; opname = "minus"; goto binop;
2901 op = Cop_Add; opname = "plus"; goto binop;
2903 op = Cop_And; opname = "and"; goto binop;
2905 op = Cop_Mul; opname = "mul"; goto binop;
2909 PUSH( ML_(CfiExpr_Binop)( dst, op, ix2, ix ) );
2911 VG_(printf)("DW_OP_%s", opname);
2916 PUSH( ML_(CfiExpr_Deref)( dst, ix ) );
2918 VG_(printf)("DW_OP_deref");
2923 VG_(message)(Vg_DebugMsg,
2924 "Warning: DWARF2 CFI reader: unhandled DW_OP_ "
2925 "opcode 0x%x\n", (Int)opcode);
2929 if (expr < limit && ddump_frames)
2934 vg_assert(sp >= -1 && sp < N_EXPR_STACK);
2938 if (0 && ddump_frames)
2939 ML_(ppCfiExpr)( dst, stack[sp] );
2944 # undef N_EXPR_STACK
2948 /* ------------ Run/show CFI instructions ------------ */
2950 /* Run a CFI instruction, and also return its length.
2951 Returns 0 if the instruction could not be executed.
2953 static Int run_CF_instruction ( /*MOD*/UnwindContext* ctx,
2955 UnwindContext* restore_ctx,
2956 AddressDecodingInfo* adi,
2957 struct _DebugInfo* di )
2959 Int off, reg, reg2, nleb, len;
2964 UChar hi2 = (instr[i] >> 6) & 3;
2965 UChar lo6 = instr[i] & 0x3F;
2966 Addr printing_bias = ((Addr)ctx->initloc) - ((Addr)di->text_bias);
2967 struct UnwindContextState* ctxs;
2970 if (ctx->state_sp < 0 || ctx->state_sp >= N_RR_STACK)
2971 return 0; /* bogus reg-rule stack pointer */
2973 ctxs = &ctx->state[ctx->state_sp];
2974 if (hi2 == DW_CFA_advance_loc) {
2976 delta *= ctx->code_a_f;
2978 if (di->ddump_frames)
2979 VG_(printf)(" DW_CFA_advance_loc: %d to %08lx\n",
2980 (Int)delta, (Addr)ctx->loc + printing_bias);
2984 if (hi2 == DW_CFA_offset) {
2985 /* Set rule for reg 'lo6' to CFAOff(off * data_af) */
2986 off = read_leb128( &instr[i], &nleb, 0 );
2989 if (reg < 0 || reg >= N_CFI_REGS)
2990 return 0; /* fail */
2991 ctxs->reg[reg].tag = RR_CFAOff;
2992 ctxs->reg[reg].arg = off * ctx->data_a_f;
2993 if (di->ddump_frames)
2994 VG_(printf)(" DW_CFA_offset: r%d at cfa%s%d\n",
2996 ctxs->reg[reg].arg < 0 ? "" : "+",
2997 (Int)ctxs->reg[reg].arg );
3001 if (hi2 == DW_CFA_restore) {
3003 if (reg < 0 || reg >= N_CFI_REGS)
3004 return 0; /* fail */
3005 if (restore_ctx == NULL)
3006 return 0; /* fail */
3007 ctxs->reg[reg] = restore_ctx->state[restore_ctx->state_sp].reg[reg];
3008 if (di->ddump_frames)
3009 VG_(printf)(" DW_CFA_restore: r%d\n", (Int)reg);
3013 vg_assert(hi2 == DW_CFA_use_secondary);
3017 if (di->ddump_frames)
3018 VG_(printf)(" DW_CFA_nop\n");
3020 case DW_CFA_set_loc:
3022 ctx->loc = read_Addr(&instr[i]) - ctx->initloc; i+= sizeof(Addr);
3023 Was this ever right? */
3024 /* 2007 Feb 23: No. binutils/dwarf.c treats it as an encoded
3025 address and that appears to be in accordance with the
3027 ctx->loc = read_encoded_Addr(&len, adi, &instr[i]);
3029 if (di->ddump_frames)
3030 VG_(printf)(" rci:DW_CFA_set_loc\n");
3032 case DW_CFA_advance_loc1:
3033 delta = (UInt)read_UChar(&instr[i]); i+= sizeof(UChar);
3034 delta *= ctx->code_a_f;
3036 if (di->ddump_frames)
3037 VG_(printf)(" DW_CFA_advance_loc1: %d to %08lx\n",
3038 (Int)delta, (Addr)ctx->loc + printing_bias);
3040 case DW_CFA_advance_loc2:
3041 delta = (UInt)read_UShort(&instr[i]); i+= sizeof(UShort);
3042 delta *= ctx->code_a_f;
3044 if (di->ddump_frames)
3045 VG_(printf)(" DW_CFA_advance_loc2: %d to %08lx\n",
3046 (Int)delta, (Addr)ctx->loc + printing_bias);
3048 case DW_CFA_advance_loc4:
3049 delta = (UInt)read_UInt(&instr[i]); i+= sizeof(UInt);
3050 delta *= ctx->code_a_f;
3052 if (di->ddump_frames)
3053 VG_(printf)(" DW_CFA_advance_loc4: %d to %08lx\n",
3054 (Int)delta, (Addr)ctx->loc + printing_bias);
3057 case DW_CFA_def_cfa:
3058 reg = read_leb128( &instr[i], &nleb, 0 );
3060 off = read_leb128( &instr[i], &nleb, 0 );
3062 if (reg < 0 || reg >= N_CFI_REGS)
3063 return 0; /* fail */
3064 ctxs->cfa_is_regoff = True;
3065 ctxs->cfa_expr_ix = 0;
3066 ctxs->cfa_reg = reg;
3067 ctxs->cfa_off = off;
3068 if (di->ddump_frames)
3069 VG_(printf)(" DW_CFA_def_cfa: r%d ofs %d\n", (Int)reg, (Int)off);
3072 case DW_CFA_def_cfa_sf:
3073 reg = read_leb128( &instr[i], &nleb, 0 );
3075 off = read_leb128( &instr[i], &nleb, 1 );
3077 if (reg < 0 || reg >= N_CFI_REGS)
3078 return 0; /* fail */
3079 ctxs->cfa_is_regoff = True;
3080 ctxs->cfa_expr_ix = 0;
3081 ctxs->cfa_reg = reg;
3082 ctxs->cfa_off = off * ctx->data_a_f;
3083 if (di->ddump_frames)
3084 VG_(printf)(" rci:DW_CFA_def_cfa_sf\n");
3087 case DW_CFA_register:
3088 reg = read_leb128( &instr[i], &nleb, 0);
3090 reg2 = read_leb128( &instr[i], &nleb, 0);
3092 if (reg < 0 || reg >= N_CFI_REGS)
3093 return 0; /* fail */
3094 if (reg2 < 0 || reg2 >= N_CFI_REGS)
3095 return 0; /* fail */
3096 ctxs->reg[reg].tag = RR_Reg;
3097 ctxs->reg[reg].arg = reg2;
3098 if (di->ddump_frames)
3099 VG_(printf)(" DW_CFA_register: r%d in r%d\n",
3100 (Int)reg, (Int)reg2);
3103 case DW_CFA_offset_extended:
3104 reg = read_leb128( &instr[i], &nleb, 0 );
3106 off = read_leb128( &instr[i], &nleb, 0 );
3108 if (reg < 0 || reg >= N_CFI_REGS)
3109 return 0; /* fail */
3110 ctxs->reg[reg].tag = RR_CFAOff;
3111 ctxs->reg[reg].arg = off * ctx->data_a_f;
3112 if (di->ddump_frames)
3113 VG_(printf)(" rci:DW_CFA_offset_extended\n");
3116 case DW_CFA_offset_extended_sf:
3117 reg = read_leb128( &instr[i], &nleb, 0 );
3119 off = read_leb128( &instr[i], &nleb, 1 );
3121 if (reg < 0 || reg >= N_CFI_REGS)
3122 return 0; /* fail */
3123 ctxs->reg[reg].tag = RR_CFAOff;
3124 ctxs->reg[reg].arg = off * ctx->data_a_f;
3125 if (di->ddump_frames)
3126 VG_(printf)(" DW_CFA_offset_extended_sf: r%d at cfa%s%d\n",
3128 ctxs->reg[reg].arg < 0 ? "" : "+",
3129 (Int)ctxs->reg[reg].arg);
3132 case DW_CFA_GNU_negative_offset_extended:
3133 reg = read_leb128( &instr[i], &nleb, 0 );
3135 off = read_leb128( &instr[i], &nleb, 0 );
3137 if (reg < 0 || reg >= N_CFI_REGS)
3138 return 0; /* fail */
3139 ctxs->reg[reg].tag = RR_CFAOff;
3140 ctxs->reg[reg].arg = (-off) * ctx->data_a_f;
3141 if (di->ddump_frames)
3142 VG_(printf)(" rci:DW_CFA_GNU_negative_offset_extended\n");
3145 case DW_CFA_restore_extended:
3146 reg = read_leb128( &instr[i], &nleb, 0 );
3148 if (reg < 0 || reg >= N_CFI_REGS)
3149 return 0; /* fail */
3150 if (restore_ctx == NULL)
3151 return 0; /* fail */
3152 ctxs->reg[reg] = restore_ctx->state[restore_ctx->state_sp].reg[reg];
3153 if (di->ddump_frames)
3154 VG_(printf)(" rci:DW_CFA_restore_extended\n");
3157 case DW_CFA_val_offset:
3158 reg = read_leb128( &instr[i], &nleb, 0 );
3160 off = read_leb128( &instr[i], &nleb, 0 );
3162 if (reg < 0 || reg >= N_CFI_REGS)
3163 return 0; /* fail */
3164 ctxs->reg[reg].tag = RR_CFAValOff;
3165 ctxs->reg[reg].arg = off * ctx->data_a_f;
3166 if (di->ddump_frames)
3167 VG_(printf)(" rci:DW_CFA_val_offset\n");
3170 case DW_CFA_val_offset_sf:
3171 reg = read_leb128( &instr[i], &nleb, 0 );
3173 off = read_leb128( &instr[i], &nleb, 1 );
3175 if (reg < 0 || reg >= N_CFI_REGS)
3176 return 0; /* fail */
3177 ctxs->reg[reg].tag = RR_CFAValOff;
3178 ctxs->reg[reg].arg = off * ctx->data_a_f;
3179 if (di->ddump_frames)
3180 VG_(printf)(" rci:DW_CFA_val_offset_sf\n");
3183 case DW_CFA_def_cfa_register:
3184 reg = read_leb128( &instr[i], &nleb, 0);
3186 if (reg < 0 || reg >= N_CFI_REGS)
3187 return 0; /* fail */
3188 ctxs->cfa_is_regoff = True;
3189 ctxs->cfa_expr_ix = 0;
3190 ctxs->cfa_reg = reg;
3191 /* ->cfa_off unchanged */
3192 if (di->ddump_frames)
3193 VG_(printf)(" DW_CFA_def_cfa_register: r%d\n", (Int)reg );
3196 case DW_CFA_def_cfa_offset:
3197 off = read_leb128( &instr[i], &nleb, 0);
3199 ctxs->cfa_is_regoff = True;
3200 ctxs->cfa_expr_ix = 0;
3201 /* ->reg is unchanged */
3202 ctxs->cfa_off = off;
3203 if (di->ddump_frames)
3204 VG_(printf)(" DW_CFA_def_cfa_offset: %d\n", (Int)off);
3207 case DW_CFA_def_cfa_offset_sf:
3208 off = read_leb128( &instr[i], &nleb, 1);
3210 ctxs->cfa_is_regoff = True;
3211 ctxs->cfa_expr_ix = 0;
3212 /* ->reg is unchanged */
3213 ctxs->cfa_off = off * ctx->data_a_f;
3214 if (di->ddump_frames)
3215 VG_(printf)(" DW_CFA_def_cfa_offset_sf: %d\n", ctxs->cfa_off);
3218 case DW_CFA_undefined:
3219 reg = read_leb128( &instr[i], &nleb, 0);
3221 if (reg < 0 || reg >= N_CFI_REGS)
3222 return 0; /* fail */
3223 ctxs->reg[reg].tag = RR_Undef;
3224 ctxs->reg[reg].arg = 0;
3225 if (di->ddump_frames)
3226 VG_(printf)(" rci:DW_CFA_undefined\n");
3229 case DW_CFA_same_value:
3230 reg = read_leb128( &instr[i], &nleb, 0);
3232 if (reg < 0 || reg >= N_CFI_REGS)
3233 return 0; /* fail */
3234 ctxs->reg[reg].tag = RR_Same;
3235 ctxs->reg[reg].arg = 0;
3236 if (di->ddump_frames)
3237 VG_(printf)(" rci:DW_CFA_same_value\n");
3240 case DW_CFA_GNU_args_size:
3241 /* No idea what is supposed to happen. gdb-6.3 simply
3243 /*off = */ read_leb128( &instr[i], &nleb, 0 );
3245 if (di->ddump_frames)
3246 VG_(printf)(" rci:DW_CFA_GNU_args_size (ignored)\n");
3249 case DW_CFA_expression:
3250 /* Identical to DW_CFA_val_expression except that the value
3251 computed is an address and so needs one final
3253 reg = read_leb128( &instr[i], &nleb, 0 );
3255 len = read_leb128( &instr[i], &nleb, 0 );
3259 if (reg < 0 || reg >= N_CFI_REGS)
3260 return 0; /* fail */
3261 if (di->ddump_frames)
3262 VG_(printf)(" DW_CFA_expression: r%d (",
3264 /* Convert the expression into a dag rooted at ctx->exprs index j,
3266 j = dwarfexpr_to_dag ( ctx, expr, len, True/*push CFA at start*/,
3268 if (di->ddump_frames)
3272 vg_assert(ctx->exprs);
3273 vg_assert( j < VG_(sizeXA)(ctx->exprs) );
3276 return 0; /* fail */
3277 /* Add an extra dereference */
3278 j = ML_(CfiExpr_Deref)( ctx->exprs, j );
3279 ctxs->reg[reg].tag = RR_ValExpr;
3280 ctxs->reg[reg].arg = j;
3283 case DW_CFA_val_expression:
3284 reg = read_leb128( &instr[i], &nleb, 0 );
3286 len = read_leb128( &instr[i], &nleb, 0 );
3290 if (reg < 0 || reg >= N_CFI_REGS)
3291 return 0; /* fail */
3292 if (di->ddump_frames)
3293 VG_(printf)(" DW_CFA_val_expression: r%d (",
3295 /* Convert the expression into a dag rooted at ctx->exprs index j,
3297 j = dwarfexpr_to_dag ( ctx, expr, len, True/*push CFA at start*/,
3299 if (di->ddump_frames)
3303 vg_assert(ctx->exprs);
3304 vg_assert( j < VG_(sizeXA)(ctx->exprs) );
3307 return 0; /* fail */
3308 ctxs->reg[reg].tag = RR_ValExpr;
3309 ctxs->reg[reg].arg = j;
3312 case DW_CFA_def_cfa_expression:
3313 len = read_leb128( &instr[i], &nleb, 0 );
3317 if (di->ddump_frames)
3318 VG_(printf)(" DW_CFA_def_cfa_expression (");
3319 /* Convert the expression into a dag rooted at ctx->exprs index j,
3321 j = dwarfexpr_to_dag ( ctx, expr, len, True/*push CFA at start*/,
3323 if (di->ddump_frames)
3325 ctxs->cfa_is_regoff = False;
3328 ctxs->cfa_expr_ix = j;
3331 case DW_CFA_GNU_window_save:
3332 /* Ignored. This appears to be sparc-specific; quite why it
3333 turns up in SuSE-supplied x86 .so's beats me. */
3334 if (di->ddump_frames)
3335 VG_(printf)(" DW_CFA_GNU_window_save\n");
3338 case DW_CFA_remember_state:
3339 if (di->ddump_frames)
3340 VG_(printf)(" DW_CFA_remember_state\n");
3341 /* we just checked this at entry, so: */
3342 vg_assert(ctx->state_sp >= 0 && ctx->state_sp < N_RR_STACK);
3344 if (ctx->state_sp == N_RR_STACK) {
3345 /* stack overflow. We're hosed. */
3346 VG_(message)(Vg_DebugMsg, "DWARF2 CFI reader: N_RR_STACK is "
3347 "too low; increase and recompile.");
3348 i = 0; /* indicate failure */
3350 VG_(memcpy)(/*dst*/&ctx->state[ctx->state_sp],
3351 /*src*/&ctx->state[ctx->state_sp - 1],
3352 sizeof(ctx->state[ctx->state_sp]) );
3356 case DW_CFA_restore_state:
3357 if (di->ddump_frames)
3358 VG_(printf)(" DW_CFA_restore_state\n");
3359 /* we just checked this at entry, so: */
3360 vg_assert(ctx->state_sp >= 0 && ctx->state_sp < N_RR_STACK);
3361 if (ctx->state_sp == 0) {
3362 /* stack overflow. Give up. */
3363 i = 0; /* indicate failure */
3365 /* simply fall back to previous entry */
3371 VG_(message)(Vg_DebugMsg, "DWARF2 CFI reader: unhandled CFI "
3372 "instruction 0:%d\n", (Int)lo6);
3373 if (di->ddump_frames)
3374 VG_(printf)(" rci:run_CF_instruction:default\n");
3383 /* Show a CFI instruction, and also return its length. Show it as
3384 close as possible (preferably identical) to how GNU binutils
3385 readelf --debug-dump=frames would. */
3387 static Int show_CF_instruction ( UChar* instr,
3388 AddressDecodingInfo* adi,
3389 Int code_a_f, Int data_a_f )
3392 Int off, coff, reg, reg2, nleb, len;
3395 UChar hi2 = (instr[i] >> 6) & 3;
3396 UChar lo6 = instr[i] & 0x3F;
3399 if (0) VG_(printf)("raw:%x/%x:%x:%x:%x:%x:%x:%x:%x:%x\n",
3401 instr[i+0], instr[i+1], instr[i+2], instr[i+3],
3402 instr[i+4], instr[i+5], instr[i+6], instr[i+7] );
3404 if (hi2 == DW_CFA_advance_loc) {
3405 VG_(printf)(" sci:DW_CFA_advance_loc(%d)\n", (Int)lo6);
3409 if (hi2 == DW_CFA_offset) {
3410 off = read_leb128( &instr[i], &nleb, 0 );
3412 coff = off * data_a_f;
3413 VG_(printf)(" DW_CFA_offset: r%d at cfa%s%d\n",
3414 (Int)lo6, coff < 0 ? "" : "+", (Int)coff );
3418 if (hi2 == DW_CFA_restore) {
3419 VG_(printf)(" sci:DW_CFA_restore(r%d)\n", (Int)lo6);
3423 vg_assert(hi2 == DW_CFA_use_secondary);
3428 VG_(printf)(" DW_CFA_nop\n");
3431 case DW_CFA_set_loc:
3432 /* WAS: loc = read_Addr(&instr[i]); i+= sizeof(Addr);
3433 (now known to be incorrect -- the address is encoded) */
3434 loc = read_encoded_Addr(&len, adi, &instr[i]);
3436 VG_(printf)(" sci:DW_CFA_set_loc(%#lx)\n", loc);
3439 case DW_CFA_advance_loc1:
3440 delta = (UInt)read_UChar(&instr[i]); i+= sizeof(UChar);
3441 VG_(printf)(" sci:DW_CFA_advance_loc1(%d)\n", delta);
3444 case DW_CFA_advance_loc2:
3445 delta = (UInt)read_UShort(&instr[i]); i+= sizeof(UShort);
3446 VG_(printf)(" sci:DW_CFA_advance_loc2(%d)\n", delta);
3449 case DW_CFA_advance_loc4:
3450 delta = (UInt)read_UInt(&instr[i]); i+= sizeof(UInt);
3451 VG_(printf)(" DW_CFA_advance_loc4(%d)\n", delta);
3454 case DW_CFA_def_cfa:
3455 reg = read_leb128( &instr[i], &nleb, 0 );
3457 off = read_leb128( &instr[i], &nleb, 0 );
3459 VG_(printf)(" DW_CFA_def_cfa: r%d ofs %d\n", (Int)reg, (Int)off);
3462 case DW_CFA_def_cfa_sf:
3463 reg = read_leb128( &instr[i], &nleb, 0 );
3465 off = read_leb128( &instr[i], &nleb, 1 );
3467 VG_(printf)(" DW_CFA_def_cfa_sf: r%d ofs %d\n",
3468 (Int)reg, (Int)(off * data_a_f));
3471 case DW_CFA_register:
3472 reg = read_leb128( &instr[i], &nleb, 0);
3474 reg2 = read_leb128( &instr[i], &nleb, 0);
3476 VG_(printf)(" sci:DW_CFA_register(r%d, r%d)\n", reg, reg2);
3479 case DW_CFA_def_cfa_register:
3480 reg = read_leb128( &instr[i], &nleb, 0);
3482 VG_(printf)(" sci:DW_CFA_def_cfa_register(r%d)\n", reg);
3485 case DW_CFA_def_cfa_offset:
3486 off = read_leb128( &instr[i], &nleb, 0);
3488 VG_(printf)(" sci:DW_CFA_def_cfa_offset(%d)\n", off);
3491 case DW_CFA_def_cfa_offset_sf:
3492 off = read_leb128( &instr[i], &nleb, 1);
3494 VG_(printf)(" sci:DW_CFA_def_cfa_offset_sf(%d)\n", off);
3497 case DW_CFA_restore_extended:
3498 reg = read_leb128( &instr[i], &nleb, 0);
3500 VG_(printf)(" sci:DW_CFA_restore_extended(r%d)\n", reg);
3503 case DW_CFA_undefined:
3504 reg = read_leb128( &instr[i], &nleb, 0);
3506 VG_(printf)(" sci:DW_CFA_undefined(r%d)\n", reg);
3509 case DW_CFA_same_value:
3510 reg = read_leb128( &instr[i], &nleb, 0);
3512 VG_(printf)(" sci:DW_CFA_same_value(r%d)\n", reg);
3515 case DW_CFA_remember_state:
3516 VG_(printf)(" sci:DW_CFA_remember_state\n");
3519 case DW_CFA_restore_state:
3520 VG_(printf)(" sci:DW_CFA_restore_state\n");
3523 case DW_CFA_GNU_args_size:
3524 off = read_leb128( &instr[i], &nleb, 0 );
3526 VG_(printf)(" sci:DW_CFA_GNU_args_size(%d)\n", off );
3529 case DW_CFA_def_cfa_expression:
3530 len = read_leb128( &instr[i], &nleb, 0 );
3533 VG_(printf)(" sci:DW_CFA_def_cfa_expression(length %d)\n", len);
3536 case DW_CFA_expression:
3537 reg = read_leb128( &instr[i], &nleb, 0 );
3539 len = read_leb128( &instr[i], &nleb, 0 );
3542 VG_(printf)(" sci:DW_CFA_expression(r%d, length %d)\n", reg, len);
3545 case DW_CFA_val_expression:
3546 reg = read_leb128( &instr[i], &nleb, 0 );
3548 len = read_leb128( &instr[i], &nleb, 0 );
3551 VG_(printf)(" sci:DW_CFA_val_expression(r%d, length %d)\n", reg, len);
3554 case DW_CFA_offset_extended:
3555 reg = read_leb128( &instr[i], &nleb, 0 );
3557 off = read_leb128( &instr[i], &nleb, 0 );
3559 VG_(printf)(" sci:DW_CFA_offset_extended(r%d, "
3560 "off %d x data_af)\n", reg, off);
3563 case DW_CFA_offset_extended_sf:
3564 reg = read_leb128( &instr[i], &nleb, 0 );
3566 off = read_leb128( &instr[i], &nleb, 1 );
3568 coff = (Int)(off * data_a_f);
3569 VG_(printf)(" DW_CFA_offset_extended_sf: r%d at cfa%s%d\n",
3570 reg, coff < 0 ? "" : "+", coff);
3573 case DW_CFA_GNU_negative_offset_extended:
3574 reg = read_leb128( &instr[i], &nleb, 0 );
3576 off = read_leb128( &instr[i], &nleb, 0 );
3578 VG_(printf)(" sci:DW_CFA_GNU_negative_offset_extended"
3579 "(r%d, off %d x data_af)\n", reg, -off);
3582 case DW_CFA_val_offset:
3583 reg = read_leb128( &instr[i], &nleb, 0 );
3585 off = read_leb128( &instr[i], &nleb, 0 );
3587 VG_(printf)(" sci:DW_CFA_val_offset(r%d, off %d x data_af)\n",
3591 case DW_CFA_val_offset_sf:
3592 reg = read_leb128( &instr[i], &nleb, 0 );
3594 off = read_leb128( &instr[i], &nleb, 1 );
3596 VG_(printf)(" sci:DW_CFA_val_offset_sf(r%d, off %d x data_af)\n",
3600 case DW_CFA_GNU_window_save:
3601 VG_(printf)(" sci:DW_CFA_GNU_window_save\n");
3605 VG_(printf)(" sci:0:%d\n", (Int)lo6);
3613 /* Show the instructions in instrs[0 .. ilen-1]. */
3614 static void show_CF_instructions ( UChar* instrs, Int ilen,
3615 AddressDecodingInfo* adi,
3616 Int code_a_f, Int data_a_f )
3620 if (i >= ilen) break;
3621 i += show_CF_instruction( &instrs[i], adi, code_a_f, data_a_f );
3626 /* Run the CF instructions in instrs[0 .. ilen-1], until the end is
3627 reached, or until there is a failure. Return True iff success.
3630 Bool run_CF_instructions ( struct _DebugInfo* di,
3632 UnwindContext* ctx, UChar* instrs, Int ilen,
3634 UnwindContext* restore_ctx,
3635 AddressDecodingInfo* adi )
3641 if (0) ppUnwindContext(ctx);
3642 if (0) ppUnwindContext_summary(ctx);
3644 loc_prev = ctx->loc;
3645 if (i >= ilen) break;
3646 if (0) (void)show_CF_instruction( &instrs[i], adi,
3647 ctx->code_a_f, ctx->data_a_f );
3648 j = run_CF_instruction( ctx, &instrs[i], restore_ctx, adi, di );
3650 return False; /* execution failed */
3652 if (0) ppUnwindContext(ctx);
3653 if (record && loc_prev != ctx->loc) {
3654 summ_ok = summarise_context ( &cfsi, loc_prev, ctx, di );
3656 ML_(addDiCfSI)(di, &cfsi);
3658 ML_(ppDiCfSI)(di->cfsi_exprs, &cfsi);
3662 if (ctx->loc < fde_arange) {
3663 loc_prev = ctx->loc;
3664 ctx->loc = fde_arange;
3666 summ_ok = summarise_context ( &cfsi, loc_prev, ctx, di );
3668 ML_(addDiCfSI)(di, &cfsi);
3670 ML_(ppDiCfSI)(di->cfsi_exprs, &cfsi);
3678 /* ------------ Main entry point for CFI reading ------------ */
3682 /* This gives the CIE an identity to which FDEs will refer. */
3684 /* Code, data factors. */
3687 /* Return-address pseudo-register. */
3689 UChar address_encoding;
3690 /* Where are the instrs? Note, this are simply pointers back to
3691 the transiently-mapped-in section. */
3694 /* God knows .. don't ask */
3695 Bool saw_z_augmentation;
3699 static void init_CIE ( CIE* cie )
3705 cie->address_encoding = 0;
3708 cie->saw_z_augmentation = False;
3712 static CIE the_CIEs[N_CIEs];
3715 void ML_(read_callframe_info_dwarf3)
3716 ( /*OUT*/struct _DebugInfo* di, UChar* frame_image, SizeT frame_size,
3722 UChar* data = frame_image;
3723 UWord ehframe_cfsis = 0;
3724 Addr frame_avma = for_eh ? di->ehframe_avma : 0;
3726 # if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
3727 /* These targets don't use CFI-based stack unwinding. */
3731 /* If we are reading .debug_frame after .eh_frame has been read, only
3732 add FDEs which weren't covered in .eh_frame. To be able to quickly
3733 search the FDEs, the records must be sorted. */
3734 if ( ! for_eh && di->ehframe_size && di->cfsi_used ) {
3735 ML_(canonicaliseCFI) ( di );
3736 ehframe_cfsis = di->cfsi_used;
3739 if (di->trace_cfi) {
3740 VG_(printf)("\n-----------------------------------------------\n");
3741 VG_(printf)("CFI info: szB %ld, _avma %#lx, _image %p\n",
3742 frame_size, frame_avma, frame_image );
3743 VG_(printf)("CFI info: name %s\n",
3747 /* Loop over CIEs/FDEs */
3749 /* Conceptually, the frame info is a sequence of FDEs, one for each
3750 function. Inside an FDE is a miniature program for a special
3751 state machine, which, when run, produces the stack-unwinding
3752 info for that function.
3754 Because the FDEs typically have much in common, and because the
3755 DWARF designers appear to have been fanatical about space
3756 saving, the common parts are factored out into so-called CIEs.
3757 That means that what we traverse is a sequence of structs, each
3758 of which is either a FDE (usually) or a CIE (occasionally).
3759 Each FDE has a field indicating which CIE is the one pertaining
3762 The following loop traverses the sequence. FDEs are dealt with
3763 immediately; once we harvest the useful info in an FDE, it is
3764 then forgotten about. By contrast, CIEs are validated and
3765 dumped into an array, because later FDEs may refer to any
3766 previously-seen CIE.
3769 UChar* ciefde_start;
3775 if (data == frame_image + frame_size)
3778 /* Overshot the end? Means something is wrong */
3779 if (data > frame_image + frame_size) {
3780 how = "overran the end of .eh_frame";
3784 /* Ok, we must be looking at the start of a new CIE or FDE.
3785 Figure out which it is. */
3787 ciefde_start = data;
3789 VG_(printf)("\ncie/fde.start = %p (frame_image + 0x%lx)\n",
3791 ciefde_start - frame_image + 0UL);
3793 ciefde_len = (ULong) read_UInt(data); data += sizeof(UInt);
3795 VG_(printf)("cie/fde.length = %lld\n", ciefde_len);
3797 /* Apparently, if the .length field is zero, we are at the end
3798 of the sequence. This is stated in the Generic Elf
3799 Specification (see comments far above here) and is one of the
3800 places where .eh_frame and .debug_frame data differ. */
3801 if (ciefde_len == 0) {
3802 if (di->ddump_frames)
3803 VG_(printf)("%08lx ZERO terminator\n\n",
3804 ((Addr)ciefde_start) - ((Addr)frame_image));
3808 /* If the .length field is 0xFFFFFFFF then we're dealing with
3809 64-bit DWARF, and the real length is stored as a 64-bit
3810 number immediately following it. */
3812 if (ciefde_len == 0xFFFFFFFFUL) {
3814 ciefde_len = read_ULong(data); data += sizeof(ULong);
3817 /* Now get the CIE ID, whose size depends on the DWARF 32 vs
3820 cie_pointer = read_ULong(data);
3821 data += sizeof(ULong); /* XXX see XXX below */
3823 cie_pointer = (ULong)read_UInt(data);
3824 data += sizeof(UInt); /* XXX see XXX below */
3828 VG_(printf)("cie.pointer = %lld\n", cie_pointer);
3830 /* If cie_pointer is zero for .eh_frame or all ones for .debug_frame,
3831 we've got a CIE; else it's an FDE. */
3832 if (cie_pointer == (for_eh ? 0ULL
3833 : dw64 ? 0xFFFFFFFFFFFFFFFFULL : 0xFFFFFFFFULL)) {
3837 UChar* cie_augmentation;
3839 /* --------- CIE --------- */
3841 VG_(printf)("------ new CIE (#%d of 0 .. %d) ------\n",
3842 n_CIEs, N_CIEs - 1);
3844 /* Allocate a new CIE record. */
3845 vg_assert(n_CIEs >= 0 && n_CIEs <= N_CIEs);
3846 if (n_CIEs == N_CIEs) {
3847 how = "N_CIEs is too low. Increase and recompile.";
3853 init_CIE( &the_CIEs[this_CIE] );
3855 /* Record its offset. This is how we will find it again
3856 later when looking at an FDE. */
3857 the_CIEs[this_CIE].offset = (ULong)(ciefde_start - frame_image);
3859 if (di->ddump_frames)
3860 VG_(printf)("%08lx %08lx %08lx CIE\n",
3861 ((Addr)ciefde_start) - ((Addr)frame_image),
3863 (Addr)(UWord)cie_pointer );
3865 cie_version = read_UChar(data); data += sizeof(UChar);
3867 VG_(printf)("cie.version = %d\n", (Int)cie_version);
3868 if (di->ddump_frames)
3869 VG_(printf)(" Version: %d\n", (Int)cie_version);
3870 if (cie_version != 1 && cie_version != 3 && cie_version != 4) {
3871 how = "unexpected CIE version (not 1 nor 3 nor 4)";
3875 cie_augmentation = data;
3876 data += 1 + VG_(strlen)(cie_augmentation);
3878 VG_(printf)("cie.augment = \"%s\"\n", cie_augmentation);
3879 if (di->ddump_frames)
3880 VG_(printf)(" Augmentation: \"%s\"\n", cie_augmentation);
3882 if (cie_augmentation[0] == 'e' && cie_augmentation[1] == 'h') {
3883 data += sizeof(Addr);
3884 cie_augmentation += 2;
3887 if (cie_version >= 4) {
3888 if (read_UChar(data) != sizeof(Addr)) {
3889 how = "unexpected address size";
3892 data += sizeof(UChar);
3893 if (read_UChar(data) != 0) {
3894 how = "unexpected non-zero segment size";
3897 data += sizeof(UChar);
3900 the_CIEs[this_CIE].code_a_f = read_leb128( data, &nbytes, 0);
3903 VG_(printf)("cie.code_af = %d\n",
3904 the_CIEs[this_CIE].code_a_f);
3905 if (di->ddump_frames)
3906 VG_(printf)(" Code alignment factor: %d\n",
3907 (Int)the_CIEs[this_CIE].code_a_f);
3909 the_CIEs[this_CIE].data_a_f = read_leb128( data, &nbytes, 1);
3912 VG_(printf)("cie.data_af = %d\n",
3913 the_CIEs[this_CIE].data_a_f);
3914 if (di->ddump_frames)
3915 VG_(printf)(" Data alignment factor: %d\n",
3916 (Int)the_CIEs[this_CIE].data_a_f);
3918 if (cie_version == 1) {
3919 the_CIEs[this_CIE].ra_reg = (Int)read_UChar(data);
3920 data += sizeof(UChar);
3922 the_CIEs[this_CIE].ra_reg = read_leb128( data, &nbytes, 0);
3926 VG_(printf)("cie.ra_reg = %d\n",
3927 the_CIEs[this_CIE].ra_reg);
3928 if (di->ddump_frames)
3929 VG_(printf)(" Return address column: %d\n",
3930 (Int)the_CIEs[this_CIE].ra_reg);
3932 if (the_CIEs[this_CIE].ra_reg < 0
3933 || the_CIEs[this_CIE].ra_reg >= N_CFI_REGS) {
3934 how = "cie.ra_reg has implausible value";
3938 the_CIEs[this_CIE].saw_z_augmentation
3939 = *cie_augmentation == 'z';
3940 if (the_CIEs[this_CIE].saw_z_augmentation) {
3941 UInt length = read_leb128( data, &nbytes, 0);
3943 the_CIEs[this_CIE].instrs = data + length;
3945 if (di->ddump_frames) {
3947 VG_(printf)(" Augmentation data: ");
3948 for (i = 0; i < length; i++)
3949 VG_(printf)(" %02x", (UInt)data[i]);
3953 the_CIEs[this_CIE].instrs = NULL;
3956 the_CIEs[this_CIE].address_encoding = default_Addr_encoding();
3958 while (*cie_augmentation) {
3959 switch (*cie_augmentation) {
3965 the_CIEs[this_CIE].address_encoding
3966 = read_UChar(data); data += sizeof(UChar);
3970 data += size_of_encoded_Addr( read_UChar(data) );
3978 if (the_CIEs[this_CIE].instrs == NULL) {
3979 how = "unhandled cie.augmentation";
3982 data = the_CIEs[this_CIE].instrs;
3983 goto done_augmentation;
3990 VG_(printf)("cie.encoding = 0x%x\n",
3991 the_CIEs[this_CIE].address_encoding);
3993 the_CIEs[this_CIE].instrs = data;
3994 the_CIEs[this_CIE].ilen
3995 = ciefde_start + ciefde_len + sizeof(UInt) - data;
3996 if (di->trace_cfi) {
3997 VG_(printf)("cie.instrs = %p\n", the_CIEs[this_CIE].instrs);
3998 VG_(printf)("cie.ilen = %d\n", the_CIEs[this_CIE].ilen);
4001 if (the_CIEs[this_CIE].ilen < 0
4002 || the_CIEs[this_CIE].ilen > frame_size) {
4003 how = "implausible # cie initial insns";
4007 data += the_CIEs[this_CIE].ilen;
4009 /* Show the CIE's instructions (the preamble for each FDE
4010 that uses this CIE). */
4011 if (di->ddump_frames)
4014 if (di->trace_cfi || di->ddump_frames) {
4015 AddressDecodingInfo adi;
4016 adi.encoding = the_CIEs[this_CIE].address_encoding;
4017 adi.ehframe_image = frame_image;
4018 adi.ehframe_avma = frame_avma;
4019 adi.text_bias = di->text_debug_bias;
4020 show_CF_instructions( the_CIEs[this_CIE].instrs,
4021 the_CIEs[this_CIE].ilen, &adi,
4022 the_CIEs[this_CIE].code_a_f,
4023 the_CIEs[this_CIE].data_a_f );
4026 if (di->ddump_frames)
4031 AddressDecodingInfo adi;
4032 UnwindContext ctx, restore_ctx;
4041 /* --------- FDE --------- */
4043 /* Find the relevant CIE. The CIE we want is located
4044 cie_pointer bytes back from here. */
4046 /* re sizeof(UInt) / sizeof(ULong), matches XXX above. */
4048 look_for = (data - (dw64 ? sizeof(ULong) : sizeof(UInt))
4052 look_for = cie_pointer;
4054 for (cie = 0; cie < n_CIEs; cie++) {
4055 if (0) VG_(printf)("look for %lld %lld\n",
4056 look_for, the_CIEs[cie].offset );
4057 if (the_CIEs[cie].offset == look_for)
4060 vg_assert(cie >= 0 && cie <= n_CIEs);
4061 if (cie == n_CIEs) {
4062 how = "FDE refers to not-findable CIE";
4066 adi.encoding = the_CIEs[cie].address_encoding;
4067 adi.ehframe_image = frame_image;
4068 adi.ehframe_avma = frame_avma;
4069 adi.text_bias = di->text_debug_bias;
4070 fde_initloc = read_encoded_Addr(&nbytes, &adi, data);
4073 VG_(printf)("fde.initloc = %#lx\n", fde_initloc);
4075 adi.encoding = the_CIEs[cie].address_encoding & 0xf;
4076 adi.ehframe_image = frame_image;
4077 adi.ehframe_avma = frame_avma;
4078 adi.text_bias = di->text_debug_bias;
4080 /* WAS (incorrectly):
4081 fde_arange = read_encoded_Addr(&nbytes, &adi, data);
4083 The following corresponds to what binutils/dwarf.c does:
4085 { UInt ptr_size = size_of_encoded_Addr( adi.encoding );
4087 case 8: case 4: case 2: case 1:
4089 = (UWord)read_le_u_encoded_literal(data, ptr_size);
4093 how = "unknown arange field encoding in FDE";
4099 VG_(printf)("fde.arangec = %#lx\n", fde_arange);
4101 if (di->ddump_frames)
4102 VG_(printf)("%08lx %08lx %08lx FDE cie=%08lx pc=%08lx..%08lx\n",
4103 ((Addr)ciefde_start) - ((Addr)frame_image),
4105 (Addr)(UWord)cie_pointer,
4107 ((Addr)fde_initloc) - di->text_debug_bias,
4108 ((Addr)fde_initloc) - di->text_debug_bias + fde_arange);
4110 if (the_CIEs[cie].saw_z_augmentation) {
4111 UInt length = read_leb128( data, &nbytes, 0);
4113 if (di->ddump_frames && (length > 0)) {
4115 VG_(printf)(" Augmentation data: ");
4116 for (i = 0; i < length; i++)
4117 VG_(printf)(" %02x", (UInt)data[i]);
4118 VG_(printf)("\n\n");
4124 fde_ilen = ciefde_start + ciefde_len + sizeof(UInt) - data;
4125 if (di->trace_cfi) {
4126 VG_(printf)("fde.instrs = %p\n", fde_instrs);
4127 VG_(printf)("fde.ilen = %d\n", (Int)fde_ilen);
4130 if (fde_ilen < 0 || fde_ilen > frame_size) {
4131 how = "implausible # fde insns";
4137 if (ehframe_cfsis) {
4138 Addr a_mid_lo, a_mid_hi;
4141 hi = ehframe_cfsis-1;
4143 /* current unsearched space is from lo to hi, inclusive. */
4144 if (lo > hi) break; /* not found */
4145 mid = (lo + hi) / 2;
4146 a_mid_lo = di->cfsi[mid].base;
4147 size = di->cfsi[mid].len;
4148 a_mid_hi = a_mid_lo + size - 1;
4149 vg_assert(a_mid_hi >= a_mid_lo);
4150 if (fde_initloc + fde_arange <= a_mid_lo) {
4151 hi = mid-1; continue;
4153 if (fde_initloc > a_mid_hi) { lo = mid+1; continue; }
4157 /* The range this .debug_frame FDE covers has been already
4158 covered in .eh_frame section. Don't add it from .debug_frame
4164 adi.encoding = the_CIEs[cie].address_encoding;
4165 adi.ehframe_image = frame_image;
4166 adi.ehframe_avma = frame_avma;
4167 adi.text_bias = di->text_debug_bias;
4170 show_CF_instructions( fde_instrs, fde_ilen, &adi,
4171 the_CIEs[cie].code_a_f,
4172 the_CIEs[cie].data_a_f );
4174 initUnwindContext(&ctx);
4175 ctx.code_a_f = the_CIEs[cie].code_a_f;
4176 ctx.data_a_f = the_CIEs[cie].data_a_f;
4177 ctx.initloc = fde_initloc;
4178 ctx.ra_reg = the_CIEs[cie].ra_reg;
4179 ctx.exprs = VG_(newXA)( ML_(dinfo_zalloc), "di.rcid.1",
4182 vg_assert(ctx.exprs);
4184 /* Run the CIE's instructions. Ugly hack: if
4185 --debug-dump=frames is in effect, suppress output for
4186 these instructions since they will already have been shown
4187 at the time the CIE was first encountered. Note, not
4188 thread safe - if this reader is ever made threaded, should
4190 { Bool hack = di->ddump_frames;
4191 di->ddump_frames = False;
4192 initUnwindContext(&restore_ctx);
4193 ok = run_CF_instructions(
4194 di, False, &ctx, the_CIEs[cie].instrs,
4195 the_CIEs[cie].ilen, 0, NULL, &adi
4197 di->ddump_frames = hack;
4199 /* And now run the instructions for the FDE, starting from
4200 the state created by running the CIE preamble
4204 ok = run_CF_instructions(
4205 di, True, &ctx, fde_instrs, fde_ilen, fde_arange,
4208 if (di->ddump_frames)
4212 VG_(deleteXA)( ctx.exprs );
4219 if (!VG_(clo_xml) && VG_(clo_verbosity) > 1)
4220 VG_(message)(Vg_UserMsg,
4221 "Warning: %s in DWARF2 CFI reading\n", how);
4225 #endif // defined(VGO_linux) || defined(VGO_darwin)
4227 /*--------------------------------------------------------------------*/
4229 /*--------------------------------------------------------------------*/