source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/PESchedule.cpp@ 767

Last change on this file since 767 was 750, checked in by イグトランス (egtra), 16 years ago

BOOST_FOREACHを可能なものはVC++ 2005 for eachへ置換(やや速くなる)。

File size: 2.8 KB
Line 
1#include "stdafx.h"
2
3#include <Compiler.h>
4
5#include "../BasicCompiler_Common/common.h"
6
7#ifdef _AMD64_
8#include "../compiler_x64/opcode.h"
9#else
10#include "../compiler_x86/opcode.h"
11#endif
12
13extern HANDLE hHeap;
14
15/////////////////////////////////////////////
16// 再配置スケジュール
17//
18// メモリの再配置は.textセクション、または.dataセクションに行われることが予想される。
19// .textセクション … セクションデータへのポインタ
20// .dataセクション … vtblの再配置
21/////////////////////////////////////////////
22
23CReloc *pobj_Reloc;
24
25CReloc::CReloc(){
26 buffer=0;
27 length=0;
28 NowPageRVAToReloc=0;
29}
30CReloc::~CReloc(){
31 AllFree();
32}
33void CReloc::AllFree(void){
34 if(buffer){
35 HeapDefaultFree(buffer);
36 buffer=0;
37 }
38}
39void CReloc::copy(CReloc *por){
40 AllFree();
41
42 if(por->buffer){
43 buffer=(char *)HeapReAlloc(hHeap,0,buffer,por->length);
44 memcpy(buffer,por->buffer,por->length);
45 }
46 else buffer=0;
47
48 length=por->length;
49
50 NowPageRVAToReloc=por->NowPageRVAToReloc;
51 NowCountAddrToReloc=por->NowCountAddrToReloc;
52
53 codeSectionAddresses = por->codeSectionAddresses;
54
55 dataSectionAddresses = por->dataSectionAddresses;
56}
57
58void CReloc::AddSchedule_CodeSection(DWORD addr){
59 if( !compiler.IsDll() ) return;
60
61 codeSectionAddresses.push_back( addr );
62}
63void CReloc::AddSchedule_DataSection(DWORD addr){
64 if( !compiler.IsDll() ) return;
65
66 dataSectionAddresses.push_back( addr );
67}
68
69void CReloc::__add(DWORD addr){
70 if( !compiler.IsDll() ) return;
71
72 BOOL sw;
73 sw=0;
74 while((addr-(addr%MEM_ALIGNMENT))>NowPageRVAToReloc){
75 NowPageRVAToReloc+=MEM_ALIGNMENT;
76 sw=1;
77 }
78
79 if(sw){
80 //ページを増やす
81 while(length%4){
82 buffer=(char *)HeapReAlloc(hHeap,0,buffer,length+sizeof(WORD));
83 (*(WORD *)(buffer+length))=0;
84 length+=2;
85
86 (*(DWORD *)(buffer+NowCountAddrToReloc))+=sizeof(WORD);
87 }
88
89 buffer=(char *)HeapReAlloc(hHeap,0,buffer,
90 length+
91 sizeof(DWORD)+
92 sizeof(DWORD));
93
94 //Page RVA
95 *(DWORD *)(buffer+length)=NowPageRVAToReloc;
96 length+=sizeof(DWORD);
97
98 NowCountAddrToReloc=length;
99
100 //Block size
101 *(DWORD *)(buffer+length)=sizeof(DWORD)*2;
102 length+=sizeof(DWORD);
103 }
104
105 //リロケーション情報の追加
106 buffer=(char *)HeapReAlloc(hHeap,0,buffer,length+sizeof(WORD));
107 buffer[length]=0;
108 buffer[length+1]=0x30; //=IMAGE_REL_BASED_HIGHLOW
109 (*(WORD *)(buffer+length))|=addr&0x0FFF;
110 length+=2;
111
112 (*(DWORD *)(buffer+NowCountAddrToReloc))+=sizeof(WORD);
113}
114void CReloc::ResetRelocBuffer(void){
115 if(buffer) HeapDefaultFree(buffer);
116
117 buffer=(char *)HeapAlloc(hHeap,0,1);
118 length=0;
119 NowPageRVAToReloc=0;
120
121 foreach( DWORD addr, codeSectionAddresses )
122 {
123 extern int MemPos_CodeSection;
124 __add(MemPos_CodeSection + addr);
125 }
126
127 foreach( DWORD addr, dataSectionAddresses )
128 {
129 extern int MemPos_DataSection;
130 __add(MemPos_DataSection + addr);
131 }
132}
Note: See TracBrowser for help on using the repository browser.