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