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