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/BinaryFormat/Dwarf.h"
13 #include "llvm/ObjectYAML/DWARFYAML.h"
14 #include "llvm/Support/Errc.h"
15 #include "llvm/Support/Error.h"
20 void DWARFYAML::VisitorImpl<T>::onVariableSizeValue(uint64_t U, unsigned Size) {
35 llvm_unreachable("Invalid integer write size.");
39 static unsigned getOffsetSize(const DWARFYAML::Unit &Unit) {
40 return Unit.Format == dwarf::DWARF64 ? 8 : 4;
43 static unsigned getRefSize(const DWARFYAML::Unit &Unit) {
44 if (Unit.Version == 2)
46 return getOffsetSize(Unit);
49 template <typename T> Error DWARFYAML::VisitorImpl<T>::traverseDebugInfo() {
50 for (auto &Unit : DebugInfo.CompileUnits) {
51 onStartCompileUnit(Unit);
52 if (Unit.Entries.empty())
55 for (auto &Entry : Unit.Entries) {
56 onStartDIE(Unit, Entry);
57 uint32_t AbbrCode = Entry.AbbrCode;
58 if (AbbrCode == 0 || Entry.Values.empty())
61 if (AbbrCode > DebugInfo.AbbrevDecls.size())
62 return createStringError(
63 errc::invalid_argument,
64 "abbrev code must be less than or equal to the number of "
65 "entries in abbreviation table");
66 const DWARFYAML::Abbrev &Abbrev = DebugInfo.AbbrevDecls[AbbrCode - 1];
67 auto FormVal = Entry.Values.begin();
68 auto AbbrForm = Abbrev.Attributes.begin();
70 FormVal != Entry.Values.end() && AbbrForm != Abbrev.Attributes.end();
71 ++FormVal, ++AbbrForm) {
72 onForm(*AbbrForm, *FormVal);
73 dwarf::Form Form = AbbrForm->Form;
78 case dwarf::DW_FORM_addr:
79 onVariableSizeValue(FormVal->Value, Unit.AddrSize);
81 case dwarf::DW_FORM_ref_addr:
82 onVariableSizeValue(FormVal->Value, getRefSize(Unit));
84 case dwarf::DW_FORM_exprloc:
85 case dwarf::DW_FORM_block:
86 onValue((uint64_t)FormVal->BlockData.size(), true);
88 MemoryBufferRef(StringRef((const char *)&FormVal->BlockData[0],
89 FormVal->BlockData.size()),
92 case dwarf::DW_FORM_block1: {
93 auto writeSize = FormVal->BlockData.size();
94 onValue((uint8_t)writeSize);
96 MemoryBufferRef(StringRef((const char *)&FormVal->BlockData[0],
97 FormVal->BlockData.size()),
101 case dwarf::DW_FORM_block2: {
102 auto writeSize = FormVal->BlockData.size();
103 onValue((uint16_t)writeSize);
105 MemoryBufferRef(StringRef((const char *)&FormVal->BlockData[0],
106 FormVal->BlockData.size()),
110 case dwarf::DW_FORM_block4: {
111 auto writeSize = FormVal->BlockData.size();
112 onValue((uint32_t)writeSize);
114 MemoryBufferRef(StringRef((const char *)&FormVal->BlockData[0],
115 FormVal->BlockData.size()),
119 case dwarf::DW_FORM_strx:
120 case dwarf::DW_FORM_addrx:
121 case dwarf::DW_FORM_rnglistx:
122 case dwarf::DW_FORM_loclistx:
123 onValue((uint64_t)FormVal->Value, /*LEB=*/true);
125 case dwarf::DW_FORM_data1:
126 case dwarf::DW_FORM_ref1:
127 case dwarf::DW_FORM_flag:
128 case dwarf::DW_FORM_strx1:
129 case dwarf::DW_FORM_addrx1:
130 onValue((uint8_t)FormVal->Value);
132 case dwarf::DW_FORM_data2:
133 case dwarf::DW_FORM_ref2:
134 case dwarf::DW_FORM_strx2:
135 case dwarf::DW_FORM_addrx2:
136 onValue((uint16_t)FormVal->Value);
138 case dwarf::DW_FORM_data4:
139 case dwarf::DW_FORM_ref4:
140 case dwarf::DW_FORM_ref_sup4:
141 case dwarf::DW_FORM_strx4:
142 case dwarf::DW_FORM_addrx4:
143 onValue((uint32_t)FormVal->Value);
145 case dwarf::DW_FORM_data8:
146 case dwarf::DW_FORM_ref8:
147 case dwarf::DW_FORM_ref_sup8:
148 onValue((uint64_t)FormVal->Value);
150 case dwarf::DW_FORM_sdata:
151 onValue((int64_t)FormVal->Value, true);
153 case dwarf::DW_FORM_udata:
154 case dwarf::DW_FORM_ref_udata:
155 onValue((uint64_t)FormVal->Value, true);
157 case dwarf::DW_FORM_string:
158 onValue(FormVal->CStr);
160 case dwarf::DW_FORM_indirect:
161 onValue((uint64_t)FormVal->Value, true);
163 Form = static_cast<dwarf::Form>((uint64_t)FormVal->Value);
166 case dwarf::DW_FORM_strp:
167 case dwarf::DW_FORM_sec_offset:
168 case dwarf::DW_FORM_GNU_ref_alt:
169 case dwarf::DW_FORM_GNU_strp_alt:
170 case dwarf::DW_FORM_line_strp:
171 case dwarf::DW_FORM_strp_sup:
172 onVariableSizeValue(FormVal->Value, getOffsetSize(Unit));
174 case dwarf::DW_FORM_ref_sig8:
175 onValue((uint64_t)FormVal->Value);
177 case dwarf::DW_FORM_GNU_addr_index:
178 case dwarf::DW_FORM_GNU_str_index:
179 onValue((uint64_t)FormVal->Value, true);
186 onEndDIE(Unit, Entry);
188 onEndCompileUnit(Unit);
191 return Error::success();
194 // Explicitly instantiate the two template expansions.
195 template class DWARFYAML::VisitorImpl<DWARFYAML::Data>;
196 template class DWARFYAML::VisitorImpl<const DWARFYAML::Data>;