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