readdwarf3.c revision 7cf4e6b6aed533af53339f36099ed244dc4a5b7f
1
2/*--------------------------------------------------------------------*/
3/*--- Read DWARF3 ".debug_info" sections (DIE trees).              ---*/
4/*---                                                 readdwarf3.c ---*/
5/*--------------------------------------------------------------------*/
6
7/*
8   This file is part of Valgrind, a dynamic binary instrumentation
9   framework.
10
11   Copyright (C) 2008-2008 OpenWorks LLP
12      info@open-works.co.uk
13
14   This program is free software; you can redistribute it and/or
15   modify it under the terms of the GNU General Public License as
16   published by the Free Software Foundation; either version 2 of the
17   License, or (at your option) any later version.
18
19   This program is distributed in the hope that it will be useful, but
20   WITHOUT ANY WARRANTY; without even the implied warranty of
21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   General Public License for more details.
23
24   You should have received a copy of the GNU General Public License
25   along with this program; if not, write to the Free Software
26   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27   02111-1307, USA.
28
29   The GNU General Public License is contained in the file COPYING.
30
31   Neither the names of the U.S. Department of Energy nor the
32   University of California nor the names of its contributors may be
33   used to endorse or promote products derived from this software
34   without prior written permission.
35*/
36
37/* REFERENCE (without which this code will not make much sense):
38
39   DWARF Debugging Information Format, Version 3,
40   dated 20 December 2005 (the "D3 spec").
41
42   Available at http://www.dwarfstd.org/Dwarf3.pdf.  There's also a
43   .doc (MS Word) version, but for some reason the section numbers
44   between the Word and PDF versions differ by 1 in the first digit.
45   All section references in this code are to the PDF version.
46
47   CURRENT HACKS:
48
49   DW_TAG_{const,volatile}_type no DW_AT_type is allowed; it is
50      assumed to mean "const void" or "volatile void" respectively.
51      GDB appears to interpret them like this, anyway.
52
53   In many cases it is important to know the svma of a CU (the "base
54   address of the CU", as the D3 spec calls it).  There are some
55   situations in which the spec implies this value is unknown, but the
56   Dwarf3 produced by gcc-4.1 seems to assume is not unknown but
57   merely zero when not explicitly stated.  So we too have to make
58   that assumption.
59
60   TODO, 2008 Feb 17:
61
62   get rid of cu_svma_known and document the assumed-zero svma hack.
63
64   ML_(sizeOfType): differentiate between zero sized types and types
65   for which the size is unknown.  Is this important?  I don't know.
66
67   DW_AT_array_types: deal with explicit sizes (currently we compute
68   the size from the bounds and the element size, although that's
69   fragile, if the bounds incompletely specified, or completely
70   absent)
71
72   Document reason for difference (by 1) of stack preening depth in
73   parse_var_DIE vs parse_type_DIE.
74
75   Don't hand to ML_(addVars), vars whose locations are entirely in
76   registers (DW_OP_reg*).  This is merely a space-saving
77   optimisation, as ML_(evaluate_Dwarf3_Expr) should handle these
78   expressions correctly, by failing to evaluate them and hence
79   effectively ignoring the variable with which they are associated.
80
81   Deal with DW_AT_array_types which have element size != stride
82
83   In some cases, the info for a variable is split between two
84   different DIEs (generally a declarer and a definer).  We punt on
85   these.  Could do better here.
86
87   The 'data_bias' argument passed to the expression evaluator
88   (ML_(evaluate_Dwarf3_Expr)) should really be changed to a
89   MaybeUWord, to make it clear when we do vs don't know what it is
90   for the evaluation of an expression.  At the moment zero is passed
91   for this parameter in the don't know case.  That's a bit fragile
92   and obscure; using a MaybeUWord would be clearer.
93
94   POTENTIAL PERFORMANCE IMPROVEMENTS:
95
96   The number of type entities that end up in the list of TyAdmins
97   rapidly becomes huge (eg, for libQtGui.so.4.3.2 (amd64-linux, size
98   80729047 bytes), there are 786860 entries in the list).  Mostly
99   this seems to be caused by g++ adding type DIEs for all the basic
100   types once for each source file contributing to the compilation
101   unit, and for a large library they add up quickly.  That causes
102   both a lot of work for this reader module, and also wastes vast
103   amounts of memory storing this duplicated information.  We could
104   surely do a lot better here.
105
106   Handle interaction between read_DIE and parse_{var,type}_DIE
107   better.  Currently read_DIE reads the entire DIE just to find where
108   the end is (and for debug printing), so that it can later reliably
109   move the cursor to the end regardless of what parse_{var,type}_DIE
110   do.  This means many DIEs (most, even?) are read twice.  It would
111   be smarter to make parse_{var,type}_DIE return a Bool indicating
112   whether or not they advanced the DIE cursor, and only if they
113   didn't should read_DIE itself read through the DIE.
114
115   ML_(addVar) and add_var_to_arange: quite a lot of DiAddrRanges have
116   zero variables in their .vars XArray.  Rather than have an XArray
117   with zero elements (which uses 2 malloc'd blocks), allow the .vars
118   pointer to be NULL in this case.
119
120   More generally, reduce the amount of memory allocated and freed
121   while reading Dwarf3 type/variable information.  Even modest (20MB)
122   objects cause this module to allocate and free hundreds of
123   thousands of small blocks, and ML_(arena_malloc) and its various
124   groupies always show up at the top of performance profiles. */
125
126#include "pub_core_basics.h"
127#include "pub_core_libcbase.h"
128#include "pub_core_libcassert.h"
129#include "pub_core_libcprint.h"
130#include "pub_core_options.h"
131#include "pub_core_xarray.h"
132#include "priv_misc.h"             /* dinfo_zalloc/free */
133#include "priv_tytypes.h"
134#include "priv_d3basics.h"
135#include "priv_storage.h"
136#include "priv_readdwarf3.h"       /* self */
137
138
139/*------------------------------------------------------------*/
140/*---                                                      ---*/
141/*--- Basic machinery for parsing DIEs.                    ---*/
142/*---                                                      ---*/
143/*------------------------------------------------------------*/
144
145#define TRACE_D3(format, args...) \
146   if (td3) { VG_(printf)(format, ## args); }
147
148#define D3_INVALID_CUOFF  ((void*)(-1UL))
149#define D3_FAKEVOID_CUOFF ((void*)(-2UL))
150
151typedef
152   struct {
153      UChar* region_start_img;
154      UWord  region_szB;
155      UWord  region_next;
156      void (*barf)( HChar* ) __attribute__((noreturn));
157      HChar* barfstr;
158   }
159   Cursor;
160
161static inline Bool is_sane_Cursor ( Cursor* c ) {
162   if (!c)                return False;
163   if (!c->barf)          return False;
164   if (!c->barfstr)       return False;
165   return True;
166}
167
168static void init_Cursor ( Cursor* c,
169                          UChar*  region_start_img,
170                          UWord   region_szB,
171                          UWord   region_next,
172                          __attribute__((noreturn)) void (*barf)( HChar* ),
173                          HChar*  barfstr )
174{
175   vg_assert(c);
176   VG_(memset)(c, 0, sizeof(*c));
177   c->region_start_img = region_start_img;
178   c->region_szB       = region_szB;
179   c->region_next      = region_next;
180   c->barf             = barf;
181   c->barfstr          = barfstr;
182   vg_assert(is_sane_Cursor(c));
183}
184
185static Bool is_at_end_Cursor ( Cursor* c ) {
186   vg_assert(is_sane_Cursor(c));
187   return c->region_next >= c->region_szB;
188}
189
190static inline UWord get_position_of_Cursor ( Cursor* c ) {
191   vg_assert(is_sane_Cursor(c));
192   return c->region_next;
193}
194static inline void set_position_of_Cursor ( Cursor* c, UWord pos ) {
195   c->region_next = pos;
196   vg_assert(is_sane_Cursor(c));
197}
198
199static /*signed*/Word get_remaining_length_Cursor ( Cursor* c ) {
200   vg_assert(is_sane_Cursor(c));
201   return c->region_szB - c->region_next;
202}
203
204static UChar* get_address_of_Cursor ( Cursor* c ) {
205   vg_assert(is_sane_Cursor(c));
206   return &c->region_start_img[ c->region_next ];
207}
208
209__attribute__((noreturn))
210static void failWith ( Cursor* c, HChar* str ) {
211   vg_assert(c);
212   vg_assert(c->barf);
213   c->barf(str);
214   /*NOTREACHED*/
215   vg_assert(0);
216}
217
218/* FIXME: document assumptions on endianness for
219   get_UShort/UInt/ULong. */
220static inline UChar get_UChar ( Cursor* c ) {
221   UChar r;
222   /* vg_assert(is_sane_Cursor(c)); */
223   if (c->region_next + sizeof(UChar) > c->region_szB) {
224      c->barf(c->barfstr);
225      /*NOTREACHED*/
226      vg_assert(0);
227   }
228   r = * (UChar*) &c->region_start_img[ c->region_next ];
229   c->region_next += sizeof(UChar);
230   return r;
231}
232static UShort get_UShort ( Cursor* c ) {
233   UShort r;
234   vg_assert(is_sane_Cursor(c));
235   if (c->region_next + sizeof(UShort) > c->region_szB) {
236      c->barf(c->barfstr);
237      /*NOTREACHED*/
238      vg_assert(0);
239   }
240   r = * (UShort*) &c->region_start_img[ c->region_next ];
241   c->region_next += sizeof(UShort);
242   return r;
243}
244static UInt get_UInt ( Cursor* c ) {
245   UInt r;
246   vg_assert(is_sane_Cursor(c));
247   if (c->region_next + sizeof(UInt) > c->region_szB) {
248      c->barf(c->barfstr);
249      /*NOTREACHED*/
250      vg_assert(0);
251   }
252   r = * (UInt*) &c->region_start_img[ c->region_next ];
253   c->region_next += sizeof(UInt);
254   return r;
255}
256static ULong get_ULong ( Cursor* c ) {
257   ULong r;
258   vg_assert(is_sane_Cursor(c));
259   if (c->region_next + sizeof(ULong) > c->region_szB) {
260      c->barf(c->barfstr);
261      /*NOTREACHED*/
262      vg_assert(0);
263   }
264   r = * (ULong*) &c->region_start_img[ c->region_next ];
265   c->region_next += sizeof(ULong);
266   return r;
267}
268static inline ULong get_ULEB128 ( Cursor* c ) {
269   ULong result;
270   Int   shift;
271   UChar byte;
272   /* unroll first iteration */
273   byte = get_UChar( c );
274   result = (ULong)(byte & 0x7f);
275   if (LIKELY(!(byte & 0x80))) return result;
276   shift = 7;
277   /* end unroll first iteration */
278   do {
279      byte = get_UChar( c );
280      result |= ((ULong)(byte & 0x7f)) << shift;
281      shift += 7;
282   } while (byte & 0x80);
283   return result;
284}
285static Long get_SLEB128 ( Cursor* c ) {
286   ULong  result = 0;
287   Int    shift = 0;
288   UChar  byte;
289   do {
290      byte = get_UChar(c);
291      result |= ((ULong)(byte & 0x7f)) << shift;
292      shift += 7;
293   } while (byte & 0x80);
294   if (shift < 64 && (byte & 0x40))
295      result |= -(1ULL << shift);
296   return result;
297}
298
299/* Assume 'c' points to the start of a string.  Return the absolute
300   address of whatever it points at, and advance it past the
301   terminating zero.  This makes it safe for the caller to then copy
302   the string with ML_(addStr), since (w.r.t. image overruns) the
303   process of advancing past the terminating zero will already have
304   "vetted" the string. */
305static UChar* get_AsciiZ ( Cursor* c ) {
306   UChar  uc;
307   UChar* res = get_address_of_Cursor(c);
308   do { uc = get_UChar(c); } while (uc != 0);
309   return res;
310}
311
312static ULong peek_ULEB128 ( Cursor* c ) {
313   Word here = c->region_next;
314   ULong r = get_ULEB128( c );
315   c->region_next = here;
316   return r;
317}
318static UChar peek_UChar ( Cursor* c ) {
319   Word here = c->region_next;
320   UChar r = get_UChar( c );
321   c->region_next = here;
322   return r;
323}
324
325static ULong get_Dwarfish_UWord ( Cursor* c, Bool is_dw64 ) {
326   return is_dw64 ? get_ULong(c) : (ULong) get_UInt(c);
327}
328
329static UWord get_UWord ( Cursor* c ) {
330   vg_assert(sizeof(UWord) == sizeof(void*));
331   if (sizeof(UWord) == 4) return get_UInt(c);
332   if (sizeof(UWord) == 8) return get_ULong(c);
333   vg_assert(0);
334}
335
336
337/* Read a DWARF3 'Initial Length' field */
338static ULong get_Initial_Length ( /*OUT*/Bool* is64,
339                                  Cursor* c,
340                                  HChar* barfMsg )
341{
342   ULong w64;
343   UInt  w32;
344   *is64 = False;
345   w32 = get_UInt( c );
346   if (w32 >= 0xFFFFFFF0 && w32 < 0xFFFFFFFF) {
347      c->barf( barfMsg );
348   }
349   else if (w32 == 0xFFFFFFFF) {
350      *is64 = True;
351      w64   = get_ULong( c );
352   } else {
353      *is64 = False;
354      w64 = (ULong)w32;
355   }
356   return w64;
357}
358
359
360/*------------------------------------------------------------*/
361/*---                                                      ---*/
362/*--- "CUConst" structure                                  ---*/
363/*---                                                      ---*/
364/*------------------------------------------------------------*/
365
366#define N_ABBV_CACHE 32
367
368/* Holds information that is constant through the parsing of a
369   Compilation Unit.  This is basically plumbed through to
370   everywhere. */
371typedef
372   struct {
373      /* Call here if anything goes wrong */
374      void (*barf)( HChar* ) __attribute__((noreturn));
375      /* Is this 64-bit DWARF ? */
376      Bool   is_dw64;
377      /* Which DWARF version ?  (2 or 3) */
378      UShort version;
379      /* Length of this Compilation Unit, excluding its Header */
380      ULong  unit_length;
381      /* Offset of start of this unit in .debug_info */
382      UWord  cu_start_offset;
383      /* SVMA for this CU.  In the D3 spec, is known as the "base
384         address of the compilation unit (last para sec 3.1.1).
385         Needed for (amongst things) interpretation of location-list
386         values. */
387      Addr   cu_svma;
388      Bool   cu_svma_known;
389      /* The debug_abbreviations table to be used for this Unit */
390      UChar* debug_abbv;
391      /* Upper bound on size thereof (an overestimate, in general) */
392      UWord  debug_abbv_maxszB;
393      /* Where is .debug_str ? */
394      UChar* debug_str_img;
395      UWord  debug_str_sz;
396      /* Where is .debug_ranges ? */
397      UChar* debug_ranges_img;
398      UWord  debug_ranges_sz;
399      /* Where is .debug_loc ? */
400      UChar* debug_loc_img;
401      UWord  debug_loc_sz;
402      /* Where is .debug_line? */
403      UChar* debug_line_img;
404      UWord  debug_line_sz;
405      /* --- Needed so we can add stuff to the string table. --- */
406      struct _DebugInfo* di;
407      /* --- a cache for set_abbv_Cursor --- */
408      /* abbv_code == (ULong)-1 for an unused entry. */
409      struct { ULong abbv_code; UWord posn; } saC_cache[N_ABBV_CACHE];
410      UWord saC_cache_queries;
411      UWord saC_cache_misses;
412   }
413   CUConst;
414
415
416/*------------------------------------------------------------*/
417/*---                                                      ---*/
418/*--- Helper functions for Guarded Expressions             ---*/
419/*---                                                      ---*/
420/*------------------------------------------------------------*/
421
422/* Parse the location list starting at img-offset 'debug_loc_offset'
423   in .debug_loc.  Results are biased with 'svma_of_referencing_CU'
424   and so I believe are correct SVMAs for the object as a whole.  This
425   function allocates the UChar*, and the caller must deallocate it.
426   The resulting block is in so-called Guarded-Expression format.
427
428   Guarded-Expression format is similar but not identical to the DWARF3
429   location-list format.  The format of each returned block is:
430
431      UChar biasMe;
432      UChar isEnd;
433      followed by zero or more of
434
435      (Addr aMin;  Addr aMax;  UShort nbytes;  ..bytes..;  UChar isEnd)
436
437   '..bytes..' is an standard DWARF3 location expression which is
438   valid when aMin <= pc <= aMax (possibly after suitable biasing).
439
440   The number of bytes in '..bytes..' is nbytes.
441
442   The end of the sequence is marked by an isEnd == 1 value.  All
443   previous isEnd values must be zero.
444
445   biasMe is 1 if the aMin/aMax fields need this DebugInfo's
446   text_bias added before use, and 0 if the GX is this is not
447   necessary (is ready to go).
448
449   Hence the block can be quickly parsed and is self-describing.  Note
450   that aMax is 1 less than the corresponding value in a DWARF3
451   location list.  Zero length ranges, with aMax == aMin-1, are not
452   allowed.
453*/
454void ML_(pp_GX) ( GExpr* gx ) {
455   Addr   aMin, aMax;
456   UChar  uc;
457   UShort nbytes;
458   UChar* p = &gx->payload[0];
459   uc = *p++;
460   VG_(printf)("GX(%s){", uc == 0 ? "final" : "Breqd" );
461   vg_assert(uc == 0 || uc == 1);
462   while (True) {
463      uc = *p++;
464      if (uc == 1)
465         break; /*isEnd*/
466      vg_assert(uc == 0);
467      aMin   = * (Addr*)p;  p += sizeof(Addr);
468      aMax   = * (Addr*)p;  p += sizeof(Addr);
469      nbytes = * (UShort*)p; p += sizeof(UShort);
470      VG_(printf)("[%p,%p]=", aMin, aMax);
471      while (nbytes > 0) {
472         VG_(printf)("%02x", (UInt)*p++);
473         nbytes--;
474      }
475      if (*p == 0)
476         VG_(printf)(",");
477   }
478   VG_(printf)("}");
479}
480
481/* "Comment_Regarding_DWARF3_Text_Biasing" (is referred to elsewhere)
482    -----------------------------------------------------------------
483    apply_kludgey_text_bias() is our mechanism for biasing text
484    addresses found in DWARF3 .debug_info, .debug_ranges, .debug_loc
485    sections.  This is a nasty and unprincipled hack.
486
487    Biasing the text svmas, so as to obtain text avmas, should be
488    straightforward, right?  We just add on di->text_bias, as
489    carefully computed by readelf.c.
490
491    That works OK most of the time.  But in the following case it fails:
492    1. The object is made in the usual way (gcc -g, etc)
493    2. The DWARF3 stuff removed from it and parked in a .debuginfo object
494    3. The remaining (base) object is then prelinked.
495
496    Prelinking changes the text svmas throughout an object by some
497    constant amount, including the DWARF3 stuff.  So if the DWARF3
498    stuff remains attached to the original object, then there is no
499    problem.  However, if the DWARF3 stuff is detached, and the
500    remaining object is prelinked and the debuginfo object isn't, then
501    we have a problem: the text bias computed for the main object
502    isn't correct for the debuginfo object.
503
504    So the following kludged is used to bias text svmas.
505
506    1. First, try with the text bias computed for the main object.  If
507       that gives an avma inside the area in which the text segment is
508       known to have been mapped, then all well and good.
509
510    2. If not, try using the avma of the text mapped area as a bias.
511       Again, if that works out, fine.  This is the heart of the
512       kludge.  It implicitly treats the svma-s to be biased as if
513       they had been prelinked to zero.
514
515    3. If even that doesn't work, just return the avma unchanged.
516
517    For each object/object-pair, we count the number of times each
518    case occurs.  We flag an error (which the user gets to see) if (3)
519    ever occurs, or if a mixture of (1) and (2) occurs.  That should
520    at least catch the most obvious snafus.
521
522    Caveats: the main remaining worry is whether this problem somehow
523    also affects the data-biasing done for case DW_OP_addr in
524    ML_(evaluate_Dwarf3_Expr) in d3basics.c.  This is currently
525    unknown.
526
527    Possible sources of info: canonical description seems to be:
528
529       http://people.redhat.com/jakub/prelink.pdf
530
531    See para at line 337 starting "DWARF 2 debugging information ..."
532
533    This thread looks like the gdb people hitting the same issue:
534
535       http://sourceware.org/ml/gdb-patches/2007-01/msg00278.html
536*/
537typedef
538   struct {
539      /* FIXED */
540      Addr  rx_map_avma;
541      SizeT rx_map_size;
542      OffT  text_bias;
543      /* VARIABLE -- count stats */
544      UWord n_straightforward_biasings;
545      UWord n_kludgey_biasings;
546      UWord n_failed_biasings;
547   }
548   KludgeyTextBiaser;
549
550static Addr apply_kludgey_text_bias ( KludgeyTextBiaser* ktb,
551                                      Addr allegedly_text_svma ) {
552   Addr res;
553   res = allegedly_text_svma + ktb->text_bias;
554   if (res >= ktb->rx_map_avma
555       && res < ktb->rx_map_avma + ktb->rx_map_size) {
556      ktb->n_straightforward_biasings++;
557      return res;
558   }
559   res = allegedly_text_svma + ktb->rx_map_avma;
560   if (res >= ktb->rx_map_avma
561       && res < ktb->rx_map_avma + ktb->rx_map_size) {
562      ktb->n_kludgey_biasings++;
563      return res;
564   }
565   ktb->n_failed_biasings++;
566   return allegedly_text_svma; /* this svma is a luzer */
567}
568
569
570/* Apply a text bias to a GX.  Kludgily :-( */
571static void bias_GX ( /*MOD*/GExpr* gx, KludgeyTextBiaser* ktb )
572{
573   UShort nbytes;
574   Addr*  pA;
575   UChar* p = &gx->payload[0];
576   UChar  uc;
577   uc = *p++; /*biasMe*/
578   if (uc == 0)
579      return;
580   vg_assert(uc == 1);
581   p[-1] = 0; /* mark it as done */
582   while (True) {
583      uc = *p++;
584      if (uc == 1)
585         break; /*isEnd*/
586      vg_assert(uc == 0);
587      /* t-bias aMin */
588      pA = (Addr*)p;
589      *pA = apply_kludgey_text_bias( ktb, *pA );
590      p += sizeof(Addr);
591      /* t-bias aMax */
592      pA = (Addr*)p;
593      *pA = apply_kludgey_text_bias( ktb, *pA );
594      p += sizeof(Addr);
595      /* nbytes, and actual expression */
596      nbytes = * (UShort*)p; p += sizeof(UShort);
597      p += nbytes;
598   }
599}
600
601__attribute__((noinline))
602static GExpr* make_singleton_GX ( UChar* block, UWord nbytes )
603{
604   SizeT  bytesReqd;
605   GExpr* gx;
606   UChar *p, *pstart;
607
608   vg_assert(sizeof(UWord) == sizeof(Addr));
609   vg_assert(nbytes <= 0xFFFF); /* else we overflow the nbytes field */
610   bytesReqd
611      =   sizeof(UChar)  /*biasMe*/    + sizeof(UChar) /*!isEnd*/
612        + sizeof(UWord)  /*aMin*/      + sizeof(UWord) /*aMax*/
613        + sizeof(UShort) /*nbytes*/    + nbytes
614        + sizeof(UChar); /*isEnd*/
615
616   gx = ML_(dinfo_zalloc)( sizeof(GExpr) + bytesReqd );
617   vg_assert(gx);
618
619   p = pstart = &gx->payload[0];
620
621   * ((UChar*)p)  = 0;          /*biasMe*/ p += sizeof(UChar);
622   * ((UChar*)p)  = 0;          /*!isEnd*/ p += sizeof(UChar);
623   * ((Addr*)p)   = 0;          /*aMin*/   p += sizeof(Addr);
624   * ((Addr*)p)   = ~((Addr)0); /*aMax */  p += sizeof(Addr);
625   * ((UShort*)p) = (UShort)nbytes; /*nbytes*/ p += sizeof(UShort);
626   VG_(memcpy)(p, block, nbytes); p += nbytes;
627   * ((UChar*)p)  = 1;          /*isEnd*/  p += sizeof(UChar);
628
629   vg_assert( (SizeT)(p - pstart) == bytesReqd);
630   vg_assert( &gx->payload[bytesReqd]
631              == ((UChar*)gx) + sizeof(GExpr) + bytesReqd );
632
633   gx->next = NULL;
634   return gx;
635}
636
637__attribute__((noinline))
638static GExpr* make_general_GX ( CUConst* cc,
639                                Bool     td3,
640                                UWord    debug_loc_offset,
641                                Addr     svma_of_referencing_CU )
642{
643   Addr      base;
644   Cursor    loc;
645   XArray*   xa; /* XArray of UChar */
646   GExpr*    gx;
647   Word      nbytes;
648
649   vg_assert(sizeof(UWord) == sizeof(Addr));
650   if (cc->debug_loc_sz == 0)
651      cc->barf("make_general_GX: .debug_loc is empty/missing");
652
653   init_Cursor( &loc, cc->debug_loc_img,
654                cc->debug_loc_sz, 0, cc->barf,
655                "Overrun whilst reading .debug_loc section(2)" );
656   set_position_of_Cursor( &loc, debug_loc_offset );
657
658   TRACE_D3("make_general_GX (.debug_loc_offset = %lu, img = %p) {\n",
659            debug_loc_offset, get_address_of_Cursor( &loc ) );
660
661   /* Who frees this xa?  It is freed before this fn exits. */
662   xa = VG_(newXA)( ML_(dinfo_zalloc), ML_(dinfo_free),
663                    sizeof(UChar) );
664
665   { UChar c = 1; /*biasMe*/ VG_(addBytesToXA)( xa, &c, sizeof(c) ); }
666
667   base = 0;
668   while (True) {
669      Bool  acquire;
670      UWord len;
671      /* Read a (host-)word pair.  This is something of a hack since
672         the word size to read is really dictated by the ELF file;
673         however, we assume we're reading a file with the same
674         word-sizeness as the host.  Reasonably enough. */
675      UWord w1 = get_UWord( &loc );
676      UWord w2 = get_UWord( &loc );
677
678      TRACE_D3("   %08lx %08lx\n", w1, w2);
679      if (w1 == 0 && w2 == 0)
680         break; /* end of list */
681
682      if (w1 == -1UL) {
683         /* new value for 'base' */
684         base = w2;
685         continue;
686      }
687
688      /* else a location expression follows */
689      /* else enumerate [w1+base, w2+base) */
690      /* w2 is 1 past end of range, as per D3 defn for "DW_AT_high_pc"
691         (sec 2.17.2) */
692      if (w1 > w2) {
693         TRACE_D3("negative range is for .debug_loc expr at "
694                  "file offset %lu\n",
695                  debug_loc_offset);
696         cc->barf( "negative range in .debug_loc section" );
697      }
698
699      /* ignore zero length ranges */
700      acquire = w1 < w2;
701      len     = (UWord)get_UShort( &loc );
702
703      if (acquire) {
704         UWord  w;
705         UShort s;
706         UChar  c;
707         c = 0; /* !isEnd*/
708         VG_(addBytesToXA)( xa, &c, sizeof(c) );
709         w = w1    + base + svma_of_referencing_CU;
710         VG_(addBytesToXA)( xa, &w, sizeof(w) );
711         w = w2 -1 + base + svma_of_referencing_CU;
712         VG_(addBytesToXA)( xa, &w, sizeof(w) );
713         s = (UShort)len;
714         VG_(addBytesToXA)( xa, &s, sizeof(s) );
715      }
716
717      while (len > 0) {
718         UChar byte = get_UChar( &loc );
719         TRACE_D3("%02x", (UInt)byte);
720         if (acquire)
721            VG_(addBytesToXA)( xa, &byte, 1 );
722         len--;
723      }
724      TRACE_D3("\n");
725   }
726
727   { UChar c = 1; /*isEnd*/ VG_(addBytesToXA)( xa, &c, sizeof(c) ); }
728
729   nbytes = VG_(sizeXA)( xa );
730   vg_assert(nbytes >= 1);
731
732   gx = ML_(dinfo_zalloc)( sizeof(GExpr) + nbytes );
733   vg_assert(gx);
734   VG_(memcpy)( &gx->payload[0], (UChar*)VG_(indexXA)(xa,0), nbytes );
735   vg_assert( &gx->payload[nbytes]
736              == ((UChar*)gx) + sizeof(GExpr) + nbytes );
737
738   VG_(deleteXA)( xa );
739
740   gx->next = NULL;
741
742   TRACE_D3("}\n");
743
744   return gx;
745}
746
747
748/*------------------------------------------------------------*/
749/*---                                                      ---*/
750/*--- Helper functions for range lists and CU headers      ---*/
751/*---                                                      ---*/
752/*------------------------------------------------------------*/
753
754/* Denotes an address range.  Both aMin and aMax are included in the
755   range; hence a complete range is (0, ~0) and an empty range is any
756   (X, X-1) for X > 0.*/
757typedef
758   struct { Addr aMin; Addr aMax; }
759   AddrRange;
760
761
762__attribute__((noinline))
763static XArray* /* of AddrRange */ empty_range_list ( void )
764{
765   XArray* xa; /* XArray of AddrRange */
766   /* Who frees this xa?  varstack_preen() does. */
767   xa = VG_(newXA)( ML_(dinfo_zalloc), ML_(dinfo_free),
768                    sizeof(AddrRange) );
769   return xa;
770}
771
772
773static XArray* unitary_range_list ( Addr aMin, Addr aMax )
774{
775   XArray*   xa;
776   AddrRange pair;
777   vg_assert(aMin <= aMax);
778   /* Who frees this xa?  varstack_preen() does. */
779   xa = VG_(newXA)( ML_(dinfo_zalloc), ML_(dinfo_free),
780                    sizeof(AddrRange) );
781   pair.aMin = aMin;
782   pair.aMax = aMax;
783   VG_(addToXA)( xa, &pair );
784   return xa;
785}
786
787
788/* Enumerate the address ranges starting at img-offset
789   'debug_ranges_offset' in .debug_ranges.  Results are biased with
790   'svma_of_referencing_CU' and so I believe are correct SVMAs for the
791   object as a whole.  This function allocates the XArray, and the
792   caller must deallocate it. */
793__attribute__((noinline))
794static XArray* /* of AddrRange */
795       get_range_list ( CUConst* cc,
796                        Bool     td3,
797                        UWord    debug_ranges_offset,
798                        Addr     svma_of_referencing_CU )
799{
800   Addr      base;
801   Cursor    ranges;
802   XArray*   xa; /* XArray of AddrRange */
803   AddrRange pair;
804
805   if (cc->debug_ranges_sz == 0)
806      cc->barf("get_range_list: .debug_ranges is empty/missing");
807
808   init_Cursor( &ranges, cc->debug_ranges_img,
809                cc->debug_ranges_sz, 0, cc->barf,
810                "Overrun whilst reading .debug_ranges section(2)" );
811   set_position_of_Cursor( &ranges, debug_ranges_offset );
812
813   /* Who frees this xa?  varstack_preen() does. */
814   xa = VG_(newXA)( ML_(dinfo_zalloc), ML_(dinfo_free),
815                    sizeof(AddrRange) );
816   base = 0;
817   while (True) {
818      /* Read a (host-)word pair.  This is something of a hack since
819         the word size to read is really dictated by the ELF file;
820         however, we assume we're reading a file with the same
821         word-sizeness as the host.  Reasonably enough. */
822      UWord w1 = get_UWord( &ranges );
823      UWord w2 = get_UWord( &ranges );
824
825      if (w1 == 0 && w2 == 0)
826         break; /* end of list. */
827
828      if (w1 == -1UL) {
829         /* new value for 'base' */
830         base = w2;
831         continue;
832      }
833
834      /* else enumerate [w1+base, w2+base) */
835      /* w2 is 1 past end of range, as per D3 defn for "DW_AT_high_pc"
836         (sec 2.17.2) */
837      if (w1 > w2)
838         cc->barf( "negative range in .debug_ranges section" );
839      if (w1 < w2) {
840         pair.aMin = w1     + base + svma_of_referencing_CU;
841         pair.aMax = w2 - 1 + base + svma_of_referencing_CU;
842         vg_assert(pair.aMin <= pair.aMax);
843         VG_(addToXA)( xa, &pair );
844      }
845   }
846   return xa;
847}
848
849
850/* Parse the Compilation Unit header indicated at 'c' and
851   initialise 'cc' accordingly. */
852static __attribute__((noinline))
853void parse_CU_Header ( /*OUT*/CUConst* cc,
854                       Bool td3,
855                       Cursor* c,
856                       UChar* debug_abbv_img, UWord debug_abbv_sz )
857{
858   UChar  address_size;
859   UWord  debug_abbrev_offset;
860   Int    i;
861
862   VG_(memset)(cc, 0, sizeof(*cc));
863   vg_assert(c && c->barf);
864   cc->barf = c->barf;
865
866   /* initial_length field */
867   cc->unit_length
868      = get_Initial_Length( &cc->is_dw64, c,
869           "parse_CU_Header: invalid initial-length field" );
870
871   TRACE_D3("   Length:        %lld\n", cc->unit_length );
872
873   /* version */
874   cc->version = get_UShort( c );
875   if (cc->version != 2 && cc->version != 3)
876      cc->barf( "parse_CU_Header: is neither DWARF2 nor DWARF3" );
877   TRACE_D3("   Version:       %d\n", (Int)cc->version );
878
879   /* debug_abbrev_offset */
880   debug_abbrev_offset = get_Dwarfish_UWord( c, cc->is_dw64 );
881   if (debug_abbrev_offset >= debug_abbv_sz)
882      cc->barf( "parse_CU_Header: invalid debug_abbrev_offset" );
883   TRACE_D3("   Abbrev Offset: %ld\n", debug_abbrev_offset );
884
885   /* address size.  If this isn't equal to the host word size, just
886      give up.  This makes it safe to assume elsewhere that
887      DW_FORM_addr can be treated as a host word. */
888   address_size = get_UChar( c );
889   if (address_size != sizeof(void*))
890      cc->barf( "parse_CU_Header: invalid address_size" );
891   TRACE_D3("   Pointer Size:  %d\n", (Int)address_size );
892
893   /* Set up so that cc->debug_abbv points to the relevant table for
894      this CU.  Set the szB so that at least we can't read off the end
895      of the debug_abbrev section -- potentially (and quite likely)
896      too big, if this isn't the last table in the section, but at
897      least it's safe. */
898   cc->debug_abbv        = debug_abbv_img + debug_abbrev_offset;
899   cc->debug_abbv_maxszB = debug_abbv_sz  - debug_abbrev_offset;
900   /* and empty out the set_abbv_Cursor cache */
901   if (0) VG_(printf)("XXXXXX initialise set_abbv_Cursor cache\n");
902   for (i = 0; i < N_ABBV_CACHE; i++) {
903      cc->saC_cache[i].abbv_code = (ULong)-1; /* unused */
904      cc->saC_cache[i].posn = 0;
905   }
906   cc->saC_cache_queries = 0;
907   cc->saC_cache_misses = 0;
908}
909
910
911/* Set up 'c' so it is ready to parse the abbv table entry code
912   'abbv_code' for this compilation unit.  */
913static __attribute__((noinline))
914void set_abbv_Cursor ( /*OUT*/Cursor* c, Bool td3,
915                       CUConst* cc, ULong abbv_code )
916{
917   Int   i;
918   ULong acode;
919
920   if (abbv_code == 0)
921      cc->barf("set_abbv_Cursor: abbv_code == 0" );
922
923   /* (ULong)-1 is used to represent an empty cache slot.  So we can't
924      allow it.  In any case no valid DWARF3 should make a reference
925      to a negative abbreviation code.  [at least, they always seem to
926      be numbered upwards from zero as far as I have seen] */
927   vg_assert(abbv_code != (ULong)-1);
928
929   /* First search the cache. */
930   if (0) VG_(printf)("XXXXXX search set_abbv_Cursor cache\n");
931   cc->saC_cache_queries++;
932   for (i = 0; i < N_ABBV_CACHE; i++) {
933      /* No need to test the cached abbv_codes for -1 (empty), since
934         we just asserted that abbv_code is not -1. */
935     if (cc->saC_cache[i].abbv_code == abbv_code) {
936        /* Found it.  Cool.  Set up the parser using the cached
937           position, and move this cache entry 1 step closer to the
938           front. */
939        if (0) VG_(printf)("XXXXXX found in set_abbv_Cursor cache\n");
940        init_Cursor( c, cc->debug_abbv,
941                     cc->debug_abbv_maxszB, cc->saC_cache[i].posn,
942                     cc->barf,
943                     "Overrun whilst parsing .debug_abbrev section(1)" );
944        if (i > 0) {
945           ULong t_abbv_code = cc->saC_cache[i].abbv_code;
946           UWord t_posn = cc->saC_cache[i].posn;
947           while (i > 0) {
948              cc->saC_cache[i] = cc->saC_cache[i-1];
949              cc->saC_cache[0].abbv_code = t_abbv_code;
950              cc->saC_cache[0].posn = t_posn;
951              i--;
952           }
953        }
954        return;
955     }
956   }
957
958   /* No.  It's not in the cache.  We have to search through
959      .debug_abbrev, of course taking care to update the cache
960      when done. */
961
962   cc->saC_cache_misses++;
963   init_Cursor( c, cc->debug_abbv, cc->debug_abbv_maxszB, 0, cc->barf,
964               "Overrun whilst parsing .debug_abbrev section(2)" );
965
966   /* Now iterate though the table until we find the requested
967      entry. */
968   while (True) {
969      ULong atag;
970      UInt  has_children;
971      acode = get_ULEB128( c );
972      if (acode == 0) break; /* end of the table */
973      if (acode == abbv_code) break; /* found it */
974      atag         = get_ULEB128( c );
975      has_children = get_UChar( c );
976      //TRACE_D3("   %llu      %s    [%s]\n",
977      //         acode, pp_DW_TAG(atag), pp_DW_children(has_children));
978      while (True) {
979         ULong at_name = get_ULEB128( c );
980         ULong at_form = get_ULEB128( c );
981         if (at_name == 0 && at_form == 0) break;
982         //TRACE_D3("    %18s %s\n",
983         //         pp_DW_AT(at_name), pp_DW_FORM(at_form));
984      }
985   }
986
987   if (acode == 0) {
988      /* Not found.  This is fatal. */
989      cc->barf("set_abbv_Cursor: abbv_code not found");
990   }
991
992   /* Otherwise, 'c' is now set correctly to parse the relevant entry,
993      starting from the abbreviation entry's tag.  So just cache
994      the result, and return. */
995   for (i = N_ABBV_CACHE-1; i > N_ABBV_CACHE/2; i--) {
996      cc->saC_cache[i] = cc->saC_cache[i-1];
997   }
998   if (0) VG_(printf)("XXXXXX update set_abbv_Cursor cache\n");
999   cc->saC_cache[N_ABBV_CACHE/2].abbv_code = abbv_code;
1000   cc->saC_cache[N_ABBV_CACHE/2].posn = get_position_of_Cursor(c);
1001}
1002
1003
1004/* From 'c', get the Form data into the lowest 1/2/4/8 bytes of *cts.
1005
1006   If *cts itself contains the entire result, then *ctsSzB is set to
1007   1,2,4 or 8 accordingly and *ctsMemSzB is set to zero.
1008
1009   Alternatively, the result can be a block of data (in the
1010   transiently mapped-in object, so-called "image" space).  If so then
1011   the lowest sizeof(void*)/8 bytes of *cts hold a pointer to said
1012   image, *ctsSzB is zero, and *ctsMemSzB is the size of the block.
1013
1014   Unfortunately this means it is impossible to represent a zero-size
1015   image block since that would have *ctsSzB == 0 and *ctsMemSzB == 0
1016   and so is ambiguous (which case it is?)
1017
1018   Invariant on successful return:
1019      (*ctsSzB > 0 && *ctsMemSzB == 0)
1020      || (*ctsSzB == 0 && *ctsMemSzB > 0)
1021*/
1022static
1023void get_Form_contents ( /*OUT*/ULong* cts,
1024                         /*OUT*/Int*   ctsSzB,
1025                         /*OUT*/UWord* ctsMemSzB,
1026                         CUConst* cc, Cursor* c,
1027                         Bool td3, DW_FORM form )
1028{
1029   *cts       = 0;
1030   *ctsSzB    = 0;
1031   *ctsMemSzB = 0;
1032   switch (form) {
1033      case DW_FORM_data1:
1034         *cts = (ULong)(UChar)get_UChar(c);
1035         *ctsSzB = 1;
1036         TRACE_D3("%u", (UInt)*cts);
1037         break;
1038      case DW_FORM_data2:
1039         *cts = (ULong)(UShort)get_UShort(c);
1040         *ctsSzB = 2;
1041         TRACE_D3("%u", (UInt)*cts);
1042         break;
1043      case DW_FORM_data4:
1044         *cts = (ULong)(UInt)get_UInt(c);
1045         *ctsSzB = 4;
1046         TRACE_D3("%u", (UInt)*cts);
1047         break;
1048      case DW_FORM_data8:
1049         *cts = get_ULong(c);
1050         *ctsSzB = 8;
1051         TRACE_D3("%llu", *cts);
1052         break;
1053      case DW_FORM_sdata:
1054         *cts = (ULong)(Long)get_SLEB128(c);
1055         *ctsSzB = 8;
1056         TRACE_D3("%lld", (Long)*cts);
1057         break;
1058      case DW_FORM_addr:
1059         /* note, this is a hack.  DW_FORM_addr is defined as getting
1060            a word the size of the target machine as defined by the
1061            address_size field in the CU Header.  However,
1062            parse_CU_Header() rejects all inputs except those for
1063            which address_size == sizeof(Word), hence we can just
1064            treat it as a (host) Word.  */
1065         *cts = (ULong)(UWord)get_UWord(c);
1066         *ctsSzB = sizeof(UWord);
1067         TRACE_D3("0x%lx", (UWord)*cts);
1068         break;
1069      case DW_FORM_strp: {
1070         /* this is an offset into .debug_str */
1071         UChar* str;
1072         UWord uw = (UWord)get_Dwarfish_UWord( c, cc->is_dw64 );
1073         if (cc->debug_str_img == NULL || uw >= cc->debug_str_sz)
1074            cc->barf("read_and_show_Form: DW_FORM_strp "
1075                     "points outside .debug_str");
1076         /* FIXME: check the entire string lies inside debug_str,
1077            not just the first byte of it. */
1078         str = (UChar*)cc->debug_str_img + uw;
1079         TRACE_D3("(indirect string, offset: 0x%lx): %s", uw, str);
1080         *cts = (ULong)(UWord)str;
1081         *ctsMemSzB = 1 + (ULong)VG_(strlen)(str);
1082         break;
1083      }
1084      case DW_FORM_string: {
1085         UChar* str = get_AsciiZ(c);
1086         TRACE_D3("%s", str);
1087         *cts = (ULong)(UWord)str;
1088         /* strlen is safe because get_AsciiZ already 'vetted' the
1089            entire string */
1090         *ctsMemSzB = 1 + (ULong)VG_(strlen)(str);
1091         break;
1092      }
1093      case DW_FORM_ref4: {
1094         UInt  u32 = get_UInt(c);
1095         UWord res = cc->cu_start_offset + (UWord)u32;
1096         *cts = (ULong)res;
1097         *ctsSzB = sizeof(UWord);
1098         TRACE_D3("<%lx>", res);
1099         break;
1100      }
1101      case DW_FORM_flag: {
1102         UChar u8 = get_UChar(c);
1103         TRACE_D3("%u", (UInt)u8);
1104         *cts = (ULong)u8;
1105         *ctsSzB = 1;
1106         break;
1107      }
1108      case DW_FORM_block1: {
1109         ULong  u64b;
1110         ULong  u64 = (ULong)get_UChar(c);
1111         UChar* block = get_address_of_Cursor(c);
1112         TRACE_D3("%llu byte block: ", u64);
1113         for (u64b = u64; u64b > 0; u64b--) {
1114            UChar u8 = get_UChar(c);
1115            TRACE_D3("%x ", (UInt)u8);
1116         }
1117         *cts = (ULong)(UWord)block;
1118         *ctsMemSzB = (UWord)u64;
1119         break;
1120      }
1121      default:
1122         VG_(printf)("get_Form_contents: unhandled %lld (%s)\n",
1123                     form, ML_(pp_DW_FORM)(form));
1124         c->barf("get_Form_contents: unhandled DW_FORM");
1125   }
1126}
1127
1128
1129/*------------------------------------------------------------*/
1130/*---                                                      ---*/
1131/*--- Parsing of variable-related DIEs                     ---*/
1132/*---                                                      ---*/
1133/*------------------------------------------------------------*/
1134
1135typedef
1136   struct _TempVar {
1137      struct _TempVar* next;
1138      UChar*  name; /* in DebugInfo's .strchunks */
1139      /* Represent ranges economically.  nRanges is the number of
1140         ranges.  Cases:
1141         0: .rngOneMin .rngOneMax .manyRanges are all zero
1142         1: .rngOneMin .rngOneMax hold the range; .rngMany is NULL
1143         2: .rngOneMin .rngOneMax are zero; .rngMany holds the ranges.
1144         This is merely an optimisation to avoid having to allocate
1145         and free the XArray in the common (98%) of cases where there
1146         is zero or one address ranges. */
1147      UWord   nRanges;
1148      Addr    rngOneMin;
1149      Addr    rngOneMax;
1150      XArray* rngMany; /* of AddrRange.  UNIQUE PTR in AR_DINFO. */
1151      /* --- */
1152      Int     level;
1153      Type*   typeR;
1154      GExpr*  gexpr; /* for this variable */
1155      GExpr*  fbGX;  /* to find the frame base of the enclosing fn, if
1156                        any */
1157      UChar*  fName; /* declaring file name, or NULL */
1158      Int     fLine; /* declaring file line number, or zero */
1159      /* offset in .debug_info, so that abstract instances can be
1160         found to satisfy references from concrete instances. */
1161      UWord   dioff;
1162      UWord   absOri; /* so the absOri fields refer to dioff fields
1163                         in some other, related TempVar. */
1164   }
1165   TempVar;
1166
1167#define N_D3_VAR_STACK 48
1168
1169typedef
1170   struct {
1171      /* Contains the range stack: a stack of address ranges, one
1172         stack entry for each nested scope.
1173
1174         Some scope entries are created by function definitions
1175         (DW_AT_subprogram), and for those, we also note the GExpr
1176         derived from its DW_AT_frame_base attribute, if any.
1177         Consequently it should be possible to find, for any
1178         variable's DIE, the GExpr for the the containing function's
1179         DW_AT_frame_base by scanning back through the stack to find
1180         the nearest entry associated with a function.  This somewhat
1181         elaborate scheme is provided so as to make it possible to
1182         obtain the correct DW_AT_frame_base expression even in the
1183         presence of nested functions (or to be more precise, in the
1184         presence of nested DW_AT_subprogram DIEs).
1185      */
1186      Int     sp; /* [sp] is innermost active entry; sp==-1 for empty
1187                     stack */
1188      XArray* ranges[N_D3_VAR_STACK]; /* XArray of AddrRange */
1189      Int     level[N_D3_VAR_STACK];  /* D3 DIE levels */
1190      Bool    isFunc[N_D3_VAR_STACK]; /* from DW_AT_subprogram? */
1191      GExpr*  fbGX[N_D3_VAR_STACK];   /* if isFunc, contains the FB
1192                                         expr, else NULL */
1193      /* The file name table.  Is a mapping from integer index to the
1194         (permanent) copy of the string, iow a non-img area. */
1195      XArray* /* of UChar* */ filenameTable;
1196   }
1197   D3VarParser;
1198
1199static void varstack_show ( D3VarParser* parser, HChar* str ) {
1200   Word i, j;
1201   VG_(printf)("  varstack (%s) {\n", str);
1202   for (i = 0; i <= parser->sp; i++) {
1203      XArray* xa = parser->ranges[i];
1204      vg_assert(xa);
1205      VG_(printf)("    [%ld] (level %d)", i, parser->level[i]);
1206      if (parser->isFunc[i]) {
1207         VG_(printf)(" (fbGX=%p)", parser->fbGX[i]);
1208      } else {
1209         vg_assert(parser->fbGX[i] == NULL);
1210      }
1211      VG_(printf)(": ");
1212      if (VG_(sizeXA)( xa ) == 0) {
1213         VG_(printf)("** empty PC range array **");
1214      } else {
1215         for (j = 0; j < VG_(sizeXA)( xa ); j++) {
1216            AddrRange* range = (AddrRange*) VG_(indexXA)( xa, j );
1217            vg_assert(range);
1218            VG_(printf)("[%p,%p] ", range->aMin, range->aMax);
1219         }
1220      }
1221      VG_(printf)("\n");
1222   }
1223   VG_(printf)("  }\n");
1224}
1225
1226/* Remove from the stack, all entries with .level > 'level' */
1227static
1228void varstack_preen ( D3VarParser* parser, Bool td3, Int level )
1229{
1230   Bool changed = False;
1231   vg_assert(parser->sp < N_D3_VAR_STACK);
1232   while (True) {
1233      vg_assert(parser->sp >= -1);
1234      if (parser->sp == -1) break;
1235      if (parser->level[parser->sp] <= level) break;
1236      if (0)
1237         TRACE_D3("BBBBAAAA varstack_pop [newsp=%d]\n", parser->sp-1);
1238      vg_assert(parser->ranges[parser->sp]);
1239      /* Who allocated this xa?  get_range_list() or
1240         unitary_range_list(). */
1241      VG_(deleteXA)( parser->ranges[parser->sp] );
1242      parser->ranges[parser->sp] = NULL;
1243      parser->level[parser->sp]  = 0;
1244      parser->isFunc[parser->sp] = False;
1245      parser->fbGX[parser->sp]   = NULL;
1246      parser->sp--;
1247      changed = True;
1248   }
1249   if (changed && td3)
1250      varstack_show( parser, "after preen" );
1251}
1252
1253static void varstack_push ( CUConst* cc,
1254                            D3VarParser* parser,
1255                            Bool td3,
1256                            XArray* ranges, Int level,
1257                            Bool    isFunc, GExpr* fbGX ) {
1258   if (0)
1259   TRACE_D3("BBBBAAAA varstack_push[newsp=%d]: %d  %p\n",
1260            parser->sp+1, level, ranges);
1261
1262   /* First we need to zap everything >= 'level', as we are about to
1263      replace any previous entry at 'level', so .. */
1264   varstack_preen(parser, /*td3*/False, level-1);
1265
1266   vg_assert(parser->sp >= -1);
1267   vg_assert(parser->sp < N_D3_VAR_STACK);
1268   if (parser->sp == N_D3_VAR_STACK-1)
1269      cc->barf("varstack_push: N_D3_VAR_STACK is too low; "
1270               "increase and recompile");
1271   if (parser->sp >= 0)
1272      vg_assert(parser->level[parser->sp] < level);
1273   parser->sp++;
1274   vg_assert(parser->ranges[parser->sp] == NULL);
1275   vg_assert(parser->level[parser->sp]  == 0);
1276   vg_assert(parser->isFunc[parser->sp] == False);
1277   vg_assert(parser->fbGX[parser->sp]   == NULL);
1278   vg_assert(ranges != NULL);
1279   if (!isFunc) vg_assert(fbGX == NULL);
1280   parser->ranges[parser->sp] = ranges;
1281   parser->level[parser->sp]  = level;
1282   parser->isFunc[parser->sp] = isFunc;
1283   parser->fbGX[parser->sp]   = fbGX;
1284   if (td3)
1285      varstack_show( parser, "after push" );
1286}
1287
1288
1289/* cts, ctsSzB, ctsMemSzB are derived from a DW_AT_location and so
1290   refer either to a location expression or to a location list.
1291   Figure out which, and in both cases bundle the expression or
1292   location list into a so-called GExpr (guarded expression). */
1293__attribute__((noinline))
1294static GExpr* get_GX ( CUConst* cc, Bool td3,
1295                       ULong cts, Int ctsSzB, UWord ctsMemSzB )
1296{
1297   GExpr* gexpr = NULL;
1298   if (ctsMemSzB > 0 && ctsSzB == 0) {
1299      /* represents an in-line location expression, and cts points
1300         right at it */
1301      gexpr = make_singleton_GX( (UChar*)(UWord)cts, ctsMemSzB );
1302   }
1303   else
1304   if (ctsMemSzB == 0 && ctsSzB > 0) {
1305      /* represents location list.  cts is the offset of it in
1306         .debug_loc. */
1307      if (!cc->cu_svma_known)
1308         cc->barf("get_GX: location list, but CU svma is unknown");
1309      gexpr = make_general_GX( cc, td3, (UWord)cts, cc->cu_svma );
1310   }
1311   else {
1312      vg_assert(0); /* else caller is bogus */
1313   }
1314   return gexpr;
1315}
1316
1317
1318static
1319void read_filename_table( /*MOD*/D3VarParser* parser,
1320                          CUConst* cc, UWord debug_line_offset,
1321                          Bool td3 )
1322{
1323   Bool   is_dw64;
1324   Cursor c;
1325   Word   i;
1326   ULong  unit_length;
1327   UShort version;
1328   ULong  header_length;
1329   UChar  minimum_instruction_length;
1330   UChar  default_is_stmt;
1331   Char   line_base;
1332   UChar  line_range;
1333   UChar  opcode_base;
1334   UChar* str;
1335
1336   vg_assert(parser && cc && cc->barf);
1337   if ((!cc->debug_line_img)
1338       || cc->debug_line_sz <= debug_line_offset)
1339      cc->barf("read_filename_table: .debug_line is missing?");
1340
1341   init_Cursor( &c, cc->debug_line_img,
1342                cc->debug_line_sz, debug_line_offset, cc->barf,
1343                "Overrun whilst reading .debug_line section(1)" );
1344
1345   unit_length
1346      = get_Initial_Length( &is_dw64, &c,
1347           "read_filename_table: invalid initial-length field" );
1348   version = get_UShort( &c );
1349   if (version != 2)
1350     cc->barf("read_filename_table: Only DWARF version 2 line info "
1351              "is currently supported.");
1352   header_length = (ULong)get_Dwarfish_UWord( &c, is_dw64 );
1353   minimum_instruction_length = get_UChar( &c );
1354   default_is_stmt            = get_UChar( &c );
1355   line_base                  = (Char)get_UChar( &c );
1356   line_range                 = get_UChar( &c );
1357   opcode_base                = get_UChar( &c );
1358   /* skip over "standard_opcode_lengths" */
1359   for (i = 1; i < (Word)opcode_base; i++)
1360     (void)get_UChar( &c );
1361
1362   /* skip over the directory names table */
1363   while (peek_UChar(&c) != 0) {
1364     (void)get_AsciiZ(&c);
1365   }
1366   (void)get_UChar(&c); /* skip terminating zero */
1367
1368   /* Read and record the file names table */
1369   vg_assert(parser->filenameTable);
1370   vg_assert( VG_(sizeXA)( parser->filenameTable ) == 0 );
1371   /* Add a dummy index-zero entry.  DWARF3 numbers its files
1372      from 1, for some reason. */
1373   str = ML_(addStr)( cc->di, "<unknown_file>", -1 );
1374   VG_(addToXA)( parser->filenameTable, &str );
1375   while (peek_UChar(&c) != 0) {
1376      str = get_AsciiZ(&c);
1377      TRACE_D3("  read_filename_table: %ld %s\n",
1378               VG_(sizeXA)(parser->filenameTable), str);
1379      str = ML_(addStr)( cc->di, str, -1 );
1380      VG_(addToXA)( parser->filenameTable, &str );
1381      (void)get_ULEB128( &c ); /* skip directory index # */
1382      (void)get_ULEB128( &c ); /* skip last mod time */
1383      (void)get_ULEB128( &c ); /* file size */
1384   }
1385   /* We're done!  The rest of it is not interesting. */
1386}
1387
1388
1389__attribute__((noinline))
1390static void parse_var_DIE ( /*OUT*/TempVar** tempvars,
1391                            /*OUT*/GExpr** gexprs,
1392                            /*MOD*/D3VarParser* parser,
1393                            DW_TAG dtag,
1394                            UWord posn,
1395                            Int level,
1396                            Cursor* c_die,
1397                            Cursor* c_abbv,
1398                            CUConst* cc,
1399                            Bool td3 )
1400{
1401   ULong       cts;
1402   Int         ctsSzB;
1403   UWord       ctsMemSzB;
1404
1405   UWord saved_die_c_offset  = get_position_of_Cursor( c_die );
1406   UWord saved_abbv_c_offset = get_position_of_Cursor( c_abbv );
1407
1408   varstack_preen( parser, td3, level-1 );
1409
1410   if (dtag == DW_TAG_compile_unit) {
1411      Bool have_lo    = False;
1412      Bool have_hi1   = False;
1413      Bool have_range = False;
1414      Addr ip_lo    = 0;
1415      Addr ip_hi1   = 0;
1416      Addr rangeoff = 0;
1417      while (True) {
1418         DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
1419         DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
1420         if (attr == 0 && form == 0) break;
1421         get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
1422                            cc, c_die, False/*td3*/, form );
1423         if (attr == DW_AT_low_pc && ctsSzB > 0) {
1424            ip_lo   = cts;
1425            have_lo = True;
1426         }
1427         if (attr == DW_AT_high_pc && ctsSzB > 0) {
1428            ip_hi1   = cts;
1429            have_hi1 = True;
1430         }
1431         if (attr == DW_AT_ranges && ctsSzB > 0) {
1432            rangeoff = cts;
1433            have_range = True;
1434         }
1435         if (attr == DW_AT_stmt_list && ctsSzB > 0) {
1436            read_filename_table( parser, cc, (UWord)cts, td3 );
1437         }
1438      }
1439      /* Now, does this give us an opportunity to find this
1440         CU's svma? */
1441#if 0
1442      if (level == 0 && have_lo) {
1443         vg_assert(!cc->cu_svma_known); /* if this fails, it must be
1444         because we've already seen a DW_TAG_compile_unit DIE at level
1445         0.  But that can't happen, because DWARF3 only allows exactly
1446         one top level DIE per CU. */
1447         cc->cu_svma_known = True;
1448         cc->cu_svma = ip_lo;
1449         if (1)
1450            TRACE_D3("BBBBAAAA acquire CU_SVMA of %p\n", cc->cu_svma);
1451         /* Now, it may be that this DIE doesn't tell us the CU's
1452            SVMA, by way of not having a DW_AT_low_pc.  That's OK --
1453            the CU doesn't *have* to have its SVMA specified.
1454
1455            But as per last para D3 spec sec 3.1.1 ("Normal and
1456            Partial Compilation Unit Entries", "If the base address
1457            (viz, the SVMA) is undefined, then any DWARF entry of
1458            structure defined interms of the base address of that
1459            compilation unit is not valid.".  So that means, if whilst
1460            processing the children of this top level DIE (or their
1461            children, etc) we see a DW_AT_range, and cu_svma_known is
1462            False, then the DIE that contains it is (per the spec)
1463            invalid, and we can legitimately stop and complain. */
1464      }
1465#else
1466      /* .. whereas The Reality is, simply assume the SVMA is zero
1467         if it isn't specified. */
1468      if (level == 0) {
1469         vg_assert(!cc->cu_svma_known);
1470         cc->cu_svma_known = True;
1471         if (have_lo)
1472            cc->cu_svma = ip_lo;
1473         else
1474            cc->cu_svma = 0;
1475      }
1476#endif
1477      /* Do we have something that looks sane? */
1478      if (have_lo && have_hi1 && (!have_range)) {
1479         if (ip_lo < ip_hi1)
1480            varstack_push( cc, parser, td3,
1481                           unitary_range_list(ip_lo, ip_hi1 - 1),
1482                           level,
1483                           False/*isFunc*/, NULL/*fbGX*/ );
1484      } else
1485      if ((!have_lo) && (!have_hi1) && have_range) {
1486         varstack_push( cc, parser, td3,
1487                        get_range_list( cc, td3,
1488                                        rangeoff, cc->cu_svma ),
1489                        level,
1490                        False/*isFunc*/, NULL/*fbGX*/ );
1491      } else
1492      if ((!have_lo) && (!have_hi1) && (!have_range)) {
1493         /* CU has no code, presumably? */
1494         varstack_push( cc, parser, td3,
1495                        empty_range_list(),
1496                        level,
1497                        False/*isFunc*/, NULL/*fbGX*/ );
1498      } else
1499         goto bad_DIE;
1500   }
1501
1502   if (dtag == DW_TAG_lexical_block || dtag == DW_TAG_subprogram) {
1503      Bool   have_lo    = False;
1504      Bool   have_hi1   = False;
1505      Bool   have_range = False;
1506      Addr   ip_lo      = 0;
1507      Addr   ip_hi1     = 0;
1508      Addr   rangeoff   = 0;
1509      Bool   isFunc     = dtag == DW_TAG_subprogram;
1510      GExpr* fbGX       = NULL;
1511      while (True) {
1512         DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
1513         DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
1514         if (attr == 0 && form == 0) break;
1515         get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
1516                            cc, c_die, False/*td3*/, form );
1517         if (attr == DW_AT_low_pc && ctsSzB > 0) {
1518            ip_lo   = cts;
1519            have_lo = True;
1520         }
1521         if (attr == DW_AT_high_pc && ctsSzB > 0) {
1522            ip_hi1   = cts;
1523            have_hi1 = True;
1524         }
1525         if (attr == DW_AT_ranges && ctsSzB > 0) {
1526            rangeoff = cts;
1527            have_range = True;
1528         }
1529         if (isFunc
1530             && attr == DW_AT_frame_base
1531             && ((ctsMemSzB > 0 && ctsSzB == 0)
1532                 || (ctsMemSzB == 0 && ctsSzB > 0))) {
1533            fbGX = get_GX( cc, False/*td3*/, cts, ctsSzB, ctsMemSzB );
1534            vg_assert(fbGX);
1535            vg_assert(!fbGX->next);
1536            fbGX->next = *gexprs;
1537            *gexprs = fbGX;
1538         }
1539      }
1540      /* Do we have something that looks sane? */
1541      if (dtag == DW_TAG_subprogram
1542          && (!have_lo) && (!have_hi1) && (!have_range)) {
1543         /* This is legit - ignore it. Sec 3.3.3: "A subroutine entry
1544            representing a subroutine declaration that is not also a
1545            definition does not have code address or range
1546            attributes." */
1547      } else
1548      if (dtag == DW_TAG_lexical_block
1549          && (!have_lo) && (!have_hi1) && (!have_range)) {
1550         /* I believe this is legit, and means the lexical block
1551            contains no insns (whatever that might mean).  Ignore. */
1552      } else
1553      if (have_lo && have_hi1 && (!have_range)) {
1554         /* This scope supplies just a single address range. */
1555         if (ip_lo < ip_hi1)
1556            varstack_push( cc, parser, td3,
1557                           unitary_range_list(ip_lo, ip_hi1 - 1),
1558                           level, isFunc, fbGX );
1559      } else
1560      if ((!have_lo) && (!have_hi1) && have_range) {
1561         /* This scope supplies multiple address ranges via the use of
1562            a range list. */
1563         varstack_push( cc, parser, td3,
1564                        get_range_list( cc, td3,
1565                                        rangeoff, cc->cu_svma ),
1566                        level, isFunc, fbGX );
1567      } else
1568      if (have_lo && (!have_hi1) && (!have_range)) {
1569         /* This scope is bogus.  The D3 spec sec 3.4 (Lexical Block
1570            Entries) says fairly clearly that a scope must have either
1571            _range or (_low_pc and _high_pc). */
1572         /* The spec is a bit ambiguous though.  Perhaps a single byte
1573            range is intended?  See sec 2.17 (Code Addresses And Ranges) */
1574         /* This case is here because icc9 produced this:
1575         <2><13bd>: DW_TAG_lexical_block
1576            DW_AT_decl_line   : 5229
1577            DW_AT_decl_column : 37
1578            DW_AT_decl_file   : 1
1579            DW_AT_low_pc      : 0x401b03
1580         */
1581         /* Ignore (seems safe than pushing a single byte range) */
1582      } else
1583         goto bad_DIE;
1584   }
1585
1586   if (dtag == DW_TAG_variable || dtag == DW_TAG_formal_parameter) {
1587      UChar* name        = NULL;
1588      Type*  typeR       = D3_INVALID_CUOFF;
1589      Bool   external    = False;
1590      GExpr* gexpr       = NULL;
1591      Int    n_attrs     = 0;
1592      UWord  abs_ori     = (UWord)D3_INVALID_CUOFF;
1593      Bool   declaration = False;
1594      Int    lineNo      = 0;
1595      UChar* fileName    = NULL;
1596      while (True) {
1597         DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
1598         DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
1599         if (attr == 0 && form == 0) break;
1600         get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
1601                            cc, c_die, False/*td3*/, form );
1602         n_attrs++;
1603         if (attr == DW_AT_name && ctsMemSzB > 0) {
1604            name = ML_(addStr)( cc->di, (UChar*)(UWord)cts, -1 );
1605         }
1606         if (attr == DW_AT_location
1607             && ((ctsMemSzB > 0 && ctsSzB == 0)
1608                 || (ctsMemSzB == 0 && ctsSzB > 0))) {
1609            gexpr = get_GX( cc, False/*td3*/, cts, ctsSzB, ctsMemSzB );
1610            vg_assert(gexpr);
1611            vg_assert(!gexpr->next);
1612            gexpr->next = *gexprs;
1613            *gexprs = gexpr;
1614         }
1615         if (attr == DW_AT_type && ctsSzB > 0) {
1616            typeR = (Type*)(UWord)cts;
1617         }
1618         if (attr == DW_AT_external && ctsSzB > 0 && cts > 0) {
1619            external = True;
1620         }
1621         if (attr == DW_AT_abstract_origin && ctsSzB > 0) {
1622            abs_ori = (UWord)cts;
1623         }
1624         if (attr == DW_AT_declaration && ctsSzB > 0 && cts > 0) {
1625            declaration = True;
1626         }
1627         if (attr == DW_AT_decl_line && ctsSzB > 0) {
1628            lineNo = (Int)cts;
1629         }
1630         if (attr == DW_AT_decl_file && ctsSzB > 0) {
1631            Int ftabIx = (Int)cts;
1632            if (ftabIx >= 1
1633                && ftabIx < VG_(sizeXA)( parser->filenameTable )) {
1634               fileName = *(UChar**)
1635                          VG_(indexXA)( parser->filenameTable, ftabIx );
1636               vg_assert(fileName);
1637            }
1638            if (0) VG_(printf)("XXX filename = %s\n", fileName);
1639         }
1640      }
1641      /* We'll collect it under if one of the following three
1642         conditions holds:
1643         (1) has location and type    -> completed
1644         (2) has type only            -> is an abstract instance
1645         (3) has location and abs_ori -> is a concrete instance
1646         Name, filename and line number are all option frills.
1647      */
1648      if ( /* 1 */ (gexpr && typeR != D3_INVALID_CUOFF)
1649           /* 2 */ || (typeR != D3_INVALID_CUOFF)
1650           /* 3 */ || (gexpr && abs_ori != (UWord)D3_INVALID_CUOFF) ) {
1651
1652         /* Add this variable to the list of interesting looking
1653            variables.  Crucially, note along with it the address
1654            range(s) associated with the variable, which for locals
1655            will be the address ranges at the top of the varparser's
1656            stack. */
1657         GExpr*   fbGX = NULL;
1658         Word     i, nRanges;
1659         XArray*  /* of AddrRange */ xa;
1660         TempVar* tv;
1661         /* Stack can't be empty; we put a dummy entry on it for the
1662            entire address range before starting with the DIEs for
1663            this CU. */
1664         vg_assert(parser->sp >= 0);
1665
1666         /* If this is a local variable (non-external), try to find
1667            the GExpr for the DW_AT_frame_base of the containing
1668            function.  It should have been pushed on the stack at the
1669            time we encountered its DW_TAG_subprogram DIE, so the way
1670            to find it is to scan back down the stack looking for it.
1671            If there isn't an enclosing stack entry marked 'isFunc'
1672            then we must be seeing variable or formal param DIEs
1673            outside of a function, so we deem the Dwarf to be
1674            malformed if that happens.  Note that the fbGX may be NULL
1675            if the containing DT_TAG_subprogram didn't supply a
1676            DW_AT_frame_base -- that's OK, but there must actually be
1677            a containing DW_TAG_subprogram. */
1678         if (!external) {
1679            Bool found = False;
1680            for (i = parser->sp; i >= 0; i--) {
1681               if (parser->isFunc[i]) {
1682                  fbGX = parser->fbGX[i];
1683                  found = True;
1684                  break;
1685               }
1686            }
1687            if (!found) {
1688               if (0 && VG_(clo_verbosity) >= 0) {
1689                  VG_(message)(Vg_DebugMsg,
1690                     "warning: parse_var_DIE: non-external variable "
1691                     "outside DW_TAG_subprogram");
1692               }
1693               /* goto bad_DIE; */
1694               /* This seems to happen a lot.  Just ignore it -- if,
1695                  when we come to evaluation of the location (guarded)
1696                  expression, it requires a frame base value, and
1697                  there's no expression for that, then evaluation as a
1698                  whole will fail.  Harmless - a bit of a waste of
1699                  cycles but nothing more. */
1700            }
1701         }
1702
1703         /* re "external ? 0 : parser->sp" (twice), if the var is
1704            marked 'external' then we must put it at the global scope,
1705            as only the global scope (level 0) covers the entire PC
1706            address space.  It is asserted elsewhere that level 0
1707            always covers the entire address space. */
1708         xa = parser->ranges[external ? 0 : parser->sp];
1709         nRanges = VG_(sizeXA)(xa);
1710         vg_assert(nRanges >= 0);
1711
1712         tv = ML_(dinfo_zalloc)( sizeof(TempVar) );
1713         tv->name   = name;
1714         tv->level  = external ? 0 : parser->sp;
1715         tv->typeR  = typeR;
1716         tv->gexpr  = gexpr;
1717         tv->fbGX   = fbGX;
1718         tv->fName  = fileName;
1719         tv->fLine  = lineNo;
1720         tv->dioff  = posn;
1721         tv->absOri = abs_ori;
1722
1723         /* See explanation on definition of type TempVar for the
1724            reason for this elaboration. */
1725         tv->nRanges = nRanges;
1726         tv->rngOneMin = 0;
1727         tv->rngOneMax = 0;
1728         tv->rngMany = NULL;
1729         if (nRanges == 1) {
1730            AddrRange* range = VG_(indexXA)(xa, 0);
1731            tv->rngOneMin = range->aMin;
1732            tv->rngOneMax = range->aMax;
1733         }
1734         else if (nRanges > 1) {
1735            tv->rngMany = VG_(cloneXA)( xa ); /* free when 'tv' freed */
1736         }
1737
1738         tv->next  = *tempvars;
1739         *tempvars = tv;
1740
1741         TRACE_D3("  Recording this variable, with %ld PC range(s)\n",
1742                  VG_(sizeXA)(xa) );
1743         /* collect stats on how effective the ->ranges special
1744            casing is */
1745         if (0) {
1746           static Int ntot=0, ngt=0;
1747           ntot++;
1748           if (tv->rngMany) ngt++;
1749           if (0 == (ntot % 100000))
1750              VG_(printf)("XXXX %d tot, %d cloned\n", ntot, ngt);
1751         }
1752
1753      }
1754
1755      /* Here are some other weird cases seen in the wild:
1756
1757            We have a variable with a name and a type, but no
1758            location.  I guess that's a sign that it has been
1759            optimised away.  Ignore it.  Here's an example:
1760
1761            static Int lc_compar(void* n1, void* n2) {
1762               MC_Chunk* mc1 = *(MC_Chunk**)n1;
1763               MC_Chunk* mc2 = *(MC_Chunk**)n2;
1764               return (mc1->data < mc2->data ? -1 : 1);
1765            }
1766
1767            Both mc1 and mc2 are like this
1768            <2><5bc>: Abbrev Number: 21 (DW_TAG_variable)
1769                DW_AT_name        : mc1
1770                DW_AT_decl_file   : 1
1771                DW_AT_decl_line   : 216
1772                DW_AT_type        : <5d3>
1773
1774            whereas n1 and n2 do have locations specified.
1775
1776            ---------------------------------------------
1777
1778            We see a DW_TAG_formal_parameter with a type, but
1779            no name and no location.  It's probably part of a function type
1780            construction, thusly, hence ignore it:
1781         <1><2b4>: Abbrev Number: 12 (DW_TAG_subroutine_type)
1782             DW_AT_sibling     : <2c9>
1783             DW_AT_prototyped  : 1
1784             DW_AT_type        : <114>
1785         <2><2be>: Abbrev Number: 13 (DW_TAG_formal_parameter)
1786             DW_AT_type        : <13e>
1787         <2><2c3>: Abbrev Number: 13 (DW_TAG_formal_parameter)
1788             DW_AT_type        : <133>
1789
1790            ---------------------------------------------
1791
1792            Is very minimal, like this:
1793            <4><81d>: Abbrev Number: 44 (DW_TAG_variable)
1794                DW_AT_abstract_origin: <7ba>
1795            What that signifies I have no idea.  Ignore.
1796
1797            ----------------------------------------------
1798
1799            Is very minimal, like this:
1800            <200f>: DW_TAG_formal_parameter
1801                DW_AT_abstract_ori: <1f4c>
1802                DW_AT_location    : 13440
1803            What that signifies I have no idea.  Ignore.
1804            It might be significant, though: the variable at least
1805            has a location and so might exist somewhere.
1806            Maybe we should handle this.
1807
1808            ---------------------------------------------
1809
1810            <22407>: DW_TAG_variable
1811              DW_AT_name        : (indirect string, offset: 0x6579):
1812                                  vgPlain_trampoline_stuff_start
1813              DW_AT_decl_file   : 29
1814              DW_AT_decl_line   : 56
1815              DW_AT_external    : 1
1816              DW_AT_declaration : 1
1817
1818            Nameless and typeless variable that has a location?  Who
1819            knows.  Not me.
1820            <2><3d178>: Abbrev Number: 22 (DW_TAG_variable)
1821                 DW_AT_location    : 9 byte block: 3 c0 c7 13 38 0 0 0 0
1822                                     (DW_OP_addr: 3813c7c0)
1823
1824            No, really.  Check it out.  gcc is quite simply borked.
1825            <3><168cc>: Abbrev Number: 141 (DW_TAG_variable)
1826            // followed by no attributes, and the next DIE is a sibling,
1827            // not a child
1828            */
1829   }
1830   return;
1831
1832  bad_DIE:
1833   set_position_of_Cursor( c_die,  saved_die_c_offset );
1834   set_position_of_Cursor( c_abbv, saved_abbv_c_offset );
1835   VG_(printf)("\nparse_var_DIE: confused by:\n");
1836   VG_(printf)(" <%d><%lx>: %s\n", level, posn, ML_(pp_DW_TAG)( dtag ) );
1837   while (True) {
1838      DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
1839      DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
1840      if (attr == 0 && form == 0) break;
1841      VG_(printf)("     %18s: ", ML_(pp_DW_AT)(attr));
1842      /* Get the form contents, so as to print them */
1843      get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
1844                         cc, c_die, True, form );
1845      VG_(printf)("\t\n");
1846   }
1847   VG_(printf)("\n");
1848   cc->barf("parse_var_DIE: confused by the above DIE");
1849   /*NOTREACHED*/
1850}
1851
1852
1853/*------------------------------------------------------------*/
1854/*---                                                      ---*/
1855/*--- Parsing of type-related DIEs                         ---*/
1856/*---                                                      ---*/
1857/*------------------------------------------------------------*/
1858
1859#define N_D3_TYPE_STACK 16
1860
1861typedef
1862   struct {
1863      /* What source language?  'C'=C/C++, 'F'=Fortran, '?'=other
1864         Established once per compilation unit. */
1865      UChar language;
1866      /* A stack of types which are currently under construction */
1867      Int   sp; /* [sp] is innermost active entry; sp==-1 for empty
1868                   stack */
1869      Type* qparent[N_D3_TYPE_STACK];
1870      Int   qlevel[N_D3_TYPE_STACK];
1871
1872   }
1873   D3TypeParser;
1874
1875static void typestack_show ( D3TypeParser* parser, HChar* str ) {
1876   Word i;
1877   VG_(printf)("  typestack (%s) {\n", str);
1878   for (i = 0; i <= parser->sp; i++) {
1879      VG_(printf)("    [%ld] (level %d): ", i, parser->qlevel[i]);
1880      ML_(pp_Type)( parser->qparent[i] );
1881      VG_(printf)("\n");
1882   }
1883   VG_(printf)("  }\n");
1884}
1885
1886/* Remove from the stack, all entries with .level > 'level' */
1887static
1888void typestack_preen ( D3TypeParser* parser, Bool td3, Int level )
1889{
1890   Bool changed = False;
1891   vg_assert(parser->sp < N_D3_TYPE_STACK);
1892   while (True) {
1893      vg_assert(parser->sp >= -1);
1894      if (parser->sp == -1) break;
1895      if (parser->qlevel[parser->sp] <= level) break;
1896      if (0)
1897         TRACE_D3("BBBBAAAA typestack_pop [newsp=%d]\n", parser->sp-1);
1898      vg_assert(parser->qparent[parser->sp]);
1899      parser->qparent[parser->sp] = NULL;
1900      parser->qlevel[parser->sp]  = 0;
1901      parser->sp--;
1902      changed = True;
1903   }
1904   if (changed && td3)
1905      typestack_show( parser, "after preen" );
1906}
1907
1908static Bool typestack_is_empty ( D3TypeParser* parser ) {
1909   vg_assert(parser->sp >= -1 && parser->sp < N_D3_TYPE_STACK);
1910   return parser->sp == -1;
1911}
1912
1913static void typestack_push ( CUConst* cc,
1914                             D3TypeParser* parser,
1915                             Bool td3,
1916                             Type* parent, Int level ) {
1917   if (0)
1918   TRACE_D3("BBBBAAAA typestack_push[newsp=%d]: %d  %p\n",
1919            parser->sp+1, level, parent);
1920
1921   /* First we need to zap everything >= 'level', as we are about to
1922      replace any previous entry at 'level', so .. */
1923   typestack_preen(parser, /*td3*/False, level-1);
1924
1925   vg_assert(parser->sp >= -1);
1926   vg_assert(parser->sp < N_D3_TYPE_STACK);
1927   if (parser->sp == N_D3_TYPE_STACK-1)
1928      cc->barf("typestack_push: N_D3_TYPE_STACK is too low; "
1929               "increase and recompile");
1930   if (parser->sp >= 0)
1931      vg_assert(parser->qlevel[parser->sp] < level);
1932   parser->sp++;
1933   vg_assert(parser->qparent[parser->sp] == NULL);
1934   vg_assert(parser->qlevel[parser->sp]  == 0);
1935   vg_assert(parent != NULL);
1936   parser->qparent[parser->sp] = parent;
1937   parser->qlevel[parser->sp]  = level;
1938   if (td3)
1939      typestack_show( parser, "after push" );
1940}
1941
1942
1943/* Parse a type-related DIE.  'parser' holds the current parser state.
1944   'admin' is where the completed types are dumped.  'dtag' is the tag
1945   for this DIE.  'c_die' points to the start of the data fields (FORM
1946   stuff) for the DIE.  c_abbv points to the start of the (name,form)
1947   pairs which describe the DIE.
1948
1949   We may find the DIE uninteresting, in which case we should ignore
1950   it.
1951*/
1952__attribute__((noinline))
1953static void parse_type_DIE ( /*OUT*/TyAdmin** admin,
1954                             /*MOD*/D3TypeParser* parser,
1955                             DW_TAG dtag,
1956                             UWord posn,
1957                             Int level,
1958                             Cursor* c_die,
1959                             Cursor* c_abbv,
1960                             CUConst* cc,
1961                             Bool td3 )
1962{
1963   ULong     cts;
1964   Int       ctsSzB;
1965   UWord     ctsMemSzB;
1966   Type*     type   = NULL;
1967   TyAtom*   atom   = NULL;
1968   TyField*  field  = NULL;
1969   D3Expr*   expr   = NULL;
1970   TyBounds* bounds = NULL;
1971
1972   UWord saved_die_c_offset  = get_position_of_Cursor( c_die );
1973   UWord saved_abbv_c_offset = get_position_of_Cursor( c_abbv );
1974
1975   /* If we've returned to a level at or above any previously noted
1976      parent, un-note it, so we don't believe we're still collecting
1977      its children. */
1978   typestack_preen( parser, td3, level-1 );
1979
1980   if (dtag == DW_TAG_compile_unit) {
1981      /* See if we can find DW_AT_language, since it is important for
1982         establishing array bounds (see DW_TAG_subrange_type below in
1983         this fn) */
1984      while (True) {
1985         DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
1986         DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
1987         if (attr == 0 && form == 0) break;
1988         get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
1989                            cc, c_die, False/*td3*/, form );
1990         if (attr != DW_AT_language)
1991            continue;
1992         if (ctsSzB == 0)
1993           goto bad_DIE;
1994         switch (cts) {
1995            case DW_LANG_C89: case DW_LANG_C:
1996            case DW_LANG_C_plus_plus: case DW_LANG_ObjC:
1997            case DW_LANG_ObjC_plus_plus: case DW_LANG_UPC:
1998            case DW_LANG_Upc:
1999               parser->language = 'C'; break;
2000            case DW_LANG_Fortran77: case DW_LANG_Fortran90:
2001            case DW_LANG_Fortran95:
2002               parser->language = 'F'; break;
2003            case DW_LANG_Ada83: case DW_LANG_Cobol74:
2004            case DW_LANG_Cobol85: case DW_LANG_Pascal83:
2005            case DW_LANG_Modula2: case DW_LANG_Java:
2006            case DW_LANG_C99: case DW_LANG_Ada95:
2007            case DW_LANG_PLI: case DW_LANG_D:
2008            case DW_LANG_Mips_Assembler:
2009               parser->language = '?'; break;
2010            default:
2011               goto bad_DIE;
2012         }
2013      }
2014   }
2015
2016   if (dtag == DW_TAG_base_type) {
2017      /* We can pick up a new base type any time. */
2018      type = ML_(new_Type)();
2019      type->tag = Ty_Base;
2020      while (True) {
2021         DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
2022         DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
2023         if (attr == 0 && form == 0) break;
2024         get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
2025                            cc, c_die, False/*td3*/, form );
2026         if (attr == DW_AT_name && ctsMemSzB > 0) {
2027            type->Ty.Base.name
2028               = ML_(addStr)( cc->di, (UChar*)(UWord)cts, -1 );
2029         }
2030         if (attr == DW_AT_byte_size && ctsSzB > 0) {
2031            type->Ty.Base.szB = cts;
2032         }
2033         if (attr == DW_AT_encoding && ctsSzB > 0) {
2034            switch (cts) {
2035               case DW_ATE_unsigned: case DW_ATE_unsigned_char:
2036               case DW_ATE_boolean:/* FIXME - is this correct? */
2037                  type->Ty.Base.enc = 'U'; break;
2038               case DW_ATE_signed: case DW_ATE_signed_char:
2039                  type->Ty.Base.enc = 'S'; break;
2040               case DW_ATE_float:
2041                  type->Ty.Base.enc = 'F'; break;
2042               case DW_ATE_complex_float:
2043                  type->Ty.Base.enc = 'C'; break;
2044               default:
2045                  goto bad_DIE;
2046            }
2047         }
2048      }
2049
2050      /* Invent a name if it doesn't have one.  gcc-4.3
2051         -ftree-vectorize is observed to emit nameless base types. */
2052      if (!type->Ty.Base.name)
2053         type->Ty.Base.name
2054            = ML_(addStr)( cc->di, "<anon_base_type>", -1 );
2055
2056      /* Do we have something that looks sane? */
2057      if (/* must have a name */
2058          type->Ty.Base.name == NULL
2059          /* and a plausible size.  Yes, really 32: "complex long
2060             double" apparently has size=32 */
2061          || type->Ty.Base.szB < 0 || type->Ty.Base.szB > 32
2062          /* and a plausible encoding */
2063          || (type->Ty.Base.enc != 'U'
2064              && type->Ty.Base.enc != 'S'
2065              && type->Ty.Base.enc != 'F'
2066              && type->Ty.Base.enc != 'C'))
2067         goto bad_DIE;
2068      /* Last minute hack: if we see this
2069         <1><515>: DW_TAG_base_type
2070             DW_AT_byte_size   : 0
2071             DW_AT_encoding    : 5
2072             DW_AT_name        : void
2073         convert it into a real Void type. */
2074      if (type->Ty.Base.szB == 0
2075          && 0 == VG_(strcmp)("void", type->Ty.Base.name)) {
2076         VG_(memset)(type, 0, sizeof(*type));
2077         type->tag = Ty_Void;
2078         type->Ty.Void.isFake = False; /* it's a real one! */
2079      }
2080      goto acquire_Type;
2081   }
2082
2083   if (dtag == DW_TAG_pointer_type || dtag == DW_TAG_reference_type
2084       || dtag == DW_TAG_ptr_to_member_type) {
2085      /* This seems legit for _pointer_type and _reference_type.  I
2086         don't know if rolling _ptr_to_member_type in here really is
2087         legit, but it's better than not handling it at all. */
2088      type = ML_(new_Type)();
2089      type->tag = Ty_PorR;
2090      /* target type defaults to void */
2091      type->Ty.PorR.typeR = D3_FAKEVOID_CUOFF;
2092      type->Ty.PorR.isPtr = dtag == DW_TAG_pointer_type
2093                            || dtag == DW_TAG_ptr_to_member_type;
2094      /* Pointer types don't *have* to specify their size, in which
2095         case we assume it's a machine word.  But if they do specify
2096         it, it must be a machine word :-) This probably assumes that
2097         the word size of the Dwarf3 we're reading is the same size as
2098         that on the machine.  gcc appears to give a size whereas icc9
2099         doesn't. */
2100      if (type->Ty.PorR.isPtr)
2101         type->Ty.PorR.szB = sizeof(Word);
2102      while (True) {
2103         DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
2104         DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
2105         if (attr == 0 && form == 0) break;
2106         get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
2107                            cc, c_die, False/*td3*/, form );
2108         if (attr == DW_AT_byte_size && ctsSzB > 0) {
2109            type->Ty.PorR.szB = cts;
2110         }
2111         if (attr == DW_AT_type && ctsSzB > 0) {
2112            type->Ty.PorR.typeR = (Type*)(UWord)cts;
2113         }
2114      }
2115      /* Do we have something that looks sane? */
2116      if (type->Ty.PorR.szB != sizeof(Word))
2117         goto bad_DIE;
2118      else
2119         goto acquire_Type;
2120   }
2121
2122   if (dtag == DW_TAG_enumeration_type) {
2123      /* Create a new Type to hold the results. */
2124      type = ML_(new_Type)();
2125      type->tag = Ty_Enum;
2126      type->Ty.Enum.name = NULL;
2127      type->Ty.Enum.atomRs
2128         = VG_(newXA)( ML_(dinfo_zalloc), ML_(dinfo_free),
2129                       sizeof(TyAtom*) );
2130      while (True) {
2131         DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
2132         DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
2133         if (attr == 0 && form == 0) break;
2134         get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
2135                            cc, c_die, False/*td3*/, form );
2136         if (attr == DW_AT_name && ctsMemSzB > 0) {
2137            type->Ty.Enum.name
2138               = ML_(addStr)( cc->di, (UChar*)(UWord)cts, -1 );
2139         }
2140         if (attr == DW_AT_byte_size && ctsSzB > 0) {
2141            type->Ty.Enum.szB = cts;
2142         }
2143      }
2144      /* Do we have something that looks sane? */
2145      if (type->Ty.Enum.szB == 0 /* we must know the size */
2146          /* But the name can be present, or not */)
2147         goto bad_DIE;
2148      /* On't stack! */
2149      typestack_push( cc, parser, td3, type, level );
2150      goto acquire_Type;
2151   }
2152
2153   if (dtag == DW_TAG_enumerator) {
2154      Bool have_value = False;
2155      atom = ML_(new_TyAtom)( NULL, 0 );
2156      while (True) {
2157         DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
2158         DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
2159         if (attr == 0 && form == 0) break;
2160         get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
2161                            cc, c_die, False/*td3*/, form );
2162         if (attr == DW_AT_name && ctsMemSzB > 0) {
2163            atom->name = ML_(addStr)( cc->di, (UChar*)(UWord)cts, -1 );
2164         }
2165         if (attr == DW_AT_const_value && ctsSzB > 0) {
2166            atom->value = cts;
2167            have_value = True;
2168         }
2169      }
2170      /* Do we have something that looks sane? */
2171      if ((!have_value) || atom->name == NULL)
2172         goto bad_DIE;
2173      /* Do we have a plausible parent? */
2174      if (typestack_is_empty(parser)) goto bad_DIE;
2175      vg_assert(parser->qparent[parser->sp]);
2176      if (level != parser->qlevel[parser->sp]+1) goto bad_DIE;
2177      if (parser->qparent[parser->sp]->tag != Ty_Enum) goto bad_DIE;
2178      /* Record this child in the parent */
2179      vg_assert(parser->qparent[parser->sp]->Ty.Enum.atomRs);
2180      VG_(addToXA)( parser->qparent[parser->sp]->Ty.Enum.atomRs, &atom );
2181      /* And record the child itself */
2182      goto acquire_Atom;
2183   }
2184
2185   if (dtag == DW_TAG_structure_type || dtag == DW_TAG_union_type) {
2186      Bool have_szB = False;
2187      Bool is_decl  = False;
2188      Bool is_spec  = False;
2189      /* Create a new Type to hold the results. */
2190      type = ML_(new_Type)();
2191      type->tag = Ty_StOrUn;
2192      type->Ty.StOrUn.name = NULL;
2193      type->Ty.StOrUn.fields
2194         = VG_(newXA)( ML_(dinfo_zalloc), ML_(dinfo_free),
2195                       sizeof(TyAtom*) );
2196      type->Ty.StOrUn.complete = True;
2197      type->Ty.StOrUn.isStruct = dtag == DW_TAG_structure_type;
2198      while (True) {
2199         DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
2200         DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
2201         if (attr == 0 && form == 0) break;
2202         get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
2203                            cc, c_die, False/*td3*/, form );
2204         if (attr == DW_AT_name && ctsMemSzB > 0) {
2205            type->Ty.StOrUn.name
2206               = ML_(addStr)( cc->di, (UChar*)(UWord)cts, -1 );
2207         }
2208         if (attr == DW_AT_byte_size && ctsSzB >= 0) {
2209            type->Ty.StOrUn.szB = cts;
2210            have_szB = True;
2211         }
2212         if (attr == DW_AT_declaration && ctsSzB > 0 && cts > 0) {
2213            is_decl = True;
2214         }
2215         if (attr == DW_AT_specification && ctsSzB > 0 && cts > 0) {
2216            is_spec = True;
2217         }
2218      }
2219      /* Do we have something that looks sane? */
2220      if (is_decl && (!is_spec)) {
2221         /* It's a DW_AT_declaration.  We require the name but
2222            nothing else. */
2223         if (type->Ty.StOrUn.name == NULL)
2224            goto bad_DIE;
2225         type->Ty.StOrUn.complete = False;
2226         goto acquire_Type;
2227      }
2228      if ((!is_decl) /* && (!is_spec) */) {
2229         /* this is the common, ordinary case */
2230         if ((!have_szB) /* we must know the size */
2231             /* But the name can be present, or not */)
2232            goto bad_DIE;
2233         /* On't stack! */
2234         typestack_push( cc, parser, td3, type, level );
2235         goto acquire_Type;
2236      }
2237      else {
2238         /* don't know how to handle any other variants just now */
2239         goto bad_DIE;
2240      }
2241   }
2242
2243   if (dtag == DW_TAG_member) {
2244      /* Acquire member entries for both DW_TAG_structure_type and
2245         DW_TAG_union_type.  They differ minorly, in that struct
2246         members must have a DW_AT_data_member_location expression
2247         whereas union members must not. */
2248      Bool parent_is_struct;
2249      field = ML_(new_TyField)( NULL, NULL, NULL );
2250      field->typeR = D3_INVALID_CUOFF;
2251      expr  = NULL;
2252      while (True) {
2253         DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
2254         DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
2255         if (attr == 0 && form == 0) break;
2256         get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
2257                            cc, c_die, False/*td3*/, form );
2258         if (attr == DW_AT_name && ctsMemSzB > 0) {
2259            field->name = ML_(addStr)( cc->di, (UChar*)(UWord)cts, -1 );
2260         }
2261         if (attr == DW_AT_type && ctsSzB > 0) {
2262            field->typeR = (Type*)(UWord)cts;
2263         }
2264         if (attr == DW_AT_data_member_location && ctsMemSzB > 0) {
2265            UChar* copy = ML_(addStr)( cc->di, (UChar*)(UWord)cts,
2266                                               (Int)ctsMemSzB );
2267            expr = ML_(new_D3Expr)( copy, (UWord)ctsMemSzB );
2268         }
2269      }
2270      /* Do we have a plausible parent? */
2271      if (typestack_is_empty(parser)) goto bad_DIE;
2272      vg_assert(parser->qparent[parser->sp]);
2273      if (level != parser->qlevel[parser->sp]+1) goto bad_DIE;
2274      if (parser->qparent[parser->sp]->tag != Ty_StOrUn) goto bad_DIE;
2275      /* Do we have something that looks sane?  If this a member of a
2276         struct, we must have a location expression; but if a member
2277         of a union that is irrelevant (D3 spec sec 5.6.6).  We ought
2278         to reject in the latter case, but some compilers have been
2279         observed to emit constant-zero expressions.  So just ignore
2280         them. */
2281      parent_is_struct
2282         = parser->qparent[parser->sp]->Ty.StOrUn.isStruct;
2283      if (!field->name)
2284         field->name = ML_(addStr)(cc->di, "<anon_field>", -1);
2285      if ((!field->name) || (field->typeR == D3_INVALID_CUOFF))
2286         goto bad_DIE;
2287      if (parent_is_struct && (!expr))
2288         goto bad_DIE;
2289      if ((!parent_is_struct) && expr) {
2290         /* If this is a union type, pretend we haven't seen the data
2291            member location expression, as it is by definition
2292            redundant (it must be zero). */
2293         expr = NULL;
2294      }
2295      /* Record this child in the parent */
2296      field->isStruct = parent_is_struct;
2297      if (expr)
2298         field->loc = expr;
2299      vg_assert(parser->qparent[parser->sp]->Ty.StOrUn.fields);
2300      VG_(addToXA)( parser->qparent[parser->sp]->Ty.StOrUn.fields,
2301                    &field );
2302      /* And record the child itself */
2303      goto acquire_Field_and_Expr;
2304   }
2305
2306   if (dtag == DW_TAG_array_type) {
2307      type = ML_(new_Type)();
2308      type->tag = Ty_Array;
2309      type->Ty.Array.typeR = D3_INVALID_CUOFF;
2310      type->Ty.Array.bounds
2311         = VG_(newXA)( ML_(dinfo_zalloc), ML_(dinfo_free),
2312                       sizeof(TyBounds*) );
2313      while (True) {
2314         DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
2315         DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
2316         if (attr == 0 && form == 0) break;
2317         get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
2318                            cc, c_die, False/*td3*/, form );
2319         if (attr == DW_AT_type && ctsSzB > 0) {
2320            type->Ty.Array.typeR = (Type*)(UWord)cts;
2321         }
2322      }
2323      if (type->Ty.Array.typeR == D3_INVALID_CUOFF)
2324         goto bad_DIE;
2325      /* On't stack! */
2326      typestack_push( cc, parser, td3, type, level );
2327      goto acquire_Type;
2328   }
2329
2330   if (dtag == DW_TAG_subrange_type) {
2331      Bool have_lower = False;
2332      Bool have_upper = False;
2333      Bool have_count = False;
2334      Long lower = 0;
2335      Long upper = 0;
2336      Long count = 0;
2337
2338      switch (parser->language) {
2339         case 'C': have_lower = True;  lower = 0; break;
2340         case 'F': have_lower = True;  lower = 1; break;
2341         case '?': have_lower = False; break;
2342         default:  vg_assert(0); /* assured us by handling of
2343                                    DW_TAG_compile_unit in this fn */
2344      }
2345      bounds = ML_(new_TyBounds)();
2346      while (True) {
2347         DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
2348         DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
2349         if (attr == 0 && form == 0) break;
2350         get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
2351                            cc, c_die, False/*td3*/, form );
2352         if (attr == DW_AT_lower_bound && ctsSzB > 0) {
2353            lower      = (Long)cts;
2354            have_lower = True;
2355         }
2356         if (attr == DW_AT_upper_bound && ctsSzB > 0) {
2357            upper      = (Long)cts;
2358            have_upper = True;
2359         }
2360         if (attr == DW_AT_count && ctsSzB > 0) {
2361            count      = cts;
2362            have_count = True;
2363         }
2364      }
2365      /* FIXME: potentially skip the rest if no parent present, since
2366         it could be the case that this subrange type is free-standing
2367         (not being used to describe the bounds of a containing array
2368         type) */
2369      /* Do we have a plausible parent? */
2370      if (typestack_is_empty(parser)) goto bad_DIE;
2371      vg_assert(parser->qparent[parser->sp]);
2372      if (level != parser->qlevel[parser->sp]+1) goto bad_DIE;
2373      if (parser->qparent[parser->sp]->tag != Ty_Array) goto bad_DIE;
2374
2375      /* Figure out if we have a definite range or not */
2376      if (have_lower && have_upper && (!have_count)) {
2377         bounds->knownL = True;
2378         bounds->knownU = True;
2379         bounds->boundL = lower;
2380         bounds->boundU = upper;
2381      }
2382      else if (have_lower && (!have_upper) && (!have_count)) {
2383         bounds->knownL = True;
2384         bounds->knownU = False;
2385         bounds->boundL = lower;
2386         bounds->boundU = 0;
2387      } else {
2388         /* FIXME: handle more cases */
2389         goto bad_DIE;
2390      }
2391
2392      /* Record this bound in the parent */
2393      vg_assert(parser->qparent[parser->sp]->Ty.Array.bounds);
2394      VG_(addToXA)( parser->qparent[parser->sp]->Ty.Array.bounds,
2395                    &bounds );
2396      /* And record the child itself */
2397      goto acquire_Bounds;
2398   }
2399
2400   if (dtag == DW_TAG_typedef) {
2401      /* We can pick up a new base type any time. */
2402      type = ML_(new_Type)();
2403      type->tag = Ty_TyDef;
2404      type->Ty.TyDef.name = NULL;
2405      type->Ty.TyDef.typeR = D3_INVALID_CUOFF;
2406      while (True) {
2407         DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
2408         DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
2409         if (attr == 0 && form == 0) break;
2410         get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
2411                            cc, c_die, False/*td3*/, form );
2412         if (attr == DW_AT_name && ctsMemSzB > 0) {
2413            type->Ty.TyDef.name
2414               = ML_(addStr)( cc->di, (UChar*)(UWord)cts, -1 );
2415         }
2416         if (attr == DW_AT_type && ctsSzB > 0) {
2417            type->Ty.TyDef.typeR = (Type*)(UWord)cts;
2418         }
2419      }
2420      /* Do we have something that looks sane? */
2421      if (/* must have a name */
2422          type->Ty.TyDef.name == NULL
2423          /* but the referred-to type can be absent */)
2424         goto bad_DIE;
2425      else
2426         goto acquire_Type;
2427   }
2428
2429   if (dtag == DW_TAG_subroutine_type) {
2430      /* function type? just record that one fact and ask no
2431         further questions. */
2432      type = ML_(new_Type)();
2433      type->tag = Ty_Fn;
2434      goto acquire_Type;
2435   }
2436
2437   if (dtag == DW_TAG_volatile_type || dtag == DW_TAG_const_type) {
2438      Int have_ty = 0;
2439      type = ML_(new_Type)();
2440      type->tag = Ty_Qual;
2441      type->Ty.Qual.qual
2442         = dtag == DW_TAG_volatile_type ? 'V' : 'C';
2443      /* target type defaults to 'void' */
2444      type->Ty.Qual.typeR = D3_FAKEVOID_CUOFF;
2445      while (True) {
2446         DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
2447         DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
2448         if (attr == 0 && form == 0) break;
2449         get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
2450                            cc, c_die, False/*td3*/, form );
2451         if (attr == DW_AT_type && ctsSzB > 0) {
2452            type->Ty.Qual.typeR = (Type*)(UWord)cts;
2453            have_ty++;
2454         }
2455      }
2456      /* gcc sometimes generates DW_TAG_const/volatile_type without
2457         DW_AT_type and GDB appears to interpret the type as 'const
2458         void' (resp. 'volatile void').  So just allow it .. */
2459      if (have_ty == 1 || have_ty == 0)
2460         goto acquire_Type;
2461      else
2462         goto bad_DIE;
2463   }
2464
2465   /* else ignore this DIE */
2466   return;
2467   /*NOTREACHED*/
2468
2469  acquire_Type:
2470   if (0) VG_(printf)("YYYY Acquire Type\n");
2471   vg_assert(type); vg_assert(!atom); vg_assert(!field);
2472   vg_assert(!expr); vg_assert(!bounds);
2473   *admin            = ML_(new_TyAdmin)( posn, *admin );
2474   (*admin)->payload = type;
2475   (*admin)->tag     = TyA_Type;
2476   return;
2477   /*NOTREACHED*/
2478
2479  acquire_Atom:
2480   if (0) VG_(printf)("YYYY Acquire Atom\n");
2481   vg_assert(!type); vg_assert(atom); vg_assert(!field);
2482   vg_assert(!expr); vg_assert(!bounds);
2483   *admin            = ML_(new_TyAdmin)( posn, *admin );
2484   (*admin)->payload = atom;
2485   (*admin)->tag     = TyA_Atom;
2486   return;
2487   /*NOTREACHED*/
2488
2489  acquire_Field_and_Expr:
2490   /* For union members, Expr should be absent */
2491   if (0) VG_(printf)("YYYY Acquire Field and Expr\n");
2492   vg_assert(!type); vg_assert(!atom); vg_assert(field);
2493   /*vg_assert(expr);*/ vg_assert(!bounds);
2494   if (expr) {
2495      *admin            = ML_(new_TyAdmin)( (UWord)D3_INVALID_CUOFF,
2496                                            *admin );
2497      (*admin)->payload = expr;
2498      (*admin)->tag     = TyA_Expr;
2499   }
2500   *admin            = ML_(new_TyAdmin)( posn, *admin );
2501   (*admin)->payload = field;
2502   (*admin)->tag     = TyA_Field;
2503   return;
2504   /*NOTREACHED*/
2505
2506  acquire_Bounds:
2507   if (0) VG_(printf)("YYYY Acquire Bounds\n");
2508   vg_assert(!type); vg_assert(!atom); vg_assert(!field);
2509   vg_assert(!expr); vg_assert(bounds);
2510   *admin            = ML_(new_TyAdmin)( posn, *admin );
2511   (*admin)->payload = bounds;
2512   (*admin)->tag     = TyA_Bounds;
2513   return;
2514   /*NOTREACHED*/
2515
2516  bad_DIE:
2517   set_position_of_Cursor( c_die,  saved_die_c_offset );
2518   set_position_of_Cursor( c_abbv, saved_abbv_c_offset );
2519   VG_(printf)("\nparse_type_DIE: confused by:\n");
2520   VG_(printf)(" <%d><%lx>: %s\n", level, posn, ML_(pp_DW_TAG)( dtag ) );
2521   while (True) {
2522      DW_AT   attr = (DW_AT)  get_ULEB128( c_abbv );
2523      DW_FORM form = (DW_FORM)get_ULEB128( c_abbv );
2524      if (attr == 0 && form == 0) break;
2525      VG_(printf)("     %18s: ", ML_(pp_DW_AT)(attr));
2526      /* Get the form contents, so as to print them */
2527      get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
2528                         cc, c_die, True, form );
2529      VG_(printf)("\t\n");
2530   }
2531   VG_(printf)("\n");
2532   cc->barf("parse_type_DIE: confused by the above DIE");
2533   /*NOTREACHED*/
2534}
2535
2536
2537/*------------------------------------------------------------*/
2538/*---                                                      ---*/
2539/*--- Resolution of references to type DIEs                ---*/
2540/*---                                                      ---*/
2541/*------------------------------------------------------------*/
2542
2543static Int cmp_D3TyAdmin_by_cuOff ( void* v1, void* v2 ) {
2544   TyAdmin* a1 = *(TyAdmin**)v1;
2545   TyAdmin* a2 = *(TyAdmin**)v2;
2546   if (a1->cuOff < a2->cuOff) return -1;
2547   if (a1->cuOff > a2->cuOff) return 1;
2548   return 0;
2549}
2550
2551/* Look up 'cuOff' in 'map', to find the associated D3TyAdmin*.  Check
2552   that the found D3TyAdmin has tag 'adtag'.  Sets *payload to be the
2553   resulting payload pointer and returns True on success.
2554
2555   Also, if 'allow_invalid' is True, then if cuOff is
2556   D3_INVALID_CUOFF, return NULL in *payload.
2557
2558   Otherwise (conceptually fails) and returns False. */
2559__attribute__((noinline))
2560static Bool resolve_binding ( /*OUT*/void** payload,
2561                              XArray* map, void* cuOff,
2562                              TyAdminTag tag,
2563                              Bool allow_invalid ) {
2564   Bool    found;
2565   Word    ixLo, ixHi;
2566   TyAdmin dummy, *dummyP, *admin;
2567
2568   if (cuOff == D3_INVALID_CUOFF && allow_invalid) {
2569      *payload = NULL;
2570      return True;
2571   }
2572
2573   VG_(memset)(&dummy, 0, sizeof(dummy));
2574   dummy.cuOff = (UWord)cuOff;
2575   dummyP = &dummy;
2576   found = VG_(lookupXA)( map, &dummyP, &ixLo, &ixHi );
2577   if (!found)
2578      return False;
2579   /* If this doesn't hold, we must have seen more than one DIE with
2580      the same cuOff(set).  Which isn't possible. */
2581   vg_assert(ixLo == ixHi);
2582   admin = *(TyAdmin**)VG_(indexXA)( map, ixLo );
2583   /* All payload pointers should be non-NULL.  Ensured by assertion in
2584      loop in resolve_type_entities that creates 'map'.  Hence it is
2585      safe to return NULL to indicate 'not found'. */
2586   vg_assert(admin->payload);
2587   vg_assert(admin->cuOff == (UWord)cuOff); /* stay sane */
2588
2589   if (admin->tag != tag)
2590      return False;
2591
2592   *payload = admin->payload;
2593   return True;
2594}
2595
2596__attribute__((noinline))
2597static void resolve_type_entities ( /*MOD*/TyAdmin* admin,
2598                                    /*MOD*/TempVar* vars )
2599{
2600   Bool     ok;
2601   void*    payload;
2602   TyAdmin* adp;
2603   XArray* /* of D3TyAdmin* */ map;
2604
2605   map = VG_(newXA)( ML_(dinfo_zalloc), ML_(dinfo_free),
2606                     sizeof(TyAdmin*) );
2607   for (adp = admin; adp; adp = adp->next) {
2608      vg_assert(adp);
2609      vg_assert(adp->payload != NULL);
2610      if (adp->cuOff != (UWord)D3_INVALID_CUOFF) {
2611         VG_(addToXA)( map, &adp );
2612      }
2613   }
2614
2615   VG_(setCmpFnXA)( map, cmp_D3TyAdmin_by_cuOff );
2616   if (0)
2617      VG_(printf)("XXXXXX sorting map with %d entries\n",
2618                  (Int)VG_(sizeXA)(map));
2619   VG_(sortXA)( map );
2620
2621   for (adp = admin; adp; adp = adp->next) {
2622      vg_assert(adp->payload);
2623      switch (adp->tag) {
2624      case TyA_Bounds: {
2625         TyBounds* bounds = (TyBounds*)adp->payload;
2626         if (bounds->knownL && bounds->knownU
2627             && bounds->knownL > bounds->knownU) goto baaad;
2628         break;
2629      }
2630      case TyA_Atom: {
2631         TyAtom* atom = (TyAtom*)adp->payload;
2632         if (!atom->name) goto baaad;
2633         break;
2634      }
2635      case TyA_Expr: {
2636         D3Expr* expr = (D3Expr*)adp->payload;
2637         if (!expr->bytes) goto baaad;
2638         break;
2639      }
2640      case TyA_Field: {
2641         TyField* field = (TyField*)adp->payload;
2642         if (!field->name) goto baaad;
2643         if ( (field->isStruct && (!field->loc))
2644              || ((!field->isStruct) && field->loc))
2645            goto baaad;
2646         ok = resolve_binding( &payload, map, field->typeR,
2647                               TyA_Type, False/*!allow_invalid*/ );
2648         if (!ok) goto baaad;
2649         field->typeR = payload;
2650         break;
2651      }
2652      case TyA_Type: {
2653         UChar   enc;
2654         XArray* xa;
2655         Type* ty = (Type*)adp->payload;
2656         switch (ty->tag) {
2657            case Ty_Base:
2658               enc = ty->Ty.Base.enc;
2659               if ((!ty->Ty.Base.name)
2660                   || ty->Ty.Base.szB < 1 || ty->Ty.Base.szB > 32
2661                   || (enc != 'S' && enc != 'U' && enc != 'F' && enc != 'C'))
2662                  goto baaad;
2663               break;
2664            case Ty_TyDef:
2665               if (!ty->Ty.TyDef.name) goto baaad;
2666               ok = resolve_binding( &payload, map,
2667                                     ty->Ty.TyDef.typeR,
2668                                     TyA_Type,
2669                                     True/*allow_invalid*/ );
2670               if (!ok) goto baaad;
2671               ty->Ty.TyDef.typeR = payload;
2672               break;
2673            case Ty_PorR:
2674               if (ty->Ty.PorR.szB != sizeof(Word)) goto baaad;
2675               ok = resolve_binding( &payload, map,
2676                                     ty->Ty.PorR.typeR,
2677                                     TyA_Type,
2678                                     False/*!allow_invalid*/ );
2679               if (!ok) goto baaad;
2680               ty->Ty.PorR.typeR = payload;
2681               break;
2682            case Ty_Array:
2683               if (!ty->Ty.Array.bounds) goto baaad;
2684               ok = resolve_binding( &payload, map,
2685                                     ty->Ty.Array.typeR,
2686                                     TyA_Type,
2687                                     False/*!allow_invalid*/ );
2688               if (!ok) goto baaad;
2689               ty->Ty.Array.typeR = payload;
2690               break;
2691            case Ty_Enum:
2692               if ((!ty->Ty.Enum.atomRs)
2693                   || ty->Ty.Enum.szB < 1
2694                   || ty->Ty.Enum.szB > 8) goto baaad;
2695               xa = ty->Ty.Enum.atomRs;
2696               break;
2697            case Ty_StOrUn:
2698               xa = ty->Ty.StOrUn.fields;
2699               if (!xa) goto baaad;
2700               break;
2701            case Ty_Fn:
2702               break;
2703            case Ty_Qual:
2704               if (ty->Ty.Qual.qual != 'C'
2705                   && ty->Ty.Qual.qual != 'V') goto baaad;
2706               ok = resolve_binding( &payload, map,
2707                                     ty->Ty.Qual.typeR,
2708                                     TyA_Type,
2709                                     False/*!allow_invalid*/ );
2710               if (!ok) goto baaad;
2711               ty->Ty.Qual.typeR = payload;
2712               break;
2713            case Ty_Void:
2714               if (ty->Ty.Void.isFake != False
2715                   && ty->Ty.Void.isFake != True) goto baaad;
2716               break;
2717            default:
2718               goto baaad;
2719         }
2720         break;
2721      }
2722      baaad:
2723      default:
2724         VG_(printf)("valgrind: bad D3TyAdmin: ");
2725         ML_(pp_TyAdmin)(adp);
2726         VG_(printf)("\n");
2727      }
2728   }
2729
2730   /* Now resolve the variables list */
2731   for (; vars; vars = vars->next) {
2732      payload = NULL;
2733      ok = resolve_binding( &payload, map, vars->typeR,
2734                            TyA_Type, True/*allow_invalid*/ );
2735
2736      if (0 && !ok)
2737         VG_(printf)("Can't resolve type reference 0x%lx\n",
2738                     (UWord)vars->typeR);
2739      //vg_assert(ok);
2740      vars->typeR = payload;
2741   }
2742
2743   VG_(deleteXA)( map );
2744}
2745
2746
2747/*------------------------------------------------------------*/
2748/*---                                                      ---*/
2749/*--- Parsing of Compilation Units                         ---*/
2750/*---                                                      ---*/
2751/*------------------------------------------------------------*/
2752
2753static Int cmp_TempVar_by_dioff ( void* v1, void* v2 ) {
2754   TempVar* t1 = *(TempVar**)v1;
2755   TempVar* t2 = *(TempVar**)v2;
2756   if (t1->dioff < t2->dioff) return -1;
2757   if (t1->dioff > t2->dioff) return 1;
2758   return 0;
2759}
2760
2761static void read_DIE ( /*OUT*/TyAdmin** admin,
2762                       /*OUT*/TempVar** tempvars,
2763                       /*OUT*/GExpr** gexprs,
2764                       /*MOD*/D3TypeParser* typarser,
2765                       /*MOD*/D3VarParser* varparser,
2766                       Cursor* c, Bool td3, CUConst* cc, Int level )
2767{
2768   Cursor abbv;
2769   ULong  atag, abbv_code;
2770   UWord  posn;
2771   UInt   has_children;
2772   UWord  start_die_c_offset, start_abbv_c_offset;
2773   UWord  after_die_c_offset, after_abbv_c_offset;
2774
2775   /* --- Deal with this DIE --- */
2776   posn      = get_position_of_Cursor( c );
2777   abbv_code = get_ULEB128( c );
2778   set_abbv_Cursor( &abbv, td3, cc, abbv_code );
2779   atag      = get_ULEB128( &abbv );
2780   TRACE_D3("\n");
2781   TRACE_D3(" <%d><%lx>: Abbrev Number: %llu (%s)\n",
2782            level, posn, abbv_code, ML_(pp_DW_TAG)( atag ) );
2783
2784   if (atag == 0)
2785      cc->barf("read_DIE: invalid zero tag on DIE");
2786
2787   has_children = get_UChar( &abbv );
2788   if (has_children != DW_children_no && has_children != DW_children_yes)
2789      cc->barf("read_DIE: invalid has_children value");
2790
2791   /* We're set up to look at the fields of this DIE.  Hand it off to
2792      any parser(s) that want to see it.  Since they will in general
2793      advance both the DIE and abbrev cursors, remember their current
2794      settings so that we can then back up and do one final pass over
2795      the DIE, to print out its contents. */
2796
2797   start_die_c_offset  = get_position_of_Cursor( c );
2798   start_abbv_c_offset = get_position_of_Cursor( &abbv );
2799
2800   while (True) {
2801      ULong cts;
2802      Int   ctsSzB;
2803      UWord ctsMemSzB;
2804      ULong at_name = get_ULEB128( &abbv );
2805      ULong at_form = get_ULEB128( &abbv );
2806      if (at_name == 0 && at_form == 0) break;
2807      TRACE_D3("     %18s: ", ML_(pp_DW_AT)(at_name));
2808      /* Get the form contents, but ignore them; the only purpose is
2809         to print them, if td3 is True */
2810      get_Form_contents( &cts, &ctsSzB, &ctsMemSzB,
2811                         cc, c, td3, (DW_FORM)at_form );
2812      TRACE_D3("\t");
2813      TRACE_D3("\n");
2814   }
2815
2816   after_die_c_offset  = get_position_of_Cursor( c );
2817   after_abbv_c_offset = get_position_of_Cursor( &abbv );
2818
2819   set_position_of_Cursor( c,     start_die_c_offset );
2820   set_position_of_Cursor( &abbv, start_abbv_c_offset );
2821
2822   parse_type_DIE( admin,
2823                   typarser,
2824                   (DW_TAG)atag,
2825                   posn,
2826                   level,
2827                   c,     /* DIE cursor */
2828                   &abbv, /* abbrev cursor */
2829                   cc,
2830                   td3 );
2831
2832   set_position_of_Cursor( c,     start_die_c_offset );
2833   set_position_of_Cursor( &abbv, start_abbv_c_offset );
2834
2835   parse_var_DIE( tempvars,
2836                  gexprs,
2837                  varparser,
2838                  (DW_TAG)atag,
2839                  posn,
2840                  level,
2841                  c,     /* DIE cursor */
2842                  &abbv, /* abbrev cursor */
2843                  cc,
2844                  td3 );
2845
2846   set_position_of_Cursor( c,     after_die_c_offset );
2847   set_position_of_Cursor( &abbv, after_abbv_c_offset );
2848
2849   /* --- Now recurse into its children, if any --- */
2850   if (has_children == DW_children_yes) {
2851      if (0) TRACE_D3("BEGIN children of level %d\n", level);
2852      while (True) {
2853         atag = peek_ULEB128( c );
2854         if (atag == 0) break;
2855         read_DIE( admin, tempvars, gexprs, typarser, varparser,
2856                   c, td3, cc, level+1 );
2857      }
2858      /* Now we need to eat the terminating zero */
2859      atag = get_ULEB128( c );
2860      vg_assert(atag == 0);
2861      if (0) TRACE_D3("END children of level %d\n", level);
2862   }
2863
2864}
2865
2866
2867static
2868void new_dwarf3_reader_wrk (
2869   struct _DebugInfo* di,
2870   __attribute__((noreturn))
2871   void (*barf)( HChar* ),
2872   UChar* debug_info_img,   SizeT debug_info_sz,
2873   UChar* debug_abbv_img,   SizeT debug_abbv_sz,
2874   UChar* debug_line_img,   SizeT debug_line_sz,
2875   UChar* debug_str_img,    SizeT debug_str_sz,
2876   UChar* debug_ranges_img, SizeT debug_ranges_sz,
2877   UChar* debug_loc_img,    SizeT debug_loc_sz
2878)
2879{
2880   TyAdmin *admin, *adminp;
2881   TempVar *tempvars, *varp, *varp2;
2882   GExpr *gexprs, *gexpr;
2883   Cursor abbv; /* for showing .debug_abbrev */
2884   Cursor info; /* primary cursor for parsing .debug_info */
2885   Cursor ranges; /* for showing .debug_ranges */
2886   D3TypeParser typarser;
2887   D3VarParser varparser;
2888   Addr  dr_base;
2889   UWord dr_offset;
2890   Word  i;
2891   Bool td3 = di->trace_symtab;
2892   XArray* /* of TempVar* */ dioff_lookup_tab;
2893   Bool text_biasing_borked;
2894   KludgeyTextBiaser ktb;
2895
2896#if 0
2897   /* This doesn't work properly because it assumes all entries are
2898      packed end to end, with no holes.  But that doesn't always
2899      appear to be the case, so it loses sync.  And the D3 spec
2900      doesn't appear to require a no-hole situation either. */
2901   /* Display .debug_loc */
2902   Addr  dl_base;
2903   UWord dl_offset;
2904   Cursor loc; /* for showing .debug_loc */
2905   TRACE_SYMTAB("\n");
2906   TRACE_SYMTAB("\n------ The contents of .debug_loc ------\n");
2907   TRACE_SYMTAB("    Offset   Begin    End      Expression\n");
2908   init_Cursor( &loc, debug_loc_img,
2909                debug_loc_sz, 0, barf,
2910                "Overrun whilst reading .debug_loc section(1)" );
2911   dl_base = 0;
2912   dl_offset = 0;
2913   while (True) {
2914      UWord  w1, w2;
2915      UWord  len;
2916      if (is_at_end_Cursor( &loc ))
2917         break;
2918
2919      /* Read a (host-)word pair.  This is something of a hack since
2920         the word size to read is really dictated by the ELF file;
2921         however, we assume we're reading a file with the same
2922         word-sizeness as the host.  Reasonably enough. */
2923      w1 = get_UWord( &loc );
2924      w2 = get_UWord( &loc );
2925
2926      if (w1 == 0 && w2 == 0) {
2927         /* end of list.  reset 'base' */
2928         TRACE_D3("    %08lx <End of list>\n", dl_offset);
2929         dl_base = 0;
2930         dl_offset = get_position_of_Cursor( &loc );
2931         continue;
2932      }
2933
2934      if (w1 == -1UL) {
2935         /* new value for 'base' */
2936         TRACE_D3("    %08lx %16lx %08lx (base address)\n",
2937                  dl_offset, w1, w2);
2938         dl_base = w2;
2939         continue;
2940      }
2941
2942      /* else a location expression follows */
2943      TRACE_D3("    %08lx %08lx %08lx ",
2944               dl_offset, w1 + dl_base, w2 + dl_base);
2945      len = (UWord)get_UShort( &loc );
2946      while (len > 0) {
2947         UChar byte = get_UChar( &loc );
2948         TRACE_D3("%02x", (UInt)byte);
2949         len--;
2950      }
2951      TRACE_SYMTAB("\n");
2952   }
2953#endif
2954
2955   /* Display .debug_ranges */
2956   TRACE_SYMTAB("\n");
2957   TRACE_SYMTAB("\n------ The contents of .debug_ranges ------\n");
2958   TRACE_SYMTAB("    Offset   Begin    End\n");
2959   init_Cursor( &ranges, debug_ranges_img,
2960                debug_ranges_sz, 0, barf,
2961                "Overrun whilst reading .debug_ranges section(1)" );
2962   dr_base = 0;
2963   dr_offset = 0;
2964   while (True) {
2965      UWord  w1, w2;
2966
2967      if (is_at_end_Cursor( &ranges ))
2968         break;
2969
2970      /* Read a (host-)word pair.  This is something of a hack since
2971         the word size to read is really dictated by the ELF file;
2972         however, we assume we're reading a file with the same
2973         word-sizeness as the host.  Reasonably enough. */
2974      w1 = get_UWord( &ranges );
2975      w2 = get_UWord( &ranges );
2976
2977      if (w1 == 0 && w2 == 0) {
2978         /* end of list.  reset 'base' */
2979         TRACE_D3("    %08lx <End of list>\n", dr_offset);
2980         dr_base = 0;
2981         dr_offset = get_position_of_Cursor( &ranges );
2982         continue;
2983      }
2984
2985      if (w1 == -1UL) {
2986         /* new value for 'base' */
2987         TRACE_D3("    %08lx %16lx %08lx (base address)\n",
2988                  dr_offset, w1, w2);
2989         dr_base = w2;
2990         continue;
2991      }
2992
2993      /* else a range [w1+base, w2+base) is denoted */
2994      TRACE_D3("    %08lx %08lx %08lx\n",
2995               dr_offset, w1 + dr_base, w2 + dr_base);
2996   }
2997
2998
2999   /* Display .debug_abbrev */
3000   init_Cursor( &abbv, debug_abbv_img, debug_abbv_sz, 0, barf,
3001                "Overrun whilst reading .debug_abbrev section" );
3002   TRACE_SYMTAB("\n");
3003   TRACE_SYMTAB("\n------ The contents of .debug_abbrev ------\n");
3004   while (True) {
3005      if (is_at_end_Cursor( &abbv ))
3006         break;
3007      /* Read one abbreviation table */
3008      TRACE_D3("  Number TAG\n");
3009      while (True) {
3010         ULong atag;
3011         UInt  has_children;
3012         ULong acode = get_ULEB128( &abbv );
3013         if (acode == 0) break; /* end of the table */
3014         atag = get_ULEB128( &abbv );
3015         has_children = get_UChar( &abbv );
3016         TRACE_D3("   %llu      %s    [%s]\n",
3017                  acode, ML_(pp_DW_TAG)(atag),
3018                         ML_(pp_DW_children)(has_children));
3019         while (True) {
3020            ULong at_name = get_ULEB128( &abbv );
3021            ULong at_form = get_ULEB128( &abbv );
3022            if (at_name == 0 && at_form == 0) break;
3023            TRACE_D3("    %18s %s\n",
3024                     ML_(pp_DW_AT)(at_name), ML_(pp_DW_FORM)(at_form));
3025         }
3026      }
3027   }
3028   TRACE_SYMTAB("\n");
3029
3030   /* Now loop over the Compilation Units listed in the .debug_info
3031      section (see D3SPEC sec 7.5) paras 1 and 2.  Each compilation
3032      unit contains a Compilation Unit Header followed by precisely
3033      one DW_TAG_compile_unit or DW_TAG_partial_unit DIE. */
3034   init_Cursor( &info, debug_info_img, debug_info_sz, 0, barf,
3035                "Overrun whilst reading .debug_info section" );
3036
3037   /* We'll park the harvested type information in here.  Also create
3038      a fake "void" entry with offset D3_FAKEVOID_CUOFF, so we always
3039      have at least one type entry to refer to.  D3_FAKEVOID_CUOFF is
3040      huge and presumably will not occur in any valid DWARF3 file --
3041      it would need to have a .debug_info section 4GB long for that to
3042      happen.  These type entries end up in the DebugInfo. */
3043   admin = NULL;
3044   { Type* tVoid = ML_(new_Type)();
3045     tVoid->tag = Ty_Void;
3046     tVoid->Ty.Void.isFake = True;
3047     admin = ML_(new_TyAdmin)( (UWord)D3_FAKEVOID_CUOFF, admin );
3048     admin->payload = tVoid;
3049     admin->tag     = TyA_Type;
3050   }
3051
3052   /* List of variables we're accumulating.  These don't end up in the
3053      DebugInfo; instead their contents are handed to ML_(addVar) and
3054      the list elements are then deleted. */
3055   tempvars = NULL;
3056
3057   /* List of GExprs we're accumulating.  These wind up in the
3058      DebugInfo. */
3059   gexprs = NULL;
3060
3061   /* We need a D3TypeParser to keep track of partially constructed
3062      types.  It'll be discarded as soon as we've completed the CU,
3063      since the resulting information is tipped in to 'admin' as it is
3064      generated. */
3065   VG_(memset)( &typarser, 0, sizeof(typarser) );
3066   typarser.sp = -1;
3067   typarser.language = '?';
3068
3069   VG_(memset)( &varparser, 0, sizeof(varparser) );
3070   varparser.sp = -1;
3071
3072   TRACE_D3("\n------ Parsing .debug_info section ------\n");
3073   while (True) {
3074      UWord   cu_start_offset, cu_offset_now;
3075      CUConst cc;
3076
3077      /* It seems icc9 finishes the DIE info before debug_info_sz
3078         bytes have been used up.  So be flexible, and declare the
3079         sequence complete if there is not enough remaining bytes to
3080         hold even the smallest conceivable CU header.  (11 bytes I
3081         reckon). */
3082      Word avail = get_remaining_length_Cursor( &info );
3083      if (avail < 11) {
3084         if (avail > 0)
3085            TRACE_D3("new_dwarf3_reader_wrk: warning: "
3086                     "%ld unused bytes after end of DIEs\n", avail);
3087         break;
3088      }
3089
3090      /* Check the varparser's stack is in a sane state. */
3091      vg_assert(varparser.sp == -1);
3092      for (i = 0; i < N_D3_VAR_STACK; i++) {
3093         vg_assert(varparser.ranges[i] == NULL);
3094         vg_assert(varparser.level[i] == 0);
3095      }
3096      for (i = 0; i < N_D3_TYPE_STACK; i++) {
3097         vg_assert(typarser.qparent[i] == NULL);
3098         vg_assert(typarser.qlevel[i] == 0);
3099      }
3100
3101      cu_start_offset = get_position_of_Cursor( &info );
3102      TRACE_D3("\n");
3103      TRACE_D3("  Compilation Unit @ offset 0x%lx:\n", cu_start_offset);
3104      /* parse_CU_header initialises the CU's set_abbv_Cursor cache
3105         (saC_cache) */
3106      parse_CU_Header( &cc, td3, &info,
3107                       (UChar*)debug_abbv_img, debug_abbv_sz );
3108      cc.debug_str_img    = debug_str_img;
3109      cc.debug_str_sz     = debug_str_sz;
3110      cc.debug_ranges_img = debug_ranges_img;
3111      cc.debug_ranges_sz  = debug_ranges_sz;
3112      cc.debug_loc_img    = debug_loc_img;
3113      cc.debug_loc_sz     = debug_loc_sz;
3114      cc.debug_line_img   = debug_line_img;
3115      cc.debug_line_sz    = debug_line_sz;
3116      cc.cu_start_offset  = cu_start_offset;
3117      cc.di = di;
3118      /* The CU's svma can be deduced by looking at the AT_low_pc
3119         value in the top level TAG_compile_unit, which is the topmost
3120         DIE.  We'll leave it for the 'varparser' to acquire that info
3121         and fill it in -- since it is the only party to want to know
3122         it. */
3123      cc.cu_svma_known = False;
3124      cc.cu_svma       = 0;
3125
3126      /* Create a fake outermost-level range covering the entire
3127         address range.  So we always have *something* to catch all
3128         variable declarations. */
3129      varstack_push( &cc, &varparser, td3,
3130                     unitary_range_list(0UL, ~0UL),
3131                     -1, False/*isFunc*/, NULL/*fbGX*/ );
3132
3133      /* And set up the file name table.  When we come across the top
3134         level DIE for this CU (which is what the next call to
3135         read_DIE should process) we will copy all the file names out
3136         of the .debug_line img area and use this table to look up the
3137         copies when we later see filename numbers in DW_TAG_variables
3138         etc. */
3139      vg_assert(!varparser.filenameTable );
3140      varparser.filenameTable
3141         = VG_(newXA)( ML_(dinfo_zalloc), ML_(dinfo_free),
3142                       sizeof(UChar*) );
3143      vg_assert(varparser.filenameTable );
3144
3145      /* Now read the one-and-only top-level DIE for this CU. */
3146      vg_assert(varparser.sp == 0);
3147      read_DIE( &admin, &tempvars, &gexprs, &typarser, &varparser,
3148                &info, td3, &cc, 0 );
3149
3150      cu_offset_now = get_position_of_Cursor( &info );
3151      if (1) TRACE_D3("offset now %ld, d-i-size %ld\n",
3152                      cu_offset_now, debug_info_sz);
3153      if (cu_offset_now > debug_info_sz)
3154         barf("toplevel DIEs beyond end of CU");
3155      if (cu_offset_now == debug_info_sz)
3156         break;
3157
3158      /* Preen to level -2.  DIEs have level >= 0 so -2 cannot occur
3159         anywhere else at all.  Our fake the-entire-address-space
3160         range is at level -1, so preening to -2 should completely
3161         empty the stack out. */
3162      TRACE_D3("\n");
3163      varstack_preen( &varparser, td3, -2 );
3164      /* Similarly, empty the type stack out. */
3165      typestack_preen( &typarser, td3, -2 );
3166      /* else keep going */
3167
3168      TRACE_D3("set_abbv_Cursor cache: %lu queries, %lu misses\n",
3169               cc.saC_cache_queries, cc.saC_cache_misses);
3170
3171      vg_assert(varparser.filenameTable );
3172      VG_(deleteXA)( varparser.filenameTable );
3173      varparser.filenameTable = NULL;
3174   }
3175
3176   /* Put the type entry list the right way round.  Not strictly
3177      necessary, but makes it easier to read. */
3178   vg_assert(admin);
3179   if (admin) {
3180      TyAdmin *next, *prev = NULL;
3181      for (adminp = admin; adminp; adminp = next) {
3182         next = adminp->next;
3183         adminp->next = prev;
3184         prev = adminp;
3185      }
3186      admin = prev;
3187   }
3188
3189   /* Put the variable list the right way round.  Not strictly
3190      necessary, but makes it easier to read. */
3191   if (tempvars) {
3192      TempVar *next, *prev = NULL;
3193      for (varp = tempvars; varp; varp = next) {
3194         next = varp->next;
3195         varp->next = prev;
3196         prev = varp;
3197      }
3198      tempvars = prev;
3199   }
3200
3201   TRACE_D3("\n");
3202   TRACE_D3("------ Acquired the following type entities: ------\n");
3203   for (adminp = admin; adminp; adminp = adminp->next) {
3204      TRACE_D3("   ");
3205      if (td3) ML_(pp_TyAdmin)( adminp );
3206      TRACE_D3("\n");
3207   }
3208   TRACE_D3("\n");
3209   TRACE_D3("------ Resolving type entries ------\n");
3210
3211   /* See "Comment_Regarding_DWARF3_Text_Biasing" above. */
3212   VG_(memset)( &ktb, 0, sizeof(ktb ));
3213   ktb.rx_map_avma = di->rx_map_avma;
3214   ktb.rx_map_size = di->rx_map_size;
3215   ktb.text_bias   = di->text_bias;
3216
3217   resolve_type_entities( admin, tempvars );
3218   for (gexpr = gexprs; gexpr; gexpr = gexpr->next) {
3219      bias_GX( gexpr, &ktb );
3220   }
3221
3222   TRACE_D3("\n");
3223   TRACE_D3("------ Acquired the following variables: ------\n\n");
3224
3225   /* Park (pointers to) all the vars in an XArray, so we can look up
3226      abstract origins quickly.  The array is sorted (hence, looked-up
3227      by) the .dioff fields.  Since the .dioffs should be instrictly
3228      ascending order, there is no need to sort the array after
3229      construction.  The ascendingness is however asserted for. */
3230   dioff_lookup_tab
3231      = VG_(newXA)( ML_(dinfo_zalloc), ML_(dinfo_free),
3232                    sizeof(TempVar*) );
3233   vg_assert(dioff_lookup_tab);
3234   varp2 = NULL;
3235   for (varp = tempvars; varp; varp = varp->next) {
3236      if (varp2)
3237         vg_assert(varp2->dioff < varp->dioff);
3238      VG_(addToXA)( dioff_lookup_tab, &varp );
3239      varp2 = varp;
3240   }
3241   VG_(setCmpFnXA)( dioff_lookup_tab, cmp_TempVar_by_dioff );
3242   VG_(sortXA)( dioff_lookup_tab ); /* POINTLESS; FIXME: rm */
3243
3244   /* Now visit each var.  Collect up as much info as possible for
3245      each var and hand it to ML_(addVar). */
3246   for (varp = tempvars; varp; varp = varp->next) {
3247
3248      /* Possibly show .. */
3249      if (td3) {
3250         VG_(printf)("<%lx> addVar: level %d: %s :: ",
3251                     varp->dioff,
3252                     varp->level,
3253                     varp->name ? varp->name : (UChar*)"<anon_var>" );
3254         if (varp->typeR) {
3255            ML_(pp_Type_C_ishly)( varp->typeR );
3256         } else {
3257            VG_(printf)("NULL");
3258         }
3259         VG_(printf)("\n  Loc=");
3260         if (varp->gexpr) {
3261            ML_(pp_GX)(varp->gexpr);
3262         } else {
3263            VG_(printf)("NULL");
3264         }
3265         VG_(printf)("\n");
3266         if (varp->fbGX) {
3267            VG_(printf)("  FrB=");
3268            ML_(pp_GX)( varp->fbGX );
3269            VG_(printf)("\n");
3270         } else {
3271            VG_(printf)("  FrB=none\n");
3272         }
3273         VG_(printf)("  declared at: %s:%d\n",
3274                     varp->fName ? varp->fName : (UChar*)"NULL",
3275                     varp->fLine );
3276         if (varp->absOri != (UWord)D3_INVALID_CUOFF)
3277            VG_(printf)("  abstract origin: <%lx>\n", varp->absOri);
3278      }
3279
3280      /* Skip variables which have no location.  These must be
3281         abstract instances; they are useless as-is since with no
3282         location they have no specified memory location.  They will
3283         presumably be referred to via the absOri fields of other
3284         variables. */
3285      if (!varp->gexpr) {
3286         TRACE_D3("  SKIP (no location)\n\n");
3287         continue;
3288      }
3289
3290      /* So it has a location, at least.  If it refers to some other
3291         entry through its absOri field, pull in further info through
3292         that. */
3293      if (varp->absOri != (UWord)D3_INVALID_CUOFF) {
3294         Bool found;
3295         Word ixFirst, ixLast;
3296         TempVar key;
3297         TempVar* keyp = &key;
3298         TempVar *varAI;
3299         VG_(memset)(&key, 0, sizeof(key)); /* not necessary */
3300         key.dioff = varp->absOri; /* this is what we want to find */
3301         found = VG_(lookupXA)( dioff_lookup_tab, &keyp,
3302                                &ixFirst, &ixLast );
3303         if (!found)
3304            barf("DW_AT_abstract_origin can't be resolved");
3305         /* If the following fails, there is more than one entry with
3306            the same dioff.  Which can't happen. */
3307         vg_assert(ixFirst == ixLast);
3308         varAI = *(TempVar**)VG_(indexXA)( dioff_lookup_tab, ixFirst );
3309         /* stay sane */
3310         vg_assert(varAI);
3311         vg_assert(varAI->dioff == varp->absOri);
3312
3313         /* Copy what useful info we can. */
3314         if (varAI->typeR && !varp->typeR)
3315            varp->typeR = varAI->typeR;
3316         if (varAI->name && !varp->name)
3317            varp->name = varAI->name;
3318         if (varAI->fName && !varp->fName)
3319            varp->fName = varAI->fName;
3320         if (varAI->fLine > 0 && varp->fLine == 0)
3321            varp->fLine = varAI->fLine;
3322      }
3323
3324      /* Give it a name if it doesn't have one. */
3325      if (!varp->name)
3326         varp->name = ML_(addStr)( di, "<anon_var>", -1 );
3327
3328      /* So now does it have enough info to be useful? */
3329      /* NOTE: re typeR: this is a hack.  If typeR is NULL then the
3330         type didn't get resolved.  Really, in that case something's
3331         broken earlier on, and should be fixed, rather than just
3332         skipping the variable. */
3333      if (!varp->typeR) continue;
3334      vg_assert(varp->gexpr);
3335      vg_assert(varp->name);
3336      vg_assert(varp->typeR);
3337      vg_assert(varp->level >= 0);
3338
3339      /* Ok.  So we're going to keep it.  Call ML_(addVar) once for
3340         each address range in which the variable exists. */
3341      TRACE_D3("  ACQUIRE for range(s) ");
3342      { AddrRange  oneRange;
3343        AddrRange* varPcRanges;
3344        Word       nVarPcRanges;
3345        /* Set up to iterate over address ranges, however
3346           represented. */
3347        if (varp->nRanges == 0 || varp->nRanges == 1) {
3348           vg_assert(!varp->rngMany);
3349           if (varp->nRanges == 0) {
3350              vg_assert(varp->rngOneMin == 0);
3351              vg_assert(varp->rngOneMax == 0);
3352           }
3353           nVarPcRanges = varp->nRanges;
3354           oneRange.aMin = varp->rngOneMin;
3355           oneRange.aMax = varp->rngOneMax;
3356           varPcRanges = &oneRange;
3357        } else {
3358           vg_assert(varp->rngMany);
3359           vg_assert(varp->rngOneMin == 0);
3360           vg_assert(varp->rngOneMax == 0);
3361           nVarPcRanges = VG_(sizeXA)(varp->rngMany);
3362           vg_assert(nVarPcRanges >= 2);
3363           vg_assert(nVarPcRanges == (Word)varp->nRanges);
3364           varPcRanges = VG_(indexXA)(varp->rngMany, 0);
3365        }
3366        if (varp->level == 0)
3367           vg_assert( nVarPcRanges == 1 );
3368        /* and iterate */
3369        for (i = 0; i < nVarPcRanges; i++) {
3370           Addr pcMin = varPcRanges[i].aMin;
3371           Addr pcMax = varPcRanges[i].aMax;
3372           vg_assert(pcMin <= pcMax);
3373           /* Level 0 is the global address range.  So at level 0 we
3374              don't want to bias pcMin/pcMax; but at all other levels
3375              we do since those are derived from svmas in the Dwarf
3376              we're reading.  Be paranoid ... */
3377           if (varp->level == 0) {
3378              vg_assert(pcMin == (Addr)0);
3379              vg_assert(pcMax == ~(Addr)0);
3380           } else {
3381              /* vg_assert(pcMin > (Addr)0);
3382                 No .. we can legitimately expect to see ranges like
3383                 0x0-0x11D (pre-biasing, of course). */
3384              vg_assert(pcMax < ~(Addr)0);
3385           }
3386
3387           /* Apply text biasing, for non-global variables. */
3388           if (varp->level > 0) {
3389              pcMin = apply_kludgey_text_bias( &ktb, pcMin );
3390              pcMax = apply_kludgey_text_bias( &ktb, pcMax );
3391           }
3392
3393           if (i > 0 && (i%2) == 0)
3394              TRACE_D3("\n                       ");
3395           TRACE_D3("[%p,%p] ", pcMin, pcMax );
3396
3397           ML_(addVar)(
3398              di, varp->level,
3399                  pcMin, pcMax,
3400                  varp->name, (void*)varp->typeR,
3401                  varp->gexpr, varp->fbGX,
3402                  varp->fName, varp->fLine, td3
3403           );
3404        }
3405      }
3406
3407      TRACE_D3("\n\n");
3408      /* and move on to the next var */
3409   }
3410
3411   /* For the text biasing to work out, we expect that:
3412      - there were no failures, and
3413      - either all were done straightforwardly, or all kludgily,
3414        but not with a mixture
3415   */
3416   text_biasing_borked
3417      = ktb.n_failed_biasings > 0
3418        || (ktb.n_straightforward_biasings > 0 && ktb.n_kludgey_biasings > 0);
3419
3420   if (td3 || text_biasing_borked) {
3421      VG_(printf)("TEXT SVMA BIASING STATISTICS:\n");
3422      VG_(printf)("   straightforward biasings: %lu\n",
3423                  ktb.n_straightforward_biasings );
3424      VG_(printf)("           kludgey biasings: %lu\n",
3425                  ktb.n_kludgey_biasings );
3426      VG_(printf)("            failed biasings: %lu\n\n",
3427                  ktb.n_failed_biasings );
3428   }
3429   if (text_biasing_borked)
3430      barf("couldn't make sense of DWARF3 text-svma biasing; details above");
3431
3432   /* Now free all the TempVars */
3433   for (varp = tempvars; varp; varp = varp2) {
3434      varp2 = varp->next;
3435      if (varp->rngMany)
3436         VG_(deleteXA)(varp->rngMany);
3437      ML_(dinfo_free)(varp);
3438   }
3439   tempvars = NULL;
3440
3441   /* And get rid of the temporary mapping table. */
3442   VG_(deleteXA)( dioff_lookup_tab );
3443
3444   /* record the TyAdmins and the GExprs in di so they can be freed
3445      later */
3446   vg_assert(!di->admin_tyadmins);
3447   di->admin_tyadmins = admin;
3448   vg_assert(!di->admin_gexprs);
3449   di->admin_gexprs = gexprs;
3450}
3451
3452
3453/*------------------------------------------------------------*/
3454/*---                                                      ---*/
3455/*--- The "new" DWARF3 reader -- top level control logic   ---*/
3456/*---                                                      ---*/
3457/*------------------------------------------------------------*/
3458
3459/* --- !!! --- EXTERNAL HEADERS start --- !!! --- */
3460#include <setjmp.h>   /* For jmp_buf */
3461/* --- !!! --- EXTERNAL HEADERS end --- !!! --- */
3462
3463static Bool    d3rd_jmpbuf_valid  = False;
3464static HChar*  d3rd_jmpbuf_reason = NULL;
3465static jmp_buf d3rd_jmpbuf;
3466
3467static __attribute__((noreturn)) void barf ( HChar* reason ) {
3468   vg_assert(d3rd_jmpbuf_valid);
3469   d3rd_jmpbuf_reason = reason;
3470   __builtin_longjmp(&d3rd_jmpbuf, 1);
3471   /*NOTREACHED*/
3472   vg_assert(0);
3473}
3474
3475
3476void
3477ML_(new_dwarf3_reader) (
3478   struct _DebugInfo* di,
3479   UChar* debug_info_img,   SizeT debug_info_sz,
3480   UChar* debug_abbv_img,   SizeT debug_abbv_sz,
3481   UChar* debug_line_img,   SizeT debug_line_sz,
3482   UChar* debug_str_img,    SizeT debug_str_sz,
3483   UChar* debug_ranges_img, SizeT debug_ranges_sz,
3484   UChar* debug_loc_img,    SizeT debug_loc_sz
3485)
3486{
3487   volatile Int  jumped;
3488   volatile Bool td3 = di->trace_symtab;
3489
3490   /* Run the _wrk function to read the dwarf3.  If it succeeds, it
3491      just returns normally.  If there is any failure, it longjmp's
3492      back here, having first set d3rd_jmpbuf_reason to something
3493      useful. */
3494   vg_assert(d3rd_jmpbuf_valid  == False);
3495   vg_assert(d3rd_jmpbuf_reason == NULL);
3496
3497   d3rd_jmpbuf_valid = True;
3498   jumped = __builtin_setjmp(&d3rd_jmpbuf);
3499   if (jumped == 0) {
3500      /* try this ... */
3501      new_dwarf3_reader_wrk( di, barf,
3502                             debug_info_img,   debug_info_sz,
3503                             debug_abbv_img,   debug_abbv_sz,
3504                             debug_line_img,   debug_line_sz,
3505                             debug_str_img,    debug_str_sz,
3506                             debug_ranges_img, debug_ranges_sz,
3507                             debug_loc_img,    debug_loc_sz );
3508      d3rd_jmpbuf_valid = False;
3509      TRACE_D3("\n------ .debug_info reading was successful ------\n");
3510   } else {
3511      /* It longjmp'd. */
3512      d3rd_jmpbuf_valid = False;
3513      /* Can't longjump without giving some sort of reason. */
3514      vg_assert(d3rd_jmpbuf_reason != NULL);
3515
3516      TRACE_D3("\n------ .debug_info reading failed ------\n");
3517
3518      ML_(symerr)(di, True, d3rd_jmpbuf_reason);
3519   }
3520
3521   d3rd_jmpbuf_valid  = False;
3522   d3rd_jmpbuf_reason = NULL;
3523}
3524
3525
3526
3527/* --- Unused code fragments which might be useful one day. --- */
3528
3529#if 0
3530   /* Read the arange tables */
3531   TRACE_SYMTAB("\n");
3532   TRACE_SYMTAB("\n------ The contents of .debug_arange ------\n");
3533   init_Cursor( &aranges, debug_aranges_img,
3534                debug_aranges_sz, 0, barf,
3535                "Overrun whilst reading .debug_aranges section" );
3536   while (True) {
3537      ULong  len, d_i_offset;
3538      Bool   is64;
3539      UShort version;
3540      UChar  asize, segsize;
3541
3542      if (is_at_end_Cursor( &aranges ))
3543         break;
3544      /* Read one arange thingy */
3545      /* initial_length field */
3546      len = get_Initial_Length( &is64, &aranges,
3547               "in .debug_aranges: invalid initial-length field" );
3548      version    = get_UShort( &aranges );
3549      d_i_offset = get_Dwarfish_UWord( &aranges, is64 );
3550      asize      = get_UChar( &aranges );
3551      segsize    = get_UChar( &aranges );
3552      TRACE_D3("  Length:                   %llu\n", len);
3553      TRACE_D3("  Version:                  %d\n", (Int)version);
3554      TRACE_D3("  Offset into .debug_info:  %llx\n", d_i_offset);
3555      TRACE_D3("  Pointer Size:             %d\n", (Int)asize);
3556      TRACE_D3("  Segment Size:             %d\n", (Int)segsize);
3557      TRACE_D3("\n");
3558      TRACE_D3("    Address            Length\n");
3559
3560      while ((get_position_of_Cursor( &aranges ) % (2 * asize)) > 0) {
3561         (void)get_UChar( & aranges );
3562      }
3563      while (True) {
3564         ULong address = get_Dwarfish_UWord( &aranges, asize==8 );
3565         ULong length = get_Dwarfish_UWord( &aranges, asize==8 );
3566         TRACE_D3("    0x%016llx 0x%llx\n", address, length);
3567         if (address == 0 && length == 0) break;
3568      }
3569   }
3570   TRACE_SYMTAB("\n");
3571#endif
3572
3573/*--------------------------------------------------------------------*/
3574/*--- end                                             readdwarf3.c ---*/
3575/*--------------------------------------------------------------------*/
3576