* Makefile.in (@COMMON_MAKEFILE_FRAG@): Use
[deliverable/binutils-gdb.git] / gas / config / obj-elf.c
1 /* ELF object file format
2 Copyright (C) 1992, 93, 94, 95, 96, 1997 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_MIPS
37 #include "elf/mips.h"
38 #endif
39
40 #ifdef TC_PPC
41 #include "elf/ppc.h"
42 #endif
43
44 #ifdef NEED_ECOFF_DEBUG
45 static boolean elf_get_extr PARAMS ((asymbol *, EXTR *));
46 static void elf_set_index PARAMS ((asymbol *, bfd_size_type));
47 #endif
48
49 static void obj_elf_line PARAMS ((int));
50 void obj_elf_version PARAMS ((int));
51 static void obj_elf_size PARAMS ((int));
52 static void obj_elf_type PARAMS ((int));
53 static void obj_elf_ident PARAMS ((int));
54 static void obj_elf_weak PARAMS ((int));
55 static void obj_elf_local PARAMS ((int));
56 static void obj_elf_common PARAMS ((int));
57 static void obj_elf_data PARAMS ((int));
58 static void obj_elf_text PARAMS ((int));
59
60 static const pseudo_typeS elf_pseudo_table[] =
61 {
62 {"comm", obj_elf_common, 0},
63 {"ident", obj_elf_ident, 0},
64 {"local", obj_elf_local, 0},
65 {"previous", obj_elf_previous, 0},
66 {"section", obj_elf_section, 0},
67 {"section.s", obj_elf_section, 0},
68 {"sect", obj_elf_section, 0},
69 {"sect.s", obj_elf_section, 0},
70 {"size", obj_elf_size, 0},
71 {"type", obj_elf_type, 0},
72 {"version", obj_elf_version, 0},
73 {"weak", obj_elf_weak, 0},
74
75 /* These are used for stabs-in-elf configurations. */
76 {"line", obj_elf_line, 0},
77
78 /* These are used for dwarf. */
79 {"2byte", cons, 2},
80 {"4byte", cons, 4},
81 {"8byte", cons, 8},
82
83 /* We need to trap the section changing calls to handle .previous. */
84 {"data", obj_elf_data, 0},
85 {"text", obj_elf_text, 0},
86
87 /* End sentinel. */
88 {NULL},
89 };
90
91 static const pseudo_typeS ecoff_debug_pseudo_table[] =
92 {
93 #ifdef NEED_ECOFF_DEBUG
94 /* COFF style debugging information for ECOFF. .ln is not used; .loc
95 is used instead. */
96 { "def", ecoff_directive_def, 0 },
97 { "dim", ecoff_directive_dim, 0 },
98 { "endef", ecoff_directive_endef, 0 },
99 { "file", ecoff_directive_file, 0 },
100 { "scl", ecoff_directive_scl, 0 },
101 { "tag", ecoff_directive_tag, 0 },
102 { "val", ecoff_directive_val, 0 },
103
104 /* COFF debugging requires pseudo-ops .size and .type, but ELF
105 already has meanings for those. We use .esize and .etype
106 instead. These are only generated by gcc anyhow. */
107 { "esize", ecoff_directive_size, 0 },
108 { "etype", ecoff_directive_type, 0 },
109
110 /* ECOFF specific debugging information. */
111 { "begin", ecoff_directive_begin, 0 },
112 { "bend", ecoff_directive_bend, 0 },
113 { "end", ecoff_directive_end, 0 },
114 { "ent", ecoff_directive_ent, 0 },
115 { "fmask", ecoff_directive_fmask, 0 },
116 { "frame", ecoff_directive_frame, 0 },
117 { "loc", ecoff_directive_loc, 0 },
118 { "mask", ecoff_directive_mask, 0 },
119
120 /* Other ECOFF directives. */
121 { "extern", ecoff_directive_extern, 0 },
122
123 /* These are used on Irix. I don't know how to implement them. */
124 { "alias", s_ignore, 0 },
125 { "bgnb", s_ignore, 0 },
126 { "endb", s_ignore, 0 },
127 { "lab", s_ignore, 0 },
128 { "noalias", s_ignore, 0 },
129 { "verstamp", s_ignore, 0 },
130 { "vreg", s_ignore, 0 },
131 #endif
132
133 {NULL} /* end sentinel */
134 };
135
136 #undef NO_RELOC
137 #include "aout/aout64.h"
138
139 /* This is called when the assembler starts. */
140
141 void
142 elf_begin ()
143 {
144 /* Add symbols for the known sections to the symbol table. */
145 symbol_table_insert (section_symbol (bfd_get_section_by_name (stdoutput,
146 ".text")));
147 symbol_table_insert (section_symbol (bfd_get_section_by_name (stdoutput,
148 ".data")));
149 symbol_table_insert (section_symbol (bfd_get_section_by_name (stdoutput,
150 ".bss")));
151 }
152
153 void
154 elf_pop_insert ()
155 {
156 pop_insert (elf_pseudo_table);
157 if (ECOFF_DEBUGGING)
158 pop_insert (ecoff_debug_pseudo_table);
159 }
160
161 static bfd_vma
162 elf_s_get_size (sym)
163 symbolS *sym;
164 {
165 return S_GET_SIZE (sym);
166 }
167
168 static void
169 elf_s_set_size (sym, sz)
170 symbolS *sym;
171 bfd_vma sz;
172 {
173 S_SET_SIZE (sym, sz);
174 }
175
176 static bfd_vma
177 elf_s_get_align (sym)
178 symbolS *sym;
179 {
180 return S_GET_ALIGN (sym);
181 }
182
183 static void
184 elf_s_set_align (sym, align)
185 symbolS *sym;
186 bfd_vma align;
187 {
188 S_SET_ALIGN (sym, align);
189 }
190
191 static void
192 elf_copy_symbol_attributes (dest, src)
193 symbolS *dest, *src;
194 {
195 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
196 }
197
198 static int
199 elf_sec_sym_ok_for_reloc (sec)
200 asection *sec;
201 {
202 return obj_sec_sym_ok_for_reloc (sec);
203 }
204
205 void
206 elf_file_symbol (s)
207 char *s;
208 {
209 symbolS *sym;
210
211 sym = symbol_new (s, absolute_section, (valueT) 0, (struct frag *) 0);
212 sym->sy_frag = &zero_address_frag;
213 sym->bsym->flags |= BSF_FILE;
214
215 if (symbol_rootP != sym)
216 {
217 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
218 symbol_insert (sym, symbol_rootP, &symbol_rootP, &symbol_lastP);
219 #ifdef DEBUG
220 verify_symbol_chain (symbol_rootP, symbol_lastP);
221 #endif
222 }
223
224 #ifdef NEED_ECOFF_DEBUG
225 ecoff_new_file (s);
226 #endif
227 }
228
229 static void
230 obj_elf_common (ignore)
231 int ignore;
232 {
233 char *name;
234 char c;
235 char *p;
236 int temp, size;
237 symbolS *symbolP;
238 int have_align;
239
240 name = input_line_pointer;
241 c = get_symbol_end ();
242 /* just after name is now '\0' */
243 p = input_line_pointer;
244 *p = c;
245 SKIP_WHITESPACE ();
246 if (*input_line_pointer != ',')
247 {
248 as_bad ("Expected comma after symbol-name");
249 ignore_rest_of_line ();
250 return;
251 }
252 input_line_pointer++; /* skip ',' */
253 if ((temp = get_absolute_expression ()) < 0)
254 {
255 as_bad (".COMMon length (%d.) <0! Ignored.", temp);
256 ignore_rest_of_line ();
257 return;
258 }
259 size = temp;
260 *p = 0;
261 symbolP = symbol_find_or_make (name);
262 *p = c;
263 if (S_IS_DEFINED (symbolP))
264 {
265 as_bad ("Ignoring attempt to re-define symbol");
266 ignore_rest_of_line ();
267 return;
268 }
269 if (S_GET_VALUE (symbolP) != 0)
270 {
271 if (S_GET_VALUE (symbolP) != size)
272 {
273 as_warn ("Length of .comm \"%s\" is already %ld. Not changed to %d.",
274 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
275 }
276 }
277 know (symbolP->sy_frag == &zero_address_frag);
278 if (*input_line_pointer != ',')
279 have_align = 0;
280 else
281 {
282 have_align = 1;
283 input_line_pointer++;
284 SKIP_WHITESPACE ();
285 }
286 if (! have_align || *input_line_pointer != '"')
287 {
288 if (! have_align)
289 temp = 0;
290 else
291 {
292 temp = get_absolute_expression ();
293 if (temp < 0)
294 {
295 temp = 0;
296 as_warn ("Common alignment negative; 0 assumed");
297 }
298 }
299 if (symbolP->local)
300 {
301 segT old_sec;
302 int old_subsec;
303 char *pfrag;
304 int align;
305
306 /* allocate_bss: */
307 old_sec = now_seg;
308 old_subsec = now_subseg;
309 if (temp)
310 {
311 /* convert to a power of 2 alignment */
312 for (align = 0; (temp & 1) == 0; temp >>= 1, ++align);
313 if (temp != 1)
314 {
315 as_bad ("Common alignment not a power of 2");
316 ignore_rest_of_line ();
317 return;
318 }
319 }
320 else
321 align = 0;
322 record_alignment (bss_section, align);
323 subseg_set (bss_section, 0);
324 if (align)
325 frag_align (align, 0);
326 if (S_GET_SEGMENT (symbolP) == bss_section)
327 symbolP->sy_frag->fr_symbol = 0;
328 symbolP->sy_frag = frag_now;
329 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
330 (char *) 0);
331 *pfrag = 0;
332 S_SET_SIZE (symbolP, size);
333 S_SET_SEGMENT (symbolP, bss_section);
334 S_CLEAR_EXTERNAL (symbolP);
335 subseg_set (old_sec, old_subsec);
336 }
337 else
338 {
339 allocate_common:
340 S_SET_VALUE (symbolP, (valueT) size);
341 S_SET_ALIGN (symbolP, temp);
342 S_SET_EXTERNAL (symbolP);
343 /* should be common, but this is how gas does it for now */
344 S_SET_SEGMENT (symbolP, bfd_und_section_ptr);
345 }
346 }
347 else
348 {
349 input_line_pointer++;
350 /* @@ Some use the dot, some don't. Can we get some consistency?? */
351 if (*input_line_pointer == '.')
352 input_line_pointer++;
353 /* @@ Some say data, some say bss. */
354 if (strncmp (input_line_pointer, "bss\"", 4)
355 && strncmp (input_line_pointer, "data\"", 5))
356 {
357 while (*--input_line_pointer != '"')
358 ;
359 input_line_pointer--;
360 goto bad_common_segment;
361 }
362 while (*input_line_pointer++ != '"')
363 ;
364 goto allocate_common;
365 }
366 demand_empty_rest_of_line ();
367 return;
368
369 {
370 bad_common_segment:
371 p = input_line_pointer;
372 while (*p && *p != '\n')
373 p++;
374 c = *p;
375 *p = '\0';
376 as_bad ("bad .common segment %s", input_line_pointer + 1);
377 *p = c;
378 input_line_pointer = p;
379 ignore_rest_of_line ();
380 return;
381 }
382 }
383
384 static void
385 obj_elf_local (ignore)
386 int ignore;
387 {
388 char *name;
389 int c;
390 symbolS *symbolP;
391
392 do
393 {
394 name = input_line_pointer;
395 c = get_symbol_end ();
396 symbolP = symbol_find_or_make (name);
397 *input_line_pointer = c;
398 SKIP_WHITESPACE ();
399 S_CLEAR_EXTERNAL (symbolP);
400 symbolP->local = 1;
401 if (c == ',')
402 {
403 input_line_pointer++;
404 SKIP_WHITESPACE ();
405 if (*input_line_pointer == '\n')
406 c = '\n';
407 }
408 }
409 while (c == ',');
410 demand_empty_rest_of_line ();
411 }
412
413 static void
414 obj_elf_weak (ignore)
415 int ignore;
416 {
417 char *name;
418 int c;
419 symbolS *symbolP;
420
421 do
422 {
423 name = input_line_pointer;
424 c = get_symbol_end ();
425 symbolP = symbol_find_or_make (name);
426 *input_line_pointer = c;
427 SKIP_WHITESPACE ();
428 S_SET_WEAK (symbolP);
429 symbolP->local = 1;
430 if (c == ',')
431 {
432 input_line_pointer++;
433 SKIP_WHITESPACE ();
434 if (*input_line_pointer == '\n')
435 c = '\n';
436 }
437 }
438 while (c == ',');
439 demand_empty_rest_of_line ();
440 }
441
442 static segT previous_section;
443 static int previous_subsection;
444
445 /* Handle the .section pseudo-op. This code supports two different
446 syntaxes.
447
448 The first is found on Solaris, and looks like
449 .section ".sec1",#alloc,#execinstr,#write
450 Here the names after '#' are the SHF_* flags to turn on for the
451 section. I'm not sure how it determines the SHT_* type (BFD
452 doesn't really give us control over the type, anyhow).
453
454 The second format is found on UnixWare, and probably most SVR4
455 machines, and looks like
456 .section .sec1,"a",@progbits
457 The quoted string may contain any combination of a, w, x, and
458 represents the SHF_* flags to turn on for the section. The string
459 beginning with '@' can be progbits or nobits. There should be
460 other possibilities, but I don't know what they are. In any case,
461 BFD doesn't really let us set the section type. */
462
463 /* Certain named sections have particular defined types, listed on p.
464 4-19 of the ABI. */
465 struct special_section
466 {
467 const char *name;
468 int type;
469 int attributes;
470 };
471
472 static struct special_section special_sections[] =
473 {
474 { ".bss", SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
475 { ".comment", SHT_PROGBITS, 0 },
476 { ".data", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
477 { ".data1", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
478 { ".debug", SHT_PROGBITS, 0 },
479 { ".fini", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
480 { ".init", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
481 { ".line", SHT_PROGBITS, 0 },
482 { ".note", SHT_NOTE, 0 },
483 { ".rodata", SHT_PROGBITS, SHF_ALLOC },
484 { ".rodata1", SHT_PROGBITS, SHF_ALLOC },
485 { ".text", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
486
487 #ifdef ELF_TC_SPECIAL_SECTIONS
488 ELF_TC_SPECIAL_SECTIONS
489 #endif
490
491 #if 0
492 /* The following section names are special, but they can not
493 reasonably appear in assembler code. Some of the attributes are
494 processor dependent. */
495 { ".dynamic", SHT_DYNAMIC, SHF_ALLOC /* + SHF_WRITE */ },
496 { ".dynstr", SHT_STRTAB, SHF_ALLOC },
497 { ".dynsym", SHT_DYNSYM, SHF_ALLOC },
498 { ".got", SHT_PROGBITS, 0 },
499 { ".hash", SHT_HASH, SHF_ALLOC },
500 { ".interp", SHT_PROGBITS, /* SHF_ALLOC */ },
501 { ".plt", SHT_PROGBITS, 0 },
502 { ".shstrtab",SHT_STRTAB, 0 },
503 { ".strtab", SHT_STRTAB, /* SHF_ALLOC */ },
504 { ".symtab", SHT_SYMTAB, /* SHF_ALLOC */ },
505 #endif
506
507 { NULL, 0, 0 }
508 };
509
510 void
511 obj_elf_section (xxx)
512 int xxx;
513 {
514 char *string;
515 int new_sec;
516 segT sec;
517 int type, attr;
518 int i;
519 flagword flags;
520 symbolS *secsym;
521
522 #ifdef md_flush_pending_output
523 md_flush_pending_output ();
524 #endif
525
526 if (flag_mri)
527 {
528 char mri_type;
529
530 previous_section = now_seg;
531 previous_subsection = now_subseg;
532
533 s_mri_sect (&mri_type);
534
535 #ifdef md_elf_section_change_hook
536 md_elf_section_change_hook ();
537 #endif
538
539 return;
540 }
541
542 /* Get name of section. */
543 SKIP_WHITESPACE ();
544 if (*input_line_pointer == '"')
545 {
546 string = demand_copy_C_string (&xxx);
547 if (string == NULL)
548 {
549 ignore_rest_of_line ();
550 return;
551 }
552 }
553 else
554 {
555 char *p = input_line_pointer;
556 char c;
557 while (0 == strchr ("\n\t,; ", *p))
558 p++;
559 if (p == input_line_pointer)
560 {
561 as_warn ("Missing section name");
562 ignore_rest_of_line ();
563 return;
564 }
565 c = *p;
566 *p = 0;
567 string = xmalloc ((unsigned long) (p - input_line_pointer + 1));
568 strcpy (string, input_line_pointer);
569 *p = c;
570 input_line_pointer = p;
571 }
572
573 /* Switch to the section, creating it if necessary. */
574 previous_section = now_seg;
575 previous_subsection = now_subseg;
576
577 new_sec = bfd_get_section_by_name (stdoutput, string) == NULL;
578 sec = subseg_new (string, 0);
579
580 /* If this section already existed, we don't bother to change the
581 flag values. */
582 if (! new_sec)
583 {
584 while (! is_end_of_line[(unsigned char) *input_line_pointer])
585 ++input_line_pointer;
586 ++input_line_pointer;
587
588 #ifdef md_elf_section_change_hook
589 md_elf_section_change_hook ();
590 #endif
591
592 return;
593 }
594
595 SKIP_WHITESPACE ();
596
597 type = SHT_NULL;
598 attr = 0;
599
600 if (*input_line_pointer == ',')
601 {
602 /* Skip the comma. */
603 ++input_line_pointer;
604
605 SKIP_WHITESPACE ();
606 if (*input_line_pointer == '"')
607 {
608 /* Pick up a string with a combination of a, w, x. */
609 ++input_line_pointer;
610 while (*input_line_pointer != '"')
611 {
612 switch (*input_line_pointer)
613 {
614 case 'a':
615 attr |= SHF_ALLOC;
616 break;
617 case 'w':
618 attr |= SHF_WRITE;
619 break;
620 case 'x':
621 attr |= SHF_EXECINSTR;
622 break;
623 default:
624 {
625 char *bad_msg = "Bad .section directive: want a,w,x in string";
626 #ifdef md_elf_section_letter
627 int md_attr = md_elf_section_letter (*input_line_pointer, &bad_msg);
628 if (md_attr)
629 attr |= md_attr;
630 else
631 #endif
632 {
633 as_warn (bad_msg);
634 ignore_rest_of_line ();
635 return;
636 }
637 }
638 }
639 ++input_line_pointer;
640 }
641
642 /* Skip the closing quote. */
643 ++input_line_pointer;
644
645 SKIP_WHITESPACE ();
646 if (*input_line_pointer == ',')
647 {
648 ++input_line_pointer;
649 SKIP_WHITESPACE ();
650 if (*input_line_pointer == '@')
651 {
652 ++input_line_pointer;
653 if (strncmp (input_line_pointer, "progbits",
654 sizeof "progbits" - 1) == 0)
655 {
656 type = SHT_PROGBITS;
657 input_line_pointer += sizeof "progbits" - 1;
658 }
659 else if (strncmp (input_line_pointer, "nobits",
660 sizeof "nobits" - 1) == 0)
661 {
662 type = SHT_NOBITS;
663 input_line_pointer += sizeof "nobits" - 1;
664 }
665 else
666 {
667 #ifdef md_elf_section_type
668 int md_type = md_elf_section_type (&input_line_pointer);
669 if (md_type)
670 type = md_type;
671 else
672 #endif
673 {
674 as_warn ("Unrecognized section type");
675 ignore_rest_of_line ();
676 }
677 }
678 }
679 }
680 }
681 else
682 {
683 do
684 {
685 SKIP_WHITESPACE ();
686 if (*input_line_pointer != '#')
687 {
688 as_warn ("Bad .section directive");
689 ignore_rest_of_line ();
690 return;
691 }
692 ++input_line_pointer;
693 if (strncmp (input_line_pointer, "write",
694 sizeof "write" - 1) == 0)
695 {
696 attr |= SHF_WRITE;
697 input_line_pointer += sizeof "write" - 1;
698 }
699 else if (strncmp (input_line_pointer, "alloc",
700 sizeof "alloc" - 1) == 0)
701 {
702 attr |= SHF_ALLOC;
703 input_line_pointer += sizeof "alloc" - 1;
704 }
705 else if (strncmp (input_line_pointer, "execinstr",
706 sizeof "execinstr" - 1) == 0)
707 {
708 attr |= SHF_EXECINSTR;
709 input_line_pointer += sizeof "execinstr" - 1;
710 }
711 else
712 {
713 #ifdef md_elf_section_word
714 int md_attr = md_elf_section_word (&input_line_pointer);
715 if (md_attr)
716 attr |= md_attr;
717 else
718 #endif
719 {
720 as_warn ("Unrecognized section attribute");
721 ignore_rest_of_line ();
722 return;
723 }
724 }
725 SKIP_WHITESPACE ();
726 }
727 while (*input_line_pointer++ == ',');
728 --input_line_pointer;
729 }
730 }
731
732 /* See if this is one of the special sections. */
733 for (i = 0; special_sections[i].name != NULL; i++)
734 {
735 if (string[1] == special_sections[i].name[1]
736 && strcmp (string, special_sections[i].name) == 0)
737 {
738 if (type == SHT_NULL)
739 type = special_sections[i].type;
740 else if (type != special_sections[i].type)
741 as_warn ("Setting incorrect section type for %s", string);
742
743 if ((attr &~ special_sections[i].attributes) != 0)
744 as_warn ("Setting incorrect section attributes for %s", string);
745 attr |= special_sections[i].attributes;
746
747 break;
748 }
749 }
750
751 flags = (SEC_RELOC
752 | ((attr & SHF_WRITE) ? 0 : SEC_READONLY)
753 | ((attr & SHF_ALLOC) ? SEC_ALLOC : 0)
754 | (((attr & SHF_ALLOC) && type != SHT_NOBITS) ? SEC_LOAD : 0)
755 | ((attr & SHF_EXECINSTR) ? SEC_CODE : 0));
756 if (special_sections[i].name == NULL)
757 {
758 if (type == SHT_PROGBITS)
759 flags |= SEC_ALLOC | SEC_LOAD;
760 else if (type == SHT_NOBITS)
761 {
762 flags |= SEC_ALLOC;
763 flags &=~ SEC_LOAD;
764 }
765
766 #ifdef md_elf_section_flags
767 flags = md_elf_section_flags (flags, attr, type);
768 #endif
769 }
770
771 bfd_set_section_flags (stdoutput, sec, flags);
772
773 /* Add a symbol for this section to the symbol table. */
774 secsym = symbol_find (string);
775 if (secsym != NULL)
776 secsym->bsym = sec->symbol;
777 else
778 symbol_table_insert (section_symbol (sec));
779
780 #ifdef md_elf_section_change_hook
781 md_elf_section_change_hook ();
782 #endif
783
784 demand_empty_rest_of_line ();
785 }
786
787 /* Change to the .data section. */
788
789 static void
790 obj_elf_data (i)
791 int i;
792 {
793 previous_section = now_seg;
794 previous_subsection = now_subseg;
795 s_data (i);
796 }
797
798 /* Change to the .text section. */
799
800 static void
801 obj_elf_text (i)
802 int i;
803 {
804 previous_section = now_seg;
805 previous_subsection = now_subseg;
806 s_text (i);
807 }
808
809 void
810 obj_elf_previous (ignore)
811 int ignore;
812 {
813 if (previous_section == 0)
814 {
815 as_bad (".previous without corresponding .section; ignored");
816 return;
817 }
818 subseg_set (previous_section, previous_subsection);
819 previous_section = 0;
820 }
821
822 static void
823 obj_elf_line (ignore)
824 int ignore;
825 {
826 /* Assume delimiter is part of expression. BSD4.2 as fails with
827 delightful bug, so we are not being incompatible here. */
828 new_logical_line ((char *) NULL, (int) (get_absolute_expression ()));
829 demand_empty_rest_of_line ();
830 }
831
832 void
833 obj_read_begin_hook ()
834 {
835 #ifdef NEED_ECOFF_DEBUG
836 if (ECOFF_DEBUGGING)
837 ecoff_read_begin_hook ();
838 #endif
839 }
840
841 void
842 obj_symbol_new_hook (symbolP)
843 symbolS *symbolP;
844 {
845 symbolP->sy_obj = 0;
846
847 #ifdef NEED_ECOFF_DEBUG
848 if (ECOFF_DEBUGGING)
849 ecoff_symbol_new_hook (symbolP);
850 #endif
851 }
852
853 void
854 obj_elf_version (ignore)
855 int ignore;
856 {
857 char *name;
858 unsigned int c;
859 char ch;
860 char *p;
861 asection *seg = now_seg;
862 subsegT subseg = now_subseg;
863 Elf_Internal_Note i_note;
864 Elf_External_Note e_note;
865 asection *note_secp = (asection *) NULL;
866 int i, len;
867
868 SKIP_WHITESPACE ();
869 if (*input_line_pointer == '\"')
870 {
871 ++input_line_pointer; /* -> 1st char of string. */
872 name = input_line_pointer;
873
874 while (is_a_char (c = next_char_of_string ()))
875 ;
876 c = *input_line_pointer;
877 *input_line_pointer = '\0';
878 *(input_line_pointer - 1) = '\0';
879 *input_line_pointer = c;
880
881 /* create the .note section */
882
883 note_secp = subseg_new (".note", 0);
884 bfd_set_section_flags (stdoutput,
885 note_secp,
886 SEC_HAS_CONTENTS | SEC_READONLY);
887
888 /* process the version string */
889
890 len = strlen (name);
891
892 i_note.namesz = ((len + 1) + 3) & ~3; /* round this to word boundary */
893 i_note.descsz = 0; /* no description */
894 i_note.type = NT_VERSION;
895 p = frag_more (sizeof (e_note.namesz));
896 md_number_to_chars (p, (valueT) i_note.namesz, 4);
897 p = frag_more (sizeof (e_note.descsz));
898 md_number_to_chars (p, (valueT) i_note.descsz, 4);
899 p = frag_more (sizeof (e_note.type));
900 md_number_to_chars (p, (valueT) i_note.type, 4);
901
902 for (i = 0; i < len; i++)
903 {
904 ch = *(name + i);
905 {
906 FRAG_APPEND_1_CHAR (ch);
907 }
908 }
909 frag_align (2, 0);
910
911 subseg_set (seg, subseg);
912 }
913 else
914 {
915 as_bad ("Expected quoted string");
916 }
917 demand_empty_rest_of_line ();
918 }
919
920 static void
921 obj_elf_size (ignore)
922 int ignore;
923 {
924 char *name = input_line_pointer;
925 char c = get_symbol_end ();
926 char *p;
927 expressionS exp;
928 symbolS *sym;
929
930 p = input_line_pointer;
931 *p = c;
932 SKIP_WHITESPACE ();
933 if (*input_line_pointer != ',')
934 {
935 *p = 0;
936 as_bad ("expected comma after name `%s' in .size directive", name);
937 *p = c;
938 ignore_rest_of_line ();
939 return;
940 }
941 input_line_pointer++;
942 expression (&exp);
943 if (exp.X_op == O_absent)
944 {
945 as_bad ("missing expression in .size directive");
946 exp.X_op = O_constant;
947 exp.X_add_number = 0;
948 }
949 *p = 0;
950 sym = symbol_find_or_make (name);
951 *p = c;
952 if (exp.X_op == O_constant)
953 S_SET_SIZE (sym, exp.X_add_number);
954 else
955 {
956 sym->sy_obj = (expressionS *) xmalloc (sizeof (expressionS));
957 *sym->sy_obj = exp;
958 }
959 demand_empty_rest_of_line ();
960 }
961
962 /* Handle the ELF .type pseudo-op. This sets the type of a symbol.
963 There are three syntaxes. The first (used on Solaris) is
964 .type SYM,#function
965 The second (used on UnixWare) is
966 .type SYM,@function
967 The third (reportedly to be used on Irix 6.0) is
968 .type SYM STT_FUNC
969 */
970
971 static void
972 obj_elf_type (ignore)
973 int ignore;
974 {
975 char *name;
976 char c;
977 int type;
978 const char *typename;
979 symbolS *sym;
980
981 name = input_line_pointer;
982 c = get_symbol_end ();
983 sym = symbol_find_or_make (name);
984 *input_line_pointer = c;
985
986 SKIP_WHITESPACE ();
987 if (*input_line_pointer == ',')
988 ++input_line_pointer;
989
990 SKIP_WHITESPACE ();
991 if (*input_line_pointer == '#' || *input_line_pointer == '@')
992 ++input_line_pointer;
993
994 typename = input_line_pointer;
995 c = get_symbol_end ();
996
997 type = 0;
998 if (strcmp (typename, "function") == 0
999 || strcmp (typename, "STT_FUNC") == 0)
1000 type = BSF_FUNCTION;
1001 else if (strcmp (typename, "object") == 0
1002 || strcmp (typename, "STT_OBJECT") == 0)
1003 type = BSF_OBJECT;
1004 else
1005 as_bad ("ignoring unrecognized symbol type \"%s\"", typename);
1006
1007 *input_line_pointer = c;
1008
1009 sym->bsym->flags |= type;
1010
1011 demand_empty_rest_of_line ();
1012 }
1013
1014 static void
1015 obj_elf_ident (ignore)
1016 int ignore;
1017 {
1018 static segT comment_section;
1019 segT old_section = now_seg;
1020 int old_subsection = now_subseg;
1021
1022 if (!comment_section)
1023 {
1024 char *p;
1025 comment_section = subseg_new (".comment", 0);
1026 bfd_set_section_flags (stdoutput, comment_section,
1027 SEC_READONLY | SEC_HAS_CONTENTS);
1028 p = frag_more (1);
1029 *p = 0;
1030 }
1031 else
1032 subseg_set (comment_section, 0);
1033 stringer (1);
1034 subseg_set (old_section, old_subsection);
1035 }
1036
1037 #ifdef INIT_STAB_SECTION
1038
1039 /* The first entry in a .stabs section is special. */
1040
1041 void
1042 obj_elf_init_stab_section (seg)
1043 segT seg;
1044 {
1045 char *file;
1046 char *p;
1047 char *stabstr_name;
1048 unsigned int stroff;
1049
1050 /* Force the section to align to a longword boundary. Without this,
1051 UnixWare ar crashes. */
1052 bfd_set_section_alignment (stdoutput, seg, 2);
1053
1054 /* Make space for this first symbol. */
1055 p = frag_more (12);
1056 /* Zero it out. */
1057 memset (p, 0, 12);
1058 as_where (&file, (unsigned int *) NULL);
1059 stabstr_name = (char *) alloca (strlen (segment_name (seg)) + 4);
1060 strcpy (stabstr_name, segment_name (seg));
1061 strcat (stabstr_name, "str");
1062 stroff = get_stab_string_offset (file, stabstr_name);
1063 know (stroff == 1);
1064 md_number_to_chars (p, stroff, 4);
1065 seg_info (seg)->stabu.p = p;
1066 }
1067
1068 #endif
1069
1070 /* Fill in the counts in the first entry in a .stabs section. */
1071
1072 static void
1073 adjust_stab_sections (abfd, sec, xxx)
1074 bfd *abfd;
1075 asection *sec;
1076 PTR xxx;
1077 {
1078 char *name;
1079 asection *strsec;
1080 char *p;
1081 int strsz, nsyms;
1082
1083 if (strncmp (".stab", sec->name, 5))
1084 return;
1085 if (!strcmp ("str", sec->name + strlen (sec->name) - 3))
1086 return;
1087
1088 name = (char *) alloca (strlen (sec->name) + 4);
1089 strcpy (name, sec->name);
1090 strcat (name, "str");
1091 strsec = bfd_get_section_by_name (abfd, name);
1092 if (strsec)
1093 strsz = bfd_section_size (abfd, strsec);
1094 else
1095 strsz = 0;
1096 nsyms = bfd_section_size (abfd, sec) / 12 - 1;
1097
1098 p = seg_info (sec)->stabu.p;
1099 assert (p != 0);
1100
1101 bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6);
1102 bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8);
1103 }
1104
1105 #ifdef NEED_ECOFF_DEBUG
1106
1107 /* This function is called by the ECOFF code. It is supposed to
1108 record the external symbol information so that the backend can
1109 write it out correctly. The ELF backend doesn't actually handle
1110 this at the moment, so we do it ourselves. We save the information
1111 in the symbol. */
1112
1113 void
1114 elf_ecoff_set_ext (sym, ext)
1115 symbolS *sym;
1116 struct ecoff_extr *ext;
1117 {
1118 sym->bsym->udata.p = (PTR) ext;
1119 }
1120
1121 /* This function is called by bfd_ecoff_debug_externals. It is
1122 supposed to *EXT to the external symbol information, and return
1123 whether the symbol should be used at all. */
1124
1125 static boolean
1126 elf_get_extr (sym, ext)
1127 asymbol *sym;
1128 EXTR *ext;
1129 {
1130 if (sym->udata.p == NULL)
1131 return false;
1132 *ext = *(EXTR *) sym->udata.p;
1133 return true;
1134 }
1135
1136 /* This function is called by bfd_ecoff_debug_externals. It has
1137 nothing to do for ELF. */
1138
1139 /*ARGSUSED*/
1140 static void
1141 elf_set_index (sym, indx)
1142 asymbol *sym;
1143 bfd_size_type indx;
1144 {
1145 }
1146
1147 #endif /* NEED_ECOFF_DEBUG */
1148
1149 void
1150 elf_frob_symbol (symp, puntp)
1151 symbolS *symp;
1152 int *puntp;
1153 {
1154 #ifdef NEED_ECOFF_DEBUG
1155 if (ECOFF_DEBUGGING)
1156 ecoff_frob_symbol (symp);
1157 #endif
1158
1159 if (symp->sy_obj)
1160 {
1161 switch (symp->sy_obj->X_op)
1162 {
1163 case O_subtract:
1164 S_SET_SIZE (symp,
1165 (S_GET_VALUE (symp->sy_obj->X_add_symbol)
1166 + symp->sy_obj->X_add_number
1167 - S_GET_VALUE (symp->sy_obj->X_op_symbol)));
1168 break;
1169 case O_constant:
1170 S_SET_SIZE (symp,
1171 (S_GET_VALUE (symp->sy_obj->X_add_symbol)
1172 + symp->sy_obj->X_add_number));
1173 break;
1174 default:
1175 as_bad (".size expression too complicated to fix up");
1176 break;
1177 }
1178 free (symp->sy_obj);
1179 symp->sy_obj = 0;
1180 }
1181
1182 /* Double check weak symbols. */
1183 if (symp->bsym->flags & BSF_WEAK)
1184 {
1185 if (S_IS_COMMON (symp))
1186 as_bad ("Symbol `%s' can not be both weak and common",
1187 S_GET_NAME (symp));
1188 }
1189
1190 #ifdef TC_MIPS
1191 /* The Irix 5 assembler appears to set the type of any common symbol
1192 to STT_OBJECT. We try to be compatible, since the Irix 5 linker
1193 apparently sometimes cares. FIXME: What about Irix 6? */
1194 if (S_IS_COMMON (symp))
1195 symp->bsym->flags |= BSF_OBJECT;
1196 #endif
1197
1198 #ifdef TC_PPC
1199 /* Frob the PowerPC, so that the symbol always has object type
1200 if it is not some other type. VxWorks needs this. */
1201 if ((symp->bsym->flags & (BSF_FUNCTION | BSF_FILE | BSF_SECTION_SYM)) == 0
1202 && S_IS_DEFINED (symp))
1203 symp->bsym->flags |= BSF_OBJECT;
1204 #endif
1205 }
1206
1207 void
1208 elf_frob_file ()
1209 {
1210 bfd_map_over_sections (stdoutput, adjust_stab_sections, (PTR) 0);
1211
1212 #ifdef elf_tc_final_processing
1213 elf_tc_final_processing ();
1214 #endif
1215 }
1216
1217 /* It is required that we let write_relocs have the opportunity to
1218 optimize away fixups before output has begun, since it is possible
1219 to eliminate all fixups for a section and thus we never should
1220 have generated the relocation section. */
1221
1222 void
1223 elf_frob_file_after_relocs ()
1224 {
1225 #ifdef NEED_ECOFF_DEBUG
1226 if (ECOFF_DEBUGGING)
1227 /* Generate the ECOFF debugging information. */
1228 {
1229 const struct ecoff_debug_swap *debug_swap;
1230 struct ecoff_debug_info debug;
1231 char *buf;
1232 asection *sec;
1233
1234 debug_swap
1235 = get_elf_backend_data (stdoutput)->elf_backend_ecoff_debug_swap;
1236 know (debug_swap != (const struct ecoff_debug_swap *) NULL);
1237 ecoff_build_debug (&debug.symbolic_header, &buf, debug_swap);
1238
1239 /* Set up the pointers in debug. */
1240 #define SET(ptr, offset, type) \
1241 debug.ptr = (type) (buf + debug.symbolic_header.offset)
1242
1243 SET (line, cbLineOffset, unsigned char *);
1244 SET (external_dnr, cbDnOffset, PTR);
1245 SET (external_pdr, cbPdOffset, PTR);
1246 SET (external_sym, cbSymOffset, PTR);
1247 SET (external_opt, cbOptOffset, PTR);
1248 SET (external_aux, cbAuxOffset, union aux_ext *);
1249 SET (ss, cbSsOffset, char *);
1250 SET (external_fdr, cbFdOffset, PTR);
1251 SET (external_rfd, cbRfdOffset, PTR);
1252 /* ssext and external_ext are set up just below. */
1253
1254 #undef SET
1255
1256 /* Set up the external symbols. */
1257 debug.ssext = debug.ssext_end = NULL;
1258 debug.external_ext = debug.external_ext_end = NULL;
1259 if (! bfd_ecoff_debug_externals (stdoutput, &debug, debug_swap, true,
1260 elf_get_extr, elf_set_index))
1261 as_fatal ("Failed to set up debugging information: %s",
1262 bfd_errmsg (bfd_get_error ()));
1263
1264 sec = bfd_get_section_by_name (stdoutput, ".mdebug");
1265 assert (sec != NULL);
1266
1267 know (stdoutput->output_has_begun == false);
1268
1269 /* We set the size of the section, call bfd_set_section_contents
1270 to force the ELF backend to allocate a file position, and then
1271 write out the data. FIXME: Is this really the best way to do
1272 this? */
1273 sec->_raw_size = bfd_ecoff_debug_size (stdoutput, &debug, debug_swap);
1274
1275 if (! bfd_set_section_contents (stdoutput, sec, (PTR) NULL,
1276 (file_ptr) 0, (bfd_size_type) 0))
1277 as_fatal ("Can't start writing .mdebug section: %s",
1278 bfd_errmsg (bfd_get_error ()));
1279
1280 know (stdoutput->output_has_begun == true);
1281 know (sec->filepos != 0);
1282
1283 if (! bfd_ecoff_write_debug (stdoutput, &debug, debug_swap,
1284 sec->filepos))
1285 as_fatal ("Could not write .mdebug section: %s",
1286 bfd_errmsg (bfd_get_error ()));
1287 }
1288 #endif /* NEED_ECOFF_DEBUG */
1289 }
1290
1291 const struct format_ops elf_format_ops =
1292 {
1293 bfd_target_elf_flavour,
1294 0,
1295 1,
1296 elf_frob_symbol,
1297 elf_frob_file,
1298 elf_frob_file_after_relocs,
1299 elf_s_get_size, elf_s_set_size,
1300 elf_s_get_align, elf_s_set_align,
1301 elf_copy_symbol_attributes,
1302 #ifdef NEED_ECOFF_DEBUG
1303 ecoff_generate_asm_lineno,
1304 ecoff_stab,
1305 #else
1306 0,
1307 0, /* process_stab */
1308 #endif
1309 elf_sec_sym_ok_for_reloc,
1310 elf_pop_insert,
1311 #ifdef NEED_ECOFF_DEBUG
1312 elf_ecoff_set_ext,
1313 #else
1314 0,
1315 #endif
1316 obj_read_begin_hook,
1317 obj_symbol_new_hook,
1318 };
This page took 0.059955 seconds and 4 git commands to generate.