From Stephen Williams <steve@icarus.icarus.com>:
[deliverable/binutils-gdb.git] / gas / ecoff.c
index cfd0901c14edde822c61b901322140894f11b5a2..e8d66c318e433ebc54d844b6a968ecc7f9d1fdf6 100644 (file)
@@ -1,5 +1,5 @@
 /* ECOFF debugging support.
-   Copyright (C) 1993 Free Software Foundation, Inc.
+   Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
    Contributed by Cygnus Support.
    This file was put together by Ian Lance Taylor <ian@cygnus.com>.  A
    good deal of it comes directly from mips-tfile.c, by Michael
@@ -18,8 +18,9 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GAS; see the file COPYING.  If not, write to
-   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+   along with GAS; see the file COPYING.  If not, write to the Free
+   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+   02111-1307, USA.  */
 
 #include "as.h"
 
                array, pointer, function, etc. qualifiers.  The
                current base types that I have documentation for are:
 
-                       btNil           -- undefined 
+                       btNil           -- undefined
                        btAdr           -- address - integer same size as ptr
-                       btChar          -- character 
-                       btUChar         -- unsigned character 
-                       btShort         -- short 
-                       btUShort        -- unsigned short 
-                       btInt           -- int 
-                       btUInt          -- unsigned int 
-                       btLong          -- long 
-                       btULong         -- unsigned long 
-                       btFloat         -- float (real) 
-                       btDouble        -- Double (real) 
-                       btStruct        -- Structure (Record) 
-                       btUnion         -- Union (variant) 
-                       btEnum          -- Enumerated 
-                       btTypedef       -- defined via a typedef isymRef 
-                       btRange         -- subrange of int 
-                       btSet           -- pascal sets 
-                       btComplex       -- fortran complex 
-                       btDComplex      -- fortran double complex 
-                       btIndirect      -- forward or unnamed typedef 
-                       btFixedDec      -- Fixed Decimal 
-                       btFloatDec      -- Float Decimal 
-                       btString        -- Varying Length Character String 
-                       btBit           -- Aligned Bit String 
+                       btChar          -- character
+                       btUChar         -- unsigned character
+                       btShort         -- short
+                       btUShort        -- unsigned short
+                       btInt           -- int
+                       btUInt          -- unsigned int
+                       btLong          -- long
+                       btULong         -- unsigned long
+                       btFloat         -- float (real)
+                       btDouble        -- Double (real)
+                       btStruct        -- Structure (Record)
+                       btUnion         -- Union (variant)
+                       btEnum          -- Enumerated
+                       btTypedef       -- defined via a typedef isymRef
+                       btRange         -- subrange of int
+                       btSet           -- pascal sets
+                       btComplex       -- fortran complex
+                       btDComplex      -- fortran double complex
+                       btIndirect      -- forward or unnamed typedef
+                       btFixedDec      -- Fixed Decimal
+                       btFloatDec      -- Float Decimal
+                       btString        -- Varying Length Character String
+                       btBit           -- Aligned Bit String
                        btPicture       -- Picture
                        btVoid          -- Void (MIPS cc revision >= 2.00)
 
                current type qualifier fields I have documentation for
                are:
 
-                       tqNil           -- no more qualifiers 
-                       tqPtr           -- pointer 
-                       tqProc          -- procedure 
-                       tqArray         -- array 
-                       tqFar           -- 8086 far pointers 
-                       tqVol           -- volatile 
+                       tqNil           -- no more qualifiers
+                       tqPtr           -- pointer
+                       tqProc          -- procedure
+                       tqArray         -- array
+                       tqFar           -- 8086 far pointers
+                       tqVol           -- volatile
 
 
    The dense number table is used in the front ends, and disappears by
        }
 
    Mips-tdump produces the following information:
-   
+
    Global file header:
        magic number             0x162
        # sections               2
        symbolic header size     96
        optional header          56
        flags                    0x0
-   
+
    Symbolic header, magic number = 0x7009, vstamp = 1.31:
-   
+
        Info                      Offset      Number       Bytes
        ====                      ======      ======      =====
-   
+
        Line numbers                 380           4           4 [13]
        Dense numbers                  0           0           0
        Procedures Tables            384           1          52
        File Tables                 1008           2         144
        Relative Files                 0           0           0
        External Symbols            1152          20         320
-   
+
    File #0, "hello2.c"
-   
+
        Name index  = 1          Readin      = No
        Merge       = No         Endian      = LITTLE
        Debug level = G2         Language    = C
        Adr         = 0x00000000
-   
+
        Info                       Start      Number        Size      Offset
        ====                       =====      ======        ====      ======
        Local strings                  0          15          15         784
        Procedures                     0           1          52         384
        Auxiliary symbols              0          14          56         628
        Relative Files                 0           0           0           0
-   
+
     There are 6 local symbols, starting at 436
 
        Symbol# 0: "hello2.c"
@@ -778,7 +779,6 @@ enum aux_type {
 #define MAX_CLUSTER_PAGES 63
 #endif
 
-
 /* Linked list connecting separate page allocations.  */
 typedef struct vlinks {
   struct vlinks        *prev;          /* previous set of pages */
@@ -833,6 +833,7 @@ typedef struct scope {
 typedef struct localsym {
   const char *name;            /* symbol name */
   symbolS *as_sym;             /* symbol as seen by gas */
+  bfd_vma addend;              /* addend to as_sym value */
   struct efdr *file_ptr;       /* file pointer */
   struct ecoff_proc *proc_ptr; /* proc pointer */
   struct localsym *begin_ptr;  /* symbol at start of block */
@@ -937,6 +938,7 @@ typedef struct efdr {
   FDR           fdr;           /* File header to be written out */
   FDR          *orig_fdr;      /* original file header */
   char         *name;          /* filename */
+  int           fake;          /* whether this is faked .file */
   symint_t      void_type;     /* aux. pointer to 'void' type */
   symint_t      int_type;      /* aux. pointer to 'int' type */
   scope_t      *cur_scope;     /* current nested scopes */
@@ -953,7 +955,7 @@ typedef struct efdr {
 } efdr_t;
 
 /* Pre-initialized extended file structure.  */
-static const efdr_t init_file = 
+static const efdr_t init_file =
 {
   {                    /* FDR structure */
     0,                 /* adr:         memory address of beginning of file */
@@ -973,7 +975,7 @@ static const efdr_t init_file =
     0,                 /* rfdBase:     index into the file indirect table */
     0,                 /* crfd:        count file indirect entries */
     langC,             /* lang:        language for this file */
-    0,                 /* fMerge:      whether this file can be merged */
+    1,                 /* fMerge:      whether this file can be merged */
     0,                 /* fReadin:     true if read in (not just created) */
 #ifdef TARGET_BYTES_BIG_ENDIAN
     1,                 /* fBigendian:  if 1, compiled on big endian machine */
@@ -988,6 +990,7 @@ static const efdr_t init_file =
 
   (FDR *)0,            /* orig_fdr:    original file header pointer */
   (char *)0,           /* name:        pointer to filename */
+  0,                   /* fake:        whether this is a faked .file */
   0,                   /* void_type:   ptr to aux node for void type */
   0,                   /* int_type:    ptr to aux node for int type */
   (scope_t *)0,                /* cur_scope:   current scope being processed */
@@ -1021,6 +1024,7 @@ typedef struct lineno_list {
 } lineno_list_t;
 
 static lineno_list_t *first_lineno;
+static lineno_list_t *last_lineno;
 static lineno_list_t **last_lineno_ptr = &first_lineno;
 
 /* Sometimes there will be some .loc statements before a .ent.  We
@@ -1391,9 +1395,13 @@ static const st_t map_coff_sym_type[] = {
 /* Keep track of different sized allocation requests.  */
 static alloc_info_t alloc_counts[ (int)alloc_type_last ];
 \f
+/* Record whether we have seen any debugging information.  */
+int ecoff_debugging_seen = 0;
+
 /* Various statics.  */
 static efdr_t  *cur_file_ptr   = (efdr_t *) 0; /* current file desc. header */
 static proc_t  *cur_proc_ptr   = (proc_t *) 0; /* current procedure header */
+static proc_t  *first_proc_ptr  = (proc_t *) 0; /* first procedure header */
 static thead_t *top_tag_head   = (thead_t *) 0; /* top level tag head */
 static thead_t *cur_tag_head   = (thead_t *) 0; /* current tag head */
 #ifdef ECOFF_DEBUG
@@ -1401,6 +1409,8 @@ static int        debug           = 0;            /* trace functions */
 #endif
 static int     stabs_seen      = 0;            /* != 0 if stabs have been seen */
 
+static int current_file_idx;
+static const char *current_stabs_filename;
 
 /* Pseudo symbol to use when putting stabs into the symbol table.  */
 #ifndef STABS_SYMBOL
@@ -1418,7 +1428,7 @@ static symint_t add_string PARAMS ((varray_t *vp,
                                    shash_t **ret_hash));
 static localsym_t *add_ecoff_symbol PARAMS ((const char *str, st_t type,
                                             sc_t storage, symbolS *sym,
-                                            symint_t value,
+                                            bfd_vma addend, symint_t value,
                                             symint_t indx));
 static symint_t add_aux_sym_symint PARAMS ((symint_t aux_word));
 static symint_t add_aux_sym_rndx PARAMS ((int file_index,
@@ -1430,7 +1440,7 @@ static tag_t *get_tag PARAMS ((const char *tag, localsym_t *sym,
                               bt_t basic_type));
 static void add_unknown_tag PARAMS ((tag_t *ptag));
 static void add_procedure PARAMS ((char *func));
-static void add_file PARAMS ((const char *file_name, int indx));
+static void add_file PARAMS ((const char *file_name, int indx, int fake));
 #ifdef ECOFF_DEBUG
 static char *sc_to_string PARAMS ((sc_t storage_class));
 static char *st_to_string PARAMS ((st_t symbol_type));
@@ -1462,10 +1472,7 @@ static unsigned long ecoff_build_ss
 static unsigned long ecoff_build_fdr
   PARAMS ((const struct ecoff_debug_swap *backend, char **buf, char **bufend,
           unsigned long offset));
-static unsigned long ecoff_build_ext
-  PARAMS ((const struct ecoff_debug_swap *backend, char **buf, char **bufend,
-          unsigned long offset, varray_t *ext_strings,
-          struct hash_control *ext_str_hash));
+static void ecoff_setup_ext PARAMS ((void));
 static page_t *allocate_cluster PARAMS ((unsigned long npages));
 static page_t *allocate_page PARAMS ((void));
 static scope_t *allocate_scope PARAMS ((void));
@@ -1479,6 +1486,7 @@ static forward_t *allocate_forward PARAMS ((void));
 static thead_t *allocate_thead PARAMS ((void));
 static void free_thead PARAMS ((thead_t *ptr));
 static lineno_list_t *allocate_lineno_list PARAMS ((void));
+static void generate_ecoff_stab PARAMS ((int, const char *, int, int, int));
 \f
 /* This function should be called when the assembler starts up.  */
 
@@ -1499,11 +1507,17 @@ void
 ecoff_symbol_new_hook (symbolP)
      symbolS *symbolP;
 {
-  if (cur_file_ptr == (efdr_t *) NULL)
-    add_file ((const char *) NULL, 0);
+  /* Make sure that we have a file pointer, but only if we have seen a
+     file.  If we haven't seen a file, then this is a probably special
+     symbol created by md_begin which may required special handling at
+     some point.  Creating a dummy file with a dummy name is certainly
+     wrong.  */
+  if (cur_file_ptr == (efdr_t *) NULL
+      && seen_at_least_1_file ())
+    add_file ((const char *) NULL, 0, 1);
   symbolP->ecoff_file = cur_file_ptr;
   symbolP->ecoff_symbol = NULL;
-  symbolP->ecoff_undefined = 0;
+  symbolP->ecoff_extern_size = 0;
 }
 \f
 /* Add a page to a varray object.  */
@@ -1589,11 +1603,12 @@ add_string (vp, hash_tbl, str, ret_hash)
 /* Add debugging information for a symbol.  */
 
 static localsym_t *
-add_ecoff_symbol (str, type, storage, sym_value, value, indx)
+add_ecoff_symbol (str, type, storage, sym_value, addend, value, indx)
      const char *str;                  /* symbol name */
      st_t type;                                /* symbol type */
      sc_t storage;                     /* storage class */
      symbolS *sym_value;               /* associated symbol.  */
+     bfd_vma addend;                   /* addend to sym_value.  */
      symint_t value;                   /* value of symbol */
      symint_t indx;                    /* index to local/aux. syms */
 {
@@ -1623,6 +1638,7 @@ add_ecoff_symbol (str, type, storage, sym_value, value, indx)
   psym->as_sym = sym_value;
   if (sym_value != (symbolS *) NULL)
     sym_value->ecoff_symbol = psym;
+  psym->addend = addend;
   psym->file_ptr = cur_file_ptr;
   psym->proc_ptr = cur_proc_ptr;
   psym->begin_ptr = (localsym_t *) NULL;
@@ -1936,7 +1952,7 @@ add_aux_sym_tir (t, state, hash_tbl)
   ret = vp->num_allocated++;
 
   /* Add bitfield length if it exists.
-     
+
      NOTE:  Mips documentation claims bitfield goes at the end of the
      AUX record, but the DECstation compiler emits it here.
      (This would only make a difference for enum bitfields.)
@@ -2093,6 +2109,7 @@ add_unknown_tag (ptag)
                          st_Block,
                          sc_Info,
                          (symbolS *) NULL,
+                         (bfd_vma) 0,
                          (symint_t) 0,
                          (symint_t) 0);
 
@@ -2100,6 +2117,7 @@ add_unknown_tag (ptag)
                           st_End,
                           sc_Info,
                           (symbolS *) NULL,
+                          (bfd_vma) 0,
                           (symint_t) 0,
                           (symint_t) 0);
 
@@ -2117,6 +2135,7 @@ add_procedure (func)
 {
   register varray_t *vp;
   register proc_t *new_proc_ptr;
+  symbolS *sym;
 
 #ifdef ECOFF_DEBUG
   if (debug)
@@ -2133,6 +2152,9 @@ add_procedure (func)
 
   cur_proc_ptr = new_proc_ptr = &vp->last->datum->proc[vp->objects_last_page++];
 
+  if (first_proc_ptr == (proc_t *) NULL)
+    first_proc_ptr = new_proc_ptr;
+
   vp->num_allocated++;
 
   new_proc_ptr->pdr.isym = -1;
@@ -2140,10 +2162,14 @@ add_procedure (func)
   new_proc_ptr->pdr.lnLow = -1;
   new_proc_ptr->pdr.lnHigh = -1;
 
+  /* Set the BSF_FUNCTION flag for the symbol.  */
+  sym = symbol_find_or_make (func);
+  sym->bsym->flags |= BSF_FUNCTION;
+
   /* Push the start of the function.  */
   new_proc_ptr->sym = add_ecoff_symbol ((const char *) NULL, st_Proc, sc_Text,
-                                       symbol_find_or_make (func),
-                                       (symint_t) 0, (symint_t) 0);
+                                       sym, (bfd_vma) 0, (symint_t) 0,
+                                       (symint_t) 0);
 
   ++proc_cnt;
 
@@ -2156,7 +2182,10 @@ add_procedure (func)
        l->proc = new_proc_ptr;
       *last_lineno_ptr = noproc_lineno;
       while (*last_lineno_ptr != NULL)
-       last_lineno_ptr = &(*last_lineno_ptr)->next;
+       {
+         last_lineno = *last_lineno_ptr;
+         last_lineno_ptr = &last_lineno->next;
+       }
       noproc_lineno = (lineno_list_t *) NULL;
     }
 }
@@ -2166,9 +2195,10 @@ add_procedure (func)
    where the current file structure lives.  */
 
 static void
-add_file (file_name, indx)
+add_file (file_name, indx, fake)
      const char *file_name;            /* file name */
      int indx;
+     int fake;
 {
   register int first_ch;
   register efdr_t *fil_ptr;
@@ -2188,13 +2218,20 @@ add_file (file_name, indx)
        as_fatal ("fake .file after real one");
       as_where (&file, (unsigned int *) NULL);
       file_name = (const char *) file;
+
+      if (! symbol_table_frozen)
+       generate_asm_lineno = 1;
     }
+  else
+      generate_asm_lineno = 0;
 
 #ifndef NO_LISTING
   if (listing)
     listing_source_file (file_name);
 #endif
 
+  current_stabs_filename = file_name;
+
   /* If we're creating stabs, then we don't actually make a new FDR.
      Instead, we just create a stabs symbol.  */
   if (stabs_seen)
@@ -2203,21 +2240,37 @@ add_file (file_name, indx)
                               symbol_new ("L0\001", now_seg,
                                           (valueT) frag_now_fix (),
                                           frag_now),
-                              0, ECOFF_MARK_STAB (N_SOL));
+                              (bfd_vma) 0, 0, ECOFF_MARK_STAB (N_SOL));
       return;
     }
 
   first_ch = *file_name;
 
+  /* FIXME: We can't safely merge files which have line number
+     information (fMerge will be zero in this case).  Otherwise, we
+     get incorrect line number debugging info.  See for instance
+     ecoff_build_lineno, which will end up setting all file->fdr.*
+     fields multiple times, resulting in incorrect debug info.  In
+     order to make this work right, all line number and symbol info
+     for the same source file has to be adjacent in the object file,
+     so that a single file descriptor can be used to point to them.
+     This would require maintaining file specific lists of line
+     numbers and symbols for each file, so that they can be merged
+     together (or output together) when two .file pseudo-ops are
+     merged into one file descriptor.  */
+
   /* See if the file has already been created.  */
   for (fil_ptr = first_file;
        fil_ptr != (efdr_t *) NULL;
        fil_ptr = fil_ptr->next_file)
     {
       if (first_ch == fil_ptr->name[0]
-         && strcmp (file_name, fil_ptr->name) == 0)
+         && strcmp (file_name, fil_ptr->name) == 0
+         && fil_ptr->fdr.fMerge)
        {
          cur_file_ptr = fil_ptr;
+         if (! fake)
+           cur_file_ptr->fake = 0;
          break;
        }
     }
@@ -2232,9 +2285,11 @@ add_file (file_name, indx)
        &file_desc.last->datum->file[file_desc.objects_last_page++];
       *fil_ptr = init_file;
 
-      fil_ptr->file_index = indx;
+      fil_ptr->file_index = current_file_idx++;
       ++file_desc.num_allocated;
 
+      fil_ptr->fake = fake;
+
       /* Allocate the string hash table.  */
       fil_ptr->str_hash = hash_new ();
 
@@ -2250,7 +2305,7 @@ add_file (file_name, indx)
       /* Push the start of the filename. We assume that the filename
          will be stored at string offset 1.  */
       (void) add_ecoff_symbol (file_name, st_File, sc_Text,
-                              (symbolS *) NULL,
+                              (symbolS *) NULL, (bfd_vma) 0,
                               (symint_t) 0, (symint_t) 0);
       fil_ptr->fdr.rss = 1;
       fil_ptr->name = &fil_ptr->strings.last->datum->byte[1];
@@ -2268,8 +2323,39 @@ add_file (file_name, indx)
       fil_ptr->int_type = add_aux_sym_tir (&int_type_info,
                                           hash_yes,
                                           &cur_file_ptr->thash_head[0]);
+      /* gas used to have a bug that if the file does not have any
+        symbol, it either will abort or will not build the file,
+        the following is to get around that problem. ---kung*/
+#if 0
+      if (generate_asm_lineno)
+       {
+         mark_stabs (0);
+          (void) add_ecoff_symbol (file_name, st_Nil, sc_Nil,
+                                  symbol_new ("L0\001", now_seg,
+                                              (valueT) frag_now_fix (),
+                                              frag_now),
+                                  (bfd_vma) 0, 0, ECOFF_MARK_STAB (N_SO));
+          (void) add_ecoff_symbol ("void:t1=1", st_Nil, sc_Nil,
+                                  (symbolS *) NULL, (bfd_vma) 0, 0,
+                                  ECOFF_MARK_STAB (N_LSYM));
+       }
+#endif
     }
 }
+
+/* This function is called when the assembler notices a preprocessor
+   directive switching to a new file.  This will not happen in
+   compiler output, only in hand coded assembler.  */
+
+void
+ecoff_new_file (name)
+     const char *name;
+{
+  if (cur_file_ptr != NULL && strcmp (cur_file_ptr->name, name) == 0)
+    return;
+  add_file (name, 0, 0);
+  generate_asm_lineno = 1;
+}
 \f
 #ifdef ECOFF_DEBUG
 
@@ -2373,13 +2459,13 @@ ecoff_directive_begin (ignore)
       demand_empty_rest_of_line ();
       return;
     }
-  
+
   name = input_line_pointer;
   name_end = get_symbol_end ();
 
   (void) add_ecoff_symbol ((const char *) NULL, st_Block, sc_Text,
                           symbol_find_or_make (name),
-                          (symint_t) 0, (symint_t) 0);
+                          (bfd_vma) 0, (symint_t) 0, (symint_t) 0);
 
   *input_line_pointer = name_end;
 
@@ -2424,7 +2510,7 @@ ecoff_directive_bend (ignore)
     as_warn (".bend directive names unknown symbol");
   else
     (void) add_ecoff_symbol ((const char *) NULL, st_End, sc_Text, endsym,
-                            (symint_t) 0, (symint_t) 0);
+                            (bfd_vma) 0, (symint_t) 0, (symint_t) 0);
 
   *input_line_pointer = name_end;
 
@@ -2444,7 +2530,8 @@ static st_t coff_symbol_typ;
 static int coff_is_function;
 static char *coff_tag;
 static valueT coff_value;
-symbolS *coff_sym_value;
+static symbolS *coff_sym_value;
+static bfd_vma coff_sym_addend;
 static int coff_inside_enumeration;
 
 /* Handle a .def directive: start defining a symbol.  */
@@ -2456,6 +2543,8 @@ ecoff_directive_def (ignore)
   char *name;
   char name_end;
 
+  ecoff_debugging_seen = 1;
+
   SKIP_WHITESPACE ();
 
   name = input_line_pointer;
@@ -2480,6 +2569,7 @@ ecoff_directive_def (ignore)
       coff_tag = (char *) NULL;
       coff_value = 0;
       coff_sym_value = (symbolS *) NULL;
+      coff_sym_addend = 0;
     }
 
   *input_line_pointer = name_end;
@@ -2642,7 +2732,11 @@ ecoff_directive_type (ignore)
     {
       if (tq_ptr == &coff_type.type_qualifiers[0])
        {
-         as_warn ("Too derived values in .type argument");
+         /* FIXME: We could handle this by setting the continued bit.
+             There would still be a limit: the .type argument can not
+             be infinite.  */
+         as_warn ("The type of %s is too complex; it will be simplified",
+                  coff_sym_name);
          break;
        }
       if (ISPTR (val))
@@ -2714,6 +2808,8 @@ void
 ecoff_directive_val (ignore)
      int ignore;
 {
+  expressionS exp;
+
   if (coff_sym_name == (char *) NULL)
     {
       as_warn (".val pseudo-op used outside of .def/.endef; ignored");
@@ -2721,26 +2817,20 @@ ecoff_directive_val (ignore)
       return;
     }
 
-  if (! is_name_beginner ((unsigned char) *input_line_pointer))
-    coff_value = get_absolute_expression ();
-  else
+  expression (&exp);
+  if (exp.X_op != O_constant && exp.X_op != O_symbol)
     {
-      char *name;
-      char name_end;
-
-      name = input_line_pointer;
-      name_end = get_symbol_end ();
-
-      if (strcmp (name, ".") == 0)
-       as_warn ("`.val .' not supported");
-      else
-       coff_sym_value = symbol_find_or_make (name);
-
-      *input_line_pointer = name_end;
+      as_bad (".val expression is too copmlex");
+      demand_empty_rest_of_line ();
+      return;
+    }
 
-      /* FIXME: gcc can generate address expressions here in unusual
-        cases (search for "obscure" in sdbout.c), although this is
-        very unlikely for a MIPS chip.  */
+  if (exp.X_op == O_constant)
+    coff_value = exp.X_add_number;
+  else
+    {
+      coff_sym_value = exp.X_add_symbol;
+      coff_sym_addend = exp.X_add_number;
     }
 
   demand_empty_rest_of_line ();
@@ -2808,8 +2898,10 @@ ecoff_directive_endef (ignore)
 
          coff_type.num_sizes = i + 1;
          for (i--; i >= 0; i--)
-           coff_type.sizes[i] = (coff_type.sizes[i + 1]
-                                 / coff_type.dimensions[i + 1]);
+           coff_type.sizes[i] = (coff_type.dimensions[i + 1] == 0
+                                 ? 0
+                                 : (coff_type.sizes[i + 1]
+                                    / coff_type.dimensions[i + 1]));
        }
     }
   else if (coff_symbol_typ == st_Member
@@ -2902,6 +2994,7 @@ ecoff_directive_endef (ignore)
                          coff_symbol_typ,
                          coff_storage_class,
                          coff_sym_value,
+                         coff_sym_addend,
                          (symint_t) coff_value,
                          indx);
 
@@ -2951,7 +3044,7 @@ ecoff_directive_end (ignore)
 
   name = input_line_pointer;
   name_end = get_symbol_end ();
-  
+
   ch = *name;
   if (! is_name_beginner (ch))
     {
@@ -2969,11 +3062,25 @@ ecoff_directive_end (ignore)
   if (ent == (symbolS *) NULL)
     as_warn (".end directive names unknown symbol");
   else
-    (void) add_ecoff_symbol ((const char *) NULL, st_End, sc_Text,
-                            symbol_new ("L0\001", now_seg,
-                                        (valueT) frag_now_fix (),
-                                        frag_now),
-                            (symint_t) 0, (symint_t) 0);
+    {
+      (void) add_ecoff_symbol ((const char *) NULL, st_End, sc_Text,
+                              symbol_new ("L0\001", now_seg,
+                                          (valueT) frag_now_fix (),
+                                          frag_now),
+                              (bfd_vma) 0, (symint_t) 0, (symint_t) 0);
+
+      if (stabs_seen && generate_asm_lineno)
+       {
+       char *n;
+
+         n = xmalloc (strlen (name) + 4);
+         strcpy (n, name);
+         strcat (n, ":F1");
+         (void) add_ecoff_symbol ((const char *) n, stGlobal, scText, 
+                                  ent, (bfd_vma) 0, 0,
+                                  ECOFF_MARK_STAB (N_FUN));
+       }
+    }
 
   cur_proc_ptr = (proc_t *) NULL;
 
@@ -2992,7 +3099,7 @@ ecoff_directive_ent (ignore)
   register int ch;
 
   if (cur_file_ptr == (efdr_t *) NULL)
-    add_file ((const char *) NULL, 0);
+    add_file ((const char *) NULL, 0, 1);
 
   if (cur_proc_ptr != (proc_t *) NULL)
     {
@@ -3033,6 +3140,31 @@ ecoff_directive_ent (ignore)
   demand_empty_rest_of_line ();
 }
 \f
+/* Parse .extern directives.  */
+
+void
+ecoff_directive_extern (ignore)
+     int ignore;
+{
+  char *name;
+  int c;
+  symbolS *symbolp;
+  valueT size;
+
+  name = input_line_pointer;
+  c = get_symbol_end ();
+  symbolp = symbol_find_or_make (name);
+  *input_line_pointer = c;
+
+  S_SET_EXTERNAL (symbolp);
+
+  if (*input_line_pointer == ',')
+    ++input_line_pointer;
+  size = get_absolute_expression ();
+
+  symbolp->ecoff_extern_size = size;
+}
+\f
 /* Parse .file directives.  */
 
 void
@@ -3055,7 +3187,7 @@ ecoff_directive_file (ignore)
   /* FIXME: we don't have to save the name here.  */
   name = demand_copy_C_string (&len);
 
-  add_file (name, indx - 1);
+  add_file (name, indx - 1, 0);
 
   demand_empty_rest_of_line ();
 }
@@ -3120,7 +3252,13 @@ ecoff_directive_frame (ignore)
 
   cur_proc_ptr->pdr.pcreg = tc_get_register (0);
 
+#if 0 /* Alpha-OSF1 adds "the offset of saved $a0 from $sp", according
+        to Sandro.  I don't yet know where this value should be stored, if
+        anywhere.  */
   demand_empty_rest_of_line ();
+#else
+  s_ignore (42);
+#endif
 }
 \f
 /* Parse .mask directives.  */
@@ -3195,7 +3333,7 @@ ecoff_directive_loc (ignore)
                               symbol_new ("L0\001", now_seg,
                                           (valueT) frag_now_fix (),
                                           frag_now),
-                              0, lineno);
+                              (bfd_vma) 0, 0, lineno);
       return;
     }
 
@@ -3208,6 +3346,9 @@ ecoff_directive_loc (ignore)
   list->paddr = frag_now_fix ();
   list->lineno = lineno;
 
+  /* We don't want to merge files which have line numbers.  */
+  cur_file_ptr->fdr.fMerge = 0;
+
   /* A .loc directive will sometimes appear before a .ent directive,
      which means that cur_proc_ptr will be NULL here.  Arrange to
      patch this up.  */
@@ -3222,10 +3363,29 @@ ecoff_directive_loc (ignore)
     }
   else
     {
+      last_lineno = list;
       *last_lineno_ptr = list;
       last_lineno_ptr = &list->next;
     }
 }
+
+/* The MIPS assembler sometimes inserts nop instructions in the
+   instruction stream.  When this happens, we must patch up the .loc
+   information so that it points to the instruction after the nop.  */
+
+void
+ecoff_fix_loc (old_frag, old_frag_offset)
+     fragS *old_frag;
+     unsigned long old_frag_offset;
+{
+  if (last_lineno != NULL
+      && last_lineno->frag == old_frag
+      && last_lineno->paddr == old_frag_offset)
+    {
+      last_lineno->frag = frag_now;
+      last_lineno->paddr = frag_now_fix ();
+    }
+}
 \f
 /* Make sure the @stabs symbol is emitted.  */
 
@@ -3239,10 +3399,59 @@ mark_stabs (ignore)
       stabs_seen = 1;
       (void) add_ecoff_symbol (stabs_symbol, stNil, scInfo,
                               (symbolS *) NULL,
-                              (symint_t) -1, ECOFF_MARK_STAB (0));
+                              (bfd_vma) 0, (symint_t) -1,
+                              ECOFF_MARK_STAB (0));
     }
 }
 \f
+/* Parse .weakext directives.  */
+
+void
+ecoff_directive_weakext (ignore)
+     int ignore;
+{
+  char *name;
+  int c;
+  symbolS *symbolP;
+  expressionS exp;
+
+  name = input_line_pointer;
+  c = get_symbol_end ();
+  symbolP = symbol_find_or_make (name);
+  *input_line_pointer = c;
+
+  SKIP_WHITESPACE ();
+
+  if (*input_line_pointer == ',')
+    {
+      if (S_IS_DEFINED (symbolP))
+       {
+         as_bad ("Ignoring attempt to redefine symbol `%s'.",
+                 S_GET_NAME (symbolP));
+         ignore_rest_of_line ();
+         return;
+       }
+
+      ++input_line_pointer;
+      SKIP_WHITESPACE ();
+      if (! is_end_of_line[(unsigned char) *input_line_pointer])
+       {
+         expression (&exp);
+         if (exp.X_op != O_symbol)
+           {
+             as_bad ("bad .weakext directive");
+             ignore_rest_of_line();
+             return;
+           }
+         symbolP->sy_value = exp;
+       }
+    }
+
+  S_SET_WEAK (symbolP);
+
+  demand_empty_rest_of_line ();
+}
+\f
 /* Handle .stabs directives.  The actual parsing routine is done by a
    generic routine.  This routine is called via OBJ_PROCESS_STAB.
    When this is called, input_line_pointer will be pointing at the
@@ -3276,7 +3485,8 @@ mark_stabs (ignore)
        value           a numeric value or an address.  */
 
 void
-ecoff_stab (what, string, type, other, desc)
+ecoff_stab (sec, what, string, type, other, desc)
+     segT sec;
      int what;
      const char *string;
      int type;
@@ -3286,9 +3496,13 @@ ecoff_stab (what, string, type, other, desc)
   efdr_t *save_file_ptr = cur_file_ptr;
   symbolS *sym;
   symint_t value;
+  bfd_vma addend;
   st_t st;
   sc_t sc;
   symint_t indx;
+  localsym_t *hold = NULL;
+
+  ecoff_debugging_seen = 1;
 
   /* We don't handle .stabd.  */
   if (what != 's' && what != 'n')
@@ -3308,7 +3522,7 @@ ecoff_stab (what, string, type, other, desc)
   /* Make sure we have a current file.  */
   if (cur_file_ptr == (efdr_t *) NULL)
     {
-      add_file ((const char *) NULL, 0);
+      add_file ((const char *) NULL, 0, 1);
       save_file_ptr = cur_file_ptr;
     }
 
@@ -3349,6 +3563,7 @@ ecoff_stab (what, string, type, other, desc)
       *input_line_pointer = name_end;
 
       value = 0;
+      addend = 0;
       st = st_Label;
       sc = sc_Text;
       indx = desc;
@@ -3359,7 +3574,7 @@ ecoff_stab (what, string, type, other, desc)
       if (listing && (type == N_SO || type == N_SOL))
        listing_source_file (string);
 #endif
-      
+
       if (isdigit (*input_line_pointer)
          || *input_line_pointer == '-'
          || *input_line_pointer == '+')
@@ -3368,6 +3583,7 @@ ecoff_stab (what, string, type, other, desc)
          sc = sc_Nil;
          sym = (symbolS *) NULL;
          value = get_absolute_expression ();
+         addend = 0;
        }
       else if (! is_name_beginner ((unsigned char) *input_line_pointer))
        {
@@ -3376,37 +3592,89 @@ ecoff_stab (what, string, type, other, desc)
        }
       else
        {
-         char *name;
-         char name_end;
-
-         name = input_line_pointer;
-         name_end = get_symbol_end ();
-
-         sym = symbol_find_or_make (name);
+         expressionS exp;
 
          sc = sc_Nil;
          st = st_Nil;
-         value = 0;
 
-         *input_line_pointer = name_end;
-         if (name_end == '+' || name_end == '-')
+         expression (&exp);
+         if (exp.X_op == O_constant)
+           {
+             sym = NULL;
+             value = exp.X_add_number;
+             addend = 0;
+           }
+         else if (exp.X_op == O_symbol)
+           {
+             sym = exp.X_add_symbol;
+             value = 0;
+             addend = exp.X_add_number;
+           }
+         else
            {
-             ++input_line_pointer;
-             value = get_absolute_expression ();
-             if (name_end == '-')
-               value = - value;
+             sym = make_expr_symbol (&exp);
+             value = 0;
+             addend = 0;
            }
        }
 
       indx = ECOFF_MARK_STAB (type);
     }
 
-  (void) add_ecoff_symbol (string, st, sc, sym, value, indx);
+  /* Don't store the stabs symbol we are creating as the type of the
+     ECOFF symbol.  We want to compute the type of the ECOFF symbol
+     independently.  */
+  if (sym != (symbolS *) NULL)
+    hold = sym->ecoff_symbol;
+
+  (void) add_ecoff_symbol (string, st, sc, sym, addend, value, indx);
+
+  if (sym != (symbolS *) NULL)
+    sym->ecoff_symbol = hold;
 
   /* Restore normal file type.  */
   cur_file_ptr = save_file_ptr;
 }
 \f
+/* Frob an ECOFF symbol.  Small common symbols go into a special
+   .scommon section rather than bfd_com_section.  */
+
+void
+ecoff_frob_symbol (sym)
+     symbolS *sym;
+{
+  if (S_IS_COMMON (sym)
+      && S_GET_VALUE (sym) > 0
+      && S_GET_VALUE (sym) <= bfd_get_gp_size (stdoutput))
+    {
+      static asection scom_section;
+      static asymbol scom_symbol;
+
+      /* We must construct a fake section similar to bfd_com_section
+        but with the name .scommon.  */
+      if (scom_section.name == NULL)
+       {
+         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;
+       }
+      S_SET_SEGMENT (sym, &scom_section);
+    }
+
+  /* Double check weak symbols.  */
+  if (sym->bsym->flags & BSF_WEAK)
+    {
+      if (S_IS_COMMON (sym))
+       as_bad ("Symbol `%s' can not be both weak and common",
+               S_GET_NAME (sym));
+    }
+}
+\f
 /* Add bytes to the symbolic information buffer.  */
 
 static char *
@@ -3477,6 +3745,7 @@ ecoff_build_lineno (backend, buf, bufend, offset, linecntptr)
   unsigned long c;
   long iline;
   long totcount;
+  lineno_list_t first;
 
   if (linecntptr != (long *) NULL)
     *linecntptr = 0;
@@ -3489,6 +3758,31 @@ ecoff_build_lineno (backend, buf, bufend, offset, linecntptr)
   c = offset;
   iline = 0;
   totcount = 0;
+
+  /* For some reason the address of the first procedure is ignored
+     when reading line numbers.  This doesn't matter if the address of
+     the first procedure is 0, but when gcc is generating MIPS
+     embedded PIC code, it will put strings in the .text section
+     before the first procedure.  We cope by inserting a dummy line if
+     the address of the first procedure is not 0.  Hopefully this
+     won't screw things up too badly.  */
+  if (first_proc_ptr != (proc_t *) NULL
+      && first_lineno != (lineno_list_t *) NULL
+      && ((S_GET_VALUE (first_proc_ptr->sym->as_sym)
+          + bfd_get_section_vma (stdoutput,
+                                 S_GET_SEGMENT (first_proc_ptr->sym->as_sym)))
+         != 0))
+    {
+      first.file = first_lineno->file;
+      first.proc = first_lineno->proc;
+      first.frag = &zero_address_frag;
+      first.paddr = 0;
+      first.lineno = 0;
+
+      first.next = first_lineno;
+      first_lineno = &first;
+    }
+
   for (l = first_lineno; l != (lineno_list_t *) NULL; l = l->next)
     {
       long count;
@@ -3498,17 +3792,22 @@ ecoff_build_lineno (backend, buf, bufend, offset, linecntptr)
         (in words).  Do this first, so that we can skip ahead to the
         next useful line number entry.  */
       if (l->next == (lineno_list_t *) NULL)
-       count = 0;
-      else
+       {
+         /* We want a count of zero, but it will be decremented
+            before it is used.  */
+         count = 1;
+       }
+      else if (l->next->frag->fr_address + l->next->paddr
+              > l->frag->fr_address + l->paddr)
        {
          count = ((l->next->frag->fr_address + l->next->paddr
                    - (l->frag->fr_address + l->paddr))
                   >> 2);
-         if (count <= 0)
-           {
-             /* Don't change last, so we still get the right delta.  */
-             continue;
-           }
+       }
+      else
+       {
+         /* Don't change last, so we still get the right delta.  */
+         continue;
        }
 
       if (l->file != file || l->proc != proc)
@@ -3518,8 +3817,6 @@ ecoff_build_lineno (backend, buf, bufend, offset, linecntptr)
          if (l->file != file && file != (efdr_t *) NULL)
            {
              file->fdr.cbLine = c - file->fdr.cbLineOffset;
-             /* The cline field is ill-documented.  This is a guess
-                at the right value.  */
              file->fdr.cline = totcount + count;
              if (linecntptr != (long *) NULL)
                *linecntptr += totcount + count;
@@ -3528,8 +3825,14 @@ ecoff_build_lineno (backend, buf, bufend, offset, linecntptr)
 
          if (l->file != file)
            {
+             efdr_t *last_file = file;
+
              file = l->file;
-             file->fdr.ilineBase = iline;
+             if (last_file != (efdr_t *) NULL)
+               file->fdr.ilineBase
+                 = last_file->fdr.ilineBase + last_file->fdr.cline;
+             else
+               file->fdr.ilineBase = 0;
              file->fdr.cbLineOffset = c;
            }
          if (l->proc != proc)
@@ -3539,14 +3842,12 @@ ecoff_build_lineno (backend, buf, bufend, offset, linecntptr)
                {
                  proc->pdr.lnLow = l->lineno;
                  proc->pdr.cbLineOffset = c - file->fdr.cbLineOffset;
-                 /* The iline field is ill-documented.  This is a
-                    guess at the right value.  */
                  proc->pdr.iline = totcount;
                }
            }
 
          last = (lineno_list_t *) NULL;
-       }      
+       }
 
       totcount += count;
 
@@ -3751,13 +4052,17 @@ ecoff_build_symbols (backend, buf, bufend, offset)
                        sym_ptr->ecoff_sym.asym.value =
                          (S_GET_VALUE (as_sym)
                           + bfd_get_section_vma (stdoutput,
-                                                 S_GET_SEGMENT (as_sym)));
+                                                 S_GET_SEGMENT (as_sym))
+                          + sym_ptr->addend);
+
+                     sym_ptr->ecoff_sym.weakext = S_IS_WEAK (as_sym);
 
                      /* Set st_Proc to st_StaticProc for local
                         functions.  */
                      if (sym_ptr->ecoff_sym.asym.st == st_Proc
                          && S_IS_DEFINED (as_sym)
-                         && ! S_IS_EXTERNAL (as_sym))
+                         && ! S_IS_EXTERNAL (as_sym)
+                         && ! S_IS_WEAK (as_sym))
                        sym_ptr->ecoff_sym.asym.st = st_StaticProc;
 
                      /* Get the type and storage class based on where
@@ -3778,23 +4083,36 @@ ecoff_build_symbols (backend, buf, bufend, offset)
                          seg = S_GET_SEGMENT (as_sym);
                          segname = segment_name (seg);
 
-                         if (S_IS_EXTERNAL (as_sym)
-                             || ! S_IS_DEFINED (as_sym))
-                           st = st_Global;
+                         if (! ECOFF_IS_STAB (&sym_ptr->ecoff_sym.asym)
+                             && (S_IS_EXTERNAL (as_sym)
+                                 || S_IS_WEAK (as_sym)
+                                 || ! S_IS_DEFINED (as_sym)))
+                           {
+                             if ((as_sym->bsym->flags & BSF_FUNCTION) != 0)
+                               st = st_Proc;
+                             else
+                               st = st_Global;
+                           }
                          else if (seg == text_section)
                            st = st_Label;
                          else
                            st = st_Static;
 
-                         if (! S_IS_DEFINED (as_sym)
-                             || as_sym->ecoff_undefined)
+                         if (! S_IS_DEFINED (as_sym))
                            {
-                             if (S_GET_VALUE (as_sym) > 0
-                                 && (S_GET_VALUE (as_sym)
-                                     <= bfd_get_gp_size (stdoutput)))
-                               sc = sc_SUndefined;
-                             else
+                             if (as_sym->ecoff_extern_size == 0
+                                 || (as_sym->ecoff_extern_size
+                                     > bfd_get_gp_size (stdoutput)))
                                sc = sc_Undefined;
+                             else
+                               {
+                                 sc = sc_SUndefined;
+                                 sym_ptr->ecoff_sym.asym.value =
+                                   as_sym->ecoff_extern_size;
+                               }
+#ifdef S_SET_SIZE
+                             S_SET_SIZE (as_sym, as_sym->ecoff_extern_size);
+#endif
                            }
                          else if (S_IS_COMMON (as_sym))
                            {
@@ -3821,7 +4139,12 @@ ecoff_build_symbols (backend, buf, bufend, offset)
                          else if (seg == &bfd_abs_section)
                            sc = sc_Abs;
                          else
-                           abort ();
+                           {
+                             /* This must be a user named section.
+                                 This is not possible in ECOFF, but it
+                                 is in ELF.  */
+                             sc = sc_Data;
+                           }
 
                          sym_ptr->ecoff_sym.asym.st = (int) st;
                          sym_ptr->ecoff_sym.asym.sc = (int) sc;
@@ -3834,12 +4157,18 @@ ecoff_build_symbols (backend, buf, bufend, offset)
                         than the actual symbol.  Should we handle
                         them here?  */
                      if ((S_IS_EXTERNAL (as_sym)
+                          || S_IS_WEAK (as_sym)
                           || ! S_IS_DEFINED (as_sym))
                          && sym_ptr->proc_ptr == (proc_t *) NULL
                          && sym_ptr->ecoff_sym.asym.st != (int) st_Nil
                          && ! ECOFF_IS_STAB (&sym_ptr->ecoff_sym.asym))
                        local = 0;
 
+                     /* This is just an external symbol if it is a
+                         common symbol.  */
+                     if (S_IS_COMMON (as_sym))
+                       local = 0;
+
                      /* If an st_end symbol has an associated gas
                         symbol, then it is a local label created for
                         a .bend or .end directive.  Stabs line
@@ -3906,6 +4235,16 @@ ecoff_build_symbols (backend, buf, bufend, offset)
                          sym_ptr->ecoff_sym.asym.value =
                            (S_GET_VALUE (as_sym)
                             - S_GET_VALUE (begin_ptr->as_sym));
+
+                         /* If the size is odd, this is probably a
+                             mips16 function; force it to be even.  */
+                         if ((sym_ptr->ecoff_sym.asym.value & 1) != 0)
+                           ++sym_ptr->ecoff_sym.asym.value;
+
+#ifdef S_SET_SIZE
+                         S_SET_SIZE (begin_ptr->as_sym,
+                                     sym_ptr->ecoff_sym.asym.value);
+#endif
                        }
                      else if (begin_type == st_Block
                               && sym_ptr->ecoff_sym.asym.sc != (int) sc_Info)
@@ -3954,14 +4293,13 @@ ecoff_build_symbols (backend, buf, bufend, offset)
                  /* Record the local symbol index and file number in
                     case this is an external symbol.  Note that this
                     destroys the asym.index field.  */
-                 if (as_sym != (symbolS *) NULL)
+                 if (as_sym != (symbolS *) NULL
+                     && as_sym->ecoff_symbol == sym_ptr)
                    {
-                     if (sym_ptr->ecoff_sym.asym.st == st_Proc
-                         || sym_ptr->ecoff_sym.asym.st == st_StaticProc)
-                       {
-                         know (local);
-                         sym_ptr->ecoff_sym.asym.index = isym - ifilesym - 1;
-                       }
+                     if ((sym_ptr->ecoff_sym.asym.st == st_Proc
+                          || sym_ptr->ecoff_sym.asym.st == st_StaticProc)
+                         && local)
+                       sym_ptr->ecoff_sym.asym.index = isym - ifilesym - 1;
                      sym_ptr->ecoff_sym.ifd = fil_ptr->file_index;
                    }
                }
@@ -3986,13 +4324,11 @@ ecoff_build_procs (backend, buf, bufend, offset)
   void (* const swap_pdr_out) PARAMS ((bfd *, const PDR *, PTR))
     = backend->swap_pdr_out;
   char *pdr_out;
-  int first_fil;
   long iproc;
   vlinks_t *file_link;
 
   pdr_out = *buf + offset;
-  
-  first_fil = 1;
+
   iproc = 0;
 
   /* The procedures are stored by file.  */
@@ -4042,10 +4378,11 @@ ecoff_build_procs (backend, buf, bufend, offset)
                                                S_GET_SEGMENT (adr_sym)));
                  if (first)
                    {
-                     if (first_fil)
-                       first_fil = 0;
-                     else
-                       fil_ptr->fdr.adr = adr;
+                     /* This code used to force the adr of the very
+                         first fdr to be 0.  However, the native tools
+                         don't do that, and I can't remember why it
+                         used to work that way, so I took it out.  */
+                     fil_ptr->fdr.adr = adr;
                      first = 0;
                    }
                  proc_ptr->pdr.adr = adr - fil_ptr->fdr.adr;
@@ -4079,10 +4416,10 @@ ecoff_build_aux (backend, buf, bufend, offset)
   long iaux;
   vlinks_t *file_link;
 
-  bigendian = stdoutput->xvec->header_byteorder_big_p;
+  bigendian = bfd_big_endian (stdoutput);
 
   aux_out = (union aux_ext *) (*buf + offset);
-  
+
   iaux = 0;
 
   /* The aux entries are stored by file.  */
@@ -4130,12 +4467,14 @@ ecoff_build_aux (backend, buf, bufend, offset)
                  switch (aux_ptr->type)
                    {
                    case aux_tir:
-                     ecoff_swap_tir_out (bigendian, &aux_ptr->data.ti,
-                                         &aux_out->a_ti);
+                     (*backend->swap_tir_out) (bigendian,
+                                               &aux_ptr->data.ti,
+                                               &aux_out->a_ti);
                      break;
                    case aux_rndx:
-                     ecoff_swap_rndx_out (bigendian, &aux_ptr->data.rndx,
-                                          &aux_out->a_rndx);
+                     (*backend->swap_rndx_out) (bigendian,
+                                                &aux_ptr->data.rndx,
+                                                &aux_out->a_rndx);
                      break;
                    case aux_dnLow:
                      AUX_PUT_DNLOW (bigendian, aux_ptr->data.dnLow,
@@ -4308,27 +4647,15 @@ ecoff_build_fdr (backend, buf, bufend, offset)
   return offset + ifile * external_fdr_size;
 }
 
-/* Swap out the external symbols.  These are the symbols that the
-   machine independent code has put in the symtab for the BFD.  */
+/* Set up the external symbols.  These are supposed to be handled by
+   the backend.  This routine just gets the right information and
+   calls a backend function to deal with it.  */
 
-static unsigned long
-ecoff_build_ext (backend, buf, bufend, offset, ext_strings, ext_str_hash)
-     const struct ecoff_debug_swap *backend;
-     char **buf;
-     char **bufend;
-     unsigned long offset;
-     varray_t *ext_strings;
-     struct hash_control *ext_str_hash;
+static void
+ecoff_setup_ext ()
 {
-  const bfd_size_type external_ext_size = backend->external_ext_size;
-  void (* const swap_ext_out) PARAMS ((bfd *, const EXTR *, PTR))
-    = backend->swap_ext_out;
   register symbolS *sym;
-  char *ext_out;
-  long iext;
 
-  ext_out = *buf + offset;
-  iext = 0;
   for (sym = symbol_rootP; sym != (symbolS *) NULL; sym = symbol_next (sym))
     {
       if (sym->ecoff_symbol == NULL)
@@ -4336,6 +4663,7 @@ ecoff_build_ext (backend, buf, bufend, offset, ext_strings, ext_str_hash)
 
       /* If this is a local symbol, then force the fields to zero.  */
       if (! S_IS_EXTERNAL (sym)
+         && ! S_IS_WEAK (sym)
          && S_IS_DEFINED (sym))
        {
          sym->ecoff_symbol->ecoff_sym.asym.value = 0;
@@ -4344,23 +4672,11 @@ ecoff_build_ext (backend, buf, bufend, offset, ext_strings, ext_str_hash)
          sym->ecoff_symbol->ecoff_sym.asym.index = indexNil;
        }
 
-      sym->ecoff_symbol->ecoff_sym.asym.iss =
-       add_string (ext_strings, ext_str_hash, S_GET_NAME (sym),
-                   (shash_t **) NULL);
-      if (*bufend - ext_out < external_ext_size)
-       ext_out = ecoff_add_bytes (buf, bufend, ext_out, external_ext_size);
-      (*swap_ext_out) (stdoutput, &sym->ecoff_symbol->ecoff_sym, ext_out);
-#ifdef obj_set_sym_index
-      obj_set_sym_index (as_sym->bsym, iext);
-#endif
-      ext_out += external_ext_size;
-      ++iext;
+      obj_ecoff_set_ext (sym, &sym->ecoff_symbol->ecoff_sym);
     }
-
-  return offset + iext * external_ext_size;
 }
 
-/* Build the ECOFF dbeugging information.  */
+/* Build the ECOFF debugging information.  */
 
 unsigned long
 ecoff_build_debug (hdr, bufp, backend)
@@ -4379,16 +4695,10 @@ ecoff_build_debug (hdr, bufp, backend)
   char *buf;
   char *bufend;
   unsigned long offset;
-  char *extbuf;
-  char *extbufend;
-  unsigned long extoffset;
-  varray_t ext_strings;
-  static varray_t init_ext_strings = INIT_VARRAY (char);
-  struct hash_control *ext_str_hash;
 
   /* Make sure we have a file.  */
   if (first_file == (efdr_t *) NULL)
-    add_file ((const char *) NULL, 0);
+    add_file ((const char *) NULL, 0, 1);
 
   /* Handle any top level tags.  */
   for (ptag = top_tag_head->first_tag;
@@ -4419,7 +4729,7 @@ ecoff_build_debug (hdr, bufp, backend)
 
       cur_file_ptr = sym->ecoff_file;
       add_ecoff_symbol ((const char *) NULL, st_Nil, sc_Nil, sym,
-                       S_GET_VALUE (sym), indexNil);
+                       (bfd_vma) 0, S_GET_VALUE (sym), indexNil);
     }
   cur_proc_ptr = hold_proc_ptr;
   cur_file_ptr = hold_file_ptr;
@@ -4433,20 +4743,23 @@ ecoff_build_debug (hdr, bufp, backend)
        fil_ptr = fil_ptr->next_file)
     {
       cur_file_ptr = fil_ptr;
-      while (cur_file_ptr->cur_scope->prev != (scope_t *) NULL)
+      while (cur_file_ptr->cur_scope != (scope_t *) NULL
+            && cur_file_ptr->cur_scope->prev != (scope_t *) NULL)
        {
          cur_file_ptr->cur_scope = cur_file_ptr->cur_scope->prev;
-         if (! end_warning)
+         if (! end_warning && ! cur_file_ptr->fake)
            {
              as_warn ("Missing .end or .bend at end of file");
              end_warning = 1;
            }
        }
-      (void) add_ecoff_symbol ((const char *) NULL,
-                              st_End, sc_Text,
-                              (symbolS *) NULL,
-                              (symint_t) 0,
-                              (symint_t) 0);
+      if (cur_file_ptr->cur_scope != (scope_t *) NULL)
+       (void) add_ecoff_symbol ((const char *) NULL,
+                                st_End, sc_Text,
+                                (symbolS *) NULL,
+                                (bfd_vma) 0,
+                                (symint_t) 0,
+                                (symint_t) 0);
     }
 
   /* Build the symbolic information.  */
@@ -4497,24 +4810,6 @@ ecoff_build_debug (hdr, bufp, backend)
   offset = ecoff_build_ss (backend, &buf, &bufend, offset);
   hdr->issMax = offset - hdr->cbSsOffset;
 
-  /* Build the external symbols and external strings.  We build these
-     now because we want to copy out the external strings now.  We
-     copy out the external symbol information down below.  */
-  extbuf = xmalloc (PAGE_SIZE);
-  extbufend = extbuf + PAGE_SIZE;
-  ext_strings = init_ext_strings;
-  ext_str_hash = hash_new ();
-  extoffset = ecoff_build_ext (backend, &extbuf, &extbufend,
-                              (unsigned long) 0, &ext_strings,
-                              ext_str_hash);
-
-  /* Copy out the external strings.  */
-  hdr->cbSsExtOffset = offset;
-  offset += ecoff_build_strings (&buf, &bufend, offset, &ext_strings);
-  offset = ecoff_padding_adjust (backend, &buf, &bufend, offset,
-                                (char **) NULL);
-  hdr->issExtMax = offset - hdr->cbSsExtOffset;
-
   /* We don't use relative file descriptors.  */
   hdr->crfd = 0;
   hdr->cbRfdOffset = 0;
@@ -4524,19 +4819,23 @@ ecoff_build_debug (hdr, bufp, backend)
   offset = ecoff_build_fdr (backend, &buf, &bufend, offset);
   hdr->ifdMax = (offset - hdr->cbFdOffset) / backend->external_fdr_size;
 
-  /* Copy out the external symbols.  */
-  hdr->cbExtOffset = offset;
-  if (bufend - (buf + offset) < extoffset)
-    (void) ecoff_add_bytes (&buf, &bufend, buf + offset, extoffset);
-  memcpy (buf + offset, extbuf, extoffset);
-  offset += extoffset;
-  hdr->iextMax = (offset - hdr->cbExtOffset) / backend->external_ext_size;
+  /* Set up the external symbols, which are handled by the BFD back
+     end.  */
+  hdr->issExtMax = 0;
+  hdr->cbSsExtOffset = 0;
+  hdr->iextMax = 0;
+  hdr->cbExtOffset = 0;
+  ecoff_setup_ext ();
 
   know ((offset & (backend->debug_align - 1)) == 0);
 
-  hdr->magic = backend->sym_magic;
-  /* FIXME: what should hdr->vstamp be?  */
+  /* FIXME: This value should be determined from the .verstamp directive,
+     with reasonable defaults in config files.  */
+#ifdef TC_ALPHA
+  hdr->vstamp = 0x030b;
+#else
   hdr->vstamp = 0x020b;
+#endif
 
   *bufp = buf;
   return offset;
@@ -4931,4 +5230,201 @@ allocate_lineno_list ()
   return ptr;
 }
 
+void
+ecoff_set_gp_prolog_size (sz)
+     int sz;
+{
+  if (cur_proc_ptr == 0)
+    return;
+
+  cur_proc_ptr->pdr.gp_prologue = sz;
+  if (cur_proc_ptr->pdr.gp_prologue != sz)
+    {
+      as_warn ("GP prologue size exceeds field size, using 0 instead");
+      cur_proc_ptr->pdr.gp_prologue = 0;
+    }
+
+  cur_proc_ptr->pdr.gp_used = 1;
+}
+
+static void
+generate_ecoff_stab (what, string, type, other, desc)
+     int what;
+     const char *string;
+     int type;
+     int other;
+     int desc;
+{
+  efdr_t *save_file_ptr = cur_file_ptr;
+  symbolS *sym;
+  symint_t value;
+  st_t st;
+  sc_t sc;
+  symint_t indx;
+  localsym_t *hold = NULL;
+
+  /* We don't handle .stabd.  */
+  if (what != 's' && what != 'n')
+    {
+      as_bad (".stab%c is not supported", what);
+      return;
+    }
+
+  /* We ignore the other field.  */
+  if (other != 0)
+    as_warn (".stab%c: ignoring non-zero other field", what);
+
+  /* Make sure we have a current file.  */
+  if (cur_file_ptr == (efdr_t *) NULL)
+    {
+      add_file ((const char *) NULL, 0, 1);
+      save_file_ptr = cur_file_ptr;
+    }
+
+  /* For stabs in ECOFF, the first symbol must be @stabs.  This is a
+     signal to gdb.  */
+  if (stabs_seen == 0)
+    mark_stabs (0);
+
+  /* Line number stabs are handled differently, since they have two
+     values, the line number and the address of the label.  We use the
+     index field (aka desc) to hold the line number, and the value
+     field to hold the address.  The symbol type is st_Label, which
+     should be different from the other stabs, so that gdb can
+     recognize it.  */
+  if (type == N_SLINE)
+    {
+      SYMR dummy_symr;
+
+#ifndef NO_LISTING
+      if (listing)
+       listing_source_line ((unsigned int) desc);
+#endif
+
+      dummy_symr.index = desc;
+      if (dummy_symr.index != desc)
+       {
+         as_warn ("Line number (%d) for .stab%c directive cannot fit in index field (20 bits)",
+                  desc, what);
+         return;
+       }
+
+      sym = symbol_find_or_make ((char *)string);
+      value = 0;
+      st = st_Label;
+      sc = sc_Text;
+      indx = desc;
+    }
+  else
+    {
+#ifndef NO_LISTING
+      if (listing && (type == N_SO || type == N_SOL))
+       listing_source_file (string);
+#endif
+
+      sym = symbol_find_or_make ((char *)string);
+      sc = sc_Nil;
+      st = st_Nil;
+      value = 0;
+      indx = ECOFF_MARK_STAB (type);
+    }
+
+  /* Don't store the stabs symbol we are creating as the type of the
+     ECOFF symbol.  We want to compute the type of the ECOFF symbol
+     independently.  */
+  if (sym != (symbolS *) NULL)
+    hold = sym->ecoff_symbol;
+
+  (void) add_ecoff_symbol (string, st, sc, sym, (bfd_vma) 0, value, indx);
+
+  if (sym != (symbolS *) NULL)
+    sym->ecoff_symbol = hold;
+
+  /* Restore normal file type.  */
+  cur_file_ptr = save_file_ptr;
+}
+
+int 
+ecoff_no_current_file ()
+{
+  return cur_file_ptr == (efdr_t *) NULL;
+}
+
+void
+ecoff_generate_asm_lineno (filename, lineno)
+     const char *filename;
+     int lineno;
+{
+  lineno_list_t *list;
+
+  /* this potential can cause problem, when we start to see stab half the 
+     way thru the file */
+/*
+  if (stabs_seen)
+    ecoff_generate_asm_line_stab(filename, lineno);
+*/
+
+  if (current_stabs_filename == (char *)NULL || strcmp (current_stabs_filename, filename))
+    {
+      add_file (filename, 0, 1);
+      generate_asm_lineno = 1;
+    }
+
+  list = allocate_lineno_list ();
+
+  list->next = (lineno_list_t *) NULL;
+  list->file = cur_file_ptr;
+  list->proc = cur_proc_ptr;
+  list->frag = frag_now;
+  list->paddr = frag_now_fix ();
+  list->lineno = lineno;
+
+  /* We don't want to merge files which have line numbers.  */
+  cur_file_ptr->fdr.fMerge = 0;
+
+  /* A .loc directive will sometimes appear before a .ent directive,
+     which means that cur_proc_ptr will be NULL here.  Arrange to
+     patch this up.  */
+  if (cur_proc_ptr == (proc_t *) NULL)
+    {
+      lineno_list_t **pl;
+
+      pl = &noproc_lineno;
+      while (*pl != (lineno_list_t *) NULL)
+        pl = &(*pl)->next;
+      *pl = list;
+    }
+  else
+    {
+      last_lineno = list;
+      *last_lineno_ptr = list;
+      last_lineno_ptr = &list->next;
+    }
+}
+
+static int line_label_cnt = 0;
+void
+ecoff_generate_asm_line_stab (filename, lineno)
+    char *filename;
+    int lineno;
+{
+  char *ll;
+
+  if (strcmp (current_stabs_filename, filename)) 
+    {
+      add_file (filename, 0, 1);
+      generate_asm_lineno = 1;
+    }
+
+  line_label_cnt++;
+  /* generate local label $LMnn */
+  ll = xmalloc(10);
+  sprintf(ll, "$LM%d", line_label_cnt);
+  colon (ll);
+
+  /* generate stab for the line */
+  generate_ecoff_stab ('n', ll, N_SLINE, 0, lineno); 
+
+}
+
 #endif /* ECOFF_DEBUGGING */
This page took 0.0455 seconds and 4 git commands to generate.