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