Skip to content

Commit

Permalink
testcode & fixes to make it work
Browse files Browse the repository at this point in the history
  • Loading branch information
Rochus Keller committed Apr 18, 2021
1 parent 7383b0e commit a5e2be2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
9 changes: 8 additions & 1 deletion PELib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,14 @@ bool PELib::DumpPEFile(std::string file, bool isexe, bool isgui)
WorkingAssembly()->Number(n); // give initial PE Indexes for field resolution..

peWriter_ = new PEWriter(isexe, isgui, WorkingAssembly()->SNKFile());
size_t moduleIndex = peWriter_->HashString("Module");

// RK: Unhandled Exception on Mono 3 and 5:
// System.TypeLoadException: Could not load type 'Module' from assembly 'test6, Version=0.0.0.0, Culture=neutral,
// PublicKeyToken=null'.
// [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: Could not load type 'Module' from assembly 'test6,
// Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
size_t moduleIndex = peWriter_->HashString("<Module>"); // RK fix: "<Module>" instead of "Module" fixes the issue

TypeDefOrRef typeDef(TypeDefOrRef::TypeDef, 0);
TableEntryBase* table = new TypeDefTableEntry(0, moduleIndex, 0, typeDef, 1, 1);
peWriter_->AddTableEntry(table);
Expand Down
4 changes: 2 additions & 2 deletions PEReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ TableEntryBase* TableEntryFactory::GetEntry(size_t index)
}
PEReader::~PEReader()
{
delete[] inputFile_;
delete inputFile_;
delete[] objects_;
delete[] stringData_;
delete[] blobData_;
Expand Down Expand Up @@ -469,4 +469,4 @@ int PEReader::ReadTables(size_t Cor20)
delete[] tableMem;
return 0;
}
} // namespace DotNetPELib
} // namespace DotNetPELib
55 changes: 54 additions & 1 deletion test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,58 @@
#include "DotNetPELib.h"
#include <QCoreApplication>
#include <QtDebug>
using namespace DotNetPELib;

int main()
{
return 0;
PELib peFile("HiThere", PELib::ilonly | PELib::bits32);

AssemblyDef* assembly = peFile.WorkingAssembly();

peFile.LoadAssembly("mscorlib");

MethodSignature* sigMain = peFile.AllocateMethodSignature("$Main",MethodSignature::Managed, assembly);
sigMain->ReturnType(peFile.AllocateType(Type::Void, 0));
Method* methMain = peFile.AllocateMethod( sigMain, Qualifiers::Private |
Qualifiers::Static |
Qualifiers::HideBySig |
Qualifiers::CIL |
Qualifiers::Managed, true);
assembly->Add(methMain);


Type stringType(Type::string, 0);
std::vector<Type*> argList;
argList.push_back(&stringType);
Method* methWriteLine = nullptr;
MethodSignature* sigWriteLine;
if( peFile.Find( "System.Console.WriteLine", &methWriteLine, argList ) == PELib::s_method )
sigWriteLine = methWriteLine->Signature();
else
{
qCritical() << "cannot find WriteLine in mscorlib";
return -1;
}

methMain->AddInstruction(
peFile.AllocateInstruction(Instruction::i_ldstr,
peFile.AllocateOperand("Hi there!", true)));
methMain->AddInstruction(
peFile.AllocateInstruction(Instruction::i_call,
peFile.AllocateOperand(peFile.AllocateMethodName(sigWriteLine))));
methMain->AddInstruction(
peFile.AllocateInstruction(Instruction::i_ret, nullptr));

try
{
methMain->Optimize(peFile);
}catch (PELibError exc)
{
qCritical() << "Optimizer error: " << exc.what();
}

peFile.DumpOutputFile("HiThere.il", PELib::ilasm, false);
peFile.DumpOutputFile("HiThere.exe", PELib::peexe, false);

return 0;
}

0 comments on commit a5e2be2

Please sign in to comment.