Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 1 | //===--- RuntimeDyldChecker.cpp - 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 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 10 | #include "llvm/ADT/STLExtras.h" |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 11 | #include "llvm/ExecutionEngine/RuntimeDyldChecker.h" |
| 12 | #include "llvm/MC/MCContext.h" |
| 13 | #include "llvm/MC/MCDisassembler.h" |
| 14 | #include "llvm/MC/MCInst.h" |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 15 | #include "llvm/Support/Path.h" |
| 16 | #include "RuntimeDyldCheckerImpl.h" |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 17 | #include "RuntimeDyldImpl.h" |
| 18 | #include <cctype> |
| 19 | #include <memory> |
| 20 | |
| 21 | #define DEBUG_TYPE "rtdyld" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace llvm { |
| 26 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 27 | // Helper class that implements the language evaluated by RuntimeDyldChecker. |
| 28 | class RuntimeDyldCheckerExprEval { |
| 29 | public: |
| 30 | RuntimeDyldCheckerExprEval(const RuntimeDyldCheckerImpl &Checker, |
| 31 | raw_ostream &ErrStream) |
| 32 | : Checker(Checker) {} |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 33 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 34 | bool evaluate(StringRef Expr) const { |
| 35 | // Expect equality expression of the form 'LHS = RHS'. |
| 36 | Expr = Expr.trim(); |
| 37 | size_t EQIdx = Expr.find('='); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 38 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 39 | ParseContext OutsideLoad(false); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 40 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 41 | // Evaluate LHS. |
| 42 | StringRef LHSExpr = Expr.substr(0, EQIdx).rtrim(); |
| 43 | StringRef RemainingExpr; |
| 44 | EvalResult LHSResult; |
| 45 | std::tie(LHSResult, RemainingExpr) = |
| 46 | evalComplexExpr(evalSimpleExpr(LHSExpr, OutsideLoad), OutsideLoad); |
| 47 | if (LHSResult.hasError()) |
| 48 | return handleError(Expr, LHSResult); |
| 49 | if (RemainingExpr != "") |
| 50 | return handleError(Expr, unexpectedToken(RemainingExpr, LHSExpr, "")); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 51 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 52 | // Evaluate RHS. |
| 53 | StringRef RHSExpr = Expr.substr(EQIdx + 1).ltrim(); |
| 54 | EvalResult RHSResult; |
| 55 | std::tie(RHSResult, RemainingExpr) = |
| 56 | evalComplexExpr(evalSimpleExpr(RHSExpr, OutsideLoad), OutsideLoad); |
| 57 | if (RHSResult.hasError()) |
| 58 | return handleError(Expr, RHSResult); |
| 59 | if (RemainingExpr != "") |
| 60 | return handleError(Expr, unexpectedToken(RemainingExpr, RHSExpr, "")); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 61 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 62 | if (LHSResult.getValue() != RHSResult.getValue()) { |
| 63 | Checker.ErrStream << "Expression '" << Expr << "' is false: " |
| 64 | << format("0x%" PRIx64, LHSResult.getValue()) |
| 65 | << " != " << format("0x%" PRIx64, RHSResult.getValue()) |
| 66 | << "\n"; |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 67 | return false; |
| 68 | } |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 69 | return true; |
| 70 | } |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 71 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 72 | private: |
| 73 | // RuntimeDyldCheckerExprEval requires some context when parsing exprs. In |
| 74 | // particular, it needs to know whether a symbol is being evaluated in the |
| 75 | // context of a load, in which case we want the linker's local address for |
| 76 | // the symbol, or outside of a load, in which case we want the symbol's |
| 77 | // address in the remote target. |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 78 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 79 | struct ParseContext { |
| 80 | bool IsInsideLoad; |
| 81 | ParseContext(bool IsInsideLoad) : IsInsideLoad(IsInsideLoad) {} |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 84 | const RuntimeDyldCheckerImpl &Checker; |
| 85 | |
| 86 | enum class BinOpToken : unsigned { |
| 87 | Invalid, |
| 88 | Add, |
| 89 | Sub, |
| 90 | BitwiseAnd, |
| 91 | BitwiseOr, |
| 92 | ShiftLeft, |
| 93 | ShiftRight |
| 94 | }; |
| 95 | |
| 96 | class EvalResult { |
| 97 | public: |
| 98 | EvalResult() : Value(0), ErrorMsg("") {} |
| 99 | EvalResult(uint64_t Value) : Value(Value), ErrorMsg("") {} |
| 100 | EvalResult(std::string ErrorMsg) : Value(0), ErrorMsg(ErrorMsg) {} |
| 101 | uint64_t getValue() const { return Value; } |
| 102 | bool hasError() const { return ErrorMsg != ""; } |
| 103 | const std::string &getErrorMsg() const { return ErrorMsg; } |
| 104 | |
| 105 | private: |
| 106 | uint64_t Value; |
| 107 | std::string ErrorMsg; |
| 108 | }; |
| 109 | |
| 110 | StringRef getTokenForError(StringRef Expr) const { |
| 111 | if (Expr.empty()) |
| 112 | return ""; |
| 113 | |
| 114 | StringRef Token, Remaining; |
| 115 | if (isalpha(Expr[0])) |
| 116 | std::tie(Token, Remaining) = parseSymbol(Expr); |
| 117 | else if (isdigit(Expr[0])) |
| 118 | std::tie(Token, Remaining) = parseNumberString(Expr); |
| 119 | else { |
| 120 | unsigned TokLen = 1; |
| 121 | if (Expr.startswith("<<") || Expr.startswith(">>")) |
| 122 | TokLen = 2; |
| 123 | Token = Expr.substr(0, TokLen); |
| 124 | } |
| 125 | return Token; |
| 126 | } |
| 127 | |
| 128 | EvalResult unexpectedToken(StringRef TokenStart, StringRef SubExpr, |
| 129 | StringRef ErrText) const { |
| 130 | std::string ErrorMsg("Encountered unexpected token '"); |
| 131 | ErrorMsg += getTokenForError(TokenStart); |
| 132 | if (SubExpr != "") { |
| 133 | ErrorMsg += "' while parsing subexpression '"; |
| 134 | ErrorMsg += SubExpr; |
| 135 | } |
| 136 | ErrorMsg += "'"; |
| 137 | if (ErrText != "") { |
| 138 | ErrorMsg += " "; |
| 139 | ErrorMsg += ErrText; |
| 140 | } |
| 141 | return EvalResult(std::move(ErrorMsg)); |
| 142 | } |
| 143 | |
| 144 | bool handleError(StringRef Expr, const EvalResult &R) const { |
| 145 | assert(R.hasError() && "Not an error result."); |
| 146 | Checker.ErrStream << "Error evaluating expression '" << Expr |
| 147 | << "': " << R.getErrorMsg() << "\n"; |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | std::pair<BinOpToken, StringRef> parseBinOpToken(StringRef Expr) const { |
| 152 | if (Expr.empty()) |
| 153 | return std::make_pair(BinOpToken::Invalid, ""); |
| 154 | |
| 155 | // Handle the two 2-character tokens. |
| 156 | if (Expr.startswith("<<")) |
| 157 | return std::make_pair(BinOpToken::ShiftLeft, Expr.substr(2).ltrim()); |
| 158 | if (Expr.startswith(">>")) |
| 159 | return std::make_pair(BinOpToken::ShiftRight, Expr.substr(2).ltrim()); |
| 160 | |
| 161 | // Handle one-character tokens. |
| 162 | BinOpToken Op; |
| 163 | switch (Expr[0]) { |
| 164 | default: |
| 165 | return std::make_pair(BinOpToken::Invalid, Expr); |
| 166 | case '+': |
| 167 | Op = BinOpToken::Add; |
| 168 | break; |
| 169 | case '-': |
| 170 | Op = BinOpToken::Sub; |
| 171 | break; |
| 172 | case '&': |
| 173 | Op = BinOpToken::BitwiseAnd; |
| 174 | break; |
| 175 | case '|': |
| 176 | Op = BinOpToken::BitwiseOr; |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | return std::make_pair(Op, Expr.substr(1).ltrim()); |
| 181 | } |
| 182 | |
| 183 | EvalResult computeBinOpResult(BinOpToken Op, const EvalResult &LHSResult, |
| 184 | const EvalResult &RHSResult) const { |
| 185 | switch (Op) { |
| 186 | default: |
| 187 | llvm_unreachable("Tried to evaluate unrecognized operation."); |
| 188 | case BinOpToken::Add: |
| 189 | return EvalResult(LHSResult.getValue() + RHSResult.getValue()); |
| 190 | case BinOpToken::Sub: |
| 191 | return EvalResult(LHSResult.getValue() - RHSResult.getValue()); |
| 192 | case BinOpToken::BitwiseAnd: |
| 193 | return EvalResult(LHSResult.getValue() & RHSResult.getValue()); |
| 194 | case BinOpToken::BitwiseOr: |
| 195 | return EvalResult(LHSResult.getValue() | RHSResult.getValue()); |
| 196 | case BinOpToken::ShiftLeft: |
| 197 | return EvalResult(LHSResult.getValue() << RHSResult.getValue()); |
| 198 | case BinOpToken::ShiftRight: |
| 199 | return EvalResult(LHSResult.getValue() >> RHSResult.getValue()); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // Parse a symbol and return a (string, string) pair representing the symbol |
| 204 | // name and expression remaining to be parsed. |
| 205 | std::pair<StringRef, StringRef> parseSymbol(StringRef Expr) const { |
| 206 | size_t FirstNonSymbol = Expr.find_first_not_of("0123456789" |
| 207 | "abcdefghijklmnopqrstuvwxyz" |
| 208 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 209 | ":_.$"); |
| 210 | return std::make_pair(Expr.substr(0, FirstNonSymbol), |
| 211 | Expr.substr(FirstNonSymbol).ltrim()); |
| 212 | } |
| 213 | |
| 214 | // Evaluate a call to decode_operand. Decode the instruction operand at the |
| 215 | // given symbol and get the value of the requested operand. |
| 216 | // Returns an error if the instruction cannot be decoded, or the requested |
| 217 | // operand is not an immediate. |
| 218 | // On success, retuns a pair containing the value of the operand, plus |
| 219 | // the expression remaining to be evaluated. |
| 220 | std::pair<EvalResult, StringRef> evalDecodeOperand(StringRef Expr) const { |
| 221 | if (!Expr.startswith("(")) |
| 222 | return std::make_pair(unexpectedToken(Expr, Expr, "expected '('"), ""); |
| 223 | StringRef RemainingExpr = Expr.substr(1).ltrim(); |
| 224 | StringRef Symbol; |
| 225 | std::tie(Symbol, RemainingExpr) = parseSymbol(RemainingExpr); |
| 226 | |
| 227 | if (!Checker.isSymbolValid(Symbol)) |
| 228 | return std::make_pair( |
| 229 | EvalResult(("Cannot decode unknown symbol '" + Symbol + "'").str()), |
| 230 | ""); |
| 231 | |
| 232 | if (!RemainingExpr.startswith(",")) |
| 233 | return std::make_pair( |
| 234 | unexpectedToken(RemainingExpr, RemainingExpr, "expected ','"), ""); |
| 235 | RemainingExpr = RemainingExpr.substr(1).ltrim(); |
| 236 | |
| 237 | EvalResult OpIdxExpr; |
| 238 | std::tie(OpIdxExpr, RemainingExpr) = evalNumberExpr(RemainingExpr); |
| 239 | if (OpIdxExpr.hasError()) |
| 240 | return std::make_pair(OpIdxExpr, ""); |
| 241 | |
| 242 | if (!RemainingExpr.startswith(")")) |
| 243 | return std::make_pair( |
| 244 | unexpectedToken(RemainingExpr, RemainingExpr, "expected ')'"), ""); |
| 245 | RemainingExpr = RemainingExpr.substr(1).ltrim(); |
| 246 | |
| 247 | MCInst Inst; |
| 248 | uint64_t Size; |
| 249 | if (!decodeInst(Symbol, Inst, Size)) |
| 250 | return std::make_pair( |
| 251 | EvalResult(("Couldn't decode instruction at '" + Symbol + "'").str()), |
| 252 | ""); |
| 253 | |
| 254 | unsigned OpIdx = OpIdxExpr.getValue(); |
| 255 | if (OpIdx >= Inst.getNumOperands()) { |
| 256 | std::string ErrMsg; |
| 257 | raw_string_ostream ErrMsgStream(ErrMsg); |
| 258 | ErrMsgStream << "Invalid operand index '" << format("%i", OpIdx) |
| 259 | << "' for instruction '" << Symbol |
| 260 | << "'. Instruction has only " |
| 261 | << format("%i", Inst.getNumOperands()) |
| 262 | << " operands.\nInstruction is:\n "; |
| 263 | Inst.dump_pretty(ErrMsgStream, |
| 264 | Checker.Disassembler->getContext().getAsmInfo(), |
| 265 | Checker.InstPrinter); |
| 266 | return std::make_pair(EvalResult(ErrMsgStream.str()), ""); |
| 267 | } |
| 268 | |
| 269 | const MCOperand &Op = Inst.getOperand(OpIdx); |
| 270 | if (!Op.isImm()) { |
| 271 | std::string ErrMsg; |
| 272 | raw_string_ostream ErrMsgStream(ErrMsg); |
| 273 | ErrMsgStream << "Operand '" << format("%i", OpIdx) << "' of instruction '" |
| 274 | << Symbol << "' is not an immediate.\nInstruction is:\n "; |
| 275 | Inst.dump_pretty(ErrMsgStream, |
| 276 | Checker.Disassembler->getContext().getAsmInfo(), |
| 277 | Checker.InstPrinter); |
| 278 | |
| 279 | return std::make_pair(EvalResult(ErrMsgStream.str()), ""); |
| 280 | } |
| 281 | |
| 282 | return std::make_pair(EvalResult(Op.getImm()), RemainingExpr); |
| 283 | } |
| 284 | |
| 285 | // Evaluate a call to next_pc. |
| 286 | // Decode the instruction at the given symbol and return the following program |
| 287 | // counter. |
| 288 | // Returns an error if the instruction cannot be decoded. |
| 289 | // On success, returns a pair containing the next PC, plus of the |
| 290 | // expression remaining to be evaluated. |
| 291 | std::pair<EvalResult, StringRef> evalNextPC(StringRef Expr, |
| 292 | ParseContext PCtx) const { |
| 293 | if (!Expr.startswith("(")) |
| 294 | return std::make_pair(unexpectedToken(Expr, Expr, "expected '('"), ""); |
| 295 | StringRef RemainingExpr = Expr.substr(1).ltrim(); |
| 296 | StringRef Symbol; |
| 297 | std::tie(Symbol, RemainingExpr) = parseSymbol(RemainingExpr); |
| 298 | |
| 299 | if (!Checker.isSymbolValid(Symbol)) |
| 300 | return std::make_pair( |
| 301 | EvalResult(("Cannot decode unknown symbol '" + Symbol + "'").str()), |
| 302 | ""); |
| 303 | |
| 304 | if (!RemainingExpr.startswith(")")) |
| 305 | return std::make_pair( |
| 306 | unexpectedToken(RemainingExpr, RemainingExpr, "expected ')'"), ""); |
| 307 | RemainingExpr = RemainingExpr.substr(1).ltrim(); |
| 308 | |
| 309 | MCInst Inst; |
| 310 | uint64_t InstSize; |
| 311 | if (!decodeInst(Symbol, Inst, InstSize)) |
| 312 | return std::make_pair( |
| 313 | EvalResult(("Couldn't decode instruction at '" + Symbol + "'").str()), |
| 314 | ""); |
| 315 | |
| 316 | uint64_t SymbolAddr = PCtx.IsInsideLoad |
| 317 | ? Checker.getSymbolLinkerAddr(Symbol) |
| 318 | : Checker.getSymbolRemoteAddr(Symbol); |
| 319 | uint64_t NextPC = SymbolAddr + InstSize; |
| 320 | |
| 321 | return std::make_pair(EvalResult(NextPC), RemainingExpr); |
| 322 | } |
| 323 | |
| 324 | // Evaluate a call to stub_addr. |
| 325 | // Look up and return the address of the stub for the given |
| 326 | // (<file name>, <section name>, <symbol name>) tuple. |
| 327 | // On success, returns a pair containing the stub address, plus the expression |
| 328 | // remaining to be evaluated. |
| 329 | std::pair<EvalResult, StringRef> evalStubAddr(StringRef Expr, |
| 330 | ParseContext PCtx) const { |
| 331 | if (!Expr.startswith("(")) |
| 332 | return std::make_pair(unexpectedToken(Expr, Expr, "expected '('"), ""); |
| 333 | StringRef RemainingExpr = Expr.substr(1).ltrim(); |
| 334 | |
| 335 | // Handle file-name specially, as it may contain characters that aren't |
| 336 | // legal for symbols. |
| 337 | StringRef FileName; |
| 338 | size_t ComaIdx = RemainingExpr.find(','); |
| 339 | FileName = RemainingExpr.substr(0, ComaIdx).rtrim(); |
| 340 | RemainingExpr = RemainingExpr.substr(ComaIdx).ltrim(); |
| 341 | |
| 342 | if (!RemainingExpr.startswith(",")) |
| 343 | return std::make_pair( |
| 344 | unexpectedToken(RemainingExpr, Expr, "expected ','"), ""); |
| 345 | RemainingExpr = RemainingExpr.substr(1).ltrim(); |
| 346 | |
| 347 | StringRef SectionName; |
| 348 | std::tie(SectionName, RemainingExpr) = parseSymbol(RemainingExpr); |
| 349 | |
| 350 | if (!RemainingExpr.startswith(",")) |
| 351 | return std::make_pair( |
| 352 | unexpectedToken(RemainingExpr, Expr, "expected ','"), ""); |
| 353 | RemainingExpr = RemainingExpr.substr(1).ltrim(); |
| 354 | |
| 355 | StringRef Symbol; |
| 356 | std::tie(Symbol, RemainingExpr) = parseSymbol(RemainingExpr); |
| 357 | |
| 358 | if (!RemainingExpr.startswith(")")) |
| 359 | return std::make_pair( |
| 360 | unexpectedToken(RemainingExpr, Expr, "expected ')'"), ""); |
| 361 | RemainingExpr = RemainingExpr.substr(1).ltrim(); |
| 362 | |
| 363 | uint64_t StubAddr; |
| 364 | std::string ErrorMsg = ""; |
| 365 | std::tie(StubAddr, ErrorMsg) = Checker.getStubAddrFor( |
| 366 | FileName, SectionName, Symbol, PCtx.IsInsideLoad); |
| 367 | |
| 368 | if (ErrorMsg != "") |
| 369 | return std::make_pair(EvalResult(ErrorMsg), ""); |
| 370 | |
| 371 | return std::make_pair(EvalResult(StubAddr), RemainingExpr); |
| 372 | } |
| 373 | |
| 374 | std::pair<EvalResult, StringRef> evalSectionAddr(StringRef Expr, |
| 375 | ParseContext PCtx) const { |
| 376 | if (!Expr.startswith("(")) |
| 377 | return std::make_pair(unexpectedToken(Expr, Expr, "expected '('"), ""); |
| 378 | StringRef RemainingExpr = Expr.substr(1).ltrim(); |
| 379 | |
| 380 | // Handle file-name specially, as it may contain characters that aren't |
| 381 | // legal for symbols. |
| 382 | StringRef FileName; |
| 383 | size_t ComaIdx = RemainingExpr.find(','); |
| 384 | FileName = RemainingExpr.substr(0, ComaIdx).rtrim(); |
| 385 | RemainingExpr = RemainingExpr.substr(ComaIdx).ltrim(); |
| 386 | |
| 387 | if (!RemainingExpr.startswith(",")) |
| 388 | return std::make_pair( |
| 389 | unexpectedToken(RemainingExpr, Expr, "expected ','"), ""); |
| 390 | RemainingExpr = RemainingExpr.substr(1).ltrim(); |
| 391 | |
| 392 | StringRef SectionName; |
| 393 | std::tie(SectionName, RemainingExpr) = parseSymbol(RemainingExpr); |
| 394 | |
| 395 | if (!RemainingExpr.startswith(")")) |
| 396 | return std::make_pair( |
| 397 | unexpectedToken(RemainingExpr, Expr, "expected ')'"), ""); |
| 398 | RemainingExpr = RemainingExpr.substr(1).ltrim(); |
| 399 | |
| 400 | uint64_t StubAddr; |
| 401 | std::string ErrorMsg = ""; |
| 402 | std::tie(StubAddr, ErrorMsg) = Checker.getSectionAddr( |
| 403 | FileName, SectionName, PCtx.IsInsideLoad); |
| 404 | |
| 405 | if (ErrorMsg != "") |
| 406 | return std::make_pair(EvalResult(ErrorMsg), ""); |
| 407 | |
| 408 | return std::make_pair(EvalResult(StubAddr), RemainingExpr); |
| 409 | } |
| 410 | |
| 411 | // Evaluate an identiefer expr, which may be a symbol, or a call to |
| 412 | // one of the builtin functions: get_insn_opcode or get_insn_length. |
| 413 | // Return the result, plus the expression remaining to be parsed. |
| 414 | std::pair<EvalResult, StringRef> evalIdentifierExpr(StringRef Expr, |
| 415 | ParseContext PCtx) const { |
| 416 | StringRef Symbol; |
| 417 | StringRef RemainingExpr; |
| 418 | std::tie(Symbol, RemainingExpr) = parseSymbol(Expr); |
| 419 | |
| 420 | // Check for builtin function calls. |
| 421 | if (Symbol == "decode_operand") |
| 422 | return evalDecodeOperand(RemainingExpr); |
| 423 | else if (Symbol == "next_pc") |
| 424 | return evalNextPC(RemainingExpr, PCtx); |
| 425 | else if (Symbol == "stub_addr") |
| 426 | return evalStubAddr(RemainingExpr, PCtx); |
| 427 | else if (Symbol == "section_addr") |
| 428 | return evalSectionAddr(RemainingExpr, PCtx); |
| 429 | |
| 430 | if (!Checker.isSymbolValid(Symbol)) { |
| 431 | std::string ErrMsg("No known address for symbol '"); |
| 432 | ErrMsg += Symbol; |
| 433 | ErrMsg += "'"; |
| 434 | if (Symbol.startswith("L")) |
| 435 | ErrMsg += " (this appears to be an assembler local label - " |
| 436 | " perhaps drop the 'L'?)"; |
| 437 | |
| 438 | return std::make_pair(EvalResult(ErrMsg), ""); |
| 439 | } |
| 440 | |
| 441 | // The value for the symbol depends on the context we're evaluating in: |
| 442 | // Inside a load this is the address in the linker's memory, outside a |
| 443 | // load it's the address in the target processes memory. |
| 444 | uint64_t Value = PCtx.IsInsideLoad ? Checker.getSymbolLinkerAddr(Symbol) |
| 445 | : Checker.getSymbolRemoteAddr(Symbol); |
| 446 | |
| 447 | // Looks like a plain symbol reference. |
| 448 | return std::make_pair(EvalResult(Value), RemainingExpr); |
| 449 | } |
| 450 | |
| 451 | // Parse a number (hexadecimal or decimal) and return a (string, string) |
| 452 | // pair representing the number and the expression remaining to be parsed. |
| 453 | std::pair<StringRef, StringRef> parseNumberString(StringRef Expr) const { |
| 454 | size_t FirstNonDigit = StringRef::npos; |
| 455 | if (Expr.startswith("0x")) { |
| 456 | FirstNonDigit = Expr.find_first_not_of("0123456789abcdefABCDEF", 2); |
| 457 | if (FirstNonDigit == StringRef::npos) |
| 458 | FirstNonDigit = Expr.size(); |
| 459 | } else { |
| 460 | FirstNonDigit = Expr.find_first_not_of("0123456789"); |
| 461 | if (FirstNonDigit == StringRef::npos) |
| 462 | FirstNonDigit = Expr.size(); |
| 463 | } |
| 464 | return std::make_pair(Expr.substr(0, FirstNonDigit), |
| 465 | Expr.substr(FirstNonDigit)); |
| 466 | } |
| 467 | |
| 468 | // Evaluate a constant numeric expression (hexidecimal or decimal) and |
| 469 | // return a pair containing the result, and the expression remaining to be |
| 470 | // evaluated. |
| 471 | std::pair<EvalResult, StringRef> evalNumberExpr(StringRef Expr) const { |
| 472 | StringRef ValueStr; |
| 473 | StringRef RemainingExpr; |
| 474 | std::tie(ValueStr, RemainingExpr) = parseNumberString(Expr); |
| 475 | |
| 476 | if (ValueStr.empty() || !isdigit(ValueStr[0])) |
| 477 | return std::make_pair( |
| 478 | unexpectedToken(RemainingExpr, RemainingExpr, "expected number"), ""); |
| 479 | uint64_t Value; |
| 480 | ValueStr.getAsInteger(0, Value); |
| 481 | return std::make_pair(EvalResult(Value), RemainingExpr); |
| 482 | } |
| 483 | |
| 484 | // Evaluate an expression of the form "(<expr>)" and return a pair |
| 485 | // containing the result of evaluating <expr>, plus the expression |
| 486 | // remaining to be parsed. |
| 487 | std::pair<EvalResult, StringRef> evalParensExpr(StringRef Expr, |
| 488 | ParseContext PCtx) const { |
| 489 | assert(Expr.startswith("(") && "Not a parenthesized expression"); |
| 490 | EvalResult SubExprResult; |
| 491 | StringRef RemainingExpr; |
| 492 | std::tie(SubExprResult, RemainingExpr) = |
| 493 | evalComplexExpr(evalSimpleExpr(Expr.substr(1).ltrim(), PCtx), PCtx); |
| 494 | if (SubExprResult.hasError()) |
| 495 | return std::make_pair(SubExprResult, ""); |
| 496 | if (!RemainingExpr.startswith(")")) |
| 497 | return std::make_pair( |
| 498 | unexpectedToken(RemainingExpr, Expr, "expected ')'"), ""); |
| 499 | RemainingExpr = RemainingExpr.substr(1).ltrim(); |
| 500 | return std::make_pair(SubExprResult, RemainingExpr); |
| 501 | } |
| 502 | |
| 503 | // Evaluate an expression in one of the following forms: |
| 504 | // *{<number>}<expr> |
| 505 | // Return a pair containing the result, plus the expression remaining to be |
| 506 | // parsed. |
| 507 | std::pair<EvalResult, StringRef> evalLoadExpr(StringRef Expr) const { |
| 508 | assert(Expr.startswith("*") && "Not a load expression"); |
| 509 | StringRef RemainingExpr = Expr.substr(1).ltrim(); |
| 510 | |
| 511 | // Parse read size. |
| 512 | if (!RemainingExpr.startswith("{")) |
| 513 | return std::make_pair(EvalResult("Expected '{' following '*'."), ""); |
| 514 | RemainingExpr = RemainingExpr.substr(1).ltrim(); |
| 515 | EvalResult ReadSizeExpr; |
| 516 | std::tie(ReadSizeExpr, RemainingExpr) = evalNumberExpr(RemainingExpr); |
| 517 | if (ReadSizeExpr.hasError()) |
| 518 | return std::make_pair(ReadSizeExpr, RemainingExpr); |
| 519 | uint64_t ReadSize = ReadSizeExpr.getValue(); |
| 520 | if (ReadSize < 1 || ReadSize > 8) |
| 521 | return std::make_pair(EvalResult("Invalid size for dereference."), ""); |
| 522 | if (!RemainingExpr.startswith("}")) |
| 523 | return std::make_pair(EvalResult("Missing '}' for dereference."), ""); |
| 524 | RemainingExpr = RemainingExpr.substr(1).ltrim(); |
| 525 | |
| 526 | // Evaluate the expression representing the load address. |
| 527 | ParseContext LoadCtx(true); |
| 528 | EvalResult LoadAddrExprResult; |
| 529 | std::tie(LoadAddrExprResult, RemainingExpr) = |
| 530 | evalComplexExpr(evalSimpleExpr(RemainingExpr, LoadCtx), LoadCtx); |
| 531 | |
| 532 | if (LoadAddrExprResult.hasError()) |
| 533 | return std::make_pair(LoadAddrExprResult, ""); |
| 534 | |
| 535 | uint64_t LoadAddr = LoadAddrExprResult.getValue(); |
| 536 | |
| 537 | return std::make_pair( |
| 538 | EvalResult(Checker.readMemoryAtAddr(LoadAddr, ReadSize)), |
| 539 | RemainingExpr); |
| 540 | } |
| 541 | |
| 542 | // Evaluate a "simple" expression. This is any expression that _isn't_ an |
| 543 | // un-parenthesized binary expression. |
| 544 | // |
| 545 | // "Simple" expressions can be optionally bit-sliced. See evalSlicedExpr. |
| 546 | // |
| 547 | // Returns a pair containing the result of the evaluation, plus the |
| 548 | // expression remaining to be parsed. |
| 549 | std::pair<EvalResult, StringRef> evalSimpleExpr(StringRef Expr, |
| 550 | ParseContext PCtx) const { |
| 551 | EvalResult SubExprResult; |
| 552 | StringRef RemainingExpr; |
| 553 | |
| 554 | if (Expr.empty()) |
| 555 | return std::make_pair(EvalResult("Unexpected end of expression"), ""); |
| 556 | |
| 557 | if (Expr[0] == '(') |
| 558 | std::tie(SubExprResult, RemainingExpr) = evalParensExpr(Expr, PCtx); |
| 559 | else if (Expr[0] == '*') |
| 560 | std::tie(SubExprResult, RemainingExpr) = evalLoadExpr(Expr); |
| 561 | else if (isalpha(Expr[0]) || Expr[0] == '_') |
| 562 | std::tie(SubExprResult, RemainingExpr) = evalIdentifierExpr(Expr, PCtx); |
| 563 | else if (isdigit(Expr[0])) |
| 564 | std::tie(SubExprResult, RemainingExpr) = evalNumberExpr(Expr); |
| 565 | else |
| 566 | return std::make_pair( |
| 567 | unexpectedToken(Expr, Expr, |
| 568 | "expected '(', '*', identifier, or number"), ""); |
| 569 | |
| 570 | if (SubExprResult.hasError()) |
| 571 | return std::make_pair(SubExprResult, RemainingExpr); |
| 572 | |
| 573 | // Evaluate bit-slice if present. |
| 574 | if (RemainingExpr.startswith("[")) |
| 575 | std::tie(SubExprResult, RemainingExpr) = |
| 576 | evalSliceExpr(std::make_pair(SubExprResult, RemainingExpr)); |
| 577 | |
| 578 | return std::make_pair(SubExprResult, RemainingExpr); |
| 579 | } |
| 580 | |
| 581 | // Evaluate a bit-slice of an expression. |
| 582 | // A bit-slice has the form "<expr>[high:low]". The result of evaluating a |
| 583 | // slice is the bits between high and low (inclusive) in the original |
| 584 | // expression, right shifted so that the "low" bit is in position 0 in the |
| 585 | // result. |
| 586 | // Returns a pair containing the result of the slice operation, plus the |
| 587 | // expression remaining to be parsed. |
| 588 | std::pair<EvalResult, StringRef> |
| 589 | evalSliceExpr(std::pair<EvalResult, StringRef> Ctx) const { |
| 590 | EvalResult SubExprResult; |
| 591 | StringRef RemainingExpr; |
| 592 | std::tie(SubExprResult, RemainingExpr) = Ctx; |
| 593 | |
| 594 | assert(RemainingExpr.startswith("[") && "Not a slice expr."); |
| 595 | RemainingExpr = RemainingExpr.substr(1).ltrim(); |
| 596 | |
| 597 | EvalResult HighBitExpr; |
| 598 | std::tie(HighBitExpr, RemainingExpr) = evalNumberExpr(RemainingExpr); |
| 599 | |
| 600 | if (HighBitExpr.hasError()) |
| 601 | return std::make_pair(HighBitExpr, RemainingExpr); |
| 602 | |
| 603 | if (!RemainingExpr.startswith(":")) |
| 604 | return std::make_pair( |
| 605 | unexpectedToken(RemainingExpr, RemainingExpr, "expected ':'"), ""); |
| 606 | RemainingExpr = RemainingExpr.substr(1).ltrim(); |
| 607 | |
| 608 | EvalResult LowBitExpr; |
| 609 | std::tie(LowBitExpr, RemainingExpr) = evalNumberExpr(RemainingExpr); |
| 610 | |
| 611 | if (LowBitExpr.hasError()) |
| 612 | return std::make_pair(LowBitExpr, RemainingExpr); |
| 613 | |
| 614 | if (!RemainingExpr.startswith("]")) |
| 615 | return std::make_pair( |
| 616 | unexpectedToken(RemainingExpr, RemainingExpr, "expected ']'"), ""); |
| 617 | RemainingExpr = RemainingExpr.substr(1).ltrim(); |
| 618 | |
| 619 | unsigned HighBit = HighBitExpr.getValue(); |
| 620 | unsigned LowBit = LowBitExpr.getValue(); |
| 621 | uint64_t Mask = ((uint64_t)1 << (HighBit - LowBit + 1)) - 1; |
| 622 | uint64_t SlicedValue = (SubExprResult.getValue() >> LowBit) & Mask; |
| 623 | return std::make_pair(EvalResult(SlicedValue), RemainingExpr); |
| 624 | } |
| 625 | |
| 626 | // Evaluate a "complex" expression. |
| 627 | // Takes an already evaluated subexpression and checks for the presence of a |
| 628 | // binary operator, computing the result of the binary operation if one is |
| 629 | // found. Used to make arithmetic expressions left-associative. |
| 630 | // Returns a pair containing the ultimate result of evaluating the |
| 631 | // expression, plus the expression remaining to be evaluated. |
| 632 | std::pair<EvalResult, StringRef> |
| 633 | evalComplexExpr(std::pair<EvalResult, StringRef> LHSAndRemaining, |
| 634 | ParseContext PCtx) const { |
| 635 | EvalResult LHSResult; |
| 636 | StringRef RemainingExpr; |
| 637 | std::tie(LHSResult, RemainingExpr) = LHSAndRemaining; |
| 638 | |
| 639 | // If there was an error, or there's nothing left to evaluate, return the |
| 640 | // result. |
| 641 | if (LHSResult.hasError() || RemainingExpr == "") |
| 642 | return std::make_pair(LHSResult, RemainingExpr); |
| 643 | |
| 644 | // Otherwise check if this is a binary expressioan. |
| 645 | BinOpToken BinOp; |
| 646 | std::tie(BinOp, RemainingExpr) = parseBinOpToken(RemainingExpr); |
| 647 | |
| 648 | // If this isn't a recognized expression just return. |
| 649 | if (BinOp == BinOpToken::Invalid) |
| 650 | return std::make_pair(LHSResult, RemainingExpr); |
| 651 | |
| 652 | // This is a recognized bin-op. Evaluate the RHS, then evaluate the binop. |
| 653 | EvalResult RHSResult; |
| 654 | std::tie(RHSResult, RemainingExpr) = evalSimpleExpr(RemainingExpr, PCtx); |
| 655 | |
| 656 | // If there was an error evaluating the RHS, return it. |
| 657 | if (RHSResult.hasError()) |
| 658 | return std::make_pair(RHSResult, RemainingExpr); |
| 659 | |
| 660 | // This is a binary expression - evaluate and try to continue as a |
| 661 | // complex expr. |
| 662 | EvalResult ThisResult(computeBinOpResult(BinOp, LHSResult, RHSResult)); |
| 663 | |
| 664 | return evalComplexExpr(std::make_pair(ThisResult, RemainingExpr), PCtx); |
| 665 | } |
| 666 | |
| 667 | bool decodeInst(StringRef Symbol, MCInst &Inst, uint64_t &Size) const { |
| 668 | MCDisassembler *Dis = Checker.Disassembler; |
| 669 | StringRef SectionMem = Checker.getSubsectionStartingAt(Symbol); |
| 670 | ArrayRef<uint8_t> SectionBytes( |
| 671 | reinterpret_cast<const uint8_t *>(SectionMem.data()), |
| 672 | SectionMem.size()); |
| 673 | |
| 674 | MCDisassembler::DecodeStatus S = |
| 675 | Dis->getInstruction(Inst, Size, SectionBytes, 0, nulls(), nulls()); |
| 676 | |
| 677 | return (S == MCDisassembler::Success); |
| 678 | } |
| 679 | }; |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 682 | RuntimeDyldCheckerImpl::RuntimeDyldCheckerImpl(RuntimeDyld &RTDyld, |
| 683 | MCDisassembler *Disassembler, |
| 684 | MCInstPrinter *InstPrinter, |
| 685 | raw_ostream &ErrStream) |
| 686 | : RTDyld(RTDyld), Disassembler(Disassembler), InstPrinter(InstPrinter), |
| 687 | ErrStream(ErrStream) { |
| 688 | RTDyld.Checker = this; |
| 689 | } |
| 690 | |
| 691 | bool RuntimeDyldCheckerImpl::check(StringRef CheckExpr) const { |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 692 | CheckExpr = CheckExpr.trim(); |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 693 | DEBUG(dbgs() << "RuntimeDyldChecker: Checking '" << CheckExpr << "'...\n"); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 694 | RuntimeDyldCheckerExprEval P(*this, ErrStream); |
| 695 | bool Result = P.evaluate(CheckExpr); |
| 696 | (void)Result; |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 697 | DEBUG(dbgs() << "RuntimeDyldChecker: '" << CheckExpr << "' " |
| 698 | << (Result ? "passed" : "FAILED") << ".\n"); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 699 | return Result; |
| 700 | } |
| 701 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 702 | bool RuntimeDyldCheckerImpl::checkAllRulesInBuffer(StringRef RulePrefix, |
| 703 | MemoryBuffer *MemBuf) const { |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 704 | bool DidAllTestsPass = true; |
| 705 | unsigned NumRules = 0; |
| 706 | |
| 707 | const char *LineStart = MemBuf->getBufferStart(); |
| 708 | |
| 709 | // Eat whitespace. |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 710 | while (LineStart != MemBuf->getBufferEnd() && std::isspace(*LineStart)) |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 711 | ++LineStart; |
| 712 | |
| 713 | while (LineStart != MemBuf->getBufferEnd() && *LineStart != '\0') { |
| 714 | const char *LineEnd = LineStart; |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 715 | while (LineEnd != MemBuf->getBufferEnd() && *LineEnd != '\r' && |
| 716 | *LineEnd != '\n') |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 717 | ++LineEnd; |
| 718 | |
| 719 | StringRef Line(LineStart, LineEnd - LineStart); |
| 720 | if (Line.startswith(RulePrefix)) { |
| 721 | DidAllTestsPass &= check(Line.substr(RulePrefix.size())); |
| 722 | ++NumRules; |
| 723 | } |
| 724 | |
| 725 | // Eat whitespace. |
| 726 | LineStart = LineEnd; |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 727 | while (LineStart != MemBuf->getBufferEnd() && std::isspace(*LineStart)) |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 728 | ++LineStart; |
| 729 | } |
| 730 | return DidAllTestsPass && (NumRules != 0); |
| 731 | } |
| 732 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 733 | bool RuntimeDyldCheckerImpl::isSymbolValid(StringRef Symbol) const { |
| 734 | return getRTDyld().getSymbolAddress(Symbol) != nullptr; |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 737 | uint64_t RuntimeDyldCheckerImpl::getSymbolLinkerAddr(StringRef Symbol) const { |
| 738 | return static_cast<uint64_t>( |
| 739 | reinterpret_cast<uintptr_t>(getRTDyld().getSymbolAddress(Symbol))); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 742 | uint64_t RuntimeDyldCheckerImpl::getSymbolRemoteAddr(StringRef Symbol) const { |
| 743 | return getRTDyld().getAnySymbolRemoteAddress(Symbol); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 746 | uint64_t RuntimeDyldCheckerImpl::readMemoryAtAddr(uint64_t SrcAddr, |
| 747 | unsigned Size) const { |
| 748 | uintptr_t PtrSizedAddr = static_cast<uintptr_t>(SrcAddr); |
| 749 | assert(PtrSizedAddr == SrcAddr && "Linker memory pointer out-of-range."); |
| 750 | uint8_t *Src = reinterpret_cast<uint8_t*>(PtrSizedAddr); |
| 751 | return getRTDyld().readBytesUnaligned(Src, Size); |
| 752 | } |
| 753 | |
| 754 | |
| 755 | std::pair<const RuntimeDyldCheckerImpl::SectionAddressInfo*, std::string> |
| 756 | RuntimeDyldCheckerImpl::findSectionAddrInfo(StringRef FileName, |
| 757 | StringRef SectionName) const { |
| 758 | |
| 759 | auto SectionMapItr = Stubs.find(FileName); |
| 760 | if (SectionMapItr == Stubs.end()) { |
| 761 | std::string ErrorMsg = "File '"; |
| 762 | ErrorMsg += FileName; |
| 763 | ErrorMsg += "' not found. "; |
| 764 | if (Stubs.empty()) |
| 765 | ErrorMsg += "No stubs registered."; |
| 766 | else { |
| 767 | ErrorMsg += "Available files are:"; |
| 768 | for (const auto& StubEntry : Stubs) { |
| 769 | ErrorMsg += " '"; |
| 770 | ErrorMsg += StubEntry.first; |
| 771 | ErrorMsg += "'"; |
| 772 | } |
| 773 | } |
| 774 | ErrorMsg += "\n"; |
| 775 | return std::make_pair(nullptr, ErrorMsg); |
| 776 | } |
| 777 | |
| 778 | auto SectionInfoItr = SectionMapItr->second.find(SectionName); |
| 779 | if (SectionInfoItr == SectionMapItr->second.end()) |
| 780 | return std::make_pair(nullptr, |
| 781 | ("Section '" + SectionName + "' not found in file '" + |
| 782 | FileName + "'\n").str()); |
| 783 | |
| 784 | return std::make_pair(&SectionInfoItr->second, std::string("")); |
| 785 | } |
| 786 | |
| 787 | std::pair<uint64_t, std::string> RuntimeDyldCheckerImpl::getSectionAddr( |
| 788 | StringRef FileName, StringRef SectionName, bool IsInsideLoad) const { |
| 789 | |
| 790 | const SectionAddressInfo *SectionInfo = nullptr; |
| 791 | { |
| 792 | std::string ErrorMsg; |
| 793 | std::tie(SectionInfo, ErrorMsg) = |
| 794 | findSectionAddrInfo(FileName, SectionName); |
| 795 | if (ErrorMsg != "") |
| 796 | return std::make_pair(0, ErrorMsg); |
| 797 | } |
| 798 | |
| 799 | unsigned SectionID = SectionInfo->SectionID; |
| 800 | uint64_t Addr; |
| 801 | if (IsInsideLoad) |
| 802 | Addr = |
| 803 | static_cast<uint64_t>( |
| 804 | reinterpret_cast<uintptr_t>(getRTDyld().Sections[SectionID].Address)); |
| 805 | else |
| 806 | Addr = getRTDyld().Sections[SectionID].LoadAddress; |
| 807 | |
| 808 | return std::make_pair(Addr, std::string("")); |
| 809 | } |
| 810 | |
| 811 | std::pair<uint64_t, std::string> RuntimeDyldCheckerImpl::getStubAddrFor( |
| 812 | StringRef FileName, StringRef SectionName, StringRef SymbolName, |
| 813 | bool IsInsideLoad) const { |
| 814 | |
| 815 | const SectionAddressInfo *SectionInfo = nullptr; |
| 816 | { |
| 817 | std::string ErrorMsg; |
| 818 | std::tie(SectionInfo, ErrorMsg) = |
| 819 | findSectionAddrInfo(FileName, SectionName); |
| 820 | if (ErrorMsg != "") |
| 821 | return std::make_pair(0, ErrorMsg); |
| 822 | } |
| 823 | |
| 824 | unsigned SectionID = SectionInfo->SectionID; |
| 825 | const StubOffsetsMap &SymbolStubs = SectionInfo->StubOffsets; |
| 826 | auto StubOffsetItr = SymbolStubs.find(SymbolName); |
| 827 | if (StubOffsetItr == SymbolStubs.end()) |
| 828 | return std::make_pair(0, |
| 829 | ("Stub for symbol '" + SymbolName + "' not found. " |
| 830 | "If '" + SymbolName + "' is an internal symbol this " |
| 831 | "may indicate that the stub target offset is being " |
| 832 | "computed incorrectly.\n").str()); |
| 833 | |
| 834 | uint64_t StubOffset = StubOffsetItr->second; |
| 835 | |
| 836 | uint64_t Addr; |
| 837 | if (IsInsideLoad) { |
| 838 | uintptr_t SectionBase = |
| 839 | reinterpret_cast<uintptr_t>(getRTDyld().Sections[SectionID].Address); |
| 840 | Addr = static_cast<uint64_t>(SectionBase) + StubOffset; |
| 841 | } else { |
| 842 | uint64_t SectionBase = getRTDyld().Sections[SectionID].LoadAddress; |
| 843 | Addr = SectionBase + StubOffset; |
| 844 | } |
| 845 | |
| 846 | return std::make_pair(Addr, std::string("")); |
| 847 | } |
| 848 | |
| 849 | StringRef |
| 850 | RuntimeDyldCheckerImpl::getSubsectionStartingAt(StringRef Name) const { |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 851 | RuntimeDyldImpl::SymbolTableMap::const_iterator pos = |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 852 | getRTDyld().GlobalSymbolTable.find(Name); |
| 853 | if (pos == getRTDyld().GlobalSymbolTable.end()) |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 854 | return StringRef(); |
| 855 | RuntimeDyldImpl::SymbolLoc Loc = pos->second; |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 856 | uint8_t *SectionAddr = getRTDyld().getSectionAddress(Loc.first); |
| 857 | return StringRef(reinterpret_cast<const char *>(SectionAddr) + Loc.second, |
| 858 | getRTDyld().Sections[Loc.first].Size - Loc.second); |
| 859 | } |
| 860 | |
| 861 | void RuntimeDyldCheckerImpl::registerSection( |
| 862 | StringRef FilePath, unsigned SectionID) { |
| 863 | StringRef FileName = sys::path::filename(FilePath); |
| 864 | const SectionEntry &Section = getRTDyld().Sections[SectionID]; |
| 865 | StringRef SectionName = Section.Name; |
| 866 | |
| 867 | Stubs[FileName][SectionName].SectionID = SectionID; |
| 868 | } |
| 869 | |
| 870 | void RuntimeDyldCheckerImpl::registerStubMap( |
| 871 | StringRef FilePath, unsigned SectionID, |
| 872 | const RuntimeDyldImpl::StubMap &RTDyldStubs) { |
| 873 | StringRef FileName = sys::path::filename(FilePath); |
| 874 | const SectionEntry &Section = getRTDyld().Sections[SectionID]; |
| 875 | StringRef SectionName = Section.Name; |
| 876 | |
| 877 | Stubs[FileName][SectionName].SectionID = SectionID; |
| 878 | |
| 879 | for (auto &StubMapEntry : RTDyldStubs) { |
| 880 | std::string SymbolName = ""; |
| 881 | |
| 882 | if (StubMapEntry.first.SymbolName) |
| 883 | SymbolName = StubMapEntry.first.SymbolName; |
| 884 | else { |
| 885 | // If this is a (Section, Offset) pair, do a reverse lookup in the |
| 886 | // global symbol table to find the name. |
| 887 | for (auto &GSTEntry : getRTDyld().GlobalSymbolTable) { |
| 888 | if (GSTEntry.second.first == StubMapEntry.first.SectionID && |
| 889 | GSTEntry.second.second == |
| 890 | static_cast<uint64_t>(StubMapEntry.first.Offset)) { |
| 891 | SymbolName = GSTEntry.first(); |
| 892 | break; |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | if (SymbolName != "") |
| 898 | Stubs[FileName][SectionName].StubOffsets[SymbolName] = |
| 899 | StubMapEntry.second; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | RuntimeDyldChecker::RuntimeDyldChecker(RuntimeDyld &RTDyld, |
| 904 | MCDisassembler *Disassembler, |
| 905 | MCInstPrinter *InstPrinter, |
| 906 | raw_ostream &ErrStream) |
| 907 | : Impl(make_unique<RuntimeDyldCheckerImpl>(RTDyld, Disassembler, |
| 908 | InstPrinter, ErrStream)) {} |
| 909 | |
| 910 | RuntimeDyldChecker::~RuntimeDyldChecker() {} |
| 911 | |
| 912 | RuntimeDyld& RuntimeDyldChecker::getRTDyld() { |
| 913 | return Impl->RTDyld; |
| 914 | } |
| 915 | |
| 916 | const RuntimeDyld& RuntimeDyldChecker::getRTDyld() const { |
| 917 | return Impl->RTDyld; |
| 918 | } |
| 919 | |
| 920 | bool RuntimeDyldChecker::check(StringRef CheckExpr) const { |
| 921 | return Impl->check(CheckExpr); |
| 922 | } |
| 923 | |
| 924 | bool RuntimeDyldChecker::checkAllRulesInBuffer(StringRef RulePrefix, |
| 925 | MemoryBuffer *MemBuf) const { |
| 926 | return Impl->checkAllRulesInBuffer(RulePrefix, MemBuf); |
| 927 | } |
| 928 | |
| 929 | std::pair<uint64_t, std::string> |
| 930 | RuntimeDyldChecker::getSectionAddr(StringRef FileName, StringRef SectionName, |
| 931 | bool LinkerAddress) { |
| 932 | return Impl->getSectionAddr(FileName, SectionName, LinkerAddress); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 933 | } |