summaryrefslogtreecommitdiff
path: root/third_party/llvm-project/DWARFDebugLoc.cpp
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2019-12-19 09:04:08 -0800
committerGitHub <noreply@github.com>2019-12-19 09:04:08 -0800
commit4d28d3f32e7f213e300b24bc61c3f0ac9d6e1ab6 (patch)
tree91bffc2d47b1fe4bba01e7ada77006ef340bd138 /third_party/llvm-project/DWARFDebugLoc.cpp
parent0048f5b004ddf50e750aa335d0be314a73852058 (diff)
downloadbinaryen-4d28d3f32e7f213e300b24bc61c3f0ac9d6e1ab6.tar.gz
binaryen-4d28d3f32e7f213e300b24bc61c3f0ac9d6e1ab6.tar.bz2
binaryen-4d28d3f32e7f213e300b24bc61c3f0ac9d6e1ab6.zip
DWARF parsing and writing support using LLVM (#2520)
This imports LLVM code for DWARF handling. That code has the Apache 2 license like us. It's also the same code used to emit DWARF in the common toolchain, so it seems like a safe choice. This adds two passes: --dwarfdump which runs the same code LLVM runs for llvm-dwarfdump. This shows we can parse it ok, and will be useful for debugging. And --dwarfupdate writes out the DWARF sections (unchanged from what we read, so it just roundtrips - for updating we need #2515). This puts LLVM in thirdparty which is added here. All the LLVM code is behind USE_LLVM_DWARF, which is on by default, but off in JS for now, as it increases code size by 20%. This current approach imports the LLVM files directly. This is not how they are intended to be used, so it required a bunch of local changes - more than I expected actually, for the platform-specific stuff. For now this seems to work, so it may be good enough, but in the long term we may want to switch to linking against libllvm. A downside to doing that is that binaryen users would need to have an LLVM build, and even in the waterfall builds we'd have a problem - while we ship LLVM there anyhow, we constantly update it, which means that binaryen would need to be on latest llvm all the time too (which otherwise, given DWARF is quite stable, we might not need to constantly update). An even larger issue is that as I did this work I learned about how DWARF works in LLVM, and while the reading code is easy to reuse, the writing code is trickier. The main code path is heavily integrated with the MC layer, which we don't have - we might want to create a "fake MC layer" for that, but it sounds hard. Instead, there is the YAML path which is used mostly for testing, and which can convert DWARF to and from YAML and from binary. Using the non-YAML parts there, we can convert binary DWARF to the YAML layer's nice Info data, then convert that to binary. This works, however, this is not the path LLVM uses normally, and it supports only some basic DWARF sections - I had to add ranges support, in fact. So if we need more complex things, we may end up needing to use the MC layer approach, or consider some other DWARF library. However, hopefully that should not affect the core binaryen code which just calls a library for DWARF stuff. Helps #2400
Diffstat (limited to 'third_party/llvm-project/DWARFDebugLoc.cpp')
-rw-r--r--third_party/llvm-project/DWARFDebugLoc.cpp326
1 files changed, 326 insertions, 0 deletions
diff --git a/third_party/llvm-project/DWARFDebugLoc.cpp b/third_party/llvm-project/DWARFDebugLoc.cpp
new file mode 100644
index 000000000..e0faaed03
--- /dev/null
+++ b/third_party/llvm-project/DWARFDebugLoc.cpp
@@ -0,0 +1,326 @@
+//===- DWARFDebugLoc.cpp --------------------------------------------------===//
+//
+// 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 "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/BinaryFormat/Dwarf.h"
+#include "llvm/DebugInfo/DWARF/DWARFContext.h"
+#include "llvm/DebugInfo/DWARF/DWARFExpression.h"
+#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
+#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
+#include "llvm/Support/Compiler.h"
+#include "llvm/Support/Format.h"
+#include "llvm/Support/WithColor.h"
+#include "llvm/Support/raw_ostream.h"
+#include <algorithm>
+#include <cinttypes>
+#include <cstdint>
+
+using namespace llvm;
+
+// When directly dumping the .debug_loc without a compile unit, we have to guess
+// at the DWARF version. This only affects DW_OP_call_ref, which is a rare
+// expression that LLVM doesn't produce. Guessing the wrong version means we
+// won't be able to pretty print expressions in DWARF2 binaries produced by
+// non-LLVM tools.
+static void dumpExpression(raw_ostream &OS, ArrayRef<uint8_t> Data,
+ bool IsLittleEndian, unsigned AddressSize,
+ const MCRegisterInfo *MRI, DWARFUnit *U) {
+ DWARFDataExtractor Extractor(toStringRef(Data), IsLittleEndian, AddressSize);
+ DWARFExpression(Extractor, dwarf::DWARF_VERSION, AddressSize).print(OS, MRI, U);
+}
+
+void DWARFDebugLoc::LocationList::dump(raw_ostream &OS, uint64_t BaseAddress,
+ bool IsLittleEndian,
+ unsigned AddressSize,
+ const MCRegisterInfo *MRI, DWARFUnit *U,
+ DIDumpOptions DumpOpts,
+ unsigned Indent) const {
+ for (const Entry &E : Entries) {
+ OS << '\n';
+ OS.indent(Indent);
+ OS << format("[0x%*.*" PRIx64 ", ", AddressSize * 2, AddressSize * 2,
+ BaseAddress + E.Begin);
+ OS << format(" 0x%*.*" PRIx64 ")", AddressSize * 2, AddressSize * 2,
+ BaseAddress + E.End);
+ OS << ": ";
+
+ dumpExpression(OS, E.Loc, IsLittleEndian, AddressSize, MRI, U);
+ }
+}
+
+DWARFDebugLoc::LocationList const *
+DWARFDebugLoc::getLocationListAtOffset(uint64_t Offset) const {
+ auto It = partition_point(
+ Locations, [=](const LocationList &L) { return L.Offset < Offset; });
+ if (It != Locations.end() && It->Offset == Offset)
+ return &(*It);
+ return nullptr;
+}
+
+void DWARFDebugLoc::dump(raw_ostream &OS, const MCRegisterInfo *MRI, DIDumpOptions DumpOpts,
+ Optional<uint64_t> Offset) const {
+ auto DumpLocationList = [&](const LocationList &L) {
+ OS << format("0x%8.8" PRIx64 ": ", L.Offset);
+ L.dump(OS, 0, IsLittleEndian, AddressSize, MRI, nullptr, DumpOpts, 12);
+ OS << "\n";
+ };
+
+ if (Offset) {
+ if (auto *L = getLocationListAtOffset(*Offset))
+ DumpLocationList(*L);
+ return;
+ }
+
+ for (const LocationList &L : Locations) {
+ DumpLocationList(L);
+ if (&L != &Locations.back())
+ OS << '\n';
+ }
+}
+
+Expected<DWARFDebugLoc::LocationList>
+DWARFDebugLoc::parseOneLocationList(const DWARFDataExtractor &Data,
+ uint64_t *Offset) {
+ LocationList LL;
+ LL.Offset = *Offset;
+ AddressSize = Data.getAddressSize();
+ DataExtractor::Cursor C(*Offset);
+
+ // 2.6.2 Location Lists
+ // A location list entry consists of:
+ while (true) {
+ Entry E;
+
+ // 1. A beginning address offset. ...
+ E.Begin = Data.getRelocatedAddress(C);
+
+ // 2. An ending address offset. ...
+ E.End = Data.getRelocatedAddress(C);
+
+ if (Error Err = C.takeError())
+ return std::move(Err);
+
+ // The end of any given location list is marked by an end of list entry,
+ // which consists of a 0 for the beginning address offset and a 0 for the
+ // ending address offset.
+ if (E.Begin == 0 && E.End == 0) {
+ *Offset = C.tell();
+ return LL;
+ }
+
+ if (E.Begin != (AddressSize == 4 ? -1U : -1ULL)) {
+ unsigned Bytes = Data.getU16(C);
+ // A single location description describing the location of the object...
+ Data.getU8(C, E.Loc, Bytes);
+ }
+
+ LL.Entries.push_back(std::move(E));
+ }
+}
+
+void DWARFDebugLoc::parse(const DWARFDataExtractor &data) {
+ IsLittleEndian = data.isLittleEndian();
+ AddressSize = data.getAddressSize();
+
+ uint64_t Offset = 0;
+ while (Offset < data.getData().size()) {
+ if (auto LL = parseOneLocationList(data, &Offset))
+ Locations.push_back(std::move(*LL));
+ else {
+ logAllUnhandledErrors(LL.takeError(), WithColor::error());
+ break;
+ }
+ }
+}
+
+Error DWARFDebugLoclists::visitLocationList(
+ const DWARFDataExtractor &Data, uint64_t *Offset, uint16_t Version,
+ llvm::function_ref<bool(const Entry &)> F) {
+
+ DataExtractor::Cursor C(*Offset);
+ bool Continue = true;
+ while (Continue) {
+ Entry E;
+ E.Offset = C.tell();
+ E.Kind = Data.getU8(C);
+ switch (E.Kind) {
+ case dwarf::DW_LLE_end_of_list:
+ break;
+ case dwarf::DW_LLE_base_addressx:
+ E.Value0 = Data.getULEB128(C);
+ break;
+ case dwarf::DW_LLE_startx_length:
+ E.Value0 = Data.getULEB128(C);
+ // Pre-DWARF 5 has different interpretation of the length field. We have
+ // to support both pre- and standartized styles for the compatibility.
+ if (Version < 5)
+ E.Value1 = Data.getU32(C);
+ else
+ E.Value1 = Data.getULEB128(C);
+ break;
+ case dwarf::DW_LLE_offset_pair:
+ E.Value0 = Data.getULEB128(C);
+ E.Value1 = Data.getULEB128(C);
+ break;
+ case dwarf::DW_LLE_base_address:
+ E.Value0 = Data.getRelocatedAddress(C);
+ break;
+ case dwarf::DW_LLE_start_length:
+ E.Value0 = Data.getRelocatedAddress(C);
+ E.Value1 = Data.getULEB128(C);
+ break;
+ case dwarf::DW_LLE_startx_endx:
+ case dwarf::DW_LLE_default_location:
+ case dwarf::DW_LLE_start_end:
+ default:
+ cantFail(C.takeError());
+ return createStringError(errc::illegal_byte_sequence,
+ "LLE of kind %x not supported", (int)E.Kind);
+ }
+
+ if (E.Kind != dwarf::DW_LLE_base_address &&
+ E.Kind != dwarf::DW_LLE_base_addressx &&
+ E.Kind != dwarf::DW_LLE_end_of_list) {
+ unsigned Bytes = Version >= 5 ? Data.getULEB128(C) : Data.getU16(C);
+ // A single location description describing the location of the object...
+ Data.getU8(C, E.Loc, Bytes);
+ }
+
+ if (!C)
+ return C.takeError();
+ Continue = F(E) && E.Kind != dwarf::DW_LLE_end_of_list;
+ }
+ *Offset = C.tell();
+ return Error::success();
+}
+
+bool DWARFDebugLoclists::dumpLocationList(const DWARFDataExtractor &Data,
+ uint64_t *Offset, uint16_t Version,
+ raw_ostream &OS, uint64_t BaseAddr,
+ const MCRegisterInfo *MRI,
+ DWARFUnit *U, DIDumpOptions DumpOpts,
+ unsigned Indent) {
+ size_t MaxEncodingStringLength = 0;
+ if (DumpOpts.Verbose) {
+#define HANDLE_DW_LLE(ID, NAME) \
+ MaxEncodingStringLength = std::max(MaxEncodingStringLength, \
+ dwarf::LocListEncodingString(ID).size());
+#include "llvm/BinaryFormat/Dwarf.def"
+ }
+
+ OS << format("0x%8.8" PRIx64 ": ", *Offset);
+ Error E = visitLocationList(Data, Offset, Version, [&](const Entry &E) {
+ E.dump(OS, BaseAddr, Data.isLittleEndian(), Data.getAddressSize(), MRI, U,
+ DumpOpts, Indent, MaxEncodingStringLength);
+ return true;
+ });
+ if (E) {
+ OS << "\n";
+ OS.indent(Indent);
+ OS << "error: " << toString(std::move(E));
+ return false;
+ }
+ return true;
+}
+
+void DWARFDebugLoclists::Entry::dump(raw_ostream &OS, uint64_t &BaseAddr,
+ bool IsLittleEndian, unsigned AddressSize,
+ const MCRegisterInfo *MRI, DWARFUnit *U,
+ DIDumpOptions DumpOpts, unsigned Indent,
+ size_t MaxEncodingStringLength) const {
+ if (DumpOpts.Verbose) {
+ OS << "\n";
+ OS.indent(Indent);
+ auto EncodingString = dwarf::LocListEncodingString(Kind);
+ // Unsupported encodings should have been reported during parsing.
+ assert(!EncodingString.empty() && "Unknown loclist entry encoding");
+ OS << format("%s%*c", EncodingString.data(),
+ MaxEncodingStringLength - EncodingString.size() + 1, '(');
+ switch (Kind) {
+ case dwarf::DW_LLE_startx_length:
+ case dwarf::DW_LLE_start_length:
+ case dwarf::DW_LLE_offset_pair:
+ OS << format("0x%*.*" PRIx64 ", 0x%*.*" PRIx64, AddressSize * 2,
+ AddressSize * 2, Value0, AddressSize * 2, AddressSize * 2,
+ Value1);
+ break;
+ case dwarf::DW_LLE_base_addressx:
+ case dwarf::DW_LLE_base_address:
+ OS << format("0x%*.*" PRIx64, AddressSize * 2, AddressSize * 2,
+ Value0);
+ break;
+ case dwarf::DW_LLE_end_of_list:
+ break;
+ }
+ OS << ')';
+ }
+ auto PrintPrefix = [&] {
+ OS << "\n";
+ OS.indent(Indent);
+ if (DumpOpts.Verbose)
+ OS << format("%*s", MaxEncodingStringLength, (const char *)"=> ");
+ };
+ switch (Kind) {
+ case dwarf::DW_LLE_startx_length:
+ PrintPrefix();
+ OS << "Addr idx " << Value0 << " (w/ length " << Value1 << "): ";
+ break;
+ case dwarf::DW_LLE_start_length:
+ PrintPrefix();
+ DWARFAddressRange(Value0, Value0 + Value1)
+ .dump(OS, AddressSize, DumpOpts);
+ OS << ": ";
+ break;
+ case dwarf::DW_LLE_offset_pair:
+ PrintPrefix();
+ DWARFAddressRange(BaseAddr + Value0, BaseAddr + Value1)
+ .dump(OS, AddressSize, DumpOpts);
+ OS << ": ";
+ break;
+ case dwarf::DW_LLE_base_addressx:
+ if (!DumpOpts.Verbose)
+ return;
+ break;
+ case dwarf::DW_LLE_end_of_list:
+ if (!DumpOpts.Verbose)
+ return;
+ break;
+ case dwarf::DW_LLE_base_address:
+ BaseAddr = Value0;
+ if (!DumpOpts.Verbose)
+ return;
+ break;
+ default:
+ llvm_unreachable("unreachable locations list kind");
+ }
+
+ dumpExpression(OS, Loc, IsLittleEndian, AddressSize, MRI, U);
+}
+
+void DWARFDebugLoclists::dumpRange(const DWARFDataExtractor &Data,
+ uint64_t StartOffset, uint64_t Size,
+ uint16_t Version, raw_ostream &OS,
+ uint64_t BaseAddr, const MCRegisterInfo *MRI,
+ DIDumpOptions DumpOpts) {
+ if (!Data.isValidOffsetForDataOfSize(StartOffset, Size)) {
+ OS << "Invalid dump range\n";
+ return;
+ }
+ uint64_t Offset = StartOffset;
+ StringRef Separator;
+ bool CanContinue = true;
+ while (CanContinue && Offset < StartOffset + Size) {
+ OS << Separator;
+ Separator = "\n";
+
+ CanContinue = dumpLocationList(Data, &Offset, Version, OS, BaseAddr, MRI,
+ nullptr, DumpOpts, /*Indent=*/12);
+ OS << '\n';
+ }
+}