ECMA-335: Is it possible parse custom attributes signature without additional data from referenced a

A

Alex Sedow

Continue discuss. (Previous post was: ECMA-335: How to decode PackedLen)

For parsing custom attributes signatures parser needs data from MethodDef
signature. For example (from System.dll):
Custom attribute signature:
0x08 - blob length
0x01 0x00 - Prolog
0xfc 0x17 0x00 0x00 - Val (enum)
0x00 0x00 - NumNamed
Method signature:
0x06 - blob length
0x20 - HASTHIS
0x01 - ParamCount
0x01 - ELEMENT_TYPE_VOID
0x11 - ELEMENT_TYPE_VALUETYPE
0x82, 0x6d - TypeRefOrDefEncoded: System.AttributeTargets

For correct parsing ValueType parser must know what is it: value type or
enum. Signature for value type contain SerString, signature for enum contain
binary value of underlying integer type. I.e. parser must determine on which
type TypeRefOrDefEncoded refers. But if TypeRefOrDefEncoded is reference to
type defined in another assembly, than this reference contain only namespace
and name. And parser must before parse all assemblies on which refers
valuetype typerefs in custom attributes signatures?

So the question is: Is it possible parse custom attributes signature without
additional data from referenced assemblies? Or something missing?

Alex.
 
M

Mattias Sjögren

Alex,
For correct parsing ValueType parser must know what is it: value type or
enum. Signature for value type contain SerString, signature for enum contain
binary value of underlying integer type.

What do you mean by "value type" here? I mean, enums are value types
too, as are the primitive numeric types.

If you mean a custom value type (struct in C#), then that shouldn't
happen since those can't be used in a custom attribute signature.

So basicly, whenever you see E_T_VALUETYPE it should be an enum
parameter.

I.e. parser must determine on which
type TypeRefOrDefEncoded refers. But if TypeRefOrDefEncoded is reference to
type defined in another assembly, than this reference contain only namespace
and name. And parser must before parse all assemblies on which refers
valuetype typerefs in custom attributes signatures?

No, if it refers to an enum in another assembly (i.e. the token is a
TypeRef), then the ResolutionScope in the TypeRef table will tell you
which assembly it's from.

So the question is: Is it possible parse custom attributes signature without
additional data from referenced assemblies? Or something missing?

Unfortunately not. For enum parameters, the blob contains the raw
value, but you can't know how many bytes that value takes (1, 2, 4, or
8) without resolving the enum type and looking at it's underlying
integer type.



Mattias
 
A

Alex Sedow

Mattias,
If you mean a custom value type (struct in C#), then that shouldn't
happen since those can't be used in a custom attribute signature.

So basicly, whenever you see E_T_VALUETYPE it should be an enum
parameter.

I missing that.
Unfortunately not. For enum parameters, the blob contains the raw
value, but you can't know how many bytes that value takes (1, 2, 4, or
8) without resolving the enum type and looking at it's underlying
integer type.

This looks like a specification bug.
Why metadata does not contain some prefix before raw data?
It will be fixed in future framework versions?

Thank you for valuable help (again).

Alex.
 
M

Mattias Sjögren

Alex,
This looks like a specification bug.
Why metadata does not contain some prefix before raw data?
It will be fixed in future framework versions?

I wouldn't call it a bug, but it sure is annoying.

I don't know why they did it this way, and I don't know if they'll
ever "fix" it (I think that would be hard to do without breaking
existing implementations).



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top