source: dev/trunk/abdev/BasicCompiler64/MakePeHdr.cpp@ 309

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