1 /* Copyright (C) 2021-2023 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "DbeSession.h"
27 #include "DataObject.h"
29 #include "LoadObject.h"
31 #include "DefaultMap.h"
34 datatypeCmp (const void *a
, const void *b
)
36 uint32_t o1
= ((datatype_t
*) a
)->datatype_id
;
37 uint32_t o2
= ((datatype_t
*) b
)->datatype_id
;
38 return o1
== o2
? 0 : (o1
< o2
? -1 : 1);
42 targetOffsetCmp (const void *a
, const void *b
)
44 uint32_t o1
= ((target_info_t
*) a
)->offset
;
45 uint32_t o2
= ((target_info_t
*) b
)->offset
;
46 return o1
== o2
? 0 : (o1
< o2
? -1 : 1);
50 //////////////////////////////////////////////////////////
56 Dwr_type (int64_t _cu_die_offset
, int _tag
)
58 cu_die_offset
= _cu_die_offset
;
74 char *name
, *dobj_name
;
75 int64_t cu_die_offset
, ref_type
, extent
, parent
, child
, next
;
76 int64_t size
, elems
, offset
;
79 DataObject
*get_dobj (Dwarf_cnt
*ctx
);
80 char *get_dobjname (Dwarf_cnt
*ctx
);
85 datatype_t
*get_datatype (Dwarf_cnt
*ctx
);
86 void get_dobj_for_members (Dwarf_cnt
*ctx
);
87 void set_dobjname (char *spec
, char *nm
);
91 //////////////////////////////////////////////////////////
93 Dwarf_cnt::Dwarf_cnt ()
107 Dwarf_cnt::get_dwr_type (int64_t cu_die_offset
)
109 Dwr_type
*t
= dwr_types
->get (cu_die_offset
);
112 Dprintf (DUMP_DWARFLIB
, "DWARF_ERROR: %s:%d wrong cu_die_offset=%lld in Dwarf_cnt::get_dwr_type\n",
113 get_basename (__FILE__
), (int) __LINE__
,
114 (long long) cu_die_offset
);
115 t
= put_dwr_type (cu_die_offset
, 0); // DOBJ_UNSPECIFIED
121 Dwarf_cnt::put_dwr_type (int64_t cu_die_offset
, int tag
)
123 Dwr_type
*t
= new Dwr_type (cu_die_offset
, tag
);
124 dwr_types
->put (cu_die_offset
, t
);
129 Dwarf_cnt::put_dwr_type (Dwr_Tag
*dwrTag
)
131 Dwr_type
*t
= new Dwr_type (dwrTag
->die
, dwrTag
->tag
);
132 dwr_types
->put (dwrTag
->die
, t
);
136 //////////////////////////////////////////////////////////
141 char *s
= dbe_sprintf ("%lld %-15s name='%s' parent=%lld next=%lld child=%lld dtype=%llx",
142 (long long) cu_die_offset
, DwrCU::tag2str (tag
),
143 STR (name
), (long long) parent
, (long long) next
,
144 (long long) child
, (long long) dtype
);
149 Dwr_type::set_dobjname (char *spec
, char *nm
)
154 dobj_name
= dbe_sprintf ("%s%s", spec
, nm
);
156 dobj_name
= dbe_sprintf ("%s<ANON=%lld>", spec
,
157 (long long) cu_die_offset
);
162 dobj_name
= dbe_sprintf ("%s", nm
);
164 dobj_name
= dbe_sprintf ("<ANON=%lld>", (long long) cu_die_offset
);
169 Dwr_type::get_dobjname (Dwarf_cnt
*ctx
)
175 case DW_TAG_base_type
:
176 set_dobjname (NULL
, name
);
177 for (int i
= 0, len
= (int) strlen (dobj_name
); i
< len
; i
++)
179 if (dobj_name
[i
] == ' ')
183 case DW_TAG_constant
:
184 case DW_TAG_formal_parameter
:
185 case DW_TAG_variable
:
187 Dwr_type
*t
= ctx
->get_dwr_type (ref_type
);
188 set_dobjname (NULL
, t
->get_dobjname (ctx
));
191 case DW_TAG_unspecified_type
:
192 set_dobjname (NTXT ("unspecified:"), name
);
194 case DW_TAG_enumeration_type
:
195 set_dobjname (NTXT ("enumeration:"), name
);
199 Dwr_type
*t
= ctx
->get_dwr_type (ref_type
);
200 dobj_name
= dbe_sprintf ("%s=%s", name
, t
->get_dobjname (ctx
));
203 case DW_TAG_const_type
:
204 set_dobjname (NTXT ("const+"), name
);
206 case DW_TAG_volatile_type
:
207 set_dobjname (NTXT ("volatile+"), name
);
209 case DW_TAG_pointer_type
:
211 Dwr_type
*t
= ctx
->get_dwr_type (ref_type
);
212 set_dobjname (NTXT ("pointer+"), t
->get_dobjname (ctx
));
215 case DW_TAG_reference_type
:
217 Dwr_type
*t
= ctx
->get_dwr_type (ref_type
);
218 set_dobjname (NTXT ("reference+"), t
->get_dobjname (ctx
));
221 case DW_TAG_array_type
:
223 Dwr_type
*t
= ctx
->get_dwr_type (ref_type
);
225 dobj_name
= dbe_sprintf ("array[%lld]:%s",
226 (long long) elems
, t
->get_dobjname (ctx
));
228 dobj_name
= dbe_sprintf ("array[]:%s", t
->get_dobjname (ctx
));
231 case DW_TAG_structure_type
:
232 set_dobjname (NTXT ("structure:"), name
);
234 case DW_TAG_union_type
:
235 set_dobjname (NTXT ("union:"), name
);
237 case DW_TAG_class_type
:
238 set_dobjname (NTXT ("class:"), name
);
242 Dwr_type
*t
= ctx
->get_dwr_type (ref_type
);
244 dobj_name
= dbe_sprintf (NTXT ("%s:%lld"), t
->get_dobjname (ctx
),
245 (long long) bit_size
);
247 dobj_name
= dbe_sprintf (NTXT ("%s"), t
->get_dobjname (ctx
));
251 Dprintf (DUMP_DWARFLIB
, NTXT ("DWARF_ERROR: %s:%d No case for %s cu_die_offset=%lld\n"),
252 get_basename (__FILE__
), (int) __LINE__
,
253 DwrCU::tag2str (tag
), (long long) cu_die_offset
);
254 set_dobjname (NTXT ("Undefined:"), NULL
);
261 Dwr_type::get_datatype (Dwarf_cnt
*ctx
)
265 dtype
= new datatype_t
;
266 dtype
->datatype_id
= (unsigned) cu_die_offset
;
267 dtype
->memop_refs
= 0;
268 dtype
->event_data
= 0;
270 ctx
->module
->datatypes
->incorporate (dtype
, datatypeCmp
);
275 Dwr_type::get_dobj (Dwarf_cnt
*ctx
)
278 dtype
= get_datatype (ctx
);
280 DataObject
*dobj
= dtype
->dobj
;
285 dobj
= dbeSession
->find_dobj_by_name (PTXT (DOBJ_UNSPECIFIED
));
288 dobj
= dbeSession
->createDataObject ();
290 dobj
->offset
= offset
;
291 dobj
->scope
= ctx
->func
? (Histable
*) ctx
->func
: (Histable
*) ctx
->module
;
296 Dwr_type
*t
= ctx
->get_dwr_type (parent
);
297 dobj
->parent
= t
->get_dobj (ctx
);
302 Dwr_type
*t
= ctx
->get_dwr_type (ref_type
);
315 case DW_TAG_array_type
:
316 case DW_TAG_base_type
:
317 case DW_TAG_unspecified_type
:
318 case DW_TAG_enumeration_type
:
320 case DW_TAG_const_type
:
321 case DW_TAG_volatile_type
:
322 case DW_TAG_pointer_type
:
323 case DW_TAG_reference_type
:
324 dobj
->set_dobjname (get_dobjname (ctx
), NULL
);
326 case DW_TAG_structure_type
:
327 case DW_TAG_union_type
:
328 case DW_TAG_class_type
:
329 dobj
->set_dobjname (get_dobjname (ctx
), NULL
);
330 dobj
->master
= dbeSession
->find_dobj_by_name (dobj_name
);
331 get_dobj_for_members (ctx
);
333 case DW_TAG_constant
:
334 case DW_TAG_formal_parameter
:
336 case DW_TAG_variable
:
337 if (dobj
->parent
== NULL
)
338 dobj
->parent
= dbeSession
->get_Scalars_DataObject ();
339 dobj
->set_dobjname (get_dobjname (ctx
), name
);
342 Dprintf (DUMP_DWARFLIB
, NTXT ("DWARF_ERROR: %s:%d No case for %s cu_die_offset=%lld\n"),
343 get_basename (__FILE__
), (int) __LINE__
,
344 DwrCU::tag2str (tag
), (long long) cu_die_offset
);
351 Dwr_type::get_dobj_for_members (Dwarf_cnt
*ctx
)
353 for (int64_t i
= child
; i
!= 0;)
355 Dwr_type
*t
= ctx
->get_dwr_type (i
);
361 //////////////////////////////////////////////////////////
363 Dwarf::Dwarf (Stabs
*_stabs
)
366 status
= Stabs::DBGD_ERR_NONE
;
368 debug_infoSec
= NULL
;
369 debug_abbrevSec
= NULL
;
371 debug_lineSec
= NULL
;
372 debug_rangesSec
= NULL
;
373 elf
= stabs
->openElf (true);
376 status
= Stabs::DBGD_ERR_BAD_ELF_FORMAT
;
379 debug_infoSec
= dwrGetSec (NTXT (".debug_info"));
382 debug_infoSec
->reloc
= ElfReloc::get_elf_reloc (elf
, NTXT (".rela.debug_info"), NULL
);
383 debug_infoSec
->reloc
= ElfReloc::get_elf_reloc (elf
, NTXT (".rel.debug_info"), debug_infoSec
->reloc
);
384 if (debug_infoSec
->reloc
)
385 debug_infoSec
->reloc
->dump ();
387 debug_abbrevSec
= dwrGetSec (NTXT (".debug_abbrev"));
388 debug_strSec
= dwrGetSec (NTXT (".debug_str"));
389 debug_lineSec
= dwrGetSec (NTXT (".debug_line"));
390 debug_rangesSec
= dwrGetSec (NTXT (".debug_ranges"));
392 if ((debug_infoSec
== NULL
) || (debug_abbrevSec
== NULL
) || (debug_lineSec
== NULL
))
394 status
= Stabs::DBGD_ERR_NO_DWARF
;
401 delete debug_infoSec
;
402 delete debug_abbrevSec
;
404 delete debug_lineSec
;
405 delete debug_rangesSec
;
410 Dwarf::dwrGetSec (const char *sec_name
)
412 int secN
= elf
->elf_get_sec_num (sec_name
);
415 Elf_Data
*elfData
= elf
->elf_getdata (secN
);
417 return new DwrSec ((unsigned char *) elfData
->d_buf
, elfData
->d_size
,
418 elf
->need_swap_endian
,
419 elf
->elf_getclass () == ELFCLASS32
);
427 uint64_t pc
= Dwarf_addr (DW_AT_low_pc
);
434 DwrCU::get_linkage_name ()
436 char *nm
= Dwarf_string (DW_AT_linkage_name
);
439 nm
= Dwarf_string (DW_AT_SUN_link_name
);
442 return Dwarf_string (DW_AT_MIPS_linkage_name
);
446 DwrCU::parseChild (Dwarf_cnt
*ctx
)
448 if (!dwrTag
.hasChild
)
450 uint64_t old_size
= debug_infoSec
->size
;
451 uint64_t next_die_offset
= 0;
453 if (read_ref_attr (DW_AT_sibling
, &next_die
) == DW_DLV_OK
)
455 next_die_offset
= next_die
+ cu_offset
;
456 if (next_die_offset
<= debug_infoSec
->offset
)
458 Dprintf (DEBUG_ERR_MSG
, NTXT ("DwrCU::parseChild: next_die(0x%llx) <= debug_infoSec->offset(%llx)\n"),
459 (long long) next_die
, (long long) debug_infoSec
->offset
);
462 else if (debug_infoSec
->size
> next_die_offset
)
463 debug_infoSec
->size
= next_die_offset
;
469 if (set_die (0) != DW_DLV_OK
)
473 int hasChild
= dwrTag
.hasChild
;
476 case DW_TAG_imported_declaration
:
477 if (Stabs::is_fortran (ctx
->module
->lang_code
))
479 char *link_name
= Dwarf_string (DW_AT_name
);
480 ctx
->fortranMAIN
= NULL
;
483 if (ctx
->fortranMAIN
)
485 ctx
->fortranMAIN
->set_match_name (link_name
);
486 ctx
->fortranMAIN
= NULL
;
490 case DW_TAG_subprogram
:
491 if (dwrTag
.get_attr (DW_AT_abstract_origin
))
493 if (dwrTag
.get_attr (DW_AT_declaration
))
496 if (Stabs::is_fortran (ctx
->module
->lang_code
))
498 char *link_name
= Dwarf_string (DW_AT_name
);
499 if (link_name
&& streq (link_name
, NTXT ("MAIN")))
500 ctx
->fortranMAIN
= Stabs::find_func (NTXT ("MAIN"), ctx
->module
->functions
, true, true);
502 if (get_linkage_name () == NULL
)
505 func
= append_Function (ctx
);
508 if (Stabs::is_fortran (ctx
->module
->lang_code
) &&
509 streq (func
->get_match_name (), NTXT ("MAIN")))
510 ctx
->fortranMAIN
= func
;
511 old_name
= ctx
->name
;
512 Function
*old_func
= ctx
->func
;
513 ctx
->name
= func
->get_match_name ();
517 ctx
->name
= old_name
;
518 ctx
->func
= old_func
;
522 old_name
= ctx
->name
;
523 ctx
->name
= Dwarf_string (DW_AT_SUN_link_name
);
526 ctx
->name
= old_name
;
528 case DW_TAG_class_type
:
529 old_name
= ctx
->name
;
530 ctx
->name
= Dwarf_string (DW_AT_name
);
533 ctx
->name
= old_name
;
535 case DW_TAG_structure_type
:
536 old_name
= ctx
->name
;
540 ctx
->name
= old_name
;
542 case DW_TAG_namespace
:
543 old_name
= ctx
->name
;
544 ctx
->name
= Dwarf_string (DW_AT_name
);
547 ctx
->name
= old_name
;
549 case DW_TAG_lexical_block
:
550 old_name
= ctx
->name
;
554 ctx
->name
= old_name
;
556 case DW_TAG_SUN_memop_info
:
559 case DW_TAG_inlined_subroutine
:
562 parse_inlined_subroutine (ctx
);
566 default: // No more special cases
574 if (next_die_offset
!= 0)
575 debug_infoSec
->offset
= next_die_offset
;
576 debug_infoSec
->size
= old_size
;
580 Dwarf::archive_Dwarf (LoadObject
*lo
)
582 if (debug_infoSec
== NULL
)
586 dwrCUs
= new Vector
<DwrCU
*>;
588 debug_infoSec
->offset
= 0;
589 while (debug_infoSec
->offset
< debug_infoSec
->sizeSec
)
591 DwrCU
*dwrCU
= new DwrCU (this);
592 dwrCUs
->append (dwrCU
);
593 debug_infoSec
->size
= debug_infoSec
->sizeSec
;
594 debug_infoSec
->offset
= dwrCU
->next_cu_offset
;
596 if (dwrCU
->set_die (dwrCU
->cu_header_offset
) != DW_DLV_OK
)
598 Dprintf (1, "DwrCU::archive_Dwarf: CU=%lld (offset=0x%llx); set_die(0x%llx) failed\n",
599 (long long) dwrCUs
->size (), (long long) dwrCU
->cu_offset
,
600 (long long) dwrCU
->cu_header_offset
);
604 Module
*mod
= dwrCU
->parse_cu_header (lo
);
607 mod
->hdrOffset
= dwrCUs
->size ();
608 DwrLineRegs
*lineReg
= dwrCU
->get_dwrLineReg ();
611 dwrCU
->srcFiles
= new Vector
<SourceFile
*> (VecSize (lineReg
->file_names
));
612 for (long i
= 0, sz
= VecSize (lineReg
->file_names
); i
< sz
; i
++)
614 char *fname
= lineReg
->getPath (i
+ 1);
615 SourceFile
*sf
= mod
->findSource (fname
, true);
616 dwrCU
->srcFiles
->append (sf
);
622 dwrCU
->parseChild (&ctx
);
623 if (dwrCU
->dwrInlinedSubrs
&& DUMP_DWARFLIB
)
626 char *lo_name
= mod
->loadobject
? mod
->loadobject
->get_name ()
628 snprintf (msg
, sizeof (msg
), NTXT ("\ndwrCUs[%lld]: %s:%s\n"),
629 (long long) dwrCUs
->size (),
630 STR (lo_name
), STR (mod
->get_name ()));
631 dwrCU
->dwrInlinedSubrs
->dump (msg
);
639 Dwarf::srcline_Dwarf (Module
*module
)
641 if (module
== NULL
|| module
->hdrOffset
== 0)
643 DwrCU
*dwrCU
= dwrCUs
->get (module
->hdrOffset
- 1);
644 dwrCU
->map_dwarf_lines (module
);
648 // parse hwcprof info for given module in loadobject
651 Dwarf::read_hwcprof_info (Module
*module
)
653 if (module
->datatypes
|| (module
->hdrOffset
== 0))
655 DwrCU
*dwrCU
= dwrCUs
->get (module
->hdrOffset
- 1);
658 module
->datatypes
= new Vector
<datatype_t
*>;
659 if (dwrCU
->set_die (dwrCU
->cu_header_offset
) != DW_DLV_OK
)
661 Dprintf (1, "Dwarf::read_hwcprof_info: CU=%lld (offset=0x%llx); set_die(0x%llx) failed\n",
662 (long long) module
->hdrOffset
, (long long) dwrCU
->cu_offset
,
663 (long long) dwrCU
->cu_header_offset
);
668 ctx
.cu_offset
= dwrCU
->cu_offset
; // CU header offset;
669 ctx
.dwr_types
= new DefaultMap
<int64_t, Dwr_type
*>;
670 ctx
.put_dwr_type (0, 0); // for DOBJ_UNSPECIFIED
671 dwrCU
->read_hwcprof_info (&ctx
);
673 Vector
<inst_info_t
*> *infoList
= module
->infoList
;
674 Dprintf (DUMP_DWARFLIB
,
675 "\n\n ### Dwarf::read_hwcprof_info: module: '%s' infoList->size()=%lld\n",
676 STR (module
->get_name ()),
677 (long long) (infoList
? infoList
->size () : -1));
678 for (int i
= 0, sz
= infoList
? infoList
->size () : -1; i
< sz
; i
++)
680 inst_info_t
*ip
= infoList
->fetch (i
);
681 memop_info_t
*mp
= ip
->memop
;
682 Dwr_type
*t
= ctx
.get_dwr_type (mp
->datatype_id
);
687 Dprintf (DUMP_DWARFLIB
,
688 "\n\n ### Dwarf::read_hwcprof_info: '%s' infoList->size()=%lld\n",
689 STR (module
->get_name ()),
690 (long long) (infoList
? infoList
->size () : 1));
691 for (int i
= 0, sz
= infoList
? infoList
->size () : -1; i
< sz
; i
++)
693 inst_info_t
*ip
= infoList
->fetch (i
);
694 memop_info_t
*mp
= ip
->memop
;
695 Dprintf (DUMP_DWARFLIB
,
696 " %d id=%lld offset=%lld signature=%lld datatype_id=%lld \n",
697 i
, (long long) mp
->id
, (long long) mp
->offset
,
698 (long long) mp
->signature
, (long long) mp
->datatype_id
);
701 Vector
<int64_t> *keys
= ctx
.dwr_types
->keySet ();
702 Dprintf (DUMP_DWARFLIB
,
703 "\n\n ### Dwarf::read_hwcprof_info: '%s' keys->size()=%lld\n",
704 STR (module
->get_name ()), (long long) (keys
? keys
->size () : -1));
705 for (int i
= 0, sz
= keys
->size (); i
< sz
; i
++)
707 int64_t ind
= keys
->fetch (i
);
708 Dwr_type
*t
= ctx
.get_dwr_type (ind
);
709 Dprintf (DUMP_DWARFLIB
, NTXT (" %d %lld %s\n"), i
,
710 (long long) ind
, t
->dump ());
716 DwrCU::read_hwcprof_info (Dwarf_cnt
*ctx
)
718 if (!dwrTag
.hasChild
)
720 uint64_t old_size
= debug_infoSec
->size
;
721 uint64_t next_die_offset
= 0;
723 if (read_ref_attr (DW_AT_sibling
, &next_die
) == DW_DLV_OK
)
725 next_die_offset
= next_die
+ cu_offset
;
726 if (next_die_offset
<= debug_infoSec
->offset
)
728 else if (debug_infoSec
->size
> next_die_offset
)
729 debug_infoSec
->size
= next_die_offset
;
735 if (set_die (0) != DW_DLV_OK
)
737 Dprintf (DUMP_DWARFLIB
, NTXT ("%s:%d <%lld:%lld> cu_die=%lld %-15s\n"),
738 get_basename (__FILE__
), (int) __LINE__
, (long long) ctx
->level
,
739 (long long) dwrTag
.die
, (long long) dwrTag
.offset
,
740 DwrCU::tag2str (dwrTag
.tag
));
743 case DW_TAG_SUN_memop_info
:
745 if (ctx
->func
== NULL
)
747 Dwarf_Unsigned mid
= Dwarf_data (DW_AT_SUN_profile_id
);
748 Dwarf_Unsigned off
= Dwarf_data (DW_AT_SUN_func_offset
);
749 Dwarf_Unsigned sig
= Dwarf_data (DW_AT_SUN_memop_signature
);
750 Dwarf_Off ref
= Dwarf_ref (DW_AT_SUN_memop_type_ref
);
752 // define memop entry
753 memop_info_t
*memop
= new memop_info_t
;
754 memop
->id
= (unsigned) mid
;
755 memop
->signature
= (unsigned) sig
;
756 memop
->datatype_id
= ref
? (unsigned) ref
: 0;
757 memop
->offset
= (unsigned) (ctx
->func
->img_offset
+ off
);
759 // define instop entry
760 inst_info_t
*instop
= new inst_info_t
;
761 instop
->type
= CPF_INSTR_TYPE_PREFETCH
; // XXXX UNKNOWN
762 instop
->offset
= memop
->offset
;
763 instop
->memop
= memop
;
764 if (ctx
->module
->infoList
== NULL
)
765 ctx
->module
->infoList
= new Vector
<inst_info_t
*>;
766 ctx
->module
->infoList
->append (instop
);
769 case DW_TAG_SUN_codeflags
:
771 if (ctx
->func
== NULL
)
773 Dwarf_Unsigned kind
= Dwarf_data (DW_AT_SUN_cf_kind
);
774 if (kind
== DW_ATCF_SUN_branch_target
)
776 DwrSec
*secp
= Dwarf_block (DW_AT_SUN_func_offsets
);
780 for (int i
= 0; secp
->offset
< secp
->size
; i
++)
782 int val
= (int) secp
->GetSLEB128 ();
783 if (i
== 0 || val
!= 0)
786 target_info_t
*t
= new target_info_t
;
787 t
->offset
= (unsigned) (ctx
->func
->img_offset
+ foffset
);
788 ctx
->module
->bTargets
.incorporate (t
, targetOffsetCmp
);
796 case DW_TAG_subprogram
:
798 Function
*old_func
= ctx
->func
;
799 if (dwrTag
.get_attr (DW_AT_abstract_origin
)
800 || dwrTag
.get_attr (DW_AT_declaration
))
803 ctx
->func
= append_Function (ctx
);
804 read_hwcprof_info (ctx
);
805 ctx
->func
= old_func
;
808 case DW_TAG_base_type
:
810 Dwr_type
*t
= ctx
->put_dwr_type (dwrTag
.die
, dwrTag
.tag
);
811 t
->name
= Dwarf_string (DW_AT_name
);
812 t
->size
= Dwarf_data (DW_AT_byte_size
);
815 case DW_TAG_unspecified_type
:
816 ctx
->put_dwr_type (dwrTag
.die
, dwrTag
.tag
);
818 case DW_TAG_enumeration_type
:
820 Dwr_type
*t
= ctx
->put_dwr_type (dwrTag
.die
, dwrTag
.tag
);
821 t
->name
= Dwarf_string (DW_AT_name
);
822 t
->size
= Dwarf_data (DW_AT_byte_size
);
825 case DW_TAG_constant
:
826 case DW_TAG_formal_parameter
:
827 case DW_TAG_variable
:
829 case DW_TAG_const_type
:
830 case DW_TAG_volatile_type
:
832 Dwr_type
*t
= ctx
->put_dwr_type (dwrTag
.die
, dwrTag
.tag
);
833 t
->name
= Dwarf_string (DW_AT_name
);
834 t
->ref_type
= Dwarf_ref (DW_AT_type
);
837 case DW_TAG_pointer_type
:
838 case DW_TAG_reference_type
:
840 Dwr_type
*t
= ctx
->put_dwr_type (dwrTag
.die
, dwrTag
.tag
);
841 t
->name
= Dwarf_string (DW_AT_name
);
842 t
->ref_type
= Dwarf_ref (DW_AT_type
);
843 t
->size
= (dwarf
->stabs
->get_class () == W64
) ? 8 : 4;
846 case DW_TAG_array_type
:
848 Dwr_type
*t
= ctx
->put_dwr_type (dwrTag
.die
, dwrTag
.tag
);
849 t
->name
= Dwarf_string (DW_AT_name
);
850 t
->ref_type
= Dwarf_ref (DW_AT_type
);
851 t
->size
= Dwarf_data (DW_AT_byte_size
);
853 read_hwcprof_info (ctx
);
854 t
->elems
= ctx
->size
;
857 case DW_TAG_subrange_type
:
859 int64_t ref_type
= Dwarf_ref (DW_AT_type
);
860 int64_t hi
= Dwarf_data (DW_AT_upper_bound
);
861 int64_t lo
= Dwarf_data (DW_AT_lower_bound
);
862 int64_t ss
= Dwarf_data (DW_AT_stride_size
);
863 ctx
->size
= 1 + hi
- lo
;
866 Dprintf (DUMP_DWARFLIB
,
867 "Got subrange [%lld:%lld:%lld] indexed <%lld>: size=%lld\n",
868 (long long) lo
, (long long) hi
, (long long) ss
,
869 (long long) ref_type
, (long long) ctx
->size
);
872 case DW_TAG_structure_type
:
873 case DW_TAG_union_type
:
874 case DW_TAG_class_type
:
876 Dwr_type
*t
= ctx
->put_dwr_type (dwrTag
.die
, dwrTag
.tag
);
877 t
->name
= Dwarf_string (DW_AT_name
);
878 t
->size
= Dwarf_data (DW_AT_byte_size
);
879 t
->extent
= Dwarf_ref (DW_AT_sibling
);
880 int64_t old_parent
= ctx
->parent
;
881 ctx
->parent
= t
->cu_die_offset
;
883 char *old_name
= ctx
->name
;
884 ctx
->name
= (dwrTag
.tag
== DW_TAG_class_type
) ? Dwarf_string (DW_AT_name
) : NULL
;
885 read_hwcprof_info (ctx
);
886 ctx
->name
= old_name
;
887 ctx
->parent
= old_parent
;
889 for (int64_t i
= t
->child
, last
= 0; i
!= 0;)
891 Dwr_type
*t1
= ctx
->get_dwr_type (i
);
901 if (ctx
->parent
== 0)
903 Dprintf (DUMP_DWARFLIB
, NTXT ("DWARF_ERROR: %s:%d %s cu_die_offset=%lld\n"),
904 get_basename (__FILE__
), (int) __LINE__
,
905 DwrCU::tag2str (dwrTag
.tag
), (long long) dwrTag
.die
);
908 Dwr_type
*t
= ctx
->put_dwr_type (dwrTag
.die
, dwrTag
.tag
);
909 t
->name
= Dwarf_string (DW_AT_name
);
910 t
->ref_type
= Dwarf_ref (DW_AT_type
);
911 t
->offset
= Dwarf_location (DW_AT_data_member_location
);
912 Dwr_type
*parent
= ctx
->get_dwr_type (ctx
->parent
);
913 t
->parent
= ctx
->parent
;
914 t
->next
= parent
->child
; // a reverse order of members
915 parent
->child
= t
->cu_die_offset
;
916 t
->bit_size
= (uint32_t) Dwarf_data (DW_AT_bit_size
);
921 char *old_name
= ctx
->name
;
922 ctx
->name
= Dwarf_string (DW_AT_SUN_link_name
);
923 read_hwcprof_info (ctx
);
924 ctx
->name
= old_name
;
927 case DW_TAG_namespace
:
929 char *old_name
= ctx
->name
;
930 ctx
->name
= Dwarf_string (DW_AT_name
);
931 read_hwcprof_info (ctx
);
932 ctx
->name
= old_name
;
935 case DW_TAG_lexical_block
:
937 char *old_name
= ctx
->name
;
939 read_hwcprof_info (ctx
);
940 ctx
->name
= old_name
;
943 default: // No more special cases
944 read_hwcprof_info (ctx
);
950 if (next_die_offset
!= 0)
951 debug_infoSec
->offset
= next_die_offset
;
952 debug_infoSec
->size
= old_size
;
955 // Append function to module
957 DwrCU::append_Function (Dwarf_cnt
*ctx
)
959 char *outerName
= ctx
->name
, *name
, tmpname
[2048];
961 char *fname
= Dwarf_string (DW_AT_name
);
962 if (fname
&& outerName
&& !strchr (fname
, '.'))
964 size_t outerlen
= strlen (outerName
);
965 if (outerlen
> 0 && outerName
[outerlen
- 1] == '_')
968 snprintf (tmpname
, sizeof (tmpname
), NTXT ("%s"), outerName
);
969 snprintf (tmpname
+ outerlen
, sizeof (tmpname
) - outerlen
, NTXT (".%s_"), fname
);
972 snprintf (tmpname
, sizeof (tmpname
), NTXT ("%s.%s"), outerName
, fname
);
974 Dprintf (DUMP_DWARFLIB
, NTXT ("Generated innerfunc name %s\n"), name
);
979 char *link_name
= get_linkage_name ();
980 if (link_name
== NULL
)
983 uint64_t pc
= get_low_pc ();
984 func
= dwarf
->stabs
->append_Function (module
, link_name
, pc
);
987 int lineno
= (int) Dwarf_data (DW_AT_decl_line
);
988 func
->set_match_name (name
);
991 func
->setLineFirst (lineno
);
992 int fileno
= ((int) Dwarf_data (DW_AT_decl_file
)) - 1;
993 SourceFile
*sf
= ((fileno
>= 0) && (fileno
< VecSize (srcFiles
))) ? srcFiles
->get (fileno
)
994 : module
->getMainSrc ();
995 func
->setDefSrc (sf
);
996 func
->pushSrcFile (func
->def_source
, 0);
1003 // Get language code
1005 DwrCU::Dwarf_lang ()
1007 char *str
= Dwarf_string (DW_AT_producer
);
1008 if (str
&& strncmp (str
, NTXT ("GNU"), 3) == 0)
1010 int64_t lang
= Dwarf_data (DW_AT_language
);
1015 return Sp_lang_c
; // Sp_lang_ansic?
1018 case DW_LANG_C_plus_plus
:
1019 return isGNU
? Sp_lang_gcc
: Sp_lang_cplusplus
;
1020 case DW_LANG_Fortran90
:
1021 return Sp_lang_fortran90
;
1022 case DW_LANG_Fortran77
:
1023 return Sp_lang_fortran
;
1025 return Sp_lang_java
;
1026 case DW_LANG_Mips_Assembler
:
1027 case DW_LANG_SUN_Assembler
:
1029 case DW_LANG_Pascal83
:
1030 return Sp_lang_pascal
;
1033 case DW_LANG_Cobol74
:
1034 case DW_LANG_Cobol85
:
1035 case DW_LANG_Modula2
:
1037 case DW_LANG_Fortran95
:
1038 case DW_LANG_lo_user
:
1039 return Sp_lang_unknown
;