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

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

LexicalAnalyzerクラスでBasicSourceクラスを使用しないようにした。

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