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