source: dev/trunk/ab5.0/abdev/BasicCompiler32/MakePeHdr.cpp@ 451

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

DLLコンパイルに失敗してしまうバグを修正。

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