diff --git a/src/coreclr/tools/ILTrim/ILTrim/DependencyAnalysis/MethodBodyNode.cs b/src/coreclr/tools/ILTrim/ILTrim/DependencyAnalysis/MethodBodyNode.cs index 770fe36ec6c..96bee8b1012 100644 --- a/src/coreclr/tools/ILTrim/ILTrim/DependencyAnalysis/MethodBodyNode.cs +++ b/src/coreclr/tools/ILTrim/ILTrim/DependencyAnalysis/MethodBodyNode.cs @@ -113,6 +113,22 @@ public int Write(ModuleWritingContext writeContext) MethodBodyBlock bodyBlock = _module.PEReader.GetMethodBody(rva); var exceptionRegions = bodyBlock.ExceptionRegions; + // Use small exception regions when the code size of the try block and + // the handler code are less than 256 bytes and offsets smaller than 65536 bytes. + bool useSmallExceptionRegions = ExceptionRegionEncoder.IsSmallRegionCount(exceptionRegions.Length); + if (useSmallExceptionRegions) + { + foreach (var exceptionRegion in exceptionRegions) + { + if (!ExceptionRegionEncoder.IsSmallExceptionRegion(exceptionRegion.TryOffset, exceptionRegion.TryLength) || + !ExceptionRegionEncoder.IsSmallExceptionRegion(exceptionRegion.HandlerOffset, exceptionRegion.HandlerLength)) + { + useSmallExceptionRegions = false; + break; + } + } + } + BlobBuilder outputBodyBuilder = writeContext.GetSharedBlobBuilder(); byte[] bodyBytes = bodyBlock.GetILBytes(); ILReader ilReader = new ILReader(bodyBytes); @@ -187,7 +203,7 @@ public int Write(ModuleWritingContext writeContext) outputBodyBuilder.Count, bodyBlock.MaxStack, exceptionRegionCount: exceptionRegions.Length, - hasSmallExceptionRegions: false, + hasSmallExceptionRegions: useSmallExceptionRegions, (StandaloneSignatureHandle)writeContext.TokenMap.MapToken(bodyBlock.LocalSignature), bodyBlock.LocalVariablesInitialized ? MethodBodyAttributes.InitLocals : MethodBodyAttributes.None); BlobWriter instructionsWriter = new(bodyEncoder.Instructions);