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

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

ジェネリクスのベースを実装

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