xref: /aosp_15_r20/external/llvm/docs/MCJITDesignAndImplementation.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker===============================
2*9880d681SAndroid Build Coastguard WorkerMCJIT Design and Implementation
3*9880d681SAndroid Build Coastguard Worker===============================
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard WorkerIntroduction
6*9880d681SAndroid Build Coastguard Worker============
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard WorkerThis document describes the internal workings of the MCJIT execution
9*9880d681SAndroid Build Coastguard Workerengine and the RuntimeDyld component.  It is intended as a high level
10*9880d681SAndroid Build Coastguard Workeroverview of the implementation, showing the flow and interactions of
11*9880d681SAndroid Build Coastguard Workerobjects throughout the code generation and dynamic loading process.
12*9880d681SAndroid Build Coastguard Worker
13*9880d681SAndroid Build Coastguard WorkerEngine Creation
14*9880d681SAndroid Build Coastguard Worker===============
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard WorkerIn most cases, an EngineBuilder object is used to create an instance of
17*9880d681SAndroid Build Coastguard Workerthe MCJIT execution engine.  The EngineBuilder takes an llvm::Module
18*9880d681SAndroid Build Coastguard Workerobject as an argument to its constructor.  The client may then set various
19*9880d681SAndroid Build Coastguard Workeroptions that we control the later be passed along to the MCJIT engine,
20*9880d681SAndroid Build Coastguard Workerincluding the selection of MCJIT as the engine type to be created.
21*9880d681SAndroid Build Coastguard WorkerOf particular interest is the EngineBuilder::setMCJITMemoryManager
22*9880d681SAndroid Build Coastguard Workerfunction.  If the client does not explicitly create a memory manager at
23*9880d681SAndroid Build Coastguard Workerthis time, a default memory manager (specifically SectionMemoryManager)
24*9880d681SAndroid Build Coastguard Workerwill be created when the MCJIT engine is instantiated.
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard WorkerOnce the options have been set, a client calls EngineBuilder::create to
27*9880d681SAndroid Build Coastguard Workercreate an instance of the MCJIT engine.  If the client does not use the
28*9880d681SAndroid Build Coastguard Workerform of this function that takes a TargetMachine as a parameter, a new
29*9880d681SAndroid Build Coastguard WorkerTargetMachine will be created based on the target triple associated with
30*9880d681SAndroid Build Coastguard Workerthe Module that was used to create the EngineBuilder.
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker.. image:: MCJIT-engine-builder.png
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard WorkerEngineBuilder::create will call the static MCJIT::createJIT function,
35*9880d681SAndroid Build Coastguard Workerpassing in its pointers to the module, memory manager and target machine
36*9880d681SAndroid Build Coastguard Workerobjects, all of which will subsequently be owned by the MCJIT object.
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard WorkerThe MCJIT class has a member variable, Dyld, which contains an instance of
39*9880d681SAndroid Build Coastguard Workerthe RuntimeDyld wrapper class.  This member will be used for
40*9880d681SAndroid Build Coastguard Workercommunications between MCJIT and the actual RuntimeDyldImpl object that
41*9880d681SAndroid Build Coastguard Workergets created when an object is loaded.
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker.. image:: MCJIT-creation.png
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard WorkerUpon creation, MCJIT holds a pointer to the Module object that it received
46*9880d681SAndroid Build Coastguard Workerfrom EngineBuilder but it does not immediately generate code for this
47*9880d681SAndroid Build Coastguard Workermodule.  Code generation is deferred until either the
48*9880d681SAndroid Build Coastguard WorkerMCJIT::finalizeObject method is called explicitly or a function such as
49*9880d681SAndroid Build Coastguard WorkerMCJIT::getPointerToFunction is called which requires the code to have been
50*9880d681SAndroid Build Coastguard Workergenerated.
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard WorkerCode Generation
53*9880d681SAndroid Build Coastguard Worker===============
54*9880d681SAndroid Build Coastguard Worker
55*9880d681SAndroid Build Coastguard WorkerWhen code generation is triggered, as described above, MCJIT will first
56*9880d681SAndroid Build Coastguard Workerattempt to retrieve an object image from its ObjectCache member, if one
57*9880d681SAndroid Build Coastguard Workerhas been set.  If a cached object image cannot be retrieved, MCJIT will
58*9880d681SAndroid Build Coastguard Workercall its emitObject method.  MCJIT::emitObject uses a local PassManager
59*9880d681SAndroid Build Coastguard Workerinstance and creates a new ObjectBufferStream instance, both of which it
60*9880d681SAndroid Build Coastguard Workerpasses to TargetMachine::addPassesToEmitMC before calling PassManager::run
61*9880d681SAndroid Build Coastguard Workeron the Module with which it was created.
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker.. image:: MCJIT-load.png
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard WorkerThe PassManager::run call causes the MC code generation mechanisms to emit
66*9880d681SAndroid Build Coastguard Workera complete relocatable binary object image (either in either ELF or MachO
67*9880d681SAndroid Build Coastguard Workerformat, depending on the target) into the ObjectBufferStream object, which
68*9880d681SAndroid Build Coastguard Workeris flushed to complete the process.  If an ObjectCache is being used, the
69*9880d681SAndroid Build Coastguard Workerimage will be passed to the ObjectCache here.
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard WorkerAt this point, the ObjectBufferStream contains the raw object image.
72*9880d681SAndroid Build Coastguard WorkerBefore the code can be executed, the code and data sections from this
73*9880d681SAndroid Build Coastguard Workerimage must be loaded into suitable memory, relocations must be applied and
74*9880d681SAndroid Build Coastguard Workermemory permission and code cache invalidation (if required) must be completed.
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard WorkerObject Loading
77*9880d681SAndroid Build Coastguard Worker==============
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard WorkerOnce an object image has been obtained, either through code generation or
80*9880d681SAndroid Build Coastguard Workerhaving been retrieved from an ObjectCache, it is passed to RuntimeDyld to
81*9880d681SAndroid Build Coastguard Workerbe loaded.  The RuntimeDyld wrapper class examines the object to determine
82*9880d681SAndroid Build Coastguard Workerits file format and creates an instance of either RuntimeDyldELF or
83*9880d681SAndroid Build Coastguard WorkerRuntimeDyldMachO (both of which derive from the RuntimeDyldImpl base
84*9880d681SAndroid Build Coastguard Workerclass) and calls the RuntimeDyldImpl::loadObject method to perform that
85*9880d681SAndroid Build Coastguard Workeractual loading.
86*9880d681SAndroid Build Coastguard Worker
87*9880d681SAndroid Build Coastguard Worker.. image:: MCJIT-dyld-load.png
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard WorkerRuntimeDyldImpl::loadObject begins by creating an ObjectImage instance
90*9880d681SAndroid Build Coastguard Workerfrom the ObjectBuffer it received.  ObjectImage, which wraps the
91*9880d681SAndroid Build Coastguard WorkerObjectFile class, is a helper class which parses the binary object image
92*9880d681SAndroid Build Coastguard Workerand provides access to the information contained in the format-specific
93*9880d681SAndroid Build Coastguard Workerheaders, including section, symbol and relocation information.
94*9880d681SAndroid Build Coastguard Worker
95*9880d681SAndroid Build Coastguard WorkerRuntimeDyldImpl::loadObject then iterates through the symbols in the
96*9880d681SAndroid Build Coastguard Workerimage.  Information about common symbols is collected for later use.  For
97*9880d681SAndroid Build Coastguard Workereach function or data symbol, the associated section is loaded into memory
98*9880d681SAndroid Build Coastguard Workerand the symbol is stored in a symbol table map data structure.  When the
99*9880d681SAndroid Build Coastguard Workeriteration is complete, a section is emitted for the common symbols.
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard WorkerNext, RuntimeDyldImpl::loadObject iterates through the sections in the
102*9880d681SAndroid Build Coastguard Workerobject image and for each section iterates through the relocations for
103*9880d681SAndroid Build Coastguard Workerthat sections.  For each relocation, it calls the format-specific
104*9880d681SAndroid Build Coastguard WorkerprocessRelocationRef method, which will examine the relocation and store
105*9880d681SAndroid Build Coastguard Workerit in one of two data structures, a section-based relocation list map and
106*9880d681SAndroid Build Coastguard Workeran external symbol relocation map.
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker.. image:: MCJIT-load-object.png
109*9880d681SAndroid Build Coastguard Worker
110*9880d681SAndroid Build Coastguard WorkerWhen RuntimeDyldImpl::loadObject returns, all of the code and data
111*9880d681SAndroid Build Coastguard Workersections for the object will have been loaded into memory allocated by the
112*9880d681SAndroid Build Coastguard Workermemory manager and relocation information will have been prepared, but the
113*9880d681SAndroid Build Coastguard Workerrelocations have not yet been applied and the generated code is still not
114*9880d681SAndroid Build Coastguard Workerready to be executed.
115*9880d681SAndroid Build Coastguard Worker
116*9880d681SAndroid Build Coastguard Worker[Currently (as of August 2013) the MCJIT engine will immediately apply
117*9880d681SAndroid Build Coastguard Workerrelocations when loadObject completes.  However, this shouldn't be
118*9880d681SAndroid Build Coastguard Workerhappening.  Because the code may have been generated for a remote target,
119*9880d681SAndroid Build Coastguard Workerthe client should be given a chance to re-map the section addresses before
120*9880d681SAndroid Build Coastguard Workerrelocations are applied.  It is possible to apply relocations multiple
121*9880d681SAndroid Build Coastguard Workertimes, but in the case where addresses are to be re-mapped, this first
122*9880d681SAndroid Build Coastguard Workerapplication is wasted effort.]
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard WorkerAddress Remapping
125*9880d681SAndroid Build Coastguard Worker=================
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard WorkerAt any time after initial code has been generated and before
128*9880d681SAndroid Build Coastguard WorkerfinalizeObject is called, the client can remap the address of sections in
129*9880d681SAndroid Build Coastguard Workerthe object.  Typically this is done because the code was generated for an
130*9880d681SAndroid Build Coastguard Workerexternal process and is being mapped into that process' address space.
131*9880d681SAndroid Build Coastguard WorkerThe client remaps the section address by calling MCJIT::mapSectionAddress.
132*9880d681SAndroid Build Coastguard WorkerThis should happen before the section memory is copied to its new
133*9880d681SAndroid Build Coastguard Workerlocation.
134*9880d681SAndroid Build Coastguard Worker
135*9880d681SAndroid Build Coastguard WorkerWhen MCJIT::mapSectionAddress is called, MCJIT passes the call on to
136*9880d681SAndroid Build Coastguard WorkerRuntimeDyldImpl (via its Dyld member).  RuntimeDyldImpl stores the new
137*9880d681SAndroid Build Coastguard Workeraddress in an internal data structure but does not update the code at this
138*9880d681SAndroid Build Coastguard Workertime, since other sections are likely to change.
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard WorkerWhen the client is finished remapping section addresses, it will call
141*9880d681SAndroid Build Coastguard WorkerMCJIT::finalizeObject to complete the remapping process.
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard WorkerFinal Preparations
144*9880d681SAndroid Build Coastguard Worker==================
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard WorkerWhen MCJIT::finalizeObject is called, MCJIT calls
147*9880d681SAndroid Build Coastguard WorkerRuntimeDyld::resolveRelocations.  This function will attempt to locate any
148*9880d681SAndroid Build Coastguard Workerexternal symbols and then apply all relocations for the object.
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard WorkerExternal symbols are resolved by calling the memory manager's
151*9880d681SAndroid Build Coastguard WorkergetPointerToNamedFunction method.  The memory manager will return the
152*9880d681SAndroid Build Coastguard Workeraddress of the requested symbol in the target address space.  (Note, this
153*9880d681SAndroid Build Coastguard Workermay not be a valid pointer in the host process.)  RuntimeDyld will then
154*9880d681SAndroid Build Coastguard Workeriterate through the list of relocations it has stored which are associated
155*9880d681SAndroid Build Coastguard Workerwith this symbol and invoke the resolveRelocation method which, through an
156*9880d681SAndroid Build Coastguard Workerformat-specific implementation, will apply the relocation to the loaded
157*9880d681SAndroid Build Coastguard Workersection memory.
158*9880d681SAndroid Build Coastguard Worker
159*9880d681SAndroid Build Coastguard WorkerNext, RuntimeDyld::resolveRelocations iterates through the list of
160*9880d681SAndroid Build Coastguard Workersections and for each section iterates through a list of relocations that
161*9880d681SAndroid Build Coastguard Workerhave been saved which reference that symbol and call resolveRelocation for
162*9880d681SAndroid Build Coastguard Workereach entry in this list.  The relocation list here is a list of
163*9880d681SAndroid Build Coastguard Workerrelocations for which the symbol associated with the relocation is located
164*9880d681SAndroid Build Coastguard Workerin the section associated with the list.  Each of these locations will
165*9880d681SAndroid Build Coastguard Workerhave a target location at which the relocation will be applied that is
166*9880d681SAndroid Build Coastguard Workerlikely located in a different section.
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Worker.. image:: MCJIT-resolve-relocations.png
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard WorkerOnce relocations have been applied as described above, MCJIT calls
171*9880d681SAndroid Build Coastguard WorkerRuntimeDyld::getEHFrameSection, and if a non-zero result is returned
172*9880d681SAndroid Build Coastguard Workerpasses the section data to the memory manager's registerEHFrames method.
173*9880d681SAndroid Build Coastguard WorkerThis allows the memory manager to call any desired target-specific
174*9880d681SAndroid Build Coastguard Workerfunctions, such as registering the EH frame information with a debugger.
175*9880d681SAndroid Build Coastguard Worker
176*9880d681SAndroid Build Coastguard WorkerFinally, MCJIT calls the memory manager's finalizeMemory method.  In this
177*9880d681SAndroid Build Coastguard Workermethod, the memory manager will invalidate the target code cache, if
178*9880d681SAndroid Build Coastguard Workernecessary, and apply final permissions to the memory pages it has
179*9880d681SAndroid Build Coastguard Workerallocated for code and data memory.
180*9880d681SAndroid Build Coastguard Worker
181