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