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