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

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