* symbols.c (resolve_symbol_value): Remove "finalize" param,
[deliverable/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);
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 = NULL;
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);
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 if (line_ptr->line.l_lnno == 0)
3354 {
3355 /* Turn a pointer to a symbol into the symbols' index,
3356 provided that it has been initialised. */
3357 if (line_ptr->line.l_addr.l_symndx)
3358 line_ptr->line.l_addr.l_symndx =
3359 ((symbolS *) line_ptr->line.l_addr.l_symndx)->sy_number;
3360 }
3361 else
3362 {
3363 line_ptr->line.l_addr.l_paddr += ((struct frag *) (line_ptr->frag))->fr_address;
3364 }
3365
3366 (void) bfd_coff_swap_lineno_out (abfd, &(line_ptr->line), dst);
3367 dst++;
3368
3369 }
3370
3371 s->scnhdr.s_lnnoptr = *file_cursor;
3372
3373 bfd_write (buffer, 1, s->scnhdr.s_nlnno * LINESZ, abfd);
3374 free (buffer);
3375
3376 *file_cursor += s->scnhdr.s_nlnno * LINESZ;
3377 }
3378 }
3379 H_SET_LINENO_SIZE (h, *file_cursor - start);
3380 }
3381
3382 /* Now we run through the list of frag chains in a segment and
3383 make all the subsegment frags appear at the end of the
3384 list, as if the seg 0 was extra long */
3385
3386 static void
3387 remove_subsegs ()
3388 {
3389 unsigned int i;
3390
3391 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3392 {
3393 frchainS *head = segment_info[i].frchainP;
3394 fragS dummy;
3395 fragS *prev_frag = &dummy;
3396
3397 while (head && head->frch_seg == i)
3398 {
3399 prev_frag->fr_next = head->frch_root;
3400 prev_frag = head->frch_last;
3401 head = head->frch_next;
3402 }
3403 prev_frag->fr_next = 0;
3404 }
3405 }
3406
3407 unsigned long machine;
3408 int coff_flags;
3409 extern void
3410 write_object_file ()
3411 {
3412 int i;
3413 const char *name;
3414 struct frchain *frchain_ptr;
3415
3416 object_headers headers;
3417 unsigned long file_cursor;
3418 bfd *abfd;
3419 unsigned int addr;
3420 abfd = bfd_openw (out_file_name, TARGET_FORMAT);
3421
3422 if (abfd == 0)
3423 {
3424 as_perror (_("FATAL: Can't create %s"), out_file_name);
3425 exit (EXIT_FAILURE);
3426 }
3427 bfd_set_format (abfd, bfd_object);
3428 bfd_set_arch_mach (abfd, BFD_ARCH, machine);
3429
3430 string_byte_count = 4;
3431
3432 for (frchain_ptr = frchain_root;
3433 frchain_ptr != (struct frchain *) NULL;
3434 frchain_ptr = frchain_ptr->frch_next)
3435 {
3436 /* Run through all the sub-segments and align them up. Also
3437 close any open frags. We tack a .fill onto the end of the
3438 frag chain so that any .align's size can be worked by looking
3439 at the next frag. */
3440
3441 subseg_set (frchain_ptr->frch_seg, frchain_ptr->frch_subseg);
3442
3443 #ifndef SUB_SEGMENT_ALIGN
3444 #define SUB_SEGMENT_ALIGN(SEG) 1
3445 #endif
3446 #ifdef md_do_align
3447 md_do_align (SUB_SEGMENT_ALIGN (now_seg), (char *) NULL, 0, 0,
3448 alignment_done);
3449 #endif
3450 if (subseg_text_p (now_seg))
3451 frag_align_code (SUB_SEGMENT_ALIGN (now_seg), 0);
3452 else
3453 frag_align (SUB_SEGMENT_ALIGN (now_seg), 0, 0);
3454
3455 #ifdef md_do_align
3456 alignment_done:
3457 #endif
3458
3459 frag_wane (frag_now);
3460 frag_now->fr_fix = 0;
3461 know (frag_now->fr_next == NULL);
3462 }
3463
3464 remove_subsegs ();
3465
3466 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3467 {
3468 relax_segment (segment_info[i].frchainP->frch_root, i);
3469 }
3470
3471 H_SET_NUMBER_OF_SECTIONS (&headers, 0);
3472
3473 /* Find out how big the sections are, and set the addresses. */
3474 addr = 0;
3475 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3476 {
3477 long size;
3478
3479 segment_info[i].scnhdr.s_paddr = addr;
3480 segment_info[i].scnhdr.s_vaddr = addr;
3481
3482 if (segment_info[i].scnhdr.s_name[0])
3483 {
3484 H_SET_NUMBER_OF_SECTIONS (&headers,
3485 H_GET_NUMBER_OF_SECTIONS (&headers) + 1);
3486
3487 #ifdef COFF_LONG_SECTION_NAMES
3488 /* Support long section names as found in PE. This code
3489 must coordinate with that in coff_header_append and
3490 w_strings. */
3491 {
3492 unsigned int len;
3493
3494 len = strlen (segment_info[i].name);
3495 if (len > SCNNMLEN)
3496 string_byte_count += len + 1;
3497 }
3498 #endif /* COFF_LONG_SECTION_NAMES */
3499 }
3500
3501 size = size_section (abfd, (unsigned int) i);
3502 addr += size;
3503
3504 /* I think the section alignment is only used on the i960; the
3505 i960 needs it, and it should do no harm on other targets. */
3506 #ifdef ALIGNMENT_IN_S_FLAGS
3507 segment_info[i].scnhdr.s_flags |= (section_alignment[i] & 0xF) << 8;
3508 #else
3509 segment_info[i].scnhdr.s_align = 1 << section_alignment[i];
3510 #endif
3511
3512 if (i == SEG_E0)
3513 H_SET_TEXT_SIZE (&headers, size);
3514 else if (i == SEG_E1)
3515 H_SET_DATA_SIZE (&headers, size);
3516 else if (i == SEG_E2)
3517 H_SET_BSS_SIZE (&headers, size);
3518 }
3519
3520 /* Turn the gas native symbol table shape into a coff symbol table */
3521 crawl_symbols (&headers, abfd);
3522
3523 if (string_byte_count == 4)
3524 string_byte_count = 0;
3525
3526 H_SET_STRING_SIZE (&headers, string_byte_count);
3527
3528 #ifdef tc_frob_file
3529 tc_frob_file ();
3530 #endif
3531
3532 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3533 {
3534 fixup_mdeps (segment_info[i].frchainP->frch_root, &headers, i);
3535 fixup_segment (&segment_info[i], i);
3536 }
3537
3538 /* Look for ".stab" segments and fill in their initial symbols
3539 correctly. */
3540 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3541 {
3542 name = segment_info[i].name;
3543
3544 if (name != NULL
3545 && strncmp (".stab", name, 5) == 0
3546 && strncmp (".stabstr", name, 8) != 0)
3547 adjust_stab_section (abfd, i);
3548 }
3549
3550 file_cursor = H_GET_TEXT_FILE_OFFSET (&headers);
3551
3552 bfd_seek (abfd, (file_ptr) file_cursor, 0);
3553
3554 /* Plant the data */
3555
3556 fill_section (abfd, &headers, &file_cursor);
3557
3558 do_relocs_for (abfd, &headers, &file_cursor);
3559
3560 do_linenos_for (abfd, &headers, &file_cursor);
3561
3562 H_SET_FILE_MAGIC_NUMBER (&headers, COFF_MAGIC);
3563 #ifndef OBJ_COFF_OMIT_TIMESTAMP
3564 H_SET_TIME_STAMP (&headers, (long)time((time_t *)0));
3565 #else
3566 H_SET_TIME_STAMP (&headers, 0);
3567 #endif
3568 #ifdef TC_COFF_SET_MACHINE
3569 TC_COFF_SET_MACHINE (&headers);
3570 #endif
3571
3572 #ifndef COFF_FLAGS
3573 #define COFF_FLAGS 0
3574 #endif
3575
3576 #ifdef KEEP_RELOC_INFO
3577 H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers) ? 0 : F_LNNO) |
3578 COFF_FLAGS | coff_flags));
3579 #else
3580 H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers) ? 0 : F_LNNO) |
3581 (H_GET_RELOCATION_SIZE(&headers) ? 0 : F_RELFLG) |
3582 COFF_FLAGS | coff_flags));
3583 #endif
3584
3585 {
3586 unsigned int symtable_size = H_GET_SYMBOL_TABLE_SIZE (&headers);
3587 char *buffer1 = xmalloc (symtable_size + string_byte_count + 1);
3588
3589 H_SET_SYMBOL_TABLE_POINTER (&headers, bfd_tell (abfd));
3590 w_symbols (abfd, buffer1, symbol_rootP);
3591 if (string_byte_count > 0)
3592 w_strings (buffer1 + symtable_size);
3593 bfd_write (buffer1, 1, symtable_size + string_byte_count, abfd);
3594 free (buffer1);
3595 }
3596
3597 coff_header_append (abfd, &headers);
3598 #if 0
3599 /* Recent changes to write need this, but where it should
3600 go is up to Ken.. */
3601 if (bfd_close_all_done (abfd) == false)
3602 as_fatal (_("Can't close %s: %s"), out_file_name,
3603 bfd_errmsg (bfd_get_error ()));
3604 #else
3605 {
3606 extern bfd *stdoutput;
3607 stdoutput = abfd;
3608 }
3609 #endif
3610
3611 }
3612
3613 /* Add a new segment. This is called from subseg_new via the
3614 obj_new_segment macro. */
3615
3616 segT
3617 obj_coff_add_segment (name)
3618 const char *name;
3619 {
3620 unsigned int i;
3621
3622 #ifndef COFF_LONG_SECTION_NAMES
3623 char buf[SCNNMLEN + 1];
3624
3625 strncpy (buf, name, SCNNMLEN);
3626 buf[SCNNMLEN] = '\0';
3627 name = buf;
3628 #endif
3629
3630 for (i = SEG_E0; i < SEG_LAST && segment_info[i].scnhdr.s_name[0]; i++)
3631 if (strcmp (name, segment_info[i].name) == 0)
3632 return (segT) i;
3633
3634 if (i == SEG_LAST)
3635 {
3636 as_bad (_("Too many new sections; can't add \"%s\""), name);
3637 return now_seg;
3638 }
3639
3640 /* Add a new section. */
3641 strncpy (segment_info[i].scnhdr.s_name, name,
3642 sizeof (segment_info[i].scnhdr.s_name));
3643 segment_info[i].scnhdr.s_flags = STYP_REG;
3644 segment_info[i].name = xstrdup (name);
3645
3646 return (segT) i;
3647 }
3648
3649 /*
3650 * implement the .section pseudo op:
3651 * .section name {, "flags"}
3652 * ^ ^
3653 * | +--- optional flags: 'b' for bss
3654 * | 'i' for info
3655 * +-- section name 'l' for lib
3656 * 'n' for noload
3657 * 'o' for over
3658 * 'w' for data
3659 * 'd' (apparently m88k for data)
3660 * 'x' for text
3661 * 'r' for read-only data
3662 * But if the argument is not a quoted string, treat it as a
3663 * subsegment number.
3664 */
3665
3666 void
3667 obj_coff_section (ignore)
3668 int ignore ATTRIBUTE_UNUSED;
3669 {
3670 /* Strip out the section name */
3671 char *section_name, *name;
3672 char c;
3673 unsigned int exp;
3674 long flags;
3675
3676 if (flag_mri)
3677 {
3678 char type;
3679
3680 s_mri_sect (&type);
3681 flags = 0;
3682 if (type == 'C')
3683 flags = STYP_TEXT;
3684 else if (type == 'D')
3685 flags = STYP_DATA;
3686 segment_info[now_seg].scnhdr.s_flags |= flags;
3687
3688 return;
3689 }
3690
3691 section_name = input_line_pointer;
3692 c = get_symbol_end ();
3693
3694 name = xmalloc (input_line_pointer - section_name + 1);
3695 strcpy (name, section_name);
3696
3697 *input_line_pointer = c;
3698
3699 exp = 0;
3700 flags = 0;
3701
3702 SKIP_WHITESPACE ();
3703 if (*input_line_pointer == ',')
3704 {
3705 ++input_line_pointer;
3706 SKIP_WHITESPACE ();
3707
3708 if (*input_line_pointer != '"')
3709 exp = get_absolute_expression ();
3710 else
3711 {
3712 ++input_line_pointer;
3713 while (*input_line_pointer != '"'
3714 && ! is_end_of_line[(unsigned char) *input_line_pointer])
3715 {
3716 switch (*input_line_pointer)
3717 {
3718 case 'b': flags |= STYP_BSS; break;
3719 case 'i': flags |= STYP_INFO; break;
3720 case 'l': flags |= STYP_LIB; break;
3721 case 'n': flags |= STYP_NOLOAD; break;
3722 case 'o': flags |= STYP_OVER; break;
3723 case 'd':
3724 case 'w': flags |= STYP_DATA; break;
3725 case 'x': flags |= STYP_TEXT; break;
3726 case 'r': flags |= STYP_LIT; break;
3727 default:
3728 as_warn(_("unknown section attribute '%c'"),
3729 *input_line_pointer);
3730 break;
3731 }
3732 ++input_line_pointer;
3733 }
3734 if (*input_line_pointer == '"')
3735 ++input_line_pointer;
3736 }
3737 }
3738
3739 subseg_new (name, (subsegT) exp);
3740
3741 segment_info[now_seg].scnhdr.s_flags |= flags;
3742
3743 demand_empty_rest_of_line ();
3744 }
3745
3746 static void
3747 obj_coff_text (ignore)
3748 int ignore ATTRIBUTE_UNUSED;
3749 {
3750 subseg_new (".text", get_absolute_expression ());
3751 }
3752
3753 static void
3754 obj_coff_data (ignore)
3755 int ignore ATTRIBUTE_UNUSED;
3756 {
3757 if (flag_readonly_data_in_text)
3758 subseg_new (".text", get_absolute_expression () + 1000);
3759 else
3760 subseg_new (".data", get_absolute_expression ());
3761 }
3762
3763 static void
3764 obj_coff_ident (ignore)
3765 int ignore ATTRIBUTE_UNUSED;
3766 {
3767 segT current_seg = now_seg; /* save current seg */
3768 subsegT current_subseg = now_subseg;
3769 subseg_new (".comment", 0); /* .comment seg */
3770 stringer (1); /* read string */
3771 subseg_set (current_seg, current_subseg); /* restore current seg */
3772 }
3773
3774 void
3775 c_symbol_merge (debug, normal)
3776 symbolS *debug;
3777 symbolS *normal;
3778 {
3779 S_SET_DATA_TYPE (normal, S_GET_DATA_TYPE (debug));
3780 S_SET_STORAGE_CLASS (normal, S_GET_STORAGE_CLASS (debug));
3781
3782 if (S_GET_NUMBER_AUXILIARY (debug) > S_GET_NUMBER_AUXILIARY (normal))
3783 {
3784 S_SET_NUMBER_AUXILIARY (normal, S_GET_NUMBER_AUXILIARY (debug));
3785 } /* take the most we have */
3786
3787 if (S_GET_NUMBER_AUXILIARY (debug) > 0)
3788 {
3789 memcpy ((char *) &normal->sy_symbol.ost_auxent[0],
3790 (char *) &debug->sy_symbol.ost_auxent[0],
3791 (unsigned int) (S_GET_NUMBER_AUXILIARY (debug) * AUXESZ));
3792 } /* Move all the auxiliary information */
3793
3794 /* Move the debug flags. */
3795 SF_SET_DEBUG_FIELD (normal, SF_GET_DEBUG_FIELD (debug));
3796 } /* c_symbol_merge() */
3797
3798 static int
3799 c_line_new (symbol, paddr, line_number, frag)
3800 symbolS * symbol;
3801 long paddr;
3802 int line_number;
3803 fragS * frag;
3804 {
3805 struct lineno_list *new_line =
3806 (struct lineno_list *) xmalloc (sizeof (struct lineno_list));
3807
3808 segment_info_type *s = segment_info + now_seg;
3809 new_line->line.l_lnno = line_number;
3810
3811 if (line_number == 0)
3812 {
3813 last_line_symbol = symbol;
3814 new_line->line.l_addr.l_symndx = (long) symbol;
3815 }
3816 else
3817 {
3818 new_line->line.l_addr.l_paddr = paddr;
3819 }
3820
3821 new_line->frag = (char *) frag;
3822 new_line->next = (struct lineno_list *) NULL;
3823
3824 if (s->lineno_list_head == (struct lineno_list *) NULL)
3825 {
3826 s->lineno_list_head = new_line;
3827 }
3828 else
3829 {
3830 s->lineno_list_tail->next = new_line;
3831 }
3832 s->lineno_list_tail = new_line;
3833 return LINESZ * s->scnhdr.s_nlnno++;
3834 }
3835
3836 void
3837 c_dot_file_symbol (filename)
3838 char *filename;
3839 {
3840 symbolS *symbolP;
3841
3842 symbolP = symbol_new (".file",
3843 SEG_DEBUG,
3844 0,
3845 &zero_address_frag);
3846
3847 S_SET_STORAGE_CLASS (symbolP, C_FILE);
3848 S_SET_NUMBER_AUXILIARY (symbolP, 1);
3849
3850 if (strlen (filename) > FILNMLEN)
3851 {
3852 /* Filename is too long to fit into an auxent,
3853 we stick it into the string table instead. We keep
3854 a linked list of the filenames we find so we can emit
3855 them later.*/
3856 struct filename_list *f = ((struct filename_list *)
3857 xmalloc (sizeof (struct filename_list)));
3858
3859 f->filename = filename;
3860 f->next = 0;
3861
3862 SA_SET_FILE_FNAME_ZEROS (symbolP, 0);
3863 SA_SET_FILE_FNAME_OFFSET (symbolP, 1);
3864
3865 if (filename_list_tail)
3866 filename_list_tail->next = f;
3867 else
3868 filename_list_head = f;
3869 filename_list_tail = f;
3870 }
3871 else
3872 {
3873 SA_SET_FILE_FNAME (symbolP, filename);
3874 }
3875 #ifndef NO_LISTING
3876 {
3877 extern int listing;
3878 if (listing)
3879 {
3880 listing_source_file (filename);
3881 }
3882
3883 }
3884
3885 #endif
3886 SF_SET_DEBUG (symbolP);
3887 S_SET_VALUE (symbolP, (valueT) previous_file_symbol);
3888
3889 previous_file_symbol = symbolP;
3890
3891 /* Make sure that the symbol is first on the symbol chain */
3892 if (symbol_rootP != symbolP)
3893 {
3894 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
3895 symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP);
3896 }
3897 } /* c_dot_file_symbol() */
3898
3899 /*
3900 * Build a 'section static' symbol.
3901 */
3902
3903 symbolS *
3904 c_section_symbol (name, idx)
3905 char *name;
3906 int idx;
3907 {
3908 symbolS *symbolP;
3909
3910 symbolP = symbol_find_base (name, DO_NOT_STRIP);
3911 if (symbolP == NULL)
3912 symbolP = symbol_new (name, idx, 0, &zero_address_frag);
3913 else
3914 {
3915 /* Mmmm. I just love violating interfaces. Makes me feel...dirty. */
3916 S_SET_SEGMENT (symbolP, idx);
3917 symbolP->sy_frag = &zero_address_frag;
3918 }
3919
3920 S_SET_STORAGE_CLASS (symbolP, C_STAT);
3921 S_SET_NUMBER_AUXILIARY (symbolP, 1);
3922
3923 SF_SET_STATICS (symbolP);
3924
3925 #ifdef TE_DELTA
3926 /* manfred@s-direktnet.de: section symbols *must* have the LOCAL bit cleared,
3927 which is set by the new definition of LOCAL_LABEL in tc-m68k.h. */
3928 SF_CLEAR_LOCAL (symbolP);
3929 #endif
3930 #ifdef TE_PE
3931 /* If the .linkonce pseudo-op was used for this section, we must
3932 store the information in the auxiliary entry for the section
3933 symbol. */
3934 if (segment_info[idx].linkonce != LINKONCE_UNSET)
3935 {
3936 int type;
3937
3938 switch (segment_info[idx].linkonce)
3939 {
3940 default:
3941 abort ();
3942 case LINKONCE_DISCARD:
3943 type = IMAGE_COMDAT_SELECT_ANY;
3944 break;
3945 case LINKONCE_ONE_ONLY:
3946 type = IMAGE_COMDAT_SELECT_NODUPLICATES;
3947 break;
3948 case LINKONCE_SAME_SIZE:
3949 type = IMAGE_COMDAT_SELECT_SAME_SIZE;
3950 break;
3951 case LINKONCE_SAME_CONTENTS:
3952 type = IMAGE_COMDAT_SELECT_EXACT_MATCH;
3953 break;
3954 }
3955
3956 SYM_AUXENT (symbolP)->x_scn.x_comdat = type;
3957 }
3958 #endif /* TE_PE */
3959
3960 return symbolP;
3961 } /* c_section_symbol() */
3962
3963 static void
3964 w_symbols (abfd, where, symbol_rootP)
3965 bfd * abfd;
3966 char *where;
3967 symbolS * symbol_rootP;
3968 {
3969 symbolS *symbolP;
3970 unsigned int i;
3971
3972 /* First fill in those values we have only just worked out */
3973 for (i = SEG_E0; i < SEG_LAST; i++)
3974 {
3975 symbolP = segment_info[i].dot;
3976 if (symbolP)
3977 {
3978 SA_SET_SCN_SCNLEN (symbolP, segment_info[i].scnhdr.s_size);
3979 SA_SET_SCN_NRELOC (symbolP, segment_info[i].scnhdr.s_nreloc);
3980 SA_SET_SCN_NLINNO (symbolP, segment_info[i].scnhdr.s_nlnno);
3981 }
3982 }
3983
3984 /*
3985 * Emit all symbols left in the symbol chain.
3986 */
3987 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
3988 {
3989 /* Used to save the offset of the name. It is used to point
3990 to the string in memory but must be a file offset. */
3991 register char *temp;
3992
3993 /* We can't fix the lnnoptr field in yank_symbols with the other
3994 adjustments, because we have to wait until we know where they
3995 go in the file. */
3996 if (SF_GET_ADJ_LNNOPTR (symbolP))
3997 {
3998 SA_GET_SYM_LNNOPTR (symbolP) +=
3999 segment_info[S_GET_SEGMENT (symbolP)].scnhdr.s_lnnoptr;
4000 }
4001
4002 tc_coff_symbol_emit_hook (symbolP);
4003
4004 temp = S_GET_NAME (symbolP);
4005 if (SF_GET_STRING (symbolP))
4006 {
4007 S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
4008 S_SET_ZEROES (symbolP, 0);
4009 }
4010 else
4011 {
4012 memset (symbolP->sy_symbol.ost_entry.n_name, 0, SYMNMLEN);
4013 strncpy (symbolP->sy_symbol.ost_entry.n_name, temp, SYMNMLEN);
4014 }
4015 where = symbol_to_chars (abfd, where, symbolP);
4016 S_SET_NAME (symbolP, temp);
4017 }
4018
4019 } /* w_symbols() */
4020
4021 static void
4022 obj_coff_lcomm (ignore)
4023 int ignore ATTRIBUTE_UNUSED;
4024 {
4025 s_lcomm(0);
4026 return;
4027 #if 0
4028 char *name;
4029 char c;
4030 int temp;
4031 char *p;
4032
4033 symbolS *symbolP;
4034
4035 name = input_line_pointer;
4036
4037 c = get_symbol_end ();
4038 p = input_line_pointer;
4039 *p = c;
4040 SKIP_WHITESPACE ();
4041 if (*input_line_pointer != ',')
4042 {
4043 as_bad (_("Expected comma after name"));
4044 ignore_rest_of_line ();
4045 return;
4046 }
4047 if (*input_line_pointer == '\n')
4048 {
4049 as_bad (_("Missing size expression"));
4050 return;
4051 }
4052 input_line_pointer++;
4053 if ((temp = get_absolute_expression ()) < 0)
4054 {
4055 as_warn (_("lcomm length (%d.) <0! Ignored."), temp);
4056 ignore_rest_of_line ();
4057 return;
4058 }
4059 *p = 0;
4060
4061 symbolP = symbol_find_or_make (name);
4062
4063 if (S_GET_SEGMENT (symbolP) == SEG_UNKNOWN &&
4064 S_GET_VALUE (symbolP) == 0)
4065 {
4066 if (! need_pass_2)
4067 {
4068 char *p;
4069 segT current_seg = now_seg; /* save current seg */
4070 subsegT current_subseg = now_subseg;
4071
4072 subseg_set (SEG_E2, 1);
4073 symbolP->sy_frag = frag_now;
4074 p = frag_var(rs_org, 1, 1, (relax_substateT)0, symbolP,
4075 (offsetT) temp, (char *) 0);
4076 *p = 0;
4077 subseg_set (current_seg, current_subseg); /* restore current seg */
4078 S_SET_SEGMENT (symbolP, SEG_E2);
4079 S_SET_STORAGE_CLASS (symbolP, C_STAT);
4080 }
4081 }
4082 else
4083 as_bad (_("Symbol %s already defined"), name);
4084
4085 demand_empty_rest_of_line ();
4086 #endif
4087 }
4088
4089 static void
4090 fixup_mdeps (frags, h, this_segment)
4091 fragS * frags;
4092 object_headers * h;
4093 segT this_segment;
4094 {
4095 subseg_change (this_segment, 0);
4096 while (frags)
4097 {
4098 switch (frags->fr_type)
4099 {
4100 case rs_align:
4101 case rs_align_code:
4102 case rs_align_test:
4103 case rs_org:
4104 #ifdef HANDLE_ALIGN
4105 HANDLE_ALIGN (frags);
4106 #endif
4107 frags->fr_type = rs_fill;
4108 frags->fr_offset =
4109 ((frags->fr_next->fr_address - frags->fr_address - frags->fr_fix)
4110 / frags->fr_var);
4111 break;
4112 case rs_machine_dependent:
4113 md_convert_frag (h, this_segment, frags);
4114 frag_wane (frags);
4115 break;
4116 default:
4117 ;
4118 }
4119 frags = frags->fr_next;
4120 }
4121 }
4122
4123 #if 1
4124
4125 #ifndef TC_FORCE_RELOCATION
4126 #define TC_FORCE_RELOCATION(fix) 0
4127 #endif
4128
4129 static void
4130 fixup_segment (segP, this_segment_type)
4131 segment_info_type * segP;
4132 segT this_segment_type;
4133 {
4134 register fixS * fixP;
4135 register symbolS *add_symbolP;
4136 register symbolS *sub_symbolP;
4137 long add_number;
4138 register int size;
4139 register char *place;
4140 register long where;
4141 register char pcrel;
4142 register fragS *fragP;
4143 register segT add_symbol_segment = absolute_section;
4144
4145 for (fixP = segP->fix_root; fixP; fixP = fixP->fx_next)
4146 {
4147 fragP = fixP->fx_frag;
4148 know (fragP);
4149 where = fixP->fx_where;
4150 place = fragP->fr_literal + where;
4151 size = fixP->fx_size;
4152 add_symbolP = fixP->fx_addsy;
4153 sub_symbolP = fixP->fx_subsy;
4154 add_number = fixP->fx_offset;
4155 pcrel = fixP->fx_pcrel;
4156
4157 /* We want function-relative stabs to work on systems which
4158 may use a relaxing linker; thus we must handle the sym1-sym2
4159 fixups function-relative stabs generates.
4160
4161 Of course, if you actually enable relaxing in the linker, the
4162 line and block scoping information is going to be incorrect
4163 in some cases. The only way to really fix this is to support
4164 a reloc involving the difference of two symbols. */
4165 if (linkrelax
4166 && (!sub_symbolP || pcrel))
4167 continue;
4168
4169 #ifdef TC_I960
4170 if (fixP->fx_tcbit && SF_GET_CALLNAME (add_symbolP))
4171 {
4172 /* Relocation should be done via the associated 'bal' entry
4173 point symbol. */
4174
4175 if (!SF_GET_BALNAME (tc_get_bal_of_call (add_symbolP)))
4176 {
4177 as_bad_where (fixP->fx_file, fixP->fx_line,
4178 _("No 'bal' entry point for leafproc %s"),
4179 S_GET_NAME (add_symbolP));
4180 continue;
4181 }
4182 fixP->fx_addsy = add_symbolP = tc_get_bal_of_call (add_symbolP);
4183 }
4184 #endif
4185
4186 /* Make sure the symbols have been resolved; this may not have
4187 happened if these are expression symbols. */
4188 if (add_symbolP != NULL && ! add_symbolP->sy_resolved)
4189 resolve_symbol_value (add_symbolP);
4190
4191 if (add_symbolP != NULL)
4192 {
4193 /* If this fixup is against a symbol which has been equated
4194 to another symbol, convert it to the other symbol. */
4195 if (add_symbolP->sy_value.X_op == O_symbol
4196 && (! S_IS_DEFINED (add_symbolP)
4197 || S_IS_COMMON (add_symbolP)))
4198 {
4199 while (add_symbolP->sy_value.X_op == O_symbol
4200 && (! S_IS_DEFINED (add_symbolP)
4201 || S_IS_COMMON (add_symbolP)))
4202 {
4203 symbolS *n;
4204
4205 /* We must avoid looping, as that can occur with a
4206 badly written program. */
4207 n = add_symbolP->sy_value.X_add_symbol;
4208 if (n == add_symbolP)
4209 break;
4210 add_number += add_symbolP->sy_value.X_add_number;
4211 add_symbolP = n;
4212 }
4213 fixP->fx_addsy = add_symbolP;
4214 fixP->fx_offset = add_number;
4215 }
4216 }
4217
4218 if (sub_symbolP != NULL && ! sub_symbolP->sy_resolved)
4219 resolve_symbol_value (sub_symbolP);
4220
4221 if (add_symbolP != NULL
4222 && add_symbolP->sy_mri_common)
4223 {
4224 know (add_symbolP->sy_value.X_op == O_symbol);
4225 add_number += S_GET_VALUE (add_symbolP);
4226 fixP->fx_offset = add_number;
4227 add_symbolP = fixP->fx_addsy = add_symbolP->sy_value.X_add_symbol;
4228 }
4229
4230 if (add_symbolP)
4231 {
4232 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
4233 } /* if there is an addend */
4234
4235 if (sub_symbolP)
4236 {
4237 if (add_symbolP == NULL || add_symbol_segment == absolute_section)
4238 {
4239 if (add_symbolP != NULL)
4240 {
4241 add_number += S_GET_VALUE (add_symbolP);
4242 add_symbolP = NULL;
4243 fixP->fx_addsy = NULL;
4244 }
4245
4246 /* It's just -sym. */
4247 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
4248 {
4249 add_number -= S_GET_VALUE (sub_symbolP);
4250 fixP->fx_subsy = 0;
4251 fixP->fx_done = 1;
4252 }
4253 else
4254 {
4255 #ifndef TC_M68K
4256 as_bad_where (fixP->fx_file, fixP->fx_line,
4257 _("Negative of non-absolute symbol %s"),
4258 S_GET_NAME (sub_symbolP));
4259 #endif
4260 add_number -= S_GET_VALUE (sub_symbolP);
4261 } /* not absolute */
4262
4263 /* if sub_symbol is in the same segment that add_symbol
4264 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
4265 }
4266 else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
4267 && SEG_NORMAL (add_symbol_segment))
4268 {
4269 /* Difference of 2 symbols from same segment. Can't
4270 make difference of 2 undefineds: 'value' means
4271 something different for N_UNDF. */
4272 #ifdef TC_I960
4273 /* Makes no sense to use the difference of 2 arbitrary symbols
4274 as the target of a call instruction. */
4275 if (fixP->fx_tcbit)
4276 {
4277 as_bad_where (fixP->fx_file, fixP->fx_line,
4278 _("callj to difference of 2 symbols"));
4279 }
4280 #endif /* TC_I960 */
4281 add_number += S_GET_VALUE (add_symbolP) -
4282 S_GET_VALUE (sub_symbolP);
4283 add_symbolP = NULL;
4284
4285 if (!TC_FORCE_RELOCATION (fixP))
4286 {
4287 fixP->fx_addsy = NULL;
4288 fixP->fx_subsy = NULL;
4289 fixP->fx_done = 1;
4290 #ifdef TC_M68K /* is this right? */
4291 pcrel = 0;
4292 fixP->fx_pcrel = 0;
4293 #endif
4294 }
4295 }
4296 else
4297 {
4298 /* Different segments in subtraction. */
4299 know (!(S_IS_EXTERNAL (sub_symbolP) && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
4300
4301 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
4302 {
4303 add_number -= S_GET_VALUE (sub_symbolP);
4304 }
4305 #ifdef DIFF_EXPR_OK
4306 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
4307 #if 0 /* Okay for 68k, at least... */
4308 && !pcrel
4309 #endif
4310 )
4311 {
4312 /* Make it pc-relative. */
4313 add_number += (md_pcrel_from (fixP)
4314 - S_GET_VALUE (sub_symbolP));
4315 pcrel = 1;
4316 fixP->fx_pcrel = 1;
4317 sub_symbolP = 0;
4318 fixP->fx_subsy = 0;
4319 }
4320 #endif
4321 else
4322 {
4323 as_bad_where (fixP->fx_file, fixP->fx_line,
4324 _("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %ld."),
4325 segment_name (S_GET_SEGMENT (sub_symbolP)),
4326 S_GET_NAME (sub_symbolP),
4327 (long) (fragP->fr_address + where));
4328 } /* if absolute */
4329 }
4330 } /* if sub_symbolP */
4331
4332 if (add_symbolP)
4333 {
4334 if (add_symbol_segment == this_segment_type && pcrel)
4335 {
4336 /*
4337 * This fixup was made when the symbol's segment was
4338 * SEG_UNKNOWN, but it is now in the local segment.
4339 * So we know how to do the address without relocation.
4340 */
4341 #ifdef TC_I960
4342 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
4343 * in which cases it modifies *fixP as appropriate. In the case
4344 * of a 'calls', no further work is required, and *fixP has been
4345 * set up to make the rest of the code below a no-op.
4346 */
4347 reloc_callj (fixP);
4348 #endif /* TC_I960 */
4349
4350 add_number += S_GET_VALUE (add_symbolP);
4351 add_number -= md_pcrel_from (fixP);
4352
4353 /* We used to do
4354 add_number -= segP->scnhdr.s_vaddr;
4355 if defined (TC_I386) || defined (TE_LYNX). I now
4356 think that was an error propagated from the case when
4357 we are going to emit the relocation. If we are not
4358 going to emit the relocation, then we just want to
4359 set add_number to the difference between the symbols.
4360 This is a case that would only arise when there is a
4361 PC relative reference from a section other than .text
4362 to a symbol defined in the same section, and the
4363 reference is not relaxed. Since jump instructions on
4364 the i386 are relaxed, this could only arise with a
4365 call instruction. */
4366
4367 pcrel = 0; /* Lie. Don't want further pcrel processing. */
4368 if (!TC_FORCE_RELOCATION (fixP))
4369 {
4370 fixP->fx_addsy = NULL;
4371 fixP->fx_done = 1;
4372 }
4373 }
4374 else
4375 {
4376 switch (add_symbol_segment)
4377 {
4378 case absolute_section:
4379 #ifdef TC_I960
4380 reloc_callj (fixP); /* See comment about reloc_callj() above*/
4381 #endif /* TC_I960 */
4382 add_number += S_GET_VALUE (add_symbolP);
4383 add_symbolP = NULL;
4384
4385 if (!TC_FORCE_RELOCATION (fixP))
4386 {
4387 fixP->fx_addsy = NULL;
4388 fixP->fx_done = 1;
4389 }
4390 break;
4391 default:
4392
4393 #if defined(TC_A29K) || (defined(TE_PE) && defined(TC_I386)) || defined(TC_M88K)
4394 /* This really should be handled in the linker, but
4395 backward compatibility forbids. */
4396 add_number += S_GET_VALUE (add_symbolP);
4397 #else
4398 add_number += S_GET_VALUE (add_symbolP) +
4399 segment_info[S_GET_SEGMENT (add_symbolP)].scnhdr.s_paddr;
4400 #endif
4401 break;
4402
4403 case SEG_UNKNOWN:
4404 #ifdef TC_I960
4405 if ((int) fixP->fx_bit_fixP == 13)
4406 {
4407 /* This is a COBR instruction. They have only a
4408 * 13-bit displacement and are only to be used
4409 * for local branches: flag as error, don't generate
4410 * relocation.
4411 */
4412 as_bad_where (fixP->fx_file, fixP->fx_line,
4413 _("can't use COBR format with external label"));
4414 fixP->fx_addsy = NULL;
4415 fixP->fx_done = 1;
4416 continue;
4417 } /* COBR */
4418 #endif /* TC_I960 */
4419 #if ((defined (TC_I386) || defined (TE_LYNX) || defined (TE_AUX)) && !defined(TE_PE)) || defined (COFF_COMMON_ADDEND)
4420 /* 386 COFF uses a peculiar format in which the
4421 value of a common symbol is stored in the .text
4422 segment (I've checked this on SVR3.2 and SCO
4423 3.2.2) Ian Taylor <ian@cygnus.com>. */
4424 /* This is also true for 68k COFF on sysv machines
4425 (Checked on Motorola sysv68 R3V6 and R3V7.1, and also on
4426 UNIX System V/M68000, Release 1.0 from ATT/Bell Labs)
4427 Philippe De Muyter <phdm@info.ucl.ac.be>. */
4428 if (S_IS_COMMON (add_symbolP))
4429 add_number += S_GET_VALUE (add_symbolP);
4430 #endif
4431 break;
4432
4433 } /* switch on symbol seg */
4434 } /* if not in local seg */
4435 } /* if there was a + symbol */
4436
4437 if (pcrel)
4438 {
4439 #if !defined(TC_M88K) && !(defined(TE_PE) && defined(TC_I386)) && !defined(TC_A29K)
4440 /* This adjustment is not correct on the m88k, for which the
4441 linker does all the computation. */
4442 add_number -= md_pcrel_from (fixP);
4443 #endif
4444 if (add_symbolP == 0)
4445 {
4446 fixP->fx_addsy = &abs_symbol;
4447 } /* if there's an add_symbol */
4448 #if defined (TC_I386) || defined (TE_LYNX) || defined (TC_I960) || defined (TC_M68K)
4449 /* On the 386 we must adjust by the segment vaddr as well.
4450 Ian Taylor.
4451
4452 I changed the i960 to work this way as well. This is
4453 compatible with the current GNU linker behaviour. I do
4454 not know what other i960 COFF assemblers do. This is not
4455 a common case: normally, only assembler code will contain
4456 a PC relative reloc, and only branches which do not
4457 originate in the .text section will have a non-zero
4458 address.
4459
4460 I changed the m68k to work this way as well. This will
4461 break existing PC relative relocs from sections which do
4462 not start at address 0, but it will make ld -r work.
4463 Ian Taylor, 4 Oct 96. */
4464
4465 add_number -= segP->scnhdr.s_vaddr;
4466 #endif
4467 } /* if pcrel */
4468
4469 #ifdef MD_APPLY_FIX3
4470 md_apply_fix3 (fixP, (valueT *) &add_number, this_segment_type);
4471 #else
4472 md_apply_fix (fixP, add_number);
4473 #endif
4474
4475 if (!fixP->fx_bit_fixP && ! fixP->fx_no_overflow)
4476 {
4477 #ifndef TC_M88K
4478 /* The m88k uses the offset field of the reloc to get around
4479 this problem. */
4480 if ((size == 1
4481 && ((add_number & ~0xFF)
4482 || (fixP->fx_signed && (add_number & 0x80)))
4483 && ((add_number & ~0xFF) != (-1 & ~0xFF)
4484 || (add_number & 0x80) == 0))
4485 || (size == 2
4486 && ((add_number & ~0xFFFF)
4487 || (fixP->fx_signed && (add_number & 0x8000)))
4488 && ((add_number & ~0xFFFF) != (-1 & ~0xFFFF)
4489 || (add_number & 0x8000) == 0)))
4490 {
4491 as_bad_where (fixP->fx_file, fixP->fx_line,
4492 _("Value of %ld too large for field of %d bytes at 0x%lx"),
4493 (long) add_number, size,
4494 (unsigned long) (fragP->fr_address + where));
4495 }
4496 #endif
4497 #ifdef WARN_SIGNED_OVERFLOW_WORD
4498 /* Warn if a .word value is too large when treated as a
4499 signed number. We already know it is not too negative.
4500 This is to catch over-large switches generated by gcc on
4501 the 68k. */
4502 if (!flag_signed_overflow_ok
4503 && size == 2
4504 && add_number > 0x7fff)
4505 as_bad_where (fixP->fx_file, fixP->fx_line,
4506 _("Signed .word overflow; switch may be too large; %ld at 0x%lx"),
4507 (long) add_number,
4508 (unsigned long) (fragP->fr_address + where));
4509 #endif
4510 } /* not a bit fix */
4511 } /* For each fixS in this segment. */
4512 } /* fixup_segment() */
4513
4514 #endif
4515
4516 /* The first entry in a .stab section is special. */
4517
4518 void
4519 obj_coff_init_stab_section (seg)
4520 segT seg;
4521 {
4522 char *file;
4523 char *p;
4524 char *stabstr_name;
4525 unsigned int stroff;
4526
4527 /* Make space for this first symbol. */
4528 p = frag_more (12);
4529 /* Zero it out. */
4530 memset (p, 0, 12);
4531 as_where (&file, (unsigned int *) NULL);
4532 stabstr_name = (char *) alloca (strlen (segment_info[seg].name) + 4);
4533 strcpy (stabstr_name, segment_info[seg].name);
4534 strcat (stabstr_name, "str");
4535 stroff = get_stab_string_offset (file, stabstr_name);
4536 know (stroff == 1);
4537 md_number_to_chars (p, stroff, 4);
4538 }
4539
4540 /* Fill in the counts in the first entry in a .stab section. */
4541
4542 static void
4543 adjust_stab_section(abfd, seg)
4544 bfd *abfd;
4545 segT seg;
4546 {
4547 segT stabstrseg = SEG_UNKNOWN;
4548 const char *secname, *name2;
4549 char *name;
4550 char *p = NULL;
4551 int i, strsz = 0, nsyms;
4552 fragS *frag = segment_info[seg].frchainP->frch_root;
4553
4554 /* Look for the associated string table section. */
4555
4556 secname = segment_info[seg].name;
4557 name = (char *) alloca (strlen (secname) + 4);
4558 strcpy (name, secname);
4559 strcat (name, "str");
4560
4561 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
4562 {
4563 name2 = segment_info[i].name;
4564 if (name2 != NULL && strncmp(name2, name, 8) == 0)
4565 {
4566 stabstrseg = i;
4567 break;
4568 }
4569 }
4570
4571 /* If we found the section, get its size. */
4572 if (stabstrseg != SEG_UNKNOWN)
4573 strsz = size_section (abfd, stabstrseg);
4574
4575 nsyms = size_section (abfd, seg) / 12 - 1;
4576
4577 /* Look for the first frag of sufficient size for the initial stab
4578 symbol, and collect a pointer to it. */
4579 while (frag && frag->fr_fix < 12)
4580 frag = frag->fr_next;
4581 assert (frag != 0);
4582 p = frag->fr_literal;
4583 assert (p != 0);
4584
4585 /* Write in the number of stab symbols and the size of the string
4586 table. */
4587 bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6);
4588 bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8);
4589 }
4590
4591 #endif /* not BFD_ASSEMBLER */
4592
4593 const pseudo_typeS coff_pseudo_table[] =
4594 {
4595 {"def", obj_coff_def, 0},
4596 {"dim", obj_coff_dim, 0},
4597 {"endef", obj_coff_endef, 0},
4598 {"line", obj_coff_line, 0},
4599 {"ln", obj_coff_ln, 0},
4600 #ifdef BFD_ASSEMBLER
4601 {"loc", obj_coff_loc, 0},
4602 #endif
4603 {"appline", obj_coff_ln, 1},
4604 {"scl", obj_coff_scl, 0},
4605 {"size", obj_coff_size, 0},
4606 {"tag", obj_coff_tag, 0},
4607 {"type", obj_coff_type, 0},
4608 {"val", obj_coff_val, 0},
4609 {"section", obj_coff_section, 0},
4610 {"sect", obj_coff_section, 0},
4611 /* FIXME: We ignore the MRI short attribute. */
4612 {"section.s", obj_coff_section, 0},
4613 {"sect.s", obj_coff_section, 0},
4614 /* We accept the .bss directive for backward compatibility with
4615 earlier versions of gas. */
4616 {"bss", obj_coff_bss, 0},
4617 {"weak", obj_coff_weak, 0},
4618 {"ident", obj_coff_ident, 0},
4619 #ifndef BFD_ASSEMBLER
4620 {"use", obj_coff_section, 0},
4621 {"text", obj_coff_text, 0},
4622 {"data", obj_coff_data, 0},
4623 {"lcomm", obj_coff_lcomm, 0},
4624 #else
4625 {"optim", s_ignore, 0}, /* For sun386i cc (?) */
4626 #endif
4627 {"version", s_ignore, 0},
4628 {"ABORT", s_abort, 0},
4629 #ifdef TC_M88K
4630 /* The m88k uses sdef instead of def. */
4631 {"sdef", obj_coff_def, 0},
4632 #endif
4633 {NULL, NULL, 0} /* end sentinel */
4634 }; /* coff_pseudo_table */
4635 \f
4636 #ifdef BFD_ASSEMBLER
4637
4638 /* Support for a COFF emulation. */
4639
4640 static void coff_pop_insert PARAMS ((void));
4641 static int coff_separate_stab_sections PARAMS ((void));
4642
4643 static void
4644 coff_pop_insert ()
4645 {
4646 pop_insert (coff_pseudo_table);
4647 }
4648
4649 static int
4650 coff_separate_stab_sections ()
4651 {
4652 return 1;
4653 }
4654
4655 const struct format_ops coff_format_ops =
4656 {
4657 bfd_target_coff_flavour,
4658 0, /* dfl_leading_underscore */
4659 1, /* emit_section_symbols */
4660 0, /* begin */
4661 c_dot_file_symbol,
4662 coff_frob_symbol,
4663 0, /* frob_file */
4664 0, /* frob_file_before_adjust */
4665 coff_frob_file_after_relocs,
4666 0, /* s_get_size */
4667 0, /* s_set_size */
4668 0, /* s_get_align */
4669 0, /* s_set_align */
4670 0, /* s_get_other */
4671 0, /* s_set_other */
4672 0, /* s_get_desc */
4673 0, /* s_set_desc */
4674 0, /* s_get_type */
4675 0, /* s_set_type */
4676 0, /* copy_symbol_attributes */
4677 0, /* generate_asm_lineno */
4678 0, /* process_stab */
4679 coff_separate_stab_sections,
4680 obj_coff_init_stab_section,
4681 0, /* sec_sym_ok_for_reloc */
4682 coff_pop_insert,
4683 0, /* ecoff_set_ext */
4684 coff_obj_read_begin_hook,
4685 coff_obj_symbol_new_hook
4686 };
4687
4688 #endif
This page took 0.175379 seconds and 5 git commands to generate.