source: dev/trunk/abdev/BasicCompiler32/MakePeHdr.cpp@ 435

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

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

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 UserProc *pUserProc,*psi2;
608 while(1){
609 //辞書順にサーチ
610 temporary[0]=0;
611 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
612 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
613 {
614 pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
615 if(pUserProc->IsExport()){
616 if(temporary[0]=='\0'){
617 lstrcpy(temporary,pUserProc->GetName().c_str());
618 psi2=pUserProc;
619 }
620 else{
621 i3=lstrlen(temporary);
622 i4=(int)pUserProc->GetName().size();
623 if(i3<i4) i3=i4;
624 if(memcmp(temporary,pUserProc->GetName().c_str(),i3)>0){
625 lstrcpy(temporary,pUserProc->GetName().c_str());
626 psi2=pUserProc;
627 }
628 }
629 }
630 }
631 if(psi2==0) break;
632 pUserProc=psi2;
633
634 pUserProc->ExportOff();
635
636 if( pUserProc->GetName() == "DllMain" ){
637 DllMain_EntryPoint = pUserProc->GetBeginOpAddress();
638 }
639
640 lpdwExportAddressTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportAddressTable,(ExportNum+1)*sizeof(DWORD));
641 lpdwExportAddressTable[ExportNum] = pUserProc->GetBeginOpAddress();
642
643 lpdwExportNamePointerTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportNamePointerTable,(ExportNum+1)*sizeof(DWORD));
644 lpdwExportNamePointerTable[ExportNum]=ExportNamesLength;
645
646 lpwExportOrdinalTable=(WORD *)HeapReAlloc(hHeap,0,lpwExportOrdinalTable,(ExportNum+1)*sizeof(WORD));
647 lpwExportOrdinalTable[ExportNum]=ExportNum;
648
649 lstrcpy(lpExportNames+ExportNamesLength,pUserProc->GetName().c_str());
650 ExportNamesLength+=lstrlen(lpExportNames+ExportNamesLength)+1;
651
652 ExportNum++;
653 }
654 for(i=0;i<ExportNum;i++){
655 lpdwExportNamePointerTable[i]+=
656 sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
657 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
658 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
659 ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
660 }
661
662 ImageExportDirectory.Characteristics=0;
663 ImageExportDirectory.TimeDateStamp=(DWORD)time(NULL);
664 ImageExportDirectory.MajorVersion=0;
665 ImageExportDirectory.MinorVersion=0;
666 ImageExportDirectory.Name=sizeof(IMAGE_EXPORT_DIRECTORY)+
667 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
668 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
669 ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
670 ImageExportDirectory.Base=1;
671 ImageExportDirectory.NumberOfFunctions=ExportNum;
672 ImageExportDirectory.NumberOfNames=ExportNum;
673 ImageExportDirectory.AddressOfFunctions=sizeof(IMAGE_EXPORT_DIRECTORY);
674 ImageExportDirectory.AddressOfNames=ImageExportDirectory.AddressOfFunctions+ExportNum*sizeof(DWORD);
675 ImageExportDirectory.AddressOfNameOrdinals=ImageExportDirectory.AddressOfNames+ExportNum*sizeof(DWORD);
676 }
677
678
679 /////////////////////
680 //インポートDLL情報
681 /////////////////////
682
683 /*
684 IMAGE_IMPORT_DESCRIPTOR1[size=0x14] インポートヘッダ
685 IMAGE_IMPORT_DESCRIPTOR2[size=0x14]
686 IMAGE_IMPORT_DESCRIPTOR3[size=0x14]
687 ...
688 Dll名1[size=0x10]
689 Dll名2[size=0x10]
690 Dll名3[size=0x10]
691 ...
692 ルックアップ テーブル(ヒントを示すRVA)
693 ヒントテーブル
694 インポート アドレス テーブル(ルックアップと同じ内容だが、プログラム実行時に実行アドレスに書き換えられる)
695 */
696
697 char **ppDllNames=(char **)HeapAlloc(hHeap,0,1);
698
699 int ImportDllNum=0;
700
701 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
702 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
703 {
704 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
705
706 if( !pDllProc->IsUsing() ){
707 continue;
708 }
709
710 if( pDllProc->GetDllFileName().size() > 16 ){
711 SetError(7,NULL,cp);
712 break;
713 }
714 for(i2=0;i2<ImportDllNum;i2++){
715 if( pDllProc->GetDllFileName() == ppDllNames[i2] ){
716 break;
717 }
718 }
719 if(i2==ImportDllNum){
720 ppDllNames=(char **)HeapReAlloc(hHeap,0,ppDllNames,(ImportDllNum+1)*sizeof(char **));
721 ppDllNames[ImportDllNum]=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,16);
722 lstrcpy(ppDllNames[ImportDllNum],pDllProc->GetDllFileName().c_str());
723 ImportDllNum++;
724 }
725 }
726
727 //インポート テーブル、及びルックアップ テーブル サイズの計算
728 IMAGE_IMPORT_DESCRIPTOR *pImportTable;
729 int LookupSize;
730 LookupSize=0;
731 pImportTable=(IMAGE_IMPORT_DESCRIPTOR *)HeapAlloc(hHeap,0,(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR));
732 i3=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR);
733 for(i=0;i<ImportDllNum;i++){
734 //インポート テーブル(IMAGE_IMPORT_DESCRIPTOR)
735 pImportTable[i].OriginalFirstThunk=i3+(ImportDllNum*0x10)+LookupSize;
736 pImportTable[i].TimeDateStamp=0;
737 pImportTable[i].ForwarderChain=0;
738 pImportTable[i].Name=i3+i*0x10;
739
740 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
741 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
742 {
743 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
744
745 if( !pDllProc->IsUsing() ){
746 continue;
747 }
748
749 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
750 //ルックアップデータのサイズを追加
751 LookupSize+=sizeof(DWORD);
752 }
753 }
754 LookupSize+=sizeof(DWORD); //NULL用
755 }
756 memset(&pImportTable[i],0,sizeof(IMAGE_IMPORT_DESCRIPTOR));
757
758 //ルックアップ テーブル、ヒント テーブル
759 DWORD *pLookupTable;
760 pLookupTable=(DWORD *)HeapAlloc(hHeap,0,LookupSize*sizeof(DWORD)+1);
761 char *pHintTable;
762 int HintSize,HintAllocSize;
763 HintSize=0;
764 HintAllocSize=128*2;
765 pHintTable=(char *)HeapAlloc(hHeap,0,HintAllocSize);
766 i3+=ImportDllNum*0x10;
767 for(i=0,i5=0;i<ImportDllNum;i++){
768 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
769 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
770 {
771 DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
772
773 if( !pDllProc->IsUsing() ){
774 continue;
775 }
776
777 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
778 //ルックアップの位置をセット(後にインポート アドレス テーブルにセットしなおされる)
779 pDllProc->SetLookupAddress( i3+(i5*sizeof(DWORD)) );
780
781 //ルックアップ テーブル
782 pLookupTable[i5++]=i3+LookupSize+HintSize;
783
784 //ヒント テーブル
785 pHintTable[HintSize++]=0;
786 pHintTable[HintSize++]=0;
787 lstrcpy(pHintTable+HintSize,pDllProc->GetAlias().c_str());
788 i4=(int)pDllProc->GetAlias().size();
789 HintSize+=i4+1;
790 if(i4%2==0) pHintTable[HintSize++]=0;
791
792 if(HintAllocSize<HintSize+128){
793 HintAllocSize+=128;
794 pHintTable=(char *)HeapReAlloc(hHeap,0,pHintTable,HintAllocSize);
795 }
796 }
797 }
798 pLookupTable[i5++]=0;
799 }
800
801
802 if( compiler.IsDll() ){
803 //DLLの場合はリロケーション情報を仮生成
804 //※正式な生成は各セクションのメモリ上のサイズが決定してから再度行う。
805 pobj_Reloc->ResetRelocBuffer();
806 }
807
808
809
810 //グローバル変数情報を扱う構造体も初期バッファの有無による配置を行う
811 //(デバッグ情報で利用される)
812 BOOST_FOREACH( Variable *pVar, compiler.GetObjectModule().meta.GetGlobalVars() ){
813 if(pVar->GetOffsetAddress()&0x80000000){
814 pVar->SetOffsetAddress(
815 (pVar->GetOffsetAddress()&0x7FFFFFFF)
816 + compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
817 );
818 }
819 }
820
821
822
823 ////////////////////////////////////
824 // デバッグセクションを生成
825 ////////////////////////////////////
826
827 //デバッグセクションを生成
828 CDebugSection *pobj_DebugSection;
829 pobj_DebugSection=new CDebugSection();
830 extern BOOL bDebugCompile;
831 extern BOOL bError;
832 if(bDebugCompile&&bError==0){
833 CompileMessage( "デバッグ情報を生成しています。" );
834
835 pobj_DebugSection->make();
836
837 CompileMessage( "デバッグ情報の生成が完了しました。" );
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=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+
874 16*ImportDllNum+ //DLL名
875 LookupSize+ //ルックアップテーブル
876 HintSize+ //ヒント名(関数名)テーブル
877 LookupSize; //インポート アドレス テーブル
878 if(i%FILE_ALIGNMENT) FileSize_ImportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
879 else FileSize_ImportSection=i;
880 bUse_ImportSection=1; //インポートセクションは必ず存在する
881
882 //データセクションのファイル上のサイズ
883 if(compiler.GetObjectModule().dataTable.GetSize()%FILE_ALIGNMENT) FileSize_DataSection=compiler.GetObjectModule().dataTable.GetSize()+(FILE_ALIGNMENT-compiler.GetObjectModule().dataTable.GetSize()%FILE_ALIGNMENT);
884 else FileSize_DataSection=compiler.GetObjectModule().dataTable.GetSize();
885 if(FileSize_DataSection) bUse_DataSection=1;
886 else bUse_DataSection=0;
887
888 //リライタブルセクションのファイル上のサイズ(グローバル変数の初期情報のみを格納)
889 if( compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize() % FILE_ALIGNMENT )
890 {
891 FileSize_RWSection =
892 compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
893 + (FILE_ALIGNMENT-compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()%FILE_ALIGNMENT);
894 }
895 else{
896 if( compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize() )
897 {
898 FileSize_RWSection = compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize();
899 }
900 else FileSize_RWSection=FILE_ALIGNMENT;
901 }
902 bUse_RWSection=1;
903
904 //リソースセクションのファイル上のサイズ
905 char *RSrcSectionBuffer;
906 int RSrcSectionSize;
907 RSrcSectionBuffer=GetRSrcSectionBuffer(&RSrcSectionSize);
908 if(RSrcSectionSize%FILE_ALIGNMENT) FileSize_RSrcSection=RSrcSectionSize+(FILE_ALIGNMENT-RSrcSectionSize%FILE_ALIGNMENT);
909 else FileSize_RSrcSection=RSrcSectionSize;
910 if(FileSize_RSrcSection) bUse_RSrcSection=1;
911 else bUse_RSrcSection=0;
912
913 //リロケーションセクションのファイル上のサイズ
914 if(pobj_Reloc->length%FILE_ALIGNMENT) FileSize_RelocSection=pobj_Reloc->length+(FILE_ALIGNMENT-pobj_Reloc->length%FILE_ALIGNMENT);
915 else FileSize_RelocSection=pobj_Reloc->length;
916 if(FileSize_RelocSection) bUse_RelocSection=1;
917 else bUse_RelocSection=0;
918
919 //デバッグセクションのファイル上のサイズ
920 if(pobj_DebugSection->length%FILE_ALIGNMENT) FileSize_DebugSection=pobj_DebugSection->length+(FILE_ALIGNMENT-pobj_DebugSection->length%FILE_ALIGNMENT);
921 else FileSize_DebugSection=pobj_DebugSection->length;
922 if(FileSize_DebugSection) bUse_DebugSection=1;
923 else bUse_DebugSection=0;
924
925
926 //各セッションのファイル上の位置
927 FilePos_CodeSection= EXE_HEADER_SIZE;
928 FilePos_ExportSection= FilePos_CodeSection+
929 FileSize_CodeSection;
930 FilePos_ImportSection= FilePos_ExportSection+
931 FileSize_ExportSection;
932 FilePos_DataSection= FilePos_ImportSection+
933 FileSize_ImportSection;
934 FilePos_RWSection= FilePos_DataSection+
935 FileSize_DataSection;
936 FilePos_RSrcSection= FilePos_RWSection+
937 FileSize_RWSection;
938 FilePos_RelocSection= FilePos_RSrcSection+
939 FileSize_RSrcSection;
940 FilePos_DebugSection= FilePos_RelocSection+
941 FileSize_RelocSection;
942
943
944 //コードセッションのメモリ上のサイズ
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 pImportTable[i].OriginalFirstThunk+=MemPos_ImportSection;
1047 pImportTable[i].Name+=MemPos_ImportSection;
1048
1049 //インポート アドレス テーブル(ルックアップとヒントを飛び越す)
1050 pImportTable[i].FirstThunk=pImportTable[i].OriginalFirstThunk+
1051 LookupSize+ //ルックアップテーブル
1052 HintSize; //ヒント名(関数名)テーブル
1053
1054 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
1055 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
1056 {
1057 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
1058
1059 if( !pDllProc->IsUsing() ){
1060 continue;
1061 }
1062
1063 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
1064 //ルックアップ テーブル
1065 pLookupTable[i5++]+=MemPos_ImportSection;
1066 }
1067 }
1068 i5++;
1069 }
1070
1071
1072 ////////////////////////////////////////
1073 //仮想関数データテーブルスケジュール
1074 compiler.GetObjectModule().meta.GetClasses().ActionVtblSchedule( ImageBase, MemPos_CodeSection, MemPos_DataSection );
1075
1076
1077 if( compiler.IsDll() ){
1078 //DLLの場合はリロケーション情報を生成
1079 pobj_Reloc->ResetRelocBuffer();
1080 }
1081
1082 compiler.linker.SetDataTable( compiler.GetObjectModule().dataTable );
1083
1084 compiler.linker.SetImageBase( ImageBase );
1085 compiler.linker.ResolveDataTableSchedules( MemPos_DataSection );
1086 compiler.linker.ResolveCatchAddressSchedules( MemPos_CodeSection );
1087 compiler.linker.ResolveDllProcSchedules( MemPos_CodeSection, MemPos_ImportSection, LookupSize, HintSize );
1088 compiler.linker.ResolveUserProcSchedules( MemPos_CodeSection );
1089 compiler.linker.ResolveGlobalVarSchedules( MemPos_RWSection );
1090 compiler.linker.ResolveVtblSchedule( MemPos_DataSection );
1091 compiler.linker.ResolveTypeInfoSchedule( MemPos_DataSection );
1092
1093
1094
1095 ////////////////////////////////
1096 // リソースアドレススケジュール
1097 extern DWORD *lpdwRSrcAddrSchedule;
1098 extern int RSrcAddrScheduleNum;
1099 for(i=0;i<RSrcAddrScheduleNum;i++){
1100 *(DWORD *)(RSrcSectionBuffer+lpdwRSrcAddrSchedule[i])+=MemPos_RSrcSection;
1101 }
1102 HeapDefaultFree(lpdwRSrcAddrSchedule);
1103
1104
1105 //Dosスタブ
1106 char *DosStubBuffer;
1107 int DosStubSize;
1108 hFile=CreateFile(
1109 ( Jenga::Common::Environment::GetAppDir() + "\\SubOperation\\dosstub.pgm" ).c_str(),
1110 GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
1111 if(hFile==INVALID_HANDLE_VALUE){
1112 MessageBox(hOwnerEditor,"dosstub.pgmの読み込みに失敗","error",MB_OK);
1113 goto EndWriteOpcode;
1114 }
1115 DosStubSize=GetFileSize(hFile,NULL);
1116 DosStubBuffer=(char *)HeapAlloc(hHeap,0,DosStubSize);
1117 ReadFile(hFile,DosStubBuffer,DosStubSize,(DWORD *)&i2,NULL);
1118 CloseHandle(hFile);
1119
1120
1121 extern BOOL bError;
1122 if(bError) goto EndWriteOpcode;
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_HEADERS ImagePeHdr;
1168 ImagePeHdr.Signature=IMAGE_NT_SIGNATURE;
1169
1170 //マシンタイプ
1171 ImagePeHdr.FileHeader.Machine= IMAGE_FILE_MACHINE_I386;
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_OPTIONAL32_HEADER;
1185 if( compiler.IsDll() ){
1186 ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_EXECUTABLE_IMAGE|
1187 IMAGE_FILE_32BIT_MACHINE|
1188 IMAGE_FILE_LINE_NUMS_STRIPPED|
1189 IMAGE_FILE_LOCAL_SYMS_STRIPPED|
1190 IMAGE_FILE_DLL;
1191 }
1192 else{
1193 ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_EXECUTABLE_IMAGE|
1194 IMAGE_FILE_32BIT_MACHINE|
1195 IMAGE_FILE_LINE_NUMS_STRIPPED|
1196 IMAGE_FILE_LOCAL_SYMS_STRIPPED;
1197 }
1198
1199 ImagePeHdr.OptionalHeader.Magic= 0x010B;
1200 ImagePeHdr.OptionalHeader.MajorLinkerVersion= 4;
1201 ImagePeHdr.OptionalHeader.MinorLinkerVersion= 0;
1202 ImagePeHdr.OptionalHeader.SizeOfCode= FileSize_CodeSection; //コードサイズ(.textのセッションサイズ)
1203 ImagePeHdr.OptionalHeader.SizeOfInitializedData=FileSize_DataSection; //データサイズ(.dataのセッションサイズ)
1204 ImagePeHdr.OptionalHeader.SizeOfUninitializedData=0; //未初期化データのサイズ(なし)
1205 if( compiler.IsDll() ){
1206 if(DllMain_EntryPoint==-1)
1207 ImagePeHdr.OptionalHeader.AddressOfEntryPoint=0;
1208 else
1209 ImagePeHdr.OptionalHeader.AddressOfEntryPoint=MemPos_CodeSection+DllMain_EntryPoint;
1210 }
1211 else ImagePeHdr.OptionalHeader.AddressOfEntryPoint= MemPos_CodeSection;
1212 ImagePeHdr.OptionalHeader.BaseOfCode= MemPos_CodeSection; //.textのアドレス
1213 ImagePeHdr.OptionalHeader.BaseOfData= MemPos_DataSection; //.dataのアドレス
1214
1215 ImagePeHdr.OptionalHeader.ImageBase= ImageBase; //イメージベース
1216 ImagePeHdr.OptionalHeader.SectionAlignment= MEM_ALIGNMENT; //セクションアラインメント
1217 ImagePeHdr.OptionalHeader.FileAlignment= FILE_ALIGNMENT;
1218 ImagePeHdr.OptionalHeader.MajorOperatingSystemVersion=4;
1219 ImagePeHdr.OptionalHeader.MinorOperatingSystemVersion=0;
1220 ImagePeHdr.OptionalHeader.MajorImageVersion= 0;
1221 ImagePeHdr.OptionalHeader.MinorImageVersion= 0;
1222 ImagePeHdr.OptionalHeader.MajorSubsystemVersion=4;
1223 ImagePeHdr.OptionalHeader.MinorSubsystemVersion=0;
1224 ImagePeHdr.OptionalHeader.Win32VersionValue= 0;
1225 ImagePeHdr.OptionalHeader.SizeOfImage= EXE_HEADER_SIZE+
1226 MemSize_CodeSection+
1227 MemSize_ExportSection+
1228 MemSize_ImportSection+
1229 MemSize_DataSection+
1230 MemSize_RWSection+
1231 MemSize_RSrcSection+
1232 MemSize_RelocSection+
1233 MemSize_DebugSection;//すべてのイメージサイズ
1234 ImagePeHdr.OptionalHeader.SizeOfHeaders= EXE_HEADER_SIZE;//ヘッダサイズ
1235 ImagePeHdr.OptionalHeader.CheckSum= 0;
1236 extern unsigned short TypeOfSubSystem;
1237 ImagePeHdr.OptionalHeader.Subsystem= TypeOfSubSystem;
1238 ImagePeHdr.OptionalHeader.DllCharacteristics= 0;
1239 ImagePeHdr.OptionalHeader.SizeOfStackReserve= 0x00100000;
1240 ImagePeHdr.OptionalHeader.SizeOfStackCommit= 0x00001000;
1241 ImagePeHdr.OptionalHeader.SizeOfHeapReserve= 0x00100000;
1242 ImagePeHdr.OptionalHeader.SizeOfHeapCommit= 0x00001000;
1243 ImagePeHdr.OptionalHeader.LoaderFlags= 0;
1244 ImagePeHdr.OptionalHeader.NumberOfRvaAndSizes= IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1245
1246 //データ ディクショナリ
1247 ImagePeHdr.OptionalHeader.DataDirectory[0].VirtualAddress=MemPos_ExportSection;
1248 ImagePeHdr.OptionalHeader.DataDirectory[0].Size=FileSize_ExportSection;
1249 ImagePeHdr.OptionalHeader.DataDirectory[1].VirtualAddress=MemPos_ImportSection;//インポートテーブル
1250 ImagePeHdr.OptionalHeader.DataDirectory[1].Size=FileSize_ImportSection;
1251 ImagePeHdr.OptionalHeader.DataDirectory[2].VirtualAddress=MemPos_RSrcSection;
1252 ImagePeHdr.OptionalHeader.DataDirectory[2].Size=RSrcSectionSize;
1253 ImagePeHdr.OptionalHeader.DataDirectory[3].VirtualAddress=0;
1254 ImagePeHdr.OptionalHeader.DataDirectory[3].Size=0;
1255 ImagePeHdr.OptionalHeader.DataDirectory[4].VirtualAddress=0;
1256 ImagePeHdr.OptionalHeader.DataDirectory[4].Size=0;
1257 ImagePeHdr.OptionalHeader.DataDirectory[5].VirtualAddress=MemPos_RelocSection;
1258 ImagePeHdr.OptionalHeader.DataDirectory[5].Size=pobj_Reloc->length;
1259 ImagePeHdr.OptionalHeader.DataDirectory[6].VirtualAddress=0;
1260 ImagePeHdr.OptionalHeader.DataDirectory[6].Size=0;
1261 ImagePeHdr.OptionalHeader.DataDirectory[7].VirtualAddress=0;
1262 ImagePeHdr.OptionalHeader.DataDirectory[7].Size=0;
1263 ImagePeHdr.OptionalHeader.DataDirectory[8].VirtualAddress=0;
1264 ImagePeHdr.OptionalHeader.DataDirectory[8].Size=0;
1265 ImagePeHdr.OptionalHeader.DataDirectory[9].VirtualAddress=0;
1266 ImagePeHdr.OptionalHeader.DataDirectory[9].Size=0;
1267 ImagePeHdr.OptionalHeader.DataDirectory[10].VirtualAddress=0;
1268 ImagePeHdr.OptionalHeader.DataDirectory[10].Size=0;
1269 ImagePeHdr.OptionalHeader.DataDirectory[11].VirtualAddress=0;
1270 ImagePeHdr.OptionalHeader.DataDirectory[11].Size=0;
1271 ImagePeHdr.OptionalHeader.DataDirectory[12].VirtualAddress=MemPos_ImportSection+
1272 (ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+
1273 16*ImportDllNum+ //DLL名
1274 LookupSize+ //ルックアップテーブル
1275 HintSize; //ヒント名(関数名)テーブル
1276 ImagePeHdr.OptionalHeader.DataDirectory[12].Size=LookupSize;
1277 ImagePeHdr.OptionalHeader.DataDirectory[13].VirtualAddress=0;
1278 ImagePeHdr.OptionalHeader.DataDirectory[13].Size=0;
1279 ImagePeHdr.OptionalHeader.DataDirectory[14].VirtualAddress=0;
1280 ImagePeHdr.OptionalHeader.DataDirectory[14].Size=0;
1281 ImagePeHdr.OptionalHeader.DataDirectory[15].VirtualAddress=0;
1282 ImagePeHdr.OptionalHeader.DataDirectory[15].Size=0;
1283
1284
1285 //コードセクションヘッダ
1286 IMAGE_SECTION_HEADER CodeSectionHeader;
1287 memset((char *)CodeSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1288 lstrcpy((char *)CodeSectionHeader.Name,".text");
1289 CodeSectionHeader.Misc.VirtualSize= MemSize_CodeSection;
1290 CodeSectionHeader.VirtualAddress= MemPos_CodeSection; //開始アドレス
1291 CodeSectionHeader.SizeOfRawData= FileSize_CodeSection;
1292 CodeSectionHeader.PointerToRawData= FilePos_CodeSection; //ファイル上の開始アドレス
1293 CodeSectionHeader.PointerToRelocations= 0;
1294 CodeSectionHeader.PointerToLinenumbers= 0;
1295 CodeSectionHeader.NumberOfRelocations= 0;
1296 CodeSectionHeader.NumberOfLinenumbers= 0;
1297 CodeSectionHeader.Characteristics= IMAGE_SCN_MEM_EXECUTE|
1298 IMAGE_SCN_MEM_READ|
1299 IMAGE_SCN_CNT_CODE;
1300
1301 //エクスポートセクションヘッダ
1302 IMAGE_SECTION_HEADER ExportSectionHeader;
1303 memset((char *)ExportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1304 lstrcpy((char *)ExportSectionHeader.Name,".edata");
1305 ExportSectionHeader.Misc.VirtualSize= MemSize_ExportSection;
1306 ExportSectionHeader.VirtualAddress= MemPos_ExportSection; //開始アドレス
1307 ExportSectionHeader.SizeOfRawData= FileSize_ExportSection; //サイズ
1308 ExportSectionHeader.PointerToRawData= FilePos_ExportSection; //ファイル上の開始アドレス
1309 ExportSectionHeader.PointerToRelocations= 0;
1310 ExportSectionHeader.PointerToLinenumbers= 0;
1311 ExportSectionHeader.NumberOfRelocations= 0;
1312 ExportSectionHeader.NumberOfLinenumbers= 0;
1313 ExportSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1314 IMAGE_SCN_MEM_READ;
1315
1316 //インポートセクションヘッダ
1317 IMAGE_SECTION_HEADER ImportSectionHeader;
1318 memset((char *)ImportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1319 lstrcpy((char *)ImportSectionHeader.Name,".idata");
1320 ImportSectionHeader.Misc.VirtualSize= MemSize_ImportSection;
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(OutputFileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
1412 if(hFile==INVALID_HANDLE_VALUE){
1413 SetError(53,OutputFileName,-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_HEADERS),(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 //インポート ディレクトリ テーブル(Nullディレクトリ テーブルを含む)
1527 for(i3=0;i3<(ImportDllNum+1);i3++){
1528 WriteFile(hFile,&pImportTable[i3],sizeof(IMAGE_IMPORT_DESCRIPTOR),(DWORD *)&i2,NULL);
1529 i+=i2;
1530 }
1531
1532 //DLL名
1533 for(i3=0;i3<ImportDllNum;i3++){
1534 WriteFile(hFile,ppDllNames[i3],16,(DWORD *)&i2,NULL);
1535 i+=i2;
1536 }
1537
1538 //ルックアップ テーブル
1539 WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
1540 i+=i2;
1541
1542 //ヒント テーブル
1543 WriteFile(hFile,pHintTable,HintSize,(DWORD *)&i2,NULL);
1544 i+=i2;
1545
1546 //インポート アドレス テーブル
1547 WriteFile(hFile,pLookupTable,LookupSize,(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
1578 char *temp = (char *)calloc( FileSize_RWSection, 1 );
1579 memcpy(
1580 temp,
1581 compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetBuffer(),
1582 compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
1583 );
1584
1585 WriteFile(hFile,temp,FileSize_RWSection,(DWORD *)&i2,NULL);
1586 i+=i2;
1587
1588 free( temp );
1589 }
1590
1591 //FilePos_RSrcSectionまでNULLを並べる
1592 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RSrcSection-i);
1593 WriteFile(hFile,temp2,FilePos_RSrcSection-i,(DWORD *)&i2,NULL);
1594 HeapDefaultFree(temp2);
1595 i+=i2;
1596
1597 if(bUse_RSrcSection){
1598 //リソースバッファ
1599 WriteFile(hFile,RSrcSectionBuffer,RSrcSectionSize,(DWORD *)&i2,NULL);
1600 i+=i2;
1601 }
1602
1603 //FilePos_RelocSectionまでNULLを並べる
1604 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RelocSection-i);
1605 WriteFile(hFile,temp2,FilePos_RelocSection-i,(DWORD *)&i2,NULL);
1606 HeapDefaultFree(temp2);
1607 i+=i2;
1608
1609 if(bUse_RelocSection){
1610 //リロケーション情報
1611 WriteFile(hFile,pobj_Reloc->buffer,pobj_Reloc->length,(DWORD *)&i2,NULL);
1612 i+=i2;
1613 }
1614
1615 //ファイルアラインメントを考慮
1616 if(i%FILE_ALIGNMENT){
1617 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
1618 WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
1619 HeapDefaultFree(temp2);
1620 i+=i2;
1621 }
1622
1623 if(bUse_DebugSection){
1624 //デバッグセクション
1625 WriteFile(hFile,pobj_DebugSection->buffer,pobj_DebugSection->length,(DWORD *)&i2,NULL);
1626 i+=i2;
1627 }
1628
1629 //ファイルアラインメントを考慮
1630 if(i%FILE_ALIGNMENT){
1631 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
1632 WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
1633 HeapDefaultFree(temp2);
1634 i+=i2;
1635 }
1636
1637 //書き込み終了
1638 CloseHandle(hFile);
1639
1640
1641EndWriteOpcode:
1642
1643 //Dosスタブ用のメモリを解放
1644 HeapDefaultFree(DosStubBuffer);
1645
1646 //エクスポート テーブル情報を解放
1647 HeapDefaultFree(lpdwExportAddressTable);
1648 HeapDefaultFree(lpdwExportNamePointerTable);
1649 HeapDefaultFree(lpwExportOrdinalTable);
1650
1651 //インポートDLL情報を解放
1652 HeapDefaultFree(pImportTable);
1653 for(i=0;i<ImportDllNum;i++)
1654 HeapDefaultFree(ppDllNames[i]);
1655 HeapDefaultFree(ppDllNames);
1656
1657 //ルックアップテーブルに関する情報を解放
1658 HeapDefaultFree(pLookupTable);
1659
1660 //ヒントテーブルに関する情報を解放
1661 HeapDefaultFree(pHintTable);
1662
1663 //リソースセクションバッファを解放
1664 HeapDefaultFree(RSrcSectionBuffer);
1665
1666 //デバッグセクションを開放
1667 delete pobj_DebugSection;
1668
1669 //リロケーション情報を解放
1670 delete pobj_Reloc;
1671}
Note: See TracBrowser for help on using the repository browser.