obj-coff.c revision 33965
144743Smarkm/* coff object file format
244743Smarkm   Copyright (C) 1989, 90, 91, 92, 93, 94, 95, 96, 1997
344743Smarkm   Free Software Foundation, Inc.
444743Smarkm
544743Smarkm   This file is part of GAS.
644743Smarkm
744743Smarkm   GAS is free software; you can redistribute it and/or modify
844743Smarkm   it under the terms of the GNU General Public License as published by
9277281Spfg   the Free Software Foundation; either version 2, or (at your option)
10277281Spfg   any later version.
11277281Spfg
1244743Smarkm   GAS is distributed in the hope that it will be useful,
1344743Smarkm   but WITHOUT ANY WARRANTY; without even the implied warranty of
14277281Spfg   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15277281Spfg   GNU General Public License for more details.
16277281Spfg
1744743Smarkm   You should have received a copy of the GNU General Public License
1844743Smarkm   along with GAS; see the file COPYING.  If not, write to the Free
1944743Smarkm   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20   02111-1307, USA.  */
21
22#define OBJ_HEADER "obj-coff.h"
23
24#include "as.h"
25#include "obstack.h"
26#include "subsegs.h"
27
28/* I think this is probably always correct.  */
29#ifndef KEEP_RELOC_INFO
30#define KEEP_RELOC_INFO
31#endif
32
33const char *s_get_name PARAMS ((symbolS * s));
34static symbolS *def_symbol_in_progress;
35
36
37/* stack stuff */
38typedef struct
39  {
40    unsigned long chunk_size;
41    unsigned long element_size;
42    unsigned long size;
43    char *data;
44    unsigned long pointer;
45  }
46stack;
47
48static stack *
49stack_init (chunk_size, element_size)
50     unsigned long chunk_size;
51     unsigned long element_size;
52{
53  stack *st;
54
55  st = (stack *) malloc (sizeof (stack));
56  if (!st)
57    return 0;
58  st->data = malloc (chunk_size);
59  if (!st->data)
60    {
61      free (st);
62      return 0;
63    }
64  st->pointer = 0;
65  st->size = chunk_size;
66  st->chunk_size = chunk_size;
67  st->element_size = element_size;
68  return st;
69}
70
71#if 0
72/* Not currently used.  */
73static void
74stack_delete (st)
75     stack *st;
76{
77  free (st->data);
78  free (st);
79}
80#endif
81
82static char *
83stack_push (st, element)
84     stack *st;
85     char *element;
86{
87  if (st->pointer + st->element_size >= st->size)
88    {
89      st->size += st->chunk_size;
90      if ((st->data = xrealloc (st->data, st->size)) == (char *) 0)
91	return (char *) 0;
92    }
93  memcpy (st->data + st->pointer, element, st->element_size);
94  st->pointer += st->element_size;
95  return st->data + st->pointer;
96}
97
98static char *
99stack_pop (st)
100     stack *st;
101{
102  if (st->pointer < st->element_size)
103    {
104      st->pointer = 0;
105      return (char *) 0;
106    }
107  st->pointer -= st->element_size;
108  return st->data + st->pointer;
109}
110
111/*
112 * Maintain a list of the tagnames of the structres.
113 */
114
115static struct hash_control *tag_hash;
116
117static void
118tag_init ()
119{
120  tag_hash = hash_new ();
121}
122
123static void
124tag_insert (name, symbolP)
125     const char *name;
126     symbolS *symbolP;
127{
128  const char *error_string;
129
130  if ((error_string = hash_jam (tag_hash, name, (char *) symbolP)))
131    {
132      as_fatal ("Inserting \"%s\" into structure table failed: %s",
133		name, error_string);
134    }
135}
136
137static symbolS *
138tag_find (name)
139     char *name;
140{
141#ifdef STRIP_UNDERSCORE
142  if (*name == '_')
143    name++;
144#endif /* STRIP_UNDERSCORE */
145  return (symbolS *) hash_find (tag_hash, name);
146}
147
148static symbolS *
149tag_find_or_make (name)
150     char *name;
151{
152  symbolS *symbolP;
153
154  if ((symbolP = tag_find (name)) == NULL)
155    {
156      symbolP = symbol_new (name, undefined_section,
157			    0, &zero_address_frag);
158
159      tag_insert (S_GET_NAME (symbolP), symbolP);
160#ifdef BFD_ASSEMBLER
161      symbol_table_insert (symbolP);
162#endif
163    }				/* not found */
164
165  return symbolP;
166}
167
168
169
170#ifdef BFD_ASSEMBLER
171
172static void SA_SET_SYM_TAGNDX PARAMS ((symbolS *, symbolS *));
173
174#define GET_FILENAME_STRING(X) \
175((char*)(&((X)->sy_symbol.ost_auxent->x_file.x_n.x_offset))[1])
176
177/* @@ Ick.  */
178static segT
179fetch_coff_debug_section ()
180{
181  static segT debug_section;
182  if (!debug_section)
183    {
184      CONST asymbol *s;
185      s = bfd_make_debug_symbol (stdoutput, (char *) 0, 0);
186      assert (s != 0);
187      debug_section = s->section;
188    }
189  return debug_section;
190}
191
192void
193SA_SET_SYM_ENDNDX (sym, val)
194     symbolS *sym;
195     symbolS *val;
196{
197  combined_entry_type *entry, *p;
198
199  entry = &coffsymbol (sym->bsym)->native[1];
200  p = coffsymbol (val->bsym)->native;
201  entry->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = p;
202  entry->fix_end = 1;
203}
204
205static void
206SA_SET_SYM_TAGNDX (sym, val)
207     symbolS *sym;
208     symbolS *val;
209{
210  combined_entry_type *entry, *p;
211
212  entry = &coffsymbol (sym->bsym)->native[1];
213  p = coffsymbol (val->bsym)->native;
214  entry->u.auxent.x_sym.x_tagndx.p = p;
215  entry->fix_tag = 1;
216}
217
218static int
219S_GET_DATA_TYPE (sym)
220     symbolS *sym;
221{
222  return coffsymbol (sym->bsym)->native->u.syment.n_type;
223}
224
225int
226S_SET_DATA_TYPE (sym, val)
227     symbolS *sym;
228     int val;
229{
230  coffsymbol (sym->bsym)->native->u.syment.n_type = val;
231  return val;
232}
233
234int
235S_GET_STORAGE_CLASS (sym)
236     symbolS *sym;
237{
238  return coffsymbol (sym->bsym)->native->u.syment.n_sclass;
239}
240
241int
242S_SET_STORAGE_CLASS (sym, val)
243     symbolS *sym;
244     int val;
245{
246  coffsymbol (sym->bsym)->native->u.syment.n_sclass = val;
247  return val;
248}
249
250/* Merge a debug symbol containing debug information into a normal symbol. */
251
252void
253c_symbol_merge (debug, normal)
254     symbolS *debug;
255     symbolS *normal;
256{
257  S_SET_DATA_TYPE (normal, S_GET_DATA_TYPE (debug));
258  S_SET_STORAGE_CLASS (normal, S_GET_STORAGE_CLASS (debug));
259
260  if (S_GET_NUMBER_AUXILIARY (debug) > S_GET_NUMBER_AUXILIARY (normal))
261    /* take the most we have */
262    S_SET_NUMBER_AUXILIARY (normal, S_GET_NUMBER_AUXILIARY (debug));
263
264  if (S_GET_NUMBER_AUXILIARY (debug) > 0)
265    {
266      /* Move all the auxiliary information.  */
267      /* @@ How many fields do we want to preserve?  Would it make more
268	 sense to pick and choose those we want to copy?  Should look
269	 into this further....  [raeburn:19920512.2209EST]  */
270      alent *linenos;
271      linenos = coffsymbol (normal->bsym)->lineno;
272      memcpy ((char *) &coffsymbol (normal->bsym)->native,
273	      (char *) &coffsymbol (debug->bsym)->native,
274	      S_GET_NUMBER_AUXILIARY(debug) * AUXESZ);
275      coffsymbol (normal->bsym)->lineno = linenos;
276    }
277
278  /* Move the debug flags. */
279  SF_SET_DEBUG_FIELD (normal, SF_GET_DEBUG_FIELD (debug));
280}
281
282void
283c_dot_file_symbol (filename)
284     char *filename;
285{
286  symbolS *symbolP;
287
288  symbolP = symbol_new (filename, bfd_abs_section_ptr, 0, &zero_address_frag);
289
290  S_SET_STORAGE_CLASS (symbolP, C_FILE);
291  S_SET_NUMBER_AUXILIARY (symbolP, 1);
292
293  symbolP->bsym->flags = BSF_DEBUGGING;
294
295#ifndef NO_LISTING
296  {
297    extern int listing;
298    if (listing)
299      {
300	listing_source_file (filename);
301      }
302  }
303#endif
304
305  /* Make sure that the symbol is first on the symbol chain */
306  if (symbol_rootP != symbolP)
307    {
308      symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
309      symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP);
310    }				/* if not first on the list */
311}
312
313/* Line number handling */
314
315struct line_no {
316  struct line_no *next;
317  fragS *frag;
318  alent l;
319};
320
321int coff_line_base;
322
323/* Symbol of last function, which we should hang line#s off of.  */
324static symbolS *line_fsym;
325
326#define in_function()		(line_fsym != 0)
327#define clear_function()	(line_fsym = 0)
328#define set_function(F)		(line_fsym = (F), coff_add_linesym (F))
329
330
331void
332coff_obj_symbol_new_hook (symbolP)
333     symbolS *symbolP;
334{
335  char underscore = 0;		/* Symbol has leading _ */
336
337  {
338    long sz = (OBJ_COFF_MAX_AUXENTRIES + 1) * sizeof (combined_entry_type);
339    char *s = (char *) xmalloc (sz);
340    memset (s, 0, sz);
341    coffsymbol (symbolP->bsym)->native = (combined_entry_type *) s;
342  }
343  S_SET_DATA_TYPE (symbolP, T_NULL);
344  S_SET_STORAGE_CLASS (symbolP, 0);
345  S_SET_NUMBER_AUXILIARY (symbolP, 0);
346
347  if (S_IS_STRING (symbolP))
348    SF_SET_STRING (symbolP);
349  if (!underscore && S_IS_LOCAL (symbolP))
350    SF_SET_LOCAL (symbolP);
351}
352
353
354/*
355 * Handle .ln directives.
356 */
357
358static symbolS *current_lineno_sym;
359static struct line_no *line_nos;
360/* @@ Blindly assume all .ln directives will be in the .text section...  */
361int coff_n_line_nos;
362
363static void
364add_lineno (frag, offset, num)
365     fragS *frag;
366     int offset;
367     int num;
368{
369  struct line_no *new_line =
370    (struct line_no *) xmalloc (sizeof (struct line_no));
371  if (!current_lineno_sym)
372    {
373      abort ();
374    }
375  new_line->next = line_nos;
376  new_line->frag = frag;
377  new_line->l.line_number = num;
378  new_line->l.u.offset = offset;
379  line_nos = new_line;
380  coff_n_line_nos++;
381}
382
383void
384coff_add_linesym (sym)
385     symbolS *sym;
386{
387  if (line_nos)
388    {
389      coffsymbol (current_lineno_sym->bsym)->lineno = (alent *) line_nos;
390      coff_n_line_nos++;
391      line_nos = 0;
392    }
393  current_lineno_sym = sym;
394}
395
396static void
397obj_coff_ln (appline)
398     int appline;
399{
400  int l;
401
402  if (! appline && def_symbol_in_progress != NULL)
403    {
404      as_warn (".ln pseudo-op inside .def/.endef: ignored.");
405      demand_empty_rest_of_line ();
406      return;
407    }
408
409  l = get_absolute_expression ();
410  if (!appline)
411    {
412      add_lineno (frag_now, frag_now_fix (), l);
413    }
414
415  if (appline)
416    new_logical_line ((char *) NULL, l - 1);
417
418#ifndef NO_LISTING
419  {
420    extern int listing;
421
422    if (listing)
423      {
424	if (! appline)
425	  l += coff_line_base - 1;
426	listing_source_line (l);
427      }
428  }
429#endif
430
431  demand_empty_rest_of_line ();
432}
433
434/*
435 *			def()
436 *
437 * Handle .def directives.
438 *
439 * One might ask : why can't we symbol_new if the symbol does not
440 * already exist and fill it with debug information.  Because of
441 * the C_EFCN special symbol. It would clobber the value of the
442 * function symbol before we have a chance to notice that it is
443 * a C_EFCN. And a second reason is that the code is more clear this
444 * way. (at least I think it is :-).
445 *
446 */
447
448#define SKIP_SEMI_COLON()	while (*input_line_pointer++ != ';')
449#define SKIP_WHITESPACES()	while (*input_line_pointer == ' ' || \
450				       *input_line_pointer == '\t') \
451    input_line_pointer++;
452
453static void
454obj_coff_def (what)
455     int what;
456{
457  char name_end;		/* Char after the end of name */
458  char *symbol_name;		/* Name of the debug symbol */
459  char *symbol_name_copy;	/* Temporary copy of the name */
460  unsigned int symbol_name_length;
461
462  if (def_symbol_in_progress != NULL)
463    {
464      as_warn (".def pseudo-op used inside of .def/.endef: ignored.");
465      demand_empty_rest_of_line ();
466      return;
467    }				/* if not inside .def/.endef */
468
469  SKIP_WHITESPACES ();
470
471  symbol_name = input_line_pointer;
472#ifdef STRIP_UNDERSCORE
473  if (symbol_name[0] == '_' && symbol_name[1] != 0)
474    symbol_name++;
475#endif /* STRIP_UNDERSCORE */
476
477  name_end = get_symbol_end ();
478  symbol_name_length = strlen (symbol_name);
479  symbol_name_copy = xmalloc (symbol_name_length + 1);
480  strcpy (symbol_name_copy, symbol_name);
481#ifdef tc_canonicalize_symbol_name
482  symbol_name_copy = tc_canonicalize_symbol_name (symbol_name_copy);
483#endif
484
485  /* Initialize the new symbol */
486  def_symbol_in_progress = symbol_make (symbol_name_copy);
487  def_symbol_in_progress->sy_frag = &zero_address_frag;
488  S_SET_VALUE (def_symbol_in_progress, 0);
489
490  if (S_IS_STRING (def_symbol_in_progress))
491    SF_SET_STRING (def_symbol_in_progress);
492
493  *input_line_pointer = name_end;
494
495  demand_empty_rest_of_line ();
496}
497
498unsigned int dim_index;
499
500static void
501obj_coff_endef (ignore)
502     int ignore;
503{
504  symbolS *symbolP;
505  /* DIM BUG FIX sac@cygnus.com */
506  dim_index = 0;
507  if (def_symbol_in_progress == NULL)
508    {
509      as_warn (".endef pseudo-op used outside of .def/.endef: ignored.");
510      demand_empty_rest_of_line ();
511      return;
512    }				/* if not inside .def/.endef */
513
514  /* Set the section number according to storage class. */
515  switch (S_GET_STORAGE_CLASS (def_symbol_in_progress))
516    {
517    case C_STRTAG:
518    case C_ENTAG:
519    case C_UNTAG:
520      SF_SET_TAG (def_symbol_in_progress);
521      /* intentional fallthrough */
522    case C_FILE:
523    case C_TPDEF:
524      SF_SET_DEBUG (def_symbol_in_progress);
525      S_SET_SEGMENT (def_symbol_in_progress, fetch_coff_debug_section ());
526      break;
527
528    case C_EFCN:
529      SF_SET_LOCAL (def_symbol_in_progress);	/* Do not emit this symbol. */
530      /* intentional fallthrough */
531    case C_BLOCK:
532      SF_SET_PROCESS (def_symbol_in_progress);	/* Will need processing before writing */
533      /* intentional fallthrough */
534    case C_FCN:
535      {
536	CONST char *name;
537	S_SET_SEGMENT (def_symbol_in_progress, text_section);
538
539	name = bfd_asymbol_name (def_symbol_in_progress->bsym);
540	if (name[1] == 'b' && name[2] == 'f')
541	  {
542	    if (! in_function ())
543	      as_warn ("`%s' symbol without preceding function", name);
544/*	    SA_SET_SYM_LNNO (def_symbol_in_progress, 12345);*/
545	    /* Will need relocating */
546	    SF_SET_PROCESS (def_symbol_in_progress);
547	    clear_function ();
548	  }
549      }
550      break;
551
552#ifdef C_AUTOARG
553    case C_AUTOARG:
554#endif /* C_AUTOARG */
555    case C_AUTO:
556    case C_REG:
557    case C_ARG:
558    case C_REGPARM:
559    case C_FIELD:
560      SF_SET_DEBUG (def_symbol_in_progress);
561      S_SET_SEGMENT (def_symbol_in_progress, absolute_section);
562      break;
563
564    case C_MOS:
565    case C_MOE:
566    case C_MOU:
567    case C_EOS:
568      S_SET_SEGMENT (def_symbol_in_progress, absolute_section);
569      break;
570
571    case C_EXT:
572    case C_STAT:
573    case C_LABEL:
574      /* Valid but set somewhere else (s_comm, s_lcomm, colon) */
575      break;
576
577    case C_USTATIC:
578    case C_EXTDEF:
579    case C_ULABEL:
580      as_warn ("unexpected storage class %d",
581	       S_GET_STORAGE_CLASS (def_symbol_in_progress));
582      break;
583    }				/* switch on storage class */
584
585  /* Now that we have built a debug symbol, try to find if we should
586     merge with an existing symbol or not.  If a symbol is C_EFCN or
587     SEG_ABSOLUTE or untagged SEG_DEBUG it never merges. */
588
589  /* Two cases for functions.  Either debug followed by definition or
590     definition followed by debug.  For definition first, we will
591     merge the debug symbol into the definition.  For debug first, the
592     lineno entry MUST point to the definition function or else it
593     will point off into space when obj_crawl_symbol_chain() merges
594     the debug symbol into the real symbol.  Therefor, let's presume
595     the debug symbol is a real function reference. */
596
597  /* FIXME-SOON If for some reason the definition label/symbol is
598     never seen, this will probably leave an undefined symbol at link
599     time. */
600
601  if (S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_EFCN
602      || (!strcmp (bfd_get_section_name (stdoutput,
603					 S_GET_SEGMENT (def_symbol_in_progress)),
604		   "*DEBUG*")
605	  && !SF_GET_TAG (def_symbol_in_progress))
606      || S_GET_SEGMENT (def_symbol_in_progress) == absolute_section
607      || (symbolP = symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP)) == NULL)
608    {
609      if (def_symbol_in_progress != symbol_lastP)
610	symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP,
611		       &symbol_lastP);
612    }
613  else
614    {
615      /* This symbol already exists, merge the newly created symbol
616	 into the old one.  This is not mandatory. The linker can
617	 handle duplicate symbols correctly. But I guess that it save
618	 a *lot* of space if the assembly file defines a lot of
619	 symbols. [loic] */
620
621      /* The debug entry (def_symbol_in_progress) is merged into the
622	 previous definition. */
623
624      c_symbol_merge (def_symbol_in_progress, symbolP);
625      symbol_remove (def_symbol_in_progress, &symbol_rootP, &symbol_lastP);
626
627      def_symbol_in_progress = symbolP;
628
629      if (SF_GET_FUNCTION (def_symbol_in_progress)
630	  || SF_GET_TAG (def_symbol_in_progress)
631	  || S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_STAT)
632	{
633	  /* For functions, and tags, and static symbols, the symbol
634	     *must* be where the debug symbol appears.  Move the
635	     existing symbol to the current place. */
636	  /* If it already is at the end of the symbol list, do nothing */
637	  if (def_symbol_in_progress != symbol_lastP)
638	    {
639	      symbol_remove (def_symbol_in_progress, &symbol_rootP, &symbol_lastP);
640	      symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP, &symbol_lastP);
641	    }
642	}
643    }
644
645  if (SF_GET_TAG (def_symbol_in_progress))
646    {
647      symbolS *oldtag;
648
649      oldtag = symbol_find_base (S_GET_NAME (def_symbol_in_progress),
650				 DO_NOT_STRIP);
651      if (oldtag == NULL || ! SF_GET_TAG (oldtag))
652	tag_insert (S_GET_NAME (def_symbol_in_progress),
653		    def_symbol_in_progress);
654    }
655
656  if (SF_GET_FUNCTION (def_symbol_in_progress))
657    {
658      know (sizeof (def_symbol_in_progress) <= sizeof (long));
659      set_function (def_symbol_in_progress);
660      SF_SET_PROCESS (def_symbol_in_progress);
661
662      if (symbolP == NULL)
663	{
664	  /* That is, if this is the first time we've seen the
665	     function... */
666	  symbol_table_insert (def_symbol_in_progress);
667	} /* definition follows debug */
668    } /* Create the line number entry pointing to the function being defined */
669
670  def_symbol_in_progress = NULL;
671  demand_empty_rest_of_line ();
672}
673
674static void
675obj_coff_dim (ignore)
676     int ignore;
677{
678  int dim_index;
679
680  if (def_symbol_in_progress == NULL)
681    {
682      as_warn (".dim pseudo-op used outside of .def/.endef: ignored.");
683      demand_empty_rest_of_line ();
684      return;
685    }				/* if not inside .def/.endef */
686
687  S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
688
689  for (dim_index = 0; dim_index < DIMNUM; dim_index++)
690    {
691      SKIP_WHITESPACES ();
692      SA_SET_SYM_DIMEN (def_symbol_in_progress, dim_index,
693			get_absolute_expression ());
694
695      switch (*input_line_pointer)
696	{
697	case ',':
698	  input_line_pointer++;
699	  break;
700
701	default:
702	  as_warn ("badly formed .dim directive ignored");
703	  /* intentional fallthrough */
704	case '\n':
705	case ';':
706	  dim_index = DIMNUM;
707	  break;
708	}
709    }
710
711  demand_empty_rest_of_line ();
712}
713
714static void
715obj_coff_line (ignore)
716     int ignore;
717{
718  int this_base;
719
720  if (def_symbol_in_progress == NULL)
721    {
722      /* Probably stabs-style line?  */
723      obj_coff_ln (0);
724      return;
725    }
726
727  this_base = get_absolute_expression ();
728  if (!strcmp (".bf", S_GET_NAME (def_symbol_in_progress)))
729    coff_line_base = this_base;
730
731  S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
732  SA_SET_SYM_LNNO (def_symbol_in_progress, coff_line_base);
733
734  demand_empty_rest_of_line ();
735
736#ifndef NO_LISTING
737  if (strcmp (".bf", S_GET_NAME (def_symbol_in_progress)) == 0)
738    {
739      extern int listing;
740
741      if (listing)
742	listing_source_line ((unsigned int) coff_line_base);
743    }
744#endif
745}
746
747static void
748obj_coff_size (ignore)
749     int ignore;
750{
751  if (def_symbol_in_progress == NULL)
752    {
753      as_warn (".size pseudo-op used outside of .def/.endef ignored.");
754      demand_empty_rest_of_line ();
755      return;
756    }				/* if not inside .def/.endef */
757
758  S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
759  SA_SET_SYM_SIZE (def_symbol_in_progress, get_absolute_expression ());
760  demand_empty_rest_of_line ();
761}
762
763static void
764obj_coff_scl (ignore)
765     int ignore;
766{
767  if (def_symbol_in_progress == NULL)
768    {
769      as_warn (".scl pseudo-op used outside of .def/.endef ignored.");
770      demand_empty_rest_of_line ();
771      return;
772    }				/* if not inside .def/.endef */
773
774  S_SET_STORAGE_CLASS (def_symbol_in_progress, get_absolute_expression ());
775  demand_empty_rest_of_line ();
776}
777
778static void
779obj_coff_tag (ignore)
780     int ignore;
781{
782  char *symbol_name;
783  char name_end;
784
785  if (def_symbol_in_progress == NULL)
786    {
787      as_warn (".tag pseudo-op used outside of .def/.endef ignored.");
788      demand_empty_rest_of_line ();
789      return;
790    }
791
792  S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
793  symbol_name = input_line_pointer;
794  name_end = get_symbol_end ();
795
796#ifdef tc_canonicalize_symbol_name
797  symbol_name = tc_canonicalize_symbol_name (symbol_name);
798#endif
799
800  /* Assume that the symbol referred to by .tag is always defined.
801     This was a bad assumption.  I've added find_or_make. xoxorich. */
802  SA_SET_SYM_TAGNDX (def_symbol_in_progress,
803		     tag_find_or_make (symbol_name));
804  if (SA_GET_SYM_TAGNDX (def_symbol_in_progress) == 0L)
805    {
806      as_warn ("tag not found for .tag %s", symbol_name);
807    }				/* not defined */
808
809  SF_SET_TAGGED (def_symbol_in_progress);
810  *input_line_pointer = name_end;
811
812  demand_empty_rest_of_line ();
813}
814
815static void
816obj_coff_type (ignore)
817     int ignore;
818{
819  if (def_symbol_in_progress == NULL)
820    {
821      as_warn (".type pseudo-op used outside of .def/.endef ignored.");
822      demand_empty_rest_of_line ();
823      return;
824    }				/* if not inside .def/.endef */
825
826  S_SET_DATA_TYPE (def_symbol_in_progress, get_absolute_expression ());
827
828  if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress)) &&
829      S_GET_STORAGE_CLASS (def_symbol_in_progress) != C_TPDEF)
830    {
831      SF_SET_FUNCTION (def_symbol_in_progress);
832    }				/* is a function */
833
834  demand_empty_rest_of_line ();
835}
836
837static void
838obj_coff_val (ignore)
839     int ignore;
840{
841  if (def_symbol_in_progress == NULL)
842    {
843      as_warn (".val pseudo-op used outside of .def/.endef ignored.");
844      demand_empty_rest_of_line ();
845      return;
846    }				/* if not inside .def/.endef */
847
848  if (is_name_beginner (*input_line_pointer))
849    {
850      char *symbol_name = input_line_pointer;
851      char name_end = get_symbol_end ();
852
853#ifdef tc_canonicalize_symbol_name
854  symbol_name = tc_canonicalize_symbol_name (symbol_name);
855#endif
856      if (!strcmp (symbol_name, "."))
857	{
858	  def_symbol_in_progress->sy_frag = frag_now;
859	  S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ());
860	  /* If the .val is != from the .def (e.g. statics) */
861	}
862      else if (strcmp (S_GET_NAME (def_symbol_in_progress), symbol_name))
863	{
864	  def_symbol_in_progress->sy_value.X_op = O_symbol;
865	  def_symbol_in_progress->sy_value.X_add_symbol =
866	    symbol_find_or_make (symbol_name);
867	  def_symbol_in_progress->sy_value.X_op_symbol = NULL;
868	  def_symbol_in_progress->sy_value.X_add_number = 0;
869
870	  /* If the segment is undefined when the forward reference is
871	     resolved, then copy the segment id from the forward
872	     symbol.  */
873	  SF_SET_GET_SEGMENT (def_symbol_in_progress);
874	}
875      /* Otherwise, it is the name of a non debug symbol and its value will be calculated later. */
876      *input_line_pointer = name_end;
877    }
878  else
879    {
880      S_SET_VALUE (def_symbol_in_progress, get_absolute_expression ());
881    }				/* if symbol based */
882
883  demand_empty_rest_of_line ();
884}
885
886void
887coff_obj_read_begin_hook ()
888{
889  /* These had better be the same.  Usually 18 bytes. */
890#ifndef BFD_HEADERS
891  know (sizeof (SYMENT) == sizeof (AUXENT));
892  know (SYMESZ == AUXESZ);
893#endif
894  tag_init ();
895}
896
897
898symbolS *coff_last_function;
899static symbolS *coff_last_bf;
900
901void
902coff_frob_symbol (symp, punt)
903     symbolS *symp;
904     int *punt;
905{
906  static symbolS *last_tagP;
907  static stack *block_stack;
908  static symbolS *set_end;
909  symbolS *next_set_end = NULL;
910
911  if (symp == &abs_symbol)
912    {
913      *punt = 1;
914      return;
915    }
916
917  if (current_lineno_sym)
918    coff_add_linesym ((symbolS *) 0);
919
920  if (!block_stack)
921    block_stack = stack_init (512, sizeof (symbolS*));
922
923  if (!S_IS_DEFINED (symp) && S_GET_STORAGE_CLASS (symp) != C_STAT)
924    S_SET_STORAGE_CLASS (symp, C_EXT);
925
926  if (!SF_GET_DEBUG (symp))
927    {
928      symbolS *real;
929      if (!SF_GET_LOCAL (symp)
930	  && !SF_GET_STATICS (symp)
931	  && (real = symbol_find_base (S_GET_NAME (symp), DO_NOT_STRIP))
932	  && real != symp)
933	{
934	  c_symbol_merge (symp, real);
935	  *punt = 1;
936	}
937      if (!S_IS_DEFINED (symp) && !SF_GET_LOCAL (symp))
938	{
939	  assert (S_GET_VALUE (symp) == 0);
940	  S_SET_EXTERNAL (symp);
941	}
942      else if (S_GET_STORAGE_CLASS (symp) == C_NULL)
943	{
944	  if (S_GET_SEGMENT (symp) == text_section
945	      && symp != seg_info (text_section)->sym)
946	    S_SET_STORAGE_CLASS (symp, C_LABEL);
947	  else
948	    S_SET_STORAGE_CLASS (symp, C_STAT);
949	}
950      if (SF_GET_PROCESS (symp))
951	{
952	  if (S_GET_STORAGE_CLASS (symp) == C_BLOCK)
953	    {
954	      if (!strcmp (S_GET_NAME (symp), ".bb"))
955		stack_push (block_stack, (char *) &symp);
956	      else
957		{
958		  symbolS *begin;
959		  begin = *(symbolS **) stack_pop (block_stack);
960		  if (begin == 0)
961		    as_warn ("mismatched .eb");
962		  else
963		    next_set_end = begin;
964		}
965	    }
966	  if (coff_last_function == 0 && SF_GET_FUNCTION (symp))
967	    {
968	      union internal_auxent *auxp;
969	      coff_last_function = symp;
970	      if (S_GET_NUMBER_AUXILIARY (symp) < 1)
971		S_SET_NUMBER_AUXILIARY (symp, 1);
972	      auxp = &coffsymbol (symp->bsym)->native[1].u.auxent;
973	      memset (auxp->x_sym.x_fcnary.x_ary.x_dimen, 0,
974		      sizeof (auxp->x_sym.x_fcnary.x_ary.x_dimen));
975	    }
976	  if (S_GET_STORAGE_CLASS (symp) == C_EFCN)
977	    {
978	      if (coff_last_function == 0)
979		as_fatal ("C_EFCN symbol out of scope");
980	      SA_SET_SYM_FSIZE (coff_last_function,
981				(long) (S_GET_VALUE (symp)
982					- S_GET_VALUE (coff_last_function)));
983	      next_set_end = coff_last_function;
984	      coff_last_function = 0;
985	    }
986	}
987      if (S_IS_EXTERNAL (symp))
988	S_SET_STORAGE_CLASS (symp, C_EXT);
989      else if (SF_GET_LOCAL (symp))
990	*punt = 1;
991
992      if (SF_GET_FUNCTION (symp))
993	symp->bsym->flags |= BSF_FUNCTION;
994
995      /* more ... */
996    }
997
998  if (SF_GET_TAG (symp))
999    last_tagP = symp;
1000  else if (S_GET_STORAGE_CLASS (symp) == C_EOS)
1001    next_set_end = last_tagP;
1002
1003#ifdef OBJ_XCOFF
1004  /* This is pretty horrible, but we have to set *punt correctly in
1005     order to call SA_SET_SYM_ENDNDX correctly.  */
1006  if (! symp->sy_used_in_reloc
1007      && ((symp->bsym->flags & BSF_SECTION_SYM) != 0
1008	  || (! S_IS_EXTERNAL (symp)
1009	      && ! symp->sy_tc.output
1010	      && S_GET_STORAGE_CLASS (symp) != C_FILE)))
1011    *punt = 1;
1012#endif
1013
1014  if (set_end != (symbolS *) NULL
1015      && ! *punt
1016      && ((symp->bsym->flags & BSF_NOT_AT_END) != 0
1017	  || (S_IS_DEFINED (symp)
1018	      && ! S_IS_COMMON (symp)
1019	      && (! S_IS_EXTERNAL (symp) || SF_GET_FUNCTION (symp)))))
1020    {
1021      SA_SET_SYM_ENDNDX (set_end, symp);
1022      set_end = NULL;
1023    }
1024
1025  if (next_set_end != NULL
1026      && ! *punt)
1027    set_end = next_set_end;
1028
1029  if (! *punt
1030      && S_GET_STORAGE_CLASS (symp) == C_FCN
1031      && strcmp (S_GET_NAME (symp), ".bf") == 0)
1032    {
1033      if (coff_last_bf != NULL)
1034	SA_SET_SYM_ENDNDX (coff_last_bf, symp);
1035      coff_last_bf = symp;
1036    }
1037
1038  if (coffsymbol (symp->bsym)->lineno)
1039    {
1040      int i;
1041      struct line_no *lptr;
1042      alent *l;
1043
1044      lptr = (struct line_no *) coffsymbol (symp->bsym)->lineno;
1045      for (i = 0; lptr; lptr = lptr->next)
1046	i++;
1047      lptr = (struct line_no *) coffsymbol (symp->bsym)->lineno;
1048
1049      /* We need i entries for line numbers, plus 1 for the first
1050	 entry which BFD will override, plus 1 for the last zero
1051	 entry (a marker for BFD).  */
1052      l = (alent *) xmalloc ((i + 2) * sizeof (alent));
1053      coffsymbol (symp->bsym)->lineno = l;
1054      l[i + 1].line_number = 0;
1055      l[i + 1].u.sym = NULL;
1056      for (; i > 0; i--)
1057	{
1058	  if (lptr->frag)
1059	    lptr->l.u.offset += lptr->frag->fr_address;
1060	  l[i] = lptr->l;
1061	  lptr = lptr->next;
1062	}
1063    }
1064}
1065
1066void
1067coff_adjust_section_syms (abfd, sec, x)
1068     bfd *abfd;
1069     asection *sec;
1070     PTR x;
1071{
1072  symbolS *secsym;
1073  segment_info_type *seginfo = seg_info (sec);
1074  int nlnno, nrelocs = 0;
1075
1076  /* RS/6000 gas creates a .debug section manually in ppc_frob_file in
1077     tc-ppc.c.  Do not get confused by it.  */
1078  if (seginfo == NULL)
1079    return;
1080
1081  if (!strcmp (sec->name, ".text"))
1082    nlnno = coff_n_line_nos;
1083  else
1084    nlnno = 0;
1085  {
1086    /* @@ Hope that none of the fixups expand to more than one reloc
1087       entry...  */
1088    fixS *fixp = seginfo->fix_root;
1089    while (fixp)
1090      {
1091	fixp = fixp->fx_next;
1092	nrelocs++;
1093      }
1094  }
1095  if (bfd_get_section_size_before_reloc (sec) == 0
1096      && nrelocs == 0
1097      && nlnno == 0
1098      && sec != text_section
1099      && sec != data_section
1100      && sec != bss_section)
1101    return;
1102  secsym = section_symbol (sec);
1103  SA_SET_SCN_NRELOC (secsym, nrelocs);
1104  SA_SET_SCN_NLINNO (secsym, nlnno);
1105}
1106
1107void
1108coff_frob_file ()
1109{
1110  bfd_map_over_sections (stdoutput, coff_adjust_section_syms, (char*) 0);
1111}
1112
1113/*
1114 * implement the .section pseudo op:
1115 *	.section name {, "flags"}
1116 *                ^         ^
1117 *                |         +--- optional flags: 'b' for bss
1118 *                |                              'i' for info
1119 *                +-- section name               'l' for lib
1120 *                                               'n' for noload
1121 *                                               'o' for over
1122 *                                               'w' for data
1123 *						 'd' (apparently m88k for data)
1124 *                                               'x' for text
1125 *						 'r' for read-only data
1126 * But if the argument is not a quoted string, treat it as a
1127 * subsegment number.
1128 */
1129
1130void
1131obj_coff_section (ignore)
1132     int ignore;
1133{
1134  /* Strip out the section name */
1135  char *section_name;
1136  char c;
1137  char *name;
1138  unsigned int exp;
1139  flagword flags;
1140  asection *sec;
1141
1142  if (flag_mri)
1143    {
1144      char type;
1145
1146      s_mri_sect (&type);
1147      return;
1148    }
1149
1150  section_name = input_line_pointer;
1151  c = get_symbol_end ();
1152
1153  name = xmalloc (input_line_pointer - section_name + 1);
1154  strcpy (name, section_name);
1155
1156  *input_line_pointer = c;
1157
1158  SKIP_WHITESPACE ();
1159
1160  exp = 0;
1161  flags = SEC_NO_FLAGS;
1162
1163  if (*input_line_pointer == ',')
1164    {
1165      ++input_line_pointer;
1166      SKIP_WHITESPACE ();
1167      if (*input_line_pointer != '"')
1168	exp = get_absolute_expression ();
1169      else
1170	{
1171	  ++input_line_pointer;
1172	  while (*input_line_pointer != '"'
1173		 && ! is_end_of_line[(unsigned char) *input_line_pointer])
1174	    {
1175	      switch (*input_line_pointer)
1176		{
1177		case 'b': flags |= SEC_ALLOC; flags &=~ SEC_LOAD; break;
1178		case 'n': flags &=~ SEC_LOAD; break;
1179		case 'd':
1180		case 'w': flags &=~ SEC_READONLY; break;
1181		case 'x': flags |= SEC_CODE; break;
1182		case 'r': flags |= SEC_READONLY; break;
1183
1184		case 'i': /* STYP_INFO */
1185		case 'l': /* STYP_LIB */
1186		case 'o': /* STYP_OVER */
1187		  as_warn ("unsupported section attribute '%c'",
1188			   *input_line_pointer);
1189		  break;
1190
1191		default:
1192		  as_warn("unknown section attribute '%c'",
1193			  *input_line_pointer);
1194		  break;
1195		}
1196	      ++input_line_pointer;
1197	    }
1198	  if (*input_line_pointer == '"')
1199	    ++input_line_pointer;
1200	}
1201    }
1202
1203  sec = subseg_new (name, (subsegT) exp);
1204
1205  if (flags != SEC_NO_FLAGS)
1206    {
1207      if (! bfd_set_section_flags (stdoutput, sec, flags))
1208	as_warn ("error setting flags for \"%s\": %s",
1209		 bfd_section_name (stdoutput, sec),
1210		 bfd_errmsg (bfd_get_error ()));
1211    }
1212
1213  demand_empty_rest_of_line ();
1214}
1215
1216void
1217coff_adjust_symtab ()
1218{
1219  if (symbol_rootP == NULL
1220      || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE)
1221    c_dot_file_symbol ("fake");
1222}
1223
1224void
1225coff_frob_section (sec)
1226     segT sec;
1227{
1228  segT strsec;
1229  char *p;
1230  fragS *fragp;
1231  bfd_vma size, n_entries, mask;
1232
1233  /* The COFF back end in BFD requires that all section sizes be
1234     rounded up to multiples of the corresponding section alignments.
1235     Seems kinda silly to me, but that's the way it is.  */
1236  size = bfd_get_section_size_before_reloc (sec);
1237  mask = ((bfd_vma) 1 << (bfd_vma) sec->alignment_power) - 1;
1238  if (size & mask)
1239    {
1240      size = (size + mask) & ~mask;
1241      bfd_set_section_size (stdoutput, sec, size);
1242    }
1243
1244  /* If the section size is non-zero, the section symbol needs an aux
1245     entry associated with it, indicating the size.  We don't know
1246     all the values yet; coff_frob_symbol will fill them in later.  */
1247  if (size != 0
1248      || sec == text_section
1249      || sec == data_section
1250      || sec == bss_section)
1251    {
1252      symbolS *secsym = section_symbol (sec);
1253
1254      S_SET_STORAGE_CLASS (secsym, C_STAT);
1255      S_SET_NUMBER_AUXILIARY (secsym, 1);
1256      SF_SET_STATICS (secsym);
1257      SA_SET_SCN_SCNLEN (secsym, size);
1258    }
1259
1260  /* @@ these should be in a "stabs.h" file, or maybe as.h */
1261#ifndef STAB_SECTION_NAME
1262#define STAB_SECTION_NAME ".stab"
1263#endif
1264#ifndef STAB_STRING_SECTION_NAME
1265#define STAB_STRING_SECTION_NAME ".stabstr"
1266#endif
1267  if (strcmp (STAB_STRING_SECTION_NAME, sec->name))
1268    return;
1269
1270  strsec = sec;
1271  sec = subseg_get (STAB_SECTION_NAME, 0);
1272  /* size is already rounded up, since other section will be listed first */
1273  size = bfd_get_section_size_before_reloc (strsec);
1274
1275  n_entries = bfd_get_section_size_before_reloc (sec) / 12 - 1;
1276
1277  /* Find first non-empty frag.  It should be large enough.  */
1278  fragp = seg_info (sec)->frchainP->frch_root;
1279  while (fragp && fragp->fr_fix == 0)
1280    fragp = fragp->fr_next;
1281  assert (fragp != 0 && fragp->fr_fix >= 12);
1282
1283  /* Store the values.  */
1284  p = fragp->fr_literal;
1285  bfd_h_put_16 (stdoutput, n_entries, (bfd_byte *) p + 6);
1286  bfd_h_put_32 (stdoutput, size, (bfd_byte *) p + 8);
1287}
1288
1289void
1290obj_coff_init_stab_section (seg)
1291     segT seg;
1292{
1293  char *file;
1294  char *p;
1295  char *stabstr_name;
1296  unsigned int stroff;
1297
1298  /* Make space for this first symbol. */
1299  p = frag_more (12);
1300  /* Zero it out. */
1301  memset (p, 0, 12);
1302  as_where (&file, (unsigned int *) NULL);
1303  stabstr_name = (char *) alloca (strlen (seg->name) + 4);
1304  strcpy (stabstr_name, seg->name);
1305  strcat (stabstr_name, "str");
1306  stroff = get_stab_string_offset (file, stabstr_name);
1307  know (stroff == 1);
1308  md_number_to_chars (p, stroff, 4);
1309}
1310
1311#ifdef DEBUG
1312/* for debugging */
1313const char *
1314s_get_name (s)
1315     symbolS *s;
1316{
1317  return ((s == NULL) ? "(NULL)" : S_GET_NAME (s));
1318}
1319
1320void
1321symbol_dump ()
1322{
1323  symbolS *symbolP;
1324
1325  for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
1326    {
1327      printf("0x%lx: \"%s\" type = %ld, class = %d, segment = %d\n",
1328	     (unsigned long) symbolP,
1329	     S_GET_NAME(symbolP),
1330	     (long) S_GET_DATA_TYPE(symbolP),
1331	     S_GET_STORAGE_CLASS(symbolP),
1332	     (int) S_GET_SEGMENT(symbolP));
1333    }
1334}
1335
1336#endif /* DEBUG */
1337
1338#else /* not BFD_ASSEMBLER */
1339
1340#include "frags.h"
1341/* This is needed because we include internal bfd things. */
1342#include <time.h>
1343
1344#include "libbfd.h"
1345#include "libcoff.h"
1346
1347#ifdef TE_PE
1348#include "coff/pe.h"
1349#endif
1350
1351/* The NOP_OPCODE is for the alignment fill value.  Fill with nop so
1352   that we can stick sections together without causing trouble.  */
1353#ifndef NOP_OPCODE
1354#define NOP_OPCODE 0x00
1355#endif
1356
1357/* The zeroes if symbol name is longer than 8 chars */
1358#define S_SET_ZEROES(s,v)		((s)->sy_symbol.ost_entry.n_zeroes = (v))
1359
1360#define MIN(a,b) ((a) < (b)? (a) : (b))
1361/* This vector is used to turn an internal segment into a section #
1362   suitable for insertion into a coff symbol table
1363 */
1364
1365const short seg_N_TYPE[] =
1366{				/* in: segT   out: N_TYPE bits */
1367  C_ABS_SECTION,
1368  1,    2,  3,   4,    5,   6,   7,   8,   9,  10,
1369  11,  12,  13,  14,  15,  16,  17,  18,  19,  20,
1370  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,
1371  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,
1372  C_UNDEF_SECTION,		/* SEG_UNKNOWN */
1373  C_UNDEF_SECTION,		/* SEG_GOOF */
1374  C_UNDEF_SECTION,		/* SEG_EXPR */
1375  C_DEBUG_SECTION,		/* SEG_DEBUG */
1376  C_NTV_SECTION,		/* SEG_NTV */
1377  C_PTV_SECTION,		/* SEG_PTV */
1378  C_REGISTER_SECTION,		/* SEG_REGISTER */
1379};
1380
1381int function_lineoff = -1;	/* Offset in line#s where the last function
1382				   started (the odd entry for line #0) */
1383
1384/* structure used to keep the filenames which
1385   are too long around so that we can stick them
1386   into the string table */
1387struct filename_list
1388{
1389  char *filename;
1390  struct filename_list *next;
1391};
1392
1393static struct filename_list *filename_list_head;
1394static struct filename_list *filename_list_tail;
1395
1396static symbolS *last_line_symbol;
1397
1398/* Add 4 to the real value to get the index and compensate the
1399   negatives. This vector is used by S_GET_SEGMENT to turn a coff
1400   section number into a segment number
1401*/
1402static symbolS *previous_file_symbol;
1403void c_symbol_merge ();
1404static int line_base;
1405
1406symbolS *c_section_symbol ();
1407bfd *abfd;
1408
1409static void fixup_segment PARAMS ((segment_info_type *segP,
1410				   segT this_segment_type));
1411
1412
1413static void fixup_mdeps PARAMS ((fragS *,
1414				 object_headers *,
1415				 segT));
1416
1417
1418static void fill_section PARAMS ((bfd * abfd,
1419				  object_headers *,
1420				  unsigned long *));
1421
1422
1423static int c_line_new PARAMS ((symbolS * symbol, long paddr,
1424			       int line_number,
1425			       fragS * frag));
1426
1427
1428static void w_symbols PARAMS ((bfd * abfd, char *where,
1429			       symbolS * symbol_rootP));
1430
1431static void adjust_stab_section PARAMS ((bfd *abfd, segT seg));
1432
1433static void obj_coff_lcomm PARAMS ((int));
1434static void obj_coff_text PARAMS ((int));
1435static void obj_coff_data PARAMS ((int));
1436static void obj_coff_bss PARAMS ((int));
1437static void obj_coff_ident PARAMS ((int));
1438void obj_coff_section PARAMS ((int));
1439
1440/* Section stuff
1441
1442   We allow more than just the standard 3 sections, infact, we allow
1443   40 sections, (though the usual three have to be there).
1444
1445   This structure performs the mappings for us:
1446*/
1447
1448
1449typedef struct
1450{
1451  segT seg_t;
1452  int i;
1453} seg_info_type;
1454
1455static const seg_info_type seg_info_off_by_4[] =
1456{
1457 {SEG_PTV,  },
1458 {SEG_NTV,  },
1459 {SEG_DEBUG, },
1460 {SEG_ABSOLUTE,  },
1461 {SEG_UNKNOWN,	 },
1462 {SEG_E0}, {SEG_E1}, {SEG_E2}, {SEG_E3}, {SEG_E4},
1463 {SEG_E5}, {SEG_E6}, {SEG_E7}, {SEG_E8}, {SEG_E9},
1464 {SEG_E10},{SEG_E11},{SEG_E12},{SEG_E13},{SEG_E14},
1465 {SEG_E15},{SEG_E16},{SEG_E17},{SEG_E18},{SEG_E19},
1466 {SEG_E20},{SEG_E21},{SEG_E22},{SEG_E23},{SEG_E24},
1467 {SEG_E25},{SEG_E26},{SEG_E27},{SEG_E28},{SEG_E29},
1468 {SEG_E30},{SEG_E31},{SEG_E32},{SEG_E33},{SEG_E34},
1469 {SEG_E35},{SEG_E36},{SEG_E37},{SEG_E38},{SEG_E39},
1470 {(segT)40},
1471 {(segT)41},
1472 {(segT)42},
1473 {(segT)43},
1474 {(segT)44},
1475 {(segT)45},
1476 {(segT)0},
1477 {(segT)0},
1478 {(segT)0},
1479 {SEG_REGISTER}
1480};
1481
1482
1483
1484#define SEG_INFO_FROM_SECTION_NUMBER(x) (seg_info_off_by_4[(x)+4])
1485
1486static relax_addressT
1487relax_align (address, alignment)
1488     relax_addressT address;
1489     long alignment;
1490{
1491  relax_addressT mask;
1492  relax_addressT new_address;
1493
1494  mask = ~((~0) << alignment);
1495  new_address = (address + mask) & (~mask);
1496  return (new_address - address);
1497}
1498
1499
1500segT
1501s_get_segment (x)
1502     symbolS * x;
1503{
1504  return SEG_INFO_FROM_SECTION_NUMBER (x->sy_symbol.ost_entry.n_scnum).seg_t;
1505}
1506
1507/* calculate the size of the frag chain and fill in the section header
1508   to contain all of it, also fill in the addr of the sections */
1509static unsigned int
1510size_section (abfd, idx)
1511     bfd * abfd;
1512     unsigned int idx;
1513{
1514
1515  unsigned int size = 0;
1516  fragS *frag = segment_info[idx].frchainP->frch_root;
1517  while (frag)
1518    {
1519      size = frag->fr_address;
1520      if (frag->fr_address != size)
1521	{
1522	  fprintf (stderr, "Out of step\n");
1523	  size = frag->fr_address;
1524	}
1525
1526      switch (frag->fr_type)
1527	{
1528#ifdef TC_COFF_SIZEMACHDEP
1529	case rs_machine_dependent:
1530	  size += TC_COFF_SIZEMACHDEP (frag);
1531	  break;
1532#endif
1533	case rs_space:
1534	  assert (frag->fr_symbol == 0);
1535	case rs_fill:
1536	case rs_org:
1537	  size += frag->fr_fix;
1538	  size += frag->fr_offset * frag->fr_var;
1539	  break;
1540	case rs_align:
1541	case rs_align_code:
1542	  {
1543	    addressT off;
1544
1545	    size += frag->fr_fix;
1546	    off = relax_align (size, frag->fr_offset);
1547	    if (frag->fr_subtype != 0 && off > frag->fr_subtype)
1548	      off = 0;
1549	    size += off;
1550	  }
1551	  break;
1552	default:
1553	  BAD_CASE (frag->fr_type);
1554	  break;
1555	}
1556      frag = frag->fr_next;
1557    }
1558  segment_info[idx].scnhdr.s_size = size;
1559  return size;
1560}
1561
1562
1563static unsigned int
1564count_entries_in_chain (idx)
1565     unsigned int idx;
1566{
1567  unsigned int nrelocs;
1568  fixS *fixup_ptr;
1569
1570  /* Count the relocations */
1571  fixup_ptr = segment_info[idx].fix_root;
1572  nrelocs = 0;
1573  while (fixup_ptr != (fixS *) NULL)
1574    {
1575      if (fixup_ptr->fx_done == 0 && TC_COUNT_RELOC (fixup_ptr))
1576	{
1577#ifdef TC_A29K
1578	  if (fixup_ptr->fx_r_type == RELOC_CONSTH)
1579	    nrelocs += 2;
1580	  else
1581	    nrelocs++;
1582#else
1583	  nrelocs++;
1584#endif
1585	}
1586
1587      fixup_ptr = fixup_ptr->fx_next;
1588    }
1589  return nrelocs;
1590}
1591
1592#ifdef TE_AUX
1593
1594static int compare_external_relocs PARAMS ((const PTR, const PTR));
1595
1596/* AUX's ld expects relocations to be sorted */
1597static int
1598compare_external_relocs (x, y)
1599     const PTR x;
1600     const PTR y;
1601{
1602  struct external_reloc *a = (struct external_reloc *) x;
1603  struct external_reloc *b = (struct external_reloc *) y;
1604  bfd_vma aadr = bfd_getb32 (a->r_vaddr);
1605  bfd_vma badr = bfd_getb32 (b->r_vaddr);
1606  return (aadr < badr ? -1 : badr < aadr ? 1 : 0);
1607}
1608
1609#endif
1610
1611/* output all the relocations for a section */
1612void
1613do_relocs_for (abfd, h, file_cursor)
1614     bfd * abfd;
1615     object_headers * h;
1616     unsigned long *file_cursor;
1617{
1618  unsigned int nrelocs;
1619  unsigned int idx;
1620  unsigned long reloc_start = *file_cursor;
1621
1622  for (idx = SEG_E0; idx < SEG_LAST; idx++)
1623    {
1624      if (segment_info[idx].scnhdr.s_name[0])
1625	{
1626	  struct external_reloc *ext_ptr;
1627	  struct external_reloc *external_reloc_vec;
1628	  unsigned int external_reloc_size;
1629	  unsigned int base = segment_info[idx].scnhdr.s_paddr;
1630	  fixS *fix_ptr = segment_info[idx].fix_root;
1631	  nrelocs = count_entries_in_chain (idx);
1632
1633	  if (nrelocs)
1634	    /* Bypass this stuff if no relocs.  This also incidentally
1635	       avoids a SCO bug, where free(malloc(0)) tends to crash.  */
1636	    {
1637	      external_reloc_size = nrelocs * RELSZ;
1638	      external_reloc_vec =
1639		(struct external_reloc *) malloc (external_reloc_size);
1640
1641	      ext_ptr = external_reloc_vec;
1642
1643	      /* Fill in the internal coff style reloc struct from the
1644		 internal fix list.  */
1645	      while (fix_ptr)
1646		{
1647		  struct internal_reloc intr;
1648
1649		  /* Only output some of the relocations */
1650		  if (fix_ptr->fx_done == 0 && TC_COUNT_RELOC (fix_ptr))
1651		    {
1652#ifdef TC_RELOC_MANGLE
1653		      TC_RELOC_MANGLE (&segment_info[idx], fix_ptr, &intr,
1654				       base);
1655
1656#else
1657		      symbolS *dot;
1658		      symbolS *symbol_ptr = fix_ptr->fx_addsy;
1659
1660		      intr.r_type = TC_COFF_FIX2RTYPE (fix_ptr);
1661		      intr.r_vaddr =
1662			base + fix_ptr->fx_frag->fr_address + fix_ptr->fx_where;
1663
1664#ifdef TC_KEEP_FX_OFFSET
1665		      intr.r_offset = fix_ptr->fx_offset;
1666#else
1667		      intr.r_offset = 0;
1668#endif
1669
1670		      while (symbol_ptr->sy_value.X_op == O_symbol
1671			     && (! S_IS_DEFINED (symbol_ptr)
1672				 || S_IS_COMMON (symbol_ptr)))
1673			{
1674			  symbolS *n;
1675
1676			  /* We must avoid looping, as that can occur
1677                             with a badly written program.  */
1678			  n = symbol_ptr->sy_value.X_add_symbol;
1679			  if (n == symbol_ptr)
1680			    break;
1681			  symbol_ptr = n;
1682			}
1683
1684		      /* Turn the segment of the symbol into an offset.  */
1685		      if (symbol_ptr)
1686			{
1687			  resolve_symbol_value (symbol_ptr);
1688			  if (! symbol_ptr->sy_resolved)
1689			    {
1690			      char *file;
1691			      unsigned int line;
1692
1693			      if (expr_symbol_where (symbol_ptr, &file, &line))
1694				as_bad_where (file, line,
1695					      "unresolved relocation");
1696			      else
1697				as_bad ("bad relocation: symbol `%s' not in symbol table",
1698					S_GET_NAME (symbol_ptr));
1699			    }
1700			  dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1701			  if (dot)
1702			    {
1703			      intr.r_symndx = dot->sy_number;
1704			    }
1705			  else
1706			    {
1707			      intr.r_symndx = symbol_ptr->sy_number;
1708			    }
1709
1710			}
1711		      else
1712			{
1713			  intr.r_symndx = -1;
1714			}
1715#endif
1716
1717		      (void) bfd_coff_swap_reloc_out (abfd, &intr, ext_ptr);
1718		      ext_ptr++;
1719
1720#if defined(TC_A29K)
1721
1722		      /* The 29k has a special kludge for the high 16 bit
1723			 reloc.  Two relocations are emited, R_IHIHALF,
1724			 and R_IHCONST. The second one doesn't contain a
1725			 symbol, but uses the value for offset.  */
1726
1727		      if (intr.r_type == R_IHIHALF)
1728			{
1729			  /* now emit the second bit */
1730			  intr.r_type = R_IHCONST;
1731			  intr.r_symndx = fix_ptr->fx_addnumber;
1732			  (void) bfd_coff_swap_reloc_out (abfd, &intr, ext_ptr);
1733			  ext_ptr++;
1734			}
1735#endif
1736		    }
1737
1738		  fix_ptr = fix_ptr->fx_next;
1739		}
1740
1741#ifdef TE_AUX
1742	      /* Sort the reloc table */
1743	      qsort ((PTR) external_reloc_vec, nrelocs,
1744		     sizeof (struct external_reloc), compare_external_relocs);
1745#endif
1746
1747	      /* Write out the reloc table */
1748	      bfd_write ((PTR) external_reloc_vec, 1, external_reloc_size,
1749			 abfd);
1750	      free (external_reloc_vec);
1751
1752	      /* Fill in section header info.  */
1753	      segment_info[idx].scnhdr.s_relptr = *file_cursor;
1754	      *file_cursor += external_reloc_size;
1755	      segment_info[idx].scnhdr.s_nreloc = nrelocs;
1756	    }
1757	  else
1758	    {
1759	      /* No relocs */
1760	      segment_info[idx].scnhdr.s_relptr = 0;
1761	    }
1762	}
1763    }
1764  /* Set relocation_size field in file headers */
1765  H_SET_RELOCATION_SIZE (h, *file_cursor - reloc_start, 0);
1766}
1767
1768
1769/* run through a frag chain and write out the data to go with it, fill
1770   in the scnhdrs with the info on the file postions
1771*/
1772static void
1773fill_section (abfd, h, file_cursor)
1774     bfd * abfd;
1775     object_headers *h;
1776     unsigned long *file_cursor;
1777{
1778
1779  unsigned int i;
1780  unsigned int paddr = 0;
1781
1782  for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1783    {
1784      unsigned int offset = 0;
1785      struct internal_scnhdr *s = &(segment_info[i].scnhdr);
1786
1787      PROGRESS (1);
1788
1789      if (s->s_name[0])
1790	{
1791	  fragS *frag = segment_info[i].frchainP->frch_root;
1792	  char *buffer;
1793
1794	  if (s->s_size == 0)
1795	    s->s_scnptr = 0;
1796	  else
1797	    {
1798	      buffer = xmalloc (s->s_size);
1799	      s->s_scnptr = *file_cursor;
1800	    }
1801	  know (s->s_paddr == paddr);
1802
1803	  if (strcmp (s->s_name, ".text") == 0)
1804	    s->s_flags |= STYP_TEXT;
1805	  else if (strcmp (s->s_name, ".data") == 0)
1806	    s->s_flags |= STYP_DATA;
1807	  else if (strcmp (s->s_name, ".bss") == 0)
1808	    {
1809	      s->s_scnptr = 0;
1810	      s->s_flags |= STYP_BSS;
1811
1812	      /* @@ Should make the i386 and a29k coff targets define
1813		 COFF_NOLOAD_PROBLEM, and have only one test here.  */
1814#ifndef TC_I386
1815#ifndef TC_A29K
1816#ifndef COFF_NOLOAD_PROBLEM
1817	      /* Apparently the SVR3 linker (and exec syscall) and UDI
1818		 mondfe progrem are confused by noload sections.  */
1819	      s->s_flags |= STYP_NOLOAD;
1820#endif
1821#endif
1822#endif
1823	    }
1824	  else if (strcmp (s->s_name, ".lit") == 0)
1825	    s->s_flags = STYP_LIT | STYP_TEXT;
1826	  else if (strcmp (s->s_name, ".init") == 0)
1827	    s->s_flags |= STYP_TEXT;
1828	  else if (strcmp (s->s_name, ".fini") == 0)
1829	    s->s_flags |= STYP_TEXT;
1830	  else if (strncmp (s->s_name, ".comment", 8) == 0)
1831	    s->s_flags |= STYP_INFO;
1832
1833	  while (frag)
1834	    {
1835	      unsigned int fill_size;
1836	      switch (frag->fr_type)
1837		{
1838		case rs_machine_dependent:
1839		  if (frag->fr_fix)
1840		    {
1841		      memcpy (buffer + frag->fr_address,
1842			      frag->fr_literal,
1843			      (unsigned int) frag->fr_fix);
1844		      offset += frag->fr_fix;
1845		    }
1846
1847		  break;
1848		case rs_space:
1849		  assert (frag->fr_symbol == 0);
1850		case rs_fill:
1851		case rs_align:
1852		case rs_align_code:
1853		case rs_org:
1854		  if (frag->fr_fix)
1855		    {
1856		      memcpy (buffer + frag->fr_address,
1857			      frag->fr_literal,
1858			      (unsigned int) frag->fr_fix);
1859		      offset += frag->fr_fix;
1860		    }
1861
1862		  fill_size = frag->fr_var;
1863		  if (fill_size && frag->fr_offset > 0)
1864		    {
1865		      unsigned int count;
1866		      unsigned int off = frag->fr_fix;
1867		      for (count = frag->fr_offset; count; count--)
1868			{
1869			  if (fill_size + frag->fr_address + off <= s->s_size)
1870			    {
1871			      memcpy (buffer + frag->fr_address + off,
1872				      frag->fr_literal + frag->fr_fix,
1873				      fill_size);
1874			      off += fill_size;
1875			      offset += fill_size;
1876			    }
1877			}
1878		    }
1879		  break;
1880		case rs_broken_word:
1881		  break;
1882		default:
1883		  abort ();
1884		}
1885	      frag = frag->fr_next;
1886	    }
1887
1888	  if (s->s_size != 0)
1889	    {
1890	      if (s->s_scnptr != 0)
1891		{
1892		  bfd_write (buffer, s->s_size, 1, abfd);
1893		  *file_cursor += s->s_size;
1894		}
1895	      free (buffer);
1896	    }
1897	  paddr += s->s_size;
1898	}
1899    }
1900}
1901
1902/* Coff file generation & utilities */
1903
1904static void
1905coff_header_append (abfd, h)
1906     bfd * abfd;
1907     object_headers * h;
1908{
1909  unsigned int i;
1910  char buffer[1000];
1911  char buffero[1000];
1912#ifdef COFF_LONG_SECTION_NAMES
1913  unsigned long string_size = 4;
1914#endif
1915
1916  bfd_seek (abfd, 0, 0);
1917
1918#ifndef OBJ_COFF_OMIT_OPTIONAL_HEADER
1919  H_SET_MAGIC_NUMBER (h, COFF_MAGIC);
1920  H_SET_VERSION_STAMP (h, 0);
1921  H_SET_ENTRY_POINT (h, 0);
1922  H_SET_TEXT_START (h, segment_info[SEG_E0].frchainP->frch_root->fr_address);
1923  H_SET_DATA_START (h, segment_info[SEG_E1].frchainP->frch_root->fr_address);
1924  H_SET_SIZEOF_OPTIONAL_HEADER (h, bfd_coff_swap_aouthdr_out(abfd, &h->aouthdr,
1925							     buffero));
1926#else /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */
1927  H_SET_SIZEOF_OPTIONAL_HEADER (h, 0);
1928#endif /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */
1929
1930  i = bfd_coff_swap_filehdr_out (abfd, &h->filehdr, buffer);
1931
1932  bfd_write (buffer, i, 1, abfd);
1933  bfd_write (buffero, H_GET_SIZEOF_OPTIONAL_HEADER (h), 1, abfd);
1934
1935  for (i = SEG_E0; i < SEG_LAST; i++)
1936    {
1937      if (segment_info[i].scnhdr.s_name[0])
1938	{
1939	  unsigned int size;
1940
1941#ifdef COFF_LONG_SECTION_NAMES
1942	  /* Support long section names as found in PE.  This code
1943             must coordinate with that in write_object_file and
1944             w_strings.  */
1945	  if (strlen (segment_info[i].name) > SCNNMLEN)
1946	    {
1947	      memset (segment_info[i].scnhdr.s_name, 0, SCNNMLEN);
1948	      sprintf (segment_info[i].scnhdr.s_name, "/%lu", string_size);
1949	      string_size += strlen (segment_info[i].name) + 1;
1950	    }
1951#endif
1952
1953	  size = bfd_coff_swap_scnhdr_out (abfd,
1954					   &(segment_info[i].scnhdr),
1955					   buffer);
1956	  if (size == 0)
1957	    as_bad ("bfd_coff_swap_scnhdr_out failed");
1958	  bfd_write (buffer, size, 1, abfd);
1959	}
1960    }
1961}
1962
1963
1964char *
1965symbol_to_chars (abfd, where, symbolP)
1966     bfd * abfd;
1967     char *where;
1968     symbolS * symbolP;
1969{
1970  unsigned int numaux = symbolP->sy_symbol.ost_entry.n_numaux;
1971  unsigned int i;
1972  valueT val;
1973
1974  /* Turn any symbols with register attributes into abs symbols */
1975  if (S_GET_SEGMENT (symbolP) == reg_section)
1976    {
1977      S_SET_SEGMENT (symbolP, absolute_section);
1978    }
1979  /* At the same time, relocate all symbols to their output value */
1980
1981  val = (segment_info[S_GET_SEGMENT (symbolP)].scnhdr.s_paddr
1982	 + S_GET_VALUE (symbolP));
1983
1984  S_SET_VALUE (symbolP, val);
1985
1986  symbolP->sy_symbol.ost_entry.n_value = val;
1987
1988  where += bfd_coff_swap_sym_out (abfd, &symbolP->sy_symbol.ost_entry,
1989				  where);
1990
1991  for (i = 0; i < numaux; i++)
1992    {
1993      where += bfd_coff_swap_aux_out (abfd,
1994				      &symbolP->sy_symbol.ost_auxent[i],
1995				      S_GET_DATA_TYPE (symbolP),
1996				      S_GET_STORAGE_CLASS (symbolP),
1997				      i, numaux, where);
1998    }
1999  return where;
2000
2001}
2002
2003void
2004coff_obj_symbol_new_hook (symbolP)
2005     symbolS *symbolP;
2006{
2007  char underscore = 0;		/* Symbol has leading _ */
2008
2009  /* Effective symbol */
2010  /* Store the pointer in the offset. */
2011  S_SET_ZEROES (symbolP, 0L);
2012  S_SET_DATA_TYPE (symbolP, T_NULL);
2013  S_SET_STORAGE_CLASS (symbolP, 0);
2014  S_SET_NUMBER_AUXILIARY (symbolP, 0);
2015  /* Additional information */
2016  symbolP->sy_symbol.ost_flags = 0;
2017  /* Auxiliary entries */
2018  memset ((char *) &symbolP->sy_symbol.ost_auxent[0], 0, AUXESZ);
2019
2020  if (S_IS_STRING (symbolP))
2021    SF_SET_STRING (symbolP);
2022  if (!underscore && S_IS_LOCAL (symbolP))
2023    SF_SET_LOCAL (symbolP);
2024}
2025
2026/*
2027 * Handle .ln directives.
2028 */
2029
2030static void
2031obj_coff_ln (appline)
2032     int appline;
2033{
2034  int l;
2035
2036  if (! appline && def_symbol_in_progress != NULL)
2037    {
2038      as_warn (".ln pseudo-op inside .def/.endef: ignored.");
2039      demand_empty_rest_of_line ();
2040      return;
2041    }				/* wrong context */
2042
2043  l = get_absolute_expression ();
2044  c_line_new (0, frag_now_fix (), l, frag_now);
2045
2046  if (appline)
2047    new_logical_line ((char *) NULL, l - 1);
2048
2049#ifndef NO_LISTING
2050  {
2051    extern int listing;
2052
2053    if (listing)
2054      {
2055	if (! appline)
2056	  l += line_base - 1;
2057	listing_source_line ((unsigned int) l);
2058      }
2059
2060  }
2061#endif
2062  demand_empty_rest_of_line ();
2063}
2064
2065/*
2066 *			def()
2067 *
2068 * Handle .def directives.
2069 *
2070 * One might ask : why can't we symbol_new if the symbol does not
2071 * already exist and fill it with debug information.  Because of
2072 * the C_EFCN special symbol. It would clobber the value of the
2073 * function symbol before we have a chance to notice that it is
2074 * a C_EFCN. And a second reason is that the code is more clear this
2075 * way. (at least I think it is :-).
2076 *
2077 */
2078
2079#define SKIP_SEMI_COLON()	while (*input_line_pointer++ != ';')
2080#define SKIP_WHITESPACES()	while (*input_line_pointer == ' ' || \
2081				      *input_line_pointer == '\t') \
2082                                         input_line_pointer++;
2083
2084static void
2085obj_coff_def (what)
2086     int what;
2087{
2088  char name_end;		/* Char after the end of name */
2089  char *symbol_name;		/* Name of the debug symbol */
2090  char *symbol_name_copy;	/* Temporary copy of the name */
2091  unsigned int symbol_name_length;
2092
2093  if (def_symbol_in_progress != NULL)
2094    {
2095      as_warn (".def pseudo-op used inside of .def/.endef: ignored.");
2096      demand_empty_rest_of_line ();
2097      return;
2098    }				/* if not inside .def/.endef */
2099
2100  SKIP_WHITESPACES ();
2101
2102  def_symbol_in_progress = (symbolS *) obstack_alloc (&notes, sizeof (*def_symbol_in_progress));
2103  memset (def_symbol_in_progress, 0, sizeof (*def_symbol_in_progress));
2104
2105  symbol_name = input_line_pointer;
2106  name_end = get_symbol_end ();
2107  symbol_name_length = strlen (symbol_name);
2108  symbol_name_copy = xmalloc (symbol_name_length + 1);
2109  strcpy (symbol_name_copy, symbol_name);
2110#ifdef tc_canonicalize_symbol_name
2111  symbol_name_copy = tc_canonicalize_symbol_name (symbol_name_copy);
2112#endif
2113
2114  /* Initialize the new symbol */
2115#ifdef STRIP_UNDERSCORE
2116  S_SET_NAME (def_symbol_in_progress, (*symbol_name_copy == '_'
2117				       ? symbol_name_copy + 1
2118				       : symbol_name_copy));
2119#else /* STRIP_UNDERSCORE */
2120  S_SET_NAME (def_symbol_in_progress, symbol_name_copy);
2121#endif /* STRIP_UNDERSCORE */
2122  /* free(symbol_name_copy); */
2123  def_symbol_in_progress->sy_name_offset = (unsigned long) ~0;
2124  def_symbol_in_progress->sy_number = ~0;
2125  def_symbol_in_progress->sy_frag = &zero_address_frag;
2126  S_SET_VALUE (def_symbol_in_progress, 0);
2127
2128  if (S_IS_STRING (def_symbol_in_progress))
2129    SF_SET_STRING (def_symbol_in_progress);
2130
2131  *input_line_pointer = name_end;
2132
2133  demand_empty_rest_of_line ();
2134}
2135
2136unsigned int dim_index;
2137
2138
2139static void
2140obj_coff_endef (ignore)
2141     int ignore;
2142{
2143  symbolS *symbolP = 0;
2144  /* DIM BUG FIX sac@cygnus.com */
2145  dim_index = 0;
2146  if (def_symbol_in_progress == NULL)
2147    {
2148      as_warn (".endef pseudo-op used outside of .def/.endef: ignored.");
2149      demand_empty_rest_of_line ();
2150      return;
2151    }				/* if not inside .def/.endef */
2152
2153  /* Set the section number according to storage class. */
2154  switch (S_GET_STORAGE_CLASS (def_symbol_in_progress))
2155    {
2156    case C_STRTAG:
2157    case C_ENTAG:
2158    case C_UNTAG:
2159      SF_SET_TAG (def_symbol_in_progress);
2160      /* intentional fallthrough */
2161    case C_FILE:
2162    case C_TPDEF:
2163      SF_SET_DEBUG (def_symbol_in_progress);
2164      S_SET_SEGMENT (def_symbol_in_progress, SEG_DEBUG);
2165      break;
2166
2167    case C_EFCN:
2168      SF_SET_LOCAL (def_symbol_in_progress);	/* Do not emit this symbol. */
2169      /* intentional fallthrough */
2170    case C_BLOCK:
2171      SF_SET_PROCESS (def_symbol_in_progress);	/* Will need processing before writing */
2172      /* intentional fallthrough */
2173    case C_FCN:
2174      S_SET_SEGMENT (def_symbol_in_progress, SEG_E0);
2175
2176      if (strcmp (S_GET_NAME (def_symbol_in_progress), ".bf") == 0)
2177	{			/* .bf */
2178	  if (function_lineoff < 0)
2179	    {
2180	      fprintf (stderr, "`.bf' symbol without preceding function\n");
2181	    }			/* missing function symbol */
2182	  SA_GET_SYM_LNNOPTR (last_line_symbol) = function_lineoff;
2183
2184	  SF_SET_PROCESS (last_line_symbol);
2185	  SF_SET_ADJ_LNNOPTR (last_line_symbol);
2186	  SF_SET_PROCESS (def_symbol_in_progress);
2187	  function_lineoff = -1;
2188	}
2189      /* Value is always set to . */
2190      def_symbol_in_progress->sy_frag = frag_now;
2191      S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ());
2192      break;
2193
2194#ifdef C_AUTOARG
2195    case C_AUTOARG:
2196#endif /* C_AUTOARG */
2197    case C_AUTO:
2198    case C_REG:
2199    case C_MOS:
2200    case C_MOE:
2201    case C_MOU:
2202    case C_ARG:
2203    case C_REGPARM:
2204    case C_FIELD:
2205    case C_EOS:
2206      SF_SET_DEBUG (def_symbol_in_progress);
2207      S_SET_SEGMENT (def_symbol_in_progress, absolute_section);
2208      break;
2209
2210    case C_EXT:
2211    case C_STAT:
2212    case C_LABEL:
2213      /* Valid but set somewhere else (s_comm, s_lcomm, colon) */
2214      break;
2215
2216    case C_USTATIC:
2217    case C_EXTDEF:
2218    case C_ULABEL:
2219      as_warn ("unexpected storage class %d", S_GET_STORAGE_CLASS (def_symbol_in_progress));
2220      break;
2221    }				/* switch on storage class */
2222
2223  /* Now that we have built a debug symbol, try to find if we should
2224     merge with an existing symbol or not.  If a symbol is C_EFCN or
2225     absolute_section or untagged SEG_DEBUG it never merges.  We also
2226     don't merge labels, which are in a different namespace, nor
2227     symbols which have not yet been defined since they are typically
2228     unique, nor do we merge tags with non-tags.  */
2229
2230  /* Two cases for functions.  Either debug followed by definition or
2231     definition followed by debug.  For definition first, we will
2232     merge the debug symbol into the definition.  For debug first, the
2233     lineno entry MUST point to the definition function or else it
2234     will point off into space when crawl_symbols() merges the debug
2235     symbol into the real symbol.  Therefor, let's presume the debug
2236     symbol is a real function reference. */
2237
2238  /* FIXME-SOON If for some reason the definition label/symbol is
2239     never seen, this will probably leave an undefined symbol at link
2240     time. */
2241
2242  if (S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_EFCN
2243      || S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_LABEL
2244      || (S_GET_SEGMENT (def_symbol_in_progress) == SEG_DEBUG
2245	  && !SF_GET_TAG (def_symbol_in_progress))
2246      || S_GET_SEGMENT (def_symbol_in_progress) == absolute_section
2247      || def_symbol_in_progress->sy_value.X_op != O_constant
2248      || (symbolP = symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP)) == NULL
2249      || (SF_GET_TAG (def_symbol_in_progress) != SF_GET_TAG (symbolP)))
2250    {
2251      symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP,
2252		     &symbol_lastP);
2253    }
2254  else
2255    {
2256      /* This symbol already exists, merge the newly created symbol
2257	 into the old one.  This is not mandatory. The linker can
2258	 handle duplicate symbols correctly. But I guess that it save
2259	 a *lot* of space if the assembly file defines a lot of
2260	 symbols. [loic] */
2261
2262      /* The debug entry (def_symbol_in_progress) is merged into the
2263	 previous definition.  */
2264
2265      c_symbol_merge (def_symbol_in_progress, symbolP);
2266      /* FIXME-SOON Should *def_symbol_in_progress be free'd? xoxorich. */
2267      def_symbol_in_progress = symbolP;
2268
2269      if (SF_GET_FUNCTION (def_symbol_in_progress)
2270	  || SF_GET_TAG (def_symbol_in_progress)
2271	  || S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_STAT)
2272	{
2273	  /* For functions, and tags, and static symbols, the symbol
2274	     *must* be where the debug symbol appears.  Move the
2275	     existing symbol to the current place. */
2276	  /* If it already is at the end of the symbol list, do nothing */
2277	  if (def_symbol_in_progress != symbol_lastP)
2278	    {
2279	      symbol_remove (def_symbol_in_progress, &symbol_rootP,
2280			     &symbol_lastP);
2281	      symbol_append (def_symbol_in_progress, symbol_lastP,
2282			     &symbol_rootP, &symbol_lastP);
2283	    }			/* if not already in place */
2284	}			/* if function */
2285    }				/* normal or mergable */
2286
2287  if (SF_GET_TAG (def_symbol_in_progress))
2288    {
2289      symbolS *oldtag;
2290
2291      oldtag = symbol_find_base (S_GET_NAME (def_symbol_in_progress),
2292				 DO_NOT_STRIP);
2293      if (oldtag == NULL || ! SF_GET_TAG (oldtag))
2294	tag_insert (S_GET_NAME (def_symbol_in_progress),
2295		    def_symbol_in_progress);
2296    }
2297
2298  if (SF_GET_FUNCTION (def_symbol_in_progress))
2299    {
2300      know (sizeof (def_symbol_in_progress) <= sizeof (long));
2301      function_lineoff
2302	= c_line_new (def_symbol_in_progress, 0, 0, &zero_address_frag);
2303
2304      SF_SET_PROCESS (def_symbol_in_progress);
2305
2306      if (symbolP == NULL)
2307	{
2308	  /* That is, if this is the first time we've seen the
2309	     function... */
2310	  symbol_table_insert (def_symbol_in_progress);
2311	}			/* definition follows debug */
2312    }				/* Create the line number entry pointing to the function being defined */
2313
2314  def_symbol_in_progress = NULL;
2315  demand_empty_rest_of_line ();
2316}
2317
2318static void
2319obj_coff_dim (ignore)
2320     int ignore;
2321{
2322  int dim_index;
2323
2324  if (def_symbol_in_progress == NULL)
2325    {
2326      as_warn (".dim pseudo-op used outside of .def/.endef: ignored.");
2327      demand_empty_rest_of_line ();
2328      return;
2329    }				/* if not inside .def/.endef */
2330
2331  S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
2332
2333  for (dim_index = 0; dim_index < DIMNUM; dim_index++)
2334    {
2335      SKIP_WHITESPACES ();
2336      SA_SET_SYM_DIMEN (def_symbol_in_progress, dim_index,
2337			get_absolute_expression ());
2338
2339      switch (*input_line_pointer)
2340	{
2341	case ',':
2342	  input_line_pointer++;
2343	  break;
2344
2345	default:
2346	  as_warn ("badly formed .dim directive ignored");
2347	  /* intentional fallthrough */
2348	case '\n':
2349	case ';':
2350	  dim_index = DIMNUM;
2351	  break;
2352	}
2353    }
2354
2355  demand_empty_rest_of_line ();
2356}
2357
2358static void
2359obj_coff_line (ignore)
2360     int ignore;
2361{
2362  int this_base;
2363  const char *name;
2364
2365  if (def_symbol_in_progress == NULL)
2366    {
2367      obj_coff_ln (0);
2368      return;
2369    }
2370
2371  name = S_GET_NAME (def_symbol_in_progress);
2372  this_base = get_absolute_expression ();
2373
2374  /* Only .bf symbols indicate the use of a new base line number; the
2375     line numbers associated with .ef, .bb, .eb are relative to the
2376     start of the containing function.  */
2377  if (!strcmp (".bf", name))
2378    {
2379#if 0 /* XXX Can we ever have line numbers going backwards?  */
2380      if (this_base > line_base)
2381#endif
2382	{
2383	  line_base = this_base;
2384	}
2385
2386#ifndef NO_LISTING
2387      {
2388	extern int listing;
2389	if (listing)
2390	  {
2391	    listing_source_line ((unsigned int) line_base);
2392	  }
2393      }
2394#endif
2395    }
2396
2397  S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
2398  SA_SET_SYM_LNNO (def_symbol_in_progress, this_base);
2399
2400  demand_empty_rest_of_line ();
2401}
2402
2403static void
2404obj_coff_size (ignore)
2405     int ignore;
2406{
2407  if (def_symbol_in_progress == NULL)
2408    {
2409      as_warn (".size pseudo-op used outside of .def/.endef ignored.");
2410      demand_empty_rest_of_line ();
2411      return;
2412    }				/* if not inside .def/.endef */
2413
2414  S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
2415  SA_SET_SYM_SIZE (def_symbol_in_progress, get_absolute_expression ());
2416  demand_empty_rest_of_line ();
2417}
2418
2419static void
2420obj_coff_scl (ignore)
2421     int ignore;
2422{
2423  if (def_symbol_in_progress == NULL)
2424    {
2425      as_warn (".scl pseudo-op used outside of .def/.endef ignored.");
2426      demand_empty_rest_of_line ();
2427      return;
2428    }				/* if not inside .def/.endef */
2429
2430  S_SET_STORAGE_CLASS (def_symbol_in_progress, get_absolute_expression ());
2431  demand_empty_rest_of_line ();
2432}
2433
2434static void
2435obj_coff_tag (ignore)
2436     int ignore;
2437{
2438  char *symbol_name;
2439  char name_end;
2440
2441  if (def_symbol_in_progress == NULL)
2442    {
2443      as_warn (".tag pseudo-op used outside of .def/.endef ignored.");
2444      demand_empty_rest_of_line ();
2445      return;
2446    }
2447
2448  S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
2449  symbol_name = input_line_pointer;
2450  name_end = get_symbol_end ();
2451#ifdef tc_canonicalize_symbol_name
2452  symbol_name = tc_canonicalize_symbol_name (symbol_name);
2453#endif
2454
2455  /* Assume that the symbol referred to by .tag is always defined.
2456     This was a bad assumption.  I've added find_or_make. xoxorich. */
2457  SA_SET_SYM_TAGNDX (def_symbol_in_progress,
2458		     (long) tag_find_or_make (symbol_name));
2459  if (SA_GET_SYM_TAGNDX (def_symbol_in_progress) == 0L)
2460    {
2461      as_warn ("tag not found for .tag %s", symbol_name);
2462    }				/* not defined */
2463
2464  SF_SET_TAGGED (def_symbol_in_progress);
2465  *input_line_pointer = name_end;
2466
2467  demand_empty_rest_of_line ();
2468}
2469
2470static void
2471obj_coff_type (ignore)
2472     int ignore;
2473{
2474  if (def_symbol_in_progress == NULL)
2475    {
2476      as_warn (".type pseudo-op used outside of .def/.endef ignored.");
2477      demand_empty_rest_of_line ();
2478      return;
2479    }				/* if not inside .def/.endef */
2480
2481  S_SET_DATA_TYPE (def_symbol_in_progress, get_absolute_expression ());
2482
2483  if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress)) &&
2484      S_GET_STORAGE_CLASS (def_symbol_in_progress) != C_TPDEF)
2485    {
2486      SF_SET_FUNCTION (def_symbol_in_progress);
2487    }				/* is a function */
2488
2489  demand_empty_rest_of_line ();
2490}
2491
2492static void
2493obj_coff_val (ignore)
2494     int ignore;
2495{
2496  if (def_symbol_in_progress == NULL)
2497    {
2498      as_warn (".val pseudo-op used outside of .def/.endef ignored.");
2499      demand_empty_rest_of_line ();
2500      return;
2501    }				/* if not inside .def/.endef */
2502
2503  if (is_name_beginner (*input_line_pointer))
2504    {
2505      char *symbol_name = input_line_pointer;
2506      char name_end = get_symbol_end ();
2507
2508#ifdef tc_canonicalize_symbol_name
2509  symbol_name = tc_canonicalize_symbol_name (symbol_name);
2510#endif
2511
2512      if (!strcmp (symbol_name, "."))
2513	{
2514	  def_symbol_in_progress->sy_frag = frag_now;
2515	  S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ());
2516	  /* If the .val is != from the .def (e.g. statics) */
2517	}
2518      else if (strcmp (S_GET_NAME (def_symbol_in_progress), symbol_name))
2519	{
2520	  def_symbol_in_progress->sy_value.X_op = O_symbol;
2521	  def_symbol_in_progress->sy_value.X_add_symbol =
2522	    symbol_find_or_make (symbol_name);
2523	  def_symbol_in_progress->sy_value.X_op_symbol = NULL;
2524	  def_symbol_in_progress->sy_value.X_add_number = 0;
2525
2526	  /* If the segment is undefined when the forward reference is
2527	     resolved, then copy the segment id from the forward
2528	     symbol.  */
2529	  SF_SET_GET_SEGMENT (def_symbol_in_progress);
2530
2531	  /* FIXME: gcc can generate address expressions
2532	     here in unusual cases (search for "obscure"
2533	     in sdbout.c).  We just ignore the offset
2534	     here, thus generating incorrect debugging
2535	     information.  We ignore the rest of the
2536	     line just below.  */
2537	}
2538      /* Otherwise, it is the name of a non debug symbol and
2539	 its value will be calculated later. */
2540      *input_line_pointer = name_end;
2541
2542      /* FIXME: this is to avoid an error message in the
2543	 FIXME case mentioned just above.  */
2544      while (! is_end_of_line[(unsigned char) *input_line_pointer])
2545	++input_line_pointer;
2546    }
2547  else
2548    {
2549      S_SET_VALUE (def_symbol_in_progress,
2550		   (valueT) get_absolute_expression ());
2551    }				/* if symbol based */
2552
2553  demand_empty_rest_of_line ();
2554}
2555
2556#ifdef TE_PE
2557
2558/* Handle the .linkonce pseudo-op.  This is parsed by s_linkonce in
2559   read.c, which then calls this object file format specific routine.  */
2560
2561void
2562obj_coff_pe_handle_link_once (type)
2563     enum linkonce_type type;
2564{
2565  seg_info (now_seg)->scnhdr.s_flags |= IMAGE_SCN_LNK_COMDAT;
2566
2567  /* We store the type in the seg_info structure, and use it to set up
2568     the auxiliary entry for the section symbol in c_section_symbol.  */
2569  seg_info (now_seg)->linkonce = type;
2570}
2571
2572#endif /* TE_PE */
2573
2574void
2575coff_obj_read_begin_hook ()
2576{
2577  /* These had better be the same.  Usually 18 bytes. */
2578#ifndef BFD_HEADERS
2579  know (sizeof (SYMENT) == sizeof (AUXENT));
2580  know (SYMESZ == AUXESZ);
2581#endif
2582  tag_init ();
2583}
2584
2585/* This function runs through the symbol table and puts all the
2586   externals onto another chain */
2587
2588/* The chain of globals.  */
2589symbolS *symbol_globalP;
2590symbolS *symbol_global_lastP;
2591
2592/* The chain of externals */
2593symbolS *symbol_externP;
2594symbolS *symbol_extern_lastP;
2595
2596stack *block_stack;
2597symbolS *last_functionP;
2598static symbolS *last_bfP;
2599symbolS *last_tagP;
2600
2601static unsigned int
2602yank_symbols ()
2603{
2604  symbolS *symbolP;
2605  unsigned int symbol_number = 0;
2606  unsigned int last_file_symno = 0;
2607
2608  struct filename_list *filename_list_scan = filename_list_head;
2609
2610  for (symbolP = symbol_rootP;
2611       symbolP;
2612       symbolP = symbolP ? symbol_next (symbolP) : symbol_rootP)
2613    {
2614      if (symbolP->sy_mri_common)
2615	{
2616	  if (S_GET_STORAGE_CLASS (symbolP) == C_EXT)
2617	    as_bad ("%s: global symbols not supported in common sections",
2618		    S_GET_NAME (symbolP));
2619	  symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2620	  continue;
2621	}
2622
2623      if (!SF_GET_DEBUG (symbolP))
2624	{
2625	  /* Debug symbols do not need all this rubbish */
2626	  symbolS *real_symbolP;
2627
2628	  /* L* and C_EFCN symbols never merge. */
2629	  if (!SF_GET_LOCAL (symbolP)
2630	      && !SF_GET_STATICS (symbolP)
2631	      && S_GET_STORAGE_CLASS (symbolP) != C_LABEL
2632	      && symbolP->sy_value.X_op == O_constant
2633	      && (real_symbolP = symbol_find_base (S_GET_NAME (symbolP), DO_NOT_STRIP))
2634	      && real_symbolP != symbolP)
2635	    {
2636	      /* FIXME-SOON: where do dups come from?
2637		 Maybe tag references before definitions? xoxorich. */
2638	      /* Move the debug data from the debug symbol to the
2639		 real symbol. Do NOT do the oposite (i.e. move from
2640		 real symbol to debug symbol and remove real symbol from the
2641		 list.) Because some pointers refer to the real symbol
2642		 whereas no pointers refer to the debug symbol. */
2643	      c_symbol_merge (symbolP, real_symbolP);
2644	      /* Replace the current symbol by the real one */
2645	      /* The symbols will never be the last or the first
2646		 because : 1st symbol is .file and 3 last symbols are
2647		 .text, .data, .bss */
2648	      symbol_remove (real_symbolP, &symbol_rootP, &symbol_lastP);
2649	      symbol_insert (real_symbolP, symbolP, &symbol_rootP, &symbol_lastP);
2650	      symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2651	      symbolP = real_symbolP;
2652	    }			/* if not local but dup'd */
2653
2654	  if (flag_readonly_data_in_text && (S_GET_SEGMENT (symbolP) == SEG_E1))
2655	    {
2656	      S_SET_SEGMENT (symbolP, SEG_E0);
2657	    }			/* push data into text */
2658
2659	  resolve_symbol_value (symbolP);
2660
2661	  if (S_GET_STORAGE_CLASS (symbolP) == C_NULL)
2662	    {
2663	      if (!S_IS_DEFINED (symbolP) && !SF_GET_LOCAL (symbolP))
2664		{
2665		  S_SET_EXTERNAL (symbolP);
2666		}
2667	      else if (S_GET_SEGMENT (symbolP) == SEG_E0)
2668		{
2669		  S_SET_STORAGE_CLASS (symbolP, C_LABEL);
2670		}
2671	      else
2672		{
2673		  S_SET_STORAGE_CLASS (symbolP, C_STAT);
2674		}
2675	    }
2676
2677	  /* Mainly to speed up if not -g */
2678	  if (SF_GET_PROCESS (symbolP))
2679	    {
2680	      /* Handle the nested blocks auxiliary info. */
2681	      if (S_GET_STORAGE_CLASS (symbolP) == C_BLOCK)
2682		{
2683		  if (!strcmp (S_GET_NAME (symbolP), ".bb"))
2684		    stack_push (block_stack, (char *) &symbolP);
2685		  else
2686		    {		/* .eb */
2687		      register symbolS *begin_symbolP;
2688		      begin_symbolP = *(symbolS **) stack_pop (block_stack);
2689		      if (begin_symbolP == (symbolS *) 0)
2690			as_warn ("mismatched .eb");
2691		      else
2692			SA_SET_SYM_ENDNDX (begin_symbolP, symbol_number + 2);
2693		    }
2694		}
2695	      /* If we are able to identify the type of a function, and we
2696	       are out of a function (last_functionP == 0) then, the
2697	       function symbol will be associated with an auxiliary
2698	       entry. */
2699	      if (last_functionP == (symbolS *) 0 &&
2700		  SF_GET_FUNCTION (symbolP))
2701		{
2702		  last_functionP = symbolP;
2703
2704		  if (S_GET_NUMBER_AUXILIARY (symbolP) < 1)
2705		    {
2706		      S_SET_NUMBER_AUXILIARY (symbolP, 1);
2707		    }		/* make it at least 1 */
2708
2709		  /* Clobber possible stale .dim information. */
2710#if 0
2711		  /* Iffed out by steve - this fries the lnnoptr info too */
2712		  bzero (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen,
2713			 sizeof (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen));
2714#endif
2715		}
2716	      if (S_GET_STORAGE_CLASS (symbolP) == C_FCN)
2717		{
2718		  if (strcmp (S_GET_NAME (symbolP), ".bf") == 0)
2719		    {
2720		      if (last_bfP != NULL)
2721			SA_SET_SYM_ENDNDX (last_bfP, symbol_number);
2722		      last_bfP = symbolP;
2723		    }
2724		}
2725	      else if (S_GET_STORAGE_CLASS (symbolP) == C_EFCN)
2726		{
2727		  /* I don't even know if this is needed for sdb. But
2728		     the standard assembler generates it, so...  */
2729		  if (last_functionP == (symbolS *) 0)
2730		    as_fatal ("C_EFCN symbol out of scope");
2731		  SA_SET_SYM_FSIZE (last_functionP,
2732				    (long) (S_GET_VALUE (symbolP) -
2733					    S_GET_VALUE (last_functionP)));
2734		  SA_SET_SYM_ENDNDX (last_functionP, symbol_number);
2735		 last_functionP = (symbolS *) 0;
2736		}
2737	    }
2738	}
2739      else if (SF_GET_TAG (symbolP))
2740	{
2741	  /* First descriptor of a structure must point to
2742	       the first slot after the structure description. */
2743	  last_tagP = symbolP;
2744
2745	}
2746      else if (S_GET_STORAGE_CLASS (symbolP) == C_EOS)
2747	{
2748	  /* +2 take in account the current symbol */
2749	  SA_SET_SYM_ENDNDX (last_tagP, symbol_number + 2);
2750	}
2751      else if (S_GET_STORAGE_CLASS (symbolP) == C_FILE)
2752	{
2753	  /* If the filename was too long to fit in the
2754	     auxent, put it in the string table */
2755	  if (SA_GET_FILE_FNAME_ZEROS (symbolP) == 0
2756	      && SA_GET_FILE_FNAME_OFFSET (symbolP) != 0)
2757	    {
2758	      SA_SET_FILE_FNAME_OFFSET (symbolP, string_byte_count);
2759	      string_byte_count += strlen (filename_list_scan->filename) + 1;
2760	      filename_list_scan = filename_list_scan->next;
2761	    }
2762	  if (S_GET_VALUE (symbolP))
2763	    {
2764	      S_SET_VALUE (symbolP, last_file_symno);
2765	      last_file_symno = symbol_number;
2766	    }			/* no one points at the first .file symbol */
2767	}			/* if debug or tag or eos or file */
2768
2769#ifdef tc_frob_coff_symbol
2770      tc_frob_coff_symbol (symbolP);
2771#endif
2772
2773      /* We must put the external symbols apart. The loader
2774	 does not bomb if we do not. But the references in
2775	 the endndx field for a .bb symbol are not corrected
2776	 if an external symbol is removed between .bb and .be.
2777	 I.e in the following case :
2778	 [20] .bb endndx = 22
2779	 [21] foo external
2780	 [22] .be
2781	 ld will move the symbol 21 to the end of the list but
2782	 endndx will still be 22 instead of 21. */
2783
2784
2785      if (SF_GET_LOCAL (symbolP))
2786	{
2787	  /* remove C_EFCN and LOCAL (L...) symbols */
2788	  /* next pointer remains valid */
2789	  symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2790
2791	}
2792      else if (symbolP->sy_value.X_op == O_symbol
2793	       && (! S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP)))
2794	{
2795	  /* Skip symbols which were equated to undefined or common
2796	     symbols.  */
2797	  symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2798	}
2799      else if (!S_IS_DEFINED (symbolP)
2800	       && !S_IS_DEBUG (symbolP)
2801	       && !SF_GET_STATICS (symbolP) &&
2802	       S_GET_STORAGE_CLASS (symbolP) == C_EXT)
2803	{			/* C_EXT && !SF_GET_FUNCTION(symbolP))  */
2804	  /* if external, Remove from the list */
2805	  symbolS *hold = symbol_previous (symbolP);
2806
2807	  symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2808	  symbol_clear_list_pointers (symbolP);
2809	  symbol_append (symbolP, symbol_extern_lastP, &symbol_externP, &symbol_extern_lastP);
2810	  symbolP = hold;
2811	}
2812      else if (! S_IS_DEBUG (symbolP)
2813	       && ! SF_GET_STATICS (symbolP)
2814	       && ! SF_GET_FUNCTION (symbolP)
2815	       && S_GET_STORAGE_CLASS (symbolP) == C_EXT)
2816	{
2817	  symbolS *hold = symbol_previous (symbolP);
2818
2819	  /* The O'Reilly COFF book says that defined global symbols
2820             come at the end of the symbol table, just before
2821             undefined global symbols.  */
2822
2823	  symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2824	  symbol_clear_list_pointers (symbolP);
2825	  symbol_append (symbolP, symbol_global_lastP, &symbol_globalP,
2826			 &symbol_global_lastP);
2827	  symbolP = hold;
2828	}
2829      else
2830	{
2831	  if (SF_GET_STRING (symbolP))
2832	    {
2833	      symbolP->sy_name_offset = string_byte_count;
2834	      string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
2835	    }
2836	  else
2837	    {
2838	      symbolP->sy_name_offset = 0;
2839	    }			/* fix "long" names */
2840
2841	  symbolP->sy_number = symbol_number;
2842	  symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
2843	}			/* if local symbol */
2844    }				/* traverse the symbol list */
2845  return symbol_number;
2846
2847}
2848
2849
2850static unsigned int
2851glue_symbols (head, tail)
2852     symbolS **head;
2853     symbolS **tail;
2854{
2855  unsigned int symbol_number = 0;
2856
2857  while (*head != NULL)
2858    {
2859      symbolS *tmp = *head;
2860
2861      /* append */
2862      symbol_remove (tmp, head, tail);
2863      symbol_append (tmp, symbol_lastP, &symbol_rootP, &symbol_lastP);
2864
2865      /* and process */
2866      if (SF_GET_STRING (tmp))
2867	{
2868	  tmp->sy_name_offset = string_byte_count;
2869	  string_byte_count += strlen (S_GET_NAME (tmp)) + 1;
2870	}
2871      else
2872	{
2873	  tmp->sy_name_offset = 0;
2874	}			/* fix "long" names */
2875
2876      tmp->sy_number = symbol_number;
2877      symbol_number += 1 + S_GET_NUMBER_AUXILIARY (tmp);
2878    }				/* append the entire extern chain */
2879
2880  return symbol_number;
2881}
2882
2883static unsigned int
2884tie_tags ()
2885{
2886  unsigned int symbol_number = 0;
2887  symbolS *symbolP;
2888
2889  for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
2890    {
2891      symbolP->sy_number = symbol_number;
2892
2893      if (SF_GET_TAGGED (symbolP))
2894	{
2895	  SA_SET_SYM_TAGNDX
2896	    (symbolP,
2897	     ((symbolS *) SA_GET_SYM_TAGNDX (symbolP))->sy_number);
2898	}
2899
2900      symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
2901    }
2902
2903  return symbol_number;
2904}
2905
2906static void
2907crawl_symbols (h, abfd)
2908     object_headers *h;
2909     bfd * abfd;
2910{
2911  unsigned int i;
2912
2913  /* Initialize the stack used to keep track of the matching .bb .be */
2914
2915  block_stack = stack_init (512, sizeof (symbolS *));
2916
2917  /* The symbol list should be ordered according to the following sequence
2918   * order :
2919   * . .file symbol
2920   * . debug entries for functions
2921   * . fake symbols for the sections, including .text .data and .bss
2922   * . defined symbols
2923   * . undefined symbols
2924   * But this is not mandatory. The only important point is to put the
2925   * undefined symbols at the end of the list.
2926   */
2927
2928  /* Is there a .file symbol ? If not insert one at the beginning. */
2929  if (symbol_rootP == NULL
2930      || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE)
2931    {
2932      c_dot_file_symbol ("fake");
2933    }
2934
2935  /*
2936   * Build up static symbols for the sections, they are filled in later
2937   */
2938
2939
2940  for (i = SEG_E0; i < SEG_LAST; i++)
2941    if (segment_info[i].scnhdr.s_name[0])
2942      segment_info[i].dot = c_section_symbol (segment_info[i].name,
2943					      i - SEG_E0 + 1);
2944
2945  /* Take all the externals out and put them into another chain */
2946  H_SET_SYMBOL_TABLE_SIZE (h, yank_symbols ());
2947  /* Take the externals and glue them onto the end.*/
2948  H_SET_SYMBOL_TABLE_SIZE (h,
2949			   (H_GET_SYMBOL_COUNT (h)
2950			    + glue_symbols (&symbol_globalP,
2951					    &symbol_global_lastP)
2952			    + glue_symbols (&symbol_externP,
2953					    &symbol_extern_lastP)));
2954
2955  H_SET_SYMBOL_TABLE_SIZE (h, tie_tags ());
2956  know (symbol_globalP == NULL);
2957  know (symbol_global_lastP == NULL);
2958  know (symbol_externP == NULL);
2959  know (symbol_extern_lastP == NULL);
2960}
2961
2962/*
2963 * Find strings by crawling along symbol table chain.
2964 */
2965
2966void
2967w_strings (where)
2968     char *where;
2969{
2970  symbolS *symbolP;
2971  struct filename_list *filename_list_scan = filename_list_head;
2972
2973  /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
2974  md_number_to_chars (where, (valueT) string_byte_count, 4);
2975  where += 4;
2976
2977#ifdef COFF_LONG_SECTION_NAMES
2978  /* Support long section names as found in PE.  This code must
2979     coordinate with that in coff_header_append and write_object_file.  */
2980  {
2981    unsigned int i;
2982
2983    for (i = SEG_E0; i < SEG_LAST; i++)
2984      {
2985	if (segment_info[i].scnhdr.s_name[0]
2986	    && strlen (segment_info[i].name) > SCNNMLEN)
2987	  {
2988	    unsigned int size;
2989
2990	    size = strlen (segment_info[i].name) + 1;
2991	    memcpy (where, segment_info[i].name, size);
2992	    where += size;
2993	  }
2994      }
2995  }
2996#endif /* COFF_LONG_SECTION_NAMES */
2997
2998  for (symbolP = symbol_rootP;
2999       symbolP;
3000       symbolP = symbol_next (symbolP))
3001    {
3002      unsigned int size;
3003
3004      if (SF_GET_STRING (symbolP))
3005	{
3006	  size = strlen (S_GET_NAME (symbolP)) + 1;
3007	  memcpy (where, S_GET_NAME (symbolP), size);
3008	  where += size;
3009	}
3010      if (S_GET_STORAGE_CLASS (symbolP) == C_FILE
3011	  && SA_GET_FILE_FNAME_ZEROS (symbolP) == 0
3012	  && SA_GET_FILE_FNAME_OFFSET (symbolP) != 0)
3013	{
3014	  size = strlen (filename_list_scan->filename) + 1;
3015	  memcpy (where, filename_list_scan->filename, size);
3016	  filename_list_scan = filename_list_scan ->next;
3017	  where += size;
3018	}
3019    }
3020}
3021
3022static void
3023do_linenos_for (abfd, h, file_cursor)
3024     bfd * abfd;
3025     object_headers * h;
3026     unsigned long *file_cursor;
3027{
3028  unsigned int idx;
3029  unsigned long start = *file_cursor;
3030
3031  for (idx = SEG_E0; idx < SEG_LAST; idx++)
3032    {
3033      segment_info_type *s = segment_info + idx;
3034
3035
3036      if (s->scnhdr.s_nlnno != 0)
3037	{
3038	  struct lineno_list *line_ptr;
3039
3040	  struct external_lineno *buffer =
3041	  (struct external_lineno *) xmalloc (s->scnhdr.s_nlnno * LINESZ);
3042
3043	  struct external_lineno *dst = buffer;
3044
3045	  /* Run through the table we've built and turn it into its external
3046	 form, take this chance to remove duplicates */
3047
3048	  for (line_ptr = s->lineno_list_head;
3049	       line_ptr != (struct lineno_list *) NULL;
3050	       line_ptr = line_ptr->next)
3051	    {
3052
3053	      if (line_ptr->line.l_lnno == 0)
3054		{
3055		  /* Turn a pointer to a symbol into the symbols' index */
3056		  line_ptr->line.l_addr.l_symndx =
3057		    ((symbolS *) line_ptr->line.l_addr.l_symndx)->sy_number;
3058		}
3059	      else
3060		{
3061		  line_ptr->line.l_addr.l_paddr += ((struct frag *) (line_ptr->frag))->fr_address;
3062		}
3063
3064
3065	      (void) bfd_coff_swap_lineno_out (abfd, &(line_ptr->line), dst);
3066	      dst++;
3067
3068	    }
3069
3070	  s->scnhdr.s_lnnoptr = *file_cursor;
3071
3072	  bfd_write (buffer, 1, s->scnhdr.s_nlnno * LINESZ, abfd);
3073	  free (buffer);
3074
3075	  *file_cursor += s->scnhdr.s_nlnno * LINESZ;
3076	}
3077    }
3078  H_SET_LINENO_SIZE (h, *file_cursor - start);
3079}
3080
3081
3082/* Now we run through the list of frag chains in a segment and
3083   make all the subsegment frags appear at the end of the
3084   list, as if the seg 0 was extra long */
3085
3086static void
3087remove_subsegs ()
3088{
3089  unsigned int i;
3090
3091  for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3092    {
3093      frchainS *head = segment_info[i].frchainP;
3094      fragS dummy;
3095      fragS *prev_frag = &dummy;
3096
3097      while (head && head->frch_seg == i)
3098	{
3099	  prev_frag->fr_next = head->frch_root;
3100	  prev_frag = head->frch_last;
3101	  head = head->frch_next;
3102	}
3103      prev_frag->fr_next = 0;
3104    }
3105}
3106
3107unsigned long machine;
3108int coff_flags;
3109extern void
3110write_object_file ()
3111{
3112  int i;
3113  const char *name;
3114  struct frchain *frchain_ptr;
3115
3116  object_headers headers;
3117  unsigned long file_cursor;
3118  bfd *abfd;
3119  unsigned int addr;
3120  abfd = bfd_openw (out_file_name, TARGET_FORMAT);
3121
3122
3123  if (abfd == 0)
3124    {
3125      as_perror ("FATAL: Can't create %s", out_file_name);
3126      exit (EXIT_FAILURE);
3127    }
3128  bfd_set_format (abfd, bfd_object);
3129  bfd_set_arch_mach (abfd, BFD_ARCH, machine);
3130
3131  string_byte_count = 4;
3132
3133  for (frchain_ptr = frchain_root;
3134       frchain_ptr != (struct frchain *) NULL;
3135       frchain_ptr = frchain_ptr->frch_next)
3136    {
3137      /* Run through all the sub-segments and align them up.  Also
3138	 close any open frags.  We tack a .fill onto the end of the
3139	 frag chain so that any .align's size can be worked by looking
3140	 at the next frag.  */
3141
3142      subseg_set (frchain_ptr->frch_seg, frchain_ptr->frch_subseg);
3143#ifndef SUB_SEGMENT_ALIGN
3144#define SUB_SEGMENT_ALIGN(SEG) 1
3145#endif
3146#ifdef md_do_align
3147      md_do_align (SUB_SEGMENT_ALIGN (now_seg), (char *) NULL, 0, 0,
3148		   alignment_done);
3149#endif
3150      frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE, 0);
3151#ifdef md_do_align
3152    alignment_done:
3153#endif
3154      frag_wane (frag_now);
3155      frag_now->fr_fix = 0;
3156      know (frag_now->fr_next == NULL);
3157    }
3158
3159
3160  remove_subsegs ();
3161
3162
3163  for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3164    {
3165      relax_segment (segment_info[i].frchainP->frch_root, i);
3166    }
3167
3168  H_SET_NUMBER_OF_SECTIONS (&headers, 0);
3169
3170  /* Find out how big the sections are, and set the addresses.  */
3171  addr = 0;
3172  for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3173    {
3174      long size;
3175
3176      segment_info[i].scnhdr.s_paddr = addr;
3177      segment_info[i].scnhdr.s_vaddr = addr;
3178
3179      if (segment_info[i].scnhdr.s_name[0])
3180	{
3181	  H_SET_NUMBER_OF_SECTIONS (&headers,
3182				    H_GET_NUMBER_OF_SECTIONS (&headers) + 1);
3183
3184#ifdef COFF_LONG_SECTION_NAMES
3185	  /* Support long section names as found in PE.  This code
3186	     must coordinate with that in coff_header_append and
3187	     w_strings.  */
3188	  {
3189	    unsigned int len;
3190
3191	    len = strlen (segment_info[i].name);
3192	    if (len > SCNNMLEN)
3193	      string_byte_count += len + 1;
3194	  }
3195#endif /* COFF_LONG_SECTION_NAMES */
3196	}
3197
3198      size = size_section (abfd, (unsigned int) i);
3199      addr += size;
3200
3201      /* I think the section alignment is only used on the i960; the
3202	 i960 needs it, and it should do no harm on other targets.  */
3203      segment_info[i].scnhdr.s_align = 1 << section_alignment[i];
3204
3205      if (i == SEG_E0)
3206	H_SET_TEXT_SIZE (&headers, size);
3207      else if (i == SEG_E1)
3208	H_SET_DATA_SIZE (&headers, size);
3209      else if (i == SEG_E2)
3210	H_SET_BSS_SIZE (&headers, size);
3211    }
3212
3213  /* Turn the gas native symbol table shape into a coff symbol table */
3214  crawl_symbols (&headers, abfd);
3215
3216  if (string_byte_count == 4)
3217    string_byte_count = 0;
3218
3219  H_SET_STRING_SIZE (&headers, string_byte_count);
3220
3221#ifdef tc_frob_file
3222  tc_frob_file ();
3223#endif
3224
3225  for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3226    {
3227      fixup_mdeps (segment_info[i].frchainP->frch_root, &headers, i);
3228      fixup_segment (&segment_info[i], i);
3229    }
3230
3231  /* Look for ".stab" segments and fill in their initial symbols
3232     correctly. */
3233  for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3234    {
3235      name = segment_info[i].name;
3236
3237      if (name != NULL
3238	  && strncmp (".stab", name, 5) == 0
3239	  && strncmp (".stabstr", name, 8) != 0)
3240	adjust_stab_section (abfd, i);
3241    }
3242
3243  file_cursor = H_GET_TEXT_FILE_OFFSET (&headers);
3244
3245  bfd_seek (abfd, (file_ptr) file_cursor, 0);
3246
3247  /* Plant the data */
3248
3249  fill_section (abfd, &headers, &file_cursor);
3250
3251  do_relocs_for (abfd, &headers, &file_cursor);
3252
3253  do_linenos_for (abfd, &headers, &file_cursor);
3254
3255  H_SET_FILE_MAGIC_NUMBER (&headers, COFF_MAGIC);
3256#ifndef OBJ_COFF_OMIT_TIMESTAMP
3257  H_SET_TIME_STAMP (&headers, (long)time((time_t *)0));
3258#else
3259  H_SET_TIME_STAMP (&headers, 0);
3260#endif
3261#ifdef TC_COFF_SET_MACHINE
3262  TC_COFF_SET_MACHINE (&headers);
3263#endif
3264
3265#ifndef COFF_FLAGS
3266#define COFF_FLAGS 0
3267#endif
3268
3269#ifdef KEEP_RELOC_INFO
3270  H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers) ? 0 : F_LNNO) |
3271			  COFF_FLAGS | coff_flags));
3272#else
3273  H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers)     ? 0 : F_LNNO)   |
3274			  (H_GET_RELOCATION_SIZE(&headers) ? 0 : F_RELFLG) |
3275			  COFF_FLAGS | coff_flags));
3276#endif
3277
3278  {
3279    unsigned int symtable_size = H_GET_SYMBOL_TABLE_SIZE (&headers);
3280    char *buffer1 = xmalloc (symtable_size + string_byte_count + 1);
3281
3282    H_SET_SYMBOL_TABLE_POINTER (&headers, bfd_tell (abfd));
3283    w_symbols (abfd, buffer1, symbol_rootP);
3284    if (string_byte_count > 0)
3285      w_strings (buffer1 + symtable_size);
3286    bfd_write (buffer1, 1, symtable_size + string_byte_count, abfd);
3287    free (buffer1);
3288  }
3289
3290  coff_header_append (abfd, &headers);
3291#if 0
3292  /* Recent changes to write need this, but where it should
3293     go is up to Ken.. */
3294  if (bfd_close_all_done (abfd) == false)
3295    as_fatal ("Can't close %s: %s", out_file_name,
3296	      bfd_errmsg (bfd_get_error ()));
3297#else
3298  {
3299    extern bfd *stdoutput;
3300    stdoutput = abfd;
3301  }
3302#endif
3303
3304}
3305
3306/* Add a new segment.  This is called from subseg_new via the
3307   obj_new_segment macro.  */
3308
3309segT
3310obj_coff_add_segment (name)
3311     const char *name;
3312{
3313  unsigned int i;
3314
3315#ifndef COFF_LONG_SECTION_NAMES
3316  char buf[SCNNMLEN + 1];
3317
3318  strncpy (buf, name, SCNNMLEN);
3319  buf[SCNNMLEN] = '\0';
3320  name = buf;
3321#endif
3322
3323  for (i = SEG_E0; i < SEG_LAST && segment_info[i].scnhdr.s_name[0]; i++)
3324    if (strcmp (name, segment_info[i].name) == 0)
3325      return (segT) i;
3326
3327  if (i == SEG_LAST)
3328    {
3329      as_bad ("Too many new sections; can't add \"%s\"", name);
3330      return now_seg;
3331    }
3332
3333  /* Add a new section.  */
3334  strncpy (segment_info[i].scnhdr.s_name, name,
3335	   sizeof (segment_info[i].scnhdr.s_name));
3336  segment_info[i].scnhdr.s_flags = STYP_REG;
3337  segment_info[i].name = xstrdup (name);
3338
3339  return (segT) i;
3340}
3341
3342/*
3343 * implement the .section pseudo op:
3344 *	.section name {, "flags"}
3345 *                ^         ^
3346 *                |         +--- optional flags: 'b' for bss
3347 *                |                              'i' for info
3348 *                +-- section name               'l' for lib
3349 *                                               'n' for noload
3350 *                                               'o' for over
3351 *                                               'w' for data
3352 *						 'd' (apparently m88k for data)
3353 *                                               'x' for text
3354 *						 'r' for read-only data
3355 * But if the argument is not a quoted string, treat it as a
3356 * subsegment number.
3357 */
3358
3359void
3360obj_coff_section (ignore)
3361     int ignore;
3362{
3363  /* Strip out the section name */
3364  char *section_name, *name;
3365  char c;
3366  unsigned int exp;
3367  long flags;
3368
3369  if (flag_mri)
3370    {
3371      char type;
3372
3373      s_mri_sect (&type);
3374      flags = 0;
3375      if (type == 'C')
3376	flags = STYP_TEXT;
3377      else if (type == 'D')
3378	flags = STYP_DATA;
3379      segment_info[now_seg].scnhdr.s_flags |= flags;
3380
3381      return;
3382    }
3383
3384  section_name = input_line_pointer;
3385  c = get_symbol_end ();
3386
3387  name = xmalloc (input_line_pointer - section_name + 1);
3388  strcpy (name, section_name);
3389
3390  *input_line_pointer = c;
3391
3392  exp = 0;
3393  flags = 0;
3394
3395  SKIP_WHITESPACE ();
3396  if (*input_line_pointer == ',')
3397    {
3398      ++input_line_pointer;
3399      SKIP_WHITESPACE ();
3400
3401      if (*input_line_pointer != '"')
3402	exp = get_absolute_expression ();
3403      else
3404	{
3405	  ++input_line_pointer;
3406	  while (*input_line_pointer != '"'
3407		 && ! is_end_of_line[(unsigned char) *input_line_pointer])
3408	    {
3409	      switch (*input_line_pointer)
3410		{
3411		case 'b': flags |= STYP_BSS;    break;
3412		case 'i': flags |= STYP_INFO;   break;
3413		case 'l': flags |= STYP_LIB;    break;
3414		case 'n': flags |= STYP_NOLOAD; break;
3415		case 'o': flags |= STYP_OVER;   break;
3416		case 'd':
3417		case 'w': flags |= STYP_DATA;   break;
3418		case 'x': flags |= STYP_TEXT;   break;
3419		case 'r': flags |= STYP_LIT;	break;
3420		default:
3421		  as_warn("unknown section attribute '%c'",
3422			  *input_line_pointer);
3423		  break;
3424		}
3425	      ++input_line_pointer;
3426	    }
3427	  if (*input_line_pointer == '"')
3428	    ++input_line_pointer;
3429	}
3430    }
3431
3432  subseg_new (name, (subsegT) exp);
3433
3434  segment_info[now_seg].scnhdr.s_flags |= flags;
3435
3436  demand_empty_rest_of_line ();
3437}
3438
3439
3440static void
3441obj_coff_text (ignore)
3442     int ignore;
3443{
3444  subseg_new (".text", get_absolute_expression ());
3445}
3446
3447
3448static void
3449obj_coff_data (ignore)
3450     int ignore;
3451{
3452  if (flag_readonly_data_in_text)
3453    subseg_new (".text", get_absolute_expression () + 1000);
3454  else
3455    subseg_new (".data", get_absolute_expression ());
3456}
3457
3458static void
3459obj_coff_bss (ignore)
3460     int ignore;
3461{
3462  if (*input_line_pointer == '\n')	/* .bss 		*/
3463    subseg_new(".bss", get_absolute_expression());
3464  else					/* .bss id,expr		*/
3465    obj_coff_lcomm(0);
3466}
3467
3468static void
3469obj_coff_ident (ignore)
3470     int ignore;
3471{
3472  segT current_seg = now_seg;		/* save current seg	*/
3473  subsegT current_subseg = now_subseg;
3474  subseg_new (".comment", 0);		/* .comment seg		*/
3475  stringer (1);				/* read string		*/
3476  subseg_set (current_seg, current_subseg);	/* restore current seg	*/
3477}
3478
3479void
3480c_symbol_merge (debug, normal)
3481     symbolS *debug;
3482     symbolS *normal;
3483{
3484  S_SET_DATA_TYPE (normal, S_GET_DATA_TYPE (debug));
3485  S_SET_STORAGE_CLASS (normal, S_GET_STORAGE_CLASS (debug));
3486
3487  if (S_GET_NUMBER_AUXILIARY (debug) > S_GET_NUMBER_AUXILIARY (normal))
3488    {
3489      S_SET_NUMBER_AUXILIARY (normal, S_GET_NUMBER_AUXILIARY (debug));
3490    }				/* take the most we have */
3491
3492  if (S_GET_NUMBER_AUXILIARY (debug) > 0)
3493    {
3494      memcpy ((char *) &normal->sy_symbol.ost_auxent[0],
3495	      (char *) &debug->sy_symbol.ost_auxent[0],
3496	      (unsigned int) (S_GET_NUMBER_AUXILIARY (debug) * AUXESZ));
3497    }				/* Move all the auxiliary information */
3498
3499  /* Move the debug flags. */
3500  SF_SET_DEBUG_FIELD (normal, SF_GET_DEBUG_FIELD (debug));
3501}				/* c_symbol_merge() */
3502
3503static int
3504c_line_new (symbol, paddr, line_number, frag)
3505     symbolS * symbol;
3506     long paddr;
3507     int line_number;
3508     fragS * frag;
3509{
3510  struct lineno_list *new_line =
3511  (struct lineno_list *) xmalloc (sizeof (struct lineno_list));
3512
3513  segment_info_type *s = segment_info + now_seg;
3514  new_line->line.l_lnno = line_number;
3515
3516  if (line_number == 0)
3517    {
3518      last_line_symbol = symbol;
3519      new_line->line.l_addr.l_symndx = (long) symbol;
3520    }
3521  else
3522    {
3523      new_line->line.l_addr.l_paddr = paddr;
3524    }
3525
3526  new_line->frag = (char *) frag;
3527  new_line->next = (struct lineno_list *) NULL;
3528
3529
3530  if (s->lineno_list_head == (struct lineno_list *) NULL)
3531    {
3532      s->lineno_list_head = new_line;
3533    }
3534  else
3535    {
3536      s->lineno_list_tail->next = new_line;
3537    }
3538  s->lineno_list_tail = new_line;
3539  return LINESZ * s->scnhdr.s_nlnno++;
3540}
3541
3542void
3543c_dot_file_symbol (filename)
3544     char *filename;
3545{
3546  symbolS *symbolP;
3547
3548  symbolP = symbol_new (".file",
3549			SEG_DEBUG,
3550			0,
3551			&zero_address_frag);
3552
3553  S_SET_STORAGE_CLASS (symbolP, C_FILE);
3554  S_SET_NUMBER_AUXILIARY (symbolP, 1);
3555
3556  if (strlen (filename) > FILNMLEN)
3557    {
3558      /* Filename is too long to fit into an auxent,
3559	 we stick it into the string table instead.  We keep
3560	 a linked list of the filenames we find so we can emit
3561	 them later.*/
3562      struct filename_list *f = ((struct filename_list *)
3563				 xmalloc (sizeof (struct filename_list)));
3564
3565      f->filename = filename;
3566      f->next = 0;
3567
3568      SA_SET_FILE_FNAME_ZEROS (symbolP, 0);
3569      SA_SET_FILE_FNAME_OFFSET (symbolP, 1);
3570
3571      if (filename_list_tail)
3572	filename_list_tail->next = f;
3573      else
3574	filename_list_head = f;
3575      filename_list_tail = f;
3576    }
3577  else
3578    {
3579      SA_SET_FILE_FNAME (symbolP, filename);
3580    }
3581#ifndef NO_LISTING
3582  {
3583    extern int listing;
3584    if (listing)
3585      {
3586	listing_source_file (filename);
3587      }
3588
3589  }
3590
3591#endif
3592  SF_SET_DEBUG (symbolP);
3593  S_SET_VALUE (symbolP, (valueT) previous_file_symbol);
3594
3595  previous_file_symbol = symbolP;
3596
3597  /* Make sure that the symbol is first on the symbol chain */
3598  if (symbol_rootP != symbolP)
3599    {
3600      symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
3601      symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP);
3602    }
3603}				/* c_dot_file_symbol() */
3604
3605/*
3606 * Build a 'section static' symbol.
3607 */
3608
3609symbolS *
3610c_section_symbol (name, idx)
3611     char *name;
3612     int idx;
3613{
3614  symbolS *symbolP;
3615
3616  symbolP = symbol_new (name, idx,
3617			0,
3618			&zero_address_frag);
3619
3620  S_SET_STORAGE_CLASS (symbolP, C_STAT);
3621  S_SET_NUMBER_AUXILIARY (symbolP, 1);
3622
3623  SF_SET_STATICS (symbolP);
3624
3625#ifdef TE_DELTA
3626  /* manfred@s-direktnet.de: section symbols *must* have the LOCAL bit cleared,
3627     which is set by the new definition of LOCAL_LABEL in tc-m68k.h.  */
3628  SF_CLEAR_LOCAL (symbolP);
3629#endif
3630#ifdef TE_PE
3631  /* If the .linkonce pseudo-op was used for this section, we must
3632     store the information in the auxiliary entry for the section
3633     symbol.  */
3634  if (segment_info[idx].linkonce != LINKONCE_UNSET)
3635    {
3636      int type;
3637
3638      switch (segment_info[idx].linkonce)
3639	{
3640	default:
3641	  abort ();
3642	case LINKONCE_DISCARD:
3643	  type = IMAGE_COMDAT_SELECT_ANY;
3644	  break;
3645	case LINKONCE_ONE_ONLY:
3646	  type = IMAGE_COMDAT_SELECT_NODUPLICATES;
3647	  break;
3648	case LINKONCE_SAME_SIZE:
3649	  type = IMAGE_COMDAT_SELECT_SAME_SIZE;
3650	  break;
3651	case LINKONCE_SAME_CONTENTS:
3652	  type = IMAGE_COMDAT_SELECT_EXACT_MATCH;
3653	  break;
3654	}
3655
3656      SYM_AUXENT (symbolP)->x_scn.x_comdat = type;
3657    }
3658#endif /* TE_PE */
3659
3660  return symbolP;
3661}				/* c_section_symbol() */
3662
3663static void
3664w_symbols (abfd, where, symbol_rootP)
3665     bfd * abfd;
3666     char *where;
3667     symbolS * symbol_rootP;
3668{
3669  symbolS *symbolP;
3670  unsigned int i;
3671
3672  /* First fill in those values we have only just worked out */
3673  for (i = SEG_E0; i < SEG_LAST; i++)
3674    {
3675      symbolP = segment_info[i].dot;
3676      if (symbolP)
3677	{
3678	  SA_SET_SCN_SCNLEN (symbolP, segment_info[i].scnhdr.s_size);
3679	  SA_SET_SCN_NRELOC (symbolP, segment_info[i].scnhdr.s_nreloc);
3680	  SA_SET_SCN_NLINNO (symbolP, segment_info[i].scnhdr.s_nlnno);
3681	}
3682    }
3683
3684  /*
3685     * Emit all symbols left in the symbol chain.
3686     */
3687  for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
3688    {
3689      /* Used to save the offset of the name. It is used to point
3690	       to the string in memory but must be a file offset. */
3691      register char *temp;
3692
3693      /* We can't fix the lnnoptr field in yank_symbols with the other
3694         adjustments, because we have to wait until we know where they
3695         go in the file.  */
3696      if (SF_GET_ADJ_LNNOPTR (symbolP))
3697	{
3698	  SA_GET_SYM_LNNOPTR (symbolP) +=
3699	    segment_info[S_GET_SEGMENT (symbolP)].scnhdr.s_lnnoptr;
3700	}
3701
3702      tc_coff_symbol_emit_hook (symbolP);
3703
3704      temp = S_GET_NAME (symbolP);
3705      if (SF_GET_STRING (symbolP))
3706	{
3707	  S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
3708	  S_SET_ZEROES (symbolP, 0);
3709	}
3710      else
3711	{
3712	  memset (symbolP->sy_symbol.ost_entry.n_name, 0, SYMNMLEN);
3713	  strncpy (symbolP->sy_symbol.ost_entry.n_name, temp, SYMNMLEN);
3714	}
3715      where = symbol_to_chars (abfd, where, symbolP);
3716      S_SET_NAME (symbolP, temp);
3717    }
3718
3719}				/* w_symbols() */
3720
3721static void
3722obj_coff_lcomm (ignore)
3723     int ignore;
3724{
3725  s_lcomm(0);
3726  return;
3727#if 0
3728  char *name;
3729  char c;
3730  int temp;
3731  char *p;
3732
3733  symbolS *symbolP;
3734
3735  name = input_line_pointer;
3736
3737  c = get_symbol_end ();
3738  p = input_line_pointer;
3739  *p = c;
3740  SKIP_WHITESPACE ();
3741  if (*input_line_pointer != ',')
3742    {
3743      as_bad ("Expected comma after name");
3744      ignore_rest_of_line ();
3745      return;
3746    }
3747  if (*input_line_pointer == '\n')
3748    {
3749      as_bad ("Missing size expression");
3750      return;
3751    }
3752  input_line_pointer++;
3753  if ((temp = get_absolute_expression ()) < 0)
3754    {
3755      as_warn ("lcomm length (%d.) <0! Ignored.", temp);
3756      ignore_rest_of_line ();
3757      return;
3758    }
3759  *p = 0;
3760
3761  symbolP = symbol_find_or_make(name);
3762
3763  if (S_GET_SEGMENT(symbolP) == SEG_UNKNOWN &&
3764      S_GET_VALUE(symbolP) == 0)
3765    {
3766      if (! need_pass_2)
3767	{
3768	  char *p;
3769	  segT current_seg = now_seg; 	/* save current seg     */
3770	  subsegT current_subseg = now_subseg;
3771
3772	  subseg_set (SEG_E2, 1);
3773	  symbolP->sy_frag = frag_now;
3774	  p = frag_var(rs_org, 1, 1, (relax_substateT)0, symbolP,
3775		       (offsetT) temp, (char *) 0);
3776	  *p = 0;
3777	  subseg_set (current_seg, current_subseg); /* restore current seg */
3778	  S_SET_SEGMENT(symbolP, SEG_E2);
3779	  S_SET_STORAGE_CLASS(symbolP, C_STAT);
3780	}
3781    }
3782  else
3783    as_bad("Symbol %s already defined", name);
3784
3785  demand_empty_rest_of_line();
3786#endif
3787}
3788
3789static void
3790fixup_mdeps (frags, h, this_segment)
3791     fragS * frags;
3792     object_headers * h;
3793     segT this_segment;
3794{
3795  subseg_change (this_segment, 0);
3796  while (frags)
3797    {
3798      switch (frags->fr_type)
3799	{
3800	case rs_align:
3801	case rs_align_code:
3802	case rs_org:
3803#ifdef HANDLE_ALIGN
3804	  HANDLE_ALIGN (frags);
3805#endif
3806	  frags->fr_type = rs_fill;
3807	  frags->fr_offset =
3808	    ((frags->fr_next->fr_address - frags->fr_address - frags->fr_fix)
3809	     / frags->fr_var);
3810	  break;
3811	case rs_machine_dependent:
3812	  md_convert_frag (h, this_segment, frags);
3813	  frag_wane (frags);
3814	  break;
3815	default:
3816	  ;
3817	}
3818      frags = frags->fr_next;
3819    }
3820}
3821
3822#if 1
3823
3824#ifndef TC_FORCE_RELOCATION
3825#define TC_FORCE_RELOCATION(fix) 0
3826#endif
3827
3828static void
3829fixup_segment (segP, this_segment_type)
3830     segment_info_type * segP;
3831     segT this_segment_type;
3832{
3833  register fixS * fixP;
3834  register symbolS *add_symbolP;
3835  register symbolS *sub_symbolP;
3836  long add_number;
3837  register int size;
3838  register char *place;
3839  register long where;
3840  register char pcrel;
3841  register fragS *fragP;
3842  register segT add_symbol_segment = absolute_section;
3843
3844  for (fixP = segP->fix_root; fixP; fixP = fixP->fx_next)
3845    {
3846      fragP = fixP->fx_frag;
3847      know (fragP);
3848      where = fixP->fx_where;
3849      place = fragP->fr_literal + where;
3850      size = fixP->fx_size;
3851      add_symbolP = fixP->fx_addsy;
3852      sub_symbolP = fixP->fx_subsy;
3853      add_number = fixP->fx_offset;
3854      pcrel = fixP->fx_pcrel;
3855
3856      /* We want function-relative stabs to work on systems which
3857	 may use a relaxing linker; thus we must handle the sym1-sym2
3858	 fixups function-relative stabs generates.
3859
3860	 Of course, if you actually enable relaxing in the linker, the
3861	 line and block scoping information is going to be incorrect
3862	 in some cases.  The only way to really fix this is to support
3863	 a reloc involving the difference of two symbols.  */
3864      if (linkrelax
3865	  && (!sub_symbolP || pcrel))
3866	continue;
3867
3868#ifdef TC_I960
3869      if (fixP->fx_tcbit && SF_GET_CALLNAME (add_symbolP))
3870	{
3871	  /* Relocation should be done via the associated 'bal' entry
3872	     point symbol. */
3873
3874	  if (!SF_GET_BALNAME (tc_get_bal_of_call (add_symbolP)))
3875	    {
3876	      as_bad_where (fixP->fx_file, fixP->fx_line,
3877			    "No 'bal' entry point for leafproc %s",
3878			    S_GET_NAME (add_symbolP));
3879	      continue;
3880	    }
3881	  fixP->fx_addsy = add_symbolP = tc_get_bal_of_call (add_symbolP);
3882	}
3883#endif
3884
3885      /* Make sure the symbols have been resolved; this may not have
3886         happened if these are expression symbols.  */
3887      if (add_symbolP != NULL && ! add_symbolP->sy_resolved)
3888	resolve_symbol_value (add_symbolP);
3889
3890      if (add_symbolP != NULL)
3891	{
3892	  /* If this fixup is against a symbol which has been equated
3893	     to another symbol, convert it to the other symbol.  */
3894	  if (add_symbolP->sy_value.X_op == O_symbol
3895	      && (! S_IS_DEFINED (add_symbolP)
3896		  || S_IS_COMMON (add_symbolP)))
3897	    {
3898	      while (add_symbolP->sy_value.X_op == O_symbol
3899		     && (! S_IS_DEFINED (add_symbolP)
3900			 || S_IS_COMMON (add_symbolP)))
3901		{
3902		  symbolS *n;
3903
3904		  /* We must avoid looping, as that can occur with a
3905		     badly written program.  */
3906		  n = add_symbolP->sy_value.X_add_symbol;
3907		  if (n == add_symbolP)
3908		    break;
3909		  add_number += add_symbolP->sy_value.X_add_number;
3910		  add_symbolP = n;
3911		}
3912	      fixP->fx_addsy = add_symbolP;
3913	      fixP->fx_offset = add_number;
3914	    }
3915	}
3916
3917      if (sub_symbolP != NULL && ! sub_symbolP->sy_resolved)
3918	resolve_symbol_value (sub_symbolP);
3919
3920      if (add_symbolP != NULL
3921	  && add_symbolP->sy_mri_common)
3922	{
3923	  know (add_symbolP->sy_value.X_op == O_symbol);
3924	  add_number += S_GET_VALUE (add_symbolP);
3925	  fixP->fx_offset = add_number;
3926	  add_symbolP = fixP->fx_addsy = add_symbolP->sy_value.X_add_symbol;
3927	}
3928
3929      if (add_symbolP)
3930	{
3931	  add_symbol_segment = S_GET_SEGMENT (add_symbolP);
3932	}			/* if there is an addend */
3933
3934      if (sub_symbolP)
3935	{
3936	  if (add_symbolP == NULL || add_symbol_segment == absolute_section)
3937	    {
3938	      if (add_symbolP != NULL)
3939		{
3940		  add_number += S_GET_VALUE (add_symbolP);
3941		  add_symbolP = NULL;
3942		  fixP->fx_addsy = NULL;
3943		}
3944
3945	      /* It's just -sym.  */
3946	      if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
3947		{
3948		  add_number -= S_GET_VALUE (sub_symbolP);
3949		  fixP->fx_subsy = 0;
3950		  fixP->fx_done = 1;
3951		}
3952	      else
3953		{
3954#ifndef TC_M68K
3955		  as_bad_where (fixP->fx_file, fixP->fx_line,
3956				"Negative of non-absolute symbol %s",
3957				S_GET_NAME (sub_symbolP));
3958#endif
3959		  add_number -= S_GET_VALUE (sub_symbolP);
3960		}		/* not absolute */
3961
3962	      /* if sub_symbol is in the same segment that add_symbol
3963		 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
3964	    }
3965	  else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
3966		   && SEG_NORMAL (add_symbol_segment))
3967	    {
3968	      /* Difference of 2 symbols from same segment.  Can't
3969		 make difference of 2 undefineds: 'value' means
3970		 something different for N_UNDF. */
3971#ifdef TC_I960
3972	      /* Makes no sense to use the difference of 2 arbitrary symbols
3973	         as the target of a call instruction.  */
3974	      if (fixP->fx_tcbit)
3975		{
3976		  as_bad_where (fixP->fx_file, fixP->fx_line,
3977				"callj to difference of 2 symbols");
3978		}
3979#endif /* TC_I960 */
3980	      add_number += S_GET_VALUE (add_symbolP) -
3981		S_GET_VALUE (sub_symbolP);
3982	      add_symbolP = NULL;
3983
3984	      if (!TC_FORCE_RELOCATION (fixP))
3985		{
3986		  fixP->fx_addsy = NULL;
3987		  fixP->fx_subsy = NULL;
3988		  fixP->fx_done = 1;
3989#ifdef TC_M68K /* is this right? */
3990		  pcrel = 0;
3991		  fixP->fx_pcrel = 0;
3992#endif
3993		}
3994	    }
3995	  else
3996	    {
3997	      /* Different segments in subtraction. */
3998	      know (!(S_IS_EXTERNAL (sub_symbolP) && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
3999
4000	      if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
4001		{
4002		  add_number -= S_GET_VALUE (sub_symbolP);
4003		}
4004#ifdef DIFF_EXPR_OK
4005	      else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
4006#if 0 /* Okay for 68k, at least... */
4007		       && !pcrel
4008#endif
4009		       )
4010		{
4011		  /* Make it pc-relative.  */
4012		  add_number += (md_pcrel_from (fixP)
4013				 - S_GET_VALUE (sub_symbolP));
4014		  pcrel = 1;
4015		  fixP->fx_pcrel = 1;
4016		  sub_symbolP = 0;
4017		  fixP->fx_subsy = 0;
4018		}
4019#endif
4020	      else
4021		{
4022		  as_bad_where (fixP->fx_file, fixP->fx_line,
4023				"Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %ld.",
4024				segment_name (S_GET_SEGMENT (sub_symbolP)),
4025				S_GET_NAME (sub_symbolP),
4026				(long) (fragP->fr_address + where));
4027		}		/* if absolute */
4028	    }
4029	}			/* if sub_symbolP */
4030
4031      if (add_symbolP)
4032	{
4033	  if (add_symbol_segment == this_segment_type && pcrel)
4034	    {
4035	      /*
4036	       * This fixup was made when the symbol's segment was
4037	       * SEG_UNKNOWN, but it is now in the local segment.
4038	       * So we know how to do the address without relocation.
4039	       */
4040#ifdef TC_I960
4041	      /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
4042	       * in which cases it modifies *fixP as appropriate.  In the case
4043	       * of a 'calls', no further work is required, and *fixP has been
4044	       * set up to make the rest of the code below a no-op.
4045	       */
4046	      reloc_callj (fixP);
4047#endif /* TC_I960 */
4048
4049	      add_number += S_GET_VALUE (add_symbolP);
4050	      add_number -= md_pcrel_from (fixP);
4051#if defined (TC_I386) || defined (TE_LYNX)
4052	      /* On the 386 we must adjust by the segment vaddr as
4053		 well.  Ian Taylor.  */
4054	      add_number -= segP->scnhdr.s_vaddr;
4055#endif
4056	      pcrel = 0;	/* Lie. Don't want further pcrel processing. */
4057	      if (!TC_FORCE_RELOCATION (fixP))
4058		{
4059		  fixP->fx_addsy = NULL;
4060		  fixP->fx_done = 1;
4061		}
4062	    }
4063	  else
4064	    {
4065	      switch (add_symbol_segment)
4066		{
4067		case absolute_section:
4068#ifdef TC_I960
4069		  reloc_callj (fixP);	/* See comment about reloc_callj() above*/
4070#endif /* TC_I960 */
4071		  add_number += S_GET_VALUE (add_symbolP);
4072		  add_symbolP = NULL;
4073
4074		  if (!TC_FORCE_RELOCATION (fixP))
4075		    {
4076		      fixP->fx_addsy = NULL;
4077		      fixP->fx_done = 1;
4078		    }
4079		  break;
4080		default:
4081
4082
4083#if defined(TC_A29K) || (defined(TE_PE) && defined(TC_I386)) || defined(TC_M88K)
4084		  /* This really should be handled in the linker, but
4085		     backward compatibility forbids.  */
4086		  add_number += S_GET_VALUE (add_symbolP);
4087#else
4088		  add_number += S_GET_VALUE (add_symbolP) +
4089		    segment_info[S_GET_SEGMENT (add_symbolP)].scnhdr.s_paddr;
4090#endif
4091		  break;
4092
4093		case SEG_UNKNOWN:
4094#ifdef TC_I960
4095		  if ((int) fixP->fx_bit_fixP == 13)
4096		    {
4097		      /* This is a COBR instruction.  They have only a
4098		       * 13-bit displacement and are only to be used
4099		       * for local branches: flag as error, don't generate
4100		       * relocation.
4101		       */
4102		      as_bad_where (fixP->fx_file, fixP->fx_line,
4103				    "can't use COBR format with external label");
4104		      fixP->fx_addsy = NULL;
4105		      fixP->fx_done = 1;
4106		      continue;
4107		    }		/* COBR */
4108#endif /* TC_I960 */
4109#if ((defined (TC_I386) || defined (TE_LYNX) || defined (TE_AUX)) && !defined(TE_PE)) || defined (COFF_COMMON_ADDEND)
4110		  /* 386 COFF uses a peculiar format in which the
4111		     value of a common symbol is stored in the .text
4112		     segment (I've checked this on SVR3.2 and SCO
4113		     3.2.2) Ian Taylor <ian@cygnus.com>.  */
4114		  /* This is also true for 68k COFF on sysv machines
4115		     (Checked on Motorola sysv68 R3V6 and R3V7.1, and also on
4116		     UNIX System V/M68000, Release 1.0 from ATT/Bell Labs)
4117		     Philippe De Muyter <phdm@info.ucl.ac.be>. */
4118		  if (S_IS_COMMON (add_symbolP))
4119		    add_number += S_GET_VALUE (add_symbolP);
4120#endif
4121		  break;
4122
4123
4124		}		/* switch on symbol seg */
4125	    }			/* if not in local seg */
4126	}			/* if there was a + symbol */
4127
4128      if (pcrel)
4129	{
4130#if !defined(TC_M88K) && !(defined(TE_PE) && defined(TC_I386)) && !defined(TC_A29K)
4131	  /* This adjustment is not correct on the m88k, for which the
4132	     linker does all the computation.  */
4133	  add_number -= md_pcrel_from (fixP);
4134#endif
4135	  if (add_symbolP == 0)
4136	    {
4137	      fixP->fx_addsy = &abs_symbol;
4138	    }			/* if there's an add_symbol */
4139#if defined (TC_I386) || defined (TE_LYNX) || defined (TC_I960) || defined (TC_M68K)
4140	  /* On the 386 we must adjust by the segment vaddr as well.
4141	     Ian Taylor.
4142
4143	     I changed the i960 to work this way as well.  This is
4144	     compatible with the current GNU linker behaviour.  I do
4145	     not know what other i960 COFF assemblers do.  This is not
4146	     a common case: normally, only assembler code will contain
4147	     a PC relative reloc, and only branches which do not
4148	     originate in the .text section will have a non-zero
4149	     address.
4150
4151	     I changed the m68k to work this way as well.  This will
4152	     break existing PC relative relocs from sections which do
4153	     not start at address 0, but it will make ld -r work.
4154	     Ian Taylor, 4 Oct 96.  */
4155
4156	  add_number -= segP->scnhdr.s_vaddr;
4157#endif
4158	}			/* if pcrel */
4159
4160      if (!fixP->fx_bit_fixP && ! fixP->fx_no_overflow)
4161	{
4162#ifndef TC_M88K
4163	  /* The m88k uses the offset field of the reloc to get around
4164	     this problem.  */
4165	  if ((size == 1
4166	       && ((add_number & ~0xFF)
4167		   || (fixP->fx_signed && (add_number & 0x80)))
4168	       && ((add_number & ~0xFF) != (-1 & ~0xFF)
4169		   || (add_number & 0x80) == 0))
4170	      || (size == 2
4171		  && ((add_number & ~0xFFFF)
4172		      || (fixP->fx_signed && (add_number & 0x8000)))
4173		  && ((add_number & ~0xFFFF) != (-1 & ~0xFFFF)
4174		      || (add_number & 0x8000) == 0)))
4175	    {
4176	      as_bad_where (fixP->fx_file, fixP->fx_line,
4177			    "Value of %ld too large for field of %d bytes at 0x%lx",
4178			    (long) add_number, size,
4179			    (unsigned long) (fragP->fr_address + where));
4180	    }
4181#endif
4182#ifdef WARN_SIGNED_OVERFLOW_WORD
4183	  /* Warn if a .word value is too large when treated as a
4184	     signed number.  We already know it is not too negative.
4185	     This is to catch over-large switches generated by gcc on
4186	     the 68k.  */
4187	  if (!flag_signed_overflow_ok
4188	      && size == 2
4189	      && add_number > 0x7fff)
4190	    as_bad_where (fixP->fx_file, fixP->fx_line,
4191			  "Signed .word overflow; switch may be too large; %ld at 0x%lx",
4192			  (long) add_number,
4193			  (unsigned long) (fragP->fr_address + where));
4194#endif
4195	}			/* not a bit fix */
4196      /* Once this fix has been applied, we don't have to output
4197	 anything nothing more need be done.  */
4198#ifdef MD_APPLY_FIX3
4199      md_apply_fix3 (fixP, &add_number, this_segment_type);
4200#else
4201      md_apply_fix (fixP, add_number);
4202#endif
4203    }				/* For each fixS in this segment. */
4204}				/* fixup_segment() */
4205
4206#endif
4207
4208/* The first entry in a .stab section is special.  */
4209
4210void
4211obj_coff_init_stab_section (seg)
4212     segT seg;
4213{
4214  char *file;
4215  char *p;
4216  char *stabstr_name;
4217  unsigned int stroff;
4218
4219  /* Make space for this first symbol. */
4220  p = frag_more (12);
4221  /* Zero it out. */
4222  memset (p, 0, 12);
4223  as_where (&file, (unsigned int *) NULL);
4224  stabstr_name = (char *) alloca (strlen (segment_info[seg].name) + 4);
4225  strcpy (stabstr_name, segment_info[seg].name);
4226  strcat (stabstr_name, "str");
4227  stroff = get_stab_string_offset (file, stabstr_name);
4228  know (stroff == 1);
4229  md_number_to_chars (p, stroff, 4);
4230}
4231
4232/* Fill in the counts in the first entry in a .stab section.  */
4233
4234static void
4235adjust_stab_section(abfd, seg)
4236     bfd *abfd;
4237     segT seg;
4238{
4239  segT stabstrseg = SEG_UNKNOWN;
4240  const char *secname, *name2;
4241  char *name;
4242  char *p = NULL;
4243  int i, strsz = 0, nsyms;
4244  fragS *frag = segment_info[seg].frchainP->frch_root;
4245
4246  /* Look for the associated string table section. */
4247
4248  secname = segment_info[seg].name;
4249  name = (char *) alloca (strlen (secname) + 4);
4250  strcpy (name, secname);
4251  strcat (name, "str");
4252
4253  for (i = SEG_E0; i < SEG_UNKNOWN; i++)
4254    {
4255      name2 = segment_info[i].name;
4256      if (name2 != NULL && strncmp(name2, name, 8) == 0)
4257	{
4258	  stabstrseg = i;
4259	  break;
4260	}
4261    }
4262
4263  /* If we found the section, get its size. */
4264  if (stabstrseg != SEG_UNKNOWN)
4265    strsz = size_section (abfd, stabstrseg);
4266
4267  nsyms = size_section (abfd, seg) / 12 - 1;
4268
4269  /* Look for the first frag of sufficient size for the initial stab
4270     symbol, and collect a pointer to it. */
4271  while (frag && frag->fr_fix < 12)
4272    frag = frag->fr_next;
4273  assert (frag != 0);
4274  p = frag->fr_literal;
4275  assert (p != 0);
4276
4277  /* Write in the number of stab symbols and the size of the string
4278     table. */
4279  bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6);
4280  bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8);
4281}
4282
4283#endif /* not BFD_ASSEMBLER */
4284
4285const pseudo_typeS obj_pseudo_table[] =
4286{
4287  {"def", obj_coff_def, 0},
4288  {"dim", obj_coff_dim, 0},
4289  {"endef", obj_coff_endef, 0},
4290  {"line", obj_coff_line, 0},
4291  {"ln", obj_coff_ln, 0},
4292  {"appline", obj_coff_ln, 1},
4293  {"scl", obj_coff_scl, 0},
4294  {"size", obj_coff_size, 0},
4295  {"tag", obj_coff_tag, 0},
4296  {"type", obj_coff_type, 0},
4297  {"val", obj_coff_val, 0},
4298  {"section", obj_coff_section, 0},
4299  {"sect", obj_coff_section, 0},
4300  /* FIXME: We ignore the MRI short attribute.  */
4301  {"section.s", obj_coff_section, 0},
4302  {"sect.s", obj_coff_section, 0},
4303#ifndef BFD_ASSEMBLER
4304  {"use", obj_coff_section, 0},
4305  {"text", obj_coff_text, 0},
4306  {"data", obj_coff_data, 0},
4307  {"bss", obj_coff_bss, 0},
4308  {"lcomm", obj_coff_lcomm, 0},
4309  {"ident", obj_coff_ident, 0},
4310#else
4311  {"optim", s_ignore, 0},	/* For sun386i cc (?) */
4312  {"ident", s_ignore, 0},	/* we don't yet handle this. */
4313#endif
4314  {"version", s_ignore, 0},
4315  {"ABORT", s_abort, 0},
4316#ifdef TC_M88K
4317  /* The m88k uses sdef instead of def.  */
4318  {"sdef", obj_coff_def, 0},
4319#endif
4320  {NULL}			/* end sentinel */
4321};				/* obj_pseudo_table */
4322
4323#ifdef BFD_ASSEMBLER
4324
4325/* Support for a COFF emulation.  */
4326
4327static void
4328coff_pop_insert ()
4329{
4330  pop_insert (obj_pseudo_table);
4331}
4332
4333static int
4334coff_sec_sym_ok_for_reloc (sec)
4335     asection *sec;
4336{
4337  return 0;
4338}
4339
4340static void
4341no_func ()
4342{
4343  abort ();
4344}
4345
4346const struct format_ops coff_format_ops =
4347{
4348  bfd_target_coff_flavour,
4349  0,
4350  1,
4351  coff_frob_symbol,
4352  coff_frob_file,
4353  no_func,
4354  0, 0,
4355  0, 0,
4356  0,
4357#if 0
4358  obj_generate_asm_lineno,
4359#else
4360  no_func,
4361#endif
4362#if 0
4363  obj_stab,
4364#else
4365  no_func,
4366#endif
4367  coff_sec_sym_ok_for_reloc,
4368  coff_pop_insert,
4369#if 0
4370  obj_set_ext,
4371#else
4372  no_func,
4373#endif
4374  coff_obj_read_begin_hook,
4375  coff_obj_symbol_new_hook,
4376};
4377
4378#endif
4379