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