OSDN Git Service

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