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