patcher.cs

Thursday, March 23, 2006

//
// patcher.cs
//
// Author:
//   Jb Evain (jbevain@gmail.com)
//
// (C) 2005 Jb Evain
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using System.Text;

using Mono.Cecil.Binary;

class Patcher {

    static void Main (string [] args)
    {
        if (args.Length != 1) {
            Usage ();
            return;
        }

        string file = args [0];
        Image img = Image.GetImage (file);

        using (FileStream fs = new FileStream (
            file, FileMode.Open, FileAccess.ReadWrite)) {

            // hack, move the data directory CLIHeader to Reserved

            int pos = 0x0168;
            fs.Position = pos;
            byte [] cliheader = new byte [8];
            fs.Read (cliheader, 0, 8);

            fs.Position = pos;
            fs.Write (new byte [8], 0, 8);
            fs.Write (cliheader, 0, 8);

            // hack, remove the ilonly flags

            fs.Position = img.ResolveVirtualAddress (
                img.PEOptionalHeader.DataDirectories.CLIHeader.VirtualAddress) + 16;

            fs.Write (new byte [4], 0, 4);

            fs.Position = img.ResolveVirtualAddress (
                img.ImportAddressTable.HintNameTableRVA);

            // patch Hint

            fs.Position += 2;

            // patch RuntimeMain
            if (img.HintNameTable.RuntimeMain.ToLower ().IndexOf ("dll") == -1)
                Write (fs, "MonoExeMain");
            else
                Write (fs, "MonoDllMain");

            fs.Position = img.ResolveVirtualAddress (
                img.ImportTable.Name);

            // patch RuntimeLibrary
            Write (fs, "monoldr.dll");
        }

        Console.WriteLine ("Assembly {0} patched", file);
    }

    static void Write (FileStream fs, string s)
    {
        byte [] str = Encoding.ASCII.GetBytes (s);
        fs.Write (str, 0, str.Length);
    }

    static void Usage ()
    {
        Console.WriteLine ("Mono Assembly Patcher");
        Console.WriteLine ("usage: patcher.exe assembly");
    }
}