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