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

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