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

Last change on this file since 407 was 407, checked in by dai_9181, 16 years ago

Enum.cpp/Enum.hを移動した

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