source: dev/trunk/ab5.0/abdev/compiler_x64/MakePeHdr.cpp@ 829

Last change on this file since 829 was 829, checked in by イグトランス (egtra), 12 years ago

svn:eol-styleとsvn:mime-type(文字コード指定含む)の設定

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