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