Thu Mar 20 12:48:45 1997 Philippe De Muyter <phdm@info.ucl.ac.be>
[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 /* calculate the size of the frag chain and fill in the section header
1508 to contain all of it, also fill in the addr of the sections */
1509 static unsigned int
1510 size_section (abfd, idx)
1511 bfd * abfd;
1512 unsigned int idx;
1513 {
1514
1515 unsigned int size = 0;
1516 fragS *frag = segment_info[idx].frchainP->frch_root;
1517 while (frag)
1518 {
1519 size = frag->fr_address;
1520 if (frag->fr_address != size)
1521 {
1522 fprintf (stderr, "Out of step\n");
1523 size = frag->fr_address;
1524 }
1525
1526 switch (frag->fr_type)
1527 {
1528 #ifdef TC_COFF_SIZEMACHDEP
1529 case rs_machine_dependent:
1530 size += TC_COFF_SIZEMACHDEP (frag);
1531 break;
1532 #endif
1533 case rs_space:
1534 assert (frag->fr_symbol == 0);
1535 case rs_fill:
1536 case rs_org:
1537 size += frag->fr_fix;
1538 size += frag->fr_offset * frag->fr_var;
1539 break;
1540 case rs_align:
1541 case rs_align_code:
1542 {
1543 addressT off;
1544
1545 size += frag->fr_fix;
1546 off = relax_align (size, frag->fr_offset);
1547 if (frag->fr_subtype != 0 && off > frag->fr_subtype)
1548 off = 0;
1549 size += off;
1550 }
1551 break;
1552 default:
1553 BAD_CASE (frag->fr_type);
1554 break;
1555 }
1556 frag = frag->fr_next;
1557 }
1558 segment_info[idx].scnhdr.s_size = size;
1559 return size;
1560 }
1561
1562
1563 static unsigned int
1564 count_entries_in_chain (idx)
1565 unsigned int idx;
1566 {
1567 unsigned int nrelocs;
1568 fixS *fixup_ptr;
1569
1570 /* Count the relocations */
1571 fixup_ptr = segment_info[idx].fix_root;
1572 nrelocs = 0;
1573 while (fixup_ptr != (fixS *) NULL)
1574 {
1575 if (fixup_ptr->fx_done == 0 && TC_COUNT_RELOC (fixup_ptr))
1576 {
1577 #ifdef TC_A29K
1578 if (fixup_ptr->fx_r_type == RELOC_CONSTH)
1579 nrelocs += 2;
1580 else
1581 nrelocs++;
1582 #else
1583 nrelocs++;
1584 #endif
1585 }
1586
1587 fixup_ptr = fixup_ptr->fx_next;
1588 }
1589 return nrelocs;
1590 }
1591
1592 #ifdef TE_AUX
1593
1594 static int compare_external_relocs PARAMS ((const PTR, const PTR));
1595
1596 /* AUX's ld expects relocations to be sorted */
1597 static int
1598 compare_external_relocs (x, y)
1599 const PTR x;
1600 const PTR y;
1601 {
1602 struct external_reloc *a = (struct external_reloc *) x;
1603 struct external_reloc *b = (struct external_reloc *) y;
1604 bfd_vma aadr = bfd_getb32 (a->r_vaddr);
1605 bfd_vma badr = bfd_getb32 (b->r_vaddr);
1606 return (aadr < badr ? -1 : badr < aadr ? 1 : 0);
1607 }
1608
1609 #endif
1610
1611 /* output all the relocations for a section */
1612 void
1613 do_relocs_for (abfd, h, file_cursor)
1614 bfd * abfd;
1615 object_headers * h;
1616 unsigned long *file_cursor;
1617 {
1618 unsigned int nrelocs;
1619 unsigned int idx;
1620 unsigned long reloc_start = *file_cursor;
1621
1622 for (idx = SEG_E0; idx < SEG_LAST; idx++)
1623 {
1624 if (segment_info[idx].scnhdr.s_name[0])
1625 {
1626 struct external_reloc *ext_ptr;
1627 struct external_reloc *external_reloc_vec;
1628 unsigned int external_reloc_size;
1629 unsigned int base = segment_info[idx].scnhdr.s_paddr;
1630 fixS *fix_ptr = segment_info[idx].fix_root;
1631 nrelocs = count_entries_in_chain (idx);
1632
1633 if (nrelocs)
1634 /* Bypass this stuff if no relocs. This also incidentally
1635 avoids a SCO bug, where free(malloc(0)) tends to crash. */
1636 {
1637 external_reloc_size = nrelocs * RELSZ;
1638 external_reloc_vec =
1639 (struct external_reloc *) malloc (external_reloc_size);
1640
1641 ext_ptr = external_reloc_vec;
1642
1643 /* Fill in the internal coff style reloc struct from the
1644 internal fix list. */
1645 while (fix_ptr)
1646 {
1647 struct internal_reloc intr;
1648
1649 /* Only output some of the relocations */
1650 if (fix_ptr->fx_done == 0 && TC_COUNT_RELOC (fix_ptr))
1651 {
1652 #ifdef TC_RELOC_MANGLE
1653 TC_RELOC_MANGLE (&segment_info[idx], fix_ptr, &intr,
1654 base);
1655
1656 #else
1657 symbolS *dot;
1658 symbolS *symbol_ptr = fix_ptr->fx_addsy;
1659
1660 intr.r_type = TC_COFF_FIX2RTYPE (fix_ptr);
1661 intr.r_vaddr =
1662 base + fix_ptr->fx_frag->fr_address + fix_ptr->fx_where;
1663
1664 #ifdef TC_KEEP_FX_OFFSET
1665 intr.r_offset = fix_ptr->fx_offset;
1666 #else
1667 intr.r_offset = 0;
1668 #endif
1669
1670 while (symbol_ptr->sy_value.X_op == O_symbol
1671 && (! S_IS_DEFINED (symbol_ptr)
1672 || S_IS_COMMON (symbol_ptr)))
1673 {
1674 symbolS *n;
1675
1676 /* We must avoid looping, as that can occur
1677 with a badly written program. */
1678 n = symbol_ptr->sy_value.X_add_symbol;
1679 if (n == symbol_ptr)
1680 break;
1681 symbol_ptr = n;
1682 }
1683
1684 /* Turn the segment of the symbol into an offset. */
1685 if (symbol_ptr)
1686 {
1687 resolve_symbol_value (symbol_ptr);
1688 if (! symbol_ptr->sy_resolved)
1689 {
1690 char *file;
1691 unsigned int line;
1692
1693 if (expr_symbol_where (symbol_ptr, &file, &line))
1694 as_bad_where (file, line,
1695 "unresolved relocation");
1696 else
1697 as_bad ("bad relocation: symbol `%s' not in symbol table",
1698 S_GET_NAME (symbol_ptr));
1699 }
1700 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1701 if (dot)
1702 {
1703 intr.r_symndx = dot->sy_number;
1704 }
1705 else
1706 {
1707 intr.r_symndx = symbol_ptr->sy_number;
1708 }
1709
1710 }
1711 else
1712 {
1713 intr.r_symndx = -1;
1714 }
1715 #endif
1716
1717 (void) bfd_coff_swap_reloc_out (abfd, &intr, ext_ptr);
1718 ext_ptr++;
1719
1720 #if defined(TC_A29K)
1721
1722 /* The 29k has a special kludge for the high 16 bit
1723 reloc. Two relocations are emited, R_IHIHALF,
1724 and R_IHCONST. The second one doesn't contain a
1725 symbol, but uses the value for offset. */
1726
1727 if (intr.r_type == R_IHIHALF)
1728 {
1729 /* now emit the second bit */
1730 intr.r_type = R_IHCONST;
1731 intr.r_symndx = fix_ptr->fx_addnumber;
1732 (void) bfd_coff_swap_reloc_out (abfd, &intr, ext_ptr);
1733 ext_ptr++;
1734 }
1735 #endif
1736 }
1737
1738 fix_ptr = fix_ptr->fx_next;
1739 }
1740
1741 #ifdef TE_AUX
1742 /* Sort the reloc table */
1743 qsort ((PTR) external_reloc_vec, nrelocs,
1744 sizeof (struct external_reloc), compare_external_relocs);
1745 #endif
1746
1747 /* Write out the reloc table */
1748 bfd_write ((PTR) external_reloc_vec, 1, external_reloc_size,
1749 abfd);
1750 free (external_reloc_vec);
1751
1752 /* Fill in section header info. */
1753 segment_info[idx].scnhdr.s_relptr = *file_cursor;
1754 *file_cursor += external_reloc_size;
1755 segment_info[idx].scnhdr.s_nreloc = nrelocs;
1756 }
1757 else
1758 {
1759 /* No relocs */
1760 segment_info[idx].scnhdr.s_relptr = 0;
1761 }
1762 }
1763 }
1764 /* Set relocation_size field in file headers */
1765 H_SET_RELOCATION_SIZE (h, *file_cursor - reloc_start, 0);
1766 }
1767
1768
1769 /* run through a frag chain and write out the data to go with it, fill
1770 in the scnhdrs with the info on the file postions
1771 */
1772 static void
1773 fill_section (abfd, h, file_cursor)
1774 bfd * abfd;
1775 object_headers *h;
1776 unsigned long *file_cursor;
1777 {
1778
1779 unsigned int i;
1780 unsigned int paddr = 0;
1781
1782 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1783 {
1784 unsigned int offset = 0;
1785 struct internal_scnhdr *s = &(segment_info[i].scnhdr);
1786
1787 PROGRESS (1);
1788
1789 if (s->s_name[0])
1790 {
1791 fragS *frag = segment_info[i].frchainP->frch_root;
1792 char *buffer;
1793
1794 if (s->s_size == 0)
1795 s->s_scnptr = 0;
1796 else
1797 {
1798 buffer = xmalloc (s->s_size);
1799 s->s_scnptr = *file_cursor;
1800 }
1801 know (s->s_paddr == paddr);
1802
1803 if (strcmp (s->s_name, ".text") == 0)
1804 s->s_flags |= STYP_TEXT;
1805 else if (strcmp (s->s_name, ".data") == 0)
1806 s->s_flags |= STYP_DATA;
1807 else if (strcmp (s->s_name, ".bss") == 0)
1808 {
1809 s->s_scnptr = 0;
1810 s->s_flags |= STYP_BSS;
1811
1812 /* @@ Should make the i386 and a29k coff targets define
1813 COFF_NOLOAD_PROBLEM, and have only one test here. */
1814 #ifndef TC_I386
1815 #ifndef TC_A29K
1816 #ifndef COFF_NOLOAD_PROBLEM
1817 /* Apparently the SVR3 linker (and exec syscall) and UDI
1818 mondfe progrem are confused by noload sections. */
1819 s->s_flags |= STYP_NOLOAD;
1820 #endif
1821 #endif
1822 #endif
1823 }
1824 else if (strcmp (s->s_name, ".lit") == 0)
1825 s->s_flags = STYP_LIT | STYP_TEXT;
1826 else if (strcmp (s->s_name, ".init") == 0)
1827 s->s_flags |= STYP_TEXT;
1828 else if (strcmp (s->s_name, ".fini") == 0)
1829 s->s_flags |= STYP_TEXT;
1830 else if (strncmp (s->s_name, ".comment", 8) == 0)
1831 s->s_flags |= STYP_INFO;
1832
1833 while (frag)
1834 {
1835 unsigned int fill_size;
1836 switch (frag->fr_type)
1837 {
1838 case rs_machine_dependent:
1839 if (frag->fr_fix)
1840 {
1841 memcpy (buffer + frag->fr_address,
1842 frag->fr_literal,
1843 (unsigned int) frag->fr_fix);
1844 offset += frag->fr_fix;
1845 }
1846
1847 break;
1848 case rs_space:
1849 assert (frag->fr_symbol == 0);
1850 case rs_fill:
1851 case rs_align:
1852 case rs_align_code:
1853 case rs_org:
1854 if (frag->fr_fix)
1855 {
1856 memcpy (buffer + frag->fr_address,
1857 frag->fr_literal,
1858 (unsigned int) frag->fr_fix);
1859 offset += frag->fr_fix;
1860 }
1861
1862 fill_size = frag->fr_var;
1863 if (fill_size && frag->fr_offset > 0)
1864 {
1865 unsigned int count;
1866 unsigned int off = frag->fr_fix;
1867 for (count = frag->fr_offset; count; count--)
1868 {
1869 if (fill_size + frag->fr_address + off <= s->s_size)
1870 {
1871 memcpy (buffer + frag->fr_address + off,
1872 frag->fr_literal + frag->fr_fix,
1873 fill_size);
1874 off += fill_size;
1875 offset += fill_size;
1876 }
1877 }
1878 }
1879 break;
1880 case rs_broken_word:
1881 break;
1882 default:
1883 abort ();
1884 }
1885 frag = frag->fr_next;
1886 }
1887
1888 if (s->s_size != 0)
1889 {
1890 if (s->s_scnptr != 0)
1891 {
1892 bfd_write (buffer, s->s_size, 1, abfd);
1893 *file_cursor += s->s_size;
1894 }
1895 free (buffer);
1896 }
1897 paddr += s->s_size;
1898 }
1899 }
1900 }
1901
1902 /* Coff file generation & utilities */
1903
1904 static void
1905 coff_header_append (abfd, h)
1906 bfd * abfd;
1907 object_headers * h;
1908 {
1909 unsigned int i;
1910 char buffer[1000];
1911 char buffero[1000];
1912
1913 bfd_seek (abfd, 0, 0);
1914
1915 #ifndef OBJ_COFF_OMIT_OPTIONAL_HEADER
1916 H_SET_MAGIC_NUMBER (h, COFF_MAGIC);
1917 H_SET_VERSION_STAMP (h, 0);
1918 H_SET_ENTRY_POINT (h, 0);
1919 H_SET_TEXT_START (h, segment_info[SEG_E0].frchainP->frch_root->fr_address);
1920 H_SET_DATA_START (h, segment_info[SEG_E1].frchainP->frch_root->fr_address);
1921 H_SET_SIZEOF_OPTIONAL_HEADER (h, bfd_coff_swap_aouthdr_out(abfd, &h->aouthdr,
1922 buffero));
1923 #else /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */
1924 H_SET_SIZEOF_OPTIONAL_HEADER (h, 0);
1925 #endif /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */
1926
1927 i = bfd_coff_swap_filehdr_out (abfd, &h->filehdr, buffer);
1928
1929 bfd_write (buffer, i, 1, abfd);
1930 bfd_write (buffero, H_GET_SIZEOF_OPTIONAL_HEADER (h), 1, abfd);
1931
1932 for (i = SEG_E0; i < SEG_LAST; i++)
1933 {
1934 #ifdef COFF_LONG_SECTION_NAMES
1935 unsigned long string_size = 4;
1936 #endif
1937
1938 if (segment_info[i].scnhdr.s_name[0])
1939 {
1940 unsigned int size;
1941
1942 #ifdef COFF_LONG_SECTION_NAMES
1943 /* Support long section names as found in PE. This code
1944 must coordinate with that in write_object_file and
1945 w_strings. */
1946 if (strlen (segment_info[i].name) > SCNNMLEN)
1947 {
1948 memset (segment_info[i].scnhdr.s_name, 0, SCNNMLEN);
1949 sprintf (segment_info[i].scnhdr.s_name, "/%lu", string_size);
1950 string_size += strlen (segment_info[i].name) + 1;
1951 }
1952 #endif
1953
1954 size = bfd_coff_swap_scnhdr_out (abfd,
1955 &(segment_info[i].scnhdr),
1956 buffer);
1957 if (size == 0)
1958 as_bad ("bfd_coff_swap_scnhdr_out failed");
1959 bfd_write (buffer, size, 1, abfd);
1960 }
1961 }
1962 }
1963
1964
1965 char *
1966 symbol_to_chars (abfd, where, symbolP)
1967 bfd * abfd;
1968 char *where;
1969 symbolS * symbolP;
1970 {
1971 unsigned int numaux = symbolP->sy_symbol.ost_entry.n_numaux;
1972 unsigned int i;
1973 valueT val;
1974
1975 /* Turn any symbols with register attributes into abs symbols */
1976 if (S_GET_SEGMENT (symbolP) == reg_section)
1977 {
1978 S_SET_SEGMENT (symbolP, absolute_section);
1979 }
1980 /* At the same time, relocate all symbols to their output value */
1981
1982 val = (segment_info[S_GET_SEGMENT (symbolP)].scnhdr.s_paddr
1983 + S_GET_VALUE (symbolP));
1984
1985 S_SET_VALUE (symbolP, val);
1986
1987 symbolP->sy_symbol.ost_entry.n_value = val;
1988
1989 where += bfd_coff_swap_sym_out (abfd, &symbolP->sy_symbol.ost_entry,
1990 where);
1991
1992 for (i = 0; i < numaux; i++)
1993 {
1994 where += bfd_coff_swap_aux_out (abfd,
1995 &symbolP->sy_symbol.ost_auxent[i],
1996 S_GET_DATA_TYPE (symbolP),
1997 S_GET_STORAGE_CLASS (symbolP),
1998 i, numaux, where);
1999 }
2000 return where;
2001
2002 }
2003
2004 void
2005 coff_obj_symbol_new_hook (symbolP)
2006 symbolS *symbolP;
2007 {
2008 char underscore = 0; /* Symbol has leading _ */
2009
2010 /* Effective symbol */
2011 /* Store the pointer in the offset. */
2012 S_SET_ZEROES (symbolP, 0L);
2013 S_SET_DATA_TYPE (symbolP, T_NULL);
2014 S_SET_STORAGE_CLASS (symbolP, 0);
2015 S_SET_NUMBER_AUXILIARY (symbolP, 0);
2016 /* Additional information */
2017 symbolP->sy_symbol.ost_flags = 0;
2018 /* Auxiliary entries */
2019 memset ((char *) &symbolP->sy_symbol.ost_auxent[0], 0, AUXESZ);
2020
2021 if (S_IS_STRING (symbolP))
2022 SF_SET_STRING (symbolP);
2023 if (!underscore && S_IS_LOCAL (symbolP))
2024 SF_SET_LOCAL (symbolP);
2025 }
2026
2027 /*
2028 * Handle .ln directives.
2029 */
2030
2031 static void
2032 obj_coff_ln (appline)
2033 int appline;
2034 {
2035 int l;
2036
2037 if (! appline && def_symbol_in_progress != NULL)
2038 {
2039 as_warn (".ln pseudo-op inside .def/.endef: ignored.");
2040 demand_empty_rest_of_line ();
2041 return;
2042 } /* wrong context */
2043
2044 l = get_absolute_expression ();
2045 c_line_new (0, frag_now_fix (), l, frag_now);
2046
2047 if (appline)
2048 new_logical_line ((char *) NULL, l - 1);
2049
2050 #ifndef NO_LISTING
2051 {
2052 extern int listing;
2053
2054 if (listing)
2055 {
2056 if (! appline)
2057 l += line_base - 1;
2058 listing_source_line ((unsigned int) l);
2059 }
2060
2061 }
2062 #endif
2063 demand_empty_rest_of_line ();
2064 }
2065
2066 /*
2067 * def()
2068 *
2069 * Handle .def directives.
2070 *
2071 * One might ask : why can't we symbol_new if the symbol does not
2072 * already exist and fill it with debug information. Because of
2073 * the C_EFCN special symbol. It would clobber the value of the
2074 * function symbol before we have a chance to notice that it is
2075 * a C_EFCN. And a second reason is that the code is more clear this
2076 * way. (at least I think it is :-).
2077 *
2078 */
2079
2080 #define SKIP_SEMI_COLON() while (*input_line_pointer++ != ';')
2081 #define SKIP_WHITESPACES() while (*input_line_pointer == ' ' || \
2082 *input_line_pointer == '\t') \
2083 input_line_pointer++;
2084
2085 static void
2086 obj_coff_def (what)
2087 int what;
2088 {
2089 char name_end; /* Char after the end of name */
2090 char *symbol_name; /* Name of the debug symbol */
2091 char *symbol_name_copy; /* Temporary copy of the name */
2092 unsigned int symbol_name_length;
2093
2094 if (def_symbol_in_progress != NULL)
2095 {
2096 as_warn (".def pseudo-op used inside of .def/.endef: ignored.");
2097 demand_empty_rest_of_line ();
2098 return;
2099 } /* if not inside .def/.endef */
2100
2101 SKIP_WHITESPACES ();
2102
2103 def_symbol_in_progress = (symbolS *) obstack_alloc (&notes, sizeof (*def_symbol_in_progress));
2104 memset (def_symbol_in_progress, 0, sizeof (*def_symbol_in_progress));
2105
2106 symbol_name = input_line_pointer;
2107 name_end = get_symbol_end ();
2108 symbol_name_length = strlen (symbol_name);
2109 symbol_name_copy = xmalloc (symbol_name_length + 1);
2110 strcpy (symbol_name_copy, symbol_name);
2111 #ifdef tc_canonicalize_symbol_name
2112 symbol_name_copy = tc_canonicalize_symbol_name (symbol_name_copy);
2113 #endif
2114
2115 /* Initialize the new symbol */
2116 #ifdef STRIP_UNDERSCORE
2117 S_SET_NAME (def_symbol_in_progress, (*symbol_name_copy == '_'
2118 ? symbol_name_copy + 1
2119 : symbol_name_copy));
2120 #else /* STRIP_UNDERSCORE */
2121 S_SET_NAME (def_symbol_in_progress, symbol_name_copy);
2122 #endif /* STRIP_UNDERSCORE */
2123 /* free(symbol_name_copy); */
2124 def_symbol_in_progress->sy_name_offset = (unsigned long) ~0;
2125 def_symbol_in_progress->sy_number = ~0;
2126 def_symbol_in_progress->sy_frag = &zero_address_frag;
2127 S_SET_VALUE (def_symbol_in_progress, 0);
2128
2129 if (S_IS_STRING (def_symbol_in_progress))
2130 SF_SET_STRING (def_symbol_in_progress);
2131
2132 *input_line_pointer = name_end;
2133
2134 demand_empty_rest_of_line ();
2135 }
2136
2137 unsigned int dim_index;
2138
2139
2140 static void
2141 obj_coff_endef (ignore)
2142 int ignore;
2143 {
2144 symbolS *symbolP = 0;
2145 /* DIM BUG FIX sac@cygnus.com */
2146 dim_index = 0;
2147 if (def_symbol_in_progress == NULL)
2148 {
2149 as_warn (".endef pseudo-op used outside of .def/.endef: ignored.");
2150 demand_empty_rest_of_line ();
2151 return;
2152 } /* if not inside .def/.endef */
2153
2154 /* Set the section number according to storage class. */
2155 switch (S_GET_STORAGE_CLASS (def_symbol_in_progress))
2156 {
2157 case C_STRTAG:
2158 case C_ENTAG:
2159 case C_UNTAG:
2160 SF_SET_TAG (def_symbol_in_progress);
2161 /* intentional fallthrough */
2162 case C_FILE:
2163 case C_TPDEF:
2164 SF_SET_DEBUG (def_symbol_in_progress);
2165 S_SET_SEGMENT (def_symbol_in_progress, SEG_DEBUG);
2166 break;
2167
2168 case C_EFCN:
2169 SF_SET_LOCAL (def_symbol_in_progress); /* Do not emit this symbol. */
2170 /* intentional fallthrough */
2171 case C_BLOCK:
2172 SF_SET_PROCESS (def_symbol_in_progress); /* Will need processing before writing */
2173 /* intentional fallthrough */
2174 case C_FCN:
2175 S_SET_SEGMENT (def_symbol_in_progress, SEG_E0);
2176
2177 if (strcmp (S_GET_NAME (def_symbol_in_progress), ".bf") == 0)
2178 { /* .bf */
2179 if (function_lineoff < 0)
2180 {
2181 fprintf (stderr, "`.bf' symbol without preceding function\n");
2182 } /* missing function symbol */
2183 SA_GET_SYM_LNNOPTR (last_line_symbol) = function_lineoff;
2184
2185 SF_SET_PROCESS (last_line_symbol);
2186 SF_SET_ADJ_LNNOPTR (last_line_symbol);
2187 SF_SET_PROCESS (def_symbol_in_progress);
2188 function_lineoff = -1;
2189 }
2190 /* Value is always set to . */
2191 def_symbol_in_progress->sy_frag = frag_now;
2192 S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ());
2193 break;
2194
2195 #ifdef C_AUTOARG
2196 case C_AUTOARG:
2197 #endif /* C_AUTOARG */
2198 case C_AUTO:
2199 case C_REG:
2200 case C_MOS:
2201 case C_MOE:
2202 case C_MOU:
2203 case C_ARG:
2204 case C_REGPARM:
2205 case C_FIELD:
2206 case C_EOS:
2207 SF_SET_DEBUG (def_symbol_in_progress);
2208 S_SET_SEGMENT (def_symbol_in_progress, absolute_section);
2209 break;
2210
2211 case C_EXT:
2212 case C_STAT:
2213 case C_LABEL:
2214 /* Valid but set somewhere else (s_comm, s_lcomm, colon) */
2215 break;
2216
2217 case C_USTATIC:
2218 case C_EXTDEF:
2219 case C_ULABEL:
2220 as_warn ("unexpected storage class %d", S_GET_STORAGE_CLASS (def_symbol_in_progress));
2221 break;
2222 } /* switch on storage class */
2223
2224 /* Now that we have built a debug symbol, try to find if we should
2225 merge with an existing symbol or not. If a symbol is C_EFCN or
2226 absolute_section or untagged SEG_DEBUG it never merges. We also
2227 don't merge labels, which are in a different namespace, nor
2228 symbols which have not yet been defined since they are typically
2229 unique, nor do we merge tags with non-tags. */
2230
2231 /* Two cases for functions. Either debug followed by definition or
2232 definition followed by debug. For definition first, we will
2233 merge the debug symbol into the definition. For debug first, the
2234 lineno entry MUST point to the definition function or else it
2235 will point off into space when crawl_symbols() merges the debug
2236 symbol into the real symbol. Therefor, let's presume the debug
2237 symbol is a real function reference. */
2238
2239 /* FIXME-SOON If for some reason the definition label/symbol is
2240 never seen, this will probably leave an undefined symbol at link
2241 time. */
2242
2243 if (S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_EFCN
2244 || S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_LABEL
2245 || (S_GET_SEGMENT (def_symbol_in_progress) == SEG_DEBUG
2246 && !SF_GET_TAG (def_symbol_in_progress))
2247 || S_GET_SEGMENT (def_symbol_in_progress) == absolute_section
2248 || def_symbol_in_progress->sy_value.X_op != O_constant
2249 || (symbolP = symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP)) == NULL
2250 || (SF_GET_TAG (def_symbol_in_progress) != SF_GET_TAG (symbolP)))
2251 {
2252 symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP,
2253 &symbol_lastP);
2254 }
2255 else
2256 {
2257 /* This symbol already exists, merge the newly created symbol
2258 into the old one. This is not mandatory. The linker can
2259 handle duplicate symbols correctly. But I guess that it save
2260 a *lot* of space if the assembly file defines a lot of
2261 symbols. [loic] */
2262
2263 /* The debug entry (def_symbol_in_progress) is merged into the
2264 previous definition. */
2265
2266 c_symbol_merge (def_symbol_in_progress, symbolP);
2267 /* FIXME-SOON Should *def_symbol_in_progress be free'd? xoxorich. */
2268 def_symbol_in_progress = symbolP;
2269
2270 if (SF_GET_FUNCTION (def_symbol_in_progress)
2271 || SF_GET_TAG (def_symbol_in_progress)
2272 || S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_STAT)
2273 {
2274 /* For functions, and tags, and static symbols, the symbol
2275 *must* be where the debug symbol appears. Move the
2276 existing symbol to the current place. */
2277 /* If it already is at the end of the symbol list, do nothing */
2278 if (def_symbol_in_progress != symbol_lastP)
2279 {
2280 symbol_remove (def_symbol_in_progress, &symbol_rootP,
2281 &symbol_lastP);
2282 symbol_append (def_symbol_in_progress, symbol_lastP,
2283 &symbol_rootP, &symbol_lastP);
2284 } /* if not already in place */
2285 } /* if function */
2286 } /* normal or mergable */
2287
2288 if (SF_GET_TAG (def_symbol_in_progress))
2289 {
2290 symbolS *oldtag;
2291
2292 oldtag = symbol_find_base (S_GET_NAME (def_symbol_in_progress),
2293 DO_NOT_STRIP);
2294 if (oldtag == NULL || ! SF_GET_TAG (oldtag))
2295 tag_insert (S_GET_NAME (def_symbol_in_progress),
2296 def_symbol_in_progress);
2297 }
2298
2299 if (SF_GET_FUNCTION (def_symbol_in_progress))
2300 {
2301 know (sizeof (def_symbol_in_progress) <= sizeof (long));
2302 function_lineoff
2303 = c_line_new (def_symbol_in_progress, 0, 0, &zero_address_frag);
2304
2305 SF_SET_PROCESS (def_symbol_in_progress);
2306
2307 if (symbolP == NULL)
2308 {
2309 /* That is, if this is the first time we've seen the
2310 function... */
2311 symbol_table_insert (def_symbol_in_progress);
2312 } /* definition follows debug */
2313 } /* Create the line number entry pointing to the function being defined */
2314
2315 def_symbol_in_progress = NULL;
2316 demand_empty_rest_of_line ();
2317 }
2318
2319 static void
2320 obj_coff_dim (ignore)
2321 int ignore;
2322 {
2323 int dim_index;
2324
2325 if (def_symbol_in_progress == NULL)
2326 {
2327 as_warn (".dim pseudo-op used outside of .def/.endef: ignored.");
2328 demand_empty_rest_of_line ();
2329 return;
2330 } /* if not inside .def/.endef */
2331
2332 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
2333
2334 for (dim_index = 0; dim_index < DIMNUM; dim_index++)
2335 {
2336 SKIP_WHITESPACES ();
2337 SA_SET_SYM_DIMEN (def_symbol_in_progress, dim_index,
2338 get_absolute_expression ());
2339
2340 switch (*input_line_pointer)
2341 {
2342 case ',':
2343 input_line_pointer++;
2344 break;
2345
2346 default:
2347 as_warn ("badly formed .dim directive ignored");
2348 /* intentional fallthrough */
2349 case '\n':
2350 case ';':
2351 dim_index = DIMNUM;
2352 break;
2353 }
2354 }
2355
2356 demand_empty_rest_of_line ();
2357 }
2358
2359 static void
2360 obj_coff_line (ignore)
2361 int ignore;
2362 {
2363 int this_base;
2364 const char *name;
2365
2366 if (def_symbol_in_progress == NULL)
2367 {
2368 obj_coff_ln (0);
2369 return;
2370 }
2371
2372 name = S_GET_NAME (def_symbol_in_progress);
2373 this_base = get_absolute_expression ();
2374
2375 /* Only .bf symbols indicate the use of a new base line number; the
2376 line numbers associated with .ef, .bb, .eb are relative to the
2377 start of the containing function. */
2378 if (!strcmp (".bf", name))
2379 {
2380 #if 0 /* XXX Can we ever have line numbers going backwards? */
2381 if (this_base > line_base)
2382 #endif
2383 {
2384 line_base = this_base;
2385 }
2386
2387 #ifndef NO_LISTING
2388 {
2389 extern int listing;
2390 if (listing)
2391 {
2392 listing_source_line ((unsigned int) line_base);
2393 }
2394 }
2395 #endif
2396 }
2397
2398 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
2399 SA_SET_SYM_LNNO (def_symbol_in_progress, this_base);
2400
2401 demand_empty_rest_of_line ();
2402 }
2403
2404 static void
2405 obj_coff_size (ignore)
2406 int ignore;
2407 {
2408 if (def_symbol_in_progress == NULL)
2409 {
2410 as_warn (".size pseudo-op used outside of .def/.endef ignored.");
2411 demand_empty_rest_of_line ();
2412 return;
2413 } /* if not inside .def/.endef */
2414
2415 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
2416 SA_SET_SYM_SIZE (def_symbol_in_progress, get_absolute_expression ());
2417 demand_empty_rest_of_line ();
2418 }
2419
2420 static void
2421 obj_coff_scl (ignore)
2422 int ignore;
2423 {
2424 if (def_symbol_in_progress == NULL)
2425 {
2426 as_warn (".scl pseudo-op used outside of .def/.endef ignored.");
2427 demand_empty_rest_of_line ();
2428 return;
2429 } /* if not inside .def/.endef */
2430
2431 S_SET_STORAGE_CLASS (def_symbol_in_progress, get_absolute_expression ());
2432 demand_empty_rest_of_line ();
2433 }
2434
2435 static void
2436 obj_coff_tag (ignore)
2437 int ignore;
2438 {
2439 char *symbol_name;
2440 char name_end;
2441
2442 if (def_symbol_in_progress == NULL)
2443 {
2444 as_warn (".tag pseudo-op used outside of .def/.endef ignored.");
2445 demand_empty_rest_of_line ();
2446 return;
2447 }
2448
2449 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
2450 symbol_name = input_line_pointer;
2451 name_end = get_symbol_end ();
2452 #ifdef tc_canonicalize_symbol_name
2453 symbol_name = tc_canonicalize_symbol_name (symbol_name);
2454 #endif
2455
2456 /* Assume that the symbol referred to by .tag is always defined.
2457 This was a bad assumption. I've added find_or_make. xoxorich. */
2458 SA_SET_SYM_TAGNDX (def_symbol_in_progress,
2459 (long) tag_find_or_make (symbol_name));
2460 if (SA_GET_SYM_TAGNDX (def_symbol_in_progress) == 0L)
2461 {
2462 as_warn ("tag not found for .tag %s", symbol_name);
2463 } /* not defined */
2464
2465 SF_SET_TAGGED (def_symbol_in_progress);
2466 *input_line_pointer = name_end;
2467
2468 demand_empty_rest_of_line ();
2469 }
2470
2471 static void
2472 obj_coff_type (ignore)
2473 int ignore;
2474 {
2475 if (def_symbol_in_progress == NULL)
2476 {
2477 as_warn (".type pseudo-op used outside of .def/.endef ignored.");
2478 demand_empty_rest_of_line ();
2479 return;
2480 } /* if not inside .def/.endef */
2481
2482 S_SET_DATA_TYPE (def_symbol_in_progress, get_absolute_expression ());
2483
2484 if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress)) &&
2485 S_GET_STORAGE_CLASS (def_symbol_in_progress) != C_TPDEF)
2486 {
2487 SF_SET_FUNCTION (def_symbol_in_progress);
2488 } /* is a function */
2489
2490 demand_empty_rest_of_line ();
2491 }
2492
2493 static void
2494 obj_coff_val (ignore)
2495 int ignore;
2496 {
2497 if (def_symbol_in_progress == NULL)
2498 {
2499 as_warn (".val pseudo-op used outside of .def/.endef ignored.");
2500 demand_empty_rest_of_line ();
2501 return;
2502 } /* if not inside .def/.endef */
2503
2504 if (is_name_beginner (*input_line_pointer))
2505 {
2506 char *symbol_name = input_line_pointer;
2507 char name_end = get_symbol_end ();
2508
2509 #ifdef tc_canonicalize_symbol_name
2510 symbol_name = tc_canonicalize_symbol_name (symbol_name);
2511 #endif
2512
2513 if (!strcmp (symbol_name, "."))
2514 {
2515 def_symbol_in_progress->sy_frag = frag_now;
2516 S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ());
2517 /* If the .val is != from the .def (e.g. statics) */
2518 }
2519 else if (strcmp (S_GET_NAME (def_symbol_in_progress), symbol_name))
2520 {
2521 def_symbol_in_progress->sy_value.X_op = O_symbol;
2522 def_symbol_in_progress->sy_value.X_add_symbol =
2523 symbol_find_or_make (symbol_name);
2524 def_symbol_in_progress->sy_value.X_op_symbol = NULL;
2525 def_symbol_in_progress->sy_value.X_add_number = 0;
2526
2527 /* If the segment is undefined when the forward reference is
2528 resolved, then copy the segment id from the forward
2529 symbol. */
2530 SF_SET_GET_SEGMENT (def_symbol_in_progress);
2531
2532 /* FIXME: gcc can generate address expressions
2533 here in unusual cases (search for "obscure"
2534 in sdbout.c). We just ignore the offset
2535 here, thus generating incorrect debugging
2536 information. We ignore the rest of the
2537 line just below. */
2538 }
2539 /* Otherwise, it is the name of a non debug symbol and
2540 its value will be calculated later. */
2541 *input_line_pointer = name_end;
2542
2543 /* FIXME: this is to avoid an error message in the
2544 FIXME case mentioned just above. */
2545 while (! is_end_of_line[(unsigned char) *input_line_pointer])
2546 ++input_line_pointer;
2547 }
2548 else
2549 {
2550 S_SET_VALUE (def_symbol_in_progress,
2551 (valueT) get_absolute_expression ());
2552 } /* if symbol based */
2553
2554 demand_empty_rest_of_line ();
2555 }
2556
2557 #ifdef TE_PE
2558
2559 /* Handle the .linkonce pseudo-op. This is parsed by s_linkonce in
2560 read.c, which then calls this object file format specific routine. */
2561
2562 void
2563 obj_coff_pe_handle_link_once (type)
2564 enum linkonce_type type;
2565 {
2566 seg_info (now_seg)->scnhdr.s_flags |= IMAGE_SCN_LNK_COMDAT;
2567
2568 /* We store the type in the seg_info structure, and use it to set up
2569 the auxiliary entry for the section symbol in c_section_symbol. */
2570 seg_info (now_seg)->linkonce = type;
2571 }
2572
2573 #endif /* TE_PE */
2574
2575 void
2576 coff_obj_read_begin_hook ()
2577 {
2578 /* These had better be the same. Usually 18 bytes. */
2579 #ifndef BFD_HEADERS
2580 know (sizeof (SYMENT) == sizeof (AUXENT));
2581 know (SYMESZ == AUXESZ);
2582 #endif
2583 tag_init ();
2584 }
2585
2586 /* This function runs through the symbol table and puts all the
2587 externals onto another chain */
2588
2589 /* The chain of globals. */
2590 symbolS *symbol_globalP;
2591 symbolS *symbol_global_lastP;
2592
2593 /* The chain of externals */
2594 symbolS *symbol_externP;
2595 symbolS *symbol_extern_lastP;
2596
2597 stack *block_stack;
2598 symbolS *last_functionP;
2599 static symbolS *last_bfP;
2600 symbolS *last_tagP;
2601
2602 static unsigned int
2603 yank_symbols ()
2604 {
2605 symbolS *symbolP;
2606 unsigned int symbol_number = 0;
2607 unsigned int last_file_symno = 0;
2608
2609 struct filename_list *filename_list_scan = filename_list_head;
2610
2611 for (symbolP = symbol_rootP;
2612 symbolP;
2613 symbolP = symbolP ? symbol_next (symbolP) : symbol_rootP)
2614 {
2615 if (symbolP->sy_mri_common)
2616 {
2617 if (S_GET_STORAGE_CLASS (symbolP) == C_EXT)
2618 as_bad ("%s: global symbols not supported in common sections",
2619 S_GET_NAME (symbolP));
2620 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2621 continue;
2622 }
2623
2624 if (!SF_GET_DEBUG (symbolP))
2625 {
2626 /* Debug symbols do not need all this rubbish */
2627 symbolS *real_symbolP;
2628
2629 /* L* and C_EFCN symbols never merge. */
2630 if (!SF_GET_LOCAL (symbolP)
2631 && !SF_GET_STATICS (symbolP)
2632 && S_GET_STORAGE_CLASS (symbolP) != C_LABEL
2633 && symbolP->sy_value.X_op == O_constant
2634 && (real_symbolP = symbol_find_base (S_GET_NAME (symbolP), DO_NOT_STRIP))
2635 && real_symbolP != symbolP)
2636 {
2637 /* FIXME-SOON: where do dups come from?
2638 Maybe tag references before definitions? xoxorich. */
2639 /* Move the debug data from the debug symbol to the
2640 real symbol. Do NOT do the oposite (i.e. move from
2641 real symbol to debug symbol and remove real symbol from the
2642 list.) Because some pointers refer to the real symbol
2643 whereas no pointers refer to the debug symbol. */
2644 c_symbol_merge (symbolP, real_symbolP);
2645 /* Replace the current symbol by the real one */
2646 /* The symbols will never be the last or the first
2647 because : 1st symbol is .file and 3 last symbols are
2648 .text, .data, .bss */
2649 symbol_remove (real_symbolP, &symbol_rootP, &symbol_lastP);
2650 symbol_insert (real_symbolP, symbolP, &symbol_rootP, &symbol_lastP);
2651 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2652 symbolP = real_symbolP;
2653 } /* if not local but dup'd */
2654
2655 if (flag_readonly_data_in_text && (S_GET_SEGMENT (symbolP) == SEG_E1))
2656 {
2657 S_SET_SEGMENT (symbolP, SEG_E0);
2658 } /* push data into text */
2659
2660 resolve_symbol_value (symbolP);
2661
2662 if (S_GET_STORAGE_CLASS (symbolP) == C_NULL)
2663 {
2664 if (!S_IS_DEFINED (symbolP) && !SF_GET_LOCAL (symbolP))
2665 {
2666 S_SET_EXTERNAL (symbolP);
2667 }
2668 else if (S_GET_SEGMENT (symbolP) == SEG_E0)
2669 {
2670 S_SET_STORAGE_CLASS (symbolP, C_LABEL);
2671 }
2672 else
2673 {
2674 S_SET_STORAGE_CLASS (symbolP, C_STAT);
2675 }
2676 }
2677
2678 /* Mainly to speed up if not -g */
2679 if (SF_GET_PROCESS (symbolP))
2680 {
2681 /* Handle the nested blocks auxiliary info. */
2682 if (S_GET_STORAGE_CLASS (symbolP) == C_BLOCK)
2683 {
2684 if (!strcmp (S_GET_NAME (symbolP), ".bb"))
2685 stack_push (block_stack, (char *) &symbolP);
2686 else
2687 { /* .eb */
2688 register symbolS *begin_symbolP;
2689 begin_symbolP = *(symbolS **) stack_pop (block_stack);
2690 if (begin_symbolP == (symbolS *) 0)
2691 as_warn ("mismatched .eb");
2692 else
2693 SA_SET_SYM_ENDNDX (begin_symbolP, symbol_number + 2);
2694 }
2695 }
2696 /* If we are able to identify the type of a function, and we
2697 are out of a function (last_functionP == 0) then, the
2698 function symbol will be associated with an auxiliary
2699 entry. */
2700 if (last_functionP == (symbolS *) 0 &&
2701 SF_GET_FUNCTION (symbolP))
2702 {
2703 last_functionP = symbolP;
2704
2705 if (S_GET_NUMBER_AUXILIARY (symbolP) < 1)
2706 {
2707 S_SET_NUMBER_AUXILIARY (symbolP, 1);
2708 } /* make it at least 1 */
2709
2710 /* Clobber possible stale .dim information. */
2711 #if 0
2712 /* Iffed out by steve - this fries the lnnoptr info too */
2713 bzero (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen,
2714 sizeof (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen));
2715 #endif
2716 }
2717 if (S_GET_STORAGE_CLASS (symbolP) == C_FCN)
2718 {
2719 if (strcmp (S_GET_NAME (symbolP), ".bf") == 0)
2720 {
2721 if (last_bfP != NULL)
2722 SA_SET_SYM_ENDNDX (last_bfP, symbol_number);
2723 last_bfP = symbolP;
2724 }
2725 }
2726 else if (S_GET_STORAGE_CLASS (symbolP) == C_EFCN)
2727 {
2728 /* I don't even know if this is needed for sdb. But
2729 the standard assembler generates it, so... */
2730 if (last_functionP == (symbolS *) 0)
2731 as_fatal ("C_EFCN symbol out of scope");
2732 SA_SET_SYM_FSIZE (last_functionP,
2733 (long) (S_GET_VALUE (symbolP) -
2734 S_GET_VALUE (last_functionP)));
2735 SA_SET_SYM_ENDNDX (last_functionP, symbol_number);
2736 last_functionP = (symbolS *) 0;
2737 }
2738 }
2739 }
2740 else if (SF_GET_TAG (symbolP))
2741 {
2742 /* First descriptor of a structure must point to
2743 the first slot after the structure description. */
2744 last_tagP = symbolP;
2745
2746 }
2747 else if (S_GET_STORAGE_CLASS (symbolP) == C_EOS)
2748 {
2749 /* +2 take in account the current symbol */
2750 SA_SET_SYM_ENDNDX (last_tagP, symbol_number + 2);
2751 }
2752 else if (S_GET_STORAGE_CLASS (symbolP) == C_FILE)
2753 {
2754 /* If the filename was too long to fit in the
2755 auxent, put it in the string table */
2756 if (SA_GET_FILE_FNAME_ZEROS (symbolP) == 0
2757 && SA_GET_FILE_FNAME_OFFSET (symbolP) != 0)
2758 {
2759 SA_SET_FILE_FNAME_OFFSET (symbolP, string_byte_count);
2760 string_byte_count += strlen (filename_list_scan->filename) + 1;
2761 filename_list_scan = filename_list_scan->next;
2762 }
2763 if (S_GET_VALUE (symbolP))
2764 {
2765 S_SET_VALUE (symbolP, last_file_symno);
2766 last_file_symno = symbol_number;
2767 } /* no one points at the first .file symbol */
2768 } /* if debug or tag or eos or file */
2769
2770 #ifdef tc_frob_coff_symbol
2771 tc_frob_coff_symbol (symbolP);
2772 #endif
2773
2774 /* We must put the external symbols apart. The loader
2775 does not bomb if we do not. But the references in
2776 the endndx field for a .bb symbol are not corrected
2777 if an external symbol is removed between .bb and .be.
2778 I.e in the following case :
2779 [20] .bb endndx = 22
2780 [21] foo external
2781 [22] .be
2782 ld will move the symbol 21 to the end of the list but
2783 endndx will still be 22 instead of 21. */
2784
2785
2786 if (SF_GET_LOCAL (symbolP))
2787 {
2788 /* remove C_EFCN and LOCAL (L...) symbols */
2789 /* next pointer remains valid */
2790 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2791
2792 }
2793 else if (symbolP->sy_value.X_op == O_symbol
2794 && (! S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP)))
2795 {
2796 /* Skip symbols which were equated to undefined or common
2797 symbols. */
2798 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2799 }
2800 else if (!S_IS_DEFINED (symbolP)
2801 && !S_IS_DEBUG (symbolP)
2802 && !SF_GET_STATICS (symbolP) &&
2803 S_GET_STORAGE_CLASS (symbolP) == C_EXT)
2804 { /* C_EXT && !SF_GET_FUNCTION(symbolP)) */
2805 /* if external, Remove from the list */
2806 symbolS *hold = symbol_previous (symbolP);
2807
2808 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2809 symbol_clear_list_pointers (symbolP);
2810 symbol_append (symbolP, symbol_extern_lastP, &symbol_externP, &symbol_extern_lastP);
2811 symbolP = hold;
2812 }
2813 else if (! S_IS_DEBUG (symbolP)
2814 && ! SF_GET_STATICS (symbolP)
2815 && ! SF_GET_FUNCTION (symbolP)
2816 && S_GET_STORAGE_CLASS (symbolP) == C_EXT)
2817 {
2818 symbolS *hold = symbol_previous (symbolP);
2819
2820 /* The O'Reilly COFF book says that defined global symbols
2821 come at the end of the symbol table, just before
2822 undefined global symbols. */
2823
2824 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2825 symbol_clear_list_pointers (symbolP);
2826 symbol_append (symbolP, symbol_global_lastP, &symbol_globalP,
2827 &symbol_global_lastP);
2828 symbolP = hold;
2829 }
2830 else
2831 {
2832 if (SF_GET_STRING (symbolP))
2833 {
2834 symbolP->sy_name_offset = string_byte_count;
2835 string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
2836 }
2837 else
2838 {
2839 symbolP->sy_name_offset = 0;
2840 } /* fix "long" names */
2841
2842 symbolP->sy_number = symbol_number;
2843 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
2844 } /* if local symbol */
2845 } /* traverse the symbol list */
2846 return symbol_number;
2847
2848 }
2849
2850
2851 static unsigned int
2852 glue_symbols (head, tail)
2853 symbolS **head;
2854 symbolS **tail;
2855 {
2856 unsigned int symbol_number = 0;
2857
2858 while (*head != NULL)
2859 {
2860 symbolS *tmp = *head;
2861
2862 /* append */
2863 symbol_remove (tmp, head, tail);
2864 symbol_append (tmp, symbol_lastP, &symbol_rootP, &symbol_lastP);
2865
2866 /* and process */
2867 if (SF_GET_STRING (tmp))
2868 {
2869 tmp->sy_name_offset = string_byte_count;
2870 string_byte_count += strlen (S_GET_NAME (tmp)) + 1;
2871 }
2872 else
2873 {
2874 tmp->sy_name_offset = 0;
2875 } /* fix "long" names */
2876
2877 tmp->sy_number = symbol_number;
2878 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (tmp);
2879 } /* append the entire extern chain */
2880
2881 return symbol_number;
2882 }
2883
2884 static unsigned int
2885 tie_tags ()
2886 {
2887 unsigned int symbol_number = 0;
2888 symbolS *symbolP;
2889
2890 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
2891 {
2892 symbolP->sy_number = symbol_number;
2893
2894 if (SF_GET_TAGGED (symbolP))
2895 {
2896 SA_SET_SYM_TAGNDX
2897 (symbolP,
2898 ((symbolS *) SA_GET_SYM_TAGNDX (symbolP))->sy_number);
2899 }
2900
2901 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
2902 }
2903
2904 return symbol_number;
2905 }
2906
2907 static void
2908 crawl_symbols (h, abfd)
2909 object_headers *h;
2910 bfd * abfd;
2911 {
2912 unsigned int i;
2913
2914 /* Initialize the stack used to keep track of the matching .bb .be */
2915
2916 block_stack = stack_init (512, sizeof (symbolS *));
2917
2918 /* The symbol list should be ordered according to the following sequence
2919 * order :
2920 * . .file symbol
2921 * . debug entries for functions
2922 * . fake symbols for the sections, including .text .data and .bss
2923 * . defined symbols
2924 * . undefined symbols
2925 * But this is not mandatory. The only important point is to put the
2926 * undefined symbols at the end of the list.
2927 */
2928
2929 /* Is there a .file symbol ? If not insert one at the beginning. */
2930 if (symbol_rootP == NULL
2931 || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE)
2932 {
2933 c_dot_file_symbol ("fake");
2934 }
2935
2936 /*
2937 * Build up static symbols for the sections, they are filled in later
2938 */
2939
2940
2941 for (i = SEG_E0; i < SEG_LAST; i++)
2942 if (segment_info[i].scnhdr.s_name[0])
2943 segment_info[i].dot = c_section_symbol (segment_info[i].name,
2944 i - SEG_E0 + 1);
2945
2946 /* Take all the externals out and put them into another chain */
2947 H_SET_SYMBOL_TABLE_SIZE (h, yank_symbols ());
2948 /* Take the externals and glue them onto the end.*/
2949 H_SET_SYMBOL_TABLE_SIZE (h,
2950 (H_GET_SYMBOL_COUNT (h)
2951 + glue_symbols (&symbol_globalP,
2952 &symbol_global_lastP)
2953 + glue_symbols (&symbol_externP,
2954 &symbol_extern_lastP)));
2955
2956 H_SET_SYMBOL_TABLE_SIZE (h, tie_tags ());
2957 know (symbol_globalP == NULL);
2958 know (symbol_global_lastP == NULL);
2959 know (symbol_externP == NULL);
2960 know (symbol_extern_lastP == NULL);
2961 }
2962
2963 /*
2964 * Find strings by crawling along symbol table chain.
2965 */
2966
2967 void
2968 w_strings (where)
2969 char *where;
2970 {
2971 symbolS *symbolP;
2972 struct filename_list *filename_list_scan = filename_list_head;
2973
2974 /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
2975 md_number_to_chars (where, (valueT) string_byte_count, 4);
2976 where += 4;
2977
2978 #ifdef COFF_LONG_SECTION_NAMES
2979 /* Support long section names as found in PE. This code must
2980 coordinate with that in coff_header_append and write_object_file. */
2981 {
2982 unsigned int i;
2983
2984 for (i = SEG_E0; i < SEG_LAST; i++)
2985 {
2986 if (segment_info[i].scnhdr.s_name[0]
2987 && strlen (segment_info[i].name) > SCNNMLEN)
2988 {
2989 unsigned int size;
2990
2991 size = strlen (segment_info[i].name) + 1;
2992 memcpy (where, segment_info[i].name, size);
2993 where += size;
2994 }
2995 }
2996 }
2997 #endif /* COFF_LONG_SECTION_NAMES */
2998
2999 for (symbolP = symbol_rootP;
3000 symbolP;
3001 symbolP = symbol_next (symbolP))
3002 {
3003 unsigned int size;
3004
3005 if (SF_GET_STRING (symbolP))
3006 {
3007 size = strlen (S_GET_NAME (symbolP)) + 1;
3008 memcpy (where, S_GET_NAME (symbolP), size);
3009 where += size;
3010 }
3011 if (S_GET_STORAGE_CLASS (symbolP) == C_FILE
3012 && SA_GET_FILE_FNAME_ZEROS (symbolP) == 0
3013 && SA_GET_FILE_FNAME_OFFSET (symbolP) != 0)
3014 {
3015 size = strlen (filename_list_scan->filename) + 1;
3016 memcpy (where, filename_list_scan->filename, size);
3017 filename_list_scan = filename_list_scan ->next;
3018 where += size;
3019 }
3020 }
3021 }
3022
3023 static void
3024 do_linenos_for (abfd, h, file_cursor)
3025 bfd * abfd;
3026 object_headers * h;
3027 unsigned long *file_cursor;
3028 {
3029 unsigned int idx;
3030 unsigned long start = *file_cursor;
3031
3032 for (idx = SEG_E0; idx < SEG_LAST; idx++)
3033 {
3034 segment_info_type *s = segment_info + idx;
3035
3036
3037 if (s->scnhdr.s_nlnno != 0)
3038 {
3039 struct lineno_list *line_ptr;
3040
3041 struct external_lineno *buffer =
3042 (struct external_lineno *) xmalloc (s->scnhdr.s_nlnno * LINESZ);
3043
3044 struct external_lineno *dst = buffer;
3045
3046 /* Run through the table we've built and turn it into its external
3047 form, take this chance to remove duplicates */
3048
3049 for (line_ptr = s->lineno_list_head;
3050 line_ptr != (struct lineno_list *) NULL;
3051 line_ptr = line_ptr->next)
3052 {
3053
3054 if (line_ptr->line.l_lnno == 0)
3055 {
3056 /* Turn a pointer to a symbol into the symbols' index */
3057 line_ptr->line.l_addr.l_symndx =
3058 ((symbolS *) line_ptr->line.l_addr.l_symndx)->sy_number;
3059 }
3060 else
3061 {
3062 line_ptr->line.l_addr.l_paddr += ((struct frag *) (line_ptr->frag))->fr_address;
3063 }
3064
3065
3066 (void) bfd_coff_swap_lineno_out (abfd, &(line_ptr->line), dst);
3067 dst++;
3068
3069 }
3070
3071 s->scnhdr.s_lnnoptr = *file_cursor;
3072
3073 bfd_write (buffer, 1, s->scnhdr.s_nlnno * LINESZ, abfd);
3074 free (buffer);
3075
3076 *file_cursor += s->scnhdr.s_nlnno * LINESZ;
3077 }
3078 }
3079 H_SET_LINENO_SIZE (h, *file_cursor - start);
3080 }
3081
3082
3083 /* Now we run through the list of frag chains in a segment and
3084 make all the subsegment frags appear at the end of the
3085 list, as if the seg 0 was extra long */
3086
3087 static void
3088 remove_subsegs ()
3089 {
3090 unsigned int i;
3091
3092 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3093 {
3094 frchainS *head = segment_info[i].frchainP;
3095 fragS dummy;
3096 fragS *prev_frag = &dummy;
3097
3098 while (head && head->frch_seg == i)
3099 {
3100 prev_frag->fr_next = head->frch_root;
3101 prev_frag = head->frch_last;
3102 head = head->frch_next;
3103 }
3104 prev_frag->fr_next = 0;
3105 }
3106 }
3107
3108 unsigned long machine;
3109 int coff_flags;
3110 extern void
3111 write_object_file ()
3112 {
3113 int i;
3114 const char *name;
3115 struct frchain *frchain_ptr;
3116
3117 object_headers headers;
3118 unsigned long file_cursor;
3119 bfd *abfd;
3120 unsigned int addr;
3121 abfd = bfd_openw (out_file_name, TARGET_FORMAT);
3122
3123
3124 if (abfd == 0)
3125 {
3126 as_perror ("FATAL: Can't create %s", out_file_name);
3127 exit (EXIT_FAILURE);
3128 }
3129 bfd_set_format (abfd, bfd_object);
3130 bfd_set_arch_mach (abfd, BFD_ARCH, machine);
3131
3132 string_byte_count = 4;
3133
3134 for (frchain_ptr = frchain_root;
3135 frchain_ptr != (struct frchain *) NULL;
3136 frchain_ptr = frchain_ptr->frch_next)
3137 {
3138 /* Run through all the sub-segments and align them up. Also
3139 close any open frags. We tack a .fill onto the end of the
3140 frag chain so that any .align's size can be worked by looking
3141 at the next frag. */
3142
3143 subseg_set (frchain_ptr->frch_seg, frchain_ptr->frch_subseg);
3144 #ifndef SUB_SEGMENT_ALIGN
3145 #define SUB_SEGMENT_ALIGN(SEG) 1
3146 #endif
3147 #ifdef md_do_align
3148 {
3149 static char nop = NOP_OPCODE;
3150 md_do_align (SUB_SEGMENT_ALIGN (now_seg), &nop, 1, 0, alignment_done);
3151 }
3152 #endif
3153 frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE, 0);
3154 #ifdef md_do_align
3155 alignment_done:
3156 #endif
3157 frag_wane (frag_now);
3158 frag_now->fr_fix = 0;
3159 know (frag_now->fr_next == NULL);
3160 }
3161
3162
3163 remove_subsegs ();
3164
3165
3166 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3167 {
3168 relax_segment (segment_info[i].frchainP->frch_root, i);
3169 }
3170
3171 H_SET_NUMBER_OF_SECTIONS (&headers, 0);
3172
3173 /* Find out how big the sections are, and set the addresses. */
3174 addr = 0;
3175 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3176 {
3177 long size;
3178
3179 segment_info[i].scnhdr.s_paddr = addr;
3180 segment_info[i].scnhdr.s_vaddr = addr;
3181
3182 if (segment_info[i].scnhdr.s_name[0])
3183 {
3184 H_SET_NUMBER_OF_SECTIONS (&headers,
3185 H_GET_NUMBER_OF_SECTIONS (&headers) + 1);
3186
3187 #ifdef COFF_LONG_SECTION_NAMES
3188 /* Support long section names as found in PE. This code
3189 must coordinate with that in coff_header_append and
3190 w_strings. */
3191 {
3192 unsigned int len;
3193
3194 len = strlen (segment_info[i].name);
3195 if (len > SCNNMLEN)
3196 string_byte_count += len + 1;
3197 }
3198 #endif /* COFF_LONG_SECTION_NAMES */
3199 }
3200
3201 size = size_section (abfd, (unsigned int) i);
3202 addr += size;
3203
3204 /* I think the section alignment is only used on the i960; the
3205 i960 needs it, and it should do no harm on other targets. */
3206 segment_info[i].scnhdr.s_align = 1 << section_alignment[i];
3207
3208 if (i == SEG_E0)
3209 H_SET_TEXT_SIZE (&headers, size);
3210 else if (i == SEG_E1)
3211 H_SET_DATA_SIZE (&headers, size);
3212 else if (i == SEG_E2)
3213 H_SET_BSS_SIZE (&headers, size);
3214 }
3215
3216 /* Turn the gas native symbol table shape into a coff symbol table */
3217 crawl_symbols (&headers, abfd);
3218
3219 if (string_byte_count == 4)
3220 string_byte_count = 0;
3221
3222 H_SET_STRING_SIZE (&headers, string_byte_count);
3223
3224 #ifdef tc_frob_file
3225 tc_frob_file ();
3226 #endif
3227
3228 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3229 {
3230 fixup_mdeps (segment_info[i].frchainP->frch_root, &headers, i);
3231 fixup_segment (&segment_info[i], i);
3232 }
3233
3234 /* Look for ".stab" segments and fill in their initial symbols
3235 correctly. */
3236 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3237 {
3238 name = segment_info[i].name;
3239
3240 if (name != NULL
3241 && strncmp (".stab", name, 5) == 0
3242 && strncmp (".stabstr", name, 8) != 0)
3243 adjust_stab_section (abfd, i);
3244 }
3245
3246 file_cursor = H_GET_TEXT_FILE_OFFSET (&headers);
3247
3248 bfd_seek (abfd, (file_ptr) file_cursor, 0);
3249
3250 /* Plant the data */
3251
3252 fill_section (abfd, &headers, &file_cursor);
3253
3254 do_relocs_for (abfd, &headers, &file_cursor);
3255
3256 do_linenos_for (abfd, &headers, &file_cursor);
3257
3258 H_SET_FILE_MAGIC_NUMBER (&headers, COFF_MAGIC);
3259 #ifndef OBJ_COFF_OMIT_TIMESTAMP
3260 H_SET_TIME_STAMP (&headers, (long)time((time_t *)0));
3261 #else
3262 H_SET_TIME_STAMP (&headers, 0);
3263 #endif
3264 #ifdef TC_COFF_SET_MACHINE
3265 TC_COFF_SET_MACHINE (&headers);
3266 #endif
3267
3268 #ifndef COFF_FLAGS
3269 #define COFF_FLAGS 0
3270 #endif
3271
3272 #ifdef KEEP_RELOC_INFO
3273 H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers) ? 0 : F_LNNO) |
3274 COFF_FLAGS | coff_flags));
3275 #else
3276 H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers) ? 0 : F_LNNO) |
3277 (H_GET_RELOCATION_SIZE(&headers) ? 0 : F_RELFLG) |
3278 COFF_FLAGS | coff_flags));
3279 #endif
3280
3281 {
3282 unsigned int symtable_size = H_GET_SYMBOL_TABLE_SIZE (&headers);
3283 char *buffer1 = xmalloc (symtable_size + string_byte_count + 1);
3284
3285 H_SET_SYMBOL_TABLE_POINTER (&headers, bfd_tell (abfd));
3286 w_symbols (abfd, buffer1, symbol_rootP);
3287 if (string_byte_count > 0)
3288 w_strings (buffer1 + symtable_size);
3289 bfd_write (buffer1, 1, symtable_size + string_byte_count, abfd);
3290 free (buffer1);
3291 }
3292
3293 coff_header_append (abfd, &headers);
3294 #if 0
3295 /* Recent changes to write need this, but where it should
3296 go is up to Ken.. */
3297 if (bfd_close_all_done (abfd) == false)
3298 as_fatal ("Can't close %s: %s", out_file_name,
3299 bfd_errmsg (bfd_get_error ()));
3300 #else
3301 {
3302 extern bfd *stdoutput;
3303 stdoutput = abfd;
3304 }
3305 #endif
3306
3307 }
3308
3309 /* Add a new segment. This is called from subseg_new via the
3310 obj_new_segment macro. */
3311
3312 segT
3313 obj_coff_add_segment (name)
3314 const char *name;
3315 {
3316 unsigned int i;
3317
3318 #ifndef COFF_LONG_SECTION_NAMES
3319 char buf[SCNNMLEN + 1];
3320
3321 strncpy (buf, name, SCNNMLEN);
3322 buf[SCNNMLEN] = '\0';
3323 name = buf;
3324 #endif
3325
3326 for (i = SEG_E0; i < SEG_LAST && segment_info[i].scnhdr.s_name[0]; i++)
3327 if (strcmp (name, segment_info[i].name) == 0)
3328 return (segT) i;
3329
3330 if (i == SEG_LAST)
3331 {
3332 as_bad ("Too many new sections; can't add \"%s\"", name);
3333 return now_seg;
3334 }
3335
3336 /* Add a new section. */
3337 strncpy (segment_info[i].scnhdr.s_name, name,
3338 sizeof (segment_info[i].scnhdr.s_name));
3339 segment_info[i].scnhdr.s_flags = STYP_REG;
3340 segment_info[i].name = xstrdup (name);
3341
3342 return (segT) i;
3343 }
3344
3345 /*
3346 * implement the .section pseudo op:
3347 * .section name {, "flags"}
3348 * ^ ^
3349 * | +--- optional flags: 'b' for bss
3350 * | 'i' for info
3351 * +-- section name 'l' for lib
3352 * 'n' for noload
3353 * 'o' for over
3354 * 'w' for data
3355 * 'd' (apparently m88k for data)
3356 * 'x' for text
3357 * 'r' for read-only data
3358 * But if the argument is not a quoted string, treat it as a
3359 * subsegment number.
3360 */
3361
3362 void
3363 obj_coff_section (ignore)
3364 int ignore;
3365 {
3366 /* Strip out the section name */
3367 char *section_name, *name;
3368 char c;
3369 unsigned int exp;
3370 long flags;
3371
3372 if (flag_mri)
3373 {
3374 char type;
3375
3376 s_mri_sect (&type);
3377 flags = 0;
3378 if (type == 'C')
3379 flags = STYP_TEXT;
3380 else if (type == 'D')
3381 flags = STYP_DATA;
3382 segment_info[now_seg].scnhdr.s_flags |= flags;
3383
3384 return;
3385 }
3386
3387 section_name = input_line_pointer;
3388 c = get_symbol_end ();
3389
3390 name = xmalloc (input_line_pointer - section_name + 1);
3391 strcpy (name, section_name);
3392
3393 *input_line_pointer = c;
3394
3395 exp = 0;
3396 flags = 0;
3397
3398 SKIP_WHITESPACE ();
3399 if (*input_line_pointer == ',')
3400 {
3401 ++input_line_pointer;
3402 SKIP_WHITESPACE ();
3403
3404 if (*input_line_pointer != '"')
3405 exp = get_absolute_expression ();
3406 else
3407 {
3408 ++input_line_pointer;
3409 while (*input_line_pointer != '"'
3410 && ! is_end_of_line[(unsigned char) *input_line_pointer])
3411 {
3412 switch (*input_line_pointer)
3413 {
3414 case 'b': flags |= STYP_BSS; break;
3415 case 'i': flags |= STYP_INFO; break;
3416 case 'l': flags |= STYP_LIB; break;
3417 case 'n': flags |= STYP_NOLOAD; break;
3418 case 'o': flags |= STYP_OVER; break;
3419 case 'd':
3420 case 'w': flags |= STYP_DATA; break;
3421 case 'x': flags |= STYP_TEXT; break;
3422 case 'r': flags |= STYP_LIT; break;
3423 default:
3424 as_warn("unknown section attribute '%c'",
3425 *input_line_pointer);
3426 break;
3427 }
3428 ++input_line_pointer;
3429 }
3430 if (*input_line_pointer == '"')
3431 ++input_line_pointer;
3432 }
3433 }
3434
3435 subseg_new (name, (subsegT) exp);
3436
3437 segment_info[now_seg].scnhdr.s_flags |= flags;
3438
3439 demand_empty_rest_of_line ();
3440 }
3441
3442
3443 static void
3444 obj_coff_text (ignore)
3445 int ignore;
3446 {
3447 subseg_new (".text", get_absolute_expression ());
3448 }
3449
3450
3451 static void
3452 obj_coff_data (ignore)
3453 int ignore;
3454 {
3455 if (flag_readonly_data_in_text)
3456 subseg_new (".text", get_absolute_expression () + 1000);
3457 else
3458 subseg_new (".data", get_absolute_expression ());
3459 }
3460
3461 static void
3462 obj_coff_bss (ignore)
3463 int ignore;
3464 {
3465 if (*input_line_pointer == '\n') /* .bss */
3466 subseg_new(".bss", get_absolute_expression());
3467 else /* .bss id,expr */
3468 obj_coff_lcomm(0);
3469 }
3470
3471 static void
3472 obj_coff_ident (ignore)
3473 int ignore;
3474 {
3475 segT current_seg = now_seg; /* save current seg */
3476 subsegT current_subseg = now_subseg;
3477 subseg_new (".comment", 0); /* .comment seg */
3478 stringer (1); /* read string */
3479 subseg_set (current_seg, current_subseg); /* restore current seg */
3480 }
3481
3482 void
3483 c_symbol_merge (debug, normal)
3484 symbolS *debug;
3485 symbolS *normal;
3486 {
3487 S_SET_DATA_TYPE (normal, S_GET_DATA_TYPE (debug));
3488 S_SET_STORAGE_CLASS (normal, S_GET_STORAGE_CLASS (debug));
3489
3490 if (S_GET_NUMBER_AUXILIARY (debug) > S_GET_NUMBER_AUXILIARY (normal))
3491 {
3492 S_SET_NUMBER_AUXILIARY (normal, S_GET_NUMBER_AUXILIARY (debug));
3493 } /* take the most we have */
3494
3495 if (S_GET_NUMBER_AUXILIARY (debug) > 0)
3496 {
3497 memcpy ((char *) &normal->sy_symbol.ost_auxent[0],
3498 (char *) &debug->sy_symbol.ost_auxent[0],
3499 (unsigned int) (S_GET_NUMBER_AUXILIARY (debug) * AUXESZ));
3500 } /* Move all the auxiliary information */
3501
3502 /* Move the debug flags. */
3503 SF_SET_DEBUG_FIELD (normal, SF_GET_DEBUG_FIELD (debug));
3504 } /* c_symbol_merge() */
3505
3506 static int
3507 c_line_new (symbol, paddr, line_number, frag)
3508 symbolS * symbol;
3509 long paddr;
3510 int line_number;
3511 fragS * frag;
3512 {
3513 struct lineno_list *new_line =
3514 (struct lineno_list *) xmalloc (sizeof (struct lineno_list));
3515
3516 segment_info_type *s = segment_info + now_seg;
3517 new_line->line.l_lnno = line_number;
3518
3519 if (line_number == 0)
3520 {
3521 last_line_symbol = symbol;
3522 new_line->line.l_addr.l_symndx = (long) symbol;
3523 }
3524 else
3525 {
3526 new_line->line.l_addr.l_paddr = paddr;
3527 }
3528
3529 new_line->frag = (char *) frag;
3530 new_line->next = (struct lineno_list *) NULL;
3531
3532
3533 if (s->lineno_list_head == (struct lineno_list *) NULL)
3534 {
3535 s->lineno_list_head = new_line;
3536 }
3537 else
3538 {
3539 s->lineno_list_tail->next = new_line;
3540 }
3541 s->lineno_list_tail = new_line;
3542 return LINESZ * s->scnhdr.s_nlnno++;
3543 }
3544
3545 void
3546 c_dot_file_symbol (filename)
3547 char *filename;
3548 {
3549 symbolS *symbolP;
3550
3551 symbolP = symbol_new (".file",
3552 SEG_DEBUG,
3553 0,
3554 &zero_address_frag);
3555
3556 S_SET_STORAGE_CLASS (symbolP, C_FILE);
3557 S_SET_NUMBER_AUXILIARY (symbolP, 1);
3558
3559 if (strlen (filename) > FILNMLEN)
3560 {
3561 /* Filename is too long to fit into an auxent,
3562 we stick it into the string table instead. We keep
3563 a linked list of the filenames we find so we can emit
3564 them later.*/
3565 struct filename_list *f = ((struct filename_list *)
3566 xmalloc (sizeof (struct filename_list)));
3567
3568 f->filename = filename;
3569 f->next = 0;
3570
3571 SA_SET_FILE_FNAME_ZEROS (symbolP, 0);
3572 SA_SET_FILE_FNAME_OFFSET (symbolP, 1);
3573
3574 if (filename_list_tail)
3575 filename_list_tail->next = f;
3576 else
3577 filename_list_head = f;
3578 filename_list_tail = f;
3579 }
3580 else
3581 {
3582 SA_SET_FILE_FNAME (symbolP, filename);
3583 }
3584 #ifndef NO_LISTING
3585 {
3586 extern int listing;
3587 if (listing)
3588 {
3589 listing_source_file (filename);
3590 }
3591
3592 }
3593
3594 #endif
3595 SF_SET_DEBUG (symbolP);
3596 S_SET_VALUE (symbolP, (valueT) previous_file_symbol);
3597
3598 previous_file_symbol = symbolP;
3599
3600 /* Make sure that the symbol is first on the symbol chain */
3601 if (symbol_rootP != symbolP)
3602 {
3603 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
3604 symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP);
3605 }
3606 } /* c_dot_file_symbol() */
3607
3608 /*
3609 * Build a 'section static' symbol.
3610 */
3611
3612 symbolS *
3613 c_section_symbol (name, idx)
3614 char *name;
3615 int idx;
3616 {
3617 symbolS *symbolP;
3618
3619 symbolP = symbol_new (name, idx,
3620 0,
3621 &zero_address_frag);
3622
3623 S_SET_STORAGE_CLASS (symbolP, C_STAT);
3624 S_SET_NUMBER_AUXILIARY (symbolP, 1);
3625
3626 SF_SET_STATICS (symbolP);
3627
3628 #ifdef TE_PE
3629 /* If the .linkonce pseudo-op was used for this section, we must
3630 store the information in the auxiliary entry for the section
3631 symbol. */
3632 if (segment_info[idx].linkonce != LINKONCE_UNSET)
3633 {
3634 int type;
3635
3636 switch (segment_info[idx].linkonce)
3637 {
3638 default:
3639 abort ();
3640 case LINKONCE_DISCARD:
3641 type = IMAGE_COMDAT_SELECT_ANY;
3642 break;
3643 case LINKONCE_ONE_ONLY:
3644 type = IMAGE_COMDAT_SELECT_NODUPLICATES;
3645 break;
3646 case LINKONCE_SAME_SIZE:
3647 type = IMAGE_COMDAT_SELECT_SAME_SIZE;
3648 break;
3649 case LINKONCE_SAME_CONTENTS:
3650 type = IMAGE_COMDAT_SELECT_EXACT_MATCH;
3651 break;
3652 }
3653
3654 SYM_AUXENT (symbolP)->x_scn.x_comdat = type;
3655 }
3656 #endif /* TE_PE */
3657
3658 return symbolP;
3659 } /* c_section_symbol() */
3660
3661 static void
3662 w_symbols (abfd, where, symbol_rootP)
3663 bfd * abfd;
3664 char *where;
3665 symbolS * symbol_rootP;
3666 {
3667 symbolS *symbolP;
3668 unsigned int i;
3669
3670 /* First fill in those values we have only just worked out */
3671 for (i = SEG_E0; i < SEG_LAST; i++)
3672 {
3673 symbolP = segment_info[i].dot;
3674 if (symbolP)
3675 {
3676 SA_SET_SCN_SCNLEN (symbolP, segment_info[i].scnhdr.s_size);
3677 SA_SET_SCN_NRELOC (symbolP, segment_info[i].scnhdr.s_nreloc);
3678 SA_SET_SCN_NLINNO (symbolP, segment_info[i].scnhdr.s_nlnno);
3679 }
3680 }
3681
3682 /*
3683 * Emit all symbols left in the symbol chain.
3684 */
3685 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
3686 {
3687 /* Used to save the offset of the name. It is used to point
3688 to the string in memory but must be a file offset. */
3689 register char *temp;
3690
3691 /* We can't fix the lnnoptr field in yank_symbols with the other
3692 adjustments, because we have to wait until we know where they
3693 go in the file. */
3694 if (SF_GET_ADJ_LNNOPTR (symbolP))
3695 {
3696 SA_GET_SYM_LNNOPTR (symbolP) +=
3697 segment_info[S_GET_SEGMENT (symbolP)].scnhdr.s_lnnoptr;
3698 }
3699
3700 tc_coff_symbol_emit_hook (symbolP);
3701
3702 temp = S_GET_NAME (symbolP);
3703 if (SF_GET_STRING (symbolP))
3704 {
3705 S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
3706 S_SET_ZEROES (symbolP, 0);
3707 }
3708 else
3709 {
3710 memset (symbolP->sy_symbol.ost_entry.n_name, 0, SYMNMLEN);
3711 strncpy (symbolP->sy_symbol.ost_entry.n_name, temp, SYMNMLEN);
3712 }
3713 where = symbol_to_chars (abfd, where, symbolP);
3714 S_SET_NAME (symbolP, temp);
3715 }
3716
3717 } /* w_symbols() */
3718
3719 static void
3720 obj_coff_lcomm (ignore)
3721 int ignore;
3722 {
3723 s_lcomm(0);
3724 return;
3725 #if 0
3726 char *name;
3727 char c;
3728 int temp;
3729 char *p;
3730
3731 symbolS *symbolP;
3732
3733 name = input_line_pointer;
3734
3735 c = get_symbol_end ();
3736 p = input_line_pointer;
3737 *p = c;
3738 SKIP_WHITESPACE ();
3739 if (*input_line_pointer != ',')
3740 {
3741 as_bad ("Expected comma after name");
3742 ignore_rest_of_line ();
3743 return;
3744 }
3745 if (*input_line_pointer == '\n')
3746 {
3747 as_bad ("Missing size expression");
3748 return;
3749 }
3750 input_line_pointer++;
3751 if ((temp = get_absolute_expression ()) < 0)
3752 {
3753 as_warn ("lcomm length (%d.) <0! Ignored.", temp);
3754 ignore_rest_of_line ();
3755 return;
3756 }
3757 *p = 0;
3758
3759 symbolP = symbol_find_or_make(name);
3760
3761 if (S_GET_SEGMENT(symbolP) == SEG_UNKNOWN &&
3762 S_GET_VALUE(symbolP) == 0)
3763 {
3764 if (! need_pass_2)
3765 {
3766 char *p;
3767 segT current_seg = now_seg; /* save current seg */
3768 subsegT current_subseg = now_subseg;
3769
3770 subseg_set (SEG_E2, 1);
3771 symbolP->sy_frag = frag_now;
3772 p = frag_var(rs_org, 1, 1, (relax_substateT)0, symbolP,
3773 temp, (char *)0);
3774 *p = 0;
3775 subseg_set (current_seg, current_subseg); /* restore current seg */
3776 S_SET_SEGMENT(symbolP, SEG_E2);
3777 S_SET_STORAGE_CLASS(symbolP, C_STAT);
3778 }
3779 }
3780 else
3781 as_bad("Symbol %s already defined", name);
3782
3783 demand_empty_rest_of_line();
3784 #endif
3785 }
3786
3787 static void
3788 fixup_mdeps (frags, h, this_segment)
3789 fragS * frags;
3790 object_headers * h;
3791 segT this_segment;
3792 {
3793 subseg_change (this_segment, 0);
3794 while (frags)
3795 {
3796 switch (frags->fr_type)
3797 {
3798 case rs_align:
3799 case rs_align_code:
3800 case rs_org:
3801 #ifdef HANDLE_ALIGN
3802 HANDLE_ALIGN (frags);
3803 #endif
3804 frags->fr_type = rs_fill;
3805 frags->fr_offset =
3806 ((frags->fr_next->fr_address - frags->fr_address - frags->fr_fix)
3807 / frags->fr_var);
3808 break;
3809 case rs_machine_dependent:
3810 md_convert_frag (h, this_segment, frags);
3811 frag_wane (frags);
3812 break;
3813 default:
3814 ;
3815 }
3816 frags = frags->fr_next;
3817 }
3818 }
3819
3820 #if 1
3821
3822 #ifndef TC_FORCE_RELOCATION
3823 #define TC_FORCE_RELOCATION(fix) 0
3824 #endif
3825
3826 static void
3827 fixup_segment (segP, this_segment_type)
3828 segment_info_type * segP;
3829 segT this_segment_type;
3830 {
3831 register fixS * fixP;
3832 register symbolS *add_symbolP;
3833 register symbolS *sub_symbolP;
3834 long add_number;
3835 register int size;
3836 register char *place;
3837 register long where;
3838 register char pcrel;
3839 register fragS *fragP;
3840 register segT add_symbol_segment = absolute_section;
3841
3842 for (fixP = segP->fix_root; fixP; fixP = fixP->fx_next)
3843 {
3844 fragP = fixP->fx_frag;
3845 know (fragP);
3846 where = fixP->fx_where;
3847 place = fragP->fr_literal + where;
3848 size = fixP->fx_size;
3849 add_symbolP = fixP->fx_addsy;
3850 sub_symbolP = fixP->fx_subsy;
3851 add_number = fixP->fx_offset;
3852 pcrel = fixP->fx_pcrel;
3853
3854 /* We want function-relative stabs to work on systems which
3855 may use a relaxing linker; thus we must handle the sym1-sym2
3856 fixups function-relative stabs generates.
3857
3858 Of course, if you actually enable relaxing in the linker, the
3859 line and block scoping information is going to be incorrect
3860 in some cases. The only way to really fix this is to support
3861 a reloc involving the difference of two symbols. */
3862 if (linkrelax
3863 && (!sub_symbolP || pcrel))
3864 continue;
3865
3866 #ifdef TC_I960
3867 if (fixP->fx_tcbit && SF_GET_CALLNAME (add_symbolP))
3868 {
3869 /* Relocation should be done via the associated 'bal' entry
3870 point symbol. */
3871
3872 if (!SF_GET_BALNAME (tc_get_bal_of_call (add_symbolP)))
3873 {
3874 as_bad_where (fixP->fx_file, fixP->fx_line,
3875 "No 'bal' entry point for leafproc %s",
3876 S_GET_NAME (add_symbolP));
3877 continue;
3878 }
3879 fixP->fx_addsy = add_symbolP = tc_get_bal_of_call (add_symbolP);
3880 }
3881 #endif
3882
3883 /* Make sure the symbols have been resolved; this may not have
3884 happened if these are expression symbols. */
3885 if (add_symbolP != NULL && ! add_symbolP->sy_resolved)
3886 resolve_symbol_value (add_symbolP);
3887
3888 if (add_symbolP != NULL)
3889 {
3890 /* If this fixup is against a symbol which has been equated
3891 to another symbol, convert it to the other symbol. */
3892 if (add_symbolP->sy_value.X_op == O_symbol
3893 && (! S_IS_DEFINED (add_symbolP)
3894 || S_IS_COMMON (add_symbolP)))
3895 {
3896 while (add_symbolP->sy_value.X_op == O_symbol
3897 && (! S_IS_DEFINED (add_symbolP)
3898 || S_IS_COMMON (add_symbolP)))
3899 {
3900 symbolS *n;
3901
3902 /* We must avoid looping, as that can occur with a
3903 badly written program. */
3904 n = add_symbolP->sy_value.X_add_symbol;
3905 if (n == add_symbolP)
3906 break;
3907 add_number += add_symbolP->sy_value.X_add_number;
3908 add_symbolP = n;
3909 }
3910 fixP->fx_addsy = add_symbolP;
3911 fixP->fx_offset = add_number;
3912 }
3913 }
3914
3915 if (sub_symbolP != NULL && ! sub_symbolP->sy_resolved)
3916 resolve_symbol_value (sub_symbolP);
3917
3918 if (add_symbolP != NULL
3919 && add_symbolP->sy_mri_common)
3920 {
3921 know (add_symbolP->sy_value.X_op == O_symbol);
3922 add_number += S_GET_VALUE (add_symbolP);
3923 fixP->fx_offset = add_number;
3924 add_symbolP = fixP->fx_addsy = add_symbolP->sy_value.X_add_symbol;
3925 }
3926
3927 if (add_symbolP)
3928 {
3929 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
3930 } /* if there is an addend */
3931
3932 if (sub_symbolP)
3933 {
3934 if (add_symbolP == NULL || add_symbol_segment == absolute_section)
3935 {
3936 if (add_symbolP != NULL)
3937 {
3938 add_number += S_GET_VALUE (add_symbolP);
3939 add_symbolP = NULL;
3940 fixP->fx_addsy = NULL;
3941 }
3942
3943 /* It's just -sym. */
3944 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
3945 {
3946 add_number -= S_GET_VALUE (sub_symbolP);
3947 fixP->fx_subsy = 0;
3948 fixP->fx_done = 1;
3949 }
3950 else
3951 {
3952 #ifndef TC_M68K
3953 as_bad_where (fixP->fx_file, fixP->fx_line,
3954 "Negative of non-absolute symbol %s",
3955 S_GET_NAME (sub_symbolP));
3956 #endif
3957 add_number -= S_GET_VALUE (sub_symbolP);
3958 } /* not absolute */
3959
3960 /* if sub_symbol is in the same segment that add_symbol
3961 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
3962 }
3963 else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
3964 && SEG_NORMAL (add_symbol_segment))
3965 {
3966 /* Difference of 2 symbols from same segment. Can't
3967 make difference of 2 undefineds: 'value' means
3968 something different for N_UNDF. */
3969 #ifdef TC_I960
3970 /* Makes no sense to use the difference of 2 arbitrary symbols
3971 as the target of a call instruction. */
3972 if (fixP->fx_tcbit)
3973 {
3974 as_bad_where (fixP->fx_file, fixP->fx_line,
3975 "callj to difference of 2 symbols");
3976 }
3977 #endif /* TC_I960 */
3978 add_number += S_GET_VALUE (add_symbolP) -
3979 S_GET_VALUE (sub_symbolP);
3980 add_symbolP = NULL;
3981
3982 if (!TC_FORCE_RELOCATION (fixP))
3983 {
3984 fixP->fx_addsy = NULL;
3985 fixP->fx_subsy = NULL;
3986 fixP->fx_done = 1;
3987 #ifdef TC_M68K /* is this right? */
3988 pcrel = 0;
3989 fixP->fx_pcrel = 0;
3990 #endif
3991 }
3992 }
3993 else
3994 {
3995 /* Different segments in subtraction. */
3996 know (!(S_IS_EXTERNAL (sub_symbolP) && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
3997
3998 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
3999 {
4000 add_number -= S_GET_VALUE (sub_symbolP);
4001 }
4002 #ifdef DIFF_EXPR_OK
4003 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
4004 #if 0 /* Okay for 68k, at least... */
4005 && !pcrel
4006 #endif
4007 )
4008 {
4009 /* Make it pc-relative. */
4010 add_number += (md_pcrel_from (fixP)
4011 - S_GET_VALUE (sub_symbolP));
4012 pcrel = 1;
4013 fixP->fx_pcrel = 1;
4014 sub_symbolP = 0;
4015 fixP->fx_subsy = 0;
4016 }
4017 #endif
4018 else
4019 {
4020 as_bad_where (fixP->fx_file, fixP->fx_line,
4021 "Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %ld.",
4022 segment_name (S_GET_SEGMENT (sub_symbolP)),
4023 S_GET_NAME (sub_symbolP),
4024 (long) (fragP->fr_address + where));
4025 } /* if absolute */
4026 }
4027 } /* if sub_symbolP */
4028
4029 if (add_symbolP)
4030 {
4031 if (add_symbol_segment == this_segment_type && pcrel)
4032 {
4033 /*
4034 * This fixup was made when the symbol's segment was
4035 * SEG_UNKNOWN, but it is now in the local segment.
4036 * So we know how to do the address without relocation.
4037 */
4038 #ifdef TC_I960
4039 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
4040 * in which cases it modifies *fixP as appropriate. In the case
4041 * of a 'calls', no further work is required, and *fixP has been
4042 * set up to make the rest of the code below a no-op.
4043 */
4044 reloc_callj (fixP);
4045 #endif /* TC_I960 */
4046
4047 add_number += S_GET_VALUE (add_symbolP);
4048 add_number -= md_pcrel_from (fixP);
4049 #if defined (TC_I386) || defined (TE_LYNX)
4050 /* On the 386 we must adjust by the segment vaddr as
4051 well. Ian Taylor. */
4052 add_number -= segP->scnhdr.s_vaddr;
4053 #endif
4054 pcrel = 0; /* Lie. Don't want further pcrel processing. */
4055 if (!TC_FORCE_RELOCATION (fixP))
4056 {
4057 fixP->fx_addsy = NULL;
4058 fixP->fx_done = 1;
4059 }
4060 }
4061 else
4062 {
4063 switch (add_symbol_segment)
4064 {
4065 case absolute_section:
4066 #ifdef TC_I960
4067 reloc_callj (fixP); /* See comment about reloc_callj() above*/
4068 #endif /* TC_I960 */
4069 add_number += S_GET_VALUE (add_symbolP);
4070 add_symbolP = NULL;
4071
4072 if (!TC_FORCE_RELOCATION (fixP))
4073 {
4074 fixP->fx_addsy = NULL;
4075 fixP->fx_done = 1;
4076 }
4077 break;
4078 default:
4079
4080
4081 #if defined(TC_A29K) || (defined(TE_PE) && defined(TC_I386)) || defined(TC_M88K)
4082 /* This really should be handled in the linker, but
4083 backward compatibility forbids. */
4084 add_number += S_GET_VALUE (add_symbolP);
4085 #else
4086 add_number += S_GET_VALUE (add_symbolP) +
4087 segment_info[S_GET_SEGMENT (add_symbolP)].scnhdr.s_paddr;
4088 #endif
4089 break;
4090
4091 case SEG_UNKNOWN:
4092 #ifdef TC_I960
4093 if ((int) fixP->fx_bit_fixP == 13)
4094 {
4095 /* This is a COBR instruction. They have only a
4096 * 13-bit displacement and are only to be used
4097 * for local branches: flag as error, don't generate
4098 * relocation.
4099 */
4100 as_bad_where (fixP->fx_file, fixP->fx_line,
4101 "can't use COBR format with external label");
4102 fixP->fx_addsy = NULL;
4103 fixP->fx_done = 1;
4104 continue;
4105 } /* COBR */
4106 #endif /* TC_I960 */
4107 #if ((defined (TC_I386) || defined (TE_LYNX) || defined (TE_AUX)) && !defined(TE_PE)) || defined (COFF_COMMON_ADDEND)
4108 /* 386 COFF uses a peculiar format in which the
4109 value of a common symbol is stored in the .text
4110 segment (I've checked this on SVR3.2 and SCO
4111 3.2.2) Ian Taylor <ian@cygnus.com>. */
4112 /* This is also true for 68k COFF on sysv machines
4113 (Checked on Motorola sysv68 R3V6 and R3V7.1, and also on
4114 UNIX System V/M68000, Release 1.0 from ATT/Bell Labs)
4115 Philippe De Muyter <phdm@info.ucl.ac.be>. */
4116 if (S_IS_COMMON (add_symbolP))
4117 add_number += S_GET_VALUE (add_symbolP);
4118 #endif
4119 break;
4120
4121
4122 } /* switch on symbol seg */
4123 } /* if not in local seg */
4124 } /* if there was a + symbol */
4125
4126 if (pcrel)
4127 {
4128 #if !defined(TC_M88K) && !(defined(TE_PE) && defined(TC_I386)) && !defined(TC_A29K)
4129 /* This adjustment is not correct on the m88k, for which the
4130 linker does all the computation. */
4131 add_number -= md_pcrel_from (fixP);
4132 #endif
4133 if (add_symbolP == 0)
4134 {
4135 fixP->fx_addsy = &abs_symbol;
4136 } /* if there's an add_symbol */
4137 #if defined (TC_I386) || defined (TE_LYNX) || defined (TC_I960) || defined (TC_M68K)
4138 /* On the 386 we must adjust by the segment vaddr as well.
4139 Ian Taylor.
4140
4141 I changed the i960 to work this way as well. This is
4142 compatible with the current GNU linker behaviour. I do
4143 not know what other i960 COFF assemblers do. This is not
4144 a common case: normally, only assembler code will contain
4145 a PC relative reloc, and only branches which do not
4146 originate in the .text section will have a non-zero
4147 address.
4148
4149 I changed the m68k to work this way as well. This will
4150 break existing PC relative relocs from sections which do
4151 not start at address 0, but it will make ld -r work.
4152 Ian Taylor, 4 Oct 96. */
4153
4154 add_number -= segP->scnhdr.s_vaddr;
4155 #endif
4156 } /* if pcrel */
4157
4158 if (!fixP->fx_bit_fixP && ! fixP->fx_no_overflow)
4159 {
4160 #ifndef TC_M88K
4161 /* The m88k uses the offset field of the reloc to get around
4162 this problem. */
4163 if ((size == 1
4164 && ((add_number & ~0xFF)
4165 || (fixP->fx_signed && (add_number & 0x80)))
4166 && ((add_number & ~0xFF) != (-1 & ~0xFF)
4167 || (add_number & 0x80) == 0))
4168 || (size == 2
4169 && ((add_number & ~0xFFFF)
4170 || (fixP->fx_signed && (add_number & 0x8000)))
4171 && ((add_number & ~0xFFFF) != (-1 & ~0xFFFF)
4172 || (add_number & 0x8000) == 0)))
4173 {
4174 as_bad_where (fixP->fx_file, fixP->fx_line,
4175 "Value of %ld too large for field of %d bytes at 0x%lx",
4176 (long) add_number, size,
4177 (unsigned long) (fragP->fr_address + where));
4178 }
4179 #endif
4180 #ifdef WARN_SIGNED_OVERFLOW_WORD
4181 /* Warn if a .word value is too large when treated as a
4182 signed number. We already know it is not too negative.
4183 This is to catch over-large switches generated by gcc on
4184 the 68k. */
4185 if (!flag_signed_overflow_ok
4186 && size == 2
4187 && add_number > 0x7fff)
4188 as_bad_where (fixP->fx_file, fixP->fx_line,
4189 "Signed .word overflow; switch may be too large; %ld at 0x%lx",
4190 (long) add_number,
4191 (unsigned long) (fragP->fr_address + where));
4192 #endif
4193 } /* not a bit fix */
4194 /* Once this fix has been applied, we don't have to output
4195 anything nothing more need be done. */
4196 #ifdef MD_APPLY_FIX3
4197 md_apply_fix3 (fixP, &add_number, this_segment_type);
4198 #else
4199 md_apply_fix (fixP, add_number);
4200 #endif
4201 } /* For each fixS in this segment. */
4202 } /* fixup_segment() */
4203
4204 #endif
4205
4206 /* The first entry in a .stab section is special. */
4207
4208 void
4209 obj_coff_init_stab_section (seg)
4210 segT seg;
4211 {
4212 char *file;
4213 char *p;
4214 char *stabstr_name;
4215 unsigned int stroff;
4216
4217 /* Make space for this first symbol. */
4218 p = frag_more (12);
4219 /* Zero it out. */
4220 memset (p, 0, 12);
4221 as_where (&file, (unsigned int *) NULL);
4222 stabstr_name = (char *) alloca (strlen (segment_info[seg].name) + 4);
4223 strcpy (stabstr_name, segment_info[seg].name);
4224 strcat (stabstr_name, "str");
4225 stroff = get_stab_string_offset (file, stabstr_name);
4226 know (stroff == 1);
4227 md_number_to_chars (p, stroff, 4);
4228 }
4229
4230 /* Fill in the counts in the first entry in a .stab section. */
4231
4232 static void
4233 adjust_stab_section(abfd, seg)
4234 bfd *abfd;
4235 segT seg;
4236 {
4237 segT stabstrseg = SEG_UNKNOWN;
4238 const char *secname, *name2;
4239 char *name;
4240 char *p = NULL;
4241 int i, strsz = 0, nsyms;
4242 fragS *frag = segment_info[seg].frchainP->frch_root;
4243
4244 /* Look for the associated string table section. */
4245
4246 secname = segment_info[seg].name;
4247 name = (char *) alloca (strlen (secname) + 4);
4248 strcpy (name, secname);
4249 strcat (name, "str");
4250
4251 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
4252 {
4253 name2 = segment_info[i].name;
4254 if (name2 != NULL && strncmp(name2, name, 8) == 0)
4255 {
4256 stabstrseg = i;
4257 break;
4258 }
4259 }
4260
4261 /* If we found the section, get its size. */
4262 if (stabstrseg != SEG_UNKNOWN)
4263 strsz = size_section (abfd, stabstrseg);
4264
4265 nsyms = size_section (abfd, seg) / 12 - 1;
4266
4267 /* Look for the first frag of sufficient size for the initial stab
4268 symbol, and collect a pointer to it. */
4269 while (frag && frag->fr_fix < 12)
4270 frag = frag->fr_next;
4271 assert (frag != 0);
4272 p = frag->fr_literal;
4273 assert (p != 0);
4274
4275 /* Write in the number of stab symbols and the size of the string
4276 table. */
4277 bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6);
4278 bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8);
4279 }
4280
4281 #endif /* not BFD_ASSEMBLER */
4282
4283 const pseudo_typeS obj_pseudo_table[] =
4284 {
4285 {"def", obj_coff_def, 0},
4286 {"dim", obj_coff_dim, 0},
4287 {"endef", obj_coff_endef, 0},
4288 {"line", obj_coff_line, 0},
4289 {"ln", obj_coff_ln, 0},
4290 {"appline", obj_coff_ln, 1},
4291 {"scl", obj_coff_scl, 0},
4292 {"size", obj_coff_size, 0},
4293 {"tag", obj_coff_tag, 0},
4294 {"type", obj_coff_type, 0},
4295 {"val", obj_coff_val, 0},
4296 {"section", obj_coff_section, 0},
4297 {"sect", obj_coff_section, 0},
4298 /* FIXME: We ignore the MRI short attribute. */
4299 {"section.s", obj_coff_section, 0},
4300 {"sect.s", obj_coff_section, 0},
4301 #ifndef BFD_ASSEMBLER
4302 {"use", obj_coff_section, 0},
4303 {"text", obj_coff_text, 0},
4304 {"data", obj_coff_data, 0},
4305 {"bss", obj_coff_bss, 0},
4306 {"lcomm", obj_coff_lcomm, 0},
4307 {"ident", obj_coff_ident, 0},
4308 #else
4309 {"optim", s_ignore, 0}, /* For sun386i cc (?) */
4310 {"ident", s_ignore, 0}, /* we don't yet handle this. */
4311 #endif
4312 {"version", s_ignore, 0},
4313 {"ABORT", s_abort, 0},
4314 #ifdef TC_M88K
4315 /* The m88k uses sdef instead of def. */
4316 {"sdef", obj_coff_def, 0},
4317 #endif
4318 {NULL} /* end sentinel */
4319 }; /* obj_pseudo_table */
4320 \f
4321 #ifdef BFD_ASSEMBLER
4322
4323 /* Support for a COFF emulation. */
4324
4325 static void
4326 coff_pop_insert ()
4327 {
4328 pop_insert (obj_pseudo_table);
4329 }
4330
4331 static int
4332 coff_sec_sym_ok_for_reloc (sec)
4333 asection *sec;
4334 {
4335 return 0;
4336 }
4337
4338 static void
4339 no_func ()
4340 {
4341 abort ();
4342 }
4343
4344 const struct format_ops coff_format_ops =
4345 {
4346 bfd_target_coff_flavour,
4347 0,
4348 1,
4349 coff_frob_symbol,
4350 coff_frob_file,
4351 no_func,
4352 0, 0,
4353 0, 0,
4354 0,
4355 #if 0
4356 obj_generate_asm_lineno,
4357 #else
4358 no_func,
4359 #endif
4360 #if 0
4361 obj_stab,
4362 #else
4363 no_func,
4364 #endif
4365 coff_sec_sym_ok_for_reloc,
4366 coff_pop_insert,
4367 #if 0
4368 obj_set_ext,
4369 #else
4370 no_func,
4371 #endif
4372 coff_obj_read_begin_hook,
4373 coff_obj_symbol_new_hook,
4374 };
4375
4376 #endif
This page took 0.115032 seconds and 5 git commands to generate.