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