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

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

MetaImplを廃止し、Metaにした。
ObjectModuleクラス、Linkerクラスを用意。

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