asmjit_test_x86_sections.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // [AsmJit]
  2. // Machine Code Generation for C++.
  3. //
  4. // [License]
  5. // Zlib - See LICENSE.md file in the package.
  6. // This is a working example that demonstrates how multiple sections can be
  7. // used in a JIT-based code generator. It shows also the necessary tooling
  8. // that is expected to be done by the user when the feature is used. It's
  9. // important to handle the following cases:
  10. //
  11. // - Assign offsets to sections when the code generation is finished.
  12. // - Tell the CodeHolder to resolve unresolved links and check whether
  13. // all links were resolved.
  14. // - Relocate the code
  15. // - Copy the code to the location you want.
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "./asmjit.h"
  20. using namespace asmjit;
  21. // The generated function is very simple, it only accesses the built-in data
  22. // (from .data section) at the index as provided by its first argument. This
  23. // data is inlined into the resulting function so we can use it this array
  24. // for verification that the function returns correct values.
  25. static const uint8_t dataArray[] = { 2, 9, 4, 7, 1, 3, 8, 5, 6, 0 };
  26. static void fail(const char* message, Error err) {
  27. printf("%s: %s\n", message, DebugUtils::errorAsString(err));
  28. exit(1);
  29. }
  30. int main(int argc, char* argv[]) {
  31. ASMJIT_UNUSED(argc);
  32. ASMJIT_UNUSED(argv);
  33. CodeInfo codeInfo(ArchInfo::kIdHost);
  34. JitAllocator allocator;
  35. FileLogger logger(stdout);
  36. logger.setIndentation(FormatOptions::kIndentationCode, 2);
  37. CodeHolder code;
  38. code.init(codeInfo);
  39. code.setLogger(&logger);
  40. Section* section;
  41. Error err = code.newSection(&section, ".data", SIZE_MAX, 0, 8);
  42. if (err) {
  43. fail("Failed to create a .data section", err);
  44. }
  45. else {
  46. printf("Generating code:\n");
  47. x86::Assembler a(&code);
  48. x86::Gp idx = a.zax();
  49. x86::Gp addr = a.zcx();
  50. Label data = a.newLabel();
  51. FuncDetail func;
  52. func.init(FuncSignatureT<size_t, size_t>(CallConv::kIdHost));
  53. FuncFrame frame;
  54. frame.init(func);
  55. frame.addDirtyRegs(idx, addr);
  56. FuncArgsAssignment args(&func);
  57. args.assignAll(idx);
  58. args.updateFuncFrame(frame);
  59. frame.finalize();
  60. a.emitProlog(frame);
  61. a.emitArgsAssignment(frame, args);
  62. a.lea(addr, x86::ptr(data));
  63. a.movzx(idx, x86::byte_ptr(addr, idx));
  64. a.emitEpilog(frame);
  65. a.section(section);
  66. a.bind(data);
  67. a.embed(dataArray, sizeof(dataArray));
  68. }
  69. // Manually change he offsets of each section, start at 0. This code is very
  70. // similar to what `CodeHolder::flatten()` does, however, it's shown here
  71. // how to do it explicitly.
  72. printf("\nCalculating section offsets:\n");
  73. uint64_t offset = 0;
  74. for (Section* section : code.sections()) {
  75. offset = Support::alignUp(offset, section->alignment());
  76. section->setOffset(offset);
  77. offset += section->realSize();
  78. printf(" [0x%08X %s] {Id=%u Size=%u}\n",
  79. uint32_t(section->offset()),
  80. section->name(),
  81. section->id(),
  82. uint32_t(section->realSize()));
  83. }
  84. size_t codeSize = size_t(offset);
  85. printf(" Final code size: %zu\n", codeSize);
  86. // Resolve cross-section links (if any). On 32-bit X86 this is not necessary
  87. // as this is handled through relocations as the addressing is different.
  88. if (code.hasUnresolvedLinks()) {
  89. printf("\nResolving cross-section links:\n");
  90. printf(" Before 'resolveUnresolvedLinks()': %zu\n", code.unresolvedLinkCount());
  91. err = code.resolveUnresolvedLinks();
  92. if (err)
  93. fail("Failed to resolve cross-section links", err);
  94. printf(" After 'resolveUnresolvedLinks()': %zu\n", code.unresolvedLinkCount());
  95. }
  96. // Allocate memory for the function and relocate it there.
  97. void* roPtr;
  98. void* rwPtr;
  99. err = allocator.alloc(&roPtr, &rwPtr, codeSize);
  100. if (err)
  101. fail("Failed to allocate executable memory", err);
  102. // Relocate to the base-address of the allocated memory.
  103. code.relocateToBase(uint64_t(uintptr_t(roPtr)));
  104. // Copy the flattened code into `mem.rw`. There are two ways. You can either copy
  105. // everything manually by iterating over all sections or use `copyFlattenedData`.
  106. // This code is similar to what `copyFlattenedData(p, codeSize, 0)` would do:
  107. for (Section* section : code.sections())
  108. memcpy(static_cast<uint8_t*>(rwPtr) + size_t(section->offset()), section->data(), section->bufferSize());
  109. // Execute the function and test whether it works.
  110. typedef size_t (*Func)(size_t idx);
  111. Func fn = (Func)roPtr;
  112. printf("\nTesting the generated function:\n");
  113. if (fn(0) != dataArray[0] ||
  114. fn(3) != dataArray[3] ||
  115. fn(6) != dataArray[6] ||
  116. fn(9) != dataArray[9] ) {
  117. printf(" [FAILED] The generated function returned incorrect result(s)\n");
  118. return 1;
  119. }
  120. else {
  121. printf(" [PASSED] The generated function returned expected results\n");
  122. }
  123. allocator.release((void*)fn);
  124. return 0;
  125. }