RuntimeDyldChecker.h revision 321369
1//===---- RuntimeDyldChecker.h - RuntimeDyld tester framework -----*- C++ -*-=//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_EXECUTIONENGINE_RUNTIMEDYLDCHECKER_H
11#define LLVM_EXECUTIONENGINE_RUNTIMEDYLDCHECKER_H
12
13#include "llvm/ADT/Optional.h"
14
15#include <cstdint>
16#include <memory>
17#include <string>
18#include <utility>
19
20namespace llvm {
21
22class StringRef;
23class MCDisassembler;
24class MemoryBuffer;
25class MCInstPrinter;
26class RuntimeDyld;
27class RuntimeDyldCheckerImpl;
28class raw_ostream;
29
30/// \brief RuntimeDyld invariant checker for verifying that RuntimeDyld has
31///        correctly applied relocations.
32///
33/// The RuntimeDyldChecker class evaluates expressions against an attached
34/// RuntimeDyld instance to verify that relocations have been applied
35/// correctly.
36///
37/// The expression language supports basic pointer arithmetic and bit-masking,
38/// and has limited disassembler integration for accessing instruction
39/// operands and the next PC (program counter) address for each instruction.
40///
41/// The language syntax is:
42///
43/// check = expr '=' expr
44///
45/// expr = binary_expr
46///      | sliceable_expr
47///
48/// sliceable_expr = '*{' number '}' load_addr_expr [slice]
49///                | '(' expr ')' [slice]
50///                | ident_expr [slice]
51///                | number [slice]
52///
53/// slice = '[' high-bit-index ':' low-bit-index ']'
54///
55/// load_addr_expr = symbol
56///                | '(' symbol '+' number ')'
57///                | '(' symbol '-' number ')'
58///
59/// ident_expr = 'decode_operand' '(' symbol ',' operand-index ')'
60///            | 'next_pc'        '(' symbol ')'
61///            | 'stub_addr' '(' file-name ',' section-name ',' symbol ')'
62///            | symbol
63///
64/// binary_expr = expr '+' expr
65///             | expr '-' expr
66///             | expr '&' expr
67///             | expr '|' expr
68///             | expr '<<' expr
69///             | expr '>>' expr
70///
71class RuntimeDyldChecker {
72public:
73  RuntimeDyldChecker(RuntimeDyld &RTDyld, MCDisassembler *Disassembler,
74                     MCInstPrinter *InstPrinter, raw_ostream &ErrStream);
75  ~RuntimeDyldChecker();
76
77  // \brief Get the associated RTDyld instance.
78  RuntimeDyld& getRTDyld();
79
80  // \brief Get the associated RTDyld instance.
81  const RuntimeDyld& getRTDyld() const;
82
83  /// \brief Check a single expression against the attached RuntimeDyld
84  ///        instance.
85  bool check(StringRef CheckExpr) const;
86
87  /// \brief Scan the given memory buffer for lines beginning with the string
88  ///        in RulePrefix. The remainder of the line is passed to the check
89  ///        method to be evaluated as an expression.
90  bool checkAllRulesInBuffer(StringRef RulePrefix, MemoryBuffer *MemBuf) const;
91
92  /// \brief Returns the address of the requested section (or an error message
93  ///        in the second element of the pair if the address cannot be found).
94  ///
95  /// if 'LocalAddress' is true, this returns the address of the section
96  /// within the linker's memory. If 'LocalAddress' is false it returns the
97  /// address within the target process (i.e. the load address).
98  std::pair<uint64_t, std::string> getSectionAddr(StringRef FileName,
99                                                  StringRef SectionName,
100                                                  bool LocalAddress);
101
102  /// \brief If there is a section at the given local address, return its load
103  ///        address, otherwise return none.
104  Optional<uint64_t> getSectionLoadAddress(void *LocalAddress) const;
105
106private:
107  std::unique_ptr<RuntimeDyldCheckerImpl> Impl;
108};
109
110} // end namespace llvm
111
112#endif
113