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