bfd/
[deliverable/binutils-gdb.git] / gas / config / obj-elf.c
CommitLineData
252b5132 1/* ELF object file format
cc8a6dd0 2 Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
a0ea3e1d 3 2001, 2002, 2003 Free Software Foundation, Inc.
252b5132
RH
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2,
10 or (at your option) any later version.
11
12 GAS is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
bf514e21 20 02111-1307, USA. */
252b5132
RH
21
22#define OBJ_HEADER "obj-elf.h"
23#include "as.h"
3882b010 24#include "safe-ctype.h"
252b5132
RH
25#include "subsegs.h"
26#include "obstack.h"
9758f3fc 27#include "struc-symbol.h"
87ccc1b0 28#include "dwarf2dbg.h"
252b5132
RH
29
30#ifndef ECOFF_DEBUGGING
31#define ECOFF_DEBUGGING 0
32#else
33#define NEED_ECOFF_DEBUG
34#endif
35
36#ifdef NEED_ECOFF_DEBUG
37#include "ecoff.h"
38#endif
39
40#ifdef TC_ALPHA
41#include "elf/alpha.h"
42#endif
43
44#ifdef TC_MIPS
45#include "elf/mips.h"
46#endif
47
48#ifdef TC_PPC
49#include "elf/ppc.h"
50#endif
51
5b93d8bb
AM
52#ifdef TC_I370
53#include "elf/i370.h"
54#endif
55
dbe2df79
AM
56static void obj_elf_line (int);
57static void obj_elf_size (int);
58static void obj_elf_type (int);
59static void obj_elf_ident (int);
60static void obj_elf_weak (int);
61static void obj_elf_local (int);
62static void obj_elf_visibility (int);
63static void obj_elf_symver (int);
64static void obj_elf_subsection (int);
65static void obj_elf_popsection (int);
66static void obj_elf_tls_common (int);
13c56984 67static void obj_elf_lcomm (int);
252b5132
RH
68
69static const pseudo_typeS elf_pseudo_table[] =
70{
71 {"comm", obj_elf_common, 0},
9be1cda6 72 {"common", obj_elf_common, 1},
252b5132 73 {"ident", obj_elf_ident, 0},
13c56984 74 {"lcomm", obj_elf_lcomm, 0},
252b5132
RH
75 {"local", obj_elf_local, 0},
76 {"previous", obj_elf_previous, 0},
77 {"section", obj_elf_section, 0},
78 {"section.s", obj_elf_section, 0},
79 {"sect", obj_elf_section, 0},
80 {"sect.s", obj_elf_section, 0},
9de8d8f1
RH
81 {"pushsection", obj_elf_section, 1},
82 {"popsection", obj_elf_popsection, 0},
252b5132
RH
83 {"size", obj_elf_size, 0},
84 {"type", obj_elf_type, 0},
85 {"version", obj_elf_version, 0},
86 {"weak", obj_elf_weak, 0},
87
bf514e21 88 /* These define symbol visibility. */
2e13b764
NC
89 {"internal", obj_elf_visibility, STV_INTERNAL},
90 {"hidden", obj_elf_visibility, STV_HIDDEN},
91 {"protected", obj_elf_visibility, STV_PROTECTED},
92
252b5132
RH
93 /* These are used for stabs-in-elf configurations. */
94 {"line", obj_elf_line, 0},
95
96 /* This is a GNU extension to handle symbol versions. */
97 {"symver", obj_elf_symver, 0},
98
99 /* A GNU extension to change subsection only. */
100 {"subsection", obj_elf_subsection, 0},
101
102 /* These are GNU extensions to aid in garbage collecting C++ vtables. */
dbe2df79
AM
103 {"vtable_inherit", (void (*) (int)) &obj_elf_vtable_inherit, 0},
104 {"vtable_entry", (void (*) (int)) &obj_elf_vtable_entry, 0},
252b5132 105
bf514e21 106 /* These are used for dwarf. */
252b5132
RH
107 {"2byte", cons, 2},
108 {"4byte", cons, 4},
109 {"8byte", cons, 8},
87ccc1b0 110 /* These are used for dwarf2. */
dbe2df79 111 { "file", (void (*) (int)) dwarf2_directive_file, 0 },
87ccc1b0 112 { "loc", dwarf2_directive_loc, 0 },
252b5132
RH
113
114 /* We need to trap the section changing calls to handle .previous. */
115 {"data", obj_elf_data, 0},
116 {"text", obj_elf_text, 0},
117
13ae64f3
JJ
118 {"tls_common", obj_elf_tls_common, 0},
119
252b5132 120 /* End sentinel. */
ab9da554 121 {NULL, NULL, 0},
252b5132
RH
122};
123
124static const pseudo_typeS ecoff_debug_pseudo_table[] =
125{
126#ifdef NEED_ECOFF_DEBUG
127 /* COFF style debugging information for ECOFF. .ln is not used; .loc
128 is used instead. */
129 { "def", ecoff_directive_def, 0 },
130 { "dim", ecoff_directive_dim, 0 },
131 { "endef", ecoff_directive_endef, 0 },
132 { "file", ecoff_directive_file, 0 },
133 { "scl", ecoff_directive_scl, 0 },
134 { "tag", ecoff_directive_tag, 0 },
135 { "val", ecoff_directive_val, 0 },
136
137 /* COFF debugging requires pseudo-ops .size and .type, but ELF
138 already has meanings for those. We use .esize and .etype
139 instead. These are only generated by gcc anyhow. */
140 { "esize", ecoff_directive_size, 0 },
141 { "etype", ecoff_directive_type, 0 },
142
143 /* ECOFF specific debugging information. */
144 { "begin", ecoff_directive_begin, 0 },
145 { "bend", ecoff_directive_bend, 0 },
146 { "end", ecoff_directive_end, 0 },
147 { "ent", ecoff_directive_ent, 0 },
148 { "fmask", ecoff_directive_fmask, 0 },
149 { "frame", ecoff_directive_frame, 0 },
150 { "loc", ecoff_directive_loc, 0 },
151 { "mask", ecoff_directive_mask, 0 },
152
153 /* Other ECOFF directives. */
154 { "extern", ecoff_directive_extern, 0 },
155
156 /* These are used on Irix. I don't know how to implement them. */
157 { "alias", s_ignore, 0 },
158 { "bgnb", s_ignore, 0 },
159 { "endb", s_ignore, 0 },
160 { "lab", s_ignore, 0 },
161 { "noalias", s_ignore, 0 },
162 { "verstamp", s_ignore, 0 },
163 { "vreg", s_ignore, 0 },
164#endif
165
ab9da554 166 {NULL, NULL, 0} /* end sentinel */
252b5132
RH
167};
168
169#undef NO_RELOC
170#include "aout/aout64.h"
171
172/* This is called when the assembler starts. */
173
174void
dbe2df79 175elf_begin (void)
252b5132 176{
dbe2df79
AM
177 asection *s;
178
252b5132 179 /* Add symbols for the known sections to the symbol table. */
dbe2df79
AM
180 s = bfd_get_section_by_name (stdoutput, TEXT_SECTION_NAME);
181 symbol_table_insert (section_symbol (s));
182 s = bfd_get_section_by_name (stdoutput, DATA_SECTION_NAME);
183 symbol_table_insert (section_symbol (s));
184 s = bfd_get_section_by_name (stdoutput, BSS_SECTION_NAME);
185 symbol_table_insert (section_symbol (s));
252b5132
RH
186}
187
188void
dbe2df79 189elf_pop_insert (void)
252b5132
RH
190{
191 pop_insert (elf_pseudo_table);
192 if (ECOFF_DEBUGGING)
193 pop_insert (ecoff_debug_pseudo_table);
194}
195
196static bfd_vma
dbe2df79 197elf_s_get_size (symbolS *sym)
252b5132
RH
198{
199 return S_GET_SIZE (sym);
200}
201
202static void
dbe2df79 203elf_s_set_size (symbolS *sym, bfd_vma sz)
252b5132
RH
204{
205 S_SET_SIZE (sym, sz);
206}
207
208static bfd_vma
dbe2df79 209elf_s_get_align (symbolS *sym)
252b5132
RH
210{
211 return S_GET_ALIGN (sym);
212}
213
214static void
dbe2df79 215elf_s_set_align (symbolS *sym, bfd_vma align)
252b5132
RH
216{
217 S_SET_ALIGN (sym, align);
218}
219
4c63da97 220int
dbe2df79 221elf_s_get_other (symbolS *sym)
4c63da97
AM
222{
223 return elf_symbol (symbol_get_bfdsym (sym))->internal_elf_sym.st_other;
224}
225
5110c57e 226static void
dbe2df79 227elf_s_set_other (symbolS *sym, int other)
5110c57e
HPN
228{
229 S_SET_OTHER (sym, other);
230}
231
252b5132 232static int
dbe2df79 233elf_sec_sym_ok_for_reloc (asection *sec)
252b5132
RH
234{
235 return obj_sec_sym_ok_for_reloc (sec);
236}
237
238void
dbe2df79 239elf_file_symbol (const char *s)
252b5132
RH
240{
241 symbolS *sym;
242
dbe2df79 243 sym = symbol_new (s, absolute_section, 0, NULL);
49309057
ILT
244 symbol_set_frag (sym, &zero_address_frag);
245 symbol_get_bfdsym (sym)->flags |= BSF_FILE;
252b5132
RH
246
247 if (symbol_rootP != sym)
248 {
249 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
250 symbol_insert (sym, symbol_rootP, &symbol_rootP, &symbol_lastP);
251#ifdef DEBUG
252 verify_symbol_chain (symbol_rootP, symbol_lastP);
253#endif
254 }
255
256#ifdef NEED_ECOFF_DEBUG
257 ecoff_new_file (s);
258#endif
259}
260
e13bab5a
AM
261/* Called from read.c:s_comm after we've parsed .comm symbol, size.
262 Parse a possible alignment value. */
263
13ae64f3 264static symbolS *
e13bab5a 265elf_common_parse (int ignore ATTRIBUTE_UNUSED, symbolS *symbolP, addressT size)
252b5132 266{
e13bab5a
AM
267 addressT align = 0;
268 int is_local = symbol_get_obj (symbolP)->local;
252b5132 269
e13bab5a 270 if (*input_line_pointer == ',')
9be1cda6 271 {
e13bab5a 272 char *save = input_line_pointer;
9be1cda6 273
252b5132
RH
274 input_line_pointer++;
275 SKIP_WHITESPACE ();
e13bab5a
AM
276
277 if (*input_line_pointer == '"')
252b5132 278 {
e13bab5a
AM
279 /* For sparc. Accept .common symbol, length, "bss" */
280 input_line_pointer++;
281 /* Some use the dot, some don't. */
282 if (*input_line_pointer == '.')
283 input_line_pointer++;
284 /* Some say data, some say bss. */
285 if (strncmp (input_line_pointer, "bss\"", 4) == 0)
286 input_line_pointer += 4;
287 else if (strncmp (input_line_pointer, "data\"", 5) == 0)
288 input_line_pointer += 5;
289 else
252b5132 290 {
e13bab5a
AM
291 char *p = input_line_pointer;
292 char c;
293
294 while (*--p != '"')
295 ;
296 while (!is_end_of_line[(unsigned char) *input_line_pointer])
297 if (*input_line_pointer++ == '"')
298 break;
299 c = *input_line_pointer;
300 *input_line_pointer = '\0';
301 as_bad (_("bad .common segment %s"), p);
302 *input_line_pointer = c;
303 ignore_rest_of_line ();
304 return NULL;
252b5132 305 }
e13bab5a
AM
306 /* ??? Don't ask me why these are always global. */
307 is_local = 0;
252b5132
RH
308 }
309 else
310 {
e13bab5a
AM
311 input_line_pointer = save;
312 align = parse_align (is_local);
313 if (align == (addressT) -1)
314 return NULL;
252b5132
RH
315 }
316 }
e13bab5a
AM
317
318 if (is_local)
319 {
320 bss_alloc (symbolP, size, align);
321 S_CLEAR_EXTERNAL (symbolP);
322 }
252b5132
RH
323 else
324 {
e13bab5a
AM
325 S_SET_VALUE (symbolP, size);
326 S_SET_ALIGN (symbolP, align);
327 S_SET_EXTERNAL (symbolP);
328 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
252b5132
RH
329 }
330
49309057 331 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
252b5132 332
13ae64f3 333 return symbolP;
252b5132
RH
334}
335
13ae64f3 336void
dbe2df79 337obj_elf_common (int is_common)
13ae64f3 338{
e13bab5a
AM
339 if (flag_mri && is_common)
340 s_mri_common (0);
341 else
342 s_comm_internal (0, elf_common_parse);
13ae64f3
JJ
343}
344
345static void
dbe2df79 346obj_elf_tls_common (int ignore ATTRIBUTE_UNUSED)
13ae64f3 347{
e13bab5a 348 symbolS *symbolP = s_comm_internal (0, elf_common_parse);
13ae64f3
JJ
349
350 if (symbolP)
351 symbol_get_bfdsym (symbolP)->flags |= BSF_THREAD_LOCAL;
352}
353
13c56984
AM
354static void
355obj_elf_lcomm (int ignore ATTRIBUTE_UNUSED)
356{
357 symbolS *symbolP = s_comm_internal (0, s_lcomm_internal);
358
359 if (symbolP)
360 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
361}
362
252b5132 363static void
dbe2df79 364obj_elf_local (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
365{
366 char *name;
367 int c;
368 symbolS *symbolP;
369
370 do
371 {
372 name = input_line_pointer;
373 c = get_symbol_end ();
374 symbolP = symbol_find_or_make (name);
375 *input_line_pointer = c;
376 SKIP_WHITESPACE ();
377 S_CLEAR_EXTERNAL (symbolP);
49309057 378 symbol_get_obj (symbolP)->local = 1;
252b5132
RH
379 if (c == ',')
380 {
381 input_line_pointer++;
382 SKIP_WHITESPACE ();
383 if (*input_line_pointer == '\n')
384 c = '\n';
385 }
386 }
387 while (c == ',');
388 demand_empty_rest_of_line ();
389}
390
391static void
dbe2df79 392obj_elf_weak (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
393{
394 char *name;
395 int c;
396 symbolS *symbolP;
397
398 do
399 {
400 name = input_line_pointer;
401 c = get_symbol_end ();
402 symbolP = symbol_find_or_make (name);
403 *input_line_pointer = c;
404 SKIP_WHITESPACE ();
405 S_SET_WEAK (symbolP);
49309057 406 symbol_get_obj (symbolP)->local = 1;
252b5132
RH
407 if (c == ',')
408 {
409 input_line_pointer++;
410 SKIP_WHITESPACE ();
411 if (*input_line_pointer == '\n')
412 c = '\n';
413 }
414 }
415 while (c == ',');
416 demand_empty_rest_of_line ();
417}
418
2e13b764 419static void
dbe2df79 420obj_elf_visibility (int visibility)
2e13b764
NC
421{
422 char *name;
423 int c;
424 symbolS *symbolP;
425 asymbol *bfdsym;
426 elf_symbol_type *elfsym;
427
428 do
429 {
430 name = input_line_pointer;
431 c = get_symbol_end ();
432 symbolP = symbol_find_or_make (name);
433 *input_line_pointer = c;
fa306131 434
2e13b764 435 SKIP_WHITESPACE ();
fa306131 436
2e13b764
NC
437 bfdsym = symbol_get_bfdsym (symbolP);
438 elfsym = elf_symbol_from (bfd_asymbol_bfd (bfdsym), bfdsym);
fa306131 439
2e13b764 440 assert (elfsym);
fa306131 441
20e420c2
RH
442 elfsym->internal_elf_sym.st_other &= ~3;
443 elfsym->internal_elf_sym.st_other |= visibility;
fa306131 444
2e13b764
NC
445 if (c == ',')
446 {
447 input_line_pointer ++;
fa306131 448
2e13b764 449 SKIP_WHITESPACE ();
fa306131 450
2e13b764
NC
451 if (*input_line_pointer == '\n')
452 c = '\n';
453 }
454 }
455 while (c == ',');
fa306131 456
2e13b764
NC
457 demand_empty_rest_of_line ();
458}
459
252b5132
RH
460static segT previous_section;
461static int previous_subsection;
462
9de8d8f1
RH
463struct section_stack
464{
465 struct section_stack *next;
466 segT seg, prev_seg;
467 int subseg, prev_subseg;
468};
469
470static struct section_stack *section_stack;
471
fafe6678 472static bfd_boolean
86654c12
L
473get_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
474{
fafe6678 475 const char *gname = inf;
86654c12
L
476 const char *group_name = elf_group_name (sec);
477
fafe6678 478 return (group_name == gname
86654c12 479 || (group_name != NULL
fafe6678
L
480 && gname != NULL
481 && strcmp (group_name, gname) == 0));
86654c12
L
482}
483
252b5132
RH
484/* Handle the .section pseudo-op. This code supports two different
485 syntaxes.
486
487 The first is found on Solaris, and looks like
488 .section ".sec1",#alloc,#execinstr,#write
489 Here the names after '#' are the SHF_* flags to turn on for the
490 section. I'm not sure how it determines the SHT_* type (BFD
491 doesn't really give us control over the type, anyhow).
492
493 The second format is found on UnixWare, and probably most SVR4
494 machines, and looks like
495 .section .sec1,"a",@progbits
496 The quoted string may contain any combination of a, w, x, and
497 represents the SHF_* flags to turn on for the section. The string
498 beginning with '@' can be progbits or nobits. There should be
499 other possibilities, but I don't know what they are. In any case,
500 BFD doesn't really let us set the section type. */
501
cca86cc8 502void
dbe2df79
AM
503obj_elf_change_section (const char *name,
504 int type,
505 int attr,
506 int entsize,
507 const char *group_name,
508 int linkonce,
509 int push)
252b5132 510{
fafe6678 511 asection *old_sec;
252b5132 512 segT sec;
742f45cf 513 flagword flags;
f61e8019 514 const struct bfd_elf_special_section *ssect;
252b5132
RH
515
516#ifdef md_flush_pending_output
517 md_flush_pending_output ();
518#endif
519
9de8d8f1
RH
520 /* Switch to the section, creating it if necessary. */
521 if (push)
522 {
523 struct section_stack *elt;
524 elt = xmalloc (sizeof (struct section_stack));
525 elt->next = section_stack;
526 elt->seg = now_seg;
527 elt->prev_seg = previous_section;
528 elt->subseg = now_subseg;
529 elt->prev_subseg = previous_subsection;
530 section_stack = elt;
531 }
532 previous_section = now_seg;
533 previous_subsection = now_subseg;
534
fafe6678
L
535 old_sec = bfd_get_section_by_name_if (stdoutput, name, get_section,
536 (void *) group_name);
537 if (old_sec)
86654c12 538 {
fafe6678 539 sec = old_sec;
86654c12
L
540 subseg_set (sec, 0);
541 }
542 else
543 sec = subseg_force_new (name, 0);
544
f61e8019 545 ssect = _bfd_elf_get_sec_type_attr (stdoutput, name);
9de8d8f1 546
f61e8019 547 if (ssect != NULL)
2f89ff8d 548 {
ea8f8eab
L
549 bfd_boolean override = FALSE;
550
2f89ff8d 551 if (type == SHT_NULL)
f61e8019
AM
552 type = ssect->type;
553 else if (type != ssect->type)
2f89ff8d 554 {
fafe6678 555 if (old_sec == NULL
2f89ff8d 556 /* FIXME: gcc, as of 2002-10-22, will emit
7ed1d346 557
2f89ff8d 558 .section .init_array,"aw",@progbits
7ed1d346 559
2f89ff8d
L
560 for __attribute__ ((section (".init_array"))).
561 "@progbits" is incorrect. */
f61e8019
AM
562 && ssect->type != SHT_INIT_ARRAY
563 && ssect->type != SHT_FINI_ARRAY
564 && ssect->type != SHT_PREINIT_ARRAY)
2f89ff8d
L
565 {
566 /* We allow to specify any type for a .note section. */
f61e8019 567 if (ssect->type != SHT_NOTE)
2f89ff8d
L
568 as_warn (_("setting incorrect section type for %s"),
569 name);
570 }
571 else
572 {
573 as_warn (_("ignoring incorrect section type for %s"),
742f45cf 574 name);
f61e8019 575 type = ssect->type;
2f89ff8d
L
576 }
577 }
578
fafe6678 579 if (old_sec == NULL && (attr & ~ssect->attr) != 0)
2f89ff8d
L
580 {
581 /* As a GNU extension, we permit a .note section to be
f61e8019 582 allocatable. If the linker sees an allocatable .note
2f89ff8d 583 section, it will create a PT_NOTE segment in the output
92191b29 584 file. We also allow "x" for .note.GNU-stack. */
f61e8019
AM
585 if (ssect->type == SHT_NOTE
586 && (attr == SHF_ALLOC || attr == SHF_EXECINSTR))
587 ;
588 /* Allow different SHF_MERGE and SHF_STRINGS if we have
589 something like .rodata.str. */
590 else if (ssect->suffix_length == -2
591 && name[ssect->prefix_length] == '.'
ea8f8eab
L
592 && (attr
593 & ~ssect->attr
594 & ~SHF_MERGE
595 & ~SHF_STRINGS) == 0)
f61e8019 596 ;
ea8f8eab
L
597 /* .interp, .strtab and .symtab can have SHF_ALLOC. */
598 else if (attr == SHF_ALLOC
599 && (strcmp (name, ".interp") == 0
600 || strcmp (name, ".strtab") == 0
601 || strcmp (name, ".symtab") == 0))
602 override = TRUE;
f61e8019 603 else
ea8f8eab 604 {
86654c12
L
605 if (group_name == NULL)
606 as_warn (_("setting incorrect section attributes for %s"),
607 name);
ea8f8eab
L
608 override = TRUE;
609 }
2f89ff8d 610 }
fafe6678 611 if (!override && old_sec == NULL)
f61e8019 612 attr |= ssect->attr;
2f89ff8d 613 }
742f45cf 614
3cddba1e
L
615 if (type != SHT_NULL)
616 elf_section_type (sec) = type;
617 if (attr != 0)
618 elf_section_flags (sec) = attr;
8e8c182c 619
742f45cf
AM
620 /* Convert ELF type and flags to BFD flags. */
621 flags = (SEC_RELOC
622 | ((attr & SHF_WRITE) ? 0 : SEC_READONLY)
623 | ((attr & SHF_ALLOC) ? SEC_ALLOC : 0)
624 | (((attr & SHF_ALLOC) && type != SHT_NOBITS) ? SEC_LOAD : 0)
f5fa8ca2
JJ
625 | ((attr & SHF_EXECINSTR) ? SEC_CODE : 0)
626 | ((attr & SHF_MERGE) ? SEC_MERGE : 0)
13ae64f3
JJ
627 | ((attr & SHF_STRINGS) ? SEC_STRINGS : 0)
628 | ((attr & SHF_TLS) ? SEC_THREAD_LOCAL : 0));
9de8d8f1 629#ifdef md_elf_section_flags
742f45cf 630 flags = md_elf_section_flags (flags, attr, type);
9de8d8f1
RH
631#endif
632
86654c12
L
633 if (linkonce)
634 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
635
fafe6678 636 if (old_sec == NULL)
742f45cf
AM
637 {
638 symbolS *secsym;
639
9de8d8f1
RH
640 /* Prevent SEC_HAS_CONTENTS from being inadvertently set. */
641 if (type == SHT_NOBITS)
13c56984 642 seg_info (sec)->bss = 1;
9de8d8f1
RH
643
644 bfd_set_section_flags (stdoutput, sec, flags);
f5fa8ca2
JJ
645 if (flags & SEC_MERGE)
646 sec->entsize = entsize;
aa1f4858 647 elf_group_name (sec) = group_name;
9de8d8f1
RH
648
649 /* Add a symbol for this section to the symbol table. */
650 secsym = symbol_find (name);
651 if (secsym != NULL)
652 symbol_set_bfdsym (secsym, sec->symbol);
653 else
13c56984 654 symbol_table_insert (section_symbol (sec));
9de8d8f1 655 }
742f45cf
AM
656 else if (attr != 0)
657 {
658 /* If section attributes are specified the second time we see a
659 particular section, then check that they are the same as we
660 saw the first time. */
fafe6678 661 if (((old_sec->flags ^ flags)
d2dab548
AM
662 & (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
663 | SEC_EXCLUDE | SEC_SORT_ENTRIES | SEC_MERGE | SEC_STRINGS
68bfbfcc
AM
664 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
665 | SEC_THREAD_LOCAL)))
6ce8b369 666 as_warn (_("ignoring changed section attributes for %s"), name);
fafe6678 667 if ((flags & SEC_MERGE) && old_sec->entsize != (unsigned) entsize)
6ce8b369 668 as_warn (_("ignoring changed section entity size for %s"), name);
742f45cf 669 }
9de8d8f1
RH
670
671#ifdef md_elf_section_change_hook
742f45cf 672 md_elf_section_change_hook ();
9de8d8f1
RH
673#endif
674}
675
8d28c9d7 676static int
dbe2df79 677obj_elf_parse_section_letters (char *str, size_t len)
9de8d8f1
RH
678{
679 int attr = 0;
680
681 while (len > 0)
682 {
683 switch (*str)
684 {
685 case 'a':
686 attr |= SHF_ALLOC;
687 break;
688 case 'w':
689 attr |= SHF_WRITE;
690 break;
691 case 'x':
692 attr |= SHF_EXECINSTR;
693 break;
9469ddf0 694 case 'M':
f5fa8ca2
JJ
695 attr |= SHF_MERGE;
696 break;
9469ddf0 697 case 'S':
f5fa8ca2
JJ
698 attr |= SHF_STRINGS;
699 break;
060adf0e
AM
700 case 'G':
701 attr |= SHF_GROUP;
702 break;
13ae64f3
JJ
703 case 'T':
704 attr |= SHF_TLS;
705 break;
34105363
L
706 /* Compatibility. */
707 case 'm':
708 if (*(str - 1) == 'a')
709 {
710 attr |= SHF_MERGE;
711 if (len > 1 && str[1] == 's')
712 {
713 attr |= SHF_STRINGS;
714 str++, len--;
715 }
716 break;
717 }
9de8d8f1
RH
718 default:
719 {
13ae64f3 720 char *bad_msg = _("unrecognized .section attribute: want a,w,x,M,S,G,T");
9de8d8f1
RH
721#ifdef md_elf_section_letter
722 int md_attr = md_elf_section_letter (*str, &bad_msg);
723 if (md_attr >= 0)
724 attr |= md_attr;
725 else
726#endif
711ef82f 727 as_fatal ("%s", bad_msg);
9de8d8f1
RH
728 }
729 break;
730 }
731 str++, len--;
732 }
733
734 return attr;
735}
736
8d28c9d7 737static int
dbe2df79 738obj_elf_section_word (char *str, size_t len)
9de8d8f1
RH
739{
740 if (len == 5 && strncmp (str, "write", 5) == 0)
741 return SHF_WRITE;
742 if (len == 5 && strncmp (str, "alloc", 5) == 0)
743 return SHF_ALLOC;
744 if (len == 9 && strncmp (str, "execinstr", 9) == 0)
745 return SHF_EXECINSTR;
b9734f35
JJ
746 if (len == 3 && strncmp (str, "tls", 3) == 0)
747 return SHF_TLS;
9de8d8f1
RH
748
749#ifdef md_elf_section_word
750 {
751 int md_attr = md_elf_section_word (str, len);
752 if (md_attr >= 0)
753 return md_attr;
754 }
755#endif
756
6ce8b369 757 as_warn (_("unrecognized section attribute"));
9de8d8f1
RH
758 return 0;
759}
760
8d28c9d7 761static int
dbe2df79 762obj_elf_section_type (char *str, size_t len)
9de8d8f1
RH
763{
764 if (len == 8 && strncmp (str, "progbits", 8) == 0)
765 return SHT_PROGBITS;
766 if (len == 6 && strncmp (str, "nobits", 6) == 0)
767 return SHT_NOBITS;
34f70875
L
768 if (len == 4 && strncmp (str, "note", 4) == 0)
769 return SHT_NOTE;
9de8d8f1
RH
770
771#ifdef md_elf_section_type
772 {
773 int md_type = md_elf_section_type (str, len);
774 if (md_type >= 0)
775 return md_type;
776 }
777#endif
778
6ce8b369 779 as_warn (_("unrecognized section type"));
9de8d8f1
RH
780 return 0;
781}
782
6ce8b369
AM
783/* Get name of section. */
784static char *
dbe2df79 785obj_elf_section_name (void)
6ce8b369
AM
786{
787 char *name;
788
789 SKIP_WHITESPACE ();
790 if (*input_line_pointer == '"')
791 {
792 int dummy;
793
794 name = demand_copy_C_string (&dummy);
795 if (name == NULL)
796 {
797 ignore_rest_of_line ();
798 return NULL;
799 }
800 }
801 else
802 {
803 char *end = input_line_pointer;
804
805 while (0 == strchr ("\n\t,; ", *end))
806 end++;
807 if (end == input_line_pointer)
808 {
c95b35a9 809 as_bad (_("missing name"));
6ce8b369
AM
810 ignore_rest_of_line ();
811 return NULL;
812 }
813
814 name = xmalloc (end - input_line_pointer + 1);
815 memcpy (name, input_line_pointer, end - input_line_pointer);
816 name[end - input_line_pointer] = '\0';
612d7b83
L
817#ifdef tc_canonicalize_section_name
818 name = tc_canonicalize_section_name (name);
819#endif
6ce8b369
AM
820 input_line_pointer = end;
821 }
822 SKIP_WHITESPACE ();
823 return name;
824}
825
9de8d8f1 826void
dbe2df79 827obj_elf_section (int push)
9de8d8f1 828{
aa1f4858 829 char *name, *group_name, *beg;
9de8d8f1 830 int type, attr, dummy;
f5fa8ca2 831 int entsize;
d2dab548 832 int linkonce;
9de8d8f1 833
5b93d8bb 834#ifndef TC_I370
252b5132
RH
835 if (flag_mri)
836 {
837 char mri_type;
838
9de8d8f1 839#ifdef md_flush_pending_output
60bcf0fa 840 md_flush_pending_output ();
9de8d8f1
RH
841#endif
842
252b5132
RH
843 previous_section = now_seg;
844 previous_subsection = now_subseg;
845
846 s_mri_sect (&mri_type);
847
848#ifdef md_elf_section_change_hook
849 md_elf_section_change_hook ();
850#endif
851
852 return;
853 }
5b93d8bb 854#endif /* ! defined (TC_I370) */
252b5132 855
6ce8b369
AM
856 name = obj_elf_section_name ();
857 if (name == NULL)
858 return;
252b5132
RH
859 type = SHT_NULL;
860 attr = 0;
aa1f4858 861 group_name = NULL;
f5fa8ca2 862 entsize = 0;
d2dab548 863 linkonce = 0;
252b5132
RH
864
865 if (*input_line_pointer == ',')
866 {
867 /* Skip the comma. */
868 ++input_line_pointer;
252b5132
RH
869 SKIP_WHITESPACE ();
870
871 if (*input_line_pointer == '"')
872 {
9de8d8f1
RH
873 beg = demand_copy_C_string (&dummy);
874 if (beg == NULL)
252b5132 875 {
9de8d8f1
RH
876 ignore_rest_of_line ();
877 return;
252b5132 878 }
9de8d8f1 879 attr |= obj_elf_parse_section_letters (beg, strlen (beg));
252b5132
RH
880
881 SKIP_WHITESPACE ();
882 if (*input_line_pointer == ',')
883 {
9de8d8f1 884 char c;
060adf0e
AM
885 char *save = input_line_pointer;
886
252b5132
RH
887 ++input_line_pointer;
888 SKIP_WHITESPACE ();
9de8d8f1
RH
889 c = *input_line_pointer;
890 if (c == '"')
252b5132 891 {
9de8d8f1
RH
892 beg = demand_copy_C_string (&dummy);
893 if (beg == NULL)
252b5132 894 {
9de8d8f1
RH
895 ignore_rest_of_line ();
896 return;
252b5132 897 }
9de8d8f1 898 type = obj_elf_section_type (beg, strlen (beg));
9de8d8f1
RH
899 }
900 else if (c == '@' || c == '%')
901 {
902 beg = ++input_line_pointer;
903 c = get_symbol_end ();
904 *input_line_pointer = c;
905 type = obj_elf_section_type (beg, input_line_pointer - beg);
252b5132 906 }
060adf0e
AM
907 else
908 input_line_pointer = save;
252b5132 909 }
f5fa8ca2
JJ
910
911 SKIP_WHITESPACE ();
6ce8b369 912 if ((attr & SHF_MERGE) != 0 && *input_line_pointer == ',')
f5fa8ca2
JJ
913 {
914 ++input_line_pointer;
915 SKIP_WHITESPACE ();
916 entsize = get_absolute_expression ();
6ce8b369 917 SKIP_WHITESPACE ();
f5fa8ca2
JJ
918 if (entsize < 0)
919 {
6ce8b369 920 as_warn (_("invalid merge entity size"));
f5fa8ca2
JJ
921 attr &= ~SHF_MERGE;
922 entsize = 0;
923 }
924 }
6ce8b369
AM
925 else if ((attr & SHF_MERGE) != 0)
926 {
927 as_warn (_("entity size for SHF_MERGE not specified"));
928 attr &= ~SHF_MERGE;
929 }
060adf0e
AM
930
931 if ((attr & SHF_GROUP) != 0 && *input_line_pointer == ',')
932 {
933 ++input_line_pointer;
aa1f4858
AM
934 group_name = obj_elf_section_name ();
935 if (group_name == NULL)
060adf0e 936 attr &= ~SHF_GROUP;
d2dab548
AM
937 else if (strncmp (input_line_pointer, ",comdat", 7) == 0)
938 {
939 input_line_pointer += 7;
940 linkonce = 1;
941 }
942 else if (strncmp (name, ".gnu.linkonce", 13) == 0)
943 linkonce = 1;
060adf0e
AM
944 }
945 else if ((attr & SHF_GROUP) != 0)
946 {
947 as_warn (_("group name for SHF_GROUP not specified"));
948 attr &= ~SHF_GROUP;
949 }
252b5132
RH
950 }
951 else
952 {
953 do
954 {
9de8d8f1
RH
955 char c;
956
252b5132
RH
957 SKIP_WHITESPACE ();
958 if (*input_line_pointer != '#')
959 {
c95b35a9 960 as_bad (_("character following name is not '#'"));
252b5132
RH
961 ignore_rest_of_line ();
962 return;
963 }
9de8d8f1
RH
964 beg = ++input_line_pointer;
965 c = get_symbol_end ();
966 *input_line_pointer = c;
967
968 attr |= obj_elf_section_word (beg, input_line_pointer - beg);
969
252b5132
RH
970 SKIP_WHITESPACE ();
971 }
972 while (*input_line_pointer++ == ',');
973 --input_line_pointer;
974 }
975 }
976
252b5132 977 demand_empty_rest_of_line ();
9de8d8f1 978
d2dab548 979 obj_elf_change_section (name, type, attr, entsize, group_name, linkonce, push);
252b5132
RH
980}
981
982/* Change to the .data section. */
983
16b93d88 984void
dbe2df79 985obj_elf_data (int i)
252b5132
RH
986{
987#ifdef md_flush_pending_output
988 md_flush_pending_output ();
989#endif
990
991 previous_section = now_seg;
992 previous_subsection = now_subseg;
993 s_data (i);
994
995#ifdef md_elf_section_change_hook
996 md_elf_section_change_hook ();
997#endif
998}
999
1000/* Change to the .text section. */
1001
16b93d88 1002void
dbe2df79 1003obj_elf_text (int i)
252b5132
RH
1004{
1005#ifdef md_flush_pending_output
1006 md_flush_pending_output ();
1007#endif
1008
1009 previous_section = now_seg;
1010 previous_subsection = now_subseg;
1011 s_text (i);
1012
1013#ifdef md_elf_section_change_hook
1014 md_elf_section_change_hook ();
1015#endif
1016}
1017
1018static void
dbe2df79 1019obj_elf_subsection (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
1020{
1021 register int temp;
1022
1023#ifdef md_flush_pending_output
1024 md_flush_pending_output ();
1025#endif
1026
1027 previous_section = now_seg;
1028 previous_subsection = now_subseg;
1029
1030 temp = get_absolute_expression ();
1031 subseg_set (now_seg, (subsegT) temp);
1032 demand_empty_rest_of_line ();
1033
1034#ifdef md_elf_section_change_hook
1035 md_elf_section_change_hook ();
1036#endif
1037}
1038
1039/* This can be called from the processor backends if they change
1040 sections. */
1041
1042void
dbe2df79 1043obj_elf_section_change_hook (void)
252b5132
RH
1044{
1045 previous_section = now_seg;
1046 previous_subsection = now_subseg;
1047}
1048
1049void
dbe2df79 1050obj_elf_previous (int ignore ATTRIBUTE_UNUSED)
252b5132 1051{
9de8d8f1
RH
1052 segT new_section;
1053 int new_subsection;
1054
252b5132
RH
1055 if (previous_section == 0)
1056 {
6ce8b369 1057 as_warn (_(".previous without corresponding .section; ignored"));
252b5132
RH
1058 return;
1059 }
1060
1061#ifdef md_flush_pending_output
1062 md_flush_pending_output ();
1063#endif
1064
9de8d8f1
RH
1065 new_section = previous_section;
1066 new_subsection = previous_subsection;
1067 previous_section = now_seg;
1068 previous_subsection = now_subseg;
1069 subseg_set (new_section, new_subsection);
1070
1071#ifdef md_elf_section_change_hook
1072 md_elf_section_change_hook ();
1073#endif
1074}
1075
1076static void
dbe2df79 1077obj_elf_popsection (int xxx ATTRIBUTE_UNUSED)
9de8d8f1
RH
1078{
1079 struct section_stack *top = section_stack;
1080
1081 if (top == NULL)
1082 {
6ce8b369 1083 as_warn (_(".popsection without corresponding .pushsection; ignored"));
9de8d8f1
RH
1084 return;
1085 }
1086
1087#ifdef md_flush_pending_output
1088 md_flush_pending_output ();
1089#endif
1090
1091 section_stack = top->next;
1092 previous_section = top->prev_seg;
1093 previous_subsection = top->prev_subseg;
1094 subseg_set (top->seg, top->subseg);
1095 free (top);
252b5132
RH
1096
1097#ifdef md_elf_section_change_hook
1098 md_elf_section_change_hook ();
1099#endif
1100}
1101
1102static void
dbe2df79 1103obj_elf_line (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
1104{
1105 /* Assume delimiter is part of expression. BSD4.2 as fails with
bf514e21 1106 delightful bug, so we are not being incompatible here. */
dbe2df79 1107 new_logical_line (NULL, get_absolute_expression ());
252b5132
RH
1108 demand_empty_rest_of_line ();
1109}
1110
1111/* This handles the .symver pseudo-op, which is used to specify a
1112 symbol version. The syntax is ``.symver NAME,SYMVERNAME''.
1113 SYMVERNAME may contain ELF_VER_CHR ('@') characters. This
1114 pseudo-op causes the assembler to emit a symbol named SYMVERNAME
1115 with the same value as the symbol NAME. */
1116
1117static void
dbe2df79 1118obj_elf_symver (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
1119{
1120 char *name;
1121 char c;
468cced8 1122 char old_lexat;
252b5132
RH
1123 symbolS *sym;
1124
1125 name = input_line_pointer;
1126 c = get_symbol_end ();
1127
1128 sym = symbol_find_or_make (name);
1129
1130 *input_line_pointer = c;
1131
252b5132
RH
1132 SKIP_WHITESPACE ();
1133 if (*input_line_pointer != ',')
1134 {
1135 as_bad (_("expected comma after name in .symver"));
1136 ignore_rest_of_line ();
1137 return;
1138 }
1139
1140 ++input_line_pointer;
eba874d8 1141 SKIP_WHITESPACE ();
252b5132 1142 name = input_line_pointer;
468cced8
AM
1143
1144 /* Temporarily include '@' in symbol names. */
1145 old_lexat = lex_type[(unsigned char) '@'];
1146 lex_type[(unsigned char) '@'] |= LEX_NAME;
1147 c = get_symbol_end ();
1148 lex_type[(unsigned char) '@'] = old_lexat;
252b5132 1149
339681c0
L
1150 if (symbol_get_obj (sym)->versioned_name == NULL)
1151 {
1152 symbol_get_obj (sym)->versioned_name = xstrdup (name);
252b5132 1153
339681c0 1154 *input_line_pointer = c;
252b5132 1155
6f620856
L
1156 if (strchr (symbol_get_obj (sym)->versioned_name,
1157 ELF_VER_CHR) == NULL)
339681c0
L
1158 {
1159 as_bad (_("missing version name in `%s' for symbol `%s'"),
1160 symbol_get_obj (sym)->versioned_name,
1161 S_GET_NAME (sym));
1162 ignore_rest_of_line ();
1163 return;
1164 }
1165 }
1166 else
252b5132 1167 {
339681c0
L
1168 if (strcmp (symbol_get_obj (sym)->versioned_name, name))
1169 {
1170 as_bad (_("multiple versions [`%s'|`%s'] for symbol `%s'"),
1171 name, symbol_get_obj (sym)->versioned_name,
1172 S_GET_NAME (sym));
1173 ignore_rest_of_line ();
1174 return;
1175 }
1176
1177 *input_line_pointer = c;
252b5132
RH
1178 }
1179
1180 demand_empty_rest_of_line ();
1181}
1182
1183/* This handles the .vtable_inherit pseudo-op, which is used to indicate
1184 to the linker the hierarchy in which a particular table resides. The
1185 syntax is ".vtable_inherit CHILDNAME, PARENTNAME". */
1186
904a31bf 1187struct fix *
dbe2df79 1188obj_elf_vtable_inherit (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
1189{
1190 char *cname, *pname;
1191 symbolS *csym, *psym;
1192 char c, bad = 0;
1193
1194 if (*input_line_pointer == '#')
1195 ++input_line_pointer;
1196
1197 cname = input_line_pointer;
1198 c = get_symbol_end ();
1199 csym = symbol_find (cname);
1200
1201 /* GCFIXME: should check that we don't have two .vtable_inherits for
1202 the same child symbol. Also, we can currently only do this if the
1203 child symbol is already exists and is placed in a fragment. */
1204
49309057 1205 if (csym == NULL || symbol_get_frag (csym) == NULL)
252b5132
RH
1206 {
1207 as_bad ("expected `%s' to have already been set for .vtable_inherit",
1208 cname);
1209 bad = 1;
1210 }
1211
1212 *input_line_pointer = c;
1213
1214 SKIP_WHITESPACE ();
1215 if (*input_line_pointer != ',')
1216 {
1217 as_bad ("expected comma after name in .vtable_inherit");
1218 ignore_rest_of_line ();
904a31bf 1219 return NULL;
252b5132
RH
1220 }
1221
1222 ++input_line_pointer;
1223 SKIP_WHITESPACE ();
1224
1225 if (*input_line_pointer == '#')
1226 ++input_line_pointer;
1227
1228 if (input_line_pointer[0] == '0'
1229 && (input_line_pointer[1] == '\0'
3882b010 1230 || ISSPACE (input_line_pointer[1])))
252b5132
RH
1231 {
1232 psym = section_symbol (absolute_section);
1233 ++input_line_pointer;
1234 }
1235 else
1236 {
1237 pname = input_line_pointer;
1238 c = get_symbol_end ();
1239 psym = symbol_find_or_make (pname);
1240 *input_line_pointer = c;
1241 }
1242
1243 demand_empty_rest_of_line ();
1244
1245 if (bad)
904a31bf 1246 return NULL;
252b5132 1247
49309057 1248 assert (symbol_get_value_expression (csym)->X_op == O_constant);
904a31bf
AM
1249 return fix_new (symbol_get_frag (csym),
1250 symbol_get_value_expression (csym)->X_add_number,
1251 0, psym, 0, 0, BFD_RELOC_VTABLE_INHERIT);
252b5132 1252}
fa306131 1253
252b5132
RH
1254/* This handles the .vtable_entry pseudo-op, which is used to indicate
1255 to the linker that a vtable slot was used. The syntax is
1256 ".vtable_entry tablename, offset". */
1257
904a31bf 1258struct fix *
dbe2df79 1259obj_elf_vtable_entry (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
1260{
1261 char *name;
1262 symbolS *sym;
1263 offsetT offset;
1264 char c;
1265
1266 if (*input_line_pointer == '#')
1267 ++input_line_pointer;
1268
1269 name = input_line_pointer;
1270 c = get_symbol_end ();
1271 sym = symbol_find_or_make (name);
1272 *input_line_pointer = c;
1273
1274 SKIP_WHITESPACE ();
1275 if (*input_line_pointer != ',')
1276 {
1277 as_bad ("expected comma after name in .vtable_entry");
1278 ignore_rest_of_line ();
904a31bf 1279 return NULL;
252b5132
RH
1280 }
1281
1282 ++input_line_pointer;
1283 if (*input_line_pointer == '#')
1284 ++input_line_pointer;
1285
1286 offset = get_absolute_expression ();
1287
252b5132 1288 demand_empty_rest_of_line ();
904a31bf
AM
1289
1290 return fix_new (frag_now, frag_now_fix (), 0, sym, offset, 0,
1291 BFD_RELOC_VTABLE_ENTRY);
252b5132
RH
1292}
1293
1294void
dbe2df79 1295elf_obj_read_begin_hook (void)
252b5132
RH
1296{
1297#ifdef NEED_ECOFF_DEBUG
1298 if (ECOFF_DEBUGGING)
1299 ecoff_read_begin_hook ();
1300#endif
1301}
1302
1303void
dbe2df79 1304elf_obj_symbol_new_hook (symbolS *symbolP)
252b5132 1305{
49309057
ILT
1306 struct elf_obj_sy *sy_obj;
1307
1308 sy_obj = symbol_get_obj (symbolP);
1309 sy_obj->size = NULL;
1310 sy_obj->versioned_name = NULL;
252b5132
RH
1311
1312#ifdef NEED_ECOFF_DEBUG
1313 if (ECOFF_DEBUGGING)
1314 ecoff_symbol_new_hook (symbolP);
1315#endif
1316}
1317
8fd3e36b
AM
1318/* When setting one symbol equal to another, by default we probably
1319 want them to have the same "size", whatever it means in the current
1320 context. */
1321
1322void
dbe2df79 1323elf_copy_symbol_attributes (symbolS *dest, symbolS *src)
8fd3e36b 1324{
060adf0e
AM
1325 struct elf_obj_sy *srcelf = symbol_get_obj (src);
1326 struct elf_obj_sy *destelf = symbol_get_obj (dest);
1327 if (srcelf->size)
1328 {
1329 if (destelf->size == NULL)
dbe2df79 1330 destelf->size = xmalloc (sizeof (expressionS));
060adf0e
AM
1331 *destelf->size = *srcelf->size;
1332 }
1333 else
1334 {
1335 if (destelf->size != NULL)
1336 free (destelf->size);
1337 destelf->size = NULL;
1338 }
1339 S_SET_SIZE (dest, S_GET_SIZE (src));
26eb4093
JJ
1340 /* Don't copy visibility. */
1341 S_SET_OTHER (dest, (ELF_ST_VISIBILITY (S_GET_OTHER (dest))
1342 | (S_GET_OTHER (src) & ~ELF_ST_VISIBILITY (-1))));
8fd3e36b
AM
1343}
1344
252b5132 1345void
dbe2df79 1346obj_elf_version (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
1347{
1348 char *name;
1349 unsigned int c;
252b5132
RH
1350 char *p;
1351 asection *seg = now_seg;
1352 subsegT subseg = now_subseg;
1353 Elf_Internal_Note i_note;
1354 Elf_External_Note e_note;
dbe2df79 1355 asection *note_secp = NULL;
e4afe742 1356 int len;
252b5132
RH
1357
1358 SKIP_WHITESPACE ();
1359 if (*input_line_pointer == '\"')
1360 {
bf514e21 1361 ++input_line_pointer; /* -> 1st char of string. */
252b5132
RH
1362 name = input_line_pointer;
1363
1364 while (is_a_char (c = next_char_of_string ()))
1365 ;
1366 c = *input_line_pointer;
1367 *input_line_pointer = '\0';
1368 *(input_line_pointer - 1) = '\0';
1369 *input_line_pointer = c;
1370
1371 /* create the .note section */
1372
1373 note_secp = subseg_new (".note", 0);
1374 bfd_set_section_flags (stdoutput,
1375 note_secp,
1376 SEC_HAS_CONTENTS | SEC_READONLY);
1377
1378 /* process the version string */
1379
1380 len = strlen (name);
1381
1382 i_note.namesz = ((len + 1) + 3) & ~3; /* round this to word boundary */
1383 i_note.descsz = 0; /* no description */
1384 i_note.type = NT_VERSION;
1385 p = frag_more (sizeof (e_note.namesz));
dbe2df79 1386 md_number_to_chars (p, i_note.namesz, sizeof (e_note.namesz));
252b5132 1387 p = frag_more (sizeof (e_note.descsz));
dbe2df79 1388 md_number_to_chars (p, i_note.descsz, sizeof (e_note.descsz));
252b5132 1389 p = frag_more (sizeof (e_note.type));
dbe2df79 1390 md_number_to_chars (p, i_note.type, sizeof (e_note.type));
e4afe742
AM
1391 p = frag_more (len + 1);
1392 strcpy (p, name);
252b5132 1393
252b5132
RH
1394 frag_align (2, 0, 0);
1395
1396 subseg_set (seg, subseg);
1397 }
1398 else
1399 {
6ce8b369 1400 as_bad (_("expected quoted string"));
252b5132
RH
1401 }
1402 demand_empty_rest_of_line ();
1403}
1404
1405static void
dbe2df79 1406obj_elf_size (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
1407{
1408 char *name = input_line_pointer;
1409 char c = get_symbol_end ();
1410 char *p;
1411 expressionS exp;
1412 symbolS *sym;
1413
1414 p = input_line_pointer;
1415 *p = c;
1416 SKIP_WHITESPACE ();
1417 if (*input_line_pointer != ',')
1418 {
1419 *p = 0;
1420 as_bad (_("expected comma after name `%s' in .size directive"), name);
1421 *p = c;
1422 ignore_rest_of_line ();
1423 return;
1424 }
1425 input_line_pointer++;
1426 expression (&exp);
1427 if (exp.X_op == O_absent)
1428 {
1429 as_bad (_("missing expression in .size directive"));
1430 exp.X_op = O_constant;
1431 exp.X_add_number = 0;
1432 }
1433 *p = 0;
1434 sym = symbol_find_or_make (name);
1435 *p = c;
1436 if (exp.X_op == O_constant)
c538998c
JJ
1437 {
1438 S_SET_SIZE (sym, exp.X_add_number);
1439 if (symbol_get_obj (sym)->size)
1440 {
1441 xfree (symbol_get_obj (sym)->size);
1442 symbol_get_obj (sym)->size = NULL;
1443 }
1444 }
252b5132
RH
1445 else
1446 {
dbe2df79 1447 symbol_get_obj (sym)->size = xmalloc (sizeof (expressionS));
49309057 1448 *symbol_get_obj (sym)->size = exp;
252b5132
RH
1449 }
1450 demand_empty_rest_of_line ();
1451}
1452
1453/* Handle the ELF .type pseudo-op. This sets the type of a symbol.
aa8c34c3 1454 There are five syntaxes:
fa306131 1455
252b5132
RH
1456 The first (used on Solaris) is
1457 .type SYM,#function
1458 The second (used on UnixWare) is
1459 .type SYM,@function
1460 The third (reportedly to be used on Irix 6.0) is
1461 .type SYM STT_FUNC
1462 The fourth (used on NetBSD/Arm and Linux/ARM) is
1463 .type SYM,%function
aa8c34c3
JE
1464 The fifth (used on SVR4/860) is
1465 .type SYM,"function"
252b5132
RH
1466 */
1467
1468static void
dbe2df79 1469obj_elf_type (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
1470{
1471 char *name;
1472 char c;
1473 int type;
1474 const char *typename;
1475 symbolS *sym;
904a31bf 1476 elf_symbol_type *elfsym;
252b5132
RH
1477
1478 name = input_line_pointer;
1479 c = get_symbol_end ();
1480 sym = symbol_find_or_make (name);
904a31bf 1481 elfsym = (elf_symbol_type *) symbol_get_bfdsym (sym);
252b5132
RH
1482 *input_line_pointer = c;
1483
1484 SKIP_WHITESPACE ();
1485 if (*input_line_pointer == ',')
1486 ++input_line_pointer;
1487
1488 SKIP_WHITESPACE ();
1489 if ( *input_line_pointer == '#'
1490 || *input_line_pointer == '@'
aa8c34c3 1491 || *input_line_pointer == '"'
252b5132
RH
1492 || *input_line_pointer == '%')
1493 ++input_line_pointer;
1494
1495 typename = input_line_pointer;
1496 c = get_symbol_end ();
1497
1498 type = 0;
1499 if (strcmp (typename, "function") == 0
1500 || strcmp (typename, "STT_FUNC") == 0)
1501 type = BSF_FUNCTION;
1502 else if (strcmp (typename, "object") == 0
1503 || strcmp (typename, "STT_OBJECT") == 0)
1504 type = BSF_OBJECT;
b9734f35
JJ
1505 else if (strcmp (typename, "tls_object") == 0
1506 || strcmp (typename, "STT_TLS") == 0)
1507 type = BSF_OBJECT | BSF_THREAD_LOCAL;
e7b9a8c1
L
1508 else if (strcmp (typename, "notype") == 0
1509 || strcmp (typename, "STT_NOTYPE") == 0)
1510 ;
904a31bf
AM
1511#ifdef md_elf_symbol_type
1512 else if ((type = md_elf_symbol_type (typename, sym, elfsym)) != -1)
1513 ;
1514#endif
252b5132 1515 else
6ce8b369 1516 as_bad (_("unrecognized symbol type \"%s\""), typename);
252b5132
RH
1517
1518 *input_line_pointer = c;
1519
aa8c34c3
JE
1520 if (*input_line_pointer == '"')
1521 ++input_line_pointer;
1522
904a31bf 1523 elfsym->symbol.flags |= type;
252b5132
RH
1524
1525 demand_empty_rest_of_line ();
1526}
1527
1528static void
dbe2df79 1529obj_elf_ident (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
1530{
1531 static segT comment_section;
1532 segT old_section = now_seg;
1533 int old_subsection = now_subseg;
1534
5f91fe03
ILT
1535#ifdef md_flush_pending_output
1536 md_flush_pending_output ();
1537#endif
1538
252b5132
RH
1539 if (!comment_section)
1540 {
1541 char *p;
1542 comment_section = subseg_new (".comment", 0);
1543 bfd_set_section_flags (stdoutput, comment_section,
1544 SEC_READONLY | SEC_HAS_CONTENTS);
1545 p = frag_more (1);
1546 *p = 0;
1547 }
1548 else
1549 subseg_set (comment_section, 0);
1550 stringer (1);
1551 subseg_set (old_section, old_subsection);
1552}
1553
1554#ifdef INIT_STAB_SECTION
1555
1556/* The first entry in a .stabs section is special. */
1557
1558void
dbe2df79 1559obj_elf_init_stab_section (segT seg)
252b5132
RH
1560{
1561 char *file;
1562 char *p;
1563 char *stabstr_name;
1564 unsigned int stroff;
1565
1566 /* Force the section to align to a longword boundary. Without this,
1567 UnixWare ar crashes. */
1568 bfd_set_section_alignment (stdoutput, seg, 2);
1569
bf514e21 1570 /* Make space for this first symbol. */
252b5132 1571 p = frag_more (12);
bf514e21 1572 /* Zero it out. */
252b5132 1573 memset (p, 0, 12);
dbe2df79
AM
1574 as_where (&file, NULL);
1575 stabstr_name = xmalloc (strlen (segment_name (seg)) + 4);
252b5132
RH
1576 strcpy (stabstr_name, segment_name (seg));
1577 strcat (stabstr_name, "str");
1578 stroff = get_stab_string_offset (file, stabstr_name);
1579 know (stroff == 1);
1580 md_number_to_chars (p, stroff, 4);
1581 seg_info (seg)->stabu.p = p;
1582}
1583
1584#endif
1585
1586/* Fill in the counts in the first entry in a .stabs section. */
1587
1588static void
dbe2df79 1589adjust_stab_sections (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
252b5132
RH
1590{
1591 char *name;
1592 asection *strsec;
1593 char *p;
1594 int strsz, nsyms;
1595
1596 if (strncmp (".stab", sec->name, 5))
1597 return;
1598 if (!strcmp ("str", sec->name + strlen (sec->name) - 3))
1599 return;
1600
dbe2df79 1601 name = alloca (strlen (sec->name) + 4);
252b5132
RH
1602 strcpy (name, sec->name);
1603 strcat (name, "str");
1604 strsec = bfd_get_section_by_name (abfd, name);
1605 if (strsec)
1606 strsz = bfd_section_size (abfd, strsec);
1607 else
1608 strsz = 0;
1609 nsyms = bfd_section_size (abfd, sec) / 12 - 1;
1610
1611 p = seg_info (sec)->stabu.p;
1612 assert (p != 0);
1613
dbe2df79
AM
1614 bfd_h_put_16 (abfd, nsyms, p + 6);
1615 bfd_h_put_32 (abfd, strsz, p + 8);
252b5132
RH
1616}
1617
1618#ifdef NEED_ECOFF_DEBUG
1619
1620/* This function is called by the ECOFF code. It is supposed to
1621 record the external symbol information so that the backend can
1622 write it out correctly. The ELF backend doesn't actually handle
1623 this at the moment, so we do it ourselves. We save the information
1624 in the symbol. */
1625
1626void
dbe2df79 1627elf_ecoff_set_ext (symbolS *sym, struct ecoff_extr *ext)
252b5132 1628{
dbe2df79 1629 symbol_get_bfdsym (sym)->udata.p = ext;
252b5132
RH
1630}
1631
1632/* This function is called by bfd_ecoff_debug_externals. It is
1633 supposed to *EXT to the external symbol information, and return
1634 whether the symbol should be used at all. */
1635
b34976b6 1636static bfd_boolean
dbe2df79 1637elf_get_extr (asymbol *sym, EXTR *ext)
252b5132
RH
1638{
1639 if (sym->udata.p == NULL)
b34976b6 1640 return FALSE;
252b5132 1641 *ext = *(EXTR *) sym->udata.p;
b34976b6 1642 return TRUE;
252b5132
RH
1643}
1644
1645/* This function is called by bfd_ecoff_debug_externals. It has
1646 nothing to do for ELF. */
1647
252b5132 1648static void
dbe2df79
AM
1649elf_set_index (asymbol *sym ATTRIBUTE_UNUSED,
1650 bfd_size_type indx ATTRIBUTE_UNUSED)
252b5132
RH
1651{
1652}
1653
1654#endif /* NEED_ECOFF_DEBUG */
1655
1656void
dbe2df79 1657elf_frob_symbol (symbolS *symp, int *puntp)
252b5132 1658{
49309057
ILT
1659 struct elf_obj_sy *sy_obj;
1660
252b5132
RH
1661#ifdef NEED_ECOFF_DEBUG
1662 if (ECOFF_DEBUGGING)
1663 ecoff_frob_symbol (symp);
1664#endif
1665
49309057
ILT
1666 sy_obj = symbol_get_obj (symp);
1667
1668 if (sy_obj->size != NULL)
252b5132 1669 {
49309057 1670 switch (sy_obj->size->X_op)
252b5132
RH
1671 {
1672 case O_subtract:
1673 S_SET_SIZE (symp,
49309057
ILT
1674 (S_GET_VALUE (sy_obj->size->X_add_symbol)
1675 + sy_obj->size->X_add_number
1676 - S_GET_VALUE (sy_obj->size->X_op_symbol)));
252b5132
RH
1677 break;
1678 case O_constant:
1679 S_SET_SIZE (symp,
49309057
ILT
1680 (S_GET_VALUE (sy_obj->size->X_add_symbol)
1681 + sy_obj->size->X_add_number));
252b5132
RH
1682 break;
1683 default:
1684 as_bad (_(".size expression too complicated to fix up"));
1685 break;
1686 }
49309057
ILT
1687 free (sy_obj->size);
1688 sy_obj->size = NULL;
252b5132
RH
1689 }
1690
49309057 1691 if (sy_obj->versioned_name != NULL)
252b5132 1692 {
79082ff0
L
1693 char *p;
1694
1695 p = strchr (sy_obj->versioned_name, ELF_VER_CHR);
1696 know (p != NULL);
1697
252b5132
RH
1698 /* This symbol was given a new name with the .symver directive.
1699
13c56984
AM
1700 If this is an external reference, just rename the symbol to
1701 include the version string. This will make the relocs be
1702 against the correct versioned symbol.
252b5132
RH
1703
1704 If this is a definition, add an alias. FIXME: Using an alias
1705 will permit the debugging information to refer to the right
1706 symbol. However, it's not clear whether it is the best
1707 approach. */
1708
1709 if (! S_IS_DEFINED (symp))
1710 {
252b5132 1711 /* Verify that the name isn't using the @@ syntax--this is
13c56984
AM
1712 reserved for definitions of the default version to link
1713 against. */
252b5132
RH
1714 if (p[1] == ELF_VER_CHR)
1715 {
1716 as_bad (_("invalid attempt to declare external version name as default in symbol `%s'"),
49309057 1717 sy_obj->versioned_name);
b34976b6 1718 *puntp = TRUE;
252b5132 1719 }
49309057 1720 S_SET_NAME (symp, sy_obj->versioned_name);
252b5132
RH
1721 }
1722 else
1723 {
dbe2df79 1724 if (p[1] == ELF_VER_CHR && p[2] == ELF_VER_CHR)
79082ff0
L
1725 {
1726 size_t l;
1727
1728 /* The @@@ syntax is a special case. It renames the
1729 symbol name to versioned_name with one `@' removed. */
1730 l = strlen (&p[3]) + 1;
dbe2df79 1731 memmove (&p[2], &p[3], l);
79082ff0
L
1732 S_SET_NAME (symp, sy_obj->versioned_name);
1733 }
1734 else
1735 {
1736 symbolS *symp2;
252b5132 1737
79082ff0
L
1738 /* FIXME: Creating a new symbol here is risky. We're
1739 in the final loop over the symbol table. We can
1740 get away with it only because the symbol goes to
1741 the end of the list, where the loop will still see
1742 it. It would probably be better to do this in
1743 obj_frob_file_before_adjust. */
252b5132 1744
79082ff0 1745 symp2 = symbol_find_or_make (sy_obj->versioned_name);
252b5132 1746
79082ff0 1747 /* Now we act as though we saw symp2 = sym. */
252b5132 1748
79082ff0 1749 S_SET_SEGMENT (symp2, S_GET_SEGMENT (symp));
252b5132 1750
79082ff0
L
1751 /* Subtracting out the frag address here is a hack
1752 because we are in the middle of the final loop. */
1753 S_SET_VALUE (symp2,
1754 (S_GET_VALUE (symp)
1755 - symbol_get_frag (symp)->fr_address));
252b5132 1756
79082ff0 1757 symbol_set_frag (symp2, symbol_get_frag (symp));
252b5132 1758
79082ff0
L
1759 /* This will copy over the size information. */
1760 copy_symbol_attributes (symp2, symp);
252b5132 1761
26eb4093
JJ
1762 S_SET_OTHER (symp2, S_GET_OTHER (symp));
1763
79082ff0
L
1764 if (S_IS_WEAK (symp))
1765 S_SET_WEAK (symp2);
252b5132 1766
79082ff0
L
1767 if (S_IS_EXTERNAL (symp))
1768 S_SET_EXTERNAL (symp2);
1769 }
252b5132
RH
1770 }
1771 }
1772
1773 /* Double check weak symbols. */
49309057 1774 if (S_IS_WEAK (symp))
252b5132
RH
1775 {
1776 if (S_IS_COMMON (symp))
6ce8b369 1777 as_bad (_("symbol `%s' can not be both weak and common"),
252b5132
RH
1778 S_GET_NAME (symp));
1779 }
1780
1781#ifdef TC_MIPS
1782 /* The Irix 5 and 6 assemblers set the type of any common symbol and
1783 any undefined non-function symbol to STT_OBJECT. We try to be
1784 compatible, since newer Irix 5 and 6 linkers care. However, we
1785 only set undefined symbols to be STT_OBJECT if we are on Irix,
1786 because that is the only time gcc will generate the necessary
1787 .global directives to mark functions. */
1788
1789 if (S_IS_COMMON (symp))
49309057 1790 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
252b5132
RH
1791
1792 if (strstr (TARGET_OS, "irix") != NULL
49309057
ILT
1793 && ! S_IS_DEFINED (symp)
1794 && (symbol_get_bfdsym (symp)->flags & BSF_FUNCTION) == 0)
1795 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
252b5132
RH
1796#endif
1797
c5e54cc2
ILT
1798#if 0 /* TC_PPC */
1799 /* If TC_PPC is defined, we used to force the type of a symbol to be
1800 BSF_OBJECT if it was otherwise unset. This was required by some
1801 version of VxWorks. Thomas de Lellis <tdel@windriver.com> says
1802 that this is no longer needed, so it is now commented out. */
49309057
ILT
1803 if ((symbol_get_bfdsym (symp)->flags
1804 & (BSF_FUNCTION | BSF_FILE | BSF_SECTION_SYM)) == 0
252b5132 1805 && S_IS_DEFINED (symp))
49309057 1806 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
252b5132
RH
1807#endif
1808}
1809
060adf0e
AM
1810struct group_list
1811{
1812 asection **head; /* Section lists. */
1813 unsigned int *elt_count; /* Number of sections in each list. */
1814 unsigned int num_group; /* Number of lists. */
1815};
1816
1817/* Called via bfd_map_over_sections. If SEC is a member of a group,
1818 add it to a list of sections belonging to the group. INF is a
1819 pointer to a struct group_list, which is where we store the head of
1820 each list. */
1821
1822static void
dbe2df79 1823build_group_lists (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
060adf0e 1824{
dbe2df79 1825 struct group_list *list = inf;
aa1f4858 1826 const char *group_name = elf_group_name (sec);
060adf0e
AM
1827 unsigned int i;
1828
1829 if (group_name == NULL)
1830 return;
1831
1832 /* If this group already has a list, add the section to the head of
1833 the list. */
1834 for (i = 0; i < list->num_group; i++)
1835 {
aa1f4858 1836 if (strcmp (group_name, elf_group_name (list->head[i])) == 0)
060adf0e 1837 {
aa1f4858 1838 elf_next_in_group (sec) = list->head[i];
060adf0e
AM
1839 list->head[i] = sec;
1840 list->elt_count[i] += 1;
1841 return;
1842 }
1843 }
1844
1845 /* New group. Make the arrays bigger in chunks to minimize calls to
1846 realloc. */
1847 i = list->num_group;
1848 if ((i & 127) == 0)
1849 {
1850 unsigned int newsize = i + 128;
1851 list->head = xrealloc (list->head, newsize * sizeof (*list->head));
1852 list->elt_count = xrealloc (list->elt_count,
1853 newsize * sizeof (*list->elt_count));
1854 }
1855 list->head[i] = sec;
1856 list->elt_count[i] = 1;
1857 list->num_group += 1;
1858}
1859
252b5132 1860void
dbe2df79 1861elf_frob_file (void)
252b5132 1862{
060adf0e
AM
1863 struct group_list list;
1864 unsigned int i;
1865
dbe2df79 1866 bfd_map_over_sections (stdoutput, adjust_stab_sections, NULL);
252b5132 1867
060adf0e
AM
1868 /* Go find section groups. */
1869 list.num_group = 0;
1870 list.head = NULL;
1871 list.elt_count = NULL;
dbe2df79 1872 bfd_map_over_sections (stdoutput, build_group_lists, &list);
060adf0e
AM
1873
1874 /* Make the SHT_GROUP sections that describe each section group. We
1875 can't set up the section contents here yet, because elf section
1876 indices have yet to be calculated. elf.c:set_group_contents does
1877 the rest of the work. */
1878 for (i = 0; i < list.num_group; i++)
1879 {
aa1f4858 1880 const char *group_name = elf_group_name (list.head[i]);
9758f3fc 1881 const char *sec_name;
060adf0e
AM
1882 asection *s;
1883 flagword flags;
9758f3fc
AM
1884 struct symbol *sy;
1885 int has_sym;
060adf0e 1886
060adf0e 1887 flags = SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_GROUP;
d2dab548 1888 for (s = list.head[i]; s != NULL; s = elf_next_in_group (s))
68bfbfcc 1889 if ((s->flags ^ flags) & SEC_LINK_ONCE)
d2dab548
AM
1890 {
1891 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1892 if (s != list.head[i])
1893 {
1894 as_warn (_("assuming all members of group `%s' are COMDAT"),
1895 group_name);
1896 break;
1897 }
1898 }
1899
9758f3fc
AM
1900 sec_name = group_name;
1901 sy = symbol_find_exact (group_name);
1902 has_sym = 0;
1903 if (sy != NULL
1904 && (sy == symbol_lastP
1905 || (sy->sy_next != NULL
1906 && sy->sy_next->sy_previous == sy)))
1907 {
1908 has_sym = 1;
1909 sec_name = ".group";
1910 }
1911 s = subseg_force_new (sec_name, 0);
060adf0e
AM
1912 if (s == NULL
1913 || !bfd_set_section_flags (stdoutput, s, flags)
1914 || !bfd_set_section_alignment (stdoutput, s, 2))
1915 {
1916 as_fatal (_("can't create group: %s"),
1917 bfd_errmsg (bfd_get_error ()));
1918 }
2f89ff8d 1919 elf_section_type (s) = SHT_GROUP;
060adf0e 1920
aa1f4858
AM
1921 /* Pass a pointer to the first section in this group. */
1922 elf_next_in_group (s) = list.head[i];
9758f3fc
AM
1923 if (has_sym)
1924 elf_group_id (s) = sy->bsym;
060adf0e
AM
1925
1926 s->_raw_size = 4 * (list.elt_count[i] + 1);
1927 s->contents = frag_more (s->_raw_size);
1928 frag_now->fr_fix = frag_now_fix_octets ();
1929 }
1930
252b5132
RH
1931#ifdef elf_tc_final_processing
1932 elf_tc_final_processing ();
1933#endif
1934}
1935
4a1805b1 1936/* It removes any unneeded versioned symbols from the symbol table. */
339681c0
L
1937
1938void
dbe2df79 1939elf_frob_file_before_adjust (void)
339681c0
L
1940{
1941 if (symbol_rootP)
1942 {
1943 symbolS *symp;
1944
1945 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
00e6e13d 1946 if (!S_IS_DEFINED (symp))
79082ff0 1947 {
00e6e13d 1948 if (symbol_get_obj (symp)->versioned_name)
79082ff0
L
1949 {
1950 char *p;
1951
1952 /* The @@@ syntax is a special case. If the symbol is
1953 not defined, 2 `@'s will be removed from the
1954 versioned_name. */
1955
1956 p = strchr (symbol_get_obj (symp)->versioned_name,
1957 ELF_VER_CHR);
1958 know (p != NULL);
dbe2df79 1959 if (p[1] == ELF_VER_CHR && p[2] == ELF_VER_CHR)
79082ff0
L
1960 {
1961 size_t l = strlen (&p[3]) + 1;
dbe2df79 1962 memmove (&p[1], &p[3], l);
79082ff0
L
1963 }
1964 if (symbol_used_p (symp) == 0
1965 && symbol_used_in_reloc_p (symp) == 0)
1966 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1967 }
00e6e13d
JJ
1968
1969 /* If there was .weak foo, but foo was neither defined nor
1970 used anywhere, remove it. */
1971
1972 else if (S_IS_WEAK (symp)
1973 && symbol_used_p (symp) == 0
1974 && symbol_used_in_reloc_p (symp) == 0)
1975 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
79082ff0 1976 }
339681c0
L
1977 }
1978}
1979
252b5132
RH
1980/* It is required that we let write_relocs have the opportunity to
1981 optimize away fixups before output has begun, since it is possible
1982 to eliminate all fixups for a section and thus we never should
1983 have generated the relocation section. */
1984
1985void
dbe2df79 1986elf_frob_file_after_relocs (void)
252b5132
RH
1987{
1988#ifdef NEED_ECOFF_DEBUG
1989 if (ECOFF_DEBUGGING)
1990 /* Generate the ECOFF debugging information. */
1991 {
1992 const struct ecoff_debug_swap *debug_swap;
1993 struct ecoff_debug_info debug;
1994 char *buf;
1995 asection *sec;
1996
1997 debug_swap
1998 = get_elf_backend_data (stdoutput)->elf_backend_ecoff_debug_swap;
dbe2df79 1999 know (debug_swap != NULL);
252b5132
RH
2000 ecoff_build_debug (&debug.symbolic_header, &buf, debug_swap);
2001
2002 /* Set up the pointers in debug. */
2003#define SET(ptr, offset, type) \
2004 debug.ptr = (type) (buf + debug.symbolic_header.offset)
2005
2006 SET (line, cbLineOffset, unsigned char *);
dbe2df79
AM
2007 SET (external_dnr, cbDnOffset, void *);
2008 SET (external_pdr, cbPdOffset, void *);
2009 SET (external_sym, cbSymOffset, void *);
2010 SET (external_opt, cbOptOffset, void *);
252b5132
RH
2011 SET (external_aux, cbAuxOffset, union aux_ext *);
2012 SET (ss, cbSsOffset, char *);
dbe2df79
AM
2013 SET (external_fdr, cbFdOffset, void *);
2014 SET (external_rfd, cbRfdOffset, void *);
252b5132
RH
2015 /* ssext and external_ext are set up just below. */
2016
2017#undef SET
2018
2019 /* Set up the external symbols. */
2020 debug.ssext = debug.ssext_end = NULL;
2021 debug.external_ext = debug.external_ext_end = NULL;
b34976b6 2022 if (! bfd_ecoff_debug_externals (stdoutput, &debug, debug_swap, TRUE,
252b5132 2023 elf_get_extr, elf_set_index))
6ce8b369 2024 as_fatal (_("failed to set up debugging information: %s"),
252b5132
RH
2025 bfd_errmsg (bfd_get_error ()));
2026
2027 sec = bfd_get_section_by_name (stdoutput, ".mdebug");
2028 assert (sec != NULL);
2029
b34976b6 2030 know (!stdoutput->output_has_begun);
252b5132
RH
2031
2032 /* We set the size of the section, call bfd_set_section_contents
2033 to force the ELF backend to allocate a file position, and then
2034 write out the data. FIXME: Is this really the best way to do
2035 this? */
2036 sec->_raw_size = bfd_ecoff_debug_size (stdoutput, &debug, debug_swap);
2037
5f91fe03 2038 /* Pass BUF to bfd_set_section_contents because this will
13c56984
AM
2039 eventually become a call to fwrite, and ISO C prohibits
2040 passing a NULL pointer to a stdio function even if the
2041 pointer will not be used. */
dbe2df79 2042 if (! bfd_set_section_contents (stdoutput, sec, buf, 0, 0))
6ce8b369 2043 as_fatal (_("can't start writing .mdebug section: %s"),
252b5132
RH
2044 bfd_errmsg (bfd_get_error ()));
2045
b34976b6 2046 know (stdoutput->output_has_begun);
252b5132
RH
2047 know (sec->filepos != 0);
2048
2049 if (! bfd_ecoff_write_debug (stdoutput, &debug, debug_swap,
2050 sec->filepos))
6ce8b369 2051 as_fatal (_("could not write .mdebug section: %s"),
252b5132
RH
2052 bfd_errmsg (bfd_get_error ()));
2053 }
2054#endif /* NEED_ECOFF_DEBUG */
2055}
2056
2057#ifdef SCO_ELF
2058
aaa2624b 2059/* Heavily plagiarized from obj_elf_version. The idea is to emit the
252b5132
RH
2060 SCO specific identifier in the .notes section to satisfy the SCO
2061 linker.
2062
2063 This looks more complicated than it really is. As opposed to the
2064 "obvious" solution, this should handle the cross dev cases
2065 correctly. (i.e, hosting on a 64 bit big endian processor, but
2066 generating SCO Elf code) Efficiency isn't a concern, as there
2067 should be exactly one of these sections per object module.
2068
2069 SCO OpenServer 5 identifies it's ELF modules with a standard ELF
2070 .note section.
2071
fa306131
AM
2072 int_32 namesz = 4 ; Name size
2073 int_32 descsz = 12 ; Descriptive information
2074 int_32 type = 1 ;
2075 char name[4] = "SCO" ; Originator name ALWAYS SCO + NULL
252b5132
RH
2076 int_32 version = (major ver # << 16) | version of tools ;
2077 int_32 source = (tool_id << 16 ) | 1 ;
2078 int_32 info = 0 ; These are set by the SCO tools, but we
13c56984 2079 don't know enough about the source
252b5132
RH
2080 environment to set them. SCO ld currently
2081 ignores them, and recommends we set them
2082 to zero. */
2083
2084#define SCO_MAJOR_VERSION 0x1
2085#define SCO_MINOR_VERSION 0x1
2086
2087void
dbe2df79 2088sco_id (void)
252b5132
RH
2089{
2090
2091 char *name;
2092 unsigned int c;
2093 char ch;
2094 char *p;
2095 asection *seg = now_seg;
2096 subsegT subseg = now_subseg;
2097 Elf_Internal_Note i_note;
2098 Elf_External_Note e_note;
dbe2df79 2099 asection *note_secp = NULL;
252b5132
RH
2100 int i, len;
2101
2102 /* create the .note section */
2103
2104 note_secp = subseg_new (".note", 0);
2105 bfd_set_section_flags (stdoutput,
2106 note_secp,
2107 SEC_HAS_CONTENTS | SEC_READONLY);
2108
2109 /* process the version string */
2110
fa306131 2111 i_note.namesz = 4;
252b5132
RH
2112 i_note.descsz = 12; /* 12 descriptive bytes */
2113 i_note.type = NT_VERSION; /* Contains a version string */
2114
2115 p = frag_more (sizeof (i_note.namesz));
dbe2df79 2116 md_number_to_chars (p, i_note.namesz, 4);
252b5132
RH
2117
2118 p = frag_more (sizeof (i_note.descsz));
dbe2df79 2119 md_number_to_chars (p, i_note.descsz, 4);
252b5132
RH
2120
2121 p = frag_more (sizeof (i_note.type));
dbe2df79 2122 md_number_to_chars (p, i_note.type, 4);
252b5132
RH
2123
2124 p = frag_more (4);
fa306131 2125 strcpy (p, "SCO");
252b5132
RH
2126
2127 /* Note: this is the version number of the ELF we're representing */
2128 p = frag_more (4);
2129 md_number_to_chars (p, (SCO_MAJOR_VERSION << 16) | (SCO_MINOR_VERSION), 4);
2130
2131 /* Here, we pick a magic number for ourselves (yes, I "registered"
2132 it with SCO. The bottom bit shows that we are compat with the
2133 SCO ABI. */
2134 p = frag_more (4);
2135 md_number_to_chars (p, 0x4c520000 | 0x0001, 4);
2136
2137 /* If we knew (or cared) what the source language options were, we'd
2138 fill them in here. SCO has given us permission to ignore these
2139 and just set them to zero. */
2140 p = frag_more (4);
2141 md_number_to_chars (p, 0x0000, 4);
fa306131 2142
252b5132
RH
2143 frag_align (2, 0, 0);
2144
2145 /* We probably can't restore the current segment, for there likely
2146 isn't one yet... */
2147 if (seg && subseg)
2148 subseg_set (seg, subseg);
2149
2150}
2151
2152#endif /* SCO_ELF */
2153
5110c57e 2154static int
dbe2df79 2155elf_separate_stab_sections (void)
5110c57e
HPN
2156{
2157#ifdef NEED_ECOFF_DEBUG
2158 return (!ECOFF_DEBUGGING);
2159#else
2160 return 1;
2161#endif
2162}
2163
2164static void
dbe2df79 2165elf_init_stab_section (segT seg)
5110c57e
HPN
2166{
2167#ifdef NEED_ECOFF_DEBUG
2168 if (!ECOFF_DEBUGGING)
2169#endif
2170 obj_elf_init_stab_section (seg);
2171}
2172
252b5132
RH
2173const struct format_ops elf_format_ops =
2174{
2175 bfd_target_elf_flavour,
4c63da97
AM
2176 0, /* dfl_leading_underscore */
2177 1, /* emit_section_symbols */
5110c57e
HPN
2178 elf_begin,
2179 elf_file_symbol,
252b5132
RH
2180 elf_frob_symbol,
2181 elf_frob_file,
339681c0 2182 elf_frob_file_before_adjust,
a161fe53 2183 0, /* obj_frob_file_before_fix */
252b5132
RH
2184 elf_frob_file_after_relocs,
2185 elf_s_get_size, elf_s_set_size,
2186 elf_s_get_align, elf_s_set_align,
4c63da97 2187 elf_s_get_other,
5110c57e 2188 elf_s_set_other,
4c63da97 2189 0, /* s_get_desc */
5110c57e
HPN
2190 0, /* s_set_desc */
2191 0, /* s_get_type */
2192 0, /* s_set_type */
252b5132
RH
2193 elf_copy_symbol_attributes,
2194#ifdef NEED_ECOFF_DEBUG
2195 ecoff_generate_asm_lineno,
2196 ecoff_stab,
2197#else
4c63da97
AM
2198 0, /* generate_asm_lineno */
2199 0, /* process_stab */
252b5132 2200#endif
5110c57e
HPN
2201 elf_separate_stab_sections,
2202 elf_init_stab_section,
252b5132
RH
2203 elf_sec_sym_ok_for_reloc,
2204 elf_pop_insert,
2205#ifdef NEED_ECOFF_DEBUG
2206 elf_ecoff_set_ext,
2207#else
4c63da97 2208 0, /* ecoff_set_ext */
252b5132 2209#endif
4c63da97 2210 elf_obj_read_begin_hook,
5110c57e 2211 elf_obj_symbol_new_hook
252b5132 2212};
This page took 0.330438 seconds and 4 git commands to generate.