Patchwork [Ada] Small adjustment for platforms using SEH

login
register
mail settings
Submitter Eric Botcazou
Date Jan. 22, 2013, 9:48 a.m.
Message ID <3064790.7YtnS0ExuW@polaris>
Download mbox | patch
Permalink /patch/214480/
State New
Headers show

Comments

Eric Botcazou - Jan. 22, 2013, 9:48 a.m.
This patch makes it so that an exception handler is installed around the main 
entry point to catch unhandled exceptions on SEH targets.

Tested on x86_64-suse-linux, applied on the mainline.


2013-01-22  Tristan Gingold  <gingold@adacore.com>

	* gcc-interface/gigi.h (ADT_unhandled_except_decl,
	ADT_unhandled_others_decl): New.
	(unhandled_others_decl, unhandled_except_decl): Define.
	* gcc-interface/trans.c: Include common/common-target.h.
	(gigi): Initialize them.
	(Subprogram_Body_to_gnu): On SEH targets, wrap the body of the main
	function in a try/catch clause.

Patch

Index: gcc-interface/gigi.h
===================================================================
--- gcc-interface/gigi.h	(revision 195310)
+++ gcc-interface/gigi.h	(working copy)
@@ -6,7 +6,7 @@ 
  *                                                                          *
  *                              C Header File                               *
  *                                                                          *
- *          Copyright (C) 1992-2012, Free Software Foundation, Inc.         *
+ *          Copyright (C) 1992-2013, Free Software Foundation, Inc.         *
  *                                                                          *
  * GNAT is free software;  you can  redistribute it  and/or modify it under *
  * terms of the  GNU General Public License as published  by the Free Soft- *
@@ -413,8 +413,10 @@  enum standard_datatypes
   ADT_reraise_zcx_decl,
   ADT_begin_handler_decl,
   ADT_end_handler_decl,
+  ADT_unhandled_except_decl,
   ADT_others_decl,
   ADT_all_others_decl,
+  ADT_unhandled_others_decl,
   ADT_LAST};
 
 /* Define kind of exception information associated with raise statements.  */
@@ -460,7 +462,9 @@  extern GTY(()) tree gnat_raise_decls_ext
 #define begin_handler_decl gnat_std_decls[(int) ADT_begin_handler_decl]
 #define others_decl gnat_std_decls[(int) ADT_others_decl]
 #define all_others_decl gnat_std_decls[(int) ADT_all_others_decl]
+#define unhandled_others_decl gnat_std_decls[(int) ADT_unhandled_others_decl]
 #define end_handler_decl gnat_std_decls[(int) ADT_end_handler_decl]
+#define unhandled_except_decl gnat_std_decls[(int) ADT_unhandled_except_decl]
 
 /* Routines expected by the gcc back-end. They must have exactly the same
    prototype and names as below.  */
Index: gcc-interface/trans.c
===================================================================
--- gcc-interface/trans.c	(revision 195310)
+++ gcc-interface/trans.c	(working copy)
@@ -37,6 +37,7 @@ 
 #include "bitmap.h"
 #include "cgraph.h"
 #include "target.h"
+#include "common/common-target.h"
 
 #include "ada.h"
 #include "adadecode.h"
@@ -504,6 +505,13 @@  gigi (Node_Id gnat_root, int max_gnat_no
 			   Empty);
   DECL_IGNORED_P (end_handler_decl) = 1;
 
+  unhandled_except_decl
+    = create_subprog_decl (get_identifier ("__gnat_unhandled_except_handler"),
+			   NULL_TREE,
+			   ftype, NULL_TREE, false, true, true, true, NULL,
+			   Empty);
+  DECL_IGNORED_P (unhandled_except_decl) = 1;
+
   reraise_zcx_decl
     = create_subprog_decl (get_identifier ("__gnat_reraise_zcx"), NULL_TREE,
 			   ftype, NULL_TREE, false, true, true, true, NULL,
@@ -639,6 +647,12 @@  gigi (Node_Id gnat_root, int max_gnat_no
 		       integer_type_node, NULL_TREE, true, false, true, false,
 		       NULL, Empty);
 
+  unhandled_others_decl
+    = create_var_decl (get_identifier ("UNHANDLED_OTHERS"),
+		       get_identifier ("__gnat_unhandled_others_value"),
+		       integer_type_node, NULL_TREE, true, false, true, false,
+		       NULL, Empty);
+
   main_identifier_node = get_identifier ("main");
 
   /* Install the builtins we might need, either internally or as
@@ -3495,6 +3509,26 @@  Subprogram_Body_to_gnu (Node_Id gnat_nod
   set_end_locus_from_node (gnu_result, gnat_node);
   set_end_locus_from_node (gnu_subprog_decl, gnat_node);
 
+  /* On SEH targets, install an exception handler around the main entry
+     point to catch unhandled exceptions.  */
+  if (DECL_NAME (gnu_subprog_decl) == main_identifier_node
+      && targetm_common.except_unwind_info (&global_options) == UI_SEH)
+    {
+      tree t;
+      tree etype;
+
+      t = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
+			   1, integer_zero_node);
+      t = build_call_n_expr (unhandled_except_decl, 1, t);
+
+      etype = build_unary_op (ADDR_EXPR, NULL_TREE, unhandled_others_decl);
+      etype = tree_cons (NULL_TREE, etype, NULL_TREE);
+
+      t = build2 (CATCH_EXPR, void_type_node, etype, t);
+      gnu_result = build2 (TRY_CATCH_EXPR, TREE_TYPE (gnu_result),
+			   gnu_result, t);
+    }
+
   end_subprog_body (gnu_result);
 
   /* Finally annotate the parameters and disconnect the trees for parameters