Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the information about the entry point of an assembly written in the assembly?

I used to think that an assembly could have only one main() method until I saw Jon Skeet's MiscUtil in a video lecture he delivered at the Microsoft office in Copenhagen.

So, I wrote this little app that had two main() methods like so:

namespace ManyMains
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            Console.ReadKey();
        }
    }

    class YetAnotherProgram
    {
        static void Main()
        {
            Console.WriteLine("Yet another program.");
            Console.ReadKey();
        }
    }
}

I set the StartUp object in Visual Studio and it worked. Okay, no cause for distress. Then, I wanted to see where exactly this information is stored in the assembly, so I opened the compiled binary in reflector and saw absolutely no metadata to that effect.

I'm wondering if that sort of information is written into the manifest or some COFF header of the PE image that can't be seen in a disassembler but could be seen in a hex editor?

like image 347
Water Cooler v2 Avatar asked Oct 17 '25 19:10

Water Cooler v2


2 Answers

I just opened up one of my executables in the IL Disassembler. Notice the .entrypoint line for the Main method.

.method public hidebysig static void  Main() cil managed
{
  .entrypoint
  .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) 
  .custom instance void [mscorlib]System.Diagnostics.DebuggerNonUserCodeAttribute::.ctor() = ( 01 00 00 00 ) 
  // Code size       22 (0x16)
  .maxstack  1
  .locals init ([0] class AuctionSniper.Main.App app)
  IL_0000:  nop
  ... <snipped>

vs a non-entry point method - Lets say InitializeComponent()

.method public hidebysig instance void  InitializeComponent() cil managed
{
  .custom instance void [mscorlib]System.Diagnostics.DebuggerNonUserCodeAttribute::.ctor() = ( 01 00 00 00 ) 
  // Code size       20 (0x14)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldarg.0
  ... <snipped>
like image 193
Gishu Avatar answered Oct 20 '25 07:10

Gishu


You can check that using ildasm.exe

ildasm /ALL /TEXT program.exe
like image 34
Dirk Vollmar Avatar answered Oct 20 '25 08:10

Dirk Vollmar