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