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