RISC-V: Fix linker crash in section symbol check.
[external/binutils.git] / bfd / elf64-sparc.c
1 /* SPARC-specific support for 64-bit ELF
2    Copyright (C) 1993-2019 Free Software Foundation, Inc.
3
4    This file is part of BFD, the Binary File Descriptor library.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20
21 #include "sysdep.h"
22 #include <limits.h>
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/sparc.h"
27 #include "opcode/sparc.h"
28 #include "elfxx-sparc.h"
29
30 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
31 #define MINUS_ONE (~ (bfd_vma) 0)
32
33 /* Due to the way how we handle R_SPARC_OLO10, each entry in a SHT_RELA
34    section can represent up to two relocs, we must tell the user to allocate
35    more space.  */
36
37 #if GCC_VERSION >= 4003
38 # pragma GCC diagnostic push
39 # pragma GCC diagnostic ignored "-Wtype-limits"
40 #endif
41 static long
42 elf64_sparc_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
43 {
44   if (sec->reloc_count >= LONG_MAX / 2 / sizeof (arelent *))
45     {
46       bfd_set_error (bfd_error_file_too_big);
47       return -1;
48     }
49   return (sec->reloc_count * 2 + 1) * sizeof (arelent *);
50 }
51 #if GCC_VERSION >= 4003
52 # pragma GCC diagnostic pop
53 #endif
54
55 static long
56 elf64_sparc_get_dynamic_reloc_upper_bound (bfd *abfd)
57 {
58   long ret = _bfd_elf_get_dynamic_reloc_upper_bound (abfd);
59   if (ret > LONG_MAX / 2)
60     {
61       bfd_set_error (bfd_error_file_too_big);
62       ret = -1;
63     }
64   else if (ret > 0)
65     ret *= 2;
66   return ret;
67 }
68
69 /* Read  relocations for ASECT from REL_HDR.  There are RELOC_COUNT of
70    them.  We cannot use generic elf routines for this,  because R_SPARC_OLO10
71    has secondary addend in ELF64_R_TYPE_DATA.  We handle it as two relocations
72    for the same location,  R_SPARC_LO10 and R_SPARC_13.  */
73
74 static bfd_boolean
75 elf64_sparc_slurp_one_reloc_table (bfd *abfd, asection *asect,
76                                    Elf_Internal_Shdr *rel_hdr,
77                                    asymbol **symbols, bfd_boolean dynamic)
78 {
79   void * allocated = NULL;
80   bfd_byte *native_relocs;
81   arelent *relent;
82   unsigned int i;
83   int entsize;
84   bfd_size_type count;
85   arelent *relents;
86
87   allocated = bfd_malloc (rel_hdr->sh_size);
88   if (allocated == NULL)
89     goto error_return;
90
91   if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
92       || bfd_bread (allocated, rel_hdr->sh_size, abfd) != rel_hdr->sh_size)
93     goto error_return;
94
95   native_relocs = (bfd_byte *) allocated;
96
97   relents = asect->relocation + canon_reloc_count (asect);
98
99   entsize = rel_hdr->sh_entsize;
100   BFD_ASSERT (entsize == sizeof (Elf64_External_Rela));
101
102   count = rel_hdr->sh_size / entsize;
103
104   for (i = 0, relent = relents; i < count;
105        i++, relent++, native_relocs += entsize)
106     {
107       Elf_Internal_Rela rela;
108       unsigned int r_type;
109
110       bfd_elf64_swap_reloca_in (abfd, native_relocs, &rela);
111
112       /* The address of an ELF reloc is section relative for an object
113          file, and absolute for an executable file or shared library.
114          The address of a normal BFD reloc is always section relative,
115          and the address of a dynamic reloc is absolute..  */
116       if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
117         relent->address = rela.r_offset;
118       else
119         relent->address = rela.r_offset - asect->vma;
120
121       if (ELF64_R_SYM (rela.r_info) == STN_UNDEF)
122         relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
123       else if (/* PR 17512: file: 996185f8.  */
124                (!dynamic && ELF64_R_SYM(rela.r_info) > bfd_get_symcount(abfd))
125                || (dynamic
126                    && ELF64_R_SYM(rela.r_info) > bfd_get_dynamic_symcount(abfd)))
127         {
128           _bfd_error_handler
129             /* xgettext:c-format */
130             (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
131              abfd, asect, i, (long) ELF64_R_SYM (rela.r_info));
132           bfd_set_error (bfd_error_bad_value);
133           relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
134         }
135       else
136         {
137           asymbol **ps, *s;
138
139           ps = symbols + ELF64_R_SYM (rela.r_info) - 1;
140           s = *ps;
141
142           /* Canonicalize ELF section symbols.  FIXME: Why?  */
143           if ((s->flags & BSF_SECTION_SYM) == 0)
144             relent->sym_ptr_ptr = ps;
145           else
146             relent->sym_ptr_ptr = s->section->symbol_ptr_ptr;
147         }
148
149       relent->addend = rela.r_addend;
150
151       r_type = ELF64_R_TYPE_ID (rela.r_info);
152       if (r_type == R_SPARC_OLO10)
153         {
154           relent->howto = _bfd_sparc_elf_info_to_howto_ptr (abfd, R_SPARC_LO10);
155           relent[1].address = relent->address;
156           relent++;
157           relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
158           relent->addend = ELF64_R_TYPE_DATA (rela.r_info);
159           relent->howto = _bfd_sparc_elf_info_to_howto_ptr (abfd, R_SPARC_13);
160         }
161       else
162         {
163           relent->howto = _bfd_sparc_elf_info_to_howto_ptr (abfd, r_type);
164           if (relent->howto == NULL)
165             goto error_return;
166         }
167     }
168
169   canon_reloc_count (asect) += relent - relents;
170
171   if (allocated != NULL)
172     free (allocated);
173
174   return TRUE;
175
176  error_return:
177   if (allocated != NULL)
178     free (allocated);
179   return FALSE;
180 }
181
182 /* Read in and swap the external relocs.  */
183
184 static bfd_boolean
185 elf64_sparc_slurp_reloc_table (bfd *abfd, asection *asect,
186                                asymbol **symbols, bfd_boolean dynamic)
187 {
188   struct bfd_elf_section_data * const d = elf_section_data (asect);
189   Elf_Internal_Shdr *rel_hdr;
190   Elf_Internal_Shdr *rel_hdr2;
191   bfd_size_type amt;
192
193   if (asect->relocation != NULL)
194     return TRUE;
195
196   if (! dynamic)
197     {
198       if ((asect->flags & SEC_RELOC) == 0
199           || asect->reloc_count == 0)
200         return TRUE;
201
202       rel_hdr = d->rel.hdr;
203       rel_hdr2 = d->rela.hdr;
204
205       BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
206                   || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
207     }
208   else
209     {
210       /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
211          case because relocations against this section may use the
212          dynamic symbol table, and in that case bfd_section_from_shdr
213          in elf.c does not update the RELOC_COUNT.  */
214       if (asect->size == 0)
215         return TRUE;
216
217       rel_hdr = &d->this_hdr;
218       asect->reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
219       rel_hdr2 = NULL;
220     }
221
222   amt = asect->reloc_count;
223   amt *= 2 * sizeof (arelent);
224   asect->relocation = (arelent *) bfd_alloc (abfd, amt);
225   if (asect->relocation == NULL)
226     return FALSE;
227
228   /* The elf64_sparc_slurp_one_reloc_table routine increments
229      canon_reloc_count.  */
230   canon_reloc_count (asect) = 0;
231
232   if (rel_hdr
233       && !elf64_sparc_slurp_one_reloc_table (abfd, asect, rel_hdr, symbols,
234                                              dynamic))
235     return FALSE;
236
237   if (rel_hdr2
238       && !elf64_sparc_slurp_one_reloc_table (abfd, asect, rel_hdr2, symbols,
239                                              dynamic))
240     return FALSE;
241
242   return TRUE;
243 }
244
245 /* Canonicalize the relocs.  */
246
247 static long
248 elf64_sparc_canonicalize_reloc (bfd *abfd, sec_ptr section,
249                                 arelent **relptr, asymbol **symbols)
250 {
251   arelent *tblptr;
252   unsigned int i;
253   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
254
255   if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
256     return -1;
257
258   tblptr = section->relocation;
259   for (i = 0; i < canon_reloc_count (section); i++)
260     *relptr++ = tblptr++;
261
262   *relptr = NULL;
263
264   return canon_reloc_count (section);
265 }
266
267
268 /* Canonicalize the dynamic relocation entries.  Note that we return
269    the dynamic relocations as a single block, although they are
270    actually associated with particular sections; the interface, which
271    was designed for SunOS style shared libraries, expects that there
272    is only one set of dynamic relocs.  Any section that was actually
273    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
274    the dynamic symbol table, is considered to be a dynamic reloc
275    section.  */
276
277 static long
278 elf64_sparc_canonicalize_dynamic_reloc (bfd *abfd, arelent **storage,
279                                         asymbol **syms)
280 {
281   asection *s;
282   long ret;
283
284   if (elf_dynsymtab (abfd) == 0)
285     {
286       bfd_set_error (bfd_error_invalid_operation);
287       return -1;
288     }
289
290   ret = 0;
291   for (s = abfd->sections; s != NULL; s = s->next)
292     {
293       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
294           && (elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
295         {
296           arelent *p;
297           long count, i;
298
299           if (! elf64_sparc_slurp_reloc_table (abfd, s, syms, TRUE))
300             return -1;
301           count = canon_reloc_count (s);
302           p = s->relocation;
303           for (i = 0; i < count; i++)
304             *storage++ = p++;
305           ret += count;
306         }
307     }
308
309   *storage = NULL;
310
311   return ret;
312 }
313
314 /* Install a new set of internal relocs.  */
315
316 static void
317 elf64_sparc_set_reloc (bfd *abfd ATTRIBUTE_UNUSED,
318                        asection *asect,
319                        arelent **location,
320                        unsigned int count)
321 {
322   asect->orelocation = location;
323   canon_reloc_count (asect) = count;
324 }
325
326 /* Write out the relocs.  */
327
328 static void
329 elf64_sparc_write_relocs (bfd *abfd, asection *sec, void * data)
330 {
331   bfd_boolean *failedp = (bfd_boolean *) data;
332   Elf_Internal_Shdr *rela_hdr;
333   bfd_vma addr_offset;
334   Elf64_External_Rela *outbound_relocas, *src_rela;
335   unsigned int idx, count;
336   asymbol *last_sym = 0;
337   int last_sym_idx = 0;
338
339   /* If we have already failed, don't do anything.  */
340   if (*failedp)
341     return;
342
343   if ((sec->flags & SEC_RELOC) == 0)
344     return;
345
346   /* The linker backend writes the relocs out itself, and sets the
347      reloc_count field to zero to inhibit writing them here.  Also,
348      sometimes the SEC_RELOC flag gets set even when there aren't any
349      relocs.  */
350   if (canon_reloc_count (sec) == 0)
351     return;
352
353   /* We can combine two relocs that refer to the same address
354      into R_SPARC_OLO10 if first one is R_SPARC_LO10 and the
355      latter is R_SPARC_13 with no associated symbol.  */
356   count = 0;
357   for (idx = 0; idx < canon_reloc_count (sec); idx++)
358     {
359       bfd_vma addr;
360
361       ++count;
362
363       addr = sec->orelocation[idx]->address;
364       if (sec->orelocation[idx]->howto->type == R_SPARC_LO10
365           && idx < canon_reloc_count (sec) - 1)
366         {
367           arelent *r = sec->orelocation[idx + 1];
368
369           if (r->howto->type == R_SPARC_13
370               && r->address == addr
371               && bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
372               && (*r->sym_ptr_ptr)->value == 0)
373             ++idx;
374         }
375     }
376
377   rela_hdr = elf_section_data (sec)->rela.hdr;
378
379   rela_hdr->sh_size = rela_hdr->sh_entsize * count;
380   rela_hdr->contents = bfd_alloc (abfd, rela_hdr->sh_size);
381   if (rela_hdr->contents == NULL)
382     {
383       *failedp = TRUE;
384       return;
385     }
386
387   /* Figure out whether the relocations are RELA or REL relocations.  */
388   if (rela_hdr->sh_type != SHT_RELA)
389     abort ();
390
391   /* The address of an ELF reloc is section relative for an object
392      file, and absolute for an executable file or shared library.
393      The address of a BFD reloc is always section relative.  */
394   addr_offset = 0;
395   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
396     addr_offset = sec->vma;
397
398   /* orelocation has the data, reloc_count has the count...  */
399   outbound_relocas = (Elf64_External_Rela *) rela_hdr->contents;
400   src_rela = outbound_relocas;
401
402   for (idx = 0; idx < canon_reloc_count (sec); idx++)
403     {
404       Elf_Internal_Rela dst_rela;
405       arelent *ptr;
406       asymbol *sym;
407       int n;
408
409       ptr = sec->orelocation[idx];
410       sym = *ptr->sym_ptr_ptr;
411       if (sym == last_sym)
412         n = last_sym_idx;
413       else if (bfd_is_abs_section (sym->section) && sym->value == 0)
414         n = STN_UNDEF;
415       else
416         {
417           last_sym = sym;
418           n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
419           if (n < 0)
420             {
421               *failedp = TRUE;
422               return;
423             }
424           last_sym_idx = n;
425         }
426
427       if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
428           && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
429           && ! _bfd_elf_validate_reloc (abfd, ptr))
430         {
431           *failedp = TRUE;
432           return;
433         }
434
435       if (ptr->howto->type == R_SPARC_LO10
436           && idx < canon_reloc_count (sec) - 1)
437         {
438           arelent *r = sec->orelocation[idx + 1];
439
440           if (r->howto->type == R_SPARC_13
441               && r->address == ptr->address
442               && bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
443               && (*r->sym_ptr_ptr)->value == 0)
444             {
445               idx++;
446               dst_rela.r_info
447                 = ELF64_R_INFO (n, ELF64_R_TYPE_INFO (r->addend,
448                                                       R_SPARC_OLO10));
449             }
450           else
451             dst_rela.r_info = ELF64_R_INFO (n, R_SPARC_LO10);
452         }
453       else
454         dst_rela.r_info = ELF64_R_INFO (n, ptr->howto->type);
455
456       dst_rela.r_offset = ptr->address + addr_offset;
457       dst_rela.r_addend = ptr->addend;
458
459       bfd_elf64_swap_reloca_out (abfd, &dst_rela, (bfd_byte *) src_rela);
460       ++src_rela;
461     }
462 }
463 \f
464 /* Hook called by the linker routine which adds symbols from an object
465    file.  We use it for STT_REGISTER symbols.  */
466
467 static bfd_boolean
468 elf64_sparc_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
469                              Elf_Internal_Sym *sym, const char **namep,
470                              flagword *flagsp ATTRIBUTE_UNUSED,
471                              asection **secp ATTRIBUTE_UNUSED,
472                              bfd_vma *valp ATTRIBUTE_UNUSED)
473 {
474   static const char *const stt_types[] = { "NOTYPE", "OBJECT", "FUNCTION" };
475
476   if (ELF_ST_TYPE (sym->st_info) == STT_REGISTER)
477     {
478       int reg;
479       struct _bfd_sparc_elf_app_reg *p;
480
481       reg = (int)sym->st_value;
482       switch (reg & ~1)
483         {
484         case 2: reg -= 2; break;
485         case 6: reg -= 4; break;
486         default:
487           _bfd_error_handler
488             (_("%pB: only registers %%g[2367] can be declared using STT_REGISTER"),
489              abfd);
490           return FALSE;
491         }
492
493       if (info->output_bfd->xvec != abfd->xvec
494           || (abfd->flags & DYNAMIC) != 0)
495         {
496           /* STT_REGISTER only works when linking an elf64_sparc object.
497              If STT_REGISTER comes from a dynamic object, don't put it into
498              the output bfd.  The dynamic linker will recheck it.  */
499           *namep = NULL;
500           return TRUE;
501         }
502
503       p = _bfd_sparc_elf_hash_table(info)->app_regs + reg;
504
505       if (p->name != NULL && strcmp (p->name, *namep))
506         {
507           _bfd_error_handler
508             /* xgettext:c-format */
509             (_("register %%g%d used incompatibly: %s in %pB,"
510                " previously %s in %pB"),
511              (int) sym->st_value, **namep ? *namep : "#scratch", abfd,
512              *p->name ? p->name : "#scratch", p->abfd);
513           return FALSE;
514         }
515
516       if (p->name == NULL)
517         {
518           if (**namep)
519             {
520               struct elf_link_hash_entry *h;
521
522               h = (struct elf_link_hash_entry *)
523                 bfd_link_hash_lookup (info->hash, *namep, FALSE, FALSE, FALSE);
524
525               if (h != NULL)
526                 {
527                   unsigned char type = h->type;
528
529                   if (type > STT_FUNC)
530                     type = 0;
531                   _bfd_error_handler
532                     /* xgettext:c-format */
533                     (_("symbol `%s' has differing types: REGISTER in %pB,"
534                        " previously %s in %pB"),
535                      *namep, abfd, stt_types[type], p->abfd);
536                   return FALSE;
537                 }
538
539               p->name = bfd_hash_allocate (&info->hash->table,
540                                            strlen (*namep) + 1);
541               if (!p->name)
542                 return FALSE;
543
544               strcpy (p->name, *namep);
545             }
546           else
547             p->name = "";
548           p->bind = ELF_ST_BIND (sym->st_info);
549           p->abfd = abfd;
550           p->shndx = sym->st_shndx;
551         }
552       else
553         {
554           if (p->bind == STB_WEAK
555               && ELF_ST_BIND (sym->st_info) == STB_GLOBAL)
556             {
557               p->bind = STB_GLOBAL;
558               p->abfd = abfd;
559             }
560         }
561       *namep = NULL;
562       return TRUE;
563     }
564   else if (*namep && **namep
565            && info->output_bfd->xvec == abfd->xvec)
566     {
567       int i;
568       struct _bfd_sparc_elf_app_reg *p;
569
570       p = _bfd_sparc_elf_hash_table(info)->app_regs;
571       for (i = 0; i < 4; i++, p++)
572         if (p->name != NULL && ! strcmp (p->name, *namep))
573           {
574             unsigned char type = ELF_ST_TYPE (sym->st_info);
575
576             if (type > STT_FUNC)
577               type = 0;
578             _bfd_error_handler
579               /* xgettext:c-format */
580               (_("Symbol `%s' has differing types: %s in %pB,"
581                  " previously REGISTER in %pB"),
582                *namep, stt_types[type], abfd, p->abfd);
583             return FALSE;
584           }
585     }
586   return TRUE;
587 }
588
589 /* This function takes care of emitting STT_REGISTER symbols
590    which we cannot easily keep in the symbol hash table.  */
591
592 static bfd_boolean
593 elf64_sparc_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
594                               struct bfd_link_info *info,
595                               void * flaginfo,
596                               int (*func) (void *, const char *,
597                                            Elf_Internal_Sym *,
598                                            asection *,
599                                            struct elf_link_hash_entry *))
600 {
601   int reg;
602   struct _bfd_sparc_elf_app_reg *app_regs =
603     _bfd_sparc_elf_hash_table(info)->app_regs;
604   Elf_Internal_Sym sym;
605
606   for (reg = 0; reg < 4; reg++)
607     if (app_regs [reg].name != NULL)
608       {
609         if (info->strip == strip_some
610             && bfd_hash_lookup (info->keep_hash,
611                                 app_regs [reg].name,
612                                 FALSE, FALSE) == NULL)
613           continue;
614
615         sym.st_value = reg < 2 ? reg + 2 : reg + 4;
616         sym.st_size = 0;
617         sym.st_other = 0;
618         sym.st_info = ELF_ST_INFO (app_regs [reg].bind, STT_REGISTER);
619         sym.st_shndx = app_regs [reg].shndx;
620         sym.st_target_internal = 0;
621         if ((*func) (flaginfo, app_regs [reg].name, &sym,
622                      sym.st_shndx == SHN_ABS
623                      ? bfd_abs_section_ptr : bfd_und_section_ptr,
624                      NULL) != 1)
625           return FALSE;
626       }
627
628   return TRUE;
629 }
630
631 static int
632 elf64_sparc_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
633 {
634   if (ELF_ST_TYPE (elf_sym->st_info) == STT_REGISTER)
635     return STT_REGISTER;
636   else
637     return type;
638 }
639
640 /* A STB_GLOBAL,STT_REGISTER symbol should be BSF_GLOBAL
641    even in SHN_UNDEF section.  */
642
643 static void
644 elf64_sparc_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *asym)
645 {
646   elf_symbol_type *elfsym;
647
648   elfsym = (elf_symbol_type *) asym;
649   if (elfsym->internal_elf_sym.st_info
650       == ELF_ST_INFO (STB_GLOBAL, STT_REGISTER))
651     {
652       asym->flags |= BSF_GLOBAL;
653     }
654 }
655
656 \f
657 /* Functions for dealing with the e_flags field.  */
658
659 /* Merge backend specific data from an object file to the output
660    object file when linking.  */
661
662 static bfd_boolean
663 elf64_sparc_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
664 {
665   bfd *obfd = info->output_bfd;
666   bfd_boolean error;
667   flagword new_flags, old_flags;
668   int new_mm, old_mm;
669
670   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
671       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
672     return TRUE;
673
674   new_flags = elf_elfheader (ibfd)->e_flags;
675   old_flags = elf_elfheader (obfd)->e_flags;
676
677   if (!elf_flags_init (obfd))   /* First call, no flags set */
678     {
679       elf_flags_init (obfd) = TRUE;
680       elf_elfheader (obfd)->e_flags = new_flags;
681     }
682
683   else if (new_flags == old_flags)      /* Compatible flags are ok */
684     ;
685
686   else                                  /* Incompatible flags */
687     {
688       error = FALSE;
689
690 #define EF_SPARC_ISA_EXTENSIONS \
691   (EF_SPARC_SUN_US1 | EF_SPARC_SUN_US3 | EF_SPARC_HAL_R1)
692
693       if ((ibfd->flags & DYNAMIC) != 0)
694         {
695           /* We don't want dynamic objects memory ordering and
696              architecture to have any role. That's what dynamic linker
697              should do.  */
698           new_flags &= ~(EF_SPARCV9_MM | EF_SPARC_ISA_EXTENSIONS);
699           new_flags |= (old_flags
700                         & (EF_SPARCV9_MM | EF_SPARC_ISA_EXTENSIONS));
701         }
702       else
703         {
704           /* Choose the highest architecture requirements.  */
705           old_flags |= (new_flags & EF_SPARC_ISA_EXTENSIONS);
706           new_flags |= (old_flags & EF_SPARC_ISA_EXTENSIONS);
707           if ((old_flags & (EF_SPARC_SUN_US1 | EF_SPARC_SUN_US3))
708               && (old_flags & EF_SPARC_HAL_R1))
709             {
710               error = TRUE;
711               _bfd_error_handler
712                 (_("%pB: linking UltraSPARC specific with HAL specific code"),
713                  ibfd);
714             }
715           /* Choose the most restrictive memory ordering.  */
716           old_mm = (old_flags & EF_SPARCV9_MM);
717           new_mm = (new_flags & EF_SPARCV9_MM);
718           old_flags &= ~EF_SPARCV9_MM;
719           new_flags &= ~EF_SPARCV9_MM;
720           if (new_mm < old_mm)
721             old_mm = new_mm;
722           old_flags |= old_mm;
723           new_flags |= old_mm;
724         }
725
726       /* Warn about any other mismatches */
727       if (new_flags != old_flags)
728         {
729           error = TRUE;
730           _bfd_error_handler
731             /* xgettext:c-format */
732             (_("%pB: uses different e_flags (%#x) fields than previous modules (%#x)"),
733              ibfd, new_flags, old_flags);
734         }
735
736       elf_elfheader (obfd)->e_flags = old_flags;
737
738       if (error)
739         {
740           bfd_set_error (bfd_error_bad_value);
741           return FALSE;
742         }
743     }
744   return _bfd_sparc_elf_merge_private_bfd_data (ibfd, info);
745 }
746
747 /* MARCO: Set the correct entry size for the .stab section.  */
748
749 static bfd_boolean
750 elf64_sparc_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
751                            Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED,
752                            asection *sec)
753 {
754   const char *name;
755
756   name = bfd_get_section_name (abfd, sec);
757
758   if (strcmp (name, ".stab") == 0)
759     {
760       /* Even in the 64bit case the stab entries are only 12 bytes long.  */
761       elf_section_data (sec)->this_hdr.sh_entsize = 12;
762     }
763
764   return TRUE;
765 }
766 \f
767 /* Print a STT_REGISTER symbol to file FILE.  */
768
769 static const char *
770 elf64_sparc_print_symbol_all (bfd *abfd ATTRIBUTE_UNUSED, void * filep,
771                               asymbol *symbol)
772 {
773   FILE *file = (FILE *) filep;
774   int reg, type;
775
776   if (ELF_ST_TYPE (((elf_symbol_type *) symbol)->internal_elf_sym.st_info)
777       != STT_REGISTER)
778     return NULL;
779
780   reg = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
781   type = symbol->flags;
782   fprintf (file, "REG_%c%c%11s%c%c    R", "GOLI" [reg / 8], '0' + (reg & 7), "",
783                  ((type & BSF_LOCAL)
784                   ? (type & BSF_GLOBAL) ? '!' : 'l'
785                   : (type & BSF_GLOBAL) ? 'g' : ' '),
786                  (type & BSF_WEAK) ? 'w' : ' ');
787   if (symbol->name == NULL || symbol->name [0] == '\0')
788     return "#scratch";
789   else
790     return symbol->name;
791 }
792 \f
793 /* Used to decide how to sort relocs in an optimal manner for the
794    dynamic linker, before writing them out.  */
795
796 static enum elf_reloc_type_class
797 elf64_sparc_reloc_type_class (const struct bfd_link_info *info,
798                               const asection *rel_sec ATTRIBUTE_UNUSED,
799                               const Elf_Internal_Rela *rela)
800 {
801   bfd *abfd = info->output_bfd;
802   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
803   struct _bfd_sparc_elf_link_hash_table *htab
804     = _bfd_sparc_elf_hash_table (info);
805   BFD_ASSERT (htab != NULL);
806
807   if (htab->elf.dynsym != NULL
808       && htab->elf.dynsym->contents != NULL)
809     {
810       /* Check relocation against STT_GNU_IFUNC symbol if there are
811          dynamic symbols.  */
812       unsigned long r_symndx = htab->r_symndx (rela->r_info);
813       if (r_symndx != STN_UNDEF)
814         {
815           Elf_Internal_Sym sym;
816           if (!bed->s->swap_symbol_in (abfd,
817                                        (htab->elf.dynsym->contents
818                                         + r_symndx * bed->s->sizeof_sym),
819                                        0, &sym))
820             abort ();
821
822           if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
823             return reloc_class_ifunc;
824         }
825     }
826
827   switch ((int) ELF64_R_TYPE (rela->r_info))
828     {
829     case R_SPARC_IRELATIVE:
830       return reloc_class_ifunc;
831     case R_SPARC_RELATIVE:
832       return reloc_class_relative;
833     case R_SPARC_JMP_SLOT:
834       return reloc_class_plt;
835     case R_SPARC_COPY:
836       return reloc_class_copy;
837     default:
838       return reloc_class_normal;
839     }
840 }
841
842 /* Relocations in the 64 bit SPARC ELF ABI are more complex than in
843    standard ELF, because R_SPARC_OLO10 has secondary addend in
844    ELF64_R_TYPE_DATA field.  This structure is used to redirect the
845    relocation handling routines.  */
846
847 const struct elf_size_info elf64_sparc_size_info =
848 {
849   sizeof (Elf64_External_Ehdr),
850   sizeof (Elf64_External_Phdr),
851   sizeof (Elf64_External_Shdr),
852   sizeof (Elf64_External_Rel),
853   sizeof (Elf64_External_Rela),
854   sizeof (Elf64_External_Sym),
855   sizeof (Elf64_External_Dyn),
856   sizeof (Elf_External_Note),
857   4,            /* hash-table entry size.  */
858   /* Internal relocations per external relocations.
859      For link purposes we use just 1 internal per
860      1 external, for assembly and slurp symbol table
861      we use 2.  */
862   1,
863   64,           /* arch_size.  */
864   3,            /* log_file_align.  */
865   ELFCLASS64,
866   EV_CURRENT,
867   bfd_elf64_write_out_phdrs,
868   bfd_elf64_write_shdrs_and_ehdr,
869   bfd_elf64_checksum_contents,
870   elf64_sparc_write_relocs,
871   bfd_elf64_swap_symbol_in,
872   bfd_elf64_swap_symbol_out,
873   elf64_sparc_slurp_reloc_table,
874   bfd_elf64_slurp_symbol_table,
875   bfd_elf64_swap_dyn_in,
876   bfd_elf64_swap_dyn_out,
877   bfd_elf64_swap_reloc_in,
878   bfd_elf64_swap_reloc_out,
879   bfd_elf64_swap_reloca_in,
880   bfd_elf64_swap_reloca_out
881 };
882
883 #define TARGET_BIG_SYM  sparc_elf64_vec
884 #define TARGET_BIG_NAME "elf64-sparc"
885 #define ELF_ARCH        bfd_arch_sparc
886 #define ELF_MAXPAGESIZE 0x100000
887 #define ELF_COMMONPAGESIZE 0x2000
888
889 /* This is the official ABI value.  */
890 #define ELF_MACHINE_CODE EM_SPARCV9
891
892 /* This is the value that we used before the ABI was released.  */
893 #define ELF_MACHINE_ALT1 EM_OLD_SPARCV9
894
895 #define elf_backend_reloc_type_class \
896   elf64_sparc_reloc_type_class
897 #define bfd_elf64_get_reloc_upper_bound \
898   elf64_sparc_get_reloc_upper_bound
899 #define bfd_elf64_get_dynamic_reloc_upper_bound \
900   elf64_sparc_get_dynamic_reloc_upper_bound
901 #define bfd_elf64_canonicalize_reloc \
902   elf64_sparc_canonicalize_reloc
903 #define bfd_elf64_canonicalize_dynamic_reloc \
904   elf64_sparc_canonicalize_dynamic_reloc
905 #define bfd_elf64_set_reloc \
906   elf64_sparc_set_reloc
907 #define elf_backend_add_symbol_hook \
908   elf64_sparc_add_symbol_hook
909 #define elf_backend_get_symbol_type \
910   elf64_sparc_get_symbol_type
911 #define elf_backend_symbol_processing \
912   elf64_sparc_symbol_processing
913 #define elf_backend_print_symbol_all \
914   elf64_sparc_print_symbol_all
915 #define elf_backend_output_arch_syms \
916   elf64_sparc_output_arch_syms
917 #define bfd_elf64_bfd_merge_private_bfd_data \
918   elf64_sparc_merge_private_bfd_data
919 #define elf_backend_fake_sections \
920   elf64_sparc_fake_sections
921 #define elf_backend_size_info \
922   elf64_sparc_size_info
923
924 #define elf_backend_plt_sym_val \
925   _bfd_sparc_elf_plt_sym_val
926 #define bfd_elf64_bfd_link_hash_table_create \
927   _bfd_sparc_elf_link_hash_table_create
928 #define elf_info_to_howto \
929   _bfd_sparc_elf_info_to_howto
930 #define elf_backend_copy_indirect_symbol \
931   _bfd_sparc_elf_copy_indirect_symbol
932 #define bfd_elf64_bfd_reloc_type_lookup \
933   _bfd_sparc_elf_reloc_type_lookup
934 #define bfd_elf64_bfd_reloc_name_lookup \
935   _bfd_sparc_elf_reloc_name_lookup
936 #define bfd_elf64_bfd_relax_section \
937   _bfd_sparc_elf_relax_section
938 #define bfd_elf64_new_section_hook \
939   _bfd_sparc_elf_new_section_hook
940
941 #define elf_backend_create_dynamic_sections \
942   _bfd_sparc_elf_create_dynamic_sections
943 #define elf_backend_relocs_compatible \
944   _bfd_elf_relocs_compatible
945 #define elf_backend_check_relocs \
946   _bfd_sparc_elf_check_relocs
947 #define elf_backend_adjust_dynamic_symbol \
948   _bfd_sparc_elf_adjust_dynamic_symbol
949 #define elf_backend_omit_section_dynsym \
950   _bfd_sparc_elf_omit_section_dynsym
951 #define elf_backend_size_dynamic_sections \
952   _bfd_sparc_elf_size_dynamic_sections
953 #define elf_backend_relocate_section \
954   _bfd_sparc_elf_relocate_section
955 #define elf_backend_finish_dynamic_symbol \
956   _bfd_sparc_elf_finish_dynamic_symbol
957 #define elf_backend_finish_dynamic_sections \
958   _bfd_sparc_elf_finish_dynamic_sections
959 #define elf_backend_fixup_symbol \
960   _bfd_sparc_elf_fixup_symbol
961
962 #define bfd_elf64_mkobject \
963   _bfd_sparc_elf_mkobject
964 #define elf_backend_object_p \
965   _bfd_sparc_elf_object_p
966 #define elf_backend_gc_mark_hook \
967   _bfd_sparc_elf_gc_mark_hook
968 #define elf_backend_init_index_section \
969   _bfd_elf_init_1_index_section
970
971 #define elf_backend_can_gc_sections 1
972 #define elf_backend_can_refcount 1
973 #define elf_backend_want_got_plt 0
974 #define elf_backend_plt_readonly 0
975 #define elf_backend_want_plt_sym 1
976 #define elf_backend_got_header_size 8
977 #define elf_backend_want_dynrelro 1
978 #define elf_backend_rela_normal 1
979
980 /* Section 5.2.4 of the ABI specifies a 256-byte boundary for the table.  */
981 #define elf_backend_plt_alignment 8
982
983 #include "elf64-target.h"
984
985 /* FreeBSD support */
986 #undef  TARGET_BIG_SYM
987 #define TARGET_BIG_SYM sparc_elf64_fbsd_vec
988 #undef  TARGET_BIG_NAME
989 #define TARGET_BIG_NAME "elf64-sparc-freebsd"
990 #undef  ELF_OSABI
991 #define ELF_OSABI ELFOSABI_FREEBSD
992
993 #undef  elf64_bed
994 #define elf64_bed                               elf64_sparc_fbsd_bed
995
996 #include "elf64-target.h"
997
998 /* Solaris 2.  */
999
1000 #undef  TARGET_BIG_SYM
1001 #define TARGET_BIG_SYM                          sparc_elf64_sol2_vec
1002 #undef  TARGET_BIG_NAME
1003 #define TARGET_BIG_NAME                         "elf64-sparc-sol2"
1004
1005 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
1006    objects won't be recognized.  */
1007 #undef  ELF_OSABI
1008
1009 #undef elf64_bed
1010 #define elf64_bed                               elf64_sparc_sol2_bed
1011
1012 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
1013    boundary.  */
1014 #undef elf_backend_static_tls_alignment
1015 #define elf_backend_static_tls_alignment        16
1016
1017 #include "elf64-target.h"