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

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

[415]を64bit版にマージ。

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