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