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