blob: 50e6562c79eeef44c674c014d275fb1cbffd0fc8 [file] [log] [blame]
Evan Cheng148b6a42007-07-05 21:15:40 +00001//===-- ARMJITInfo.cpp - Implement the JIT interfaces for the ARM target --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng148b6a42007-07-05 21:15:40 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the JIT interfaces for the ARM target.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "jit"
15#include "ARMJITInfo.h"
Evan Cheng580c0df2008-11-12 01:02:24 +000016#include "ARMInstrInfo.h"
Evan Cheng25e04782008-11-04 00:50:32 +000017#include "ARMConstantPoolValue.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000018#include "ARMRelocations.h"
19#include "ARMSubtarget.h"
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +000020#include "llvm/Function.h"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000021#include "llvm/CodeGen/JITCodeEmitter.h"
Evan Cheng588920b2008-11-10 23:14:47 +000022#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000024#include "llvm/Support/raw_ostream.h"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000025#include "llvm/Support/Memory.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000026#include <cstdlib>
27using namespace llvm;
28
29void ARMJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
Chris Lattner75361b62010-04-07 22:58:41 +000030 report_fatal_error("ARMJITInfo::replaceMachineCodeForFunction");
Evan Cheng148b6a42007-07-05 21:15:40 +000031}
32
33/// JITCompilerFunction - This contains the address of the JIT function used to
34/// compile a function lazily.
35static TargetJITInfo::JITCompilerFn JITCompilerFunction;
36
Evan Cheng95ce1172008-09-02 07:49:03 +000037// Get the ASMPREFIX for the current host. This is often '_'.
38#ifndef __USER_LABEL_PREFIX__
39#define __USER_LABEL_PREFIX__
40#endif
41#define GETASMPREFIX2(X) #X
42#define GETASMPREFIX(X) GETASMPREFIX2(X)
43#define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
44
Evan Cheng148b6a42007-07-05 21:15:40 +000045// CompilationCallback stub - We can't use a C function with inline assembly in
Jim Grosbach932a32d2008-10-20 21:39:23 +000046// it, because we the prolog/epilog inserted by GCC won't work for us (we need
47// to preserve more context and manipulate the stack directly). Instead,
Jim Grosbach764ab522009-08-11 15:33:49 +000048// write our own wrapper, which does things our way, so we have complete
Jim Grosbach932a32d2008-10-20 21:39:23 +000049// control over register saving and restoring.
Evan Cheng148b6a42007-07-05 21:15:40 +000050extern "C" {
51#if defined(__arm__)
Dan Gohmana9ad0412009-08-12 22:10:57 +000052 void ARMCompilationCallback();
Evan Cheng148b6a42007-07-05 21:15:40 +000053 asm(
54 ".text\n"
55 ".align 2\n"
Evan Cheng95ce1172008-09-02 07:49:03 +000056 ".globl " ASMPREFIX "ARMCompilationCallback\n"
57 ASMPREFIX "ARMCompilationCallback:\n"
Jim Grosbach932a32d2008-10-20 21:39:23 +000058 // Save caller saved registers since they may contain stuff
59 // for the real target function right now. We have to act as if this
60 // whole compilation callback doesn't exist as far as the caller is
61 // concerned, so we can't just preserve the callee saved regs.
Jim Grosbacha9ab95b2008-10-21 16:54:12 +000062 "stmdb sp!, {r0, r1, r2, r3, lr}\n"
Xerxes Ranby901a8b72010-03-02 13:42:03 +000063#if (defined(__VFP_FP__) && !defined(__SOFTFP__))
Evan Cheng28f31292008-11-13 23:28:54 +000064 "fstmfdd sp!, {d0, d1, d2, d3, d4, d5, d6, d7}\n"
65#endif
Jim Grosbach932a32d2008-10-20 21:39:23 +000066 // The LR contains the address of the stub function on entry.
67 // pass it as the argument to the C part of the callback
68 "mov r0, lr\n"
69 "sub sp, sp, #4\n"
70 // Call the C portion of the callback
71 "bl " ASMPREFIX "ARMCompilationCallbackC\n"
72 "add sp, sp, #4\n"
73 // Restoring the LR to the return address of the function that invoked
74 // the stub and de-allocating the stack space for it requires us to
75 // swap the two saved LR values on the stack, as they're backwards
76 // for what we need since the pop instruction has a pre-determined
77 // order for the registers.
78 // +--------+
79 // 0 | LR | Original return address
Jim Grosbach764ab522009-08-11 15:33:49 +000080 // +--------+
Jim Grosbach932a32d2008-10-20 21:39:23 +000081 // 1 | LR | Stub address (start of stub)
82 // 2-5 | R3..R0 | Saved registers (we need to preserve all regs)
Evan Cheng28f31292008-11-13 23:28:54 +000083 // 6-20 | D0..D7 | Saved VFP registers
Jim Grosbach764ab522009-08-11 15:33:49 +000084 // +--------+
Jim Grosbach932a32d2008-10-20 21:39:23 +000085 //
Xerxes Ranby901a8b72010-03-02 13:42:03 +000086#if (defined(__VFP_FP__) && !defined(__SOFTFP__))
Evan Cheng28f31292008-11-13 23:28:54 +000087 // Restore VFP caller-saved registers.
88 "fldmfdd sp!, {d0, d1, d2, d3, d4, d5, d6, d7}\n"
89#endif
90 //
Jim Grosbach932a32d2008-10-20 21:39:23 +000091 // We need to exchange the values in slots 0 and 1 so we can
92 // return to the address in slot 1 with the address in slot 0
93 // restored to the LR.
94 "ldr r0, [sp,#20]\n"
95 "ldr r1, [sp,#16]\n"
96 "str r1, [sp,#20]\n"
97 "str r0, [sp,#16]\n"
98 // Return to the (newly modified) stub to invoke the real function.
99 // The above twiddling of the saved return addresses allows us to
100 // deallocate everything, including the LR the stub saved, all in one
101 // pop instruction.
Jim Grosbacha9ab95b2008-10-21 16:54:12 +0000102 "ldmia sp!, {r0, r1, r2, r3, lr, pc}\n"
Evan Cheng95ce1172008-09-02 07:49:03 +0000103 );
104#else // Not an ARM host
Evan Cheng148b6a42007-07-05 21:15:40 +0000105 void ARMCompilationCallback() {
Torok Edwinc23197a2009-07-14 16:55:14 +0000106 llvm_unreachable("Cannot call ARMCompilationCallback() on a non-ARM arch!");
Evan Cheng148b6a42007-07-05 21:15:40 +0000107 }
108#endif
109}
110
Jim Grosbach764ab522009-08-11 15:33:49 +0000111/// ARMCompilationCallbackC - This is the target-specific function invoked
112/// by the function stub when we did not know the real target of a call.
113/// This function must locate the start of the stub or call site and pass
Jim Grosbach932a32d2008-10-20 21:39:23 +0000114/// it into the JIT compiler function.
115extern "C" void ARMCompilationCallbackC(intptr_t StubAddr) {
116 // Get the address of the compiled code for this function.
117 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)StubAddr);
Evan Cheng148b6a42007-07-05 21:15:40 +0000118
119 // Rewrite the call target... so that we don't end up here every time we
Jim Grosbach932a32d2008-10-20 21:39:23 +0000120 // execute the call. We're replacing the first two instructions of the
121 // stub with:
122 // ldr pc, [pc,#-4]
123 // <addr>
Evan Chengae166412008-11-08 08:16:49 +0000124 if (!sys::Memory::setRangeWritable((void*)StubAddr, 8)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000125 llvm_unreachable("ERROR: Unable to mark stub writable");
Evan Chengae166412008-11-08 08:16:49 +0000126 }
127 *(intptr_t *)StubAddr = 0xe51ff004; // ldr pc, [pc, #-4]
Jim Grosbach932a32d2008-10-20 21:39:23 +0000128 *(intptr_t *)(StubAddr+4) = NewVal;
Evan Chengae166412008-11-08 08:16:49 +0000129 if (!sys::Memory::setRangeExecutable((void*)StubAddr, 8)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000130 llvm_unreachable("ERROR: Unable to mark stub executable");
Evan Chengae166412008-11-08 08:16:49 +0000131 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000132}
133
134TargetJITInfo::LazyResolverFn
135ARMJITInfo::getLazyResolverFunction(JITCompilerFn F) {
136 JITCompilerFunction = F;
137 return ARMCompilationCallback;
138}
139
Evan Cheng9ed2f802008-11-10 01:08:07 +0000140void *ARMJITInfo::emitGlobalValueIndirectSym(const GlobalValue *GV, void *Ptr,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000141 JITCodeEmitter &JCE) {
Jeffrey Yasskin32d7e6e2009-12-15 22:42:46 +0000142 uint8_t Buffer[4];
143 uint8_t *Cur = Buffer;
144 MachineCodeEmitter::emitWordLEInto(Cur, (intptr_t)Ptr);
145 void *PtrAddr = JCE.allocIndirectGV(
146 GV, Buffer, sizeof(Buffer), /*Alignment=*/4);
Evan Cheng588920b2008-11-10 23:14:47 +0000147 addIndirectSymAddr(Ptr, (intptr_t)PtrAddr);
148 return PtrAddr;
Evan Chenge96a4902008-11-08 01:31:27 +0000149}
150
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000151TargetJITInfo::StubLayout ARMJITInfo::getStubLayout() {
152 // The stub contains up to 3 4-byte instructions, aligned at 4 bytes, and a
153 // 4-byte address. See emitFunctionStub for details.
154 StubLayout Result = {16, 4};
155 return Result;
156}
157
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +0000158void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000159 JITCodeEmitter &JCE) {
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000160 void *Addr;
Evan Cheng148b6a42007-07-05 21:15:40 +0000161 // If this is just a call to an external function, emit a branch instead of a
162 // call. The code is the same except for one bit of the last instruction.
163 if (Fn != (void*)(intptr_t)ARMCompilationCallback) {
Evan Chenge96a4902008-11-08 01:31:27 +0000164 // Branch to the corresponding function addr.
Evan Cheng588920b2008-11-10 23:14:47 +0000165 if (IsPIC) {
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000166 // The stub is 16-byte size and 4-aligned.
Evan Cheng588920b2008-11-10 23:14:47 +0000167 intptr_t LazyPtr = getIndirectSymAddr(Fn);
168 if (!LazyPtr) {
169 // In PIC mode, the function stub is loading a lazy-ptr.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000170 LazyPtr= (intptr_t)emitGlobalValueIndirectSym((GlobalValue*)F, Fn, JCE);
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000171 DEBUG(if (F)
Jim Grosbach764ab522009-08-11 15:33:49 +0000172 errs() << "JIT: Indirect symbol emitted at [" << LazyPtr
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000173 << "] for GV '" << F->getName() << "'\n";
174 else
175 errs() << "JIT: Stub emitted at [" << LazyPtr
176 << "] for external function at '" << Fn << "'\n");
Evan Cheng588920b2008-11-10 23:14:47 +0000177 }
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000178 JCE.emitAlignment(4);
179 Addr = (void*)JCE.getCurrentPCValue();
180 if (!sys::Memory::setRangeWritable(Addr, 16)) {
Evan Cheng04cedd32009-09-09 01:56:29 +0000181 llvm_unreachable("ERROR: Unable to mark stub writable");
182 }
Eric Christopher0dde9712009-11-20 00:21:55 +0000183 JCE.emitWordLE(0xe59fc004); // ldr ip, [pc, #+4]
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000184 JCE.emitWordLE(0xe08fc00c); // L_func$scv: add ip, pc, ip
185 JCE.emitWordLE(0xe59cf000); // ldr pc, [ip]
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000186 JCE.emitWordLE(LazyPtr - (intptr_t(Addr)+4+8)); // func - (L_func$scv+8)
187 sys::Memory::InvalidateInstructionCache(Addr, 16);
188 if (!sys::Memory::setRangeExecutable(Addr, 16)) {
Evan Cheng04cedd32009-09-09 01:56:29 +0000189 llvm_unreachable("ERROR: Unable to mark stub executable");
190 }
Evan Cheng588920b2008-11-10 23:14:47 +0000191 } else {
192 // The stub is 8-byte size and 4-aligned.
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000193 JCE.emitAlignment(4);
194 Addr = (void*)JCE.getCurrentPCValue();
195 if (!sys::Memory::setRangeWritable(Addr, 8)) {
Evan Cheng04cedd32009-09-09 01:56:29 +0000196 llvm_unreachable("ERROR: Unable to mark stub writable");
197 }
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000198 JCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
199 JCE.emitWordLE((intptr_t)Fn); // addr of function
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000200 sys::Memory::InvalidateInstructionCache(Addr, 8);
201 if (!sys::Memory::setRangeExecutable(Addr, 8)) {
Evan Cheng04cedd32009-09-09 01:56:29 +0000202 llvm_unreachable("ERROR: Unable to mark stub executable");
203 }
Evan Cheng588920b2008-11-10 23:14:47 +0000204 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000205 } else {
Jim Grosbach932a32d2008-10-20 21:39:23 +0000206 // The compilation callback will overwrite the first two words of this
Jim Grosbach764ab522009-08-11 15:33:49 +0000207 // stub with indirect branch instructions targeting the compiled code.
Jim Grosbach932a32d2008-10-20 21:39:23 +0000208 // This stub sets the return address to restart the stub, so that
209 // the new branch will be invoked when we come back.
210 //
Evan Chenge96a4902008-11-08 01:31:27 +0000211 // Branch and link to the compilation callback.
212 // The stub is 16-byte size and 4-byte aligned.
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000213 JCE.emitAlignment(4);
214 Addr = (void*)JCE.getCurrentPCValue();
215 if (!sys::Memory::setRangeWritable(Addr, 16)) {
Evan Cheng04cedd32009-09-09 01:56:29 +0000216 llvm_unreachable("ERROR: Unable to mark stub writable");
217 }
Jim Grosbach932a32d2008-10-20 21:39:23 +0000218 // Save LR so the callback can determine which stub called it.
219 // The compilation callback is responsible for popping this prior
220 // to returning.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000221 JCE.emitWordLE(0xe92d4000); // push {lr}
Evan Chengae166412008-11-08 08:16:49 +0000222 // Set the return address to go back to the start of this stub.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000223 JCE.emitWordLE(0xe24fe00c); // sub lr, pc, #12
Evan Chengae166412008-11-08 08:16:49 +0000224 // Invoke the compilation callback.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000225 JCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
Evan Chengae166412008-11-08 08:16:49 +0000226 // The address of the compilation callback.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000227 JCE.emitWordLE((intptr_t)ARMCompilationCallback);
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000228 sys::Memory::InvalidateInstructionCache(Addr, 16);
229 if (!sys::Memory::setRangeExecutable(Addr, 16)) {
Evan Cheng04cedd32009-09-09 01:56:29 +0000230 llvm_unreachable("ERROR: Unable to mark stub executable");
231 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000232 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000233
Jeffrey Yasskin108c8382009-11-23 23:35:19 +0000234 return Addr;
Evan Cheng148b6a42007-07-05 21:15:40 +0000235}
236
Evan Cheng4df60f52008-11-07 09:06:08 +0000237intptr_t ARMJITInfo::resolveRelocDestAddr(MachineRelocation *MR) const {
Evan Cheng25e04782008-11-04 00:50:32 +0000238 ARM::RelocationType RT = (ARM::RelocationType)MR->getRelocationType();
Evan Cheng580c0df2008-11-12 01:02:24 +0000239 switch (RT) {
240 default:
241 return (intptr_t)(MR->getResultPointer());
242 case ARM::reloc_arm_pic_jt:
Evan Cheng437c1732008-11-07 22:30:53 +0000243 // Destination address - jump table base.
244 return (intptr_t)(MR->getResultPointer()) - MR->getConstantVal();
Evan Cheng580c0df2008-11-12 01:02:24 +0000245 case ARM::reloc_arm_jt_base:
Evan Cheng437c1732008-11-07 22:30:53 +0000246 // Jump table base address.
Evan Cheng4df60f52008-11-07 09:06:08 +0000247 return getJumpTableBaseAddr(MR->getJumpTableIndex());
Evan Cheng580c0df2008-11-12 01:02:24 +0000248 case ARM::reloc_arm_cp_entry:
249 case ARM::reloc_arm_vfp_cp_entry:
Evan Cheng437c1732008-11-07 22:30:53 +0000250 // Constant pool entry address.
Evan Cheng25e04782008-11-04 00:50:32 +0000251 return getConstantPoolEntryAddr(MR->getConstantPoolIndex());
Evan Cheng580c0df2008-11-12 01:02:24 +0000252 case ARM::reloc_arm_machine_cp_entry: {
Evan Cheng413a89f2008-11-07 22:57:53 +0000253 ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)MR->getConstantVal();
Evan Cheng25e04782008-11-04 00:50:32 +0000254 assert((!ACPV->hasModifier() && !ACPV->mustAddCurrentAddress()) &&
255 "Can't handle this machine constant pool entry yet!");
256 intptr_t Addr = (intptr_t)(MR->getResultPointer());
257 Addr -= getPCLabelAddr(ACPV->getLabelId()) + ACPV->getPCAdjustment();
258 return Addr;
259 }
Evan Cheng580c0df2008-11-12 01:02:24 +0000260 }
Evan Cheng25e04782008-11-04 00:50:32 +0000261}
262
Evan Cheng148b6a42007-07-05 21:15:40 +0000263/// relocate - Before the JIT can run a block of code that has been emitted,
264/// it must rewrite the code to contain the actual addresses of any
265/// referenced global symbols.
266void ARMJITInfo::relocate(void *Function, MachineRelocation *MR,
267 unsigned NumRelocs, unsigned char* GOTBase) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000268 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
269 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
Evan Cheng4df60f52008-11-07 09:06:08 +0000270 intptr_t ResultPtr = resolveRelocDestAddr(MR);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000271 switch ((ARM::RelocationType)MR->getRelocationType()) {
Evan Cheng0f282432008-10-29 23:55:43 +0000272 case ARM::reloc_arm_cp_entry:
Evan Cheng580c0df2008-11-12 01:02:24 +0000273 case ARM::reloc_arm_vfp_cp_entry:
Evan Cheng0ff94f72007-08-07 01:37:15 +0000274 case ARM::reloc_arm_relative: {
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000275 // It is necessary to calculate the correct PC relative value. We
276 // subtract the base addr from the target addr to form a byte offset.
Evan Cheng580c0df2008-11-12 01:02:24 +0000277 ResultPtr = ResultPtr - (intptr_t)RelocPos - 8;
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000278 // If the result is positive, set bit U(23) to 1.
279 if (ResultPtr >= 0)
Evan Cheng580c0df2008-11-12 01:02:24 +0000280 *((intptr_t*)RelocPos) |= 1 << ARMII::U_BitShift;
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000281 else {
Evan Cheng580c0df2008-11-12 01:02:24 +0000282 // Otherwise, obtain the absolute value and set bit U(23) to 0.
Evan Chengcb579822008-11-12 21:37:59 +0000283 *((intptr_t*)RelocPos) &= ~(1 << ARMII::U_BitShift);
Evan Cheng580c0df2008-11-12 01:02:24 +0000284 ResultPtr = - ResultPtr;
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000285 }
Evan Cheng25e04782008-11-04 00:50:32 +0000286 // Set the immed value calculated.
Evan Cheng580c0df2008-11-12 01:02:24 +0000287 // VFP immediate offset is multiplied by 4.
288 if (MR->getRelocationType() == ARM::reloc_arm_vfp_cp_entry)
289 ResultPtr = ResultPtr >> 2;
290 *((intptr_t*)RelocPos) |= ResultPtr;
Evan Cheng25e04782008-11-04 00:50:32 +0000291 // Set register Rn to PC.
Evan Cheng580c0df2008-11-12 01:02:24 +0000292 *((intptr_t*)RelocPos) |=
Jim Grosbacha4c3c8f2010-09-15 20:26:25 +0000293 getARMRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000294 break;
295 }
Evan Cheng437c1732008-11-07 22:30:53 +0000296 case ARM::reloc_arm_pic_jt:
Evan Cheng25e04782008-11-04 00:50:32 +0000297 case ARM::reloc_arm_machine_cp_entry:
Evan Cheng0f282432008-10-29 23:55:43 +0000298 case ARM::reloc_arm_absolute: {
Evan Cheng25e04782008-11-04 00:50:32 +0000299 // These addresses have already been resolved.
Evan Cheng580c0df2008-11-12 01:02:24 +0000300 *((intptr_t*)RelocPos) |= (intptr_t)ResultPtr;
Evan Cheng0f282432008-10-29 23:55:43 +0000301 break;
302 }
Evan Cheng0ff94f72007-08-07 01:37:15 +0000303 case ARM::reloc_arm_branch: {
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000304 // It is necessary to calculate the correct value of signed_immed_24
305 // field. We subtract the base addr from the target addr to form a
306 // byte offset, which must be inside the range -33554432 and +33554428.
307 // Then, we set the signed_immed_24 field of the instruction to bits
308 // [25:2] of the byte offset. More details ARM-ARM p. A4-11.
Evan Cheng4df60f52008-11-07 09:06:08 +0000309 ResultPtr = ResultPtr - (intptr_t)RelocPos - 8;
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000310 ResultPtr = (ResultPtr & 0x03FFFFFC) >> 2;
311 assert(ResultPtr >= -33554432 && ResultPtr <= 33554428);
Evan Cheng580c0df2008-11-12 01:02:24 +0000312 *((intptr_t*)RelocPos) |= ResultPtr;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000313 break;
314 }
Evan Cheng4df60f52008-11-07 09:06:08 +0000315 case ARM::reloc_arm_jt_base: {
316 // JT base - (instruction addr + 8)
317 ResultPtr = ResultPtr - (intptr_t)RelocPos - 8;
Evan Cheng580c0df2008-11-12 01:02:24 +0000318 *((intptr_t*)RelocPos) |= ResultPtr;
Evan Cheng4df60f52008-11-07 09:06:08 +0000319 break;
320 }
Zonr Changf86399b2010-05-25 08:42:45 +0000321 case ARM::reloc_arm_movw: {
322 ResultPtr = ResultPtr & 0xFFFF;
323 *((intptr_t*)RelocPos) |= ResultPtr & 0xFFF;
324 *((intptr_t*)RelocPos) |= ((ResultPtr >> 12) & 0xF) << 16;
325 break;
326 }
327 case ARM::reloc_arm_movt: {
328 ResultPtr = (ResultPtr >> 16) & 0xFFFF;
329 *((intptr_t*)RelocPos) |= ResultPtr & 0xFFF;
330 *((intptr_t*)RelocPos) |= ((ResultPtr >> 12) & 0xF) << 16;
331 break;
332 }
Evan Cheng0ff94f72007-08-07 01:37:15 +0000333 }
334 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000335}