* elf64-ppc.c (ppc64_elf_edit_opd): When deleting relocs, adjust
[binutils.git] / gas / config / obj-elf.c
blob259785d2de382d0e3482f4324759a5b3aa579bc3
1 /* ELF object file format
2 Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002 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 "safe-ctype.h"
25 #include "subsegs.h"
26 #include "obstack.h"
27 #include "struc-symbol.h"
29 #ifndef ECOFF_DEBUGGING
30 #define ECOFF_DEBUGGING 0
31 #else
32 #define NEED_ECOFF_DEBUG
33 #endif
35 #ifdef NEED_ECOFF_DEBUG
36 #include "ecoff.h"
37 #endif
39 #ifdef TC_ALPHA
40 #include "elf/alpha.h"
41 #endif
43 #ifdef TC_MIPS
44 #include "elf/mips.h"
45 #endif
47 #ifdef TC_PPC
48 #include "elf/ppc.h"
49 #endif
51 #ifdef TC_I370
52 #include "elf/i370.h"
53 #endif
55 static bfd_vma elf_s_get_size PARAMS ((symbolS *));
56 static void elf_s_set_size PARAMS ((symbolS *, bfd_vma));
57 static bfd_vma elf_s_get_align PARAMS ((symbolS *));
58 static void elf_s_set_align PARAMS ((symbolS *, bfd_vma));
59 static void elf_s_set_other PARAMS ((symbolS *, int));
60 static int elf_sec_sym_ok_for_reloc PARAMS ((asection *));
61 static void adjust_stab_sections PARAMS ((bfd *, asection *, PTR));
62 static void build_group_lists PARAMS ((bfd *, asection *, PTR));
63 static int elf_separate_stab_sections PARAMS ((void));
64 static void elf_init_stab_section PARAMS ((segT));
65 static symbolS *elf_common PARAMS ((int));
67 #ifdef NEED_ECOFF_DEBUG
68 static boolean elf_get_extr PARAMS ((asymbol *, EXTR *));
69 static void elf_set_index PARAMS ((asymbol *, bfd_size_type));
70 #endif
72 static void obj_elf_line PARAMS ((int));
73 void obj_elf_version PARAMS ((int));
74 static void obj_elf_size PARAMS ((int));
75 static void obj_elf_type PARAMS ((int));
76 static void obj_elf_ident PARAMS ((int));
77 static void obj_elf_weak PARAMS ((int));
78 static void obj_elf_local PARAMS ((int));
79 static void obj_elf_visibility PARAMS ((int));
80 static int obj_elf_parse_section_letters PARAMS ((char *, size_t));
81 static int obj_elf_section_word PARAMS ((char *, size_t));
82 static char *obj_elf_section_name PARAMS ((void));
83 static int obj_elf_section_type PARAMS ((char *, size_t));
84 static void obj_elf_symver PARAMS ((int));
85 static void obj_elf_subsection PARAMS ((int));
86 static void obj_elf_popsection PARAMS ((int));
87 static void obj_elf_tls_common PARAMS ((int));
89 static const pseudo_typeS elf_pseudo_table[] =
91 {"comm", obj_elf_common, 0},
92 {"common", obj_elf_common, 1},
93 {"ident", obj_elf_ident, 0},
94 {"local", obj_elf_local, 0},
95 {"previous", obj_elf_previous, 0},
96 {"section", obj_elf_section, 0},
97 {"section.s", obj_elf_section, 0},
98 {"sect", obj_elf_section, 0},
99 {"sect.s", obj_elf_section, 0},
100 {"pushsection", obj_elf_section, 1},
101 {"popsection", obj_elf_popsection, 0},
102 {"size", obj_elf_size, 0},
103 {"type", obj_elf_type, 0},
104 {"version", obj_elf_version, 0},
105 {"weak", obj_elf_weak, 0},
107 /* These define symbol visibility. */
108 {"internal", obj_elf_visibility, STV_INTERNAL},
109 {"hidden", obj_elf_visibility, STV_HIDDEN},
110 {"protected", obj_elf_visibility, STV_PROTECTED},
112 /* These are used for stabs-in-elf configurations. */
113 {"line", obj_elf_line, 0},
115 /* This is a GNU extension to handle symbol versions. */
116 {"symver", obj_elf_symver, 0},
118 /* A GNU extension to change subsection only. */
119 {"subsection", obj_elf_subsection, 0},
121 /* These are GNU extensions to aid in garbage collecting C++ vtables. */
122 {"vtable_inherit", (void (*) PARAMS ((int))) &obj_elf_vtable_inherit, 0},
123 {"vtable_entry", (void (*) PARAMS ((int))) &obj_elf_vtable_entry, 0},
125 /* These are used for dwarf. */
126 {"2byte", cons, 2},
127 {"4byte", cons, 4},
128 {"8byte", cons, 8},
130 /* We need to trap the section changing calls to handle .previous. */
131 {"data", obj_elf_data, 0},
132 {"text", obj_elf_text, 0},
134 {"tls_common", obj_elf_tls_common, 0},
136 /* End sentinel. */
137 {NULL, NULL, 0},
140 static const pseudo_typeS ecoff_debug_pseudo_table[] =
142 #ifdef NEED_ECOFF_DEBUG
143 /* COFF style debugging information for ECOFF. .ln is not used; .loc
144 is used instead. */
145 { "def", ecoff_directive_def, 0 },
146 { "dim", ecoff_directive_dim, 0 },
147 { "endef", ecoff_directive_endef, 0 },
148 { "file", ecoff_directive_file, 0 },
149 { "scl", ecoff_directive_scl, 0 },
150 { "tag", ecoff_directive_tag, 0 },
151 { "val", ecoff_directive_val, 0 },
153 /* COFF debugging requires pseudo-ops .size and .type, but ELF
154 already has meanings for those. We use .esize and .etype
155 instead. These are only generated by gcc anyhow. */
156 { "esize", ecoff_directive_size, 0 },
157 { "etype", ecoff_directive_type, 0 },
159 /* ECOFF specific debugging information. */
160 { "begin", ecoff_directive_begin, 0 },
161 { "bend", ecoff_directive_bend, 0 },
162 { "end", ecoff_directive_end, 0 },
163 { "ent", ecoff_directive_ent, 0 },
164 { "fmask", ecoff_directive_fmask, 0 },
165 { "frame", ecoff_directive_frame, 0 },
166 { "loc", ecoff_directive_loc, 0 },
167 { "mask", ecoff_directive_mask, 0 },
169 /* Other ECOFF directives. */
170 { "extern", ecoff_directive_extern, 0 },
172 /* These are used on Irix. I don't know how to implement them. */
173 { "alias", s_ignore, 0 },
174 { "bgnb", s_ignore, 0 },
175 { "endb", s_ignore, 0 },
176 { "lab", s_ignore, 0 },
177 { "noalias", s_ignore, 0 },
178 { "verstamp", s_ignore, 0 },
179 { "vreg", s_ignore, 0 },
180 #endif
182 {NULL, NULL, 0} /* end sentinel */
185 #undef NO_RELOC
186 #include "aout/aout64.h"
188 /* This is called when the assembler starts. */
190 void
191 elf_begin ()
193 /* Add symbols for the known sections to the symbol table. */
194 symbol_table_insert (section_symbol (bfd_get_section_by_name (stdoutput,
195 TEXT_SECTION_NAME)));
196 symbol_table_insert (section_symbol (bfd_get_section_by_name (stdoutput,
197 DATA_SECTION_NAME)));
198 symbol_table_insert (section_symbol (bfd_get_section_by_name (stdoutput,
199 BSS_SECTION_NAME)));
202 void
203 elf_pop_insert ()
205 pop_insert (elf_pseudo_table);
206 if (ECOFF_DEBUGGING)
207 pop_insert (ecoff_debug_pseudo_table);
210 static bfd_vma
211 elf_s_get_size (sym)
212 symbolS *sym;
214 return S_GET_SIZE (sym);
217 static void
218 elf_s_set_size (sym, sz)
219 symbolS *sym;
220 bfd_vma sz;
222 S_SET_SIZE (sym, sz);
225 static bfd_vma
226 elf_s_get_align (sym)
227 symbolS *sym;
229 return S_GET_ALIGN (sym);
232 static void
233 elf_s_set_align (sym, align)
234 symbolS *sym;
235 bfd_vma align;
237 S_SET_ALIGN (sym, align);
241 elf_s_get_other (sym)
242 symbolS *sym;
244 return elf_symbol (symbol_get_bfdsym (sym))->internal_elf_sym.st_other;
247 static void
248 elf_s_set_other (sym, other)
249 symbolS *sym;
250 int other;
252 S_SET_OTHER (sym, other);
255 static int
256 elf_sec_sym_ok_for_reloc (sec)
257 asection *sec;
259 return obj_sec_sym_ok_for_reloc (sec);
262 void
263 elf_file_symbol (s)
264 const char *s;
266 symbolS *sym;
268 sym = symbol_new (s, absolute_section, (valueT) 0, (struct frag *) 0);
269 symbol_set_frag (sym, &zero_address_frag);
270 symbol_get_bfdsym (sym)->flags |= BSF_FILE;
272 if (symbol_rootP != sym)
274 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
275 symbol_insert (sym, symbol_rootP, &symbol_rootP, &symbol_lastP);
276 #ifdef DEBUG
277 verify_symbol_chain (symbol_rootP, symbol_lastP);
278 #endif
281 #ifdef NEED_ECOFF_DEBUG
282 ecoff_new_file (s);
283 #endif
286 static symbolS *
287 elf_common (is_common)
288 int is_common;
290 char *name;
291 char c;
292 char *p;
293 int temp, size;
294 symbolS *symbolP;
295 int have_align;
297 if (flag_mri && is_common)
299 s_mri_common (0);
300 return NULL;
303 name = input_line_pointer;
304 c = get_symbol_end ();
305 /* just after name is now '\0' */
306 p = input_line_pointer;
307 *p = c;
308 SKIP_WHITESPACE ();
309 if (*input_line_pointer != ',')
311 as_bad (_("expected comma after symbol-name"));
312 ignore_rest_of_line ();
313 return NULL;
315 input_line_pointer++; /* skip ',' */
316 if ((temp = get_absolute_expression ()) < 0)
318 as_bad (_(".COMMon length (%d.) <0! Ignored."), temp);
319 ignore_rest_of_line ();
320 return NULL;
322 size = temp;
323 *p = 0;
324 symbolP = symbol_find_or_make (name);
325 *p = c;
326 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
328 as_bad (_("symbol `%s' is already defined"), S_GET_NAME (symbolP));
329 ignore_rest_of_line ();
330 return NULL;
332 if (S_GET_VALUE (symbolP) != 0)
334 if (S_GET_VALUE (symbolP) != (valueT) size)
336 as_warn (_("length of .comm \"%s\" is already %ld; not changed to %d"),
337 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
340 know (symbolP->sy_frag == &zero_address_frag);
341 if (*input_line_pointer != ',')
342 have_align = 0;
343 else
345 have_align = 1;
346 input_line_pointer++;
347 SKIP_WHITESPACE ();
349 if (! have_align || *input_line_pointer != '"')
351 if (! have_align)
352 temp = 0;
353 else
355 temp = get_absolute_expression ();
356 if (temp < 0)
358 temp = 0;
359 as_warn (_("common alignment negative; 0 assumed"));
362 if (symbol_get_obj (symbolP)->local)
364 segT old_sec;
365 int old_subsec;
366 char *pfrag;
367 int align;
369 /* allocate_bss: */
370 old_sec = now_seg;
371 old_subsec = now_subseg;
372 if (temp)
374 /* convert to a power of 2 alignment */
375 for (align = 0; (temp & 1) == 0; temp >>= 1, ++align);
376 if (temp != 1)
378 as_bad (_("common alignment not a power of 2"));
379 ignore_rest_of_line ();
380 return NULL;
383 else
384 align = 0;
385 record_alignment (bss_section, align);
386 subseg_set (bss_section, 0);
387 if (align)
388 frag_align (align, 0, 0);
389 if (S_GET_SEGMENT (symbolP) == bss_section)
390 symbol_get_frag (symbolP)->fr_symbol = 0;
391 symbol_set_frag (symbolP, frag_now);
392 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
393 (offsetT) size, (char *) 0);
394 *pfrag = 0;
395 S_SET_SIZE (symbolP, size);
396 S_SET_SEGMENT (symbolP, bss_section);
397 S_CLEAR_EXTERNAL (symbolP);
398 subseg_set (old_sec, old_subsec);
400 else
402 allocate_common:
403 S_SET_VALUE (symbolP, (valueT) size);
404 S_SET_ALIGN (symbolP, temp);
405 S_SET_EXTERNAL (symbolP);
406 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
409 else
411 input_line_pointer++;
412 /* @@ Some use the dot, some don't. Can we get some consistency?? */
413 if (*input_line_pointer == '.')
414 input_line_pointer++;
415 /* @@ Some say data, some say bss. */
416 if (strncmp (input_line_pointer, "bss\"", 4)
417 && strncmp (input_line_pointer, "data\"", 5))
419 while (*--input_line_pointer != '"')
421 input_line_pointer--;
422 goto bad_common_segment;
424 while (*input_line_pointer++ != '"')
426 goto allocate_common;
429 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
431 demand_empty_rest_of_line ();
432 return symbolP;
435 bad_common_segment:
436 p = input_line_pointer;
437 while (*p && *p != '\n')
438 p++;
439 c = *p;
440 *p = '\0';
441 as_bad (_("bad .common segment %s"), input_line_pointer + 1);
442 *p = c;
443 input_line_pointer = p;
444 ignore_rest_of_line ();
445 return NULL;
449 void
450 obj_elf_common (is_common)
451 int is_common;
453 elf_common (is_common);
456 static void
457 obj_elf_tls_common (ignore)
458 int ignore ATTRIBUTE_UNUSED;
460 symbolS *symbolP = elf_common (0);
462 if (symbolP)
463 symbol_get_bfdsym (symbolP)->flags |= BSF_THREAD_LOCAL;
466 static void
467 obj_elf_local (ignore)
468 int ignore ATTRIBUTE_UNUSED;
470 char *name;
471 int c;
472 symbolS *symbolP;
476 name = input_line_pointer;
477 c = get_symbol_end ();
478 symbolP = symbol_find_or_make (name);
479 *input_line_pointer = c;
480 SKIP_WHITESPACE ();
481 S_CLEAR_EXTERNAL (symbolP);
482 symbol_get_obj (symbolP)->local = 1;
483 if (c == ',')
485 input_line_pointer++;
486 SKIP_WHITESPACE ();
487 if (*input_line_pointer == '\n')
488 c = '\n';
491 while (c == ',');
492 demand_empty_rest_of_line ();
495 static void
496 obj_elf_weak (ignore)
497 int ignore ATTRIBUTE_UNUSED;
499 char *name;
500 int c;
501 symbolS *symbolP;
505 name = input_line_pointer;
506 c = get_symbol_end ();
507 symbolP = symbol_find_or_make (name);
508 *input_line_pointer = c;
509 SKIP_WHITESPACE ();
510 S_SET_WEAK (symbolP);
511 symbol_get_obj (symbolP)->local = 1;
512 if (c == ',')
514 input_line_pointer++;
515 SKIP_WHITESPACE ();
516 if (*input_line_pointer == '\n')
517 c = '\n';
520 while (c == ',');
521 demand_empty_rest_of_line ();
524 static void
525 obj_elf_visibility (visibility)
526 int visibility;
528 char *name;
529 int c;
530 symbolS *symbolP;
531 asymbol *bfdsym;
532 elf_symbol_type *elfsym;
536 name = input_line_pointer;
537 c = get_symbol_end ();
538 symbolP = symbol_find_or_make (name);
539 *input_line_pointer = c;
541 SKIP_WHITESPACE ();
543 bfdsym = symbol_get_bfdsym (symbolP);
544 elfsym = elf_symbol_from (bfd_asymbol_bfd (bfdsym), bfdsym);
546 assert (elfsym);
548 elfsym->internal_elf_sym.st_other = visibility;
550 if (c == ',')
552 input_line_pointer ++;
554 SKIP_WHITESPACE ();
556 if (*input_line_pointer == '\n')
557 c = '\n';
560 while (c == ',');
562 demand_empty_rest_of_line ();
565 static segT previous_section;
566 static int previous_subsection;
568 struct section_stack
570 struct section_stack *next;
571 segT seg, prev_seg;
572 int subseg, prev_subseg;
575 static struct section_stack *section_stack;
577 /* Handle the .section pseudo-op. This code supports two different
578 syntaxes.
580 The first is found on Solaris, and looks like
581 .section ".sec1",#alloc,#execinstr,#write
582 Here the names after '#' are the SHF_* flags to turn on for the
583 section. I'm not sure how it determines the SHT_* type (BFD
584 doesn't really give us control over the type, anyhow).
586 The second format is found on UnixWare, and probably most SVR4
587 machines, and looks like
588 .section .sec1,"a",@progbits
589 The quoted string may contain any combination of a, w, x, and
590 represents the SHF_* flags to turn on for the section. The string
591 beginning with '@' can be progbits or nobits. There should be
592 other possibilities, but I don't know what they are. In any case,
593 BFD doesn't really let us set the section type. */
595 /* Certain named sections have particular defined types, listed on p.
596 4-19 of the ABI. */
597 struct special_section
599 const char *name;
600 int type;
601 int attributes;
604 static struct special_section const special_sections[] =
606 { ".bss", SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
607 { ".comment", SHT_PROGBITS, 0 },
608 { ".data", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
609 { ".data1", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
610 { ".debug", SHT_PROGBITS, 0 },
611 { ".fini", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
612 { ".init", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
613 { ".line", SHT_PROGBITS, 0 },
614 { ".note", SHT_NOTE, 0 },
615 { ".rodata", SHT_PROGBITS, SHF_ALLOC },
616 { ".rodata1", SHT_PROGBITS, SHF_ALLOC },
617 { ".tbss", SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
618 { ".tdata", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
619 { ".text", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
620 { ".init_array",SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
621 { ".fini_array",SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
622 { ".preinit_array",SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
624 #ifdef ELF_TC_SPECIAL_SECTIONS
625 ELF_TC_SPECIAL_SECTIONS
626 #endif
628 #if 0
629 /* The following section names are special, but they can not
630 reasonably appear in assembler code. Some of the attributes are
631 processor dependent. */
632 { ".dynamic", SHT_DYNAMIC, SHF_ALLOC /* + SHF_WRITE */ },
633 { ".dynstr", SHT_STRTAB, SHF_ALLOC },
634 { ".dynsym", SHT_DYNSYM, SHF_ALLOC },
635 { ".got", SHT_PROGBITS, 0 },
636 { ".hash", SHT_HASH, SHF_ALLOC },
637 { ".interp", SHT_PROGBITS, /* SHF_ALLOC */ },
638 { ".plt", SHT_PROGBITS, 0 },
639 { ".shstrtab",SHT_STRTAB, 0 },
640 { ".strtab", SHT_STRTAB, /* SHF_ALLOC */ },
641 { ".symtab", SHT_SYMTAB, /* SHF_ALLOC */ },
642 #endif
644 { NULL, 0, 0 }
647 void
648 obj_elf_change_section (name, type, attr, entsize, group_name, linkonce, push)
649 const char *name;
650 int type;
651 int attr;
652 int entsize;
653 const char *group_name;
654 int linkonce;
655 int push;
657 asection *old_sec;
658 segT sec;
659 flagword flags;
660 int i;
662 #ifdef md_flush_pending_output
663 md_flush_pending_output ();
664 #endif
666 /* Switch to the section, creating it if necessary. */
667 if (push)
669 struct section_stack *elt;
670 elt = xmalloc (sizeof (struct section_stack));
671 elt->next = section_stack;
672 elt->seg = now_seg;
673 elt->prev_seg = previous_section;
674 elt->subseg = now_subseg;
675 elt->prev_subseg = previous_subsection;
676 section_stack = elt;
678 previous_section = now_seg;
679 previous_subsection = now_subseg;
681 old_sec = bfd_get_section_by_name (stdoutput, name);
682 sec = subseg_new (name, 0);
684 /* See if this is one of the special sections. */
685 for (i = 0; special_sections[i].name != NULL; i++)
686 if (strcmp (name, special_sections[i].name) == 0)
688 if (type == SHT_NULL)
689 type = special_sections[i].type;
690 else if (type != special_sections[i].type)
692 if (old_sec == NULL
693 /* FIXME: gcc, as of 2002-10-22, will emit
695 .section .init_array,"aw",@progbits
697 for __attribute__ ((section (".init_array"))).
698 "@progbits" is incorrect. */
699 && special_sections[i].type != SHT_INIT_ARRAY
700 && special_sections[i].type != SHT_FINI_ARRAY
701 && special_sections[i].type != SHT_PREINIT_ARRAY)
703 as_warn (_("setting incorrect section type for %s"), name);
705 else
707 as_warn (_("ignoring incorrect section type for %s"), name);
708 type = special_sections[i].type;
711 if ((attr &~ special_sections[i].attributes) != 0
712 && old_sec == NULL)
714 /* As a GNU extension, we permit a .note section to be
715 allocatable. If the linker sees an allocateable .note
716 section, it will create a PT_NOTE segment in the output
717 file. */
718 if (strcmp (name, ".note") != 0
719 || attr != SHF_ALLOC)
720 as_warn (_("setting incorrect section attributes for %s"),
721 name);
723 attr |= special_sections[i].attributes;
724 break;
727 /* Convert ELF type and flags to BFD flags. */
728 flags = (SEC_RELOC
729 | ((attr & SHF_WRITE) ? 0 : SEC_READONLY)
730 | ((attr & SHF_ALLOC) ? SEC_ALLOC : 0)
731 | (((attr & SHF_ALLOC) && type != SHT_NOBITS) ? SEC_LOAD : 0)
732 | ((attr & SHF_EXECINSTR) ? SEC_CODE : 0)
733 | ((attr & SHF_MERGE) ? SEC_MERGE : 0)
734 | ((attr & SHF_STRINGS) ? SEC_STRINGS : 0)
735 | ((attr & SHF_TLS) ? SEC_THREAD_LOCAL : 0));
736 #ifdef md_elf_section_flags
737 flags = md_elf_section_flags (flags, attr, type);
738 #endif
740 if (old_sec == NULL)
742 symbolS *secsym;
744 /* Prevent SEC_HAS_CONTENTS from being inadvertently set. */
745 if (type == SHT_NOBITS)
746 seg_info (sec)->bss = 1;
748 bfd_set_section_flags (stdoutput, sec, flags);
749 if (flags & SEC_MERGE)
750 sec->entsize = entsize;
751 elf_group_name (sec) = group_name;
752 elf_linkonce_p (sec) = linkonce;
754 /* Add a symbol for this section to the symbol table. */
755 secsym = symbol_find (name);
756 if (secsym != NULL)
757 symbol_set_bfdsym (secsym, sec->symbol);
758 else
759 symbol_table_insert (section_symbol (sec));
761 else if (attr != 0)
763 /* If section attributes are specified the second time we see a
764 particular section, then check that they are the same as we
765 saw the first time. */
766 if (((old_sec->flags ^ flags)
767 & (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
768 | SEC_EXCLUDE | SEC_SORT_ENTRIES | SEC_MERGE | SEC_STRINGS
769 | SEC_THREAD_LOCAL))
770 || linkonce != elf_linkonce_p (sec))
771 as_warn (_("ignoring changed section attributes for %s"), name);
772 if ((flags & SEC_MERGE) && old_sec->entsize != (unsigned) entsize)
773 as_warn (_("ignoring changed section entity size for %s"), name);
774 if ((attr & SHF_GROUP) != 0
775 && strcmp (elf_group_name (old_sec), group_name) != 0)
776 as_warn (_("ignoring new section group for %s"), name);
779 #ifdef md_elf_section_change_hook
780 md_elf_section_change_hook ();
781 #endif
784 static int
785 obj_elf_parse_section_letters (str, len)
786 char *str;
787 size_t len;
789 int attr = 0;
791 while (len > 0)
793 switch (*str)
795 case 'a':
796 attr |= SHF_ALLOC;
797 break;
798 case 'w':
799 attr |= SHF_WRITE;
800 break;
801 case 'x':
802 attr |= SHF_EXECINSTR;
803 break;
804 case 'M':
805 attr |= SHF_MERGE;
806 break;
807 case 'S':
808 attr |= SHF_STRINGS;
809 break;
810 case 'G':
811 attr |= SHF_GROUP;
812 break;
813 case 'T':
814 attr |= SHF_TLS;
815 break;
816 /* Compatibility. */
817 case 'm':
818 if (*(str - 1) == 'a')
820 attr |= SHF_MERGE;
821 if (len > 1 && str[1] == 's')
823 attr |= SHF_STRINGS;
824 str++, len--;
826 break;
828 default:
830 char *bad_msg = _("unrecognized .section attribute: want a,w,x,M,S,G,T");
831 #ifdef md_elf_section_letter
832 int md_attr = md_elf_section_letter (*str, &bad_msg);
833 if (md_attr >= 0)
834 attr |= md_attr;
835 else
836 #endif
838 as_warn ("%s", bad_msg);
839 attr = -1;
842 break;
844 str++, len--;
847 return attr;
850 static int
851 obj_elf_section_word (str, len)
852 char *str;
853 size_t len;
855 if (len == 5 && strncmp (str, "write", 5) == 0)
856 return SHF_WRITE;
857 if (len == 5 && strncmp (str, "alloc", 5) == 0)
858 return SHF_ALLOC;
859 if (len == 9 && strncmp (str, "execinstr", 9) == 0)
860 return SHF_EXECINSTR;
862 #ifdef md_elf_section_word
864 int md_attr = md_elf_section_word (str, len);
865 if (md_attr >= 0)
866 return md_attr;
868 #endif
870 as_warn (_("unrecognized section attribute"));
871 return 0;
874 static int
875 obj_elf_section_type (str, len)
876 char *str;
877 size_t len;
879 if (len == 8 && strncmp (str, "progbits", 8) == 0)
880 return SHT_PROGBITS;
881 if (len == 6 && strncmp (str, "nobits", 6) == 0)
882 return SHT_NOBITS;
884 #ifdef md_elf_section_type
886 int md_type = md_elf_section_type (str, len);
887 if (md_type >= 0)
888 return md_type;
890 #endif
892 as_warn (_("unrecognized section type"));
893 return 0;
896 /* Get name of section. */
897 static char *
898 obj_elf_section_name ()
900 char *name;
902 SKIP_WHITESPACE ();
903 if (*input_line_pointer == '"')
905 int dummy;
907 name = demand_copy_C_string (&dummy);
908 if (name == NULL)
910 ignore_rest_of_line ();
911 return NULL;
914 else
916 char *end = input_line_pointer;
918 while (0 == strchr ("\n\t,; ", *end))
919 end++;
920 if (end == input_line_pointer)
922 as_warn (_("missing name"));
923 ignore_rest_of_line ();
924 return NULL;
927 name = xmalloc (end - input_line_pointer + 1);
928 memcpy (name, input_line_pointer, end - input_line_pointer);
929 name[end - input_line_pointer] = '\0';
930 input_line_pointer = end;
932 SKIP_WHITESPACE ();
933 return name;
936 void
937 obj_elf_section (push)
938 int push;
940 char *name, *group_name, *beg;
941 int type, attr, dummy;
942 int entsize;
943 int linkonce;
945 #ifndef TC_I370
946 if (flag_mri)
948 char mri_type;
950 #ifdef md_flush_pending_output
951 md_flush_pending_output ();
952 #endif
954 previous_section = now_seg;
955 previous_subsection = now_subseg;
957 s_mri_sect (&mri_type);
959 #ifdef md_elf_section_change_hook
960 md_elf_section_change_hook ();
961 #endif
963 return;
965 #endif /* ! defined (TC_I370) */
967 name = obj_elf_section_name ();
968 if (name == NULL)
969 return;
970 type = SHT_NULL;
971 attr = 0;
972 group_name = NULL;
973 entsize = 0;
974 linkonce = 0;
976 if (*input_line_pointer == ',')
978 /* Skip the comma. */
979 ++input_line_pointer;
980 SKIP_WHITESPACE ();
982 if (*input_line_pointer == '"')
984 beg = demand_copy_C_string (&dummy);
985 if (beg == NULL)
987 ignore_rest_of_line ();
988 return;
990 attr |= obj_elf_parse_section_letters (beg, strlen (beg));
992 SKIP_WHITESPACE ();
993 if (*input_line_pointer == ',')
995 char c;
996 char *save = input_line_pointer;
998 ++input_line_pointer;
999 SKIP_WHITESPACE ();
1000 c = *input_line_pointer;
1001 if (c == '"')
1003 beg = demand_copy_C_string (&dummy);
1004 if (beg == NULL)
1006 ignore_rest_of_line ();
1007 return;
1009 type = obj_elf_section_type (beg, strlen (beg));
1011 else if (c == '@' || c == '%')
1013 beg = ++input_line_pointer;
1014 c = get_symbol_end ();
1015 *input_line_pointer = c;
1016 type = obj_elf_section_type (beg, input_line_pointer - beg);
1018 else
1019 input_line_pointer = save;
1022 SKIP_WHITESPACE ();
1023 if ((attr & SHF_MERGE) != 0 && *input_line_pointer == ',')
1025 ++input_line_pointer;
1026 SKIP_WHITESPACE ();
1027 entsize = get_absolute_expression ();
1028 SKIP_WHITESPACE ();
1029 if (entsize < 0)
1031 as_warn (_("invalid merge entity size"));
1032 attr &= ~SHF_MERGE;
1033 entsize = 0;
1036 else if ((attr & SHF_MERGE) != 0)
1038 as_warn (_("entity size for SHF_MERGE not specified"));
1039 attr &= ~SHF_MERGE;
1042 if ((attr & SHF_GROUP) != 0 && *input_line_pointer == ',')
1044 ++input_line_pointer;
1045 group_name = obj_elf_section_name ();
1046 if (group_name == NULL)
1047 attr &= ~SHF_GROUP;
1048 else if (strncmp (input_line_pointer, ",comdat", 7) == 0)
1050 input_line_pointer += 7;
1051 linkonce = 1;
1053 else if (strncmp (name, ".gnu.linkonce", 13) == 0)
1054 linkonce = 1;
1056 else if ((attr & SHF_GROUP) != 0)
1058 as_warn (_("group name for SHF_GROUP not specified"));
1059 attr &= ~SHF_GROUP;
1062 else
1066 char c;
1068 SKIP_WHITESPACE ();
1069 if (*input_line_pointer != '#')
1071 as_warn (_("character following name is not '#'"));
1072 ignore_rest_of_line ();
1073 return;
1075 beg = ++input_line_pointer;
1076 c = get_symbol_end ();
1077 *input_line_pointer = c;
1079 attr |= obj_elf_section_word (beg, input_line_pointer - beg);
1081 SKIP_WHITESPACE ();
1083 while (*input_line_pointer++ == ',');
1084 --input_line_pointer;
1088 demand_empty_rest_of_line ();
1090 obj_elf_change_section (name, type, attr, entsize, group_name, linkonce, push);
1093 /* Change to the .data section. */
1095 void
1096 obj_elf_data (i)
1097 int i;
1099 #ifdef md_flush_pending_output
1100 md_flush_pending_output ();
1101 #endif
1103 previous_section = now_seg;
1104 previous_subsection = now_subseg;
1105 s_data (i);
1107 #ifdef md_elf_section_change_hook
1108 md_elf_section_change_hook ();
1109 #endif
1112 /* Change to the .text section. */
1114 void
1115 obj_elf_text (i)
1116 int i;
1118 #ifdef md_flush_pending_output
1119 md_flush_pending_output ();
1120 #endif
1122 previous_section = now_seg;
1123 previous_subsection = now_subseg;
1124 s_text (i);
1126 #ifdef md_elf_section_change_hook
1127 md_elf_section_change_hook ();
1128 #endif
1131 static void
1132 obj_elf_subsection (ignore)
1133 int ignore ATTRIBUTE_UNUSED;
1135 register int temp;
1137 #ifdef md_flush_pending_output
1138 md_flush_pending_output ();
1139 #endif
1141 previous_section = now_seg;
1142 previous_subsection = now_subseg;
1144 temp = get_absolute_expression ();
1145 subseg_set (now_seg, (subsegT) temp);
1146 demand_empty_rest_of_line ();
1148 #ifdef md_elf_section_change_hook
1149 md_elf_section_change_hook ();
1150 #endif
1153 /* This can be called from the processor backends if they change
1154 sections. */
1156 void
1157 obj_elf_section_change_hook ()
1159 previous_section = now_seg;
1160 previous_subsection = now_subseg;
1163 void
1164 obj_elf_previous (ignore)
1165 int ignore ATTRIBUTE_UNUSED;
1167 segT new_section;
1168 int new_subsection;
1170 if (previous_section == 0)
1172 as_warn (_(".previous without corresponding .section; ignored"));
1173 return;
1176 #ifdef md_flush_pending_output
1177 md_flush_pending_output ();
1178 #endif
1180 new_section = previous_section;
1181 new_subsection = previous_subsection;
1182 previous_section = now_seg;
1183 previous_subsection = now_subseg;
1184 subseg_set (new_section, new_subsection);
1186 #ifdef md_elf_section_change_hook
1187 md_elf_section_change_hook ();
1188 #endif
1191 static void
1192 obj_elf_popsection (xxx)
1193 int xxx ATTRIBUTE_UNUSED;
1195 struct section_stack *top = section_stack;
1197 if (top == NULL)
1199 as_warn (_(".popsection without corresponding .pushsection; ignored"));
1200 return;
1203 #ifdef md_flush_pending_output
1204 md_flush_pending_output ();
1205 #endif
1207 section_stack = top->next;
1208 previous_section = top->prev_seg;
1209 previous_subsection = top->prev_subseg;
1210 subseg_set (top->seg, top->subseg);
1211 free (top);
1213 #ifdef md_elf_section_change_hook
1214 md_elf_section_change_hook ();
1215 #endif
1218 static void
1219 obj_elf_line (ignore)
1220 int ignore ATTRIBUTE_UNUSED;
1222 /* Assume delimiter is part of expression. BSD4.2 as fails with
1223 delightful bug, so we are not being incompatible here. */
1224 new_logical_line ((char *) NULL, (int) (get_absolute_expression ()));
1225 demand_empty_rest_of_line ();
1228 /* This handles the .symver pseudo-op, which is used to specify a
1229 symbol version. The syntax is ``.symver NAME,SYMVERNAME''.
1230 SYMVERNAME may contain ELF_VER_CHR ('@') characters. This
1231 pseudo-op causes the assembler to emit a symbol named SYMVERNAME
1232 with the same value as the symbol NAME. */
1234 static void
1235 obj_elf_symver (ignore)
1236 int ignore ATTRIBUTE_UNUSED;
1238 char *name;
1239 char c;
1240 char old_lexat;
1241 symbolS *sym;
1243 name = input_line_pointer;
1244 c = get_symbol_end ();
1246 sym = symbol_find_or_make (name);
1248 *input_line_pointer = c;
1250 SKIP_WHITESPACE ();
1251 if (*input_line_pointer != ',')
1253 as_bad (_("expected comma after name in .symver"));
1254 ignore_rest_of_line ();
1255 return;
1258 ++input_line_pointer;
1259 name = input_line_pointer;
1261 /* Temporarily include '@' in symbol names. */
1262 old_lexat = lex_type[(unsigned char) '@'];
1263 lex_type[(unsigned char) '@'] |= LEX_NAME;
1264 c = get_symbol_end ();
1265 lex_type[(unsigned char) '@'] = old_lexat;
1267 if (symbol_get_obj (sym)->versioned_name == NULL)
1269 symbol_get_obj (sym)->versioned_name = xstrdup (name);
1271 *input_line_pointer = c;
1273 if (strchr (symbol_get_obj (sym)->versioned_name,
1274 ELF_VER_CHR) == NULL)
1276 as_bad (_("missing version name in `%s' for symbol `%s'"),
1277 symbol_get_obj (sym)->versioned_name,
1278 S_GET_NAME (sym));
1279 ignore_rest_of_line ();
1280 return;
1283 else
1285 if (strcmp (symbol_get_obj (sym)->versioned_name, name))
1287 as_bad (_("multiple versions [`%s'|`%s'] for symbol `%s'"),
1288 name, symbol_get_obj (sym)->versioned_name,
1289 S_GET_NAME (sym));
1290 ignore_rest_of_line ();
1291 return;
1294 *input_line_pointer = c;
1297 demand_empty_rest_of_line ();
1300 /* This handles the .vtable_inherit pseudo-op, which is used to indicate
1301 to the linker the hierarchy in which a particular table resides. The
1302 syntax is ".vtable_inherit CHILDNAME, PARENTNAME". */
1304 struct fix *
1305 obj_elf_vtable_inherit (ignore)
1306 int ignore ATTRIBUTE_UNUSED;
1308 char *cname, *pname;
1309 symbolS *csym, *psym;
1310 char c, bad = 0;
1312 if (*input_line_pointer == '#')
1313 ++input_line_pointer;
1315 cname = input_line_pointer;
1316 c = get_symbol_end ();
1317 csym = symbol_find (cname);
1319 /* GCFIXME: should check that we don't have two .vtable_inherits for
1320 the same child symbol. Also, we can currently only do this if the
1321 child symbol is already exists and is placed in a fragment. */
1323 if (csym == NULL || symbol_get_frag (csym) == NULL)
1325 as_bad ("expected `%s' to have already been set for .vtable_inherit",
1326 cname);
1327 bad = 1;
1330 *input_line_pointer = c;
1332 SKIP_WHITESPACE ();
1333 if (*input_line_pointer != ',')
1335 as_bad ("expected comma after name in .vtable_inherit");
1336 ignore_rest_of_line ();
1337 return NULL;
1340 ++input_line_pointer;
1341 SKIP_WHITESPACE ();
1343 if (*input_line_pointer == '#')
1344 ++input_line_pointer;
1346 if (input_line_pointer[0] == '0'
1347 && (input_line_pointer[1] == '\0'
1348 || ISSPACE (input_line_pointer[1])))
1350 psym = section_symbol (absolute_section);
1351 ++input_line_pointer;
1353 else
1355 pname = input_line_pointer;
1356 c = get_symbol_end ();
1357 psym = symbol_find_or_make (pname);
1358 *input_line_pointer = c;
1361 demand_empty_rest_of_line ();
1363 if (bad)
1364 return NULL;
1366 assert (symbol_get_value_expression (csym)->X_op == O_constant);
1367 return fix_new (symbol_get_frag (csym),
1368 symbol_get_value_expression (csym)->X_add_number,
1369 0, psym, 0, 0, BFD_RELOC_VTABLE_INHERIT);
1372 /* This handles the .vtable_entry pseudo-op, which is used to indicate
1373 to the linker that a vtable slot was used. The syntax is
1374 ".vtable_entry tablename, offset". */
1376 struct fix *
1377 obj_elf_vtable_entry (ignore)
1378 int ignore ATTRIBUTE_UNUSED;
1380 char *name;
1381 symbolS *sym;
1382 offsetT offset;
1383 char c;
1385 if (*input_line_pointer == '#')
1386 ++input_line_pointer;
1388 name = input_line_pointer;
1389 c = get_symbol_end ();
1390 sym = symbol_find_or_make (name);
1391 *input_line_pointer = c;
1393 SKIP_WHITESPACE ();
1394 if (*input_line_pointer != ',')
1396 as_bad ("expected comma after name in .vtable_entry");
1397 ignore_rest_of_line ();
1398 return NULL;
1401 ++input_line_pointer;
1402 if (*input_line_pointer == '#')
1403 ++input_line_pointer;
1405 offset = get_absolute_expression ();
1407 demand_empty_rest_of_line ();
1409 return fix_new (frag_now, frag_now_fix (), 0, sym, offset, 0,
1410 BFD_RELOC_VTABLE_ENTRY);
1413 void
1414 elf_obj_read_begin_hook ()
1416 #ifdef NEED_ECOFF_DEBUG
1417 if (ECOFF_DEBUGGING)
1418 ecoff_read_begin_hook ();
1419 #endif
1422 void
1423 elf_obj_symbol_new_hook (symbolP)
1424 symbolS *symbolP;
1426 struct elf_obj_sy *sy_obj;
1428 sy_obj = symbol_get_obj (symbolP);
1429 sy_obj->size = NULL;
1430 sy_obj->versioned_name = NULL;
1432 #ifdef NEED_ECOFF_DEBUG
1433 if (ECOFF_DEBUGGING)
1434 ecoff_symbol_new_hook (symbolP);
1435 #endif
1438 /* When setting one symbol equal to another, by default we probably
1439 want them to have the same "size", whatever it means in the current
1440 context. */
1442 void
1443 elf_copy_symbol_attributes (dest, src)
1444 symbolS *dest, *src;
1446 struct elf_obj_sy *srcelf = symbol_get_obj (src);
1447 struct elf_obj_sy *destelf = symbol_get_obj (dest);
1448 if (srcelf->size)
1450 if (destelf->size == NULL)
1451 destelf->size =
1452 (expressionS *) xmalloc (sizeof (expressionS));
1453 *destelf->size = *srcelf->size;
1455 else
1457 if (destelf->size != NULL)
1458 free (destelf->size);
1459 destelf->size = NULL;
1461 S_SET_SIZE (dest, S_GET_SIZE (src));
1462 /* Don't copy visibility. */
1463 S_SET_OTHER (dest, (ELF_ST_VISIBILITY (S_GET_OTHER (dest))
1464 | (S_GET_OTHER (src) & ~ELF_ST_VISIBILITY (-1))));
1467 void
1468 obj_elf_version (ignore)
1469 int ignore ATTRIBUTE_UNUSED;
1471 char *name;
1472 unsigned int c;
1473 char *p;
1474 asection *seg = now_seg;
1475 subsegT subseg = now_subseg;
1476 Elf_Internal_Note i_note;
1477 Elf_External_Note e_note;
1478 asection *note_secp = (asection *) NULL;
1479 int len;
1481 SKIP_WHITESPACE ();
1482 if (*input_line_pointer == '\"')
1484 ++input_line_pointer; /* -> 1st char of string. */
1485 name = input_line_pointer;
1487 while (is_a_char (c = next_char_of_string ()))
1489 c = *input_line_pointer;
1490 *input_line_pointer = '\0';
1491 *(input_line_pointer - 1) = '\0';
1492 *input_line_pointer = c;
1494 /* create the .note section */
1496 note_secp = subseg_new (".note", 0);
1497 bfd_set_section_flags (stdoutput,
1498 note_secp,
1499 SEC_HAS_CONTENTS | SEC_READONLY);
1501 /* process the version string */
1503 len = strlen (name);
1505 i_note.namesz = ((len + 1) + 3) & ~3; /* round this to word boundary */
1506 i_note.descsz = 0; /* no description */
1507 i_note.type = NT_VERSION;
1508 p = frag_more (sizeof (e_note.namesz));
1509 md_number_to_chars (p, (valueT) i_note.namesz, sizeof (e_note.namesz));
1510 p = frag_more (sizeof (e_note.descsz));
1511 md_number_to_chars (p, (valueT) i_note.descsz, sizeof (e_note.descsz));
1512 p = frag_more (sizeof (e_note.type));
1513 md_number_to_chars (p, (valueT) i_note.type, sizeof (e_note.type));
1514 p = frag_more (len + 1);
1515 strcpy (p, name);
1517 frag_align (2, 0, 0);
1519 subseg_set (seg, subseg);
1521 else
1523 as_bad (_("expected quoted string"));
1525 demand_empty_rest_of_line ();
1528 static void
1529 obj_elf_size (ignore)
1530 int ignore ATTRIBUTE_UNUSED;
1532 char *name = input_line_pointer;
1533 char c = get_symbol_end ();
1534 char *p;
1535 expressionS exp;
1536 symbolS *sym;
1538 p = input_line_pointer;
1539 *p = c;
1540 SKIP_WHITESPACE ();
1541 if (*input_line_pointer != ',')
1543 *p = 0;
1544 as_bad (_("expected comma after name `%s' in .size directive"), name);
1545 *p = c;
1546 ignore_rest_of_line ();
1547 return;
1549 input_line_pointer++;
1550 expression (&exp);
1551 if (exp.X_op == O_absent)
1553 as_bad (_("missing expression in .size directive"));
1554 exp.X_op = O_constant;
1555 exp.X_add_number = 0;
1557 *p = 0;
1558 sym = symbol_find_or_make (name);
1559 *p = c;
1560 if (exp.X_op == O_constant)
1562 S_SET_SIZE (sym, exp.X_add_number);
1563 if (symbol_get_obj (sym)->size)
1565 xfree (symbol_get_obj (sym)->size);
1566 symbol_get_obj (sym)->size = NULL;
1569 else
1571 symbol_get_obj (sym)->size =
1572 (expressionS *) xmalloc (sizeof (expressionS));
1573 *symbol_get_obj (sym)->size = exp;
1575 demand_empty_rest_of_line ();
1578 /* Handle the ELF .type pseudo-op. This sets the type of a symbol.
1579 There are five syntaxes:
1581 The first (used on Solaris) is
1582 .type SYM,#function
1583 The second (used on UnixWare) is
1584 .type SYM,@function
1585 The third (reportedly to be used on Irix 6.0) is
1586 .type SYM STT_FUNC
1587 The fourth (used on NetBSD/Arm and Linux/ARM) is
1588 .type SYM,%function
1589 The fifth (used on SVR4/860) is
1590 .type SYM,"function"
1593 static void
1594 obj_elf_type (ignore)
1595 int ignore ATTRIBUTE_UNUSED;
1597 char *name;
1598 char c;
1599 int type;
1600 const char *typename;
1601 symbolS *sym;
1602 elf_symbol_type *elfsym;
1604 name = input_line_pointer;
1605 c = get_symbol_end ();
1606 sym = symbol_find_or_make (name);
1607 elfsym = (elf_symbol_type *) symbol_get_bfdsym (sym);
1608 *input_line_pointer = c;
1610 SKIP_WHITESPACE ();
1611 if (*input_line_pointer == ',')
1612 ++input_line_pointer;
1614 SKIP_WHITESPACE ();
1615 if ( *input_line_pointer == '#'
1616 || *input_line_pointer == '@'
1617 || *input_line_pointer == '"'
1618 || *input_line_pointer == '%')
1619 ++input_line_pointer;
1621 typename = input_line_pointer;
1622 c = get_symbol_end ();
1624 type = 0;
1625 if (strcmp (typename, "function") == 0
1626 || strcmp (typename, "STT_FUNC") == 0)
1627 type = BSF_FUNCTION;
1628 else if (strcmp (typename, "object") == 0
1629 || strcmp (typename, "STT_OBJECT") == 0)
1630 type = BSF_OBJECT;
1631 #ifdef md_elf_symbol_type
1632 else if ((type = md_elf_symbol_type (typename, sym, elfsym)) != -1)
1634 #endif
1635 else
1636 as_bad (_("unrecognized symbol type \"%s\""), typename);
1638 *input_line_pointer = c;
1640 if (*input_line_pointer == '"')
1641 ++input_line_pointer;
1643 elfsym->symbol.flags |= type;
1645 demand_empty_rest_of_line ();
1648 static void
1649 obj_elf_ident (ignore)
1650 int ignore ATTRIBUTE_UNUSED;
1652 static segT comment_section;
1653 segT old_section = now_seg;
1654 int old_subsection = now_subseg;
1656 #ifdef md_flush_pending_output
1657 md_flush_pending_output ();
1658 #endif
1660 if (!comment_section)
1662 char *p;
1663 comment_section = subseg_new (".comment", 0);
1664 bfd_set_section_flags (stdoutput, comment_section,
1665 SEC_READONLY | SEC_HAS_CONTENTS);
1666 p = frag_more (1);
1667 *p = 0;
1669 else
1670 subseg_set (comment_section, 0);
1671 stringer (1);
1672 subseg_set (old_section, old_subsection);
1675 #ifdef INIT_STAB_SECTION
1677 /* The first entry in a .stabs section is special. */
1679 void
1680 obj_elf_init_stab_section (seg)
1681 segT seg;
1683 char *file;
1684 char *p;
1685 char *stabstr_name;
1686 unsigned int stroff;
1688 /* Force the section to align to a longword boundary. Without this,
1689 UnixWare ar crashes. */
1690 bfd_set_section_alignment (stdoutput, seg, 2);
1692 /* Make space for this first symbol. */
1693 p = frag_more (12);
1694 /* Zero it out. */
1695 memset (p, 0, 12);
1696 as_where (&file, (unsigned int *) NULL);
1697 stabstr_name = (char *) xmalloc (strlen (segment_name (seg)) + 4);
1698 strcpy (stabstr_name, segment_name (seg));
1699 strcat (stabstr_name, "str");
1700 stroff = get_stab_string_offset (file, stabstr_name);
1701 know (stroff == 1);
1702 md_number_to_chars (p, stroff, 4);
1703 seg_info (seg)->stabu.p = p;
1706 #endif
1708 /* Fill in the counts in the first entry in a .stabs section. */
1710 static void
1711 adjust_stab_sections (abfd, sec, xxx)
1712 bfd *abfd;
1713 asection *sec;
1714 PTR xxx ATTRIBUTE_UNUSED;
1716 char *name;
1717 asection *strsec;
1718 char *p;
1719 int strsz, nsyms;
1721 if (strncmp (".stab", sec->name, 5))
1722 return;
1723 if (!strcmp ("str", sec->name + strlen (sec->name) - 3))
1724 return;
1726 name = (char *) alloca (strlen (sec->name) + 4);
1727 strcpy (name, sec->name);
1728 strcat (name, "str");
1729 strsec = bfd_get_section_by_name (abfd, name);
1730 if (strsec)
1731 strsz = bfd_section_size (abfd, strsec);
1732 else
1733 strsz = 0;
1734 nsyms = bfd_section_size (abfd, sec) / 12 - 1;
1736 p = seg_info (sec)->stabu.p;
1737 assert (p != 0);
1739 bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6);
1740 bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8);
1743 #ifdef NEED_ECOFF_DEBUG
1745 /* This function is called by the ECOFF code. It is supposed to
1746 record the external symbol information so that the backend can
1747 write it out correctly. The ELF backend doesn't actually handle
1748 this at the moment, so we do it ourselves. We save the information
1749 in the symbol. */
1751 void
1752 elf_ecoff_set_ext (sym, ext)
1753 symbolS *sym;
1754 struct ecoff_extr *ext;
1756 symbol_get_bfdsym (sym)->udata.p = (PTR) ext;
1759 /* This function is called by bfd_ecoff_debug_externals. It is
1760 supposed to *EXT to the external symbol information, and return
1761 whether the symbol should be used at all. */
1763 static boolean
1764 elf_get_extr (sym, ext)
1765 asymbol *sym;
1766 EXTR *ext;
1768 if (sym->udata.p == NULL)
1769 return false;
1770 *ext = *(EXTR *) sym->udata.p;
1771 return true;
1774 /* This function is called by bfd_ecoff_debug_externals. It has
1775 nothing to do for ELF. */
1777 /*ARGSUSED*/
1778 static void
1779 elf_set_index (sym, indx)
1780 asymbol *sym ATTRIBUTE_UNUSED;
1781 bfd_size_type indx ATTRIBUTE_UNUSED;
1785 #endif /* NEED_ECOFF_DEBUG */
1787 void
1788 elf_frob_symbol (symp, puntp)
1789 symbolS *symp;
1790 int *puntp;
1792 struct elf_obj_sy *sy_obj;
1794 #ifdef NEED_ECOFF_DEBUG
1795 if (ECOFF_DEBUGGING)
1796 ecoff_frob_symbol (symp);
1797 #endif
1799 sy_obj = symbol_get_obj (symp);
1801 if (sy_obj->size != NULL)
1803 switch (sy_obj->size->X_op)
1805 case O_subtract:
1806 S_SET_SIZE (symp,
1807 (S_GET_VALUE (sy_obj->size->X_add_symbol)
1808 + sy_obj->size->X_add_number
1809 - S_GET_VALUE (sy_obj->size->X_op_symbol)));
1810 break;
1811 case O_constant:
1812 S_SET_SIZE (symp,
1813 (S_GET_VALUE (sy_obj->size->X_add_symbol)
1814 + sy_obj->size->X_add_number));
1815 break;
1816 default:
1817 as_bad (_(".size expression too complicated to fix up"));
1818 break;
1820 free (sy_obj->size);
1821 sy_obj->size = NULL;
1824 if (sy_obj->versioned_name != NULL)
1826 char *p;
1828 p = strchr (sy_obj->versioned_name, ELF_VER_CHR);
1829 know (p != NULL);
1831 /* This symbol was given a new name with the .symver directive.
1833 If this is an external reference, just rename the symbol to
1834 include the version string. This will make the relocs be
1835 against the correct versioned symbol.
1837 If this is a definition, add an alias. FIXME: Using an alias
1838 will permit the debugging information to refer to the right
1839 symbol. However, it's not clear whether it is the best
1840 approach. */
1842 if (! S_IS_DEFINED (symp))
1844 /* Verify that the name isn't using the @@ syntax--this is
1845 reserved for definitions of the default version to link
1846 against. */
1847 if (p[1] == ELF_VER_CHR)
1849 as_bad (_("invalid attempt to declare external version name as default in symbol `%s'"),
1850 sy_obj->versioned_name);
1851 *puntp = true;
1853 S_SET_NAME (symp, sy_obj->versioned_name);
1855 else
1857 if (p [1] == ELF_VER_CHR && p [2] == ELF_VER_CHR)
1859 size_t l;
1861 /* The @@@ syntax is a special case. It renames the
1862 symbol name to versioned_name with one `@' removed. */
1863 l = strlen (&p[3]) + 1;
1864 memmove (&p [2], &p[3], l);
1865 S_SET_NAME (symp, sy_obj->versioned_name);
1867 else
1869 symbolS *symp2;
1871 /* FIXME: Creating a new symbol here is risky. We're
1872 in the final loop over the symbol table. We can
1873 get away with it only because the symbol goes to
1874 the end of the list, where the loop will still see
1875 it. It would probably be better to do this in
1876 obj_frob_file_before_adjust. */
1878 symp2 = symbol_find_or_make (sy_obj->versioned_name);
1880 /* Now we act as though we saw symp2 = sym. */
1882 S_SET_SEGMENT (symp2, S_GET_SEGMENT (symp));
1884 /* Subtracting out the frag address here is a hack
1885 because we are in the middle of the final loop. */
1886 S_SET_VALUE (symp2,
1887 (S_GET_VALUE (symp)
1888 - symbol_get_frag (symp)->fr_address));
1890 symbol_set_frag (symp2, symbol_get_frag (symp));
1892 /* This will copy over the size information. */
1893 copy_symbol_attributes (symp2, symp);
1895 S_SET_OTHER (symp2, S_GET_OTHER (symp));
1897 if (S_IS_WEAK (symp))
1898 S_SET_WEAK (symp2);
1900 if (S_IS_EXTERNAL (symp))
1901 S_SET_EXTERNAL (symp2);
1906 /* Double check weak symbols. */
1907 if (S_IS_WEAK (symp))
1909 if (S_IS_COMMON (symp))
1910 as_bad (_("symbol `%s' can not be both weak and common"),
1911 S_GET_NAME (symp));
1914 #ifdef TC_MIPS
1915 /* The Irix 5 and 6 assemblers set the type of any common symbol and
1916 any undefined non-function symbol to STT_OBJECT. We try to be
1917 compatible, since newer Irix 5 and 6 linkers care. However, we
1918 only set undefined symbols to be STT_OBJECT if we are on Irix,
1919 because that is the only time gcc will generate the necessary
1920 .global directives to mark functions. */
1922 if (S_IS_COMMON (symp))
1923 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1925 if (strstr (TARGET_OS, "irix") != NULL
1926 && ! S_IS_DEFINED (symp)
1927 && (symbol_get_bfdsym (symp)->flags & BSF_FUNCTION) == 0)
1928 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1929 #endif
1931 #if 0 /* TC_PPC */
1932 /* If TC_PPC is defined, we used to force the type of a symbol to be
1933 BSF_OBJECT if it was otherwise unset. This was required by some
1934 version of VxWorks. Thomas de Lellis <tdel@windriver.com> says
1935 that this is no longer needed, so it is now commented out. */
1936 if ((symbol_get_bfdsym (symp)->flags
1937 & (BSF_FUNCTION | BSF_FILE | BSF_SECTION_SYM)) == 0
1938 && S_IS_DEFINED (symp))
1939 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1940 #endif
1943 struct group_list
1945 asection **head; /* Section lists. */
1946 unsigned int *elt_count; /* Number of sections in each list. */
1947 unsigned int num_group; /* Number of lists. */
1950 /* Called via bfd_map_over_sections. If SEC is a member of a group,
1951 add it to a list of sections belonging to the group. INF is a
1952 pointer to a struct group_list, which is where we store the head of
1953 each list. */
1955 static void
1956 build_group_lists (abfd, sec, inf)
1957 bfd *abfd ATTRIBUTE_UNUSED;
1958 asection *sec;
1959 PTR inf;
1961 struct group_list *list = (struct group_list *) inf;
1962 const char *group_name = elf_group_name (sec);
1963 unsigned int i;
1965 if (group_name == NULL)
1966 return;
1968 /* If this group already has a list, add the section to the head of
1969 the list. */
1970 for (i = 0; i < list->num_group; i++)
1972 if (strcmp (group_name, elf_group_name (list->head[i])) == 0)
1974 elf_next_in_group (sec) = list->head[i];
1975 list->head[i] = sec;
1976 list->elt_count[i] += 1;
1977 return;
1981 /* New group. Make the arrays bigger in chunks to minimize calls to
1982 realloc. */
1983 i = list->num_group;
1984 if ((i & 127) == 0)
1986 unsigned int newsize = i + 128;
1987 list->head = xrealloc (list->head, newsize * sizeof (*list->head));
1988 list->elt_count = xrealloc (list->elt_count,
1989 newsize * sizeof (*list->elt_count));
1991 list->head[i] = sec;
1992 list->elt_count[i] = 1;
1993 list->num_group += 1;
1996 void
1997 elf_frob_file ()
1999 struct group_list list;
2000 unsigned int i;
2002 bfd_map_over_sections (stdoutput, adjust_stab_sections, (PTR) 0);
2004 /* Go find section groups. */
2005 list.num_group = 0;
2006 list.head = NULL;
2007 list.elt_count = NULL;
2008 bfd_map_over_sections (stdoutput, build_group_lists, (PTR) &list);
2010 /* Make the SHT_GROUP sections that describe each section group. We
2011 can't set up the section contents here yet, because elf section
2012 indices have yet to be calculated. elf.c:set_group_contents does
2013 the rest of the work. */
2014 for (i = 0; i < list.num_group; i++)
2016 const char *group_name = elf_group_name (list.head[i]);
2017 const char *sec_name;
2018 asection *s;
2019 flagword flags;
2020 struct symbol *sy;
2021 int has_sym;
2023 flags = SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_GROUP;
2024 for (s = list.head[i]; s != NULL; s = elf_next_in_group (s))
2025 if (elf_linkonce_p (s) != ((flags & SEC_LINK_ONCE) != 0))
2027 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2028 if (s != list.head[i])
2030 as_warn (_("assuming all members of group `%s' are COMDAT"),
2031 group_name);
2032 break;
2036 sec_name = group_name;
2037 sy = symbol_find_exact (group_name);
2038 has_sym = 0;
2039 if (sy != NULL
2040 && (sy == symbol_lastP
2041 || (sy->sy_next != NULL
2042 && sy->sy_next->sy_previous == sy)))
2044 has_sym = 1;
2045 sec_name = ".group";
2047 s = subseg_force_new (sec_name, 0);
2048 if (s == NULL
2049 || !bfd_set_section_flags (stdoutput, s, flags)
2050 || !bfd_set_section_alignment (stdoutput, s, 2))
2052 as_fatal (_("can't create group: %s"),
2053 bfd_errmsg (bfd_get_error ()));
2056 /* Pass a pointer to the first section in this group. */
2057 elf_next_in_group (s) = list.head[i];
2058 if (has_sym)
2059 elf_group_id (s) = sy->bsym;
2061 s->_raw_size = 4 * (list.elt_count[i] + 1);
2062 s->contents = frag_more (s->_raw_size);
2063 frag_now->fr_fix = frag_now_fix_octets ();
2066 #ifdef elf_tc_final_processing
2067 elf_tc_final_processing ();
2068 #endif
2071 /* It removes any unneeded versioned symbols from the symbol table. */
2073 void
2074 elf_frob_file_before_adjust ()
2076 if (symbol_rootP)
2078 symbolS *symp;
2080 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2081 if (!S_IS_DEFINED (symp))
2083 if (symbol_get_obj (symp)->versioned_name)
2085 char *p;
2087 /* The @@@ syntax is a special case. If the symbol is
2088 not defined, 2 `@'s will be removed from the
2089 versioned_name. */
2091 p = strchr (symbol_get_obj (symp)->versioned_name,
2092 ELF_VER_CHR);
2093 know (p != NULL);
2094 if (p [1] == ELF_VER_CHR && p [2] == ELF_VER_CHR)
2096 size_t l = strlen (&p[3]) + 1;
2097 memmove (&p [1], &p[3], l);
2099 if (symbol_used_p (symp) == 0
2100 && symbol_used_in_reloc_p (symp) == 0)
2101 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2104 /* If there was .weak foo, but foo was neither defined nor
2105 used anywhere, remove it. */
2107 else if (S_IS_WEAK (symp)
2108 && symbol_used_p (symp) == 0
2109 && symbol_used_in_reloc_p (symp) == 0)
2110 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2115 /* It is required that we let write_relocs have the opportunity to
2116 optimize away fixups before output has begun, since it is possible
2117 to eliminate all fixups for a section and thus we never should
2118 have generated the relocation section. */
2120 void
2121 elf_frob_file_after_relocs ()
2123 #ifdef NEED_ECOFF_DEBUG
2124 if (ECOFF_DEBUGGING)
2125 /* Generate the ECOFF debugging information. */
2127 const struct ecoff_debug_swap *debug_swap;
2128 struct ecoff_debug_info debug;
2129 char *buf;
2130 asection *sec;
2132 debug_swap
2133 = get_elf_backend_data (stdoutput)->elf_backend_ecoff_debug_swap;
2134 know (debug_swap != (const struct ecoff_debug_swap *) NULL);
2135 ecoff_build_debug (&debug.symbolic_header, &buf, debug_swap);
2137 /* Set up the pointers in debug. */
2138 #define SET(ptr, offset, type) \
2139 debug.ptr = (type) (buf + debug.symbolic_header.offset)
2141 SET (line, cbLineOffset, unsigned char *);
2142 SET (external_dnr, cbDnOffset, PTR);
2143 SET (external_pdr, cbPdOffset, PTR);
2144 SET (external_sym, cbSymOffset, PTR);
2145 SET (external_opt, cbOptOffset, PTR);
2146 SET (external_aux, cbAuxOffset, union aux_ext *);
2147 SET (ss, cbSsOffset, char *);
2148 SET (external_fdr, cbFdOffset, PTR);
2149 SET (external_rfd, cbRfdOffset, PTR);
2150 /* ssext and external_ext are set up just below. */
2152 #undef SET
2154 /* Set up the external symbols. */
2155 debug.ssext = debug.ssext_end = NULL;
2156 debug.external_ext = debug.external_ext_end = NULL;
2157 if (! bfd_ecoff_debug_externals (stdoutput, &debug, debug_swap, true,
2158 elf_get_extr, elf_set_index))
2159 as_fatal (_("failed to set up debugging information: %s"),
2160 bfd_errmsg (bfd_get_error ()));
2162 sec = bfd_get_section_by_name (stdoutput, ".mdebug");
2163 assert (sec != NULL);
2165 know (stdoutput->output_has_begun == false);
2167 /* We set the size of the section, call bfd_set_section_contents
2168 to force the ELF backend to allocate a file position, and then
2169 write out the data. FIXME: Is this really the best way to do
2170 this? */
2171 sec->_raw_size = bfd_ecoff_debug_size (stdoutput, &debug, debug_swap);
2173 /* Pass BUF to bfd_set_section_contents because this will
2174 eventually become a call to fwrite, and ISO C prohibits
2175 passing a NULL pointer to a stdio function even if the
2176 pointer will not be used. */
2177 if (! bfd_set_section_contents (stdoutput, sec, (PTR) buf,
2178 (file_ptr) 0, (bfd_size_type) 0))
2179 as_fatal (_("can't start writing .mdebug section: %s"),
2180 bfd_errmsg (bfd_get_error ()));
2182 know (stdoutput->output_has_begun == true);
2183 know (sec->filepos != 0);
2185 if (! bfd_ecoff_write_debug (stdoutput, &debug, debug_swap,
2186 sec->filepos))
2187 as_fatal (_("could not write .mdebug section: %s"),
2188 bfd_errmsg (bfd_get_error ()));
2190 #endif /* NEED_ECOFF_DEBUG */
2193 #ifdef SCO_ELF
2195 /* Heavily plagarized from obj_elf_version. The idea is to emit the
2196 SCO specific identifier in the .notes section to satisfy the SCO
2197 linker.
2199 This looks more complicated than it really is. As opposed to the
2200 "obvious" solution, this should handle the cross dev cases
2201 correctly. (i.e, hosting on a 64 bit big endian processor, but
2202 generating SCO Elf code) Efficiency isn't a concern, as there
2203 should be exactly one of these sections per object module.
2205 SCO OpenServer 5 identifies it's ELF modules with a standard ELF
2206 .note section.
2208 int_32 namesz = 4 ; Name size
2209 int_32 descsz = 12 ; Descriptive information
2210 int_32 type = 1 ;
2211 char name[4] = "SCO" ; Originator name ALWAYS SCO + NULL
2212 int_32 version = (major ver # << 16) | version of tools ;
2213 int_32 source = (tool_id << 16 ) | 1 ;
2214 int_32 info = 0 ; These are set by the SCO tools, but we
2215 don't know enough about the source
2216 environment to set them. SCO ld currently
2217 ignores them, and recommends we set them
2218 to zero. */
2220 #define SCO_MAJOR_VERSION 0x1
2221 #define SCO_MINOR_VERSION 0x1
2223 void
2224 sco_id ()
2227 char *name;
2228 unsigned int c;
2229 char ch;
2230 char *p;
2231 asection *seg = now_seg;
2232 subsegT subseg = now_subseg;
2233 Elf_Internal_Note i_note;
2234 Elf_External_Note e_note;
2235 asection *note_secp = (asection *) NULL;
2236 int i, len;
2238 /* create the .note section */
2240 note_secp = subseg_new (".note", 0);
2241 bfd_set_section_flags (stdoutput,
2242 note_secp,
2243 SEC_HAS_CONTENTS | SEC_READONLY);
2245 /* process the version string */
2247 i_note.namesz = 4;
2248 i_note.descsz = 12; /* 12 descriptive bytes */
2249 i_note.type = NT_VERSION; /* Contains a version string */
2251 p = frag_more (sizeof (i_note.namesz));
2252 md_number_to_chars (p, (valueT) i_note.namesz, 4);
2254 p = frag_more (sizeof (i_note.descsz));
2255 md_number_to_chars (p, (valueT) i_note.descsz, 4);
2257 p = frag_more (sizeof (i_note.type));
2258 md_number_to_chars (p, (valueT) i_note.type, 4);
2260 p = frag_more (4);
2261 strcpy (p, "SCO");
2263 /* Note: this is the version number of the ELF we're representing */
2264 p = frag_more (4);
2265 md_number_to_chars (p, (SCO_MAJOR_VERSION << 16) | (SCO_MINOR_VERSION), 4);
2267 /* Here, we pick a magic number for ourselves (yes, I "registered"
2268 it with SCO. The bottom bit shows that we are compat with the
2269 SCO ABI. */
2270 p = frag_more (4);
2271 md_number_to_chars (p, 0x4c520000 | 0x0001, 4);
2273 /* If we knew (or cared) what the source language options were, we'd
2274 fill them in here. SCO has given us permission to ignore these
2275 and just set them to zero. */
2276 p = frag_more (4);
2277 md_number_to_chars (p, 0x0000, 4);
2279 frag_align (2, 0, 0);
2281 /* We probably can't restore the current segment, for there likely
2282 isn't one yet... */
2283 if (seg && subseg)
2284 subseg_set (seg, subseg);
2288 #endif /* SCO_ELF */
2290 static int
2291 elf_separate_stab_sections ()
2293 #ifdef NEED_ECOFF_DEBUG
2294 return (!ECOFF_DEBUGGING);
2295 #else
2296 return 1;
2297 #endif
2300 static void
2301 elf_init_stab_section (seg)
2302 segT seg;
2304 #ifdef NEED_ECOFF_DEBUG
2305 if (!ECOFF_DEBUGGING)
2306 #endif
2307 obj_elf_init_stab_section (seg);
2310 const struct format_ops elf_format_ops =
2312 bfd_target_elf_flavour,
2313 0, /* dfl_leading_underscore */
2314 1, /* emit_section_symbols */
2315 elf_begin,
2316 elf_file_symbol,
2317 elf_frob_symbol,
2318 elf_frob_file,
2319 elf_frob_file_before_adjust,
2320 0, /* obj_frob_file_before_fix */
2321 elf_frob_file_after_relocs,
2322 elf_s_get_size, elf_s_set_size,
2323 elf_s_get_align, elf_s_set_align,
2324 elf_s_get_other,
2325 elf_s_set_other,
2326 0, /* s_get_desc */
2327 0, /* s_set_desc */
2328 0, /* s_get_type */
2329 0, /* s_set_type */
2330 elf_copy_symbol_attributes,
2331 #ifdef NEED_ECOFF_DEBUG
2332 ecoff_generate_asm_lineno,
2333 ecoff_stab,
2334 #else
2335 0, /* generate_asm_lineno */
2336 0, /* process_stab */
2337 #endif
2338 elf_separate_stab_sections,
2339 elf_init_stab_section,
2340 elf_sec_sym_ok_for_reloc,
2341 elf_pop_insert,
2342 #ifdef NEED_ECOFF_DEBUG
2343 elf_ecoff_set_ext,
2344 #else
2345 0, /* ecoff_set_ext */
2346 #endif
2347 elf_obj_read_begin_hook,
2348 elf_obj_symbol_new_hook