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