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