2016-10-08 05:34:46 +08:00
|
|
|
//===- CVSymbolVisitor.cpp --------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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-10-08 05:34:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/DebugInfo/CodeView/CVSymbolVisitor.h"
|
|
|
|
|
|
|
|
#include "llvm/DebugInfo/CodeView/CodeViewError.h"
|
|
|
|
#include "llvm/DebugInfo/CodeView/SymbolVisitorCallbacks.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::codeview;
|
|
|
|
|
|
|
|
CVSymbolVisitor::CVSymbolVisitor(SymbolVisitorCallbacks &Callbacks)
|
|
|
|
: Callbacks(Callbacks) {}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static Error visitKnownRecord(CVSymbol &Record,
|
|
|
|
SymbolVisitorCallbacks &Callbacks) {
|
|
|
|
SymbolRecordKind RK = static_cast<SymbolRecordKind>(Record.Type);
|
|
|
|
T KnownRecord(RK);
|
|
|
|
if (auto EC = Callbacks.visitKnownRecord(Record, KnownRecord))
|
|
|
|
return EC;
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-07-01 05:35:00 +08:00
|
|
|
static Error finishVisitation(CVSymbol &Record,
|
|
|
|
SymbolVisitorCallbacks &Callbacks) {
|
2016-10-08 05:34:46 +08:00
|
|
|
switch (Record.Type) {
|
|
|
|
default:
|
|
|
|
if (auto EC = Callbacks.visitUnknownSymbol(Record))
|
|
|
|
return EC;
|
|
|
|
break;
|
|
|
|
#define SYMBOL_RECORD(EnumName, EnumVal, Name) \
|
|
|
|
case EnumName: { \
|
|
|
|
if (auto EC = visitKnownRecord<Name>(Record, Callbacks)) \
|
|
|
|
return EC; \
|
|
|
|
break; \
|
|
|
|
}
|
|
|
|
#define SYMBOL_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) \
|
|
|
|
SYMBOL_RECORD(EnumVal, EnumVal, AliasName)
|
2017-05-31 05:53:05 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/CodeViewSymbols.def"
|
2016-10-08 05:34:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (auto EC = Callbacks.visitSymbolEnd(Record))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-07-01 05:35:00 +08:00
|
|
|
Error CVSymbolVisitor::visitSymbolRecord(CVSymbol &Record) {
|
|
|
|
if (auto EC = Callbacks.visitSymbolBegin(Record))
|
|
|
|
return EC;
|
|
|
|
return finishVisitation(Record, Callbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error CVSymbolVisitor::visitSymbolRecord(CVSymbol &Record, uint32_t Offset) {
|
|
|
|
if (auto EC = Callbacks.visitSymbolBegin(Record, Offset))
|
|
|
|
return EC;
|
|
|
|
return finishVisitation(Record, Callbacks);
|
|
|
|
}
|
|
|
|
|
2016-10-08 05:34:46 +08:00
|
|
|
Error CVSymbolVisitor::visitSymbolStream(const CVSymbolArray &Symbols) {
|
|
|
|
for (auto I : Symbols) {
|
|
|
|
if (auto EC = visitSymbolRecord(I))
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
2017-07-01 05:35:00 +08:00
|
|
|
|
|
|
|
Error CVSymbolVisitor::visitSymbolStream(const CVSymbolArray &Symbols,
|
|
|
|
uint32_t InitialOffset) {
|
|
|
|
for (auto I : Symbols) {
|
Support skewed stream arrays.
VarStreamArray was built on the assumption that it is backed by a
StreamRef, and offset 0 of that StreamRef is the first byte of the first
record in the array.
This is a logical and intuitive assumption, but unfortunately we have
use cases where it doesn't hold. Specifically, a PDB module's symbol
stream is prefixed by 4 bytes containing a magic value, and the first
byte of record data in the array is actually at offset 4 of this byte
sequence.
Previously, we would just truncate the first 4 bytes and then construct
the VarStreamArray with the resulting StreamRef, so that offset 0 of the
underlying stream did correspond to the first byte of the first record,
but this is problematic, because symbol records reference other symbol
records by the absolute offset including that initial magic 4 bytes. So
if another record wants to refer to the first record in the array, it
would say "the record at offset 4".
This led to extremely confusing hacks and semantics in loading code, and
after spending 30 minutes trying to get some math right and failing, I
decided to fix this in the underlying implementation of VarStreamArray.
Now, we can say that a stream is skewed by a particular amount. This
way, when we access a record by absolute offset, we can use the same
values that the records themselves contain, instead of having to do
fixups.
Differential Revision: https://reviews.llvm.org/D55344
llvm-svn: 348499
2018-12-07 00:55:00 +08:00
|
|
|
if (auto EC = visitSymbolRecord(I, InitialOffset + Symbols.skew()))
|
2017-07-01 05:35:00 +08:00
|
|
|
return EC;
|
|
|
|
InitialOffset += I.length();
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|