source: dev/trunk/abdev/BasicCompiler32/MakePeHdr.cpp@ 201

Last change on this file since 201 was 201, checked in by dai_9181, 17 years ago
File size: 55.2 KB
Line 
1#include <jenga/include/common/Environment.h>
2
3#include <jenga/include/smoothie/Smoothie.h>
4
5#include <LexicalScopingImpl.h>
6#include <ClassImpl.h>
7#include <Compiler.h>
8#include <NamespaceSupporter.h>
9
10#include "../BasicCompiler_Common/common.h"
11#include "Opcode.h"
12
13extern int GlobalOpBufferSize;
14
15
16////////////////////////////
17// 特殊関数の構造体ポインタ
18////////////////////////////
19
20// グローバル関数、静的メソッド
21UserProc
22 *pSub_System_StartupProgram,
23 *pSub_DebugSys_StartProc,
24 *pSub_DebugSys_EndProc,
25 *pSub_DebugSys_SaveContext,
26 *pSub_System_GetEip,
27 *pSub_System_InitDllGlobalVariables,
28 *pSub_System_InitStaticLocalVariables,
29 *pSub_System_Call_Destructor_of_GlobalObject,
30 *pSub_System_End,
31 *pSub_System_GetSp,
32 *pSub_pow,
33 *pSub_calloc,
34 *pSub_realloc,
35 *pSub_free,
36 *pSub_System_GC_malloc_ForObject,
37 *pSub_System_GC_malloc_ForObjectPtr,
38 *pSub_System_GC_free_for_SweepingDelete,
39 *pSubStaticMethod_System_TypeBase_InitializeUserTypes,
40
41 *pSub_allrem,
42 *pSub_aullrem,
43 *pSub_allmul,
44 *pSub_alldiv,
45 *pSub_aulldiv,
46 *pSub_allshl,
47 *pSub_allshr,
48 *pSub_aullshr,
49
50 *pSub_esp_error;
51
52// 動的メソッド
53UserProc
54 *pUserProc_System_CGarbageCollection_RegisterGlobalRoots;
55
56
57//////////////////////////////
58// 各セクションの位置とサイズ
59int FileSize_CodeSection,
60 FileSize_ExportSection,
61 FileSize_ImportSection,
62 FileSize_DataSection,
63 FileSize_RWSection,
64 FileSize_RSrcSection,
65 FileSize_RelocSection,
66 FileSize_DebugSection;
67int FilePos_CodeSection,
68 FilePos_ExportSection,
69 FilePos_ImportSection,
70 FilePos_DataSection,
71 FilePos_RWSection,
72 FilePos_RSrcSection,
73 FilePos_RelocSection,
74 FilePos_DebugSection;
75int MemSize_CodeSection,
76 MemSize_ExportSection,
77 MemSize_ImportSection,
78 MemSize_DataSection,
79 MemSize_RWSection,
80 MemSize_RSrcSection,
81 MemSize_RelocSection,
82 MemSize_DebugSection;
83int MemPos_CodeSection,
84 MemPos_ExportSection,
85 MemPos_ImportSection,
86 MemPos_DataSection,
87 MemPos_RWSection,
88 MemPos_RSrcSection,
89 MemPos_RelocSection,
90 MemPos_DebugSection;
91int bUse_CodeSection,
92 bUse_ExportSection,
93 bUse_ImportSection,
94 bUse_DataSection,
95 bUse_RWSection,
96 bUse_RSrcSection,
97 bUse_RelocSection,
98 bUse_DebugSection;
99
100
101void DebugVariable(void){
102 char temporary[255];
103 sprintf(temporary,"_DebugSys_dwThreadID[255]%c%cDWord",1,ESC_AS);
104 OpcodeDim(temporary,DIMFLAG_INITDEBUGVAR);
105 sprintf(temporary,"_DebugSys_ProcNum[255]%c%cDWord",1,ESC_AS);
106 OpcodeDim(temporary,DIMFLAG_INITDEBUGVAR);
107 sprintf(temporary,"_DebugSys_lplpObp[255]%c%c*ULONG_PTR",1,ESC_AS);
108 OpcodeDim(temporary,DIMFLAG_INITDEBUGVAR);
109 sprintf(temporary,"_DebugSys_lplpSpBase[255]%c%c*ULONG_PTR",1,ESC_AS);
110 OpcodeDim(temporary,DIMFLAG_INITDEBUGVAR);
111}
112
113void Compile(void){
114 extern HWND hMainDlg;
115 extern HWND hOwnerEditor;
116 extern HANDLE hHeap;
117 extern BOOL bDebugCompile;
118 extern BOOL bDll;
119 extern DWORD ImageBase;
120 extern int obp_AllocSize;
121 extern char *basbuf;
122 extern char OutputFileName[MAX_PATH];
123
124 int i,i2,i3,i4,i5;
125 char temporary[MAX_PATH],*temp2;
126 HANDLE hFile;
127
128
129 //コードセクションのメモリ上の位置
130 MemPos_CodeSection= 0x1000;
131
132 //データセクションのメモリ上の位置(仮定)
133 MemPos_DataSection= 0x10000000;
134
135
136 //エクスポート セクションを利用するかどうか
137 if(bDll) bUse_ExportSection=1;
138 else bUse_ExportSection=0;
139
140
141 //////////////////
142 // データテーブル
143 compiler.GetNativeCode().GetDataTable().Init();
144 if(bDebugCompile){
145 compiler.GetNativeCode().GetDataTable().Add( (long)0x00000002 );
146 }
147
148
149 //////////////////////
150 // コード生成前の準備
151 //////////////////////
152
153 //重複エラー情報管理のメモリを確保(グローバル領域コンパイル用)
154 extern char **SynonymErrorWords;
155 extern int SynonymErrorNum;
156 SynonymErrorNum=0;
157 SynonymErrorWords=(char **)HeapAlloc(hHeap,0,1);
158
159 //列挙体に関する情報の初期化
160 CEnumParent::InitEnum();
161
162 //列挙体からクラスコードを生成
163 char *temp;
164 temp=CEnumParent::GenerateCodes();
165 AddSourceCode(temp);
166 HeapDefaultFree(temp);
167
168 //関数ポインタ情報を初期化
169 compiler.GetMeta().GetProcPointers().clear();
170
171 // 名前空間情報を取得
172 NamespaceSupporter::CollectNamespaces(
173 Smoothie::Lexical::source.GetBuffer(),
174 compiler.GetMeta().GetNamespaces()
175 );
176
177 //クラス名を取得(詳細情報はGetAllClassInfoで取得)
178 // GetSubInfo関数の中で参照されるオブジェクト名を事前に取得する。
179 // ※オブジェクトの内容までは取得しない
180 compiler.GetMeta().GetClasses().CollectClassesForNameOnly( Smoothie::Lexical::source );
181
182 //TypeDef情報を初期化
183 compiler.GetMeta().GetTypeDefs().Init();
184
185 //定数情報を取得
186 GetConstInfo();
187
188 //サブルーチン(ユーザー定義、DLL関数)の識別子、アドレスを取得
189 Smoothie::Temp::pCompilingClass=0;
190 GetSubInfo();
191
192 //クラス情報を取得(※注 - GetSubInfoの後に呼び出す)
193 compiler.GetMeta().GetClasses().GetAllClassInfo();
194
195 if( !compiler.GetMeta().Write( Jenga::Common::Environment::GetAppDir() + "\\meta_test.xml" ) )
196 {
197 MessageBox(0,"XML書き込みに失敗","test",0);
198 }
199 MetaImpl tempMeta;
200 if( !tempMeta.Read( Jenga::Common::Environment::GetAppDir() + "\\meta_test.xml" ) )
201 {
202 MessageBox(0,"XML読み込みに失敗","test",0);
203 }
204
205 //コードと行番号の関係
206 extern LINEINFO *pLineInfo;
207 extern int MaxLineInfoNum;
208 pLineInfo=(LINEINFO *)HeapAlloc(hHeap,0,1);
209 MaxLineInfoNum=0;
210
211
212 ///////////////////////////
213 // 最低限必要な関数を取得
214 ///////////////////////////
215 cp=-1;
216
217 if(pSub_System_StartupProgram=GetSubHash("_System_StartupProgram",1))
218 pSub_System_StartupProgram->Using();
219
220 if(pSub_DebugSys_StartProc=GetSubHash("_DebugSys_StartProc",1))
221 pSub_DebugSys_StartProc->Using();
222
223 if(pSub_DebugSys_EndProc=GetSubHash("_DebugSys_EndProc",1))
224 pSub_DebugSys_EndProc->Using();
225
226 if(pSub_DebugSys_SaveContext=GetSubHash("_DebugSys_SaveContext",1))
227 pSub_DebugSys_SaveContext->Using();
228
229 if(pSub_System_GetEip=GetSubHash("_System_GetEip",1)){
230 pSub_System_GetEip->Using();
231 pSub_System_GetEip->ThisIsSystemProc();
232 }
233
234 if(pSub_System_InitDllGlobalVariables=GetSubHash("_System_InitDllGlobalVariables",1)){
235 pSub_System_InitDllGlobalVariables->Using();
236 pSub_System_InitDllGlobalVariables->ThisIsSystemProc();
237 }
238
239 if(pSub_System_InitStaticLocalVariables=GetSubHash("_System_InitStaticLocalVariables",1)){
240 pSub_System_InitStaticLocalVariables->Using();
241 pSub_System_InitStaticLocalVariables->ThisIsSystemProc();
242 }
243
244 if(pSub_System_Call_Destructor_of_GlobalObject=GetSubHash("_System_Call_Destructor_of_GlobalObject",1)){
245 pSub_System_Call_Destructor_of_GlobalObject->Using();
246 pSub_System_Call_Destructor_of_GlobalObject->ThisIsSystemProc();
247 }
248
249 if(pSub_System_End=GetSubHash("_System_End",1)){
250 pSub_System_End->Using();
251 }
252
253 if(pSub_System_GetSp=GetSubHash("_System_GetSp",1)){
254 pSub_System_GetSp->Using();
255 pSub_System_GetSp->ThisIsSystemProc();
256 }
257
258 if(pSub_pow=GetSubHash("pow",1))
259 pSub_pow->Using();
260
261 if(pSub_calloc=GetSubHash("calloc",1))
262 pSub_calloc->Using();
263
264 if(pSub_realloc=GetSubHash("realloc",1))
265 pSub_realloc->Using();
266
267 if(pSub_free=GetSubHash("free",1))
268 pSub_free->Using();
269
270 if( pSub_System_GC_malloc_ForObject = GetSubHash( "_System_GC_malloc_ForObject",1 ) )
271 pSub_System_GC_malloc_ForObject->Using();
272
273 if( pSub_System_GC_malloc_ForObjectPtr = GetSubHash( "_System_GC_malloc_ForObjectPtr",1 ) )
274 pSub_System_GC_malloc_ForObjectPtr->Using();
275
276 if( pSub_System_GC_free_for_SweepingDelete = GetSubHash( "_System_GC_free_for_SweepingDelete",1 ) )
277 pSub_System_GC_free_for_SweepingDelete->Using();
278
279 if( pSubStaticMethod_System_TypeBase_InitializeUserTypes = GetSubHash( "ActiveBasic.Core._System_TypeBase.InitializeUserTypes",1 ) ){
280 pSubStaticMethod_System_TypeBase_InitializeUserTypes->Using();
281 pSubStaticMethod_System_TypeBase_InitializeUserTypes->ThisIsAutoGenerationProc();
282 }
283
284 if( pUserProc_System_CGarbageCollection_RegisterGlobalRoots = GetClassMethod( "_System_CGarbageCollection", "RegisterGlobalRoots" ) ){
285 pUserProc_System_CGarbageCollection_RegisterGlobalRoots->Using();
286 pUserProc_System_CGarbageCollection_RegisterGlobalRoots->ThisIsAutoGenerationProc();
287 }
288
289 pSub_allrem=GetSubHash("_allrem");
290 pSub_allrem->Using();
291 pSub_allrem->ThisIsSystemProc();
292
293 pSub_aullrem=GetSubHash("_aullrem");
294 pSub_aullrem->Using();
295 pSub_aullrem->ThisIsSystemProc();
296
297 pSub_allmul=GetSubHash("_allmul");
298 pSub_allmul->Using();
299 pSub_allmul->ThisIsSystemProc();
300
301 pSub_alldiv=GetSubHash("_alldiv");
302 pSub_alldiv->Using();
303 pSub_alldiv->ThisIsSystemProc();
304
305 pSub_aulldiv=GetSubHash("_aulldiv");
306 pSub_aulldiv->Using();
307 pSub_aulldiv->ThisIsSystemProc();
308
309 pSub_allshl=GetSubHash("_allshl");
310 pSub_allshl->Using();
311 pSub_allshl->ThisIsSystemProc();
312
313 pSub_allshr=GetSubHash("_allshr");
314 pSub_allshr->Using();
315 pSub_allshr->ThisIsSystemProc();
316
317 pSub_aullshr=GetSubHash("_aullshr");
318 pSub_aullshr->Using();
319 pSub_aullshr->ThisIsSystemProc();
320
321 pSub_esp_error=GetSubHash("_esp_error");
322 pSub_esp_error->Using();
323
324
325
326
327 //データテーブルスケジュール
328 pobj_DataTableSchedule=new CSchedule();
329 pobj_DataTableSchedule->SetFlag(SCHEDULE_FLAG_RELOC);
330
331 //インポートテーブルスケジュール
332 pobj_ImportAddrSchedule=new CImportAddrSchedule();
333 pobj_ImportAddrSchedule->SetFlag(SCHEDULE_FLAG_RELOC);
334
335 //プロシージャポインタスケジュール
336 pobj_SubAddrSchedule=new CSubAddrSchedule();
337
338 //グローバル変数アドレススケジュール
339 pobj_GlobalVarSchedule=new CSchedule();
340 pobj_GlobalVarSchedule->SetFlag(SCHEDULE_FLAG_RELOC);
341
342 //グローバル変数の初期バッファ
343 extern BYTE *initGlobalBuf;
344 initGlobalBuf=(BYTE *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,1);
345
346 //リロケーション情報
347 pobj_Reloc=new CReloc();
348
349 obp_AllocSize=8192*2;
350 OpBuffer=(char *)malloc(obp_AllocSize);
351 obp=0;
352
353 //レキシカルスコープ情報を初期化
354 GetLexicalScopes().Init(obp);
355
356
357 /////////////////////////////////////////////////////////////////
358 // デバッグコンパイル用のログを生成する
359 /////////////////////////////////////////////////////////////////
360#ifdef _DEBUG
361 {
362 ofstream ofs( ( Jenga::Common::Environment::GetAppDir() + "\\middle_code.txt" ).c_str() );
363 ofs << basbuf << endl;
364 ofs.close();
365 }
366#endif
367
368
369 //////////////////////
370 // グローバル実行領域
371 //////////////////////
372
373 cp=-1;
374 UserProc::CompileStartForGlobalArea();
375
376 if(!bDll){
377 // 名前空間が初期化されているかをチェック
378 if( compiler.GetNamespaceSupporter().GetLivingNamespaceScopes().size() ){
379 SetError();
380 }
381
382 //ラベル用のメモリを確保
383 extern LABEL *pLabelNames;
384 extern int MaxLabelNum;
385 pLabelNames=(LABEL *)HeapAlloc(hHeap,0,1);
386 MaxLabelNum=0;
387
388 //Gotoラベルスケジュール
389 extern GOTOLABELSCHEDULE *pGotoLabelSchedule;
390 extern int GotoLabelScheduleNum;
391 pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapAlloc(hHeap,0,1);
392 GotoLabelScheduleNum=0;
393
394 //With情報のメモリを確保
395 extern WITHINFO WithInfo;
396 WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
397 WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
398 WithInfo.num=0;
399
400 //Continueアドレスを初期化
401 extern DWORD dwContinueAddress;
402 dwContinueAddress=-1;
403
404
405 //未完成
406 //OpBuffer[obp++]=(char)0xCC;
407
408 //push ebp
409 op_push(REG_EBP);
410
411 if(bDebugCompile){
412 //デバッグ用の変数を定義
413 DebugVariable();
414 }
415
416 //GC用の変数を定義
417 InitGCVariables();
418
419 //_System_StartupProgramの呼び出し
420 op_call(pSub_System_StartupProgram);
421
422 //クラスに属する静的メンバを定義
423 compiler.GetMeta().GetClasses().InitStaticMember();
424
425 //グローバル実行領域をコンパイル開始
426 CompileBuffer(0,0);
427
428 //ラベル用のメモリを解放
429 for(i=0;i<MaxLabelNum;i++){
430 if(pLabelNames[i].pName) HeapDefaultFree(pLabelNames[i].pName);
431 }
432 HeapDefaultFree(pLabelNames);
433
434 //Goto未知ラベルスケジュールを解放
435 for(i=0;i<GotoLabelScheduleNum;i++){
436 if(pGotoLabelSchedule[i].pName){
437 SetError(6,pGotoLabelSchedule[i].pName,pGotoLabelSchedule[i].now_cp);
438 HeapDefaultFree(pGotoLabelSchedule[i].pName);
439 }
440 else{
441 sprintf(temporary,"%d",pGotoLabelSchedule[i].line);
442 SetError(6,temporary,pGotoLabelSchedule[i].now_cp);
443 }
444 }
445 HeapDefaultFree(pGotoLabelSchedule);
446
447
448 ///////////////////////////////////////
449 // グローバル文字列変数の解放処理
450 ///////////////////////////////////////
451
452 //call _System_End
453 extern UserProc *pSub_System_End;
454 op_call(pSub_System_End);
455
456
457 //xor eax,eax(eaxを0に初期化する)
458 op_zero_reg(REG_EAX);
459
460 //pop ebp
461 op_pop(REG_EBP);
462
463 //ret
464 op_ret();
465
466 //グローバル実行領域のコードサイズ
467 GlobalOpBufferSize=obp;
468
469 //With情報のメモリを解放
470 for(i=0;i<WithInfo.num;i++){
471 SetError(22,"With",WithInfo.pWithCp[i]);
472 HeapDefaultFree(WithInfo.ppName[i]);
473 }
474 HeapDefaultFree(WithInfo.ppName);
475 HeapDefaultFree(WithInfo.pWithCp);
476
477 // 名前空間が正しく閉じられているかをチェック
478 if( compiler.GetNamespaceSupporter().GetLivingNamespaceScopes().size() ){
479 SetError(63,NULL,-1);
480 }
481
482 }
483 else{
484 ////////////////
485 // DLLの場合
486 ////////////////
487
488 }
489
490 //重複エラー情報管理のメモリを解放(グローバル領域用)
491 for(i=0;i<SynonymErrorNum;i++) HeapDefaultFree(SynonymErrorWords[i]);
492 HeapDefaultFree(SynonymErrorWords);
493 SynonymErrorWords=0;
494
495
496 StepCompileProgress();
497
498
499 /////////////////////
500 // ローカル実行領域
501 /////////////////////
502
503 //プロシージャをコンパイル開始
504 cp=0;
505 CompileLocal();
506
507 // 終了
508 ///////////////////////
509
510 StepCompileProgress();
511
512
513
514 ////////////////////////////////
515 // ここで一旦ログを取る
516 ////////////////////////////////
517 Diagnose();
518
519
520
521 ////////////////////////////////
522 // 使用するDLL関数のチェック
523 ////////////////////////////////
524 for(i=0;i<MAX_HASH;i++){
525 extern DllProc **ppDeclareHash;
526 const DllProc *pDllProc = ppDeclareHash[i];
527 while(pDllProc){
528 if( !pDllProc->IsUsing() ){
529 pDllProc=pDllProc->pNextData;
530 continue;
531 }
532
533 //エラーチェック
534 HINSTANCE hLib;
535 hLib=LoadLibrary( pDllProc->GetDllFileName().c_str() );
536 if(!hLib){
537 extern char OutputFileName[MAX_PATH];
538 char temp2[MAX_PATH],temp3[MAX_PATH];
539 _splitpath(OutputFileName,temp2,temp3,NULL,NULL);
540 lstrcat(temp2,temp3);
541 lstrcpy(temp3,pDllProc->GetDllFileName().c_str());
542 GetFullPath(temp3,temp2);
543 hLib=LoadLibrary(temp3);
544
545 if(!hLib){
546 SetError(-106,pDllProc->GetDllFileName().c_str(),pDllProc->GetCodePos());
547 }
548 }
549
550 if(hLib){
551 if(!GetProcAddress(hLib,pDllProc->GetAlias().c_str())){
552 FreeLibrary(hLib);
553 SetError(-107,pDllProc->GetAlias(),pDllProc->GetCodePos());
554 }
555 FreeLibrary(hLib);
556 }
557
558 pDllProc=pDllProc->pNextData;
559 }
560 }
561
562
563
564 /////////////////////////////
565 // リソースデータを読み込む
566 /////////////////////////////
567 extern char ResourceFileName[MAX_PATH];
568 GetResourceData(ResourceFileName);
569
570
571 //////////////////////////////
572 // エクスポート情報(DLLのみ)
573 //////////////////////////////
574 IMAGE_EXPORT_DIRECTORY ImageExportDirectory;
575 DWORD *lpdwExportAddressTable;
576 DWORD *lpdwExportNamePointerTable;
577 WORD *lpwExportOrdinalTable;
578 char lpExportNames[8192];
579 int ExportNamesLength;
580 int ExportNum;
581 int DllMain_EntryPoint;
582
583 DllMain_EntryPoint=-1;
584 ExportNum=0;
585 ExportNamesLength=0;
586 lpdwExportAddressTable=(DWORD *)HeapAlloc(hHeap,0,1);
587 lpdwExportNamePointerTable=(DWORD *)HeapAlloc(hHeap,0,1);
588 lpwExportOrdinalTable=(WORD *)HeapAlloc(hHeap,0,1);
589
590 if(bUse_ExportSection){
591 _splitpath(OutputFileName,NULL,NULL,lpExportNames,temporary);
592 lstrcat(lpExportNames,temporary);
593 ExportNamesLength=lstrlen(lpExportNames)+1;
594
595 extern GlobalProc **ppSubHash;
596 GlobalProc *pUserProc,*psi2;
597 while(1){
598 //辞書順にサーチ
599 temporary[0]=0;
600 for(i=0,psi2=0;i<MAX_HASH;i++){
601 pUserProc=ppSubHash[i];
602 while(pUserProc){
603 if(pUserProc->IsExport()){
604 if(temporary[0]=='\0'){
605 lstrcpy(temporary,pUserProc->GetName().c_str());
606 psi2=pUserProc;
607 }
608 else{
609 i3=lstrlen(temporary);
610 i4=(int)pUserProc->GetName().size();
611 if(i3<i4) i3=i4;
612 if(memcmp(temporary,pUserProc->GetName().c_str(),i3)>0){
613 lstrcpy(temporary,pUserProc->GetName().c_str());
614 psi2=pUserProc;
615 }
616 }
617 }
618 pUserProc=pUserProc->pNextData;
619 }
620 }
621 if(psi2==0) break;
622 pUserProc=psi2;
623
624 pUserProc->ExportOff();
625
626 if( pUserProc->GetName() == "DllMain" ){
627 DllMain_EntryPoint=pUserProc->beginOpAddress;
628 }
629
630 lpdwExportAddressTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportAddressTable,(ExportNum+1)*sizeof(DWORD));
631 lpdwExportAddressTable[ExportNum]=pUserProc->beginOpAddress;
632
633 lpdwExportNamePointerTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportNamePointerTable,(ExportNum+1)*sizeof(DWORD));
634 lpdwExportNamePointerTable[ExportNum]=ExportNamesLength;
635
636 lpwExportOrdinalTable=(WORD *)HeapReAlloc(hHeap,0,lpwExportOrdinalTable,(ExportNum+1)*sizeof(WORD));
637 lpwExportOrdinalTable[ExportNum]=ExportNum;
638
639 lstrcpy(lpExportNames+ExportNamesLength,pUserProc->GetName().c_str());
640 ExportNamesLength+=lstrlen(lpExportNames+ExportNamesLength)+1;
641
642 ExportNum++;
643 }
644 for(i=0;i<ExportNum;i++){
645 lpdwExportNamePointerTable[i]+=
646 sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
647 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
648 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
649 ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
650 }
651
652 ImageExportDirectory.Characteristics=0;
653 ImageExportDirectory.TimeDateStamp=(DWORD)time(NULL);
654 ImageExportDirectory.MajorVersion=0;
655 ImageExportDirectory.MinorVersion=0;
656 ImageExportDirectory.Name=sizeof(IMAGE_EXPORT_DIRECTORY)+
657 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
658 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
659 ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
660 ImageExportDirectory.Base=1;
661 ImageExportDirectory.NumberOfFunctions=ExportNum;
662 ImageExportDirectory.NumberOfNames=ExportNum;
663 ImageExportDirectory.AddressOfFunctions=sizeof(IMAGE_EXPORT_DIRECTORY);
664 ImageExportDirectory.AddressOfNames=ImageExportDirectory.AddressOfFunctions+ExportNum*sizeof(DWORD);
665 ImageExportDirectory.AddressOfNameOrdinals=ImageExportDirectory.AddressOfNames+ExportNum*sizeof(DWORD);
666 }
667
668
669 /////////////////////
670 //インポートDLL情報
671 /////////////////////
672 extern DllProc **ppDeclareHash;
673 char **ppDllNames;
674 int ImportDllNum;
675
676 /*
677 IMAGE_IMPORT_DESCRIPTOR1[size=0x14] インポートヘッダ
678 IMAGE_IMPORT_DESCRIPTOR2[size=0x14]
679 IMAGE_IMPORT_DESCRIPTOR3[size=0x14]
680 ...
681 Dll名1[size=0x10]
682 Dll名2[size=0x10]
683 Dll名3[size=0x10]
684 ...
685 ルックアップ テーブル(ヒントを示すRVA)
686 ヒントテーブル
687 インポート アドレス テーブル(ルックアップと同じ内容だが、プログラム実行時に実行アドレスに書き換えられる)
688 */
689
690 ppDllNames=(char **)HeapAlloc(hHeap,0,1);
691
692 ImportDllNum=0;
693 for(i=0;i<MAX_HASH;i++){
694 const DllProc *pDllProc=ppDeclareHash[i];
695 while(pDllProc){
696 if( !pDllProc->IsUsing() ){
697 pDllProc=pDllProc->pNextData;
698 continue;
699 }
700
701 if( pDllProc->GetDllFileName().size() > 16 ){
702 SetError(7,NULL,cp);
703 break;
704 }
705 for(i2=0;i2<ImportDllNum;i2++){
706 if( pDllProc->GetDllFileName() == ppDllNames[i2] ){
707 break;
708 }
709 }
710 if(i2==ImportDllNum){
711 ppDllNames=(char **)HeapReAlloc(hHeap,0,ppDllNames,(ImportDllNum+1)*sizeof(char **));
712 ppDllNames[ImportDllNum]=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,16);
713 lstrcpy(ppDllNames[ImportDllNum],pDllProc->GetDllFileName().c_str());
714 ImportDllNum++;
715 }
716
717 pDllProc=pDllProc->pNextData;
718 }
719 }
720
721 //インポート テーブル、及びルックアップ テーブル サイズの計算
722 IMAGE_IMPORT_DESCRIPTOR *pImportTable;
723 int LookupSize;
724 LookupSize=0;
725 pImportTable=(IMAGE_IMPORT_DESCRIPTOR *)HeapAlloc(hHeap,0,(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR));
726 i3=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR);
727 for(i=0;i<ImportDllNum;i++){
728 //インポート テーブル(IMAGE_IMPORT_DESCRIPTOR)
729 pImportTable[i].OriginalFirstThunk=i3+(ImportDllNum*0x10)+LookupSize;
730 pImportTable[i].TimeDateStamp=0;
731 pImportTable[i].ForwarderChain=0;
732 pImportTable[i].Name=i3+i*0x10;
733 for(i2=0;i2<MAX_HASH;i2++){
734 const DllProc *pDllProc=ppDeclareHash[i2];
735 while(pDllProc){
736 if( !pDllProc->IsUsing() ){
737 pDllProc=pDllProc->pNextData;
738 continue;
739 }
740
741 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
742 //ルックアップデータのサイズを追加
743 LookupSize+=sizeof(DWORD);
744 }
745
746 pDllProc=pDllProc->pNextData;
747 }
748 }
749 LookupSize+=sizeof(DWORD); //NULL用
750 }
751 memset(&pImportTable[i],0,sizeof(IMAGE_IMPORT_DESCRIPTOR));
752
753 //ルックアップ テーブル、ヒント テーブル
754 DWORD *pLookupTable;
755 pLookupTable=(DWORD *)HeapAlloc(hHeap,0,LookupSize*sizeof(DWORD)+1);
756 char *pHintTable;
757 int HintSize,HintAllocSize;
758 HintSize=0;
759 HintAllocSize=128*2;
760 pHintTable=(char *)HeapAlloc(hHeap,0,HintAllocSize);
761 i3+=ImportDllNum*0x10;
762 for(i=0,i5=0;i<ImportDllNum;i++){
763 for(i2=0;i2<MAX_HASH;i2++){
764 DllProc *pDllProc=ppDeclareHash[i2];
765 while(pDllProc){
766 if( !pDllProc->IsUsing() ){
767 pDllProc=pDllProc->pNextData;
768 continue;
769 }
770
771 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
772 //ルックアップの位置をセット(後にインポート アドレス テーブルにセットしなおされる)
773 pDllProc->SetLookupAddress( i3+(i5*sizeof(DWORD)) );
774
775 //ルックアップ テーブル
776 pLookupTable[i5++]=i3+LookupSize+HintSize;
777
778 //ヒント テーブル
779 pHintTable[HintSize++]=0;
780 pHintTable[HintSize++]=0;
781 lstrcpy(pHintTable+HintSize,pDllProc->GetAlias().c_str());
782 i4=(int)pDllProc->GetAlias().size();
783 HintSize+=i4+1;
784 if(i4%2==0) pHintTable[HintSize++]=0;
785
786 if(HintAllocSize<HintSize+128){
787 HintAllocSize+=128;
788 pHintTable=(char *)HeapReAlloc(hHeap,0,pHintTable,HintAllocSize);
789 }
790 }
791
792 pDllProc=pDllProc->pNextData;
793 }
794 }
795 pLookupTable[i5++]=0;
796 }
797
798
799 if(bDll){
800 //DLLの場合はリロケーション情報を仮生成
801 //※正式な生成は各セクションのメモリ上のサイズが決定してから再度行う。
802 pobj_Reloc->ResetRelocBuffer();
803 }
804
805
806
807 //グローバル変数情報を扱う構造体も初期バッファの有無による配置を行う
808 //(デバッグ情報で利用される)
809 extern int AllInitGlobalVarSize;
810 BOOST_FOREACH( Variable *pVar, globalVars ){
811 if(pVar->offset&0x80000000){
812 pVar->offset=(pVar->offset&0x7FFFFFFF)+AllInitGlobalVarSize;
813 }
814 }
815
816
817
818 ////////////////////////////////////
819 // デバッグセクションを生成
820 ////////////////////////////////////
821
822 //機械語コードとBasicコードの関係を整理
823 extern LINEINFO *pLineInfo;
824 extern int MaxLineInfoNum;
825 int MinObp,MaxObp;
826 LINEINFO *pTempLineInfo;
827 pTempLineInfo=(LINEINFO *)HeapAlloc(hHeap,0,MaxLineInfoNum*sizeof(LINEINFO));
828 i2=0;
829 MaxObp=0;
830 while(1){
831 for(i3=0,MinObp=0x0FFFFFFF;i3<MaxLineInfoNum;i3++){
832 if(pLineInfo[i3].TopObp<MinObp&&MaxObp<pLineInfo[i3].TopObp){
833 MinObp=pLineInfo[i3].TopObp;
834 i4=i3;
835 }
836 }
837 if(MinObp==0x0FFFFFFF) break;
838 pTempLineInfo[i2]=pLineInfo[i4];
839 MaxObp=pTempLineInfo[i2].TopObp;
840 i2++;
841 }
842 HeapDefaultFree(pLineInfo);
843 pLineInfo=(LINEINFO *)HeapAlloc(hHeap,0,i2*sizeof(LINEINFO));
844 memcpy(pLineInfo,pTempLineInfo,i2*sizeof(LINEINFO));
845 MaxLineInfoNum=i2;
846 HeapDefaultFree(pTempLineInfo);
847
848 //デバッグセクションを生成
849 CDebugSection *pobj_DebugSection;
850 pobj_DebugSection=new CDebugSection();
851 extern BOOL bDebugCompile;
852 extern BOOL bError;
853 if(bDebugCompile&&bError==0){
854 pobj_DebugSection->make();
855 }
856
857
858
859 /////////////////////////////////////
860 // 各セクションの位置とサイズを計算
861 /////////////////////////////////////
862
863 //コードセッションのファイル上のサイズ
864 if(obp%FILE_ALIGNMENT) FileSize_CodeSection=obp+(FILE_ALIGNMENT-obp%FILE_ALIGNMENT);
865 else FileSize_CodeSection=obp;
866 if(FileSize_CodeSection) bUse_CodeSection=1;
867 else bUse_CodeSection=0;
868
869 //エクスポートセクションのファイル上のサイズ
870 i= sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
871 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
872 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
873 ExportNum*sizeof(WORD)+ //エクスポート序数テーブルを飛び越す
874 ExportNamesLength; //シンボル名バッファ
875 if(bUse_ExportSection){
876 if(i%FILE_ALIGNMENT) FileSize_ExportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
877 else FileSize_ExportSection=i;
878 }
879 else FileSize_ExportSection=0;
880
881 //インポートセクションのファイル上のサイズ
882 i=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+
883 16*ImportDllNum+ //DLL名
884 LookupSize+ //ルックアップテーブル
885 HintSize+ //ヒント名(関数名)テーブル
886 LookupSize; //インポート アドレス テーブル
887 if(i%FILE_ALIGNMENT) FileSize_ImportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
888 else FileSize_ImportSection=i;
889 bUse_ImportSection=1; //インポートセクションは必ず存在する
890
891 //データセクションのファイル上のサイズ
892 if(compiler.GetNativeCode().GetDataTable().GetSize()%FILE_ALIGNMENT) FileSize_DataSection=compiler.GetNativeCode().GetDataTable().GetSize()+(FILE_ALIGNMENT-compiler.GetNativeCode().GetDataTable().GetSize()%FILE_ALIGNMENT);
893 else FileSize_DataSection=compiler.GetNativeCode().GetDataTable().GetSize();
894 if(FileSize_DataSection) bUse_DataSection=1;
895 else bUse_DataSection=0;
896
897 //リライタブルセクションのファイル上のサイズ(グローバル変数の初期情報のみを格納)
898 if(AllInitGlobalVarSize%FILE_ALIGNMENT) FileSize_RWSection=AllInitGlobalVarSize+(FILE_ALIGNMENT-AllInitGlobalVarSize%FILE_ALIGNMENT);
899 else{
900 if(AllInitGlobalVarSize) FileSize_RWSection=AllInitGlobalVarSize;
901 else FileSize_RWSection=FILE_ALIGNMENT;
902 }
903 bUse_RWSection=1;
904
905 //リソースセクションのファイル上のサイズ
906 char *RSrcSectionBuffer;
907 int RSrcSectionSize;
908 RSrcSectionBuffer=GetRSrcSectionBuffer(&RSrcSectionSize);
909 if(RSrcSectionSize%FILE_ALIGNMENT) FileSize_RSrcSection=RSrcSectionSize+(FILE_ALIGNMENT-RSrcSectionSize%FILE_ALIGNMENT);
910 else FileSize_RSrcSection=RSrcSectionSize;
911 if(FileSize_RSrcSection) bUse_RSrcSection=1;
912 else bUse_RSrcSection=0;
913
914 //リロケーションセクションのファイル上のサイズ
915 if(pobj_Reloc->length%FILE_ALIGNMENT) FileSize_RelocSection=pobj_Reloc->length+(FILE_ALIGNMENT-pobj_Reloc->length%FILE_ALIGNMENT);
916 else FileSize_RelocSection=pobj_Reloc->length;
917 if(FileSize_RelocSection) bUse_RelocSection=1;
918 else bUse_RelocSection=0;
919
920 //デバッグセクションのファイル上のサイズ
921 if(pobj_DebugSection->length%FILE_ALIGNMENT) FileSize_DebugSection=pobj_DebugSection->length+(FILE_ALIGNMENT-pobj_DebugSection->length%FILE_ALIGNMENT);
922 else FileSize_DebugSection=pobj_DebugSection->length;
923 if(FileSize_DebugSection) bUse_DebugSection=1;
924 else bUse_DebugSection=0;
925
926
927 //各セッションのファイル上の位置
928 FilePos_CodeSection= EXE_HEADER_SIZE;
929 FilePos_ExportSection= FilePos_CodeSection+
930 FileSize_CodeSection;
931 FilePos_ImportSection= FilePos_ExportSection+
932 FileSize_ExportSection;
933 FilePos_DataSection= FilePos_ImportSection+
934 FileSize_ImportSection;
935 FilePos_RWSection= FilePos_DataSection+
936 FileSize_DataSection;
937 FilePos_RSrcSection= FilePos_RWSection+
938 FileSize_RWSection;
939 FilePos_RelocSection= FilePos_RSrcSection+
940 FileSize_RSrcSection;
941 FilePos_DebugSection= FilePos_RelocSection+
942 FileSize_RelocSection;
943
944
945 //コードセッションのメモリ上のサイズ
946 if(FileSize_CodeSection%MEM_ALIGNMENT) MemSize_CodeSection=FileSize_CodeSection+(MEM_ALIGNMENT-FileSize_CodeSection%MEM_ALIGNMENT);
947 else MemSize_CodeSection=FileSize_CodeSection;
948
949 //エクスポートセクションのメモリ上のサイズ
950 if(FileSize_ExportSection%MEM_ALIGNMENT) MemSize_ExportSection=FileSize_ExportSection+(MEM_ALIGNMENT-FileSize_ExportSection%MEM_ALIGNMENT);
951 else MemSize_ExportSection=FileSize_ExportSection;
952
953 //インポートセクションのメモリ上のサイズ
954 if(FileSize_ImportSection%MEM_ALIGNMENT) MemSize_ImportSection=FileSize_ImportSection+(MEM_ALIGNMENT-FileSize_ImportSection%MEM_ALIGNMENT);
955 else MemSize_ImportSection=FileSize_ImportSection;
956
957 //データセクションのメモリ上のサイズ
958 if(FileSize_DataSection%MEM_ALIGNMENT) MemSize_DataSection=FileSize_DataSection+(MEM_ALIGNMENT-FileSize_DataSection%MEM_ALIGNMENT);
959 else MemSize_DataSection=FileSize_DataSection;
960
961 //リライタブルセクションのメモリ上のサイズ
962 extern int AllGlobalVarSize;
963 i=AllInitGlobalVarSize+AllGlobalVarSize;
964 if(i%MEM_ALIGNMENT) MemSize_RWSection=i+(MEM_ALIGNMENT-i%MEM_ALIGNMENT);
965 else MemSize_RWSection=i;
966
967 //リソースセクションのメモリ上のサイズ
968 if(FileSize_RSrcSection%MEM_ALIGNMENT) MemSize_RSrcSection=FileSize_RSrcSection+(MEM_ALIGNMENT-FileSize_RSrcSection%MEM_ALIGNMENT);
969 else MemSize_RSrcSection=FileSize_RSrcSection;
970
971 //リロケーションセクションのメモリ上のサイズ
972 if(FileSize_RelocSection%MEM_ALIGNMENT) MemSize_RelocSection=FileSize_RelocSection+(MEM_ALIGNMENT-FileSize_RelocSection%MEM_ALIGNMENT);
973 else MemSize_RelocSection=FileSize_RelocSection;
974
975 //デバッグセクションのメモリ上のサイズ
976 if(FileSize_DebugSection%MEM_ALIGNMENT) MemSize_DebugSection=FileSize_DebugSection+(MEM_ALIGNMENT-FileSize_DebugSection%MEM_ALIGNMENT);
977 else MemSize_DebugSection=FileSize_DebugSection;
978
979
980 //各セッションのメモリ上の位置
981 if(bUse_ExportSection) MemPos_ExportSection= 0x1000+
982 MemSize_CodeSection;
983 else MemPos_ExportSection=0;
984 if(bUse_ImportSection) MemPos_ImportSection= 0x1000+
985 MemSize_CodeSection+
986 MemSize_ExportSection;
987 else MemPos_ImportSection=0;
988 if(bUse_DataSection) MemPos_DataSection= 0x1000+
989 MemSize_CodeSection+
990 MemSize_ExportSection+
991 MemSize_ImportSection;
992 else MemPos_DataSection=0;
993 if(bUse_RWSection) MemPos_RWSection= 0x1000+
994 MemSize_CodeSection+
995 MemSize_ExportSection+
996 MemSize_ImportSection+
997 MemSize_DataSection;
998 else MemPos_RWSection=0;
999 if(bUse_RSrcSection) MemPos_RSrcSection= 0x1000+
1000 MemSize_CodeSection+
1001 MemSize_ExportSection+
1002 MemSize_ImportSection+
1003 MemSize_DataSection+
1004 MemSize_RWSection;
1005 else MemPos_RSrcSection=0;
1006 if(bUse_RelocSection) MemPos_RelocSection= 0x1000+
1007 MemSize_CodeSection+
1008 MemSize_ExportSection+
1009 MemSize_ImportSection+
1010 MemSize_DataSection+
1011 MemSize_RWSection+
1012 MemSize_RSrcSection;
1013 else MemPos_RelocSection=0;
1014 if(bUse_DebugSection) MemPos_DebugSection= 0x1000+
1015 MemSize_CodeSection+
1016 MemSize_ExportSection+
1017 MemSize_ImportSection+
1018 MemSize_DataSection+
1019 MemSize_RWSection+
1020 MemSize_RSrcSection+
1021 MemSize_RelocSection;
1022 else MemPos_DebugSection=0;
1023
1024
1025
1026 /////////////////////////////
1027 // GC用の変数スケジュール
1028 /////////////////////////////
1029 PerformedGcVarSchedule();
1030
1031
1032
1033 ////////////////////////////
1034 // エクスポート情報の再配置
1035 ////////////////////////////
1036 if(bUse_ExportSection){
1037 for(i=0;i<ExportNum;i++){
1038 lpdwExportAddressTable[i]+=MemPos_CodeSection;
1039 lpdwExportNamePointerTable[i]+=MemPos_ExportSection;
1040 }
1041
1042 ImageExportDirectory.Name+= MemPos_ExportSection;
1043 ImageExportDirectory.AddressOfFunctions+= MemPos_ExportSection;
1044 ImageExportDirectory.AddressOfNames+= MemPos_ExportSection;
1045 ImageExportDirectory.AddressOfNameOrdinals+= MemPos_ExportSection;
1046 }
1047
1048
1049 ////////////////////////////
1050 // インポート情報の再配置
1051 ////////////////////////////
1052 for(i=0,i5=0;i<ImportDllNum;i++){
1053 //インポート テーブル(IMAGE_IMPORT_DESCRIPTOR)
1054 pImportTable[i].OriginalFirstThunk+=MemPos_ImportSection;
1055 pImportTable[i].Name+=MemPos_ImportSection;
1056
1057 //インポート アドレス テーブル(ルックアップとヒントを飛び越す)
1058 pImportTable[i].FirstThunk=pImportTable[i].OriginalFirstThunk+
1059 LookupSize+ //ルックアップテーブル
1060 HintSize; //ヒント名(関数名)テーブル
1061
1062 for(i2=0;i2<MAX_HASH;i2++){
1063 const DllProc *pDllProc=ppDeclareHash[i2];
1064 while(pDllProc){
1065 if( !pDllProc->IsUsing() ){
1066 pDllProc=pDllProc->pNextData;
1067 continue;
1068 }
1069
1070 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
1071 //ルックアップ テーブル
1072 pLookupTable[i5++]+=MemPos_ImportSection;
1073 }
1074
1075 pDllProc=pDllProc->pNextData;
1076 }
1077 }
1078 i5++;
1079 }
1080
1081
1082 ////////////////////////////////////////
1083 //仮想関数データテーブルスケジュール
1084 compiler.GetMeta().GetClasses().ActionVtblSchedule(ImageBase,MemPos_CodeSection);
1085
1086
1087 //////////////////////////////
1088 // データテーブルスケジュール
1089 for(i=0;i<pobj_DataTableSchedule->num;i++){
1090 *((long *)(OpBuffer+pobj_DataTableSchedule->pObpValues[i]))+=ImageBase+MemPos_DataSection;
1091 }
1092 delete pobj_DataTableSchedule;
1093
1094
1095 //////////////////////////////////
1096 // インポートテーブルスケジュール(インポート アドレス テーブルを指し示させる)
1097 for(i=0;i<pobj_ImportAddrSchedule->num;i++){
1098 const DllProc *pDllProc=pobj_ImportAddrSchedule->ppdi[i];
1099 *((long *)(OpBuffer+pobj_ImportAddrSchedule->pObpValues[i]))=
1100 ImageBase+
1101 MemPos_ImportSection+
1102 LookupSize+ //ルックアップテーブル
1103 HintSize+ //ヒント名(関数名)テーブル
1104 pDllProc->GetLookupAddress();
1105 }
1106 delete pobj_ImportAddrSchedule;
1107
1108
1109 ////////////////////////////////////
1110 // プロシージャポインタスケジュール
1111 for(i=0;i<pobj_SubAddrSchedule->num;i++){
1112 if(pobj_SubAddrSchedule->ppsi[i]->beginOpAddress==0
1113 &&pobj_SubAddrSchedule->ppsi[i]->endOpAddress==0){
1114 SetError(300,NULL,-1);
1115 }
1116
1117 if(pobj_SubAddrSchedule->pbCall[i]){
1118 *((long *)(OpBuffer+pobj_SubAddrSchedule->pObpValues[i]))=
1119 pobj_SubAddrSchedule->ppsi[i]->beginOpAddress-(pobj_SubAddrSchedule->pObpValues[i]+sizeof(long));
1120 }
1121 else{
1122 *((long *)(OpBuffer+pobj_SubAddrSchedule->pObpValues[i]))=
1123 pobj_SubAddrSchedule->ppsi[i]->beginOpAddress+ImageBase+MemPos_CodeSection;
1124 }
1125 }
1126 delete pobj_SubAddrSchedule;
1127
1128
1129 if(bDll){
1130 //DLLの場合はリロケーション情報を生成
1131 pobj_Reloc->ResetRelocBuffer();
1132 }
1133
1134
1135
1136 //////////////////////////////////////
1137 // グローバル変数アドレススケジュール
1138 //////////////////////////////////////
1139
1140 /*
1141 GlobalArea1 - 初期バッファあり
1142 GlobalArea2 - 初期バッファなし
1143 */
1144 for(i=0;i<pobj_GlobalVarSchedule->num;i++){
1145 if(*((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i])) & 0x80000000){
1146 //初期バッファなし
1147 *((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i]))=
1148 AllInitGlobalVarSize+
1149 (*((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i])) & 0x7FFFFFFF)+
1150 ImageBase+MemPos_RWSection;
1151 }
1152 else{
1153 //初期バッファあり
1154 *((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i]))+=
1155 ImageBase+MemPos_RWSection;
1156 }
1157 }
1158 delete pobj_GlobalVarSchedule;
1159
1160
1161
1162 ////////////////////////////////
1163 // リソースアドレススケジュール
1164 extern DWORD *lpdwRSrcAddrSchedule;
1165 extern int RSrcAddrScheduleNum;
1166 for(i=0;i<RSrcAddrScheduleNum;i++){
1167 *(DWORD *)(RSrcSectionBuffer+lpdwRSrcAddrSchedule[i])+=MemPos_RSrcSection;
1168 }
1169 HeapDefaultFree(lpdwRSrcAddrSchedule);
1170
1171
1172 //Dosスタブ
1173 char *DosStubBuffer;
1174 int DosStubSize;
1175 hFile=CreateFile(
1176 ( Jenga::Common::Environment::GetAppDir() + "\\SubOperation\\dosstub.pgm" ).c_str(),
1177 GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
1178 if(hFile==INVALID_HANDLE_VALUE){
1179 MessageBox(hOwnerEditor,"dosstub.pgmの読み込みに失敗","error",MB_OK);
1180 goto EndWriteOpcode;
1181 }
1182 DosStubSize=GetFileSize(hFile,NULL);
1183 DosStubBuffer=(char *)HeapAlloc(hHeap,0,DosStubSize);
1184 ReadFile(hFile,DosStubBuffer,DosStubSize,(DWORD *)&i2,NULL);
1185 CloseHandle(hFile);
1186
1187
1188 extern BOOL bError;
1189 if(bError) goto EndWriteOpcode;
1190
1191
1192 ////////////////////////////
1193 // EXEファイルのヘッダ情報
1194 ////////////////////////////
1195
1196 IMAGE_DOS_HEADER ImageDosHeader;
1197 ImageDosHeader.e_magic= 0x5A4D;
1198 ImageDosHeader.e_cblp= 0x0090;
1199 ImageDosHeader.e_cp= 0x0003;
1200 ImageDosHeader.e_crlc= 0;
1201 ImageDosHeader.e_cparhdr=4;
1202 ImageDosHeader.e_minalloc=0x0000;
1203 ImageDosHeader.e_maxalloc=0xFFFF;
1204 ImageDosHeader.e_ss= 0x0000;
1205 ImageDosHeader.e_sp= 0x00B8;
1206 ImageDosHeader.e_csum= 0x0000;
1207 ImageDosHeader.e_ip= 0x0000;
1208 ImageDosHeader.e_cs= 0x0000;
1209 ImageDosHeader.e_lfarlc=0x0040;
1210 ImageDosHeader.e_ovno= 0x0000;
1211 ImageDosHeader.e_res[0]=0;
1212 ImageDosHeader.e_res[1]=0;
1213 ImageDosHeader.e_res[2]=0;
1214 ImageDosHeader.e_res[3]=0;
1215 ImageDosHeader.e_oemid= 0x0000;
1216 ImageDosHeader.e_oeminfo=0x0000;
1217 ImageDosHeader.e_res2[0]=0;
1218 ImageDosHeader.e_res2[1]=0;
1219 ImageDosHeader.e_res2[2]=0;
1220 ImageDosHeader.e_res2[3]=0;
1221 ImageDosHeader.e_res2[4]=0;
1222 ImageDosHeader.e_res2[5]=0;
1223 ImageDosHeader.e_res2[6]=0;
1224 ImageDosHeader.e_res2[7]=0;
1225 ImageDosHeader.e_res2[8]=0;
1226 ImageDosHeader.e_res2[9]=0;
1227 ImageDosHeader.e_lfanew=0x0100; //PEヘッダの位置
1228
1229
1230 /////////////////////////////////////////////
1231 // PEヘッダ
1232 /////////////////////////////////////////////
1233
1234 IMAGE_NT_HEADERS ImagePeHdr;
1235 ImagePeHdr.Signature=IMAGE_NT_SIGNATURE;
1236
1237 //マシンタイプ
1238 ImagePeHdr.FileHeader.Machine= IMAGE_FILE_MACHINE_I386;
1239
1240 ImagePeHdr.FileHeader.NumberOfSections= bUse_CodeSection+
1241 bUse_ExportSection+
1242 bUse_ImportSection+
1243 bUse_DataSection+
1244 bUse_RWSection+
1245 bUse_RSrcSection+
1246 bUse_RelocSection+
1247 bUse_DebugSection; //セクション数
1248 ImagePeHdr.FileHeader.TimeDateStamp= (DWORD)time(NULL);
1249 ImagePeHdr.FileHeader.PointerToSymbolTable= 0x00000000;
1250 ImagePeHdr.FileHeader.NumberOfSymbols= 0x00000000;
1251 ImagePeHdr.FileHeader.SizeOfOptionalHeader= IMAGE_SIZEOF_NT_OPTIONAL32_HEADER;
1252 if(bDll){
1253 ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_EXECUTABLE_IMAGE|
1254 IMAGE_FILE_32BIT_MACHINE|
1255 IMAGE_FILE_LINE_NUMS_STRIPPED|
1256 IMAGE_FILE_LOCAL_SYMS_STRIPPED|
1257 IMAGE_FILE_DLL;
1258 }
1259 else{
1260 ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_EXECUTABLE_IMAGE|
1261 IMAGE_FILE_32BIT_MACHINE|
1262 IMAGE_FILE_LINE_NUMS_STRIPPED|
1263 IMAGE_FILE_LOCAL_SYMS_STRIPPED;
1264 }
1265
1266 ImagePeHdr.OptionalHeader.Magic= 0x010B;
1267 ImagePeHdr.OptionalHeader.MajorLinkerVersion= 4;
1268 ImagePeHdr.OptionalHeader.MinorLinkerVersion= 0;
1269 ImagePeHdr.OptionalHeader.SizeOfCode= FileSize_CodeSection; //コードサイズ(.textのセッションサイズ)
1270 ImagePeHdr.OptionalHeader.SizeOfInitializedData=FileSize_DataSection; //データサイズ(.dataのセッションサイズ)
1271 ImagePeHdr.OptionalHeader.SizeOfUninitializedData=0; //未初期化データのサイズ(なし)
1272 if(bDll){
1273 if(DllMain_EntryPoint==-1)
1274 ImagePeHdr.OptionalHeader.AddressOfEntryPoint=0;
1275 else
1276 ImagePeHdr.OptionalHeader.AddressOfEntryPoint=MemPos_CodeSection+DllMain_EntryPoint;
1277 }
1278 else ImagePeHdr.OptionalHeader.AddressOfEntryPoint= MemPos_CodeSection;
1279 ImagePeHdr.OptionalHeader.BaseOfCode= MemPos_CodeSection; //.textのアドレス
1280 ImagePeHdr.OptionalHeader.BaseOfData= MemPos_DataSection; //.dataのアドレス
1281
1282 ImagePeHdr.OptionalHeader.ImageBase= ImageBase; //イメージベース
1283 ImagePeHdr.OptionalHeader.SectionAlignment= MEM_ALIGNMENT; //セクションアラインメント
1284 ImagePeHdr.OptionalHeader.FileAlignment= FILE_ALIGNMENT;
1285 ImagePeHdr.OptionalHeader.MajorOperatingSystemVersion=4;
1286 ImagePeHdr.OptionalHeader.MinorOperatingSystemVersion=0;
1287 ImagePeHdr.OptionalHeader.MajorImageVersion= 0;
1288 ImagePeHdr.OptionalHeader.MinorImageVersion= 0;
1289 ImagePeHdr.OptionalHeader.MajorSubsystemVersion=4;
1290 ImagePeHdr.OptionalHeader.MinorSubsystemVersion=0;
1291 ImagePeHdr.OptionalHeader.Win32VersionValue= 0;
1292 ImagePeHdr.OptionalHeader.SizeOfImage= EXE_HEADER_SIZE+
1293 MemSize_CodeSection+
1294 MemSize_ExportSection+
1295 MemSize_ImportSection+
1296 MemSize_DataSection+
1297 MemSize_RWSection+
1298 MemSize_RSrcSection+
1299 MemSize_RelocSection+
1300 MemSize_DebugSection;//すべてのイメージサイズ
1301 ImagePeHdr.OptionalHeader.SizeOfHeaders= EXE_HEADER_SIZE;//ヘッダサイズ
1302 ImagePeHdr.OptionalHeader.CheckSum= 0;
1303 extern unsigned short TypeOfSubSystem;
1304 ImagePeHdr.OptionalHeader.Subsystem= TypeOfSubSystem;
1305 ImagePeHdr.OptionalHeader.DllCharacteristics= 0;
1306 ImagePeHdr.OptionalHeader.SizeOfStackReserve= 0x00100000;
1307 ImagePeHdr.OptionalHeader.SizeOfStackCommit= 0x00001000;
1308 ImagePeHdr.OptionalHeader.SizeOfHeapReserve= 0x00100000;
1309 ImagePeHdr.OptionalHeader.SizeOfHeapCommit= 0x00001000;
1310 ImagePeHdr.OptionalHeader.LoaderFlags= 0;
1311 ImagePeHdr.OptionalHeader.NumberOfRvaAndSizes= IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1312
1313 //データ ディクショナリ
1314 ImagePeHdr.OptionalHeader.DataDirectory[0].VirtualAddress=MemPos_ExportSection;
1315 ImagePeHdr.OptionalHeader.DataDirectory[0].Size=FileSize_ExportSection;
1316 ImagePeHdr.OptionalHeader.DataDirectory[1].VirtualAddress=MemPos_ImportSection;//インポートテーブル
1317 ImagePeHdr.OptionalHeader.DataDirectory[1].Size=FileSize_ImportSection;
1318 ImagePeHdr.OptionalHeader.DataDirectory[2].VirtualAddress=MemPos_RSrcSection;
1319 ImagePeHdr.OptionalHeader.DataDirectory[2].Size=RSrcSectionSize;
1320 ImagePeHdr.OptionalHeader.DataDirectory[3].VirtualAddress=0;
1321 ImagePeHdr.OptionalHeader.DataDirectory[3].Size=0;
1322 ImagePeHdr.OptionalHeader.DataDirectory[4].VirtualAddress=0;
1323 ImagePeHdr.OptionalHeader.DataDirectory[4].Size=0;
1324 ImagePeHdr.OptionalHeader.DataDirectory[5].VirtualAddress=MemPos_RelocSection;
1325 ImagePeHdr.OptionalHeader.DataDirectory[5].Size=pobj_Reloc->length;
1326 ImagePeHdr.OptionalHeader.DataDirectory[6].VirtualAddress=0;
1327 ImagePeHdr.OptionalHeader.DataDirectory[6].Size=0;
1328 ImagePeHdr.OptionalHeader.DataDirectory[7].VirtualAddress=0;
1329 ImagePeHdr.OptionalHeader.DataDirectory[7].Size=0;
1330 ImagePeHdr.OptionalHeader.DataDirectory[8].VirtualAddress=0;
1331 ImagePeHdr.OptionalHeader.DataDirectory[8].Size=0;
1332 ImagePeHdr.OptionalHeader.DataDirectory[9].VirtualAddress=0;
1333 ImagePeHdr.OptionalHeader.DataDirectory[9].Size=0;
1334 ImagePeHdr.OptionalHeader.DataDirectory[10].VirtualAddress=0;
1335 ImagePeHdr.OptionalHeader.DataDirectory[10].Size=0;
1336 ImagePeHdr.OptionalHeader.DataDirectory[11].VirtualAddress=0;
1337 ImagePeHdr.OptionalHeader.DataDirectory[11].Size=0;
1338 ImagePeHdr.OptionalHeader.DataDirectory[12].VirtualAddress=MemPos_ImportSection+
1339 (ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+
1340 16*ImportDllNum+ //DLL名
1341 LookupSize+ //ルックアップテーブル
1342 HintSize; //ヒント名(関数名)テーブル
1343 ImagePeHdr.OptionalHeader.DataDirectory[12].Size=LookupSize;
1344 ImagePeHdr.OptionalHeader.DataDirectory[13].VirtualAddress=0;
1345 ImagePeHdr.OptionalHeader.DataDirectory[13].Size=0;
1346 ImagePeHdr.OptionalHeader.DataDirectory[14].VirtualAddress=0;
1347 ImagePeHdr.OptionalHeader.DataDirectory[14].Size=0;
1348 ImagePeHdr.OptionalHeader.DataDirectory[15].VirtualAddress=0;
1349 ImagePeHdr.OptionalHeader.DataDirectory[15].Size=0;
1350
1351
1352 //コードセクションヘッダ
1353 IMAGE_SECTION_HEADER CodeSectionHeader;
1354 memset((char *)CodeSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1355 lstrcpy((char *)CodeSectionHeader.Name,".text");
1356 CodeSectionHeader.Misc.VirtualSize= MemSize_CodeSection;
1357 CodeSectionHeader.VirtualAddress= MemPos_CodeSection; //開始アドレス
1358 CodeSectionHeader.SizeOfRawData= FileSize_CodeSection;
1359 CodeSectionHeader.PointerToRawData= FilePos_CodeSection; //ファイル上の開始アドレス
1360 CodeSectionHeader.PointerToRelocations= 0;
1361 CodeSectionHeader.PointerToLinenumbers= 0;
1362 CodeSectionHeader.NumberOfRelocations= 0;
1363 CodeSectionHeader.NumberOfLinenumbers= 0;
1364 CodeSectionHeader.Characteristics= IMAGE_SCN_MEM_EXECUTE|
1365 IMAGE_SCN_MEM_READ|
1366 IMAGE_SCN_CNT_CODE;
1367
1368 //エクスポートセクションヘッダ
1369 IMAGE_SECTION_HEADER ExportSectionHeader;
1370 memset((char *)ExportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1371 lstrcpy((char *)ExportSectionHeader.Name,".edata");
1372 ExportSectionHeader.Misc.VirtualSize= MemSize_ExportSection;
1373 ExportSectionHeader.VirtualAddress= MemPos_ExportSection; //開始アドレス
1374 ExportSectionHeader.SizeOfRawData= FileSize_ExportSection; //サイズ
1375 ExportSectionHeader.PointerToRawData= FilePos_ExportSection; //ファイル上の開始アドレス
1376 ExportSectionHeader.PointerToRelocations= 0;
1377 ExportSectionHeader.PointerToLinenumbers= 0;
1378 ExportSectionHeader.NumberOfRelocations= 0;
1379 ExportSectionHeader.NumberOfLinenumbers= 0;
1380 ExportSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1381 IMAGE_SCN_MEM_READ;
1382
1383 //インポートセクションヘッダ
1384 IMAGE_SECTION_HEADER ImportSectionHeader;
1385 memset((char *)ImportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1386 lstrcpy((char *)ImportSectionHeader.Name,".idata");
1387 ImportSectionHeader.Misc.VirtualSize= MemSize_ImportSection;
1388 ImportSectionHeader.VirtualAddress= MemPos_ImportSection; //開始アドレス
1389 ImportSectionHeader.SizeOfRawData= FileSize_ImportSection; //サイズ
1390 ImportSectionHeader.PointerToRawData= FilePos_ImportSection; //ファイル上の開始アドレス
1391 ImportSectionHeader.PointerToRelocations= 0;
1392 ImportSectionHeader.PointerToLinenumbers= 0;
1393 ImportSectionHeader.NumberOfRelocations= 0;
1394 ImportSectionHeader.NumberOfLinenumbers= 0;
1395 ImportSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1396 IMAGE_SCN_MEM_READ;
1397
1398 //データセクションヘッダ
1399 IMAGE_SECTION_HEADER DataSectionHeader;
1400 memset((char *)DataSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1401 lstrcpy((char *)DataSectionHeader.Name,".sdata");
1402 DataSectionHeader.Misc.VirtualSize= MemSize_DataSection;
1403 DataSectionHeader.VirtualAddress= MemPos_DataSection;
1404 DataSectionHeader.SizeOfRawData= FileSize_DataSection;
1405 DataSectionHeader.PointerToRawData= FilePos_DataSection;
1406 DataSectionHeader.PointerToRelocations= 0;
1407 DataSectionHeader.PointerToLinenumbers= 0;
1408 DataSectionHeader.NumberOfRelocations= 0;
1409 DataSectionHeader.NumberOfLinenumbers= 0;
1410 DataSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1411 IMAGE_SCN_MEM_READ|
1412 IMAGE_SCN_MEM_WRITE;
1413
1414 //リライタブルセクションヘッダ
1415 IMAGE_SECTION_HEADER RWSectionHeader;
1416 memset((char *)RWSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1417 lstrcpy((char *)RWSectionHeader.Name,".data");
1418 RWSectionHeader.Misc.VirtualSize= AllInitGlobalVarSize+AllGlobalVarSize;
1419 RWSectionHeader.VirtualAddress= MemPos_RWSection;
1420 RWSectionHeader.SizeOfRawData= FileSize_RWSection;
1421 RWSectionHeader.PointerToRawData= FilePos_RWSection;
1422 RWSectionHeader.PointerToRelocations= 0;
1423 RWSectionHeader.PointerToLinenumbers= 0;
1424 RWSectionHeader.NumberOfRelocations= 0;
1425 RWSectionHeader.NumberOfLinenumbers= 0;
1426 RWSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1427 IMAGE_SCN_MEM_READ|
1428 IMAGE_SCN_MEM_WRITE;
1429
1430 //リソースセクションヘッダ
1431 IMAGE_SECTION_HEADER RSrcSectionHeader;
1432 memset((char *)RSrcSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1433 lstrcpy((char *)RSrcSectionHeader.Name,".rsrc");
1434 RSrcSectionHeader.Misc.VirtualSize= RSrcSectionSize;
1435 RSrcSectionHeader.VirtualAddress= MemPos_RSrcSection;
1436 RSrcSectionHeader.SizeOfRawData= FileSize_RSrcSection;
1437 RSrcSectionHeader.PointerToRawData= FilePos_RSrcSection;
1438 RSrcSectionHeader.PointerToRelocations= 0;
1439 RSrcSectionHeader.PointerToLinenumbers= 0;
1440 RSrcSectionHeader.NumberOfRelocations= 0;
1441 RSrcSectionHeader.NumberOfLinenumbers= 0;
1442 RSrcSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1443 IMAGE_SCN_MEM_READ;
1444
1445 //リロケーションセクションヘッダ
1446 IMAGE_SECTION_HEADER RelocSectionHeader;
1447 memset((char *)RelocSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1448 lstrcpy((char *)RelocSectionHeader.Name,".reloc");
1449 RelocSectionHeader.Misc.VirtualSize= pobj_Reloc->length;
1450 RelocSectionHeader.VirtualAddress= MemPos_RelocSection; //開始アドレス
1451 RelocSectionHeader.SizeOfRawData= FileSize_RelocSection; //サイズ
1452 RelocSectionHeader.PointerToRawData= FilePos_RelocSection; //ファイル上の開始アドレス
1453 RelocSectionHeader.PointerToRelocations= 0;
1454 RelocSectionHeader.PointerToLinenumbers= 0;
1455 RelocSectionHeader.NumberOfRelocations= 0;
1456 RelocSectionHeader.NumberOfLinenumbers= 0;
1457 RelocSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1458 IMAGE_SCN_MEM_DISCARDABLE|
1459 IMAGE_SCN_MEM_READ;
1460
1461 //デバッグセクションヘッダ
1462 IMAGE_SECTION_HEADER DebugSectionHeader;
1463 memset((char *)DebugSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1464 lstrcpy((char *)DebugSectionHeader.Name,".debug");
1465 DebugSectionHeader.Misc.VirtualSize= pobj_DebugSection->length;
1466 DebugSectionHeader.VirtualAddress= MemPos_DebugSection; //開始アドレス
1467 DebugSectionHeader.SizeOfRawData= FileSize_DebugSection; //サイズ
1468 DebugSectionHeader.PointerToRawData= FilePos_DebugSection; //ファイル上の開始アドレス
1469 DebugSectionHeader.PointerToRelocations= 0;
1470 DebugSectionHeader.PointerToLinenumbers= 0;
1471 DebugSectionHeader.NumberOfRelocations= 0;
1472 DebugSectionHeader.NumberOfLinenumbers= 0;
1473 DebugSectionHeader.Characteristics= IMAGE_SCN_MEM_DISCARDABLE|
1474 IMAGE_SCN_MEM_READ;
1475
1476
1477 hFile=CreateFile(OutputFileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
1478 if(hFile==INVALID_HANDLE_VALUE){
1479 SetError(53,OutputFileName,-1);
1480 goto EndWriteOpcode;
1481 }
1482
1483 //ヘッダ
1484 WriteFile(hFile,(void *)&ImageDosHeader,sizeof(IMAGE_DOS_HEADER),(DWORD *)&i2,NULL);
1485 i=i2;
1486
1487 //Dosスタブ
1488 WriteFile(hFile,DosStubBuffer,DosStubSize,(DWORD *)&i2,NULL);
1489 i+=i2;
1490
1491 //0x0100までNULLを並べる
1492 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,0x0100-i);
1493 WriteFile(hFile,temp2,0x0100-i,(DWORD *)&i2,NULL);
1494 HeapDefaultFree(temp2);
1495 i+=i2;
1496
1497 //PEヘッダ
1498 WriteFile(hFile,&ImagePeHdr,sizeof(IMAGE_NT_HEADERS),(DWORD *)&i2,NULL);
1499 i+=i2;
1500
1501 //コード セクション ヘッダ
1502 WriteFile(hFile,&CodeSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1503 i+=i2;
1504
1505 if(bUse_ExportSection){
1506 //エクスポート セクション ヘッダ
1507 WriteFile(hFile,&ExportSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1508 i+=i2;
1509 }
1510 if(bUse_ImportSection){
1511 //インポート セクション ヘッダ
1512 WriteFile(hFile,&ImportSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1513 i+=i2;
1514 }
1515 if(bUse_DataSection){
1516 //データ セクション ヘッダ
1517 WriteFile(hFile,&DataSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1518 i+=i2;
1519 }
1520 if(bUse_RWSection){
1521 //リライタブルセクションヘッダ
1522 WriteFile(hFile,&RWSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1523 i+=i2;
1524 }
1525 if(bUse_RSrcSection){
1526 //リソースセクションヘッダ
1527 WriteFile(hFile,&RSrcSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1528 i+=i2;
1529 }
1530 if(bUse_RelocSection){
1531 //リロケーションセクションヘッダ
1532 WriteFile(hFile,&RelocSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1533 i+=i2;
1534 }
1535 if(bUse_DebugSection){
1536 //デバッグセクションヘッダ
1537 WriteFile(hFile,&DebugSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1538 i+=i2;
1539 }
1540
1541 //EXE_HEADER_SIZEまでNULLを並べる
1542 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,EXE_HEADER_SIZE-i);
1543 WriteFile(hFile,temp2,EXE_HEADER_SIZE-i,(DWORD *)&i2,NULL);
1544 HeapDefaultFree(temp2);
1545 i+=i2;
1546
1547 //コード
1548 WriteFile(hFile,OpBuffer,obp,(DWORD *)&i2,NULL);
1549 i+=i2;
1550
1551 //FilePos_ExportSectionまでNULLを並べる
1552 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_ExportSection-i);
1553 WriteFile(hFile,temp2,FilePos_ExportSection-i,(DWORD *)&i2,NULL);
1554 HeapDefaultFree(temp2);
1555 i+=i2;
1556
1557 if(bUse_ExportSection){
1558 //エクスポート ディレクトリ テーブル
1559 WriteFile(hFile,&ImageExportDirectory,sizeof(IMAGE_EXPORT_DIRECTORY),(DWORD *)&i2,NULL);
1560 i+=i2;
1561
1562 //エクスポート アドレス テーブル
1563 WriteFile(hFile,lpdwExportAddressTable,ExportNum*sizeof(DWORD),(DWORD *)&i2,NULL);
1564 i+=i2;
1565
1566 //エクスポート名ポインタ テーブル
1567 WriteFile(hFile,lpdwExportNamePointerTable,ExportNum*sizeof(DWORD),(DWORD *)&i2,NULL);
1568 i+=i2;
1569
1570 //エクスポート序数テーブル
1571 WriteFile(hFile,lpwExportOrdinalTable,ExportNum*sizeof(WORD),(DWORD *)&i2,NULL);
1572 i+=i2;
1573
1574 //シンボル名
1575 WriteFile(hFile,lpExportNames,ExportNamesLength,(DWORD *)&i2,NULL);
1576 i+=i2;
1577 }
1578
1579 //FilePos_ImportSectionまでNULLを並べる
1580 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_ImportSection-i);
1581 WriteFile(hFile,temp2,FilePos_ImportSection-i,(DWORD *)&i2,NULL);
1582 HeapDefaultFree(temp2);
1583 i+=i2;
1584
1585 if(bUse_ImportSection){
1586 //インポート ディレクトリ テーブル(Nullディレクトリ テーブルを含む)
1587 for(i3=0;i3<(ImportDllNum+1);i3++){
1588 WriteFile(hFile,&pImportTable[i3],sizeof(IMAGE_IMPORT_DESCRIPTOR),(DWORD *)&i2,NULL);
1589 i+=i2;
1590 }
1591
1592 //DLL名
1593 for(i3=0;i3<ImportDllNum;i3++){
1594 WriteFile(hFile,ppDllNames[i3],16,(DWORD *)&i2,NULL);
1595 i+=i2;
1596 }
1597
1598 //ルックアップ テーブル
1599 WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
1600 i+=i2;
1601
1602 //ヒント テーブル
1603 WriteFile(hFile,pHintTable,HintSize,(DWORD *)&i2,NULL);
1604 i+=i2;
1605
1606 //インポート アドレス テーブル
1607 WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
1608 i+=i2;
1609 }
1610
1611 //FilePos_DataSectionまでNULLを並べる
1612 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_DataSection-i);
1613 WriteFile(hFile,temp2,FilePos_DataSection-i,(DWORD *)&i2,NULL);
1614 HeapDefaultFree(temp2);
1615 i+=i2;
1616
1617 if(bUse_DataSection){
1618 //データ テーブル
1619 WriteFile(hFile,compiler.GetNativeCode().GetDataTable().GetPtr(),compiler.GetNativeCode().GetDataTable().GetSize(),(DWORD *)&i2,NULL);
1620 i+=i2;
1621 }
1622
1623 //FilePos_RWSectionまでNULLを並べる
1624 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RWSection-i);
1625 WriteFile(hFile,temp2,FilePos_RWSection-i,(DWORD *)&i2,NULL);
1626 HeapDefaultFree(temp2);
1627 i+=i2;
1628
1629 if(bUse_RWSection){
1630 //リライタブル データ テーブル(グローバル変数の初期バッファ)
1631 initGlobalBuf=(BYTE *)HeapReAlloc(hHeap,
1632 HEAP_ZERO_MEMORY,
1633 initGlobalBuf,
1634 FileSize_RWSection);
1635 WriteFile(hFile,initGlobalBuf,FileSize_RWSection,(DWORD *)&i2,NULL);
1636 i+=i2;
1637 }
1638
1639 //FilePos_RSrcSectionまでNULLを並べる
1640 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RSrcSection-i);
1641 WriteFile(hFile,temp2,FilePos_RSrcSection-i,(DWORD *)&i2,NULL);
1642 HeapDefaultFree(temp2);
1643 i+=i2;
1644
1645 if(bUse_RSrcSection){
1646 //リソースバッファ
1647 WriteFile(hFile,RSrcSectionBuffer,RSrcSectionSize,(DWORD *)&i2,NULL);
1648 i+=i2;
1649 }
1650
1651 //FilePos_RelocSectionまでNULLを並べる
1652 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RelocSection-i);
1653 WriteFile(hFile,temp2,FilePos_RelocSection-i,(DWORD *)&i2,NULL);
1654 HeapDefaultFree(temp2);
1655 i+=i2;
1656
1657 if(bUse_RelocSection){
1658 //リロケーション情報
1659 WriteFile(hFile,pobj_Reloc->buffer,pobj_Reloc->length,(DWORD *)&i2,NULL);
1660 i+=i2;
1661 }
1662
1663 //ファイルアラインメントを考慮
1664 if(i%FILE_ALIGNMENT){
1665 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
1666 WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
1667 HeapDefaultFree(temp2);
1668 i+=i2;
1669 }
1670
1671 if(bUse_DebugSection){
1672 //デバッグセクション
1673 WriteFile(hFile,pobj_DebugSection->buffer,pobj_DebugSection->length,(DWORD *)&i2,NULL);
1674 i+=i2;
1675 }
1676
1677 //ファイルアラインメントを考慮
1678 if(i%FILE_ALIGNMENT){
1679 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
1680 WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
1681 HeapDefaultFree(temp2);
1682 i+=i2;
1683 }
1684
1685 //書き込み終了
1686 CloseHandle(hFile);
1687
1688
1689EndWriteOpcode:
1690
1691 //Dosスタブ用のメモリを解放
1692 HeapDefaultFree(DosStubBuffer);
1693
1694 //エクスポート テーブル情報を解放
1695 HeapDefaultFree(lpdwExportAddressTable);
1696 HeapDefaultFree(lpdwExportNamePointerTable);
1697 HeapDefaultFree(lpwExportOrdinalTable);
1698
1699 //インポートDLL情報を解放
1700 HeapDefaultFree(pImportTable);
1701 for(i=0;i<ImportDllNum;i++)
1702 HeapDefaultFree(ppDllNames[i]);
1703 HeapDefaultFree(ppDllNames);
1704
1705 //ルックアップテーブルに関する情報を解放
1706 HeapDefaultFree(pLookupTable);
1707
1708 //ヒントテーブルに関する情報を解放
1709 HeapDefaultFree(pHintTable);
1710
1711 //グローバル変数の初期バッファを解放
1712 HeapDefaultFree(initGlobalBuf);
1713
1714 //コードバッファを解放
1715 free(OpBuffer);
1716 OpBuffer=0;
1717
1718 //リソースセクションバッファを解放
1719 HeapDefaultFree(RSrcSectionBuffer);
1720
1721 //デバッグセクションを開放
1722 delete pobj_DebugSection;
1723
1724 //リロケーション情報を解放
1725 delete pobj_Reloc;
1726
1727 //列挙体に関する情報の破棄
1728 CEnumParent::DestroyEnum();
1729
1730 //クラスに関するメモリを解放
1731 compiler.GetMeta().GetClasses().Clear();
1732}
Note: See TracBrowser for help on using the repository browser.