]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/valgrind/src/valgrind-3.6.0-svn/coregrind/m_debuginfo/storage.c
update
[l4.git] / l4 / pkg / valgrind / src / valgrind-3.6.0-svn / coregrind / m_debuginfo / storage.c
1
2 /*--------------------------------------------------------------------*/
3 /*--- Format-neutral storage of and querying of info acquired from ---*/
4 /*--- ELF/XCOFF stabs/dwarf1/dwarf2/dwarf3 debug info.             ---*/
5 /*---                                                    storage.c ---*/
6 /*--------------------------------------------------------------------*/
7
8 /*
9    This file is part of Valgrind, a dynamic binary instrumentation
10    framework.
11
12    Copyright (C) 2000-2010 Julian Seward 
13       jseward@acm.org
14
15    This program is free software; you can redistribute it and/or
16    modify it under the terms of the GNU General Public License as
17    published by the Free Software Foundation; either version 2 of the
18    License, or (at your option) any later version.
19
20    This program is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public License
26    along with this program; if not, write to the Free Software
27    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28    02111-1307, USA.
29
30    The GNU General Public License is contained in the file COPYING.
31 */
32
33 /* This file manages the data structures built by the debuginfo
34    system.  These are: the top level SegInfo list.  For each SegInfo,
35    there are tables for for address-to-symbol mappings,
36    address-to-src-file/line mappings, and address-to-CFI-info
37    mappings.
38 */
39
40 #include "pub_core_basics.h"
41 #include "pub_core_options.h"      /* VG_(clo_verbosity) */
42 #include "pub_core_debuginfo.h"
43 #include "pub_core_libcassert.h"
44 #include "pub_core_libcbase.h"
45 #include "pub_core_libcprint.h"
46 #include "pub_core_xarray.h"
47 #include "pub_core_oset.h"
48
49 #include "priv_misc.h"         /* dinfo_zalloc/free/strdup */
50 #include "priv_d3basics.h"     /* ML_(pp_GX) */
51 #include "priv_tytypes.h"
52 #include "priv_storage.h"      /* self */
53
54
55 /*------------------------------------------------------------*/
56 /*--- Misc (printing, errors)                              ---*/
57 /*------------------------------------------------------------*/
58
59 /* Show a non-fatal debug info reading error.  Use vg_panic if
60    terminal.  'serious' errors are shown regardless of the
61    verbosity setting. */
62 void ML_(symerr) ( struct _DebugInfo* di, Bool serious, HChar* msg )
63 {
64    /* XML mode hides everything :-( */
65    if (VG_(clo_xml))
66       return;
67
68    if (serious) {
69
70       VG_(message)(Vg_DebugMsg, "WARNING: Serious error when "
71                                 "reading debug info\n");
72       if (True || VG_(clo_verbosity) < 2) {
73          /* Need to show what the file name is, at verbosity levels 2
74             or below, since that won't already have been shown */
75          VG_(message)(Vg_DebugMsg, 
76                       "When reading debug info from %s:\n",
77                       (di && di->filename) ? di->filename : (UChar*)"???");
78       }
79       VG_(message)(Vg_DebugMsg, "%s\n", msg);
80
81    } else { /* !serious */
82
83       if (VG_(clo_verbosity) >= 2)
84          VG_(message)(Vg_DebugMsg, "%s\n", msg);
85
86    }
87 }
88
89
90 /* Print a symbol. */
91 void ML_(ppSym) ( Int idx, DiSym* sym )
92 {
93    VG_(printf)( "%5d:  %#8lx .. %#8lx (%d)      %s\n",
94                 idx,
95                 sym->addr, 
96                 sym->addr + sym->size - 1, sym->size,
97                 sym->name );
98 }
99
100 /* Print a call-frame-info summary. */
101 void ML_(ppDiCfSI) ( XArray* /* of CfiExpr */ exprs, DiCfSI* si )
102 {
103 #  define SHOW_HOW(_how, _off)                   \
104       do {                                       \
105          if (_how == CFIR_UNKNOWN) {             \
106             VG_(printf)("Unknown");              \
107          } else                                  \
108          if (_how == CFIR_SAME) {                \
109             VG_(printf)("Same");                 \
110          } else                                  \
111          if (_how == CFIR_CFAREL) {              \
112             VG_(printf)("cfa+%d", _off);         \
113          } else                                  \
114          if (_how == CFIR_MEMCFAREL) {           \
115             VG_(printf)("*(cfa+%d)", _off);      \
116          } else                                  \
117          if (_how == CFIR_EXPR) {                \
118             VG_(printf)("{");                    \
119             ML_(ppCfiExpr)(exprs, _off);         \
120             VG_(printf)("}");                    \
121          } else {                                \
122             vg_assert(0+0);                      \
123          }                                       \
124       } while (0)
125
126    VG_(printf)("[%#lx .. %#lx]: ", si->base,
127                                si->base + (UWord)si->len - 1);
128    switch (si->cfa_how) {
129       case CFIC_IA_SPREL: 
130          VG_(printf)("let cfa=oldSP+%d", si->cfa_off); 
131          break;
132       case CFIC_IA_BPREL: 
133          VG_(printf)("let cfa=oldBP+%d", si->cfa_off); 
134          break;
135       case CFIC_ARM_R13REL: 
136          VG_(printf)("let cfa=oldR13+%d", si->cfa_off); 
137          break;
138       case CFIC_ARM_R12REL: 
139          VG_(printf)("let cfa=oldR12+%d", si->cfa_off); 
140          break;
141       case CFIC_ARM_R11REL: 
142          VG_(printf)("let cfa=oldR11+%d", si->cfa_off); 
143          break;
144       case CFIR_SAME:
145          VG_(printf)("let cfa=Same");
146          break;
147       case CFIC_ARM_R7REL: 
148          VG_(printf)("let cfa=oldR7+%d", si->cfa_off); 
149          break;
150       case CFIC_EXPR: 
151          VG_(printf)("let cfa={"); 
152          ML_(ppCfiExpr)(exprs, si->cfa_off);
153          VG_(printf)("}"); 
154          break;
155       default: 
156          vg_assert(0);
157    }
158
159    VG_(printf)(" in RA=");
160    SHOW_HOW(si->ra_how, si->ra_off);
161 #  if defined(VGA_x86) || defined(VGA_amd64)
162    VG_(printf)(" SP=");
163    SHOW_HOW(si->sp_how, si->sp_off);
164    VG_(printf)(" BP=");
165    SHOW_HOW(si->bp_how, si->bp_off);
166 #  elif defined(VGA_arm)
167    VG_(printf)(" R14=");
168    SHOW_HOW(si->r14_how, si->r14_off);
169    VG_(printf)(" R13=");
170    SHOW_HOW(si->r13_how, si->r13_off);
171    VG_(printf)(" R12=");
172    SHOW_HOW(si->r12_how, si->r12_off);
173    VG_(printf)(" R11=");
174    SHOW_HOW(si->r11_how, si->r11_off);
175    VG_(printf)(" R7=");
176    SHOW_HOW(si->r7_how, si->r7_off);
177 #  elif defined(VGA_ppc32) || defined(VGA_ppc64)
178 #  elif defined(VGA_s390x)
179    VG_(printf)(" SP=");
180    SHOW_HOW(si->sp_how, si->sp_off);
181    VG_(printf)(" FP=");
182    SHOW_HOW(si->fp_how, si->fp_off);
183 #  else
184 #    error "Unknown arch"
185 #  endif
186    VG_(printf)("\n");
187 #  undef SHOW_HOW
188 }
189
190
191 /*------------------------------------------------------------*/
192 /*--- Adding stuff                                         ---*/
193 /*------------------------------------------------------------*/
194
195 /* Add a str to the string table, including terminating zero, and
196    return pointer to the string in vg_strtab.  Unless it's been seen
197    recently, in which case we find the old pointer and return that.
198    This avoids the most egregious duplications.
199
200    JSGF: changed from returning an index to a pointer, and changed to
201    a chunking memory allocator rather than reallocating, so the
202    pointers are stable.
203 */
204 UChar* ML_(addStr) ( struct _DebugInfo* di, UChar* str, Int len )
205 {
206    struct strchunk *chunk;
207    Int    space_needed;
208    UChar* p;
209
210    if (len == -1) {
211       len = VG_(strlen)(str);
212    } else {
213       vg_assert(len >= 0);
214    }
215
216    space_needed = 1 + len;
217
218    // Allocate a new strtab chunk if necessary
219    if (di->strchunks == NULL || 
220        (di->strchunks->strtab_used 
221         + space_needed) > SEGINFO_STRCHUNKSIZE) {
222       chunk = ML_(dinfo_zalloc)("di.storage.addStr.1", sizeof(*chunk));
223       chunk->strtab_used = 0;
224       chunk->next = di->strchunks;
225       di->strchunks = chunk;
226    }
227    chunk = di->strchunks;
228
229    p = &chunk->strtab[chunk->strtab_used];
230    VG_(memcpy)(p, str, len);
231    chunk->strtab[chunk->strtab_used+len] = '\0';
232    chunk->strtab_used += space_needed;
233
234    return p;
235 }
236
237
238 /* Add a symbol to the symbol table. 
239 */
240 void ML_(addSym) ( struct _DebugInfo* di, DiSym* sym )
241 {
242    UInt   new_sz, i;
243    DiSym* new_tab;
244
245    /* Ignore zero-sized syms. */
246    if (sym->size == 0) return;
247
248    if (di->symtab_used == di->symtab_size) {
249       new_sz = 2 * di->symtab_size;
250       if (new_sz == 0) new_sz = 500;
251       new_tab = ML_(dinfo_zalloc)( "di.storage.addSym.1", 
252                                    new_sz * sizeof(DiSym) );
253       if (di->symtab != NULL) {
254          for (i = 0; i < di->symtab_used; i++)
255             new_tab[i] = di->symtab[i];
256          ML_(dinfo_free)(di->symtab);
257       }
258       di->symtab = new_tab;
259       di->symtab_size = new_sz;
260    }
261
262    di->symtab[di->symtab_used] = *sym;
263    di->symtab_used++;
264    vg_assert(di->symtab_used <= di->symtab_size);
265 }
266
267
268 /* Resize the symbol table to save memory.
269 */
270 void ML_(shrinkSym)( struct _DebugInfo* di )
271 {
272    DiSym* new_tab;
273    UInt new_sz = di->symtab_used;
274    if (new_sz == di->symtab_size) return;
275
276    new_tab = ML_(dinfo_zalloc)( "di.storage.shrinkSym", 
277                                 new_sz * sizeof(DiSym) );
278    VG_(memcpy)(new_tab, di->symtab, new_sz * sizeof(DiSym));
279
280    ML_(dinfo_free)(di->symtab);
281    di->symtab = new_tab;
282    di->symtab_size = new_sz;
283 }
284
285
286 /* Add a location to the location table. 
287 */
288 static void addLoc ( struct _DebugInfo* di, DiLoc* loc )
289 {
290    UInt   new_sz, i;
291    DiLoc* new_tab;
292
293    /* Zero-sized locs should have been ignored earlier */
294    vg_assert(loc->size > 0);
295
296    if (di->loctab_used == di->loctab_size) {
297       new_sz = 2 * di->loctab_size;
298       if (new_sz == 0) new_sz = 500;
299       new_tab = ML_(dinfo_zalloc)( "di.storage.addLoc.1",
300                                    new_sz * sizeof(DiLoc) );
301       if (di->loctab != NULL) {
302          for (i = 0; i < di->loctab_used; i++)
303             new_tab[i] = di->loctab[i];
304          ML_(dinfo_free)(di->loctab);
305       }
306       di->loctab = new_tab;
307       di->loctab_size = new_sz;
308    }
309
310    di->loctab[di->loctab_used] = *loc;
311    di->loctab_used++;
312    vg_assert(di->loctab_used <= di->loctab_size);
313 }
314
315
316 /* Resize the lineinfo table to save memory.
317 */
318 void ML_(shrinkLineInfo)( struct _DebugInfo* di )
319 {
320    DiLoc* new_tab;
321    UInt new_sz = di->loctab_used;
322    if (new_sz == di->loctab_size) return;
323
324    new_tab = ML_(dinfo_zalloc)( "di.storage.shrinkLineInfo", 
325                                 new_sz * sizeof(DiLoc) );
326    VG_(memcpy)(new_tab, di->loctab, new_sz * sizeof(DiLoc));
327
328    ML_(dinfo_free)(di->loctab);
329    di->loctab = new_tab;
330    di->loctab_size = new_sz;
331 }
332
333
334 /* Top-level place to call to add a source-location mapping entry.
335 */
336 void ML_(addLineInfo) ( struct _DebugInfo* di,
337                         UChar*   filename,
338                         UChar*   dirname, /* NULL == directory is unknown */
339                         Addr     this,
340                         Addr     next,
341                         Int      lineno,
342                         Int      entry /* only needed for debug printing */
343      )
344 {
345    static const Bool debug = False;
346    DiLoc loc;
347    Int size = next - this;
348
349    /* Ignore zero-sized locs */
350    if (this == next) return;
351
352    if (debug)
353       VG_(printf)( "  src %s %s line %d %#lx-%#lx\n",
354                    dirname ? dirname : (UChar*)"(unknown)",
355                    filename, lineno, this, next );
356
357    /* Maximum sanity checking.  Some versions of GNU as do a shabby
358     * job with stabs entries; if anything looks suspicious, revert to
359     * a size of 1.  This should catch the instruction of interest
360     * (since if using asm-level debug info, one instruction will
361     * correspond to one line, unlike with C-level debug info where
362     * multiple instructions can map to the one line), but avoid
363     * catching any other instructions bogusly. */
364    if (this > next) {
365        if (VG_(clo_verbosity) > 2) {
366            VG_(message)(Vg_DebugMsg, 
367                         "warning: line info addresses out of order "
368                         "at entry %d: 0x%lx 0x%lx\n", entry, this, next);
369        }
370        size = 1;
371    }
372
373    if (size > MAX_LOC_SIZE) {
374        if (0)
375        VG_(message)(Vg_DebugMsg, 
376                     "warning: line info address range too large "
377                     "at entry %d: %d\n", entry, size);
378        size = 1;
379    }
380
381    /* Rule out ones which are completely outside the r-x mapped area.
382       See "Comment_Regarding_Text_Range_Checks" elsewhere in this file
383       for background and rationale. */
384    vg_assert(di->have_rx_map && di->have_rw_map);
385    if (next-1 < di->rx_map_avma
386        || this >= di->rx_map_avma + di->rx_map_size ) {
387        if (0)
388           VG_(message)(Vg_DebugMsg, 
389                        "warning: ignoring line info entry falling "
390                        "outside current DebugInfo: %#lx %#lx %#lx %#lx\n",
391                        di->text_avma, 
392                        di->text_avma + di->text_size, 
393                        this, next-1);
394        return;
395    }
396
397    vg_assert(lineno >= 0);
398    if (lineno > MAX_LINENO) {
399       static Bool complained = False;
400       if (!complained) {
401          complained = True;
402          VG_(message)(Vg_UserMsg, 
403                       "warning: ignoring line info entry with "
404                       "huge line number (%d)\n", lineno);
405          VG_(message)(Vg_UserMsg, 
406                       "         Can't handle line numbers "
407                       "greater than %d, sorry\n", MAX_LINENO);
408          VG_(message)(Vg_UserMsg, 
409                       "(Nb: this message is only shown once)\n");
410       }
411       return;
412    }
413
414    loc.addr      = this;
415    loc.size      = (UShort)size;
416    loc.lineno    = lineno;
417    loc.filename  = filename;
418    loc.dirname   = dirname;
419
420    if (0) VG_(message)(Vg_DebugMsg, 
421                        "addLoc: addr %#lx, size %d, line %d, file %s\n",
422                        this,size,lineno,filename);
423
424    addLoc ( di, &loc );
425 }
426
427
428 /* Top-level place to call to add a CFI summary record.  The supplied
429    DiCfSI is copied. */
430 void ML_(addDiCfSI) ( struct _DebugInfo* di, DiCfSI* cfsi_orig )
431 {
432    static const Bool debug = False;
433    UInt    new_sz, i;
434    DiCfSI* new_tab;
435    SSizeT  delta;
436
437    /* copy the original, so we can mess with it */
438    DiCfSI cfsi = *cfsi_orig;
439
440    if (debug) {
441       VG_(printf)("adding DiCfSI: ");
442       ML_(ppDiCfSI)(di->cfsi_exprs, &cfsi);
443    }
444
445    /* sanity */
446    vg_assert(cfsi.len > 0);
447    /* If this fails, the implication is you have a single procedure
448       with more than 5 million bytes of code.  Which is pretty
449       unlikely.  Either that, or the debuginfo reader is somehow
450       broken.  5 million is of course arbitrary; but it's big enough
451       to be bigger than the size of any plausible piece of code that
452       would fall within a single procedure. */
453    vg_assert(cfsi.len < 5000000);
454
455    vg_assert(di->have_rx_map && di->have_rw_map);
456    /* If we have an empty r-x mapping (is that possible?) then the
457       DiCfSI can't possibly fall inside it.  In which case skip. */
458    if (di->rx_map_size == 0)
459       return;
460
461    /* Rule out ones which are completely outside the r-x mapped area.
462       See "Comment_Regarding_Text_Range_Checks" elsewhere in this file
463       for background and rationale. */
464    if (cfsi.base + cfsi.len - 1 < di->rx_map_avma
465        || cfsi.base >= di->rx_map_avma + di->rx_map_size) {
466       static Int complaints = 10;
467       if (VG_(clo_trace_cfi) || complaints > 0) {
468          complaints--;
469          if (VG_(clo_verbosity) > 1) {
470             VG_(message)(
471                Vg_DebugMsg,
472                "warning: DiCfSI %#lx .. %#lx outside segment %#lx .. %#lx\n",
473                cfsi.base, 
474                cfsi.base + cfsi.len - 1,
475                di->text_avma,
476                di->text_avma + di->text_size - 1 
477             );
478          }
479          if (VG_(clo_trace_cfi)) 
480             ML_(ppDiCfSI)(di->cfsi_exprs, &cfsi);
481       }
482       return;
483    }
484
485    /* Now we know the range is at least partially inside the r-x
486       mapped area.  That implies that at least one of the ends of the
487       range falls inside the area.  If necessary, clip it so it is
488       completely within the area.  If we don't do this,
489       check_CFSI_related_invariants() in debuginfo.c (invariant #2)
490       will fail.  See
491       "Comment_on_IMPORTANT_CFSI_REPRESENTATIONAL_INVARIANTS" in
492       priv_storage.h for background. */
493    if (cfsi.base < di->rx_map_avma) {
494       /* Lower end is outside the mapped area.  Hence upper end must
495          be inside it. */
496       if (0) VG_(printf)("XXX truncate lower\n");
497       vg_assert(cfsi.base + cfsi.len - 1 >= di->rx_map_avma);
498       delta = (SSizeT)(di->rx_map_avma - cfsi.base);
499       vg_assert(delta > 0);
500       vg_assert(delta < (SSizeT)cfsi.len);
501       cfsi.base += delta;
502       cfsi.len -= delta;
503    }
504    else
505    if (cfsi.base + cfsi.len - 1 > di->rx_map_avma + di->rx_map_size - 1) {
506       /* Upper end is outside the mapped area.  Hence lower end must be
507          inside it. */
508       if (0) VG_(printf)("XXX truncate upper\n");
509       vg_assert(cfsi.base <= di->rx_map_avma + di->rx_map_size - 1);
510       delta = (SSizeT)( (cfsi.base + cfsi.len - 1) 
511                         - (di->rx_map_avma + di->rx_map_size - 1) );
512       vg_assert(delta > 0); vg_assert(delta < (SSizeT)cfsi.len);
513       cfsi.len -= delta;
514    }
515
516    /* Final checks */
517
518    /* Because: either cfsi was entirely inside the range, in which
519       case we asserted that len > 0 at the start, OR it fell partially
520       inside the range, in which case we reduced it by some size
521       (delta) which is < its original size. */
522    vg_assert(cfsi.len > 0);
523
524    /* Similar logic applies for the next two assertions. */
525    vg_assert(cfsi.base >= di->rx_map_avma);
526    vg_assert(cfsi.base + cfsi.len - 1
527              <= di->rx_map_avma + di->rx_map_size - 1);
528
529    if (di->cfsi_used == di->cfsi_size) {
530       new_sz = 2 * di->cfsi_size;
531       if (new_sz == 0) new_sz = 20;
532       new_tab = ML_(dinfo_zalloc)( "di.storage.addDiCfSI.1",
533                                    new_sz * sizeof(DiCfSI) );
534       if (di->cfsi != NULL) {
535          for (i = 0; i < di->cfsi_used; i++)
536             new_tab[i] = di->cfsi[i];
537          ML_(dinfo_free)(di->cfsi);
538       }
539       di->cfsi = new_tab;
540       di->cfsi_size = new_sz;
541    }
542
543    di->cfsi[di->cfsi_used] = cfsi;
544    di->cfsi_used++;
545    vg_assert(di->cfsi_used <= di->cfsi_size);
546 }
547
548
549 Int ML_(CfiExpr_Undef)( XArray* dst )
550 {
551    CfiExpr e;
552    VG_(memset)( &e, 0, sizeof(e) );
553    e.tag = Cex_Undef;
554    return (Int)VG_(addToXA)( dst, &e );
555 }
556 Int ML_(CfiExpr_Deref)( XArray* dst, Int ixAddr )
557 {
558    CfiExpr e;
559    VG_(memset)( &e, 0, sizeof(e) );
560    e.tag = Cex_Deref;
561    e.Cex.Deref.ixAddr = ixAddr;
562    return (Int)VG_(addToXA)( dst, &e );
563 }
564 Int ML_(CfiExpr_Const)( XArray* dst, UWord con )
565 {
566    CfiExpr e;
567    VG_(memset)( &e, 0, sizeof(e) );
568    e.tag = Cex_Const;
569    e.Cex.Const.con = con;
570    return (Int)VG_(addToXA)( dst, &e );
571 }
572 Int ML_(CfiExpr_Binop)( XArray* dst, CfiOp op, Int ixL, Int ixR )
573 {
574    CfiExpr e;
575    VG_(memset)( &e, 0, sizeof(e) );
576    e.tag = Cex_Binop;
577    e.Cex.Binop.op  = op;
578    e.Cex.Binop.ixL = ixL;
579    e.Cex.Binop.ixR = ixR;
580    return (Int)VG_(addToXA)( dst, &e );
581 }
582 Int ML_(CfiExpr_CfiReg)( XArray* dst, CfiReg reg )
583 {
584    CfiExpr e;
585    VG_(memset)( &e, 0, sizeof(e) );
586    e.tag = Cex_CfiReg;
587    e.Cex.CfiReg.reg = reg;
588    return (Int)VG_(addToXA)( dst, &e );
589 }
590 Int ML_(CfiExpr_DwReg)( XArray* dst, Int reg )
591 {
592    CfiExpr e;
593    VG_(memset)( &e, 0, sizeof(e) );
594    e.tag = Cex_DwReg;
595    e.Cex.DwReg.reg = reg;
596    return (Int)VG_(addToXA)( dst, &e );
597 }
598
599 static void ppCfiOp ( CfiOp op ) 
600 {
601    switch (op) {
602       case Cop_Add: VG_(printf)("+"); break;
603       case Cop_Sub: VG_(printf)("-"); break;
604       case Cop_And: VG_(printf)("&"); break;
605       case Cop_Mul: VG_(printf)("*"); break;
606       default:      vg_assert(0);
607    }
608 }
609
610 static void ppCfiReg ( CfiReg reg )
611 {
612    switch (reg) {
613       case Creg_IA_SP:   VG_(printf)("xSP"); break;
614       case Creg_IA_BP:   VG_(printf)("xBP"); break;
615       case Creg_IA_IP:   VG_(printf)("xIP"); break;
616       case Creg_ARM_R13: VG_(printf)("R13"); break;
617       case Creg_ARM_R12: VG_(printf)("R12"); break;
618       case Creg_ARM_R15: VG_(printf)("R15"); break;
619       case Creg_ARM_R14: VG_(printf)("R14"); break;
620       default: vg_assert(0);
621    }
622 }
623
624 void ML_(ppCfiExpr)( XArray* src, Int ix )
625 {
626    /* VG_(indexXA) checks for invalid src/ix values, so we can
627       use it indiscriminately. */
628    CfiExpr* e = (CfiExpr*) VG_(indexXA)( src, ix );
629    switch (e->tag) {
630       case Cex_Undef: 
631          VG_(printf)("Undef"); 
632          break;
633       case Cex_Deref: 
634          VG_(printf)("*("); 
635          ML_(ppCfiExpr)(src, e->Cex.Deref.ixAddr);
636          VG_(printf)(")"); 
637          break;
638       case Cex_Const: 
639          VG_(printf)("0x%lx", e->Cex.Const.con); 
640          break;
641       case Cex_Binop: 
642          VG_(printf)("(");
643          ML_(ppCfiExpr)(src, e->Cex.Binop.ixL);
644          VG_(printf)(")");
645          ppCfiOp(e->Cex.Binop.op);
646          VG_(printf)("(");
647          ML_(ppCfiExpr)(src, e->Cex.Binop.ixR);
648          VG_(printf)(")");
649          break;
650       case Cex_CfiReg:
651          ppCfiReg(e->Cex.CfiReg.reg);
652          break;
653       case Cex_DwReg:
654          VG_(printf)("dwr%d", e->Cex.DwReg.reg);
655          break;
656       default: 
657          VG_(core_panic)("ML_(ppCfiExpr)"); 
658          /*NOTREACHED*/
659          break;
660    }
661 }
662
663
664 Word ML_(cmp_for_DiAddrRange_range) ( const void* keyV,
665                                       const void* elemV ) {
666    const Addr* key = (const Addr*)keyV;
667    const DiAddrRange* elem = (const DiAddrRange*)elemV;
668    if (0)
669       VG_(printf)("cmp_for_DiAddrRange_range: %#lx vs %#lx\n",
670                   *key, elem->aMin);
671    if ((*key) < elem->aMin) return -1;
672    if ((*key) > elem->aMax) return 1;
673    return 0;
674 }
675
676 static
677 void show_scope ( OSet* /* of DiAddrRange */ scope, HChar* who )
678 {
679    DiAddrRange* range;
680    VG_(printf)("Scope \"%s\" = {\n", who);
681    VG_(OSetGen_ResetIter)( scope );
682    while (True) {
683       range = VG_(OSetGen_Next)( scope );
684       if (!range) break;
685       VG_(printf)("   %#lx .. %#lx: %lu vars\n", range->aMin, range->aMax,
686                   range->vars ? VG_(sizeXA)(range->vars) : 0);
687    }
688    VG_(printf)("}\n");
689 }
690
691 /* Add the variable 'var' to 'scope' for the address range [aMin,aMax]
692    (inclusive of aMin and aMax).  Split existing ranges as required if
693    aMin or aMax or both don't match existing range boundaries, and add
694    'var' to all required ranges.  Take great care to preserve the
695    invariant that the ranges in 'scope' cover the entire address range
696    exactly once, with no overlaps and no holes. */
697 static void add_var_to_arange ( 
698                /*MOD*/OSet* /* of DiAddrRange */ scope,
699                Addr aMin, 
700                Addr aMax,
701                DiVariable* var
702             )
703 {
704    DiAddrRange *first, *last, *range;
705    /* These xx variables are for assertion checking only; they don't
706       contribute anything to the actual work of this function. */
707    DiAddrRange *xxRangep, *xxFirst, *xxLast;
708    UWord       xxIters;
709
710    vg_assert(aMin <= aMax);
711
712    if (0) VG_(printf)("add_var_to_arange: %#lx .. %#lx\n", aMin, aMax);
713    if (0) show_scope( scope, "add_var_to_arange(1)" );
714
715    /* See if the lower end of the range (aMin) falls exactly on an
716       existing range boundary.  If not, find the range it does fall
717       into, and split it (copying the variables in the process), so
718       that aMin does exactly fall on a range boundary. */
719    first = VG_(OSetGen_Lookup)( scope, &aMin );
720    /* It must be present, since the presented OSet must cover
721       the entire address range. */
722    vg_assert(first);
723    vg_assert(first->aMin <= first->aMax);
724    vg_assert(first->aMin <= aMin && aMin <= first->aMax);
725
726    /* Fast track common case, which is that the range specified for
727       the variable exactly coincides with one already-existing
728       range. */
729    if (first->aMin == aMin && first->aMax == aMax) {
730       vg_assert(first->vars);
731       VG_(addToXA)( first->vars, var );
732       return;
733    }
734
735    /* We have to get into splitting ranges, which is complex
736       and slow. */
737    if (first->aMin < aMin) {
738       DiAddrRange* nyu;
739       /* Ok.  We'll have to split 'first'. */
740       /* truncate the upper end of 'first' */
741       Addr tmp = first->aMax;
742       first->aMax = aMin-1;
743       vg_assert(first->aMin <= first->aMax);
744       /* create a new range */
745       nyu = VG_(OSetGen_AllocNode)( scope, sizeof(DiAddrRange) );
746       vg_assert(nyu);
747       nyu->aMin = aMin;
748       nyu->aMax = tmp;
749       vg_assert(nyu->aMin <= nyu->aMax);
750       /* copy vars into it */
751       vg_assert(first->vars);
752       nyu->vars = VG_(cloneXA)( "di.storage.avta.1", first->vars );
753       vg_assert(nyu->vars);
754       VG_(OSetGen_Insert)( scope, nyu );
755       first = nyu;
756    }
757
758    vg_assert(first->aMin == aMin);
759
760    /* Now do exactly the same for the upper end (aMax): if it doesn't
761       fall on a boundary, cause it to do so by splitting the range it
762       does currently fall into. */
763    last = VG_(OSetGen_Lookup)( scope, &aMax );
764    vg_assert(last->aMin <= last->aMax);
765    vg_assert(last->aMin <= aMax && aMax <= last->aMax);
766
767    if (aMax < last->aMax) {
768       DiAddrRange* nyu;
769       /* We have to split 'last'. */
770       /* truncate the lower end of 'last' */
771       Addr tmp = last->aMin;
772       last->aMin = aMax+1;
773       vg_assert(last->aMin <= last->aMax);
774       /* create a new range */
775       nyu = VG_(OSetGen_AllocNode)( scope, sizeof(DiAddrRange) );
776       vg_assert(nyu);
777       nyu->aMin = tmp;
778       nyu->aMax = aMax;
779       vg_assert(nyu->aMin <= nyu->aMax);
780       /* copy vars into it */
781       vg_assert(last->vars);
782       nyu->vars = VG_(cloneXA)( "di.storage.avta.2", last->vars );
783       vg_assert(nyu->vars);
784       VG_(OSetGen_Insert)( scope, nyu );
785       last = nyu;
786    }
787
788    vg_assert(aMax == last->aMax);
789
790    xxFirst = (DiAddrRange*)VG_(OSetGen_Lookup)(scope, &aMin);
791    xxLast  = (DiAddrRange*)VG_(OSetGen_Lookup)(scope, &aMax);
792    vg_assert(xxFirst);
793    vg_assert(xxLast);
794    vg_assert(xxFirst->aMin == aMin);
795    vg_assert(xxLast->aMax == aMax);
796    if (xxFirst != xxLast)
797       vg_assert(xxFirst->aMax < xxLast->aMin);
798
799    /* Great.  Now we merely need to iterate over the segments from
800       'first' to 'last' inclusive, and add 'var' to the variable set
801       of each of them. */
802    if (0) {
803       static UWord ctr = 0;
804       ctr++;
805       VG_(printf)("ctr = %lu\n", ctr);
806       if (ctr >= 33263) show_scope( scope, "add_var_to_arange(2)" );
807    }
808
809    xxIters = 0;
810    range = xxRangep = NULL;
811    VG_(OSetGen_ResetIterAt)( scope, &aMin );
812    while (True) {
813       xxRangep = range;
814       range    = VG_(OSetGen_Next)( scope );
815       if (!range) break;
816       if (range->aMin > aMax) break;
817       xxIters++;
818       if (0) VG_(printf)("have range %#lx %#lx\n",
819                          range->aMin, range->aMax);
820
821       /* Sanity checks */
822       if (!xxRangep) {
823          /* This is the first in the range */
824          vg_assert(range->aMin == aMin);
825       } else {
826          vg_assert(xxRangep->aMax + 1 == range->aMin);
827       }
828
829       vg_assert(range->vars);
830       VG_(addToXA)( range->vars, var );
831    }
832    /* Done.  We should have seen at least one range. */
833    vg_assert(xxIters >= 1);
834    if (xxIters == 1) vg_assert(xxFirst == xxLast);
835    if (xxFirst == xxLast) vg_assert(xxIters == 1);
836    vg_assert(xxRangep);
837    vg_assert(xxRangep->aMax == aMax);
838    vg_assert(xxRangep == xxLast);
839 }
840
841
842 /* Top-level place to call to add a variable description (as extracted
843    from a DWARF3 .debug_info section. */
844 void ML_(addVar)( struct _DebugInfo* di,
845                   Int    level,
846                   Addr   aMin,
847                   Addr   aMax,
848                   UChar* name, /* in di's .strchunks */
849                   UWord  typeR, /* a cuOff */
850                   GExpr* gexpr,
851                   GExpr* fbGX,
852                   UChar* fileName, /* where decl'd - may be NULL.
853                                       in di's .strchunks */
854                   Int    lineNo, /* where decl'd - may be zero */
855                   Bool   show )
856 {
857    OSet* /* of DiAddrRange */ scope;
858    DiVariable var;
859    Bool       all;
860    TyEnt*     ent;
861    MaybeULong mul;
862    HChar*     badness;
863
864    tl_assert(di && di->admin_tyents);
865
866    if (0) {
867       VG_(printf)("  ML_(addVar): level %d  %#lx-%#lx  %s :: ",
868                   level, aMin, aMax, name );
869       ML_(pp_TyEnt_C_ishly)( di->admin_tyents, typeR );
870       VG_(printf)("\n  Var=");
871       ML_(pp_GX)(gexpr);
872       VG_(printf)("\n");
873       if (fbGX) {
874          VG_(printf)("  FrB=");
875          ML_(pp_GX)( fbGX );
876          VG_(printf)("\n");
877       } else {
878          VG_(printf)("  FrB=none\n");
879       }
880       VG_(printf)("\n");
881    }
882
883    vg_assert(level >= 0);
884    vg_assert(aMin <= aMax);
885    vg_assert(name);
886    vg_assert(gexpr);
887
888    ent = ML_(TyEnts__index_by_cuOff)( di->admin_tyents, NULL, typeR);
889    tl_assert(ent);
890    vg_assert(ML_(TyEnt__is_type)(ent));
891
892    /* "Comment_Regarding_Text_Range_Checks" (is referred to elsewhere)
893       ----------------------------------------------------------------
894       Ignore any variables whose aMin .. aMax (that is, range of text
895       addresses for which they actually exist) falls outside the text
896       segment.  Is this indicative of a bug in the reader?  Maybe.
897       (LATER): instead of restricting strictly to the .text segment,
898       be a bit more relaxed, and accept any variable whose text range
899       falls inside the r-x mapped area.  This is useful because .text
900       is not always the only instruction-carrying segment: others are:
901       .init .plt __libc_freeres_fn and .fini.  This implicitly assumes
902       that those extra sections have the same bias as .text, but that
903       seems a reasonable assumption to me. */
904    /* This is assured us by top level steering logic in debuginfo.c,
905       and it is re-checked at the start of
906       ML_(read_elf_debug_info). */
907    vg_assert(di->have_rx_map && di->have_rw_map);
908    if (level > 0
909        && (aMax < di->rx_map_avma
910            || aMin >= di->rx_map_avma + di->rx_map_size)) {
911       if (VG_(clo_verbosity) >= 0) {
912          VG_(message)(Vg_DebugMsg, 
913             "warning: addVar: in range %#lx .. %#lx outside "
914             "segment %#lx .. %#lx (%s)\n",
915             aMin, aMax,
916             di->text_avma, di->text_avma + di->text_size -1,
917             name
918          );
919       }
920       return;
921    }
922
923    /* If the type's size is zero (which can mean unknown size), ignore
924       it.  We will never be able to actually relate a data address to
925       a data object with zero size, so there's no point in storing
926       info on it.  On 32-bit platforms, also reject types whose size
927       is 2^32 bytes or large.  (It's amazing what junk shows up ..) */
928    mul = ML_(sizeOfType)(di->admin_tyents, typeR);
929
930    badness = NULL;
931    if (mul.b != True) 
932       badness = "unknown size";
933    else if (mul.ul == 0)
934       badness = "zero size   ";
935    else if (sizeof(void*) == 4 && mul.ul >= (1ULL<<32))
936       badness = "implausibly large";
937
938    if (badness) {
939       static Int complaints = 10;
940       if (VG_(clo_verbosity) >= 2 && complaints > 0) {
941          VG_(message)(Vg_DebugMsg, "warning: addVar: %s (%s)\n",
942                                    badness, name );
943          complaints--;
944       }
945       return;
946    }
947
948    if (!di->varinfo) {
949       di->varinfo = VG_(newXA)( ML_(dinfo_zalloc), 
950                                 "di.storage.addVar.1",
951                                 ML_(dinfo_free),
952                                 sizeof(OSet*) );
953    }
954
955    vg_assert(level < 256); /* arbitrary; stay sane */
956    /* Expand the top level array enough to map this level */
957    while ( VG_(sizeXA)(di->varinfo) <= level ) {
958       DiAddrRange* nyu;
959       scope = VG_(OSetGen_Create)( offsetof(DiAddrRange,aMin), 
960                                    ML_(cmp_for_DiAddrRange_range),
961                                    ML_(dinfo_zalloc), "di.storage.addVar.2",
962                                    ML_(dinfo_free) );
963       vg_assert(scope);
964       if (0) VG_(printf)("create: scope = %p, adding at %ld\n",
965                          scope, VG_(sizeXA)(di->varinfo));
966       VG_(addToXA)( di->varinfo, &scope );
967       /* Add a single range covering the entire address space.  At
968          level 0 we require this doesn't get split.  At levels above 0
969          we require that any additions to it cause it to get split.
970          All of these invariants get checked both add_var_to_arange
971          and after reading is complete, in canonicaliseVarInfo. */
972       nyu = VG_(OSetGen_AllocNode)( scope, sizeof(DiAddrRange) );
973       vg_assert(nyu);
974       nyu->aMin = (Addr)0;
975       nyu->aMax = ~(Addr)0;
976       nyu->vars = VG_(newXA)( ML_(dinfo_zalloc), "di.storage.addVar.3",
977                               ML_(dinfo_free),
978                               sizeof(DiVariable) );
979       vg_assert(nyu->vars);
980       VG_(OSetGen_Insert)( scope, nyu );
981    }
982
983    vg_assert( VG_(sizeXA)(di->varinfo) > level );
984    scope = *(OSet**)VG_(indexXA)( di->varinfo, level );
985    vg_assert(scope);
986
987    var.name     = name;
988    var.typeR    = typeR;
989    var.gexpr    = gexpr;
990    var.fbGX     = fbGX;
991    var.fileName = fileName;
992    var.lineNo   = lineNo;
993
994    all = aMin == (Addr)0 && aMax == ~(Addr)0;
995    vg_assert(level == 0 ? all : !all);
996
997    add_var_to_arange( /*MOD*/scope, aMin, aMax, &var );
998 }
999
1000
1001 /* This really just checks the constructed data structure, as there is
1002    no canonicalisation to do. */
1003 static void canonicaliseVarInfo ( struct _DebugInfo* di )
1004 {
1005    Word i, nInThisScope;
1006
1007    if (!di->varinfo)
1008       return;
1009
1010    for (i = 0; i < VG_(sizeXA)(di->varinfo); i++) {
1011
1012       DiAddrRange *range, *rangep;
1013       OSet* scope = *(OSet**)VG_(indexXA)(di->varinfo, i);
1014       if (!scope) continue;
1015
1016       /* Deal with the global-scope case. */
1017       if (i == 0) {
1018          Addr zero = 0;
1019          vg_assert(VG_(OSetGen_Size)( scope ) == 1);
1020          range = VG_(OSetGen_Lookup)( scope, &zero );
1021          vg_assert(range);
1022          vg_assert(range->aMin == (Addr)0);
1023          vg_assert(range->aMax == ~(Addr)0);
1024          continue;
1025       }
1026
1027       /* All the rest of this is for the local-scope case. */
1028       /* iterate over all entries in 'scope' */
1029       nInThisScope = 0;
1030       rangep = NULL;
1031       VG_(OSetGen_ResetIter)(scope);
1032       while (True) {
1033          range = VG_(OSetGen_Next)(scope);
1034          if (!range) {
1035            /* We just saw the last one.  There must have been at
1036               least one entry in the range. */
1037            vg_assert(rangep);
1038            vg_assert(rangep->aMax == ~(Addr)0);
1039            break;
1040          }
1041
1042          vg_assert(range->aMin <= range->aMax);
1043          vg_assert(range->vars);
1044
1045          if (!rangep) {
1046            /* This is the first entry in the range. */
1047            vg_assert(range->aMin == 0);
1048          } else {
1049            vg_assert(rangep->aMax + 1 == range->aMin);
1050          }
1051
1052          rangep = range;
1053          nInThisScope++;
1054       } /* iterating over ranges in a given scope */
1055
1056       /* If there's only one entry in this (local) scope, it must
1057          cover the entire address space (obviously), but it must not
1058          contain any vars. */
1059
1060       vg_assert(nInThisScope > 0);
1061       if (nInThisScope == 1) {
1062          Addr zero = 0;
1063          vg_assert(VG_(OSetGen_Size)( scope ) == 1);
1064          range = VG_(OSetGen_Lookup)( scope, &zero );
1065          vg_assert(range);
1066          vg_assert(range->aMin == (Addr)0);
1067          vg_assert(range->aMax == ~(Addr)0);
1068          vg_assert(range->vars);
1069          vg_assert(VG_(sizeXA)(range->vars) == 0);
1070       }
1071
1072    } /* iterate over scopes */
1073 }
1074
1075
1076 /*------------------------------------------------------------*/
1077 /*--- Canonicalisers                                       ---*/
1078 /*------------------------------------------------------------*/
1079
1080 /* Sort the symtab by starting address, and emit warnings if any
1081    symbols have overlapping address ranges.  We use that old chestnut,
1082    shellsort.  Mash the table around so as to establish the property
1083    that addresses are in order and the ranges to not overlap.  This
1084    facilitates using binary search to map addresses to symbols when we
1085    come to query the table.
1086 */
1087 static Int compare_DiSym ( void* va, void* vb ) 
1088 {
1089    DiSym* a = (DiSym*)va;
1090    DiSym* b = (DiSym*)vb;
1091    if (a->addr < b->addr) return -1;
1092    if (a->addr > b->addr) return  1;
1093    return 0;
1094 }
1095
1096
1097 /* Two symbols have the same address.  Which name do we prefer?  In order:
1098
1099    - Prefer "PMPI_<foo>" over "MPI_<foo>".
1100
1101    - Else, prefer a non-NULL name over a NULL one.
1102
1103    - Else, prefer a non-whitespace name over an all-whitespace name.
1104
1105    - Else, prefer the shorter symbol name.  If the symbol contains a
1106      version symbol ('@' on Linux, other platforms may differ), which means it
1107      is versioned, then the length up to the version symbol is used for length
1108      comparison purposes (so "foo@GLIBC_2.4.2" is considered shorter than
1109      "foobar"). 
1110      
1111    - Else, if two symbols have the same length, prefer a versioned symbol over
1112      a non-versioned symbol.
1113      
1114    - Else, use alphabetical ordering.
1115
1116    - Otherwise, they must be the same;  use the symbol with the lower address.
1117
1118    Very occasionally this goes wrong (eg. 'memcmp' and 'bcmp' are
1119    aliases in glibc, we choose the 'bcmp' symbol because it's shorter,
1120    so we can misdescribe memcmp() as bcmp()).  This is hard to avoid.
1121    It's mentioned in the FAQ file.
1122  */
1123 static DiSym* prefersym ( struct _DebugInfo* di, DiSym* a, DiSym* b )
1124 {
1125    Word cmp;
1126    Word vlena, vlenb;           /* length without version */
1127    const UChar *vpa, *vpb;
1128
1129    Bool preferA = False;
1130    Bool preferB = False;
1131
1132    vg_assert(a->addr == b->addr);
1133
1134    vlena = VG_(strlen)(a->name);
1135    vlenb = VG_(strlen)(b->name);
1136
1137 #if defined(VGO_linux) || defined(VGO_aix5) || defined(VGO_l4re)
1138 #  define VERSION_CHAR '@'
1139 #elif defined(VGO_darwin)
1140 #  define VERSION_CHAR '$'
1141 #else
1142 #  error Unknown OS
1143 #endif
1144
1145    vpa = VG_(strchr)(a->name, VERSION_CHAR);
1146    vpb = VG_(strchr)(b->name, VERSION_CHAR);
1147
1148    if (vpa)
1149       vlena = vpa - a->name;
1150    if (vpb)
1151       vlenb = vpb - b->name;
1152
1153    /* MPI hack: prefer PMPI_Foo over MPI_Foo */
1154    if (0==VG_(strncmp)(a->name, "MPI_", 4)
1155        && 0==VG_(strncmp)(b->name, "PMPI_", 5)
1156        && 0==VG_(strcmp)(a->name, 1+b->name)) {
1157       preferB = True; goto out;
1158    } 
1159    if (0==VG_(strncmp)(b->name, "MPI_", 4)
1160        && 0==VG_(strncmp)(a->name, "PMPI_", 5)
1161        && 0==VG_(strcmp)(b->name, 1+a->name)) {
1162       preferA = True; goto out;
1163    }
1164
1165    /* Prefer non-empty name. */
1166    if (vlena  &&  !vlenb) {
1167       preferA = True; goto out;
1168    }
1169    if (vlenb  &&  !vlena) {
1170       preferB = True; goto out;
1171    }
1172
1173    /* Prefer non-whitespace name. */
1174    {
1175       Bool blankA = True;
1176       Bool blankB = True;
1177       Char *s;
1178       s = a->name;
1179       while (*s) {
1180          if (!VG_(isspace)(*s++)) {
1181             blankA = False;
1182             break;
1183          }
1184       }
1185       s = b->name;
1186       while (*s) {
1187          if (!VG_(isspace)(*s++)) {
1188             blankB = False;
1189             break;
1190          }
1191       }
1192
1193       if (!blankA  &&  blankB) {
1194          preferA = True; goto out;
1195       }
1196       if (!blankB  &&  blankA) {
1197          preferB = True; goto out;
1198       }
1199    }
1200
1201    /* Select the shortest unversioned name */
1202    if (vlena < vlenb) {
1203       preferA = True; goto out;
1204    } 
1205    if (vlenb < vlena) {
1206       preferB = True; goto out;
1207    }
1208
1209    /* Equal lengths; select the versioned name */
1210    if (vpa && !vpb) {
1211       preferA = True; goto out;
1212    }
1213    if (vpb && !vpa) {
1214       preferB = True; goto out;
1215    }
1216
1217    /* Either both versioned or neither is versioned; select them
1218       alphabetically */
1219    cmp = VG_(strcmp)(a->name, b->name);
1220    if (cmp < 0) {
1221       preferA = True; goto out;
1222    }
1223    if (cmp > 0) {
1224       preferB = True; goto out;
1225    }
1226
1227    /* If we get here, they are the same name. */
1228
1229    /* In this case we could choose either (arbitrarily), but might as
1230       well choose the one with the lowest DiSym* address, so as to try
1231       and make the comparison mechanism more stable (a la sorting
1232       parlance).  Also, skip the diagnostic printing in this case. */
1233    return a <= b  ? a  : b;
1234
1235    /*NOTREACHED*/
1236    vg_assert(0);
1237   out:
1238    if (preferA && !preferB) {
1239       TRACE_SYMTAB("sym at %#lx: prefer '%s' to '%s'\n",
1240                    a->addr, a->name, b->name );
1241       return a;
1242    }
1243    if (preferB && !preferA) {
1244       TRACE_SYMTAB("sym at %#lx: prefer '%s' to '%s'\n",
1245                    b->addr, b->name, a->name );
1246       return b;
1247    }
1248    /*NOTREACHED*/
1249    vg_assert(0);
1250 }
1251
1252 static void canonicaliseSymtab ( struct _DebugInfo* di )
1253 {
1254    Word  i, j, n_merged, n_truncated;
1255    Addr  s1, s2, e1, e2, p1, p2;
1256    UChar *n1, *n2;
1257    Bool t1, t2, f1, f2;
1258
1259 #  define SWAP(ty,aa,bb) \
1260       do { ty tt = (aa); (aa) = (bb); (bb) = tt; } while (0)
1261
1262    if (di->symtab_used == 0)
1263       return;
1264
1265    VG_(ssort)(di->symtab, di->symtab_used, 
1266                           sizeof(*di->symtab), compare_DiSym);
1267
1268   cleanup_more:
1269  
1270    /* If two symbols have identical address ranges, we pick one
1271       using prefersym() (see it for details). */
1272    do {
1273       n_merged = 0;
1274       j = di->symtab_used;
1275       di->symtab_used = 0;
1276       for (i = 0; i < j; i++) {
1277          if (i < j-1
1278              && di->symtab[i].addr   == di->symtab[i+1].addr
1279              && di->symtab[i].size   == di->symtab[i+1].size
1280              ) {
1281             n_merged++;
1282             /* merge the two into one */
1283             di->symtab[di->symtab_used++] 
1284                = *prefersym(di, &di->symtab[i], &di->symtab[i+1]);
1285             i++;
1286          } else {
1287             di->symtab[di->symtab_used++] = di->symtab[i];
1288          }
1289       }
1290       TRACE_SYMTAB( "canonicaliseSymtab: %ld symbols merged\n", n_merged);
1291    }
1292    while (n_merged > 0);
1293
1294    /* Detect and "fix" overlapping address ranges. */
1295    n_truncated = 0;
1296
1297    for (i = 0; i < ((Word)di->symtab_used) -1; i++) {
1298
1299       vg_assert(di->symtab[i].addr <= di->symtab[i+1].addr);
1300
1301       /* Check for common (no overlap) case. */ 
1302       if (di->symtab[i].addr + di->symtab[i].size 
1303           <= di->symtab[i+1].addr)
1304          continue;
1305
1306       /* There's an overlap.  Truncate one or the other. */
1307       if (di->trace_symtab) {
1308          VG_(printf)("overlapping address ranges in symbol table\n\t");
1309          ML_(ppSym)( i, &di->symtab[i] );
1310          VG_(printf)("\t");
1311          ML_(ppSym)( i+1, &di->symtab[i+1] );
1312          VG_(printf)("\n");
1313       }
1314
1315       /* Truncate one or the other. */
1316       s1 = di->symtab[i].addr;
1317       e1 = s1 + di->symtab[i].size - 1;
1318       p1 = di->symtab[i].tocptr;
1319       n1 = di->symtab[i].name;
1320       t1 = di->symtab[i].isText;
1321       f1 = di->symtab[i].isIFunc;
1322       s2 = di->symtab[i+1].addr;
1323       e2 = s2 + di->symtab[i+1].size - 1;
1324       p2 = di->symtab[i+1].tocptr;
1325       n2 = di->symtab[i+1].name;
1326       t2 = di->symtab[i+1].isText;
1327       f2 = di->symtab[i+1].isIFunc;
1328       if (s1 < s2) {
1329          e1 = s2-1;
1330       } else {
1331          vg_assert(s1 == s2);
1332          if (e1 > e2) { 
1333             s1 = e2+1; SWAP(Addr,s1,s2); SWAP(Addr,e1,e2); SWAP(Addr,p1,p2);
1334                        SWAP(UChar *,n1,n2); SWAP(Bool,t1,t2);
1335          } else 
1336          if (e1 < e2) {
1337             s2 = e1+1;
1338          } else {
1339            /* e1 == e2.  Identical addr ranges.  We'll eventually wind
1340               up back at cleanup_more, which will take care of it. */
1341          }
1342       }
1343       di->symtab[i].addr    = s1;
1344       di->symtab[i].size    = e1 - s1 + 1;
1345       di->symtab[i].tocptr  = p1;
1346       di->symtab[i].name    = n1;
1347       di->symtab[i].isText  = t1;
1348       di->symtab[i].isIFunc = f1;
1349       di->symtab[i+1].addr    = s2;
1350       di->symtab[i+1].size    = e2 - s2 + 1;
1351       di->symtab[i+1].tocptr  = p2;
1352       di->symtab[i+1].name    = n2;
1353       di->symtab[i+1].isText  = t2;
1354       di->symtab[i+1].isIFunc = f2;
1355       vg_assert(s1 <= s2);
1356       vg_assert(di->symtab[i].size > 0);
1357       vg_assert(di->symtab[i+1].size > 0);
1358       /* It may be that the i+1 entry now needs to be moved further
1359          along to maintain the address order requirement. */
1360       j = i+1;
1361       while (j < ((Word)di->symtab_used)-1 
1362              && di->symtab[j].addr > di->symtab[j+1].addr) {
1363          SWAP(DiSym,di->symtab[j],di->symtab[j+1]);
1364          j++;
1365       }
1366       n_truncated++;
1367    }
1368
1369    if (n_truncated > 0) goto cleanup_more;
1370
1371    /* Ensure relevant postconditions hold. */
1372    for (i = 0; i < ((Word)di->symtab_used)-1; i++) {
1373       /* No zero-sized symbols. */
1374       vg_assert(di->symtab[i].size > 0);
1375       /* In order. */
1376       vg_assert(di->symtab[i].addr < di->symtab[i+1].addr);
1377       /* No overlaps. */
1378       vg_assert(di->symtab[i].addr + di->symtab[i].size - 1
1379                 < di->symtab[i+1].addr);
1380    }
1381 #  undef SWAP
1382 }
1383
1384
1385 /* Sort the location table by starting address.  Mash the table around
1386    so as to establish the property that addresses are in order and the
1387    ranges do not overlap.  This facilitates using binary search to map
1388    addresses to locations when we come to query the table.
1389 */
1390 static Int compare_DiLoc ( void* va, void* vb ) 
1391 {
1392    DiLoc* a = (DiLoc*)va;
1393    DiLoc* b = (DiLoc*)vb;
1394    if (a->addr < b->addr) return -1;
1395    if (a->addr > b->addr) return  1;
1396    return 0;
1397 }
1398
1399 static void canonicaliseLoctab ( struct _DebugInfo* di )
1400 {
1401    Word i, j;
1402
1403 #  define SWAP(ty,aa,bb) \
1404       do { ty tt = (aa); (aa) = (bb); (bb) = tt; } while (0);
1405
1406    if (di->loctab_used == 0)
1407       return;
1408
1409    /* Sort by start address. */
1410    VG_(ssort)(di->loctab, di->loctab_used, 
1411                           sizeof(*di->loctab), compare_DiLoc);
1412
1413    /* If two adjacent entries overlap, truncate the first. */
1414    for (i = 0; i < ((Word)di->loctab_used)-1; i++) {
1415       vg_assert(di->loctab[i].size < 10000);
1416       if (di->loctab[i].addr + di->loctab[i].size > di->loctab[i+1].addr) {
1417          /* Do this in signed int32 because the actual .size fields
1418             are only 12 bits. */
1419          Int new_size = di->loctab[i+1].addr - di->loctab[i].addr;
1420          if (new_size < 0) {
1421             di->loctab[i].size = 0;
1422          } else
1423          if (new_size > MAX_LOC_SIZE) {
1424            di->loctab[i].size = MAX_LOC_SIZE;
1425          } else {
1426            di->loctab[i].size = (UShort)new_size;
1427          }
1428       }
1429    }
1430
1431    /* Zap any zero-sized entries resulting from the truncation
1432       process. */
1433    j = 0;
1434    for (i = 0; i < (Word)di->loctab_used; i++) {
1435       if (di->loctab[i].size > 0) {
1436          if (j != i)
1437             di->loctab[j] = di->loctab[i];
1438          j++;
1439       }
1440    }
1441    di->loctab_used = j;
1442
1443    /* Ensure relevant postconditions hold. */
1444    for (i = 0; i < ((Word)di->loctab_used)-1; i++) {
1445       /* 
1446       VG_(printf)("%d   (%d) %d 0x%x\n", 
1447                    i, di->loctab[i+1].confident, 
1448                    di->loctab[i+1].size, di->loctab[i+1].addr );
1449       */
1450       /* No zero-sized symbols. */
1451       vg_assert(di->loctab[i].size > 0);
1452       /* In order. */
1453       vg_assert(di->loctab[i].addr < di->loctab[i+1].addr);
1454       /* No overlaps. */
1455       vg_assert(di->loctab[i].addr + di->loctab[i].size - 1
1456                 < di->loctab[i+1].addr);
1457    }
1458 #  undef SWAP
1459 }
1460
1461
1462 /* Sort the call-frame-info table by starting address.  Mash the table
1463    around so as to establish the property that addresses are in order
1464    and the ranges do not overlap.  This facilitates using binary
1465    search to map addresses to locations when we come to query the
1466    table.  
1467
1468    Also, set cfisi_minaddr and cfisi_maxaddr to be the min and max of
1469    any of the address ranges contained in cfisi[0 .. cfisi_used-1], so
1470    as to facilitate rapidly skipping this SegInfo when looking for an
1471    address which falls outside that range.
1472 */
1473 static Int compare_DiCfSI ( void* va, void* vb )
1474 {
1475    DiCfSI* a = (DiCfSI*)va;
1476    DiCfSI* b = (DiCfSI*)vb;
1477    if (a->base < b->base) return -1;
1478    if (a->base > b->base) return  1;
1479    return 0;
1480 }
1481
1482 void ML_(canonicaliseCFI) ( struct _DebugInfo* di )
1483 {
1484    Word  i, j;
1485    const Addr minAvma = 0;
1486    const Addr maxAvma = ~minAvma;
1487
1488    /* Note: take care in here.  di->cfsi can be NULL, in which
1489       case _used and _size fields will be zero. */
1490    if (di->cfsi == NULL) {
1491       vg_assert(di->cfsi_used == 0);
1492       vg_assert(di->cfsi_size == 0);
1493    }
1494
1495    /* Set cfsi_minavma and cfsi_maxavma to summarise the entire
1496       address range contained in cfsi[0 .. cfsi_used-1]. */
1497    di->cfsi_minavma = maxAvma; 
1498    di->cfsi_maxavma = minAvma;
1499    for (i = 0; i < (Word)di->cfsi_used; i++) {
1500       Addr here_min = di->cfsi[i].base;
1501       Addr here_max = di->cfsi[i].base + di->cfsi[i].len - 1;
1502       if (here_min < di->cfsi_minavma)
1503          di->cfsi_minavma = here_min;
1504       if (here_max > di->cfsi_maxavma)
1505          di->cfsi_maxavma = here_max;
1506    }
1507
1508    if (di->trace_cfi)
1509       VG_(printf)("canonicaliseCfiSI: %ld entries, %#lx .. %#lx\n",
1510                   di->cfsi_used,
1511                   di->cfsi_minavma, di->cfsi_maxavma);
1512
1513    /* Sort the cfsi array by base address. */
1514    VG_(ssort)(di->cfsi, di->cfsi_used, sizeof(*di->cfsi), compare_DiCfSI);
1515
1516    /* If two adjacent entries overlap, truncate the first. */
1517    for (i = 0; i < (Word)di->cfsi_used-1; i++) {
1518       if (di->cfsi[i].base + di->cfsi[i].len > di->cfsi[i+1].base) {
1519          Word new_len = di->cfsi[i+1].base - di->cfsi[i].base;
1520          /* how could it be otherwise?  The entries are sorted by the
1521             .base field. */         
1522          vg_assert(new_len >= 0);
1523          vg_assert(new_len <= di->cfsi[i].len);
1524          di->cfsi[i].len = new_len;
1525       }
1526    }
1527
1528    /* Zap any zero-sized entries resulting from the truncation
1529       process. */
1530    j = 0;
1531    for (i = 0; i < (Word)di->cfsi_used; i++) {
1532       if (di->cfsi[i].len > 0) {
1533          if (j != i)
1534             di->cfsi[j] = di->cfsi[i];
1535          j++;
1536       }
1537    }
1538    /* VG_(printf)("XXXXXXXXXXXXX %d %d\n", di->cfsi_used, j); */
1539    di->cfsi_used = j;
1540
1541    /* Ensure relevant postconditions hold. */
1542    for (i = 0; i < (Word)di->cfsi_used; i++) {
1543       /* No zero-length ranges. */
1544       vg_assert(di->cfsi[i].len > 0);
1545       /* Makes sense w.r.t. summary address range */
1546       vg_assert(di->cfsi[i].base >= di->cfsi_minavma);
1547       vg_assert(di->cfsi[i].base + di->cfsi[i].len - 1
1548                 <= di->cfsi_maxavma);
1549
1550       if (i < di->cfsi_used - 1) {
1551          /*
1552          if (!(di->cfsi[i].base < di->cfsi[i+1].base)) {
1553             VG_(printf)("\nOOO cfsis:\n");
1554             ML_(ppCfiSI)(&di->cfsi[i]);
1555             ML_(ppCfiSI)(&di->cfsi[i+1]);
1556          }
1557          */
1558          /* In order. */
1559          vg_assert(di->cfsi[i].base < di->cfsi[i+1].base);
1560          /* No overlaps. */
1561          vg_assert(di->cfsi[i].base + di->cfsi[i].len - 1
1562                    < di->cfsi[i+1].base);
1563       }
1564    }
1565
1566 }
1567
1568
1569 /* Canonicalise the tables held by 'di', in preparation for use.  Call
1570    this after finishing adding entries to these tables. */
1571 void ML_(canonicaliseTables) ( struct _DebugInfo* di )
1572 {
1573    canonicaliseSymtab ( di );
1574    canonicaliseLoctab ( di );
1575    ML_(canonicaliseCFI) ( di );
1576    canonicaliseVarInfo ( di );
1577 }
1578
1579
1580 /*------------------------------------------------------------*/
1581 /*--- Searching the tables                                 ---*/
1582 /*------------------------------------------------------------*/
1583
1584 /* Find a symbol-table index containing the specified pointer, or -1
1585    if not found.  Binary search.  */
1586
1587 Word ML_(search_one_symtab) ( struct _DebugInfo* di, Addr ptr,
1588                               Bool match_anywhere_in_sym,
1589                               Bool findText )
1590 {
1591    Addr a_mid_lo, a_mid_hi;
1592    Word mid, size, 
1593         lo = 0, 
1594         hi = di->symtab_used-1;
1595    while (True) {
1596       /* current unsearched space is from lo to hi, inclusive. */
1597       if (lo > hi) return -1; /* not found */
1598       mid      = (lo + hi) / 2;
1599       a_mid_lo = di->symtab[mid].addr;
1600       size = ( match_anywhere_in_sym
1601              ? di->symtab[mid].size
1602              : 1);
1603       a_mid_hi = ((Addr)di->symtab[mid].addr) + size - 1;
1604
1605       if (ptr < a_mid_lo) { hi = mid-1; continue; } 
1606       if (ptr > a_mid_hi) { lo = mid+1; continue; }
1607       vg_assert(ptr >= a_mid_lo && ptr <= a_mid_hi);
1608       /* Found a symbol with the correct address range.  But is it
1609          of the right kind (text vs data) ? */
1610       if (  findText   &&   di->symtab[mid].isText  ) return mid;
1611       if ( (!findText) && (!di->symtab[mid].isText) ) return mid;
1612       return -1;
1613    }
1614 }
1615
1616
1617 /* Find a location-table index containing the specified pointer, or -1
1618    if not found.  Binary search.  */
1619
1620 Word ML_(search_one_loctab) ( struct _DebugInfo* di, Addr ptr )
1621 {
1622    Addr a_mid_lo, a_mid_hi;
1623    Word mid, 
1624         lo = 0, 
1625         hi = di->loctab_used-1;
1626    while (True) {
1627       /* current unsearched space is from lo to hi, inclusive. */
1628       if (lo > hi) return -1; /* not found */
1629       mid      = (lo + hi) / 2;
1630       a_mid_lo = di->loctab[mid].addr;
1631       a_mid_hi = ((Addr)di->loctab[mid].addr) + di->loctab[mid].size - 1;
1632
1633       if (ptr < a_mid_lo) { hi = mid-1; continue; } 
1634       if (ptr > a_mid_hi) { lo = mid+1; continue; }
1635       vg_assert(ptr >= a_mid_lo && ptr <= a_mid_hi);
1636       return mid;
1637    }
1638 }
1639
1640
1641 /* Find a CFI-table index containing the specified pointer, or -1
1642    if not found.  Binary search.  */
1643
1644 Word ML_(search_one_cfitab) ( struct _DebugInfo* di, Addr ptr )
1645 {
1646    Addr a_mid_lo, a_mid_hi;
1647    Word mid, size, 
1648         lo = 0, 
1649         hi = di->cfsi_used-1;
1650    while (True) {
1651       /* current unsearched space is from lo to hi, inclusive. */
1652       if (lo > hi) return -1; /* not found */
1653       mid      = (lo + hi) / 2;
1654       a_mid_lo = di->cfsi[mid].base;
1655       size     = di->cfsi[mid].len;
1656       a_mid_hi = a_mid_lo + size - 1;
1657       vg_assert(a_mid_hi >= a_mid_lo);
1658       if (ptr < a_mid_lo) { hi = mid-1; continue; } 
1659       if (ptr > a_mid_hi) { lo = mid+1; continue; }
1660       vg_assert(ptr >= a_mid_lo && ptr <= a_mid_hi);
1661       return mid;
1662    }
1663 }
1664
1665
1666 /* Find a FPO-table index containing the specified pointer, or -1
1667    if not found.  Binary search.  */
1668
1669 Word ML_(search_one_fpotab) ( struct _DebugInfo* di, Addr ptr )
1670 {
1671    Addr const addr = ptr - di->rx_map_avma;
1672    Addr a_mid_lo, a_mid_hi;
1673    Word mid, size,
1674         lo = 0,
1675         hi = di->fpo_size-1;
1676    while (True) {
1677       /* current unsearched space is from lo to hi, inclusive. */
1678       if (lo > hi) return -1; /* not found */
1679       mid      = (lo + hi) / 2;
1680       a_mid_lo = di->fpo[mid].ulOffStart;
1681       size     = di->fpo[mid].cbProcSize;
1682       a_mid_hi = a_mid_lo + size - 1;
1683       vg_assert(a_mid_hi >= a_mid_lo);
1684       if (addr < a_mid_lo) { hi = mid-1; continue; }
1685       if (addr > a_mid_hi) { lo = mid+1; continue; }
1686       vg_assert(addr >= a_mid_lo && addr <= a_mid_hi);
1687       return mid;
1688    }
1689 }
1690
1691 /*--------------------------------------------------------------------*/
1692 /*--- end                                                          ---*/
1693 /*--------------------------------------------------------------------*/