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

Last change on this file since 242 was 242, checked in by dai_9181, 17 years ago
File size: 57.4 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 obp_AllocSize=8192*2;
329 OpBuffer=(char *)calloc(obp_AllocSize,1);
330 obp=0;
331
332 //レジスタのブロッキングを管理するためのオブジェクトを生成
333 pobj_BlockReg=new CBlockReg;
334
335 //レキシカルスコープ情報を初期化
336 GetLexicalScopes().Init(obp);
337
338
339 /////////////////////////////////////////////////////////////////
340 // デバッグコンパイル用のログを生成する
341 /////////////////////////////////////////////////////////////////
342#ifdef _DEBUG
343 {
344 ofstream ofs("middle_code.txt");
345 ofs << basbuf << endl;
346 ofs.close();
347 }
348#endif
349
350
351 //////////////////////
352 // グローバル実行領域
353 //////////////////////
354
355 cp=-1;
356 UserProc::CompileStartForGlobalArea();
357
358 if(!bDll){
359 // 名前空間が初期化されているかをチェック
360 if( compiler.GetNamespaceSupporter().GetLivingNamespaceScopes().size() ){
361 SetError();
362 }
363
364 //ラベル用のメモリを確保
365 extern LABEL *pLabelNames;
366 extern int MaxLabelNum;
367 pLabelNames=(LABEL *)HeapAlloc(hHeap,0,1);
368 MaxLabelNum=0;
369
370 //Gotoラベルスケジュール
371 extern GOTOLABELSCHEDULE *pGotoLabelSchedule;
372 extern int GotoLabelScheduleNum;
373 pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapAlloc(hHeap,0,1);
374 GotoLabelScheduleNum=0;
375
376 //With情報のメモリを確保
377 extern WITHINFO WithInfo;
378 WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
379 WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
380 WithInfo.num=0;
381
382 //Continueアドレスを初期化
383 compiler.codeGenerator.ClearContinueArea();
384
385 //スタックフレーム管理用クラスを初期化
386 extern CStackFrame *pobj_sf;
387 pobj_sf=new CStackFrame();
388
389 // コード生成対象を選択
390 compiler.codeGenerator.Select( compiler.globalNativeCode );
391
392 trace_for_sourcecodestep( "★★★ グローバル領域のコンパイルを開始" );
393
394
395 //未完成
396 //OpBuffer[obp++]=(char)0xCC;
397
398 //sub rsp,スタックフレームサイズ
399 int StackFrameSchedule;
400 compiler.codeGenerator.op_sub_rsp(0xFFFFFFFF);
401 StackFrameSchedule=obp-sizeof(long);
402
403 if(bDebugCompile){
404 //デバッグ用の変数を定義
405 DebugVariable();
406 }
407
408 //GC用の変数を定義
409 InitGCVariables();
410
411 //_System_StartupProgramの呼び出し
412 compiler.codeGenerator.op_call(pSub_System_StartupProgram);
413
414 //クラスに属する静的メンバを定義
415 compiler.GetMeta().GetClasses().InitStaticMember();
416
417 //グローバル実行領域をコンパイル開始
418 CompileBuffer(0,0);
419
420 //ラベル用のメモリを解放
421 for(i=0;i<MaxLabelNum;i++){
422 if(pLabelNames[i].pName) HeapDefaultFree(pLabelNames[i].pName);
423 }
424 HeapDefaultFree(pLabelNames);
425
426 //Goto未知ラベルスケジュールを解放
427 for(i=0;i<GotoLabelScheduleNum;i++){
428 if(pGotoLabelSchedule[i].pName){
429 SetError(6,pGotoLabelSchedule[i].pName,pGotoLabelSchedule[i].now_cp);
430 HeapDefaultFree(pGotoLabelSchedule[i].pName);
431 }
432 else{
433 sprintf(temporary,"%d",pGotoLabelSchedule[i].line);
434 SetError(6,temporary,pGotoLabelSchedule[i].now_cp);
435 }
436 }
437 HeapDefaultFree(pGotoLabelSchedule);
438
439
440 ///////////////////////////////////////
441 // グローバル文字列変数の解放処理
442 ///////////////////////////////////////
443
444 //call _System_Call_Destructor_of_GlobalObject
445 extern const UserProc *pSub_System_Call_Destructor_of_GlobalObject;
446 compiler.codeGenerator.op_call(pSub_System_Call_Destructor_of_GlobalObject);
447
448
449 //add rsp,スタックフレームサイズ
450 int stackFrameSize = pobj_sf->GetFrameSize(0);
451 compiler.codeGenerator.op_add_RV( REG_RSP, stackFrameSize );
452
453 //xor rax,rax(raxを0に初期化する)
454 compiler.codeGenerator.op_zero_reg(REG_RAX);
455
456 //ret
457 compiler.codeGenerator.op_ret();
458
459 //グローバル実行領域のコードサイズ
460 extern int GlobalOpBufferSize;
461 GlobalOpBufferSize=obp;
462
463 //スタックフレームスケジュール(subコマンドに渡す値)
464 *((long *)(OpBuffer+StackFrameSchedule)) = stackFrameSize;
465
466 //スタックフレームスケジュールを実行
467 pobj_sf->RunningSchedule( stackFrameSize );
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.GetDataTable().GetSize()%FILE_ALIGNMENT) FileSize_DataSection=compiler.GetDataTable().GetSize()+(FILE_ALIGNMENT-compiler.GetDataTable().GetSize()%FILE_ALIGNMENT);
925 else FileSize_DataSection=compiler.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 // エクスポート情報の再配置
1060 ////////////////////////////
1061 if(bUse_ExportSection){
1062 for(i=0;i<ExportNum;i++){
1063 lpdwExportAddressTable[i]+=MemPos_CodeSection;
1064 lpdwExportNamePointerTable[i]+=MemPos_ExportSection;
1065 }
1066
1067 ImageExportDirectory.Name+= MemPos_ExportSection;
1068 ImageExportDirectory.AddressOfFunctions+= MemPos_ExportSection;
1069 ImageExportDirectory.AddressOfNames+= MemPos_ExportSection;
1070 ImageExportDirectory.AddressOfNameOrdinals+= MemPos_ExportSection;
1071 }
1072
1073
1074 ////////////////////////////
1075 // インポート情報の再配置
1076 ////////////////////////////
1077 for(i=0,i5=0;i<ImportDllNum;i++){
1078 //IMAGE_IMPORT_DESCRIPTOR
1079 pImportDescriptor[i].OriginalFirstThunk+=MemPos_ImportSection;
1080 pImportDescriptor[i].Name+=MemPos_ImportSection;
1081
1082 //インポート アドレス テーブル(ルックアップ テーブルを差し引く)
1083 pImportDescriptor[i].FirstThunk=pImportDescriptor[i].OriginalFirstThunk-
1084 LookupSize; //ルックアップテーブル
1085
1086 compiler.GetMeta().GetDllProcs().Iterator_Reset();
1087 while( compiler.GetMeta().GetDllProcs().Iterator_HasNext() )
1088 {
1089 const DllProc *pDllProc = compiler.GetMeta().GetDllProcs().Iterator_GetNext();
1090
1091 if( !pDllProc->IsUsing() ){
1092 pDllProc=pDllProc->pNextData;
1093 continue;
1094 }
1095
1096 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
1097 //ルックアップ テーブル
1098 pLookupTable[i5++]+=MemPos_ImportSection;
1099 }
1100 }
1101 i5++;
1102 }
1103
1104
1105 ////////////////////////////////////////
1106 //仮想関数データテーブルスケジュール
1107 compiler.GetMeta().GetClasses().ActionVtblSchedule(ImageBase,MemPos_CodeSection);
1108
1109
1110 //////////////////////////////
1111 // データテーブルスケジュール
1112 for(i=0;i<pobj_DataTableSchedule->num;i++){
1113 *((long *)(OpBuffer+pobj_DataTableSchedule->pObpValues[i]))+=ImageBase+MemPos_DataSection;
1114 }
1115 delete pobj_DataTableSchedule;
1116
1117
1118 //////////////////////////////////
1119 // インポートテーブルスケジュール(インポート アドレス テーブルを指し示させる)
1120 // ※x86はRVAからのオフセット。x64はRPI(実行中アドレス)からのオフセット
1121 for(i=0;i<pobj_ImportAddrSchedule->num;i++){
1122 const DllProc *pDllProc = pobj_ImportAddrSchedule->ppdi[i];
1123 *((long *)(OpBuffer+pobj_ImportAddrSchedule->pObpValues[i]))=
1124 MemPos_ImportSection+
1125 pDllProc->GetLookupAddress()
1126 -(MemPos_CodeSection+pobj_ImportAddrSchedule->pObpValues[i]+sizeof(long));
1127 }
1128 delete pobj_ImportAddrSchedule;
1129
1130
1131 ////////////////////////////////////
1132 // プロシージャポインタスケジュール
1133 for(i=0;i<pobj_SubAddrSchedule->num;i++){
1134 if(pobj_SubAddrSchedule->ppsi[i]->GetBeginOpAddress()==0
1135 &&pobj_SubAddrSchedule->ppsi[i]->GetEndOpAddress()==0){
1136 SetError(300,NULL,-1);
1137 }
1138
1139 if(pobj_SubAddrSchedule->pbCall[i]){
1140 *((long *)(OpBuffer+pobj_SubAddrSchedule->pObpValues[i]))=
1141 pobj_SubAddrSchedule->ppsi[i]->GetBeginOpAddress()-(pobj_SubAddrSchedule->pObpValues[i]+sizeof(long));
1142 }
1143 else{
1144 *((long *)(OpBuffer+pobj_SubAddrSchedule->pObpValues[i]))=
1145 pobj_SubAddrSchedule->ppsi[i]->GetBeginOpAddress()+ImageBase+MemPos_CodeSection;
1146 }
1147 }
1148 delete pobj_SubAddrSchedule;
1149
1150
1151 if(bDll){
1152 //DLLの場合はリロケーション情報を生成
1153 pobj_Reloc->ResetRelocBuffer();
1154 }
1155
1156
1157
1158 //////////////////////////////////////
1159 // グローバル変数アドレススケジュール
1160 //////////////////////////////////////
1161
1162 /*
1163 GlobalArea1 - 初期バッファあり
1164 GlobalArea2 - 初期バッファなし
1165 */
1166 for(i=0;i<pobj_GlobalVarSchedule->num;i++){
1167 if(*((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i])) & 0x80000000){
1168 //初期バッファなし
1169 *((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i]))=
1170 AllInitGlobalVarSize+
1171 (*((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i])) & 0x7FFFFFFF)+
1172 ImageBase+MemPos_RWSection;
1173 }
1174 else{
1175 //初期バッファあり
1176 *((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i]))+=
1177 ImageBase+MemPos_RWSection;
1178 }
1179 }
1180 delete pobj_GlobalVarSchedule;
1181
1182
1183 ////////////////////////////////
1184 // リソースアドレススケジュール
1185 extern DWORD *lpdwRSrcAddrSchedule;
1186 extern int RSrcAddrScheduleNum;
1187 for(i=0;i<RSrcAddrScheduleNum;i++){
1188 *(DWORD *)(RSrcSectionBuffer+lpdwRSrcAddrSchedule[i])+=MemPos_RSrcSection;
1189 }
1190 HeapDefaultFree(lpdwRSrcAddrSchedule);
1191
1192
1193 //Dosスタブ
1194 char *DosStubBuffer;
1195 int DosStubSize;
1196 hFile=CreateFile(
1197 ( Jenga::Common::Environment::GetAppDir() + "\\SubOperation\\dosstub.pgm" ).c_str(),
1198 GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
1199 if(hFile==INVALID_HANDLE_VALUE){
1200 MessageBox(hOwnerEditor,"dosstub.pgmの読み込みに失敗","error",MB_OK);
1201 goto EndWriteOpcode;
1202 }
1203 DosStubSize=GetFileSize(hFile,NULL);
1204 DosStubBuffer=(char *)HeapAlloc(hHeap,0,DosStubSize);
1205 ReadFile(hFile,DosStubBuffer,DosStubSize,(DWORD *)&i2,NULL);
1206 CloseHandle(hFile);
1207
1208
1209 extern BOOL bError;
1210 if(bError) goto EndWriteOpcode;
1211
1212
1213 ////////////////////////////
1214 // EXEファイルのヘッダ情報
1215 ////////////////////////////
1216
1217 IMAGE_DOS_HEADER ImageDosHeader;
1218 ImageDosHeader.e_magic= 0x5A4D;
1219 ImageDosHeader.e_cblp= 0x0090;
1220 ImageDosHeader.e_cp= 0x0003;
1221 ImageDosHeader.e_crlc= 0;
1222 ImageDosHeader.e_cparhdr=4;
1223 ImageDosHeader.e_minalloc=0x0000;
1224 ImageDosHeader.e_maxalloc=0xFFFF;
1225 ImageDosHeader.e_ss= 0x0000;
1226 ImageDosHeader.e_sp= 0x00B8;
1227 ImageDosHeader.e_csum= 0x0000;
1228 ImageDosHeader.e_ip= 0x0000;
1229 ImageDosHeader.e_cs= 0x0000;
1230 ImageDosHeader.e_lfarlc=0x0040;
1231 ImageDosHeader.e_ovno= 0x0000;
1232 ImageDosHeader.e_res[0]=0;
1233 ImageDosHeader.e_res[1]=0;
1234 ImageDosHeader.e_res[2]=0;
1235 ImageDosHeader.e_res[3]=0;
1236 ImageDosHeader.e_oemid= 0x0000;
1237 ImageDosHeader.e_oeminfo=0x0000;
1238 ImageDosHeader.e_res2[0]=0;
1239 ImageDosHeader.e_res2[1]=0;
1240 ImageDosHeader.e_res2[2]=0;
1241 ImageDosHeader.e_res2[3]=0;
1242 ImageDosHeader.e_res2[4]=0;
1243 ImageDosHeader.e_res2[5]=0;
1244 ImageDosHeader.e_res2[6]=0;
1245 ImageDosHeader.e_res2[7]=0;
1246 ImageDosHeader.e_res2[8]=0;
1247 ImageDosHeader.e_res2[9]=0;
1248 ImageDosHeader.e_lfanew=0x0100; //PEヘッダの位置
1249
1250
1251 /////////////////////////////////////////////
1252 // PEヘッダ
1253 /////////////////////////////////////////////
1254
1255 IMAGE_NT_HEADERS64 ImagePeHdr;
1256 ImagePeHdr.Signature=IMAGE_NT_SIGNATURE;
1257
1258 //マシンタイプ
1259 ImagePeHdr.FileHeader.Machine= IMAGE_FILE_MACHINE_AMD64;
1260
1261 ImagePeHdr.FileHeader.NumberOfSections= bUse_CodeSection+
1262 bUse_ExportSection+
1263 bUse_ImportSection+
1264 bUse_DataSection+
1265 bUse_RWSection+
1266 bUse_RSrcSection+
1267 bUse_RelocSection+
1268 bUse_DebugSection; //セクション数
1269 ImagePeHdr.FileHeader.TimeDateStamp= (DWORD)time(NULL);
1270 ImagePeHdr.FileHeader.PointerToSymbolTable= 0x00000000;
1271 ImagePeHdr.FileHeader.NumberOfSymbols= 0x00000000;
1272 ImagePeHdr.FileHeader.SizeOfOptionalHeader= IMAGE_SIZEOF_NT_OPTIONAL64_HEADER;
1273 if(bDll){
1274 ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_EXECUTABLE_IMAGE|
1275 IMAGE_FILE_DLL|
1276 IMAGE_FILE_LARGE_ADDRESS_AWARE;
1277 }
1278 else{
1279 ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_RELOCS_STRIPPED|
1280 IMAGE_FILE_EXECUTABLE_IMAGE|
1281 IMAGE_FILE_LARGE_ADDRESS_AWARE;
1282 }
1283
1284 ImagePeHdr.OptionalHeader.Magic= IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1285 ImagePeHdr.OptionalHeader.MajorLinkerVersion= 5;
1286 ImagePeHdr.OptionalHeader.MinorLinkerVersion= 0;
1287 ImagePeHdr.OptionalHeader.SizeOfCode= FileSize_CodeSection; //コードサイズ(.textのセッションサイズ)
1288 ImagePeHdr.OptionalHeader.SizeOfInitializedData=FileSize_DataSection; //データサイズ(.dataのセッションサイズ)
1289 ImagePeHdr.OptionalHeader.SizeOfUninitializedData=0; //未初期化データのサイズ(なし)
1290 if(bDll){
1291 if(DllMain_EntryPoint==-1)
1292 ImagePeHdr.OptionalHeader.AddressOfEntryPoint=0;
1293 else
1294 ImagePeHdr.OptionalHeader.AddressOfEntryPoint=MemPos_CodeSection+DllMain_EntryPoint;
1295 }
1296 else ImagePeHdr.OptionalHeader.AddressOfEntryPoint= MemPos_CodeSection;
1297 ImagePeHdr.OptionalHeader.BaseOfCode= MemPos_CodeSection; //.textのアドレス
1298
1299 ImagePeHdr.OptionalHeader.ImageBase= ImageBase; //イメージベース
1300 ImagePeHdr.OptionalHeader.SectionAlignment= MEM_ALIGNMENT; //セクションアラインメント
1301 ImagePeHdr.OptionalHeader.FileAlignment= FILE_ALIGNMENT;
1302 ImagePeHdr.OptionalHeader.MajorOperatingSystemVersion=4;
1303 ImagePeHdr.OptionalHeader.MinorOperatingSystemVersion=0;
1304 ImagePeHdr.OptionalHeader.MajorImageVersion= 0;
1305 ImagePeHdr.OptionalHeader.MinorImageVersion= 0;
1306 ImagePeHdr.OptionalHeader.MajorSubsystemVersion=4;
1307 ImagePeHdr.OptionalHeader.MinorSubsystemVersion=0;
1308 ImagePeHdr.OptionalHeader.Win32VersionValue= 0;
1309 ImagePeHdr.OptionalHeader.SizeOfImage= EXE_HEADER_SIZE+
1310 MemSize_CodeSection+
1311 MemSize_ExportSection+
1312 MemSize_ImportSection+
1313 MemSize_DataSection+
1314 MemSize_RWSection+
1315 MemSize_RSrcSection+
1316 MemSize_RelocSection+
1317 MemSize_DebugSection;//すべてのイメージサイズ
1318 ImagePeHdr.OptionalHeader.SizeOfHeaders= EXE_HEADER_SIZE;//ヘッダサイズ
1319 ImagePeHdr.OptionalHeader.CheckSum= 0;
1320 extern unsigned short TypeOfSubSystem;
1321 ImagePeHdr.OptionalHeader.Subsystem= TypeOfSubSystem;
1322 ImagePeHdr.OptionalHeader.DllCharacteristics= 0;
1323 ImagePeHdr.OptionalHeader.SizeOfStackReserve= 0x00100000;
1324 ImagePeHdr.OptionalHeader.SizeOfStackCommit= 0x00001000;
1325 ImagePeHdr.OptionalHeader.SizeOfHeapReserve= 0x00100000;
1326 ImagePeHdr.OptionalHeader.SizeOfHeapCommit= 0x00001000;
1327 ImagePeHdr.OptionalHeader.LoaderFlags= 0;
1328 ImagePeHdr.OptionalHeader.NumberOfRvaAndSizes= IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1329
1330 //データ ディクショナリ
1331 ImagePeHdr.OptionalHeader.DataDirectory[0].VirtualAddress=MemPos_ExportSection;
1332 ImagePeHdr.OptionalHeader.DataDirectory[0].Size=FileSize_ExportSection;
1333 ImagePeHdr.OptionalHeader.DataDirectory[1].VirtualAddress= //IMAGE_IMPORT_DESCRIPTORを指す
1334 MemPos_ImportSection+
1335 LookupSize+ //インポート アドレス テーブル
1336 LookupSize; //ルックアップ テーブル
1337 ImagePeHdr.OptionalHeader.DataDirectory[1].Size=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR);
1338 ImagePeHdr.OptionalHeader.DataDirectory[2].VirtualAddress=MemPos_RSrcSection;
1339 ImagePeHdr.OptionalHeader.DataDirectory[2].Size=RSrcSectionSize;
1340 ImagePeHdr.OptionalHeader.DataDirectory[3].VirtualAddress=0;
1341 ImagePeHdr.OptionalHeader.DataDirectory[3].Size=0;
1342 ImagePeHdr.OptionalHeader.DataDirectory[4].VirtualAddress=0;
1343 ImagePeHdr.OptionalHeader.DataDirectory[4].Size=0;
1344 ImagePeHdr.OptionalHeader.DataDirectory[5].VirtualAddress=MemPos_RelocSection;
1345 ImagePeHdr.OptionalHeader.DataDirectory[5].Size=pobj_Reloc->length;
1346 ImagePeHdr.OptionalHeader.DataDirectory[6].VirtualAddress=0;
1347 ImagePeHdr.OptionalHeader.DataDirectory[6].Size=0;
1348 ImagePeHdr.OptionalHeader.DataDirectory[7].VirtualAddress=0;
1349 ImagePeHdr.OptionalHeader.DataDirectory[7].Size=0;
1350 ImagePeHdr.OptionalHeader.DataDirectory[8].VirtualAddress=0;
1351 ImagePeHdr.OptionalHeader.DataDirectory[8].Size=0;
1352 ImagePeHdr.OptionalHeader.DataDirectory[9].VirtualAddress=0;
1353 ImagePeHdr.OptionalHeader.DataDirectory[9].Size=0;
1354 ImagePeHdr.OptionalHeader.DataDirectory[10].VirtualAddress=0;
1355 ImagePeHdr.OptionalHeader.DataDirectory[10].Size=0;
1356 ImagePeHdr.OptionalHeader.DataDirectory[11].VirtualAddress=0;
1357 ImagePeHdr.OptionalHeader.DataDirectory[11].Size=0;
1358 ImagePeHdr.OptionalHeader.DataDirectory[12].VirtualAddress=MemPos_ImportSection;//インポート アドレス テーブル
1359 ImagePeHdr.OptionalHeader.DataDirectory[12].Size=LookupSize;
1360 ImagePeHdr.OptionalHeader.DataDirectory[13].VirtualAddress=0;
1361 ImagePeHdr.OptionalHeader.DataDirectory[13].Size=0;
1362 ImagePeHdr.OptionalHeader.DataDirectory[14].VirtualAddress=0;
1363 ImagePeHdr.OptionalHeader.DataDirectory[14].Size=0;
1364 ImagePeHdr.OptionalHeader.DataDirectory[15].VirtualAddress=0;
1365 ImagePeHdr.OptionalHeader.DataDirectory[15].Size=0;
1366
1367
1368 //コードセクションヘッダ
1369 IMAGE_SECTION_HEADER CodeSectionHeader;
1370 memset((char *)CodeSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1371 lstrcpy((char *)CodeSectionHeader.Name,".text");
1372 CodeSectionHeader.Misc.VirtualSize= obp;
1373 CodeSectionHeader.VirtualAddress= MemPos_CodeSection; //開始アドレス
1374 CodeSectionHeader.SizeOfRawData= FileSize_CodeSection;
1375 CodeSectionHeader.PointerToRawData= FilePos_CodeSection; //ファイル上の開始アドレス
1376 CodeSectionHeader.PointerToRelocations= 0;
1377 CodeSectionHeader.PointerToLinenumbers= 0;
1378 CodeSectionHeader.NumberOfRelocations= 0;
1379 CodeSectionHeader.NumberOfLinenumbers= 0;
1380 CodeSectionHeader.Characteristics= IMAGE_SCN_MEM_EXECUTE|
1381 IMAGE_SCN_MEM_READ|
1382 IMAGE_SCN_CNT_CODE;
1383
1384 //エクスポートセクションヘッダ
1385 IMAGE_SECTION_HEADER ExportSectionHeader;
1386 memset((char *)ExportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1387 lstrcpy((char *)ExportSectionHeader.Name,".edata");
1388 ExportSectionHeader.Misc.VirtualSize= MemSize_ExportSection;
1389 ExportSectionHeader.VirtualAddress= MemPos_ExportSection; //開始アドレス
1390 ExportSectionHeader.SizeOfRawData= FileSize_ExportSection; //サイズ
1391 ExportSectionHeader.PointerToRawData= FilePos_ExportSection; //ファイル上の開始アドレス
1392 ExportSectionHeader.PointerToRelocations= 0;
1393 ExportSectionHeader.PointerToLinenumbers= 0;
1394 ExportSectionHeader.NumberOfRelocations= 0;
1395 ExportSectionHeader.NumberOfLinenumbers= 0;
1396 ExportSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1397 IMAGE_SCN_MEM_READ;
1398
1399 //インポートセクションヘッダ
1400 IMAGE_SECTION_HEADER ImportSectionHeader;
1401 memset((char *)ImportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1402 lstrcpy((char *)ImportSectionHeader.Name,".idata");
1403 ImportSectionHeader.Misc.VirtualSize=
1404 LookupSize+ //インポートアドレステーブル
1405 LookupSize+ //ルックアップテーブル
1406 (ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+
1407 16*ImportDllNum+ //DLL名
1408 HintSize; //ヒント名(関数名)テーブル
1409 ImportSectionHeader.VirtualAddress= MemPos_ImportSection; //開始アドレス
1410 ImportSectionHeader.SizeOfRawData= FileSize_ImportSection; //サイズ
1411 ImportSectionHeader.PointerToRawData= FilePos_ImportSection; //ファイル上の開始アドレス
1412 ImportSectionHeader.PointerToRelocations= 0;
1413 ImportSectionHeader.PointerToLinenumbers= 0;
1414 ImportSectionHeader.NumberOfRelocations= 0;
1415 ImportSectionHeader.NumberOfLinenumbers= 0;
1416 ImportSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1417 IMAGE_SCN_MEM_READ;
1418
1419 //データセクションヘッダ
1420 IMAGE_SECTION_HEADER DataSectionHeader;
1421 memset((char *)DataSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1422 lstrcpy((char *)DataSectionHeader.Name,".sdata");
1423 DataSectionHeader.Misc.VirtualSize= MemSize_DataSection;
1424 DataSectionHeader.VirtualAddress= MemPos_DataSection;
1425 DataSectionHeader.SizeOfRawData= FileSize_DataSection;
1426 DataSectionHeader.PointerToRawData= FilePos_DataSection;
1427 DataSectionHeader.PointerToRelocations= 0;
1428 DataSectionHeader.PointerToLinenumbers= 0;
1429 DataSectionHeader.NumberOfRelocations= 0;
1430 DataSectionHeader.NumberOfLinenumbers= 0;
1431 DataSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1432 IMAGE_SCN_MEM_READ|
1433 IMAGE_SCN_MEM_WRITE;
1434
1435 //リライタブルセクションヘッダ
1436 IMAGE_SECTION_HEADER RWSectionHeader;
1437 memset((char *)RWSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1438 lstrcpy((char *)RWSectionHeader.Name,".data");
1439 RWSectionHeader.Misc.VirtualSize= AllInitGlobalVarSize+AllGlobalVarSize;
1440 RWSectionHeader.VirtualAddress= MemPos_RWSection;
1441 RWSectionHeader.SizeOfRawData= FileSize_RWSection;
1442 RWSectionHeader.PointerToRawData= FilePos_RWSection;
1443 RWSectionHeader.PointerToRelocations= 0;
1444 RWSectionHeader.PointerToLinenumbers= 0;
1445 RWSectionHeader.NumberOfRelocations= 0;
1446 RWSectionHeader.NumberOfLinenumbers= 0;
1447 RWSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1448 IMAGE_SCN_MEM_READ|
1449 IMAGE_SCN_MEM_WRITE;
1450
1451 //リソースセクションヘッダ
1452 IMAGE_SECTION_HEADER RSrcSectionHeader;
1453 memset((char *)RSrcSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1454 lstrcpy((char *)RSrcSectionHeader.Name,".rsrc");
1455 RSrcSectionHeader.Misc.VirtualSize= RSrcSectionSize;
1456 RSrcSectionHeader.VirtualAddress= MemPos_RSrcSection;
1457 RSrcSectionHeader.SizeOfRawData= FileSize_RSrcSection;
1458 RSrcSectionHeader.PointerToRawData= FilePos_RSrcSection;
1459 RSrcSectionHeader.PointerToRelocations= 0;
1460 RSrcSectionHeader.PointerToLinenumbers= 0;
1461 RSrcSectionHeader.NumberOfRelocations= 0;
1462 RSrcSectionHeader.NumberOfLinenumbers= 0;
1463 RSrcSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1464 IMAGE_SCN_MEM_READ;
1465
1466 //リロケーションセクションヘッダ
1467 IMAGE_SECTION_HEADER RelocSectionHeader;
1468 memset((char *)RelocSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1469 lstrcpy((char *)RelocSectionHeader.Name,".reloc");
1470 RelocSectionHeader.Misc.VirtualSize= pobj_Reloc->length;
1471 RelocSectionHeader.VirtualAddress= MemPos_RelocSection; //開始アドレス
1472 RelocSectionHeader.SizeOfRawData= FileSize_RelocSection; //サイズ
1473 RelocSectionHeader.PointerToRawData= FilePos_RelocSection; //ファイル上の開始アドレス
1474 RelocSectionHeader.PointerToRelocations= 0;
1475 RelocSectionHeader.PointerToLinenumbers= 0;
1476 RelocSectionHeader.NumberOfRelocations= 0;
1477 RelocSectionHeader.NumberOfLinenumbers= 0;
1478 RelocSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1479 IMAGE_SCN_MEM_DISCARDABLE|
1480 IMAGE_SCN_MEM_READ;
1481
1482 //デバッグセクションヘッダ
1483 IMAGE_SECTION_HEADER DebugSectionHeader;
1484 memset((char *)DebugSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1485 lstrcpy((char *)DebugSectionHeader.Name,".debug");
1486 DebugSectionHeader.Misc.VirtualSize= pobj_DebugSection->length;
1487 DebugSectionHeader.VirtualAddress= MemPos_DebugSection; //開始アドレス
1488 DebugSectionHeader.SizeOfRawData= FileSize_DebugSection; //サイズ
1489 DebugSectionHeader.PointerToRawData= FilePos_DebugSection; //ファイル上の開始アドレス
1490 DebugSectionHeader.PointerToRelocations= 0;
1491 DebugSectionHeader.PointerToLinenumbers= 0;
1492 DebugSectionHeader.NumberOfRelocations= 0;
1493 DebugSectionHeader.NumberOfLinenumbers= 0;
1494 DebugSectionHeader.Characteristics= IMAGE_SCN_MEM_DISCARDABLE|
1495 IMAGE_SCN_MEM_READ;
1496
1497
1498 hFile=CreateFile(OutputFileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
1499 if(hFile==INVALID_HANDLE_VALUE){
1500 SetError(53,OutputFileName,-1);
1501 goto EndWriteOpcode;
1502 }
1503
1504 //ヘッダ
1505 WriteFile(hFile,(void *)&ImageDosHeader,sizeof(IMAGE_DOS_HEADER),(DWORD *)&i2,NULL);
1506 i=i2;
1507
1508 //Dosスタブ
1509 WriteFile(hFile,DosStubBuffer,DosStubSize,(DWORD *)&i2,NULL);
1510 i+=i2;
1511
1512 //0x0100までNULLを並べる
1513 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,0x0100-i);
1514 WriteFile(hFile,temp2,0x0100-i,(DWORD *)&i2,NULL);
1515 HeapDefaultFree(temp2);
1516 i+=i2;
1517
1518 //PEヘッダ
1519 WriteFile(hFile,&ImagePeHdr,sizeof(IMAGE_NT_HEADERS64),(DWORD *)&i2,NULL);
1520 i+=i2;
1521
1522 //コード セクション ヘッダ
1523 WriteFile(hFile,&CodeSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1524 i+=i2;
1525
1526 if(bUse_ExportSection){
1527 //エクスポート セクション ヘッダ
1528 WriteFile(hFile,&ExportSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1529 i+=i2;
1530 }
1531 if(bUse_ImportSection){
1532 //インポート セクション ヘッダ
1533 WriteFile(hFile,&ImportSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1534 i+=i2;
1535 }
1536 if(bUse_DataSection){
1537 //データ セクション ヘッダ
1538 WriteFile(hFile,&DataSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1539 i+=i2;
1540 }
1541 if(bUse_RWSection){
1542 //リライタブルセクションヘッダ
1543 WriteFile(hFile,&RWSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1544 i+=i2;
1545 }
1546 if(bUse_RSrcSection){
1547 //リソースセクションヘッダ
1548 WriteFile(hFile,&RSrcSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1549 i+=i2;
1550 }
1551 if(bUse_RelocSection){
1552 //リロケーションセクションヘッダ
1553 WriteFile(hFile,&RelocSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1554 i+=i2;
1555 }
1556 if(bUse_DebugSection){
1557 //デバッグセクションヘッダ
1558 WriteFile(hFile,&DebugSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1559 i+=i2;
1560 }
1561
1562 //EXE_HEADER_SIZEまでNULLを並べる
1563 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,EXE_HEADER_SIZE-i);
1564 WriteFile(hFile,temp2,EXE_HEADER_SIZE-i,(DWORD *)&i2,NULL);
1565 HeapDefaultFree(temp2);
1566 i+=i2;
1567
1568 //コード
1569 WriteFile(hFile,OpBuffer,obp,(DWORD *)&i2,NULL);
1570 i+=i2;
1571
1572 //FilePos_ExportSectionまでNULLを並べる
1573 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_ExportSection-i);
1574 WriteFile(hFile,temp2,FilePos_ExportSection-i,(DWORD *)&i2,NULL);
1575 HeapDefaultFree(temp2);
1576 i+=i2;
1577
1578 if(bUse_ExportSection){
1579 //エクスポート ディレクトリ テーブル
1580 WriteFile(hFile,&ImageExportDirectory,sizeof(IMAGE_EXPORT_DIRECTORY),(DWORD *)&i2,NULL);
1581 i+=i2;
1582
1583 //エクスポート アドレス テーブル
1584 WriteFile(hFile,lpdwExportAddressTable,ExportNum*sizeof(DWORD),(DWORD *)&i2,NULL);
1585 i+=i2;
1586
1587 //エクスポート名ポインタ テーブル
1588 WriteFile(hFile,lpdwExportNamePointerTable,ExportNum*sizeof(DWORD),(DWORD *)&i2,NULL);
1589 i+=i2;
1590
1591 //エクスポート序数テーブル
1592 WriteFile(hFile,lpwExportOrdinalTable,ExportNum*sizeof(WORD),(DWORD *)&i2,NULL);
1593 i+=i2;
1594
1595 //シンボル名
1596 WriteFile(hFile,lpExportNames,ExportNamesLength,(DWORD *)&i2,NULL);
1597 i+=i2;
1598 }
1599
1600 //FilePos_ImportSectionまでNULLを並べる
1601 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_ImportSection-i);
1602 WriteFile(hFile,temp2,FilePos_ImportSection-i,(DWORD *)&i2,NULL);
1603 HeapDefaultFree(temp2);
1604 i+=i2;
1605
1606 if(bUse_ImportSection){
1607 //インポート アドレス テーブル
1608 WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
1609 i+=i2;
1610
1611 //ルックアップ テーブル
1612 WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
1613 i+=i2;
1614
1615 //インポート ディレクトリ テーブル(Nullディレクトリ テーブルを含む)
1616 for(i3=0;i3<(ImportDllNum+1);i3++){
1617 WriteFile(hFile,&pImportDescriptor[i3],sizeof(IMAGE_IMPORT_DESCRIPTOR),(DWORD *)&i2,NULL);
1618 i+=i2;
1619 }
1620
1621 //DLL名
1622 for(i3=0;i3<ImportDllNum;i3++){
1623 WriteFile(hFile,ppDllNames[i3],16,(DWORD *)&i2,NULL);
1624 i+=i2;
1625 }
1626
1627 //ヒント テーブル
1628 WriteFile(hFile,pHintTable,HintSize,(DWORD *)&i2,NULL);
1629 i+=i2;
1630 }
1631
1632 //FilePos_DataSectionまでNULLを並べる
1633 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_DataSection-i);
1634 WriteFile(hFile,temp2,FilePos_DataSection-i,(DWORD *)&i2,NULL);
1635 HeapDefaultFree(temp2);
1636 i+=i2;
1637
1638 if(bUse_DataSection){
1639 //データ テーブル
1640 WriteFile(hFile,compiler.GetDataTable().GetPtr(),compiler.GetDataTable().GetSize(),(DWORD *)&i2,NULL);
1641 i+=i2;
1642 }
1643
1644 //FilePos_RWSectionまでNULLを並べる
1645 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RWSection-i);
1646 WriteFile(hFile,temp2,FilePos_RWSection-i,(DWORD *)&i2,NULL);
1647 HeapDefaultFree(temp2);
1648 i+=i2;
1649
1650 if(bUse_RWSection){
1651 //リライタブル データ テーブル(グローバル変数の初期バッファ)
1652 initGlobalBuf=(BYTE *)HeapReAlloc(hHeap,
1653 HEAP_ZERO_MEMORY,
1654 initGlobalBuf,
1655 FileSize_RWSection);
1656 WriteFile(hFile,initGlobalBuf,FileSize_RWSection,(DWORD *)&i2,NULL);
1657 i+=i2;
1658 }
1659
1660 //FilePos_RSrcSectionまでNULLを並べる
1661 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RSrcSection-i);
1662 WriteFile(hFile,temp2,FilePos_RSrcSection-i,(DWORD *)&i2,NULL);
1663 HeapDefaultFree(temp2);
1664 i+=i2;
1665
1666 if(bUse_RSrcSection){
1667 //リソースバッファ
1668 WriteFile(hFile,RSrcSectionBuffer,RSrcSectionSize,(DWORD *)&i2,NULL);
1669 i+=i2;
1670 }
1671
1672 //FilePos_RelocSectionまでNULLを並べる
1673 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RelocSection-i);
1674 WriteFile(hFile,temp2,FilePos_RelocSection-i,(DWORD *)&i2,NULL);
1675 HeapDefaultFree(temp2);
1676 i+=i2;
1677
1678 if(bUse_RelocSection){
1679 //リロケーション情報
1680 WriteFile(hFile,pobj_Reloc->buffer,pobj_Reloc->length,(DWORD *)&i2,NULL);
1681 i+=i2;
1682 }
1683
1684 //ファイルアラインメントを考慮
1685 if(i%FILE_ALIGNMENT){
1686 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
1687 WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
1688 HeapDefaultFree(temp2);
1689 i+=i2;
1690 }
1691
1692 if(bUse_DebugSection){
1693 //デバッグセクション
1694 WriteFile(hFile,pobj_DebugSection->buffer,pobj_DebugSection->length,(DWORD *)&i2,NULL);
1695 i+=i2;
1696 }
1697
1698 //ファイルアラインメントを考慮
1699 if(i%FILE_ALIGNMENT){
1700 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
1701 WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
1702 HeapDefaultFree(temp2);
1703 i+=i2;
1704 }
1705
1706 //書き込み終了
1707 CloseHandle(hFile);
1708
1709
1710EndWriteOpcode:
1711
1712 //Dosスタブ用のメモリを解放
1713 HeapDefaultFree(DosStubBuffer);
1714
1715 //エクスポート テーブル情報を解放
1716 HeapDefaultFree(lpdwExportAddressTable);
1717 HeapDefaultFree(lpdwExportNamePointerTable);
1718 HeapDefaultFree(lpwExportOrdinalTable);
1719
1720 //インポートDLL情報を解放
1721 HeapDefaultFree(pImportDescriptor);
1722 for(i=0;i<ImportDllNum;i++)
1723 HeapDefaultFree(ppDllNames[i]);
1724 HeapDefaultFree(ppDllNames);
1725
1726 //ルックアップテーブルに関する情報を解放
1727 HeapDefaultFree(pLookupTable);
1728
1729 //ヒントテーブルに関する情報を解放
1730 HeapDefaultFree(pHintTable);
1731
1732 //グローバル変数の初期バッファを解放
1733 HeapDefaultFree(initGlobalBuf);
1734
1735 //コードバッファを解放
1736 free(OpBuffer);
1737 OpBuffer=0;
1738
1739 //リソースセクションバッファを解放
1740 HeapDefaultFree(RSrcSectionBuffer);
1741
1742 //デバッグセクションを開放
1743 delete pobj_DebugSection;
1744
1745 //リロケーション情報を解放
1746 delete pobj_Reloc;
1747
1748 //列挙体に関する情報の破棄
1749 CEnumParent::DestroyEnum();
1750
1751 //クラスに関するメモリを解放
1752 compiler.GetMeta().GetClasses().Clear();
1753}
Note: See TracBrowser for help on using the repository browser.