OSDN Git Service

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