Sat Dec 12 15:26:34 1992 Ian Lance Taylor (ian@cygnus.com)
[deliverable/binutils-gdb.git] / gas / config / obj-coffbfd.c
1 /* coff object file format with bfd
2 Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
3
4 This file is part of GAS.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /*
21
22 How does this releate to the rest of GAS ?
23
24 Well, all the other files in gas are more or less a black box. It
25 takes care of opening files, parsing command lines, stripping blanks
26 etc etc. This module gets a chance to register what it wants to do by
27 saying that it is interested in various pseduo ops. The other big
28 change is write_object_file. This runs through all the data
29 structures that gas builds, and outputs the file in the format of our
30 choice.
31
32 Hacked for BFDness by steve chamberlain
33
34 This object module now supports the Hitachi H8/300 and the AMD 29k
35
36 sac@cygnus.com
37 */
38
39 #include "as.h"
40 #include "obstack.h"
41 #include "subsegs.h"
42 #include "frags.h"
43 #include "../bfd/libbfd.h"
44 #include "../bfd/libcoff.h"
45
46 /* The NOP_OPCODE is for the alignment fill value. Fill with nop so
47 that we can stick sections together without causing trouble. */
48 #ifndef NOP_OPCODE
49 #define NOP_OPCODE 0x00
50 #endif
51
52 #define MIN(a,b) ((a) < (b)? (a) : (b))
53 /* This vector is used to turn an internal segment into a section #
54 suitable for insertion into a coff symbol table
55 */
56
57 const short seg_N_TYPE[] =
58 { /* in: segT out: N_TYPE bits */
59 C_ABS_SECTION,
60 1,
61 2,
62 3,
63 4,
64 5,
65 6,
66 7,
67 8,
68 9,
69 10,
70 C_UNDEF_SECTION, /* SEG_UNKNOWN */
71 C_UNDEF_SECTION, /* SEG_ABSENT */
72 C_UNDEF_SECTION, /* SEG_PASS1 */
73 C_UNDEF_SECTION, /* SEG_GOOF */
74 C_UNDEF_SECTION, /* SEG_BIG */
75 C_UNDEF_SECTION, /* SEG_DIFFERENCE */
76 C_DEBUG_SECTION, /* SEG_DEBUG */
77 C_NTV_SECTION, /* SEG_NTV */
78 C_PTV_SECTION, /* SEG_PTV */
79 C_REGISTER_SECTION, /* SEG_REGISTER */
80 };
81
82
83 int function_lineoff = -1; /* Offset in line#s where the last function
84 started (the odd entry for line #0) */
85
86
87 int had_lineno = 0;
88 int had_reloc = 0;
89
90 static symbolS *last_line_symbol;
91 /* Add 4 to the real value to get the index and compensate the
92 negatives. This vector is used by S_GET_SEGMENT to turn a coff
93 section number into a segment number
94 */
95 static symbolS *previous_file_symbol = NULL;
96 void c_symbol_merge ();
97 static int line_base;
98
99 symbolS *c_section_symbol ();
100 bfd *abfd;
101 void EXFUN (bfd_as_write_hook, (struct internal_filehdr *,
102 bfd * abfd));
103
104 static void EXFUN (fixup_segment, (segment_info_type *segP,
105 segT this_segment_type));
106
107
108 static void EXFUN (fixup_mdeps, (fragS *));
109
110
111 static void EXFUN (fill_section, (bfd * abfd,
112 struct internal_filehdr * f, unsigned
113 long *));
114
115
116 char *EXFUN (s_get_name, (symbolS * s));
117 static symbolS *EXFUN (tag_find_or_make, (char *name));
118 static symbolS *EXFUN (tag_find, (char *name));
119
120
121 static int
122 EXFUN (c_line_new, (
123 symbolS * symbol,
124 long paddr,
125 unsigned short line_number,
126 fragS * frag));
127
128
129 static void EXFUN (w_symbols,
130 (bfd * abfd,
131 char *where,
132 symbolS * symbol_rootP));
133
134
135
136 static void EXFUN (obj_coff_def, (int what));
137 static void EXFUN (obj_coff_lcomm, (void));
138 static void EXFUN (obj_coff_dim, (void));
139 static void EXFUN (obj_coff_text, (void));
140 static void EXFUN (obj_coff_data, (void));
141 static void EXFUN (obj_coff_endef, (void));
142 static void EXFUN (obj_coff_line, (void));
143 static void EXFUN (obj_coff_ln, (void));
144 static void EXFUN (obj_coff_scl, (void));
145 static void EXFUN (obj_coff_size, (void));
146 static void EXFUN (obj_coff_tag, (void));
147 static void EXFUN (obj_coff_type, (void));
148 static void EXFUN (obj_coff_val, (void));
149 void EXFUN (obj_coff_section, (void));
150 static void EXFUN (tag_init, (void));
151 static void EXFUN (tag_insert, (char *name, symbolS * symbolP));
152
153
154 static struct hash_control *tag_hash;
155 static symbolS *def_symbol_in_progress = NULL;
156
157 const pseudo_typeS obj_pseudo_table[] =
158 {
159 {"def", obj_coff_def, 0},
160 {"dim", obj_coff_dim, 0},
161 {"endef", obj_coff_endef, 0},
162 {"line", obj_coff_line, 0},
163 {"ln", obj_coff_ln, 0},
164 {"scl", obj_coff_scl, 0},
165 {"size", obj_coff_size, 0},
166 {"tag", obj_coff_tag, 0},
167 {"type", obj_coff_type, 0},
168 {"val", obj_coff_val, 0},
169 {"section", obj_coff_section, 0},
170 {"use", obj_coff_section, 0},
171 {"sect", obj_coff_section, 0},
172 {"text", obj_coff_text, 0},
173 {"data", obj_coff_data, 0},
174 /* we don't yet handle this. */
175 {"ident", s_ignore, 0},
176 {"ABORT", s_abort, 0},
177 {"lcomm", obj_coff_lcomm, 0},
178 {NULL} /* end sentinel */
179 }; /* obj_pseudo_table */
180
181
182
183 /* Section stuff
184
185 We allow more than just the standard 3 sections, infact, we allow
186 10 sections, (though the usual three have to be there).
187
188 This structure performs the mappings for us:
189
190 */
191
192 /* OBS stuff
193 static struct internal_scnhdr bss_section_header;
194 struct internal_scnhdr data_section_header;
195 struct internal_scnhdr text_section_header;
196
197 const segT N_TYPE_seg [32] =
198 {
199
200 };
201
202 */
203
204 #define N_SEG 32
205 typedef struct
206 {
207 segT seg_t;
208 int i;
209 }
210
211 seg_info_type;
212
213 seg_info_type seg_info_off_by_4[N_SEG] =
214 {
215 {SEG_PTV,},
216 {SEG_NTV,},
217 {SEG_DEBUG,},
218 {SEG_ABSOLUTE,},
219 {SEG_UNKNOWN,},
220 {SEG_E0},
221 {SEG_E1},
222 {SEG_E2},
223 {SEG_E3},
224 {SEG_E4},
225 {SEG_E5},
226 {SEG_E6},
227 {SEG_E7},
228 {SEG_E8},
229 {SEG_E9},
230 {15},
231 {16},
232 {17},
233 {18},
234 {19},
235 {20},
236 {0},
237 {0},
238 {0},
239 {SEG_REGISTER}, 0, 0, 0, 0};
240
241 #define SEG_INFO_FROM_SECTION_NUMBER(x) (seg_info_off_by_4[(x)+4])
242 #define SEG_INFO_FROM_SEG_NUMBER(x) (seg_info_off_by_4[(x)])
243
244
245 relax_addressT
246 DEFUN (relax_align, (address, alignment),
247 register relax_addressT address AND
248 register long alignment)
249 {
250 relax_addressT mask;
251 relax_addressT new_address;
252
253 mask = ~((~0) << alignment);
254 new_address = (address + mask) & (~mask);
255 return (new_address - address);
256 } /* relax_align() */
257
258
259 segT
260 DEFUN (s_get_segment, (x),
261 symbolS * x)
262 {
263 return SEG_INFO_FROM_SECTION_NUMBER (x->sy_symbol.ost_entry.n_scnum).seg_t;
264 }
265
266
267
268 /* calculate the size of the frag chain and fill in the section header
269 to contain all of it, also fill in the addr of the sections */
270 static unsigned int
271 DEFUN (size_section, (abfd, idx),
272 bfd * abfd AND
273 unsigned int idx)
274 {
275
276 unsigned int size = 0;
277 fragS *frag = segment_info[idx].frchainP->frch_root;
278 while (frag)
279 {
280 size = frag->fr_address;
281 #if 0
282 if (frag->fr_address != size)
283 {
284 printf ("Out of step\n");
285 size = frag->fr_address;
286 }
287
288 switch (frag->fr_type)
289 {
290 #ifdef TC_COFF_SIZEMACHDEP
291 case rs_machine_dependent:
292 size += TC_COFF_SIZEMACHDEP (frag);
293 break;
294 #endif
295 case rs_fill:
296 case rs_org:
297 size += frag->fr_fix;
298 size += frag->fr_offset * frag->fr_var;
299 break;
300 case rs_align:
301 size += frag->fr_fix;
302 size += relax_align (size, frag->fr_offset);
303 }
304 #endif
305 frag = frag->fr_next;
306 }
307 segment_info[idx].scnhdr.s_size = size;
308 return size;
309 }
310
311
312 static unsigned int
313 DEFUN (count_entries_in_chain, (idx),
314 unsigned int idx)
315 {
316 unsigned int nrelocs;
317 fixS *fixup_ptr;
318
319 /* Count the relocations */
320 fixup_ptr = segment_info[idx].fix_root;
321 nrelocs = 0;
322 while (fixup_ptr != (fixS *) NULL)
323 {
324 if (TC_COUNT_RELOC (fixup_ptr))
325 {
326
327 #ifdef TC_A29K
328
329 if (fixup_ptr->fx_r_type == RELOC_CONSTH)
330 nrelocs += 2;
331 else
332 nrelocs++;
333 #else
334 nrelocs++;
335 #endif
336 }
337
338 fixup_ptr = fixup_ptr->fx_next;
339 }
340 return nrelocs;
341 }
342
343 /* output all the relocations for a section */
344 void
345 DEFUN (do_relocs_for, (abfd, file_cursor),
346 bfd * abfd AND
347 unsigned long *file_cursor)
348 {
349 unsigned int nrelocs;
350 unsigned int idx;
351 unsigned int addr = 0;
352 for (idx = SEG_E0; idx < SEG_E9; idx++)
353 {
354 if (segment_info[idx].scnhdr.s_name[0])
355 {
356
357 struct external_reloc *ext_ptr;
358 struct external_reloc *external_reloc_vec;
359 unsigned int external_reloc_size;
360 unsigned int count = 0;
361 unsigned int base = addr;
362 fixS *fix_ptr = segment_info[idx].fix_root;
363 nrelocs = count_entries_in_chain (idx);
364
365 if (nrelocs)
366 had_reloc = 1;
367
368 external_reloc_size = nrelocs * RELSZ;
369 external_reloc_vec =
370 (struct external_reloc *) malloc (external_reloc_size);
371
372
373
374 ext_ptr = external_reloc_vec;
375
376 /* Fill in the internal coff style reloc struct from the
377 internal fix list */
378 while (fix_ptr)
379 {
380 symbolS *symbol_ptr;
381 struct internal_reloc intr;
382
383 /* Only output some of the relocations */
384 if (TC_COUNT_RELOC (fix_ptr))
385 {
386 #ifdef TC_RELOC_MANGLE
387 TC_RELOC_MANGLE (fix_ptr, &intr, base);
388
389 #else
390 symbolS *dot;
391 symbol_ptr = fix_ptr->fx_addsy;
392
393 intr.r_type = TC_COFF_FIX2RTYPE (fix_ptr);
394 intr.r_vaddr =
395 base + fix_ptr->fx_frag->fr_address + fix_ptr->fx_where;
396
397 intr.r_offset = fix_ptr->fx_offset;
398
399 intr.r_offset = 0;
400
401 /* Turn the segment of the symbol into an offset
402 */
403 if (symbol_ptr)
404 {
405 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
406 if (dot)
407 {
408 intr.r_symndx = dot->sy_number;
409 }
410 else
411 {
412 intr.r_symndx = symbol_ptr->sy_number;
413 }
414
415 }
416 else
417 {
418 intr.r_symndx = -1;
419
420
421 }
422 #endif
423
424 (void) bfd_coff_swap_reloc_out (abfd, &intr, ext_ptr);
425 ext_ptr++;
426
427 #if defined(TC_A29K)
428 /* The 29k has a special kludge for the high 16 bit reloc.
429 Two relocations are emmited, R_IHIHALF, and
430 R_IHCONST. The second one doesn't contain a symbol,
431 but uses the value for offset */
432
433 if (intr.r_type == R_IHIHALF)
434 {
435 /* now emit the second bit */
436 intr.r_type = R_IHCONST;
437 intr.r_symndx = fix_ptr->fx_addnumber;
438 (void) bfd_coff_swap_reloc_out (abfd, &intr, ext_ptr);
439 ext_ptr++;
440 }
441 #endif
442 }
443
444 fix_ptr = fix_ptr->fx_next;
445 }
446
447 /* Write out the reloc table */
448 segment_info[idx].scnhdr.s_relptr = *file_cursor;
449 segment_info[idx].scnhdr.s_nreloc = nrelocs;
450 bfd_write ((PTR) external_reloc_vec, 1, external_reloc_size, abfd);
451 *file_cursor += external_reloc_size;
452 free (external_reloc_vec);
453 }
454 addr += segment_info[idx].scnhdr.s_size;
455 }
456 }
457
458
459 /* run through a frag chain and write out the data to go with it, fill
460 in the scnhdrs with the info on the file postions
461 */
462 static void
463 DEFUN (fill_section, (abfd, filehdr, file_cursor),
464 bfd * abfd AND
465 struct internal_filehdr *filehdr AND
466 unsigned long *file_cursor)
467 {
468
469 unsigned int i;
470 #ifdef ZERO_BASED_SEGMENTS
471 unsigned int paddr = 0;
472 #endif
473
474 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
475 {
476 unsigned int offset = 0;
477
478 struct internal_scnhdr *s = &(segment_info[i].scnhdr);
479
480 if (s->s_name[0])
481 {
482 fragS *frag = segment_info[i].frchainP->frch_root;
483 char *buffer;
484
485 if (s->s_size != 0 && i != SEG_E2)
486 {
487 buffer = malloc (s->s_size);
488 s->s_scnptr = *file_cursor;
489 #ifdef ZERO_BASED_SEGMENTS
490 s->s_paddr = paddr;
491 s->s_vaddr = paddr;
492 #endif
493 }
494 else
495 {
496 buffer = NULL;
497 s->s_scnptr = 0;
498 }
499
500 s->s_flags = STYP_REG;
501 if (strcmp (s->s_name, ".text") == 0)
502 s->s_flags |= STYP_TEXT;
503 else if (strcmp (s->s_name, ".data") == 0)
504 s->s_flags |= STYP_DATA;
505 else if (strcmp (s->s_name, ".bss") == 0)
506 s->s_flags |= STYP_BSS | STYP_NOLOAD;
507 else if (strcmp (s->s_name, ".lit") == 0)
508 s->s_flags = STYP_LIT | STYP_TEXT;
509 else if (strcmp (s->s_name, ".init") == 0)
510 s->s_flags |= STYP_TEXT;
511 else if (strcmp (s->s_name, ".fini") == 0)
512 s->s_flags |= STYP_TEXT;
513
514 while (frag)
515 {
516 unsigned int fill_size;
517 switch (frag->fr_type)
518 {
519 case rs_machine_dependent:
520 if (frag->fr_fix)
521 {
522 memcpy (buffer + frag->fr_address,
523 frag->fr_literal,
524 frag->fr_fix);
525 offset += frag->fr_fix;
526 }
527
528 break;
529 case rs_fill:
530 case rs_align:
531 case rs_org:
532 if (frag->fr_fix)
533 {
534 memcpy (buffer + frag->fr_address,
535 frag->fr_literal,
536 frag->fr_fix);
537 offset += frag->fr_fix;
538 }
539
540 fill_size = frag->fr_var;
541 if (fill_size)
542 {
543 unsigned int count;
544 unsigned int off = frag->fr_fix;
545 for (count = frag->fr_offset; count; count--)
546 {
547 memcpy (buffer + frag->fr_address + off,
548 frag->fr_literal + frag->fr_fix,
549 fill_size);
550 off += fill_size;
551 offset += fill_size;
552
553 }
554
555 }
556 break;
557 case rs_broken_word:
558 break;
559 default:
560 abort ();
561 }
562 frag = frag->fr_next;
563 }
564
565 if (s->s_size != 0 && i != SEG_E2)
566 {
567 bfd_write (buffer, s->s_size, 1, abfd);
568 free (buffer);
569 *file_cursor += s->s_size;
570 }
571 #ifdef ZERO_BASED_SEGMENTS
572 paddr += s->s_size;
573 #endif
574 }
575 }
576 }
577
578
579
580 /* Coff file generation & utilities */
581
582
583 static void
584 DEFUN (coff_header_append, (abfd, filehdr, aouthdr),
585 bfd * abfd AND
586 struct internal_filehdr *filehdr AND
587 struct internal_aouthdr *aouthdr)
588 {
589 unsigned int i;
590 char buffer[1000];
591 char buffero[1000];
592
593 bfd_seek (abfd, 0, 0);
594 #if 0
595 filehdr.f_opthdr = bfd_coff_swap_aouthdr_out (abfd, aouthdr,
596 buffero);
597 #else
598 filehdr->f_opthdr = 0;
599 #endif
600 i = bfd_coff_swap_filehdr_out (abfd, filehdr, buffer);
601
602 bfd_write (buffer, i, 1, abfd);
603 bfd_write (buffero, filehdr->f_opthdr, 1, abfd);
604
605 for (i = SEG_E0; i < SEG_E9; i++)
606 {
607 if (segment_info[i].scnhdr.s_name[0])
608 {
609 unsigned int size =
610 bfd_coff_swap_scnhdr_out (abfd,
611 &(segment_info[i].scnhdr),
612 buffer);
613 bfd_write (buffer, size, 1, abfd);
614 }
615 }
616 }
617
618
619 char *
620 DEFUN (symbol_to_chars, (abfd, where, symbolP),
621 bfd * abfd AND
622 char *where AND
623 symbolS * symbolP)
624 {
625 unsigned int numaux = symbolP->sy_symbol.ost_entry.n_numaux;
626 unsigned int i;
627
628 /* Turn any symbols with register attributes into abs symbols */
629 if (S_GET_SEGMENT (symbolP) == SEG_REGISTER)
630 {
631 S_SET_SEGMENT (symbolP, SEG_ABSOLUTE);
632 }
633 /* At the same time, relocate all symbols to their output value */
634
635 S_SET_VALUE (symbolP,
636 segment_info[S_GET_SEGMENT (symbolP)].scnhdr.s_paddr
637 + S_GET_VALUE (symbolP));
638
639 where += bfd_coff_swap_sym_out (abfd, &symbolP->sy_symbol.ost_entry,
640 where);
641
642 for (i = 0; i < numaux; i++)
643 {
644 where += bfd_coff_swap_aux_out (abfd,
645 &symbolP->sy_symbol.ost_auxent[i],
646 S_GET_DATA_TYPE (symbolP),
647 S_GET_STORAGE_CLASS (symbolP),
648 where);
649 }
650 return where;
651
652 }
653
654
655
656
657 void
658 obj_symbol_new_hook (symbolP)
659 symbolS *symbolP;
660 {
661 char underscore = 0; /* Symbol has leading _ */
662
663 /* Effective symbol */
664 /* Store the pointer in the offset. */
665 S_SET_ZEROES (symbolP, 0L);
666 S_SET_DATA_TYPE (symbolP, T_NULL);
667 S_SET_STORAGE_CLASS (symbolP, 0);
668 S_SET_NUMBER_AUXILIARY (symbolP, 0);
669 /* Additional information */
670 symbolP->sy_symbol.ost_flags = 0;
671 /* Auxiliary entries */
672 bzero ((char *) &symbolP->sy_symbol.ost_auxent[0], AUXESZ);
673
674 #ifdef STRIP_UNDERSCORE
675 /* Remove leading underscore at the beginning of the symbol.
676 * This is to be compatible with the standard librairies.
677 */
678 if (*S_GET_NAME (symbolP) == '_')
679 {
680 underscore = 1;
681 S_SET_NAME (symbolP, S_GET_NAME (symbolP) + 1);
682 } /* strip underscore */
683 #endif /* STRIP_UNDERSCORE */
684
685 if (S_IS_STRING (symbolP))
686 SF_SET_STRING (symbolP);
687 if (!underscore && S_IS_LOCAL (symbolP))
688 SF_SET_LOCAL (symbolP);
689
690 return;
691 } /* obj_symbol_new_hook() */
692
693 /* stack stuff */
694 stack *
695 stack_init (chunk_size, element_size)
696 unsigned long chunk_size;
697 unsigned long element_size;
698 {
699 stack *st;
700
701 if ((st = (stack *) malloc (sizeof (stack))) == (stack *) 0)
702 return (stack *) 0;
703 if ((st->data = malloc (chunk_size)) == (char *) 0)
704 {
705 free (st);
706 return (stack *) 0;
707 }
708 st->pointer = 0;
709 st->size = chunk_size;
710 st->chunk_size = chunk_size;
711 st->element_size = element_size;
712 return st;
713 } /* stack_init() */
714
715 void
716 stack_delete (st)
717 stack *st;
718 {
719 free (st->data);
720 free (st);
721 }
722
723 char *
724 stack_push (st, element)
725 stack *st;
726 char *element;
727 {
728 if (st->pointer + st->element_size >= st->size)
729 {
730 st->size += st->chunk_size;
731 if ((st->data = xrealloc (st->data, st->size)) == (char *) 0)
732 return (char *) 0;
733 }
734 memcpy (st->data + st->pointer, element, st->element_size);
735 st->pointer += st->element_size;
736 return st->data + st->pointer;
737 } /* stack_push() */
738
739 char *
740 stack_pop (st)
741 stack *st;
742 {
743 if ((st->pointer -= st->element_size) < 0)
744 {
745 st->pointer = 0;
746 return (char *) 0;
747 }
748
749 return st->data + st->pointer;
750 }
751
752 char *
753 stack_top (st)
754 stack *st;
755 {
756 return st->data + st->pointer - st->element_size;
757 }
758
759
760 /*
761 * Handle .ln directives.
762 */
763
764 static void
765 obj_coff_ln ()
766 {
767 int l;
768
769 if (def_symbol_in_progress != NULL)
770 {
771 as_warn (".ln pseudo-op inside .def/.endef: ignored.");
772 demand_empty_rest_of_line ();
773 return;
774 } /* wrong context */
775
776 c_line_new (0,
777 obstack_next_free (&frags) - frag_now->fr_literal,
778 l = get_absolute_expression (),
779 frag_now);
780 #ifndef NO_LISTING
781 {
782 extern int listing;
783
784 if (listing)
785 {
786 listing_source_line (l + line_base - 1);
787 }
788
789 }
790 #endif
791 demand_empty_rest_of_line ();
792 return;
793 } /* obj_coff_line() */
794
795 /*
796 * def()
797 *
798 * Handle .def directives.
799 *
800 * One might ask : why can't we symbol_new if the symbol does not
801 * already exist and fill it with debug information. Because of
802 * the C_EFCN special symbol. It would clobber the value of the
803 * function symbol before we have a chance to notice that it is
804 * a C_EFCN. And a second reason is that the code is more clear this
805 * way. (at least I think it is :-).
806 *
807 */
808
809 #define SKIP_SEMI_COLON() while (*input_line_pointer++ != ';')
810 #define SKIP_WHITESPACES() while (*input_line_pointer == ' ' || \
811 *input_line_pointer == '\t') \
812 input_line_pointer++;
813
814 static void
815 DEFUN (obj_coff_def, (what),
816 int what)
817 {
818 char name_end; /* Char after the end of name */
819 char *symbol_name; /* Name of the debug symbol */
820 char *symbol_name_copy; /* Temporary copy of the name */
821 unsigned int symbol_name_length;
822 /*$char* directiveP;$ *//* Name of the pseudo opcode */
823 /*$char directive[MAX_DIRECTIVE];$ *//* Backup of the directive */
824 /*$char end = 0;$ *//* If 1, stop parsing */
825
826 if (def_symbol_in_progress != NULL)
827 {
828 as_warn (".def pseudo-op used inside of .def/.endef: ignored.");
829 demand_empty_rest_of_line ();
830 return;
831 } /* if not inside .def/.endef */
832
833 SKIP_WHITESPACES ();
834
835 def_symbol_in_progress = (symbolS *) obstack_alloc (&notes, sizeof (*def_symbol_in_progress));
836 bzero (def_symbol_in_progress, sizeof (*def_symbol_in_progress));
837
838 symbol_name = input_line_pointer;
839 name_end = get_symbol_end ();
840 symbol_name_length = strlen (symbol_name);
841 symbol_name_copy = xmalloc (symbol_name_length + 1);
842 strcpy (symbol_name_copy, symbol_name);
843
844 /* Initialize the new symbol */
845 #ifdef STRIP_UNDERSCORE
846 S_SET_NAME (def_symbol_in_progress, (*symbol_name_copy == '_'
847 ? symbol_name_copy + 1
848 : symbol_name_copy));
849 #else /* STRIP_UNDERSCORE */
850 S_SET_NAME (def_symbol_in_progress, symbol_name_copy);
851 #endif /* STRIP_UNDERSCORE */
852 /* free(symbol_name_copy); */
853 def_symbol_in_progress->sy_name_offset = ~0;
854 def_symbol_in_progress->sy_number = ~0;
855 def_symbol_in_progress->sy_frag = &zero_address_frag;
856
857 if (S_IS_STRING (def_symbol_in_progress))
858 {
859 SF_SET_STRING (def_symbol_in_progress);
860 } /* "long" name */
861
862 *input_line_pointer = name_end;
863
864 demand_empty_rest_of_line ();
865 return;
866 } /* obj_coff_def() */
867
868 unsigned int dim_index;
869 static void
870 DEFUN_VOID (obj_coff_endef)
871 {
872 symbolS *symbolP = 0;
873 /* DIM BUG FIX sac@cygnus.com */
874 dim_index = 0;
875 if (def_symbol_in_progress == NULL)
876 {
877 as_warn (".endef pseudo-op used outside of .def/.endef: ignored.");
878 demand_empty_rest_of_line ();
879 return;
880 } /* if not inside .def/.endef */
881
882 /* Set the section number according to storage class. */
883 switch (S_GET_STORAGE_CLASS (def_symbol_in_progress))
884 {
885 case C_STRTAG:
886 case C_ENTAG:
887 case C_UNTAG:
888 SF_SET_TAG (def_symbol_in_progress);
889 /* intentional fallthrough */
890 case C_FILE:
891 case C_TPDEF:
892 SF_SET_DEBUG (def_symbol_in_progress);
893 S_SET_SEGMENT (def_symbol_in_progress, SEG_DEBUG);
894 break;
895
896 case C_EFCN:
897 SF_SET_LOCAL (def_symbol_in_progress); /* Do not emit this symbol. */
898 /* intentional fallthrough */
899 case C_BLOCK:
900 SF_SET_PROCESS (def_symbol_in_progress); /* Will need processing before writing */
901 /* intentional fallthrough */
902 case C_FCN:
903 S_SET_SEGMENT (def_symbol_in_progress, SEG_E0);
904
905 if (strcmp (def_symbol_in_progress->sy_symbol.ost_entry._n._n_nptr,
906 ".bf") == 0)
907 { /* .bf */
908 if (function_lineoff < 0)
909 {
910 fprintf (stderr, "`.bf' symbol without preceding function\n");
911 } /* missing function symbol */
912 SA_GET_SYM_LNNOPTR (last_line_symbol) = function_lineoff;
913
914 SF_SET_PROCESS (last_line_symbol);
915 function_lineoff = -1;
916 }
917 break;
918
919 #ifdef C_AUTOARG
920 case C_AUTOARG:
921 #endif /* C_AUTOARG */
922 case C_AUTO:
923 case C_REG:
924 case C_MOS:
925 case C_MOE:
926 case C_MOU:
927 case C_ARG:
928 case C_REGPARM:
929 case C_FIELD:
930 case C_EOS:
931 SF_SET_DEBUG (def_symbol_in_progress);
932 S_SET_SEGMENT (def_symbol_in_progress, SEG_ABSOLUTE);
933 break;
934
935 case C_EXT:
936 case C_STAT:
937 case C_LABEL:
938 /* Valid but set somewhere else (s_comm, s_lcomm, colon) */
939 break;
940
941 case C_USTATIC:
942 case C_EXTDEF:
943 case C_ULABEL:
944 as_warn ("unexpected storage class %d", S_GET_STORAGE_CLASS (def_symbol_in_progress));
945 break;
946 } /* switch on storage class */
947
948 /* Now that we have built a debug symbol, try to find if
949 we should merge with an existing symbol or not. If a
950 symbol is C_EFCN or SEG_ABSOLUTE or untagged
951 SEG_DEBUG it never merges. We also don't merge
952 labels, which are in a different namespace, nor
953 symbols which have not yet been defined since they
954 are typically unique, nor do we merge tags with
955 non-tags. */
956
957 /* Two cases for functions. Either debug followed
958 by definition or definition followed by debug.
959 For definition first, we will merge the debug
960 symbol into the definition. For debug first, the
961 lineno entry MUST point to the definition
962 function or else it will point off into space
963 when crawl_symbols() merges the debug
964 symbol into the real symbol. Therefor, let's
965 presume the debug symbol is a real function
966 reference. */
967
968 /* FIXME-SOON If for some reason the definition
969 label/symbol is never seen, this will probably
970 leave an undefined symbol at link time. */
971
972 if (S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_EFCN
973 || S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_LABEL
974 || (S_GET_SEGMENT (def_symbol_in_progress) == SEG_DEBUG
975 && !SF_GET_TAG (def_symbol_in_progress))
976 || S_GET_SEGMENT (def_symbol_in_progress) == SEG_ABSOLUTE
977 || def_symbol_in_progress->sy_forward != NULL
978 || (symbolP = symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP)) == NULL
979 || (SF_GET_TAG (def_symbol_in_progress) != SF_GET_TAG (symbolP)))
980 {
981
982 symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP, &symbol_lastP);
983
984 }
985 else
986 {
987 /* This symbol already exists, merge the
988 newly created symbol into the old one.
989 This is not mandatory. The linker can
990 handle duplicate symbols correctly. But I
991 guess that it save a *lot* of space if
992 the assembly file defines a lot of
993 symbols. [loic] */
994
995 /* The debug entry (def_symbol_in_progress)
996 is merged into the previous definition. */
997
998 c_symbol_merge (def_symbol_in_progress, symbolP);
999 /* FIXME-SOON Should *def_symbol_in_progress be free'd? xoxorich. */
1000 def_symbol_in_progress = symbolP;
1001
1002 if (SF_GET_FUNCTION (def_symbol_in_progress)
1003 || SF_GET_TAG (def_symbol_in_progress))
1004 {
1005 /* For functions, and tags, the symbol *must* be where the debug symbol
1006 appears. Move the existing symbol to the current place. */
1007 /* If it already is at the end of the symbol list, do nothing */
1008 if (def_symbol_in_progress != symbol_lastP)
1009 {
1010 symbol_remove (def_symbol_in_progress, &symbol_rootP, &symbol_lastP);
1011 symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP, &symbol_lastP);
1012 } /* if not already in place */
1013 } /* if function */
1014 } /* normal or mergable */
1015
1016 if (SF_GET_TAG (def_symbol_in_progress)
1017 && symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP) == NULL)
1018 {
1019 tag_insert (S_GET_NAME (def_symbol_in_progress), def_symbol_in_progress);
1020 } /* If symbol is a {structure,union} tag, associate symbol to its name. */
1021
1022 if (SF_GET_FUNCTION (def_symbol_in_progress))
1023 {
1024 know (sizeof (def_symbol_in_progress) <= sizeof (long));
1025 function_lineoff
1026 = c_line_new (def_symbol_in_progress, 0, 0, &zero_address_frag);
1027
1028
1029
1030 SF_SET_PROCESS (def_symbol_in_progress);
1031
1032 if (symbolP == NULL)
1033 {
1034 /* That is, if this is the first
1035 time we've seen the function... */
1036 symbol_table_insert (def_symbol_in_progress);
1037 } /* definition follows debug */
1038 } /* Create the line number entry pointing to the function being defined */
1039
1040 def_symbol_in_progress = NULL;
1041 demand_empty_rest_of_line ();
1042 return;
1043 } /* obj_coff_endef() */
1044
1045 static void
1046 DEFUN_VOID (obj_coff_dim)
1047 {
1048 register int dim_index;
1049
1050 if (def_symbol_in_progress == NULL)
1051 {
1052 as_warn (".dim pseudo-op used outside of .def/.endef: ignored.");
1053 demand_empty_rest_of_line ();
1054 return;
1055 } /* if not inside .def/.endef */
1056
1057 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
1058
1059 for (dim_index = 0; dim_index < DIMNUM; dim_index++)
1060 {
1061 SKIP_WHITESPACES ();
1062 SA_SET_SYM_DIMEN (def_symbol_in_progress, dim_index, get_absolute_expression ());
1063
1064 switch (*input_line_pointer)
1065 {
1066
1067 case ',':
1068 input_line_pointer++;
1069 break;
1070
1071 default:
1072 as_warn ("badly formed .dim directive ignored");
1073 /* intentional fallthrough */
1074 case '\n':
1075 case ';':
1076 dim_index = DIMNUM;
1077 break;
1078 } /* switch on following character */
1079 } /* for each dimension */
1080
1081 demand_empty_rest_of_line ();
1082 return;
1083 } /* obj_coff_dim() */
1084
1085 static void
1086 obj_coff_line ()
1087 {
1088 int this_base;
1089
1090 if (def_symbol_in_progress == NULL)
1091 {
1092 obj_coff_ln ();
1093 return;
1094 } /* if it looks like a stabs style line */
1095
1096 this_base = get_absolute_expression ();
1097 if (this_base > line_base)
1098 {
1099 line_base = this_base;
1100 }
1101
1102
1103 #ifndef NO_LISTING
1104 {
1105 extern int listing;
1106 if (listing && 0)
1107 {
1108 listing_source_line (line_base);
1109 }
1110 }
1111 #endif
1112 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
1113 SA_SET_SYM_LNNO (def_symbol_in_progress, line_base);
1114
1115 demand_empty_rest_of_line ();
1116 return;
1117 } /* obj_coff_line() */
1118
1119 static void
1120 obj_coff_size ()
1121 {
1122 if (def_symbol_in_progress == NULL)
1123 {
1124 as_warn (".size pseudo-op used outside of .def/.endef ignored.");
1125 demand_empty_rest_of_line ();
1126 return;
1127 } /* if not inside .def/.endef */
1128
1129 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
1130 SA_SET_SYM_SIZE (def_symbol_in_progress, get_absolute_expression ());
1131 demand_empty_rest_of_line ();
1132 return;
1133 } /* obj_coff_size() */
1134
1135 static void
1136 obj_coff_scl ()
1137 {
1138 if (def_symbol_in_progress == NULL)
1139 {
1140 as_warn (".scl pseudo-op used outside of .def/.endef ignored.");
1141 demand_empty_rest_of_line ();
1142 return;
1143 } /* if not inside .def/.endef */
1144
1145 S_SET_STORAGE_CLASS (def_symbol_in_progress, get_absolute_expression ());
1146 demand_empty_rest_of_line ();
1147 return;
1148 } /* obj_coff_scl() */
1149
1150 static void
1151 obj_coff_tag ()
1152 {
1153 char *symbol_name;
1154 char name_end;
1155
1156 if (def_symbol_in_progress == NULL)
1157 {
1158 as_warn (".tag pseudo-op used outside of .def/.endef ignored.");
1159 demand_empty_rest_of_line ();
1160 return;
1161 } /* if not inside .def/.endef */
1162
1163 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
1164 symbol_name = input_line_pointer;
1165 name_end = get_symbol_end ();
1166
1167 /* Assume that the symbol referred to by .tag is always defined. */
1168 /* This was a bad assumption. I've added find_or_make. xoxorich. */
1169 SA_SET_SYM_TAGNDX (def_symbol_in_progress, (long) tag_find_or_make (symbol_name));
1170 if (SA_GET_SYM_TAGNDX (def_symbol_in_progress) == 0L)
1171 {
1172 as_warn ("tag not found for .tag %s", symbol_name);
1173 } /* not defined */
1174
1175 SF_SET_TAGGED (def_symbol_in_progress);
1176 *input_line_pointer = name_end;
1177
1178 demand_empty_rest_of_line ();
1179 return;
1180 } /* obj_coff_tag() */
1181
1182 static void
1183 obj_coff_type ()
1184 {
1185 if (def_symbol_in_progress == NULL)
1186 {
1187 as_warn (".type pseudo-op used outside of .def/.endef ignored.");
1188 demand_empty_rest_of_line ();
1189 return;
1190 } /* if not inside .def/.endef */
1191
1192 S_SET_DATA_TYPE (def_symbol_in_progress, get_absolute_expression ());
1193
1194 if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress)) &&
1195 S_GET_STORAGE_CLASS (def_symbol_in_progress) != C_TPDEF)
1196 {
1197 SF_SET_FUNCTION (def_symbol_in_progress);
1198 } /* is a function */
1199
1200 demand_empty_rest_of_line ();
1201 return;
1202 } /* obj_coff_type() */
1203
1204 static void
1205 obj_coff_val ()
1206 {
1207 if (def_symbol_in_progress == NULL)
1208 {
1209 as_warn (".val pseudo-op used outside of .def/.endef ignored.");
1210 demand_empty_rest_of_line ();
1211 return;
1212 } /* if not inside .def/.endef */
1213
1214 if (is_name_beginner (*input_line_pointer))
1215 {
1216 char *symbol_name = input_line_pointer;
1217 char name_end = get_symbol_end ();
1218
1219 if (!strcmp (symbol_name, "."))
1220 {
1221 def_symbol_in_progress->sy_frag = frag_now;
1222 S_SET_VALUE (def_symbol_in_progress, obstack_next_free (&frags) - frag_now->fr_literal);
1223 /* If the .val is != from the .def (e.g. statics) */
1224 }
1225 else if (strcmp (S_GET_NAME (def_symbol_in_progress), symbol_name))
1226 {
1227 def_symbol_in_progress->sy_forward = symbol_find_or_make (symbol_name);
1228
1229 /* If the segment is undefined when the forward
1230 reference is solved, then copy the segment id
1231 from the forward symbol. */
1232 SF_SET_GET_SEGMENT (def_symbol_in_progress);
1233
1234 /* FIXME: gcc can generate address expressions
1235 here in unusual cases (search for "obscure"
1236 in sdbout.c). We just ignore the offset
1237 here, thus generating incorrect debugging
1238 information. We ignore the rest of the
1239 line just below. */
1240 }
1241 /* Otherwise, it is the name of a non debug symbol and
1242 its value will be calculated later. */
1243 *input_line_pointer = name_end;
1244
1245 /* FIXME: this is to avoid an error message in the
1246 FIXME case mentioned just above. */
1247 while (! is_end_of_line[*input_line_pointer])
1248 ++input_line_pointer;
1249 }
1250 else
1251 {
1252 S_SET_VALUE (def_symbol_in_progress, get_absolute_expression ());
1253 } /* if symbol based */
1254
1255 demand_empty_rest_of_line ();
1256 return;
1257 } /* obj_coff_val() */
1258
1259 /*
1260 * Maintain a list of the tagnames of the structres.
1261 */
1262
1263 static void
1264 tag_init ()
1265 {
1266 tag_hash = hash_new ();
1267 return;
1268 } /* tag_init() */
1269
1270 static void
1271 tag_insert (name, symbolP)
1272 char *name;
1273 symbolS *symbolP;
1274 {
1275 register char *error_string;
1276
1277 if (*(error_string = hash_jam (tag_hash, name, (char *) symbolP)))
1278 {
1279 as_fatal ("Inserting \"%s\" into structure table failed: %s",
1280 name, error_string);
1281 }
1282 return;
1283 } /* tag_insert() */
1284
1285 static symbolS *
1286 tag_find_or_make (name)
1287 char *name;
1288 {
1289 symbolS *symbolP;
1290
1291 if ((symbolP = tag_find (name)) == NULL)
1292 {
1293 symbolP = symbol_new (name,
1294 SEG_UNKNOWN,
1295 0,
1296 &zero_address_frag);
1297
1298 tag_insert (S_GET_NAME (symbolP), symbolP);
1299 } /* not found */
1300
1301 return (symbolP);
1302 } /* tag_find_or_make() */
1303
1304 static symbolS *
1305 tag_find (name)
1306 char *name;
1307 {
1308 #ifdef STRIP_UNDERSCORE
1309 if (*name == '_')
1310 name++;
1311 #endif /* STRIP_UNDERSCORE */
1312 return ((symbolS *) hash_find (tag_hash, name));
1313 } /* tag_find() */
1314
1315 void
1316 obj_read_begin_hook ()
1317 {
1318 /* These had better be the same. Usually 18 bytes. */
1319 #ifndef BFD_HEADERS
1320 know (sizeof (SYMENT) == sizeof (AUXENT));
1321 know (SYMESZ == AUXESZ);
1322 #endif
1323 tag_init ();
1324
1325 return;
1326 } /* obj_read_begin_hook() */
1327
1328 /* This function runs through the symbol table and puts all the
1329 externals onto another chain */
1330
1331 /* The chain of externals */
1332 symbolS *symbol_externP = NULL;
1333 symbolS *symbol_extern_lastP = NULL;
1334
1335 stack *block_stack;
1336 symbolS *last_functionP = NULL;
1337 symbolS *last_tagP;
1338
1339
1340 static unsigned int
1341 DEFUN_VOID (yank_symbols)
1342 {
1343 symbolS *symbolP;
1344 unsigned int symbol_number = 0;
1345
1346 for (symbolP = symbol_rootP;
1347 symbolP;
1348 symbolP = symbolP ? symbol_next (symbolP) : symbol_rootP)
1349 {
1350 if (!SF_GET_DEBUG (symbolP))
1351 {
1352 /* Debug symbols do not need all this rubbish */
1353 symbolS *real_symbolP;
1354
1355 /* L* and C_EFCN symbols never merge. */
1356 if (!SF_GET_LOCAL (symbolP)
1357 && S_GET_STORAGE_CLASS (symbolP) != C_LABEL
1358 && (real_symbolP = symbol_find_base (S_GET_NAME (symbolP), DO_NOT_STRIP))
1359 && real_symbolP != symbolP)
1360 {
1361 /* FIXME-SOON: where do dups come from?
1362 Maybe tag references before definitions? xoxorich. */
1363 /* Move the debug data from the debug symbol to the
1364 real symbol. Do NOT do the oposite (i.e. move from
1365 real symbol to debug symbol and remove real symbol from the
1366 list.) Because some pointers refer to the real symbol
1367 whereas no pointers refer to the debug symbol. */
1368 c_symbol_merge (symbolP, real_symbolP);
1369 /* Replace the current symbol by the real one */
1370 /* The symbols will never be the last or the first
1371 because : 1st symbol is .file and 3 last symbols are
1372 .text, .data, .bss */
1373 symbol_remove (real_symbolP, &symbol_rootP, &symbol_lastP);
1374 symbol_insert (real_symbolP, symbolP, &symbol_rootP, &symbol_lastP);
1375 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
1376 symbolP = real_symbolP;
1377 } /* if not local but dup'd */
1378
1379 if (flagseen['R'] && (S_GET_SEGMENT (symbolP) == SEG_E1))
1380 {
1381 S_SET_SEGMENT (symbolP, SEG_E0);
1382 } /* push data into text */
1383
1384 S_SET_VALUE (symbolP,
1385 S_GET_VALUE (symbolP) + symbolP->sy_frag->fr_address);
1386
1387 if (!S_IS_DEFINED (symbolP) && !SF_GET_LOCAL (symbolP))
1388 {
1389 S_SET_EXTERNAL (symbolP);
1390 }
1391 else if (S_GET_STORAGE_CLASS (symbolP) == C_NULL)
1392 {
1393 if (S_GET_SEGMENT (symbolP) == SEG_E0)
1394 {
1395 S_SET_STORAGE_CLASS (symbolP, C_LABEL);
1396 }
1397 else
1398 {
1399 S_SET_STORAGE_CLASS (symbolP, C_STAT);
1400 }
1401 }
1402
1403 /* Mainly to speed up if not -g */
1404 if (SF_GET_PROCESS (symbolP))
1405 {
1406 /* Handle the nested blocks auxiliary info. */
1407 if (S_GET_STORAGE_CLASS (symbolP) == C_BLOCK)
1408 {
1409 if (!strcmp (S_GET_NAME (symbolP), ".bb"))
1410 stack_push (block_stack, (char *) &symbolP);
1411 else
1412 { /* .eb */
1413 register symbolS *begin_symbolP;
1414 begin_symbolP = *(symbolS **) stack_pop (block_stack);
1415 if (begin_symbolP == (symbolS *) 0)
1416 as_warn ("mismatched .eb");
1417 else
1418 SA_SET_SYM_ENDNDX (begin_symbolP, symbol_number + 2);
1419 }
1420 }
1421 /* If we are able to identify the type of a function, and we
1422 are out of a function (last_functionP == 0) then, the
1423 function symbol will be associated with an auxiliary
1424 entry. */
1425 if (last_functionP == (symbolS *) 0 &&
1426 SF_GET_FUNCTION (symbolP))
1427 {
1428 last_functionP = symbolP;
1429
1430 if (S_GET_NUMBER_AUXILIARY (symbolP) < 1)
1431 {
1432 S_SET_NUMBER_AUXILIARY (symbolP, 1);
1433 } /* make it at least 1 */
1434
1435 /* Clobber possible stale .dim information. */
1436 #if 0
1437 /* Iffed out by steve - this fries the lnnoptr info too */
1438 bzero (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen,
1439 sizeof (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen));
1440 #endif
1441 }
1442 /* The C_FCN doesn't need any additional information.
1443 I don't even know if this is needed for sdb. But the
1444 standard assembler generates it, so...
1445 */
1446 if (S_GET_STORAGE_CLASS (symbolP) == C_EFCN)
1447 {
1448 if (last_functionP == (symbolS *) 0)
1449 as_fatal ("C_EFCN symbol out of scope");
1450 SA_SET_SYM_FSIZE (last_functionP,
1451 (long) (S_GET_VALUE (symbolP) -
1452 S_GET_VALUE (last_functionP)));
1453 SA_SET_SYM_ENDNDX (last_functionP, symbol_number);
1454 last_functionP = (symbolS *) 0;
1455 }
1456 }
1457 }
1458 else if (SF_GET_TAG (symbolP))
1459 {
1460 /* First descriptor of a structure must point to
1461 the first slot after the structure description. */
1462 last_tagP = symbolP;
1463
1464 }
1465 else if (S_GET_STORAGE_CLASS (symbolP) == C_EOS)
1466 {
1467 /* +2 take in account the current symbol */
1468 SA_SET_SYM_ENDNDX (last_tagP, symbol_number + 2);
1469 }
1470 else if (S_GET_STORAGE_CLASS (symbolP) == C_FILE)
1471 {
1472 if (S_GET_VALUE (symbolP))
1473 {
1474 S_SET_VALUE ((symbolS *) S_GET_VALUE (symbolP), symbol_number);
1475 S_SET_VALUE (symbolP, 0);
1476 } /* no one points at the first .file symbol */
1477 } /* if debug or tag or eos or file */
1478
1479 /* We must put the external symbols apart. The loader
1480 does not bomb if we do not. But the references in
1481 the endndx field for a .bb symbol are not corrected
1482 if an external symbol is removed between .bb and .be.
1483 I.e in the following case :
1484 [20] .bb endndx = 22
1485 [21] foo external
1486 [22] .be
1487 ld will move the symbol 21 to the end of the list but
1488 endndx will still be 22 instead of 21. */
1489
1490
1491 if (SF_GET_LOCAL (symbolP))
1492 {
1493 /* remove C_EFCN and LOCAL (L...) symbols */
1494 /* next pointer remains valid */
1495 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
1496
1497 }
1498 else if (!S_IS_DEFINED (symbolP)
1499 && !S_IS_DEBUG (symbolP)
1500 && !SF_GET_STATICS (symbolP) &&
1501 S_GET_STORAGE_CLASS (symbolP) == C_EXT)
1502 { /* C_EXT && !SF_GET_FUNCTION(symbolP)) */
1503 /* if external, Remove from the list */
1504 symbolS *hold = symbol_previous (symbolP);
1505
1506 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
1507 symbol_clear_list_pointers (symbolP);
1508 symbol_append (symbolP, symbol_extern_lastP, &symbol_externP, &symbol_extern_lastP);
1509 symbolP = hold;
1510 }
1511 else
1512 {
1513 if (SF_GET_STRING (symbolP))
1514 {
1515 symbolP->sy_name_offset = string_byte_count;
1516 string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
1517 }
1518 else
1519 {
1520 symbolP->sy_name_offset = 0;
1521 } /* fix "long" names */
1522
1523 symbolP->sy_number = symbol_number;
1524 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
1525 } /* if local symbol */
1526 } /* traverse the symbol list */
1527 return symbol_number;
1528
1529 }
1530
1531
1532 static unsigned int
1533 DEFUN_VOID (glue_symbols)
1534 {
1535 unsigned int symbol_number = 0;
1536 symbolS *symbolP;
1537 for (symbolP = symbol_externP; symbol_externP;)
1538 {
1539 symbolS *tmp = symbol_externP;
1540
1541 /* append */
1542 symbol_remove (tmp, &symbol_externP, &symbol_extern_lastP);
1543 symbol_append (tmp, symbol_lastP, &symbol_rootP, &symbol_lastP);
1544
1545 /* and process */
1546 if (SF_GET_STRING (tmp))
1547 {
1548 tmp->sy_name_offset = string_byte_count;
1549 string_byte_count += strlen (S_GET_NAME (tmp)) + 1;
1550 }
1551 else
1552 {
1553 tmp->sy_name_offset = 0;
1554 } /* fix "long" names */
1555
1556 tmp->sy_number = symbol_number;
1557 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (tmp);
1558 } /* append the entire extern chain */
1559 return symbol_number;
1560
1561 }
1562
1563 static unsigned int
1564 DEFUN_VOID (tie_tags)
1565 {
1566 unsigned int symbol_number = 0;
1567
1568 symbolS *symbolP;
1569 for (symbolP = symbol_rootP; symbolP; symbolP =
1570 symbol_next (symbolP))
1571 {
1572 symbolP->sy_number = symbol_number;
1573
1574
1575
1576 if (SF_GET_TAGGED (symbolP))
1577 {
1578 SA_SET_SYM_TAGNDX
1579 (symbolP,
1580 ((symbolS *) SA_GET_SYM_TAGNDX (symbolP))->sy_number);
1581 }
1582
1583 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
1584 }
1585 return symbol_number;
1586
1587 }
1588
1589 static void
1590 DEFUN (crawl_symbols, (headers, abfd),
1591 struct internal_filehdr *headers AND
1592 bfd * abfd)
1593 {
1594
1595 unsigned int i;
1596 unsigned int ptr = 0;
1597
1598
1599 symbolS *symbolP;
1600
1601 /* Initialize the stack used to keep track of the matching .bb .be */
1602
1603 block_stack = stack_init (512, sizeof (symbolS *));
1604 /* JF deal with forward references first... */
1605 for (symbolP = symbol_rootP;
1606 symbolP;
1607 symbolP = symbol_next (symbolP))
1608 {
1609
1610 if (symbolP->sy_forward)
1611 {
1612 S_SET_VALUE (symbolP, (S_GET_VALUE (symbolP)
1613 + S_GET_VALUE (symbolP->sy_forward)
1614 + symbolP->sy_forward->sy_frag->fr_address));
1615
1616 if (SF_GET_GET_SEGMENT (symbolP))
1617 {
1618 S_SET_SEGMENT (symbolP, S_GET_SEGMENT (symbolP->sy_forward));
1619 } /* forward segment also */
1620
1621 symbolP->sy_forward = 0;
1622 } /* if it has a forward reference */
1623 } /* walk the symbol chain */
1624
1625
1626 /* The symbol list should be ordered according to the following sequence
1627 * order :
1628 * . .file symbol
1629 * . debug entries for functions
1630 * . fake symbols for the sections, including.text .data and .bss
1631 * . defined symbols
1632 * . undefined symbols
1633 * But this is not mandatory. The only important point is to put the
1634 * undefined symbols at the end of the list.
1635 */
1636
1637 if (symbol_rootP == NULL
1638 || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE)
1639 {
1640 c_dot_file_symbol ("fake");
1641 }
1642 /* Is there a .file symbol ? If not insert one at the beginning. */
1643
1644 /*
1645 * Build up static symbols for the sections, they are filled in later
1646 */
1647
1648
1649 for (i = SEG_E0; i < SEG_E9; i++)
1650 {
1651 if (segment_info[i].scnhdr.s_name[0])
1652 {
1653 segment_info[i].dot =
1654 c_section_symbol (segment_info[i].scnhdr.s_name,
1655 i - SEG_E0 + 1);
1656
1657 }
1658 }
1659
1660
1661 /* Take all the externals out and put them into another chain */
1662 headers->f_nsyms = yank_symbols ();
1663 /* Take the externals and glue them onto the end.*/
1664 headers->f_nsyms += glue_symbols ();
1665
1666 headers->f_nsyms = tie_tags ();
1667 know (symbol_externP == NULL);
1668 know (symbol_extern_lastP == NULL);
1669
1670 return;
1671 }
1672
1673 /*
1674 * Find strings by crawling along symbol table chain.
1675 */
1676
1677 void
1678 DEFUN (w_strings, (where),
1679 char *where)
1680 {
1681 symbolS *symbolP;
1682
1683 /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
1684 md_number_to_chars (where, string_byte_count, sizeof (string_byte_count));
1685 where += sizeof (string_byte_count);
1686 for (symbolP = symbol_rootP;
1687 symbolP;
1688 symbolP = symbol_next (symbolP))
1689 {
1690 unsigned int size;
1691
1692 if (SF_GET_STRING (symbolP))
1693 {
1694 size = strlen (S_GET_NAME (symbolP)) + 1;
1695
1696 memcpy (where, S_GET_NAME (symbolP), size);
1697 where += size;
1698
1699 }
1700 }
1701
1702 }
1703
1704
1705
1706
1707
1708 static void
1709 DEFUN (do_linenos_for, (abfd, file_cursor),
1710 bfd * abfd AND
1711 unsigned long *file_cursor)
1712 {
1713 unsigned int idx;
1714
1715 for (idx = SEG_E0; idx < SEG_E9; idx++)
1716 {
1717 segment_info_type *s = segment_info + idx;
1718
1719
1720 if (s->scnhdr.s_nlnno != 0)
1721 {
1722 struct lineno_list *line_ptr;
1723
1724 struct external_lineno *buffer =
1725 (struct external_lineno *) xmalloc (s->scnhdr.s_nlnno * LINESZ);
1726
1727 struct external_lineno *dst = buffer;
1728
1729 /* Run through the table we've built and turn it into its external
1730 form, take this chance to remove duplicates */
1731
1732 for (line_ptr = s->lineno_list_head;
1733 line_ptr != (struct lineno_list *) NULL;
1734 line_ptr = line_ptr->next)
1735 {
1736
1737 if (line_ptr->line.l_lnno == 0)
1738 {
1739 /* Turn a pointer to a symbol into the symbols' index */
1740 line_ptr->line.l_addr.l_symndx =
1741 ((symbolS *) line_ptr->line.l_addr.l_symndx)->sy_number;
1742 }
1743 else
1744 {
1745 line_ptr->line.l_addr.l_paddr += ((struct frag *) (line_ptr->frag))->fr_address;
1746 }
1747
1748
1749 (void) bfd_coff_swap_lineno_out (abfd, &(line_ptr->line), dst);
1750 dst++;
1751
1752 }
1753
1754 s->scnhdr.s_lnnoptr = *file_cursor;
1755
1756 bfd_write (buffer, 1, s->scnhdr.s_nlnno * LINESZ, abfd);
1757 free (buffer);
1758
1759 *file_cursor += s->scnhdr.s_nlnno * LINESZ;
1760 }
1761 }
1762 }
1763
1764
1765 /* Now we run through the list of frag chains in a segment and
1766 make all the subsegment frags appear at the end of the
1767 list, as if the seg 0 was extra long */
1768
1769 static void
1770 DEFUN_VOID (remove_subsegs)
1771 {
1772 unsigned int i;
1773
1774 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1775 {
1776 frchainS *head = segment_info[i].frchainP;
1777 fragS dummy;
1778 fragS *prev_frag = &dummy;
1779
1780 while (head && head->frch_seg == i)
1781 {
1782 prev_frag->fr_next = head->frch_root;
1783 prev_frag = head->frch_last;
1784 head = head->frch_next;
1785 }
1786 prev_frag->fr_next = 0;
1787 }
1788 }
1789
1790 int machine;
1791 int coff_flags;
1792 extern void
1793 DEFUN_VOID (write_object_file)
1794 {
1795 int i;
1796 struct frchain *frchain_ptr;
1797
1798 struct internal_filehdr filehdr;
1799 struct internal_aouthdr aouthdr;
1800 unsigned long file_cursor;
1801 bfd *abfd;
1802 unsigned int addr;
1803 abfd = bfd_openw (out_file_name, TARGET_FORMAT);
1804
1805
1806 if (abfd == 0)
1807 {
1808 as_perror ("FATAL: Can't create %s", out_file_name);
1809 exit (42);
1810 }
1811 bfd_set_format (abfd, bfd_object);
1812 bfd_set_arch_mach (abfd, BFD_ARCH, machine);
1813
1814
1815
1816 string_byte_count = 4;
1817
1818 for (frchain_ptr = frchain_root;
1819 frchain_ptr != (struct frchain *) NULL;
1820 frchain_ptr = frchain_ptr->frch_next)
1821 {
1822 /* Run through all the sub-segments and align them up. Also close any
1823 open frags. We tack a .fill onto the end of the frag chain so
1824 that any .align's size can be worked by looking at the next
1825 frag */
1826
1827 subseg_new (frchain_ptr->frch_seg, frchain_ptr->frch_subseg);
1828 #ifndef SUB_SEGMENT_ALIGN
1829 #define SUB_SEGMENT_ALIGN 1
1830 #endif
1831 frag_align (SUB_SEGMENT_ALIGN, NOP_OPCODE);
1832 frag_wane (frag_now);
1833 frag_now->fr_fix = 0;
1834 know (frag_now->fr_next == NULL);
1835 }
1836
1837
1838 remove_subsegs ();
1839
1840
1841 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1842 {
1843 relax_segment (segment_info[i].frchainP->frch_root, i);
1844 }
1845
1846 filehdr.f_nscns = 0;
1847
1848 /* Find out how big the sections are, and set the addresses. */
1849 addr = 0;
1850 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1851 {
1852 segment_info[i].scnhdr.s_paddr = addr;
1853 segment_info[i].scnhdr.s_vaddr = addr;
1854
1855 if (segment_info[i].scnhdr.s_name[0])
1856 {
1857 filehdr.f_nscns++;
1858 }
1859
1860 /* Supposedly setting segment addresses non-zero
1861 causes problems for some platforms, although it
1862 shouldn't. If you define ZERO_BASED_SEGMENTS, all
1863 the segments will be based at 0. Please don't make
1864 this the default, since some systems (e.g., SVR3.2)
1865 require the segments to be non-zero based. Ian
1866 Taylor <ian@cygnus.com>. */
1867
1868 if (i == SEG_E2)
1869 {
1870 /* This is a special case, we leave the size alone, which
1871 will have been made up from all and any lcomms seen. */
1872 #ifndef ZERO_BASED_SEGMENTS
1873 addr += segment_info[i].scnhdr.s_size;
1874 #endif
1875 }
1876 else
1877 {
1878 #ifndef ZERO_BASED_SEGMENTS
1879 addr += size_section (abfd, i);
1880 #else
1881 size_section (abfd, i);
1882 #endif
1883 }
1884 }
1885
1886
1887
1888 /* Turn the gas native symbol table shape into a coff symbol table */
1889 crawl_symbols (&filehdr, abfd);
1890 #if !defined(TC_H8300) && !defined(TC_Z8K)
1891 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1892 {
1893 fixup_mdeps (segment_info[i].frchainP->frch_root);
1894 fixup_segment (&segment_info[i], i);
1895 }
1896 #endif
1897
1898 file_cursor = FILHSZ + SCNHSZ * filehdr.f_nscns;
1899
1900 bfd_seek (abfd, file_cursor, 0);
1901
1902
1903 do_relocs_for (abfd, &file_cursor);
1904
1905 do_linenos_for (abfd, &file_cursor);
1906
1907
1908 /* Plant the data */
1909
1910 fill_section (abfd, &filehdr, &file_cursor);
1911
1912
1913
1914 filehdr.f_magic = COFF_MAGIC;
1915 filehdr.f_timdat = time (0);
1916 filehdr.f_flags = COFF_FLAGS | coff_flags;
1917
1918 if (!had_lineno)
1919 {
1920 filehdr.f_flags |= F_LNNO;
1921 }
1922 if (!had_reloc)
1923 {
1924 filehdr.f_flags |= F_RELFLG;
1925 }
1926
1927
1928
1929
1930
1931
1932
1933 {
1934
1935 unsigned int symtable_size = filehdr.f_nsyms * SYMESZ;
1936 char *buffer1 = malloc (symtable_size + string_byte_count + 4);
1937 char *ptr = buffer1;
1938 filehdr.f_symptr = bfd_tell (abfd);
1939 w_symbols (abfd, buffer1, symbol_rootP);
1940 w_strings (buffer1 + symtable_size);
1941 bfd_write (buffer1, 1, symtable_size + string_byte_count + 4, abfd);
1942 free (buffer1);
1943
1944 }
1945 coff_header_append (abfd, &filehdr, &aouthdr);
1946
1947 if (bfd_close_all_done (abfd) == false)
1948 as_fatal ("Can't close %s: %s", out_file_name,
1949 bfd_errmsg (bfd_error));
1950 }
1951
1952
1953 static void
1954 DEFUN (change_to_section, (name, len, exp),
1955 char *name AND
1956 unsigned int len AND
1957 unsigned int exp)
1958 {
1959 unsigned int i;
1960 /* Find out if we've already got a section of this name etc */
1961 for (i = SEG_E0; i < SEG_E9 && segment_info[i].scnhdr.s_name[0]; i++)
1962 {
1963 if (strncmp (segment_info[i].scnhdr.s_name, name, len) == 0)
1964 {
1965 subseg_new (i, exp);
1966 return;
1967
1968 }
1969 }
1970 /* No section, add one */
1971 strncpy (segment_info[i].scnhdr.s_name, name, 8);
1972 subseg_new (i, exp);
1973 }
1974
1975 void
1976 DEFUN_VOID (obj_coff_section)
1977 {
1978 /* Strip out the section name */
1979 char *section_name;
1980 char *section_name_end;
1981 char c;
1982
1983 unsigned int len;
1984 unsigned int exp;
1985
1986 section_name = input_line_pointer;
1987 c = get_symbol_end ();
1988 section_name_end = input_line_pointer;
1989
1990 len = section_name_end - section_name;
1991 input_line_pointer++;
1992 SKIP_WHITESPACE ();
1993
1994 /* Some 386 assemblers stick a quoted string at the end of
1995 a .section; we just ignore it. */
1996 if (c == ',' && *input_line_pointer != '"')
1997 {
1998 exp = get_absolute_expression ();
1999 }
2000 else if (*input_line_pointer == ','
2001 && input_line_pointer[1] != '"')
2002 {
2003 input_line_pointer++;
2004 exp = get_absolute_expression ();
2005 }
2006 else
2007 {
2008 exp = 0;
2009 while (! is_end_of_line[*input_line_pointer])
2010 ++input_line_pointer;
2011 }
2012
2013 change_to_section (section_name, len, exp);
2014 *section_name_end = c;
2015 }
2016
2017
2018 static void
2019 obj_coff_text ()
2020 {
2021 change_to_section (".text", 5, get_absolute_expression ());
2022 }
2023
2024
2025 static void
2026 obj_coff_data ()
2027 {
2028 change_to_section (".data", 5, get_absolute_expression ());
2029 }
2030
2031 void
2032 c_symbol_merge (debug, normal)
2033 symbolS *debug;
2034 symbolS *normal;
2035 {
2036 S_SET_DATA_TYPE (normal, S_GET_DATA_TYPE (debug));
2037 S_SET_STORAGE_CLASS (normal, S_GET_STORAGE_CLASS (debug));
2038
2039 if (S_GET_NUMBER_AUXILIARY (debug) > S_GET_NUMBER_AUXILIARY (normal))
2040 {
2041 S_SET_NUMBER_AUXILIARY (normal, S_GET_NUMBER_AUXILIARY (debug));
2042 } /* take the most we have */
2043
2044 if (S_GET_NUMBER_AUXILIARY (debug) > 0)
2045 {
2046 memcpy ((char *) &normal->sy_symbol.ost_auxent[0], (char *) &debug->sy_symbol.ost_auxent[0], S_GET_NUMBER_AUXILIARY (debug) * AUXESZ);
2047 } /* Move all the auxiliary information */
2048
2049 /* Move the debug flags. */
2050 SF_SET_DEBUG_FIELD (normal, SF_GET_DEBUG_FIELD (debug));
2051 } /* c_symbol_merge() */
2052
2053 static int
2054 DEFUN (c_line_new, (symbol, paddr, line_number, frag),
2055 symbolS * symbol AND
2056 long paddr AND
2057 unsigned short line_number AND
2058 fragS * frag)
2059 {
2060 struct lineno_list *new_line =
2061 (struct lineno_list *) xmalloc (sizeof (struct lineno_list));
2062
2063 segment_info_type *s = segment_info + now_seg;
2064 new_line->line.l_lnno = line_number;
2065
2066 had_lineno = 1;
2067
2068 if (line_number == 0)
2069 {
2070 last_line_symbol = symbol;
2071 new_line->line.l_addr.l_symndx = (long) symbol;
2072 }
2073 else
2074 {
2075 new_line->line.l_addr.l_paddr = paddr;
2076 }
2077
2078 new_line->frag = (char *) frag;
2079 new_line->next = (struct lineno_list *) NULL;
2080
2081
2082 if (s->lineno_list_head == (struct lineno_list *) NULL)
2083 {
2084 s->lineno_list_head = new_line;
2085 }
2086 else
2087 {
2088 s->lineno_list_tail->next = new_line;
2089 }
2090 s->lineno_list_tail = new_line;
2091 return LINESZ * s->scnhdr.s_nlnno++;
2092 }
2093
2094 void
2095 c_dot_file_symbol (filename)
2096 char *filename;
2097 {
2098 symbolS *symbolP;
2099
2100 symbolP = symbol_new (".file",
2101 SEG_DEBUG,
2102 0,
2103 &zero_address_frag);
2104
2105 S_SET_STORAGE_CLASS (symbolP, C_FILE);
2106 S_SET_NUMBER_AUXILIARY (symbolP, 1);
2107 SA_SET_FILE_FNAME (symbolP, filename);
2108 #ifndef NO_LISTING
2109 {
2110 extern int listing;
2111 if (listing)
2112 {
2113 listing_source_file (filename);
2114 }
2115
2116 }
2117
2118 #endif
2119 SF_SET_DEBUG (symbolP);
2120 S_SET_VALUE (symbolP, (long) previous_file_symbol);
2121
2122 previous_file_symbol = symbolP;
2123
2124 /* Make sure that the symbol is first on the symbol chain */
2125 if (symbol_rootP != symbolP)
2126 {
2127 if (symbolP == symbol_lastP)
2128 {
2129 symbol_lastP = symbol_lastP->sy_previous;
2130 } /* if it was the last thing on the list */
2131
2132 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2133 symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP);
2134 symbol_rootP = symbolP;
2135 } /* if not first on the list */
2136
2137 } /* c_dot_file_symbol() */
2138
2139 /*
2140 * Build a 'section static' symbol.
2141 */
2142
2143 symbolS *
2144 c_section_symbol (name, idx)
2145 char *name;
2146 int idx;
2147 {
2148 symbolS *symbolP;
2149
2150 symbolP = symbol_new (name, idx,
2151 0,
2152 &zero_address_frag);
2153
2154 S_SET_STORAGE_CLASS (symbolP, C_STAT);
2155 S_SET_NUMBER_AUXILIARY (symbolP, 1);
2156
2157 SF_SET_STATICS (symbolP);
2158
2159 return symbolP;
2160 } /* c_section_symbol() */
2161
2162 static void
2163 DEFUN (w_symbols, (abfd, where, symbol_rootP),
2164 bfd * abfd AND
2165 char *where AND
2166 symbolS * symbol_rootP)
2167 {
2168 symbolS *symbolP;
2169 unsigned int i;
2170
2171 /* First fill in those values we have only just worked out */
2172 for (i = SEG_E0; i < SEG_E9; i++)
2173 {
2174 symbolP = segment_info[i].dot;
2175 if (symbolP)
2176 {
2177
2178 SA_SET_SCN_SCNLEN (symbolP, segment_info[i].scnhdr.s_size);
2179 SA_SET_SCN_NRELOC (symbolP, segment_info[i].scnhdr.s_nreloc);
2180 SA_SET_SCN_NLINNO (symbolP, segment_info[i].scnhdr.s_nlnno);
2181
2182 }
2183 }
2184
2185 /*
2186 * Emit all symbols left in the symbol chain.
2187 */
2188 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
2189 {
2190 /* Used to save the offset of the name. It is used to point
2191 to the string in memory but must be a file offset. */
2192 register char *temp;
2193
2194 tc_coff_symbol_emit_hook (symbolP);
2195
2196 temp = S_GET_NAME (symbolP);
2197 if (SF_GET_STRING (symbolP))
2198 {
2199 S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
2200 S_SET_ZEROES (symbolP, 0);
2201 }
2202 else
2203 {
2204 bzero (symbolP->sy_symbol.ost_entry.n_name, SYMNMLEN);
2205 strncpy (symbolP->sy_symbol.ost_entry.n_name, temp, SYMNMLEN);
2206 }
2207 where = symbol_to_chars (abfd, where, symbolP);
2208 S_SET_NAME (symbolP, temp);
2209 }
2210
2211 } /* w_symbols() */
2212
2213 static void
2214 DEFUN_VOID (obj_coff_lcomm)
2215 {
2216 char *name;
2217 char c;
2218 int temp;
2219 char *p;
2220 unsigned long vma;
2221
2222 symbolS *symbolP;
2223 name = input_line_pointer;
2224
2225
2226 c = get_symbol_end ();
2227 p = input_line_pointer;
2228 *p = c;
2229 SKIP_WHITESPACE ();
2230 if (*input_line_pointer != ',')
2231 {
2232 as_bad ("Expected comma after name");
2233 ignore_rest_of_line ();
2234 return;
2235 }
2236 if (*input_line_pointer == '\n')
2237 {
2238 as_bad ("Missing size expression");
2239 return;
2240 }
2241 input_line_pointer++;
2242 if ((temp = get_absolute_expression ()) < 0)
2243 {
2244 as_warn ("lcomm length (%d.) <0! Ignored.", temp);
2245 ignore_rest_of_line ();
2246 return;
2247 }
2248 *p = 0;
2249
2250 {
2251 /* Allocate zero static local data in the .data section now
2252 instead of the bss section as a symbol with a value */
2253 char *x;
2254 segT oldseg = now_seg;
2255 int oldsubseg = now_subseg;
2256
2257 subseg_new (SEG_DATA, 10);
2258 colon (name);
2259 frag_align (2, 0);
2260 record_alignment (SEG_DATA, 4);
2261 x = frag_var (rs_fill, 1, 1, (relax_substateT) 0, (symbolS *) 0,
2262 temp, (char *) 0);
2263 *x = 0;
2264
2265 subseg_new (oldseg, oldsubseg);
2266 }
2267 demand_empty_rest_of_line ();
2268 }
2269
2270 static void
2271 DEFUN (fixup_mdeps, (frags),
2272 fragS * frags)
2273 {
2274 while (frags)
2275 {
2276 switch (frags->fr_type)
2277 {
2278 case rs_align:
2279 case rs_org:
2280 frags->fr_type = rs_fill;
2281 frags->fr_offset =
2282 (frags->fr_next->fr_address - frags->fr_address - frags->fr_fix);
2283 break;
2284 case rs_machine_dependent:
2285 md_convert_frag (0, frags);
2286 break;
2287 default:
2288 ;
2289 }
2290 frags = frags->fr_next;
2291 }
2292
2293 }
2294
2295 #if 1
2296 static void
2297 DEFUN (fixup_segment, (fixP, this_segment_type),
2298 segment_info_type * segP AND
2299 segT this_segment_type)
2300 {
2301 register fixS * fixP;
2302 register symbolS *add_symbolP;
2303 register symbolS *sub_symbolP;
2304 register long add_number;
2305 register int size;
2306 register char *place;
2307 register long where;
2308 register char pcrel;
2309 register fragS *fragP;
2310 register segT add_symbol_segment = SEG_ABSOLUTE;
2311
2312
2313 for (fixP = segP->fix_root; fixP; fixP = fixP->fx_next)
2314 {
2315 fragP = fixP->fx_frag;
2316 know (fragP);
2317 where = fixP->fx_where;
2318 place = fragP->fr_literal + where;
2319 size = fixP->fx_size;
2320 add_symbolP = fixP->fx_addsy;
2321 #ifdef TC_I960
2322 if (fixP->fx_callj && TC_S_IS_CALLNAME (add_symbolP))
2323 {
2324 /* Relocation should be done via the
2325 associated 'bal' entry point
2326 symbol. */
2327
2328 if (!TC_S_IS_BALNAME (tc_get_bal_of_call (add_symbolP)))
2329 {
2330 as_bad ("No 'bal' entry point for leafproc %s",
2331 S_GET_NAME (add_symbolP));
2332 continue;
2333 }
2334 fixP->fx_addsy = add_symbolP = tc_get_bal_of_call (add_symbolP);
2335 } /* callj relocation */
2336 #endif
2337 sub_symbolP = fixP->fx_subsy;
2338 add_number = fixP->fx_offset;
2339 pcrel = fixP->fx_pcrel;
2340
2341 if (add_symbolP)
2342 {
2343 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
2344 } /* if there is an addend */
2345
2346 if (sub_symbolP)
2347 {
2348 if (!add_symbolP)
2349 {
2350 /* Its just -sym */
2351 if (S_GET_SEGMENT (sub_symbolP) != SEG_ABSOLUTE)
2352 {
2353 as_bad ("Negative of non-absolute symbol %s", S_GET_NAME (sub_symbolP));
2354 } /* not absolute */
2355
2356 add_number -= S_GET_VALUE (sub_symbolP);
2357 fixP->fx_subsy = 0;
2358
2359 /* if sub_symbol is in the same segment that add_symbol
2360 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
2361 }
2362 else if ((S_GET_SEGMENT (sub_symbolP) == add_symbol_segment)
2363 && (SEG_NORMAL (add_symbol_segment)
2364 || (add_symbol_segment == SEG_ABSOLUTE)))
2365 {
2366 /* Difference of 2 symbols from same segment. */
2367 /* Can't make difference of 2 undefineds: 'value' means */
2368 /* something different for N_UNDF. */
2369 #ifdef TC_I960
2370 /* Makes no sense to use the difference of 2 arbitrary symbols
2371 * as the target of a call instruction.
2372 */
2373 if (fixP->fx_callj)
2374 {
2375 as_bad ("callj to difference of 2 symbols");
2376 }
2377 #endif /* TC_I960 */
2378 add_number += S_GET_VALUE (add_symbolP) -
2379 S_GET_VALUE (sub_symbolP);
2380
2381 add_symbolP = NULL;
2382 fixP->fx_addsy = NULL;
2383 }
2384 else
2385 {
2386 /* Different segments in subtraction. */
2387 know (!(S_IS_EXTERNAL (sub_symbolP) && (S_GET_SEGMENT (sub_symbolP) == SEG_ABSOLUTE)));
2388
2389 if ((S_GET_SEGMENT (sub_symbolP) == SEG_ABSOLUTE))
2390 {
2391 add_number -= S_GET_VALUE (sub_symbolP);
2392 }
2393 else
2394 {
2395 as_bad ("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
2396 segment_name (S_GET_SEGMENT (sub_symbolP)),
2397 S_GET_NAME (sub_symbolP), fragP->fr_address + where);
2398 } /* if absolute */
2399 }
2400 } /* if sub_symbolP */
2401
2402 if (add_symbolP)
2403 {
2404 if (add_symbol_segment == this_segment_type && pcrel)
2405 {
2406 /*
2407 * This fixup was made when the symbol's segment was
2408 * SEG_UNKNOWN, but it is now in the local segment.
2409 * So we know how to do the address without relocation.
2410 */
2411 #ifdef TC_I960
2412 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
2413 * in which cases it modifies *fixP as appropriate. In the case
2414 * of a 'calls', no further work is required, and *fixP has been
2415 * set up to make the rest of the code below a no-op.
2416 */
2417 reloc_callj (fixP);
2418 #endif /* TC_I960 */
2419
2420 add_number += S_GET_VALUE (add_symbolP);
2421 add_number -= md_pcrel_from (fixP);
2422 #ifdef TC_I386
2423 /* On the 386 we must adjust by the segment
2424 vaddr as well. Ian Taylor. */
2425 add_number -= segP->scnhdr.s_vaddr;
2426 #endif
2427 pcrel = 0; /* Lie. Don't want further pcrel processing. */
2428 fixP->fx_addsy = NULL; /* No relocations please. */
2429 }
2430 else
2431 {
2432 switch (add_symbol_segment)
2433 {
2434 case SEG_ABSOLUTE:
2435 #ifdef TC_I960
2436 reloc_callj (fixP); /* See comment about reloc_callj() above*/
2437 #endif /* TC_I960 */
2438 add_number += S_GET_VALUE (add_symbolP);
2439 fixP->fx_addsy = NULL;
2440 add_symbolP = NULL;
2441 break;
2442 default:
2443
2444 add_number += S_GET_VALUE (add_symbolP) +
2445 segment_info[S_GET_SEGMENT (add_symbolP)].scnhdr.s_paddr;
2446 break;
2447
2448 case SEG_UNKNOWN:
2449 #ifdef TC_I960
2450 if ((int) fixP->fx_bit_fixP == 13)
2451 {
2452 /* This is a COBR instruction. They have only a
2453 * 13-bit displacement and are only to be used
2454 * for local branches: flag as error, don't generate
2455 * relocation.
2456 */
2457 as_bad ("can't use COBR format with external label");
2458 fixP->fx_addsy = NULL; /* No relocations please. */
2459 continue;
2460 } /* COBR */
2461 #endif /* TC_I960 */
2462 #ifdef TC_I386
2463 /* 386 COFF uses a peculiar format in
2464 which the value of a common symbol is
2465 stored in the .text segment (I've
2466 checked this on SVR3.2 and SCO 3.2.2)
2467 Ian Taylor <ian@cygnus.com>. */
2468 add_number += S_GET_VALUE (add_symbolP);
2469 #endif
2470 break;
2471
2472
2473 } /* switch on symbol seg */
2474 } /* if not in local seg */
2475 } /* if there was a + symbol */
2476
2477 if (pcrel)
2478 {
2479 add_number -= md_pcrel_from (fixP);
2480 if (add_symbolP == 0)
2481 {
2482 fixP->fx_addsy = &abs_symbol;
2483 } /* if there's an add_symbol */
2484 #ifdef TC_I386
2485 /* On the 386 we must adjust by the segment vaddr
2486 as well. Ian Taylor. */
2487 add_number -= segP->scnhdr.s_vaddr;
2488 #endif
2489 } /* if pcrel */
2490
2491 if (!fixP->fx_bit_fixP)
2492 {
2493 if ((size == 1 &&
2494 (add_number & ~0xFF) && ((add_number & ~0xFF) != (-1 & ~0xFF))) ||
2495 (size == 2 &&
2496 (add_number & ~0xFFFF) && ((add_number & ~0xFFFF) != (-1 & ~0xFFFF))))
2497 {
2498 as_bad ("Value of %d too large for field of %d bytes at 0x%x",
2499 add_number, size, fragP->fr_address + where);
2500 } /* generic error checking */
2501 #ifdef WARN_SIGNED_OVERFLOW_WORD
2502 /* Warn if a .word value is too large when treated as
2503 a signed number. We already know it is not too
2504 negative. This is to catch over-large switches
2505 generated by gcc on the 68k. */
2506 if (!flagseen['J']
2507 && size == 2
2508 && add_number > 0x7fff)
2509 as_bad ("Signed .word overflow; switch may be too large; %d at 0x%x",
2510 add_number, fragP->fr_address + where);
2511 #endif
2512 } /* not a bit fix */
2513 /* once this fix has been applied, we don't have to output anything
2514 nothing more need be done -*/
2515 md_apply_fix (fixP, add_number);
2516
2517 } /* For each fixS in this segment. */
2518
2519
2520 } /* fixup_segment() */
2521
2522 #endif
This page took 0.088573 seconds and 5 git commands to generate.