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

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

構成管理を大幅に改良。

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