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