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

Last change on this file since 357 was 357, checked in by dai_9181, 17 years ago

例外処理機構実装中...

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