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