source: dev/trunk/abdev/BasicCompiler64/MakePeHdr.cpp@ 254

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