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

Last change on this file since 206 was 206, checked in by dai_9181, 17 years ago

コード全体のリファクタリングを実施

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