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