source: dev/trunk/abdev/BasicCompiler32/MakePeHdr.cpp@ 308

Last change on this file since 308 was 308, checked in by dai_9181, 17 years ago

静的リンクライブラリにより、複数のグローバル領域が存在することになったのでそれぞれを関数ベースに分けた

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