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

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