#include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <cinttypes>
#include <cstdint>
#include <cstring>
using namespace llvm;
void DWARFDebugArangeSet::Descriptor::dump(raw_ostream &OS,
uint32_t AddressSize) const {
OS << '[';
DWARFFormValue::dumpAddress(OS, AddressSize, Address);
OS << ", ";
DWARFFormValue::dumpAddress(OS, AddressSize, getEndAddress());
OS << ')';
}
void DWARFDebugArangeSet::clear() {
Offset = -1ULL;
std::memset(&HeaderData, 0, sizeof(Header));
ArangeDescriptors.clear();
}
Error DWARFDebugArangeSet::extract(DWARFDataExtractor data,
uint64_t *offset_ptr,
function_ref<void(Error)> WarningHandler) {
assert(data.isValidOffset(*offset_ptr));
ArangeDescriptors.clear();
Offset = *offset_ptr;
Error Err = Error::success();
std::tie(HeaderData.Length, HeaderData.Format) =
data.getInitialLength(offset_ptr, &Err);
HeaderData.Version = data.getU16(offset_ptr, &Err);
HeaderData.CuOffset = data.getUnsigned(
offset_ptr, dwarf::getDwarfOffsetByteSize(HeaderData.Format), &Err);
HeaderData.AddrSize = data.getU8(offset_ptr, &Err);
HeaderData.SegSize = data.getU8(offset_ptr, &Err);
if (Err) {
return createStringError(errc::invalid_argument,
"parsing address ranges table at offset 0x%" PRIx64
": %s",
Offset, toString(std::move(Err)).c_str());
}
uint64_t full_length =
dwarf::getUnitLengthFieldByteSize(HeaderData.Format) + HeaderData.Length;
if (!data.isValidOffsetForDataOfSize(Offset, full_length))
return createStringError(errc::invalid_argument,
"the length of address range table at offset "
"0x%" PRIx64 " exceeds section size",
Offset);
if (Error SizeErr = DWARFContext::checkAddressSizeSupported(
HeaderData.AddrSize, errc::invalid_argument,
"address range table at offset 0x%" PRIx64, Offset))
return SizeErr;
if (HeaderData.SegSize != 0)
return createStringError(errc::not_supported,
"non-zero segment selector size in address range "
"table at offset 0x%" PRIx64 " is not supported",
Offset);
const uint32_t tuple_size = HeaderData.AddrSize * 2;
if (full_length % tuple_size != 0)
return createStringError(
errc::invalid_argument,
"address range table at offset 0x%" PRIx64
" has length that is not a multiple of the tuple size",
Offset);
const uint32_t header_size = *offset_ptr - Offset;
uint32_t first_tuple_offset = 0;
while (first_tuple_offset < header_size)
first_tuple_offset += tuple_size;
if (full_length <= first_tuple_offset)
return createStringError(
errc::invalid_argument,
"address range table at offset 0x%" PRIx64
" has an insufficient length to contain any entries",
Offset);
*offset_ptr = Offset + first_tuple_offset;
Descriptor arangeDescriptor;
static_assert(sizeof(arangeDescriptor.Address) ==
sizeof(arangeDescriptor.Length),
"Different datatypes for addresses and sizes!");
assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize);
uint64_t end_offset = Offset + full_length;
while (*offset_ptr < end_offset) {
uint64_t EntryOffset = *offset_ptr;
arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
if (arangeDescriptor.Length == 0 && arangeDescriptor.Address == 0) {
if (*offset_ptr == end_offset)
return ErrorSuccess();
WarningHandler(createStringError(
errc::invalid_argument,
"address range table at offset 0x%" PRIx64
" has a premature terminator entry at offset 0x%" PRIx64,
Offset, EntryOffset));
}
ArangeDescriptors.push_back(arangeDescriptor);
}
return createStringError(errc::invalid_argument,
"address range table at offset 0x%" PRIx64
" is not terminated by null entry",
Offset);
}
void DWARFDebugArangeSet::dump(raw_ostream &OS) const {
int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(HeaderData.Format);
OS << "Address Range Header: "
<< format("length = 0x%0*" PRIx64 ", ", OffsetDumpWidth, HeaderData.Length)
<< "format = " << dwarf::FormatString(HeaderData.Format) << ", "
<< format("version = 0x%4.4x, ", HeaderData.Version)
<< format("cu_offset = 0x%0*" PRIx64 ", ", OffsetDumpWidth,
HeaderData.CuOffset)
<< format("addr_size = 0x%2.2x, ", HeaderData.AddrSize)
<< format("seg_size = 0x%2.2x\n", HeaderData.SegSize);
for (const auto &Desc : ArangeDescriptors) {
Desc.dump(OS, HeaderData.AddrSize);
OS << '\n';
}
}