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

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

・GetConstInfo関数を廃止し、LexicalAnalyzer::CollectConstsメソッドを追加。

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