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

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