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

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

関数の戻り値の構造体など、一時メモリに保持された構造体のメンバに直接アクセスした場合、その一時メモリの解放が正常に行われないバグを修正(64bit版も修正した)。

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