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

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

vtbl構築をコード生成後(最終リンクの前)に行うようにした

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