* config/obj-coff-seh.c: Rewrite the entire file.
authorRichard Henderson <rth@redhat.com>
Mon, 30 Aug 2010 21:51:28 +0000 (21:51 +0000)
committerRichard Henderson <rth@redhat.com>
Mon, 30 Aug 2010 21:51:28 +0000 (21:51 +0000)
(symtab, symptr, reltab, relcount, relsize): Remove.
(seh_ctx_root, seh_ctx): Remove.
(xdata_seg, xdata_subseg, pdata_seg): New.
(switch_xdata, switch_pdata): New.
(verify_context, verify_context_and_target, skip_whitespace_and_comma):
New parsing functions.  Rewrite all parsing functions to use them.
(obj_coff_seh_32): Fix != arm thinko.
(obj_coff_seh_handler): For x64, don't accept handler pointer here,
only flags.
(obj_coff_seh_handlerdata): New.
(do_seh_endproc): Split out of ...
(obj_coff_seh_endproc): ... here.
(obj_coff_seh_proc): Use it, if needed.
(seh_x64_make_prologue_element): Use XRESIZEVEC, symbol_temp_new_now.
(seh_x64_read_reg): Remove mm_regs alternative.  Tidy integer reg
alternatives.  Don't slurp commas.
(seh_read_offset): Remove.
(obj_coff_seh_pushframe): Split out from obj_coff_seh_push.
(obj_coff_seh_scope): Remove.
(obj_coff_seh_save): Decide UWOP_SAVE_* vs _FAR immediately.
(obj_coff_seh_stackalloc): Decide _SMALL vs _LARGE immediately.
(out_one, out_two, out_four): New.
(seh_x64_write_prologue_data, seh_x64_size_prologue_data,
seh_x64_write_function_xdata, write_function_xdata): Rewrite
from seh_x64_write_xdata, seh_needed_unwind_info, seh_store_elm_data,
seh_getelm_data_size, seh_getsize_of_unwind_entry,
seh_make_unwind_entry, seh_getsize_unwind_data, and
seh_create_unwind_data.
(seh_arm_write_function_pdata): Rewrite from seh_arm_create_pdata.
(write_function_pdata): Rewrite from make_function_entry_pdata.
(seh_write_text_eh_data, make_function_entry_pdata,
seh_arm_create_pdata, seh_arm_write_pdata, seh_reloc, save_relocs,
seh_symbol_init, seh_symbol, quick_section, seh_emit_rva,
seh_emit_long, seh_make_globl, seh_make_section2, seh_make_section,
seh_make_xlbl_name, make_seh_text_label, seh_fill_pcsyms,
seh_needed_unwind_info, seh_store_elm_data, seh_getelm_data_size,
seh_getsize_of_unwind_entry, seh_make_unwind_entry,
seh_getsize_unwind_data, seh_create_unwind_data,
seh_make_function_entry_xdata, seh_x64_makescope_elem): Remove.
* config/obj-coff-seh.h (SEH_CMDS): Remove seh_savemm, seh_scope.
Add seh_handlerdata.  Adjust function/what arguments for
seh_savereg, seh_pushframe, seh_stackalloc.
(struct seh_prologue_element): Adjust members to closer match
the elements of the UNWIND_CODE structure.
(struct seh_scope_elem): Remove.
(struct seh_context): Replace char* members with symbolS or
expressionS as appropriate.  Sort members by ARM/x64 applicability.
Remove obsolete stuff wrt direct symbol and reloc manipulation.

gas/ChangeLog
gas/config/obj-coff-seh.c
gas/config/obj-coff-seh.h

index abec391..420c41e 100644 (file)
@@ -1,3 +1,55 @@
+2010-08-30  Richard Henderson   <rth@redhat.com>
+
+       * config/obj-coff-seh.c: Rewrite the entire file.
+       (symtab, symptr, reltab, relcount, relsize): Remove.
+       (seh_ctx_root, seh_ctx): Remove.
+       (xdata_seg, xdata_subseg, pdata_seg): New.
+       (switch_xdata, switch_pdata): New.
+       (verify_context, verify_context_and_target, skip_whitespace_and_comma):
+       New parsing functions.  Rewrite all parsing functions to use them.
+       (obj_coff_seh_32): Fix != arm thinko.
+       (obj_coff_seh_handler): For x64, don't accept handler pointer here,
+       only flags.
+       (obj_coff_seh_handlerdata): New.
+       (do_seh_endproc): Split out of ...
+       (obj_coff_seh_endproc): ... here.
+       (obj_coff_seh_proc): Use it, if needed.
+       (seh_x64_make_prologue_element): Use XRESIZEVEC, symbol_temp_new_now.
+       (seh_x64_read_reg): Remove mm_regs alternative.  Tidy integer reg
+       alternatives.  Don't slurp commas.
+       (seh_read_offset): Remove.
+       (obj_coff_seh_pushframe): Split out from obj_coff_seh_push.
+       (obj_coff_seh_scope): Remove.
+       (obj_coff_seh_save): Decide UWOP_SAVE_* vs _FAR immediately.
+       (obj_coff_seh_stackalloc): Decide _SMALL vs _LARGE immediately.
+       (out_one, out_two, out_four): New.
+       (seh_x64_write_prologue_data, seh_x64_size_prologue_data,
+       seh_x64_write_function_xdata, write_function_xdata): Rewrite
+       from seh_x64_write_xdata, seh_needed_unwind_info, seh_store_elm_data,
+       seh_getelm_data_size, seh_getsize_of_unwind_entry,
+       seh_make_unwind_entry, seh_getsize_unwind_data, and
+       seh_create_unwind_data.
+       (seh_arm_write_function_pdata): Rewrite from seh_arm_create_pdata.
+       (write_function_pdata): Rewrite from make_function_entry_pdata.
+       (seh_write_text_eh_data, make_function_entry_pdata,
+       seh_arm_create_pdata, seh_arm_write_pdata, seh_reloc, save_relocs,
+       seh_symbol_init, seh_symbol, quick_section, seh_emit_rva,
+       seh_emit_long, seh_make_globl, seh_make_section2, seh_make_section,
+       seh_make_xlbl_name, make_seh_text_label, seh_fill_pcsyms,
+       seh_needed_unwind_info, seh_store_elm_data, seh_getelm_data_size,
+       seh_getsize_of_unwind_entry, seh_make_unwind_entry,
+       seh_getsize_unwind_data, seh_create_unwind_data,
+       seh_make_function_entry_xdata, seh_x64_makescope_elem): Remove.
+       * config/obj-coff-seh.h (SEH_CMDS): Remove seh_savemm, seh_scope.
+       Add seh_handlerdata.  Adjust function/what arguments for
+       seh_savereg, seh_pushframe, seh_stackalloc.
+       (struct seh_prologue_element): Adjust members to closer match
+       the elements of the UNWIND_CODE structure.
+       (struct seh_scope_elem): Remove.
+       (struct seh_context): Replace char* members with symbolS or
+       expressionS as appropriate.  Sort members by ARM/x64 applicability.
+       Remove obsolete stuff wrt direct symbol and reloc manipulation.
+
 2010-08-25  Alan Modra  <amodra@gmail.com>
 
        * NEWS: Mention ampersand in macro change.
index 06394eb..b9c5b50 100644 (file)
@@ -1,5 +1,5 @@
 /* seh pdata/xdata coff object file format
-   Copyright 2009
+   Copyright 2009, 2010
    Free Software Foundation, Inc.
 
    This file is part of GAS.
 
 #include "obj-coff-seh.h"
 
-/* Forward declarations.  */
-static seh_kind seh_get_target_kind (void);
-static int seh_symbol (bfd *, const char *, const char *, const char *, asection *, int, int);
-static void seh_reloc (bfd *, bfd_size_type, int, int);
-static void save_relocs (asection *sec);
-static asection *quick_section (bfd *abfd, const char *name, int flags, int align);
-static void seh_symbol_init (bfd *abfd, unsigned int added);
-static void seh_emit_rva (const char *);
-static void seh_emit_long (const char *);
-static void seh_make_globl (char *);
-static segT seh_make_section (void);
-static segT seh_make_section2 (const char *section_name, unsigned flags);
-static char *seh_make_xlbl_name (seh_context *);
-static char *make_seh_text_label (seh_context *c, symbolS **addr);
-
-static void seh_write_text_eh_data (const char *hnd, const char *hnd_data);
-static void seh_emit_rva (const char *name);
-static int seh_needed_unwind_info (seh_context *);
-static void seh_fill_pcsyms (const seh_context *c, char **, int *);
-static size_t seh_getelm_data_size (const seh_context *, int, int, size_t *);
-static void seh_store_elm_data (const seh_context *, int, int, unsigned char *, valueT base_off);
-
-static size_t seh_getsize_of_unwind_entry (seh_context *, int, int, int);
-static void seh_make_unwind_entry (const seh_context *, char *, int, int, int, unsigned char *, size_t *, int);
-static size_t seh_getsize_unwind_data (seh_context *);
-static void seh_create_unwind_data (seh_context *, unsigned char *, size_t);
-static void seh_make_function_entry_xdata (seh_context *, char *, char *, char *, unsigned char *, size_t *,int);
-static seh_scope_elem *seh_x64_makescope_elem (seh_context *, const char *, const char *, const char *, const char *);
 
 /* Local data.  */
-static asymbol **symtab;
-static int symptr;
-static arelent *reltab = 0;
-static int relcount = 0, relsize = 0;
-
-static seh_context *seh_ctx_root = NULL;
-static seh_context *seh_ctx = NULL;
 static seh_context *seh_ctx_cur = NULL;
 
-/* Write xdata for arm, sh3, sh4, and ppc.  */
+static segT xdata_seg;
+static segT pdata_seg;
+static int  xdata_subseg;
+
+static void write_function_xdata (seh_context *);
+static void write_function_pdata (seh_context *);
 
+\f
 static void
-seh_write_text_eh_data (const char *hnd, const char *hnd_data)
+switch_xdata (int subseg)
 {
-  if (!hnd || *hnd==0)
-    return;
-  if (hnd[0] == '@')
-    seh_emit_long ("0");
-  else
-    seh_emit_long (hnd);
-  if (!hnd_data || hnd_data[0] == '@')
-    seh_emit_long ("0");
-  else
-    seh_emit_long (hnd_data);
+  if (xdata_seg == NULL)
+    {
+      xdata_seg = subseg_new (".xdata", 0);
+      bfd_set_section_flags (stdoutput, xdata_seg,
+                            ((SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA)
+                             & bfd_applicable_section_flags (stdoutput)));
+    }
+  subseg_set (xdata_seg, subseg);
 }
 
-/* Generate initial pdata for x64 and mips.  */
 static void
-make_function_entry_pdata (seh_context *c)
+switch_pdata (void)
 {
-  segT sec = NULL;
-  segT current_seg = now_seg;
-  subsegT current_subseg = now_subseg;
+  if (pdata_seg == NULL)
+    {
+      pdata_seg = subseg_new (".pdata", 0);
+      bfd_set_section_flags (stdoutput, pdata_seg,
+                            ((SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA)
+                             & bfd_applicable_section_flags (stdoutput)));
+    }
+  else
+    subseg_set (pdata_seg, 0);
+}
+\f
+/* Parsing routines.  */
 
-  sec = seh_make_section ();
-  switch (seh_get_target_kind ())
+/* Return the style of SEH unwind info to generate.  */
+
+static seh_kind
+seh_get_target_kind (void)
+{
+  if (!stdoutput)
+    return seh_kind_unknown;
+  switch (bfd_get_arch (stdoutput))
     {
-    case seh_kind_x64:
-      subseg_set (sec, 0);
-      seh_emit_rva (c->func_name);
-      seh_emit_rva (c->end_symbol);
-      seh_emit_rva (c->xdata_first);
-      break;
-    case seh_kind_mips:
-      subseg_set (sec, 0);
-      seh_emit_long (c->func_name);
-      seh_emit_long (c->end_symbol);
-      if (c->handler_name == NULL)
-       seh_emit_long ("0");
-      else if (c->handler_name[0] == '@')
+    case bfd_arch_arm:
+    case bfd_arch_powerpc:
+    case bfd_arch_sh:
+      return seh_kind_arm;
+    case bfd_arch_i386:
+      switch (bfd_get_mach (stdoutput))
        {
-         if (strcasecmp (c->handler_name, "@1") == 0)
-           seh_emit_long ("1");
-         else
-           seh_emit_long ("0");
+       case bfd_mach_x86_64:
+       case bfd_mach_x86_64_intel_syntax:
+         return seh_kind_x64;
+       default:
+         break;
        }
-      else
-       seh_emit_long (c->handler_name);
-      if (c->handler_data_name == NULL || c->handler_data_name[0] == '@')
-       seh_emit_long ("0");
-      else
-       seh_emit_long (c->handler_data_name);
-      seh_emit_long (c->endprologue_symbol ? c->endprologue_symbol : c->func_name);
-      break;
+      /* FALL THROUGH.  */
+    case bfd_arch_mips:
+      return seh_kind_mips;
+    case bfd_arch_ia64:
+      /* Should return seh_kind_x64.  But not implemented yet.  */
+      return seh_kind_unknown;
     default:
       break;
     }
-  subseg_set (current_seg, current_subseg);
+  return seh_kind_unknown;
 }
 
-/* Create and write xdata section.  */
-static void
-seh_x64_write_xdata (void)
+/* Verify that we're in the context of a seh_proc.  */
+
+static int
+verify_context (const char *directive)
 {
-  seh_context *h;
-  size_t xdata_size = 0, count_syms = 0;
-  size_t xdata_offs = 0;
-  unsigned char *data;
-  segT seg_xdata;
-  bfd *abfd = stdoutput;
-
-  h = seh_ctx_root;
-  if (!h || h->done)
-    return;
-  while (h != NULL)
+  if (seh_ctx_cur == NULL)
     {
-      h->xdata_offset = xdata_size;
-      xdata_size += seh_getsize_unwind_data (h);
-      count_syms += h->count_syms;
-      h = h->next;
+      as_bad (_("%s used outside of .seh_proc block"), directive);
+      ignore_rest_of_line ();
+      return 0;
     }
+  return 1;
+}
 
-  if (xdata_size == 0)
-    return;
+/* Similar, except we also verify the appropriate target.  */
 
-  seh_symbol_init (abfd, count_syms);
-  data = xmalloc (xdata_size);
-  seg_xdata = quick_section (abfd, ".xdata", SEC_HAS_CONTENTS, 3);
-  seg_xdata->contents = data;
-  memset (data, 0, xdata_size);
-  bfd_set_section_size (abfd, seg_xdata, xdata_size);
-  h = seh_ctx_root;
-  while (h != NULL)
+static int
+verify_context_and_target (const char *directive, seh_kind target)
+{
+  if (seh_get_target_kind () != target)
     {
-      xdata_offs = h->xdata_offset;
-      h->section = seg_xdata;
-      h->abfd = abfd;
-      if (h->done == 0)
-       {
-         h->done = 1;
-         seh_create_unwind_data (h, data, xdata_offs);
-         h->done = 1;
-       }
-      h = h->next;
+      as_warn (_("%s ignored for this target"), directive);
+      ignore_rest_of_line ();
+      return 0;
+    }
+  return verify_context (directive);
+}
+
+/* Skip whitespace and a comma.  Error if the comma is not seen.  */
+
+static int
+skip_whitespace_and_comma (int required)
+{
+  SKIP_WHITESPACE ();
+  if (*input_line_pointer == ',')
+    {
+      input_line_pointer++;
+      SKIP_WHITESPACE ();
+      return 1;
     }
-  save_relocs (seg_xdata);
-  bfd_set_symtab (abfd, symtab, symptr);
-  bfd_set_section_contents (abfd, seg_xdata, data, 0, xdata_size);
+  else if (required)
+    {
+      as_bad (_("missing separator"));
+      ignore_rest_of_line ();
+    }
+  else
+    demand_empty_rest_of_line ();
+  return 0;
 }
 
-/* Create pdata section data for arm.  */
+/* Mark current context to use 32-bit instruction (arm).  */
 
 static void
-seh_arm_create_pdata (seh_context *c, unsigned char *data, size_t pdata_offs)
+obj_coff_seh_32 (int what)
 {
-  int idx;
-  unsigned int val;
-  valueT func_len = 0;
-  valueT prolog_len = 0;
-  valueT start_len = 0;
+  if (!verify_context_and_target ((what ? ".seh_32" : ".seh_no32"),
+                                 seh_kind_arm))
+    return;
 
-  func_len = resolve_symbol_value (c->end_addr);
-  start_len = resolve_symbol_value (c->start_addr);
-  if (c->endprologue_addr)
-    prolog_len = resolve_symbol_value (c->endprologue_addr);
-  else
-    prolog_len = start_len;
-  func_len -= start_len;
-  prolog_len -= start_len;
-  if (!c || !data)
+  seh_ctx_cur->use_instruction_32 = (what ? 1 : 0);
+  demand_empty_rest_of_line ();
+}
+
+/* Set for current context the handler and optional data (arm).  */
+
+static void
+obj_coff_seh_eh (int what ATTRIBUTE_UNUSED)
+{
+  if (!verify_context_and_target (".seh_eh", seh_kind_arm))
     return;
-  /* $$$$ */
-  idx = seh_symbol (c->abfd, c->start_symbol, "", "", UNDSEC, BSF_GLOBAL, 0);
-  seh_reloc (c->abfd, pdata_offs, BFD_RELOC_32, idx);
-  val = (unsigned int) func_len;
-  val <<= 8;
-  val |= ((unsigned int) prolog_len & 0xffU);
-  if (c->use_instruction_32)
-    val |= 0x40000000U;
-  if (c->handler_written)
-    val |= 0x80000000U;
-  bfd_put_32 (c->abfd, (bfd_vma) val, data + pdata_offs + 4);
+
+  /* Write block to .text if exception handler is set.  */
+  seh_ctx_cur->handler_written = 1;
+  emit_expr (&seh_ctx_cur->handler, 4);
+  emit_expr (&seh_ctx_cur->handler_data, 4);
+
+  demand_empty_rest_of_line ();
 }
 
-/* Write pdata section for arm.  */
+/* Set for current context the default handler (x64).  */
+
 static void
-seh_arm_write_pdata (void)
+obj_coff_seh_handler (int what ATTRIBUTE_UNUSED)
 {
-  seh_context *h;
-  size_t pdata_size = 0, count_syms = 0;
-  size_t pdata_offs = 0;
-  unsigned char *data;
-  segT seg_pdata;
-  bfd *abfd = stdoutput;
-
-  h = seh_ctx_root;
-  if (h == NULL || h->done)
+  char *symbol_name;
+  char name_end;
+
+  if (!verify_context (".seh_handler"))
     return;
-  while (h != NULL)
+
+  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
+    {
+      as_bad (_(".seh_handler requires a handler"));
+      demand_empty_rest_of_line ();
+      return;
+    }
+
+  SKIP_WHITESPACE ();
+
+  if (*input_line_pointer == '@')
     {
-      h->xdata_offset = pdata_size;
-      pdata_size += 8;
-      count_syms += 1;
-      h = h->next;
+      symbol_name = input_line_pointer;
+      name_end = get_symbol_end ();
+
+      seh_ctx_cur->handler.X_op = O_constant;
+      seh_ctx_cur->handler.X_add_number = 0;
+
+      if (strcasecmp (symbol_name, "@0") == 0
+         || strcasecmp (symbol_name, "@null") == 0)
+       ;
+      else if (strcasecmp (symbol_name, "@1") == 0)
+       seh_ctx_cur->handler.X_add_number = 1;
+      else
+       as_bad (_("unknown constant value '%s' for handler"), symbol_name);
+
+      *input_line_pointer = name_end;
     }
+  else
+    expression (&seh_ctx_cur->handler);
 
-  if (pdata_size == 0)
+  seh_ctx_cur->handler_data.X_op = O_constant;
+  seh_ctx_cur->handler_data.X_add_number = 0;
+  seh_ctx_cur->handler_flags = 0;
+
+  if (!skip_whitespace_and_comma (0))
     return;
 
-  seh_symbol_init (abfd, count_syms);
-  data = xmalloc (pdata_size);
-  seg_pdata = quick_section (abfd, ".pdata", SEC_HAS_CONTENTS, 3);
-  seg_pdata->contents = data;
-  memset (data, 0, pdata_size);
-  bfd_set_section_size (abfd, seg_pdata, pdata_size);
-  h = seh_ctx_root;
-  while (h != NULL)
+  if (seh_get_target_kind () == seh_kind_x64)
     {
-      pdata_offs = h->xdata_offset;
-      h->section = seg_pdata;
-      h->abfd = abfd;
-      if (h->done != 0)
+      do
        {
-         seh_arm_create_pdata (h, data, pdata_offs);
-         h->done = 1;
+         symbol_name = input_line_pointer;
+         name_end = get_symbol_end ();
+
+         if (strcasecmp (symbol_name, "@unwind") == 0)
+           seh_ctx_cur->handler_flags |= UNW_FLAG_UHANDLER;
+         else if (strcasecmp (symbol_name, "@except") == 0)
+           seh_ctx_cur->handler_flags |= UNW_FLAG_EHANDLER;
+         else
+           as_bad (_(".seh_handler constant '%s' unknown"), symbol_name);
+
+         *input_line_pointer = name_end;
        }
-      h = h->next;
+      while (skip_whitespace_and_comma (0));
+    }
+  else
+    {
+      expression (&seh_ctx_cur->handler_data);
+      demand_empty_rest_of_line ();
+
+      if (seh_ctx_cur->handler_written)
+       as_warn (_(".seh_handler after .seh_eh is ignored"));
+    }
+}
+
+/* Switch to subsection for handler data for exception region (x64).  */
+
+static void
+obj_coff_seh_handlerdata (int what ATTRIBUTE_UNUSED)
+{
+  if (!verify_context_and_target (".seh_handlerdata", seh_kind_x64))
+    return;
+  demand_empty_rest_of_line ();
+
+  switch_xdata (seh_ctx_cur->subsection + 1);
+}
+
+/* Mark end of current context.  */
+
+static void
+do_seh_endproc (void)
+{
+  seh_ctx_cur->end_addr = symbol_temp_new_now ();
+
+  write_function_xdata (seh_ctx_cur);
+  write_function_pdata (seh_ctx_cur);
+  seh_ctx_cur = NULL;
+}
+
+static void
+obj_coff_seh_endproc (int what ATTRIBUTE_UNUSED)
+{
+  demand_empty_rest_of_line ();
+  if (seh_ctx_cur == NULL)
+    {
+      as_bad (_(".seh_endproc used without .seh_proc"));
+      return;
+    }
+
+  do_seh_endproc ();
+}
+
+/* Mark begin of new context.  */
+
+static void
+obj_coff_seh_proc (int what ATTRIBUTE_UNUSED)
+{
+  char *symbol_name;
+  char name_end;
+
+  if (seh_ctx_cur != NULL)
+    {
+      as_bad (_("previous SEH entry not closed (missing .seh_endproc)"));
+      do_seh_endproc ();
+    }
+
+  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
+    {
+      as_bad (_(".seh_proc requires function label name"));
+      demand_empty_rest_of_line ();
+      return;
+    }
+
+  seh_ctx_cur = XCNEW (seh_context);
+
+  if (seh_get_target_kind () == seh_kind_x64)
+    {
+      seh_ctx_cur->subsection = xdata_subseg;
+      xdata_subseg += 2;
     }
-  save_relocs (seg_pdata);
-  bfd_set_symtab (abfd, symtab, symptr);
-  bfd_set_section_contents (abfd, seg_pdata, data, 0, pdata_size);
+
+  SKIP_WHITESPACE ();
+
+  symbol_name = input_line_pointer;
+  name_end = get_symbol_end ();
+  seh_ctx_cur->func_name = xstrdup (symbol_name);
+  *input_line_pointer = name_end;
+
+  demand_empty_rest_of_line ();
+
+  seh_ctx_cur->start_addr = symbol_temp_new_now ();
 }
 
-/* Do object finalization to generate and write pdata/xdata
-   for arm and x64 target.  */
+/* Mark end of prologue for current context.  */
+
+static void
+obj_coff_seh_endprologue (int what ATTRIBUTE_UNUSED)
+{
+  if (!verify_context (".seh_endprologue"))
+    return;
+  demand_empty_rest_of_line ();
+
+  if (seh_ctx_cur->endprologue_addr != NULL)
+    as_warn (_("duplicate .seh_endprologue in .seh_proc block"));
+  else
+    seh_ctx_cur->endprologue_addr = symbol_temp_new_now ();
+}
+
+/* End-of-file hook.  */
+
 void
 obj_coff_seh_do_final (void)
 {
-  switch (seh_get_target_kind ())
+  if (seh_ctx_cur != NULL)
     {
-    case seh_kind_mips:
-    default:
-      break;
-    case seh_kind_arm:
-      seh_arm_write_pdata ();
-      break;
-    case seh_kind_x64:
-      seh_x64_write_xdata ();
-      break;
+      as_bad (_("open SEH entry at end of file (missing .cfi_endproc)"));
+      do_seh_endproc ();
     }
 }
 
-/* Enter a prologue element into current context.  */
+/* Enter a prologue element into current context (x64).  */
+
 static void
-seh_x64_make_prologue_element (int kind, int reg, bfd_vma off)
+seh_x64_make_prologue_element (int code, int info, offsetT off)
 {
   seh_prologue_element *n;
 
@@ -283,1211 +367,513 @@ seh_x64_make_prologue_element (int kind, int reg, bfd_vma off)
     return;
   if (seh_ctx_cur->elems_count == seh_ctx_cur->elems_max)
     {
-      seh_ctx_cur->elems = (seh_prologue_element *)
-       xrealloc (seh_ctx_cur->elems,
-                 ((seh_ctx_cur->elems_max + 8) * sizeof (seh_prologue_element)));
       seh_ctx_cur->elems_max += 8;
+      seh_ctx_cur->elems = XRESIZEVEC (seh_prologue_element,
+                                      seh_ctx_cur->elems,
+                                      seh_ctx_cur->elems_max);
     }
-  n = &seh_ctx_cur->elems[seh_ctx_cur->elems_count];
-  memset (n, 0, sizeof (seh_prologue_element));
-  n->kind = kind;
-  n->reg = reg;
-  n->offset = off;
-  n->pc_symbol = make_seh_text_label (seh_ctx_cur, &(n->pc_addr));
-  seh_ctx_cur->elems_count += 1;
+
+  n = &seh_ctx_cur->elems[seh_ctx_cur->elems_count++];
+  n->code = code;
+  n->info = info;
+  n->off = off;
+  n->pc_addr = symbol_temp_new_now ();
 }
 
-/* Helper to read a register name from input stream.  */
+/* Helper to read a register name from input stream (x64).  */
+
 static int
-seh_x64_read_reg (const char *tok, int kind, int *regno)
+seh_x64_read_reg (const char *directive, int kind)
 {
-  static const char *frame_regs[16] =
-    { "cfa", "rcx", "rdx", "rbx", "rsp", "rbp","rsi","rdi",
-      "r8","r9","r10","r11","r12","r13","r14","r15" };
-  static const char *int_regs[16] =
+  static const char * const int_regs[16] =
     { "rax", "rcx", "rdx", "rbx", "rsp", "rbp","rsi","rdi",
       "r8","r9","r10","r11","r12","r13","r14","r15" };
-  static const char *xmm_regs[16] =
+  static const char * const xmm_regs[16] =
     { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
       "xmm8", "xmm9", "xmm10","xmm11","xmm12","xmm13","xmm14","xmm15" };
-  static const char *mm_regs[16] =
-    { "xmm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
-      "xmm8", "mm9", "mm10","mm11","mm12","mm13","mm14","mm15" };
-  const char **p = NULL;
+
+  const char * const *regs = NULL;
   char name_end;
   char *symbol_name = NULL;
   int i;
 
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
-    input_line_pointer++;
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
-    input_line_pointer++;
   switch (kind)
     {
     case 0:
-      p = frame_regs;
-      break;
     case 1:
-      p = int_regs;
+      regs = int_regs;
       break;
     case 2:
-      p = mm_regs;
-      break;
-    case 3:
-      p = xmm_regs;
+      regs = xmm_regs;
       break;
     default:
       abort ();
     }
 
-  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
-    return 0;
-
+  SKIP_WHITESPACE ();
   if (*input_line_pointer == '%')
     ++input_line_pointer;
   symbol_name = input_line_pointer;
   name_end = get_symbol_end ();
 
   for (i = 0; i < 16; i++)
-    if (! strcasecmp (p[i], symbol_name))
+    if (! strcasecmp (regs[i], symbol_name))
       break;
 
-  if (i == 16)
-    as_warn (_("In %s we found the invalid register name %s.\n"),
-            tok, symbol_name);
-
   *input_line_pointer = name_end;
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
-    input_line_pointer++;
-  if (*input_line_pointer == ',')
-    ++input_line_pointer;
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
-    input_line_pointer++;
-  *regno = i;
-  return i != 16;
-}
 
-/* Read an numeric value from input stream.  */
-static int
-seh_read_offset (const char *tok, bfd_vma *off)
-{
-  bfd_vma r, v = 0, base = 10;
-  int had_one = 0;
-  int neg = 0;
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
-    input_line_pointer++;
-  if (*input_line_pointer == '$')
-    ++input_line_pointer;
-  if (*input_line_pointer == '-')
-    {
-      neg = 1;
-      ++input_line_pointer;
-    }
-  else if (*input_line_pointer == '+')
-    {
-      ++input_line_pointer;
-    }
-  if (*input_line_pointer == '0')
-    {
-      ++input_line_pointer;
-      had_one = 1;
-      base = 8;
-      switch ((*input_line_pointer))
-       {
-       case 'x':
-       case 'X':
-         base = 16;
-         ++input_line_pointer;
-         break;
-       case 'd':
-       case 'D':
-         base = 10;
-         input_line_pointer++;
-         break;
-       case 'o':
-       case 'O':
-         base = 8;
-         input_line_pointer++;
-         break;
-       }
-    }
-  while (*input_line_pointer != 0)
+  /* Error if register not found, or EAX used as a frame pointer.  */
+  if (i == 16 || (kind == 0 && i == 0))
     {
-      if (input_line_pointer[0] >= '0' && input_line_pointer[0] <='9')
-       r = (bfd_vma) (input_line_pointer[0] - '0');
-      else if (base == 16 && input_line_pointer[0] >= 'a' && input_line_pointer[0] <='f')
-       r = (bfd_vma) ((input_line_pointer[0] - 'a') + 10);
-      else if (base == 16 && input_line_pointer[0] >= 'A' && input_line_pointer[0] <='F')
-       r = (bfd_vma) ((input_line_pointer[0] - 'A') + 10);
-      else
-       break;
-      input_line_pointer++;
-      v *= base;
-      v += r;
-      had_one = 1;
+      as_bad (_("invalid register for %s"), directive);
+      return -1;
     }
 
-  if (neg)
-    v = (bfd_vma) (v ^ ((bfd_vma) -1LL)) + (bfd_vma) 1ULL;
-  *off = v;
-  if (had_one == 0)
-    {
-      as_warn (_("In %s we expect a number.\n"),
-              tok);
-    }
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
-    input_line_pointer++;
-  if (*input_line_pointer == ',')
-    ++input_line_pointer;
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
-    input_line_pointer++;
-  return had_one != 0;
+  return i;
 }
 
-/* Mark current context to use 32-bit instruction (arm).  */
+/* Add a register push-unwind token to the current context.  */
+
 static void
-obj_coff_seh_32 (int what)
+obj_coff_seh_pushreg (int what ATTRIBUTE_UNUSED)
 {
-  if (seh_ctx_cur == NULL)
-    {
-      as_fatal (_(".seh_eh requires to be in .seh_proc/.seh_endproc block.\n"));
-      demand_empty_rest_of_line ();
-      return;
-    }
-  seh_ctx_cur->use_instruction_32 = (what ? 1 : 0);
-  if (seh_get_target_kind () == seh_kind_arm)
-    as_warn (_(".seh_%s32 is ignored for this target."), (what ? "" : "no"));
+  int reg;
+
+  if (!verify_context_and_target (".seh_pushreg", seh_kind_x64))
+    return;
+
+  reg = seh_x64_read_reg (".seh_pushreg", 1);
   demand_empty_rest_of_line ();
+
+  if (reg < 0)
+    return;
+
+  seh_x64_make_prologue_element (UWOP_PUSH_NONVOL, reg, 0);
 }
 
-/* Set for current context the handler and optional data (arm).  */
+/* Add a register frame-unwind token to the current context.  */
+
 static void
-obj_coff_seh_eh (int what ATTRIBUTE_UNUSED)
+obj_coff_seh_pushframe (int what ATTRIBUTE_UNUSED)
 {
-  if (seh_ctx_cur == NULL)
-    {
-      as_fatal (_(".seh_eh requires to be in .seh_proc/.seh_endproc block.\n"));
-      demand_empty_rest_of_line ();
-      return;
-    }
-  if (seh_get_target_kind () == seh_kind_arm)
-    {
-      seh_ctx_cur->handler_written = 1;
-      /* write block to .text if exception handler is set.  */
-      seh_write_text_eh_data (seh_ctx_cur->handler_name, seh_ctx_cur->handler_data_name);
-    }
+  if (!verify_context_and_target (".seh_pushframe", seh_kind_x64))
+    return;
   demand_empty_rest_of_line ();
-}
-
-/* Set for current context the default handler (x64).  */
-static void
-obj_coff_seh_handler (int what ATTRIBUTE_UNUSED)
-{
-  char *symbol_name;
-  char name_end;
-
-  if (seh_ctx_cur == NULL)
-    {
-      as_fatal (_(".seh_handler requires to be in .seh_proc/.seh_endproc block.\n"));
-      demand_empty_rest_of_line ();
-      return;
-    }
-  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
-    {
-      as_fatal (_(".seh_handler requires a handler lable name.\n"));
-      demand_empty_rest_of_line ();
-      return;
-    }
 
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
-    input_line_pointer++;
-  symbol_name = input_line_pointer;
-  name_end = get_symbol_end ();
-  seh_ctx->handler_name = xstrdup (symbol_name);
-  if (symbol_name[0] == '@')
-    {
-      if (strcasecmp (symbol_name, "@0") != 0 && strcasecmp (symbol_name, "@1") != 0
-         && strcasecmp (symbol_name, "@null") != 0)
-       as_warn (_("Unknown constant value ,%s' for handler."), symbol_name);
-    }
-  *input_line_pointer = name_end;
-  seh_ctx->handler_data_name = NULL;
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
-    input_line_pointer++;
-  symbol_name = input_line_pointer;
-  if (*input_line_pointer != '\n' && *input_line_pointer != 0)
-    {
-      name_end = get_symbol_end ();
-      seh_ctx->handler_data_name = xstrdup (symbol_name);
-      if (symbol_name[0] == '@')
-       {
-         if (seh_get_target_kind () != seh_kind_x64)
-           as_fatal (_("For this target .seh_handler doesn't support constant user-data."));
-         else if (strcasecmp (symbol_name, "@unwind") != 0 &&
-                  strcasecmp (symbol_name, "@except") != 0)
-           as_warn (_("For .seh_handler the constant ,%s' is ignored."), symbol_name);
-       }
-      *input_line_pointer = name_end;
-    }
-  if (seh_ctx_cur->handler_written)
-    as_warn (_(".seh_handler is ignored as .seh_eh was seen before."));
-  demand_empty_rest_of_line ();
+  seh_x64_make_prologue_element (UWOP_PUSH_MACHFRAME, 0, 0);
 }
 
-/* Add a scope-element for exception region (x64).  */
+/* Add a register save-unwind token to current context.  */
+
 static void
-obj_coff_seh_scope (int what ATTRIBUTE_UNUSED)
+obj_coff_seh_save (int what)
 {
-  char *symbol_name,*beg = NULL,*end = NULL, *handl = NULL, *jmp = NULL;
-  char name_end;
+  const char *directive = (what == 1 ? ".seh_savereg" : ".seh_savexmm");
+  int code, reg, scale;
+  offsetT off;
 
-  if (seh_ctx_cur == NULL)
-    {
-      as_fatal (_(".seh_scope requires to be in .seh_proc/.seh_endproc block.\n"));
-      demand_empty_rest_of_line ();
-      return;
-    }
+  if (!verify_context_and_target (directive, seh_kind_x64))
+    return;
 
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
-    input_line_pointer++;
-  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
-    {
-      as_fatal (_(".seh_scope requires four symbol names.\n"));
-      demand_empty_rest_of_line ();
-      return;
-    }
-  symbol_name = input_line_pointer;
-  name_end = get_symbol_end ();
-  beg = xstrdup (symbol_name);
-  *input_line_pointer = name_end;
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
-    input_line_pointer++;
-  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
-    {
-      as_fatal (_(".seh_scope requires three more symbol names.\n"));
-      demand_empty_rest_of_line ();
-      return;
-    }
-  symbol_name = input_line_pointer;
-  name_end = get_symbol_end ();
-  end = xstrdup (symbol_name);
-  *input_line_pointer = name_end;
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
-    input_line_pointer++;
-  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
-    {
-      as_fatal (_(".seh_scope requires two more symbol names.\n"));
-      demand_empty_rest_of_line ();
-      return;
-    }
-  symbol_name = input_line_pointer;
-  name_end = get_symbol_end ();
-  handl = xstrdup (symbol_name);
-  *input_line_pointer = name_end;
-  if (*handl == '@')
-    {
-      if (strcasecmp (handl, "@0") != 0 && strcasecmp (handl, "@1") != 0
-         && strcasecmp (handl, "@null") != 0)
-       as_warn (_("Unknown constant for handler ,%s'."), handl);
-    }
+  reg = seh_x64_read_reg (directive, what);
 
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
-    input_line_pointer++;
-  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
-    {
-      as_fatal (_(".seh_scope requires one more symbol names.\n"));
-      demand_empty_rest_of_line ();
-      return;
-    }
-  symbol_name = input_line_pointer;
-  name_end = get_symbol_end ();
-  jmp = xstrdup (symbol_name);
-  *input_line_pointer = name_end;
-  if (*jmp == '@')
-    {
-      if (strcasecmp (jmp, "@0") != 0 && strcasecmp (handl, "@null") != 0)
-       as_warn (_("Unknown constant for jump ,%s'."), jmp);
-    }
+  if (!skip_whitespace_and_comma (1))
+    return;
 
-  if (seh_get_target_kind () != seh_kind_x64)
-    as_warn (_(".seh_scope is ignored for this target."));
-  else
-    seh_x64_makescope_elem (seh_ctx_cur, beg, end, handl, jmp);
-  if (beg)
-    free (beg);
-  if (end)
-    free (end);
-  if (handl)
-    free (handl);
-  if (jmp)
-    free (jmp);
+  off = get_absolute_expression ();
   demand_empty_rest_of_line ();
-}
-
-/* Mark begin of new context.  */
-static void
-obj_coff_seh_proc (int what ATTRIBUTE_UNUSED)
-{
-  char *symbol_name;
-  char name_end;
 
-  if (seh_ctx_cur != NULL)
-    {
-      as_warn (_(".seh_proc has to be closed by .seh_endprog\n"));
-      obj_coff_seh_endproc (0);
-    }
-
-  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
+  if (reg < 0)
+    return;
+  if (off < 0)
     {
-      as_fatal (_(".seh_proc requires function lable name.\n"));
-      demand_empty_rest_of_line ();
+      as_bad (_("%s offset is negative"), directive);
       return;
     }
 
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
-    input_line_pointer++;
-  symbol_name = input_line_pointer;
-  name_end = get_symbol_end ();
+  scale = (what == 1 ? 8 : 16);
 
-  if (seh_ctx == NULL)
-    seh_ctx_root = seh_ctx = (seh_context *) xmalloc (sizeof (seh_context));
-  else
+  if ((off & (scale - 1)) == 0 && off <= 0xffff * scale)
     {
-      seh_ctx->next = (seh_context *) xmalloc (sizeof (seh_context));
-      seh_ctx = seh_ctx->next;
+      code = (what == 1 ? UWOP_SAVE_NONVOL : UWOP_SAVE_XMM128);
+      off /= scale;
     }
-  seh_ctx_cur = seh_ctx;
-  memset (seh_ctx, 0, sizeof (seh_context));
-
-  seh_ctx->func_name = xstrdup (symbol_name);
-  *input_line_pointer = name_end;
-  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
-    input_line_pointer++;
-  seh_ctx->start_symbol = make_seh_text_label (seh_ctx_cur, &(seh_ctx_cur->start_addr));
-  demand_empty_rest_of_line ();
-}
-
-/* Mark end of current context.  */
-static void
-obj_coff_seh_endproc  (int what ATTRIBUTE_UNUSED)
-{
-  if (seh_ctx_cur == NULL)
+  else if (off < 0xffffffff)
+    code = (what == 1 ? UWOP_SAVE_NONVOL_FAR : UWOP_SAVE_XMM128_FAR);
+  else
     {
-      as_warn (_(".seh_endprog without prior .seh_proc (ignored)\n"));
-      demand_empty_rest_of_line ();
+      as_bad (_("%s offset out of range"), directive);
       return;
     }
-  seh_ctx->end_symbol = make_seh_text_label (seh_ctx, &(seh_ctx->end_addr));
-  seh_ctx->xdata_first = seh_make_xlbl_name (seh_ctx);
-  make_function_entry_pdata (seh_ctx);
-  seh_ctx_cur = NULL;
-  demand_empty_rest_of_line ();
+
+  seh_x64_make_prologue_element (code, reg, off);
 }
 
-/* Add a push-unwind-information to current context.
-   what:0 push volatile reg, 1: push machine frame, 2: set fp register.  */
+/* Add a stack-allocation token to current context.  */
+
 static void
-obj_coff_seh_push  (int what)
+obj_coff_seh_stackalloc (int what ATTRIBUTE_UNUSED)
 {
-  int reg = 0;
-  int kind = -1;
-  bfd_vma off = 0;
+  offsetT off;
+  int code, info;
 
-  if (seh_ctx_cur == NULL)
-    {
-      as_warn (_(".seh_push used outside of .seh_proc block.\n"));
-      demand_empty_rest_of_line ();
-      return;
-    }
-  /* What 0:reg, 1:pushframe.  */
-  switch (what)
-    {
-    case 0:
-      if (seh_x64_read_reg (".seh_pushreg", 1, &reg))
-       kind = UWOP_PUSH_NONVOL;
-      else
-       as_warn (_(".seh_pushreg expects register argument."));
-      break;
-    case 1:
-      kind = UWOP_PUSH_MACHFRAME;
-      break;
-    default:
-      abort ();
-    }
+  if (!verify_context_and_target (".seh_stackalloc", seh_kind_x64))
+    return;
 
-  if (seh_get_target_kind () != seh_kind_x64)
-    as_warn (_(".seh_save... is ignored for this target.\n"));
-  else if (kind != -1)
-    seh_x64_make_prologue_element (kind, reg, off);
+  off = get_absolute_expression ();
   demand_empty_rest_of_line ();
-}
 
-/* Add a register save-unwind-information to current context.  */
-static void
-obj_coff_seh_save  (int what)
-{
-  int reg;
-  bfd_vma off;
-  int kind;
-  int ok = 1;
-
-  /* what 0:reg, 1:mm, 2:xmm.  */
-  switch (what)
-    {
-    case 0:
-      ok &= seh_x64_read_reg (".seh_savereg", 1, &reg);
-      kind = UWOP_SAVE_NONVOL;
-      break;
-    case 1:
-      ok &= seh_x64_read_reg (".seh_savemm", 2, &reg);
-      kind = UWOP_SAVE_XMM;
-      break;
-    case 2:
-      ok &= seh_x64_read_reg (".seh_savexmm", 3, &reg);
-      kind = UWOP_SAVE_XMM128;
-      break;
-    default:
-      abort ();
-    }
-  ok &= seh_read_offset (".seh_save", &off);
-  if (seh_ctx_cur == NULL)
+  if (off == 0)
+    return;
+  if (off < 0)
     {
-      as_warn (_(".seh_save used outside of .seh_proc block.\n"));
-      demand_empty_rest_of_line ();
+      as_bad (_(".seh_stackalloc offset is negative"));
       return;
     }
-  if (seh_get_target_kind () != seh_kind_x64)
-    as_warn (_(".seh_save... is ignored for this target.\n"));
-  else
-    seh_x64_make_prologue_element (kind, reg, off);
-  demand_empty_rest_of_line ();
-}
 
-/* Mark end of prologue for current context.  */
-static void
-obj_coff_seh_endprologue (int what ATTRIBUTE_UNUSED)
-{
-  if (seh_ctx_cur == NULL)
-    {
-      as_warn (_(".seh_endprologue used outside of .seh_proc block.\n"));
-      demand_empty_rest_of_line ();
-      return;
-    }
-  if (seh_ctx_cur->endprologue_symbol != NULL)
-    as_warn (_(".seh_endprologue used more then once in .seh_proc block.\n"));
+  if ((off & 7) == 0 && off <= 128)
+    code = UWOP_ALLOC_SMALL, info = (off - 8) >> 3, off = 0;
+  else if ((off & 7) == 0 && off <= 0xffff * 8)
+    code = UWOP_ALLOC_LARGE, info = 0, off >>= 3;
+  else if (off <= 0xffffffff)
+    code = UWOP_ALLOC_LARGE, info = 1;
   else
-    seh_ctx_cur->endprologue_symbol = make_seh_text_label (seh_ctx_cur, &seh_ctx_cur->endprologue_addr);
-}
-
-/* Add a stack-allocation-unwind-information for current context.  */
-static void
-obj_coff_seh_stack_alloc (int what ATTRIBUTE_UNUSED)
-{
-  bfd_vma size;
-
-  if (seh_ctx_cur == NULL)
     {
-      as_warn (_(".seh_stackalloc used outside of .seh_proc block.\n"));
-      demand_empty_rest_of_line ();
+      as_bad (_(".seh_stackalloc offset out of range"));
       return;
     }
-  if (seh_read_offset (".seh_stackalloc", &size))
-    {
-      if (seh_get_target_kind () != seh_kind_x64)
-       as_warn (_(".seh_stackalloc is ignored for this target.\n"));
-      else
-       seh_x64_make_prologue_element (UWOP_ALLOC_LARGE, 0, size);
-    }
+
+  seh_x64_make_prologue_element (code, info, off);
 }
 
-/* Add frame-pointer-unwind-information to current context.  */
+/* Add a frame-pointer token to current context.  */
+
 static void
 obj_coff_seh_setframe (int what ATTRIBUTE_UNUSED)
 {
-  int kind = -1;
+  offsetT off;
   int reg;
-  int ok = 1;
-  bfd_vma off;
 
-  ok &= seh_x64_read_reg (".seh_setframe", 0, &reg);
-  ok &= seh_read_offset (".seh_setframe", &off);
-  if (seh_ctx_cur == NULL)
-    {
-      as_warn (_(".seh_setframe used outside of .seh_proc block.\n"));
-      demand_empty_rest_of_line ();
-      return;
-    }
-  if (ok)
+  if (!verify_context_and_target (".seh_setframe", seh_kind_x64))
+    return;
+
+  reg = seh_x64_read_reg (".seh_setframe", 0);
+
+  if (!skip_whitespace_and_comma (1))
+    return;
+
+  off = get_absolute_expression ();
+  demand_empty_rest_of_line ();
+
+  if (reg < 0)
+    return;
+  if (off < 0)
+    as_bad (_(".seh_setframe offset is negative"));
+  else if (off > 240)
+    as_bad (_(".seh_setframe offset out of range"));
+  else if (off & 15)
+    as_bad (_(".seh_setframe offset not a multiple of 16"));
+  else if (seh_ctx_cur->framereg != 0)
+    as_bad (_("duplicate .seh_setframe in current .seh_proc"));
+  else
     {
       seh_ctx_cur->framereg = reg;
       seh_ctx_cur->frameoff = off;
-      kind = UWOP_SET_FPREG;
+      seh_x64_make_prologue_element (UWOP_SET_FPREG, 0, 0);
     }
-  if (seh_get_target_kind () != seh_kind_x64)
-    as_warn (_(".seh_setframe is ignored for this target.\n"));
-  else if (kind != -1)
-    seh_x64_make_prologue_element (kind, reg, off);
-  demand_empty_rest_of_line ();
 }
+\f
+/* Data writing routines.  */
 
-/* Misc function helpers.  */
-static void
-seh_reloc (bfd *abfd, bfd_size_type address, int which_howto, int symidx)
-{
-  if (relcount >= relsize - 1)
-    {
-      relsize += 10;
-      if (reltab)
-       reltab = xrealloc (reltab, relsize * sizeof (arelent));
-      else
-       reltab = xmalloc (relsize * sizeof (arelent));
-    }
-  reltab[relcount].address = address;
-  reltab[relcount].addend = 0;
-  reltab[relcount].howto = bfd_reloc_type_lookup (abfd, which_howto);
-  reltab[relcount].sym_ptr_ptr = symtab + symidx;
-  relcount++;
-}
+/* Output raw integers in 1, 2, or 4 bytes.  */
 
-static void
-save_relocs (asection *sec)
+static inline void
+out_one (int byte)
 {
-  int i;
-
-  sec->relocation = reltab;
-  sec->reloc_count = relcount;
-  sec->orelocation = xmalloc ((relcount + 1) * sizeof (arelent *));
-  for (i = 0; i < relcount; i++)
-    sec->orelocation[i] = sec->relocation + i;
-  sec->orelocation[relcount] = 0;
-  sec->flags |= SEC_RELOC;
-  reltab = 0;
-  relcount = relsize = 0;
+  FRAG_APPEND_1_CHAR (byte);
 }
 
-static void
-seh_symbol_init (bfd *abfd, unsigned int added)
+static inline void
+out_two (int data)
 {
-  unsigned int oldcount;
-
-  oldcount = bfd_get_symcount (abfd);
-  symptr = oldcount;
-  symtab = xmalloc ((oldcount + added + 6) * sizeof (asymbol *));
-  if (oldcount > 0)
-    memcpy (symtab, bfd_get_outsymbols (abfd), sizeof (asymbol *) * oldcount);
+  md_number_to_chars (frag_more (2), data, 2);
 }
 
-static int
-seh_symbol (bfd *abfd, const char *n1, const char *n2, const char *n3,
-            asection *sec, int flags, int addr)
+static inline void
+out_four (int data)
 {
-  asymbol *sym;
-  char *name = xmalloc (strlen (n1) + strlen (n2) + strlen (n3) + 1);
-  int ret = symptr;
-
-  strcpy (name, n1);
-  strcat (name, n2);
-  strcat (name, n3);
-  sym = bfd_make_empty_symbol (abfd);
-  sym->name = name;
-  sym->section = sec;
-  sym->flags = flags;
-  sym->value = addr;
-  symtab[symptr++] = sym;
-  return ret;
+  md_number_to_chars (frag_more (4), data, 4);
 }
 
-static asection *
-quick_section (bfd *abfd, const char *name, int flags, int align)
+/* Write out prologue data for x64.  */
+
+static void
+seh_x64_write_prologue_data (const seh_context *c)
 {
-  asection *sec;
-  asymbol *sym;
+  int i;
 
-  sec = seh_make_section2 (name, flags);
-  bfd_set_section_alignment (abfd, sec, align);
-  /* Remember to undo this before trying to link internally!  */
+  /* We have to store in reverse order.  */
+  for (i = c->elems_count - 1; i >= 0; --i)
+    {
+      const seh_prologue_element *e = c->elems + i;
+      expressionS exp;
 
-  sym = bfd_make_empty_symbol (abfd);
-  symtab[symptr++] = sym;
-  sym->name = sec->name;
-  sym->section = sec;
-  sym->flags = BSF_LOCAL;
-  sym->value = 0;
+      /* First comes byte offset in code.  */
+      exp.X_op = O_subtract;
+      exp.X_add_symbol = e->pc_addr;
+      exp.X_op_symbol = c->start_addr;
+      exp.X_add_number = 0;
+      emit_expr (&exp, 1);
 
-  return sec;
-}
+      /* Second comes code+info packed into a byte.  */
+      out_one ((e->info << 4) | e->code);
 
-static seh_kind
-seh_get_target_kind (void)
-{
-  if (!stdoutput)
-    return seh_kind_unknown;
-  switch (bfd_get_arch (stdoutput))
-    {
-    case bfd_arch_arm:
-    case bfd_arch_powerpc:
-    case bfd_arch_sh:
-      return seh_kind_arm;
-    case bfd_arch_i386:
-      switch (bfd_get_mach (stdoutput))
+      switch (e->code)
        {
-       case bfd_mach_x86_64:
-       case bfd_mach_x86_64_intel_syntax:
-         return seh_kind_x64;
-       default:
+       case UWOP_PUSH_NONVOL:
+       case UWOP_ALLOC_SMALL:
+       case UWOP_SET_FPREG:
+       case UWOP_PUSH_MACHFRAME:
+         /* These have no extra data.  */
          break;
+
+       case UWOP_ALLOC_LARGE:
+         if (e->info)
+           {
+       case UWOP_SAVE_NONVOL_FAR:
+       case UWOP_SAVE_XMM128_FAR:
+             /* An unscaled 4 byte offset.  */
+             out_four (e->off);
+             break;
+           }
+         /* FALLTHRU */
+
+       case UWOP_SAVE_NONVOL:
+       case UWOP_SAVE_XMM128:
+         /* A scaled 2 byte offset.  */
+         out_two (e->off);
+         break;
+
+       default:
+         abort ();
        }
-      /* FALL THROUGH.  */
-    case bfd_arch_mips:
-      return seh_kind_mips;
-    case bfd_arch_ia64:
-      /* Should return seh_kind_x64.  But not implemented yet.  */
-      return seh_kind_unknown;
-    default:
-      break;
     }
-  return seh_kind_unknown;
 }
 
-static void
-seh_emit_rva (const char *name)
-{
-  char *p = (char *) xmalloc (strlen (name) + 1);
-  char *s = input_line_pointer;
+static int
+seh_x64_size_prologue_data (const seh_context *c)
+{
+  int i, ret = 0;
+
+  for (i = c->elems_count - 1; i >= 0; --i)
+    switch (c->elems[i].code)
+      {
+      case UWOP_PUSH_NONVOL:
+      case UWOP_ALLOC_SMALL:
+      case UWOP_SET_FPREG:
+      case UWOP_PUSH_MACHFRAME:
+       ret += 1;
+       break;
 
-  strcpy (p, name);
-  input_line_pointer = p;
-  s_rva (4);
-  input_line_pointer = s;
-}
+      case UWOP_SAVE_NONVOL:
+      case UWOP_SAVE_XMM128:
+       ret += 2;
+       break;
 
-static void
-seh_emit_long (const char *name)
-{
-  char *p = (char *) xmalloc (strlen (name) + 1);
-  char *s = input_line_pointer;
+      case UWOP_SAVE_NONVOL_FAR:
+      case UWOP_SAVE_XMM128_FAR:
+       ret += 3;
+       break;
 
-  strcpy (p, name);
-  input_line_pointer = p;
-  cons (4);
-  input_line_pointer = s;
-}
+      case UWOP_ALLOC_LARGE:
+       ret += (c->elems[i].info ? 3 : 2);
+       break;
 
-static void
-seh_make_globl (char *sym_name)
-{
-  char *s = input_line_pointer;
+      default:
+       abort ();
+      }
 
-  input_line_pointer = sym_name;
-  s_globl (4);
-  input_line_pointer = s;
+  return ret;
 }
 
-static segT
-seh_make_section2 (const char *section_name, unsigned flags)
+/* Write out the xdata information for one function (x64).  */
+
+static void
+seh_x64_write_function_xdata (seh_context *c)
 {
-  char *name;
-  segT sec;
+  int flags, count_unwind_codes;
+  expressionS exp;
 
-  name = xmalloc (strlen (section_name) + 1);
-  strcpy (name, section_name);
+  /* Set 4-byte alignment.  */
+  frag_align (2, 0, 0);
 
-  sec = subseg_new (name, (subsegT) 0);
-  bfd_set_section_flags (stdoutput, sec,
-                        ((SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA | flags)
-                         & bfd_applicable_section_flags (stdoutput)));
+  c->xdata_addr = symbol_temp_new_now ();
+  flags = c->handler_flags;
+  count_unwind_codes = seh_x64_size_prologue_data (c);
 
-  return sec;
-}
+  /* ubyte:3 version, ubyte:5 flags.  */
+  out_one ((flags << 3) | 1);
 
-static segT
-seh_make_section (void)
-{
-  static segT seg_pdata = NULL;
-  segT sec = NULL;
+  /* Size of prologue.  */
+  if (c->endprologue_addr)
+    {
+      exp.X_op = O_subtract;
+      exp.X_add_symbol = c->endprologue_addr;
+      exp.X_op_symbol = c->start_addr;
+      exp.X_add_number = 0;
+      emit_expr (&exp, 1);
+    }
+  else
+    out_one (0);
 
-  if (!seg_pdata)
-    seg_pdata = seh_make_section2 (".pdata", 0);
-  sec = seg_pdata;
-  return sec;
-}
+  /* Number of slots (i.e. shorts) in the unwind codes array.  */
+  if (count_unwind_codes > 255)
+    as_fatal (_("too much unwind data in this .seh_proc"));
+  out_one (count_unwind_codes);
 
-static char *
-seh_make_xlbl_name (seh_context *c)
-{
-  size_t len = strlen (".seh_xlbl_") + strlen (c->func_name) + 9 + 1;
-  char *ret = (char*) xmalloc (len);
+  /* ubyte:4 frame-reg, ubyte:4 frame-reg-offset.  */
+  /* Note that frameoff is already a multiple of 16, and therefore
+     the offset is already both scaled and shifted into place.  */
+  out_one (c->frameoff | c->framereg);
 
-  if (!ret)
-    as_fatal (_("Out of memory for xdata lable for %s"), c->func_name);
-  else
-    sprintf (ret, ".seh_xlbl_%s_%x", c->func_name, + c->xlbl_count);
-  c->xlbl_count += 1;
-  return ret;
-}
+  seh_x64_write_prologue_data (c);
 
-static char *
-make_seh_text_label (seh_context *c, symbolS **addr)
-{
-  char *sym_name;
-  size_t len = strlen (".seh_tlbl_") + strlen (c->func_name) + 9 + 1;
-
-  sym_name = (char *) xmalloc (len);
-  if (!sym_name)
-    as_fatal (_("Allocating memory for SEH's text symbol for %s failed"), c->func_name);
-  sprintf (sym_name, ".seh_tlbl_%s_%x", c->func_name, c->tlbl_count);
-  c->tlbl_count += 1;
-  if (addr)
+  /* We need to align prologue data.  */
+  if (count_unwind_codes & 1)
+    out_two (0);
+
+  if (flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
     {
-      seh_make_globl (sym_name);
-      *addr = colon (sym_name);
+      /* Force the use of segment-relative relocations instead of absolute
+         valued expressions.  Don't adjust for constants (e.g. NULL).  */
+      if (c->handler.X_op == O_symbol)
+        c->handler.X_op = O_symbol_rva;
+      emit_expr (&c->handler, 4);
     }
-  return sym_name;
+
+  /* Handler data will be tacked in here by subsections.  */
 }
 
-/* x64 secific functions.  */
+/* Write out xdata for one function.  */
 
 static void
-seh_fill_pcsyms (const seh_context *c, char **names, int *idx)
+write_function_xdata (seh_context *c)
 {
-  size_t i;
-  int count = 1;
-  valueT start_off = resolve_symbol_value (c->start_addr);
-  valueT un_off;
-  seh_prologue_element *e = c->elems;
-
-  names[0] = c->start_symbol;
-  idx[0] = 0;
-  if (c->elems_count == 0)
+  segT save_seg = now_seg;
+  int save_subseg = now_subseg;
+
+  /* MIPS, SH, ARM don't have xdata.  */
+  if (seh_get_target_kind () != seh_kind_x64)
     return;
-  for (i = 0; i < c->elems_count; i++)
-    {
-      un_off = resolve_symbol_value (e[i].pc_addr);
-      if ((un_off - start_off) > 255)
-       {
-         names[count] = e[i].pc_symbol;
-         idx[count] = (int) i;
-         count++;
-         start_off = un_off;
-       }
-    }
-}
 
-static int
-seh_needed_unwind_info (seh_context *c)
-{
-  size_t i;
-  int count = 1;
-  valueT start_off = resolve_symbol_value (c->start_addr);
-  valueT un_off;
-  seh_prologue_element *e = c->elems;
-
-  if (c->elems_count == 0)
-    return count;
-  for (i = 0; i < c->elems_count; i++)
-    {
-      un_off = resolve_symbol_value (e[i].pc_addr);
-      if ((un_off - start_off) > 255)
-       {
-         count++;
-         start_off = un_off;
-       }
-    }
-  return count;
-}
+  switch_xdata (c->subsection);
 
-static void
-seh_store_elm_data (const seh_context *c, int elm_start,int elm_end, unsigned char *puwop, valueT base)
-{
-  int i = elm_end;
-  valueT off;
+  seh_x64_write_function_xdata (c);
 
-  /* We have to store in reverse order.  */
-  while (i > elm_start)
-    {
-      --i;
-      /* First comes byte offset in code.  */
-      off = resolve_symbol_value (c->elems[i].pc_addr) - base;
-      *puwop++ = (unsigned char) off;
-      switch (c->elems[i].kind)
-       {
-        case UWOP_PUSH_NONVOL:
-         *puwop = UWOP_PUSH_NONVOL | (c->elems[i].reg << 4);
-         puwop++;
-         break;
-       case UWOP_SET_FPREG:
-         *puwop++ = UWOP_SET_FPREG;
-         break;
-       case UWOP_PUSH_MACHFRAME:
-         *puwop = UWOP_PUSH_MACHFRAME | (c->elems[i].reg << 4);
-         puwop++;
-         break;
-       case UWOP_SAVE_NONVOL:
-         if ((c->elems[i].offset & 7) != 0 ||
-           ((c->elems[i].offset / 8) > 0xffff))
-         {
-           *puwop++ = UWOP_SAVE_NONVOL_FAR | (c->elems[i].reg << 4);
-           bfd_putl32 ((bfd_vma) c->elems[i].offset, puwop);
-           puwop += 4;
-         }
-         else
-         {
-           *puwop++ = UWOP_SAVE_NONVOL | (c->elems[i].reg << 4);
-           bfd_putl16 ((bfd_vma) (c->elems[i].offset / 8), puwop);
-           puwop += 2;
-         }
-         break;
-       case UWOP_SAVE_XMM:
-         if ((c->elems[i].offset & 7) != 0 ||
-           ((c->elems[i].offset / 8) > 0xffff))
-         {
-           *puwop++ = UWOP_SAVE_XMM_FAR | (c->elems[i].reg << 4);
-           bfd_putl32 ((bfd_vma) c->elems[i].offset, puwop);
-           puwop += 4;
-         }
-         else
-         {
-           *puwop++ = UWOP_SAVE_XMM | (c->elems[i].reg << 4);
-           bfd_putl16 ((bfd_vma) (c->elems[i].offset / 8), puwop);
-           puwop += 2;
-         }
-         break;
-       case UWOP_SAVE_XMM128:
-         if ((c->elems[i].offset & 7) != 0 ||
-           ((c->elems[i].offset / 8) > 0xffff))
-         {
-           *puwop++ = UWOP_SAVE_XMM128_FAR | (c->elems[i].reg << 4);
-           bfd_putl32 ((bfd_vma) c->elems[i].offset, puwop);
-           puwop += 4;
-         }
-         else
-         {
-           *puwop++ = UWOP_SAVE_XMM128 | (c->elems[i].reg << 4);
-           bfd_putl16 ((bfd_vma) (c->elems[i].offset / 8), puwop);
-           puwop += 2;
-         }
-         break;
-       case UWOP_ALLOC_LARGE:
-         if ((c->elems[i].offset & 7) != 0 ||
-           ((c->elems[i].offset / 8) > 0xffff))
-         {
-           *puwop++ = UWOP_ALLOC_LARGE | (1 << 4);
-           bfd_putl32 ((bfd_vma) c->elems[i].offset, puwop);
-           puwop += 4;
-         }
-         else if ((c->elems[i].offset / 8) <= 0x10)
-         {
-           *puwop++ = UWOP_ALLOC_SMALL | (((c->elems[i].offset / 8) - 1) << 4);
-         }
-         else
-         {
-           *puwop++ = UWOP_ALLOC_LARGE;
-           bfd_putl16 ((bfd_vma) (c->elems[i].offset / 8), puwop);
-           puwop += 2;
-         }
-         break;
-        default:
-         puwop++;
-         abort ();
-         break;
-       }
-    }
+  subseg_set (save_seg, save_subseg);
 }
 
-static size_t
-seh_getelm_data_size (const seh_context *c, int elm_start, int elm_end, size_t *unaligned_uwcodes)
-{
-  size_t ret = 0;
-
-  while (elm_start < elm_end)
-    {
-      switch (c->elems[elm_start].kind)
-       {
-        case UWOP_PUSH_NONVOL:
-       case UWOP_PUSH_MACHFRAME:
-         ret += 2;
-         break;
-       case UWOP_SAVE_NONVOL:
-       case UWOP_SAVE_XMM:
-       case UWOP_SAVE_XMM128:
-         if ((c->elems[elm_start].offset & 7) != 0 ||
-             ((c->elems[elm_start].offset / 8) > 0xffff))
-           ret += 6;
-         else
-           ret += 4;
-         break;
-       case UWOP_ALLOC_LARGE:
-         if ((c->elems[elm_start].offset & 7) != 0 ||
-             ((c->elems[elm_start].offset / 8) > 0xffff))
-           ret += 6;
-         else if ((c->elems[elm_start].offset / 8) <= 0x10)
-           ret += 2;
-         else
-           ret += 4;
-         break;
-       case UWOP_SET_FPREG:
-         ret += 2;
-         break;
-        default:
-         ret += 2;
-         break;
-       }
-      elm_start++;
-    }
-  if (unaligned_uwcodes)
-    *unaligned_uwcodes = ret;
-  ret = PEX64_UWI_SIZEOF_UWCODE_ARRAY ((ret / 2));
-  return ret;
-}
+/* Write pdata section data for one function (arm).  */
 
-static size_t
-seh_getsize_of_unwind_entry (seh_context *c, int elm_start, int elm_end, int bechain)
+static void
+seh_arm_write_function_pdata (seh_context *c)
 {
-  size_t ret = seh_getelm_data_size(c, elm_start, elm_end, NULL);
+  expressionS exp;
+  unsigned int prol_len = 0, func_len = 0;
+  unsigned int val;
 
-  c->count_syms += 1;
-  if (bechain)
-    {
-      ret += 4 + 4;
-      c->count_syms += 1;
-      c->count_reloc += 1;
-    }
+  /* Start address of the function.  */
+  exp.X_op = O_symbol;
+  exp.X_add_symbol = c->start_addr;
+  exp.X_add_number = 0;
+  emit_expr (&exp, 4);
+
+  exp.X_op = O_subtract;
+  exp.X_add_symbol = c->end_addr;
+  exp.X_op_symbol = c->start_addr;
+  exp.X_add_number = 0;
+  if (resolve_expression (&exp) && exp.X_op == O_constant)
+    func_len = exp.X_add_number;
   else
+    as_bad (_(".seh_endproc in a different section from .seh_proc"));
+
+  if (c->endprologue_addr)
     {
-      ret += 4;
-      if (c->handler_name != NULL)
-       {
-         if (c->handler_data_name != NULL
-             && c->handler_data_name[0] != '@')
-           {
-             ret += 4;
-             c->count_syms += 2;
-             c->count_reloc += 2;
-           }
-         else
-           {
-             ret += 8 + (c->scope_count * 4) * 4;
-             c->count_syms += (c->scope_count * 4) + 1;
-             c->count_reloc += (c->scope_count * 4) + 1;
-           }
-       }
+      exp.X_op = O_subtract;
+      exp.X_add_symbol = c->endprologue_addr;
+      exp.X_op_symbol = c->start_addr;
+      exp.X_add_number = 0;
+
+      if (resolve_expression (&exp) && exp.X_op == O_constant)
+       prol_len = exp.X_add_number;
+      else
+       as_bad (_(".seh_endprologue in a different section from .seh_proc"));
     }
-  return ret;
+
+  /* Both function and prologue are in units of instructions.  */
+  func_len >>= (c->use_instruction_32 ? 2 : 1);
+  prol_len >>= (c->use_instruction_32 ? 2 : 1);
+
+  /* Assemble the second word of the pdata.  */
+  val  = prol_len & 0xff;
+  val |= (func_len & 0x3fffff) << 8;
+  if (c->use_instruction_32)
+    val |= 0x40000000U;
+  if (c->handler_written)
+    val |= 0x80000000U;
+  out_four (val);
 }
 
+/* Write out pdata for one function.  */
+
 static void
-seh_make_unwind_entry (const seh_context *c, char *name, int elm_start, int elm_end, int bechain,
-                      unsigned char *data, size_t *poffs, int no)
+write_function_pdata (seh_context *c)
 {
-  size_t off = *poffs;
-  size_t it;
-  valueT start_off = resolve_symbol_value (c->start_addr);
-  valueT end_prologue;
-  size_t unaligned_uwcodes = 0;
-  size_t uwcodes = seh_getelm_data_size(c, elm_start, elm_end, &unaligned_uwcodes);
-  unsigned int flag = UNW_FLAG_NHANDLER;
-  int idx;
-
-  if (c->handler_name != NULL)
-    {
-      flag = UNW_FLAG_EHANDLER;
-      if (c->handler_data_name != NULL && c->handler_data_name[0] != '@')
-       flag = UNW_FLAG_FHANDLER;
-      else if (c->handler_data_name != NULL &&
-              strcasecmp (c->handler_data_name, "@unwind") == 0)
-       flag = UNW_FLAG_UHANDLER;
-    }
-  if (!c->endprologue_addr)
-    end_prologue = start_off;
-  else
-    end_prologue = resolve_symbol_value (c->endprologue_addr);
-  seh_symbol (c->abfd, name, "", "", c->section, BSF_GLOBAL, (int) off);
-  data[off++] = (1 | ((bechain ? UNW_FLAG_CHAININFO : flag) << 3));
-  if (elm_start != 0)
-    start_off = (valueT) c->elems[elm_start].offset;
-  end_prologue -= start_off;
-  if (end_prologue > 255)
-    end_prologue = 255;
-  data[off++] = (unsigned char) end_prologue;
-  data[off++] = (unsigned char) (unaligned_uwcodes / 2);
-  data[off] = (unsigned char) c->framereg;
-  data[off++] |= (unsigned char) ((c->frameoff / 16) << 4);
-  seh_store_elm_data (c, elm_start, elm_end, &data[off], start_off);
-  off += uwcodes;
-  if (bechain)
-    {
-      char n[100];
+  expressionS exp;
+  segT save_seg = now_seg;
+  int save_subseg = now_subseg;
 
-      sprintf (n,"%x", no);
-      idx = seh_symbol (c->abfd, ".xdata_fct", c->func_name, n, UNDSEC, BSF_GLOBAL, (int) off);
-      seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
-      off += 4;
-    }
-  else if (c->handler_name != NULL)
+  switch_pdata ();
+
+  switch (seh_get_target_kind ())
     {
-      if (flag == UNW_FLAG_FHANDLER)
-       {
-         if (strcasecmp (c->handler_name, "@1") == 0)
-           bfd_put_32 (c->abfd, (bfd_vma) 1, &data[off]);
-         else if (c->handler_name[0] != '@')
-           {
-             idx = seh_symbol (c->abfd, c->handler_name, "", "", UNDSEC, BSF_GLOBAL, 0);
-             seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
-           }
-         off += 4;
-         idx = seh_symbol (c->abfd, c->handler_data_name, "", "", UNDSEC, BSF_GLOBAL, 0);
-         seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
-         off += 4;
-       }
-      else if (flag == UNW_FLAG_UHANDLER || flag == UNW_FLAG_EHANDLER)
-       {
-         if (strcasecmp (c->handler_name, "@1") == 0)
-           bfd_put_32 (c->abfd, (bfd_vma) 1, &data[off]);
-         else if (c->handler_name[0] != '@')
-           {
-             idx = seh_symbol (c->abfd, c->handler_name, "", "", UNDSEC, BSF_GLOBAL, 0);
-             seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
-           }
-         off += 4;
-         bfd_put_32 (c->abfd, (bfd_vma) c->scope_count, &data[off]);
-         off += 4;
-         for (it = 0; it < c->scope_count; it++)
-           {
-             idx = seh_symbol (c->abfd, c->scopes[it].begin_addr, "", "", UNDSEC, BSF_GLOBAL, 0);
-             seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
-             off += 4;
-             idx = seh_symbol (c->abfd, c->scopes[it].end_addr, "", "", UNDSEC, BSF_GLOBAL, 0);
-             seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
-             off += 4;
-             if (c->scopes[it].handler_addr[0] == '@')
-               {
-                 if (strcasecmp (c->scopes[it].handler_addr, "@1") == 0)
-                   bfd_put_32 (c->abfd, (bfd_vma) 1, &data[off]);
-               }
-             else
-               {
-                 idx = seh_symbol (c->abfd, c->scopes[it].handler_addr, "", "", UNDSEC, BSF_GLOBAL, 0);
-                 seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
-               }
-             off += 4;
-             if (c->scopes[it].jump_addr[0] == '@')
-               {
-                 if (strcasecmp (c->scopes[it].jump_addr, "@1") == 0)
-                   bfd_put_32 (c->abfd, (bfd_vma) 1, &data[off]);
-               }
-             else
-               {
-                 idx = seh_symbol (c->abfd, c->scopes[it].jump_addr, "", "", UNDSEC, BSF_GLOBAL, 0);
-                 seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
-               }
-             off += 4;
-           }
-       }
-    }
-  *poffs = off;
-}
+    case seh_kind_x64:
+      exp.X_op = O_symbol_rva;
+      exp.X_add_number = 0;
+
+      exp.X_add_symbol = c->start_addr;
+      emit_expr (&exp, 4);
+      exp.X_add_symbol = c->end_addr;
+      emit_expr (&exp, 4);
+      exp.X_add_symbol = c->xdata_addr;
+      emit_expr (&exp, 4);
+      break;
 
-static size_t
-seh_getsize_unwind_data (seh_context *c)
-{
-  int need = seh_needed_unwind_info (c);
-  int i;
-  char **names = (char **) xmalloc (sizeof (char *) * need);
-  char **pc_syms = (char **) xmalloc (sizeof (char *) * need);
-  int *elm_start = (int *) xmalloc (sizeof (int) * (need + 1));
-  size_t xdata_sz = 0;
-
-  seh_fill_pcsyms (c, pc_syms, elm_start);
-  elm_start[need] = c->elems_count;
-
-  xdata_sz += ((12 * (size_t) need));
-  c->count_syms += 5 * need;
-  xdata_sz += (seh_getsize_of_unwind_entry (c, elm_start[0], elm_start[1], 1 != need) + 7) & ~7;
-  for (i = 1; i < need; i++)
-    xdata_sz += (seh_getsize_of_unwind_entry (c, elm_start[i], elm_start[i + 1], 1 != need) + 7) & ~7;
-
-  /* Create lable names for .xdata unwind info.  */
-  names[0] = c->xdata_first;
-  for (i = 1; i < need; i++)
-    names[i] = seh_make_xlbl_name (c);
-  c->xdata_names = names;
-  c->xdata_pcsyms = pc_syms;
-  c->xdata_elm_start = elm_start;
-  c->xdata_sz = xdata_sz;
-  return xdata_sz;
-}
+    case seh_kind_mips:
+      exp.X_op = O_symbol;
+      exp.X_add_number = 0;
 
-static void
-seh_create_unwind_data (seh_context *c, unsigned char *data, size_t offs)
-{
-  int need = seh_needed_unwind_info (c);
-  int i;
-  char **names = c->xdata_names;
-  char **pc_syms = c->xdata_pcsyms;
-  int *elm_start = c->xdata_elm_start;
-
-  for (i = 1; i < need; i++)
-    seh_make_function_entry_xdata (c, pc_syms[i], c->end_symbol, names[i], data, &offs, i);
-
-  /* Generate the function entry. Remark, that just
-     first is in .pdata section and already emitted.  */
-  seh_make_unwind_entry (c, c->xdata_first, elm_start[0], elm_start[1], 1 != need, data, &offs, 1);
-  for (i = 1; i < need; i++)
-    seh_make_unwind_entry (c, names[i], elm_start[i], elm_start[i + 1], (i + 1) != need, data, &offs, i + 1);
-
-  for (i = 1; i < need; i++)
-    free (names[i]);
-  free (names);
-  free (pc_syms);
-  free (elm_start);
-  c->xdata_names = NULL;
-  c->xdata_pcsyms = NULL;
-  c->xdata_elm_start = NULL;
-}
+      exp.X_add_symbol = c->start_addr;
+      emit_expr (&exp, 4);
+      exp.X_add_symbol = c->end_addr;
+      emit_expr (&exp, 4);
 
-static void
-seh_make_function_entry_xdata (seh_context *c, char *pc_start, char *pc_end, char *pc_xdata, unsigned char *data, size_t *poffs,int no)
-{
-  bfd_vma addr = (bfd_vma) *poffs;
-  int idx;
-  char s[100];
+      emit_expr (&c->handler, 4);
+      emit_expr (&c->handler_data, 4);
 
-  if (!data)
-    return;
-  sprintf (s,"%x",no);
-  seh_symbol (c->abfd, ".xdata_fct",c->func_name, s, c->section, BSF_GLOBAL, (int) poffs[0]);
-  idx = seh_symbol (c->abfd, pc_start,"","", UNDSEC, BSF_GLOBAL,0);
-  seh_reloc (c->abfd, addr, BFD_RELOC_RVA, idx);
-  idx = seh_symbol (c->abfd, pc_end,"","", UNDSEC, BSF_GLOBAL,0);
-  seh_reloc (c->abfd, addr + 4, BFD_RELOC_RVA, idx);
-  idx = seh_symbol (c->abfd, pc_xdata,"","", UNDSEC, BSF_GLOBAL,0);
-  seh_reloc (c->abfd, addr + 8, BFD_RELOC_RVA, idx);
-  poffs[0] += 12;
-}
+      exp.X_add_symbol = (c->endprologue_addr
+                         ? c->endprologue_addr
+                         : c->start_addr);
+      emit_expr (&exp, 4);
+      break;
 
-static seh_scope_elem *
-seh_x64_makescope_elem (seh_context *c, const char *begin, const char *end,
-                       const char *handler, const char *jmp)
-{
-  seh_scope_elem *r;
+    case seh_kind_arm:
+      seh_arm_write_function_pdata (c);
+      break;
 
-  if (!end || !begin)
-    return NULL;
-  if (c->scope_count >= c->scope_max)
-    {
-      seh_scope_elem *h = (seh_scope_elem *) xmalloc (sizeof (seh_scope_elem) * (c->scope_max + 8));
-      memset (h, 0, sizeof (seh_scope_elem) * (c->scope_max + 8));
-      if (c->scopes != NULL)
-       memcpy (h, c->scopes, sizeof (seh_scope_elem) * c->scope_max);
-      if (c->scopes != NULL)
-       free (c->scopes);
-      c->scopes = h;
-      c->scope_max += 8;
+    default:
+      abort ();
     }
-  r = &c->scopes[c->scope_count++];
-  r->begin_addr = xstrdup (begin);
-  r->end_addr = xstrdup (end);
-  r->handler_addr = (!handler ? NULL : xstrdup (handler));
-  r->jump_addr = (!jmp ? NULL : xstrdup (jmp));
-  return r;
+
+  subseg_set (save_seg, save_subseg);
 }
index aa151cd..429c100 100644 (file)
@@ -45,7 +45,9 @@
   The pseudos:
   .seh_proc <fct_name>
   .seh_endprologue
-  .seh_handler <handler>[,<handler-data>]]
+  .seh_handler <handler>[,@unwind][,@except]   (x64)
+  .seh_handler <handler>[,<handler_data>]      (others)
+  .seh_handlerdata
   .seh_eh
   .seh_32/.seh_no32
   .seh_endproc
   .seh_stackalloc
   .seh_pushreg
   .seh_savereg
-  .seh_savemm
   .seh_savexmm
   .seh_pushframe
-  .seh_scope
-  */
+*/
 
 /* architecture specific pdata/xdata handling.  */
 #define SEH_CMDS \
         {"seh_proc", obj_coff_seh_proc, 0}, \
         {"seh_endproc", obj_coff_seh_endproc, 0}, \
-        {"seh_pushreg", obj_coff_seh_push, 0}, \
-        {"seh_savereg", obj_coff_seh_save, 0}, \
-        {"seh_savemm", obj_coff_seh_save, 1}, \
+        {"seh_pushreg", obj_coff_seh_pushreg, 0}, \
+        {"seh_savereg", obj_coff_seh_save, 1}, \
         {"seh_savexmm", obj_coff_seh_save, 2}, \
-        {"seh_pushframe", obj_coff_seh_push, 1}, \
+        {"seh_pushframe", obj_coff_seh_pushframe, 0}, \
         {"seh_endprologue", obj_coff_seh_endprologue, 0}, \
         {"seh_setframe", obj_coff_seh_setframe, 0}, \
-        {"seh_stackalloc", obj_coff_seh_stack_alloc, 0}, \
-       {"seh_handler", obj_coff_seh_handler, 0}, \
+        {"seh_stackalloc", obj_coff_seh_stackalloc, 0}, \
        {"seh_eh", obj_coff_seh_eh, 0}, \
        {"seh_32", obj_coff_seh_32, 1}, \
        {"seh_no32", obj_coff_seh_32, 0}, \
-       {"seh_scope", obj_coff_seh_scope, 0},
+       {"seh_handler", obj_coff_seh_handler, 0}, \
+       {"seh_handlerdata", obj_coff_seh_handlerdata, 0},
 
 /* Type definitions.  */
 
 typedef struct seh_prologue_element
 {
+  int code;
+  int info;
+  offsetT off;
   symbolS *pc_addr;
-  char *pc_symbol;
-  int kind;
-  int reg;
-  bfd_vma offset;
 } seh_prologue_element;
 
-typedef struct seh_scope_elem {
-  char *begin_addr;
-  char *end_addr;
-  char *handler_addr;
-  char *jump_addr;
-} seh_scope_elem;
-
 typedef struct seh_context
 {
   struct seh_context *next;
-  /* Was record alread processed.  */
-  int done;
+
   /* Function name.  */
   char *func_name;
   /* BeginAddress.  */
-  char *start_symbol;
   symbolS *start_addr;
-  bfd_vma start_offset;
   /* EndAddress.  */
-  char *end_symbol;
   symbolS *end_addr;
-  bfd_vma end_offset;
+  /* Unwind data.  */
+  symbolS *xdata_addr;
   /* PrologueEnd.  */
-  char *endprologue_symbol;
   symbolS *endprologue_addr;
-  bfd_vma endprologue_offset;
   /* ExceptionHandler.  */
-  char *handler_name;
-  /* ExceptionHandlerData.  */
-  char *handler_data_name;
+  expressionS handler;
+  /* ExceptionHandlerData. (arm, mips)  */
+  expressionS handler_data;
+
+  /* ARM .seh_eh directive seen.  */
   int handler_written;
+
   /* WinCE specific data.  */
   int use_instruction_32;
+  /* Was record already processed.  */
+  int done;
+
+  /* x64 flags for the xdata header.  */
+  int handler_flags;
+  int subsection;
 
-  /* the bfd to store data within.  */
-  bfd *abfd;
-  /* the current section to generate data within.  */
-  asection *section;
-  /* Relocations for section.  */
-  unsigned int count_reloc;
-  /* Symbols within section.  */
-  unsigned int count_syms;
-  /* Iterator for text lable generation.  */
-  unsigned int tlbl_count;
-  /* Iterator for xdata lable generation.  */
-  unsigned int xlbl_count;
-  /* The name of the first xdata label.  */
-  char *xdata_first;
-  /* FIelds used for x64 generation of chained information.  */
-  char **xdata_names;
-  char **xdata_pcsyms;
-  int *xdata_elm_start;
-  /* Size and offset within current generated xdata section.  */
-  size_t xdata_sz;
-  size_t xdata_offset;
   /* x64 framereg and frame offset information.  */
   int framereg;
-  bfd_vma frameoff;
+  int frameoff;
+
   /* Information about x64 specific unwind data fields.  */
-  size_t elems_count;
-  size_t elems_max;
+  int elems_count;
+  int elems_max;
   seh_prologue_element *elems;
-  size_t scope_max;
-  size_t scope_count;
-  seh_scope_elem *scopes;
 } seh_context;
 
 typedef enum seh_kind {
@@ -163,21 +135,18 @@ typedef enum seh_kind {
 } seh_kind;
 
 /* Forward declarations.  */
-static void obj_coff_seh_stack_alloc (int);
+static void obj_coff_seh_stackalloc (int);
 static void obj_coff_seh_setframe (int);
 static void obj_coff_seh_endprologue (int);
-static void obj_coff_seh_save  (int);
-static void obj_coff_seh_push  (int);
+static void obj_coff_seh_save (int);
+static void obj_coff_seh_pushreg (int);
+static void obj_coff_seh_pushframe (int);
 static void obj_coff_seh_endproc  (int);
 static void obj_coff_seh_eh (int);
 static void obj_coff_seh_32 (int);
 static void obj_coff_seh_proc  (int);
 static void obj_coff_seh_handler (int);
-static void obj_coff_seh_scope (int);
-static int seh_read_offset (const char *, bfd_vma *);
-static int seh_x64_read_reg (const char *, int, int *);
-static void seh_x64_make_prologue_element (int, int, bfd_vma);
-static void make_function_entry_pdata (seh_context *c);
+static void obj_coff_seh_handlerdata (int);
 
 #define UNDSEC (asection *) &bfd_und_section