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