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