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

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

SynonymErrorWordsを排除。
ClearSynonymErrorWordsメソッドを追加。

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