I work for Red Gate Software, a software company based in Cambridge, UK. In this series of posts, I’ll be discussing how we develop software at Red Gate, and what we get up to, all from a dev’s perspective. Before I start the series proper, in this post I’ll give you a brief background to … Read more
If you’ve just joined this series, I highly recommend you read the previous posts in this series, starting here, or at least these posts, covering the CLR metadata tables. Before we look at custom attribute encoding, we first need to have a brief look at how signatures are encoded in an assembly in general. Signature … Read more
At TechEd this year, there’s only a few sessions and expo booths aimed squarely at devs rather than sysadmins and DBAs, but one of the things I picked up on was what’s going into Visual Studio vNext. There’s no shiny new UI features (that they’ve announced so far…) but they are doing a whole lot … Read more
Just a brief note here – I’ll be going to TechEd Atlanta next week as part of a huge Red Gate contingent. My profile and session schedule is public on the TechEd directory if you want to come find me & have a chat, otherwise I’ll likely be prowling around the C# and .NET talks … Read more
In Windows XP and above, the OS loader knows natively what to do with .NET executable assemblies, and fires up an instance of the CLR. However, .NET also runs on Windows 98, ME, NT 4.0, and 2000. When you run a .NET assembly on the older operating systems, the CLR has to be loaded somehow. … Read more
The DOS stub at the top of the file is the first thing you notice when you open a .NET assembly in a hex editor. But what do those bytes mean, and what do they do? As I discussed in a previous post, the first 64 bytes are the DOS header, and the next 64 … Read more
Any close look at the method definitions in a .NET assembly has to start off with the method’s information in the metadata tables – the MethodDef. So lets do that. MethodDef The MethodDef entry for the entrypoint method in my TinyAssembly example used in previous posts has the following bytes: According to the CLR spec, … Read more
To recap from my previous posts, .NET assemblies are built on top of the PE file format, which is used for all executables and dlls in Windows. The PE file is split up into sections, and the data read by the CLR is all inside the .text section. Inside that section, the CLR data starts … Read more
Before we look any further at the CLR metadata, we need a quick diversion to understand how the metadata is actually stored. Encoding table information As an example, we’ll have a look at a row in the TypeDef table. According to the spec, each TypeDef consists of the following: Flags specifying various properties of the … Read more
Before we look at the bytes comprising the CLR-specific data inside an assembly, we first need to understand the logical format of the metadata (For this post I only be looking at simple pure-IL assemblies; mixed-mode assemblies & other things complicates things quite a bit). Metadata streams Most of the CLR-specific data inside an assembly … Read more
Today, I’ll be starting a look at what exactly is inside a .NET assembly – how the metadata and IL is stored, how Windows knows how to load it, and what all those bytes are actually doing. First of all, we need to understand the PE file format. PE files .NET assemblies are built on … Read more
In my blog posts on fault and filter exception handlers, I said that the same behaviour could be replicated using normal catch blocks. Well, that isn’t entirely true… Changing the handler semantics Consider the following: If the filter handler is engaged (true is inserted into the exception dictionary), then the following gets printed to the … Read more
Filter handlers are the second type of exception handler that aren’t accessible from C#. Unlike the other handler types, which have defined conditions for when the handlers execute, filter lets you use custom logic to determine whether the handler should be run. However, similar to a catch block, the filter block does not get run … Read more
Fault event handlers are one of the two handler types that aren’t available in C#. It behaves exactly like a finally, except it is only run if control flow exits the block due to an exception being thrown. As an example, take the following method: If we pass true to this method the following gets … Read more
An exception handler in C# combines the IL catch and finally exception handling clauses into a single try statement: How does this get compiled into IL? Initial implementation If you remember from my earlier post, finally clauses must be specified with their own .try clause. So, for the initial implementation, we take the try/catch/finally, and … Read more
Control flow in and around exception handlers is tightly controlled, due to the various ways the handler blocks can be executed. To start off with, I’ll describe what SEH does when an exception is thrown. Handling exceptions When an exception is thrown, the CLR stops program execution at the throw statement and searches up the … Read more
Today, I’ll be starting a look at the Structured Exception Handling mechanism within the CLR. Exception handling is quite a complicated business, and, as a result, the rules governing exception handling clauses in IL are quite strict; you need to be careful when writing exception clauses in IL. Exception handlers Exception handlers are specified using … Read more
Custom attributes were designed to make the .NET framework extensible; if a .NET language needs to store additional metadata on an item that isn’t expressible in IL, then an attribute could be applied to the IL item to represent this metadata. For instance, the C# compiler uses DecimalConstantAttribute and DateTimeConstantAttribute to represent compile-time decimal or … Read more
In IL, volatile is an instruction prefix used to set a memory barrier at that instruction. However, in C#, volatile is applied to a field to indicate that all accesses on that field should be prefixed with volatile. As I mentioned in my previous post, this means that the field definition needs to store this … Read more
The constructor generic constraint is a slightly wierd one. The ECMA specification simply states that it: constrains [the type] to being a concrete reference type (i.e., not abstract) that has a public constructor taking no arguments (the default constructor), or to being a value type. There seems to be no reference within the spec to … Read more