[FaultMaps] Add a parser for the __llvm__faultmaps section.

Summary:
The parser is exercised by llvm-objdump using -print-fault-maps.  As is
probably obvious, the code itself was "heavily inspired" by
http://reviews.llvm.org/D10434.

Reviewers: reames, atrick, JosephTremoulet

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D10491

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240304 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Sanjoy Das 2015-06-22 18:03:02 +00:00
parent 5a94e2a9e8
commit e0ef46e5e7
5 changed files with 286 additions and 1 deletions

View File

@ -12,6 +12,8 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Format.h"
#include <vector>
#include <map>
@ -68,6 +70,152 @@ private:
void emitFunctionInfo(const MCSymbol *FnLabel, const FunctionFaultInfos &FFI);
};
/// A parser for the __llvm_faultmaps section generated by the FaultMaps class
/// above. This parser is version locked with with the __llvm_faultmaps section
/// generated by the version of LLVM that includes it. No guarantees are made
/// with respect to forward or backward compatibility.
class FaultMapParser {
typedef uint8_t FaultMapVersionType;
static const size_t FaultMapVersionOffset = 0;
typedef uint8_t Reserved0Type;
static const size_t Reserved0Offset =
FaultMapVersionOffset + sizeof(FaultMapVersionType);
typedef uint16_t Reserved1Type;
static const size_t Reserved1Offset = Reserved0Offset + sizeof(Reserved0Type);
typedef uint32_t NumFunctionsType;
static const size_t NumFunctionsOffset =
Reserved1Offset + sizeof(Reserved1Type);
static const size_t FunctionInfosOffset =
NumFunctionsOffset + sizeof(NumFunctionsType);
const uint8_t *P;
const uint8_t *E;
template <typename T> static T read(const uint8_t *P, const uint8_t *E) {
assert(P + sizeof(T) <= E && "out of bounds read!");
return support::endian::read<T, support::little, 1>(P);
}
public:
class FunctionFaultInfoAccessor {
typedef uint32_t FaultKindType;
static const size_t FaultKindOffset = 0;
typedef uint32_t FaultingPCOffsetType;
static const size_t FaultingPCOffsetOffset =
FaultKindOffset + sizeof(FaultKindType);
typedef uint32_t HandlerPCOffsetType;
static const size_t HandlerPCOffsetOffset =
FaultingPCOffsetOffset + sizeof(FaultingPCOffsetType);
const uint8_t *P;
const uint8_t *E;
public:
static const size_t Size =
HandlerPCOffsetOffset + sizeof(HandlerPCOffsetType);
explicit FunctionFaultInfoAccessor(const uint8_t *P, const uint8_t *E)
: P(P), E(E) {}
FaultKindType getFaultKind() const {
return read<FaultKindType>(P + FaultKindOffset, E);
}
FaultingPCOffsetType getFaultingPCOffset() const {
return read<FaultingPCOffsetType>(P + FaultingPCOffsetOffset, E);
}
HandlerPCOffsetType getHandlerPCOffset() const {
return read<HandlerPCOffsetType>(P + HandlerPCOffsetOffset, E);
}
};
class FunctionInfoAccessor {
typedef uint64_t FunctionAddrType;
static const size_t FunctionAddrOffset = 0;
typedef uint32_t NumFaultingPCsType;
static const size_t NumFaultingPCsOffset =
FunctionAddrOffset + sizeof(FunctionAddrType);
typedef uint32_t ReservedType;
static const size_t ReservedOffset =
NumFaultingPCsOffset + sizeof(NumFaultingPCsType);
static const size_t FunctionFaultInfosOffset =
ReservedOffset + sizeof(ReservedType);
static const size_t FunctionInfoHeaderSize = FunctionFaultInfosOffset;
const uint8_t *P;
const uint8_t *E;
public:
FunctionInfoAccessor() : P(nullptr), E(nullptr) {}
explicit FunctionInfoAccessor(const uint8_t *P, const uint8_t *E)
: P(P), E(E) {}
FunctionAddrType getFunctionAddr() const {
return read<FunctionAddrType>(P + FunctionAddrOffset, E);
}
NumFaultingPCsType getNumFaultingPCs() const {
return read<NumFaultingPCsType>(P + NumFaultingPCsOffset, E);
}
FunctionFaultInfoAccessor getFunctionFaultInfoAt(uint32_t Index) const {
assert(Index < getNumFaultingPCs() && "index out of bounds!");
const uint8_t *Begin = P + FunctionFaultInfosOffset +
FunctionFaultInfoAccessor::Size * Index;
return FunctionFaultInfoAccessor(Begin, E);
}
FunctionInfoAccessor getNextFunctionInfo() const {
size_t MySize = FunctionInfoHeaderSize +
getNumFaultingPCs() * FunctionFaultInfoAccessor::Size;
const uint8_t *Begin = P + MySize;
assert(Begin < E && "out of bounds!");
return FunctionInfoAccessor(Begin, E);
}
};
explicit FaultMapParser(const uint8_t *Begin, const uint8_t *End)
: P(Begin), E(End) {}
FaultMapVersionType getFaultMapVersion() const {
auto Version = read<FaultMapVersionType>(P + FaultMapVersionOffset, E);
assert(Version == 1 && "only version 1 supported!");
return Version;
}
NumFunctionsType getNumFunctions() const {
return read<NumFunctionsType>(P + NumFunctionsOffset, E);
}
FunctionInfoAccessor getFirstFunctionInfo() const {
const uint8_t *Begin = P + FunctionInfosOffset;
return FunctionInfoAccessor(Begin, E);
}
};
raw_ostream &
operator<<(raw_ostream &OS,
const typename FaultMapParser::FunctionFaultInfoAccessor &);
raw_ostream &operator<<(raw_ostream &OS,
const typename FaultMapParser::FunctionInfoAccessor &);
raw_ostream &operator<<(raw_ostream &OS, const FaultMapParser &);
} // namespace llvm
#endif

View File

@ -112,3 +112,40 @@ const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
return "FaultingLoad";
}
}
raw_ostream &llvm::
operator<<(raw_ostream &OS,
const typename FaultMapParser::FunctionFaultInfoAccessor &FFI) {
OS << "Fault kind: "
<< FaultMaps::faultTypeToString((FaultMaps::FaultKind)FFI.getFaultKind())
<< ", faulting PC offset: " << FFI.getFaultingPCOffset()
<< ", handling PC offset: " << FFI.getHandlerPCOffset();
return OS;
}
raw_ostream &llvm::
operator<<(raw_ostream &OS,
const typename FaultMapParser::FunctionInfoAccessor &FI) {
OS << "FunctionAddress: " << format_hex(FI.getFunctionAddr(), 8)
<< ", NumFaultingPCs: " << FI.getNumFaultingPCs() << "\n";
for (unsigned i = 0, e = FI.getNumFaultingPCs(); i != e; ++i)
OS << FI.getFunctionFaultInfoAt(i) << "\n";
return OS;
}
raw_ostream &llvm::operator<<(raw_ostream &OS, const FaultMapParser &FMP) {
OS << "Version: " << format_hex(FMP.getFaultMapVersion(), 2) << "\n";
OS << "NumFunctions: " << FMP.getNumFunctions() << "\n";
if (FMP.getNumFunctions() == 0)
return OS;
typename FaultMapParser::FunctionInfoAccessor FI;
for (unsigned i = 0, e = FMP.getNumFunctions(); i != e; ++i) {
FI = (i == 0) ? FMP.getFirstFunctionInfo() : FI.getNextFunctionInfo();
OS << FI;
}
return OS;
}

View File

@ -1,5 +1,15 @@
; RUN: llc -O3 -mtriple=x86_64-apple-macosx -enable-implicit-null-checks < %s | FileCheck %s
; RUN: llc < %s -mtriple=x86_64-apple-macosx -enable-implicit-null-checks \
; RUN: | llvm-mc -triple x86_64-apple-macosx -filetype=obj -o - \
; RUN: | llvm-objdump -triple x86_64-apple-macosx -fault-map-section - \
; RUN: | FileCheck %s -check-prefix OBJDUMP
; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -enable-implicit-null-checks \
; RUN: | llvm-mc -triple x86_64-unknown-linux-gnu -filetype=obj -o - \
; RUN: | llvm-objdump -triple x86_64-unknown-linux-gnu -fault-map-section - \
; RUN: | FileCheck %s -check-prefix OBJDUMP
define i32 @imp_null_check_load(i32* %x) {
; CHECK-LABEL: _imp_null_check_load:
; CHECK: Ltmp1:
@ -116,3 +126,13 @@ define i32 @imp_null_check_add_result(i32* %x, i32 %p) {
; CHECK-NEXT: .long Ltmp1-_imp_null_check_load
; Fault[0].HandlerOffset:
; CHECK-NEXT: .long Ltmp0-_imp_null_check_load
; OBJDUMP: FaultMap table:
; OBJDUMP-NEXT: Version: 0x1
; OBJDUMP-NEXT: NumFunctions: 3
; OBJDUMP-NEXT: FunctionAddress: 0x000000, NumFaultingPCs: 1
; OBJDUMP-NEXT: Fault kind: FaultingLoad, faulting PC offset: 0, handling PC offset: 5
; OBJDUMP-NEXT: FunctionAddress: 0x000000, NumFaultingPCs: 1
; OBJDUMP-NEXT: Fault kind: FaultingLoad, faulting PC offset: 0, handling PC offset: 7
; OBJDUMP-NEXT: FunctionAddress: 0x000000, NumFaultingPCs: 1
; OBJDUMP-NEXT: Fault kind: FaultingLoad, faulting PC offset: 0, handling PC offset: 3

View File

@ -0,0 +1,29 @@
// RUN: llvm-mc < %s -triple=x86_64-apple-macosx -filetype=obj -o - | llvm-objdump -fault-map-section - | FileCheck %s
.section __LLVM_FAULTMAPS,__llvm_faultmaps
__LLVM_FaultMaps:
.byte 1
.byte 0
.short 0
.long 2
.quad 0xFFDEAD
.long 1
.long 0
.long 1
.long 100
.long 200
.quad 0xFFDAED
.long 1
.long 0
.long 1
.long 400
.long 500
// CHECK: FaultMap table:
// CHECK-NEXT: Version: 0x1
// CHECK-NEXT: NumFunctions: 2
// CHECK-NEXT: FunctionAddress: 0xffdead, NumFaultingPCs: 1
// CHECK-NEXT: Fault kind: FaultingLoad, faulting PC offset: 100, handling PC offset: 200
// CHECK-NEXT: FunctionAddress: 0xffdaed, NumFaultingPCs: 1
// CHECK-NEXT: Fault kind: FaultingLoad, faulting PC offset: 400, handling PC offset: 500

View File

@ -17,9 +17,11 @@
//===----------------------------------------------------------------------===//
#include "llvm-objdump.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Triple.h"
#include "llvm/CodeGen/FaultMaps.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCDisassembler.h"
@ -153,6 +155,9 @@ cl::opt<bool>
llvm::PrintImmHex("print-imm-hex",
cl::desc("Use hex format for immediate values"));
cl::opt<bool> PrintFaultMaps("fault-map-section",
cl::desc("Display contents of faultmap section"));
static StringRef ToolName;
static int ReturnValue = EXIT_SUCCESS;
@ -1226,6 +1231,49 @@ void llvm::printWeakBindTable(const ObjectFile *o) {
}
}
static void printFaultMaps(const ObjectFile *Obj) {
const char *FaultMapSectionName = nullptr;
if (isa<ELFObjectFileBase>(Obj)) {
FaultMapSectionName = ".llvm_faultmaps";
} else if (isa<MachOObjectFile>(Obj)) {
FaultMapSectionName = "__llvm_faultmaps";
} else {
errs() << "This operation is only currently supported "
"for ELF and Mach-O executable files.\n";
return;
}
Optional<object::SectionRef> FaultMapSection;
for (auto Sec : Obj->sections()) {
StringRef Name;
Sec.getName(Name);
if (Name == FaultMapSectionName) {
FaultMapSection = Sec;
break;
}
}
outs() << "FaultMap table:\n";
if (!FaultMapSection.hasValue()) {
outs() << "<not found>\n";
return;
}
StringRef FaultMapContents;
if (error(FaultMapSection.getValue().getContents(FaultMapContents))) {
errs() << "Could not read the " << FaultMapContents << " section!\n";
return;
}
FaultMapParser FMP(FaultMapContents.bytes_begin(),
FaultMapContents.bytes_end());
outs() << FMP;
}
static void printPrivateFileHeader(const ObjectFile *o) {
if (o->isELF()) {
printELFFileHeader(o);
@ -1265,6 +1313,8 @@ static void DumpObject(const ObjectFile *o) {
printLazyBindTable(o);
if (WeakBind)
printWeakBindTable(o);
if (PrintFaultMaps)
printFaultMaps(o);
}
/// @brief Dump each object file in \a a;
@ -1362,7 +1412,8 @@ int main(int argc, char **argv) {
&& !(DylibsUsed && MachOOpt)
&& !(DylibId && MachOOpt)
&& !(ObjcMetaData && MachOOpt)
&& !(DumpSections.size() != 0 && MachOOpt)) {
&& !(DumpSections.size() != 0 && MachOOpt)
&& !PrintFaultMaps) {
cl::PrintHelpMessage();
return 2;
}