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