This commit was generated by cvs2svn to track changes on a CVS vendor
[binutils-gdb.git] / gas / config / obj-aout.c
index 944d8e057a8e2091d167013895652a437223fb1e..4b4f4d80eb32dc18e94349c86acf5ba47d15bd19 100644 (file)
@@ -1,5 +1,6 @@
 /* a.out object file format
-   Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
+   Copyright (C) 1989, 90, 91, 92, 93, 94, 95, 96, 97, 98, 1999
+   Free Software Foundation, Inc.
 
 This file is part of GAS, the GNU Assembler.
 
@@ -13,9 +14,10 @@ WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 the 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. */
+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, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA. */
 
 #include "as.h"
 #ifdef BFD_ASSEMBLER
@@ -62,12 +64,18 @@ const segT N_TYPE_seg[N_TYPE + 2] =
 #endif
 
 static void obj_aout_line PARAMS ((int));
+static void obj_aout_weak PARAMS ((int));
+static void obj_aout_type PARAMS ((int));
 
 const pseudo_typeS obj_pseudo_table[] =
 {
   {"line", obj_aout_line, 0},  /* source code line number */
   {"ln", obj_aout_line, 0},    /* coff line number that we use anyway */
 
+  {"weak", obj_aout_weak, 0},  /* mark symbol as weak.  */
+
+  {"type", obj_aout_type, 0},
+
   /* coff debug pseudos (ignored) */
   {"def", s_ignore, 0},
   {"dim", s_ignore, 0},
@@ -78,7 +86,6 @@ const pseudo_typeS obj_pseudo_table[] =
   {"scl", s_ignore, 0},
   {"size", s_ignore, 0},
   {"tag", s_ignore, 0},
-  {"type", s_ignore, 0},
   {"val", s_ignore, 0},
   {"version", s_ignore, 0},
 
@@ -102,18 +109,21 @@ obj_aout_frob_symbol (sym, punt)
   asection *sec;
   int desc, type, other;
 
-  flags = sym->bsym->flags;
+  flags = symbol_get_bfdsym (sym)->flags;
   desc = S_GET_DESC (sym);
   type = S_GET_TYPE (sym);
   other = S_GET_OTHER (sym);
-  sec = sym->bsym->section;
+  sec = S_GET_SEGMENT (sym);
 
   /* Only frob simple symbols this way right now.  */
   if (! (type & ~ (N_TYPE | N_EXT)))
     {
       if (type == (N_UNDF | N_EXT)
          && sec == &bfd_abs_section)
-       sym->bsym->section = sec = &bfd_und_section;
+       {
+         sec = bfd_und_section_ptr;
+         S_SET_SEGMENT (sym, sec);
+       }
 
       if ((type & N_TYPE) != N_INDR
          && (type & N_TYPE) != N_SETA
@@ -135,30 +145,57 @@ obj_aout_frob_symbol (sym, punt)
        case N_SETB:
          /* Set the debugging flag for constructor symbols so that
             BFD leaves them alone.  */
-         sym->bsym->flags |= BSF_DEBUGGING;
+         symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
+
+         /* You can't put a common symbol in a set.  The way a set
+            element works is that the symbol has a definition and a
+            name, and the linker adds the definition to the set of
+            that name.  That does not work for a common symbol,
+            because the linker can't tell which common symbol the
+            user means.  FIXME: Using as_bad here may be
+            inappropriate, since the user may want to force a
+            particular type without regard to the semantics of sets;
+            on the other hand, we certainly don't want anybody to be
+            mislead into thinking that their code will work.  */
+         if (S_IS_COMMON (sym))
+           as_bad (_("Attempt to put a common symbol into set %s"),
+                   S_GET_NAME (sym));
+         /* Similarly, you can't put an undefined symbol in a set.  */
+         else if (! S_IS_DEFINED (sym))
+           as_bad (_("Attempt to put an undefined symbol into set %s"),
+                   S_GET_NAME (sym));
+
          break;
        case N_INDR:
          /* Put indirect symbols in the indirect section.  */
-         sym->bsym->section = &bfd_ind_section;
-         sym->bsym->flags |= BSF_INDIRECT;
+         S_SET_SEGMENT (sym, bfd_ind_section_ptr);
+         symbol_get_bfdsym (sym)->flags |= BSF_INDIRECT;
          if (type & N_EXT)
            {
-             sym->bsym->flags |= BSF_EXPORT;
-             sym->bsym->flags &=~ BSF_LOCAL;
+             symbol_get_bfdsym (sym)->flags |= BSF_EXPORT;
+             symbol_get_bfdsym (sym)->flags &=~ BSF_LOCAL;
            }
          break;
        case N_WARNING:
          /* Mark warning symbols.  */
-         sym->bsym->flags |= BSF_WARNING;
+         symbol_get_bfdsym (sym)->flags |= BSF_WARNING;
          break;
        }
     }
   else
     {
-      sym->bsym->flags |= BSF_DEBUGGING;
+      symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
     }
 
   S_SET_TYPE (sym, type);
+
+  /* Double check weak symbols.  */
+  if (S_IS_WEAK (sym))
+    {
+      if (S_IS_COMMON (sym))
+       as_bad (_("Symbol `%s' can not be both weak and common"),
+               S_GET_NAME (sym));
+    }
 }
 
 void
@@ -182,7 +219,7 @@ obj_aout_frob_file ()
   assert (x == true);
 }
 
-#else
+#else /* ! BFD_ASSEMBLER */
 
 /* Relocation. */
 
@@ -200,6 +237,26 @@ obj_emit_relocations (where, fixP, segment_address_in_file)
   for (; fixP; fixP = fixP->fx_next)
     if (fixP->fx_done == 0)
       {
+       symbolS *sym;
+
+       sym = fixP->fx_addsy;
+       while (sym->sy_value.X_op == O_symbol
+              && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
+         sym = sym->sy_value.X_add_symbol;
+       fixP->fx_addsy = sym;
+
+       if (! sym->sy_resolved && ! S_IS_DEFINED (sym))
+         {
+           char *file;
+           unsigned int line;
+
+           if (expr_symbol_where (sym, &file, &line))
+             as_bad_where (file, line, _("unresolved relocation"));
+           else
+             as_bad (_("bad relocation: symbol `%s' not in symbol table"),
+                     S_GET_NAME (sym));
+         }
+
        tc_aout_fix_to_chars (*where, fixP, segment_address_in_file);
        *where += md_reloc_size;
       }
@@ -238,7 +295,7 @@ obj_header_append (where, headers)
 #endif /* CROSS_COMPILE */
 
 }
-#endif
+#endif /* ! defined (obj_header_append) */
 
 void
 obj_symbol_to_chars (where, symbolP)
@@ -273,6 +330,20 @@ obj_emit_symbols (where, symbol_rootP)
       if (!S_IS_DEBUG (symbolP) && !S_IS_DEFINED (symbolP))
        S_SET_EXTERNAL (symbolP);
 
+      /* Adjust the type of a weak symbol.  */
+      if (S_GET_WEAK (symbolP))
+       {
+         switch (S_GET_TYPE (symbolP))
+           {
+           case N_UNDF: S_SET_TYPE (symbolP, N_WEAKU); break;
+           case N_ABS:  S_SET_TYPE (symbolP, N_WEAKA); break;
+           case N_TEXT: S_SET_TYPE (symbolP, N_WEAKT); break;
+           case N_DATA: S_SET_TYPE (symbolP, N_WEAKD); break;
+           case N_BSS:  S_SET_TYPE (symbolP, N_WEAKB); break;
+           default: as_bad (_("%s: bad type for weak symbol"), temp); break;
+           }
+       }
+
       obj_symbol_to_chars (where, symbolP);
       S_SET_NAME (symbolP, temp);
     }
@@ -291,6 +362,75 @@ obj_aout_line (ignore)
   demand_empty_rest_of_line ();
 }                              /* obj_aout_line() */
 
+/* Handle .weak.  This is a GNU extension.  */
+
+static void
+obj_aout_weak (ignore)
+     int ignore;
+{
+  char *name;
+  int c;
+  symbolS *symbolP;
+
+  do
+    {
+      name = input_line_pointer;
+      c = get_symbol_end ();
+      symbolP = symbol_find_or_make (name);
+      *input_line_pointer = c;
+      SKIP_WHITESPACE ();
+      S_SET_WEAK (symbolP);
+      if (c == ',')
+       {
+         input_line_pointer++;
+         SKIP_WHITESPACE ();
+         if (*input_line_pointer == '\n')
+           c = '\n';
+       }
+    }
+  while (c == ',');
+  demand_empty_rest_of_line ();
+}
+
+/* Handle .type.  On {Net,Open}BSD, this is used to set the n_other field,
+   which is then apparently used when doing dynamic linking.  Older
+   versions ogas ignored the .type pseudo-op, so we also ignore it if
+   we can't parse it.  */
+
+static void
+obj_aout_type (ignore)
+     int ignore;
+{
+  char *name;
+  int c;
+  symbolS *sym;
+
+  name = input_line_pointer;
+  c = get_symbol_end ();
+  sym = symbol_find (name);
+  *input_line_pointer = c;
+  if (sym != NULL)
+    {
+      SKIP_WHITESPACE ();
+      if (*input_line_pointer == ',')
+       {
+         ++input_line_pointer;
+         SKIP_WHITESPACE ();
+         if (*input_line_pointer == '@')
+           {
+             ++input_line_pointer;
+             if (strncmp (input_line_pointer, "object", 6) == 0)
+               S_SET_OTHER (sym, 1);
+             else if (strncmp (input_line_pointer, "function", 8) == 0)
+               S_SET_OTHER (sym, 2);
+           }
+       }
+    }
+
+  /* Ignore everything else on the line.  */
+  s_ignore (0);
+}
+
 void
 obj_read_begin_hook ()
 {
@@ -311,12 +451,30 @@ obj_crawl_symbol_chain (headers)
   symbolPP = &symbol_rootP;    /*->last symbol chain link. */
   while ((symbolP = *symbolPP) != NULL)
     {
+      if (symbolP->sy_mri_common)
+       {
+         if (S_IS_EXTERNAL (symbolP))
+           as_bad (_("%s: global symbols not supported in common sections"),
+                   S_GET_NAME (symbolP));
+         *symbolPP = symbol_next (symbolP);
+         continue;
+       }
+
       if (flag_readonly_data_in_text && (S_GET_SEGMENT (symbolP) == SEG_DATA))
        {
          S_SET_SEGMENT (symbolP, SEG_TEXT);
        }                       /* if pusing data into text */
 
-      resolve_symbol_value (symbolP);
+      resolve_symbol_value (symbolP, 1);
+
+      /* Skip symbols which were equated to undefined or common
+        symbols.  */
+      if (symbolP->sy_value.X_op == O_symbol
+         && (! S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP)))
+       {
+         *symbolPP = symbol_next (symbolP);
+         continue;
+       }
 
       /* OK, here is how we decide which symbols go out into the brave
         new symtab.  Symbols that do are:
@@ -354,7 +512,7 @@ obj_crawl_symbol_chain (headers)
            }
          else                  /* .Stabd case. */
            symbolP->sy_name_offset = 0;
-         symbolPP = &(symbol_next (symbolP));
+         symbolPP = &symbolP->sy_next;
        }
       else
        {
@@ -363,7 +521,7 @@ obj_crawl_symbol_chain (headers)
               Well, maybe if you're doing twisted things with
               register names...  */
            {
-             as_bad ("Local symbol %s never defined.", decode_local_label_name (S_GET_NAME (symbolP)));
+             as_bad (_("Local symbol %s never defined."), decode_local_label_name (S_GET_NAME (symbolP)));
            }                   /* oops. */
 
          /* Unhook it from the chain */
@@ -451,7 +609,7 @@ DEFUN_VOID (s_sect)
     }
   if (exp >= 1000)
     {
-      as_bad ("subsegment index too high");
+      as_bad (_("subsegment index too high"));
     }
 
   if (strcmp (section_name, ".text") == 0)