FieldDefinition.InitialValue

255 views
Skip to first unread message

Knight Commander

unread,
Aug 17, 2010, 6:20:39 AM8/17/10
to mono-...@googlegroups.com
Hi cecilers :D

Is FieldDefinition.InitialValue what should appear in ".data" section? I created a type which contains a byte[] field with a large initialValue but there is no sign of ".data" in output of ildasm but the assembly binary image contains the initialvalue content.

any suggestions?

Jb Evain

unread,
Aug 17, 2010, 7:22:11 AM8/17/10
to mono-...@googlegroups.com
Hey,

On Tue, Aug 17, 2010 at 12:20 PM, Knight Commander
<rabbit...@gmail.com> wrote:
> Is FieldDefinition.InitialValue what should appear in ".data" section? I
> created a type which contains a byte[] field with a large initialValue but
> there is no sign of ".data" in output of ildasm but the assembly binary
> image contains the initialvalue content.

Weird. Do you have a small snippet of code reproducing the issue?

--
Jb Evain  <j...@nurv.fr>

Knight Commander

unread,
Aug 17, 2010, 9:19:05 AM8/17/10
to mono-...@googlegroups.com
Hi Jb,


Here it is the code:

AssemblyDefinition assm =
        AssemblyDefinition.CreateAssembly(new AssemblyNameDefinition("myassem", new
        Version("1.0.0.0")), "mainmodule",
ModuleKind.Dll);
TypeDefinition type = new TypeDefinition("ghoori", "chai", TypeAttributes.Class);
ModuleDefinition mod = assm.Modules[0];
mod.Types.Add(type);
FieldDefinition fild = new FieldDefinition("initdar", FieldAttributes.Private, mod.Import(typeof (byte[])));
 
type.Fields.Add(fild);
fild.InitialValue = new byte[] {65, 65, 65, 65, 65, 65, 65, 65, 65, 65};
// will appear as a sequence of 'A' in binary view of output file.

assm.Write("ass.dll");
 
--
Jb Evain  <j...@nurv.fr>

--
--
mono-cecil


Jb Evain

unread,
Aug 17, 2010, 9:41:28 AM8/17/10
to mono-...@googlegroups.com
On Tue, Aug 17, 2010 at 3:19 PM, Knight Commander
<rabbit...@gmail.com> wrote:
> TypeDefinition type = new TypeDefinition("ghoori", "chai",
> TypeAttributes.Class);

You should pass the import of typeof (object) as the base type of your class.

> ModuleDefinition mod = assm.Modules[0];
> mod.Types.Add(type);
> FieldDefinition fild = new FieldDefinition("initdar",
> FieldAttributes.Private, mod.Import(typeof (byte[])));

You have to pass FieldAttributes.Static | FieldAttributes.HasFieldRVA

--
Jb Evain  <j...@nurv.fr>

Knight Commander

unread,
Aug 17, 2010, 9:46:45 AM8/17/10
to mono-...@googlegroups.com
Oh! thank you!

But, should it be always static?

On Tue, Aug 17, 2010 at 6:11 PM, Jb Evain <j...@nurv.fr> wrote:
You should pass the import of typeof (object) as the base type of your class.

You have to pass FieldAttributes.Static | FieldAttributes.HasFieldRVA

--
Jb Evain  <j...@nurv.fr>

--
--
mono-cecil


Knight Commander

unread,
Aug 17, 2010, 9:56:00 AM8/17/10
to mono-...@googlegroups.com
I applied these changes, now there is something else

I have the following line in disassembled file:

.field private static uint8[] initdar at
I_00002050
....
.data cil I_00002050 = bytearray(41)

length of this byte array is just one! when I have 5 bytes in my initialValue;


Knight Commander

unread,
Aug 17, 2010, 10:15:49 AM8/17/10
to mono-...@googlegroups.com
:))
I solved my problem :P

memory size of field which is to be initialized MUST be the same as initialization data. I created a struct "size10" with size of 10 and then defined a field of type size10. then I assigned a byte array of length 10 to that field. Now I have a ".data" section of size 10 in my assembly.

thank you Jb,
You helped me a lot. although I still do not know what RVA is :P

Jb Evain

unread,
Aug 17, 2010, 10:34:42 AM8/17/10
to mono-...@googlegroups.com
On Tue, Aug 17, 2010 at 4:15 PM, Knight Commander
<rabbit...@gmail.com> wrote:
> although I still do not know what RVA is :P

Relative Virtual Address. It's just a way of addressing data in a PE
file. It means that the field has a «pointer» to some data in the
module.

--
Jb Evain  <j...@nurv.fr>

Reply all
Reply to author
Forward
0 new messages