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