bfd/
[binutils.git] / gas / config / obj-elf.c
blob05d668f59fef31ae67933a75a9eeb551c46c8070
1 /* ELF object file format
2 Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003 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 bfd_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 offsetT temp, size, sign;
294 symbolS *symbolP;
295 int have_align;
296 expressionS exp;
298 if (flag_mri && is_common)
300 s_mri_common (0);
301 return NULL;
304 name = input_line_pointer;
305 c = get_symbol_end ();
306 /* just after name is now '\0' */
307 p = input_line_pointer;
308 *p = c;
309 SKIP_WHITESPACE ();
310 if (*input_line_pointer != ',')
312 as_bad (_("expected comma after symbol-name"));
313 ignore_rest_of_line ();
314 return NULL;
316 input_line_pointer++; /* skip ',' */
317 temp = get_absolute_expr (&exp);
318 sign = (offsetT) 1 << (stdoutput->arch_info->bits_per_address - 1);
319 size = temp & ((sign << 1) - 1);
320 if (temp != size || !exp.X_unsigned)
322 as_bad (_(".COMMon length (%ld) out of range, ignored."), (long) temp);
323 ignore_rest_of_line ();
324 return NULL;
326 *p = 0;
327 symbolP = symbol_find_or_make (name);
328 *p = c;
329 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
331 as_bad (_("symbol `%s' is already defined"), S_GET_NAME (symbolP));
332 ignore_rest_of_line ();
333 return NULL;
335 if (S_GET_VALUE (symbolP) != 0)
337 if (S_GET_VALUE (symbolP) != (valueT) size)
339 as_warn (_("length of .comm \"%s\" is already %ld; not changed to %ld"),
340 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP),
341 (long) size);
344 know (symbolP->sy_frag == &zero_address_frag);
345 if (*input_line_pointer != ',')
346 have_align = 0;
347 else
349 have_align = 1;
350 input_line_pointer++;
351 SKIP_WHITESPACE ();
353 if (! have_align || *input_line_pointer != '"')
355 if (! have_align)
356 temp = 0;
357 else
359 temp = get_absolute_expr (&exp);
360 if (!exp.X_unsigned)
362 temp = 0;
363 as_warn (_("common alignment negative; 0 assumed"));
366 if (symbol_get_obj (symbolP)->local)
368 segT old_sec;
369 int old_subsec;
370 char *pfrag;
371 int align;
373 /* allocate_bss: */
374 old_sec = now_seg;
375 old_subsec = now_subseg;
376 if (temp)
378 /* convert to a power of 2 alignment */
379 for (align = 0; (temp & 1) == 0; temp >>= 1, ++align);
380 if (temp != 1)
382 as_bad (_("common alignment not a power of 2"));
383 ignore_rest_of_line ();
384 return NULL;
387 else
388 align = 0;
389 record_alignment (bss_section, align);
390 subseg_set (bss_section, 0);
391 if (align)
392 frag_align (align, 0, 0);
393 if (S_GET_SEGMENT (symbolP) == bss_section)
394 symbol_get_frag (symbolP)->fr_symbol = 0;
395 symbol_set_frag (symbolP, frag_now);
396 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
397 (offsetT) size, (char *) 0);
398 *pfrag = 0;
399 S_SET_SIZE (symbolP, size);
400 S_SET_SEGMENT (symbolP, bss_section);
401 S_CLEAR_EXTERNAL (symbolP);
402 subseg_set (old_sec, old_subsec);
404 else
406 allocate_common:
407 S_SET_VALUE (symbolP, (valueT) size);
408 S_SET_ALIGN (symbolP, temp);
409 S_SET_EXTERNAL (symbolP);
410 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
413 else
415 input_line_pointer++;
416 /* @@ Some use the dot, some don't. Can we get some consistency?? */
417 if (*input_line_pointer == '.')
418 input_line_pointer++;
419 /* @@ Some say data, some say bss. */
420 if (strncmp (input_line_pointer, "bss\"", 4)
421 && strncmp (input_line_pointer, "data\"", 5))
423 while (*--input_line_pointer != '"')
425 input_line_pointer--;
426 goto bad_common_segment;
428 while (*input_line_pointer++ != '"')
430 goto allocate_common;
433 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
435 demand_empty_rest_of_line ();
436 return symbolP;
439 bad_common_segment:
440 p = input_line_pointer;
441 while (*p && *p != '\n')
442 p++;
443 c = *p;
444 *p = '\0';
445 as_bad (_("bad .common segment %s"), input_line_pointer + 1);
446 *p = c;
447 input_line_pointer = p;
448 ignore_rest_of_line ();
449 return NULL;
453 void
454 obj_elf_common (is_common)
455 int is_common;
457 elf_common (is_common);
460 static void
461 obj_elf_tls_common (ignore)
462 int ignore ATTRIBUTE_UNUSED;
464 symbolS *symbolP = elf_common (0);
466 if (symbolP)
467 symbol_get_bfdsym (symbolP)->flags |= BSF_THREAD_LOCAL;
470 static void
471 obj_elf_local (ignore)
472 int ignore ATTRIBUTE_UNUSED;
474 char *name;
475 int c;
476 symbolS *symbolP;
480 name = input_line_pointer;
481 c = get_symbol_end ();
482 symbolP = symbol_find_or_make (name);
483 *input_line_pointer = c;
484 SKIP_WHITESPACE ();
485 S_CLEAR_EXTERNAL (symbolP);
486 symbol_get_obj (symbolP)->local = 1;
487 if (c == ',')
489 input_line_pointer++;
490 SKIP_WHITESPACE ();
491 if (*input_line_pointer == '\n')
492 c = '\n';
495 while (c == ',');
496 demand_empty_rest_of_line ();
499 static void
500 obj_elf_weak (ignore)
501 int ignore ATTRIBUTE_UNUSED;
503 char *name;
504 int c;
505 symbolS *symbolP;
509 name = input_line_pointer;
510 c = get_symbol_end ();
511 symbolP = symbol_find_or_make (name);
512 *input_line_pointer = c;
513 SKIP_WHITESPACE ();
514 S_SET_WEAK (symbolP);
515 symbol_get_obj (symbolP)->local = 1;
516 if (c == ',')
518 input_line_pointer++;
519 SKIP_WHITESPACE ();
520 if (*input_line_pointer == '\n')
521 c = '\n';
524 while (c == ',');
525 demand_empty_rest_of_line ();
528 static void
529 obj_elf_visibility (visibility)
530 int visibility;
532 char *name;
533 int c;
534 symbolS *symbolP;
535 asymbol *bfdsym;
536 elf_symbol_type *elfsym;
540 name = input_line_pointer;
541 c = get_symbol_end ();
542 symbolP = symbol_find_or_make (name);
543 *input_line_pointer = c;
545 SKIP_WHITESPACE ();
547 bfdsym = symbol_get_bfdsym (symbolP);
548 elfsym = elf_symbol_from (bfd_asymbol_bfd (bfdsym), bfdsym);
550 assert (elfsym);
552 elfsym->internal_elf_sym.st_other &= ~3;
553 elfsym->internal_elf_sym.st_other |= visibility;
555 if (c == ',')
557 input_line_pointer ++;
559 SKIP_WHITESPACE ();
561 if (*input_line_pointer == '\n')
562 c = '\n';
565 while (c == ',');
567 demand_empty_rest_of_line ();
570 static segT previous_section;
571 static int previous_subsection;
573 struct section_stack
575 struct section_stack *next;
576 segT seg, prev_seg;
577 int subseg, prev_subseg;
580 static struct section_stack *section_stack;
582 /* Handle the .section pseudo-op. This code supports two different
583 syntaxes.
585 The first is found on Solaris, and looks like
586 .section ".sec1",#alloc,#execinstr,#write
587 Here the names after '#' are the SHF_* flags to turn on for the
588 section. I'm not sure how it determines the SHT_* type (BFD
589 doesn't really give us control over the type, anyhow).
591 The second format is found on UnixWare, and probably most SVR4
592 machines, and looks like
593 .section .sec1,"a",@progbits
594 The quoted string may contain any combination of a, w, x, and
595 represents the SHF_* flags to turn on for the section. The string
596 beginning with '@' can be progbits or nobits. There should be
597 other possibilities, but I don't know what they are. In any case,
598 BFD doesn't really let us set the section type. */
600 /* Certain named sections have particular defined types, listed on p.
601 4-19 of the ABI. */
602 struct special_section
604 const char *name;
605 int type;
606 int attributes;
609 static struct special_section const special_sections[] =
611 { ".bss", SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
612 { ".comment", SHT_PROGBITS, 0 },
613 { ".data", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
614 { ".data1", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
615 { ".debug", SHT_PROGBITS, 0 },
616 #if defined (TC_HPPA) && !defined (TE_LINUX) && TARGET_ARCH_SIZE == 64
617 { ".fini", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
618 { ".init", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
619 #else
620 { ".fini", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
621 { ".init", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
622 #endif
623 { ".line", SHT_PROGBITS, 0 },
624 { ".note", SHT_NOTE, 0 },
625 { ".rodata", SHT_PROGBITS, SHF_ALLOC },
626 { ".rodata1", SHT_PROGBITS, SHF_ALLOC },
627 { ".tbss", SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
628 { ".tdata", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
629 { ".text", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
630 { ".init_array",SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
631 { ".fini_array",SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
632 { ".preinit_array",SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
634 #ifdef ELF_TC_SPECIAL_SECTIONS
635 ELF_TC_SPECIAL_SECTIONS
636 #endif
638 #if 0
639 /* The following section names are special, but they can not
640 reasonably appear in assembler code. Some of the attributes are
641 processor dependent. */
642 { ".dynamic", SHT_DYNAMIC, SHF_ALLOC /* + SHF_WRITE */ },
643 { ".dynstr", SHT_STRTAB, SHF_ALLOC },
644 { ".dynsym", SHT_DYNSYM, SHF_ALLOC },
645 { ".got", SHT_PROGBITS, 0 },
646 { ".hash", SHT_HASH, SHF_ALLOC },
647 { ".interp", SHT_PROGBITS, /* SHF_ALLOC */ },
648 { ".plt", SHT_PROGBITS, 0 },
649 { ".shstrtab",SHT_STRTAB, 0 },
650 { ".strtab", SHT_STRTAB, /* SHF_ALLOC */ },
651 { ".symtab", SHT_SYMTAB, /* SHF_ALLOC */ },
652 #endif
654 { NULL, 0, 0 }
657 void
658 obj_elf_change_section (name, type, attr, entsize, group_name, linkonce, push)
659 const char *name;
660 int type;
661 int attr;
662 int entsize;
663 const char *group_name;
664 int linkonce;
665 int push;
667 asection *old_sec;
668 segT sec;
669 flagword flags;
670 int i;
672 #ifdef md_flush_pending_output
673 md_flush_pending_output ();
674 #endif
676 /* Switch to the section, creating it if necessary. */
677 if (push)
679 struct section_stack *elt;
680 elt = xmalloc (sizeof (struct section_stack));
681 elt->next = section_stack;
682 elt->seg = now_seg;
683 elt->prev_seg = previous_section;
684 elt->subseg = now_subseg;
685 elt->prev_subseg = previous_subsection;
686 section_stack = elt;
688 previous_section = now_seg;
689 previous_subsection = now_subseg;
691 old_sec = bfd_get_section_by_name (stdoutput, name);
692 sec = subseg_new (name, 0);
694 /* See if this is one of the special sections. */
695 for (i = 0; special_sections[i].name != NULL; i++)
696 if (strcmp (name, special_sections[i].name) == 0)
698 if (type == SHT_NULL)
699 type = special_sections[i].type;
700 else if (type != special_sections[i].type)
702 if (old_sec == NULL
703 /* FIXME: gcc, as of 2002-10-22, will emit
705 .section .init_array,"aw",@progbits
707 for __attribute__ ((section (".init_array"))).
708 "@progbits" is incorrect. */
709 && special_sections[i].type != SHT_INIT_ARRAY
710 && special_sections[i].type != SHT_FINI_ARRAY
711 && special_sections[i].type != SHT_PREINIT_ARRAY)
713 as_warn (_("setting incorrect section type for %s"), name);
715 else
717 as_warn (_("ignoring incorrect section type for %s"), name);
718 type = special_sections[i].type;
721 if ((attr &~ special_sections[i].attributes) != 0
722 && old_sec == NULL)
724 /* As a GNU extension, we permit a .note section to be
725 allocatable. If the linker sees an allocateable .note
726 section, it will create a PT_NOTE segment in the output
727 file. */
728 if (strcmp (name, ".note") != 0
729 || attr != SHF_ALLOC)
730 as_warn (_("setting incorrect section attributes for %s"),
731 name);
733 attr |= special_sections[i].attributes;
734 break;
737 /* Convert ELF type and flags to BFD flags. */
738 flags = (SEC_RELOC
739 | ((attr & SHF_WRITE) ? 0 : SEC_READONLY)
740 | ((attr & SHF_ALLOC) ? SEC_ALLOC : 0)
741 | (((attr & SHF_ALLOC) && type != SHT_NOBITS) ? SEC_LOAD : 0)
742 | ((attr & SHF_EXECINSTR) ? SEC_CODE : 0)
743 | ((attr & SHF_MERGE) ? SEC_MERGE : 0)
744 | ((attr & SHF_STRINGS) ? SEC_STRINGS : 0)
745 | ((attr & SHF_TLS) ? SEC_THREAD_LOCAL : 0));
746 #ifdef md_elf_section_flags
747 flags = md_elf_section_flags (flags, attr, type);
748 #endif
750 if (old_sec == NULL)
752 symbolS *secsym;
754 /* Prevent SEC_HAS_CONTENTS from being inadvertently set. */
755 if (type == SHT_NOBITS)
756 seg_info (sec)->bss = 1;
758 bfd_set_section_flags (stdoutput, sec, flags);
759 if (flags & SEC_MERGE)
760 sec->entsize = entsize;
761 elf_group_name (sec) = group_name;
762 elf_linkonce_p (sec) = linkonce;
764 /* Add a symbol for this section to the symbol table. */
765 secsym = symbol_find (name);
766 if (secsym != NULL)
767 symbol_set_bfdsym (secsym, sec->symbol);
768 else
769 symbol_table_insert (section_symbol (sec));
771 else if (attr != 0)
773 /* If section attributes are specified the second time we see a
774 particular section, then check that they are the same as we
775 saw the first time. */
776 if (((old_sec->flags ^ flags)
777 & (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
778 | SEC_EXCLUDE | SEC_SORT_ENTRIES | SEC_MERGE | SEC_STRINGS
779 | SEC_THREAD_LOCAL))
780 || linkonce != elf_linkonce_p (sec))
781 as_warn (_("ignoring changed section attributes for %s"), name);
782 if ((flags & SEC_MERGE) && old_sec->entsize != (unsigned) entsize)
783 as_warn (_("ignoring changed section entity size for %s"), name);
784 if ((attr & SHF_GROUP) != 0
785 && strcmp (elf_group_name (old_sec), group_name) != 0)
786 as_warn (_("ignoring new section group for %s"), name);
789 #ifdef md_elf_section_change_hook
790 md_elf_section_change_hook ();
791 #endif
794 static int
795 obj_elf_parse_section_letters (str, len)
796 char *str;
797 size_t len;
799 int attr = 0;
801 while (len > 0)
803 switch (*str)
805 case 'a':
806 attr |= SHF_ALLOC;
807 break;
808 case 'w':
809 attr |= SHF_WRITE;
810 break;
811 case 'x':
812 attr |= SHF_EXECINSTR;
813 break;
814 case 'M':
815 attr |= SHF_MERGE;
816 break;
817 case 'S':
818 attr |= SHF_STRINGS;
819 break;
820 case 'G':
821 attr |= SHF_GROUP;
822 break;
823 case 'T':
824 attr |= SHF_TLS;
825 break;
826 /* Compatibility. */
827 case 'm':
828 if (*(str - 1) == 'a')
830 attr |= SHF_MERGE;
831 if (len > 1 && str[1] == 's')
833 attr |= SHF_STRINGS;
834 str++, len--;
836 break;
838 default:
840 char *bad_msg = _("unrecognized .section attribute: want a,w,x,M,S,G,T");
841 #ifdef md_elf_section_letter
842 int md_attr = md_elf_section_letter (*str, &bad_msg);
843 if (md_attr >= 0)
844 attr |= md_attr;
845 else
846 #endif
848 as_warn ("%s", bad_msg);
849 attr = -1;
852 break;
854 str++, len--;
857 return attr;
860 static int
861 obj_elf_section_word (str, len)
862 char *str;
863 size_t len;
865 if (len == 5 && strncmp (str, "write", 5) == 0)
866 return SHF_WRITE;
867 if (len == 5 && strncmp (str, "alloc", 5) == 0)
868 return SHF_ALLOC;
869 if (len == 9 && strncmp (str, "execinstr", 9) == 0)
870 return SHF_EXECINSTR;
871 if (len == 3 && strncmp (str, "tls", 3) == 0)
872 return SHF_TLS;
874 #ifdef md_elf_section_word
876 int md_attr = md_elf_section_word (str, len);
877 if (md_attr >= 0)
878 return md_attr;
880 #endif
882 as_warn (_("unrecognized section attribute"));
883 return 0;
886 static int
887 obj_elf_section_type (str, len)
888 char *str;
889 size_t len;
891 if (len == 8 && strncmp (str, "progbits", 8) == 0)
892 return SHT_PROGBITS;
893 if (len == 6 && strncmp (str, "nobits", 6) == 0)
894 return SHT_NOBITS;
896 #ifdef md_elf_section_type
898 int md_type = md_elf_section_type (str, len);
899 if (md_type >= 0)
900 return md_type;
902 #endif
904 as_warn (_("unrecognized section type"));
905 return 0;
908 /* Get name of section. */
909 static char *
910 obj_elf_section_name ()
912 char *name;
914 SKIP_WHITESPACE ();
915 if (*input_line_pointer == '"')
917 int dummy;
919 name = demand_copy_C_string (&dummy);
920 if (name == NULL)
922 ignore_rest_of_line ();
923 return NULL;
926 else
928 char *end = input_line_pointer;
930 while (0 == strchr ("\n\t,; ", *end))
931 end++;
932 if (end == input_line_pointer)
934 as_warn (_("missing name"));
935 ignore_rest_of_line ();
936 return NULL;
939 name = xmalloc (end - input_line_pointer + 1);
940 memcpy (name, input_line_pointer, end - input_line_pointer);
941 name[end - input_line_pointer] = '\0';
942 input_line_pointer = end;
944 SKIP_WHITESPACE ();
945 return name;
948 void
949 obj_elf_section (push)
950 int push;
952 char *name, *group_name, *beg;
953 int type, attr, dummy;
954 int entsize;
955 int linkonce;
957 #ifndef TC_I370
958 if (flag_mri)
960 char mri_type;
962 #ifdef md_flush_pending_output
963 md_flush_pending_output ();
964 #endif
966 previous_section = now_seg;
967 previous_subsection = now_subseg;
969 s_mri_sect (&mri_type);
971 #ifdef md_elf_section_change_hook
972 md_elf_section_change_hook ();
973 #endif
975 return;
977 #endif /* ! defined (TC_I370) */
979 name = obj_elf_section_name ();
980 if (name == NULL)
981 return;
982 type = SHT_NULL;
983 attr = 0;
984 group_name = NULL;
985 entsize = 0;
986 linkonce = 0;
988 if (*input_line_pointer == ',')
990 /* Skip the comma. */
991 ++input_line_pointer;
992 SKIP_WHITESPACE ();
994 if (*input_line_pointer == '"')
996 beg = demand_copy_C_string (&dummy);
997 if (beg == NULL)
999 ignore_rest_of_line ();
1000 return;
1002 attr |= obj_elf_parse_section_letters (beg, strlen (beg));
1004 SKIP_WHITESPACE ();
1005 if (*input_line_pointer == ',')
1007 char c;
1008 char *save = input_line_pointer;
1010 ++input_line_pointer;
1011 SKIP_WHITESPACE ();
1012 c = *input_line_pointer;
1013 if (c == '"')
1015 beg = demand_copy_C_string (&dummy);
1016 if (beg == NULL)
1018 ignore_rest_of_line ();
1019 return;
1021 type = obj_elf_section_type (beg, strlen (beg));
1023 else if (c == '@' || c == '%')
1025 beg = ++input_line_pointer;
1026 c = get_symbol_end ();
1027 *input_line_pointer = c;
1028 type = obj_elf_section_type (beg, input_line_pointer - beg);
1030 else
1031 input_line_pointer = save;
1034 SKIP_WHITESPACE ();
1035 if ((attr & SHF_MERGE) != 0 && *input_line_pointer == ',')
1037 ++input_line_pointer;
1038 SKIP_WHITESPACE ();
1039 entsize = get_absolute_expression ();
1040 SKIP_WHITESPACE ();
1041 if (entsize < 0)
1043 as_warn (_("invalid merge entity size"));
1044 attr &= ~SHF_MERGE;
1045 entsize = 0;
1048 else if ((attr & SHF_MERGE) != 0)
1050 as_warn (_("entity size for SHF_MERGE not specified"));
1051 attr &= ~SHF_MERGE;
1054 if ((attr & SHF_GROUP) != 0 && *input_line_pointer == ',')
1056 ++input_line_pointer;
1057 group_name = obj_elf_section_name ();
1058 if (group_name == NULL)
1059 attr &= ~SHF_GROUP;
1060 else if (strncmp (input_line_pointer, ",comdat", 7) == 0)
1062 input_line_pointer += 7;
1063 linkonce = 1;
1065 else if (strncmp (name, ".gnu.linkonce", 13) == 0)
1066 linkonce = 1;
1068 else if ((attr & SHF_GROUP) != 0)
1070 as_warn (_("group name for SHF_GROUP not specified"));
1071 attr &= ~SHF_GROUP;
1074 else
1078 char c;
1080 SKIP_WHITESPACE ();
1081 if (*input_line_pointer != '#')
1083 as_warn (_("character following name is not '#'"));
1084 ignore_rest_of_line ();
1085 return;
1087 beg = ++input_line_pointer;
1088 c = get_symbol_end ();
1089 *input_line_pointer = c;
1091 attr |= obj_elf_section_word (beg, input_line_pointer - beg);
1093 SKIP_WHITESPACE ();
1095 while (*input_line_pointer++ == ',');
1096 --input_line_pointer;
1100 demand_empty_rest_of_line ();
1102 obj_elf_change_section (name, type, attr, entsize, group_name, linkonce, push);
1105 /* Change to the .data section. */
1107 void
1108 obj_elf_data (i)
1109 int i;
1111 #ifdef md_flush_pending_output
1112 md_flush_pending_output ();
1113 #endif
1115 previous_section = now_seg;
1116 previous_subsection = now_subseg;
1117 s_data (i);
1119 #ifdef md_elf_section_change_hook
1120 md_elf_section_change_hook ();
1121 #endif
1124 /* Change to the .text section. */
1126 void
1127 obj_elf_text (i)
1128 int i;
1130 #ifdef md_flush_pending_output
1131 md_flush_pending_output ();
1132 #endif
1134 previous_section = now_seg;
1135 previous_subsection = now_subseg;
1136 s_text (i);
1138 #ifdef md_elf_section_change_hook
1139 md_elf_section_change_hook ();
1140 #endif
1143 static void
1144 obj_elf_subsection (ignore)
1145 int ignore ATTRIBUTE_UNUSED;
1147 register int temp;
1149 #ifdef md_flush_pending_output
1150 md_flush_pending_output ();
1151 #endif
1153 previous_section = now_seg;
1154 previous_subsection = now_subseg;
1156 temp = get_absolute_expression ();
1157 subseg_set (now_seg, (subsegT) temp);
1158 demand_empty_rest_of_line ();
1160 #ifdef md_elf_section_change_hook
1161 md_elf_section_change_hook ();
1162 #endif
1165 /* This can be called from the processor backends if they change
1166 sections. */
1168 void
1169 obj_elf_section_change_hook ()
1171 previous_section = now_seg;
1172 previous_subsection = now_subseg;
1175 void
1176 obj_elf_previous (ignore)
1177 int ignore ATTRIBUTE_UNUSED;
1179 segT new_section;
1180 int new_subsection;
1182 if (previous_section == 0)
1184 as_warn (_(".previous without corresponding .section; ignored"));
1185 return;
1188 #ifdef md_flush_pending_output
1189 md_flush_pending_output ();
1190 #endif
1192 new_section = previous_section;
1193 new_subsection = previous_subsection;
1194 previous_section = now_seg;
1195 previous_subsection = now_subseg;
1196 subseg_set (new_section, new_subsection);
1198 #ifdef md_elf_section_change_hook
1199 md_elf_section_change_hook ();
1200 #endif
1203 static void
1204 obj_elf_popsection (xxx)
1205 int xxx ATTRIBUTE_UNUSED;
1207 struct section_stack *top = section_stack;
1209 if (top == NULL)
1211 as_warn (_(".popsection without corresponding .pushsection; ignored"));
1212 return;
1215 #ifdef md_flush_pending_output
1216 md_flush_pending_output ();
1217 #endif
1219 section_stack = top->next;
1220 previous_section = top->prev_seg;
1221 previous_subsection = top->prev_subseg;
1222 subseg_set (top->seg, top->subseg);
1223 free (top);
1225 #ifdef md_elf_section_change_hook
1226 md_elf_section_change_hook ();
1227 #endif
1230 static void
1231 obj_elf_line (ignore)
1232 int ignore ATTRIBUTE_UNUSED;
1234 /* Assume delimiter is part of expression. BSD4.2 as fails with
1235 delightful bug, so we are not being incompatible here. */
1236 new_logical_line ((char *) NULL, (int) (get_absolute_expression ()));
1237 demand_empty_rest_of_line ();
1240 /* This handles the .symver pseudo-op, which is used to specify a
1241 symbol version. The syntax is ``.symver NAME,SYMVERNAME''.
1242 SYMVERNAME may contain ELF_VER_CHR ('@') characters. This
1243 pseudo-op causes the assembler to emit a symbol named SYMVERNAME
1244 with the same value as the symbol NAME. */
1246 static void
1247 obj_elf_symver (ignore)
1248 int ignore ATTRIBUTE_UNUSED;
1250 char *name;
1251 char c;
1252 char old_lexat;
1253 symbolS *sym;
1255 name = input_line_pointer;
1256 c = get_symbol_end ();
1258 sym = symbol_find_or_make (name);
1260 *input_line_pointer = c;
1262 SKIP_WHITESPACE ();
1263 if (*input_line_pointer != ',')
1265 as_bad (_("expected comma after name in .symver"));
1266 ignore_rest_of_line ();
1267 return;
1270 ++input_line_pointer;
1271 name = input_line_pointer;
1273 /* Temporarily include '@' in symbol names. */
1274 old_lexat = lex_type[(unsigned char) '@'];
1275 lex_type[(unsigned char) '@'] |= LEX_NAME;
1276 c = get_symbol_end ();
1277 lex_type[(unsigned char) '@'] = old_lexat;
1279 if (symbol_get_obj (sym)->versioned_name == NULL)
1281 symbol_get_obj (sym)->versioned_name = xstrdup (name);
1283 *input_line_pointer = c;
1285 if (strchr (symbol_get_obj (sym)->versioned_name,
1286 ELF_VER_CHR) == NULL)
1288 as_bad (_("missing version name in `%s' for symbol `%s'"),
1289 symbol_get_obj (sym)->versioned_name,
1290 S_GET_NAME (sym));
1291 ignore_rest_of_line ();
1292 return;
1295 else
1297 if (strcmp (symbol_get_obj (sym)->versioned_name, name))
1299 as_bad (_("multiple versions [`%s'|`%s'] for symbol `%s'"),
1300 name, symbol_get_obj (sym)->versioned_name,
1301 S_GET_NAME (sym));
1302 ignore_rest_of_line ();
1303 return;
1306 *input_line_pointer = c;
1309 demand_empty_rest_of_line ();
1312 /* This handles the .vtable_inherit pseudo-op, which is used to indicate
1313 to the linker the hierarchy in which a particular table resides. The
1314 syntax is ".vtable_inherit CHILDNAME, PARENTNAME". */
1316 struct fix *
1317 obj_elf_vtable_inherit (ignore)
1318 int ignore ATTRIBUTE_UNUSED;
1320 char *cname, *pname;
1321 symbolS *csym, *psym;
1322 char c, bad = 0;
1324 if (*input_line_pointer == '#')
1325 ++input_line_pointer;
1327 cname = input_line_pointer;
1328 c = get_symbol_end ();
1329 csym = symbol_find (cname);
1331 /* GCFIXME: should check that we don't have two .vtable_inherits for
1332 the same child symbol. Also, we can currently only do this if the
1333 child symbol is already exists and is placed in a fragment. */
1335 if (csym == NULL || symbol_get_frag (csym) == NULL)
1337 as_bad ("expected `%s' to have already been set for .vtable_inherit",
1338 cname);
1339 bad = 1;
1342 *input_line_pointer = c;
1344 SKIP_WHITESPACE ();
1345 if (*input_line_pointer != ',')
1347 as_bad ("expected comma after name in .vtable_inherit");
1348 ignore_rest_of_line ();
1349 return NULL;
1352 ++input_line_pointer;
1353 SKIP_WHITESPACE ();
1355 if (*input_line_pointer == '#')
1356 ++input_line_pointer;
1358 if (input_line_pointer[0] == '0'
1359 && (input_line_pointer[1] == '\0'
1360 || ISSPACE (input_line_pointer[1])))
1362 psym = section_symbol (absolute_section);
1363 ++input_line_pointer;
1365 else
1367 pname = input_line_pointer;
1368 c = get_symbol_end ();
1369 psym = symbol_find_or_make (pname);
1370 *input_line_pointer = c;
1373 demand_empty_rest_of_line ();
1375 if (bad)
1376 return NULL;
1378 assert (symbol_get_value_expression (csym)->X_op == O_constant);
1379 return fix_new (symbol_get_frag (csym),
1380 symbol_get_value_expression (csym)->X_add_number,
1381 0, psym, 0, 0, BFD_RELOC_VTABLE_INHERIT);
1384 /* This handles the .vtable_entry pseudo-op, which is used to indicate
1385 to the linker that a vtable slot was used. The syntax is
1386 ".vtable_entry tablename, offset". */
1388 struct fix *
1389 obj_elf_vtable_entry (ignore)
1390 int ignore ATTRIBUTE_UNUSED;
1392 char *name;
1393 symbolS *sym;
1394 offsetT offset;
1395 char c;
1397 if (*input_line_pointer == '#')
1398 ++input_line_pointer;
1400 name = input_line_pointer;
1401 c = get_symbol_end ();
1402 sym = symbol_find_or_make (name);
1403 *input_line_pointer = c;
1405 SKIP_WHITESPACE ();
1406 if (*input_line_pointer != ',')
1408 as_bad ("expected comma after name in .vtable_entry");
1409 ignore_rest_of_line ();
1410 return NULL;
1413 ++input_line_pointer;
1414 if (*input_line_pointer == '#')
1415 ++input_line_pointer;
1417 offset = get_absolute_expression ();
1419 demand_empty_rest_of_line ();
1421 return fix_new (frag_now, frag_now_fix (), 0, sym, offset, 0,
1422 BFD_RELOC_VTABLE_ENTRY);
1425 void
1426 elf_obj_read_begin_hook ()
1428 #ifdef NEED_ECOFF_DEBUG
1429 if (ECOFF_DEBUGGING)
1430 ecoff_read_begin_hook ();
1431 #endif
1434 void
1435 elf_obj_symbol_new_hook (symbolP)
1436 symbolS *symbolP;
1438 struct elf_obj_sy *sy_obj;
1440 sy_obj = symbol_get_obj (symbolP);
1441 sy_obj->size = NULL;
1442 sy_obj->versioned_name = NULL;
1444 #ifdef NEED_ECOFF_DEBUG
1445 if (ECOFF_DEBUGGING)
1446 ecoff_symbol_new_hook (symbolP);
1447 #endif
1450 /* When setting one symbol equal to another, by default we probably
1451 want them to have the same "size", whatever it means in the current
1452 context. */
1454 void
1455 elf_copy_symbol_attributes (dest, src)
1456 symbolS *dest, *src;
1458 struct elf_obj_sy *srcelf = symbol_get_obj (src);
1459 struct elf_obj_sy *destelf = symbol_get_obj (dest);
1460 if (srcelf->size)
1462 if (destelf->size == NULL)
1463 destelf->size =
1464 (expressionS *) xmalloc (sizeof (expressionS));
1465 *destelf->size = *srcelf->size;
1467 else
1469 if (destelf->size != NULL)
1470 free (destelf->size);
1471 destelf->size = NULL;
1473 S_SET_SIZE (dest, S_GET_SIZE (src));
1474 /* Don't copy visibility. */
1475 S_SET_OTHER (dest, (ELF_ST_VISIBILITY (S_GET_OTHER (dest))
1476 | (S_GET_OTHER (src) & ~ELF_ST_VISIBILITY (-1))));
1479 void
1480 obj_elf_version (ignore)
1481 int ignore ATTRIBUTE_UNUSED;
1483 char *name;
1484 unsigned int c;
1485 char *p;
1486 asection *seg = now_seg;
1487 subsegT subseg = now_subseg;
1488 Elf_Internal_Note i_note;
1489 Elf_External_Note e_note;
1490 asection *note_secp = (asection *) NULL;
1491 int len;
1493 SKIP_WHITESPACE ();
1494 if (*input_line_pointer == '\"')
1496 ++input_line_pointer; /* -> 1st char of string. */
1497 name = input_line_pointer;
1499 while (is_a_char (c = next_char_of_string ()))
1501 c = *input_line_pointer;
1502 *input_line_pointer = '\0';
1503 *(input_line_pointer - 1) = '\0';
1504 *input_line_pointer = c;
1506 /* create the .note section */
1508 note_secp = subseg_new (".note", 0);
1509 bfd_set_section_flags (stdoutput,
1510 note_secp,
1511 SEC_HAS_CONTENTS | SEC_READONLY);
1513 /* process the version string */
1515 len = strlen (name);
1517 i_note.namesz = ((len + 1) + 3) & ~3; /* round this to word boundary */
1518 i_note.descsz = 0; /* no description */
1519 i_note.type = NT_VERSION;
1520 p = frag_more (sizeof (e_note.namesz));
1521 md_number_to_chars (p, (valueT) i_note.namesz, sizeof (e_note.namesz));
1522 p = frag_more (sizeof (e_note.descsz));
1523 md_number_to_chars (p, (valueT) i_note.descsz, sizeof (e_note.descsz));
1524 p = frag_more (sizeof (e_note.type));
1525 md_number_to_chars (p, (valueT) i_note.type, sizeof (e_note.type));
1526 p = frag_more (len + 1);
1527 strcpy (p, name);
1529 frag_align (2, 0, 0);
1531 subseg_set (seg, subseg);
1533 else
1535 as_bad (_("expected quoted string"));
1537 demand_empty_rest_of_line ();
1540 static void
1541 obj_elf_size (ignore)
1542 int ignore ATTRIBUTE_UNUSED;
1544 char *name = input_line_pointer;
1545 char c = get_symbol_end ();
1546 char *p;
1547 expressionS exp;
1548 symbolS *sym;
1550 p = input_line_pointer;
1551 *p = c;
1552 SKIP_WHITESPACE ();
1553 if (*input_line_pointer != ',')
1555 *p = 0;
1556 as_bad (_("expected comma after name `%s' in .size directive"), name);
1557 *p = c;
1558 ignore_rest_of_line ();
1559 return;
1561 input_line_pointer++;
1562 expression (&exp);
1563 if (exp.X_op == O_absent)
1565 as_bad (_("missing expression in .size directive"));
1566 exp.X_op = O_constant;
1567 exp.X_add_number = 0;
1569 *p = 0;
1570 sym = symbol_find_or_make (name);
1571 *p = c;
1572 if (exp.X_op == O_constant)
1574 S_SET_SIZE (sym, exp.X_add_number);
1575 if (symbol_get_obj (sym)->size)
1577 xfree (symbol_get_obj (sym)->size);
1578 symbol_get_obj (sym)->size = NULL;
1581 else
1583 symbol_get_obj (sym)->size =
1584 (expressionS *) xmalloc (sizeof (expressionS));
1585 *symbol_get_obj (sym)->size = exp;
1587 demand_empty_rest_of_line ();
1590 /* Handle the ELF .type pseudo-op. This sets the type of a symbol.
1591 There are five syntaxes:
1593 The first (used on Solaris) is
1594 .type SYM,#function
1595 The second (used on UnixWare) is
1596 .type SYM,@function
1597 The third (reportedly to be used on Irix 6.0) is
1598 .type SYM STT_FUNC
1599 The fourth (used on NetBSD/Arm and Linux/ARM) is
1600 .type SYM,%function
1601 The fifth (used on SVR4/860) is
1602 .type SYM,"function"
1605 static void
1606 obj_elf_type (ignore)
1607 int ignore ATTRIBUTE_UNUSED;
1609 char *name;
1610 char c;
1611 int type;
1612 const char *typename;
1613 symbolS *sym;
1614 elf_symbol_type *elfsym;
1616 name = input_line_pointer;
1617 c = get_symbol_end ();
1618 sym = symbol_find_or_make (name);
1619 elfsym = (elf_symbol_type *) symbol_get_bfdsym (sym);
1620 *input_line_pointer = c;
1622 SKIP_WHITESPACE ();
1623 if (*input_line_pointer == ',')
1624 ++input_line_pointer;
1626 SKIP_WHITESPACE ();
1627 if ( *input_line_pointer == '#'
1628 || *input_line_pointer == '@'
1629 || *input_line_pointer == '"'
1630 || *input_line_pointer == '%')
1631 ++input_line_pointer;
1633 typename = input_line_pointer;
1634 c = get_symbol_end ();
1636 type = 0;
1637 if (strcmp (typename, "function") == 0
1638 || strcmp (typename, "STT_FUNC") == 0)
1639 type = BSF_FUNCTION;
1640 else if (strcmp (typename, "object") == 0
1641 || strcmp (typename, "STT_OBJECT") == 0)
1642 type = BSF_OBJECT;
1643 else if (strcmp (typename, "tls_object") == 0
1644 || strcmp (typename, "STT_TLS") == 0)
1645 type = BSF_OBJECT | BSF_THREAD_LOCAL;
1646 #ifdef md_elf_symbol_type
1647 else if ((type = md_elf_symbol_type (typename, sym, elfsym)) != -1)
1649 #endif
1650 else
1651 as_bad (_("unrecognized symbol type \"%s\""), typename);
1653 *input_line_pointer = c;
1655 if (*input_line_pointer == '"')
1656 ++input_line_pointer;
1658 elfsym->symbol.flags |= type;
1660 demand_empty_rest_of_line ();
1663 static void
1664 obj_elf_ident (ignore)
1665 int ignore ATTRIBUTE_UNUSED;
1667 static segT comment_section;
1668 segT old_section = now_seg;
1669 int old_subsection = now_subseg;
1671 #ifdef md_flush_pending_output
1672 md_flush_pending_output ();
1673 #endif
1675 if (!comment_section)
1677 char *p;
1678 comment_section = subseg_new (".comment", 0);
1679 bfd_set_section_flags (stdoutput, comment_section,
1680 SEC_READONLY | SEC_HAS_CONTENTS);
1681 p = frag_more (1);
1682 *p = 0;
1684 else
1685 subseg_set (comment_section, 0);
1686 stringer (1);
1687 subseg_set (old_section, old_subsection);
1690 #ifdef INIT_STAB_SECTION
1692 /* The first entry in a .stabs section is special. */
1694 void
1695 obj_elf_init_stab_section (seg)
1696 segT seg;
1698 char *file;
1699 char *p;
1700 char *stabstr_name;
1701 unsigned int stroff;
1703 /* Force the section to align to a longword boundary. Without this,
1704 UnixWare ar crashes. */
1705 bfd_set_section_alignment (stdoutput, seg, 2);
1707 /* Make space for this first symbol. */
1708 p = frag_more (12);
1709 /* Zero it out. */
1710 memset (p, 0, 12);
1711 as_where (&file, (unsigned int *) NULL);
1712 stabstr_name = (char *) xmalloc (strlen (segment_name (seg)) + 4);
1713 strcpy (stabstr_name, segment_name (seg));
1714 strcat (stabstr_name, "str");
1715 stroff = get_stab_string_offset (file, stabstr_name);
1716 know (stroff == 1);
1717 md_number_to_chars (p, stroff, 4);
1718 seg_info (seg)->stabu.p = p;
1721 #endif
1723 /* Fill in the counts in the first entry in a .stabs section. */
1725 static void
1726 adjust_stab_sections (abfd, sec, xxx)
1727 bfd *abfd;
1728 asection *sec;
1729 PTR xxx ATTRIBUTE_UNUSED;
1731 char *name;
1732 asection *strsec;
1733 char *p;
1734 int strsz, nsyms;
1736 if (strncmp (".stab", sec->name, 5))
1737 return;
1738 if (!strcmp ("str", sec->name + strlen (sec->name) - 3))
1739 return;
1741 name = (char *) alloca (strlen (sec->name) + 4);
1742 strcpy (name, sec->name);
1743 strcat (name, "str");
1744 strsec = bfd_get_section_by_name (abfd, name);
1745 if (strsec)
1746 strsz = bfd_section_size (abfd, strsec);
1747 else
1748 strsz = 0;
1749 nsyms = bfd_section_size (abfd, sec) / 12 - 1;
1751 p = seg_info (sec)->stabu.p;
1752 assert (p != 0);
1754 bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6);
1755 bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8);
1758 #ifdef NEED_ECOFF_DEBUG
1760 /* This function is called by the ECOFF code. It is supposed to
1761 record the external symbol information so that the backend can
1762 write it out correctly. The ELF backend doesn't actually handle
1763 this at the moment, so we do it ourselves. We save the information
1764 in the symbol. */
1766 void
1767 elf_ecoff_set_ext (sym, ext)
1768 symbolS *sym;
1769 struct ecoff_extr *ext;
1771 symbol_get_bfdsym (sym)->udata.p = (PTR) ext;
1774 /* This function is called by bfd_ecoff_debug_externals. It is
1775 supposed to *EXT to the external symbol information, and return
1776 whether the symbol should be used at all. */
1778 static bfd_boolean
1779 elf_get_extr (sym, ext)
1780 asymbol *sym;
1781 EXTR *ext;
1783 if (sym->udata.p == NULL)
1784 return FALSE;
1785 *ext = *(EXTR *) sym->udata.p;
1786 return TRUE;
1789 /* This function is called by bfd_ecoff_debug_externals. It has
1790 nothing to do for ELF. */
1792 /*ARGSUSED*/
1793 static void
1794 elf_set_index (sym, indx)
1795 asymbol *sym ATTRIBUTE_UNUSED;
1796 bfd_size_type indx ATTRIBUTE_UNUSED;
1800 #endif /* NEED_ECOFF_DEBUG */
1802 void
1803 elf_frob_symbol (symp, puntp)
1804 symbolS *symp;
1805 int *puntp;
1807 struct elf_obj_sy *sy_obj;
1809 #ifdef NEED_ECOFF_DEBUG
1810 if (ECOFF_DEBUGGING)
1811 ecoff_frob_symbol (symp);
1812 #endif
1814 sy_obj = symbol_get_obj (symp);
1816 if (sy_obj->size != NULL)
1818 switch (sy_obj->size->X_op)
1820 case O_subtract:
1821 S_SET_SIZE (symp,
1822 (S_GET_VALUE (sy_obj->size->X_add_symbol)
1823 + sy_obj->size->X_add_number
1824 - S_GET_VALUE (sy_obj->size->X_op_symbol)));
1825 break;
1826 case O_constant:
1827 S_SET_SIZE (symp,
1828 (S_GET_VALUE (sy_obj->size->X_add_symbol)
1829 + sy_obj->size->X_add_number));
1830 break;
1831 default:
1832 as_bad (_(".size expression too complicated to fix up"));
1833 break;
1835 free (sy_obj->size);
1836 sy_obj->size = NULL;
1839 if (sy_obj->versioned_name != NULL)
1841 char *p;
1843 p = strchr (sy_obj->versioned_name, ELF_VER_CHR);
1844 know (p != NULL);
1846 /* This symbol was given a new name with the .symver directive.
1848 If this is an external reference, just rename the symbol to
1849 include the version string. This will make the relocs be
1850 against the correct versioned symbol.
1852 If this is a definition, add an alias. FIXME: Using an alias
1853 will permit the debugging information to refer to the right
1854 symbol. However, it's not clear whether it is the best
1855 approach. */
1857 if (! S_IS_DEFINED (symp))
1859 /* Verify that the name isn't using the @@ syntax--this is
1860 reserved for definitions of the default version to link
1861 against. */
1862 if (p[1] == ELF_VER_CHR)
1864 as_bad (_("invalid attempt to declare external version name as default in symbol `%s'"),
1865 sy_obj->versioned_name);
1866 *puntp = TRUE;
1868 S_SET_NAME (symp, sy_obj->versioned_name);
1870 else
1872 if (p [1] == ELF_VER_CHR && p [2] == ELF_VER_CHR)
1874 size_t l;
1876 /* The @@@ syntax is a special case. It renames the
1877 symbol name to versioned_name with one `@' removed. */
1878 l = strlen (&p[3]) + 1;
1879 memmove (&p [2], &p[3], l);
1880 S_SET_NAME (symp, sy_obj->versioned_name);
1882 else
1884 symbolS *symp2;
1886 /* FIXME: Creating a new symbol here is risky. We're
1887 in the final loop over the symbol table. We can
1888 get away with it only because the symbol goes to
1889 the end of the list, where the loop will still see
1890 it. It would probably be better to do this in
1891 obj_frob_file_before_adjust. */
1893 symp2 = symbol_find_or_make (sy_obj->versioned_name);
1895 /* Now we act as though we saw symp2 = sym. */
1897 S_SET_SEGMENT (symp2, S_GET_SEGMENT (symp));
1899 /* Subtracting out the frag address here is a hack
1900 because we are in the middle of the final loop. */
1901 S_SET_VALUE (symp2,
1902 (S_GET_VALUE (symp)
1903 - symbol_get_frag (symp)->fr_address));
1905 symbol_set_frag (symp2, symbol_get_frag (symp));
1907 /* This will copy over the size information. */
1908 copy_symbol_attributes (symp2, symp);
1910 S_SET_OTHER (symp2, S_GET_OTHER (symp));
1912 if (S_IS_WEAK (symp))
1913 S_SET_WEAK (symp2);
1915 if (S_IS_EXTERNAL (symp))
1916 S_SET_EXTERNAL (symp2);
1921 /* Double check weak symbols. */
1922 if (S_IS_WEAK (symp))
1924 if (S_IS_COMMON (symp))
1925 as_bad (_("symbol `%s' can not be both weak and common"),
1926 S_GET_NAME (symp));
1929 #ifdef TC_MIPS
1930 /* The Irix 5 and 6 assemblers set the type of any common symbol and
1931 any undefined non-function symbol to STT_OBJECT. We try to be
1932 compatible, since newer Irix 5 and 6 linkers care. However, we
1933 only set undefined symbols to be STT_OBJECT if we are on Irix,
1934 because that is the only time gcc will generate the necessary
1935 .global directives to mark functions. */
1937 if (S_IS_COMMON (symp))
1938 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1940 if (strstr (TARGET_OS, "irix") != NULL
1941 && ! S_IS_DEFINED (symp)
1942 && (symbol_get_bfdsym (symp)->flags & BSF_FUNCTION) == 0)
1943 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1944 #endif
1946 #if 0 /* TC_PPC */
1947 /* If TC_PPC is defined, we used to force the type of a symbol to be
1948 BSF_OBJECT if it was otherwise unset. This was required by some
1949 version of VxWorks. Thomas de Lellis <tdel@windriver.com> says
1950 that this is no longer needed, so it is now commented out. */
1951 if ((symbol_get_bfdsym (symp)->flags
1952 & (BSF_FUNCTION | BSF_FILE | BSF_SECTION_SYM)) == 0
1953 && S_IS_DEFINED (symp))
1954 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1955 #endif
1958 struct group_list
1960 asection **head; /* Section lists. */
1961 unsigned int *elt_count; /* Number of sections in each list. */
1962 unsigned int num_group; /* Number of lists. */
1965 /* Called via bfd_map_over_sections. If SEC is a member of a group,
1966 add it to a list of sections belonging to the group. INF is a
1967 pointer to a struct group_list, which is where we store the head of
1968 each list. */
1970 static void
1971 build_group_lists (abfd, sec, inf)
1972 bfd *abfd ATTRIBUTE_UNUSED;
1973 asection *sec;
1974 PTR inf;
1976 struct group_list *list = (struct group_list *) inf;
1977 const char *group_name = elf_group_name (sec);
1978 unsigned int i;
1980 if (group_name == NULL)
1981 return;
1983 /* If this group already has a list, add the section to the head of
1984 the list. */
1985 for (i = 0; i < list->num_group; i++)
1987 if (strcmp (group_name, elf_group_name (list->head[i])) == 0)
1989 elf_next_in_group (sec) = list->head[i];
1990 list->head[i] = sec;
1991 list->elt_count[i] += 1;
1992 return;
1996 /* New group. Make the arrays bigger in chunks to minimize calls to
1997 realloc. */
1998 i = list->num_group;
1999 if ((i & 127) == 0)
2001 unsigned int newsize = i + 128;
2002 list->head = xrealloc (list->head, newsize * sizeof (*list->head));
2003 list->elt_count = xrealloc (list->elt_count,
2004 newsize * sizeof (*list->elt_count));
2006 list->head[i] = sec;
2007 list->elt_count[i] = 1;
2008 list->num_group += 1;
2011 void
2012 elf_frob_file ()
2014 struct group_list list;
2015 unsigned int i;
2017 bfd_map_over_sections (stdoutput, adjust_stab_sections, (PTR) 0);
2019 /* Go find section groups. */
2020 list.num_group = 0;
2021 list.head = NULL;
2022 list.elt_count = NULL;
2023 bfd_map_over_sections (stdoutput, build_group_lists, (PTR) &list);
2025 /* Make the SHT_GROUP sections that describe each section group. We
2026 can't set up the section contents here yet, because elf section
2027 indices have yet to be calculated. elf.c:set_group_contents does
2028 the rest of the work. */
2029 for (i = 0; i < list.num_group; i++)
2031 const char *group_name = elf_group_name (list.head[i]);
2032 const char *sec_name;
2033 asection *s;
2034 flagword flags;
2035 struct symbol *sy;
2036 int has_sym;
2038 flags = SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_GROUP;
2039 for (s = list.head[i]; s != NULL; s = elf_next_in_group (s))
2040 if (elf_linkonce_p (s) != ((flags & SEC_LINK_ONCE) != 0))
2042 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2043 if (s != list.head[i])
2045 as_warn (_("assuming all members of group `%s' are COMDAT"),
2046 group_name);
2047 break;
2051 sec_name = group_name;
2052 sy = symbol_find_exact (group_name);
2053 has_sym = 0;
2054 if (sy != NULL
2055 && (sy == symbol_lastP
2056 || (sy->sy_next != NULL
2057 && sy->sy_next->sy_previous == sy)))
2059 has_sym = 1;
2060 sec_name = ".group";
2062 s = subseg_force_new (sec_name, 0);
2063 if (s == NULL
2064 || !bfd_set_section_flags (stdoutput, s, flags)
2065 || !bfd_set_section_alignment (stdoutput, s, 2))
2067 as_fatal (_("can't create group: %s"),
2068 bfd_errmsg (bfd_get_error ()));
2071 /* Pass a pointer to the first section in this group. */
2072 elf_next_in_group (s) = list.head[i];
2073 if (has_sym)
2074 elf_group_id (s) = sy->bsym;
2076 s->_raw_size = 4 * (list.elt_count[i] + 1);
2077 s->contents = frag_more (s->_raw_size);
2078 frag_now->fr_fix = frag_now_fix_octets ();
2081 #ifdef elf_tc_final_processing
2082 elf_tc_final_processing ();
2083 #endif
2086 /* It removes any unneeded versioned symbols from the symbol table. */
2088 void
2089 elf_frob_file_before_adjust ()
2091 if (symbol_rootP)
2093 symbolS *symp;
2095 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2096 if (!S_IS_DEFINED (symp))
2098 if (symbol_get_obj (symp)->versioned_name)
2100 char *p;
2102 /* The @@@ syntax is a special case. If the symbol is
2103 not defined, 2 `@'s will be removed from the
2104 versioned_name. */
2106 p = strchr (symbol_get_obj (symp)->versioned_name,
2107 ELF_VER_CHR);
2108 know (p != NULL);
2109 if (p [1] == ELF_VER_CHR && p [2] == ELF_VER_CHR)
2111 size_t l = strlen (&p[3]) + 1;
2112 memmove (&p [1], &p[3], l);
2114 if (symbol_used_p (symp) == 0
2115 && symbol_used_in_reloc_p (symp) == 0)
2116 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2119 /* If there was .weak foo, but foo was neither defined nor
2120 used anywhere, remove it. */
2122 else if (S_IS_WEAK (symp)
2123 && symbol_used_p (symp) == 0
2124 && symbol_used_in_reloc_p (symp) == 0)
2125 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2130 /* It is required that we let write_relocs have the opportunity to
2131 optimize away fixups before output has begun, since it is possible
2132 to eliminate all fixups for a section and thus we never should
2133 have generated the relocation section. */
2135 void
2136 elf_frob_file_after_relocs ()
2138 #ifdef NEED_ECOFF_DEBUG
2139 if (ECOFF_DEBUGGING)
2140 /* Generate the ECOFF debugging information. */
2142 const struct ecoff_debug_swap *debug_swap;
2143 struct ecoff_debug_info debug;
2144 char *buf;
2145 asection *sec;
2147 debug_swap
2148 = get_elf_backend_data (stdoutput)->elf_backend_ecoff_debug_swap;
2149 know (debug_swap != (const struct ecoff_debug_swap *) NULL);
2150 ecoff_build_debug (&debug.symbolic_header, &buf, debug_swap);
2152 /* Set up the pointers in debug. */
2153 #define SET(ptr, offset, type) \
2154 debug.ptr = (type) (buf + debug.symbolic_header.offset)
2156 SET (line, cbLineOffset, unsigned char *);
2157 SET (external_dnr, cbDnOffset, PTR);
2158 SET (external_pdr, cbPdOffset, PTR);
2159 SET (external_sym, cbSymOffset, PTR);
2160 SET (external_opt, cbOptOffset, PTR);
2161 SET (external_aux, cbAuxOffset, union aux_ext *);
2162 SET (ss, cbSsOffset, char *);
2163 SET (external_fdr, cbFdOffset, PTR);
2164 SET (external_rfd, cbRfdOffset, PTR);
2165 /* ssext and external_ext are set up just below. */
2167 #undef SET
2169 /* Set up the external symbols. */
2170 debug.ssext = debug.ssext_end = NULL;
2171 debug.external_ext = debug.external_ext_end = NULL;
2172 if (! bfd_ecoff_debug_externals (stdoutput, &debug, debug_swap, TRUE,
2173 elf_get_extr, elf_set_index))
2174 as_fatal (_("failed to set up debugging information: %s"),
2175 bfd_errmsg (bfd_get_error ()));
2177 sec = bfd_get_section_by_name (stdoutput, ".mdebug");
2178 assert (sec != NULL);
2180 know (!stdoutput->output_has_begun);
2182 /* We set the size of the section, call bfd_set_section_contents
2183 to force the ELF backend to allocate a file position, and then
2184 write out the data. FIXME: Is this really the best way to do
2185 this? */
2186 sec->_raw_size = bfd_ecoff_debug_size (stdoutput, &debug, debug_swap);
2188 /* Pass BUF to bfd_set_section_contents because this will
2189 eventually become a call to fwrite, and ISO C prohibits
2190 passing a NULL pointer to a stdio function even if the
2191 pointer will not be used. */
2192 if (! bfd_set_section_contents (stdoutput, sec, (PTR) buf,
2193 (file_ptr) 0, (bfd_size_type) 0))
2194 as_fatal (_("can't start writing .mdebug section: %s"),
2195 bfd_errmsg (bfd_get_error ()));
2197 know (stdoutput->output_has_begun);
2198 know (sec->filepos != 0);
2200 if (! bfd_ecoff_write_debug (stdoutput, &debug, debug_swap,
2201 sec->filepos))
2202 as_fatal (_("could not write .mdebug section: %s"),
2203 bfd_errmsg (bfd_get_error ()));
2205 #endif /* NEED_ECOFF_DEBUG */
2208 #ifdef SCO_ELF
2210 /* Heavily plagarized from obj_elf_version. The idea is to emit the
2211 SCO specific identifier in the .notes section to satisfy the SCO
2212 linker.
2214 This looks more complicated than it really is. As opposed to the
2215 "obvious" solution, this should handle the cross dev cases
2216 correctly. (i.e, hosting on a 64 bit big endian processor, but
2217 generating SCO Elf code) Efficiency isn't a concern, as there
2218 should be exactly one of these sections per object module.
2220 SCO OpenServer 5 identifies it's ELF modules with a standard ELF
2221 .note section.
2223 int_32 namesz = 4 ; Name size
2224 int_32 descsz = 12 ; Descriptive information
2225 int_32 type = 1 ;
2226 char name[4] = "SCO" ; Originator name ALWAYS SCO + NULL
2227 int_32 version = (major ver # << 16) | version of tools ;
2228 int_32 source = (tool_id << 16 ) | 1 ;
2229 int_32 info = 0 ; These are set by the SCO tools, but we
2230 don't know enough about the source
2231 environment to set them. SCO ld currently
2232 ignores them, and recommends we set them
2233 to zero. */
2235 #define SCO_MAJOR_VERSION 0x1
2236 #define SCO_MINOR_VERSION 0x1
2238 void
2239 sco_id ()
2242 char *name;
2243 unsigned int c;
2244 char ch;
2245 char *p;
2246 asection *seg = now_seg;
2247 subsegT subseg = now_subseg;
2248 Elf_Internal_Note i_note;
2249 Elf_External_Note e_note;
2250 asection *note_secp = (asection *) NULL;
2251 int i, len;
2253 /* create the .note section */
2255 note_secp = subseg_new (".note", 0);
2256 bfd_set_section_flags (stdoutput,
2257 note_secp,
2258 SEC_HAS_CONTENTS | SEC_READONLY);
2260 /* process the version string */
2262 i_note.namesz = 4;
2263 i_note.descsz = 12; /* 12 descriptive bytes */
2264 i_note.type = NT_VERSION; /* Contains a version string */
2266 p = frag_more (sizeof (i_note.namesz));
2267 md_number_to_chars (p, (valueT) i_note.namesz, 4);
2269 p = frag_more (sizeof (i_note.descsz));
2270 md_number_to_chars (p, (valueT) i_note.descsz, 4);
2272 p = frag_more (sizeof (i_note.type));
2273 md_number_to_chars (p, (valueT) i_note.type, 4);
2275 p = frag_more (4);
2276 strcpy (p, "SCO");
2278 /* Note: this is the version number of the ELF we're representing */
2279 p = frag_more (4);
2280 md_number_to_chars (p, (SCO_MAJOR_VERSION << 16) | (SCO_MINOR_VERSION), 4);
2282 /* Here, we pick a magic number for ourselves (yes, I "registered"
2283 it with SCO. The bottom bit shows that we are compat with the
2284 SCO ABI. */
2285 p = frag_more (4);
2286 md_number_to_chars (p, 0x4c520000 | 0x0001, 4);
2288 /* If we knew (or cared) what the source language options were, we'd
2289 fill them in here. SCO has given us permission to ignore these
2290 and just set them to zero. */
2291 p = frag_more (4);
2292 md_number_to_chars (p, 0x0000, 4);
2294 frag_align (2, 0, 0);
2296 /* We probably can't restore the current segment, for there likely
2297 isn't one yet... */
2298 if (seg && subseg)
2299 subseg_set (seg, subseg);
2303 #endif /* SCO_ELF */
2305 static int
2306 elf_separate_stab_sections ()
2308 #ifdef NEED_ECOFF_DEBUG
2309 return (!ECOFF_DEBUGGING);
2310 #else
2311 return 1;
2312 #endif
2315 static void
2316 elf_init_stab_section (seg)
2317 segT seg;
2319 #ifdef NEED_ECOFF_DEBUG
2320 if (!ECOFF_DEBUGGING)
2321 #endif
2322 obj_elf_init_stab_section (seg);
2325 const struct format_ops elf_format_ops =
2327 bfd_target_elf_flavour,
2328 0, /* dfl_leading_underscore */
2329 1, /* emit_section_symbols */
2330 elf_begin,
2331 elf_file_symbol,
2332 elf_frob_symbol,
2333 elf_frob_file,
2334 elf_frob_file_before_adjust,
2335 0, /* obj_frob_file_before_fix */
2336 elf_frob_file_after_relocs,
2337 elf_s_get_size, elf_s_set_size,
2338 elf_s_get_align, elf_s_set_align,
2339 elf_s_get_other,
2340 elf_s_set_other,
2341 0, /* s_get_desc */
2342 0, /* s_set_desc */
2343 0, /* s_get_type */
2344 0, /* s_set_type */
2345 elf_copy_symbol_attributes,
2346 #ifdef NEED_ECOFF_DEBUG
2347 ecoff_generate_asm_lineno,
2348 ecoff_stab,
2349 #else
2350 0, /* generate_asm_lineno */
2351 0, /* process_stab */
2352 #endif
2353 elf_separate_stab_sections,
2354 elf_init_stab_section,
2355 elf_sec_sym_ok_for_reloc,
2356 elf_pop_insert,
2357 #ifdef NEED_ECOFF_DEBUG
2358 elf_ecoff_set_ext,
2359 #else
2360 0, /* ecoff_set_ext */
2361 #endif
2362 elf_obj_read_begin_hook,
2363 elf_obj_symbol_new_hook