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

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

BasicSourceのシリアライズがうまくいっていない

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