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

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

Binaryクラスを追加

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