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

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