public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Re: [PATCH] Reimplement the .seh_scope directive
@ 2023-07-03  3:22 Julian Waters
  0 siblings, 0 replies; 3+ messages in thread
From: Julian Waters @ 2023-07-03  3:22 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 2663 bytes --]

A second revision of the patch, with a small bugfix

From 89463e94572a022b55c793d299857f85afadd2ff Mon Sep 17 00:00:00 2001
From: TheShermanTanker <tanksherman27@gmail.com>
Date: Mon, 3 Jul 2023 10:42:18 +0800
Subject: [PATCH] Reimplement the .seh_scope directive

---
 gas/config/obj-coff-seh.c | 20 ++++++++++++++++++++
 gas/config/obj-coff-seh.h |  8 +++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/gas/config/obj-coff-seh.c b/gas/config/obj-coff-seh.c
index 7b4486a..87eed0f 100644
--- a/gas/config/obj-coff-seh.c
+++ b/gas/config/obj-coff-seh.c
@@ -388,6 +388,20 @@ obj_coff_seh_handlerdata (int what ATTRIBUTE_UNUSED)
   switch_xdata (seh_ctx_cur->subsection + 1, seh_ctx_cur->code_seg);
 }

+static void obj_coff_seh_scope (int what)
+{
+  if (!verify_context_and_target (".seh_scope", seh_kind_x64))
+    return;
+
+  segT seg = now_seg;
+  int subseg = now_subseg;
+
+  seh_ctx_cur->scopes++;
+  switch_xdata (seh_ctx_cur->subsection + 1, seh_ctx_cur->code_seg);
+  s_rva (4);
+  subseg_set(seg, subseg);
+}
+
 /* Mark end of current context.  */

 static void
@@ -436,6 +450,8 @@ obj_coff_seh_proc (int what ATTRIBUTE_UNUSED)

   seh_ctx_cur = XCNEW (seh_context);

+  seh_ctx_cur->scopes = 0;
+
   seh_ctx_cur->code_seg = now_seg;

   if (seh_get_target_kind () == seh_kind_x64)
@@ -899,6 +915,10 @@ seh_x64_write_function_xdata (seh_context *c)
       emit_expr (&c->handler, 4);
     }

+  if (c->scopes > 0) {
+    out_four (c->scopes);
+  }
+
   /* Handler data will be tacked in here by subsections.  */
 }

diff --git a/gas/config/obj-coff-seh.h b/gas/config/obj-coff-seh.h
index 8d77bac..7c92921 100644
--- a/gas/config/obj-coff-seh.h
+++ b/gas/config/obj-coff-seh.h
@@ -57,6 +57,7 @@
   .seh_savexmm
   .seh_pushframe
   .seh_code
+  .seh_scope
 */

 /* architecture specific pdata/xdata handling.  */
@@ -75,7 +76,8 @@
  {"seh_no32", obj_coff_seh_32, 0}, \
  {"seh_handler", obj_coff_seh_handler, 0}, \
  {"seh_code", obj_coff_seh_code, 0}, \
- {"seh_handlerdata", obj_coff_seh_handlerdata, 0},
+ {"seh_handlerdata", obj_coff_seh_handlerdata, 0}, \
+ {"seh_scope", obj_coff_seh_scope, 0},

 /* Type definitions.  */

@@ -128,6 +130,9 @@ typedef struct seh_context
   int elems_count;
   int elems_max;
   seh_prologue_element *elems;
+
+  /* Scope count for x64 .xdata */
+  unsigned int scopes;
 } seh_context;

 typedef enum seh_kind {
@@ -151,6 +156,7 @@ static void obj_coff_seh_proc  (int);
 static void obj_coff_seh_handler (int);
 static void obj_coff_seh_handlerdata (int);
 static void obj_coff_seh_code (int);
+static void obj_coff_seh_scope (int);

 #define UNDSEC bfd_und_section_ptr

-- 
2.35.1.windows.2

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Reimplement the .seh_scope directive
@ 2023-07-03  6:12 Julian Waters
  0 siblings, 0 replies; 3+ messages in thread
From: Julian Waters @ 2023-07-03  6:12 UTC (permalink / raw)
  To: binutils


[-- Attachment #1.1: Type: text/plain, Size: 116 bytes --]

Here is the patch attached as a file, if the mailing list doesn't slice it
out once I send it

best regards,
Julian

[-- Attachment #2: 0001-Reimplement-the-.seh_scope-directive.patch --]
[-- Type: application/octet-stream, Size: 2904 bytes --]

From bb304d4eff54db673b13eaafa2b082862b80122e Mon Sep 17 00:00:00 2001
From: TheShermanTanker <tanksherman27@gmail.com>
Date: Mon, 3 Jul 2023 14:09:53 +0800
Subject: [PATCH] Reimplement the .seh_scope directive

---
 gas/config/obj-coff-seh.c | 26 ++++++++++++++++++++++++++
 gas/config/obj-coff-seh.h |  8 +++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/gas/config/obj-coff-seh.c b/gas/config/obj-coff-seh.c
index 7b4486a..e5dd8f2 100644
--- a/gas/config/obj-coff-seh.c
+++ b/gas/config/obj-coff-seh.c
@@ -388,6 +388,20 @@ obj_coff_seh_handlerdata (int what ATTRIBUTE_UNUSED)
   switch_xdata (seh_ctx_cur->subsection + 1, seh_ctx_cur->code_seg);
 }
 
+static void obj_coff_seh_scope (int what)
+{
+  if (!verify_context_and_target (".seh_scope", seh_kind_x64))
+    return;
+
+  segT seg = now_seg;
+  int subseg = now_subseg;
+
+  seh_ctx_cur->scopes++;
+  switch_xdata (seh_ctx_cur->subsection + 1, seh_ctx_cur->code_seg);
+  s_rva (4);
+  subseg_set(seg, subseg);
+}
+
 /* Mark end of current context.  */
 
 static void
@@ -436,6 +450,8 @@ obj_coff_seh_proc (int what ATTRIBUTE_UNUSED)
 
   seh_ctx_cur = XCNEW (seh_context);
 
+  seh_ctx_cur->scopes = 0;
+
   seh_ctx_cur->code_seg = now_seg;
 
   if (seh_get_target_kind () == seh_kind_x64)
@@ -902,6 +918,14 @@ seh_x64_write_function_xdata (seh_context *c)
   /* Handler data will be tacked in here by subsections.  */
 }
 
+static inline void
+seh_x64_maybe_write_scope_count (seh_context *c)
+{
+  if (c->scopes > 0) {
+    out_four (c->scopes);
+  }
+}
+
 /* Write out xdata for one function.  */
 
 static void
@@ -918,6 +942,8 @@ write_function_xdata (seh_context *c)
 
   seh_x64_write_function_xdata (c);
 
+  seh_x64_maybe_write_scope_count (c);
+
   subseg_set (save_seg, save_subseg);
 }
 
diff --git a/gas/config/obj-coff-seh.h b/gas/config/obj-coff-seh.h
index 8d77bac..7c92921 100644
--- a/gas/config/obj-coff-seh.h
+++ b/gas/config/obj-coff-seh.h
@@ -57,6 +57,7 @@
   .seh_savexmm
   .seh_pushframe
   .seh_code
+  .seh_scope
 */
 
 /* architecture specific pdata/xdata handling.  */
@@ -75,7 +76,8 @@
 	{"seh_no32", obj_coff_seh_32, 0}, \
 	{"seh_handler", obj_coff_seh_handler, 0}, \
 	{"seh_code", obj_coff_seh_code, 0}, \
-	{"seh_handlerdata", obj_coff_seh_handlerdata, 0},
+	{"seh_handlerdata", obj_coff_seh_handlerdata, 0}, \
+	{"seh_scope", obj_coff_seh_scope, 0},
 
 /* Type definitions.  */
 
@@ -128,6 +130,9 @@ typedef struct seh_context
   int elems_count;
   int elems_max;
   seh_prologue_element *elems;
+
+  /* Scope count for x64 .xdata */
+  unsigned int scopes;
 } seh_context;
 
 typedef enum seh_kind {
@@ -151,6 +156,7 @@ static void obj_coff_seh_proc  (int);
 static void obj_coff_seh_handler (int);
 static void obj_coff_seh_handlerdata (int);
 static void obj_coff_seh_code (int);
+static void obj_coff_seh_scope (int);
 
 #define UNDSEC bfd_und_section_ptr
 
-- 
2.35.1.windows.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] Reimplement the .seh_scope directive
@ 2023-07-02 10:18 Julian Waters
  0 siblings, 0 replies; 3+ messages in thread
From: Julian Waters @ 2023-07-02 10:18 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 3159 bytes --]

The .seh_scope directive was removed a little over a decade ago due to
being too Microsoft specific. This has proven to be a mistake as there is
now no easy way for reusable inline assembly to express Structured
Exception Handling scopes for Microsoft Windows targets, combined with the
handler count field that the xdata section requires. This patch
reimplements a simpler version of .seh_scope, with the proper semantics in
place

From cc9e143f8feb933ca8fc4b12deaab2985893b714 Mon Sep 17 00:00:00 2001
From: TheShermanTanker <tanksherman27@gmail.com>
Date: Sun, 2 Jul 2023 16:22:18 +0800
Subject: [PATCH] Reimplement .seh_scope

---
 gas/config/obj-coff-seh.c | 26 ++++++++++++++++++++++++--
 gas/config/obj-coff-seh.h |  5 ++++-
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/gas/config/obj-coff-seh.c b/gas/config/obj-coff-seh.c
index 7b4486a..b01a207 100644
--- a/gas/config/obj-coff-seh.c
+++ b/gas/config/obj-coff-seh.c
@@ -30,6 +32,7 @@ struct seh_seg_list {

 /* Local data.  */
 static seh_context *seh_ctx_cur = NULL;
+static unsigned int scope = 0;

 static htab_t seh_hash;

@@ -385,7 +388,21 @@ obj_coff_seh_handlerdata (int what ATTRIBUTE_UNUSED)
     return;
   demand_empty_rest_of_line ();

-  switch_xdata (seh_ctx_cur->subsection + 1, seh_ctx_cur->code_seg);
+  switch_xdata (seh_ctx_cur->subsection + (scope == 0) ? 1 : 2,
seh_ctx_cur->code_seg);
+}
+
+static void obj_coff_seh_scope (int what)
+{
+  if (!verify_context_and_target (".seh_scope", seh_kind_x64))
+    return;
+
+  segT seg = now_seg;
+  int subseg = now_subseg;
+
+  scope++;
+  switch_xdata (seh_ctx_cur->subsection + 2, seh_ctx_cur->code_seg);
+  s_rva (4);
+  subseg_set(seg, subseg);
 }

 /* Mark end of current context.  */
@@ -442,7 +459,7 @@ obj_coff_seh_proc (int what ATTRIBUTE_UNUSED)
     {
       x_segcur = seh_hash_find_or_make (seh_ctx_cur->code_seg, ".xdata");
       seh_ctx_cur->subsection = x_segcur->subseg;
-      x_segcur->subseg += 2;
+      x_segcur->subseg += 3;
     }

   SKIP_WHITESPACE ();
@@ -918,6 +935,11 @@ write_function_xdata (seh_context *c)

   seh_x64_write_function_xdata (c);

+  if (scope > 0) {
+    switch_xdata (c->subsection + 1, c->code_seg);
+    out_four (scope);
+  }
+
   subseg_set (save_seg, save_subseg);
 }

diff --git a/gas/config/obj-coff-seh.h b/gas/config/obj-coff-seh.h
index 8d77bac..feef17f 100644
--- a/gas/config/obj-coff-seh.h
+++ b/gas/config/obj-coff-seh.h
@@ -57,6 +57,7 @@
   .seh_savexmm
   .seh_pushframe
   .seh_code
+  .seh_scope
 */

 /* architecture specific pdata/xdata handling.  */
@@ -75,7 +76,8 @@
  {"seh_no32", obj_coff_seh_32, 0}, \
  {"seh_handler", obj_coff_seh_handler, 0}, \
  {"seh_code", obj_coff_seh_code, 0}, \
- {"seh_handlerdata", obj_coff_seh_handlerdata, 0},
+ {"seh_handlerdata", obj_coff_seh_handlerdata, 0}, \
+ {"seh_scope", obj_coff_seh_scope, 0},

 /* Type definitions.  */

@@ -151,6 +153,7 @@ static void obj_coff_seh_proc  (int);
 static void obj_coff_seh_handler (int);
 static void obj_coff_seh_handlerdata (int);
 static void obj_coff_seh_code (int);
+static void obj_coff_seh_scope (int);

 #define UNDSEC bfd_und_section_ptr

--
2.35.1.windows.2

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-07-03  6:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-03  3:22 [PATCH] Reimplement the .seh_scope directive Julian Waters
  -- strict thread matches above, loose matches on Subject: below --
2023-07-03  6:12 Julian Waters
2023-07-02 10:18 Julian Waters

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).