readdwarf.c revision 1936f8b3b201839583a39d0514049bcb1e3c3c2b
1
2/*--------------------------------------------------------------------*/
3/*--- Read DWARF1/2/3 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-2006 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   Stabs reader greatly improved by Nick Nethercote, Apr 02.
32   This module was also extensively hacked on by Jeremy Fitzhardinge
33   and Tom Hughes.
34*/
35
36#include "pub_core_basics.h"
37#include "pub_core_libcbase.h"
38#include "pub_core_libcassert.h"
39#include "pub_core_libcprint.h"
40#include "pub_core_mallocfree.h"
41#include "pub_core_options.h"
42#include "priv_storage.h"
43#include "priv_readdwarf.h"        /* self */
44
45
46/*------------------------------------------------------------*/
47/*---                                                      ---*/
48/*--- Read line number and CFI info from DWARF1, DWARF2    ---*/
49/*--- and to some extent DWARF3 sections.                  ---*/
50/*---                                                      ---*/
51/*------------------------------------------------------------*/
52
53/*------------------------------------------------------------*/
54/*--- Expanding arrays of words, for holding file name and ---*/
55/*--- directory name arrays.                               ---*/
56/*------------------------------------------------------------*/
57
58typedef
59   struct {
60      Word* tab;
61      UInt  tab_size;
62      UInt  tab_used;
63   }
64   WordArray;
65
66static void init_WordArray ( WordArray* wa )
67{
68   wa->tab      = NULL;
69   wa->tab_size = 0;
70   wa->tab_used = 0;
71}
72
73static void free_WordArray ( WordArray* wa )
74{
75   if (wa->tab) {
76      vg_assert(wa->tab_size > 0);
77      VG_(arena_free)(VG_AR_SYMTAB, wa->tab);
78   }
79   init_WordArray(wa);
80}
81
82static void addto_WordArray ( WordArray* wa, Word w )
83{
84   UInt  new_size, i;
85   Word* new_tab;
86
87   if (0) VG_(printf)("<<ADD %p (new sz = %d) >>\n",
88                      (HChar*)w, wa->tab_used+1);
89
90   if (wa->tab_used < wa->tab_size) {
91      /* fine */
92   } else {
93      /* expand array */
94      if (0) VG_(printf)("EXPAND ARRAY from %d\n", wa->tab_size);
95      vg_assert(wa->tab_used == wa->tab_size);
96      vg_assert( (wa->tab_size == 0 && wa->tab == NULL)
97                 || (wa->tab_size != 0 && wa->tab != NULL) );
98      new_size = wa->tab_size == 0 ? 8 : 2 * wa->tab_size;
99      new_tab  = VG_(arena_malloc)(VG_AR_SYMTAB,
100                                   new_size * sizeof(Word));
101      vg_assert(new_tab != NULL);
102      for (i = 0; i < wa->tab_used; i++)
103         new_tab[i] = wa->tab[i];
104      wa->tab_size = new_size;
105      if (wa->tab)
106         VG_(arena_free)(VG_AR_SYMTAB, wa->tab);
107      wa->tab = new_tab;
108   }
109
110   vg_assert(wa->tab_used < wa->tab_size);
111   vg_assert(wa->tab_size > 0);
112   wa->tab[wa->tab_used] = w;
113   wa->tab_used++;
114}
115
116static Word index_WordArray ( WordArray* wa, Int i )
117{
118   vg_assert(i >= 0 && i < wa->tab_used);
119   return wa->tab[i];
120}
121
122
123/*------------------------------------------------------------*/
124/*--- Read DWARF2 format line number info.                 ---*/
125/*------------------------------------------------------------*/
126
127/* Structure holding info extracted from the a .debug_line
128   section.  */
129typedef struct
130{
131  ULong  li_length;
132  UShort li_version;
133  ULong  li_header_length;
134  UChar  li_min_insn_length;
135  UChar  li_default_is_stmt;
136  Int    li_line_base;
137  UChar  li_line_range;
138  UChar  li_opcode_base;
139}
140DebugLineInfo;
141
142/* Structure holding additional infos found from a .debug_info
143 * compilation unit block */
144typedef struct
145{
146  /* Feel free to add more members here if you need ! */
147  Char* compdir;   /* Compilation directory - points to .debug_info */
148  Char* name;      /* Main file name - points to .debug_info */
149  ULong stmt_list; /* Offset in .debug_line */
150  Bool  dw64;      /* 64-bit Dwarf? */
151}
152UnitInfo;
153
154/* Line number opcodes.  */
155enum dwarf_line_number_ops
156  {
157    DW_LNS_extended_op = 0,
158    DW_LNS_copy = 1,
159    DW_LNS_advance_pc = 2,
160    DW_LNS_advance_line = 3,
161    DW_LNS_set_file = 4,
162    DW_LNS_set_column = 5,
163    DW_LNS_negate_stmt = 6,
164    DW_LNS_set_basic_block = 7,
165    DW_LNS_const_add_pc = 8,
166    DW_LNS_fixed_advance_pc = 9,
167    /* DWARF 3.  */
168    DW_LNS_set_prologue_end = 10,
169    DW_LNS_set_epilogue_begin = 11,
170    DW_LNS_set_isa = 12
171  };
172
173/* Line number extended opcodes.  */
174enum dwarf_line_number_x_ops
175  {
176    DW_LNE_end_sequence = 1,
177    DW_LNE_set_address = 2,
178    DW_LNE_define_file = 3
179  };
180
181typedef struct State_Machine_Registers
182{
183  /* Information for the last statement boundary.
184   * Needed to calculate statement lengths. */
185  Addr  last_address;
186  UInt  last_file;
187  UInt  last_line;
188
189  Addr  address;
190  UInt  file;
191  UInt  line;
192  UInt  column;
193  Int   is_stmt;
194  Int   basic_block;
195  Int   end_sequence;
196} SMR;
197
198
199static
200UInt read_leb128 ( UChar* data, Int* length_return, Int sign )
201{
202  UInt   result = 0;
203  UInt   num_read = 0;
204  Int    shift = 0;
205  UChar  byte;
206
207  do
208    {
209      byte = * data ++;
210      num_read ++;
211
212      result |= (byte & 0x7f) << shift;
213
214      shift += 7;
215
216    }
217  while (byte & 0x80);
218
219  if (length_return != NULL)
220    * length_return = num_read;
221
222  if (sign && (shift < 32) && (byte & 0x40))
223    result |= -1 << shift;
224
225  return result;
226}
227
228
229/* Small helper functions easier to use
230 * value is returned and the given pointer is
231 * moved past end of leb128 data */
232static UInt read_leb128U( UChar **data )
233{
234  Int len;
235  UInt val = read_leb128( *data, &len, 0 );
236  *data += len;
237  return val;
238}
239
240/* Same for signed data */
241static Int read_leb128S( UChar **data )
242{
243   Int len;
244   UInt val = read_leb128( *data, &len, 1 );
245   *data += len;
246   return val;
247}
248
249/* Read what the DWARF3 spec calls an "initial length field".  This
250   uses up either 4 or 12 bytes of the input and produces a 32-bit or
251   64-bit number respectively.
252
253   Read 32-bit value from p.  If it is 0xFFFFFFFF, instead read a
254   64-bit bit value from p+4.  This is used in 64-bit dwarf to encode
255   some table lengths. */
256static ULong read_initial_length_field ( UChar* p, /*OUT*/Bool* is64 )
257{
258   UInt w32 = *((UInt*)p);
259   if (w32 == 0xFFFFFFFF) {
260      *is64 = True;
261      return *((ULong*)(p+4));
262   } else {
263      *is64 = False;
264      return (ULong)w32;
265   }
266}
267
268
269static SMR state_machine_regs;
270
271static
272void reset_state_machine ( Int is_stmt )
273{
274   if (0) VG_(printf)("smr.a := %p (reset)\n", 0 );
275   state_machine_regs.last_address = 0;
276   state_machine_regs.last_file = 1;
277   state_machine_regs.last_line = 1;
278   state_machine_regs.address = 0;
279   state_machine_regs.file = 1;
280   state_machine_regs.line = 1;
281   state_machine_regs.column = 0;
282   state_machine_regs.is_stmt = is_stmt;
283   state_machine_regs.basic_block = 0;
284   state_machine_regs.end_sequence = 0;
285}
286
287/* Look up a directory name, or return NULL if unknown. */
288static
289Char* lookupDir ( Int filename_index,
290                  WordArray* fnidx2dir,
291                  WordArray* dirnames )
292{
293   Word diridx  = index_WordArray( fnidx2dir, filename_index );
294   Word dirname = index_WordArray( dirnames, (Int)diridx );
295   return (Char*)dirname;
296}
297
298////////////////////////////////////////////////////////////////////
299////////////////////////////////////////////////////////////////////
300
301/* Handled an extend line op.  Returns true if this is the end
302   of sequence.  */
303static
304Int process_extended_line_op( struct _SegInfo* si, OffT debug_offset,
305                              WordArray* filenames,
306                              WordArray* dirnames,
307                              WordArray* fnidx2dir,
308                              UChar* data, Int is_stmt)
309{
310   UChar  op_code;
311   Int    bytes_read;
312   UInt   len;
313   UChar* name;
314   Addr   adr;
315
316   len = read_leb128 (data, & bytes_read, 0);
317   data += bytes_read;
318
319   if (len == 0) {
320      VG_(message)(Vg_UserMsg,
321                   "badly formed extended line op encountered!\n");
322      return bytes_read;
323   }
324
325   len += bytes_read;
326   op_code = * data ++;
327
328   if (0) VG_(printf)("dwarf2: ext OPC: %d\n", op_code);
329
330   switch (op_code) {
331      case DW_LNE_end_sequence:
332         if (0) VG_(printf)("1001: si->o %p, smr.a %p\n",
333                            debug_offset, state_machine_regs.address );
334         /* JRS: added for compliance with spec; is pointless due to
335            reset_state_machine below */
336         state_machine_regs.end_sequence = 1;
337
338         if (state_machine_regs.is_stmt) {
339            if (state_machine_regs.last_address)
340               ML_(addLineInfo) (
341                  si,
342                  (Char*)index_WordArray(filenames,
343                                         state_machine_regs.last_file),
344                  lookupDir( state_machine_regs.last_file,
345                             fnidx2dir, dirnames ),
346                  debug_offset + state_machine_regs.last_address,
347                  debug_offset + state_machine_regs.address,
348                  state_machine_regs.last_line, 0
349               );
350         }
351         reset_state_machine (is_stmt);
352         break;
353
354      case DW_LNE_set_address:
355         adr = *((Addr *)data);
356         if (0) VG_(printf)("smr.a := %p\n", adr );
357         state_machine_regs.address = adr;
358         break;
359
360      case DW_LNE_define_file:
361         name = data;
362         addto_WordArray( filenames, (Word)ML_(addStr)(si,name,-1) );
363         data += VG_(strlen) ((char *) data) + 1;
364         read_leb128 (data, & bytes_read, 0);
365         data += bytes_read;
366         read_leb128 (data, & bytes_read, 0);
367         data += bytes_read;
368         read_leb128 (data, & bytes_read, 0);
369         break;
370
371      default:
372         break;
373   }
374
375   return len;
376}
377
378////////////////////////////////////////////////////////////////////
379////////////////////////////////////////////////////////////////////
380
381/* read a .debug_line section block for a compilation unit
382 *
383 * Input:   - theBlock must point to the start of the block
384 *            for the given compilation unit
385 *          - ui contains additional info like the compilation dir
386 *            for this unit
387 *
388 * Output: - si debug info structures get updated
389 */
390static
391void read_dwarf2_lineblock ( struct _SegInfo*  si, OffT debug_offset,
392                             UnitInfo* ui,
393                             UChar*    theBlock,
394                             Int       noLargerThan )
395{
396   DebugLineInfo  info;
397   UChar*         standard_opcodes;
398   UChar*         end_of_sequence;
399   Bool           is64;
400   WordArray      filenames;
401   WordArray      dirnames;
402   WordArray      fnidx2dir;
403
404   UChar*         external = theBlock;
405   UChar*         data = theBlock;
406
407   /* filenames is an array of file names harvested from the DWARF2
408      info.  Entry [0] is NULL and is never referred to by the state
409      machine.
410
411      Similarly, dirnames is an array of directory names.  Entry [0]
412      is also NULL and denotes "we don't know what the path is", since
413      that is different from "the path is the empty string".  Unlike
414      the file name table, the state machine does refer to entry [0],
415      which basically means "." ("the current directory of the
416      compilation", whatever that means, according to the DWARF3
417      spec.)
418
419      fnidx2dir is an array of indexes into the dirnames table.
420      (confused yet?)  filenames[] and fnidx2dir[] are indexed
421      together.  That is, for some index i in the filename table, then
422
423         the filename  is filenames[i]
424         the directory is dirnames[ fnidx2dir[i] ] */
425
426   /* Fails due to gcc padding ...
427   vg_assert(sizeof(DWARF2_External_LineInfo)
428             == sizeof(DWARF2_Internal_LineInfo));
429   */
430
431   init_WordArray(&filenames);
432   init_WordArray(&dirnames);
433   init_WordArray(&fnidx2dir);
434
435   /* DWARF2 starts numbering filename entries at 1, so we need to
436      add a dummy zeroth entry to the table.  The zeroth dirnames
437      entry denotes 'current directory of compilation' so we might
438      as well make the fnidx2dir zeroth entry denote that.
439   */
440   addto_WordArray( &filenames, (Word)NULL );
441
442   if (ui->compdir)
443      addto_WordArray( &dirnames, (Word)ML_(addStr)(si, ui->compdir, -1) );
444   else
445      addto_WordArray( &dirnames, (Word)ML_(addStr)(si, ".", -1) );
446
447   addto_WordArray( &fnidx2dir, (Word)0 );  /* compilation dir */
448
449   info.li_length = read_initial_length_field( external, &is64 );
450   external += is64 ? 12 : 4;
451   /* Check the length of the block.  */
452   if (info.li_length > noLargerThan) {
453      ML_(symerr)("DWARF line info appears to be corrupt "
454                  "- the section is too small");
455      goto out;
456   }
457
458   /* Check its version number.  */
459   info.li_version = * ((UShort *)external);
460   external += 2;
461   if (info.li_version != 2) {
462      ML_(symerr)("Only DWARF version 2 line info "
463                  "is currently supported.");
464      goto out;
465   }
466
467   info.li_header_length = ui->dw64 ? *((ULong*)external)
468                                    : (ULong)(*((UInt*)external));
469   external += ui->dw64 ? 8 : 4;
470
471   info.li_min_insn_length = * ((UChar *)external);
472   external += 1;
473
474   info.li_default_is_stmt = True;
475   /* WAS: = * ((UChar *)(external->li_default_is_stmt)); */
476   external += 1;
477   /* Josef Weidendorfer (20021021) writes:
478
479      It seems to me that the Intel Fortran compiler generates bad
480      DWARF2 line info code: It sets "is_stmt" of the state machine in
481      the the line info reader to be always false. Thus, there is
482      never a statement boundary generated and therefore never a
483      instruction range/line number mapping generated for valgrind.
484
485      Please have a look at the DWARF2 specification, Ch. 6.2
486      (x86.ddj.com/ftp/manuals/tools/dwarf.pdf).  Perhaps I understand
487      this wrong, but I don't think so.
488
489      I just had a look at the GDB DWARF2 reader...  They completely
490      ignore "is_stmt" when recording line info ;-) That's the reason
491      "objdump -S" works on files from the the intel fortran compiler.
492   */
493
494   /* JRS: changed (UInt*) to (UChar*) */
495   info.li_line_base = * ((UChar *)external);
496   info.li_line_base = (Int)(signed char)info.li_line_base;
497   external += 1;
498
499   info.li_line_range = * ((UChar *)external);
500   external += 1;
501
502   info.li_opcode_base = * ((UChar *)external);
503   external += 1;
504
505   if (0) VG_(printf)("dwarf2: line base: %d, range %d, opc base: %d\n",
506                      (Int)info.li_line_base,
507                      (Int)info.li_line_range,
508                      (Int)info.li_opcode_base);
509
510   end_of_sequence = data + info.li_length
511                          + (is64 ? 12 : 4);
512
513   reset_state_machine (info.li_default_is_stmt);
514
515   /* Read the contents of the Opcodes table.  */
516   standard_opcodes = external;
517
518   /* Read the contents of the Directory table.  */
519   data = standard_opcodes + info.li_opcode_base - 1;
520
521   while (* data != 0) {
522
523#     define NBUF 4096
524      static Char buf[NBUF];
525
526      /* If data[0] is '/', then 'data' is an absolute path and we
527         don't mess with it.  Otherwise, if we can, construct the
528         'path ui->compdir' ++ "/" ++ 'data'. */
529
530      if (*data != '/'
531          /* not an absolute path */
532          && ui->compdir != NULL
533          /* actually got something sensible for compdir */
534          && VG_(strlen)(ui->compdir) + VG_(strlen)(data) + 5/*paranoia*/ < NBUF
535          /* it's short enough to concatenate */)
536      {
537         buf[0] = 0;
538         VG_(strcat)(buf, ui->compdir);
539         VG_(strcat)(buf, "/");
540         VG_(strcat)(buf, data);
541         vg_assert(VG_(strlen)(buf) < NBUF);
542         addto_WordArray( &dirnames, (Word)ML_(addStr)(si,buf,-1) );
543         if (0) VG_(printf)("rel path  %s\n", buf);
544      } else {
545         /* just use 'data'. */
546         addto_WordArray( &dirnames, (Word)ML_(addStr)(si,data,-1) );
547         if (0) VG_(printf)("abs path  %s\n", data);
548      }
549
550      data += VG_(strlen)(data) + 1;
551
552#     undef NBUF
553   }
554   if (*data != 0) {
555      ML_(symerr)("can't find NUL at end of DWARF2 directory table");
556      goto out;
557   }
558   data ++;
559
560   /* Read the contents of the File Name table.  This produces a bunch
561      of file names, and for each, an index to the corresponding
562      direcory name entry. */
563   while (* data != 0) {
564      UChar* name;
565      Int    bytes_read, diridx;
566      name = data;
567      data += VG_(strlen) ((Char *) data) + 1;
568
569      diridx = read_leb128 (data, & bytes_read, 0);
570      data += bytes_read;
571      read_leb128 (data, & bytes_read, 0);
572      data += bytes_read;
573      read_leb128 (data, & bytes_read, 0);
574      data += bytes_read;
575
576      addto_WordArray( &filenames, (Word)ML_(addStr)(si,name,-1) );
577      addto_WordArray( &fnidx2dir, (Word)diridx );
578      if (0) VG_(printf)("file %s diridx %d\n", name, diridx );
579   }
580   if (*data != 0) {
581      ML_(symerr)("can't find NUL at end of DWARF2 file name table");
582      goto out;
583   }
584   data ++;
585
586   /* Now display the statements.  */
587
588   while (data < end_of_sequence) {
589
590      UChar op_code;
591      Int           adv;
592      Int           bytes_read;
593
594      op_code = * data ++;
595
596      if (0) VG_(printf)("dwarf2: OPC: %d\n", op_code);
597
598      if (op_code >= info.li_opcode_base) {
599
600         Int advAddr;
601         op_code -= info.li_opcode_base;
602         adv      = (op_code / info.li_line_range)
603                       * info.li_min_insn_length;
604         advAddr = adv;
605         state_machine_regs.address += adv;
606         if (0) VG_(printf)("smr.a += %p\n", adv );
607         adv = (op_code % info.li_line_range) + info.li_line_base;
608         if (0) VG_(printf)("1002: si->o %p, smr.a %p\n",
609                            debug_offset, state_machine_regs.address );
610         state_machine_regs.line += adv;
611
612         if (state_machine_regs.is_stmt) {
613            /* only add a statement if there was a previous boundary */
614            if (state_machine_regs.last_address)
615               ML_(addLineInfo)(
616                  si,
617                  (Char*)index_WordArray( &filenames,
618                                          state_machine_regs.last_file ),
619                  lookupDir( state_machine_regs.last_file,
620                             &fnidx2dir, &dirnames ),
621                  debug_offset + state_machine_regs.last_address,
622                  debug_offset + state_machine_regs.address,
623                  state_machine_regs.last_line,
624                  0
625               );
626            state_machine_regs.last_address = state_machine_regs.address;
627            state_machine_regs.last_file = state_machine_regs.file;
628            state_machine_regs.last_line = state_machine_regs.line;
629         }
630
631      }
632
633      else /* ! (op_code >= info.li_opcode_base) */
634      switch (op_code) {
635         case DW_LNS_extended_op:
636            data += process_extended_line_op (
637                       si, debug_offset, &filenames, &dirnames, &fnidx2dir,
638                       data, info.li_default_is_stmt);
639            break;
640
641         case DW_LNS_copy:
642            if (0) VG_(printf)("1002: si->o %p, smr.a %p\n",
643                               debug_offset, state_machine_regs.address );
644            if (state_machine_regs.is_stmt) {
645               /* only add a statement if there was a previous boundary */
646               if (state_machine_regs.last_address)
647                  ML_(addLineInfo)(
648                     si,
649                     (Char*)index_WordArray( &filenames,
650                                             state_machine_regs.last_file ),
651                     lookupDir( state_machine_regs.last_file,
652                                &fnidx2dir, &dirnames ),
653                     debug_offset + state_machine_regs.last_address,
654                     debug_offset + state_machine_regs.address,
655                     state_machine_regs.last_line,
656                     0
657                  );
658               state_machine_regs.last_address = state_machine_regs.address;
659               state_machine_regs.last_file = state_machine_regs.file;
660               state_machine_regs.last_line = state_machine_regs.line;
661            }
662            state_machine_regs.basic_block = 0; /* JRS added */
663            break;
664
665         case DW_LNS_advance_pc:
666            adv = info.li_min_insn_length
667                     * read_leb128 (data, & bytes_read, 0);
668            data += bytes_read;
669            state_machine_regs.address += adv;
670            if (0) VG_(printf)("smr.a += %p\n", adv );
671            break;
672
673         case DW_LNS_advance_line:
674            adv = read_leb128 (data, & bytes_read, 1);
675            data += bytes_read;
676            state_machine_regs.line += adv;
677            break;
678
679         case DW_LNS_set_file:
680            adv = read_leb128 (data, & bytes_read, 0);
681            data += bytes_read;
682            state_machine_regs.file = adv;
683            break;
684
685         case DW_LNS_set_column:
686            adv = read_leb128 (data, & bytes_read, 0);
687            data += bytes_read;
688            state_machine_regs.column = adv;
689            break;
690
691         case DW_LNS_negate_stmt:
692            adv = state_machine_regs.is_stmt;
693            adv = ! adv;
694            state_machine_regs.is_stmt = adv;
695            break;
696
697         case DW_LNS_set_basic_block:
698            state_machine_regs.basic_block = 1;
699            break;
700
701         case DW_LNS_const_add_pc:
702            adv = (((255 - info.li_opcode_base) / info.li_line_range)
703                   * info.li_min_insn_length);
704            state_machine_regs.address += adv;
705            if (0) VG_(printf)("smr.a += %p\n", adv );
706            break;
707
708         case DW_LNS_fixed_advance_pc:
709            /* XXX: Need something to get 2 bytes */
710            adv = *((UShort *)data);
711            data += 2;
712            state_machine_regs.address += adv;
713            if (0) VG_(printf)("smr.a += %p\n", adv );
714            break;
715
716         case DW_LNS_set_prologue_end:
717            break;
718
719         case DW_LNS_set_epilogue_begin:
720            break;
721
722         case DW_LNS_set_isa:
723            adv = read_leb128 (data, & bytes_read, 0);
724            data += bytes_read;
725            break;
726
727         default: {
728            Int j;
729            for (j = standard_opcodes[op_code - 1]; j > 0 ; --j) {
730               read_leb128 (data, &bytes_read, 0);
731               data += bytes_read;
732            }
733         }
734         break;
735      } /* switch (op_code) */
736
737   } /* while (data < end_of_sequence) */
738
739  out:
740   free_WordArray(&filenames);
741   free_WordArray(&dirnames);
742   free_WordArray(&fnidx2dir);
743}
744
745////////////////////////////////////////////////////////////////////
746////////////////////////////////////////////////////////////////////
747
748/* Return abbrev for given code
749 * Returned pointer points to the tag
750 * */
751static UChar* lookup_abbrev( UChar* p, UInt acode )
752{
753   UInt code;
754   UInt name;
755   for( ; ; ) {
756      code = read_leb128U( &p );
757      if ( code == acode )
758         return p;
759      read_leb128U( &p ); /* skip tag */
760      p++;                /* skip has_children flag */
761      do {
762         name = read_leb128U( &p ); /* name */
763         read_leb128U( &p );   /* form */
764      }
765      while( name != 0 ); /* until name == form == 0 */
766   }
767   return NULL;
768}
769
770/* Read general information for a particular compile unit block in
771 * the .debug_info section.
772 *
773 * Input: - unitblock is the start of a compilation
774 *          unit block in .debuginfo section
775 *        - debugabbrev is start of .debug_abbrev section
776 *        - debugstr is start of .debug_str section
777 *
778 * Output: Fill members of ui pertaining to the compilation unit:
779 *         - ui->name is the name of the compilation unit
780 *         - ui->compdir is the compilation unit directory
781 *         - ui->stmt_list is the offset in .debug_line section
782 *                for the dbginfos of this compilation unit
783 *
784 * Note : the output strings are not allocated and point
785 * directly to the memory-mapped section.
786 */
787static
788void read_unitinfo_dwarf2( /*OUT*/UnitInfo* ui,
789                                  UChar*    unitblock,
790                                  UChar*    debugabbrev,
791                                  UChar*    debugstr )
792{
793   UInt   acode, abcode;
794   ULong  atoffs, blklen;
795   Int    level;
796   UShort ver;
797
798   UChar addr_size;
799   UChar* p = unitblock;
800   UChar* end;
801   UChar* abbrev;
802
803   VG_(memset)( ui, 0, sizeof( UnitInfo ) );
804   ui->stmt_list = -1LL;
805
806   /* Read the compilation unit header in .debug_info section - See p 70 */
807
808   /* This block length */
809   blklen = read_initial_length_field( p, &ui->dw64 );
810   p += ui->dw64 ? 12 : 4;
811
812   /* version should be 2 */
813   ver = *((UShort*)p);
814   p += 2;
815
816   /* get offset in abbrev */
817   atoffs = ui->dw64 ? *((ULong*)p) : (ULong)(*((UInt*)p));
818   p += ui->dw64 ? 8 : 4;
819
820   /* Address size */
821   addr_size = *p;
822   p += 1;
823
824   end    = unitblock + blklen + (ui->dw64 ? 12 : 4); /* End of this block */
825   level  = 0;                      /* Level in the abbrev tree */
826   abbrev = debugabbrev + atoffs;   /* Abbreviation data for this block */
827
828   /* Read the compilation unit entries */
829   while ( p < end ) {
830      Bool has_child;
831      UInt tag;
832
833      acode = read_leb128U( &p ); /* abbreviation code */
834      if ( acode == 0 ) {
835         /* NULL entry used for padding - or last child for a sequence
836            - see para 7.5.3 */
837         level--;
838         continue;
839      }
840
841      /* Read abbreviation header */
842      abcode = read_leb128U( &abbrev ); /* abbreviation code */
843      if ( acode != abcode ) {
844         /* We are in in children list, and must rewind to a
845          * previously declared abbrev code.  This code works but is
846          * not triggered since we shortcut the parsing once we have
847          * read the compile_unit block.  This should only occur when
848          * level > 0 */
849         abbrev = lookup_abbrev( debugabbrev + atoffs, acode );
850      }
851
852      tag = read_leb128U( &abbrev );
853      has_child = *(abbrev++) == 1; /* DW_CHILDREN_yes */
854
855      if ( has_child )
856         level++;
857
858      /* And loop on entries */
859      for ( ; ; ) {
860         /* Read entry definition */
861         UInt  name, form;
862         ULong cval = -1LL;  /* Constant value read */
863         Char  *sval = NULL; /* String value read */
864         name = read_leb128U( &abbrev );
865         form = read_leb128U( &abbrev );
866         if ( name == 0 )
867            break;
868
869         /* Read data */
870         /* Attributes encoding explained p 71 */
871         if ( form == 0x16 /* FORM_indirect */ )
872            form = read_leb128U( &p );
873         /* Decode form. For most kinds, Just skip the amount of data since
874            we don't use it for now */
875         /* JRS 9 Feb 06: This now handles 64-bit DWARF too.  In
876            64-bit DWARF, lineptr (and loclistptr,macptr,rangelistptr
877            classes) use FORM_data8, not FORM_data4.  Also,
878            FORM_ref_addr and FORM_strp are 64-bit values, not 32-bit
879            values. */
880         switch( form ) {
881            /* Those cases extract the data properly */
882            case 0x05: /* FORM_data2 */     cval = *((UShort*)p); p +=2; break;
883            case 0x06: /* FORM_data4 */     cval = *((UInt*)p);p +=4; break;
884            case 0x0e: /* FORM_strp */      /* pointer in .debug_str */
885                       /* 2006-01-01: only generate a value if
886                          debugstr is non-NULL (which means that a
887                          debug_str section was found) */
888                                            if (debugstr && !ui->dw64)
889                                               sval = debugstr + *((UInt*)p);
890                                            if (debugstr && ui->dw64)
891                                               sval = debugstr + *((ULong*)p);
892                                            p += ui->dw64 ? 8 : 4;
893                                            break;
894            case 0x08: /* FORM_string */    sval = (Char*)p;
895                                            p += VG_(strlen)((Char*)p) + 1; break;
896            case 0x0b: /* FORM_data1 */     cval = *p; p++; break;
897
898            /* TODO : Following ones just skip data - implement if you need */
899            case 0x01: /* FORM_addr */      p += addr_size; break;
900            case 0x03: /* FORM_block2 */    p += *((UShort*)p) + 2; break;
901            case 0x04: /* FORM_block4 */    p += *((UInt*)p) + 4; break;
902            case 0x07: /* FORM_data8 */     if (ui->dw64) cval = *((ULong*)p);
903                                            p += 8; break;
904                       /* perhaps should assign unconditionally to cval? */
905            case 0x09: /* FORM_block */     p += read_leb128U( &p ); break;
906            case 0x0a: /* FORM_block1 */    p += *p + 1; break;
907            case 0x0c: /* FORM_flag */      p++; break;
908            case 0x0d: /* FORM_sdata */     read_leb128S( &p ); break;
909            case 0x0f: /* FORM_udata */     read_leb128U( &p ); break;
910            case 0x10: /* FORM_ref_addr */  p += ui->dw64 ? 8 : 4; break;
911            case 0x11: /* FORM_ref1 */      p++; break;
912            case 0x12: /* FORM_ref2 */      p += 2; break;
913            case 0x13: /* FORM_ref4 */      p += 4; break;
914            case 0x14: /* FORM_ref8 */      p += 8; break;
915            case 0x15: /* FORM_ref_udata */ read_leb128U( &p ); break;
916
917            default:
918               VG_(printf)( "### unhandled dwarf2 abbrev form code 0x%x\n", form );
919               break;
920         }
921
922         /* Now store the members we need in the UnitInfo structure */
923         if ( tag == 0x0011 /*TAG_compile_unit*/ ) {
924                 if ( name == 0x03 ) ui->name = sval;      /* DW_AT_name */
925            else if ( name == 0x1b ) ui->compdir = sval;   /* DW_AT_compdir */
926            else if ( name == 0x10 ) ui->stmt_list = cval; /* DW_AT_stmt_list */
927         }
928      }
929      /* Shortcut the parsing once we have read the compile_unit block
930       * That's enough info for us, and we are not gdb ! */
931      if ( tag == 0x0011 /*TAG_compile_unit*/ )
932         break;
933   } /* Loop on each sub block */
934
935   /* This test would be valid if we were not shortcutting the parsing
936   if (level != 0)
937      VG_(printf)( "#### Exiting debuginfo block at level %d !!!\n", level );
938   */
939}
940
941
942////////////////////////////////////////////////////////////////////
943////////////////////////////////////////////////////////////////////
944
945
946/* Collect the debug info from dwarf2 debugging sections
947 * of a given module.
948 *
949 * Inputs: given .debug_xxx sections
950 * Output: update si to contain all the dwarf2 debug infos
951 */
952void ML_(read_debuginfo_dwarf2)
953        ( struct _SegInfo* si, OffT debug_offset,
954          UChar* debuginfo,   Int debug_info_sz,  /* .debug_info */
955          UChar* debugabbrev,                     /* .debug_abbrev */
956          UChar* debugline,   Int debug_line_sz,  /* .debug_line */
957          UChar* debugstr )                       /* .debug_str */
958{
959   UnitInfo ui;
960   UShort   ver;
961   UChar*   block;
962   UChar*   end = debuginfo + debug_info_sz;
963   ULong    blklen;
964   Bool     blklen_is_64;
965   Int      blklen_len = 0;
966
967   /* Make sure we at least have a header for the first block */
968   if (debug_info_sz < 4) {
969     ML_(symerr)( "Last block truncated in .debug_info; ignoring" );
970      return;
971   }
972
973   /* Iterate on all the blocks we find in .debug_info */
974   for ( block = debuginfo; block < end - 4; block += blklen + blklen_len ) {
975
976      /* Read the compilation unit header in .debug_info section - See
977         p 70 */
978      /* This block length */
979      blklen     = read_initial_length_field( block, &blklen_is_64 );
980      blklen_len = blklen_is_64 ? 12 : 4;
981      if ( block + blklen + blklen_len > end ) {
982         ML_(symerr)( "Last block truncated in .debug_info; ignoring" );
983         return;
984      }
985
986      /* version should be 2 */
987      ver = *((UShort*)( block + blklen_len ));
988      if ( ver != 2 ) {
989         ML_(symerr)( "Ignoring non-dwarf2 block in .debug_info" );
990         continue;
991      }
992
993      /* Fill ui with offset in .debug_line and compdir */
994      if (0)
995         VG_(printf)( "Reading UnitInfo at 0x%x.....\n", block - debuginfo );
996      read_unitinfo_dwarf2( &ui, block, debugabbrev, debugstr );
997      if (0)
998         VG_(printf)( "   => LINES=0x%llx    NAME=%s     DIR=%s\n",
999                      ui.stmt_list, ui.name, ui.compdir );
1000
1001      /* Ignore blocks with no .debug_line associated block */
1002      if ( ui.stmt_list == -1LL )
1003         continue;
1004
1005      if (0)
1006         VG_(printf)("debug_line_sz %d, ui.stmt_list %lld  %s\n",
1007                     debug_line_sz, ui.stmt_list, ui.name );
1008      /* Read the .debug_line block for this compile unit */
1009      read_dwarf2_lineblock( si, debug_offset, &ui, debugline + ui.stmt_list,
1010                                      debug_line_sz - ui.stmt_list );
1011   }
1012}
1013
1014
1015////////////////////////////////////////////////////////////////////
1016////////////////////////////////////////////////////////////////////
1017
1018/*------------------------------------------------------------*/
1019/*--- Read DWARF1 format line number info.                 ---*/
1020/*------------------------------------------------------------*/
1021
1022/* DWARF1 appears to be redundant, but nevertheless the Lahey Fortran
1023   compiler generates it.
1024*/
1025
1026/* The following three enums (dwarf_tag, dwarf_form, dwarf_attribute)
1027   are taken from the file include/elf/dwarf.h in the GNU gdb-6.0
1028   sources, which are Copyright 1992, 1993, 1995, 1999 Free Software
1029   Foundation, Inc and naturally licensed under the GNU General Public
1030   License version 2 or later.
1031*/
1032
1033/* Tag names and codes.  */
1034
1035enum dwarf_tag {
1036    TAG_padding			= 0x0000,
1037    TAG_array_type		= 0x0001,
1038    TAG_class_type		= 0x0002,
1039    TAG_entry_point		= 0x0003,
1040    TAG_enumeration_type	= 0x0004,
1041    TAG_formal_parameter	= 0x0005,
1042    TAG_global_subroutine	= 0x0006,
1043    TAG_global_variable		= 0x0007,
1044    				/* 0x0008 -- reserved */
1045				/* 0x0009 -- reserved */
1046    TAG_label			= 0x000a,
1047    TAG_lexical_block		= 0x000b,
1048    TAG_local_variable		= 0x000c,
1049    TAG_member			= 0x000d,
1050				/* 0x000e -- reserved */
1051    TAG_pointer_type		= 0x000f,
1052    TAG_reference_type		= 0x0010,
1053    TAG_compile_unit		= 0x0011,
1054    TAG_string_type		= 0x0012,
1055    TAG_structure_type		= 0x0013,
1056    TAG_subroutine		= 0x0014,
1057    TAG_subroutine_type		= 0x0015,
1058    TAG_typedef			= 0x0016,
1059    TAG_union_type		= 0x0017,
1060    TAG_unspecified_parameters	= 0x0018,
1061    TAG_variant			= 0x0019,
1062    TAG_common_block		= 0x001a,
1063    TAG_common_inclusion	= 0x001b,
1064    TAG_inheritance		= 0x001c,
1065    TAG_inlined_subroutine	= 0x001d,
1066    TAG_module			= 0x001e,
1067    TAG_ptr_to_member_type	= 0x001f,
1068    TAG_set_type		= 0x0020,
1069    TAG_subrange_type		= 0x0021,
1070    TAG_with_stmt		= 0x0022,
1071
1072    /* GNU extensions */
1073
1074    TAG_format_label		= 0x8000,  /* for FORTRAN 77 and Fortran 90 */
1075    TAG_namelist		= 0x8001,  /* For Fortran 90 */
1076    TAG_function_template	= 0x8002,  /* for C++ */
1077    TAG_class_template		= 0x8003   /* for C++ */
1078};
1079
1080/* Form names and codes.  */
1081
1082enum dwarf_form {
1083    FORM_ADDR	= 0x1,
1084    FORM_REF	= 0x2,
1085    FORM_BLOCK2	= 0x3,
1086    FORM_BLOCK4	= 0x4,
1087    FORM_DATA2	= 0x5,
1088    FORM_DATA4	= 0x6,
1089    FORM_DATA8	= 0x7,
1090    FORM_STRING	= 0x8
1091};
1092
1093/* Attribute names and codes.  */
1094
1095enum dwarf_attribute {
1096    AT_sibling			= (0x0010|FORM_REF),
1097    AT_location			= (0x0020|FORM_BLOCK2),
1098    AT_name			= (0x0030|FORM_STRING),
1099    AT_fund_type		= (0x0050|FORM_DATA2),
1100    AT_mod_fund_type		= (0x0060|FORM_BLOCK2),
1101    AT_user_def_type		= (0x0070|FORM_REF),
1102    AT_mod_u_d_type		= (0x0080|FORM_BLOCK2),
1103    AT_ordering			= (0x0090|FORM_DATA2),
1104    AT_subscr_data		= (0x00a0|FORM_BLOCK2),
1105    AT_byte_size		= (0x00b0|FORM_DATA4),
1106    AT_bit_offset		= (0x00c0|FORM_DATA2),
1107    AT_bit_size			= (0x00d0|FORM_DATA4),
1108				/* (0x00e0|FORM_xxxx) -- reserved */
1109    AT_element_list		= (0x00f0|FORM_BLOCK4),
1110    AT_stmt_list		= (0x0100|FORM_DATA4),
1111    AT_low_pc			= (0x0110|FORM_ADDR),
1112    AT_high_pc			= (0x0120|FORM_ADDR),
1113    AT_language			= (0x0130|FORM_DATA4),
1114    AT_member			= (0x0140|FORM_REF),
1115    AT_discr			= (0x0150|FORM_REF),
1116    AT_discr_value		= (0x0160|FORM_BLOCK2),
1117				/* (0x0170|FORM_xxxx) -- reserved */
1118				/* (0x0180|FORM_xxxx) -- reserved */
1119    AT_string_length		= (0x0190|FORM_BLOCK2),
1120    AT_common_reference		= (0x01a0|FORM_REF),
1121    AT_comp_dir			= (0x01b0|FORM_STRING),
1122        AT_const_value_string	= (0x01c0|FORM_STRING),
1123        AT_const_value_data2	= (0x01c0|FORM_DATA2),
1124        AT_const_value_data4	= (0x01c0|FORM_DATA4),
1125        AT_const_value_data8	= (0x01c0|FORM_DATA8),
1126        AT_const_value_block2	= (0x01c0|FORM_BLOCK2),
1127        AT_const_value_block4	= (0x01c0|FORM_BLOCK4),
1128    AT_containing_type		= (0x01d0|FORM_REF),
1129        AT_default_value_addr	= (0x01e0|FORM_ADDR),
1130        AT_default_value_data2	= (0x01e0|FORM_DATA2),
1131        AT_default_value_data4	= (0x01e0|FORM_DATA4),
1132        AT_default_value_data8	= (0x01e0|FORM_DATA8),
1133        AT_default_value_string	= (0x01e0|FORM_STRING),
1134    AT_friends			= (0x01f0|FORM_BLOCK2),
1135    AT_inline			= (0x0200|FORM_STRING),
1136    AT_is_optional		= (0x0210|FORM_STRING),
1137        AT_lower_bound_ref	= (0x0220|FORM_REF),
1138        AT_lower_bound_data2	= (0x0220|FORM_DATA2),
1139        AT_lower_bound_data4	= (0x0220|FORM_DATA4),
1140        AT_lower_bound_data8	= (0x0220|FORM_DATA8),
1141    AT_private			= (0x0240|FORM_STRING),
1142    AT_producer			= (0x0250|FORM_STRING),
1143    AT_program			= (0x0230|FORM_STRING),
1144    AT_protected		= (0x0260|FORM_STRING),
1145    AT_prototyped		= (0x0270|FORM_STRING),
1146    AT_public			= (0x0280|FORM_STRING),
1147    AT_pure_virtual		= (0x0290|FORM_STRING),
1148    AT_return_addr		= (0x02a0|FORM_BLOCK2),
1149    AT_abstract_origin		= (0x02b0|FORM_REF),
1150    AT_start_scope		= (0x02c0|FORM_DATA4),
1151    AT_stride_size		= (0x02e0|FORM_DATA4),
1152        AT_upper_bound_ref	= (0x02f0|FORM_REF),
1153        AT_upper_bound_data2	= (0x02f0|FORM_DATA2),
1154        AT_upper_bound_data4	= (0x02f0|FORM_DATA4),
1155        AT_upper_bound_data8	= (0x02f0|FORM_DATA8),
1156    AT_virtual			= (0x0300|FORM_STRING),
1157
1158    /* GNU extensions.  */
1159
1160    AT_sf_names			= (0x8000|FORM_DATA4),
1161    AT_src_info			= (0x8010|FORM_DATA4),
1162    AT_mac_info			= (0x8020|FORM_DATA4),
1163    AT_src_coords		= (0x8030|FORM_DATA4),
1164    AT_body_begin		= (0x8040|FORM_ADDR),
1165    AT_body_end			= (0x8050|FORM_ADDR)
1166};
1167
1168/* end of enums taken from gdb-6.0 sources */
1169
1170void ML_(read_debuginfo_dwarf1) (
1171        struct _SegInfo* si,
1172        UChar* dwarf1d, Int dwarf1d_sz,
1173        UChar* dwarf1l, Int dwarf1l_sz )
1174{
1175   UInt   stmt_list;
1176   Bool   stmt_list_found;
1177   Int    die_offset, die_szb, at_offset;
1178   UShort die_kind, at_kind;
1179   UChar* at_base;
1180   UChar* src_filename;
1181
1182   if (0)
1183      VG_(printf)("read_debuginfo_dwarf1 ( %p, %d, %p, %d )\n",
1184	          dwarf1d, dwarf1d_sz, dwarf1l, dwarf1l_sz );
1185
1186   /* This loop scans the DIEs. */
1187   die_offset = 0;
1188   while (True) {
1189      if (die_offset >= dwarf1d_sz) break;
1190
1191      die_szb  = *(Int*)(dwarf1d + die_offset);
1192      die_kind = *(UShort*)(dwarf1d + die_offset + 4);
1193
1194      /* We're only interested in compile_unit DIEs; ignore others. */
1195      if (die_kind != TAG_compile_unit) {
1196         die_offset += die_szb;
1197         continue;
1198      }
1199
1200      if (0)
1201         VG_(printf)("compile-unit DIE: offset %d, tag 0x%x, size %d\n",
1202                     die_offset, (Int)die_kind, die_szb );
1203
1204      /* We've got a compile_unit DIE starting at (dwarf1d +
1205         die_offset+6).  Try and find the AT_name and AT_stmt_list
1206         attributes.  Then, finally, we can read the line number info
1207         for this source file. */
1208
1209      /* The next 3 are set as we find the relevant attrs. */
1210      src_filename    = NULL;
1211      stmt_list_found = False;
1212      stmt_list       = 0;
1213
1214      /* This loop scans the Attrs inside compile_unit DIEs. */
1215      at_base = dwarf1d + die_offset + 6;
1216      at_offset = 0;
1217      while (True) {
1218         if (at_offset >= die_szb-6) break;
1219
1220         at_kind = *(UShort*)(at_base + at_offset);
1221         if (0) VG_(printf)("atoffset %d, attag 0x%x\n",
1222                            at_offset, (Int)at_kind );
1223         at_offset += 2; /* step over the attribute itself */
1224	 /* We have to examine the attribute to figure out its
1225            length. */
1226         switch (at_kind) {
1227            case AT_stmt_list:
1228            case AT_language:
1229            case AT_sibling:
1230               if (at_kind == AT_stmt_list) {
1231                  stmt_list_found = True;
1232                  stmt_list = *(Int*)(at_base+at_offset);
1233               }
1234               at_offset += 4; break;
1235            case AT_high_pc:
1236            case AT_low_pc:
1237               at_offset += sizeof(void*); break;
1238            case AT_name:
1239            case AT_producer:
1240            case AT_comp_dir:
1241               /* Zero terminated string, step over it. */
1242               if (at_kind == AT_name)
1243                  src_filename = at_base + at_offset;
1244               while (at_offset < die_szb-6 && at_base[at_offset] != 0)
1245                  at_offset++;
1246               at_offset++;
1247               break;
1248            default:
1249               VG_(printf)("Unhandled DWARF-1 attribute 0x%x\n",
1250                           (Int)at_kind );
1251               VG_(core_panic)("Unhandled DWARF-1 attribute");
1252         } /* switch (at_kind) */
1253      } /* looping over attributes */
1254
1255      /* So, did we find the required stuff for a line number table in
1256         this DIE?  If yes, read it. */
1257      if (stmt_list_found /* there is a line number table */
1258          && src_filename != NULL /* we know the source filename */
1259         ) {
1260         /* Table starts:
1261               Length:
1262                  4 bytes, includes the entire table
1263               Base address:
1264                  unclear (4? 8?), assuming native pointer size here.
1265            Then a sequence of triples
1266               (source line number -- 32 bits
1267                source line column -- 16 bits
1268                address delta -- 32 bits)
1269	 */
1270         Addr   base;
1271	 Int    len;
1272         Char*  curr_filenm;
1273         UChar* ptr;
1274         UInt   prev_line, prev_delta;
1275
1276         curr_filenm = ML_(addStr) ( si, src_filename, -1 );
1277         prev_line = prev_delta = 0;
1278
1279         ptr = dwarf1l + stmt_list;
1280         len  =        *(Int*)ptr;    ptr += sizeof(Int);
1281         base = (Addr)(*(void**)ptr); ptr += sizeof(void*);
1282         len -= (sizeof(Int) + sizeof(void*));
1283         while (len > 0) {
1284            UInt   line;
1285            UShort col;
1286            UInt   delta;
1287            line = *(UInt*)ptr;  ptr += sizeof(UInt);
1288            col = *(UShort*)ptr;  ptr += sizeof(UShort);
1289            delta = *(UShort*)ptr;  ptr += sizeof(UInt);
1290	    if (0) VG_(printf)("line %d, col %d, delta %d\n",
1291                               line, (Int)col, delta );
1292            len -= (sizeof(UInt) + sizeof(UShort) + sizeof(UInt));
1293
1294	    if (delta > 0 && prev_line > 0) {
1295	       if (0) VG_(printf) ("     %d  %d-%d\n",
1296                                   prev_line, prev_delta, delta-1);
1297	       ML_(addLineInfo) ( si, curr_filenm, NULL,
1298		 	          base + prev_delta, base + delta,
1299			          prev_line, 0 );
1300	    }
1301	    prev_line = line;
1302	    prev_delta = delta;
1303	 }
1304      }
1305
1306      /* Move on the the next DIE. */
1307      die_offset += die_szb;
1308
1309   } /* Looping over DIEs */
1310
1311}
1312
1313
1314/*------------------------------------------------------------*/
1315/*--- Read call-frame info from an .eh_frame section       ---*/
1316/*------------------------------------------------------------*/
1317
1318/* Useful info ..
1319
1320   In general:
1321   gdb-6.3/gdb/dwarf2-frame.c
1322
1323   gdb-6.3/gdb/i386-tdep.c:
1324
1325   DWARF2/GCC uses the stack address *before* the function call as a
1326   frame's CFA.  [jrs: I presume this means %esp before the call as
1327   the CFA].
1328
1329   JRS: on amd64, the dwarf register numbering is, as per
1330   gdb-6.3/gdb/tdep-amd64.c and also amd64-abi-0.95.pdf:
1331
1332      0    1    2    3    4    5    6    7
1333      RAX  RDX  RCX  RBX  RSI  RDI  RBP  RSP
1334
1335      8  ...  15
1336      R8 ... R15
1337
1338      16 is the return address (RIP)
1339
1340   This is pretty strange given this not the encoding scheme for
1341   registers used in amd64 code.
1342
1343   On x86 I cannot find any documentation.  It _appears_ to be the
1344   actual instruction encoding, viz:
1345
1346      0    1    2    3    4    5    6    7
1347      EAX  ECX  EDX  EBX  ESP  EBP  ESI  EDI
1348
1349      8 is the return address (EIP) */
1350
1351/* Note that we don't support DWARF3 expressions (DW_CFA_expression,
1352   DW_CFA_def_cfa_expression, DW_CFA_val_expression).  The code just
1353   reads over them and ignores them.
1354
1355   Note also, does not support the 64-bit DWARF format (only known
1356   compiler that generates it so far is IBM's xlc/xlC/xlf suite).
1357   Only handles 32-bit DWARF.
1358*/
1359
1360/* Comments re DW_CFA_set_loc, 16 Nov 06.
1361
1362   JRS:
1363   Someone recently sent me a libcrypto.so.0.9.8 as distributed with
1364   Ubuntu of some flavour, compiled with gcc 4.1.2 on amd64.  It
1365   causes V's CF reader to complain a lot:
1366
1367   >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1368   >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1369   >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1370   >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1371   >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:48
1372   >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1373
1374   After chasing this around a bit it seems that the CF bytecode
1375   parser lost sync at a DW_CFA_set_loc, which has a single argument
1376   denoting an address.
1377
1378   As it stands that address is extracted by read_Addr().  On amd64
1379   that just fetches 8 bytes regardless of anything else.
1380
1381   read_encoded_Addr() is more sophisticated.  This appears to take
1382   into account some kind of encoding flag.  When I replace the uses
1383   of read_Addr by read_encoded_Addr for DW_CFA_set_loc, the
1384   complaints go away, there is no loss of sync, and the parsed CF
1385   instructions are the same as shown by readelf --debug-dump=frames.
1386
1387   So it seems a plausible fix.  The problem is I looked in the DWARF3
1388   spec and completely failed to figure out whether or not the arg to
1389   DW_CFA_set_loc is supposed to be encoded in a way suitable for
1390   read_encoded_Addr, nor for that matter any description of what it
1391   is that read_encoded_Addr is really decoding.
1392
1393   TomH:
1394   The problem is that the encoding is not standard - the eh_frame
1395   section uses the same encoding as the dwarf_frame section except
1396   for a few small changes, and this is one of them. So this is not
1397   something the DWARF standard covers.
1398
1399   There is an augmentation string to indicate what is going on though
1400   so that programs can recognise it.
1401
1402   What we are doing seems to match what gdb 6.5 and libdwarf 20060614
1403   do though. I'm not sure about readelf though.
1404
1405   (later): Well dwarfdump barfs on it:
1406
1407      dwarfdump ERROR:  dwarf_get_fde_info_for_reg:
1408                        DW_DLE_DF_FRAME_DECODING_ERROR(193) (193)
1409
1410   I've looked at binutils as well now, and the code in readelf agrees
1411   with your patch - ie it treats set_loc as having an encoded address
1412   if there is a zR augmentation indicating an encoding.
1413
1414   Quite why gdb and libdwarf don't understand this is an interesting
1415   question...
1416
1417   Final outcome: all uses of read_Addr were replaced by
1418   read_encoded_Addr.  A new type AddressDecodingInfo was added to
1419   make it relatively clean to plumb through the extra info needed by
1420   read_encoded_Addr.
1421*/
1422
1423/* --------------- Decls --------------- */
1424
1425#if defined(VGP_x86_linux)
1426#  define FP_REG         5
1427#  define SP_REG         4
1428#  define RA_REG_DEFAULT 8
1429#elif defined(VGP_amd64_linux)
1430#  define FP_REG         6
1431#  define SP_REG         7
1432#  define RA_REG_DEFAULT 16
1433#elif defined(VGP_ppc32_linux)
1434#  define FP_REG         1
1435#  define SP_REG         1
1436#  define RA_REG_DEFAULT 8     // CAB: What's a good default ?
1437#elif defined(VGP_ppc64_linux)
1438#  define FP_REG         1
1439#  define SP_REG         1
1440#  define RA_REG_DEFAULT 8     // CAB: What's a good default ?
1441#else
1442#  error Unknown platform
1443#endif
1444
1445/* the number of regs we are prepared to unwind */
1446#define N_CFI_REGS 20
1447
1448/* Instructions for the automaton */
1449enum dwarf_cfa_primary_ops
1450  {
1451    DW_CFA_use_secondary = 0,
1452    DW_CFA_advance_loc   = 1,
1453    DW_CFA_offset        = 2,
1454    DW_CFA_restore       = 3
1455  };
1456
1457enum dwarf_cfa_secondary_ops
1458  {
1459    DW_CFA_nop                = 0x00,
1460    DW_CFA_set_loc            = 0x01,
1461    DW_CFA_advance_loc1       = 0x02,
1462    DW_CFA_advance_loc2       = 0x03,
1463    DW_CFA_advance_loc4       = 0x04,
1464    DW_CFA_offset_extended    = 0x05,
1465    DW_CFA_restore_extended   = 0x06,
1466    DW_CFA_undefined          = 0x07,
1467    DW_CFA_same_value         = 0x08,
1468    DW_CFA_register           = 0x09,
1469    DW_CFA_remember_state     = 0x0a,
1470    DW_CFA_restore_state      = 0x0b,
1471    DW_CFA_def_cfa            = 0x0c,
1472    DW_CFA_def_cfa_register   = 0x0d,
1473    DW_CFA_def_cfa_offset     = 0x0e,
1474    DW_CFA_def_cfa_expression = 0x0f, /* DWARF3 only */
1475    DW_CFA_expression         = 0x10, /* DWARF3 only */
1476    DW_CFA_offset_extended_sf = 0x11, /* DWARF3 only */
1477    DW_CFA_def_cfa_sf         = 0x12, /* DWARF3 only */
1478    DW_CFA_def_cfa_offset_sf  = 0x13, /* DWARF3 only */
1479    DW_CFA_val_offset         = 0x14, /* DWARF3 only */
1480    DW_CFA_val_offset_sf      = 0x15, /* DWARF3 only */
1481    DW_CFA_val_expression     = 0x16, /* DWARF3 only */
1482    DW_CFA_lo_user            = 0x1c,
1483    DW_CFA_GNU_window_save    = 0x2d, /* GNU extension */
1484    DW_CFA_GNU_args_size      = 0x2e, /* GNU extension */
1485    DW_CFA_GNU_negative_offset_extended = 0x2f, /* GNU extension */
1486    DW_CFA_hi_user            = 0x3f
1487  };
1488
1489#define DW_EH_PE_absptr		0x00
1490#define DW_EH_PE_omit		0xff
1491
1492#define DW_EH_PE_uleb128	0x01
1493#define DW_EH_PE_udata2		0x02
1494#define DW_EH_PE_udata4		0x03
1495#define DW_EH_PE_udata8		0x04
1496#define DW_EH_PE_sleb128	0x09
1497#define DW_EH_PE_sdata2		0x0A
1498#define DW_EH_PE_sdata4		0x0B
1499#define DW_EH_PE_sdata8		0x0C
1500#define DW_EH_PE_signed		0x08
1501
1502#define DW_EH_PE_pcrel		0x10
1503#define DW_EH_PE_textrel	0x20
1504#define DW_EH_PE_datarel	0x30
1505#define DW_EH_PE_funcrel	0x40
1506#define DW_EH_PE_aligned	0x50
1507
1508#define DW_EH_PE_indirect	0x80
1509
1510
1511/* RegRule and UnwindContext are used temporarily to do the unwinding.
1512   The result is then summarised into a sequence of CfiSIs, if
1513   possible.  UnwindContext effectively holds the state of the
1514   abstract machine whilst it is running.
1515*/
1516typedef
1517   struct {
1518      enum { RR_Undef, RR_Same, RR_CFAoff, RR_Reg, RR_Arch, RR_Expr,
1519	     RR_CFAValoff, RR_ValExpr } tag;
1520
1521      /* Note, .coff and .reg are never both in use.  Therefore could
1522         merge them into one. */
1523
1524      /* CFA offset if tag==RR_CFAoff */
1525      Int coff;
1526
1527      /* reg, if tag==RR_Reg */
1528      Int reg;
1529   }
1530   RegRule;
1531
1532static void ppRegRule ( RegRule* reg )
1533{
1534   switch (reg->tag) {
1535      case RR_Undef:  VG_(printf)("u  "); break;
1536      case RR_Same:   VG_(printf)("s  "); break;
1537      case RR_CFAoff: VG_(printf)("c%d ", reg->coff); break;
1538      case RR_CFAValoff: VG_(printf)("v%d ", reg->coff); break;
1539      case RR_Reg:    VG_(printf)("r%d ", reg->reg); break;
1540      case RR_Arch:   VG_(printf)("a  "); break;
1541      case RR_Expr:   VG_(printf)("e  "); break;
1542      case RR_ValExpr:   VG_(printf)("ve "); break;
1543      default:        VG_(core_panic)("ppRegRule");
1544   }
1545}
1546
1547
1548typedef
1549   struct {
1550      /* Read-only fields (set by the CIE) */
1551      Int  code_a_f;
1552      Int  data_a_f;
1553      Addr initloc;
1554      Int  ra_reg;
1555      /* The rest of these fields can be modifed by
1556         run_CF_instruction. */
1557      /* The LOC entry */
1558      Addr loc;
1559      /* The CFA entry.  If -1, means we don't know (Dwarf3 Expression). */
1560      Int cfa_reg;
1561      Int cfa_offset; /* in bytes */
1562      /* register unwind rules */
1563      RegRule reg[N_CFI_REGS];
1564   }
1565   UnwindContext;
1566
1567static void ppUnwindContext ( UnwindContext* ctx )
1568{
1569   Int i;
1570   VG_(printf)("0x%llx: ", (ULong)ctx->loc);
1571   VG_(printf)("%d(r%d) ",  ctx->cfa_offset, ctx->cfa_reg);
1572   for (i = 0; i < N_CFI_REGS; i++)
1573      ppRegRule(&ctx->reg[i]);
1574   VG_(printf)("\n");
1575}
1576
1577static void initUnwindContext ( /*OUT*/UnwindContext* ctx )
1578{
1579   Int i;
1580   ctx->code_a_f   = 0;
1581   ctx->data_a_f   = 0;
1582   ctx->initloc    = 0;
1583   ctx->ra_reg     = RA_REG_DEFAULT;
1584   ctx->loc        = 0;
1585   ctx->cfa_reg    = 0;
1586   ctx->cfa_offset = 0;
1587   for (i = 0; i < N_CFI_REGS; i++) {
1588      ctx->reg[i].tag = RR_Undef;
1589      ctx->reg[i].coff = 0;
1590      ctx->reg[i].reg = 0;
1591   }
1592}
1593
1594
1595/* A structure which holds information needed by read_encoded_Addr().
1596   Not sure what these address-like fields are -- really ought to
1597   distinguish properly svma/avma/image addresses.
1598*/
1599typedef
1600   struct {
1601      UChar  encoding;
1602      UChar* ehframe;
1603      Addr   ehframe_addr;
1604   }
1605   AddressDecodingInfo;
1606
1607
1608/* ------------ Deal with summary-info records ------------ */
1609
1610static void initCfiSI ( DiCfSI* si )
1611{
1612   si->base      = 0;
1613   si->len       = 0;
1614   si->cfa_sprel = False;
1615   si->ra_how    = 0;
1616   si->sp_how    = 0;
1617   si->fp_how    = 0;
1618   si->cfa_off   = 0;
1619   si->ra_off    = 0;
1620   si->sp_off    = 0;
1621   si->fp_off    = 0;
1622}
1623
1624
1625/* --------------- Summarisation --------------- */
1626
1627/* Summarise ctx into si, if possible.  Returns True if successful.
1628   This is taken to be just after ctx's loc advances; hence the
1629   summary is up to but not including the current loc.  This works
1630   on both x86 and amd64.
1631*/
1632static Bool summarise_context( /*OUT*/DiCfSI* si,
1633                               Addr loc_start,
1634	                       UnwindContext* ctx )
1635{
1636   Int why = 0;
1637   initCfiSI(si);
1638
1639   /* How to generate the CFA */
1640   if (ctx->cfa_reg == -1) {
1641      /* it was set by DW_CFA_def_cfa_expression; we don't know what
1642         it really is */
1643      why = 6;
1644      goto failed;
1645   } else
1646   if (ctx->cfa_reg == SP_REG) {
1647      si->cfa_sprel = True;
1648      si->cfa_off   = ctx->cfa_offset;
1649   } else
1650   if (ctx->cfa_reg == FP_REG) {
1651      si->cfa_sprel = False;
1652      si->cfa_off   = ctx->cfa_offset;
1653   } else {
1654      why = 1;
1655      goto failed;
1656   }
1657
1658#  define SUMMARISE_HOW(_how, _off, _ctxreg)                             \
1659   switch (_ctxreg.tag) {                                                \
1660      case RR_Undef:  _how = CFIR_UNKNOWN;   _off = 0; break;            \
1661      case RR_Same:   _how = CFIR_SAME;      _off = 0; break;            \
1662      case RR_CFAoff: _how = CFIR_MEMCFAREL; _off = _ctxreg.coff; break; \
1663      case RR_CFAValoff: _how = CFIR_CFAREL; _off = _ctxreg.coff; break; \
1664      default:        { why = 2; goto failed; } /* otherwise give up */  \
1665   }
1666
1667   SUMMARISE_HOW(si->ra_how, si->ra_off, ctx->reg[ctx->ra_reg] );
1668   SUMMARISE_HOW(si->fp_how, si->fp_off, ctx->reg[FP_REG] );
1669
1670#  undef SUMMARISE_HOW
1671
1672   /* on x86/amd64, it seems the old %{e,r}sp value before the call is
1673      always the same as the CFA.  Therefore ... */
1674   si->sp_how = CFIR_CFAREL;
1675   si->sp_off = 0;
1676
1677   /* also, gcc says "Undef" for %{e,r}bp when it is unchanged.  So
1678      .. */
1679   if (ctx->reg[FP_REG].tag == RR_Undef)
1680      si->fp_how = CFIR_SAME;
1681
1682   /* knock out some obviously stupid cases */
1683   if (si->ra_how == CFIR_SAME)
1684      { why = 3; goto failed; }
1685
1686   /* bogus looking range?  Note, we require that the difference is
1687      representable in 32 bits. */
1688   if (loc_start >= ctx->loc)
1689      { why = 4; goto failed; }
1690   if (ctx->loc - loc_start > 10000000 /* let's say */)
1691      { why = 5; goto failed; }
1692
1693   si->base = loc_start + ctx->initloc;
1694   si->len  = (UInt)(ctx->loc - loc_start);
1695
1696   return True;
1697
1698  failed:
1699   if (VG_(clo_verbosity) > 2 || VG_(clo_trace_cfi)) {
1700      VG_(message)(Vg_DebugMsg,
1701                  "summarise_context(loc_start = %p)"
1702                  ": cannot summarise(why=%d):   ", loc_start, why);
1703      ppUnwindContext(ctx);
1704   }
1705   return False;
1706}
1707
1708static void ppUnwindContext_summary ( UnwindContext* ctx )
1709{
1710   VG_(printf)("0x%llx-1: ", (ULong)ctx->loc);
1711
1712   if (ctx->cfa_reg == SP_REG) {
1713      VG_(printf)("SP/CFA=%d+SP   ", ctx->cfa_offset);
1714   } else
1715   if (ctx->cfa_reg == FP_REG) {
1716      VG_(printf)("SP/CFA=%d+FP   ", ctx->cfa_offset);
1717   } else {
1718      VG_(printf)("SP/CFA=unknown  ", ctx->cfa_offset);
1719   }
1720
1721   VG_(printf)("RA=");
1722   ppRegRule( &ctx->reg[ctx->ra_reg] );
1723
1724   VG_(printf)("FP=");
1725   ppRegRule( &ctx->reg[FP_REG] );
1726   VG_(printf)("\n");
1727}
1728
1729
1730/* ------------ Pick apart DWARF2 byte streams ------------ */
1731
1732static inline Bool host_is_little_endian ( void )
1733{
1734   UInt x = 0x76543210;
1735   UChar* p = (UChar*)(&x);
1736   return toBool(*p == 0x10);
1737}
1738
1739static Short read_Short ( UChar* data )
1740{
1741   Short r = 0;
1742   vg_assert(host_is_little_endian());
1743   r = data[0]
1744       | ( ((UInt)data[1]) << 8 );
1745   return r;
1746}
1747
1748static Int read_Int ( UChar* data )
1749{
1750   Int r = 0;
1751   vg_assert(host_is_little_endian());
1752   r = data[0]
1753       | ( ((UInt)data[1]) << 8 )
1754       | ( ((UInt)data[2]) << 16 )
1755       | ( ((UInt)data[3]) << 24 );
1756   return r;
1757}
1758
1759static Long read_Long ( UChar* data )
1760{
1761   Long r = 0;
1762   vg_assert(host_is_little_endian());
1763   r = data[0]
1764       | ( ((ULong)data[1]) << 8 )
1765       | ( ((ULong)data[2]) << 16 )
1766       | ( ((ULong)data[3]) << 24 )
1767       | ( ((ULong)data[4]) << 32 )
1768       | ( ((ULong)data[5]) << 40 )
1769       | ( ((ULong)data[6]) << 48 )
1770       | ( ((ULong)data[7]) << 56 );
1771   return r;
1772}
1773
1774static UShort read_UShort ( UChar* data )
1775{
1776   UInt r = 0;
1777   vg_assert(host_is_little_endian());
1778   r = data[0]
1779       | ( ((UInt)data[1]) << 8 );
1780   return r;
1781}
1782
1783static UInt read_UInt ( UChar* data )
1784{
1785   UInt r = 0;
1786   vg_assert(host_is_little_endian());
1787   r = data[0]
1788       | ( ((UInt)data[1]) << 8 )
1789       | ( ((UInt)data[2]) << 16 )
1790       | ( ((UInt)data[3]) << 24 );
1791   return r;
1792}
1793
1794static ULong read_ULong ( UChar* data )
1795{
1796   ULong r = 0;
1797   vg_assert(host_is_little_endian());
1798   r = data[0]
1799       | ( ((ULong)data[1]) << 8 )
1800       | ( ((ULong)data[2]) << 16 )
1801       | ( ((ULong)data[3]) << 24 )
1802       | ( ((ULong)data[4]) << 32 )
1803       | ( ((ULong)data[5]) << 40 )
1804       | ( ((ULong)data[6]) << 48 )
1805       | ( ((ULong)data[7]) << 56 );
1806   return r;
1807}
1808
1809static UChar read_UChar ( UChar* data )
1810{
1811   return data[0];
1812}
1813
1814static UChar default_Addr_encoding ( void )
1815{
1816   switch (sizeof(Addr)) {
1817      case 4: return DW_EH_PE_udata4;
1818      case 8: return DW_EH_PE_udata8;
1819      default: vg_assert(0);
1820   }
1821}
1822
1823static UInt size_of_encoded_Addr ( UChar encoding )
1824{
1825   if (encoding == DW_EH_PE_omit)
1826      return 0;
1827
1828   switch (encoding & 0x07) {
1829      case DW_EH_PE_absptr: return sizeof(Addr);
1830      case DW_EH_PE_udata2: return sizeof(UShort);
1831      case DW_EH_PE_udata4: return sizeof(UInt);
1832      case DW_EH_PE_udata8: return sizeof(ULong);
1833      default: vg_assert(0);
1834   }
1835}
1836
1837static Addr read_encoded_Addr ( /*OUT*/Int* nbytes,
1838                                AddressDecodingInfo* adi,
1839                                UChar* data )
1840{
1841   Addr   base;
1842   Int    offset;
1843   UChar  encoding     = adi->encoding;
1844   UChar* ehframe      = adi->ehframe;
1845   Addr   ehframe_addr = adi->ehframe_addr;
1846
1847   vg_assert((encoding & DW_EH_PE_indirect) == 0);
1848
1849   *nbytes = 0;
1850
1851   switch (encoding & 0x70) {
1852      case DW_EH_PE_absptr:
1853         base = 0;
1854         break;
1855      case DW_EH_PE_pcrel:
1856         base = ehframe_addr + ( data - ehframe );
1857         break;
1858      case DW_EH_PE_datarel:
1859         vg_assert(0);
1860         base = /* data base address */ 0;
1861         break;
1862      case DW_EH_PE_textrel:
1863         vg_assert(0);
1864         base = /* text base address */ 0;
1865         break;
1866      case DW_EH_PE_funcrel:
1867         base = 0;
1868         break;
1869      case DW_EH_PE_aligned:
1870         base = 0;
1871         offset = data - ehframe;
1872         if ((offset % sizeof(Addr)) != 0) {
1873            *nbytes = sizeof(Addr) - (offset % sizeof(Addr));
1874            data += *nbytes;
1875         }
1876         break;
1877      default:
1878         vg_assert(0);
1879   }
1880
1881   if ((encoding & 0x07) == 0x00)
1882      encoding |= default_Addr_encoding();
1883
1884   switch (encoding & 0x0f) {
1885      case DW_EH_PE_udata2:
1886         *nbytes += sizeof(UShort);
1887         return base + read_UShort(data);
1888      case DW_EH_PE_udata4:
1889         *nbytes += sizeof(UInt);
1890         return base + read_UInt(data);
1891      case DW_EH_PE_udata8:
1892         *nbytes += sizeof(ULong);
1893         return base + read_ULong(data);
1894      case DW_EH_PE_sdata2:
1895         *nbytes += sizeof(Short);
1896         return base + read_Short(data);
1897      case DW_EH_PE_sdata4:
1898         *nbytes += sizeof(Int);
1899         return base + read_Int(data);
1900      case DW_EH_PE_sdata8:
1901         *nbytes += sizeof(Long);
1902         return base + read_Long(data);
1903      default:
1904         vg_assert2(0, "read encoded address %d\n", encoding & 0x0f);
1905   }
1906}
1907
1908
1909/* ------------ Run/show CFI instructions ------------ */
1910
1911/* Run a CFI instruction, and also return its length.
1912   Returns 0 if the instruction could not be executed.
1913*/
1914static Int run_CF_instruction ( /*MOD*/UnwindContext* ctx,
1915                                UChar* instr,
1916                                UnwindContext* restore_ctx,
1917                                AddressDecodingInfo* adi )
1918{
1919   Int   off, reg, reg2, nleb, len;
1920   UInt  delta;
1921   Int   i   = 0;
1922   UChar hi2 = (instr[i] >> 6) & 3;
1923   UChar lo6 = instr[i] & 0x3F;
1924   i++;
1925
1926   if (hi2 == DW_CFA_advance_loc) {
1927      delta = (UInt)lo6;
1928      ctx->loc += delta;
1929      return i;
1930   }
1931
1932   if (hi2 == DW_CFA_offset) {
1933      /* Set rule for reg 'lo6' to CFAoffset(off * data_af) */
1934      off = read_leb128( &instr[i], &nleb, 0 );
1935      i += nleb;
1936      reg = (Int)lo6;
1937      if (reg < 0 || reg >= N_CFI_REGS)
1938         return 0; /* fail */
1939      ctx->reg[reg].tag = RR_CFAoff;
1940      ctx->reg[reg].coff = off * ctx->data_a_f;
1941      return i;
1942   }
1943
1944   if (hi2 == DW_CFA_restore) {
1945      reg = (Int)lo6;
1946      if (reg < 0 || reg >= N_CFI_REGS)
1947         return 0; /* fail */
1948      if (restore_ctx == NULL)
1949         return 0; /* fail */
1950      ctx->reg[reg] = restore_ctx->reg[reg];
1951      return i;
1952   }
1953
1954   vg_assert(hi2 == DW_CFA_use_secondary);
1955
1956   switch (lo6) {
1957      case DW_CFA_nop:
1958         break;
1959      case DW_CFA_set_loc:
1960         /* WAS:
1961            ctx->loc = read_Addr(&instr[i]) - ctx->initloc; i+= sizeof(Addr);
1962            Was this ever right? */
1963         ctx->loc = read_encoded_Addr(&len, adi, &instr[i]);
1964         i += len;
1965         break;
1966      case DW_CFA_advance_loc1:
1967         delta = (UInt)read_UChar(&instr[i]); i+= sizeof(UChar);
1968         ctx->loc += delta;
1969         break;
1970      case DW_CFA_advance_loc2:
1971         delta = (UInt)read_UShort(&instr[i]); i+= sizeof(UShort);
1972         ctx->loc += delta;
1973         break;
1974      case DW_CFA_advance_loc4:
1975         delta = (UInt)read_UInt(&instr[i]); i+= sizeof(UInt);
1976         ctx->loc += delta;
1977         break;
1978
1979      case DW_CFA_def_cfa:
1980         reg = read_leb128( &instr[i], &nleb, 0 );
1981         i += nleb;
1982         off = read_leb128( &instr[i], &nleb, 0 );
1983         i += nleb;
1984         if (reg < 0 || reg >= N_CFI_REGS)
1985            return 0; /* fail */
1986         ctx->cfa_reg    = reg;
1987         ctx->cfa_offset = off;
1988         break;
1989
1990      case DW_CFA_def_cfa_sf:
1991         reg = read_leb128( &instr[i], &nleb, 0 );
1992         i += nleb;
1993         off = read_leb128( &instr[i], &nleb, 1 );
1994         i += nleb;
1995         if (reg < 0 || reg >= N_CFI_REGS)
1996            return 0; /* fail */
1997         ctx->cfa_reg    = reg;
1998         ctx->cfa_offset = off;
1999         break;
2000
2001      case DW_CFA_register:
2002         reg = read_leb128( &instr[i], &nleb, 0);
2003         i += nleb;
2004         reg2 = read_leb128( &instr[i], &nleb, 0);
2005         i += nleb;
2006         if (reg < 0 || reg >= N_CFI_REGS)
2007            return 0; /* fail */
2008         if (reg2 < 0 || reg2 >= N_CFI_REGS)
2009            return 0; /* fail */
2010         ctx->reg[reg].tag = RR_Reg;
2011         ctx->reg[reg].reg = reg2;
2012         break;
2013
2014      case DW_CFA_offset_extended:
2015         reg = read_leb128( &instr[i], &nleb, 0 );
2016         i += nleb;
2017         off = read_leb128( &instr[i], &nleb, 0 );
2018         i += nleb;
2019         if (reg < 0 || reg >= N_CFI_REGS)
2020            return 0; /* fail */
2021         ctx->reg[reg].tag = RR_CFAoff;
2022         ctx->reg[reg].coff = off * ctx->data_a_f;
2023         break;
2024
2025      case DW_CFA_offset_extended_sf:
2026         reg = read_leb128( &instr[i], &nleb, 0 );
2027         i += nleb;
2028         off = read_leb128( &instr[i], &nleb, 1 );
2029         i += nleb;
2030         if (reg < 0 || reg >= N_CFI_REGS)
2031            return 0; /* fail */
2032         ctx->reg[reg].tag = RR_CFAoff;
2033         ctx->reg[reg].coff = off * ctx->data_a_f;
2034         break;
2035
2036      case DW_CFA_GNU_negative_offset_extended:
2037         reg = read_leb128( &instr[i], &nleb, 0 );
2038         i += nleb;
2039         off = read_leb128( &instr[i], &nleb, 0 );
2040         i += nleb;
2041         if (reg < 0 || reg >= N_CFI_REGS)
2042            return 0; /* fail */
2043         ctx->reg[reg].tag = RR_CFAoff;
2044         ctx->reg[reg].coff = -off * ctx->data_a_f;
2045         break;
2046
2047      case DW_CFA_restore_extended:
2048         reg = read_leb128( &instr[i], &nleb, 0 );
2049         i += nleb;
2050         if (reg < 0 || reg >= N_CFI_REGS)
2051            return 0; /* fail */
2052	 if (restore_ctx == NULL)
2053	    return 0; /* fail */
2054	 ctx->reg[reg] = restore_ctx->reg[reg];
2055         break;
2056
2057      case DW_CFA_val_offset:
2058         reg = read_leb128( &instr[i], &nleb, 0 );
2059         i += nleb;
2060         off = read_leb128( &instr[i], &nleb, 0 );
2061         i += nleb;
2062         if (reg < 0 || reg >= N_CFI_REGS)
2063            return 0; /* fail */
2064         ctx->reg[reg].tag = RR_CFAValoff;
2065         ctx->reg[reg].coff = off * ctx->data_a_f;
2066         break;
2067
2068      case DW_CFA_val_offset_sf:
2069         reg = read_leb128( &instr[i], &nleb, 0 );
2070         i += nleb;
2071         off = read_leb128( &instr[i], &nleb, 1 );
2072         i += nleb;
2073         if (reg < 0 || reg >= N_CFI_REGS)
2074            return 0; /* fail */
2075         ctx->reg[reg].tag = RR_CFAValoff;
2076         ctx->reg[reg].coff = off * ctx->data_a_f;
2077         break;
2078
2079      case DW_CFA_def_cfa_register:
2080         reg = read_leb128( &instr[i], &nleb, 0);
2081         i += nleb;
2082         if (reg < 0 || reg >= N_CFI_REGS)
2083            return 0; /* fail */
2084         ctx->cfa_reg = reg;
2085         break;
2086
2087      case DW_CFA_def_cfa_offset:
2088         off = read_leb128( &instr[i], &nleb, 0);
2089         i += nleb;
2090         ctx->cfa_offset = off;
2091         break;
2092
2093      case DW_CFA_def_cfa_offset_sf:
2094         off = read_leb128( &instr[i], &nleb, 1);
2095         i += nleb;
2096         ctx->cfa_offset = off * ctx->data_a_f;
2097         break;
2098
2099      case DW_CFA_GNU_args_size:
2100         /* No idea what is supposed to happen.  gdb-6.3 simply
2101            ignores these. */
2102         off = read_leb128( &instr[i], &nleb, 0 );
2103         i += nleb;
2104         break;
2105
2106      case DW_CFA_expression:
2107         /* Too difficult to really handle; just skip over it and say
2108            that we don't know what do to with the register. */
2109         if (VG_(clo_trace_cfi))
2110            VG_(printf)("DWARF2 CFI reader: "
2111                        "ignoring DW_CFA_expression\n");
2112         reg = read_leb128( &instr[i], &nleb, 0 );
2113         i += nleb;
2114         len = read_leb128( &instr[i], &nleb, 0 );
2115         i += nleb;
2116         i += len;
2117         if (reg < 0 || reg >= N_CFI_REGS)
2118            return 0; /* fail */
2119         ctx->reg[reg].tag = RR_Expr;
2120         break;
2121
2122      case DW_CFA_val_expression:
2123         /* Too difficult to really handle; just skip over it and say
2124            that we don't know what do to with the register. */
2125         if (VG_(clo_trace_cfi))
2126            VG_(printf)("DWARF2 CFI reader: "
2127                        "ignoring DW_CFA_val_expression\n");
2128         reg = read_leb128( &instr[i], &nleb, 0 );
2129         i += nleb;
2130         len = read_leb128( &instr[i], &nleb, 0 );
2131         i += nleb;
2132         i += len;
2133         if (reg < 0 || reg >= N_CFI_REGS)
2134            return 0; /* fail */
2135         ctx->reg[reg].tag = RR_ValExpr;
2136         break;
2137
2138      case DW_CFA_def_cfa_expression:
2139         if (VG_(clo_trace_cfi))
2140            VG_(printf)("DWARF2 CFI reader: "
2141                        "ignoring DW_CFA_def_cfa_expression\n");
2142         len = read_leb128( &instr[i], &nleb, 0 );
2143         i += nleb;
2144         i += len;
2145         ctx->cfa_reg = -1; /* indicating we don't know */
2146         break;
2147
2148      case DW_CFA_GNU_window_save:
2149         /* Ignored.  This appears to be sparc-specific; quite why it
2150            turns up in SuSE-supplied x86 .so's beats me. */
2151         break;
2152
2153      default:
2154         VG_(message)(Vg_DebugMsg, "DWARF2 CFI reader: unhandled CFI "
2155                                   "instruction 0:%d", (Int)lo6);
2156         i = 0;
2157         break;
2158   }
2159
2160   return i;
2161}
2162
2163
2164/* Show a CFI instruction, and also return its length. */
2165
2166static Int show_CF_instruction ( UChar* instr,
2167                                 AddressDecodingInfo* adi )
2168{
2169   UInt  delta;
2170   Int   off, reg, reg2, nleb, len;
2171   Addr  loc;
2172   Int   i   = 0;
2173   UChar hi2 = (instr[i] >> 6) & 3;
2174   UChar lo6 = instr[i] & 0x3F;
2175   i++;
2176
2177   if (0) VG_(printf)("raw:%x/%x:%x:%x:%x:%x:%x:%x:%x:%x\n",
2178                      hi2, lo6,
2179                      instr[i+0], instr[i+1], instr[i+2], instr[i+3],
2180                      instr[i+4], instr[i+5], instr[i+6], instr[i+7] );
2181
2182   if (hi2 == DW_CFA_advance_loc) {
2183      VG_(printf)("DW_CFA_advance_loc(%d)\n", (Int)lo6);
2184      return i;
2185   }
2186
2187   if (hi2 == DW_CFA_offset) {
2188      off = read_leb128( &instr[i], &nleb, 0 );
2189      i += nleb;
2190      VG_(printf)("DW_CFA_offset(r%d + %d x data_af)\n", (Int)lo6, off);
2191      return i;
2192   }
2193
2194   if (hi2 == DW_CFA_restore) {
2195      VG_(printf)("DW_CFA_restore(r%d)\n", (Int)lo6);
2196      return i;
2197   }
2198
2199   vg_assert(hi2 == DW_CFA_use_secondary);
2200
2201   switch (lo6) {
2202
2203      case DW_CFA_nop:
2204         VG_(printf)("DW_CFA_nop\n");
2205         break;
2206
2207      case DW_CFA_set_loc:
2208         /* WAS: loc = read_Addr(&instr[i]); i+= sizeof(Addr); */
2209         loc = read_encoded_Addr(&len, adi, &instr[i]);
2210         i += len;
2211         VG_(printf)("DW_CFA_set_loc(%p)\n", loc);
2212         break;
2213
2214      case DW_CFA_advance_loc1:
2215         delta = (UInt)read_UChar(&instr[i]); i+= sizeof(UChar);
2216         VG_(printf)("DW_CFA_advance_loc1(%d)\n", delta);
2217         break;
2218
2219      case DW_CFA_advance_loc2:
2220         delta = (UInt)read_UShort(&instr[i]); i+= sizeof(UShort);
2221         VG_(printf)("DW_CFA_advance_loc2(%d)\n", delta);
2222         break;
2223
2224      case DW_CFA_advance_loc4:
2225         delta = (UInt)read_UInt(&instr[i]); i+= sizeof(UInt);
2226         VG_(printf)("DW_CFA_advance_loc4(%d)\n", delta);
2227         break;
2228
2229      case DW_CFA_def_cfa:
2230         reg = read_leb128( &instr[i], &nleb, 0 );
2231         i += nleb;
2232         off = read_leb128( &instr[i], &nleb, 0 );
2233         i += nleb;
2234         VG_(printf)("DW_CFA_def_cfa(r%d, off %d)\n", reg, off);
2235         break;
2236
2237      case DW_CFA_def_cfa_sf:
2238         reg = read_leb128( &instr[i], &nleb, 0 );
2239         i += nleb;
2240         off = read_leb128( &instr[i], &nleb, 1 );
2241         i += nleb;
2242         VG_(printf)("DW_CFA_def_cfa_sf(r%d, off %d)\n", reg, off);
2243         break;
2244
2245      case DW_CFA_register:
2246         reg = read_leb128( &instr[i], &nleb, 0);
2247         i += nleb;
2248         reg2 = read_leb128( &instr[i], &nleb, 0);
2249         i += nleb;
2250         VG_(printf)("DW_CFA_register(r%d, r%d)\n", reg, reg2);
2251         break;
2252
2253      case DW_CFA_def_cfa_register:
2254         reg = read_leb128( &instr[i], &nleb, 0);
2255         i += nleb;
2256         VG_(printf)("DW_CFA_def_cfa_register(r%d)\n", reg);
2257         break;
2258
2259      case DW_CFA_def_cfa_offset:
2260         off = read_leb128( &instr[i], &nleb, 0);
2261         i += nleb;
2262         VG_(printf)("DW_CFA_def_cfa_offset(%d)\n", off);
2263         break;
2264
2265      case DW_CFA_def_cfa_offset_sf:
2266         off = read_leb128( &instr[i], &nleb, 1);
2267         i += nleb;
2268         VG_(printf)("DW_CFA_def_cfa_offset_sf(%d)\n", off);
2269         break;
2270
2271      case DW_CFA_restore_extended:
2272         reg = read_leb128( &instr[i], &nleb, 0);
2273         i += nleb;
2274         VG_(printf)("DW_CFA_restore_extended(r%d)\n", reg);
2275         break;
2276
2277      case DW_CFA_undefined:
2278         reg = read_leb128( &instr[i], &nleb, 0);
2279         i += nleb;
2280         VG_(printf)("DW_CFA_undefined(r%d)\n", reg);
2281         break;
2282
2283      case DW_CFA_same_value:
2284         reg = read_leb128( &instr[i], &nleb, 0);
2285         i += nleb;
2286         VG_(printf)("DW_CFA_same_value(r%d)\n", reg);
2287         break;
2288
2289      case DW_CFA_remember_state:
2290         VG_(printf)("DW_CFA_remember_state\n");
2291         break;
2292
2293      case DW_CFA_restore_state:
2294         VG_(printf)("DW_CFA_restore_state\n");
2295         break;
2296
2297      case DW_CFA_GNU_args_size:
2298         off = read_leb128( &instr[i], &nleb, 0 );
2299         i += nleb;
2300         VG_(printf)("DW_CFA_GNU_args_size(%d)\n", off );
2301         break;
2302
2303      case DW_CFA_def_cfa_expression:
2304         len = read_leb128( &instr[i], &nleb, 0 );
2305         i += nleb;
2306         i += len;
2307         VG_(printf)("DW_CFA_def_cfa_expression(length %d)\n", len);
2308         break;
2309
2310      case DW_CFA_expression:
2311         reg = read_leb128( &instr[i], &nleb, 0 );
2312         i += nleb;
2313         len = read_leb128( &instr[i], &nleb, 0 );
2314         i += nleb;
2315         i += len;
2316         VG_(printf)("DW_CFA_expression(r%d, length %d)\n", reg, len);
2317         break;
2318
2319      case DW_CFA_val_expression:
2320         reg = read_leb128( &instr[i], &nleb, 0 );
2321         i += nleb;
2322         len = read_leb128( &instr[i], &nleb, 0 );
2323         i += nleb;
2324         i += len;
2325         VG_(printf)("DW_CFA_val_expression(r%d, length %d)\n", reg, len);
2326         break;
2327
2328      case DW_CFA_offset_extended:
2329         reg = read_leb128( &instr[i], &nleb, 0 );
2330         i += nleb;
2331         off = read_leb128( &instr[i], &nleb, 0 );
2332         i += nleb;
2333         VG_(printf)("DW_CFA_offset_extended(r%d, off %d x data_af)\n", reg, off);
2334         break;
2335
2336       case DW_CFA_offset_extended_sf:
2337         reg = read_leb128( &instr[i], &nleb, 0 );
2338         i += nleb;
2339         off = read_leb128( &instr[i], &nleb, 1 );
2340         i += nleb;
2341         VG_(printf)("DW_CFA_offset_extended_sf(r%d, off %d x data_af)\n", reg, off);
2342         break;
2343
2344      case DW_CFA_GNU_negative_offset_extended:
2345         reg = read_leb128( &instr[i], &nleb, 0 );
2346         i += nleb;
2347         off = read_leb128( &instr[i], &nleb, 0 );
2348         i += nleb;
2349         VG_(printf)("DW_CFA_GNU_negative_offset_extended(r%d, off %d x data_af)\n", reg, -off);
2350         break;
2351
2352      case DW_CFA_val_offset:
2353         reg = read_leb128( &instr[i], &nleb, 0 );
2354         i += nleb;
2355         off = read_leb128( &instr[i], &nleb, 0 );
2356         i += nleb;
2357         VG_(printf)("DW_CFA_val_offset(r%d, off %d x data_af)\n", reg, off);
2358         break;
2359
2360       case DW_CFA_val_offset_sf:
2361         reg = read_leb128( &instr[i], &nleb, 0 );
2362         i += nleb;
2363         off = read_leb128( &instr[i], &nleb, 1 );
2364         i += nleb;
2365         VG_(printf)("DW_CFA_val_offset_sf(r%d, off %d x data_af)\n", reg, off);
2366         break;
2367
2368      case DW_CFA_GNU_window_save:
2369         VG_(printf)("DW_CFA_GNU_window_save\n");
2370         break;
2371
2372      default:
2373         VG_(printf)("0:%d\n", (Int)lo6);
2374         break;
2375   }
2376
2377   return i;
2378}
2379
2380
2381static void show_CF_instructions ( UChar* instrs, Int ilen,
2382                                   AddressDecodingInfo* adi )
2383{
2384   Int i = 0;
2385   while (True) {
2386      if (i >= ilen) break;
2387      i += show_CF_instruction( &instrs[i], adi );
2388   }
2389}
2390
2391/* Run the CF instructions in instrs[0 .. ilen-1], until the end is
2392   reached, or until there is a failure.  Return True iff success.
2393*/
2394static
2395Bool run_CF_instructions ( struct _SegInfo* si,
2396                           UnwindContext* ctx, UChar* instrs, Int ilen,
2397                           UWord fde_arange,
2398                           UnwindContext* restore_ctx,
2399                           AddressDecodingInfo* adi )
2400{
2401   DiCfSI cfsi;
2402   Bool summ_ok;
2403   Int j, i = 0;
2404   Addr loc_prev;
2405   if (0) ppUnwindContext(ctx);
2406   if (0) ppUnwindContext_summary(ctx);
2407   while (True) {
2408      loc_prev = ctx->loc;
2409      if (i >= ilen) break;
2410      if (0) (void)show_CF_instruction( &instrs[i], adi );
2411      j = run_CF_instruction( ctx, &instrs[i], restore_ctx, adi );
2412      if (j == 0)
2413         return False; /* execution failed */
2414      i += j;
2415      if (0) ppUnwindContext(ctx);
2416      if (loc_prev != ctx->loc && si) {
2417         summ_ok = summarise_context ( &cfsi, loc_prev, ctx );
2418         if (summ_ok) {
2419            ML_(addDiCfSI)(si, &cfsi);
2420            if (VG_(clo_trace_cfi))
2421               ML_(ppDiCfSI)(&cfsi);
2422         }
2423      }
2424   }
2425   if (ctx->loc < fde_arange) {
2426      loc_prev = ctx->loc;
2427      ctx->loc = fde_arange;
2428      if (si) {
2429         summ_ok = summarise_context ( &cfsi, loc_prev, ctx );
2430         if (summ_ok) {
2431            ML_(addDiCfSI)(si, &cfsi);
2432            if (VG_(clo_trace_cfi))
2433               ML_(ppDiCfSI)(&cfsi);
2434         }
2435      }
2436   }
2437   return True;
2438}
2439
2440
2441/* ------------ Main entry point for CFI reading ------------ */
2442
2443typedef
2444   struct {
2445      /* This gives the CIE an identity to which FDEs will refer. */
2446      UInt   offset;
2447      /* Code, data factors. */
2448      Int    code_a_f;
2449      Int    data_a_f;
2450      /* Return-address pseudo-register. */
2451      Int    ra_reg;
2452      UChar  address_encoding;
2453      /* Where are the instrs?  Note, this are simply pointers back to
2454         the transiently-mapped-in section. */
2455      UChar* instrs;
2456      Int    ilen;
2457      /* God knows .. don't ask */
2458      Bool   saw_z_augmentation;
2459   }
2460   CIE;
2461
2462static void init_CIE ( CIE* cie )
2463{
2464   cie->offset             = 0;
2465   cie->code_a_f           = 0;
2466   cie->data_a_f           = 0;
2467   cie->ra_reg             = 0;
2468   cie->address_encoding   = 0;
2469   cie->instrs             = NULL;
2470   cie->ilen               = 0;
2471   cie->saw_z_augmentation = False;
2472}
2473
2474#define N_CIEs 2000
2475static CIE the_CIEs[N_CIEs];
2476
2477
2478void ML_(read_callframe_info_dwarf2)
2479        ( /*OUT*/struct _SegInfo* si,
2480          UChar* ehframe, Int ehframe_sz, Addr ehframe_addr )
2481{
2482   Int    nbytes;
2483   HChar* how = NULL;
2484   Int    n_CIEs = 0;
2485   UChar* data = ehframe;
2486
2487#  if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
2488   /* These targets don't use CFI-based stack unwinding. */
2489   return;
2490#  endif
2491
2492   if (VG_(clo_trace_cfi)) {
2493      VG_(printf)("\n-----------------------------------------------\n");
2494      VG_(printf)("CFI info: ehframe %p, ehframe_sz %d\n",
2495	          ehframe, ehframe_sz );
2496      VG_(printf)("CFI info: name %s\n",
2497		  si->filename );
2498   }
2499
2500   /* Loop over CIEs/FDEs */
2501
2502   /* Conceptually, the frame info is a sequence of FDEs, one for each
2503      function.  Inside an FDE is a miniature program for a special
2504      state machine, which, when run, produces the stack-unwinding
2505      info for that function.
2506
2507      Because the FDEs typically have much in common, and because the
2508      DWARF designers appear to have been fanatical about space
2509      saving, the common parts are factored out into so-called CIEs.
2510      That means that what we traverse is a sequence of structs, each
2511      of which is either a FDE (usually) or a CIE (occasionally).
2512      Each FDE has a field indicating which CIE is the one pertaining
2513      to it.
2514
2515      The following loop traverses the sequence.  FDEs are dealt with
2516      immediately; once we harvest the useful info in an FDE, it is
2517      then forgotten about.  By contrast, CIEs are validated and
2518      dumped into an array, because later FDEs may refer to any
2519      previously-seen CIE.
2520   */
2521   while (True) {
2522      UChar* ciefde_start;
2523      UInt   ciefde_len;
2524      UInt   cie_pointer;
2525
2526      /* Are we done? */
2527      if (data == ehframe + ehframe_sz)
2528         return;
2529
2530      /* Overshot the end?  Means something is wrong */
2531      if (data > ehframe + ehframe_sz) {
2532         how = "overran the end of .eh_frame";
2533         goto bad;
2534      }
2535
2536      /* Ok, we must be looking at the start of a new CIE or FDE.
2537         Figure out which it is. */
2538
2539      ciefde_start = data;
2540      if (VG_(clo_trace_cfi))
2541         VG_(printf)("\ncie/fde.start   = %p (ehframe + 0x%x)\n",
2542                     ciefde_start, ciefde_start - ehframe);
2543
2544      ciefde_len = read_UInt(data); data += sizeof(UInt);
2545      if (VG_(clo_trace_cfi))
2546         VG_(printf)("cie/fde.length  = %d\n", ciefde_len);
2547
2548      /* Apparently, if the .length field is zero, we are at the end
2549         of the sequence.  ?? Neither the DWARF2 spec not the AMD64
2550         ABI spec say this, though. */
2551      if (ciefde_len == 0) {
2552         if (data == ehframe + ehframe_sz)
2553            return;
2554         how = "zero-sized CIE/FDE but not at section end";
2555         goto bad;
2556      }
2557
2558      cie_pointer = read_UInt(data);
2559      data += sizeof(UInt); /* XXX see XXX below */
2560      if (VG_(clo_trace_cfi))
2561         VG_(printf)("cie.pointer     = %d\n", cie_pointer);
2562
2563      /* If cie_pointer is zero, we've got a CIE; else it's an FDE. */
2564      if (cie_pointer == 0) {
2565
2566         Int    this_CIE;
2567         UChar  cie_version;
2568         UChar* cie_augmentation;
2569
2570         /* --------- CIE --------- */
2571	 if (VG_(clo_trace_cfi))
2572            VG_(printf)("------ new CIE (#%d of 0 .. %d) ------\n",
2573                        n_CIEs, N_CIEs - 1);
2574
2575	 /* Allocate a new CIE record. */
2576         vg_assert(n_CIEs >= 0 && n_CIEs <= N_CIEs);
2577         if (n_CIEs == N_CIEs) {
2578            how = "N_CIEs is too low.  Increase and recompile.";
2579            goto bad;
2580         }
2581
2582         this_CIE = n_CIEs;
2583         n_CIEs++;
2584         init_CIE( &the_CIEs[this_CIE] );
2585
2586	 /* Record its offset.  This is how we will find it again
2587            later when looking at an FDE. */
2588         the_CIEs[this_CIE].offset = ciefde_start - ehframe;
2589
2590         cie_version = read_UChar(data); data += sizeof(UChar);
2591         if (VG_(clo_trace_cfi))
2592            VG_(printf)("cie.version     = %d\n", (Int)cie_version);
2593         if (cie_version != 1) {
2594            how = "unexpected CIE version (not 1)";
2595            goto bad;
2596         }
2597
2598         cie_augmentation = data;
2599         data += 1 + VG_(strlen)(cie_augmentation);
2600         if (VG_(clo_trace_cfi))
2601            VG_(printf)("cie.augment     = \"%s\"\n", cie_augmentation);
2602
2603         if (cie_augmentation[0] == 'e' && cie_augmentation[1] == 'h') {
2604            data += sizeof(Addr);
2605            cie_augmentation += 2;
2606         }
2607
2608         the_CIEs[this_CIE].code_a_f = read_leb128( data, &nbytes, 0);
2609         data += nbytes;
2610         if (VG_(clo_trace_cfi))
2611            VG_(printf)("cie.code_af     = %d\n",
2612                        the_CIEs[this_CIE].code_a_f);
2613
2614         the_CIEs[this_CIE].data_a_f = read_leb128( data, &nbytes, 1);
2615         data += nbytes;
2616         if (VG_(clo_trace_cfi))
2617            VG_(printf)("cie.data_af     = %d\n",
2618                        the_CIEs[this_CIE].data_a_f);
2619
2620         the_CIEs[this_CIE].ra_reg = (Int)read_UChar(data);
2621         data += sizeof(UChar);
2622         if (VG_(clo_trace_cfi))
2623            VG_(printf)("cie.ra_reg      = %d\n",
2624                        the_CIEs[this_CIE].ra_reg);
2625         if (the_CIEs[this_CIE].ra_reg < 0
2626             || the_CIEs[this_CIE].ra_reg >= N_CFI_REGS) {
2627            how = "cie.ra_reg has implausible value";
2628            goto bad;
2629         }
2630
2631         the_CIEs[this_CIE].saw_z_augmentation
2632            = *cie_augmentation == 'z';
2633         if (the_CIEs[this_CIE].saw_z_augmentation) {
2634            UInt length = read_leb128( data, &nbytes, 0);
2635            data += nbytes;
2636            the_CIEs[this_CIE].instrs = data + length;
2637            cie_augmentation++;
2638         } else {
2639            the_CIEs[this_CIE].instrs = NULL;
2640         }
2641
2642         the_CIEs[this_CIE].address_encoding = default_Addr_encoding();
2643
2644         while (*cie_augmentation) {
2645            switch (*cie_augmentation) {
2646               case 'L':
2647                  data++;
2648                  cie_augmentation++;
2649                  break;
2650               case 'R':
2651                  the_CIEs[this_CIE].address_encoding
2652                     = read_UChar(data); data += sizeof(UChar);
2653                  cie_augmentation++;
2654                  break;
2655               case 'P':
2656                  data += size_of_encoded_Addr( read_UChar(data) );
2657                  data++;
2658                  cie_augmentation++;
2659                  break;
2660               case 'S':
2661                  cie_augmentation++;
2662                  break;
2663               default:
2664                  if (the_CIEs[this_CIE].instrs == NULL) {
2665                     how = "unhandled cie.augmentation";
2666                     goto bad;
2667                  }
2668                  data = the_CIEs[this_CIE].instrs;
2669                  goto done_augmentation;
2670            }
2671         }
2672
2673        done_augmentation:
2674
2675         if (VG_(clo_trace_cfi))
2676            VG_(printf)("cie.encoding    = 0x%x\n",
2677                        the_CIEs[this_CIE].address_encoding);
2678
2679         the_CIEs[this_CIE].instrs = data;
2680         the_CIEs[this_CIE].ilen
2681            = ciefde_start + ciefde_len + sizeof(UInt) - data;
2682         if (VG_(clo_trace_cfi)) {
2683            VG_(printf)("cie.instrs      = %p\n", the_CIEs[this_CIE].instrs);
2684            VG_(printf)("cie.ilen        = %d\n", the_CIEs[this_CIE].ilen);
2685	 }
2686
2687         if (the_CIEs[this_CIE].ilen < 0
2688             || the_CIEs[this_CIE].ilen > ehframe_sz) {
2689            how = "implausible # cie initial insns";
2690            goto bad;
2691         }
2692
2693         data += the_CIEs[this_CIE].ilen;
2694
2695         if (VG_(clo_trace_cfi)) {
2696            AddressDecodingInfo adi;
2697            adi.encoding     = the_CIEs[this_CIE].address_encoding;
2698            adi.ehframe      = ehframe;
2699            adi.ehframe_addr = ehframe_addr;
2700            show_CF_instructions(the_CIEs[this_CIE].instrs,
2701                                 the_CIEs[this_CIE].ilen, &adi );
2702         }
2703
2704      } else {
2705
2706         AddressDecodingInfo adi;
2707         UnwindContext ctx, restore_ctx;
2708         Int    cie;
2709         UInt   look_for;
2710         Bool   ok;
2711         Addr   fde_initloc;
2712         UWord  fde_arange;
2713         UChar* fde_instrs;
2714         Int    fde_ilen;
2715
2716         /* --------- FDE --------- */
2717
2718         /* Find the relevant CIE.  The CIE we want is located
2719            cie_pointer bytes back from here. */
2720
2721         /* re sizeof(UInt), matches XXX above.  For 64-bit dwarf this
2722            will have to be a ULong instead. */
2723         look_for = (data - sizeof(UInt) - ehframe) - cie_pointer;
2724
2725         for (cie = 0; cie < n_CIEs; cie++) {
2726            if (0) VG_(printf)("look for %d   %d\n",
2727                               look_for, the_CIEs[cie].offset );
2728            if (the_CIEs[cie].offset == look_for)
2729               break;
2730	 }
2731         vg_assert(cie >= 0 && cie <= n_CIEs);
2732         if (cie == n_CIEs) {
2733            how = "FDE refers to not-findable CIE";
2734            goto bad;
2735	 }
2736
2737         adi.encoding     = the_CIEs[cie].address_encoding;
2738         adi.ehframe      = ehframe;
2739         adi.ehframe_addr = ehframe_addr;
2740         fde_initloc = read_encoded_Addr(&nbytes, &adi, data);
2741         data += nbytes;
2742         if (VG_(clo_trace_cfi))
2743            VG_(printf)("fde.initloc     = %p\n", (void*)fde_initloc);
2744
2745         adi.encoding     = the_CIEs[cie].address_encoding & 0xf;
2746         adi.ehframe      = ehframe;
2747         adi.ehframe_addr = ehframe_addr;
2748         fde_arange = read_encoded_Addr(&nbytes, &adi, data);
2749         data += nbytes;
2750         if (VG_(clo_trace_cfi))
2751            VG_(printf)("fde.arangec     = %p\n", (void*)fde_arange);
2752
2753         if (the_CIEs[cie].saw_z_augmentation) {
2754            data += read_leb128( data, &nbytes, 0);
2755            data += nbytes;
2756         }
2757
2758         fde_instrs = data;
2759         fde_ilen   = ciefde_start + ciefde_len + sizeof(UInt) - data;
2760         if (VG_(clo_trace_cfi)) {
2761            VG_(printf)("fde.instrs      = %p\n", fde_instrs);
2762            VG_(printf)("fde.ilen        = %d\n", (Int)fde_ilen);
2763	 }
2764
2765         if (fde_ilen < 0 || fde_ilen > ehframe_sz) {
2766            how = "implausible # fde insns";
2767            goto bad;
2768         }
2769
2770	 data += fde_ilen;
2771
2772         adi.encoding     = the_CIEs[cie].address_encoding;
2773         adi.ehframe      = ehframe;
2774         adi.ehframe_addr = ehframe_addr;
2775
2776         if (VG_(clo_trace_cfi))
2777            show_CF_instructions(fde_instrs, fde_ilen, &adi);
2778
2779	 initUnwindContext(&ctx);
2780         ctx.code_a_f = the_CIEs[cie].code_a_f;
2781         ctx.data_a_f = the_CIEs[cie].data_a_f;
2782         ctx.initloc  = fde_initloc;
2783         ctx.ra_reg   = the_CIEs[cie].ra_reg;
2784
2785	 initUnwindContext(&restore_ctx);
2786
2787	 ok = run_CF_instructions(
2788                 NULL, &ctx, the_CIEs[cie].instrs,
2789                 the_CIEs[cie].ilen, 0, NULL, &adi
2790              );
2791         if (ok) {
2792            restore_ctx = ctx;
2793	    ok = run_CF_instructions(
2794                    si, &ctx, fde_instrs, fde_ilen, fde_arange,
2795                    &restore_ctx, &adi
2796                 );
2797	 }
2798      }
2799   }
2800
2801   return;
2802
2803   bad:
2804    if (!VG_(clo_xml) && VG_(clo_verbosity) > 1)
2805       VG_(message)(Vg_UserMsg, "Warning: %s in DWARF2 CFI reading", how);
2806    return;
2807}
2808
2809
2810/*--------------------------------------------------------------------*/
2811/*--- end                                                          ---*/
2812/*--------------------------------------------------------------------*/
2813