1 | //===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // Data structures for DWARF info entries. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "llvm/CodeGen/DIE.h" |
14 | #include "DwarfCompileUnit.h" |
15 | #include "DwarfDebug.h" |
16 | #include "llvm/CodeGen/AsmPrinter.h" |
17 | #include "llvm/Config/llvm-config.h" |
18 | #include "llvm/MC/MCAsmInfo.h" |
19 | #include "llvm/MC/MCStreamer.h" |
20 | #include "llvm/MC/MCSymbol.h" |
21 | #include "llvm/Support/Debug.h" |
22 | #include "llvm/Support/ErrorHandling.h" |
23 | #include "llvm/Support/Format.h" |
24 | #include "llvm/Support/LEB128.h" |
25 | #include "llvm/Support/raw_ostream.h" |
26 | using namespace llvm; |
27 | |
28 | #define DEBUG_TYPE "dwarfdebug" |
29 | |
30 | //===----------------------------------------------------------------------===// |
31 | // DIEAbbrevData Implementation |
32 | //===----------------------------------------------------------------------===// |
33 | |
34 | /// Profile - Used to gather unique data for the abbreviation folding set. |
35 | /// |
36 | void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const { |
37 | // Explicitly cast to an integer type for which FoldingSetNodeID has |
38 | // overloads. Otherwise MSVC 2010 thinks this call is ambiguous. |
39 | ID.AddInteger(I: unsigned(Attribute)); |
40 | ID.AddInteger(I: unsigned(Form)); |
41 | if (Form == dwarf::DW_FORM_implicit_const) |
42 | ID.AddInteger(I: Value); |
43 | } |
44 | |
45 | //===----------------------------------------------------------------------===// |
46 | // DIEAbbrev Implementation |
47 | //===----------------------------------------------------------------------===// |
48 | |
49 | /// Profile - Used to gather unique data for the abbreviation folding set. |
50 | /// |
51 | void DIEAbbrev::Profile(FoldingSetNodeID &ID) const { |
52 | ID.AddInteger(I: unsigned(Tag)); |
53 | ID.AddInteger(I: unsigned(Children)); |
54 | |
55 | // For each attribute description. |
56 | for (unsigned i = 0, N = Data.size(); i < N; ++i) |
57 | Data[i].Profile(ID); |
58 | } |
59 | |
60 | /// Emit - Print the abbreviation using the specified asm printer. |
61 | /// |
62 | void DIEAbbrev::Emit(const AsmPrinter *AP) const { |
63 | // Emit its Dwarf tag type. |
64 | AP->emitULEB128(Value: Tag, Desc: dwarf::TagString(Tag).data()); |
65 | |
66 | // Emit whether it has children DIEs. |
67 | AP->emitULEB128(Value: (unsigned)Children, Desc: dwarf::ChildrenString(Children).data()); |
68 | |
69 | // For each attribute description. |
70 | for (unsigned i = 0, N = Data.size(); i < N; ++i) { |
71 | const DIEAbbrevData &AttrData = Data[i]; |
72 | |
73 | // Emit attribute type. |
74 | AP->emitULEB128(Value: AttrData.getAttribute(), |
75 | Desc: dwarf::AttributeString(Attribute: AttrData.getAttribute()).data()); |
76 | |
77 | // Emit form type. |
78 | #ifndef NDEBUG |
79 | // Could be an assertion, but this way we can see the failing form code |
80 | // easily, which helps track down where it came from. |
81 | if (!dwarf::isValidFormForVersion(F: AttrData.getForm(), |
82 | Version: AP->getDwarfVersion())) { |
83 | LLVM_DEBUG(dbgs() << "Invalid form " << format("0x%x" , AttrData.getForm()) |
84 | << " for DWARF version " << AP->getDwarfVersion() |
85 | << "\n" ); |
86 | llvm_unreachable("Invalid form for specified DWARF version" ); |
87 | } |
88 | #endif |
89 | AP->emitULEB128(Value: AttrData.getForm(), |
90 | Desc: dwarf::FormEncodingString(Encoding: AttrData.getForm()).data()); |
91 | |
92 | // Emit value for DW_FORM_implicit_const. |
93 | if (AttrData.getForm() == dwarf::DW_FORM_implicit_const) |
94 | AP->emitSLEB128(Value: AttrData.getValue()); |
95 | } |
96 | |
97 | // Mark end of abbreviation. |
98 | AP->emitULEB128(Value: 0, Desc: "EOM(1)" ); |
99 | AP->emitULEB128(Value: 0, Desc: "EOM(2)" ); |
100 | } |
101 | |
102 | LLVM_DUMP_METHOD |
103 | void DIEAbbrev::print(raw_ostream &O) const { |
104 | O << "Abbreviation @" |
105 | << format(Fmt: "0x%lx" , Vals: (long)(intptr_t)this) |
106 | << " " |
107 | << dwarf::TagString(Tag) |
108 | << " " |
109 | << dwarf::ChildrenString(Children) |
110 | << '\n'; |
111 | |
112 | for (unsigned i = 0, N = Data.size(); i < N; ++i) { |
113 | O << " " |
114 | << dwarf::AttributeString(Attribute: Data[i].getAttribute()) |
115 | << " " |
116 | << dwarf::FormEncodingString(Encoding: Data[i].getForm()); |
117 | |
118 | if (Data[i].getForm() == dwarf::DW_FORM_implicit_const) |
119 | O << " " << Data[i].getValue(); |
120 | |
121 | O << '\n'; |
122 | } |
123 | } |
124 | |
125 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
126 | LLVM_DUMP_METHOD void DIEAbbrev::dump() const { |
127 | print(O&: dbgs()); |
128 | } |
129 | #endif |
130 | |
131 | //===----------------------------------------------------------------------===// |
132 | // DIEAbbrevSet Implementation |
133 | //===----------------------------------------------------------------------===// |
134 | |
135 | DIEAbbrevSet::~DIEAbbrevSet() { |
136 | for (DIEAbbrev *Abbrev : Abbreviations) |
137 | Abbrev->~DIEAbbrev(); |
138 | } |
139 | |
140 | DIEAbbrev &DIEAbbrevSet::uniqueAbbreviation(DIE &Die) { |
141 | |
142 | FoldingSetNodeID ID; |
143 | DIEAbbrev Abbrev = Die.generateAbbrev(); |
144 | Abbrev.Profile(ID); |
145 | |
146 | void *InsertPos; |
147 | if (DIEAbbrev *Existing = |
148 | AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) { |
149 | Die.setAbbrevNumber(Existing->getNumber()); |
150 | return *Existing; |
151 | } |
152 | |
153 | // Move the abbreviation to the heap and assign a number. |
154 | DIEAbbrev *New = new (Alloc) DIEAbbrev(std::move(Abbrev)); |
155 | Abbreviations.push_back(x: New); |
156 | New->setNumber(Abbreviations.size()); |
157 | Die.setAbbrevNumber(Abbreviations.size()); |
158 | |
159 | // Store it for lookup. |
160 | AbbreviationsSet.InsertNode(N: New, InsertPos); |
161 | return *New; |
162 | } |
163 | |
164 | void DIEAbbrevSet::Emit(const AsmPrinter *AP, MCSection *Section) const { |
165 | if (!Abbreviations.empty()) { |
166 | // Start the debug abbrev section. |
167 | AP->OutStreamer->switchSection(Section); |
168 | AP->emitDwarfAbbrevs(Abbrevs: Abbreviations); |
169 | } |
170 | } |
171 | |
172 | //===----------------------------------------------------------------------===// |
173 | // DIE Implementation |
174 | //===----------------------------------------------------------------------===// |
175 | |
176 | DIE *DIE::getParent() const { return dyn_cast_if_present<DIE *>(Val: Owner); } |
177 | |
178 | DIEAbbrev DIE::generateAbbrev() const { |
179 | DIEAbbrev Abbrev(Tag, hasChildren()); |
180 | for (const DIEValue &V : values()) |
181 | if (V.getForm() == dwarf::DW_FORM_implicit_const) |
182 | Abbrev.AddImplicitConstAttribute(Attribute: V.getAttribute(), |
183 | Value: V.getDIEInteger().getValue()); |
184 | else |
185 | Abbrev.AddAttribute(Attribute: V.getAttribute(), Form: V.getForm()); |
186 | return Abbrev; |
187 | } |
188 | |
189 | uint64_t DIE::getDebugSectionOffset() const { |
190 | const DIEUnit *Unit = getUnit(); |
191 | assert(Unit && "DIE must be owned by a DIEUnit to get its absolute offset" ); |
192 | return Unit->getDebugSectionOffset() + getOffset(); |
193 | } |
194 | |
195 | const DIE *DIE::getUnitDie() const { |
196 | const DIE *p = this; |
197 | while (p) { |
198 | if (p->getTag() == dwarf::DW_TAG_compile_unit || |
199 | p->getTag() == dwarf::DW_TAG_skeleton_unit || |
200 | p->getTag() == dwarf::DW_TAG_type_unit) |
201 | return p; |
202 | p = p->getParent(); |
203 | } |
204 | return nullptr; |
205 | } |
206 | |
207 | DIEUnit *DIE::getUnit() const { |
208 | const DIE *UnitDie = getUnitDie(); |
209 | if (UnitDie) |
210 | return dyn_cast_if_present<DIEUnit *>(Val: UnitDie->Owner); |
211 | return nullptr; |
212 | } |
213 | |
214 | DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const { |
215 | // Iterate through all the attributes until we find the one we're |
216 | // looking for, if we can't find it return NULL. |
217 | for (const auto &V : values()) |
218 | if (V.getAttribute() == Attribute) |
219 | return V; |
220 | return DIEValue(); |
221 | } |
222 | |
223 | LLVM_DUMP_METHOD |
224 | static void printValues(raw_ostream &O, const DIEValueList &Values, |
225 | StringRef Type, unsigned Size, unsigned IndentCount) { |
226 | O << Type << ": Size: " << Size << "\n" ; |
227 | |
228 | unsigned I = 0; |
229 | const std::string Indent(IndentCount, ' '); |
230 | for (const auto &V : Values.values()) { |
231 | O << Indent; |
232 | O << "Blk[" << I++ << "]" ; |
233 | O << " " << dwarf::FormEncodingString(Encoding: V.getForm()) << " " ; |
234 | V.print(O); |
235 | O << "\n" ; |
236 | } |
237 | } |
238 | |
239 | LLVM_DUMP_METHOD |
240 | void DIE::print(raw_ostream &O, unsigned IndentCount) const { |
241 | const std::string Indent(IndentCount, ' '); |
242 | O << Indent << "Die: " << format(Fmt: "0x%lx" , Vals: (long)(intptr_t) this) |
243 | << ", Offset: " << Offset << ", Size: " << Size << "\n" ; |
244 | |
245 | O << Indent << dwarf::TagString(Tag: getTag()) << " " |
246 | << dwarf::ChildrenString(Children: hasChildren()) << "\n" ; |
247 | |
248 | IndentCount += 2; |
249 | for (const auto &V : values()) { |
250 | O << Indent; |
251 | O << dwarf::AttributeString(Attribute: V.getAttribute()); |
252 | O << " " << dwarf::FormEncodingString(Encoding: V.getForm()) << " " ; |
253 | V.print(O); |
254 | O << "\n" ; |
255 | } |
256 | IndentCount -= 2; |
257 | |
258 | for (const auto &Child : children()) |
259 | Child.print(O, IndentCount: IndentCount + 4); |
260 | |
261 | O << "\n" ; |
262 | } |
263 | |
264 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
265 | LLVM_DUMP_METHOD void DIE::dump() const { |
266 | print(O&: dbgs()); |
267 | } |
268 | #endif |
269 | |
270 | unsigned DIE::computeOffsetsAndAbbrevs(const dwarf::FormParams &FormParams, |
271 | DIEAbbrevSet &AbbrevSet, |
272 | unsigned CUOffset) { |
273 | // Unique the abbreviation and fill in the abbreviation number so this DIE |
274 | // can be emitted. |
275 | const DIEAbbrev &Abbrev = AbbrevSet.uniqueAbbreviation(Die&: *this); |
276 | |
277 | // Set compile/type unit relative offset of this DIE. |
278 | setOffset(CUOffset); |
279 | |
280 | // Add the byte size of the abbreviation code. |
281 | CUOffset += getULEB128Size(Value: getAbbrevNumber()); |
282 | |
283 | // Add the byte size of all the DIE attribute values. |
284 | for (const auto &V : values()) |
285 | CUOffset += V.sizeOf(FormParams); |
286 | |
287 | // Let the children compute their offsets and abbreviation numbers. |
288 | if (hasChildren()) { |
289 | (void)Abbrev; |
290 | assert(Abbrev.hasChildren() && "Children flag not set" ); |
291 | |
292 | for (auto &Child : children()) |
293 | CUOffset = |
294 | Child.computeOffsetsAndAbbrevs(FormParams, AbbrevSet, CUOffset); |
295 | |
296 | // Each child chain is terminated with a zero byte, adjust the offset. |
297 | CUOffset += sizeof(int8_t); |
298 | } |
299 | |
300 | // Compute the byte size of this DIE and all of its children correctly. This |
301 | // is needed so that top level DIE can help the compile unit set its length |
302 | // correctly. |
303 | setSize(CUOffset - getOffset()); |
304 | return CUOffset; |
305 | } |
306 | |
307 | //===----------------------------------------------------------------------===// |
308 | // DIEUnit Implementation |
309 | //===----------------------------------------------------------------------===// |
310 | DIEUnit::DIEUnit(dwarf::Tag UnitTag) : Die(UnitTag) { |
311 | Die.Owner = this; |
312 | assert((UnitTag == dwarf::DW_TAG_compile_unit || |
313 | UnitTag == dwarf::DW_TAG_skeleton_unit || |
314 | UnitTag == dwarf::DW_TAG_type_unit || |
315 | UnitTag == dwarf::DW_TAG_partial_unit) && |
316 | "expected a unit TAG" ); |
317 | } |
318 | |
319 | void DIEValue::emitValue(const AsmPrinter *AP) const { |
320 | switch (Ty) { |
321 | case isNone: |
322 | llvm_unreachable("Expected valid DIEValue" ); |
323 | #define HANDLE_DIEVALUE(T) \ |
324 | case is##T: \ |
325 | getDIE##T().emitValue(AP, Form); \ |
326 | break; |
327 | #include "llvm/CodeGen/DIEValue.def" |
328 | } |
329 | } |
330 | |
331 | unsigned DIEValue::sizeOf(const dwarf::FormParams &FormParams) const { |
332 | switch (Ty) { |
333 | case isNone: |
334 | llvm_unreachable("Expected valid DIEValue" ); |
335 | #define HANDLE_DIEVALUE(T) \ |
336 | case is##T: \ |
337 | return getDIE##T().sizeOf(FormParams, Form); |
338 | #include "llvm/CodeGen/DIEValue.def" |
339 | } |
340 | llvm_unreachable("Unknown DIE kind" ); |
341 | } |
342 | |
343 | LLVM_DUMP_METHOD |
344 | void DIEValue::print(raw_ostream &O) const { |
345 | switch (Ty) { |
346 | case isNone: |
347 | llvm_unreachable("Expected valid DIEValue" ); |
348 | #define HANDLE_DIEVALUE(T) \ |
349 | case is##T: \ |
350 | getDIE##T().print(O); \ |
351 | break; |
352 | #include "llvm/CodeGen/DIEValue.def" |
353 | } |
354 | } |
355 | |
356 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
357 | LLVM_DUMP_METHOD void DIEValue::dump() const { |
358 | print(O&: dbgs()); |
359 | } |
360 | #endif |
361 | |
362 | //===----------------------------------------------------------------------===// |
363 | // DIEInteger Implementation |
364 | //===----------------------------------------------------------------------===// |
365 | |
366 | /// EmitValue - Emit integer of appropriate size. |
367 | /// |
368 | void DIEInteger::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const { |
369 | switch (Form) { |
370 | case dwarf::DW_FORM_implicit_const: |
371 | case dwarf::DW_FORM_flag_present: |
372 | // Emit something to keep the lines and comments in sync. |
373 | // FIXME: Is there a better way to do this? |
374 | Asm->OutStreamer->addBlankLine(); |
375 | return; |
376 | case dwarf::DW_FORM_flag: |
377 | case dwarf::DW_FORM_ref1: |
378 | case dwarf::DW_FORM_data1: |
379 | case dwarf::DW_FORM_strx1: |
380 | case dwarf::DW_FORM_addrx1: |
381 | case dwarf::DW_FORM_ref2: |
382 | case dwarf::DW_FORM_data2: |
383 | case dwarf::DW_FORM_strx2: |
384 | case dwarf::DW_FORM_addrx2: |
385 | case dwarf::DW_FORM_strx3: |
386 | case dwarf::DW_FORM_addrx3: |
387 | case dwarf::DW_FORM_strp: |
388 | case dwarf::DW_FORM_ref4: |
389 | case dwarf::DW_FORM_data4: |
390 | case dwarf::DW_FORM_ref_sup4: |
391 | case dwarf::DW_FORM_strx4: |
392 | case dwarf::DW_FORM_addrx4: |
393 | case dwarf::DW_FORM_ref8: |
394 | case dwarf::DW_FORM_ref_sig8: |
395 | case dwarf::DW_FORM_data8: |
396 | case dwarf::DW_FORM_ref_sup8: |
397 | case dwarf::DW_FORM_GNU_ref_alt: |
398 | case dwarf::DW_FORM_GNU_strp_alt: |
399 | case dwarf::DW_FORM_line_strp: |
400 | case dwarf::DW_FORM_sec_offset: |
401 | case dwarf::DW_FORM_strp_sup: |
402 | case dwarf::DW_FORM_addr: |
403 | case dwarf::DW_FORM_ref_addr: |
404 | Asm->OutStreamer->emitIntValue(Value: Integer, |
405 | Size: sizeOf(FormParams: Asm->getDwarfFormParams(), Form)); |
406 | return; |
407 | case dwarf::DW_FORM_GNU_str_index: |
408 | case dwarf::DW_FORM_GNU_addr_index: |
409 | case dwarf::DW_FORM_ref_udata: |
410 | case dwarf::DW_FORM_strx: |
411 | case dwarf::DW_FORM_addrx: |
412 | case dwarf::DW_FORM_rnglistx: |
413 | case dwarf::DW_FORM_udata: |
414 | Asm->emitULEB128(Value: Integer); |
415 | return; |
416 | case dwarf::DW_FORM_sdata: |
417 | Asm->emitSLEB128(Value: Integer); |
418 | return; |
419 | default: llvm_unreachable("DIE Value form not supported yet" ); |
420 | } |
421 | } |
422 | |
423 | /// sizeOf - Determine size of integer value in bytes. |
424 | /// |
425 | unsigned DIEInteger::sizeOf(const dwarf::FormParams &FormParams, |
426 | dwarf::Form Form) const { |
427 | if (std::optional<uint8_t> FixedSize = |
428 | dwarf::getFixedFormByteSize(Form, Params: FormParams)) |
429 | return *FixedSize; |
430 | |
431 | switch (Form) { |
432 | case dwarf::DW_FORM_GNU_str_index: |
433 | case dwarf::DW_FORM_GNU_addr_index: |
434 | case dwarf::DW_FORM_ref_udata: |
435 | case dwarf::DW_FORM_strx: |
436 | case dwarf::DW_FORM_addrx: |
437 | case dwarf::DW_FORM_rnglistx: |
438 | case dwarf::DW_FORM_udata: |
439 | return getULEB128Size(Value: Integer); |
440 | case dwarf::DW_FORM_sdata: |
441 | return getSLEB128Size(Value: Integer); |
442 | default: llvm_unreachable("DIE Value form not supported yet" ); |
443 | } |
444 | } |
445 | |
446 | LLVM_DUMP_METHOD |
447 | void DIEInteger::print(raw_ostream &O) const { |
448 | O << "Int: " << (int64_t)Integer << " 0x" ; |
449 | O.write_hex(N: Integer); |
450 | } |
451 | |
452 | //===----------------------------------------------------------------------===// |
453 | // DIEExpr Implementation |
454 | //===----------------------------------------------------------------------===// |
455 | |
456 | /// EmitValue - Emit expression value. |
457 | /// |
458 | void DIEExpr::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
459 | AP->emitDebugValue(Value: Expr, Size: sizeOf(FormParams: AP->getDwarfFormParams(), Form)); |
460 | } |
461 | |
462 | /// SizeOf - Determine size of expression value in bytes. |
463 | /// |
464 | unsigned DIEExpr::sizeOf(const dwarf::FormParams &FormParams, |
465 | dwarf::Form Form) const { |
466 | switch (Form) { |
467 | case dwarf::DW_FORM_data4: |
468 | return 4; |
469 | case dwarf::DW_FORM_data8: |
470 | return 8; |
471 | case dwarf::DW_FORM_sec_offset: |
472 | return FormParams.getDwarfOffsetByteSize(); |
473 | default: |
474 | llvm_unreachable("DIE Value form not supported yet" ); |
475 | } |
476 | } |
477 | |
478 | LLVM_DUMP_METHOD |
479 | void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; } |
480 | |
481 | //===----------------------------------------------------------------------===// |
482 | // DIELabel Implementation |
483 | //===----------------------------------------------------------------------===// |
484 | |
485 | /// EmitValue - Emit label value. |
486 | /// |
487 | void DIELabel::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
488 | bool IsSectionRelative = Form != dwarf::DW_FORM_addr; |
489 | AP->emitLabelReference(Label, Size: sizeOf(FormParams: AP->getDwarfFormParams(), Form), |
490 | IsSectionRelative); |
491 | } |
492 | |
493 | /// sizeOf - Determine size of label value in bytes. |
494 | /// |
495 | unsigned DIELabel::sizeOf(const dwarf::FormParams &FormParams, |
496 | dwarf::Form Form) const { |
497 | switch (Form) { |
498 | case dwarf::DW_FORM_data4: |
499 | return 4; |
500 | case dwarf::DW_FORM_data8: |
501 | return 8; |
502 | case dwarf::DW_FORM_sec_offset: |
503 | case dwarf::DW_FORM_strp: |
504 | return FormParams.getDwarfOffsetByteSize(); |
505 | case dwarf::DW_FORM_addr: |
506 | return FormParams.AddrSize; |
507 | default: |
508 | llvm_unreachable("DIE Value form not supported yet" ); |
509 | } |
510 | } |
511 | |
512 | LLVM_DUMP_METHOD |
513 | void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); } |
514 | |
515 | //===----------------------------------------------------------------------===// |
516 | // DIEBaseTypeRef Implementation |
517 | //===----------------------------------------------------------------------===// |
518 | |
519 | void DIEBaseTypeRef::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
520 | uint64_t Offset = CU->ExprRefedBaseTypes[Index].Die->getOffset(); |
521 | assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit" ); |
522 | AP->emitULEB128(Value: Offset, Desc: nullptr, PadTo: ULEB128PadSize); |
523 | } |
524 | |
525 | unsigned DIEBaseTypeRef::sizeOf(const dwarf::FormParams &, dwarf::Form) const { |
526 | return ULEB128PadSize; |
527 | } |
528 | |
529 | LLVM_DUMP_METHOD |
530 | void DIEBaseTypeRef::print(raw_ostream &O) const { O << "BaseTypeRef: " << Index; } |
531 | |
532 | //===----------------------------------------------------------------------===// |
533 | // DIEDelta Implementation |
534 | //===----------------------------------------------------------------------===// |
535 | |
536 | /// EmitValue - Emit delta value. |
537 | /// |
538 | void DIEDelta::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
539 | AP->emitLabelDifference(Hi: LabelHi, Lo: LabelLo, |
540 | Size: sizeOf(FormParams: AP->getDwarfFormParams(), Form)); |
541 | } |
542 | |
543 | /// SizeOf - Determine size of delta value in bytes. |
544 | /// |
545 | unsigned DIEDelta::sizeOf(const dwarf::FormParams &FormParams, |
546 | dwarf::Form Form) const { |
547 | switch (Form) { |
548 | case dwarf::DW_FORM_data4: |
549 | return 4; |
550 | case dwarf::DW_FORM_data8: |
551 | return 8; |
552 | case dwarf::DW_FORM_sec_offset: |
553 | return FormParams.getDwarfOffsetByteSize(); |
554 | default: |
555 | llvm_unreachable("DIE Value form not supported yet" ); |
556 | } |
557 | } |
558 | |
559 | LLVM_DUMP_METHOD |
560 | void DIEDelta::print(raw_ostream &O) const { |
561 | O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName(); |
562 | } |
563 | |
564 | //===----------------------------------------------------------------------===// |
565 | // DIEString Implementation |
566 | //===----------------------------------------------------------------------===// |
567 | |
568 | /// EmitValue - Emit string value. |
569 | /// |
570 | void DIEString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
571 | // Index of string in symbol table. |
572 | switch (Form) { |
573 | case dwarf::DW_FORM_GNU_str_index: |
574 | case dwarf::DW_FORM_strx: |
575 | case dwarf::DW_FORM_strx1: |
576 | case dwarf::DW_FORM_strx2: |
577 | case dwarf::DW_FORM_strx3: |
578 | case dwarf::DW_FORM_strx4: |
579 | DIEInteger(S.getIndex()).emitValue(Asm: AP, Form); |
580 | return; |
581 | case dwarf::DW_FORM_strp: |
582 | if (AP->doesDwarfUseRelocationsAcrossSections()) |
583 | DIELabel(S.getSymbol()).emitValue(AP, Form); |
584 | else |
585 | DIEInteger(S.getOffset()).emitValue(Asm: AP, Form); |
586 | return; |
587 | default: |
588 | llvm_unreachable("Expected valid string form" ); |
589 | } |
590 | } |
591 | |
592 | /// sizeOf - Determine size of delta value in bytes. |
593 | /// |
594 | unsigned DIEString::sizeOf(const dwarf::FormParams &FormParams, |
595 | dwarf::Form Form) const { |
596 | // Index of string in symbol table. |
597 | switch (Form) { |
598 | case dwarf::DW_FORM_GNU_str_index: |
599 | case dwarf::DW_FORM_strx: |
600 | case dwarf::DW_FORM_strx1: |
601 | case dwarf::DW_FORM_strx2: |
602 | case dwarf::DW_FORM_strx3: |
603 | case dwarf::DW_FORM_strx4: |
604 | return DIEInteger(S.getIndex()).sizeOf(FormParams, Form); |
605 | case dwarf::DW_FORM_strp: |
606 | if (FormParams.DwarfUsesRelocationsAcrossSections) |
607 | return DIELabel(S.getSymbol()).sizeOf(FormParams, Form); |
608 | return DIEInteger(S.getOffset()).sizeOf(FormParams, Form); |
609 | default: |
610 | llvm_unreachable("Expected valid string form" ); |
611 | } |
612 | } |
613 | |
614 | LLVM_DUMP_METHOD |
615 | void DIEString::print(raw_ostream &O) const { |
616 | O << "String: " << S.getString(); |
617 | } |
618 | |
619 | //===----------------------------------------------------------------------===// |
620 | // DIEInlineString Implementation |
621 | //===----------------------------------------------------------------------===// |
622 | void DIEInlineString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
623 | if (Form == dwarf::DW_FORM_string) { |
624 | AP->OutStreamer->emitBytes(Data: S); |
625 | AP->emitInt8(Value: 0); |
626 | return; |
627 | } |
628 | llvm_unreachable("Expected valid string form" ); |
629 | } |
630 | |
631 | unsigned DIEInlineString::sizeOf(const dwarf::FormParams &, dwarf::Form) const { |
632 | // Emit string bytes + NULL byte. |
633 | return S.size() + 1; |
634 | } |
635 | |
636 | LLVM_DUMP_METHOD |
637 | void DIEInlineString::print(raw_ostream &O) const { |
638 | O << "InlineString: " << S; |
639 | } |
640 | |
641 | //===----------------------------------------------------------------------===// |
642 | // DIEEntry Implementation |
643 | //===----------------------------------------------------------------------===// |
644 | |
645 | /// EmitValue - Emit debug information entry offset. |
646 | /// |
647 | void DIEEntry::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
648 | |
649 | switch (Form) { |
650 | case dwarf::DW_FORM_ref1: |
651 | case dwarf::DW_FORM_ref2: |
652 | case dwarf::DW_FORM_ref4: |
653 | case dwarf::DW_FORM_ref8: |
654 | AP->OutStreamer->emitIntValue(Value: Entry->getOffset(), |
655 | Size: sizeOf(FormParams: AP->getDwarfFormParams(), Form)); |
656 | return; |
657 | |
658 | case dwarf::DW_FORM_ref_udata: |
659 | AP->emitULEB128(Value: Entry->getOffset()); |
660 | return; |
661 | |
662 | case dwarf::DW_FORM_ref_addr: { |
663 | // Get the absolute offset for this DIE within the debug info/types section. |
664 | uint64_t Addr = Entry->getDebugSectionOffset(); |
665 | if (const MCSymbol *SectionSym = |
666 | Entry->getUnit()->getCrossSectionRelativeBaseAddress()) { |
667 | AP->emitLabelPlusOffset(Label: SectionSym, Offset: Addr, |
668 | Size: sizeOf(FormParams: AP->getDwarfFormParams(), Form), IsSectionRelative: true); |
669 | return; |
670 | } |
671 | |
672 | AP->OutStreamer->emitIntValue(Value: Addr, Size: sizeOf(FormParams: AP->getDwarfFormParams(), Form)); |
673 | return; |
674 | } |
675 | default: |
676 | llvm_unreachable("Improper form for DIE reference" ); |
677 | } |
678 | } |
679 | |
680 | unsigned DIEEntry::sizeOf(const dwarf::FormParams &FormParams, |
681 | dwarf::Form Form) const { |
682 | switch (Form) { |
683 | case dwarf::DW_FORM_ref1: |
684 | return 1; |
685 | case dwarf::DW_FORM_ref2: |
686 | return 2; |
687 | case dwarf::DW_FORM_ref4: |
688 | return 4; |
689 | case dwarf::DW_FORM_ref8: |
690 | return 8; |
691 | case dwarf::DW_FORM_ref_udata: |
692 | return getULEB128Size(Value: Entry->getOffset()); |
693 | case dwarf::DW_FORM_ref_addr: |
694 | return FormParams.getRefAddrByteSize(); |
695 | |
696 | default: |
697 | llvm_unreachable("Improper form for DIE reference" ); |
698 | } |
699 | } |
700 | |
701 | LLVM_DUMP_METHOD |
702 | void DIEEntry::print(raw_ostream &O) const { |
703 | O << format(Fmt: "Die: 0x%lx" , Vals: (long)(intptr_t)&Entry); |
704 | } |
705 | |
706 | //===----------------------------------------------------------------------===// |
707 | // DIELoc Implementation |
708 | //===----------------------------------------------------------------------===// |
709 | |
710 | unsigned DIELoc::computeSize(const dwarf::FormParams &FormParams) const { |
711 | if (!Size) { |
712 | for (const auto &V : values()) |
713 | Size += V.sizeOf(FormParams); |
714 | } |
715 | |
716 | return Size; |
717 | } |
718 | |
719 | /// EmitValue - Emit location data. |
720 | /// |
721 | void DIELoc::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const { |
722 | switch (Form) { |
723 | default: llvm_unreachable("Improper form for block" ); |
724 | case dwarf::DW_FORM_block1: Asm->emitInt8(Value: Size); break; |
725 | case dwarf::DW_FORM_block2: Asm->emitInt16(Value: Size); break; |
726 | case dwarf::DW_FORM_block4: Asm->emitInt32(Value: Size); break; |
727 | case dwarf::DW_FORM_block: |
728 | case dwarf::DW_FORM_exprloc: |
729 | Asm->emitULEB128(Value: Size); |
730 | break; |
731 | } |
732 | |
733 | for (const auto &V : values()) |
734 | V.emitValue(AP: Asm); |
735 | } |
736 | |
737 | /// sizeOf - Determine size of location data in bytes. |
738 | /// |
739 | unsigned DIELoc::sizeOf(const dwarf::FormParams &, dwarf::Form Form) const { |
740 | switch (Form) { |
741 | case dwarf::DW_FORM_block1: return Size + sizeof(int8_t); |
742 | case dwarf::DW_FORM_block2: return Size + sizeof(int16_t); |
743 | case dwarf::DW_FORM_block4: return Size + sizeof(int32_t); |
744 | case dwarf::DW_FORM_block: |
745 | case dwarf::DW_FORM_exprloc: |
746 | return Size + getULEB128Size(Value: Size); |
747 | default: llvm_unreachable("Improper form for block" ); |
748 | } |
749 | } |
750 | |
751 | LLVM_DUMP_METHOD |
752 | void DIELoc::print(raw_ostream &O) const { |
753 | printValues(O, Values: *this, Type: "ExprLoc" , Size, IndentCount: 5); |
754 | } |
755 | |
756 | //===----------------------------------------------------------------------===// |
757 | // DIEBlock Implementation |
758 | //===----------------------------------------------------------------------===// |
759 | |
760 | unsigned DIEBlock::computeSize(const dwarf::FormParams &FormParams) const { |
761 | if (!Size) { |
762 | for (const auto &V : values()) |
763 | Size += V.sizeOf(FormParams); |
764 | } |
765 | |
766 | return Size; |
767 | } |
768 | |
769 | /// EmitValue - Emit block data. |
770 | /// |
771 | void DIEBlock::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const { |
772 | switch (Form) { |
773 | default: llvm_unreachable("Improper form for block" ); |
774 | case dwarf::DW_FORM_block1: Asm->emitInt8(Value: Size); break; |
775 | case dwarf::DW_FORM_block2: Asm->emitInt16(Value: Size); break; |
776 | case dwarf::DW_FORM_block4: Asm->emitInt32(Value: Size); break; |
777 | case dwarf::DW_FORM_exprloc: |
778 | case dwarf::DW_FORM_block: |
779 | Asm->emitULEB128(Value: Size); |
780 | break; |
781 | case dwarf::DW_FORM_string: break; |
782 | case dwarf::DW_FORM_data16: break; |
783 | } |
784 | |
785 | for (const auto &V : values()) |
786 | V.emitValue(AP: Asm); |
787 | } |
788 | |
789 | /// sizeOf - Determine size of block data in bytes. |
790 | /// |
791 | unsigned DIEBlock::sizeOf(const dwarf::FormParams &, dwarf::Form Form) const { |
792 | switch (Form) { |
793 | case dwarf::DW_FORM_block1: return Size + sizeof(int8_t); |
794 | case dwarf::DW_FORM_block2: return Size + sizeof(int16_t); |
795 | case dwarf::DW_FORM_block4: return Size + sizeof(int32_t); |
796 | case dwarf::DW_FORM_exprloc: |
797 | case dwarf::DW_FORM_block: return Size + getULEB128Size(Value: Size); |
798 | case dwarf::DW_FORM_data16: return 16; |
799 | default: llvm_unreachable("Improper form for block" ); |
800 | } |
801 | } |
802 | |
803 | LLVM_DUMP_METHOD |
804 | void DIEBlock::print(raw_ostream &O) const { |
805 | printValues(O, Values: *this, Type: "Blk" , Size, IndentCount: 5); |
806 | } |
807 | |
808 | //===----------------------------------------------------------------------===// |
809 | // DIELocList Implementation |
810 | //===----------------------------------------------------------------------===// |
811 | |
812 | unsigned DIELocList::sizeOf(const dwarf::FormParams &FormParams, |
813 | dwarf::Form Form) const { |
814 | switch (Form) { |
815 | case dwarf::DW_FORM_loclistx: |
816 | return getULEB128Size(Value: Index); |
817 | case dwarf::DW_FORM_data4: |
818 | assert(FormParams.Format != dwarf::DWARF64 && |
819 | "DW_FORM_data4 is not suitable to emit a pointer to a location list " |
820 | "in the 64-bit DWARF format" ); |
821 | return 4; |
822 | case dwarf::DW_FORM_data8: |
823 | assert(FormParams.Format == dwarf::DWARF64 && |
824 | "DW_FORM_data8 is not suitable to emit a pointer to a location list " |
825 | "in the 32-bit DWARF format" ); |
826 | return 8; |
827 | case dwarf::DW_FORM_sec_offset: |
828 | return FormParams.getDwarfOffsetByteSize(); |
829 | default: |
830 | llvm_unreachable("DIE Value form not supported yet" ); |
831 | } |
832 | } |
833 | |
834 | /// EmitValue - Emit label value. |
835 | /// |
836 | void DIELocList::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
837 | if (Form == dwarf::DW_FORM_loclistx) { |
838 | AP->emitULEB128(Value: Index); |
839 | return; |
840 | } |
841 | DwarfDebug *DD = AP->getDwarfDebug(); |
842 | MCSymbol *Label = DD->getDebugLocs().getList(LI: Index).Label; |
843 | AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf()); |
844 | } |
845 | |
846 | LLVM_DUMP_METHOD |
847 | void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; } |
848 | |
849 | //===----------------------------------------------------------------------===// |
850 | // DIEAddrOffset Implementation |
851 | //===----------------------------------------------------------------------===// |
852 | |
853 | unsigned DIEAddrOffset::sizeOf(const dwarf::FormParams &FormParams, |
854 | dwarf::Form) const { |
855 | return Addr.sizeOf(FormParams, Form: dwarf::DW_FORM_addrx) + |
856 | Offset.sizeOf(FormParams, Form: dwarf::DW_FORM_data4); |
857 | } |
858 | |
859 | /// EmitValue - Emit label value. |
860 | /// |
861 | void DIEAddrOffset::emitValue(const AsmPrinter *AP, dwarf::Form Form) const { |
862 | Addr.emitValue(Asm: AP, Form: dwarf::DW_FORM_addrx); |
863 | Offset.emitValue(AP, Form: dwarf::DW_FORM_data4); |
864 | } |
865 | |
866 | LLVM_DUMP_METHOD |
867 | void DIEAddrOffset::print(raw_ostream &O) const { |
868 | O << "AddrOffset: " ; |
869 | Addr.print(O); |
870 | O << " + " ; |
871 | Offset.print(O); |
872 | } |
873 | |