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