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