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