Disassembler.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //===- llvm/unittest/Object/Disassembler.cpp ------------------------------===//
  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. #include "llvm-c/Disassembler.h"
  10. #include "llvm/Support/TargetSelect.h"
  11. #include "gtest/gtest.h"
  12. using namespace llvm;
  13. static const char *symbolLookupCallback(void *DisInfo, uint64_t ReferenceValue,
  14. uint64_t *ReferenceType,
  15. uint64_t ReferencePC,
  16. const char **ReferenceName) {
  17. *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
  18. return nullptr;
  19. }
  20. TEST(Disassembler, Test1) {
  21. llvm::InitializeAllTargetInfos();
  22. llvm::InitializeAllTargetMCs();
  23. llvm::InitializeAllDisassemblers();
  24. uint8_t Bytes[] = {0x90, 0x90, 0xeb, 0xfd};
  25. uint8_t *BytesP = Bytes;
  26. const char OutStringSize = 100;
  27. char OutString[OutStringSize];
  28. LLVMDisasmContextRef DCR = LLVMCreateDisasm("x86_64-pc-linux", nullptr, 0,
  29. nullptr, symbolLookupCallback);
  30. if (!DCR)
  31. return;
  32. size_t InstSize;
  33. unsigned NumBytes = sizeof(Bytes);
  34. unsigned PC = 0;
  35. InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
  36. OutStringSize);
  37. EXPECT_EQ(InstSize, 1U);
  38. EXPECT_EQ(StringRef(OutString), "\tnop");
  39. PC += InstSize;
  40. BytesP += InstSize;
  41. NumBytes -= InstSize;
  42. InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
  43. OutStringSize);
  44. EXPECT_EQ(InstSize, 1U);
  45. EXPECT_EQ(StringRef(OutString), "\tnop");
  46. PC += InstSize;
  47. BytesP += InstSize;
  48. NumBytes -= InstSize;
  49. InstSize = LLVMDisasmInstruction(DCR, BytesP, NumBytes, PC, OutString,
  50. OutStringSize);
  51. EXPECT_EQ(InstSize, 2U);
  52. EXPECT_EQ(StringRef(OutString), "\tjmp\t0x1");
  53. LLVMDisasmDispose(DCR);
  54. }