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

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

Messenger/ErrorMessengerクラスを導入。SetError関数によるエラー生成を廃止した。

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