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

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

[585]を32bit版にマージ。

File size: 56.2 KB
Line 
1#include "stdafx.h"
2
3#include "../BasicCompiler_Common/common.h"
4#include "../BasicCompiler_Common/DebugSection.h"
5#include "Opcode.h"
6
7
8////////////////////////////
9// 特殊関数の構造体ポインタ
10////////////////////////////
11
12// グローバル関数、静的メソッド
13const UserProc
14 *pSub_System_StartupProgram,
15 *pSub_System_GlobalArea,
16 *pSub_DebugSys_StartProc,
17 *pSub_DebugSys_EndProc,
18 *pSub_DebugSys_SaveContext,
19 *pSub_System_GetEip,
20 *pSub_System_InitDllGlobalVariables,
21 *pSub_System_InitStaticLocalVariables,
22 *pSub_System_Call_Destructor_of_GlobalObject,
23 *pSub_System_End,
24 *pSub_pow,
25 *pSub_calloc,
26 *pSub_realloc,
27 *pSub_free,
28 *pSub_System_GC_malloc_ForObject,
29 *pSub_System_GC_malloc_ForObjectPtr,
30 *pSub_System_GC_free_for_SweepingDelete,
31 *pSub_System_AddNeedFreeTempStructure,
32 *pSub_System_FreeTempStructure,
33 *pSubStaticMethod_System_TypeBase_InitializeUserTypes,
34 *pSubStaticMethod_System_TypeBase_InitializeUserTypesForBaseType,
35
36 *pSub_allrem,
37 *pSub_aullrem,
38 *pSub_allmul,
39 *pSub_alldiv,
40 *pSub_aulldiv,
41 *pSub_allshl,
42 *pSub_allshr,
43 *pSub_aullshr,
44
45 *pSub_esp_error;
46
47// 動的メソッド
48const UserProc
49 *pUserProc_System_CGarbageCollection_RegisterGlobalRoots;
50
51
52//////////////////////////////
53// 各セクションの位置とサイズ
54int FileSize_CodeSection,
55 FileSize_ExportSection,
56 FileSize_ImportSection,
57 FileSize_DataSection,
58 FileSize_RWSection,
59 FileSize_RSrcSection,
60 FileSize_RelocSection,
61 FileSize_DebugSection;
62int FilePos_CodeSection,
63 FilePos_ExportSection,
64 FilePos_ImportSection,
65 FilePos_DataSection,
66 FilePos_RWSection,
67 FilePos_RSrcSection,
68 FilePos_RelocSection,
69 FilePos_DebugSection;
70int MemSize_CodeSection,
71 MemSize_ExportSection,
72 MemSize_ImportSection,
73 MemSize_DataSection,
74 MemSize_RWSection,
75 MemSize_RSrcSection,
76 MemSize_RelocSection,
77 MemSize_DebugSection;
78int MemPos_CodeSection,
79 MemPos_ExportSection,
80 MemPos_ImportSection,
81 MemPos_DataSection,
82 MemPos_RWSection,
83 MemPos_RSrcSection,
84 MemPos_RelocSection,
85 MemPos_DebugSection;
86int bUse_CodeSection,
87 bUse_ExportSection,
88 bUse_ImportSection,
89 bUse_DataSection,
90 bUse_RWSection,
91 bUse_RSrcSection,
92 bUse_RelocSection,
93 bUse_DebugSection;
94
95
96void Compile(void){
97 extern HWND hMainDlg;
98 extern HWND hOwnerEditor;
99 extern HANDLE hHeap;
100 extern DWORD ImageBase;
101 extern char *basbuf;
102
103 int i,i2,i3,i4,i5;
104 char temporary[MAX_PATH],*temp2;
105 HANDLE hFile;
106
107
108 //コードセクションのメモリ上の位置
109 MemPos_CodeSection= 0x1000;
110
111 //データセクションのメモリ上の位置(仮定)
112 MemPos_DataSection= 0x10000000;
113
114
115 //エクスポート セクションを利用するかどうか
116 if( compiler.IsDll() ) bUse_ExportSection=1;
117 else bUse_ExportSection=0;
118
119
120 // 静的リンク
121 compiler.StaticLink( compiler.staticLibraries );
122
123
124 //////////////////
125 // データテーブル
126 if( compiler.IsDebug() ){
127 compiler.GetObjectModule().nativeSection.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().nativeSection.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().nativeSection.GetCurrentSource().GetBuffer(),
159 compiler.GetObjectModule().meta.GetNamespaces()
160 );
161
162 // デリゲートに関する情報を収集
163 {
164 ActiveBasic::Compiler::LexicalAnalyzer::CollectDelegates(
165 compiler.GetObjectModule().nativeSection.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().nativeSection.GetCurrentSource().GetBuffer(),
182 compiler.GetObjectModule().meta.GetClasses()
183 );
184
185 //TypeDef情報を収集
186 ActiveBasic::Compiler::LexicalAnalyzer::CollectTypeDefs(
187 compiler.GetObjectModule().nativeSection.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().nativeSection.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().nativeSection.GetCurrentSource().GetBuffer(),
211 compiler.GetObjectModule().meta.GetUserProcs(),
212 compiler.GetObjectModule().meta.GetDllProcs()
213 );
214
215 // クラス情報を取得(※注 - CollectProceduresの後に呼び出す)
216 ActiveBasic::Compiler::LexicalAnalyzer::CollectClasses(
217 compiler.GetObjectModule().nativeSection.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().nativeSection.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 ActiveBasic::Compiler::VtblGenerator::GenerateVTablesForAllClasses(
540 compiler.GetObjectModule().meta.GetClasses()
541 );
542
543
544
545 ////////////////////////////////
546 // ここで一旦ログを取る
547 ////////////////////////////////
548
549 Diagnose();
550
551
552
553 ////////////////////////////////
554 // 使用するDLL関数のチェック
555 ////////////////////////////////
556 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
557 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
558 {
559 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
560
561 if( !pDllProc->IsUsing() ){
562 continue;
563 }
564
565 //エラーチェック
566 HINSTANCE hLib;
567 hLib=LoadLibrary( pDllProc->GetDllFileName().c_str() );
568 if(!hLib){
569 char temp2[MAX_PATH],temp3[MAX_PATH];
570 _splitpath(program.GetOutputFilePath().c_str(),temp2,temp3,NULL,NULL);
571 lstrcat(temp2,temp3);
572 lstrcpy(temp3,pDllProc->GetDllFileName().c_str());
573 GetFullPath(temp3,temp2);
574 hLib=LoadLibrary(temp3);
575
576 if(!hLib){
577 compiler.errorMessenger.Output(-106,pDllProc->GetDllFileName().c_str(),pDllProc->GetCodePos());
578 }
579 }
580
581 if(hLib){
582 if(!GetProcAddress(hLib,pDllProc->GetAlias().c_str())){
583 FreeLibrary(hLib);
584 compiler.errorMessenger.Output(-107,pDllProc->GetAlias(),pDllProc->GetCodePos());
585 }
586 FreeLibrary(hLib);
587 }
588 }
589
590
591
592 /////////////////////////////
593 // リソースデータを読み込む
594 /////////////////////////////
595 extern char ResourceFileName[MAX_PATH];
596 GetResourceData(ResourceFileName);
597
598
599 //////////////////////////////
600 // エクスポート情報(DLLのみ)
601 //////////////////////////////
602 IMAGE_EXPORT_DIRECTORY ImageExportDirectory;
603 DWORD *lpdwExportAddressTable;
604 DWORD *lpdwExportNamePointerTable;
605 WORD *lpwExportOrdinalTable;
606 char lpExportNames[8192];
607 int ExportNamesLength;
608 int ExportNum;
609 int DllMain_EntryPoint;
610
611 DllMain_EntryPoint=-1;
612 ExportNum=0;
613 ExportNamesLength=0;
614 lpdwExportAddressTable=(DWORD *)HeapAlloc(hHeap,0,1);
615 lpdwExportNamePointerTable=(DWORD *)HeapAlloc(hHeap,0,1);
616 lpwExportOrdinalTable=(WORD *)HeapAlloc(hHeap,0,1);
617
618 if(bUse_ExportSection){
619 _splitpath(program.GetOutputFilePath().c_str(),NULL,NULL,lpExportNames,temporary);
620 lstrcat(lpExportNames,temporary);
621 ExportNamesLength=lstrlen(lpExportNames)+1;
622
623 while(1)
624 {
625 UserProc *pUserProc = NULL;
626
627 //辞書順にサーチ
628 temporary[0]=0;
629 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
630 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
631 {
632 UserProc *pTempUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
633 if(pTempUserProc->IsExport()){
634 if(temporary[0]=='\0'){
635 lstrcpy(temporary,pTempUserProc->GetName().c_str());
636 pUserProc = pTempUserProc;
637 }
638 else{
639 i3=lstrlen(temporary);
640 i4=(int)pTempUserProc->GetName().size();
641 if(i3<i4) i3=i4;
642 if(memcmp(temporary,pTempUserProc->GetName().c_str(),i3)>0){
643 lstrcpy(temporary,pTempUserProc->GetName().c_str());
644 pUserProc = pTempUserProc;
645 }
646 }
647 }
648 }
649 if( pUserProc == NULL )
650 {
651 break;
652 }
653
654 pUserProc->ExportOff();
655
656 if( pUserProc->GetName() == "DllMain" ){
657 DllMain_EntryPoint = pUserProc->GetBeginOpAddress();
658 }
659
660 lpdwExportAddressTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportAddressTable,(ExportNum+1)*sizeof(DWORD));
661 lpdwExportAddressTable[ExportNum] = pUserProc->GetBeginOpAddress();
662
663 lpdwExportNamePointerTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportNamePointerTable,(ExportNum+1)*sizeof(DWORD));
664 lpdwExportNamePointerTable[ExportNum]=ExportNamesLength;
665
666 lpwExportOrdinalTable=(WORD *)HeapReAlloc(hHeap,0,lpwExportOrdinalTable,(ExportNum+1)*sizeof(WORD));
667 lpwExportOrdinalTable[ExportNum]=ExportNum;
668
669 lstrcpy(lpExportNames+ExportNamesLength,pUserProc->GetName().c_str());
670 ExportNamesLength+=lstrlen(lpExportNames+ExportNamesLength)+1;
671
672 ExportNum++;
673 }
674 for(i=0;i<ExportNum;i++){
675 lpdwExportNamePointerTable[i]+=
676 sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
677 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
678 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
679 ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
680 }
681
682 ImageExportDirectory.Characteristics=0;
683 ImageExportDirectory.TimeDateStamp=(DWORD)time(NULL);
684 ImageExportDirectory.MajorVersion=0;
685 ImageExportDirectory.MinorVersion=0;
686 ImageExportDirectory.Name=sizeof(IMAGE_EXPORT_DIRECTORY)+
687 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
688 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
689 ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
690 ImageExportDirectory.Base=1;
691 ImageExportDirectory.NumberOfFunctions=ExportNum;
692 ImageExportDirectory.NumberOfNames=ExportNum;
693 ImageExportDirectory.AddressOfFunctions=sizeof(IMAGE_EXPORT_DIRECTORY);
694 ImageExportDirectory.AddressOfNames=ImageExportDirectory.AddressOfFunctions+ExportNum*sizeof(DWORD);
695 ImageExportDirectory.AddressOfNameOrdinals=ImageExportDirectory.AddressOfNames+ExportNum*sizeof(DWORD);
696 }
697
698
699 /////////////////////
700 //インポートDLL情報
701 /////////////////////
702
703 /*
704 IMAGE_IMPORT_DESCRIPTOR1[size=0x14] インポートヘッダ
705 IMAGE_IMPORT_DESCRIPTOR2[size=0x14]
706 IMAGE_IMPORT_DESCRIPTOR3[size=0x14]
707 ...
708 Dll名1[size=0x10]
709 Dll名2[size=0x10]
710 Dll名3[size=0x10]
711 ...
712 ルックアップ テーブル(ヒントを示すRVA)
713 ヒントテーブル
714 インポート アドレス テーブル(ルックアップと同じ内容だが、プログラム実行時に実行アドレスに書き換えられる)
715 */
716
717 char **ppDllNames=(char **)HeapAlloc(hHeap,0,1);
718
719 int ImportDllNum=0;
720
721 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
722 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
723 {
724 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
725
726 if( !pDllProc->IsUsing() ){
727 continue;
728 }
729
730 if( pDllProc->GetDllFileName().size() > 16 ){
731 compiler.errorMessenger.Output(7,NULL,cp);
732 break;
733 }
734 for(i2=0;i2<ImportDllNum;i2++){
735 if( pDllProc->GetDllFileName() == ppDllNames[i2] ){
736 break;
737 }
738 }
739 if(i2==ImportDllNum){
740 ppDllNames=(char **)HeapReAlloc(hHeap,0,ppDllNames,(ImportDllNum+1)*sizeof(char **));
741 ppDllNames[ImportDllNum]=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,16);
742 lstrcpy(ppDllNames[ImportDllNum],pDllProc->GetDllFileName().c_str());
743 ImportDllNum++;
744 }
745 }
746
747 //インポート テーブル、及びルックアップ テーブル サイズの計算
748 IMAGE_IMPORT_DESCRIPTOR *pImportTable;
749 int LookupSize;
750 LookupSize=0;
751 pImportTable=(IMAGE_IMPORT_DESCRIPTOR *)HeapAlloc(hHeap,0,(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR));
752 i3=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR);
753 for(i=0;i<ImportDllNum;i++){
754 //インポート テーブル(IMAGE_IMPORT_DESCRIPTOR)
755 pImportTable[i].OriginalFirstThunk=i3+(ImportDllNum*0x10)+LookupSize;
756 pImportTable[i].TimeDateStamp=0;
757 pImportTable[i].ForwarderChain=0;
758 pImportTable[i].Name=i3+i*0x10;
759
760 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
761 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
762 {
763 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
764
765 if( !pDllProc->IsUsing() ){
766 continue;
767 }
768
769 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
770 //ルックアップデータのサイズを追加
771 LookupSize+=sizeof(DWORD);
772 }
773 }
774 LookupSize+=sizeof(DWORD); //NULL用
775 }
776 memset(&pImportTable[i],0,sizeof(IMAGE_IMPORT_DESCRIPTOR));
777
778 //ルックアップ テーブル、ヒント テーブル
779 DWORD *pLookupTable;
780 pLookupTable=(DWORD *)HeapAlloc(hHeap,0,LookupSize*sizeof(DWORD)+1);
781 char *pHintTable;
782 int HintSize,HintAllocSize;
783 HintSize=0;
784 HintAllocSize=128*2;
785 pHintTable=(char *)HeapAlloc(hHeap,0,HintAllocSize);
786 i3+=ImportDllNum*0x10;
787 for(i=0,i5=0;i<ImportDllNum;i++){
788 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
789 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
790 {
791 DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
792
793 if( !pDllProc->IsUsing() ){
794 continue;
795 }
796
797 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
798 //ルックアップの位置をセット(後にインポート アドレス テーブルにセットしなおされる)
799 pDllProc->SetLookupAddress( i3+(i5*sizeof(DWORD)) );
800
801 //ルックアップ テーブル
802 pLookupTable[i5++]=i3+LookupSize+HintSize;
803
804 //ヒント テーブル
805 pHintTable[HintSize++]=0;
806 pHintTable[HintSize++]=0;
807 lstrcpy(pHintTable+HintSize,pDllProc->GetAlias().c_str());
808 i4=(int)pDllProc->GetAlias().size();
809 HintSize+=i4+1;
810 if(i4%2==0) pHintTable[HintSize++]=0;
811
812 if(HintAllocSize<HintSize+128){
813 HintAllocSize+=128;
814 pHintTable=(char *)HeapReAlloc(hHeap,0,pHintTable,HintAllocSize);
815 }
816 }
817 }
818 pLookupTable[i5++]=0;
819 }
820
821
822 if( compiler.IsDll() ){
823 //DLLの場合はリロケーション情報を仮生成
824 //※正式な生成は各セクションのメモリ上のサイズが決定してから再度行う。
825 pobj_Reloc->ResetRelocBuffer();
826 }
827
828
829
830 //グローバル変数情報を扱う構造体も初期バッファの有無による配置を行う
831 //(デバッグ情報で利用される)
832 BOOST_FOREACH( Variable *pVar, compiler.GetObjectModule().meta.GetGlobalVars() ){
833 if(pVar->GetOffsetAddress()&0x80000000){
834 pVar->SetOffsetAddress(
835 (pVar->GetOffsetAddress()&0x7FFFFFFF)
836 + compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
837 );
838 }
839 }
840
841
842
843 ////////////////////////////////////
844 // デバッグセクションを生成
845 ////////////////////////////////////
846
847 //デバッグセクションを生成
848 CDebugSection *pobj_DebugSection;
849 pobj_DebugSection=new CDebugSection();
850 if( compiler.IsDebug() && !compiler.errorMessenger.HasError() )
851 {
852 compiler.messenger.Output( "デバッグ情報を生成しています。" );
853
854 pobj_DebugSection->make();
855
856 compiler.messenger.Output( "デバッグ情報の生成が完了しました。" );
857 }
858
859
860
861 /////////////////////////////////////
862 // 各セクションの位置とサイズを計算
863 /////////////////////////////////////
864
865 //コードセッションのファイル上のサイズ
866 if(compiler.linker.GetNativeCode().GetSize()%FILE_ALIGNMENT)
867 {
868 FileSize_CodeSection =
869 compiler.linker.GetNativeCode().GetSize()
870 + (FILE_ALIGNMENT-compiler.linker.GetNativeCode().GetSize()%FILE_ALIGNMENT);
871 }
872 else
873 {
874 FileSize_CodeSection = compiler.linker.GetNativeCode().GetSize();
875 }
876 if(FileSize_CodeSection) bUse_CodeSection=1;
877 else bUse_CodeSection=0;
878
879 //エクスポートセクションのファイル上のサイズ
880 i= sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
881 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
882 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
883 ExportNum*sizeof(WORD)+ //エクスポート序数テーブルを飛び越す
884 ExportNamesLength; //シンボル名バッファ
885 if(bUse_ExportSection){
886 if(i%FILE_ALIGNMENT) FileSize_ExportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
887 else FileSize_ExportSection=i;
888 }
889 else FileSize_ExportSection=0;
890
891 //インポートセクションのファイル上のサイズ
892 i=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+
893 16*ImportDllNum+ //DLL名
894 LookupSize+ //ルックアップテーブル
895 HintSize+ //ヒント名(関数名)テーブル
896 LookupSize; //インポート アドレス テーブル
897 if(i%FILE_ALIGNMENT) FileSize_ImportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
898 else FileSize_ImportSection=i;
899 bUse_ImportSection=1; //インポートセクションは必ず存在する
900
901 //データセクションのファイル上のサイズ
902 if(compiler.GetObjectModule().nativeSection.dataTable.GetSize()%FILE_ALIGNMENT) FileSize_DataSection=compiler.GetObjectModule().nativeSection.dataTable.GetSize()+(FILE_ALIGNMENT-compiler.GetObjectModule().nativeSection.dataTable.GetSize()%FILE_ALIGNMENT);
903 else FileSize_DataSection=compiler.GetObjectModule().nativeSection.dataTable.GetSize();
904 if(FileSize_DataSection) bUse_DataSection=1;
905 else bUse_DataSection=0;
906
907 //リライタブルセクションのファイル上のサイズ(グローバル変数の初期情報のみを格納)
908 if( compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize() % FILE_ALIGNMENT )
909 {
910 FileSize_RWSection =
911 compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
912 + (FILE_ALIGNMENT-compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()%FILE_ALIGNMENT);
913 }
914 else{
915 if( compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize() )
916 {
917 FileSize_RWSection = compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize();
918 }
919 else FileSize_RWSection=FILE_ALIGNMENT;
920 }
921 bUse_RWSection=1;
922
923 //リソースセクションのファイル上のサイズ
924 char *RSrcSectionBuffer;
925 int RSrcSectionSize;
926 RSrcSectionBuffer=GetRSrcSectionBuffer(&RSrcSectionSize);
927 if(RSrcSectionSize%FILE_ALIGNMENT) FileSize_RSrcSection=RSrcSectionSize+(FILE_ALIGNMENT-RSrcSectionSize%FILE_ALIGNMENT);
928 else FileSize_RSrcSection=RSrcSectionSize;
929 if(FileSize_RSrcSection) bUse_RSrcSection=1;
930 else bUse_RSrcSection=0;
931
932 //リロケーションセクションのファイル上のサイズ
933 if(pobj_Reloc->length%FILE_ALIGNMENT) FileSize_RelocSection=pobj_Reloc->length+(FILE_ALIGNMENT-pobj_Reloc->length%FILE_ALIGNMENT);
934 else FileSize_RelocSection=pobj_Reloc->length;
935 if(FileSize_RelocSection) bUse_RelocSection=1;
936 else bUse_RelocSection=0;
937
938 //デバッグセクションのファイル上のサイズ
939 if(pobj_DebugSection->length%FILE_ALIGNMENT) FileSize_DebugSection=pobj_DebugSection->length+(FILE_ALIGNMENT-pobj_DebugSection->length%FILE_ALIGNMENT);
940 else FileSize_DebugSection=pobj_DebugSection->length;
941 if(FileSize_DebugSection) bUse_DebugSection=1;
942 else bUse_DebugSection=0;
943
944
945 //各セッションのファイル上の位置
946 FilePos_CodeSection= EXE_HEADER_SIZE;
947 FilePos_ExportSection= FilePos_CodeSection+
948 FileSize_CodeSection;
949 FilePos_ImportSection= FilePos_ExportSection+
950 FileSize_ExportSection;
951 FilePos_DataSection= FilePos_ImportSection+
952 FileSize_ImportSection;
953 FilePos_RWSection= FilePos_DataSection+
954 FileSize_DataSection;
955 FilePos_RSrcSection= FilePos_RWSection+
956 FileSize_RWSection;
957 FilePos_RelocSection= FilePos_RSrcSection+
958 FileSize_RSrcSection;
959 FilePos_DebugSection= FilePos_RelocSection+
960 FileSize_RelocSection;
961
962
963 //コードセッションのメモリ上のサイズ
964 if(FileSize_CodeSection%MEM_ALIGNMENT) MemSize_CodeSection=FileSize_CodeSection+(MEM_ALIGNMENT-FileSize_CodeSection%MEM_ALIGNMENT);
965 else MemSize_CodeSection=FileSize_CodeSection;
966
967 //エクスポートセクションのメモリ上のサイズ
968 if(FileSize_ExportSection%MEM_ALIGNMENT) MemSize_ExportSection=FileSize_ExportSection+(MEM_ALIGNMENT-FileSize_ExportSection%MEM_ALIGNMENT);
969 else MemSize_ExportSection=FileSize_ExportSection;
970
971 //インポートセクションのメモリ上のサイズ
972 if(FileSize_ImportSection%MEM_ALIGNMENT) MemSize_ImportSection=FileSize_ImportSection+(MEM_ALIGNMENT-FileSize_ImportSection%MEM_ALIGNMENT);
973 else MemSize_ImportSection=FileSize_ImportSection;
974
975 //データセクションのメモリ上のサイズ
976 if(FileSize_DataSection%MEM_ALIGNMENT) MemSize_DataSection=FileSize_DataSection+(MEM_ALIGNMENT-FileSize_DataSection%MEM_ALIGNMENT);
977 else MemSize_DataSection=FileSize_DataSection;
978
979 //リライタブルセクションのメモリ上のサイズ
980 i = compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
981 + compiler.GetObjectModule().meta.GetGlobalVars().GetAllSize();
982 if(i%MEM_ALIGNMENT) MemSize_RWSection=i+(MEM_ALIGNMENT-i%MEM_ALIGNMENT);
983 else MemSize_RWSection=i;
984
985 //リソースセクションのメモリ上のサイズ
986 if(FileSize_RSrcSection%MEM_ALIGNMENT) MemSize_RSrcSection=FileSize_RSrcSection+(MEM_ALIGNMENT-FileSize_RSrcSection%MEM_ALIGNMENT);
987 else MemSize_RSrcSection=FileSize_RSrcSection;
988
989 //リロケーションセクションのメモリ上のサイズ
990 if(FileSize_RelocSection%MEM_ALIGNMENT) MemSize_RelocSection=FileSize_RelocSection+(MEM_ALIGNMENT-FileSize_RelocSection%MEM_ALIGNMENT);
991 else MemSize_RelocSection=FileSize_RelocSection;
992
993 //デバッグセクションのメモリ上のサイズ
994 if(FileSize_DebugSection%MEM_ALIGNMENT) MemSize_DebugSection=FileSize_DebugSection+(MEM_ALIGNMENT-FileSize_DebugSection%MEM_ALIGNMENT);
995 else MemSize_DebugSection=FileSize_DebugSection;
996
997
998 //各セッションのメモリ上の位置
999 if(bUse_ExportSection) MemPos_ExportSection= 0x1000+
1000 MemSize_CodeSection;
1001 else MemPos_ExportSection=0;
1002 if(bUse_ImportSection) MemPos_ImportSection= 0x1000+
1003 MemSize_CodeSection+
1004 MemSize_ExportSection;
1005 else MemPos_ImportSection=0;
1006 if(bUse_DataSection) MemPos_DataSection= 0x1000+
1007 MemSize_CodeSection+
1008 MemSize_ExportSection+
1009 MemSize_ImportSection;
1010 else MemPos_DataSection=0;
1011 if(bUse_RWSection) MemPos_RWSection= 0x1000+
1012 MemSize_CodeSection+
1013 MemSize_ExportSection+
1014 MemSize_ImportSection+
1015 MemSize_DataSection;
1016 else MemPos_RWSection=0;
1017 if(bUse_RSrcSection) MemPos_RSrcSection= 0x1000+
1018 MemSize_CodeSection+
1019 MemSize_ExportSection+
1020 MemSize_ImportSection+
1021 MemSize_DataSection+
1022 MemSize_RWSection;
1023 else MemPos_RSrcSection=0;
1024 if(bUse_RelocSection) MemPos_RelocSection= 0x1000+
1025 MemSize_CodeSection+
1026 MemSize_ExportSection+
1027 MemSize_ImportSection+
1028 MemSize_DataSection+
1029 MemSize_RWSection+
1030 MemSize_RSrcSection;
1031 else MemPos_RelocSection=0;
1032 if(bUse_DebugSection) MemPos_DebugSection= 0x1000+
1033 MemSize_CodeSection+
1034 MemSize_ExportSection+
1035 MemSize_ImportSection+
1036 MemSize_DataSection+
1037 MemSize_RWSection+
1038 MemSize_RSrcSection+
1039 MemSize_RelocSection;
1040 else MemPos_DebugSection=0;
1041
1042
1043
1044 ////////////////////////////
1045 // エクスポート情報の再配置
1046 ////////////////////////////
1047 if(bUse_ExportSection){
1048 for(i=0;i<ExportNum;i++){
1049 lpdwExportAddressTable[i]+=MemPos_CodeSection;
1050 lpdwExportNamePointerTable[i]+=MemPos_ExportSection;
1051 }
1052
1053 ImageExportDirectory.Name+= MemPos_ExportSection;
1054 ImageExportDirectory.AddressOfFunctions+= MemPos_ExportSection;
1055 ImageExportDirectory.AddressOfNames+= MemPos_ExportSection;
1056 ImageExportDirectory.AddressOfNameOrdinals+= MemPos_ExportSection;
1057 }
1058
1059
1060 ////////////////////////////
1061 // インポート情報の再配置
1062 ////////////////////////////
1063 for(i=0,i5=0;i<ImportDllNum;i++){
1064 //インポート テーブル(IMAGE_IMPORT_DESCRIPTOR)
1065 pImportTable[i].OriginalFirstThunk+=MemPos_ImportSection;
1066 pImportTable[i].Name+=MemPos_ImportSection;
1067
1068 //インポート アドレス テーブル(ルックアップとヒントを飛び越す)
1069 pImportTable[i].FirstThunk=pImportTable[i].OriginalFirstThunk+
1070 LookupSize+ //ルックアップテーブル
1071 HintSize; //ヒント名(関数名)テーブル
1072
1073 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
1074 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
1075 {
1076 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
1077
1078 if( !pDllProc->IsUsing() ){
1079 continue;
1080 }
1081
1082 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
1083 //ルックアップ テーブル
1084 pLookupTable[i5++]+=MemPos_ImportSection;
1085 }
1086 }
1087 i5++;
1088 }
1089
1090
1091 ////////////////////////////////////////
1092 //仮想関数データテーブルスケジュール
1093 ActiveBasic::Compiler::VtblGenerator::ActionVtblScheduleForAllClasses(
1094 compiler.GetObjectModule().meta.GetClasses(),
1095 ImageBase,
1096 MemPos_CodeSection,
1097 MemPos_DataSection
1098 );
1099
1100
1101 if( compiler.IsDll() ){
1102 //DLLの場合はリロケーション情報を生成
1103 pobj_Reloc->ResetRelocBuffer();
1104 }
1105
1106 compiler.linker.SetDataTable( compiler.GetObjectModule().nativeSection.dataTable );
1107
1108 compiler.linker.SetImageBase( ImageBase );
1109 compiler.linker.ResolveDataTableSchedules( MemPos_DataSection );
1110 compiler.linker.ResolveCatchAddressSchedules( MemPos_CodeSection );
1111 compiler.linker.ResolveDllProcSchedules( MemPos_CodeSection, MemPos_ImportSection, LookupSize, HintSize );
1112 compiler.linker.ResolveUserProcSchedules( MemPos_CodeSection );
1113 compiler.linker.ResolveGlobalVarSchedules( MemPos_RWSection );
1114 compiler.linker.ResolveVtblSchedule( MemPos_DataSection );
1115 compiler.linker.ResolveTypeInfoSchedule( MemPos_DataSection );
1116
1117
1118
1119 ////////////////////////////////
1120 // リソースアドレススケジュール
1121 extern DWORD *lpdwRSrcAddrSchedule;
1122 extern int RSrcAddrScheduleNum;
1123 for(i=0;i<RSrcAddrScheduleNum;i++){
1124 *(DWORD *)(RSrcSectionBuffer+lpdwRSrcAddrSchedule[i])+=MemPos_RSrcSection;
1125 }
1126 HeapDefaultFree(lpdwRSrcAddrSchedule);
1127
1128
1129 //Dosスタブ
1130 char *DosStubBuffer;
1131 int DosStubSize;
1132 hFile=CreateFile(
1133 ( ActiveBasic::Common::Environment::GetAbdevSystemDirPath() + "\\dosstub.pgm" ).c_str(),
1134 GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
1135 if(hFile==INVALID_HANDLE_VALUE){
1136 MessageBox(hOwnerEditor,"dosstub.pgmの読み込みに失敗","error",MB_OK);
1137 goto EndWriteOpcode;
1138 }
1139 DosStubSize=GetFileSize(hFile,NULL);
1140 DosStubBuffer=(char *)HeapAlloc(hHeap,0,DosStubSize);
1141 ReadFile(hFile,DosStubBuffer,DosStubSize,(DWORD *)&i2,NULL);
1142 CloseHandle(hFile);
1143
1144
1145 if( compiler.errorMessenger.HasError() )
1146 {
1147 goto EndWriteOpcode;
1148 }
1149
1150
1151 ////////////////////////////
1152 // EXEファイルのヘッダ情報
1153 ////////////////////////////
1154
1155 IMAGE_DOS_HEADER ImageDosHeader;
1156 ImageDosHeader.e_magic= 0x5A4D;
1157 ImageDosHeader.e_cblp= 0x0090;
1158 ImageDosHeader.e_cp= 0x0003;
1159 ImageDosHeader.e_crlc= 0;
1160 ImageDosHeader.e_cparhdr=4;
1161 ImageDosHeader.e_minalloc=0x0000;
1162 ImageDosHeader.e_maxalloc=0xFFFF;
1163 ImageDosHeader.e_ss= 0x0000;
1164 ImageDosHeader.e_sp= 0x00B8;
1165 ImageDosHeader.e_csum= 0x0000;
1166 ImageDosHeader.e_ip= 0x0000;
1167 ImageDosHeader.e_cs= 0x0000;
1168 ImageDosHeader.e_lfarlc=0x0040;
1169 ImageDosHeader.e_ovno= 0x0000;
1170 ImageDosHeader.e_res[0]=0;
1171 ImageDosHeader.e_res[1]=0;
1172 ImageDosHeader.e_res[2]=0;
1173 ImageDosHeader.e_res[3]=0;
1174 ImageDosHeader.e_oemid= 0x0000;
1175 ImageDosHeader.e_oeminfo=0x0000;
1176 ImageDosHeader.e_res2[0]=0;
1177 ImageDosHeader.e_res2[1]=0;
1178 ImageDosHeader.e_res2[2]=0;
1179 ImageDosHeader.e_res2[3]=0;
1180 ImageDosHeader.e_res2[4]=0;
1181 ImageDosHeader.e_res2[5]=0;
1182 ImageDosHeader.e_res2[6]=0;
1183 ImageDosHeader.e_res2[7]=0;
1184 ImageDosHeader.e_res2[8]=0;
1185 ImageDosHeader.e_res2[9]=0;
1186 ImageDosHeader.e_lfanew=0x0100; //PEヘッダの位置
1187
1188
1189 /////////////////////////////////////////////
1190 // PEヘッダ
1191 /////////////////////////////////////////////
1192
1193 IMAGE_NT_HEADERS ImagePeHdr;
1194 ImagePeHdr.Signature=IMAGE_NT_SIGNATURE;
1195
1196 //マシンタイプ
1197 ImagePeHdr.FileHeader.Machine= IMAGE_FILE_MACHINE_I386;
1198
1199 ImagePeHdr.FileHeader.NumberOfSections= bUse_CodeSection+
1200 bUse_ExportSection+
1201 bUse_ImportSection+
1202 bUse_DataSection+
1203 bUse_RWSection+
1204 bUse_RSrcSection+
1205 bUse_RelocSection+
1206 bUse_DebugSection; //セクション数
1207 ImagePeHdr.FileHeader.TimeDateStamp= (DWORD)time(NULL);
1208 ImagePeHdr.FileHeader.PointerToSymbolTable= 0x00000000;
1209 ImagePeHdr.FileHeader.NumberOfSymbols= 0x00000000;
1210 ImagePeHdr.FileHeader.SizeOfOptionalHeader= IMAGE_SIZEOF_NT_OPTIONAL32_HEADER;
1211 if( compiler.IsDll() ){
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 IMAGE_FILE_DLL;
1217 }
1218 else{
1219 ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_EXECUTABLE_IMAGE|
1220 IMAGE_FILE_32BIT_MACHINE|
1221 IMAGE_FILE_LINE_NUMS_STRIPPED|
1222 IMAGE_FILE_LOCAL_SYMS_STRIPPED;
1223 }
1224
1225 ImagePeHdr.OptionalHeader.Magic= 0x010B;
1226 ImagePeHdr.OptionalHeader.MajorLinkerVersion= 4;
1227 ImagePeHdr.OptionalHeader.MinorLinkerVersion= 0;
1228 ImagePeHdr.OptionalHeader.SizeOfCode= FileSize_CodeSection; //コードサイズ(.textのセッションサイズ)
1229 ImagePeHdr.OptionalHeader.SizeOfInitializedData=FileSize_DataSection; //データサイズ(.dataのセッションサイズ)
1230 ImagePeHdr.OptionalHeader.SizeOfUninitializedData=0; //未初期化データのサイズ(なし)
1231 if( compiler.IsDll() ){
1232 if(DllMain_EntryPoint==-1)
1233 ImagePeHdr.OptionalHeader.AddressOfEntryPoint=0;
1234 else
1235 ImagePeHdr.OptionalHeader.AddressOfEntryPoint=MemPos_CodeSection+DllMain_EntryPoint;
1236 }
1237 else ImagePeHdr.OptionalHeader.AddressOfEntryPoint= MemPos_CodeSection;
1238 ImagePeHdr.OptionalHeader.BaseOfCode= MemPos_CodeSection; //.textのアドレス
1239 ImagePeHdr.OptionalHeader.BaseOfData= MemPos_DataSection; //.dataのアドレス
1240
1241 ImagePeHdr.OptionalHeader.ImageBase= ImageBase; //イメージベース
1242 ImagePeHdr.OptionalHeader.SectionAlignment= MEM_ALIGNMENT; //セクションアラインメント
1243 ImagePeHdr.OptionalHeader.FileAlignment= FILE_ALIGNMENT;
1244 ImagePeHdr.OptionalHeader.MajorOperatingSystemVersion=4;
1245 ImagePeHdr.OptionalHeader.MinorOperatingSystemVersion=0;
1246 ImagePeHdr.OptionalHeader.MajorImageVersion= 0;
1247 ImagePeHdr.OptionalHeader.MinorImageVersion= 0;
1248 ImagePeHdr.OptionalHeader.MajorSubsystemVersion=4;
1249 ImagePeHdr.OptionalHeader.MinorSubsystemVersion=0;
1250 ImagePeHdr.OptionalHeader.Win32VersionValue= 0;
1251 ImagePeHdr.OptionalHeader.SizeOfImage= EXE_HEADER_SIZE+
1252 MemSize_CodeSection+
1253 MemSize_ExportSection+
1254 MemSize_ImportSection+
1255 MemSize_DataSection+
1256 MemSize_RWSection+
1257 MemSize_RSrcSection+
1258 MemSize_RelocSection+
1259 MemSize_DebugSection;//すべてのイメージサイズ
1260 ImagePeHdr.OptionalHeader.SizeOfHeaders= EXE_HEADER_SIZE;//ヘッダサイズ
1261 ImagePeHdr.OptionalHeader.CheckSum= 0;
1262 extern unsigned short TypeOfSubSystem;
1263 ImagePeHdr.OptionalHeader.Subsystem= TypeOfSubSystem;
1264 ImagePeHdr.OptionalHeader.DllCharacteristics= 0;
1265 ImagePeHdr.OptionalHeader.SizeOfStackReserve= 0x00100000;
1266 ImagePeHdr.OptionalHeader.SizeOfStackCommit= 0x00001000;
1267 ImagePeHdr.OptionalHeader.SizeOfHeapReserve= 0x00100000;
1268 ImagePeHdr.OptionalHeader.SizeOfHeapCommit= 0x00001000;
1269 ImagePeHdr.OptionalHeader.LoaderFlags= 0;
1270 ImagePeHdr.OptionalHeader.NumberOfRvaAndSizes= IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1271
1272 //データ ディクショナリ
1273 ImagePeHdr.OptionalHeader.DataDirectory[0].VirtualAddress=MemPos_ExportSection;
1274 ImagePeHdr.OptionalHeader.DataDirectory[0].Size=FileSize_ExportSection;
1275 ImagePeHdr.OptionalHeader.DataDirectory[1].VirtualAddress=MemPos_ImportSection;//インポートテーブル
1276 ImagePeHdr.OptionalHeader.DataDirectory[1].Size=FileSize_ImportSection;
1277 ImagePeHdr.OptionalHeader.DataDirectory[2].VirtualAddress=MemPos_RSrcSection;
1278 ImagePeHdr.OptionalHeader.DataDirectory[2].Size=RSrcSectionSize;
1279 ImagePeHdr.OptionalHeader.DataDirectory[3].VirtualAddress=0;
1280 ImagePeHdr.OptionalHeader.DataDirectory[3].Size=0;
1281 ImagePeHdr.OptionalHeader.DataDirectory[4].VirtualAddress=0;
1282 ImagePeHdr.OptionalHeader.DataDirectory[4].Size=0;
1283 ImagePeHdr.OptionalHeader.DataDirectory[5].VirtualAddress=MemPos_RelocSection;
1284 ImagePeHdr.OptionalHeader.DataDirectory[5].Size=pobj_Reloc->length;
1285 ImagePeHdr.OptionalHeader.DataDirectory[6].VirtualAddress=0;
1286 ImagePeHdr.OptionalHeader.DataDirectory[6].Size=0;
1287 ImagePeHdr.OptionalHeader.DataDirectory[7].VirtualAddress=0;
1288 ImagePeHdr.OptionalHeader.DataDirectory[7].Size=0;
1289 ImagePeHdr.OptionalHeader.DataDirectory[8].VirtualAddress=0;
1290 ImagePeHdr.OptionalHeader.DataDirectory[8].Size=0;
1291 ImagePeHdr.OptionalHeader.DataDirectory[9].VirtualAddress=0;
1292 ImagePeHdr.OptionalHeader.DataDirectory[9].Size=0;
1293 ImagePeHdr.OptionalHeader.DataDirectory[10].VirtualAddress=0;
1294 ImagePeHdr.OptionalHeader.DataDirectory[10].Size=0;
1295 ImagePeHdr.OptionalHeader.DataDirectory[11].VirtualAddress=0;
1296 ImagePeHdr.OptionalHeader.DataDirectory[11].Size=0;
1297 ImagePeHdr.OptionalHeader.DataDirectory[12].VirtualAddress=MemPos_ImportSection+
1298 (ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+
1299 16*ImportDllNum+ //DLL名
1300 LookupSize+ //ルックアップテーブル
1301 HintSize; //ヒント名(関数名)テーブル
1302 ImagePeHdr.OptionalHeader.DataDirectory[12].Size=LookupSize;
1303 ImagePeHdr.OptionalHeader.DataDirectory[13].VirtualAddress=0;
1304 ImagePeHdr.OptionalHeader.DataDirectory[13].Size=0;
1305 ImagePeHdr.OptionalHeader.DataDirectory[14].VirtualAddress=0;
1306 ImagePeHdr.OptionalHeader.DataDirectory[14].Size=0;
1307 ImagePeHdr.OptionalHeader.DataDirectory[15].VirtualAddress=0;
1308 ImagePeHdr.OptionalHeader.DataDirectory[15].Size=0;
1309
1310
1311 //コードセクションヘッダ
1312 IMAGE_SECTION_HEADER CodeSectionHeader;
1313 memset((char *)CodeSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1314 lstrcpy((char *)CodeSectionHeader.Name,".text");
1315 CodeSectionHeader.Misc.VirtualSize= MemSize_CodeSection;
1316 CodeSectionHeader.VirtualAddress= MemPos_CodeSection; //開始アドレス
1317 CodeSectionHeader.SizeOfRawData= FileSize_CodeSection;
1318 CodeSectionHeader.PointerToRawData= FilePos_CodeSection; //ファイル上の開始アドレス
1319 CodeSectionHeader.PointerToRelocations= 0;
1320 CodeSectionHeader.PointerToLinenumbers= 0;
1321 CodeSectionHeader.NumberOfRelocations= 0;
1322 CodeSectionHeader.NumberOfLinenumbers= 0;
1323 CodeSectionHeader.Characteristics= IMAGE_SCN_MEM_EXECUTE|
1324 IMAGE_SCN_MEM_READ|
1325 IMAGE_SCN_CNT_CODE;
1326
1327 //エクスポートセクションヘッダ
1328 IMAGE_SECTION_HEADER ExportSectionHeader;
1329 memset((char *)ExportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1330 lstrcpy((char *)ExportSectionHeader.Name,".edata");
1331 ExportSectionHeader.Misc.VirtualSize= MemSize_ExportSection;
1332 ExportSectionHeader.VirtualAddress= MemPos_ExportSection; //開始アドレス
1333 ExportSectionHeader.SizeOfRawData= FileSize_ExportSection; //サイズ
1334 ExportSectionHeader.PointerToRawData= FilePos_ExportSection; //ファイル上の開始アドレス
1335 ExportSectionHeader.PointerToRelocations= 0;
1336 ExportSectionHeader.PointerToLinenumbers= 0;
1337 ExportSectionHeader.NumberOfRelocations= 0;
1338 ExportSectionHeader.NumberOfLinenumbers= 0;
1339 ExportSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1340 IMAGE_SCN_MEM_READ;
1341
1342 //インポートセクションヘッダ
1343 IMAGE_SECTION_HEADER ImportSectionHeader;
1344 memset((char *)ImportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1345 lstrcpy((char *)ImportSectionHeader.Name,".idata");
1346 ImportSectionHeader.Misc.VirtualSize= MemSize_ImportSection;
1347 ImportSectionHeader.VirtualAddress= MemPos_ImportSection; //開始アドレス
1348 ImportSectionHeader.SizeOfRawData= FileSize_ImportSection; //サイズ
1349 ImportSectionHeader.PointerToRawData= FilePos_ImportSection; //ファイル上の開始アドレス
1350 ImportSectionHeader.PointerToRelocations= 0;
1351 ImportSectionHeader.PointerToLinenumbers= 0;
1352 ImportSectionHeader.NumberOfRelocations= 0;
1353 ImportSectionHeader.NumberOfLinenumbers= 0;
1354 ImportSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1355 IMAGE_SCN_MEM_READ;
1356
1357 //データセクションヘッダ
1358 IMAGE_SECTION_HEADER DataSectionHeader;
1359 memset((char *)DataSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1360 lstrcpy((char *)DataSectionHeader.Name,".sdata");
1361 DataSectionHeader.Misc.VirtualSize= MemSize_DataSection;
1362 DataSectionHeader.VirtualAddress= MemPos_DataSection;
1363 DataSectionHeader.SizeOfRawData= FileSize_DataSection;
1364 DataSectionHeader.PointerToRawData= FilePos_DataSection;
1365 DataSectionHeader.PointerToRelocations= 0;
1366 DataSectionHeader.PointerToLinenumbers= 0;
1367 DataSectionHeader.NumberOfRelocations= 0;
1368 DataSectionHeader.NumberOfLinenumbers= 0;
1369 DataSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1370 IMAGE_SCN_MEM_READ|
1371 IMAGE_SCN_MEM_WRITE;
1372
1373 //リライタブルセクションヘッダ
1374 IMAGE_SECTION_HEADER RWSectionHeader;
1375 memset((char *)RWSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1376 lstrcpy((char *)RWSectionHeader.Name,".data");
1377 RWSectionHeader.Misc.VirtualSize= compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
1378 + compiler.GetObjectModule().meta.GetGlobalVars().GetAllSize();
1379 RWSectionHeader.VirtualAddress= MemPos_RWSection;
1380 RWSectionHeader.SizeOfRawData= FileSize_RWSection;
1381 RWSectionHeader.PointerToRawData= FilePos_RWSection;
1382 RWSectionHeader.PointerToRelocations= 0;
1383 RWSectionHeader.PointerToLinenumbers= 0;
1384 RWSectionHeader.NumberOfRelocations= 0;
1385 RWSectionHeader.NumberOfLinenumbers= 0;
1386 RWSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1387 IMAGE_SCN_MEM_READ|
1388 IMAGE_SCN_MEM_WRITE;
1389
1390 //リソースセクションヘッダ
1391 IMAGE_SECTION_HEADER RSrcSectionHeader;
1392 memset((char *)RSrcSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1393 lstrcpy((char *)RSrcSectionHeader.Name,".rsrc");
1394 RSrcSectionHeader.Misc.VirtualSize= RSrcSectionSize;
1395 RSrcSectionHeader.VirtualAddress= MemPos_RSrcSection;
1396 RSrcSectionHeader.SizeOfRawData= FileSize_RSrcSection;
1397 RSrcSectionHeader.PointerToRawData= FilePos_RSrcSection;
1398 RSrcSectionHeader.PointerToRelocations= 0;
1399 RSrcSectionHeader.PointerToLinenumbers= 0;
1400 RSrcSectionHeader.NumberOfRelocations= 0;
1401 RSrcSectionHeader.NumberOfLinenumbers= 0;
1402 RSrcSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1403 IMAGE_SCN_MEM_READ;
1404
1405 //リロケーションセクションヘッダ
1406 IMAGE_SECTION_HEADER RelocSectionHeader;
1407 memset((char *)RelocSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1408 lstrcpy((char *)RelocSectionHeader.Name,".reloc");
1409 RelocSectionHeader.Misc.VirtualSize= pobj_Reloc->length;
1410 RelocSectionHeader.VirtualAddress= MemPos_RelocSection; //開始アドレス
1411 RelocSectionHeader.SizeOfRawData= FileSize_RelocSection; //サイズ
1412 RelocSectionHeader.PointerToRawData= FilePos_RelocSection; //ファイル上の開始アドレス
1413 RelocSectionHeader.PointerToRelocations= 0;
1414 RelocSectionHeader.PointerToLinenumbers= 0;
1415 RelocSectionHeader.NumberOfRelocations= 0;
1416 RelocSectionHeader.NumberOfLinenumbers= 0;
1417 RelocSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1418 IMAGE_SCN_MEM_DISCARDABLE|
1419 IMAGE_SCN_MEM_READ;
1420
1421 //デバッグセクションヘッダ
1422 IMAGE_SECTION_HEADER DebugSectionHeader;
1423 memset((char *)DebugSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1424 lstrcpy((char *)DebugSectionHeader.Name,".debug");
1425 DebugSectionHeader.Misc.VirtualSize= pobj_DebugSection->length;
1426 DebugSectionHeader.VirtualAddress= MemPos_DebugSection; //開始アドレス
1427 DebugSectionHeader.SizeOfRawData= FileSize_DebugSection; //サイズ
1428 DebugSectionHeader.PointerToRawData= FilePos_DebugSection; //ファイル上の開始アドレス
1429 DebugSectionHeader.PointerToRelocations= 0;
1430 DebugSectionHeader.PointerToLinenumbers= 0;
1431 DebugSectionHeader.NumberOfRelocations= 0;
1432 DebugSectionHeader.NumberOfLinenumbers= 0;
1433 DebugSectionHeader.Characteristics= IMAGE_SCN_MEM_DISCARDABLE|
1434 IMAGE_SCN_MEM_READ;
1435
1436
1437 hFile=CreateFile(program.GetOutputFilePath().c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
1438 if(hFile==INVALID_HANDLE_VALUE){
1439 compiler.errorMessenger.Output(53,program.GetOutputFilePath().c_str(),-1);
1440 goto EndWriteOpcode;
1441 }
1442
1443 //ヘッダ
1444 WriteFile(hFile,(void *)&ImageDosHeader,sizeof(IMAGE_DOS_HEADER),(DWORD *)&i2,NULL);
1445 i=i2;
1446
1447 //Dosスタブ
1448 WriteFile(hFile,DosStubBuffer,DosStubSize,(DWORD *)&i2,NULL);
1449 i+=i2;
1450
1451 //0x0100までNULLを並べる
1452 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,0x0100-i);
1453 WriteFile(hFile,temp2,0x0100-i,(DWORD *)&i2,NULL);
1454 HeapDefaultFree(temp2);
1455 i+=i2;
1456
1457 //PEヘッダ
1458 WriteFile(hFile,&ImagePeHdr,sizeof(IMAGE_NT_HEADERS),(DWORD *)&i2,NULL);
1459 i+=i2;
1460
1461 //コード セクション ヘッダ
1462 WriteFile(hFile,&CodeSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1463 i+=i2;
1464
1465 if(bUse_ExportSection){
1466 //エクスポート セクション ヘッダ
1467 WriteFile(hFile,&ExportSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1468 i+=i2;
1469 }
1470 if(bUse_ImportSection){
1471 //インポート セクション ヘッダ
1472 WriteFile(hFile,&ImportSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1473 i+=i2;
1474 }
1475 if(bUse_DataSection){
1476 //データ セクション ヘッダ
1477 WriteFile(hFile,&DataSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1478 i+=i2;
1479 }
1480 if(bUse_RWSection){
1481 //リライタブルセクションヘッダ
1482 WriteFile(hFile,&RWSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1483 i+=i2;
1484 }
1485 if(bUse_RSrcSection){
1486 //リソースセクションヘッダ
1487 WriteFile(hFile,&RSrcSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1488 i+=i2;
1489 }
1490 if(bUse_RelocSection){
1491 //リロケーションセクションヘッダ
1492 WriteFile(hFile,&RelocSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1493 i+=i2;
1494 }
1495 if(bUse_DebugSection){
1496 //デバッグセクションヘッダ
1497 WriteFile(hFile,&DebugSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1498 i+=i2;
1499 }
1500
1501 //EXE_HEADER_SIZEまでNULLを並べる
1502 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,EXE_HEADER_SIZE-i);
1503 WriteFile(hFile,temp2,EXE_HEADER_SIZE-i,(DWORD *)&i2,NULL);
1504 HeapDefaultFree(temp2);
1505 i+=i2;
1506
1507 //コード
1508 WriteFile(
1509 hFile,
1510 compiler.linker.GetNativeCode().GetBuffer(),
1511 compiler.linker.GetNativeCode().GetSize(),
1512 (DWORD *)&i2,
1513 NULL
1514 );
1515 i+=i2;
1516
1517 //FilePos_ExportSectionまでNULLを並べる
1518 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_ExportSection-i);
1519 WriteFile(hFile,temp2,FilePos_ExportSection-i,(DWORD *)&i2,NULL);
1520 HeapDefaultFree(temp2);
1521 i+=i2;
1522
1523 if(bUse_ExportSection){
1524 //エクスポート ディレクトリ テーブル
1525 WriteFile(hFile,&ImageExportDirectory,sizeof(IMAGE_EXPORT_DIRECTORY),(DWORD *)&i2,NULL);
1526 i+=i2;
1527
1528 //エクスポート アドレス テーブル
1529 WriteFile(hFile,lpdwExportAddressTable,ExportNum*sizeof(DWORD),(DWORD *)&i2,NULL);
1530 i+=i2;
1531
1532 //エクスポート名ポインタ テーブル
1533 WriteFile(hFile,lpdwExportNamePointerTable,ExportNum*sizeof(DWORD),(DWORD *)&i2,NULL);
1534 i+=i2;
1535
1536 //エクスポート序数テーブル
1537 WriteFile(hFile,lpwExportOrdinalTable,ExportNum*sizeof(WORD),(DWORD *)&i2,NULL);
1538 i+=i2;
1539
1540 //シンボル名
1541 WriteFile(hFile,lpExportNames,ExportNamesLength,(DWORD *)&i2,NULL);
1542 i+=i2;
1543 }
1544
1545 //FilePos_ImportSectionまでNULLを並べる
1546 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_ImportSection-i);
1547 WriteFile(hFile,temp2,FilePos_ImportSection-i,(DWORD *)&i2,NULL);
1548 HeapDefaultFree(temp2);
1549 i+=i2;
1550
1551 if(bUse_ImportSection){
1552 //インポート ディレクトリ テーブル(Nullディレクトリ テーブルを含む)
1553 for(i3=0;i3<(ImportDllNum+1);i3++){
1554 WriteFile(hFile,&pImportTable[i3],sizeof(IMAGE_IMPORT_DESCRIPTOR),(DWORD *)&i2,NULL);
1555 i+=i2;
1556 }
1557
1558 //DLL名
1559 for(i3=0;i3<ImportDllNum;i3++){
1560 WriteFile(hFile,ppDllNames[i3],16,(DWORD *)&i2,NULL);
1561 i+=i2;
1562 }
1563
1564 //ルックアップ テーブル
1565 WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
1566 i+=i2;
1567
1568 //ヒント テーブル
1569 WriteFile(hFile,pHintTable,HintSize,(DWORD *)&i2,NULL);
1570 i+=i2;
1571
1572 //インポート アドレス テーブル
1573 WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
1574 i+=i2;
1575 }
1576
1577 //FilePos_DataSectionまでNULLを並べる
1578 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_DataSection-i);
1579 WriteFile(hFile,temp2,FilePos_DataSection-i,(DWORD *)&i2,NULL);
1580 HeapDefaultFree(temp2);
1581 i+=i2;
1582
1583 if(bUse_DataSection){
1584 //データ テーブル
1585 WriteFile(
1586 hFile,
1587 compiler.linker.GetDataTable().GetPtr(),
1588 compiler.linker.GetDataTable().GetSize(),
1589 (DWORD *)&i2,
1590 NULL
1591 );
1592 i+=i2;
1593 }
1594
1595 //FilePos_RWSectionまでNULLを並べる
1596 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RWSection-i);
1597 WriteFile(hFile,temp2,FilePos_RWSection-i,(DWORD *)&i2,NULL);
1598 HeapDefaultFree(temp2);
1599 i+=i2;
1600
1601 if(bUse_RWSection){
1602 //リライタブル データ テーブル(グローバル変数の初期バッファ)
1603
1604 char *temp = (char *)calloc( FileSize_RWSection, 1 );
1605 memcpy(
1606 temp,
1607 compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetBuffer(),
1608 compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
1609 );
1610
1611 WriteFile(hFile,temp,FileSize_RWSection,(DWORD *)&i2,NULL);
1612 i+=i2;
1613
1614 free( temp );
1615 }
1616
1617 //FilePos_RSrcSectionまでNULLを並べる
1618 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RSrcSection-i);
1619 WriteFile(hFile,temp2,FilePos_RSrcSection-i,(DWORD *)&i2,NULL);
1620 HeapDefaultFree(temp2);
1621 i+=i2;
1622
1623 if(bUse_RSrcSection){
1624 //リソースバッファ
1625 WriteFile(hFile,RSrcSectionBuffer,RSrcSectionSize,(DWORD *)&i2,NULL);
1626 i+=i2;
1627 }
1628
1629 //FilePos_RelocSectionまでNULLを並べる
1630 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RelocSection-i);
1631 WriteFile(hFile,temp2,FilePos_RelocSection-i,(DWORD *)&i2,NULL);
1632 HeapDefaultFree(temp2);
1633 i+=i2;
1634
1635 if(bUse_RelocSection){
1636 //リロケーション情報
1637 WriteFile(hFile,pobj_Reloc->buffer,pobj_Reloc->length,(DWORD *)&i2,NULL);
1638 i+=i2;
1639 }
1640
1641 //ファイルアラインメントを考慮
1642 if(i%FILE_ALIGNMENT){
1643 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
1644 WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
1645 HeapDefaultFree(temp2);
1646 i+=i2;
1647 }
1648
1649 if(bUse_DebugSection){
1650 //デバッグセクション
1651 WriteFile(hFile,pobj_DebugSection->buffer,pobj_DebugSection->length,(DWORD *)&i2,NULL);
1652 i+=i2;
1653 }
1654
1655 //ファイルアラインメントを考慮
1656 if(i%FILE_ALIGNMENT){
1657 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
1658 WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
1659 HeapDefaultFree(temp2);
1660 i+=i2;
1661 }
1662
1663 //書き込み終了
1664 CloseHandle(hFile);
1665
1666
1667EndWriteOpcode:
1668
1669 //Dosスタブ用のメモリを解放
1670 HeapDefaultFree(DosStubBuffer);
1671
1672 //エクスポート テーブル情報を解放
1673 HeapDefaultFree(lpdwExportAddressTable);
1674 HeapDefaultFree(lpdwExportNamePointerTable);
1675 HeapDefaultFree(lpwExportOrdinalTable);
1676
1677 //インポートDLL情報を解放
1678 HeapDefaultFree(pImportTable);
1679 for(i=0;i<ImportDllNum;i++)
1680 HeapDefaultFree(ppDllNames[i]);
1681 HeapDefaultFree(ppDllNames);
1682
1683 //ルックアップテーブルに関する情報を解放
1684 HeapDefaultFree(pLookupTable);
1685
1686 //ヒントテーブルに関する情報を解放
1687 HeapDefaultFree(pHintTable);
1688
1689 //リソースセクションバッファを解放
1690 HeapDefaultFree(RSrcSectionBuffer);
1691
1692 //デバッグセクションを開放
1693 delete pobj_DebugSection;
1694
1695 //リロケーション情報を解放
1696 delete pobj_Reloc;
1697}
Note: See TracBrowser for help on using the repository browser.