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