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