2017-01-12 22:35:21 +01:00
|
|
|
//===- DWARFEmitter - Convert YAML to DWARF binary data -------------------===//
|
2016-12-07 23:30:15 +01:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// 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
|
2016-12-07 23:30:15 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 17:54:18 +02:00
|
|
|
/// The DWARF component of yaml2obj. Provided as library code for tests.
|
2016-12-07 23:30:15 +01:00
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-01-12 22:35:21 +01:00
|
|
|
#include "llvm/ObjectYAML/DWARFEmitter.h"
|
2020-07-23 04:25:01 +02:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2017-07-01 03:35:55 +02:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2020-07-31 14:06:30 +02:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2020-06-05 06:15:34 +02:00
|
|
|
#include "llvm/BinaryFormat/Dwarf.h"
|
2016-12-07 23:30:15 +01:00
|
|
|
#include "llvm/ObjectYAML/DWARFYAML.h"
|
2020-06-09 12:52:14 +02:00
|
|
|
#include "llvm/Support/Errc.h"
|
2016-12-07 23:30:15 +01:00
|
|
|
#include "llvm/Support/Error.h"
|
2017-07-01 03:35:55 +02:00
|
|
|
#include "llvm/Support/Host.h"
|
2016-12-07 23:30:15 +01:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2017-07-01 03:35:55 +02:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2020-06-29 09:35:12 +02:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2016-12-22 22:58:03 +01:00
|
|
|
#include "llvm/Support/SwapByteOrder.h"
|
2017-07-01 03:35:55 +02:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-12-22 23:44:27 +01:00
|
|
|
#include <algorithm>
|
2017-07-01 03:35:55 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-12-22 23:44:27 +01:00
|
|
|
|
2016-12-07 23:30:15 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2016-12-22 22:58:03 +01:00
|
|
|
template <typename T>
|
2017-02-11 12:06:55 +01:00
|
|
|
static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) {
|
2016-12-22 22:58:03 +01:00
|
|
|
if (IsLittleEndian != sys::IsLittleEndianHost)
|
|
|
|
sys::swapByteOrder(Integer);
|
|
|
|
OS.write(reinterpret_cast<char *>(&Integer), sizeof(T));
|
|
|
|
}
|
|
|
|
|
2020-06-17 03:29:04 +02:00
|
|
|
static Error writeVariableSizedInteger(uint64_t Integer, size_t Size,
|
|
|
|
raw_ostream &OS, bool IsLittleEndian) {
|
2016-12-22 22:58:03 +01:00
|
|
|
if (8 == Size)
|
|
|
|
writeInteger((uint64_t)Integer, OS, IsLittleEndian);
|
|
|
|
else if (4 == Size)
|
|
|
|
writeInteger((uint32_t)Integer, OS, IsLittleEndian);
|
|
|
|
else if (2 == Size)
|
|
|
|
writeInteger((uint16_t)Integer, OS, IsLittleEndian);
|
|
|
|
else if (1 == Size)
|
|
|
|
writeInteger((uint8_t)Integer, OS, IsLittleEndian);
|
|
|
|
else
|
2020-06-17 03:29:04 +02:00
|
|
|
return createStringError(errc::not_supported,
|
|
|
|
"invalid integer write size: %zu", Size);
|
|
|
|
|
|
|
|
return Error::success();
|
2016-12-22 22:58:03 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 12:06:55 +01:00
|
|
|
static void ZeroFillBytes(raw_ostream &OS, size_t Size) {
|
2016-12-09 01:26:44 +01:00
|
|
|
std::vector<uint8_t> FillData;
|
|
|
|
FillData.insert(FillData.begin(), Size, 0);
|
|
|
|
OS.write(reinterpret_cast<char *>(FillData.data()), Size);
|
|
|
|
}
|
|
|
|
|
2020-06-12 15:10:14 +02:00
|
|
|
static void writeInitialLength(const dwarf::DwarfFormat Format,
|
|
|
|
const uint64_t Length, raw_ostream &OS,
|
|
|
|
bool IsLittleEndian) {
|
|
|
|
bool IsDWARF64 = Format == dwarf::DWARF64;
|
|
|
|
if (IsDWARF64)
|
2020-06-17 03:29:04 +02:00
|
|
|
cantFail(writeVariableSizedInteger(dwarf::DW_LENGTH_DWARF64, 4, OS,
|
|
|
|
IsLittleEndian));
|
|
|
|
cantFail(
|
|
|
|
writeVariableSizedInteger(Length, IsDWARF64 ? 8 : 4, OS, IsLittleEndian));
|
2020-06-12 15:10:14 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 06:10:21 +02:00
|
|
|
static void writeDWARFOffset(uint64_t Offset, dwarf::DwarfFormat Format,
|
|
|
|
raw_ostream &OS, bool IsLittleEndian) {
|
|
|
|
cantFail(writeVariableSizedInteger(Offset, Format == dwarf::DWARF64 ? 8 : 4,
|
|
|
|
OS, IsLittleEndian));
|
|
|
|
}
|
|
|
|
|
2020-06-08 16:48:32 +02:00
|
|
|
Error DWARFYAML::emitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) {
|
2016-12-07 23:30:15 +01:00
|
|
|
for (auto Str : DI.DebugStrings) {
|
|
|
|
OS.write(Str.data(), Str.size());
|
|
|
|
OS.write('\0');
|
|
|
|
}
|
2020-06-08 16:48:32 +02:00
|
|
|
|
|
|
|
return Error::success();
|
2016-12-07 23:30:15 +01:00
|
|
|
}
|
|
|
|
|
2020-06-08 16:48:32 +02:00
|
|
|
Error DWARFYAML::emitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) {
|
2020-08-21 04:11:33 +02:00
|
|
|
for (const DWARFYAML::AbbrevTable &AbbrevTable : DI.DebugAbbrev) {
|
2020-08-28 12:42:19 +02:00
|
|
|
uint64_t AbbrevCode = 0;
|
2020-08-21 04:11:33 +02:00
|
|
|
for (const DWARFYAML::Abbrev &AbbrevDecl : AbbrevTable.Table) {
|
|
|
|
AbbrevCode =
|
|
|
|
AbbrevDecl.Code ? (uint64_t)*AbbrevDecl.Code : AbbrevCode + 1;
|
|
|
|
encodeULEB128(AbbrevCode, OS);
|
|
|
|
encodeULEB128(AbbrevDecl.Tag, OS);
|
|
|
|
OS.write(AbbrevDecl.Children);
|
|
|
|
for (auto Attr : AbbrevDecl.Attributes) {
|
|
|
|
encodeULEB128(Attr.Attribute, OS);
|
|
|
|
encodeULEB128(Attr.Form, OS);
|
|
|
|
if (Attr.Form == dwarf::DW_FORM_implicit_const)
|
|
|
|
encodeSLEB128(Attr.Value, OS);
|
|
|
|
}
|
|
|
|
encodeULEB128(0, OS);
|
|
|
|
encodeULEB128(0, OS);
|
2016-12-07 23:30:15 +01:00
|
|
|
}
|
2020-06-08 16:48:32 +02:00
|
|
|
|
2020-08-21 04:11:33 +02:00
|
|
|
// The abbreviations for a given compilation unit end with an entry
|
|
|
|
// consisting of a 0 byte for the abbreviation code.
|
|
|
|
OS.write_zeros(1);
|
|
|
|
}
|
2020-07-01 17:17:18 +02:00
|
|
|
|
2020-06-08 16:48:32 +02:00
|
|
|
return Error::success();
|
2016-12-07 23:30:15 +01:00
|
|
|
}
|
2016-12-09 01:26:44 +01:00
|
|
|
|
2020-06-08 16:48:32 +02:00
|
|
|
Error DWARFYAML::emitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
|
2020-07-31 06:56:10 +02:00
|
|
|
assert(DI.DebugAranges && "unexpected emitDebugAranges() call");
|
|
|
|
for (auto Range : *DI.DebugAranges) {
|
2020-07-30 11:39:39 +02:00
|
|
|
uint8_t AddrSize;
|
|
|
|
if (Range.AddrSize)
|
|
|
|
AddrSize = *Range.AddrSize;
|
|
|
|
else
|
|
|
|
AddrSize = DI.Is64BitAddrSize ? 8 : 4;
|
|
|
|
|
2020-07-30 11:42:18 +02:00
|
|
|
uint64_t Length = 4; // sizeof(version) 2 + sizeof(address_size) 1 +
|
|
|
|
// sizeof(segment_selector_size) 1
|
|
|
|
Length +=
|
|
|
|
Range.Format == dwarf::DWARF64 ? 8 : 4; // sizeof(debug_info_offset)
|
|
|
|
|
|
|
|
const uint64_t HeaderLength =
|
|
|
|
Length + (Range.Format == dwarf::DWARF64
|
|
|
|
? 12
|
|
|
|
: 4); // sizeof(unit_header) = 12 (DWARF64) or 4 (DWARF32)
|
|
|
|
const uint64_t PaddedHeaderLength = alignTo(HeaderLength, AddrSize * 2);
|
|
|
|
|
|
|
|
if (Range.Length) {
|
|
|
|
Length = *Range.Length;
|
|
|
|
} else {
|
|
|
|
Length += PaddedHeaderLength - HeaderLength;
|
|
|
|
Length += AddrSize * 2 * (Range.Descriptors.size() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeInitialLength(Range.Format, Length, OS, DI.IsLittleEndian);
|
2016-12-22 22:58:03 +01:00
|
|
|
writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian);
|
2020-07-24 06:10:21 +02:00
|
|
|
writeDWARFOffset(Range.CuOffset, Range.Format, OS, DI.IsLittleEndian);
|
2020-07-30 11:39:39 +02:00
|
|
|
writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian);
|
2016-12-22 22:58:03 +01:00
|
|
|
writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian);
|
2020-07-30 11:42:18 +02:00
|
|
|
ZeroFillBytes(OS, PaddedHeaderLength - HeaderLength);
|
2016-12-09 01:26:44 +01:00
|
|
|
|
|
|
|
for (auto Descriptor : Range.Descriptors) {
|
2020-07-30 11:39:39 +02:00
|
|
|
if (Error Err = writeVariableSizedInteger(Descriptor.Address, AddrSize,
|
|
|
|
OS, DI.IsLittleEndian))
|
2020-06-17 03:29:04 +02:00
|
|
|
return createStringError(errc::not_supported,
|
|
|
|
"unable to write debug_aranges address: %s",
|
|
|
|
toString(std::move(Err)).c_str());
|
2020-07-30 11:39:39 +02:00
|
|
|
cantFail(writeVariableSizedInteger(Descriptor.Length, AddrSize, OS,
|
2020-06-17 03:29:04 +02:00
|
|
|
DI.IsLittleEndian));
|
2016-12-09 01:26:44 +01:00
|
|
|
}
|
2020-07-30 11:39:39 +02:00
|
|
|
ZeroFillBytes(OS, AddrSize * 2);
|
2016-12-09 01:26:44 +01:00
|
|
|
}
|
2020-06-08 16:48:32 +02:00
|
|
|
|
|
|
|
return Error::success();
|
2016-12-09 01:26:44 +01:00
|
|
|
}
|
2016-12-19 23:22:12 +01:00
|
|
|
|
2020-06-08 16:48:32 +02:00
|
|
|
Error DWARFYAML::emitDebugRanges(raw_ostream &OS, const DWARFYAML::Data &DI) {
|
2020-04-24 04:25:12 +02:00
|
|
|
const size_t RangesOffset = OS.tell();
|
2020-06-09 12:52:14 +02:00
|
|
|
uint64_t EntryIndex = 0;
|
2020-05-14 07:01:57 +02:00
|
|
|
for (auto DebugRanges : DI.DebugRanges) {
|
2020-04-24 04:25:12 +02:00
|
|
|
const size_t CurrOffset = OS.tell() - RangesOffset;
|
2020-06-11 02:36:06 +02:00
|
|
|
if (DebugRanges.Offset && (uint64_t)*DebugRanges.Offset < CurrOffset)
|
2020-06-09 12:52:14 +02:00
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"'Offset' for 'debug_ranges' with index " +
|
|
|
|
Twine(EntryIndex) +
|
|
|
|
" must be greater than or equal to the "
|
|
|
|
"number of bytes written already (0x" +
|
|
|
|
Twine::utohexstr(CurrOffset) + ")");
|
2020-06-11 02:36:06 +02:00
|
|
|
if (DebugRanges.Offset)
|
|
|
|
ZeroFillBytes(OS, *DebugRanges.Offset - CurrOffset);
|
2020-06-14 06:00:46 +02:00
|
|
|
|
|
|
|
uint8_t AddrSize;
|
|
|
|
if (DebugRanges.AddrSize)
|
|
|
|
AddrSize = *DebugRanges.AddrSize;
|
|
|
|
else
|
2020-07-14 04:55:34 +02:00
|
|
|
AddrSize = DI.Is64BitAddrSize ? 8 : 4;
|
2020-05-14 07:01:57 +02:00
|
|
|
for (auto Entry : DebugRanges.Entries) {
|
2020-06-17 03:29:04 +02:00
|
|
|
if (Error Err = writeVariableSizedInteger(Entry.LowOffset, AddrSize, OS,
|
|
|
|
DI.IsLittleEndian))
|
|
|
|
return createStringError(
|
|
|
|
errc::not_supported,
|
|
|
|
"unable to write debug_ranges address offset: %s",
|
|
|
|
toString(std::move(Err)).c_str());
|
|
|
|
cantFail(writeVariableSizedInteger(Entry.HighOffset, AddrSize, OS,
|
|
|
|
DI.IsLittleEndian));
|
2020-04-24 04:25:12 +02:00
|
|
|
}
|
2020-06-14 06:00:46 +02:00
|
|
|
ZeroFillBytes(OS, AddrSize * 2);
|
2020-06-09 12:52:14 +02:00
|
|
|
++EntryIndex;
|
2020-04-24 04:25:12 +02:00
|
|
|
}
|
2020-06-08 16:48:32 +02:00
|
|
|
|
|
|
|
return Error::success();
|
2020-04-24 04:25:12 +02:00
|
|
|
}
|
|
|
|
|
2020-07-31 14:02:20 +02:00
|
|
|
static Error emitPubSection(raw_ostream &OS, const DWARFYAML::PubSection &Sect,
|
|
|
|
bool IsLittleEndian, bool IsGNUPubSec = false) {
|
2020-08-13 12:37:58 +02:00
|
|
|
writeInitialLength(Sect.Format, Sect.Length, OS, IsLittleEndian);
|
2016-12-22 22:58:03 +01:00
|
|
|
writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian);
|
|
|
|
writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian);
|
|
|
|
writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian);
|
2016-12-19 23:22:12 +01:00
|
|
|
for (auto Entry : Sect.Entries) {
|
2016-12-22 22:58:03 +01:00
|
|
|
writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian);
|
2020-07-03 11:56:02 +02:00
|
|
|
if (IsGNUPubSec)
|
2020-06-25 02:06:56 +02:00
|
|
|
writeInteger((uint8_t)Entry.Descriptor, OS, IsLittleEndian);
|
2016-12-19 23:22:12 +01:00
|
|
|
OS.write(Entry.Name.data(), Entry.Name.size());
|
|
|
|
OS.write('\0');
|
|
|
|
}
|
2020-06-08 16:48:32 +02:00
|
|
|
|
|
|
|
return Error::success();
|
2016-12-22 23:44:27 +01:00
|
|
|
}
|
|
|
|
|
2020-07-31 14:02:20 +02:00
|
|
|
Error DWARFYAML::emitDebugPubnames(raw_ostream &OS, const Data &DI) {
|
|
|
|
assert(DI.PubNames && "unexpected emitDebugPubnames() call");
|
|
|
|
return emitPubSection(OS, *DI.PubNames, DI.IsLittleEndian);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DWARFYAML::emitDebugPubtypes(raw_ostream &OS, const Data &DI) {
|
|
|
|
assert(DI.PubTypes && "unexpected emitDebugPubtypes() call");
|
|
|
|
return emitPubSection(OS, *DI.PubTypes, DI.IsLittleEndian);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DWARFYAML::emitDebugGNUPubnames(raw_ostream &OS, const Data &DI) {
|
|
|
|
assert(DI.GNUPubNames && "unexpected emitDebugGNUPubnames() call");
|
|
|
|
return emitPubSection(OS, *DI.GNUPubNames, DI.IsLittleEndian,
|
|
|
|
/*IsGNUStyle=*/true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DWARFYAML::emitDebugGNUPubtypes(raw_ostream &OS, const Data &DI) {
|
|
|
|
assert(DI.GNUPubTypes && "unexpected emitDebugGNUPubtypes() call");
|
|
|
|
return emitPubSection(OS, *DI.GNUPubTypes, DI.IsLittleEndian,
|
|
|
|
/*IsGNUStyle=*/true);
|
|
|
|
}
|
|
|
|
|
2020-08-21 13:01:35 +02:00
|
|
|
static Expected<uint64_t> writeDIE(const DWARFYAML::Data &DI, uint64_t CUIndex,
|
|
|
|
uint64_t AbbrevTableID,
|
2020-08-06 10:37:52 +02:00
|
|
|
const dwarf::FormParams &Params,
|
2020-07-23 17:00:19 +02:00
|
|
|
const DWARFYAML::Entry &Entry,
|
|
|
|
raw_ostream &OS, bool IsLittleEndian) {
|
|
|
|
uint64_t EntryBegin = OS.tell();
|
|
|
|
encodeULEB128(Entry.AbbrCode, OS);
|
|
|
|
uint32_t AbbrCode = Entry.AbbrCode;
|
|
|
|
if (AbbrCode == 0 || Entry.Values.empty())
|
|
|
|
return OS.tell() - EntryBegin;
|
|
|
|
|
2020-08-21 13:01:35 +02:00
|
|
|
Expected<uint64_t> AbbrevTableIndexOrErr =
|
|
|
|
DI.getAbbrevTableIndexByID(AbbrevTableID);
|
|
|
|
if (!AbbrevTableIndexOrErr)
|
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
toString(AbbrevTableIndexOrErr.takeError()) +
|
|
|
|
" for compilation unit with index " +
|
|
|
|
utostr(CUIndex));
|
2020-08-21 06:14:58 +02:00
|
|
|
|
2020-08-21 13:01:35 +02:00
|
|
|
ArrayRef<DWARFYAML::Abbrev> AbbrevDecls(
|
|
|
|
DI.DebugAbbrev[*AbbrevTableIndexOrErr].Table);
|
2020-08-21 04:11:33 +02:00
|
|
|
|
2020-07-23 17:00:19 +02:00
|
|
|
if (AbbrCode > AbbrevDecls.size())
|
|
|
|
return createStringError(
|
|
|
|
errc::invalid_argument,
|
|
|
|
"abbrev code must be less than or equal to the number of "
|
|
|
|
"entries in abbreviation table");
|
|
|
|
const DWARFYAML::Abbrev &Abbrev = AbbrevDecls[AbbrCode - 1];
|
|
|
|
auto FormVal = Entry.Values.begin();
|
|
|
|
auto AbbrForm = Abbrev.Attributes.begin();
|
|
|
|
for (; FormVal != Entry.Values.end() && AbbrForm != Abbrev.Attributes.end();
|
|
|
|
++FormVal, ++AbbrForm) {
|
|
|
|
dwarf::Form Form = AbbrForm->Form;
|
|
|
|
bool Indirect;
|
|
|
|
do {
|
|
|
|
Indirect = false;
|
|
|
|
switch (Form) {
|
|
|
|
case dwarf::DW_FORM_addr:
|
|
|
|
// TODO: Test this error.
|
2020-07-24 10:54:31 +02:00
|
|
|
if (Error Err = writeVariableSizedInteger(
|
2020-08-06 10:37:52 +02:00
|
|
|
FormVal->Value, Params.AddrSize, OS, IsLittleEndian))
|
2020-07-23 17:00:19 +02:00
|
|
|
return std::move(Err);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_FORM_ref_addr:
|
|
|
|
// TODO: Test this error.
|
2020-08-06 10:37:52 +02:00
|
|
|
if (Error Err = writeVariableSizedInteger(FormVal->Value,
|
|
|
|
Params.getRefAddrByteSize(),
|
|
|
|
OS, IsLittleEndian))
|
2020-07-23 17:00:19 +02:00
|
|
|
return std::move(Err);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_FORM_exprloc:
|
|
|
|
case dwarf::DW_FORM_block:
|
|
|
|
encodeULEB128(FormVal->BlockData.size(), OS);
|
|
|
|
OS.write((const char *)FormVal->BlockData.data(),
|
|
|
|
FormVal->BlockData.size());
|
|
|
|
break;
|
|
|
|
case dwarf::DW_FORM_block1: {
|
|
|
|
writeInteger((uint8_t)FormVal->BlockData.size(), OS, IsLittleEndian);
|
|
|
|
OS.write((const char *)FormVal->BlockData.data(),
|
|
|
|
FormVal->BlockData.size());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case dwarf::DW_FORM_block2: {
|
|
|
|
writeInteger((uint16_t)FormVal->BlockData.size(), OS, IsLittleEndian);
|
|
|
|
OS.write((const char *)FormVal->BlockData.data(),
|
|
|
|
FormVal->BlockData.size());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case dwarf::DW_FORM_block4: {
|
|
|
|
writeInteger((uint32_t)FormVal->BlockData.size(), OS, IsLittleEndian);
|
|
|
|
OS.write((const char *)FormVal->BlockData.data(),
|
|
|
|
FormVal->BlockData.size());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case dwarf::DW_FORM_strx:
|
|
|
|
case dwarf::DW_FORM_addrx:
|
|
|
|
case dwarf::DW_FORM_rnglistx:
|
|
|
|
case dwarf::DW_FORM_loclistx:
|
|
|
|
case dwarf::DW_FORM_udata:
|
|
|
|
case dwarf::DW_FORM_ref_udata:
|
|
|
|
case dwarf::DW_FORM_GNU_addr_index:
|
|
|
|
case dwarf::DW_FORM_GNU_str_index:
|
|
|
|
encodeULEB128(FormVal->Value, OS);
|
|
|
|
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:
|
|
|
|
writeInteger((uint8_t)FormVal->Value, OS, IsLittleEndian);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_FORM_data2:
|
|
|
|
case dwarf::DW_FORM_ref2:
|
|
|
|
case dwarf::DW_FORM_strx2:
|
|
|
|
case dwarf::DW_FORM_addrx2:
|
|
|
|
writeInteger((uint16_t)FormVal->Value, OS, IsLittleEndian);
|
|
|
|
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:
|
|
|
|
writeInteger((uint32_t)FormVal->Value, OS, IsLittleEndian);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_FORM_data8:
|
|
|
|
case dwarf::DW_FORM_ref8:
|
|
|
|
case dwarf::DW_FORM_ref_sup8:
|
|
|
|
case dwarf::DW_FORM_ref_sig8:
|
|
|
|
writeInteger((uint64_t)FormVal->Value, OS, IsLittleEndian);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_FORM_sdata:
|
|
|
|
encodeSLEB128(FormVal->Value, OS);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_FORM_string:
|
|
|
|
OS.write(FormVal->CStr.data(), FormVal->CStr.size());
|
|
|
|
OS.write('\0');
|
|
|
|
break;
|
|
|
|
case dwarf::DW_FORM_indirect:
|
|
|
|
encodeULEB128(FormVal->Value, OS);
|
|
|
|
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:
|
2020-08-06 10:37:52 +02:00
|
|
|
cantFail(writeVariableSizedInteger(FormVal->Value,
|
|
|
|
Params.getDwarfOffsetByteSize(), OS,
|
|
|
|
IsLittleEndian));
|
2020-07-23 17:00:19 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (Indirect);
|
2017-03-06 21:52:12 +01:00
|
|
|
}
|
2017-07-01 03:35:55 +02:00
|
|
|
|
2020-07-23 17:00:19 +02:00
|
|
|
return OS.tell() - EntryBegin;
|
|
|
|
}
|
2017-07-01 03:35:55 +02:00
|
|
|
|
2020-07-23 17:00:19 +02:00
|
|
|
Error DWARFYAML::emitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
|
2020-08-21 13:01:35 +02:00
|
|
|
for (uint64_t I = 0; I < DI.CompileUnits.size(); ++I) {
|
|
|
|
const DWARFYAML::Unit &Unit = DI.CompileUnits[I];
|
2020-08-12 15:47:27 +02:00
|
|
|
uint8_t AddrSize;
|
|
|
|
if (Unit.AddrSize)
|
|
|
|
AddrSize = *Unit.AddrSize;
|
|
|
|
else
|
|
|
|
AddrSize = DI.Is64BitAddrSize ? 8 : 4;
|
|
|
|
dwarf::FormParams Params = {Unit.Version, AddrSize, Unit.Format};
|
2020-07-23 17:00:19 +02:00
|
|
|
uint64_t Length = 3; // sizeof(version) + sizeof(address_size)
|
2020-08-06 10:37:52 +02:00
|
|
|
Length += Unit.Version >= 5 ? 1 : 0; // sizeof(unit_type)
|
|
|
|
Length += Params.getDwarfOffsetByteSize(); // sizeof(debug_abbrev_offset)
|
2020-07-23 17:00:19 +02:00
|
|
|
|
|
|
|
// Since the length of the current compilation unit is undetermined yet, we
|
|
|
|
// firstly write the content of the compilation unit to a buffer to
|
|
|
|
// calculate it and then serialize the buffer content to the actual output
|
|
|
|
// stream.
|
|
|
|
std::string EntryBuffer;
|
|
|
|
raw_string_ostream EntryBufferOS(EntryBuffer);
|
|
|
|
|
2020-08-21 13:01:35 +02:00
|
|
|
uint64_t AbbrevTableID = Unit.AbbrevTableID.getValueOr(I);
|
2020-07-23 17:00:19 +02:00
|
|
|
for (const DWARFYAML::Entry &Entry : Unit.Entries) {
|
2020-08-21 13:01:35 +02:00
|
|
|
if (Expected<uint64_t> EntryLength =
|
|
|
|
writeDIE(DI, I, AbbrevTableID, Params, Entry, EntryBufferOS,
|
|
|
|
DI.IsLittleEndian))
|
2020-07-23 17:00:19 +02:00
|
|
|
Length += *EntryLength;
|
|
|
|
else
|
|
|
|
return EntryLength.takeError();
|
|
|
|
}
|
2017-03-06 21:52:12 +01:00
|
|
|
|
2020-07-23 17:00:19 +02:00
|
|
|
// If the length is specified in the YAML description, we use it instead of
|
|
|
|
// the actual length.
|
|
|
|
if (Unit.Length)
|
|
|
|
Length = *Unit.Length;
|
|
|
|
|
2020-08-06 10:37:52 +02:00
|
|
|
writeInitialLength(Unit.Format, Length, OS, DI.IsLittleEndian);
|
|
|
|
writeInteger((uint16_t)Unit.Version, OS, DI.IsLittleEndian);
|
|
|
|
if (Unit.Version >= 5) {
|
2020-07-23 17:00:19 +02:00
|
|
|
writeInteger((uint8_t)Unit.Type, OS, DI.IsLittleEndian);
|
2020-08-12 15:47:27 +02:00
|
|
|
writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian);
|
2020-08-06 10:37:52 +02:00
|
|
|
writeDWARFOffset(Unit.AbbrOffset, Unit.Format, OS, DI.IsLittleEndian);
|
2020-07-23 17:00:19 +02:00
|
|
|
} else {
|
2020-08-06 10:37:52 +02:00
|
|
|
writeDWARFOffset(Unit.AbbrOffset, Unit.Format, OS, DI.IsLittleEndian);
|
2020-08-12 15:47:27 +02:00
|
|
|
writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian);
|
2020-07-23 17:00:19 +02:00
|
|
|
}
|
2017-03-06 21:52:12 +01:00
|
|
|
|
2020-07-23 17:00:19 +02:00
|
|
|
OS.write(EntryBuffer.data(), EntryBuffer.size());
|
2017-03-06 21:52:12 +01:00
|
|
|
}
|
|
|
|
|
2020-07-23 17:00:19 +02:00
|
|
|
return Error::success();
|
2016-12-22 23:44:27 +01:00
|
|
|
}
|
2017-01-10 07:22:49 +01:00
|
|
|
|
2020-06-08 11:30:23 +02:00
|
|
|
static void emitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
|
2017-01-10 07:22:49 +01:00
|
|
|
OS.write(File.Name.data(), File.Name.size());
|
|
|
|
OS.write('\0');
|
|
|
|
encodeULEB128(File.DirIdx, OS);
|
|
|
|
encodeULEB128(File.ModTime, OS);
|
|
|
|
encodeULEB128(File.Length, OS);
|
|
|
|
}
|
|
|
|
|
2020-08-26 14:34:36 +02:00
|
|
|
static void writeLineTableOpcode(const DWARFYAML::LineTableOpcode &Op,
|
|
|
|
uint8_t OpcodeBase, uint8_t AddrSize,
|
|
|
|
raw_ostream &OS, bool IsLittleEndian) {
|
|
|
|
writeInteger((uint8_t)Op.Opcode, OS, IsLittleEndian);
|
|
|
|
if (Op.Opcode == 0) {
|
|
|
|
encodeULEB128(Op.ExtLen, OS);
|
|
|
|
writeInteger((uint8_t)Op.SubOpcode, OS, IsLittleEndian);
|
|
|
|
switch (Op.SubOpcode) {
|
|
|
|
case dwarf::DW_LNE_set_address:
|
|
|
|
cantFail(
|
|
|
|
writeVariableSizedInteger(Op.Data, AddrSize, OS, IsLittleEndian));
|
|
|
|
break;
|
|
|
|
case dwarf::DW_LNE_define_file:
|
|
|
|
emitFileEntry(OS, Op.FileEntry);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_LNE_set_discriminator:
|
|
|
|
encodeULEB128(Op.Data, OS);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_LNE_end_sequence:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
for (auto OpByte : Op.UnknownOpcodeData)
|
|
|
|
writeInteger((uint8_t)OpByte, OS, IsLittleEndian);
|
|
|
|
}
|
|
|
|
} else if (Op.Opcode < OpcodeBase) {
|
|
|
|
switch (Op.Opcode) {
|
|
|
|
case dwarf::DW_LNS_copy:
|
|
|
|
case dwarf::DW_LNS_negate_stmt:
|
|
|
|
case dwarf::DW_LNS_set_basic_block:
|
|
|
|
case dwarf::DW_LNS_const_add_pc:
|
|
|
|
case dwarf::DW_LNS_set_prologue_end:
|
|
|
|
case dwarf::DW_LNS_set_epilogue_begin:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case dwarf::DW_LNS_advance_pc:
|
|
|
|
case dwarf::DW_LNS_set_file:
|
|
|
|
case dwarf::DW_LNS_set_column:
|
|
|
|
case dwarf::DW_LNS_set_isa:
|
|
|
|
encodeULEB128(Op.Data, OS);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case dwarf::DW_LNS_advance_line:
|
|
|
|
encodeSLEB128(Op.SData, OS);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case dwarf::DW_LNS_fixed_advance_pc:
|
|
|
|
writeInteger((uint16_t)Op.Data, OS, IsLittleEndian);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
for (auto OpData : Op.StandardOpcodeData) {
|
|
|
|
encodeULEB128(OpData, OS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 16:48:32 +02:00
|
|
|
Error DWARFYAML::emitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
|
2020-08-26 14:34:36 +02:00
|
|
|
for (const DWARFYAML::LineTable &LineTable : DI.DebugLines) {
|
|
|
|
// Buffer holds the bytes following the header_length (or prologue_length in
|
|
|
|
// DWARFv2) field to the end of the line number program itself.
|
|
|
|
std::string Buffer;
|
|
|
|
raw_string_ostream BufferOS(Buffer);
|
|
|
|
|
|
|
|
writeInteger(LineTable.MinInstLength, BufferOS, DI.IsLittleEndian);
|
|
|
|
// TODO: Add support for emitting DWARFv5 line table.
|
2017-01-10 07:22:49 +01:00
|
|
|
if (LineTable.Version >= 4)
|
2020-08-26 14:34:36 +02:00
|
|
|
writeInteger(LineTable.MaxOpsPerInst, BufferOS, DI.IsLittleEndian);
|
|
|
|
writeInteger(LineTable.DefaultIsStmt, BufferOS, DI.IsLittleEndian);
|
|
|
|
writeInteger(LineTable.LineBase, BufferOS, DI.IsLittleEndian);
|
|
|
|
writeInteger(LineTable.LineRange, BufferOS, DI.IsLittleEndian);
|
|
|
|
writeInteger(LineTable.OpcodeBase, BufferOS, DI.IsLittleEndian);
|
|
|
|
|
|
|
|
for (uint8_t OpcodeLength : LineTable.StandardOpcodeLengths)
|
|
|
|
writeInteger(OpcodeLength, BufferOS, DI.IsLittleEndian);
|
|
|
|
|
|
|
|
for (StringRef IncludeDir : LineTable.IncludeDirs) {
|
|
|
|
BufferOS.write(IncludeDir.data(), IncludeDir.size());
|
|
|
|
BufferOS.write('\0');
|
2017-01-10 07:22:49 +01:00
|
|
|
}
|
2020-08-26 14:34:36 +02:00
|
|
|
BufferOS.write('\0');
|
2017-01-10 07:22:49 +01:00
|
|
|
|
2020-08-26 14:34:36 +02:00
|
|
|
for (const DWARFYAML::File &File : LineTable.Files)
|
|
|
|
emitFileEntry(BufferOS, File);
|
|
|
|
BufferOS.write('\0');
|
2017-01-10 07:22:49 +01:00
|
|
|
|
2020-08-26 14:34:36 +02:00
|
|
|
uint64_t HeaderLength =
|
|
|
|
LineTable.PrologueLength ? *LineTable.PrologueLength : Buffer.size();
|
|
|
|
|
|
|
|
for (const DWARFYAML::LineTableOpcode &Op : LineTable.Opcodes)
|
|
|
|
writeLineTableOpcode(Op, LineTable.OpcodeBase, DI.Is64BitAddrSize ? 8 : 4,
|
|
|
|
BufferOS, DI.IsLittleEndian);
|
|
|
|
|
|
|
|
uint64_t Length;
|
|
|
|
if (LineTable.Length) {
|
|
|
|
Length = *LineTable.Length;
|
|
|
|
} else {
|
|
|
|
Length = 2; // sizeof(version)
|
|
|
|
Length +=
|
|
|
|
(LineTable.Format == dwarf::DWARF64 ? 8 : 4); // sizeof(header_length)
|
|
|
|
Length += Buffer.size();
|
2017-01-10 07:22:49 +01:00
|
|
|
}
|
2020-08-26 14:34:36 +02:00
|
|
|
|
|
|
|
writeInitialLength(LineTable.Format, Length, OS, DI.IsLittleEndian);
|
|
|
|
writeInteger(LineTable.Version, OS, DI.IsLittleEndian);
|
|
|
|
writeDWARFOffset(HeaderLength, LineTable.Format, OS, DI.IsLittleEndian);
|
|
|
|
OS.write(Buffer.data(), Buffer.size());
|
2017-01-10 07:22:49 +01:00
|
|
|
}
|
2020-06-08 16:48:32 +02:00
|
|
|
|
|
|
|
return Error::success();
|
2017-01-10 07:22:49 +01:00
|
|
|
}
|
2017-01-20 20:03:14 +01:00
|
|
|
|
2020-06-16 04:50:49 +02:00
|
|
|
Error DWARFYAML::emitDebugAddr(raw_ostream &OS, const Data &DI) {
|
|
|
|
for (const AddrTableEntry &TableEntry : DI.DebugAddr) {
|
|
|
|
uint8_t AddrSize;
|
|
|
|
if (TableEntry.AddrSize)
|
|
|
|
AddrSize = *TableEntry.AddrSize;
|
|
|
|
else
|
2020-07-14 04:55:34 +02:00
|
|
|
AddrSize = DI.Is64BitAddrSize ? 8 : 4;
|
2020-06-16 04:50:49 +02:00
|
|
|
|
|
|
|
uint64_t Length;
|
|
|
|
if (TableEntry.Length)
|
|
|
|
Length = (uint64_t)*TableEntry.Length;
|
|
|
|
else
|
|
|
|
// 2 (version) + 1 (address_size) + 1 (segment_selector_size) = 4
|
|
|
|
Length = 4 + (AddrSize + TableEntry.SegSelectorSize) *
|
|
|
|
TableEntry.SegAddrPairs.size();
|
|
|
|
|
|
|
|
writeInitialLength(TableEntry.Format, Length, OS, DI.IsLittleEndian);
|
|
|
|
writeInteger((uint16_t)TableEntry.Version, OS, DI.IsLittleEndian);
|
|
|
|
writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian);
|
|
|
|
writeInteger((uint8_t)TableEntry.SegSelectorSize, OS, DI.IsLittleEndian);
|
|
|
|
|
|
|
|
for (const SegAddrPair &Pair : TableEntry.SegAddrPairs) {
|
|
|
|
if (TableEntry.SegSelectorSize != 0)
|
2020-06-17 03:29:04 +02:00
|
|
|
if (Error Err = writeVariableSizedInteger(Pair.Segment,
|
|
|
|
TableEntry.SegSelectorSize,
|
|
|
|
OS, DI.IsLittleEndian))
|
|
|
|
return createStringError(errc::not_supported,
|
|
|
|
"unable to write debug_addr segment: %s",
|
|
|
|
toString(std::move(Err)).c_str());
|
2020-06-16 04:50:49 +02:00
|
|
|
if (AddrSize != 0)
|
2020-06-17 03:29:04 +02:00
|
|
|
if (Error Err = writeVariableSizedInteger(Pair.Address, AddrSize, OS,
|
|
|
|
DI.IsLittleEndian))
|
|
|
|
return createStringError(errc::not_supported,
|
|
|
|
"unable to write debug_addr address: %s",
|
|
|
|
toString(std::move(Err)).c_str());
|
2020-06-16 04:50:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2020-07-16 16:32:31 +02:00
|
|
|
Error DWARFYAML::emitDebugStrOffsets(raw_ostream &OS, const Data &DI) {
|
|
|
|
assert(DI.DebugStrOffsets && "unexpected emitDebugStrOffsets() call");
|
|
|
|
for (const DWARFYAML::StringOffsetsTable &Table : *DI.DebugStrOffsets) {
|
|
|
|
uint64_t Length;
|
|
|
|
if (Table.Length)
|
|
|
|
Length = *Table.Length;
|
|
|
|
else
|
|
|
|
// sizeof(version) + sizeof(padding) = 4
|
|
|
|
Length =
|
|
|
|
4 + Table.Offsets.size() * (Table.Format == dwarf::DWARF64 ? 8 : 4);
|
|
|
|
|
|
|
|
writeInitialLength(Table.Format, Length, OS, DI.IsLittleEndian);
|
|
|
|
writeInteger((uint16_t)Table.Version, OS, DI.IsLittleEndian);
|
|
|
|
writeInteger((uint16_t)Table.Padding, OS, DI.IsLittleEndian);
|
|
|
|
|
2020-07-24 06:10:21 +02:00
|
|
|
for (uint64_t Offset : Table.Offsets)
|
|
|
|
writeDWARFOffset(Offset, Table.Format, OS, DI.IsLittleEndian);
|
2020-07-16 16:32:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2020-07-28 16:20:10 +02:00
|
|
|
static Error checkOperandCount(StringRef EncodingString,
|
|
|
|
ArrayRef<yaml::Hex64> Values,
|
|
|
|
uint64_t ExpectedOperands) {
|
2020-07-23 16:57:30 +02:00
|
|
|
if (Values.size() != ExpectedOperands)
|
|
|
|
return createStringError(
|
|
|
|
errc::invalid_argument,
|
|
|
|
"invalid number (%zu) of operands for the operator: %s, %" PRIu64
|
|
|
|
" expected",
|
|
|
|
Values.size(), EncodingString.str().c_str(), ExpectedOperands);
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Error writeListEntryAddress(StringRef EncodingName, raw_ostream &OS,
|
|
|
|
uint64_t Addr, uint8_t AddrSize,
|
|
|
|
bool IsLittleEndian) {
|
|
|
|
if (Error Err = writeVariableSizedInteger(Addr, AddrSize, OS, IsLittleEndian))
|
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"unable to write address for the operator %s: %s",
|
|
|
|
EncodingName.str().c_str(),
|
|
|
|
toString(std::move(Err)).c_str());
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2020-08-03 17:19:42 +02:00
|
|
|
static Expected<uint64_t>
|
|
|
|
writeDWARFExpression(raw_ostream &OS,
|
|
|
|
const DWARFYAML::DWARFOperation &Operation,
|
|
|
|
uint8_t AddrSize, bool IsLittleEndian) {
|
|
|
|
auto CheckOperands = [&](uint64_t ExpectedOperands) -> Error {
|
|
|
|
return checkOperandCount(dwarf::OperationEncodingString(Operation.Operator),
|
|
|
|
Operation.Values, ExpectedOperands);
|
|
|
|
};
|
|
|
|
|
|
|
|
uint64_t ExpressionBegin = OS.tell();
|
|
|
|
writeInteger((uint8_t)Operation.Operator, OS, IsLittleEndian);
|
|
|
|
switch (Operation.Operator) {
|
|
|
|
case dwarf::DW_OP_consts:
|
|
|
|
if (Error Err = CheckOperands(1))
|
|
|
|
return std::move(Err);
|
|
|
|
encodeSLEB128(Operation.Values[0], OS);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_OP_stack_value:
|
|
|
|
if (Error Err = CheckOperands(0))
|
|
|
|
return std::move(Err);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
StringRef EncodingStr = dwarf::OperationEncodingString(Operation.Operator);
|
|
|
|
return createStringError(errc::not_supported,
|
|
|
|
"DWARF expression: " +
|
|
|
|
(EncodingStr.empty()
|
|
|
|
? "0x" + utohexstr(Operation.Operator)
|
|
|
|
: EncodingStr) +
|
|
|
|
" is not supported");
|
|
|
|
}
|
|
|
|
return OS.tell() - ExpressionBegin;
|
|
|
|
}
|
|
|
|
|
2020-07-23 04:25:01 +02:00
|
|
|
static Expected<uint64_t> writeListEntry(raw_ostream &OS,
|
|
|
|
const DWARFYAML::RnglistEntry &Entry,
|
|
|
|
uint8_t AddrSize,
|
|
|
|
bool IsLittleEndian) {
|
2020-07-20 04:42:27 +02:00
|
|
|
uint64_t BeginOffset = OS.tell();
|
|
|
|
writeInteger((uint8_t)Entry.Operator, OS, IsLittleEndian);
|
|
|
|
|
2020-07-23 16:57:30 +02:00
|
|
|
StringRef EncodingName = dwarf::RangeListEncodingString(Entry.Operator);
|
2020-07-20 04:42:27 +02:00
|
|
|
|
2020-07-23 16:57:30 +02:00
|
|
|
auto CheckOperands = [&](uint64_t ExpectedOperands) -> Error {
|
2020-07-28 16:20:10 +02:00
|
|
|
return checkOperandCount(EncodingName, Entry.Values, ExpectedOperands);
|
2020-07-20 04:42:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto WriteAddress = [&](uint64_t Addr) -> Error {
|
2020-07-23 16:57:30 +02:00
|
|
|
return writeListEntryAddress(EncodingName, OS, Addr, AddrSize,
|
|
|
|
IsLittleEndian);
|
2020-07-20 04:42:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
switch (Entry.Operator) {
|
|
|
|
case dwarf::DW_RLE_end_of_list:
|
|
|
|
if (Error Err = CheckOperands(0))
|
|
|
|
return std::move(Err);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_RLE_base_addressx:
|
|
|
|
if (Error Err = CheckOperands(1))
|
|
|
|
return std::move(Err);
|
|
|
|
encodeULEB128(Entry.Values[0], OS);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_RLE_startx_endx:
|
|
|
|
case dwarf::DW_RLE_startx_length:
|
|
|
|
case dwarf::DW_RLE_offset_pair:
|
|
|
|
if (Error Err = CheckOperands(2))
|
|
|
|
return std::move(Err);
|
|
|
|
encodeULEB128(Entry.Values[0], OS);
|
|
|
|
encodeULEB128(Entry.Values[1], OS);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_RLE_base_address:
|
|
|
|
if (Error Err = CheckOperands(1))
|
|
|
|
return std::move(Err);
|
|
|
|
if (Error Err = WriteAddress(Entry.Values[0]))
|
|
|
|
return std::move(Err);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_RLE_start_end:
|
|
|
|
if (Error Err = CheckOperands(2))
|
|
|
|
return std::move(Err);
|
|
|
|
if (Error Err = WriteAddress(Entry.Values[0]))
|
|
|
|
return std::move(Err);
|
|
|
|
cantFail(WriteAddress(Entry.Values[1]));
|
|
|
|
break;
|
|
|
|
case dwarf::DW_RLE_start_length:
|
|
|
|
if (Error Err = CheckOperands(2))
|
|
|
|
return std::move(Err);
|
|
|
|
if (Error Err = WriteAddress(Entry.Values[0]))
|
|
|
|
return std::move(Err);
|
|
|
|
encodeULEB128(Entry.Values[1], OS);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OS.tell() - BeginOffset;
|
|
|
|
}
|
|
|
|
|
2020-08-03 17:19:42 +02:00
|
|
|
static Expected<uint64_t> writeListEntry(raw_ostream &OS,
|
|
|
|
const DWARFYAML::LoclistEntry &Entry,
|
|
|
|
uint8_t AddrSize,
|
|
|
|
bool IsLittleEndian) {
|
|
|
|
uint64_t BeginOffset = OS.tell();
|
|
|
|
writeInteger((uint8_t)Entry.Operator, OS, IsLittleEndian);
|
|
|
|
|
|
|
|
StringRef EncodingName = dwarf::LocListEncodingString(Entry.Operator);
|
|
|
|
|
|
|
|
auto CheckOperands = [&](uint64_t ExpectedOperands) -> Error {
|
|
|
|
return checkOperandCount(EncodingName, Entry.Values, ExpectedOperands);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto WriteAddress = [&](uint64_t Addr) -> Error {
|
|
|
|
return writeListEntryAddress(EncodingName, OS, Addr, AddrSize,
|
|
|
|
IsLittleEndian);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto WriteDWARFOperations = [&]() -> Error {
|
|
|
|
std::string OpBuffer;
|
|
|
|
raw_string_ostream OpBufferOS(OpBuffer);
|
|
|
|
uint64_t DescriptionsLength = 0;
|
|
|
|
|
|
|
|
for (const DWARFYAML::DWARFOperation &Op : Entry.Descriptions) {
|
|
|
|
if (Expected<uint64_t> OpSize =
|
|
|
|
writeDWARFExpression(OpBufferOS, Op, AddrSize, IsLittleEndian))
|
|
|
|
DescriptionsLength += *OpSize;
|
|
|
|
else
|
|
|
|
return OpSize.takeError();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Entry.DescriptionsLength)
|
|
|
|
DescriptionsLength = *Entry.DescriptionsLength;
|
|
|
|
else
|
|
|
|
DescriptionsLength = OpBuffer.size();
|
|
|
|
|
|
|
|
encodeULEB128(DescriptionsLength, OS);
|
|
|
|
OS.write(OpBuffer.data(), OpBuffer.size());
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (Entry.Operator) {
|
|
|
|
case dwarf::DW_LLE_end_of_list:
|
|
|
|
if (Error Err = CheckOperands(0))
|
|
|
|
return std::move(Err);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_LLE_base_addressx:
|
|
|
|
if (Error Err = CheckOperands(1))
|
|
|
|
return std::move(Err);
|
|
|
|
encodeULEB128(Entry.Values[0], OS);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_LLE_startx_endx:
|
|
|
|
case dwarf::DW_LLE_startx_length:
|
|
|
|
case dwarf::DW_LLE_offset_pair:
|
|
|
|
if (Error Err = CheckOperands(2))
|
|
|
|
return std::move(Err);
|
|
|
|
encodeULEB128(Entry.Values[0], OS);
|
|
|
|
encodeULEB128(Entry.Values[1], OS);
|
|
|
|
if (Error Err = WriteDWARFOperations())
|
|
|
|
return std::move(Err);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_LLE_default_location:
|
|
|
|
if (Error Err = CheckOperands(0))
|
|
|
|
return std::move(Err);
|
|
|
|
if (Error Err = WriteDWARFOperations())
|
|
|
|
return std::move(Err);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_LLE_base_address:
|
|
|
|
if (Error Err = CheckOperands(1))
|
|
|
|
return std::move(Err);
|
|
|
|
if (Error Err = WriteAddress(Entry.Values[0]))
|
|
|
|
return std::move(Err);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_LLE_start_end:
|
|
|
|
if (Error Err = CheckOperands(2))
|
|
|
|
return std::move(Err);
|
|
|
|
if (Error Err = WriteAddress(Entry.Values[0]))
|
|
|
|
return std::move(Err);
|
|
|
|
cantFail(WriteAddress(Entry.Values[1]));
|
|
|
|
if (Error Err = WriteDWARFOperations())
|
|
|
|
return std::move(Err);
|
|
|
|
break;
|
|
|
|
case dwarf::DW_LLE_start_length:
|
|
|
|
if (Error Err = CheckOperands(2))
|
|
|
|
return std::move(Err);
|
|
|
|
if (Error Err = WriteAddress(Entry.Values[0]))
|
|
|
|
return std::move(Err);
|
|
|
|
encodeULEB128(Entry.Values[1], OS);
|
|
|
|
if (Error Err = WriteDWARFOperations())
|
|
|
|
return std::move(Err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OS.tell() - BeginOffset;
|
|
|
|
}
|
|
|
|
|
2020-07-23 04:25:01 +02:00
|
|
|
template <typename EntryType>
|
2020-08-19 15:59:39 +02:00
|
|
|
static Error writeDWARFLists(raw_ostream &OS,
|
|
|
|
ArrayRef<DWARFYAML::ListTable<EntryType>> Tables,
|
|
|
|
bool IsLittleEndian, bool Is64BitAddrSize) {
|
2020-07-23 04:25:01 +02:00
|
|
|
for (const DWARFYAML::ListTable<EntryType> &Table : Tables) {
|
2020-07-20 04:42:27 +02:00
|
|
|
// sizeof(version) + sizeof(address_size) + sizeof(segment_selector_size) +
|
|
|
|
// sizeof(offset_entry_count) = 8
|
|
|
|
uint64_t Length = 8;
|
|
|
|
|
|
|
|
uint8_t AddrSize;
|
|
|
|
if (Table.AddrSize)
|
|
|
|
AddrSize = *Table.AddrSize;
|
|
|
|
else
|
2020-07-23 04:25:01 +02:00
|
|
|
AddrSize = Is64BitAddrSize ? 8 : 4;
|
2020-07-20 04:42:27 +02:00
|
|
|
|
2020-07-23 04:25:01 +02:00
|
|
|
// Since the length of the current range/location lists entry is
|
|
|
|
// undetermined yet, we firstly write the content of the range/location
|
|
|
|
// lists to a buffer to calculate the length and then serialize the buffer
|
|
|
|
// content to the actual output stream.
|
2020-07-20 04:42:27 +02:00
|
|
|
std::string ListBuffer;
|
|
|
|
raw_string_ostream ListBufferOS(ListBuffer);
|
|
|
|
|
2020-07-23 04:25:01 +02:00
|
|
|
// Offsets holds offsets for each range/location list. The i-th element is
|
|
|
|
// the offset from the beginning of the first range/location list to the
|
|
|
|
// location of the i-th range list.
|
2020-07-20 04:42:27 +02:00
|
|
|
std::vector<uint64_t> Offsets;
|
|
|
|
|
2020-07-23 04:25:01 +02:00
|
|
|
for (const DWARFYAML::ListEntries<EntryType> &List : Table.Lists) {
|
2020-07-20 04:42:27 +02:00
|
|
|
Offsets.push_back(ListBufferOS.tell());
|
2020-07-28 16:10:44 +02:00
|
|
|
if (List.Content) {
|
|
|
|
List.Content->writeAsBinary(ListBufferOS, UINT64_MAX);
|
|
|
|
Length += List.Content->binary_size();
|
|
|
|
} else if (List.Entries) {
|
|
|
|
for (const EntryType &Entry : *List.Entries) {
|
|
|
|
Expected<uint64_t> EntrySize =
|
|
|
|
writeListEntry(ListBufferOS, Entry, AddrSize, IsLittleEndian);
|
|
|
|
if (!EntrySize)
|
|
|
|
return EntrySize.takeError();
|
|
|
|
Length += *EntrySize;
|
|
|
|
}
|
2020-07-20 04:42:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the offset_entry_count field isn't specified, yaml2obj will infer it
|
|
|
|
// from the 'Offsets' field in the YAML description. If the 'Offsets' field
|
|
|
|
// isn't specified either, yaml2obj will infer it from the auto-generated
|
|
|
|
// offsets.
|
|
|
|
uint32_t OffsetEntryCount;
|
|
|
|
if (Table.OffsetEntryCount)
|
|
|
|
OffsetEntryCount = *Table.OffsetEntryCount;
|
|
|
|
else
|
|
|
|
OffsetEntryCount = Table.Offsets ? Table.Offsets->size() : Offsets.size();
|
|
|
|
uint64_t OffsetsSize =
|
|
|
|
OffsetEntryCount * (Table.Format == dwarf::DWARF64 ? 8 : 4);
|
|
|
|
Length += OffsetsSize;
|
|
|
|
|
|
|
|
// If the length is specified in the YAML description, we use it instead of
|
|
|
|
// the actual length.
|
|
|
|
if (Table.Length)
|
|
|
|
Length = *Table.Length;
|
|
|
|
|
2020-07-23 04:25:01 +02:00
|
|
|
writeInitialLength(Table.Format, Length, OS, IsLittleEndian);
|
|
|
|
writeInteger((uint16_t)Table.Version, OS, IsLittleEndian);
|
|
|
|
writeInteger((uint8_t)AddrSize, OS, IsLittleEndian);
|
|
|
|
writeInteger((uint8_t)Table.SegSelectorSize, OS, IsLittleEndian);
|
|
|
|
writeInteger((uint32_t)OffsetEntryCount, OS, IsLittleEndian);
|
2020-07-20 04:42:27 +02:00
|
|
|
|
|
|
|
auto EmitOffsets = [&](ArrayRef<uint64_t> Offsets, uint64_t OffsetsSize) {
|
2020-07-24 06:10:21 +02:00
|
|
|
for (uint64_t Offset : Offsets)
|
|
|
|
writeDWARFOffset(OffsetsSize + Offset, Table.Format, OS,
|
|
|
|
IsLittleEndian);
|
2020-07-20 04:42:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (Table.Offsets)
|
|
|
|
EmitOffsets(ArrayRef<uint64_t>((const uint64_t *)Table.Offsets->data(),
|
|
|
|
Table.Offsets->size()),
|
|
|
|
0);
|
2020-08-03 16:04:33 +02:00
|
|
|
else if (OffsetEntryCount != 0)
|
2020-07-20 04:42:27 +02:00
|
|
|
EmitOffsets(Offsets, OffsetsSize);
|
|
|
|
|
|
|
|
OS.write(ListBuffer.data(), ListBuffer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2020-07-23 04:25:01 +02:00
|
|
|
Error DWARFYAML::emitDebugRnglists(raw_ostream &OS, const Data &DI) {
|
|
|
|
assert(DI.DebugRnglists && "unexpected emitDebugRnglists() call");
|
|
|
|
return writeDWARFLists<DWARFYAML::RnglistEntry>(
|
|
|
|
OS, *DI.DebugRnglists, DI.IsLittleEndian, DI.Is64BitAddrSize);
|
|
|
|
}
|
|
|
|
|
2020-08-03 17:19:42 +02:00
|
|
|
Error DWARFYAML::emitDebugLoclists(raw_ostream &OS, const Data &DI) {
|
|
|
|
assert(DI.DebugLoclists && "unexpected emitDebugRnglists() call");
|
|
|
|
return writeDWARFLists<DWARFYAML::LoclistEntry>(
|
|
|
|
OS, *DI.DebugLoclists, DI.IsLittleEndian, DI.Is64BitAddrSize);
|
|
|
|
}
|
|
|
|
|
2020-07-31 14:06:30 +02:00
|
|
|
std::function<Error(raw_ostream &, const DWARFYAML::Data &)>
|
|
|
|
DWARFYAML::getDWARFEmitterByName(StringRef SecName) {
|
|
|
|
auto EmitFunc =
|
|
|
|
StringSwitch<
|
|
|
|
std::function<Error(raw_ostream &, const DWARFYAML::Data &)>>(SecName)
|
|
|
|
.Case("debug_abbrev", DWARFYAML::emitDebugAbbrev)
|
|
|
|
.Case("debug_addr", DWARFYAML::emitDebugAddr)
|
|
|
|
.Case("debug_aranges", DWARFYAML::emitDebugAranges)
|
|
|
|
.Case("debug_gnu_pubnames", DWARFYAML::emitDebugGNUPubnames)
|
|
|
|
.Case("debug_gnu_pubtypes", DWARFYAML::emitDebugGNUPubtypes)
|
|
|
|
.Case("debug_info", DWARFYAML::emitDebugInfo)
|
|
|
|
.Case("debug_line", DWARFYAML::emitDebugLine)
|
2020-08-03 17:19:42 +02:00
|
|
|
.Case("debug_loclists", DWARFYAML::emitDebugLoclists)
|
2020-07-31 14:06:30 +02:00
|
|
|
.Case("debug_pubnames", DWARFYAML::emitDebugPubnames)
|
|
|
|
.Case("debug_pubtypes", DWARFYAML::emitDebugPubtypes)
|
|
|
|
.Case("debug_ranges", DWARFYAML::emitDebugRanges)
|
|
|
|
.Case("debug_rnglists", DWARFYAML::emitDebugRnglists)
|
|
|
|
.Case("debug_str", DWARFYAML::emitDebugStr)
|
|
|
|
.Case("debug_str_offsets", DWARFYAML::emitDebugStrOffsets)
|
|
|
|
.Default([&](raw_ostream &, const DWARFYAML::Data &) {
|
|
|
|
return createStringError(errc::not_supported,
|
|
|
|
SecName + " is not supported");
|
|
|
|
});
|
|
|
|
|
|
|
|
return EmitFunc;
|
|
|
|
}
|
2017-01-20 20:03:14 +01:00
|
|
|
|
2020-06-08 16:48:32 +02:00
|
|
|
static Error
|
2020-07-31 14:06:30 +02:00
|
|
|
emitDebugSectionImpl(const DWARFYAML::Data &DI, StringRef Sec,
|
2017-02-11 12:06:55 +01:00
|
|
|
StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
|
2017-01-20 20:03:14 +01:00
|
|
|
std::string Data;
|
|
|
|
raw_string_ostream DebugInfoStream(Data);
|
2020-07-31 14:06:30 +02:00
|
|
|
|
|
|
|
auto EmitFunc = DWARFYAML::getDWARFEmitterByName(Sec);
|
|
|
|
|
2020-06-08 16:48:32 +02:00
|
|
|
if (Error Err = EmitFunc(DebugInfoStream, DI))
|
|
|
|
return Err;
|
2017-01-20 20:03:14 +01:00
|
|
|
DebugInfoStream.flush();
|
|
|
|
if (!Data.empty())
|
|
|
|
OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data);
|
2020-06-08 16:48:32 +02:00
|
|
|
|
|
|
|
return Error::success();
|
2017-01-20 20:03:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
|
2020-08-04 18:09:12 +02:00
|
|
|
DWARFYAML::emitDebugSections(StringRef YAMLString, bool IsLittleEndian,
|
|
|
|
bool Is64BitAddrSize) {
|
2020-06-29 09:35:12 +02:00
|
|
|
auto CollectDiagnostic = [](const SMDiagnostic &Diag, void *DiagContext) {
|
|
|
|
*static_cast<SMDiagnostic *>(DiagContext) = Diag;
|
|
|
|
};
|
|
|
|
|
|
|
|
SMDiagnostic GeneratedDiag;
|
|
|
|
yaml::Input YIn(YAMLString, /*Ctxt=*/nullptr, CollectDiagnostic,
|
|
|
|
&GeneratedDiag);
|
2017-01-20 20:03:14 +01:00
|
|
|
|
|
|
|
DWARFYAML::Data DI;
|
|
|
|
DI.IsLittleEndian = IsLittleEndian;
|
2020-08-04 18:09:12 +02:00
|
|
|
DI.Is64BitAddrSize = Is64BitAddrSize;
|
|
|
|
|
2017-01-20 20:03:14 +01:00
|
|
|
YIn >> DI;
|
|
|
|
if (YIn.error())
|
2020-06-29 09:35:12 +02:00
|
|
|
return createStringError(YIn.error(), GeneratedDiag.getMessage());
|
2017-01-20 20:03:14 +01:00
|
|
|
|
2018-04-20 14:33:49 +02:00
|
|
|
StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
|
2020-07-31 14:06:30 +02:00
|
|
|
Error Err = Error::success();
|
|
|
|
cantFail(std::move(Err));
|
|
|
|
|
|
|
|
for (StringRef SecName : DI.getNonEmptySectionNames())
|
|
|
|
Err = joinErrors(std::move(Err),
|
|
|
|
emitDebugSectionImpl(DI, SecName, DebugSections));
|
2020-06-08 16:48:32 +02:00
|
|
|
|
|
|
|
if (Err)
|
|
|
|
return std::move(Err);
|
2020-02-10 16:06:45 +01:00
|
|
|
return std::move(DebugSections);
|
2017-01-20 20:03:14 +01:00
|
|
|
}
|