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