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