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