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