gas -Z flag
[deliverable/binutils-gdb.git] / gas / config / obj-elf.c
... / ...
CommitLineData
1/* ELF object file format
2 Copyright (C) 1992-2020 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 3,
9 or (at your option) any later version.
10
11 GAS is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21#define OBJ_HEADER "obj-elf.h"
22#include "as.h"
23#include "safe-ctype.h"
24#include "subsegs.h"
25#include "obstack.h"
26#include "dwarf2dbg.h"
27
28#ifndef ECOFF_DEBUGGING
29#define ECOFF_DEBUGGING 0
30#else
31#define NEED_ECOFF_DEBUG
32#endif
33
34#ifdef NEED_ECOFF_DEBUG
35#include "ecoff.h"
36#include "bfd/ecoff-bfd.h"
37#endif
38
39#ifdef TC_ALPHA
40#include "elf/alpha.h"
41#endif
42
43#ifdef TC_MIPS
44#include "elf/mips.h"
45#endif
46
47#ifdef TC_PPC
48#include "elf/ppc.h"
49#endif
50
51#ifdef TC_I386
52#include "elf/x86-64.h"
53#endif
54
55#ifdef TC_MEP
56#include "elf/mep.h"
57#endif
58
59#ifdef TC_NIOS2
60#include "elf/nios2.h"
61#endif
62
63#ifdef TC_PRU
64#include "elf/pru.h"
65#endif
66
67static void obj_elf_line (int);
68static void obj_elf_size (int);
69static void obj_elf_type (int);
70static void obj_elf_ident (int);
71static void obj_elf_weak (int);
72static void obj_elf_local (int);
73static void obj_elf_visibility (int);
74static void obj_elf_symver (int);
75static void obj_elf_subsection (int);
76static void obj_elf_popsection (int);
77static void obj_elf_gnu_attribute (int);
78static void obj_elf_tls_common (int);
79static void obj_elf_lcomm (int);
80static void obj_elf_struct (int);
81
82static const pseudo_typeS elf_pseudo_table[] =
83{
84 {"comm", obj_elf_common, 0},
85 {"common", obj_elf_common, 1},
86 {"ident", obj_elf_ident, 0},
87 {"lcomm", obj_elf_lcomm, 0},
88 {"local", obj_elf_local, 0},
89 {"previous", obj_elf_previous, 0},
90 {"section", obj_elf_section, 0},
91 {"section.s", obj_elf_section, 0},
92 {"sect", obj_elf_section, 0},
93 {"sect.s", obj_elf_section, 0},
94 {"pushsection", obj_elf_section, 1},
95 {"popsection", obj_elf_popsection, 0},
96 {"size", obj_elf_size, 0},
97 {"type", obj_elf_type, 0},
98 {"version", obj_elf_version, 0},
99 {"weak", obj_elf_weak, 0},
100
101 /* These define symbol visibility. */
102 {"internal", obj_elf_visibility, STV_INTERNAL},
103 {"hidden", obj_elf_visibility, STV_HIDDEN},
104 {"protected", obj_elf_visibility, STV_PROTECTED},
105
106 /* These are used for stabs-in-elf configurations. */
107 {"line", obj_elf_line, 0},
108
109 /* This is a GNU extension to handle symbol versions. */
110 {"symver", obj_elf_symver, 0},
111
112 /* A GNU extension to change subsection only. */
113 {"subsection", obj_elf_subsection, 0},
114
115 /* These are GNU extensions to aid in garbage collecting C++ vtables. */
116 {"vtable_inherit", obj_elf_vtable_inherit, 0},
117 {"vtable_entry", obj_elf_vtable_entry, 0},
118
119 /* A GNU extension for object attributes. */
120 {"gnu_attribute", obj_elf_gnu_attribute, 0},
121
122 /* These are used for dwarf. */
123 {"2byte", cons, 2},
124 {"4byte", cons, 4},
125 {"8byte", cons, 8},
126 /* These are used for dwarf2. */
127 { "file", dwarf2_directive_file, 0 },
128 { "loc", dwarf2_directive_loc, 0 },
129 { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
130
131 /* We need to trap the section changing calls to handle .previous. */
132 {"data", obj_elf_data, 0},
133 {"offset", obj_elf_struct, 0},
134 {"struct", obj_elf_struct, 0},
135 {"text", obj_elf_text, 0},
136
137 {"tls_common", obj_elf_tls_common, 0},
138
139 /* End sentinel. */
140 {NULL, NULL, 0},
141};
142
143static const pseudo_typeS ecoff_debug_pseudo_table[] =
144{
145#ifdef NEED_ECOFF_DEBUG
146 /* COFF style debugging information for ECOFF. .ln is not used; .loc
147 is used instead. */
148 { "def", ecoff_directive_def, 0 },
149 { "dim", ecoff_directive_dim, 0 },
150 { "endef", ecoff_directive_endef, 0 },
151 { "file", ecoff_directive_file, 0 },
152 { "scl", ecoff_directive_scl, 0 },
153 { "tag", ecoff_directive_tag, 0 },
154 { "val", ecoff_directive_val, 0 },
155
156 /* COFF debugging requires pseudo-ops .size and .type, but ELF
157 already has meanings for those. We use .esize and .etype
158 instead. These are only generated by gcc anyhow. */
159 { "esize", ecoff_directive_size, 0 },
160 { "etype", ecoff_directive_type, 0 },
161
162 /* ECOFF specific debugging information. */
163 { "aent", ecoff_directive_ent, 1 },
164 { "begin", ecoff_directive_begin, 0 },
165 { "bend", ecoff_directive_bend, 0 },
166 { "end", ecoff_directive_end, 0 },
167 { "ent", ecoff_directive_ent, 0 },
168 { "fmask", ecoff_directive_fmask, 0 },
169 { "frame", ecoff_directive_frame, 0 },
170 { "loc", ecoff_directive_loc, 0 },
171 { "mask", ecoff_directive_mask, 0 },
172
173 /* Other ECOFF directives. */
174 { "extern", ecoff_directive_extern, 0 },
175
176 /* These are used on Irix. I don't know how to implement them. */
177 { "alias", s_ignore, 0 },
178 { "bgnb", s_ignore, 0 },
179 { "endb", s_ignore, 0 },
180 { "lab", s_ignore, 0 },
181 { "noalias", s_ignore, 0 },
182 { "verstamp", s_ignore, 0 },
183 { "vreg", s_ignore, 0 },
184#endif
185
186 {NULL, NULL, 0} /* end sentinel */
187};
188
189#undef NO_RELOC
190#include "aout/aout64.h"
191
192/* This is called when the assembler starts. */
193
194asection *elf_com_section_ptr;
195
196void
197elf_begin (void)
198{
199 asection *s;
200
201 /* Add symbols for the known sections to the symbol table. */
202 s = bfd_get_section_by_name (stdoutput, TEXT_SECTION_NAME);
203 symbol_table_insert (section_symbol (s));
204 s = bfd_get_section_by_name (stdoutput, DATA_SECTION_NAME);
205 symbol_table_insert (section_symbol (s));
206 s = bfd_get_section_by_name (stdoutput, BSS_SECTION_NAME);
207 symbol_table_insert (section_symbol (s));
208 elf_com_section_ptr = bfd_com_section_ptr;
209}
210
211void
212elf_pop_insert (void)
213{
214 pop_insert (elf_pseudo_table);
215 if (ECOFF_DEBUGGING)
216 pop_insert (ecoff_debug_pseudo_table);
217}
218
219static bfd_vma
220elf_s_get_size (symbolS *sym)
221{
222 return S_GET_SIZE (sym);
223}
224
225static void
226elf_s_set_size (symbolS *sym, bfd_vma sz)
227{
228 S_SET_SIZE (sym, sz);
229}
230
231static bfd_vma
232elf_s_get_align (symbolS *sym)
233{
234 return S_GET_ALIGN (sym);
235}
236
237static void
238elf_s_set_align (symbolS *sym, bfd_vma align)
239{
240 S_SET_ALIGN (sym, align);
241}
242
243int
244elf_s_get_other (symbolS *sym)
245{
246 return elf_symbol (symbol_get_bfdsym (sym))->internal_elf_sym.st_other;
247}
248
249static void
250elf_s_set_other (symbolS *sym, int other)
251{
252 S_SET_OTHER (sym, other);
253}
254
255static int
256elf_sec_sym_ok_for_reloc (asection *sec)
257{
258 return obj_sec_sym_ok_for_reloc (sec);
259}
260
261void
262elf_file_symbol (const char *s, int appfile)
263{
264 asymbol *bsym;
265
266 if (!appfile
267 || symbol_rootP == NULL
268 || (bsym = symbol_get_bfdsym (symbol_rootP)) == NULL
269 || (bsym->flags & BSF_FILE) == 0)
270 {
271 symbolS *sym;
272 size_t name_length;
273
274 sym = symbol_new (s, absolute_section, 0, NULL);
275 symbol_set_frag (sym, &zero_address_frag);
276
277 name_length = strlen (s);
278 if (name_length > strlen (S_GET_NAME (sym)))
279 {
280 obstack_grow (&notes, s, name_length + 1);
281 S_SET_NAME (sym, (const char *) obstack_finish (&notes));
282 }
283 else
284 strcpy ((char *) S_GET_NAME (sym), s);
285
286 symbol_get_bfdsym (sym)->flags |= BSF_FILE;
287
288 if (symbol_rootP != sym
289 && ((bsym = symbol_get_bfdsym (symbol_rootP)) == NULL
290 || (bsym->flags & BSF_FILE) == 0))
291 {
292 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
293 symbol_insert (sym, symbol_rootP, &symbol_rootP, &symbol_lastP);
294 }
295
296#ifdef DEBUG
297 verify_symbol_chain (symbol_rootP, symbol_lastP);
298#endif
299 }
300
301#ifdef NEED_ECOFF_DEBUG
302 ecoff_new_file (s, appfile);
303#endif
304}
305
306/* Called from read.c:s_comm after we've parsed .comm symbol, size.
307 Parse a possible alignment value. */
308
309symbolS *
310elf_common_parse (int ignore ATTRIBUTE_UNUSED, symbolS *symbolP, addressT size)
311{
312 addressT align = 0;
313 int is_local = symbol_get_obj (symbolP)->local;
314
315 if (*input_line_pointer == ',')
316 {
317 char *save = input_line_pointer;
318
319 input_line_pointer++;
320 SKIP_WHITESPACE ();
321
322 if (*input_line_pointer == '"')
323 {
324 /* For sparc. Accept .common symbol, length, "bss" */
325 input_line_pointer++;
326 /* Some use the dot, some don't. */
327 if (*input_line_pointer == '.')
328 input_line_pointer++;
329 /* Some say data, some say bss. */
330 if (strncmp (input_line_pointer, "bss\"", 4) == 0)
331 input_line_pointer += 4;
332 else if (strncmp (input_line_pointer, "data\"", 5) == 0)
333 input_line_pointer += 5;
334 else
335 {
336 char *p = input_line_pointer;
337 char c;
338
339 while (*--p != '"')
340 ;
341 while (!is_end_of_line[(unsigned char) *input_line_pointer])
342 if (*input_line_pointer++ == '"')
343 break;
344 c = *input_line_pointer;
345 *input_line_pointer = '\0';
346 as_bad (_("bad .common segment %s"), p);
347 *input_line_pointer = c;
348 ignore_rest_of_line ();
349 return NULL;
350 }
351 /* ??? Don't ask me why these are always global. */
352 is_local = 0;
353 }
354 else
355 {
356 input_line_pointer = save;
357 align = parse_align (is_local);
358 if (align == (addressT) -1)
359 return NULL;
360 }
361 }
362
363 if (is_local)
364 {
365 bss_alloc (symbolP, size, align);
366 S_CLEAR_EXTERNAL (symbolP);
367 }
368 else
369 {
370 S_SET_VALUE (symbolP, size);
371 S_SET_ALIGN (symbolP, align);
372 S_SET_EXTERNAL (symbolP);
373 S_SET_SEGMENT (symbolP, elf_com_section_ptr);
374 }
375
376 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
377
378 return symbolP;
379}
380
381void
382obj_elf_common (int is_common)
383{
384 if (flag_mri && is_common)
385 s_mri_common (0);
386 else
387 s_comm_internal (0, elf_common_parse);
388}
389
390static void
391obj_elf_tls_common (int ignore ATTRIBUTE_UNUSED)
392{
393 symbolS *symbolP = s_comm_internal (0, elf_common_parse);
394
395 if (symbolP)
396 symbol_get_bfdsym (symbolP)->flags |= BSF_THREAD_LOCAL;
397}
398
399static void
400obj_elf_lcomm (int ignore ATTRIBUTE_UNUSED)
401{
402 symbolS *symbolP = s_comm_internal (0, s_lcomm_internal);
403
404 if (symbolP)
405 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
406}
407
408static symbolS *
409get_sym_from_input_line_and_check (void)
410{
411 char *name;
412 char c;
413 symbolS *sym;
414
415 c = get_symbol_name (& name);
416 sym = symbol_find_or_make (name);
417 *input_line_pointer = c;
418 SKIP_WHITESPACE_AFTER_NAME ();
419
420 /* There is no symbol name if input_line_pointer has not moved. */
421 if (name == input_line_pointer)
422 as_bad (_("Missing symbol name in directive"));
423 return sym;
424}
425
426static void
427obj_elf_local (int ignore ATTRIBUTE_UNUSED)
428{
429 int c;
430 symbolS *symbolP;
431
432 do
433 {
434 symbolP = get_sym_from_input_line_and_check ();
435 c = *input_line_pointer;
436 S_CLEAR_EXTERNAL (symbolP);
437 symbol_get_obj (symbolP)->local = 1;
438 if (c == ',')
439 {
440 input_line_pointer++;
441 SKIP_WHITESPACE ();
442 if (*input_line_pointer == '\n')
443 c = '\n';
444 }
445 }
446 while (c == ',');
447 demand_empty_rest_of_line ();
448}
449
450static void
451obj_elf_weak (int ignore ATTRIBUTE_UNUSED)
452{
453 int c;
454 symbolS *symbolP;
455
456 do
457 {
458 symbolP = get_sym_from_input_line_and_check ();
459 c = *input_line_pointer;
460 S_SET_WEAK (symbolP);
461 if (c == ',')
462 {
463 input_line_pointer++;
464 SKIP_WHITESPACE ();
465 if (*input_line_pointer == '\n')
466 c = '\n';
467 }
468 }
469 while (c == ',');
470 demand_empty_rest_of_line ();
471}
472
473static void
474obj_elf_visibility (int visibility)
475{
476 int c;
477 symbolS *symbolP;
478 asymbol *bfdsym;
479 elf_symbol_type *elfsym;
480
481 do
482 {
483 symbolP = get_sym_from_input_line_and_check ();
484
485 bfdsym = symbol_get_bfdsym (symbolP);
486 elfsym = elf_symbol_from (bfd_asymbol_bfd (bfdsym), bfdsym);
487
488 gas_assert (elfsym);
489
490 elfsym->internal_elf_sym.st_other &= ~3;
491 elfsym->internal_elf_sym.st_other |= visibility;
492
493 c = *input_line_pointer;
494 if (c == ',')
495 {
496 input_line_pointer ++;
497
498 SKIP_WHITESPACE ();
499
500 if (*input_line_pointer == '\n')
501 c = '\n';
502 }
503 }
504 while (c == ',');
505
506 demand_empty_rest_of_line ();
507}
508
509static segT previous_section;
510static int previous_subsection;
511
512struct section_stack
513{
514 struct section_stack *next;
515 segT seg, prev_seg;
516 int subseg, prev_subseg;
517};
518
519static struct section_stack *section_stack;
520
521static bfd_boolean
522get_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
523{
524 struct elf_section_match *match = (struct elf_section_match *) inf;
525 const char *gname = match->group_name;
526 const char *group_name = elf_group_name (sec);
527 const char *linked_to_symbol_name
528 = sec->map_head.linked_to_symbol_name;
529 unsigned int info = elf_section_data (sec)->this_hdr.sh_info;
530
531 return (info == match->info
532 && ((bfd_section_flags (sec) & SEC_ASSEMBLER_SECTION_ID)
533 == (match->flags & SEC_ASSEMBLER_SECTION_ID))
534 && sec->section_id == match->section_id
535 && (group_name == gname
536 || (group_name != NULL
537 && gname != NULL
538 && strcmp (group_name, gname) == 0))
539 && (linked_to_symbol_name == match->linked_to_symbol_name
540 || (linked_to_symbol_name != NULL
541 && match->linked_to_symbol_name != NULL
542 && strcmp (linked_to_symbol_name,
543 match->linked_to_symbol_name) == 0)));
544}
545
546/* Handle the .section pseudo-op. This code supports two different
547 syntaxes.
548
549 The first is found on Solaris, and looks like
550 .section ".sec1",#alloc,#execinstr,#write
551 Here the names after '#' are the SHF_* flags to turn on for the
552 section. I'm not sure how it determines the SHT_* type (BFD
553 doesn't really give us control over the type, anyhow).
554
555 The second format is found on UnixWare, and probably most SVR4
556 machines, and looks like
557 .section .sec1,"a",@progbits
558 The quoted string may contain any combination of a, w, x, and
559 represents the SHF_* flags to turn on for the section. The string
560 beginning with '@' can be progbits or nobits. There should be
561 other possibilities, but I don't know what they are. In any case,
562 BFD doesn't really let us set the section type. */
563
564void
565obj_elf_change_section (const char *name,
566 unsigned int type,
567 bfd_vma attr,
568 int entsize,
569 struct elf_section_match *match_p,
570 int linkonce,
571 int push)
572{
573 asection *old_sec;
574 segT sec;
575 flagword flags;
576 const struct elf_backend_data *bed;
577 const struct bfd_elf_special_section *ssect;
578
579 if (match_p == NULL)
580 {
581 static struct elf_section_match unused_match;
582 match_p = &unused_match;
583 }
584
585#ifdef md_flush_pending_output
586 md_flush_pending_output ();
587#endif
588
589 /* Switch to the section, creating it if necessary. */
590 if (push)
591 {
592 struct section_stack *elt;
593 elt = XNEW (struct section_stack);
594 elt->next = section_stack;
595 elt->seg = now_seg;
596 elt->prev_seg = previous_section;
597 elt->subseg = now_subseg;
598 elt->prev_subseg = previous_subsection;
599 section_stack = elt;
600 }
601 previous_section = now_seg;
602 previous_subsection = now_subseg;
603
604 old_sec = bfd_get_section_by_name_if (stdoutput, name, get_section,
605 (void *) match_p);
606 if (old_sec)
607 {
608 sec = old_sec;
609 subseg_set (sec, 0);
610 }
611 else
612 sec = subseg_force_new (name, 0);
613
614 bed = get_elf_backend_data (stdoutput);
615 ssect = (*bed->get_sec_type_attr) (stdoutput, sec);
616
617 if (ssect != NULL)
618 {
619 bfd_boolean override = FALSE;
620
621 if (type == SHT_NULL)
622 type = ssect->type;
623 else if (type != ssect->type)
624 {
625 if (old_sec == NULL
626 /* Some older versions of gcc will emit
627
628 .section .init_array,"aw",@progbits
629
630 for __attribute__ ((section (".init_array"))).
631 "@progbits" is incorrect. Also for x86-64 large bss
632 sections, some older versions of gcc will emit
633
634 .section .lbss,"aw",@progbits
635
636 "@progbits" is incorrect. */
637#ifdef TC_I386
638 && (bed->s->arch_size != 64
639 || !(ssect->attr & SHF_X86_64_LARGE))
640#endif
641 && ssect->type != SHT_INIT_ARRAY
642 && ssect->type != SHT_FINI_ARRAY
643 && ssect->type != SHT_PREINIT_ARRAY)
644 {
645 /* We allow to specify any type for a .note section. */
646 if (ssect->type != SHT_NOTE
647 /* Processor and application defined types are allowed too. */
648 && type < SHT_LOPROC)
649 as_warn (_("setting incorrect section type for %s"),
650 name);
651 }
652 else
653 {
654 as_warn (_("ignoring incorrect section type for %s"),
655 name);
656 type = ssect->type;
657 }
658 }
659
660 if (old_sec == NULL && ((attr & ~(SHF_MASKOS | SHF_MASKPROC))
661 & ~ssect->attr) != 0)
662 {
663 /* As a GNU extension, we permit a .note section to be
664 allocatable. If the linker sees an allocatable .note
665 section, it will create a PT_NOTE segment in the output
666 file. We also allow "x" for .note.GNU-stack. */
667 if (ssect->type == SHT_NOTE
668 && (attr == SHF_ALLOC || attr == SHF_EXECINSTR))
669 ;
670 /* Allow different SHF_MERGE and SHF_STRINGS if we have
671 something like .rodata.str. */
672 else if (ssect->suffix_length == -2
673 && name[ssect->prefix_length] == '.'
674 && (attr
675 & ~ssect->attr
676 & ~SHF_MERGE
677 & ~SHF_STRINGS) == 0)
678 ;
679 /* .interp, .strtab and .symtab can have SHF_ALLOC. */
680 else if (attr == SHF_ALLOC
681 && (strcmp (name, ".interp") == 0
682 || strcmp (name, ".strtab") == 0
683 || strcmp (name, ".symtab") == 0))
684 override = TRUE;
685 /* .note.GNU-stack can have SHF_EXECINSTR. */
686 else if (attr == SHF_EXECINSTR
687 && strcmp (name, ".note.GNU-stack") == 0)
688 override = TRUE;
689#ifdef TC_ALPHA
690 /* A section on Alpha may have SHF_ALPHA_GPREL. */
691 else if ((attr & ~ssect->attr) == SHF_ALPHA_GPREL)
692 override = TRUE;
693#endif
694#ifdef TC_RX
695 else if (attr == (SHF_EXECINSTR | SHF_WRITE | SHF_ALLOC)
696 && (ssect->type == SHT_INIT_ARRAY
697 || ssect->type == SHT_FINI_ARRAY
698 || ssect->type == SHT_PREINIT_ARRAY))
699 /* RX init/fini arrays can and should have the "awx" attributes set. */
700 ;
701#endif
702 else
703 {
704 if (match_p->group_name == NULL)
705 as_warn (_("setting incorrect section attributes for %s"),
706 name);
707 override = TRUE;
708 }
709 }
710
711 if (!override && old_sec == NULL)
712 attr |= ssect->attr;
713 }
714
715 /* Convert ELF type and flags to BFD flags. */
716 flags = (SEC_RELOC
717 | ((attr & SHF_WRITE) ? 0 : SEC_READONLY)
718 | ((attr & SHF_ALLOC) ? SEC_ALLOC : 0)
719 | (((attr & SHF_ALLOC) && type != SHT_NOBITS) ? SEC_LOAD : 0)
720 | ((attr & SHF_EXECINSTR) ? SEC_CODE : 0)
721 | ((attr & SHF_MERGE) ? SEC_MERGE : 0)
722 | ((attr & SHF_STRINGS) ? SEC_STRINGS : 0)
723 | ((attr & SHF_EXCLUDE) ? SEC_EXCLUDE: 0)
724 | ((attr & SHF_TLS) ? SEC_THREAD_LOCAL : 0));
725#ifdef md_elf_section_flags
726 flags = md_elf_section_flags (flags, attr, type);
727#endif
728
729 if (linkonce)
730 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
731
732 if (old_sec == NULL)
733 {
734 symbolS *secsym;
735
736 if (type == SHT_NULL)
737 type = bfd_elf_get_default_section_type (flags);
738 elf_section_type (sec) = type;
739 elf_section_flags (sec) = attr;
740 elf_section_data (sec)->this_hdr.sh_info = match_p->info;
741
742 /* Prevent SEC_HAS_CONTENTS from being inadvertently set. */
743 if (type == SHT_NOBITS)
744 seg_info (sec)->bss = 1;
745
746 /* Set the section ID and flags. */
747 sec->section_id = match_p->section_id;
748 flags |= match_p->flags;
749
750 /* Set the linked-to symbol name. */
751 sec->map_head.linked_to_symbol_name
752 = match_p->linked_to_symbol_name;
753
754 bfd_set_section_flags (sec, flags);
755 if (flags & SEC_MERGE)
756 sec->entsize = entsize;
757 elf_group_name (sec) = match_p->group_name;
758
759 /* Add a symbol for this section to the symbol table. */
760 secsym = symbol_find (name);
761 if (secsym != NULL)
762 symbol_set_bfdsym (secsym, sec->symbol);
763 else
764 symbol_table_insert (section_symbol (sec));
765 }
766 else
767 {
768 if (type != SHT_NULL
769 && (unsigned) type != elf_section_type (old_sec))
770 as_warn (_("ignoring changed section type for %s"), name);
771
772 if (attr != 0)
773 {
774 /* If section attributes are specified the second time we see a
775 particular section, then check that they are the same as we
776 saw the first time. */
777 if (((old_sec->flags ^ flags)
778 & (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
779 | SEC_EXCLUDE | SEC_SORT_ENTRIES | SEC_MERGE | SEC_STRINGS
780 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
781 | SEC_THREAD_LOCAL)))
782 as_warn (_("ignoring changed section attributes for %s"), name);
783 else
784 /* FIXME: Maybe we should consider removing a previously set
785 processor or application specific attribute as suspicious ? */
786 elf_section_flags (sec) = attr;
787
788 if ((flags & SEC_MERGE) && old_sec->entsize != (unsigned) entsize)
789 as_warn (_("ignoring changed section entity size for %s"), name);
790 }
791 }
792
793#ifdef md_elf_section_change_hook
794 md_elf_section_change_hook ();
795#endif
796}
797
798static bfd_vma
799obj_elf_parse_section_letters (char *str, size_t len,
800 bfd_boolean *is_clone, bfd_vma *gnu_attr)
801{
802 bfd_vma attr = 0;
803 *is_clone = FALSE;
804
805 while (len > 0)
806 {
807 switch (*str)
808 {
809 case 'a':
810 attr |= SHF_ALLOC;
811 break;
812 case 'e':
813 attr |= SHF_EXCLUDE;
814 break;
815 case 'o':
816 attr |= SHF_LINK_ORDER;
817 break;
818 case 'w':
819 attr |= SHF_WRITE;
820 break;
821 case 'x':
822 attr |= SHF_EXECINSTR;
823 break;
824 case 'M':
825 attr |= SHF_MERGE;
826 break;
827 case 'S':
828 attr |= SHF_STRINGS;
829 break;
830 case 'G':
831 attr |= SHF_GROUP;
832 break;
833 case 'T':
834 attr |= SHF_TLS;
835 break;
836 case 'd':
837 *gnu_attr |= SHF_GNU_MBIND;
838 break;
839 case '?':
840 *is_clone = TRUE;
841 break;
842 /* Compatibility. */
843 case 'm':
844 if (*(str - 1) == 'a')
845 {
846 attr |= SHF_MERGE;
847 if (len > 1 && str[1] == 's')
848 {
849 attr |= SHF_STRINGS;
850 str++, len--;
851 }
852 break;
853 }
854 /* Fall through. */
855 default:
856 {
857 const char *bad_msg = _("unrecognized .section attribute:"
858 " want a,e,o,w,x,M,S,G,T or number");
859#ifdef md_elf_section_letter
860 bfd_vma md_attr = md_elf_section_letter (*str, &bad_msg);
861 if (md_attr != (bfd_vma) -1)
862 attr |= md_attr;
863 else
864#endif
865 if (ISDIGIT (*str))
866 {
867 char * end;
868
869 attr |= strtoul (str, & end, 0);
870 /* Update str and len, allowing for the fact that
871 we will execute str++ and len-- below. */
872 end --;
873 len -= (end - str);
874 str = end;
875 }
876 else
877 as_fatal ("%s", bad_msg);
878 }
879 break;
880 }
881 str++, len--;
882 }
883
884 return attr;
885}
886
887static int
888obj_elf_section_type (char *str, size_t len, bfd_boolean warn)
889{
890 if (len == 8 && strncmp (str, "progbits", 8) == 0)
891 return SHT_PROGBITS;
892 if (len == 6 && strncmp (str, "nobits", 6) == 0)
893 return SHT_NOBITS;
894 if (len == 4 && strncmp (str, "note", 4) == 0)
895 return SHT_NOTE;
896 if (len == 10 && strncmp (str, "init_array", 10) == 0)
897 return SHT_INIT_ARRAY;
898 if (len == 10 && strncmp (str, "fini_array", 10) == 0)
899 return SHT_FINI_ARRAY;
900 if (len == 13 && strncmp (str, "preinit_array", 13) == 0)
901 return SHT_PREINIT_ARRAY;
902
903#ifdef md_elf_section_type
904 {
905 int md_type = md_elf_section_type (str, len);
906 if (md_type >= 0)
907 return md_type;
908 }
909#endif
910
911 if (ISDIGIT (*str))
912 {
913 char * end;
914 int type = strtoul (str, & end, 0);
915
916 if (warn && (size_t) (end - str) != len)
917 as_warn (_("extraneous characters at end of numeric section type"));
918
919 return type;
920 }
921
922 if (warn)
923 as_warn (_("unrecognized section type"));
924 return 0;
925}
926
927static bfd_vma
928obj_elf_section_word (char *str, size_t len, int *type)
929{
930 int ret;
931
932 if (len == 5 && strncmp (str, "write", 5) == 0)
933 return SHF_WRITE;
934 if (len == 5 && strncmp (str, "alloc", 5) == 0)
935 return SHF_ALLOC;
936 if (len == 9 && strncmp (str, "execinstr", 9) == 0)
937 return SHF_EXECINSTR;
938 if (len == 7 && strncmp (str, "exclude", 7) == 0)
939 return SHF_EXCLUDE;
940 if (len == 3 && strncmp (str, "tls", 3) == 0)
941 return SHF_TLS;
942
943#ifdef md_elf_section_word
944 {
945 bfd_vma md_attr = md_elf_section_word (str, len);
946 if (md_attr > 0)
947 return md_attr;
948 }
949#endif
950
951 ret = obj_elf_section_type (str, len, FALSE);
952 if (ret != 0)
953 *type = ret;
954 else
955 as_warn (_("unrecognized section attribute"));
956
957 return 0;
958}
959
960/* Get name of section. */
961const char *
962obj_elf_section_name (void)
963{
964 char *name;
965
966 SKIP_WHITESPACE ();
967 if (*input_line_pointer == '"')
968 {
969 int dummy;
970
971 name = demand_copy_C_string (&dummy);
972 if (name == NULL)
973 {
974 ignore_rest_of_line ();
975 return NULL;
976 }
977 }
978 else
979 {
980 char *end = input_line_pointer;
981
982 while (0 == strchr ("\n\t,; ", *end))
983 end++;
984 if (end == input_line_pointer)
985 {
986 as_bad (_("missing name"));
987 ignore_rest_of_line ();
988 return NULL;
989 }
990
991 name = xmemdup0 (input_line_pointer, end - input_line_pointer);
992
993 while (flag_sectname_subst)
994 {
995 char *subst = strchr (name, '%');
996 if (subst && subst[1] == 'S')
997 {
998 int oldlen = strlen (name);
999 int substlen = strlen (now_seg->name);
1000 int newlen = oldlen - 2 + substlen;
1001 char *newname = XNEWVEC (char, newlen + 1);
1002 int headlen = subst - name;
1003 memcpy (newname, name, headlen);
1004 strcpy (newname + headlen, now_seg->name);
1005 strcat (newname + headlen, subst + 2);
1006 xfree (name);
1007 name = newname;
1008 }
1009 else
1010 break;
1011 }
1012
1013#ifdef tc_canonicalize_section_name
1014 name = tc_canonicalize_section_name (name);
1015#endif
1016 input_line_pointer = end;
1017 }
1018 SKIP_WHITESPACE ();
1019 return name;
1020}
1021
1022void
1023obj_elf_section (int push)
1024{
1025 const char *name;
1026 char *beg;
1027 int type, dummy;
1028 bfd_vma attr;
1029 bfd_vma gnu_attr;
1030 int entsize;
1031 int linkonce;
1032 subsegT new_subsection = -1;
1033 struct elf_section_match match;
1034
1035 if (flag_mri)
1036 {
1037 char mri_type;
1038
1039#ifdef md_flush_pending_output
1040 md_flush_pending_output ();
1041#endif
1042
1043 previous_section = now_seg;
1044 previous_subsection = now_subseg;
1045
1046 s_mri_sect (&mri_type);
1047
1048#ifdef md_elf_section_change_hook
1049 md_elf_section_change_hook ();
1050#endif
1051
1052 return;
1053 }
1054
1055 name = obj_elf_section_name ();
1056 if (name == NULL)
1057 return;
1058
1059 memset (&match, 0, sizeof (match));
1060
1061 symbolS * sym;
1062 if ((sym = symbol_find (name)) != NULL
1063 && ! symbol_section_p (sym)
1064 && S_IS_DEFINED (sym)
1065 && ! S_IS_VOLATILE (sym)
1066 && ! S_CAN_BE_REDEFINED (sym))
1067 {
1068 as_bad (_("section name '%s' already defined as another symbol"), name);
1069 ignore_rest_of_line ();
1070 return;
1071 }
1072 type = SHT_NULL;
1073 attr = 0;
1074 gnu_attr = 0;
1075 entsize = 0;
1076 linkonce = 0;
1077
1078 if (*input_line_pointer == ',')
1079 {
1080 /* Skip the comma. */
1081 ++input_line_pointer;
1082 SKIP_WHITESPACE ();
1083
1084 if (push && ISDIGIT (*input_line_pointer))
1085 {
1086 /* .pushsection has an optional subsection. */
1087 new_subsection = (subsegT) get_absolute_expression ();
1088
1089 SKIP_WHITESPACE ();
1090
1091 /* Stop if we don't see a comma. */
1092 if (*input_line_pointer != ',')
1093 goto done;
1094
1095 /* Skip the comma. */
1096 ++input_line_pointer;
1097 SKIP_WHITESPACE ();
1098 }
1099
1100 if (*input_line_pointer == '"')
1101 {
1102 bfd_boolean is_clone;
1103
1104 beg = demand_copy_C_string (&dummy);
1105 if (beg == NULL)
1106 {
1107 ignore_rest_of_line ();
1108 return;
1109 }
1110 attr |= obj_elf_parse_section_letters (beg, strlen (beg),
1111 &is_clone, &gnu_attr);
1112
1113 SKIP_WHITESPACE ();
1114 if (*input_line_pointer == ',')
1115 {
1116 char c;
1117 char *save = input_line_pointer;
1118
1119 ++input_line_pointer;
1120 SKIP_WHITESPACE ();
1121 c = *input_line_pointer;
1122 if (c == '"')
1123 {
1124 beg = demand_copy_C_string (&dummy);
1125 if (beg == NULL)
1126 {
1127 ignore_rest_of_line ();
1128 return;
1129 }
1130 type = obj_elf_section_type (beg, strlen (beg), TRUE);
1131 }
1132 else if (c == '@' || c == '%')
1133 {
1134 ++input_line_pointer;
1135
1136 if (ISDIGIT (* input_line_pointer))
1137 type = strtoul (input_line_pointer, &input_line_pointer, 0);
1138 else
1139 {
1140 c = get_symbol_name (& beg);
1141 (void) restore_line_pointer (c);
1142 type = obj_elf_section_type (beg,
1143 input_line_pointer - beg,
1144 TRUE);
1145 }
1146 }
1147 else
1148 input_line_pointer = save;
1149 }
1150
1151 SKIP_WHITESPACE ();
1152 if ((attr & SHF_MERGE) != 0 && *input_line_pointer == ',')
1153 {
1154 ++input_line_pointer;
1155 SKIP_WHITESPACE ();
1156 entsize = get_absolute_expression ();
1157 SKIP_WHITESPACE ();
1158 if (entsize < 0)
1159 {
1160 as_warn (_("invalid merge entity size"));
1161 attr &= ~SHF_MERGE;
1162 entsize = 0;
1163 }
1164 }
1165 else if ((attr & SHF_MERGE) != 0)
1166 {
1167 as_warn (_("entity size for SHF_MERGE not specified"));
1168 attr &= ~SHF_MERGE;
1169 }
1170
1171 if ((attr & SHF_LINK_ORDER) != 0 && *input_line_pointer == ',')
1172 {
1173 char c;
1174 unsigned int length;
1175 ++input_line_pointer;
1176 SKIP_WHITESPACE ();
1177 c = get_symbol_name (& beg);
1178 (void) restore_line_pointer (c);
1179 length = input_line_pointer - beg;
1180 if (length)
1181 match.linked_to_symbol_name = xmemdup0 (beg, length);
1182 }
1183
1184 if ((attr & SHF_GROUP) != 0 && is_clone)
1185 {
1186 as_warn (_("? section flag ignored with G present"));
1187 is_clone = FALSE;
1188 }
1189 if ((attr & SHF_GROUP) != 0 && *input_line_pointer == ',')
1190 {
1191 ++input_line_pointer;
1192 match.group_name = obj_elf_section_name ();
1193 if (match.group_name == NULL)
1194 attr &= ~SHF_GROUP;
1195 else if (*input_line_pointer == ',')
1196 {
1197 ++input_line_pointer;
1198 SKIP_WHITESPACE ();
1199 if (strncmp (input_line_pointer, "comdat", 6) == 0)
1200 {
1201 input_line_pointer += 6;
1202 linkonce = 1;
1203 }
1204 }
1205 else if (strncmp (name, ".gnu.linkonce", 13) == 0)
1206 linkonce = 1;
1207 }
1208 else if ((attr & SHF_GROUP) != 0)
1209 {
1210 as_warn (_("group name for SHF_GROUP not specified"));
1211 attr &= ~SHF_GROUP;
1212 }
1213
1214 if (is_clone)
1215 {
1216 const char *now_group = elf_group_name (now_seg);
1217 if (now_group != NULL)
1218 {
1219 match.group_name = xstrdup (now_group);
1220 linkonce = (now_seg->flags & SEC_LINK_ONCE) != 0;
1221 }
1222 }
1223
1224 if ((gnu_attr & SHF_GNU_MBIND) != 0 && *input_line_pointer == ',')
1225 {
1226 char *save = input_line_pointer;
1227 ++input_line_pointer;
1228 SKIP_WHITESPACE ();
1229 if (ISDIGIT (* input_line_pointer))
1230 {
1231 char *t = input_line_pointer;
1232 match.info = strtoul (input_line_pointer,
1233 &input_line_pointer, 0);
1234 if (match.info == (unsigned int) -1)
1235 {
1236 as_warn (_("unsupported mbind section info: %s"), t);
1237 match.info = 0;
1238 }
1239 }
1240 else
1241 input_line_pointer = save;
1242 }
1243
1244 if (*input_line_pointer == ',')
1245 {
1246 char *save = input_line_pointer;
1247 ++input_line_pointer;
1248 SKIP_WHITESPACE ();
1249 if (strncmp (input_line_pointer, "unique", 6) == 0)
1250 {
1251 input_line_pointer += 6;
1252 SKIP_WHITESPACE ();
1253 if (*input_line_pointer == ',')
1254 {
1255 ++input_line_pointer;
1256 SKIP_WHITESPACE ();
1257 if (ISDIGIT (* input_line_pointer))
1258 {
1259 bfd_vma id;
1260 bfd_boolean overflow;
1261 char *t = input_line_pointer;
1262 if (sizeof (bfd_vma) <= sizeof (unsigned long))
1263 {
1264 errno = 0;
1265 id = strtoul (input_line_pointer,
1266 &input_line_pointer, 0);
1267 overflow = (id == (unsigned long) -1
1268 && errno == ERANGE);
1269 }
1270 else
1271 {
1272 id = bfd_scan_vma
1273 (input_line_pointer,
1274 (const char **) &input_line_pointer, 0);
1275 overflow = id == ~(bfd_vma) 0;
1276 }
1277 if (overflow || id > (unsigned int) -1)
1278 {
1279 char *linefeed, saved_char = 0;
1280 if ((linefeed = strchr (t, '\n')) != NULL)
1281 {
1282 saved_char = *linefeed;
1283 *linefeed = '\0';
1284 }
1285 as_bad (_("unsupported section id: %s"), t);
1286 if (saved_char)
1287 *linefeed = saved_char;
1288 }
1289 else
1290 {
1291 match.section_id = id;
1292 match.flags |= SEC_ASSEMBLER_SECTION_ID;
1293 }
1294 }
1295 }
1296 }
1297 else
1298 input_line_pointer = save;
1299 }
1300 }
1301 else
1302 {
1303 do
1304 {
1305 char c;
1306
1307 SKIP_WHITESPACE ();
1308 if (*input_line_pointer != '#')
1309 {
1310 as_bad (_("character following name is not '#'"));
1311 ignore_rest_of_line ();
1312 return;
1313 }
1314 ++input_line_pointer;
1315 c = get_symbol_name (& beg);
1316 (void) restore_line_pointer (c);
1317
1318 attr |= obj_elf_section_word (beg, input_line_pointer - beg,
1319 &type);
1320
1321 SKIP_WHITESPACE ();
1322 }
1323 while (*input_line_pointer++ == ',');
1324 --input_line_pointer;
1325 }
1326 }
1327
1328done:
1329 demand_empty_rest_of_line ();
1330
1331 obj_elf_change_section (name, type, attr, entsize, &match, linkonce,
1332 push);
1333
1334 if ((gnu_attr & SHF_GNU_MBIND) != 0)
1335 {
1336 struct elf_backend_data *bed;
1337
1338 if ((attr & SHF_ALLOC) == 0)
1339 as_bad (_("SHF_ALLOC isn't set for GNU_MBIND section: %s"), name);
1340
1341 bed = (struct elf_backend_data *) get_elf_backend_data (stdoutput);
1342 if (bed->elf_osabi == ELFOSABI_NONE)
1343 bed->elf_osabi = ELFOSABI_GNU;
1344 else if (bed->elf_osabi != ELFOSABI_GNU
1345 && bed->elf_osabi != ELFOSABI_FREEBSD)
1346 as_bad (_("GNU_MBIND section is supported only by GNU "
1347 "and FreeBSD targets"));
1348 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1349 }
1350 elf_section_flags (now_seg) |= gnu_attr;
1351
1352 if (push && new_subsection != -1)
1353 subseg_set (now_seg, new_subsection);
1354}
1355
1356/* Change to the .data section. */
1357
1358void
1359obj_elf_data (int i)
1360{
1361#ifdef md_flush_pending_output
1362 md_flush_pending_output ();
1363#endif
1364
1365 previous_section = now_seg;
1366 previous_subsection = now_subseg;
1367 s_data (i);
1368
1369#ifdef md_elf_section_change_hook
1370 md_elf_section_change_hook ();
1371#endif
1372}
1373
1374/* Change to the .text section. */
1375
1376void
1377obj_elf_text (int i)
1378{
1379#ifdef md_flush_pending_output
1380 md_flush_pending_output ();
1381#endif
1382
1383 previous_section = now_seg;
1384 previous_subsection = now_subseg;
1385 s_text (i);
1386
1387#ifdef md_elf_section_change_hook
1388 md_elf_section_change_hook ();
1389#endif
1390}
1391
1392/* Change to the *ABS* section. */
1393
1394void
1395obj_elf_struct (int i)
1396{
1397#ifdef md_flush_pending_output
1398 md_flush_pending_output ();
1399#endif
1400
1401 previous_section = now_seg;
1402 previous_subsection = now_subseg;
1403 s_struct (i);
1404
1405#ifdef md_elf_section_change_hook
1406 md_elf_section_change_hook ();
1407#endif
1408}
1409
1410static void
1411obj_elf_subsection (int ignore ATTRIBUTE_UNUSED)
1412{
1413 int temp;
1414
1415#ifdef md_flush_pending_output
1416 md_flush_pending_output ();
1417#endif
1418
1419 previous_section = now_seg;
1420 previous_subsection = now_subseg;
1421
1422 temp = get_absolute_expression ();
1423 subseg_set (now_seg, (subsegT) temp);
1424 demand_empty_rest_of_line ();
1425
1426#ifdef md_elf_section_change_hook
1427 md_elf_section_change_hook ();
1428#endif
1429}
1430
1431/* This can be called from the processor backends if they change
1432 sections. */
1433
1434void
1435obj_elf_section_change_hook (void)
1436{
1437 previous_section = now_seg;
1438 previous_subsection = now_subseg;
1439}
1440
1441void
1442obj_elf_previous (int ignore ATTRIBUTE_UNUSED)
1443{
1444 segT new_section;
1445 int new_subsection;
1446
1447 if (previous_section == 0)
1448 {
1449 as_warn (_(".previous without corresponding .section; ignored"));
1450 return;
1451 }
1452
1453#ifdef md_flush_pending_output
1454 md_flush_pending_output ();
1455#endif
1456
1457 new_section = previous_section;
1458 new_subsection = previous_subsection;
1459 previous_section = now_seg;
1460 previous_subsection = now_subseg;
1461 subseg_set (new_section, new_subsection);
1462
1463#ifdef md_elf_section_change_hook
1464 md_elf_section_change_hook ();
1465#endif
1466}
1467
1468static void
1469obj_elf_popsection (int xxx ATTRIBUTE_UNUSED)
1470{
1471 struct section_stack *top = section_stack;
1472
1473 if (top == NULL)
1474 {
1475 as_warn (_(".popsection without corresponding .pushsection; ignored"));
1476 return;
1477 }
1478
1479#ifdef md_flush_pending_output
1480 md_flush_pending_output ();
1481#endif
1482
1483 section_stack = top->next;
1484 previous_section = top->prev_seg;
1485 previous_subsection = top->prev_subseg;
1486 subseg_set (top->seg, top->subseg);
1487 free (top);
1488
1489#ifdef md_elf_section_change_hook
1490 md_elf_section_change_hook ();
1491#endif
1492}
1493
1494static void
1495obj_elf_line (int ignore ATTRIBUTE_UNUSED)
1496{
1497 /* Assume delimiter is part of expression. BSD4.2 as fails with
1498 delightful bug, so we are not being incompatible here. */
1499 new_logical_line (NULL, get_absolute_expression ());
1500 demand_empty_rest_of_line ();
1501}
1502
1503/* This handles the .symver pseudo-op, which is used to specify a
1504 symbol version. The syntax is ``.symver NAME,SYMVERNAME''.
1505 SYMVERNAME may contain ELF_VER_CHR ('@') characters. This
1506 pseudo-op causes the assembler to emit a symbol named SYMVERNAME
1507 with the same value as the symbol NAME. */
1508
1509static void
1510obj_elf_symver (int ignore ATTRIBUTE_UNUSED)
1511{
1512 char *name;
1513 char c;
1514 char old_lexat;
1515 symbolS *sym;
1516
1517 sym = get_sym_from_input_line_and_check ();
1518
1519 if (*input_line_pointer != ',')
1520 {
1521 as_bad (_("expected comma after name in .symver"));
1522 ignore_rest_of_line ();
1523 return;
1524 }
1525
1526 ++input_line_pointer;
1527 SKIP_WHITESPACE ();
1528
1529 /* Temporarily include '@' in symbol names. */
1530 old_lexat = lex_type[(unsigned char) '@'];
1531 lex_type[(unsigned char) '@'] |= LEX_NAME;
1532 c = get_symbol_name (& name);
1533 lex_type[(unsigned char) '@'] = old_lexat;
1534
1535 if (S_IS_COMMON (sym))
1536 {
1537 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
1538 name, S_GET_NAME (sym));
1539 ignore_rest_of_line ();
1540 return;
1541 }
1542
1543 if (symbol_get_obj (sym)->versioned_name == NULL)
1544 {
1545 symbol_get_obj (sym)->versioned_name = xstrdup (name);
1546
1547 (void) restore_line_pointer (c);
1548
1549 if (strchr (symbol_get_obj (sym)->versioned_name,
1550 ELF_VER_CHR) == NULL)
1551 {
1552 as_bad (_("missing version name in `%s' for symbol `%s'"),
1553 symbol_get_obj (sym)->versioned_name,
1554 S_GET_NAME (sym));
1555 ignore_rest_of_line ();
1556 return;
1557 }
1558 }
1559 else
1560 {
1561 if (strcmp (symbol_get_obj (sym)->versioned_name, name))
1562 {
1563 as_bad (_("multiple versions [`%s'|`%s'] for symbol `%s'"),
1564 name, symbol_get_obj (sym)->versioned_name,
1565 S_GET_NAME (sym));
1566 ignore_rest_of_line ();
1567 return;
1568 }
1569
1570 (void) restore_line_pointer (c);
1571 }
1572
1573 demand_empty_rest_of_line ();
1574}
1575
1576/* This handles the .vtable_inherit pseudo-op, which is used to indicate
1577 to the linker the hierarchy in which a particular table resides. The
1578 syntax is ".vtable_inherit CHILDNAME, PARENTNAME". */
1579
1580struct fix *
1581obj_elf_get_vtable_inherit (void)
1582{
1583 char *cname, *pname;
1584 symbolS *csym, *psym;
1585 char c, bad = 0;
1586
1587 if (*input_line_pointer == '#')
1588 ++input_line_pointer;
1589
1590 c = get_symbol_name (& cname);
1591 csym = symbol_find (cname);
1592
1593 /* GCFIXME: should check that we don't have two .vtable_inherits for
1594 the same child symbol. Also, we can currently only do this if the
1595 child symbol is already exists and is placed in a fragment. */
1596
1597 if (csym == NULL || symbol_get_frag (csym) == NULL)
1598 {
1599 as_bad (_("expected `%s' to have already been set for .vtable_inherit"),
1600 cname);
1601 bad = 1;
1602 }
1603
1604 *input_line_pointer = c;
1605
1606 SKIP_WHITESPACE_AFTER_NAME ();
1607 if (*input_line_pointer != ',')
1608 {
1609 as_bad (_("expected comma after name in .vtable_inherit"));
1610 ignore_rest_of_line ();
1611 return NULL;
1612 }
1613
1614 ++input_line_pointer;
1615 SKIP_WHITESPACE ();
1616
1617 if (*input_line_pointer == '#')
1618 ++input_line_pointer;
1619
1620 if (input_line_pointer[0] == '0'
1621 && (input_line_pointer[1] == '\0'
1622 || ISSPACE (input_line_pointer[1])))
1623 {
1624 psym = section_symbol (absolute_section);
1625 ++input_line_pointer;
1626 }
1627 else
1628 {
1629 c = get_symbol_name (& pname);
1630 psym = symbol_find_or_make (pname);
1631 restore_line_pointer (c);
1632 }
1633
1634 demand_empty_rest_of_line ();
1635
1636 if (bad)
1637 return NULL;
1638
1639 gas_assert (symbol_get_value_expression (csym)->X_op == O_constant);
1640 return fix_new (symbol_get_frag (csym),
1641 symbol_get_value_expression (csym)->X_add_number,
1642 0, psym, 0, 0, BFD_RELOC_VTABLE_INHERIT);
1643}
1644
1645/* This is a version of obj_elf_get_vtable_inherit() that is
1646 suitable for use in struct _pseudo_type tables. */
1647
1648void
1649obj_elf_vtable_inherit (int ignore ATTRIBUTE_UNUSED)
1650{
1651 (void) obj_elf_get_vtable_inherit ();
1652}
1653
1654/* This handles the .vtable_entry pseudo-op, which is used to indicate
1655 to the linker that a vtable slot was used. The syntax is
1656 ".vtable_entry tablename, offset". */
1657
1658struct fix *
1659obj_elf_get_vtable_entry (void)
1660{
1661 symbolS *sym;
1662 offsetT offset;
1663
1664 if (*input_line_pointer == '#')
1665 ++input_line_pointer;
1666
1667 sym = get_sym_from_input_line_and_check ();
1668 if (*input_line_pointer != ',')
1669 {
1670 as_bad (_("expected comma after name in .vtable_entry"));
1671 ignore_rest_of_line ();
1672 return NULL;
1673 }
1674
1675 ++input_line_pointer;
1676 if (*input_line_pointer == '#')
1677 ++input_line_pointer;
1678
1679 offset = get_absolute_expression ();
1680
1681 demand_empty_rest_of_line ();
1682
1683 return fix_new (frag_now, frag_now_fix (), 0, sym, offset, 0,
1684 BFD_RELOC_VTABLE_ENTRY);
1685}
1686
1687/* This is a version of obj_elf_get_vtable_entry() that is
1688 suitable for use in struct _pseudo_type tables. */
1689
1690void
1691obj_elf_vtable_entry (int ignore ATTRIBUTE_UNUSED)
1692{
1693 (void) obj_elf_get_vtable_entry ();
1694}
1695
1696#define skip_whitespace(str) do { if (*(str) == ' ') ++(str); } while (0)
1697
1698static inline int
1699skip_past_char (char ** str, char c)
1700{
1701 if (**str == c)
1702 {
1703 (*str)++;
1704 return 0;
1705 }
1706 else
1707 return -1;
1708}
1709#define skip_past_comma(str) skip_past_char (str, ',')
1710
1711/* A list of attributes that have been explicitly set by the assembly code.
1712 VENDOR is the vendor id, BASE is the tag shifted right by the number
1713 of bits in MASK, and bit N of MASK is set if tag BASE+N has been set. */
1714struct recorded_attribute_info {
1715 struct recorded_attribute_info *next;
1716 int vendor;
1717 unsigned int base;
1718 unsigned long mask;
1719};
1720static struct recorded_attribute_info *recorded_attributes;
1721
1722/* Record that we have seen an explicit specification of attribute TAG
1723 for vendor VENDOR. */
1724
1725static void
1726record_attribute (int vendor, unsigned int tag)
1727{
1728 unsigned int base;
1729 unsigned long mask;
1730 struct recorded_attribute_info *rai;
1731
1732 base = tag / (8 * sizeof (rai->mask));
1733 mask = 1UL << (tag % (8 * sizeof (rai->mask)));
1734 for (rai = recorded_attributes; rai; rai = rai->next)
1735 if (rai->vendor == vendor && rai->base == base)
1736 {
1737 rai->mask |= mask;
1738 return;
1739 }
1740
1741 rai = XNEW (struct recorded_attribute_info);
1742 rai->next = recorded_attributes;
1743 rai->vendor = vendor;
1744 rai->base = base;
1745 rai->mask = mask;
1746 recorded_attributes = rai;
1747}
1748
1749/* Return true if we have seen an explicit specification of attribute TAG
1750 for vendor VENDOR. */
1751
1752bfd_boolean
1753obj_elf_seen_attribute (int vendor, unsigned int tag)
1754{
1755 unsigned int base;
1756 unsigned long mask;
1757 struct recorded_attribute_info *rai;
1758
1759 base = tag / (8 * sizeof (rai->mask));
1760 mask = 1UL << (tag % (8 * sizeof (rai->mask)));
1761 for (rai = recorded_attributes; rai; rai = rai->next)
1762 if (rai->vendor == vendor && rai->base == base)
1763 return (rai->mask & mask) != 0;
1764 return FALSE;
1765}
1766
1767/* Parse an attribute directive for VENDOR.
1768 Returns the attribute number read, or zero on error. */
1769
1770int
1771obj_elf_vendor_attribute (int vendor)
1772{
1773 expressionS exp;
1774 int type;
1775 int tag;
1776 unsigned int i = 0;
1777 char *s = NULL;
1778
1779 /* Read the first number or name. */
1780 skip_whitespace (input_line_pointer);
1781 s = input_line_pointer;
1782 if (ISDIGIT (*input_line_pointer))
1783 {
1784 expression (& exp);
1785 if (exp.X_op != O_constant)
1786 goto bad;
1787 tag = exp.X_add_number;
1788 }
1789 else
1790 {
1791 char *name;
1792
1793 /* A name may contain '_', but no other punctuation. */
1794 for (; ISALNUM (*input_line_pointer) || *input_line_pointer == '_';
1795 ++input_line_pointer)
1796 i++;
1797 if (i == 0)
1798 goto bad;
1799
1800 name = xstrndup (s, i);
1801
1802#ifndef CONVERT_SYMBOLIC_ATTRIBUTE
1803#define CONVERT_SYMBOLIC_ATTRIBUTE(a) -1
1804#endif
1805
1806 tag = CONVERT_SYMBOLIC_ATTRIBUTE (name);
1807 if (tag == -1)
1808 {
1809 as_bad (_("Attribute name not recognised: %s"), name);
1810 ignore_rest_of_line ();
1811 free (name);
1812 return 0;
1813 }
1814 free (name);
1815 }
1816
1817 type = _bfd_elf_obj_attrs_arg_type (stdoutput, vendor, tag);
1818
1819 if (skip_past_comma (&input_line_pointer) == -1)
1820 goto bad;
1821 if (type & 1)
1822 {
1823 expression (& exp);
1824 if (exp.X_op != O_constant)
1825 {
1826 as_bad (_("expected numeric constant"));
1827 ignore_rest_of_line ();
1828 return 0;
1829 }
1830 i = exp.X_add_number;
1831 }
1832 if ((type & 3) == 3
1833 && skip_past_comma (&input_line_pointer) == -1)
1834 {
1835 as_bad (_("expected comma"));
1836 ignore_rest_of_line ();
1837 return 0;
1838 }
1839 if (type & 2)
1840 {
1841 int len;
1842
1843 skip_whitespace (input_line_pointer);
1844 if (*input_line_pointer != '"')
1845 goto bad_string;
1846 s = demand_copy_C_string (&len);
1847 }
1848
1849 record_attribute (vendor, tag);
1850 switch (type & 3)
1851 {
1852 case 3:
1853 bfd_elf_add_obj_attr_int_string (stdoutput, vendor, tag, i, s);
1854 break;
1855 case 2:
1856 bfd_elf_add_obj_attr_string (stdoutput, vendor, tag, s);
1857 break;
1858 case 1:
1859 bfd_elf_add_obj_attr_int (stdoutput, vendor, tag, i);
1860 break;
1861 default:
1862 abort ();
1863 }
1864
1865 demand_empty_rest_of_line ();
1866 return tag;
1867bad_string:
1868 as_bad (_("bad string constant"));
1869 ignore_rest_of_line ();
1870 return 0;
1871bad:
1872 as_bad (_("expected <tag> , <value>"));
1873 ignore_rest_of_line ();
1874 return 0;
1875}
1876
1877/* Parse a .gnu_attribute directive. */
1878
1879static void
1880obj_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED)
1881{
1882 obj_elf_vendor_attribute (OBJ_ATTR_GNU);
1883}
1884
1885void
1886elf_obj_read_begin_hook (void)
1887{
1888#ifdef NEED_ECOFF_DEBUG
1889 if (ECOFF_DEBUGGING)
1890 ecoff_read_begin_hook ();
1891#endif
1892}
1893
1894void
1895elf_obj_symbol_new_hook (symbolS *symbolP)
1896{
1897 struct elf_obj_sy *sy_obj;
1898
1899 sy_obj = symbol_get_obj (symbolP);
1900 sy_obj->size = NULL;
1901 sy_obj->versioned_name = NULL;
1902
1903#ifdef NEED_ECOFF_DEBUG
1904 if (ECOFF_DEBUGGING)
1905 ecoff_symbol_new_hook (symbolP);
1906#endif
1907}
1908
1909/* When setting one symbol equal to another, by default we probably
1910 want them to have the same "size", whatever it means in the current
1911 context. */
1912
1913void
1914elf_copy_symbol_attributes (symbolS *dest, symbolS *src)
1915{
1916 struct elf_obj_sy *srcelf = symbol_get_obj (src);
1917 struct elf_obj_sy *destelf = symbol_get_obj (dest);
1918 if (srcelf->size)
1919 {
1920 if (destelf->size == NULL)
1921 destelf->size = XNEW (expressionS);
1922 *destelf->size = *srcelf->size;
1923 }
1924 else
1925 {
1926 if (destelf->size != NULL)
1927 free (destelf->size);
1928 destelf->size = NULL;
1929 }
1930 S_SET_SIZE (dest, S_GET_SIZE (src));
1931 /* Don't copy visibility. */
1932 S_SET_OTHER (dest, (ELF_ST_VISIBILITY (S_GET_OTHER (dest))
1933 | (S_GET_OTHER (src) & ~ELF_ST_VISIBILITY (-1))));
1934}
1935
1936void
1937obj_elf_version (int ignore ATTRIBUTE_UNUSED)
1938{
1939 char *name;
1940 unsigned int c;
1941 char *p;
1942 asection *seg = now_seg;
1943 subsegT subseg = now_subseg;
1944 Elf_Internal_Note i_note;
1945 Elf_External_Note e_note;
1946 asection *note_secp = NULL;
1947
1948 SKIP_WHITESPACE ();
1949 if (*input_line_pointer == '\"')
1950 {
1951 unsigned int len;
1952
1953 ++input_line_pointer; /* -> 1st char of string. */
1954 name = input_line_pointer;
1955
1956 while (is_a_char (c = next_char_of_string ()))
1957 ;
1958 c = *input_line_pointer;
1959 *input_line_pointer = '\0';
1960 *(input_line_pointer - 1) = '\0';
1961 *input_line_pointer = c;
1962
1963 /* Create the .note section. */
1964 note_secp = subseg_new (".note", 0);
1965 bfd_set_section_flags (note_secp, SEC_HAS_CONTENTS | SEC_READONLY);
1966 record_alignment (note_secp, 2);
1967
1968 /* Process the version string. */
1969 len = strlen (name) + 1;
1970
1971 /* PR 3456: Although the name field is padded out to an 4-byte
1972 boundary, the namesz field should not be adjusted. */
1973 i_note.namesz = len;
1974 i_note.descsz = 0; /* No description. */
1975 i_note.type = NT_VERSION;
1976 p = frag_more (sizeof (e_note.namesz));
1977 md_number_to_chars (p, i_note.namesz, sizeof (e_note.namesz));
1978 p = frag_more (sizeof (e_note.descsz));
1979 md_number_to_chars (p, i_note.descsz, sizeof (e_note.descsz));
1980 p = frag_more (sizeof (e_note.type));
1981 md_number_to_chars (p, i_note.type, sizeof (e_note.type));
1982 p = frag_more (len);
1983 memcpy (p, name, len);
1984
1985 frag_align (2, 0, 0);
1986
1987 subseg_set (seg, subseg);
1988 }
1989 else
1990 as_bad (_("expected quoted string"));
1991
1992 demand_empty_rest_of_line ();
1993}
1994
1995static void
1996obj_elf_size (int ignore ATTRIBUTE_UNUSED)
1997{
1998 char *name;
1999 char c = get_symbol_name (&name);
2000 char *p;
2001 expressionS exp;
2002 symbolS *sym;
2003
2004 p = input_line_pointer;
2005 *p = c;
2006 SKIP_WHITESPACE_AFTER_NAME ();
2007 if (*input_line_pointer != ',')
2008 {
2009 *p = 0;
2010 as_bad (_("expected comma after name `%s' in .size directive"), name);
2011 *p = c;
2012 ignore_rest_of_line ();
2013 return;
2014 }
2015 input_line_pointer++;
2016 expression (&exp);
2017 if (exp.X_op == O_absent)
2018 {
2019 as_bad (_("missing expression in .size directive"));
2020 exp.X_op = O_constant;
2021 exp.X_add_number = 0;
2022 }
2023 *p = 0;
2024 sym = symbol_find_or_make (name);
2025 *p = c;
2026 if (exp.X_op == O_constant)
2027 {
2028 S_SET_SIZE (sym, exp.X_add_number);
2029 if (symbol_get_obj (sym)->size)
2030 {
2031 xfree (symbol_get_obj (sym)->size);
2032 symbol_get_obj (sym)->size = NULL;
2033 }
2034 }
2035 else
2036 {
2037 symbol_get_obj (sym)->size = XNEW (expressionS);
2038 *symbol_get_obj (sym)->size = exp;
2039 }
2040 demand_empty_rest_of_line ();
2041}
2042
2043/* Handle the ELF .type pseudo-op. This sets the type of a symbol.
2044 There are six syntaxes:
2045
2046 The first (used on Solaris) is
2047 .type SYM,#function
2048 The second (used on UnixWare) is
2049 .type SYM,@function
2050 The third (reportedly to be used on Irix 6.0) is
2051 .type SYM STT_FUNC
2052 The fourth (used on NetBSD/Arm and Linux/ARM) is
2053 .type SYM,%function
2054 The fifth (used on SVR4/860) is
2055 .type SYM,"function"
2056 The sixth (emitted by recent SunPRO under Solaris) is
2057 .type SYM,[0-9]
2058 where the integer is the STT_* value.
2059 */
2060
2061static char *
2062obj_elf_type_name (char *cp)
2063{
2064 char *p;
2065
2066 p = input_line_pointer;
2067 if (*input_line_pointer >= '0'
2068 && *input_line_pointer <= '9')
2069 {
2070 while (*input_line_pointer >= '0'
2071 && *input_line_pointer <= '9')
2072 ++input_line_pointer;
2073 *cp = *input_line_pointer;
2074 *input_line_pointer = '\0';
2075 }
2076 else
2077 *cp = get_symbol_name (&p);
2078
2079 return p;
2080}
2081
2082static void
2083obj_elf_type (int ignore ATTRIBUTE_UNUSED)
2084{
2085 char c;
2086 int type;
2087 const char *type_name;
2088 symbolS *sym;
2089 elf_symbol_type *elfsym;
2090
2091 sym = get_sym_from_input_line_and_check ();
2092 c = *input_line_pointer;
2093 elfsym = (elf_symbol_type *) symbol_get_bfdsym (sym);
2094
2095 if (*input_line_pointer == ',')
2096 ++input_line_pointer;
2097
2098 SKIP_WHITESPACE ();
2099 if ( *input_line_pointer == '#'
2100 || *input_line_pointer == '@'
2101 || *input_line_pointer == '"'
2102 || *input_line_pointer == '%')
2103 ++input_line_pointer;
2104
2105 type_name = obj_elf_type_name (& c);
2106
2107 type = 0;
2108 if (strcmp (type_name, "function") == 0
2109 || strcmp (type_name, "2") == 0
2110 || strcmp (type_name, "STT_FUNC") == 0)
2111 type = BSF_FUNCTION;
2112 else if (strcmp (type_name, "object") == 0
2113 || strcmp (type_name, "1") == 0
2114 || strcmp (type_name, "STT_OBJECT") == 0)
2115 type = BSF_OBJECT;
2116 else if (strcmp (type_name, "tls_object") == 0
2117 || strcmp (type_name, "6") == 0
2118 || strcmp (type_name, "STT_TLS") == 0)
2119 type = BSF_OBJECT | BSF_THREAD_LOCAL;
2120 else if (strcmp (type_name, "notype") == 0
2121 || strcmp (type_name, "0") == 0
2122 || strcmp (type_name, "STT_NOTYPE") == 0)
2123 ;
2124 else if (strcmp (type_name, "common") == 0
2125 || strcmp (type_name, "5") == 0
2126 || strcmp (type_name, "STT_COMMON") == 0)
2127 {
2128 type = BSF_OBJECT;
2129
2130 if (! S_IS_COMMON (sym))
2131 {
2132 if (S_IS_VOLATILE (sym))
2133 {
2134 sym = symbol_clone (sym, 1);
2135 S_SET_SEGMENT (sym, bfd_com_section_ptr);
2136 S_SET_VALUE (sym, 0);
2137 S_SET_EXTERNAL (sym);
2138 symbol_set_frag (sym, &zero_address_frag);
2139 S_CLEAR_VOLATILE (sym);
2140 }
2141 else if (S_IS_DEFINED (sym) || symbol_equated_p (sym))
2142 as_bad (_("symbol '%s' is already defined"), S_GET_NAME (sym));
2143 else
2144 {
2145 /* FIXME: Is it safe to just change the section ? */
2146 S_SET_SEGMENT (sym, bfd_com_section_ptr);
2147 S_SET_VALUE (sym, 0);
2148 S_SET_EXTERNAL (sym);
2149 }
2150 }
2151 }
2152 else if (strcmp (type_name, "gnu_indirect_function") == 0
2153 || strcmp (type_name, "10") == 0
2154 || strcmp (type_name, "STT_GNU_IFUNC") == 0)
2155 {
2156 struct elf_backend_data *bed;
2157
2158 bed = (struct elf_backend_data *) get_elf_backend_data (stdoutput);
2159 if (bed->elf_osabi == ELFOSABI_NONE)
2160 bed->elf_osabi = ELFOSABI_GNU;
2161 else if (bed->elf_osabi != ELFOSABI_GNU
2162 && bed->elf_osabi != ELFOSABI_FREEBSD)
2163 as_bad (_("symbol type \"%s\" is supported only by GNU "
2164 "and FreeBSD targets"), type_name);
2165 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
2166 type = BSF_FUNCTION | BSF_GNU_INDIRECT_FUNCTION;
2167 }
2168 else if (strcmp (type_name, "gnu_unique_object") == 0)
2169 {
2170 struct elf_backend_data *bed;
2171
2172 bed = (struct elf_backend_data *) get_elf_backend_data (stdoutput);
2173 if (bed->elf_osabi == ELFOSABI_NONE)
2174 bed->elf_osabi = ELFOSABI_GNU;
2175 else if (bed->elf_osabi != ELFOSABI_GNU)
2176 as_bad (_("symbol type \"%s\" is supported only by GNU targets"),
2177 type_name);
2178 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_unique;
2179 type = BSF_OBJECT | BSF_GNU_UNIQUE;
2180 }
2181#ifdef md_elf_symbol_type
2182 else if ((type = md_elf_symbol_type (type_name, sym, elfsym)) != -1)
2183 ;
2184#endif
2185 else
2186 as_bad (_("unrecognized symbol type \"%s\""), type_name);
2187
2188 *input_line_pointer = c;
2189
2190 if (*input_line_pointer == '"')
2191 ++input_line_pointer;
2192
2193#ifdef md_elf_symbol_type_change
2194 if (!md_elf_symbol_type_change (sym, elfsym, type))
2195#endif
2196 {
2197 flagword mask = BSF_FUNCTION | BSF_OBJECT;
2198
2199 if (type != BSF_FUNCTION)
2200 mask |= BSF_GNU_INDIRECT_FUNCTION;
2201 if (type != BSF_OBJECT)
2202 {
2203 mask |= BSF_GNU_UNIQUE | BSF_THREAD_LOCAL;
2204
2205 if (S_IS_COMMON (sym))
2206 {
2207 as_bad (_("cannot change type of common symbol '%s'"),
2208 S_GET_NAME (sym));
2209 mask = type = 0;
2210 }
2211 }
2212
2213 /* Don't warn when changing to STT_NOTYPE. */
2214 if (type)
2215 {
2216 flagword new = (elfsym->symbol.flags & ~mask) | type;
2217
2218 if (new != (elfsym->symbol.flags | type))
2219 as_warn (_("symbol '%s' already has its type set"), S_GET_NAME (sym));
2220 elfsym->symbol.flags = new;
2221 }
2222 else
2223 elfsym->symbol.flags &= ~mask;
2224 }
2225
2226 demand_empty_rest_of_line ();
2227}
2228
2229static void
2230obj_elf_ident (int ignore ATTRIBUTE_UNUSED)
2231{
2232 static segT comment_section;
2233 segT old_section = now_seg;
2234 int old_subsection = now_subseg;
2235
2236#ifdef md_flush_pending_output
2237 md_flush_pending_output ();
2238#endif
2239
2240 if (!comment_section)
2241 {
2242 char *p;
2243 comment_section = subseg_new (".comment", 0);
2244 bfd_set_section_flags (comment_section, (SEC_READONLY | SEC_HAS_CONTENTS
2245 | SEC_MERGE | SEC_STRINGS));
2246 comment_section->entsize = 1;
2247#ifdef md_elf_section_change_hook
2248 md_elf_section_change_hook ();
2249#endif
2250 p = frag_more (1);
2251 *p = 0;
2252 }
2253 else
2254 subseg_set (comment_section, 0);
2255 stringer (8 + 1);
2256 subseg_set (old_section, old_subsection);
2257}
2258
2259#ifdef INIT_STAB_SECTION
2260
2261/* The first entry in a .stabs section is special. */
2262
2263void
2264obj_elf_init_stab_section (segT seg)
2265{
2266 const char *file;
2267 char *p;
2268 char *stabstr_name;
2269 unsigned int stroff;
2270
2271 /* Force the section to align to a longword boundary. Without this,
2272 UnixWare ar crashes. */
2273 bfd_set_section_alignment (seg, 2);
2274
2275 /* Make space for this first symbol. */
2276 p = frag_more (12);
2277 /* Zero it out. */
2278 memset (p, 0, 12);
2279 file = as_where (NULL);
2280 stabstr_name = concat (segment_name (seg), "str", (char *) NULL);
2281 stroff = get_stab_string_offset (file, stabstr_name, TRUE);
2282 know (stroff == 1 || (stroff == 0 && file[0] == '\0'));
2283 md_number_to_chars (p, stroff, 4);
2284 seg_info (seg)->stabu.p = p;
2285}
2286
2287#endif
2288
2289/* Fill in the counts in the first entry in a .stabs section. */
2290
2291static void
2292adjust_stab_sections (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
2293{
2294 char *name;
2295 asection *strsec;
2296 char *p;
2297 int strsz, nsyms;
2298
2299 if (strncmp (".stab", sec->name, 5))
2300 return;
2301 if (!strcmp ("str", sec->name + strlen (sec->name) - 3))
2302 return;
2303
2304 name = concat (sec->name, "str", NULL);
2305 strsec = bfd_get_section_by_name (abfd, name);
2306 if (strsec)
2307 strsz = bfd_section_size (strsec);
2308 else
2309 strsz = 0;
2310 nsyms = bfd_section_size (sec) / 12 - 1;
2311
2312 p = seg_info (sec)->stabu.p;
2313 gas_assert (p != 0);
2314
2315 bfd_h_put_16 (abfd, nsyms, p + 6);
2316 bfd_h_put_32 (abfd, strsz, p + 8);
2317 free (name);
2318}
2319
2320#ifdef NEED_ECOFF_DEBUG
2321
2322/* This function is called by the ECOFF code. It is supposed to
2323 record the external symbol information so that the backend can
2324 write it out correctly. The ELF backend doesn't actually handle
2325 this at the moment, so we do it ourselves. We save the information
2326 in the symbol. */
2327
2328#ifdef OBJ_MAYBE_ELF
2329static
2330#endif
2331void
2332elf_ecoff_set_ext (symbolS *sym, struct ecoff_extr *ext)
2333{
2334 symbol_get_bfdsym (sym)->udata.p = ext;
2335}
2336
2337/* This function is called by bfd_ecoff_debug_externals. It is
2338 supposed to *EXT to the external symbol information, and return
2339 whether the symbol should be used at all. */
2340
2341static bfd_boolean
2342elf_get_extr (asymbol *sym, EXTR *ext)
2343{
2344 if (sym->udata.p == NULL)
2345 return FALSE;
2346 *ext = *(EXTR *) sym->udata.p;
2347 return TRUE;
2348}
2349
2350/* This function is called by bfd_ecoff_debug_externals. It has
2351 nothing to do for ELF. */
2352
2353static void
2354elf_set_index (asymbol *sym ATTRIBUTE_UNUSED,
2355 bfd_size_type indx ATTRIBUTE_UNUSED)
2356{
2357}
2358
2359#endif /* NEED_ECOFF_DEBUG */
2360
2361void
2362elf_frob_symbol (symbolS *symp, int *puntp)
2363{
2364 struct elf_obj_sy *sy_obj;
2365 expressionS *size;
2366
2367#ifdef NEED_ECOFF_DEBUG
2368 if (ECOFF_DEBUGGING)
2369 ecoff_frob_symbol (symp);
2370#endif
2371
2372 sy_obj = symbol_get_obj (symp);
2373
2374 size = sy_obj->size;
2375 if (size != NULL)
2376 {
2377 if (resolve_expression (size)
2378 && size->X_op == O_constant)
2379 S_SET_SIZE (symp, size->X_add_number);
2380 else
2381 {
2382 if (!flag_allow_nonconst_size)
2383 as_bad (_(".size expression for %s "
2384 "does not evaluate to a constant"), S_GET_NAME (symp));
2385 else
2386 as_warn (_(".size expression for %s "
2387 "does not evaluate to a constant"), S_GET_NAME (symp));
2388 }
2389 free (sy_obj->size);
2390 sy_obj->size = NULL;
2391 }
2392
2393 if (sy_obj->versioned_name != NULL)
2394 {
2395 char *p;
2396
2397 p = strchr (sy_obj->versioned_name, ELF_VER_CHR);
2398 if (p == NULL)
2399 /* We will have already reported an error about a missing version. */
2400 *puntp = TRUE;
2401
2402 /* This symbol was given a new name with the .symver directive.
2403
2404 If this is an external reference, just rename the symbol to
2405 include the version string. This will make the relocs be
2406 against the correct versioned symbol.
2407
2408 If this is a definition, add an alias. FIXME: Using an alias
2409 will permit the debugging information to refer to the right
2410 symbol. However, it's not clear whether it is the best
2411 approach. */
2412
2413 else if (! S_IS_DEFINED (symp))
2414 {
2415 /* Verify that the name isn't using the @@ syntax--this is
2416 reserved for definitions of the default version to link
2417 against. */
2418 if (p[1] == ELF_VER_CHR)
2419 {
2420 as_bad (_("invalid attempt to declare external version name"
2421 " as default in symbol `%s'"),
2422 sy_obj->versioned_name);
2423 *puntp = TRUE;
2424 }
2425 S_SET_NAME (symp, sy_obj->versioned_name);
2426 }
2427 else
2428 {
2429 if (p[1] == ELF_VER_CHR && p[2] == ELF_VER_CHR)
2430 {
2431 size_t l;
2432
2433 /* The @@@ syntax is a special case. It renames the
2434 symbol name to versioned_name with one `@' removed. */
2435 l = strlen (&p[3]) + 1;
2436 memmove (&p[2], &p[3], l);
2437 S_SET_NAME (symp, sy_obj->versioned_name);
2438 }
2439 else
2440 {
2441 symbolS *symp2;
2442
2443 /* FIXME: Creating a new symbol here is risky. We're
2444 in the final loop over the symbol table. We can
2445 get away with it only because the symbol goes to
2446 the end of the list, where the loop will still see
2447 it. It would probably be better to do this in
2448 obj_frob_file_before_adjust. */
2449
2450 symp2 = symbol_find_or_make (sy_obj->versioned_name);
2451
2452 /* Now we act as though we saw symp2 = sym. */
2453 if (S_IS_COMMON (symp))
2454 {
2455 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
2456 sy_obj->versioned_name, S_GET_NAME (symp));
2457 *puntp = TRUE;
2458 return;
2459 }
2460
2461 S_SET_SEGMENT (symp2, S_GET_SEGMENT (symp));
2462
2463 /* Subtracting out the frag address here is a hack
2464 because we are in the middle of the final loop. */
2465 S_SET_VALUE (symp2,
2466 (S_GET_VALUE (symp)
2467 - symbol_get_frag (symp)->fr_address));
2468
2469 symbol_set_frag (symp2, symbol_get_frag (symp));
2470
2471 /* This will copy over the size information. */
2472 copy_symbol_attributes (symp2, symp);
2473
2474 S_SET_OTHER (symp2, S_GET_OTHER (symp));
2475
2476 if (S_IS_WEAK (symp))
2477 S_SET_WEAK (symp2);
2478
2479 if (S_IS_EXTERNAL (symp))
2480 S_SET_EXTERNAL (symp2);
2481 }
2482 }
2483 }
2484
2485 /* Double check weak symbols. */
2486 if (S_IS_WEAK (symp))
2487 {
2488 if (S_IS_COMMON (symp))
2489 as_bad (_("symbol `%s' can not be both weak and common"),
2490 S_GET_NAME (symp));
2491 }
2492}
2493
2494struct group_list
2495{
2496 asection **head; /* Section lists. */
2497 unsigned int num_group; /* Number of lists. */
2498 struct hash_control *indexes; /* Maps group name to index in head array. */
2499};
2500
2501static struct group_list groups;
2502
2503/* Called via bfd_map_over_sections. If SEC is a member of a group,
2504 add it to a list of sections belonging to the group. INF is a
2505 pointer to a struct group_list, which is where we store the head of
2506 each list. If its link_to_symbol_name isn't NULL, set up its
2507 linked-to section. */
2508
2509static void
2510build_additional_section_info (bfd *abfd ATTRIBUTE_UNUSED,
2511 asection *sec, void *inf)
2512{
2513 struct group_list *list = (struct group_list *) inf;
2514 const char *group_name = elf_group_name (sec);
2515 unsigned int i;
2516 unsigned int *elem_idx;
2517 unsigned int *idx_ptr;
2518
2519 if (sec->map_head.linked_to_symbol_name)
2520 {
2521 symbolS *linked_to_sym;
2522 linked_to_sym = symbol_find (sec->map_head.linked_to_symbol_name);
2523 if (!linked_to_sym || !S_IS_DEFINED (linked_to_sym))
2524 as_bad (_("undefined linked-to symbol `%s' on section `%s'"),
2525 sec->map_head.linked_to_symbol_name,
2526 bfd_section_name (sec));
2527 else
2528 elf_linked_to_section (sec) = S_GET_SEGMENT (linked_to_sym);
2529 }
2530
2531 if (group_name == NULL)
2532 return;
2533
2534 /* If this group already has a list, add the section to the head of
2535 the list. */
2536 elem_idx = (unsigned int *) hash_find (list->indexes, group_name);
2537 if (elem_idx != NULL)
2538 {
2539 elf_next_in_group (sec) = list->head[*elem_idx];
2540 list->head[*elem_idx] = sec;
2541 return;
2542 }
2543
2544 /* New group. Make the arrays bigger in chunks to minimize calls to
2545 realloc. */
2546 i = list->num_group;
2547 if ((i & 127) == 0)
2548 {
2549 unsigned int newsize = i + 128;
2550 list->head = XRESIZEVEC (asection *, list->head, newsize);
2551 }
2552 list->head[i] = sec;
2553 list->num_group += 1;
2554
2555 /* Add index to hash. */
2556 idx_ptr = XNEW (unsigned int);
2557 *idx_ptr = i;
2558 hash_insert (list->indexes, group_name, idx_ptr);
2559}
2560
2561static void free_section_idx (const char *key ATTRIBUTE_UNUSED, void *val)
2562{
2563 free ((unsigned int *) val);
2564}
2565
2566/* Create symbols for group signature. */
2567
2568void
2569elf_adjust_symtab (void)
2570{
2571 unsigned int i;
2572
2573 /* Go find section groups. */
2574 groups.num_group = 0;
2575 groups.head = NULL;
2576 groups.indexes = hash_new ();
2577 bfd_map_over_sections (stdoutput, build_additional_section_info,
2578 &groups);
2579
2580 /* Make the SHT_GROUP sections that describe each section group. We
2581 can't set up the section contents here yet, because elf section
2582 indices have yet to be calculated. elf.c:set_group_contents does
2583 the rest of the work. */
2584 for (i = 0; i < groups.num_group; i++)
2585 {
2586 const char *group_name = elf_group_name (groups.head[i]);
2587 const char *sec_name;
2588 asection *s;
2589 flagword flags;
2590 struct symbol *sy;
2591
2592 flags = SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_GROUP;
2593 for (s = groups.head[i]; s != NULL; s = elf_next_in_group (s))
2594 if ((s->flags ^ flags) & SEC_LINK_ONCE)
2595 {
2596 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2597 if (s != groups.head[i])
2598 {
2599 as_warn (_("assuming all members of group `%s' are COMDAT"),
2600 group_name);
2601 break;
2602 }
2603 }
2604
2605 sec_name = ".group";
2606 s = subseg_force_new (sec_name, 0);
2607 if (s == NULL
2608 || !bfd_set_section_flags (s, flags)
2609 || !bfd_set_section_alignment (s, 2))
2610 {
2611 as_fatal (_("can't create group: %s"),
2612 bfd_errmsg (bfd_get_error ()));
2613 }
2614 elf_section_type (s) = SHT_GROUP;
2615
2616 /* Pass a pointer to the first section in this group. */
2617 elf_next_in_group (s) = groups.head[i];
2618 elf_sec_group (groups.head[i]) = s;
2619 /* Make sure that the signature symbol for the group has the
2620 name of the group. */
2621 sy = symbol_find_exact (group_name);
2622 if (!sy || !symbol_on_chain (sy, symbol_rootP, symbol_lastP))
2623 {
2624 /* Create the symbol now. */
2625 sy = symbol_new (group_name, now_seg, (valueT) 0, frag_now);
2626#ifdef TE_SOLARIS
2627 /* Before Solaris 11 build 154, Sun ld rejects local group
2628 signature symbols, so make them weak hidden instead. */
2629 symbol_get_bfdsym (sy)->flags |= BSF_WEAK;
2630 S_SET_OTHER (sy, STV_HIDDEN);
2631#else
2632 symbol_get_obj (sy)->local = 1;
2633#endif
2634 symbol_table_insert (sy);
2635 }
2636 elf_group_id (s) = symbol_get_bfdsym (sy);
2637 }
2638}
2639
2640void
2641elf_frob_file (void)
2642{
2643 bfd_map_over_sections (stdoutput, adjust_stab_sections, NULL);
2644
2645#ifdef elf_tc_final_processing
2646 elf_tc_final_processing ();
2647#endif
2648}
2649
2650/* It removes any unneeded versioned symbols from the symbol table. */
2651
2652void
2653elf_frob_file_before_adjust (void)
2654{
2655 if (symbol_rootP)
2656 {
2657 symbolS *symp;
2658
2659 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2660 if (!S_IS_DEFINED (symp))
2661 {
2662 if (symbol_get_obj (symp)->versioned_name)
2663 {
2664 char *p;
2665
2666 /* The @@@ syntax is a special case. If the symbol is
2667 not defined, 2 `@'s will be removed from the
2668 versioned_name. */
2669
2670 p = strchr (symbol_get_obj (symp)->versioned_name,
2671 ELF_VER_CHR);
2672 if (p != NULL && p[1] == ELF_VER_CHR && p[2] == ELF_VER_CHR)
2673 {
2674 size_t l = strlen (&p[3]) + 1;
2675 memmove (&p[1], &p[3], l);
2676 }
2677 if (symbol_used_p (symp) == 0
2678 && symbol_used_in_reloc_p (symp) == 0)
2679 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2680 }
2681
2682 /* If there was .weak foo, but foo was neither defined nor
2683 used anywhere, remove it. */
2684
2685 else if (S_IS_WEAK (symp)
2686 && symbol_used_p (symp) == 0
2687 && symbol_used_in_reloc_p (symp) == 0)
2688 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2689 }
2690 }
2691}
2692
2693/* It is required that we let write_relocs have the opportunity to
2694 optimize away fixups before output has begun, since it is possible
2695 to eliminate all fixups for a section and thus we never should
2696 have generated the relocation section. */
2697
2698void
2699elf_frob_file_after_relocs (void)
2700{
2701 unsigned int i;
2702
2703 /* Set SHT_GROUP section size. */
2704 for (i = 0; i < groups.num_group; i++)
2705 {
2706 asection *s, *head, *group;
2707 bfd_size_type size;
2708
2709 head = groups.head[i];
2710 size = 4;
2711 for (s = head; s != NULL; s = elf_next_in_group (s))
2712 size += (s->flags & SEC_RELOC) != 0 ? 8 : 4;
2713
2714 group = elf_sec_group (head);
2715 subseg_set (group, 0);
2716 bfd_set_section_size (group, size);
2717 group->contents = (unsigned char *) frag_more (size);
2718 frag_now->fr_fix = frag_now_fix_octets ();
2719 frag_wane (frag_now);
2720 }
2721
2722 /* Cleanup hash. */
2723 hash_traverse (groups.indexes, free_section_idx);
2724 hash_die (groups.indexes);
2725
2726#ifdef NEED_ECOFF_DEBUG
2727 if (ECOFF_DEBUGGING)
2728 /* Generate the ECOFF debugging information. */
2729 {
2730 const struct ecoff_debug_swap *debug_swap;
2731 struct ecoff_debug_info debug;
2732 char *buf;
2733 asection *sec;
2734
2735 debug_swap
2736 = get_elf_backend_data (stdoutput)->elf_backend_ecoff_debug_swap;
2737 know (debug_swap != NULL);
2738 ecoff_build_debug (&debug.symbolic_header, &buf, debug_swap);
2739
2740 /* Set up the pointers in debug. */
2741#define SET(ptr, offset, type) \
2742 debug.ptr = (type) (buf + debug.symbolic_header.offset)
2743
2744 SET (line, cbLineOffset, unsigned char *);
2745 SET (external_dnr, cbDnOffset, void *);
2746 SET (external_pdr, cbPdOffset, void *);
2747 SET (external_sym, cbSymOffset, void *);
2748 SET (external_opt, cbOptOffset, void *);
2749 SET (external_aux, cbAuxOffset, union aux_ext *);
2750 SET (ss, cbSsOffset, char *);
2751 SET (external_fdr, cbFdOffset, void *);
2752 SET (external_rfd, cbRfdOffset, void *);
2753 /* ssext and external_ext are set up just below. */
2754
2755#undef SET
2756
2757 /* Set up the external symbols. */
2758 debug.ssext = debug.ssext_end = NULL;
2759 debug.external_ext = debug.external_ext_end = NULL;
2760 if (! bfd_ecoff_debug_externals (stdoutput, &debug, debug_swap, TRUE,
2761 elf_get_extr, elf_set_index))
2762 as_fatal (_("failed to set up debugging information: %s"),
2763 bfd_errmsg (bfd_get_error ()));
2764
2765 sec = bfd_get_section_by_name (stdoutput, ".mdebug");
2766 gas_assert (sec != NULL);
2767
2768 know (!stdoutput->output_has_begun);
2769
2770 /* We set the size of the section, call bfd_set_section_contents
2771 to force the ELF backend to allocate a file position, and then
2772 write out the data. FIXME: Is this really the best way to do
2773 this? */
2774 bfd_set_section_size (sec, bfd_ecoff_debug_size (stdoutput, &debug,
2775 debug_swap));
2776
2777 /* Pass BUF to bfd_set_section_contents because this will
2778 eventually become a call to fwrite, and ISO C prohibits
2779 passing a NULL pointer to a stdio function even if the
2780 pointer will not be used. */
2781 if (! bfd_set_section_contents (stdoutput, sec, buf, 0, 0))
2782 as_fatal (_("can't start writing .mdebug section: %s"),
2783 bfd_errmsg (bfd_get_error ()));
2784
2785 know (stdoutput->output_has_begun);
2786 know (sec->filepos != 0);
2787
2788 if (! bfd_ecoff_write_debug (stdoutput, &debug, debug_swap,
2789 sec->filepos))
2790 as_fatal (_("could not write .mdebug section: %s"),
2791 bfd_errmsg (bfd_get_error ()));
2792 }
2793#endif /* NEED_ECOFF_DEBUG */
2794}
2795
2796static void
2797elf_generate_asm_lineno (void)
2798{
2799#ifdef NEED_ECOFF_DEBUG
2800 if (ECOFF_DEBUGGING)
2801 ecoff_generate_asm_lineno ();
2802#endif
2803}
2804
2805static void
2806elf_process_stab (segT sec ATTRIBUTE_UNUSED,
2807 int what ATTRIBUTE_UNUSED,
2808 const char *string ATTRIBUTE_UNUSED,
2809 int type ATTRIBUTE_UNUSED,
2810 int other ATTRIBUTE_UNUSED,
2811 int desc ATTRIBUTE_UNUSED)
2812{
2813#ifdef NEED_ECOFF_DEBUG
2814 if (ECOFF_DEBUGGING)
2815 ecoff_stab (sec, what, string, type, other, desc);
2816#endif
2817}
2818
2819static int
2820elf_separate_stab_sections (void)
2821{
2822#ifdef NEED_ECOFF_DEBUG
2823 return (!ECOFF_DEBUGGING);
2824#else
2825 return 1;
2826#endif
2827}
2828
2829static void
2830elf_init_stab_section (segT seg)
2831{
2832#ifdef NEED_ECOFF_DEBUG
2833 if (!ECOFF_DEBUGGING)
2834#endif
2835 obj_elf_init_stab_section (seg);
2836}
2837
2838const struct format_ops elf_format_ops =
2839{
2840 bfd_target_elf_flavour,
2841 0, /* dfl_leading_underscore */
2842 1, /* emit_section_symbols */
2843 elf_begin,
2844 elf_file_symbol,
2845 elf_frob_symbol,
2846 elf_frob_file,
2847 elf_frob_file_before_adjust,
2848 0, /* obj_frob_file_before_fix */
2849 elf_frob_file_after_relocs,
2850 elf_s_get_size, elf_s_set_size,
2851 elf_s_get_align, elf_s_set_align,
2852 elf_s_get_other,
2853 elf_s_set_other,
2854 0, /* s_get_desc */
2855 0, /* s_set_desc */
2856 0, /* s_get_type */
2857 0, /* s_set_type */
2858 elf_copy_symbol_attributes,
2859 elf_generate_asm_lineno,
2860 elf_process_stab,
2861 elf_separate_stab_sections,
2862 elf_init_stab_section,
2863 elf_sec_sym_ok_for_reloc,
2864 elf_pop_insert,
2865#ifdef NEED_ECOFF_DEBUG
2866 elf_ecoff_set_ext,
2867#else
2868 0, /* ecoff_set_ext */
2869#endif
2870 elf_obj_read_begin_hook,
2871 elf_obj_symbol_new_hook,
2872 0,
2873 elf_adjust_symtab
2874};
This page took 0.033498 seconds and 4 git commands to generate.