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