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

Last change on this file since 281 was 281, checked in by dai_9181, 17 years ago
File size: 53.9 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 if( compiler.GetObjectModule().meta.GetGlobalVars().Find( Symbol( "_DebugSys_dwThreadID" ) ) == NULL )
109 {
110 // 未定義の場合は定義する
111 sprintf(temporary,"_DebugSys_dwThreadID[255]%c%cDWord",1,ESC_AS);
112 OpcodeDim(temporary,DIMFLAG_INITDEBUGVAR);
113 sprintf(temporary,"_DebugSys_ProcNum[255]%c%cDWord",1,ESC_AS);
114 OpcodeDim(temporary,DIMFLAG_INITDEBUGVAR);
115 sprintf(temporary,"_DebugSys_lplpObp[255]%c%c*ULONG_PTR",1,ESC_AS);
116 OpcodeDim(temporary,DIMFLAG_INITDEBUGVAR);
117 sprintf(temporary,"_DebugSys_lplpSpBase[255]%c%c*ULONG_PTR",1,ESC_AS);
118 OpcodeDim(temporary,DIMFLAG_INITDEBUGVAR);
119 }
120}
121
122void Compile(void){
123 extern HWND hMainDlg;
124 extern HWND hOwnerEditor;
125 extern HANDLE hHeap;
126 extern BOOL bDebugCompile;
127 extern DWORD ImageBase;
128 extern char *basbuf;
129 extern char OutputFileName[MAX_PATH];
130
131 int i,i2,i3,i4,i5;
132 char temporary[MAX_PATH],*temp2;
133 HANDLE hFile;
134
135
136 //コードセクションのメモリ上の位置
137 MemPos_CodeSection= 0x1000;
138
139 //データセクションのメモリ上の位置(仮定)
140 MemPos_DataSection= 0x10000000;
141
142
143 //エクスポート セクションを利用するかどうか
144 if( compiler.IsDll() ) bUse_ExportSection=1;
145 else bUse_ExportSection=0;
146
147
148 // 静的リンク
149 compiler.StaticLink( compiler.staticLibraries );
150
151
152 //////////////////
153 // データテーブル
154 if(bDebugCompile){
155 compiler.GetObjectModule().dataTable.Add( (long)0x00000002 );
156 }
157
158
159 //////////////////////
160 // コード生成前の準備
161 //////////////////////
162
163 //重複エラー情報管理のメモリを確保(グローバル領域コンパイル用)
164 extern char **SynonymErrorWords;
165 extern int SynonymErrorNum;
166 SynonymErrorNum=0;
167 SynonymErrorWords=(char **)HeapAlloc(hHeap,0,1);
168
169 //列挙体に関する情報の初期化
170 CEnumParent::InitEnum();
171
172 //列挙体からクラスコードを生成
173 char *temp;
174 temp=CEnumParent::GenerateCodes();
175 AddSourceCode(temp);
176 HeapDefaultFree(temp);
177
178 // 名前空間情報を取得
179 NamespaceSupporter::CollectNamespaces(
180 compiler.GetObjectModule().GetCurrentSource().GetBuffer(),
181 compiler.GetObjectModule().meta.GetNamespaces()
182 );
183
184 //クラス名を取得(詳細情報はGetAllClassInfoで取得)
185 // CollectProcedures関数の中で参照されるオブジェクト名を事前に取得する。
186 // ※オブジェクトの内容までは取得しない
187 compiler.GetObjectModule().meta.GetClasses().CollectClassesForNameOnly( compiler.GetObjectModule().GetCurrentSource() );
188
189 //TypeDef情報を初期化
190 compiler.GetObjectModule().meta.GetTypeDefs().CollectTypeDefs();
191
192 //定数情報を取得
193 GetConstInfo();
194
195 // サブルーチン(ユーザー定義、DLL関数)の識別子、アドレスを取得
196 compiler.pCompilingClass = NULL;
197 CollectProcedures(
198 compiler.GetObjectModule().GetCurrentSource(),
199 compiler.GetObjectModule().meta.GetUserProcs(),
200 compiler.GetObjectModule().meta.GetDllProcs()
201 );
202
203 // クラス情報を取得(※注 - CollectProceduresの後に呼び出す)
204 compiler.GetObjectModule().meta.GetClasses().GetAllClassInfo();
205
206 // サブルーチン(ユーザー定義、DLL関数)のイテレータの準備
207 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Init();
208 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Init();
209
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 extern SourceLines oldSourceLines;
508 oldSourceLines = compiler.linker.GetNativeCode().GetSourceLines();
509
510
511
512 ////////////////////////////////
513 // ここで一旦ログを取る
514 ////////////////////////////////
515 Diagnose();
516
517
518
519 ////////////////////////////////
520 // 使用するDLL関数のチェック
521 ////////////////////////////////
522 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
523 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
524 {
525 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
526
527 if( !pDllProc->IsUsing() ){
528 continue;
529 }
530
531 //エラーチェック
532 HINSTANCE hLib;
533 hLib=LoadLibrary( pDllProc->GetDllFileName().c_str() );
534 if(!hLib){
535 extern char OutputFileName[MAX_PATH];
536 char temp2[MAX_PATH],temp3[MAX_PATH];
537 _splitpath(OutputFileName,temp2,temp3,NULL,NULL);
538 lstrcat(temp2,temp3);
539 lstrcpy(temp3,pDllProc->GetDllFileName().c_str());
540 GetFullPath(temp3,temp2);
541 hLib=LoadLibrary(temp3);
542
543 if(!hLib){
544 SetError(-106,pDllProc->GetDllFileName().c_str(),pDllProc->GetCodePos());
545 }
546 }
547
548 if(hLib){
549 if(!GetProcAddress(hLib,pDllProc->GetAlias().c_str())){
550 FreeLibrary(hLib);
551 SetError(-107,pDllProc->GetAlias(),pDllProc->GetCodePos());
552 }
553 FreeLibrary(hLib);
554 }
555 }
556
557
558
559 /////////////////////////////
560 // リソースデータを読み込む
561 /////////////////////////////
562 extern char ResourceFileName[MAX_PATH];
563 GetResourceData(ResourceFileName);
564
565
566 //////////////////////////////
567 // エクスポート情報(DLLのみ)
568 //////////////////////////////
569 IMAGE_EXPORT_DIRECTORY ImageExportDirectory;
570 DWORD *lpdwExportAddressTable;
571 DWORD *lpdwExportNamePointerTable;
572 WORD *lpwExportOrdinalTable;
573 char lpExportNames[8192];
574 int ExportNamesLength;
575 int ExportNum;
576 int DllMain_EntryPoint;
577
578 DllMain_EntryPoint=-1;
579 ExportNum=0;
580 ExportNamesLength=0;
581 lpdwExportAddressTable=(DWORD *)HeapAlloc(hHeap,0,1);
582 lpdwExportNamePointerTable=(DWORD *)HeapAlloc(hHeap,0,1);
583 lpwExportOrdinalTable=(WORD *)HeapAlloc(hHeap,0,1);
584
585 if(bUse_ExportSection){
586 _splitpath(OutputFileName,NULL,NULL,lpExportNames,temporary);
587 lstrcat(lpExportNames,temporary);
588 ExportNamesLength=lstrlen(lpExportNames)+1;
589
590 UserProc *pUserProc,*psi2;
591 while(1){
592 //辞書順にサーチ
593 temporary[0]=0;
594 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
595 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
596 {
597 pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
598 if(pUserProc->IsExport()){
599 if(temporary[0]=='\0'){
600 lstrcpy(temporary,pUserProc->GetName().c_str());
601 psi2=pUserProc;
602 }
603 else{
604 i3=lstrlen(temporary);
605 i4=(int)pUserProc->GetName().size();
606 if(i3<i4) i3=i4;
607 if(memcmp(temporary,pUserProc->GetName().c_str(),i3)>0){
608 lstrcpy(temporary,pUserProc->GetName().c_str());
609 psi2=pUserProc;
610 }
611 }
612 }
613 }
614 if(psi2==0) break;
615 pUserProc=psi2;
616
617 pUserProc->ExportOff();
618
619 if( pUserProc->GetName() == "DllMain" ){
620 DllMain_EntryPoint = pUserProc->GetBeginOpAddress();
621 }
622
623 lpdwExportAddressTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportAddressTable,(ExportNum+1)*sizeof(DWORD));
624 lpdwExportAddressTable[ExportNum] = pUserProc->GetBeginOpAddress();
625
626 lpdwExportNamePointerTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportNamePointerTable,(ExportNum+1)*sizeof(DWORD));
627 lpdwExportNamePointerTable[ExportNum]=ExportNamesLength;
628
629 lpwExportOrdinalTable=(WORD *)HeapReAlloc(hHeap,0,lpwExportOrdinalTable,(ExportNum+1)*sizeof(WORD));
630 lpwExportOrdinalTable[ExportNum]=ExportNum;
631
632 lstrcpy(lpExportNames+ExportNamesLength,pUserProc->GetName().c_str());
633 ExportNamesLength+=lstrlen(lpExportNames+ExportNamesLength)+1;
634
635 ExportNum++;
636 }
637 for(i=0;i<ExportNum;i++){
638 lpdwExportNamePointerTable[i]+=
639 sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
640 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
641 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
642 ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
643 }
644
645 ImageExportDirectory.Characteristics=0;
646 ImageExportDirectory.TimeDateStamp=(DWORD)time(NULL);
647 ImageExportDirectory.MajorVersion=0;
648 ImageExportDirectory.MinorVersion=0;
649 ImageExportDirectory.Name=sizeof(IMAGE_EXPORT_DIRECTORY)+
650 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
651 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
652 ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
653 ImageExportDirectory.Base=1;
654 ImageExportDirectory.NumberOfFunctions=ExportNum;
655 ImageExportDirectory.NumberOfNames=ExportNum;
656 ImageExportDirectory.AddressOfFunctions=sizeof(IMAGE_EXPORT_DIRECTORY);
657 ImageExportDirectory.AddressOfNames=ImageExportDirectory.AddressOfFunctions+ExportNum*sizeof(DWORD);
658 ImageExportDirectory.AddressOfNameOrdinals=ImageExportDirectory.AddressOfNames+ExportNum*sizeof(DWORD);
659 }
660
661
662 /////////////////////
663 //インポートDLL情報
664 /////////////////////
665
666 /*
667 IMAGE_IMPORT_DESCRIPTOR1[size=0x14] インポートヘッダ
668 IMAGE_IMPORT_DESCRIPTOR2[size=0x14]
669 IMAGE_IMPORT_DESCRIPTOR3[size=0x14]
670 ...
671 Dll名1[size=0x10]
672 Dll名2[size=0x10]
673 Dll名3[size=0x10]
674 ...
675 ルックアップ テーブル(ヒントを示すRVA)
676 ヒントテーブル
677 インポート アドレス テーブル(ルックアップと同じ内容だが、プログラム実行時に実行アドレスに書き換えられる)
678 */
679
680 char **ppDllNames=(char **)HeapAlloc(hHeap,0,1);
681
682 int ImportDllNum=0;
683
684 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
685 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
686 {
687 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
688
689 if( !pDllProc->IsUsing() ){
690 continue;
691 }
692
693 if( pDllProc->GetDllFileName().size() > 16 ){
694 SetError(7,NULL,cp);
695 break;
696 }
697 for(i2=0;i2<ImportDllNum;i2++){
698 if( pDllProc->GetDllFileName() == ppDllNames[i2] ){
699 break;
700 }
701 }
702 if(i2==ImportDllNum){
703 ppDllNames=(char **)HeapReAlloc(hHeap,0,ppDllNames,(ImportDllNum+1)*sizeof(char **));
704 ppDllNames[ImportDllNum]=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,16);
705 lstrcpy(ppDllNames[ImportDllNum],pDllProc->GetDllFileName().c_str());
706 ImportDllNum++;
707 }
708 }
709
710 //インポート テーブル、及びルックアップ テーブル サイズの計算
711 IMAGE_IMPORT_DESCRIPTOR *pImportTable;
712 int LookupSize;
713 LookupSize=0;
714 pImportTable=(IMAGE_IMPORT_DESCRIPTOR *)HeapAlloc(hHeap,0,(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR));
715 i3=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR);
716 for(i=0;i<ImportDllNum;i++){
717 //インポート テーブル(IMAGE_IMPORT_DESCRIPTOR)
718 pImportTable[i].OriginalFirstThunk=i3+(ImportDllNum*0x10)+LookupSize;
719 pImportTable[i].TimeDateStamp=0;
720 pImportTable[i].ForwarderChain=0;
721 pImportTable[i].Name=i3+i*0x10;
722
723 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
724 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
725 {
726 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
727
728 if( !pDllProc->IsUsing() ){
729 continue;
730 }
731
732 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
733 //ルックアップデータのサイズを追加
734 LookupSize+=sizeof(DWORD);
735 }
736 }
737 LookupSize+=sizeof(DWORD); //NULL用
738 }
739 memset(&pImportTable[i],0,sizeof(IMAGE_IMPORT_DESCRIPTOR));
740
741 //ルックアップ テーブル、ヒント テーブル
742 DWORD *pLookupTable;
743 pLookupTable=(DWORD *)HeapAlloc(hHeap,0,LookupSize*sizeof(DWORD)+1);
744 char *pHintTable;
745 int HintSize,HintAllocSize;
746 HintSize=0;
747 HintAllocSize=128*2;
748 pHintTable=(char *)HeapAlloc(hHeap,0,HintAllocSize);
749 i3+=ImportDllNum*0x10;
750 for(i=0,i5=0;i<ImportDllNum;i++){
751 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
752 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
753 {
754 DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
755
756 if( !pDllProc->IsUsing() ){
757 continue;
758 }
759
760 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
761 //ルックアップの位置をセット(後にインポート アドレス テーブルにセットしなおされる)
762 pDllProc->SetLookupAddress( i3+(i5*sizeof(DWORD)) );
763
764 //ルックアップ テーブル
765 pLookupTable[i5++]=i3+LookupSize+HintSize;
766
767 //ヒント テーブル
768 pHintTable[HintSize++]=0;
769 pHintTable[HintSize++]=0;
770 lstrcpy(pHintTable+HintSize,pDllProc->GetAlias().c_str());
771 i4=(int)pDllProc->GetAlias().size();
772 HintSize+=i4+1;
773 if(i4%2==0) pHintTable[HintSize++]=0;
774
775 if(HintAllocSize<HintSize+128){
776 HintAllocSize+=128;
777 pHintTable=(char *)HeapReAlloc(hHeap,0,pHintTable,HintAllocSize);
778 }
779 }
780 }
781 pLookupTable[i5++]=0;
782 }
783
784
785 if( compiler.IsDll() ){
786 //DLLの場合はリロケーション情報を仮生成
787 //※正式な生成は各セクションのメモリ上のサイズが決定してから再度行う。
788 pobj_Reloc->ResetRelocBuffer();
789 }
790
791
792
793 //グローバル変数情報を扱う構造体も初期バッファの有無による配置を行う
794 //(デバッグ情報で利用される)
795 BOOST_FOREACH( Variable *pVar, compiler.GetObjectModule().meta.GetGlobalVars() ){
796 if(pVar->GetOffsetAddress()&0x80000000){
797 pVar->SetOffsetAddress(
798 (pVar->GetOffsetAddress()&0x7FFFFFFF)
799 + compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize()
800 );
801 }
802 }
803
804
805
806 ////////////////////////////////////
807 // デバッグセクションを生成
808 ////////////////////////////////////
809
810 //デバッグセクションを生成
811 CDebugSection *pobj_DebugSection;
812 pobj_DebugSection=new CDebugSection();
813 extern BOOL bDebugCompile;
814 extern BOOL bError;
815 if(bDebugCompile&&bError==0){
816 pobj_DebugSection->make();
817 }
818
819
820
821 /////////////////////////////////////
822 // 各セクションの位置とサイズを計算
823 /////////////////////////////////////
824
825 //コードセッションのファイル上のサイズ
826 if(compiler.linker.GetNativeCode().GetSize()%FILE_ALIGNMENT)
827 {
828 FileSize_CodeSection =
829 compiler.linker.GetNativeCode().GetSize()
830 + (FILE_ALIGNMENT-compiler.linker.GetNativeCode().GetSize()%FILE_ALIGNMENT);
831 }
832 else
833 {
834 FileSize_CodeSection = compiler.linker.GetNativeCode().GetSize();
835 }
836 if(FileSize_CodeSection) bUse_CodeSection=1;
837 else bUse_CodeSection=0;
838
839 //エクスポートセクションのファイル上のサイズ
840 i= sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
841 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
842 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
843 ExportNum*sizeof(WORD)+ //エクスポート序数テーブルを飛び越す
844 ExportNamesLength; //シンボル名バッファ
845 if(bUse_ExportSection){
846 if(i%FILE_ALIGNMENT) FileSize_ExportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
847 else FileSize_ExportSection=i;
848 }
849 else FileSize_ExportSection=0;
850
851 //インポートセクションのファイル上のサイズ
852 i=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+
853 16*ImportDllNum+ //DLL名
854 LookupSize+ //ルックアップテーブル
855 HintSize+ //ヒント名(関数名)テーブル
856 LookupSize; //インポート アドレス テーブル
857 if(i%FILE_ALIGNMENT) FileSize_ImportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
858 else FileSize_ImportSection=i;
859 bUse_ImportSection=1; //インポートセクションは必ず存在する
860
861 //データセクションのファイル上のサイズ
862 if(compiler.GetObjectModule().dataTable.GetSize()%FILE_ALIGNMENT) FileSize_DataSection=compiler.GetObjectModule().dataTable.GetSize()+(FILE_ALIGNMENT-compiler.GetObjectModule().dataTable.GetSize()%FILE_ALIGNMENT);
863 else FileSize_DataSection=compiler.GetObjectModule().dataTable.GetSize();
864 if(FileSize_DataSection) bUse_DataSection=1;
865 else bUse_DataSection=0;
866
867 //リライタブルセクションのファイル上のサイズ(グローバル変数の初期情報のみを格納)
868 if( compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize() % FILE_ALIGNMENT )
869 {
870 FileSize_RWSection =
871 compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize()
872 + (FILE_ALIGNMENT-compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize()%FILE_ALIGNMENT);
873 }
874 else{
875 if( compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize() )
876 {
877 FileSize_RWSection = compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize();
878 }
879 else FileSize_RWSection=FILE_ALIGNMENT;
880 }
881 bUse_RWSection=1;
882
883 //リソースセクションのファイル上のサイズ
884 char *RSrcSectionBuffer;
885 int RSrcSectionSize;
886 RSrcSectionBuffer=GetRSrcSectionBuffer(&RSrcSectionSize);
887 if(RSrcSectionSize%FILE_ALIGNMENT) FileSize_RSrcSection=RSrcSectionSize+(FILE_ALIGNMENT-RSrcSectionSize%FILE_ALIGNMENT);
888 else FileSize_RSrcSection=RSrcSectionSize;
889 if(FileSize_RSrcSection) bUse_RSrcSection=1;
890 else bUse_RSrcSection=0;
891
892 //リロケーションセクションのファイル上のサイズ
893 if(pobj_Reloc->length%FILE_ALIGNMENT) FileSize_RelocSection=pobj_Reloc->length+(FILE_ALIGNMENT-pobj_Reloc->length%FILE_ALIGNMENT);
894 else FileSize_RelocSection=pobj_Reloc->length;
895 if(FileSize_RelocSection) bUse_RelocSection=1;
896 else bUse_RelocSection=0;
897
898 //デバッグセクションのファイル上のサイズ
899 if(pobj_DebugSection->length%FILE_ALIGNMENT) FileSize_DebugSection=pobj_DebugSection->length+(FILE_ALIGNMENT-pobj_DebugSection->length%FILE_ALIGNMENT);
900 else FileSize_DebugSection=pobj_DebugSection->length;
901 if(FileSize_DebugSection) bUse_DebugSection=1;
902 else bUse_DebugSection=0;
903
904
905 //各セッションのファイル上の位置
906 FilePos_CodeSection= EXE_HEADER_SIZE;
907 FilePos_ExportSection= FilePos_CodeSection+
908 FileSize_CodeSection;
909 FilePos_ImportSection= FilePos_ExportSection+
910 FileSize_ExportSection;
911 FilePos_DataSection= FilePos_ImportSection+
912 FileSize_ImportSection;
913 FilePos_RWSection= FilePos_DataSection+
914 FileSize_DataSection;
915 FilePos_RSrcSection= FilePos_RWSection+
916 FileSize_RWSection;
917 FilePos_RelocSection= FilePos_RSrcSection+
918 FileSize_RSrcSection;
919 FilePos_DebugSection= FilePos_RelocSection+
920 FileSize_RelocSection;
921
922
923 //コードセッションのメモリ上のサイズ
924 if(FileSize_CodeSection%MEM_ALIGNMENT) MemSize_CodeSection=FileSize_CodeSection+(MEM_ALIGNMENT-FileSize_CodeSection%MEM_ALIGNMENT);
925 else MemSize_CodeSection=FileSize_CodeSection;
926
927 //エクスポートセクションのメモリ上のサイズ
928 if(FileSize_ExportSection%MEM_ALIGNMENT) MemSize_ExportSection=FileSize_ExportSection+(MEM_ALIGNMENT-FileSize_ExportSection%MEM_ALIGNMENT);
929 else MemSize_ExportSection=FileSize_ExportSection;
930
931 //インポートセクションのメモリ上のサイズ
932 if(FileSize_ImportSection%MEM_ALIGNMENT) MemSize_ImportSection=FileSize_ImportSection+(MEM_ALIGNMENT-FileSize_ImportSection%MEM_ALIGNMENT);
933 else MemSize_ImportSection=FileSize_ImportSection;
934
935 //データセクションのメモリ上のサイズ
936 if(FileSize_DataSection%MEM_ALIGNMENT) MemSize_DataSection=FileSize_DataSection+(MEM_ALIGNMENT-FileSize_DataSection%MEM_ALIGNMENT);
937 else MemSize_DataSection=FileSize_DataSection;
938
939 //リライタブルセクションのメモリ上のサイズ
940 i = compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize()
941 + compiler.GetObjectModule().meta.GetGlobalVars().GetAllSize();
942 if(i%MEM_ALIGNMENT) MemSize_RWSection=i+(MEM_ALIGNMENT-i%MEM_ALIGNMENT);
943 else MemSize_RWSection=i;
944
945 //リソースセクションのメモリ上のサイズ
946 if(FileSize_RSrcSection%MEM_ALIGNMENT) MemSize_RSrcSection=FileSize_RSrcSection+(MEM_ALIGNMENT-FileSize_RSrcSection%MEM_ALIGNMENT);
947 else MemSize_RSrcSection=FileSize_RSrcSection;
948
949 //リロケーションセクションのメモリ上のサイズ
950 if(FileSize_RelocSection%MEM_ALIGNMENT) MemSize_RelocSection=FileSize_RelocSection+(MEM_ALIGNMENT-FileSize_RelocSection%MEM_ALIGNMENT);
951 else MemSize_RelocSection=FileSize_RelocSection;
952
953 //デバッグセクションのメモリ上のサイズ
954 if(FileSize_DebugSection%MEM_ALIGNMENT) MemSize_DebugSection=FileSize_DebugSection+(MEM_ALIGNMENT-FileSize_DebugSection%MEM_ALIGNMENT);
955 else MemSize_DebugSection=FileSize_DebugSection;
956
957
958 //各セッションのメモリ上の位置
959 if(bUse_ExportSection) MemPos_ExportSection= 0x1000+
960 MemSize_CodeSection;
961 else MemPos_ExportSection=0;
962 if(bUse_ImportSection) MemPos_ImportSection= 0x1000+
963 MemSize_CodeSection+
964 MemSize_ExportSection;
965 else MemPos_ImportSection=0;
966 if(bUse_DataSection) MemPos_DataSection= 0x1000+
967 MemSize_CodeSection+
968 MemSize_ExportSection+
969 MemSize_ImportSection;
970 else MemPos_DataSection=0;
971 if(bUse_RWSection) MemPos_RWSection= 0x1000+
972 MemSize_CodeSection+
973 MemSize_ExportSection+
974 MemSize_ImportSection+
975 MemSize_DataSection;
976 else MemPos_RWSection=0;
977 if(bUse_RSrcSection) MemPos_RSrcSection= 0x1000+
978 MemSize_CodeSection+
979 MemSize_ExportSection+
980 MemSize_ImportSection+
981 MemSize_DataSection+
982 MemSize_RWSection;
983 else MemPos_RSrcSection=0;
984 if(bUse_RelocSection) MemPos_RelocSection= 0x1000+
985 MemSize_CodeSection+
986 MemSize_ExportSection+
987 MemSize_ImportSection+
988 MemSize_DataSection+
989 MemSize_RWSection+
990 MemSize_RSrcSection;
991 else MemPos_RelocSection=0;
992 if(bUse_DebugSection) MemPos_DebugSection= 0x1000+
993 MemSize_CodeSection+
994 MemSize_ExportSection+
995 MemSize_ImportSection+
996 MemSize_DataSection+
997 MemSize_RWSection+
998 MemSize_RSrcSection+
999 MemSize_RelocSection;
1000 else MemPos_DebugSection=0;
1001
1002
1003
1004 ////////////////////////////
1005 // エクスポート情報の再配置
1006 ////////////////////////////
1007 if(bUse_ExportSection){
1008 for(i=0;i<ExportNum;i++){
1009 lpdwExportAddressTable[i]+=MemPos_CodeSection;
1010 lpdwExportNamePointerTable[i]+=MemPos_ExportSection;
1011 }
1012
1013 ImageExportDirectory.Name+= MemPos_ExportSection;
1014 ImageExportDirectory.AddressOfFunctions+= MemPos_ExportSection;
1015 ImageExportDirectory.AddressOfNames+= MemPos_ExportSection;
1016 ImageExportDirectory.AddressOfNameOrdinals+= MemPos_ExportSection;
1017 }
1018
1019
1020 ////////////////////////////
1021 // インポート情報の再配置
1022 ////////////////////////////
1023 for(i=0,i5=0;i<ImportDllNum;i++){
1024 //インポート テーブル(IMAGE_IMPORT_DESCRIPTOR)
1025 pImportTable[i].OriginalFirstThunk+=MemPos_ImportSection;
1026 pImportTable[i].Name+=MemPos_ImportSection;
1027
1028 //インポート アドレス テーブル(ルックアップとヒントを飛び越す)
1029 pImportTable[i].FirstThunk=pImportTable[i].OriginalFirstThunk+
1030 LookupSize+ //ルックアップテーブル
1031 HintSize; //ヒント名(関数名)テーブル
1032
1033 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
1034 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
1035 {
1036 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
1037
1038 if( !pDllProc->IsUsing() ){
1039 continue;
1040 }
1041
1042 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
1043 //ルックアップ テーブル
1044 pLookupTable[i5++]+=MemPos_ImportSection;
1045 }
1046 }
1047 i5++;
1048 }
1049
1050
1051 ////////////////////////////////////////
1052 //仮想関数データテーブルスケジュール
1053 compiler.GetObjectModule().meta.GetClasses().ActionVtblSchedule(ImageBase,MemPos_CodeSection);
1054
1055
1056 if( compiler.IsDll() ){
1057 //DLLの場合はリロケーション情報を生成
1058 pobj_Reloc->ResetRelocBuffer();
1059 }
1060
1061
1062 compiler.linker.SetImageBase( ImageBase );
1063 compiler.linker.ResolveDataTableSchedules( MemPos_DataSection );
1064 compiler.linker.ResolveDllProcSchedules( MemPos_CodeSection, MemPos_ImportSection, LookupSize, HintSize );
1065 compiler.linker.ResolveUserProcSchedules( MemPos_CodeSection );
1066 compiler.linker.ResolveGlobalVarSchedules( MemPos_RWSection );
1067
1068
1069
1070 ////////////////////////////////
1071 // リソースアドレススケジュール
1072 extern DWORD *lpdwRSrcAddrSchedule;
1073 extern int RSrcAddrScheduleNum;
1074 for(i=0;i<RSrcAddrScheduleNum;i++){
1075 *(DWORD *)(RSrcSectionBuffer+lpdwRSrcAddrSchedule[i])+=MemPos_RSrcSection;
1076 }
1077 HeapDefaultFree(lpdwRSrcAddrSchedule);
1078
1079
1080 //Dosスタブ
1081 char *DosStubBuffer;
1082 int DosStubSize;
1083 hFile=CreateFile(
1084 ( Jenga::Common::Environment::GetAppDir() + "\\SubOperation\\dosstub.pgm" ).c_str(),
1085 GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
1086 if(hFile==INVALID_HANDLE_VALUE){
1087 MessageBox(hOwnerEditor,"dosstub.pgmの読み込みに失敗","error",MB_OK);
1088 goto EndWriteOpcode;
1089 }
1090 DosStubSize=GetFileSize(hFile,NULL);
1091 DosStubBuffer=(char *)HeapAlloc(hHeap,0,DosStubSize);
1092 ReadFile(hFile,DosStubBuffer,DosStubSize,(DWORD *)&i2,NULL);
1093 CloseHandle(hFile);
1094
1095
1096 extern BOOL bError;
1097 if(bError) goto EndWriteOpcode;
1098
1099
1100 ////////////////////////////
1101 // EXEファイルのヘッダ情報
1102 ////////////////////////////
1103
1104 IMAGE_DOS_HEADER ImageDosHeader;
1105 ImageDosHeader.e_magic= 0x5A4D;
1106 ImageDosHeader.e_cblp= 0x0090;
1107 ImageDosHeader.e_cp= 0x0003;
1108 ImageDosHeader.e_crlc= 0;
1109 ImageDosHeader.e_cparhdr=4;
1110 ImageDosHeader.e_minalloc=0x0000;
1111 ImageDosHeader.e_maxalloc=0xFFFF;
1112 ImageDosHeader.e_ss= 0x0000;
1113 ImageDosHeader.e_sp= 0x00B8;
1114 ImageDosHeader.e_csum= 0x0000;
1115 ImageDosHeader.e_ip= 0x0000;
1116 ImageDosHeader.e_cs= 0x0000;
1117 ImageDosHeader.e_lfarlc=0x0040;
1118 ImageDosHeader.e_ovno= 0x0000;
1119 ImageDosHeader.e_res[0]=0;
1120 ImageDosHeader.e_res[1]=0;
1121 ImageDosHeader.e_res[2]=0;
1122 ImageDosHeader.e_res[3]=0;
1123 ImageDosHeader.e_oemid= 0x0000;
1124 ImageDosHeader.e_oeminfo=0x0000;
1125 ImageDosHeader.e_res2[0]=0;
1126 ImageDosHeader.e_res2[1]=0;
1127 ImageDosHeader.e_res2[2]=0;
1128 ImageDosHeader.e_res2[3]=0;
1129 ImageDosHeader.e_res2[4]=0;
1130 ImageDosHeader.e_res2[5]=0;
1131 ImageDosHeader.e_res2[6]=0;
1132 ImageDosHeader.e_res2[7]=0;
1133 ImageDosHeader.e_res2[8]=0;
1134 ImageDosHeader.e_res2[9]=0;
1135 ImageDosHeader.e_lfanew=0x0100; //PEヘッダの位置
1136
1137
1138 /////////////////////////////////////////////
1139 // PEヘッダ
1140 /////////////////////////////////////////////
1141
1142 IMAGE_NT_HEADERS ImagePeHdr;
1143 ImagePeHdr.Signature=IMAGE_NT_SIGNATURE;
1144
1145 //マシンタイプ
1146 ImagePeHdr.FileHeader.Machine= IMAGE_FILE_MACHINE_I386;
1147
1148 ImagePeHdr.FileHeader.NumberOfSections= bUse_CodeSection+
1149 bUse_ExportSection+
1150 bUse_ImportSection+
1151 bUse_DataSection+
1152 bUse_RWSection+
1153 bUse_RSrcSection+
1154 bUse_RelocSection+
1155 bUse_DebugSection; //セクション数
1156 ImagePeHdr.FileHeader.TimeDateStamp= (DWORD)time(NULL);
1157 ImagePeHdr.FileHeader.PointerToSymbolTable= 0x00000000;
1158 ImagePeHdr.FileHeader.NumberOfSymbols= 0x00000000;
1159 ImagePeHdr.FileHeader.SizeOfOptionalHeader= IMAGE_SIZEOF_NT_OPTIONAL32_HEADER;
1160 if( compiler.IsDll() ){
1161 ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_EXECUTABLE_IMAGE|
1162 IMAGE_FILE_32BIT_MACHINE|
1163 IMAGE_FILE_LINE_NUMS_STRIPPED|
1164 IMAGE_FILE_LOCAL_SYMS_STRIPPED|
1165 IMAGE_FILE_DLL;
1166 }
1167 else{
1168 ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_EXECUTABLE_IMAGE|
1169 IMAGE_FILE_32BIT_MACHINE|
1170 IMAGE_FILE_LINE_NUMS_STRIPPED|
1171 IMAGE_FILE_LOCAL_SYMS_STRIPPED;
1172 }
1173
1174 ImagePeHdr.OptionalHeader.Magic= 0x010B;
1175 ImagePeHdr.OptionalHeader.MajorLinkerVersion= 4;
1176 ImagePeHdr.OptionalHeader.MinorLinkerVersion= 0;
1177 ImagePeHdr.OptionalHeader.SizeOfCode= FileSize_CodeSection; //コードサイズ(.textのセッションサイズ)
1178 ImagePeHdr.OptionalHeader.SizeOfInitializedData=FileSize_DataSection; //データサイズ(.dataのセッションサイズ)
1179 ImagePeHdr.OptionalHeader.SizeOfUninitializedData=0; //未初期化データのサイズ(なし)
1180 if( compiler.IsDll() ){
1181 if(DllMain_EntryPoint==-1)
1182 ImagePeHdr.OptionalHeader.AddressOfEntryPoint=0;
1183 else
1184 ImagePeHdr.OptionalHeader.AddressOfEntryPoint=MemPos_CodeSection+DllMain_EntryPoint;
1185 }
1186 else ImagePeHdr.OptionalHeader.AddressOfEntryPoint= MemPos_CodeSection;
1187 ImagePeHdr.OptionalHeader.BaseOfCode= MemPos_CodeSection; //.textのアドレス
1188 ImagePeHdr.OptionalHeader.BaseOfData= MemPos_DataSection; //.dataのアドレス
1189
1190 ImagePeHdr.OptionalHeader.ImageBase= ImageBase; //イメージベース
1191 ImagePeHdr.OptionalHeader.SectionAlignment= MEM_ALIGNMENT; //セクションアラインメント
1192 ImagePeHdr.OptionalHeader.FileAlignment= FILE_ALIGNMENT;
1193 ImagePeHdr.OptionalHeader.MajorOperatingSystemVersion=4;
1194 ImagePeHdr.OptionalHeader.MinorOperatingSystemVersion=0;
1195 ImagePeHdr.OptionalHeader.MajorImageVersion= 0;
1196 ImagePeHdr.OptionalHeader.MinorImageVersion= 0;
1197 ImagePeHdr.OptionalHeader.MajorSubsystemVersion=4;
1198 ImagePeHdr.OptionalHeader.MinorSubsystemVersion=0;
1199 ImagePeHdr.OptionalHeader.Win32VersionValue= 0;
1200 ImagePeHdr.OptionalHeader.SizeOfImage= EXE_HEADER_SIZE+
1201 MemSize_CodeSection+
1202 MemSize_ExportSection+
1203 MemSize_ImportSection+
1204 MemSize_DataSection+
1205 MemSize_RWSection+
1206 MemSize_RSrcSection+
1207 MemSize_RelocSection+
1208 MemSize_DebugSection;//すべてのイメージサイズ
1209 ImagePeHdr.OptionalHeader.SizeOfHeaders= EXE_HEADER_SIZE;//ヘッダサイズ
1210 ImagePeHdr.OptionalHeader.CheckSum= 0;
1211 extern unsigned short TypeOfSubSystem;
1212 ImagePeHdr.OptionalHeader.Subsystem= TypeOfSubSystem;
1213 ImagePeHdr.OptionalHeader.DllCharacteristics= 0;
1214 ImagePeHdr.OptionalHeader.SizeOfStackReserve= 0x00100000;
1215 ImagePeHdr.OptionalHeader.SizeOfStackCommit= 0x00001000;
1216 ImagePeHdr.OptionalHeader.SizeOfHeapReserve= 0x00100000;
1217 ImagePeHdr.OptionalHeader.SizeOfHeapCommit= 0x00001000;
1218 ImagePeHdr.OptionalHeader.LoaderFlags= 0;
1219 ImagePeHdr.OptionalHeader.NumberOfRvaAndSizes= IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1220
1221 //データ ディクショナリ
1222 ImagePeHdr.OptionalHeader.DataDirectory[0].VirtualAddress=MemPos_ExportSection;
1223 ImagePeHdr.OptionalHeader.DataDirectory[0].Size=FileSize_ExportSection;
1224 ImagePeHdr.OptionalHeader.DataDirectory[1].VirtualAddress=MemPos_ImportSection;//インポートテーブル
1225 ImagePeHdr.OptionalHeader.DataDirectory[1].Size=FileSize_ImportSection;
1226 ImagePeHdr.OptionalHeader.DataDirectory[2].VirtualAddress=MemPos_RSrcSection;
1227 ImagePeHdr.OptionalHeader.DataDirectory[2].Size=RSrcSectionSize;
1228 ImagePeHdr.OptionalHeader.DataDirectory[3].VirtualAddress=0;
1229 ImagePeHdr.OptionalHeader.DataDirectory[3].Size=0;
1230 ImagePeHdr.OptionalHeader.DataDirectory[4].VirtualAddress=0;
1231 ImagePeHdr.OptionalHeader.DataDirectory[4].Size=0;
1232 ImagePeHdr.OptionalHeader.DataDirectory[5].VirtualAddress=MemPos_RelocSection;
1233 ImagePeHdr.OptionalHeader.DataDirectory[5].Size=pobj_Reloc->length;
1234 ImagePeHdr.OptionalHeader.DataDirectory[6].VirtualAddress=0;
1235 ImagePeHdr.OptionalHeader.DataDirectory[6].Size=0;
1236 ImagePeHdr.OptionalHeader.DataDirectory[7].VirtualAddress=0;
1237 ImagePeHdr.OptionalHeader.DataDirectory[7].Size=0;
1238 ImagePeHdr.OptionalHeader.DataDirectory[8].VirtualAddress=0;
1239 ImagePeHdr.OptionalHeader.DataDirectory[8].Size=0;
1240 ImagePeHdr.OptionalHeader.DataDirectory[9].VirtualAddress=0;
1241 ImagePeHdr.OptionalHeader.DataDirectory[9].Size=0;
1242 ImagePeHdr.OptionalHeader.DataDirectory[10].VirtualAddress=0;
1243 ImagePeHdr.OptionalHeader.DataDirectory[10].Size=0;
1244 ImagePeHdr.OptionalHeader.DataDirectory[11].VirtualAddress=0;
1245 ImagePeHdr.OptionalHeader.DataDirectory[11].Size=0;
1246 ImagePeHdr.OptionalHeader.DataDirectory[12].VirtualAddress=MemPos_ImportSection+
1247 (ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+
1248 16*ImportDllNum+ //DLL名
1249 LookupSize+ //ルックアップテーブル
1250 HintSize; //ヒント名(関数名)テーブル
1251 ImagePeHdr.OptionalHeader.DataDirectory[12].Size=LookupSize;
1252 ImagePeHdr.OptionalHeader.DataDirectory[13].VirtualAddress=0;
1253 ImagePeHdr.OptionalHeader.DataDirectory[13].Size=0;
1254 ImagePeHdr.OptionalHeader.DataDirectory[14].VirtualAddress=0;
1255 ImagePeHdr.OptionalHeader.DataDirectory[14].Size=0;
1256 ImagePeHdr.OptionalHeader.DataDirectory[15].VirtualAddress=0;
1257 ImagePeHdr.OptionalHeader.DataDirectory[15].Size=0;
1258
1259
1260 //コードセクションヘッダ
1261 IMAGE_SECTION_HEADER CodeSectionHeader;
1262 memset((char *)CodeSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1263 lstrcpy((char *)CodeSectionHeader.Name,".text");
1264 CodeSectionHeader.Misc.VirtualSize= MemSize_CodeSection;
1265 CodeSectionHeader.VirtualAddress= MemPos_CodeSection; //開始アドレス
1266 CodeSectionHeader.SizeOfRawData= FileSize_CodeSection;
1267 CodeSectionHeader.PointerToRawData= FilePos_CodeSection; //ファイル上の開始アドレス
1268 CodeSectionHeader.PointerToRelocations= 0;
1269 CodeSectionHeader.PointerToLinenumbers= 0;
1270 CodeSectionHeader.NumberOfRelocations= 0;
1271 CodeSectionHeader.NumberOfLinenumbers= 0;
1272 CodeSectionHeader.Characteristics= IMAGE_SCN_MEM_EXECUTE|
1273 IMAGE_SCN_MEM_READ|
1274 IMAGE_SCN_CNT_CODE;
1275
1276 //エクスポートセクションヘッダ
1277 IMAGE_SECTION_HEADER ExportSectionHeader;
1278 memset((char *)ExportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1279 lstrcpy((char *)ExportSectionHeader.Name,".edata");
1280 ExportSectionHeader.Misc.VirtualSize= MemSize_ExportSection;
1281 ExportSectionHeader.VirtualAddress= MemPos_ExportSection; //開始アドレス
1282 ExportSectionHeader.SizeOfRawData= FileSize_ExportSection; //サイズ
1283 ExportSectionHeader.PointerToRawData= FilePos_ExportSection; //ファイル上の開始アドレス
1284 ExportSectionHeader.PointerToRelocations= 0;
1285 ExportSectionHeader.PointerToLinenumbers= 0;
1286 ExportSectionHeader.NumberOfRelocations= 0;
1287 ExportSectionHeader.NumberOfLinenumbers= 0;
1288 ExportSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1289 IMAGE_SCN_MEM_READ;
1290
1291 //インポートセクションヘッダ
1292 IMAGE_SECTION_HEADER ImportSectionHeader;
1293 memset((char *)ImportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1294 lstrcpy((char *)ImportSectionHeader.Name,".idata");
1295 ImportSectionHeader.Misc.VirtualSize= MemSize_ImportSection;
1296 ImportSectionHeader.VirtualAddress= MemPos_ImportSection; //開始アドレス
1297 ImportSectionHeader.SizeOfRawData= FileSize_ImportSection; //サイズ
1298 ImportSectionHeader.PointerToRawData= FilePos_ImportSection; //ファイル上の開始アドレス
1299 ImportSectionHeader.PointerToRelocations= 0;
1300 ImportSectionHeader.PointerToLinenumbers= 0;
1301 ImportSectionHeader.NumberOfRelocations= 0;
1302 ImportSectionHeader.NumberOfLinenumbers= 0;
1303 ImportSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1304 IMAGE_SCN_MEM_READ;
1305
1306 //データセクションヘッダ
1307 IMAGE_SECTION_HEADER DataSectionHeader;
1308 memset((char *)DataSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1309 lstrcpy((char *)DataSectionHeader.Name,".sdata");
1310 DataSectionHeader.Misc.VirtualSize= MemSize_DataSection;
1311 DataSectionHeader.VirtualAddress= MemPos_DataSection;
1312 DataSectionHeader.SizeOfRawData= FileSize_DataSection;
1313 DataSectionHeader.PointerToRawData= FilePos_DataSection;
1314 DataSectionHeader.PointerToRelocations= 0;
1315 DataSectionHeader.PointerToLinenumbers= 0;
1316 DataSectionHeader.NumberOfRelocations= 0;
1317 DataSectionHeader.NumberOfLinenumbers= 0;
1318 DataSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1319 IMAGE_SCN_MEM_READ|
1320 IMAGE_SCN_MEM_WRITE;
1321
1322 //リライタブルセクションヘッダ
1323 IMAGE_SECTION_HEADER RWSectionHeader;
1324 memset((char *)RWSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1325 lstrcpy((char *)RWSectionHeader.Name,".data");
1326 RWSectionHeader.Misc.VirtualSize= compiler.GetObjectModule().meta.GetGlobalVars().GetAllInitSize()
1327 + compiler.GetObjectModule().meta.GetGlobalVars().GetAllSize();
1328 RWSectionHeader.VirtualAddress= MemPos_RWSection;
1329 RWSectionHeader.SizeOfRawData= FileSize_RWSection;
1330 RWSectionHeader.PointerToRawData= FilePos_RWSection;
1331 RWSectionHeader.PointerToRelocations= 0;
1332 RWSectionHeader.PointerToLinenumbers= 0;
1333 RWSectionHeader.NumberOfRelocations= 0;
1334 RWSectionHeader.NumberOfLinenumbers= 0;
1335 RWSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1336 IMAGE_SCN_MEM_READ|
1337 IMAGE_SCN_MEM_WRITE;
1338
1339 //リソースセクションヘッダ
1340 IMAGE_SECTION_HEADER RSrcSectionHeader;
1341 memset((char *)RSrcSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1342 lstrcpy((char *)RSrcSectionHeader.Name,".rsrc");
1343 RSrcSectionHeader.Misc.VirtualSize= RSrcSectionSize;
1344 RSrcSectionHeader.VirtualAddress= MemPos_RSrcSection;
1345 RSrcSectionHeader.SizeOfRawData= FileSize_RSrcSection;
1346 RSrcSectionHeader.PointerToRawData= FilePos_RSrcSection;
1347 RSrcSectionHeader.PointerToRelocations= 0;
1348 RSrcSectionHeader.PointerToLinenumbers= 0;
1349 RSrcSectionHeader.NumberOfRelocations= 0;
1350 RSrcSectionHeader.NumberOfLinenumbers= 0;
1351 RSrcSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1352 IMAGE_SCN_MEM_READ;
1353
1354 //リロケーションセクションヘッダ
1355 IMAGE_SECTION_HEADER RelocSectionHeader;
1356 memset((char *)RelocSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1357 lstrcpy((char *)RelocSectionHeader.Name,".reloc");
1358 RelocSectionHeader.Misc.VirtualSize= pobj_Reloc->length;
1359 RelocSectionHeader.VirtualAddress= MemPos_RelocSection; //開始アドレス
1360 RelocSectionHeader.SizeOfRawData= FileSize_RelocSection; //サイズ
1361 RelocSectionHeader.PointerToRawData= FilePos_RelocSection; //ファイル上の開始アドレス
1362 RelocSectionHeader.PointerToRelocations= 0;
1363 RelocSectionHeader.PointerToLinenumbers= 0;
1364 RelocSectionHeader.NumberOfRelocations= 0;
1365 RelocSectionHeader.NumberOfLinenumbers= 0;
1366 RelocSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1367 IMAGE_SCN_MEM_DISCARDABLE|
1368 IMAGE_SCN_MEM_READ;
1369
1370 //デバッグセクションヘッダ
1371 IMAGE_SECTION_HEADER DebugSectionHeader;
1372 memset((char *)DebugSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1373 lstrcpy((char *)DebugSectionHeader.Name,".debug");
1374 DebugSectionHeader.Misc.VirtualSize= pobj_DebugSection->length;
1375 DebugSectionHeader.VirtualAddress= MemPos_DebugSection; //開始アドレス
1376 DebugSectionHeader.SizeOfRawData= FileSize_DebugSection; //サイズ
1377 DebugSectionHeader.PointerToRawData= FilePos_DebugSection; //ファイル上の開始アドレス
1378 DebugSectionHeader.PointerToRelocations= 0;
1379 DebugSectionHeader.PointerToLinenumbers= 0;
1380 DebugSectionHeader.NumberOfRelocations= 0;
1381 DebugSectionHeader.NumberOfLinenumbers= 0;
1382 DebugSectionHeader.Characteristics= IMAGE_SCN_MEM_DISCARDABLE|
1383 IMAGE_SCN_MEM_READ;
1384
1385
1386 hFile=CreateFile(OutputFileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
1387 if(hFile==INVALID_HANDLE_VALUE){
1388 SetError(53,OutputFileName,-1);
1389 goto EndWriteOpcode;
1390 }
1391
1392 //ヘッダ
1393 WriteFile(hFile,(void *)&ImageDosHeader,sizeof(IMAGE_DOS_HEADER),(DWORD *)&i2,NULL);
1394 i=i2;
1395
1396 //Dosスタブ
1397 WriteFile(hFile,DosStubBuffer,DosStubSize,(DWORD *)&i2,NULL);
1398 i+=i2;
1399
1400 //0x0100までNULLを並べる
1401 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,0x0100-i);
1402 WriteFile(hFile,temp2,0x0100-i,(DWORD *)&i2,NULL);
1403 HeapDefaultFree(temp2);
1404 i+=i2;
1405
1406 //PEヘッダ
1407 WriteFile(hFile,&ImagePeHdr,sizeof(IMAGE_NT_HEADERS),(DWORD *)&i2,NULL);
1408 i+=i2;
1409
1410 //コード セクション ヘッダ
1411 WriteFile(hFile,&CodeSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1412 i+=i2;
1413
1414 if(bUse_ExportSection){
1415 //エクスポート セクション ヘッダ
1416 WriteFile(hFile,&ExportSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1417 i+=i2;
1418 }
1419 if(bUse_ImportSection){
1420 //インポート セクション ヘッダ
1421 WriteFile(hFile,&ImportSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1422 i+=i2;
1423 }
1424 if(bUse_DataSection){
1425 //データ セクション ヘッダ
1426 WriteFile(hFile,&DataSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1427 i+=i2;
1428 }
1429 if(bUse_RWSection){
1430 //リライタブルセクションヘッダ
1431 WriteFile(hFile,&RWSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1432 i+=i2;
1433 }
1434 if(bUse_RSrcSection){
1435 //リソースセクションヘッダ
1436 WriteFile(hFile,&RSrcSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1437 i+=i2;
1438 }
1439 if(bUse_RelocSection){
1440 //リロケーションセクションヘッダ
1441 WriteFile(hFile,&RelocSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1442 i+=i2;
1443 }
1444 if(bUse_DebugSection){
1445 //デバッグセクションヘッダ
1446 WriteFile(hFile,&DebugSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1447 i+=i2;
1448 }
1449
1450 //EXE_HEADER_SIZEまでNULLを並べる
1451 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,EXE_HEADER_SIZE-i);
1452 WriteFile(hFile,temp2,EXE_HEADER_SIZE-i,(DWORD *)&i2,NULL);
1453 HeapDefaultFree(temp2);
1454 i+=i2;
1455
1456 //コード
1457 WriteFile(
1458 hFile,
1459 compiler.linker.GetNativeCode().GetCodeBuffer(),
1460 compiler.linker.GetNativeCode().GetSize(),
1461 (DWORD *)&i2,
1462 NULL
1463 );
1464 i+=i2;
1465
1466 //FilePos_ExportSectionまでNULLを並べる
1467 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_ExportSection-i);
1468 WriteFile(hFile,temp2,FilePos_ExportSection-i,(DWORD *)&i2,NULL);
1469 HeapDefaultFree(temp2);
1470 i+=i2;
1471
1472 if(bUse_ExportSection){
1473 //エクスポート ディレクトリ テーブル
1474 WriteFile(hFile,&ImageExportDirectory,sizeof(IMAGE_EXPORT_DIRECTORY),(DWORD *)&i2,NULL);
1475 i+=i2;
1476
1477 //エクスポート アドレス テーブル
1478 WriteFile(hFile,lpdwExportAddressTable,ExportNum*sizeof(DWORD),(DWORD *)&i2,NULL);
1479 i+=i2;
1480
1481 //エクスポート名ポインタ テーブル
1482 WriteFile(hFile,lpdwExportNamePointerTable,ExportNum*sizeof(DWORD),(DWORD *)&i2,NULL);
1483 i+=i2;
1484
1485 //エクスポート序数テーブル
1486 WriteFile(hFile,lpwExportOrdinalTable,ExportNum*sizeof(WORD),(DWORD *)&i2,NULL);
1487 i+=i2;
1488
1489 //シンボル名
1490 WriteFile(hFile,lpExportNames,ExportNamesLength,(DWORD *)&i2,NULL);
1491 i+=i2;
1492 }
1493
1494 //FilePos_ImportSectionまでNULLを並べる
1495 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_ImportSection-i);
1496 WriteFile(hFile,temp2,FilePos_ImportSection-i,(DWORD *)&i2,NULL);
1497 HeapDefaultFree(temp2);
1498 i+=i2;
1499
1500 if(bUse_ImportSection){
1501 //インポート ディレクトリ テーブル(Nullディレクトリ テーブルを含む)
1502 for(i3=0;i3<(ImportDllNum+1);i3++){
1503 WriteFile(hFile,&pImportTable[i3],sizeof(IMAGE_IMPORT_DESCRIPTOR),(DWORD *)&i2,NULL);
1504 i+=i2;
1505 }
1506
1507 //DLL名
1508 for(i3=0;i3<ImportDllNum;i3++){
1509 WriteFile(hFile,ppDllNames[i3],16,(DWORD *)&i2,NULL);
1510 i+=i2;
1511 }
1512
1513 //ルックアップ テーブル
1514 WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
1515 i+=i2;
1516
1517 //ヒント テーブル
1518 WriteFile(hFile,pHintTable,HintSize,(DWORD *)&i2,NULL);
1519 i+=i2;
1520
1521 //インポート アドレス テーブル
1522 WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
1523 i+=i2;
1524 }
1525
1526 //FilePos_DataSectionまでNULLを並べる
1527 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_DataSection-i);
1528 WriteFile(hFile,temp2,FilePos_DataSection-i,(DWORD *)&i2,NULL);
1529 HeapDefaultFree(temp2);
1530 i+=i2;
1531
1532 if(bUse_DataSection){
1533 //データ テーブル
1534 WriteFile(hFile,compiler.GetObjectModule().dataTable.GetPtr(),compiler.GetObjectModule().dataTable.GetSize(),(DWORD *)&i2,NULL);
1535 i+=i2;
1536 }
1537
1538 //FilePos_RWSectionまでNULLを並べる
1539 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RWSection-i);
1540 WriteFile(hFile,temp2,FilePos_RWSection-i,(DWORD *)&i2,NULL);
1541 HeapDefaultFree(temp2);
1542 i+=i2;
1543
1544 if(bUse_RWSection){
1545 //リライタブル データ テーブル(グローバル変数の初期バッファ)
1546 initGlobalBuf=(BYTE *)HeapReAlloc(hHeap,
1547 HEAP_ZERO_MEMORY,
1548 initGlobalBuf,
1549 FileSize_RWSection);
1550 WriteFile(hFile,initGlobalBuf,FileSize_RWSection,(DWORD *)&i2,NULL);
1551 i+=i2;
1552 }
1553
1554 //FilePos_RSrcSectionまでNULLを並べる
1555 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RSrcSection-i);
1556 WriteFile(hFile,temp2,FilePos_RSrcSection-i,(DWORD *)&i2,NULL);
1557 HeapDefaultFree(temp2);
1558 i+=i2;
1559
1560 if(bUse_RSrcSection){
1561 //リソースバッファ
1562 WriteFile(hFile,RSrcSectionBuffer,RSrcSectionSize,(DWORD *)&i2,NULL);
1563 i+=i2;
1564 }
1565
1566 //FilePos_RelocSectionまでNULLを並べる
1567 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RelocSection-i);
1568 WriteFile(hFile,temp2,FilePos_RelocSection-i,(DWORD *)&i2,NULL);
1569 HeapDefaultFree(temp2);
1570 i+=i2;
1571
1572 if(bUse_RelocSection){
1573 //リロケーション情報
1574 WriteFile(hFile,pobj_Reloc->buffer,pobj_Reloc->length,(DWORD *)&i2,NULL);
1575 i+=i2;
1576 }
1577
1578 //ファイルアラインメントを考慮
1579 if(i%FILE_ALIGNMENT){
1580 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
1581 WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
1582 HeapDefaultFree(temp2);
1583 i+=i2;
1584 }
1585
1586 if(bUse_DebugSection){
1587 //デバッグセクション
1588 WriteFile(hFile,pobj_DebugSection->buffer,pobj_DebugSection->length,(DWORD *)&i2,NULL);
1589 i+=i2;
1590 }
1591
1592 //ファイルアラインメントを考慮
1593 if(i%FILE_ALIGNMENT){
1594 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
1595 WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
1596 HeapDefaultFree(temp2);
1597 i+=i2;
1598 }
1599
1600 //書き込み終了
1601 CloseHandle(hFile);
1602
1603
1604EndWriteOpcode:
1605
1606 //Dosスタブ用のメモリを解放
1607 HeapDefaultFree(DosStubBuffer);
1608
1609 //エクスポート テーブル情報を解放
1610 HeapDefaultFree(lpdwExportAddressTable);
1611 HeapDefaultFree(lpdwExportNamePointerTable);
1612 HeapDefaultFree(lpwExportOrdinalTable);
1613
1614 //インポートDLL情報を解放
1615 HeapDefaultFree(pImportTable);
1616 for(i=0;i<ImportDllNum;i++)
1617 HeapDefaultFree(ppDllNames[i]);
1618 HeapDefaultFree(ppDllNames);
1619
1620 //ルックアップテーブルに関する情報を解放
1621 HeapDefaultFree(pLookupTable);
1622
1623 //ヒントテーブルに関する情報を解放
1624 HeapDefaultFree(pHintTable);
1625
1626 //グローバル変数の初期バッファを解放
1627 HeapDefaultFree(initGlobalBuf);
1628
1629 //リソースセクションバッファを解放
1630 HeapDefaultFree(RSrcSectionBuffer);
1631
1632 //デバッグセクションを開放
1633 delete pobj_DebugSection;
1634
1635 //リロケーション情報を解放
1636 delete pobj_Reloc;
1637
1638 //列挙体に関する情報の破棄
1639 CEnumParent::DestroyEnum();
1640
1641 //クラスに関するメモリを解放
1642 compiler.GetObjectModule().meta.GetClasses().Clear();
1643}
Note: See TracBrowser for help on using the repository browser.