~bkerensa/ubuntu/raring/valgrind/merge-from-deb

« back to all changes in this revision

Viewing changes to coregrind/m_debuginfo/readdwarf.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrés Roldán
  • Date: 2008-06-13 02:31:40 UTC
  • mto: (1.4.1 upstream) (2.2.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: james.westby@ubuntu.com-20080613023140-iwk33rz9rhvfkr96
Import upstream version 3.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
   This file is part of Valgrind, a dynamic binary instrumentation
8
8
   framework.
9
9
 
10
 
   Copyright (C) 2000-2006 Julian Seward
 
10
   Copyright (C) 2000-2007 Julian Seward
11
11
      jseward@acm.org
12
12
 
13
13
   This program is free software; you can redistribute it and/or
39
39
#include "pub_core_libcprint.h"
40
40
#include "pub_core_mallocfree.h"
41
41
#include "pub_core_options.h"
 
42
#include "pub_core_xarray.h"
42
43
#include "priv_storage.h"
43
44
#include "priv_readdwarf.h"        /* self */
44
45
 
113
114
   wa->tab_used++;
114
115
}
115
116
 
116
 
static Word index_WordArray ( WordArray* wa, Int i )
 
117
static Word index_WordArray ( /*OUT*/Bool* inRange, WordArray* wa, Int i )
117
118
{
118
 
   vg_assert(i >= 0 && i < wa->tab_used);
119
 
   return wa->tab[i];
 
119
   vg_assert(inRange);
 
120
   if (i >= 0 && i < wa->tab_used) {
 
121
      *inRange = True;
 
122
      return wa->tab[i];
 
123
   } else {
 
124
      *inRange = False;
 
125
      return 0;
 
126
   }
120
127
}
121
128
 
122
129
 
178
185
    DW_LNE_define_file = 3
179
186
  };
180
187
 
181
 
typedef struct State_Machine_Registers
 
188
typedef struct
182
189
{
183
190
  /* Information for the last statement boundary.
184
191
   * Needed to calculate statement lengths. */
193
200
  Int   is_stmt;
194
201
  Int   basic_block;
195
202
  Int   end_sequence;
196
 
} SMR;
 
203
} LineSMR;
197
204
 
198
205
 
199
206
static 
200
 
UInt read_leb128 ( UChar* data, Int* length_return, Int sign )
 
207
ULong read_leb128 ( UChar* data, Int* length_return, Int sign )
201
208
{
202
 
  UInt   result = 0;
 
209
  ULong  result = 0;
203
210
  UInt   num_read = 0;
204
211
  Int    shift = 0;
205
212
  UChar  byte;
206
213
 
 
214
  vg_assert(sign == 0 || sign == 1);
 
215
 
207
216
  do
208
217
    {
209
218
      byte = * data ++;
210
219
      num_read ++;
211
220
 
212
 
      result |= (byte & 0x7f) << shift;
 
221
      result |= ((ULong)(byte & 0x7f)) << shift;
213
222
 
214
223
      shift += 7;
215
224
 
219
228
  if (length_return != NULL)
220
229
    * length_return = num_read;
221
230
 
222
 
  if (sign && (shift < 32) && (byte & 0x40))
223
 
    result |= -1 << shift;
 
231
  if (sign && (shift < 64) && (byte & 0x40))
 
232
    result |= (-1ULL) << shift;
224
233
 
225
234
  return result;
226
235
}
227
236
 
228
 
 
229
237
/* Small helper functions easier to use
230
238
 * value is returned and the given pointer is
231
239
 * moved past end of leb128 data */
232
 
static UInt read_leb128U( UChar **data )
 
240
static ULong read_leb128U( UChar **data )
233
241
{
234
242
  Int len;
235
 
  UInt val = read_leb128( *data, &len, 0 );
 
243
  ULong val = read_leb128( *data, &len, 0 );
236
244
  *data += len;
237
245
  return val;
238
246
}
239
247
 
240
248
/* Same for signed data */
241
 
static Int read_leb128S( UChar **data )
 
249
static Long read_leb128S( UChar **data )
242
250
{
243
251
   Int len;
244
 
   UInt val = read_leb128( *data, &len, 1 );
 
252
   ULong val = read_leb128( *data, &len, 1 );
245
253
   *data += len;
246
 
   return val;
 
254
   return (Long)val;
247
255
}
248
256
 
249
257
/* Read what the DWARF3 spec calls an "initial length field".  This
252
260
 
253
261
   Read 32-bit value from p.  If it is 0xFFFFFFFF, instead read a
254
262
   64-bit bit value from p+4.  This is used in 64-bit dwarf to encode
255
 
   some table lengths. */
256
 
static ULong read_initial_length_field ( UChar* p, /*OUT*/Bool* is64 )
 
263
   some table lengths. 
 
264
 
 
265
   XXX this is a hack: the endianness of the initial length field is
 
266
   specified by the DWARF we're reading.  This happens to work only
 
267
   because we don't do cross-arch jitting, hence this code runs on a
 
268
   platform of the same endianness as the DWARF it is reading.  Same
 
269
   applies for initial lengths for CIE/FDEs and probably in zillions
 
270
   of other places -- to be precise, exactly the places where
 
271
   binutils/dwarf.c calls byte_get().
 
272
*/
 
273
static ULong read_initial_length_field ( UChar* p_img, /*OUT*/Bool* is64 )
257
274
{
258
 
   UInt w32 = *((UInt*)p);
 
275
   UInt w32 = *((UInt*)p_img);
259
276
   if (w32 == 0xFFFFFFFF) {
260
277
      *is64 = True;
261
 
      return *((ULong*)(p+4));
 
278
      return *((ULong*)(p_img+4));
262
279
   } else {
263
280
      *is64 = False;
264
281
      return (ULong)w32;
266
283
}
267
284
 
268
285
 
269
 
static SMR state_machine_regs;
 
286
static LineSMR state_machine_regs;
270
287
 
271
288
static 
272
289
void reset_state_machine ( Int is_stmt )
290
307
                  WordArray* fnidx2dir,
291
308
                  WordArray* dirnames )
292
309
{
293
 
   Word diridx  = index_WordArray( fnidx2dir, filename_index );
294
 
   Word dirname = index_WordArray( dirnames, (Int)diridx );
 
310
   Bool inRange;
 
311
   Word diridx, dirname;
 
312
 
 
313
   diridx = index_WordArray( &inRange, fnidx2dir, filename_index );
 
314
   if (!inRange) goto bad;
 
315
 
 
316
   dirname = index_WordArray( &inRange, dirnames, (Int)diridx );
 
317
   if (!inRange) goto bad;
 
318
 
295
319
   return (Char*)dirname;
 
320
  bad:
 
321
   return NULL;
296
322
}
297
323
 
298
324
////////////////////////////////////////////////////////////////////
299
325
////////////////////////////////////////////////////////////////////
300
326
 
301
 
/* Handled an extend line op.  Returns true if this is the end
302
 
   of sequence.  */
 
327
/* Handled an extended line op starting at 'data'.  Returns the number
 
328
   of bytes that 'data' should be advanced by. */
303
329
static 
304
 
Int 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)
 
330
Word process_extended_line_op( struct _SegInfo* si, OffT debug_offset,
 
331
                               WordArray* filenames, 
 
332
                               WordArray* dirnames, 
 
333
                               WordArray* fnidx2dir, 
 
334
                               UChar* data, Int is_stmt)
309
335
{
310
336
   UChar  op_code;
311
337
   Int    bytes_read;
318
344
 
319
345
   if (len == 0) {
320
346
      VG_(message)(Vg_UserMsg,
321
 
                   "badly formed extended line op encountered!\n");
322
 
      return bytes_read;
 
347
                   "Warning: DWARF2 reader: "
 
348
                   "Badly formed extended line op encountered");
 
349
      return (Word)bytes_read;
323
350
   }
324
351
 
325
352
   len += bytes_read;
336
363
         state_machine_regs.end_sequence = 1; 
337
364
 
338
365
         if (state_machine_regs.is_stmt) {
339
 
            if (state_machine_regs.last_address)
 
366
            if (state_machine_regs.last_address) {
 
367
               Bool inRange = False;
 
368
               Char* filename
 
369
                  = (Char*)index_WordArray( &inRange, filenames, 
 
370
                                            state_machine_regs.last_file);
 
371
               if (!inRange || !filename)
 
372
                  filename = "???";
340
373
               ML_(addLineInfo) (
341
374
                  si, 
342
 
                  (Char*)index_WordArray(filenames, 
343
 
                                         state_machine_regs.last_file), 
 
375
                  filename, 
344
376
                  lookupDir( state_machine_regs.last_file,
345
377
                             fnidx2dir, dirnames ),
346
378
                  debug_offset + state_machine_regs.last_address, 
347
379
                  debug_offset + state_machine_regs.address, 
348
380
                  state_machine_regs.last_line, 0
349
381
               );
 
382
            }
350
383
         }
351
384
         reset_state_machine (is_stmt);
 
385
         if (si->ddump_line)
 
386
            VG_(printf)("  Extended opcode %d: End of Sequence\n\n", 
 
387
                        (Int)op_code);
352
388
         break;
353
389
 
354
390
      case DW_LNE_set_address:
355
391
         adr = *((Addr *)data);
356
 
         if (0) VG_(printf)("smr.a := %p\n", adr );
357
392
         state_machine_regs.address = adr;
 
393
         if (si->ddump_line)
 
394
            VG_(printf)("  Extended opcode %d: set Address to 0x%lx\n",
 
395
                        (Int)op_code, (Addr)adr);
358
396
         break;
359
397
 
360
398
      case DW_LNE_define_file:
366
404
         read_leb128 (data, & bytes_read, 0);
367
405
         data += bytes_read;
368
406
         read_leb128 (data, & bytes_read, 0);
 
407
         if (si->ddump_line)
 
408
            VG_(printf)("  DWARF2-line: set_address\n");
369
409
         break;
370
410
 
371
411
      default:
 
412
         if (si->ddump_line)
 
413
            VG_(printf)("process_extended_line_op:default\n");
372
414
         break;
373
415
   }
374
416
 
375
 
   return len;
 
417
   return (Word)len;
376
418
}
377
419
 
378
420
////////////////////////////////////////////////////////////////////
390
432
static 
391
433
void read_dwarf2_lineblock ( struct _SegInfo*  si, OffT debug_offset,
392
434
                             UnitInfo* ui, 
393
 
                             UChar*    theBlock, 
 
435
                             UChar*    theBlock, /* IMAGE */
394
436
                             Int       noLargerThan )
395
437
{
 
438
   Int            i;
396
439
   DebugLineInfo  info;
397
440
   UChar*         standard_opcodes;
398
441
   UChar*         end_of_sequence;
448
491
 
449
492
   info.li_length = read_initial_length_field( external, &is64 );
450
493
   external += is64 ? 12 : 4;
 
494
   if (si->ddump_line)
 
495
      VG_(printf)("  Length:                      %llu\n", 
 
496
                  info.li_length);
 
497
 
451
498
   /* Check the length of the block.  */
452
499
   if (info.li_length > noLargerThan) {
453
500
      ML_(symerr)("DWARF line info appears to be corrupt "
458
505
   /* Check its version number.  */
459
506
   info.li_version = * ((UShort *)external);
460
507
   external += 2;
 
508
   if (si->ddump_line)
 
509
      VG_(printf)("  DWARF Version:               %d\n", 
 
510
                  (Int)info.li_version);
 
511
 
461
512
   if (info.li_version != 2) {
462
513
      ML_(symerr)("Only DWARF version 2 line info "
463
514
                  "is currently supported.");
467
518
   info.li_header_length = ui->dw64 ? *((ULong*)external) 
468
519
                                    : (ULong)(*((UInt*)external));
469
520
   external += ui->dw64 ? 8 : 4;
 
521
   if (si->ddump_line)
 
522
      VG_(printf)("  Prologue Length:             %llu\n", 
 
523
                  info.li_header_length);
470
524
 
471
525
   info.li_min_insn_length = * ((UChar *)external);
472
526
   external += 1;
 
527
   if (si->ddump_line)
 
528
      VG_(printf)("  Minimum Instruction Length:  %d\n", 
 
529
                  (Int)info.li_min_insn_length);
473
530
 
474
 
   info.li_default_is_stmt = True; 
475
 
   /* WAS: = * ((UChar *)(external->li_default_is_stmt)); */
 
531
   info.li_default_is_stmt = * ((UChar *)external);
476
532
   external += 1;
 
533
   if (si->ddump_line)
 
534
      VG_(printf)("  Initial value of 'is_stmt':  %d\n", 
 
535
                  (Int)info.li_default_is_stmt);
 
536
 
477
537
   /* Josef Weidendorfer (20021021) writes:
478
538
 
479
539
      It seems to me that the Intel Fortran compiler generates bad
489
549
      I just had a look at the GDB DWARF2 reader...  They completely
490
550
      ignore "is_stmt" when recording line info ;-) That's the reason
491
551
      "objdump -S" works on files from the the intel fortran compiler.
492
 
   */
 
552
 
 
553
      Therefore: */
 
554
   info.li_default_is_stmt = True; 
493
555
 
494
556
   /* JRS: changed (UInt*) to (UChar*) */
495
557
   info.li_line_base = * ((UChar *)external);
496
558
   info.li_line_base = (Int)(signed char)info.li_line_base;
497
559
   external += 1;
 
560
   if (si->ddump_line)
 
561
      VG_(printf)("  Line Base:                   %d\n", 
 
562
                  info.li_line_base);
498
563
 
499
564
   info.li_line_range = * ((UChar *)external);
500
565
   external += 1;
 
566
   if (si->ddump_line)
 
567
      VG_(printf)("  Line Range:                  %d\n", 
 
568
                  (Int)info.li_line_range);
501
569
 
502
570
   info.li_opcode_base = * ((UChar *)external);
503
571
   external += 1;
 
572
   if (si->ddump_line)
 
573
      VG_(printf)("  Opcode Base:                 %d\n\n", 
 
574
                  info.li_opcode_base);
504
575
 
505
576
   if (0) VG_(printf)("dwarf2: line base: %d, range %d, opc base: %d\n",
506
577
                      (Int)info.li_line_base, 
514
585
 
515
586
   /* Read the contents of the Opcodes table.  */
516
587
   standard_opcodes = external;
 
588
   if (si->ddump_line) {
 
589
      VG_(printf)(" Opcodes:\n");
 
590
      for (i = 1; i < (Int)info.li_opcode_base; i++) {
 
591
         VG_(printf)("  Opcode %d has %d args\n", 
 
592
                     i, (Int)standard_opcodes[i-1]);
 
593
      }
 
594
      VG_(printf)("\n");
 
595
   }
517
596
 
518
597
   /* Read the contents of the Directory table.  */
519
598
   data = standard_opcodes + info.li_opcode_base - 1;
520
599
 
 
600
   if (si->ddump_line)
 
601
      VG_(printf)(" The Directory Table%s\n", 
 
602
                  *data == 0 ? " is empty." : ":" );
 
603
 
521
604
   while (* data != 0) {
522
605
 
523
606
#     define NBUF 4096
524
607
      static Char buf[NBUF];
525
608
 
 
609
      if (si->ddump_line)
 
610
         VG_(printf)("  %s\n", data);
 
611
 
526
612
      /* If data[0] is '/', then 'data' is an absolute path and we
527
613
         don't mess with it.  Otherwise, if we can, construct the
528
614
         'path ui->compdir' ++ "/" ++ 'data'. */
551
637
 
552
638
#     undef NBUF
553
639
   }
 
640
 
 
641
   if (si->ddump_line)
 
642
      VG_(printf)("\n");
 
643
 
554
644
   if (*data != 0) {
555
645
      ML_(symerr)("can't find NUL at end of DWARF2 directory table");
556
646
      goto out;
559
649
 
560
650
   /* Read the contents of the File Name table.  This produces a bunch
561
651
      of file names, and for each, an index to the corresponding
562
 
      direcory name entry. */
 
652
      directory name entry. */
 
653
   if (si->ddump_line) {
 
654
      VG_(printf)(" The File Name Table:\n");
 
655
      VG_(printf)("  Entry      Dir     Time    Size    Name\n");
 
656
   }
 
657
 
 
658
   i = 1;
563
659
   while (* data != 0) {
564
660
      UChar* name;
565
661
      Int    bytes_read, diridx;
 
662
      Int    uu_time, uu_size; /* unused, and a guess */
566
663
      name = data;
567
664
      data += VG_(strlen) ((Char *) data) + 1;
568
665
 
569
666
      diridx = read_leb128 (data, & bytes_read, 0);
570
667
      data += bytes_read;
571
 
      read_leb128 (data, & bytes_read, 0);
 
668
      uu_time = read_leb128 (data, & bytes_read, 0);
572
669
      data += bytes_read;
573
 
      read_leb128 (data, & bytes_read, 0);
 
670
      uu_size = read_leb128 (data, & bytes_read, 0);
574
671
      data += bytes_read;
575
672
 
576
673
      addto_WordArray( &filenames, (Word)ML_(addStr)(si,name,-1) );
577
674
      addto_WordArray( &fnidx2dir, (Word)diridx );
578
675
      if (0) VG_(printf)("file %s diridx %d\n", name, diridx );
 
676
      if (si->ddump_line)
 
677
         VG_(printf)("  %d\t%d\t%d\t%d\t%s\n", 
 
678
                     i, diridx, uu_time, uu_size, name);
 
679
      i++;
579
680
   }
 
681
 
 
682
   if (si->ddump_line)
 
683
      VG_(printf)("\n");
 
684
 
580
685
   if (*data != 0) {
581
686
      ML_(symerr)("can't find NUL at end of DWARF2 file name table");
582
687
      goto out;
583
688
   }
584
689
   data ++;
585
690
 
 
691
   if (si->ddump_line)
 
692
      VG_(printf)(" Line Number Statements:\n");
 
693
 
586
694
   /* Now display the statements.  */
587
695
 
588
696
   while (data < end_of_sequence) {
603
711
                       * info.li_min_insn_length;
604
712
         advAddr = adv;
605
713
         state_machine_regs.address += adv;
 
714
 
606
715
         if (0) VG_(printf)("smr.a += %p\n", adv );
607
716
         adv = (op_code % info.li_line_range) + info.li_line_base;
608
717
         if (0) VG_(printf)("1002: si->o %p, smr.a %p\n", 
609
718
                            debug_offset, state_machine_regs.address );
610
719
         state_machine_regs.line += adv;
611
720
 
 
721
         if (si->ddump_line)
 
722
            VG_(printf)("  Special opcode %d: advance Address by %d "
 
723
                        "to 0x%lx and Line by %d to %d\n", 
 
724
                        (Int)op_code, advAddr, state_machine_regs.address,
 
725
                        (Int)adv, (Int)state_machine_regs.line );
 
726
 
612
727
         if (state_machine_regs.is_stmt) {
613
728
            /* only add a statement if there was a previous boundary */
614
 
            if (state_machine_regs.last_address) 
 
729
            if (state_machine_regs.last_address) {
 
730
               Bool inRange = False;
 
731
               Char* filename
 
732
                  = (Char*)index_WordArray( &inRange, &filenames, 
 
733
                                            state_machine_regs.last_file);
 
734
               if (!inRange || !filename)
 
735
                  filename = "???";
615
736
               ML_(addLineInfo)(
616
737
                  si, 
617
 
                  (Char*)index_WordArray( &filenames,
618
 
                                          state_machine_regs.last_file ),
 
738
                  filename,
619
739
                  lookupDir( state_machine_regs.last_file,
620
740
                             &fnidx2dir, &dirnames ),
621
741
                  debug_offset + state_machine_regs.last_address, 
623
743
                  state_machine_regs.last_line, 
624
744
                  0
625
745
               );
 
746
            }
626
747
            state_machine_regs.last_address = state_machine_regs.address;
627
748
            state_machine_regs.last_file = state_machine_regs.file;
628
749
            state_machine_regs.last_line = state_machine_regs.line;
630
751
 
631
752
      }
632
753
 
633
 
      else /* ! (op_code >= info.li_opcode_base) */
 
754
      else { /* ! (op_code >= info.li_opcode_base) */
 
755
 
634
756
      switch (op_code) {
635
757
         case DW_LNS_extended_op:
636
758
            data += process_extended_line_op (
643
765
                               debug_offset, state_machine_regs.address );
644
766
            if (state_machine_regs.is_stmt) {
645
767
               /* only add a statement if there was a previous boundary */
646
 
               if (state_machine_regs.last_address) 
 
768
               if (state_machine_regs.last_address) {
 
769
                  Bool inRange = False;
 
770
                  Char* filename
 
771
                     = (Char*)index_WordArray( &inRange, &filenames,
 
772
                                               state_machine_regs.last_file );
 
773
                  if (!inRange || !filename)
 
774
                     filename = "???";
647
775
                  ML_(addLineInfo)(
648
776
                     si, 
649
 
                     (Char*)index_WordArray( &filenames,
650
 
                                             state_machine_regs.last_file ),
 
777
                     filename,
651
778
                     lookupDir( state_machine_regs.last_file,
652
779
                                &fnidx2dir, &dirnames ),
653
780
                     debug_offset + state_machine_regs.last_address, 
655
782
                     state_machine_regs.last_line, 
656
783
                     0
657
784
                  );
 
785
               }
658
786
               state_machine_regs.last_address = state_machine_regs.address;
659
787
               state_machine_regs.last_file = state_machine_regs.file;
660
788
               state_machine_regs.last_line = state_machine_regs.line;
661
789
            }
662
790
            state_machine_regs.basic_block = 0; /* JRS added */
 
791
            if (si->ddump_line)
 
792
               VG_(printf)("  Copy\n");
663
793
            break;
664
794
 
665
795
         case DW_LNS_advance_pc:
668
798
            data += bytes_read;
669
799
            state_machine_regs.address += adv;
670
800
            if (0) VG_(printf)("smr.a += %p\n", adv );
 
801
            if (si->ddump_line)
 
802
               VG_(printf)("  Advance PC by %d to 0x%lx\n", 
 
803
                           (Int)adv, state_machine_regs.address);
671
804
            break;
672
805
 
673
806
         case DW_LNS_advance_line:
674
807
            adv = read_leb128 (data, & bytes_read, 1);
675
808
            data += bytes_read;
676
809
            state_machine_regs.line += adv;
 
810
            if (si->ddump_line)
 
811
               VG_(printf)("  Advance Line by %d to %d\n", 
 
812
                           (Int)adv, (Int)state_machine_regs.line);
677
813
            break;
678
814
 
679
815
         case DW_LNS_set_file:
680
816
            adv = read_leb128 (data, & bytes_read, 0);
681
817
            data += bytes_read;
682
818
            state_machine_regs.file = adv;
 
819
            if (si->ddump_line)
 
820
               VG_(printf)("  Set File Name to entry %d in the File Name Table\n",
 
821
                           (Int)adv);
683
822
            break;
684
823
 
685
824
         case DW_LNS_set_column:
686
825
            adv = read_leb128 (data, & bytes_read, 0);
687
826
            data += bytes_read;
688
827
            state_machine_regs.column = adv;
 
828
            if (si->ddump_line)
 
829
               VG_(printf)("  DWARF2-line: set_column\n");
689
830
            break;
690
831
 
691
832
         case DW_LNS_negate_stmt:
692
833
            adv = state_machine_regs.is_stmt;
693
834
            adv = ! adv;
694
835
            state_machine_regs.is_stmt = adv;
 
836
            if (si->ddump_line)
 
837
               VG_(printf)("  DWARF2-line: negate_stmt\n");
695
838
            break;
696
839
 
697
840
         case DW_LNS_set_basic_block:
698
841
            state_machine_regs.basic_block = 1;
 
842
            if (si->ddump_line)
 
843
               VG_(printf)("  DWARF2-line: set_basic_block\n");
699
844
            break;
700
845
 
701
846
         case DW_LNS_const_add_pc:
703
848
                   * info.li_min_insn_length);
704
849
            state_machine_regs.address += adv;
705
850
            if (0) VG_(printf)("smr.a += %p\n", adv );
 
851
            if (si->ddump_line)
 
852
               VG_(printf)("  Advance PC by constant %d to 0x%lx\n", 
 
853
                           (Int)adv, (Addr)state_machine_regs.address);
706
854
            break;
707
855
 
708
856
         case DW_LNS_fixed_advance_pc:
711
859
            data += 2;
712
860
            state_machine_regs.address += adv;
713
861
            if (0) VG_(printf)("smr.a += %p\n", adv );
 
862
            if (si->ddump_line)
 
863
               VG_(printf)("  DWARF2-line: fixed_advance_pc\n");
714
864
            break;
715
865
 
716
866
         case DW_LNS_set_prologue_end:
 
867
            if (si->ddump_line)
 
868
               VG_(printf)("  DWARF2-line: set_prologue_end\n");
717
869
            break;
718
870
 
719
871
         case DW_LNS_set_epilogue_begin:
 
872
            if (si->ddump_line)
 
873
               VG_(printf)("  DWARF2-line: set_epilogue_begin\n");
720
874
            break;
721
875
 
722
876
         case DW_LNS_set_isa:
723
877
            adv = read_leb128 (data, & bytes_read, 0);
724
878
            data += bytes_read;
 
879
            if (si->ddump_line)
 
880
               VG_(printf)("  DWARF2-line: set_isa\n");
725
881
            break;
726
882
 
727
883
         default: {
730
886
               read_leb128 (data, &bytes_read, 0);
731
887
               data += bytes_read;
732
888
            }
 
889
            if (si->ddump_line)
 
890
               VG_(printf)("  Unknown opcode %d\n", (Int)op_code);
 
891
            break;
733
892
         }
734
 
         break;
735
893
      } /* switch (op_code) */
736
894
 
 
895
      } /* if (op_code >= info.li_opcode_base) */
 
896
 
737
897
   } /* while (data < end_of_sequence) */
738
898
 
 
899
   if (si->ddump_line)
 
900
      VG_(printf)("\n");
 
901
 
739
902
  out:
740
903
   free_WordArray(&filenames);
741
904
   free_WordArray(&dirnames);
786
949
 */
787
950
static 
788
951
void read_unitinfo_dwarf2( /*OUT*/UnitInfo* ui,
789
 
                                  UChar*    unitblock,
790
 
                                  UChar*    debugabbrev,
791
 
                                  UChar*    debugstr )
 
952
                                  UChar*    unitblock_img,
 
953
                                  UChar*    debugabbrev_img,
 
954
                                  UChar*    debugstr_img,
 
955
                                  struct _SegInfo* si )
792
956
{
793
957
   UInt   acode, abcode;
794
958
   ULong  atoffs, blklen;
796
960
   UShort ver;
797
961
 
798
962
   UChar addr_size;
799
 
   UChar* p = unitblock;
800
 
   UChar* end;
801
 
   UChar* abbrev;
 
963
   UChar* p = unitblock_img;
 
964
   UChar* end_img;
 
965
   UChar* abbrev_img;
802
966
 
803
967
   VG_(memset)( ui, 0, sizeof( UnitInfo ) );
804
968
   ui->stmt_list = -1LL;
821
985
   addr_size = *p;
822
986
   p += 1;
823
987
 
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 */
 
988
   end_img     = unitblock_img 
 
989
                 + blklen + (ui->dw64 ? 12 : 4); /* End of this block */
 
990
   level       = 0;                        /* Level in the abbrev tree */
 
991
   abbrev_img  = debugabbrev_img 
 
992
                 + atoffs; /* Abbreviation data for this block */
827
993
   
828
994
   /* Read the compilation unit entries */
829
 
   while ( p < end ) {
 
995
   while ( p < end_img ) {
830
996
      Bool has_child;
831
997
      UInt tag;
832
998
 
839
1005
      }
840
1006
      
841
1007
      /* Read abbreviation header */
842
 
      abcode = read_leb128U( &abbrev ); /* abbreviation code */
 
1008
      abcode = read_leb128U( &abbrev_img ); /* abbreviation code */
843
1009
      if ( acode != abcode ) {
844
1010
         /* We are in in children list, and must rewind to a
845
1011
          * previously declared abbrev code.  This code works but is
846
1012
          * not triggered since we shortcut the parsing once we have
847
1013
          * read the compile_unit block.  This should only occur when
848
1014
          * level > 0 */
849
 
         abbrev = lookup_abbrev( debugabbrev + atoffs, acode );
 
1015
         abbrev_img = lookup_abbrev( debugabbrev_img + atoffs, acode );
850
1016
      }
851
1017
 
852
 
      tag = read_leb128U( &abbrev );
853
 
      has_child = *(abbrev++) == 1; /* DW_CHILDREN_yes */
 
1018
      tag = read_leb128U( &abbrev_img );
 
1019
      has_child = *(abbrev_img++) == 1; /* DW_CHILDREN_yes */
854
1020
 
855
1021
      if ( has_child )
856
1022
         level++;
861
1027
         UInt  name, form;
862
1028
         ULong cval = -1LL;  /* Constant value read */
863
1029
         Char  *sval = NULL; /* String value read */
864
 
         name = read_leb128U( &abbrev );
865
 
         form = read_leb128U( &abbrev );
 
1030
         name = read_leb128U( &abbrev_img );
 
1031
         form = read_leb128U( &abbrev_img );
866
1032
         if ( name == 0 )
867
1033
            break;
868
1034
       
885
1051
                       /* 2006-01-01: only generate a value if
886
1052
                          debugstr is non-NULL (which means that a
887
1053
                          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); 
 
1054
                                            if (debugstr_img && !ui->dw64)
 
1055
                                               sval = debugstr_img + *((UInt*)p); 
 
1056
                                            if (debugstr_img && ui->dw64)
 
1057
                                               sval = debugstr_img + *((ULong*)p); 
892
1058
                                            p += ui->dw64 ? 8 : 4; 
893
1059
                                            break;
894
1060
            case 0x08: /* FORM_string */    sval = (Char*)p; 
951
1117
 */
952
1118
void ML_(read_debuginfo_dwarf2) 
953
1119
        ( 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 */
 
1120
          UChar* debuginfo_img,   Int debug_info_sz, /* .debug_info */
 
1121
          UChar* debugabbrev_img,                    /* .debug_abbrev */
 
1122
          UChar* debugline_img,   Int debug_line_sz, /* .debug_line */
 
1123
          UChar* debugstr_img )                      /* .debug_str */
958
1124
{
959
1125
   UnitInfo ui;
960
1126
   UShort   ver;
961
 
   UChar*   block;
962
 
   UChar*   end = debuginfo + debug_info_sz;
 
1127
   UChar*   block_img;
 
1128
   UChar*   end_img = debuginfo_img + debug_info_sz;
963
1129
   ULong    blklen;
964
1130
   Bool     blklen_is_64;
965
1131
   Int      blklen_len = 0;
971
1137
   }
972
1138
 
973
1139
   /* Iterate on all the blocks we find in .debug_info */
974
 
   for ( block = debuginfo; block < end - 4; block += blklen + blklen_len ) {
 
1140
   for ( block_img = debuginfo_img; block_img < end_img - 4; 
 
1141
                                    block_img += blklen + blklen_len ) {
975
1142
 
976
1143
      /* Read the compilation unit header in .debug_info section - See
977
1144
         p 70 */
978
1145
      /* This block length */
979
 
      blklen     = read_initial_length_field( block, &blklen_is_64 );
 
1146
      blklen     = read_initial_length_field( block_img, &blklen_is_64 );
980
1147
      blklen_len = blklen_is_64 ? 12 : 4;
981
 
      if ( block + blklen + blklen_len > end ) {
 
1148
      if ( block_img + blklen + blklen_len > end_img ) {
982
1149
         ML_(symerr)( "Last block truncated in .debug_info; ignoring" );
983
1150
         return;
984
1151
      }
985
1152
 
986
1153
      /* version should be 2 */
987
 
      ver = *((UShort*)( block + blklen_len ));
 
1154
      ver = *((UShort*)( block_img + blklen_len ));
988
1155
      if ( ver != 2 ) {
989
1156
         ML_(symerr)( "Ignoring non-dwarf2 block in .debug_info" );
990
1157
         continue;
992
1159
      
993
1160
      /* Fill ui with offset in .debug_line and compdir */
994
1161
      if (0)
995
 
         VG_(printf)( "Reading UnitInfo at 0x%x.....\n", block - debuginfo );
996
 
      read_unitinfo_dwarf2( &ui, block, debugabbrev, debugstr );
 
1162
         VG_(printf)( "Reading UnitInfo at 0x%x.....\n", 
 
1163
                      block_img - debuginfo_img );
 
1164
      read_unitinfo_dwarf2( &ui, block_img, debugabbrev_img, debugstr_img, si );
997
1165
      if (0)
998
1166
         VG_(printf)( "   => LINES=0x%llx    NAME=%s     DIR=%s\n", 
999
1167
                      ui.stmt_list, ui.name, ui.compdir );
1006
1174
         VG_(printf)("debug_line_sz %d, ui.stmt_list %lld  %s\n", 
1007
1175
                     debug_line_sz, ui.stmt_list, ui.name );
1008
1176
      /* 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 );
 
1177
      read_dwarf2_lineblock( 
 
1178
         si, debug_offset, &ui, debugline_img + ui.stmt_list, 
 
1179
                                debug_line_sz - ui.stmt_list );
1011
1180
   }
1012
1181
}
1013
1182
 
1315
1484
/*--- Read call-frame info from an .eh_frame section       ---*/
1316
1485
/*------------------------------------------------------------*/
1317
1486
 
 
1487
/* Sources of info:
 
1488
 
 
1489
   The DWARF3 spec, available from http://www.dwarfstd.org/Download.php 
 
1490
 
 
1491
   This describes how to read CFA data from .debug_frame sections.
 
1492
   So as to maximise everybody's annoyance and confusion, .eh_frame
 
1493
   sections are almost the same as .debug_frame sections, but differ
 
1494
   in a few subtle and ill documented but important aspects.
 
1495
 
 
1496
   Generic ELF Specification, sections 7.5 (DWARF Extensions) and 7.6
 
1497
   (Exception Frames), available from
 
1498
 
 
1499
   http://www.linux-foundation.org/spec/book/ELF-generic/ELF-generic.html
 
1500
 
 
1501
   This really does describe .eh_frame, at least the aspects that
 
1502
   differ from standard DWARF3.  It's better than guessing, and
 
1503
   (marginally) more fun than reading the gdb source code.
 
1504
*/
 
1505
 
1318
1506
/* Useful info ..
1319
1507
 
1320
1508
   In general:
1327
1515
   the CFA]. 
1328
1516
 
1329
1517
   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:
 
1518
   gdb-6.3/gdb/tdep-amd64.c and also amd64-abi-0.98.pdf:
1331
1519
 
1332
1520
      0    1    2    3    4    5    6    7
1333
1521
      RAX  RDX  RCX  RBX  RSI  RDI  RBP  RSP
1336
1524
      R8 ... R15
1337
1525
 
1338
1526
      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.
 
1527
      "The table defines Return Address to have a register number,
 
1528
      even though the address is stored in 0(%rsp) and not in a 
 
1529
      physical register."
 
1530
 
 
1531
      17   ...   24
 
1532
      XMM0 ... XMM7
 
1533
 
 
1534
      25   ...    32
 
1535
      XMM8 ... XMM15
 
1536
 
 
1537
      33   ...   40
 
1538
      ST0  ...  ST7
 
1539
 
 
1540
      41   ...   48
 
1541
      MM0  ...  MM7
 
1542
 
 
1543
      49                  RFLAGS
 
1544
      50,51,52,53,54,55   ES,CS,SS,DS,FS,GS
 
1545
      58                  FS.BASE  (what's that?)
 
1546
      59                  GS.BASE  (what's that?)
 
1547
      62                  TR (task register)
 
1548
      63                  LDTR (LDT register)
 
1549
      64                  MXCSR
 
1550
      65                  FCW (x87 control word)
 
1551
      66                  FSW (x86 status word)
1342
1552
 
1343
1553
   On x86 I cannot find any documentation.  It _appears_ to be the
1344
1554
   actual instruction encoding, viz:
1348
1558
 
1349
1559
      8 is the return address (EIP) */
1350
1560
 
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.
 
1561
 
 
1562
/* Comments re DW_CFA_set_loc, 16 Nov 06.
 
1563
 
 
1564
   JRS:
 
1565
   Someone recently sent me a libcrypto.so.0.9.8 as distributed with
 
1566
   Ubuntu of some flavour, compiled with gcc 4.1.2 on amd64.  It
 
1567
   causes V's CF reader to complain a lot:
 
1568
 
 
1569
   >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
 
1570
   >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
 
1571
   >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
 
1572
   >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
 
1573
   >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:48
 
1574
   >> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
 
1575
 
 
1576
   After chasing this around a bit it seems that the CF bytecode
 
1577
   parser lost sync at a DW_CFA_set_loc, which has a single argument
 
1578
   denoting an address.
 
1579
 
 
1580
   As it stands that address is extracted by read_Addr().  On amd64
 
1581
   that just fetches 8 bytes regardless of anything else.
 
1582
 
 
1583
   read_encoded_Addr() is more sophisticated.  This appears to take
 
1584
   into account some kind of encoding flag.  When I replace the uses
 
1585
   of read_Addr by read_encoded_Addr for DW_CFA_set_loc, the
 
1586
   complaints go away, there is no loss of sync, and the parsed CF
 
1587
   instructions are the same as shown by readelf --debug-dump=frames.
 
1588
 
 
1589
   So it seems a plausible fix.  The problem is I looked in the DWARF3
 
1590
   spec and completely failed to figure out whether or not the arg to
 
1591
   DW_CFA_set_loc is supposed to be encoded in a way suitable for
 
1592
   read_encoded_Addr, nor for that matter any description of what it
 
1593
   is that read_encoded_Addr is really decoding.
 
1594
 
 
1595
   TomH:
 
1596
   The problem is that the encoding is not standard - the eh_frame
 
1597
   section uses the same encoding as the dwarf_frame section except
 
1598
   for a few small changes, and this is one of them. So this is not
 
1599
   something the DWARF standard covers.
 
1600
 
 
1601
   There is an augmentation string to indicate what is going on though
 
1602
   so that programs can recognise it.
 
1603
 
 
1604
   What we are doing seems to match what gdb 6.5 and libdwarf 20060614
 
1605
   do though. I'm not sure about readelf though.
 
1606
 
 
1607
   (later): Well dwarfdump barfs on it:
 
1608
 
 
1609
      dwarfdump ERROR:  dwarf_get_fde_info_for_reg:  
 
1610
                        DW_DLE_DF_FRAME_DECODING_ERROR(193) (193)
 
1611
 
 
1612
   I've looked at binutils as well now, and the code in readelf agrees
 
1613
   with your patch - ie it treats set_loc as having an encoded address
 
1614
   if there is a zR augmentation indicating an encoding.
 
1615
 
 
1616
   Quite why gdb and libdwarf don't understand this is an interesting
 
1617
   question...
 
1618
 
 
1619
   Final outcome: all uses of read_Addr were replaced by
 
1620
   read_encoded_Addr.  A new type AddressDecodingInfo was added to
 
1621
   make it relatively clean to plumb through the extra info needed by
 
1622
   read_encoded_Addr.
 
1623
*/
 
1624
 
 
1625
/* More badness re address encoding, 12 Jan 07.
 
1626
 
 
1627
   Most gcc provided CIEs have a "zR" augmentation, which means they
 
1628
   supply their own address encoding, and that works fine.  However,
 
1629
   some icc9 supplied CIEs have no augmentation, which means they use
 
1630
   the default_Addr_encoding().  That says to use a machine-word sized
 
1631
   value, literally unmodified.
 
1632
 
 
1633
   Since .so's are, in general, relocated when loaded, having absolute
 
1634
   addresses in the CFI data makes no sense when read_encoded_Addr is
 
1635
   used to find the initial location for a FDE.  The resulting saga:
 
1636
 
 
1637
   TomH:
 
1638
   > I'm chasing a stack backtrace failure for an amd64 .so which was 
 
1639
   > created I believe by icc 9.1.  After a while I wound up looking at
 
1640
   > this: (readdwarf.c)
 
1641
   >
 
1642
   >   5083        tom static UChar default_Addr_encoding ( void )
 
1643
   >   3584        tom {
 
1644
   >   3584        tom    switch (sizeof(Addr)) {
 
1645
   >   3584        tom       case 4: return DW_EH_PE_udata4;
 
1646
   >   3584        tom       case 8: return DW_EH_PE_udata8;
 
1647
   >   3584        tom       default: vg_assert(0);
 
1648
   >   3584        tom    }
 
1649
   >   3584        tom }
 
1650
   >
 
1651
   > If a CIE does not have an "augmentation string" (typically "zR") then 
 
1652
   > addresses are decoded as described by default_Addr_encoding.  If there
 
1653
   > is an 'R' in the augmentation string then the encoding to use 
 
1654
   > is specified by the CIE itself, which works fine with GCC compiled code
 
1655
   > since that always appears to specify zR.
 
1656
 
 
1657
   Correct.
 
1658
 
 
1659
   > Problem is this .so has no augmentation string and so uses the
 
1660
   > default encoding, viz DW_EH_PE_udata8.  That appears to mean
 
1661
   > "read a 64 bit number" and use that as-is (for the starting value
 
1662
   > of the program counter when running the CFA program).
 
1663
 
 
1664
   Strictly speaking the default is DW_EH_PE_absptr, but that amounts
 
1665
   to either udata4 or udata8 depending on the platform's pointer size
 
1666
   which is a shortcut I used.
 
1667
 
 
1668
   > For this .so that gives nonsense (very small) PCs which are later
 
1669
   > rejected by the sanity check which ensures PC ranges fall inside
 
1670
   > the mapped text segment.  It seems like the .so expects to have the
 
1671
   > start VMA of the text segment added on.  This would correspond to
 
1672
   >
 
1673
   >   static UChar default_Addr_encoding ( void )
 
1674
   >   {
 
1675
   >      switch (sizeof(Addr)) {
 
1676
   >         case 4: return DW_EH_PE_textrel + DW_EH_PE_udata4;
 
1677
   >         case 8: return DW_EH_PE_textrel + DW_EH_PE_udata8;
 
1678
   >         default: vg_assert(0);
 
1679
   >      }
 
1680
   >   }
 
1681
 
 
1682
   The problem you're seeing is that you have absolute pointers inside
 
1683
   a shared library, which obviously makes little sense on the face of
 
1684
   things as how would the linker know where the library will be
 
1685
   loaded?
 
1686
 
 
1687
   The answer of course is that it doesn't, so if it points absolute
 
1688
   pointers in the frame unwind data is has to include relocations for
 
1689
   them, and I'm betting that if you look at the relocations in the
 
1690
   library you will there are some for that data.
 
1691
 
 
1692
   That is fine of course when ld.so maps the library - it will
 
1693
   relocate the eh_frame data as it maps it (or prelinking will
 
1694
   already have done so) and when the g++ exception code kicks in and
 
1695
   unwinds the stack it will see relocated data.
 
1696
 
 
1697
   We of course are mapping the section from the ELF file ourselves
 
1698
   and are not applying the relocations, hence the problem you are
 
1699
   seeing.
 
1700
 
 
1701
   Strictly speaking we should apply the relocations but the cheap
 
1702
   solution is essentially to do what you've done - strictly speaking
 
1703
   you should adjust by the difference between the address the library
 
1704
   was linked for and the address it has been loaded at, but a shared
 
1705
   library will normally be linked for address zero I believe. It's
 
1706
   possible that prelinking might change that though?
 
1707
 
 
1708
   JRS:
 
1709
   That all syncs with what I am seeing.
 
1710
 
 
1711
   So what I am inclined to do is:
 
1712
 
 
1713
   - Leave default_Addr_encoding as it is
 
1714
 
 
1715
   - Change read_encoded_Addr's handling of "case DW_EH_PE_absptr" so
 
1716
     it sets base to, as you say, the difference between the address
 
1717
     the library was linked for and the address it has been loaded at
 
1718
     (== the SegInfo's text_bias)
 
1719
 
 
1720
   Does that sound sane?  I think it should even handle the prelinked
 
1721
   case.
 
1722
 
 
1723
   (JRS, later)
 
1724
 
 
1725
   Hmm.  Plausible as it sounds, it doesn't work.  It now produces
 
1726
   bogus backtraces for locations inside the (statically linked)
 
1727
   memcheck executable.
 
1728
 
 
1729
   Besides, there are a couple of other places where read_encoded_Addr
 
1730
   is used -- one of which is used to establish the length of the
 
1731
   address range covered by the current FDE:
 
1732
 
 
1733
         fde_arange = read_encoded_Addr(&nbytes, &adi, data);
 
1734
 
 
1735
   and it doesn't seem to make any sense for read_encoded_Addr to add
 
1736
   on the text segment bias in that context.  The DWARF3 spec says
 
1737
   that both the initial_location and address_range (length) fields
 
1738
   are encoded the same way ("target address"), so it is unclear at
 
1739
   what stage in the process it would be appropriate to relocate the
 
1740
   former but not the latter.
 
1741
 
 
1742
   One unprincipled kludge that does work is the following: just
 
1743
   before handing one of the address range fragments off to
 
1744
   ML_(addDiCfSI) for permanent storage, check its start address.  If
 
1745
   that is very low (less than 2 M), and is far below the mapped text
 
1746
   segment, and adding the text bias would move the fragment entirely
 
1747
   inside the mapped text segment, then do so.  A kind of kludged
 
1748
   last-minute relocation, if you like.
 
1749
 
 
1750
   12 Jan 07: committing said kludge (see kludge_then_addDiCfSI).  If
 
1751
   the situation clarifies, it can easily enough be backed out and
 
1752
   replaced by a better fix.
1358
1753
*/
1359
1754
 
1360
1755
/* --------------- Decls --------------- */
1449
1844
   The result is then summarised into a sequence of CfiSIs, if
1450
1845
   possible.  UnwindContext effectively holds the state of the
1451
1846
   abstract machine whilst it is running.
 
1847
 
 
1848
   The CFA can either be a signed offset from a register,
 
1849
   or an expression:
 
1850
 
 
1851
   CFA = cfa_reg + cfa_off   when UnwindContext.cfa_is_regoff==True
 
1852
       | [[ cfa_expr_id ]]
 
1853
 
 
1854
   When .cfa_is_regoff == True,  cfa_expr_id must be zero
 
1855
   When .cfa_is_regoff == False, cfa_reg must be zero
 
1856
                                 and cfa_off must be zero
 
1857
 
 
1858
   RegRule describes, for each register, how to get its
 
1859
   value in the previous frame, where 'cfa' denotes the cfa
 
1860
   for the frame as a whole:
 
1861
 
 
1862
   RegRule = RR_Undef          -- undefined
 
1863
           | RR_Same           -- same as in previous frame
 
1864
           | RR_CFAOff    arg  -- is at * ( cfa + arg )
 
1865
           | RR_CFAValOff arg  -- is ( cfa + arg )
 
1866
           | RR_Reg       arg  -- is in register 'arg' 
 
1867
           | RR_Expr      arg  -- is at * [[ arg ]]
 
1868
           | RR_ValExpr   arg  -- is [[ arg ]]
 
1869
           | RR_Arch           -- dunno
 
1870
 
 
1871
   Note that RR_Expr is redundant since the same can be represented
 
1872
   using RR_ValExpr with an explicit dereference (CfiExpr_Deref) at
 
1873
   the outermost level.
 
1874
 
 
1875
   All expressions are stored in exprs in the containing
 
1876
   UnwindContext.  Since the UnwindContext gets reinitialised for each
 
1877
   new FDE, summarise_context needs to copy out any expressions it
 
1878
   wants to keep into the cfsi_exprs field of the containing SegInfo.
1452
1879
*/
1453
1880
typedef
1454
1881
   struct {
1455
 
      enum { RR_Undef, RR_Same, RR_CFAoff, RR_Reg, RR_Arch, RR_Expr,
1456
 
             RR_CFAValoff, RR_ValExpr } tag;
1457
 
 
1458
 
      /* Note, .coff and .reg are never both in use.  Therefore could
1459
 
         merge them into one. */
1460
 
 
1461
 
      /* CFA offset if tag==RR_CFAoff */
1462
 
      Int coff;
1463
 
 
1464
 
      /* reg, if tag==RR_Reg */
1465
 
      Int reg;
 
1882
      enum { RR_Undef, RR_Same, RR_CFAOff, RR_CFAValOff, 
 
1883
             RR_Reg, /*RR_Expr,*/ RR_ValExpr, RR_Arch } tag;
 
1884
      /* meaning:  int offset for CFAoff/CFAValOff
 
1885
                   reg # for Reg
 
1886
                   expr index for Expr/ValExpr */
 
1887
      Int arg;
1466
1888
   }
1467
1889
   RegRule;
1468
1890
 
1469
 
static void ppRegRule ( RegRule* reg )
 
1891
static void ppRegRule ( XArray* exprs, RegRule* rrule )
1470
1892
{
1471
 
   switch (reg->tag) {
1472
 
      case RR_Undef:  VG_(printf)("u  "); break;
1473
 
      case RR_Same:   VG_(printf)("s  "); break;
1474
 
      case RR_CFAoff: VG_(printf)("c%d ", reg->coff); break;
1475
 
      case RR_CFAValoff: VG_(printf)("v%d ", reg->coff); break;
1476
 
      case RR_Reg:    VG_(printf)("r%d ", reg->reg); break;
1477
 
      case RR_Arch:   VG_(printf)("a  "); break;
1478
 
      case RR_Expr:   VG_(printf)("e  "); break;
1479
 
      case RR_ValExpr:   VG_(printf)("ve "); break;
1480
 
      default:        VG_(core_panic)("ppRegRule");
 
1893
   vg_assert(exprs);
 
1894
   switch (rrule->tag) {
 
1895
      case RR_Undef:     VG_(printf)("u  "); break;
 
1896
      case RR_Same:      VG_(printf)("s  "); break;
 
1897
      case RR_CFAOff:    VG_(printf)("c%d ", rrule->arg); break;
 
1898
      case RR_CFAValOff: VG_(printf)("v%d ", rrule->arg); break;
 
1899
      case RR_Reg:       VG_(printf)("r%d ", rrule->arg); break;
 
1900
      case RR_ValExpr:   VG_(printf)("ve{"); 
 
1901
                         ML_(ppCfiExpr)( exprs, rrule->arg ); 
 
1902
                         VG_(printf)("} "); 
 
1903
                         break;
 
1904
      case RR_Arch:      VG_(printf)("a  "); break;
 
1905
      default:           VG_(core_panic)("ppRegRule");
1481
1906
   }
1482
1907
}
1483
1908
 
1485
1910
typedef
1486
1911
   struct {
1487
1912
      /* Read-only fields (set by the CIE) */
1488
 
      Int  code_a_f;
1489
 
      Int  data_a_f;
1490
 
      Addr initloc;
1491
 
      Int  ra_reg;
 
1913
      Int     code_a_f;
 
1914
      Int     data_a_f;
 
1915
      Addr    initloc;
 
1916
      Int     ra_reg;
1492
1917
      /* The rest of these fields can be modifed by
1493
1918
         run_CF_instruction. */
1494
1919
      /* The LOC entry */
1495
 
      Addr loc;
1496
 
      /* The CFA entry.  If -1, means we don't know (Dwarf3 Expression). */
1497
 
      Int cfa_reg;
1498
 
      Int cfa_offset; /* in bytes */
 
1920
      Addr    loc;
 
1921
      /* The CFA entry.  This can be either reg+/-offset or an expr. */
 
1922
      Bool    cfa_is_regoff; /* True=>is reg+offset; False=>is expr */
 
1923
      Int     cfa_reg;
 
1924
      Int     cfa_off;  /* in bytes */
 
1925
      Int     cfa_expr_ix; /* index into cfa_exprs */
1499
1926
      /* register unwind rules */
1500
1927
      RegRule reg[N_CFI_REGS];
 
1928
      /* array of CfiExpr, shared by reg[] and cfa_expr_ix */
 
1929
      XArray* exprs;
1501
1930
   }
1502
1931
   UnwindContext;
1503
1932
 
1505
1934
{
1506
1935
   Int i;
1507
1936
   VG_(printf)("0x%llx: ", (ULong)ctx->loc);
1508
 
   VG_(printf)("%d(r%d) ",  ctx->cfa_offset, ctx->cfa_reg);
 
1937
   if (ctx->cfa_is_regoff) {
 
1938
      VG_(printf)("%d(r%d) ",  ctx->cfa_off, ctx->cfa_reg);
 
1939
   } else {
 
1940
      vg_assert(ctx->exprs);
 
1941
      VG_(printf)("{");
 
1942
      ML_(ppCfiExpr)( ctx->exprs, ctx->cfa_expr_ix );
 
1943
      VG_(printf)("} ");
 
1944
   }
1509
1945
   for (i = 0; i < N_CFI_REGS; i++)
1510
 
      ppRegRule(&ctx->reg[i]);
 
1946
      ppRegRule(ctx->exprs, &ctx->reg[i]);
1511
1947
   VG_(printf)("\n");
1512
1948
}
1513
1949
 
 
1950
static void* symtab_alloc ( SizeT szB ) {
 
1951
   return VG_(arena_malloc)( VG_AR_SYMTAB, szB );
 
1952
}
 
1953
static void symtab_free ( void* v ) {
 
1954
   VG_(arena_free)( VG_AR_SYMTAB, v );
 
1955
}
 
1956
 
1514
1957
static void initUnwindContext ( /*OUT*/UnwindContext* ctx )
1515
1958
{
1516
1959
   Int i;
1517
 
   ctx->code_a_f   = 0;
1518
 
   ctx->data_a_f   = 0;
1519
 
   ctx->initloc    = 0;
1520
 
   ctx->ra_reg     = RA_REG_DEFAULT;
1521
 
   ctx->loc        = 0;
1522
 
   ctx->cfa_reg    = 0;
1523
 
   ctx->cfa_offset = 0;
 
1960
   ctx->code_a_f      = 0;
 
1961
   ctx->data_a_f      = 0;
 
1962
   ctx->initloc       = 0;
 
1963
   ctx->ra_reg        = RA_REG_DEFAULT;
 
1964
   ctx->loc           = 0;
 
1965
   ctx->cfa_is_regoff = True;
 
1966
   ctx->cfa_reg       = 0;
 
1967
   ctx->cfa_off       = 0;
 
1968
   ctx->cfa_expr_ix   = 0;
 
1969
   ctx->exprs         = NULL;
1524
1970
   for (i = 0; i < N_CFI_REGS; i++) {
1525
1971
      ctx->reg[i].tag = RR_Undef;
1526
 
      ctx->reg[i].coff = 0;
1527
 
      ctx->reg[i].reg = 0;
 
1972
      ctx->reg[i].arg = 0;
1528
1973
   }
1529
1974
}
1530
1975
 
1531
1976
 
 
1977
/* A structure which holds information needed by read_encoded_Addr(). 
 
1978
*/
 
1979
typedef
 
1980
   struct {
 
1981
      UChar  encoding;
 
1982
      UChar* ehframe_image;
 
1983
      Addr   ehframe_avma;
 
1984
      Addr   text_bias;
 
1985
   }
 
1986
   AddressDecodingInfo;
 
1987
 
 
1988
 
1532
1989
/* ------------ Deal with summary-info records ------------ */
1533
1990
 
1534
1991
static void initCfiSI ( DiCfSI* si )
1535
1992
{
1536
 
   si->base      = 0;
1537
 
   si->len       = 0;
1538
 
   si->cfa_sprel = False;
1539
 
   si->ra_how    = 0;
1540
 
   si->sp_how    = 0;
1541
 
   si->fp_how    = 0;
1542
 
   si->cfa_off   = 0;
1543
 
   si->ra_off    = 0;
1544
 
   si->sp_off    = 0;
1545
 
   si->fp_off    = 0;
 
1993
   si->base    = 0;
 
1994
   si->len     = 0;
 
1995
   si->cfa_how = 0;
 
1996
   si->ra_how  = 0;
 
1997
   si->sp_how  = 0;
 
1998
   si->fp_how  = 0;
 
1999
   si->cfa_off = 0;
 
2000
   si->ra_off  = 0;
 
2001
   si->sp_off  = 0;
 
2002
   si->fp_off  = 0;
1546
2003
}
1547
2004
 
1548
2005
 
1549
2006
/* --------------- Summarisation --------------- */
1550
2007
 
 
2008
/* Forward */
 
2009
static 
 
2010
Int copy_convert_CfiExpr_tree ( XArray*        dst,
 
2011
                                UnwindContext* srcuc, 
 
2012
                                Int            nd );
 
2013
 
1551
2014
/* Summarise ctx into si, if possible.  Returns True if successful.
1552
2015
   This is taken to be just after ctx's loc advances; hence the
1553
2016
   summary is up to but not including the current loc.  This works
1555
2018
*/
1556
2019
static Bool summarise_context( /*OUT*/DiCfSI* si,
1557
2020
                               Addr loc_start,
1558
 
                               UnwindContext* ctx )
 
2021
                               UnwindContext* ctx,
 
2022
                               struct _SegInfo* seginfo )
1559
2023
{
1560
2024
   Int why = 0;
1561
2025
   initCfiSI(si);
1562
2026
 
1563
2027
   /* How to generate the CFA */
1564
 
   if (ctx->cfa_reg == -1) {
1565
 
      /* it was set by DW_CFA_def_cfa_expression; we don't know what
1566
 
         it really is */
1567
 
      why = 6;
1568
 
      goto failed;
1569
 
   } else
1570
 
   if (ctx->cfa_reg == SP_REG) {
1571
 
      si->cfa_sprel = True;
1572
 
      si->cfa_off   = ctx->cfa_offset;
1573
 
   } else
1574
 
   if (ctx->cfa_reg == FP_REG) {
1575
 
      si->cfa_sprel = False;
1576
 
      si->cfa_off   = ctx->cfa_offset;
 
2028
   if (!ctx->cfa_is_regoff) {
 
2029
      /* it was set by DW_CFA_def_cfa_expression; try to convert */
 
2030
      XArray *src, *dst;
 
2031
      Int    conv;
 
2032
      src = ctx->exprs;
 
2033
      dst = seginfo->cfsi_exprs;
 
2034
      if (src && (VG_(sizeXA)(src) > 0) && (!dst)) {
 
2035
         dst = VG_(newXA)( symtab_alloc, symtab_free,
 
2036
                           sizeof(CfiExpr) );
 
2037
         vg_assert(dst);
 
2038
         seginfo->cfsi_exprs = dst;
 
2039
      }
 
2040
      conv = copy_convert_CfiExpr_tree
 
2041
                    ( dst, ctx, ctx->cfa_expr_ix );
 
2042
      vg_assert(conv >= -1);
 
2043
      if (conv == -1) { why = 6; goto failed; }
 
2044
      si->cfa_how = CFIC_EXPR;
 
2045
      si->cfa_off = conv;
 
2046
      if (0 && seginfo->ddump_frames)
 
2047
         ML_(ppCfiExpr)(dst, conv);
 
2048
   } else
 
2049
   if (ctx->cfa_is_regoff && ctx->cfa_reg == SP_REG) {
 
2050
      si->cfa_how = CFIC_SPREL;
 
2051
      si->cfa_off = ctx->cfa_off;
 
2052
   } else
 
2053
   if (ctx->cfa_is_regoff && ctx->cfa_reg == FP_REG) {
 
2054
      si->cfa_how = CFIC_FPREL;
 
2055
      si->cfa_off = ctx->cfa_off;
1577
2056
   } else {
1578
2057
      why = 1;
1579
2058
      goto failed;
1580
2059
   }
1581
2060
 
1582
 
#  define SUMMARISE_HOW(_how, _off, _ctxreg)                             \
1583
 
   switch (_ctxreg.tag) {                                                \
1584
 
      case RR_Undef:  _how = CFIR_UNKNOWN;   _off = 0; break;            \
1585
 
      case RR_Same:   _how = CFIR_SAME;      _off = 0; break;            \
1586
 
      case RR_CFAoff: _how = CFIR_MEMCFAREL; _off = _ctxreg.coff; break; \
1587
 
      case RR_CFAValoff: _how = CFIR_CFAREL; _off = _ctxreg.coff; break; \
1588
 
      default:        { why = 2; goto failed; } /* otherwise give up */  \
 
2061
#  define SUMMARISE_HOW(_how, _off, _ctxreg)                  \
 
2062
   switch (_ctxreg.tag) {                                     \
 
2063
      case RR_Undef:                                          \
 
2064
         _how = CFIR_UNKNOWN;   _off = 0; break;              \
 
2065
      case RR_Same:                                           \
 
2066
         _how = CFIR_SAME;      _off = 0; break;              \
 
2067
      case RR_CFAOff:                                         \
 
2068
         _how = CFIR_MEMCFAREL; _off = _ctxreg.arg; break;    \
 
2069
      case RR_CFAValOff:                                      \
 
2070
         _how = CFIR_CFAREL;    _off = _ctxreg.arg; break;    \
 
2071
      case RR_ValExpr: {                                      \
 
2072
         XArray *src, *dst;                                   \
 
2073
         Int    conv;                                         \
 
2074
         src = ctx->exprs;                                    \
 
2075
         dst = seginfo->cfsi_exprs;                           \
 
2076
         if (src && (VG_(sizeXA)(src) > 0) && (!dst)) {       \
 
2077
            dst = VG_(newXA)( symtab_alloc, symtab_free,      \
 
2078
                              sizeof(CfiExpr) );              \
 
2079
            vg_assert(dst);                                   \
 
2080
            seginfo->cfsi_exprs = dst;                        \
 
2081
         }                                                    \
 
2082
         conv = copy_convert_CfiExpr_tree                     \
 
2083
                       ( dst, ctx, _ctxreg.arg );             \
 
2084
         vg_assert(conv >= -1);                               \
 
2085
         if (conv == -1) { why = 7; goto failed; }            \
 
2086
         _how = CFIR_EXPR;                                    \
 
2087
         _off = conv;                                         \
 
2088
         if (0 && seginfo->ddump_frames)                      \
 
2089
            ML_(ppCfiExpr)(dst, conv);                        \
 
2090
         break;                                               \
 
2091
      }                                                       \
 
2092
      default:                                                \
 
2093
         why = 2; goto failed; /* otherwise give up */        \
1589
2094
   }
1590
2095
 
1591
2096
   SUMMARISE_HOW(si->ra_how, si->ra_off, ctx->reg[ctx->ra_reg] );
1620
2125
   return True;
1621
2126
 
1622
2127
  failed:
1623
 
   if (VG_(clo_verbosity) > 2 || VG_(clo_trace_cfi)) {
 
2128
   if (VG_(clo_verbosity) > 2 || seginfo->trace_cfi) {
1624
2129
      VG_(message)(Vg_DebugMsg,
1625
2130
                  "summarise_context(loc_start = %p)"
1626
2131
                  ": cannot summarise(why=%d):   ", loc_start, why);
1629
2134
   return False;
1630
2135
}
1631
2136
 
 
2137
/* Copy the tree rooted at srcuc->exprs node srcix to dstxa, on the
 
2138
   way converting any DwReg regs (regs numbered using the Dwarf scheme
 
2139
   defined by each architecture's ABI) into CfiRegs, which are
 
2140
   platform independent.  If the conversion isn't possible because
 
2141
   there is no equivalent register, return -1.  This has the
 
2142
   undesirable side effect of de-dagifying the input; oh well. */
 
2143
static Int copy_convert_CfiExpr_tree ( XArray*        dstxa,
 
2144
                                       UnwindContext* srcuc, 
 
2145
                                       Int            srcix )
 
2146
{
 
2147
   CfiExpr* src;
 
2148
   Int      cpL, cpR, cpA, dwreg;
 
2149
   XArray*  srcxa = srcuc->exprs;
 
2150
   vg_assert(srcxa);
 
2151
   vg_assert(dstxa);
 
2152
   vg_assert(srcix >= 0 && srcix < VG_(sizeXA)(srcxa));
 
2153
 
 
2154
   src = VG_(indexXA)( srcxa, srcix );
 
2155
   switch (src->tag) {
 
2156
      case Cex_Undef:
 
2157
         return ML_(CfiExpr_Undef)( dstxa );
 
2158
      case Cex_Deref:
 
2159
         cpA = copy_convert_CfiExpr_tree( dstxa, srcuc, src->Cex.Deref.ixAddr );
 
2160
         if (cpA == -1)
 
2161
            return -1; /* propagate failure */
 
2162
         return ML_(CfiExpr_Deref)( dstxa, cpA );
 
2163
      case Cex_Const:
 
2164
         return ML_(CfiExpr_Const)( dstxa, src->Cex.Const.con );
 
2165
      case Cex_Binop:
 
2166
         cpL = copy_convert_CfiExpr_tree( dstxa, srcuc, src->Cex.Binop.ixL );
 
2167
         cpR = copy_convert_CfiExpr_tree( dstxa, srcuc, src->Cex.Binop.ixR );
 
2168
         vg_assert(cpL >= -1 && cpR >= -1);
 
2169
         if (cpL == -1 || cpR == -1)
 
2170
            return -1; /* propagate failure */
 
2171
         return ML_(CfiExpr_Binop)( dstxa, src->Cex.Binop.op, cpL, cpR );
 
2172
      case Cex_CfiReg:
 
2173
         /* should not see these in input (are created only by this
 
2174
            conversion step!) */
 
2175
         VG_(core_panic)("copy_convert_CfiExpr_tree: CfiReg in input");
 
2176
      case Cex_DwReg:
 
2177
         /* This is the only place where the conversion can fail. */
 
2178
         dwreg = src->Cex.DwReg.reg;
 
2179
         if (dwreg == SP_REG)
 
2180
            return ML_(CfiExpr_CfiReg)( dstxa, Creg_SP );
 
2181
         if (dwreg == FP_REG)
 
2182
            return ML_(CfiExpr_CfiReg)( dstxa, Creg_FP );
 
2183
         if (dwreg == srcuc->ra_reg)
 
2184
            return ML_(CfiExpr_CfiReg)( dstxa, Creg_IP ); /* correct? */
 
2185
         /* else we must fail - can't represent the reg */
 
2186
         return -1;
 
2187
      default:
 
2188
         VG_(core_panic)("copy_convert_CfiExpr_tree: default");
 
2189
   }
 
2190
}
 
2191
 
 
2192
 
1632
2193
static void ppUnwindContext_summary ( UnwindContext* ctx )
1633
2194
{
1634
2195
   VG_(printf)("0x%llx-1: ", (ULong)ctx->loc);
1635
2196
 
1636
2197
   if (ctx->cfa_reg == SP_REG) {
1637
 
      VG_(printf)("SP/CFA=%d+SP   ", ctx->cfa_offset);
 
2198
      VG_(printf)("SP/CFA=%d+SP   ", ctx->cfa_off);
1638
2199
   } else
1639
2200
   if (ctx->cfa_reg == FP_REG) {
1640
 
      VG_(printf)("SP/CFA=%d+FP   ", ctx->cfa_offset);
 
2201
      VG_(printf)("SP/CFA=%d+FP   ", ctx->cfa_off);
1641
2202
   } else {
1642
 
      VG_(printf)("SP/CFA=unknown  ", ctx->cfa_offset);
 
2203
      VG_(printf)("SP/CFA=unknown  ", ctx->cfa_off);
1643
2204
   }
1644
2205
 
1645
2206
   VG_(printf)("RA=");
1646
 
   ppRegRule( &ctx->reg[ctx->ra_reg] );
 
2207
   ppRegRule( ctx->exprs, &ctx->reg[ctx->ra_reg] );
1647
2208
 
1648
2209
   VG_(printf)("FP=");
1649
 
   ppRegRule( &ctx->reg[FP_REG] );
 
2210
   ppRegRule( ctx->exprs, &ctx->reg[FP_REG] );
1650
2211
   VG_(printf)("\n");
1651
2212
}
1652
2213
 
1730
2291
   return r;
1731
2292
}
1732
2293
 
1733
 
static Addr read_Addr ( UChar* data )
1734
 
{
1735
 
#  if VG_WORDSIZE == 4
1736
 
   return read_UInt(data);
1737
 
#  else
1738
 
   return read_ULong(data);
1739
 
#  endif
1740
 
}
1741
 
 
1742
2294
static UChar read_UChar ( UChar* data )
1743
2295
{
1744
2296
   return data[0];
1745
2297
}
1746
2298
 
 
2299
static ULong read_le_u_encoded_literal ( UChar* data, UInt size )
 
2300
{
 
2301
   switch (size) {
 
2302
      case 8:  return (ULong)read_ULong( data );
 
2303
      case 4:  return (ULong)read_UInt( data );
 
2304
      case 2:  return (ULong)read_UShort( data );
 
2305
      case 1:  return (ULong)read_UChar( data );
 
2306
      default: vg_assert(0); /*NOTREACHED*/ return 0;
 
2307
   }
 
2308
}
 
2309
 
 
2310
static Long read_le_s_encoded_literal ( UChar* data, UInt size )
 
2311
{
 
2312
   Long s64 = read_le_u_encoded_literal( data, size );
 
2313
   switch (size) {
 
2314
      case 8:  break;
 
2315
      case 4:  s64 <<= 32; s64 >>= 32; break;
 
2316
      case 2:  s64 <<= 48; s64 >>= 48; break;
 
2317
      case 1:  s64 <<= 56; s64 >>= 56; break;
 
2318
      default: vg_assert(0); /*NOTREACHED*/ return 0;
 
2319
   }
 
2320
   return s64;
 
2321
}
 
2322
 
1747
2323
static UChar default_Addr_encoding ( void )
1748
2324
{
1749
2325
   switch (sizeof(Addr)) {
1767
2343
   }
1768
2344
}
1769
2345
 
1770
 
static Addr read_encoded_Addr ( UChar* data, UChar encoding, Int *nbytes,
1771
 
                                UChar* ehframe, Addr ehframe_addr )
 
2346
static Addr read_encoded_Addr ( /*OUT*/Int* nbytes,
 
2347
                                AddressDecodingInfo* adi,
 
2348
                                UChar* data )
1772
2349
{
1773
 
   Addr base;
1774
 
   Int offset;
 
2350
   /* Regarding the handling of DW_EH_PE_absptr.  DWARF3 says this
 
2351
      denotes an absolute address, hence you would think 'base' is
 
2352
      zero.  However, that is nonsensical (unless relocations are to
 
2353
      be applied to the unwind data before reading it, which sounds
 
2354
      unlikely).  My interpretation is that DW_EH_PE_absptr indicates
 
2355
      an address relative to where the object was loaded (technically,
 
2356
      relative to its stated load VMA, hence the use of text_bias
 
2357
      rather than text_avma).  Hmm, should we use text_bias or
 
2358
      text_avma here?  Not sure.
 
2359
 
 
2360
      This view appears to be supported by DWARF3 spec sec 7.3
 
2361
      "Executable Objects and Shared Objects":
 
2362
 
 
2363
         This requirement makes the debugging information for shared
 
2364
         objects position independent.  Virtual addresses in a shared
 
2365
         object may be calculated by adding the offset to the base
 
2366
         address at which the object was attached.  This offset is
 
2367
         available in the run-time linker's data structures.
 
2368
   */
 
2369
   Addr   base;
 
2370
   Word   offset;
 
2371
   UChar  encoding      = adi->encoding;
 
2372
   UChar* ehframe_image = adi->ehframe_image;
 
2373
   Addr   ehframe_avma  = adi->ehframe_avma;
1775
2374
 
1776
2375
   vg_assert((encoding & DW_EH_PE_indirect) == 0);
1777
2376
 
1779
2378
 
1780
2379
   switch (encoding & 0x70) {
1781
2380
      case DW_EH_PE_absptr:
1782
 
         base = 0;
 
2381
         base = adi->text_bias;
1783
2382
         break;
1784
2383
      case DW_EH_PE_pcrel:
1785
 
         base = ehframe_addr + ( data - ehframe );
 
2384
         base = ehframe_avma + ( data - ehframe_image );
1786
2385
         break;
1787
2386
      case DW_EH_PE_datarel:
1788
2387
         vg_assert(0);
1797
2396
         break;
1798
2397
      case DW_EH_PE_aligned:
1799
2398
         base = 0;
1800
 
         offset = data - ehframe;
 
2399
         offset = data - ehframe_image;
1801
2400
         if ((offset % sizeof(Addr)) != 0) {
1802
2401
            *nbytes = sizeof(Addr) - (offset % sizeof(Addr));
1803
2402
            data += *nbytes;
1835
2434
}
1836
2435
 
1837
2436
 
 
2437
/* ------------ Run/show DWARF3 expressions ---------- */
 
2438
 
 
2439
/* Taken from binutils-2.17/include/elf/dwarf2.h */
 
2440
enum dwarf_location_atom
 
2441
  {
 
2442
    DW_OP_addr = 0x03,
 
2443
    DW_OP_deref = 0x06,
 
2444
    DW_OP_const1u = 0x08,
 
2445
    DW_OP_const1s = 0x09,
 
2446
    DW_OP_const2u = 0x0a,
 
2447
    DW_OP_const2s = 0x0b,
 
2448
    DW_OP_const4u = 0x0c,
 
2449
    DW_OP_const4s = 0x0d,
 
2450
    DW_OP_const8u = 0x0e,
 
2451
    DW_OP_const8s = 0x0f,
 
2452
    DW_OP_constu = 0x10,
 
2453
    DW_OP_consts = 0x11,
 
2454
    DW_OP_dup = 0x12,
 
2455
    DW_OP_drop = 0x13,
 
2456
    DW_OP_over = 0x14,
 
2457
    DW_OP_pick = 0x15,
 
2458
    DW_OP_swap = 0x16,
 
2459
    DW_OP_rot = 0x17,
 
2460
    DW_OP_xderef = 0x18,
 
2461
    DW_OP_abs = 0x19,
 
2462
    DW_OP_and = 0x1a,
 
2463
    DW_OP_div = 0x1b,
 
2464
    DW_OP_minus = 0x1c,
 
2465
    DW_OP_mod = 0x1d,
 
2466
    DW_OP_mul = 0x1e,
 
2467
    DW_OP_neg = 0x1f,
 
2468
    DW_OP_not = 0x20,
 
2469
    DW_OP_or = 0x21,
 
2470
    DW_OP_plus = 0x22,
 
2471
    DW_OP_plus_uconst = 0x23,
 
2472
    DW_OP_shl = 0x24,
 
2473
    DW_OP_shr = 0x25,
 
2474
    DW_OP_shra = 0x26,
 
2475
    DW_OP_xor = 0x27,
 
2476
    DW_OP_bra = 0x28,
 
2477
    DW_OP_eq = 0x29,
 
2478
    DW_OP_ge = 0x2a,
 
2479
    DW_OP_gt = 0x2b,
 
2480
    DW_OP_le = 0x2c,
 
2481
    DW_OP_lt = 0x2d,
 
2482
    DW_OP_ne = 0x2e,
 
2483
    DW_OP_skip = 0x2f,
 
2484
    DW_OP_lit0 = 0x30,
 
2485
    DW_OP_lit1 = 0x31,
 
2486
    DW_OP_lit2 = 0x32,
 
2487
    DW_OP_lit3 = 0x33,
 
2488
    DW_OP_lit4 = 0x34,
 
2489
    DW_OP_lit5 = 0x35,
 
2490
    DW_OP_lit6 = 0x36,
 
2491
    DW_OP_lit7 = 0x37,
 
2492
    DW_OP_lit8 = 0x38,
 
2493
    DW_OP_lit9 = 0x39,
 
2494
    DW_OP_lit10 = 0x3a,
 
2495
    DW_OP_lit11 = 0x3b,
 
2496
    DW_OP_lit12 = 0x3c,
 
2497
    DW_OP_lit13 = 0x3d,
 
2498
    DW_OP_lit14 = 0x3e,
 
2499
    DW_OP_lit15 = 0x3f,
 
2500
    DW_OP_lit16 = 0x40,
 
2501
    DW_OP_lit17 = 0x41,
 
2502
    DW_OP_lit18 = 0x42,
 
2503
    DW_OP_lit19 = 0x43,
 
2504
    DW_OP_lit20 = 0x44,
 
2505
    DW_OP_lit21 = 0x45,
 
2506
    DW_OP_lit22 = 0x46,
 
2507
    DW_OP_lit23 = 0x47,
 
2508
    DW_OP_lit24 = 0x48,
 
2509
    DW_OP_lit25 = 0x49,
 
2510
    DW_OP_lit26 = 0x4a,
 
2511
    DW_OP_lit27 = 0x4b,
 
2512
    DW_OP_lit28 = 0x4c,
 
2513
    DW_OP_lit29 = 0x4d,
 
2514
    DW_OP_lit30 = 0x4e,
 
2515
    DW_OP_lit31 = 0x4f,
 
2516
    DW_OP_reg0 = 0x50,
 
2517
    DW_OP_reg1 = 0x51,
 
2518
    DW_OP_reg2 = 0x52,
 
2519
    DW_OP_reg3 = 0x53,
 
2520
    DW_OP_reg4 = 0x54,
 
2521
    DW_OP_reg5 = 0x55,
 
2522
    DW_OP_reg6 = 0x56,
 
2523
    DW_OP_reg7 = 0x57,
 
2524
    DW_OP_reg8 = 0x58,
 
2525
    DW_OP_reg9 = 0x59,
 
2526
    DW_OP_reg10 = 0x5a,
 
2527
    DW_OP_reg11 = 0x5b,
 
2528
    DW_OP_reg12 = 0x5c,
 
2529
    DW_OP_reg13 = 0x5d,
 
2530
    DW_OP_reg14 = 0x5e,
 
2531
    DW_OP_reg15 = 0x5f,
 
2532
    DW_OP_reg16 = 0x60,
 
2533
    DW_OP_reg17 = 0x61,
 
2534
    DW_OP_reg18 = 0x62,
 
2535
    DW_OP_reg19 = 0x63,
 
2536
    DW_OP_reg20 = 0x64,
 
2537
    DW_OP_reg21 = 0x65,
 
2538
    DW_OP_reg22 = 0x66,
 
2539
    DW_OP_reg23 = 0x67,
 
2540
    DW_OP_reg24 = 0x68,
 
2541
    DW_OP_reg25 = 0x69,
 
2542
    DW_OP_reg26 = 0x6a,
 
2543
    DW_OP_reg27 = 0x6b,
 
2544
    DW_OP_reg28 = 0x6c,
 
2545
    DW_OP_reg29 = 0x6d,
 
2546
    DW_OP_reg30 = 0x6e,
 
2547
    DW_OP_reg31 = 0x6f,
 
2548
    DW_OP_breg0 = 0x70,
 
2549
    DW_OP_breg1 = 0x71,
 
2550
    DW_OP_breg2 = 0x72,
 
2551
    DW_OP_breg3 = 0x73,
 
2552
    DW_OP_breg4 = 0x74,
 
2553
    DW_OP_breg5 = 0x75,
 
2554
    DW_OP_breg6 = 0x76,
 
2555
    DW_OP_breg7 = 0x77,
 
2556
    DW_OP_breg8 = 0x78,
 
2557
    DW_OP_breg9 = 0x79,
 
2558
    DW_OP_breg10 = 0x7a,
 
2559
    DW_OP_breg11 = 0x7b,
 
2560
    DW_OP_breg12 = 0x7c,
 
2561
    DW_OP_breg13 = 0x7d,
 
2562
    DW_OP_breg14 = 0x7e,
 
2563
    DW_OP_breg15 = 0x7f,
 
2564
    DW_OP_breg16 = 0x80,
 
2565
    DW_OP_breg17 = 0x81,
 
2566
    DW_OP_breg18 = 0x82,
 
2567
    DW_OP_breg19 = 0x83,
 
2568
    DW_OP_breg20 = 0x84,
 
2569
    DW_OP_breg21 = 0x85,
 
2570
    DW_OP_breg22 = 0x86,
 
2571
    DW_OP_breg23 = 0x87,
 
2572
    DW_OP_breg24 = 0x88,
 
2573
    DW_OP_breg25 = 0x89,
 
2574
    DW_OP_breg26 = 0x8a,
 
2575
    DW_OP_breg27 = 0x8b,
 
2576
    DW_OP_breg28 = 0x8c,
 
2577
    DW_OP_breg29 = 0x8d,
 
2578
    DW_OP_breg30 = 0x8e,
 
2579
    DW_OP_breg31 = 0x8f,
 
2580
    DW_OP_regx = 0x90,
 
2581
    DW_OP_fbreg = 0x91,
 
2582
    DW_OP_bregx = 0x92,
 
2583
    DW_OP_piece = 0x93,
 
2584
    DW_OP_deref_size = 0x94,
 
2585
    DW_OP_xderef_size = 0x95,
 
2586
    DW_OP_nop = 0x96,
 
2587
    /* DWARF 3 extensions.  */
 
2588
    DW_OP_push_object_address = 0x97,
 
2589
    DW_OP_call2 = 0x98,
 
2590
    DW_OP_call4 = 0x99,
 
2591
    DW_OP_call_ref = 0x9a,
 
2592
    DW_OP_form_tls_address = 0x9b,
 
2593
    DW_OP_call_frame_cfa = 0x9c,
 
2594
    DW_OP_bit_piece = 0x9d,
 
2595
    /* GNU extensions.  */
 
2596
    DW_OP_GNU_push_tls_address = 0xe0,
 
2597
    /* HP extensions.  */
 
2598
    DW_OP_HP_unknown     = 0xe0, /* Ouch, the same as GNU_push_tls_address.  */
 
2599
    DW_OP_HP_is_value    = 0xe1,
 
2600
    DW_OP_HP_fltconst4   = 0xe2,
 
2601
    DW_OP_HP_fltconst8   = 0xe3,
 
2602
    DW_OP_HP_mod_range   = 0xe4,
 
2603
    DW_OP_HP_unmod_range = 0xe5,
 
2604
    DW_OP_HP_tls         = 0xe6
 
2605
  };
 
2606
 
 
2607
 
 
2608
/* Convert the DWARF3 expression in expr[0 .. exprlen-1] into a dag
 
2609
   (of CfiExprs) stored in ctx->exprs, and return the index in
 
2610
   ctx->exprs of the root node.  Or fail in which case return -1. */
 
2611
/* IMPORTANT: when adding expression forms here, also remember to
 
2612
   add suitable evaluation code in evalCfiExpr in debuginfo.c. */
 
2613
static Int dwarfexpr_to_dag ( UnwindContext* ctx, 
 
2614
                              UChar* expr, Int exprlen, 
 
2615
                              Bool push_cfa_at_start,
 
2616
                              Bool ddump_frames )
 
2617
{
 
2618
#  define N_EXPR_STACK 20
 
2619
 
 
2620
#  define PUSH(_arg)                               \
 
2621
      do {                                         \
 
2622
         vg_assert(sp >= -1 && sp < N_EXPR_STACK); \
 
2623
         if (sp == N_EXPR_STACK-1)                 \
 
2624
            return -1;                             \
 
2625
         sp++;                                     \
 
2626
         stack[sp] = (_arg);                       \
 
2627
      } while (0)
 
2628
 
 
2629
#  define POP(_lval)                               \
 
2630
      do {                                         \
 
2631
         vg_assert(sp >= -1 && sp < N_EXPR_STACK); \
 
2632
         if (sp == -1)                             \
 
2633
            return -1;                             \
 
2634
         _lval = stack[sp];                        \
 
2635
         sp--;                                     \
 
2636
      } while (0)
 
2637
 
 
2638
   Int    ix, ix2, reg;
 
2639
   UChar  opcode;
 
2640
   Word   sw;
 
2641
   UWord  uw;
 
2642
   CfiOp  op;
 
2643
   HChar* opname;
 
2644
 
 
2645
   Int sp; /* # of top element: valid is -1 .. N_EXPR_STACK-1 */
 
2646
   Int stack[N_EXPR_STACK];  /* indices into ctx->exprs */
 
2647
 
 
2648
   XArray* dst   = ctx->exprs;
 
2649
   UChar*  limit = expr + exprlen;
 
2650
 
 
2651
   vg_assert(dst);
 
2652
   vg_assert(exprlen >= 0);
 
2653
 
 
2654
   sp = -1; /* empty */
 
2655
 
 
2656
   /* Synthesise the CFA as a CfiExpr */
 
2657
   if (push_cfa_at_start) {
 
2658
      if (ctx->cfa_is_regoff) {
 
2659
         /* cfa is reg +/- offset */
 
2660
         ix = ML_(CfiExpr_Binop)( dst,
 
2661
                 Cop_Add,
 
2662
                 ML_(CfiExpr_DwReg)( dst, ctx->cfa_reg ),
 
2663
                 ML_(CfiExpr_Const)( dst, (UWord)(Word)ctx->cfa_off )
 
2664
              );
 
2665
         PUSH(ix);
 
2666
      } else {
 
2667
         /* CFA is already an expr; use its root node */
 
2668
         PUSH(ctx->cfa_expr_ix);
 
2669
      }
 
2670
   }
 
2671
 
 
2672
   while (True) {
 
2673
 
 
2674
      vg_assert(sp >= -1 && sp < N_EXPR_STACK);
 
2675
 
 
2676
      if (expr > limit) 
 
2677
         return -1;  /* overrun - something's wrong */
 
2678
 
 
2679
      if (expr == limit) {
 
2680
        /* end of expr - return expr on the top of stack. */
 
2681
        if (sp == -1)
 
2682
           return -1; /* stack empty.  Bad. */
 
2683
        else
 
2684
           break;
 
2685
      }
 
2686
 
 
2687
      op = 0; opname = NULL; /* excessively conservative */
 
2688
 
 
2689
      opcode = *expr++;
 
2690
      switch (opcode) {
 
2691
 
 
2692
         case DW_OP_lit0 ... DW_OP_lit31:
 
2693
            /* push: literal 0 .. 31 */
 
2694
            sw = (Word)opcode - (Word)DW_OP_lit0;
 
2695
            vg_assert(sw >= 0 && sw <= 31);
 
2696
            PUSH( ML_(CfiExpr_Const)( dst, (UWord)sw ) );
 
2697
            if (ddump_frames)
 
2698
               VG_(printf)("DW_OP_lit%ld", sw);
 
2699
            break;
 
2700
 
 
2701
         case DW_OP_breg0 ... DW_OP_breg31:
 
2702
            /* push: reg + sleb128 */
 
2703
            reg = (Int)opcode - (Int)DW_OP_breg0;
 
2704
            vg_assert(reg >= 0 && reg <= 31);
 
2705
            sw = read_leb128S( &expr );
 
2706
            ix = ML_(CfiExpr_Binop)( dst,
 
2707
                    Cop_Add,
 
2708
                    ML_(CfiExpr_DwReg)( dst, reg ),
 
2709
                    ML_(CfiExpr_Const)( dst, (UWord)sw )
 
2710
                 );
 
2711
            PUSH(ix);
 
2712
            if (ddump_frames)
 
2713
               VG_(printf)("DW_OP_breg%d: %ld", reg, sw);
 
2714
            break;
 
2715
 
 
2716
         case DW_OP_plus_uconst:
 
2717
            uw = read_leb128U( &expr );
 
2718
            PUSH( ML_(CfiExpr_Const)( dst, uw ) );
 
2719
            POP( ix );
 
2720
            POP( ix2 );
 
2721
            PUSH( ML_(CfiExpr_Binop)( dst, op, ix2, ix ) );
 
2722
            if (ddump_frames)
 
2723
               VG_(printf)("DW_OP_plus_uconst: %lu", uw);
 
2724
            break;
 
2725
 
 
2726
         case DW_OP_const4s:
 
2727
            /* push: 32-bit signed immediate */
 
2728
            sw = read_le_s_encoded_literal( expr, 4 );
 
2729
            expr += 4;
 
2730
            PUSH( ML_(CfiExpr_Const)( dst, (UWord)sw ) );
 
2731
            if (ddump_frames)
 
2732
               VG_(printf)("DW_OP_const4s: %ld", sw);
 
2733
            break;
 
2734
 
 
2735
         case DW_OP_minus:
 
2736
            op = Cop_Sub; opname = "minus"; goto binop;
 
2737
         case DW_OP_plus:
 
2738
            op = Cop_Add; opname = "plus"; goto binop;
 
2739
         case DW_OP_and:
 
2740
            op = Cop_And; opname = "and"; goto binop;
 
2741
         case DW_OP_mul:
 
2742
            op = Cop_Mul; opname = "mul"; goto binop;
 
2743
         binop:
 
2744
            POP( ix );
 
2745
            POP( ix2 );
 
2746
            PUSH( ML_(CfiExpr_Binop)( dst, op, ix2, ix ) );
 
2747
            if (ddump_frames)
 
2748
               VG_(printf)("DW_OP_%s", opname);
 
2749
            break;
 
2750
 
 
2751
         case DW_OP_deref:
 
2752
            POP( ix );
 
2753
            PUSH( ML_(CfiExpr_Deref)( dst, ix ) );
 
2754
            if (ddump_frames)
 
2755
               VG_(printf)("DW_OP_deref");
 
2756
            break;
 
2757
 
 
2758
         default:
 
2759
            if (!VG_(clo_xml))
 
2760
               VG_(message)(Vg_DebugMsg, 
 
2761
                            "Warning: DWARF2 CFI reader: unhandled DW_OP_ "
 
2762
                            "opcode 0x%x", (Int)opcode); 
 
2763
            return -1;
 
2764
      }
 
2765
 
 
2766
      if (expr < limit && ddump_frames)
 
2767
         VG_(printf)("; ");
 
2768
 
 
2769
   }
 
2770
 
 
2771
   vg_assert(sp >= -1 && sp < N_EXPR_STACK);
 
2772
   if (sp == -1)
 
2773
      return -1;
 
2774
 
 
2775
   if (0 && ddump_frames)
 
2776
      ML_(ppCfiExpr)( dst, stack[sp] );
 
2777
   return stack[sp];
 
2778
 
 
2779
#  undef POP
 
2780
#  undef PUSH
 
2781
#  undef N_EXPR_STACK
 
2782
}
 
2783
 
 
2784
 
1838
2785
/* ------------ Run/show CFI instructions ------------ */
1839
2786
 
1840
2787
/* Run a CFI instruction, and also return its length.
1842
2789
*/
1843
2790
static Int run_CF_instruction ( /*MOD*/UnwindContext* ctx, 
1844
2791
                                UChar* instr,
1845
 
                                UnwindContext* restore_ctx )
 
2792
                                UnwindContext* restore_ctx,
 
2793
                                AddressDecodingInfo* adi,
 
2794
                                struct _SegInfo* si )
1846
2795
{
1847
 
   Int   off, reg, reg2, nleb, len;
1848
 
   UInt  delta;
1849
 
   Int   i   = 0;
1850
 
   UChar hi2 = (instr[i] >> 6) & 3;
1851
 
   UChar lo6 = instr[i] & 0x3F;
 
2796
   Int    off, reg, reg2, nleb, len;
 
2797
   UInt   delta;
 
2798
   UChar* expr;
 
2799
   Int    j;
 
2800
   Int    i   = 0;
 
2801
   UChar  hi2 = (instr[i] >> 6) & 3;
 
2802
   UChar  lo6 = instr[i] & 0x3F;
 
2803
   Addr   printing_bias = ((Addr)ctx->initloc) - ((Addr)si->text_bias);
1852
2804
   i++;
1853
2805
 
1854
2806
   if (hi2 == DW_CFA_advance_loc) {
1855
2807
      delta = (UInt)lo6;
1856
2808
      ctx->loc += delta;
 
2809
      if (si->ddump_frames)
 
2810
         VG_(printf)("  DW_CFA_advance_loc: %d to %08lx\n", 
 
2811
                     (Int)delta, (Addr)ctx->loc + printing_bias);
1857
2812
      return i;
1858
2813
   }
1859
2814
 
1860
2815
   if (hi2 == DW_CFA_offset) {
1861
 
      /* Set rule for reg 'lo6' to CFAoffset(off * data_af) */
 
2816
      /* Set rule for reg 'lo6' to CFAOff(off * data_af) */
1862
2817
      off = read_leb128( &instr[i], &nleb, 0 );
1863
2818
      i += nleb;
1864
2819
      reg = (Int)lo6;
1865
2820
      if (reg < 0 || reg >= N_CFI_REGS) 
1866
2821
         return 0; /* fail */
1867
 
      ctx->reg[reg].tag = RR_CFAoff;
1868
 
      ctx->reg[reg].coff = off * ctx->data_a_f;
 
2822
      ctx->reg[reg].tag = RR_CFAOff;
 
2823
      ctx->reg[reg].arg = off * ctx->data_a_f;
 
2824
      if (si->ddump_frames)
 
2825
         VG_(printf)("  DW_CFA_offset: r%d at cfa%s%d\n",
 
2826
                     (Int)reg, ctx->reg[reg].arg < 0 ? "" : "+", 
 
2827
                     (Int)ctx->reg[reg].arg );
1869
2828
      return i;
1870
2829
   }
1871
2830
 
1876
2835
      if (restore_ctx == NULL)
1877
2836
         return 0; /* fail */
1878
2837
      ctx->reg[reg] = restore_ctx->reg[reg];
 
2838
      if (si->ddump_frames)
 
2839
         VG_(printf)("  DW_CFA_restore: r%d\n", (Int)reg);
1879
2840
      return i;
1880
2841
   }
1881
2842
 
1883
2844
 
1884
2845
   switch (lo6) {
1885
2846
      case DW_CFA_nop: 
 
2847
         if (si->ddump_frames)
 
2848
            VG_(printf)("  DW_CFA_nop\n");
1886
2849
         break;
1887
2850
      case DW_CFA_set_loc:
1888
 
         ctx->loc = read_Addr(&instr[i]) - ctx->initloc; i+= sizeof(Addr);
 
2851
         /* WAS: 
 
2852
            ctx->loc = read_Addr(&instr[i]) - ctx->initloc; i+= sizeof(Addr);
 
2853
            Was this ever right? */
 
2854
         /* 2007 Feb 23: No.  binutils/dwarf.c treats it as an encoded
 
2855
            address and that appears to be in accordance with the
 
2856
            DWARF3 spec. */
 
2857
         ctx->loc = read_encoded_Addr(&len, adi, &instr[i]);
 
2858
         i += len;
 
2859
         if (si->ddump_frames)
 
2860
            VG_(printf)("  rci:DW_CFA_set_loc\n");
1889
2861
         break;
1890
2862
      case DW_CFA_advance_loc1:
1891
2863
         delta = (UInt)read_UChar(&instr[i]); i+= sizeof(UChar);
1892
2864
         ctx->loc += delta;
 
2865
         if (si->ddump_frames)
 
2866
            VG_(printf)("  DW_CFA_advance_loc1: %d to %08lx\n", 
 
2867
                        (Int)delta, (Addr)ctx->loc + printing_bias);
1893
2868
         break;
1894
2869
      case DW_CFA_advance_loc2:
1895
2870
         delta = (UInt)read_UShort(&instr[i]); i+= sizeof(UShort);
1896
2871
         ctx->loc += delta;
 
2872
         if (si->ddump_frames)
 
2873
            VG_(printf)("  DW_CFA_advance_loc2: %d to %08lx\n", 
 
2874
                        (Int)delta, (Addr)ctx->loc + printing_bias);
1897
2875
         break;
1898
2876
      case DW_CFA_advance_loc4:
1899
2877
         delta = (UInt)read_UInt(&instr[i]); i+= sizeof(UInt);
1900
2878
         ctx->loc += delta;
 
2879
         if (si->ddump_frames)
 
2880
            VG_(printf)("  DW_CFA_advance_loc4: %d to %08lx\n", 
 
2881
                        (Int)delta, (Addr)ctx->loc + printing_bias);
1901
2882
         break;
1902
2883
 
1903
2884
      case DW_CFA_def_cfa:
1907
2888
         i += nleb;
1908
2889
         if (reg < 0 || reg >= N_CFI_REGS) 
1909
2890
            return 0; /* fail */
1910
 
         ctx->cfa_reg    = reg;
1911
 
         ctx->cfa_offset = off;
 
2891
         ctx->cfa_is_regoff = True;
 
2892
         ctx->cfa_expr_ix   = 0;
 
2893
         ctx->cfa_reg       = reg;
 
2894
         ctx->cfa_off       = off;
 
2895
         if (si->ddump_frames)
 
2896
            VG_(printf)("  DW_CFA_def_cfa: r%d ofs %d\n", (Int)reg, (Int)off);
1912
2897
         break;
1913
2898
 
1914
2899
      case DW_CFA_def_cfa_sf:
1918
2903
         i += nleb;
1919
2904
         if (reg < 0 || reg >= N_CFI_REGS)
1920
2905
            return 0; /* fail */
1921
 
         ctx->cfa_reg    = reg;
1922
 
         ctx->cfa_offset = off;
 
2906
         ctx->cfa_is_regoff = True;
 
2907
         ctx->cfa_expr_ix   = 0;
 
2908
         ctx->cfa_reg       = reg;
 
2909
         ctx->cfa_off       = off * ctx->data_a_f;
 
2910
         if (si->ddump_frames)
 
2911
            VG_(printf)("  rci:DW_CFA_def_cfa_sf\n");
1923
2912
         break;
1924
2913
 
1925
2914
      case DW_CFA_register:
1932
2921
         if (reg2 < 0 || reg2 >= N_CFI_REGS) 
1933
2922
            return 0; /* fail */
1934
2923
         ctx->reg[reg].tag = RR_Reg;
1935
 
         ctx->reg[reg].reg = reg2;
 
2924
         ctx->reg[reg].arg = reg2;
 
2925
         if (si->ddump_frames)
 
2926
            VG_(printf)("  DW_CFA_register: r%d in r%d\n", 
 
2927
                        (Int)reg, (Int)reg2);
1936
2928
         break;
1937
2929
 
1938
2930
      case DW_CFA_offset_extended:
1942
2934
         i += nleb;
1943
2935
         if (reg < 0 || reg >= N_CFI_REGS)
1944
2936
            return 0; /* fail */
1945
 
         ctx->reg[reg].tag = RR_CFAoff;
1946
 
         ctx->reg[reg].coff = off * ctx->data_a_f;
 
2937
         ctx->reg[reg].tag = RR_CFAOff;
 
2938
         ctx->reg[reg].arg = off * ctx->data_a_f;
 
2939
         if (si->ddump_frames)
 
2940
            VG_(printf)("  rci:DW_CFA_offset_extended\n");
1947
2941
         break;
1948
2942
 
1949
2943
      case DW_CFA_offset_extended_sf:
1953
2947
         i += nleb;
1954
2948
         if (reg < 0 || reg >= N_CFI_REGS) 
1955
2949
            return 0; /* fail */
1956
 
         ctx->reg[reg].tag = RR_CFAoff;
1957
 
         ctx->reg[reg].coff = off * ctx->data_a_f;
1958
 
         break;         
 
2950
         ctx->reg[reg].tag = RR_CFAOff;
 
2951
         ctx->reg[reg].arg = off * ctx->data_a_f;
 
2952
         if (si->ddump_frames)
 
2953
            VG_(printf)("  DW_CFA_offset_extended_sf: r%d at cfa%s%d\n", 
 
2954
                        reg, ctx->reg[reg].arg < 0 ? "" : "+", 
 
2955
                        (Int)ctx->reg[reg].arg);
 
2956
         break;
1959
2957
 
1960
2958
      case DW_CFA_GNU_negative_offset_extended:
1961
2959
         reg = read_leb128( &instr[i], &nleb, 0 );
1964
2962
         i += nleb;
1965
2963
         if (reg < 0 || reg >= N_CFI_REGS)
1966
2964
            return 0; /* fail */
1967
 
         ctx->reg[reg].tag = RR_CFAoff;
1968
 
         ctx->reg[reg].coff = -off * ctx->data_a_f;
 
2965
         ctx->reg[reg].tag = RR_CFAOff;
 
2966
         ctx->reg[reg].arg = (-off) * ctx->data_a_f;
 
2967
         if (si->ddump_frames)
 
2968
            VG_(printf)("  rci:DW_CFA_GNU_negative_offset_extended\n");
1969
2969
         break;
1970
2970
 
1971
2971
      case DW_CFA_restore_extended:
1976
2976
         if (restore_ctx == NULL)
1977
2977
            return 0; /* fail */
1978
2978
         ctx->reg[reg] = restore_ctx->reg[reg];
 
2979
         if (si->ddump_frames)
 
2980
            VG_(printf)("  rci:DW_CFA_restore_extended\n");
1979
2981
         break;
1980
2982
 
1981
2983
      case DW_CFA_val_offset:
1985
2987
         i += nleb;
1986
2988
         if (reg < 0 || reg >= N_CFI_REGS)
1987
2989
            return 0; /* fail */
1988
 
         ctx->reg[reg].tag = RR_CFAValoff;
1989
 
         ctx->reg[reg].coff = off * ctx->data_a_f;
 
2990
         ctx->reg[reg].tag = RR_CFAValOff;
 
2991
         ctx->reg[reg].arg = off * ctx->data_a_f;
 
2992
         if (si->ddump_frames)
 
2993
            VG_(printf)("  rci:DW_CFA_val_offset\n");
1990
2994
         break;
1991
2995
 
1992
2996
      case DW_CFA_val_offset_sf:
1996
3000
         i += nleb;
1997
3001
         if (reg < 0 || reg >= N_CFI_REGS)
1998
3002
            return 0; /* fail */
1999
 
         ctx->reg[reg].tag = RR_CFAValoff;
2000
 
         ctx->reg[reg].coff = off * ctx->data_a_f;
 
3003
         ctx->reg[reg].tag = RR_CFAValOff;
 
3004
         ctx->reg[reg].arg = off * ctx->data_a_f;
 
3005
         if (si->ddump_frames)
 
3006
            VG_(printf)("  rci:DW_CFA_val_offset_sf\n");
2001
3007
         break;
2002
3008
 
2003
3009
      case DW_CFA_def_cfa_register:
2005
3011
         i += nleb;
2006
3012
         if (reg < 0 || reg >= N_CFI_REGS) 
2007
3013
            return 0; /* fail */
2008
 
         ctx->cfa_reg = reg;
 
3014
         ctx->cfa_is_regoff = True;
 
3015
         ctx->cfa_expr_ix   = 0;
 
3016
         ctx->cfa_reg       = reg;
 
3017
         /* ->cfa_off unchanged */
 
3018
         if (si->ddump_frames)
 
3019
            VG_(printf)("  DW_CFA_def_cfa_reg: r%d\n", (Int)reg );
2009
3020
         break;
2010
3021
 
2011
3022
      case DW_CFA_def_cfa_offset:
2012
3023
         off = read_leb128( &instr[i], &nleb, 0);
2013
3024
         i += nleb;
2014
 
         ctx->cfa_offset = off;
 
3025
         ctx->cfa_is_regoff = True;
 
3026
         ctx->cfa_expr_ix   = 0;
 
3027
         /* ->reg is unchanged */
 
3028
         ctx->cfa_off       = off;
 
3029
         if (si->ddump_frames)
 
3030
            VG_(printf)("  DW_CFA_def_cfa_offset: %d\n", (Int)off);
2015
3031
         break;
2016
3032
 
2017
3033
      case DW_CFA_def_cfa_offset_sf:
2018
3034
         off = read_leb128( &instr[i], &nleb, 1);
2019
3035
         i += nleb;
2020
 
         ctx->cfa_offset = off * ctx->data_a_f;
 
3036
         ctx->cfa_is_regoff = True;
 
3037
         ctx->cfa_expr_ix   = 0;
 
3038
         /* ->reg is unchanged */
 
3039
         ctx->cfa_off       = off * ctx->data_a_f;
 
3040
         if (si->ddump_frames)
 
3041
            VG_(printf)("  DW_CFA_def_cfa_offset_sf: %d\n", ctx->cfa_off);
 
3042
         break;
 
3043
 
 
3044
      case DW_CFA_undefined:
 
3045
         reg = read_leb128( &instr[i], &nleb, 0);
 
3046
         i += nleb;
 
3047
         if (reg < 0 || reg >= N_CFI_REGS) 
 
3048
            return 0; /* fail */
 
3049
         ctx->reg[reg].tag = RR_Undef;
 
3050
         ctx->reg[reg].arg = 0;
 
3051
         if (si->ddump_frames)
 
3052
            VG_(printf)("  rci:DW_CFA_undefined\n");
2021
3053
         break;
2022
3054
 
2023
3055
      case DW_CFA_GNU_args_size:
2025
3057
            ignores these. */
2026
3058
         off = read_leb128( &instr[i], &nleb, 0 );
2027
3059
         i += nleb;
 
3060
         if (si->ddump_frames)
 
3061
            VG_(printf)("  rci:DW_CFA_GNU_args_size (ignored)\n");
2028
3062
         break;
2029
3063
 
2030
3064
      case DW_CFA_expression:
2031
 
         /* Too difficult to really handle; just skip over it and say
2032
 
            that we don't know what do to with the register. */
2033
 
         if (VG_(clo_trace_cfi))
2034
 
            VG_(printf)("DWARF2 CFI reader: "
2035
 
                        "ignoring DW_CFA_expression\n");
 
3065
         /* Identical to DW_CFA_val_expression except that the value
 
3066
            computed is an address and so needs one final
 
3067
            dereference. */
2036
3068
         reg = read_leb128( &instr[i], &nleb, 0 );
2037
3069
         i += nleb;
2038
3070
         len = read_leb128( &instr[i], &nleb, 0 );
2039
3071
         i += nleb;
 
3072
         expr = &instr[i];
2040
3073
         i += len;
2041
 
         if (reg < 0 || reg >= N_CFI_REGS) 
2042
 
            return 0; /* fail */
2043
 
         ctx->reg[reg].tag = RR_Expr;
 
3074
         if (reg < 0 || reg >= N_CFI_REGS)
 
3075
            return 0; /* fail */
 
3076
         if (si->ddump_frames)
 
3077
            VG_(printf)("  DW_CFA_expression: r%d (", 
 
3078
                        (Int)reg);
 
3079
         /* Convert the expression into a dag rooted at ctx->exprs index j,
 
3080
            or fail. */
 
3081
         j = dwarfexpr_to_dag ( ctx, expr, len, True/*push CFA at start*/, 
 
3082
                                si->ddump_frames);
 
3083
         if (si->ddump_frames)
 
3084
            VG_(printf)(")\n");
 
3085
         vg_assert(j >= -1);
 
3086
         if (j >= 0) {
 
3087
            vg_assert(ctx->exprs);
 
3088
            vg_assert( j < VG_(sizeXA)(ctx->exprs) );
 
3089
         }
 
3090
         if (j == -1)
 
3091
            return 0; /* fail */
 
3092
         /* Add an extra dereference */
 
3093
         j = ML_(CfiExpr_Deref)( ctx->exprs, j );
 
3094
         ctx->reg[reg].tag = RR_ValExpr;
 
3095
         ctx->reg[reg].arg = j;
2044
3096
         break;
2045
3097
 
2046
3098
      case DW_CFA_val_expression:
2047
 
         /* Too difficult to really handle; just skip over it and say
2048
 
            that we don't know what do to with the register. */
2049
 
         if (VG_(clo_trace_cfi))
2050
 
            VG_(printf)("DWARF2 CFI reader: "
2051
 
                        "ignoring DW_CFA_val_expression\n");
2052
3099
         reg = read_leb128( &instr[i], &nleb, 0 );
2053
3100
         i += nleb;
2054
3101
         len = read_leb128( &instr[i], &nleb, 0 );
2055
3102
         i += nleb;
 
3103
         expr = &instr[i];
2056
3104
         i += len;
2057
3105
         if (reg < 0 || reg >= N_CFI_REGS)
2058
3106
            return 0; /* fail */
 
3107
         if (si->ddump_frames)
 
3108
            VG_(printf)("  DW_CFA_val_expression: r%d (", 
 
3109
                        (Int)reg);
 
3110
         /* Convert the expression into a dag rooted at ctx->exprs index j,
 
3111
            or fail. */
 
3112
         j = dwarfexpr_to_dag ( ctx, expr, len, True/*push CFA at start*/, 
 
3113
                                si->ddump_frames);
 
3114
         if (si->ddump_frames)
 
3115
            VG_(printf)(")\n");
 
3116
         vg_assert(j >= -1);
 
3117
         if (j >= 0) {
 
3118
            vg_assert(ctx->exprs);
 
3119
            vg_assert( j < VG_(sizeXA)(ctx->exprs) );
 
3120
         }
 
3121
         if (j == -1)
 
3122
            return 0; /* fail */
2059
3123
         ctx->reg[reg].tag = RR_ValExpr;
 
3124
         ctx->reg[reg].arg = j;
2060
3125
         break;
2061
3126
 
2062
3127
      case DW_CFA_def_cfa_expression:
2063
 
         if (VG_(clo_trace_cfi))
2064
 
            VG_(printf)("DWARF2 CFI reader: "
2065
 
                        "ignoring DW_CFA_def_cfa_expression\n");
2066
3128
         len = read_leb128( &instr[i], &nleb, 0 );
2067
3129
         i += nleb;
 
3130
         expr = &instr[i];
2068
3131
         i += len;
2069
 
         ctx->cfa_reg = -1; /* indicating we don't know */
 
3132
         if (si->ddump_frames)
 
3133
            VG_(printf)("  DW_CFA_def_cfa_expression (");
 
3134
         /* Convert the expression into a dag rooted at ctx->exprs index j,
 
3135
            or fail. */
 
3136
         j = dwarfexpr_to_dag ( ctx, expr, len, True/*push CFA at start*/, 
 
3137
                                si->ddump_frames);
 
3138
         if (si->ddump_frames)
 
3139
            VG_(printf)(")\n");
 
3140
         ctx->cfa_is_regoff = False;
 
3141
         ctx->cfa_reg       = 0;
 
3142
         ctx->cfa_off       = 0;
 
3143
         ctx->cfa_expr_ix   = j;
2070
3144
         break;
2071
3145
 
2072
3146
      case DW_CFA_GNU_window_save:
2073
3147
         /* Ignored.  This appears to be sparc-specific; quite why it
2074
3148
            turns up in SuSE-supplied x86 .so's beats me. */
 
3149
         if (si->ddump_frames)
 
3150
            VG_(printf)("DW_CFA_GNU_window_save\n");
2075
3151
         break;
2076
3152
 
2077
3153
      default: 
2078
3154
         VG_(message)(Vg_DebugMsg, "DWARF2 CFI reader: unhandled CFI "
2079
3155
                                   "instruction 0:%d", (Int)lo6); 
 
3156
         if (si->ddump_frames)
 
3157
            VG_(printf)("  rci:run_CF_instruction:default\n");
2080
3158
         i = 0;
2081
3159
         break;
2082
3160
   }
2085
3163
}
2086
3164
 
2087
3165
 
2088
 
/* Show a CFI instruction, and also return its length. */
 
3166
/* Show a CFI instruction, and also return its length.  Show it as
 
3167
   close as possible (preferably identical) to how GNU binutils
 
3168
   readelf --debug-dump=frames would. */
2089
3169
 
2090
 
static Int show_CF_instruction ( UChar* instr )
 
3170
static Int show_CF_instruction ( UChar* instr,
 
3171
                                 AddressDecodingInfo* adi,
 
3172
                                 Int code_a_f, Int data_a_f )
2091
3173
{
2092
3174
   UInt  delta;
2093
 
   Int   off, reg, reg2, nleb, len;
 
3175
   Int   off, coff, reg, reg2, nleb, len;
2094
3176
   Addr  loc;
2095
3177
   Int   i   = 0;
2096
3178
   UChar hi2 = (instr[i] >> 6) & 3;
2103
3185
                      instr[i+4], instr[i+5], instr[i+6], instr[i+7] );
2104
3186
   
2105
3187
   if (hi2 == DW_CFA_advance_loc) {
2106
 
      VG_(printf)("DW_CFA_advance_loc(%d)\n", (Int)lo6);
 
3188
      VG_(printf)("  sci:DW_CFA_advance_loc(%d)\n", (Int)lo6);
2107
3189
      return i;
2108
3190
   }
2109
3191
 
2110
3192
   if (hi2 == DW_CFA_offset) {
2111
3193
      off = read_leb128( &instr[i], &nleb, 0 );
2112
3194
      i += nleb;
2113
 
      VG_(printf)("DW_CFA_offset(r%d + %d x data_af)\n", (Int)lo6, off);
 
3195
      coff = off * data_a_f;
 
3196
      VG_(printf)("  DW_CFA_offset: r%d at cfa%s%d\n",
 
3197
                  (Int)lo6, coff < 0 ? "" : "+", (Int)coff );
2114
3198
      return i;
2115
3199
   }
2116
3200
 
2117
3201
   if (hi2 == DW_CFA_restore) {
2118
 
      VG_(printf)("DW_CFA_restore(r%d)\n", (Int)lo6);
 
3202
      VG_(printf)("  sci:DW_CFA_restore(r%d)\n", (Int)lo6);
2119
3203
      return i;
2120
3204
   }
2121
3205
 
2124
3208
   switch (lo6) {
2125
3209
 
2126
3210
      case DW_CFA_nop: 
2127
 
         VG_(printf)("DW_CFA_nop\n"); 
 
3211
         VG_(printf)("  DW_CFA_nop\n"); 
2128
3212
         break;
2129
3213
 
2130
3214
      case DW_CFA_set_loc:
2131
 
         loc = read_Addr(&instr[i]); i+= sizeof(Addr);
2132
 
         VG_(printf)("DW_CFA_set_loc(%p)\n", loc); 
 
3215
         /* WAS: loc = read_Addr(&instr[i]); i+= sizeof(Addr); 
 
3216
            (now known to be incorrect -- the address is encoded) */
 
3217
         loc = read_encoded_Addr(&len, adi, &instr[i]);
 
3218
         i += len;
 
3219
         VG_(printf)("  sci:DW_CFA_set_loc(%p)\n", loc); 
2133
3220
         break;
2134
3221
 
2135
3222
      case DW_CFA_advance_loc1:
2136
3223
         delta = (UInt)read_UChar(&instr[i]); i+= sizeof(UChar);
2137
 
         VG_(printf)("DW_CFA_advance_loc1(%d)\n", delta); 
 
3224
         VG_(printf)("  sci:DW_CFA_advance_loc1(%d)\n", delta); 
2138
3225
         break;
2139
3226
 
2140
3227
      case DW_CFA_advance_loc2:
2141
3228
         delta = (UInt)read_UShort(&instr[i]); i+= sizeof(UShort);
2142
 
         VG_(printf)("DW_CFA_advance_loc2(%d)\n", delta); 
 
3229
         VG_(printf)("  sci:DW_CFA_advance_loc2(%d)\n", delta); 
2143
3230
         break;
2144
3231
 
2145
3232
      case DW_CFA_advance_loc4:
2146
3233
         delta = (UInt)read_UInt(&instr[i]); i+= sizeof(UInt);
2147
 
         VG_(printf)("DW_CFA_advance_loc4(%d)\n", delta); 
 
3234
         VG_(printf)("  DW_CFA_advance_loc4(%d)\n", delta); 
2148
3235
         break;
2149
3236
 
2150
3237
      case DW_CFA_def_cfa:
2152
3239
         i += nleb;
2153
3240
         off = read_leb128( &instr[i], &nleb, 0 );
2154
3241
         i += nleb;
2155
 
         VG_(printf)("DW_CFA_def_cfa(r%d, off %d)\n", reg, off); 
 
3242
         VG_(printf)("  DW_CFA_def_cfa: r%d ofs %d\n", (Int)reg, (Int)off); 
2156
3243
         break;
2157
3244
 
2158
3245
      case DW_CFA_def_cfa_sf:
2160
3247
         i += nleb;
2161
3248
         off = read_leb128( &instr[i], &nleb, 1 );
2162
3249
         i += nleb;
2163
 
         VG_(printf)("DW_CFA_def_cfa_sf(r%d, off %d)\n", reg, off);
 
3250
         VG_(printf)("  DW_CFA_def_cfa_sf: r%d ofs %d\n", 
 
3251
                     (Int)reg, (Int)(off * data_a_f));
2164
3252
         break;
2165
3253
 
2166
3254
      case DW_CFA_register:
2168
3256
         i += nleb;
2169
3257
         reg2 = read_leb128( &instr[i], &nleb, 0);
2170
3258
         i += nleb;
2171
 
         VG_(printf)("DW_CFA_register(r%d, r%d)\n", reg, reg2); 
 
3259
         VG_(printf)("  sci:DW_CFA_register(r%d, r%d)\n", reg, reg2); 
2172
3260
         break;
2173
3261
 
2174
3262
      case DW_CFA_def_cfa_register:
2175
3263
         reg = read_leb128( &instr[i], &nleb, 0);
2176
3264
         i += nleb;
2177
 
         VG_(printf)("DW_CFA_def_cfa_register(r%d)\n", reg); 
 
3265
         VG_(printf)("  sci:DW_CFA_def_cfa_register(r%d)\n", reg); 
2178
3266
         break;
2179
3267
 
2180
3268
      case DW_CFA_def_cfa_offset: 
2181
3269
         off = read_leb128( &instr[i], &nleb, 0);
2182
3270
         i += nleb;
2183
 
         VG_(printf)("DW_CFA_def_cfa_offset(%d)\n", off); 
 
3271
         VG_(printf)("  sci:DW_CFA_def_cfa_offset(%d)\n", off); 
2184
3272
         break;
2185
3273
 
2186
3274
      case DW_CFA_def_cfa_offset_sf:
2187
3275
         off = read_leb128( &instr[i], &nleb, 1);
2188
3276
         i += nleb;
2189
 
         VG_(printf)("DW_CFA_def_cfa_offset_sf(%d)\n", off);
 
3277
         VG_(printf)("  sci:DW_CFA_def_cfa_offset_sf(%d)\n", off);
2190
3278
         break;
2191
3279
 
2192
3280
      case DW_CFA_restore_extended:
2193
3281
         reg = read_leb128( &instr[i], &nleb, 0);
2194
3282
         i += nleb;
2195
 
         VG_(printf)("DW_CFA_restore_extended(r%d)\n", reg);
 
3283
         VG_(printf)("  sci:DW_CFA_restore_extended(r%d)\n", reg);
2196
3284
         break;
2197
3285
 
2198
3286
      case DW_CFA_undefined:
2199
3287
         reg = read_leb128( &instr[i], &nleb, 0);
2200
3288
         i += nleb;
2201
 
         VG_(printf)("DW_CFA_undefined(r%d)\n", reg);
 
3289
         VG_(printf)("  sci:DW_CFA_undefined(r%d)\n", reg);
2202
3290
         break;
2203
3291
 
2204
3292
      case DW_CFA_same_value:
2205
3293
         reg = read_leb128( &instr[i], &nleb, 0);
2206
3294
         i += nleb;
2207
 
         VG_(printf)("DW_CFA_same_value(r%d)\n", reg);
 
3295
         VG_(printf)("  sci:DW_CFA_same_value(r%d)\n", reg);
2208
3296
         break;
2209
3297
 
2210
3298
      case DW_CFA_remember_state:
2211
 
         VG_(printf)("DW_CFA_remember_state\n");
 
3299
         VG_(printf)("  sci:DW_CFA_remember_state\n");
2212
3300
         break;
2213
3301
 
2214
3302
      case DW_CFA_restore_state:
2215
 
         VG_(printf)("DW_CFA_restore_state\n");
 
3303
         VG_(printf)("  sci:DW_CFA_restore_state\n");
2216
3304
         break;
2217
3305
 
2218
3306
      case DW_CFA_GNU_args_size:
2219
3307
         off = read_leb128( &instr[i], &nleb, 0 );
2220
3308
         i += nleb;
2221
 
         VG_(printf)("DW_CFA_GNU_args_size(%d)\n", off ); 
 
3309
         VG_(printf)("  sci:DW_CFA_GNU_args_size(%d)\n", off ); 
2222
3310
         break;
2223
3311
 
2224
3312
      case DW_CFA_def_cfa_expression:
2225
3313
         len = read_leb128( &instr[i], &nleb, 0 );
2226
3314
         i += nleb;
2227
3315
         i += len;
2228
 
         VG_(printf)("DW_CFA_def_cfa_expression(length %d)\n", len);
 
3316
         VG_(printf)("  sci:DW_CFA_def_cfa_expression(length %d)\n", len);
2229
3317
         break;
2230
3318
 
2231
3319
      case DW_CFA_expression:
2234
3322
         len = read_leb128( &instr[i], &nleb, 0 );
2235
3323
         i += nleb;
2236
3324
         i += len;
2237
 
         VG_(printf)("DW_CFA_expression(r%d, length %d)\n", reg, len);
 
3325
         VG_(printf)("  sci:DW_CFA_expression(r%d, length %d)\n", reg, len);
2238
3326
         break;
2239
3327
 
2240
3328
      case DW_CFA_val_expression:
2243
3331
         len = read_leb128( &instr[i], &nleb, 0 );
2244
3332
         i += nleb;
2245
3333
         i += len;
2246
 
         VG_(printf)("DW_CFA_val_expression(r%d, length %d)\n", reg, len);
 
3334
         VG_(printf)("  sci:DW_CFA_val_expression(r%d, length %d)\n", reg, len);
2247
3335
         break;
2248
3336
 
2249
3337
      case DW_CFA_offset_extended:
2251
3339
         i += nleb;
2252
3340
         off = read_leb128( &instr[i], &nleb, 0 );
2253
3341
         i += nleb;
2254
 
         VG_(printf)("DW_CFA_offset_extended(r%d, off %d x data_af)\n", reg, off);
 
3342
         VG_(printf)("  sci:DW_CFA_offset_extended(r%d, "
 
3343
                     "off %d x data_af)\n", reg, off);
2255
3344
         break;
2256
3345
 
2257
 
       case DW_CFA_offset_extended_sf:
 
3346
      case DW_CFA_offset_extended_sf:
2258
3347
         reg = read_leb128( &instr[i], &nleb, 0 );
2259
3348
         i += nleb;
2260
3349
         off = read_leb128( &instr[i], &nleb, 1 );
2261
3350
         i += nleb;
2262
 
         VG_(printf)("DW_CFA_offset_extended_sf(r%d, off %d x data_af)\n", reg, off);
 
3351
         coff = (Int)(off * data_a_f);
 
3352
         VG_(printf)("  DW_CFA_offset_extended_sf: r%d at cfa%s%d\n", 
 
3353
                        reg, coff < 0 ? "" : "+", coff);
2263
3354
         break;
2264
3355
 
2265
3356
      case DW_CFA_GNU_negative_offset_extended:
2267
3358
         i += nleb;
2268
3359
         off = read_leb128( &instr[i], &nleb, 0 );
2269
3360
         i += nleb;
2270
 
         VG_(printf)("DW_CFA_GNU_negative_offset_extended(r%d, off %d x data_af)\n", reg, -off);
 
3361
         VG_(printf)("  sci:DW_CFA_GNU_negative_offset_extended"
 
3362
                     "(r%d, off %d x data_af)\n", reg, -off);
2271
3363
         break;
2272
3364
 
2273
3365
      case DW_CFA_val_offset:
2275
3367
         i += nleb;
2276
3368
         off = read_leb128( &instr[i], &nleb, 0 );
2277
3369
         i += nleb;
2278
 
         VG_(printf)("DW_CFA_val_offset(r%d, off %d x data_af)\n", reg, off);
 
3370
         VG_(printf)("  sci:DW_CFA_val_offset(r%d, off %d x data_af)\n", 
 
3371
                     reg, off);
2279
3372
         break;
2280
3373
 
2281
3374
       case DW_CFA_val_offset_sf:
2283
3376
         i += nleb;
2284
3377
         off = read_leb128( &instr[i], &nleb, 1 );
2285
3378
         i += nleb;
2286
 
         VG_(printf)("DW_CFA_val_offset_sf(r%d, off %d x data_af)\n", reg, off);
 
3379
         VG_(printf)("  sci:DW_CFA_val_offset_sf(r%d, off %d x data_af)\n", 
 
3380
                     reg, off);
2287
3381
         break;
2288
3382
 
2289
3383
      case DW_CFA_GNU_window_save:
2290
 
         VG_(printf)("DW_CFA_GNU_window_save\n");
 
3384
         VG_(printf)("  sci:DW_CFA_GNU_window_save\n");
2291
3385
         break;
2292
3386
 
2293
3387
      default: 
2294
 
         VG_(printf)("0:%d\n", (Int)lo6); 
 
3388
         VG_(printf)("  sci:0:%d\n", (Int)lo6); 
2295
3389
         break;
2296
3390
   }
2297
3391
 
2299
3393
}
2300
3394
 
2301
3395
 
2302
 
static void show_CF_instructions ( UChar* instrs, Int ilen )
 
3396
/* Show the instructions in instrs[0 .. ilen-1]. */
 
3397
static void show_CF_instructions ( UChar* instrs, Int ilen,
 
3398
                                   AddressDecodingInfo* adi,
 
3399
                                   Int code_a_f, Int data_a_f )
2303
3400
{
2304
3401
   Int i = 0;
2305
3402
   while (True) {
2306
3403
      if (i >= ilen) break;
2307
 
      i += show_CF_instruction( &instrs[i] );
 
3404
      i += show_CF_instruction( &instrs[i], adi, code_a_f, data_a_f );
2308
3405
   }
2309
3406
}
2310
3407
 
 
3408
 
2311
3409
/* Run the CF instructions in instrs[0 .. ilen-1], until the end is
2312
3410
   reached, or until there is a failure.  Return True iff success. 
2313
3411
*/
2314
3412
static 
2315
3413
Bool run_CF_instructions ( struct _SegInfo* si,
 
3414
                           Bool record,
2316
3415
                           UnwindContext* ctx, UChar* instrs, Int ilen,
2317
3416
                           UWord fde_arange,
2318
 
                           UnwindContext* restore_ctx )
 
3417
                           UnwindContext* restore_ctx,
 
3418
                           AddressDecodingInfo* adi )
2319
3419
{
2320
3420
   DiCfSI cfsi;
2321
3421
   Bool summ_ok;
2326
3426
   while (True) {
2327
3427
      loc_prev = ctx->loc;
2328
3428
      if (i >= ilen) break;
2329
 
      if (0) (void)show_CF_instruction( &instrs[i] );
2330
 
      j = run_CF_instruction( ctx, &instrs[i], restore_ctx );
 
3429
      if (0) (void)show_CF_instruction( &instrs[i], adi, 
 
3430
                                        ctx->code_a_f, ctx->data_a_f );
 
3431
      j = run_CF_instruction( ctx, &instrs[i], restore_ctx, adi, si );
2331
3432
      if (j == 0)
2332
3433
         return False; /* execution failed */
2333
3434
      i += j;
2334
3435
      if (0) ppUnwindContext(ctx);
2335
 
      if (loc_prev != ctx->loc && si) {
2336
 
         summ_ok = summarise_context ( &cfsi, loc_prev, ctx );
 
3436
      if (record && loc_prev != ctx->loc) {
 
3437
         summ_ok = summarise_context ( &cfsi, loc_prev, ctx, si );
2337
3438
         if (summ_ok) {
2338
3439
            ML_(addDiCfSI)(si, &cfsi);
2339
 
            if (VG_(clo_trace_cfi))
2340
 
               ML_(ppDiCfSI)(&cfsi);
 
3440
            if (si->trace_cfi)
 
3441
               ML_(ppDiCfSI)(si->cfsi_exprs, &cfsi);
2341
3442
         }
2342
3443
      }
2343
3444
   }
2344
3445
   if (ctx->loc < fde_arange) {
2345
3446
      loc_prev = ctx->loc;
2346
3447
      ctx->loc = fde_arange;
2347
 
      if (si) {
2348
 
         summ_ok = summarise_context ( &cfsi, loc_prev, ctx );
 
3448
      if (record) {
 
3449
         summ_ok = summarise_context ( &cfsi, loc_prev, ctx, si );
2349
3450
         if (summ_ok) {
2350
3451
            ML_(addDiCfSI)(si, &cfsi);
2351
 
            if (VG_(clo_trace_cfi))
2352
 
               ML_(ppDiCfSI)(&cfsi);
 
3452
            if (si->trace_cfi)
 
3453
               ML_(ppDiCfSI)(si->cfsi_exprs, &cfsi);
2353
3454
         }
2354
3455
      }
2355
3456
   }
2362
3463
typedef
2363
3464
   struct {
2364
3465
      /* This gives the CIE an identity to which FDEs will refer. */
2365
 
      UInt   offset;
 
3466
      ULong  offset;
2366
3467
      /* Code, data factors. */
2367
3468
      Int    code_a_f;
2368
3469
      Int    data_a_f;
2394
3495
static CIE the_CIEs[N_CIEs];
2395
3496
 
2396
3497
 
2397
 
void ML_(read_callframe_info_dwarf2) 
 
3498
void ML_(read_callframe_info_dwarf3)
2398
3499
        ( /*OUT*/struct _SegInfo* si, 
2399
 
          UChar* ehframe, Int ehframe_sz, Addr ehframe_addr )
 
3500
          UChar* ehframe_image, Int ehframe_sz, Addr ehframe_avma )
2400
3501
{
2401
3502
   Int    nbytes;
2402
3503
   HChar* how = NULL;
2403
3504
   Int    n_CIEs = 0;
2404
 
   UChar* data = ehframe;
 
3505
   UChar* data = ehframe_image;
2405
3506
 
2406
 
#if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
2407
 
   // CAB: tmp hack for ppc - no stacktraces for now...
 
3507
#  if defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
 
3508
   /* These targets don't use CFI-based stack unwinding. */
2408
3509
   return;
2409
 
#endif
 
3510
#  endif
2410
3511
 
2411
 
   if (VG_(clo_trace_cfi)) {
 
3512
   if (si->trace_cfi) {
2412
3513
      VG_(printf)("\n-----------------------------------------------\n");
2413
 
      VG_(printf)("CFI info: ehframe %p, ehframe_sz %d\n",
2414
 
                  ehframe, ehframe_sz );
 
3514
      VG_(printf)("CFI info: szB %d, _avma %p, _image %p\n",
 
3515
                  ehframe_sz, (void*)ehframe_avma, (void*)ehframe_image );
2415
3516
      VG_(printf)("CFI info: name %s\n",
2416
3517
                  si->filename );
2417
3518
   }
2439
3540
   */
2440
3541
   while (True) {
2441
3542
      UChar* ciefde_start;
2442
 
      UInt   ciefde_len;
2443
 
      UInt   cie_pointer;
 
3543
      ULong  ciefde_len;
 
3544
      ULong  cie_pointer;
 
3545
      Bool   dw64;
2444
3546
 
2445
3547
      /* Are we done? */
2446
 
      if (data == ehframe + ehframe_sz)
 
3548
      if (data == ehframe_image + ehframe_sz)
2447
3549
         return;
2448
3550
 
2449
3551
      /* Overshot the end?  Means something is wrong */
2450
 
      if (data > ehframe + ehframe_sz) {
 
3552
      if (data > ehframe_image + ehframe_sz) {
2451
3553
         how = "overran the end of .eh_frame";
2452
3554
         goto bad;
2453
3555
      }
2456
3558
         Figure out which it is. */
2457
3559
 
2458
3560
      ciefde_start = data;
2459
 
      if (VG_(clo_trace_cfi)) 
2460
 
         VG_(printf)("\ncie/fde.start   = %p (ehframe + 0x%x)\n", 
2461
 
                     ciefde_start, ciefde_start - ehframe);
 
3561
      if (si->trace_cfi) 
 
3562
         VG_(printf)("\ncie/fde.start   = %p (ehframe_image + 0x%lx)\n", 
 
3563
                     ciefde_start, ciefde_start - ehframe_image);
2462
3564
 
2463
 
      ciefde_len = read_UInt(data); data += sizeof(UInt);
2464
 
      if (VG_(clo_trace_cfi)) 
2465
 
         VG_(printf)("cie/fde.length  = %d\n", ciefde_len);
 
3565
      ciefde_len = (ULong) read_UInt(data); data += sizeof(UInt);
 
3566
      if (si->trace_cfi) 
 
3567
         VG_(printf)("cie/fde.length  = %lld\n", ciefde_len);
2466
3568
 
2467
3569
      /* Apparently, if the .length field is zero, we are at the end
2468
 
         of the sequence.  ?? Neither the DWARF2 spec not the AMD64
2469
 
         ABI spec say this, though. */
 
3570
         of the sequence.  This is stated in the Generic Elf
 
3571
         Specification (see comments far above here) and is one of the
 
3572
         places where .eh_frame and .debug_frame data differ. */
2470
3573
      if (ciefde_len == 0) {
2471
 
         if (data == ehframe + ehframe_sz)
2472
 
            return;
2473
 
         how = "zero-sized CIE/FDE but not at section end";
2474
 
         goto bad;
2475
 
      }
2476
 
 
2477
 
      cie_pointer = read_UInt(data); 
2478
 
      data += sizeof(UInt); /* XXX see XXX below */
2479
 
      if (VG_(clo_trace_cfi)) 
2480
 
         VG_(printf)("cie.pointer     = %d\n", cie_pointer);
 
3574
         if (si->ddump_frames)
 
3575
            VG_(printf)("%08lx ZERO terminator\n\n",
 
3576
                        ((Addr)ciefde_start) - ((Addr)ehframe_image));
 
3577
         return;
 
3578
      }
 
3579
 
 
3580
      /* If the .length field is 0xFFFFFFFF then we're dealing with
 
3581
         64-bit DWARF, and the real length is stored as a 64-bit
 
3582
         number immediately following it. */
 
3583
      dw64 = False;
 
3584
      if (ciefde_len == 0xFFFFFFFFUL) {
 
3585
         dw64 = True;
 
3586
         ciefde_len = read_ULong(data); data += sizeof(ULong);
 
3587
      }
 
3588
 
 
3589
      /* Now get the CIE ID, whose size depends on the DWARF 32 vs
 
3590
         64-ness. */
 
3591
      if (dw64) {
 
3592
         cie_pointer = read_ULong(data); 
 
3593
         data += sizeof(ULong); /* XXX see XXX below */
 
3594
      } else {
 
3595
         cie_pointer = (ULong)read_UInt(data); 
 
3596
         data += sizeof(UInt); /* XXX see XXX below */
 
3597
      }
 
3598
 
 
3599
      if (si->trace_cfi) 
 
3600
         VG_(printf)("cie.pointer     = %lld\n", cie_pointer);
2481
3601
 
2482
3602
      /* If cie_pointer is zero, we've got a CIE; else it's an FDE. */
2483
3603
      if (cie_pointer == 0) {
2487
3607
         UChar* cie_augmentation;
2488
3608
 
2489
3609
         /* --------- CIE --------- */
2490
 
         if (VG_(clo_trace_cfi)) 
 
3610
         if (si->trace_cfi) 
2491
3611
            VG_(printf)("------ new CIE (#%d of 0 .. %d) ------\n", 
2492
3612
                        n_CIEs, N_CIEs - 1);
2493
3613
 
2504
3624
 
2505
3625
         /* Record its offset.  This is how we will find it again
2506
3626
            later when looking at an FDE. */
2507
 
         the_CIEs[this_CIE].offset = ciefde_start - ehframe;
 
3627
         the_CIEs[this_CIE].offset = (ULong)(ciefde_start - ehframe_image);
 
3628
 
 
3629
         if (si->ddump_frames)
 
3630
            VG_(printf)("%08lx %08lx %08lx CIE\n",
 
3631
                        ((Addr)ciefde_start) - ((Addr)ehframe_image),
 
3632
                        (Addr)ciefde_len,
 
3633
                        (Addr)(UWord)cie_pointer );
2508
3634
 
2509
3635
         cie_version = read_UChar(data); data += sizeof(UChar);
2510
 
         if (VG_(clo_trace_cfi))
 
3636
         if (si->trace_cfi)
2511
3637
            VG_(printf)("cie.version     = %d\n", (Int)cie_version);
 
3638
         if (si->ddump_frames)
 
3639
            VG_(printf)("  Version:               %d\n", (Int)cie_version);
2512
3640
         if (cie_version != 1) {
2513
3641
            how = "unexpected CIE version (not 1)";
2514
3642
            goto bad;
2516
3644
 
2517
3645
         cie_augmentation = data;
2518
3646
         data += 1 + VG_(strlen)(cie_augmentation);
2519
 
         if (VG_(clo_trace_cfi)) 
 
3647
         if (si->trace_cfi) 
2520
3648
            VG_(printf)("cie.augment     = \"%s\"\n", cie_augmentation);
 
3649
         if (si->ddump_frames)
 
3650
            VG_(printf)("  Augmentation:          \"%s\"\n", cie_augmentation);
2521
3651
 
2522
3652
         if (cie_augmentation[0] == 'e' && cie_augmentation[1] == 'h') {
2523
3653
            data += sizeof(Addr);
2526
3656
 
2527
3657
         the_CIEs[this_CIE].code_a_f = read_leb128( data, &nbytes, 0);
2528
3658
         data += nbytes;
2529
 
         if (VG_(clo_trace_cfi)) 
 
3659
         if (si->trace_cfi) 
2530
3660
            VG_(printf)("cie.code_af     = %d\n", 
2531
3661
                        the_CIEs[this_CIE].code_a_f);
 
3662
         if (si->ddump_frames)
 
3663
            VG_(printf)("  Code alignment factor: %d\n",
 
3664
                        (Int)the_CIEs[this_CIE].code_a_f);
2532
3665
 
2533
3666
         the_CIEs[this_CIE].data_a_f = read_leb128( data, &nbytes, 1);
2534
3667
         data += nbytes;
2535
 
         if (VG_(clo_trace_cfi)) 
 
3668
         if (si->trace_cfi) 
2536
3669
            VG_(printf)("cie.data_af     = %d\n",
2537
3670
                        the_CIEs[this_CIE].data_a_f);
 
3671
         if (si->ddump_frames)
 
3672
            VG_(printf)("  Data alignment factor: %d\n",
 
3673
                        (Int)the_CIEs[this_CIE].data_a_f);
2538
3674
 
2539
3675
         the_CIEs[this_CIE].ra_reg = (Int)read_UChar(data); 
2540
3676
         data += sizeof(UChar);
2541
 
         if (VG_(clo_trace_cfi)) 
 
3677
         if (si->trace_cfi) 
2542
3678
            VG_(printf)("cie.ra_reg      = %d\n", 
2543
3679
                        the_CIEs[this_CIE].ra_reg);
 
3680
         if (si->ddump_frames)
 
3681
            VG_(printf)("  Return address column: %d\n",
 
3682
                        (Int)the_CIEs[this_CIE].ra_reg);
 
3683
 
2544
3684
         if (the_CIEs[this_CIE].ra_reg < 0 
2545
3685
             || the_CIEs[this_CIE].ra_reg >= N_CFI_REGS) {
2546
3686
            how = "cie.ra_reg has implausible value";
2554
3694
            data += nbytes;
2555
3695
            the_CIEs[this_CIE].instrs = data + length;
2556
3696
            cie_augmentation++;
 
3697
            if (si->ddump_frames) {
 
3698
               UInt i;
 
3699
               VG_(printf)("  Augmentation data:    ");
 
3700
               for (i = 0; i < length; i++)
 
3701
                  VG_(printf)(" %02x", (UInt)data[i]);
 
3702
               VG_(printf)("\n");
 
3703
            }
2557
3704
         } else {
2558
3705
            the_CIEs[this_CIE].instrs = NULL;
2559
3706
         }
2591
3738
 
2592
3739
        done_augmentation:
2593
3740
 
2594
 
         if (VG_(clo_trace_cfi)) 
 
3741
         if (si->trace_cfi) 
2595
3742
            VG_(printf)("cie.encoding    = 0x%x\n", 
2596
3743
                        the_CIEs[this_CIE].address_encoding);
2597
3744
 
2598
3745
         the_CIEs[this_CIE].instrs = data;
2599
3746
         the_CIEs[this_CIE].ilen
2600
3747
            = ciefde_start + ciefde_len + sizeof(UInt) - data;
2601
 
         if (VG_(clo_trace_cfi)) {
 
3748
         if (si->trace_cfi) {
2602
3749
            VG_(printf)("cie.instrs      = %p\n", the_CIEs[this_CIE].instrs);
2603
3750
            VG_(printf)("cie.ilen        = %d\n", the_CIEs[this_CIE].ilen);
2604
3751
         }
2611
3758
 
2612
3759
         data += the_CIEs[this_CIE].ilen;
2613
3760
 
2614
 
         if (VG_(clo_trace_cfi)) 
2615
 
            show_CF_instructions(the_CIEs[this_CIE].instrs, 
2616
 
                                 the_CIEs[this_CIE].ilen);
 
3761
         /* Show the CIE's instructions (the preamble for each FDE
 
3762
            that uses this CIE). */ 
 
3763
         if (si->ddump_frames)
 
3764
            VG_(printf)("\n");
 
3765
 
 
3766
         if (si->trace_cfi || si->ddump_frames) {
 
3767
            AddressDecodingInfo adi;
 
3768
            adi.encoding      = the_CIEs[this_CIE].address_encoding;
 
3769
            adi.ehframe_image = ehframe_image;
 
3770
            adi.ehframe_avma  = ehframe_avma;
 
3771
            adi.text_bias     = si->text_bias;
 
3772
            show_CF_instructions( the_CIEs[this_CIE].instrs, 
 
3773
                                  the_CIEs[this_CIE].ilen, &adi,
 
3774
                                  the_CIEs[this_CIE].code_a_f,
 
3775
                                  the_CIEs[this_CIE].data_a_f );
 
3776
         }
 
3777
 
 
3778
         if (si->ddump_frames)
 
3779
            VG_(printf)("\n");
2617
3780
 
2618
3781
      } else {
2619
3782
 
 
3783
         AddressDecodingInfo adi;
2620
3784
         UnwindContext ctx, restore_ctx;
2621
3785
         Int    cie;
2622
 
         UInt   look_for;
 
3786
         ULong  look_for;
2623
3787
         Bool   ok;
2624
3788
         Addr   fde_initloc;
2625
3789
         UWord  fde_arange;
2631
3795
         /* Find the relevant CIE.  The CIE we want is located
2632
3796
            cie_pointer bytes back from here. */
2633
3797
 
2634
 
         /* re sizeof(UInt), matches XXX above.  For 64-bit dwarf this
2635
 
            will have to be a ULong instead. */
2636
 
         look_for = (data - sizeof(UInt) - ehframe) - cie_pointer;
 
3798
         /* re sizeof(UInt) / sizeof(ULong), matches XXX above. */
 
3799
         look_for = (data - (dw64 ? sizeof(ULong) : sizeof(UInt)) 
 
3800
                          - ehframe_image) 
 
3801
                    - cie_pointer;
2637
3802
 
2638
3803
         for (cie = 0; cie < n_CIEs; cie++) {
2639
 
            if (0) VG_(printf)("look for %d   %d\n",
 
3804
            if (0) VG_(printf)("look for %lld   %lld\n",
2640
3805
                               look_for, the_CIEs[cie].offset );
2641
3806
            if (the_CIEs[cie].offset == look_for)
2642
3807
               break;
2647
3812
            goto bad;
2648
3813
         }
2649
3814
 
2650
 
         fde_initloc 
2651
 
            = read_encoded_Addr(data, the_CIEs[cie].address_encoding,
2652
 
                                &nbytes, ehframe, ehframe_addr);
 
3815
         adi.encoding      = the_CIEs[cie].address_encoding;
 
3816
         adi.ehframe_image = ehframe_image;
 
3817
         adi.ehframe_avma  = ehframe_avma;
 
3818
         adi.text_bias     = si->text_bias;
 
3819
         fde_initloc = read_encoded_Addr(&nbytes, &adi, data);
2653
3820
         data += nbytes;
2654
 
         if (VG_(clo_trace_cfi)) 
 
3821
         if (si->trace_cfi) 
2655
3822
            VG_(printf)("fde.initloc     = %p\n", (void*)fde_initloc);
2656
3823
 
2657
 
         fde_arange 
2658
 
            = read_encoded_Addr(data, the_CIEs[cie].address_encoding & 0xf,
2659
 
                                &nbytes, ehframe, ehframe_addr);
2660
 
         data += nbytes;
2661
 
         if (VG_(clo_trace_cfi)) 
 
3824
         adi.encoding      = the_CIEs[cie].address_encoding & 0xf;
 
3825
         adi.ehframe_image = ehframe_image;
 
3826
         adi.ehframe_avma  = ehframe_avma;
 
3827
         adi.text_bias     = si->text_bias;
 
3828
 
 
3829
         /* WAS (incorrectly):
 
3830
            fde_arange = read_encoded_Addr(&nbytes, &adi, data);
 
3831
            data += nbytes;
 
3832
            The following corresponds to what binutils/dwarf.c does:
 
3833
         */
 
3834
         { UInt ptr_size = size_of_encoded_Addr( adi.encoding );
 
3835
           switch (ptr_size) {
 
3836
              case 8: case 4: case 2: case 1: 
 
3837
                 fde_arange 
 
3838
                    = (UWord)read_le_u_encoded_literal(data, ptr_size);
 
3839
                 data += ptr_size;
 
3840
                 break;
 
3841
              default: 
 
3842
                 how = "unknown arange field encoding in FDE";
 
3843
                 goto bad;
 
3844
           }
 
3845
         }
 
3846
 
 
3847
         if (si->trace_cfi) 
2662
3848
            VG_(printf)("fde.arangec     = %p\n", (void*)fde_arange);
2663
3849
 
 
3850
         if (si->ddump_frames)
 
3851
            VG_(printf)("%08lx %08lx %08lx FDE cie=%08lx pc=%08lx..%08lx\n",
 
3852
                        ((Addr)ciefde_start) - ((Addr)ehframe_image),
 
3853
                        (Addr)ciefde_len,
 
3854
                        (Addr)(UWord)cie_pointer,
 
3855
                        (Addr)look_for, 
 
3856
                        ((Addr)fde_initloc) - si->text_bias, 
 
3857
                        ((Addr)fde_initloc) - si->text_bias + fde_arange);
 
3858
 
2664
3859
         if (the_CIEs[cie].saw_z_augmentation) {
2665
 
            data += read_leb128( data, &nbytes, 0);
 
3860
            UInt length = read_leb128( data, &nbytes, 0);
2666
3861
            data += nbytes;
 
3862
            if (si->ddump_frames && (length > 0)) {
 
3863
               UInt i;
 
3864
               VG_(printf)("  Augmentation data:    ");
 
3865
               for (i = 0; i < length; i++)
 
3866
                  VG_(printf)(" %02x", (UInt)data[i]);
 
3867
               VG_(printf)("\n\n");
 
3868
            }
 
3869
            data += length;
2667
3870
         }
2668
3871
 
2669
3872
         fde_instrs = data;
2670
3873
         fde_ilen   = ciefde_start + ciefde_len + sizeof(UInt) - data;
2671
 
         if (VG_(clo_trace_cfi)) {
 
3874
         if (si->trace_cfi) {
2672
3875
            VG_(printf)("fde.instrs      = %p\n", fde_instrs);
2673
3876
            VG_(printf)("fde.ilen        = %d\n", (Int)fde_ilen);
2674
3877
         }
2680
3883
 
2681
3884
         data += fde_ilen;
2682
3885
 
2683
 
         if (VG_(clo_trace_cfi)) 
2684
 
            show_CF_instructions(fde_instrs, fde_ilen);
 
3886
         adi.encoding      = the_CIEs[cie].address_encoding;
 
3887
         adi.ehframe_image = ehframe_image;
 
3888
         adi.ehframe_avma  = ehframe_avma;
 
3889
         adi.text_bias     = si->text_bias;
 
3890
 
 
3891
         if (si->trace_cfi)
 
3892
            show_CF_instructions( fde_instrs, fde_ilen, &adi,
 
3893
                                  the_CIEs[cie].code_a_f,
 
3894
                                  the_CIEs[cie].data_a_f );
2685
3895
 
2686
3896
         initUnwindContext(&ctx);
2687
3897
         ctx.code_a_f = the_CIEs[cie].code_a_f;
2688
3898
         ctx.data_a_f = the_CIEs[cie].data_a_f;
2689
3899
         ctx.initloc  = fde_initloc;
2690
3900
         ctx.ra_reg   = the_CIEs[cie].ra_reg;
2691
 
 
2692
 
         initUnwindContext(&restore_ctx);
2693
 
 
2694
 
         ok = run_CF_instructions(
2695
 
                 NULL, &ctx, the_CIEs[cie].instrs, 
2696
 
                             the_CIEs[cie].ilen, 0, NULL);
 
3901
         ctx.exprs    = VG_(newXA)( symtab_alloc, symtab_free, 
 
3902
                                    sizeof(CfiExpr) );
 
3903
         vg_assert(ctx.exprs);
 
3904
 
 
3905
         /* Run the CIE's instructions.  Ugly hack: if
 
3906
            --debug-dump=frames is in effect, suppress output for
 
3907
            these instructions since they will already have been shown
 
3908
            at the time the CIE was first encountered.  Note, not
 
3909
            thread safe - if this reader is ever made threaded, should
 
3910
            fix properly. */
 
3911
         { Bool hack = si->ddump_frames; 
 
3912
           si->ddump_frames = False;
 
3913
           initUnwindContext(&restore_ctx);
 
3914
           ok = run_CF_instructions(
 
3915
                   si, False, &ctx, the_CIEs[cie].instrs, 
 
3916
                   the_CIEs[cie].ilen, 0, NULL, &adi
 
3917
                );
 
3918
           si->ddump_frames = hack;
 
3919
         }
 
3920
         /* And now run the instructions for the FDE, starting from
 
3921
            the state created by running the CIE preamble
 
3922
            instructions. */
2697
3923
         if (ok) {
2698
3924
            restore_ctx = ctx;
2699
3925
            ok = run_CF_instructions(
2700
 
                    si, &ctx, fde_instrs, fde_ilen, fde_arange, 
2701
 
                    &restore_ctx);
 
3926
                    si, True, &ctx, fde_instrs, fde_ilen, fde_arange, 
 
3927
                    &restore_ctx, &adi
 
3928
                 );
 
3929
            if (si->ddump_frames)
 
3930
               VG_(printf)("\n");
2702
3931
         }
 
3932
 
 
3933
         VG_(deleteXA)( ctx.exprs );
2703
3934
      }
2704
3935
   }
2705
3936