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

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