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