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

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

Labelクラスをリファクタリング

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