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