source: dev/BasicCompiler_Common/PESchedule.cpp@ 6

Last change on this file since 6 was 5, checked in by dai_9181, 18 years ago
File size: 6.9 KB
RevLine 
[4]1#include "../BasicCompiler_Common/common.h"
2
3#ifdef _AMD64_
4#include "../BasicCompiler64/opcode.h"
5#else
[5]6#include "../BasicCompiler32/opcode.h"
[4]7#endif
8
9extern HANDLE hHeap;
10
11/////////////////////////////////////////////
12// 再配置スケジュール
13//
14// メモリの再配置は.textセクション、または.dataセクションに行われることが予想される。
15// .textセクション … セクションデータへのポインタ
16// .dataセクション … vtblの再配置
17/////////////////////////////////////////////
18
19CReloc *pobj_Reloc;
20
21CReloc::CReloc(){
22 buffer=0;
23 length=0;
24 NowPageRVAToReloc=0;
25
26 pdwData_CodeSection=(DWORD *)HeapAlloc(hHeap,0,1);
27 iNum_CodeSection=0;
28 pdwData_DataSection=(DWORD *)HeapAlloc(hHeap,0,1);
29 iNum_DataSection=0;
30}
31CReloc::~CReloc(){
32 AllFree();
33}
34void CReloc::AllFree(void){
35 if(buffer){
36 HeapDefaultFree(buffer);
37 buffer=0;
38 }
39
40 HeapDefaultFree(pdwData_CodeSection);
41 HeapDefaultFree(pdwData_DataSection);
42}
43void CReloc::copy(CReloc *por){
44 AllFree();
45
46 if(por->buffer){
47 buffer=(char *)HeapReAlloc(hHeap,0,buffer,por->length);
48 memcpy(buffer,por->buffer,por->length);
49 }
50 else buffer=0;
51
52 length=por->length;
53
54 NowPageRVAToReloc=por->NowPageRVAToReloc;
55 NowCountAddrToReloc=por->NowCountAddrToReloc;
56
57 iNum_CodeSection=por->iNum_CodeSection;
58 pdwData_CodeSection=(DWORD *)HeapAlloc(hHeap,0,iNum_CodeSection*sizeof(DWORD));
59 memcpy(pdwData_CodeSection,por->pdwData_CodeSection,iNum_CodeSection*sizeof(DWORD));
60
61 iNum_DataSection=por->iNum_DataSection;
62 pdwData_DataSection=(DWORD *)HeapAlloc(hHeap,0,iNum_DataSection*sizeof(DWORD));
63 memcpy(pdwData_DataSection,por->pdwData_DataSection,iNum_DataSection*sizeof(DWORD));
64}
65
66void CReloc::AddSchedule_CodeSection(DWORD addr){
67 extern BOOL bDll;
68 if(!bDll) return;
69
70 pdwData_CodeSection=(DWORD *)HeapReAlloc(hHeap,0,pdwData_CodeSection,(iNum_CodeSection+1)*sizeof(DWORD));
71 pdwData_CodeSection[iNum_CodeSection]=addr;
72 iNum_CodeSection++;
73}
74void CReloc::AddSchedule_DataSection(DWORD addr){
75 extern BOOL bDll;
76 if(!bDll) return;
77
78 pdwData_DataSection=(DWORD *)HeapReAlloc(hHeap,0,pdwData_DataSection,(iNum_DataSection+1)*sizeof(DWORD));
79 pdwData_DataSection[iNum_DataSection]=addr;
80 iNum_DataSection++;
81}
82
83void CReloc::__add(DWORD addr){
84 extern BOOL bDll;
85 if(!bDll) return;
86
87 BOOL sw;
88 sw=0;
89 while((addr-(addr%MEM_ALIGNMENT))>NowPageRVAToReloc){
90 NowPageRVAToReloc+=MEM_ALIGNMENT;
91 sw=1;
92 }
93
94 if(sw){
95 //ページを増やす
96 while(length%4){
97 buffer=(char *)HeapReAlloc(hHeap,0,buffer,length+sizeof(WORD));
98 (*(WORD *)(buffer+length))=0;
99 length+=2;
100
101 (*(DWORD *)(buffer+NowCountAddrToReloc))+=sizeof(WORD);
102 }
103
104 buffer=(char *)HeapReAlloc(hHeap,0,buffer,
105 length+
106 sizeof(DWORD)+
107 sizeof(DWORD));
108
109 //Page RVA
110 *(DWORD *)(buffer+length)=NowPageRVAToReloc;
111 length+=sizeof(DWORD);
112
113 NowCountAddrToReloc=length;
114
115 //Block size
116 *(DWORD *)(buffer+length)=sizeof(DWORD)*2;
117 length+=sizeof(DWORD);
118 }
119
120 //リロケーション情報の追加
121 buffer=(char *)HeapReAlloc(hHeap,0,buffer,length+sizeof(WORD));
122 buffer[length]=0;
123 buffer[length+1]=0x30; //=IMAGE_REL_BASED_HIGHLOW
124 (*(WORD *)(buffer+length))|=addr&0x0FFF;
125 length+=2;
126
127 (*(DWORD *)(buffer+NowCountAddrToReloc))+=sizeof(WORD);
128}
129void CReloc::ResetRelocBuffer(void){
130 if(buffer) HeapDefaultFree(buffer);
131
132 buffer=(char *)HeapAlloc(hHeap,0,1);
133 length=0;
134 NowPageRVAToReloc=0;
135
136 int i;
137 for(i=0;i<iNum_CodeSection;i++){
138 extern int MemPos_CodeSection;
139 __add(MemPos_CodeSection + pdwData_CodeSection[i]);
140 }
141
142 for(i=0;i<iNum_DataSection;i++){
143 extern int MemPos_DataSection;
144 __add(MemPos_DataSection + pdwData_DataSection[i]);
145 }
146}
147
148
149
150
151//////////////////////////
152// 一般スケジュール
153//////////////////////////
154
155CSchedule::CSchedule(){
156 pObpValues=(int *)HeapAlloc(hHeap,0,1);
157 num=0;
158
159 this->flag=0;
160}
161CSchedule::~CSchedule(){
162 HeapDefaultFree(pObpValues);
163}
164void CSchedule::SetFlag(int flag){
165 this->flag=flag;
166}
167void CSchedule::add(){
168 pObpValues=(int *)HeapReAlloc(hHeap,0,pObpValues,(num+1)*sizeof(int));
169 pObpValues[num]=obp;
170 num++;
171
172 if(flag&SCHEDULE_FLAG_RELOC){
173 //リロケーション情報を追加する
174 pobj_Reloc->AddSchedule_CodeSection(obp);
175 }
176}
177void CSchedule::move(int iStartPos,int iSize,int offset){
178 int i;
179 for(i=0;i<num;i++){
180 if(iStartPos<=pObpValues[i] && pObpValues[i]<iStartPos+iSize){
181 pObpValues[i]+=offset;
182 }
183 }
184}
185
186//データテーブルスケジュール
187CSchedule *pobj_DataTableSchedule;
188
189//グローバル変数アドレススケジュール
190CSchedule *pobj_GlobalVarSchedule;
191
192
193
194/////////////////////////////////////////
195// インポート アドレス スケジューリング
196/////////////////////////////////////////
197
198CImportAddrSchedule::CImportAddrSchedule(){
199 ppdi=(DECLAREINFO **)HeapAlloc(hHeap,0,1);
200}
201CImportAddrSchedule::~CImportAddrSchedule(){
202 HeapDefaultFree(ppdi);
203}
204
205void CImportAddrSchedule::add(DECLAREINFO *pdi){
206 ppdi=(DECLAREINFO **)HeapReAlloc(hHeap,0,ppdi,(num+1)*sizeof(DECLAREINFO *));
207 ppdi[num]=pdi;
208
209 CSchedule::add();
210}
211
212CImportAddrSchedule *pobj_ImportAddrSchedule;
213
214
215
216
217/////////////////////////////////////////
218// プロシージャ アドレス スケジューリング
219/////////////////////////////////////////
220
221CSubAddrSchedule::CSubAddrSchedule(){
222 ppsi=(SUBINFO **)HeapAlloc(hHeap,0,1);
223 pbCall=(BOOL *)HeapAlloc(hHeap,0,1);
224}
225CSubAddrSchedule::~CSubAddrSchedule(){
226 HeapDefaultFree(ppsi);
227 HeapDefaultFree(pbCall);
228}
229
230void CSubAddrSchedule::add(SUBINFO *psi,BOOL bCall){
231 if(!psi) return;
232
233 ppsi=(SUBINFO **)HeapReAlloc(hHeap,0,ppsi,(num+1)*sizeof(SUBINFO *));
234 ppsi[num]=psi;
235 pbCall=(BOOL *)HeapReAlloc(hHeap,0,pbCall,(num+1)*sizeof(BOOL));
236 pbCall[num]=bCall;
237
238 CSchedule::add();
239
240 if(!bCall){
241 //リロケーション情報を追加する
242 pobj_Reloc->AddSchedule_CodeSection(obp);
243 }
244}
245
246CSubAddrSchedule *pobj_SubAddrSchedule;
247
248
249
250
251/////////////////////////////////////////
252// 一時スケジューリングポインタ
253/////////////////////////////////////////
254CTempSchedule::CTempSchedule(){
255 ppObpValues=(int **)HeapAlloc(hHeap,0,1);
256 num=0;
257}
258CTempSchedule::~CTempSchedule(){
259 HeapDefaultFree(ppObpValues);
260}
261void CTempSchedule::lock(int *pobp){
262 ppObpValues=(int **)HeapReAlloc(hHeap,0,ppObpValues,(num+1)*sizeof(int *));
263 ppObpValues[num]=pobp;
264 num++;
265}
266void CTempSchedule::unlock(){
267 num--;
268}
269void CTempSchedule::move(int iStartPos,int iSize,int offset){
270 int i;
271 for(i=0;i<num;i++){
272 if(iStartPos<=*ppObpValues[i] && *ppObpValues[i]<iStartPos+iSize){
273 (*ppObpValues[i])+=offset;
274 }
275 }
276}
277
278CTempSchedule *pobj_TempSchedule;
279
280
281
282/////////////////////////
283// 静的再配置
284/////////////////////////
285
286void NativeBuffer_StaticRelocation(int iStartPos,int iSize,int offset){
287 pobj_DataTableSchedule->move(iStartPos,iSize,offset);
288 pobj_GlobalVarSchedule->move(iStartPos,iSize,offset);
289 pobj_ImportAddrSchedule->move(iStartPos,iSize,offset);
290 pobj_SubAddrSchedule->move(iStartPos,iSize,offset);
291
292 pobj_TempSchedule->move(iStartPos,iSize,offset);
293
294#ifdef _AMD64_
295 pobj_sf->move(iStartPos,iSize,offset);
296#endif
297}
298
Note: See TracBrowser for help on using the repository browser.