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

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

UserProc/DllProc/ProcPointerクラスをSymbolクラスからの派生にした

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