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

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