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

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