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

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

DllProc::pNextDataを排除した

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