Fix RELOC_FOR_GLOBAL_SYMBOLS macro so that it can cope with user defined symbols...
[binutils-gdb.git] / bfd / elf32-arc.c
blobbd182996654ce4df8b2c1c925198fc1d4a04c7bc
1 /* ARC-specific support for 32-bit ELF
2 Copyright (C) 1994-2024 Free Software Foundation, Inc.
3 Contributed by Cupertino Miranda (cmiranda@synopsys.com).
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/arc.h"
27 #include "libiberty.h"
28 #include "opcode/arc-func.h"
29 #include "opcode/arc.h"
30 #include "arc-plt.h"
32 #define FEATURE_LIST_NAME bfd_feature_list
33 #define CONFLICT_LIST bfd_conflict_list
34 #include "opcode/arc-attrs.h"
36 /* #define ARC_ENABLE_DEBUG 1 */
37 #ifdef ARC_ENABLE_DEBUG
38 static const char *
39 name_for_global_symbol (struct elf_link_hash_entry *h)
41 static char *local_str = "(local)";
42 if (h == NULL)
43 return local_str;
44 return h->root.root.string;
46 #define ARC_DEBUG(fmt, args...) fprintf (stderr, fmt, ##args)
47 #else
48 #define ARC_DEBUG(...)
49 #endif
52 #define ADD_RELA(BFD, SECTION, OFFSET, SYM_IDX, TYPE, ADDEND) \
53 { \
54 struct elf_link_hash_table *_htab = elf_hash_table (info); \
55 Elf_Internal_Rela _rel; \
56 bfd_byte * _loc; \
58 if (_htab->dynamic_sections_created) \
59 { \
60 BFD_ASSERT (_htab->srel##SECTION &&_htab->srel##SECTION->contents); \
61 _loc = _htab->srel##SECTION->contents \
62 + ((_htab->srel##SECTION->reloc_count) \
63 * sizeof (Elf32_External_Rela)); \
64 _htab->srel##SECTION->reloc_count++; \
65 _rel.r_addend = ADDEND; \
66 _rel.r_offset = (_htab->s##SECTION)->output_section->vma \
67 + (_htab->s##SECTION)->output_offset + OFFSET; \
68 BFD_ASSERT ((long) SYM_IDX != -1); \
69 _rel.r_info = ELF32_R_INFO (SYM_IDX, TYPE); \
70 bfd_elf32_swap_reloca_out (BFD, &_rel, _loc); \
71 } \
74 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
75 case VALUE: \
76 return "R_" #TYPE; \
77 break;
79 static ATTRIBUTE_UNUSED const char *
80 reloc_type_to_name (unsigned int type)
82 switch (type)
84 #include "elf/arc-reloc.def"
86 default:
87 return "UNKNOWN";
88 break;
92 #undef ARC_RELOC_HOWTO
94 /* Try to minimize the amount of space occupied by relocation tables
95 on the ROM (not that the ROM won't be swamped by other ELF overhead). */
97 #define USE_REL 1
99 /* Similar with bfd_get_32 but taking into account the
100 middle-endianess of the ARC CPUs. Only to be used in code
101 sections. */
103 static bfd_vma
104 bfd_get_32_me (bfd * abfd,const unsigned char * data)
106 bfd_vma value = 0;
108 if (bfd_big_endian (abfd))
109 value = bfd_get_32 (abfd, data);
110 else
112 value = ((bfd_get_8 (abfd, data) & 255) << 16);
113 value |= ((bfd_get_8 (abfd, data + 1) & 255) << 24);
114 value |= (bfd_get_8 (abfd, data + 2) & 255);
115 value |= ((bfd_get_8 (abfd, data + 3) & 255) << 8);
118 return value;
121 static void
122 bfd_put_32_me (bfd *abfd, bfd_vma value,unsigned char *data)
124 bfd_put_16 (abfd, (value & 0xffff0000) >> 16, data);
125 bfd_put_16 (abfd, value & 0xffff, data + 2);
128 static ATTRIBUTE_UNUSED bool
129 is_reloc_PC_relative (reloc_howto_type *howto)
131 return strstr (howto->name, "PC") != NULL;
134 static bool
135 is_reloc_SDA_relative (reloc_howto_type *howto)
137 return strstr (howto->name, "SDA") != NULL;
140 static bool
141 is_reloc_for_GOT (reloc_howto_type * howto)
143 if (strstr (howto->name, "TLS") != NULL)
144 return false;
145 return strstr (howto->name, "GOT") != NULL;
148 static bool
149 is_reloc_for_PLT (reloc_howto_type * howto)
151 return strstr (howto->name, "PLT") != NULL;
154 static bool
155 is_reloc_for_TLS (reloc_howto_type *howto)
157 return strstr (howto->name, "TLS") != NULL;
160 struct arc_relocation_data
162 bfd_signed_vma reloc_offset;
163 bfd_signed_vma reloc_addend;
164 bfd_signed_vma got_offset_value;
166 bfd_signed_vma sym_value;
167 asection *sym_section;
169 reloc_howto_type *howto;
171 asection *input_section;
173 bfd_signed_vma sdata_begin_symbol_vma;
174 bool sdata_begin_symbol_vma_set;
175 bfd_signed_vma got_symbol_vma;
177 bool should_relocate;
179 const char *symbol_name;
182 /* ARC ELF linker hash entry. */
183 struct elf_arc_link_hash_entry
185 struct elf_link_hash_entry root;
187 struct got_entry *got_ents;
191 /* Should be included at this location due to static declarations
192 defined before this point. */
193 #include "arc-got.h"
195 #define arc_bfd_get_8(A,B,C) bfd_get_8(A,B)
196 #define arc_bfd_get_16(A,B,C) bfd_get_16(A,B)
197 #define arc_bfd_get_32(A,B,C) bfd_get_32(A,B)
198 #define arc_bfd_put_8(A,B,C,D) bfd_put_8(A,B,C)
199 #define arc_bfd_put_16(A,B,C,D) bfd_put_16(A,B,C)
200 #define arc_bfd_put_32(A,B,C,D) bfd_put_32(A,B,C)
203 static bfd_reloc_status_type
204 arc_elf_reloc (bfd *abfd ATTRIBUTE_UNUSED,
205 arelent *reloc_entry,
206 asymbol *symbol_in,
207 void *data ATTRIBUTE_UNUSED,
208 asection *input_section,
209 bfd *output_bfd,
210 char ** error_message ATTRIBUTE_UNUSED)
212 if (output_bfd != NULL)
214 reloc_entry->address += input_section->output_offset;
216 /* In case of relocateable link and if the reloc is against a
217 section symbol, the addend needs to be adjusted according to
218 where the section symbol winds up in the output section. */
219 if ((symbol_in->flags & BSF_SECTION_SYM) && symbol_in->section)
220 reloc_entry->addend += symbol_in->section->output_offset;
222 return bfd_reloc_ok;
225 return bfd_reloc_continue;
229 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
230 TYPE = VALUE,
232 enum howto_list
234 #include "elf/arc-reloc.def"
235 HOWTO_LIST_LAST
238 #undef ARC_RELOC_HOWTO
240 #define ARC_RELOC_HOWTO(TYPE, VALUE, RSIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
241 [TYPE] = HOWTO (R_##TYPE, 0, RSIZE, BITSIZE, false, 0, \
242 complain_overflow_##OVERFLOW, arc_elf_reloc, \
243 "R_" #TYPE, false, 0, 0, false),
245 static struct reloc_howto_struct elf_arc_howto_table[] =
247 #include "elf/arc-reloc.def"
248 /* Example of what is generated by the preprocessor. Currently kept as an
249 example.
250 HOWTO (R_ARC_NONE, // Type.
251 0, // Rightshift.
252 4, // Size.
253 32, // Bitsize.
254 false, // PC_relative.
255 0, // Bitpos.
256 complain_overflow_bitfield, // Complain_on_overflow.
257 bfd_elf_generic_reloc, // Special_function.
258 "R_ARC_NONE", // Name.
259 true, // Partial_inplace.
260 0, // Src_mask.
261 0, // Dst_mask.
262 false), // PCrel_offset.
265 #undef ARC_RELOC_HOWTO
267 static void
268 arc_elf_howto_init (void)
270 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
271 elf_arc_howto_table[TYPE].pc_relative = \
272 (strstr (#FORMULA, " P ") != NULL || strstr (#FORMULA, " PDATA ") != NULL); \
273 elf_arc_howto_table[TYPE].dst_mask = RELOC_FUNCTION(0, ~0); \
274 /* Only 32 bit data relocations should be marked as ME. */ \
275 if (strstr (#FORMULA, " ME ") != NULL) \
277 BFD_ASSERT (SIZE == 4); \
280 #include "elf/arc-reloc.def"
283 #undef ARC_RELOC_HOWTO
286 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
287 [TYPE] = VALUE,
289 const int howto_table_lookup[] =
291 #include "elf/arc-reloc.def"
294 #undef ARC_RELOC_HOWTO
296 static reloc_howto_type *
297 arc_elf_howto (unsigned int r_type)
299 if (elf_arc_howto_table[R_ARC_32].dst_mask == 0)
300 arc_elf_howto_init ();
301 return &elf_arc_howto_table[r_type];
304 /* Map BFD reloc types to ARC ELF reloc types. */
306 struct arc_reloc_map
308 bfd_reloc_code_real_type bfd_reloc_val;
309 unsigned char elf_reloc_val;
312 /* ARC ELF linker hash table. */
313 struct elf_arc_link_hash_table
315 struct elf_link_hash_table elf;
318 static struct bfd_hash_entry *
319 elf_arc_link_hash_newfunc (struct bfd_hash_entry *entry,
320 struct bfd_hash_table *table,
321 const char *string)
323 struct elf_arc_link_hash_entry * ret =
324 (struct elf_arc_link_hash_entry *) entry;
326 /* Allocate the structure if it has not already been allocated by a
327 subclass. */
328 if (ret == NULL)
329 ret = (struct elf_arc_link_hash_entry *)
330 bfd_hash_allocate (table, sizeof (struct elf_arc_link_hash_entry));
331 if (ret == NULL)
332 return (struct bfd_hash_entry *) ret;
334 /* Call the allocation method of the superclass. */
335 ret = ((struct elf_arc_link_hash_entry *)
336 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
337 table, string));
338 if (ret != NULL)
340 ret->got_ents = NULL;
343 return (struct bfd_hash_entry *) ret;
346 /* Destroy an ARC ELF linker hash table. */
347 static void
348 elf_arc_link_hash_table_free (bfd *obfd)
350 _bfd_elf_link_hash_table_free (obfd);
353 /* Create an ARC ELF linker hash table. */
355 static struct bfd_link_hash_table *
356 arc_elf_link_hash_table_create (bfd *abfd)
358 struct elf_arc_link_hash_table *ret;
360 ret = (struct elf_arc_link_hash_table *) bfd_zmalloc (sizeof (*ret));
361 if (ret == NULL)
362 return NULL;
364 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
365 elf_arc_link_hash_newfunc,
366 sizeof (struct elf_arc_link_hash_entry),
367 ARC_ELF_DATA))
369 free (ret);
370 return NULL;
373 ret->elf.root.hash_table_free = elf_arc_link_hash_table_free;
375 return &ret->elf.root;
378 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
379 { BFD_RELOC_##TYPE, R_##TYPE },
381 static const struct arc_reloc_map arc_reloc_map[] =
383 #include "elf/arc-reloc.def"
385 {BFD_RELOC_NONE, R_ARC_NONE},
386 {BFD_RELOC_8, R_ARC_8},
387 {BFD_RELOC_16, R_ARC_16},
388 {BFD_RELOC_24, R_ARC_24},
389 {BFD_RELOC_32, R_ARC_32},
392 #undef ARC_RELOC_HOWTO
394 typedef ATTRIBUTE_UNUSED unsigned (*replace_func) (unsigned, int ATTRIBUTE_UNUSED);
396 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
397 case TYPE: \
398 func = RELOC_FUNCTION; \
399 break;
401 static replace_func
402 get_replace_function (bfd *abfd, unsigned int r_type)
404 replace_func func = NULL;
406 switch (r_type)
408 #include "elf/arc-reloc.def"
411 if (func == replace_bits24 && bfd_big_endian (abfd))
412 func = replace_bits24_be;
414 return func;
416 #undef ARC_RELOC_HOWTO
418 static reloc_howto_type *
419 arc_elf32_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
420 bfd_reloc_code_real_type code)
422 unsigned int i;
424 for (i = ARRAY_SIZE (arc_reloc_map); i--;)
426 if (arc_reloc_map[i].bfd_reloc_val == code)
427 return arc_elf_howto (arc_reloc_map[i].elf_reloc_val);
430 return NULL;
433 /* Function to set the ELF flag bits. */
434 static bool
435 arc_elf_set_private_flags (bfd *abfd, flagword flags)
437 elf_elfheader (abfd)->e_flags = flags;
438 elf_flags_init (abfd) = true;
439 return true;
442 /* Print private flags. */
443 static bool
444 arc_elf_print_private_bfd_data (bfd *abfd, void * ptr)
446 FILE *file = (FILE *) ptr;
447 flagword flags;
449 BFD_ASSERT (abfd != NULL && ptr != NULL);
451 /* Print normal ELF private data. */
452 _bfd_elf_print_private_bfd_data (abfd, ptr);
454 flags = elf_elfheader (abfd)->e_flags;
455 fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
457 switch (flags & EF_ARC_MACH_MSK)
459 case EF_ARC_CPU_ARCV2HS : fprintf (file, " -mcpu=ARCv2HS"); break;
460 case EF_ARC_CPU_ARCV2EM : fprintf (file, " -mcpu=ARCv2EM"); break;
461 case E_ARC_MACH_ARC600 : fprintf (file, " -mcpu=ARC600"); break;
462 case E_ARC_MACH_ARC601 : fprintf (file, " -mcpu=ARC601"); break;
463 case E_ARC_MACH_ARC700 : fprintf (file, " -mcpu=ARC700"); break;
464 default:
465 fprintf (file, "-mcpu=unknown");
466 break;
469 switch (flags & EF_ARC_OSABI_MSK)
471 case E_ARC_OSABI_ORIG : fprintf (file, " (ABI:legacy)"); break;
472 case E_ARC_OSABI_V2 : fprintf (file, " (ABI:v2)"); break;
473 case E_ARC_OSABI_V3 : fprintf (file, " (ABI:v3)"); break;
474 case E_ARC_OSABI_V4 : fprintf (file, " (ABI:v4)"); break;
475 default:
476 fprintf (file, " (ABI:unknown)");
477 break;
480 fputc ('\n', file);
481 return true;
484 /* Copy backend specific data from one object module to another. */
486 static bool
487 arc_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
489 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
490 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
491 return true;
493 BFD_ASSERT (!elf_flags_init (obfd)
494 || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
496 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
497 elf_flags_init (obfd) = true;
499 /* Copy object attributes. */
500 _bfd_elf_copy_obj_attributes (ibfd, obfd);
502 return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
505 static reloc_howto_type *
506 bfd_elf32_bfd_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED,
507 const char *r_name)
509 unsigned int i;
511 for (i = 0; i < ARRAY_SIZE (elf_arc_howto_table); i++)
512 if (elf_arc_howto_table[i].name != NULL
513 && strcasecmp (elf_arc_howto_table[i].name, r_name) == 0)
514 return arc_elf_howto (i);
516 return NULL;
519 /* Set the howto pointer for an ARC ELF reloc. */
521 static bool
522 arc_info_to_howto_rel (bfd * abfd,
523 arelent * cache_ptr,
524 Elf_Internal_Rela * dst)
526 unsigned int r_type;
528 r_type = ELF32_R_TYPE (dst->r_info);
529 if (r_type >= (unsigned int) R_ARC_max)
531 /* xgettext:c-format */
532 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
533 abfd, r_type);
534 bfd_set_error (bfd_error_bad_value);
535 return false;
538 cache_ptr->howto = arc_elf_howto (r_type);
539 return true;
542 /* Extract CPU features from an NTBS. */
544 static unsigned
545 arc_extract_features (const char *p)
547 unsigned i, r = 0;
549 if (!p)
550 return 0;
552 for (i = 0; i < ARRAY_SIZE (bfd_feature_list); i++)
554 char *t = strstr (p, bfd_feature_list[i].attr);
555 unsigned l = strlen (bfd_feature_list[i].attr);
556 if ((t != NULL)
557 && (t[l] == ','
558 || t[l] == '\0'))
559 r |= bfd_feature_list[i].feature;
562 return r;
565 /* Concatenate two strings. s1 can be NULL but not
566 s2. */
568 static char *
569 arc_stralloc (char * s1, const char * s2)
571 char *p;
573 /* Only s1 can be null. */
574 BFD_ASSERT (s2);
576 p = s1 ? concat (s1, ",", s2, NULL) : (char *)s2;
578 return p;
581 /* Merge ARC object attributes from IBFD into OBFD. Raise an error if
582 there are conflicting attributes. */
584 static bool
585 arc_elf_merge_attributes (bfd *ibfd, struct bfd_link_info *info)
587 bfd *obfd = info->output_bfd;
588 obj_attribute *in_attr;
589 obj_attribute *out_attr;
590 int i;
591 bool result = true;
592 const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
593 char *tagname = NULL;
595 /* Skip the linker stubs file. This preserves previous behavior
596 of accepting unknown attributes in the first input file - but
597 is that a bug? */
598 if (ibfd->flags & BFD_LINKER_CREATED)
599 return true;
601 /* Skip any input that hasn't attribute section.
602 This enables to link object files without attribute section with
603 any others. */
604 if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
605 return true;
607 if (!elf_known_obj_attributes_proc (obfd)[0].i)
609 /* This is the first object. Copy the attributes. */
610 _bfd_elf_copy_obj_attributes (ibfd, obfd);
612 out_attr = elf_known_obj_attributes_proc (obfd);
614 /* Use the Tag_null value to indicate the attributes have been
615 initialized. */
616 out_attr[0].i = 1;
618 return true;
621 in_attr = elf_known_obj_attributes_proc (ibfd);
622 out_attr = elf_known_obj_attributes_proc (obfd);
624 for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
626 /* Merge this attribute with existing attributes. */
627 switch (i)
629 case Tag_ARC_PCS_config:
630 if (out_attr[i].i == 0)
631 out_attr[i].i = in_attr[i].i;
632 else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i)
634 const char *tagval[] = { "Absent", "Bare-metal/mwdt",
635 "Bare-metal/newlib", "Linux/uclibc",
636 "Linux/glibc" };
637 BFD_ASSERT (in_attr[i].i < 5);
638 BFD_ASSERT (out_attr[i].i < 5);
639 /* It's sometimes ok to mix different configs, so this is only
640 a warning. */
641 _bfd_error_handler
642 (_("warning: %pB: conflicting platform configuration "
643 "%s with %s"), ibfd,
644 tagval[in_attr[i].i],
645 tagval[out_attr[i].i]);
647 break;
649 case Tag_ARC_CPU_base:
650 if (out_attr[i].i == 0)
651 out_attr[i].i = in_attr[i].i;
652 else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i
653 && ((out_attr[i].i + in_attr[i].i) < 6))
655 const char *tagval[] = { "Absent", "ARC6xx", "ARC7xx",
656 "ARCEM", "ARCHS" };
657 BFD_ASSERT (in_attr[i].i < 5);
658 BFD_ASSERT (out_attr[i].i < 5);
659 /* We cannot mix code for different CPUs. */
660 _bfd_error_handler
661 (_("error: %pB: unable to merge CPU base attributes "
662 "%s with %s"),
663 obfd,
664 tagval[in_attr[i].i],
665 tagval[out_attr[i].i]);
666 result = false;
667 break;
669 else
671 /* The CPUs may be different, check if we can still mix
672 the objects against the output choosen CPU. */
673 unsigned in_feature = 0;
674 unsigned out_feature = 0;
675 char *p1 = in_attr[Tag_ARC_ISA_config].s;
676 char *p2 = out_attr[Tag_ARC_ISA_config].s;
677 unsigned j;
678 unsigned cpu_out;
679 unsigned opcode_map[] = {0, ARC_OPCODE_ARC600, ARC_OPCODE_ARC700,
680 ARC_OPCODE_ARCv2EM, ARC_OPCODE_ARCv2HS};
682 BFD_ASSERT (in_attr[i].i < (sizeof (opcode_map)
683 / sizeof (unsigned)));
684 BFD_ASSERT (out_attr[i].i < (sizeof (opcode_map)
685 / sizeof (unsigned)));
686 cpu_out = opcode_map[out_attr[i].i];
688 in_feature = arc_extract_features (p1);
689 out_feature = arc_extract_features (p2);
691 /* First, check if a feature is compatible with the
692 output object chosen CPU. */
693 for (j = 0; j < ARRAY_SIZE (bfd_feature_list); j++)
694 if (((in_feature | out_feature) & bfd_feature_list[j].feature)
695 && (!(cpu_out & bfd_feature_list[j].cpus)))
697 _bfd_error_handler
698 (_("error: %pB: unable to merge ISA extension attributes "
699 "%s"),
700 obfd, bfd_feature_list[j].name);
701 result = false;
702 break;
704 /* Second, if we have compatible features with the
705 chosen CPU, check if they are compatible among
706 them. */
707 for (j = 0; j < ARRAY_SIZE (bfd_conflict_list); j++)
708 if (((in_feature | out_feature) & bfd_conflict_list[j])
709 == bfd_conflict_list[j])
711 unsigned k;
712 for (k = 0; k < ARRAY_SIZE (bfd_feature_list); k++)
714 if (in_feature & bfd_feature_list[k].feature
715 & bfd_conflict_list[j])
716 p1 = (char *) bfd_feature_list[k].name;
717 if (out_feature & bfd_feature_list[k].feature
718 & bfd_conflict_list[j])
719 p2 = (char *) bfd_feature_list[k].name;
721 _bfd_error_handler
722 (_("error: %pB: conflicting ISA extension attributes "
723 "%s with %s"),
724 obfd, p1, p2);
725 result = false;
726 break;
728 /* Everithing is alright. */
729 out_feature |= in_feature;
730 p1 = NULL;
731 for (j = 0; j < ARRAY_SIZE (bfd_feature_list); j++)
732 if (out_feature & bfd_feature_list[j].feature)
733 p1 = arc_stralloc (p1, bfd_feature_list[j].attr);
734 if (p1)
735 out_attr[Tag_ARC_ISA_config].s =
736 _bfd_elf_attr_strdup (obfd, p1);
738 /* Fall through. */
739 case Tag_ARC_CPU_variation:
740 case Tag_ARC_ISA_mpy_option:
741 case Tag_ARC_ABI_osver:
742 /* Use the largest value specified. */
743 if (in_attr[i].i > out_attr[i].i)
744 out_attr[i].i = in_attr[i].i;
745 break;
747 /* The CPU name is given by the vendor, just choose an
748 existing one if missing or different. There are no fail
749 criteria if they different or both missing. */
750 case Tag_ARC_CPU_name:
751 if (!out_attr[i].s && in_attr[i].s)
752 out_attr[i].s = _bfd_elf_attr_strdup (obfd, in_attr[i].s);
753 break;
755 case Tag_ARC_ABI_rf16:
756 if (out_attr[i].i == 0)
757 out_attr[i].i = in_attr[i].i;
758 else if (out_attr[i].i != in_attr[i].i)
760 /* We cannot mix code with rf16 and without. */
761 _bfd_error_handler
762 (_("error: %pB: cannot mix rf16 with full register set %pB"),
763 obfd, ibfd);
764 result = false;
766 break;
768 case Tag_ARC_ABI_pic:
769 tagname = "PIC";
770 /* fall through */
771 case Tag_ARC_ABI_sda:
772 if (!tagname)
773 tagname = "SDA";
774 /* fall through */
775 case Tag_ARC_ABI_tls:
777 const char *tagval[] = { "Absent", "MWDT", "GNU" };
779 if (!tagname)
780 tagname = "TLS";
782 BFD_ASSERT (in_attr[i].i < 3);
783 BFD_ASSERT (out_attr[i].i < 3);
784 if (out_attr[i].i == 0)
785 out_attr[i].i = in_attr[i].i;
786 else if (out_attr[i].i != 0 && in_attr[i].i != 0
787 && out_attr[i].i != in_attr[i].i)
789 _bfd_error_handler
790 (_("error: %pB: conflicting attributes %s: %s with %s"),
791 obfd, tagname,
792 tagval[in_attr[i].i],
793 tagval[out_attr[i].i]);
794 result = false;
796 tagname = NULL;
797 break;
800 case Tag_ARC_ABI_double_size:
801 tagname = "Double size";
802 /* fall through */
803 case Tag_ARC_ABI_enumsize:
804 if (!tagname)
805 tagname = "Enum size";
806 /* fall through */
807 case Tag_ARC_ABI_exceptions:
808 if (!tagname)
809 tagname = "ABI exceptions";
811 if (out_attr[i].i == 0)
812 out_attr[i].i = in_attr[i].i;
813 else if (out_attr[i].i != 0 && in_attr[i].i != 0
814 && out_attr[i].i != in_attr[i].i)
816 _bfd_error_handler
817 (_("error: %pB: conflicting attributes %s"),
818 obfd, tagname);
819 result = false;
821 break;
823 case Tag_ARC_ISA_apex:
824 break; /* Do nothing for APEX attributes. */
826 case Tag_ARC_ISA_config:
827 /* It is handled in Tag_ARC_CPU_base. */
828 break;
830 case Tag_ARC_ATR_version:
831 if (out_attr[i].i == 0)
832 out_attr[i].i = in_attr[i].i;
833 break;
835 default:
836 result
837 = result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
840 /* If out_attr was copied from in_attr then it won't have a type yet. */
841 if (in_attr[i].type && !out_attr[i].type)
842 out_attr[i].type = in_attr[i].type;
845 /* Merge Tag_compatibility attributes and any common GNU ones. */
846 if (!_bfd_elf_merge_object_attributes (ibfd, info))
847 return false;
849 /* Check for any attributes not known on ARC. */
850 result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
852 return result;
855 /* Merge backend specific data from an object file to the output
856 object file when linking. */
858 static bool
859 arc_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
861 bfd *obfd = info->output_bfd;
862 unsigned short mach_ibfd;
863 static unsigned short mach_obfd = EM_NONE;
864 flagword out_flags;
865 flagword in_flags;
866 asection *sec;
868 /* Check if we have the same endianess. */
869 if (! _bfd_generic_verify_endian_match (ibfd, info))
870 return false;
872 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
873 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
874 return true;
876 /* Collect ELF flags. */
877 in_flags = elf_elfheader (ibfd)->e_flags & EF_ARC_MACH_MSK;
878 out_flags = elf_elfheader (obfd)->e_flags & EF_ARC_MACH_MSK;
880 if (!elf_flags_init (obfd)) /* First call, no flags set. */
882 elf_flags_init (obfd) = true;
883 out_flags = in_flags;
886 if (!arc_elf_merge_attributes (ibfd, info))
887 return false;
889 /* Check to see if the input BFD actually contains any sections. Do
890 not short-circuit dynamic objects; their section list may be
891 emptied by elf_link_add_object_symbols. */
892 if (!(ibfd->flags & DYNAMIC))
894 bool null_input_bfd = true;
895 bool only_data_sections = true;
897 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
899 if ((bfd_section_flags (sec)
900 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
901 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
902 only_data_sections = false;
904 null_input_bfd = false;
907 if (null_input_bfd || only_data_sections)
908 return true;
911 /* Complain about various flag/architecture mismatches. */
912 mach_ibfd = elf_elfheader (ibfd)->e_machine;
913 if (mach_obfd == EM_NONE)
915 mach_obfd = mach_ibfd;
917 else
919 if (mach_ibfd != mach_obfd)
921 /* xgettext:c-format */
922 _bfd_error_handler (_("error: attempting to link %pB "
923 "with a binary %pB of different architecture"),
924 ibfd, obfd);
925 return false;
927 else if ((in_flags != out_flags)
928 /* If we have object attributes, then we already
929 checked the objects compatibility, skip it. */
930 && !bfd_elf_get_obj_attr_int (ibfd, OBJ_ATTR_PROC,
931 Tag_ARC_CPU_base))
933 if (in_flags && out_flags)
935 /* Warn if different flags. */
936 _bfd_error_handler
937 /* xgettext:c-format */
938 (_("%pB: uses different e_flags (%#x) fields than "
939 "previous modules (%#x)"),
940 ibfd, in_flags, out_flags);
941 return false;
943 /* MWDT doesnt set the eflags hence make sure we choose the
944 eflags set by gcc. */
945 in_flags = in_flags > out_flags ? in_flags : out_flags;
947 else
949 /* Everything is correct; don't change the output flags. */
950 in_flags = out_flags;
954 /* Update the flags. */
955 elf_elfheader (obfd)->e_flags = in_flags;
957 if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
959 return bfd_set_arch_mach (obfd, bfd_arch_arc, bfd_get_mach (ibfd));
962 return true;
965 /* Return a best guess for the machine number based on the attributes. */
967 static unsigned int
968 bfd_arc_get_mach_from_attributes (bfd * abfd)
970 int arch = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_ARC_CPU_base);
971 unsigned e_machine = elf_elfheader (abfd)->e_machine;
973 switch (arch)
975 case TAG_CPU_ARC6xx:
976 return bfd_mach_arc_arc600;
977 case TAG_CPU_ARC7xx:
978 return bfd_mach_arc_arc700;
979 case TAG_CPU_ARCEM:
980 case TAG_CPU_ARCHS:
981 return bfd_mach_arc_arcv2;
982 default:
983 break;
985 return (e_machine == EM_ARC_COMPACT)
986 ? bfd_mach_arc_arc700 : bfd_mach_arc_arcv2;
989 /* Set the right machine number for an ARC ELF file. */
990 static bool
991 arc_elf_object_p (bfd * abfd)
993 /* Make sure this is initialised, or you'll have the potential of passing
994 garbage---or misleading values---into the call to
995 bfd_default_set_arch_mach (). */
996 unsigned int mach = bfd_mach_arc_arc700;
997 unsigned long arch = elf_elfheader (abfd)->e_flags & EF_ARC_MACH_MSK;
998 unsigned e_machine = elf_elfheader (abfd)->e_machine;
1000 if (e_machine == EM_ARC_COMPACT || e_machine == EM_ARC_COMPACT2)
1002 switch (arch)
1004 case E_ARC_MACH_ARC600:
1005 mach = bfd_mach_arc_arc600;
1006 break;
1007 case E_ARC_MACH_ARC601:
1008 mach = bfd_mach_arc_arc601;
1009 break;
1010 case E_ARC_MACH_ARC700:
1011 mach = bfd_mach_arc_arc700;
1012 break;
1013 case EF_ARC_CPU_ARCV2HS:
1014 case EF_ARC_CPU_ARCV2EM:
1015 mach = bfd_mach_arc_arcv2;
1016 break;
1017 default:
1018 mach = bfd_arc_get_mach_from_attributes (abfd);
1019 break;
1022 else
1024 if (e_machine == EM_ARC)
1026 _bfd_error_handler
1027 (_("error: the ARC4 architecture is no longer supported"));
1028 return false;
1030 else
1032 _bfd_error_handler
1033 (_("warning: unset or old architecture flags; "
1034 "use default machine"));
1038 return bfd_default_set_arch_mach (abfd, bfd_arch_arc, mach);
1041 /* The final processing done just before writing out an ARC ELF object file.
1042 This gets the ARC architecture right based on the machine number. */
1044 static bool
1045 arc_elf_final_write_processing (bfd *abfd)
1047 unsigned long emf;
1049 switch (bfd_get_mach (abfd))
1051 case bfd_mach_arc_arcv2:
1052 emf = EM_ARC_COMPACT2;
1053 break;
1054 default:
1055 emf = EM_ARC_COMPACT;
1056 break;
1059 elf_elfheader (abfd)->e_machine = emf;
1061 /* Record whatever is the current syscall ABI version. */
1062 int osver = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC,
1063 Tag_ARC_ABI_osver);
1064 flagword e_flags = elf_elfheader (abfd)->e_flags;
1065 if (osver)
1066 e_flags = (e_flags & ~EF_ARC_OSABI_MSK) | ((osver & 0x0f) << 8);
1067 else if ((e_flags & EF_ARC_OSABI_MSK) == 0)
1068 e_flags |= E_ARC_OSABI_V3;
1070 elf_elfheader (abfd)->e_flags = e_flags;
1071 return _bfd_elf_final_write_processing (abfd);
1074 #ifdef ARC_ENABLE_DEBUG
1075 #define DEBUG_ARC_RELOC(A) debug_arc_reloc (A)
1077 static void
1078 debug_arc_reloc (struct arc_relocation_data reloc_data)
1080 ARC_DEBUG ("Reloc type=%s, should_relocate = %s\n",
1081 reloc_data.howto->name,
1082 reloc_data.should_relocate ? "true" : "false");
1083 ARC_DEBUG (" offset = 0x%x, addend = 0x%x\n",
1084 (unsigned int) reloc_data.reloc_offset,
1085 (unsigned int) reloc_data.reloc_addend);
1086 ARC_DEBUG (" Symbol:\n");
1087 ARC_DEBUG (" value = 0x%08x\n",
1088 (unsigned int) reloc_data.sym_value);
1089 if (reloc_data.sym_section != NULL)
1091 ARC_DEBUG (" Symbol Section:\n");
1092 ARC_DEBUG (" section name = %s, output_offset 0x%08x",
1093 reloc_data.sym_section->name,
1094 (unsigned int) reloc_data.sym_section->output_offset);
1095 if (reloc_data.sym_section->output_section != NULL)
1096 ARC_DEBUG (", output_section->vma = 0x%08x",
1097 ((unsigned int) reloc_data.sym_section->output_section->vma));
1098 ARC_DEBUG ("\n");
1099 if (reloc_data.sym_section->owner
1100 && reloc_data.sym_section->owner->filename)
1101 ARC_DEBUG (" file: %s\n", reloc_data.sym_section->owner->filename);
1103 else
1105 ARC_DEBUG (" symbol section is NULL\n");
1108 ARC_DEBUG (" Input_section:\n");
1109 if (reloc_data.input_section != NULL)
1111 ARC_DEBUG (" section name = %s, output_offset 0x%08x, output_section->vma = 0x%08x\n",
1112 reloc_data.input_section->name,
1113 (unsigned int) reloc_data.input_section->output_offset,
1114 (unsigned int) reloc_data.input_section->output_section->vma);
1115 ARC_DEBUG (" changed_address = 0x%08x\n",
1116 (unsigned int) (reloc_data.input_section->output_section->vma
1117 + reloc_data.input_section->output_offset
1118 + reloc_data.reloc_offset));
1119 ARC_DEBUG (" file: %s\n", reloc_data.input_section->owner->filename);
1121 else
1123 ARC_DEBUG (" input section is NULL\n");
1126 #else
1127 #define DEBUG_ARC_RELOC(A)
1128 #endif /* ARC_ENABLE_DEBUG */
1130 static bfd_vma
1131 middle_endian_convert (bfd_vma insn, bool do_it)
1133 if (do_it)
1135 insn
1136 = ((insn & 0xffff0000) >> 16)
1137 | ((insn & 0xffff) << 16);
1139 return insn;
1142 /* This function is called for relocations that are otherwise marked as NOT
1143 requiring overflow checks. In here we perform non-standard checks of
1144 the relocation value. */
1146 static inline bfd_reloc_status_type
1147 arc_special_overflow_checks (const struct arc_relocation_data reloc_data,
1148 bfd_signed_vma relocation,
1149 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1151 switch (reloc_data.howto->type)
1153 case R_ARC_NPS_CMEM16:
1154 if (((relocation >> 16) & 0xffff) != NPS_CMEM_HIGH_VALUE)
1156 if (reloc_data.reloc_addend == 0)
1157 _bfd_error_handler
1158 /* xgettext:c-format */
1159 (_("%pB(%pA+%#" PRIx64 "): CMEM relocation to `%s' is invalid, "
1160 "16 MSB should be %#x (value is %#" PRIx64 ")"),
1161 reloc_data.input_section->owner,
1162 reloc_data.input_section,
1163 (uint64_t) reloc_data.reloc_offset,
1164 reloc_data.symbol_name,
1165 NPS_CMEM_HIGH_VALUE,
1166 (uint64_t) relocation);
1167 else
1168 _bfd_error_handler
1169 /* xgettext:c-format */
1170 (_("%pB(%pA+%#" PRIx64 "): CMEM relocation to `%s+%#" PRIx64
1171 "' is invalid, 16 MSB should be %#x (value is %#" PRIx64 ")"),
1172 reloc_data.input_section->owner,
1173 reloc_data.input_section,
1174 (uint64_t) reloc_data.reloc_offset,
1175 reloc_data.symbol_name,
1176 (uint64_t) reloc_data.reloc_addend,
1177 NPS_CMEM_HIGH_VALUE,
1178 (uint64_t) relocation);
1179 return bfd_reloc_overflow;
1181 break;
1183 default:
1184 break;
1187 return bfd_reloc_ok;
1190 #define ME(reloc) (reloc)
1192 #define IS_ME(FORMULA,BFD) ((strstr (FORMULA, "ME") != NULL) \
1193 && (!bfd_big_endian (BFD)))
1195 #define S ((bfd_signed_vma) (reloc_data.sym_value \
1196 + (reloc_data.sym_section->output_section != NULL ? \
1197 (reloc_data.sym_section->output_offset \
1198 + reloc_data.sym_section->output_section->vma) : 0)))
1199 #define L ((bfd_signed_vma) (reloc_data.sym_value \
1200 + (reloc_data.sym_section->output_section != NULL ? \
1201 (reloc_data.sym_section->output_offset \
1202 + reloc_data.sym_section->output_section->vma) : 0)))
1203 #define A (reloc_data.reloc_addend)
1204 #define B (0)
1205 #define G (reloc_data.got_offset_value)
1206 #define GOT (reloc_data.got_symbol_vma)
1207 #define GOT_BEGIN (htab->sgot->output_section->vma)
1209 #define MES (0)
1210 /* P: relative offset to PCL The offset should be to the
1211 current location aligned to 32 bits. */
1212 #define P ((bfd_signed_vma) ( \
1214 (reloc_data.input_section->output_section != NULL ? \
1215 reloc_data.input_section->output_section->vma : 0) \
1216 + reloc_data.input_section->output_offset \
1217 + (reloc_data.reloc_offset - (bitsize >= 32 ? 4 : 0))) \
1218 & ~0x3))
1219 #define PDATA ((bfd_signed_vma) ( \
1220 (reloc_data.input_section->output_section->vma \
1221 + reloc_data.input_section->output_offset \
1222 + (reloc_data.reloc_offset))))
1223 #define SECTSTART (bfd_signed_vma) (reloc_data.sym_section->output_section->vma \
1224 + reloc_data.sym_section->output_offset)
1225 #define FINAL_SECTSTART \
1226 (bfd_signed_vma) (reloc_data.sym_section->output_section->vma)
1227 #define JLI (bfd_signed_vma) (reloc_data.sym_section->output_section->vma)
1228 #define _SDA_BASE_ (bfd_signed_vma) (reloc_data.sdata_begin_symbol_vma)
1229 #define TLS_REL (bfd_signed_vma)(tls_sec->output_section->vma)
1230 #define TLS_TBSS (align_power (TCB_SIZE, tls_sec->alignment_power))
1232 #define none (0)
1234 #ifdef ARC_ENABLE_DEBUG
1235 #define PRINT_DEBUG_RELOC_INFO_BEFORE(FORMULA, TYPE) \
1236 do \
1238 asection *sym_section = reloc_data.sym_section; \
1239 asection *input_section = reloc_data.input_section; \
1240 ARC_DEBUG ("RELOC_TYPE = " TYPE "\n"); \
1241 ARC_DEBUG ("FORMULA = " FORMULA "\n"); \
1242 ARC_DEBUG ("S = %#lx\n", S); \
1243 ARC_DEBUG ("A = %#lx\n", A); \
1244 ARC_DEBUG ("L = %lx\n", L); \
1245 if (sym_section->output_section != NULL) \
1246 ARC_DEBUG ("symbol_section->vma = %#lx\n", \
1247 sym_section->output_section->vma \
1248 + sym_section->output_offset); \
1249 else \
1250 ARC_DEBUG ("symbol_section->vma = NULL\n"); \
1251 if (input_section->output_section != NULL) \
1252 ARC_DEBUG ("input_section->vma = %#lx\n", \
1253 input_section->output_section->vma \
1254 + input_section->output_offset); \
1255 else \
1256 ARC_DEBUG ("input_section->vma = NULL\n"); \
1257 ARC_DEBUG ("PCL = %#lx\n", P); \
1258 ARC_DEBUG ("P = %#lx\n", P); \
1259 ARC_DEBUG ("G = %#lx\n", G); \
1260 ARC_DEBUG ("SDA_OFFSET = %#lx\n", _SDA_BASE_); \
1261 ARC_DEBUG ("SDA_SET = %d\n", reloc_data.sdata_begin_symbol_vma_set); \
1262 ARC_DEBUG ("GOT_OFFSET = %#lx\n", GOT); \
1263 ARC_DEBUG ("relocation = %#08lx\n", relocation); \
1264 ARC_DEBUG ("before = %#08x\n", (unsigned) insn); \
1265 ARC_DEBUG ("data = %08x (%u) (%d)\n", (unsigned) relocation, \
1266 (unsigned) relocation, (int) relocation); \
1268 while (0)
1270 #define PRINT_DEBUG_RELOC_INFO_AFTER \
1271 do \
1273 ARC_DEBUG ("after = 0x%08x\n", (unsigned int) insn); \
1275 while (0)
1277 #else
1279 #define PRINT_DEBUG_RELOC_INFO_BEFORE(...)
1280 #define PRINT_DEBUG_RELOC_INFO_AFTER
1282 #endif /* ARC_ENABLE_DEBUG */
1284 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
1285 case R_##TYPE: \
1287 bfd_signed_vma bitsize ATTRIBUTE_UNUSED = BITSIZE; \
1288 relocation = FORMULA ; \
1289 PRINT_DEBUG_RELOC_INFO_BEFORE (#FORMULA, #TYPE); \
1290 insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
1291 insn = (* get_replace_function (abfd, TYPE)) (insn, relocation); \
1292 insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
1293 PRINT_DEBUG_RELOC_INFO_AFTER; \
1295 break;
1297 static bfd_reloc_status_type
1298 arc_do_relocation (bfd_byte * contents,
1299 struct arc_relocation_data reloc_data,
1300 struct bfd_link_info *info)
1302 bfd_signed_vma relocation = 0;
1303 bfd_vma insn;
1304 bfd_vma orig_insn ATTRIBUTE_UNUSED;
1305 bfd * abfd = reloc_data.input_section->owner;
1306 struct elf_link_hash_table *htab ATTRIBUTE_UNUSED = elf_hash_table (info);
1307 bfd_reloc_status_type flag;
1308 asection *tls_sec = htab->tls_sec;
1310 if (!reloc_data.should_relocate)
1311 return bfd_reloc_ok;
1313 switch (bfd_get_reloc_size (reloc_data.howto))
1315 case 4:
1316 insn = arc_bfd_get_32 (abfd,
1317 contents + reloc_data.reloc_offset,
1318 reloc_data.input_section);
1319 break;
1320 case 2:
1321 insn = arc_bfd_get_16 (abfd,
1322 contents + reloc_data.reloc_offset,
1323 reloc_data.input_section);
1324 break;
1325 case 1:
1326 insn = arc_bfd_get_8 (abfd,
1327 contents + reloc_data.reloc_offset,
1328 reloc_data.input_section);
1329 break;
1330 default:
1331 insn = 0;
1332 BFD_ASSERT (0);
1333 break;
1336 orig_insn = insn;
1338 /* If we resolve a TLS relocation, make sure we do have a valid TLS
1339 section. */
1340 switch (reloc_data.howto->type)
1342 case R_ARC_TLS_LE_32:
1343 if (tls_sec == NULL)
1344 return bfd_reloc_notsupported;
1345 break;
1347 default:
1348 break;
1352 switch (reloc_data.howto->type)
1354 #include "elf/arc-reloc.def"
1356 default:
1357 BFD_ASSERT (0);
1358 break;
1361 /* Check for relocation overflow. */
1362 if (reloc_data.howto->complain_on_overflow != complain_overflow_dont)
1363 flag = bfd_check_overflow (reloc_data.howto->complain_on_overflow,
1364 reloc_data.howto->bitsize,
1365 reloc_data.howto->rightshift,
1366 bfd_arch_bits_per_address (abfd),
1367 relocation);
1368 else
1369 flag = arc_special_overflow_checks (reloc_data, relocation, info);
1371 if (flag != bfd_reloc_ok)
1373 ARC_DEBUG ("Relocation overflows !\n");
1374 DEBUG_ARC_RELOC (reloc_data);
1375 ARC_DEBUG ("Relocation value = signed -> %d, unsigned -> %u"
1376 ", hex -> (0x%08x)\n",
1377 (int) relocation, (unsigned) relocation, (int) relocation);
1379 return flag;
1382 /* Write updated instruction back to memory. */
1383 switch (bfd_get_reloc_size (reloc_data.howto))
1385 case 4:
1386 arc_bfd_put_32 (abfd, insn,
1387 contents + reloc_data.reloc_offset,
1388 reloc_data.input_section);
1389 break;
1390 case 2:
1391 arc_bfd_put_16 (abfd, insn,
1392 contents + reloc_data.reloc_offset,
1393 reloc_data.input_section);
1394 break;
1395 case 1:
1396 arc_bfd_put_8 (abfd, insn,
1397 contents + reloc_data.reloc_offset,
1398 reloc_data.input_section);
1399 break;
1400 default:
1401 ARC_DEBUG ("size = %d\n", reloc_data.howto->size);
1402 BFD_ASSERT (0);
1403 break;
1406 return bfd_reloc_ok;
1408 #undef S
1409 #undef A
1410 #undef B
1411 #undef G
1412 #undef GOT
1413 #undef L
1414 #undef MES
1415 #undef P
1416 #undef SECTSTAR
1417 #undef SECTSTART
1418 #undef JLI
1419 #undef _SDA_BASE_
1420 #undef none
1422 #undef ARC_RELOC_HOWTO
1425 /* Relocate an arc ELF section.
1426 Function : elf_arc_relocate_section
1427 Brief : Relocate an arc section, by handling all the relocations
1428 appearing in that section.
1429 Args : output_bfd : The bfd being written to.
1430 info : Link information.
1431 input_bfd : The input bfd.
1432 input_section : The section being relocated.
1433 contents : contents of the section being relocated.
1434 relocs : List of relocations in the section.
1435 local_syms : is a pointer to the swapped in local symbols.
1436 local_section : is an array giving the section in the input file
1437 corresponding to the st_shndx field of each
1438 local symbol. */
1439 static int
1440 elf_arc_relocate_section (bfd * output_bfd,
1441 struct bfd_link_info * info,
1442 bfd * input_bfd,
1443 asection * input_section,
1444 bfd_byte * contents,
1445 Elf_Internal_Rela * relocs,
1446 Elf_Internal_Sym * local_syms,
1447 asection ** local_sections)
1449 Elf_Internal_Shdr * symtab_hdr;
1450 struct elf_link_hash_entry ** sym_hashes;
1451 Elf_Internal_Rela * rel;
1452 Elf_Internal_Rela * wrel;
1453 Elf_Internal_Rela * relend;
1454 struct elf_link_hash_table * htab = elf_hash_table (info);
1456 symtab_hdr = &((elf_tdata (input_bfd))->symtab_hdr);
1457 sym_hashes = elf_sym_hashes (input_bfd);
1459 rel = wrel = relocs;
1460 relend = relocs + input_section->reloc_count;
1461 for (; rel < relend; wrel++, rel++)
1463 enum elf_arc_reloc_type r_type;
1464 reloc_howto_type *howto;
1465 unsigned long r_symndx;
1466 struct elf_link_hash_entry *h;
1467 Elf_Internal_Sym *sym;
1468 asection *sec;
1469 struct elf_link_hash_entry *h2;
1470 const char *msg;
1471 bool unresolved_reloc = false;
1473 struct arc_relocation_data reloc_data =
1475 .reloc_offset = 0,
1476 .reloc_addend = 0,
1477 .got_offset_value = 0,
1478 .sym_value = 0,
1479 .sym_section = NULL,
1480 .howto = NULL,
1481 .input_section = NULL,
1482 .sdata_begin_symbol_vma = 0,
1483 .sdata_begin_symbol_vma_set = false,
1484 .got_symbol_vma = 0,
1485 .should_relocate = false
1488 r_type = ELF32_R_TYPE (rel->r_info);
1490 if (r_type >= (int) R_ARC_max)
1492 bfd_set_error (bfd_error_bad_value);
1493 return false;
1495 howto = arc_elf_howto (r_type);
1497 r_symndx = ELF32_R_SYM (rel->r_info);
1499 /* If we are generating another .o file and the symbol in not
1500 local, skip this relocation. */
1501 if (bfd_link_relocatable (info))
1503 /* This is a relocateable link. We don't have to change
1504 anything, unless the reloc is against a section symbol,
1505 in which case we have to adjust according to where the
1506 section symbol winds up in the output section. */
1508 /* Checks if this is a local symbol and thus the reloc
1509 might (will??) be against a section symbol. */
1510 if (r_symndx < symtab_hdr->sh_info)
1512 sym = local_syms + r_symndx;
1513 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1515 sec = local_sections[r_symndx];
1517 /* For RELA relocs. Just adjust the addend
1518 value in the relocation entry. */
1519 rel->r_addend += sec->output_offset + sym->st_value;
1521 ARC_DEBUG ("local symbols reloc (section=%d %s) seen in %s\n",
1522 (int) r_symndx, local_sections[r_symndx]->name,
1523 __PRETTY_FUNCTION__);
1528 h2 = elf_link_hash_lookup (elf_hash_table (info), "__SDATA_BEGIN__",
1529 false, false, true);
1531 if (!reloc_data.sdata_begin_symbol_vma_set
1532 && h2 != NULL && h2->root.type != bfd_link_hash_undefined
1533 && h2->root.u.def.section->output_section != NULL)
1534 /* TODO: Verify this condition. */
1536 reloc_data.sdata_begin_symbol_vma =
1537 (h2->root.u.def.value
1538 + h2->root.u.def.section->output_section->vma);
1539 reloc_data.sdata_begin_symbol_vma_set = true;
1542 reloc_data.input_section = input_section;
1543 reloc_data.howto = howto;
1544 reloc_data.reloc_offset = rel->r_offset;
1545 reloc_data.reloc_addend = rel->r_addend;
1547 /* This is a final link. */
1548 h = NULL;
1549 sym = NULL;
1550 sec = NULL;
1552 if (r_symndx < symtab_hdr->sh_info) /* A local symbol. */
1554 sym = local_syms + r_symndx;
1555 sec = local_sections[r_symndx];
1557 else
1559 bool warned, ignored;
1560 bfd_vma relocation ATTRIBUTE_UNUSED;
1562 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1563 r_symndx, symtab_hdr, sym_hashes,
1564 h, sec, relocation,
1565 unresolved_reloc, warned, ignored);
1567 /* TODO: This code is repeated from below. We should
1568 clean it and remove duplications.
1569 Sec is used check for discarded sections.
1570 Need to redesign code below. */
1572 /* Get the symbol's entry in the symtab. */
1573 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1575 while (h->root.type == bfd_link_hash_indirect
1576 || h->root.type == bfd_link_hash_warning)
1577 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1579 /* If we have encountered a definition for this symbol. */
1580 if (h->root.type == bfd_link_hash_defined
1581 || h->root.type == bfd_link_hash_defweak)
1583 reloc_data.sym_value = h->root.u.def.value;
1584 sec = h->root.u.def.section;
1588 /* Clean relocs for symbols in discarded sections. */
1589 if (sec != NULL && discarded_section (sec))
1591 _bfd_clear_contents (howto, input_bfd, input_section,
1592 contents, rel->r_offset);
1593 rel->r_info = 0;
1594 rel->r_addend = 0;
1596 /* For ld -r, remove relocations in debug sections against
1597 sections defined in discarded sections. Not done for
1598 eh_frame editing code expects to be present. */
1599 if (bfd_link_relocatable (info)
1600 && (input_section->flags & SEC_DEBUGGING))
1601 wrel--;
1603 continue;
1606 if (bfd_link_relocatable (info))
1608 if (wrel != rel)
1609 *wrel = *rel;
1610 continue;
1613 if (r_symndx < symtab_hdr->sh_info) /* A local symbol. */
1615 reloc_data.sym_value = sym->st_value;
1616 reloc_data.sym_section = sec;
1617 reloc_data.symbol_name =
1618 bfd_elf_string_from_elf_section (input_bfd,
1619 symtab_hdr->sh_link,
1620 sym->st_name);
1622 /* Mergeable section handling. */
1623 if ((sec->flags & SEC_MERGE)
1624 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1626 asection *msec;
1627 msec = sec;
1628 rel->r_addend = _bfd_elf_rel_local_sym (output_bfd, sym,
1629 &msec, rel->r_addend);
1630 rel->r_addend -= (sec->output_section->vma
1631 + sec->output_offset
1632 + sym->st_value);
1633 rel->r_addend += msec->output_section->vma + msec->output_offset;
1635 reloc_data.reloc_addend = rel->r_addend;
1638 BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto));
1639 if (htab->sgot != NULL)
1640 reloc_data.got_symbol_vma = htab->sgot->output_section->vma
1641 + htab->sgot->output_offset;
1643 reloc_data.should_relocate = true;
1645 else /* Global symbol. */
1647 /* FIXME: We should use the RELOC_FOR_GLOBAL_SYMBOL macro
1648 (defined in elf-bfd.h) here. */
1650 /* Get the symbol's entry in the symtab. */
1651 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1653 while (h->root.type == bfd_link_hash_indirect
1654 || h->root.type == bfd_link_hash_warning)
1656 struct elf_arc_link_hash_entry *ah_old =
1657 (struct elf_arc_link_hash_entry *) h;
1658 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1659 struct elf_arc_link_hash_entry *ah =
1660 (struct elf_arc_link_hash_entry *) h;
1662 if (ah->got_ents == 0 && ah_old->got_ents != ah->got_ents)
1663 ah->got_ents = ah_old->got_ents;
1666 /* TODO: Need to validate what was the intention. */
1667 /* BFD_ASSERT ((h->dynindx == -1) || (h->forced_local != 0)); */
1668 reloc_data.symbol_name = h->root.root.string;
1670 /* If we have encountered a definition for this symbol. */
1671 if (h->root.type == bfd_link_hash_defined
1672 || h->root.type == bfd_link_hash_defweak)
1674 reloc_data.sym_value = h->root.u.def.value;
1675 reloc_data.sym_section = h->root.u.def.section;
1677 reloc_data.should_relocate = true;
1679 if (is_reloc_for_GOT (howto) && !bfd_link_pic (info))
1681 struct elf_arc_link_hash_entry *ah =
1682 (struct elf_arc_link_hash_entry *) h;
1683 /* TODO: Change it to use arc_do_relocation with
1684 ARC_32 reloc. Try to use ADD_RELA macro. */
1685 bfd_vma relocation =
1686 reloc_data.sym_value + reloc_data.reloc_addend
1687 + (reloc_data.sym_section->output_section != NULL ?
1688 (reloc_data.sym_section->output_offset
1689 + reloc_data.sym_section->output_section->vma)
1690 : 0);
1692 BFD_ASSERT (ah->got_ents);
1693 bfd_vma got_offset = ah->got_ents->offset;
1694 bfd_put_32 (output_bfd, relocation,
1695 htab->sgot->contents + got_offset);
1697 if (is_reloc_for_PLT (howto) && h->plt.offset != (bfd_vma) -1)
1699 /* TODO: This is repeated up here. */
1700 reloc_data.sym_value = h->plt.offset;
1701 reloc_data.sym_section = htab->splt;
1704 else if (h->root.type == bfd_link_hash_undefweak)
1706 /* Is weak symbol and has no definition. */
1707 if (is_reloc_for_GOT (howto))
1709 reloc_data.sym_value = h->root.u.def.value;
1710 reloc_data.sym_section = htab->sgot;
1711 reloc_data.should_relocate = true;
1713 else if (is_reloc_for_PLT (howto)
1714 && h->plt.offset != (bfd_vma) -1)
1716 /* TODO: This is repeated up here. */
1717 reloc_data.sym_value = h->plt.offset;
1718 reloc_data.sym_section = htab->splt;
1719 reloc_data.should_relocate = true;
1721 else
1722 continue;
1724 else
1726 if (is_reloc_for_GOT (howto))
1728 reloc_data.sym_value = h->root.u.def.value;
1729 reloc_data.sym_section = htab->sgot;
1731 reloc_data.should_relocate = true;
1733 else if (is_reloc_for_PLT (howto))
1735 /* Fail if it is linking for PIE and the symbol is
1736 undefined. */
1737 if (bfd_link_executable (info))
1738 (*info->callbacks->undefined_symbol)
1739 (info, h->root.root.string, input_bfd, input_section,
1740 rel->r_offset, true);
1741 reloc_data.sym_value = h->plt.offset;
1742 reloc_data.sym_section = htab->splt;
1744 reloc_data.should_relocate = true;
1746 else if (!bfd_link_pic (info) || bfd_link_executable (info))
1747 (*info->callbacks->undefined_symbol)
1748 (info, h->root.root.string, input_bfd, input_section,
1749 rel->r_offset, true);
1752 BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto));
1753 if (htab->sgot != NULL)
1754 reloc_data.got_symbol_vma = htab->sgot->output_section->vma
1755 + htab->sgot->output_offset;
1758 if ((is_reloc_for_GOT (howto)
1759 || is_reloc_for_TLS (howto)))
1761 reloc_data.should_relocate = true;
1763 struct got_entry **list
1764 = get_got_entry_list_for_symbol (input_bfd, r_symndx, h);
1766 reloc_data.got_offset_value
1767 = relocate_fix_got_relocs_for_got_info (list,
1768 tls_type_for_reloc (howto),
1769 info,
1770 output_bfd,
1771 r_symndx,
1772 local_syms,
1773 local_sections,
1775 &reloc_data);
1777 if (h == NULL)
1779 create_got_dynrelocs_for_single_entry (
1780 got_entry_for_type (list,
1781 arc_got_entry_type_for_reloc (howto)),
1782 output_bfd, info, NULL);
1787 #define IS_ARC_PCREL_TYPE(TYPE) \
1788 ( (TYPE == R_ARC_PC32) \
1789 || (TYPE == R_ARC_32_PCREL))
1791 switch (r_type)
1793 case R_ARC_32:
1794 case R_ARC_32_ME:
1795 case R_ARC_PC32:
1796 case R_ARC_32_PCREL:
1797 if (bfd_link_pic (info)
1798 && (input_section->flags & SEC_ALLOC) != 0
1799 && (!IS_ARC_PCREL_TYPE (r_type)
1800 || (h != NULL
1801 && h->dynindx != -1
1802 && !h->def_regular
1803 && (!info->symbolic || !h->def_regular))))
1805 Elf_Internal_Rela outrel;
1806 bfd_byte *loc;
1807 bool skip = false;
1808 bool relocate = false;
1809 asection *sreloc = _bfd_elf_get_dynamic_reloc_section
1810 (input_bfd, input_section,
1811 /*RELA*/ true);
1813 BFD_ASSERT (sreloc != NULL);
1815 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
1816 info,
1817 input_section,
1818 rel->r_offset);
1820 if (outrel.r_offset == (bfd_vma) -1)
1821 skip = true;
1823 outrel.r_addend = rel->r_addend;
1824 outrel.r_offset += (input_section->output_section->vma
1825 + input_section->output_offset);
1827 if (skip)
1829 memset (&outrel, 0, sizeof outrel);
1830 relocate = false;
1832 else if (h != NULL
1833 && h->dynindx != -1
1834 && (IS_ARC_PCREL_TYPE (r_type)
1835 || !(bfd_link_executable (info)
1836 || SYMBOLIC_BIND (info, h))
1837 || ! h->def_regular))
1839 BFD_ASSERT (h != NULL);
1840 if ((input_section->flags & SEC_ALLOC) != 0)
1841 relocate = false;
1842 else
1843 relocate = true;
1845 BFD_ASSERT (h->dynindx != -1);
1846 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1848 else
1850 /* Handle local symbols, they either do not have a
1851 global hash table entry (h == NULL), or are
1852 forced local due to a version script
1853 (h->forced_local), or the third condition is
1854 legacy, it appears to say something like, for
1855 links where we are pre-binding the symbols, or
1856 there's not an entry for this symbol in the
1857 dynamic symbol table, and it's a regular symbol
1858 not defined in a shared object, then treat the
1859 symbol as local, resolve it now. */
1860 relocate = true;
1861 /* outrel.r_addend = 0; */
1862 outrel.r_info = ELF32_R_INFO (0, R_ARC_RELATIVE);
1865 BFD_ASSERT (sreloc->contents != 0);
1867 loc = sreloc->contents;
1868 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
1869 sreloc->reloc_count += 1;
1871 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1873 if (!relocate)
1874 continue;
1876 break;
1877 default:
1878 break;
1881 if (is_reloc_SDA_relative (howto)
1882 && !reloc_data.sdata_begin_symbol_vma_set)
1884 _bfd_error_handler
1885 ("error: linker symbol __SDATA_BEGIN__ not found");
1886 bfd_set_error (bfd_error_bad_value);
1887 return false;
1890 DEBUG_ARC_RELOC (reloc_data);
1892 /* Make sure we have with a dynamic linker. In case of GOT and PLT
1893 the sym_section should point to .got or .plt respectively. */
1894 if ((is_reloc_for_GOT (howto) || is_reloc_for_PLT (howto))
1895 && reloc_data.sym_section == NULL)
1897 _bfd_error_handler
1898 (_("GOT and PLT relocations cannot be fixed with a non dynamic linker"));
1899 bfd_set_error (bfd_error_bad_value);
1900 return false;
1903 msg = NULL;
1904 switch (arc_do_relocation (contents, reloc_data, info))
1906 case bfd_reloc_ok:
1907 continue; /* The reloc processing loop. */
1909 case bfd_reloc_overflow:
1910 (*info->callbacks->reloc_overflow)
1911 (info, (h ? &h->root : NULL), reloc_data.symbol_name, howto->name, (bfd_vma) 0,
1912 input_bfd, input_section, rel->r_offset);
1913 break;
1915 case bfd_reloc_undefined:
1916 (*info->callbacks->undefined_symbol)
1917 (info, reloc_data.symbol_name, input_bfd, input_section, rel->r_offset, true);
1918 break;
1920 case bfd_reloc_other:
1921 /* xgettext:c-format */
1922 msg = _("%pB(%pA): warning: unaligned access to symbol '%s' in the small data area");
1923 break;
1925 case bfd_reloc_outofrange:
1926 /* xgettext:c-format */
1927 msg = _("%pB(%pA): internal error: out of range error");
1928 break;
1930 case bfd_reloc_notsupported:
1931 /* xgettext:c-format */
1932 msg = _("%pB(%pA): internal error: unsupported relocation error");
1933 break;
1935 case bfd_reloc_dangerous:
1936 /* xgettext:c-format */
1937 msg = _("%pB(%pA): internal error: dangerous relocation");
1938 break;
1940 default:
1941 /* xgettext:c-format */
1942 msg = _("%pB(%pA): internal error: unknown error");
1943 break;
1946 if (msg)
1947 _bfd_error_handler (msg, input_bfd, input_section, reloc_data.symbol_name);
1948 return false;
1951 return true;
1954 #define elf_arc_hash_table(p) \
1955 ((is_elf_hash_table ((p)->hash) \
1956 && elf_hash_table_id (elf_hash_table (p)) == ARC_ELF_DATA) \
1957 ? (struct elf_arc_link_hash_table *) (p)->hash : NULL)
1959 static bool
1960 elf_arc_check_relocs (bfd * abfd,
1961 struct bfd_link_info * info,
1962 asection * sec,
1963 const Elf_Internal_Rela * relocs)
1965 Elf_Internal_Shdr * symtab_hdr;
1966 struct elf_link_hash_entry ** sym_hashes;
1967 const Elf_Internal_Rela * rel;
1968 const Elf_Internal_Rela * rel_end;
1969 bfd * dynobj;
1970 asection * sreloc = NULL;
1971 struct elf_link_hash_table * htab = elf_hash_table (info);
1973 if (bfd_link_relocatable (info))
1974 return true;
1976 if (htab->dynobj == NULL)
1977 htab->dynobj = abfd;
1979 dynobj = (elf_hash_table (info))->dynobj;
1980 symtab_hdr = &((elf_tdata (abfd))->symtab_hdr);
1981 sym_hashes = elf_sym_hashes (abfd);
1983 rel_end = relocs + sec->reloc_count;
1984 for (rel = relocs; rel < rel_end; rel++)
1986 enum elf_arc_reloc_type r_type;
1987 reloc_howto_type *howto;
1988 unsigned long r_symndx;
1989 struct elf_link_hash_entry *h;
1991 r_type = ELF32_R_TYPE (rel->r_info);
1993 if (r_type >= (int) R_ARC_max)
1995 bfd_set_error (bfd_error_bad_value);
1996 return false;
1998 howto = arc_elf_howto (r_type);
2000 /* Load symbol information. */
2001 r_symndx = ELF32_R_SYM (rel->r_info);
2002 if (r_symndx < symtab_hdr->sh_info) /* Is a local symbol. */
2003 h = NULL;
2004 else /* Global one. */
2006 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2007 while (h->root.type == bfd_link_hash_indirect
2008 || h->root.type == bfd_link_hash_warning)
2009 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2013 switch (r_type)
2015 case R_ARC_32:
2016 case R_ARC_32_ME:
2017 /* During shared library creation, these relocs should not
2018 appear in a shared library (as memory will be read only
2019 and the dynamic linker can not resolve these. However
2020 the error should not occur for e.g. debugging or
2021 non-readonly sections. */
2022 if (h != NULL
2023 && (bfd_link_dll (info) && !bfd_link_pie (info))
2024 && (sec->flags & SEC_ALLOC) != 0
2025 && (sec->flags & SEC_READONLY) != 0
2026 && ((sec->flags & SEC_CODE) != 0
2027 || (sec->flags & SEC_DEBUGGING) != 0))
2029 const char *name;
2030 if (h)
2031 name = h->root.root.string;
2032 else
2033 name = "UNKNOWN";
2034 _bfd_error_handler
2035 /* xgettext:c-format */
2036 (_("%pB: relocation %s against `%s' can not be used"
2037 " when making a shared object; recompile with -fPIC"),
2038 abfd,
2039 arc_elf_howto (r_type)->name,
2040 name);
2041 bfd_set_error (bfd_error_bad_value);
2042 return false;
2045 /* In some cases we are not setting the 'non_got_ref'
2046 flag, even though the relocations don't require a GOT
2047 access. We should extend the testing in this area to
2048 ensure that no significant cases are being missed. */
2049 if (h)
2050 h->non_got_ref = 1;
2051 /* FALLTHROUGH */
2052 case R_ARC_PC32:
2053 case R_ARC_32_PCREL:
2054 if ((bfd_link_pic (info))
2055 && ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL)
2056 || (h != NULL
2057 && (!info->symbolic || !h->def_regular))))
2059 if (sreloc == NULL)
2061 if (info->dynamic
2062 && ! htab->dynamic_sections_created
2063 && ! _bfd_elf_link_create_dynamic_sections (abfd, info))
2064 return false;
2065 sreloc = _bfd_elf_make_dynamic_reloc_section (sec, dynobj,
2066 2, abfd,
2067 /*rela*/
2068 true);
2070 if (sreloc == NULL)
2071 return false;
2073 sreloc->size += sizeof (Elf32_External_Rela);
2076 default:
2077 break;
2080 if (is_reloc_for_PLT (howto))
2082 if (h == NULL)
2083 continue;
2084 else
2085 if (h->forced_local == 0)
2086 h->needs_plt = 1;
2089 /* Add info to the symbol got_entry_list. */
2090 if (is_reloc_for_GOT (howto)
2091 || is_reloc_for_TLS (howto))
2093 if (bfd_link_dll (info) && !bfd_link_pie (info)
2094 && (r_type == R_ARC_TLS_LE_32 || r_type == R_ARC_TLS_LE_S9))
2096 const char *name;
2097 if (h)
2098 name = h->root.root.string;
2099 else
2100 /* bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL); */
2101 name = "UNKNOWN";
2102 _bfd_error_handler
2103 /* xgettext:c-format */
2104 (_("%pB: relocation %s against `%s' can not be used"
2105 " when making a shared object; recompile with -fPIC"),
2106 abfd,
2107 arc_elf_howto (r_type)->name,
2108 name);
2109 bfd_set_error (bfd_error_bad_value);
2110 return false;
2112 if (! _bfd_elf_create_got_section (dynobj, info))
2113 return false;
2115 arc_fill_got_info_for_reloc (
2116 arc_got_entry_type_for_reloc (howto),
2117 get_got_entry_list_for_symbol (abfd, r_symndx, h),
2118 info,
2123 return true;
2126 #define ELF_DYNAMIC_INTERPRETER "/sbin/ld-uClibc.so"
2128 static const struct plt_version_t *
2129 arc_get_plt_version (struct bfd_link_info *info)
2131 int i;
2133 for (i = 0; i < 1; i++)
2135 ARC_DEBUG ("%d: size1 = %d, size2 = %d\n", i,
2136 (int) plt_versions[i].entry_size,
2137 (int) plt_versions[i].elem_size);
2140 if (bfd_get_mach (info->output_bfd) == bfd_mach_arc_arcv2)
2142 if (bfd_link_pic (info))
2143 return &(plt_versions[ELF_ARCV2_PIC]);
2144 else
2145 return &(plt_versions[ELF_ARCV2_ABS]);
2147 else
2149 if (bfd_link_pic (info))
2150 return &(plt_versions[ELF_ARC_PIC]);
2151 else
2152 return &(plt_versions[ELF_ARC_ABS]);
2156 static bfd_vma
2157 add_symbol_to_plt (struct bfd_link_info *info)
2159 struct elf_link_hash_table *htab = elf_hash_table (info);
2160 bfd_vma ret;
2162 const struct plt_version_t *plt_data = arc_get_plt_version (info);
2164 /* If this is the first .plt entry, make room for the special first
2165 entry. */
2166 if (htab->splt->size == 0)
2167 htab->splt->size += plt_data->entry_size;
2169 ret = htab->splt->size;
2171 htab->splt->size += plt_data->elem_size;
2172 ARC_DEBUG ("PLT_SIZE = %d\n", (int) htab->splt->size);
2174 htab->sgotplt->size += 4;
2175 htab->srelplt->size += sizeof (Elf32_External_Rela);
2177 return ret;
2180 #define PLT_DO_RELOCS_FOR_ENTRY(ABFD, DS, RELOCS) \
2181 plt_do_relocs_for_symbol (ABFD, DS, RELOCS, 0, 0)
2183 static void
2184 plt_do_relocs_for_symbol (bfd *abfd,
2185 struct elf_link_hash_table *htab,
2186 const struct plt_reloc *reloc,
2187 bfd_vma plt_offset,
2188 bfd_vma symbol_got_offset)
2190 while (SYM_ONLY (reloc->symbol) != LAST_RELOC)
2192 bfd_vma relocation = 0;
2194 switch (SYM_ONLY (reloc->symbol))
2196 case SGOT:
2197 relocation
2198 = htab->sgotplt->output_section->vma
2199 + htab->sgotplt->output_offset + symbol_got_offset;
2200 break;
2202 relocation += reloc->addend;
2204 if (IS_RELATIVE (reloc->symbol))
2206 bfd_vma reloc_offset = reloc->offset;
2207 reloc_offset -= (IS_INSN_32 (reloc->symbol)) ? 4 : 0;
2208 reloc_offset -= (IS_INSN_24 (reloc->symbol)) ? 2 : 0;
2210 relocation -= htab->splt->output_section->vma
2211 + htab->splt->output_offset
2212 + plt_offset + reloc_offset;
2215 /* TODO: being ME is not a property of the relocation but of the
2216 section of which is applying the relocation. */
2217 if (IS_MIDDLE_ENDIAN (reloc->symbol) && !bfd_big_endian (abfd))
2219 relocation
2220 = ((relocation & 0xffff0000) >> 16)
2221 | ((relocation & 0xffff) << 16);
2224 switch (reloc->size)
2226 case 32:
2227 bfd_put_32 (htab->splt->output_section->owner,
2228 relocation,
2229 htab->splt->contents + plt_offset + reloc->offset);
2230 break;
2233 reloc = &(reloc[1]); /* Jump to next relocation. */
2237 static void
2238 relocate_plt_for_symbol (bfd *output_bfd,
2239 struct bfd_link_info *info,
2240 struct elf_link_hash_entry *h)
2242 const struct plt_version_t *plt_data = arc_get_plt_version (info);
2243 struct elf_link_hash_table *htab = elf_hash_table (info);
2245 bfd_vma plt_index = (h->plt.offset - plt_data->entry_size)
2246 / plt_data->elem_size;
2247 bfd_vma got_offset = (plt_index + 3) * 4;
2249 ARC_DEBUG ("arc_info: PLT_OFFSET = %#lx, PLT_ENTRY_VMA = %#lx, \
2250 GOT_ENTRY_OFFSET = %#lx, GOT_ENTRY_VMA = %#lx, for symbol %s\n",
2251 (long) h->plt.offset,
2252 (long) (htab->splt->output_section->vma
2253 + htab->splt->output_offset
2254 + h->plt.offset),
2255 (long) got_offset,
2256 (long) (htab->sgotplt->output_section->vma
2257 + htab->sgotplt->output_offset
2258 + got_offset),
2259 h->root.root.string);
2262 bfd_vma i = 0;
2263 uint16_t *ptr = (uint16_t *) plt_data->elem;
2265 for (i = 0; i < plt_data->elem_size/2; i++)
2267 uint16_t data = ptr[i];
2268 bfd_put_16 (output_bfd,
2269 (bfd_vma) data,
2270 htab->splt->contents + h->plt.offset + (i*2));
2274 plt_do_relocs_for_symbol (output_bfd, htab,
2275 plt_data->elem_relocs,
2276 h->plt.offset,
2277 got_offset);
2279 /* Fill in the entry in the global offset table. */
2280 bfd_put_32 (output_bfd,
2281 (bfd_vma) (htab->splt->output_section->vma
2282 + htab->splt->output_offset),
2283 htab->sgotplt->contents + got_offset);
2285 /* TODO: Fill in the entry in the .rela.plt section. */
2287 Elf_Internal_Rela rel;
2288 bfd_byte *loc;
2290 rel.r_offset = (htab->sgotplt->output_section->vma
2291 + htab->sgotplt->output_offset
2292 + got_offset);
2293 rel.r_addend = 0;
2295 BFD_ASSERT (h->dynindx != -1);
2296 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_JMP_SLOT);
2298 loc = htab->srelplt->contents;
2299 loc += plt_index * sizeof (Elf32_External_Rela); /* relA */
2300 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2304 static void
2305 relocate_plt_for_entry (bfd *abfd,
2306 struct bfd_link_info *info)
2308 const struct plt_version_t *plt_data = arc_get_plt_version (info);
2309 struct elf_link_hash_table *htab = elf_hash_table (info);
2312 bfd_vma i = 0;
2313 uint16_t *ptr = (uint16_t *) plt_data->entry;
2314 for (i = 0; i < plt_data->entry_size/2; i++)
2316 uint16_t data = ptr[i];
2317 bfd_put_16 (abfd,
2318 (bfd_vma) data,
2319 htab->splt->contents + (i*2));
2322 PLT_DO_RELOCS_FOR_ENTRY (abfd, htab, plt_data->entry_relocs);
2325 /* Desc : Adjust a symbol defined by a dynamic object and referenced
2326 by a regular object. The current definition is in some section of
2327 the dynamic object, but we're not including those sections. We
2328 have to change the definition to something the rest of the link can
2329 understand. */
2331 static bool
2332 elf_arc_adjust_dynamic_symbol (struct bfd_link_info *info,
2333 struct elf_link_hash_entry *h)
2335 asection *s;
2336 bfd *dynobj = (elf_hash_table (info))->dynobj;
2337 struct elf_link_hash_table *htab = elf_hash_table (info);
2339 if (h->type == STT_FUNC
2340 || h->type == STT_GNU_IFUNC
2341 || h->needs_plt == 1)
2343 if (!bfd_link_pic (info) && !h->def_dynamic && !h->ref_dynamic)
2345 /* This case can occur if we saw a PLT32 reloc in an input
2346 file, but the symbol was never referred to by a dynamic
2347 object. In such a case, we don't actually need to build
2348 a procedure linkage table, and we can just do a PC32
2349 reloc instead. */
2350 BFD_ASSERT (h->needs_plt);
2351 return true;
2354 /* Make sure this symbol is output as a dynamic symbol. */
2355 if (h->dynindx == -1 && !h->forced_local
2356 && !bfd_elf_link_record_dynamic_symbol (info, h))
2357 return false;
2359 if (bfd_link_pic (info)
2360 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2362 bfd_vma loc = add_symbol_to_plt (info);
2364 if (bfd_link_executable (info) && !h->def_regular)
2366 h->root.u.def.section = htab->splt;
2367 h->root.u.def.value = loc;
2369 h->plt.offset = loc;
2371 else
2373 h->plt.offset = (bfd_vma) -1;
2374 h->needs_plt = 0;
2376 return true;
2379 /* If this is a weak symbol, and there is a real definition, the
2380 processor independent code will have arranged for us to see the
2381 real definition first, and we can just use the same value. */
2382 if (h->is_weakalias)
2384 struct elf_link_hash_entry *def = weakdef (h);
2385 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2386 h->root.u.def.section = def->root.u.def.section;
2387 h->root.u.def.value = def->root.u.def.value;
2388 return true;
2391 /* This is a reference to a symbol defined by a dynamic object which
2392 is not a function. */
2394 /* If we are creating a shared library, we must presume that the
2395 only references to the symbol are via the global offset table.
2396 For such cases we need not do anything here; the relocations will
2397 be handled correctly by relocate_section. */
2398 if (!bfd_link_executable (info))
2399 return true;
2401 /* If there are no non-GOT references, we do not need a copy
2402 relocation. */
2403 if (!h->non_got_ref)
2404 return true;
2406 /* If -z nocopyreloc was given, we won't generate them either. */
2407 if (info->nocopyreloc)
2409 h->non_got_ref = 0;
2410 return true;
2413 /* We must allocate the symbol in our .dynbss section, which will
2414 become part of the .bss section of the executable. There will be
2415 an entry for this symbol in the .dynsym section. The dynamic
2416 object will contain position independent code, so all references
2417 from the dynamic object to this symbol will go through the global
2418 offset table. The dynamic linker will use the .dynsym entry to
2419 determine the address it must put in the global offset table, so
2420 both the dynamic object and the regular object will refer to the
2421 same memory location for the variable. */
2423 if (htab == NULL)
2424 return false;
2426 /* We must generate a R_ARC_COPY reloc to tell the dynamic linker to
2427 copy the initial value out of the dynamic object and into the
2428 runtime process image. We need to remember the offset into the
2429 .rela.bss section we are going to use. */
2430 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2432 struct elf_arc_link_hash_table *arc_htab = elf_arc_hash_table (info);
2434 BFD_ASSERT (arc_htab->elf.srelbss != NULL);
2435 arc_htab->elf.srelbss->size += sizeof (Elf32_External_Rela);
2436 h->needs_copy = 1;
2439 /* TODO: Move this also to arc_hash_table. */
2440 s = bfd_get_section_by_name (dynobj, ".dynbss");
2441 BFD_ASSERT (s != NULL);
2443 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2446 /* Function : elf_arc_finish_dynamic_symbol
2447 Brief : Finish up dynamic symbol handling. We set the
2448 contents of various dynamic sections here.
2449 Args : output_bfd :
2450 info :
2452 sym :
2453 Returns : True/False as the return status. */
2455 static bool
2456 elf_arc_finish_dynamic_symbol (bfd * output_bfd,
2457 struct bfd_link_info *info,
2458 struct elf_link_hash_entry *h,
2459 Elf_Internal_Sym * sym)
2461 if (h->plt.offset != (bfd_vma) -1)
2463 relocate_plt_for_symbol (output_bfd, info, h);
2465 if (!h->def_regular)
2467 /* Mark the symbol as undefined, rather than as defined in
2468 the .plt section. Leave the value alone. */
2469 sym->st_shndx = SHN_UNDEF;
2474 /* This function traverses list of GOT entries and
2475 create respective dynamic relocs. */
2476 /* TODO: Make function to get list and not access the list directly. */
2477 /* TODO: Move function to relocate_section create this relocs eagerly. */
2478 struct elf_arc_link_hash_entry *ah =
2479 (struct elf_arc_link_hash_entry *) h;
2480 create_got_dynrelocs_for_got_info (&ah->got_ents,
2481 output_bfd,
2482 info,
2485 if (h->needs_copy)
2487 struct elf_arc_link_hash_table *arc_htab = elf_arc_hash_table (info);
2489 if (h->dynindx == -1
2490 || (h->root.type != bfd_link_hash_defined
2491 && h->root.type != bfd_link_hash_defweak)
2492 || arc_htab->elf.srelbss == NULL)
2493 abort ();
2495 bfd_vma rel_offset = (h->root.u.def.value
2496 + h->root.u.def.section->output_section->vma
2497 + h->root.u.def.section->output_offset);
2499 bfd_byte * loc = arc_htab->elf.srelbss->contents
2500 + (arc_htab->elf.srelbss->reloc_count * sizeof (Elf32_External_Rela));
2501 arc_htab->elf.srelbss->reloc_count++;
2503 Elf_Internal_Rela rel;
2504 rel.r_addend = 0;
2505 rel.r_offset = rel_offset;
2507 BFD_ASSERT (h->dynindx != -1);
2508 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_COPY);
2510 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2513 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2514 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2515 || strcmp (h->root.root.string, "__DYNAMIC") == 0
2516 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2517 sym->st_shndx = SHN_ABS;
2519 return true;
2522 #define GET_SYMBOL_OR_SECTION(TAG, SYMBOL, SECTION) \
2523 case TAG: \
2524 if (SYMBOL != NULL) \
2525 h = elf_link_hash_lookup (elf_hash_table (info), \
2526 SYMBOL, false, false, true); \
2527 else if (SECTION != NULL) \
2528 s = bfd_get_linker_section (dynobj, SECTION); \
2529 break;
2532 struct obfd_info_group {
2533 bfd *output_bfd;
2534 struct bfd_link_info *info;
2537 static bool
2538 arc_create_forced_local_got_entries_for_tls (struct bfd_hash_entry *bh,
2539 void *data)
2541 struct elf_arc_link_hash_entry * h =
2542 (struct elf_arc_link_hash_entry *) bh;
2543 struct obfd_info_group *tmp = (struct obfd_info_group *) data;
2545 if (h->got_ents != NULL)
2547 BFD_ASSERT (h);
2549 struct got_entry *list = h->got_ents;
2551 while (list != NULL)
2553 create_got_dynrelocs_for_single_entry (list, tmp->output_bfd,
2554 tmp->info,
2555 (struct elf_link_hash_entry *) h);
2556 list = list->next;
2560 return true;
2564 /* Function : elf_arc_finish_dynamic_sections
2565 Brief : Finish up the dynamic sections handling.
2566 Args : output_bfd :
2567 info :
2569 sym :
2570 Returns : True/False as the return status. */
2572 static bool
2573 elf_arc_finish_dynamic_sections (bfd * output_bfd,
2574 struct bfd_link_info *info)
2576 struct elf_link_hash_table *htab = elf_hash_table (info);
2577 bfd *dynobj = (elf_hash_table (info))->dynobj;
2578 asection *sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2580 if (sdyn)
2582 Elf32_External_Dyn *dyncon, *dynconend;
2584 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2585 dynconend
2586 = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2587 for (; dyncon < dynconend; dyncon++)
2589 Elf_Internal_Dyn internal_dyn;
2590 bool do_it = false;
2592 struct elf_link_hash_entry *h = NULL;
2593 asection *s = NULL;
2595 bfd_elf32_swap_dyn_in (dynobj, dyncon, &internal_dyn);
2597 switch (internal_dyn.d_tag)
2599 GET_SYMBOL_OR_SECTION (DT_INIT, info->init_function, NULL)
2600 GET_SYMBOL_OR_SECTION (DT_FINI, info->fini_function, NULL)
2601 GET_SYMBOL_OR_SECTION (DT_PLTGOT, NULL, ".plt")
2602 GET_SYMBOL_OR_SECTION (DT_JMPREL, NULL, ".rela.plt")
2603 GET_SYMBOL_OR_SECTION (DT_PLTRELSZ, NULL, ".rela.plt")
2604 GET_SYMBOL_OR_SECTION (DT_VERSYM, NULL, ".gnu.version")
2605 GET_SYMBOL_OR_SECTION (DT_VERDEF, NULL, ".gnu.version_d")
2606 GET_SYMBOL_OR_SECTION (DT_VERNEED, NULL, ".gnu.version_r")
2607 default:
2608 break;
2611 /* In case the dynamic symbols should be updated with a symbol. */
2612 if (h != NULL
2613 && (h->root.type == bfd_link_hash_defined
2614 || h->root.type == bfd_link_hash_defweak))
2616 asection *asec_ptr;
2618 internal_dyn.d_un.d_val = h->root.u.def.value;
2619 asec_ptr = h->root.u.def.section;
2620 if (asec_ptr->output_section != NULL)
2622 internal_dyn.d_un.d_val +=
2623 (asec_ptr->output_section->vma
2624 + asec_ptr->output_offset);
2626 else
2628 /* The symbol is imported from another shared
2629 library and does not apply to this one. */
2630 internal_dyn.d_un.d_val = 0;
2632 do_it = true;
2634 else if (s != NULL) /* With a section information. */
2636 switch (internal_dyn.d_tag)
2638 case DT_PLTGOT:
2639 case DT_JMPREL:
2640 case DT_VERSYM:
2641 case DT_VERDEF:
2642 case DT_VERNEED:
2643 internal_dyn.d_un.d_ptr = (s->output_section->vma
2644 + s->output_offset);
2645 do_it = true;
2646 break;
2648 case DT_PLTRELSZ:
2649 internal_dyn.d_un.d_val = s->size;
2650 do_it = true;
2651 break;
2653 default:
2654 break;
2658 if (do_it)
2659 bfd_elf32_swap_dyn_out (output_bfd, &internal_dyn, dyncon);
2662 if (htab->splt->size > 0)
2664 relocate_plt_for_entry (output_bfd, info);
2667 /* TODO: Validate this. */
2668 if (htab->srelplt->output_section != bfd_abs_section_ptr)
2669 elf_section_data (htab->srelplt->output_section)
2670 ->this_hdr.sh_entsize = 12;
2673 /* Fill in the first three entries in the global offset table. */
2674 if (htab->sgot)
2676 struct elf_link_hash_entry *h;
2677 h = elf_link_hash_lookup (elf_hash_table (info), "_GLOBAL_OFFSET_TABLE_",
2678 false, false, true);
2680 if (h != NULL && h->root.type != bfd_link_hash_undefined
2681 && h->root.u.def.section != NULL)
2683 asection *sec = h->root.u.def.section;
2685 if (sdyn == NULL)
2686 bfd_put_32 (output_bfd, (bfd_vma) 0,
2687 sec->contents);
2688 else
2689 bfd_put_32 (output_bfd,
2690 sdyn->output_section->vma + sdyn->output_offset,
2691 sec->contents);
2692 bfd_put_32 (output_bfd, (bfd_vma) 0, sec->contents + 4);
2693 bfd_put_32 (output_bfd, (bfd_vma) 0, sec->contents + 8);
2697 struct obfd_info_group group;
2698 group.output_bfd = output_bfd;
2699 group.info = info;
2700 bfd_hash_traverse (&info->hash->table,
2701 arc_create_forced_local_got_entries_for_tls, &group);
2703 return true;
2706 #define ADD_DYNAMIC_SYMBOL(NAME, TAG) \
2707 h = elf_link_hash_lookup (elf_hash_table (info), \
2708 NAME, false, false, false); \
2709 if ((h != NULL && (h->ref_regular || h->def_regular))) \
2710 if (! _bfd_elf_add_dynamic_entry (info, TAG, 0)) \
2711 return false;
2713 /* Set the sizes of the dynamic sections. */
2714 static bool
2715 elf_arc_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2716 struct bfd_link_info *info)
2718 bfd *dynobj;
2719 asection *s;
2720 bool relocs_exist = false;
2721 struct elf_link_hash_table *htab = elf_hash_table (info);
2723 dynobj = htab->dynobj;
2724 if (dynobj == NULL)
2725 return true;
2727 if (htab->dynamic_sections_created)
2729 struct elf_link_hash_entry *h;
2731 /* Set the contents of the .interp section to the
2732 interpreter. */
2733 if (bfd_link_executable (info) && !info->nointerp)
2735 s = bfd_get_section_by_name (dynobj, ".interp");
2736 BFD_ASSERT (s != NULL);
2737 s->size = sizeof (ELF_DYNAMIC_INTERPRETER);
2738 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2741 /* Add some entries to the .dynamic section. We fill in some of
2742 the values later, in elf_bfd_final_link, but we must add the
2743 entries now so that we know the final size of the .dynamic
2744 section. Checking if the .init section is present. We also
2745 create DT_INIT and DT_FINI entries if the init_str has been
2746 changed by the user. */
2747 ADD_DYNAMIC_SYMBOL (info->init_function, DT_INIT);
2748 ADD_DYNAMIC_SYMBOL (info->fini_function, DT_FINI);
2750 else
2752 /* We may have created entries in the .rela.got section.
2753 However, if we are not creating the dynamic sections, we will
2754 not actually use these entries. Reset the size of .rela.got,
2755 which will cause it to get stripped from the output file
2756 below. */
2757 if (htab->srelgot != NULL)
2758 htab->srelgot->size = 0;
2761 for (s = dynobj->sections; s != NULL; s = s->next)
2763 if ((s->flags & SEC_LINKER_CREATED) == 0)
2764 continue;
2766 if (s == htab->splt
2767 || s == htab->sgot
2768 || s == htab->sgotplt
2769 || s == htab->sdynbss)
2771 /* Strip this section if we don't need it. */
2773 else if (startswith (s->name, ".rela"))
2775 if (s->size != 0 && s != htab->srelplt)
2776 relocs_exist = true;
2778 /* We use the reloc_count field as a counter if we need to
2779 copy relocs into the output file. */
2780 s->reloc_count = 0;
2782 else
2784 /* It's not one of our sections, so don't allocate space. */
2785 continue;
2788 if (s->size == 0)
2790 s->flags |= SEC_EXCLUDE;
2791 continue;
2794 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2795 continue;
2797 /* Allocate memory for the section contents. */
2798 s->contents = bfd_zalloc (dynobj, s->size);
2799 if (s->contents == NULL)
2800 return false;
2803 return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs_exist);
2807 /* Classify dynamic relocs such that -z combreloc can reorder and combine
2808 them. */
2809 static enum elf_reloc_type_class
2810 elf32_arc_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2811 const asection *rel_sec ATTRIBUTE_UNUSED,
2812 const Elf_Internal_Rela *rela)
2814 switch ((int) ELF32_R_TYPE (rela->r_info))
2816 case R_ARC_RELATIVE:
2817 return reloc_class_relative;
2818 case R_ARC_JMP_SLOT:
2819 return reloc_class_plt;
2820 case R_ARC_COPY:
2821 return reloc_class_copy;
2822 /* TODO: Needed in future to support ifunc. */
2824 case R_ARC_IRELATIVE:
2825 return reloc_class_ifunc;
2827 default:
2828 return reloc_class_normal;
2832 const struct elf_size_info arc_elf32_size_info =
2834 sizeof (Elf32_External_Ehdr),
2835 sizeof (Elf32_External_Phdr),
2836 sizeof (Elf32_External_Shdr),
2837 sizeof (Elf32_External_Rel),
2838 sizeof (Elf32_External_Rela),
2839 sizeof (Elf32_External_Sym),
2840 sizeof (Elf32_External_Dyn),
2841 sizeof (Elf_External_Note),
2844 32, 2,
2845 ELFCLASS32, EV_CURRENT,
2846 bfd_elf32_write_out_phdrs,
2847 bfd_elf32_write_shdrs_and_ehdr,
2848 bfd_elf32_checksum_contents,
2849 bfd_elf32_write_relocs,
2850 bfd_elf32_swap_symbol_in,
2851 bfd_elf32_swap_symbol_out,
2852 bfd_elf32_slurp_reloc_table,
2853 bfd_elf32_slurp_symbol_table,
2854 bfd_elf32_swap_dyn_in,
2855 bfd_elf32_swap_dyn_out,
2856 bfd_elf32_swap_reloc_in,
2857 bfd_elf32_swap_reloc_out,
2858 bfd_elf32_swap_reloca_in,
2859 bfd_elf32_swap_reloca_out
2862 #define elf_backend_size_info arc_elf32_size_info
2864 /* GDB expects general purpose registers to be in section .reg. However Linux
2865 kernel doesn't create this section and instead writes registers to NOTE
2866 section. It is up to the binutils to create a pseudo-section .reg from the
2867 contents of NOTE. Also BFD will read pid and signal number from NOTE. This
2868 function relies on offsets inside elf_prstatus structure in Linux to be
2869 stable. */
2871 static bool
2872 elf32_arc_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2874 int offset;
2875 size_t size;
2877 switch (note->descsz)
2879 default:
2880 return false;
2882 case 236: /* sizeof (struct elf_prstatus) on Linux/arc. */
2883 /* pr_cursig */
2884 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2885 /* pr_pid */
2886 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
2887 /* pr_regs */
2888 offset = 72;
2889 size = (40 * 4); /* There are 40 registers in user_regs_struct. */
2890 break;
2892 /* Make a ".reg/999" section. */
2893 return _bfd_elfcore_make_pseudosection (abfd, ".reg", size,
2894 note->descpos + offset);
2897 /* Determine whether an object attribute tag takes an integer, a
2898 string or both. */
2900 static int
2901 elf32_arc_obj_attrs_arg_type (int tag)
2903 if (tag == Tag_ARC_CPU_name
2904 || tag == Tag_ARC_ISA_config
2905 || tag == Tag_ARC_ISA_apex)
2906 return ATTR_TYPE_FLAG_STR_VAL;
2907 else if (tag < (Tag_ARC_ISA_mpy_option + 1))
2908 return ATTR_TYPE_FLAG_INT_VAL;
2909 else
2910 return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
2913 /* Attribute numbers >=14 can be safely ignored. */
2915 static bool
2916 elf32_arc_obj_attrs_handle_unknown (bfd *abfd, int tag)
2918 if ((tag & 127) < (Tag_ARC_ISA_mpy_option + 1))
2920 _bfd_error_handler
2921 (_("%pB: unknown mandatory ARC object attribute %d"),
2922 abfd, tag);
2923 bfd_set_error (bfd_error_bad_value);
2924 return false;
2926 else
2928 _bfd_error_handler
2929 (_("warning: %pB: unknown ARC object attribute %d"),
2930 abfd, tag);
2931 return true;
2935 /* Handle an ARC specific section when reading an object file. This is
2936 called when bfd_section_from_shdr finds a section with an unknown
2937 type. */
2939 static bool
2940 elf32_arc_section_from_shdr (bfd *abfd,
2941 Elf_Internal_Shdr * hdr,
2942 const char *name,
2943 int shindex)
2945 switch (hdr->sh_type)
2947 case 0x0c: /* MWDT specific section, don't complain about it. */
2948 case SHT_ARC_ATTRIBUTES:
2949 break;
2951 default:
2952 return false;
2955 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2956 return false;
2958 return true;
2961 /* Relaxation hook.
2963 These are the current relaxing opportunities available:
2965 * R_ARC_GOTPC32 => R_ARC_PCREL.
2969 static bool
2970 arc_elf_relax_section (bfd *abfd, asection *sec,
2971 struct bfd_link_info *link_info, bool *again)
2973 Elf_Internal_Shdr *symtab_hdr;
2974 Elf_Internal_Rela *internal_relocs;
2975 Elf_Internal_Rela *irel, *irelend;
2976 bfd_byte *contents = NULL;
2977 Elf_Internal_Sym *isymbuf = NULL;
2979 /* Assume nothing changes. */
2980 *again = false;
2982 /* We don't have to do anything for a relocatable link, if this
2983 section does not have relocs, or if this is not a code
2984 section. */
2985 if (bfd_link_relocatable (link_info)
2986 || sec->reloc_count == 0
2987 || (sec->flags & SEC_RELOC) == 0
2988 || (sec->flags & SEC_HAS_CONTENTS) == 0
2989 || (sec->flags & SEC_CODE) == 0)
2990 return true;
2992 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2994 /* Get a copy of the native relocations. */
2995 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
2996 link_info->keep_memory);
2997 if (internal_relocs == NULL)
2998 goto error_return;
3000 /* Walk through them looking for relaxing opportunities. */
3001 irelend = internal_relocs + sec->reloc_count;
3002 for (irel = internal_relocs; irel < irelend; irel++)
3004 /* If this isn't something that can be relaxed, then ignore
3005 this reloc. */
3006 if (ELF32_R_TYPE (irel->r_info) != (int) R_ARC_GOTPC32)
3007 continue;
3009 /* Get the section contents if we haven't done so already. */
3010 if (contents == NULL)
3012 /* Get cached copy if it exists. */
3013 if (elf_section_data (sec)->this_hdr.contents != NULL)
3014 contents = elf_section_data (sec)->this_hdr.contents;
3015 /* Go get them off disk. */
3016 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3017 goto error_return;
3020 /* Read this BFD's local symbols if we haven't done so already. */
3021 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
3023 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3024 if (isymbuf == NULL)
3025 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3026 symtab_hdr->sh_info, 0,
3027 NULL, NULL, NULL);
3028 if (isymbuf == NULL)
3029 goto error_return;
3032 struct elf_link_hash_entry *htop = NULL;
3034 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
3036 /* An external symbol. */
3037 unsigned int indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3038 htop = elf_sym_hashes (abfd)[indx];
3041 if (ELF32_R_TYPE (irel->r_info) == (int) R_ARC_GOTPC32
3042 && SYMBOL_REFERENCES_LOCAL (link_info, htop))
3044 unsigned int code;
3046 /* Get the opcode. */
3047 code = bfd_get_32_me (abfd, contents + irel->r_offset - 4);
3049 /* Note that we've changed the relocs, section contents, etc. */
3050 elf_section_data (sec)->relocs = internal_relocs;
3051 elf_section_data (sec)->this_hdr.contents = contents;
3052 symtab_hdr->contents = (unsigned char *) isymbuf;
3054 /* Fix the relocation's type. */
3055 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_ARC_PC32);
3057 /* ld rA,[pcl,symbol@tgot] -> add rA,pcl,symbol@pcl. */
3058 /* 0010 0bbb aa11 0ZZX DBBB 1111 10AA AAAA.
3059 111 00 000 0111 xx xxxx*/
3060 code &= ~0x27307F80;
3061 BFD_ASSERT (code <= 62UL);
3062 code |= 0x27007F80;
3064 /* Write back the new instruction. */
3065 bfd_put_32_me (abfd, code, contents + irel->r_offset - 4);
3067 /* The size isn't changed, don't redo. */
3068 *again = false;
3072 if (isymbuf != NULL
3073 && symtab_hdr->contents != (unsigned char *) isymbuf)
3075 if (!link_info->keep_memory)
3076 free (isymbuf);
3077 else
3078 /* Cache the symbols for elf_link_input_bfd. */
3079 symtab_hdr->contents = (unsigned char *) isymbuf;
3082 if (contents != NULL
3083 && elf_section_data (sec)->this_hdr.contents != contents)
3085 if (!link_info->keep_memory)
3086 free (contents);
3087 else
3088 /* Cache the section contents for elf_link_input_bfd. */
3089 elf_section_data (sec)->this_hdr.contents = contents;
3092 if (elf_section_data (sec)->relocs != internal_relocs)
3093 free (internal_relocs);
3095 return true;
3097 error_return:
3098 if (symtab_hdr->contents != (unsigned char *) isymbuf)
3099 free (isymbuf);
3100 if (elf_section_data (sec)->this_hdr.contents != contents)
3101 free (contents);
3102 if (elf_section_data (sec)->relocs != internal_relocs)
3103 free (internal_relocs);
3105 return false;
3108 #define TARGET_LITTLE_SYM arc_elf32_le_vec
3109 #define TARGET_LITTLE_NAME "elf32-littlearc"
3110 #define TARGET_BIG_SYM arc_elf32_be_vec
3111 #define TARGET_BIG_NAME "elf32-bigarc"
3112 #define ELF_ARCH bfd_arch_arc
3113 #define ELF_TARGET_ID ARC_ELF_DATA
3114 #define ELF_MACHINE_CODE EM_ARC_COMPACT
3115 #define ELF_MACHINE_ALT1 EM_ARC_COMPACT2
3116 #define ELF_MAXPAGESIZE 0x2000
3118 #define bfd_elf32_bfd_link_hash_table_create arc_elf_link_hash_table_create
3120 #define bfd_elf32_bfd_merge_private_bfd_data arc_elf_merge_private_bfd_data
3121 #define bfd_elf32_bfd_reloc_type_lookup arc_elf32_bfd_reloc_type_lookup
3122 #define bfd_elf32_bfd_set_private_flags arc_elf_set_private_flags
3123 #define bfd_elf32_bfd_print_private_bfd_data arc_elf_print_private_bfd_data
3124 #define bfd_elf32_bfd_copy_private_bfd_data arc_elf_copy_private_bfd_data
3125 #define bfd_elf32_bfd_relax_section arc_elf_relax_section
3127 #define elf_info_to_howto_rel arc_info_to_howto_rel
3128 #define elf_backend_object_p arc_elf_object_p
3129 #define elf_backend_final_write_processing arc_elf_final_write_processing
3131 #define elf_backend_relocate_section elf_arc_relocate_section
3132 #define elf_backend_check_relocs elf_arc_check_relocs
3133 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
3135 #define elf_backend_reloc_type_class elf32_arc_reloc_type_class
3137 #define elf_backend_adjust_dynamic_symbol elf_arc_adjust_dynamic_symbol
3138 #define elf_backend_finish_dynamic_symbol elf_arc_finish_dynamic_symbol
3140 #define elf_backend_finish_dynamic_sections elf_arc_finish_dynamic_sections
3141 #define elf_backend_late_size_sections elf_arc_late_size_sections
3143 #define elf_backend_can_gc_sections 1
3144 #define elf_backend_want_got_plt 1
3145 #define elf_backend_plt_readonly 1
3146 #define elf_backend_rela_plts_and_copies_p 1
3147 #define elf_backend_want_plt_sym 0
3148 #define elf_backend_got_header_size 12
3149 #define elf_backend_dtrel_excludes_plt 1
3151 #define elf_backend_may_use_rel_p 0
3152 #define elf_backend_may_use_rela_p 1
3153 #define elf_backend_default_use_rela_p 1
3155 #define elf_backend_grok_prstatus elf32_arc_grok_prstatus
3157 #define elf_backend_default_execstack 0
3159 #undef elf_backend_obj_attrs_vendor
3160 #define elf_backend_obj_attrs_vendor "ARC"
3161 #undef elf_backend_obj_attrs_section
3162 #define elf_backend_obj_attrs_section ".ARC.attributes"
3163 #undef elf_backend_obj_attrs_arg_type
3164 #define elf_backend_obj_attrs_arg_type elf32_arc_obj_attrs_arg_type
3165 #undef elf_backend_obj_attrs_section_type
3166 #define elf_backend_obj_attrs_section_type SHT_ARC_ATTRIBUTES
3167 #define elf_backend_obj_attrs_handle_unknown elf32_arc_obj_attrs_handle_unknown
3169 #define elf_backend_section_from_shdr elf32_arc_section_from_shdr
3171 #include "elf32-target.h"