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

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