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