#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
#include "llvm/Support/DataExtractor.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include <cstddef>
#include <cstdint>
using namespace llvm;
using namespace dwarf;
void DWARFAbbreviationDeclaration::clear() {
Code = 0;
Tag = DW_TAG_null;
CodeByteSize = 0;
HasChildren = false;
AttributeSpecs.clear();
FixedAttributeSize.reset();
}
DWARFAbbreviationDeclaration::DWARFAbbreviationDeclaration() {
clear();
}
bool
DWARFAbbreviationDeclaration::extract(DataExtractor Data,
uint64_t* OffsetPtr) {
clear();
const uint64_t Offset = *OffsetPtr;
Code = Data.getULEB128(OffsetPtr);
if (Code == 0) {
return false;
}
CodeByteSize = *OffsetPtr - Offset;
Tag = static_cast<llvm::dwarf::Tag>(Data.getULEB128(OffsetPtr));
if (Tag == DW_TAG_null) {
clear();
return false;
}
uint8_t ChildrenByte = Data.getU8(OffsetPtr);
HasChildren = (ChildrenByte == DW_CHILDREN_yes);
FixedAttributeSize = FixedSizeInfo();
while (true) {
auto A = static_cast<Attribute>(Data.getULEB128(OffsetPtr));
auto F = static_cast<Form>(Data.getULEB128(OffsetPtr));
if (A && F) {
bool IsImplicitConst = (F == DW_FORM_implicit_const);
if (IsImplicitConst) {
int64_t V = Data.getSLEB128(OffsetPtr);
AttributeSpecs.push_back(AttributeSpec(A, F, V));
continue;
}
Optional<uint8_t> ByteSize;
switch (F) {
case DW_FORM_addr:
if (FixedAttributeSize)
++FixedAttributeSize->NumAddrs;
break;
case DW_FORM_ref_addr:
if (FixedAttributeSize)
++FixedAttributeSize->NumRefAddrs;
break;
case DW_FORM_strp:
case DW_FORM_GNU_ref_alt:
case DW_FORM_GNU_strp_alt:
case DW_FORM_line_strp:
case DW_FORM_sec_offset:
case DW_FORM_strp_sup:
if (FixedAttributeSize)
++FixedAttributeSize->NumDwarfOffsets;
break;
default:
if ((ByteSize = dwarf::getFixedFormByteSize(F, dwarf::FormParams()))) {
if (FixedAttributeSize)
FixedAttributeSize->NumBytes += *ByteSize;
break;
}
FixedAttributeSize.reset();
break;
}
AttributeSpecs.push_back(AttributeSpec(A, F, ByteSize));
} else if (A == 0 && F == 0) {
break;
} else {
clear();
return false;
}
}
return true;
}
void DWARFAbbreviationDeclaration::dump(raw_ostream &OS) const {
OS << '[' << getCode() << "] ";
OS << formatv("{0}", getTag());
OS << "\tDW_CHILDREN_" << (hasChildren() ? "yes" : "no") << '\n';
for (const AttributeSpec &Spec : AttributeSpecs) {
OS << formatv("\t{0}\t{1}", Spec.Attr, Spec.Form);
if (Spec.isImplicitConst())
OS << '\t' << Spec.getImplicitConstValue();
OS << '\n';
}
OS << '\n';
}
Optional<uint32_t>
DWARFAbbreviationDeclaration::findAttributeIndex(dwarf::Attribute Attr) const {
for (uint32_t i = 0, e = AttributeSpecs.size(); i != e; ++i) {
if (AttributeSpecs[i].Attr == Attr)
return i;
}
return None;
}
uint64_t DWARFAbbreviationDeclaration::getAttributeOffsetFromIndex(
uint32_t AttrIndex, uint64_t DIEOffset, const DWARFUnit &U) const {
DWARFDataExtractor DebugInfoData = U.getDebugInfoExtractor();
uint64_t Offset = DIEOffset + CodeByteSize;
for (uint32_t CurAttrIdx = 0; CurAttrIdx != AttrIndex; ++CurAttrIdx)
if (auto FixedSize = AttributeSpecs[CurAttrIdx].getByteSize(U))
Offset += *FixedSize;
else
DWARFFormValue::skipValue(AttributeSpecs[CurAttrIdx].Form, DebugInfoData,
&Offset, U.getFormParams());
return Offset;
}
Optional<DWARFFormValue>
DWARFAbbreviationDeclaration::getAttributeValueFromOffset(
uint32_t AttrIndex, uint64_t Offset, const DWARFUnit &U) const {
assert(AttributeSpecs.size() > AttrIndex &&
"Attribute Index is out of bounds.");
const AttributeSpec &Spec = AttributeSpecs[AttrIndex];
if (Spec.isImplicitConst())
return DWARFFormValue::createFromSValue(Spec.Form,
Spec.getImplicitConstValue());
DWARFFormValue FormValue(Spec.Form);
DWARFDataExtractor DebugInfoData = U.getDebugInfoExtractor();
if (FormValue.extractValue(DebugInfoData, &Offset, U.getFormParams(), &U))
return FormValue;
return None;
}
Optional<DWARFFormValue>
DWARFAbbreviationDeclaration::getAttributeValue(const uint64_t DIEOffset,
const dwarf::Attribute Attr,
const DWARFUnit &U) const {
Optional<uint32_t> MatchAttrIndex = findAttributeIndex(Attr);
if (!MatchAttrIndex)
return None;
uint64_t Offset = getAttributeOffsetFromIndex(*MatchAttrIndex, DIEOffset, U);
return getAttributeValueFromOffset(*MatchAttrIndex, Offset, U);
}
size_t DWARFAbbreviationDeclaration::FixedSizeInfo::getByteSize(
const DWARFUnit &U) const {
size_t ByteSize = NumBytes;
if (NumAddrs)
ByteSize += NumAddrs * U.getAddressByteSize();
if (NumRefAddrs)
ByteSize += NumRefAddrs * U.getRefAddrByteSize();
if (NumDwarfOffsets)
ByteSize += NumDwarfOffsets * U.getDwarfOffsetByteSize();
return ByteSize;
}
Optional<int64_t> DWARFAbbreviationDeclaration::AttributeSpec::getByteSize(
const DWARFUnit &U) const {
if (isImplicitConst())
return 0;
if (ByteSize.HasByteSize)
return ByteSize.ByteSize;
Optional<int64_t> S;
auto FixedByteSize = dwarf::getFixedFormByteSize(Form, U.getFormParams());
if (FixedByteSize)
S = *FixedByteSize;
return S;
}
Optional<size_t> DWARFAbbreviationDeclaration::getFixedAttributesByteSize(
const DWARFUnit &U) const {
if (FixedAttributeSize)
return FixedAttributeSize->getByteSize(U);
return None;
}