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