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