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

Last change on this file since 279 was 279, checked in by dai_9181, 17 years ago

sourceをObjectModuleに入れた

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