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