2017-02-28 07:43:14 +08:00
|
|
|
//===- DWARFDebugLine.cpp -------------------------------------------------===//
|
2011-09-15 10:12:05 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
|
2018-02-24 07:01:06 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2017-02-28 07:43:14 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2017-06-24 05:57:40 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/Dwarf.h"
|
2017-05-03 05:40:47 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
|
2017-02-28 07:43:14 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
|
2011-09-15 10:12:05 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2012-08-30 15:49:50 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2011-09-15 10:12:05 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-09-15 10:19:33 +08:00
|
|
|
#include <algorithm>
|
2017-02-28 07:43:14 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cinttypes>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <utility>
|
|
|
|
|
2011-09-15 10:12:05 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace dwarf;
|
2017-02-28 07:43:14 +08:00
|
|
|
|
2017-06-24 05:57:40 +08:00
|
|
|
using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
|
|
|
|
|
2017-05-03 05:40:47 +08:00
|
|
|
namespace {
|
2017-06-24 05:57:40 +08:00
|
|
|
|
2017-05-03 05:40:47 +08:00
|
|
|
struct ContentDescriptor {
|
|
|
|
dwarf::LineNumberEntryFormat Type;
|
|
|
|
dwarf::Form Form;
|
|
|
|
};
|
2017-06-24 05:57:40 +08:00
|
|
|
|
|
|
|
using ContentDescriptors = SmallVector<ContentDescriptor, 4>;
|
|
|
|
|
2017-05-03 05:40:47 +08:00
|
|
|
} // end anonmyous namespace
|
2011-09-15 10:12:05 +08:00
|
|
|
|
2018-02-24 07:01:06 +08:00
|
|
|
void DWARFDebugLine::ContentTypeTracker::trackContentType(
|
|
|
|
dwarf::LineNumberEntryFormat ContentType) {
|
|
|
|
switch (ContentType) {
|
|
|
|
case dwarf::DW_LNCT_timestamp:
|
|
|
|
HasModTime = true;
|
|
|
|
break;
|
|
|
|
case dwarf::DW_LNCT_size:
|
|
|
|
HasLength = true;
|
|
|
|
break;
|
|
|
|
case dwarf::DW_LNCT_MD5:
|
|
|
|
HasMD5 = true;
|
|
|
|
break;
|
|
|
|
case dwarf::DW_LNCT_LLVM_source:
|
|
|
|
HasSource = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// We only care about values we consider optional, and new values may be
|
|
|
|
// added in the vendor extension range, so we do not match exhaustively.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:09:37 +08:00
|
|
|
DWARFDebugLine::Prologue::Prologue() { clear(); }
|
2014-04-30 05:28:13 +08:00
|
|
|
|
|
|
|
void DWARFDebugLine::Prologue::clear() {
|
2017-06-27 02:43:01 +08:00
|
|
|
TotalLength = PrologueLength = 0;
|
|
|
|
SegSelectorSize = 0;
|
2014-04-30 05:28:13 +08:00
|
|
|
MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
|
|
|
|
OpcodeBase = 0;
|
2017-06-27 02:43:01 +08:00
|
|
|
FormParams = DWARFFormParams({0, 0, DWARF32});
|
2018-02-24 07:01:06 +08:00
|
|
|
ContentTypes = ContentTypeTracker();
|
2014-04-30 05:28:13 +08:00
|
|
|
StandardOpcodeLengths.clear();
|
|
|
|
IncludeDirectories.clear();
|
|
|
|
FileNames.clear();
|
|
|
|
}
|
|
|
|
|
2018-02-06 04:43:15 +08:00
|
|
|
void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
|
|
|
|
DIDumpOptions DumpOptions) const {
|
2011-09-15 10:12:05 +08:00
|
|
|
OS << "Line table prologue:\n"
|
2015-05-28 23:38:17 +08:00
|
|
|
<< format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength)
|
2017-06-27 02:43:01 +08:00
|
|
|
<< format(" version: %u\n", getVersion());
|
|
|
|
if (getVersion() >= 5)
|
|
|
|
OS << format(" address_size: %u\n", getAddressSize())
|
|
|
|
<< format(" seg_select_size: %u\n", SegSelectorSize);
|
|
|
|
OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
|
2014-02-25 07:58:54 +08:00
|
|
|
<< format(" min_inst_length: %u\n", MinInstLength)
|
2017-06-27 02:43:01 +08:00
|
|
|
<< format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
|
2014-02-25 07:58:54 +08:00
|
|
|
<< format(" default_is_stmt: %u\n", DefaultIsStmt)
|
|
|
|
<< format(" line_base: %i\n", LineBase)
|
|
|
|
<< format(" line_range: %u\n", LineRange)
|
|
|
|
<< format(" opcode_base: %u\n", OpcodeBase);
|
2011-09-15 10:12:05 +08:00
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
|
2016-10-05 13:59:29 +08:00
|
|
|
OS << format("standard_opcode_lengths[%s] = %u\n",
|
2017-05-02 07:27:55 +08:00
|
|
|
LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
|
2011-09-15 10:12:05 +08:00
|
|
|
|
2018-01-19 04:33:35 +08:00
|
|
|
if (!IncludeDirectories.empty()) {
|
|
|
|
// DWARF v5 starts directory indexes at 0.
|
|
|
|
uint32_t DirBase = getVersion() >= 5 ? 0 : 1;
|
2018-02-06 04:43:15 +08:00
|
|
|
for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) {
|
|
|
|
OS << format("include_directories[%3u] = ", I + DirBase);
|
|
|
|
IncludeDirectories[I].dump(OS, DumpOptions);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
2018-01-19 04:33:35 +08:00
|
|
|
}
|
2011-09-15 10:12:05 +08:00
|
|
|
|
|
|
|
if (!FileNames.empty()) {
|
2018-02-09 07:08:02 +08:00
|
|
|
// DWARF v5 starts file indexes at 0.
|
|
|
|
uint32_t FileBase = getVersion() >= 5 ? 0 : 1;
|
2017-05-02 07:27:55 +08:00
|
|
|
for (uint32_t I = 0; I != FileNames.size(); ++I) {
|
|
|
|
const FileNameEntry &FileEntry = FileNames[I];
|
2018-02-24 07:01:06 +08:00
|
|
|
OS << format("file_names[%3u]:\n", I + FileBase);
|
|
|
|
OS << " name: ";
|
2018-02-06 04:43:15 +08:00
|
|
|
FileEntry.Name.dump(OS, DumpOptions);
|
2018-02-24 07:01:06 +08:00
|
|
|
OS << '\n'
|
|
|
|
<< format(" dir_index: %" PRIu64 "\n", FileEntry.DirIdx);
|
|
|
|
if (ContentTypes.HasMD5)
|
|
|
|
OS << " md5_checksum: " << FileEntry.Checksum.digest() << '\n';
|
|
|
|
if (ContentTypes.HasModTime)
|
|
|
|
OS << format(" mod_time: 0x%8.8" PRIx64 "\n", FileEntry.ModTime);
|
|
|
|
if (ContentTypes.HasLength)
|
|
|
|
OS << format(" length: 0x%8.8" PRIx64 "\n", FileEntry.Length);
|
|
|
|
if (ContentTypes.HasSource) {
|
|
|
|
OS << " source: ";
|
|
|
|
FileEntry.Source.dump(OS, DumpOptions);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
2011-09-15 10:12:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 05:40:47 +08:00
|
|
|
// Parse v2-v4 directory and file tables.
|
|
|
|
static void
|
2017-06-30 00:52:08 +08:00
|
|
|
parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
|
|
|
|
uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
|
2018-02-24 07:01:06 +08:00
|
|
|
DWARFDebugLine::ContentTypeTracker &ContentTypes,
|
2018-02-06 04:43:15 +08:00
|
|
|
std::vector<DWARFFormValue> &IncludeDirectories,
|
2017-05-03 05:40:47 +08:00
|
|
|
std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
|
|
|
|
while (*OffsetPtr < EndPrologueOffset) {
|
|
|
|
StringRef S = DebugLineData.getCStrRef(OffsetPtr);
|
|
|
|
if (S.empty())
|
|
|
|
break;
|
2018-02-06 04:43:15 +08:00
|
|
|
DWARFFormValue Dir(dwarf::DW_FORM_string);
|
|
|
|
Dir.setPValue(S.data());
|
|
|
|
IncludeDirectories.push_back(Dir);
|
2017-05-03 05:40:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (*OffsetPtr < EndPrologueOffset) {
|
|
|
|
StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
|
|
|
|
if (Name.empty())
|
|
|
|
break;
|
|
|
|
DWARFDebugLine::FileNameEntry FileEntry;
|
2018-02-06 04:43:15 +08:00
|
|
|
FileEntry.Name.setForm(dwarf::DW_FORM_string);
|
|
|
|
FileEntry.Name.setPValue(Name.data());
|
2017-05-03 05:40:47 +08:00
|
|
|
FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
|
|
|
|
FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
|
|
|
|
FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
|
|
|
|
FileNames.push_back(FileEntry);
|
|
|
|
}
|
2018-02-24 07:01:06 +08:00
|
|
|
|
|
|
|
ContentTypes.HasModTime = true;
|
|
|
|
ContentTypes.HasLength = true;
|
2017-05-03 05:40:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse v5 directory/file entry content descriptions.
|
|
|
|
// Returns the descriptors, or an empty vector if we did not find a path or
|
|
|
|
// ran off the end of the prologue.
|
|
|
|
static ContentDescriptors
|
2018-02-24 07:01:06 +08:00
|
|
|
parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t
|
|
|
|
*OffsetPtr, uint64_t EndPrologueOffset, DWARFDebugLine::ContentTypeTracker
|
|
|
|
*ContentTypes) {
|
2017-05-03 05:40:47 +08:00
|
|
|
ContentDescriptors Descriptors;
|
|
|
|
int FormatCount = DebugLineData.getU8(OffsetPtr);
|
|
|
|
bool HasPath = false;
|
|
|
|
for (int I = 0; I != FormatCount; ++I) {
|
|
|
|
if (*OffsetPtr >= EndPrologueOffset)
|
|
|
|
return ContentDescriptors();
|
|
|
|
ContentDescriptor Descriptor;
|
|
|
|
Descriptor.Type =
|
|
|
|
dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
|
|
|
|
Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
|
|
|
|
if (Descriptor.Type == dwarf::DW_LNCT_path)
|
|
|
|
HasPath = true;
|
2018-02-24 07:01:06 +08:00
|
|
|
if (ContentTypes)
|
|
|
|
ContentTypes->trackContentType(Descriptor.Type);
|
2017-05-03 05:40:47 +08:00
|
|
|
Descriptors.push_back(Descriptor);
|
|
|
|
}
|
|
|
|
return HasPath ? Descriptors : ContentDescriptors();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-06-30 00:52:08 +08:00
|
|
|
parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
|
|
|
|
uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
|
2018-02-24 07:01:06 +08:00
|
|
|
const DWARFFormParams &FormParams, const DWARFContext
|
|
|
|
&Ctx, const DWARFUnit *U,
|
|
|
|
DWARFDebugLine::ContentTypeTracker &ContentTypes,
|
2018-02-06 04:43:15 +08:00
|
|
|
std::vector<DWARFFormValue> &IncludeDirectories,
|
2017-05-03 05:40:47 +08:00
|
|
|
std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
|
|
|
|
// Get the directory entry description.
|
|
|
|
ContentDescriptors DirDescriptors =
|
2017-12-19 03:08:35 +08:00
|
|
|
parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr);
|
2017-05-03 05:40:47 +08:00
|
|
|
if (DirDescriptors.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Get the directory entries, according to the format described above.
|
|
|
|
int DirEntryCount = DebugLineData.getU8(OffsetPtr);
|
|
|
|
for (int I = 0; I != DirEntryCount; ++I) {
|
|
|
|
if (*OffsetPtr >= EndPrologueOffset)
|
|
|
|
return false;
|
|
|
|
for (auto Descriptor : DirDescriptors) {
|
|
|
|
DWARFFormValue Value(Descriptor.Form);
|
|
|
|
switch (Descriptor.Type) {
|
|
|
|
case DW_LNCT_path:
|
2018-01-30 04:57:43 +08:00
|
|
|
if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
|
2017-05-03 05:40:47 +08:00
|
|
|
return false;
|
2018-02-06 04:43:15 +08:00
|
|
|
IncludeDirectories.push_back(Value);
|
2017-05-03 05:40:47 +08:00
|
|
|
break;
|
|
|
|
default:
|
2017-06-27 02:43:01 +08:00
|
|
|
if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
|
2017-05-03 05:40:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the file entry description.
|
|
|
|
ContentDescriptors FileDescriptors =
|
2018-02-24 07:01:06 +08:00
|
|
|
parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset,
|
|
|
|
&ContentTypes);
|
2017-05-03 05:40:47 +08:00
|
|
|
if (FileDescriptors.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Get the file entries, according to the format described above.
|
|
|
|
int FileEntryCount = DebugLineData.getU8(OffsetPtr);
|
|
|
|
for (int I = 0; I != FileEntryCount; ++I) {
|
|
|
|
if (*OffsetPtr >= EndPrologueOffset)
|
|
|
|
return false;
|
|
|
|
DWARFDebugLine::FileNameEntry FileEntry;
|
|
|
|
for (auto Descriptor : FileDescriptors) {
|
|
|
|
DWARFFormValue Value(Descriptor.Form);
|
2018-01-30 04:57:43 +08:00
|
|
|
if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
|
2017-05-03 05:40:47 +08:00
|
|
|
return false;
|
|
|
|
switch (Descriptor.Type) {
|
|
|
|
case DW_LNCT_path:
|
2018-02-06 04:43:15 +08:00
|
|
|
FileEntry.Name = Value;
|
2017-05-03 05:40:47 +08:00
|
|
|
break;
|
2018-02-24 07:01:06 +08:00
|
|
|
case DW_LNCT_LLVM_source:
|
|
|
|
FileEntry.Source = Value;
|
|
|
|
break;
|
2017-05-03 05:40:47 +08:00
|
|
|
case DW_LNCT_directory_index:
|
|
|
|
FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
|
|
|
|
break;
|
|
|
|
case DW_LNCT_timestamp:
|
|
|
|
FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
|
|
|
|
break;
|
|
|
|
case DW_LNCT_size:
|
|
|
|
FileEntry.Length = Value.getAsUnsignedConstant().getValue();
|
|
|
|
break;
|
2017-12-19 03:08:35 +08:00
|
|
|
case DW_LNCT_MD5:
|
|
|
|
assert(Value.getAsBlock().getValue().size() == 16);
|
|
|
|
std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16,
|
|
|
|
FileEntry.Checksum.Bytes.begin());
|
|
|
|
break;
|
2017-05-03 05:40:47 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FileNames.push_back(FileEntry);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-30 00:52:08 +08:00
|
|
|
bool DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
|
2018-01-30 04:57:43 +08:00
|
|
|
uint32_t *OffsetPtr,
|
|
|
|
const DWARFContext &Ctx,
|
|
|
|
const DWARFUnit *U) {
|
2017-05-02 07:27:55 +08:00
|
|
|
const uint64_t PrologueOffset = *OffsetPtr;
|
2014-04-30 05:28:13 +08:00
|
|
|
|
|
|
|
clear();
|
2017-05-02 07:27:55 +08:00
|
|
|
TotalLength = DebugLineData.getU32(OffsetPtr);
|
2015-05-28 23:38:17 +08:00
|
|
|
if (TotalLength == UINT32_MAX) {
|
2017-06-27 02:43:01 +08:00
|
|
|
FormParams.Format = dwarf::DWARF64;
|
2017-05-02 07:27:55 +08:00
|
|
|
TotalLength = DebugLineData.getU64(OffsetPtr);
|
2017-06-27 02:43:01 +08:00
|
|
|
} else if (TotalLength >= 0xffffff00) {
|
2015-05-28 23:38:17 +08:00
|
|
|
return false;
|
|
|
|
}
|
2017-06-27 02:43:01 +08:00
|
|
|
FormParams.Version = DebugLineData.getU16(OffsetPtr);
|
|
|
|
if (getVersion() < 2)
|
2014-04-30 05:28:13 +08:00
|
|
|
return false;
|
|
|
|
|
2017-06-27 02:43:01 +08:00
|
|
|
if (getVersion() >= 5) {
|
|
|
|
FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
|
2017-11-22 23:33:17 +08:00
|
|
|
assert((DebugLineData.getAddressSize() == 0 ||
|
|
|
|
DebugLineData.getAddressSize() == getAddressSize()) &&
|
2017-06-27 02:43:01 +08:00
|
|
|
"Line table header and data extractor disagree");
|
2017-05-03 05:40:47 +08:00
|
|
|
SegSelectorSize = DebugLineData.getU8(OffsetPtr);
|
|
|
|
}
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
|
|
|
|
const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
|
|
|
|
MinInstLength = DebugLineData.getU8(OffsetPtr);
|
2017-06-27 02:43:01 +08:00
|
|
|
if (getVersion() >= 4)
|
2017-05-02 07:27:55 +08:00
|
|
|
MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
|
|
|
|
DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
|
|
|
|
LineBase = DebugLineData.getU8(OffsetPtr);
|
|
|
|
LineRange = DebugLineData.getU8(OffsetPtr);
|
|
|
|
OpcodeBase = DebugLineData.getU8(OffsetPtr);
|
2014-04-30 05:28:13 +08:00
|
|
|
|
|
|
|
StandardOpcodeLengths.reserve(OpcodeBase - 1);
|
2017-05-02 07:27:55 +08:00
|
|
|
for (uint32_t I = 1; I < OpcodeBase; ++I) {
|
|
|
|
uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
|
|
|
|
StandardOpcodeLengths.push_back(OpLen);
|
2014-04-30 05:28:13 +08:00
|
|
|
}
|
|
|
|
|
2017-06-27 02:43:01 +08:00
|
|
|
if (getVersion() >= 5) {
|
2017-05-03 05:40:47 +08:00
|
|
|
if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
|
2018-02-24 07:01:06 +08:00
|
|
|
FormParams, Ctx, U, ContentTypes,
|
|
|
|
IncludeDirectories, FileNames)) {
|
2017-05-03 05:40:47 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"warning: parsing line table prologue at 0x%8.8" PRIx64
|
|
|
|
" found an invalid directory or file table description at"
|
|
|
|
" 0x%8.8" PRIx64 "\n", PrologueOffset, (uint64_t)*OffsetPtr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
|
2018-02-24 07:01:06 +08:00
|
|
|
ContentTypes, IncludeDirectories, FileNames);
|
2014-04-30 05:28:13 +08:00
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
if (*OffsetPtr != EndPrologueOffset) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"warning: parsing line table prologue at 0x%8.8" PRIx64
|
|
|
|
" should have ended at 0x%8.8" PRIx64
|
|
|
|
" but it ended at 0x%8.8" PRIx64 "\n",
|
|
|
|
PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
|
2014-04-30 05:28:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
|
2014-04-30 05:28:13 +08:00
|
|
|
|
2011-09-15 10:12:05 +08:00
|
|
|
void DWARFDebugLine::Row::postAppend() {
|
|
|
|
BasicBlock = false;
|
|
|
|
PrologueEnd = false;
|
|
|
|
EpilogueBegin = false;
|
|
|
|
}
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
|
2011-09-15 10:12:05 +08:00
|
|
|
Address = 0;
|
|
|
|
Line = 1;
|
|
|
|
Column = 0;
|
|
|
|
File = 1;
|
|
|
|
Isa = 0;
|
2014-02-15 03:27:53 +08:00
|
|
|
Discriminator = 0;
|
2017-05-02 07:27:55 +08:00
|
|
|
IsStmt = DefaultIsStmt;
|
2011-09-15 10:12:05 +08:00
|
|
|
BasicBlock = false;
|
|
|
|
EndSequence = false;
|
|
|
|
PrologueEnd = false;
|
|
|
|
EpilogueBegin = false;
|
|
|
|
}
|
|
|
|
|
2017-05-03 06:48:52 +08:00
|
|
|
void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
|
|
|
|
OS << "Address Line Column File ISA Discriminator Flags\n"
|
|
|
|
<< "------------------ ------ ------ ------ --- ------------- "
|
|
|
|
"-------------\n";
|
|
|
|
}
|
|
|
|
|
2011-09-15 10:12:05 +08:00
|
|
|
void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
|
2011-11-05 16:57:40 +08:00
|
|
|
OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
|
2014-02-15 03:27:53 +08:00
|
|
|
<< format(" %6u %3u %13u ", File, Isa, Discriminator)
|
2016-04-29 06:09:37 +08:00
|
|
|
<< (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
|
2011-09-15 10:12:05 +08:00
|
|
|
<< (PrologueEnd ? " prologue_end" : "")
|
|
|
|
<< (EpilogueBegin ? " epilogue_begin" : "")
|
2016-04-29 06:09:37 +08:00
|
|
|
<< (EndSequence ? " end_sequence" : "") << '\n';
|
2011-09-15 10:12:05 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 06:09:37 +08:00
|
|
|
DWARFDebugLine::Sequence::Sequence() { reset(); }
|
2014-04-30 05:28:13 +08:00
|
|
|
|
|
|
|
void DWARFDebugLine::Sequence::reset() {
|
|
|
|
LowPC = 0;
|
|
|
|
HighPC = 0;
|
|
|
|
FirstRowIndex = 0;
|
|
|
|
LastRowIndex = 0;
|
|
|
|
Empty = true;
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:09:37 +08:00
|
|
|
DWARFDebugLine::LineTable::LineTable() { clear(); }
|
2014-04-30 05:28:13 +08:00
|
|
|
|
2018-02-06 04:43:15 +08:00
|
|
|
void DWARFDebugLine::LineTable::dump(raw_ostream &OS,
|
|
|
|
DIDumpOptions DumpOptions) const {
|
|
|
|
Prologue.dump(OS, DumpOptions);
|
2011-09-15 10:12:05 +08:00
|
|
|
OS << '\n';
|
|
|
|
|
|
|
|
if (!Rows.empty()) {
|
2017-05-03 06:48:52 +08:00
|
|
|
Row::dumpTableHeader(OS);
|
2014-03-13 15:52:54 +08:00
|
|
|
for (const Row &R : Rows) {
|
|
|
|
R.dump(OS);
|
|
|
|
}
|
2011-09-15 10:12:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 05:28:13 +08:00
|
|
|
void DWARFDebugLine::LineTable::clear() {
|
|
|
|
Prologue.clear();
|
|
|
|
Rows.clear();
|
|
|
|
Sequences.clear();
|
|
|
|
}
|
|
|
|
|
2014-04-30 08:09:19 +08:00
|
|
|
DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
|
2017-06-24 05:57:40 +08:00
|
|
|
: LineTable(LT) {
|
2014-04-30 08:09:19 +08:00
|
|
|
resetRowAndSequence();
|
|
|
|
}
|
2011-09-15 11:41:51 +08:00
|
|
|
|
2014-04-30 08:09:19 +08:00
|
|
|
void DWARFDebugLine::ParsingState::resetRowAndSequence() {
|
|
|
|
Row.reset(LineTable->Prologue.DefaultIsStmt);
|
|
|
|
Sequence.reset();
|
2011-09-15 10:12:05 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) {
|
2014-04-30 08:09:19 +08:00
|
|
|
if (Sequence.Empty) {
|
|
|
|
// Record the beginning of instruction sequence.
|
|
|
|
Sequence.Empty = false;
|
|
|
|
Sequence.LowPC = Row.Address;
|
|
|
|
Sequence.FirstRowIndex = RowNumber;
|
2012-08-07 19:46:57 +08:00
|
|
|
}
|
2014-04-30 08:09:19 +08:00
|
|
|
++RowNumber;
|
|
|
|
LineTable->appendRow(Row);
|
|
|
|
if (Row.EndSequence) {
|
|
|
|
// Record the end of instruction sequence.
|
|
|
|
Sequence.HighPC = Row.Address;
|
|
|
|
Sequence.LastRowIndex = RowNumber;
|
|
|
|
if (Sequence.isValid())
|
|
|
|
LineTable->appendSequence(Sequence);
|
|
|
|
Sequence.reset();
|
2012-08-07 19:46:57 +08:00
|
|
|
}
|
2014-04-30 08:09:19 +08:00
|
|
|
Row.postAppend();
|
2011-09-15 10:12:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const DWARFDebugLine::LineTable *
|
2017-05-02 07:27:55 +08:00
|
|
|
DWARFDebugLine::getLineTable(uint32_t Offset) const {
|
|
|
|
LineTableConstIter Pos = LineTableMap.find(Offset);
|
|
|
|
if (Pos != LineTableMap.end())
|
|
|
|
return &Pos->second;
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2011-09-15 10:12:05 +08:00
|
|
|
}
|
|
|
|
|
2011-09-16 04:43:18 +08:00
|
|
|
const DWARFDebugLine::LineTable *
|
2017-11-22 23:48:30 +08:00
|
|
|
DWARFDebugLine::getOrParseLineTable(DWARFDataExtractor &DebugLineData,
|
2018-01-30 04:57:43 +08:00
|
|
|
uint32_t Offset, const DWARFContext &Ctx,
|
|
|
|
const DWARFUnit *U) {
|
[DWARF] Don't attempt to parse line tables at invalid offsets
Whilst working on improvements to the error handling of the debug line
parsing code, I noticed that if an invalid offset were to be specified
in a call to getOrParseLineTable(), an entry in the LineTableMap would
still be created, even if the offset was not within the section range.
The immediate parsing attempt afterwards would fail (it would end up
getting a version of 0), and thereafter, any subsequent calls to
getOrParseLineTable or getLineTable would return the default-
constructed, invalid line table. In reality, we shouldn't even attempt
to parse this table, and we should always return a nullptr from these
two functions for this situation.
I have tested this via a unit test, which required some new framework
for unit testing debug line. My plan is to add quite a few more unit
tests for the new error reporting mechanism that will follow shortly,
hence the reason why the supporting code for the tests are written the
way they are - I intend to extend the DwarfGenerator class to support
generating debug line. At that point, I'll make sure that there are a
few positive test cases for this and the parsing code too.
Differential Revision: https://reviews.llvm.org/D44200
Reviewers: JDevlieghere, aprantl
llvm-svn: 326995
2018-03-08 18:53:34 +08:00
|
|
|
if (!DebugLineData.isValidOffset(Offset))
|
|
|
|
return nullptr;
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
std::pair<LineTableIter, bool> Pos =
|
|
|
|
LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
|
|
|
|
LineTable *LT = &Pos.first->second;
|
|
|
|
if (Pos.second) {
|
2018-01-30 04:57:43 +08:00
|
|
|
if (!LT->parse(DebugLineData, &Offset, Ctx, U))
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2011-09-16 04:43:18 +08:00
|
|
|
}
|
2014-04-30 08:09:19 +08:00
|
|
|
return LT;
|
2011-09-16 04:43:18 +08:00
|
|
|
}
|
|
|
|
|
2017-11-22 23:48:30 +08:00
|
|
|
bool DWARFDebugLine::LineTable::parse(DWARFDataExtractor &DebugLineData,
|
2018-01-30 04:57:43 +08:00
|
|
|
uint32_t *OffsetPtr,
|
|
|
|
const DWARFContext &Ctx,
|
|
|
|
const DWARFUnit *U, raw_ostream *OS) {
|
2017-05-02 07:27:55 +08:00
|
|
|
const uint32_t DebugLineOffset = *OffsetPtr;
|
2011-09-15 10:12:05 +08:00
|
|
|
|
2014-04-30 08:09:19 +08:00
|
|
|
clear();
|
2011-09-15 10:12:05 +08:00
|
|
|
|
2018-01-30 04:57:43 +08:00
|
|
|
if (!Prologue.parse(DebugLineData, OffsetPtr, Ctx, U)) {
|
2011-09-15 10:12:05 +08:00
|
|
|
// Restore our offset and return false to indicate failure!
|
2017-05-02 07:27:55 +08:00
|
|
|
*OffsetPtr = DebugLineOffset;
|
2011-09-15 10:12:05 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-06 04:43:15 +08:00
|
|
|
if (OS) {
|
|
|
|
// The presence of OS signals verbose dumping.
|
|
|
|
DIDumpOptions DumpOptions;
|
|
|
|
DumpOptions.Verbose = true;
|
|
|
|
Prologue.dump(*OS, DumpOptions);
|
|
|
|
}
|
2017-09-22 04:15:30 +08:00
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
const uint32_t EndOffset =
|
|
|
|
DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
|
2011-09-15 10:12:05 +08:00
|
|
|
|
2017-11-22 23:48:30 +08:00
|
|
|
// See if we should tell the data extractor the address size.
|
|
|
|
if (DebugLineData.getAddressSize() == 0)
|
|
|
|
DebugLineData.setAddressSize(Prologue.getAddressSize());
|
|
|
|
else
|
|
|
|
assert(Prologue.getAddressSize() == 0 ||
|
|
|
|
Prologue.getAddressSize() == DebugLineData.getAddressSize());
|
|
|
|
|
2014-04-30 08:09:19 +08:00
|
|
|
ParsingState State(this);
|
2011-09-16 05:59:13 +08:00
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
while (*OffsetPtr < EndOffset) {
|
2017-09-22 04:15:30 +08:00
|
|
|
if (OS)
|
|
|
|
*OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
|
2011-09-15 10:12:05 +08:00
|
|
|
|
2017-09-22 04:15:30 +08:00
|
|
|
if (OS)
|
|
|
|
*OS << format("%02.02" PRIx8 " ", Opcode);
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
if (Opcode == 0) {
|
2011-09-15 10:12:05 +08:00
|
|
|
// Extended Opcodes always start with a zero opcode followed by
|
|
|
|
// a uleb128 length so you can skip ones you don't know about
|
2017-05-02 07:27:55 +08:00
|
|
|
uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
|
2017-11-22 23:14:49 +08:00
|
|
|
uint32_t ExtOffset = *OffsetPtr;
|
|
|
|
|
|
|
|
// Tolerate zero-length; assume length is correct and soldier on.
|
|
|
|
if (Len == 0) {
|
|
|
|
if (OS)
|
|
|
|
*OS << "Badly formed extended line op (length 0)\n";
|
|
|
|
continue;
|
|
|
|
}
|
2011-09-15 10:12:05 +08:00
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
|
2017-09-22 04:15:30 +08:00
|
|
|
if (OS)
|
|
|
|
*OS << LNExtendedString(SubOpcode);
|
2017-05-02 07:27:55 +08:00
|
|
|
switch (SubOpcode) {
|
2011-09-15 10:12:05 +08:00
|
|
|
case DW_LNE_end_sequence:
|
|
|
|
// Set the end_sequence register of the state machine to true and
|
|
|
|
// append a row to the matrix using the current values of the
|
|
|
|
// state-machine registers. Then reset the registers to the initial
|
|
|
|
// values specified above. Every statement program sequence must end
|
|
|
|
// with a DW_LNE_end_sequence instruction which creates a row whose
|
|
|
|
// address is that of the byte after the last target machine instruction
|
|
|
|
// of the sequence.
|
2014-04-30 08:09:19 +08:00
|
|
|
State.Row.EndSequence = true;
|
2017-05-02 07:27:55 +08:00
|
|
|
State.appendRowToMatrix(*OffsetPtr);
|
2017-09-22 04:15:30 +08:00
|
|
|
if (OS) {
|
|
|
|
*OS << "\n";
|
|
|
|
OS->indent(12);
|
|
|
|
State.Row.dump(*OS);
|
|
|
|
}
|
2014-04-30 08:09:19 +08:00
|
|
|
State.resetRowAndSequence();
|
2011-09-15 10:12:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DW_LNE_set_address:
|
|
|
|
// Takes a single relocatable address as an operand. The size of the
|
|
|
|
// operand is the size appropriate to hold an address on the target
|
|
|
|
// machine. Set the address register to the value given by the
|
|
|
|
// relocatable address. All of the other statement program opcodes
|
|
|
|
// that affect the address register add a delta to it. This instruction
|
|
|
|
// stores a relocatable value into it instead.
|
2017-11-22 23:48:30 +08:00
|
|
|
//
|
|
|
|
// Make sure the extractor knows the address size. If not, infer it
|
|
|
|
// from the size of the operand.
|
|
|
|
if (DebugLineData.getAddressSize() == 0)
|
|
|
|
DebugLineData.setAddressSize(Len - 1);
|
|
|
|
else
|
|
|
|
assert(DebugLineData.getAddressSize() == Len - 1);
|
2017-06-30 00:52:08 +08:00
|
|
|
State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr);
|
2017-09-22 04:15:30 +08:00
|
|
|
if (OS)
|
|
|
|
*OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address);
|
2011-09-15 10:12:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DW_LNE_define_file:
|
|
|
|
// Takes 4 arguments. The first is a null terminated string containing
|
|
|
|
// a source file name. The second is an unsigned LEB128 number
|
|
|
|
// representing the directory index of the directory in which the file
|
|
|
|
// was found. The third is an unsigned LEB128 number representing the
|
|
|
|
// time of last modification of the file. The fourth is an unsigned
|
|
|
|
// LEB128 number representing the length in bytes of the file. The time
|
|
|
|
// and length fields may contain LEB128(0) if the information is not
|
|
|
|
// available.
|
|
|
|
//
|
|
|
|
// The directory index represents an entry in the include_directories
|
|
|
|
// section of the statement program prologue. The index is LEB128(0)
|
|
|
|
// if the file was found in the current directory of the compilation,
|
|
|
|
// LEB128(1) if it was found in the first directory in the
|
|
|
|
// include_directories section, and so on. The directory index is
|
|
|
|
// ignored for file names that represent full path names.
|
|
|
|
//
|
|
|
|
// The files are numbered, starting at 1, in the order in which they
|
|
|
|
// appear; the names in the prologue come before names defined by
|
|
|
|
// the DW_LNE_define_file instruction. These numbers are used in the
|
|
|
|
// the file register of the state machine.
|
|
|
|
{
|
2017-05-02 07:27:55 +08:00
|
|
|
FileNameEntry FileEntry;
|
2018-02-06 04:43:15 +08:00
|
|
|
const char *Name = DebugLineData.getCStr(OffsetPtr);
|
|
|
|
FileEntry.Name.setForm(dwarf::DW_FORM_string);
|
|
|
|
FileEntry.Name.setPValue(Name);
|
2017-05-02 07:27:55 +08:00
|
|
|
FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
|
|
|
|
FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
|
|
|
|
FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
|
|
|
|
Prologue.FileNames.push_back(FileEntry);
|
2017-09-22 04:15:30 +08:00
|
|
|
if (OS)
|
2018-02-06 04:43:15 +08:00
|
|
|
*OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
|
2017-09-22 04:15:30 +08:00
|
|
|
<< format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
|
|
|
|
<< ", length=" << FileEntry.Length << ")";
|
2011-09-15 10:12:05 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-02-15 03:27:53 +08:00
|
|
|
case DW_LNE_set_discriminator:
|
2017-05-02 07:27:55 +08:00
|
|
|
State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
|
2017-09-22 04:15:30 +08:00
|
|
|
if (OS)
|
|
|
|
*OS << " (" << State.Row.Discriminator << ")";
|
2014-02-15 03:27:53 +08:00
|
|
|
break;
|
|
|
|
|
2011-09-15 10:12:05 +08:00
|
|
|
default:
|
2017-11-22 23:14:49 +08:00
|
|
|
if (OS)
|
|
|
|
*OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
|
|
|
|
<< format(" length %" PRIx64, Len);
|
|
|
|
// Len doesn't include the zero opcode byte or the length itself, but
|
|
|
|
// it does include the sub_opcode, so we have to adjust for that.
|
|
|
|
(*OffsetPtr) += Len - 1;
|
2011-09-15 10:12:05 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-11-22 23:14:49 +08:00
|
|
|
// Make sure the stated and parsed lengths are the same.
|
|
|
|
// Otherwise we have an unparseable line-number program.
|
|
|
|
if (*OffsetPtr - ExtOffset != Len) {
|
|
|
|
fprintf(stderr, "Unexpected line op length at offset 0x%8.8" PRIx32
|
|
|
|
" expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32 "\n",
|
|
|
|
ExtOffset, Len, *OffsetPtr - ExtOffset);
|
|
|
|
// Skip the rest of the line-number program.
|
|
|
|
*OffsetPtr = EndOffset;
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-02 07:27:55 +08:00
|
|
|
} else if (Opcode < Prologue.OpcodeBase) {
|
2017-09-22 04:15:30 +08:00
|
|
|
if (OS)
|
|
|
|
*OS << LNStandardString(Opcode);
|
2017-05-02 07:27:55 +08:00
|
|
|
switch (Opcode) {
|
2011-09-15 10:12:05 +08:00
|
|
|
// Standard Opcodes
|
|
|
|
case DW_LNS_copy:
|
|
|
|
// Takes no arguments. Append a row to the matrix using the
|
|
|
|
// current values of the state-machine registers. Then set
|
|
|
|
// the basic_block register to false.
|
2017-05-02 07:27:55 +08:00
|
|
|
State.appendRowToMatrix(*OffsetPtr);
|
2017-09-22 04:15:30 +08:00
|
|
|
if (OS) {
|
|
|
|
*OS << "\n";
|
|
|
|
OS->indent(12);
|
|
|
|
State.Row.dump(*OS);
|
|
|
|
*OS << "\n";
|
|
|
|
}
|
2011-09-15 10:12:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DW_LNS_advance_pc:
|
|
|
|
// Takes a single unsigned LEB128 operand, multiplies it by the
|
|
|
|
// min_inst_length field of the prologue, and adds the
|
|
|
|
// result to the address register of the state machine.
|
2017-09-22 04:15:30 +08:00
|
|
|
{
|
|
|
|
uint64_t AddrOffset =
|
|
|
|
DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
|
|
|
|
State.Row.Address += AddrOffset;
|
|
|
|
if (OS)
|
|
|
|
*OS << " (" << AddrOffset << ")";
|
|
|
|
}
|
2011-09-15 10:12:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DW_LNS_advance_line:
|
|
|
|
// Takes a single signed LEB128 operand and adds that value to
|
|
|
|
// the line register of the state machine.
|
2017-05-02 07:27:55 +08:00
|
|
|
State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
|
2017-09-22 04:15:30 +08:00
|
|
|
if (OS)
|
|
|
|
*OS << " (" << State.Row.Line << ")";
|
2011-09-15 10:12:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DW_LNS_set_file:
|
|
|
|
// Takes a single unsigned LEB128 operand and stores it in the file
|
|
|
|
// register of the state machine.
|
2017-05-02 07:27:55 +08:00
|
|
|
State.Row.File = DebugLineData.getULEB128(OffsetPtr);
|
2017-09-22 04:15:30 +08:00
|
|
|
if (OS)
|
|
|
|
*OS << " (" << State.Row.File << ")";
|
2011-09-15 10:12:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DW_LNS_set_column:
|
|
|
|
// Takes a single unsigned LEB128 operand and stores it in the
|
|
|
|
// column register of the state machine.
|
2017-05-02 07:27:55 +08:00
|
|
|
State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
|
2017-09-22 04:15:30 +08:00
|
|
|
if (OS)
|
|
|
|
*OS << " (" << State.Row.Column << ")";
|
2011-09-15 10:12:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DW_LNS_negate_stmt:
|
|
|
|
// Takes no arguments. Set the is_stmt register of the state
|
|
|
|
// machine to the logical negation of its current value.
|
2014-04-30 08:09:19 +08:00
|
|
|
State.Row.IsStmt = !State.Row.IsStmt;
|
2011-09-15 10:12:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DW_LNS_set_basic_block:
|
|
|
|
// Takes no arguments. Set the basic_block register of the
|
|
|
|
// state machine to true
|
2014-04-30 08:09:19 +08:00
|
|
|
State.Row.BasicBlock = true;
|
2011-09-15 10:12:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DW_LNS_const_add_pc:
|
|
|
|
// Takes no arguments. Add to the address register of the state
|
|
|
|
// machine the address increment value corresponding to special
|
|
|
|
// opcode 255. The motivation for DW_LNS_const_add_pc is this:
|
|
|
|
// when the statement program needs to advance the address by a
|
|
|
|
// small amount, it can use a single special opcode, which occupies
|
|
|
|
// a single byte. When it needs to advance the address by up to
|
|
|
|
// twice the range of the last special opcode, it can use
|
|
|
|
// DW_LNS_const_add_pc followed by a special opcode, for a total
|
|
|
|
// of two bytes. Only if it needs to advance the address by more
|
|
|
|
// than twice that range will it need to use both DW_LNS_advance_pc
|
|
|
|
// and a special opcode, requiring three or more bytes.
|
|
|
|
{
|
2017-05-02 07:27:55 +08:00
|
|
|
uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
|
|
|
|
uint64_t AddrOffset =
|
|
|
|
(AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
|
|
|
|
State.Row.Address += AddrOffset;
|
2017-09-22 04:15:30 +08:00
|
|
|
if (OS)
|
|
|
|
*OS
|
|
|
|
<< format(" (0x%16.16" PRIx64 ")", AddrOffset);
|
2011-09-15 10:12:05 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DW_LNS_fixed_advance_pc:
|
|
|
|
// Takes a single uhalf operand. Add to the address register of
|
|
|
|
// the state machine the value of the (unencoded) operand. This
|
|
|
|
// is the only extended opcode that takes an argument that is not
|
|
|
|
// a variable length number. The motivation for DW_LNS_fixed_advance_pc
|
|
|
|
// is this: existing assemblers cannot emit DW_LNS_advance_pc or
|
|
|
|
// special opcodes because they cannot encode LEB128 numbers or
|
|
|
|
// judge when the computation of a special opcode overflows and
|
|
|
|
// requires the use of DW_LNS_advance_pc. Such assemblers, however,
|
|
|
|
// can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
|
2017-09-22 04:15:30 +08:00
|
|
|
{
|
|
|
|
uint16_t PCOffset = DebugLineData.getU16(OffsetPtr);
|
|
|
|
State.Row.Address += PCOffset;
|
|
|
|
if (OS)
|
|
|
|
*OS
|
|
|
|
<< format(" (0x%16.16" PRIx64 ")", PCOffset);
|
|
|
|
}
|
2011-09-15 10:12:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DW_LNS_set_prologue_end:
|
|
|
|
// Takes no arguments. Set the prologue_end register of the
|
|
|
|
// state machine to true
|
2014-04-30 08:09:19 +08:00
|
|
|
State.Row.PrologueEnd = true;
|
2011-09-15 10:12:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DW_LNS_set_epilogue_begin:
|
|
|
|
// Takes no arguments. Set the basic_block register of the
|
|
|
|
// state machine to true
|
2014-04-30 08:09:19 +08:00
|
|
|
State.Row.EpilogueBegin = true;
|
2011-09-15 10:12:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DW_LNS_set_isa:
|
|
|
|
// Takes a single unsigned LEB128 operand and stores it in the
|
|
|
|
// column register of the state machine.
|
2017-05-02 07:27:55 +08:00
|
|
|
State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
|
2017-09-22 04:15:30 +08:00
|
|
|
if (OS)
|
|
|
|
*OS << " (" << State.Row.Isa << ")";
|
2011-09-15 10:12:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Handle any unknown standard opcodes here. We know the lengths
|
|
|
|
// of such opcodes because they are specified in the prologue
|
|
|
|
// as a multiple of LEB128 operands for each opcode.
|
|
|
|
{
|
2017-05-02 07:27:55 +08:00
|
|
|
assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
|
|
|
|
uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
|
2017-09-22 04:15:30 +08:00
|
|
|
for (uint8_t I = 0; I < OpcodeLength; ++I) {
|
|
|
|
uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
|
|
|
|
if (OS)
|
|
|
|
*OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
|
|
|
|
Value);
|
|
|
|
}
|
2011-09-15 10:12:05 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Special Opcodes
|
|
|
|
|
|
|
|
// A special opcode value is chosen based on the amount that needs
|
|
|
|
// to be added to the line and address registers. The maximum line
|
|
|
|
// increment for a special opcode is the value of the line_base
|
|
|
|
// field in the header, plus the value of the line_range field,
|
|
|
|
// minus 1 (line base + line range - 1). If the desired line
|
|
|
|
// increment is greater than the maximum line increment, a standard
|
2011-10-08 19:22:47 +08:00
|
|
|
// opcode must be used instead of a special opcode. The "address
|
|
|
|
// advance" is calculated by dividing the desired address increment
|
2011-09-15 10:12:05 +08:00
|
|
|
// by the minimum_instruction_length field from the header. The
|
|
|
|
// special opcode is then calculated using the following formula:
|
|
|
|
//
|
|
|
|
// opcode = (desired line increment - line_base) +
|
|
|
|
// (line_range * address advance) + opcode_base
|
|
|
|
//
|
|
|
|
// If the resulting opcode is greater than 255, a standard opcode
|
|
|
|
// must be used instead.
|
|
|
|
//
|
|
|
|
// To decode a special opcode, subtract the opcode_base from the
|
|
|
|
// opcode itself to give the adjusted opcode. The amount to
|
|
|
|
// increment the address register is the result of the adjusted
|
|
|
|
// opcode divided by the line_range multiplied by the
|
|
|
|
// minimum_instruction_length field from the header. That is:
|
|
|
|
//
|
|
|
|
// address increment = (adjusted opcode / line_range) *
|
|
|
|
// minimum_instruction_length
|
|
|
|
//
|
|
|
|
// The amount to increment the line register is the line_base plus
|
|
|
|
// the result of the adjusted opcode modulo the line_range. That is:
|
|
|
|
//
|
|
|
|
// line increment = line_base + (adjusted opcode % line_range)
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
|
|
|
|
uint64_t AddrOffset =
|
|
|
|
(AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
|
|
|
|
int32_t LineOffset =
|
|
|
|
Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
|
|
|
|
State.Row.Line += LineOffset;
|
|
|
|
State.Row.Address += AddrOffset;
|
2017-09-22 04:15:30 +08:00
|
|
|
|
|
|
|
if (OS) {
|
|
|
|
*OS << "address += " << ((uint32_t)AdjustOpcode)
|
|
|
|
<< ", line += " << LineOffset << "\n";
|
|
|
|
OS->indent(12);
|
|
|
|
State.Row.dump(*OS);
|
|
|
|
}
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
State.appendRowToMatrix(*OffsetPtr);
|
2016-04-29 06:09:37 +08:00
|
|
|
// Reset discriminator to 0.
|
|
|
|
State.Row.Discriminator = 0;
|
2011-09-15 10:12:05 +08:00
|
|
|
}
|
2017-09-22 04:15:30 +08:00
|
|
|
if(OS)
|
|
|
|
*OS << "\n";
|
2011-09-15 10:12:05 +08:00
|
|
|
}
|
|
|
|
|
2014-04-30 08:09:19 +08:00
|
|
|
if (!State.Sequence.Empty) {
|
|
|
|
fprintf(stderr, "warning: last sequence in debug line table is not"
|
|
|
|
"terminated!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort all sequences so that address lookup will work faster.
|
|
|
|
if (!Sequences.empty()) {
|
|
|
|
std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
|
|
|
|
// Note: actually, instruction address ranges of sequences should not
|
|
|
|
// overlap (in shared objects and executables). If they do, the address
|
|
|
|
// lookup would still work, though, but result would be ambiguous.
|
|
|
|
// We don't report warning in this case. For example,
|
|
|
|
// sometimes .so compiled from multiple object files contains a few
|
|
|
|
// rudimentary sequences for address ranges [0x0, 0xsomething).
|
|
|
|
}
|
2011-09-15 10:12:05 +08:00
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
return EndOffset;
|
2011-09-15 10:12:05 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 07:37:04 +08:00
|
|
|
uint32_t
|
2017-05-02 07:27:55 +08:00
|
|
|
DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq,
|
|
|
|
uint64_t Address) const {
|
|
|
|
if (!Seq.containsPC(Address))
|
2015-06-01 07:37:04 +08:00
|
|
|
return UnknownRowIndex;
|
|
|
|
// Search for instruction address in the rows describing the sequence.
|
|
|
|
// Rows are stored in a vector, so we may use arithmetical operations with
|
|
|
|
// iterators.
|
2017-05-02 07:27:55 +08:00
|
|
|
DWARFDebugLine::Row Row;
|
|
|
|
Row.Address = Address;
|
|
|
|
RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
|
|
|
|
RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
|
|
|
|
LineTable::RowIter RowPos = std::lower_bound(
|
|
|
|
FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
|
|
|
|
if (RowPos == LastRow) {
|
|
|
|
return Seq.LastRowIndex - 1;
|
2015-06-01 07:37:04 +08:00
|
|
|
}
|
2017-05-02 07:27:55 +08:00
|
|
|
uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
|
|
|
|
if (RowPos->Address > Address) {
|
|
|
|
if (RowPos == FirstRow)
|
2015-06-01 07:37:04 +08:00
|
|
|
return UnknownRowIndex;
|
|
|
|
else
|
2017-05-02 07:27:55 +08:00
|
|
|
Index--;
|
2015-06-01 07:37:04 +08:00
|
|
|
}
|
2017-05-02 07:27:55 +08:00
|
|
|
return Index;
|
2015-06-01 07:37:04 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t Address) const {
|
2012-08-07 19:46:57 +08:00
|
|
|
if (Sequences.empty())
|
2015-06-01 07:37:04 +08:00
|
|
|
return UnknownRowIndex;
|
2012-08-07 19:46:57 +08:00
|
|
|
// First, find an instruction sequence containing the given address.
|
2017-05-02 07:27:55 +08:00
|
|
|
DWARFDebugLine::Sequence Sequence;
|
|
|
|
Sequence.LowPC = Address;
|
|
|
|
SequenceIter FirstSeq = Sequences.begin();
|
|
|
|
SequenceIter LastSeq = Sequences.end();
|
|
|
|
SequenceIter SeqPos = std::lower_bound(
|
|
|
|
FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
|
|
|
|
DWARFDebugLine::Sequence FoundSeq;
|
|
|
|
if (SeqPos == LastSeq) {
|
|
|
|
FoundSeq = Sequences.back();
|
|
|
|
} else if (SeqPos->LowPC == Address) {
|
|
|
|
FoundSeq = *SeqPos;
|
2012-08-07 19:46:57 +08:00
|
|
|
} else {
|
2017-05-02 07:27:55 +08:00
|
|
|
if (SeqPos == FirstSeq)
|
2015-06-01 07:37:04 +08:00
|
|
|
return UnknownRowIndex;
|
2017-05-02 07:27:55 +08:00
|
|
|
FoundSeq = *(SeqPos - 1);
|
2012-08-07 19:46:57 +08:00
|
|
|
}
|
2017-05-02 07:27:55 +08:00
|
|
|
return findRowInSeq(FoundSeq, Address);
|
2011-09-15 10:12:05 +08:00
|
|
|
}
|
2012-08-30 15:49:50 +08:00
|
|
|
|
2014-04-30 05:28:13 +08:00
|
|
|
bool DWARFDebugLine::LineTable::lookupAddressRange(
|
2017-05-02 07:27:55 +08:00
|
|
|
uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const {
|
2013-01-26 08:28:05 +08:00
|
|
|
if (Sequences.empty())
|
|
|
|
return false;
|
2017-05-02 07:27:55 +08:00
|
|
|
uint64_t EndAddr = Address + Size;
|
2013-01-26 08:28:05 +08:00
|
|
|
// First, find an instruction sequence containing the given address.
|
2017-05-02 07:27:55 +08:00
|
|
|
DWARFDebugLine::Sequence Sequence;
|
|
|
|
Sequence.LowPC = Address;
|
|
|
|
SequenceIter FirstSeq = Sequences.begin();
|
|
|
|
SequenceIter LastSeq = Sequences.end();
|
|
|
|
SequenceIter SeqPos = std::lower_bound(
|
|
|
|
FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
|
|
|
|
if (SeqPos == LastSeq || SeqPos->LowPC != Address) {
|
|
|
|
if (SeqPos == FirstSeq)
|
2013-01-26 08:28:05 +08:00
|
|
|
return false;
|
2017-05-02 07:27:55 +08:00
|
|
|
SeqPos--;
|
2013-01-26 08:28:05 +08:00
|
|
|
}
|
2017-05-02 07:27:55 +08:00
|
|
|
if (!SeqPos->containsPC(Address))
|
2013-01-26 08:28:05 +08:00
|
|
|
return false;
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
SequenceIter StartPos = SeqPos;
|
2013-01-26 08:28:05 +08:00
|
|
|
|
|
|
|
// Add the rows from the first sequence to the vector, starting with the
|
|
|
|
// index we just calculated
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
|
|
|
|
const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
|
2015-06-01 07:37:04 +08:00
|
|
|
// For the first sequence, we need to find which row in the sequence is the
|
|
|
|
// first in our range.
|
2017-05-02 07:27:55 +08:00
|
|
|
uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
|
|
|
|
if (SeqPos == StartPos)
|
|
|
|
FirstRowIndex = findRowInSeq(CurSeq, Address);
|
2015-06-01 07:37:04 +08:00
|
|
|
|
|
|
|
// Figure out the last row in the range.
|
2017-05-02 07:27:55 +08:00
|
|
|
uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1);
|
|
|
|
if (LastRowIndex == UnknownRowIndex)
|
|
|
|
LastRowIndex = CurSeq.LastRowIndex - 1;
|
2013-01-26 08:28:05 +08:00
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
assert(FirstRowIndex != UnknownRowIndex);
|
|
|
|
assert(LastRowIndex != UnknownRowIndex);
|
2015-06-01 07:37:04 +08:00
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
|
|
|
|
Result.push_back(I);
|
2013-01-26 08:28:05 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
++SeqPos;
|
2013-01-26 08:28:05 +08:00
|
|
|
}
|
2013-01-26 09:45:06 +08:00
|
|
|
|
|
|
|
return true;
|
2013-01-26 08:28:05 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
|
2016-07-22 09:41:32 +08:00
|
|
|
return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
|
|
|
|
}
|
|
|
|
|
2018-02-24 07:01:06 +08:00
|
|
|
Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
|
|
|
|
FileLineInfoKind Kind) const {
|
|
|
|
if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
|
|
|
|
return None;
|
|
|
|
const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
|
|
|
|
if (Optional<const char *> source = Entry.Source.getAsCString())
|
|
|
|
return StringRef(*source);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2017-05-02 07:27:55 +08:00
|
|
|
bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
|
|
|
|
const char *CompDir,
|
|
|
|
FileLineInfoKind Kind,
|
|
|
|
std::string &Result) const {
|
2016-07-22 09:41:32 +08:00
|
|
|
if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
|
2012-08-30 15:49:50 +08:00
|
|
|
return false;
|
|
|
|
const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
|
2018-02-06 04:43:15 +08:00
|
|
|
StringRef FileName = Entry.Name.getAsCString().getValue();
|
2014-05-16 05:24:32 +08:00
|
|
|
if (Kind != FileLineInfoKind::AbsoluteFilePath ||
|
2012-08-30 15:49:50 +08:00
|
|
|
sys::path::is_absolute(FileName)) {
|
|
|
|
Result = FileName;
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-19 23:11:51 +08:00
|
|
|
|
2012-08-30 15:49:50 +08:00
|
|
|
SmallString<16> FilePath;
|
|
|
|
uint64_t IncludeDirIndex = Entry.DirIdx;
|
2017-05-03 01:37:32 +08:00
|
|
|
StringRef IncludeDir;
|
2012-08-30 15:49:50 +08:00
|
|
|
// Be defensive about the contents of Entry.
|
|
|
|
if (IncludeDirIndex > 0 &&
|
2014-09-19 23:11:51 +08:00
|
|
|
IncludeDirIndex <= Prologue.IncludeDirectories.size())
|
2018-02-06 04:43:15 +08:00
|
|
|
IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1]
|
|
|
|
.getAsCString()
|
|
|
|
.getValue();
|
2014-09-19 23:11:51 +08:00
|
|
|
|
|
|
|
// We may still need to append compilation directory of compile unit.
|
|
|
|
// We know that FileName is not absolute, the only way to have an
|
|
|
|
// absolute path at this point would be if IncludeDir is absolute.
|
|
|
|
if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
|
|
|
|
sys::path::is_relative(IncludeDir))
|
|
|
|
sys::path::append(FilePath, CompDir);
|
|
|
|
|
|
|
|
// sys::path::append skips empty strings.
|
|
|
|
sys::path::append(FilePath, IncludeDir, FileName);
|
2012-08-30 15:49:50 +08:00
|
|
|
Result = FilePath.str();
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-19 23:11:51 +08:00
|
|
|
|
2016-04-29 06:09:37 +08:00
|
|
|
bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
|
|
|
|
uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
|
|
|
|
DILineInfo &Result) const {
|
2014-09-19 23:11:51 +08:00
|
|
|
// Get the index of row we're looking for in the line table.
|
|
|
|
uint32_t RowIndex = lookupAddress(Address);
|
|
|
|
if (RowIndex == -1U)
|
|
|
|
return false;
|
|
|
|
// Take file number and line/column from the row.
|
|
|
|
const auto &Row = Rows[RowIndex];
|
|
|
|
if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
|
|
|
|
return false;
|
|
|
|
Result.Line = Row.Line;
|
|
|
|
Result.Column = Row.Column;
|
2016-12-15 02:29:39 +08:00
|
|
|
Result.Discriminator = Row.Discriminator;
|
2018-02-24 07:01:06 +08:00
|
|
|
Result.Source = getSourceByIndex(Row.File, Kind);
|
2014-09-19 23:11:51 +08:00
|
|
|
return true;
|
|
|
|
}
|