2017-02-28 07:43:14 +08:00
|
|
|
//===- DWARFAbbreviationDeclaration.cpp -----------------------------------===//
|
2011-09-14 03:42:23 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
|
2017-02-28 07:43:14 +08:00
|
|
|
#include "llvm/ADT/None.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/Dwarf.h"
|
2016-11-15 09:23:06 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
|
2017-02-28 07:43:14 +08:00
|
|
|
#include "llvm/Support/DataExtractor.h"
|
2011-09-15 12:15:59 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
[dwarf] Unify unknown dwarf enum formatting code
Summary:
We have had at least three pieces of code (in DWARFAbbreviationDeclaration,
DWARFAcceleratorTable and DWARFDie) that have hand-rolled support for
dumping unknown dwarf enum values. While not terrible, they are a bit
distracting and enable small differences to creep in (Unknown_ffff vs.
Unknown_0xffff). I ended up needing to add a fourth place
(DWARFVerifier), so it seems it would be a good time to centralize.
This patch creates an alternative to the XXXString dumping functions in
the BinaryFormat library, which formats an unknown value as
DW_TYPE_unknown_1234, instead of just an empty string. It is based on
the formatv function, as that allows us to avoid materializing the
string for unknown values (and because this way I don't have to invent a
name for the new functions :P).
In this patch I add formatters for dwarf attributes, forms, tags, and
index attributes as these are the ones in use currently, but adding
other enums is straight-forward.
Reviewers: dblaikie, JDevlieghere, aprantl
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D44570
llvm-svn: 328090
2018-03-21 19:46:37 +08:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2011-09-14 03:42:23 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-02-28 07:43:14 +08:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
|
2011-09-14 03:42:23 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace dwarf;
|
|
|
|
|
2013-11-01 01:20:14 +08:00
|
|
|
void DWARFAbbreviationDeclaration::clear() {
|
|
|
|
Code = 0;
|
2016-10-28 00:32:04 +08:00
|
|
|
Tag = DW_TAG_null;
|
2016-11-15 09:23:06 +08:00
|
|
|
CodeByteSize = 0;
|
2013-11-01 01:20:14 +08:00
|
|
|
HasChildren = false;
|
2014-03-13 15:52:54 +08:00
|
|
|
AttributeSpecs.clear();
|
2016-11-15 09:23:06 +08:00
|
|
|
FixedAttributeSize.reset();
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|
|
|
|
|
2013-11-01 01:20:14 +08:00
|
|
|
DWARFAbbreviationDeclaration::DWARFAbbreviationDeclaration() {
|
|
|
|
clear();
|
|
|
|
}
|
2011-09-14 03:42:23 +08:00
|
|
|
|
2013-11-01 01:20:14 +08:00
|
|
|
bool
|
2018-07-31 03:41:25 +08:00
|
|
|
DWARFAbbreviationDeclaration::extract(DataExtractor Data,
|
2016-10-28 00:32:04 +08:00
|
|
|
uint32_t* OffsetPtr) {
|
2013-11-01 01:20:14 +08:00
|
|
|
clear();
|
2016-11-15 09:23:06 +08:00
|
|
|
const uint32_t Offset = *OffsetPtr;
|
2013-11-01 01:20:14 +08:00
|
|
|
Code = Data.getULEB128(OffsetPtr);
|
|
|
|
if (Code == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-15 09:23:06 +08:00
|
|
|
CodeByteSize = *OffsetPtr - Offset;
|
2016-10-28 00:32:04 +08:00
|
|
|
Tag = static_cast<llvm::dwarf::Tag>(Data.getULEB128(OffsetPtr));
|
|
|
|
if (Tag == DW_TAG_null) {
|
|
|
|
clear();
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-01 01:20:14 +08:00
|
|
|
uint8_t ChildrenByte = Data.getU8(OffsetPtr);
|
|
|
|
HasChildren = (ChildrenByte == DW_CHILDREN_yes);
|
2016-11-15 09:23:06 +08:00
|
|
|
// Assign a value to our optional FixedAttributeSize member variable. If
|
|
|
|
// this member variable still has a value after the while loop below, then
|
|
|
|
// all attribute data in this abbreviation declaration has a fixed byte size.
|
|
|
|
FixedAttributeSize = FixedSizeInfo();
|
2011-09-14 03:42:23 +08:00
|
|
|
|
2016-11-15 09:23:06 +08:00
|
|
|
// Read all of the abbreviation attributes and forms.
|
2013-11-01 01:20:14 +08:00
|
|
|
while (true) {
|
2016-10-28 00:32:04 +08:00
|
|
|
auto A = static_cast<Attribute>(Data.getULEB128(OffsetPtr));
|
|
|
|
auto F = static_cast<Form>(Data.getULEB128(OffsetPtr));
|
|
|
|
if (A && F) {
|
2017-01-11 05:18:26 +08:00
|
|
|
bool IsImplicitConst = (F == DW_FORM_implicit_const);
|
2017-06-27 02:43:01 +08:00
|
|
|
if (IsImplicitConst) {
|
2017-09-21 23:27:45 +08:00
|
|
|
int64_t V = Data.getSLEB128(OffsetPtr);
|
2017-06-27 02:43:01 +08:00
|
|
|
AttributeSpecs.push_back(AttributeSpec(A, F, V));
|
2017-01-11 05:18:26 +08:00
|
|
|
continue;
|
2017-06-27 02:43:01 +08:00
|
|
|
}
|
2017-09-21 23:27:45 +08:00
|
|
|
Optional<uint8_t> ByteSize;
|
2016-11-15 09:23:06 +08:00
|
|
|
// If this abbrevation still has a fixed byte size, then update the
|
|
|
|
// FixedAttributeSize as needed.
|
2017-06-27 02:43:01 +08:00
|
|
|
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:
|
|
|
|
// The form has a byte size that doesn't depend on Params.
|
|
|
|
// If it's a fixed size, keep track of it.
|
2018-03-14 17:39:54 +08:00
|
|
|
if ((ByteSize = dwarf::getFixedFormByteSize(F, dwarf::FormParams()))) {
|
2017-06-27 02:43:01 +08:00
|
|
|
if (FixedAttributeSize)
|
2017-09-21 23:27:45 +08:00
|
|
|
FixedAttributeSize->NumBytes += *ByteSize;
|
2017-06-27 02:43:01 +08:00
|
|
|
break;
|
2016-11-15 09:23:06 +08:00
|
|
|
}
|
2017-06-27 02:43:01 +08:00
|
|
|
// Indicate we no longer have a fixed byte size for this
|
|
|
|
// abbreviation by clearing the FixedAttributeSize optional value
|
|
|
|
// so it doesn't have a value.
|
|
|
|
FixedAttributeSize.reset();
|
|
|
|
break;
|
2016-11-15 09:23:06 +08:00
|
|
|
}
|
2017-06-27 02:43:01 +08:00
|
|
|
// Record this attribute and its fixed size if it has one.
|
2017-09-21 23:27:45 +08:00
|
|
|
AttributeSpecs.push_back(AttributeSpec(A, F, ByteSize));
|
2016-10-28 00:32:04 +08:00
|
|
|
} else if (A == 0 && F == 0) {
|
|
|
|
// We successfully reached the end of this abbreviation declaration
|
|
|
|
// since both attribute and form are zero.
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// Attribute and form pairs must either both be non-zero, in which case
|
|
|
|
// they are added to the abbreviation declaration, or both be zero to
|
|
|
|
// terminate the abbrevation declaration. In this case only one was
|
|
|
|
// zero which is an error.
|
2013-11-01 01:20:14 +08:00
|
|
|
clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DWARFAbbreviationDeclaration::dump(raw_ostream &OS) const {
|
2011-09-15 12:15:59 +08:00
|
|
|
OS << '[' << getCode() << "] ";
|
[dwarf] Unify unknown dwarf enum formatting code
Summary:
We have had at least three pieces of code (in DWARFAbbreviationDeclaration,
DWARFAcceleratorTable and DWARFDie) that have hand-rolled support for
dumping unknown dwarf enum values. While not terrible, they are a bit
distracting and enable small differences to creep in (Unknown_ffff vs.
Unknown_0xffff). I ended up needing to add a fourth place
(DWARFVerifier), so it seems it would be a good time to centralize.
This patch creates an alternative to the XXXString dumping functions in
the BinaryFormat library, which formats an unknown value as
DW_TYPE_unknown_1234, instead of just an empty string. It is based on
the formatv function, as that allows us to avoid materializing the
string for unknown values (and because this way I don't have to invent a
name for the new functions :P).
In this patch I add formatters for dwarf attributes, forms, tags, and
index attributes as these are the ones in use currently, but adding
other enums is straight-forward.
Reviewers: dblaikie, JDevlieghere, aprantl
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D44570
llvm-svn: 328090
2018-03-21 19:46:37 +08:00
|
|
|
OS << formatv("{0}", getTag());
|
2011-09-15 12:15:59 +08:00
|
|
|
OS << "\tDW_CHILDREN_" << (hasChildren() ? "yes" : "no") << '\n';
|
2014-03-13 15:52:54 +08:00
|
|
|
for (const AttributeSpec &Spec : AttributeSpecs) {
|
[dwarf] Unify unknown dwarf enum formatting code
Summary:
We have had at least three pieces of code (in DWARFAbbreviationDeclaration,
DWARFAcceleratorTable and DWARFDie) that have hand-rolled support for
dumping unknown dwarf enum values. While not terrible, they are a bit
distracting and enable small differences to creep in (Unknown_ffff vs.
Unknown_0xffff). I ended up needing to add a fourth place
(DWARFVerifier), so it seems it would be a good time to centralize.
This patch creates an alternative to the XXXString dumping functions in
the BinaryFormat library, which formats an unknown value as
DW_TYPE_unknown_1234, instead of just an empty string. It is based on
the formatv function, as that allows us to avoid materializing the
string for unknown values (and because this way I don't have to invent a
name for the new functions :P).
In this patch I add formatters for dwarf attributes, forms, tags, and
index attributes as these are the ones in use currently, but adding
other enums is straight-forward.
Reviewers: dblaikie, JDevlieghere, aprantl
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D44570
llvm-svn: 328090
2018-03-21 19:46:37 +08:00
|
|
|
OS << formatv("\t{0}\t{1}", Spec.Attr, Spec.Form);
|
2017-01-11 05:18:26 +08:00
|
|
|
if (Spec.isImplicitConst())
|
2017-09-21 23:27:45 +08:00
|
|
|
OS << '\t' << Spec.getImplicitConstValue();
|
2011-09-15 12:15:59 +08:00
|
|
|
OS << '\n';
|
2011-09-15 12:00:58 +08:00
|
|
|
}
|
2011-09-14 03:42:23 +08:00
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2016-11-15 09:23:06 +08:00
|
|
|
Optional<uint32_t>
|
|
|
|
DWARFAbbreviationDeclaration::findAttributeIndex(dwarf::Attribute Attr) const {
|
2014-03-13 15:52:54 +08:00
|
|
|
for (uint32_t i = 0, e = AttributeSpecs.size(); i != e; ++i) {
|
2016-11-15 09:23:06 +08:00
|
|
|
if (AttributeSpecs[i].Attr == Attr)
|
2011-09-14 03:42:23 +08:00
|
|
|
return i;
|
|
|
|
}
|
2016-11-15 09:23:06 +08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2016-12-14 07:20:56 +08:00
|
|
|
Optional<DWARFFormValue> DWARFAbbreviationDeclaration::getAttributeValue(
|
|
|
|
const uint32_t DIEOffset, const dwarf::Attribute Attr,
|
|
|
|
const DWARFUnit &U) const {
|
2016-11-15 09:23:06 +08:00
|
|
|
Optional<uint32_t> MatchAttrIndex = findAttributeIndex(Attr);
|
|
|
|
if (!MatchAttrIndex)
|
2016-12-14 07:20:56 +08:00
|
|
|
return None;
|
2016-11-15 09:23:06 +08:00
|
|
|
|
|
|
|
auto DebugInfoData = U.getDebugInfoExtractor();
|
|
|
|
|
|
|
|
// Add the byte size of ULEB that for the abbrev Code so we can start
|
|
|
|
// skipping the attribute data.
|
|
|
|
uint32_t Offset = DIEOffset + CodeByteSize;
|
|
|
|
uint32_t AttrIndex = 0;
|
|
|
|
for (const auto &Spec : AttributeSpecs) {
|
|
|
|
if (*MatchAttrIndex == AttrIndex) {
|
|
|
|
// We have arrived at the attribute to extract, extract if from Offset.
|
2016-12-14 07:20:56 +08:00
|
|
|
DWARFFormValue FormValue(Spec.Form);
|
2017-01-11 05:18:26 +08:00
|
|
|
if (Spec.isImplicitConst()) {
|
2017-09-21 23:27:45 +08:00
|
|
|
FormValue.setSValue(Spec.getImplicitConstValue());
|
2017-01-11 05:18:26 +08:00
|
|
|
return FormValue;
|
|
|
|
}
|
2017-11-08 03:57:12 +08:00
|
|
|
if (FormValue.extractValue(DebugInfoData, &Offset, U.getFormParams(), &U))
|
2016-12-14 07:20:56 +08:00
|
|
|
return FormValue;
|
2016-11-15 09:23:06 +08:00
|
|
|
}
|
|
|
|
// March Offset along until we get to the attribute we want.
|
2017-01-11 05:18:26 +08:00
|
|
|
if (auto FixedSize = Spec.getByteSize(U))
|
2016-11-15 09:23:06 +08:00
|
|
|
Offset += *FixedSize;
|
|
|
|
else
|
2017-06-27 02:43:01 +08:00
|
|
|
DWARFFormValue::skipValue(Spec.Form, DebugInfoData, &Offset,
|
|
|
|
U.getFormParams());
|
2016-11-15 09:23:06 +08:00
|
|
|
++AttrIndex;
|
|
|
|
}
|
2016-12-14 07:20:56 +08:00
|
|
|
return None;
|
2016-11-15 09:23:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-11 05:18:26 +08:00
|
|
|
Optional<int64_t> DWARFAbbreviationDeclaration::AttributeSpec::getByteSize(
|
2016-11-15 09:23:06 +08:00
|
|
|
const DWARFUnit &U) const {
|
2017-01-11 05:18:26 +08:00
|
|
|
if (isImplicitConst())
|
|
|
|
return 0;
|
2017-11-01 03:55:08 +08:00
|
|
|
if (ByteSize.HasByteSize)
|
|
|
|
return ByteSize.ByteSize;
|
2017-01-11 05:18:26 +08:00
|
|
|
Optional<int64_t> S;
|
2018-03-14 17:39:54 +08:00
|
|
|
auto FixedByteSize = dwarf::getFixedFormByteSize(Form, U.getFormParams());
|
2017-01-11 05:18:26 +08:00
|
|
|
if (FixedByteSize)
|
|
|
|
S = *FixedByteSize;
|
|
|
|
return S;
|
2016-11-15 09:23:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Optional<size_t> DWARFAbbreviationDeclaration::getFixedAttributesByteSize(
|
|
|
|
const DWARFUnit &U) const {
|
|
|
|
if (FixedAttributeSize)
|
|
|
|
return FixedAttributeSize->getByteSize(U);
|
|
|
|
return None;
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|