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