Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support entry-point method and executable assemblies #37

Closed
osman-turan opened this issue Apr 30, 2019 · 5 comments · Fixed by #162
Closed

Support entry-point method and executable assemblies #37

osman-turan opened this issue Apr 30, 2019 · 5 comments · Fixed by #162
Assignees
Labels
enhancement New feature or request

Comments

@osman-turan
Copy link
Collaborator

Initial code had some problems to support executable assemblies (e.g. console applications). The main reason was we need to know entry-point method handle ahead of its serialization to able to serialize <Module> type as first type in the assembly. See:

// Add "<Module>" type definition *before* any type definition.
//
// TODO: [osman] methodList argument should be as following:
//
// methodList: entryPoint.IsNil ? MetadataTokens.MethodDefinitionHandle(1) : entryPoint
//
// But, in order to work above code, we need to serialize
// entry point *without* serializing any type definition.
// This is not needed for libraries since they don't have any entry point.
_metadata.Builder.AddTypeDefinition(
default,
default,
_metadata.GetOrAddString("<Module>"),
default,
MetadataTokens.FieldDefinitionHandle(1),
MetadataTokens.MethodDefinitionHandle(1));

With #31, we could reserve metadata tokens prior to serialization. So, this could help us to support executable assemblies.

@osman-turan osman-turan added the enhancement New feature or request label Apr 30, 2019
@osman-turan osman-turan self-assigned this Apr 30, 2019
@luhis
Copy link

luhis commented Mar 29, 2020

I see that this issue is still open. Where would you suggest I go reading to get my head around this issue?

@vermorel
Copy link
Member

@luhis Thanks a lot for your feedback. You have the SandboxSubject.csproj in the solution that let you test a target of your choice, for example an executable. Don't hesitate to submit if PR if you can. We don't have resources for this project right now, but any help is highly appreciated.

@jwhitehorn
Copy link

jwhitehorn commented Feb 18, 2021

This would be a very useful feature to have.

I just started taking a shot at it, and have made a little progress. Unfortunately the .entrypoint is being added to the constructor, not the actual entrypoint.

.class private auto ansi Program
	extends [System.Private.CoreLib]System.Object
{
	// Methods
	.method public specialname rtspecialname 
		instance void .ctor () cil managed 
	{
		// Method begins at RVA 0x2050
		// Code size 7 (0x7)
		.maxstack 8
		.entrypoint

		IL_0000: ldarg.0
		IL_0001: call instance void [System.Private.CoreLib]System.Object::.ctor()
		IL_0006: ret
	} // end of method Program::.ctor

	.method public static 
		void Main () cil managed 
	{
		// Method begins at RVA 0x2058
		// Code size 11 (0xb)
		.maxstack 8

		IL_0000: ldstr "Hello World!"
		IL_0005: call void [System.Console]System.Console::WriteLine(string)
		IL_000a: ret
	} // end of method Program::Main

} // end of class Program

I suspect this has something to do with @osman-turan 's comment:

"...we need to serialize entry point without serializing any type definition."

I'm afraid I'm not certain what that implies - this is a little new to me. Any clarification or elaboration would be greatly appreciated.

@osman-turan
Copy link
Collaborator Author

@jwhitehorn While defining the entry point, we need a method handle. I think you use ctor's handle by mistake (or more likely you use a constant method handle that's coincidently resolved to the ctor handle at the method serializing step due to its serialization order). My initial post describes more about the problem that I described in the code comments. The most important part is:

The main reason was we need to know entry-point method handle ahead of its serialization to able to serialize type as first type in the assembly.

So we need to "reserve" a method handle for entry-point "without" actually serializing the method (otherwise .NET Core will complain). Then we should feed this reserved handle to the entry-point definition. And finally, we can serialize the entry-point method handle with the previously reserved method handle.

I hope, it's a bit more clear now 🙂

@OlegRa OlegRa assigned OlegRa and unassigned osman-turan May 11, 2022
OlegRa added a commit to OlegRa/ILPack that referenced this issue May 12, 2022
vermorel pushed a commit that referenced this issue May 12, 2022
@NickStrupat
Copy link

Hi folks! Great library.

I'm trying to figure out how to set the entry point of an AssemblyBuilder instance. Is it possible with .NET Core/5+?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants