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

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

静的リンクライブラリの読み込みに失敗したときにコンパイルエラーメッセージを表示するようにした

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