]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/obj-elf.c
Update year range in copyright notice of binutils files
[thirdparty/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 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 const 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 = 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 (mbind_p)
1444 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1445 if ((gnu_attr & SHF_GNU_RETAIN) != 0)
1446 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_retain;
1447
1448 attr |= gnu_attr;
1449 }
1450 }
1451
1452 obj_elf_change_section (name, type, attr, entsize, &match, linkonce,
1453 push);
1454
1455 if (linked_to_section_index != -1UL)
1456 {
1457 elf_section_flags (now_seg) |= SHF_LINK_ORDER;
1458 elf_section_data (now_seg)->this_hdr.sh_link = linked_to_section_index;
1459 /* FIXME: Should we perform some sanity checking on the section index ? */
1460 }
1461
1462 if (push && new_subsection != -1)
1463 subseg_set (now_seg, new_subsection);
1464 }
1465
1466 /* Change to the .data section. */
1467
1468 void
1469 obj_elf_data (int i)
1470 {
1471 #ifdef md_flush_pending_output
1472 md_flush_pending_output ();
1473 #endif
1474
1475 previous_section = now_seg;
1476 previous_subsection = now_subseg;
1477 s_data (i);
1478
1479 #ifdef md_elf_section_change_hook
1480 md_elf_section_change_hook ();
1481 #endif
1482 }
1483
1484 /* Change to the .text section. */
1485
1486 void
1487 obj_elf_text (int i)
1488 {
1489 #ifdef md_flush_pending_output
1490 md_flush_pending_output ();
1491 #endif
1492
1493 previous_section = now_seg;
1494 previous_subsection = now_subseg;
1495 s_text (i);
1496
1497 #ifdef md_elf_section_change_hook
1498 md_elf_section_change_hook ();
1499 #endif
1500 }
1501
1502 /* Change to the *ABS* section. */
1503
1504 void
1505 obj_elf_struct (int i)
1506 {
1507 #ifdef md_flush_pending_output
1508 md_flush_pending_output ();
1509 #endif
1510
1511 previous_section = now_seg;
1512 previous_subsection = now_subseg;
1513 s_struct (i);
1514
1515 #ifdef md_elf_section_change_hook
1516 md_elf_section_change_hook ();
1517 #endif
1518 }
1519
1520 static void
1521 obj_elf_subsection (int ignore ATTRIBUTE_UNUSED)
1522 {
1523 int temp;
1524
1525 #ifdef md_flush_pending_output
1526 md_flush_pending_output ();
1527 #endif
1528
1529 previous_section = now_seg;
1530 previous_subsection = now_subseg;
1531
1532 temp = get_absolute_expression ();
1533 subseg_set (now_seg, (subsegT) temp);
1534 demand_empty_rest_of_line ();
1535
1536 #ifdef md_elf_section_change_hook
1537 md_elf_section_change_hook ();
1538 #endif
1539 }
1540
1541 /* This can be called from the processor backends if they change
1542 sections. */
1543
1544 void
1545 obj_elf_section_change_hook (void)
1546 {
1547 previous_section = now_seg;
1548 previous_subsection = now_subseg;
1549 }
1550
1551 void
1552 obj_elf_previous (int ignore ATTRIBUTE_UNUSED)
1553 {
1554 segT new_section;
1555 int new_subsection;
1556
1557 if (previous_section == 0)
1558 {
1559 as_warn (_(".previous without corresponding .section; ignored"));
1560 return;
1561 }
1562
1563 #ifdef md_flush_pending_output
1564 md_flush_pending_output ();
1565 #endif
1566
1567 new_section = previous_section;
1568 new_subsection = previous_subsection;
1569 previous_section = now_seg;
1570 previous_subsection = now_subseg;
1571 subseg_set (new_section, new_subsection);
1572
1573 #ifdef md_elf_section_change_hook
1574 md_elf_section_change_hook ();
1575 #endif
1576 }
1577
1578 static void
1579 obj_elf_popsection (int xxx ATTRIBUTE_UNUSED)
1580 {
1581 struct section_stack *top = section_stack;
1582
1583 if (top == NULL)
1584 {
1585 as_warn (_(".popsection without corresponding .pushsection; ignored"));
1586 return;
1587 }
1588
1589 #ifdef md_flush_pending_output
1590 md_flush_pending_output ();
1591 #endif
1592
1593 section_stack = top->next;
1594 previous_section = top->prev_seg;
1595 previous_subsection = top->prev_subseg;
1596 subseg_set (top->seg, top->subseg);
1597 free (top);
1598
1599 #ifdef md_elf_section_change_hook
1600 md_elf_section_change_hook ();
1601 #endif
1602 }
1603
1604 static void
1605 obj_elf_line (int ignore ATTRIBUTE_UNUSED)
1606 {
1607 /* Assume delimiter is part of expression. BSD4.2 as fails with
1608 delightful bug, so we are not being incompatible here. */
1609 new_logical_line (NULL, get_absolute_expression ());
1610 demand_empty_rest_of_line ();
1611 }
1612
1613 static struct elf_versioned_name_list *
1614 obj_elf_find_and_add_versioned_name (const char *version_name,
1615 const char *sym_name,
1616 const char *ver,
1617 struct elf_obj_sy *sy_obj)
1618 {
1619 struct elf_versioned_name_list *versioned_name;
1620 const char *p;
1621
1622 for (p = ver + 1; *p == ELF_VER_CHR; p++)
1623 ;
1624
1625 /* NB: Since some tests in ld/testsuite/ld-elfvers have no version
1626 names, we have to disable this. */
1627 if (0 && *p == '\0')
1628 {
1629 as_bad (_("missing version name in `%s' for symbol `%s'"),
1630 version_name, sym_name);
1631 return NULL;
1632 }
1633
1634 versioned_name = sy_obj->versioned_name;
1635
1636 switch (p - ver)
1637 {
1638 case 1:
1639 case 2:
1640 break;
1641 case 3:
1642 if (sy_obj->rename)
1643 {
1644 if (strcmp (versioned_name->name, version_name) == 0)
1645 return versioned_name;
1646 else
1647 {
1648 as_bad (_("only one version name with `@@@' is allowed "
1649 "for symbol `%s'"), sym_name);
1650 return NULL;
1651 }
1652 }
1653 sy_obj->rename = TRUE;
1654 break;
1655 default:
1656 as_bad (_("invalid version name '%s' for symbol `%s'"),
1657 version_name, sym_name);
1658 return NULL;
1659 }
1660
1661 for (;
1662 versioned_name != NULL;
1663 versioned_name = versioned_name->next)
1664 if (strcmp (versioned_name->name, version_name) == 0)
1665 return versioned_name;
1666
1667 /* Add this versioned name to the head of the list, */
1668 versioned_name = (struct elf_versioned_name_list *)
1669 xmalloc (sizeof (*versioned_name));
1670 versioned_name->name = xstrdup (version_name);
1671 versioned_name->next = sy_obj->versioned_name;
1672 sy_obj->versioned_name = versioned_name;
1673
1674 return versioned_name;
1675 }
1676
1677 /* This handles the .symver pseudo-op, which is used to specify a
1678 symbol version. The syntax is ``.symver NAME,SYMVERNAME''.
1679 SYMVERNAME may contain ELF_VER_CHR ('@') characters. This
1680 pseudo-op causes the assembler to emit a symbol named SYMVERNAME
1681 with the same value as the symbol NAME. */
1682
1683 static void
1684 obj_elf_symver (int ignore ATTRIBUTE_UNUSED)
1685 {
1686 char *name;
1687 const char *sym_name;
1688 char c;
1689 char old_lexat;
1690 symbolS *sym;
1691 struct elf_obj_sy *sy_obj;
1692 char *p;
1693
1694 sym = get_sym_from_input_line_and_check ();
1695
1696 if (*input_line_pointer != ',')
1697 {
1698 as_bad (_("expected comma after name in .symver"));
1699 ignore_rest_of_line ();
1700 return;
1701 }
1702
1703 ++input_line_pointer;
1704 SKIP_WHITESPACE ();
1705
1706 /* Temporarily include '@' in symbol names. */
1707 old_lexat = lex_type[(unsigned char) '@'];
1708 lex_type[(unsigned char) '@'] |= LEX_NAME;
1709 c = get_symbol_name (& name);
1710 lex_type[(unsigned char) '@'] = old_lexat;
1711 sym_name = S_GET_NAME (sym);
1712
1713 if (S_IS_COMMON (sym))
1714 {
1715 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
1716 name, sym_name);
1717 ignore_rest_of_line ();
1718 return;
1719 }
1720
1721 p = strchr (name, ELF_VER_CHR);
1722 if (p == NULL)
1723 {
1724 as_bad (_("missing version name in `%s' for symbol `%s'"),
1725 name, sym_name);
1726 ignore_rest_of_line ();
1727 return;
1728 }
1729
1730 sy_obj = symbol_get_obj (sym);
1731 if (obj_elf_find_and_add_versioned_name (name, sym_name,
1732 p, sy_obj) == NULL)
1733 {
1734 sy_obj->bad_version = TRUE;
1735 ignore_rest_of_line ();
1736 return;
1737 }
1738
1739 (void) restore_line_pointer (c);
1740
1741 if (*input_line_pointer == ',')
1742 {
1743 char *save = input_line_pointer;
1744
1745 ++input_line_pointer;
1746 SKIP_WHITESPACE ();
1747 if (strncmp (input_line_pointer, "local", 5) == 0)
1748 {
1749 input_line_pointer += 5;
1750 sy_obj->visibility = visibility_local;
1751 }
1752 else if (strncmp (input_line_pointer, "hidden", 6) == 0)
1753 {
1754 input_line_pointer += 6;
1755 sy_obj->visibility = visibility_hidden;
1756 }
1757 else if (strncmp (input_line_pointer, "remove", 6) == 0)
1758 {
1759 input_line_pointer += 6;
1760 sy_obj->visibility = visibility_remove;
1761 }
1762 else
1763 input_line_pointer = save;
1764 }
1765
1766 demand_empty_rest_of_line ();
1767 }
1768
1769 /* This handles the .vtable_inherit pseudo-op, which is used to indicate
1770 to the linker the hierarchy in which a particular table resides. The
1771 syntax is ".vtable_inherit CHILDNAME, PARENTNAME". */
1772
1773 struct fix *
1774 obj_elf_get_vtable_inherit (void)
1775 {
1776 char *cname, *pname;
1777 symbolS *csym, *psym;
1778 char c, bad = 0;
1779
1780 if (*input_line_pointer == '#')
1781 ++input_line_pointer;
1782
1783 c = get_symbol_name (& cname);
1784 csym = symbol_find (cname);
1785
1786 /* GCFIXME: should check that we don't have two .vtable_inherits for
1787 the same child symbol. Also, we can currently only do this if the
1788 child symbol is already exists and is placed in a fragment. */
1789
1790 if (csym == NULL || symbol_get_frag (csym) == NULL)
1791 {
1792 as_bad (_("expected `%s' to have already been set for .vtable_inherit"),
1793 cname);
1794 bad = 1;
1795 }
1796
1797 *input_line_pointer = c;
1798
1799 SKIP_WHITESPACE_AFTER_NAME ();
1800 if (*input_line_pointer != ',')
1801 {
1802 as_bad (_("expected comma after name in .vtable_inherit"));
1803 ignore_rest_of_line ();
1804 return NULL;
1805 }
1806
1807 ++input_line_pointer;
1808 SKIP_WHITESPACE ();
1809
1810 if (*input_line_pointer == '#')
1811 ++input_line_pointer;
1812
1813 if (input_line_pointer[0] == '0'
1814 && (input_line_pointer[1] == '\0'
1815 || ISSPACE (input_line_pointer[1])))
1816 {
1817 psym = section_symbol (absolute_section);
1818 ++input_line_pointer;
1819 }
1820 else
1821 {
1822 c = get_symbol_name (& pname);
1823 psym = symbol_find_or_make (pname);
1824 restore_line_pointer (c);
1825 }
1826
1827 demand_empty_rest_of_line ();
1828
1829 if (bad)
1830 return NULL;
1831
1832 gas_assert (symbol_get_value_expression (csym)->X_op == O_constant);
1833 return fix_new (symbol_get_frag (csym),
1834 symbol_get_value_expression (csym)->X_add_number,
1835 0, psym, 0, 0, BFD_RELOC_VTABLE_INHERIT);
1836 }
1837
1838 /* This is a version of obj_elf_get_vtable_inherit() that is
1839 suitable for use in struct _pseudo_type tables. */
1840
1841 void
1842 obj_elf_vtable_inherit (int ignore ATTRIBUTE_UNUSED)
1843 {
1844 (void) obj_elf_get_vtable_inherit ();
1845 }
1846
1847 /* This handles the .vtable_entry pseudo-op, which is used to indicate
1848 to the linker that a vtable slot was used. The syntax is
1849 ".vtable_entry tablename, offset". */
1850
1851 struct fix *
1852 obj_elf_get_vtable_entry (void)
1853 {
1854 symbolS *sym;
1855 offsetT offset;
1856
1857 if (*input_line_pointer == '#')
1858 ++input_line_pointer;
1859
1860 sym = get_sym_from_input_line_and_check ();
1861 if (*input_line_pointer != ',')
1862 {
1863 as_bad (_("expected comma after name in .vtable_entry"));
1864 ignore_rest_of_line ();
1865 return NULL;
1866 }
1867
1868 ++input_line_pointer;
1869 if (*input_line_pointer == '#')
1870 ++input_line_pointer;
1871
1872 offset = get_absolute_expression ();
1873
1874 demand_empty_rest_of_line ();
1875
1876 return fix_new (frag_now, frag_now_fix (), 0, sym, offset, 0,
1877 BFD_RELOC_VTABLE_ENTRY);
1878 }
1879
1880 /* This is a version of obj_elf_get_vtable_entry() that is
1881 suitable for use in struct _pseudo_type tables. */
1882
1883 void
1884 obj_elf_vtable_entry (int ignore ATTRIBUTE_UNUSED)
1885 {
1886 (void) obj_elf_get_vtable_entry ();
1887 }
1888
1889 #define skip_whitespace(str) do { if (*(str) == ' ') ++(str); } while (0)
1890
1891 static inline int
1892 skip_past_char (char ** str, char c)
1893 {
1894 if (**str == c)
1895 {
1896 (*str)++;
1897 return 0;
1898 }
1899 else
1900 return -1;
1901 }
1902 #define skip_past_comma(str) skip_past_char (str, ',')
1903
1904 /* A list of attributes that have been explicitly set by the assembly code.
1905 VENDOR is the vendor id, BASE is the tag shifted right by the number
1906 of bits in MASK, and bit N of MASK is set if tag BASE+N has been set. */
1907 struct recorded_attribute_info {
1908 struct recorded_attribute_info *next;
1909 int vendor;
1910 unsigned int base;
1911 unsigned long mask;
1912 };
1913 static struct recorded_attribute_info *recorded_attributes;
1914
1915 /* Record that we have seen an explicit specification of attribute TAG
1916 for vendor VENDOR. */
1917
1918 static void
1919 record_attribute (int vendor, unsigned int tag)
1920 {
1921 unsigned int base;
1922 unsigned long mask;
1923 struct recorded_attribute_info *rai;
1924
1925 base = tag / (8 * sizeof (rai->mask));
1926 mask = 1UL << (tag % (8 * sizeof (rai->mask)));
1927 for (rai = recorded_attributes; rai; rai = rai->next)
1928 if (rai->vendor == vendor && rai->base == base)
1929 {
1930 rai->mask |= mask;
1931 return;
1932 }
1933
1934 rai = XNEW (struct recorded_attribute_info);
1935 rai->next = recorded_attributes;
1936 rai->vendor = vendor;
1937 rai->base = base;
1938 rai->mask = mask;
1939 recorded_attributes = rai;
1940 }
1941
1942 /* Return true if we have seen an explicit specification of attribute TAG
1943 for vendor VENDOR. */
1944
1945 bfd_boolean
1946 obj_elf_seen_attribute (int vendor, unsigned int tag)
1947 {
1948 unsigned int base;
1949 unsigned long mask;
1950 struct recorded_attribute_info *rai;
1951
1952 base = tag / (8 * sizeof (rai->mask));
1953 mask = 1UL << (tag % (8 * sizeof (rai->mask)));
1954 for (rai = recorded_attributes; rai; rai = rai->next)
1955 if (rai->vendor == vendor && rai->base == base)
1956 return (rai->mask & mask) != 0;
1957 return FALSE;
1958 }
1959
1960 /* Parse an attribute directive for VENDOR.
1961 Returns the attribute number read, or zero on error. */
1962
1963 int
1964 obj_elf_vendor_attribute (int vendor)
1965 {
1966 expressionS exp;
1967 int type;
1968 int tag;
1969 unsigned int i = 0;
1970 char *s = NULL;
1971
1972 /* Read the first number or name. */
1973 skip_whitespace (input_line_pointer);
1974 s = input_line_pointer;
1975 if (ISDIGIT (*input_line_pointer))
1976 {
1977 expression (& exp);
1978 if (exp.X_op != O_constant)
1979 goto bad;
1980 tag = exp.X_add_number;
1981 }
1982 else
1983 {
1984 char *name;
1985
1986 /* A name may contain '_', but no other punctuation. */
1987 for (; ISALNUM (*input_line_pointer) || *input_line_pointer == '_';
1988 ++input_line_pointer)
1989 i++;
1990 if (i == 0)
1991 goto bad;
1992
1993 name = xstrndup (s, i);
1994
1995 #ifndef CONVERT_SYMBOLIC_ATTRIBUTE
1996 #define CONVERT_SYMBOLIC_ATTRIBUTE(a) -1
1997 #endif
1998
1999 tag = CONVERT_SYMBOLIC_ATTRIBUTE (name);
2000 if (tag == -1)
2001 {
2002 as_bad (_("Attribute name not recognised: %s"), name);
2003 ignore_rest_of_line ();
2004 free (name);
2005 return 0;
2006 }
2007 free (name);
2008 }
2009
2010 type = _bfd_elf_obj_attrs_arg_type (stdoutput, vendor, tag);
2011
2012 if (skip_past_comma (&input_line_pointer) == -1)
2013 goto bad;
2014 if (type & 1)
2015 {
2016 expression (& exp);
2017 if (exp.X_op != O_constant)
2018 {
2019 as_bad (_("expected numeric constant"));
2020 ignore_rest_of_line ();
2021 return 0;
2022 }
2023 i = exp.X_add_number;
2024 }
2025 if ((type & 3) == 3
2026 && skip_past_comma (&input_line_pointer) == -1)
2027 {
2028 as_bad (_("expected comma"));
2029 ignore_rest_of_line ();
2030 return 0;
2031 }
2032 if (type & 2)
2033 {
2034 int len;
2035
2036 skip_whitespace (input_line_pointer);
2037 if (*input_line_pointer != '"')
2038 goto bad_string;
2039 s = demand_copy_C_string (&len);
2040 }
2041
2042 record_attribute (vendor, tag);
2043 switch (type & 3)
2044 {
2045 case 3:
2046 bfd_elf_add_obj_attr_int_string (stdoutput, vendor, tag, i, s);
2047 break;
2048 case 2:
2049 bfd_elf_add_obj_attr_string (stdoutput, vendor, tag, s);
2050 break;
2051 case 1:
2052 bfd_elf_add_obj_attr_int (stdoutput, vendor, tag, i);
2053 break;
2054 default:
2055 abort ();
2056 }
2057
2058 demand_empty_rest_of_line ();
2059 return tag;
2060 bad_string:
2061 as_bad (_("bad string constant"));
2062 ignore_rest_of_line ();
2063 return 0;
2064 bad:
2065 as_bad (_("expected <tag> , <value>"));
2066 ignore_rest_of_line ();
2067 return 0;
2068 }
2069
2070 /* Parse a .gnu_attribute directive. */
2071
2072 static void
2073 obj_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED)
2074 {
2075 obj_elf_vendor_attribute (OBJ_ATTR_GNU);
2076 }
2077
2078 void
2079 elf_obj_read_begin_hook (void)
2080 {
2081 #ifdef NEED_ECOFF_DEBUG
2082 if (ECOFF_DEBUGGING)
2083 ecoff_read_begin_hook ();
2084 #endif
2085 }
2086
2087 void
2088 elf_obj_symbol_new_hook (symbolS *symbolP)
2089 {
2090 struct elf_obj_sy *sy_obj;
2091
2092 sy_obj = symbol_get_obj (symbolP);
2093 sy_obj->size = NULL;
2094 sy_obj->versioned_name = NULL;
2095
2096 #ifdef NEED_ECOFF_DEBUG
2097 if (ECOFF_DEBUGGING)
2098 ecoff_symbol_new_hook (symbolP);
2099 #endif
2100 }
2101
2102 /* Deduplicate size expressions. We might get into trouble with
2103 multiple freeing or use after free if we leave them pointing to the
2104 same expressionS. */
2105
2106 void
2107 elf_obj_symbol_clone_hook (symbolS *newsym, symbolS *orgsym ATTRIBUTE_UNUSED)
2108 {
2109 struct elf_obj_sy *newelf = symbol_get_obj (newsym);
2110 if (newelf->size)
2111 {
2112 expressionS *exp = XNEW (expressionS);
2113 *exp = *newelf->size;
2114 newelf->size = exp;
2115 }
2116 }
2117
2118 /* When setting one symbol equal to another, by default we probably
2119 want them to have the same "size", whatever it means in the current
2120 context. */
2121
2122 void
2123 elf_copy_symbol_attributes (symbolS *dest, symbolS *src)
2124 {
2125 struct elf_obj_sy *srcelf = symbol_get_obj (src);
2126 struct elf_obj_sy *destelf = symbol_get_obj (dest);
2127 if (srcelf->size)
2128 {
2129 if (destelf->size == NULL)
2130 destelf->size = XNEW (expressionS);
2131 *destelf->size = *srcelf->size;
2132 }
2133 else
2134 {
2135 free (destelf->size);
2136 destelf->size = NULL;
2137 }
2138 S_SET_SIZE (dest, S_GET_SIZE (src));
2139 /* Don't copy visibility. */
2140 S_SET_OTHER (dest, (ELF_ST_VISIBILITY (S_GET_OTHER (dest))
2141 | (S_GET_OTHER (src) & ~ELF_ST_VISIBILITY (-1))));
2142 }
2143
2144 void
2145 obj_elf_version (int ignore ATTRIBUTE_UNUSED)
2146 {
2147 char *name;
2148 unsigned int c;
2149 char *p;
2150 asection *seg = now_seg;
2151 subsegT subseg = now_subseg;
2152 Elf_Internal_Note i_note;
2153 Elf_External_Note e_note;
2154 asection *note_secp = NULL;
2155
2156 SKIP_WHITESPACE ();
2157 if (*input_line_pointer == '\"')
2158 {
2159 unsigned int len;
2160
2161 ++input_line_pointer; /* -> 1st char of string. */
2162 name = input_line_pointer;
2163
2164 while (is_a_char (c = next_char_of_string ()))
2165 ;
2166 c = *input_line_pointer;
2167 *input_line_pointer = '\0';
2168 *(input_line_pointer - 1) = '\0';
2169 *input_line_pointer = c;
2170
2171 /* Create the .note section. */
2172 note_secp = subseg_new (".note", 0);
2173 bfd_set_section_flags (note_secp, SEC_HAS_CONTENTS | SEC_READONLY);
2174 record_alignment (note_secp, 2);
2175
2176 /* Process the version string. */
2177 len = strlen (name) + 1;
2178
2179 /* PR 3456: Although the name field is padded out to an 4-byte
2180 boundary, the namesz field should not be adjusted. */
2181 i_note.namesz = len;
2182 i_note.descsz = 0; /* No description. */
2183 i_note.type = NT_VERSION;
2184 p = frag_more (sizeof (e_note.namesz));
2185 md_number_to_chars (p, i_note.namesz, sizeof (e_note.namesz));
2186 p = frag_more (sizeof (e_note.descsz));
2187 md_number_to_chars (p, i_note.descsz, sizeof (e_note.descsz));
2188 p = frag_more (sizeof (e_note.type));
2189 md_number_to_chars (p, i_note.type, sizeof (e_note.type));
2190 p = frag_more (len);
2191 memcpy (p, name, len);
2192
2193 frag_align (2, 0, 0);
2194
2195 subseg_set (seg, subseg);
2196 }
2197 else
2198 as_bad (_("expected quoted string"));
2199
2200 demand_empty_rest_of_line ();
2201 }
2202
2203 static void
2204 obj_elf_size (int ignore ATTRIBUTE_UNUSED)
2205 {
2206 char *name;
2207 char c = get_symbol_name (&name);
2208 char *p;
2209 expressionS exp;
2210 symbolS *sym;
2211
2212 p = input_line_pointer;
2213 *p = c;
2214 SKIP_WHITESPACE_AFTER_NAME ();
2215 if (*input_line_pointer != ',')
2216 {
2217 *p = 0;
2218 as_bad (_("expected comma after name `%s' in .size directive"), name);
2219 *p = c;
2220 ignore_rest_of_line ();
2221 return;
2222 }
2223 input_line_pointer++;
2224 expression (&exp);
2225 if (exp.X_op == O_absent)
2226 {
2227 as_bad (_("missing expression in .size directive"));
2228 exp.X_op = O_constant;
2229 exp.X_add_number = 0;
2230 }
2231 *p = 0;
2232 sym = symbol_find_or_make (name);
2233 *p = c;
2234 if (exp.X_op == O_constant)
2235 {
2236 S_SET_SIZE (sym, exp.X_add_number);
2237 xfree (symbol_get_obj (sym)->size);
2238 symbol_get_obj (sym)->size = NULL;
2239 }
2240 else
2241 {
2242 symbol_get_obj (sym)->size = XNEW (expressionS);
2243 *symbol_get_obj (sym)->size = exp;
2244 }
2245 demand_empty_rest_of_line ();
2246 }
2247
2248 /* Handle the ELF .type pseudo-op. This sets the type of a symbol.
2249 There are six syntaxes:
2250
2251 The first (used on Solaris) is
2252 .type SYM,#function
2253 The second (used on UnixWare) is
2254 .type SYM,@function
2255 The third (reportedly to be used on Irix 6.0) is
2256 .type SYM STT_FUNC
2257 The fourth (used on NetBSD/Arm and Linux/ARM) is
2258 .type SYM,%function
2259 The fifth (used on SVR4/860) is
2260 .type SYM,"function"
2261 The sixth (emitted by recent SunPRO under Solaris) is
2262 .type SYM,[0-9]
2263 where the integer is the STT_* value.
2264 */
2265
2266 static char *
2267 obj_elf_type_name (char *cp)
2268 {
2269 char *p;
2270
2271 p = input_line_pointer;
2272 if (*input_line_pointer >= '0'
2273 && *input_line_pointer <= '9')
2274 {
2275 while (*input_line_pointer >= '0'
2276 && *input_line_pointer <= '9')
2277 ++input_line_pointer;
2278 *cp = *input_line_pointer;
2279 *input_line_pointer = '\0';
2280 }
2281 else
2282 *cp = get_symbol_name (&p);
2283
2284 return p;
2285 }
2286
2287 static void
2288 obj_elf_type (int ignore ATTRIBUTE_UNUSED)
2289 {
2290 char c;
2291 int type;
2292 const char *type_name;
2293 symbolS *sym;
2294 elf_symbol_type *elfsym;
2295
2296 sym = get_sym_from_input_line_and_check ();
2297 c = *input_line_pointer;
2298 elfsym = (elf_symbol_type *) symbol_get_bfdsym (sym);
2299
2300 if (*input_line_pointer == ',')
2301 ++input_line_pointer;
2302
2303 SKIP_WHITESPACE ();
2304 if ( *input_line_pointer == '#'
2305 || *input_line_pointer == '@'
2306 || *input_line_pointer == '"'
2307 || *input_line_pointer == '%')
2308 ++input_line_pointer;
2309
2310 type_name = obj_elf_type_name (& c);
2311
2312 type = 0;
2313 if (strcmp (type_name, "function") == 0
2314 || strcmp (type_name, "2") == 0
2315 || strcmp (type_name, "STT_FUNC") == 0)
2316 type = BSF_FUNCTION;
2317 else if (strcmp (type_name, "object") == 0
2318 || strcmp (type_name, "1") == 0
2319 || strcmp (type_name, "STT_OBJECT") == 0)
2320 type = BSF_OBJECT;
2321 else if (strcmp (type_name, "tls_object") == 0
2322 || strcmp (type_name, "6") == 0
2323 || strcmp (type_name, "STT_TLS") == 0)
2324 type = BSF_OBJECT | BSF_THREAD_LOCAL;
2325 else if (strcmp (type_name, "notype") == 0
2326 || strcmp (type_name, "0") == 0
2327 || strcmp (type_name, "STT_NOTYPE") == 0)
2328 ;
2329 else if (strcmp (type_name, "common") == 0
2330 || strcmp (type_name, "5") == 0
2331 || strcmp (type_name, "STT_COMMON") == 0)
2332 {
2333 type = BSF_OBJECT;
2334
2335 if (! S_IS_COMMON (sym))
2336 {
2337 if (S_IS_VOLATILE (sym))
2338 {
2339 sym = symbol_clone (sym, 1);
2340 S_SET_SEGMENT (sym, bfd_com_section_ptr);
2341 S_SET_VALUE (sym, 0);
2342 S_SET_EXTERNAL (sym);
2343 symbol_set_frag (sym, &zero_address_frag);
2344 S_CLEAR_VOLATILE (sym);
2345 }
2346 else if (S_IS_DEFINED (sym) || symbol_equated_p (sym))
2347 as_bad (_("symbol '%s' is already defined"), S_GET_NAME (sym));
2348 else
2349 {
2350 /* FIXME: Is it safe to just change the section ? */
2351 S_SET_SEGMENT (sym, bfd_com_section_ptr);
2352 S_SET_VALUE (sym, 0);
2353 S_SET_EXTERNAL (sym);
2354 }
2355 }
2356 }
2357 else if (strcmp (type_name, "gnu_indirect_function") == 0
2358 || strcmp (type_name, "10") == 0
2359 || strcmp (type_name, "STT_GNU_IFUNC") == 0)
2360 {
2361 const struct elf_backend_data *bed;
2362
2363 bed = get_elf_backend_data (stdoutput);
2364 if (bed->elf_osabi != ELFOSABI_NONE
2365 && bed->elf_osabi != ELFOSABI_GNU
2366 && bed->elf_osabi != ELFOSABI_FREEBSD)
2367 as_bad (_("symbol type \"%s\" is supported only by GNU "
2368 "and FreeBSD targets"), type_name);
2369 /* MIPS targets do not support IFUNCS. */
2370 else if (bed->target_id == MIPS_ELF_DATA)
2371 as_bad (_("symbol type \"%s\" is not supported by "
2372 "MIPS targets"), type_name);
2373 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
2374 type = BSF_FUNCTION | BSF_GNU_INDIRECT_FUNCTION;
2375 }
2376 else if (strcmp (type_name, "gnu_unique_object") == 0)
2377 {
2378 const struct elf_backend_data *bed;
2379
2380 bed = get_elf_backend_data (stdoutput);
2381 if (bed->elf_osabi != ELFOSABI_NONE
2382 && bed->elf_osabi != ELFOSABI_GNU)
2383 as_bad (_("symbol type \"%s\" is supported only by GNU targets"),
2384 type_name);
2385 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_unique;
2386 type = BSF_OBJECT | BSF_GNU_UNIQUE;
2387 }
2388 #ifdef md_elf_symbol_type
2389 else if ((type = md_elf_symbol_type (type_name, sym, elfsym)) != -1)
2390 ;
2391 #endif
2392 else
2393 as_bad (_("unrecognized symbol type \"%s\""), type_name);
2394
2395 *input_line_pointer = c;
2396
2397 if (*input_line_pointer == '"')
2398 ++input_line_pointer;
2399
2400 #ifdef md_elf_symbol_type_change
2401 if (!md_elf_symbol_type_change (sym, elfsym, type))
2402 #endif
2403 {
2404 flagword mask = BSF_FUNCTION | BSF_OBJECT;
2405
2406 if (type != BSF_FUNCTION)
2407 mask |= BSF_GNU_INDIRECT_FUNCTION;
2408 if (type != BSF_OBJECT)
2409 {
2410 mask |= BSF_GNU_UNIQUE | BSF_THREAD_LOCAL;
2411
2412 if (S_IS_COMMON (sym))
2413 {
2414 as_bad (_("cannot change type of common symbol '%s'"),
2415 S_GET_NAME (sym));
2416 mask = type = 0;
2417 }
2418 }
2419
2420 /* Don't warn when changing to STT_NOTYPE. */
2421 if (type)
2422 {
2423 flagword new = (elfsym->symbol.flags & ~mask) | type;
2424
2425 if (new != (elfsym->symbol.flags | type))
2426 as_warn (_("symbol '%s' already has its type set"), S_GET_NAME (sym));
2427 elfsym->symbol.flags = new;
2428 }
2429 else
2430 elfsym->symbol.flags &= ~mask;
2431 }
2432
2433 demand_empty_rest_of_line ();
2434 }
2435
2436 static void
2437 obj_elf_ident (int ignore ATTRIBUTE_UNUSED)
2438 {
2439 static segT comment_section;
2440 segT old_section = now_seg;
2441 int old_subsection = now_subseg;
2442
2443 #ifdef md_flush_pending_output
2444 md_flush_pending_output ();
2445 #endif
2446
2447 if (!comment_section)
2448 {
2449 char *p;
2450 comment_section = subseg_new (".comment", 0);
2451 bfd_set_section_flags (comment_section, (SEC_READONLY | SEC_HAS_CONTENTS
2452 | SEC_MERGE | SEC_STRINGS));
2453 comment_section->entsize = 1;
2454 #ifdef md_elf_section_change_hook
2455 md_elf_section_change_hook ();
2456 #endif
2457 p = frag_more (1);
2458 *p = 0;
2459 }
2460 else
2461 subseg_set (comment_section, 0);
2462 stringer (8 + 1);
2463 subseg_set (old_section, old_subsection);
2464 }
2465
2466 #ifdef INIT_STAB_SECTION
2467
2468 /* The first entry in a .stabs section is special. */
2469
2470 void
2471 obj_elf_init_stab_section (segT seg)
2472 {
2473 const char *file;
2474 char *p;
2475 char *stabstr_name;
2476 unsigned int stroff;
2477
2478 /* Force the section to align to a longword boundary. Without this,
2479 UnixWare ar crashes. */
2480 bfd_set_section_alignment (seg, 2);
2481
2482 /* Make space for this first symbol. */
2483 p = frag_more (12);
2484 /* Zero it out. */
2485 memset (p, 0, 12);
2486 file = remap_debug_filename (as_where (NULL));
2487 stabstr_name = concat (segment_name (seg), "str", (char *) NULL);
2488 stroff = get_stab_string_offset (file, stabstr_name, TRUE);
2489 know (stroff == 1 || (stroff == 0 && file[0] == '\0'));
2490 md_number_to_chars (p, stroff, 4);
2491 seg_info (seg)->stabu.p = p;
2492 xfree ((char *) file);
2493 }
2494
2495 #endif
2496
2497 /* Fill in the counts in the first entry in a .stabs section. */
2498
2499 static void
2500 adjust_stab_sections (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
2501 {
2502 char *name;
2503 asection *strsec;
2504 char *p;
2505 int strsz, nsyms;
2506
2507 if (strncmp (".stab", sec->name, 5))
2508 return;
2509 if (!strcmp ("str", sec->name + strlen (sec->name) - 3))
2510 return;
2511
2512 name = concat (sec->name, "str", NULL);
2513 strsec = bfd_get_section_by_name (abfd, name);
2514 if (strsec)
2515 strsz = bfd_section_size (strsec);
2516 else
2517 strsz = 0;
2518 nsyms = bfd_section_size (sec) / 12 - 1;
2519
2520 p = seg_info (sec)->stabu.p;
2521 gas_assert (p != 0);
2522
2523 bfd_h_put_16 (abfd, nsyms, p + 6);
2524 bfd_h_put_32 (abfd, strsz, p + 8);
2525 free (name);
2526 }
2527
2528 #ifdef NEED_ECOFF_DEBUG
2529
2530 /* This function is called by the ECOFF code. It is supposed to
2531 record the external symbol information so that the backend can
2532 write it out correctly. The ELF backend doesn't actually handle
2533 this at the moment, so we do it ourselves. We save the information
2534 in the symbol. */
2535
2536 #ifdef OBJ_MAYBE_ELF
2537 static
2538 #endif
2539 void
2540 elf_ecoff_set_ext (symbolS *sym, struct ecoff_extr *ext)
2541 {
2542 symbol_get_bfdsym (sym)->udata.p = ext;
2543 }
2544
2545 /* This function is called by bfd_ecoff_debug_externals. It is
2546 supposed to *EXT to the external symbol information, and return
2547 whether the symbol should be used at all. */
2548
2549 static bfd_boolean
2550 elf_get_extr (asymbol *sym, EXTR *ext)
2551 {
2552 if (sym->udata.p == NULL)
2553 return FALSE;
2554 *ext = *(EXTR *) sym->udata.p;
2555 return TRUE;
2556 }
2557
2558 /* This function is called by bfd_ecoff_debug_externals. It has
2559 nothing to do for ELF. */
2560
2561 static void
2562 elf_set_index (asymbol *sym ATTRIBUTE_UNUSED,
2563 bfd_size_type indx ATTRIBUTE_UNUSED)
2564 {
2565 }
2566
2567 #endif /* NEED_ECOFF_DEBUG */
2568
2569 void
2570 elf_frob_symbol (symbolS *symp, int *puntp)
2571 {
2572 struct elf_obj_sy *sy_obj;
2573 expressionS *size;
2574 struct elf_versioned_name_list *versioned_name;
2575
2576 #ifdef NEED_ECOFF_DEBUG
2577 if (ECOFF_DEBUGGING)
2578 ecoff_frob_symbol (symp);
2579 #endif
2580
2581 sy_obj = symbol_get_obj (symp);
2582
2583 size = sy_obj->size;
2584 if (size != NULL)
2585 {
2586 if (resolve_expression (size)
2587 && size->X_op == O_constant)
2588 S_SET_SIZE (symp, size->X_add_number);
2589 else
2590 {
2591 if (!flag_allow_nonconst_size)
2592 as_bad (_(".size expression for %s "
2593 "does not evaluate to a constant"), S_GET_NAME (symp));
2594 else
2595 as_warn (_(".size expression for %s "
2596 "does not evaluate to a constant"), S_GET_NAME (symp));
2597 }
2598 free (sy_obj->size);
2599 sy_obj->size = NULL;
2600 }
2601
2602 versioned_name = sy_obj->versioned_name;
2603 if (versioned_name)
2604 {
2605 /* This symbol was given a new name with the .symver directive.
2606 If this is an external reference, just rename the symbol to
2607 include the version string. This will make the relocs be
2608 against the correct versioned symbol. */
2609
2610 /* We will have already reported an version error. */
2611 if (sy_obj->bad_version)
2612 *puntp = TRUE;
2613 /* elf_frob_file_before_adjust only allows one version symbol for
2614 renamed symbol. */
2615 else if (sy_obj->rename)
2616 S_SET_NAME (symp, versioned_name->name);
2617 else if (S_IS_COMMON (symp))
2618 {
2619 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
2620 versioned_name->name, S_GET_NAME (symp));
2621 *puntp = TRUE;
2622 }
2623 else
2624 {
2625 asymbol *bfdsym;
2626 elf_symbol_type *elfsym;
2627
2628 /* This is a definition. Add an alias for each version.
2629 FIXME: Using an alias will permit the debugging information
2630 to refer to the right symbol. However, it's not clear
2631 whether it is the best approach. */
2632
2633 /* FIXME: Creating a new symbol here is risky. We're
2634 in the final loop over the symbol table. We can
2635 get away with it only because the symbol goes to
2636 the end of the list, where the loop will still see
2637 it. It would probably be better to do this in
2638 obj_frob_file_before_adjust. */
2639 for (; versioned_name != NULL;
2640 versioned_name = versioned_name->next)
2641 {
2642 symbolS *symp2 = symbol_find_or_make (versioned_name->name);
2643
2644 S_SET_SEGMENT (symp2, S_GET_SEGMENT (symp));
2645
2646 /* Subtracting out the frag address here is a hack
2647 because we are in the middle of the final loop. */
2648 S_SET_VALUE (symp2,
2649 (S_GET_VALUE (symp)
2650 - (symbol_get_frag (symp)->fr_address
2651 / OCTETS_PER_BYTE)));
2652
2653 symbol_set_frag (symp2, symbol_get_frag (symp));
2654
2655 /* This will copy over the size information. */
2656 copy_symbol_attributes (symp2, symp);
2657
2658 S_SET_OTHER (symp2, S_GET_OTHER (symp));
2659
2660 if (S_IS_WEAK (symp))
2661 S_SET_WEAK (symp2);
2662
2663 if (S_IS_EXTERNAL (symp))
2664 S_SET_EXTERNAL (symp2);
2665 }
2666
2667 switch (symbol_get_obj (symp)->visibility)
2668 {
2669 case visibility_unchanged:
2670 break;
2671 case visibility_hidden:
2672 bfdsym = symbol_get_bfdsym (symp);
2673 elfsym = elf_symbol_from (bfdsym);
2674 elfsym->internal_elf_sym.st_other &= ~3;
2675 elfsym->internal_elf_sym.st_other |= STV_HIDDEN;
2676 break;
2677 case visibility_remove:
2678 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2679 break;
2680 case visibility_local:
2681 S_CLEAR_EXTERNAL (symp);
2682 break;
2683 }
2684 }
2685 }
2686
2687 /* Double check weak symbols. */
2688 if (S_IS_WEAK (symp))
2689 {
2690 if (S_IS_COMMON (symp))
2691 as_bad (_("symbol `%s' can not be both weak and common"),
2692 S_GET_NAME (symp));
2693 }
2694 }
2695
2696 struct group_list
2697 {
2698 asection **head; /* Section lists. */
2699 unsigned int num_group; /* Number of lists. */
2700 htab_t indexes; /* Maps group name to index in head array. */
2701 };
2702
2703 static struct group_list groups;
2704
2705 /* Called via bfd_map_over_sections. If SEC is a member of a group,
2706 add it to a list of sections belonging to the group. INF is a
2707 pointer to a struct group_list, which is where we store the head of
2708 each list. If its link_to_symbol_name isn't NULL, set up its
2709 linked-to section. */
2710
2711 static void
2712 build_additional_section_info (bfd *abfd ATTRIBUTE_UNUSED,
2713 asection *sec, void *inf)
2714 {
2715 struct group_list *list = (struct group_list *) inf;
2716 const char *group_name = elf_group_name (sec);
2717 unsigned int i;
2718 unsigned int *elem_idx;
2719 unsigned int *idx_ptr;
2720
2721 if (sec->map_head.linked_to_symbol_name)
2722 {
2723 symbolS *linked_to_sym;
2724 linked_to_sym = symbol_find (sec->map_head.linked_to_symbol_name);
2725 if (!linked_to_sym || !S_IS_DEFINED (linked_to_sym))
2726 as_bad (_("undefined linked-to symbol `%s' on section `%s'"),
2727 sec->map_head.linked_to_symbol_name,
2728 bfd_section_name (sec));
2729 else
2730 elf_linked_to_section (sec) = S_GET_SEGMENT (linked_to_sym);
2731 }
2732
2733 if (group_name == NULL)
2734 return;
2735
2736 /* If this group already has a list, add the section to the head of
2737 the list. */
2738 elem_idx = (unsigned int *) str_hash_find (list->indexes, group_name);
2739 if (elem_idx != NULL)
2740 {
2741 elf_next_in_group (sec) = list->head[*elem_idx];
2742 list->head[*elem_idx] = sec;
2743 return;
2744 }
2745
2746 /* New group. Make the arrays bigger in chunks to minimize calls to
2747 realloc. */
2748 i = list->num_group;
2749 if ((i & 127) == 0)
2750 {
2751 unsigned int newsize = i + 128;
2752 list->head = XRESIZEVEC (asection *, list->head, newsize);
2753 }
2754 list->head[i] = sec;
2755 list->num_group += 1;
2756
2757 /* Add index to hash. */
2758 idx_ptr = XNEW (unsigned int);
2759 *idx_ptr = i;
2760 str_hash_insert (list->indexes, group_name, idx_ptr, 0);
2761 }
2762
2763 static int
2764 free_section_idx (void **slot, void *arg ATTRIBUTE_UNUSED)
2765 {
2766 string_tuple_t *tuple = *((string_tuple_t **) slot);
2767 free ((char *)tuple->value);
2768 return 1;
2769 }
2770
2771 /* Create symbols for group signature. */
2772
2773 void
2774 elf_adjust_symtab (void)
2775 {
2776 unsigned int i;
2777
2778 /* Go find section groups. */
2779 groups.num_group = 0;
2780 groups.head = NULL;
2781 groups.indexes = str_htab_create ();
2782 bfd_map_over_sections (stdoutput, build_additional_section_info,
2783 &groups);
2784
2785 /* Make the SHT_GROUP sections that describe each section group. We
2786 can't set up the section contents here yet, because elf section
2787 indices have yet to be calculated. elf.c:set_group_contents does
2788 the rest of the work. */
2789 for (i = 0; i < groups.num_group; i++)
2790 {
2791 const char *group_name = elf_group_name (groups.head[i]);
2792 const char *sec_name;
2793 asection *s;
2794 flagword flags;
2795 struct symbol *sy;
2796
2797 flags = SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_GROUP;
2798 for (s = groups.head[i]; s != NULL; s = elf_next_in_group (s))
2799 if ((s->flags ^ flags) & SEC_LINK_ONCE)
2800 {
2801 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2802 if (s != groups.head[i])
2803 {
2804 as_warn (_("assuming all members of group `%s' are COMDAT"),
2805 group_name);
2806 break;
2807 }
2808 }
2809
2810 sec_name = ".group";
2811 s = subseg_force_new (sec_name, 0);
2812 if (s == NULL
2813 || !bfd_set_section_flags (s, flags)
2814 || !bfd_set_section_alignment (s, 2))
2815 {
2816 as_fatal (_("can't create group: %s"),
2817 bfd_errmsg (bfd_get_error ()));
2818 }
2819 elf_section_type (s) = SHT_GROUP;
2820
2821 /* Pass a pointer to the first section in this group. */
2822 elf_next_in_group (s) = groups.head[i];
2823 elf_sec_group (groups.head[i]) = s;
2824 /* Make sure that the signature symbol for the group has the
2825 name of the group. */
2826 sy = symbol_find_exact (group_name);
2827 if (!sy || !symbol_on_chain (sy, symbol_rootP, symbol_lastP))
2828 {
2829 /* Create the symbol now. */
2830 sy = symbol_new (group_name, now_seg, frag_now, 0);
2831 #ifdef TE_SOLARIS
2832 /* Before Solaris 11 build 154, Sun ld rejects local group
2833 signature symbols, so make them weak hidden instead. */
2834 symbol_get_bfdsym (sy)->flags |= BSF_WEAK;
2835 S_SET_OTHER (sy, STV_HIDDEN);
2836 #else
2837 symbol_get_obj (sy)->local = 1;
2838 #endif
2839 symbol_table_insert (sy);
2840 }
2841 elf_group_id (s) = symbol_get_bfdsym (sy);
2842 }
2843 }
2844
2845 void
2846 elf_frob_file (void)
2847 {
2848 bfd_map_over_sections (stdoutput, adjust_stab_sections, NULL);
2849
2850 #ifdef elf_tc_final_processing
2851 elf_tc_final_processing ();
2852 #endif
2853 }
2854
2855 /* It removes any unneeded versioned symbols from the symbol table. */
2856
2857 void
2858 elf_frob_file_before_adjust (void)
2859 {
2860 if (symbol_rootP)
2861 {
2862 symbolS *symp;
2863
2864 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2865 {
2866 struct elf_obj_sy *sy_obj = symbol_get_obj (symp);
2867 int is_defined = !!S_IS_DEFINED (symp);
2868
2869 if (sy_obj->versioned_name)
2870 {
2871 char *p = strchr (sy_obj->versioned_name->name,
2872 ELF_VER_CHR);
2873
2874 if (sy_obj->rename)
2875 {
2876 /* The @@@ syntax is a special case. If the symbol is
2877 not defined, 2 `@'s will be removed from the
2878 versioned_name. Otherwise, 1 `@' will be removed. */
2879 size_t l = strlen (&p[3]) + 1;
2880 memmove (&p[1 + is_defined], &p[3], l);
2881 }
2882
2883 if (!is_defined)
2884 {
2885 /* Verify that the name isn't using the @@ syntax--this
2886 is reserved for definitions of the default version
2887 to link against. */
2888 if (!sy_obj->rename && p[1] == ELF_VER_CHR)
2889 {
2890 as_bad (_("invalid attempt to declare external "
2891 "version name as default in symbol `%s'"),
2892 sy_obj->versioned_name->name);
2893 return;
2894 }
2895
2896 /* Only one version symbol is allowed for undefined
2897 symbol. */
2898 if (sy_obj->versioned_name->next)
2899 {
2900 as_bad (_("multiple versions [`%s'|`%s'] for "
2901 "symbol `%s'"),
2902 sy_obj->versioned_name->name,
2903 sy_obj->versioned_name->next->name,
2904 S_GET_NAME (symp));
2905 return;
2906 }
2907
2908 sy_obj->rename = TRUE;
2909 }
2910 }
2911
2912 /* If there was .symver or .weak, but symbol was neither
2913 defined nor used anywhere, remove it. */
2914 if (!is_defined
2915 && (sy_obj->versioned_name || S_IS_WEAK (symp))
2916 && symbol_used_p (symp) == 0
2917 && symbol_used_in_reloc_p (symp) == 0)
2918 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2919 }
2920 }
2921 }
2922
2923 /* It is required that we let write_relocs have the opportunity to
2924 optimize away fixups before output has begun, since it is possible
2925 to eliminate all fixups for a section and thus we never should
2926 have generated the relocation section. */
2927
2928 void
2929 elf_frob_file_after_relocs (void)
2930 {
2931 unsigned int i;
2932
2933 /* Set SHT_GROUP section size. */
2934 for (i = 0; i < groups.num_group; i++)
2935 {
2936 asection *s, *head, *group;
2937 bfd_size_type size;
2938
2939 head = groups.head[i];
2940 size = 4;
2941 for (s = head; s != NULL; s = elf_next_in_group (s))
2942 size += (s->flags & SEC_RELOC) != 0 ? 8 : 4;
2943
2944 group = elf_sec_group (head);
2945 subseg_set (group, 0);
2946 bfd_set_section_size (group, size);
2947 group->contents = (unsigned char *) frag_more (size);
2948 frag_now->fr_fix = frag_now_fix_octets ();
2949 frag_wane (frag_now);
2950 }
2951
2952 /* Cleanup hash. */
2953 htab_traverse (groups.indexes, free_section_idx, NULL);
2954 htab_delete (groups.indexes);
2955
2956 #ifdef NEED_ECOFF_DEBUG
2957 if (ECOFF_DEBUGGING)
2958 /* Generate the ECOFF debugging information. */
2959 {
2960 const struct ecoff_debug_swap *debug_swap;
2961 struct ecoff_debug_info debug;
2962 char *buf;
2963 asection *sec;
2964
2965 debug_swap
2966 = get_elf_backend_data (stdoutput)->elf_backend_ecoff_debug_swap;
2967 know (debug_swap != NULL);
2968 ecoff_build_debug (&debug.symbolic_header, &buf, debug_swap);
2969
2970 /* Set up the pointers in debug. */
2971 #define SET(ptr, offset, type) \
2972 debug.ptr = (type) (buf + debug.symbolic_header.offset)
2973
2974 SET (line, cbLineOffset, unsigned char *);
2975 SET (external_dnr, cbDnOffset, void *);
2976 SET (external_pdr, cbPdOffset, void *);
2977 SET (external_sym, cbSymOffset, void *);
2978 SET (external_opt, cbOptOffset, void *);
2979 SET (external_aux, cbAuxOffset, union aux_ext *);
2980 SET (ss, cbSsOffset, char *);
2981 SET (external_fdr, cbFdOffset, void *);
2982 SET (external_rfd, cbRfdOffset, void *);
2983 /* ssext and external_ext are set up just below. */
2984
2985 #undef SET
2986
2987 /* Set up the external symbols. */
2988 debug.ssext = debug.ssext_end = NULL;
2989 debug.external_ext = debug.external_ext_end = NULL;
2990 if (! bfd_ecoff_debug_externals (stdoutput, &debug, debug_swap, TRUE,
2991 elf_get_extr, elf_set_index))
2992 as_fatal (_("failed to set up debugging information: %s"),
2993 bfd_errmsg (bfd_get_error ()));
2994
2995 sec = bfd_get_section_by_name (stdoutput, ".mdebug");
2996 gas_assert (sec != NULL);
2997
2998 know (!stdoutput->output_has_begun);
2999
3000 /* We set the size of the section, call bfd_set_section_contents
3001 to force the ELF backend to allocate a file position, and then
3002 write out the data. FIXME: Is this really the best way to do
3003 this? */
3004 bfd_set_section_size (sec, bfd_ecoff_debug_size (stdoutput, &debug,
3005 debug_swap));
3006
3007 /* Pass BUF to bfd_set_section_contents because this will
3008 eventually become a call to fwrite, and ISO C prohibits
3009 passing a NULL pointer to a stdio function even if the
3010 pointer will not be used. */
3011 if (! bfd_set_section_contents (stdoutput, sec, buf, 0, 0))
3012 as_fatal (_("can't start writing .mdebug section: %s"),
3013 bfd_errmsg (bfd_get_error ()));
3014
3015 know (stdoutput->output_has_begun);
3016 know (sec->filepos != 0);
3017
3018 if (! bfd_ecoff_write_debug (stdoutput, &debug, debug_swap,
3019 sec->filepos))
3020 as_fatal (_("could not write .mdebug section: %s"),
3021 bfd_errmsg (bfd_get_error ()));
3022 }
3023 #endif /* NEED_ECOFF_DEBUG */
3024 }
3025
3026 static void
3027 elf_generate_asm_lineno (void)
3028 {
3029 #ifdef NEED_ECOFF_DEBUG
3030 if (ECOFF_DEBUGGING)
3031 ecoff_generate_asm_lineno ();
3032 #endif
3033 }
3034
3035 static void
3036 elf_process_stab (segT sec ATTRIBUTE_UNUSED,
3037 int what ATTRIBUTE_UNUSED,
3038 const char *string ATTRIBUTE_UNUSED,
3039 int type ATTRIBUTE_UNUSED,
3040 int other ATTRIBUTE_UNUSED,
3041 int desc ATTRIBUTE_UNUSED)
3042 {
3043 #ifdef NEED_ECOFF_DEBUG
3044 if (ECOFF_DEBUGGING)
3045 ecoff_stab (sec, what, string, type, other, desc);
3046 #endif
3047 }
3048
3049 static int
3050 elf_separate_stab_sections (void)
3051 {
3052 #ifdef NEED_ECOFF_DEBUG
3053 return (!ECOFF_DEBUGGING);
3054 #else
3055 return 1;
3056 #endif
3057 }
3058
3059 static void
3060 elf_init_stab_section (segT seg)
3061 {
3062 #ifdef NEED_ECOFF_DEBUG
3063 if (!ECOFF_DEBUGGING)
3064 #endif
3065 obj_elf_init_stab_section (seg);
3066 }
3067
3068 const struct format_ops elf_format_ops =
3069 {
3070 bfd_target_elf_flavour,
3071 0, /* dfl_leading_underscore */
3072 1, /* emit_section_symbols */
3073 elf_begin,
3074 elf_file_symbol,
3075 elf_frob_symbol,
3076 elf_frob_file,
3077 elf_frob_file_before_adjust,
3078 0, /* obj_frob_file_before_fix */
3079 elf_frob_file_after_relocs,
3080 elf_s_get_size, elf_s_set_size,
3081 elf_s_get_align, elf_s_set_align,
3082 elf_s_get_other,
3083 elf_s_set_other,
3084 0, /* s_get_desc */
3085 0, /* s_set_desc */
3086 0, /* s_get_type */
3087 0, /* s_set_type */
3088 elf_copy_symbol_attributes,
3089 elf_generate_asm_lineno,
3090 elf_process_stab,
3091 elf_separate_stab_sections,
3092 elf_init_stab_section,
3093 elf_sec_sym_ok_for_reloc,
3094 elf_pop_insert,
3095 #ifdef NEED_ECOFF_DEBUG
3096 elf_ecoff_set_ext,
3097 #else
3098 0, /* ecoff_set_ext */
3099 #endif
3100 elf_obj_read_begin_hook,
3101 elf_obj_symbol_new_hook,
3102 elf_obj_symbol_clone_hook,
3103 elf_adjust_symtab
3104 };