Tải bản đầy đủ (.pdf) (530 trang)

expert .net 2.0 il assembler

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.42 MB, 530 trang )

CYAN
MAGENTA
YELLOW
BLACK
PANTONE 123 CV
this print for content only—size & color not accurate
7" x 9-1/4" / CASEBOUND / MALLOY
(1.0625 INCH BULK 536 pages 50# Thor)
THE EXPERT’S VOICE
®
IN .NET
Serge Lidin
Expert
.NET 2.0 IL
Assembler
An in-depth view of inner workings of the .NET 2.0
common language runtime and the runtime’s own
language—the IL assembler
BOOKS FOR PROFESSIONALS BY PROFESSIONALS
®
Expert .NET 2.0 IL Assembler
Dear Reader,
This book is about the inner workings of version 2.0 of the Microsoft .NET
common language runtime and about the intricacies of programming in the
runtime’s own language—the IL assembly language. The IL assembly language
(ILAsm), unlike high-level programming languages such as C#, provides access to
the full functionality of the .NET runtime. Many compilers and programming
tools, ranging from purely academic projects to enterprise systems, use the IL
assembler as their back end for code generation. Any .NET application, regard-
less of the language it was originally written in, can be represented in ILAsm, so
you can always disassemble a .NET assembly or module into ILAsm and see for


yourself how it really works.
This book is a revision and an extension of my previous book Inside
Microsoft .NET IL Assembler, which was the first book to describe the inner
workings of ILAsm in the .NET 1.0 runtime. A great deal of time has passed
since the release of that version of the runtime (and the IL assembler) in early
2002, and in our industry technologies innovate quickly. Now that the more
powerful .NET 2.0 version has been released, I realized I needed to get back to
writing.
By reading this book you will learn how .NET 2.0 applications are built, how
the runtime functions, and how to program in the IL assembly language. You
will also discover how to build compilers and tools that generate ILAsm code
and how to read and analyze the ILAsm code the IL disassembler shows you.
Best regards,
Serge Lidin
Author of
Inside Microsoft
.NET IL Assembler
Shelve in Programming/
Microsoft/.NET
User level:
Advanced
www.apress.com
SOURCE CODE ONLINE
forums.apress.com
FOR PROFESSIONALS
BY PROFESSIONALS

Join online discussions:
THE APRESS ROADMAP
Pro C# 2005 and the

.NET 2.0 Platform, Third Edition
Pro VB 2005 and the
.NET 2.0 Platform, Second Edition
Pro ASP.NET 2.0
in C# 2005 / in VB 2005
Expert Service-Oriented Architecture
in C#, Second Edition
Expert ASP.NET 2.0
Advanced Application Design
Expert .NET 2.0
IL Assembler
.NET 2.0 IL Assembler
Lidin
ISBN 1-59059-646-3
9 781590 596463
90000
6 89253 59646 3
Companion eBook
Available
Companion eBook
See last page for details
on $10 eBook version
Expert
Serge Lidin
Expert .NET 2.0
IL Assembler
Ch00_6463_FINAL 7/27/06 3:00 PM Page i
Expert .NET 2.0 IL Assembler
Copyright © 2006 by Serge Lidin
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,

electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13: 978-1-59059-646-3
ISBN-10: 1-59059-646-3
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Ewan Buckingham
Technical Reviewers: Jim Hogg, Vance Morrison
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick,
Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser,
Keir Thomas, Matt Wade
Project Manager: Sofia Marchant
Copy Edit Manager: Nicole LeClerc
Copy Editor: Kim Wimpsett
Assistant Production Director: Kari Brooks-Copony
Senior Production Editor: Laura Cheu
Compositor: Diana Van Winkle, Van Winkle Design
Proofreader: Linda Seifert
Indexer: Broccoli Information Management
Artist: Diana Van Winkle, Van Winkle Design
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail ,
or visit .
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley,
CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit .
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution

has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any
person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by
the information contained in this work.
The source code for this book is available to readers at in the Source Code section.
You will need to answer questions pertaining to this book in order to successfully download the code.
Ch00_6463_FINAL 7/27/06 3:00 PM Page ii
To Alenushka, with all my love.
Ch00_6463_FINAL 7/27/06 3:00 PM Page iii
Ch00_6463_FINAL 7/27/06 3:00 PM Page iv
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
About the Technical Reviewers
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Acknowledgments
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii
Introduction
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
PART 1
■ ■ ■
Quick Start
■CHAPTER 1 Simple Sample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
■CHAPTER 2 Enhancing the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
■CHAPTER 3 Making the Coding Easier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
PART 2
■ ■ ■
Underlying Structures
■CHAPTER 4 The Structure of a Managed Executable File . . . . . . . . . . . . . . . . . . . 41
■CHAPTER 5 Metadata Tables Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
PART 3
■ ■ ■

Fundamental Components
■CHAPTER 6 Modules and Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
■CHAPTER 7 Namespaces and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
■CHAPTER 8 Primitive Types and Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
■CHAPTER 9 Fields and Data Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
■CHAPTER 10 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
■CHAPTER 11 Generic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
■CHAPTER 12 Generic Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
PART 4
■ ■ ■
Inside the Execution Engine
■CHAPTER 13 IL Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
■CHAPTER 14 Managed Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
v
Ch00_6463_FINAL 7/27/06 3:00 PM Page v
PART 5
■ ■ ■
Special Components
■CHAPTER 15 Events and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
■CHAPTER 16 Custom Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
■CHAPTER 17 Security Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
■CHAPTER 18 Managed and Unmanaged Code Interoperation . . . . . . . . . . . . . . . 363
■CHAPTER 19 Multilanguage Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
PART 6
■ ■ ■
Appendixes
■APPENDIX A ILAsm Grammar Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
■APPENDIX B Metadata Tables Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
■APPENDIX C IL Instruction Set Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
■APPENDIX D IL Assembler and Disassembler Command-Line Options . . . . . . . 453

■APPENDIX E Offline Verification Tool Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Ch00_6463_FINAL 7/27/06 3:00 PM Page vi
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
About the Technical Reviewers
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Acknowledgments
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii
Introduction
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
PART 1
■ ■ ■
Quick Start
■CHAPTER 1 Simple Sample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Basics of the Common Language Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Simple Sample: The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Program Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Class Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Field Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Method Declaration
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Global Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Mapped Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Data Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Value Type As Placeholder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Calling Unmanaged Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Forward Declaration of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
■CHAPTER 2 Enhancing the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Compacting the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Protecting the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
■CHAPTER 3 Making the Coding Easier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Compilation Control Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Referencing the Current Class and Its Relatives . . . . . . . . . . . . . . . . . . . . . 37
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
vii
Ch00_6463_FINAL 7/27/06 3:00 PM Page vii
PART 2
■ ■ ■
Underlying Structures
■CHAPTER 4 The Structure of a Managed Executable File . . . . . . . . . . . . . 41
PE/COFF Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
MS-DOS Header/Stub and PE Signature . . . . . . . . . . . . . . . . . . . . . . . 42
COFF Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
PE Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Section Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Common Language Runtime Header
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Header Structure
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Flags Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
EntryPointToken Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
VTableFixups Field
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
StrongNameSignature Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Relocation Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Text Section

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Data Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Data Constants
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
V-Table
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Unmanaged Export Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Thread Local Storage
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Resources
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Unmanaged Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Managed Resources
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Summary
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Phase 1: Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Phase 2: Source Code Parsing
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Phase 3: Image Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Phase 4: Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
■CHAPTER 5 Metadata Tables Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
What Is Metadata? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Heaps and Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
General Metadata Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Metadata Table Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
RIDs and Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
RIDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

■CONTENTSviii
Ch00_6463_FINAL 7/27/06 3:00 PM Page viii
Coded Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Metadata Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
PART 3
■ ■ ■
Fundamental Components
■CHAPTER 6 Modules and Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
What Is an Assembly? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Private and Shared Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Application Domains As Logical Units of Execution . . . . . . . . . . . . . . . . . . 94
Manifest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Assembly Metadata Table and Declaration . . . . . . . . . . . . . . . . . . . . . . . . . 97
AssemblyRef Metadata Table and Declaration . . . . . . . . . . . . . . . . . . . . . . 99
Autodetection of Referenced Assemblies . . . . . . . . . . . . . . . . . . . . . 101
The Loader in Search of Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Module Metadata Table and Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . 105
ModuleRef Metadata Table and Declaration . . . . . . . . . . . . . . . . . . . . . . . 105
File Metadata Table and Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Managed Resource Metadata and Declaration . . . . . . . . . . . . . . . . . . . . . 107
ExportedType Metadata Table and Declaration
. . . . . . . . . . . . . . . . . . . . . 110
Order of Manifest Declarations in ILAsm
. . . . . . . . . . . . . . . . . . . . . . . . . . 112
Single-Module and Multimodule Assemblies . . . . . . . . . . . . . . . . . . . . . . 112
Summary of Metadata Validity Rules
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Assembly Table Validity Rules
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

AssemblyRef Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Module Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
ModuleRef Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
File Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
ManifestResource Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . 115
ExportedType Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
■CHAPTER 7 Namespaces and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Class Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
TypeDef Metadata Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
TypeRef Metadata Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
InterfaceImpl Metadata Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
NestedClass Metadata Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
ClassLayout Metadata Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
■CONTENTS
ix
Ch00_6463_FINAL 7/27/06 3:00 PM Page ix
Namespace and Full Class Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
ILAsm Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Full Class Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Class Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Class Visibility and Friend Assemblies . . . . . . . . . . . . . . . . . . . . . . . . 128
Class References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Parent of the Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Interface Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Class Layout Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Boxed and Unboxed Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

Instance Members of Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Derivation of Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Nested Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Class Augmentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Summary of the Metadata Validity Rules
. . . . . . . . . . . . . . . . . . . . . . . . . . 142
TypeDef Table Validity Rules
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Enumeration-Specific Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . 143
TypeRef Table Validity Rules
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
InterfaceImpl Table Validity Rules
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
NestedClass Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
ClassLayout Table Validity Rules
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
■CHAPTER 8 Primitive Types and Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Primitive Types in the Common Language Runtime . . . . . . . . . . . . . . . . . 145
Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Data Pointer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Function Pointer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Vectors and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Native Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Variant Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Representing Classes in Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Calling Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

■CONTENTSx
Ch00_6463_FINAL 7/27/06 3:00 PM Page x
Field Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Method and Property Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
MemberRef Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Indirect Call Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Local Variables Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Type Specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Summary of Signature Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
■CHAPTER 9 Fields and Data Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Field Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Defining a Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Referencing a Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Instance and Static Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Mapped Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Data Constants Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Explicit Layouts and Union Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Global Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Constructors vs. Data Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Summary of Metadata Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Field Table Validity Rules
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
FieldLayout Table Validity Rules
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
FieldRVA Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
FieldMarshal Table Validity Rules
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Constant Table Validity Rules
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

MemberRef Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
■CHAPTER 10 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Method Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Method Table Record Entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Method Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Method Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Method Implementation Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Method Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Referencing the Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Method Implementation Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Static, Instance, Virtual Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Explicit Method Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Method Overriding and Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
■CONTENTS
xi
Ch00_6463_FINAL 7/27/06 3:00 PM Page xi
Method Header Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Class Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Class Constructors and the beforefieldinit Flag . . . . . . . . . . . . . . . . 210
Module Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Instance Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Instance Finalizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Variable Argument Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Method Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Global Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Summary of Metadata Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Method Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Param Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
MethodImpl Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

■CHAPTER 11 Generic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Generic Type Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
GenericParam Metadata Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
GenericParamConstraint Metadata Table . . . . . . . . . . . . . . . . . . . . . 229
TypeSpec Metadata Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Constraint Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Defining Generic Types in ILAsm
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Addressing the Type Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Generic Type Instantiations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Defining Generic Types: Inheritance, Implementation, Constraints
. . . . 233
Defining Generic Types: Cyclic Dependencies . . . . . . . . . . . . . . . . . . . . . . 234
The Members of Generic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Virtual Methods in Generic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Nested Generic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Summary of the Metadata Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . 245
■CHAPTER 12 Generic Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Generic Method Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
MethodSpec Metadata Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Signatures of Generic Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Defining Generic Methods in ILAsm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Calling Generic Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Overriding Virtual Generic Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Summary of the Metadata Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . 257
■CONTENTSxii
Ch00_6463_FINAL 7/27/06 3:00 PM Page xii
PART 4
■ ■ ■

Inside the Execution Engine
■CHAPTER 13 IL Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Long-Parameter and Short-Parameter Instructions . . . . . . . . . . . . . . . . . 262
Labels and Flow Control Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Unconditional Branching Instructions . . . . . . . . . . . . . . . . . . . . . . . . 263
Conditional Branching Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Comparative Branching Instructions . . . . . . . . . . . . . . . . . . . . . . . . . 264
The switch Instruction
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
The break Instruction
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Managed EH Block Exiting Instructions . . . . . . . . . . . . . . . . . . . . . . 266
EH Block Ending Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
The ret Instruction
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Arithmetical Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Stack Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Constant Loading
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Indirect Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Indirect Storing
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Arithmetical Operations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Overflow Arithmetical Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Bitwise Operations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Shift Operations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Conversion Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

Overflow Conversion Operations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Logical Condition Check Instructions
. . . . . . . . . . . . . . . . . . . . . . . . 275
Block Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Addressing Arguments and Local Variables
. . . . . . . . . . . . . . . . . . . . . . . . 276
Method Argument Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Method Argument Address Loading . . . . . . . . . . . . . . . . . . . . . . . . . 277
Method Argument Storing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Method Argument List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Local Variable Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Local Variable Reference Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Local Variable Storing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Local Block Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Prefix Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Addressing Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Calling Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Direct Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
■CONTENTS
xiii
Ch00_6463_FINAL 7/27/06 3:00 PM Page xiii
Indirect Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Tail Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Constrained Virtual Calls
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Addressing Classes and Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Vector Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Vector Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Element Address Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

Element Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Element Storing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Code Verifiability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
■CHAPTER 14 Managed Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
EH Clause Internal Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Types of EH Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Label Form of EH Clause Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Scope Form of EH Clause Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Processing the Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Exception Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Loader Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
JIT Compiler Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Execution Engine Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Interoperability Exceptions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Subclassing the Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Unmanaged Exception Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Summary of EH Clause Structuring Rules
. . . . . . . . . . . . . . . . . . . . . . . . . 309
PART 5
■ ■ ■
Special Components
■CHAPTER 15 Events and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Events and Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Event Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
The Event Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
The EventMap Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
The MethodSemantics Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Event Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Property Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321

The Property Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
The PropertyMap Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
■CONTENTSxiv
Ch00_6463_FINAL 7/27/06 3:00 PM Page xiv
Property Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Summary of Metadata Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Event Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
EventMap Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Property Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
PropertyMap Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
MethodSemantics Table Validity Rules . . . . . . . . . . . . . . . . . . . . . . . 325
■CHAPTER 16 Custom Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Concept of a Custom Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
CustomAttribute Metadata Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Custom Attribute Value Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Verbal Description of Custom Attribute Value . . . . . . . . . . . . . . . . . . . . . . 331
Custom Attribute Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Classification of Custom Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Execution Engine and JIT Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Interoperation Subsystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Remoting Subsystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Visual Studio Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Assembly Linker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Common Language Specification (CLS) Compliance
. . . . . . . . . . . 344
Pseudocustom Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Summary of Metadata Validity Rules
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
■CHAPTER 17 Security Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347

Declarative Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Declarative Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Security Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Access Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Identity Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Custom Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Permission Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Declarative Security Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Permission Set Blob Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Security Attribute Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Summary of Metadata Validity Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
■CONTENTS
xv
Ch00_6463_FINAL 7/27/06 3:00 PM Page xv
■CHAPTER 18 Managed and Unmanaged Code Interoperation . . . . . . . . . 363
Thunks and Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
P/Invoke Thunks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Implementation Map Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
IJW Thunks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
COM Callable Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Runtime Callable Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Data Marshaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Blittable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
In/Out Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
String Marshaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Object Marshaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
More Object Marshaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Array Marshaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Delegate Marshaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Providing Managed Methods As Callbacks for Unmanaged Code

. . . . . 377
Managed Methods As Unmanaged Exports . . . . . . . . . . . . . . . . . . . . . . . . 380
Export Table Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
■CHAPTER 19 Multilanguage Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
IL Disassembler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Principles of Round-Tripping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Creative Round-Tripping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Using Class Augmentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Module Linking Through Round-Tripping . . . . . . . . . . . . . . . . . . . . . . . . . . 397
ASMMETA: Resolving Circular Dependencies . . . . . . . . . . . . . . . . . . . . . . 398
IL Inlining in High-Level Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Compiling in Debug Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
■CONTENTSxvi
Ch00_6463_FINAL 7/27/06 3:00 PM Page xvi
PART 6
■ ■ ■
Appendixes
■APPENDIX A ILAsm Grammar Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Lexical Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Auxiliary Lexical Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Data Type Nonterminals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Identifier Nonterminals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Class Referencing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Module-Level Declarations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Compilation Control Directives
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Module Parameter Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413

V-Table Fixup Table Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Manifest Declarations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Managed Types in Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Native Types in Marshaling Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Method and Field Referencing
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Class Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Generic Type Parameters Declaration
. . . . . . . . . . . . . . . . . . . . . . . . 421
Class Body Declarations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Field Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Method Declaration
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Method Body Declarations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
External Source Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Managed Exception Handling Directives
. . . . . . . . . . . . . . . . . . . . . 425
IL Instructions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Event Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Property Declaration
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Constant Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Custom Attribute Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Verbal Description of Custom Attribute Initialization Blob . . . . . . . 429
Security Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Aliasing of Types, Methods, Fields, and Custom Attributes . . . . . . . . . . . 431

Data Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
■APPENDIX B Metadata Tables Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
■APPENDIX C IL Instruction Set Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
■CONTENTS
xvii
Ch00_6463_FINAL 7/27/06 3:00 PM Page xvii
■APPENDIX D IL Assembler and Disassembler
Command-Line Options
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
IL Assembler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
IL Disassembler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Output Redirection Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
ILAsm Code-Formatting Options (PE Files Only) . . . . . . . . . . . . . . . 456
File Output Options (PE Files Only) . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
File or Console Output Options (PE Files Only) . . . . . . . . . . . . . . . . . 457
Metadata Summary Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
■APPENDIX E Offline Verification Tool Reference . . . . . . . . . . . . . . . . . . . . . . . 459
Error Codes and Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
■CONTENTSxviii
Ch00_6463_FINAL 7/27/06 3:00 PM Page xviii
About the Author
■SERGE LIDIN, a Russian-born Canadian with more than 20 years in the
computer industry, has programmed in more languages and for more
platforms than he can recall, in areas varying from astrophysics models
to industrial process simulations to transaction processing in financial
systems. From 1999 to mid-2005, he worked on the Microsoft .NET com-
mon language runtime team, where he designed and developed the IL
assembler, IL disassembler, Metadata validator, and run-time metadata
validation in the execution engine. Currently, Serge works on the Microsoft Phoenix team,

developing future frameworks for code generation and transformation. When not writing
software or sleeping, he plays tennis, skis, and reads books (his literary taste is below any
criticism). Serge shares his time between Vancouver, British Columbia, where his heart is,
and Redmond, Washington, where his brain is.
xix
Ch00_6463_FINAL 7/27/06 3:00 PM Page xix
Ch00_6463_FINAL 7/27/06 3:00 PM Page xx
About the Technical Reviewers
■JIM HOGG joined Microsoft seven years ago as a program manager—first on the .NET runtime
team, working on metadata, and now with the compiler team, working on optimizations. His
previous experience includes stints in computational physics, seismic processing, and operat-
ing systems.
■VANCE MORRISON has been working at Microsoft for the past seven years and has been
involved in the design of the .NET runtime since its inception. He drove the design for the
.NET intermediate language (IL) and was the lead for the just-in-time (JIT) compiler team
for much of that time. He is currently the compiler architect for Microsoft’s .NET runtime.
xxi
Ch00_6463_FINAL 7/27/06 3:00 PM Page xxi
Ch00_6463_FINAL 7/27/06 3:00 PM Page xxii
Acknowledgments
First I would like to thank the editing team from Apress who worked with me on this book:
Ewan Buckingham, Sofia Marchant, Kim Wimpsett (ah, those unforgettable discussions about
subjunctive tense vs. indicative tense!), and Laura Cheu. It was a pleasure and an honor to
work with such a highly professional team.
I would also like to thank my colleagues Jim Hogg and Vance Morrison, who were the
principal technical reviewers of this book. Jim worked on the common language runtime team
for quite a while and was the driving force of the ECMA/ISO standardization effort concerning
the .NET common language infrastructure. Vance has worked on the CLR team since the
team’s inception in 1998, he led the just-in-time compiler team for a long time, and he helped
me a lot with the IL assembler. Jim and Vance provided invaluable feedback on the draft of the

book, leaving no stone unturned.
And of course I would like to extend my thanks to my colleagues who helped me write
this book and the first IL assembler book by answering my questions and digging into the
specifications and source code with me: Larry Sullivan, Jim Miller, Bill Evans, Chris Brumme,
Mei-Chin Tsai, Erik Meijer, Thorsten Brunklaus, Ronald Laeremans, Kevin Ransom, Suzanne
Cook, Shajan Dasan, Craig Sinclair, and many others.
xxiii
Ch00_6463_FINAL 7/27/06 3:00 PM Page xxiii
Ch00_6463_FINAL 7/27/06 3:00 PM Page xxiv

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×