1 // AsmJit - Machine code generation for C++
2 //
3 //  * Official AsmJit Home Page: https://asmjit.com
4 //  * Official Github Repository: https://github.com/asmjit/asmjit
5 //
6 // Copyright (c) 2008-2020 The AsmJit Authors
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 //    claim that you wrote the original software. If you use this software
18 //    in a product, an acknowledgment in the product documentation would be
19 //    appreciated but is not required.
20 // 2. Altered source versions must be plainly marked as such, and must not be
21 //    misrepresented as being the original software.
22 // 3. This notice may not be removed or altered from any source distribution.
23 
24 // ----------------------------------------------------------------------------
25 // This is a working example that demonstrates how multiple sections can be
26 // used in a JIT-based code generator. It shows also the necessary tooling
27 // that is expected to be done by the user when the feature is used. It's
28 // important to handle the following cases:
29 //
30 //   - Assign offsets to sections when the code generation is finished.
31 //   - Tell the CodeHolder to resolve unresolved links and check whether
32 //     all links were resolved.
33 //   - Relocate the code
34 //   - Copy the code to the destination address.
35 // ----------------------------------------------------------------------------
36 
37 #include <asmjit/x86.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 using namespace asmjit;
43 
44 // The generated function is very simple, it only accesses the built-in data
45 // (from .data section) at the index as provided by its first argument. This
46 // data is inlined into the resulting function so we can use it this array
47 // for verification that the function returns correct values.
48 static const uint8_t dataArray[] = { 2, 9, 4, 7, 1, 3, 8, 5, 6, 0 };
49 
fail(const char * message,Error err)50 static void fail(const char* message, Error err) {
51   printf("%s: %s\n", message, DebugUtils::errorAsString(err));
52   exit(1);
53 }
54 
main()55 int main() {
56   printf("AsmJit X86 Sections Test\n\n");
57 
58   Environment env = hostEnvironment();
59   JitAllocator allocator;
60 
61 #ifndef ASMJIT_NO_LOGGING
62   FileLogger logger(stdout);
63   logger.setIndentation(FormatOptions::kIndentationCode, 2);
64 #endif
65 
66   CodeHolder code;
67   code.init(env);
68 
69 #ifndef ASMJIT_NO_LOGGING
70   code.setLogger(&logger);
71 #endif
72 
73   Section* dataSection;
74   Error err = code.newSection(&dataSection, ".data", SIZE_MAX, 0, 8);
75 
76   if (err) {
77     fail("Failed to create a .data section", err);
78   }
79   else {
80     printf("Generating code:\n");
81     x86::Assembler a(&code);
82     x86::Gp idx = a.zax();
83     x86::Gp addr = a.zcx();
84 
85     Label data = a.newLabel();
86 
87     FuncDetail func;
88     func.init(FuncSignatureT<size_t, size_t>(CallConv::kIdHost), code.environment());
89 
90     FuncFrame frame;
91     frame.init(func);
92     frame.addDirtyRegs(idx, addr);
93 
94     FuncArgsAssignment args(&func);
95     args.assignAll(idx);
96     args.updateFuncFrame(frame);
97     frame.finalize();
98 
99     a.emitProlog(frame);
100     a.emitArgsAssignment(frame, args);
101 
102     a.lea(addr, x86::ptr(data));
103     a.movzx(idx, x86::byte_ptr(addr, idx));
104 
105     a.emitEpilog(frame);
106 
107     a.section(dataSection);
108     a.bind(data);
109 
110     a.embed(dataArray, sizeof(dataArray));
111   }
112 
113   // Manually change he offsets of each section, start at 0. This code is very
114   // similar to what `CodeHolder::flatten()` does, however, it's shown here
115   // how to do it explicitly.
116   printf("\nCalculating section offsets:\n");
117   uint64_t offset = 0;
118   for (Section* section : code.sections()) {
119     offset = Support::alignUp(offset, section->alignment());
120     section->setOffset(offset);
121     offset += section->realSize();
122 
123     printf("  [0x%08X %s] {Id=%u Size=%u}\n",
124            uint32_t(section->offset()),
125            section->name(),
126            section->id(),
127            uint32_t(section->realSize()));
128   }
129   size_t codeSize = size_t(offset);
130   printf("  Final code size: %zu\n", codeSize);
131 
132   // Resolve cross-section links (if any). On 32-bit X86 this is not necessary
133   // as this is handled through relocations as the addressing is different.
134   if (code.hasUnresolvedLinks()) {
135     printf("\nResolving cross-section links:\n");
136     printf("  Before 'resolveUnresolvedLinks()': %zu\n", code.unresolvedLinkCount());
137 
138     err = code.resolveUnresolvedLinks();
139     if (err)
140       fail("Failed to resolve cross-section links", err);
141     printf("  After 'resolveUnresolvedLinks()': %zu\n", code.unresolvedLinkCount());
142   }
143 
144   // Allocate memory for the function and relocate it there.
145   void* roPtr;
146   void* rwPtr;
147   err = allocator.alloc(&roPtr, &rwPtr, codeSize);
148   if (err)
149     fail("Failed to allocate executable memory", err);
150 
151   // Relocate to the base-address of the allocated memory.
152   code.relocateToBase(uint64_t(uintptr_t(roPtr)));
153 
154   // Copy the flattened code into `mem.rw`. There are two ways. You can either copy
155   // everything manually by iterating over all sections or use `copyFlattenedData`.
156   // This code is similar to what `copyFlattenedData(p, codeSize, 0)` would do:
157   for (Section* section : code.sections())
158     memcpy(static_cast<uint8_t*>(rwPtr) + size_t(section->offset()), section->data(), section->bufferSize());
159 
160   // Execute the function and test whether it works.
161   typedef size_t (*Func)(size_t idx);
162   Func fn = (Func)roPtr;
163 
164   printf("\n");
165   if (fn(0) != dataArray[0] ||
166       fn(3) != dataArray[3] ||
167       fn(6) != dataArray[6] ||
168       fn(9) != dataArray[9] ) {
169     printf("Failure:\n  The generated function returned incorrect result(s)\n");
170     return 1;
171   }
172 
173   printf("Success:\n  The generated function returned expected results\n");
174   return 0;
175 }
176