source: dev/trunk/ab5.0/abdev/BasicCompiler32/MakePeHdr.cpp@ 467

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

いくつかのグローバル変数をProgram/Debuggerクラスにまとめた。

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