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

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