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