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

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