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

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