source: dev/trunk/ab5.0/abdev/compiler_x86/MakePeHdr.cpp@ 519

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

jenga.hを追加。

File size: 55.3 KB
Line 
1#include "stdafx.h"
2
3#include "../BasicCompiler_Common/common.h"
4#include "../BasicCompiler_Common/DebugSection.h"
5#include "Opcode.h"
6
7
8////////////////////////////
9// 特殊関数の構造体ポインタ
10////////////////////////////
11
12// グローバル関数、静的メソッド
13const UserProc
14 *pSub_System_StartupProgram,
15 *pSub_System_GlobalArea,
16 *pSub_DebugSys_StartProc,
17 *pSub_DebugSys_EndProc,
18 *pSub_DebugSys_SaveContext,
19 *pSub_System_GetEip,
20 *pSub_System_InitDllGlobalVariables,
21 *pSub_System_InitStaticLocalVariables,
22 *pSub_System_Call_Destructor_of_GlobalObject,
23 *pSub_System_End,
24 *pSub_pow,
25 *pSub_calloc,
26 *pSub_realloc,
27 *pSub_free,
28 *pSub_System_GC_malloc_ForObject,
29 *pSub_System_GC_malloc_ForObjectPtr,
30 *pSub_System_GC_free_for_SweepingDelete,
31 *pSub_System_AddNeedFreeTempStructure,
32 *pSub_System_FreeTempStructure,
33 *pSubStaticMethod_System_TypeBase_InitializeUserTypes,
34 *pSubStaticMethod_System_TypeBase_InitializeUserTypesForBaseType,
35
36 *pSub_allrem,
37 *pSub_aullrem,
38 *pSub_allmul,
39 *pSub_alldiv,
40 *pSub_aulldiv,
41 *pSub_allshl,
42 *pSub_allshr,
43 *pSub_aullshr,
44
45 *pSub_esp_error;
46
47// 動的メソッド
48const UserProc
49 *pUserProc_System_CGarbageCollection_RegisterGlobalRoots;
50
51
52//////////////////////////////
53// 各セクションの位置とサイズ
54int FileSize_CodeSection,
55 FileSize_ExportSection,
56 FileSize_ImportSection,
57 FileSize_DataSection,
58 FileSize_RWSection,
59 FileSize_RSrcSection,
60 FileSize_RelocSection,
61 FileSize_DebugSection;
62int FilePos_CodeSection,
63 FilePos_ExportSection,
64 FilePos_ImportSection,
65 FilePos_DataSection,
66 FilePos_RWSection,
67 FilePos_RSrcSection,
68 FilePos_RelocSection,
69 FilePos_DebugSection;
70int MemSize_CodeSection,
71 MemSize_ExportSection,
72 MemSize_ImportSection,
73 MemSize_DataSection,
74 MemSize_RWSection,
75 MemSize_RSrcSection,
76 MemSize_RelocSection,
77 MemSize_DebugSection;
78int MemPos_CodeSection,
79 MemPos_ExportSection,
80 MemPos_ImportSection,
81 MemPos_DataSection,
82 MemPos_RWSection,
83 MemPos_RSrcSection,
84 MemPos_RelocSection,
85 MemPos_DebugSection;
86int bUse_CodeSection,
87 bUse_ExportSection,
88 bUse_ImportSection,
89 bUse_DataSection,
90 bUse_RWSection,
91 bUse_RSrcSection,
92 bUse_RelocSection,
93 bUse_DebugSection;
94
95
96void Compile(void){
97 extern HWND hMainDlg;
98 extern HWND hOwnerEditor;
99 extern HANDLE hHeap;
100 extern DWORD ImageBase;
101 extern char *basbuf;
102
103 int i,i2,i3,i4,i5;
104 char temporary[MAX_PATH],*temp2;
105 HANDLE hFile;
106
107
108 //コードセクションのメモリ上の位置
109 MemPos_CodeSection= 0x1000;
110
111 //データセクションのメモリ上の位置(仮定)
112 MemPos_DataSection= 0x10000000;
113
114
115 //エクスポート セクションを利用するかどうか
116 if( compiler.IsDll() ) bUse_ExportSection=1;
117 else bUse_ExportSection=0;
118
119
120 // 静的リンク
121 compiler.StaticLink( compiler.staticLibraries );
122
123
124 //////////////////
125 // データテーブル
126 if( compiler.IsDebug() ){
127 compiler.GetObjectModule().dataTable.Add( (long)0x00000002 );
128 }
129
130
131 //////////////////////
132 // コード生成前の準備
133 //////////////////////
134
135 //重複エラー情報管理のメモリを確保(グローバル領域コンパイル用)
136 extern char **SynonymErrorWords;
137 extern int SynonymErrorNum;
138 SynonymErrorNum=0;
139 SynonymErrorWords=(char **)HeapAlloc(hHeap,0,1);
140
141 //列挙体に関する情報を収集
142 compiler.enumInfoCollection.InitEnum();
143
144 //列挙体からクラスコードを生成
145 char *temp;
146 temp=compiler.enumInfoCollection.GenerateSourceCode();
147 AddSourceCode(temp);
148 HeapDefaultFree(temp);
149
150 // 名前空間情報を取得
151 ActiveBasic::Compiler::LexicalAnalyzer::CollectNamespaces(
152 compiler.GetObjectModule().GetCurrentSource().GetBuffer(),
153 compiler.GetObjectModule().meta.GetNamespaces()
154 );
155
156 // デリゲートに関する情報を収集
157 {
158 compiler.GetObjectModule().meta.GetDelegates().Collect(
159 compiler.GetObjectModule().GetCurrentSource()
160 );
161 compiler.GetObjectModule().meta.GetDelegates().Iterator_Init();
162
163 // デリゲートからクラスコードを生成
164 std::string tempSource;
165 compiler.GetObjectModule().meta.GetDelegates().GenerateSourceCode( tempSource );
166 AddSourceCode( tempSource.c_str() );
167 }
168
169 //クラス名を取得(詳細情報はCollectClassesで取得)
170 // CollectProcedures関数の中で参照されるオブジェクト名を事前に取得する。
171 // ※オブジェクトの内容までは取得しない
172 ActiveBasic::Compiler::LexicalAnalyzer::CollectClassesForNameOnly(
173 compiler.GetObjectModule().GetCurrentSource().GetBuffer(),
174 compiler.GetObjectModule().meta.GetClasses()
175 );
176
177 //TypeDef情報を初期化
178 compiler.GetObjectModule().meta.GetTypeDefs().CollectTypeDefs();
179
180 /*
181 デリゲートのパラメータを再取得
182 クラス/TypeDefなどの型名前情報がすべて揃ってからでないと
183 型情報に依存するパラメータ情報を取得できないため、ここでの再取得が必要
184 */
185 compiler.GetObjectModule().meta.GetDelegates().RefleshParameterAndReturnType();
186
187 //定数情報を取得
188 GetConstInfo();
189
190 // サブルーチン(ユーザー定義、DLL関数)の識別子、アドレスを取得
191 compiler.pCompilingClass = NULL;
192 ActiveBasic::Compiler::LexicalAnalyzer::CollectProcedures(
193 compiler.GetObjectModule().GetCurrentSource(),
194 compiler.GetObjectModule().meta.GetUserProcs(),
195 compiler.GetObjectModule().meta.GetDllProcs()
196 );
197
198 // クラス情報を取得(※注 - CollectProceduresの後に呼び出す)
199 ActiveBasic::Compiler::LexicalAnalyzer::CollectClasses(
200 compiler.GetObjectModule().GetCurrentSource().GetBuffer(),
201 compiler.GetObjectModule().meta.GetClasses()
202 );
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 compiler.errorMessenger.OutputFatalError();
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( compiler.IsDebug() )
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 compiler.errorMessenger.Output(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 compiler.errorMessenger.Output(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( program.GetOutputFilePath() );
497 Jenga::Common::Directory dir( path.GetDriveName() + path.GetDirName(), true );
498
499 // 書き込む
500 if( !compiler.GetObjectModule().Write( program.GetOutputFilePath() ) )
501 {
502 MessageBox(0,"XML書き込みに失敗","test",0);
503 }
504 return;
505 }
506
507 if( !compiler.errorMessenger.HasError() )
508 {
509 compiler.messenger.Output( "リンク中..." );
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 char temp2[MAX_PATH],temp3[MAX_PATH];
552 _splitpath(program.GetOutputFilePath().c_str(),temp2,temp3,NULL,NULL);
553 lstrcat(temp2,temp3);
554 lstrcpy(temp3,pDllProc->GetDllFileName().c_str());
555 GetFullPath(temp3,temp2);
556 hLib=LoadLibrary(temp3);
557
558 if(!hLib){
559 compiler.errorMessenger.Output(-106,pDllProc->GetDllFileName().c_str(),pDllProc->GetCodePos());
560 }
561 }
562
563 if(hLib){
564 if(!GetProcAddress(hLib,pDllProc->GetAlias().c_str())){
565 FreeLibrary(hLib);
566 compiler.errorMessenger.Output(-107,pDllProc->GetAlias(),pDllProc->GetCodePos());
567 }
568 FreeLibrary(hLib);
569 }
570 }
571
572
573
574 /////////////////////////////
575 // リソースデータを読み込む
576 /////////////////////////////
577 extern char ResourceFileName[MAX_PATH];
578 GetResourceData(ResourceFileName);
579
580
581 //////////////////////////////
582 // エクスポート情報(DLLのみ)
583 //////////////////////////////
584 IMAGE_EXPORT_DIRECTORY ImageExportDirectory;
585 DWORD *lpdwExportAddressTable;
586 DWORD *lpdwExportNamePointerTable;
587 WORD *lpwExportOrdinalTable;
588 char lpExportNames[8192];
589 int ExportNamesLength;
590 int ExportNum;
591 int DllMain_EntryPoint;
592
593 DllMain_EntryPoint=-1;
594 ExportNum=0;
595 ExportNamesLength=0;
596 lpdwExportAddressTable=(DWORD *)HeapAlloc(hHeap,0,1);
597 lpdwExportNamePointerTable=(DWORD *)HeapAlloc(hHeap,0,1);
598 lpwExportOrdinalTable=(WORD *)HeapAlloc(hHeap,0,1);
599
600 if(bUse_ExportSection){
601 _splitpath(program.GetOutputFilePath().c_str(),NULL,NULL,lpExportNames,temporary);
602 lstrcat(lpExportNames,temporary);
603 ExportNamesLength=lstrlen(lpExportNames)+1;
604
605 while(1)
606 {
607 UserProc *pUserProc = NULL;
608
609 //辞書順にサーチ
610 temporary[0]=0;
611 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
612 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
613 {
614 UserProc *pTempUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
615 if(pTempUserProc->IsExport()){
616 if(temporary[0]=='\0'){
617 lstrcpy(temporary,pTempUserProc->GetName().c_str());
618 pUserProc = pTempUserProc;
619 }
620 else{
621 i3=lstrlen(temporary);
622 i4=(int)pTempUserProc->GetName().size();
623 if(i3<i4) i3=i4;
624 if(memcmp(temporary,pTempUserProc->GetName().c_str(),i3)>0){
625 lstrcpy(temporary,pTempUserProc->GetName().c_str());
626 pUserProc = pTempUserProc;
627 }
628 }
629 }
630 }
631 if( pUserProc == NULL )
632 {
633 break;
634 }
635
636 pUserProc->ExportOff();
637
638 if( pUserProc->GetName() == "DllMain" ){
639 DllMain_EntryPoint = pUserProc->GetBeginOpAddress();
640 }
641
642 lpdwExportAddressTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportAddressTable,(ExportNum+1)*sizeof(DWORD));
643 lpdwExportAddressTable[ExportNum] = pUserProc->GetBeginOpAddress();
644
645 lpdwExportNamePointerTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportNamePointerTable,(ExportNum+1)*sizeof(DWORD));
646 lpdwExportNamePointerTable[ExportNum]=ExportNamesLength;
647
648 lpwExportOrdinalTable=(WORD *)HeapReAlloc(hHeap,0,lpwExportOrdinalTable,(ExportNum+1)*sizeof(WORD));
649 lpwExportOrdinalTable[ExportNum]=ExportNum;
650
651 lstrcpy(lpExportNames+ExportNamesLength,pUserProc->GetName().c_str());
652 ExportNamesLength+=lstrlen(lpExportNames+ExportNamesLength)+1;
653
654 ExportNum++;
655 }
656 for(i=0;i<ExportNum;i++){
657 lpdwExportNamePointerTable[i]+=
658 sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
659 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
660 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
661 ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
662 }
663
664 ImageExportDirectory.Characteristics=0;
665 ImageExportDirectory.TimeDateStamp=(DWORD)time(NULL);
666 ImageExportDirectory.MajorVersion=0;
667 ImageExportDirectory.MinorVersion=0;
668 ImageExportDirectory.Name=sizeof(IMAGE_EXPORT_DIRECTORY)+
669 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
670 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
671 ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
672 ImageExportDirectory.Base=1;
673 ImageExportDirectory.NumberOfFunctions=ExportNum;
674 ImageExportDirectory.NumberOfNames=ExportNum;
675 ImageExportDirectory.AddressOfFunctions=sizeof(IMAGE_EXPORT_DIRECTORY);
676 ImageExportDirectory.AddressOfNames=ImageExportDirectory.AddressOfFunctions+ExportNum*sizeof(DWORD);
677 ImageExportDirectory.AddressOfNameOrdinals=ImageExportDirectory.AddressOfNames+ExportNum*sizeof(DWORD);
678 }
679
680
681 /////////////////////
682 //インポートDLL情報
683 /////////////////////
684
685 /*
686 IMAGE_IMPORT_DESCRIPTOR1[size=0x14] インポートヘッダ
687 IMAGE_IMPORT_DESCRIPTOR2[size=0x14]
688 IMAGE_IMPORT_DESCRIPTOR3[size=0x14]
689 ...
690 Dll名1[size=0x10]
691 Dll名2[size=0x10]
692 Dll名3[size=0x10]
693 ...
694 ルックアップ テーブル(ヒントを示すRVA)
695 ヒントテーブル
696 インポート アドレス テーブル(ルックアップと同じ内容だが、プログラム実行時に実行アドレスに書き換えられる)
697 */
698
699 char **ppDllNames=(char **)HeapAlloc(hHeap,0,1);
700
701 int ImportDllNum=0;
702
703 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
704 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
705 {
706 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
707
708 if( !pDllProc->IsUsing() ){
709 continue;
710 }
711
712 if( pDllProc->GetDllFileName().size() > 16 ){
713 compiler.errorMessenger.Output(7,NULL,cp);
714 break;
715 }
716 for(i2=0;i2<ImportDllNum;i2++){
717 if( pDllProc->GetDllFileName() == ppDllNames[i2] ){
718 break;
719 }
720 }
721 if(i2==ImportDllNum){
722 ppDllNames=(char **)HeapReAlloc(hHeap,0,ppDllNames,(ImportDllNum+1)*sizeof(char **));
723 ppDllNames[ImportDllNum]=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,16);
724 lstrcpy(ppDllNames[ImportDllNum],pDllProc->GetDllFileName().c_str());
725 ImportDllNum++;
726 }
727 }
728
729 //インポート テーブル、及びルックアップ テーブル サイズの計算
730 IMAGE_IMPORT_DESCRIPTOR *pImportTable;
731 int LookupSize;
732 LookupSize=0;
733 pImportTable=(IMAGE_IMPORT_DESCRIPTOR *)HeapAlloc(hHeap,0,(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR));
734 i3=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR);
735 for(i=0;i<ImportDllNum;i++){
736 //インポート テーブル(IMAGE_IMPORT_DESCRIPTOR)
737 pImportTable[i].OriginalFirstThunk=i3+(ImportDllNum*0x10)+LookupSize;
738 pImportTable[i].TimeDateStamp=0;
739 pImportTable[i].ForwarderChain=0;
740 pImportTable[i].Name=i3+i*0x10;
741
742 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
743 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
744 {
745 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
746
747 if( !pDllProc->IsUsing() ){
748 continue;
749 }
750
751 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
752 //ルックアップデータのサイズを追加
753 LookupSize+=sizeof(DWORD);
754 }
755 }
756 LookupSize+=sizeof(DWORD); //NULL用
757 }
758 memset(&pImportTable[i],0,sizeof(IMAGE_IMPORT_DESCRIPTOR));
759
760 //ルックアップ テーブル、ヒント テーブル
761 DWORD *pLookupTable;
762 pLookupTable=(DWORD *)HeapAlloc(hHeap,0,LookupSize*sizeof(DWORD)+1);
763 char *pHintTable;
764 int HintSize,HintAllocSize;
765 HintSize=0;
766 HintAllocSize=128*2;
767 pHintTable=(char *)HeapAlloc(hHeap,0,HintAllocSize);
768 i3+=ImportDllNum*0x10;
769 for(i=0,i5=0;i<ImportDllNum;i++){
770 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
771 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
772 {
773 DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
774
775 if( !pDllProc->IsUsing() ){
776 continue;
777 }
778
779 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
780 //ルックアップの位置をセット(後にインポート アドレス テーブルにセットしなおされる)
781 pDllProc->SetLookupAddress( i3+(i5*sizeof(DWORD)) );
782
783 //ルックアップ テーブル
784 pLookupTable[i5++]=i3+LookupSize+HintSize;
785
786 //ヒント テーブル
787 pHintTable[HintSize++]=0;
788 pHintTable[HintSize++]=0;
789 lstrcpy(pHintTable+HintSize,pDllProc->GetAlias().c_str());
790 i4=(int)pDllProc->GetAlias().size();
791 HintSize+=i4+1;
792 if(i4%2==0) pHintTable[HintSize++]=0;
793
794 if(HintAllocSize<HintSize+128){
795 HintAllocSize+=128;
796 pHintTable=(char *)HeapReAlloc(hHeap,0,pHintTable,HintAllocSize);
797 }
798 }
799 }
800 pLookupTable[i5++]=0;
801 }
802
803
804 if( compiler.IsDll() ){
805 //DLLの場合はリロケーション情報を仮生成
806 //※正式な生成は各セクションのメモリ上のサイズが決定してから再度行う。
807 pobj_Reloc->ResetRelocBuffer();
808 }
809
810
811
812 //グローバル変数情報を扱う構造体も初期バッファの有無による配置を行う
813 //(デバッグ情報で利用される)
814 BOOST_FOREACH( Variable *pVar, compiler.GetObjectModule().meta.GetGlobalVars() ){
815 if(pVar->GetOffsetAddress()&0x80000000){
816 pVar->SetOffsetAddress(
817 (pVar->GetOffsetAddress()&0x7FFFFFFF)
818 + compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
819 );
820 }
821 }
822
823
824
825 ////////////////////////////////////
826 // デバッグセクションを生成
827 ////////////////////////////////////
828
829 //デバッグセクションを生成
830 CDebugSection *pobj_DebugSection;
831 pobj_DebugSection=new CDebugSection();
832 if( compiler.IsDebug() && !compiler.errorMessenger.HasError() )
833 {
834 compiler.messenger.Output( "デバッグ情報を生成しています。" );
835
836 pobj_DebugSection->make();
837
838 compiler.messenger.Output( "デバッグ情報の生成が完了しました。" );
839 }
840
841
842
843 /////////////////////////////////////
844 // 各セクションの位置とサイズを計算
845 /////////////////////////////////////
846
847 //コードセッションのファイル上のサイズ
848 if(compiler.linker.GetNativeCode().GetSize()%FILE_ALIGNMENT)
849 {
850 FileSize_CodeSection =
851 compiler.linker.GetNativeCode().GetSize()
852 + (FILE_ALIGNMENT-compiler.linker.GetNativeCode().GetSize()%FILE_ALIGNMENT);
853 }
854 else
855 {
856 FileSize_CodeSection = compiler.linker.GetNativeCode().GetSize();
857 }
858 if(FileSize_CodeSection) bUse_CodeSection=1;
859 else bUse_CodeSection=0;
860
861 //エクスポートセクションのファイル上のサイズ
862 i= sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
863 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
864 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
865 ExportNum*sizeof(WORD)+ //エクスポート序数テーブルを飛び越す
866 ExportNamesLength; //シンボル名バッファ
867 if(bUse_ExportSection){
868 if(i%FILE_ALIGNMENT) FileSize_ExportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
869 else FileSize_ExportSection=i;
870 }
871 else FileSize_ExportSection=0;
872
873 //インポートセクションのファイル上のサイズ
874 i=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+
875 16*ImportDllNum+ //DLL名
876 LookupSize+ //ルックアップテーブル
877 HintSize+ //ヒント名(関数名)テーブル
878 LookupSize; //インポート アドレス テーブル
879 if(i%FILE_ALIGNMENT) FileSize_ImportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
880 else FileSize_ImportSection=i;
881 bUse_ImportSection=1; //インポートセクションは必ず存在する
882
883 //データセクションのファイル上のサイズ
884 if(compiler.GetObjectModule().dataTable.GetSize()%FILE_ALIGNMENT) FileSize_DataSection=compiler.GetObjectModule().dataTable.GetSize()+(FILE_ALIGNMENT-compiler.GetObjectModule().dataTable.GetSize()%FILE_ALIGNMENT);
885 else FileSize_DataSection=compiler.GetObjectModule().dataTable.GetSize();
886 if(FileSize_DataSection) bUse_DataSection=1;
887 else bUse_DataSection=0;
888
889 //リライタブルセクションのファイル上のサイズ(グローバル変数の初期情報のみを格納)
890 if( compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize() % FILE_ALIGNMENT )
891 {
892 FileSize_RWSection =
893 compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
894 + (FILE_ALIGNMENT-compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()%FILE_ALIGNMENT);
895 }
896 else{
897 if( compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize() )
898 {
899 FileSize_RWSection = compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize();
900 }
901 else FileSize_RWSection=FILE_ALIGNMENT;
902 }
903 bUse_RWSection=1;
904
905 //リソースセクションのファイル上のサイズ
906 char *RSrcSectionBuffer;
907 int RSrcSectionSize;
908 RSrcSectionBuffer=GetRSrcSectionBuffer(&RSrcSectionSize);
909 if(RSrcSectionSize%FILE_ALIGNMENT) FileSize_RSrcSection=RSrcSectionSize+(FILE_ALIGNMENT-RSrcSectionSize%FILE_ALIGNMENT);
910 else FileSize_RSrcSection=RSrcSectionSize;
911 if(FileSize_RSrcSection) bUse_RSrcSection=1;
912 else bUse_RSrcSection=0;
913
914 //リロケーションセクションのファイル上のサイズ
915 if(pobj_Reloc->length%FILE_ALIGNMENT) FileSize_RelocSection=pobj_Reloc->length+(FILE_ALIGNMENT-pobj_Reloc->length%FILE_ALIGNMENT);
916 else FileSize_RelocSection=pobj_Reloc->length;
917 if(FileSize_RelocSection) bUse_RelocSection=1;
918 else bUse_RelocSection=0;
919
920 //デバッグセクションのファイル上のサイズ
921 if(pobj_DebugSection->length%FILE_ALIGNMENT) FileSize_DebugSection=pobj_DebugSection->length+(FILE_ALIGNMENT-pobj_DebugSection->length%FILE_ALIGNMENT);
922 else FileSize_DebugSection=pobj_DebugSection->length;
923 if(FileSize_DebugSection) bUse_DebugSection=1;
924 else bUse_DebugSection=0;
925
926
927 //各セッションのファイル上の位置
928 FilePos_CodeSection= EXE_HEADER_SIZE;
929 FilePos_ExportSection= FilePos_CodeSection+
930 FileSize_CodeSection;
931 FilePos_ImportSection= FilePos_ExportSection+
932 FileSize_ExportSection;
933 FilePos_DataSection= FilePos_ImportSection+
934 FileSize_ImportSection;
935 FilePos_RWSection= FilePos_DataSection+
936 FileSize_DataSection;
937 FilePos_RSrcSection= FilePos_RWSection+
938 FileSize_RWSection;
939 FilePos_RelocSection= FilePos_RSrcSection+
940 FileSize_RSrcSection;
941 FilePos_DebugSection= FilePos_RelocSection+
942 FileSize_RelocSection;
943
944
945 //コードセッションのメモリ上のサイズ
946 if(FileSize_CodeSection%MEM_ALIGNMENT) MemSize_CodeSection=FileSize_CodeSection+(MEM_ALIGNMENT-FileSize_CodeSection%MEM_ALIGNMENT);
947 else MemSize_CodeSection=FileSize_CodeSection;
948
949 //エクスポートセクションのメモリ上のサイズ
950 if(FileSize_ExportSection%MEM_ALIGNMENT) MemSize_ExportSection=FileSize_ExportSection+(MEM_ALIGNMENT-FileSize_ExportSection%MEM_ALIGNMENT);
951 else MemSize_ExportSection=FileSize_ExportSection;
952
953 //インポートセクションのメモリ上のサイズ
954 if(FileSize_ImportSection%MEM_ALIGNMENT) MemSize_ImportSection=FileSize_ImportSection+(MEM_ALIGNMENT-FileSize_ImportSection%MEM_ALIGNMENT);
955 else MemSize_ImportSection=FileSize_ImportSection;
956
957 //データセクションのメモリ上のサイズ
958 if(FileSize_DataSection%MEM_ALIGNMENT) MemSize_DataSection=FileSize_DataSection+(MEM_ALIGNMENT-FileSize_DataSection%MEM_ALIGNMENT);
959 else MemSize_DataSection=FileSize_DataSection;
960
961 //リライタブルセクションのメモリ上のサイズ
962 i = compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
963 + compiler.GetObjectModule().meta.GetGlobalVars().GetAllSize();
964 if(i%MEM_ALIGNMENT) MemSize_RWSection=i+(MEM_ALIGNMENT-i%MEM_ALIGNMENT);
965 else MemSize_RWSection=i;
966
967 //リソースセクションのメモリ上のサイズ
968 if(FileSize_RSrcSection%MEM_ALIGNMENT) MemSize_RSrcSection=FileSize_RSrcSection+(MEM_ALIGNMENT-FileSize_RSrcSection%MEM_ALIGNMENT);
969 else MemSize_RSrcSection=FileSize_RSrcSection;
970
971 //リロケーションセクションのメモリ上のサイズ
972 if(FileSize_RelocSection%MEM_ALIGNMENT) MemSize_RelocSection=FileSize_RelocSection+(MEM_ALIGNMENT-FileSize_RelocSection%MEM_ALIGNMENT);
973 else MemSize_RelocSection=FileSize_RelocSection;
974
975 //デバッグセクションのメモリ上のサイズ
976 if(FileSize_DebugSection%MEM_ALIGNMENT) MemSize_DebugSection=FileSize_DebugSection+(MEM_ALIGNMENT-FileSize_DebugSection%MEM_ALIGNMENT);
977 else MemSize_DebugSection=FileSize_DebugSection;
978
979
980 //各セッションのメモリ上の位置
981 if(bUse_ExportSection) MemPos_ExportSection= 0x1000+
982 MemSize_CodeSection;
983 else MemPos_ExportSection=0;
984 if(bUse_ImportSection) MemPos_ImportSection= 0x1000+
985 MemSize_CodeSection+
986 MemSize_ExportSection;
987 else MemPos_ImportSection=0;
988 if(bUse_DataSection) MemPos_DataSection= 0x1000+
989 MemSize_CodeSection+
990 MemSize_ExportSection+
991 MemSize_ImportSection;
992 else MemPos_DataSection=0;
993 if(bUse_RWSection) MemPos_RWSection= 0x1000+
994 MemSize_CodeSection+
995 MemSize_ExportSection+
996 MemSize_ImportSection+
997 MemSize_DataSection;
998 else MemPos_RWSection=0;
999 if(bUse_RSrcSection) MemPos_RSrcSection= 0x1000+
1000 MemSize_CodeSection+
1001 MemSize_ExportSection+
1002 MemSize_ImportSection+
1003 MemSize_DataSection+
1004 MemSize_RWSection;
1005 else MemPos_RSrcSection=0;
1006 if(bUse_RelocSection) MemPos_RelocSection= 0x1000+
1007 MemSize_CodeSection+
1008 MemSize_ExportSection+
1009 MemSize_ImportSection+
1010 MemSize_DataSection+
1011 MemSize_RWSection+
1012 MemSize_RSrcSection;
1013 else MemPos_RelocSection=0;
1014 if(bUse_DebugSection) MemPos_DebugSection= 0x1000+
1015 MemSize_CodeSection+
1016 MemSize_ExportSection+
1017 MemSize_ImportSection+
1018 MemSize_DataSection+
1019 MemSize_RWSection+
1020 MemSize_RSrcSection+
1021 MemSize_RelocSection;
1022 else MemPos_DebugSection=0;
1023
1024
1025
1026 ////////////////////////////
1027 // エクスポート情報の再配置
1028 ////////////////////////////
1029 if(bUse_ExportSection){
1030 for(i=0;i<ExportNum;i++){
1031 lpdwExportAddressTable[i]+=MemPos_CodeSection;
1032 lpdwExportNamePointerTable[i]+=MemPos_ExportSection;
1033 }
1034
1035 ImageExportDirectory.Name+= MemPos_ExportSection;
1036 ImageExportDirectory.AddressOfFunctions+= MemPos_ExportSection;
1037 ImageExportDirectory.AddressOfNames+= MemPos_ExportSection;
1038 ImageExportDirectory.AddressOfNameOrdinals+= MemPos_ExportSection;
1039 }
1040
1041
1042 ////////////////////////////
1043 // インポート情報の再配置
1044 ////////////////////////////
1045 for(i=0,i5=0;i<ImportDllNum;i++){
1046 //インポート テーブル(IMAGE_IMPORT_DESCRIPTOR)
1047 pImportTable[i].OriginalFirstThunk+=MemPos_ImportSection;
1048 pImportTable[i].Name+=MemPos_ImportSection;
1049
1050 //インポート アドレス テーブル(ルックアップとヒントを飛び越す)
1051 pImportTable[i].FirstThunk=pImportTable[i].OriginalFirstThunk+
1052 LookupSize+ //ルックアップテーブル
1053 HintSize; //ヒント名(関数名)テーブル
1054
1055 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
1056 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
1057 {
1058 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
1059
1060 if( !pDllProc->IsUsing() ){
1061 continue;
1062 }
1063
1064 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
1065 //ルックアップ テーブル
1066 pLookupTable[i5++]+=MemPos_ImportSection;
1067 }
1068 }
1069 i5++;
1070 }
1071
1072
1073 ////////////////////////////////////////
1074 //仮想関数データテーブルスケジュール
1075 compiler.GetObjectModule().meta.GetClasses().ActionVtblSchedule( ImageBase, MemPos_CodeSection, MemPos_DataSection );
1076
1077
1078 if( compiler.IsDll() ){
1079 //DLLの場合はリロケーション情報を生成
1080 pobj_Reloc->ResetRelocBuffer();
1081 }
1082
1083 compiler.linker.SetDataTable( compiler.GetObjectModule().dataTable );
1084
1085 compiler.linker.SetImageBase( ImageBase );
1086 compiler.linker.ResolveDataTableSchedules( MemPos_DataSection );
1087 compiler.linker.ResolveCatchAddressSchedules( MemPos_CodeSection );
1088 compiler.linker.ResolveDllProcSchedules( MemPos_CodeSection, MemPos_ImportSection, LookupSize, HintSize );
1089 compiler.linker.ResolveUserProcSchedules( MemPos_CodeSection );
1090 compiler.linker.ResolveGlobalVarSchedules( MemPos_RWSection );
1091 compiler.linker.ResolveVtblSchedule( MemPos_DataSection );
1092 compiler.linker.ResolveTypeInfoSchedule( MemPos_DataSection );
1093
1094
1095
1096 ////////////////////////////////
1097 // リソースアドレススケジュール
1098 extern DWORD *lpdwRSrcAddrSchedule;
1099 extern int RSrcAddrScheduleNum;
1100 for(i=0;i<RSrcAddrScheduleNum;i++){
1101 *(DWORD *)(RSrcSectionBuffer+lpdwRSrcAddrSchedule[i])+=MemPos_RSrcSection;
1102 }
1103 HeapDefaultFree(lpdwRSrcAddrSchedule);
1104
1105
1106 //Dosスタブ
1107 char *DosStubBuffer;
1108 int DosStubSize;
1109 hFile=CreateFile(
1110 ( ActiveBasic::Common::Environment::GetAbdevSystemDirPath() + "\\dosstub.pgm" ).c_str(),
1111 GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
1112 if(hFile==INVALID_HANDLE_VALUE){
1113 MessageBox(hOwnerEditor,"dosstub.pgmの読み込みに失敗","error",MB_OK);
1114 goto EndWriteOpcode;
1115 }
1116 DosStubSize=GetFileSize(hFile,NULL);
1117 DosStubBuffer=(char *)HeapAlloc(hHeap,0,DosStubSize);
1118 ReadFile(hFile,DosStubBuffer,DosStubSize,(DWORD *)&i2,NULL);
1119 CloseHandle(hFile);
1120
1121
1122 if( compiler.errorMessenger.HasError() )
1123 {
1124 goto EndWriteOpcode;
1125 }
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(program.GetOutputFilePath().c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
1415 if(hFile==INVALID_HANDLE_VALUE){
1416 compiler.errorMessenger.Output(53,program.GetOutputFilePath().c_str(),-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.