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