From 7550c456cbdd67c386837347a514c2271d95be4f Mon Sep 17 00:00:00 2001 From: "Denis Kuzmin [ GitHub/3F ]" Date: Sun, 8 Dec 2019 19:49:33 +0300 Subject: [PATCH] Implemented optional rebasing of system object in assembly: `netstandard` } `System.Runtime` } `mscorlib` Closes #7 --- src/dlls/mscorpe/ceefilegenwriter.cpp | 29 ++------------------------- src/ilasm/assem.cpp | 2 ++ src/ilasm/assembler.cpp | 28 ++++++++++++++++++++++++++ src/ilasm/assembler.h | 4 ++++ src/ilasm/main.cpp | 8 +++++++- 5 files changed, 43 insertions(+), 28 deletions(-) diff --git a/src/dlls/mscorpe/ceefilegenwriter.cpp b/src/dlls/mscorpe/ceefilegenwriter.cpp index 4fb3f9c59a10..cd72b377c288 100644 --- a/src/dlls/mscorpe/ceefilegenwriter.cpp +++ b/src/dlls/mscorpe/ceefilegenwriter.cpp @@ -1048,26 +1048,6 @@ void pathToCvtRes(LPCWSTR workdir, LPCWSTR path, LPCWSTR fname, __out PathString } } -HRESULT GetClrSystemDirectory(SString& pbuffer) -{ - HRESULT hr = S_OK; - - - PathString pPath; - DWORD dwPath; - - _ASSERTE (g_hThisInst); - - dwPath = WszGetModuleFileName(g_hThisInst, pPath); - if(dwPath == 0) - { - hr = HRESULT_FROM_GetLastErrorNA(); - return (hr); - } - - return CopySystemDirectory(pPath, pbuffer); -} - #ifndef FEATURE_PAL BOOL CeeFileGenWriter::RunProcess(LPCWSTR tempResObj, LPCWSTR pszFilename, DWORD* pdwExitCode, PEWriter &pewriter) { @@ -1075,13 +1055,8 @@ BOOL CeeFileGenWriter::RunProcess(LPCWSTR tempResObj, LPCWSTR pszFilename, DWORD PROCESS_INFORMATION pi; - PathString wszSystemDir, cvtres; - if(FAILED(GetClrSystemDirectory(wszSystemDir))) { - pathToCvtRes(nullptr, m_pathToCvtRes, nullptr, cvtres); - } - else { - pathToCvtRes(wszSystemDir.GetUnicode(), m_pathToCvtRes, nullptr, cvtres); - } + PathString cvtres; + pathToCvtRes(nullptr, m_pathToCvtRes, nullptr, cvtres); const WCHAR* wzMachine; if(pewriter.isIA64()) diff --git a/src/ilasm/assem.cpp b/src/ilasm/assem.cpp index a3b8daf8ae07..01a62200c1bd 100644 --- a/src/ilasm/assem.cpp +++ b/src/ilasm/assem.cpp @@ -151,6 +151,8 @@ Assembler::Assembler() m_pbsMD = NULL; + m_sysObjRebase = false; + m_pOutputBuffer = new BYTE[OUTPUT_BUFFER_SIZE]; m_pCurOutputPos = m_pOutputBuffer; diff --git a/src/ilasm/assembler.cpp b/src/ilasm/assembler.cpp index b03af5095941..5c839dd335e8 100644 --- a/src/ilasm/assembler.cpp +++ b/src/ilasm/assembler.cpp @@ -284,6 +284,11 @@ mdToken Assembler::GetBaseAsmRef() return GetAsmRef(coreLibAsm->szAlias ? coreLibAsm->szAlias : coreLibAsm->szName); } + return (m_sysObjRebase)? GetBaseAsmRefClr() : GetBaseAsmRefRt(); +} + +mdToken Assembler::GetBaseAsmRefRt() +{ AsmManAssembly* sysRuntime = m_pManifest->GetAsmRefByAsmName("System.Runtime"); if(sysRuntime != NULL) { @@ -305,6 +310,29 @@ mdToken Assembler::GetBaseAsmRef() return GetAsmRef("mscorlib"); } +mdToken Assembler::GetBaseAsmRefClr() +{ + AsmManAssembly* mscorlibAsm = m_pManifest->GetAsmRefByAsmName("mscorlib"); + if(mscorlibAsm != NULL) + { + return GetAsmRef(mscorlibAsm->szAlias ? mscorlibAsm->szAlias : mscorlibAsm->szName); + } + + AsmManAssembly* sysRuntime = m_pManifest->GetAsmRefByAsmName("System.Runtime"); + if(sysRuntime != NULL) + { + return GetAsmRef(sysRuntime->szAlias ? sysRuntime->szAlias : sysRuntime->szName); + } + + AsmManAssembly* netstandardAsm = m_pManifest->GetAsmRefByAsmName("netstandard"); + if (netstandardAsm != NULL) + { + return GetAsmRef(netstandardAsm->szAlias ? netstandardAsm->szAlias : netstandardAsm->szName); + } + + return GetAsmRef("mscorlib"); +} + mdToken Assembler::GetInterfaceImpl(mdToken tsClass, mdToken tsInterface) { mdToken result = mdTokenNil; diff --git a/src/ilasm/assembler.h b/src/ilasm/assembler.h index 559950d30c11..d0a5605a468c 100644 --- a/src/ilasm/assembler.h +++ b/src/ilasm/assembler.h @@ -748,6 +748,8 @@ class Assembler { BinStr* m_pbsMD; + bool m_sysObjRebase; + Instr m_Instr[INSTR_POOL_SIZE]; // 16 inline Instr* GetInstr() { @@ -795,6 +797,8 @@ class Assembler { mdToken ResolveClassRef(mdToken tkResScope, __in __nullterminated const char *pszClassName, Class** ppClass); mdToken ResolveTypeSpec(BinStr* typeSpec); mdToken GetBaseAsmRef(); + mdToken GetBaseAsmRefRt(); + mdToken GetBaseAsmRefClr(); mdToken GetAsmRef(__in __nullterminated const char* szName); mdToken GetModRef(__in __nullterminated char* szName); mdToken GetInterfaceImpl(mdToken tsClass, mdToken tsInterface); diff --git a/src/ilasm/main.cpp b/src/ilasm/main.cpp index cde912c462ca..83c1f0184586 100644 --- a/src/ilasm/main.cpp +++ b/src/ilasm/main.cpp @@ -184,6 +184,8 @@ extern "C" int _cdecl wmain(int argc, __in WCHAR **argv) // printf("\n/ERROR Try to create .exe or .dll file despite errors reported"); // printf("\n Warning! Results are unpredictable, use this option at your own risk!"); printf("\n/CVRES= Set path to cvtres tool: /CVR=cvtres.exe /CVR=tool\\cvtres.cmd /CVR=D:\\tool\\"); + printf("\n/REBASE Try to rebase system object: `netstandard` > `System.Runtime` > `mscorlib`"); + printf("\n ! This option at your own risk: https://github.com/3F/DllExport/issues/125"); printf("\n/OUTPUT= Compile to file with specified name \n\t\t\t(user must provide extension, if any)"); printf("\n/KEY= Compile with strong signature \n\t\t\t( contains private key)"); printf("\n/KEY=@ Compile with strong signature \n\t\t\t( is the private key source name)"); @@ -411,13 +413,17 @@ extern "C" int _cdecl wmain(int argc, __in WCHAR **argv) } else if (!_stricmp(szOpt, "CVR")) { - // TODO: ^v omg, how about common handler o_O + // TODO: ^v omg, how about a common handler o_O WCHAR *pStr = EqualOrColon(argv[i]); if(pStr == NULL) goto InvalidOption; for(pStr++; *pStr == L' '; pStr++); //skip the blanks if(wcslen(pStr) == 0) goto InvalidOption; //if no file name wzPathToCvtRes = pStr; } + else if (!_stricmp(szOpt, "REB")) + { + pAsm->m_sysObjRebase = true; + } else if (!_stricmp(szOpt, "OUT")) { WCHAR *pStr = EqualOrColon(argv[i]);