source: dev/trunk/ab5.0/abdev/BasicCompiler64/MakePeHdr.cpp@ 460

Last change on this file since 460 was 460, checked in by dai_9181, 16 years ago

[459]を64bit版にもマージ。

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