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