icsharpcode/ILSpy · error · NotSupportedException

Cannot get RVA section prefix from module

Error message

Cannot get RVA section prefix from module

What it means

NotSupportedException from ReflectionDisassembler.GetRVASectionPrefix when the module is not a PEFile. Computing the RVA section prefix (the I_/T_/D_ label for a field's '.field ... at <prefix>_<rva>') needs PE section headers, which only exist on a PEFile. A module that is a bare metadata source (no PE backing) cannot supply them.

Source

Thrown at ICSharpCode.Decompiler/Disassembler/ReflectionDisassembler.cs:1400

				int rva = fieldDefinition.GetRelativeVirtualAddress();
				sectionPrefix = GetRVASectionPrefix(module, rva);
				output.Write(" at {1}_{0:X8}", rva, sectionPrefix);
			}

			var defaultValue = fieldDefinition.GetDefaultValue();
			if (!defaultValue.IsNil)
			{
				output.Write(" = ");
				WriteConstant(metadata, metadata.GetConstant(defaultValue));
			}

			return sectionPrefix;
		}

		char GetRVASectionPrefix(MetadataFile module, int rva)
		{
			if (module is not PEFile peFile)
				throw new NotSupportedException("Cannot get RVA section prefix from module");
			int sectionIndex = peFile.Reader.PEHeaders.GetContainingSectionIndex(rva);
			if (sectionIndex < 0)
				return 'D';
			var sectionHeader = peFile.Reader.PEHeaders.SectionHeaders[sectionIndex];
			switch (sectionHeader.Name)
			{
				case ".tls":
					return 'T';
				case ".text":
					return 'I';
				default:
					return 'D';
			}
		}
		#endregion

		#region Disassemble Property
		internal static readonly EnumNameCollection<PropertyAttributes> propertyAttributes = new EnumNameCollection<PropertyAttributes>() {

View on GitHub (pinned to 60c08fcb74)

Solutions

  1. Guard with 'if (module is PEFile)' before disassembling fields, or skip RVA-qualified fields for non-PE modules.
  2. Provide a real PEFile (open from the on-disk assembly path) when you need RVA output.
  3. Catch NotSupportedException and degrade the output by omitting the 'at <prefix>_<rva>' clause.

Example fix

// before
disassembler.DisassembleField(module, fieldHandle); // throws when module is not PEFile and field has RVA

// after
if (module is PEFile)
    disassembler.DisassembleField(module, fieldHandle);
else
    logger.Warning("Skipping RVA field on non-PE module");
Defensive patterns

Strategy: type-guard

Validate before calling

if (module is PEFile peFile)
    disassembler.DisassembleField(peFile, fieldHandle);
else
    logger.Warning("Skipping RVA field on non-PE module {Module}", module.FileName);

Type guard

static bool HasPeSectionHeaders(MetadataFile module) => module is PEFile;

Prevention

When it happens

Trigger: Disassembling a field that has HasFieldRVA on a MetadataFile that is not a PEFile, e.g. a module built from a MetadataReaderProvider created over a non-PE blob, or an in-memory/ephemeral module.

Common situations: Disassembling modules loaded via metadata-only paths without PE headers; CIL produced by assembly generators that do not emit a full PE image.

Related errors


AI-assisted analysis of icsharpcode/ILSpy@60c08fcb74 (2026-08-13). Data as JSON: /api/errors/7a63a885f2a0e3e5. Report an issue: GitHub.