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