This commit was manufactured by cvs2svn to create branch 'binutils-
[external/binutils.git] / gas / config / tc-tic6x.c
index 730b786..017254a 100644 (file)
@@ -1,6 +1,8 @@
 /* TI C6X assembler.
-   Copyright 2010
+   Copyright 2010, 2011
    Free Software Foundation, Inc.
+   Contributed by Joseph Myers <joseph@codesourcery.com>
+                 Bernd Schmidt  <bernds@codesourcery.com>
 
    This file is part of GAS, the GNU Assembler.
 
 
 #include "as.h"
 #include "dwarf2dbg.h"
+#include "dw2gencfi.h"
 #include "safe-ctype.h"
 #include "subsegs.h"
 #include "opcode/tic6x.h"
+#include "elf/tic6x.h"
+#include "elf32-tic6x.h"
 
 /* Truncate and sign-extend at 32 bits, so that building on a 64-bit
    host gives identical results to a 32-bit host.  */
 #define TRUNC(X)       ((valueT) (X) & 0xffffffffU)
 #define SEXT(X)                ((TRUNC (X) ^ 0x80000000U) - 0x80000000U)
 
+#define streq(a, b)           (strcmp (a, b) == 0)
+
+/* Stuff for .scomm symbols.  */
+static segT sbss_section;
+static asection scom_section;
+static asymbol scom_symbol;
+
 const char comment_chars[] = ";";
 const char line_comment_chars[] = "#*;";
 const char line_separator_chars[] = "@";
@@ -42,42 +54,53 @@ const char *md_shortopts = "";
 enum
   {
     OPTION_MARCH = OPTION_MD_BASE,
-    OPTION_MATOMIC,
-    OPTION_MNO_ATOMIC,
     OPTION_MBIG_ENDIAN,
-    OPTION_MLITTLE_ENDIAN
+    OPTION_MLITTLE_ENDIAN,
+    OPTION_MDSBT,
+    OPTION_MNO_DSBT,
+    OPTION_MPID,
+    OPTION_MPIC,
+    OPTION_MNO_PIC,
+    OPTION_MGENERATE_REL
   };
 
 struct option md_longopts[] =
   {
     { "march", required_argument, NULL, OPTION_MARCH },
-    { "matomic", no_argument, NULL, OPTION_MATOMIC },
-    { "mno-atomic", no_argument, NULL, OPTION_MNO_ATOMIC },
     { "mbig-endian", no_argument, NULL, OPTION_MBIG_ENDIAN },
     { "mlittle-endian", no_argument, NULL, OPTION_MLITTLE_ENDIAN },
+    { "mdsbt", no_argument, NULL, OPTION_MDSBT },
+    { "mno-dsbt", no_argument, NULL, OPTION_MNO_DSBT },
+    { "mpid", required_argument, NULL, OPTION_MPID },
+    { "mpic", no_argument, NULL, OPTION_MPIC },
+    { "mno-pic", no_argument, NULL, OPTION_MNO_PIC },
+    { "mgenerate-rel", no_argument, NULL, OPTION_MGENERATE_REL },
     { NULL, no_argument, NULL, 0 }
   };
 size_t md_longopts_size = sizeof (md_longopts);
 
-/* Whether to enable atomic instructions.  1 to enable them, 0 to
-   disable, -1 to default from architecture.  */
-static int tic6x_atomic = -1;
-
 /* The instructions enabled based only on the selected architecture
-   (all instructions, if no architecture specified).  Atomic
-   instructions may be enabled or disabled separately.  */
+   (all instructions, if no architecture specified).  */
 static unsigned short tic6x_arch_enable = (TIC6X_INSN_C62X
                                           | TIC6X_INSN_C64X
                                           | TIC6X_INSN_C64XP
                                           | TIC6X_INSN_C67X
                                           | TIC6X_INSN_C67XP
-                                          | TIC6X_INSN_C674X
-                                          | TIC6X_INSN_ATOMIC);
+                                          | TIC6X_INSN_C674X);
 
 /* The instructions enabled based on the current set of features
    (architecture, as modified by other options).  */
 static unsigned short tic6x_features;
 
+/* The architecture attribute value, or C6XABI_Tag_ISA_none if
+   not yet set.  */
+static int tic6x_arch_attribute = C6XABI_Tag_ISA_none;
+
+/* Whether any instructions at all have been seen.  Once any
+   instructions have been seen, architecture attributes merge into the
+   previous attribute value rather than replacing it.  */
+static bfd_boolean tic6x_seen_insns = FALSE;
+
 /* The number of registers in each register file supported by the
    current architecture.  */
 static unsigned int tic6x_num_registers;
@@ -95,27 +118,96 @@ static bfd_boolean tic6x_long_data_constraints;
 /* Whether compact instructions are available.  */
 static bfd_boolean tic6x_compact_insns;
 
+/* Whether to generate RELA relocations.  */
+static bfd_boolean tic6x_generate_rela = TRUE;
+
+/* Whether the code uses DSBT addressing.  */
+static bfd_boolean tic6x_dsbt;
+
+/* Types of position-independent data (attribute values for
+   Tag_ABI_PID).  */
+typedef enum
+  {
+    tic6x_pid_no = 0,
+    tic6x_pid_near = 1,
+    tic6x_pid_far = 2
+  } tic6x_pid_type;
+
+/* The type of data addressing used in this code.  */
+static tic6x_pid_type tic6x_pid;
+
+/* Whether the code uses position-independent code.  */
+static bfd_boolean tic6x_pic;
+
 /* Table of supported architecture variants.  */
 typedef struct
 {
   const char *arch;
+  int attr;
   unsigned short features;
 } tic6x_arch_table;
 static const tic6x_arch_table tic6x_arches[] =
   {
-    { "c62x", TIC6X_INSN_C62X },
-    { "c64x", TIC6X_INSN_C62X | TIC6X_INSN_C64X },
-    { "c64x+", TIC6X_INSN_C62X | TIC6X_INSN_C64X | TIC6X_INSN_C64XP },
-    { "c67x", TIC6X_INSN_C62X | TIC6X_INSN_C67X },
-    { "c67x+", TIC6X_INSN_C62X | TIC6X_INSN_C67X | TIC6X_INSN_C67XP },
-    { "c674x", (TIC6X_INSN_C62X
-               | TIC6X_INSN_C64X
-               | TIC6X_INSN_C64XP
-               | TIC6X_INSN_C67X
-               | TIC6X_INSN_C67XP
-               | TIC6X_INSN_C674X) }
+    { "c62x", C6XABI_Tag_ISA_C62X, TIC6X_INSN_C62X },
+    { "c64x", C6XABI_Tag_ISA_C64X, TIC6X_INSN_C62X | TIC6X_INSN_C64X },
+    { "c64x+", C6XABI_Tag_ISA_C64XP, (TIC6X_INSN_C62X
+                                     | TIC6X_INSN_C64X
+                                     | TIC6X_INSN_C64XP) },
+    { "c67x", C6XABI_Tag_ISA_C67X, TIC6X_INSN_C62X | TIC6X_INSN_C67X },
+    { "c67x+", C6XABI_Tag_ISA_C67XP, (TIC6X_INSN_C62X
+                                     | TIC6X_INSN_C67X
+                                     | TIC6X_INSN_C67XP) },
+    { "c674x", C6XABI_Tag_ISA_C674X, (TIC6X_INSN_C62X
+                                     | TIC6X_INSN_C64X
+                                     | TIC6X_INSN_C64XP
+                                     | TIC6X_INSN_C67X
+                                     | TIC6X_INSN_C67XP
+                                     | TIC6X_INSN_C674X) }
   };
 
+/* Caller saved register encodings.  The standard frame layout uses this
+   order, starting from the highest address.  There must be
+   TIC6X_NUM_UNWIND_REGS values.  */
+enum
+{
+  UNWIND_A15,
+  UNWIND_B15,
+  UNWIND_B14,
+  UNWIND_B13,
+  UNWIND_B12,
+  UNWIND_B11,
+  UNWIND_B10,
+  UNWIND_B3,
+  UNWIND_A14,
+  UNWIND_A13,
+  UNWIND_A12,
+  UNWIND_A11,
+  UNWIND_A10
+};
+
+static void tic6x_output_unwinding (bfd_boolean need_extab);
+
+/* Return the frame unwind state for the current function, allocating
+   as necessary.  */
+
+static tic6x_unwind_info *tic6x_get_unwind (void)
+{
+  tic6x_unwind_info *unwind;
+
+  unwind = seg_info (now_seg)->tc_segment_info_data.unwind;
+  if (unwind)
+    return unwind;
+
+  unwind = seg_info (now_seg)->tc_segment_info_data.text_unwind;
+  if (unwind)
+    return unwind;
+
+  unwind = (tic6x_unwind_info *)xmalloc (sizeof (tic6x_unwind_info));
+  seg_info (now_seg)->tc_segment_info_data.unwind = unwind;
+  memset (unwind, 0, sizeof (*unwind));
+  return unwind;
+}
+
 /* Update the selected architecture based on ARCH, giving an error if
    ARCH is an invalid value.  Does not call tic6x_update_features; the
    caller must do that if necessary.  */
@@ -129,12 +221,48 @@ tic6x_use_arch (const char *arch)
     if (strcmp (arch, tic6x_arches[i].arch) == 0)
       {
        tic6x_arch_enable = tic6x_arches[i].features;
+       if (tic6x_seen_insns)
+         tic6x_arch_attribute
+           = elf32_tic6x_merge_arch_attributes (tic6x_arch_attribute,
+                                                tic6x_arches[i].attr);
+       else
+         tic6x_arch_attribute = tic6x_arches[i].attr;
        return;
       }
 
   as_bad (_("unknown architecture '%s'"), arch);
 }
 
+/* Table of supported -mpid arguments.  */
+typedef struct
+{
+  const char *arg;
+  tic6x_pid_type attr;
+} tic6x_pid_type_table;
+static const tic6x_pid_type_table tic6x_pid_types[] =
+  {
+    { "no", tic6x_pid_no },
+    { "near", tic6x_pid_near },
+    { "far", tic6x_pid_far }
+  };
+
+/* Handle -mpid=ARG.  */
+
+static void
+tic6x_use_pid (const char *arg)
+{
+  unsigned int i;
+
+  for (i = 0; i < ARRAY_SIZE (tic6x_pid_types); i++)
+    if (strcmp (arg, tic6x_pid_types[i].arg) == 0)
+      {
+       tic6x_pid = tic6x_pid_types[i].attr;
+       return;
+      }
+
+  as_bad (_("unknown -mpid= argument '%s'"), arg);
+}
+
 /* Parse a target-specific option.  */
 
 int
@@ -146,14 +274,6 @@ md_parse_option (int c, char *arg)
       tic6x_use_arch (arg);
       break;
 
-    case OPTION_MATOMIC:
-      tic6x_atomic = 1;
-      break;
-
-    case OPTION_MNO_ATOMIC:
-      tic6x_atomic = 0;
-      break;
-
     case OPTION_MBIG_ENDIAN:
       target_big_endian = 1;
       break;
@@ -162,6 +282,30 @@ md_parse_option (int c, char *arg)
       target_big_endian = 0;
       break;
 
+    case OPTION_MDSBT:
+      tic6x_dsbt = 1;
+      break;
+
+    case OPTION_MNO_DSBT:
+      tic6x_dsbt = 0;
+      break;
+
+    case OPTION_MPID:
+      tic6x_use_pid (arg);
+      break;
+
+    case OPTION_MPIC:
+      tic6x_pic = 1;
+      break;
+
+    case OPTION_MNO_PIC:
+      tic6x_pic = 0;
+      break;
+
+    case OPTION_MGENERATE_REL:
+      tic6x_generate_rela = FALSE;
+      break;
+
     default:
       return 0;
     }
@@ -176,10 +320,19 @@ md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
   fputc ('\n', stream);
   fprintf (stream, _("TMS320C6000 options:\n"));
   fprintf (stream, _("  -march=ARCH             enable instructions from architecture ARCH\n"));
-  fprintf (stream, _("  -matomic                enable atomic operation instructions\n"));
-  fprintf (stream, _("  -mno-atomic             disable atomic operation instructions\n"));
   fprintf (stream, _("  -mbig-endian            generate big-endian code\n"));
   fprintf (stream, _("  -mlittle-endian         generate little-endian code\n"));
+  fprintf (stream, _("  -mdsbt                  code uses DSBT addressing\n"));
+  fprintf (stream, _("  -mno-dsbt               code does not use DSBT addressing\n"));
+  fprintf (stream, _("  -mpid=no                code uses position-dependent data addressing\n"));
+  fprintf (stream, _("  -mpid=near              code uses position-independent data addressing,\n"
+                    "                            GOT accesses use near DP addressing\n"));
+  fprintf (stream, _("  -mpid=far               code uses position-independent data addressing,\n"
+                    "                            GOT accesses use far DP addressing\n"));
+  fprintf (stream, _("  -mpic                   code addressing is position-independent\n"));
+  fprintf (stream, _("  -mno-pic                code addressing is position-dependent\n"));
+  /* -mgenerate-rel is only for testsuite use and is deliberately
+      undocumented.  */
 
   fputc ('\n', stream);
   fprintf (stream, _("Supported ARCH values are:"));
@@ -193,23 +346,7 @@ md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
 static void
 tic6x_update_features (void)
 {
-  switch (tic6x_atomic)
-    {
-    case -1:
-      tic6x_features = tic6x_arch_enable;
-      break;
-
-    case 0:
-      tic6x_features = tic6x_arch_enable & ~TIC6X_INSN_ATOMIC;
-      break;
-
-    case 1:
-      tic6x_features = tic6x_arch_enable | TIC6X_INSN_ATOMIC;
-      break;
-
-    default:
-      abort ();
-    }
+  tic6x_features = tic6x_arch_enable;
 
   tic6x_num_registers
     = (tic6x_arch_enable & (TIC6X_INSN_C64X | TIC6X_INSN_C67XP)) ? 32 : 16;
@@ -234,8 +371,122 @@ tic6x_after_parse_args (void)
   tic6x_update_features ();
 }
 
-/* Parse a .arch directive.  */
+/* Parse a .cantunwind directive.  */
+static void
+s_tic6x_cantunwind (int ignored ATTRIBUTE_UNUSED)
+{
+  tic6x_unwind_info *unwind = tic6x_get_unwind ();
+
+  /* GCC sometimes spits out superfluous .cantunwind directives, so ignore
+     them.  */
+  if (unwind->data_bytes == 0)
+    return;
+
+  if (unwind->data_bytes != -1)
+    {
+      as_bad (_("unexpected .cantunwind directive"));
+      return;
+    }
+
+  demand_empty_rest_of_line ();
+
+  if (unwind->personality_routine || unwind->personality_index != -1)
+    as_bad (_("personality routine specified for cantunwind frame"));
+
+  unwind->personality_index = -2;
+}
+
+/* Parse a .handlerdata directive.  */
+static void
+s_tic6x_handlerdata (int ignored ATTRIBUTE_UNUSED)
+{
+  tic6x_unwind_info *unwind = tic6x_get_unwind ();
+
+  if (!unwind->saved_seg)
+    {
+      as_bad (_("unexpected .handlerdata directive"));
+      return;
+    }
+
+  if (unwind->table_entry || unwind->personality_index == -2)
+    {
+      as_bad (_("duplicate .handlerdata directive"));
+      return;
+    }
+
+  if (unwind->personality_index == -1 && unwind->personality_routine == NULL)
+    {
+      as_bad (_("personality routine required before .handlerdata directive"));
+      return;
+    }
 
+  tic6x_output_unwinding (TRUE);
+}
+
+/* Parse a .endp directive.  */
+static void
+s_tic6x_endp (int ignored ATTRIBUTE_UNUSED)
+{
+  tic6x_unwind_info *unwind = tic6x_get_unwind ();
+
+  if (unwind->data_bytes != 0)
+    {
+      /* Output a .exidx entry if we have not already done so.
+        Then switch back to the text section.  */
+      if (!unwind->table_entry)
+       tic6x_output_unwinding (FALSE);
+
+      subseg_set (unwind->saved_seg, unwind->saved_subseg);
+    }
+
+  unwind->saved_seg = NULL;
+  unwind->table_entry = NULL;
+  unwind->data_bytes = 0;
+}
+
+/* Parse a .personalityindex directive.  */
+static void
+s_tic6x_personalityindex (int ignored ATTRIBUTE_UNUSED)
+{
+  tic6x_unwind_info *unwind = tic6x_get_unwind ();
+  expressionS exp;
+
+  if (unwind->personality_routine || unwind->personality_index != -1)
+    as_bad (_("duplicate .personalityindex directive"));
+
+  expression (&exp);
+
+  if (exp.X_op != O_constant
+      || exp.X_add_number < 0 || exp.X_add_number > 15)
+    {
+      as_bad (_("bad personality routine number"));
+      ignore_rest_of_line ();
+      return;
+    }
+
+  unwind->personality_index = exp.X_add_number;
+
+  demand_empty_rest_of_line ();
+}
+
+static void
+s_tic6x_personality (int ignored ATTRIBUTE_UNUSED)
+{
+  char *name, *p, c;
+  tic6x_unwind_info *unwind = tic6x_get_unwind ();
+
+  if (unwind->personality_routine || unwind->personality_index != -1)
+    as_bad (_("duplicate .personality directive"));
+
+  name = input_line_pointer;
+  c = get_symbol_end ();
+  p = input_line_pointer;
+  unwind->personality_routine = symbol_find_or_make (name);
+  *p = c;
+  demand_empty_rest_of_line ();
+}
+
+/* Parse a .arch directive.  */
 static void
 s_tic6x_arch (int ignored ATTRIBUTE_UNUSED)
 {
@@ -254,42 +505,240 @@ s_tic6x_arch (int ignored ATTRIBUTE_UNUSED)
   demand_empty_rest_of_line ();
 }
 
-/* Parse a .atomic directive.  */
+/* Parse a .ehtype directive.  */
 
 static void
-s_tic6x_atomic (int ignored ATTRIBUTE_UNUSED)
+s_tic6x_ehtype (int ignored ATTRIBUTE_UNUSED)
 {
-  tic6x_atomic = 1;
-  tic6x_update_features ();
+  expressionS exp;
+  char *p;
+
+#ifdef md_flush_pending_output
+  md_flush_pending_output ();
+#endif
+
+  if (is_it_end_of_statement ())
+    {
+      demand_empty_rest_of_line ();
+      return;
+    }
+
+#ifdef md_cons_align
+  md_cons_align (4);
+#endif
+
+
+  expression (&exp);
+
+  if (exp.X_op != O_symbol)
+    {
+      as_bad (_("expected symbol"));
+      return;
+    }
+
+  p = frag_more (4);
+  fix_new_exp (frag_now, p - frag_now->fr_literal, 4,
+              &exp, 0, BFD_RELOC_C6000_EHTYPE);
+
   demand_empty_rest_of_line ();
 }
 
-/* Parse a .noatomic directive.  */
+/* Parse a .nocmp directive.  */
 
 static void
-s_tic6x_noatomic (int ignored ATTRIBUTE_UNUSED)
+s_tic6x_nocmp (int ignored ATTRIBUTE_UNUSED)
 {
-  tic6x_atomic = 0;
-  tic6x_update_features ();
+  seg_info (now_seg)->tc_segment_info_data.nocmp = TRUE;
   demand_empty_rest_of_line ();
 }
 
-/* Parse a .nocmp directive.  */
+/* .scomm pseudo-op handler.
+
+   This is a new pseudo-op to handle putting objects in .scommon.
+   By doing this the linker won't need to do any work,
+   and more importantly it removes the implicit -G arg necessary to
+   correctly link the object file.  */
 
 static void
-s_tic6x_nocmp (int ignored ATTRIBUTE_UNUSED)
+s_tic6x_scomm (int ignore ATTRIBUTE_UNUSED)
 {
-  seg_info (now_seg)->tc_segment_info_data.nocmp = TRUE;
+  char *name;
+  char c;
+  char *p;
+  offsetT size;
+  symbolS *symbolP;
+  offsetT align;
+  int align2;
+
+  name = input_line_pointer;
+  c = get_symbol_end ();
+
+  /* Just after name is now '\0'.  */
+  p = input_line_pointer;
+  *p = c;
+  SKIP_WHITESPACE ();
+  if (*input_line_pointer != ',')
+    {
+      as_bad (_("expected comma after symbol name"));
+      ignore_rest_of_line ();
+      return;
+    }
+
+  /* Skip ','.  */
+  input_line_pointer++;
+  if ((size = get_absolute_expression ()) < 0)
+    {
+      /* xgettext:c-format  */
+      as_warn (_("invalid length for .scomm directive"));
+      ignore_rest_of_line ();
+      return;
+    }
+
+  /* The third argument to .scomm is the alignment.  */
+  if (*input_line_pointer != ',')
+    align = 8;
+  else
+    {
+      ++input_line_pointer;
+      align = get_absolute_expression ();
+      if (align <= 0)
+       {
+         as_warn (_("alignment is not a positive number"));
+         align = 8;
+       }
+    }
+
+  /* Convert to a power of 2 alignment.  */
+  if (align)
+    {
+      for (align2 = 0; (align & 1) == 0; align >>= 1, ++align2)
+       continue;
+      if (align != 1)
+       {
+         as_bad (_("alignment is not a power of 2"));
+         ignore_rest_of_line ();
+         return;
+       }
+    }
+  else
+    align2 = 0;
+
+  *p = 0;
+  symbolP = symbol_find_or_make (name);
+  *p = c;
+
+  if (S_IS_DEFINED (symbolP))
+    {
+      /* xgettext:c-format  */
+      as_bad (_("attempt to re-define symbol `%s'"),
+             S_GET_NAME (symbolP));
+      ignore_rest_of_line ();
+      return;
+    }
+
+  if (S_GET_VALUE (symbolP) && S_GET_VALUE (symbolP) != (valueT) size)
+    {
+      /* xgettext:c-format  */
+      as_bad (_("attempt to redefine `%s' with a different length"),
+             S_GET_NAME (symbolP));
+
+      ignore_rest_of_line ();
+      return;
+    }
+
+  if (symbol_get_obj (symbolP)->local)
+    {
+      segT old_sec = now_seg;
+      int old_subsec = now_subseg;
+      char *pfrag;
+
+      record_alignment (sbss_section, align2);
+      subseg_set (sbss_section, 0);
+
+      if (align2)
+       frag_align (align2, 0, 0);
+
+      if (S_GET_SEGMENT (symbolP) == sbss_section)
+       symbol_get_frag (symbolP)->fr_symbol = 0;
+
+      symbol_set_frag (symbolP, frag_now);
+
+      pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
+                       (char *) 0);
+      *pfrag = 0;
+      S_SET_SIZE (symbolP, size);
+      S_SET_SEGMENT (symbolP, sbss_section);
+      S_CLEAR_EXTERNAL (symbolP);
+      subseg_set (old_sec, old_subsec);
+    }
+  else
+    {
+      S_SET_VALUE (symbolP, (valueT) size);
+      S_SET_ALIGN (symbolP, 1 << align2);
+      S_SET_EXTERNAL (symbolP);
+      S_SET_SEGMENT (symbolP, &scom_section);
+    }
+
+  symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
+
   demand_empty_rest_of_line ();
 }
 
+/* Track for each attribute whether it has been set explicitly (and so
+   should not have a default value set by the assembler).  */
+static bfd_boolean tic6x_attributes_set_explicitly[NUM_KNOWN_OBJ_ATTRIBUTES];
+
+/* Parse a .c6xabi_attribute directive.  */
+
+static void
+s_tic6x_c6xabi_attribute (int ignored ATTRIBUTE_UNUSED)
+{
+  int tag = s_vendor_attribute (OBJ_ATTR_PROC);
+
+  if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
+    tic6x_attributes_set_explicitly[tag] = TRUE;
+}
+
+typedef struct
+{
+  const char *name;
+  int tag;
+} tic6x_attribute_table;
+
+static const tic6x_attribute_table tic6x_attributes[] =
+  {
+#define TAG(tag, value) { #tag, tag },
+#include "elf/tic6x-attrs.h"
+#undef TAG
+  };
+
+/* Convert an attribute name to a number.  */
+
+int
+tic6x_convert_symbolic_attribute (const char *name)
+{
+  unsigned int i;
+
+  for (i = 0; i < ARRAY_SIZE (tic6x_attributes); i++)
+    if (strcmp (name, tic6x_attributes[i].name) == 0)
+      return tic6x_attributes[i].tag;
+
+  return -1;
+}
+
 const pseudo_typeS md_pseudo_table[] =
   {
     { "arch", s_tic6x_arch, 0 },
-    { "atomic", s_tic6x_atomic, 0 },
-    { "noatomic", s_tic6x_noatomic, 0 },
+    { "c6xabi_attribute", s_tic6x_c6xabi_attribute, 0 },
     { "nocmp", s_tic6x_nocmp, 0 },
+    { "scomm", s_tic6x_scomm, 0 },
     { "word", cons, 4 },
+    { "ehtype", s_tic6x_ehtype, 0 },
+    { "endp", s_tic6x_endp, 0 },
+    { "handlerdata", s_tic6x_handlerdata, 0 },
+    { "personalityindex", s_tic6x_personalityindex, 0 },
+    { "personality", s_tic6x_personality, 0 },
+    { "cantunwind", s_tic6x_cantunwind, 0 },
     { 0, 0, 0 }
   };
 
@@ -304,6 +753,9 @@ void
 md_begin (void)
 {
   tic6x_opcode_id id;
+  flagword applicable;
+  segT seg;
+  subsegT subseg;
 
   bfd_set_arch_mach (stdoutput, TARGET_ARCH, 0);
 
@@ -320,6 +772,32 @@ md_begin (void)
          != NULL)
        as_fatal ("%s", _(errmsg));
     }
+
+  /* Save the current subseg so we can restore it [it's the default one and
+     we don't want the initial section to be .sbss].  */
+  seg = now_seg;
+  subseg = now_subseg;
+
+  /* The sbss section is for local .scomm symbols.  */
+  sbss_section = subseg_new (".bss", 0);
+  seg_info (sbss_section)->bss = 1;
+
+  /* This is copied from perform_an_assembly_pass.  */
+  applicable = bfd_applicable_section_flags (stdoutput);
+  bfd_set_section_flags (stdoutput, sbss_section, applicable & SEC_ALLOC);
+
+  subseg_set (seg, subseg);
+
+  /* We must construct a fake section similar to bfd_com_section
+     but with the name .scommon.  */
+  scom_section                = bfd_com_section;
+  scom_section.name           = ".scommon";
+  scom_section.output_section = & scom_section;
+  scom_section.symbol         = & scom_symbol;
+  scom_section.symbol_ptr_ptr = & scom_section.symbol;
+  scom_symbol                 = * bfd_com_section.symbol;
+  scom_symbol.name            = ".scommon";
+  scom_symbol.section         = & scom_section;
 }
 
 /* Whether the current line being parsed had the "||" parallel bars.  */
@@ -453,8 +931,11 @@ tic6x_unrecognized_line (int c)
 /* Do any target-specific handling of a label required.  */
 
 void
-tic6x_frob_label (symbolS *sym ATTRIBUTE_UNUSED)
+tic6x_frob_label (symbolS *sym)
 {
+  segment_info_type *si;
+  tic6x_label_list *list;
+
   if (tic6x_line_parallel)
     {
       as_bad (_("label after '||'"));
@@ -468,7 +949,11 @@ tic6x_frob_label (symbolS *sym ATTRIBUTE_UNUSED)
       tic6x_line_z = 0;
     }
 
-  seg_info (now_seg)->tc_segment_info_data.seen_label = TRUE;
+  si = seg_info (now_seg);
+  list = si->tc_segment_info_data.label_list;
+  si->tc_segment_info_data.label_list = xmalloc (sizeof (tic6x_label_list));
+  si->tc_segment_info_data.label_list->next = list;
+  si->tc_segment_info_data.label_list->label = sym;
 
   /* Defining tc_frob_label overrides the ELF definition of
      obj_frob_label, so we need to apply its effects here.  */
@@ -503,8 +988,9 @@ tic6x_start_line_hook (void)
   tic6x_end_of_line ();
 }
 
-/* Do target-specific handling immediately after all input files have
-   been read.  */
+/* Do target-specific handling immediately after an input file from
+   the command line, and any other inputs it includes, have been
+   read.  */
 
 void
 tic6x_cleanup (void)
@@ -512,6 +998,29 @@ tic6x_cleanup (void)
   tic6x_end_of_line ();
 }
 
+/* Do target-specific initialization after arguments have been
+   processed and the output file created.  */
+
+void
+tic6x_init_after_args (void)
+{
+  elf32_tic6x_set_use_rela_p (stdoutput, tic6x_generate_rela);
+}
+
+/* Free LIST of labels (possibly NULL).  */
+
+static void
+tic6x_free_label_list (tic6x_label_list *list)
+{
+  while (list)
+    {
+      tic6x_label_list *old = list;
+
+      list = list->next;
+      free (old);
+    }
+}
+
 /* Handle a data alignment of N bytes.  */
 
 void
@@ -521,10 +1030,62 @@ tic6x_cons_align (int n ATTRIBUTE_UNUSED)
 
   /* Data means there is no current execute packet, and that any label
      applies to that data rather than a subsequent instruction.  */
-  seginfo->tc_segment_info_data.num_execute_packet_insns = 0;
-  seginfo->tc_segment_info_data.seen_label = FALSE;
+  tic6x_free_label_list (seginfo->tc_segment_info_data.label_list);
+  seginfo->tc_segment_info_data.label_list = NULL;
+  seginfo->tc_segment_info_data.execute_packet_frag = NULL;
   seginfo->tc_segment_info_data.last_insn_lsb = NULL;
   seginfo->tc_segment_info_data.spmask_addr = NULL;
+  seginfo->tc_segment_info_data.func_units_used = 0;
+}
+
+/* Handle an alignment directive.  Return TRUE if the
+   machine-independent frag generation should be skipped.  */
+
+bfd_boolean
+tic6x_do_align (int n, char *fill, int len ATTRIBUTE_UNUSED, int max)
+{
+  /* Given code alignments of 4, 8, 16 or 32 bytes, we try to handle
+     them in the md_end pass by inserting NOPs in parallel with
+     previous instructions.  We only do this in sections containing
+     nothing but instructions.  Code alignments of 1 or 2 bytes have
+     no effect in such sections (but we record them with
+     machine-dependent frags anyway so they can be skipped or
+     converted to machine-independent), while those of more than 64
+     bytes cannot reliably be handled in this way.  */
+  if (n > 0
+      && max >= 0
+      && max < (1 << n)
+      && !need_pass_2
+      && fill == NULL
+      && subseg_text_p (now_seg))
+    {
+      fragS *align_frag;
+      char *p;
+
+      if (n > 5)
+       return FALSE;
+
+      /* Machine-independent code would generate a frag here, but we
+        wish to handle it in a machine-dependent way.  */
+      if (frag_now_fix () != 0)
+       {
+         if (frag_now->fr_type != rs_machine_dependent)
+           frag_wane (frag_now);
+
+         frag_new (0);
+       }
+      frag_grow (32);
+      align_frag = frag_now;
+      p = frag_var (rs_machine_dependent, 32, 32, max, NULL, n, NULL);
+      /* This must be the same as the frag to which a pointer was just
+        saved.  */
+      if (p != align_frag->fr_literal)
+       abort ();
+      align_frag->tc_frag_data.is_insns = FALSE;
+      return TRUE;
+    }
+  else
+    return FALSE;
 }
 
 /* Types of operand for parsing purposes.  These are used as bit-masks
@@ -1177,6 +1738,8 @@ static const tic6x_operator_table tic6x_operators[] = {
   { "dpr_hword", O_dpr_hword },
 #define O_dpr_word O_md6
   { "dpr_word", O_dpr_word },
+#define O_pcr_offset O_md7
+  { "pcr_offset", O_pcr_offset }
 };
 
 /* Parse a name in some machine-specific way.  Used on C6X to handle
@@ -1191,7 +1754,7 @@ tic6x_parse_name (const char *name, expressionS *exprP,
   const char *inner_name;
   unsigned int i;
   operatorT op = O_illegal;
-  symbolS *sym;
+  symbolS *sym, *op_sym = NULL;
 
   if (*name != '$')
     return 0;
@@ -1230,15 +1793,46 @@ tic6x_parse_name (const char *name, expressionS *exprP,
   name_end = p;
   skip_whitespace (p);
 
-  if (*p != ')')
+  if (op == O_pcr_offset)
     {
-      *input_line_pointer = 0;
-      return 0;
-    }
+      char *op_name_start, *op_name_end;
 
-  input_line_pointer = p + 1;
-  *nextchar = *input_line_pointer;
-  *input_line_pointer = 0;
+      if (*p != ',')
+       {
+         *input_line_pointer = 0;
+         return 0;
+       }
+      p++;
+      skip_whitespace (p);
+
+      if (!is_name_beginner (*p))
+       {
+         *input_line_pointer = 0;
+         return 0;
+       }
+
+      op_name_start = p;
+      p++;
+      while (is_part_of_name (*p))
+       p++;
+      op_name_end = p;
+      skip_whitespace (p);
+
+      c = *op_name_end;
+      *op_name_end = 0;
+      op_sym = symbol_find_or_make (op_name_start);
+      *op_name_end = c;
+    }
+
+  if (*p != ')')
+    {
+      *input_line_pointer = 0;
+      return 0;
+    }
+
+  input_line_pointer = p + 1;
+  *nextchar = *input_line_pointer;
+  *input_line_pointer = 0;
 
   c = *name_end;
   *name_end = 0;
@@ -1254,7 +1848,7 @@ tic6x_parse_name (const char *name, expressionS *exprP,
   exprP->X_op = op;
   exprP->X_add_symbol = sym;
   exprP->X_add_number = 0;
-  exprP->X_op_symbol = NULL;
+  exprP->X_op_symbol = op_sym;
   exprP->X_md = 0;
 
   return 1;
@@ -1272,6 +1866,7 @@ tic6x_fix_new_exp (fragS *frag, int where, int size, expressionS *exp,
                   bfd_boolean fix_adda)
 {
   bfd_reloc_code_real_type new_reloc = BFD_RELOC_UNUSED;
+  symbolS *subsy = NULL;
   fixS *fix;
 
   switch (exp->X_op)
@@ -1374,6 +1969,25 @@ tic6x_fix_new_exp (fragS *frag, int where, int size, expressionS *exp,
        }
       break;
 
+    case O_pcr_offset:
+      subsy = exp->X_op_symbol;
+      switch (r_type)
+       {
+       case BFD_RELOC_C6000_ABS_S16:
+       case BFD_RELOC_C6000_ABS_L16:
+         new_reloc = BFD_RELOC_C6000_PCR_L16;
+         break;
+
+       case BFD_RELOC_C6000_ABS_H16:
+         new_reloc = BFD_RELOC_C6000_PCR_H16;
+         break;
+
+       default:
+         as_bad (_("$PCR_OFFSET not supported in this context"));
+         return;
+       }
+      break;
+
     case O_symbol:
       break;
 
@@ -1391,6 +2005,7 @@ tic6x_fix_new_exp (fragS *frag, int where, int size, expressionS *exp,
   else
     fix = fix_new (frag, where, size, exp->X_add_symbol, exp->X_add_number,
                   pcrel, new_reloc);
+  fix->tc_fix_data.fix_subsy = subsy;
   fix->tc_fix_data.fix_adda = fix_adda;
 }
 
@@ -1430,6 +2045,34 @@ void
 tic6x_init_fix_data (fixS *fixP)
 {
   fixP->tc_fix_data.fix_adda = FALSE;
+  fixP->tc_fix_data.fix_subsy = NULL;
+}
+
+/* Return true if the fix can be handled by GAS, false if it must
+   be passed through to the linker.  */
+
+bfd_boolean
+tic6x_fix_adjustable (fixS *fixP)
+{
+  switch (fixP->fx_r_type)
+    {
+      /* Adjust_reloc_syms doesn't know about the GOT.  */
+    case BFD_RELOC_C6000_SBR_GOT_U15_W:
+    case BFD_RELOC_C6000_SBR_GOT_H16_W:
+    case BFD_RELOC_C6000_SBR_GOT_L16_W:
+    case BFD_RELOC_C6000_EHTYPE:
+      return 0;
+
+    case BFD_RELOC_C6000_PREL31:
+      return 0;
+
+    case BFD_RELOC_C6000_PCR_H16:
+    case BFD_RELOC_C6000_PCR_L16:
+      return 0;
+      
+    default:
+      return 1;
+    }
 }
 
 /* Given the fine-grained form of an operand, return the coarse
@@ -2325,6 +2968,7 @@ tic6x_try_encode (tic6x_opcode_id id, tic6x_operand *operands,
 
          if (opct->variable_fields[fld].coding_method == tic6x_coding_fstg)
            {
+             int i, t;
              if (operands[opno].value.exp.X_add_number < 0
                  || (operands[opno].value.exp.X_add_number
                      >= (1 << (fldd->width - fcyc_bits))))
@@ -2335,7 +2979,13 @@ tic6x_try_encode (tic6x_opcode_id id, tic6x_operand *operands,
                  *ok = FALSE;
                  return 0;
                }
-             value = operands[opno].value.exp.X_add_number << fcyc_bits;
+             value = operands[opno].value.exp.X_add_number;
+             for (t = 0, i = fcyc_bits; i < fldd->width; i++)
+               {
+                 t = (t << 1) | (value & 1);
+                 value >>= 1;
+               }
+             value = t << fcyc_bits;
            }
          else
            {
@@ -2453,7 +3103,7 @@ md_assemble (char *str)
   bfd_boolean this_line_spmask;
   unsigned int this_line_creg;
   unsigned int this_line_z;
-  bfd_boolean this_insn_label;
+  tic6x_label_list *this_insn_label_list;
   segment_info_type *seginfo;
   tic6x_opcode_list *opc_list, *opc;
   tic6x_func_unit_base func_unit_base = tic6x_func_unit_nfu;
@@ -2481,6 +3131,7 @@ md_assemble (char *str)
   int fix_pcrel = 0;
   bfd_reloc_code_real_type fx_r_type = BFD_RELOC_UNUSED;
   bfd_boolean fix_adda = FALSE;
+  fragS *insn_frag;
   char *output;
 
   p = str;
@@ -2492,6 +3143,16 @@ md_assemble (char *str)
   if (p == str)
     abort ();
 
+  /* Now an instruction has been seen, architecture attributes from
+     .arch directives merge with rather than overriding the previous
+     value.  */
+  tic6x_seen_insns = TRUE;
+  /* If no .arch directives or -march options have been seen, we are
+     assessing instruction validity based on the C674X default, so set
+     the attribute accordingly.  */
+  if (tic6x_arch_attribute == C6XABI_Tag_ISA_none)
+    tic6x_arch_attribute = C6XABI_Tag_ISA_C674X;
+
   /* Reset global settings for parallel bars and predicates now to
      avoid extra errors if there are problems with this opcode.  */
   this_line_parallel = tic6x_line_parallel;
@@ -2503,8 +3164,8 @@ md_assemble (char *str)
   tic6x_line_creg = 0;
   tic6x_line_z = 0;
   seginfo = seg_info (now_seg);
-  this_insn_label = seginfo->tc_segment_info_data.seen_label;
-  seginfo->tc_segment_info_data.seen_label = FALSE;
+  this_insn_label_list = seginfo->tc_segment_info_data.label_list;
+  seginfo->tc_segment_info_data.label_list = NULL;
 
   opc_list = hash_find_n (opcode_hash, str, p - str);
   if (opc_list == NULL)
@@ -2958,19 +3619,20 @@ md_assemble (char *str)
 
   if (this_line_parallel)
     {
-      if (seginfo->tc_segment_info_data.num_execute_packet_insns == 0)
+      insn_frag = seginfo->tc_segment_info_data.execute_packet_frag;
+      if (insn_frag == NULL)
        {
          as_bad (_("parallel instruction not following another instruction"));
          return;
        }
 
-      if (seginfo->tc_segment_info_data.num_execute_packet_insns >= 8)
+      if (insn_frag->fr_fix >= 32)
        {
          as_bad (_("too many instructions in execute packet"));
          return;
        }
 
-      if (this_insn_label)
+      if (this_insn_label_list != NULL)
        as_bad (_("label not at start of execute packet"));
 
       if (opct->flags & TIC6X_FLAG_FIRST)
@@ -2978,11 +3640,53 @@ md_assemble (char *str)
                opc_len, str);
 
       *seginfo->tc_segment_info_data.last_insn_lsb |= 0x1;
+      output = insn_frag->fr_literal + insn_frag->fr_fix;
     }
   else
     {
-      seginfo->tc_segment_info_data.num_execute_packet_insns = 0;
+      tic6x_label_list *l;
+
       seginfo->tc_segment_info_data.spmask_addr = NULL;
+      seginfo->tc_segment_info_data.func_units_used = 0;
+
+      /* Start a new frag for this execute packet.  */
+      if (frag_now_fix () != 0)
+       {
+         if (frag_now->fr_type != rs_machine_dependent)
+           frag_wane (frag_now);
+
+         frag_new (0);
+       }
+      frag_grow (32);
+      insn_frag = seginfo->tc_segment_info_data.execute_packet_frag = frag_now;
+      for (l = this_insn_label_list; l; l = l->next)
+       {
+         symbol_set_frag (l->label, frag_now);
+         S_SET_VALUE (l->label, 0);
+         S_SET_SEGMENT (l->label, now_seg);
+       }
+      tic6x_free_label_list (this_insn_label_list);
+      dwarf2_emit_insn (0);
+      output = frag_var (rs_machine_dependent, 32, 32, 0, NULL, 0, NULL);
+      /* This must be the same as the frag to which a pointer was just
+        saved.  */
+      if (output != insn_frag->fr_literal)
+       abort ();
+      insn_frag->tc_frag_data.is_insns = TRUE;
+      insn_frag->tc_frag_data.can_cross_fp_boundary
+       = tic6x_can_cross_fp_boundary;
+    }
+
+  if (func_unit_base != tic6x_func_unit_nfu)
+    {
+      unsigned int func_unit_enc;
+
+      func_unit_enc = tic6x_encode_spmask (func_unit_base, func_unit_side);
+
+      if (seginfo->tc_segment_info_data.func_units_used & func_unit_enc)
+       as_bad (_("functional unit already used in this execute packet"));
+
+      seginfo->tc_segment_info_data.func_units_used |= func_unit_enc;
     }
 
   if (opct->flags & TIC6X_FLAG_SPLOOP)
@@ -3029,17 +3733,16 @@ md_assemble (char *str)
     }
 
   record_alignment (now_seg, 5);
-  output = frag_more (4);
   md_number_to_chars (output, opcode_value, 4);
   if (fix_needed)
-    tic6x_fix_new_exp (frag_now, output - frag_now->fr_literal, 4, fix_exp,
+    tic6x_fix_new_exp (insn_frag, output - insn_frag->fr_literal, 4, fix_exp,
                       fix_pcrel, fx_r_type, fix_adda);
-  seginfo->tc_segment_info_data.num_execute_packet_insns++;
+  insn_frag->fr_fix += 4;
+  insn_frag->fr_var -= 4;
   seginfo->tc_segment_info_data.last_insn_lsb
     = (target_big_endian ? output + 3 : output);
   if (opct->flags & TIC6X_FLAG_SPMASK)
     seginfo->tc_segment_info_data.spmask_addr = output;
-  dwarf2_emit_insn (4);
 }
 
 /* Modify NEWVAL (32-bit) by inserting VALUE, shifted right by SHIFT
@@ -3072,6 +3775,7 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
   switch (fixP->fx_r_type)
     {
     case BFD_RELOC_NONE:
+    case BFD_RELOC_C6000_EHTYPE:
       /* Force output to the object file.  */
       fixP->fx_done = 0;
       break;
@@ -3111,8 +3815,25 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
       if (fixP->fx_done || !seg->use_rela_p)
        {
          offsetT newval = md_chars_to_number (buf, 4);
+         int shift;
+
+         switch (fixP->fx_r_type)
+           {
+           case BFD_RELOC_C6000_SBR_L16_H:
+             shift = 1;
+             break;
+
+           case BFD_RELOC_C6000_SBR_L16_W:
+           case BFD_RELOC_C6000_SBR_GOT_L16_W:
+             shift = 2;
+             break;
 
-         MODIFY_VALUE (newval, value, 0, 7, 16);
+           default:
+             shift = 0;
+             break;
+           }
+
+         MODIFY_VALUE (newval, value, shift, 7, 16);
          if ((value < -0x8000 || value > 0x7fff)
              && (fixP->fx_r_type == BFD_RELOC_C6000_ABS_S16
                  || fixP->fx_r_type == BFD_RELOC_C6000_SBR_S16))
@@ -3135,8 +3856,25 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
       if (fixP->fx_done || !seg->use_rela_p)
        {
          offsetT newval = md_chars_to_number (buf, 4);
+         int shift;
 
-         MODIFY_VALUE (newval, value, 16, 7, 16);
+         switch (fixP->fx_r_type)
+           {
+           case BFD_RELOC_C6000_SBR_H16_H:
+             shift = 17;
+             break;
+
+           case BFD_RELOC_C6000_SBR_H16_W:
+           case BFD_RELOC_C6000_SBR_GOT_H16_W:
+             shift = 18;
+             break;
+
+           default:
+             shift = 16;
+             break;
+           }
+
+         MODIFY_VALUE (newval, value, shift, 7, 16);
 
          md_number_to_chars (buf, newval, 4);
        }
@@ -3144,6 +3882,19 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
        abort ();
       break;
 
+    case BFD_RELOC_C6000_PCR_H16:
+    case BFD_RELOC_C6000_PCR_L16:
+      if (fixP->fx_done || !seg->use_rela_p)
+       {
+         offsetT newval = md_chars_to_number (buf, 4);
+         int shift = fixP->fx_r_type == BFD_RELOC_C6000_PCR_H16 ? 16 : 0;
+
+         MODIFY_VALUE (newval, value, shift, 7, 16);
+
+         md_number_to_chars (buf, newval, 4);
+       }
+      break;
+
     case BFD_RELOC_C6000_SBR_U15_B:
       if (fixP->fx_done || !seg->use_rela_p)
        {
@@ -3295,6 +4046,11 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
        }
       break;
 
+    case BFD_RELOC_C6000_PREL31:
+      /* Force output to the object file.  */
+      fixP->fx_done = 0;
+      break;
+
     default:
       abort ();
     }
@@ -3308,7 +4064,348 @@ md_atof (int type, char *litP, int *sizeP)
   return ieee_md_atof (type, litP, sizeP, target_big_endian);
 }
 
-/* No machine-dependent frags yet.  */
+/* Adjust the frags in SECTION (see tic6x_end).  */
+
+static void
+tic6x_adjust_section (bfd *abfd ATTRIBUTE_UNUSED, segT section,
+                     void *dummy ATTRIBUTE_UNUSED)
+{
+  segment_info_type *info;
+  frchainS *frchp;
+  fragS *fragp;
+  bfd_boolean have_code = FALSE;
+  bfd_boolean have_non_code = FALSE;
+
+  info = seg_info (section);
+  if (info == NULL)
+    return;
+
+  for (frchp = info->frchainP; frchp; frchp = frchp->frch_next)
+    for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next)
+      switch (fragp->fr_type)
+       {
+       case rs_machine_dependent:
+         if (fragp->tc_frag_data.is_insns)
+           have_code = TRUE;
+         break;
+
+       case rs_dummy:
+       case rs_fill:
+         if (fragp->fr_fix > 0)
+           have_non_code = TRUE;
+         break;
+
+       default:
+         have_non_code = TRUE;
+         break;
+       }
+
+  /* Process alignment requirements in a code-only section.  */
+  if (have_code && !have_non_code)
+    {
+      /* If we need to insert an odd number of instructions to meet an
+        alignment requirement, there must have been an odd number of
+        instructions since the last 8-byte-aligned execute packet
+        boundary.  So there must have been an execute packet with an
+        odd number (and so a number fewer than 8) of instructions
+        into which we can insert a NOP without breaking any previous
+        alignments.
+
+        If then we need to insert a number 2 mod 4 of instructions,
+        the number of instructions since the last 16-byte-aligned
+        execute packet boundary must be 2 mod 4.  So between that
+        boundary and the following 8-byte-aligned boundary there must
+        either be at least one execute packet with 2-mod-4
+        instructions, or at least two with an odd number of
+        instructions; again, greedily inserting NOPs as soon as
+        possible suffices to meet the alignment requirement.
+
+        If then we need to insert 4 instructions, we look between the
+        last 32-byte-aligned boundary and the following
+        16-byte-aligned boundary.  The sizes of the execute packets
+        in this range total 4 instructions mod 8, so again there is
+        room for greedy insertion of NOPs to meet the alignment
+        requirement, and before any intermediate point with 8-byte
+        (2-instruction) alignment requirement the sizes of execute
+        packets (and so the room for NOPs) will total 2 instructions
+        mod 4 so greedy insertion will not break such alignments.
+
+        So we can always meet these alignment requirements by
+        inserting NOPs in parallel with existing execute packets, and
+        by induction the approach described above inserts the minimum
+        number of such NOPs.  */
+
+      /* The number of NOPs we are currently looking to insert, if we
+        have gone back to insert NOPs.  */
+      unsigned int want_insert = 0;
+
+      /* Out of that number, the number inserted so far in the current
+        stage of the above algorithm.  */
+      unsigned int want_insert_done_so_far = 0;
+
+      /* The position mod 32 at the start of the current frag.  */
+      unsigned int pos = 0;
+
+      /* The locations in the frag chain of the most recent frags at
+        the start of which there is the given alignment.  */
+      frchainS *frchp_last32, *frchp_last16, *frchp_last8;
+      fragS *fragp_last32, *fragp_last16, *fragp_last8;
+      unsigned int pos_last32, pos_last16, pos_last8;
+
+      frchp_last32 = frchp_last16 = frchp_last8 = info->frchainP;
+      fragp_last32 = fragp_last16 = fragp_last8 = info->frchainP->frch_root;
+      pos_last32 = pos_last16 = pos_last8 = 0;
+
+      for (frchp = info->frchainP; frchp; frchp = frchp->frch_next)
+       for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next)
+       look_at_frag:
+         {
+           bfd_boolean go_back = FALSE;
+           frchainS *frchp_next;
+           fragS *fragp_next;
+
+           if (fragp->fr_type != rs_machine_dependent)
+             continue;
+
+           if (fragp->tc_frag_data.is_insns
+               && pos + fragp->fr_fix > 32
+               && !fragp->tc_frag_data.can_cross_fp_boundary)
+             {
+               /* As described above, we should always have met an
+                  alignment requirement by the time we come back to
+                  it.  */
+               if (want_insert)
+                 abort ();
+
+               if (pos & 3)
+                 abort ();
+               want_insert = (32 - pos) >> 2;
+               if (want_insert > 7)
+                 abort ();
+               want_insert_done_so_far = 0;
+               go_back = TRUE;
+             }
+
+           if (!fragp->tc_frag_data.is_insns)
+             {
+               unsigned int would_insert_bytes;
+
+               if (!(pos & ((1 << fragp->fr_offset) - 1)))
+                 /* This alignment requirement is already met.  */
+                 continue;
+
+               /* As described above, we should always have met an
+                  alignment requirement by the time we come back to
+                  it.  */
+               if (want_insert)
+                 abort ();
+
+               /* We may not be able to meet this requirement within
+                  the given number of characters.  */
+               would_insert_bytes
+                 = ((1 << fragp->fr_offset)
+                    - (pos & ((1 << fragp->fr_offset) - 1)));
+
+               if (fragp->fr_subtype != 0
+                   && would_insert_bytes > fragp->fr_subtype)
+                 continue;
+
+               /* An unmet alignment must be 8, 16 or 32 bytes;
+                  smaller ones must always be met within code-only
+                  sections and larger ones cause the section not to
+                  be code-only.  */
+               if (fragp->fr_offset != 3
+                   && fragp->fr_offset != 4
+                   && fragp->fr_offset != 5)
+                 abort ();
+
+               if (would_insert_bytes & 3)
+                 abort ();
+               want_insert = would_insert_bytes >> 2;
+               if (want_insert > 7)
+                 abort ();
+               want_insert_done_so_far = 0;
+               go_back = TRUE;
+             }
+           else if (want_insert && !go_back)
+             {
+               unsigned int num_insns = fragp->fr_fix >> 2;
+               unsigned int max_poss_nops = 8 - num_insns;
+
+               if (max_poss_nops)
+                 {
+                   unsigned int cur_want_nops, max_want_nops, do_nops, i;
+
+                   if (want_insert & 1)
+                     cur_want_nops = 1;
+                   else if (want_insert & 2)
+                     cur_want_nops = 2;
+                   else if (want_insert & 4)
+                     cur_want_nops = 4;
+                   else
+                     abort ();
+
+                   max_want_nops = cur_want_nops - want_insert_done_so_far;
+
+                   do_nops = (max_poss_nops < max_want_nops
+                              ? max_poss_nops
+                              : max_want_nops);
+                   for (i = 0; i < do_nops; i++)
+                     {
+                       md_number_to_chars (fragp->fr_literal + fragp->fr_fix,
+                                           0, 4);
+                       if (target_big_endian)
+                         fragp->fr_literal[fragp->fr_fix - 1] |= 0x1;
+                       else
+                         fragp->fr_literal[fragp->fr_fix - 4] |= 0x1;
+                       fragp->fr_fix += 4;
+                       fragp->fr_var -= 4;
+                     }
+                   want_insert_done_so_far += do_nops;
+                   if (want_insert_done_so_far == cur_want_nops)
+                     {
+                       want_insert -= want_insert_done_so_far;
+                       want_insert_done_so_far = 0;
+                       if (want_insert)
+                         go_back = TRUE;
+                     }
+                 }
+             }
+           if (go_back)
+             {
+               if (want_insert & 1)
+                 {
+                   frchp = frchp_last8;
+                   fragp = fragp_last8;
+                   pos = pos_last8;
+                 }
+               else if (want_insert & 2)
+                 {
+                   frchp = frchp_last8 = frchp_last16;
+                   fragp = fragp_last8 = fragp_last16;
+                   pos = pos_last8 = pos_last16;
+                 }
+               else if (want_insert & 4)
+                 {
+                   frchp = frchp_last8 = frchp_last16 = frchp_last32;
+                   fragp = fragp_last8 = fragp_last16 = fragp_last32;
+                   pos = pos_last8 = pos_last16 = pos_last32;
+                 }
+               else
+                 abort ();
+
+               goto look_at_frag;
+             }
+
+           /* Update current position for moving past a code
+              frag.  */
+           pos += fragp->fr_fix;
+           pos &= 31;
+           frchp_next = frchp;
+           fragp_next = fragp->fr_next;
+           if (fragp_next == NULL)
+             {
+               frchp_next = frchp->frch_next;
+               if (frchp_next != NULL)
+                 fragp_next = frchp_next->frch_root;
+             }
+           if (!(pos & 7))
+             {
+               frchp_last8 = frchp_next;
+               fragp_last8 = fragp_next;
+               pos_last8 = pos;
+             }
+           if (!(pos & 15))
+             {
+               frchp_last16 = frchp_next;
+               fragp_last16 = fragp_next;
+               pos_last16 = pos;
+             }
+           if (!(pos & 31))
+             {
+               frchp_last32 = frchp_next;
+               fragp_last32 = fragp_next;
+               pos_last32 = pos;
+             }
+         }
+    }
+
+  /* Now convert the machine-dependent frags to machine-independent
+     ones.  */
+  for (frchp = info->frchainP; frchp; frchp = frchp->frch_next)
+    for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next)
+      {
+       if (fragp->fr_type == rs_machine_dependent)
+         {
+           if (fragp->tc_frag_data.is_insns)
+             frag_wane (fragp);
+           else
+             {
+               fragp->fr_type = rs_align_code;
+               fragp->fr_var = 1;
+               *fragp->fr_literal = 0;
+             }
+         }
+      }
+}
+
+/* Initialize the machine-dependent parts of a frag.  */
+
+void
+tic6x_frag_init (fragS *fragp)
+{
+  fragp->tc_frag_data.is_insns = FALSE;
+  fragp->tc_frag_data.can_cross_fp_boundary = FALSE;
+}
+
+/* Set an attribute if it has not already been set by the user.  */
+
+static void
+tic6x_set_attribute_int (int tag, int value)
+{
+  if (tag < 1
+      || tag >= NUM_KNOWN_OBJ_ATTRIBUTES)
+    abort ();
+  if (!tic6x_attributes_set_explicitly[tag])
+    bfd_elf_add_proc_attr_int (stdoutput, tag, value);
+}
+
+/* Set object attributes deduced from the input file and command line
+   rather than given explicitly.  */
+static void
+tic6x_set_attributes (void)
+{
+  if (tic6x_arch_attribute == C6XABI_Tag_ISA_none)
+    tic6x_arch_attribute = C6XABI_Tag_ISA_C674X;
+
+  tic6x_set_attribute_int (Tag_ISA, tic6x_arch_attribute);
+  tic6x_set_attribute_int (Tag_ABI_DSBT, tic6x_dsbt);
+  tic6x_set_attribute_int (Tag_ABI_PID, tic6x_pid);
+  tic6x_set_attribute_int (Tag_ABI_PIC, tic6x_pic);
+}
+
+/* Do machine-dependent manipulations of the frag chains after all
+   input has been read and before the machine-independent sizing and
+   relaxing.  */
+
+void
+tic6x_end (void)
+{
+  /* Set object attributes at this point if not explicitly set.  */
+  tic6x_set_attributes ();
+
+  /* Meeting alignment requirements may require inserting NOPs in
+     parallel in execute packets earlier in the segment.  Future
+     16-bit instruction generation involves whole-segment optimization
+     to determine the best choice and ordering of 32-bit or 16-bit
+     instructions.  This doesn't fit will in the general relaxation
+     framework, so handle alignment and 16-bit instruction generation
+     here.  */
+  bfd_map_over_sections (stdoutput, tic6x_adjust_section, NULL);
+}
+
+/* No machine-dependent frags at this stage; all converted in
+   tic6x_end.  */
 
 void
 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
@@ -3317,7 +4414,8 @@ md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
   abort ();
 }
 
-/* No machine-dependent frags yet.  */
+/* No machine-dependent frags at this stage; all converted in
+   tic6x_end.  */
 
 int
 md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
@@ -3348,8 +4446,12 @@ md_operand (expressionS *op ATTRIBUTE_UNUSED)
    packet.  */
 
 long
-md_pcrel_from (fixS *fixp)
+tic6x_pcrel_from_section (fixS *fixp, segT sec)
 {
+  if (fixp->fx_addsy != NULL
+      && (!S_IS_DEFINED (fixp->fx_addsy)
+         || S_GET_SEGMENT (fixp->fx_addsy) != sec))
+    return 0;
   return (fixp->fx_where + fixp->fx_frag->fr_address) & ~(long) 0x1f;
 }
 
@@ -3380,13 +4482,15 @@ arelent *
 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
 {
   arelent *reloc;
+  asymbol *symbol;
   bfd_reloc_code_real_type r_type;
 
   reloc = xmalloc (sizeof (arelent));
   reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
-  *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
+  symbol = symbol_get_bfdsym (fixp->fx_addsy);
+  *reloc->sym_ptr_ptr = symbol;
   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
-  reloc->addend = fixp->fx_offset;
+  reloc->addend = (tic6x_generate_rela ? fixp->fx_offset : 0);
   r_type = fixp->fx_r_type;
   reloc->howto = bfd_reloc_type_lookup (stdoutput, r_type);
 
@@ -3398,5 +4502,852 @@ tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
       return NULL;
     }
 
+  /* Correct for adjustments bfd_install_relocation will make.  */
+  if (reloc->howto->pcrel_offset && reloc->howto->partial_inplace)
+    {
+      reloc->addend += reloc->address;
+      if (!bfd_is_com_section (symbol))
+       reloc->addend -= symbol->value;
+    }
+  if (r_type == BFD_RELOC_C6000_PCR_H16
+      || r_type == BFD_RELOC_C6000_PCR_L16)
+    {
+      symbolS *t = fixp->tc_fix_data.fix_subsy;
+      segT sub_symbol_segment;
+
+      resolve_symbol_value (t);
+      sub_symbol_segment = S_GET_SEGMENT (t);
+      if (sub_symbol_segment == undefined_section)
+       as_bad_where (fixp->fx_file, fixp->fx_line,
+                     _("undefined symbol %s in PCR relocation"),
+                     S_GET_NAME (t));
+      else
+       {
+         reloc->addend = reloc->address & ~0x1F;
+         reloc->addend -= S_GET_VALUE (t);
+       }
+    }
   return reloc;
 }
+
+/* Convert REGNAME to a DWARF-2 register number.  */
+
+int
+tic6x_regname_to_dw2regnum (char *regname)
+{
+  bfd_boolean reg_ok;
+  tic6x_register reg;
+  char *rq = regname;
+
+  reg_ok = tic6x_parse_register (&rq, &reg);
+
+  if (!reg_ok)
+    return -1;
+
+  switch (reg.side)
+    {
+    case 1: /* A regs.  */
+      if (reg.num < 16)
+       return reg.num;
+      else if (reg.num < 32)
+       return (reg.num - 16) + 37;
+      else
+       return -1;
+
+    case 2: /* B regs.  */
+      if (reg.num < 16)
+       return reg.num + 16;
+      else if (reg.num < 32)
+       return (reg.num - 16) + 53;
+      else
+       return -1;
+
+    default:
+      return -1;
+    }
+}
+
+/* Initialize the DWARF-2 unwind information for this procedure.  */
+
+void
+tic6x_frame_initial_instructions (void)
+{
+  /* CFA is initial stack pointer (B15).  */
+  cfi_add_CFA_def_cfa (31, 0);
+}
+
+/* Start an exception table entry.  If idx is nonzero this is an index table
+   entry.  */
+
+static void
+tic6x_start_unwind_section (const segT text_seg, int idx)
+{
+  tic6x_unwind_info *unwind = tic6x_get_unwind ();
+  const char * text_name;
+  const char * prefix;
+  const char * prefix_once;
+  const char * group_name;
+  size_t prefix_len;
+  size_t text_len;
+  char * sec_name;
+  size_t sec_name_len;
+  int type;
+  int flags;
+  int linkonce;
+
+  if (idx)
+    {
+      prefix = ELF_STRING_C6000_unwind;
+      prefix_once = ELF_STRING_C6000_unwind_once;
+      type = SHT_C6000_UNWIND;
+    }
+  else
+    {
+      prefix = ELF_STRING_C6000_unwind_info;
+      prefix_once = ELF_STRING_C6000_unwind_info_once;
+      type = SHT_PROGBITS;
+    }
+
+  text_name = segment_name (text_seg);
+  if (streq (text_name, ".text"))
+    text_name = "";
+
+  if (strncmp (text_name, ".gnu.linkonce.t.",
+              strlen (".gnu.linkonce.t.")) == 0)
+    {
+      prefix = prefix_once;
+      text_name += strlen (".gnu.linkonce.t.");
+    }
+
+  prefix_len = strlen (prefix);
+  text_len = strlen (text_name);
+  sec_name_len = prefix_len + text_len;
+  sec_name = (char *) xmalloc (sec_name_len + 1);
+  memcpy (sec_name, prefix, prefix_len);
+  memcpy (sec_name + prefix_len, text_name, text_len);
+  sec_name[prefix_len + text_len] = '\0';
+
+  flags = SHF_ALLOC;
+  linkonce = 0;
+  group_name = 0;
+
+  /* Handle COMDAT group.  */
+  if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
+    {
+      group_name = elf_group_name (text_seg);
+      if (group_name == NULL)
+       {
+         as_bad (_("group section `%s' has no group signature"),
+                 segment_name (text_seg));
+         ignore_rest_of_line ();
+         return;
+       }
+      flags |= SHF_GROUP;
+      linkonce = 1;
+    }
+
+  obj_elf_change_section (sec_name, type, flags, 0, group_name, linkonce, 0);
+
+  /* Set the section link for index tables.  */
+  if (idx)
+    elf_linked_to_section (now_seg) = text_seg;
+
+  seg_info (now_seg)->tc_segment_info_data.text_unwind = unwind;
+}
+
+
+static const int
+tic6x_unwind_frame_regs[TIC6X_NUM_UNWIND_REGS] = 
+/* A15 B15 B14 B13 B12 B11 B10  B3 A14 A13 A12 A11 A10.  */
+  { 15, 31, 30, 29, 28, 27, 26, 19, 14, 13, 12, 11, 10 };
+
+/* Register save offsets for __c6xabi_push_rts.  */
+static const int
+tic6x_pop_rts_offset_little[TIC6X_NUM_UNWIND_REGS] = 
+/* A15 B15 B14 B13 B12 B11 B10  B3 A14 A13 A12 A11 A10.  */
+  { -1,  1,  0, -3, -4, -7, -8,-11, -2, -5, -6, -9,-10};
+
+static const int
+tic6x_pop_rts_offset_big[TIC6X_NUM_UNWIND_REGS] = 
+/* A15 B15 B14 B13 B12 B11 B10  B3 A14 A13 A12 A11 A10.  */
+  { -2,  1,  0, -4, -3, -8, -7,-12, -1, -6, -5,-10, -9};
+
+/* Map from dwarf register number to unwind frame register number.  */
+static int
+tic6x_unwind_reg_from_dwarf (int dwarf)
+{
+  int reg;
+
+  for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
+    {
+      if (tic6x_unwind_frame_regs[reg] == dwarf)
+       return reg;
+    }
+
+  return -1;
+}
+
+/* Unwinding bytecode definitions.  */
+#define UNWIND_OP_ADD_SP  0x00
+#define UNWIND_OP_ADD_SP2 0xd2
+#define UNWIND_OP2_POP 0x8000
+#define UNWIND_OP2_POP_COMPACT 0xa000
+#define UNWIND_OP_POP_REG 0xc0
+#define UNWIND_OP_MV_FP 0xd0
+#define UNWIND_OP_POP_RTS 0xd1
+#define UNWIND_OP_RET 0xe0
+
+/* Maximum stack adjustment for __c6xabi_unwind_cpp_pr3/4 */
+#define MAX_COMPACT_SP_OFFSET (0x7f << 3)
+
+static void
+tic6x_flush_unwind_word (valueT data)
+{
+  tic6x_unwind_info *unwind = tic6x_get_unwind ();
+  char *ptr;
+
+  /* Create EXTAB entry if it does not exist.  */
+  if (unwind->table_entry == NULL)
+    {
+      tic6x_start_unwind_section (unwind->saved_seg, 0);
+      frag_align (2, 0, 0);
+      record_alignment (now_seg, 2);
+      unwind->table_entry = expr_build_dot ();
+      ptr = frag_more (4);
+      unwind->frag_start = ptr;
+    }
+  else
+    {
+      /* Append additional word of data.  */
+      ptr = frag_more (4);
+    }
+
+  md_number_to_chars (ptr, data, 4);
+}
+
+/* Add a single byte of unwinding data.  */
+
+static void
+tic6x_unwind_byte (int byte)
+{
+  tic6x_unwind_info *unwind = tic6x_get_unwind ();
+
+  unwind->data_bytes++;
+  /* Only flush the first word after we know multiple words are required.  */
+  if (unwind->data_bytes == 5)
+    {
+      if (unwind->personality_index == -1)
+       {
+         /* At this point we know we are too big for pr0.  */
+         unwind->personality_index = 1;
+         tic6x_flush_unwind_word (0x81000000 | ((unwind->data >> 8) & 0xffff));
+         unwind->data = ((unwind->data & 0xff) << 8) | byte;
+         unwind->data_bytes++;
+       }
+      else
+       {
+         tic6x_flush_unwind_word (unwind->data);
+         unwind->data = byte;
+       }
+    }
+  else
+    {
+      unwind->data = (unwind->data << 8) | byte;
+      if ((unwind->data_bytes & 3) == 0 && unwind->data_bytes > 4)
+       {
+         tic6x_flush_unwind_word (unwind->data);
+         unwind->data = 0;
+       }
+    }
+}
+
+/* Add a two-byte unwinding opcode.  */
+static void
+tic6x_unwind_2byte (int bytes)
+{
+  tic6x_unwind_byte (bytes >> 8);
+  tic6x_unwind_byte (bytes & 0xff);
+}
+
+static void
+tic6x_unwind_uleb (offsetT offset)
+{
+  while (offset > 0x7f)
+    {
+      tic6x_unwind_byte ((offset & 0x7f) | 0x80);
+      offset >>= 7;
+    }
+  tic6x_unwind_byte (offset);
+}
+
+void
+tic6x_cfi_startproc (void)
+{
+  tic6x_unwind_info *unwind = tic6x_get_unwind ();
+
+  unwind->personality_index = -1;
+  unwind->personality_routine = NULL;
+  if (unwind->table_entry)
+    as_bad (_("missing .endp before .cfi_startproc"));
+
+  unwind->table_entry = NULL;
+  unwind->data_bytes = -1;
+}
+
+static void
+tic6x_output_exidx_entry (void)
+{
+  char *ptr;
+  long where;
+  unsigned int marked_pr_dependency;
+  segT old_seg;
+  subsegT old_subseg;
+  tic6x_unwind_info *unwind = tic6x_get_unwind ();
+
+  old_seg = now_seg;
+  old_subseg = now_subseg;
+
+  /* Add index table entry.  This is two words.         */
+  tic6x_start_unwind_section (unwind->saved_seg, 1);
+  frag_align (2, 0, 0);
+  record_alignment (now_seg, 2);
+
+  ptr = frag_more (8);
+  where = frag_now_fix () - 8;
+
+  /* Self relative offset of the function start.  */
+  fix_new (frag_now, where, 4, unwind->function_start, 0, 1,
+          BFD_RELOC_C6000_PREL31);
+
+  /* Indicate dependency on ABI-defined personality routines to the
+     linker, if it hasn't been done already.  */
+  marked_pr_dependency
+    = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
+  if (unwind->personality_index >= 0 && unwind->personality_index < 5
+      && !(marked_pr_dependency & (1 << unwind->personality_index)))
+    {
+      static const char *const name[] =
+       {
+         "__c6xabi_unwind_cpp_pr0",
+         "__c6xabi_unwind_cpp_pr1",
+         "__c6xabi_unwind_cpp_pr2",
+         "__c6xabi_unwind_cpp_pr3",
+         "__c6xabi_unwind_cpp_pr4"
+       };
+      symbolS *pr = symbol_find_or_make (name[unwind->personality_index]);
+      fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
+      seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
+       |= 1 << unwind->personality_index;
+    }
+
+  if (unwind->table_entry)
+    {
+      /* Self relative offset of the table entry.       */
+      fix_new (frag_now, where + 4, 4, unwind->table_entry, 0, 1,
+              BFD_RELOC_C6000_PREL31);
+    }
+  else
+    {
+      /* Inline exception table entry.  */
+      md_number_to_chars (ptr + 4, unwind->data, 4);
+    }
+
+  /* Restore the original section.  */
+  subseg_set (old_seg, old_subseg);
+}
+
+static void
+tic6x_output_unwinding (bfd_boolean need_extab)
+{
+  tic6x_unwind_info *unwind = tic6x_get_unwind ();
+  unsigned safe_mask = unwind->safe_mask;
+  unsigned compact_mask = unwind->compact_mask;
+  unsigned reg_saved_mask = unwind->reg_saved_mask;
+  offsetT cfa_offset = unwind->cfa_offset;
+  long where;
+  int reg;
+
+  if (unwind->personality_index == -2)
+    {
+      /* Function can not be unwound.  */
+      unwind->data = 1;
+      tic6x_output_exidx_entry ();
+      return;
+    }
+
+  if (unwind->personality_index == -1 && unwind->personality_routine == NULL)
+    {
+      /* Auto-select a personality routine if none specified.  */
+      if (reg_saved_mask || cfa_offset >= MAX_COMPACT_SP_OFFSET)
+       unwind->personality_index = -1;
+      else if (safe_mask)
+       unwind->personality_index = 3;
+      else
+       unwind->personality_index = 4;
+    }
+
+  /* Calculate unwinding opcodes, and emit to EXTAB if necessary.  */
+  unwind->table_entry = NULL;
+  if (unwind->personality_index == 3 || unwind->personality_index == 4)
+    {
+      if (cfa_offset >= MAX_COMPACT_SP_OFFSET)
+       {
+         as_bad (_("stack pointer offset too large for personality routine"));
+         return;
+       }
+      if (reg_saved_mask
+         || (unwind->personality_index == 3 && compact_mask != 0)
+         || (unwind->personality_index == 4 && safe_mask != 0))
+       {
+         as_bad (_("stack frame layout does not match personality routine"));
+         return;
+       }
+
+      unwind->data = (1u << 31) | (unwind->personality_index << 24);
+      if (unwind->cfa_reg == 15)
+       unwind->data |= 0x7f << 17;
+      else
+       unwind->data |= cfa_offset << (17 - 3);
+
+      if (unwind->personality_index == 3)
+       unwind->data |= safe_mask << 4;
+      else
+       unwind->data |= compact_mask << 4;
+      unwind->data |= unwind->return_reg;
+      unwind->data_bytes = 4;
+    }
+  else
+    {
+      if (unwind->personality_routine)
+       {
+         unwind->data = 0;
+         unwind->data_bytes = 5;
+         tic6x_flush_unwind_word (0);
+         /* First word is personality routine.  */
+         where = frag_now_fix () - 4;
+         fix_new (frag_now, where, 4, unwind->personality_routine, 0, 1,
+                  BFD_RELOC_C6000_PREL31);
+       }
+      else if (unwind->personality_index > 0)
+       {
+         unwind->data = 0x8000 | (unwind->personality_index << 8);
+         unwind->data_bytes = 2;
+       }
+      else /* pr0 or undecided */
+       {
+         unwind->data = 0x80;
+         unwind->data_bytes = 1;
+       }
+
+      if (unwind->return_reg != UNWIND_B3)
+       {
+         tic6x_unwind_byte (UNWIND_OP_RET | unwind->return_reg);
+       }
+
+      if (unwind->cfa_reg == 15)
+       {
+         tic6x_unwind_byte (UNWIND_OP_MV_FP);
+       }
+      else if (cfa_offset != 0)
+       {
+         cfa_offset >>= 3;
+         if (cfa_offset > 0x80)
+           {
+             tic6x_unwind_byte (UNWIND_OP_ADD_SP2);
+             tic6x_unwind_uleb (cfa_offset - 0x81);
+           }
+         else if (cfa_offset > 0x40)
+           {
+             tic6x_unwind_byte (UNWIND_OP_ADD_SP | 0x3f);
+             tic6x_unwind_byte (UNWIND_OP_ADD_SP | (cfa_offset - 0x40));
+           }
+         else
+           {
+             tic6x_unwind_byte (UNWIND_OP_ADD_SP | (cfa_offset - 1));
+           }
+       }
+
+      if (safe_mask)
+       tic6x_unwind_2byte (UNWIND_OP2_POP | unwind->safe_mask);
+      else if (unwind->pop_rts)
+       tic6x_unwind_byte (UNWIND_OP_POP_RTS);
+      else if (compact_mask)
+       tic6x_unwind_2byte (UNWIND_OP2_POP_COMPACT | unwind->compact_mask);
+      else if (reg_saved_mask)
+       {
+         offsetT cur_offset;
+         int val;
+         int last_val;
+
+         tic6x_unwind_byte (UNWIND_OP_POP_REG | unwind->saved_reg_count);
+         last_val = 0;
+         for (cur_offset = 0; unwind->saved_reg_count > 0; cur_offset -= 4)
+           {
+             val = 0xf;
+             for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
+               {
+                 if (!unwind->reg_saved[reg])
+                   continue;
+
+                 if (unwind->reg_offset[reg] == cur_offset)
+                   {
+                     unwind->saved_reg_count--;
+                     val = reg;
+                     break;
+                   }
+               }
+             if ((cur_offset & 4) == 4)
+               tic6x_unwind_byte ((last_val << 4) | val);
+             else
+               last_val = val;
+           }
+         if ((cur_offset & 4) == 4)
+           tic6x_unwind_byte ((last_val << 4) | 0xf);
+       }
+
+      /* Pad with RETURN opcodes.  */
+      while ((unwind->data_bytes & 3) != 0)
+       tic6x_unwind_byte (UNWIND_OP_RET | UNWIND_B3);
+
+      if (unwind->personality_index == -1 && unwind->personality_routine == NULL)
+       unwind->personality_index = 0;
+    }
+
+  /* Force creation of an EXTAB entry if an LSDA is required.  */
+  if (need_extab && !unwind->table_entry)
+    {
+      if (unwind->data_bytes != 4)
+       abort ();
+
+      tic6x_flush_unwind_word (unwind->data);
+    }
+  else if (unwind->table_entry && !need_extab)
+    {
+      /* Add an empty descriptor if there is no user-specified data.   */
+      char *ptr = frag_more (4);
+      md_number_to_chars (ptr, 0, 4);
+    }
+
+  /* Fill in length of unwinding bytecode.  */
+  if (unwind->table_entry)
+    {
+      valueT tmp;
+      if (unwind->data_bytes > 0x400)
+       as_bad (_("too many unwinding instructions"));
+
+      if (unwind->personality_index == -1)
+       {
+         tmp = md_chars_to_number (unwind->frag_start + 4, 4);
+         tmp |= ((unwind->data_bytes - 8) >> 2) << 24;
+         md_number_to_chars (unwind->frag_start + 4, tmp, 4);
+       }
+      else if (unwind->personality_index == 1 || unwind->personality_index == 2)
+       {
+         tmp = md_chars_to_number (unwind->frag_start, 4);
+         tmp |= ((unwind->data_bytes - 4) >> 2) << 16;
+         md_number_to_chars (unwind->frag_start, tmp, 4);
+       }
+    }
+  tic6x_output_exidx_entry ();
+}
+
+/* FIXME: This will get horribly confused if cfi directives are emitted for
+   function epilogue.  */
+void
+tic6x_cfi_endproc (struct fde_entry *fde)
+{
+  tic6x_unwind_info *unwind = tic6x_get_unwind ();
+  struct cfi_insn_data *insn;
+  int reg;
+  unsigned safe_mask = 0;
+  unsigned compact_mask = 0;
+  unsigned reg_saved_mask = 0;
+  offsetT cfa_offset = 0;
+  offsetT save_offset = 0;
+
+  unwind->cfa_reg = 31;
+  unwind->return_reg = UNWIND_B3;
+  unwind->saved_reg_count = 0;
+  unwind->pop_rts = FALSE;
+
+  unwind->saved_seg = now_seg;
+  unwind->saved_subseg = now_subseg;
+
+  for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
+    unwind->reg_saved[reg] = FALSE;
+
+  /* Scan FDE instructions to build up stack frame layout.  */
+  for (insn = fde->data; insn; insn = insn->next)
+    {
+      switch (insn->insn)
+       {
+       case DW_CFA_advance_loc:
+         break;
+
+       case DW_CFA_def_cfa:
+         unwind->cfa_reg = insn->u.ri.reg;
+         cfa_offset = insn->u.ri.offset;
+         break;
+
+       case DW_CFA_def_cfa_register:
+         unwind->cfa_reg = insn->u.r;
+         break;
+
+       case DW_CFA_def_cfa_offset:
+         cfa_offset = insn->u.i;
+         break;
+
+       case DW_CFA_undefined:
+       case DW_CFA_same_value:
+         reg = tic6x_unwind_reg_from_dwarf (insn->u.r);
+         if (reg >= 0)
+           unwind->reg_saved[reg] = FALSE;
+         break;
+
+       case DW_CFA_offset:
+         reg = tic6x_unwind_reg_from_dwarf (insn->u.ri.reg);
+         if (reg < 0)
+           {
+             as_bad (_("unable to generate unwinding opcode for reg %d"),
+                     insn->u.ri.reg);
+             return;
+           }
+         unwind->reg_saved[reg] = TRUE;
+         unwind->reg_offset[reg] = insn->u.ri.offset;
+         if (insn->u.ri.reg == UNWIND_B3)
+           unwind->return_reg = UNWIND_B3;
+         break;
+
+       case DW_CFA_register:
+         if (insn->u.rr.reg1 != 19)
+           {
+             as_bad (_("unable to generate unwinding opcode for reg %d"),
+                     insn->u.rr.reg1);
+             return;
+           }
+
+         reg = tic6x_unwind_reg_from_dwarf (insn->u.rr.reg2);
+         if (reg < 0)
+           {
+             as_bad (_("unable to generate unwinding opcode for reg %d"),
+                     insn->u.rr.reg2);
+             return;
+           }
+
+         unwind->return_reg = reg;
+         unwind->reg_saved[UNWIND_B3] = FALSE;
+         if (unwind->reg_saved[reg])
+           {
+             as_bad (_("unable to restore return address from "
+                       "previously restored reg"));
+             return;
+           }
+         break;
+
+       case DW_CFA_restore:
+       case DW_CFA_remember_state:
+       case DW_CFA_restore_state:
+       case DW_CFA_GNU_window_save:
+       case CFI_escape:
+       case CFI_val_encoded_addr:
+         as_bad (_("unhandled CFA insn for unwinding (%d)"), insn->insn);
+         break;
+
+       default:
+         abort ();
+       }
+    }
+
+  if (unwind->cfa_reg != 15 && unwind->cfa_reg != 31)
+    {
+      as_bad (_("unable to generate unwinding opcode for frame pointer reg %d"),
+             unwind->cfa_reg);
+      return;
+    }
+
+  if (unwind->cfa_reg == 15)
+    {
+      if (cfa_offset != 0)
+       {
+         as_bad (_("unable to generate unwinding opcode for "
+                   "frame pointer offset"));
+         return;
+       }
+    }
+  else
+    {
+      if ((cfa_offset & 7) != 0)
+       {
+         as_bad (_("unwound stack pointer not doubleword aligned"));
+         return;
+       }
+    }
+
+  for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
+    {
+      if (unwind->reg_saved[reg])
+       reg_saved_mask |= 1 << (TIC6X_NUM_UNWIND_REGS - (reg + 1));
+    }
+
+  /* Check for standard "safe debug" frame layout */
+  if (reg_saved_mask)
+    {
+      save_offset = 0;
+      for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
+       {
+         if (!unwind->reg_saved[reg])
+           continue;
+
+         if (target_big_endian
+             && reg < TIC6X_NUM_UNWIND_REGS - 1
+             && unwind->reg_saved[reg + 1]
+             && tic6x_unwind_frame_regs[reg]
+                 == tic6x_unwind_frame_regs[reg + 1] + 1
+             && (tic6x_unwind_frame_regs[reg] & 1) == 1
+             && (save_offset & 4) == 4)
+           {
+             /* Swapped pair */
+             if (save_offset != unwind->reg_offset[reg + 1]
+                 || save_offset - 4 != unwind->reg_offset[reg])
+               break;
+             save_offset -= 8;
+             reg++;
+           }
+         else
+           {
+             if (save_offset != unwind->reg_offset[reg])
+               break;
+             save_offset -= 4;
+           }
+       }
+      if (reg == TIC6X_NUM_UNWIND_REGS)
+       {
+         safe_mask = reg_saved_mask;
+         reg_saved_mask = 0;
+       }
+    }
+
+  /* Check for compact frame layout.  */
+  if (reg_saved_mask)
+    {
+      save_offset = 0;
+      for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
+       {
+         int reg2;
+
+         if (!unwind->reg_saved[reg])
+           continue;
+
+         if (reg < TIC6X_NUM_UNWIND_REGS - 1)
+           {
+             reg2 = reg + 1;
+
+             if (!unwind->reg_saved[reg2]
+                 || tic6x_unwind_frame_regs[reg]
+                     != tic6x_unwind_frame_regs[reg2] + 1
+                 || (tic6x_unwind_frame_regs[reg2] & 1) != 0
+                 || save_offset == 0)
+               reg2 = -1;
+           }
+         else
+           reg2 = -1;
+
+         if (reg2 >= 0)
+           {
+             int high_offset;
+             if (target_big_endian)
+               high_offset = 4; /* lower address = positive stack offset.  */
+             else
+               high_offset = 0;
+
+             if (save_offset + 4 - high_offset != unwind->reg_offset[reg]
+                 || save_offset + high_offset != unwind->reg_offset[reg2])
+               {
+                 break;
+               }
+             reg++;
+           }
+         else
+           {
+             if (save_offset != unwind->reg_offset[reg])
+               break;
+           }
+         save_offset -= 8;
+       }
+
+      if (reg == TIC6X_NUM_UNWIND_REGS)
+       {
+         compact_mask = reg_saved_mask;
+         reg_saved_mask = 0;
+       }
+    }
+
+  /* Check for __c6xabi_pop_rts format */
+  if (reg_saved_mask == 0x17ff)
+    {
+      const int *pop_rts_offset = target_big_endian
+                               ? tic6x_pop_rts_offset_big
+                               : tic6x_pop_rts_offset_little;
+
+      save_offset = 0;
+      for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
+       {
+         if (reg == UNWIND_B15)
+           continue;
+
+         if (unwind->reg_offset[reg] != pop_rts_offset[reg] * 4)
+           break;
+       }
+
+      if (reg == TIC6X_NUM_UNWIND_REGS)
+       {
+         unwind->pop_rts = TRUE;
+         reg_saved_mask = 0;
+       }
+    }
+  /* If all else fails then describe the frame manually.  */
+  if (reg_saved_mask)
+    {
+      save_offset = 0;
+
+      for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
+       {
+         if (!unwind->reg_saved[reg])
+           continue;
+
+         unwind->saved_reg_count++;
+         /* Encoding uses 4 bits per word, so size of unwinding opcode data 
+            limits the save area size.  The exact cap will be figured out
+            later due to overflow, the 0x800 here is just a quick sanity
+            check to weed out obviously excessive offsets.  */
+         if (unwind->reg_offset[reg] > 0 || unwind->reg_offset[reg] < -0x800
+             || (unwind->reg_offset[reg] & 3) != 0)
+           {
+             as_bad (_("stack frame layout too complex for unwinder"));
+             return;
+           }
+
+         if (unwind->reg_offset[reg] < save_offset)
+           save_offset = unwind->reg_offset[reg] - 4;
+       }
+    }
+
+  /* Align to 8-byte boundary (stack grows towards negative offsets).  */
+  save_offset &= ~7;
+
+  if (unwind->cfa_reg == 31 && !reg_saved_mask)
+    {
+      cfa_offset += save_offset;
+      if (cfa_offset < 0)
+       {
+         as_bad (_("unwound frame has negative size"));
+         return;
+       }
+    }
+
+  unwind->safe_mask = safe_mask;
+  unwind->compact_mask = compact_mask;
+  unwind->reg_saved_mask = reg_saved_mask;
+  unwind->cfa_offset = cfa_offset;
+  unwind->function_start = fde->start_address;
+}