1 /* -*- mode: C; c-basic-offset: 3; -*- */
2
3 /*--------------------------------------------------------------------*/
4 /*--- Read DWARF1/2/3/4 debug info. readdwarf.c ---*/
5 /*--------------------------------------------------------------------*/
6
7 /*
8 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
10
11 Copyright (C) 2000-2013 Julian Seward
12 jseward@acm.org
13
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
28
29 The GNU General Public License is contained in the file COPYING.
30 */
31
32 #if defined(VGO_linux) || defined(VGO_darwin)
33
34 #include "pub_core_basics.h"
35 #include "pub_core_debuginfo.h"
36 #include "pub_core_libcbase.h"
37 #include "pub_core_libcassert.h"
38 #include "pub_core_libcprint.h"
39 #include "pub_core_options.h"
40 #include "pub_core_xarray.h"
41 #include "pub_core_tooliface.h" /* VG_(needs) */
42 #include "priv_misc.h" /* dinfo_zalloc/free/strdup */
43 #include "priv_image.h"
44 #include "priv_d3basics.h"
45 #include "priv_tytypes.h"
46 #include "priv_storage.h"
47 #include "priv_readdwarf.h" /* self */
48
49
50 /*------------------------------------------------------------*/
51 /*--- ---*/
52 /*--- Read line number and CFI info from DWARF1, DWARF2 ---*/
53 /*--- and to some extent DWARF3 sections. ---*/
54 /*--- ---*/
55 /*------------------------------------------------------------*/
56
57 /*------------------------------------------------------------*/
58 /*--- Expanding arrays of words, for holding file name and ---*/
59 /*--- directory name arrays. ---*/
60 /*------------------------------------------------------------*/
61
62 typedef
63 struct {
64 Word* tab;
65 UInt tab_size;
66 UInt tab_used;
67 }
68 WordArray;
69
init_WordArray(WordArray * wa)70 static void init_WordArray ( WordArray* wa )
71 {
72 wa->tab = NULL;
73 wa->tab_size = 0;
74 wa->tab_used = 0;
75 }
76
free_WordArray(WordArray * wa)77 static void free_WordArray ( WordArray* wa )
78 {
79 if (wa->tab) {
80 vg_assert(wa->tab_size > 0);
81 ML_(dinfo_free)(wa->tab);
82 }
83 init_WordArray(wa);
84 }
85
addto_WordArray(WordArray * wa,Word w)86 static void addto_WordArray ( WordArray* wa, Word w )
87 {
88 UInt new_size, i;
89 Word* new_tab;
90
91 if (0) VG_(printf)("<<ADD %p (new sz = %d) >>\n",
92 (HChar*)w, wa->tab_used+1);
93
94 if (wa->tab_used < wa->tab_size) {
95 /* fine */
96 } else {
97 /* expand array */
98 if (0) VG_(printf)("EXPAND ARRAY from %d\n", wa->tab_size);
99 vg_assert(wa->tab_used == wa->tab_size);
100 vg_assert( (wa->tab_size == 0 && wa->tab == NULL)
101 || (wa->tab_size != 0 && wa->tab != NULL) );
102 new_size = wa->tab_size == 0 ? 8 : 2 * wa->tab_size;
103 new_tab = ML_(dinfo_zalloc)("di.aWA.1", new_size * sizeof(Word));
104 vg_assert(new_tab != NULL);
105 for (i = 0; i < wa->tab_used; i++)
106 new_tab[i] = wa->tab[i];
107 wa->tab_size = new_size;
108 if (wa->tab)
109 ML_(dinfo_free)(wa->tab);
110 wa->tab = new_tab;
111 }
112
113 vg_assert(wa->tab_used < wa->tab_size);
114 vg_assert(wa->tab_size > 0);
115 wa->tab[wa->tab_used] = w;
116 wa->tab_used++;
117 }
118
index_WordArray(Bool * inRange,WordArray * wa,Int i)119 static Word index_WordArray ( /*OUT*/Bool* inRange, WordArray* wa, Int i )
120 {
121 vg_assert(inRange);
122 if (i >= 0 && i < wa->tab_used) {
123 *inRange = True;
124 return wa->tab[i];
125 } else {
126 *inRange = False;
127 return 0;
128 }
129 }
130
131
132 /*------------------------------------------------------------*/
133 /*--- Read DWARF2 format line number info. ---*/
134 /*------------------------------------------------------------*/
135
136 /* Structure holding info extracted from the a .debug_line
137 section. */
138 typedef struct
139 {
140 ULong li_length;
141 UShort li_version;
142 ULong li_header_length;
143 UChar li_min_insn_length;
144 UChar li_max_ops_per_insn;
145 UChar li_default_is_stmt;
146 Int li_line_base;
147 UChar li_line_range;
148 UChar li_opcode_base;
149 }
150 DebugLineInfo;
151
152 /* Structure holding additional infos found from a .debug_info
153 * compilation unit block */
154 typedef struct
155 {
156 /* Feel free to add more members here if you need ! */
157 DiCursor compdir; /* Compilation directory - points to .debug_info */
158 DiCursor name; /* Main file name - points to .debug_info */
159 ULong stmt_list; /* Offset in .debug_line */
160 Bool dw64; /* 64-bit Dwarf? */
161 }
162 UnitInfo;
163
164 /* Line number opcodes. */
165 enum dwarf_line_number_ops
166 {
167 DW_LNS_extended_op = 0,
168 DW_LNS_copy = 1,
169 DW_LNS_advance_pc = 2,
170 DW_LNS_advance_line = 3,
171 DW_LNS_set_file = 4,
172 DW_LNS_set_column = 5,
173 DW_LNS_negate_stmt = 6,
174 DW_LNS_set_basic_block = 7,
175 DW_LNS_const_add_pc = 8,
176 DW_LNS_fixed_advance_pc = 9,
177 /* DWARF 3. */
178 DW_LNS_set_prologue_end = 10,
179 DW_LNS_set_epilogue_begin = 11,
180 DW_LNS_set_isa = 12
181 };
182
183 /* Line number extended opcodes. */
184 enum dwarf_line_number_x_ops
185 {
186 DW_LNE_end_sequence = 1,
187 DW_LNE_set_address = 2,
188 DW_LNE_define_file = 3,
189 DW_LNE_set_discriminator = 4
190 };
191
192 typedef struct
193 {
194 /* Information for the last statement boundary.
195 * Needed to calculate statement lengths. */
196 Addr last_address;
197 UInt last_file;
198 UInt last_line;
199
200 Addr address;
201 UInt file;
202 UInt line;
203 UInt column;
204 Int is_stmt;
205 Int basic_block;
206 UChar end_sequence;
207 } LineSMR;
208
209
210 /* FIXME: duplicated in readdwarf3.c */
211 /* Read a 'leb128' and advance *data accordingly. */
step_leb128(DiCursor * data,Int sign)212 static ULong step_leb128 ( DiCursor* data, Int sign )
213 {
214 ULong result = 0;
215 Int shift = 0;
216 UChar byte;
217
218 vg_assert(sign == 0 || sign == 1);
219
220 do {
221 byte = ML_(cur_step_UChar)(data);
222 result |= ((ULong)(byte & 0x7f)) << shift;
223 shift += 7;
224 }
225 while (byte & 0x80);
226
227 if (sign && (shift < 64) && (byte & 0x40))
228 result |= -(1ULL << shift);
229
230 return result;
231 }
232
233 /* FIXME: duplicated in readdwarf3.c */
step_leb128U(DiCursor * data)234 static ULong step_leb128U( DiCursor* data ) {
235 return step_leb128( data, 0 );
236 }
237
238 /* FIXME: duplicated in readdwarf3.c */
step_leb128S(DiCursor * data)239 static Long step_leb128S( DiCursor* data ) {
240 return step_leb128( data, 1 );
241 }
242
243 /* Read what the DWARF3 spec calls an "initial length field". This
244 uses up either 4 or 12 bytes of the input and produces a 32-bit or
245 64-bit number respectively.
246
247 Read 32-bit value from p. If it is 0xFFFFFFFF, instead read a
248 64-bit bit value from p+4. This is used in 64-bit dwarf to encode
249 some table lengths. Advance the cursor (p) accordingly.
250
251 XXX this is a hack: the endianness of the initial length field is
252 specified by the DWARF we're reading. This happens to work only
253 because we don't do cross-arch jitting, hence this code runs on a
254 platform of the same endianness as the DWARF it is reading. Same
255 applies for initial lengths for CIE/FDEs and probably in zillions
256 of other places -- to be precise, exactly the places where
257 binutils/dwarf.c calls byte_get().
258 */
259 static
step_initial_length_field(DiCursor * p_img,Bool * is64)260 ULong step_initial_length_field ( DiCursor* p_img, /*OUT*/Bool* is64 )
261 {
262 UInt w32 = ML_(cur_step_UInt)(p_img);
263 if (w32 == 0xFFFFFFFF) {
264 *is64 = True;
265 return ML_(cur_step_ULong)(p_img);
266 } else {
267 *is64 = False;
268 return (ULong)w32;
269 }
270 }
271
272 static
read_initial_length_field(DiCursor p_img,Bool * is64)273 ULong read_initial_length_field ( DiCursor p_img, /*OUT*/Bool* is64 )
274 {
275 /* Something of a roundabout approach .. the modification to p_img
276 is abandoned. */
277 return step_initial_length_field( &p_img, is64 );
278 }
279
280
281 static LineSMR state_machine_regs;
282
283 static
reset_state_machine(Int is_stmt)284 void reset_state_machine ( Int is_stmt )
285 {
286 if (0) VG_(printf)("smr.a := %p (reset)\n", NULL );
287 state_machine_regs.last_address = 0;
288 state_machine_regs.last_file = 1;
289 state_machine_regs.last_line = 1;
290 state_machine_regs.address = 0;
291 state_machine_regs.file = 1;
292 state_machine_regs.line = 1;
293 state_machine_regs.column = 0;
294 state_machine_regs.is_stmt = is_stmt;
295 state_machine_regs.basic_block = 0;
296 state_machine_regs.end_sequence = 0;
297 }
298
299 /* Look up a directory name, or return NULL if unknown. */
300 static
lookupDir(Int filename_index,WordArray * fnidx2dir,WordArray * dirnames)301 HChar* lookupDir ( Int filename_index,
302 WordArray* fnidx2dir,
303 WordArray* dirnames )
304 {
305 Bool inRange;
306 Word diridx, dirname;
307
308 diridx = index_WordArray( &inRange, fnidx2dir, filename_index );
309 if (!inRange) goto bad;
310
311 dirname = index_WordArray( &inRange, dirnames, (Int)diridx );
312 if (!inRange) goto bad;
313
314 return (HChar*)dirname;
315 bad:
316 return NULL;
317 }
318
319 ////////////////////////////////////////////////////////////////////
320 ////////////////////////////////////////////////////////////////////
321
322 /* Handled an extended line op starting at *data, and advance *data
323 accordingly. */
324 static
process_extended_line_op(struct _DebugInfo * di,WordArray * filenames,WordArray * dirnames,WordArray * fnidx2dir,DiCursor * data,Int is_stmt)325 void process_extended_line_op( struct _DebugInfo* di,
326 WordArray* filenames,
327 WordArray* dirnames,
328 WordArray* fnidx2dir,
329 DiCursor* data, Int is_stmt)
330 {
331 UInt len = step_leb128U(data);
332 if (len == 0) {
333 VG_(message)(Vg_UserMsg,
334 "Warning: DWARF2 reader: "
335 "Badly formed extended line op encountered\n");
336 return;
337 }
338
339 UChar op_code = ML_(cur_step_UChar)(data);
340 if (0) VG_(printf)("dwarf2: ext OPC: %d\n", op_code);
341
342 switch (op_code) {
343 case DW_LNE_end_sequence:
344 if (0) VG_(printf)("1001: si->o %#lx, smr.a %#lx\n",
345 di->text_debug_bias, state_machine_regs.address );
346 /* JRS: added for compliance with spec; is pointless due to
347 reset_state_machine below */
348 state_machine_regs.end_sequence = 1;
349
350 if (state_machine_regs.is_stmt) {
351 if (state_machine_regs.last_address) {
352 Bool inRange = False;
353 const HChar* filename
354 = (HChar*)index_WordArray( &inRange, filenames,
355 state_machine_regs.last_file);
356 if (!inRange || !filename)
357 filename = "???";
358 ML_(addLineInfo) (
359 di,
360 filename,
361 lookupDir( state_machine_regs.last_file,
362 fnidx2dir, dirnames ),
363 di->text_debug_bias + state_machine_regs.last_address,
364 di->text_debug_bias + state_machine_regs.address,
365 state_machine_regs.last_line, 0
366 );
367 }
368 }
369 reset_state_machine (is_stmt);
370 if (di->ddump_line)
371 VG_(printf)(" Extended opcode %d: End of Sequence\n\n",
372 (Int)op_code);
373 break;
374
375 case DW_LNE_set_address: {
376 Addr adr = ML_(cur_step_Addr)(data);
377 state_machine_regs.address = adr;
378 if (di->ddump_line)
379 VG_(printf)(" Extended opcode %d: set Address to 0x%lx\n",
380 (Int)op_code, (Addr)adr);
381 break;
382 }
383
384 case DW_LNE_define_file: {
385 HChar* name = ML_(cur_step_strdup)(data, "di.pelo.1");
386 addto_WordArray( filenames, (Word)ML_(addStr)(di,name,-1) );
387 ML_(dinfo_free)(name);
388 (void)step_leb128U(data); // ignored: dir index
389 (void)step_leb128U(data); // ignored: mod time
390 (void)step_leb128U(data); // ignored: file size
391 if (di->ddump_line)
392 VG_(printf)(" DWARF2-line: set_address\n");
393 break;
394 }
395
396 case DW_LNE_set_discriminator:
397 (void)step_leb128U(data); // ignored: new 'discriminator' value
398 break;
399
400 default:
401 if (di->ddump_line)
402 VG_(printf)("process_extended_line_op:default\n");
403 break;
404 }
405 }
406
407 ////////////////////////////////////////////////////////////////////
408 ////////////////////////////////////////////////////////////////////
409
410 /* read a .debug_line section block for a compilation unit
411 *
412 * Input: - theBlock must point to the start of the block
413 * for the given compilation unit
414 * - ui contains additional info like the compilation dir
415 * for this unit
416 *
417 * Output: - si debug info structures get updated
418 */
419 static
read_dwarf2_lineblock(struct _DebugInfo * di,UnitInfo * ui,DiCursor theBlock,Int noLargerThan)420 void read_dwarf2_lineblock ( struct _DebugInfo* di,
421 UnitInfo* ui,
422 DiCursor theBlock, /* IMAGE */
423 Int noLargerThan )
424 {
425 Int i;
426 DebugLineInfo info;
427 Bool is64;
428 WordArray filenames;
429 WordArray dirnames;
430 WordArray fnidx2dir;
431
432 DiCursor external = theBlock;
433 DiCursor data = theBlock;
434
435 /* filenames is an array of file names harvested from the DWARF2
436 info. Entry [0] is NULL and is never referred to by the state
437 machine.
438
439 Similarly, dirnames is an array of directory names. Entry [0]
440 is also NULL and denotes "we don't know what the path is", since
441 that is different from "the path is the empty string". Unlike
442 the file name table, the state machine does refer to entry [0],
443 which basically means "." ("the current directory of the
444 compilation", whatever that means, according to the DWARF3
445 spec.)
446
447 fnidx2dir is an array of indexes into the dirnames table.
448 (confused yet?) filenames[] and fnidx2dir[] are indexed
449 together. That is, for some index i in the filename table, then
450
451 the filename is filenames[i]
452 the directory is dirnames[ fnidx2dir[i] ] */
453
454 /* Fails due to gcc padding ...
455 vg_assert(sizeof(DWARF2_External_LineInfo)
456 == sizeof(DWARF2_Internal_LineInfo));
457 */
458
459 init_WordArray(&filenames);
460 init_WordArray(&dirnames);
461 init_WordArray(&fnidx2dir);
462
463 /* DWARF2 starts numbering filename entries at 1, so we need to
464 add a dummy zeroth entry to the table. The zeroth dirnames
465 entry denotes 'current directory of compilation' so we might
466 as well make the fnidx2dir zeroth entry denote that.
467 */
468 addto_WordArray( &filenames, (Word)NULL );
469
470 if (ML_(cur_is_valid)(ui->compdir))
471 addto_WordArray( &dirnames,
472 (Word)ML_(addStrFromCursor)(di, ui->compdir) );
473 else
474 addto_WordArray( &dirnames, (Word)ML_(addStr)(di, ".", -1) );
475
476 addto_WordArray( &fnidx2dir, (Word)0 ); /* compilation dir */
477
478 info.li_length = step_initial_length_field( &external, &is64 );
479 if (di->ddump_line)
480 VG_(printf)(" Length: %llu\n",
481 info.li_length);
482
483 /* Check the length of the block. */
484 if (info.li_length > noLargerThan) {
485 ML_(symerr)(di, True,
486 "DWARF line info appears to be corrupt "
487 "- the section is too small");
488 goto out;
489 }
490
491 /* Check its version number. */
492 info.li_version = ML_(cur_step_UShort)(&external);
493 if (di->ddump_line)
494 VG_(printf)(" DWARF Version: %d\n",
495 (Int)info.li_version);
496
497 if (info.li_version != 2 && info.li_version != 3 && info.li_version != 4) {
498 ML_(symerr)(di, True,
499 "Only DWARF version 2, 3 and 4 line info "
500 "is currently supported.");
501 goto out;
502 }
503
504 info.li_header_length = is64 ? ML_(cur_step_ULong)(&external)
505 : (ULong)(ML_(cur_step_UInt)(&external));
506 if (di->ddump_line)
507 VG_(printf)(" Prologue Length: %llu\n",
508 info.li_header_length);
509
510 info.li_min_insn_length = ML_(cur_step_UChar)(&external);
511 if (di->ddump_line)
512 VG_(printf)(" Minimum Instruction Length: %d\n",
513 (Int)info.li_min_insn_length);
514
515 /* We only support machines with one opcode per instruction
516 for now. If we ever want to support VLIW machines there is
517 code to handle multiple opcodes per instruction in the
518 patch attached to BZ#233595.
519 */
520 if (info.li_version >= 4) {
521 info.li_max_ops_per_insn = ML_(cur_step_UChar)(&external);
522 if (info.li_max_ops_per_insn != 1) {
523 ML_(symerr)(di, True,
524 "Invalid Maximum Ops Per Insn in line info.");
525 goto out;
526 }
527 if (di->ddump_line)
528 VG_(printf)(" Maximum Ops Per Insn: %d\n",
529 (Int)info.li_max_ops_per_insn);
530 } else {
531 info.li_max_ops_per_insn = 1;
532 }
533
534 info.li_default_is_stmt = ML_(cur_step_UChar)(&external);
535 if (di->ddump_line)
536 VG_(printf)(" Initial value of 'is_stmt': %d\n",
537 (Int)info.li_default_is_stmt);
538
539 /* Josef Weidendorfer (20021021) writes:
540
541 It seems to me that the Intel Fortran compiler generates bad
542 DWARF2 line info code: It sets "is_stmt" of the state machine in
543 the the line info reader to be always false. Thus, there is
544 never a statement boundary generated and therefore never a
545 instruction range/line number mapping generated for valgrind.
546
547 Please have a look at the DWARF2 specification, Ch. 6.2
548 (x86.ddj.com/ftp/manuals/tools/dwarf.pdf). Perhaps I understand
549 this wrong, but I don't think so.
550
551 I just had a look at the GDB DWARF2 reader... They completely
552 ignore "is_stmt" when recording line info ;-) That's the reason
553 "objdump -S" works on files from the the intel fortran compiler.
554
555 Therefore: */
556 info.li_default_is_stmt = True;
557
558 /* JRS: changed (UInt*) to (UChar*) */
559 info.li_line_base = ML_(cur_step_UChar)(&external);
560 info.li_line_base = (Int)(Char)info.li_line_base;
561 if (di->ddump_line)
562 VG_(printf)(" Line Base: %d\n",
563 info.li_line_base);
564
565 info.li_line_range = ML_(cur_step_UChar)(&external);
566 if (di->ddump_line)
567 VG_(printf)(" Line Range: %d\n",
568 (Int)info.li_line_range);
569
570 info.li_opcode_base = ML_(cur_step_UChar)(&external);
571 if (di->ddump_line)
572 VG_(printf)(" Opcode Base: %d\n\n",
573 info.li_opcode_base);
574
575 if (0) VG_(printf)("dwarf2: line base: %d, range %d, opc base: %d\n",
576 (Int)info.li_line_base,
577 (Int)info.li_line_range,
578 (Int)info.li_opcode_base);
579
580 DiCursor end_of_sequence
581 = ML_(cur_plus)(data, info.li_length + (is64 ? 12 : 4));
582
583 reset_state_machine (info.li_default_is_stmt);
584
585 /* Read the contents of the Opcodes table. */
586 DiCursor standard_opcodes = external;
587 if (di->ddump_line) {
588 VG_(printf)(" Opcodes:\n");
589 for (i = 1; i < (Int)info.li_opcode_base; i++) {
590 VG_(printf)(" Opcode %d has %d args\n",
591 i, (Int)ML_(cur_read_UChar)(
592 ML_(cur_plus)(standard_opcodes,
593 (i-1) * sizeof(UChar)) ));
594 }
595 VG_(printf)("\n");
596 }
597
598 /* Read the contents of the Directory table. */
599 data = ML_(cur_plus)(standard_opcodes, info.li_opcode_base - 1);
600
601 if (di->ddump_line)
602 VG_(printf)(" The Directory Table%s\n",
603 ML_(cur_read_UChar)(data) == 0 ? " is empty." : ":" );
604
605 while (ML_(cur_read_UChar)(data) != 0) {
606
607 # define NBUF 4096
608 static HChar buf[NBUF];
609
610 HChar* data_str = ML_(cur_read_strdup)(data, "di.rd2l.1");
611 if (di->ddump_line)
612 VG_(printf)(" %s\n", data_str);
613
614 /* If data[0] is '/', then 'data' is an absolute path and we
615 don't mess with it. Otherwise, if we can, construct the
616 path 'ui->compdir' ++ "/" ++ 'data'. */
617
618 if (data_str[0] != '/'
619 /* not an absolute path */
620 && ML_(cur_is_valid)(ui->compdir)
621 /* actually got something sensible for compdir */
622 && ML_(cur_strlen)(ui->compdir)
623 + VG_(strlen)(data_str) + 5/*paranoia*/ < NBUF
624 /* it's short enough to concatenate */)
625 {
626 buf[0] = 0;
627 HChar* compdir_str = ML_(cur_read_strdup)(ui->compdir, "di.rd2l.1b");
628 VG_(strcat)(buf, compdir_str);
629 VG_(strcat)(buf, "/");
630 VG_(strcat)(buf, data_str);
631 vg_assert(VG_(strlen)(buf) < NBUF);
632 addto_WordArray( &dirnames, (Word)ML_(addStr)(di,buf,-1) );
633 if (0) VG_(printf)("rel path %s\n", buf);
634 ML_(dinfo_free)(compdir_str);
635 } else {
636 /* just use 'data'. */
637 addto_WordArray( &dirnames, (Word)ML_(addStr)(di,data_str,-1) );
638 if (0) VG_(printf)("abs path %s\n", data_str);
639 }
640
641 data = ML_(cur_plus)(data, VG_(strlen)(data_str) + 1);
642 ML_(dinfo_free)(data_str);
643
644 # undef NBUF
645 }
646
647 if (di->ddump_line)
648 VG_(printf)("\n");
649
650 if (ML_(cur_read_UChar)(data) != 0) {
651 ML_(symerr)(di, True,
652 "can't find NUL at end of DWARF2 directory table");
653 goto out;
654 }
655 data = ML_(cur_plus)(data, 1);
656
657 /* Read the contents of the File Name table. This produces a bunch
658 of file names, and for each, an index to the corresponding
659 directory name entry. */
660 if (di->ddump_line) {
661 VG_(printf)(" The File Name Table:\n");
662 VG_(printf)(" Entry Dir Time Size Name\n");
663 }
664
665 i = 1;
666 while (ML_(cur_read_UChar)(data) != 0) {
667 HChar* name = ML_(cur_step_strdup)(&data, "di.rd2l.2");
668 Int diridx = step_leb128U(&data);
669 Int uu_time = step_leb128U(&data); /* unused */
670 Int uu_size = step_leb128U(&data); /* unused */
671 addto_WordArray( &filenames, (Word)ML_(addStr)(di,name,-1) );
672 addto_WordArray( &fnidx2dir, (Word)diridx );
673 if (0) VG_(printf)("file %s diridx %d\n", name, diridx );
674 if (di->ddump_line)
675 VG_(printf)(" %d\t%d\t%d\t%d\t%s\n",
676 i, diridx, uu_time, uu_size, name);
677 i++;
678 ML_(dinfo_free)(name);
679 }
680
681 if (di->ddump_line)
682 VG_(printf)("\n");
683
684 if (ML_(cur_read_UChar)(data) != 0) {
685 ML_(symerr)(di, True,
686 "can't find NUL at end of DWARF2 file name table");
687 goto out;
688 }
689 data = ML_(cur_plus)(data, 1);
690
691 if (di->ddump_line)
692 VG_(printf)(" Line Number Statements:\n");
693
694 /* Now display the statements. */
695
696 while (ML_(cur_cmpLT)(data, end_of_sequence)) {
697 UChar op_code = ML_(cur_step_UChar)(&data);
698
699 if (0) VG_(printf)("dwarf2: OPC: %d\n", op_code);
700
701 if (op_code >= info.li_opcode_base) {
702 op_code -= info.li_opcode_base;
703 Word adv = (op_code / info.li_line_range)
704 * info.li_min_insn_length;
705 Int advAddr = adv;
706 state_machine_regs.address += adv;
707
708 if (0) VG_(printf)("smr.a += %#lx\n", adv );
709 adv = (op_code % info.li_line_range) + info.li_line_base;
710 if (0) VG_(printf)("1002: di->o %#lx, smr.a %#lx\n",
711 di->text_debug_bias, state_machine_regs.address );
712 state_machine_regs.line += adv;
713
714 if (di->ddump_line)
715 VG_(printf)(" Special opcode %d: advance Address by %d "
716 "to 0x%lx and Line by %d to %d\n",
717 (Int)op_code, advAddr, state_machine_regs.address,
718 (Int)adv, (Int)state_machine_regs.line );
719
720 if (state_machine_regs.is_stmt) {
721 /* only add a statement if there was a previous boundary */
722 if (state_machine_regs.last_address) {
723 Bool inRange = False;
724 const HChar* filename
725 = (HChar*)index_WordArray( &inRange, &filenames,
726 state_machine_regs.last_file);
727 if (!inRange || !filename)
728 filename = "???";
729 ML_(addLineInfo)(
730 di,
731 filename,
732 lookupDir( state_machine_regs.last_file,
733 &fnidx2dir, &dirnames ),
734 di->text_debug_bias + state_machine_regs.last_address,
735 di->text_debug_bias + state_machine_regs.address,
736 state_machine_regs.last_line,
737 0
738 );
739 }
740 state_machine_regs.last_address = state_machine_regs.address;
741 state_machine_regs.last_file = state_machine_regs.file;
742 state_machine_regs.last_line = state_machine_regs.line;
743 }
744 }
745
746 else { /* ! (op_code >= info.li_opcode_base) */
747
748 switch (op_code) {
749 case DW_LNS_extended_op:
750 process_extended_line_op (
751 di, &filenames, &dirnames, &fnidx2dir,
752 &data, info.li_default_is_stmt);
753 break;
754
755 case DW_LNS_copy:
756 if (0) VG_(printf)("1002: di->o %#lx, smr.a %#lx\n",
757 di->text_debug_bias, state_machine_regs.address );
758 if (state_machine_regs.is_stmt) {
759 /* only add a statement if there was a previous boundary */
760 if (state_machine_regs.last_address) {
761 Bool inRange = False;
762 const HChar* filename
763 = (HChar*)index_WordArray( &inRange, &filenames,
764 state_machine_regs.last_file );
765 if (!inRange || !filename)
766 filename = "???";
767 ML_(addLineInfo)(
768 di,
769 filename,
770 lookupDir( state_machine_regs.last_file,
771 &fnidx2dir, &dirnames ),
772 di->text_debug_bias + state_machine_regs.last_address,
773 di->text_debug_bias + state_machine_regs.address,
774 state_machine_regs.last_line,
775 0
776 );
777 }
778 state_machine_regs.last_address = state_machine_regs.address;
779 state_machine_regs.last_file = state_machine_regs.file;
780 state_machine_regs.last_line = state_machine_regs.line;
781 }
782 state_machine_regs.basic_block = 0; /* JRS added */
783 if (di->ddump_line)
784 VG_(printf)(" Copy\n");
785 break;
786
787 case DW_LNS_advance_pc: {
788 Word adv = info.li_min_insn_length * step_leb128U(&data);
789 state_machine_regs.address += adv;
790 if (0) VG_(printf)("smr.a += %#lx\n", adv );
791 if (di->ddump_line)
792 VG_(printf)(" Advance PC by %ld to 0x%lx\n",
793 adv, state_machine_regs.address);
794 break;
795 }
796 case DW_LNS_advance_line: {
797 Word adv = step_leb128S(&data);
798 state_machine_regs.line += adv;
799 if (di->ddump_line)
800 VG_(printf)(" Advance Line by %ld to %d\n",
801 adv, (Int)state_machine_regs.line);
802 break;
803 }
804 case DW_LNS_set_file: {
805 Word adv = step_leb128U(&data);
806 state_machine_regs.file = adv;
807 if (di->ddump_line)
808 VG_(printf)(" Set File Name to entry %ld in the "
809 "File Name Table\n", adv);
810 break;
811 }
812 case DW_LNS_set_column: {
813 Word adv = step_leb128U(&data);
814 state_machine_regs.column = adv;
815 if (di->ddump_line)
816 VG_(printf)(" Set column to %ld\n", adv);
817 break;
818 }
819 case DW_LNS_negate_stmt: {
820 Int adv = state_machine_regs.is_stmt;
821 adv = ! adv;
822 state_machine_regs.is_stmt = adv;
823 if (di->ddump_line)
824 VG_(printf)(" DWARF2-line: negate_stmt\n");
825 break;
826 }
827 case DW_LNS_set_basic_block: {
828 state_machine_regs.basic_block = 1;
829 if (di->ddump_line)
830 VG_(printf)(" DWARF2-line: set_basic_block\n");
831 break;
832 }
833 case DW_LNS_const_add_pc: {
834 Word adv = (((255 - info.li_opcode_base) / info.li_line_range)
835 * info.li_min_insn_length);
836 state_machine_regs.address += adv;
837 if (0) VG_(printf)("smr.a += %#lx\n", adv );
838 if (di->ddump_line)
839 VG_(printf)(" Advance PC by constant %ld to 0x%lx\n",
840 adv, (Addr)state_machine_regs.address);
841 break;
842 }
843 case DW_LNS_fixed_advance_pc: {
844 /* XXX: Need something to get 2 bytes */
845 Word adv = ML_(cur_step_UShort)(&data);
846 state_machine_regs.address += adv;
847 if (0) VG_(printf)("smr.a += %#lx\n", adv );
848 if (di->ddump_line)
849 VG_(printf)(" DWARF2-line: fixed_advance_pc\n");
850 break;
851 }
852 case DW_LNS_set_prologue_end:
853 if (di->ddump_line)
854 VG_(printf)(" DWARF2-line: set_prologue_end\n");
855 break;
856
857 case DW_LNS_set_epilogue_begin:
858 if (di->ddump_line)
859 VG_(printf)(" DWARF2-line: set_epilogue_begin\n");
860 break;
861
862 case DW_LNS_set_isa:
863 (void)step_leb128U(&data);
864 if (di->ddump_line)
865 VG_(printf)(" DWARF2-line: set_isa\n");
866 break;
867
868 default: {
869 Int j;
870 for (j = (Int)ML_(cur_read_UChar)(
871 ML_(cur_plus)(standard_opcodes,
872 (op_code-1) * sizeof(UChar)));
873 j > 0 ; --j) {
874 step_leb128U(&data);
875 }
876 if (di->ddump_line)
877 VG_(printf)(" Unknown opcode %d\n", (Int)op_code);
878 break;
879 }
880 } /* switch (op_code) */
881
882 } /* if (op_code >= info.li_opcode_base) */
883
884 } /* while (data < end_of_sequence) */
885
886 if (di->ddump_line)
887 VG_(printf)("\n");
888
889 out:
890 free_WordArray(&filenames);
891 free_WordArray(&dirnames);
892 free_WordArray(&fnidx2dir);
893 }
894
895 ////////////////////////////////////////////////////////////////////
896 ////////////////////////////////////////////////////////////////////
897
898 /* Return abbrev for given code
899 * Returned cursor points to the tag
900 * */
lookup_abbrev(DiCursor p,ULong acode)901 static DiCursor lookup_abbrev( DiCursor p, ULong acode )
902 {
903 while (1) {
904 ULong code = step_leb128U(&p);
905 if (code == acode)
906 return p;
907 (void)step_leb128U(&p); /* skip tag */
908 p = ML_(cur_plus)(p,1); /* skip has_children flag */
909 ULong name;
910 do {
911 name = step_leb128U(&p); /* name */
912 (void)step_leb128U(&p); /* form */
913 }
914 while (name != 0); /* until name == form == 0 */
915 }
916 }
917
918 /* Read general information for a particular compile unit block in
919 * the .debug_info section. In particular read the name, compdir and
920 * stmt_list needed to parse the line number information.
921 *
922 * Input: - unitblock is the start of a compilation
923 * unit block in .debuginfo section
924 * - debugabbrev is start of .debug_abbrev section
925 * - debugstr is start of .debug_str section
926 * - debugstr_alt_img is start of .debug_str section in alt debug file
927 *
928 * Output: Fill members of ui pertaining to the compilation unit:
929 * - ui->name is the name of the compilation unit
930 * - ui->compdir is the compilation unit directory
931 * - ui->stmt_list is the offset in .debug_line section
932 * for the dbginfos of this compilation unit
933 *
934 * Note : the output strings are not allocated and point
935 * directly to the memory-mapped section.
936 */
937 static
read_unitinfo_dwarf2(UnitInfo * ui,DiCursor unitblock_img,DiCursor debugabbrev_img,DiCursor debugstr_img,DiCursor debugstr_alt_img)938 void read_unitinfo_dwarf2( /*OUT*/UnitInfo* ui,
939 DiCursor unitblock_img,
940 DiCursor debugabbrev_img,
941 DiCursor debugstr_img,
942 DiCursor debugstr_alt_img )
943 {
944 UInt acode, abcode;
945 ULong atoffs, blklen;
946 UShort ver;
947
948 UChar addr_size;
949 DiCursor p = unitblock_img;
950 DiCursor end_img;
951 DiCursor abbrev_img;
952
953 VG_(memset)( ui, 0, sizeof( UnitInfo ) );
954 ui->stmt_list = -1LL;
955
956 /* Read the compilation unit header in .debug_info section - See p 70 */
957
958 /* This block length */
959 blklen = step_initial_length_field( &p, &ui->dw64 );
960
961 /* version should be 2, 3 or 4 */
962 ver = ML_(cur_step_UShort)(&p);
963
964 /* get offset in abbrev */
965 atoffs = ui->dw64 ? ML_(cur_step_ULong)(&p)
966 : (ULong)(ML_(cur_step_UInt)(&p));
967
968 /* Address size */
969 addr_size = ML_(cur_step_UChar)(&p);
970
971 /* End of this block */
972 end_img = ML_(cur_plus)(unitblock_img, blklen + (ui->dw64 ? 12 : 4));
973
974 /* Abbreviation data for this block */
975 abbrev_img = ML_(cur_plus)(debugabbrev_img, atoffs);
976
977 /* Read the compilation unit entry - this is always the first DIE.
978 * See DWARF4 para 7.5. */
979 if (ML_(cur_cmpLT)(p, end_img)) {
980 UInt tag;
981
982 acode = step_leb128U( &p ); /* abbreviation code */
983
984 /* Read abbreviation header */
985 abcode = step_leb128U( &abbrev_img ); /* abbreviation code */
986 if ( acode != abcode ) {
987 /* This isn't illegal, but somewhat unlikely. Normally the
988 * first abbrev describes the first DIE, the compile_unit.
989 * But maybe this abbrevation data is shared with another
990 * or it is a NULL entry used for padding. See para 7.5.3. */
991 abbrev_img = lookup_abbrev( ML_(cur_plus)(debugabbrev_img, atoffs),
992 acode );
993 }
994
995 tag = step_leb128U( &abbrev_img );
996
997 if ( tag != 0x0011 /*TAG_compile_unit*/ )
998 return; /* Not a compile unit (might be partial) or broken DWARF. */
999
1000 /* DW_CHILDREN_yes or DW_CHILDREN_no */
1001 abbrev_img = ML_(cur_plus)(abbrev_img, 1);
1002
1003 /* And loop on entries */
1004 for ( ; ; ) {
1005 /* Read entry definition */
1006 ULong cval = -1LL; /* Constant value read */
1007 DiCursor sval = DiCursor_INVALID; /* String value read */
1008 UInt name = step_leb128U( &abbrev_img );
1009 UInt form = step_leb128U( &abbrev_img );
1010 if (name == 0)
1011 break;
1012
1013 /* Read data */
1014 /* Attributes encoding explained p 71 */
1015 if ( form == 0x16 /* FORM_indirect */ )
1016 form = step_leb128U( &p );
1017 /* Decode form. For most kinds, Just skip the amount of data since
1018 we don't use it for now */
1019 /* JRS 9 Feb 06: This now handles 64-bit DWARF too. In
1020 64-bit DWARF, lineptr (and loclistptr,macptr,rangelistptr
1021 classes) use FORM_data8, not FORM_data4. Also,
1022 FORM_ref_addr and FORM_strp are 64-bit values, not 32-bit
1023 values. */
1024 /* TJH 27 Apr 10: in DWARF 4 lineptr (and loclistptr,macptr,
1025 rangelistptr classes) use FORM_sec_offset which is 64 bits
1026 in 64 bit DWARF and 32 bits in 32 bit DWARF. */
1027 /* JRS 20 Apr 11: LLVM-2.9 encodes DW_AT_stmt_list using
1028 FORM_addr rather than the FORM_data4 that GCC uses. Hence
1029 handle FORM_addr too. */
1030 switch( form ) {
1031 /* Those cases extract the data properly */
1032 case 0x05: /* FORM_data2 */
1033 cval = ML_(cur_step_UShort)(&p);
1034 break;
1035 case 0x06: /* FORM_data4 */
1036 cval = ML_(cur_step_UInt)(&p);
1037 break;
1038 case 0x0e: /* FORM_strp */ /* pointer in .debug_str */
1039 /* 2006-01-01: only generate a value if a debug_str
1040 section was found) */
1041 if (ML_(cur_is_valid)(debugstr_img) && !ui->dw64)
1042 sval = ML_(cur_plus)(debugstr_img, ML_(cur_read_UInt)(p));
1043 if (ML_(cur_is_valid)(debugstr_img) && ui->dw64)
1044 sval = ML_(cur_plus)(debugstr_img, ML_(cur_read_ULong)(p));
1045 p = ML_(cur_plus)(p, ui->dw64 ? 8 : 4);
1046 break;
1047 case 0x08: /* FORM_string */
1048 sval = p;
1049 p = ML_(cur_plus)(p, ML_(cur_strlen)(p) + 1);
1050 break;
1051 case 0x0b: /* FORM_data1 */
1052 cval = ML_(cur_step_UChar)(&p);
1053 break;
1054 case 0x17: /* FORM_sec_offset */
1055 if (ui->dw64) {
1056 cval = ML_(cur_step_ULong)(&p);
1057 } else {
1058 cval = ML_(cur_step_UInt)(&p);
1059 };
1060 break;
1061 case 0x07: /* FORM_data8 */
1062 if (ui->dw64) cval = ML_(cur_read_ULong)(p);
1063 p = ML_(cur_plus)(p, 8);
1064 /* perhaps should assign unconditionally to cval? */
1065 break;
1066 /* TODO : Following ones just skip data - implement if you need */
1067 case 0x01: /* FORM_addr */
1068 p = ML_(cur_plus)(p, addr_size);
1069 break;
1070 case 0x03: /* FORM_block2 */
1071 p = ML_(cur_plus)(p, ML_(cur_read_UShort)(p) + 2);
1072 break;
1073 case 0x04: /* FORM_block4 */
1074 p = ML_(cur_plus)(p, ML_(cur_read_UInt)(p) + 4);
1075 break;
1076 case 0x09: /* FORM_block */ /* fallthrough */
1077 case 0x18: { /* FORM_exprloc */
1078 ULong block_len = step_leb128U(&p);
1079 p = ML_(cur_plus)(p, block_len);
1080 break;
1081 }
1082 case 0x0a: /* FORM_block1 */
1083 p = ML_(cur_plus)(p, ML_(cur_read_UChar)(p) + 1);
1084 break;
1085 case 0x0c: /* FORM_flag */
1086 p = ML_(cur_plus)(p, 1);
1087 break;
1088 case 0x0d: /* FORM_sdata */
1089 (void)step_leb128S(&p);
1090 break;
1091 case 0x0f: /* FORM_udata */
1092 (void)step_leb128U(&p);
1093 break;
1094 case 0x10: /* FORM_ref_addr */
1095 p = ML_(cur_plus)(p, (ver == 2) ? addr_size
1096 : (ui->dw64 ? 8 : 4));
1097 break;
1098 case 0x11: /* FORM_ref1 */
1099 p = ML_(cur_plus)(p, 1);
1100 break;
1101 case 0x12: /* FORM_ref2 */
1102 p = ML_(cur_plus)(p, 2);
1103 break;
1104 case 0x13: /* FORM_ref4 */
1105 p = ML_(cur_plus)(p, 4);
1106 break;
1107 case 0x14: /* FORM_ref8 */
1108 p = ML_(cur_plus)(p, 8);
1109 break;
1110 case 0x15: /* FORM_ref_udata */
1111 (void)step_leb128U(&p);
1112 break;
1113 case 0x19: /* FORM_flag_present */
1114 break;
1115 case 0x20: /* FORM_ref_sig8 */
1116 p = ML_(cur_plus)(p, 8);
1117 break;
1118 case 0x1f20: /* FORM_GNU_ref_alt */
1119 p = ML_(cur_plus)(p, ui->dw64 ? 8 : 4);
1120 break;
1121 case 0x1f21: /* FORM_GNU_strp_alt */
1122 if (ML_(cur_is_valid)(debugstr_alt_img) && !ui->dw64)
1123 sval = ML_(cur_plus)(debugstr_alt_img,
1124 ML_(cur_read_UInt)(p));
1125 if (ML_(cur_is_valid)(debugstr_alt_img) && ui->dw64)
1126 sval = ML_(cur_plus)(debugstr_alt_img,
1127 ML_(cur_read_ULong)(p));
1128 p = ML_(cur_plus)(p, ui->dw64 ? 8 : 4);
1129 break;
1130
1131 default:
1132 VG_(printf)( "### unhandled dwarf2 abbrev form code 0x%x\n",
1133 form );
1134 break;
1135 }
1136
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 */
1142 }
1143 }
1144 } /* Just read the first DIE, if that wasn't the compile_unit then
1145 * this might have been a partial unit or broken DWARF info.
1146 * That's enough info for us, and we are not gdb ! */
1147 }
1148
1149
1150 ////////////////////////////////////////////////////////////////////
1151 ////////////////////////////////////////////////////////////////////
1152
1153 /* Collect the debug info from DWARF3 debugging sections
1154 * of a given module.
1155 *
1156 * Inputs: given .debug_xxx sections
1157 * Output: update di to contain all the DWARF3 debug infos
1158 */
ML_(read_debuginfo_dwarf3)1159 void ML_(read_debuginfo_dwarf3)
1160 ( struct _DebugInfo* di,
1161 DiSlice escn_debug_info, /* .debug_info */
1162 DiSlice escn_debug_types, /* .debug_types */
1163 DiSlice escn_debug_abbv, /* .debug_abbrev */
1164 DiSlice escn_debug_line, /* .debug_line */
1165 DiSlice escn_debug_str, /* .debug_str */
1166 DiSlice escn_debug_str_alt ) /* .debug_str */
1167 {
1168 UnitInfo ui;
1169 UShort ver;
1170 ULong blklen;
1171 Bool blklen_is_64;
1172
1173 /* Make sure we at least have a header for the first block */
1174 if (escn_debug_info.szB < 4) {
1175 ML_(symerr)( di, True,
1176 "Last block truncated in .debug_info; ignoring" );
1177 return;
1178 }
1179
1180 DiCursor block_img = DiCursor_INVALID;
1181 DiCursor end1_img = ML_(cur_plus)( ML_(cur_from_sli)(escn_debug_info),
1182 escn_debug_info.szB );
1183 Int blklen_len = 0;
1184
1185 /* Iterate on all the blocks we find in .debug_info */
1186 for ( block_img = ML_(cur_from_sli)(escn_debug_info);
1187 ML_(cur_cmpLT)(block_img, ML_(cur_plus)(end1_img, -(DiOffT)4));
1188 block_img = ML_(cur_plus)(block_img, blklen + blklen_len) ) {
1189
1190 /* Read the compilation unit header in .debug_info section - See
1191 p 70 */
1192 /* This block length */
1193 blklen = read_initial_length_field( block_img, &blklen_is_64 );
1194 blklen_len = blklen_is_64 ? 12 : 4;
1195
1196 if (ML_(cur_cmpGT)( ML_(cur_plus)(block_img, blklen + blklen_len),
1197 end1_img )) {
1198 ML_(symerr)( di, True,
1199 "Last block truncated in .debug_info; ignoring" );
1200 return;
1201 }
1202
1203 /* version should be 2 */
1204 ver = ML_(cur_read_UShort)( ML_(cur_plus)(block_img, blklen_len) );
1205 if ( ver != 2 && ver != 3 && ver != 4 ) {
1206 ML_(symerr)( di, True,
1207 "Ignoring non-Dwarf2/3/4 block in .debug_info" );
1208 continue;
1209 }
1210
1211 /* Fill ui with offset in .debug_line and compdir */
1212 if (0)
1213 VG_(printf)(
1214 "Reading UnitInfo at 0x%llx.....\n",
1215 (ULong)ML_(cur_minus)( block_img,
1216 ML_(cur_from_sli)(escn_debug_info)) );
1217 read_unitinfo_dwarf2( &ui, block_img,
1218 ML_(cur_from_sli)(escn_debug_abbv),
1219 ML_(cur_from_sli)(escn_debug_str),
1220 ML_(cur_from_sli)(escn_debug_str_alt) );
1221 if (0) {
1222 HChar* str_name = ML_(cur_read_strdup)(ui.name, "di.rdd3.1");
1223 HChar* str_compdir = ML_(cur_read_strdup)(ui.compdir, "di.rdd3.2");
1224 VG_(printf)( " => LINES=0x%llx NAME=%s DIR=%s\n",
1225 ui.stmt_list, str_name, str_compdir );
1226 ML_(dinfo_free)(str_name);
1227 ML_(dinfo_free)(str_compdir);
1228 }
1229
1230 /* Ignore blocks with no .debug_line associated block */
1231 if ( ui.stmt_list == -1LL )
1232 continue;
1233
1234 if (0) {
1235 HChar* str_name = ML_(cur_read_strdup)(ui.name, "di.rdd3.3");
1236 VG_(printf)("debug_line_sz %lld, ui.stmt_list %lld %s\n",
1237 escn_debug_line.szB, ui.stmt_list, str_name );
1238 ML_(dinfo_free)(str_name);
1239 }
1240
1241 /* Read the .debug_line block for this compile unit */
1242 read_dwarf2_lineblock(
1243 di, &ui,
1244 ML_(cur_plus)(ML_(cur_from_sli)(escn_debug_line), ui.stmt_list),
1245 escn_debug_line.szB - ui.stmt_list
1246 );
1247 }
1248 }
1249
1250
1251 ////////////////////////////////////////////////////////////////////
1252 ////////////////////////////////////////////////////////////////////
1253
1254 /*------------------------------------------------------------*/
1255 /*--- Read DWARF1 format line number info. ---*/
1256 /*------------------------------------------------------------*/
1257
1258 /* DWARF1 appears to be redundant, but nevertheless the Lahey Fortran
1259 compiler generates it.
1260 */
1261
1262 /* The following three enums (dwarf_tag, dwarf_form, dwarf_attribute)
1263 are taken from the file include/elf/dwarf.h in the GNU gdb-6.0
1264 sources, which are Copyright 1992, 1993, 1995, 1999 Free Software
1265 Foundation, Inc and naturally licensed under the GNU General Public
1266 License version 2 or later.
1267 */
1268
1269 /* Tag names and codes. */
1270
1271 enum dwarf_tag {
1272 TAG_padding = 0x0000,
1273 TAG_array_type = 0x0001,
1274 TAG_class_type = 0x0002,
1275 TAG_entry_point = 0x0003,
1276 TAG_enumeration_type = 0x0004,
1277 TAG_formal_parameter = 0x0005,
1278 TAG_global_subroutine = 0x0006,
1279 TAG_global_variable = 0x0007,
1280 /* 0x0008 -- reserved */
1281 /* 0x0009 -- reserved */
1282 TAG_label = 0x000a,
1283 TAG_lexical_block = 0x000b,
1284 TAG_local_variable = 0x000c,
1285 TAG_member = 0x000d,
1286 /* 0x000e -- reserved */
1287 TAG_pointer_type = 0x000f,
1288 TAG_reference_type = 0x0010,
1289 TAG_compile_unit = 0x0011,
1290 TAG_string_type = 0x0012,
1291 TAG_structure_type = 0x0013,
1292 TAG_subroutine = 0x0014,
1293 TAG_subroutine_type = 0x0015,
1294 TAG_typedef = 0x0016,
1295 TAG_union_type = 0x0017,
1296 TAG_unspecified_parameters = 0x0018,
1297 TAG_variant = 0x0019,
1298 TAG_common_block = 0x001a,
1299 TAG_common_inclusion = 0x001b,
1300 TAG_inheritance = 0x001c,
1301 TAG_inlined_subroutine = 0x001d,
1302 TAG_module = 0x001e,
1303 TAG_ptr_to_member_type = 0x001f,
1304 TAG_set_type = 0x0020,
1305 TAG_subrange_type = 0x0021,
1306 TAG_with_stmt = 0x0022,
1307
1308 /* GNU extensions */
1309
1310 TAG_format_label = 0x8000, /* for FORTRAN 77 and Fortran 90 */
1311 TAG_namelist = 0x8001, /* For Fortran 90 */
1312 TAG_function_template = 0x8002, /* for C++ */
1313 TAG_class_template = 0x8003 /* for C++ */
1314 };
1315
1316 /* Form names and codes. */
1317
1318 enum dwarf_form {
1319 FORM_ADDR = 0x1,
1320 FORM_REF = 0x2,
1321 FORM_BLOCK2 = 0x3,
1322 FORM_BLOCK4 = 0x4,
1323 FORM_DATA2 = 0x5,
1324 FORM_DATA4 = 0x6,
1325 FORM_DATA8 = 0x7,
1326 FORM_STRING = 0x8
1327 };
1328
1329 /* Attribute names and codes. */
1330
1331 enum dwarf_attribute {
1332 AT_sibling = (0x0010|FORM_REF),
1333 AT_location = (0x0020|FORM_BLOCK2),
1334 AT_name = (0x0030|FORM_STRING),
1335 AT_fund_type = (0x0050|FORM_DATA2),
1336 AT_mod_fund_type = (0x0060|FORM_BLOCK2),
1337 AT_user_def_type = (0x0070|FORM_REF),
1338 AT_mod_u_d_type = (0x0080|FORM_BLOCK2),
1339 AT_ordering = (0x0090|FORM_DATA2),
1340 AT_subscr_data = (0x00a0|FORM_BLOCK2),
1341 AT_byte_size = (0x00b0|FORM_DATA4),
1342 AT_bit_offset = (0x00c0|FORM_DATA2),
1343 AT_bit_size = (0x00d0|FORM_DATA4),
1344 /* (0x00e0|FORM_xxxx) -- reserved */
1345 AT_element_list = (0x00f0|FORM_BLOCK4),
1346 AT_stmt_list = (0x0100|FORM_DATA4),
1347 AT_low_pc = (0x0110|FORM_ADDR),
1348 AT_high_pc = (0x0120|FORM_ADDR),
1349 AT_language = (0x0130|FORM_DATA4),
1350 AT_member = (0x0140|FORM_REF),
1351 AT_discr = (0x0150|FORM_REF),
1352 AT_discr_value = (0x0160|FORM_BLOCK2),
1353 /* (0x0170|FORM_xxxx) -- reserved */
1354 /* (0x0180|FORM_xxxx) -- reserved */
1355 AT_string_length = (0x0190|FORM_BLOCK2),
1356 AT_common_reference = (0x01a0|FORM_REF),
1357 AT_comp_dir = (0x01b0|FORM_STRING),
1358 AT_const_value_string = (0x01c0|FORM_STRING),
1359 AT_const_value_data2 = (0x01c0|FORM_DATA2),
1360 AT_const_value_data4 = (0x01c0|FORM_DATA4),
1361 AT_const_value_data8 = (0x01c0|FORM_DATA8),
1362 AT_const_value_block2 = (0x01c0|FORM_BLOCK2),
1363 AT_const_value_block4 = (0x01c0|FORM_BLOCK4),
1364 AT_containing_type = (0x01d0|FORM_REF),
1365 AT_default_value_addr = (0x01e0|FORM_ADDR),
1366 AT_default_value_data2 = (0x01e0|FORM_DATA2),
1367 AT_default_value_data4 = (0x01e0|FORM_DATA4),
1368 AT_default_value_data8 = (0x01e0|FORM_DATA8),
1369 AT_default_value_string = (0x01e0|FORM_STRING),
1370 AT_friends = (0x01f0|FORM_BLOCK2),
1371 AT_inline = (0x0200|FORM_STRING),
1372 AT_is_optional = (0x0210|FORM_STRING),
1373 AT_lower_bound_ref = (0x0220|FORM_REF),
1374 AT_lower_bound_data2 = (0x0220|FORM_DATA2),
1375 AT_lower_bound_data4 = (0x0220|FORM_DATA4),
1376 AT_lower_bound_data8 = (0x0220|FORM_DATA8),
1377 AT_private = (0x0240|FORM_STRING),
1378 AT_producer = (0x0250|FORM_STRING),
1379 AT_program = (0x0230|FORM_STRING),
1380 AT_protected = (0x0260|FORM_STRING),
1381 AT_prototyped = (0x0270|FORM_STRING),
1382 AT_public = (0x0280|FORM_STRING),
1383 AT_pure_virtual = (0x0290|FORM_STRING),
1384 AT_return_addr = (0x02a0|FORM_BLOCK2),
1385 AT_abstract_origin = (0x02b0|FORM_REF),
1386 AT_start_scope = (0x02c0|FORM_DATA4),
1387 AT_stride_size = (0x02e0|FORM_DATA4),
1388 AT_upper_bound_ref = (0x02f0|FORM_REF),
1389 AT_upper_bound_data2 = (0x02f0|FORM_DATA2),
1390 AT_upper_bound_data4 = (0x02f0|FORM_DATA4),
1391 AT_upper_bound_data8 = (0x02f0|FORM_DATA8),
1392 AT_virtual = (0x0300|FORM_STRING),
1393
1394 /* GNU extensions. */
1395
1396 AT_sf_names = (0x8000|FORM_DATA4),
1397 AT_src_info = (0x8010|FORM_DATA4),
1398 AT_mac_info = (0x8020|FORM_DATA4),
1399 AT_src_coords = (0x8030|FORM_DATA4),
1400 AT_body_begin = (0x8040|FORM_ADDR),
1401 AT_body_end = (0x8050|FORM_ADDR)
1402 };
1403
1404 /* end of enums taken from gdb-6.0 sources */
1405 #if 0
1406 void ML_(read_debuginfo_dwarf1) (
1407 struct _DebugInfo* di,
1408 UChar* dwarf1d, Int dwarf1d_sz,
1409 UChar* dwarf1l, Int dwarf1l_sz )
1410 {
1411 UInt stmt_list;
1412 Bool stmt_list_found;
1413 Int die_offset, die_szb, at_offset;
1414 UShort die_kind, at_kind;
1415 UChar* at_base;
1416 HChar* src_filename;
1417
1418 if (0)
1419 VG_(printf)("read_debuginfo_dwarf1 ( %p, %d, %p, %d )\n",
1420 dwarf1d, dwarf1d_sz, dwarf1l, dwarf1l_sz );
1421
1422 /* This loop scans the DIEs. */
1423 die_offset = 0;
1424 while (True) {
1425 if (die_offset >= dwarf1d_sz) break;
1426
1427 die_szb = ML_(read_Int)(dwarf1d + die_offset);
1428 die_kind = ML_(read_UShort)(dwarf1d + die_offset + 4);
1429
1430 /* We're only interested in compile_unit DIEs; ignore others. */
1431 if (die_kind != TAG_compile_unit) {
1432 die_offset += die_szb;
1433 continue;
1434 }
1435
1436 if (0)
1437 VG_(printf)("compile-unit DIE: offset %d, tag 0x%x, size %d\n",
1438 die_offset, (Int)die_kind, die_szb );
1439
1440 /* We've got a compile_unit DIE starting at (dwarf1d +
1441 die_offset+6). Try and find the AT_name and AT_stmt_list
1442 attributes. Then, finally, we can read the line number info
1443 for this source file. */
1444
1445 /* The next 3 are set as we find the relevant attrs. */
1446 src_filename = NULL;
1447 stmt_list_found = False;
1448 stmt_list = 0;
1449
1450 /* This loop scans the Attrs inside compile_unit DIEs. */
1451 at_base = dwarf1d + die_offset + 6;
1452 at_offset = 0;
1453 while (True) {
1454 if (at_offset >= die_szb-6) break;
1455
1456 at_kind = ML_(read_UShort)(at_base + at_offset);
1457 if (0) VG_(printf)("atoffset %d, attag 0x%x\n",
1458 at_offset, (Int)at_kind );
1459 at_offset += 2; /* step over the attribute itself */
1460 /* We have to examine the attribute to figure out its
1461 length. */
1462 switch (at_kind) {
1463 case AT_stmt_list:
1464 case AT_language:
1465 case AT_sibling:
1466 if (at_kind == AT_stmt_list) {
1467 stmt_list_found = True;
1468 stmt_list = ML_(read_Int)(at_base+at_offset);
1469 }
1470 at_offset += 4; break;
1471 case AT_high_pc:
1472 case AT_low_pc:
1473 at_offset += sizeof(void*); break;
1474 case AT_name:
1475 case AT_producer:
1476 case AT_comp_dir:
1477 /* Zero terminated string, step over it. */
1478 if (at_kind == AT_name)
1479 src_filename = (HChar *)(at_base + at_offset);
1480 while (at_offset < die_szb-6 && at_base[at_offset] != 0)
1481 at_offset++;
1482 at_offset++;
1483 break;
1484 default:
1485 VG_(printf)("Unhandled DWARF-1 attribute 0x%x\n",
1486 (Int)at_kind );
1487 VG_(core_panic)("Unhandled DWARF-1 attribute");
1488 } /* switch (at_kind) */
1489 } /* looping over attributes */
1490
1491 /* So, did we find the required stuff for a line number table in
1492 this DIE? If yes, read it. */
1493 if (stmt_list_found /* there is a line number table */
1494 && src_filename != NULL /* we know the source filename */
1495 ) {
1496 /* Table starts:
1497 Length:
1498 4 bytes, includes the entire table
1499 Base address:
1500 unclear (4? 8?), assuming native pointer size here.
1501 Then a sequence of triples
1502 (source line number -- 32 bits
1503 source line column -- 16 bits
1504 address delta -- 32 bits)
1505 */
1506 Addr base;
1507 Int len;
1508 HChar* curr_filenm;
1509 UChar* ptr;
1510 UInt prev_line, prev_delta;
1511
1512 curr_filenm = ML_(addStr) ( di, src_filename, -1 );
1513 prev_line = prev_delta = 0;
1514
1515 ptr = dwarf1l + stmt_list;
1516 len = ML_(read_Int)(ptr); ptr += sizeof(Int);
1517 base = ML_(read_Addr)(ptr); ptr += sizeof(void*);
1518 len -= (sizeof(Int) + sizeof(void*));
1519 while (len > 0) {
1520 UInt line;
1521 UShort col;
1522 UInt delta;
1523 line = ML_(read_UInt)(ptr); ptr += sizeof(UInt);
1524 col = ML_(read_UShort)(ptr); ptr += sizeof(UShort);
1525 delta = ML_(read_UInt)(ptr); ptr += sizeof(UInt);
1526 if (0) VG_(printf)("line %d, col %d, delta %d\n",
1527 line, (Int)col, delta );
1528 len -= (sizeof(UInt) + sizeof(UShort) + sizeof(UInt));
1529
1530 if (delta > 0 && prev_line > 0) {
1531 if (0) VG_(printf) (" %d %d-%d\n",
1532 prev_line, prev_delta, delta-1);
1533 ML_(addLineInfo) ( di, curr_filenm, NULL,
1534 base + prev_delta, base + delta,
1535 prev_line, 0 );
1536 }
1537 prev_line = line;
1538 prev_delta = delta;
1539 }
1540 }
1541
1542 /* Move on the the next DIE. */
1543 die_offset += die_szb;
1544
1545 } /* Looping over DIEs */
1546
1547 }
1548 #endif
1549
1550 /*------------------------------------------------------------*/
1551 /*--- Read call-frame info from an .eh_frame section ---*/
1552 /*------------------------------------------------------------*/
1553
1554 /* Sources of info:
1555
1556 The DWARF3 spec, available from http://www.dwarfstd.org/Download.php
1557
1558 This describes how to read CFA data from .debug_frame sections.
1559 So as to maximise everybody's annoyance and confusion, .eh_frame
1560 sections are almost the same as .debug_frame sections, but differ
1561 in a few subtle and ill documented but important aspects.
1562
1563 Generic ELF Specification, sections 7.5 (DWARF Extensions) and 7.6
1564 (Exception Frames), available from
1565
1566 http://www.linux-foundation.org/spec/book/ELF-generic/ELF-generic.html
1567
1568 This really does describe .eh_frame, at least the aspects that
1569 differ from standard DWARF3. It's better than guessing, and
1570 (marginally) more fun than reading the gdb source code.
1571 */
1572
1573 /* Useful info ..
1574
1575 In general:
1576 gdb-6.3/gdb/dwarf2-frame.c
1577
1578 gdb-6.3/gdb/i386-tdep.c:
1579
1580 DWARF2/GCC uses the stack address *before* the function call as a
1581 frame's CFA. [jrs: I presume this means %esp before the call as
1582 the CFA].
1583
1584 JRS: on amd64, the dwarf register numbering is, as per
1585 gdb-6.3/gdb/amd64-tdep.c and also amd64-abi-0.98.pdf:
1586
1587 0 1 2 3 4 5 6 7
1588 RAX RDX RCX RBX RSI RDI RBP RSP
1589
1590 8 ... 15
1591 R8 ... R15
1592
1593 16 is the return address (RIP)
1594 "The table defines Return Address to have a register number,
1595 even though the address is stored in 0(%rsp) and not in a
1596 physical register."
1597
1598 17 ... 24
1599 XMM0 ... XMM7
1600
1601 25 ... 32
1602 XMM8 ... XMM15
1603
1604 33 ... 40
1605 ST0 ... ST7
1606
1607 41 ... 48
1608 MM0 ... MM7
1609
1610 49 RFLAGS
1611 50,51,52,53,54,55 ES,CS,SS,DS,FS,GS
1612 58 FS.BASE (what's that?)
1613 59 GS.BASE (what's that?)
1614 62 TR (task register)
1615 63 LDTR (LDT register)
1616 64 MXCSR
1617 65 FCW (x87 control word)
1618 66 FSW (x86 status word)
1619
1620 On x86 I cannot find any documentation. It _appears_ to be the
1621 actual instruction encoding, viz:
1622
1623 0 1 2 3 4 5 6 7
1624 EAX ECX EDX EBX ESP EBP ESI EDI
1625
1626 8 is the return address (EIP) */
1627
1628
1629 /* Comments re DW_CFA_set_loc, 16 Nov 06.
1630
1631 JRS:
1632 Someone recently sent me a libcrypto.so.0.9.8 as distributed with
1633 Ubuntu of some flavour, compiled with gcc 4.1.2 on amd64. It
1634 causes V's CF reader to complain a lot:
1635
1636 >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1637 >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1638 >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1639 >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1640 >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:48
1641 >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1642
1643 After chasing this around a bit it seems that the CF bytecode
1644 parser lost sync at a DW_CFA_set_loc, which has a single argument
1645 denoting an address.
1646
1647 As it stands that address is extracted by read_Addr(). On amd64
1648 that just fetches 8 bytes regardless of anything else.
1649
1650 read_encoded_Addr() is more sophisticated. This appears to take
1651 into account some kind of encoding flag. When I replace the uses
1652 of read_Addr by read_encoded_Addr for DW_CFA_set_loc, the
1653 complaints go away, there is no loss of sync, and the parsed CF
1654 instructions are the same as shown by readelf --debug-dump=frames.
1655
1656 So it seems a plausible fix. The problem is I looked in the DWARF3
1657 spec and completely failed to figure out whether or not the arg to
1658 DW_CFA_set_loc is supposed to be encoded in a way suitable for
1659 read_encoded_Addr, nor for that matter any description of what it
1660 is that read_encoded_Addr is really decoding.
1661
1662 TomH:
1663 The problem is that the encoding is not standard - the eh_frame
1664 section uses the same encoding as the dwarf_frame section except
1665 for a few small changes, and this is one of them. So this is not
1666 something the DWARF standard covers.
1667
1668 There is an augmentation string to indicate what is going on though
1669 so that programs can recognise it.
1670
1671 What we are doing seems to match what gdb 6.5 and libdwarf 20060614
1672 do though. I'm not sure about readelf though.
1673
1674 (later): Well dwarfdump barfs on it:
1675
1676 dwarfdump ERROR: dwarf_get_fde_info_for_reg:
1677 DW_DLE_DF_FRAME_DECODING_ERROR(193) (193)
1678
1679 I've looked at binutils as well now, and the code in readelf agrees
1680 with your patch - ie it treats set_loc as having an encoded address
1681 if there is a zR augmentation indicating an encoding.
1682
1683 Quite why gdb and libdwarf don't understand this is an interesting
1684 question...
1685
1686 Final outcome: all uses of read_Addr were replaced by
1687 read_encoded_Addr. A new type AddressDecodingInfo was added to
1688 make it relatively clean to plumb through the extra info needed by
1689 read_encoded_Addr.
1690 */
1691
1692 /* More badness re address encoding, 12 Jan 07.
1693
1694 Most gcc provided CIEs have a "zR" augmentation, which means they
1695 supply their own address encoding, and that works fine. However,
1696 some icc9 supplied CIEs have no augmentation, which means they use
1697 the default_Addr_encoding(). That says to use a machine-word sized
1698 value, literally unmodified.
1699
1700 Since .so's are, in general, relocated when loaded, having absolute
1701 addresses in the CFI data makes no sense when read_encoded_Addr is
1702 used to find the initial location for a FDE. The resulting saga:
1703
1704 TomH:
1705 > I'm chasing a stack backtrace failure for an amd64 .so which was
1706 > created I believe by icc 9.1. After a while I wound up looking at
1707 > this: (readdwarf.c)
1708 >
1709 > 5083 tom static UChar default_Addr_encoding ( void )
1710 > 3584 tom {
1711 > 3584 tom switch (sizeof(Addr)) {
1712 > 3584 tom case 4: return DW_EH_PE_udata4;
1713 > 3584 tom case 8: return DW_EH_PE_udata8;
1714 > 3584 tom default: vg_assert(0);
1715 > 3584 tom }
1716 > 3584 tom }
1717 >
1718 > If a CIE does not have an "augmentation string" (typically "zR") then
1719 > addresses are decoded as described by default_Addr_encoding. If there
1720 > is an 'R' in the augmentation string then the encoding to use
1721 > is specified by the CIE itself, which works fine with GCC compiled code
1722 > since that always appears to specify zR.
1723
1724 Correct.
1725
1726 > Problem is this .so has no augmentation string and so uses the
1727 > default encoding, viz DW_EH_PE_udata8. That appears to mean
1728 > "read a 64 bit number" and use that as-is (for the starting value
1729 > of the program counter when running the CFA program).
1730
1731 Strictly speaking the default is DW_EH_PE_absptr, but that amounts
1732 to either udata4 or udata8 depending on the platform's pointer size
1733 which is a shortcut I used.
1734
1735 > For this .so that gives nonsense (very small) PCs which are later
1736 > rejected by the sanity check which ensures PC ranges fall inside
1737 > the mapped text segment. It seems like the .so expects to have the
1738 > start VMA of the text segment added on. This would correspond to
1739 >
1740 > static UChar default_Addr_encoding ( void )
1741 > {
1742 > switch (sizeof(Addr)) {
1743 > case 4: return DW_EH_PE_textrel + DW_EH_PE_udata4;
1744 > case 8: return DW_EH_PE_textrel + DW_EH_PE_udata8;
1745 > default: vg_assert(0);
1746 > }
1747 > }
1748
1749 The problem you're seeing is that you have absolute pointers inside
1750 a shared library, which obviously makes little sense on the face of
1751 things as how would the linker know where the library will be
1752 loaded?
1753
1754 The answer of course is that it doesn't, so if it points absolute
1755 pointers in the frame unwind data is has to include relocations for
1756 them, and I'm betting that if you look at the relocations in the
1757 library you will there are some for that data.
1758
1759 That is fine of course when ld.so maps the library - it will
1760 relocate the eh_frame data as it maps it (or prelinking will
1761 already have done so) and when the g++ exception code kicks in and
1762 unwinds the stack it will see relocated data.
1763
1764 We of course are mapping the section from the ELF file ourselves
1765 and are not applying the relocations, hence the problem you are
1766 seeing.
1767
1768 Strictly speaking we should apply the relocations but the cheap
1769 solution is essentially to do what you've done - strictly speaking
1770 you should adjust by the difference between the address the library
1771 was linked for and the address it has been loaded at, but a shared
1772 library will normally be linked for address zero I believe. It's
1773 possible that prelinking might change that though?
1774
1775 JRS:
1776 That all syncs with what I am seeing.
1777
1778 So what I am inclined to do is:
1779
1780 - Leave default_Addr_encoding as it is
1781
1782 - Change read_encoded_Addr's handling of "case DW_EH_PE_absptr" so
1783 it sets base to, as you say, the difference between the address
1784 the library was linked for and the address it has been loaded at
1785 (== the SegInfo's text_bias)
1786
1787 Does that sound sane? I think it should even handle the prelinked
1788 case.
1789
1790 (JRS, later)
1791
1792 Hmm. Plausible as it sounds, it doesn't work. It now produces
1793 bogus backtraces for locations inside the (statically linked)
1794 memcheck executable.
1795
1796 Besides, there are a couple of other places where read_encoded_Addr
1797 is used -- one of which is used to establish the length of the
1798 address range covered by the current FDE:
1799
1800 fde_arange = read_encoded_Addr(&nbytes, &adi, data);
1801
1802 and it doesn't seem to make any sense for read_encoded_Addr to add
1803 on the text segment bias in that context. The DWARF3 spec says
1804 that both the initial_location and address_range (length) fields
1805 are encoded the same way ("target address"), so it is unclear at
1806 what stage in the process it would be appropriate to relocate the
1807 former but not the latter.
1808
1809 One unprincipled kludge that does work is the following: just
1810 before handing one of the address range fragments off to
1811 ML_(addDiCfSI) for permanent storage, check its start address. If
1812 that is very low (less than 2 M), and is far below the mapped text
1813 segment, and adding the text bias would move the fragment entirely
1814 inside the mapped text segment, then do so. A kind of kludged
1815 last-minute relocation, if you like.
1816
1817 12 Jan 07: committing said kludge (see kludge_then_addDiCfSI). If
1818 the situation clarifies, it can easily enough be backed out and
1819 replaced by a better fix.
1820 */
1821
1822 /* --------------- Decls --------------- */
1823
1824 #if defined(VGP_x86_linux)
1825 # define FP_REG 5
1826 # define SP_REG 4
1827 # define RA_REG_DEFAULT 8
1828 #elif defined(VGP_amd64_linux)
1829 # define FP_REG 6
1830 # define SP_REG 7
1831 # define RA_REG_DEFAULT 16
1832 #elif defined(VGP_ppc32_linux)
1833 # define FP_REG 1
1834 # define SP_REG 1
1835 # define RA_REG_DEFAULT 65
1836 #elif defined(VGP_ppc64_linux)
1837 # define FP_REG 1
1838 # define SP_REG 1
1839 # define RA_REG_DEFAULT 65
1840 #elif defined(VGP_arm_linux)
1841 # define FP_REG 12
1842 # define SP_REG 13
1843 # define RA_REG_DEFAULT 14
1844 #elif defined(VGP_arm64_linux)
1845 # define FP_REG 29
1846 # define SP_REG 31
1847 # define RA_REG_DEFAULT 30
1848 #elif defined(VGP_x86_darwin)
1849 # define FP_REG 5
1850 # define SP_REG 4
1851 # define RA_REG_DEFAULT 8
1852 #elif defined(VGP_amd64_darwin)
1853 # define FP_REG 6
1854 # define SP_REG 7
1855 # define RA_REG_DEFAULT 16
1856 #elif defined(VGP_s390x_linux)
1857 # define FP_REG 11 // sometimes s390 has a frame pointer in r11
1858 # define SP_REG 15 // stack is always r15
1859 # define RA_REG_DEFAULT 14 // the return address is in r14
1860 #elif defined(VGP_mips32_linux)
1861 # define FP_REG 30
1862 # define SP_REG 29
1863 # define RA_REG_DEFAULT 31
1864 #elif defined(VGP_mips64_linux)
1865 # define FP_REG 30
1866 # define SP_REG 29
1867 # define RA_REG_DEFAULT 31
1868 #else
1869 # error "Unknown platform"
1870 #endif
1871
1872 /* The number of regs we are prepared to unwind. The number for
1873 arm-linux (320) seems ludicrously high, but the ARM IHI 0040A page
1874 7 (DWARF for the ARM Architecture) specifies that values up to 320
1875 might exist, for Neon/VFP-v3. */
1876 #if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) \
1877 || defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
1878 # define N_CFI_REGS 72
1879 #elif defined(VGP_arm_linux)
1880 # define N_CFI_REGS 320
1881 #elif defined(VGP_arm64_linux)
1882 # define N_CFI_REGS 128
1883 #else
1884 # define N_CFI_REGS 20
1885 #endif
1886
1887 /* Instructions for the automaton */
1888 enum dwarf_cfa_primary_ops
1889 {
1890 DW_CFA_use_secondary = 0,
1891 DW_CFA_advance_loc = 1,
1892 DW_CFA_offset = 2,
1893 DW_CFA_restore = 3
1894 };
1895
1896 enum dwarf_cfa_secondary_ops
1897 {
1898 DW_CFA_nop = 0x00,
1899 DW_CFA_set_loc = 0x01,
1900 DW_CFA_advance_loc1 = 0x02,
1901 DW_CFA_advance_loc2 = 0x03,
1902 DW_CFA_advance_loc4 = 0x04,
1903 DW_CFA_offset_extended = 0x05,
1904 DW_CFA_restore_extended = 0x06,
1905 DW_CFA_undefined = 0x07,
1906 DW_CFA_same_value = 0x08,
1907 DW_CFA_register = 0x09,
1908 DW_CFA_remember_state = 0x0a,
1909 DW_CFA_restore_state = 0x0b,
1910 DW_CFA_def_cfa = 0x0c,
1911 DW_CFA_def_cfa_register = 0x0d,
1912 DW_CFA_def_cfa_offset = 0x0e,
1913 DW_CFA_def_cfa_expression = 0x0f, /* DWARF3 only */
1914 DW_CFA_expression = 0x10, /* DWARF3 only */
1915 DW_CFA_offset_extended_sf = 0x11, /* DWARF3 only */
1916 DW_CFA_def_cfa_sf = 0x12, /* DWARF3 only */
1917 DW_CFA_def_cfa_offset_sf = 0x13, /* DWARF3 only */
1918 DW_CFA_val_offset = 0x14, /* DWARF3 only */
1919 DW_CFA_val_offset_sf = 0x15, /* DWARF3 only */
1920 DW_CFA_val_expression = 0x16, /* DWARF3 only */
1921 DW_CFA_lo_user = 0x1c,
1922 DW_CFA_GNU_window_save = 0x2d, /* GNU extension */
1923 DW_CFA_GNU_args_size = 0x2e, /* GNU extension */
1924 DW_CFA_GNU_negative_offset_extended = 0x2f, /* GNU extension */
1925 DW_CFA_hi_user = 0x3f
1926 };
1927
1928 #define DW_EH_PE_absptr 0x00
1929 #define DW_EH_PE_omit 0xff
1930
1931 #define DW_EH_PE_uleb128 0x01
1932 #define DW_EH_PE_udata2 0x02
1933 #define DW_EH_PE_udata4 0x03
1934 #define DW_EH_PE_udata8 0x04
1935 #define DW_EH_PE_sleb128 0x09
1936 #define DW_EH_PE_sdata2 0x0A
1937 #define DW_EH_PE_sdata4 0x0B
1938 #define DW_EH_PE_sdata8 0x0C
1939 #define DW_EH_PE_signed 0x08
1940
1941 #define DW_EH_PE_pcrel 0x10
1942 #define DW_EH_PE_textrel 0x20
1943 #define DW_EH_PE_datarel 0x30
1944 #define DW_EH_PE_funcrel 0x40
1945 #define DW_EH_PE_aligned 0x50
1946
1947 #define DW_EH_PE_indirect 0x80
1948
1949
1950 /* RegRule and UnwindContext are used temporarily to do the unwinding.
1951 The result is then summarised into a sequence of CfiSIs, if
1952 possible. UnwindContext effectively holds the state of the
1953 abstract machine whilst it is running.
1954
1955 The CFA can either be a signed offset from a register,
1956 or an expression:
1957
1958 CFA = cfa_reg + cfa_off when UnwindContext.cfa_is_regoff==True
1959 | [[ cfa_expr_id ]]
1960
1961 When .cfa_is_regoff == True, cfa_expr_id must be zero
1962 When .cfa_is_regoff == False, cfa_reg must be zero
1963 and cfa_off must be zero
1964
1965 RegRule describes, for each register, how to get its
1966 value in the previous frame, where 'cfa' denotes the cfa
1967 for the frame as a whole:
1968
1969 RegRule = RR_Undef -- undefined
1970 | RR_Same -- same as in previous frame
1971 | RR_CFAOff arg -- is at * ( cfa + arg )
1972 | RR_CFAValOff arg -- is ( cfa + arg )
1973 | RR_Reg arg -- is in register 'arg'
1974 | RR_Expr arg -- is at * [[ arg ]]
1975 | RR_ValExpr arg -- is [[ arg ]]
1976 | RR_Arch -- dunno
1977
1978 Note that RR_Expr is redundant since the same can be represented
1979 using RR_ValExpr with an explicit dereference (CfiExpr_Deref) at
1980 the outermost level.
1981
1982 All expressions are stored in exprs in the containing
1983 UnwindContext. Since the UnwindContext gets reinitialised for each
1984 new FDE, summarise_context needs to copy out any expressions it
1985 wants to keep into the cfsi_exprs field of the containing SegInfo.
1986 */
1987 typedef
1988 struct {
1989 enum { RR_Undef, RR_Same, RR_CFAOff, RR_CFAValOff,
1990 RR_Reg, /*RR_Expr,*/ RR_ValExpr, RR_Arch } tag;
1991 /* meaning: int offset for CFAoff/CFAValOff
1992 reg # for Reg
1993 expr index for Expr/ValExpr */
1994 Int arg;
1995 }
1996 RegRule;
1997
ppRegRule(XArray * exprs,RegRule * rrule)1998 static void ppRegRule ( XArray* exprs, RegRule* rrule )
1999 {
2000 vg_assert(exprs);
2001 switch (rrule->tag) {
2002 case RR_Undef: VG_(printf)("u "); break;
2003 case RR_Same: VG_(printf)("s "); break;
2004 case RR_CFAOff: VG_(printf)("c%d ", rrule->arg); break;
2005 case RR_CFAValOff: VG_(printf)("v%d ", rrule->arg); break;
2006 case RR_Reg: VG_(printf)("r%d ", rrule->arg); break;
2007 case RR_ValExpr: VG_(printf)("ve{");
2008 ML_(ppCfiExpr)( exprs, rrule->arg );
2009 VG_(printf)("} ");
2010 break;
2011 case RR_Arch: VG_(printf)("a "); break;
2012 default: VG_(core_panic)("ppRegRule");
2013 }
2014 }
2015
2016
2017 /* Size of the stack of register unwind rules. This is only
2018 exceedingly rarely used, so a stack of size 1 should actually work
2019 with almost all compiler-generated CFA. */
2020 #define N_RR_STACK 4
2021
2022 typedef
2023 struct {
2024 /* Read-only fields (set by the CIE) */
2025 Int code_a_f;
2026 Int data_a_f;
2027 Addr initloc;
2028 Int ra_reg;
2029 /* The rest of these fields can be modifed by
2030 run_CF_instruction. */
2031 /* The LOC entry */
2032 Addr loc;
2033 /* We need a stack of these in order to handle
2034 DW_CFA_{remember,restore}_state. */
2035 struct UnwindContextState {
2036 /* The CFA entry. This can be either reg+/-offset or an expr. */
2037 Bool cfa_is_regoff; /* True=>is reg+offset; False=>is expr */
2038 Int cfa_reg;
2039 Int cfa_off; /* in bytes */
2040 Int cfa_expr_ix; /* index into cfa_exprs */
2041 /* Register unwind rules. */
2042 RegRule reg[N_CFI_REGS];
2043 }
2044 state[N_RR_STACK];
2045 Int state_sp; /* 0 <= state_sp < N_RR_STACK; points at the
2046 currently-in-use rule set. */
2047 /* array of CfiExpr, shared by reg[] and cfa_expr_ix */
2048 XArray* exprs;
2049 }
2050 UnwindContext;
2051
ppUnwindContext(UnwindContext * ctx)2052 static void ppUnwindContext ( UnwindContext* ctx )
2053 {
2054 Int j, i;
2055 VG_(printf)("0x%llx: ", (ULong)ctx->loc);
2056 for (j = 0; j <= ctx->state_sp; j++) {
2057 struct UnwindContextState* ctxs = &ctx->state[j];
2058 VG_(printf)("%s[%d]={ ", j > 0 ? " " : "", j);
2059 if (ctxs->cfa_is_regoff) {
2060 VG_(printf)("%d(r%d) ", ctxs->cfa_off, ctxs->cfa_reg);
2061 } else {
2062 vg_assert(ctx->exprs);
2063 VG_(printf)("{");
2064 ML_(ppCfiExpr)( ctx->exprs, ctxs->cfa_expr_ix );
2065 VG_(printf)("} ");
2066 }
2067 VG_(printf)("{ ");
2068 for (i = 0; i < N_CFI_REGS; i++)
2069 ppRegRule(ctx->exprs, &ctxs->reg[i]);
2070 VG_(printf)("}");
2071 }
2072 VG_(printf)("\n");
2073 }
2074
initUnwindContext(UnwindContext * ctx)2075 static void initUnwindContext ( /*OUT*/UnwindContext* ctx )
2076 {
2077 Int j, i;
2078 VG_(memset)(ctx, 0, sizeof(*ctx));
2079 /* ctx->code_a_f = 0;
2080 ctx->data_a_f = 0;
2081 ctx->initloc = 0; */
2082 ctx->ra_reg = RA_REG_DEFAULT;
2083 /* ctx->loc = 0;
2084 ctx->exprs = NULL;
2085 ctx->state_sp = 0; */
2086 for (j = 0; j < N_RR_STACK; j++) {
2087 ctx->state[j].cfa_is_regoff = True;
2088 /* ctx->state[j].cfa_reg = 0;
2089 ctx->state[j].cfa_off = 0;
2090 ctx->state[j].cfa_expr_ix = 0; */
2091 for (i = 0; i < N_CFI_REGS; i++) {
2092 if (RR_Undef != 0)
2093 ctx->state[j].reg[i].tag = RR_Undef;
2094 /* ctx->state[j].reg[i].arg = 0; */
2095 }
2096 # if defined(VGA_arm)
2097 /* All callee-saved registers (or at least the ones we are
2098 summarising for) should start out as RR_Same, on ARM. */
2099 ctx->state[j].reg[11].tag = RR_Same;
2100 /* ctx->state[j].reg[13].tag = RR_Same; */
2101 ctx->state[j].reg[14].tag = RR_Same;
2102 ctx->state[j].reg[12].tag = RR_Same;
2103 ctx->state[j].reg[7].tag = RR_Same;
2104 /* this can't be right though: R12 (IP) isn't callee saved. */
2105 # elif defined(VGA_arm64)
2106 /* Callee-saved registers (that we are interested in) should
2107 start out as RR_Same. */
2108 ctx->state[j].reg[29/*FP*/].tag = RR_Same;
2109 ctx->state[j].reg[30/*LR*/].tag = RR_Same;
2110 # endif
2111 }
2112 }
2113
2114
2115 /* A structure which holds information needed by read_encoded_Addr().
2116 */
2117 typedef
2118 struct {
2119 UChar encoding;
2120 DiCursor ehframe_image;
2121 Addr ehframe_avma;
2122 Addr text_bias;
2123 }
2124 AddressDecodingInfo;
2125
2126
2127 /* ------------ Deal with summary-info records ------------ */
2128
initCfiSI(DiCfSI * si)2129 static void initCfiSI ( DiCfSI* si )
2130 {
2131 VG_(bzero_inline)(si, sizeof(*si));
2132 }
2133
2134
2135 /* --------------- Summarisation --------------- */
2136
2137 /* Forward */
2138 static
2139 Int copy_convert_CfiExpr_tree ( XArray* dst,
2140 UnwindContext* srcuc,
2141 Int nd );
2142
2143 /* Summarise ctx into si, if possible. Returns True if successful.
2144 This is taken to be just after ctx's loc advances; hence the
2145 summary is up to but not including the current loc. This works
2146 on both x86 and amd64.
2147 */
summarise_context(DiCfSI * si,Addr loc_start,UnwindContext * ctx,struct _DebugInfo * debuginfo)2148 static Bool summarise_context( /*OUT*/DiCfSI* si,
2149 Addr loc_start,
2150 UnwindContext* ctx,
2151 struct _DebugInfo* debuginfo )
2152 {
2153 Int why = 0;
2154 struct UnwindContextState* ctxs;
2155 initCfiSI(si);
2156
2157 /* Guard against obviously stupid settings of the reg-rule stack
2158 pointer. */
2159 if (ctx->state_sp < 0) { why = 8; goto failed; }
2160 if (ctx->state_sp >= N_RR_STACK) { why = 9; goto failed; }
2161 ctxs = &ctx->state[ctx->state_sp];
2162
2163 /* First, summarise the method for generating the CFA */
2164 if (!ctxs->cfa_is_regoff) {
2165 /* it was set by DW_CFA_def_cfa_expression; try to convert */
2166 XArray *src, *dst;
2167 Int conv;
2168 src = ctx->exprs;
2169 dst = debuginfo->cfsi_exprs;
2170 if (src && (VG_(sizeXA)(src) > 0) && (!dst)) {
2171 dst = VG_(newXA)( ML_(dinfo_zalloc), "di.ccCt.1", ML_(dinfo_free),
2172 sizeof(CfiExpr) );
2173 vg_assert(dst);
2174 debuginfo->cfsi_exprs = dst;
2175 }
2176 conv = copy_convert_CfiExpr_tree
2177 ( dst, ctx, ctxs->cfa_expr_ix );
2178 vg_assert(conv >= -1);
2179 if (conv == -1) { why = 6; goto failed; }
2180 si->cfa_how = CFIC_EXPR;
2181 si->cfa_off = conv;
2182 if (0 && debuginfo->ddump_frames)
2183 ML_(ppCfiExpr)(dst, conv);
2184 }
2185 else
2186 if (ctxs->cfa_is_regoff && ctxs->cfa_reg == SP_REG) {
2187 si->cfa_off = ctxs->cfa_off;
2188 # if defined(VGA_x86) || defined(VGA_amd64) || defined(VGA_s390x) \
2189 || defined(VGA_mips32) || defined(VGA_mips64)
2190 si->cfa_how = CFIC_IA_SPREL;
2191 # elif defined(VGA_arm)
2192 si->cfa_how = CFIC_ARM_R13REL;
2193 # elif defined(VGA_arm64)
2194 si->cfa_how = CFIC_ARM64_SPREL;
2195 # else
2196 si->cfa_how = 0; /* invalid */
2197 # endif
2198 }
2199 else
2200 if (ctxs->cfa_is_regoff && ctxs->cfa_reg == FP_REG) {
2201 si->cfa_off = ctxs->cfa_off;
2202 # if defined(VGA_x86) || defined(VGA_amd64) || defined(VGA_s390x) \
2203 || defined(VGA_mips32) || defined(VGA_mips64)
2204 si->cfa_how = CFIC_IA_BPREL;
2205 # elif defined(VGA_arm)
2206 si->cfa_how = CFIC_ARM_R12REL;
2207 # elif defined(VGA_arm64)
2208 si->cfa_how = CFIC_ARM64_X29REL;
2209 # else
2210 si->cfa_how = 0; /* invalid */
2211 # endif
2212 }
2213 # if defined(VGA_arm)
2214 else
2215 if (ctxs->cfa_is_regoff && ctxs->cfa_reg == 11/*??_REG*/) {
2216 si->cfa_how = CFIC_ARM_R11REL;
2217 si->cfa_off = ctxs->cfa_off;
2218 }
2219 else
2220 if (ctxs->cfa_is_regoff && ctxs->cfa_reg == 7/*??_REG*/) {
2221 si->cfa_how = CFIC_ARM_R7REL;
2222 si->cfa_off = ctxs->cfa_off;
2223 }
2224 # elif defined(VGA_arm64)
2225 // do we need any arm64 specifics here?
2226 # endif
2227 else {
2228 why = 1;
2229 goto failed;
2230 }
2231
2232 # define SUMMARISE_HOW(_how, _off, _ctxreg) \
2233 switch (_ctxreg.tag) { \
2234 case RR_Undef: \
2235 _how = CFIR_UNKNOWN; _off = 0; break; \
2236 case RR_Same: \
2237 _how = CFIR_SAME; _off = 0; break; \
2238 case RR_CFAOff: \
2239 _how = CFIR_MEMCFAREL; _off = _ctxreg.arg; break; \
2240 case RR_CFAValOff: \
2241 _how = CFIR_CFAREL; _off = _ctxreg.arg; break; \
2242 case RR_ValExpr: { \
2243 XArray *src, *dst; \
2244 Int conv; \
2245 src = ctx->exprs; \
2246 dst = debuginfo->cfsi_exprs; \
2247 if (src && (VG_(sizeXA)(src) > 0) && (!dst)) { \
2248 dst = VG_(newXA)( ML_(dinfo_zalloc), \
2249 "di.ccCt.2", \
2250 ML_(dinfo_free), \
2251 sizeof(CfiExpr) ); \
2252 vg_assert(dst); \
2253 debuginfo->cfsi_exprs = dst; \
2254 } \
2255 conv = copy_convert_CfiExpr_tree \
2256 ( dst, ctx, _ctxreg.arg ); \
2257 vg_assert(conv >= -1); \
2258 if (conv == -1) { why = 7; goto failed; } \
2259 _how = CFIR_EXPR; \
2260 _off = conv; \
2261 if (0 && debuginfo->ddump_frames) \
2262 ML_(ppCfiExpr)(dst, conv); \
2263 break; \
2264 } \
2265 default: \
2266 why = 2; goto failed; /* otherwise give up */ \
2267 }
2268
2269
2270 # if defined(VGA_x86) || defined(VGA_amd64)
2271
2272 /* --- entire tail of this fn specialised for x86/amd64 --- */
2273
2274 SUMMARISE_HOW(si->ra_how, si->ra_off,
2275 ctxs->reg[ctx->ra_reg] );
2276 SUMMARISE_HOW(si->bp_how, si->bp_off,
2277 ctxs->reg[FP_REG] );
2278
2279 /* on x86/amd64, it seems the old %{e,r}sp value before the call is
2280 always the same as the CFA. Therefore ... */
2281 si->sp_how = CFIR_CFAREL;
2282 si->sp_off = 0;
2283
2284 /* also, gcc says "Undef" for %{e,r}bp when it is unchanged. So
2285 .. */
2286 if (ctxs->reg[FP_REG].tag == RR_Undef)
2287 si->bp_how = CFIR_SAME;
2288
2289 /* knock out some obviously stupid cases */
2290 if (si->ra_how == CFIR_SAME)
2291 { why = 3; goto failed; }
2292
2293 /* bogus looking range? Note, we require that the difference is
2294 representable in 32 bits. */
2295 if (loc_start >= ctx->loc)
2296 { why = 4; goto failed; }
2297 if (ctx->loc - loc_start > 10000000 /* let's say */)
2298 { why = 5; goto failed; }
2299
2300 si->base = loc_start + ctx->initloc;
2301 si->len = (UInt)(ctx->loc - loc_start);
2302
2303 return True;
2304
2305 # elif defined(VGA_arm)
2306
2307 /* ---- entire tail of this fn specialised for arm ---- */
2308
2309 SUMMARISE_HOW(si->r14_how, si->r14_off,
2310 ctxs->reg[14] );
2311
2312 //SUMMARISE_HOW(si->r13_how, si->r13_off,
2313 // ctxs->reg[13] );
2314
2315 SUMMARISE_HOW(si->r12_how, si->r12_off,
2316 ctxs->reg[FP_REG] );
2317
2318 SUMMARISE_HOW(si->r11_how, si->r11_off,
2319 ctxs->reg[11/*FP_REG*/] );
2320
2321 SUMMARISE_HOW(si->r7_how, si->r7_off,
2322 ctxs->reg[7] );
2323
2324 if (ctxs->reg[14/*LR*/].tag == RR_Same
2325 && ctx->ra_reg == 14/*as we expect it always to be*/) {
2326 /* Generate a trivial CfiExpr, which merely says "r14". First
2327 ensure this DebugInfo has a cfsi_expr array in which to park
2328 it. */
2329 if (!debuginfo->cfsi_exprs)
2330 debuginfo->cfsi_exprs = VG_(newXA)( ML_(dinfo_zalloc),
2331 "di.ccCt.2a",
2332 ML_(dinfo_free),
2333 sizeof(CfiExpr) );
2334 si->ra_off = ML_(CfiExpr_CfiReg)( debuginfo->cfsi_exprs,
2335 Creg_ARM_R14);
2336 si->ra_how = CFIR_EXPR;
2337 } else {
2338 /* Just summarise it in the normal way */
2339 SUMMARISE_HOW(si->ra_how, si->ra_off,
2340 ctxs->reg[ctx->ra_reg] );
2341 }
2342
2343 /* on arm, it seems the old r13 (SP) value before the call is
2344 always the same as the CFA. Therefore ... */
2345 si->r13_how = CFIR_CFAREL;
2346 si->r13_off = 0;
2347
2348 /* bogus looking range? Note, we require that the difference is
2349 representable in 32 bits. */
2350 if (loc_start >= ctx->loc)
2351 { why = 4; goto failed; }
2352 if (ctx->loc - loc_start > 10000000 /* let's say */)
2353 { why = 5; goto failed; }
2354
2355 si->base = loc_start + ctx->initloc;
2356 si->len = (UInt)(ctx->loc - loc_start);
2357
2358 return True;
2359
2360 # elif defined(VGA_arm64)
2361
2362 /* --- entire tail of this fn specialised for arm64 --- */
2363
2364 SUMMARISE_HOW(si->x30_how, si->x30_off, ctxs->reg[30/*LR*/]);
2365 SUMMARISE_HOW(si->x29_how, si->x29_off, ctxs->reg[29/*FP*/]);
2366
2367 if (ctxs->reg[30/*LR*/].tag == RR_Same
2368 && ctx->ra_reg == 30/*as we expect it always to be*/) {
2369 /* Generate a trivial CfiExpr, which merely says "x30". First
2370 ensure this DebugInfo has a cfsi_expr array in which to park
2371 it. */
2372 if (!debuginfo->cfsi_exprs)
2373 debuginfo->cfsi_exprs = VG_(newXA)( ML_(dinfo_zalloc),
2374 "di.ccCt.2a-arm64",
2375 ML_(dinfo_free),
2376 sizeof(CfiExpr) );
2377 si->ra_off = ML_(CfiExpr_CfiReg)( debuginfo->cfsi_exprs,
2378 Creg_ARM64_X30);
2379 si->ra_how = CFIR_EXPR;
2380 } else {
2381 /* Just summarise it in the normal way */
2382 SUMMARISE_HOW(si->ra_how, si->ra_off, ctxs->reg[ctx->ra_reg]);
2383 }
2384
2385 /* on arm64, it seems the old SP value before the call is always
2386 the same as the CFA. Therefore ... */
2387 si->sp_how = CFIR_CFAREL;
2388 si->sp_off = 0;
2389
2390 /* bogus looking range? Note, we require that the difference is
2391 representable in 32 bits. */
2392 if (loc_start >= ctx->loc)
2393 { why = 4; goto failed; }
2394 if (ctx->loc - loc_start > 10000000 /* let's say */)
2395 { why = 5; goto failed; }
2396
2397 si->base = loc_start + ctx->initloc;
2398 si->len = (UInt)(ctx->loc - loc_start);
2399
2400 return True;
2401
2402 # elif defined(VGA_s390x)
2403
2404 /* --- entire tail of this fn specialised for s390 --- */
2405
2406 SUMMARISE_HOW(si->ra_how, si->ra_off,
2407 ctxs->reg[ctx->ra_reg] );
2408 SUMMARISE_HOW(si->fp_how, si->fp_off,
2409 ctxs->reg[FP_REG] );
2410 SUMMARISE_HOW(si->sp_how, si->sp_off,
2411 ctxs->reg[SP_REG] );
2412
2413 /* change some defaults to consumable values */
2414 if (si->sp_how == CFIR_UNKNOWN)
2415 si->sp_how = CFIR_SAME;
2416
2417 if (si->fp_how == CFIR_UNKNOWN)
2418 si->fp_how = CFIR_SAME;
2419
2420 if (si->cfa_how == CFIR_UNKNOWN) {
2421 si->cfa_how = CFIC_IA_SPREL;
2422 si->cfa_off = 160;
2423 }
2424 if (si->ra_how == CFIR_UNKNOWN) {
2425 if (!debuginfo->cfsi_exprs)
2426 debuginfo->cfsi_exprs = VG_(newXA)( ML_(dinfo_zalloc),
2427 "di.ccCt.2a",
2428 ML_(dinfo_free),
2429 sizeof(CfiExpr) );
2430 si->ra_how = CFIR_EXPR;
2431 si->ra_off = ML_(CfiExpr_CfiReg)( debuginfo->cfsi_exprs,
2432 Creg_S390_R14);
2433 }
2434
2435 /* knock out some obviously stupid cases */
2436 if (si->ra_how == CFIR_SAME)
2437 { why = 3; goto failed; }
2438
2439 /* bogus looking range? Note, we require that the difference is
2440 representable in 32 bits. */
2441 if (loc_start >= ctx->loc)
2442 { why = 4; goto failed; }
2443 if (ctx->loc - loc_start > 10000000 /* let's say */)
2444 { why = 5; goto failed; }
2445
2446 si->base = loc_start + ctx->initloc;
2447 si->len = (UInt)(ctx->loc - loc_start);
2448
2449 return True;
2450
2451 # elif defined(VGA_mips32) || defined(VGA_mips64)
2452
2453 /* --- entire tail of this fn specialised for mips --- */
2454
2455 SUMMARISE_HOW(si->ra_how, si->ra_off,
2456 ctxs->reg[ctx->ra_reg] );
2457 SUMMARISE_HOW(si->fp_how, si->fp_off,
2458 ctxs->reg[FP_REG] );
2459 SUMMARISE_HOW(si->sp_how, si->sp_off,
2460 ctxs->reg[SP_REG] );
2461 si->sp_how = CFIR_CFAREL;
2462 si->sp_off = 0;
2463
2464 if (si->fp_how == CFIR_UNKNOWN)
2465 si->fp_how = CFIR_SAME;
2466 if (si->cfa_how == CFIR_UNKNOWN) {
2467 si->cfa_how = CFIC_IA_SPREL;
2468 si->cfa_off = 160;
2469 }
2470 if (si->ra_how == CFIR_UNKNOWN) {
2471 if (!debuginfo->cfsi_exprs)
2472 debuginfo->cfsi_exprs = VG_(newXA)( ML_(dinfo_zalloc),
2473 "di.ccCt.2a",
2474 ML_(dinfo_free),
2475 sizeof(CfiExpr) );
2476 si->ra_how = CFIR_EXPR;
2477 si->ra_off = ML_(CfiExpr_CfiReg)( debuginfo->cfsi_exprs,
2478 Creg_MIPS_RA);
2479 }
2480
2481 if (si->ra_how == CFIR_SAME)
2482 { why = 3; goto failed; }
2483
2484 if (loc_start >= ctx->loc)
2485 { why = 4; goto failed; }
2486 if (ctx->loc - loc_start > 10000000 /* let's say */)
2487 { why = 5; goto failed; }
2488
2489 si->base = loc_start + ctx->initloc;
2490 si->len = (UInt)(ctx->loc - loc_start);
2491
2492 return True;
2493
2494 # elif defined(VGA_ppc32) || defined(VGA_ppc64)
2495 /* These don't use CFI based unwinding (is that really true?) */
2496
2497 # else
2498 # error "Unknown arch"
2499 # endif
2500
2501 /* --- non-specialised code after this point --- */
2502
2503 # undef SUMMARISE_HOW
2504
2505 failed:
2506 if (VG_(clo_verbosity) > 2 || debuginfo->trace_cfi) {
2507 VG_(message)(Vg_DebugMsg,
2508 "summarise_context(loc_start = %#lx)"
2509 ": cannot summarise(why=%d): \n", loc_start, why);
2510 ppUnwindContext(ctx);
2511 }
2512 return False;
2513 }
2514
2515 /* Copy the tree rooted at srcuc->exprs node srcix to dstxa, on the
2516 way converting any DwReg regs (regs numbered using the Dwarf scheme
2517 defined by each architecture's ABI) into CfiRegs, which are
2518 platform independent. If the conversion isn't possible because
2519 there is no equivalent register, return -1. This has the
2520 undesirable side effect of de-dagifying the input; oh well. */
copy_convert_CfiExpr_tree(XArray * dstxa,UnwindContext * srcuc,Int srcix)2521 static Int copy_convert_CfiExpr_tree ( XArray* dstxa,
2522 UnwindContext* srcuc,
2523 Int srcix )
2524 {
2525 CfiExpr* src;
2526 Int cpL, cpR, cpA;
2527 XArray* srcxa = srcuc->exprs;
2528 vg_assert(srcxa);
2529 vg_assert(dstxa);
2530 vg_assert(srcix >= 0 && srcix < VG_(sizeXA)(srcxa));
2531
2532 src = VG_(indexXA)( srcxa, srcix );
2533 switch (src->tag) {
2534 case Cex_Undef:
2535 return ML_(CfiExpr_Undef)( dstxa );
2536 case Cex_Deref:
2537 cpA = copy_convert_CfiExpr_tree( dstxa, srcuc, src->Cex.Deref.ixAddr );
2538 if (cpA == -1)
2539 return -1; /* propagate failure */
2540 return ML_(CfiExpr_Deref)( dstxa, cpA );
2541 case Cex_Const:
2542 return ML_(CfiExpr_Const)( dstxa, src->Cex.Const.con );
2543 case Cex_Binop:
2544 cpL = copy_convert_CfiExpr_tree( dstxa, srcuc, src->Cex.Binop.ixL );
2545 cpR = copy_convert_CfiExpr_tree( dstxa, srcuc, src->Cex.Binop.ixR );
2546 vg_assert(cpL >= -1 && cpR >= -1);
2547 if (cpL == -1 || cpR == -1)
2548 return -1; /* propagate failure */
2549 return ML_(CfiExpr_Binop)( dstxa, src->Cex.Binop.op, cpL, cpR );
2550 case Cex_CfiReg:
2551 /* should not see these in input (are created only by this
2552 conversion step!) */
2553 VG_(core_panic)("copy_convert_CfiExpr_tree: CfiReg in input");
2554 case Cex_DwReg: {
2555 /* This is the only place where the conversion can fail. */
2556 Int dwreg __attribute__((unused));
2557 dwreg = src->Cex.DwReg.reg;
2558 # if defined(VGA_x86) || defined(VGA_amd64)
2559 if (dwreg == SP_REG)
2560 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_SP );
2561 if (dwreg == FP_REG)
2562 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_BP );
2563 if (dwreg == srcuc->ra_reg)
2564 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_IP ); /* correct? */
2565 # elif defined(VGA_arm)
2566 if (dwreg == SP_REG)
2567 return ML_(CfiExpr_CfiReg)( dstxa, Creg_ARM_R13 );
2568 if (dwreg == FP_REG)
2569 return ML_(CfiExpr_CfiReg)( dstxa, Creg_ARM_R12 );
2570 if (dwreg == srcuc->ra_reg)
2571 return ML_(CfiExpr_CfiReg)( dstxa, Creg_ARM_R15 ); /* correct? */
2572 # elif defined(VGA_s390x)
2573 if (dwreg == SP_REG)
2574 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_SP );
2575 if (dwreg == FP_REG)
2576 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_BP );
2577 if (dwreg == srcuc->ra_reg)
2578 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_IP ); /* correct? */
2579 # elif defined(VGA_mips32) || defined(VGA_mips64)
2580 if (dwreg == SP_REG)
2581 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_SP );
2582 if (dwreg == FP_REG)
2583 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_BP );
2584 if (dwreg == srcuc->ra_reg)
2585 return ML_(CfiExpr_CfiReg)( dstxa, Creg_IA_IP );
2586 # elif defined(VGA_arm64)
2587 I_die_here;
2588 # elif defined(VGA_ppc32) || defined(VGA_ppc64)
2589 # else
2590 # error "Unknown arch"
2591 # endif
2592 /* else we must fail - can't represent the reg */
2593 return -1;
2594 }
2595 default:
2596 VG_(core_panic)("copy_convert_CfiExpr_tree: default");
2597 }
2598 }
2599
2600
ppUnwindContext_summary(UnwindContext * ctx)2601 static void ppUnwindContext_summary ( UnwindContext* ctx )
2602 {
2603 struct UnwindContextState* ctxs = &ctx->state[ctx->state_sp];
2604
2605 VG_(printf)("0x%llx-1: ", (ULong)ctx->loc);
2606
2607 if (ctxs->cfa_reg == SP_REG) {
2608 VG_(printf)("SP/CFA=%d+SP ", ctxs->cfa_off);
2609 } else
2610 if (ctxs->cfa_reg == FP_REG) {
2611 VG_(printf)("SP/CFA=%d+FP ", ctxs->cfa_off);
2612 } else {
2613 VG_(printf)("SP/CFA=unknown ");
2614 }
2615
2616 VG_(printf)("RA=");
2617 ppRegRule( ctx->exprs, &ctxs->reg[ctx->ra_reg] );
2618
2619 VG_(printf)("FP=");
2620 ppRegRule( ctx->exprs, &ctxs->reg[FP_REG] );
2621 VG_(printf)("\n");
2622 }
2623
2624
2625 /* ------------ Pick apart DWARF2 byte streams ------------ */
2626
step_le_u_encoded_literal(DiCursor * data,UInt size)2627 static ULong step_le_u_encoded_literal ( DiCursor* data, UInt size )
2628 {
2629 switch (size) {
2630 case 8: return (ULong)ML_(cur_step_ULong)( data );
2631 case 4: return (ULong)ML_(cur_step_UInt)( data );
2632 case 2: return (ULong)ML_(cur_step_UShort)( data );
2633 case 1: return (ULong)ML_(cur_step_UChar)( data );
2634 default: vg_assert(0); /*NOTREACHED*/ return 0;
2635 }
2636 }
2637
step_le_s_encoded_literal(DiCursor * data,UInt size)2638 static Long step_le_s_encoded_literal ( DiCursor* data, UInt size )
2639 {
2640 Long s64 = step_le_u_encoded_literal( data, size );
2641 switch (size) {
2642 case 8: break;
2643 case 4: s64 <<= 32; s64 >>= 32; break;
2644 case 2: s64 <<= 48; s64 >>= 48; break;
2645 case 1: s64 <<= 56; s64 >>= 56; break;
2646 default: vg_assert(0); /*NOTREACHED*/ return 0;
2647 }
2648 return s64;
2649 }
2650
default_Addr_encoding(void)2651 static UChar default_Addr_encoding ( void )
2652 {
2653 switch (sizeof(Addr)) {
2654 case 4: return DW_EH_PE_udata4;
2655 case 8: return DW_EH_PE_udata8;
2656 default: vg_assert(0);
2657 }
2658 }
2659
size_of_encoded_Addr(UChar encoding)2660 static UInt size_of_encoded_Addr ( UChar encoding )
2661 {
2662 if (encoding == DW_EH_PE_omit)
2663 return 0;
2664
2665 switch (encoding & 0x07) {
2666 case DW_EH_PE_absptr: return sizeof(Addr);
2667 case DW_EH_PE_udata2: return sizeof(UShort);
2668 case DW_EH_PE_udata4: return sizeof(UInt);
2669 case DW_EH_PE_udata8: return sizeof(ULong);
2670 default: vg_assert(0);
2671 }
2672 }
2673
step_encoded_Addr(AddressDecodingInfo * adi,DiCursor * data)2674 static Addr step_encoded_Addr ( AddressDecodingInfo* adi,
2675 /*MOD*/DiCursor* data )
2676 {
2677 /* Regarding the handling of DW_EH_PE_absptr. DWARF3 says this
2678 denotes an absolute address, hence you would think 'base' is
2679 zero. However, that is nonsensical (unless relocations are to
2680 be applied to the unwind data before reading it, which sounds
2681 unlikely). My interpretation is that DW_EH_PE_absptr indicates
2682 an address relative to where the object was loaded (technically,
2683 relative to its stated load VMA, hence the use of text_bias
2684 rather than text_avma). Hmm, should we use text_bias or
2685 text_avma here? Not sure.
2686
2687 This view appears to be supported by DWARF3 spec sec 7.3
2688 "Executable Objects and Shared Objects":
2689
2690 This requirement makes the debugging information for shared
2691 objects position independent. Virtual addresses in a shared
2692 object may be calculated by adding the offset to the base
2693 address at which the object was attached. This offset is
2694 available in the run-time linker's data structures.
2695 */
2696 Addr base;
2697 Word offset;
2698 UChar encoding = adi->encoding;
2699 DiCursor ehframe_image = adi->ehframe_image;
2700 Addr ehframe_avma = adi->ehframe_avma;
2701
2702 vg_assert((encoding & DW_EH_PE_indirect) == 0);
2703
2704 switch (encoding & 0x70) {
2705 case DW_EH_PE_absptr:
2706 base = adi->text_bias;
2707 break;
2708 case DW_EH_PE_pcrel:
2709 base = ehframe_avma + ML_(cur_minus)(*data, ehframe_image);
2710 break;
2711 case DW_EH_PE_datarel:
2712 vg_assert(0);
2713 base = /* data base address */ 0;
2714 break;
2715 case DW_EH_PE_textrel:
2716 vg_assert(0);
2717 base = /* text base address */ 0;
2718 break;
2719 case DW_EH_PE_funcrel:
2720 base = 0;
2721 break;
2722 case DW_EH_PE_aligned:
2723 base = 0;
2724 offset = ML_(cur_minus)(*data, ehframe_image);
2725 if ((offset % sizeof(Addr)) != 0) {
2726 Word nbytes = sizeof(Addr) - (offset % sizeof(Addr));
2727 *data = ML_(cur_plus)(*data, nbytes);
2728 }
2729 break;
2730 default:
2731 vg_assert(0);
2732 }
2733
2734 if ((encoding & 0x07) == 0x00)
2735 encoding |= default_Addr_encoding();
2736
2737 switch (encoding & 0x0f) {
2738 case DW_EH_PE_udata2:
2739 return base + ML_(cur_step_UShort)(data);
2740 case DW_EH_PE_udata4:
2741 return base + ML_(cur_step_UInt)(data);
2742 case DW_EH_PE_udata8:
2743 return base + ML_(cur_step_ULong)(data);
2744 case DW_EH_PE_sdata2:
2745 return base + ML_(cur_step_Short)(data);
2746 case DW_EH_PE_sdata4:
2747 return base + ML_(cur_step_Int)(data);
2748 case DW_EH_PE_sdata8:
2749 return base + ML_(cur_step_Long)(data);
2750 default:
2751 vg_assert2(0, "read encoded address %d\n", encoding & 0x0f);
2752 }
2753 }
2754
2755
2756 /* ------------ Run/show DWARF3 expressions ---------- */
2757
2758 /* Convert the DWARF3 expression in expr[0 .. exprlen-1] into a dag
2759 (of CfiExprs) stored in ctx->exprs, and return the index in
2760 ctx->exprs of the root node. Or fail in which case return -1. */
2761 /* IMPORTANT: when adding expression forms here, also remember to
2762 add suitable evaluation code in evalCfiExpr in debuginfo.c. */
dwarfexpr_to_dag(UnwindContext * ctx,DiCursor expr,Int exprlen,Bool push_cfa_at_start,Bool ddump_frames)2763 static Int dwarfexpr_to_dag ( UnwindContext* ctx,
2764 DiCursor expr, Int exprlen,
2765 Bool push_cfa_at_start,
2766 Bool ddump_frames )
2767 {
2768 # define N_EXPR_STACK 20
2769
2770 # define PUSH(_arg) \
2771 do { \
2772 vg_assert(sp >= -1 && sp < N_EXPR_STACK); \
2773 if (sp == N_EXPR_STACK-1) \
2774 return -1; \
2775 sp++; \
2776 stack[sp] = (_arg); \
2777 } while (0)
2778
2779 # define POP(_lval) \
2780 do { \
2781 vg_assert(sp >= -1 && sp < N_EXPR_STACK); \
2782 if (sp == -1) \
2783 return -1; \
2784 _lval = stack[sp]; \
2785 sp--; \
2786 } while (0)
2787
2788 Int ix, ix2, reg;
2789 UChar opcode;
2790 Word sw;
2791 UWord uw;
2792 CfiUnop uop;
2793 CfiBinop bop;
2794 const HChar* opname;
2795
2796 Int sp; /* # of top element: valid is -1 .. N_EXPR_STACK-1 */
2797 Int stack[N_EXPR_STACK]; /* indices into ctx->exprs */
2798 struct UnwindContextState* ctxs = &ctx->state[ctx->state_sp];
2799
2800 XArray* dst = ctx->exprs;
2801 DiCursor limit = ML_(cur_plus)(expr, exprlen);
2802
2803 vg_assert(dst);
2804 vg_assert(exprlen >= 0);
2805
2806 sp = -1; /* empty */
2807
2808 /* Synthesise the CFA as a CfiExpr */
2809 if (push_cfa_at_start) {
2810 if (ctxs->cfa_is_regoff) {
2811 /* cfa is reg +/- offset */
2812 ix = ML_(CfiExpr_Binop)( dst,
2813 Cbinop_Add,
2814 ML_(CfiExpr_DwReg)( dst, ctxs->cfa_reg ),
2815 ML_(CfiExpr_Const)( dst, (UWord)(Word)ctxs->cfa_off )
2816 );
2817 PUSH(ix);
2818 } else {
2819 /* CFA is already an expr; use its root node */
2820 PUSH(ctxs->cfa_expr_ix);
2821 }
2822 }
2823
2824 while (True) {
2825
2826 vg_assert(sp >= -1 && sp < N_EXPR_STACK);
2827
2828 if (ML_(cur_cmpGT)(expr, limit)) /* "expr > limit" */
2829 return -1; /* overrun - something's wrong */
2830
2831 if (ML_(cur_cmpEQ)(expr, limit)) { /* "expr == limit" */
2832 /* end of expr - return expr on the top of stack. */
2833 if (sp == -1)
2834 return -1; /* stack empty. Bad. */
2835 else
2836 break;
2837 }
2838
2839 uop = 0; bop = 0; opname = NULL; /* excessively conservative */
2840
2841 opcode = ML_(cur_step_UChar)(&expr);
2842 switch (opcode) {
2843
2844 case DW_OP_lit0 ... DW_OP_lit31:
2845 /* push: literal 0 .. 31 */
2846 sw = (Word)opcode - (Word)DW_OP_lit0;
2847 vg_assert(sw >= 0 && sw <= 31);
2848 PUSH( ML_(CfiExpr_Const)( dst, (UWord)sw ) );
2849 if (ddump_frames)
2850 VG_(printf)("DW_OP_lit%ld", sw);
2851 break;
2852
2853 case DW_OP_breg0 ... DW_OP_breg31:
2854 /* push: reg + sleb128 */
2855 reg = (Int)opcode - (Int)DW_OP_breg0;
2856 vg_assert(reg >= 0 && reg <= 31);
2857 sw = step_leb128S( &expr );
2858 ix = ML_(CfiExpr_Binop)( dst,
2859 Cbinop_Add,
2860 ML_(CfiExpr_DwReg)( dst, reg ),
2861 ML_(CfiExpr_Const)( dst, (UWord)sw )
2862 );
2863 PUSH(ix);
2864 if (ddump_frames)
2865 VG_(printf)("DW_OP_breg%d: %ld", reg, sw);
2866 break;
2867
2868 case DW_OP_reg0 ... DW_OP_reg31:
2869 /* push: reg */
2870 reg = (Int)opcode - (Int)DW_OP_reg0;
2871 vg_assert(reg >= 0 && reg <= 31);
2872 ix = ML_(CfiExpr_DwReg)( dst, reg );
2873 PUSH(ix);
2874 if (ddump_frames)
2875 VG_(printf)("DW_OP_reg%d", reg);
2876 break;
2877
2878 case DW_OP_plus_uconst:
2879 uw = step_leb128U( &expr );
2880 PUSH( ML_(CfiExpr_Const)( dst, uw ) );
2881 POP( ix );
2882 POP( ix2 );
2883 PUSH( ML_(CfiExpr_Binop)( dst, Cbinop_Add, ix2, ix ) );
2884 if (ddump_frames)
2885 VG_(printf)("DW_OP_plus_uconst: %lu", uw);
2886 break;
2887
2888 case DW_OP_const4s:
2889 /* push: 32-bit signed immediate */
2890 sw = step_le_s_encoded_literal( &expr, 4 );
2891 PUSH( ML_(CfiExpr_Const)( dst, (UWord)sw ) );
2892 if (ddump_frames)
2893 VG_(printf)("DW_OP_const4s: %ld", sw);
2894 break;
2895
2896 case DW_OP_const2s:
2897 /* push: 16-bit signed immediate */
2898 sw = step_le_s_encoded_literal( &expr, 2 );
2899 PUSH( ML_(CfiExpr_Const)( dst, (UWord)sw ) );
2900 if (ddump_frames)
2901 VG_(printf)("DW_OP_const2s: %ld", sw);
2902 break;
2903
2904 case DW_OP_const1s:
2905 /* push: 8-bit signed immediate */
2906 sw = step_le_s_encoded_literal( &expr, 1 );
2907 PUSH( ML_(CfiExpr_Const)( dst, (UWord)sw ) );
2908 if (ddump_frames)
2909 VG_(printf)("DW_OP_const1s: %ld", sw);
2910 break;
2911
2912 case DW_OP_const1u:
2913 /* push: 8-bit unsigned immediate */
2914 uw = step_le_u_encoded_literal( &expr, 1 );
2915 PUSH( ML_(CfiExpr_Const)( dst, uw ) );
2916 if (ddump_frames)
2917 VG_(printf)("DW_OP_const1: %lu", uw);
2918 break;
2919
2920 case DW_OP_const2u:
2921 /* push: 16-bit unsigned immediate */
2922 uw = step_le_u_encoded_literal( &expr, 2 );
2923 PUSH( ML_(CfiExpr_Const)( dst, uw ) );
2924 if (ddump_frames)
2925 VG_(printf)("DW_OP_const2: %lu", uw);
2926 break;
2927
2928 case DW_OP_const4u:
2929 /* push: 32-bit unsigned immediate */
2930 uw = step_le_u_encoded_literal( &expr, 4 );
2931 PUSH( ML_(CfiExpr_Const)( dst, uw ) );
2932 if (ddump_frames)
2933 VG_(printf)("DW_OP_const4: %lu", uw);
2934 break;
2935
2936 case DW_OP_abs:
2937 uop = Cunop_Abs; opname = "abs"; goto unop;
2938 case DW_OP_neg:
2939 uop = Cunop_Neg; opname = "neg"; goto unop;
2940 case DW_OP_not:
2941 uop = Cunop_Not; opname = "not"; goto unop;
2942 unop:
2943 POP( ix );
2944 PUSH( ML_(CfiExpr_Unop)( dst, uop, ix ) );
2945 if (ddump_frames)
2946 VG_(printf)("DW_OP_%s", opname);
2947 break;
2948
2949 case DW_OP_minus:
2950 bop = Cbinop_Sub; opname = "minus"; goto binop;
2951 case DW_OP_plus:
2952 bop = Cbinop_Add; opname = "plus"; goto binop;
2953 case DW_OP_and:
2954 bop = Cbinop_And; opname = "and"; goto binop;
2955 case DW_OP_mul:
2956 bop = Cbinop_Mul; opname = "mul"; goto binop;
2957 case DW_OP_shl:
2958 bop = Cbinop_Shl; opname = "shl"; goto binop;
2959 case DW_OP_shr:
2960 bop = Cbinop_Shr; opname = "shr"; goto binop;
2961 case DW_OP_eq:
2962 bop = Cbinop_Eq; opname = "eq"; goto binop;
2963 case DW_OP_ge:
2964 bop = Cbinop_Ge; opname = "ge"; goto binop;
2965 case DW_OP_gt:
2966 bop = Cbinop_Gt; opname = "gt"; goto binop;
2967 case DW_OP_le:
2968 bop = Cbinop_Le; opname = "le"; goto binop;
2969 case DW_OP_lt:
2970 bop = Cbinop_Lt; opname = "lt"; goto binop;
2971 case DW_OP_ne:
2972 bop = Cbinop_Ne; opname = "ne"; goto binop;
2973 binop:
2974 POP( ix );
2975 POP( ix2 );
2976 PUSH( ML_(CfiExpr_Binop)( dst, bop, ix2, ix ) );
2977 if (ddump_frames)
2978 VG_(printf)("DW_OP_%s", opname);
2979 break;
2980
2981 case DW_OP_deref:
2982 POP( ix );
2983 PUSH( ML_(CfiExpr_Deref)( dst, ix ) );
2984 if (ddump_frames)
2985 VG_(printf)("DW_OP_deref");
2986 break;
2987
2988 default:
2989 if (!VG_(clo_xml))
2990 VG_(message)(Vg_DebugMsg,
2991 "Warning: DWARF2 CFI reader: unhandled DW_OP_ "
2992 "opcode 0x%x\n", (Int)opcode);
2993 return -1;
2994 }
2995
2996 if (ML_(cur_cmpLT)(expr, limit) && ddump_frames)
2997 VG_(printf)("; ");
2998
2999 }
3000
3001 vg_assert(sp >= -1 && sp < N_EXPR_STACK);
3002 if (sp == -1)
3003 return -1;
3004
3005 if (0 && ddump_frames)
3006 ML_(ppCfiExpr)( dst, stack[sp] );
3007 return stack[sp];
3008
3009 # undef POP
3010 # undef PUSH
3011 # undef N_EXPR_STACK
3012 }
3013
3014
3015 /* ------------ Run/show CFI instructions ------------ */
3016
3017 /* Run a CFI instruction, and also return its length.
3018 Returns 0 if the instruction could not be executed.
3019 */
run_CF_instruction(UnwindContext * ctx,DiCursor instrIN,UnwindContext * restore_ctx,AddressDecodingInfo * adi,struct _DebugInfo * di)3020 static Int run_CF_instruction ( /*MOD*/UnwindContext* ctx,
3021 DiCursor instrIN,
3022 UnwindContext* restore_ctx,
3023 AddressDecodingInfo* adi,
3024 struct _DebugInfo* di )
3025 {
3026 Int off, reg, reg2, len, j;
3027 UInt delta;
3028 Addr printing_bias = ((Addr)ctx->initloc) - ((Addr)di->text_bias);
3029 struct UnwindContextState* ctxs;
3030
3031 DiCursor instr = instrIN;
3032 UChar instr_0 = ML_(cur_step_UChar)(&instr);
3033 UChar hi2 = (instr_0 >> 6) & 3;
3034 UChar lo6 = instr_0 & 0x3F;
3035
3036 if (ctx->state_sp < 0 || ctx->state_sp >= N_RR_STACK)
3037 return 0; /* bogus reg-rule stack pointer */
3038
3039 ctxs = &ctx->state[ctx->state_sp];
3040 if (hi2 == DW_CFA_advance_loc) {
3041 delta = (UInt)lo6;
3042 delta *= ctx->code_a_f;
3043 ctx->loc += delta;
3044 if (di->ddump_frames)
3045 VG_(printf)(" DW_CFA_advance_loc: %d to %08lx\n",
3046 (Int)delta, (Addr)ctx->loc + printing_bias);
3047 return ML_(cur_minus)(instr, instrIN);
3048 }
3049
3050 if (hi2 == DW_CFA_offset) {
3051 /* Set rule for reg 'lo6' to CFAOff(off * data_af) */
3052 off = step_leb128( &instr, 0 );
3053 reg = (Int)lo6;
3054 if (reg < 0 || reg >= N_CFI_REGS)
3055 return 0; /* fail */
3056 ctxs->reg[reg].tag = RR_CFAOff;
3057 ctxs->reg[reg].arg = off * ctx->data_a_f;
3058 if (di->ddump_frames)
3059 VG_(printf)(" DW_CFA_offset: r%d at cfa%s%d\n",
3060 (Int)reg,
3061 ctxs->reg[reg].arg < 0 ? "" : "+",
3062 (Int)ctxs->reg[reg].arg );
3063 return ML_(cur_minus)(instr, instrIN);
3064 }
3065
3066 if (hi2 == DW_CFA_restore) {
3067 reg = (Int)lo6;
3068 if (reg < 0 || reg >= N_CFI_REGS)
3069 return 0; /* fail */
3070 if (restore_ctx == NULL)
3071 return 0; /* fail */
3072 ctxs->reg[reg] = restore_ctx->state[restore_ctx->state_sp].reg[reg];
3073 if (di->ddump_frames)
3074 VG_(printf)(" DW_CFA_restore: r%d\n", (Int)reg);
3075 return ML_(cur_minus)(instr, instrIN);
3076 }
3077
3078 vg_assert(hi2 == DW_CFA_use_secondary);
3079
3080 switch (lo6) {
3081 case DW_CFA_nop:
3082 if (di->ddump_frames)
3083 VG_(printf)(" DW_CFA_nop\n");
3084 break;
3085 case DW_CFA_set_loc:
3086 /* WAS:
3087 ctx->loc = read_Addr(&instr[i]) - ctx->initloc; i+= sizeof(Addr);
3088 Was this ever right? */
3089 /* 2007 Feb 23: No. binutils/dwarf.c treats it as an encoded
3090 address and that appears to be in accordance with the
3091 DWARF3 spec. */
3092 ctx->loc = step_encoded_Addr(adi, &instr);
3093 if (di->ddump_frames)
3094 VG_(printf)(" rci:DW_CFA_set_loc\n");
3095 break;
3096 case DW_CFA_advance_loc1:
3097 delta = (UInt)ML_(cur_step_UChar)(&instr);
3098 delta *= ctx->code_a_f;
3099 ctx->loc += delta;
3100 if (di->ddump_frames)
3101 VG_(printf)(" DW_CFA_advance_loc1: %d to %08lx\n",
3102 (Int)delta, (Addr)ctx->loc + printing_bias);
3103 break;
3104 case DW_CFA_advance_loc2:
3105 delta = (UInt)ML_(cur_step_UShort)(&instr);
3106 delta *= ctx->code_a_f;
3107 ctx->loc += delta;
3108 if (di->ddump_frames)
3109 VG_(printf)(" DW_CFA_advance_loc2: %d to %08lx\n",
3110 (Int)delta, (Addr)ctx->loc + printing_bias);
3111 break;
3112 case DW_CFA_advance_loc4:
3113 delta = (UInt)ML_(cur_step_UInt)(&instr);
3114 delta *= ctx->code_a_f;
3115 ctx->loc += delta;
3116 if (di->ddump_frames)
3117 VG_(printf)(" DW_CFA_advance_loc4: %d to %08lx\n",
3118 (Int)delta, (Addr)ctx->loc + printing_bias);
3119 break;
3120
3121 case DW_CFA_def_cfa:
3122 reg = step_leb128( &instr, 0 );
3123 off = step_leb128( &instr, 0 );
3124 if (reg < 0 || reg >= N_CFI_REGS)
3125 return 0; /* fail */
3126 ctxs->cfa_is_regoff = True;
3127 ctxs->cfa_expr_ix = 0;
3128 ctxs->cfa_reg = reg;
3129 ctxs->cfa_off = off;
3130 if (di->ddump_frames)
3131 VG_(printf)(" DW_CFA_def_cfa: r%d ofs %d\n", (Int)reg, (Int)off);
3132 break;
3133
3134 case DW_CFA_def_cfa_sf:
3135 reg = step_leb128( &instr, 0 );
3136 off = step_leb128( &instr, 1 );
3137 if (reg < 0 || reg >= N_CFI_REGS)
3138 return 0; /* fail */
3139 ctxs->cfa_is_regoff = True;
3140 ctxs->cfa_expr_ix = 0;
3141 ctxs->cfa_reg = reg;
3142 ctxs->cfa_off = off * ctx->data_a_f;
3143 if (di->ddump_frames)
3144 VG_(printf)(" rci:DW_CFA_def_cfa_sf\n");
3145 break;
3146
3147 case DW_CFA_register:
3148 reg = step_leb128( &instr, 0 );
3149 reg2 = step_leb128( &instr, 0 );
3150 if (reg < 0 || reg >= N_CFI_REGS)
3151 return 0; /* fail */
3152 if (reg2 < 0 || reg2 >= N_CFI_REGS)
3153 return 0; /* fail */
3154 ctxs->reg[reg].tag = RR_Reg;
3155 ctxs->reg[reg].arg = reg2;
3156 if (di->ddump_frames)
3157 VG_(printf)(" DW_CFA_register: r%d in r%d\n",
3158 (Int)reg, (Int)reg2);
3159 break;
3160
3161 case DW_CFA_offset_extended:
3162 reg = step_leb128( &instr, 0 );
3163 off = step_leb128( &instr, 0 );
3164 if (reg < 0 || reg >= N_CFI_REGS)
3165 return 0; /* fail */
3166 ctxs->reg[reg].tag = RR_CFAOff;
3167 ctxs->reg[reg].arg = off * ctx->data_a_f;
3168 if (di->ddump_frames)
3169 VG_(printf)(" rci:DW_CFA_offset_extended\n");
3170 break;
3171
3172 case DW_CFA_offset_extended_sf:
3173 reg = step_leb128( &instr, 0 );
3174 off = step_leb128( &instr, 1 );
3175 if (reg < 0 || reg >= N_CFI_REGS)
3176 return 0; /* fail */
3177 ctxs->reg[reg].tag = RR_CFAOff;
3178 ctxs->reg[reg].arg = off * ctx->data_a_f;
3179 if (di->ddump_frames)
3180 VG_(printf)(" DW_CFA_offset_extended_sf: r%d at cfa%s%d\n",
3181 reg,
3182 ctxs->reg[reg].arg < 0 ? "" : "+",
3183 (Int)ctxs->reg[reg].arg);
3184 break;
3185
3186 case DW_CFA_GNU_negative_offset_extended:
3187 reg = step_leb128( &instr, 0 );
3188 off = step_leb128( &instr, 0 );
3189 if (reg < 0 || reg >= N_CFI_REGS)
3190 return 0; /* fail */
3191 ctxs->reg[reg].tag = RR_CFAOff;
3192 ctxs->reg[reg].arg = (-off) * ctx->data_a_f;
3193 if (di->ddump_frames)
3194 VG_(printf)(" rci:DW_CFA_GNU_negative_offset_extended\n");
3195 break;
3196
3197 case DW_CFA_restore_extended:
3198 reg = step_leb128( &instr, 0 );
3199 if (reg < 0 || reg >= N_CFI_REGS)
3200 return 0; /* fail */
3201 if (restore_ctx == NULL)
3202 return 0; /* fail */
3203 ctxs->reg[reg] = restore_ctx->state[restore_ctx->state_sp].reg[reg];
3204 if (di->ddump_frames)
3205 VG_(printf)(" rci:DW_CFA_restore_extended\n");
3206 break;
3207
3208 case DW_CFA_val_offset:
3209 reg = step_leb128( &instr, 0 );
3210 off = step_leb128( &instr, 0 );
3211 if (reg < 0 || reg >= N_CFI_REGS)
3212 return 0; /* fail */
3213 ctxs->reg[reg].tag = RR_CFAValOff;
3214 ctxs->reg[reg].arg = off * ctx->data_a_f;
3215 if (di->ddump_frames)
3216 VG_(printf)(" rci:DW_CFA_val_offset\n");
3217 break;
3218
3219 case DW_CFA_val_offset_sf:
3220 reg = step_leb128( &instr, 0 );
3221 off = step_leb128( &instr, 1 );
3222 if (reg < 0 || reg >= N_CFI_REGS)
3223 return 0; /* fail */
3224 ctxs->reg[reg].tag = RR_CFAValOff;
3225 ctxs->reg[reg].arg = off * ctx->data_a_f;
3226 if (di->ddump_frames)
3227 VG_(printf)(" rci:DW_CFA_val_offset_sf\n");
3228 break;
3229
3230 case DW_CFA_def_cfa_register:
3231 reg = step_leb128( &instr, 0);
3232 if (reg < 0 || reg >= N_CFI_REGS)
3233 return 0; /* fail */
3234 ctxs->cfa_is_regoff = True;
3235 ctxs->cfa_expr_ix = 0;
3236 ctxs->cfa_reg = reg;
3237 /* ->cfa_off unchanged */
3238 if (di->ddump_frames)
3239 VG_(printf)(" DW_CFA_def_cfa_register: r%d\n", (Int)reg );
3240 break;
3241
3242 case DW_CFA_def_cfa_offset:
3243 off = step_leb128( &instr, 0);
3244 ctxs->cfa_is_regoff = True;
3245 ctxs->cfa_expr_ix = 0;
3246 /* ->reg is unchanged */
3247 ctxs->cfa_off = off;
3248 if (di->ddump_frames)
3249 VG_(printf)(" DW_CFA_def_cfa_offset: %d\n", (Int)off);
3250 break;
3251
3252 case DW_CFA_def_cfa_offset_sf:
3253 off = step_leb128( &instr, 1);
3254 ctxs->cfa_is_regoff = True;
3255 ctxs->cfa_expr_ix = 0;
3256 /* ->reg is unchanged */
3257 ctxs->cfa_off = off * ctx->data_a_f;
3258 if (di->ddump_frames)
3259 VG_(printf)(" DW_CFA_def_cfa_offset_sf: %d\n", ctxs->cfa_off);
3260 break;
3261
3262 case DW_CFA_undefined:
3263 reg = step_leb128( &instr, 0);
3264 if (reg < 0 || reg >= N_CFI_REGS)
3265 return 0; /* fail */
3266 ctxs->reg[reg].tag = RR_Undef;
3267 ctxs->reg[reg].arg = 0;
3268 if (di->ddump_frames)
3269 VG_(printf)(" rci:DW_CFA_undefined\n");
3270 break;
3271
3272 case DW_CFA_same_value:
3273 reg = step_leb128( &instr, 0);
3274 if (reg < 0 || reg >= N_CFI_REGS)
3275 return 0; /* fail */
3276 ctxs->reg[reg].tag = RR_Same;
3277 ctxs->reg[reg].arg = 0;
3278 if (di->ddump_frames)
3279 VG_(printf)(" rci:DW_CFA_same_value\n");
3280 break;
3281
3282 case DW_CFA_GNU_args_size:
3283 /* No idea what is supposed to happen. gdb-6.3 simply
3284 ignores these. */
3285 /*off = */ (void)step_leb128( &instr, 0 );
3286 if (di->ddump_frames)
3287 VG_(printf)(" rci:DW_CFA_GNU_args_size (ignored)\n");
3288 break;
3289
3290 case DW_CFA_expression: {
3291 /* Identical to DW_CFA_val_expression except that the value
3292 computed is an address and so needs one final
3293 dereference. */
3294 DiCursor expr;
3295 reg = step_leb128( &instr, 0 );
3296 len = step_leb128( &instr, 0 );
3297 expr = instr;
3298 instr = ML_(cur_plus)(instr, len);
3299 if (reg < 0 || reg >= N_CFI_REGS)
3300 return 0; /* fail */
3301 if (di->ddump_frames)
3302 VG_(printf)(" DW_CFA_expression: r%d (",
3303 (Int)reg);
3304 /* Convert the expression into a dag rooted at ctx->exprs index j,
3305 or fail. */
3306 j = dwarfexpr_to_dag ( ctx, expr, len, True/*push CFA at start*/,
3307 di->ddump_frames);
3308 if (di->ddump_frames)
3309 VG_(printf)(")\n");
3310 vg_assert(j >= -1);
3311 if (j >= 0) {
3312 vg_assert(ctx->exprs);
3313 vg_assert( j < VG_(sizeXA)(ctx->exprs) );
3314 }
3315 if (j == -1)
3316 return 0; /* fail */
3317 /* Add an extra dereference */
3318 j = ML_(CfiExpr_Deref)( ctx->exprs, j );
3319 ctxs->reg[reg].tag = RR_ValExpr;
3320 ctxs->reg[reg].arg = j;
3321 break;
3322 }
3323
3324 case DW_CFA_val_expression: {
3325 DiCursor expr;
3326 reg = step_leb128( &instr, 0 );
3327 len = step_leb128( &instr, 0 );
3328 expr = instr;
3329 instr = ML_(cur_plus)(instr, len);
3330 if (reg < 0 || reg >= N_CFI_REGS)
3331 return 0; /* fail */
3332 if (di->ddump_frames)
3333 VG_(printf)(" DW_CFA_val_expression: r%d (",
3334 (Int)reg);
3335 /* Convert the expression into a dag rooted at ctx->exprs index j,
3336 or fail. */
3337 j = dwarfexpr_to_dag ( ctx, expr, len, True/*push CFA at start*/,
3338 di->ddump_frames);
3339 if (di->ddump_frames)
3340 VG_(printf)(")\n");
3341 vg_assert(j >= -1);
3342 if (j >= 0) {
3343 vg_assert(ctx->exprs);
3344 vg_assert( j < VG_(sizeXA)(ctx->exprs) );
3345 }
3346 if (j == -1)
3347 return 0; /* fail */
3348 ctxs->reg[reg].tag = RR_ValExpr;
3349 ctxs->reg[reg].arg = j;
3350 break;
3351 }
3352
3353 case DW_CFA_def_cfa_expression: {
3354 DiCursor expr;
3355 len = step_leb128( &instr, 0 );
3356 expr = instr;
3357 instr = ML_(cur_plus)(instr, len);
3358 if (di->ddump_frames)
3359 VG_(printf)(" DW_CFA_def_cfa_expression (");
3360 /* Convert the expression into a dag rooted at ctx->exprs index j,
3361 or fail. */
3362 j = dwarfexpr_to_dag ( ctx, expr, len, True/*push CFA at start*/,
3363 di->ddump_frames);
3364 if (di->ddump_frames)
3365 VG_(printf)(")\n");
3366 ctxs->cfa_is_regoff = False;
3367 ctxs->cfa_reg = 0;
3368 ctxs->cfa_off = 0;
3369 ctxs->cfa_expr_ix = j;
3370 break;
3371 }
3372
3373 case DW_CFA_GNU_window_save:
3374 /* Ignored. This appears to be sparc-specific; quite why it
3375 turns up in SuSE-supplied x86 .so's beats me. */
3376 if (di->ddump_frames)
3377 VG_(printf)(" DW_CFA_GNU_window_save\n");
3378 break;
3379
3380 case DW_CFA_remember_state:
3381 if (di->ddump_frames)
3382 VG_(printf)(" DW_CFA_remember_state\n");
3383 /* we just checked this at entry, so: */
3384 vg_assert(ctx->state_sp >= 0 && ctx->state_sp < N_RR_STACK);
3385 ctx->state_sp++;
3386 if (ctx->state_sp == N_RR_STACK) {
3387 /* stack overflow. We're hosed. */
3388 VG_(message)(Vg_DebugMsg, "DWARF2 CFI reader: N_RR_STACK is "
3389 "too low; increase and recompile.");
3390 return 0; /* indicate failure */
3391 } else {
3392 VG_(memcpy)(/*dst*/&ctx->state[ctx->state_sp],
3393 /*src*/&ctx->state[ctx->state_sp - 1],
3394 sizeof(ctx->state[ctx->state_sp]) );
3395 }
3396 break;
3397
3398 case DW_CFA_restore_state:
3399 if (di->ddump_frames)
3400 VG_(printf)(" DW_CFA_restore_state\n");
3401 /* we just checked this at entry, so: */
3402 vg_assert(ctx->state_sp >= 0 && ctx->state_sp < N_RR_STACK);
3403 if (ctx->state_sp == 0) {
3404 /* stack undefflow. Give up. */
3405 return 0; /* indicate failure */
3406 } else {
3407 /* simply fall back to previous entry */
3408 ctx->state_sp--;
3409 }
3410 break;
3411
3412 default:
3413 VG_(message)(Vg_DebugMsg, "DWARF2 CFI reader: unhandled CFI "
3414 "instruction 0:%d\n", (Int)lo6);
3415 if (di->ddump_frames)
3416 VG_(printf)(" rci:run_CF_instruction:default\n");
3417 return 0; /* failure */
3418 /*NOTREACHED*/
3419 }
3420
3421 return ML_(cur_minus)(instr, instrIN);
3422 }
3423
3424
3425 /* Show a CFI instruction, and also return its length. Show it as
3426 close as possible (preferably identical) to how GNU binutils
3427 readelf --debug-dump=frames would. */
3428
show_CF_instruction(DiCursor instrIN,AddressDecodingInfo * adi,Int code_a_f,Int data_a_f)3429 static Int show_CF_instruction ( DiCursor instrIN,
3430 AddressDecodingInfo* adi,
3431 Int code_a_f, Int data_a_f )
3432 {
3433 Int off, coff, reg, reg2, len;
3434 UInt delta;
3435 Addr loc;
3436 DiCursor instr = instrIN;
3437 UChar instr_0 = ML_(cur_step_UChar)(&instr);
3438 UChar hi2 = (instr_0 >> 6) & 3;
3439 UChar lo6 = instr_0 & 0x3F;
3440
3441 if (0) {
3442 DiCursor tmpi = instrIN;
3443 UInt i_0 = ML_(cur_step_UChar)(&tmpi);
3444 UInt i_1 = ML_(cur_step_UChar)(&tmpi);
3445 UInt i_2 = ML_(cur_step_UChar)(&tmpi);
3446 UInt i_3 = ML_(cur_step_UChar)(&tmpi);
3447 UInt i_4 = ML_(cur_step_UChar)(&tmpi);
3448 UInt i_5 = ML_(cur_step_UChar)(&tmpi);
3449 UInt i_6 = ML_(cur_step_UChar)(&tmpi);
3450 UInt i_7 = ML_(cur_step_UChar)(&tmpi);
3451 VG_(printf)("raw:%x/%x:%x:%x:%x:%x:%x:%x:%x:%x\n",
3452 hi2, lo6, i_0, i_1, i_2, i_3, i_4, i_5, i_6, i_7);
3453 }
3454
3455 if (hi2 == DW_CFA_advance_loc) {
3456 VG_(printf)(" sci:DW_CFA_advance_loc(%d)\n", (Int)lo6);
3457 return ML_(cur_minus)(instr, instrIN);
3458 }
3459
3460 if (hi2 == DW_CFA_offset) {
3461 off = step_leb128( &instr, 0 );
3462 coff = off * data_a_f;
3463 VG_(printf)(" DW_CFA_offset: r%d at cfa%s%d\n",
3464 (Int)lo6, coff < 0 ? "" : "+", (Int)coff );
3465 return ML_(cur_minus)(instr, instrIN);
3466 }
3467
3468 if (hi2 == DW_CFA_restore) {
3469 VG_(printf)(" sci:DW_CFA_restore(r%d)\n", (Int)lo6);
3470 return ML_(cur_minus)(instr, instrIN);
3471 }
3472
3473 vg_assert(hi2 == DW_CFA_use_secondary);
3474
3475 switch (lo6) {
3476
3477 case DW_CFA_nop:
3478 VG_(printf)(" DW_CFA_nop\n");
3479 break;
3480
3481 case DW_CFA_set_loc:
3482 /* WAS: loc = read_Addr(&instr[i]); i+= sizeof(Addr);
3483 (now known to be incorrect -- the address is encoded) */
3484 loc = step_encoded_Addr(adi, &instr);
3485 VG_(printf)(" sci:DW_CFA_set_loc(%#lx)\n", loc);
3486 break;
3487
3488 case DW_CFA_advance_loc1:
3489 delta = (UInt)ML_(cur_step_UChar)(&instr);
3490 VG_(printf)(" sci:DW_CFA_advance_loc1(%d)\n", delta);
3491 break;
3492
3493 case DW_CFA_advance_loc2:
3494 delta = (UInt)ML_(cur_step_UShort)(&instr);
3495 VG_(printf)(" sci:DW_CFA_advance_loc2(%d)\n", delta);
3496 break;
3497
3498 case DW_CFA_advance_loc4:
3499 delta = (UInt)ML_(cur_step_UInt)(&instr);
3500 VG_(printf)(" DW_CFA_advance_loc4(%d)\n", delta);
3501 break;
3502
3503 case DW_CFA_def_cfa:
3504 reg = step_leb128( &instr, 0 );
3505 off = step_leb128( &instr, 0 );
3506 VG_(printf)(" DW_CFA_def_cfa: r%d ofs %d\n", (Int)reg, (Int)off);
3507 break;
3508
3509 case DW_CFA_def_cfa_sf:
3510 reg = step_leb128( &instr, 0 );
3511 off = step_leb128( &instr, 1 );
3512 VG_(printf)(" DW_CFA_def_cfa_sf: r%d ofs %d\n",
3513 (Int)reg, (Int)(off * data_a_f));
3514 break;
3515
3516 case DW_CFA_register:
3517 reg = step_leb128( &instr, 0);
3518 reg2 = step_leb128( &instr, 0);
3519 VG_(printf)(" sci:DW_CFA_register(r%d, r%d)\n", reg, reg2);
3520 break;
3521
3522 case DW_CFA_def_cfa_register:
3523 reg = step_leb128( &instr, 0);
3524 VG_(printf)(" sci:DW_CFA_def_cfa_register(r%d)\n", reg);
3525 break;
3526
3527 case DW_CFA_def_cfa_offset:
3528 off = step_leb128( &instr, 0);
3529 VG_(printf)(" sci:DW_CFA_def_cfa_offset(%d)\n", off);
3530 break;
3531
3532 case DW_CFA_def_cfa_offset_sf:
3533 off = step_leb128( &instr, 1);
3534 VG_(printf)(" sci:DW_CFA_def_cfa_offset_sf(%d)\n", off);
3535 break;
3536
3537 case DW_CFA_restore_extended:
3538 reg = step_leb128( &instr, 0);
3539 VG_(printf)(" sci:DW_CFA_restore_extended(r%d)\n", reg);
3540 break;
3541
3542 case DW_CFA_undefined:
3543 reg = step_leb128( &instr, 0);
3544 VG_(printf)(" sci:DW_CFA_undefined(r%d)\n", reg);
3545 break;
3546
3547 case DW_CFA_same_value:
3548 reg = step_leb128( &instr, 0);
3549 VG_(printf)(" sci:DW_CFA_same_value(r%d)\n", reg);
3550 break;
3551
3552 case DW_CFA_remember_state:
3553 VG_(printf)(" sci:DW_CFA_remember_state\n");
3554 break;
3555
3556 case DW_CFA_restore_state:
3557 VG_(printf)(" sci:DW_CFA_restore_state\n");
3558 break;
3559
3560 case DW_CFA_GNU_args_size:
3561 off = step_leb128( &instr, 0 );
3562 VG_(printf)(" sci:DW_CFA_GNU_args_size(%d)\n", off );
3563 break;
3564
3565 case DW_CFA_def_cfa_expression:
3566 len = step_leb128( &instr, 0 );
3567 instr = ML_(cur_plus)(instr, len);
3568 VG_(printf)(" sci:DW_CFA_def_cfa_expression(length %d)\n", len);
3569 break;
3570
3571 case DW_CFA_expression:
3572 reg = step_leb128( &instr, 0 );
3573 len = step_leb128( &instr, 0 );
3574 instr = ML_(cur_plus)(instr, len);
3575 VG_(printf)(" sci:DW_CFA_expression(r%d, length %d)\n", reg, len);
3576 break;
3577
3578 case DW_CFA_val_expression:
3579 reg = step_leb128( &instr, 0 );
3580 len = step_leb128( &instr, 0 );
3581 instr = ML_(cur_plus)(instr, len);
3582 VG_(printf)(" sci:DW_CFA_val_expression(r%d, length %d)\n", reg, len);
3583 break;
3584
3585 case DW_CFA_offset_extended:
3586 reg = step_leb128( &instr, 0 );
3587 off = step_leb128( &instr, 0 );
3588 VG_(printf)(" sci:DW_CFA_offset_extended(r%d, "
3589 "off %d x data_af)\n", reg, off);
3590 break;
3591
3592 case DW_CFA_offset_extended_sf:
3593 reg = step_leb128( &instr, 0 );
3594 off = step_leb128( &instr, 1 );
3595 coff = (Int)(off * data_a_f);
3596 VG_(printf)(" DW_CFA_offset_extended_sf: r%d at cfa%s%d\n",
3597 reg, coff < 0 ? "" : "+", coff);
3598 break;
3599
3600 case DW_CFA_GNU_negative_offset_extended:
3601 reg = step_leb128( &instr, 0 );
3602 off = step_leb128( &instr, 0 );
3603 VG_(printf)(" sci:DW_CFA_GNU_negative_offset_extended"
3604 "(r%d, off %d x data_af)\n", reg, -off);
3605 break;
3606
3607 case DW_CFA_val_offset:
3608 reg = step_leb128( &instr, 0 );
3609 off = step_leb128( &instr, 0 );
3610 VG_(printf)(" sci:DW_CFA_val_offset(r%d, off %d x data_af)\n",
3611 reg, off);
3612 break;
3613
3614 case DW_CFA_val_offset_sf:
3615 reg = step_leb128( &instr, 0 );
3616 off = step_leb128( &instr, 1 );
3617 VG_(printf)(" sci:DW_CFA_val_offset_sf(r%d, off %d x data_af)\n",
3618 reg, off);
3619 break;
3620
3621 case DW_CFA_GNU_window_save:
3622 VG_(printf)(" sci:DW_CFA_GNU_window_save\n");
3623 break;
3624
3625 default:
3626 VG_(printf)(" sci:0:%d\n", (Int)lo6);
3627 break;
3628 }
3629
3630 return ML_(cur_minus)(instr, instrIN);
3631 }
3632
3633
3634 /* Show the instructions in instrs[0 .. ilen-1]. */
show_CF_instructions(DiCursor instrs,Int ilen,AddressDecodingInfo * adi,Int code_a_f,Int data_a_f)3635 static void show_CF_instructions ( DiCursor instrs, Int ilen,
3636 AddressDecodingInfo* adi,
3637 Int code_a_f, Int data_a_f )
3638 {
3639 Int i = 0;
3640 while (True) {
3641 if (i >= ilen) break;
3642 i += show_CF_instruction( ML_(cur_plus)(instrs, i),
3643 adi, code_a_f, data_a_f );
3644 }
3645 }
3646
3647
3648 /* Run the CF instructions in instrs[0 .. ilen-1], until the end is
3649 reached, or until there is a failure. Return True iff success.
3650 */
3651 static
run_CF_instructions(struct _DebugInfo * di,Bool record,UnwindContext * ctx,DiCursor instrs,Int ilen,UWord fde_arange,UnwindContext * restore_ctx,AddressDecodingInfo * adi)3652 Bool run_CF_instructions ( struct _DebugInfo* di,
3653 Bool record,
3654 UnwindContext* ctx, DiCursor instrs, Int ilen,
3655 UWord fde_arange,
3656 UnwindContext* restore_ctx,
3657 AddressDecodingInfo* adi )
3658 {
3659 DiCfSI cfsi;
3660 Bool summ_ok;
3661 Int j, i = 0;
3662 Addr loc_prev;
3663 if (0) ppUnwindContext(ctx);
3664 if (0) ppUnwindContext_summary(ctx);
3665 while (True) {
3666 loc_prev = ctx->loc;
3667 if (i >= ilen) break;
3668 if (0) (void)show_CF_instruction( ML_(cur_plus)(instrs,i), adi,
3669 ctx->code_a_f, ctx->data_a_f );
3670 j = run_CF_instruction( ctx, ML_(cur_plus)(instrs,i),
3671 restore_ctx, adi, di );
3672 if (j == 0)
3673 return False; /* execution failed */
3674 i += j;
3675 if (0) ppUnwindContext(ctx);
3676 if (record && loc_prev != ctx->loc) {
3677 summ_ok = summarise_context ( &cfsi, loc_prev, ctx, di );
3678 if (summ_ok) {
3679 ML_(addDiCfSI)(di, &cfsi);
3680 if (di->trace_cfi)
3681 ML_(ppDiCfSI)(di->cfsi_exprs, &cfsi);
3682 }
3683 }
3684 }
3685 if (ctx->loc < fde_arange) {
3686 loc_prev = ctx->loc;
3687 ctx->loc = fde_arange;
3688 if (record) {
3689 summ_ok = summarise_context ( &cfsi, loc_prev, ctx, di );
3690 if (summ_ok) {
3691 ML_(addDiCfSI)(di, &cfsi);
3692 if (di->trace_cfi)
3693 ML_(ppDiCfSI)(di->cfsi_exprs, &cfsi);
3694 }
3695 }
3696 }
3697 return True;
3698 }
3699
3700
3701 /* ------------ Main entry point for CFI reading ------------ */
3702
3703 typedef
3704 struct {
3705 /* This gives the CIE an identity to which FDEs will refer. */
3706 ULong offset;
3707 /* Code, data factors. */
3708 Int code_a_f;
3709 Int data_a_f;
3710 /* Return-address pseudo-register. */
3711 Int ra_reg;
3712 UChar address_encoding;
3713 /* Where are the instrs? */
3714 DiCursor instrs;
3715 Int ilen;
3716 /* God knows .. don't ask */
3717 Bool saw_z_augmentation;
3718 }
3719 CIE;
3720
init_CIE(CIE * cie)3721 static void init_CIE ( CIE* cie )
3722 {
3723 cie->offset = 0;
3724 cie->code_a_f = 0;
3725 cie->data_a_f = 0;
3726 cie->ra_reg = 0;
3727 cie->address_encoding = 0;
3728 cie->instrs = DiCursor_INVALID;
3729 cie->ilen = 0;
3730 cie->saw_z_augmentation = False;
3731 }
3732
3733 #define N_CIEs 8000
3734 static CIE the_CIEs[N_CIEs];
3735
3736
3737 /* Read, summarise and store CFA unwind info from .eh_frame and
3738 .debug_frame sections. is_ehframe tells us which kind we are
3739 dealing with -- they are slightly different. */
ML_(read_callframe_info_dwarf3)3740 void ML_(read_callframe_info_dwarf3)
3741 ( /*OUT*/struct _DebugInfo* di,
3742 DiSlice escn_frame, Addr frame_avma, Bool is_ehframe )
3743 {
3744 const HChar* how = NULL;
3745 Int n_CIEs = 0;
3746 DiCursor frame_image = ML_(cur_from_sli)(escn_frame); /* fixed */
3747 DiOffT frame_size = escn_frame.szB;
3748 DiCursor data = frame_image;
3749 UWord cfsi_used_orig;
3750
3751 /* If we're dealing with a .debug_frame, assume zero frame_avma. */
3752 if (!is_ehframe)
3753 vg_assert(frame_avma == 0);
3754
3755 # if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
3756 /* These targets don't use CFI-based stack unwinding. */
3757 return;
3758 # endif
3759
3760 /* If we read more than one .debug_frame or .eh_frame for this
3761 DebugInfo*, the second and subsequent reads should only add FDEs
3762 for address ranges not already covered by the FDEs already
3763 present. To be able to quickly check which address ranges are
3764 already present, any existing records (DiCFSIs) must be sorted,
3765 so we can binary-search them in the code below. We also record
3766 di->cfsi_used so that we know where the boundary is between
3767 existing and new records. */
3768 if (di->cfsi_used > 0) {
3769 ML_(canonicaliseCFI) ( di );
3770 }
3771 cfsi_used_orig = di->cfsi_used;
3772
3773 if (di->trace_cfi) {
3774 VG_(printf)("\n-----------------------------------------------\n");
3775 VG_(printf)("CFI info: szB %lld, _avma %#lx\n",
3776 escn_frame.szB, frame_avma );
3777 VG_(printf)("CFI info: name %s\n", di->fsm.filename );
3778 }
3779
3780 /* Loop over CIEs/FDEs */
3781
3782 /* Conceptually, the frame info is a sequence of FDEs, one for each
3783 function. Inside an FDE is a miniature program for a special
3784 state machine, which, when run, produces the stack-unwinding
3785 info for that function.
3786
3787 Because the FDEs typically have much in common, and because the
3788 DWARF designers appear to have been fanatical about space
3789 saving, the common parts are factored out into so-called CIEs.
3790 That means that what we traverse is a sequence of structs, each
3791 of which is either a FDE (usually) or a CIE (occasionally).
3792 Each FDE has a field indicating which CIE is the one pertaining
3793 to it.
3794
3795 The following loop traverses the sequence. FDEs are dealt with
3796 immediately; once we harvest the useful info in an FDE, it is
3797 then forgotten about. By contrast, CIEs are validated and
3798 dumped into an array, because later FDEs may refer to any
3799 previously-seen CIE.
3800 */
3801 while (True) {
3802 DiCursor ciefde_start;
3803 ULong ciefde_len;
3804 ULong cie_pointer;
3805 Bool dw64;
3806
3807 /* Are we done? */
3808 if (ML_(cur_cmpEQ)(data, ML_(cur_plus)(frame_image, frame_size)))
3809 return;
3810
3811 /* Overshot the end? Means something is wrong */
3812 if (ML_(cur_cmpGT)(data, ML_(cur_plus)(frame_image, frame_size))) {
3813 how = "overran the end of .eh_frame";
3814 goto bad;
3815 }
3816
3817 /* Ok, we must be looking at the start of a new CIE or FDE.
3818 Figure out which it is. */
3819
3820 ciefde_start = data;
3821 if (di->trace_cfi)
3822 VG_(printf)("\ncie/fde.start = (frame_image + 0x%llx)\n",
3823 ML_(cur_minus)(ciefde_start, frame_image));
3824
3825 ciefde_len = (ULong)ML_(cur_step_UInt)(&data);
3826 if (di->trace_cfi)
3827 VG_(printf)("cie/fde.length = %lld\n", ciefde_len);
3828
3829 /* Apparently, if the .length field is zero, we are at the end
3830 of the sequence. This is stated in the Generic Elf
3831 Specification (see comments far above here) and is one of the
3832 places where .eh_frame and .debug_frame data differ. */
3833 if (ciefde_len == 0) {
3834 if (di->ddump_frames)
3835 VG_(printf)("%08llx ZERO terminator\n\n",
3836 ML_(cur_minus)(ciefde_start, frame_image));
3837 return;
3838 }
3839
3840 /* If the .length field is 0xFFFFFFFF then we're dealing with
3841 64-bit DWARF, and the real length is stored as a 64-bit
3842 number immediately following it. */
3843 dw64 = False;
3844 if (ciefde_len == 0xFFFFFFFFUL) {
3845 dw64 = True;
3846 ciefde_len = ML_(cur_step_ULong)(&data);
3847 }
3848
3849 /* Now get the CIE ID, whose size depends on the DWARF 32 vs
3850 64-ness. */
3851 if (dw64) {
3852 /* see XXX below */
3853 cie_pointer = ML_(cur_step_ULong)(&data);
3854 } else {
3855 /* see XXX below */
3856 cie_pointer = (ULong)ML_(cur_step_UInt)(&data);
3857 }
3858
3859 if (di->trace_cfi)
3860 VG_(printf)("cie.pointer = %lld\n", cie_pointer);
3861
3862 /* If cie_pointer is zero for .eh_frame or all ones for .debug_frame,
3863 we've got a CIE; else it's an FDE. */
3864 if (cie_pointer == (is_ehframe ? 0ULL
3865 : dw64 ? 0xFFFFFFFFFFFFFFFFULL : 0xFFFFFFFFULL)) {
3866
3867 Int this_CIE;
3868 UChar cie_version;
3869 DiCursor cie_augmentation;
3870
3871 /* --------- CIE --------- */
3872 if (di->trace_cfi)
3873 VG_(printf)("------ new CIE (#%d of 0 .. %d) ------\n",
3874 n_CIEs, N_CIEs - 1);
3875
3876 /* Allocate a new CIE record. */
3877 vg_assert(n_CIEs >= 0 && n_CIEs <= N_CIEs);
3878 if (n_CIEs == N_CIEs) {
3879 how = "N_CIEs is too low. Increase and recompile.";
3880 goto bad;
3881 }
3882
3883 this_CIE = n_CIEs;
3884 n_CIEs++;
3885 init_CIE( &the_CIEs[this_CIE] );
3886
3887 /* Record its offset. This is how we will find it again
3888 later when looking at an FDE. */
3889 the_CIEs[this_CIE].offset
3890 = (ULong)ML_(cur_minus)(ciefde_start, frame_image);
3891
3892 if (di->ddump_frames)
3893 VG_(printf)("%08lx %08lx %08lx CIE\n",
3894 (Addr)ML_(cur_minus)(ciefde_start, frame_image),
3895 (Addr)ciefde_len,
3896 (Addr)(UWord)cie_pointer );
3897
3898 cie_version = ML_(cur_step_UChar)(&data);
3899 if (di->trace_cfi)
3900 VG_(printf)("cie.version = %d\n", (Int)cie_version);
3901 if (di->ddump_frames)
3902 VG_(printf)(" Version: %d\n", (Int)cie_version);
3903 if (cie_version != 1 && cie_version != 3 && cie_version != 4) {
3904 how = "unexpected CIE version (not 1 nor 3 nor 4)";
3905 goto bad;
3906 }
3907
3908 cie_augmentation = data;
3909 data = ML_(cur_plus)(data, 1 + ML_(cur_strlen)(cie_augmentation));
3910
3911 if (di->trace_cfi || di->ddump_frames) {
3912 HChar* str = ML_(cur_read_strdup)(cie_augmentation, "di.rcid3.1");
3913 if (di->trace_cfi)
3914 VG_(printf)("cie.augment = \"%s\"\n", str);
3915 if (di->ddump_frames)
3916 VG_(printf)(" Augmentation: \"%s\"\n", str);
3917 ML_(dinfo_free)(str);
3918 }
3919
3920 if (ML_(cur_read_UChar)(cie_augmentation) == 'e'
3921 && ML_(cur_read_UChar)
3922 (ML_(cur_plus)(cie_augmentation, 1)) == 'h') {
3923 data = ML_(cur_plus)(data, sizeof(Addr));
3924 cie_augmentation = ML_(cur_plus)(cie_augmentation, 2);
3925 }
3926
3927 if (cie_version >= 4) {
3928 if (ML_(cur_step_UChar)(&data) != sizeof(Addr)) {
3929 how = "unexpected address size";
3930 goto bad;
3931 }
3932 if (ML_(cur_step_UChar)(&data) != 0) {
3933 how = "unexpected non-zero segment size";
3934 goto bad;
3935 }
3936 }
3937
3938 the_CIEs[this_CIE].code_a_f = step_leb128( &data, 0);
3939 if (di->trace_cfi)
3940 VG_(printf)("cie.code_af = %d\n",
3941 the_CIEs[this_CIE].code_a_f);
3942 if (di->ddump_frames)
3943 VG_(printf)(" Code alignment factor: %d\n",
3944 (Int)the_CIEs[this_CIE].code_a_f);
3945
3946 the_CIEs[this_CIE].data_a_f = step_leb128( &data, 1);
3947 if (di->trace_cfi)
3948 VG_(printf)("cie.data_af = %d\n",
3949 the_CIEs[this_CIE].data_a_f);
3950 if (di->ddump_frames)
3951 VG_(printf)(" Data alignment factor: %d\n",
3952 (Int)the_CIEs[this_CIE].data_a_f);
3953
3954 if (cie_version == 1) {
3955 the_CIEs[this_CIE].ra_reg = (Int)ML_(cur_step_UChar)(&data);
3956 } else {
3957 the_CIEs[this_CIE].ra_reg = step_leb128( &data, 0);
3958 }
3959 if (di->trace_cfi)
3960 VG_(printf)("cie.ra_reg = %d\n",
3961 the_CIEs[this_CIE].ra_reg);
3962 if (di->ddump_frames)
3963 VG_(printf)(" Return address column: %d\n",
3964 (Int)the_CIEs[this_CIE].ra_reg);
3965
3966 if (the_CIEs[this_CIE].ra_reg < 0
3967 || the_CIEs[this_CIE].ra_reg >= N_CFI_REGS) {
3968 how = "cie.ra_reg has implausible value";
3969 goto bad;
3970 }
3971
3972 the_CIEs[this_CIE].saw_z_augmentation
3973 = ML_(cur_read_UChar)(cie_augmentation) == 'z';
3974 if (the_CIEs[this_CIE].saw_z_augmentation) {
3975 UInt length = step_leb128( &data, 0);
3976 the_CIEs[this_CIE].instrs = ML_(cur_plus)(data, length);
3977 cie_augmentation = ML_(cur_plus)(cie_augmentation, 1);
3978 if (di->ddump_frames) {
3979 UInt i;
3980 VG_(printf)(" Augmentation data: ");
3981 for (i = 0; i < length; i++)
3982 VG_(printf)(" %02x", (UInt)ML_(cur_read_UChar)
3983 (ML_(cur_plus)(data, i)));
3984 VG_(printf)("\n");
3985 }
3986 } else {
3987 the_CIEs[this_CIE].instrs = DiCursor_INVALID;
3988 }
3989
3990 the_CIEs[this_CIE].address_encoding = default_Addr_encoding();
3991
3992 while (ML_(cur_read_UChar)(cie_augmentation)) {
3993 switch (ML_(cur_read_UChar)(cie_augmentation)) {
3994 case 'L':
3995 data = ML_(cur_plus)(data, 1);
3996 cie_augmentation = ML_(cur_plus)(cie_augmentation, 1);
3997 break;
3998 case 'R':
3999 the_CIEs[this_CIE].address_encoding
4000 = ML_(cur_step_UChar)(&data);
4001 cie_augmentation = ML_(cur_plus)(cie_augmentation, 1);
4002 break;
4003 case 'P':
4004 data = ML_(cur_plus)(data, size_of_encoded_Addr(
4005 ML_(cur_read_UChar)(data) ));
4006 data = ML_(cur_plus)(data, 1);
4007 cie_augmentation = ML_(cur_plus)(cie_augmentation, 1);
4008 break;
4009 case 'S':
4010 cie_augmentation = ML_(cur_plus)(cie_augmentation, 1);
4011 break;
4012 default:
4013 if (!ML_(cur_is_valid)(the_CIEs[this_CIE].instrs)) {
4014 how = "unhandled cie.augmentation";
4015 goto bad;
4016 }
4017 data = the_CIEs[this_CIE].instrs;
4018 goto done_augmentation;
4019 }
4020 }
4021
4022 done_augmentation:
4023
4024 if (di->trace_cfi)
4025 VG_(printf)("cie.encoding = 0x%x\n",
4026 the_CIEs[this_CIE].address_encoding);
4027
4028 the_CIEs[this_CIE].instrs = data;
4029 the_CIEs[this_CIE].ilen = ML_(cur_minus)(ciefde_start, data)
4030 + (Long)ciefde_len + (Long)sizeof(UInt);
4031 if (di->trace_cfi) {
4032 //VG_(printf)("cie.instrs = %p\n", the_CIEs[this_CIE].instrs);
4033 VG_(printf)("cie.ilen = %d\n", the_CIEs[this_CIE].ilen);
4034 }
4035
4036 if (the_CIEs[this_CIE].ilen < 0
4037 || the_CIEs[this_CIE].ilen > frame_size) {
4038 how = "implausible # cie initial insns";
4039 goto bad;
4040 }
4041
4042 data = ML_(cur_plus)(data, the_CIEs[this_CIE].ilen);
4043
4044 /* Show the CIE's instructions (the preamble for each FDE
4045 that uses this CIE). */
4046 if (di->ddump_frames)
4047 VG_(printf)("\n");
4048
4049 if (di->trace_cfi || di->ddump_frames) {
4050 AddressDecodingInfo adi;
4051 adi.encoding = the_CIEs[this_CIE].address_encoding;
4052 adi.ehframe_image = frame_image;
4053 adi.ehframe_avma = frame_avma;
4054 adi.text_bias = di->text_debug_bias;
4055 show_CF_instructions( the_CIEs[this_CIE].instrs,
4056 the_CIEs[this_CIE].ilen, &adi,
4057 the_CIEs[this_CIE].code_a_f,
4058 the_CIEs[this_CIE].data_a_f );
4059 }
4060
4061 if (di->ddump_frames)
4062 VG_(printf)("\n");
4063
4064 } else {
4065
4066 AddressDecodingInfo adi;
4067 UnwindContext ctx, restore_ctx;
4068 Int cie;
4069 ULong look_for;
4070 Bool ok;
4071 Addr fde_initloc;
4072 UWord fde_arange;
4073 DiCursor fde_instrs;
4074 Int fde_ilen;
4075
4076 /* --------- FDE --------- */
4077
4078 /* Find the relevant CIE. The CIE we want is located
4079 cie_pointer bytes back from here. */
4080
4081 /* re sizeof(UInt) / sizeof(ULong), matches XXX above. */
4082 if (is_ehframe)
4083 look_for = ML_(cur_minus)(data, frame_image)
4084 - (dw64 ? sizeof(ULong) : sizeof(UInt))
4085 - cie_pointer;
4086 else
4087 look_for = cie_pointer;
4088
4089 for (cie = 0; cie < n_CIEs; cie++) {
4090 if (0) VG_(printf)("look for %lld %lld\n",
4091 look_for, the_CIEs[cie].offset );
4092 if (the_CIEs[cie].offset == look_for)
4093 break;
4094 }
4095 vg_assert(cie >= 0 && cie <= n_CIEs);
4096 if (cie == n_CIEs) {
4097 how = "FDE refers to not-findable CIE";
4098 goto bad;
4099 }
4100
4101 adi.encoding = the_CIEs[cie].address_encoding;
4102 adi.ehframe_image = frame_image;
4103 adi.ehframe_avma = frame_avma;
4104 adi.text_bias = di->text_debug_bias;
4105 fde_initloc = step_encoded_Addr(&adi, &data);
4106 if (di->trace_cfi)
4107 VG_(printf)("fde.initloc = %#lx\n", fde_initloc);
4108
4109 adi.encoding = the_CIEs[cie].address_encoding & 0xf;
4110 adi.ehframe_image = frame_image;
4111 adi.ehframe_avma = frame_avma;
4112 adi.text_bias = di->text_debug_bias;
4113
4114 /* WAS (incorrectly):
4115 fde_arange = read_encoded_Addr(&nbytes, &adi, data);
4116 data += nbytes;
4117 The following corresponds to what binutils/dwarf.c does:
4118 */
4119 { UInt ptr_size = size_of_encoded_Addr( adi.encoding );
4120 switch (ptr_size) {
4121 case 8: case 4: case 2: case 1:
4122 fde_arange
4123 = (UWord)step_le_u_encoded_literal(&data, ptr_size);
4124 break;
4125 default:
4126 how = "unknown arange field encoding in FDE";
4127 goto bad;
4128 }
4129 }
4130
4131 if (di->trace_cfi)
4132 VG_(printf)("fde.arangec = %#lx\n", fde_arange);
4133
4134 if (di->ddump_frames)
4135 VG_(printf)("%08lx %08lx %08lx FDE cie=%08lx pc=%08lx..%08lx\n",
4136 (Addr)ML_(cur_minus)(ciefde_start, frame_image),
4137 (Addr)ciefde_len,
4138 (Addr)(UWord)cie_pointer,
4139 (Addr)look_for,
4140 ((Addr)fde_initloc) - di->text_debug_bias,
4141 ((Addr)fde_initloc) - di->text_debug_bias + fde_arange);
4142
4143 if (the_CIEs[cie].saw_z_augmentation) {
4144 UInt length = step_leb128( &data, 0);
4145 if (di->ddump_frames && (length > 0)) {
4146 UInt i;
4147 VG_(printf)(" Augmentation data: ");
4148 for (i = 0; i < length; i++)
4149 VG_(printf)(" %02x", (UInt)ML_(cur_read_UChar)
4150 (ML_(cur_plus)(data, i)));
4151 VG_(printf)("\n\n");
4152 }
4153 data = ML_(cur_plus)(data, length);
4154 }
4155
4156 fde_instrs = data;
4157 fde_ilen = ML_(cur_minus)(ciefde_start, data)
4158 + (Long)ciefde_len + (Long)sizeof(UInt);
4159 if (di->trace_cfi) {
4160 //VG_(printf)("fde.instrs = %p\n", fde_instrs);
4161 VG_(printf)("fde.ilen = %d\n", (Int)fde_ilen);
4162 }
4163
4164 if (fde_ilen < 0 || fde_ilen > frame_size) {
4165 how = "implausible # fde insns";
4166 goto bad;
4167 }
4168
4169 data = ML_(cur_plus)(data, fde_ilen);
4170
4171 /* If this object's DebugInfo* had some DiCFSIs from a
4172 previous .eh_frame or .debug_frame read, we must check
4173 that we're not adding a duplicate. */
4174 if (cfsi_used_orig > 0) {
4175 Addr a_mid_lo, a_mid_hi;
4176 Word mid, size,
4177 lo = 0,
4178 hi = cfsi_used_orig-1;
4179 while (True) {
4180 /* current unsearched space is from lo to hi, inclusive. */
4181 if (lo > hi) break; /* not found */
4182 mid = (lo + hi) / 2;
4183 a_mid_lo = di->cfsi[mid].base;
4184 size = di->cfsi[mid].len;
4185 a_mid_hi = a_mid_lo + size - 1;
4186 vg_assert(a_mid_hi >= a_mid_lo);
4187 if (fde_initloc + fde_arange <= a_mid_lo) {
4188 hi = mid-1; continue;
4189 }
4190 if (fde_initloc > a_mid_hi) { lo = mid+1; continue; }
4191 break;
4192 }
4193
4194 /* The range this .debug_frame FDE covers has been already
4195 covered in .eh_frame section. Don't add it from .debug_frame
4196 section again. */
4197 if (lo <= hi)
4198 continue;
4199 }
4200
4201 adi.encoding = the_CIEs[cie].address_encoding;
4202 adi.ehframe_image = frame_image;
4203 adi.ehframe_avma = frame_avma;
4204 adi.text_bias = di->text_debug_bias;
4205
4206 if (di->trace_cfi)
4207 show_CF_instructions( fde_instrs, fde_ilen, &adi,
4208 the_CIEs[cie].code_a_f,
4209 the_CIEs[cie].data_a_f );
4210
4211 initUnwindContext(&ctx);
4212 ctx.code_a_f = the_CIEs[cie].code_a_f;
4213 ctx.data_a_f = the_CIEs[cie].data_a_f;
4214 ctx.initloc = fde_initloc;
4215 ctx.ra_reg = the_CIEs[cie].ra_reg;
4216 ctx.exprs = VG_(newXA)( ML_(dinfo_zalloc), "di.rcid.1",
4217 ML_(dinfo_free),
4218 sizeof(CfiExpr) );
4219 vg_assert(ctx.exprs);
4220
4221 /* Run the CIE's instructions. Ugly hack: if
4222 --debug-dump=frames is in effect, suppress output for
4223 these instructions since they will already have been shown
4224 at the time the CIE was first encountered. Note, not
4225 thread safe - if this reader is ever made threaded, should
4226 fix properly. */
4227 { Bool hack = di->ddump_frames;
4228 di->ddump_frames = False;
4229 initUnwindContext(&restore_ctx);
4230 ok = run_CF_instructions(
4231 di, False, &ctx, the_CIEs[cie].instrs,
4232 the_CIEs[cie].ilen, 0, NULL, &adi
4233 );
4234 di->ddump_frames = hack;
4235 }
4236 /* And now run the instructions for the FDE, starting from
4237 the state created by running the CIE preamble
4238 instructions. */
4239 if (ok) {
4240 restore_ctx = ctx;
4241 ok = run_CF_instructions(
4242 di, True, &ctx, fde_instrs, fde_ilen, fde_arange,
4243 &restore_ctx, &adi
4244 );
4245 if (di->ddump_frames)
4246 VG_(printf)("\n");
4247 }
4248
4249 VG_(deleteXA)( ctx.exprs );
4250 }
4251 }
4252
4253 return;
4254
4255 bad:
4256 if (!VG_(clo_xml) && VG_(clo_verbosity) > 1)
4257 VG_(message)(Vg_UserMsg,
4258 "Warning: %s in DWARF2 CFI reading\n", how);
4259 return;
4260 }
4261
4262 #endif // defined(VGO_linux) || defined(VGO_darwin)
4263
4264 /*--------------------------------------------------------------------*/
4265 /*--- end ---*/
4266 /*--------------------------------------------------------------------*/
4267