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

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