1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sw=4 et tw=79:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Copyright (C) 2009 Apple Inc. All rights reserved.
6 : *
7 : * Redistribution and use in source and binary forms, with or without
8 : * modification, are permitted provided that the following conditions
9 : * are met:
10 : * 1. Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * 2. Redistributions in binary form must reproduce the above copyright
13 : * notice, this list of conditions and the following disclaimer in the
14 : * documentation and/or other materials provided with the distribution.
15 : *
16 : * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
17 : * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 : * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
20 : * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 : * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 : *
28 : * ***** END LICENSE BLOCK ***** */
29 :
30 : #ifndef RepatchBuffer_h
31 : #define RepatchBuffer_h
32 :
33 : #include "assembler/wtf/Platform.h"
34 :
35 : #if ENABLE_ASSEMBLER
36 :
37 : #include <assembler/MacroAssembler.h>
38 : #include <moco/MocoStubs.h> //MOCO
39 :
40 : namespace JSC {
41 :
42 : // RepatchBuffer:
43 : //
44 : // This class is used to modify code after code generation has been completed,
45 : // and after the code has potentially already been executed. This mechanism is
46 : // used to apply optimizations to the code.
47 : //
48 : class RepatchBuffer {
49 : typedef MacroAssemblerCodePtr CodePtr;
50 :
51 : public:
52 468755 : RepatchBuffer(const MacroAssemblerCodeRef &ref)
53 : {
54 468755 : m_start = ref.m_code.executableAddress();
55 468755 : m_size = ref.m_size;
56 468755 : mprot = true;
57 :
58 468755 : if (mprot)
59 468755 : ExecutableAllocator::makeWritable(m_start, m_size);
60 468755 : }
61 :
62 259855 : RepatchBuffer(const JITCode &code)
63 : {
64 259855 : m_start = code.start();
65 259855 : m_size = code.size();
66 259855 : mprot = true;
67 :
68 259855 : if (mprot)
69 259855 : ExecutableAllocator::makeWritable(m_start, m_size);
70 259855 : }
71 :
72 728610 : ~RepatchBuffer()
73 : {
74 728610 : if (mprot)
75 728610 : ExecutableAllocator::makeExecutable(m_start, m_size);
76 728610 : }
77 :
78 347394 : void relink(CodeLocationJump jump, CodeLocationLabel destination)
79 : {
80 347394 : MacroAssembler::repatchJump(jump, destination);
81 347394 : }
82 :
83 13004 : bool canRelink(CodeLocationJump jump, CodeLocationLabel destination)
84 : {
85 13004 : return MacroAssembler::canRepatchJump(jump, destination);
86 : }
87 :
88 : void relink(CodeLocationCall call, CodeLocationLabel destination)
89 : {
90 : MacroAssembler::repatchCall(call, destination);
91 : }
92 :
93 66466 : void relink(CodeLocationCall call, FunctionPtr destination)
94 : {
95 66466 : MacroAssembler::repatchCall(call, destination);
96 66466 : }
97 :
98 : void relink(CodeLocationNearCall nearCall, CodePtr destination)
99 : {
100 : MacroAssembler::repatchNearCall(nearCall, CodeLocationLabel(destination));
101 : }
102 :
103 : void relink(CodeLocationNearCall nearCall, CodeLocationLabel destination)
104 : {
105 : MacroAssembler::repatchNearCall(nearCall, destination);
106 : }
107 :
108 672202 : void repatch(CodeLocationDataLabel32 dataLabel32, int32_t value)
109 : {
110 672202 : MacroAssembler::repatchInt32(dataLabel32, value);
111 672202 : }
112 :
113 355607 : void repatch(CodeLocationDataLabelPtr dataLabelPtr, const void* value)
114 : {
115 355607 : MacroAssembler::repatchPointer(dataLabelPtr, (void*) value);
116 355607 : }
117 :
118 101792 : void repatchLoadPtrToLEA(CodeLocationInstruction instruction)
119 : {
120 101792 : MacroAssembler::repatchLoadPtrToLEA(instruction);
121 101792 : }
122 :
123 : void repatchLEAToLoadPtr(CodeLocationInstruction instruction)
124 : {
125 : MacroAssembler::repatchLEAToLoadPtr(instruction);
126 : }
127 :
128 : void relinkCallerToTrampoline(ReturnAddressPtr returnAddress, CodeLocationLabel label)
129 : {
130 : relink(CodeLocationCall(CodePtr(returnAddress)), label);
131 : }
132 :
133 : void relinkCallerToTrampoline(ReturnAddressPtr returnAddress, CodePtr newCalleeFunction)
134 : {
135 : relinkCallerToTrampoline(returnAddress, CodeLocationLabel(newCalleeFunction));
136 : }
137 :
138 : void relinkCallerToFunction(ReturnAddressPtr returnAddress, FunctionPtr function)
139 : {
140 : relink(CodeLocationCall(CodePtr(returnAddress)), function);
141 : }
142 :
143 : void relinkNearCallerToTrampoline(ReturnAddressPtr returnAddress, CodeLocationLabel label)
144 : {
145 : relink(CodeLocationNearCall(CodePtr(returnAddress)), label);
146 : }
147 :
148 : void relinkNearCallerToTrampoline(ReturnAddressPtr returnAddress, CodePtr newCalleeFunction)
149 : {
150 : relinkNearCallerToTrampoline(returnAddress, CodeLocationLabel(newCalleeFunction));
151 : }
152 :
153 : protected:
154 : void* m_start;
155 : size_t m_size;
156 : bool mprot;
157 : };
158 :
159 : } // namespace JSC
160 :
161 : #endif // ENABLE(ASSEMBLER)
162 :
163 : #endif // RepatchBuffer_h
|