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