LLVM 24.0.0git
DwarfTransformer.cpp
Go to the documentation of this file.
1//===- DwarfTransformer.cpp -----------------------------------------------===//
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
12#include "llvm/Support/Error.h"
15
22
23#include <optional>
24
25using namespace llvm;
26using namespace gsym;
27
30 const char *CompDir;
31 std::vector<uint32_t> FileCache;
32 uint64_t Language = 0;
34
37 CompDir = CU->getCompilationDir();
38 FileCache.clear();
39 if (LineTable)
40 FileCache.assign(LineTable->Prologue.FileNames.size() + 1, UINT32_MAX);
41 DWARFDie Die = CU->getUnitDIE();
42 Language = dwarf::toUnsigned(Die.find(dwarf::DW_AT_language), 0);
43 AddrSize = CU->getAddressByteSize();
44 }
45
46 /// Return true if Addr is the highest address for a given compile unit. The
47 /// highest address is encoded as -1, of all ones in the address. These high
48 /// addresses are used by some linkers to indicate that a function has been
49 /// dead stripped or didn't end up in the linked executable.
50 bool isHighestAddress(uint64_t Addr) const {
51 if (AddrSize == 4)
52 return Addr == UINT32_MAX;
53 else if (AddrSize == 8)
54 return Addr == UINT64_MAX;
55 return false;
56 }
57
58 /// Convert a DWARF compile unit file index into a GSYM global file index.
59 ///
60 /// Each compile unit in DWARF has its own file table in the line table
61 /// prologue. GSYM has a single large file table that applies to all files
62 /// from all of the info in a GSYM file. This function converts between the
63 /// two and caches and DWARF CU file index that has already been converted so
64 /// the first client that asks for a compile unit file index will end up
65 /// doing the conversion, and subsequent clients will get the cached GSYM
66 /// index.
67 std::optional<uint32_t> DWARFToGSYMFileIndex(GsymCreator &Gsym,
68 uint32_t DwarfFileIdx) {
69 if (!LineTable || DwarfFileIdx >= FileCache.size())
70 return std::nullopt;
71 uint32_t &GsymFileIdx = FileCache[DwarfFileIdx];
72 if (GsymFileIdx != UINT32_MAX)
73 return GsymFileIdx;
74 std::string File;
75 if (LineTable->getFileNameByIndex(
76 DwarfFileIdx, CompDir,
78 GsymFileIdx = Gsym.insertFile(File);
79 else
80 GsymFileIdx = 0;
81 return GsymFileIdx;
82 }
83};
84
85
87 if (DWARFDie SpecDie =
88 Die.getAttributeValueAsReferencedDie(dwarf::DW_AT_specification)) {
89 if (DWARFDie SpecParent = GetParentDeclContextDIE(SpecDie))
90 return SpecParent;
91 }
92 if (DWARFDie AbstDie =
93 Die.getAttributeValueAsReferencedDie(dwarf::DW_AT_abstract_origin)) {
94 if (DWARFDie AbstParent = GetParentDeclContextDIE(AbstDie))
95 return AbstParent;
96 }
97
98 // We never want to follow parent for inlined subroutine - that would
99 // give us information about where the function is inlined, not what
100 // function is inlined
101 if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine)
102 return DWARFDie();
103
104 DWARFDie ParentDie = Die.getParent();
105 if (!ParentDie)
106 return DWARFDie();
107
108 switch (ParentDie.getTag()) {
109 case dwarf::DW_TAG_namespace:
110 case dwarf::DW_TAG_structure_type:
111 case dwarf::DW_TAG_union_type:
112 case dwarf::DW_TAG_class_type:
113 case dwarf::DW_TAG_subprogram:
114 return ParentDie; // Found parent decl context DIE
115 case dwarf::DW_TAG_lexical_block:
116 return GetParentDeclContextDIE(ParentDie);
117 default:
118 break;
119 }
120
121 return DWARFDie();
122}
123
124/// Get the GsymCreator string table offset for the qualified name for the
125/// DIE passed in. This function will avoid making copies of any strings in
126/// the GsymCreator when possible. We don't need to copy a string when the
127/// string comes from our .debug_str section or is an inlined string in the
128/// .debug_info. If we create a qualified name string in this function by
129/// combining multiple strings in the DWARF string table or info, we will make
130/// a copy of the string when we add it to the string table.
131static std::optional<gsym_strp_t>
133 // If the dwarf has mangled name, use mangled name
134 if (auto LinkageName = Die.getLinkageName()) {
135 // We have seen cases were linkage name is actually empty.
136 if (strlen(LinkageName) > 0)
137 return Gsym.insertString(LinkageName, /* Copy */ false);
138 }
139
141 if (ShortName.empty())
142 return std::nullopt;
143
144 // For C++ and ObjC, prepend names of all parent declaration contexts
145 if (!(Language == dwarf::DW_LANG_C_plus_plus ||
146 Language == dwarf::DW_LANG_C_plus_plus_03 ||
147 Language == dwarf::DW_LANG_C_plus_plus_11 ||
148 Language == dwarf::DW_LANG_C_plus_plus_14 ||
149 Language == dwarf::DW_LANG_ObjC_plus_plus ||
150 // This should not be needed for C, but we see C++ code marked as C
151 // in some binaries. This should hurt, so let's do it for C as well
152 Language == dwarf::DW_LANG_C))
153 return Gsym.insertString(ShortName, /* Copy */ false);
154
155 // Some GCC optimizations create functions with names ending with .isra.<num>
156 // or .part.<num> and those names are just DW_AT_name, not DW_AT_linkage_name
157 // If it looks like it could be the case, don't add any prefix
158 if (ShortName.starts_with("_Z") &&
159 (ShortName.contains(".isra.") || ShortName.contains(".part.")))
160 return Gsym.insertString(ShortName, /* Copy */ false);
161
162 DWARFDie ParentDeclCtxDie = GetParentDeclContextDIE(Die);
163 if (ParentDeclCtxDie) {
164 std::string Name = ShortName.str();
165 while (ParentDeclCtxDie) {
166 StringRef ParentName(ParentDeclCtxDie.getName(DINameKind::ShortName));
167 if (!ParentName.empty()) {
168 // "lambda" names are wrapped in < >. Replace with { }
169 // to be consistent with demangled names and not to confuse with
170 // templates
171 if (ParentName.front() == '<' && ParentName.back() == '>')
172 Name = "{" + ParentName.substr(1, ParentName.size() - 2).str() + "}" +
173 "::" + Name;
174 else
175 Name = ParentName.str() + "::" + Name;
176 }
177 ParentDeclCtxDie = GetParentDeclContextDIE(ParentDeclCtxDie);
178 }
179 // Copy the name since we created a new name in a std::string.
180 return Gsym.insertString(Name, /* Copy */ true);
181 }
182 // Don't copy the name since it exists in the DWARF object file.
183 return Gsym.insertString(ShortName, /* Copy */ false);
184}
185
187 bool CheckChildren = true;
188 switch (Die.getTag()) {
189 case dwarf::DW_TAG_subprogram:
190 // Don't look into functions within functions.
191 CheckChildren = Depth == 0;
192 break;
193 case dwarf::DW_TAG_inlined_subroutine:
194 return true;
195 default:
196 break;
197 }
198 if (!CheckChildren)
199 return false;
200 for (DWARFDie ChildDie : Die.children()) {
201 if (hasInlineInfo(ChildDie, Depth + 1))
202 return true;
203 }
204 return false;
205}
206
207static AddressRanges
209 AddressRanges Ranges;
210 for (const DWARFAddressRange &DwarfRange : DwarfRanges) {
211 if (DwarfRange.LowPC < DwarfRange.HighPC)
212 Ranges.insert({DwarfRange.LowPC, DwarfRange.HighPC});
213 }
214 return Ranges;
215}
216
218 CUInfo &CUI, DWARFDie Die, uint32_t Depth,
219 FunctionInfo &FI, InlineInfo &Parent,
220 const AddressRanges &AllParentRanges,
221 bool &WarnIfEmpty) {
222 if (!hasInlineInfo(Die, Depth))
223 return;
224
225 dwarf::Tag Tag = Die.getTag();
226 if (Tag == dwarf::DW_TAG_inlined_subroutine) {
227 // create new InlineInfo and append to parent.children
229 AddressRanges AllInlineRanges;
231 if (RangesOrError) {
232 AllInlineRanges = ConvertDWARFRanges(RangesOrError.get());
233 uint32_t EmptyCount = 0;
234 for (const AddressRange &InlineRange : AllInlineRanges) {
235 // Check for empty inline range in case inline function was outlined
236 // or has not code
237 if (InlineRange.empty()) {
238 ++EmptyCount;
239 } else {
240 if (Parent.Ranges.contains(InlineRange)) {
241 II.Ranges.insert(InlineRange);
242 } else {
243 // Only warn if the current inline range is not within any of all
244 // of the parent ranges. If we have a DW_TAG_subpgram with multiple
245 // ranges we will emit a FunctionInfo for each range of that
246 // function that only emits information within the current range,
247 // so we only want to emit an error if the DWARF has issues, not
248 // when a range currently just isn't in the range we are currently
249 // parsing for.
250 if (AllParentRanges.contains(InlineRange)) {
251 WarnIfEmpty = false;
252 } else
253 Out.Report("Function DIE has uncontained address range",
254 [&](raw_ostream &OS) {
255 OS << "error: inlined function DIE at "
256 << HEX32(Die.getOffset()) << " has a range ["
257 << HEX64(InlineRange.start()) << " - "
258 << HEX64(InlineRange.end())
259 << ") that isn't contained in "
260 << "any parent address ranges, this inline range "
261 "will be "
262 "removed.\n";
263 });
264 }
265 }
266 }
267 // If we have all empty ranges for the inlines, then don't warn if we
268 // have an empty InlineInfo at the top level as all inline functions
269 // were elided.
270 if (EmptyCount == AllInlineRanges.size())
271 WarnIfEmpty = false;
272 }
273 if (II.Ranges.empty())
274 return;
275
276 if (auto NameIndex = getQualifiedNameIndex(Die, CUI.Language, Gsym))
277 II.Name = *NameIndex;
278 const uint64_t DwarfFileIdx = dwarf::toUnsigned(
279 Die.findRecursively(dwarf::DW_AT_call_file), UINT32_MAX);
280 std::optional<uint32_t> OptGSymFileIdx =
281 CUI.DWARFToGSYMFileIndex(Gsym, DwarfFileIdx);
282 if (OptGSymFileIdx) {
283 II.CallFile = OptGSymFileIdx.value();
284 II.CallLine = dwarf::toUnsigned(Die.find(dwarf::DW_AT_call_line), 0);
285 // parse all children and append to parent
286 for (DWARFDie ChildDie : Die.children())
287 parseInlineInfo(Gsym, Out, CUI, ChildDie, Depth + 1, FI, II,
288 AllInlineRanges, WarnIfEmpty);
289 Parent.Children.emplace_back(std::move(II));
290 } else
291 Out.Report(
292 "Inlined function die has invlaid file index in DW_AT_call_file",
293 [&](raw_ostream &OS) {
294 OS << "error: inlined function DIE at " << HEX32(Die.getOffset())
295 << " has an invalid file index " << DwarfFileIdx
296 << " in its DW_AT_call_file attribute, this inline entry and "
297 "all "
298 << "children will be removed.\n";
299 });
300 return;
301 }
302 if (Tag == dwarf::DW_TAG_subprogram || Tag == dwarf::DW_TAG_lexical_block) {
303 // skip this Die and just recurse down
304 for (DWARFDie ChildDie : Die.children())
305 parseInlineInfo(Gsym, Out, CUI, ChildDie, Depth + 1, FI, Parent,
306 AllParentRanges, WarnIfEmpty);
307 }
308}
309
311 DWARFDie Die, GsymCreator &Gsym,
312 FunctionInfo &FI) {
313 std::vector<uint32_t> RowVector;
314 const uint64_t StartAddress = FI.startAddress();
315 const uint64_t EndAddress = FI.endAddress();
316 const uint64_t RangeSize = EndAddress - StartAddress;
317 const object::SectionedAddress SecAddress{
319
320 // Attempt to retrieve DW_AT_LLVM_stmt_sequence if present.
321 std::optional<uint64_t> StmtSeqOffset;
322 if (auto StmtSeqAttr = Die.find(llvm::dwarf::DW_AT_LLVM_stmt_sequence)) {
323 // The `DW_AT_LLVM_stmt_sequence` attribute might be set to an invalid
324 // sentinel value when it refers to an empty line sequence. In such cases,
325 // the DWARF linker will exclude the empty sequence from the final output
326 // and assign the sentinel value to the `DW_AT_LLVM_stmt_sequence`
327 // attribute. The sentinel value is UINT32_MAX for DWARF32 and UINT64_MAX
328 // for DWARF64.
329 const uint64_t InvalidOffset =
331 uint64_t StmtSeqVal = dwarf::toSectionOffset(StmtSeqAttr, InvalidOffset);
332 if (StmtSeqVal != InvalidOffset)
333 StmtSeqOffset = StmtSeqVal;
334 }
335
336 if (!CUI.LineTable->lookupAddressRange(SecAddress, RangeSize, RowVector,
337 StmtSeqOffset)) {
338 // If StmtSeqOffset had a value but the lookup failed, try again without it.
339 // If the second lookup succeeds, we know the DW_AT_LLVM_stmt_sequence value
340 // was invalid, but we still have valid line entries.
341 if (StmtSeqOffset &&
342 CUI.LineTable->lookupAddressRange(SecAddress, RangeSize, RowVector)) {
343 Out.Report("Invalid DW_AT_LLVM_stmt_sequence value",
344 [&](raw_ostream &OS) {
345 OS << "error: function DIE at " << HEX32(Die.getOffset())
346 << " has a DW_AT_LLVM_stmt_sequence value "
347 << HEX32(*StmtSeqOffset)
348 << " which doesn't match any line table "
349 << "sequence offset but there are " << RowVector.size()
350 << " matching line entries in other sequences.\n";
351 });
352 } else {
353 // If we have a DW_TAG_subprogram but no line entries, fall back to using
354 // the DW_AT_decl_file an d DW_AT_decl_line if we have both attributes.
355 std::string FilePath = Die.getDeclFile(
357 if (FilePath.empty()) {
358 // If we had a DW_AT_decl_file, but got no file then we need to emit a
359 // warning.
360 const uint64_t DwarfFileIdx = dwarf::toUnsigned(
361 Die.findRecursively(dwarf::DW_AT_decl_file), UINT32_MAX);
362 // Check if there is no DW_AT_decl_line attribute, and don't report an
363 // error if it isn't there.
364 if (DwarfFileIdx == UINT32_MAX)
365 return;
366 Out.Report("Invalid file index in DW_AT_decl_file", [&](raw_ostream
367 &OS) {
368 OS << "error: function DIE at " << HEX32(Die.getOffset())
369 << " has an invalid file index " << DwarfFileIdx
370 << " in its DW_AT_decl_file attribute, unable to create a single "
371 << "line entry from the DW_AT_decl_file/DW_AT_decl_line "
372 << "attributes.\n";
373 });
374 return;
375 }
376 if (auto Line = dwarf::toUnsigned(
377 Die.findRecursively({dwarf::DW_AT_decl_line}))) {
378 LineEntry LE(StartAddress, Gsym.insertFile(FilePath), *Line);
379 FI.OptLineTable = LineTable();
380 FI.OptLineTable->push(LE);
381 }
382 return;
383 }
384 }
385
386 FI.OptLineTable = LineTable();
387 DWARFDebugLine::Row PrevRow;
388 for (uint32_t RowIndex : RowVector) {
389 // Take file number and line/column from the row.
390 const DWARFDebugLine::Row &Row = CUI.LineTable->Rows[RowIndex];
391 std::optional<uint32_t> OptFileIdx =
392 CUI.DWARFToGSYMFileIndex(Gsym, Row.File);
393 if (!OptFileIdx) {
394 Out.Report(
395 "Invalid file index in DWARF line table", [&](raw_ostream &OS) {
396 OS << "error: function DIE at " << HEX32(Die.getOffset()) << " has "
397 << "a line entry with invalid DWARF file index, this entry will "
398 << "be removed:\n";
399 Row.dumpTableHeader(OS, /*Indent=*/0);
400 Row.dump(OS);
401 OS << "\n";
402 });
403 continue;
404 }
405 const uint32_t FileIdx = OptFileIdx.value();
406 uint64_t RowAddress = Row.Address.Address;
407 // Watch out for a RowAddress that is in the middle of a line table entry
408 // in the DWARF. If we pass an address in between two line table entries
409 // we will get a RowIndex for the previous valid line table row which won't
410 // be contained in our function. This is usually a bug in the DWARF due to
411 // linker problems or LTO or other DWARF re-linking so it is worth emitting
412 // an error, but not worth stopping the creation of the GSYM.
413 if (!FI.Range.contains(RowAddress)) {
414 if (RowAddress < FI.Range.start()) {
415 Out.Report("Start address lies between valid Row table entries",
416 [&](raw_ostream &OS) {
417 OS << "error: DIE has a start address whose LowPC is "
418 "between the "
419 "line table Row["
420 << RowIndex << "] with address " << HEX64(RowAddress)
421 << " and the next one.\n";
423 });
424 RowAddress = FI.Range.start();
425 } else {
426 continue;
427 }
428 }
429
430 LineEntry LE(RowAddress, FileIdx, Row.Line);
431 if (RowIndex != RowVector[0] && Row.Address < PrevRow.Address) {
432 // We have seen full duplicate line tables for functions in some
433 // DWARF files. Watch for those here by checking the last
434 // row was the function's end address (HighPC) and that the
435 // current line table entry's address is the same as the first
436 // line entry we already have in our "function_info.Lines". If
437 // so break out after printing a warning.
438 auto FirstLE = FI.OptLineTable->first();
439 if (FirstLE && *FirstLE == LE)
440 Out.Report("Duplicate line table detected", [&](raw_ostream &OS) {
441 OS << "warning: duplicate line table detected for DIE:\n";
443 });
444 else
445 Out.Report("Non-monotonically increasing addresses",
446 [&](raw_ostream &OS) {
447 OS << "error: line table has addresses that do not "
448 << "monotonically increase:\n";
449 for (uint32_t RowIndex2 : RowVector)
450 CUI.LineTable->Rows[RowIndex2].dump(OS);
452 });
453 break;
454 }
455
456 // Skip multiple line entries for the same file and line.
457 auto LastLE = FI.OptLineTable->last();
458 if (LastLE && LastLE->File == FileIdx && LastLE->Line == Row.Line)
459 continue;
460 // Only push a row if it isn't an end sequence. End sequence markers are
461 // included for the last address in a function or the last contiguous
462 // address in a sequence.
463 if (Row.EndSequence) {
464 // End sequence means that the next line entry could have a lower address
465 // that the previous entries. So we clear the previous row so we don't
466 // trigger the line table error about address that do not monotonically
467 // increase.
468 PrevRow = DWARFDebugLine::Row();
469 } else {
470 FI.OptLineTable->push(LE);
471 PrevRow = Row;
472 }
473 }
474 // If not line table rows were added, clear the line table so we don't encode
475 // on in the GSYM file.
476 if (FI.OptLineTable->empty())
477 FI.OptLineTable = std::nullopt;
478}
479
480void DwarfTransformer::handleDie(OutputAggregator &Out, CUInfo &CUI,
481 DWARFDie Die) {
482 switch (Die.getTag()) {
483 case dwarf::DW_TAG_subprogram: {
484 Expected<DWARFAddressRangesVector> RangesOrError = Die.getAddressRanges();
485 if (!RangesOrError) {
486 consumeError(RangesOrError.takeError());
487 break;
488 }
489 const DWARFAddressRangesVector &Ranges = RangesOrError.get();
490 if (Ranges.empty())
491 break;
492 auto NameIndex = getQualifiedNameIndex(Die, CUI.Language, Gsym);
493 if (!NameIndex) {
494 Out.Report("Function has no name", [&](raw_ostream &OS) {
495 OS << "error: function at " << HEX64(Die.getOffset())
496 << " has no name\n ";
498 });
499 break;
500 }
501 // All ranges for the subprogram DIE in case it has multiple. We need to
502 // pass this down into parseInlineInfo so we don't warn about inline
503 // ranges that are not in the current subrange of a function when they
504 // actually are in another subgrange. We do this because when a function
505 // has discontiguos ranges, we create multiple function entries with only
506 // the info for that range contained inside of it.
507 AddressRanges AllSubprogramRanges = ConvertDWARFRanges(Ranges);
508
509 // Create a function_info for each range
510 for (const DWARFAddressRange &Range : Ranges) {
511 // The low PC must be less than the high PC. Many linkers don't remove
512 // DWARF for functions that don't get linked into the final executable.
513 // If both the high and low pc have relocations, linkers will often set
514 // the address values for both to the same value to indicate the function
515 // has been remove. Other linkers have been known to set the one or both
516 // PC values to a UINT32_MAX for 4 byte addresses and UINT64_MAX for 8
517 // byte addresses to indicate the function isn't valid. The check below
518 // tries to watch for these cases and abort if it runs into them.
519 if (Range.LowPC >= Range.HighPC || CUI.isHighestAddress(Range.LowPC))
520 break;
521
522 // Many linkers can't remove DWARF and might set the LowPC to zero. Since
523 // high PC can be an offset from the low PC in more recent DWARF versions
524 // we need to watch for a zero'ed low pc which we do using ValidTextRanges
525 // below.
526 if (!Gsym.IsValidTextAddress(Range.LowPC)) {
527 // We expect zero and -1 to be invalid addresses in DWARF depending
528 // on the linker of the DWARF. This indicates a function was stripped
529 // and the debug info wasn't able to be stripped from the DWARF. If
530 // the LowPC isn't zero or -1, then we should emit an error.
531 if (Range.LowPC != 0) {
532 // Unexpected invalid address, emit a warning
533 Out.Report("Address range starts outside executable section",
534 [&](raw_ostream &OS) {
535 OS << "warning: DIE has an address range whose "
536 "start address "
537 "is not in any executable sections ("
538 << *Gsym.GetValidTextRanges()
539 << ") and will not be processed:\n";
541 });
542 }
543 break;
544 }
545
546 FunctionInfo FI;
547 FI.Range = {Range.LowPC, Range.HighPC};
548 FI.Name = *NameIndex;
549 if (CUI.LineTable)
550 convertFunctionLineTable(Out, CUI, Die, Gsym, FI);
551
552 if (hasInlineInfo(Die, 0)) {
553 FI.Inline = InlineInfo();
554 FI.Inline->Name = *NameIndex;
555 FI.Inline->Ranges.insert(FI.Range);
556 bool WarnIfEmpty = true;
557 parseInlineInfo(Gsym, Out, CUI, Die, 0, FI, *FI.Inline,
558 AllSubprogramRanges, WarnIfEmpty);
559 // Make sure we at least got some valid inline info other than just
560 // the top level function. If we didn't then remove the inline info
561 // from the function info. We have seen cases where LTO tries to modify
562 // the DWARF for functions and it messes up the address ranges for
563 // the inline functions so it is no longer valid.
564 //
565 // By checking if there are any valid children on the top level inline
566 // information object, we will know if we got anything valid from the
567 // debug info.
568 if (FI.Inline->Children.empty()) {
569 if (WarnIfEmpty)
570 Out.Report("DIE contains inline functions with no valid ranges",
571 [&](raw_ostream &OS) {
572 OS << "warning: DIE contains inline function "
573 "information that has no valid ranges, removing "
574 "inline information:\n";
576 });
577 FI.Inline = std::nullopt;
578 }
579 }
580
581 // If dwarf-callsites flag is set, parse DW_TAG_call_site DIEs.
582 if (LoadDwarfCallSites)
583 parseCallSiteInfoFromDwarf(CUI, Die, FI);
584
585 Gsym.addFunctionInfo(std::move(FI));
586 }
587 } break;
588 default:
589 break;
590 }
591 for (DWARFDie ChildDie : Die.children())
592 handleDie(Out, CUI, ChildDie);
593}
594
595void DwarfTransformer::parseCallSiteInfoFromDwarf(CUInfo &CUI, DWARFDie Die,
596 FunctionInfo &FI) {
597 // Parse all DW_TAG_call_site DIEs that are children of this subprogram DIE.
598 // DWARF specification:
599 // - DW_TAG_call_site can have DW_AT_call_return_pc for return address offset.
600 // - DW_AT_call_origin might point to a DIE of the function being called.
601 // For simplicity, we will just extract return_offset and possibly target name
602 // if available.
603
604 CallSiteInfoCollection CSIC;
605
606 for (DWARFDie Child : Die.children()) {
607 if (Child.getTag() != dwarf::DW_TAG_call_site)
608 continue;
609
610 CallSiteInfo CSI;
611 // DW_AT_call_return_pc: the return PC (address). We'll convert it to
612 // offset relative to FI's start.
613 auto ReturnPC =
614 dwarf::toAddress(Child.findRecursively(dwarf::DW_AT_call_return_pc));
615 if (!ReturnPC || !FI.Range.contains(*ReturnPC))
616 continue;
617
618 CSI.ReturnOffset = *ReturnPC - FI.startAddress();
619
620 // Attempt to get function name from DW_AT_call_origin. If present, we can
621 // insert it as a match regex.
622 if (DWARFDie OriginDie =
623 Child.getAttributeValueAsReferencedDie(dwarf::DW_AT_call_origin)) {
624
625 // Include the full unmangled name if available, otherwise the short name.
626 if (const char *LinkName = OriginDie.getLinkageName()) {
627 gsym_strp_t LinkNameOff = Gsym.insertString(LinkName, /*Copy=*/false);
628 CSI.MatchRegex.push_back(LinkNameOff);
629 } else if (const char *ShortName = OriginDie.getShortName()) {
630 gsym_strp_t ShortNameOff = Gsym.insertString(ShortName, /*Copy=*/false);
631 CSI.MatchRegex.push_back(ShortNameOff);
632 }
633 }
634
635 // For now, we won't attempt to deduce InternalCall/ExternalCall flags
636 // from DWARF.
638
639 CSIC.CallSites.push_back(CSI);
640 }
641
642 if (!CSIC.CallSites.empty()) {
643 if (!FI.CallSites)
644 FI.CallSites = CallSiteInfoCollection();
645 // Append parsed DWARF callsites:
646 llvm::append_range(FI.CallSites->CallSites, CSIC.CallSites);
647 }
648}
649
651 size_t NumBefore = Gsym.getNumFunctionInfos();
652 auto getDie = [&](DWARFUnit &DwarfUnit) -> DWARFDie {
653 // Apple uses DW_AT_GNU_dwo_id for things other than split DWARF.
654 if (IsMachO)
655 return DwarfUnit.getUnitDIE(false);
656
657 if (DwarfUnit.getDWOId()) {
658 DWARFUnit *DWOCU = DwarfUnit.getNonSkeletonUnitDIE(false).getDwarfUnit();
659 if (!DWOCU->isDWOUnit())
660 Out.Report(
661 "warning: Unable to retrieve DWO .debug_info section for some "
662 "object files. (Remove the --quiet flag for full output)",
663 [&](raw_ostream &OS) {
664 std::string DWOName = dwarf::toString(
665 DwarfUnit.getUnitDIE().find(
666 {dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}),
667 "");
668 OS << "warning: Unable to retrieve DWO .debug_info section for "
669 << DWOName << "\n";
670 });
671 else {
672 return DWOCU->getUnitDIE(false);
673 }
674 }
675 return DwarfUnit.getUnitDIE(false);
676 };
677 if (NumThreads == 1) {
678 // Parse all DWARF data from this thread, use the same string/file table
679 // for everything
680 for (const auto &CU : DICtx.compile_units()) {
681 DWARFDie Die = getDie(*CU);
682 CUInfo CUI(DICtx, dyn_cast<DWARFCompileUnit>(CU.get()));
683 handleDie(Out, CUI, Die);
684 }
685 } else {
686 // LLVM Dwarf parser is not thread-safe and we need to parse all DWARF up
687 // front before we start accessing any DIEs since there might be
688 // cross compile unit references in the DWARF. If we don't do this we can
689 // end up crashing.
690
691 // We need to call getAbbreviations sequentially first so that getUnitDIE()
692 // only works with its local data.
693 for (const auto &CU : DICtx.compile_units())
694 CU->getAbbreviations();
695
696 // Now parse all DIEs in case we have cross compile unit references in a
697 // thread pool.
698 DefaultThreadPool pool(hardware_concurrency(NumThreads));
699 for (const auto &CU : DICtx.compile_units())
700 pool.async([&CU]() { CU->getUnitDIE(false /*CUDieOnly*/); });
701 pool.wait();
702
703 // Now convert all DWARF to GSYM in a thread pool.
704 std::mutex LogMutex;
705 for (const auto &CU : DICtx.compile_units()) {
706 DWARFDie Die = getDie(*CU);
707 if (Die) {
708 CUInfo CUI(DICtx, dyn_cast<DWARFCompileUnit>(CU.get()));
709 pool.async([this, CUI, &LogMutex, &Out, Die]() mutable {
710 std::string storage;
711 raw_string_ostream StrStream(storage);
712 OutputAggregator ThreadOut(Out.GetOS() ? &StrStream : nullptr,
713 Out.IsQuiet());
714 handleDie(ThreadOut, CUI, Die);
715 // Print ThreadLogStorage lines into an actual stream under a lock
716 std::lock_guard<std::mutex> guard(LogMutex);
717 if (Out.GetOS()) {
718 Out << storage;
719 }
720 Out.Merge(ThreadOut);
721 });
722 }
723 }
724 pool.wait();
725 }
726 size_t FunctionsAddedCount = Gsym.getNumFunctionInfos() - NumBefore;
727 Out << "Loaded " << FunctionsAddedCount << " functions from DWARF.\n";
728 return Error::success();
729}
730
732 OutputAggregator &Out) {
733 Out << "Verifying GSYM file \"" << GsymPath << "\":\n";
734
736 GsymReader::openFile(GsymPath);
737 if (!GsymOrErr)
738 return GsymOrErr.takeError();
739 std::unique_ptr<GsymReader> &Gsym = *GsymOrErr;
740
741 auto NumAddrs = Gsym->getNumAddresses();
744 DILineInfoSpecifier::FunctionNameKind::LinkageName);
745 std::string gsymFilename;
746 for (uint32_t I = 0; I < NumAddrs; ++I) {
747 auto FuncAddr = Gsym->getAddress(I);
748 if (!FuncAddr)
749 return createStringError(std::errc::invalid_argument,
750 "failed to extract address[%i]", I);
751
752 auto FI = Gsym->getFunctionInfo(*FuncAddr);
753 if (!FI)
754 return createStringError(
755 std::errc::invalid_argument,
756 "failed to extract function info for address 0x%" PRIu64, *FuncAddr);
757
758 for (auto Addr = *FuncAddr; Addr < *FuncAddr + FI->size(); ++Addr) {
759 const object::SectionedAddress SectAddr{
761 auto LR = Gsym->lookup(Addr);
762 if (!LR)
763 return LR.takeError();
764
765 auto DwarfInlineInfos =
766 DICtx.getInliningInfoForAddress(SectAddr, DLIS);
767 uint32_t NumDwarfInlineInfos = DwarfInlineInfos.getNumberOfFrames();
768 if (NumDwarfInlineInfos == 0) {
769 DwarfInlineInfos.addFrame(
770 DICtx.getLineInfoForAddress(SectAddr, DLIS).value_or(DILineInfo()));
771 }
772
773 // Check for 1 entry that has no file and line info
774 if (NumDwarfInlineInfos == 1 &&
775 DwarfInlineInfos.getFrame(0).FileName == "<invalid>") {
776 DwarfInlineInfos = DIInliningInfo();
777 NumDwarfInlineInfos = 0;
778 }
779 if (NumDwarfInlineInfos > 0 &&
780 NumDwarfInlineInfos != LR->Locations.size()) {
781 if (Out.GetOS()) {
782 raw_ostream &Log = *Out.GetOS();
783 Log << "error: address " << HEX64(Addr) << " has "
784 << NumDwarfInlineInfos << " DWARF inline frames and GSYM has "
785 << LR->Locations.size() << "\n";
786 Log << " " << NumDwarfInlineInfos << " DWARF frames:\n";
787 for (size_t Idx = 0; Idx < NumDwarfInlineInfos; ++Idx) {
788 const auto &dii = DwarfInlineInfos.getFrame(Idx);
789 Log << " [" << Idx << "]: " << dii.FunctionName << " @ "
790 << dii.FileName << ':' << dii.Line << '\n';
791 }
792 Log << " " << LR->Locations.size() << " GSYM frames:\n";
793 for (size_t Idx = 0, count = LR->Locations.size(); Idx < count;
794 ++Idx) {
795 const auto &gii = LR->Locations[Idx];
796 Log << " [" << Idx << "]: " << gii.Name << " @ " << gii.Dir
797 << '/' << gii.Base << ':' << gii.Line << '\n';
798 }
799 Gsym->dump(Log, *FI);
800 }
801 continue;
802 }
803
804 for (size_t Idx = 0, count = LR->Locations.size(); Idx < count;
805 ++Idx) {
806 const auto &gii = LR->Locations[Idx];
807 if (Idx < NumDwarfInlineInfos) {
808 const auto &dii = DwarfInlineInfos.getFrame(Idx);
809 gsymFilename = LR->getSourceFile(Idx);
810 // Verify function name
811 if (!StringRef(dii.FunctionName).starts_with(gii.Name))
812 Out << "error: address " << HEX64(Addr) << " DWARF function \""
813 << dii.FunctionName.c_str()
814 << "\" doesn't match GSYM function \"" << gii.Name << "\"\n";
815
816 // Verify source file path
817 if (dii.FileName != gsymFilename)
818 Out << "error: address " << HEX64(Addr) << " DWARF path \""
819 << dii.FileName.c_str() << "\" doesn't match GSYM path \""
820 << gsymFilename.c_str() << "\"\n";
821 // Verify source file line
822 if (dii.Line != gii.Line)
823 Out << "error: address " << HEX64(Addr) << " DWARF line "
824 << dii.Line << " != GSYM line " << gii.Line << "\n";
825 }
826 }
827 }
828 }
829 return Error::success();
830}
unsigned uint64_t
@ CallSiteInfo
@ InlineInfo
static void parseInlineInfo(GsymCreator &Gsym, OutputAggregator &Out, CUInfo &CUI, DWARFDie Die, uint32_t Depth, FunctionInfo &FI, InlineInfo &Parent, const AddressRanges &AllParentRanges, bool &WarnIfEmpty)
static bool hasInlineInfo(DWARFDie Die, uint32_t Depth)
static std::optional< gsym_strp_t > getQualifiedNameIndex(DWARFDie &Die, uint64_t Language, GsymCreator &Gsym)
Get the GsymCreator string table offset for the qualified name for the DIE passed in.
static AddressRanges ConvertDWARFRanges(const DWARFAddressRangesVector &DwarfRanges)
static DWARFDie GetParentDeclContextDIE(DWARFDie &Die)
static void convertFunctionLineTable(OutputAggregator &Out, CUInfo &CUI, DWARFDie Die, GsymCreator &Gsym, FunctionInfo &FI)
#define HEX64(v)
#define HEX32(v)
#define I(x, y, z)
Definition MD5.cpp:57
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
A class that represents an address range.
uint64_t start() const
bool contains(uint64_t Addr) const
bool contains(uint64_t Addr) const
The AddressRanges class helps normalize address range collections.
A format-neutral container for inlined code description.
Definition DIContext.h:94
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
const DWARFDebugLine::LineTable * getLineTableForUnit(DWARFUnit *U)
Get a pointer to a parsed line table corresponding to a compile unit.
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition DWARFDie.h:43
uint64_t getOffset() const
Get the absolute offset into the debug info or types section.
Definition DWARFDie.h:68
LLVM_ABI Expected< DWARFAddressRangesVector > getAddressRanges() const
Get the address ranges for this DIE.
Definition DWARFDie.cpp:454
iterator_range< iterator > children() const
Definition DWARFDie.h:407
LLVM_ABI DWARFDie getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE as the referenced DIE.
Definition DWARFDie.cpp:373
LLVM_ABI DWARFDie getParent() const
Get the parent of this DIE object.
Definition DWARFDie.cpp:736
LLVM_ABI std::optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
Definition DWARFDie.cpp:317
DWARFUnit * getDwarfUnit() const
Definition DWARFDie.h:55
LLVM_ABI std::optional< DWARFFormValue > findRecursively(ArrayRef< dwarf::Attribute > Attrs) const
Extract the first value of any attribute in Attrs from this DIE and recurse into any DW_AT_specificat...
Definition DWARFDie.cpp:341
LLVM_ABI const char * getName(DINameKind Kind) const
Return the DIE name resolving DW_AT_specification or DW_AT_abstract_origin references if necessary.
Definition DWARFDie.cpp:542
LLVM_ABI std::string getDeclFile(DILineInfoSpecifier::FileLineInfoKind Kind) const
Definition DWARFDie.cpp:574
dwarf::Tag getTag() const
Definition DWARFDie.h:73
LLVM_ABI const char * getLinkageName() const
Return the DIE linkage name resolving DW_AT_specification or DW_AT_abstract_origin references if nece...
Definition DWARFDie.cpp:560
LLVM_ABI void dump(raw_ostream &OS, unsigned indent=0, DIDumpOptions DumpOpts=DIDumpOptions()) const
Dump the DIE and all of its attributes to the supplied stream.
Definition DWARFDie.cpp:674
const dwarf::FormParams & getFormParams() const
Definition DWARFUnit.h:329
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition DWARFUnit.h:450
bool isDWOUnit() const
Definition DWARFUnit.h:325
This dwarf writer support class manages information associated with a source file.
Definition DwarfUnit.h:36
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
void wait() override
Blocking wait for all the tasks to execute first.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:597
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
char back() const
Get the last character in the string.
Definition StringRef.h:153
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
char front() const
Get the first character in the string.
Definition StringRef.h:147
auto async(Function &&F, Args &&...ArgList)
Asynchronous submission of a task to the pool.
Definition ThreadPool.h:80
LLVM_ABI llvm::Error convert(uint32_t NumThreads, OutputAggregator &OS)
Extract the DWARF from the supplied object file and convert it into the Gsym format in the GsymCreato...
LLVM_ABI llvm::Error verify(StringRef GsymPath, OutputAggregator &OS)
GsymCreator is used to emit GSYM data to a stand alone file or section within a file.
LLVM_ABI gsym_strp_t insertString(StringRef S, bool Copy=true)
Insert a string into the GSYM string table.
LLVM_ABI uint32_t insertFile(StringRef Path, sys::path::Style Style=sys::path::Style::native)
Insert a file into this GSYM creator.
static LLVM_ABI llvm::Expected< std::unique_ptr< GsymReader > > openFile(StringRef Path)
Construct a GsymReader from a file on disk.
LineTable class contains deserialized versions of line tables for each function's address ranges.
Definition LineTable.h:119
void Report(StringRef s, std::function< void(raw_ostream &o)> detailCallback)
raw_ostream * GetOS() const
void Merge(const OutputAggregator &other)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
#define UINT64_MAX
Definition DataTypes.h:77
std::optional< uint64_t > toAddress(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an address.
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
std::optional< uint64_t > toSectionOffset(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an section offset.
std::optional< uint64_t > toUnsigned(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
uint64_t gsym_strp_t
The type of string offset used in the code.
Definition GsymTypes.h:21
This is an optimization pass for GlobalISel generic memory operations.
ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount=0)
Returns a default thread strategy where all available hardware resources are to be used,...
Definition Threading.h:190
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition STLExtras.h:2012
SingleThreadExecutor DefaultThreadPool
Definition ThreadPool.h:262
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
std::vector< DWARFAddressRange > DWARFAddressRangesVector
DWARFAddressRangesVector - represents a set of absolute address ranges.
static DIDumpOptions getForSingleDIE()
Return default option set for printing a single DIE without children.
Definition DIContext.h:220
Controls which fields of DILineInfo container should be filled with data.
Definition DIContext.h:146
A format-neutral container for source line information.
Definition DIContext.h:32
LLVM_ABI bool lookupAddressRange(object::SectionedAddress Address, uint64_t Size, std::vector< uint32_t > &Result, std::optional< uint64_t > StmtSequenceOffset=std::nullopt) const
Fills the Result argument with the indices of the rows that correspond to the address range specified...
Standard .debug_line state machine structure.
object::SectionedAddress Address
The program-counter value corresponding to a machine instruction generated by the compiler and sectio...
uint64_t getDwarfMaxOffset() const
Definition Dwarf.h:1220
const DWARFDebugLine::LineTable * LineTable
std::optional< uint32_t > DWARFToGSYMFileIndex(GsymCreator &Gsym, uint32_t DwarfFileIdx)
Convert a DWARF compile unit file index into a GSYM global file index.
CUInfo(DWARFContext &DICtx, DWARFCompileUnit *CU)
bool isHighestAddress(uint64_t Addr) const
Return true if Addr is the highest address for a given compile unit.
std::vector< uint32_t > FileCache
std::vector< CallSiteInfo > CallSites
uint64_t ReturnOffset
The return offset of the call site - relative to the function start.
std::vector< gsym_strp_t > MatchRegex
Offsets into the string table for function names regex patterns.
Function information in GSYM files encodes information for one contiguous address range.
std::optional< InlineInfo > Inline
uint64_t startAddress() const
uint64_t endAddress() const
std::optional< CallSiteInfoCollection > CallSites
uint64_t size() const
gsym_strp_t Name
String table offset in the string table.
std::optional< LineTable > OptLineTable
Inline information stores the name of the inline function along with an array of address ranges.
Definition InlineInfo.h:61
std::vector< InlineInfo > Children
Definition InlineInfo.h:67
AddressRanges Ranges
Definition InlineInfo.h:66
Line entries are used to encode the line tables in FunctionInfo objects.
Definition LineEntry.h:22
static const uint64_t UndefSection
Definition ObjectFile.h:148