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

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