Skip to content

Option to emit exception handler clauses #225

@Astaelan

Description

@Astaelan

Would you consider adding an --emit-eh-info option to enable SettingsTunnel.EmitEHInfo?

I am working on an operating system that uses the UEFI OS and none (custom) lib settings, but I want the exception handler clauses available to implement try/catch/finally in my project.

I am currently forking it as a submodule to achieve this but it added 4 or 5 minutes to my docker build process, so it would be nice to have it in a precompiled release, not to mention the hassle of nuget on github inside docker to build from source. This is probably not the last thing I'll need adjustment in bflat for, but it's the first thing I've run across.

If you're willing to take upstream PR's, I can provide one for what I'm looking for.

Thanks for your consideration.

  diff --git a/src/bflat/BuildCommand.cs b/src/bflat/BuildCommand.cs
  index 0ff3ca9..a15c031 100644
  --- a/src/bflat/BuildCommand.cs
  +++ b/src/bflat/BuildCommand.cs
  @@ -47,6 +47,7 @@ internal class BuildCommand : CommandBase
       private static Option<bool> NoStackTraceDataOption = new Option<bool>("--no-stacktrace-data", "Disable support for textual stack traces");
       private static Option<bool> NoGlobalizationOption = new Option<bool>("--no-globalization", "Disable support for globalization (use invariant mode)");
       private static Option<bool> NoExceptionMessagesOption = new Option<bool>("--no-exception-messages", "Disable exception messages");
  +    private static Option<bool> EmitEHInfoOption = new Option<bool>("--emit-eh-info", "Emit exception handling tables (for custom runtimes with EH support)");
       private static Option<bool> NoPieOption = new Option<bool>("--no-pie", "Do not generate position independent executable");

       private static Option<bool> NoLinkOption = new Option<bool>("-c", "Produce object file, but don't run linker");
  @@ -113,6 +114,7 @@ internal class BuildCommand : CommandBase
               NoStackTraceDataOption,
               NoGlobalizationOption,
               NoExceptionMessagesOption,
  +            EmitEHInfoOption,
               NoPieOption,
               SeparateSymbolsOption,
               CommonOptions.NoDebugInfoOption,
  @@ -328,6 +330,13 @@ internal class BuildCommand : CommandBase
               //    SettingsTunnel.EmitUnwindInfo = false;
           }

  +        // Allow explicit override of EH info for custom runtimes with exception handling support
  +        bool emitEHInfo = result.GetValueForOption(EmitEHInfoOption);
  +        if (emitEHInfo)
  +        {
  +            SettingsTunnel.EmitEHInfo = true;
  +        }
  +
           bool supportsReflection = !disableReflection && systemModuleName == DefaultSystemModule;

           //

This adds a --emit-eh-info flag that sets SettingsTunnel.EmitEHInfo = true, enabling NativeAOT's exception handling table emission for custom runtimes that implement their own EH support.

  [EH Test] Testing exception handling...
  [EH Test] Inside try block
  [EH] RhpThrowEx_Handler called, exception=0x000000000000F358 RIP=0x000000000DD02232
  [EH] DispatchNativeAotException at RIP=0x000000000DD02232
  [EH] Frame 0: func RVA=0x0001F1C0-0x0001F259 unwindRVA=0x00002634
  [EH]   unwind ver=1 flags=0x0000 prolog=6 codes=3
  [EH]   unwindBlockFlags=0x0004 @offset 10 ehInfoRVA=0x000025D0 -> OK
  [EH]   ehInfo=0x000000000DCE55D0 first bytes: 0002 0080 0031 0003 0065 0002 0068 0020
  [EH] Found 1 EH clause(s), offset in function: 0x00000072
  [EH] Clause 0: kind=0 try=[0x00000040-0x00000073] handler=0x00000099 typeRva=0x0000463E
  [EH] Found matching clause!
  [EH] Pass 1: Found handler in frame 0 at offset 0x00000099
  [EH] Transferring to handler at 0x000000000DD02259
  [EH Test] Caught Exception!
  [EH Test] Exception handling test PASSED

Proof it works, and also that UEFI on the .NET 10 RC1 build is working correctly (I also tested with zero lib prior to starting my own lib).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions