2001-07-10 John Healy <jhealy@redhat.com>
[binutils-gdb.git] / gas / config / obj-elf.c
1 /* ELF object file format
2 Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
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
20 02111-1307, USA. */
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
49 #ifdef TC_I370
50 #include "elf/i370.h"
51 #endif
52
53 static bfd_vma elf_s_get_size PARAMS ((symbolS *));
54 static void elf_s_set_size PARAMS ((symbolS *, bfd_vma));
55 static bfd_vma elf_s_get_align PARAMS ((symbolS *));
56 static void elf_s_set_align PARAMS ((symbolS *, bfd_vma));
57 static void elf_s_set_other PARAMS ((symbolS *, int));
58 static void elf_copy_symbol_attributes PARAMS ((symbolS *, symbolS *));
59 static int elf_sec_sym_ok_for_reloc PARAMS ((asection *));
60 static void adjust_stab_sections PARAMS ((bfd *, asection *, PTR));
61 static int elf_separate_stab_sections PARAMS ((void));
62 static void elf_init_stab_section PARAMS ((segT));
63
64 #ifdef NEED_ECOFF_DEBUG
65 static boolean elf_get_extr PARAMS ((asymbol *, EXTR *));
66 static void elf_set_index PARAMS ((asymbol *, bfd_size_type));
67 #endif
68
69 static void obj_elf_line PARAMS ((int));
70 void obj_elf_version PARAMS ((int));
71 static void obj_elf_size PARAMS ((int));
72 static void obj_elf_type PARAMS ((int));
73 static void obj_elf_ident PARAMS ((int));
74 static void obj_elf_weak PARAMS ((int));
75 static void obj_elf_local PARAMS ((int));
76 static void obj_elf_visibility PARAMS ((int));
77 static void obj_elf_symver PARAMS ((int));
78 static void obj_elf_subsection PARAMS ((int));
79 static void obj_elf_popsection PARAMS ((int));
80
81 static const pseudo_typeS elf_pseudo_table[] =
82 {
83 {"comm", obj_elf_common, 0},
84 {"common", obj_elf_common, 1},
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},
92 {"pushsection", obj_elf_section, 1},
93 {"popsection", obj_elf_popsection, 0},
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
99 /* These define symbol visibility. */
100 {"internal", obj_elf_visibility, STV_INTERNAL},
101 {"hidden", obj_elf_visibility, STV_HIDDEN},
102 {"protected", obj_elf_visibility, STV_PROTECTED},
103
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. */
114 {"vtable_inherit", (void (*) PARAMS ((int))) &obj_elf_vtable_inherit, 0},
115 {"vtable_entry", (void (*) PARAMS ((int))) &obj_elf_vtable_entry, 0},
116
117 /* These are used for dwarf. */
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. */
127 {NULL, NULL, 0},
128 };
129
130 static 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
172 {NULL, NULL, 0} /* end sentinel */
173 };
174
175 #undef NO_RELOC
176 #include "aout/aout64.h"
177
178 /* This is called when the assembler starts. */
179
180 void
181 elf_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
192 void
193 elf_pop_insert ()
194 {
195 pop_insert (elf_pseudo_table);
196 if (ECOFF_DEBUGGING)
197 pop_insert (ecoff_debug_pseudo_table);
198 }
199
200 static bfd_vma
201 elf_s_get_size (sym)
202 symbolS *sym;
203 {
204 return S_GET_SIZE (sym);
205 }
206
207 static void
208 elf_s_set_size (sym, sz)
209 symbolS *sym;
210 bfd_vma sz;
211 {
212 S_SET_SIZE (sym, sz);
213 }
214
215 static bfd_vma
216 elf_s_get_align (sym)
217 symbolS *sym;
218 {
219 return S_GET_ALIGN (sym);
220 }
221
222 static void
223 elf_s_set_align (sym, align)
224 symbolS *sym;
225 bfd_vma align;
226 {
227 S_SET_ALIGN (sym, align);
228 }
229
230 int
231 elf_s_get_other (sym)
232 symbolS *sym;
233 {
234 return elf_symbol (symbol_get_bfdsym (sym))->internal_elf_sym.st_other;
235 }
236
237 static void
238 elf_s_set_other (sym, other)
239 symbolS *sym;
240 int other;
241 {
242 S_SET_OTHER (sym, other);
243 }
244
245 static void
246 elf_copy_symbol_attributes (dest, src)
247 symbolS *dest, *src;
248 {
249 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
250 }
251
252 static int
253 elf_sec_sym_ok_for_reloc (sec)
254 asection *sec;
255 {
256 return obj_sec_sym_ok_for_reloc (sec);
257 }
258
259 void
260 elf_file_symbol (s)
261 const char *s;
262 {
263 symbolS *sym;
264
265 sym = symbol_new (s, absolute_section, (valueT) 0, (struct frag *) 0);
266 symbol_set_frag (sym, &zero_address_frag);
267 symbol_get_bfdsym (sym)->flags |= BSF_FILE;
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
283 void
284 obj_elf_common (is_common)
285 int is_common;
286 {
287 char *name;
288 char c;
289 char *p;
290 int temp, size;
291 symbolS *symbolP;
292 int have_align;
293
294 if (flag_mri && is_common)
295 {
296 s_mri_common (0);
297 return;
298 }
299
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 {
331 if (S_GET_VALUE (symbolP) != (valueT) size)
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 }
359 if (symbol_get_obj (symbolP)->local)
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)
387 symbol_get_frag (symbolP)->fr_symbol = 0;
388 symbol_set_frag (symbolP, frag_now);
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
426 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
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
446 static void
447 obj_elf_local (ignore)
448 int ignore ATTRIBUTE_UNUSED;
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);
462 symbol_get_obj (symbolP)->local = 1;
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
475 static void
476 obj_elf_weak (ignore)
477 int ignore ATTRIBUTE_UNUSED;
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);
491 symbol_get_obj (symbolP)->local = 1;
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
504 static void
505 obj_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;
520
521 SKIP_WHITESPACE ();
522
523 bfdsym = symbol_get_bfdsym (symbolP);
524 elfsym = elf_symbol_from (bfd_asymbol_bfd (bfdsym), bfdsym);
525
526 assert (elfsym);
527
528 elfsym->internal_elf_sym.st_other = visibility;
529
530 if (c == ',')
531 {
532 input_line_pointer ++;
533
534 SKIP_WHITESPACE ();
535
536 if (*input_line_pointer == '\n')
537 c = '\n';
538 }
539 }
540 while (c == ',');
541
542 demand_empty_rest_of_line ();
543 }
544
545 static segT previous_section;
546 static int previous_subsection;
547
548 struct section_stack
549 {
550 struct section_stack *next;
551 segT seg, prev_seg;
552 int subseg, prev_subseg;
553 };
554
555 static struct section_stack *section_stack;
556
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. */
577 struct special_section
578 {
579 const char *name;
580 int type;
581 int attributes;
582 };
583
584 static struct special_section const special_sections[] =
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
622 void
623 obj_elf_change_section (name, type, attr, entsize, push)
624 char *name;
625 int type, attr, entsize, push;
626 {
627 asection *old_sec;
628 segT sec;
629 flagword flags;
630 int i;
631
632 #ifdef md_flush_pending_output
633 md_flush_pending_output ();
634 #endif
635
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
651 old_sec = bfd_get_section_by_name (stdoutput, name);
652 sec = subseg_new (name, 0);
653
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)
663 {
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;
670 }
671 }
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)
694 | ((attr & SHF_MERGE) ? SEC_MERGE : 0)
695 | ((attr & SHF_STRINGS) ? SEC_STRINGS : 0));
696 #ifdef md_elf_section_flags
697 flags = md_elf_section_flags (flags, attr, type);
698 #endif
699
700 if (old_sec == NULL)
701 {
702 symbolS *secsym;
703
704 /* Prevent SEC_HAS_CONTENTS from being inadvertently set. */
705 if (type == SHT_NOBITS)
706 seg_info (sec)->bss = 1;
707
708 bfd_set_section_flags (stdoutput, sec, flags);
709 if (flags & SEC_MERGE)
710 sec->entsize = entsize;
711
712 /* Add a symbol for this section to the symbol table. */
713 secsym = symbol_find (name);
714 if (secsym != NULL)
715 symbol_set_bfdsym (secsym, sec->symbol);
716 else
717 symbol_table_insert (section_symbol (sec));
718 }
719 else if (attr != 0)
720 {
721 /* If section attributes are specified the second time we see a
722 particular section, then check that they are the same as we
723 saw the first time. */
724 if ((old_sec->flags ^ flags)
725 & (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
726 | SEC_EXCLUDE | SEC_SORT_ENTRIES | SEC_MERGE | SEC_STRINGS))
727 as_warn (_("Ignoring changed section attributes for %s"), name);
728 else if ((flags & SEC_MERGE) && old_sec->entsize != entsize)
729 as_warn (_("Ignoring changed section entity size for %s"), name);
730 }
731
732 #ifdef md_elf_section_change_hook
733 md_elf_section_change_hook ();
734 #endif
735 }
736
737 int
738 obj_elf_parse_section_letters (str, len)
739 char *str;
740 size_t len;
741 {
742 int attr = 0;
743
744 while (len > 0)
745 {
746 switch (*str)
747 {
748 case 'a':
749 attr |= SHF_ALLOC;
750 break;
751 case 'w':
752 attr |= SHF_WRITE;
753 break;
754 case 'x':
755 attr |= SHF_EXECINSTR;
756 break;
757 case 'm':
758 attr |= SHF_MERGE;
759 break;
760 case 's':
761 attr |= SHF_STRINGS;
762 break;
763 default:
764 {
765 char *bad_msg = _("Unrecognized .section attribute: want a,m,s,w,x");
766 #ifdef md_elf_section_letter
767 int md_attr = md_elf_section_letter (*str, &bad_msg);
768 if (md_attr >= 0)
769 attr |= md_attr;
770 else
771 #endif
772 {
773 as_warn ("%s", bad_msg);
774 attr = -1;
775 }
776 }
777 break;
778 }
779 str++, len--;
780 }
781
782 return attr;
783 }
784
785 int
786 obj_elf_section_word (str, len)
787 char *str;
788 size_t len;
789 {
790 if (len == 5 && strncmp (str, "write", 5) == 0)
791 return SHF_WRITE;
792 if (len == 5 && strncmp (str, "alloc", 5) == 0)
793 return SHF_ALLOC;
794 if (len == 9 && strncmp (str, "execinstr", 9) == 0)
795 return SHF_EXECINSTR;
796
797 #ifdef md_elf_section_word
798 {
799 int md_attr = md_elf_section_word (str, len);
800 if (md_attr >= 0)
801 return md_attr;
802 }
803 #endif
804
805 as_warn (_("Unrecognized section attribute"));
806 return 0;
807 }
808
809 int
810 obj_elf_section_type (str, len)
811 char *str;
812 size_t len;
813 {
814 if (len == 8 && strncmp (str, "progbits", 8) == 0)
815 return SHT_PROGBITS;
816 if (len == 6 && strncmp (str, "nobits", 6) == 0)
817 return SHT_NOBITS;
818
819 #ifdef md_elf_section_type
820 {
821 int md_type = md_elf_section_type (str, len);
822 if (md_type >= 0)
823 return md_type;
824 }
825 #endif
826
827 as_warn (_("Unrecognized section type"));
828 return 0;
829 }
830
831 void
832 obj_elf_section (push)
833 int push;
834 {
835 char *name, *beg, *end;
836 int type, attr, dummy;
837 int entsize;
838
839 #ifndef TC_I370
840 if (flag_mri)
841 {
842 char mri_type;
843
844 #ifdef md_flush_pending_output
845 md_flush_pending_output ();
846 #endif
847
848 previous_section = now_seg;
849 previous_subsection = now_subseg;
850
851 s_mri_sect (&mri_type);
852
853 #ifdef md_elf_section_change_hook
854 md_elf_section_change_hook ();
855 #endif
856
857 return;
858 }
859 #endif /* ! defined (TC_I370) */
860
861 /* Get name of section. */
862 SKIP_WHITESPACE ();
863 if (*input_line_pointer == '"')
864 {
865 name = demand_copy_C_string (&dummy);
866 if (name == NULL)
867 {
868 ignore_rest_of_line ();
869 return;
870 }
871 }
872 else
873 {
874 end = input_line_pointer;
875 while (0 == strchr ("\n\t,; ", *end))
876 end++;
877 if (end == input_line_pointer)
878 {
879 as_warn (_("Missing section name"));
880 ignore_rest_of_line ();
881 return;
882 }
883
884 name = xmalloc (end - input_line_pointer + 1);
885 memcpy (name, input_line_pointer, end - input_line_pointer);
886 name[end - input_line_pointer] = '\0';
887 input_line_pointer = end;
888 }
889 SKIP_WHITESPACE ();
890
891 type = SHT_NULL;
892 attr = 0;
893 entsize = 0;
894
895 if (*input_line_pointer == ',')
896 {
897 /* Skip the comma. */
898 ++input_line_pointer;
899 SKIP_WHITESPACE ();
900
901 if (*input_line_pointer == '"')
902 {
903 beg = demand_copy_C_string (&dummy);
904 if (beg == NULL)
905 {
906 ignore_rest_of_line ();
907 return;
908 }
909 attr |= obj_elf_parse_section_letters (beg, strlen (beg));
910
911 SKIP_WHITESPACE ();
912 if (*input_line_pointer == ',')
913 {
914 char c;
915 ++input_line_pointer;
916 SKIP_WHITESPACE ();
917 c = *input_line_pointer;
918 if (c == '"')
919 {
920 beg = demand_copy_C_string (&dummy);
921 if (beg == NULL)
922 {
923 ignore_rest_of_line ();
924 return;
925 }
926 type = obj_elf_section_type (beg, strlen (beg));
927 }
928 else if (c == '@' || c == '%')
929 {
930 beg = ++input_line_pointer;
931 c = get_symbol_end ();
932 *input_line_pointer = c;
933 type = obj_elf_section_type (beg, input_line_pointer - beg);
934 }
935 }
936
937 SKIP_WHITESPACE ();
938 if ((attr & SHF_MERGE) && *input_line_pointer == ',')
939 {
940 ++input_line_pointer;
941 SKIP_WHITESPACE ();
942 entsize = get_absolute_expression ();
943 if (entsize < 0)
944 {
945 as_warn (_("Bad .section directive - invalid merge entity size"));
946 attr &= ~SHF_MERGE;
947 entsize = 0;
948 }
949 }
950 }
951 else
952 {
953 do
954 {
955 char c;
956
957 SKIP_WHITESPACE ();
958 if (*input_line_pointer != '#')
959 {
960 as_warn (_("Bad .section directive - character following name is not '#'"));
961 ignore_rest_of_line ();
962 return;
963 }
964 beg = ++input_line_pointer;
965 c = get_symbol_end ();
966 *input_line_pointer = c;
967
968 attr |= obj_elf_section_word (beg, input_line_pointer - beg);
969
970 SKIP_WHITESPACE ();
971 }
972 while (*input_line_pointer++ == ',');
973 --input_line_pointer;
974 }
975 }
976
977 demand_empty_rest_of_line ();
978
979 if ((attr & SHF_MERGE) && entsize == 0)
980 {
981 as_warn (_("Entity size for SHF_MERGE not specified.\nSpecify entity size as 4th argument"));
982 attr &= SHF_MERGE;
983 }
984
985 #ifdef md_elf_section_change_data_hook
986 md_elf_section_change_data_hook (name, type, attr, entsize, push);
987 #endif
988
989 obj_elf_change_section (name, type, attr, entsize, push);
990 }
991
992 /* Change to the .data section. */
993
994 void
995 obj_elf_data (i)
996 int i;
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 s_data (i);
1005
1006 #ifdef md_elf_section_change_hook
1007 md_elf_section_change_hook ();
1008 #endif
1009 }
1010
1011 /* Change to the .text section. */
1012
1013 void
1014 obj_elf_text (i)
1015 int i;
1016 {
1017 #ifdef md_flush_pending_output
1018 md_flush_pending_output ();
1019 #endif
1020
1021 previous_section = now_seg;
1022 previous_subsection = now_subseg;
1023 s_text (i);
1024
1025 #ifdef md_elf_section_change_hook
1026 md_elf_section_change_hook ();
1027 #endif
1028 }
1029
1030 static void
1031 obj_elf_subsection (ignore)
1032 int ignore ATTRIBUTE_UNUSED;
1033 {
1034 register int temp;
1035
1036 #ifdef md_flush_pending_output
1037 md_flush_pending_output ();
1038 #endif
1039
1040 previous_section = now_seg;
1041 previous_subsection = now_subseg;
1042
1043 temp = get_absolute_expression ();
1044 subseg_set (now_seg, (subsegT) temp);
1045 demand_empty_rest_of_line ();
1046
1047 #ifdef md_elf_section_change_hook
1048 md_elf_section_change_hook ();
1049 #endif
1050 }
1051
1052 /* This can be called from the processor backends if they change
1053 sections. */
1054
1055 void
1056 obj_elf_section_change_hook ()
1057 {
1058 previous_section = now_seg;
1059 previous_subsection = now_subseg;
1060 }
1061
1062 void
1063 obj_elf_previous (ignore)
1064 int ignore ATTRIBUTE_UNUSED;
1065 {
1066 segT new_section;
1067 int new_subsection;
1068
1069 if (previous_section == 0)
1070 {
1071 as_bad (_(".previous without corresponding .section; ignored"));
1072 return;
1073 }
1074
1075 #ifdef md_flush_pending_output
1076 md_flush_pending_output ();
1077 #endif
1078
1079 new_section = previous_section;
1080 new_subsection = previous_subsection;
1081 previous_section = now_seg;
1082 previous_subsection = now_subseg;
1083 subseg_set (new_section, new_subsection);
1084
1085 #ifdef md_elf_section_change_hook
1086 md_elf_section_change_hook ();
1087 #endif
1088 }
1089
1090 static void
1091 obj_elf_popsection (xxx)
1092 int xxx ATTRIBUTE_UNUSED;
1093 {
1094 struct section_stack *top = section_stack;
1095
1096 if (top == NULL)
1097 {
1098 as_bad (_(".popsection without corresponding .pushsection; ignored"));
1099 return;
1100 }
1101
1102 #ifdef md_flush_pending_output
1103 md_flush_pending_output ();
1104 #endif
1105
1106 section_stack = top->next;
1107 previous_section = top->prev_seg;
1108 previous_subsection = top->prev_subseg;
1109 subseg_set (top->seg, top->subseg);
1110 free (top);
1111
1112 #ifdef md_elf_section_change_hook
1113 md_elf_section_change_hook ();
1114 #endif
1115 }
1116
1117 static void
1118 obj_elf_line (ignore)
1119 int ignore ATTRIBUTE_UNUSED;
1120 {
1121 /* Assume delimiter is part of expression. BSD4.2 as fails with
1122 delightful bug, so we are not being incompatible here. */
1123 new_logical_line ((char *) NULL, (int) (get_absolute_expression ()));
1124 demand_empty_rest_of_line ();
1125 }
1126
1127 /* This handles the .symver pseudo-op, which is used to specify a
1128 symbol version. The syntax is ``.symver NAME,SYMVERNAME''.
1129 SYMVERNAME may contain ELF_VER_CHR ('@') characters. This
1130 pseudo-op causes the assembler to emit a symbol named SYMVERNAME
1131 with the same value as the symbol NAME. */
1132
1133 static void
1134 obj_elf_symver (ignore)
1135 int ignore ATTRIBUTE_UNUSED;
1136 {
1137 char *name;
1138 char c;
1139 symbolS *sym;
1140
1141 name = input_line_pointer;
1142 c = get_symbol_end ();
1143
1144 sym = symbol_find_or_make (name);
1145
1146 *input_line_pointer = c;
1147
1148 SKIP_WHITESPACE ();
1149 if (*input_line_pointer != ',')
1150 {
1151 as_bad (_("expected comma after name in .symver"));
1152 ignore_rest_of_line ();
1153 return;
1154 }
1155
1156 ++input_line_pointer;
1157 name = input_line_pointer;
1158 while (1)
1159 {
1160 c = get_symbol_end ();
1161 if (c != ELF_VER_CHR)
1162 break;
1163 *input_line_pointer++ = c;
1164 }
1165
1166 if (symbol_get_obj (sym)->versioned_name == NULL)
1167 {
1168 symbol_get_obj (sym)->versioned_name = xstrdup (name);
1169
1170 *input_line_pointer = c;
1171
1172 if (strchr (symbol_get_obj (sym)->versioned_name,
1173 ELF_VER_CHR) == NULL)
1174 {
1175 as_bad (_("missing version name in `%s' for symbol `%s'"),
1176 symbol_get_obj (sym)->versioned_name,
1177 S_GET_NAME (sym));
1178 ignore_rest_of_line ();
1179 return;
1180 }
1181 }
1182 else
1183 {
1184 if (strcmp (symbol_get_obj (sym)->versioned_name, name))
1185 {
1186 as_bad (_("multiple versions [`%s'|`%s'] for symbol `%s'"),
1187 name, symbol_get_obj (sym)->versioned_name,
1188 S_GET_NAME (sym));
1189 ignore_rest_of_line ();
1190 return;
1191 }
1192
1193 *input_line_pointer = c;
1194 }
1195
1196 demand_empty_rest_of_line ();
1197 }
1198
1199 /* This handles the .vtable_inherit pseudo-op, which is used to indicate
1200 to the linker the hierarchy in which a particular table resides. The
1201 syntax is ".vtable_inherit CHILDNAME, PARENTNAME". */
1202
1203 struct fix *
1204 obj_elf_vtable_inherit (ignore)
1205 int ignore ATTRIBUTE_UNUSED;
1206 {
1207 char *cname, *pname;
1208 symbolS *csym, *psym;
1209 char c, bad = 0;
1210
1211 if (*input_line_pointer == '#')
1212 ++input_line_pointer;
1213
1214 cname = input_line_pointer;
1215 c = get_symbol_end ();
1216 csym = symbol_find (cname);
1217
1218 /* GCFIXME: should check that we don't have two .vtable_inherits for
1219 the same child symbol. Also, we can currently only do this if the
1220 child symbol is already exists and is placed in a fragment. */
1221
1222 if (csym == NULL || symbol_get_frag (csym) == NULL)
1223 {
1224 as_bad ("expected `%s' to have already been set for .vtable_inherit",
1225 cname);
1226 bad = 1;
1227 }
1228
1229 *input_line_pointer = c;
1230
1231 SKIP_WHITESPACE ();
1232 if (*input_line_pointer != ',')
1233 {
1234 as_bad ("expected comma after name in .vtable_inherit");
1235 ignore_rest_of_line ();
1236 return NULL;
1237 }
1238
1239 ++input_line_pointer;
1240 SKIP_WHITESPACE ();
1241
1242 if (*input_line_pointer == '#')
1243 ++input_line_pointer;
1244
1245 if (input_line_pointer[0] == '0'
1246 && (input_line_pointer[1] == '\0'
1247 || isspace ((unsigned char) input_line_pointer[1])))
1248 {
1249 psym = section_symbol (absolute_section);
1250 ++input_line_pointer;
1251 }
1252 else
1253 {
1254 pname = input_line_pointer;
1255 c = get_symbol_end ();
1256 psym = symbol_find_or_make (pname);
1257 *input_line_pointer = c;
1258 }
1259
1260 demand_empty_rest_of_line ();
1261
1262 if (bad)
1263 return NULL;
1264
1265 assert (symbol_get_value_expression (csym)->X_op == O_constant);
1266 return fix_new (symbol_get_frag (csym),
1267 symbol_get_value_expression (csym)->X_add_number,
1268 0, psym, 0, 0, BFD_RELOC_VTABLE_INHERIT);
1269 }
1270
1271 /* This handles the .vtable_entry pseudo-op, which is used to indicate
1272 to the linker that a vtable slot was used. The syntax is
1273 ".vtable_entry tablename, offset". */
1274
1275 struct fix *
1276 obj_elf_vtable_entry (ignore)
1277 int ignore ATTRIBUTE_UNUSED;
1278 {
1279 char *name;
1280 symbolS *sym;
1281 offsetT offset;
1282 char c;
1283
1284 if (*input_line_pointer == '#')
1285 ++input_line_pointer;
1286
1287 name = input_line_pointer;
1288 c = get_symbol_end ();
1289 sym = symbol_find_or_make (name);
1290 *input_line_pointer = c;
1291
1292 SKIP_WHITESPACE ();
1293 if (*input_line_pointer != ',')
1294 {
1295 as_bad ("expected comma after name in .vtable_entry");
1296 ignore_rest_of_line ();
1297 return NULL;
1298 }
1299
1300 ++input_line_pointer;
1301 if (*input_line_pointer == '#')
1302 ++input_line_pointer;
1303
1304 offset = get_absolute_expression ();
1305
1306 demand_empty_rest_of_line ();
1307
1308 return fix_new (frag_now, frag_now_fix (), 0, sym, offset, 0,
1309 BFD_RELOC_VTABLE_ENTRY);
1310 }
1311
1312 void
1313 elf_obj_read_begin_hook ()
1314 {
1315 #ifdef NEED_ECOFF_DEBUG
1316 if (ECOFF_DEBUGGING)
1317 ecoff_read_begin_hook ();
1318 #endif
1319 }
1320
1321 void
1322 elf_obj_symbol_new_hook (symbolP)
1323 symbolS *symbolP;
1324 {
1325 struct elf_obj_sy *sy_obj;
1326
1327 sy_obj = symbol_get_obj (symbolP);
1328 sy_obj->size = NULL;
1329 sy_obj->versioned_name = NULL;
1330
1331 #ifdef NEED_ECOFF_DEBUG
1332 if (ECOFF_DEBUGGING)
1333 ecoff_symbol_new_hook (symbolP);
1334 #endif
1335 }
1336
1337 void
1338 obj_elf_version (ignore)
1339 int ignore ATTRIBUTE_UNUSED;
1340 {
1341 char *name;
1342 unsigned int c;
1343 char ch;
1344 char *p;
1345 asection *seg = now_seg;
1346 subsegT subseg = now_subseg;
1347 Elf_Internal_Note i_note;
1348 Elf_External_Note e_note;
1349 asection *note_secp = (asection *) NULL;
1350 int i, len;
1351
1352 SKIP_WHITESPACE ();
1353 if (*input_line_pointer == '\"')
1354 {
1355 ++input_line_pointer; /* -> 1st char of string. */
1356 name = input_line_pointer;
1357
1358 while (is_a_char (c = next_char_of_string ()))
1359 ;
1360 c = *input_line_pointer;
1361 *input_line_pointer = '\0';
1362 *(input_line_pointer - 1) = '\0';
1363 *input_line_pointer = c;
1364
1365 /* create the .note section */
1366
1367 note_secp = subseg_new (".note", 0);
1368 bfd_set_section_flags (stdoutput,
1369 note_secp,
1370 SEC_HAS_CONTENTS | SEC_READONLY);
1371
1372 /* process the version string */
1373
1374 len = strlen (name);
1375
1376 i_note.namesz = ((len + 1) + 3) & ~3; /* round this to word boundary */
1377 i_note.descsz = 0; /* no description */
1378 i_note.type = NT_VERSION;
1379 p = frag_more (sizeof (e_note.namesz));
1380 md_number_to_chars (p, (valueT) i_note.namesz, 4);
1381 p = frag_more (sizeof (e_note.descsz));
1382 md_number_to_chars (p, (valueT) i_note.descsz, 4);
1383 p = frag_more (sizeof (e_note.type));
1384 md_number_to_chars (p, (valueT) i_note.type, 4);
1385
1386 for (i = 0; i < len; i++)
1387 {
1388 ch = *(name + i);
1389 {
1390 FRAG_APPEND_1_CHAR (ch);
1391 }
1392 }
1393 frag_align (2, 0, 0);
1394
1395 subseg_set (seg, subseg);
1396 }
1397 else
1398 {
1399 as_bad (_("Expected quoted string"));
1400 }
1401 demand_empty_rest_of_line ();
1402 }
1403
1404 static void
1405 obj_elf_size (ignore)
1406 int ignore ATTRIBUTE_UNUSED;
1407 {
1408 char *name = input_line_pointer;
1409 char c = get_symbol_end ();
1410 char *p;
1411 expressionS exp;
1412 symbolS *sym;
1413
1414 p = input_line_pointer;
1415 *p = c;
1416 SKIP_WHITESPACE ();
1417 if (*input_line_pointer != ',')
1418 {
1419 *p = 0;
1420 as_bad (_("expected comma after name `%s' in .size directive"), name);
1421 *p = c;
1422 ignore_rest_of_line ();
1423 return;
1424 }
1425 input_line_pointer++;
1426 expression (&exp);
1427 if (exp.X_op == O_absent)
1428 {
1429 as_bad (_("missing expression in .size directive"));
1430 exp.X_op = O_constant;
1431 exp.X_add_number = 0;
1432 }
1433 *p = 0;
1434 sym = symbol_find_or_make (name);
1435 *p = c;
1436 if (exp.X_op == O_constant)
1437 S_SET_SIZE (sym, exp.X_add_number);
1438 else
1439 {
1440 symbol_get_obj (sym)->size =
1441 (expressionS *) xmalloc (sizeof (expressionS));
1442 *symbol_get_obj (sym)->size = exp;
1443 }
1444 demand_empty_rest_of_line ();
1445 }
1446
1447 /* Handle the ELF .type pseudo-op. This sets the type of a symbol.
1448 There are five syntaxes:
1449
1450 The first (used on Solaris) is
1451 .type SYM,#function
1452 The second (used on UnixWare) is
1453 .type SYM,@function
1454 The third (reportedly to be used on Irix 6.0) is
1455 .type SYM STT_FUNC
1456 The fourth (used on NetBSD/Arm and Linux/ARM) is
1457 .type SYM,%function
1458 The fifth (used on SVR4/860) is
1459 .type SYM,"function"
1460 */
1461
1462 static void
1463 obj_elf_type (ignore)
1464 int ignore ATTRIBUTE_UNUSED;
1465 {
1466 char *name;
1467 char c;
1468 int type;
1469 const char *typename;
1470 symbolS *sym;
1471 elf_symbol_type *elfsym;
1472
1473 name = input_line_pointer;
1474 c = get_symbol_end ();
1475 sym = symbol_find_or_make (name);
1476 elfsym = (elf_symbol_type *) symbol_get_bfdsym (sym);
1477 *input_line_pointer = c;
1478
1479 SKIP_WHITESPACE ();
1480 if (*input_line_pointer == ',')
1481 ++input_line_pointer;
1482
1483 SKIP_WHITESPACE ();
1484 if ( *input_line_pointer == '#'
1485 || *input_line_pointer == '@'
1486 || *input_line_pointer == '"'
1487 || *input_line_pointer == '%')
1488 ++input_line_pointer;
1489
1490 typename = input_line_pointer;
1491 c = get_symbol_end ();
1492
1493 type = 0;
1494 if (strcmp (typename, "function") == 0
1495 || strcmp (typename, "STT_FUNC") == 0)
1496 type = BSF_FUNCTION;
1497 else if (strcmp (typename, "object") == 0
1498 || strcmp (typename, "STT_OBJECT") == 0)
1499 type = BSF_OBJECT;
1500 #ifdef md_elf_symbol_type
1501 else if ((type = md_elf_symbol_type (typename, sym, elfsym)) != -1)
1502 ;
1503 #endif
1504 else
1505 as_bad (_("ignoring unrecognized symbol type \"%s\""), typename);
1506
1507 *input_line_pointer = c;
1508
1509 if (*input_line_pointer == '"')
1510 ++input_line_pointer;
1511
1512 elfsym->symbol.flags |= type;
1513
1514 demand_empty_rest_of_line ();
1515 }
1516
1517 static void
1518 obj_elf_ident (ignore)
1519 int ignore ATTRIBUTE_UNUSED;
1520 {
1521 static segT comment_section;
1522 segT old_section = now_seg;
1523 int old_subsection = now_subseg;
1524
1525 #ifdef md_flush_pending_output
1526 md_flush_pending_output ();
1527 #endif
1528
1529 if (!comment_section)
1530 {
1531 char *p;
1532 comment_section = subseg_new (".comment", 0);
1533 bfd_set_section_flags (stdoutput, comment_section,
1534 SEC_READONLY | SEC_HAS_CONTENTS);
1535 p = frag_more (1);
1536 *p = 0;
1537 }
1538 else
1539 subseg_set (comment_section, 0);
1540 stringer (1);
1541 subseg_set (old_section, old_subsection);
1542 }
1543
1544 #ifdef INIT_STAB_SECTION
1545
1546 /* The first entry in a .stabs section is special. */
1547
1548 void
1549 obj_elf_init_stab_section (seg)
1550 segT seg;
1551 {
1552 char *file;
1553 char *p;
1554 char *stabstr_name;
1555 unsigned int stroff;
1556
1557 /* Force the section to align to a longword boundary. Without this,
1558 UnixWare ar crashes. */
1559 bfd_set_section_alignment (stdoutput, seg, 2);
1560
1561 /* Make space for this first symbol. */
1562 p = frag_more (12);
1563 /* Zero it out. */
1564 memset (p, 0, 12);
1565 as_where (&file, (unsigned int *) NULL);
1566 stabstr_name = (char *) alloca (strlen (segment_name (seg)) + 4);
1567 strcpy (stabstr_name, segment_name (seg));
1568 strcat (stabstr_name, "str");
1569 stroff = get_stab_string_offset (file, stabstr_name);
1570 know (stroff == 1);
1571 md_number_to_chars (p, stroff, 4);
1572 seg_info (seg)->stabu.p = p;
1573 }
1574
1575 #endif
1576
1577 /* Fill in the counts in the first entry in a .stabs section. */
1578
1579 static void
1580 adjust_stab_sections (abfd, sec, xxx)
1581 bfd *abfd;
1582 asection *sec;
1583 PTR xxx ATTRIBUTE_UNUSED;
1584 {
1585 char *name;
1586 asection *strsec;
1587 char *p;
1588 int strsz, nsyms;
1589
1590 if (strncmp (".stab", sec->name, 5))
1591 return;
1592 if (!strcmp ("str", sec->name + strlen (sec->name) - 3))
1593 return;
1594
1595 name = (char *) alloca (strlen (sec->name) + 4);
1596 strcpy (name, sec->name);
1597 strcat (name, "str");
1598 strsec = bfd_get_section_by_name (abfd, name);
1599 if (strsec)
1600 strsz = bfd_section_size (abfd, strsec);
1601 else
1602 strsz = 0;
1603 nsyms = bfd_section_size (abfd, sec) / 12 - 1;
1604
1605 p = seg_info (sec)->stabu.p;
1606 assert (p != 0);
1607
1608 bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6);
1609 bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8);
1610 }
1611
1612 #ifdef NEED_ECOFF_DEBUG
1613
1614 /* This function is called by the ECOFF code. It is supposed to
1615 record the external symbol information so that the backend can
1616 write it out correctly. The ELF backend doesn't actually handle
1617 this at the moment, so we do it ourselves. We save the information
1618 in the symbol. */
1619
1620 void
1621 elf_ecoff_set_ext (sym, ext)
1622 symbolS *sym;
1623 struct ecoff_extr *ext;
1624 {
1625 symbol_get_bfdsym (sym)->udata.p = (PTR) ext;
1626 }
1627
1628 /* This function is called by bfd_ecoff_debug_externals. It is
1629 supposed to *EXT to the external symbol information, and return
1630 whether the symbol should be used at all. */
1631
1632 static boolean
1633 elf_get_extr (sym, ext)
1634 asymbol *sym;
1635 EXTR *ext;
1636 {
1637 if (sym->udata.p == NULL)
1638 return false;
1639 *ext = *(EXTR *) sym->udata.p;
1640 return true;
1641 }
1642
1643 /* This function is called by bfd_ecoff_debug_externals. It has
1644 nothing to do for ELF. */
1645
1646 /*ARGSUSED*/
1647 static void
1648 elf_set_index (sym, indx)
1649 asymbol *sym ATTRIBUTE_UNUSED;
1650 bfd_size_type indx ATTRIBUTE_UNUSED;
1651 {
1652 }
1653
1654 #endif /* NEED_ECOFF_DEBUG */
1655
1656 void
1657 elf_frob_symbol (symp, puntp)
1658 symbolS *symp;
1659 int *puntp;
1660 {
1661 struct elf_obj_sy *sy_obj;
1662
1663 #ifdef NEED_ECOFF_DEBUG
1664 if (ECOFF_DEBUGGING)
1665 ecoff_frob_symbol (symp);
1666 #endif
1667
1668 sy_obj = symbol_get_obj (symp);
1669
1670 if (sy_obj->size != NULL)
1671 {
1672 switch (sy_obj->size->X_op)
1673 {
1674 case O_subtract:
1675 S_SET_SIZE (symp,
1676 (S_GET_VALUE (sy_obj->size->X_add_symbol)
1677 + sy_obj->size->X_add_number
1678 - S_GET_VALUE (sy_obj->size->X_op_symbol)));
1679 break;
1680 case O_constant:
1681 S_SET_SIZE (symp,
1682 (S_GET_VALUE (sy_obj->size->X_add_symbol)
1683 + sy_obj->size->X_add_number));
1684 break;
1685 default:
1686 as_bad (_(".size expression too complicated to fix up"));
1687 break;
1688 }
1689 free (sy_obj->size);
1690 sy_obj->size = NULL;
1691 }
1692
1693 if (sy_obj->versioned_name != NULL)
1694 {
1695 char *p;
1696
1697 p = strchr (sy_obj->versioned_name, ELF_VER_CHR);
1698 know (p != NULL);
1699
1700 /* This symbol was given a new name with the .symver directive.
1701
1702 If this is an external reference, just rename the symbol to
1703 include the version string. This will make the relocs be
1704 against the correct versioned symbol.
1705
1706 If this is a definition, add an alias. FIXME: Using an alias
1707 will permit the debugging information to refer to the right
1708 symbol. However, it's not clear whether it is the best
1709 approach. */
1710
1711 if (! S_IS_DEFINED (symp))
1712 {
1713 /* Verify that the name isn't using the @@ syntax--this is
1714 reserved for definitions of the default version to link
1715 against. */
1716 if (p[1] == ELF_VER_CHR)
1717 {
1718 as_bad (_("invalid attempt to declare external version name as default in symbol `%s'"),
1719 sy_obj->versioned_name);
1720 *puntp = true;
1721 }
1722 S_SET_NAME (symp, sy_obj->versioned_name);
1723 }
1724 else
1725 {
1726 if (p [1] == ELF_VER_CHR && p [2] == ELF_VER_CHR)
1727 {
1728 size_t l;
1729
1730 /* The @@@ syntax is a special case. It renames the
1731 symbol name to versioned_name with one `@' removed. */
1732 l = strlen (&p[3]) + 1;
1733 memmove (&p [2], &p[3], l);
1734 S_SET_NAME (symp, sy_obj->versioned_name);
1735 }
1736 else
1737 {
1738 symbolS *symp2;
1739
1740 /* FIXME: Creating a new symbol here is risky. We're
1741 in the final loop over the symbol table. We can
1742 get away with it only because the symbol goes to
1743 the end of the list, where the loop will still see
1744 it. It would probably be better to do this in
1745 obj_frob_file_before_adjust. */
1746
1747 symp2 = symbol_find_or_make (sy_obj->versioned_name);
1748
1749 /* Now we act as though we saw symp2 = sym. */
1750
1751 S_SET_SEGMENT (symp2, S_GET_SEGMENT (symp));
1752
1753 /* Subtracting out the frag address here is a hack
1754 because we are in the middle of the final loop. */
1755 S_SET_VALUE (symp2,
1756 (S_GET_VALUE (symp)
1757 - symbol_get_frag (symp)->fr_address));
1758
1759 symbol_set_frag (symp2, symbol_get_frag (symp));
1760
1761 /* This will copy over the size information. */
1762 copy_symbol_attributes (symp2, symp);
1763
1764 if (S_IS_WEAK (symp))
1765 S_SET_WEAK (symp2);
1766
1767 if (S_IS_EXTERNAL (symp))
1768 S_SET_EXTERNAL (symp2);
1769 }
1770 }
1771 }
1772
1773 /* Double check weak symbols. */
1774 if (S_IS_WEAK (symp))
1775 {
1776 if (S_IS_COMMON (symp))
1777 as_bad (_("Symbol `%s' can not be both weak and common"),
1778 S_GET_NAME (symp));
1779 }
1780
1781 #ifdef TC_MIPS
1782 /* The Irix 5 and 6 assemblers set the type of any common symbol and
1783 any undefined non-function symbol to STT_OBJECT. We try to be
1784 compatible, since newer Irix 5 and 6 linkers care. However, we
1785 only set undefined symbols to be STT_OBJECT if we are on Irix,
1786 because that is the only time gcc will generate the necessary
1787 .global directives to mark functions. */
1788
1789 if (S_IS_COMMON (symp))
1790 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1791
1792 if (strstr (TARGET_OS, "irix") != NULL
1793 && ! S_IS_DEFINED (symp)
1794 && (symbol_get_bfdsym (symp)->flags & BSF_FUNCTION) == 0)
1795 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1796 #endif
1797
1798 #if 0 /* TC_PPC */
1799 /* If TC_PPC is defined, we used to force the type of a symbol to be
1800 BSF_OBJECT if it was otherwise unset. This was required by some
1801 version of VxWorks. Thomas de Lellis <tdel@windriver.com> says
1802 that this is no longer needed, so it is now commented out. */
1803 if ((symbol_get_bfdsym (symp)->flags
1804 & (BSF_FUNCTION | BSF_FILE | BSF_SECTION_SYM)) == 0
1805 && S_IS_DEFINED (symp))
1806 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1807 #endif
1808 }
1809
1810 void
1811 elf_frob_file ()
1812 {
1813 bfd_map_over_sections (stdoutput, adjust_stab_sections, (PTR) 0);
1814
1815 #ifdef elf_tc_final_processing
1816 elf_tc_final_processing ();
1817 #endif
1818 }
1819
1820 /* It removes any unneeded versioned symbols from the symbol table. */
1821
1822 void
1823 elf_frob_file_before_adjust ()
1824 {
1825 if (symbol_rootP)
1826 {
1827 symbolS *symp;
1828
1829 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1830 if (symbol_get_obj (symp)->versioned_name)
1831 {
1832 if (!S_IS_DEFINED (symp))
1833 {
1834 char *p;
1835
1836 /* The @@@ syntax is a special case. If the symbol is
1837 not defined, 2 `@'s will be removed from the
1838 versioned_name. */
1839
1840 p = strchr (symbol_get_obj (symp)->versioned_name,
1841 ELF_VER_CHR);
1842 know (p != NULL);
1843 if (p [1] == ELF_VER_CHR && p [2] == ELF_VER_CHR)
1844 {
1845 size_t l = strlen (&p[3]) + 1;
1846 memmove (&p [1], &p[3], l);
1847 }
1848 if (symbol_used_p (symp) == 0
1849 && symbol_used_in_reloc_p (symp) == 0)
1850 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1851 }
1852 }
1853 }
1854 }
1855
1856 /* It is required that we let write_relocs have the opportunity to
1857 optimize away fixups before output has begun, since it is possible
1858 to eliminate all fixups for a section and thus we never should
1859 have generated the relocation section. */
1860
1861 void
1862 elf_frob_file_after_relocs ()
1863 {
1864 #ifdef NEED_ECOFF_DEBUG
1865 if (ECOFF_DEBUGGING)
1866 /* Generate the ECOFF debugging information. */
1867 {
1868 const struct ecoff_debug_swap *debug_swap;
1869 struct ecoff_debug_info debug;
1870 char *buf;
1871 asection *sec;
1872
1873 debug_swap
1874 = get_elf_backend_data (stdoutput)->elf_backend_ecoff_debug_swap;
1875 know (debug_swap != (const struct ecoff_debug_swap *) NULL);
1876 ecoff_build_debug (&debug.symbolic_header, &buf, debug_swap);
1877
1878 /* Set up the pointers in debug. */
1879 #define SET(ptr, offset, type) \
1880 debug.ptr = (type) (buf + debug.symbolic_header.offset)
1881
1882 SET (line, cbLineOffset, unsigned char *);
1883 SET (external_dnr, cbDnOffset, PTR);
1884 SET (external_pdr, cbPdOffset, PTR);
1885 SET (external_sym, cbSymOffset, PTR);
1886 SET (external_opt, cbOptOffset, PTR);
1887 SET (external_aux, cbAuxOffset, union aux_ext *);
1888 SET (ss, cbSsOffset, char *);
1889 SET (external_fdr, cbFdOffset, PTR);
1890 SET (external_rfd, cbRfdOffset, PTR);
1891 /* ssext and external_ext are set up just below. */
1892
1893 #undef SET
1894
1895 /* Set up the external symbols. */
1896 debug.ssext = debug.ssext_end = NULL;
1897 debug.external_ext = debug.external_ext_end = NULL;
1898 if (! bfd_ecoff_debug_externals (stdoutput, &debug, debug_swap, true,
1899 elf_get_extr, elf_set_index))
1900 as_fatal (_("Failed to set up debugging information: %s"),
1901 bfd_errmsg (bfd_get_error ()));
1902
1903 sec = bfd_get_section_by_name (stdoutput, ".mdebug");
1904 assert (sec != NULL);
1905
1906 know (stdoutput->output_has_begun == false);
1907
1908 /* We set the size of the section, call bfd_set_section_contents
1909 to force the ELF backend to allocate a file position, and then
1910 write out the data. FIXME: Is this really the best way to do
1911 this? */
1912 sec->_raw_size = bfd_ecoff_debug_size (stdoutput, &debug, debug_swap);
1913
1914 /* Pass BUF to bfd_set_section_contents because this will
1915 eventually become a call to fwrite, and ISO C prohibits
1916 passing a NULL pointer to a stdio function even if the
1917 pointer will not be used. */
1918 if (! bfd_set_section_contents (stdoutput, sec, (PTR) buf,
1919 (file_ptr) 0, (bfd_size_type) 0))
1920 as_fatal (_("Can't start writing .mdebug section: %s"),
1921 bfd_errmsg (bfd_get_error ()));
1922
1923 know (stdoutput->output_has_begun == true);
1924 know (sec->filepos != 0);
1925
1926 if (! bfd_ecoff_write_debug (stdoutput, &debug, debug_swap,
1927 sec->filepos))
1928 as_fatal (_("Could not write .mdebug section: %s"),
1929 bfd_errmsg (bfd_get_error ()));
1930 }
1931 #endif /* NEED_ECOFF_DEBUG */
1932 }
1933
1934 #ifdef SCO_ELF
1935
1936 /* Heavily plagarized from obj_elf_version. The idea is to emit the
1937 SCO specific identifier in the .notes section to satisfy the SCO
1938 linker.
1939
1940 This looks more complicated than it really is. As opposed to the
1941 "obvious" solution, this should handle the cross dev cases
1942 correctly. (i.e, hosting on a 64 bit big endian processor, but
1943 generating SCO Elf code) Efficiency isn't a concern, as there
1944 should be exactly one of these sections per object module.
1945
1946 SCO OpenServer 5 identifies it's ELF modules with a standard ELF
1947 .note section.
1948
1949 int_32 namesz = 4 ; Name size
1950 int_32 descsz = 12 ; Descriptive information
1951 int_32 type = 1 ;
1952 char name[4] = "SCO" ; Originator name ALWAYS SCO + NULL
1953 int_32 version = (major ver # << 16) | version of tools ;
1954 int_32 source = (tool_id << 16 ) | 1 ;
1955 int_32 info = 0 ; These are set by the SCO tools, but we
1956 don't know enough about the source
1957 environment to set them. SCO ld currently
1958 ignores them, and recommends we set them
1959 to zero. */
1960
1961 #define SCO_MAJOR_VERSION 0x1
1962 #define SCO_MINOR_VERSION 0x1
1963
1964 void
1965 sco_id ()
1966 {
1967
1968 char *name;
1969 unsigned int c;
1970 char ch;
1971 char *p;
1972 asection *seg = now_seg;
1973 subsegT subseg = now_subseg;
1974 Elf_Internal_Note i_note;
1975 Elf_External_Note e_note;
1976 asection *note_secp = (asection *) NULL;
1977 int i, len;
1978
1979 /* create the .note section */
1980
1981 note_secp = subseg_new (".note", 0);
1982 bfd_set_section_flags (stdoutput,
1983 note_secp,
1984 SEC_HAS_CONTENTS | SEC_READONLY);
1985
1986 /* process the version string */
1987
1988 i_note.namesz = 4;
1989 i_note.descsz = 12; /* 12 descriptive bytes */
1990 i_note.type = NT_VERSION; /* Contains a version string */
1991
1992 p = frag_more (sizeof (i_note.namesz));
1993 md_number_to_chars (p, (valueT) i_note.namesz, 4);
1994
1995 p = frag_more (sizeof (i_note.descsz));
1996 md_number_to_chars (p, (valueT) i_note.descsz, 4);
1997
1998 p = frag_more (sizeof (i_note.type));
1999 md_number_to_chars (p, (valueT) i_note.type, 4);
2000
2001 p = frag_more (4);
2002 strcpy (p, "SCO");
2003
2004 /* Note: this is the version number of the ELF we're representing */
2005 p = frag_more (4);
2006 md_number_to_chars (p, (SCO_MAJOR_VERSION << 16) | (SCO_MINOR_VERSION), 4);
2007
2008 /* Here, we pick a magic number for ourselves (yes, I "registered"
2009 it with SCO. The bottom bit shows that we are compat with the
2010 SCO ABI. */
2011 p = frag_more (4);
2012 md_number_to_chars (p, 0x4c520000 | 0x0001, 4);
2013
2014 /* If we knew (or cared) what the source language options were, we'd
2015 fill them in here. SCO has given us permission to ignore these
2016 and just set them to zero. */
2017 p = frag_more (4);
2018 md_number_to_chars (p, 0x0000, 4);
2019
2020 frag_align (2, 0, 0);
2021
2022 /* We probably can't restore the current segment, for there likely
2023 isn't one yet... */
2024 if (seg && subseg)
2025 subseg_set (seg, subseg);
2026
2027 }
2028
2029 #endif /* SCO_ELF */
2030
2031 static int
2032 elf_separate_stab_sections ()
2033 {
2034 #ifdef NEED_ECOFF_DEBUG
2035 return (!ECOFF_DEBUGGING);
2036 #else
2037 return 1;
2038 #endif
2039 }
2040
2041 static void
2042 elf_init_stab_section (seg)
2043 segT seg;
2044 {
2045 #ifdef NEED_ECOFF_DEBUG
2046 if (!ECOFF_DEBUGGING)
2047 #endif
2048 obj_elf_init_stab_section (seg);
2049 }
2050
2051 const struct format_ops elf_format_ops =
2052 {
2053 bfd_target_elf_flavour,
2054 0, /* dfl_leading_underscore */
2055 1, /* emit_section_symbols */
2056 elf_begin,
2057 elf_file_symbol,
2058 elf_frob_symbol,
2059 elf_frob_file,
2060 elf_frob_file_before_adjust,
2061 elf_frob_file_after_relocs,
2062 elf_s_get_size, elf_s_set_size,
2063 elf_s_get_align, elf_s_set_align,
2064 elf_s_get_other,
2065 elf_s_set_other,
2066 0, /* s_get_desc */
2067 0, /* s_set_desc */
2068 0, /* s_get_type */
2069 0, /* s_set_type */
2070 elf_copy_symbol_attributes,
2071 #ifdef NEED_ECOFF_DEBUG
2072 ecoff_generate_asm_lineno,
2073 ecoff_stab,
2074 #else
2075 0, /* generate_asm_lineno */
2076 0, /* process_stab */
2077 #endif
2078 elf_separate_stab_sections,
2079 elf_init_stab_section,
2080 elf_sec_sym_ok_for_reloc,
2081 elf_pop_insert,
2082 #ifdef NEED_ECOFF_DEBUG
2083 elf_ecoff_set_ext,
2084 #else
2085 0, /* ecoff_set_ext */
2086 #endif
2087 elf_obj_read_begin_hook,
2088 elf_obj_symbol_new_hook
2089 };