Stackdb
Stackdb is a stackable, multi-target and -level source debugger and memory forensics library.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dwarf_debuginfo.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 2012, 2013, 2014 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <stdint.h>
27 #include <inttypes.h>
28 #include <assert.h>
29 #include <regex.h>
30 
31 #include "config.h"
32 #include "common.h"
33 #include "glib_wrapper.h"
34 #include "log.h"
35 #include "output.h"
36 #include "list.h"
37 #include "alist.h"
38 #include "binfile.h"
39 #include "dwdebug.h"
40 #include "dwdebug_priv.h"
41 
42 #include <dwarf.h>
43 #include <gelf.h>
44 #include <elfutils/libebl.h>
45 #include <elfutils/libdw.h>
46 #include <elfutils/libdwfl.h>
47 
48 #include "memory-access.h"
49 
50 /*
51  * Prototypes.
52  */
53 
54 struct attrcb_args {
56 
57  int level;
58  Dwarf_Off cu_offset;
59  Dwarf_Off die_offset;
60  Dwarf_Off sibling;
61 
62  struct symbol *symbol;
63  struct symbol *parentsymbol;
64  struct symbol *voidsymbol;
65 
66  Dwarf_Off ref;
67  Dwarf_Off specification_ref;
68 
71  Dwarf_Word stmt_list_offset;
72  struct range *ranges;
73  uint8_t lowpc_set:1,
74  highpc_set:1,
76  reloading:1,
79  ref_set:1;
80 };
81 
82 extern int dwarf_load_cfa(struct debugfile *debugfile,
83  char *buf,unsigned int len,Dwarf *dbg);
84 extern int dwarf_unload_cfa(struct debugfile *debugfile);
85 /* Declare these now; they are used in attr_callback. */
86 static struct range *dwarf_get_ranges(struct symbol_root_dwarf *srd,
87  unsigned int attr,Dwarf_Word offset);
88 
89 static struct location *dwarf_get_loclistloc(struct symbol_root_dwarf *srd,
90  unsigned int attr,
91  Dwarf_Word offset);
92 int dwarf_get_lines(struct symbol_root_dwarf *srd,Dwarf_Off offset);
93 static inline char *dwarf_language_string(int language);
94 
95 const char *dwarf_tag_string(unsigned int tag);
96 const char *dwarf_attr_string(unsigned int attrnum);
97 const char *dwarf_form_string(unsigned int form);
98 const char *dwarf_lang_string(unsigned int lang);
99 const char *dwarf_inline_string(unsigned int code);
100 const char *dwarf_encoding_string(unsigned int code);
101 const char *dwarf_access_string(unsigned int code);
102 const char *dwarf_visibility_string(unsigned int code);
103 const char *dwarf_virtuality_string(unsigned int code);
104 const char *dwarf_identifier_case_string(unsigned int code);
105 const char *dwarf_calling_convention_string(unsigned int code);
106 const char *dwarf_ordering_string(unsigned int code);
107 const char *dwarf_discr_list_string(unsigned int code);
108 
110  struct symbol *symbol,SMOFFSET ref) {
111  GSList *reflist;
112 
113  reflist = (GSList *)g_hash_table_lookup(srd->refuselist,
114  (gpointer)(uintptr_t)ref);
115  reflist = g_slist_prepend(reflist,(gpointer)symbol);
116  RHOLD(symbol,srd->refuselist);
117  g_hash_table_insert(srd->refuselist,(gpointer)(uintptr_t)ref,reflist);
118 
119  return 0;
120 }
121 
123  struct symbol *symbol,SMOFFSET ref) {
124  GSList *reflist;
125  REFCNT trefcnt;
126 
127  reflist = (GSList *)g_hash_table_lookup(srd->refuselist,
128  (gpointer)(uintptr_t)ref);
129  if (reflist) {
130  reflist = g_slist_remove(reflist,(gpointer)symbol);
131  RPUT(symbol,symbol,srd->refuselist,trefcnt);
132  g_hash_table_insert(srd->refuselist,(gpointer)(uintptr_t)ref,reflist);
133  }
134 
135  return 0;
136 }
137 
139  return (GSList *)g_hash_table_lookup(srd->refuselist,
140  (gpointer)(uintptr_t)ref);
141 }
142 
143 /* Should be called before symbol_free!! */
145  struct symbol *symbol) {
146  if (symbol->datatype_ref) {
147  dwarf_refuselist_release(srd,symbol,symbol->datatype_ref);
148  symbol->datatype_ref = 0;
149  }
150 
151  SYMBOL_RX_INLINE(symbol,sii);
152  if (sii && sii->origin_ref) {
153  dwarf_refuselist_release(srd,symbol,sii->origin_ref);
154  sii->origin_ref = 0;
155  }
156 
157  return 0;
158 }
159 
160 /*
161  * This function handles the case when the @refsym at offset @ref has
162  * been set for the first time, or changed, for @symbol that was using
163  * @ref. This gives us a chance to update any held pointers for
164  * @symbol.
165  */
167  struct symbol *symbol,
168  SMOFFSET ref,struct symbol *refsym) {
169  REFCNT trefcnt;
170 
171  /*
172  * The ref is either a datatype_ref; an origin_ref; or else the ref
173  * is on our inline instances list. If it's the final case, we
174  * have to go through the list looking for symbols with @ref, and
175  * replacing them in the list with @refsym.
176  */
177  if (symbol->datatype_ref == ref) {
178  vdebug(6,LA_DEBUG,LF_DWARF,"changing datatype for ");
179  LOGDUMPSYMBOL(6,LA_DEBUG,LF_DWARF,symbol);
180  vdebugc(6,LA_DEBUG,LF_DWARF," from ");
181  if (symbol->datatype) {
183  }
184  else {
185  vdebugc(6,LA_DEBUG,LF_DWARF,"NULL");
186  }
187  vdebugc(6,LA_DEBUG,LF_DWARF," to ");
189 
190  /*
191  * Dump the old one; grab the new one. Be careful to drop refs
192  * if the datatype was a shared one, or if the symbol was
193  * synthetic. Neither should happen, but just in case...
194  */
195  if (symbol->datatype && (symbol->usesshareddatatype
196  || symbol->issynthetic)) {
197  RPUT(symbol->datatype,symbol,symbol,trefcnt);
198  }
199 
200  if (refsym->isshared || symbol->issynthetic) {
201  if (refsym->isshared)
202  symbol->usesshareddatatype = 1;
203  RHOLD(refsym,symbol);
204  }
205 
206  symbol->datatype = refsym;
207  }
208  else {
209  SYMBOL_RX_INLINE(symbol,sii);
210  if (sii && sii->origin_ref == ref) {
211  vdebug(6,LA_DEBUG,LF_DWARF,"changing origin for ");
212  LOGDUMPSYMBOL(6,LA_DEBUG,LF_DWARF,symbol);
213  vdebugc(6,LA_DEBUG,LF_DWARF," from ");
214  if (sii->origin) {
215  LOGDUMPSYMBOL(6,LA_DEBUG,LF_DWARF,sii->origin);
216  }
217  else {
218  vdebugc(6,LA_DEBUG,LF_DWARF,"NULL");
219  }
220  vdebugc(6,LA_DEBUG,LF_DWARF," to ");
222 
223  sii->origin = refsym;
224 
226  "adding instance ");
227  LOGDUMPSYMBOL(6,LA_DEBUG,LF_DWARF,symbol);
228  vdebugc(6,LA_DEBUG,LF_DWARF," to ");
230 
231  symbol_add_inline_instance(refsym,symbol);
232 
233  /*
234  * NB: don't copy anything from the origin to the instance
235  * anymore; the symbol_* functions do this following
236  * automatically as needed.
237  */
238  /*
239  symbol->datatype = sii->origin->datatype;
240  symbol->datatype_ref = sii->origin->datatype_ref;
241  //memcpy(&symbol->s.ii->l,&symbol->s.ii->origin->s.ii->l,
242  // sizeof(struct location));
243 
244  if (symbol->datatype)
245  RHOLD(symbol->datatype,sii->origin->dataype);
246 
247  vdebug(4,LA_DEBUG,LF_SYMBOL,
248  "copied datatype %s//%s (0x%"PRIxSMOFFSET")"
249  " for inline instance %s//%s"
250  " (0x%"PRIxSMOFFSET"\n",
251  symbol->datatype ? DATATYPE(symbol->datatype->datatype_code) : NULL,
252  symbol->datatype ? symbol_get_name(symbol->datatype) : NULL,
253  symbol->datatype ? symbol->datatype->ref : 0,
254  SYMBOL_TYPE(symbol->type),symbol_get_name(symbol),
255  symbol->ref);
256  */
257  }
258  }
259 
260  return 0;
261 }
262 
263 /*
264  * Handle changes to the reftab -- either new symbols, or symbol
265  * replacements! This means that any symbol that had (or would)
266  * reference the new/changed symbol needs to adjust its pointer(s).
267  */
269  SMOFFSET ref,struct symbol *refsym) {
270  GSList *uselist;
271  GSList *gsltmp;
272  struct symbol *symbol;
273 
274  uselist = (GSList *) \
275  g_hash_table_lookup(srd->refuselist,(gpointer)(uintptr_t)ref);
276  if (!uselist)
277  return 0;
278 
279  v_g_slist_foreach(uselist,gsltmp,symbol) {
280  dwarf_symbol_ref_symbol_changed(srd,symbol,ref,refsym);
281  }
282 
283  return 0;
284 }
285 
286 /*
287  * Record in the refuselist that we want this datatype_ref; resolve it
288  * if possible.
289  */
291  struct symbol *symbol,SMOFFSET ref) {
292  dwarf_refuselist_hold(srd,symbol,ref);
293 
294  symbol->datatype_ref = ref;
295  /*
296  * Set the datatype symbol if the reftab has it; otherwise, it will
297  * get set later!
298  */
299  symbol->datatype = (struct symbol *) \
300  g_hash_table_lookup(srd->reftab,(gpointer)(uintptr_t)ref);
301 
302  return 0;
303 }
304 
305 /*
306  * Record in the refuselist that we want this origin_ref; resolve it
307  * if possible.
308  */
310  struct symbol *symbol,SMOFFSET ref) {
311  SYMBOL_WX_INLINE(symbol,sii,-1);
312 
313  dwarf_refuselist_hold(srd,symbol,ref);
314 
315  /*
316  * First, setup stuff for the referencing symbol. Set the origin
317  * symbol if the reftab has it; otherwise, it will get set later!
318  */
319  sii->origin_ref = ref;
320  sii->origin = (struct symbol *) \
321  g_hash_table_lookup(srd->reftab,(gpointer)(uintptr_t)ref);
322 
323  /*
324  * Second, setup stuff for the *referenced* symbol. If the
325  * referenced symbol exists, add this symbol as an inline instance
326  * now. If it doesn't exist, we place it on the refuselist too;
327  * dwarf_refuselist_notify_reftab_changed() will handle that case
328  * when the symbol ref actually is set.
329  *
330  * Note, then, that we support changing the origin, technically, but
331  * it should not ever happen -- origins and instances are pretty
332  * firmly linked and should not ever be replaced. We don't even try
333  * to support this case for the inline instances lists.
334  */
335  if (sii->origin)
336  symbol_add_inline_instance(sii->origin,symbol);
337 
338  return 0;
339 }
340 
341 /*
342  * Insert a symbol into the reftab. This takes care of safely removing
343  * whatever symbol is already in it; and notifies any users of the
344  * symbol in the refuselist hashtable once the change has been made.
345  */
347  struct symbol *symbol,SMOFFSET ref) {
348  struct symbol *existing;
349  REFCNT trefcnt;
350 
351  existing = (struct symbol *) \
352  g_hash_table_lookup(srd->reftab,(gpointer)(uintptr_t)ref);
353 
354  if (existing) {
355  if (existing == symbol)
356  return 0;
357 
358  RPUT(existing,symbol,srd->reftab,trefcnt);
359  }
360 
361  g_hash_table_insert(srd->reftab,(gpointer)(uintptr_t)ref,symbol);
362  RHOLD(symbol,srd->reftab);
363 
365 
366  return 0;
367 }
368 
370  struct symbol *existing,
371  struct symbol *new,SMOFFSET ref,GHashTableIter *iter) {
372 
373  REFCNT trefcnt;
374 
375  if (existing) {
376  if (existing == new)
377  return 0;
378 
379  RPUT(existing,symbol,srd->reftab,trefcnt);
380  }
381 
382  g_hash_table_iter_replace(iter,new);
383  RHOLD(new,srd->reftab);
384 
386 
387  return 0;
388 }
389 
390 static void dwarf_reftab_clean(struct symbol *root,int force) {
391  struct symbol_root_dwarf *srd = SYMBOLX_ROOT(root)->priv;
392  GHashTableIter iter;
393  struct symbol *tsymbol;
394  REFCNT trefcnt;
395 
396  if (!(SYMBOL_IS_FULL(root) || force))
397  return;
398 
399  if (!srd->reftab)
400  return;
401 
402  /*
403  * Put all the tmp refs we took on reftab symbols. Some might
404  * be NULL if we employed type compression.
405  *
406  * If a symbol is still in the refuselist, leave it in the reftab
407  * too. We might still need it!
408  */
409  g_hash_table_iter_init(&iter,srd->reftab);
410  while (g_hash_table_iter_next(&iter,NULL,(gpointer *)&tsymbol)) {
411  if (!tsymbol)
412  continue;
413  else if (force) {
414  ;
415  }
416  else if (srd->refuselist
417  && g_hash_table_lookup_extended(srd->refuselist,
418  (gpointer)(uintptr_t)tsymbol->ref,
419  NULL,NULL) == TRUE)
420  continue;
421 
422  RPUT(tsymbol,symbol,srd->reftab,trefcnt);
423  g_hash_table_iter_remove(&iter);
424  }
425 }
426 
427 static void dwarf_refuselist_clean(struct symbol *root,int force) {
428  struct symbol_root_dwarf *srd = SYMBOLX_ROOT(root)->priv;
429  GHashTableIter iter;
430  gpointer key,value;
431  struct symbol *tsymbol;
432  GSList *gsltmp;
433  REFCNT trefcnt;
434 
435  if (!(SYMBOL_IS_FULL(root) || force))
436  return;
437 
438  if (!srd->refuselist)
439  return;
440 
441  /*
442  * If the CU has been fully loaded or if there was an error, clear
443  * out the reftab and refuselist tables!
444  *
445  * However, there are a few exceptions. Any symbol that might have
446  * to get replaced with a symbol in another CU we have to leave in
447  * place for now! For now, that's only undefined declarations. So,
448  * go through the refuselist, check for decl key symbols; save
449  * those. Then, however, we can clean out the whole reftab -- we
450  * don't need any of that because there are symbols on the
451  * refuselist, not refs.
452  */
453  g_hash_table_iter_init(&iter,srd->refuselist);
454  while (g_hash_table_iter_next(&iter,&key,&value)) {
455  tsymbol = NULL;
456  if (!force) {
457  if (srd->reftab)
458  tsymbol = (struct symbol *) \
459  g_hash_table_lookup(srd->reftab,(gpointer)(uintptr_t)key);
460  if (!(tsymbol && tsymbol->isdeclaration && !tsymbol->decldefined))
461  tsymbol = NULL;
462  }
463  if (force || !tsymbol) {
464  v_g_slist_foreach((GSList *)value,gsltmp,tsymbol) {
465  RPUT(tsymbol,symbol,srd->refuselist,trefcnt);
466  }
467  g_slist_free((GSList *)value);
468  g_hash_table_iter_remove(&iter);
469  }
470  }
471 }
472 
473 /*
474  * If we get a definition that specifies a declaration, we need to move
475  * the @specification into the scope containing @declaration.
476  * Basically, we want the definition symbol to actually move into the
477  * declaration's scope, from wherever scope it is currently on.
478  *
479  * But then, it gets even worse. We basically want anything useful from
480  * the declaration to be incorporated into the definition (we won't
481  * worry about the other way around for now -- in fact one useful thing
482  * to do might be to get rid of the declaration symbol entirely!). This
483  * is tricky. Here's an example:
484  *
485  * <3><28e>: Abbrev Number: 34 (DW_TAG_subprogram)
486  * <28f> DW_AT_external : 1
487  * <290> DW_AT_name : (indirect string, offset: 0xcf): exception
488  * <294> DW_AT_artificial : 1
489  * <295> DW_AT_declaration : 1
490  * <296> DW_AT_object_pointer: <0x29a>
491  * <4><29a>: Abbrev Number: 8 (DW_TAG_formal_parameter)
492  * <29b> DW_AT_type : <0x2a8>
493  * <29f> DW_AT_artificial : 1
494  * <4><2a0>: Abbrev Number: 9 (DW_TAG_formal_parameter)
495  * <2a1> DW_AT_type : <0x2f6>
496  *
497  *
498  * <1><301>: Abbrev Number: 39 (DW_TAG_subprogram)
499  * <302> DW_AT_specification: <0x28e>
500  * <306> DW_AT_decl_file : 1
501  * <307> DW_AT_decl_line : 61
502  * <308> DW_AT_inline : 2 (declared as inline but ignored)
503  * <309> DW_AT_object_pointer: <0x311>
504  * <30d> DW_AT_sibling : <0x321>
505  * <2><311>: Abbrev Number: 36 (DW_TAG_formal_parameter)
506  * <312> DW_AT_name : (indirect string, offset: 0x72): this
507  * <316> DW_AT_type : <0x2c7>
508  * <31a> DW_AT_artificial : 1
509  * <2><31b>: Abbrev Number: 9 (DW_TAG_formal_parameter)
510  * <31c> DW_AT_type : <0x321>
511  *
512  * and another:
513  *
514  * <3><368>: Abbrev Number: 41 (DW_TAG_subprogram)
515  * <369> DW_AT_external : 1
516  * <36a> DW_AT_name : (indirect string, offset: 0x18d): pushIt
517  * <36e> DW_AT_decl_file : 2
518  * <36f> DW_AT_decl_line : 10
519  * <370> DW_AT_MIPS_linkage_name: (indirect string, offset: 0xd9): _ZN2N12N26pushItEi
520  * <374> DW_AT_type : <0x1e2>
521  * <378> DW_AT_declaration : 1
522  * <4><379>: Abbrev Number: 9 (DW_TAG_formal_parameter)
523  * <37a> DW_AT_type : <0x1e2>
524  * <1><381>: Abbrev Number: 42 (DW_TAG_subprogram)
525  * <382> DW_AT_specification: <0x368>
526  * <386> DW_AT_low_pc : 0x4009e4
527  * <38e> DW_AT_high_pc : 0x400a2e
528  * <396> DW_AT_frame_base : 0xc0 (location list)
529  * <39a> DW_AT_sibling : <0x3ab>
530  * <2><39e>: Abbrev Number: 43 (DW_TAG_formal_parameter)
531  * <39f> DW_AT_name : x
532  * <3a1> DW_AT_decl_file : 2
533  * <3a2> DW_AT_decl_line : 10
534  * <3a3> DW_AT_type : <0x1e2>
535  * <3a7> DW_AT_location : 2 byte block: 91 5c (DW_OP_fbreg: -36)
536  *
537  *
538  * So we can see we need to pull external, name, potentially datatype,
539  * ... but that's about it. What we should really try to do is pull all
540  * the settings we don't have set already.
541  */
542 static int dwarf_specify_definition(struct symbol *spec,struct symbol *def) {
543 
544  /* Cursory check. */
545  if (spec->type != def->type || spec->datatype_code != def->datatype_code)
546  return -1;
547 
548  /* @name */
549  if (spec->name && !def->name) {
550  if (spec->name_nofree) {
551  def->name = spec->name;
552  def->name_nofree = spec->name_nofree;
553  }
554  else {
555  def->name = strdup(spec->name);
556  def->name_nofree = 0;
557  }
558  def->orig_name_offset = spec->orig_name_offset;
559  }
560  /* @srcline -- XXX: need to handle this better??? srcfile could be diff. */
561  if (!def->srcline)
562  def->srcline = spec->srcline;
563 
564  /*
565  * Various flags. Basically, we need to handle
566  * ismember/isparam/isenumval/isexternal/has_linkage_name/; don't
567  * need to handle isshared (because a specified decl will never be
568  * defined by a spec ref).
569  */
570  def->ismember = spec->ismember;
571  def->isparam = spec->isparam;
572  def->isenumval = spec->isenumval;
573  def->isexternal = spec->isexternal;
574  def->has_linkage_name = spec->has_linkage_name;
575 
576  /* @datatype */
577  if (!def->datatype && spec->datatype) {
578  def->datatype = spec->datatype;
579  if (spec->usesshareddatatype) {
580  def->usesshareddatatype = 1;
581  RHOLD(def->datatype,def);
582  }
583  }
584  if (!def->datatype_ref)
585  def->datatype_ref = spec->datatype_ref;
586  /* @isexternal */
587  if (spec->isexternal)
588  def->isexternal = 1;
589  /*
590  * XXX: hm, don't worry about inline stuff right now. Instances
591  * would be problematic because if they ref the decl, we might not
592  * have them all by the time we get here.
593  *
594  * XXX: constval stuff?
595  */
596  /* @addr */
597  if (spec->has_addr && !def->has_addr) {
598  def->has_addr = 1;
599  def->addr = spec->addr;
600  }
601  /* @size */
602  if ((spec->size_is_bits || spec->size_is_bytes)
603  && !def->size_is_bits && !def->size_is_bytes) {
604  def->size_is_bits = spec->size_is_bits;
605  def->size_is_bytes = spec->size_is_bytes;
606  memcpy(&def->size,&spec->size,sizeof(spec->size));
607  }
608 
609  /*
610  * That's it for the core stuff. Now, don't copy any member info,
611  * BUT do check the per-symbol extra info.
612  */
613  if (SYMBOL_HAS_EXTRA(spec)) {
614  if (SYMBOL_IS_FUNC(spec)) {
615  SYMBOL_RX_FUNC(spec,sfr);
616  SYMBOL_WX_FUNC(def,sfw,-1);
617 
618  if (sfr->fbloc && !sfw->fbloc)
619  sfw->fbloc = location_copy(sfr->fbloc);
620  if (sfr->has_entry_pc && !sfw->has_entry_pc) {
621  sfw->has_entry_pc = 1;
622  sfw->entry_pc = sfr->entry_pc;
623  }
624  if ((sfr->prologue_known || sfr->prologue_guessed)
625  && !sfw->prologue_known && !sfw->prologue_guessed) {
626  sfw->prologue_known = 1;
627  sfw->prologue_end = sfr->prologue_end;
628  }
629  if (sfr->epilogue_known && !sfw->epilogue_known) {
630  sfw->epilogue_known = 1;
631  sfw->epilogue_begin = sfr->epilogue_begin;
632  }
633  }
634  else if (SYMBOL_IS_VAR(spec)) {
635  SYMBOL_RX_VAR(spec,svr);
636  SYMBOL_WX_VAR(def,svw,-1);
637 
638  if (svr->loc && !svw->loc)
639  svw->loc = location_copy(svr->loc);
640  /*
641  * We should never have to worry about constval copying; if
642  * the specifying "declaration" has a constval, how can
643  * there ever be a "definition" of it? :) Hopefully DWARF
644  * generators will honor my reasoning here.
645  */
646  }
647  else if (SYMBOL_IST_ARRAY(spec)) {
648  if (SYMBOLX_SUBRANGES(spec) && !SYMBOLX_SUBRANGES(def)) {
649  SYMBOLX_SUBRANGES(def) = g_slist_copy(SYMBOLX_SUBRANGES(spec));
650  }
651  }
652  /*
653  * Should not have to worry about any other type information.
654  */
655  }
656 
657  return 0;
658 }
659 
661  struct symbol *specification,
662  struct symbol *definition) {
663  int rc;
664 
665  rc = dwarf_specify_definition(specification,definition);
666 
668  "used specification ");
669  LOGDUMPSYMBOL(8,LA_DEBUG,LF_DFILE | LF_SYMBOL,specification);
671  " to complete definition ");
673 
674  return rc;
675 }
676 
678  struct symbol *specification,
679  struct symbol *definition) {
680  int rc;
681  GSList *mlist;
682  GSList *nmlist = NULL;
683  GSList *tomove = NULL;
684  GSList *gsl;
685  struct symbol *m,*l;
686  int moved = 0;
687  int updated = 0;
688  struct scope *scope;
689 
690  /*
691  * If this is a definition that is replacing an earlier declaration,
692  * we need to harvest any remaining members in the declaration that
693  * need to get moved into the definition. If any members were
694  * specification decls for definition members, they have already
695  * been removed. So we just go through the members remaining in the
696  * declaration; check if they already match a member in the
697  * definition; and if they don't, move them in. Otherwise delete
698  * them? Is that safe???
699  */
700 
701  /*
702  * First, we need to process members in order!
703  */
704  mlist = SYMBOLX_MEMBERS(specification);
705  gsl = NULL;
706  v_g_slist_foreach(mlist,gsl,m) {
707  if (!m->name)
708  continue;
709 
710  l = symbol_get_sym(definition,m->name,symbol_to_type_flag_t(m));
711  if (!l)
712  tomove = g_slist_append(tomove,m);
713  else {
715  ++updated;
716  }
717  }
718 
719  /*
720  * Second, we need to process non-member contained symbols (types
721  * are the big things; but even a namespace decl could contain other
722  * var/func symbols).
723  */
724  scope = symbol_read_owned_scope(specification);
725  if (scope)
726  nmlist = scope_match_syms(scope,NULL,SYMBOL_TYPE_FLAG_NONE);
727  gsl = NULL;
728  v_g_slist_foreach(nmlist,gsl,m) {
729  if (!m->name)
730  continue;
731 
732  l = symbol_get_sym(definition,m->name,symbol_to_type_flag_t(m));
733  if (!l)
734  tomove = g_slist_append(tomove,m);
735  else {
737  ++updated;
738  }
739  }
740  if (nmlist)
741  g_slist_free(nmlist);
742 
743  gsl = NULL;
744  v_g_slist_foreach(tomove,gsl,m) {
745  symbol_change_parent(specification,m,definition);
746  ++moved;
747  }
748  if (tomove)
749  g_slist_free(tomove);
750 
752  "used specification ");
753  LOGDUMPSYMBOL(8,LA_DEBUG,LF_DFILE | LF_SYMBOL,specification);
755  " to complete definition (moved %d; updated %d) ",moved,updated);
757 
758  return 0;
759 }
760 
761 static int attr_callback(Dwarf_Attribute *attrp,void *arg) {
762  struct attrcb_args *cbargs = (struct attrcb_args *)arg;
763 
764  Dwarf_Off die_offset = cbargs->die_offset;
765  const int level = cbargs->level;
766  struct symbol *symbol = cbargs->symbol;
767  struct symbol_root_dwarf *srd = cbargs->srd;
768  struct debugfile *debugfile = srd->debugfile;
769  Dwarf_Half version = srd->version;
770  uint8_t offsize = srd->offsize;
771 
772  if (unlikely(attrp == NULL)) {
773  verror("cannot get attribute: %s",dwarf_errmsg (-1));
774  return DWARF_CB_ABORT;
775  }
776 
777  unsigned int attr = attrp->code;
778  unsigned int form = attrp->form;
779 
780  if (unlikely(attr == 0)) {
781  verror("attr code was 0, aborting!\n");
782  goto errout;
783  }
784  if (unlikely(form == 0)) {
785  verror("form code was 0, aborting!\n");
786  goto errout;
787  }
788 
789  /* if form is a string */
790  char *str = NULL;
791 
792  Dwarf_Word num;
793  Dwarf_Addr addr;
794  Dwarf_Block block;
795  bool flag;
796  SMOFFSET ref = 0;
797  Dwarf_Die rref;
798 
799  uint8_t str_set = 0;
800  uint8_t str_copy = 0;
801  uint8_t num_set = 0;
802  uint8_t addr_set = 0;
803  uint8_t flag_set = 0;
804  uint8_t ref_set = 0;
805  uint8_t block_set = 0;
806  uint8_t sec_offset_set = 0;
807 
808  struct location *loc;
809 
810 #define DFE(msg) \
811  verror(" <%d><%"PRIx64"> %s: %s\n", \
812  level,die_offset,dwarf_attr_string(attr),msg);
813 
814  switch(form) {
815  case DW_FORM_string:
816  // XXX: do we need to strcpy this one? It's not in our dbg_strtab...
817  str = (char *)attrp->valp;
818  str_set = 1;
819  str_copy = 1;
820  break;
821  case DW_FORM_strp:
822  case DW_FORM_indirect:
823  //str = dwarf_formstring(attrp);
824  //str_set = 1;
825  //break;
826  if (*(attrp->valp) > (debugfile->dbg_strtablen - 1)) {
828  " <%d><%"PRIx64"> %s: dwarf str at 0x%lx not in dbg_strtab;"
829  " copying!\n",
830  level,die_offset,dwarf_attr_string(attr),
831  (unsigned long int)*(attrp->valp));
832  str_copy = 1;
833  }
834  // XXX relocation...
835  if (offsize == 4)
836  str = &debugfile->dbg_strtab[*((uint32_t *)attrp->valp)];
837  else
838  str = &debugfile->dbg_strtab[*((uint64_t *)attrp->valp)];
839 #ifdef DWDEBUG_NOUSE_STRTAB
840  str_copy = 1;
841 #else
842  str_copy = 0;
843 #endif
844  str_set = 1;
845  break;
846  case DW_FORM_addr:
847  if (unlikely(dwarf_formaddr(attrp,&addr) != 0)) {
848  DFE("could not get dwarf addr!");
849  goto errout;
850  }
851  addr_set = 1;
852  break;
853  case DW_FORM_ref_addr:
855  " <%d><%"PRIx64"> %s: cross-CU ref %"PRIxADDR"\n",
856  level,die_offset,dwarf_attr_string(attr),ref);
857  case DW_FORM_ref_udata:
858  case DW_FORM_ref8:
859  case DW_FORM_ref4:
860  case DW_FORM_ref2:
861  case DW_FORM_ref1:
862  if (unlikely(dwarf_formref_die(attrp,&rref) == NULL)) {
863  DFE("could not get dwarf die ref!");
864  goto errout;
865  }
866  ref = dwarf_dieoffset(&rref);
867  ref_set = 1;
868  break;
869 #if _INT_ELFUTILS_VERSION > 141
870  case DW_FORM_sec_offset:
871  /*
872  * Util .153, dwarf_forudata could not handle DW_FORM_sec_offset.
873  */
874 #if _INT_ELFUTILS_VERSION < 153
875  attrp->form =
876  offsize == 8 ? DW_FORM_data8 : DW_FORM_data4;
877 #endif
878  sec_offset_set = 1;
879  /* Fall through. */
880 #endif
881  case DW_FORM_udata:
882  case DW_FORM_sdata:
883  case DW_FORM_data8:
884  case DW_FORM_data4:
885  case DW_FORM_data2:
886  case DW_FORM_data1:
887  if (unlikely(dwarf_formudata(attrp,&num) != 0)) {
888  DFE("could not load dwarf num!");
889  goto errout;
890  }
891  num_set = 1;
892  break;
893 #if _INT_ELFUTILS_VERSION > 141
894  case DW_FORM_exprloc:
895 #endif
896  case DW_FORM_block4:
897  case DW_FORM_block2:
898  case DW_FORM_block1:
899  case DW_FORM_block:
900  if (unlikely(dwarf_formblock(attrp,&block) != 0)) {
901  DFE("could not load dwarf block!");
902  goto errout;
903  }
904  block_set = 1;
905  break;
906  case DW_FORM_flag:
907 #if _INT_ELFUTILS_VERSION > 141
908  case DW_FORM_flag_present:
909 #endif
910  if (unlikely(dwarf_formflag(attrp,&flag) != 0)) {
911  DFE("could not load dwarf flag!");
912  goto errout;
913  }
914  flag_set = 1;
915  break;
916  default:
918  " <%d><"PRIx64"> %s: unrecognized form %s (0x%x)!\n",
919  level,die_offset,dwarf_attr_string(attr),
920  dwarf_form_string(form),form);
921  goto errout;
922  }
923 
924  /* Quick debugging. */
926  if (str_set)
928  " <%d><%x> %s = '%s'\n",
929  (int)level,die_offset,dwarf_attr_string(attr),str);
930  else if (addr_set)
932  " <%d><%x> %s = 0x%"PRIxADDR"\n",
933  (int)level,die_offset,dwarf_attr_string(attr),addr);
934  else if (num_set)
936  " <%d><%x> %s = %d\n",
937  (int)level,die_offset,dwarf_attr_string(attr),num);
938  else if (block_set)
940  " <%d><%x> %s = 0x%"PRIxADDR"\n",
941  (int)level,die_offset,dwarf_attr_string(attr),block);
942  else if (ref_set)
944  " <%d><%x> %s = 0x%"PRIxSMOFFSET"\n",
945  (int)level,die_offset,dwarf_attr_string(attr),ref);
946  else if (flag_set)
948  " <%d><%x> %s = %d\n",
949  (int)level,die_offset,dwarf_attr_string(attr),flag);
950  else
952  " <%d><%x> %s\n",
953  (int)level,die_offset,dwarf_attr_string(attr));
954  }
955 
956 #define DAW_STR(msg) \
957  { vwarnopt(6,LA_DEBUG,LF_DWARFATTR, \
958  " <%d><%"PRIx64"> %s (%s): %s\n", \
959  level,die_offset,dwarf_attr_string(attr),str,msg); }
960 #define DAW_ADDR(msg) \
961  { vwarnopt(6,LA_DEBUG,LF_DWARFATTR, \
962  " <%d><%"PRIx64"> %s (0x%"PRIxADDR"): %s\n", \
963  level,die_offset,dwarf_attr_string(attr),addr,msg); }
964 #define DAW_NUM(msg) \
965  { vwarnopt(6,LA_DEBUG,LF_DWARFATTR, \
966  " <%d><%"PRIx64"> %s (%d): %s\n", \
967  level,die_offset,dwarf_attr_string(attr),num,msg); }
968 #define DAW_BLOCK(msg) \
969  { vwarnopt(6,LA_DEBUG,LF_DWARFATTR, \
970  " <%d><%"PRIx64"> %s (0x%"PRIxADDR"): %s\n", \
971  level,die_offset,dwarf_attr_string(attr),block,msg); }
972 #define DAW_REF(msg) \
973  { vwarnopt(6,LA_DEBUG,LF_DWARFATTR, \
974  " <%d><%"PRIx64"> %s (0x%"PRIxSMOFFSET"): %s\n", \
975  level,die_offset,dwarf_attr_string(attr),ref,msg); }
976 #define DAW_FLAG(msg) \
977  { vwarnopt(6,LA_DEBUG,LF_DWARFATTR, \
978  " <%d><%"PRIx64"> %s (%d): %s\n", \
979  level,die_offset,dwarf_attr_string(attr),flag,msg); }
980 #define DAW(msg) \
981  { vwarnopt(6,LA_DEBUG,LF_DWARFATTR, \
982  " <%d><%"PRIx64"> %s: %s\n", \
983  level,die_offset,dwarf_attr_string(attr),msg); }
984 #define DAWL(msg) \
985  { vwarnopt(16,LA_DEBUG,LF_DWARFATTR, \
986  " <%d><%"PRIx64"> %s: %s\n", \
987  level,die_offset,dwarf_attr_string(attr),msg); }
988 
989  /*
990  * Ok, finally process them!!
991  */
992  switch (attr) {
993  case DW_AT_name:
994  if (cbargs->reloading)
995  break;
996 
997  if (symbol)
998  symbol_set_name(symbol,str,str_copy);
999  else
1000  DAW_STR("bad context");
1001  break;
1002  case DW_AT_stmt_list:
1003  /* XXX: don't do line numbers yet. */
1004  if (num_set) {
1005  cbargs->stmt_list_offset = num;
1006  cbargs->have_stmt_list_offset = 1;
1007  }
1008  else
1009  DAW("bad form");
1010  break;
1011  case DW_AT_producer:
1012  if (level == 0)
1013  symbol_set_root_producer(symbol,str,str_copy);
1014  else
1015  DAW_STR("bad context");
1016  break;
1017  case DW_AT_comp_dir:
1018  if (level == 0)
1019  symbol_set_root_compdir(symbol,str,str_copy);
1020  else
1021  DAW_STR("bad context");
1022  break;
1023  case DW_AT_language:
1024  if (level == 0)
1025  symbol_set_root_language(symbol,dwarf_language_string(num),0,num);
1026  else
1027  DAW_NUM("bad context");
1028  break;
1029  case DW_AT_low_pc:
1030  /* Just stash it here; do the updating in attr postprocessing. */
1031  cbargs->lowpc = addr;
1032  cbargs->lowpc_set = 1;
1033  break;
1034  case DW_AT_high_pc:
1035  if (num_set) {
1036  /* Just stash it and postprocess after all attrs processed. */
1037  cbargs->highpc = num;
1038  cbargs->highpc_set = 1;
1039  cbargs->highpc_is_offset = 1;
1040  }
1041  else if (addr_set) {
1042  /* Just stash it and postprocess after all attrs processed. */
1043  cbargs->highpc = addr;
1044  cbargs->highpc_set = 1;
1045  cbargs->highpc_is_offset = 0;
1046  }
1047  else
1048  DAW("bad form");
1049  break;
1050  case DW_AT_entry_pc:
1051  if (addr_set) {
1052  if (SYMBOL_IS_FUNC(symbol) || SYMBOL_IS_ROOT(symbol))
1053  symbol_set_entry_pc(symbol,addr);
1054  else
1055  DAW_ADDR("bad context");
1056  }
1057  else
1058  DAW("bad form");
1059  break;
1060  case DW_AT_decl_file:
1061  if (symbol) {
1062  ; // XXX
1063  }
1064  else
1065  DAW("bad context");
1066  break;
1067  case DW_AT_decl_line:
1068  if (symbol)
1069  symbol_set_srcline(symbol,(int)num);
1070  else
1071  DAW("bad context");
1072  break;
1073  /* Don't bother with these yet. */
1074  case DW_AT_decl_column:
1075  case DW_AT_call_file:
1076  case DW_AT_call_line:
1077  case DW_AT_call_column:
1078  break;
1079  case DW_AT_encoding:
1080  if (symbol && SYMBOL_IST_BASE(symbol)) {
1081  /* our encoding_t is 1<->1 map to the DWARF encoding codes. */
1082  symbol_set_encoding(symbol,(encoding_t)num);
1083  }
1084  else
1085  DAW("bad context");
1086  break;
1087  case DW_AT_declaration:
1088  if (symbol)
1089  symbol->isdeclaration = flag;
1090  else
1091  DAW("bad context");
1092  break;
1093  case DW_AT_external:
1094  if (symbol
1095  && (SYMBOL_IS_FUNC(symbol) || SYMBOL_IS_VAR(symbol))) {
1096  /*
1097  * C++ struct/class members marked with AT_external are
1098  * really static data members; do not mark them external.
1099  */
1100  if (SYMBOL_IS_VAR(symbol)
1101  && cbargs->parentsymbol
1102  && SYMBOL_IST_STUNC(cbargs->parentsymbol))
1103  symbol->isexternal = 0;
1104  else
1105  symbol->isexternal = flag;
1106  }
1107  else if (symbol && SYMBOL_IST_FUNC(symbol)) {
1108  symbol->isexternal = flag;
1109  }
1110  else
1111  DAW("bad context");
1112  break;
1113  case DW_AT_linkage_name:
1114  case DW_AT_MIPS_linkage_name:
1115  if (symbol) {
1116  /*
1117  * We need to record this so that we don't mark AT_external
1118  * vars as globals, if they have external linkage. We just
1119  * ignore AT_external for stuff that has external linkage
1120  * names; we don't try to put an alias in for the linkage
1121  * symbol -- we're not a linker and users won't care!
1122  */
1123  symbol->has_linkage_name = 1;
1124  }
1125  else
1126  DAW("bad context");
1127  break;
1128  case DW_AT_prototyped:
1129  if (symbol && (SYMBOL_IS_FUNC(symbol) || SYMBOL_IST_FUNC(symbol)))
1130  symbol->isprototyped = flag;
1131  else
1132  DAW("bad context");
1133  break;
1134  case DW_AT_inline:
1135  if (num_set && symbol && SYMBOL_IS_FUNC(symbol)) {
1136  if (num == 1)
1137  symbol_set_inline_info(symbol,1,0);
1138  else if (num == 2)
1139  symbol_set_inline_info(symbol,0,1);
1140  else if (num == 3)
1141  symbol_set_inline_info(symbol,1,1);
1142  else if (num == 0)
1143  ; /* Ignore; same as not decl nor inlined; spec v4 p. 59 */
1144  else
1145  DAW_NUM("bad inline number");
1146  }
1147  else
1148  DAW("bad context");
1149  break;
1150  case DW_AT_abstract_origin:
1151  if (ref_set && SYMBOL_IS_INLINEABLE(symbol)) {
1152  if (symbol_set_inline_origin(symbol,ref,NULL)) {
1153  DAW_REF("failed to set inline origin");
1154  break;
1155  }
1156 
1157  /* Record it as needing resolution in our CU table. */
1158  dwarf_symbol_set_origin_ref(srd,symbol,ref);
1159  }
1160  else
1161  DAW("bad context");
1162  break;
1163  case DW_AT_type:
1164  if (cbargs->reloading)
1165  break;
1166 
1167  if (ref_set && symbol) {
1168  if (SYMBOL_IS_TYPE(symbol)) {
1169  if (SYMBOL_IST_PTR(symbol) || SYMBOL_IST_TYPEDEF(symbol)
1170  || SYMBOL_IST_ARRAY(symbol) || SYMBOL_IST_CONST(symbol)
1171  || SYMBOL_IST_VOL(symbol) || SYMBOL_IST_FUNC(symbol)) {
1172  dwarf_symbol_set_datatype_ref(srd,symbol,ref);
1173  }
1174  else
1175  DAW_REF("bogus: type ref for unknown type symbol");
1176  }
1177  else {
1178  dwarf_symbol_set_datatype_ref(srd,symbol,ref);
1179  }
1180  }
1181  else if (ref_set && !symbol && cbargs->parentsymbol
1182  && SYMBOL_IST_ARRAY(cbargs->parentsymbol)) {
1183  /*
1184  * If the parent was an array_type, don't worry about typing
1185  * its array subranges.
1186  */
1187  ;
1188  }
1189  else
1190  DAW("bad context");
1191  break;
1192  case DW_AT_const_value:
1193  if (!symbol || !SYMBOL_IS_VAR(symbol)) {
1194  DAW("bad context");
1195  break;
1196  }
1197  else if (num_set && symbol->datatype
1198  && symbol_get_bytesize(symbol->datatype) > 0) {
1199  symbol_set_constval(symbol,(void *)&num,
1200  symbol_get_bytesize(symbol->datatype),
1201  1);
1202  }
1203  else if (num_set && SYMBOL_IS_VAR(symbol)) {
1204  /*
1205  * XXX: just use a 64 bit unsigned for now, since we may not
1206  * have seen the type for this symbol yet. We can always
1207  * deal with it later.
1208  */
1209  symbol_set_constval(symbol,(void *)&num,sizeof(Dwarf_Word),1);
1210  }
1211  else if (str_set) {
1212  if (str_copy)
1213  symbol_set_constval(symbol,str,strlen(str) + 1,1);
1214  else
1215  symbol_set_constval(symbol,str,-1,0);
1216  }
1217  else if (block_set)
1218  symbol_set_constval(symbol,block.data,block.length,1);
1219  else
1220  DAW("bad context");
1221  break;
1222  /*
1223  * XXX: byte/bit sizes/offsets can technically be a reference
1224  * to another DIE, or an exprloc... but they should always be
1225  * consts for C!
1226  */
1227  case DW_AT_byte_size:
1228  if (!symbol) {
1229  DAW("bad context");
1230  break;
1231  }
1232  else if (num_set)
1233  symbol_set_bytesize(symbol,num);
1234  else
1235  DAW("bad form");
1236  break;
1237  case DW_AT_bit_size:
1238  if (!symbol) {
1239  DAW("bad context");
1240  break;
1241  }
1242  else if (num_set)
1243  symbol_set_bitsize(symbol,num);
1244  else
1245  DAW("bad form");
1246  break;
1247  case DW_AT_bit_offset:
1248  if (!symbol) {
1249  DAW("bad context");
1250  break;
1251  }
1252  else if (num_set)
1253  symbol_set_bitoffset(symbol,num);
1254  else
1255  DAW("bad form");
1256  break;
1257  case DW_AT_sibling:
1258  if (ref_set)
1259  cbargs->sibling = ref;
1260  else
1261  DAW("bad form; expected ref");
1262  break;
1263  case DW_AT_data_member_location:
1264  if (!symbol || !SYMBOL_IS_VAR(symbol)) {
1265  DAW("bad context");
1266  break;
1267  }
1268 
1269  /* In V3 or V4, this can be either an exprloc, loclistptr, or a
1270  * constant. In V2, it can only be an exprloc (block) or a
1271  * loclistptr (reference).
1272  *
1273  * We know if block_set, it is an exprloc (see above in form
1274  * processing), regardless of V2, V3, or V4.
1275  *
1276  * However, if the version is DWARF3, we cannot tell the
1277  * difference between a constant and loclistptr if the form is
1278  * FORM_data4 or FORM_data8 (the comment in V3 spec, p. 140 is:
1279  * Because classes lineptr, loclistptr, macptr and
1280  * rangelistptr share a common representation, it is not
1281  * possible for an attribute to allow more than one of these
1282  * classes. If an attribute allows both class constant and
1283  * one of lineptr, loclistptr, macptr or rangelistptr, then
1284  * DW_FORM_data4 and DW_FORM_data8 are interpreted as members
1285  * of the latter as appropriate (not class constant).
1286  * ). This rule seems awfully bad to me, but we follow it!
1287  *
1288  * Then, if the version is 4 and we see a FORM_data4 or
1289  * FORM_data8, we take it as a constant. This seems to be the
1290  * correct behavior, because the V4 spec does not include the
1291  * above caveat. For V4, we only consider it a loclistptr if
1292  * FORM_sec_offset was used.
1293  *
1294  * NB: This *also means that if you use this library on a DWARF4
1295  * file, but your elfutils does not support FORM_sec_offset,
1296  * things will break!!!
1297  */
1298  loc = NULL;
1299  if (block_set) {
1300  if (symbol->ismember) {
1301  loc = dwarf_get_static_ops(srd,block.data,block.length,attr);
1302  if (!loc) {
1303  DAW_BLOCK("failed get_static_ops");
1304  }
1305  else if (symbol_set_location(symbol,loc)) {
1306  DAW_BLOCK("failed symbol_set_location");
1307  location_free(loc);
1308  }
1309  }
1310  else
1311  DAW_BLOCK("nonmember symbol");
1312  }
1313  else if (num_set) {
1314  if ((version == 3 && (form == DW_FORM_data4
1315  || form == DW_FORM_data8))
1316  || (version >= 4 && sec_offset_set)) {
1317  if (symbol->ismember) {
1318  loc = dwarf_get_loclistloc(srd,attr,num);
1319  if (!loc)
1320  DAW_NUM("failed get_loclistloc");
1321  }
1322  else
1323  DAW_BLOCK("nonmember symbol");
1324  }
1325  else {
1326  /* it's a constant */
1327  loc = location_create();
1328  location_set_member_offset(loc,(int32_t)num);
1329  }
1330 
1331  if (loc && symbol_set_location(symbol,loc)) {
1332  DAW_NUM("failed symbol_set_location");
1333  location_free(loc);
1334  }
1335  }
1336  else
1337  DAW("bad form");
1338  break;
1339  case DW_AT_frame_base:
1340  if (!symbol || !SYMBOL_IS_FUNC(symbol)) {
1341  DAW("bad context");
1342  break;
1343  }
1344 
1345  loc = NULL;
1346  /* if it's a loclist */
1347  if (num_set && (form == DW_FORM_data4 || form == DW_FORM_data8)) {
1348  loc = dwarf_get_loclistloc(srd,attr,num);
1349  if (!loc) {
1350  DAW_NUM("failed get_loclistloc");
1351  break;
1352  }
1353  SYMBOL_WX_FUNC(symbol,sf,-1);
1354  sf->fbloc = loc;
1355  }
1356  /* if it's an exprloc in a block */
1357  else if (block_set) {
1358  loc = dwarf_get_static_ops(srd,block.data,block.length,attr);
1359  if (!loc) {
1360  DAW_BLOCK("failed to get_static_ops");
1361  break;
1362  }
1363  SYMBOL_WX_FUNC(symbol,sf,-1);
1364  sf->fbloc = loc;
1365  }
1366  else
1367  DAW("frame_base not num/block");
1368  break;
1369  case DW_AT_ranges:
1370  /* always a rangelistptr */
1371  if (num_set && (sec_offset_set
1372  || form == DW_FORM_data4
1373  || form == DW_FORM_data8)) {
1374  cbargs->ranges = dwarf_get_ranges(srd,attr,num);
1375  if (!cbargs->ranges) {
1376  DAW_NUM("failed to get_ranges");
1377  break;
1378  }
1379  }
1380  else
1381  DAW("bad form");
1382  break;
1383  case DW_AT_location:
1384  /* We only accept this for params and variables */
1385  if (!symbol || !SYMBOL_IS_VAR(symbol)) {
1386  DAW("bad context");
1387  break;
1388  }
1389 
1390  if (num_set && (sec_offset_set
1391  || form == DW_FORM_data4
1392  || form == DW_FORM_data8)) {
1393  loc = dwarf_get_loclistloc(srd,attr,num);
1394  if (!loc) {
1395  DAW_NUM("failed to get_loclistloc");
1396  break;
1397  }
1398  else if (symbol_set_location(symbol,loc)) {
1399  DAW_NUM("failed to set_location!");
1400  location_free(loc);
1401  break;
1402  }
1403  }
1404  else if (block_set) {
1405  loc = dwarf_get_static_ops(srd,block.data,block.length,attr);
1406  if (!loc) {
1407  DAW("failed to get_static_ops");
1408  break;
1409  }
1410  else if (symbol_set_location(symbol,loc)) {
1411  DAW_NUM("failed to set_location!");
1412  location_free(loc);
1413  break;
1414  }
1415  }
1416  else
1417  DAW("bad form");
1418  break;
1419  case DW_AT_lower_bound:
1420  if (num_set && num) {
1421  DAW_NUM("we only support lower_bound attrs of 0");
1422  }
1423  else
1424  DAW("bad attr/form");
1425  break;
1426  case DW_AT_count:
1427  //DAW("interpreting AT_count as AT_upper_bound");
1428  case DW_AT_upper_bound:
1429  /* it's a constant, not a block op */
1430  if (num_set && !sec_offset_set) {
1431  if (!symbol && cbargs->parentsymbol
1432  && SYMBOL_IST_ARRAY(cbargs->parentsymbol)) {
1433  symbol_add_subrange(cbargs->parentsymbol,(int)num);
1434  }
1435  else
1436  DAW_NUM("non array-type parent or bad context");
1437  break;
1438  }
1439  else
1440  DAW("bad attr/form");
1441  break;
1442  case DW_AT_specification:
1443  if (ref_set && symbol) {
1444  cbargs->specification_ref = ref;
1445  cbargs->specification_set = 1;
1446  }
1447  else
1448  DAW("bad context/form");
1449  break;
1450  case DW_AT_import:
1451  if (ref_set && !symbol) {
1452  cbargs->ref = ref;
1453  cbargs->ref_set = 1;
1454  }
1455  else
1456  DAW("bad context/form");
1457  break;
1458  /* Skip these things. */
1459  case DW_AT_artificial:
1460  break;
1461  /* Skip DW_AT_GNU_vector, which not all elfutils versions know about. */
1462  case 8455:
1463  break;
1464 
1465  /* Skip a few that we might add support for later. */
1466  case DW_AT_object_pointer:
1467  case DW_AT_accessibility:
1468  case DW_AT_containing_type:
1469  case DW_AT_virtuality:
1470  case DW_AT_vtable_elem_location:
1471  case DW_AT_explicit:
1472  DAWL("known unhandled");
1473  break;
1474 
1475  default:
1476  DAW("unrecognized");
1477  //goto errout;
1478  break;
1479  }
1480 
1481  goto out;
1482 
1483  errout:
1484  return DWARF_CB_ABORT;
1485  out:
1486  return 0;
1487 }
1488 
1489 static inline char *dwarf_language_string(int language) {
1490  switch (language) {
1491  case DW_LANG_C89: return "C89";
1492  case DW_LANG_C: return "C";
1493  case DW_LANG_Ada83: return "Ada83";
1494  case DW_LANG_C_plus_plus: return "C++";
1495  case DW_LANG_Cobol74: return "Cobol74";
1496  case DW_LANG_Cobol85: return "Cobol85";
1497  case DW_LANG_Fortran77: return "Fortran77";
1498  case DW_LANG_Fortran90: return "Fortran90";
1499  case DW_LANG_Pascal83: return "Pascal83";
1500  case DW_LANG_Modula2: return "Modula2";
1501  case DW_LANG_Java: return "Java";
1502  case DW_LANG_C99: return "C99";
1503  case DW_LANG_Ada95: return "Ada95";
1504  case DW_LANG_Fortran95: return "Fortran95";
1505  case DW_LANG_PL1: return "PL/1";
1506 #if _INT_ELFUTILS_VERSION >= 155
1507  case DW_LANG_ObjC: return "ObjectiveC";
1508 #else
1509  case DW_LANG_Objc: return "ObjectiveC";
1510 #endif
1511  case DW_LANG_ObjC_plus_plus: return "ObjectiveC++";
1512  case DW_LANG_UPC: return "UnifiedParallelC";
1513  case DW_LANG_D: return "D";
1514 #if _INT_ELFUTILS_VERSION > 147
1515  case DW_LANG_Python: return "Python";
1516 #endif
1517 #if _INT_ELFUTILS_VERSION > 149
1518  case DW_LANG_Go: return "Go";
1519 #endif
1520  case DW_LANG_Mips_Assembler: return "Assembler";
1521  default: return NULL;
1522  }
1523 }
1524 
1525 static struct range *dwarf_get_ranges(struct symbol_root_dwarf *srd,
1526  unsigned int attr,Dwarf_Word offset) {
1527  struct debugfile *debugfile = srd->debugfile;
1528  unsigned int addrsize = srd->addrsize;
1529  char *readp;
1530  char *endp;
1531  ptrdiff_t loffset;
1532  Dwarf_Addr begin;
1533  Dwarf_Addr end;
1534  int len = 0;
1535  int have_base = 0;
1536  Dwarf_Addr base = 0;
1537  struct range *retval = NULL, *lastr = NULL;
1538  ADDR cu_base;
1539 
1540  /* XXX: we can't get other_byte_order from dbg since we don't have
1541  * the struct def for it... so we assume it's not a diff byte order
1542  * than the phys host for now.
1543  */
1544  int obo = 0;
1545 
1546  if (!debugfile->rangetab || offset > debugfile->rangetablen) {
1547  errno = EFAULT;
1548  return NULL;
1549  }
1550 
1551  cu_base = symbol_get_addr(srd->root);
1552 
1553  readp = debugfile->rangetab + offset;
1554  endp = debugfile->rangetab + debugfile->rangetablen;
1555 
1556  vdebug(25,LA_DEBUG,LF_DLOC,"starting (rangetab len %d, offset %d)\n",
1557  debugfile->rangetablen,offset);
1558 
1559  while (readp < endp) {
1560  loffset = readp - debugfile->rangetab;
1561 
1562  if (unlikely((debugfile->rangetablen - loffset) < addrsize * 2)) {
1563  verror("[%6tx] invalid loclist entry\n",loffset);
1564  break;
1565  }
1566 
1567  if (addrsize == 8) {
1568  begin = read_8ubyte_unaligned_inc(obo,readp);
1569  end = read_8ubyte_unaligned_inc(obo,readp);
1570  }
1571  else {
1572  begin = read_4ubyte_unaligned_inc(obo,readp);
1573  end = read_4ubyte_unaligned_inc(obo,readp);
1574  if (begin == (Dwarf_Addr)(uint32_t)-1)
1575  begin = (Dwarf_Addr)-1l;
1576  }
1577 
1578  if (begin == (Dwarf_Addr)-1l) {
1579  /* Base address entry. */
1580  vdebug(25,LA_DEBUG,LF_DLOC,"[%6tx] base address 0x%" PRIxADDR "\n",
1581  loffset,end);
1582  have_base = 1;
1583  base = end;
1584  }
1585  else if (begin == 0 && end == 0) {
1586  /* End of list entry. */
1587  if (len == 0)
1588  vwarnopt(25,LA_DEBUG,LF_DLOC,"[%6tx] empty list\n",loffset);
1589  else
1590  vdebug(25,LA_DEBUG,LF_DLOC,"[%6tx] end of list\n",loffset);
1591  break;
1592  }
1593  else {
1594  ++len;
1595 
1596  /* We have a range entry. */
1597  if (!retval) {
1598  retval = calloc(1,sizeof(*retval));
1599  retval->start = (have_base) ? begin + base : begin + cu_base;
1600  retval->end = (have_base) ? end + base : end + cu_base;
1601  lastr = retval;
1602  }
1603  else {
1604  lastr->next = calloc(1,sizeof(*lastr));
1605  lastr->next->start = (have_base) ? begin + base : begin + cu_base;
1606  lastr->next->end = (have_base) ? end + base : end + cu_base;
1607  lastr = lastr->next;
1608  }
1609  }
1610  }
1611 
1612  return retval;
1613 }
1614 
1615 static struct location *dwarf_get_loclistloc(struct symbol_root_dwarf *srd,
1616  unsigned int attr,
1617  Dwarf_Word offset) {
1618  struct debugfile *debugfile = srd->debugfile;
1619  unsigned int addrsize = srd->addrsize;
1620  ADDR cu_base;
1621  char *readp;
1622  char *endp;
1623  ptrdiff_t loffset;
1624  Dwarf_Addr begin;
1625  Dwarf_Addr end;
1626  int len = 0;
1627  uint16_t exprlen;
1628  int have_base = 0;
1629  Dwarf_Addr base = 0;
1630  struct location *tmploc = NULL;
1631  struct location *retval = NULL;
1632  ADDR rbegin,rend;
1633 
1634  /* XXX: we can't get other_byte_order from dbg since we don't have
1635  * the struct def for it... so we assume it's not a diff byte order
1636  * than the phys host for now.
1637  */
1638  int obo = 0;
1639 
1640  if (!debugfile->loctab
1641  || offset > debugfile->loctablen) {
1642  errno = EFAULT;
1643  return NULL;
1644  }
1645 
1646  cu_base = symbol_get_addr(srd->root);
1647 
1648  readp = debugfile->loctab + offset;
1649  endp = debugfile->loctab + debugfile->loctablen;
1650 
1651  vdebug(25,LA_DEBUG,LF_DLOC,"starting (loctab len %d, offset %d)\n",
1652  debugfile->loctablen,offset);
1653 
1654  retval = location_create();
1655 
1656  while (readp < endp) {
1657  loffset = readp - debugfile->loctab;
1658 
1659  if (unlikely((debugfile->loctablen - loffset) < addrsize * 2)) {
1660  verror("[%6tx] invalid loclist entry\n",loffset);
1661  goto errout;
1662  }
1663 
1664  if (addrsize == 8) {
1665  begin = read_8ubyte_unaligned_inc(obo,readp);
1666  end = read_8ubyte_unaligned_inc(obo,readp);
1667  }
1668  else {
1669  begin = read_4ubyte_unaligned_inc(obo,readp);
1670  end = read_4ubyte_unaligned_inc(obo,readp);
1671  if (begin == (Dwarf_Addr)(uint32_t)-1)
1672  begin = (Dwarf_Addr)-1l;
1673  }
1674 
1675  if (begin == (Dwarf_Addr)-1l) {
1676  /* Base address entry. */
1677  vdebug(25,LA_DEBUG,LF_DLOC,"[%6tx] base address 0x%" PRIxADDR "\n",
1678  loffset,end);
1679  have_base = 1;
1680  base = end;
1681  }
1682  else if (begin == 0 && end == 0) {
1683  /* End of list entry. */
1684  if (len == 0)
1686  "[%6tx] empty list\n",loffset);
1687  else
1688  vdebug(25,LA_DEBUG,LF_DLOC,"[%6tx] end of list\n");
1689  break;
1690  }
1691  else {
1692  ++len;
1693 
1694  /* We have a location expression entry. */
1695  exprlen = read_2ubyte_unaligned_inc(obo,readp);
1696 
1697  if (endp - readp <= (ptrdiff_t) exprlen) {
1698  verror("[%6tx] invalid exprlen (%hd) in entry\n",loffset,exprlen);
1699  goto errout;
1700  }
1701 
1702  /* GCC apparently produces these meaningless entries; ignore them! */
1703  if (begin == end) {
1705  "[%6tx] begin (0x%"PRIxADDR") == end (0x%"PRIxADDR")\n",
1706  loffset,begin,end);
1707  goto cont;
1708  }
1709 
1710  vdebug(25,LA_DEBUG,LF_DLOC,
1711  "[%6tx] loc expr range(0x%"PRIxADDR",0x%"PRIxADDR") len %hd\n",
1712  loffset,begin,end,exprlen);
1713 
1714  tmploc = dwarf_get_static_ops(srd,(const unsigned char *)readp,
1715  exprlen,attr);
1716 
1717  if (!tmploc) {
1719  "get_static_ops (%d) failed!\n",exprlen);
1720  goto errout;
1721  }
1722  else {
1723  vdebug(25,LA_DEBUG,LF_DLOC,
1724  "get_static_ops (%d) succeeded!\n",exprlen);
1725  }
1726 
1727  rbegin = (have_base) ? begin + base : begin + cu_base;
1728  rend = (have_base) ? end + base : end + cu_base;
1729  if (location_update_loclist(retval,rbegin,rend,tmploc,NULL)) {
1730  verror("location_update_loclist failed!\n");
1731  goto errout;
1732  }
1733 
1734  cont:
1735  readp += exprlen;
1736  }
1737  }
1738 
1739  return retval;
1740 
1741  errout:
1742  if (tmploc)
1743  location_free(tmploc);
1744  if (retval)
1745  location_free(retval);
1746  return NULL;
1747 }
1748 
1749 /* Used in fill_debuginfo; defined right afer it for ease of
1750  * understanding the code.
1751  */
1752 
1753 void finalize_die_symbol_name(struct symbol *symbol);
1754 void finalize_die_symbol(struct debugfile *debugfile,int level,
1755  struct symbol *symbol,
1756  struct symbol *parentsymbol,
1757  struct symbol *voidsymbol,
1758  GHashTable *reftab,struct array_list *die_offsets,
1759  SMOFFSET cu_offset);
1760 
1761 struct symbol *do_void_symbol(struct debugfile *debugfile,
1762  struct symbol *root) {
1763  /* symbol_create dups the name, so we just pass a static buf */
1764  struct symbol *symbol;
1765 
1766  symbol = symbol_get_sym(root,"void",SYMBOL_TYPE_FLAG_TYPE);
1767  if (symbol)
1768  return symbol;
1769 
1772  symbol->datatype_code = DATATYPE_VOID;
1773 
1774  /* Always put it in its primary symtab, of course -- probably the CU's. */
1775  symbol_insert_symbol(root,symbol);
1776 
1777  /* And also always put it in the debugfile's global types table. */
1778  if (!(debugfile->opts->flags & DEBUGFILE_LOAD_FLAG_REDUCETYPES))
1779  debugfile_add_type(debugfile,symbol);
1780 
1781  return symbol;
1782 }
1783 
1784 struct symbol *do_word_symbol(struct debugfile *debugfile,
1785  struct symbol *root) {
1786  struct symbol_root_dwarf *srd = SYMBOLX_ROOT(root)->priv;
1787  /* symbol_create dups the name, so we just pass a static buf */
1788  struct symbol *symbol;
1789 
1790  symbol = symbol_get_sym(root,"long unsigned int",SYMBOL_TYPE_FLAG_TYPE);
1791  if (symbol)
1792  return symbol;
1793 
1795  "long unsigned int",0,0,LOADTYPE_FULL,
1796  symbol_read_owned_scope(root));
1797  symbol->datatype_code = DATATYPE_BASE;
1798  symbol_set_bytesize(symbol,srd->addrsize);
1800 
1801  /* Always put it in its primary symtab, of course -- probably the CU's. */
1802  symbol_insert_symbol(root,symbol);
1803 
1804  /* And also always put it in the debugfile's global types table. */
1805  if (!(debugfile->opts->flags & DEBUGFILE_LOAD_FLAG_REDUCETYPES))
1806  debugfile_add_type(debugfile,symbol);
1807 
1808  return symbol;
1809 }
1810 
1811 /*
1812  * If you specify @die_offsets, you can specify that those DIEs should
1813  * be expanded if they've already been partially loaded, by setting
1814  * @expand_dies nonzero.
1815  *
1816  * XXX: a major bad thing we do is reduce the Dwarf_Off offset to a
1817  * 32-bit value, instead of a (potential) 64-bit value. However, doing
1818  * this saves us lots of struct bytes, and if anybody supplies a
1819  * debuginfo file > 4GB in size, we're just not going to support it.
1820  */
1821 static int dwarf_load_cu(struct symbol_root_dwarf *srd,
1822  Dwarf_Off *cu_offset,
1823  struct array_list *init_die_offsets,int expand_dies) {
1824  Dwarf *dbg = srd->dbg;
1825  struct debugfile *debugfile = srd->debugfile;
1826  struct debugfile_load_opts *dopts = debugfile->opts;
1827  int retval = 0;
1828  Dwarf_Off offset = *cu_offset;
1829  struct attrcb_args args;
1830  int maxdies = 8;
1831  int level = 0;
1832  Dwarf_Die *dies = (Dwarf_Die *)malloc(maxdies*sizeof(Dwarf_Die));
1833 
1834  struct symbol *root;
1835  struct scope *root_scope;
1836  struct symbol **symbols = (struct symbol **) \
1837  calloc(maxdies,sizeof(struct symbol *));
1838  struct scope **scopes = (struct scope **) \
1839  calloc(maxdies,sizeof(struct scope *));
1840  struct symbol **imported_modules = (struct symbol **) \
1841  calloc(maxdies,sizeof(struct symbol *));
1842 
1843  struct symbol *voidsymbol;
1844  struct symbol *rsymbol;
1845  int quick = dopts->flags & DEBUGFILE_LOAD_FLAG_PARTIALSYM;
1846  load_type_t loadtype =
1847  (!quick || expand_dies) ? LOADTYPE_FULL : LOADTYPE_PARTIAL;
1848  Dwarf_Off until_die_offset = 0;
1849  Dwarf_Die top_level_first_die;
1850  /*
1851  * XXX: what if we are using a CU symbol created in get_aranges or
1852  * get_pubnames, and we don't end up hashing the symbol in
1853  * debugfiles->srcfiles because it doesn't have a name? What about
1854  * symbols from aranges or pubnames that aren't in debuginfo? Those
1855  * will be leaked. Ack, don't worry about it for now; that would be
1856  * a DWARF bug :).
1857  */
1858  int root_added = 0;
1859  int root_preexisting = 0;
1860 
1861  int have_stmt_list_offset = 0;
1862  Dwarf_Word cu_stmt_list_offset = 0;
1863 
1864  struct array_list *die_offsets = NULL;
1865  int i;
1866  int tmplpc;
1867  struct symbol *tsymbol;
1868  char *sname;
1869  int accept;
1870  gpointer key;
1871  int trefcnt;
1872  struct scope *reparent_scope;
1873  struct scope *specification_scope;
1874  struct symbol *specification_symbol;
1875  struct symbol *specification_symbol_parent;
1876 
1877  /*
1878  * If we only want to load specific die offsets, clone the incoming
1879  * list so we can append to it as we discover necessary!
1880  */
1881  if (init_die_offsets)
1882  die_offsets = array_list_clone(init_die_offsets,0);
1883 
1884  /*
1885  * Set up the root variable. CU symbols go in two places in
1886  * the debugfile struct: in ->cuoffsets (with the CU offset as key),
1887  * and in ->srcfiles (with the CU name as the key). When they are
1888  * created in get_aranges, we don't yet know their name, so they are
1889  * only in ->cuoffsets. If we see one of these, we know this is the
1890  * initial load of the CU, and we have to process the CU DIE.
1891  * Otherwise, we can skip *processing* the CU die -- we still have
1892  * to load it in dies[].
1893  */
1894 
1895  root = (struct symbol *)debugfile_lookup_root(debugfile,offset);
1896  if (!root) {
1898  "creating new CU symbol at 0x%"PRIx64"!\n",offset);
1899 
1900  /* attr_callback has to fill root, and *MUST* fill at least
1901  * the name field; otherwise we can't add the symbol to our hash table.
1902  */
1904  NULL,0,(SMOFFSET)offset,loadtype,NULL);
1905  symbol_set_root_priv(root,srd);
1906  srd->root = root;
1907  srd->debugfile = debugfile;
1908  debugfile_insert_root(debugfile,root);
1909 
1910  /* Set the top-level scope. */
1911  scopes[0] = root_scope = symbol_write_owned_scope(root);
1912  }
1913  else {
1915  "using existing CU symbol %s (offset 0x%"PRIx64")!\n",
1916  symbol_get_name(root),offset);
1917 
1918  /*
1919  * Make sure, if we got a root symbol from dwarf_load_aranges,
1920  * that it has our metadata recorded, and vice versa.
1921  */
1922  if (!srd->root) {
1923  srd->root = root;
1924  srd->debugfile = debugfile;
1925  }
1926  if (!SYMBOLX_ROOT(root)->priv)
1927  symbol_set_root_priv(root,srd);
1928 
1929  root_preexisting = 1;
1930 
1931  symbols[0] = root;
1932  /* Get the top-level scope. */
1933  scopes[0] = root_scope = symbol_write_owned_scope(root);
1934  }
1935 
1936  /* Set the load type -- the expected, no-errors state :)! */
1937  if (root->loadtag == LOADTYPE_UNLOADED
1938  && (die_offsets || dopts->flags & DEBUGFILE_LOAD_FLAG_PARTIALSYM))
1939  root->loadtag = LOADTYPE_PARTIAL;
1940  else if (root->loadtag == LOADTYPE_PARTIAL
1941  && (die_offsets || dopts->flags & DEBUGFILE_LOAD_FLAG_PARTIALSYM))
1942  root->loadtag = LOADTYPE_PARTIAL;
1943  else if (root->loadtag == LOADTYPE_FULL && die_offsets) {
1944  verror("CU %s (offset 0x%"PRIxSMOFFSET") already fully loaded!\n",
1945  root->name,root->ref);
1946  goto errout;
1947  }
1948  else
1949  root->loadtag = LOADTYPE_FULL;
1950 
1951  /* If we've loaded this one before, we don't want to add it again
1952  * (which we can only do after processing its DIE attrs, the first
1953  * time); so say here, don't add it again!
1954  */
1955  if (root->name
1956  && (g_hash_table_lookup(debugfile->srcfiles,root->name)
1957  || g_hash_table_lookup(debugfile->srcfiles_multiuse,root->name))) {
1958  root_added = 1;
1959  }
1960 
1961  /* Either create the void symbol for this CU, or grab it from
1962  * elsewhere.
1963  */
1964  voidsymbol = do_void_symbol(debugfile,root);
1965 
1966  /* Setup our args for attr_callback! */
1967  args.srd = srd;
1968 
1969  args.level = level;
1970  args.cu_offset = offset;
1971  args.have_stmt_list_offset = 0;
1972  args.stmt_list_offset = 0;
1973 
1974  args.symbol = NULL;
1975  args.parentsymbol = NULL;
1976  args.voidsymbol = voidsymbol;
1977 
1978  /* Skip the CU header. */
1979  offset += srd->cuhl;
1980 
1981  /* If we are doing a partial CU load, we still have to parse the CU
1982  * DIE's attributes! So, we have to hold the skip to the first
1983  * offset in die_offsets until we've done that.
1984  */
1985 
1986  if (dwarf_offdie(dbg,offset,&dies[level]) == NULL) {
1987  verror("cannot get DIE at offset %" PRIx64 ": %s\n",
1988  offset,dwarf_errmsg(-1));
1989  goto errout;
1990  }
1991 
2013  do {
2014  struct scope *newscope;
2015  struct symbol *ts;
2016  int nofinalize;
2017  int action;
2018  ADDR highpc;
2019  int tag;
2020 
2021  offset = dwarf_dieoffset(&dies[level]);
2022  if (offset == ~0ul) {
2023  verror("cannot get DIE offset: %s",dwarf_errmsg(-1));
2024  goto errout;
2025  }
2026 
2027  /* Add to the top_level_dies_offset range list. */
2028  if (dopts->flags & DEBUGFILE_LOAD_FLAG_PARTIALSYM && level == 1) {
2029  /* And don't worry if it's already in the list. */
2031  (void *)(uintptr_t)offset);
2032  vdebug(6,LA_DEBUG,LF_DWARF,"added top level DIE 0x%x\n",offset);
2033  }
2034 
2035  /* Initialize some defaults for this iteration. */
2036  args.reloading = 0;
2037  symbols[level] = NULL;
2038  newscope = NULL;
2039  nofinalize = 0;
2040 
2041  /*
2042  * If the offset is already in reftab, AND if it's a FULL
2043  * symbol, skip to its sibling; don't process either its
2044  * attributes nor its children.
2045  */
2046  ts = (struct symbol *) \
2047  g_hash_table_lookup(srd->reftab,(gpointer)(uintptr_t)offset);
2048  if (ts && SYMBOL_IS_FULL(ts)) {
2049  /*
2050  * This is tricky. Set up its "parent" if it had one, just
2051  * in case the sibling (if we process one) needs it. WAIT
2052  * -- you might think you need to do that, but you don't
2053  * because the only way you process a sibling of something
2054  * that was already processed is to get to it is to have
2055  * already processed its parent in this loop (or loaded
2056  * symbols/scopes[level] at level - 1 from reftab in an
2057  * earlier iteration of this loop).
2058  */
2059  symbols[level] = ts;
2060  scopes[level] = symbol_containing_scope(ts);
2062  "existing reftab symbol (full) %s 0x%"PRIxSMOFFSET";"
2063  " skipping to sibling\n",
2064  symbol_get_name(ts),ts->ref);
2065  goto do_sibling;
2066  }
2067  /*
2068  * If it's a partial symbol:
2069  * If @expand_dies, we need to fully load it, which means we
2070  * need to malloc its type/instance struct, AND then
2071  * re-process its attributes, BUT not finalize it.
2072  *
2073  * If not @expand_dies, we don't want to do anything to this,
2074  * nor load its children; skip to its sibling.
2075  */
2076  else if (ts && SYMBOL_IS_PARTIAL(ts)) {
2077  if (expand_dies) {
2078  //if (!(dopts->flags & DEBUGFILE_LOAD_FLAG_CUHEADERS
2079  // || dopts->flags & DEBUGFILE_LOAD_FLAG_PUBNAMES)
2080  // || expand_dies) {
2081  nofinalize = 1;
2082  symbols[level] = ts;
2083  scopes[level] = symbol_containing_scope(ts);
2084 
2086  "existing reftab symbol (partial) %s 0x%"PRIxSMOFFSET
2087  " on symtab; expanding attrs and children\n",
2088  symbol_get_name(ts),ts->ref);
2089 
2090  args.reloading = 1;
2091  }
2092  else {
2093  symbols[level] = NULL;
2094  scopes[level] = symbol_containing_scope(ts);
2095 
2097  "existing reftab symbol (partial) %s 0x%"PRIxSMOFFSET";"
2098  " skipping to sibling\n",
2099  symbol_get_name(ts),ts->ref);
2100 
2101  goto do_sibling;
2102  }
2103  }
2104 
2105  /*
2106  * Otherwise, start processing the DIE.
2107  */
2108 
2109  /* We need the tag even if we don't process it at first. */
2110  tag = dwarf_tag(&dies[level]);
2111 
2112  if (tag == DW_TAG_invalid) {
2113  verror("cannot get tag of DIE at offset %" PRIx64 ": %s\n",
2114  offset,dwarf_errmsg(-1));
2115  goto errout;
2116  }
2117 
2118  vdebug(4,LA_DEBUG,LF_DWARF,"<%d><%"PRIx64"> %s\n",
2119  (int)level,(uint64_t)offset,dwarf_tag_string(tag));
2120 
2143  if (tag == DW_TAG_variable
2144  || tag == DW_TAG_formal_parameter
2145  || tag == DW_TAG_enumerator) {
2146  if (!symbols[level]) {
2147  symbols[level] =
2149  NULL,0,offset,loadtype,scopes[level]);
2150  if (tag == DW_TAG_formal_parameter)
2151  symbol_set_parameter(symbols[level]);
2152  if (tag == DW_TAG_enumerator) {
2153  symbol_set_enumval(symbols[level]);
2154 
2155  /*
2156  * Assume the parent for this symbol is the
2157  * enumerated datatype... for C/C++ that is the only
2158  * way it could be. So don't even check.
2159  *
2160  * Yes, this means we don't do type compression for
2161  * enumerated types! See comments near datatype_ref
2162  * resolution near the bottom of this function.
2163  *
2164  * It's important that the datatype get set before
2165  * attr processing, because const vals for
2166  * enumerators should/must be loaded according to
2167  * their datatype.
2168  */
2169  symbols[level]->datatype = symbols[level-1];
2170  symbols[level]->datatype_ref = symbols[level-1]->ref;
2171  }
2172  }
2173  }
2174  else if (tag == DW_TAG_member) {
2175  if (!symbols[level]) {
2176  symbols[level] =
2178  NULL,0,offset,loadtype,scopes[level]);
2179  }
2180  symbol_set_member(symbols[level]);
2181  }
2182  else if (tag == DW_TAG_label) {
2183  if (!symbols[level]) {
2184  symbols[level] =
2186  NULL,0,offset,loadtype,scopes[level]);
2187  }
2188  }
2189  else if (tag == DW_TAG_unspecified_parameters) {
2190  if (!symbols[level-1])
2191  vwarn("cannot handle unspecified_parameters without parent!\n");
2192  else if (SYMBOL_IST_FUNC(symbols[level-1])
2193  || SYMBOL_IS_FUNC(symbols[level-1]))
2194  symbol_set_unspec_params(symbols[level-1]);
2195  }
2196  else if (tag == DW_TAG_base_type
2197  || tag == DW_TAG_typedef
2198  || tag == DW_TAG_pointer_type
2199  || tag == DW_TAG_reference_type
2200  || tag == DW_TAG_array_type
2201  || tag == DW_TAG_structure_type
2202  || tag == DW_TAG_enumeration_type
2203  || tag == DW_TAG_union_type
2204  || tag == DW_TAG_const_type
2205  || tag == DW_TAG_volatile_type
2206  || tag == DW_TAG_subroutine_type
2207  || tag == DW_TAG_class_type
2208  || tag == DW_TAG_namespace
2209  ) {
2210  if (!symbols[level]) {
2211  symbols[level] =
2213  NULL,0,offset,loadtype,scopes[level]);
2214 
2215  switch (tag) {
2216  case DW_TAG_base_type:
2217  symbols[level]->datatype_code = DATATYPE_BASE; break;
2218  case DW_TAG_typedef:
2219  symbols[level]->datatype_code = DATATYPE_TYPEDEF; break;
2220  case DW_TAG_pointer_type:
2221  symbols[level]->datatype_code = DATATYPE_PTR; break;
2222  case DW_TAG_reference_type:
2223  symbols[level]->datatype_code = DATATYPE_REF; break;
2224  case DW_TAG_array_type:
2225  symbols[level]->datatype_code = DATATYPE_ARRAY; break;
2226  case DW_TAG_structure_type:
2227  symbols[level]->datatype_code = DATATYPE_STRUCT; break;
2228  case DW_TAG_enumeration_type:
2229  symbols[level]->datatype_code = DATATYPE_ENUM; break;
2230  case DW_TAG_union_type:
2231  symbols[level]->datatype_code = DATATYPE_UNION; break;
2232  case DW_TAG_const_type:
2233  symbols[level]->datatype_code = DATATYPE_CONST; break;
2234  case DW_TAG_volatile_type:
2235  symbols[level]->datatype_code = DATATYPE_VOL; break;
2236  case DW_TAG_subroutine_type:
2237  symbols[level]->datatype_code = DATATYPE_FUNC; break;
2238  case DW_TAG_class_type:
2239  symbols[level]->datatype_code = DATATYPE_CLASS; break;
2240  case DW_TAG_namespace:
2241  symbols[level]->datatype_code = DATATYPE_NAMESPACE;break;
2242  default:
2243  break;
2244  }
2245  }
2246  else if (SYMBOL_IST_CONTAINER(symbols[level])) {
2247  /*
2248  * Make sure we've got it if it exists. It might exist
2249  * even if we did a partial load before, if attributes
2250  * required it to be created.
2251  */
2252  newscope = symbol_read_owned_scope(symbols[level]);
2253  }
2254  }
2255  else if (tag == DW_TAG_subrange_type) {
2256  /*
2257  * We cheat and don't actually type subranges... we're C
2258  * hackers, after all :).
2259  */
2260  ;
2261  }
2262  else if (tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine) {
2263  if (!symbols[level]) {
2264  symbols[level] =
2266  NULL,0,offset,loadtype,scopes[level]);
2267  }
2268  else
2269  /*
2270  * Make sure we've got it if it exists. It might exist
2271  * even if we did a partial load before, if attributes
2272  * required it to be created.
2273  */
2274  newscope = symbol_read_owned_scope(symbols[level]);
2275  }
2276  else if (tag == DW_TAG_lexical_block) {
2277  /*
2278  * Always build the scope for these things, even if not
2279  * fully loading. Good to get their range attributes.
2280  */
2281  newscope = scope_create(NULL,offset);
2282  }
2283  else if (tag == DW_TAG_compile_unit) {
2284  symbols[level] = root;
2285  }
2286  else if (tag == DW_TAG_imported_module) {
2287  /*
2288  * The main work here is to parse the attrs and find the
2289  * referenced "module" (C++ namespace, in our case).
2290  */
2291  ;
2292  }
2293  else if (tag == DW_TAG_imported_declaration
2294  || tag == DW_TAG_template_type_parameter
2295  || tag == DW_TAG_template_value_parameter
2296  || tag == DW_TAG_imported_module
2297  || tag == DW_TAG_inheritance) {
2299  "known unhandled dwarf tag %s!\n",dwarf_tag_string(tag));
2300  symbols[level] = NULL;
2301  goto do_sibling;
2302  }
2303  else {
2305  "unknown dwarf tag %s!\n",dwarf_tag_string(tag));
2306  symbols[level] = NULL;
2307  goto do_sibling;
2308  }
2309 
2313  args.level = level;
2314  if (level > 1)
2315  args.parentsymbol = symbols[level-1];
2316  else
2317  args.parentsymbol = NULL;
2318  args.symbol = symbols[level];
2319 
2320  args.lowpc = args.highpc = 0;
2321  args.lowpc_set = 0;
2322  args.highpc_set = 0;
2323  args.highpc_is_offset = 0;
2324  args.specification_set = 0;
2325  args.specification_ref = 0;
2326  args.ranges = NULL;
2327 
2328  args.sibling = 0;
2329  args.die_offset = offset;
2330  (void)dwarf_getattrs(&dies[level],attr_callback,&args,0);
2331 
2336  /*
2337  * Unmark member 'declaration' flag if the symbol is not also
2338  * external. We do this to avoid marking C++ struct/class
2339  * members as declarations if they are not also marked external
2340  * (external signals they are static members, and static members
2341  * probably will have definitions, so we can leave them as
2342  * declarations) -- because a non-static data member of a C/C++
2343  * struct/class is, for all practical purposes, a definition
2344  * (DWARF Spec v4, p 85).
2345  */
2346  if (symbols[level] && SYMBOL_IS_VAR(symbols[level])
2347  && symbols[level]->ismember
2348  && symbols[level]->isdeclaration
2349  && !symbols[level]->isexternal) {
2351  "unmarking declaration flag for non-static member!\n");
2352  symbols[level]->isdeclaration = 0;
2353  }
2354 
2355  reparent_scope = NULL;
2356 
2357  /*
2358  * See if this was an imported module (for our case, a C++ using
2359  * directive). If so, find that module symbol and start using
2360  * it as the current scope parent!
2361  *
2362  * If we haven't seen the imported module ref yet, and we're in
2363  * partial symbol mode, go get it first, then do this one.
2364  * Otherwise, error -- we do not jump forwards in time for
2365  * these. If we ever see imported modules in advance of their
2366  * declarations/definitions, we can try to fix this, but it is
2367  * hard because we never jump forward in a non-partial (full)
2368  * load; we only linearly traverse and patch up forward refs
2369  * later.
2370  */
2371  if (tag == DW_TAG_imported_module) {
2372  if (!args.ref_set) {
2373  verror("no AT_import ref for TAG_imported_module"
2374  " at 0x%"PRIxOFFSET"; skipping!\n",offset);
2375  goto do_sibling;
2376  }
2377  else if (!(imported_modules[level] = (struct symbol *) \
2378  g_hash_table_lookup(srd->reftab,(gpointer)(uintptr_t)args.ref))) {
2379  if (die_offsets) {
2381  "forward ref 0x%"PRIxOFFSET" for TAG_imported_module;"
2382  " pushing that in front of us and redoing after\n",
2383  offset);
2384  /* NB: see setup_skip_to_next_die() for why --i ! */
2385  --i;
2386  array_list_add_item_at(die_offsets,
2387  (void *)(uintptr_t)args.ref,i);
2388  /*
2389  * This sends us to setup the "next" DIE (the one we
2390  * just inserted into the list :)).
2391  */
2392  goto do_sibling;
2393  }
2394  else {
2395  vwarn("cannot resolve forward ref 0x%"PRIxOFFSET" for"
2396  " TAG_imported_module at 0x%"PRIxOFFSET"; skipping;"
2397  " scope hier might be wrong!\n",args.ref,offset);
2398  goto do_sibling;
2399  }
2400  }
2401  else {
2402  /*
2403  * Ok, imported_modules[level] has our temporary
2404  * "parent" namespace; just keep going (or wherever).
2405  */
2406  reparent_scope = symbol_link_owned_scope(imported_modules[level],
2407  NULL);
2408  goto do_sibling;
2409  }
2410  }
2411  else if (imported_modules[level]) {
2412  reparent_scope = symbol_read_owned_scope(imported_modules[level]);
2413 
2414  /*
2415  * Change the child's scope.
2416  */
2417  if (symbols[level])
2418  symbols[level]->scope = reparent_scope;
2419 
2421  "continuing to use imported module scope 0x%"PRIxSMOFFSET
2422  " (%s) for new DIE 0x%"PRIxOFFSET"\n",
2423  reparent_scope->ref,
2424  reparent_scope->symbol ? symbol_get_name(reparent_scope->symbol) : NULL,
2425  offset);
2426  }
2427 
2428  /*
2429  * Check if this symbol links back to an earlier declaration via
2430  * a specification reference. If it does, make sure the
2431  * declaration is fully loaded, and merge the declaration's
2432  * contents into this definition symbol *once it is fully loaded*!
2433  *
2434  * NB: if we *do* have one, when we link the scope in below, we
2435  * need to use specification_scope as the "new_parent" argument
2436  * to symbol_link_owned_scope!!!
2437  */
2438  specification_scope = NULL;
2439  specification_symbol = NULL;
2440  specification_symbol_parent = NULL;
2441  if (args.specification_set && symbols[level]) {
2442  // XXXX: fix up for partial loads!
2443  specification_symbol = (struct symbol *) \
2444  g_hash_table_lookup(srd->reftab,
2445  (gpointer)(uintptr_t)args.specification_ref);
2446  if (!specification_symbol) {
2447  /*
2448  * XXX: force a load, now! Load will be partial or
2449  * full; but we have to load it now so that attr
2450  * processing happens for the decl first, then the
2451  * current symbol (the definition). We could relax
2452  * this...
2453  */
2454  ;
2455  }
2456  else {
2457  specification_scope =
2458  symbol_containing_scope(specification_symbol);
2459 
2460  reparent_scope = specification_scope;
2461 
2462  /*
2463  * Change the child's scope.
2464  */
2465  symbols[level]->scope = specification_scope;
2466 
2467  /*
2468  * If we're going to shift it for the scope, we have to
2469  * change the parent too.
2470  */
2471  specification_symbol_parent = specification_scope->symbol;
2472 
2473  /*
2474  * Update the definition with the info from the
2475  * declaration insofar as it makes sense.
2476  */
2477  dwarf_specify_definition_attrs(srd,specification_symbol,
2478  symbols[level]);
2479 
2480  g_hash_table_insert(srd->spec_reftab,symbols[level],
2481  specification_symbol);
2482  }
2483  }
2484 
2485  /*
2486  * Handle low_pc/high_pc attrs together. If we have a scope,
2487  * try to update the scope's range(s). If we have a symbol,
2488  * update its base address.
2489  */
2490  if (symbols[level] && args.lowpc_set)
2491  symbol_set_addr(symbols[level],args.lowpc);
2492 
2493  /*
2494  * Be careful. Only try to set a range if the symbol can have
2495  * one, or if one was just created (i.e. for a block).
2496  */
2497  if (((symbols[level] && SYMBOL_CAN_OWN_SCOPE(symbols[level]))
2498  || newscope)
2499  && args.lowpc_set && args.highpc_set) {
2500  if (!newscope)
2501  newscope = symbol_link_owned_scope(symbols[level],
2502  reparent_scope);
2503 
2504  if (args.highpc_is_offset || args.highpc >= args.lowpc) {
2505  if (!args.highpc_is_offset)
2506  highpc = args.highpc;
2507  else
2508  highpc = args.lowpc + args.highpc;
2509 
2510  action = 0;
2511  scope_update_range(newscope,args.lowpc,highpc,&action);
2512 
2513  /*
2514  * Only update the fast range lookup struct if this is a
2515  * CU range or if the user asked for it via the
2516  * ALLRANGES flag.
2517  */
2518  if (level == 0 || dopts->flags & DEBUGFILE_LOAD_FLAG_ALLRANGES) {
2519  if (action == 1)
2520  clrange_add(&debugfile->ranges,args.lowpc,highpc,
2521  newscope);
2522  else if (action == 2)
2523  clrange_update_end(&debugfile->ranges,args.lowpc,highpc,
2524  newscope);
2525  }
2526  }
2527  else {
2528  verror("bad lowpc/highpc (0x%"PRIxADDR",0x%"PRIxADDR")"
2529  " at 0x%"PRIxOFFSET"\n",
2530  args.lowpc,args.highpc,offset);
2531  }
2532  }
2533  else if (args.lowpc_set && args.highpc_set) {
2534  vwarn("bad context for lowpc/highpc at offset 0x%"PRIxOFFSET"\n",
2535  offset);
2536  }
2537 
2538  /*
2539  * Handle AT_ranges, if there was any. Build and link in the
2540  * scope if we need.
2541  */
2542  if (args.ranges
2543  && ((symbols[level] && SYMBOL_CAN_OWN_SCOPE(symbols[level]))
2544  || newscope)) {
2545  struct range *range, *lastr;
2546  int action;
2547 
2548  if (!newscope)
2549  newscope = symbol_link_owned_scope(symbols[level],
2550  reparent_scope);
2551 
2552  range = args.ranges;
2553  while (range) {
2554  action = 0;
2555  scope_update_range(newscope,range->start,range->end,&action);
2556 
2557  /*
2558  * Only update the fast range lookup struct if this is a
2559  * CU range or if the user asked for it via the
2560  * ALLRANGES flag.
2561  */
2562  if (level == 0 || dopts->flags & DEBUGFILE_LOAD_FLAG_ALLRANGES) {
2563  if (action == 1)
2564  clrange_add(&debugfile->ranges,range->start,range->end,
2565  newscope);
2566  else if (action == 2)
2567  clrange_update_end(&debugfile->ranges,
2568  range->start,range->end,newscope);
2569  }
2570  lastr = range;
2571  range = range->next;
2572  free(lastr);
2573  }
2574  args.ranges = NULL;
2575  }
2576 
2577  /*
2578  * The first time we are not level 0 (i.e., at the CU's DIE),
2579  * check that we found a src filename attr; we must have it to
2580  * hash the symtab.
2581  */
2582  if (tag == DW_TAG_compile_unit && unlikely(!root_added)) {
2583  /* Try to find prologue info from line table for this CU. */
2584  if (args.have_stmt_list_offset) {
2585  have_stmt_list_offset = 1;
2586  cu_stmt_list_offset = args.stmt_list_offset;
2587  }
2588 
2589  if (!symbol_get_name(root)) {
2590  verror("CU did not have a src filename; aborting processing!\n");
2591  /* Don't free preexisting ones! */
2592  if (!root_preexisting)
2593  /* This will free the symbol! */
2594  debugfile_remove_root(debugfile,root);
2595  root = NULL;
2596  goto out;
2597  }
2598  else {
2599  if (root_preexisting) {
2600  if (debugfile_update_root(debugfile,root)) {
2602  "could not update CU symbol %s to debugfile;"
2603  " aborting processing!\n",
2604  symbol_get_name(root));
2605  /* Don't free preexisting ones! */
2606  //symbol_free(root);
2607  root = NULL;
2608  goto out;
2609  }
2610  }
2611  else if (debugfile_insert_root(debugfile,root)) {
2613  "could not add CU symbol %s to debugfile;"
2614  " aborting processing!\n",
2615  symbol_get_name(root));
2616  /* This will free the symbol! */
2617  debugfile_remove_root(debugfile,root);
2618  root = NULL;
2619  goto out;
2620  }
2621  root_added = 1;
2622  }
2623 
2624  /*
2625  * Only check CU load options on the initial load, because
2626  * if we have to expand it later, it is because a symbol or
2627  * address search required it... and the initial load opts
2628  * are meaningless.
2629  */
2630  if (dopts->flags & DEBUGFILE_LOAD_FLAG_CUHEADERS)
2631  goto out;
2632  if (dopts->flags & DEBUGFILE_LOAD_FLAG_PUBNAMES
2633  && (!die_offsets || array_list_len(die_offsets) == 0))
2634  goto out;
2635  /*
2636  * If we have regexes for root symbols and this one doesn't
2637  * match, skip it!
2638  */
2639  else if (dopts->srcfile_filter) {
2640  rfilter_check(dopts->srcfile_filter,symbol_get_name(root),
2641  &accept,NULL);
2642  if (accept == RF_REJECT) {
2643  vdebug(3,LA_DEBUG,LF_DWARF,"skipping CU '%s'\n",
2644  symbol_get_name(root));
2645  goto out;
2646  }
2647  }
2648 
2649  /* Find the offset of the first top-level (level 1) DIE */
2650  if (dwarf_child(&dies[level],&top_level_first_die) == 0)
2652  dwarf_dieoffset(&top_level_first_die);
2653  else
2654  srd->first_top_level_die_offset = 0;
2655  }
2656 
2657  /*
2658  * If we have die_offsets to load, and we're not just going to
2659  * load the full CU, AND if we have now processed the CU
2660  * symbol's attributes, we need to skip to the first DIE in our
2661  * list.
2662  */
2663  if (tag == DW_TAG_compile_unit && die_offsets) {
2664  if (array_list_len(die_offsets)) {
2665  i = 0;
2666  offset = (SMOFFSET)(uintptr_t)array_list_item(die_offsets,i);
2667  ++i;
2668 
2669  /*
2670  * Now, if the requested DIE is not one of the
2671  * top_level_die_offsets, we have to load the previous
2672  * top_level_die_offset instead of the requested DIE.
2673  * But if there are *no* top_level_die_offsets, we have
2674  * to just keep loading the entire CU if it's not
2675  * already loaded. In this case, we need to clear the
2676  * flags that we had setup for the partial load, or
2677  * whatever, and load all the DIEs. I think this can
2678  * only happen if PUBNAMES and there are no
2679  * AT_siblings.
2680  *
2681  * Do we fully load the top-level DIE? Shoot, even if
2682  * init_die_offsets, we might still only be partially
2683  * loading them. The flags are not setup to help us
2684  * here. Basically, we have to temporarily change them
2685  * to say "load full until you see until_die_offset is
2686  * loaded (either partially or fully), then revert to
2687  * partial until you finish the DIE". Crap.
2688  */
2689  until_die_offset = offset;
2690 
2691  ADDR tstart = 0;
2692  if (srd->top_level_die_offsets
2693  && clmatchone_find(&srd->top_level_die_offsets,offset,
2694  &tstart) != NULL) {
2695  offset = (Dwarf_Off)tstart;
2696 
2698  "skipping to top level DIE 0x%x to load DIE 0x%x\n",
2699  offset,until_die_offset);
2700  }
2701  else {
2702  /*
2703  * All we can do is load the CU until we find the
2704  * desired offset.
2705  */
2706  offset = srd->first_top_level_die_offset;
2707 
2709  "skipping to first CU top level DIE 0x%x to load DIE 0x%x\n",
2710  offset,until_die_offset);
2711  }
2712 
2713  /*
2714  * So many things key off level == 0 that we set it to 1
2715  * deliberately. Abstractly, we don't know what depth
2716  * we're heading for anyway!
2717  *
2718  * Ok, now we know we're going to level 1, always, for
2719  * partial symbol loads.
2720  */
2721  level = 1;
2722  if (dwarf_offdie(dbg,offset,&dies[level]) == NULL) {
2723  verror("cannot get first DIE at offset 0x%"PRIx64
2724  " during partial CU load: %s\n",
2725  offset,dwarf_errmsg(-1));
2726  goto errout;
2727  }
2729  "skipping to first DIE 0x%x\n",offset);
2730  scopes[level] = scopes[level-1];
2731  continue;
2732  }
2733  else {
2734  /* We're done -- we just wanted to load the CU header. */
2735  goto out;
2736  }
2737  }
2738 
2739  /*
2740  * Later on, when we decide whether to descend into this
2741  * symbol's children or not, we may re-mark this as partial, if
2742  * we are looking for a DIE that is not inside this DIE. But we
2743  * don't check that until later -- because it's related to
2744  * whether we descend or not -- and we want the check in one
2745  * place. This is the default -- expand to LOADTYPE_FULL.
2746  */
2747  if (expand_dies && ts && SYMBOL_IS_PARTIAL(ts))
2748  ts->loadtag = LOADTYPE_FULL;
2749 
2750  /* If we're actually handling this CU, then... */
2751 
2752  /* THIS MUST HAPPEN FIRST:
2753  *
2754  * If we are going to give the symbol an extname, we must do it
2755  * now, before it goes onto any hashtables. We cannot rename in
2756  * finalize_die_symbol; that is too late and will cause memory
2757  * corruption because the symbol will have already been inserted
2758  * into hashtables.
2759  */
2760  if (symbols[level] && !nofinalize)
2761  finalize_die_symbol_name(symbols[level]);
2762 
2763  /*
2764  * Type compression, part 1:
2765  *
2766  * If we've loaded the attrs for a non-enumerated type, and it
2767  * is already present in our global type table, AND if the user
2768  * hasn't specified that we must *fully* check the type
2769  * equivalence (DEBUGFILE_LOAD_FLAG_REDUCETYPES_FULL_EQUIV), and
2770  * if the looked up symbol is not in our CU, AND if we're at
2771  * level 1 (we only consider types that are at the top level),
2772  * we can
2773  * free this symbol, use the looked-up value right away, and
2774  * skip any of our children!
2775  *
2776  * We can only do this stuff for C; we cannot for C++ because we
2777  * don't check namespaces. We could only technically share
2778  * types within namespaces -- not globally.
2779  *
2780  * Type compression, part 2 continues below (needed if
2781  * DEBUGFILE_LOAD_FLAG_REDUCETYPES_FULL_EQUIV is set because we
2782  * can't fully check type equivalence until we've loaded this
2783  * type and all its children, if any).
2784  */
2785  if (level == 1 && symbols[level]
2786  && SYMBOLX_ROOT(root)
2787  && (SYMBOLX_ROOT(root)->lang_code == DW_LANG_C
2788  || SYMBOLX_ROOT(root)->lang_code == DW_LANG_C89
2789  || SYMBOLX_ROOT(root)->lang_code == DW_LANG_C99)
2790  && SYMBOL_IS_TYPE(symbols[level]) && !SYMBOL_IST_ENUM(symbols[level])
2793  && symbol_get_name(symbols[level])) {
2794  if ((tsymbol = (struct symbol *) \
2795  g_hash_table_lookup(debugfile->shared_types,
2796  symbol_get_name(symbols[level])))) {
2797  if (SYMBOL_IS_TYPE(tsymbol) && !SYMBOL_IST_ENUM(tsymbol)) {
2798  /* Insert the looked up symbol into our CU's temp
2799  * reftab so that any of our DIEs that tries to use
2800  * it gets the "global" one instead.
2801  */
2803  "inserting shared symbol (quick check) %s (%s"
2804  " 0x%"PRIxSMOFFSET") of type %s at offset 0x%"
2805  PRIxSMOFFSET" into reftab\n",
2806  symbol_get_name(tsymbol),
2807  symbol_get_name_orig(tsymbol),
2808  tsymbol->ref,
2809  SYMBOL_TYPE(tsymbol->type),symbols[level]->ref);
2810 
2811  /*
2812  * It's not on a scope yet; so no need to remove
2813  * it. Then insert it into the reftab.
2814  */
2815  //scope_remove_symbol(symbol_containing_scope(symbols[level]),
2816  // symbols[level]);
2817  dwarf_reftab_insert(srd,tsymbol,
2818  (SMOFFSET)symbols[level]->ref);
2819 
2820  /*
2821  * Nobody holds a ref to it yet, except possibly it
2822  * referenced another symbol -- so must free it from
2823  * refuselists!
2824  *
2825  * BUT, since reftab had not held on this symbol
2826  * yet, only the refuselist had, potentially! So
2827  * hold it quickly; then release; then RPUT. This
2828  * protects us from not freeing if this symbol had
2829  * not referenced anyone else (and thus the
2830  * refuselist had not held it).
2831  */
2832  RHOLD(symbols[level],srd);
2833  dwarf_symbol_refuselist_release_all(srd,symbols[level]);
2834  RPUT(symbols[level],symbol,srd,trefcnt);
2835  symbols[level] = NULL;
2836  /* Skip to the next sibling, or to the next DIE if
2837  * we're doing a partial CU load.
2838  */
2839  goto do_sibling;
2840  }
2841  }
2842  else if (!symbols[level]->isdeclaration) {
2844  "sharing symbol (quick check) %s (%s) of type %s\n",
2845  symbol_get_name(symbols[level]),
2846  symbol_get_name_orig(symbols[level]),
2847  SYMBOL_TYPE(symbols[level]->type));
2848  symbols[level]->isshared = 1;
2849  g_hash_table_insert(debugfile->shared_types,
2850  symbol_get_name(symbols[level]),
2851  symbols[level]);
2852  /*
2853  * NOTE: hold a ref here for clarity.
2854  */
2855  RHOLD(symbols[level],debugfile);
2856  /*
2857  * Place it in debugfile->types too.
2858  */
2859  debugfile_replace_type(debugfile,symbols[level]);
2860  }
2861  }
2862 
2863  if (symbols[level]
2864  && SYMBOL_IS_INSTANCE(symbols[level])
2865  && !symbol_get_name_orig(symbols[level])) {
2866  /* This is actually ok because function type params can
2867  * be unnamed, and so can inlined functions.
2868  */
2869  if (!((SYMBOL_IS_FUNC(symbols[level])
2870  && symbols[level]->isinlineinstance)
2871  || (SYMBOL_IS_LABEL(symbols[level])
2872  && (symbols[level]->isinlineinstance))
2873  || (SYMBOL_IS_VAR(symbols[level])
2874  && (symbols[level]->isinlineinstance
2875  || (level > 0
2876  && symbols[level]->isparam
2877  && symbols[level-1]
2878  && (SYMBOL_IS_FUNC(symbols[level-1])
2879  || SYMBOL_IST_FUNC(symbols[level-1])))
2880  || (level > 0
2881  && symbols[level]->ismember
2882  && symbols[level-1]
2883  && SYMBOL_IST_CONTAINER(symbols[level-1]))))))
2885  "anonymous symbol of type %s at DIE 0x%"PRIx64"!\n",
2886  SYMBOL_TYPE(symbols[level]->type),offset);
2887  }
2888 
2889  /*
2890  * Add to this CU's reference offset table. We originally only
2891  * did this for types, but since inlined func/param instances
2892  * can refer to funcs/vars, we have to do it for every symbol.
2893  *
2894  * Don't add the root symbol! This causes chicken and egg
2895  * problems when we remove symbols from the reftab.
2896  */
2897  if (!ts && symbols[level] && level > 0)
2898  dwarf_reftab_insert(srd,symbols[level],(SMOFFSET)offset);
2899 
2900  /*
2901  * See if we have a child to process so we can create a scope
2902  * for the current symbol if we tried to optimize by not
2903  * creating it yet (i.e., if it's a function or
2904  * struct/union/classdecl, and has no children, it won't need a
2905  * scope, so we save mem).
2906  *
2907  * Ok, this is knarly. We need to know if we're going to have a
2908  * child. If so (even if we're not fully loading the DIE this
2909  * pass), create the scope and link it in.
2910  *
2911  * We also do it in this out-of-order way so that we can change
2912  * a symbol's scope appropriately if we're going to need to.
2913  */
2914 
2915  /*
2916  * Make room for the next level's DIE.
2917  *
2918  * Also, make sure to NULL out the new values! This is
2919  * important for imported_modules; we don't NULL those out like
2920  * we do symbols[] and scopes[]!
2921  */
2922  if (level + 1 == maxdies) {
2923  maxdies += 8;
2924  dies = (Dwarf_Die *)realloc(dies,maxdies*sizeof(Dwarf_Die));
2925  symbols = (struct symbol **) \
2926  realloc(symbols,maxdies*sizeof(struct symbol *));
2927  scopes = (struct scope **) \
2928  realloc(scopes,maxdies*sizeof(struct scope *));
2929  imported_modules = (struct symbol **) \
2930  realloc(imported_modules,maxdies*sizeof(struct symbol *));
2931 
2932  for (tmplpc = level + 1; tmplpc < maxdies; ++tmplpc) {
2933  symbols[tmplpc] = NULL;
2934  imported_modules[tmplpc] = NULL;
2935  }
2936  }
2937 
2938  int res = dwarf_child(&dies[level],&dies[level + 1]);
2939 
2940  /*
2941  * NB: but! first, very important. If our current symbol
2942  * can own a scope, but we didn't have to create it yet, we
2943  * must create and link it in now! We cannot wait for
2944  * later.
2945  * And, remember from above, to use reparent_scope to
2946  * handle the case where we're moving this symbol into its
2947  * specifier's or imported module's scope.
2948  */
2949  if (res == 0
2950  && symbols[level] && SYMBOL_CAN_OWN_SCOPE(symbols[level])
2951  && !newscope) {
2952  newscope =
2953  symbol_link_owned_scope(symbols[level],reparent_scope);
2954  }
2955 
2956  /*
2957  * Make sure we link the newscope into the scope struct. If we
2958  * need to move the symbol onto a different scope (i.e., the one
2959  * that contained our specifying declaration), we will do it by
2960  * setting teh second arg to symbol_link_owned_scope.
2961  *
2962  * We might not have a newscope yet, if we didn't need one. So
2963  * -- we also have to check this again if we are going to load
2964  * children. If we are going to load children, we *must* have
2965  * setup our owned scope before we process the child.
2966  * Otherwise, if we had multiple levels (i.e., function, block,
2967  * var, and called tried to insert...
2968  */
2969  //if (newscope && symbols[level])
2970  // symbol_link_owned_scope(symbols[level],reparent_scope);
2971 
2972  /*
2973  * Handle adding child symbols/scopes to parents!
2974  */
2975  if (level > 0) {
2976  /*
2977  * Be careful. If you have symbol/symbol, must call
2978  * symbol_insert_symbol; else you can call
2979  * scope_insert_symbol; or scope_insert_scope.
2980  * symbol_insert_symbol takes care of allocating
2981  * symbol-owned scopes for you.
2982  */
2983  if (symbols[level]) {
2984  if (args.specification_set && specification_symbol_parent) {
2985  symbol_insert_symbol(specification_symbol_parent,
2986  symbols[level]);
2987  }
2988  else if (imported_modules[level]) {
2989  symbol_insert_symbol(imported_modules[level],symbols[level]);
2990  }
2991  else if (ts) {
2992  /*
2993  * The symbol already existed, and we're not
2994  * changing it, so it's already on the parent.
2995  */
2996  ;
2997  }
2998  else if (symbols[level-1])
2999  symbol_insert_symbol(symbols[level-1],symbols[level]);
3000  else if (scopes[level])
3001  scope_insert_symbol(scopes[level],symbols[level]);
3002  else {
3003  verror("symbol(%s:0x%"PRIxSMOFFSET") but no parent to add to!\n",
3004  symbol_get_name(symbols[level]),symbols[level]->ref);
3005  goto errout;
3006  }
3007  }
3008  else if (newscope) {
3009  if (scopes[level])
3010  scope_insert_scope(scopes[level],newscope);
3011  else {
3012  verror("scope at 0x%"PRIx64" has no parent!\n",
3013  offset);
3014  goto errout;
3015  }
3016  }
3017  }
3018 
3019  /* The last thing to do is finalize the symbol (which we can do
3020  * before processing its children.
3021  *
3022  * Make sure to pass the new specification_symbol_parent or
3023  * imported_modules[level] parent if there is one!!
3024  */
3025  if (symbols[level] && !nofinalize) {
3026  struct symbol *finalize_parent;
3027  if (specification_symbol_parent)
3028  finalize_parent = specification_symbol_parent;
3029  else if (imported_modules[level])
3030  finalize_parent = imported_modules[level];
3031  else if (level > 0 && symbols[level - 1])
3032  finalize_parent = symbols[level - 1];
3033  else
3034  finalize_parent = NULL;
3035 
3036  finalize_die_symbol(debugfile,level,symbols[level],
3037  finalize_parent,
3038  voidsymbol,
3039  srd->reftab,die_offsets,(SMOFFSET)*cu_offset);
3040  /*
3041  * NB: we cannot free the symbol once we are here, because 1) the
3042  * symbol is already on our reftab (see above), and 2) the
3043  * symbol may have been put on a parent list (see above
3044  * block).
3045  */
3046  }
3047 
3048  inline int setup_skip_to_next_die(void) {
3049  int alen = array_list_len(die_offsets);
3050  struct symbol *sss;
3051 
3052  /* Maybe skip to the next offset if we haven't already
3053  * processed this DIE.
3054  */
3055  while (1) {
3056  if (i >= alen) {
3057  vdebug(5,LA_DEBUG,LF_DWARF,"end of partial load DIE list!\n");
3058  return 0;
3059  }
3060  offset = (SMOFFSET)(uintptr_t)array_list_item(die_offsets,i);
3061  ++i;
3062 
3063  sss = (struct symbol *) \
3064  g_hash_table_lookup(srd->reftab,(gpointer)(uintptr_t)offset);
3065  if (!sss || sss->loadtag != LOADTYPE_FULL)
3066  break;
3067  else
3068  continue;
3069  }
3070 
3071  /*
3072  * Need to do the same as above; skip to the next top-level
3073  * DIE and load/expand that.
3074  */
3075  until_die_offset = offset;
3076 
3077  ADDR tstart = 0;
3078  if (srd->top_level_die_offsets
3079  && clmatchone_find(&srd->top_level_die_offsets,offset,
3080  &tstart) != NULL) {
3081  offset = (Dwarf_Off)tstart;
3082 
3084  "skipping to top level DIE 0x%x to load DIE 0x%x\n",
3085  offset,until_die_offset);
3086  }
3087  else {
3088  offset = srd->first_top_level_die_offset;
3089 
3091  "skipping to first CU top level DIE 0x%x to load DIE 0x%x\n",
3092  offset,until_die_offset);
3093  }
3094 
3095  /* So many things key off level == 0 that we set
3096  * it to 1 deliberately.
3097  *
3098  * Ok, now we know we're going to level 1, always, for
3099  * partial symbol loads.
3100  */
3101  level = 1;
3102  scopes[level] = root_scope;
3103  if (dwarf_offdie(dbg,offset,&dies[level]) == NULL) {
3104  verror("cannot get DIE %d at offset 0x%"PRIx64
3105  " during partial CU (0x%"PRIx64") load: %s\n",
3106  i - 1,offset,*cu_offset,dwarf_errmsg(-1));
3107  return -1;
3108  }
3109  /*
3110  * Clear out all the imported_module statements we might
3111  * have seen; we don't want to add any symbols to these
3112  * imported scopes erroneously!
3113  *
3114  * (NB: in general, imported modules are problematic for
3115  * partial loads -- yet one more thing that forces us to
3116  * parse the full level == 1 of each CU, at the very least.)
3117  */
3119  "skipping to DIE %d at 0x%x in CU 0x%"PRIx64"\n",
3120  i,offset,*cu_offset);
3121  return 1;
3122  }
3123 
3124  int res2;
3125  if (res > 0) {
3126  /*
3127  * If there are no children, this symbol was fully loaded
3128  * even if we didn't try to do that. So mark it as fully
3129  * loaded.
3130  */
3131  if (symbols[level])
3132  symbols[level]->loadtag = LOADTYPE_FULL;
3133 
3134  if (level >= 0 && symbols[level]
3135  && symbols[level]->isdeclaration)
3136  debugfile_save_declaration(debugfile,symbols[level]);
3137 
3138  do_sibling:
3139  /* If we were teleported here, set res just in case somebody
3140  * expects it to be valid in this block, if the block ever
3141  * gets code that needs it!
3142  */
3143  res = 1;
3144 
3145  /*
3146  * Complete the symbol.
3147  */
3148  if (level >= 0
3149  && symbols[level]
3150  && symbols[level]->loadtag == LOADTYPE_FULL) {
3151  struct symbol *specsym;
3152 
3153  specsym = (struct symbol *) \
3154  g_hash_table_lookup(srd->spec_reftab,symbols[level]);
3155  if (specsym) {
3156  dwarf_specify_definition_members(srd,specsym,symbols[level]);
3157  g_hash_table_remove(srd->spec_reftab,symbols[level]);
3158 
3159  /*
3160  * Also, if we're going to replace the declaration
3161  * (specification) symbol with its definition, do it.
3162  */
3163  if (!(dopts->flags & DEBUGFILE_LOAD_FLAG_KEEPDECLS)) {
3164  struct scope *tscope;
3165  tscope = symbol_containing_scope(specsym);
3166  if (tscope && tscope->symbol)
3167  symbol_remove_symbol(tscope->symbol,specsym);
3168  else if (tscope)
3169  scope_remove_symbol(tscope,specsym);
3171  }
3172  dwarf_reftab_insert(srd,symbols[level],specsym->ref);
3173  }
3174  }
3175 
3176  if (die_offsets && level == 1 && offset > until_die_offset) {
3177  /* No new child, but possibly a new sibling, so nuke this
3178  * level's symbol. If it was a declaration, let the
3179  * library maybe replace it with a definition symbol, or
3180  * maybe just copy the info.
3181  */
3182  if (die_offsets && symbols[level] && symbols[level]->name
3183  && symbols[level]->isdeclaration)
3185  symbols[level]->name);
3186  symbols[level] = NULL;
3187 
3188  res2 = setup_skip_to_next_die();
3189  /* error; bail */
3190  if (res2 == -1) goto errout;
3191  /* no DIEs left to load in CU */
3192  else if (res2 == 0) { level = -1; }
3193  /* next die offset is setup; continue */
3194  else if (res2 == 1) continue;
3195  }
3196  else {
3197  while ((res = dwarf_siblingof(&dies[level],&dies[level])) == 1) {
3198  int oldlevel = level--;
3199 
3200  /*
3201  * Complete the symbol if it has been fully loaded.
3202  */
3203  if (level >= 0
3204  && symbols[level]
3205  && symbols[level]->loadtag == LOADTYPE_FULL) {
3206  struct symbol *specsym;
3207 
3208  specsym = (struct symbol *) \
3209  g_hash_table_lookup(srd->spec_reftab,symbols[level]);
3210  if (specsym) {
3212  symbols[level]);
3213  g_hash_table_remove(srd->spec_reftab,symbols[level]);
3214 
3215  /*
3216  * Also, if we're going to replace the declaration
3217  * (specification) symbol with its definition, do it.
3218  */
3219  if (!(dopts->flags & DEBUGFILE_LOAD_FLAG_KEEPDECLS)) {
3220  struct scope *tscope;
3221  tscope = symbol_containing_scope(specsym);
3222  if (tscope && tscope->symbol)
3223  symbol_remove_symbol(tscope->symbol,specsym);
3224  else if (tscope)
3225  scope_remove_symbol(tscope,specsym);
3226  }
3228  dwarf_reftab_insert(srd,symbols[level],specsym->ref);
3229  }
3230  }
3231 
3232  /* If we're loading a partial CU, if there are more DIEs
3233  * we need to load, do them! We don't process any
3234  * siblings at level 1, since that's the level we start
3235  * each DIE load in a partial CU load at.
3236  */
3237  if (die_offsets && oldlevel == 1 && offset > until_die_offset) {
3238  /* Now that a DIE's children have all been parsed, and
3239  * we're leveling up, NULL out this symbol. If
3240  * it was a declaration, let the library maybe
3241  * replace it with a definition symbol, or maybe
3242  * just copy the info.
3243  */
3244  if (die_offsets && symbols[level] && symbols[level]->name
3245  && symbols[level]->isdeclaration)
3247  symbols[level]->name);
3248  symbols[level] = NULL;
3249 
3250  res2 = setup_skip_to_next_die();
3251  /* error; bail */
3252  if (res2 == -1) goto errout;
3253  /* no DIEs left to load in CU */
3254  else if (res2 == 0) { level = -1; break; }
3255  /* next die offset is setup; continue */
3256  else if (res2 == 1) continue;
3257  }
3258  /* Otherwise, we stop when the level was zero! */
3259  else if (oldlevel == 0)
3260  break;
3261 
3262  /* Now that a DIE's children have all been parsed,
3263  * and we're leveling up, NULL out this symbol. If
3264  * it was a declaration, let the library maybe
3265  * replace it with a definition symbol, or maybe
3266  * just copy the info.
3267  */
3268  if (die_offsets && symbols[level] && symbols[level]->name
3269  && symbols[level]->isdeclaration)
3271  symbols[level]->name);
3272  symbols[level] = NULL;
3273  /*if (symbols[level-1]
3274  && symbols[level-1]->type == SYMBOL_TYPE_FUNCTION
3275  && symtab->parent)
3276  symtab = symtab->parent;*/
3277 
3278  /*
3279  * Get rid of the old level's imported_module, if
3280  * any; but obviously don't clear
3281  * imported_modules[level]; that might still be
3282  * relevant to any other DIEs we're still going to
3283  * parse in level!
3284  */
3285  imported_modules[oldlevel] = NULL;
3286  }
3287 
3288  if (res == -1) {
3289  verror("cannot get next DIE: %s\n",dwarf_errmsg(-1));
3290  goto errout;
3291  }
3292  else if (res == 0 && die_offsets && level == 1 && offset > until_die_offset) {
3293  /* If there IS a sibling, but we don't want to
3294  * process it, because we finished the DIE we wanted
3295  * to do, skip to the next DIE.
3296  */
3297  res2 = setup_skip_to_next_die();
3298  /* error; bail */
3299  if (res2 == -1) goto errout;
3300  /* no DIEs left to load in CU */
3301  else if (res2 == 0) { level = -1; }
3302  /* next die offset is setup; continue */
3303  else if (res2 == 1) continue;
3304  }
3305  }
3306  }
3307  else if (res < 0) {
3308  verror("cannot get next DIE: %s",dwarf_errmsg(-1));
3309  goto errout;
3310  }
3311  else {
3312  /*
3313  * New child DIE. If we're loading partial symbols, only
3314  * process it given some conditions.
3315  *
3316  * NB: if this was an enumerated type, we *have* to process
3317  * the enumerators now -- because otherwise we'll never load
3318  * them unless the user/library later loads the type they
3319  * were contained in!
3320  */
3321 
3322  if ((!quick || expand_dies || tag == DW_TAG_enumeration_type) || level < 1
3323  || (until_die_offset && args.sibling && until_die_offset < args.sibling)) {
3324  ++level;
3325  symbols[level] = NULL;
3326  if (!newscope)
3327  scopes[level] = scopes[level-1];
3328  else
3329  scopes[level] = newscope;
3330  }
3331  else {
3332  /* Skip to the next sibling. */
3333  if (until_die_offset && args.sibling && until_die_offset >= args.sibling) {
3334  /*
3335  * But first, if there were children we didn't
3336  * process because the current DIE's range (to its
3337  * sibling) does not contain the until_die_offset we
3338  * are looking for, we can mark this as only
3339  * partially loaded!
3340  */
3341  symbols[level]->loadtag = LOADTYPE_PARTIAL;
3342  }
3343  goto do_sibling;
3344  }
3345  }
3346  }
3347  while (level >= 0);
3348 
3349  do_word_symbol(debugfile,root);
3350 
3351  /* Type compression part 2a:
3352  *
3353  * We go through our entire reftab, again, now that all refs have
3354  * been resolved and we can do full type equivalence checks (if
3355  * we're doing DEBUGFILE_LOAD_FLAG_REDUCETYPES and dopts->flags &
3356  * DEBUGFILE_LOAD_FLAG_REDUCETYPES_FULL_EQUIV (if we didn't need to
3357  * check for full equiv, that check has already been done in
3358  * debuginfo_load_cu as a shortcut), check full type equivalence).
3359  *
3360  * Unfortunately, full equivalence requires us to check *here*, once
3361  * we've post-passed all our datatype refs. Why? For the same
3362  * reason that the post-pass is there -- we may not have fully
3363  * resolved the type refs for the type we are examining (i.e., for a
3364  * struct type, some of the members' types may still be
3365  * unresolved until after the post-pass).
3366  *
3367  * Basically, for each symbol with a valid name, we look it up in
3368  * the debugfile->types hashtable. If we find it there already, we
3369  * change reftab so that the DIE offset for that symbol points to
3370  * the one we find. (This means, that to be useful, we have to
3371  * construct names for pointer/const/vol/array types so that we can
3372  * look them up too... but we can try the base type thing first and
3373  * see if it works good enough.) If we find it, we free it.
3374  *
3375  * XXX: but how do we know if we can free what it points to? Should
3376  * we maintain a counter of who points to what refs, and any that
3377  * have zero, remove? Should we do this with symbol refcnt
3378  * mechanism? WAIT -- the principle is that any type we find that
3379  * matches a type at one of our refs *fully* matches, including all
3380  * of the types it references, so when we rewrite reftab to use the
3381  * global type we found, we can safely free the type from this CU
3382  * AND any types it references. Suppose we free a pointer to a
3383  * struct type because we matched it against a global type. Ok,
3384  * that means we free the struct type and anything it references.
3385  * Later, when we process the reftab entry for the struct type
3386  * itself, we will also find a match in the globals table, and reuse
3387  * *that* global entry in reftab. What this means, though, is that
3388  * we can't free any symbols that did match until after the loop!
3389  * So we keep a free list.
3390  *
3391  * We also need to build fake names for some kinds of types once
3392  * they've been resolved -- pointers are the big one, because if we
3393  * can save pointer/const/vol/types,
3394  *
3395  * Of course, this may be temporarily very wasteful; we may have
3396  * created lots of type symbols we will now remove, and this is
3397  * essentially our third pass through the CU (and we still have a
3398  * 4th one to rewrite all our ->datatype fields. But it should
3399  * result in less memory usage if there are lots of CUs that have
3400  * identical types.
3401  *
3402  */
3403  if (SYMBOLX_ROOT(root)
3404  && (SYMBOLX_ROOT(root)->lang_code == DW_LANG_C
3405  || SYMBOLX_ROOT(root)->lang_code == DW_LANG_C89
3406  || SYMBOLX_ROOT(root)->lang_code == DW_LANG_C99)
3408  //GHashTable *updated = g_hash_table_new(g_direct_hash,g_direct_equal);
3409  GSList *klist = g_hash_table_get_keys_slist(srd->reftab);
3410  GSList *gsltmp;
3411  GHashTable *eqcache = g_hash_table_new(g_direct_hash,g_direct_equal);
3412 
3413  v_g_slist_foreach(klist,gsltmp,key) {
3414  offset = (uintptr_t)key;
3415  rsymbol = (struct symbol *) \
3416  g_hash_table_lookup(srd->reftab,(gpointer)(uintptr_t)offset);
3417  if (!rsymbol || !SYMBOL_IS_TYPE(rsymbol) || SYMBOL_IST_ENUM(rsymbol))
3418  continue;
3419 
3420  if (!(sname = symbol_get_name(rsymbol)))
3421  continue;
3422 
3423  if ((tsymbol = (struct symbol *) \
3424  g_hash_table_lookup(debugfile->shared_types,sname))
3425  && rsymbol != tsymbol
3426  && tsymbol->isshared) {
3427  if (symbol_type_equal(rsymbol,tsymbol,eqcache,NULL) == 0) {
3429  "inserting shared symbol (slow check) %s (%s"
3430  " 0x%"PRIxSMOFFSET") of type %s at offset 0x%"
3431  PRIxSMOFFSET" into reftab\n",
3432  symbol_get_name(tsymbol),
3433  symbol_get_name_orig(tsymbol),
3434  tsymbol->ref,SYMBOL_TYPE(tsymbol->type),offset);
3435 
3436  /*
3437  * Insert the looked up symbol into our CU's temp
3438  * reftab so that any of our DIEs that tries to use
3439  * it gets the "global" one instead.
3440  */
3441  //g_hash_table_iter_replace(&iter,tsymbol);
3442 
3443  //g_hash_table_insert(updated,(gpointer)(uintptr_t)offset,
3444  // (gpointer)tsymbol);
3445  /*
3446  * RPUT() once on rsymbol to remove it from reftab;
3447  * and once to remove it from its scope.
3448  */
3449  dwarf_reftab_insert(srd,tsymbol,offset);
3451  scope_remove_symbol(symbol_containing_scope(rsymbol),rsymbol);
3452  //RPUT(rsymbol,symbol,srd->reftab,trefcnt);
3453  }
3454  else {
3456  "no shared match for symbol (slow check) %s (%s"
3457  " 0x%"PRIxSMOFFSET") of type %s at offset 0x%"
3458  PRIxSMOFFSET" into reftab\n",
3459  symbol_get_name(rsymbol),
3460  symbol_get_name_orig(rsymbol),
3461  rsymbol->ref,SYMBOL_TYPE(rsymbol->type),offset);
3462  }
3463  }
3464  else if (rsymbol != tsymbol && !rsymbol->isdeclaration) {
3466  "sharing symbol (slow check) %s (%s"
3467  " 0x%"PRIxSMOFFSET") of type %s at offset 0x%"
3468  PRIxSMOFFSET" into reftab\n",
3469  symbol_get_name(rsymbol),symbol_get_name_orig(rsymbol),
3470  rsymbol->ref,SYMBOL_TYPE(rsymbol->type),offset);
3471 
3472  /*
3473  * Mark it as a shared symbol; then insert it into the
3474  * shared_types table.
3475  */
3476  rsymbol->isshared = 1;
3477  g_hash_table_insert(debugfile->shared_types,sname,rsymbol);
3478  /*
3479  * Hold a ref just because it's in our table.
3480  */
3481  RHOLD(rsymbol,debugfile);
3482  /*
3483  * Place it in debugfile->types too.
3484  */
3485  debugfile_replace_type(debugfile,rsymbol);
3486  }
3487  }
3488  g_slist_free(klist);
3489  g_hash_table_destroy(eqcache);
3490  //g_hash_table_destroy(updated);
3491  }
3492 
3493  /* Try to find prologue info from line table for this CU. */
3494  if (have_stmt_list_offset) {
3495  dwarf_get_lines(srd,cu_stmt_list_offset);
3496  }
3497 
3498  /* Save off whatever offset we got to! */
3499  *cu_offset = offset;
3500 
3501  goto out;
3502 
3503  errout:
3504  retval = -1;
3505 
3506  out:
3507  if (die_offsets)
3508  array_list_free(die_offsets);
3509  free(dies);
3510  free(symbols);
3511  free(scopes);
3512  free(imported_modules);
3513 
3514  /*
3515  * Clean up whatever we can!
3516  */
3517  dwarf_refuselist_clean(root,retval ? 1 : 0);
3518  dwarf_reftab_clean(root,retval ? 1 : 0);
3519 
3520  return retval;
3521 }
3522 
3523 int dwarf_symbol_replace(struct debugfile *debugfile,
3524  struct symbol *old,struct symbol *new) {
3525  struct symbol *root;
3526  struct symbol_root_dwarf *srd;
3527  struct scope *scope;
3528 
3529  /*
3530  * Just like for type compression, where we replace one symbol with
3531  * another symbol from a (likely) different CU, we have to get the
3532  * root symbol associated with the one we're replacing (the old
3533  * one), and insert the new symbol into the old symbol's reftab --
3534  * and then let dwarf_reftab_insert work its magic of replacing any
3535  * refs and deleting the old symbol.
3536  */
3537  root = symbol_find_root(old);
3538  if (!root) {
3539  verror("could not find root symbol for old ");
3540  ERRORDUMPSYMBOL(old);
3541  verrorc(" replacing with new ");
3542  ERRORDUMPSYMBOL_NL(new);
3543  return -1;
3544  }
3545  srd = SYMBOLX_ROOT(root)->priv;
3546 
3547  dwarf_reftab_insert(srd,new,old->ref);
3549  scope = symbol_containing_scope(old);
3550  if (scope)
3551  scope_remove_symbol(scope,old);
3552  else {
3553  vwarn("no scope for old ");
3554  WARNDUMPSYMBOL_NL(old);
3555  }
3556 
3557  return 0;
3558 }
3559 
3560 int dwarf_symbol_expand(struct debugfile *debugfile,
3561  struct symbol *root,struct symbol *symbol) {
3562  Dwarf_Off die_offset;
3563  struct array_list *sal;
3564  int retval;
3565  struct symbol_root_dwarf *srd = SYMBOLX_ROOT(root)->priv;
3566 
3567  /* Caller should protect against this, but let's be sure. */
3568  if (SYMBOL_IS_FULL(symbol) || SYMBOL_IS_FULL(root))
3569  return 0;
3570 
3571  sal = array_list_create(1);
3572  die_offset = root->ref;
3573 
3574  array_list_append(sal,(void *)(uintptr_t)symbol->ref);
3575 
3577  "expanding symbol(%s:0x%"PRIxOFFSET") in CU %s\n",
3578  symbol_get_name(symbol),die_offset,symbol_get_name(root));
3579 
3580  retval = dwarf_load_cu(srd,&die_offset,sal,1);
3581 
3582  array_list_free(sal);
3583 
3584  /*
3585  * (Ok, the following text is no longer true; we handle resolving
3586  * declarations dynamically if we're expanding symbols now.)
3587  *
3588  * NB: we have to do this at the very end, since it is not
3589  * per-CU. We can't always guarantee it happened when we unearthed
3590  * new globals or type definitions, because the datatypes of those
3591  * symbols may not have been resolved yet. So, we have to retry
3592  * this each time we load more content for the debugfile.
3593  */
3594 #if 0
3595  if (!retval)
3597 #endif
3598 
3599  return retval;
3600 }
3601 
3602 int dwarf_symbol_root_expand(struct debugfile *debugfile,struct symbol *root) {
3603  Dwarf_Off cu_offset = root->ref;
3604  int rc;
3605  struct symbol_root_dwarf *srd = SYMBOLX_ROOT(root)->priv;
3606 
3607  if (SYMBOL_IS_FULL(root)) {
3609  "CU %s already fully loaded!\n",symbol_get_name(root));
3610  return 0;
3611  }
3612 
3614  "loading entire CU %s (offset 0x%"PRIxOFFSET")!\n",
3615  symbol_get_name(root),cu_offset);
3616 
3617  rc = dwarf_load_cu(srd,&cu_offset,NULL,1);
3618 
3619  /*
3620  * (Ok, the following text is no longer true; we handle resolving
3621  * declarations dynamically if we're expanding symbols now.)
3622  *
3623  * NB: we have to do this at the very end, since it is not
3624  * per-CU. We can't always guarantee it happened when we unearthed
3625  * new globals or type definitions, because the datatypes of those
3626  * symbols may not have been resolved yet. So, we have to retry
3627  * this each time we load more content for the debugfile.
3628  */
3629 #if 0
3630  if (!rc)
3632 #endif
3633 
3634  return rc;
3635 }
3636 
3637 /*
3638  * Traverses the entire debuginfo section in order, one pass. For each
3639  * CU, parse it according to constraints in @srcfile_regex_list,
3640  * @symbol_regex_list, and @quick; then does a post-pass after each CU
3641  * to resolve references (necessary since we do a strict one-pass
3642  * traversal).
3643  *
3644  * If @srcfile_regex_list, and the CU srcfile name doesn't match
3645  * anything, skip the CU.
3646  *
3647  * Then, if we're processing the CU, and if @symbol_regex_list is set,
3648  * load all type symbols and inlined origins, and any other symbols that
3649  * match our regex list; but once we finish the CU, and have resolved
3650  * references, remove any symbols that have refcnt zero.
3651  *
3652  * Finally, if @quick, do not load any detailed information for symbols,
3653  * including children (i.e., params, members) -- EXCEPT for enumerated
3654  * vars. They are technically children, but they are in the namespace
3655  * of the parent.
3656  *
3657  * If the user supplies cu_die_offsets, we only do the CUs specified
3658  * (and debuginfo_load_cu might only do the DIEs requested too!). That
3659  * works like this:
3660  *
3661  * Traverses the debuginfo section by hopping around as needed, with a
3662  * post-pass to resolve accumulated references. We start with an
3663  * initial set of symbol names that map to struct dwarf_cu_die_ref
3664  * offset pairs. If the DEBUGFILE_LOAD_FLAG_FULL_CU flag is set, we
3665  * load the full CU for any offset. Otherwise, we load only the
3666  * specified DIE, save off any other DIEs it references, and add those
3667  * offsets to a list to load. If we don't load full CUs, we have to do
3668  * the post-pass on the reftab/abs origin stuff ourselves still, because
3669  * we're not going to handle the DIE's refs recursively.
3670  */
3671 static int debuginfo_load(struct debugfile *debugfile,
3672  Dwfl_Module *dwflmod,Dwarf *dbg) {
3673  struct debugfile_load_opts *dopts = debugfile->opts;
3674  int rc;
3675  int retval = 0;
3676  GHashTable *cu_die_offsets = NULL;
3677  Dwarf_Off offset = 0;
3678  struct symbol_root_dwarf *srd;
3679  gpointer cu_offset;
3680  struct array_list *die_offsets = NULL;
3681  GHashTableIter iter;
3682  struct dwarf_cu_die_ref *dcd;
3683  struct array_list *tmpal;
3684  int i;
3685  gpointer key;
3686  gpointer value;
3687  struct rfilter_entry *rfe;
3688  int accept = RF_ACCEPT;
3689 
3690  vdebug(1,LA_DEBUG,LF_DWARF,"starting on %s \n",debugfile->filename);
3691 
3692  if (dopts->flags & DEBUGFILE_LOAD_FLAG_PUBNAMES) {
3693  offset = OFFSETMAX;
3694  cu_die_offsets = g_hash_table_new_full(g_direct_hash,g_direct_equal,
3695  NULL,
3696  (GDestroyNotify)array_list_free);
3697  g_hash_table_iter_init(&iter,debugfile->pubnames);
3698  while (g_hash_table_iter_next(&iter,&key,(gpointer)&value)) {
3699  dcd = (struct dwarf_cu_die_ref *)value;
3700  if (!(tmpal = (struct array_list *) \
3701  g_hash_table_lookup(cu_die_offsets,
3702  (gpointer)(uintptr_t)dcd->cu_offset))) {
3703  tmpal = array_list_create(1);
3704  g_hash_table_insert(cu_die_offsets,
3705  (gpointer)(uintptr_t)dcd->cu_offset,
3706  (gpointer *)tmpal);
3707  }
3708 
3709  /* Check the pubname against our rfilter of symbol names, if
3710  * any, and skip or include it as the rfilter dictates.
3711  *
3712  * NOTE that we insert an empty list for the CU to make sure
3713  * its header gets loaded. This way (since each CU in a
3714  * sane C program will have at least one thing in
3715  * debug_pubnames -- otherwise what's the point?), we force
3716  * each CU header to be parsed. If this turns out to not
3717  * work well enough, we'll have to force it another way.
3718  */
3719  if (dopts->symbol_filter) {
3720  rfilter_check(dopts->symbol_filter,(char *)key,
3721  &accept,&rfe);
3722  if (accept == RF_REJECT)
3723  continue;
3724  }
3725 
3726  array_list_append(tmpal,(void *)(uintptr_t)(dcd->die_offset \
3727  + dcd->cu_offset));
3728  if ((Dwarf_Off)dcd->cu_offset < offset)
3729  offset = (Dwarf_Off)dcd->cu_offset;
3730  }
3731 
3732  g_hash_table_iter_init(&iter,cu_die_offsets);
3733  while (g_hash_table_iter_next(&iter,&cu_offset,&value)) {
3734  tmpal = (struct array_list *)value;
3735  vdebug(5,LA_DEBUG,LF_DWARF,"preloading offsets for CU 0x%"PRIxSMOFFSET": ",
3736  (SMOFFSET)(uintptr_t)cu_offset);
3737  for (i = 0; i < array_list_len(tmpal); ++i) {
3738  vdebugc(5,LA_DEBUG,LF_DWARF,"0x%"PRIxSMOFFSET" ",
3739  (SMOFFSET)(uintptr_t)array_list_item(tmpal,i));
3740  }
3741  vdebugc(5,LA_DEBUG,LF_DWARF,"\n");
3742  }
3743 
3744  /* Get the first one to seed the loop below. */
3745  g_hash_table_iter_init(&iter,cu_die_offsets);
3746  if (!g_hash_table_iter_next(&iter,&cu_offset,&value))
3747  goto out;
3748  die_offsets = (struct array_list *)value;
3749  offset = (Dwarf_Off)(uintptr_t)cu_offset;
3750  }
3751 
3752  while (1) {
3753  srd = (struct symbol_root_dwarf *)calloc(1,sizeof(*srd));
3754  srd->debugfile = debugfile;
3755  srd->dwflmod = dwflmod;
3756  srd->dbg = dbg;
3757  srd->addrsize = 0;
3758  srd->offsize = 0;
3759 
3760 #if defined(LIBDW_HAVE_NEXT_UNIT) && LIBDW_HAVE_NEXT_UNIT == 1
3761  if ((rc = dwarf_next_unit(dbg,offset,&srd->nextcu,&srd->cuhl,
3762  &srd->version,&srd->abbroffset,&srd->addrsize,
3763  &srd->offsize,NULL,NULL)) < 0) {
3764  verror("dwarf_next_unit: %s (%d)\n",dwarf_errmsg(dwarf_errno()),rc);
3765  free(srd);
3766  goto errout;
3767  }
3768  else if (rc > 0) {
3770  "dwarf_next_unit returned (%d), aborting successfully.\n",rc);
3771  free(srd);
3772  goto out;
3773  }
3774 #else
3775  if ((rc = dwarf_nextcu(dbg,offset,&srd->nextcu,&srd->cuhl,
3776  &srd->abbroffset,&srd->addrsize,&
3777  srd->offsize)) < 0) {
3778  verror("dwarf_nextcu: %s (%d)\n",dwarf_errmsg(dwarf_errno()),rc);
3779  free(srd);
3780  goto errout;
3781  }
3782  else if (rc > 0) {
3784  "dwarf_nextcu returned (%d), aborting successfully.\n",rc);
3785  free(srd);
3786  goto out;
3787  }
3788 
3789  vwarnopt(4,LA_DEBUG,LF_DWARF,"assuming DWARF version 4; old elfutils!\n");
3790  srd->version = 4;
3791 #endif
3792 
3793  /*
3794  * Make a reftab for loading the CU. This is alive until the CU
3795  * is fully loaded. Also, all symbols in it have refs held on
3796  * them by the srd->reftab (itself).
3797  */
3798  srd->reftab = g_hash_table_new(g_direct_hash,g_direct_equal);
3799  /*
3800  * Make a range-searchable list of the top-level die offsets in
3801  * this CU. We have to do this because if we load DIEs out of
3802  * order, one DIE may reference another DIE that is in a
3803  * different parent hierarchy -- and we might not have loaded
3804  * that parent hierarchy! We could throw in all kinds of
3805  * optimizations to try to figure out exactly how many DIEs in
3806  * that hierarchy we have to load, but since dwarf_load_cu isn't
3807  * well-ordered towards loading individual DIEs and reconciling
3808  * their parent hierarchy *after* loading, what we will do is,
3809  * when loading a particular referenced DIE, we will fully load
3810  * the top-level die containing it. Again, this is a balance
3811  * between simplicity of implementation and runtime speed for
3812  * the PARTIALSYM case.
3813  *
3814  * We create this list like this. If PARTIALSYM, we build it up
3815  * as we come across new top-level DIEs during our in-order
3816  * traversal. If not PARTIALSYM, and (CUHEADERS || PUBNAMES),
3817  * we pre-scan the top-level DIEs IFF they have sibling
3818  * attributes (but if there are no sibling attributes, we will
3819  * just have to expand the whole CU to the one DIE we need!).
3820  *
3821  * Then, when we want to load a partial symbol at DIE offset X,
3822  * we just find the previous top-level DIE to load!
3823  */
3825  srd->refuselist = g_hash_table_new(g_direct_hash,g_direct_equal);
3826  srd->spec_reftab = g_hash_table_new(g_direct_hash,g_direct_equal);
3827 
3828  rc = dwarf_load_cu(srd,&offset,die_offsets,0);
3829 
3830  if (rc) {
3831  retval = -1;
3832  goto errout;
3833  }
3834 
3835  if (cu_die_offsets) {
3836  if (!g_hash_table_iter_next(&iter,&cu_offset,
3837  &value))
3838  break;
3839  die_offsets = (struct array_list *)value;
3840  offset = (Dwarf_Off)(uintptr_t)cu_offset;
3841  }
3842  else {
3843  offset = srd->nextcu;
3844  if (offset == 0)
3845  break;
3846  }
3847  }
3848 
3849  goto out;
3850 
3851  errout:
3852  retval = -1;
3853 
3854  out:
3855 
3856  /*
3857  * This is now dynamically handled for symbol expansion, but we're
3858  * not doing that here, so do it one final time.
3859  *
3860  * XXX: NB: we have to do this at the very end, since it is not
3861  * per-CU. We can't always guarantee it happened when we unearthed
3862  * new globals or type definitions, because the datatypes of those
3863  * symbols may not have been resolved yet. So, we have to retry
3864  * this each time we load more content for the debugfile.
3865  */
3866  if (retval == 0) {
3867  debugfile_resolve_declarations(debugfile);
3868  }
3869  if (dopts->flags & DEBUGFILE_LOAD_FLAG_PUBNAMES)
3870  g_hash_table_destroy(cu_die_offsets);
3871  return retval;
3872 }
3873 
3874 /*
3875  * If we have to change the name of the symbol, we do it IMMEDIATELY
3876  * after parsing DIE attrs so that naming is consistent for all the
3877  * hashtable insertions we do!
3878  */
3879 void finalize_die_symbol_name(struct symbol *symbol) {
3880  if (symbol->name && !symbol->orig_name_offset
3881  && (SYMBOL_IST_STUN(symbol) || SYMBOL_IST_ENUM(symbol))) {
3882  /*
3883  * NOTE!!! If this is a struct, union, or enum type, we
3884  * *have* to place the struct/union/enum type in front of
3885  * the alphanumeric name, since you can have typedefs that
3886  * are named the same name as a struct/union/enum. So we
3887  * have to use the full type name as the hashtable key;
3888  * otherwise we'll see collisions with typedefs.
3889  *
3890  * This means the user has to lookup those types with the
3891  * fully-qualified type names (i.e., 'struct task_struct'),
3892  * not just 'task_struct'.
3893  */
3894  symbol_build_extname(symbol);
3895  }
3896 }
3897 
3898 /*
3899  * Returns 0 if the symbol was successfully inserted into symbol tables,
3900  * and 1 if not (which may not be an error).
3901  */
3902 void finalize_die_symbol(struct debugfile *debugfile,int level,
3903  struct symbol *symbol,
3904  struct symbol *parentsymbol,
3905  struct symbol *voidsymbol,
3906  GHashTable *reftab,struct array_list *die_offsets,
3907  SMOFFSET cu_offset) {
3908  struct location *loc;
3909 
3910  if (!symbol) {
3911  verror("null symbol!\n");
3912  return;
3913  }
3914 
3915  /*
3916  * First, handle symbols that need a type. Declarations don't get a
3917  * type if they don't have one!
3918  */
3919  if (!symbol->isdeclaration && !symbol->isinlineinstance
3920  && symbol->datatype == NULL && symbol->datatype_ref == 0) {
3921  if (SYMBOL_IS_TYPE(symbol)) {
3922  /* If it's a valid symbol, and it's not a base type, but doesn't
3923  * have a type, make it void!
3924  */
3925  if (!SYMBOL_IST_BASE(symbol)
3926  && (SYMBOL_IST_PTR(symbol)
3927  || SYMBOL_IST_TYPEDEF(symbol)
3928  /* Not sure if C lets these cases through, but whatever */
3929  || SYMBOL_IST_CONST(symbol)
3930  || SYMBOL_IST_VOL(symbol)
3931  || SYMBOL_IST_FUNC(symbol))) {
3933  "setting symbol(%s:0x%"PRIxSMOFFSET") type %s"
3934  " without type to void\n",
3935  symbol->name,symbol->ref,DATATYPE(symbol->datatype_code));
3936  symbol->datatype = voidsymbol;
3937  }
3938  }
3939  else if (SYMBOL_IS_FUNC(symbol) || SYMBOL_IS_VAR(symbol)) {
3941  "setting symbol(%s:0x%"PRIxSMOFFSET") type %s"
3942  " without type to void\n",
3943  symbol->name,symbol->ref,DATATYPE(symbol->datatype_code));
3944  symbol->datatype = voidsymbol;
3945  }
3946  }
3947 
3948  /*
3949  * Set a member offset of 0 for each union's member.
3950  */
3951  if (SYMBOL_IS_VAR(symbol)
3952  && parentsymbol && SYMBOL_IST_UNION(parentsymbol)) {
3953  loc = location_create();
3955  symbol_set_location(symbol,loc);
3956  }
3957 
3958  /*
3959  * If we have a base_addr for the symbol, insert it into the
3960  * addresses table.
3961  */
3962  if (SYMBOL_IS_INSTANCE(symbol) && symbol_get_name_orig(symbol)) {
3963  if (symbol_has_addr(symbol)) {
3964  g_hash_table_insert(debugfile->addresses,
3965  (gpointer)(uintptr_t)symbol_get_addr(symbol),
3966  symbol);
3968  "inserted %s %s(0x%"PRIxSMOFFSET") with base_addr 0x%"PRIxADDR
3969  " into debugfile addresses table\n",
3970  SYMBOL_TYPE(symbol->type),symbol_get_name(symbol),symbol->ref,
3971  symbol_get_addr(symbol));
3972  }
3973  }
3974 
3975  /*
3976  * If we're doing a partial CU load (i.e., loading specific DIE
3977  * offset within this CU), any other symbols referenced by this
3978  * symbol need to get appended to our DIE load list if we haven't
3979  * already loaded them!
3980  */
3981  if (die_offsets) {
3982  if (!symbol->datatype
3983  && symbol->datatype_ref
3984  && !(symbol->datatype = (struct symbol *) \
3985  g_hash_table_lookup(reftab,
3986  (gpointer)(uintptr_t)symbol->datatype_ref))) {
3987  array_list_append(die_offsets,
3988  (void *)(uintptr_t)symbol->datatype_ref);
3989  }
3990 
3991  /* We can't do this for inlined formal params, vars, or labels,
3992  * because we will jump into the middle of a subprogram, without
3993  * knowing we are in that subprogram DIE -- so our symtab
3994  * hierarchy will be screwed up!
3995  *
3996  * Ok, now we *can* do this, because our symbol expander will
3997  * load the containing top-level symbol even if the symbol is a
3998  * param, var, or label that is not at level 1!
3999  */
4000  SYMBOL_RX_INLINE(symbol,sii);
4001  if (symbol->isinlineinstance && sii && sii->origin_ref) {
4002  array_list_append(die_offsets,
4003  (void *)(uintptr_t)sii->origin_ref);
4004  }
4005  }
4006 
4007  /*
4008  * Generate names for symbols if we need to; handle declarations; ...
4009  */
4010  if (SYMBOL_IS_TYPE(symbol) && symbol_get_name_orig(symbol)) {
4011  if (parentsymbol && SYMBOL_IS_ROOT(parentsymbol)) {
4012  if (!(debugfile->opts->flags & DEBUGFILE_LOAD_FLAG_REDUCETYPES)
4013  && !symbol->isdeclaration)
4014  debugfile_add_type(debugfile,symbol);
4015 
4016  if (0 && symbol->isdeclaration)
4017  debugfile_handle_declaration(debugfile,symbol);
4018  }
4019  }
4020  else if (SYMBOL_IS_INSTANCE(symbol) && symbol_get_name_orig(symbol)) {
4021  if (parentsymbol && SYMBOL_IS_ROOT(parentsymbol)) {
4022  if (!symbol->has_linkage_name
4023  && symbol->isexternal
4024  && !symbol->isdeclaration)
4025  debugfile_add_global(debugfile,symbol);
4026  else if (0 && symbol->isdeclaration)
4027  debugfile_handle_declaration(debugfile,symbol);
4028  }
4029  }
4030  else if (SYMBOL_IS_INSTANCE(symbol) && symbol->isinlineinstance) {
4031  /* An inlined instance; definitely need it in the symbol
4032  * tables. But we have to give it a name. And the name *has*
4033  * to be unique... so we do our best:
4034  * __INLINED(<symbol_mem_addr>:(iref<src_sym_dwarf_addr>
4035  * |<src_sym_name))
4036  * (we really should use the DWARF DIE addr for easier debug,
4037  * but that would cost us 8 bytes more in the symbol struct.)
4038  */
4039  SYMBOL_RX_INLINE(symbol,sii);
4040 
4041  /* XXX: don't give inline instances names?? */
4042 
4043  if (0 && sii) {
4044  char *inname;
4045  int inlen;
4046  if (sii->origin) {
4047  inlen = 9 + 1 + 18 + 1 + strlen(symbol_get_name_orig(sii->origin)) + 1 + 1;
4048  inname = malloc(sizeof(char)*inlen);
4049  sprintf(inname,"__INLINED(ref%"PRIxSMOFFSET":%s)",
4050  symbol->ref,symbol_get_name_orig(sii->origin));
4051  }
4052  else {
4053  inlen = 9 + 1 + 18 + 1 + 4 + 16 + 1 + 1;
4054  inname = malloc(sizeof(char)*inlen);
4055  sprintf(inname,"__INLINED(ref%"PRIxSMOFFSET":iref%"PRIxSMOFFSET")",
4056  symbol->ref,sii->origin_ref);
4057  }
4058 
4059  symbol_set_name(symbol,inname,1);
4060  free(inname);
4061  }
4062  }
4063  else if (SYMBOL_IS_VAR(symbol) && symbol->isparam
4064  && parentsymbol && SYMBOL_IS_FUNC(parentsymbol)
4065  && parentsymbol->isinlineinstance) {
4066  /* Sometimes it seems we see unnamed function params that are
4067  * not marked as inline instances, BUT have a subprogram parent
4068  * that is an inline instance.
4069  */
4070  ;
4071  }
4072  else if (SYMBOL_IS_VAR(symbol) && (symbol->isparam || symbol->ismember)) {
4073  /* We allow unnamed params, of course, BUT we don't put them
4074  * into the symbol table. We leave them on the function
4075  * symbol/function type to be freed in symbol_free!
4076  *
4077  * XXX: we only need this for subroutine type formal parameters;
4078  * should we make the check above more robust?
4079  */
4080  ;
4081  }
4082 
4083  vdebug(5,LA_DEBUG,LF_SYMBOL,"finalized %s symbol(%s:0x%"PRIxSMOFFSET") %p\n",
4084  SYMBOL_TYPE(symbol->type),symbol->name,symbol->ref,symbol);
4085 }
4086 
4087 int dwarf_load_pubnames(struct debugfile *debugfile,
4088  unsigned char *buf,unsigned int len,Dwarf *dbg) {
4089  const unsigned char *readp = buf;
4090  const unsigned char *readendp = buf + len;
4091  /* XXX: we can't get other_byte_order from dbg since we don't have
4092  * the struct def for it... so we assume it's not a diff byte order
4093  * than the phys host for now.
4094  */
4095  int obo = 0;
4096 
4097  while (readp < readendp) {
4098  /* Each entry starts with a header:
4099 
4100  1. A 4-byte or 12-byte length containing the length of the
4101  set of entries for this compilation unit, not including the
4102  length field itself. [...]
4103 
4104  2. A 2-byte version identifier containing the value 2 for
4105  DWARF Version 2.1.
4106 
4107  3. A 4-byte or 8-byte offset into the .debug_info section. [...]
4108 
4109  4. A 4-byte or 8-byte length of the CU in the .debug_info section.
4110  */
4111 
4112  Dwarf_Word length = read_4ubyte_unaligned_inc(obo,readp);
4113  int is64 = 0;
4114 
4115  if (length == DWARF3_LENGTH_64_BIT) {
4117  "64-bit DWARF length %"PRIu64"; continuing.\n",length);
4118  length = read_8ubyte_unaligned_inc(obo,readp);
4119  is64 = 1;
4120  }
4121  else if (unlikely(length >= DWARF3_LENGTH_MIN_ESCAPE_CODE
4122  && length <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
4124  "bad DWARF length %"PRIu64"; continuing anyway!\n",length);
4125 
4126  unsigned int version = read_2ubyte_unaligned_inc(obo,readp);
4127  if (version != 2)
4129  "bad DWARF arange version %u; continuing anyway!\n",
4130  version);
4131 
4132  Dwarf_Word offset = read_4ubyte_unaligned_inc(obo,readp);
4133 
4134  /* Dwarf_Word cu_length = */ read_4ubyte_unaligned_inc(obo,readp);
4135 
4136  while (1) {
4137  Dwarf_Word die_offset;
4138 
4139  if (is64)
4140  die_offset = read_8ubyte_unaligned_inc(obo,readp);
4141  else
4142  die_offset = read_4ubyte_unaligned_inc(obo,readp);
4143 
4144  /* A zero value marks the end. */
4145  if (die_offset == 0)
4146  break;
4147 
4148  /* Use a global offset, not a per-CU offset. */
4149  /* die_offset += offset; */
4150 
4151  /* XXX: this is wasteful for 64-bit hosts; we could save all
4152  * these mallocs by just using a u64 and giving half the
4153  * bits to the cuoffset and half to the dieoffset.
4154  */
4155  struct dwarf_cu_die_ref *dcd = (struct dwarf_cu_die_ref *) \
4156  malloc(sizeof(*dcd));
4157  dcd->cu_offset = offset;
4158  dcd->die_offset = die_offset;
4159 
4160  g_hash_table_insert(debugfile->pubnames,strdup((const char *)readp),
4161  (gpointer)dcd);
4162  readp += strlen((const char *)readp) + 1;
4163  }
4164  }
4165 
4166  return 0;
4167 }
4168 
4169 int dwarf_load_aranges(struct debugfile *debugfile,
4170  unsigned char *buf,unsigned int len,Dwarf *dbg) {
4171  struct symbol *root;
4172  struct scope *cu_scope;
4173  const unsigned char *readp = buf;
4174  const unsigned char *readendp = buf + len;
4175  /* XXX: we can't get other_byte_order from dbg since we don't have
4176  * the struct def for it... so we assume it's not a diff byte order
4177  * than the phys host for now.
4178  */
4179  int obo = 0;
4180 
4181  while (readp < readendp) {
4182  const unsigned char *hdrstart = readp;
4183 
4184  /* Each entry starts with a header:
4185 
4186  1. A 4-byte or 12-byte length containing the length of the
4187  set of entries for this compilation unit, not including the
4188  length field itself. [...]
4189 
4190  2. A 2-byte version identifier containing the value 2 for
4191  DWARF Version 2.1.
4192 
4193  3. A 4-byte or 8-byte offset into the .debug_info section. [...]
4194 
4195  4. A 1-byte unsigned integer containing the size in bytes of
4196  an address (or the offset portion of an address for segmented
4197  addressing) on the target system.
4198 
4199  5. A 1-byte unsigned integer containing the size in bytes of
4200  a segment descriptor on the target system.
4201  */
4202 
4203  Dwarf_Word length = read_4ubyte_unaligned_inc(obo,readp);
4204 
4205  if (length == DWARF3_LENGTH_64_BIT) {
4207  "64-bit DWARF length %"PRIu64"; continuing.\n",length);
4208  length = read_8ubyte_unaligned_inc(obo,readp);
4209  }
4210  else if (unlikely(length >= DWARF3_LENGTH_MIN_ESCAPE_CODE
4211  && length <= DWARF3_LENGTH_MAX_ESCAPE_CODE))
4213  "bad DWARF length %"PRIu64"; continuing anyway!\n",length);
4214 
4215  unsigned int version = read_2ubyte_unaligned_inc(obo,readp);
4216  if (version != 2)
4218  "bad DWARF arange version %u; continuing anyway!\n",
4219  version);
4220 
4221  Dwarf_Word offset = read_4ubyte_unaligned_inc(obo,readp);
4222 
4223  unsigned int address_size = *readp++;
4224  if (address_size != 4 && address_size != 8)
4226  "bad DWARF address size %u; continuing anyway!\n",
4227  address_size);
4228 
4229  /* unsigned int segment_size = * */ readp++;
4230 
4231  /* Round the address to the next multiple of 2*address_size. */
4232  readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
4233  % (2 * address_size));
4234 
4235  /*
4236  * Lookup the scope at this offset, or create one if it doesn't
4237  * exist yet.
4238  */
4239  root = debugfile_lookup_root(debugfile,offset);
4240  if (!root) {
4242  (SMOFFSET)offset,LOADTYPE_PARTIAL,NULL);
4243  debugfile_insert_root(debugfile,root);
4244  }
4245  cu_scope = symbol_write_owned_scope(root);
4246 
4247  while (1) {
4248  Dwarf_Word range_address;
4249  Dwarf_Word range_length;
4250  int action = 0;
4251 
4252  if (address_size == 8) {
4253  range_address = read_8ubyte_unaligned_inc(obo,readp);
4254  range_length = read_8ubyte_unaligned_inc(obo,readp);
4255  }
4256  else {
4257  range_address = read_4ubyte_unaligned_inc(obo,readp);
4258  range_length = read_4ubyte_unaligned_inc(obo,readp);
4259  }
4260 
4261  /* Two zero values mark the end. */
4262  if (range_address == 0 && range_length == 0)
4263  break;
4264 
4265  scope_update_range(cu_scope,range_address,
4266  range_address + range_length,&action);
4267 
4268  /*
4269  * We always populate the debugfile->ranges struct with CU
4270  * range info, no matter what the ALLRANGES flag is set to.
4271  */
4272  if (action == 1)
4273  clrange_add(&debugfile->ranges,range_address,
4274  range_address + range_length,cu_scope);
4275  else if (action == 2)
4276  clrange_update_end(&debugfile->ranges,range_address,
4277  range_address + range_length,cu_scope);
4278  }
4279  }
4280 
4281  return 0;
4282 }
4283 
4284 static int process_dwflmod (Dwfl_Module *dwflmod,
4285  void **userdata __attribute__ ((unused)),
4286  const char *uname __attribute__ ((unused)),
4287  Dwarf_Addr base __attribute__ ((unused)),
4288  void *arg) {
4289  struct debugfile *debugfile = (struct debugfile *)arg;
4290  struct dwarf_debugfile_info *ddi =
4291  (struct dwarf_debugfile_info *)debugfile->priv;
4292  struct debugfile_load_opts *dopts = debugfile->opts;
4293  struct binfile *binfile = debugfile->binfile;
4294  struct binfile *binfile_pointing = NULL;
4295  struct binfile_elf *bfelf = (struct binfile_elf *)binfile->priv;
4296  struct binfile_elf *bfelf_pointing = NULL;
4297  GElf_Addr dwflbias;
4298  Dwarf_Addr dwbias;
4299  Dwarf *dbg;
4300  GElf_Shdr *shdr;
4301  Elf_Scn *scn;
4302  int i,ii;
4303  char *name,*name2;
4304  char **saveptr;
4305  unsigned int *saveptrlen;
4306  Elf_Data *edata;
4307 
4308  /*
4309  * Grab the pointing binfile in case it has section contents that
4310  * binfile does not.
4311  */
4312  binfile_pointing = debugfile->binfile_pointing;
4313  if (binfile_pointing)
4314  bfelf_pointing = (struct binfile_elf *)binfile_pointing->priv;
4315 
4316  dwfl_module_getelf(dwflmod,&dwflbias);
4317 
4318  /*
4319  * Last setup before parsing DWARF stuff!
4320  */
4321  dbg = dwfl_module_getdwarf(dwflmod,&dwbias);
4322  if (!dbg) {
4324  "could not get dwarf module in debugfile %s!\n",
4325  debugfile->filename);
4326  goto errout;
4327  }
4328 
4329  for (i = 0; i < bfelf->ehdr.e_shnum; ++i) {
4330  shdr = &bfelf->shdrs[i];
4331  scn = elf_getscn(bfelf->elf,i);
4332 
4333  if (shdr && shdr->sh_size > 0) { // &&shdr->sh_type != SHT_PROGBITS) {
4334  //shdr_mem.sh_flags & SHF_STRINGS) {
4335  name = elf_strptr(bfelf->elf,bfelf->shstrndx,shdr->sh_name);
4336 
4337  if (strcmp(name,".debug_str") == 0) {
4338  saveptr = &debugfile->dbg_strtab;
4339  saveptrlen = &debugfile->dbg_strtablen;
4340  }
4341  else if (strcmp(name,".debug_loc") == 0) {
4342  saveptr = &debugfile->loctab;
4343  saveptrlen = &debugfile->loctablen;
4344  }
4345  else if (strcmp(name,".debug_ranges") == 0) {
4346  saveptr = &debugfile->rangetab;
4347  saveptrlen = &debugfile->rangetablen;
4348  }
4349  else if (strcmp(name,".debug_line") == 0) {
4350  saveptr = &debugfile->linetab;
4351  saveptrlen = &debugfile->linetablen;
4352  }
4353  else if (strcmp(name,".eh_frame") == 0) {
4354  if (debugfile->frametab) {
4355  vwarn("already saw frame section; not processing"
4356  " .eh_frame!\n");
4357  continue;
4358  }
4359  ddi->is_eh_frame = 1;
4360  ddi->frame_sec_offset = shdr->sh_offset;
4361  ddi->frame_sec_addr = shdr->sh_addr;
4362  saveptr = &debugfile->frametab;
4363  saveptrlen = &debugfile->frametablen;
4364  }
4365  else if (strcmp(name,".debug_frame") == 0) {
4366  if (debugfile->frametab) {
4367  vwarn("already saw frame section; not processing"
4368  " .debug_frame!\n");
4369  continue;
4370  }
4371  ddi->frame_sec_offset = shdr->sh_offset;
4372  ddi->frame_sec_addr = shdr->sh_addr;
4373  saveptr = &debugfile->frametab;
4374  saveptrlen = &debugfile->frametablen;
4375  }
4376  else if (strcmp(name,".debug_aranges") == 0
4377  || strcmp(name,".debug_pubnames") == 0) {
4378  /* Skip to the !saveptr checks below and load these
4379  * sections immediately.
4380  */
4381  saveptr = NULL;
4382  saveptrlen = NULL;
4383  }
4384  else {
4385  continue;
4386  }
4387 
4389  "found %s section (%d) (%p) in debugfile %s\n",
4390  name,shdr->sh_size,scn,debugfile->filename);
4391 
4392  edata = elf_rawdata(scn,NULL);
4393  if (!edata) {
4394  verror("cannot get raw data for valid section '%s': %s\n",
4395  name,elf_errmsg(-1));
4396  continue;
4397  }
4398  else if (!edata->d_buf && edata->d_size) {
4399  scn = NULL;
4400  if (bfelf_pointing && bfelf_pointing->elf) {
4402  "cannot get raw data for valid section '%s': %s;"
4403  " trying getdata on the pointing binfile\n",
4404  name,elf_errmsg(-1));
4405 
4406  for (ii = 0; ii < bfelf_pointing->ehdr.e_shnum; ++ii) {
4407  shdr = &bfelf_pointing->shdrs[ii];
4408  scn = elf_getscn(bfelf_pointing->elf,ii);
4409 
4410  if (shdr && shdr->sh_size > 0) { // &&shdr->sh_type != SHT_PROGBITS) {
4411  //shdr_mem.sh_flags & SHF_STRINGS) {
4412  name2 = elf_strptr(bfelf_pointing->elf,
4413  bfelf_pointing->shstrndx,shdr->sh_name);
4414 
4415  if (strcmp(name,name2) == 0) {
4416  edata = elf_getdata(scn,NULL);
4417  if (!edata) {
4418  verror("still cannot get data for valid section '%s': %s\n",
4419  name,elf_errmsg(-1));
4420  scn = NULL;
4421  break;
4422  }
4423  else if (!edata->d_buf) {
4424  vwarn("still cannot get data for valid section '%s' (%d);"
4425  " skipping!\n",
4426  name,(int)edata->d_size);
4427  scn = NULL;
4428  break;
4429  }
4430 
4431  break;
4432  }
4433  }
4434  scn = NULL;
4435  }
4436 
4437  if (!scn) {
4438  verror("cannot get raw data for valid section '%s': %s;"
4439  " could not getdata on the pointing binfile\n",
4440  name,elf_errmsg(-1));
4441  continue;
4442  }
4443  }
4444  else {
4445  verror("cannot get raw data for valid section '%s': %s;"
4446  " could not try getdata on the pointing binfile\n",
4447  name,elf_errmsg(-1));
4448  continue;
4449  }
4450  }
4451 
4452  /*
4453  * aranges and pubnames are special. We parse them
4454  * *immediately*, using them to setup our CU scopes, which
4455  * are then validated and filled in as we parse debuginfo.
4456  */
4457  if (!saveptr) {
4458  if (strcmp(name,".debug_aranges") == 0) {
4459  dwarf_load_aranges(debugfile,edata->d_buf,edata->d_size,dbg);
4460  continue;
4461  }
4462  else if (strcmp(name,".debug_pubnames") == 0) {
4463  dwarf_load_pubnames(debugfile,edata->d_buf,edata->d_size,dbg);
4464  continue;
4465  }
4466  }
4467  else {
4468  /*
4469  * We just malloc a big buf now, and then we don't free
4470  * anything in scopes or syms that is present in here!
4471  */
4472  *saveptrlen = edata->d_size;
4473  *saveptr = malloc(edata->d_size);
4474  memcpy(*saveptr,edata->d_buf,edata->d_size);
4475 
4476  /*
4477  * frames is also special, but we need the persistent
4478  * copy to stay in memory for on-demand evaluation as we
4479  * use debuginfo to load symbols and examine memory. We
4480  * also pre-process it just to create an in-memory index
4481  * of DWARF FDEs that can be decoded later, on-demand.
4482  */
4483  if (strcmp(name,".debug_frame") == 0
4484  || strcmp(name,".eh_frame") == 0) {
4485  dwarf_load_cfa(debugfile,*saveptr,*saveptrlen,dbg);
4486  }
4487  }
4488  }
4489  else if (shdr && shdr->sh_size == 0) {
4490  vdebug(2,LA_DEBUG,LF_DWARF,"section empty, which is fine!\n");
4491  }
4492  }
4493  if (!debugfile->dbg_strtab) {
4494  vwarn("no debug string table found for debugfile %s; things may break!\n",
4495  debugfile->filename);
4496  }
4497 
4498  /* now rescan for debug_info sections */
4499  for (i = 0; i < bfelf->ehdr.e_shnum; ++i) {
4500  shdr = &bfelf->shdrs[i];
4501  scn = elf_getscn(bfelf->elf,i);
4502 
4503  if (shdr && shdr->sh_size > 0 && shdr->sh_type == SHT_PROGBITS) {
4504  name = elf_strptr(bfelf->elf,bfelf->shstrndx,shdr->sh_name);
4505 
4506  if (strcmp(name,".debug_info") == 0) {
4508  "found .debug_info section in debugfile %s\n",
4509  debugfile->filename);
4510  debuginfo_load(debugfile,dwflmod,dbg);
4511  //break;
4512  }
4513  }
4514  }
4515 
4516  /* Don't free any of the copied ELF data structs if we're partially
4517  * loading the debuginfo!
4518  */
4521  || dopts->flags & DEBUGFILE_LOAD_FLAG_PUBNAMES)
4522  goto out;
4523 
4524  /* Now free up the temp loc/range tables. */
4525  if (debugfile->loctab) {
4526  free(debugfile->loctab);
4527  debugfile->loctablen = 0;
4528  debugfile->loctab = NULL;
4529  }
4530  if (debugfile->rangetab) {
4531  free(debugfile->rangetab);
4532  debugfile->rangetablen = 0;
4533  debugfile->rangetab = NULL;
4534  }
4535  if (debugfile->linetab) {
4536  free(debugfile->linetab);
4537  debugfile->linetablen = 0;
4538  debugfile->linetab = NULL;
4539  }
4540  /*
4541  * Only save dbg_strtab if we're gonna use it.
4542  */
4543 #ifdef DWDEBUG_NOUSE_STRTAB
4544  if (debugfile->dbg_strtab) {
4545  free(debugfile->dbg_strtab);
4546  debugfile->dbg_strtablen = 0;
4547  debugfile->dbg_strtab = NULL;
4548  }
4549 #endif
4550 
4551  out:
4552  /* Let caller (or debugfile free) free ebl later. */
4553  return DWARF_CB_OK;
4554 
4555  errout:
4556  /* Let caller (or debugfile free) free ebl later. */
4557  return DWARF_CB_ABORT;
4558 }
4559 
4560 
4561 /*
4562  * Stub callback telling
4563  */
4564 static int find_no_debuginfo(Dwfl_Module *mod __attribute__ ((unused)),
4565  void **userdata __attribute__ ((unused)),
4566  const char *modname __attribute__ ((unused)),
4567  Dwarf_Addr base __attribute__ ((unused)),
4568  const char *file_name __attribute__ ((unused)),
4569  const char *debuglink_file __attribute__ ((unused)),
4570  GElf_Word debuglink_crc __attribute__ ((unused)),
4571  char **debuginfo_file_name __attribute__ ((unused))) {
4572  return -1;
4573 }
4574 
4575 static int bfi_find_elf(Dwfl_Module *mod __attribute__ ((unused)),
4576  void **userdata,
4577  const char *modname __attribute__ ((unused)),
4578  Dwarf_Addr base __attribute__ ((unused)),
4579  char **file_name __attribute__ ((unused)),
4580  Elf **elfp) {
4581  struct binfile *binfile;
4582  //struct binfile_instance *bfi;
4583  struct binfile_elf *bfelf;
4584 
4585  if (!(binfile = (struct binfile *)*userdata)) {
4586  verror("no binfile; bug!?\n");
4587  return -1;
4588  }
4589 
4590  //if (!(bfi = binfile->instance)) {
4591  // verror("no instance info for binfile %s!\n",binfile->filename);
4592  // return -1;
4593  //}
4594 
4595  if (!(bfelf = (struct binfile_elf *)binfile->priv)) {
4596  verror("no ELF info for binfile %s!\n",binfile->filename);
4597  return -1;
4598  }
4599 
4600  if (elfp)
4601  *elfp = bfelf->elf;
4602 
4603  return -1;
4604 }
4605 
4606 static int bfi_find_section_address(Dwfl_Module *mod,void **userdata,
4607  const char *modname,Dwarf_Addr base,
4608  const char *secname,GElf_Word shndx,
4609  const GElf_Shdr *shdr,Dwarf_Addr *addr) {
4610  struct binfile *binfile;
4611  struct binfile_instance *bfi;
4612  struct binfile_instance_elf *bfielf;
4613  ADDR tmp;
4614 
4615  if (!(binfile = (struct binfile *)*userdata)) {
4616  verror("no binfile; bug!?\n");
4617  return -1;
4618  }
4619 
4620  if (!(bfi = binfile->instance)) {
4621  verror("no instance info for binfile %s!\n",binfile->filename);
4622  return -1;
4623  }
4624 
4625  if (!(bfielf = (struct binfile_instance_elf *)(bfi->priv))) {
4626  verror("no ELF instance info for binfile %s!\n",binfile->filename);
4627  return -1;
4628  }
4629 
4630  if (addr) {
4631  if (shndx >= bfielf->num_sections) {
4632  /*
4633  * XXX: we probably were using a binfile instance that
4634  * doesn't correspond exactly to the binfile; but in this
4635  * case, we can just pretend that the section didn't get
4636  * mapped :).
4637  */
4639  "section %d out of range (%d) in binfile instance %s!\n",
4640  shndx,bfielf->num_sections,bfi->filename);
4641  tmp = 0;
4642  //return -1;
4643  }
4644  else
4645  tmp = ((struct binfile_instance_elf *)(bfi->priv))->section_tab[shndx];
4646 
4648  "shndx = %d addr = %"PRIxADDR" base = %x\n",
4649  shndx,tmp,base);
4650 
4651  if (tmp == 0)
4652  /*
4653  * If the section was not mapped, this is how we tell
4654  * elfutils.
4655  */
4656  *addr = (Dwarf_Addr)-1L;
4657  else
4658  *addr = (Dwarf_Addr)tmp;
4659  }
4660 
4661  return DWARF_CB_OK;
4662 }
4663 
4664 /*
4665  * Primary debuginfo interface. Given an ELF filename, load all its
4666  * debuginfo into the supplied debugfile using elfutils libs.
4667  */
4668 int dwarf_load_debuginfo(struct debugfile *debugfile) {
4669  struct debugfile_load_opts *dopts = debugfile->opts;
4670  struct binfile *binfile = debugfile->binfile;
4671  struct binfile_elf *bfelf = (struct binfile_elf *)binfile->priv;
4672  struct binfile_instance *bfi = binfile->instance;
4673  struct binfile_instance_elf *bfielf = NULL;
4674  void **userdata = NULL;
4675  Dwfl_Module *mod;
4676 
4677  if (bfi)
4678  bfielf = (struct binfile_instance_elf *)bfi->priv;
4679 
4680  Dwfl_Callbacks callbacks = {
4681  .find_debuginfo = find_no_debuginfo,
4682  };
4683 
4684  bfelf->dwfl = dwfl_begin(&callbacks);
4685  if (bfelf->dwfl == NULL) {
4686  verror("could not init libdwfl: %s\n",dwfl_errmsg(dwfl_errno()));
4687  return -1;
4688  }
4689 
4690  /*
4691  * For relocatable files that are in binfile->image, binfile->elf is
4692  * already open on them. But, we can't exactly dup or clone ->image
4693  * (at least I'm not sure it's safe), so when dwfl_close gets
4694  * called, elf_end will get called too. Catch that and set
4695  * bfelf->elf = NULL in those cases...
4696  */
4697 
4698  if (bfi && bfielf && binfile->image) {
4699  callbacks.section_address = bfi_find_section_address;
4700  callbacks.find_elf = bfi_find_elf;
4701 
4702  /*
4703  * Handle via relocation; must do this specially and manually.
4704  */
4705  dwfl_report_begin(bfelf->dwfl);
4706  mod = dwfl_report_module(bfelf->dwfl,bfi->filename,bfi->start,bfi->end);
4707  if (!mod) {
4708  verror("could not report relocatable module in binfile instance %s!\n",
4709  bfi->filename);
4710  dwfl_end(bfelf->dwfl);
4711  bfelf->dwfl = NULL;
4712  bfelf->dwfl_fd = -1;
4713  bfelf->elf = NULL;
4714  return -1;
4715  }
4716  dwfl_module_info(mod,&userdata,NULL,NULL,NULL,NULL,NULL,NULL);
4717  *userdata = binfile;
4718  }
4719  else if (binfile->fd > -1) {
4720  callbacks.section_address = dwfl_offline_section_address;
4721 
4722  if (bfelf->dwfl_fd < 0) {
4723  bfelf->dwfl_fd = dup(binfile->fd);
4724  if (bfelf->dwfl_fd == -1) {
4725  verror("dup(%d): %s\n",binfile->fd,strerror(errno));
4726  return -1;
4727  }
4728  }
4729 
4730  // XXX do we really need this? Can't have it without libdwflP.h
4731  //dwfl->offline_next_address = 0;
4732  if (!dwfl_report_offline(bfelf->dwfl,debugfile->filename,
4733  debugfile->filename,bfelf->dwfl_fd)) {
4734  verror("dwfl_report_offline: %s\n",dwfl_errmsg(dwfl_errno()));
4735  dwfl_end(bfelf->dwfl);
4736  bfelf->dwfl = NULL;
4737  bfelf->dwfl_fd = -1;
4738  return -1;
4739  }
4740  }
4741  else {
4742  verror("binfile %s had no fd nor memory image!\n",binfile->filename);
4743  dwfl_end(bfelf->dwfl);
4744  bfelf->dwfl = NULL;
4745  bfelf->dwfl_fd = -1;
4746  return -1;
4747  }
4748 
4749  dwfl_report_end(bfelf->dwfl,NULL,NULL);
4750 
4751  /*
4752  * This is where the guts of the work happen -- and that stuff all
4753  * happens in the callback.
4754  */
4755  if (dwfl_getmodules(bfelf->dwfl,&process_dwflmod,debugfile,0) < 0) {
4756  verror("getting dwarf modules: %s\n",dwfl_errmsg(dwfl_errno()));
4757  dwfl_end(bfelf->dwfl);
4758  bfelf->dwfl = NULL;
4759  if (bfi && bfielf && binfile->image)
4760  bfelf->elf = NULL;
4761  return -1;
4762  }
4763 
4764  /* Don't save any of this stuff if we did a full load. */
4765  if (!(dopts->flags & DEBUGFILE_LOAD_FLAG_PARTIALSYM
4767  || dopts->flags & DEBUGFILE_LOAD_FLAG_PUBNAMES)) {
4768  dwfl_end(bfelf->dwfl);
4769  bfelf->dwfl = NULL;
4770  if (bfi && bfielf && binfile->image)
4771  bfelf->elf = NULL;
4772  }
4773 
4774  return 0;
4775 }
4776 
4777 
4778 int dwarf_symbol_root_priv_free(struct debugfile *debugfile,struct symbol *root) {
4779  struct symbol_root_dwarf *srd;
4780 
4781  SYMBOL_RX_ROOT(root,sr);
4782  if (!sr)
4783  return -1;
4784 
4785  srd = (struct symbol_root_dwarf *)sr->priv;
4786  if (!srd)
4787  return -1;
4788 
4789  /*
4790  * Clean up whatever we can!
4791  */
4792  dwarf_refuselist_clean(root,1);
4793  dwarf_reftab_clean(root,1);
4794 
4795  if (srd->reftab)
4796  g_hash_table_destroy(srd->reftab);
4797  if (srd->refuselist)
4798  g_hash_table_destroy(srd->refuselist);
4799  if (srd->spec_reftab)
4800  g_hash_table_destroy(srd->spec_reftab);
4801 
4802  free(srd);
4803  sr->priv = NULL;
4804 
4805  return 0;
4806 }
4807 
4808 int dwarf_init(struct debugfile *debugfile) {
4809  struct dwarf_debugfile_info *ddi;
4810 
4811  if (debugfile->priv)
4812  return -1;
4813 
4814  ddi = calloc(1,sizeof(*ddi));
4815  debugfile->priv = ddi;
4816 
4817  return 0;
4818 }
4819 
4820 int dwarf_fini(struct debugfile *debugfile) {
4821  struct dwarf_debugfile_info *ddi;
4822 
4823  if (!debugfile->priv)
4824  return 0;
4825 
4826  ddi = (struct dwarf_debugfile_info *)debugfile->priv;
4827 
4828  dwarf_unload_cfa(debugfile);
4829 
4830  free(ddi);
4831  debugfile->priv = NULL;
4832 
4833  return 0;
4834 }
4835 
4836 extern int dwarf_cfa_read_saved_reg(struct debugfile *debugfile,
4837  struct location_ctxt *lctxt,
4838  REG reg,REGVAL *o_regval);
4839 extern int dwarf_cfa_read_retaddr(struct debugfile *debugfile,
4840  struct location_ctxt *lctxt,
4841  ADDR *o_retaddr);
4842 
4843 /*
4844  * Our debugfile ops.
4845  */
4847  .init = dwarf_init,
4848  .load = dwarf_load_debuginfo,
4849  .symbol_replace = dwarf_symbol_replace,
4850  .symbol_expand = dwarf_symbol_expand,
4851  .symbol_root_expand = dwarf_symbol_root_expand,
4852  .frame_read_saved_reg = dwarf_cfa_read_saved_reg,
4853  .frame_read_retaddr = dwarf_cfa_read_retaddr,
4854  .symbol_root_priv_free = dwarf_symbol_root_priv_free,
4855  .fini = dwarf_fini,
4856 };
4857 
4858 
4859 /*
4860 * Report the open ELF file as a module. Always consumes ELF and FD. *
4861 static Dwfl_Module *
4862 process_elf (Dwfl *dwfl, const char *name, const char *file_name, int fd,
4863  Elf *elf)
4864 {
4865  Dwfl_Module *mod = __libdwfl_report_elf (dwfl, name, file_name, fd, elf,
4866  dwfl->offline_next_address, false);
4867  if (mod != NULL)
4868  {
4869  * If this is an ET_EXEC file with fixed addresses, the address range
4870  it consumed may or may not intersect with the arbitrary range we
4871  will use for relocatable modules. Make sure we always use a free
4872  range for the offline allocations. If this module did use
4873  offline_next_address, it may have rounded it up for the module's
4874  alignment requirements. *
4875  if ((dwfl->offline_next_address >= mod->low_addr
4876  || mod->low_addr - dwfl->offline_next_address < OFFLINE_REDZONE)
4877  && dwfl->offline_next_address < mod->high_addr + OFFLINE_REDZONE)
4878  dwfl->offline_next_address = mod->high_addr + OFFLINE_REDZONE;
4879 
4880  * Don't keep the file descriptor around. *
4881  if (mod->main.fd != -1 && elf_cntl (mod->main.elf, ELF_C_FDREAD) == 0)
4882  {
4883  close (mod->main.fd);
4884  mod->main.fd = -1;
4885  }
4886  }
4887 
4888  return mod;
4889 }
4890 
4891 Dwfl_Module *
4892 libdwfl_report_offline_custom (Dwfl *dwfl, const char *name,
4893  const char *file_name, int fd, bool closefd)
4894 {
4895  Elf *elf;
4896  *
4897  Dwfl_Error error = __libdw_open_file (&fd, &elf, closefd, true);
4898  if (error != DWFL_E_NOERROR)
4899  {
4900  __libdwfl_seterrno (error);
4901  return NULL;
4902  }
4903  *
4904  Dwfl_Module *mod = process_elf (dwfl, name, file_name, fd, elf);
4905  if (mod == NULL)
4906  {
4907  elf_end (elf);
4908  if (closefd)
4909  close (fd);
4910  }
4911  return mod;
4912 }
4913 */
int debugfile_add_type(struct debugfile *debugfile, struct symbol *symbol)
Definition: debug.c:2300
int debugfile_insert_root(struct debugfile *debugfile, struct symbol *symbol)
Definition: debug.c:1891
const char * dwarf_access_string(unsigned int code)
const char * dwarf_attr_string(unsigned int attrnum)
unsigned int srcline
Definition: dwdebug_priv.h:824
int symbol_insert_symbol(struct symbol *parent, struct symbol *child)
Definition: debug.c:2758
#define RF_ACCEPT
Definition: rfilter.h:29
int dwarf_refuselist_release(struct symbol_root_dwarf *srd, struct symbol *symbol, SMOFFSET ref)
const char * dwarf_encoding_string(unsigned int code)
#define vwarnopt(level, area, flags, format,...)
Definition: log.h:37
int dwarf_refuselist_hold(struct symbol_root_dwarf *srd, struct symbol *symbol, SMOFFSET ref)
struct location * location_copy(struct location *location)
Definition: location.c:313
encoding_t
Definition: dwdebug.h:266
#define OFFSETMAX
Definition: common.h:75
SMOFFSET die_offset
Definition: dwdebug.h:985
int dwarf_specify_definition_attrs(struct symbol_root_dwarf *srd, struct symbol *specification, struct symbol *definition)
#define SYMBOLX_SUBRANGES(sym)
union location::@6 l
uint8_t reloading
int dwarf_init(struct debugfile *debugfile)
unsigned int has_addr
Definition: dwdebug_priv.h:839
struct scope * symbol_write_owned_scope(struct symbol *symbol)
Definition: debug.c:2689
ADDR end
Definition: dwdebug_priv.h:438
char * filename
Definition: binfile.h:243
#define unlikely(expr)
Definition: debugpred.h:101
unsigned int orig_name_offset
Definition: dwdebug_priv.h:821
int symbol_set_root_language(struct symbol *symbol, char *language, int copy, short int lang_code)
Definition: debug.c:3318
void symbol_set_bytesize(struct symbol *s, uint32_t b)
Definition: debug.c:3219
const char * dwarf_ordering_string(unsigned int code)
#define SYMBOL_IST_VOL(sym)
void symbol_set_bitoffset(struct symbol *s, uint32_t bo)
Definition: debug.c:3247
void debugfile_save_declaration(struct debugfile *debugfile, struct symbol *symbol)
Definition: debug.c:2081
char * linetab
Definition: dwdebug.h:859
#define SYMBOL_RX_ROOT(sym, rvar)
clrangesimple_t clrangesimple_create(void)
Definition: clfit.c:648
union symbol::@7 size
const char * dwarf_visibility_string(unsigned int code)
int symbol_set_constval(struct symbol *symbol, void *value, int len, int copy)
Definition: debug.c:3429
SMOFFSET cu_offset
Definition: dwdebug.h:984
int fd
Definition: binfile.h:214
#define symbol_set_enumval(s)
load_type_t
Definition: dwdebug.h:166
void * clmatchone_find(clmatchone_t *clf, Word_t index, Word_t *o_index)
Definition: clfit.c:977
Dwarf_Off first_top_level_die_offset
char * DATATYPE(int n)
Definition: debug.c:5684
#define SYMBOL_RX_FUNC(sym, rvar)
struct symbol * symbol_create(symbol_type_t symtype, symbol_source_t source, char *name, int name_copy, SMOFFSET offset, load_type_t loadtype, struct scope *scope)
Definition: debug.c:2550
Definition: probe.h:392
struct scope * symbol_read_owned_scope(struct symbol *symbol)
Definition: debug.c:2674
Dwarf_Off abbroffset
#define SYMBOL_RX_VAR(sym, rvar)
GHashTable * srcfiles
Definition: dwdebug.h:883
Dwarf_Word stmt_list_offset
GHashTable * shared_types
Definition: dwdebug.h:918
#define SYMBOL_WX_VAR(sym, wvar, reterr)
uint8_t lowpc_set
unsigned int linetablen
Definition: dwdebug.h:871
static uint64_t unsigned int i
load_type_t loadtag
Definition: dwdebug_priv.h:830
int dwarf_load_aranges(struct debugfile *debugfile, unsigned char *buf, unsigned int len, Dwarf *dbg)
int scope_insert_symbol(struct scope *scope, struct symbol *symbol)
Definition: scope.c:57
#define DAW_ADDR(msg)
#define SYMBOLX_MEMBERS(sym)
struct array_list __attribute__
unsigned int isexternal
Definition: dwdebug_priv.h:839
struct symbol * symbol_find_root(struct symbol *symbol)
Definition: debug.c:3119
struct location * dwarf_get_static_ops(struct symbol_root_dwarf *srd, const unsigned char *data, Dwarf_Word len, unsigned int attr)
Definition: dwarf_expr.c:987
char * image
Definition: binfile.h:213
uint8_t ref_set
#define v_g_slist_foreach(gslhead, gslcur, elm)
Definition: glib_wrapper.h:60
SMOFFSET ref
Definition: dwdebug_priv.h:868
int dwarf_fini(struct debugfile *debugfile)
struct debugfile_load_opts * opts
Definition: dwdebug.h:803
int dwarf_reftab_replace(struct symbol_root_dwarf *srd, struct symbol *existing, struct symbol *new, SMOFFSET ref, GHashTableIter *iter)
unsigned int size_is_bytes
Definition: dwdebug_priv.h:839
#define SYMBOL_IST_ENUM(sym)
void debugfile_handle_declaration(struct debugfile *debugfile, struct symbol *symbol)
Definition: debug.c:2100
ADDR addr
Definition: dwdebug_priv.h:916
clmatchone_t top_level_die_offsets
unsigned int isshared
Definition: dwdebug_priv.h:839
#define SYMBOL_IS_FULL(sym)
#define SYMBOL_IS_INSTANCE(sym)
int32_t SMOFFSET
Definition: common.h:100
ADDR symbol_get_addr(struct symbol *symbol)
Definition: debug.c:3096
#define SYMBOL_IS_INLINEABLE(sym)
uint8_t highpc_set
#define DFE(msg)
const char * dwarf_virtuality_string(unsigned int code)
uint32_t symbol_get_bytesize(struct symbol *symbol)
Definition: debug.c:3065
Dwfl * dwfl
Definition: binfile.h:352
ADDR start
Definition: dwdebug_priv.h:437
void symbol_set_srcline(struct symbol *s, int sl)
Definition: debug.c:3194
Dwarf_Off ref
int symbol_type_equal(struct symbol *t1, struct symbol *t2, GHashTable *eqcache, GHashTable *updated_datatype_refs)
Definition: debug.c:3801
int clmatchone_add(clmatchone_t *clf, Word_t index, void *data)
Definition: clfit.c:934
SMOFFSET datatype_ref
Definition: dwdebug_priv.h:882
int debugfile_update_root(struct debugfile *debugfile, struct symbol *symbol)
Definition: debug.c:1826
void debugfile_resolve_declarations(struct debugfile *debugfile)
Definition: debug.c:2250
#define verror(format,...)
Definition: log.h:30
debugfile_load_flags_t flags
Definition: dwdebug.h:761
#define verrorc(format,...)
Definition: log.h:32
char * rangetab
Definition: dwdebug.h:852
#define SYMBOL_IST_CONST(sym)
#define SYMBOLX_ROOT(sym)
struct debugfile_ops dwarf_debugfile_ops
#define LOGDUMPSYMBOL_NL(dl, lt, lf, s)
Definition: dwdebug_priv.h:30
#define SYMBOL_IST_UNION(sym)
int dwarf_refuselist_notify_reftab_changed(struct symbol_root_dwarf *srd, SMOFFSET ref, struct symbol *refsym)
#define DAW_STR(msg)
symbol_type_t type
Definition: dwdebug_priv.h:833
char * dbg_strtab
Definition: dwdebug.h:838
void finalize_die_symbol_name(struct symbol *symbol)
int symbol_set_root_compdir(struct symbol *symbol, char *compdirname, int copy)
Definition: debug.c:3294
GSList * scope_match_syms(struct scope *scope, struct rfilter *symbol_filter, symbol_type_flag_t flags)
Definition: scope.c:811
OFFSET offset
Definition: dwdebug_priv.h:603
#define vwarn(format,...)
Definition: log.h:33
int location_update_loclist(struct location *loc, ADDR start, ADDR end, struct location *rloc, int *action)
Definition: location.c:199
void free(void *ptr)
Definition: debugserver.c:207
int dwarf_cfa_read_saved_reg(struct debugfile *debugfile, struct location_ctxt *lctxt, REG reg, REGVAL *o_regval)
Definition: dwarf_cfa.c:1392
int dwarf_get_lines(struct symbol_root_dwarf *srd, Dwarf_Off offset)
int dwarf_symbol_expand(struct debugfile *debugfile, struct symbol *root, struct symbol *symbol)
#define SYMBOL_IS_LABEL(sym)
Definition: log.h:124
Definition: log.h:69
GHashTable * spec_reftab
Dwfl_Module * dwflmod
uint8_t highpc_is_offset
unsigned int dbg_strtablen
Definition: dwdebug.h:868
int symbol_set_encoding(struct symbol *symbol, encoding_t num)
Definition: debug.c:3332
int symbol_has_addr(struct symbol *symbol)
Definition: debug.c:3047
char * symbol_get_name_orig(struct symbol *symbol)
Definition: debug.c:2612
int dwarf_cfa_read_retaddr(struct debugfile *debugfile, struct location_ctxt *lctxt, ADDR *o_retaddr)
Definition: dwarf_cfa.c:1609
void * priv
Definition: binfile.h:265
struct symbol * root
size_t shstrndx
Definition: binfile.h:332
const char * dwarf_calling_convention_string(unsigned int code)
struct scope * symbol_containing_scope(struct symbol *symbol)
Definition: debug.c:2671
int symbol_set_location(struct symbol *symbol, struct location *loc)
Definition: debug.c:3365
unsigned int isparam
Definition: dwdebug_priv.h:839
const char * dwarf_discr_list_string(unsigned int code)
#define SYMBOL_RX_INLINE(sym, rvar)
void location_free(struct location *location)
Definition: location.c:359
unsigned int frametablen
Definition: dwdebug.h:872
int dwarf_symbol_ref_symbol_changed(struct symbol_root_dwarf *srd, struct symbol *symbol, SMOFFSET ref, struct symbol *refsym)
datatype_code_t datatype_code
Definition: dwdebug_priv.h:827
#define SYMBOL_IST_BASE(sym)
struct range * next
Definition: dwdebug_priv.h:439
int dwfl_fd
Definition: binfile.h:353
unsigned int isprototyped
Definition: dwdebug_priv.h:839
int(* init)(struct debugfile *debugfile)
Definition: dwdebug_priv.h:129
unsigned int isenumval
Definition: dwdebug_priv.h:839
uint32_t offset
Definition: dwdebug_priv.h:906
Dwarf_Off cu_offset
#define symbol_set_member(s)
#define SYMBOL_IST_ARRAY(sym)
#define DAW_BLOCK(msg)
void finalize_die_symbol(struct debugfile *debugfile, int level, struct symbol *symbol, struct symbol *parentsymbol, struct symbol *voidsymbol, GHashTable *reftab, struct array_list *die_offsets, SMOFFSET cu_offset)
void symbol_set_bitsize(struct symbol *s, uint32_t b)
Definition: debug.c:3232
#define PRIxOFFSET
Definition: common.h:71
int location_set_member_offset(struct location *l, OFFSET offset)
Definition: location.c:89
int dwarf_reftab_insert(struct symbol_root_dwarf *srd, struct symbol *symbol, SMOFFSET ref)
#define SYMBOL_IST_PTR(sym)
#define SYMBOL_IST_FUNC(sym)
#define WARNDUMPSYMBOL_NL(s)
Definition: dwdebug_priv.h:38
char * name
Definition: dwdebug_priv.h:788
int symbol_add_inline_instance(struct symbol *symbol, struct symbol *instance)
Definition: debug.c:3420
struct debugfile * debugfile
const char * dwarf_tag_string(unsigned int tag)
SMOFFSET ref
Definition: dwdebug_priv.h:460
#define read_8ubyte_unaligned_inc(obo, Addr)
#define ERRORDUMPSYMBOL_NL(s)
Definition: dwdebug_priv.h:54
#define PRIxSMOFFSET
Definition: common.h:102
unsigned int name_nofree
Definition: dwdebug_priv.h:839
unsigned int has_linkage_name
Definition: dwdebug_priv.h:839
int debugfile_add_global(struct debugfile *debugfile, struct symbol *symbol)
Definition: debug.c:2262
int len
Definition: dumptarget.c:52
#define SYMBOL_IST_STUNC(sym)
#define RHOLD(x, hx)
Definition: common.h:622
#define RF_REJECT
Definition: rfilter.h:30
#define DAW_REF(msg)
void symbol_set_addr(struct symbol *s, ADDR a)
Definition: debug.c:3276
int scope_remove_symbol(struct scope *scope, struct symbol *symbol)
Definition: scope.c:132
#define SYMBOL_IST_STUN(sym)
int symbol_set_root_producer(struct symbol *symbol, char *producer, int copy)
Definition: debug.c:3306
GHashTable * refuselist
unsigned int issynthetic
Definition: dwdebug_priv.h:839
int clrange_update_end(clrange_t *clf, Word_t start, Word_t end, void *data)
Definition: clfit.c:221
#define SYMBOL_WX_INLINE(sym, wvar, reterr)
int symbol_set_inline_origin(struct symbol *symbol, SMOFFSET ref, struct symbol *origin)
Definition: debug.c:3395
Definition: rfilter.h:44
Dwarf_Half version
int dwarf_symbol_root_priv_free(struct debugfile *debugfile, struct symbol *root)
int dwarf_symbol_set_origin_ref(struct symbol_root_dwarf *srd, struct symbol *symbol, SMOFFSET ref)
#define vdebug(devel, areas, flags, format,...)
Definition: log.h:302
int debugfile_remove_root(struct debugfile *debugfile, struct symbol *symbol)
Definition: debug.c:1933
uint8_t have_stmt_list_offset
void * realloc(void *ptr, size_t size)
Definition: debugserver.c:221
GElf_Ehdr ehdr
Definition: binfile.h:343
unsigned int isdeclaration
Definition: dwdebug_priv.h:839
struct symbol * parentsymbol
struct symbol * do_word_symbol(struct debugfile *debugfile, struct symbol *root)
int dwarf_load_cfa(struct debugfile *debugfile, char *buf, unsigned int len, Dwarf *dbg)
void debugfile_resolve_one_declaration(struct debugfile *debugfile, char *name)
Definition: debug.c:2241
#define SYMBOL_IS_TYPE(sym)
#define vdebugc(devel, areas, flags, format,...)
Definition: log.h:303
struct location * location_create(void)
Definition: location.c:39
const char * dwarf_inline_string(unsigned int code)
char * loctab
Definition: dwdebug.h:845
void * calloc(size_t nmemb, size_t size)
Definition: debugserver.c:200
GHashTable * srcfiles_multiuse
Definition: dwdebug.h:891
struct scope * symbol_link_owned_scope(struct symbol *symbol, struct scope *new_parent)
Definition: debug.c:2726
#define DAW(msg)
Definition: log.h:129
struct binfile * binfile
Definition: dwdebug.h:808
struct symbol * symbol
Definition: log.h:135
char * frametab
Definition: dwdebug.h:865
int dwarf_load_debuginfo(struct debugfile *debugfile)
uint32_t REGVAL
Definition: common.h:66
#define SYMBOL_CAN_OWN_SCOPE(sym)
#define SYMBOL_HAS_EXTRA(sym)
struct symbol * debugfile_lookup_root(struct debugfile *debugfile, SMOFFSET offset)
Definition: debug.c:1972
#define SYMBOL_WX_FUNC(sym, wvar, reterr)
#define read_4ubyte_unaligned_inc(obo, Addr)
struct range * ranges
void scope_update_range(struct scope *scope, ADDR start, ADDR end, int *action)
Definition: scope.c:166
struct symbol * symbol_get_sym(struct symbol *symbol, const char *name, symbol_type_flag_t flags)
Definition: debug.c:177
const char * dwarf_lang_string(unsigned int lang)
int dwarf_symbol_set_datatype_ref(struct symbol_root_dwarf *srd, struct symbol *symbol, SMOFFSET ref)
Definition: log.h:125
struct binfile * binfile_pointing
Definition: dwdebug.h:830
int8_t REG
Definition: common.h:93
#define symbol_set_parameter(s)
int dwarf_symbol_root_expand(struct debugfile *debugfile, struct symbol *root)
GSList * dwarf_refuselist_get(struct symbol_root_dwarf *srd, SMOFFSET ref)
void symbol_set_name(struct symbol *symbol, char *name, int copy)
Definition: debug.c:2615
struct scope * scope
Definition: dwdebug_priv.h:806
struct symbol_root_dwarf * srd
uint32_t ADDR
Definition: common.h:64
int dwarf_load_pubnames(struct debugfile *debugfile, unsigned char *buf, unsigned int len, Dwarf *dbg)
GElf_Shdr * shdrs
Definition: binfile.h:345
clrange_t ranges
Definition: dwdebug.h:977
struct rfilter * srcfile_filter
Definition: dwdebug.h:759
void * priv
Definition: dwdebug.h:800
int symbol_set_root_priv(struct symbol *symbol, void *priv)
Definition: debug.c:3288
struct rfilter * symbol_filter
Definition: dwdebug.h:760
int scope_insert_scope(struct scope *parent, struct scope *child)
Definition: scope.c:88
#define symbol_set_unspec_params(s)
int symbol_change_parent(struct symbol *parent, struct symbol *child, struct symbol *newparent)
Definition: debug.c:2884
#define read_2ubyte_unaligned_inc(obo, Addr)
char * filename
Definition: dwdebug.h:817
uint32_t REFCNT
Definition: common.h:124
#define PRIxADDR
Definition: common.h:67
#define SYMBOL_IS_ROOT(sym)
struct binfile_instance * instance
Definition: binfile.h:301
char * SYMBOL_TYPE(int n)
Definition: debug.c:5662
GHashTable * addresses
Definition: dwdebug.h:965
int symbol_set_inline_info(struct symbol *symbol, int isinlined, int isdeclinline)
Definition: debug.c:3389
GHashTable * pubnames
Definition: dwdebug.h:975
int dwarf_symbol_refuselist_release_all(struct symbol_root_dwarf *srd, struct symbol *symbol)
Dwarf_Off specification_ref
struct symbol * do_void_symbol(struct debugfile *debugfile, struct symbol *root)
const char * dwarf_form_string(unsigned int form)
char * symbol_get_name(struct symbol *symbol)
Definition: debug.c:2587
int dwarf_specify_definition_members(struct symbol_root_dwarf *srd, struct symbol *specification, struct symbol *definition)
#define RPUT(x, objtype, hx, rc)
Definition: common.h:624
#define SYMBOL_IST_CONTAINER(sym)
struct symbol_root * root
Definition: dwdebug_priv.h:973
void * malloc(size_t size)
Definition: debugserver.c:214
Elf * elf
Definition: binfile.h:350
void symbol_build_extname(struct symbol *symbol)
Definition: debug.c:2628
#define ERRORDUMPSYMBOL(s)
Definition: dwdebug_priv.h:50
int symbol_add_subrange(struct symbol *symbol, int subrange)
Definition: debug.c:3448
const char * dwarf_identifier_case_string(unsigned int code)
unsigned int isinlineinstance
Definition: dwdebug_priv.h:839
#define DAWL(msg)
Dwarf_Off sibling
unsigned int decldefined
Definition: dwdebug_priv.h:839
unsigned int rangetablen
Definition: dwdebug.h:870
int symbol_set_entry_pc(struct symbol *symbol, ADDR entry_pc)
Definition: debug.c:3340
#define DAW_NUM(msg)
int vdebug_is_on(int level, log_areas_t areas, log_flags_t flags)
Definition: log.c:335
#define SYMBOL_IST_TYPEDEF(sym)
unsigned int loctablen
Definition: dwdebug.h:869
#define SYMBOL_IS_FUNC(sym)
uint8_t specification_set
int clrange_add(clrange_t *clf, Word_t start, Word_t end, void *data)
Definition: clfit.c:104
unsigned int usesshareddatatype
Definition: dwdebug_priv.h:839
unsigned int size_is_bits
Definition: dwdebug_priv.h:839
#define SYMBOL_IS_PARTIAL(sym)
int dwarf_symbol_replace(struct debugfile *debugfile, struct symbol *old, struct symbol *new)
struct symbol * datatype
Definition: dwdebug_priv.h:925
unsigned int ismember
Definition: dwdebug_priv.h:839
struct symbol * voidsymbol
Dwarf_Off die_offset
Definition: log.h:127
struct bsymbol ** symbols
Definition: dumptarget.c:53
unsigned int num_sections
Definition: binfile.h:386
#define LOGDUMPSYMBOL(dl, lt, lf, s)
Definition: dwdebug_priv.h:25
#define SYMBOL_IS_VAR(sym)
char * filename
Definition: binfile.h:368
int dwarf_unload_cfa(struct debugfile *debugfile)
Definition: dwarf_cfa.c:1802
struct scope * scope_create(struct symbol *owner, SMOFFSET ref)
Definition: scope.c:37
int debugfile_replace_type(struct debugfile *debugfile, struct symbol *symbol)
Definition: debug.c:2334
int symbol_remove_symbol(struct symbol *parent, struct symbol *child)
Definition: debug.c:2845
struct symbol * symbol
Definition: dwdebug_priv.h:463
GHashTable * reftab