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

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