* frags.c (frag_more): Warn if in absolute_section.
[deliverable/binutils-gdb.git] / gas / config / obj-coff.c
CommitLineData
fecd2382 1/* coff object file format
055a75ef
KR
2 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
355afbcd 4
a39116f1 5 This file is part of GAS.
355afbcd 6
a39116f1
RP
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
355afbcd 11
a39116f1
RP
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
355afbcd 16
a39116f1
RP
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
a2a5a4fa 19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
fecd2382 20
fecd2382 21#include "as.h"
fecd2382 22#include "obstack.h"
8fb85d50 23#include "subsegs.h"
fecd2382 24
460a34e8
KR
25/* I think this is probably always correct. */
26#ifndef KEEP_RELOC_INFO
27#define KEEP_RELOC_INFO
28#endif
29
e5c5ce23
SC
30
31/* structure used to keep the filenames which
32 are too long around so that we can stick them
33 into the string table */
34struct filename_list
35{
36 char *filename;
37 struct filename_list *next;
38};
39
40static struct filename_list *filename_list_head;
41static struct filename_list *filename_list_tail;
42
8fb85d50 43const char *s_get_name PARAMS ((symbolS * s));
355afbcd 44static symbolS *def_symbol_in_progress;
fecd2382 45
8fb85d50
KR
46\f
47/* stack stuff */
48typedef struct
49 {
50 unsigned long chunk_size;
51 unsigned long element_size;
52 unsigned long size;
53 char *data;
54 unsigned long pointer;
55 }
56stack;
57
58static stack *
59stack_init (chunk_size, element_size)
60 unsigned long chunk_size;
61 unsigned long element_size;
62{
63 stack *st;
64
65 st = (stack *) malloc (sizeof (stack));
66 if (!st)
67 return 0;
68 st->data = malloc (chunk_size);
69 if (!st->data)
70 {
71 free (st);
72 return 0;
73 }
74 st->pointer = 0;
75 st->size = chunk_size;
76 st->chunk_size = chunk_size;
77 st->element_size = element_size;
78 return st;
79}
80
eb4fd16f
ILT
81#if 0
82/* Not currently used. */
8fb85d50
KR
83static void
84stack_delete (st)
85 stack *st;
86{
87 free (st->data);
88 free (st);
89}
eb4fd16f 90#endif
8fb85d50
KR
91
92static char *
93stack_push (st, element)
94 stack *st;
95 char *element;
96{
97 if (st->pointer + st->element_size >= st->size)
98 {
99 st->size += st->chunk_size;
100 if ((st->data = xrealloc (st->data, st->size)) == (char *) 0)
101 return (char *) 0;
102 }
103 memcpy (st->data + st->pointer, element, st->element_size);
104 st->pointer += st->element_size;
105 return st->data + st->pointer;
106}
107
108static char *
109stack_pop (st)
110 stack *st;
111{
112 if (st->pointer < st->element_size)
113 {
114 st->pointer = 0;
115 return (char *) 0;
116 }
117 st->pointer -= st->element_size;
118 return st->data + st->pointer;
119}
120\f
121/*
122 * Maintain a list of the tagnames of the structres.
123 */
124
eb4fd16f
ILT
125static struct hash_control *tag_hash;
126
8fb85d50
KR
127static void
128tag_init ()
129{
130 tag_hash = hash_new ();
131}
132
133static void
134tag_insert (name, symbolP)
135 const char *name;
136 symbolS *symbolP;
137{
138 const char *error_string;
139
140 if ((error_string = hash_jam (tag_hash, name, (char *) symbolP)))
141 {
142 as_fatal ("Inserting \"%s\" into structure table failed: %s",
143 name, error_string);
144 }
145}
fecd2382 146
eb4fd16f
ILT
147static symbolS *
148tag_find (name)
149 char *name;
150{
151#ifdef STRIP_UNDERSCORE
152 if (*name == '_')
153 name++;
154#endif /* STRIP_UNDERSCORE */
155 return (symbolS *) hash_find (tag_hash, name);
156}
157
8fb85d50
KR
158static symbolS *
159tag_find_or_make (name)
160 char *name;
161{
162 symbolS *symbolP;
163
164 if ((symbolP = tag_find (name)) == NULL)
165 {
166 symbolP = symbol_new (name, undefined_section,
167 0, &zero_address_frag);
168
169 tag_insert (S_GET_NAME (symbolP), symbolP);
eb4fd16f 170#ifdef BFD_ASSEMBLER
8fb85d50 171 symbol_table_insert (symbolP);
eb4fd16f 172#endif
8fb85d50
KR
173 } /* not found */
174
175 return symbolP;
176}
177
eb4fd16f
ILT
178
179
180#ifdef BFD_ASSEMBLER
181
182static void SA_SET_SYM_TAGNDX PARAMS ((symbolS *, symbolS *));
183
85051959
ILT
184#define GET_FILENAME_STRING(X) \
185((char*)(&((X)->sy_symbol.ost_auxent->x_file.x_n.x_offset))[1])
fecd2382 186
85051959
ILT
187/* @@ Ick. */
188static segT
189fetch_coff_debug_section ()
190{
191 static segT debug_section;
192 if (!debug_section)
193 {
194 CONST asymbol *s;
195 s = bfd_make_debug_symbol (stdoutput, (char *) 0, 0);
196 assert (s != 0);
197 debug_section = s->section;
198 }
199 return debug_section;
200}
201
055a75ef 202void
85051959
ILT
203SA_SET_SYM_ENDNDX (sym, val)
204 symbolS *sym;
205 symbolS *val;
206{
207 combined_entry_type *entry, *p;
208
209 entry = &coffsymbol (sym->bsym)->native[1];
210 p = coffsymbol (val->bsym)->native;
211 entry->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = p;
212 entry->fix_end = 1;
213}
214
215static void
216SA_SET_SYM_TAGNDX (sym, val)
217 symbolS *sym;
218 symbolS *val;
219{
220 combined_entry_type *entry, *p;
221
222 entry = &coffsymbol (sym->bsym)->native[1];
223 p = coffsymbol (val->bsym)->native;
224 entry->u.auxent.x_sym.x_tagndx.p = p;
225 entry->fix_tag = 1;
226}
227
98c6bbbe
KR
228static int
229S_GET_DATA_TYPE (sym)
230 symbolS *sym;
231{
232 return coffsymbol (sym->bsym)->native->u.syment.n_type;
233}
234
055a75ef 235int
98c6bbbe
KR
236S_SET_DATA_TYPE (sym, val)
237 symbolS *sym;
238 int val;
239{
240 coffsymbol (sym->bsym)->native->u.syment.n_type = val;
241 return val;
242}
243
244int
245S_GET_STORAGE_CLASS (sym)
246 symbolS *sym;
247{
248 return coffsymbol (sym->bsym)->native->u.syment.n_sclass;
249}
250
251int
252S_SET_STORAGE_CLASS (sym, val)
253 symbolS *sym;
254 int val;
255{
256 coffsymbol (sym->bsym)->native->u.syment.n_sclass = val;
257 return val;
258}
259
fecd2382
RP
260/* Merge a debug symbol containing debug information into a normal symbol. */
261
355afbcd
KR
262void
263c_symbol_merge (debug, normal)
264 symbolS *debug;
265 symbolS *normal;
fecd2382 266{
355afbcd
KR
267 S_SET_DATA_TYPE (normal, S_GET_DATA_TYPE (debug));
268 S_SET_STORAGE_CLASS (normal, S_GET_STORAGE_CLASS (debug));
269
270 if (S_GET_NUMBER_AUXILIARY (debug) > S_GET_NUMBER_AUXILIARY (normal))
85051959
ILT
271 /* take the most we have */
272 S_SET_NUMBER_AUXILIARY (normal, S_GET_NUMBER_AUXILIARY (debug));
355afbcd
KR
273
274 if (S_GET_NUMBER_AUXILIARY (debug) > 0)
275 {
85051959 276 /* Move all the auxiliary information. */
85051959
ILT
277 /* @@ How many fields do we want to preserve? Would it make more
278 sense to pick and choose those we want to copy? Should look
279 into this further.... [raeburn:19920512.2209EST] */
280 alent *linenos;
281 linenos = coffsymbol (normal->bsym)->lineno;
282 memcpy ((char *) &coffsymbol (normal->bsym)->native,
283 (char *) &coffsymbol (debug->bsym)->native,
284 S_GET_NUMBER_AUXILIARY(debug) * AUXESZ);
285 coffsymbol (normal->bsym)->lineno = linenos;
85051959 286 }
355afbcd
KR
287
288 /* Move the debug flags. */
289 SF_SET_DEBUG_FIELD (normal, SF_GET_DEBUG_FIELD (debug));
98c6bbbe 290}
fecd2382 291
85051959 292static symbolS *previous_file_symbol;
355afbcd
KR
293void
294c_dot_file_symbol (filename)
295 char *filename;
fecd2382 296{
355afbcd
KR
297 symbolS *symbolP;
298
4325bcb9 299 symbolP = symbol_new (filename, bfd_abs_section_ptr, 0, &zero_address_frag);
355afbcd
KR
300
301 S_SET_STORAGE_CLASS (symbolP, C_FILE);
302 S_SET_NUMBER_AUXILIARY (symbolP, 1);
85051959 303
85051959 304 symbolP->bsym->flags = BSF_DEBUGGING;
355afbcd
KR
305
306#ifndef NO_LISTING
307 {
308 extern int listing;
309 if (listing)
310 {
311 listing_source_file (filename);
312 }
313 }
314#endif
315
355afbcd
KR
316 S_SET_VALUE (symbolP, (long) previous_file_symbol);
317
318 previous_file_symbol = symbolP;
319
320 /* Make sure that the symbol is first on the symbol chain */
321 if (symbol_rootP != symbolP)
322 {
323 if (symbolP == symbol_lastP)
324 {
325 symbol_lastP = symbol_lastP->sy_previous;
326 } /* if it was the last thing on the list */
327
328 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
329 symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP);
330 symbol_rootP = symbolP;
331 } /* if not first on the list */
85051959 332}
355afbcd 333
fecd2382
RP
334/*
335 * Build a 'section static' symbol.
336 */
337
355afbcd
KR
338char *
339c_section_symbol (name, value, length, nreloc, nlnno)
340 char *name;
341 long value;
342 long length;
343 unsigned short nreloc;
344 unsigned short nlnno;
fecd2382 345{
355afbcd
KR
346 symbolS *symbolP;
347
348 symbolP = symbol_new (name,
349 (name[1] == 't'
85051959
ILT
350 ? text_section
351 : name[1] == 'd'
352 ? data_section
353 : bss_section),
355afbcd
KR
354 value,
355 &zero_address_frag);
356
357 S_SET_STORAGE_CLASS (symbolP, C_STAT);
358 S_SET_NUMBER_AUXILIARY (symbolP, 1);
359
360 SA_SET_SCN_SCNLEN (symbolP, length);
361 SA_SET_SCN_NRELOC (symbolP, nreloc);
362 SA_SET_SCN_NLINNO (symbolP, nlnno);
363
364 SF_SET_STATICS (symbolP);
365
366 return (char *) symbolP;
98c6bbbe 367}
355afbcd 368
fecd2382
RP
369/* Line number handling */
370
4325bcb9
KR
371struct line_no {
372 struct line_no *next;
373 fragS *frag;
374 alent l;
375};
376
257df791 377int coff_line_base;
98c6bbbe 378
85051959 379/* Symbol of last function, which we should hang line#s off of. */
98c6bbbe
KR
380static symbolS *line_fsym;
381
382#define in_function() (line_fsym != 0)
383#define clear_function() (line_fsym = 0)
257df791 384#define set_function(F) (line_fsym = (F), coff_add_linesym (F))
85051959 385
98c6bbbe 386\f
355afbcd
KR
387void
388obj_symbol_new_hook (symbolP)
389 symbolS *symbolP;
fecd2382 390{
355afbcd
KR
391 char underscore = 0; /* Symbol has leading _ */
392
85051959
ILT
393 {
394 long sz = (OBJ_COFF_MAX_AUXENTRIES + 1) * sizeof (combined_entry_type);
395 char *s = (char *) bfd_alloc_by_size_t (stdoutput, sz);
396 memset (s, 0, sz);
397 coffsymbol (symbolP->bsym)->native = (combined_entry_type *) s;
398 }
85051959
ILT
399 S_SET_DATA_TYPE (symbolP, T_NULL);
400 S_SET_STORAGE_CLASS (symbolP, 0);
401 S_SET_NUMBER_AUXILIARY (symbolP, 0);
355afbcd 402
355afbcd
KR
403 if (S_IS_STRING (symbolP))
404 SF_SET_STRING (symbolP);
405 if (!underscore && S_IS_LOCAL (symbolP))
406 SF_SET_LOCAL (symbolP);
85051959 407}
fecd2382 408
98c6bbbe 409\f
fecd2382
RP
410/*
411 * Handle .ln directives.
412 */
413
85051959
ILT
414static symbolS *current_lineno_sym;
415static struct line_no *line_nos;
4325bcb9
KR
416/* @@ Blindly assume all .ln directives will be in the .text section... */
417static int n_line_nos;
85051959
ILT
418
419static void
420add_lineno (frag, offset, num)
421 fragS *frag;
422 int offset;
423 int num;
424{
425 struct line_no *new_line = (struct line_no *) bfd_alloc_by_size_t (stdoutput,
426 sizeof (struct line_no));
427 if (!current_lineno_sym)
428 {
429 abort ();
430 }
431 new_line->next = line_nos;
432 new_line->frag = frag;
433 new_line->l.line_number = num;
434 new_line->l.u.offset = offset;
435 line_nos = new_line;
4325bcb9 436 n_line_nos++;
85051959
ILT
437}
438
257df791
ILT
439void
440coff_add_linesym (sym)
85051959
ILT
441 symbolS *sym;
442{
443 if (line_nos)
444 {
85051959 445 coffsymbol (current_lineno_sym->bsym)->lineno = (alent *) line_nos;
4325bcb9 446 n_line_nos++;
85051959
ILT
447 line_nos = 0;
448 }
449 current_lineno_sym = sym;
450}
85051959 451
355afbcd 452static void
85051959
ILT
453obj_coff_ln (appline)
454 int appline;
355afbcd
KR
455{
456 int l;
85051959
ILT
457
458 if (! appline && def_symbol_in_progress != NULL)
355afbcd
KR
459 {
460 as_warn (".ln pseudo-op inside .def/.endef: ignored.");
461 demand_empty_rest_of_line ();
462 return;
85051959 463 }
355afbcd 464
85051959 465 l = get_absolute_expression ();
98c6bbbe
KR
466 if (!appline)
467 {
98c6bbbe 468 add_lineno (frag_now, frag_now_fix (), l);
98c6bbbe 469 }
355afbcd
KR
470
471#ifndef NO_LISTING
472 {
473 extern int listing;
474
475 if (listing)
476 {
85051959 477 if (! appline)
257df791 478 l += coff_line_base - 1;
85051959 479 listing_source_line (l);
355afbcd
KR
480 }
481 }
482#endif
483
484 demand_empty_rest_of_line ();
98c6bbbe 485}
fecd2382
RP
486
487/*
488 * def()
489 *
490 * Handle .def directives.
491 *
492 * One might ask : why can't we symbol_new if the symbol does not
493 * already exist and fill it with debug information. Because of
494 * the C_EFCN special symbol. It would clobber the value of the
495 * function symbol before we have a chance to notice that it is
496 * a C_EFCN. And a second reason is that the code is more clear this
497 * way. (at least I think it is :-).
498 *
499 */
500
501#define SKIP_SEMI_COLON() while (*input_line_pointer++ != ';')
502#define SKIP_WHITESPACES() while (*input_line_pointer == ' ' || \
a39116f1
RP
503 *input_line_pointer == '\t') \
504 input_line_pointer++;
fecd2382 505
355afbcd
KR
506static void
507obj_coff_def (what)
508 int what;
fecd2382 509{
355afbcd
KR
510 char name_end; /* Char after the end of name */
511 char *symbol_name; /* Name of the debug symbol */
512 char *symbol_name_copy; /* Temporary copy of the name */
513 unsigned int symbol_name_length;
355afbcd
KR
514
515 if (def_symbol_in_progress != NULL)
516 {
517 as_warn (".def pseudo-op used inside of .def/.endef: ignored.");
518 demand_empty_rest_of_line ();
519 return;
520 } /* if not inside .def/.endef */
521
522 SKIP_WHITESPACES ();
523
355afbcd 524 symbol_name = input_line_pointer;
85051959
ILT
525#ifdef STRIP_UNDERSCORE
526 if (symbol_name[0] == '_' && symbol_name[1] != 0)
527 symbol_name++;
528#endif /* STRIP_UNDERSCORE */
529
355afbcd
KR
530 name_end = get_symbol_end ();
531 symbol_name_length = strlen (symbol_name);
532 symbol_name_copy = xmalloc (symbol_name_length + 1);
533 strcpy (symbol_name_copy, symbol_name);
534
535 /* Initialize the new symbol */
85051959 536 def_symbol_in_progress = symbol_make (symbol_name_copy);
355afbcd 537 def_symbol_in_progress->sy_frag = &zero_address_frag;
98c6bbbe 538 S_SET_VALUE (def_symbol_in_progress, 0);
355afbcd
KR
539
540 if (S_IS_STRING (def_symbol_in_progress))
85051959 541 SF_SET_STRING (def_symbol_in_progress);
355afbcd
KR
542
543 *input_line_pointer = name_end;
544
545 demand_empty_rest_of_line ();
85051959 546}
fecd2382
RP
547
548unsigned int dim_index;
1f029792 549
355afbcd 550static void
8fb85d50
KR
551obj_coff_endef (ignore)
552 int ignore;
355afbcd
KR
553{
554 symbolS *symbolP;
555 /* DIM BUG FIX sac@cygnus.com */
556 dim_index = 0;
557 if (def_symbol_in_progress == NULL)
558 {
559 as_warn (".endef pseudo-op used outside of .def/.endef: ignored.");
560 demand_empty_rest_of_line ();
561 return;
562 } /* if not inside .def/.endef */
563
564 /* Set the section number according to storage class. */
565 switch (S_GET_STORAGE_CLASS (def_symbol_in_progress))
566 {
567 case C_STRTAG:
568 case C_ENTAG:
569 case C_UNTAG:
570 SF_SET_TAG (def_symbol_in_progress);
571 /* intentional fallthrough */
572 case C_FILE:
573 case C_TPDEF:
574 SF_SET_DEBUG (def_symbol_in_progress);
85051959 575 S_SET_SEGMENT (def_symbol_in_progress, fetch_coff_debug_section ());
355afbcd
KR
576 break;
577
578 case C_EFCN:
579 SF_SET_LOCAL (def_symbol_in_progress); /* Do not emit this symbol. */
580 /* intentional fallthrough */
581 case C_BLOCK:
582 SF_SET_PROCESS (def_symbol_in_progress); /* Will need processing before writing */
583 /* intentional fallthrough */
584 case C_FCN:
85051959
ILT
585 {
586 CONST char *name;
587 S_SET_SEGMENT (def_symbol_in_progress, text_section);
355afbcd 588
85051959 589 name = bfd_asymbol_name (def_symbol_in_progress->bsym);
85051959
ILT
590 if (name[1] == 'b' && name[2] == 'f')
591 {
98c6bbbe 592 if (! in_function ())
85051959 593 as_warn ("`%s' symbol without preceding function", name);
98c6bbbe 594/* SA_SET_SYM_LNNO (def_symbol_in_progress, 12345);*/
85051959
ILT
595 /* Will need relocating */
596 SF_SET_PROCESS (def_symbol_in_progress);
98c6bbbe 597 clear_function ();
85051959
ILT
598 }
599 }
355afbcd
KR
600 break;
601
fecd2382 602#ifdef C_AUTOARG
355afbcd 603 case C_AUTOARG:
fecd2382 604#endif /* C_AUTOARG */
355afbcd
KR
605 case C_AUTO:
606 case C_REG:
607 case C_MOS:
608 case C_MOE:
609 case C_MOU:
610 case C_ARG:
611 case C_REGPARM:
612 case C_FIELD:
613 case C_EOS:
614 SF_SET_DEBUG (def_symbol_in_progress);
85051959 615 S_SET_SEGMENT (def_symbol_in_progress, absolute_section);
355afbcd
KR
616 break;
617
618 case C_EXT:
619 case C_STAT:
620 case C_LABEL:
621 /* Valid but set somewhere else (s_comm, s_lcomm, colon) */
622 break;
623
624 case C_USTATIC:
625 case C_EXTDEF:
626 case C_ULABEL:
85051959
ILT
627 as_warn ("unexpected storage class %d",
628 S_GET_STORAGE_CLASS (def_symbol_in_progress));
355afbcd
KR
629 break;
630 } /* switch on storage class */
631
85051959
ILT
632 /* Now that we have built a debug symbol, try to find if we should
633 merge with an existing symbol or not. If a symbol is C_EFCN or
634 SEG_ABSOLUTE or untagged SEG_DEBUG it never merges. */
635
636 /* Two cases for functions. Either debug followed by definition or
637 definition followed by debug. For definition first, we will
638 merge the debug symbol into the definition. For debug first, the
639 lineno entry MUST point to the definition function or else it
640 will point off into space when obj_crawl_symbol_chain() merges
641 the debug symbol into the real symbol. Therefor, let's presume
642 the debug symbol is a real function reference. */
643
644 /* FIXME-SOON If for some reason the definition label/symbol is
645 never seen, this will probably leave an undefined symbol at link
646 time. */
355afbcd
KR
647
648 if (S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_EFCN
85051959
ILT
649 || (!strcmp (bfd_get_section_name (stdoutput,
650 S_GET_SEGMENT (def_symbol_in_progress)),
651 "*DEBUG*")
652 && !SF_GET_TAG (def_symbol_in_progress))
85051959 653 || S_GET_SEGMENT (def_symbol_in_progress) == absolute_section
355afbcd
KR
654 || (symbolP = symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP)) == NULL)
655 {
85051959
ILT
656 if (def_symbol_in_progress != symbol_lastP)
657 symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP,
658 &symbol_lastP);
355afbcd
KR
659 }
660 else
661 {
85051959
ILT
662 /* This symbol already exists, merge the newly created symbol
663 into the old one. This is not mandatory. The linker can
664 handle duplicate symbols correctly. But I guess that it save
665 a *lot* of space if the assembly file defines a lot of
666 symbols. [loic] */
355afbcd 667
85051959
ILT
668 /* The debug entry (def_symbol_in_progress) is merged into the
669 previous definition. */
355afbcd
KR
670
671 c_symbol_merge (def_symbol_in_progress, symbolP);
672 /* FIXME-SOON Should *def_symbol_in_progress be free'd? xoxorich. */
673 def_symbol_in_progress = symbolP;
674
675 if (SF_GET_FUNCTION (def_symbol_in_progress)
676 || SF_GET_TAG (def_symbol_in_progress))
677 {
85051959
ILT
678 /* For functions, and tags, the symbol *must* be where the
679 debug symbol appears. Move the existing symbol to the
680 current place. */
355afbcd
KR
681 /* If it already is at the end of the symbol list, do nothing */
682 if (def_symbol_in_progress != symbol_lastP)
683 {
684 symbol_remove (def_symbol_in_progress, &symbol_rootP, &symbol_lastP);
685 symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP, &symbol_lastP);
85051959
ILT
686 }
687 }
688 }
355afbcd
KR
689
690 if (SF_GET_TAG (def_symbol_in_progress)
691 && symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP) == NULL)
692 {
693 tag_insert (S_GET_NAME (def_symbol_in_progress), def_symbol_in_progress);
85051959 694 }
355afbcd
KR
695
696 if (SF_GET_FUNCTION (def_symbol_in_progress))
697 {
698 know (sizeof (def_symbol_in_progress) <= sizeof (long));
98c6bbbe 699 set_function (def_symbol_in_progress);
355afbcd
KR
700 SF_SET_PROCESS (def_symbol_in_progress);
701
702 if (symbolP == NULL)
703 {
85051959
ILT
704 /* That is, if this is the first time we've seen the
705 function... */
355afbcd 706 symbol_table_insert (def_symbol_in_progress);
98c6bbbe
KR
707 } /* definition follows debug */
708 } /* Create the line number entry pointing to the function being defined */
355afbcd
KR
709
710 def_symbol_in_progress = NULL;
711 demand_empty_rest_of_line ();
85051959 712}
355afbcd
KR
713
714static void
8fb85d50
KR
715obj_coff_dim (ignore)
716 int ignore;
355afbcd 717{
98c6bbbe 718 int dim_index;
355afbcd
KR
719
720 if (def_symbol_in_progress == NULL)
721 {
722 as_warn (".dim pseudo-op used outside of .def/.endef: ignored.");
723 demand_empty_rest_of_line ();
724 return;
725 } /* if not inside .def/.endef */
726
727 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
728
729 for (dim_index = 0; dim_index < DIMNUM; dim_index++)
730 {
731 SKIP_WHITESPACES ();
98c6bbbe
KR
732 SA_SET_SYM_DIMEN (def_symbol_in_progress, dim_index,
733 get_absolute_expression ());
355afbcd
KR
734
735 switch (*input_line_pointer)
736 {
355afbcd
KR
737 case ',':
738 input_line_pointer++;
739 break;
740
741 default:
742 as_warn ("badly formed .dim directive ignored");
743 /* intentional fallthrough */
744 case '\n':
745 case ';':
746 dim_index = DIMNUM;
747 break;
98c6bbbe
KR
748 }
749 }
355afbcd
KR
750
751 demand_empty_rest_of_line ();
98c6bbbe 752}
355afbcd
KR
753
754static void
8fb85d50
KR
755obj_coff_line (ignore)
756 int ignore;
355afbcd
KR
757{
758 int this_base;
759
760 if (def_symbol_in_progress == NULL)
761 {
98c6bbbe 762 /* Probably stabs-style line? */
85051959 763 obj_coff_ln (0);
355afbcd 764 return;
98c6bbbe 765 }
355afbcd
KR
766
767 this_base = get_absolute_expression ();
4325bcb9 768 if (!strcmp (".bf", S_GET_NAME (def_symbol_in_progress)))
257df791 769 coff_line_base = this_base;
355afbcd
KR
770
771 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
257df791 772 SA_SET_SYM_LNNO (def_symbol_in_progress, coff_line_base);
355afbcd
KR
773
774 demand_empty_rest_of_line ();
3f8416d5
ILT
775
776#ifndef NO_LISTING
777 if (strcmp (".bf", S_GET_NAME (def_symbol_in_progress)) == 0)
778 {
779 extern int listing;
780
781 if (listing)
782 listing_source_line ((unsigned int) coff_line_base);
783 }
784#endif
98c6bbbe 785}
355afbcd
KR
786
787static void
8fb85d50
KR
788obj_coff_size (ignore)
789 int ignore;
355afbcd
KR
790{
791 if (def_symbol_in_progress == NULL)
792 {
793 as_warn (".size pseudo-op used outside of .def/.endef ignored.");
794 demand_empty_rest_of_line ();
795 return;
796 } /* if not inside .def/.endef */
797
798 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
799 SA_SET_SYM_SIZE (def_symbol_in_progress, get_absolute_expression ());
800 demand_empty_rest_of_line ();
98c6bbbe 801}
355afbcd
KR
802
803static void
8fb85d50
KR
804obj_coff_scl (ignore)
805 int ignore;
355afbcd
KR
806{
807 if (def_symbol_in_progress == NULL)
808 {
809 as_warn (".scl pseudo-op used outside of .def/.endef ignored.");
810 demand_empty_rest_of_line ();
811 return;
812 } /* if not inside .def/.endef */
813
814 S_SET_STORAGE_CLASS (def_symbol_in_progress, get_absolute_expression ());
815 demand_empty_rest_of_line ();
98c6bbbe 816}
355afbcd
KR
817
818static void
8fb85d50
KR
819obj_coff_tag (ignore)
820 int ignore;
a39116f1 821{
355afbcd
KR
822 char *symbol_name;
823 char name_end;
824
825 if (def_symbol_in_progress == NULL)
826 {
827 as_warn (".tag pseudo-op used outside of .def/.endef ignored.");
828 demand_empty_rest_of_line ();
829 return;
98c6bbbe 830 }
355afbcd
KR
831
832 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
833 symbol_name = input_line_pointer;
834 name_end = get_symbol_end ();
835
98c6bbbe
KR
836 /* Assume that the symbol referred to by .tag is always defined.
837 This was a bad assumption. I've added find_or_make. xoxorich. */
85051959 838 SA_SET_SYM_TAGNDX (def_symbol_in_progress,
98c6bbbe 839 tag_find_or_make (symbol_name));
355afbcd
KR
840 if (SA_GET_SYM_TAGNDX (def_symbol_in_progress) == 0L)
841 {
842 as_warn ("tag not found for .tag %s", symbol_name);
843 } /* not defined */
844
845 SF_SET_TAGGED (def_symbol_in_progress);
846 *input_line_pointer = name_end;
847
848 demand_empty_rest_of_line ();
98c6bbbe 849}
355afbcd
KR
850
851static void
8fb85d50
KR
852obj_coff_type (ignore)
853 int ignore;
355afbcd
KR
854{
855 if (def_symbol_in_progress == NULL)
856 {
857 as_warn (".type pseudo-op used outside of .def/.endef ignored.");
858 demand_empty_rest_of_line ();
859 return;
860 } /* if not inside .def/.endef */
861
862 S_SET_DATA_TYPE (def_symbol_in_progress, get_absolute_expression ());
863
864 if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress)) &&
865 S_GET_STORAGE_CLASS (def_symbol_in_progress) != C_TPDEF)
866 {
867 SF_SET_FUNCTION (def_symbol_in_progress);
868 } /* is a function */
869
870 demand_empty_rest_of_line ();
98c6bbbe 871}
355afbcd
KR
872
873static void
8fb85d50
KR
874obj_coff_val (ignore)
875 int ignore;
355afbcd
KR
876{
877 if (def_symbol_in_progress == NULL)
878 {
879 as_warn (".val pseudo-op used outside of .def/.endef ignored.");
880 demand_empty_rest_of_line ();
881 return;
882 } /* if not inside .def/.endef */
883
884 if (is_name_beginner (*input_line_pointer))
885 {
886 char *symbol_name = input_line_pointer;
887 char name_end = get_symbol_end ();
888
889 if (!strcmp (symbol_name, "."))
890 {
891 def_symbol_in_progress->sy_frag = frag_now;
8fb85d50 892 S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ());
355afbcd
KR
893 /* If the .val is != from the .def (e.g. statics) */
894 }
895 else if (strcmp (S_GET_NAME (def_symbol_in_progress), symbol_name))
896 {
98c6bbbe 897 def_symbol_in_progress->sy_value.X_op = O_symbol;
5868b1fe
ILT
898 def_symbol_in_progress->sy_value.X_add_symbol =
899 symbol_find_or_make (symbol_name);
98c6bbbe 900 def_symbol_in_progress->sy_value.X_op_symbol = NULL;
5868b1fe 901 def_symbol_in_progress->sy_value.X_add_number = 0;
5868b1fe
ILT
902
903 /* If the segment is undefined when the forward reference is
904 resolved, then copy the segment id from the forward
905 symbol. */
355afbcd
KR
906 SF_SET_GET_SEGMENT (def_symbol_in_progress);
907 }
908 /* Otherwise, it is the name of a non debug symbol and its value will be calculated later. */
909 *input_line_pointer = name_end;
910 }
911 else
912 {
913 S_SET_VALUE (def_symbol_in_progress, get_absolute_expression ());
914 } /* if symbol based */
915
916 demand_empty_rest_of_line ();
98c6bbbe 917}
fecd2382 918
355afbcd
KR
919void
920obj_read_begin_hook ()
921{
922 /* These had better be the same. Usually 18 bytes. */
57574979 923#ifndef BFD_HEADERS
355afbcd
KR
924 know (sizeof (SYMENT) == sizeof (AUXENT));
925 know (SYMESZ == AUXESZ);
57574979 926#endif
355afbcd 927 tag_init ();
85051959 928}
fecd2382 929
85051959 930
055a75ef
KR
931symbolS *coff_last_function;
932
85051959 933void
98c6bbbe 934coff_frob_symbol (symp, punt)
85051959
ILT
935 symbolS *symp;
936 int *punt;
937{
055a75ef 938 static symbolS *last_tagP;
85051959 939 static stack *block_stack;
055a75ef
KR
940 static symbolS *set_end;
941
942 if (symp == &abs_symbol)
943 {
944 *punt = 1;
945 return;
946 }
85051959 947
98c6bbbe 948 if (current_lineno_sym)
257df791 949 coff_add_linesym ((symbolS *) 0);
98c6bbbe 950
85051959
ILT
951 if (!block_stack)
952 block_stack = stack_init (512, sizeof (symbolS*));
953
85051959
ILT
954 if (!S_IS_DEFINED (symp) && S_GET_STORAGE_CLASS (symp) != C_STAT)
955 S_SET_STORAGE_CLASS (symp, C_EXT);
85051959
ILT
956
957 if (!SF_GET_DEBUG (symp))
958 {
959 symbolS *real;
960 if (!SF_GET_LOCAL (symp)
961 && (real = symbol_find_base (S_GET_NAME (symp), DO_NOT_STRIP))
962 && real != symp)
963 {
964 c_symbol_merge (symp, real);
965 *punt = 1;
966 }
967 if (!S_IS_DEFINED (symp) && !SF_GET_LOCAL (symp))
968 {
969 assert (S_GET_VALUE (symp) == 0);
970 S_SET_EXTERNAL (symp);
971 }
972 else if (S_GET_STORAGE_CLASS (symp) == C_NULL)
973 {
eb4fd16f
ILT
974 if (S_GET_SEGMENT (symp) == text_section
975 && symp != seg_info (text_section)->sym)
85051959
ILT
976 S_SET_STORAGE_CLASS (symp, C_LABEL);
977 else
978 S_SET_STORAGE_CLASS (symp, C_STAT);
979 }
980 if (SF_GET_PROCESS (symp))
981 {
982 if (S_GET_STORAGE_CLASS (symp) == C_BLOCK)
983 {
984 if (!strcmp (S_GET_NAME (symp), ".bb"))
985 stack_push (block_stack, (char *) &symp);
986 else
987 {
988 symbolS *begin;
989 begin = *(symbolS **) stack_pop (block_stack);
990 if (begin == 0)
991 as_warn ("mismatched .eb");
992 else
055a75ef 993 set_end = begin;
85051959
ILT
994 }
995 }
055a75ef 996 if (coff_last_function == 0 && SF_GET_FUNCTION (symp))
85051959
ILT
997 {
998 union internal_auxent *auxp;
055a75ef 999 coff_last_function = symp;
85051959
ILT
1000 if (S_GET_NUMBER_AUXILIARY (symp) < 1)
1001 S_SET_NUMBER_AUXILIARY (symp, 1);
1002 auxp = &coffsymbol (symp->bsym)->native[1].u.auxent;
1003 memset (auxp->x_sym.x_fcnary.x_ary.x_dimen, 0,
1004 sizeof (auxp->x_sym.x_fcnary.x_ary.x_dimen));
1005 }
1006 if (S_GET_STORAGE_CLASS (symp) == C_EFCN)
1007 {
055a75ef 1008 if (coff_last_function == 0)
85051959 1009 as_fatal ("C_EFCN symbol out of scope");
055a75ef 1010 SA_SET_SYM_FSIZE (coff_last_function,
85051959 1011 (long) (S_GET_VALUE (symp)
055a75ef
KR
1012 - S_GET_VALUE (coff_last_function)));
1013 set_end = coff_last_function;
1014 coff_last_function = 0;
85051959
ILT
1015 }
1016 }
1017 else if (SF_GET_TAG (symp))
1018 last_tagP = symp;
1019 else if (S_GET_STORAGE_CLASS (symp) == C_EOS)
055a75ef 1020 set_end = last_tagP;
85051959
ILT
1021 else if (S_GET_STORAGE_CLASS (symp) == C_FILE)
1022 {
1023 if (S_GET_VALUE (symp))
1024 {
1025 S_SET_VALUE ((symbolS *) S_GET_VALUE (symp), 0xdeadbeef);
1026 S_SET_VALUE (symp, 0);
1027 }
1028 }
98c6bbbe
KR
1029 if (S_IS_EXTERNAL (symp))
1030 S_SET_STORAGE_CLASS (symp, C_EXT);
1031 else if (SF_GET_LOCAL (symp))
85051959 1032 *punt = 1;
a91c6b08
ILT
1033
1034 if (SF_GET_FUNCTION (symp))
1035 symp->bsym->flags |= BSF_FUNCTION;
1036
85051959
ILT
1037 /* more ... */
1038 }
055a75ef
KR
1039
1040 if (set_end != (symbolS *) NULL
1041 && ! *punt)
1042 {
1043 SA_SET_SYM_ENDNDX (set_end, symp);
1044 set_end = NULL;
1045 }
1046
85051959
ILT
1047 if (coffsymbol (symp->bsym)->lineno)
1048 {
d675782a 1049 int i;
85051959
ILT
1050 struct line_no *lptr;
1051 alent *l;
1052
1053 lptr = (struct line_no *) coffsymbol (symp->bsym)->lineno;
1054 for (i = 0; lptr; lptr = lptr->next)
1055 i++;
85051959 1056 lptr = (struct line_no *) coffsymbol (symp->bsym)->lineno;
d675782a
KR
1057
1058 /* We need i entries for line numbers, plus 1 for the first
1059 entry which BFD will override, plus 1 for the last zero
1060 entry (a marker for BFD). */
1061 l = (alent *) bfd_alloc_by_size_t (stdoutput, (i + 2) * sizeof (alent));
85051959 1062 coffsymbol (symp->bsym)->lineno = l;
d675782a
KR
1063 l[i + 1].line_number = 0;
1064 l[i + 1].u.sym = NULL;
1065 for (; i > 0; i--)
85051959
ILT
1066 {
1067 if (lptr->frag)
1068 lptr->l.u.offset += lptr->frag->fr_address;
1069 l[i] = lptr->l;
1070 lptr = lptr->next;
1071 }
1072 }
1073}
1074
4325bcb9
KR
1075void
1076coff_adjust_section_syms (abfd, sec, x)
1077 bfd *abfd;
1078 asection *sec;
1079 PTR x;
1080{
1081 symbolS *secsym;
1082 segment_info_type *seginfo = seg_info (sec);
1083 int nlnno, nrelocs = 0;
1084
d675782a
KR
1085 /* RS/6000 gas creates a .debug section manually in ppc_frob_file in
1086 tc-ppc.c. Do not get confused by it. */
1087 if (seginfo == NULL)
1088 return;
1089
4325bcb9
KR
1090 if (!strcmp (sec->name, ".text"))
1091 nlnno = n_line_nos;
1092 else
1093 nlnno = 0;
1094 {
1095 /* @@ Hope that none of the fixups expand to more than one reloc
1096 entry... */
1097 fixS *fixp = seginfo->fix_root;
1098 while (fixp)
1099 {
1100 fixp = fixp->fx_next;
1101 nrelocs++;
1102 }
1103 }
1104 if (bfd_get_section_size_before_reloc (sec) == 0
1105 && nrelocs == 0 && nlnno == 0)
1106 return;
1107 secsym = section_symbol (sec);
1108 SA_SET_SCN_NRELOC (secsym, nrelocs);
1109 SA_SET_SCN_NLINNO (secsym, nlnno);
1110}
1111
1112void
1113coff_frob_file ()
1114{
1115 bfd_map_over_sections (stdoutput, coff_adjust_section_syms, (char*) 0);
1116}
1117
055a75ef
KR
1118/*
1119 * implement the .section pseudo op:
1120 * .section name {, "flags"}
1121 * ^ ^
1122 * | +--- optional flags: 'b' for bss
1123 * | 'i' for info
1124 * +-- section name 'l' for lib
1125 * 'n' for noload
1126 * 'o' for over
1127 * 'w' for data
1128 * 'd' (apparently m88k for data)
1129 * 'x' for text
1130 * But if the argument is not a quoted string, treat it as a
1131 * subsegment number.
1132 */
1133
1134void
1135obj_coff_section (ignore)
1136 int ignore;
85051959
ILT
1137{
1138 /* Strip out the section name */
055a75ef 1139 char *section_name;
85051959 1140 char c;
055a75ef 1141 char *name;
85051959 1142 unsigned int exp;
055a75ef
KR
1143 flagword flags;
1144 asection *sec;
85051959 1145
055a75ef
KR
1146 section_name = input_line_pointer;
1147 c = get_symbol_end ();
85051959 1148
055a75ef
KR
1149 name = xmalloc (input_line_pointer - section_name + 1);
1150 strcpy (name, section_name);
1151
1152 *input_line_pointer = c;
1153
1154 SKIP_WHITESPACE ();
1155
1156 exp = 0;
1157 flags = SEC_NO_FLAGS;
1158
1159 if (*input_line_pointer == ',')
85051959 1160 {
055a75ef
KR
1161 ++input_line_pointer;
1162 SKIP_WHITESPACE ();
1163 if (*input_line_pointer != '"')
1164 exp = get_absolute_expression ();
1165 else
1166 {
1167 ++input_line_pointer;
1168 while (*input_line_pointer != '"'
1169 && ! is_end_of_line[(unsigned char) *input_line_pointer])
1170 {
1171 switch (*input_line_pointer)
1172 {
1173 case 'b': flags |= SEC_ALLOC; flags &=~ SEC_LOAD; break;
1174 case 'n': flags &=~ SEC_LOAD; break;
1175 case 'd':
1176 case 'w': flags &=~ SEC_READONLY; break;
1177 case 'x': flags |= SEC_CODE; break;
1178
1179 case 'i': /* STYP_INFO */
1180 case 'l': /* STYP_LIB */
1181 case 'o': /* STYP_OVER */
1182 as_warn ("unsupported section attribute '%c'",
1183 *input_line_pointer);
1184 break;
1185
1186 default:
1187 as_warn("unknown section attribute '%c'",
1188 *input_line_pointer);
1189 break;
1190 }
1191 ++input_line_pointer;
1192 }
1193 if (*input_line_pointer == '"')
1194 ++input_line_pointer;
1195 }
85051959 1196 }
055a75ef
KR
1197
1198 sec = subseg_new (name, (subsegT) exp);
1199
1200 if (flags != SEC_NO_FLAGS)
85051959 1201 {
055a75ef
KR
1202 if (! bfd_set_section_flags (stdoutput, sec, flags))
1203 as_warn ("error setting flags for \"%s\": %s",
1204 bfd_section_name (stdoutput, sec),
1205 bfd_errmsg (bfd_get_error ()));
85051959 1206 }
85051959
ILT
1207}
1208
1209void
2283f8a0 1210coff_adjust_symtab ()
85051959
ILT
1211{
1212 if (symbol_rootP == NULL
1213 || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE)
1214 {
1215 assert (previous_file_symbol == 0);
1216 c_dot_file_symbol ("fake");
1217 }
85051959 1218}
71b4de6f
KR
1219
1220void
8fb85d50
KR
1221coff_frob_section (sec)
1222 segT sec;
71b4de6f 1223{
8fb85d50 1224 segT strsec;
71b4de6f
KR
1225 char *strname, *p;
1226 fragS *fragp;
8fb85d50
KR
1227 bfd_vma size, n_entries, mask;
1228
1229 /* The COFF back end in BFD requires that all section sizes be
1230 rounded up to multiples of the corresponding section alignments.
1231 Seems kinda silly to me, but that's the way it is. */
1232 size = bfd_get_section_size_before_reloc (sec);
8fb85d50
KR
1233 mask = ((bfd_vma) 1 << (bfd_vma) sec->alignment_power) - 1;
1234 if (size & mask)
4325bcb9
KR
1235 {
1236 size = (size + mask) & ~mask;
1237 bfd_set_section_size (stdoutput, sec, size);
1238 }
1239
1240 /* If the section size is non-zero, the section symbol needs an aux
1241 entry associated with it, indicating the size. We don't know
1242 all the values yet; coff_frob_symbol will fill them in later. */
1243 if (size)
1244 {
1245 symbolS *secsym = section_symbol (sec);
1246
1247 S_SET_STORAGE_CLASS (secsym, C_STAT);
1248 S_SET_NUMBER_AUXILIARY (secsym, 1);
1249 SF_SET_STATICS (secsym);
1250 SA_SET_SCN_SCNLEN (secsym, size);
1251 }
71b4de6f
KR
1252
1253 /* @@ these should be in a "stabs.h" file, or maybe as.h */
1254#ifndef STAB_SECTION_NAME
1255#define STAB_SECTION_NAME ".stab"
1256#endif
1257#ifndef STAB_STRING_SECTION_NAME
1258#define STAB_STRING_SECTION_NAME ".stabstr"
1259#endif
8fb85d50 1260 if (strcmp (STAB_STRING_SECTION_NAME, sec->name))
71b4de6f
KR
1261 return;
1262
8fb85d50 1263 strsec = sec;
71b4de6f
KR
1264 sec = subseg_get (STAB_SECTION_NAME, 0);
1265 /* size is already rounded up, since other section will be listed first */
1266 size = bfd_get_section_size_before_reloc (strsec);
1267
1268 n_entries = bfd_get_section_size_before_reloc (sec) / 12 - 1;
1269
1270 /* Find first non-empty frag. It should be large enough. */
1271 fragp = seg_info (sec)->frchainP->frch_root;
1272 while (fragp && fragp->fr_fix == 0)
1273 fragp = fragp->fr_next;
1274 assert (fragp != 0 && fragp->fr_fix >= 12);
1275
1276 /* Store the values. */
1277 p = fragp->fr_literal;
1278 bfd_h_put_16 (stdoutput, n_entries, (bfd_byte *) p + 6);
1279 bfd_h_put_32 (stdoutput, size, (bfd_byte *) p + 8);
1280}
1281
1282void
1283obj_coff_init_stab_section (seg)
1284 segT seg;
1285{
1286 char *file;
1287 char *p;
1288 char *stabstr_name;
1289 unsigned int stroff;
1290
1291 /* Make space for this first symbol. */
1292 p = frag_more (12);
1293 /* Zero it out. */
1294 memset (p, 0, 12);
1295 as_where (&file, (unsigned int *) NULL);
1296 stabstr_name = (char *) alloca (strlen (seg->name) + 4);
1297 strcpy (stabstr_name, seg->name);
1298 strcat (stabstr_name, "str");
1299 stroff = get_stab_string_offset (file, stabstr_name);
1300 know (stroff == 1);
1301 md_number_to_chars (p, stroff, 4);
1302}
85051959 1303
fecd2382 1304#ifdef DEBUG
a39116f1 1305/* for debugging */
98c6bbbe 1306const char *
355afbcd
KR
1307s_get_name (s)
1308 symbolS *s;
fecd2382 1309{
355afbcd 1310 return ((s == NULL) ? "(NULL)" : S_GET_NAME (s));
71b4de6f 1311}
355afbcd
KR
1312
1313void
1314symbol_dump ()
1315{
1316 symbolS *symbolP;
1317
1318 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
1319 {
85051959
ILT
1320 printf("0x%lx: \"%s\" type = %ld, class = %d, segment = %d\n",
1321 (unsigned long) symbolP,
1322 S_GET_NAME(symbolP),
1323 (long) S_GET_DATA_TYPE(symbolP),
1324 S_GET_STORAGE_CLASS(symbolP),
1325 (int) S_GET_SEGMENT(symbolP));
85051959
ILT
1326 }
1327}
355afbcd 1328
fecd2382
RP
1329#endif /* DEBUG */
1330
2283f8a0
KR
1331#else /* not BFD_ASSEMBLER */
1332
1333#include "frags.h"
1334/* This is needed because we include internal bfd things. */
1335#include <time.h>
d21041b3
SS
1336
1337#include "libbfd.h"
1338#include "libcoff.h"
2283f8a0
KR
1339
1340/* The NOP_OPCODE is for the alignment fill value. Fill with nop so
1341 that we can stick sections together without causing trouble. */
1342#ifndef NOP_OPCODE
1343#define NOP_OPCODE 0x00
1344#endif
1345
eb4fd16f
ILT
1346/* The zeroes if symbol name is longer than 8 chars */
1347#define S_SET_ZEROES(s,v) ((s)->sy_symbol.ost_entry.n_zeroes = (v))
1348
2283f8a0
KR
1349#define MIN(a,b) ((a) < (b)? (a) : (b))
1350/* This vector is used to turn an internal segment into a section #
1351 suitable for insertion into a coff symbol table
1352 */
1353
1354const short seg_N_TYPE[] =
1355{ /* in: segT out: N_TYPE bits */
1356 C_ABS_SECTION,
1357 1,
1358 2,
1359 3,
1360 4,
1361 5,
1362 6,
1363 7,
1364 8,
1365 9,
1366 10,
1367 C_UNDEF_SECTION, /* SEG_UNKNOWN */
1368 C_UNDEF_SECTION, /* SEG_GOOF */
1369 C_UNDEF_SECTION, /* SEG_EXPR */
1370 C_DEBUG_SECTION, /* SEG_DEBUG */
1371 C_NTV_SECTION, /* SEG_NTV */
1372 C_PTV_SECTION, /* SEG_PTV */
1373 C_REGISTER_SECTION, /* SEG_REGISTER */
1374};
1375
1376int function_lineoff = -1; /* Offset in line#s where the last function
1377 started (the odd entry for line #0) */
1378
1379static symbolS *last_line_symbol;
1380
1381/* Add 4 to the real value to get the index and compensate the
1382 negatives. This vector is used by S_GET_SEGMENT to turn a coff
1383 section number into a segment number
1384*/
eb4fd16f 1385static symbolS *previous_file_symbol;
2283f8a0
KR
1386void c_symbol_merge ();
1387static int line_base;
1388
1389symbolS *c_section_symbol ();
1390bfd *abfd;
1391
1392static void fixup_segment PARAMS ((segment_info_type *segP,
1393 segT this_segment_type));
1394
1395
1396static void fixup_mdeps PARAMS ((fragS *,
1397 object_headers *,
1398 segT));
1399
1400
1401static void fill_section PARAMS ((bfd * abfd,
1402 object_headers *,
1403 unsigned long *));
1404
1405
2283f8a0 1406static int c_line_new PARAMS ((symbolS * symbol, long paddr,
eb4fd16f 1407 int line_number,
2283f8a0
KR
1408 fragS * frag));
1409
1410
1411static void w_symbols PARAMS ((bfd * abfd, char *where,
1412 symbolS * symbol_rootP));
1413
1414static void adjust_stab_section PARAMS ((bfd *abfd, segT seg));
1415
2283f8a0
KR
1416static void obj_coff_lcomm PARAMS ((int));
1417static void obj_coff_text PARAMS ((int));
1418static void obj_coff_data PARAMS ((int));
1419static void obj_coff_bss PARAMS ((int));
1420static void obj_coff_ident PARAMS ((int));
1421void obj_coff_section PARAMS ((int));
1422
2283f8a0
KR
1423/* Section stuff
1424
1425 We allow more than just the standard 3 sections, infact, we allow
1426 10 sections, (though the usual three have to be there).
1427
1428 This structure performs the mappings for us:
1429
1430*/
1431
1432#define N_SEG 32
1433typedef struct
1434{
1435 segT seg_t;
1436 int i;
1437} seg_info_type;
1438
eb4fd16f 1439static const seg_info_type seg_info_off_by_4[N_SEG] =
2283f8a0
KR
1440{
1441 {SEG_PTV, },
1442 {SEG_NTV, },
1443 {SEG_DEBUG, },
1444 {SEG_ABSOLUTE, },
1445 {SEG_UNKNOWN, },
1446 {SEG_E0},
1447 {SEG_E1},
1448 {SEG_E2},
1449 {SEG_E3},
1450 {SEG_E4},
1451 {SEG_E5},
1452 {SEG_E6},
1453 {SEG_E7},
1454 {SEG_E8},
1455 {SEG_E9},
1456 {(segT)15},
1457 {(segT)16},
1458 {(segT)17},
1459 {(segT)18},
1460 {(segT)19},
1461 {(segT)20},
1462 {(segT)0},
1463 {(segT)0},
1464 {(segT)0},
1465 {SEG_REGISTER}
1466};
1467
1468
1469
1470#define SEG_INFO_FROM_SECTION_NUMBER(x) (seg_info_off_by_4[(x)+4])
2283f8a0
KR
1471
1472static relax_addressT
1473relax_align (address, alignment)
1474 relax_addressT address;
1475 long alignment;
1476{
1477 relax_addressT mask;
1478 relax_addressT new_address;
1479
1480 mask = ~((~0) << alignment);
1481 new_address = (address + mask) & (~mask);
1482 return (new_address - address);
eb4fd16f 1483}
2283f8a0
KR
1484
1485
1486segT
1487s_get_segment (x)
1488 symbolS * x;
1489{
1490 return SEG_INFO_FROM_SECTION_NUMBER (x->sy_symbol.ost_entry.n_scnum).seg_t;
1491}
1492
1493
1494
1495/* calculate the size of the frag chain and fill in the section header
1496 to contain all of it, also fill in the addr of the sections */
1497static unsigned int
1498size_section (abfd, idx)
1499 bfd * abfd;
1500 unsigned int idx;
1501{
1502
1503 unsigned int size = 0;
1504 fragS *frag = segment_info[idx].frchainP->frch_root;
1505 while (frag)
1506 {
1507 size = frag->fr_address;
1508 if (frag->fr_address != size)
1509 {
eb4fd16f 1510 fprintf (stderr, "Out of step\n");
2283f8a0
KR
1511 size = frag->fr_address;
1512 }
1513
1514 switch (frag->fr_type)
1515 {
1516#ifdef TC_COFF_SIZEMACHDEP
1517 case rs_machine_dependent:
1518 size += TC_COFF_SIZEMACHDEP (frag);
1519 break;
1520#endif
aac4d5a7
KR
1521 case rs_space:
1522 assert (frag->fr_symbol == 0);
2283f8a0
KR
1523 case rs_fill:
1524 case rs_org:
1525 size += frag->fr_fix;
1526 size += frag->fr_offset * frag->fr_var;
1527 break;
1528 case rs_align:
1529 size += frag->fr_fix;
1530 size += relax_align (size, frag->fr_offset);
1531 break;
1532 default:
1533 BAD_CASE (frag->fr_type);
1534 break;
1535 }
1536 frag = frag->fr_next;
1537 }
1538 segment_info[idx].scnhdr.s_size = size;
1539 return size;
1540}
1541
1542
1543static unsigned int
1544count_entries_in_chain (idx)
1545 unsigned int idx;
1546{
1547 unsigned int nrelocs;
1548 fixS *fixup_ptr;
1549
1550 /* Count the relocations */
1551 fixup_ptr = segment_info[idx].fix_root;
1552 nrelocs = 0;
1553 while (fixup_ptr != (fixS *) NULL)
1554 {
1555 if (TC_COUNT_RELOC (fixup_ptr))
1556 {
1557#ifdef TC_A29K
1558 if (fixup_ptr->fx_r_type == RELOC_CONSTH)
1559 nrelocs += 2;
1560 else
1561 nrelocs++;
1562#else
1563 nrelocs++;
1564#endif
1565 }
1566
1567 fixup_ptr = fixup_ptr->fx_next;
1568 }
1569 return nrelocs;
1570}
1571
1572/* output all the relocations for a section */
1573void
1574do_relocs_for (abfd, h, file_cursor)
1575 bfd * abfd;
1576 object_headers * h;
1577 unsigned long *file_cursor;
1578{
1579 unsigned int nrelocs;
1580 unsigned int idx;
1581 unsigned long reloc_start = *file_cursor;
1582
1583 for (idx = SEG_E0; idx < SEG_E9; idx++)
1584 {
1585 if (segment_info[idx].scnhdr.s_name[0])
1586 {
1587 struct external_reloc *ext_ptr;
1588 struct external_reloc *external_reloc_vec;
1589 unsigned int external_reloc_size;
1590 unsigned int base = segment_info[idx].scnhdr.s_paddr;
1591 fixS *fix_ptr = segment_info[idx].fix_root;
1592 nrelocs = count_entries_in_chain (idx);
1593
1594 if (nrelocs)
1595 /* Bypass this stuff if no relocs. This also incidentally
1596 avoids a SCO bug, where free(malloc(0)) tends to crash. */
1597 {
1598 external_reloc_size = nrelocs * RELSZ;
1599 external_reloc_vec =
1600 (struct external_reloc *) malloc (external_reloc_size);
1601
1602 ext_ptr = external_reloc_vec;
1603
1604 /* Fill in the internal coff style reloc struct from the
1605 internal fix list. */
1606 while (fix_ptr)
1607 {
2283f8a0
KR
1608 struct internal_reloc intr;
1609
1610 /* Only output some of the relocations */
1611 if (TC_COUNT_RELOC (fix_ptr))
1612 {
1613#ifdef TC_RELOC_MANGLE
a91c6b08
ILT
1614 TC_RELOC_MANGLE (&segment_info[idx], fix_ptr, &intr,
1615 base);
2283f8a0
KR
1616
1617#else
1618 symbolS *dot;
96d1566d 1619 symbolS *symbol_ptr = fix_ptr->fx_addsy;
2283f8a0
KR
1620
1621 intr.r_type = TC_COFF_FIX2RTYPE (fix_ptr);
1622 intr.r_vaddr =
1623 base + fix_ptr->fx_frag->fr_address + fix_ptr->fx_where;
1624
4325bcb9 1625#ifdef TC_KEEP_FX_OFFSET
2283f8a0
KR
1626 intr.r_offset = fix_ptr->fx_offset;
1627#else
1628 intr.r_offset = 0;
1629#endif
1630
1631 /* Turn the segment of the symbol into an offset. */
1632 if (symbol_ptr)
1633 {
1634 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1635 if (dot)
1636 {
1637 intr.r_symndx = dot->sy_number;
1638 }
1639 else
1640 {
1641 intr.r_symndx = symbol_ptr->sy_number;
1642 }
1643
1644 }
1645 else
1646 {
1647 intr.r_symndx = -1;
1648 }
1649#endif
1650
1651 (void) bfd_coff_swap_reloc_out (abfd, &intr, ext_ptr);
1652 ext_ptr++;
1653
1654#if defined(TC_A29K)
1655
1656 /* The 29k has a special kludge for the high 16 bit
1657 reloc. Two relocations are emited, R_IHIHALF,
1658 and R_IHCONST. The second one doesn't contain a
1659 symbol, but uses the value for offset. */
1660
1661 if (intr.r_type == R_IHIHALF)
1662 {
1663 /* now emit the second bit */
1664 intr.r_type = R_IHCONST;
1665 intr.r_symndx = fix_ptr->fx_addnumber;
1666 (void) bfd_coff_swap_reloc_out (abfd, &intr, ext_ptr);
1667 ext_ptr++;
1668 }
1669#endif
1670 }
1671
1672 fix_ptr = fix_ptr->fx_next;
1673 }
1674
1675 /* Write out the reloc table */
1676 bfd_write ((PTR) external_reloc_vec, 1, external_reloc_size,
1677 abfd);
1678 free (external_reloc_vec);
1679
1680 /* Fill in section header info. */
1681 segment_info[idx].scnhdr.s_relptr = *file_cursor;
1682 *file_cursor += external_reloc_size;
1683 segment_info[idx].scnhdr.s_nreloc = nrelocs;
1684 }
1685 else
1686 {
1687 /* No relocs */
1688 segment_info[idx].scnhdr.s_relptr = 0;
1689 }
1690 }
1691 }
1692 /* Set relocation_size field in file headers */
1693 H_SET_RELOCATION_SIZE (h, *file_cursor - reloc_start, 0);
1694}
1695
1696
1697/* run through a frag chain and write out the data to go with it, fill
1698 in the scnhdrs with the info on the file postions
1699*/
1700static void
1701fill_section (abfd, h, file_cursor)
1702 bfd * abfd;
1703 object_headers *h;
1704 unsigned long *file_cursor;
1705{
1706
1707 unsigned int i;
1708 unsigned int paddr = 0;
1709
1710 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1711 {
1712 unsigned int offset = 0;
2283f8a0
KR
1713 struct internal_scnhdr *s = &(segment_info[i].scnhdr);
1714
d21041b3
SS
1715 PROGRESS (1);
1716
2283f8a0
KR
1717 if (s->s_name[0])
1718 {
1719 fragS *frag = segment_info[i].frchainP->frch_root;
1720 char *buffer;
1721
1722 if (s->s_size == 0)
1723 s->s_scnptr = 0;
1724 else
1725 {
1726 buffer = xmalloc (s->s_size);
1727 s->s_scnptr = *file_cursor;
1728 }
1729 know (s->s_paddr == paddr);
1730
1731 if (strcmp (s->s_name, ".text") == 0)
1732 s->s_flags |= STYP_TEXT;
1733 else if (strcmp (s->s_name, ".data") == 0)
1734 s->s_flags |= STYP_DATA;
1735 else if (strcmp (s->s_name, ".bss") == 0)
1736 {
1737 s->s_scnptr = 0;
1738 s->s_flags |= STYP_BSS;
1739
1740 /* @@ Should make the i386 and a29k coff targets define
1741 COFF_NOLOAD_PROBLEM, and have only one test here. */
1742#ifndef TC_I386
1743#ifndef TC_A29K
1744#ifndef COFF_NOLOAD_PROBLEM
1745 /* Apparently the SVR3 linker (and exec syscall) and UDI
1746 mondfe progrem are confused by noload sections. */
1747 s->s_flags |= STYP_NOLOAD;
1748#endif
1749#endif
1750#endif
1751 }
1752 else if (strcmp (s->s_name, ".lit") == 0)
1753 s->s_flags = STYP_LIT | STYP_TEXT;
1754 else if (strcmp (s->s_name, ".init") == 0)
1755 s->s_flags |= STYP_TEXT;
1756 else if (strcmp (s->s_name, ".fini") == 0)
1757 s->s_flags |= STYP_TEXT;
1758 else if (strncmp (s->s_name, ".comment", 8) == 0)
1759 s->s_flags |= STYP_INFO;
1760
1761 while (frag)
1762 {
1763 unsigned int fill_size;
1764 switch (frag->fr_type)
1765 {
1766 case rs_machine_dependent:
1767 if (frag->fr_fix)
1768 {
1769 memcpy (buffer + frag->fr_address,
1770 frag->fr_literal,
1771 (unsigned int) frag->fr_fix);
1772 offset += frag->fr_fix;
1773 }
1774
1775 break;
aac4d5a7
KR
1776 case rs_space:
1777 assert (frag->fr_symbol == 0);
2283f8a0
KR
1778 case rs_fill:
1779 case rs_align:
1780 case rs_org:
1781 if (frag->fr_fix)
1782 {
1783 memcpy (buffer + frag->fr_address,
1784 frag->fr_literal,
1785 (unsigned int) frag->fr_fix);
1786 offset += frag->fr_fix;
1787 }
1788
1789 fill_size = frag->fr_var;
1790 if (fill_size && frag->fr_offset > 0)
1791 {
1792 unsigned int count;
1793 unsigned int off = frag->fr_fix;
1794 for (count = frag->fr_offset; count; count--)
1795 {
1796 if (fill_size + frag->fr_address + off <= s->s_size)
1797 {
1798 memcpy (buffer + frag->fr_address + off,
1799 frag->fr_literal + frag->fr_fix,
1800 fill_size);
1801 off += fill_size;
1802 offset += fill_size;
1803 }
1804 }
1805 }
1806 break;
1807 case rs_broken_word:
1808 break;
1809 default:
1810 abort ();
1811 }
1812 frag = frag->fr_next;
1813 }
1814
1815 if (s->s_size != 0)
1816 {
1817 if (s->s_scnptr != 0)
1818 {
1819 bfd_write (buffer, s->s_size, 1, abfd);
1820 *file_cursor += s->s_size;
1821 }
1822 free (buffer);
1823 }
1824 paddr += s->s_size;
1825 }
1826 }
1827}
1828
1829/* Coff file generation & utilities */
1830
1831static void
1832coff_header_append (abfd, h)
1833 bfd * abfd;
1834 object_headers * h;
1835{
1836 unsigned int i;
1837 char buffer[1000];
1838 char buffero[1000];
1839
1840 bfd_seek (abfd, 0, 0);
1841
1842#ifndef OBJ_COFF_OMIT_OPTIONAL_HEADER
1843 H_SET_MAGIC_NUMBER (h, COFF_MAGIC);
1844 H_SET_VERSION_STAMP (h, 0);
1845 H_SET_ENTRY_POINT (h, 0);
1846 H_SET_TEXT_START (h, segment_info[SEG_E0].frchainP->frch_root->fr_address);
1847 H_SET_DATA_START (h, segment_info[SEG_E1].frchainP->frch_root->fr_address);
1848 H_SET_SIZEOF_OPTIONAL_HEADER (h, bfd_coff_swap_aouthdr_out(abfd, &h->aouthdr,
1849 buffero));
1850#else /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */
1851 H_SET_SIZEOF_OPTIONAL_HEADER (h, 0);
1852#endif /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */
1853
1854 i = bfd_coff_swap_filehdr_out (abfd, &h->filehdr, buffer);
1855
1856 bfd_write (buffer, i, 1, abfd);
1857 bfd_write (buffero, H_GET_SIZEOF_OPTIONAL_HEADER (h), 1, abfd);
1858
1859 for (i = SEG_E0; i < SEG_E9; i++)
1860 {
1861 if (segment_info[i].scnhdr.s_name[0])
1862 {
1863 unsigned int size =
1864 bfd_coff_swap_scnhdr_out (abfd,
1865 &(segment_info[i].scnhdr),
1866 buffer);
5fc2a6ec
ILT
1867 if (size == 0)
1868 as_bad ("bfd_coff_swap_scnhdr_out failed");
2283f8a0
KR
1869 bfd_write (buffer, size, 1, abfd);
1870 }
1871 }
1872}
1873
1874
1875char *
1876symbol_to_chars (abfd, where, symbolP)
1877 bfd * abfd;
1878 char *where;
1879 symbolS * symbolP;
1880{
1881 unsigned int numaux = symbolP->sy_symbol.ost_entry.n_numaux;
1882 unsigned int i;
1883 valueT val;
1884
1885 /* Turn any symbols with register attributes into abs symbols */
1886 if (S_GET_SEGMENT (symbolP) == reg_section)
1887 {
1888 S_SET_SEGMENT (symbolP, absolute_section);
1889 }
1890 /* At the same time, relocate all symbols to their output value */
1891
1892 val = (segment_info[S_GET_SEGMENT (symbolP)].scnhdr.s_paddr
1893 + S_GET_VALUE (symbolP));
1894
1895 S_SET_VALUE (symbolP, val);
1896
1897 symbolP->sy_symbol.ost_entry.n_value = val;
1898
1899 where += bfd_coff_swap_sym_out (abfd, &symbolP->sy_symbol.ost_entry,
1900 where);
1901
1902 for (i = 0; i < numaux; i++)
1903 {
1904 where += bfd_coff_swap_aux_out (abfd,
1905 &symbolP->sy_symbol.ost_auxent[i],
1906 S_GET_DATA_TYPE (symbolP),
1907 S_GET_STORAGE_CLASS (symbolP),
1908 i, numaux, where);
1909 }
1910 return where;
1911
1912}
1913
1914void
1915obj_symbol_new_hook (symbolP)
1916 symbolS *symbolP;
1917{
1918 char underscore = 0; /* Symbol has leading _ */
1919
1920 /* Effective symbol */
1921 /* Store the pointer in the offset. */
1922 S_SET_ZEROES (symbolP, 0L);
1923 S_SET_DATA_TYPE (symbolP, T_NULL);
1924 S_SET_STORAGE_CLASS (symbolP, 0);
1925 S_SET_NUMBER_AUXILIARY (symbolP, 0);
1926 /* Additional information */
1927 symbolP->sy_symbol.ost_flags = 0;
1928 /* Auxiliary entries */
1929 memset ((char *) &symbolP->sy_symbol.ost_auxent[0], 0, AUXESZ);
1930
1931 if (S_IS_STRING (symbolP))
1932 SF_SET_STRING (symbolP);
1933 if (!underscore && S_IS_LOCAL (symbolP))
1934 SF_SET_LOCAL (symbolP);
1935}
1936
1937/*
1938 * Handle .ln directives.
1939 */
1940
1941static void
1942obj_coff_ln (appline)
1943 int appline;
1944{
1945 int l;
1946
1947 if (! appline && def_symbol_in_progress != NULL)
1948 {
1949 as_warn (".ln pseudo-op inside .def/.endef: ignored.");
1950 demand_empty_rest_of_line ();
1951 return;
1952 } /* wrong context */
1953
4325bcb9 1954 l = get_absolute_expression ();
87e48495 1955 c_line_new (0, frag_now_fix (), l, frag_now);
2283f8a0
KR
1956#ifndef NO_LISTING
1957 {
1958 extern int listing;
1959
1960 if (listing)
1961 {
1962 if (! appline)
1963 l += line_base - 1;
1964 listing_source_line ((unsigned int) l);
1965 }
1966
1967 }
1968#endif
1969 demand_empty_rest_of_line ();
1970}
1971
1972/*
1973 * def()
1974 *
1975 * Handle .def directives.
1976 *
1977 * One might ask : why can't we symbol_new if the symbol does not
1978 * already exist and fill it with debug information. Because of
1979 * the C_EFCN special symbol. It would clobber the value of the
1980 * function symbol before we have a chance to notice that it is
1981 * a C_EFCN. And a second reason is that the code is more clear this
1982 * way. (at least I think it is :-).
1983 *
1984 */
1985
1986#define SKIP_SEMI_COLON() while (*input_line_pointer++ != ';')
1987#define SKIP_WHITESPACES() while (*input_line_pointer == ' ' || \
1988 *input_line_pointer == '\t') \
1989 input_line_pointer++;
1990
1991static void
1992obj_coff_def (what)
1993 int what;
1994{
1995 char name_end; /* Char after the end of name */
1996 char *symbol_name; /* Name of the debug symbol */
1997 char *symbol_name_copy; /* Temporary copy of the name */
1998 unsigned int symbol_name_length;
1999
2000 if (def_symbol_in_progress != NULL)
2001 {
2002 as_warn (".def pseudo-op used inside of .def/.endef: ignored.");
2003 demand_empty_rest_of_line ();
2004 return;
2005 } /* if not inside .def/.endef */
2006
2007 SKIP_WHITESPACES ();
2008
2009 def_symbol_in_progress = (symbolS *) obstack_alloc (&notes, sizeof (*def_symbol_in_progress));
2010 memset (def_symbol_in_progress, 0, sizeof (*def_symbol_in_progress));
2011
2012 symbol_name = input_line_pointer;
2013 name_end = get_symbol_end ();
2014 symbol_name_length = strlen (symbol_name);
2015 symbol_name_copy = xmalloc (symbol_name_length + 1);
2016 strcpy (symbol_name_copy, symbol_name);
2017
2018 /* Initialize the new symbol */
2019#ifdef STRIP_UNDERSCORE
2020 S_SET_NAME (def_symbol_in_progress, (*symbol_name_copy == '_'
2021 ? symbol_name_copy + 1
2022 : symbol_name_copy));
2023#else /* STRIP_UNDERSCORE */
2024 S_SET_NAME (def_symbol_in_progress, symbol_name_copy);
2025#endif /* STRIP_UNDERSCORE */
2026 /* free(symbol_name_copy); */
2027 def_symbol_in_progress->sy_name_offset = (unsigned long) ~0;
2028 def_symbol_in_progress->sy_number = ~0;
2029 def_symbol_in_progress->sy_frag = &zero_address_frag;
2030 S_SET_VALUE (def_symbol_in_progress, 0);
2031
2032 if (S_IS_STRING (def_symbol_in_progress))
2033 SF_SET_STRING (def_symbol_in_progress);
2034
2035 *input_line_pointer = name_end;
2036
2037 demand_empty_rest_of_line ();
2038}
2039
2040unsigned int dim_index;
2041
2042
2043static void
2044obj_coff_endef (ignore)
2045 int ignore;
2046{
2047 symbolS *symbolP = 0;
2048 /* DIM BUG FIX sac@cygnus.com */
2049 dim_index = 0;
2050 if (def_symbol_in_progress == NULL)
2051 {
2052 as_warn (".endef pseudo-op used outside of .def/.endef: ignored.");
2053 demand_empty_rest_of_line ();
2054 return;
2055 } /* if not inside .def/.endef */
2056
2057 /* Set the section number according to storage class. */
2058 switch (S_GET_STORAGE_CLASS (def_symbol_in_progress))
2059 {
2060 case C_STRTAG:
2061 case C_ENTAG:
2062 case C_UNTAG:
2063 SF_SET_TAG (def_symbol_in_progress);
2064 /* intentional fallthrough */
2065 case C_FILE:
2066 case C_TPDEF:
2067 SF_SET_DEBUG (def_symbol_in_progress);
2068 S_SET_SEGMENT (def_symbol_in_progress, SEG_DEBUG);
2069 break;
2070
2071 case C_EFCN:
2072 SF_SET_LOCAL (def_symbol_in_progress); /* Do not emit this symbol. */
2073 /* intentional fallthrough */
2074 case C_BLOCK:
2075 SF_SET_PROCESS (def_symbol_in_progress); /* Will need processing before writing */
2076 /* intentional fallthrough */
2077 case C_FCN:
2078 S_SET_SEGMENT (def_symbol_in_progress, SEG_E0);
2079
2080 if (strcmp (S_GET_NAME (def_symbol_in_progress), ".bf") == 0)
2081 { /* .bf */
2082 if (function_lineoff < 0)
2083 {
2084 fprintf (stderr, "`.bf' symbol without preceding function\n");
2085 } /* missing function symbol */
2086 SA_GET_SYM_LNNOPTR (last_line_symbol) = function_lineoff;
2087
2088 SF_SET_PROCESS (last_line_symbol);
2089 function_lineoff = -1;
2090 }
2091 /* Value is always set to . */
2092 def_symbol_in_progress->sy_frag = frag_now;
2093 S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ());
2094 break;
2095
2096#ifdef C_AUTOARG
2097 case C_AUTOARG:
2098#endif /* C_AUTOARG */
2099 case C_AUTO:
2100 case C_REG:
2101 case C_MOS:
2102 case C_MOE:
2103 case C_MOU:
2104 case C_ARG:
2105 case C_REGPARM:
2106 case C_FIELD:
2107 case C_EOS:
2108 SF_SET_DEBUG (def_symbol_in_progress);
2109 S_SET_SEGMENT (def_symbol_in_progress, absolute_section);
2110 break;
2111
2112 case C_EXT:
2113 case C_STAT:
2114 case C_LABEL:
2115 /* Valid but set somewhere else (s_comm, s_lcomm, colon) */
2116 break;
2117
2118 case C_USTATIC:
2119 case C_EXTDEF:
2120 case C_ULABEL:
2121 as_warn ("unexpected storage class %d", S_GET_STORAGE_CLASS (def_symbol_in_progress));
2122 break;
2123 } /* switch on storage class */
2124
2125 /* Now that we have built a debug symbol, try to find if we should
2126 merge with an existing symbol or not. If a symbol is C_EFCN or
2127 absolute_section or untagged SEG_DEBUG it never merges. We also
2128 don't merge labels, which are in a different namespace, nor
2129 symbols which have not yet been defined since they are typically
2130 unique, nor do we merge tags with non-tags. */
2131
2132 /* Two cases for functions. Either debug followed by definition or
2133 definition followed by debug. For definition first, we will
2134 merge the debug symbol into the definition. For debug first, the
2135 lineno entry MUST point to the definition function or else it
2136 will point off into space when crawl_symbols() merges the debug
2137 symbol into the real symbol. Therefor, let's presume the debug
2138 symbol is a real function reference. */
2139
2140 /* FIXME-SOON If for some reason the definition label/symbol is
2141 never seen, this will probably leave an undefined symbol at link
2142 time. */
2143
2144 if (S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_EFCN
2145 || S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_LABEL
2146 || (S_GET_SEGMENT (def_symbol_in_progress) == SEG_DEBUG
2147 && !SF_GET_TAG (def_symbol_in_progress))
2148 || S_GET_SEGMENT (def_symbol_in_progress) == absolute_section
2149 || def_symbol_in_progress->sy_value.X_op != O_constant
2150 || (symbolP = symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP)) == NULL
2151 || (SF_GET_TAG (def_symbol_in_progress) != SF_GET_TAG (symbolP)))
2152 {
2153 symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP,
2154 &symbol_lastP);
2155 }
2156 else
2157 {
2158 /* This symbol already exists, merge the newly created symbol
2159 into the old one. This is not mandatory. The linker can
2160 handle duplicate symbols correctly. But I guess that it save
2161 a *lot* of space if the assembly file defines a lot of
2162 symbols. [loic] */
2163
2164 /* The debug entry (def_symbol_in_progress) is merged into the
2165 previous definition. */
2166
2167 c_symbol_merge (def_symbol_in_progress, symbolP);
2168 /* FIXME-SOON Should *def_symbol_in_progress be free'd? xoxorich. */
2169 def_symbol_in_progress = symbolP;
2170
2171 if (SF_GET_FUNCTION (def_symbol_in_progress)
2172 || SF_GET_TAG (def_symbol_in_progress))
2173 {
2174 /* For functions, and tags, the symbol *must* be where the
2175 debug symbol appears. Move the existing symbol to the
2176 current place. */
2177 /* If it already is at the end of the symbol list, do nothing */
2178 if (def_symbol_in_progress != symbol_lastP)
2179 {
2180 symbol_remove (def_symbol_in_progress, &symbol_rootP,
2181 &symbol_lastP);
2182 symbol_append (def_symbol_in_progress, symbol_lastP,
2183 &symbol_rootP, &symbol_lastP);
2184 } /* if not already in place */
2185 } /* if function */
2186 } /* normal or mergable */
2187
2188 if (SF_GET_TAG (def_symbol_in_progress)
2189 && symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP) == NULL)
2190 {
2191 tag_insert (S_GET_NAME (def_symbol_in_progress), def_symbol_in_progress);
2192 }
2193
2194 if (SF_GET_FUNCTION (def_symbol_in_progress))
2195 {
2196 know (sizeof (def_symbol_in_progress) <= sizeof (long));
2197 function_lineoff
2198 = c_line_new (def_symbol_in_progress, 0, 0, &zero_address_frag);
2199
2200 SF_SET_PROCESS (def_symbol_in_progress);
2201
2202 if (symbolP == NULL)
2203 {
2204 /* That is, if this is the first time we've seen the
2205 function... */
2206 symbol_table_insert (def_symbol_in_progress);
2207 } /* definition follows debug */
2208 } /* Create the line number entry pointing to the function being defined */
2209
2210 def_symbol_in_progress = NULL;
2211 demand_empty_rest_of_line ();
2212}
2213
2214static void
2215obj_coff_dim (ignore)
2216 int ignore;
2217{
2218 int dim_index;
2219
2220 if (def_symbol_in_progress == NULL)
2221 {
2222 as_warn (".dim pseudo-op used outside of .def/.endef: ignored.");
2223 demand_empty_rest_of_line ();
2224 return;
2225 } /* if not inside .def/.endef */
2226
2227 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
2228
2229 for (dim_index = 0; dim_index < DIMNUM; dim_index++)
2230 {
2231 SKIP_WHITESPACES ();
2232 SA_SET_SYM_DIMEN (def_symbol_in_progress, dim_index,
2233 get_absolute_expression ());
2234
2235 switch (*input_line_pointer)
2236 {
2237 case ',':
2238 input_line_pointer++;
2239 break;
2240
2241 default:
2242 as_warn ("badly formed .dim directive ignored");
2243 /* intentional fallthrough */
2244 case '\n':
2245 case ';':
2246 dim_index = DIMNUM;
2247 break;
2248 }
2249 }
2250
2251 demand_empty_rest_of_line ();
2252}
2253
2254static void
2255obj_coff_line (ignore)
2256 int ignore;
2257{
2258 int this_base;
4325bcb9 2259 const char *name;
2283f8a0
KR
2260
2261 if (def_symbol_in_progress == NULL)
2262 {
2263 obj_coff_ln (0);
2264 return;
2265 }
2266
4325bcb9 2267 name = S_GET_NAME (def_symbol_in_progress);
2283f8a0 2268 this_base = get_absolute_expression ();
4325bcb9
KR
2269
2270 /* Only .bf symbols indicate the use of a new base line number; the
2271 line numbers associated with .ef, .bb, .eb are relative to the
2272 start of the containing function. */
2273 if (!strcmp (".bf", name))
2283f8a0 2274 {
4325bcb9
KR
2275#if 0 /* XXX Can we ever have line numbers going backwards? */
2276 if (this_base > line_base)
2277#endif
2278 {
2279 line_base = this_base;
2280 }
2283f8a0
KR
2281
2282#ifndef NO_LISTING
2283f8a0 2283 {
4325bcb9 2284 extern int listing;
3f8416d5 2285 if (listing)
4325bcb9
KR
2286 {
2287 listing_source_line ((unsigned int) line_base);
2288 }
2283f8a0 2289 }
2283f8a0 2290#endif
4325bcb9
KR
2291 }
2292
2283f8a0
KR
2293 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
2294 SA_SET_SYM_LNNO (def_symbol_in_progress, this_base);
2295
2296 demand_empty_rest_of_line ();
2297}
2298
2299static void
2300obj_coff_size (ignore)
2301 int ignore;
2302{
2303 if (def_symbol_in_progress == NULL)
2304 {
2305 as_warn (".size pseudo-op used outside of .def/.endef ignored.");
2306 demand_empty_rest_of_line ();
2307 return;
2308 } /* if not inside .def/.endef */
2309
2310 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
2311 SA_SET_SYM_SIZE (def_symbol_in_progress, get_absolute_expression ());
2312 demand_empty_rest_of_line ();
2313}
2314
2315static void
2316obj_coff_scl (ignore)
2317 int ignore;
2318{
2319 if (def_symbol_in_progress == NULL)
2320 {
2321 as_warn (".scl pseudo-op used outside of .def/.endef ignored.");
2322 demand_empty_rest_of_line ();
2323 return;
2324 } /* if not inside .def/.endef */
2325
2326 S_SET_STORAGE_CLASS (def_symbol_in_progress, get_absolute_expression ());
2327 demand_empty_rest_of_line ();
2328}
2329
2330static void
2331obj_coff_tag (ignore)
2332 int ignore;
2333{
2334 char *symbol_name;
2335 char name_end;
2336
2337 if (def_symbol_in_progress == NULL)
2338 {
2339 as_warn (".tag pseudo-op used outside of .def/.endef ignored.");
2340 demand_empty_rest_of_line ();
2341 return;
2342 }
2343
2344 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
2345 symbol_name = input_line_pointer;
2346 name_end = get_symbol_end ();
2347
2348 /* Assume that the symbol referred to by .tag is always defined.
2349 This was a bad assumption. I've added find_or_make. xoxorich. */
2350 SA_SET_SYM_TAGNDX (def_symbol_in_progress,
2351 (long) tag_find_or_make (symbol_name));
2352 if (SA_GET_SYM_TAGNDX (def_symbol_in_progress) == 0L)
2353 {
2354 as_warn ("tag not found for .tag %s", symbol_name);
2355 } /* not defined */
2356
2357 SF_SET_TAGGED (def_symbol_in_progress);
2358 *input_line_pointer = name_end;
2359
2360 demand_empty_rest_of_line ();
2361}
2362
2363static void
2364obj_coff_type (ignore)
2365 int ignore;
2366{
2367 if (def_symbol_in_progress == NULL)
2368 {
2369 as_warn (".type pseudo-op used outside of .def/.endef ignored.");
2370 demand_empty_rest_of_line ();
2371 return;
2372 } /* if not inside .def/.endef */
2373
2374 S_SET_DATA_TYPE (def_symbol_in_progress, get_absolute_expression ());
2375
2376 if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress)) &&
2377 S_GET_STORAGE_CLASS (def_symbol_in_progress) != C_TPDEF)
2378 {
2379 SF_SET_FUNCTION (def_symbol_in_progress);
2380 } /* is a function */
2381
2382 demand_empty_rest_of_line ();
2383}
2384
2385static void
2386obj_coff_val (ignore)
2387 int ignore;
2388{
2389 if (def_symbol_in_progress == NULL)
2390 {
2391 as_warn (".val pseudo-op used outside of .def/.endef ignored.");
2392 demand_empty_rest_of_line ();
2393 return;
2394 } /* if not inside .def/.endef */
2395
2396 if (is_name_beginner (*input_line_pointer))
2397 {
2398 char *symbol_name = input_line_pointer;
2399 char name_end = get_symbol_end ();
2400
2401 if (!strcmp (symbol_name, "."))
2402 {
2403 def_symbol_in_progress->sy_frag = frag_now;
2404 S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ());
2405 /* If the .val is != from the .def (e.g. statics) */
2406 }
2407 else if (strcmp (S_GET_NAME (def_symbol_in_progress), symbol_name))
2408 {
2409 def_symbol_in_progress->sy_value.X_op = O_symbol;
2410 def_symbol_in_progress->sy_value.X_add_symbol =
2411 symbol_find_or_make (symbol_name);
2412 def_symbol_in_progress->sy_value.X_op_symbol = NULL;
2413 def_symbol_in_progress->sy_value.X_add_number = 0;
2414
2415 /* If the segment is undefined when the forward reference is
2416 resolved, then copy the segment id from the forward
2417 symbol. */
2418 SF_SET_GET_SEGMENT (def_symbol_in_progress);
2419
2420 /* FIXME: gcc can generate address expressions
2421 here in unusual cases (search for "obscure"
2422 in sdbout.c). We just ignore the offset
2423 here, thus generating incorrect debugging
2424 information. We ignore the rest of the
2425 line just below. */
2426 }
2427 /* Otherwise, it is the name of a non debug symbol and
2428 its value will be calculated later. */
2429 *input_line_pointer = name_end;
2430
2431 /* FIXME: this is to avoid an error message in the
2432 FIXME case mentioned just above. */
2433 while (! is_end_of_line[(unsigned char) *input_line_pointer])
2434 ++input_line_pointer;
2435 }
2436 else
2437 {
2438 S_SET_VALUE (def_symbol_in_progress,
2439 (valueT) get_absolute_expression ());
2440 } /* if symbol based */
2441
2442 demand_empty_rest_of_line ();
2443}
2444
2445void
2446obj_read_begin_hook ()
2447{
2448 /* These had better be the same. Usually 18 bytes. */
2449#ifndef BFD_HEADERS
2450 know (sizeof (SYMENT) == sizeof (AUXENT));
2451 know (SYMESZ == AUXESZ);
2452#endif
2453 tag_init ();
2454}
2455
2456/* This function runs through the symbol table and puts all the
2457 externals onto another chain */
2458
a91c6b08
ILT
2459/* The chain of globals. */
2460symbolS *symbol_globalP;
2461symbolS *symbol_global_lastP;
2462
2283f8a0 2463/* The chain of externals */
eb4fd16f
ILT
2464symbolS *symbol_externP;
2465symbolS *symbol_extern_lastP;
2283f8a0
KR
2466
2467stack *block_stack;
eb4fd16f 2468symbolS *last_functionP;
2283f8a0
KR
2469symbolS *last_tagP;
2470
2471static unsigned int
2472yank_symbols ()
2473{
2474 symbolS *symbolP;
2475 unsigned int symbol_number = 0;
2476 unsigned int last_file_symno = 0;
2477
e5c5ce23
SC
2478 struct filename_list *filename_list_scan = filename_list_head;
2479
2283f8a0
KR
2480 for (symbolP = symbol_rootP;
2481 symbolP;
2482 symbolP = symbolP ? symbol_next (symbolP) : symbol_rootP)
2483 {
1356d77d
ILT
2484 if (symbolP->sy_mri_common)
2485 {
2486 if (S_GET_STORAGE_CLASS (symbolP) == C_EXT)
2487 as_bad ("%s: global symbols not supported in common sections",
2488 S_GET_NAME (symbolP));
2489 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2490 continue;
2491 }
2492
2283f8a0
KR
2493 if (!SF_GET_DEBUG (symbolP))
2494 {
2495 /* Debug symbols do not need all this rubbish */
2496 symbolS *real_symbolP;
2497
2498 /* L* and C_EFCN symbols never merge. */
2499 if (!SF_GET_LOCAL (symbolP)
2500 && S_GET_STORAGE_CLASS (symbolP) != C_LABEL
2501 && symbolP->sy_value.X_op == O_constant
2502 && (real_symbolP = symbol_find_base (S_GET_NAME (symbolP), DO_NOT_STRIP))
2503 && real_symbolP != symbolP)
2504 {
2505 /* FIXME-SOON: where do dups come from?
2506 Maybe tag references before definitions? xoxorich. */
2507 /* Move the debug data from the debug symbol to the
2508 real symbol. Do NOT do the oposite (i.e. move from
2509 real symbol to debug symbol and remove real symbol from the
2510 list.) Because some pointers refer to the real symbol
2511 whereas no pointers refer to the debug symbol. */
2512 c_symbol_merge (symbolP, real_symbolP);
2513 /* Replace the current symbol by the real one */
2514 /* The symbols will never be the last or the first
2515 because : 1st symbol is .file and 3 last symbols are
2516 .text, .data, .bss */
2517 symbol_remove (real_symbolP, &symbol_rootP, &symbol_lastP);
2518 symbol_insert (real_symbolP, symbolP, &symbol_rootP, &symbol_lastP);
2519 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2520 symbolP = real_symbolP;
2521 } /* if not local but dup'd */
2522
def66e24 2523 if (flag_readonly_data_in_text && (S_GET_SEGMENT (symbolP) == SEG_E1))
2283f8a0
KR
2524 {
2525 S_SET_SEGMENT (symbolP, SEG_E0);
2526 } /* push data into text */
2527
2528 resolve_symbol_value (symbolP);
2529
2530 if (S_GET_STORAGE_CLASS (symbolP) == C_NULL)
2531 {
2532 if (!S_IS_DEFINED (symbolP) && !SF_GET_LOCAL (symbolP))
2533 {
2534 S_SET_EXTERNAL (symbolP);
2535 }
2536 else if (S_GET_SEGMENT (symbolP) == SEG_E0)
2537 {
2538 S_SET_STORAGE_CLASS (symbolP, C_LABEL);
2539 }
2540 else
2541 {
2542 S_SET_STORAGE_CLASS (symbolP, C_STAT);
2543 }
2544 }
2545
2546 /* Mainly to speed up if not -g */
2547 if (SF_GET_PROCESS (symbolP))
2548 {
2549 /* Handle the nested blocks auxiliary info. */
2550 if (S_GET_STORAGE_CLASS (symbolP) == C_BLOCK)
2551 {
2552 if (!strcmp (S_GET_NAME (symbolP), ".bb"))
2553 stack_push (block_stack, (char *) &symbolP);
2554 else
2555 { /* .eb */
2556 register symbolS *begin_symbolP;
2557 begin_symbolP = *(symbolS **) stack_pop (block_stack);
2558 if (begin_symbolP == (symbolS *) 0)
2559 as_warn ("mismatched .eb");
2560 else
2561 SA_SET_SYM_ENDNDX (begin_symbolP, symbol_number + 2);
2562 }
2563 }
2564 /* If we are able to identify the type of a function, and we
2565 are out of a function (last_functionP == 0) then, the
2566 function symbol will be associated with an auxiliary
2567 entry. */
2568 if (last_functionP == (symbolS *) 0 &&
2569 SF_GET_FUNCTION (symbolP))
2570 {
2571 last_functionP = symbolP;
2572
2573 if (S_GET_NUMBER_AUXILIARY (symbolP) < 1)
2574 {
2575 S_SET_NUMBER_AUXILIARY (symbolP, 1);
2576 } /* make it at least 1 */
2577
2578 /* Clobber possible stale .dim information. */
2579#if 0
2580 /* Iffed out by steve - this fries the lnnoptr info too */
2581 bzero (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen,
2582 sizeof (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen));
2583#endif
2584 }
2585 /* The C_FCN doesn't need any additional information. I
2586 don't even know if this is needed for sdb. But the
2587 standard assembler generates it, so... */
2588 if (S_GET_STORAGE_CLASS (symbolP) == C_EFCN)
2589 {
2590 if (last_functionP == (symbolS *) 0)
2591 as_fatal ("C_EFCN symbol out of scope");
2592 SA_SET_SYM_FSIZE (last_functionP,
2593 (long) (S_GET_VALUE (symbolP) -
2594 S_GET_VALUE (last_functionP)));
2595 SA_SET_SYM_ENDNDX (last_functionP, symbol_number);
2596 last_functionP = (symbolS *) 0;
2597 }
2598 }
2599 }
2600 else if (SF_GET_TAG (symbolP))
2601 {
2602 /* First descriptor of a structure must point to
2603 the first slot after the structure description. */
2604 last_tagP = symbolP;
2605
2606 }
2607 else if (S_GET_STORAGE_CLASS (symbolP) == C_EOS)
2608 {
2609 /* +2 take in account the current symbol */
2610 SA_SET_SYM_ENDNDX (last_tagP, symbol_number + 2);
2611 }
2612 else if (S_GET_STORAGE_CLASS (symbolP) == C_FILE)
2613 {
e5c5ce23
SC
2614 /* If the filename was too long to fit in the
2615 auxent, put it in the string table */
2616 if (SA_GET_FILE_FNAME_ZEROS (symbolP) == 0)
2617 {
2618 SA_SET_FILE_FNAME_OFFSET (symbolP, string_byte_count);
2619 string_byte_count += strlen (filename_list_scan->filename) + 1;
2620 filename_list_scan = filename_list_scan->next;
2621 }
2283f8a0
KR
2622 if (S_GET_VALUE (symbolP))
2623 {
2624 S_SET_VALUE (symbolP, last_file_symno);
2625 last_file_symno = symbol_number;
2626 } /* no one points at the first .file symbol */
2627 } /* if debug or tag or eos or file */
2628
2629 /* We must put the external symbols apart. The loader
2630 does not bomb if we do not. But the references in
2631 the endndx field for a .bb symbol are not corrected
2632 if an external symbol is removed between .bb and .be.
2633 I.e in the following case :
2634 [20] .bb endndx = 22
2635 [21] foo external
2636 [22] .be
2637 ld will move the symbol 21 to the end of the list but
2638 endndx will still be 22 instead of 21. */
2639
2640
2641 if (SF_GET_LOCAL (symbolP))
2642 {
2643 /* remove C_EFCN and LOCAL (L...) symbols */
2644 /* next pointer remains valid */
2645 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2646
2647 }
2648 else if (!S_IS_DEFINED (symbolP)
2649 && !S_IS_DEBUG (symbolP)
2650 && !SF_GET_STATICS (symbolP) &&
2651 S_GET_STORAGE_CLASS (symbolP) == C_EXT)
2652 { /* C_EXT && !SF_GET_FUNCTION(symbolP)) */
2653 /* if external, Remove from the list */
2654 symbolS *hold = symbol_previous (symbolP);
2655
2656 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2657 symbol_clear_list_pointers (symbolP);
2658 symbol_append (symbolP, symbol_extern_lastP, &symbol_externP, &symbol_extern_lastP);
2659 symbolP = hold;
2660 }
a91c6b08
ILT
2661 else if (! S_IS_DEBUG (symbolP)
2662 && ! SF_GET_STATICS (symbolP)
2663 && ! SF_GET_FUNCTION (symbolP)
2664 && S_GET_STORAGE_CLASS (symbolP) == C_EXT)
2665 {
2666 symbolS *hold = symbol_previous (symbolP);
2667
2668 /* The O'Reilly COFF book says that defined global symbols
2669 come at the end of the symbol table, just before
2670 undefined global symbols. */
2671
2672 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2673 symbol_clear_list_pointers (symbolP);
2674 symbol_append (symbolP, symbol_global_lastP, &symbol_globalP,
2675 &symbol_global_lastP);
2676 symbolP = hold;
2677 }
2283f8a0
KR
2678 else
2679 {
2680 if (SF_GET_STRING (symbolP))
2681 {
2682 symbolP->sy_name_offset = string_byte_count;
2683 string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
2684 }
2685 else
2686 {
2687 symbolP->sy_name_offset = 0;
2688 } /* fix "long" names */
2689
2690 symbolP->sy_number = symbol_number;
2691 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
2692 } /* if local symbol */
2693 } /* traverse the symbol list */
2694 return symbol_number;
2695
2696}
2697
2698
2699static unsigned int
a91c6b08
ILT
2700glue_symbols (head, tail)
2701 symbolS **head;
2702 symbolS **tail;
2283f8a0
KR
2703{
2704 unsigned int symbol_number = 0;
2705 symbolS *symbolP;
a91c6b08
ILT
2706
2707 for (symbolP = *head; *head != NULL;)
2283f8a0 2708 {
a91c6b08 2709 symbolS *tmp = *head;
2283f8a0
KR
2710
2711 /* append */
a91c6b08 2712 symbol_remove (tmp, head, tail);
2283f8a0
KR
2713 symbol_append (tmp, symbol_lastP, &symbol_rootP, &symbol_lastP);
2714
2715 /* and process */
2716 if (SF_GET_STRING (tmp))
2717 {
2718 tmp->sy_name_offset = string_byte_count;
2719 string_byte_count += strlen (S_GET_NAME (tmp)) + 1;
2720 }
2721 else
2722 {
2723 tmp->sy_name_offset = 0;
2724 } /* fix "long" names */
2725
2726 tmp->sy_number = symbol_number;
2727 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (tmp);
2728 } /* append the entire extern chain */
2283f8a0 2729
a91c6b08 2730 return symbol_number;
2283f8a0
KR
2731}
2732
2733static unsigned int
2734tie_tags ()
2735{
2736 unsigned int symbol_number = 0;
2737
2738 symbolS *symbolP;
2739 for (symbolP = symbol_rootP; symbolP; symbolP =
2740 symbol_next (symbolP))
2741 {
2742 symbolP->sy_number = symbol_number;
2743
2744
2745
2746 if (SF_GET_TAGGED (symbolP))
2747 {
2748 SA_SET_SYM_TAGNDX
2749 (symbolP,
2750 ((symbolS *) SA_GET_SYM_TAGNDX (symbolP))->sy_number);
2751 }
2752
2753 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
2754 }
2755 return symbol_number;
2756
2757}
2758
2759static void
2760crawl_symbols (h, abfd)
2761 object_headers *h;
2762 bfd * abfd;
2763{
2764 unsigned int i;
2765
2766 /* Initialize the stack used to keep track of the matching .bb .be */
2767
2768 block_stack = stack_init (512, sizeof (symbolS *));
2769
2770 /* The symbol list should be ordered according to the following sequence
2771 * order :
2772 * . .file symbol
2773 * . debug entries for functions
2774 * . fake symbols for the sections, including.text .data and .bss
2775 * . defined symbols
2776 * . undefined symbols
2777 * But this is not mandatory. The only important point is to put the
2778 * undefined symbols at the end of the list.
2779 */
2780
2781 if (symbol_rootP == NULL
2782 || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE)
2783 {
2784 c_dot_file_symbol ("fake");
2785 }
2786 /* Is there a .file symbol ? If not insert one at the beginning. */
2787
2788 /*
2789 * Build up static symbols for the sections, they are filled in later
2790 */
2791
2792
2793 for (i = SEG_E0; i < SEG_E9; i++)
2794 {
2795 if (segment_info[i].scnhdr.s_name[0])
2796 {
2797 char name[9];
2798
2799 strncpy (name, segment_info[i].scnhdr.s_name, 8);
2800 name[8] = '\0';
2801 segment_info[i].dot = c_section_symbol (name, i - SEG_E0 + 1);
2802 }
2803 }
2804
2805
2806 /* Take all the externals out and put them into another chain */
2807 H_SET_SYMBOL_TABLE_SIZE (h, yank_symbols ());
2808 /* Take the externals and glue them onto the end.*/
a91c6b08
ILT
2809 H_SET_SYMBOL_TABLE_SIZE (h,
2810 (H_GET_SYMBOL_COUNT (h)
2811 + glue_symbols (&symbol_globalP,
2812 &symbol_global_lastP)
2813 + glue_symbols (&symbol_externP,
2814 &symbol_extern_lastP)));
2283f8a0
KR
2815
2816 H_SET_SYMBOL_TABLE_SIZE (h, tie_tags ());
a91c6b08
ILT
2817 know (symbol_globalP == NULL);
2818 know (symbol_global_lastP == NULL);
2283f8a0
KR
2819 know (symbol_externP == NULL);
2820 know (symbol_extern_lastP == NULL);
2821}
2822
2823/*
2824 * Find strings by crawling along symbol table chain.
2825 */
2826
2827void
2828w_strings (where)
2829 char *where;
2830{
2831 symbolS *symbolP;
e5c5ce23 2832 struct filename_list *filename_list_scan = filename_list_head;
2283f8a0
KR
2833
2834 /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
2835 md_number_to_chars (where, (valueT) string_byte_count, 4);
2836 where += 4;
2837 for (symbolP = symbol_rootP;
2838 symbolP;
2839 symbolP = symbol_next (symbolP))
2840 {
2841 unsigned int size;
2842
2843 if (SF_GET_STRING (symbolP))
2844 {
2845 size = strlen (S_GET_NAME (symbolP)) + 1;
2283f8a0
KR
2846 memcpy (where, S_GET_NAME (symbolP), size);
2847 where += size;
e5c5ce23
SC
2848 }
2849 if (S_GET_STORAGE_CLASS (symbolP) == C_FILE
2850 && SA_GET_FILE_FNAME_ZEROS (symbolP) == 0)
2851 {
2852 size = strlen (filename_list_scan->filename) + 1;
2853 memcpy (where, filename_list_scan->filename, size);
2854 filename_list_scan = filename_list_scan ->next;
2855 where += size;
2283f8a0
KR
2856 }
2857 }
2858}
2859
2860static void
2861do_linenos_for (abfd, h, file_cursor)
2862 bfd * abfd;
2863 object_headers * h;
2864 unsigned long *file_cursor;
2865{
2866 unsigned int idx;
2867 unsigned long start = *file_cursor;
2868
2869 for (idx = SEG_E0; idx < SEG_E9; idx++)
2870 {
2871 segment_info_type *s = segment_info + idx;
2872
2873
2874 if (s->scnhdr.s_nlnno != 0)
2875 {
2876 struct lineno_list *line_ptr;
2877
2878 struct external_lineno *buffer =
2879 (struct external_lineno *) xmalloc (s->scnhdr.s_nlnno * LINESZ);
2880
2881 struct external_lineno *dst = buffer;
2882
2883 /* Run through the table we've built and turn it into its external
2884 form, take this chance to remove duplicates */
2885
2886 for (line_ptr = s->lineno_list_head;
2887 line_ptr != (struct lineno_list *) NULL;
2888 line_ptr = line_ptr->next)
2889 {
2890
2891 if (line_ptr->line.l_lnno == 0)
2892 {
2893 /* Turn a pointer to a symbol into the symbols' index */
2894 line_ptr->line.l_addr.l_symndx =
2895 ((symbolS *) line_ptr->line.l_addr.l_symndx)->sy_number;
2896 }
2897 else
2898 {
2899 line_ptr->line.l_addr.l_paddr += ((struct frag *) (line_ptr->frag))->fr_address;
2900 }
2901
2902
2903 (void) bfd_coff_swap_lineno_out (abfd, &(line_ptr->line), dst);
2904 dst++;
2905
2906 }
2907
2908 s->scnhdr.s_lnnoptr = *file_cursor;
2909
2910 bfd_write (buffer, 1, s->scnhdr.s_nlnno * LINESZ, abfd);
2911 free (buffer);
2912
2913 *file_cursor += s->scnhdr.s_nlnno * LINESZ;
2914 }
2915 }
2916 H_SET_LINENO_SIZE (h, *file_cursor - start);
2917}
2918
2919
2920/* Now we run through the list of frag chains in a segment and
2921 make all the subsegment frags appear at the end of the
2922 list, as if the seg 0 was extra long */
2923
2924static void
2925remove_subsegs ()
2926{
2927 unsigned int i;
2928
2929 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
2930 {
2931 frchainS *head = segment_info[i].frchainP;
2932 fragS dummy;
2933 fragS *prev_frag = &dummy;
2934
2935 while (head && head->frch_seg == i)
2936 {
2937 prev_frag->fr_next = head->frch_root;
2938 prev_frag = head->frch_last;
2939 head = head->frch_next;
2940 }
2941 prev_frag->fr_next = 0;
2942 }
2943}
2944
2945unsigned long machine;
2946int coff_flags;
2947extern void
2948write_object_file ()
2949{
2950 int i;
2951 char *name;
2952 struct frchain *frchain_ptr;
2953
2954 object_headers headers;
2955 unsigned long file_cursor;
2956 bfd *abfd;
2957 unsigned int addr;
2958 abfd = bfd_openw (out_file_name, TARGET_FORMAT);
2959
2960
2961 if (abfd == 0)
2962 {
2963 as_perror ("FATAL: Can't create %s", out_file_name);
460531da 2964 exit (EXIT_FAILURE);
2283f8a0
KR
2965 }
2966 bfd_set_format (abfd, bfd_object);
2967 bfd_set_arch_mach (abfd, BFD_ARCH, machine);
2968
2969 string_byte_count = 4;
2970
2971 for (frchain_ptr = frchain_root;
2972 frchain_ptr != (struct frchain *) NULL;
2973 frchain_ptr = frchain_ptr->frch_next)
2974 {
eb4fd16f
ILT
2975 /* Run through all the sub-segments and align them up. Also
2976 close any open frags. We tack a .fill onto the end of the
2977 frag chain so that any .align's size can be worked by looking
2978 at the next frag. */
2283f8a0
KR
2979
2980 subseg_set (frchain_ptr->frch_seg, frchain_ptr->frch_subseg);
2981#ifndef SUB_SEGMENT_ALIGN
2982#define SUB_SEGMENT_ALIGN(SEG) 1
2983#endif
2984 frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE);
2985 frag_wane (frag_now);
2986 frag_now->fr_fix = 0;
2987 know (frag_now->fr_next == NULL);
2988 }
2989
2990
2991 remove_subsegs ();
2992
2993
2994 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
2995 {
2996 relax_segment (segment_info[i].frchainP->frch_root, i);
2997 }
2998
2999 H_SET_NUMBER_OF_SECTIONS (&headers, 0);
3000
3001 /* Find out how big the sections are, and set the addresses. */
3002 addr = 0;
3003 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3004 {
3005 long size;
3006
3007 segment_info[i].scnhdr.s_paddr = addr;
3008 segment_info[i].scnhdr.s_vaddr = addr;
3009
3010 if (segment_info[i].scnhdr.s_name[0])
3011 {
3012 H_SET_NUMBER_OF_SECTIONS (&headers,
3013 H_GET_NUMBER_OF_SECTIONS (&headers) + 1);
3014 }
3015
3016 size = size_section (abfd, (unsigned int) i);
3017 addr += size;
3018
eb4fd16f
ILT
3019 /* I think the section alignment is only used on the i960; the
3020 i960 needs it, and it should do no harm on other targets. */
3021 segment_info[i].scnhdr.s_align = section_alignment[i];
3022
2283f8a0
KR
3023 if (i == SEG_E0)
3024 H_SET_TEXT_SIZE (&headers, size);
3025 else if (i == SEG_E1)
3026 H_SET_DATA_SIZE (&headers, size);
3027 else if (i == SEG_E2)
3028 H_SET_BSS_SIZE (&headers, size);
3029 }
3030
3031 /* Turn the gas native symbol table shape into a coff symbol table */
3032 crawl_symbols (&headers, abfd);
3033
3034 if (string_byte_count == 4)
3035 string_byte_count = 0;
3036
3037 H_SET_STRING_SIZE (&headers, string_byte_count);
3038
a91c6b08
ILT
3039#ifdef tc_frob_file
3040 tc_frob_file ();
3041#endif
3042
2283f8a0
KR
3043 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3044 {
3045 fixup_mdeps (segment_info[i].frchainP->frch_root, &headers, i);
3046 fixup_segment (&segment_info[i], i);
3047 }
2283f8a0
KR
3048
3049 /* Look for ".stab" segments and fill in their initial symbols
3050 correctly. */
3051 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3052 {
3053 name = segment_info[i].scnhdr.s_name;
3054
3055 if (name != NULL
3056 && strncmp (".stab", name, 5) == 0
3057 && strncmp (".stabstr", name, 8) != 0)
3058 adjust_stab_section (abfd, i);
3059 }
3060
3061 file_cursor = H_GET_TEXT_FILE_OFFSET (&headers);
3062
3063 bfd_seek (abfd, (file_ptr) file_cursor, 0);
3064
3065 /* Plant the data */
3066
3067 fill_section (abfd, &headers, &file_cursor);
3068
3069 do_relocs_for (abfd, &headers, &file_cursor);
3070
3071 do_linenos_for (abfd, &headers, &file_cursor);
3072
3073 H_SET_FILE_MAGIC_NUMBER (&headers, COFF_MAGIC);
3074#ifndef OBJ_COFF_OMIT_TIMESTAMP
30355216 3075 H_SET_TIME_STAMP (&headers, (long)time((time_t *)0));
2283f8a0
KR
3076#else
3077 H_SET_TIME_STAMP (&headers, 0);
3078#endif
eb4fd16f
ILT
3079#ifdef TC_COFF_SET_MACHINE
3080 TC_COFF_SET_MACHINE (&headers);
3081#endif
2283f8a0 3082
df0f11ff
DE
3083#ifndef COFF_FLAGS
3084#define COFF_FLAGS 0
3085#endif
3086
2283f8a0
KR
3087#ifdef KEEP_RELOC_INFO
3088 H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers) ? 0 : F_LNNO) |
3089 COFF_FLAGS | coff_flags));
3090#else
3091 H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers) ? 0 : F_LNNO) |
3092 (H_GET_RELOCATION_SIZE(&headers) ? 0 : F_RELFLG) |
3093 COFF_FLAGS | coff_flags));
3094#endif
3095
3096 {
3097 unsigned int symtable_size = H_GET_SYMBOL_TABLE_SIZE (&headers);
3098 char *buffer1 = xmalloc (symtable_size + string_byte_count + 1);
3099
3100 H_SET_SYMBOL_TABLE_POINTER (&headers, bfd_tell (abfd));
3101 w_symbols (abfd, buffer1, symbol_rootP);
3102 if (string_byte_count > 0)
3103 w_strings (buffer1 + symtable_size);
3104 bfd_write (buffer1, 1, symtable_size + string_byte_count, abfd);
3105 free (buffer1);
3106 }
3107
3108 coff_header_append (abfd, &headers);
3109#if 0
3110 /* Recent changes to write need this, but where it should
3111 go is up to Ken.. */
3112 if (bfd_close_all_done (abfd) == false)
3113 as_fatal ("Can't close %s: %s", out_file_name,
3114 bfd_errmsg (bfd_get_error ()));
3115#else
3116 {
3117 extern bfd *stdoutput;
3118 stdoutput = abfd;
3119 }
3120#endif
3121
3122}
3123
3124/* Add a new segment. This is called from subseg_new via the
3125 obj_new_segment macro. */
3126
3127segT
3128obj_coff_add_segment (name)
3129 const char *name;
3130{
3131 unsigned int len;
3132 unsigned int i;
3133
3134 /* Find out if we've already got a section of this name. */
3135 len = strlen (name);
3136 if (len < sizeof (segment_info[i].scnhdr.s_name))
3137 ++len;
3138 else
3139 len = sizeof (segment_info[i].scnhdr.s_name);
3140 for (i = SEG_E0; i < SEG_E9 && segment_info[i].scnhdr.s_name[0]; i++)
3141 if (strncmp (segment_info[i].scnhdr.s_name, name, len) == 0
3142 && (len == sizeof (segment_info[i].scnhdr.s_name)
3143 || segment_info[i].scnhdr.s_name[len] == '\0'))
3144 return (segT) i;
3145
3146 if (i == SEG_E9)
3147 {
3148 as_bad ("Too many new sections; can't add \"%s\"", name);
3149 return now_seg;
3150 }
3151
3152 /* Add a new section. */
3153 strncpy (segment_info[i].scnhdr.s_name, name,
3154 sizeof (segment_info[i].scnhdr.s_name));
3155 segment_info[i].scnhdr.s_flags = STYP_REG;
3156
3157 return (segT) i;
3158}
3159
3160/*
3161 * implement the .section pseudo op:
3162 * .section name {, "flags"}
3163 * ^ ^
3164 * | +--- optional flags: 'b' for bss
3165 * | 'i' for info
3166 * +-- section name 'l' for lib
3167 * 'n' for noload
3168 * 'o' for over
3169 * 'w' for data
3170 * 'd' (apparently m88k for data)
3171 * 'x' for text
3172 * But if the argument is not a quoted string, treat it as a
3173 * subsegment number.
3174 */
3175
3176void
3177obj_coff_section (ignore)
3178 int ignore;
3179{
3180 /* Strip out the section name */
3181 char *section_name;
3182 char *section_name_end;
3183 char c;
3184 int argp;
3185 unsigned int len;
3186 unsigned int exp;
3187 long flags;
3188
3189 section_name = input_line_pointer;
3190 c = get_symbol_end ();
3191 section_name_end = input_line_pointer;
3192
3193 len = section_name_end - section_name;
3194 input_line_pointer++;
3195 SKIP_WHITESPACE ();
3196
3197 argp = 0;
3198 if (c == ',')
3199 argp = 1;
3200 else if (*input_line_pointer == ',')
3201 {
3202 argp = 1;
3203 ++input_line_pointer;
3204 SKIP_WHITESPACE ();
3205 }
3206
3207 exp = 0;
3208 flags = 0;
3209 if (argp)
3210 {
3211 if (*input_line_pointer != '"')
3212 exp = get_absolute_expression ();
3213 else
3214 {
3215 ++input_line_pointer;
3216 while (*input_line_pointer != '"'
3217 && ! is_end_of_line[(unsigned char) *input_line_pointer])
3218 {
3219 switch (*input_line_pointer)
3220 {
3221 case 'b': flags |= STYP_BSS; break;
3222 case 'i': flags |= STYP_INFO; break;
3223 case 'l': flags |= STYP_LIB; break;
3224 case 'n': flags |= STYP_NOLOAD; break;
3225 case 'o': flags |= STYP_OVER; break;
3226 case 'd':
3227 case 'w': flags |= STYP_DATA; break;
3228 case 'x': flags |= STYP_TEXT; break;
3229 default:
3230 as_warn("unknown section attribute '%c'",
3231 *input_line_pointer);
3232 break;
3233 }
3234 ++input_line_pointer;
3235 }
3236 if (*input_line_pointer == '"')
3237 ++input_line_pointer;
3238 }
3239 }
3240
3241 subseg_new (section_name, (subsegT) exp);
3242
3243 segment_info[now_seg].scnhdr.s_flags |= flags;
3244
3245 *section_name_end = c;
3246}
3247
3248
3249static void
3250obj_coff_text (ignore)
3251 int ignore;
3252{
3253 subseg_new (".text", get_absolute_expression ());
3254}
3255
3256
3257static void
3258obj_coff_data (ignore)
3259 int ignore;
3260{
def66e24 3261 if (flag_readonly_data_in_text)
2283f8a0
KR
3262 subseg_new (".text", get_absolute_expression () + 1000);
3263 else
3264 subseg_new (".data", get_absolute_expression ());
3265}
3266
3267static void
3268obj_coff_bss (ignore)
3269 int ignore;
3270{
3271 if (*input_line_pointer == '\n') /* .bss */
3272 subseg_new(".bss", get_absolute_expression());
3273 else /* .bss id,expr */
3274 obj_coff_lcomm(0);
3275}
3276
3277static void
3278obj_coff_ident (ignore)
3279 int ignore;
3280{
3281 segT current_seg = now_seg; /* save current seg */
3282 subsegT current_subseg = now_subseg;
3283 subseg_new (".comment", 0); /* .comment seg */
3284 stringer (1); /* read string */
3285 subseg_set (current_seg, current_subseg); /* restore current seg */
3286}
3287
3288void
3289c_symbol_merge (debug, normal)
3290 symbolS *debug;
3291 symbolS *normal;
3292{
3293 S_SET_DATA_TYPE (normal, S_GET_DATA_TYPE (debug));
3294 S_SET_STORAGE_CLASS (normal, S_GET_STORAGE_CLASS (debug));
3295
3296 if (S_GET_NUMBER_AUXILIARY (debug) > S_GET_NUMBER_AUXILIARY (normal))
3297 {
3298 S_SET_NUMBER_AUXILIARY (normal, S_GET_NUMBER_AUXILIARY (debug));
3299 } /* take the most we have */
3300
3301 if (S_GET_NUMBER_AUXILIARY (debug) > 0)
3302 {
3303 memcpy ((char *) &normal->sy_symbol.ost_auxent[0],
3304 (char *) &debug->sy_symbol.ost_auxent[0],
3305 (unsigned int) (S_GET_NUMBER_AUXILIARY (debug) * AUXESZ));
3306 } /* Move all the auxiliary information */
3307
3308 /* Move the debug flags. */
3309 SF_SET_DEBUG_FIELD (normal, SF_GET_DEBUG_FIELD (debug));
3310} /* c_symbol_merge() */
3311
3312static int
3313c_line_new (symbol, paddr, line_number, frag)
3314 symbolS * symbol;
3315 long paddr;
eb4fd16f 3316 int line_number;
2283f8a0
KR
3317 fragS * frag;
3318{
3319 struct lineno_list *new_line =
3320 (struct lineno_list *) xmalloc (sizeof (struct lineno_list));
3321
3322 segment_info_type *s = segment_info + now_seg;
3323 new_line->line.l_lnno = line_number;
3324
3325 if (line_number == 0)
3326 {
3327 last_line_symbol = symbol;
3328 new_line->line.l_addr.l_symndx = (long) symbol;
3329 }
3330 else
3331 {
3332 new_line->line.l_addr.l_paddr = paddr;
3333 }
3334
3335 new_line->frag = (char *) frag;
3336 new_line->next = (struct lineno_list *) NULL;
3337
3338
3339 if (s->lineno_list_head == (struct lineno_list *) NULL)
3340 {
3341 s->lineno_list_head = new_line;
3342 }
3343 else
3344 {
3345 s->lineno_list_tail->next = new_line;
3346 }
3347 s->lineno_list_tail = new_line;
3348 return LINESZ * s->scnhdr.s_nlnno++;
3349}
3350
3351void
3352c_dot_file_symbol (filename)
3353 char *filename;
3354{
3355 symbolS *symbolP;
3356
3357 symbolP = symbol_new (".file",
3358 SEG_DEBUG,
3359 0,
3360 &zero_address_frag);
3361
3362 S_SET_STORAGE_CLASS (symbolP, C_FILE);
3363 S_SET_NUMBER_AUXILIARY (symbolP, 1);
e5c5ce23
SC
3364
3365 if (strlen (filename) > FILNMLEN)
3366 {
3367 /* Filename is too long to fit into an auxent,
3368 we stick it into the string table instead. We keep
3369 a linked list of the filenames we find so we can emit
3370 them later.*/
3371 struct filename_list *f = xmalloc (sizeof (struct filename_list));
3372
3373 f->filename = filename;
3374 f->next = 0;
3375
3376 SA_SET_FILE_FNAME_ZEROS (symbolP, 0);
3377 SA_SET_FILE_FNAME_OFFSET (symbolP, 0);
3378
3379 if (filename_list_tail)
3380 filename_list_tail->next = f;
3381 else
3382 filename_list_head = f;
3383 filename_list_tail = f;
3384 }
3385 else
3386 {
3387 SA_SET_FILE_FNAME (symbolP, filename);
3388 }
2283f8a0
KR
3389#ifndef NO_LISTING
3390 {
3391 extern int listing;
3392 if (listing)
3393 {
3394 listing_source_file (filename);
3395 }
3396
3397 }
3398
3399#endif
3400 SF_SET_DEBUG (symbolP);
3401 S_SET_VALUE (symbolP, (valueT) previous_file_symbol);
3402
3403 previous_file_symbol = symbolP;
3404
3405 /* Make sure that the symbol is first on the symbol chain */
3406 if (symbol_rootP != symbolP)
3407 {
3408 if (symbolP == symbol_lastP)
3409 {
3410 symbol_lastP = symbol_lastP->sy_previous;
3411 } /* if it was the last thing on the list */
3412
3413 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
3414 symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP);
3415 symbol_rootP = symbolP;
3416 } /* if not first on the list */
3417
3418} /* c_dot_file_symbol() */
3419
3420/*
3421 * Build a 'section static' symbol.
3422 */
3423
3424symbolS *
3425c_section_symbol (name, idx)
3426 char *name;
3427 int idx;
3428{
3429 symbolS *symbolP;
3430
3431 symbolP = symbol_new (name, idx,
3432 0,
3433 &zero_address_frag);
3434
3435 S_SET_STORAGE_CLASS (symbolP, C_STAT);
3436 S_SET_NUMBER_AUXILIARY (symbolP, 1);
3437
3438 SF_SET_STATICS (symbolP);
3439
3440 return symbolP;
3441} /* c_section_symbol() */
3442
3443static void
3444w_symbols (abfd, where, symbol_rootP)
3445 bfd * abfd;
3446 char *where;
3447 symbolS * symbol_rootP;
3448{
3449 symbolS *symbolP;
3450 unsigned int i;
3451
3452 /* First fill in those values we have only just worked out */
3453 for (i = SEG_E0; i < SEG_E9; i++)
3454 {
3455 symbolP = segment_info[i].dot;
3456 if (symbolP)
3457 {
2283f8a0
KR
3458 SA_SET_SCN_SCNLEN (symbolP, segment_info[i].scnhdr.s_size);
3459 SA_SET_SCN_NRELOC (symbolP, segment_info[i].scnhdr.s_nreloc);
3460 SA_SET_SCN_NLINNO (symbolP, segment_info[i].scnhdr.s_nlnno);
2283f8a0
KR
3461 }
3462 }
3463
3464 /*
3465 * Emit all symbols left in the symbol chain.
3466 */
3467 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
3468 {
3469 /* Used to save the offset of the name. It is used to point
3470 to the string in memory but must be a file offset. */
3471 register char *temp;
3472
3473 tc_coff_symbol_emit_hook (symbolP);
3474
3475 temp = S_GET_NAME (symbolP);
3476 if (SF_GET_STRING (symbolP))
3477 {
3478 S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
3479 S_SET_ZEROES (symbolP, 0);
3480 }
3481 else
3482 {
3483 memset (symbolP->sy_symbol.ost_entry.n_name, 0, SYMNMLEN);
3484 strncpy (symbolP->sy_symbol.ost_entry.n_name, temp, SYMNMLEN);
3485 }
3486 where = symbol_to_chars (abfd, where, symbolP);
3487 S_SET_NAME (symbolP, temp);
3488 }
3489
3490} /* w_symbols() */
3491
3492static void
3493obj_coff_lcomm (ignore)
3494 int ignore;
3495{
3496 s_lcomm(0);
3497 return;
3498#if 0
3499 char *name;
3500 char c;
3501 int temp;
3502 char *p;
3503
3504 symbolS *symbolP;
3505
3506 name = input_line_pointer;
3507
3508 c = get_symbol_end ();
3509 p = input_line_pointer;
3510 *p = c;
3511 SKIP_WHITESPACE ();
3512 if (*input_line_pointer != ',')
3513 {
3514 as_bad ("Expected comma after name");
3515 ignore_rest_of_line ();
3516 return;
3517 }
3518 if (*input_line_pointer == '\n')
3519 {
3520 as_bad ("Missing size expression");
3521 return;
3522 }
3523 input_line_pointer++;
3524 if ((temp = get_absolute_expression ()) < 0)
3525 {
3526 as_warn ("lcomm length (%d.) <0! Ignored.", temp);
3527 ignore_rest_of_line ();
3528 return;
3529 }
3530 *p = 0;
3531
3532 symbolP = symbol_find_or_make(name);
3533
3534 if (S_GET_SEGMENT(symbolP) == SEG_UNKNOWN &&
3535 S_GET_VALUE(symbolP) == 0)
3536 {
3537 if (! need_pass_2)
3538 {
3539 char *p;
3540 segT current_seg = now_seg; /* save current seg */
3541 subsegT current_subseg = now_subseg;
3542
3543 subseg_set (SEG_E2, 1);
3544 symbolP->sy_frag = frag_now;
3545 p = frag_var(rs_org, 1, 1, (relax_substateT)0, symbolP,
3546 temp, (char *)0);
3547 *p = 0;
3548 subseg_set (current_seg, current_subseg); /* restore current seg */
3549 S_SET_SEGMENT(symbolP, SEG_E2);
3550 S_SET_STORAGE_CLASS(symbolP, C_STAT);
3551 }
3552 }
3553 else
3554 as_bad("Symbol %s already defined", name);
3555
3556 demand_empty_rest_of_line();
3557#endif
3558}
3559
3560static void
3561fixup_mdeps (frags, h, this_segment)
3562 fragS * frags;
3563 object_headers * h;
3564 segT this_segment;
3565{
3566 subseg_change (this_segment, 0);
3567 while (frags)
3568 {
3569 switch (frags->fr_type)
3570 {
3571 case rs_align:
3572 case rs_org:
a91c6b08
ILT
3573#ifdef HANDLE_ALIGN
3574 HANDLE_ALIGN (frags);
3575#endif
2283f8a0
KR
3576 frags->fr_type = rs_fill;
3577 frags->fr_offset =
3578 (frags->fr_next->fr_address - frags->fr_address - frags->fr_fix);
3579 break;
3580 case rs_machine_dependent:
a91c6b08 3581 md_convert_frag (h, this_segment, frags);
2283f8a0
KR
3582 frag_wane (frags);
3583 break;
3584 default:
3585 ;
3586 }
3587 frags = frags->fr_next;
3588 }
3589}
3590
3591#if 1
a91c6b08
ILT
3592
3593#ifndef TC_FORCE_RELOCATION
3594#define TC_FORCE_RELOCATION(fix) 0
3595#endif
3596
2283f8a0
KR
3597static void
3598fixup_segment (segP, this_segment_type)
3599 segment_info_type * segP;
3600 segT this_segment_type;
3601{
3602 register fixS * fixP;
3603 register symbolS *add_symbolP;
3604 register symbolS *sub_symbolP;
1356d77d 3605 long add_number;
2283f8a0
KR
3606 register int size;
3607 register char *place;
3608 register long where;
3609 register char pcrel;
3610 register fragS *fragP;
3611 register segT add_symbol_segment = absolute_section;
3612
d21041b3
SS
3613 if (linkrelax)
3614 return;
3615
2283f8a0
KR
3616 for (fixP = segP->fix_root; fixP; fixP = fixP->fx_next)
3617 {
3618 fragP = fixP->fx_frag;
3619 know (fragP);
3620 where = fixP->fx_where;
3621 place = fragP->fr_literal + where;
3622 size = fixP->fx_size;
3623 add_symbolP = fixP->fx_addsy;
3624#ifdef TC_I960
eb4fd16f 3625 if (fixP->fx_tcbit && SF_GET_CALLNAME (add_symbolP))
2283f8a0
KR
3626 {
3627 /* Relocation should be done via the associated 'bal' entry
3628 point symbol. */
3629
eb4fd16f 3630 if (!SF_GET_BALNAME (tc_get_bal_of_call (add_symbolP)))
2283f8a0 3631 {
eb4fd16f
ILT
3632 as_bad_where (fixP->fx_file, fixP->fx_line,
3633 "No 'bal' entry point for leafproc %s",
3634 S_GET_NAME (add_symbolP));
2283f8a0
KR
3635 continue;
3636 }
3637 fixP->fx_addsy = add_symbolP = tc_get_bal_of_call (add_symbolP);
3638 }
3639#endif
3640 sub_symbolP = fixP->fx_subsy;
3641 add_number = fixP->fx_offset;
3642 pcrel = fixP->fx_pcrel;
3643
1356d77d
ILT
3644 if (add_symbolP->sy_mri_common)
3645 {
3646 know (add_symbolP->sy_value.X_op == O_symbol);
3647 add_number += S_GET_VALUE (add_symbolP);
3648 fixP->fx_offset = add_number;
3649 add_symbolP = fixP->fx_addsy = add_symbolP->sy_value.X_add_symbol;
3650 }
3651
2283f8a0
KR
3652 if (add_symbolP)
3653 {
3654 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
3655 } /* if there is an addend */
3656
3657 if (sub_symbolP)
3658 {
3659 if (!add_symbolP)
3660 {
3661 /* Its just -sym */
3662 if (S_GET_SEGMENT (sub_symbolP) != absolute_section)
3663 {
eb4fd16f
ILT
3664 as_bad_where (fixP->fx_file, fixP->fx_line,
3665 "Negative of non-absolute symbol %s",
3666 S_GET_NAME (sub_symbolP));
2283f8a0
KR
3667 } /* not absolute */
3668
3669 add_number -= S_GET_VALUE (sub_symbolP);
3670 fixP->fx_subsy = 0;
3671
3672 /* if sub_symbol is in the same segment that add_symbol
3673 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
3674 }
3675 else if ((S_GET_SEGMENT (sub_symbolP) == add_symbol_segment)
3676 && (SEG_NORMAL (add_symbol_segment)
3677 || (add_symbol_segment == absolute_section)))
3678 {
3679 /* Difference of 2 symbols from same segment. Can't
3680 make difference of 2 undefineds: 'value' means
3681 something different for N_UNDF. */
3682#ifdef TC_I960
3683 /* Makes no sense to use the difference of 2 arbitrary symbols
3684 as the target of a call instruction. */
3685 if (fixP->fx_tcbit)
3686 {
eb4fd16f
ILT
3687 as_bad_where (fixP->fx_file, fixP->fx_line,
3688 "callj to difference of 2 symbols");
2283f8a0
KR
3689 }
3690#endif /* TC_I960 */
3691 add_number += S_GET_VALUE (add_symbolP) -
3692 S_GET_VALUE (sub_symbolP);
2283f8a0 3693 add_symbolP = NULL;
a91c6b08
ILT
3694
3695 if (!TC_FORCE_RELOCATION (fixP))
3696 {
3697 fixP->fx_addsy = NULL;
3698 fixP->fx_subsy = NULL;
3699 fixP->fx_done = 1;
1356d77d
ILT
3700#ifdef TC_M68K /* is this right? */
3701 pcrel = 0;
3702 fixP->fx_pcrel = 0;
3703#endif
a91c6b08 3704 }
2283f8a0
KR
3705 }
3706 else
3707 {
3708 /* Different segments in subtraction. */
3709 know (!(S_IS_EXTERNAL (sub_symbolP) && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
3710
3711 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
3712 {
3713 add_number -= S_GET_VALUE (sub_symbolP);
3714 }
3715#ifdef DIFF_EXPR_OK
3716 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
3717#if 0 /* Okay for 68k, at least... */
3718 && !pcrel
3719#endif
3720 )
3721 {
3722 /* Make it pc-relative. */
3723 add_number += (md_pcrel_from (fixP)
3724 - S_GET_VALUE (sub_symbolP));
3725 pcrel = 1;
3726 fixP->fx_pcrel = 1;
3727 sub_symbolP = 0;
3728 fixP->fx_subsy = 0;
3729 }
3730#endif
3731 else
3732 {
eb4fd16f
ILT
3733 as_bad_where (fixP->fx_file, fixP->fx_line,
3734 "Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %ld.",
3735 segment_name (S_GET_SEGMENT (sub_symbolP)),
3736 S_GET_NAME (sub_symbolP),
3737 (long) (fragP->fr_address + where));
2283f8a0
KR
3738 } /* if absolute */
3739 }
3740 } /* if sub_symbolP */
3741
3742 if (add_symbolP)
3743 {
3744 if (add_symbol_segment == this_segment_type && pcrel)
3745 {
3746 /*
3747 * This fixup was made when the symbol's segment was
3748 * SEG_UNKNOWN, but it is now in the local segment.
3749 * So we know how to do the address without relocation.
3750 */
3751#ifdef TC_I960
3752 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
3753 * in which cases it modifies *fixP as appropriate. In the case
3754 * of a 'calls', no further work is required, and *fixP has been
3755 * set up to make the rest of the code below a no-op.
3756 */
3757 reloc_callj (fixP);
3758#endif /* TC_I960 */
3759
3760 add_number += S_GET_VALUE (add_symbolP);
3761 add_number -= md_pcrel_from (fixP);
3762#if defined (TC_I386) || defined (TE_LYNX)
3763 /* On the 386 we must adjust by the segment
3764 vaddr as well. Ian Taylor. */
3765 add_number -= segP->scnhdr.s_vaddr;
3766#endif
3767 pcrel = 0; /* Lie. Don't want further pcrel processing. */
a91c6b08
ILT
3768 if (!TC_FORCE_RELOCATION (fixP))
3769 {
3770 fixP->fx_addsy = NULL;
3771 fixP->fx_done = 1;
3772 }
2283f8a0
KR
3773 }
3774 else
3775 {
3776 switch (add_symbol_segment)
3777 {
3778 case absolute_section:
3779#ifdef TC_I960
3780 reloc_callj (fixP); /* See comment about reloc_callj() above*/
3781#endif /* TC_I960 */
3782 add_number += S_GET_VALUE (add_symbolP);
2283f8a0 3783 add_symbolP = NULL;
a91c6b08
ILT
3784
3785 if (!TC_FORCE_RELOCATION (fixP))
3786 {
3787 fixP->fx_addsy = NULL;
3788 fixP->fx_done = 1;
3789 }
2283f8a0
KR
3790 break;
3791 default:
3792
d21041b3
SS
3793
3794#if defined(TC_A29K) || (defined(TE_PE) && defined(TC_I386))
2283f8a0
KR
3795 /* This really should be handled in the linker, but
3796 backward compatibility forbids. */
3797 add_number += S_GET_VALUE (add_symbolP);
3798#else
3799 add_number += S_GET_VALUE (add_symbolP) +
3800 segment_info[S_GET_SEGMENT (add_symbolP)].scnhdr.s_paddr;
3801#endif
3802 break;
3803
3804 case SEG_UNKNOWN:
3805#ifdef TC_I960
3806 if ((int) fixP->fx_bit_fixP == 13)
3807 {
3808 /* This is a COBR instruction. They have only a
3809 * 13-bit displacement and are only to be used
3810 * for local branches: flag as error, don't generate
3811 * relocation.
3812 */
eb4fd16f
ILT
3813 as_bad_where (fixP->fx_file, fixP->fx_line,
3814 "can't use COBR format with external label");
2283f8a0
KR
3815 fixP->fx_addsy = NULL;
3816 fixP->fx_done = 1;
3817 continue;
3818 } /* COBR */
3819#endif /* TC_I960 */
30355216 3820#if (defined (TC_I386) || defined (TE_LYNX)) && !defined(TE_PE)
0eb1f9ce
KR
3821 /* 386 COFF uses a peculiar format in which the
3822 value of a common symbol is stored in the .text
3823 segment (I've checked this on SVR3.2 and SCO
3824 3.2.2) Ian Taylor <ian@cygnus.com>. */
2283f8a0
KR
3825 if (S_IS_COMMON (add_symbolP))
3826 add_number += S_GET_VALUE (add_symbolP);
3827#endif
3828 break;
3829
3830
3831 } /* switch on symbol seg */
3832 } /* if not in local seg */
3833 } /* if there was a + symbol */
3834
3835 if (pcrel)
3836 {
d21041b3 3837#if !defined(TC_M88K) && !(defined(TE_PE) && defined(TC_I386))
2283f8a0
KR
3838 /* This adjustment is not correct on the m88k, for which the
3839 linker does all the computation. */
3840 add_number -= md_pcrel_from (fixP);
3841#endif
3842 if (add_symbolP == 0)
3843 {
3844 fixP->fx_addsy = &abs_symbol;
3845 } /* if there's an add_symbol */
3846#if defined (TC_I386) || defined (TE_LYNX)
3847 /* On the 386 we must adjust by the segment vaddr
3848 as well. Ian Taylor. */
3849 add_number -= segP->scnhdr.s_vaddr;
3850#endif
3851 } /* if pcrel */
3852
3853 if (!fixP->fx_bit_fixP)
3854 {
3855#ifndef TC_M88K
3856 /* The m88k uses the offset field of the reloc to get around
3857 this problem. */
d675782a
KR
3858 if ((size == 1
3859 && (add_number & ~0xFF)
3860 && ((add_number & ~0xFF) != (-1 & ~0xFF)))
3861 || (size == 2
3862 && (add_number & ~0xFFFF)
3863 && ((add_number & ~0xFFFF) != (-1 & ~0xFFFF))))
2283f8a0 3864 {
eb4fd16f
ILT
3865 as_bad_where (fixP->fx_file, fixP->fx_line,
3866 "Value of %ld too large for field of %d bytes at 0x%lx",
3867 (long) add_number, size,
3868 (unsigned long) (fragP->fr_address + where));
d675782a 3869 }
2283f8a0
KR
3870#endif
3871#ifdef WARN_SIGNED_OVERFLOW_WORD
0eb1f9ce
KR
3872 /* Warn if a .word value is too large when treated as a
3873 signed number. We already know it is not too negative.
3874 This is to catch over-large switches generated by gcc on
3875 the 68k. */
def66e24 3876 if (!flag_signed_overflow_ok
2283f8a0
KR
3877 && size == 2
3878 && add_number > 0x7fff)
eb4fd16f
ILT
3879 as_bad_where (fixP->fx_file, fixP->fx_line,
3880 "Signed .word overflow; switch may be too large; %ld at 0x%lx",
3881 (long) add_number,
3882 (unsigned long) (fragP->fr_address + where));
2283f8a0
KR
3883#endif
3884 } /* not a bit fix */
0eb1f9ce
KR
3885 /* Once this fix has been applied, we don't have to output
3886 anything nothing more need be done. */
a91c6b08
ILT
3887#ifdef MD_APPLY_FIX3
3888 md_apply_fix3 (fixP, &add_number, this_segment_type);
3889#else
2283f8a0 3890 md_apply_fix (fixP, add_number);
a91c6b08 3891#endif
2283f8a0
KR
3892 } /* For each fixS in this segment. */
3893} /* fixup_segment() */
3894
3895#endif
3896
3897/* The first entry in a .stab section is special. */
3898
3899void
3900obj_coff_init_stab_section (seg)
3901 segT seg;
3902{
3903 char *file;
3904 char *p;
3905 char *stabstr_name;
3906 unsigned int stroff;
3907
3908 /* Make space for this first symbol. */
3909 p = frag_more (12);
3910 /* Zero it out. */
3911 memset (p, 0, 12);
3912 as_where (&file, (unsigned int *) NULL);
3913 stabstr_name = (char *) alloca (strlen (segment_info[seg].scnhdr.s_name) + 4);
3914 strcpy (stabstr_name, segment_info[seg].scnhdr.s_name);
3915 strcat (stabstr_name, "str");
3916 stroff = get_stab_string_offset (file, stabstr_name);
3917 know (stroff == 1);
3918 md_number_to_chars (p, stroff, 4);
3919}
3920
3921/* Fill in the counts in the first entry in a .stab section. */
3922
3923static void
3924adjust_stab_section(abfd, seg)
3925 bfd *abfd;
3926 segT seg;
3927{
3928 segT stabstrseg = SEG_UNKNOWN;
3929 char *secname, *name, *name2;
3930 char *p = NULL;
3931 int i, strsz = 0, nsyms;
3932 fragS *frag = segment_info[seg].frchainP->frch_root;
3933
3934 /* Look for the associated string table section. */
3935
3936 secname = segment_info[seg].scnhdr.s_name;
3937 name = (char *) alloca (strlen (secname) + 4);
3938 strcpy (name, secname);
3939 strcat (name, "str");
3940
3941 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
3942 {
3943 name2 = segment_info[i].scnhdr.s_name;
3944 if (name2 != NULL && strncmp(name2, name, 8) == 0)
3945 {
3946 stabstrseg = i;
3947 break;
3948 }
3949 }
3950
3951 /* If we found the section, get its size. */
3952 if (stabstrseg != SEG_UNKNOWN)
3953 strsz = size_section (abfd, stabstrseg);
3954
3955 nsyms = size_section (abfd, seg) / 12 - 1;
3956
3957 /* Look for the first frag of sufficient size for the initial stab
3958 symbol, and collect a pointer to it. */
3959 while (frag && frag->fr_fix < 12)
3960 frag = frag->fr_next;
3961 assert (frag != 0);
3962 p = frag->fr_literal;
3963 assert (p != 0);
3964
3965 /* Write in the number of stab symbols and the size of the string
3966 table. */
3967 bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6);
3968 bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8);
3969}
3970
2283f8a0 3971#endif /* not BFD_ASSEMBLER */
eb4fd16f
ILT
3972
3973const pseudo_typeS obj_pseudo_table[] =
3974{
3975 {"def", obj_coff_def, 0},
3976 {"dim", obj_coff_dim, 0},
3977 {"endef", obj_coff_endef, 0},
3978 {"line", obj_coff_line, 0},
3979 {"ln", obj_coff_ln, 0},
3980 {"appline", obj_coff_ln, 1},
3981 {"scl", obj_coff_scl, 0},
3982 {"size", obj_coff_size, 0},
3983 {"tag", obj_coff_tag, 0},
3984 {"type", obj_coff_type, 0},
3985 {"val", obj_coff_val, 0},
3986 {"section", obj_coff_section, 0},
3987#ifndef BFD_ASSEMBLER
3988 {"use", obj_coff_section, 0},
3989 {"sect", obj_coff_section, 0},
3990 {"text", obj_coff_text, 0},
3991 {"data", obj_coff_data, 0},
3992 {"bss", obj_coff_bss, 0},
3993 {"lcomm", obj_coff_lcomm, 0},
3994 {"ident", obj_coff_ident, 0},
3995#else
3996 {"optim", s_ignore, 0}, /* For sun386i cc (?) */
3997 {"ident", s_ignore, 0}, /* we don't yet handle this. */
3998#endif
3999 {"ABORT", s_abort, 0},
4000#ifdef TC_M88K
4001 /* The m88k uses sdef instead of def. */
4002 {"sdef", obj_coff_def, 0},
4003#endif
4004 {NULL} /* end sentinel */
4005}; /* obj_pseudo_table */
This page took 0.387258 seconds and 4 git commands to generate.