source: dev/BasicCompiler32/MakePeHdr.cpp@ 140

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

traceログ機能を搭載
動的メンバをstl::vectorにまとめた
シンボルをクラス化した

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