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