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