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