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