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