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