A field is a named, typed storage location of a class (one per instance, or one in total when it is static) or of the module (a global field). .data puts raw bytes in the data section of the output file; a static field can be placed over such data with at. Both directives are in fieldDecl and dataDecl of the ilasm grammar (ECMA-335 II.16).
Field declarations
The form is .field [[offset]] attributes type name [at datalabel] [= initialiser]. The offset is for explicit layout (Classes) and is not allowed on a global field.
| Words | Meaning |
|---|---|
public, private, family, assembly, famandassem, famorassem, privatescope | accessibility (the last one wins); privatescope is compiler-controlled |
static | one copy for the type, not one per object |
initonly | constant after initialisation: it may be assigned only in a constructor of its own type (in the type initializer, for a static field); code that breaks this, or takes the address with ldflda, is unverifiable |
literal | a compile-time constant: no memory is allocated for it, it exists only in the metadata, and its value is the initialiser; only static fields can be literal, and compilers replace each use with the value |
notserialized | left out when the object is serialised by the runtime’s serialiser |
specialname, rtspecialname | names with a meaning for tools or the runtime (the value__ field of an enum); ilasm accepts rtspecialname and sets no bit for it |
marshal(…) | how the field is marshalled (Marshalling and platform invoke) |
flags(n) | all attribute bits as a number |
A field name is an Id. .field public static int32 x at D_0001 says that the static field x lives at the data labelled D_0001; the field and the data then share bytes (and an initialiser cannot be combined with at). The data is then read and written like any other static variable, with ldsfld and stsfld.
Field initialisers
After = a field (and a parameter in .param [n] =) can take a constant, which is stored in the metadata. The runtime does not use it to initialise the field; compilers read it for literal fields and default argument values. The type of the constant need not match the type of the field.
| Form | Stored as |
|---|---|
bool(true), bool(false) | a Boolean |
char(65) | a 16-bit character |
int8(−1), int16(…), int32(…), int64(…) | a signed integer; unsigned int8(…) and so on, or uint8(…), uint16(…), uint32(…), uint64(…), an unsigned one |
float32(1.5), float64(1.5) | a floating-point number; with an integer in the parentheses, the bits of the value (float64(0x7FF0000000000000)) |
"text" (strings may be chained with +) | a string, stored as UTF-16 |
bytearray (48 00 69 00) | a string of bytes stored without conversion |
nullref | a null object reference |
The .data directive
.data [tls | cil] [label =] body. The body is one item or a list of items in braces separated by commas; the items are laid out in order. A data label is a name without a colon; give one when code or a field refers to the data. The section word picks the place: nothing for the ordinary data section, tls for thread-local data, cil for the section that holds the code (an ilasm choice; ildasm writes it as I_ followed by the address).
| Item | Meaning |
|---|---|
int8(n), int16(n), int32(n), int64(n), float32(x), float64(x) | a value of that size; an optional [count] after it repeats the value; the item without parentheses (int32[4]) reserves zeroed space |
bytearray (hex bytes) | raw bytes |
char*("text") | the characters of a string, as UTF-16 |
&(label) | the address of a data label (a pointer to other data) |
.data theInt = int32(123)
.data theBytes = int8 (3) [10]
.data cil Message = char*("Hello")
.data Table = { int32(1), int32(2), &(theInt), bytearray (DE AD BE EF) }
.field public static int32 myInt at theInt
.class public auto ansi Holder
{
.field public static int32 other at theInt
}
.data lines are the examples of ECMA-335 II.16.3.1.ildasm writes data it finds in the file as .data items named from the address (D_00002050, T_ for the thread-local section, I_ for the code section) with a bytearray body, and the static fields that point at them with at.
Not described
Known, not yet covered
- Reference: the rules of II.16.4 and II.16.5 for how data is initialised at load time and run time (the platform-specific rules about reading data from the file image, relocations of
&(label)) are not summarised.