1 //===--- DWARFVisitor.cpp ---------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 //===----------------------------------------------------------------------===//
11 #include "DWARFVisitor.h"
12 #include "llvm/ObjectYAML/DWARFYAML.h"
17 void DWARFYAML::VisitorImpl
<T
>::onVariableSizeValue(uint64_t U
, unsigned Size
) {
32 llvm_unreachable("Invalid integer write size.");
36 static unsigned getOffsetSize(const DWARFYAML::Unit
&Unit
) {
37 return Unit
.Length
.isDWARF64() ? 8 : 4;
40 static unsigned getRefSize(const DWARFYAML::Unit
&Unit
) {
41 if (Unit
.Version
== 2)
43 return getOffsetSize(Unit
);
46 template <typename T
> void DWARFYAML::VisitorImpl
<T
>::traverseDebugInfo() {
47 for (auto &Unit
: DebugInfo
.CompileUnits
) {
48 onStartCompileUnit(Unit
);
49 auto FirstAbbrevCode
= Unit
.Entries
[0].AbbrCode
;
51 for (auto &Entry
: Unit
.Entries
) {
52 onStartDIE(Unit
, Entry
);
53 if (Entry
.AbbrCode
== 0u)
55 auto &Abbrev
= DebugInfo
.AbbrevDecls
[Entry
.AbbrCode
- FirstAbbrevCode
];
56 auto FormVal
= Entry
.Values
.begin();
57 auto AbbrForm
= Abbrev
.Attributes
.begin();
59 FormVal
!= Entry
.Values
.end() && AbbrForm
!= Abbrev
.Attributes
.end();
60 ++FormVal
, ++AbbrForm
) {
61 onForm(*AbbrForm
, *FormVal
);
62 dwarf::Form Form
= AbbrForm
->Form
;
67 case dwarf::DW_FORM_addr
:
68 onVariableSizeValue(FormVal
->Value
, Unit
.AddrSize
);
70 case dwarf::DW_FORM_ref_addr
:
71 onVariableSizeValue(FormVal
->Value
, getRefSize(Unit
));
73 case dwarf::DW_FORM_exprloc
:
74 case dwarf::DW_FORM_block
:
75 onValue((uint64_t)FormVal
->BlockData
.size(), true);
77 MemoryBufferRef(StringRef((const char *)&FormVal
->BlockData
[0],
78 FormVal
->BlockData
.size()),
81 case dwarf::DW_FORM_block1
: {
82 auto writeSize
= FormVal
->BlockData
.size();
83 onValue((uint8_t)writeSize
);
85 MemoryBufferRef(StringRef((const char *)&FormVal
->BlockData
[0],
86 FormVal
->BlockData
.size()),
90 case dwarf::DW_FORM_block2
: {
91 auto writeSize
= FormVal
->BlockData
.size();
92 onValue((uint16_t)writeSize
);
94 MemoryBufferRef(StringRef((const char *)&FormVal
->BlockData
[0],
95 FormVal
->BlockData
.size()),
99 case dwarf::DW_FORM_block4
: {
100 auto writeSize
= FormVal
->BlockData
.size();
101 onValue((uint32_t)writeSize
);
103 MemoryBufferRef(StringRef((const char *)&FormVal
->BlockData
[0],
104 FormVal
->BlockData
.size()),
108 case dwarf::DW_FORM_data1
:
109 case dwarf::DW_FORM_ref1
:
110 case dwarf::DW_FORM_flag
:
111 case dwarf::DW_FORM_strx1
:
112 case dwarf::DW_FORM_addrx1
:
113 onValue((uint8_t)FormVal
->Value
);
115 case dwarf::DW_FORM_data2
:
116 case dwarf::DW_FORM_ref2
:
117 case dwarf::DW_FORM_strx2
:
118 case dwarf::DW_FORM_addrx2
:
119 onValue((uint16_t)FormVal
->Value
);
121 case dwarf::DW_FORM_data4
:
122 case dwarf::DW_FORM_ref4
:
123 case dwarf::DW_FORM_ref_sup4
:
124 case dwarf::DW_FORM_strx4
:
125 case dwarf::DW_FORM_addrx4
:
126 onValue((uint32_t)FormVal
->Value
);
128 case dwarf::DW_FORM_data8
:
129 case dwarf::DW_FORM_ref8
:
130 case dwarf::DW_FORM_ref_sup8
:
131 onValue((uint64_t)FormVal
->Value
);
133 case dwarf::DW_FORM_sdata
:
134 onValue((int64_t)FormVal
->Value
, true);
136 case dwarf::DW_FORM_udata
:
137 case dwarf::DW_FORM_ref_udata
:
138 onValue((uint64_t)FormVal
->Value
, true);
140 case dwarf::DW_FORM_string
:
141 onValue(FormVal
->CStr
);
143 case dwarf::DW_FORM_indirect
:
144 onValue((uint64_t)FormVal
->Value
, true);
146 Form
= static_cast<dwarf::Form
>((uint64_t)FormVal
->Value
);
149 case dwarf::DW_FORM_strp
:
150 case dwarf::DW_FORM_sec_offset
:
151 case dwarf::DW_FORM_GNU_ref_alt
:
152 case dwarf::DW_FORM_GNU_strp_alt
:
153 case dwarf::DW_FORM_line_strp
:
154 case dwarf::DW_FORM_strp_sup
:
155 onVariableSizeValue(FormVal
->Value
, getOffsetSize(Unit
));
157 case dwarf::DW_FORM_ref_sig8
:
158 onValue((uint64_t)FormVal
->Value
);
160 case dwarf::DW_FORM_GNU_addr_index
:
161 case dwarf::DW_FORM_GNU_str_index
:
162 onValue((uint64_t)FormVal
->Value
, true);
169 onEndDIE(Unit
, Entry
);
171 onEndCompileUnit(Unit
);
175 // Explicitly instantiate the two template expansions.
176 template class DWARFYAML::VisitorImpl
<DWARFYAML::Data
>;
177 template class DWARFYAML::VisitorImpl
<const DWARFYAML::Data
>;