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