source: dev/BasicCompiler64/MakePeHdr.cpp@ 67

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

[32bit Compiler]op_push_value → op_push_V
Nothingに対応。

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