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