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

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

静的リンクライブラリにより、複数のグローバル領域が存在することになったのでそれぞれを関数ベースに分けた

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