1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
//===--- DWARFVisitor.cpp ---------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
//===----------------------------------------------------------------------===//
#include <unordered_map>
#include "DWARFVisitor.h"
#include "llvm/ObjectYAML/DWARFYAML.h"
using namespace llvm;
template <typename T>
void DWARFYAML::VisitorImpl<T>::onVariableSizeValue(uint64_t U, unsigned Size) {
switch (Size) {
case 8:
onValue((uint64_t)U);
break;
case 4:
onValue((uint32_t)U);
break;
case 2:
onValue((uint16_t)U);
break;
case 1:
onValue((uint8_t)U);
break;
default:
llvm_unreachable("Invalid integer write size.");
}
}
static unsigned getOffsetSize(const DWARFYAML::Unit &Unit) {
return Unit.Length.isDWARF64() ? 8 : 4;
}
static unsigned getRefSize(const DWARFYAML::Unit &Unit) {
if (Unit.Version == 2)
return Unit.AddrSize;
return getOffsetSize(Unit);
}
template <typename T> void DWARFYAML::VisitorImpl<T>::traverseDebugInfo() {
// XXX BINARYEN: Handle multiple linked compile units, each of which can
// refer to a different abbreviation list.
// TODO: This code appears to assume that abbreviation codes increment by 1
// so that lookups are linear. In LLVM output that is true, but it might not
// be in general.
// Create a map of [byte offset into the abbreviation section] => [index in
// DebugInfo.AbbrevDecls]. This avoids linear search for each CU.
std::unordered_map<size_t, size_t> abbrByteOffsetToDeclsIndex;
for (size_t i = 0; i < DebugInfo.AbbrevDecls.size(); i++) {
auto offset = DebugInfo.AbbrevDecls[i].ListOffset;
// The offset is the same for all entries for the same CU, so only note the
// first as that is where the list for the CU (that LLVM DeclSet) begins.
// That is, DebugInfo.AbbrevDecls looks like this:
//
// i CU Abbrev ListOffset
// ============================
// 0 X X1 150
// 1 X X2 150
// 2 X X3 150
// ..
// 6 Y Y1 260
// 7 Y Y2 260
//
// Note how multiple rows i have the same CU. All those abbrevs have the
// same ListOffset, which is the byte offset into the abbreviation section
// for that set of abbreviations.
if (abbrByteOffsetToDeclsIndex.count(offset)) {
continue;
}
abbrByteOffsetToDeclsIndex[offset] = i;
}
for (auto &Unit : DebugInfo.CompileUnits) {
// AbbrOffset is the byte offset into the abbreviation section, which we
// need to find among the Abbrev's ListOffsets (which are the byte offsets
// of where that abbreviation list begins).
// TODO: Optimize this to not be O(#CUs * #abbrevs).
auto offset = Unit.AbbrOffset;
assert(abbrByteOffsetToDeclsIndex.count(offset));
size_t AbbrevStart = abbrByteOffsetToDeclsIndex[offset];
assert(DebugInfo.AbbrevDecls[AbbrevStart].ListOffset == offset);
// Find the last entry in this abbreviation list.
size_t AbbrevEnd = AbbrevStart;
while (AbbrevEnd < DebugInfo.AbbrevDecls.size() &&
DebugInfo.AbbrevDecls[AbbrevEnd].Code) {
AbbrevEnd++;
}
// XXX BINARYEN If there are no abbreviations, there is nothing to
// do in this unit.
if (AbbrevStart == AbbrevEnd) {
continue;
}
onStartCompileUnit(Unit);
if (Unit.Entries.empty()) { // XXX BINARYEN
continue;
}
for (auto &Entry : Unit.Entries) {
onStartDIE(Unit, Entry);
if (Entry.AbbrCode == 0u)
continue;
// XXX BINARYEN valid abbreviation codes start from 1, so subtract that,
// and are relative to the start of the abbrev table
auto RelativeAbbrIndex = Entry.AbbrCode - 1 + AbbrevStart;
if (RelativeAbbrIndex >= AbbrevEnd) {
errs() << "warning: invalid abbreviation code " << Entry.AbbrCode
<< " (range: " << AbbrevStart << ".." << AbbrevEnd << ")\n";
continue;
}
auto &Abbrev = DebugInfo.AbbrevDecls[RelativeAbbrIndex];
auto FormVal = Entry.Values.begin();
auto AbbrForm = Abbrev.Attributes.begin();
for (;
FormVal != Entry.Values.end() && AbbrForm != Abbrev.Attributes.end();
++FormVal, ++AbbrForm) {
onForm(*AbbrForm, *FormVal);
dwarf::Form Form = AbbrForm->Form;
bool Indirect;
do {
Indirect = false;
switch (Form) {
case dwarf::DW_FORM_addr:
onVariableSizeValue(FormVal->Value, Unit.AddrSize);
break;
case dwarf::DW_FORM_ref_addr:
onVariableSizeValue(FormVal->Value, getRefSize(Unit));
break;
case dwarf::DW_FORM_exprloc:
case dwarf::DW_FORM_block:
onValue((uint64_t)FormVal->BlockData.size(), true);
onValue(
MemoryBufferRef(StringRef((const char *)FormVal->BlockData.data(),
FormVal->BlockData.size()),
""));
break;
case dwarf::DW_FORM_block1: {
auto writeSize = FormVal->BlockData.size();
onValue((uint8_t)writeSize);
onValue(
MemoryBufferRef(StringRef((const char *)FormVal->BlockData.data(),
FormVal->BlockData.size()),
""));
break;
}
case dwarf::DW_FORM_block2: {
auto writeSize = FormVal->BlockData.size();
onValue((uint16_t)writeSize);
onValue(
MemoryBufferRef(StringRef((const char *)FormVal->BlockData.data(),
FormVal->BlockData.size()),
""));
break;
}
case dwarf::DW_FORM_block4: {
auto writeSize = FormVal->BlockData.size();
onValue((uint32_t)writeSize);
onValue(
MemoryBufferRef(StringRef((const char *)FormVal->BlockData.data(),
FormVal->BlockData.size()),
""));
break;
}
case dwarf::DW_FORM_data1:
case dwarf::DW_FORM_ref1:
case dwarf::DW_FORM_flag:
case dwarf::DW_FORM_strx1:
case dwarf::DW_FORM_addrx1:
onValue((uint8_t)FormVal->Value);
break;
case dwarf::DW_FORM_data2:
case dwarf::DW_FORM_ref2:
case dwarf::DW_FORM_strx2:
case dwarf::DW_FORM_addrx2:
onValue((uint16_t)FormVal->Value);
break;
case dwarf::DW_FORM_data4:
case dwarf::DW_FORM_ref4:
case dwarf::DW_FORM_ref_sup4:
case dwarf::DW_FORM_strx4:
case dwarf::DW_FORM_addrx4:
onValue((uint32_t)FormVal->Value);
break;
case dwarf::DW_FORM_data8:
case dwarf::DW_FORM_ref8:
case dwarf::DW_FORM_ref_sup8:
onValue((uint64_t)FormVal->Value);
break;
case dwarf::DW_FORM_sdata:
onValue((int64_t)FormVal->Value, true);
break;
case dwarf::DW_FORM_udata:
case dwarf::DW_FORM_ref_udata:
onValue((uint64_t)FormVal->Value, true);
break;
case dwarf::DW_FORM_string:
onValue(FormVal->CStr);
break;
case dwarf::DW_FORM_indirect:
onValue((uint64_t)FormVal->Value, true);
Indirect = true;
Form = static_cast<dwarf::Form>((uint64_t)FormVal->Value);
++FormVal;
break;
case dwarf::DW_FORM_strp:
case dwarf::DW_FORM_sec_offset:
case dwarf::DW_FORM_GNU_ref_alt:
case dwarf::DW_FORM_GNU_strp_alt:
case dwarf::DW_FORM_line_strp:
case dwarf::DW_FORM_strp_sup:
onVariableSizeValue(FormVal->Value, getOffsetSize(Unit));
break;
case dwarf::DW_FORM_ref_sig8:
onValue((uint64_t)FormVal->Value);
break;
case dwarf::DW_FORM_GNU_addr_index:
case dwarf::DW_FORM_GNU_str_index:
onValue((uint64_t)FormVal->Value, true);
break;
default:
break;
}
} while (Indirect);
}
onEndDIE(Unit, Entry);
}
onEndCompileUnit(Unit);
}
}
// Explicitly instantiate the two template expansions.
template class DWARFYAML::VisitorImpl<DWARFYAML::Data>;
template class DWARFYAML::VisitorImpl<const DWARFYAML::Data>;
|