Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 1 | //===- DWARFAcceleratorTable.cpp ------------------------------------------===// |
Frederic Riss | 7c41c64 | 2014-11-20 16:21:06 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame^] | 11 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallVector.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame^] | 13 | #include "llvm/BinaryFormat/Dwarf.h" |
George Rimar | f8a9642 | 2017-04-21 09:12:18 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
| 16 | #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Compiler.h" |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Format.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 20 | #include <cstddef> |
| 21 | #include <cstdint> |
| 22 | #include <utility> |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 23 | |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 25 | |
| 26 | bool DWARFAcceleratorTable::extract() { |
| 27 | uint32_t Offset = 0; |
| 28 | |
| 29 | // Check that we can at least read the header. |
| 30 | if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength)+4)) |
| 31 | return false; |
| 32 | |
| 33 | Hdr.Magic = AccelSection.getU32(&Offset); |
| 34 | Hdr.Version = AccelSection.getU16(&Offset); |
| 35 | Hdr.HashFunction = AccelSection.getU16(&Offset); |
| 36 | Hdr.NumBuckets = AccelSection.getU32(&Offset); |
| 37 | Hdr.NumHashes = AccelSection.getU32(&Offset); |
| 38 | Hdr.HeaderDataLength = AccelSection.getU32(&Offset); |
| 39 | |
| 40 | // Check that we can read all the hashes and offsets from the |
| 41 | // section (see SourceLevelDebugging.rst for the structure of the index). |
| 42 | if (!AccelSection.isValidOffset(sizeof(Hdr) + Hdr.HeaderDataLength + |
| 43 | Hdr.NumBuckets*4 + Hdr.NumHashes*8)) |
| 44 | return false; |
| 45 | |
| 46 | HdrData.DIEOffsetBase = AccelSection.getU32(&Offset); |
| 47 | uint32_t NumAtoms = AccelSection.getU32(&Offset); |
| 48 | |
| 49 | for (unsigned i = 0; i < NumAtoms; ++i) { |
| 50 | uint16_t AtomType = AccelSection.getU16(&Offset); |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 51 | auto AtomForm = static_cast<dwarf::Form>(AccelSection.getU16(&Offset)); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 52 | HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm)); |
| 53 | } |
| 54 | |
| 55 | return true; |
| 56 | } |
| 57 | |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 58 | LLVM_DUMP_METHOD void DWARFAcceleratorTable::dump(raw_ostream &OS) const { |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 59 | // Dump the header. |
| 60 | OS << "Magic = " << format("0x%08x", Hdr.Magic) << '\n' |
| 61 | << "Version = " << format("0x%04x", Hdr.Version) << '\n' |
| 62 | << "Hash function = " << format("0x%08x", Hdr.HashFunction) << '\n' |
| 63 | << "Bucket count = " << Hdr.NumBuckets << '\n' |
| 64 | << "Hashes count = " << Hdr.NumHashes << '\n' |
| 65 | << "HeaderData length = " << Hdr.HeaderDataLength << '\n' |
| 66 | << "DIE offset base = " << HdrData.DIEOffsetBase << '\n' |
| 67 | << "Number of atoms = " << HdrData.Atoms.size() << '\n'; |
| 68 | |
| 69 | unsigned i = 0; |
Frederic Riss | 77a0743 | 2014-11-20 16:21:11 +0000 | [diff] [blame] | 70 | SmallVector<DWARFFormValue, 3> AtomForms; |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 71 | for (const auto &Atom: HdrData.Atoms) { |
| 72 | OS << format("Atom[%d] Type: ", i++); |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 73 | auto TypeString = dwarf::AtomTypeString(Atom.first); |
| 74 | if (!TypeString.empty()) |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 75 | OS << TypeString; |
| 76 | else |
| 77 | OS << format("DW_ATOM_Unknown_0x%x", Atom.first); |
| 78 | OS << " Form: "; |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 79 | auto FormString = dwarf::FormEncodingString(Atom.second); |
| 80 | if (!FormString.empty()) |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 81 | OS << FormString; |
| 82 | else |
Frederic Riss | 77a0743 | 2014-11-20 16:21:11 +0000 | [diff] [blame] | 83 | OS << format("DW_FORM_Unknown_0x%x", Atom.second); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 84 | OS << '\n'; |
Frederic Riss | 77a0743 | 2014-11-20 16:21:11 +0000 | [diff] [blame] | 85 | AtomForms.push_back(DWARFFormValue(Atom.second)); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // Now go through the actual tables and dump them. |
| 89 | uint32_t Offset = sizeof(Hdr) + Hdr.HeaderDataLength; |
| 90 | unsigned HashesBase = Offset + Hdr.NumBuckets * 4; |
| 91 | unsigned OffsetsBase = HashesBase + Hdr.NumHashes * 4; |
| 92 | |
| 93 | for (unsigned Bucket = 0; Bucket < Hdr.NumBuckets; ++Bucket) { |
| 94 | unsigned Index = AccelSection.getU32(&Offset); |
| 95 | |
| 96 | OS << format("Bucket[%d]\n", Bucket); |
| 97 | if (Index == UINT32_MAX) { |
| 98 | OS << " EMPTY\n"; |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | for (unsigned HashIdx = Index; HashIdx < Hdr.NumHashes; ++HashIdx) { |
| 103 | unsigned HashOffset = HashesBase + HashIdx*4; |
| 104 | unsigned OffsetsOffset = OffsetsBase + HashIdx*4; |
| 105 | uint32_t Hash = AccelSection.getU32(&HashOffset); |
| 106 | |
| 107 | if (Hash % Hdr.NumBuckets != Bucket) |
| 108 | break; |
| 109 | |
| 110 | unsigned DataOffset = AccelSection.getU32(&OffsetsOffset); |
| 111 | OS << format(" Hash = 0x%08x Offset = 0x%08x\n", Hash, DataOffset); |
| 112 | if (!AccelSection.isValidOffset(DataOffset)) { |
| 113 | OS << " Invalid section offset\n"; |
| 114 | continue; |
| 115 | } |
Frederic Riss | 7c50047 | 2014-11-14 19:30:08 +0000 | [diff] [blame] | 116 | while (AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) { |
George Rimar | f8a9642 | 2017-04-21 09:12:18 +0000 | [diff] [blame] | 117 | unsigned StringOffset = |
| 118 | getRelocatedValue(AccelSection, 4, &DataOffset, &Relocs); |
Frederic Riss | 7c50047 | 2014-11-14 19:30:08 +0000 | [diff] [blame] | 119 | if (!StringOffset) |
| 120 | break; |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 121 | OS << format(" Name: %08x \"%s\"\n", StringOffset, |
| 122 | StringSection.getCStr(&StringOffset)); |
| 123 | unsigned NumData = AccelSection.getU32(&DataOffset); |
| 124 | for (unsigned Data = 0; Data < NumData; ++Data) { |
| 125 | OS << format(" Data[%d] => ", Data); |
| 126 | unsigned i = 0; |
Frederic Riss | 77a0743 | 2014-11-20 16:21:11 +0000 | [diff] [blame] | 127 | for (auto &Atom : AtomForms) { |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 128 | OS << format("{Atom[%d]: ", i++); |
Frederic Riss | 77a0743 | 2014-11-20 16:21:11 +0000 | [diff] [blame] | 129 | if (Atom.extractValue(AccelSection, &DataOffset, nullptr)) |
Greg Clayton | cddab27 | 2016-10-31 16:46:02 +0000 | [diff] [blame] | 130 | Atom.dump(OS); |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 131 | else |
| 132 | OS << "Error extracting the value"; |
| 133 | OS << "} "; |
| 134 | } |
| 135 | OS << '\n'; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |