blob: 13fc5fd5a3e74328f4afc72abd9c3fd24d042303 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===---- 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +010030/// RuntimeDyld invariant checker for verifying that RuntimeDyld has
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010031/// 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +010077 // Get the associated RTDyld instance.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010078 RuntimeDyld& getRTDyld();
79
Andrew Scullcdfcccc2018-10-05 20:58:37 +010080 // Get the associated RTDyld instance.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010081 const RuntimeDyld& getRTDyld() const;
82
Andrew Scullcdfcccc2018-10-05 20:58:37 +010083 /// Check a single expression against the attached RuntimeDyld
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010084 /// instance.
85 bool check(StringRef CheckExpr) const;
86
Andrew Scullcdfcccc2018-10-05 20:58:37 +010087 /// Scan the given memory buffer for lines beginning with the string
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010088 /// 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +010092 /// Returns the address of the requested section (or an error message
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010093 /// 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100102 /// If there is a section at the given local address, return its load
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100103 /// 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