- 什么是程序集
- PE文件
- 元数据组成
- 元数据定义表
- 引用元数据表
- 清单元数据表
- window查看元数据示例:
程序集是一个或多个类型定义文件以及资源文件的集合。程序集是进行重用,版本控制和应用安全设置的基本单元,它允许将类型和资源文件划分到单独的文件中。这样一来无论是自己还是程序集用户都可以决定打包和部署哪些文件。
使用理由:
- 不同的类型用不同的文件,使文件能以增量方式下载
- 可在程序集中添加资源、数据文件
- 程序集包含的各个类型可以用不同的语言实现,然后把这些语言模块使用工具合并成一个程序集
PE文件(Portable Executable):可移植执行体,包括PE32(+)头、CLR头、元数据、IL。
-PE32(+)头:是Windows要求的标准信息
-CLR头:是一个小的信息块,是需要CRL模块特有的
-元数据:下面会介绍
-IL:CLR所能识别的中间语言代码。
元数据是由几个表构成的二进制数据块。有3种表,分别是定义表(definition table),引用表(reference table),和清单表(manifest table)。
元数据定义表| 元数据定义表名称 | 说明 |
|---|---|
| ModuleDef | 记录模块信息。包括模块文件名和扩展名,模块版本id。 |
| TypeDef | 记录模块定义的每个类型。包含类型的名称,基类,标记(private)以及一些索引,这些索引指向MethodDef表中该类型的方法,FieldDef中该类型的字段,PropertyDef表中改类型的属性,EventDef中该类型的事件 |
| MerhodDef | 记录模块定义的每个方法。每个记录包括方法名,标志(private static等),签名,IL代码在模块中的偏移,每个记录还会引用ParamDef表中的一个记录。 |
| FieldDef | 记录模块定义的字段。包括标志(private public等)、类型、名称。 |
| ParamDef | 记录模块定义的每个参数。包括标志(in out ref etval等)、类型、名称。 |
| PropertyDef | 记录模块定义的每个属性。包括标志、类型、名称。 |
| EventDef | 记录模块定义的每个事件。包括标志、名称。 |
用以记录引用的内容
| 引用元数据表名称 | 说明 |
|---|---|
| AssemblyRef | 记录模块引用的程序集。包括绑定该程序集所需的信息:程序集名,版本号,语言文化,公钥token。 |
| ModuleRef | 记录实现该模块所引用的类型的每个PE模块。包含模块的文件名,扩展名。可能是别的模块实现了你需要的类型,这个表的作用便是建立同那些类型的绑定关系。 |
| TypeRef | 记录模块引用的每个类型。每个记录包括类型名和一个引用(指向类型的位置) |
| MemberRef | 记录模块引用的每个成员(字段,属性,事件,方法)。每个记录都包含成员的名称和签名,并指向对成员进行定义的那个类型的TypeRef记录项。 |
清单是一个元数据表的集合,表中主要包含作为程序集组成部分的那些文件的名称。此为还描述了程序集的版本,语言文化,发布者,公开导出的类型以及构成程序集的所有文件。
| 清单元数据表名称 | 说明 |
|---|---|
| AssemblyDef | 如果模块表识的是程序集,这个元数据表就包含单一记录项列出程序集名,版本号,语言文化,标志,哈希算法,发布者公钥token。 |
| FileDef | 记录程序集组成的每个PE文件与资源文件。不包含模清单文件。每个记录都包含文件名,扩展名,标志,哈希值。如果程序集只包含它自己的文件,FileDef表将无此记录(程序集只包含主模块)。 |
| ManifestResourceDef | 记录程序集组成的每个资源。每个记录包括资源名,标志(外部可见public,否则private),FileDef 表的一个索引(指出资源或者流包含在哪个文件中),流指非(.jpg/.gif文件)。 |
| ExportedTypesDef | 记录程序集的所有PE模块中导出的每个public类型。每个记录包括类型名,FileDef表的一个索引(指出类型由哪个文件实现),以及TypeDef表的一个索引 |
由于有了清单的存在,程序集的用户不必关心程序集的划分细节。在包含清单的文件中,一些元数据信息描述了哪些文件是程序集的一部分。但是那些文件本身并不包含元数据来指出它们是程序集的一部分。
CLR操作的是程序集,换言之,CLR总是先加载包含包含“清单”元数据表的文件,再根据清单来获取程序集中其他文件的名称。
环境:VS2017,勾选安装了.net framework 或者支持net core都可以
创建window .net core console application,命名为DLLTest,输入代码如下:
using System;
namespace DLLTest
{
public class Class1
{
public static void Main()
{
Console.WriteLine("Hello dll");
Console.ReadLine();
}
}
}
生成dll:DLLTest.dll
打开本地32位ILDasm.exe :
将生产的Dll拖入ildasm:
点击视图->元信息->显示 或者通过Ctrl+M 打开元信息视图:
查看元信息如下:
===========================================================
ScopeName : DLLTest.dll
MVID : {DBA3C955-66E5-4321-BA23-314843888825}
===========================================================
Global functions
-------------------------------------------------------
Global fields
-------------------------------------------------------
Global MemberRefs
-------------------------------------------------------
TypeDef #1 (02000002)
-------------------------------------------------------
TypDefName: DLLTest.Class1 (02000002)
Flags : [Public] [AutoLayout] [Class] [AnsiClass] [BeforeFieldInit] (00100001)
Extends : 0100000C [TypeRef] System.Object
Method #1 (06000001) [ENTRYPOINT]
-------------------------------------------------------
MethodName: Main (06000001)
Flags : [Public] [Static] [HideBySig] [ReuseSlot] (00000096)
RVA : 0x00002050
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
ReturnType: Void
No arguments.
Method #2 (06000002)
-------------------------------------------------------
MethodName: .ctor (06000002)
Flags : [Public] [HideBySig] [ReuseSlot] [SpecialName] [RTSpecialName] [.ctor] (00001886)
RVA : 0x00002064
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
No arguments.
TypeRef #1 (01000001)
-------------------------------------------------------
Token: 0x01000001
ResolutionScope: 0x23000001
TypeRefName: System.Runtime.CompilerServices.CompilationRelaxationsAttribute
MemberRef #1 (0a000001)
-------------------------------------------------------
Member: (0a000001) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
1 Arguments
Argument #1: I4
TypeRef #2 (01000002)
-------------------------------------------------------
Token: 0x01000002
ResolutionScope: 0x23000001
TypeRefName: System.Runtime.CompilerServices.RuntimeCompatibilityAttribute
MemberRef #1 (0a000002)
-------------------------------------------------------
Member: (0a000002) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
No arguments.
TypeRef #3 (01000003)
-------------------------------------------------------
Token: 0x01000003
ResolutionScope: 0x23000001
TypeRefName: System.Diagnostics.DebuggableAttribute
MemberRef #1 (0a000003)
-------------------------------------------------------
Member: (0a000003) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
1 Arguments
Argument #1: ValueClass DebuggingModes
TypeRef #4 (01000004)
-------------------------------------------------------
Token: 0x01000004
ResolutionScope: 0x01000003
TypeRefName: DebuggingModes
TypeRef #5 (01000005)
-------------------------------------------------------
Token: 0x01000005
ResolutionScope: 0x23000001
TypeRefName: System.Runtime.Versioning.TargetFrameworkAttribute
MemberRef #1 (0a000004)
-------------------------------------------------------
Member: (0a000004) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
1 Arguments
Argument #1: String
TypeRef #6 (01000006)
-------------------------------------------------------
Token: 0x01000006
ResolutionScope: 0x23000001
TypeRefName: System.Reflection.AssemblyCompanyAttribute
MemberRef #1 (0a000005)
-------------------------------------------------------
Member: (0a000005) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
1 Arguments
Argument #1: String
TypeRef #7 (01000007)
-------------------------------------------------------
Token: 0x01000007
ResolutionScope: 0x23000001
TypeRefName: System.Reflection.AssemblyConfigurationAttribute
MemberRef #1 (0a000006)
-------------------------------------------------------
Member: (0a000006) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
1 Arguments
Argument #1: String
TypeRef #8 (01000008)
-------------------------------------------------------
Token: 0x01000008
ResolutionScope: 0x23000001
TypeRefName: System.Reflection.AssemblyFileVersionAttribute
MemberRef #1 (0a000007)
-------------------------------------------------------
Member: (0a000007) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
1 Arguments
Argument #1: String
TypeRef #9 (01000009)
-------------------------------------------------------
Token: 0x01000009
ResolutionScope: 0x23000001
TypeRefName: System.Reflection.AssemblyInformationalVersionAttribute
MemberRef #1 (0a000008)
-------------------------------------------------------
Member: (0a000008) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
1 Arguments
Argument #1: String
TypeRef #10 (0100000a)
-------------------------------------------------------
Token: 0x0100000a
ResolutionScope: 0x23000001
TypeRefName: System.Reflection.AssemblyProductAttribute
MemberRef #1 (0a000009)
-------------------------------------------------------
Member: (0a000009) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
1 Arguments
Argument #1: String
TypeRef #11 (0100000b)
-------------------------------------------------------
Token: 0x0100000b
ResolutionScope: 0x23000001
TypeRefName: System.Reflection.AssemblyTitleAttribute
MemberRef #1 (0a00000a)
-------------------------------------------------------
Member: (0a00000a) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
1 Arguments
Argument #1: String
TypeRef #12 (0100000c)
-------------------------------------------------------
Token: 0x0100000c
ResolutionScope: 0x23000001
TypeRefName: System.Object
MemberRef #1 (0a00000d)
-------------------------------------------------------
Member: (0a00000d) .ctor:
CallCnvntn: [DEFAULT]
hasThis
ReturnType: Void
No arguments.
TypeRef #13 (0100000d)
-------------------------------------------------------
Token: 0x0100000d
ResolutionScope: 0x23000002
TypeRefName: System.Console
MemberRef #1 (0a00000b)
-------------------------------------------------------
Member: (0a00000b) WriteLine:
CallCnvntn: [DEFAULT]
ReturnType: Void
1 Arguments
Argument #1: String
MemberRef #2 (0a00000c)
-------------------------------------------------------
Member: (0a00000c) ReadLine:
CallCnvntn: [DEFAULT]
ReturnType: String
No arguments.
Assembly
-------------------------------------------------------
Token: 0x20000001
Name : DLLTest
Public Key :
Hash Algorithm : 0x00008004
Version: 1.0.0.0
Major Version: 0x00000001
Minor Version: 0x00000000
Build Number: 0x00000000
Revision Number: 0x00000000
Locale:
Flags : [none] (00000000)
CustomAttribute #1 (0c000001)
-------------------------------------------------------
CustomAttribute Type: 0a000001
CustomAttributeName: System.Runtime.CompilerServices.CompilationRelaxationsAttribute :: instance void .ctor(int32)
Length: 8
Value : 01 00 08 00 00 00 00 00 > <
ctor args: (8)
CustomAttribute #2 (0c000002)
-------------------------------------------------------
CustomAttribute Type: 0a000002
CustomAttributeName: System.Runtime.CompilerServices.RuntimeCompatibilityAttribute :: instance void .ctor()
Length: 30
Value : 01 00 01 00 54 02 16 57 72 61 70 4e 6f 6e 45 78 > T WrapNonEx<
: 63 65 70 74 69 6f 6e 54 68 72 6f 77 73 01 >ceptionThrows <
ctor args: ()
CustomAttribute #3 (0c000003)
-------------------------------------------------------
CustomAttribute Type: 0a000003
CustomAttributeName: System.Diagnostics.DebuggableAttribute :: instance void .ctor(value class DebuggingModes)
Length: 8
Value : 01 00 07 01 00 00 00 00 > <
ctor args: ( )
CustomAttribute #4 (0c000004)
-------------------------------------------------------
CustomAttribute Type: 0a000004
CustomAttributeName: System.Runtime.Versioning.TargetFrameworkAttribute :: instance void .ctor(class System.String)
Length: 53
Value : 01 00 18 2e 4e 45 54 43 6f 72 65 41 70 70 2c 56 > .NETCoreApp,V<
: 65 72 73 69 6f 6e 3d 76 32 2e 31 01 00 54 0e 14 >ersion=v2.1 T <
: 46 72 61 6d 65 77 6f 72 6b 44 69 73 70 6c 61 79 >FrameworkDisplay<
: 4e 61 6d 65 00 >Name <
ctor args: (".NETCoreApp,Version=v2.1")
CustomAttribute #5 (0c000005)
-------------------------------------------------------
CustomAttribute Type: 0a000005
CustomAttributeName: System.Reflection.AssemblyCompanyAttribute :: instance void .ctor(class System.String)
Length: 12
Value : 01 00 07 44 4c 4c 54 65 73 74 00 00 > DLLTest <
ctor args: ("DLLTest")
CustomAttribute #6 (0c000006)
-------------------------------------------------------
CustomAttribute Type: 0a000006
CustomAttributeName: System.Reflection.AssemblyConfigurationAttribute :: instance void .ctor(class System.String)
Length: 10
Value : 01 00 05 44 65 62 75 67 00 00 > Debug <
ctor args: ("Debug")
CustomAttribute #7 (0c000007)
-------------------------------------------------------
CustomAttribute Type: 0a000007
CustomAttributeName: System.Reflection.AssemblyFileVersionAttribute :: instance void .ctor(class System.String)
Length: 12
Value : 01 00 07 31 2e 30 2e 30 2e 30 00 00 > 1.0.0.0 <
ctor args: ("1.0.0.0")
CustomAttribute #8 (0c000008)
-------------------------------------------------------
CustomAttribute Type: 0a000008
CustomAttributeName: System.Reflection.AssemblyInformationalVersionAttribute :: instance void .ctor(class System.String)
Length: 10
Value : 01 00 05 31 2e 30 2e 30 00 00 > 1.0.0 <
ctor args: ("1.0.0")
CustomAttribute #9 (0c000009)
-------------------------------------------------------
CustomAttribute Type: 0a000009
CustomAttributeName: System.Reflection.AssemblyProductAttribute :: instance void .ctor(class System.String)
Length: 12
Value : 01 00 07 44 4c 4c 54 65 73 74 00 00 > DLLTest <
ctor args: ("DLLTest")
CustomAttribute #10 (0c00000a)
-------------------------------------------------------
CustomAttribute Type: 0a00000a
CustomAttributeName: System.Reflection.AssemblyTitleAttribute :: instance void .ctor(class System.String)
Length: 12
Value : 01 00 07 44 4c 4c 54 65 73 74 00 00 > DLLTest <
ctor args: ("DLLTest")
AssemblyRef #1 (23000001)
-------------------------------------------------------
Token: 0x23000001
Public Key or Token: b0 3f 5f 7f 11 d5 0a 3a
Name: System.Runtime
Version: 4.2.1.0
Major Version: 0x00000004
Minor Version: 0x00000002
Build Number: 0x00000001
Revision Number: 0x00000000
Locale:
HashValue Blob:
Flags: [none] (00000000)
AssemblyRef #2 (23000002)
-------------------------------------------------------
Token: 0x23000002
Public Key or Token: b0 3f 5f 7f 11 d5 0a 3a
Name: System.Console
Version: 4.1.1.0
Major Version: 0x00000004
Minor Version: 0x00000001
Build Number: 0x00000001
Revision Number: 0x00000000
Locale:
HashValue Blob:
Flags: [none] (00000000)
User Strings
-------------------------------------------------------
70000001 : ( 9) L"Hello dll"
Coff symbol name overhead: 0
===========================================================
===========================================================
===========================================================



