source: dev/trunk/ab5.0/abdev/compiler_x86/MakePeHdr.cpp

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