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