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

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