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