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