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