## Image Manipulation

Images are the component of assemblies that actually hold the CIL code and metadata in the extended PE/COFF file.

### Synopsis

#include <metadata/image.h> typedef struct _MonoImage MonoImage; typedef enum { MONO_IMAGE_OK, MONO_IMAGE_ERROR_ERRNO, MONO_IMAGE_MISSING_ASSEMBLYREF, MONO_IMAGE_IMAGE_INVALID } MonoImageOpenStatus; mono_load_image MonoImage* mono_image_open (const char *fname, MonoImageOpenStatus *status); mono_image_open_full mono_image_open_from_data mono_image_open_from_data_full void mono_image_close (MonoImage *image); void mono_image_addref (MonoImage *image); mono_image_load_file_for_image mono_image_open_from_data_with_name MonoImage* mono_image_load_module (MonoImage *image, int idx); mono_image_get_guid MonoAssembly* mono_image_get_assembly (MonoImage *image); guint32 mono_image_get_entry_point (MonoImage *image); const char* mono_image_get_filename (MonoImage *image); const char* mono_image_get_name (MonoImage *image); const char* mono_image_get_resource (MonoImage *image, guint32 offset, guint32 *size); mono_image_get_table_info mono_image_get_table_rows gboolean mono_image_is_dynamic (MonoImage *image); mono_image_loaded_by_guid MonoImage* mono_image_loaded (const char *name); gpointer mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, gunichar2 *name); const char * mono_image_strerror (MonoImageOpenStatus status); MonoMethod* mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class; guint32 mono_image_strong_name_position (MonoImage *image, guint32 *size); const char* mono_image_get_public_key (MonoImage *image, guint32 *size); const char* mono_image_get_strong_name (MonoImage *image, guint32 *size); gboolean mono_image_has_authenticode_entry (MonoImage *image); mono_image_rva_map int mono_image_ensure_section_idx (MonoImage *image, int section); int mono_image_ensure_section (MonoImage *image, const char *section); mono_image_init void mono_images_init (void); void mono_images_cleanup (void); mono_image_add_to_name_cache mono_image_fixup_vtable mono_image_loaded_by_guid_full mono_image_loaded_full mono_lookup_icall_symbol

### Image APIs

Images are the actual elements in the runtime that contain the actual code being executed.

### Opening and closing MonoImages

`mono_load_image`
Syntax
mono_load_image

`mono_image_open`
Syntax
MonoImage* mono_image_open (const char *fname, MonoImageOpenStatus *status)

Parameters

fname | filename that points to the module we want to open
status | An error condition is returned in this field

Return value
An open image of type %MonoImage or `NULL` on error. The caller holds a temporary reference to the returned image which should be cleared when no longer needed by calling mono_image_close (). if `NULL`, then check the value of status for details on the error
Description

`mono_image_open_full`
Syntax
mono_image_open_full

`mono_image_open_from_data`
Syntax
mono_image_open_from_data

`mono_image_open_from_data_full`
Syntax
mono_image_open_from_data_full

`mono_image_close`
Syntax
void mono_image_close (MonoImage *image)

Parameters

image | The image file we wish to close

Description

Closes an image file, deallocates all memory consumed and unmaps all possible sections of the file
`mono_image_addref`
Syntax
void mono_image_addref (MonoImage *image)

Parameters

image | The image file we wish to add a reference to

Description

Increases the reference count of an image.
`mono_image_load_file_for_image`
Syntax
mono_image_load_file_for_image

`mono_image_open_from_data_with_name`
Syntax
mono_image_open_from_data_with_name

`mono_image_load_module`
Syntax
MonoImage* mono_image_load_module (MonoImage *image, int idx)

Description

Load the module with the one-based index IDX from IMAGE and return it. Return `NULL` if it cannot be loaded.

### Image Information

`mono_image_get_guid`
Syntax
mono_image_get_guid

`mono_image_get_assembly`
Syntax
MonoAssembly* mono_image_get_assembly (MonoImage *image)

Parameters

image | the MonoImage.

Return value
the assembly that holds this image.
Description

Use this routine to get the assembly that owns this image.

`mono_image_get_entry_point`
Syntax
guint32 mono_image_get_entry_point (MonoImage *image)

Parameters

image | the image where the entry point will be looked up.

Return value
the token for the entry point method in the image
Description

Use this routine to determine the metadata token for method that has been flagged as the entry point.

`mono_image_get_filename`
Syntax
const char* mono_image_get_filename (MonoImage *image)

Parameters

image | a MonoImage

Return value
the filename.
Description

Used to get the filename that hold the actual MonoImage

`mono_image_get_name`
Syntax
const char* mono_image_get_name (MonoImage *image)

Parameters

name | a MonoImage

Return value
the name of the assembly.
Description

`mono_image_get_resource`
Syntax
const char* mono_image_get_resource (MonoImage *image, guint32 offset, guint32 *size)

Parameters

image | the image where the resource will be looked up.
offset | The offset to add to the resource
size | a pointer to an int where the size of the resource will be stored

Return value
the pointer to the resource whose offset is offset.
Description

This is a low-level routine that fetches a resource from the metadata that starts at a given offset. The size parameter is filled with the data field as encoded in the metadata.

`mono_image_get_table_info`
Syntax
mono_image_get_table_info

`mono_image_get_table_rows`
Syntax
mono_image_get_table_rows

`mono_image_is_dynamic`
Syntax
gboolean mono_image_is_dynamic (MonoImage *image)

Parameters

image | the MonoImage

Return value
`TRUE` if the image was created dynamically, `FALSE` if not.
Description

Determines if the given image was created dynamically through the System.Reflection.Emit API

`mono_image_loaded_by_guid`
Syntax
mono_image_loaded_by_guid

`mono_image_loaded`
Syntax
MonoImage* mono_image_loaded (const char *name)

Parameters

name | path or assembly name of the image to load

Return value
the loaded MonoImage, or `NULL` on failure.
Description

This routine verifies that the given image is loaded. Reflection-only loads do not count.

`mono_image_lookup_resource`
Syntax
gpointer mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, gunichar2 *name)

Parameters

image | the image to look up the resource in
res_id | A MONO_PE_RESOURCE_ID_ that represents the resource ID to lookup.
lang_id | The language id.
name | the resource name to lookup.

Return value
`NULL` if not found, otherwise a pointer to the in-memory representation of the given resource. The caller should free it using g_free () when no longer needed.
Description

`mono_image_strerror`
Syntax
const char * mono_image_strerror (MonoImageOpenStatus status)

Parameters

status | an code indicating the result from a recent operation

Return value
a string describing the error
Description

`mono_get_method_constrained`
Syntax
MonoMethod* mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class, MonoGenericContext *context, MonoMethod **cil_method)

Description

This is used when JITing the `constrained.' opcode.

This returns two values: the contrained method, which has been inflated as the function return value; And the original CIL-stream method as declared in cil_method. The later is used for verification.

### Public Keys, Strong Names and Certificates

`mono_image_strong_name_position`
Syntax
guint32 mono_image_strong_name_position (MonoImage *image, guint32 *size)

Parameters

image | a MonoImage
size | a guint32 pointer, or `NULL`.

Return value
the position within the image file where the strong name is stored.
Description

If the image has a strong name, and size is not `NULL`, the value pointed to by size will have the size of the strong name.

`mono_image_get_public_key`
Syntax
const char* mono_image_get_public_key (MonoImage *image, guint32 *size)

Parameters

image | a MonoImage
size | a guint32 pointer, or `NULL`.

Return value
`NULL` if the image does not have a public key, or a pointer to the public key.
Description

This is used to obtain the public key in the image.

If the image has a public key, and size is not `NULL`, the value pointed to by size will have the size of the public key.

`mono_image_get_strong_name`
Syntax
const char* mono_image_get_strong_name (MonoImage *image, guint32 *size)

Parameters

image | a MonoImage
size | a guint32 pointer, or `NULL`.

Return value
`NULL` if the image does not have a strong name, or a pointer to the public key.
Description

If the image has a strong name, and size is not `NULL`, the value pointed to by size will have the size of the strong name.

`mono_image_has_authenticode_entry`
Syntax
gboolean mono_image_has_authenticode_entry (MonoImage *image)

Parameters

image | the MonoImage

Return value
`TRUE` if the image contains an authenticode entry in the PE directory.
Description

Use this routine to determine if the image has a Authenticode Certificate Table.

### Low-level features

`mono_image_rva_map`
Syntax
mono_image_rva_map

`mono_image_ensure_section_idx`
Syntax
int mono_image_ensure_section_idx (MonoImage *image, int section)

Parameters

image | The image we are operating on
section | section number that we will load/map into memory

Return value
`TRUE` on success
Description

This routine makes sure that we have an in-memory copy of an image section (.text, .rsrc, .data).

`mono_image_ensure_section`
Syntax
int mono_image_ensure_section (MonoImage *image, const char *section)

Parameters

image | The image we are operating on
section | section name that we will load/map into memory

Return value
`TRUE` on success
Description

This routine makes sure that we have an in-memory copy of an image section (.text, .rsrc, .data).

`mono_image_init`
Syntax
mono_image_init

`mono_images_init`
Syntax
void mono_images_init (void)

Description

Initialize the global variables used by this module.
`mono_images_cleanup`
Syntax
void mono_images_cleanup (void)

Description

Free all resources used by this module.
`mono_image_add_to_name_cache`
Syntax
mono_image_add_to_name_cache

`mono_image_fixup_vtable`
Syntax
mono_image_fixup_vtable

`mono_image_loaded_by_guid_full`
Syntax
mono_image_loaded_by_guid_full

`mono_image_loaded_full`
Syntax
mono_image_loaded_full