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