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

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

OutputExe→Build
Compile()→MakeExe

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