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