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

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

リンカの依存関係解決モジュールを製作中

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