2000-07-28 Jason Eckhardt <jle@cygnus.com>
[binutils.git] / gas / config / obj-elf.c
blob378df9a659c22f415c858c33d5eb7ef8ac5df42a
1 /* ELF object file format
2 Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2,
10 or (at your option) any later version.
12 GAS is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #define OBJ_HEADER "obj-elf.h"
23 #include "as.h"
24 #include "subsegs.h"
25 #include "obstack.h"
27 #ifndef ECOFF_DEBUGGING
28 #define ECOFF_DEBUGGING 0
29 #else
30 #define NEED_ECOFF_DEBUG
31 #endif
33 #ifdef NEED_ECOFF_DEBUG
34 #include "ecoff.h"
35 #endif
37 #ifdef TC_ALPHA
38 #include "elf/alpha.h"
39 #endif
41 #ifdef TC_MIPS
42 #include "elf/mips.h"
43 #endif
45 #ifdef TC_PPC
46 #include "elf/ppc.h"
47 #endif
49 #ifdef TC_I370
50 #include "elf/i370.h"
51 #endif
53 static bfd_vma elf_s_get_size PARAMS ((symbolS *));
54 static void elf_s_set_size PARAMS ((symbolS *, bfd_vma));
55 static bfd_vma elf_s_get_align PARAMS ((symbolS *));
56 static void elf_s_set_align PARAMS ((symbolS *, bfd_vma));
57 static void elf_s_set_other PARAMS ((symbolS *, int));
58 static void elf_copy_symbol_attributes PARAMS ((symbolS *, symbolS *));
59 static int elf_sec_sym_ok_for_reloc PARAMS ((asection *));
60 static void adjust_stab_sections PARAMS ((bfd *, asection *, PTR));
61 static int elf_separate_stab_sections PARAMS ((void));
62 static void elf_init_stab_section PARAMS ((segT));
64 #ifdef NEED_ECOFF_DEBUG
65 static boolean elf_get_extr PARAMS ((asymbol *, EXTR *));
66 static void elf_set_index PARAMS ((asymbol *, bfd_size_type));
67 #endif
69 static void obj_elf_line PARAMS ((int));
70 void obj_elf_version PARAMS ((int));
71 static void obj_elf_size PARAMS ((int));
72 static void obj_elf_type PARAMS ((int));
73 static void obj_elf_ident PARAMS ((int));
74 static void obj_elf_weak PARAMS ((int));
75 static void obj_elf_local PARAMS ((int));
76 static void obj_elf_visibility PARAMS ((int));
77 static void obj_elf_symver PARAMS ((int));
78 static void obj_elf_vtable_inherit PARAMS ((int));
79 static void obj_elf_vtable_entry PARAMS ((int));
80 static void obj_elf_subsection PARAMS ((int));
81 static void obj_elf_popsection PARAMS ((int));
83 static const pseudo_typeS elf_pseudo_table[] =
85 {"comm", obj_elf_common, 0},
86 {"common", obj_elf_common, 1},
87 {"ident", obj_elf_ident, 0},
88 {"local", obj_elf_local, 0},
89 {"previous", obj_elf_previous, 0},
90 {"section", obj_elf_section, 0},
91 {"section.s", obj_elf_section, 0},
92 {"sect", obj_elf_section, 0},
93 {"sect.s", obj_elf_section, 0},
94 {"pushsection", obj_elf_section, 1},
95 {"popsection", obj_elf_popsection, 0},
96 {"size", obj_elf_size, 0},
97 {"type", obj_elf_type, 0},
98 {"version", obj_elf_version, 0},
99 {"weak", obj_elf_weak, 0},
101 /* These define symbol visibility. */
102 {"internal", obj_elf_visibility, STV_INTERNAL},
103 {"hidden", obj_elf_visibility, STV_HIDDEN},
104 {"protected", obj_elf_visibility, STV_PROTECTED},
106 /* These are used for stabs-in-elf configurations. */
107 {"line", obj_elf_line, 0},
109 /* This is a GNU extension to handle symbol versions. */
110 {"symver", obj_elf_symver, 0},
112 /* A GNU extension to change subsection only. */
113 {"subsection", obj_elf_subsection, 0},
115 /* These are GNU extensions to aid in garbage collecting C++ vtables. */
116 {"vtable_inherit", obj_elf_vtable_inherit, 0},
117 {"vtable_entry", obj_elf_vtable_entry, 0},
119 /* These are used for dwarf. */
120 {"2byte", cons, 2},
121 {"4byte", cons, 4},
122 {"8byte", cons, 8},
124 /* We need to trap the section changing calls to handle .previous. */
125 {"data", obj_elf_data, 0},
126 {"text", obj_elf_text, 0},
128 /* End sentinel. */
129 {NULL, NULL, 0},
132 static const pseudo_typeS ecoff_debug_pseudo_table[] =
134 #ifdef NEED_ECOFF_DEBUG
135 /* COFF style debugging information for ECOFF. .ln is not used; .loc
136 is used instead. */
137 { "def", ecoff_directive_def, 0 },
138 { "dim", ecoff_directive_dim, 0 },
139 { "endef", ecoff_directive_endef, 0 },
140 { "file", ecoff_directive_file, 0 },
141 { "scl", ecoff_directive_scl, 0 },
142 { "tag", ecoff_directive_tag, 0 },
143 { "val", ecoff_directive_val, 0 },
145 /* COFF debugging requires pseudo-ops .size and .type, but ELF
146 already has meanings for those. We use .esize and .etype
147 instead. These are only generated by gcc anyhow. */
148 { "esize", ecoff_directive_size, 0 },
149 { "etype", ecoff_directive_type, 0 },
151 /* ECOFF specific debugging information. */
152 { "begin", ecoff_directive_begin, 0 },
153 { "bend", ecoff_directive_bend, 0 },
154 { "end", ecoff_directive_end, 0 },
155 { "ent", ecoff_directive_ent, 0 },
156 { "fmask", ecoff_directive_fmask, 0 },
157 { "frame", ecoff_directive_frame, 0 },
158 { "loc", ecoff_directive_loc, 0 },
159 { "mask", ecoff_directive_mask, 0 },
161 /* Other ECOFF directives. */
162 { "extern", ecoff_directive_extern, 0 },
164 /* These are used on Irix. I don't know how to implement them. */
165 { "alias", s_ignore, 0 },
166 { "bgnb", s_ignore, 0 },
167 { "endb", s_ignore, 0 },
168 { "lab", s_ignore, 0 },
169 { "noalias", s_ignore, 0 },
170 { "verstamp", s_ignore, 0 },
171 { "vreg", s_ignore, 0 },
172 #endif
174 {NULL, NULL, 0} /* end sentinel */
177 #undef NO_RELOC
178 #include "aout/aout64.h"
180 /* This is called when the assembler starts. */
182 void
183 elf_begin ()
185 /* Add symbols for the known sections to the symbol table. */
186 symbol_table_insert (section_symbol (bfd_get_section_by_name (stdoutput,
187 TEXT_SECTION_NAME)));
188 symbol_table_insert (section_symbol (bfd_get_section_by_name (stdoutput,
189 DATA_SECTION_NAME)));
190 symbol_table_insert (section_symbol (bfd_get_section_by_name (stdoutput,
191 BSS_SECTION_NAME)));
194 void
195 elf_pop_insert ()
197 pop_insert (elf_pseudo_table);
198 if (ECOFF_DEBUGGING)
199 pop_insert (ecoff_debug_pseudo_table);
202 static bfd_vma
203 elf_s_get_size (sym)
204 symbolS *sym;
206 return S_GET_SIZE (sym);
209 static void
210 elf_s_set_size (sym, sz)
211 symbolS *sym;
212 bfd_vma sz;
214 S_SET_SIZE (sym, sz);
217 static bfd_vma
218 elf_s_get_align (sym)
219 symbolS *sym;
221 return S_GET_ALIGN (sym);
224 static void
225 elf_s_set_align (sym, align)
226 symbolS *sym;
227 bfd_vma align;
229 S_SET_ALIGN (sym, align);
233 elf_s_get_other (sym)
234 symbolS *sym;
236 return elf_symbol (symbol_get_bfdsym (sym))->internal_elf_sym.st_other;
239 static void
240 elf_s_set_other (sym, other)
241 symbolS *sym;
242 int other;
244 S_SET_OTHER (sym, other);
247 static void
248 elf_copy_symbol_attributes (dest, src)
249 symbolS *dest, *src;
251 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
254 static int
255 elf_sec_sym_ok_for_reloc (sec)
256 asection *sec;
258 return obj_sec_sym_ok_for_reloc (sec);
261 void
262 elf_file_symbol (s)
263 const char *s;
265 symbolS *sym;
267 sym = symbol_new (s, absolute_section, (valueT) 0, (struct frag *) 0);
268 symbol_set_frag (sym, &zero_address_frag);
269 symbol_get_bfdsym (sym)->flags |= BSF_FILE;
271 if (symbol_rootP != sym)
273 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
274 symbol_insert (sym, symbol_rootP, &symbol_rootP, &symbol_lastP);
275 #ifdef DEBUG
276 verify_symbol_chain (symbol_rootP, symbol_lastP);
277 #endif
280 #ifdef NEED_ECOFF_DEBUG
281 ecoff_new_file (s);
282 #endif
285 void
286 obj_elf_common (is_common)
287 int is_common;
289 char *name;
290 char c;
291 char *p;
292 int temp, size;
293 symbolS *symbolP;
294 int have_align;
296 if (flag_mri && is_common)
298 s_mri_common (0);
299 return;
302 name = input_line_pointer;
303 c = get_symbol_end ();
304 /* just after name is now '\0' */
305 p = input_line_pointer;
306 *p = c;
307 SKIP_WHITESPACE ();
308 if (*input_line_pointer != ',')
310 as_bad (_("Expected comma after symbol-name"));
311 ignore_rest_of_line ();
312 return;
314 input_line_pointer++; /* skip ',' */
315 if ((temp = get_absolute_expression ()) < 0)
317 as_bad (_(".COMMon length (%d.) <0! Ignored."), temp);
318 ignore_rest_of_line ();
319 return;
321 size = temp;
322 *p = 0;
323 symbolP = symbol_find_or_make (name);
324 *p = c;
325 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
327 as_bad (_("Ignoring attempt to re-define symbol"));
328 ignore_rest_of_line ();
329 return;
331 if (S_GET_VALUE (symbolP) != 0)
333 if (S_GET_VALUE (symbolP) != (valueT) size)
335 as_warn (_("Length of .comm \"%s\" is already %ld. Not changed to %d."),
336 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
339 know (symbolP->sy_frag == &zero_address_frag);
340 if (*input_line_pointer != ',')
341 have_align = 0;
342 else
344 have_align = 1;
345 input_line_pointer++;
346 SKIP_WHITESPACE ();
348 if (! have_align || *input_line_pointer != '"')
350 if (! have_align)
351 temp = 0;
352 else
354 temp = get_absolute_expression ();
355 if (temp < 0)
357 temp = 0;
358 as_warn (_("Common alignment negative; 0 assumed"));
361 if (symbol_get_obj (symbolP)->local)
363 segT old_sec;
364 int old_subsec;
365 char *pfrag;
366 int align;
368 /* allocate_bss: */
369 old_sec = now_seg;
370 old_subsec = now_subseg;
371 if (temp)
373 /* convert to a power of 2 alignment */
374 for (align = 0; (temp & 1) == 0; temp >>= 1, ++align);
375 if (temp != 1)
377 as_bad (_("Common alignment not a power of 2"));
378 ignore_rest_of_line ();
379 return;
382 else
383 align = 0;
384 record_alignment (bss_section, align);
385 subseg_set (bss_section, 0);
386 if (align)
387 frag_align (align, 0, 0);
388 if (S_GET_SEGMENT (symbolP) == bss_section)
389 symbol_get_frag (symbolP)->fr_symbol = 0;
390 symbol_set_frag (symbolP, frag_now);
391 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
392 (offsetT) size, (char *) 0);
393 *pfrag = 0;
394 S_SET_SIZE (symbolP, size);
395 S_SET_SEGMENT (symbolP, bss_section);
396 S_CLEAR_EXTERNAL (symbolP);
397 subseg_set (old_sec, old_subsec);
399 else
401 allocate_common:
402 S_SET_VALUE (symbolP, (valueT) size);
403 S_SET_ALIGN (symbolP, temp);
404 S_SET_EXTERNAL (symbolP);
405 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
408 else
410 input_line_pointer++;
411 /* @@ Some use the dot, some don't. Can we get some consistency?? */
412 if (*input_line_pointer == '.')
413 input_line_pointer++;
414 /* @@ Some say data, some say bss. */
415 if (strncmp (input_line_pointer, "bss\"", 4)
416 && strncmp (input_line_pointer, "data\"", 5))
418 while (*--input_line_pointer != '"')
420 input_line_pointer--;
421 goto bad_common_segment;
423 while (*input_line_pointer++ != '"')
425 goto allocate_common;
428 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
430 demand_empty_rest_of_line ();
431 return;
434 bad_common_segment:
435 p = input_line_pointer;
436 while (*p && *p != '\n')
437 p++;
438 c = *p;
439 *p = '\0';
440 as_bad (_("bad .common segment %s"), input_line_pointer + 1);
441 *p = c;
442 input_line_pointer = p;
443 ignore_rest_of_line ();
444 return;
448 static void
449 obj_elf_local (ignore)
450 int ignore ATTRIBUTE_UNUSED;
452 char *name;
453 int c;
454 symbolS *symbolP;
458 name = input_line_pointer;
459 c = get_symbol_end ();
460 symbolP = symbol_find_or_make (name);
461 *input_line_pointer = c;
462 SKIP_WHITESPACE ();
463 S_CLEAR_EXTERNAL (symbolP);
464 symbol_get_obj (symbolP)->local = 1;
465 if (c == ',')
467 input_line_pointer++;
468 SKIP_WHITESPACE ();
469 if (*input_line_pointer == '\n')
470 c = '\n';
473 while (c == ',');
474 demand_empty_rest_of_line ();
477 static void
478 obj_elf_weak (ignore)
479 int ignore ATTRIBUTE_UNUSED;
481 char *name;
482 int c;
483 symbolS *symbolP;
487 name = input_line_pointer;
488 c = get_symbol_end ();
489 symbolP = symbol_find_or_make (name);
490 *input_line_pointer = c;
491 SKIP_WHITESPACE ();
492 S_SET_WEAK (symbolP);
493 symbol_get_obj (symbolP)->local = 1;
494 if (c == ',')
496 input_line_pointer++;
497 SKIP_WHITESPACE ();
498 if (*input_line_pointer == '\n')
499 c = '\n';
502 while (c == ',');
503 demand_empty_rest_of_line ();
506 static void
507 obj_elf_visibility (visibility)
508 int visibility;
510 char *name;
511 int c;
512 symbolS *symbolP;
513 asymbol *bfdsym;
514 elf_symbol_type *elfsym;
518 name = input_line_pointer;
519 c = get_symbol_end ();
520 symbolP = symbol_find_or_make (name);
521 *input_line_pointer = c;
523 SKIP_WHITESPACE ();
525 bfdsym = symbol_get_bfdsym (symbolP);
526 elfsym = elf_symbol_from (bfd_asymbol_bfd (bfdsym), bfdsym);
528 assert (elfsym);
530 elfsym->internal_elf_sym.st_other = visibility;
532 if (c == ',')
534 input_line_pointer ++;
536 SKIP_WHITESPACE ();
538 if (*input_line_pointer == '\n')
539 c = '\n';
542 while (c == ',');
544 demand_empty_rest_of_line ();
548 static segT previous_section;
549 static int previous_subsection;
551 struct section_stack
553 struct section_stack *next;
554 segT seg, prev_seg;
555 int subseg, prev_subseg;
558 static struct section_stack *section_stack;
561 /* Handle the .section pseudo-op. This code supports two different
562 syntaxes.
564 The first is found on Solaris, and looks like
565 .section ".sec1",#alloc,#execinstr,#write
566 Here the names after '#' are the SHF_* flags to turn on for the
567 section. I'm not sure how it determines the SHT_* type (BFD
568 doesn't really give us control over the type, anyhow).
570 The second format is found on UnixWare, and probably most SVR4
571 machines, and looks like
572 .section .sec1,"a",@progbits
573 The quoted string may contain any combination of a, w, x, and
574 represents the SHF_* flags to turn on for the section. The string
575 beginning with '@' can be progbits or nobits. There should be
576 other possibilities, but I don't know what they are. In any case,
577 BFD doesn't really let us set the section type. */
579 /* Certain named sections have particular defined types, listed on p.
580 4-19 of the ABI. */
581 struct special_section
583 const char *name;
584 int type;
585 int attributes;
588 static struct special_section const special_sections[] =
590 { ".bss", SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
591 { ".comment", SHT_PROGBITS, 0 },
592 { ".data", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
593 { ".data1", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
594 { ".debug", SHT_PROGBITS, 0 },
595 { ".fini", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
596 { ".init", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
597 { ".line", SHT_PROGBITS, 0 },
598 { ".note", SHT_NOTE, 0 },
599 { ".rodata", SHT_PROGBITS, SHF_ALLOC },
600 { ".rodata1", SHT_PROGBITS, SHF_ALLOC },
601 { ".text", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
603 #ifdef ELF_TC_SPECIAL_SECTIONS
604 ELF_TC_SPECIAL_SECTIONS
605 #endif
607 #if 0
608 /* The following section names are special, but they can not
609 reasonably appear in assembler code. Some of the attributes are
610 processor dependent. */
611 { ".dynamic", SHT_DYNAMIC, SHF_ALLOC /* + SHF_WRITE */ },
612 { ".dynstr", SHT_STRTAB, SHF_ALLOC },
613 { ".dynsym", SHT_DYNSYM, SHF_ALLOC },
614 { ".got", SHT_PROGBITS, 0 },
615 { ".hash", SHT_HASH, SHF_ALLOC },
616 { ".interp", SHT_PROGBITS, /* SHF_ALLOC */ },
617 { ".plt", SHT_PROGBITS, 0 },
618 { ".shstrtab",SHT_STRTAB, 0 },
619 { ".strtab", SHT_STRTAB, /* SHF_ALLOC */ },
620 { ".symtab", SHT_SYMTAB, /* SHF_ALLOC */ },
621 #endif
623 { NULL, 0, 0 }
626 void
627 obj_elf_change_section (name, type, attr, push)
628 char *name;
629 int type, attr, push;
631 asection *old_sec;
632 segT sec;
633 flagword flags;
634 int i;
636 #ifdef md_flush_pending_output
637 md_flush_pending_output ();
638 #endif
640 /* Switch to the section, creating it if necessary. */
641 if (push)
643 struct section_stack *elt;
644 elt = xmalloc (sizeof (struct section_stack));
645 elt->next = section_stack;
646 elt->seg = now_seg;
647 elt->prev_seg = previous_section;
648 elt->subseg = now_subseg;
649 elt->prev_subseg = previous_subsection;
650 section_stack = elt;
652 previous_section = now_seg;
653 previous_subsection = now_subseg;
655 old_sec = bfd_get_section_by_name (stdoutput, name);
656 sec = subseg_new (name, 0);
658 /* See if this is one of the special sections. */
659 for (i = 0; special_sections[i].name != NULL; i++)
660 if (strcmp (name, special_sections[i].name) == 0)
662 if (type == SHT_NULL)
663 type = special_sections[i].type;
664 else if (type != special_sections[i].type)
666 if (old_sec == NULL)
668 as_warn (_("Setting incorrect section type for %s"), name);
670 else
672 as_warn (_("Ignoring incorrect section type for %s"), name);
673 type = special_sections[i].type;
676 if ((attr &~ special_sections[i].attributes) != 0
677 && old_sec == NULL)
679 /* As a GNU extension, we permit a .note section to be
680 allocatable. If the linker sees an allocateable .note
681 section, it will create a PT_NOTE segment in the output
682 file. */
683 if (strcmp (name, ".note") != 0
684 || attr != SHF_ALLOC)
685 as_warn (_("Setting incorrect section attributes for %s"),
686 name);
688 attr |= special_sections[i].attributes;
689 break;
692 /* Convert ELF type and flags to BFD flags. */
693 flags = (SEC_RELOC
694 | ((attr & SHF_WRITE) ? 0 : SEC_READONLY)
695 | ((attr & SHF_ALLOC) ? SEC_ALLOC : 0)
696 | (((attr & SHF_ALLOC) && type != SHT_NOBITS) ? SEC_LOAD : 0)
697 | ((attr & SHF_EXECINSTR) ? SEC_CODE : 0));
698 #ifdef md_elf_section_flags
699 flags = md_elf_section_flags (flags, attr, type);
700 #endif
702 if (old_sec == NULL)
704 symbolS *secsym;
706 /* Prevent SEC_HAS_CONTENTS from being inadvertently set. */
707 if (type == SHT_NOBITS)
708 seg_info (sec)->bss = 1;
710 bfd_set_section_flags (stdoutput, sec, flags);
712 /* Add a symbol for this section to the symbol table. */
713 secsym = symbol_find (name);
714 if (secsym != NULL)
715 symbol_set_bfdsym (secsym, sec->symbol);
716 else
717 symbol_table_insert (section_symbol (sec));
719 else if (attr != 0)
721 /* If section attributes are specified the second time we see a
722 particular section, then check that they are the same as we
723 saw the first time. */
724 if ((old_sec->flags ^ flags)
725 & (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
726 | SEC_EXCLUDE | SEC_SORT_ENTRIES))
727 as_warn (_("Ignoring changed section attributes for %s"), name);
730 #ifdef md_elf_section_change_hook
731 md_elf_section_change_hook ();
732 #endif
736 obj_elf_parse_section_letters (str, len)
737 char *str;
738 size_t len;
740 int attr = 0;
742 while (len > 0)
744 switch (*str)
746 case 'a':
747 attr |= SHF_ALLOC;
748 break;
749 case 'w':
750 attr |= SHF_WRITE;
751 break;
752 case 'x':
753 attr |= SHF_EXECINSTR;
754 break;
755 default:
757 char *bad_msg = _("Unrecognized .section attribute: want a,w,x");
758 #ifdef md_elf_section_letter
759 int md_attr = md_elf_section_letter (*str, &bad_msg);
760 if (md_attr >= 0)
761 attr |= md_attr;
762 else
763 #endif
765 as_warn ("%s", bad_msg);
766 attr = -1;
769 break;
771 str++, len--;
774 return attr;
778 obj_elf_section_word (str, len)
779 char *str;
780 size_t len;
782 if (len == 5 && strncmp (str, "write", 5) == 0)
783 return SHF_WRITE;
784 if (len == 5 && strncmp (str, "alloc", 5) == 0)
785 return SHF_ALLOC;
786 if (len == 9 && strncmp (str, "execinstr", 9) == 0)
787 return SHF_EXECINSTR;
789 #ifdef md_elf_section_word
791 int md_attr = md_elf_section_word (str, len);
792 if (md_attr >= 0)
793 return md_attr;
795 #endif
797 as_warn (_("Unrecognized section attribute"));
798 return 0;
802 obj_elf_section_type (str, len)
803 char *str;
804 size_t len;
806 if (len == 8 && strncmp (str, "progbits", 8) == 0)
807 return SHT_PROGBITS;
808 if (len == 6 && strncmp (str, "nobits", 6) == 0)
809 return SHT_NOBITS;
811 #ifdef md_elf_section_type
813 int md_type = md_elf_section_type (str, len);
814 if (md_type >= 0)
815 return md_type;
817 #endif
819 as_warn (_("Unrecognized section type"));
820 return 0;
823 void
824 obj_elf_section (push)
825 int push;
827 char *name, *beg, *end;
828 int type, attr, dummy;
830 #ifndef TC_I370
831 if (flag_mri)
833 char mri_type;
835 #ifdef md_flush_pending_output
836 md_flush_pending_output ();
837 #endif
839 previous_section = now_seg;
840 previous_subsection = now_subseg;
842 s_mri_sect (&mri_type);
844 #ifdef md_elf_section_change_hook
845 md_elf_section_change_hook ();
846 #endif
848 return;
850 #endif /* ! defined (TC_I370) */
852 /* Get name of section. */
853 SKIP_WHITESPACE ();
854 if (*input_line_pointer == '"')
856 name = demand_copy_C_string (&dummy);
857 if (name == NULL)
859 ignore_rest_of_line ();
860 return;
863 else
865 end = input_line_pointer;
866 while (0 == strchr ("\n\t,; ", *end))
867 end++;
868 if (end == input_line_pointer)
870 as_warn (_("Missing section name"));
871 ignore_rest_of_line ();
872 return;
875 name = xmalloc (end - input_line_pointer + 1);
876 memcpy (name, input_line_pointer, end - input_line_pointer);
877 name[end - input_line_pointer] = '\0';
878 input_line_pointer = end;
880 SKIP_WHITESPACE ();
882 type = SHT_NULL;
883 attr = 0;
885 if (*input_line_pointer == ',')
887 /* Skip the comma. */
888 ++input_line_pointer;
889 SKIP_WHITESPACE ();
891 if (*input_line_pointer == '"')
893 beg = demand_copy_C_string (&dummy);
894 if (beg == NULL)
896 ignore_rest_of_line ();
897 return;
899 attr |= obj_elf_parse_section_letters (beg, strlen (beg));
901 SKIP_WHITESPACE ();
902 if (*input_line_pointer == ',')
904 char c;
905 ++input_line_pointer;
906 SKIP_WHITESPACE ();
907 c = *input_line_pointer;
908 if (c == '"')
910 beg = demand_copy_C_string (&dummy);
911 if (beg == NULL)
913 ignore_rest_of_line ();
914 return;
916 type = obj_elf_section_type (beg, strlen (beg));
918 else if (c == '@' || c == '%')
920 beg = ++input_line_pointer;
921 c = get_symbol_end ();
922 *input_line_pointer = c;
923 type = obj_elf_section_type (beg, input_line_pointer - beg);
927 else
931 char c;
933 SKIP_WHITESPACE ();
934 if (*input_line_pointer != '#')
936 as_warn (_("Bad .section directive - character following name is not '#'"));
937 ignore_rest_of_line ();
938 return;
940 beg = ++input_line_pointer;
941 c = get_symbol_end ();
942 *input_line_pointer = c;
944 attr |= obj_elf_section_word (beg, input_line_pointer - beg);
946 SKIP_WHITESPACE ();
948 while (*input_line_pointer++ == ',');
949 --input_line_pointer;
953 demand_empty_rest_of_line ();
955 obj_elf_change_section (name, type, attr, push);
958 /* Change to the .data section. */
960 void
961 obj_elf_data (i)
962 int i;
964 #ifdef md_flush_pending_output
965 md_flush_pending_output ();
966 #endif
968 previous_section = now_seg;
969 previous_subsection = now_subseg;
970 s_data (i);
972 #ifdef md_elf_section_change_hook
973 md_elf_section_change_hook ();
974 #endif
977 /* Change to the .text section. */
979 void
980 obj_elf_text (i)
981 int i;
983 #ifdef md_flush_pending_output
984 md_flush_pending_output ();
985 #endif
987 previous_section = now_seg;
988 previous_subsection = now_subseg;
989 s_text (i);
991 #ifdef md_elf_section_change_hook
992 md_elf_section_change_hook ();
993 #endif
996 static void
997 obj_elf_subsection (ignore)
998 int ignore ATTRIBUTE_UNUSED;
1000 register int temp;
1002 #ifdef md_flush_pending_output
1003 md_flush_pending_output ();
1004 #endif
1006 previous_section = now_seg;
1007 previous_subsection = now_subseg;
1009 temp = get_absolute_expression ();
1010 subseg_set (now_seg, (subsegT) temp);
1011 demand_empty_rest_of_line ();
1013 #ifdef md_elf_section_change_hook
1014 md_elf_section_change_hook ();
1015 #endif
1018 /* This can be called from the processor backends if they change
1019 sections. */
1021 void
1022 obj_elf_section_change_hook ()
1024 previous_section = now_seg;
1025 previous_subsection = now_subseg;
1028 void
1029 obj_elf_previous (ignore)
1030 int ignore ATTRIBUTE_UNUSED;
1032 segT new_section;
1033 int new_subsection;
1035 if (previous_section == 0)
1037 as_bad (_(".previous without corresponding .section; ignored"));
1038 return;
1041 #ifdef md_flush_pending_output
1042 md_flush_pending_output ();
1043 #endif
1045 new_section = previous_section;
1046 new_subsection = previous_subsection;
1047 previous_section = now_seg;
1048 previous_subsection = now_subseg;
1049 subseg_set (new_section, new_subsection);
1051 #ifdef md_elf_section_change_hook
1052 md_elf_section_change_hook ();
1053 #endif
1056 static void
1057 obj_elf_popsection (xxx)
1058 int xxx ATTRIBUTE_UNUSED;
1060 struct section_stack *top = section_stack;
1062 if (top == NULL)
1064 as_bad (_(".popsection without corresponding .pushsection; ignored"));
1065 return;
1068 #ifdef md_flush_pending_output
1069 md_flush_pending_output ();
1070 #endif
1072 section_stack = top->next;
1073 previous_section = top->prev_seg;
1074 previous_subsection = top->prev_subseg;
1075 subseg_set (top->seg, top->subseg);
1076 free (top);
1078 #ifdef md_elf_section_change_hook
1079 md_elf_section_change_hook ();
1080 #endif
1083 static void
1084 obj_elf_line (ignore)
1085 int ignore ATTRIBUTE_UNUSED;
1087 /* Assume delimiter is part of expression. BSD4.2 as fails with
1088 delightful bug, so we are not being incompatible here. */
1089 new_logical_line ((char *) NULL, (int) (get_absolute_expression ()));
1090 demand_empty_rest_of_line ();
1093 /* This handles the .symver pseudo-op, which is used to specify a
1094 symbol version. The syntax is ``.symver NAME,SYMVERNAME''.
1095 SYMVERNAME may contain ELF_VER_CHR ('@') characters. This
1096 pseudo-op causes the assembler to emit a symbol named SYMVERNAME
1097 with the same value as the symbol NAME. */
1099 static void
1100 obj_elf_symver (ignore)
1101 int ignore ATTRIBUTE_UNUSED;
1103 char *name;
1104 char c;
1105 symbolS *sym;
1107 name = input_line_pointer;
1108 c = get_symbol_end ();
1110 sym = symbol_find_or_make (name);
1112 *input_line_pointer = c;
1114 if (symbol_get_obj (sym)->versioned_name != NULL)
1116 as_bad (_("multiple .symver directives for symbol `%s'"),
1117 S_GET_NAME (sym));
1118 ignore_rest_of_line ();
1119 return;
1122 SKIP_WHITESPACE ();
1123 if (*input_line_pointer != ',')
1125 as_bad (_("expected comma after name in .symver"));
1126 ignore_rest_of_line ();
1127 return;
1130 ++input_line_pointer;
1131 name = input_line_pointer;
1132 while (1)
1134 c = get_symbol_end ();
1135 if (c != ELF_VER_CHR)
1136 break;
1137 *input_line_pointer++ = c;
1140 symbol_get_obj (sym)->versioned_name = xstrdup (name);
1142 *input_line_pointer = c;
1144 if (strchr (symbol_get_obj (sym)->versioned_name, ELF_VER_CHR) == NULL)
1146 as_bad (_("missing version name in `%s' for symbol `%s'"),
1147 symbol_get_obj (sym)->versioned_name, S_GET_NAME (sym));
1148 ignore_rest_of_line ();
1149 return;
1152 demand_empty_rest_of_line ();
1155 /* This handles the .vtable_inherit pseudo-op, which is used to indicate
1156 to the linker the hierarchy in which a particular table resides. The
1157 syntax is ".vtable_inherit CHILDNAME, PARENTNAME". */
1159 static void
1160 obj_elf_vtable_inherit (ignore)
1161 int ignore ATTRIBUTE_UNUSED;
1163 char *cname, *pname;
1164 symbolS *csym, *psym;
1165 char c, bad = 0;
1167 if (*input_line_pointer == '#')
1168 ++input_line_pointer;
1170 cname = input_line_pointer;
1171 c = get_symbol_end ();
1172 csym = symbol_find (cname);
1174 /* GCFIXME: should check that we don't have two .vtable_inherits for
1175 the same child symbol. Also, we can currently only do this if the
1176 child symbol is already exists and is placed in a fragment. */
1178 if (csym == NULL || symbol_get_frag (csym) == NULL)
1180 as_bad ("expected `%s' to have already been set for .vtable_inherit",
1181 cname);
1182 bad = 1;
1185 *input_line_pointer = c;
1187 SKIP_WHITESPACE ();
1188 if (*input_line_pointer != ',')
1190 as_bad ("expected comma after name in .vtable_inherit");
1191 ignore_rest_of_line ();
1192 return;
1195 ++input_line_pointer;
1196 SKIP_WHITESPACE ();
1198 if (*input_line_pointer == '#')
1199 ++input_line_pointer;
1201 if (input_line_pointer[0] == '0'
1202 && (input_line_pointer[1] == '\0'
1203 || isspace ((unsigned char) input_line_pointer[1])))
1205 psym = section_symbol (absolute_section);
1206 ++input_line_pointer;
1208 else
1210 pname = input_line_pointer;
1211 c = get_symbol_end ();
1212 psym = symbol_find_or_make (pname);
1213 *input_line_pointer = c;
1216 demand_empty_rest_of_line ();
1218 if (bad)
1219 return;
1221 assert (symbol_get_value_expression (csym)->X_op == O_constant);
1222 fix_new (symbol_get_frag (csym),
1223 symbol_get_value_expression (csym)->X_add_number, 0, psym, 0, 0,
1224 BFD_RELOC_VTABLE_INHERIT);
1227 /* This handles the .vtable_entry pseudo-op, which is used to indicate
1228 to the linker that a vtable slot was used. The syntax is
1229 ".vtable_entry tablename, offset". */
1231 static void
1232 obj_elf_vtable_entry (ignore)
1233 int ignore ATTRIBUTE_UNUSED;
1235 char *name;
1236 symbolS *sym;
1237 offsetT offset;
1238 char c;
1240 if (*input_line_pointer == '#')
1241 ++input_line_pointer;
1243 name = input_line_pointer;
1244 c = get_symbol_end ();
1245 sym = symbol_find_or_make (name);
1246 *input_line_pointer = c;
1248 SKIP_WHITESPACE ();
1249 if (*input_line_pointer != ',')
1251 as_bad ("expected comma after name in .vtable_entry");
1252 ignore_rest_of_line ();
1253 return;
1256 ++input_line_pointer;
1257 if (*input_line_pointer == '#')
1258 ++input_line_pointer;
1260 offset = get_absolute_expression ();
1262 fix_new (frag_now, frag_now_fix (), 0, sym, offset, 0,
1263 BFD_RELOC_VTABLE_ENTRY);
1265 demand_empty_rest_of_line ();
1268 void
1269 elf_obj_read_begin_hook ()
1271 #ifdef NEED_ECOFF_DEBUG
1272 if (ECOFF_DEBUGGING)
1273 ecoff_read_begin_hook ();
1274 #endif
1277 void
1278 elf_obj_symbol_new_hook (symbolP)
1279 symbolS *symbolP;
1281 struct elf_obj_sy *sy_obj;
1283 sy_obj = symbol_get_obj (symbolP);
1284 sy_obj->size = NULL;
1285 sy_obj->versioned_name = NULL;
1287 #ifdef NEED_ECOFF_DEBUG
1288 if (ECOFF_DEBUGGING)
1289 ecoff_symbol_new_hook (symbolP);
1290 #endif
1293 void
1294 obj_elf_version (ignore)
1295 int ignore ATTRIBUTE_UNUSED;
1297 char *name;
1298 unsigned int c;
1299 char ch;
1300 char *p;
1301 asection *seg = now_seg;
1302 subsegT subseg = now_subseg;
1303 Elf_Internal_Note i_note;
1304 Elf_External_Note e_note;
1305 asection *note_secp = (asection *) NULL;
1306 int i, len;
1308 SKIP_WHITESPACE ();
1309 if (*input_line_pointer == '\"')
1311 ++input_line_pointer; /* -> 1st char of string. */
1312 name = input_line_pointer;
1314 while (is_a_char (c = next_char_of_string ()))
1316 c = *input_line_pointer;
1317 *input_line_pointer = '\0';
1318 *(input_line_pointer - 1) = '\0';
1319 *input_line_pointer = c;
1321 /* create the .note section */
1323 note_secp = subseg_new (".note", 0);
1324 bfd_set_section_flags (stdoutput,
1325 note_secp,
1326 SEC_HAS_CONTENTS | SEC_READONLY);
1328 /* process the version string */
1330 len = strlen (name);
1332 i_note.namesz = ((len + 1) + 3) & ~3; /* round this to word boundary */
1333 i_note.descsz = 0; /* no description */
1334 i_note.type = NT_VERSION;
1335 p = frag_more (sizeof (e_note.namesz));
1336 md_number_to_chars (p, (valueT) i_note.namesz, 4);
1337 p = frag_more (sizeof (e_note.descsz));
1338 md_number_to_chars (p, (valueT) i_note.descsz, 4);
1339 p = frag_more (sizeof (e_note.type));
1340 md_number_to_chars (p, (valueT) i_note.type, 4);
1342 for (i = 0; i < len; i++)
1344 ch = *(name + i);
1346 FRAG_APPEND_1_CHAR (ch);
1349 frag_align (2, 0, 0);
1351 subseg_set (seg, subseg);
1353 else
1355 as_bad (_("Expected quoted string"));
1357 demand_empty_rest_of_line ();
1360 static void
1361 obj_elf_size (ignore)
1362 int ignore ATTRIBUTE_UNUSED;
1364 char *name = input_line_pointer;
1365 char c = get_symbol_end ();
1366 char *p;
1367 expressionS exp;
1368 symbolS *sym;
1370 p = input_line_pointer;
1371 *p = c;
1372 SKIP_WHITESPACE ();
1373 if (*input_line_pointer != ',')
1375 *p = 0;
1376 as_bad (_("expected comma after name `%s' in .size directive"), name);
1377 *p = c;
1378 ignore_rest_of_line ();
1379 return;
1381 input_line_pointer++;
1382 expression (&exp);
1383 if (exp.X_op == O_absent)
1385 as_bad (_("missing expression in .size directive"));
1386 exp.X_op = O_constant;
1387 exp.X_add_number = 0;
1389 *p = 0;
1390 sym = symbol_find_or_make (name);
1391 *p = c;
1392 if (exp.X_op == O_constant)
1393 S_SET_SIZE (sym, exp.X_add_number);
1394 else
1396 symbol_get_obj (sym)->size =
1397 (expressionS *) xmalloc (sizeof (expressionS));
1398 *symbol_get_obj (sym)->size = exp;
1400 demand_empty_rest_of_line ();
1403 /* Handle the ELF .type pseudo-op. This sets the type of a symbol.
1404 There are five syntaxes:
1406 The first (used on Solaris) is
1407 .type SYM,#function
1408 The second (used on UnixWare) is
1409 .type SYM,@function
1410 The third (reportedly to be used on Irix 6.0) is
1411 .type SYM STT_FUNC
1412 The fourth (used on NetBSD/Arm and Linux/ARM) is
1413 .type SYM,%function
1414 The fifth (used on SVR4/860) is
1415 .type SYM,"function"
1418 static void
1419 obj_elf_type (ignore)
1420 int ignore ATTRIBUTE_UNUSED;
1422 char *name;
1423 char c;
1424 int type;
1425 const char *typename;
1426 symbolS *sym;
1428 name = input_line_pointer;
1429 c = get_symbol_end ();
1430 sym = symbol_find_or_make (name);
1431 *input_line_pointer = c;
1433 SKIP_WHITESPACE ();
1434 if (*input_line_pointer == ',')
1435 ++input_line_pointer;
1437 SKIP_WHITESPACE ();
1438 if ( *input_line_pointer == '#'
1439 || *input_line_pointer == '@'
1440 || *input_line_pointer == '"'
1441 || *input_line_pointer == '%')
1442 ++input_line_pointer;
1444 typename = input_line_pointer;
1445 c = get_symbol_end ();
1447 type = 0;
1448 if (strcmp (typename, "function") == 0
1449 || strcmp (typename, "STT_FUNC") == 0)
1450 type = BSF_FUNCTION;
1451 else if (strcmp (typename, "object") == 0
1452 || strcmp (typename, "STT_OBJECT") == 0)
1453 type = BSF_OBJECT;
1454 else
1455 as_bad (_("ignoring unrecognized symbol type \"%s\""), typename);
1457 *input_line_pointer = c;
1459 if (*input_line_pointer == '"')
1460 ++input_line_pointer;
1462 symbol_get_bfdsym (sym)->flags |= type;
1464 demand_empty_rest_of_line ();
1467 static void
1468 obj_elf_ident (ignore)
1469 int ignore ATTRIBUTE_UNUSED;
1471 static segT comment_section;
1472 segT old_section = now_seg;
1473 int old_subsection = now_subseg;
1475 #ifdef md_flush_pending_output
1476 md_flush_pending_output ();
1477 #endif
1479 if (!comment_section)
1481 char *p;
1482 comment_section = subseg_new (".comment", 0);
1483 bfd_set_section_flags (stdoutput, comment_section,
1484 SEC_READONLY | SEC_HAS_CONTENTS);
1485 p = frag_more (1);
1486 *p = 0;
1488 else
1489 subseg_set (comment_section, 0);
1490 stringer (1);
1491 subseg_set (old_section, old_subsection);
1494 #ifdef INIT_STAB_SECTION
1496 /* The first entry in a .stabs section is special. */
1498 void
1499 obj_elf_init_stab_section (seg)
1500 segT seg;
1502 char *file;
1503 char *p;
1504 char *stabstr_name;
1505 unsigned int stroff;
1507 /* Force the section to align to a longword boundary. Without this,
1508 UnixWare ar crashes. */
1509 bfd_set_section_alignment (stdoutput, seg, 2);
1511 /* Make space for this first symbol. */
1512 p = frag_more (12);
1513 /* Zero it out. */
1514 memset (p, 0, 12);
1515 as_where (&file, (unsigned int *) NULL);
1516 stabstr_name = (char *) alloca (strlen (segment_name (seg)) + 4);
1517 strcpy (stabstr_name, segment_name (seg));
1518 strcat (stabstr_name, "str");
1519 stroff = get_stab_string_offset (file, stabstr_name);
1520 know (stroff == 1);
1521 md_number_to_chars (p, stroff, 4);
1522 seg_info (seg)->stabu.p = p;
1525 #endif
1527 /* Fill in the counts in the first entry in a .stabs section. */
1529 static void
1530 adjust_stab_sections (abfd, sec, xxx)
1531 bfd *abfd;
1532 asection *sec;
1533 PTR xxx ATTRIBUTE_UNUSED;
1535 char *name;
1536 asection *strsec;
1537 char *p;
1538 int strsz, nsyms;
1540 if (strncmp (".stab", sec->name, 5))
1541 return;
1542 if (!strcmp ("str", sec->name + strlen (sec->name) - 3))
1543 return;
1545 name = (char *) alloca (strlen (sec->name) + 4);
1546 strcpy (name, sec->name);
1547 strcat (name, "str");
1548 strsec = bfd_get_section_by_name (abfd, name);
1549 if (strsec)
1550 strsz = bfd_section_size (abfd, strsec);
1551 else
1552 strsz = 0;
1553 nsyms = bfd_section_size (abfd, sec) / 12 - 1;
1555 p = seg_info (sec)->stabu.p;
1556 assert (p != 0);
1558 bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6);
1559 bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8);
1562 #ifdef NEED_ECOFF_DEBUG
1564 /* This function is called by the ECOFF code. It is supposed to
1565 record the external symbol information so that the backend can
1566 write it out correctly. The ELF backend doesn't actually handle
1567 this at the moment, so we do it ourselves. We save the information
1568 in the symbol. */
1570 void
1571 elf_ecoff_set_ext (sym, ext)
1572 symbolS *sym;
1573 struct ecoff_extr *ext;
1575 symbol_get_bfdsym (sym)->udata.p = (PTR) ext;
1578 /* This function is called by bfd_ecoff_debug_externals. It is
1579 supposed to *EXT to the external symbol information, and return
1580 whether the symbol should be used at all. */
1582 static boolean
1583 elf_get_extr (sym, ext)
1584 asymbol *sym;
1585 EXTR *ext;
1587 if (sym->udata.p == NULL)
1588 return false;
1589 *ext = *(EXTR *) sym->udata.p;
1590 return true;
1593 /* This function is called by bfd_ecoff_debug_externals. It has
1594 nothing to do for ELF. */
1596 /*ARGSUSED*/
1597 static void
1598 elf_set_index (sym, indx)
1599 asymbol *sym ATTRIBUTE_UNUSED;
1600 bfd_size_type indx ATTRIBUTE_UNUSED;
1604 #endif /* NEED_ECOFF_DEBUG */
1606 void
1607 elf_frob_symbol (symp, puntp)
1608 symbolS *symp;
1609 int *puntp;
1611 struct elf_obj_sy *sy_obj;
1613 #ifdef NEED_ECOFF_DEBUG
1614 if (ECOFF_DEBUGGING)
1615 ecoff_frob_symbol (symp);
1616 #endif
1618 sy_obj = symbol_get_obj (symp);
1620 if (sy_obj->size != NULL)
1622 switch (sy_obj->size->X_op)
1624 case O_subtract:
1625 S_SET_SIZE (symp,
1626 (S_GET_VALUE (sy_obj->size->X_add_symbol)
1627 + sy_obj->size->X_add_number
1628 - S_GET_VALUE (sy_obj->size->X_op_symbol)));
1629 break;
1630 case O_constant:
1631 S_SET_SIZE (symp,
1632 (S_GET_VALUE (sy_obj->size->X_add_symbol)
1633 + sy_obj->size->X_add_number));
1634 break;
1635 default:
1636 as_bad (_(".size expression too complicated to fix up"));
1637 break;
1639 free (sy_obj->size);
1640 sy_obj->size = NULL;
1643 if (sy_obj->versioned_name != NULL)
1645 /* This symbol was given a new name with the .symver directive.
1647 If this is an external reference, just rename the symbol to
1648 include the version string. This will make the relocs be
1649 against the correct versioned symbol.
1651 If this is a definition, add an alias. FIXME: Using an alias
1652 will permit the debugging information to refer to the right
1653 symbol. However, it's not clear whether it is the best
1654 approach. */
1656 if (! S_IS_DEFINED (symp))
1658 char *p;
1660 /* Verify that the name isn't using the @@ syntax--this is
1661 reserved for definitions of the default version to link
1662 against. */
1663 p = strchr (sy_obj->versioned_name, ELF_VER_CHR);
1664 know (p != NULL);
1665 if (p[1] == ELF_VER_CHR)
1667 as_bad (_("invalid attempt to declare external version name as default in symbol `%s'"),
1668 sy_obj->versioned_name);
1669 *puntp = true;
1671 S_SET_NAME (symp, sy_obj->versioned_name);
1673 else
1675 symbolS *symp2;
1677 /* FIXME: Creating a new symbol here is risky. We're in the
1678 final loop over the symbol table. We can get away with
1679 it only because the symbol goes to the end of the list,
1680 where the loop will still see it. It would probably be
1681 better to do this in obj_frob_file_before_adjust. */
1683 symp2 = symbol_find_or_make (sy_obj->versioned_name);
1685 /* Now we act as though we saw symp2 = sym. */
1687 S_SET_SEGMENT (symp2, S_GET_SEGMENT (symp));
1689 /* Subtracting out the frag address here is a hack because
1690 we are in the middle of the final loop. */
1691 S_SET_VALUE (symp2,
1692 (S_GET_VALUE (symp)
1693 - symbol_get_frag (symp)->fr_address));
1695 symbol_set_frag (symp2, symbol_get_frag (symp));
1697 /* This will copy over the size information. */
1698 copy_symbol_attributes (symp2, symp);
1700 if (S_IS_WEAK (symp))
1701 S_SET_WEAK (symp2);
1703 if (S_IS_EXTERNAL (symp))
1704 S_SET_EXTERNAL (symp2);
1708 /* Double check weak symbols. */
1709 if (S_IS_WEAK (symp))
1711 if (S_IS_COMMON (symp))
1712 as_bad (_("Symbol `%s' can not be both weak and common"),
1713 S_GET_NAME (symp));
1716 #ifdef TC_MIPS
1717 /* The Irix 5 and 6 assemblers set the type of any common symbol and
1718 any undefined non-function symbol to STT_OBJECT. We try to be
1719 compatible, since newer Irix 5 and 6 linkers care. However, we
1720 only set undefined symbols to be STT_OBJECT if we are on Irix,
1721 because that is the only time gcc will generate the necessary
1722 .global directives to mark functions. */
1724 if (S_IS_COMMON (symp))
1725 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1727 if (strstr (TARGET_OS, "irix") != NULL
1728 && ! S_IS_DEFINED (symp)
1729 && (symbol_get_bfdsym (symp)->flags & BSF_FUNCTION) == 0)
1730 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1731 #endif
1733 #if 0 /* TC_PPC */
1734 /* If TC_PPC is defined, we used to force the type of a symbol to be
1735 BSF_OBJECT if it was otherwise unset. This was required by some
1736 version of VxWorks. Thomas de Lellis <tdel@windriver.com> says
1737 that this is no longer needed, so it is now commented out. */
1738 if ((symbol_get_bfdsym (symp)->flags
1739 & (BSF_FUNCTION | BSF_FILE | BSF_SECTION_SYM)) == 0
1740 && S_IS_DEFINED (symp))
1741 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1742 #endif
1745 void
1746 elf_frob_file ()
1748 bfd_map_over_sections (stdoutput, adjust_stab_sections, (PTR) 0);
1750 #ifdef elf_tc_final_processing
1751 elf_tc_final_processing ();
1752 #endif
1755 /* It is required that we let write_relocs have the opportunity to
1756 optimize away fixups before output has begun, since it is possible
1757 to eliminate all fixups for a section and thus we never should
1758 have generated the relocation section. */
1760 void
1761 elf_frob_file_after_relocs ()
1763 #ifdef NEED_ECOFF_DEBUG
1764 if (ECOFF_DEBUGGING)
1765 /* Generate the ECOFF debugging information. */
1767 const struct ecoff_debug_swap *debug_swap;
1768 struct ecoff_debug_info debug;
1769 char *buf;
1770 asection *sec;
1772 debug_swap
1773 = get_elf_backend_data (stdoutput)->elf_backend_ecoff_debug_swap;
1774 know (debug_swap != (const struct ecoff_debug_swap *) NULL);
1775 ecoff_build_debug (&debug.symbolic_header, &buf, debug_swap);
1777 /* Set up the pointers in debug. */
1778 #define SET(ptr, offset, type) \
1779 debug.ptr = (type) (buf + debug.symbolic_header.offset)
1781 SET (line, cbLineOffset, unsigned char *);
1782 SET (external_dnr, cbDnOffset, PTR);
1783 SET (external_pdr, cbPdOffset, PTR);
1784 SET (external_sym, cbSymOffset, PTR);
1785 SET (external_opt, cbOptOffset, PTR);
1786 SET (external_aux, cbAuxOffset, union aux_ext *);
1787 SET (ss, cbSsOffset, char *);
1788 SET (external_fdr, cbFdOffset, PTR);
1789 SET (external_rfd, cbRfdOffset, PTR);
1790 /* ssext and external_ext are set up just below. */
1792 #undef SET
1794 /* Set up the external symbols. */
1795 debug.ssext = debug.ssext_end = NULL;
1796 debug.external_ext = debug.external_ext_end = NULL;
1797 if (! bfd_ecoff_debug_externals (stdoutput, &debug, debug_swap, true,
1798 elf_get_extr, elf_set_index))
1799 as_fatal (_("Failed to set up debugging information: %s"),
1800 bfd_errmsg (bfd_get_error ()));
1802 sec = bfd_get_section_by_name (stdoutput, ".mdebug");
1803 assert (sec != NULL);
1805 know (stdoutput->output_has_begun == false);
1807 /* We set the size of the section, call bfd_set_section_contents
1808 to force the ELF backend to allocate a file position, and then
1809 write out the data. FIXME: Is this really the best way to do
1810 this? */
1811 sec->_raw_size = bfd_ecoff_debug_size (stdoutput, &debug, debug_swap);
1813 /* Pass BUF to bfd_set_section_contents because this will
1814 eventually become a call to fwrite, and ISO C prohibits
1815 passing a NULL pointer to a stdio function even if the
1816 pointer will not be used. */
1817 if (! bfd_set_section_contents (stdoutput, sec, (PTR) buf,
1818 (file_ptr) 0, (bfd_size_type) 0))
1819 as_fatal (_("Can't start writing .mdebug section: %s"),
1820 bfd_errmsg (bfd_get_error ()));
1822 know (stdoutput->output_has_begun == true);
1823 know (sec->filepos != 0);
1825 if (! bfd_ecoff_write_debug (stdoutput, &debug, debug_swap,
1826 sec->filepos))
1827 as_fatal (_("Could not write .mdebug section: %s"),
1828 bfd_errmsg (bfd_get_error ()));
1830 #endif /* NEED_ECOFF_DEBUG */
1833 #ifdef SCO_ELF
1835 /* Heavily plagarized from obj_elf_version. The idea is to emit the
1836 SCO specific identifier in the .notes section to satisfy the SCO
1837 linker.
1839 This looks more complicated than it really is. As opposed to the
1840 "obvious" solution, this should handle the cross dev cases
1841 correctly. (i.e, hosting on a 64 bit big endian processor, but
1842 generating SCO Elf code) Efficiency isn't a concern, as there
1843 should be exactly one of these sections per object module.
1845 SCO OpenServer 5 identifies it's ELF modules with a standard ELF
1846 .note section.
1848 int_32 namesz = 4 ; Name size
1849 int_32 descsz = 12 ; Descriptive information
1850 int_32 type = 1 ;
1851 char name[4] = "SCO" ; Originator name ALWAYS SCO + NULL
1852 int_32 version = (major ver # << 16) | version of tools ;
1853 int_32 source = (tool_id << 16 ) | 1 ;
1854 int_32 info = 0 ; These are set by the SCO tools, but we
1855 don't know enough about the source
1856 environment to set them. SCO ld currently
1857 ignores them, and recommends we set them
1858 to zero. */
1860 #define SCO_MAJOR_VERSION 0x1
1861 #define SCO_MINOR_VERSION 0x1
1863 void
1864 sco_id ()
1867 char *name;
1868 unsigned int c;
1869 char ch;
1870 char *p;
1871 asection *seg = now_seg;
1872 subsegT subseg = now_subseg;
1873 Elf_Internal_Note i_note;
1874 Elf_External_Note e_note;
1875 asection *note_secp = (asection *) NULL;
1876 int i, len;
1878 /* create the .note section */
1880 note_secp = subseg_new (".note", 0);
1881 bfd_set_section_flags (stdoutput,
1882 note_secp,
1883 SEC_HAS_CONTENTS | SEC_READONLY);
1885 /* process the version string */
1887 i_note.namesz = 4;
1888 i_note.descsz = 12; /* 12 descriptive bytes */
1889 i_note.type = NT_VERSION; /* Contains a version string */
1891 p = frag_more (sizeof (i_note.namesz));
1892 md_number_to_chars (p, (valueT) i_note.namesz, 4);
1894 p = frag_more (sizeof (i_note.descsz));
1895 md_number_to_chars (p, (valueT) i_note.descsz, 4);
1897 p = frag_more (sizeof (i_note.type));
1898 md_number_to_chars (p, (valueT) i_note.type, 4);
1900 p = frag_more (4);
1901 strcpy (p, "SCO");
1903 /* Note: this is the version number of the ELF we're representing */
1904 p = frag_more (4);
1905 md_number_to_chars (p, (SCO_MAJOR_VERSION << 16) | (SCO_MINOR_VERSION), 4);
1907 /* Here, we pick a magic number for ourselves (yes, I "registered"
1908 it with SCO. The bottom bit shows that we are compat with the
1909 SCO ABI. */
1910 p = frag_more (4);
1911 md_number_to_chars (p, 0x4c520000 | 0x0001, 4);
1913 /* If we knew (or cared) what the source language options were, we'd
1914 fill them in here. SCO has given us permission to ignore these
1915 and just set them to zero. */
1916 p = frag_more (4);
1917 md_number_to_chars (p, 0x0000, 4);
1919 frag_align (2, 0, 0);
1921 /* We probably can't restore the current segment, for there likely
1922 isn't one yet... */
1923 if (seg && subseg)
1924 subseg_set (seg, subseg);
1928 #endif /* SCO_ELF */
1930 static int
1931 elf_separate_stab_sections ()
1933 #ifdef NEED_ECOFF_DEBUG
1934 return (!ECOFF_DEBUGGING);
1935 #else
1936 return 1;
1937 #endif
1940 static void
1941 elf_init_stab_section (seg)
1942 segT seg;
1944 #ifdef NEED_ECOFF_DEBUG
1945 if (!ECOFF_DEBUGGING)
1946 #endif
1947 obj_elf_init_stab_section (seg);
1950 const struct format_ops elf_format_ops =
1952 bfd_target_elf_flavour,
1953 0, /* dfl_leading_underscore */
1954 1, /* emit_section_symbols */
1955 elf_begin,
1956 elf_file_symbol,
1957 elf_frob_symbol,
1958 elf_frob_file,
1959 elf_frob_file_after_relocs,
1960 elf_s_get_size, elf_s_set_size,
1961 elf_s_get_align, elf_s_set_align,
1962 elf_s_get_other,
1963 elf_s_set_other,
1964 0, /* s_get_desc */
1965 0, /* s_set_desc */
1966 0, /* s_get_type */
1967 0, /* s_set_type */
1968 elf_copy_symbol_attributes,
1969 #ifdef NEED_ECOFF_DEBUG
1970 ecoff_generate_asm_lineno,
1971 ecoff_stab,
1972 #else
1973 0, /* generate_asm_lineno */
1974 0, /* process_stab */
1975 #endif
1976 elf_separate_stab_sections,
1977 elf_init_stab_section,
1978 elf_sec_sym_ok_for_reloc,
1979 elf_pop_insert,
1980 #ifdef NEED_ECOFF_DEBUG
1981 elf_ecoff_set_ext,
1982 #else
1983 0, /* ecoff_set_ext */
1984 #endif
1985 elf_obj_read_begin_hook,
1986 elf_obj_symbol_new_hook