blob: ee925c219065566ec65980ee9b3be26caddae2ce [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===---- RuntimeDyldChecker.h - RuntimeDyld tester framework -----*- C++ -*-=//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_EXECUTIONENGINE_RUNTIMEDYLDCHECKER_H
10#define LLVM_EXECUTIONENGINE_RUNTIMEDYLDCHECKER_H
11
12#include "llvm/ADT/Optional.h"
13
14#include <cstdint>
15#include <memory>
16#include <string>
17#include <utility>
18
19namespace llvm {
20
21class StringRef;
22class MCDisassembler;
23class MemoryBuffer;
24class MCInstPrinter;
25class RuntimeDyld;
26class RuntimeDyldCheckerImpl;
27class raw_ostream;
28
Andrew Scullcdfcccc2018-10-05 20:58:37 +010029/// RuntimeDyld invariant checker for verifying that RuntimeDyld has
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010030/// correctly applied relocations.
31///
32/// The RuntimeDyldChecker class evaluates expressions against an attached
33/// RuntimeDyld instance to verify that relocations have been applied
34/// correctly.
35///
36/// The expression language supports basic pointer arithmetic and bit-masking,
37/// and has limited disassembler integration for accessing instruction
38/// operands and the next PC (program counter) address for each instruction.
39///
40/// The language syntax is:
41///
42/// check = expr '=' expr
43///
44/// expr = binary_expr
45/// | sliceable_expr
46///
47/// sliceable_expr = '*{' number '}' load_addr_expr [slice]
48/// | '(' expr ')' [slice]
49/// | ident_expr [slice]
50/// | number [slice]
51///
52/// slice = '[' high-bit-index ':' low-bit-index ']'
53///
54/// load_addr_expr = symbol
55/// | '(' symbol '+' number ')'
56/// | '(' symbol '-' number ')'
57///
58/// ident_expr = 'decode_operand' '(' symbol ',' operand-index ')'
59/// | 'next_pc' '(' symbol ')'
60/// | 'stub_addr' '(' file-name ',' section-name ',' symbol ')'
61/// | symbol
62///
63/// binary_expr = expr '+' expr
64/// | expr '-' expr
65/// | expr '&' expr
66/// | expr '|' expr
67/// | expr '<<' expr
68/// | expr '>>' expr
69///
70class RuntimeDyldChecker {
71public:
72 RuntimeDyldChecker(RuntimeDyld &RTDyld, MCDisassembler *Disassembler,
73 MCInstPrinter *InstPrinter, raw_ostream &ErrStream);
74 ~RuntimeDyldChecker();
75
Andrew Scullcdfcccc2018-10-05 20:58:37 +010076 // Get the associated RTDyld instance.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010077 RuntimeDyld& getRTDyld();
78
Andrew Scullcdfcccc2018-10-05 20:58:37 +010079 // Get the associated RTDyld instance.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010080 const RuntimeDyld& getRTDyld() const;
81
Andrew Scullcdfcccc2018-10-05 20:58:37 +010082 /// Check a single expression against the attached RuntimeDyld
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010083 /// instance.
84 bool check(StringRef CheckExpr) const;
85
Andrew Scullcdfcccc2018-10-05 20:58:37 +010086 /// Scan the given memory buffer for lines beginning with the string
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010087 /// in RulePrefix. The remainder of the line is passed to the check
88 /// method to be evaluated as an expression.
89 bool checkAllRulesInBuffer(StringRef RulePrefix, MemoryBuffer *MemBuf) const;
90
Andrew Scullcdfcccc2018-10-05 20:58:37 +010091 /// Returns the address of the requested section (or an error message
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010092 /// in the second element of the pair if the address cannot be found).
93 ///
94 /// if 'LocalAddress' is true, this returns the address of the section
95 /// within the linker's memory. If 'LocalAddress' is false it returns the
96 /// address within the target process (i.e. the load address).
97 std::pair<uint64_t, std::string> getSectionAddr(StringRef FileName,
98 StringRef SectionName,
99 bool LocalAddress);
100
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100101 /// If there is a section at the given local address, return its load
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100102 /// address, otherwise return none.
103 Optional<uint64_t> getSectionLoadAddress(void *LocalAddress) const;
104
105private:
106 std::unique_ptr<RuntimeDyldCheckerImpl> Impl;
107};
108
109} // end namespace llvm
110
111#endif