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

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

・Compiler::IsDebugメソッドを導入した(bDebugCompileグローバル変数は廃止)。
・bStrictグローバル変数は意味を成さないので廃止した。

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