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

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