Introduction to metadata and IL

Source: Internet
Author: User
Tags code tag mscorlib types of tables
Document directory
  •  
  • 1.3.2 metadata
  • 1.3.3 Common commands of Il
  • 1.3.4 Il and code verification

Text/Xuan Hun

1.3.2 metadata

Metadata is the data that describes the data. In the context of CLR, metadata represents a set of systems composed of descriptors. These operators include all items declared or referenced in a module. Since the CLR model is object-oriented, the items described in the metadata are the class and their members, as well as their accompanying features, attributes, and associations. This section briefly introduces metadata. The content related to the original data security will be further explained in the subsequent sections. The details of metadata are beyond the scope of this book.

Metadata is actually a piece of binary data and contains three types of tables: Definition table, reference table, and list table.

A metadata definition table is a set of definition tables, including module definition, type definition, method definition, field definition, event definition, Parameter definition, and attribute definition. When the compiler compiles the Code, all definitions generate a corresponding definition table.

The metadata reference table is used to record the types, methods, fields, and events referenced by source code in the compiler. Commonly used reference tables include assemblyref, moduleref, and typeref.

The metadata List table contains all the information required to make up the assembly and the reference information for other assembly. It explicitly specifies which entries can be opened to the outside and which can be accessed only within the Assembly.

The following describes the metadata information in the typical helloworld program, as shown in code 1-7.

Code List 1-7 helloworld program code

using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace HelloWorld{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("Hello World!");            Console.Read();         }    }}

 

Use the ildasmtool to open helloworld.exe and double-click manifest. Figure 1-7 shows the configuration information. For more information about how to use ildasm and its parameters, see the msdn documentation.

 

Figure 1-7 view the program list

Detailed list information is shown in Listing 1-8 of the Code.

Code List 1-8 helloworld. EXE configuration information

// Metadata version: v4.0.21006.assembly extern mscorlib{  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..  .ver 4:0:0:0}.assembly HelloWorld{......}  .hash algorithm 0x00008004  .ver 1:0:0:0.module HelloWorld.exe// MVID: {B8EB35DD-5AD2-402C-B422-AA63B0AACCFA}.imagebase 0x00400000.file alignment 0x00000200.stackreserve 0x00100000.subsystem 0x0003       // WINDOWS_CUI.corflags 0x00000003    //  ILONLY 32BITREQUIRED// Image base: 0x05C40000

 

The program is relatively simple. The Code contains simple information such as version and external reference. Table 1-2 describes the commands in the Assembly list of the helloword.exe Assembly used in the example.

Table 1-2 helloworld.exe instructions

Command

Description

. Assembly extern < Assembly name >

Specify another Assembly that contains the project referenced by the current module (mscorlib in this example)

. Publickeytoken < Token >

Mark of the actual key of the referenced assembly

. Ver < Version number >

Indicates the version number of the referenced assembly.

. Assembly < Assembly name >

Specify Assembly name

. Hash Algorithm < Int32 Value >

Hash Algorithm Used

. Module < File Name >

Specify the module name of the Assembly. In this example, the Assembly contains only one file.

. Subsystem < Value >

Specifies the application environment required by the program. In this example, value 3 indicates that the executable file runs from the console

. Corflags

It is currently a reserved field in the metadata.

According to the Assembly content, the Assembly List can contain many different commands. For a complete list of commands in the assembly list, please refer to the relevant documentation. This example aims to inspire others. To view the complete metadata information, use the shortcut key "ctlr + M", as shown in figure 1-8.

 

Figure 1-8 view metadata

1.3.3 Common commands of Il

Helloworld.exe is used as an example to describe the related content of Il. Due to space limitations, please refer to the relevant materials for details about Il. Figure 1-9 shows the Il code of the main method.

 

Figure 1-9 il code of the helloworld.exe main method

In an intermediate language program, if a line it indicates that this is an instruction transmitted to the assembler tool, instead of ". "the starting line is the code of the intermediate language. In. method is the method definition instruction, and the main method is defined. The parameter is in "()", and the Il code is in .. Entrypoint is the entry command, indicating that this method is the entry method .. Maxstack specifies the maximum stack depth of 8. The following il_n is the code tag, followed by the Il code. NOP is an empty command; ldstr is used to input the string "Hello World!" to the stack !"; The call command calls the static methods console. writeline (string) and console. Read (). Pop pops the value at the top of the stack. the RET command indicates the end of The method body. Il supports the "//" and "/**/" annotation methods.

Prompt in the intermediate language, if you need to call a method, you need to specify the full name of the method, including its namespace, class name, return value type, and parameter data type.

Table 1-3 lists some other commonly used commands of Il. For more commands, see the Il command table.

Table 1-3 Common commands of Il

Command

Description

. Assembly <Assembly name> {}

Set assembly

LDC. i4.N

Load a 32-bit constant (n from 0 to 8) into the stack.

Stloc.N

Store the value returned from the stack to n (n is 0 ~ 8) local variables

Add

Add two values. The parameter of the command must be loaded into the stack before calling. This function removes the parameter from the stack and pushes the operation result to the stack.

Sub

Two values subtract

Mul

Multiply two values

Newarr Type

Generate an element typeType. The array size must be loaded into the stack before the command is called. This command loads the reference of an array into the stack.

Stelem. I4

Assign a value to a group of several members. The reference, subscript, and value of the array must be loaded into the stack before the command is called.

Ldelema Type

Load the address of the array element into the stack. The reference and subscript of the array must be loaded into the stack before the command is called. Address used to call non-static functions

Ldlen

Load the length of the array into the stack. The array reference must be loaded into the stack before the command is called.

Ldloca. s Variable

Load the variable address into the stack

LDC. i4.s Value

Load an Int32 constant into the stack (used for a number greater than 8 bits)

Conv. I4

Converts the value of a stack to Int32.

Call instance Function (arguments)

Call non-static functions of a class

Bge. s Label

JumpLabelIf value1 is ≥value 2. Values 1 and 2, you must load the stack before calling this command.

Br. s Label

JumpLabel

Box Value type

Converts a value type to an Object and loads the reference of this Object into a stack.

Blt. s Label

JumpLabel. If value 1 is less than value 2. Values 1 and 2, you must load the stack before calling this command.

Ldelem. i4

Load an array element into the stack. Array references and subscript must be loaded into the stack before calling this command

Ldarga. s Argument

Load the address of function parameters into the stack

Dup

Copy a value on the stack

Stind. i4

The address of the stored value. The address and value must be loaded into the stack before calling this command.

. Field

Define class members. Used with keywords such as public, private, and static

Stsfld Static field

Replace the static field value with the value in the stack

Ldquo Field

Load a non-static field into the stack. The address of the class instance must be loaded into the stack before calling this command.

Ldarg. n

Load the nth parameter into the stack. In non-static functions, 0th parameters are an implicit parameter, representing this

Newobj Constructor

ConstructorConstructorGenerates an instance of a class. The constructor parameters must be loaded into the stack before calling this function. An instance of a class is generated and loaded into the stack.

Callvirt instance Function

Call the post-binding method of an object

 

1.3.4 Il and code verification

When msil is compiled into local code, the msil code must pass the verification process unless the Administrator has established a security policy that allows the code to skip verification. The verification process checks msil and metadata to determine whether the code is type-safe, which means that it only accesses the memory location that has been authorized to access. Type Security helps isolate objects from each other, thus protecting them from unintentional or malicious damages. It also provides a guarantee that the code can reliably enforce security restrictions.

The Runtime library uses the following conditions to verify whether the code is of type security:

Q. The type reference is strictly compatible with the referenced type.

Q. Only operations correctly defined are called on objects.

The Q mark is consistent with the claim.

Check the msil code during verification and try to confirm that the code can only access the memory location and call method through the correctly defined type. For example, the Code does not allow access to objects beyond the memory range. In addition, the verification process checks the code to determine whether the msil is correctly generated because an incorrect msil may cause a violation of type security rules. The verification process passes the correctly defined type security code set, and it only passes type security code. However, due to some restrictions in the verification process, some types of security code may not pass verification, and some languages do not produce verifiable type security code. If the security policy requires that the type security code be provided and the Code cannot pass verification, an exception is thrown when the code is run.

------------------------- Note: This article is excerpted from section 1.3 of. NET Security secrets.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.