1 /* ARC-specific support for 32-bit ELF
2 Copyright (C) 1994-2016 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. */
27 #include "libiberty.h"
28 #include "opcode/arc-func.h"
29 #include "opcode/arc.h"
33 # define PR_DEBUG(fmt, args...) fprintf (stderr, fmt, ##args)
35 # define PR_DEBUG(fmt, args...)
38 /* #define ARC_ENABLE_DEBUG 1 */
39 #ifndef ARC_ENABLE_DEBUG
40 #define ARC_DEBUG(...)
43 name_for_global_symbol (struct elf_link_hash_entry *h)
45 static char *local_str = "(local)";
49 return h->root.root.string;
51 #define ARC_DEBUG(args...) fprintf (stderr, ##args)
55 #define ADD_RELA(BFD, SECTION, OFFSET, SYM_IDX, TYPE, ADDEND) \
57 struct elf_link_hash_table *_htab = elf_hash_table (info); \
58 Elf_Internal_Rela _rel; \
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); \
73 struct dynamic_sections
75 bfd_boolean initialized;
79 asection * srelgotplt;
85 enum dyn_section_types
96 const char * dyn_section_names[DYN_SECTION_TYPES_END] =
125 struct got_entry *next;
126 enum tls_type_e type;
128 bfd_boolean processed;
129 bfd_boolean created_dyn_relocation;
130 enum tls_got_entries existing_entries;
134 new_got_entry_to_list (struct got_entry **list,
135 enum tls_type_e type,
137 enum tls_got_entries existing_entries)
139 /* Find list end. Avoid having multiple entries of the same
141 struct got_entry **p = list;
144 if ((*p)->type == type)
149 struct got_entry *entry =
150 (struct got_entry *) malloc (sizeof(struct got_entry));
153 entry->offset = offset;
155 entry->processed = FALSE;
156 entry->created_dyn_relocation = FALSE;
157 entry->existing_entries = existing_entries;
159 /* Add the entry to the end of the list. */
164 symbol_has_entry_of_type (struct got_entry *list, enum tls_type_e type)
168 if (list->type == type)
176 /* The default symbols representing the init and fini dyn values.
177 TODO: Check what is the relation of those strings with arclinux.em
179 #define INIT_SYM_STRING "_init"
180 #define FINI_SYM_STRING "_fini"
182 char * init_str = INIT_SYM_STRING;
183 char * fini_str = FINI_SYM_STRING;
185 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
190 static ATTRIBUTE_UNUSED const char *
191 reloc_type_to_name (unsigned int type)
195 #include "elf/arc-reloc.def"
202 #undef ARC_RELOC_HOWTO
204 /* Try to minimize the amount of space occupied by relocation tables
205 on the ROM (not that the ROM won't be swamped by other ELF overhead). */
209 static ATTRIBUTE_UNUSED bfd_boolean
210 is_reloc_PC_relative (reloc_howto_type *howto)
212 return (strstr (howto->name, "PC") != NULL) ? TRUE : FALSE;
216 is_reloc_SDA_relative (reloc_howto_type *howto)
218 return (strstr (howto->name, "SDA") != NULL) ? TRUE : FALSE;
222 is_reloc_for_GOT (reloc_howto_type * howto)
224 if (strstr (howto->name, "TLS") != NULL)
226 return (strstr (howto->name, "GOT") != NULL) ? TRUE : FALSE;
230 is_reloc_for_PLT (reloc_howto_type * howto)
232 return (strstr (howto->name, "PLT") != NULL) ? TRUE : FALSE;
236 is_reloc_for_TLS (reloc_howto_type *howto)
238 return (strstr (howto->name, "TLS") != NULL) ? TRUE : FALSE;
241 #define arc_bfd_get_8(A,B,C) bfd_get_8(A,B)
242 #define arc_bfd_get_16(A,B,C) bfd_get_16(A,B)
243 #define arc_bfd_get_32(A,B,C) bfd_get_32(A,B)
244 #define arc_bfd_put_8(A,B,C,D) bfd_put_8(A,B,C)
245 #define arc_bfd_put_16(A,B,C,D) bfd_put_16(A,B,C)
246 #define arc_bfd_put_32(A,B,C,D) bfd_put_32(A,B,C)
249 static bfd_reloc_status_type
250 arc_elf_reloc (bfd *abfd ATTRIBUTE_UNUSED,
251 arelent *reloc_entry,
253 void *data ATTRIBUTE_UNUSED,
254 asection *input_section,
256 char ** error_message ATTRIBUTE_UNUSED)
258 if (output_bfd != NULL)
260 reloc_entry->address += input_section->output_offset;
262 /* In case of relocateable link and if the reloc is against a
263 section symbol, the addend needs to be adjusted according to
264 where the section symbol winds up in the output section. */
265 if ((symbol_in->flags & BSF_SECTION_SYM) && symbol_in->section)
266 reloc_entry->addend += symbol_in->section->output_offset;
271 return bfd_reloc_continue;
275 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
279 #include "elf/arc-reloc.def"
282 #undef ARC_RELOC_HOWTO
284 #define ARC_RELOC_HOWTO(TYPE, VALUE, RSIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
285 [TYPE] = HOWTO (R_##TYPE, 0, RSIZE, BITSIZE, FALSE, 0, complain_overflow_##OVERFLOW, arc_elf_reloc, "R_" #TYPE, FALSE, 0, 0, FALSE),
287 static struct reloc_howto_struct elf_arc_howto_table[] =
289 #include "elf/arc-reloc.def"
290 /* Example of what is generated by the preprocessor. Currently kept as an
292 HOWTO (R_ARC_NONE, // Type.
294 2, // Size (0 = byte, 1 = short, 2 = long).
296 FALSE, // PC_relative.
298 complain_overflow_bitfield, // Complain_on_overflow.
299 bfd_elf_generic_reloc, // Special_function.
300 "R_ARC_NONE", // Name.
301 TRUE, // Partial_inplace.
304 FALSE), // PCrel_offset.
307 #undef ARC_RELOC_HOWTO
309 static void arc_elf_howto_init (void)
311 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
312 elf_arc_howto_table[TYPE].pc_relative = \
313 (strstr (#FORMULA, " P ") != NULL || strstr (#FORMULA, " PDATA ") != NULL); \
314 elf_arc_howto_table[TYPE].dst_mask = RELOC_FUNCTION(0, ~0); \
315 /* Only 32 bit data relocations should be marked as ME. */ \
316 if (strstr (#FORMULA, " ME ") != NULL) \
318 BFD_ASSERT (SIZE == 2); \
321 #include "elf/arc-reloc.def"
324 #undef ARC_RELOC_HOWTO
327 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
329 const int howto_table_lookup[] =
331 #include "elf/arc-reloc.def"
333 #undef ARC_RELOC_HOWTO
335 static reloc_howto_type *
336 arc_elf_howto (unsigned int r_type)
338 if (elf_arc_howto_table[R_ARC_32].dst_mask == 0)
339 arc_elf_howto_init ();
340 return &elf_arc_howto_table[r_type];
343 /* Map BFD reloc types to ARC ELF reloc types. */
347 bfd_reloc_code_real_type bfd_reloc_val;
348 unsigned char elf_reloc_val;
351 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
352 { BFD_RELOC_##TYPE, R_##TYPE },
353 static const struct arc_reloc_map arc_reloc_map[] =
355 #include "elf/arc-reloc.def"
357 {BFD_RELOC_NONE, R_ARC_NONE},
358 {BFD_RELOC_8, R_ARC_8},
359 {BFD_RELOC_16, R_ARC_16},
360 {BFD_RELOC_24, R_ARC_24},
361 {BFD_RELOC_32, R_ARC_32},
363 #undef ARC_RELOC_HOWTO
365 typedef ATTRIBUTE_UNUSED bfd_vma (*replace_func) (unsigned, int ATTRIBUTE_UNUSED);
367 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
369 func = (void *) RELOC_FUNCTION; \
372 get_replace_function (bfd *abfd, unsigned int r_type)
378 #include "elf/arc-reloc.def"
381 if (func == replace_bits24 && bfd_big_endian (abfd))
382 return (replace_func) replace_bits24_be;
384 return (replace_func) func;
386 #undef ARC_RELOC_HOWTO
388 static reloc_howto_type *
389 arc_elf32_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
390 bfd_reloc_code_real_type code)
394 for (i = ARRAY_SIZE (arc_reloc_map); i--;)
396 if (arc_reloc_map[i].bfd_reloc_val == code)
397 return arc_elf_howto (arc_reloc_map[i].elf_reloc_val);
403 /* Function to set the ELF flag bits. */
405 arc_elf_set_private_flags (bfd *abfd, flagword flags)
407 elf_elfheader (abfd)->e_flags = flags;
408 elf_flags_init (abfd) = TRUE;
412 /* Print private flags. */
414 arc_elf_print_private_bfd_data (bfd *abfd, void * ptr)
416 FILE *file = (FILE *) ptr;
419 BFD_ASSERT (abfd != NULL && ptr != NULL);
421 /* Print normal ELF private data. */
422 _bfd_elf_print_private_bfd_data (abfd, ptr);
424 flags = elf_elfheader (abfd)->e_flags;
425 fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
427 switch (flags & EF_ARC_MACH_MSK)
429 case EF_ARC_CPU_ARCV2HS : fprintf (file, " -mcpu=ARCv2HS"); break;
430 case EF_ARC_CPU_ARCV2EM : fprintf (file, " -mcpu=ARCv2EM"); break;
431 case E_ARC_MACH_ARC600 : fprintf (file, " -mcpu=ARC600"); break;
432 case E_ARC_MACH_ARC601 : fprintf (file, " -mcpu=ARC601"); break;
433 case E_ARC_MACH_ARC700 : fprintf (file, " -mcpu=ARC700"); break;
435 fprintf (file, "-mcpu=unknown");
439 switch (flags & EF_ARC_OSABI_MSK)
441 case E_ARC_OSABI_ORIG : fprintf (file, " (ABI:legacy)"); break;
442 case E_ARC_OSABI_V2 : fprintf (file, " (ABI:v2)"); break;
443 case E_ARC_OSABI_V3 : fprintf (file, " (ABI:v3)"); break;
445 fprintf (file, "(ABI:unknown)");
453 /* Copy backend specific data from one object module to another. */
456 arc_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
458 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
459 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
462 BFD_ASSERT (!elf_flags_init (obfd)
463 || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
465 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
466 elf_flags_init (obfd) = TRUE;
468 /* Copy object attributes. */
469 _bfd_elf_copy_obj_attributes (ibfd, obfd);
471 return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
474 static reloc_howto_type *
475 bfd_elf32_bfd_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED,
480 for (i = 0; i < ARRAY_SIZE (elf_arc_howto_table); i++)
481 if (elf_arc_howto_table[i].name != NULL
482 && strcasecmp (elf_arc_howto_table[i].name, r_name) == 0)
483 return arc_elf_howto (i);
488 /* Set the howto pointer for an ARC ELF reloc. */
491 arc_info_to_howto_rel (bfd * abfd ATTRIBUTE_UNUSED,
493 Elf_Internal_Rela * dst)
497 r_type = ELF32_R_TYPE (dst->r_info);
498 BFD_ASSERT (r_type < (unsigned int) R_ARC_max);
499 cache_ptr->howto = arc_elf_howto (r_type);
502 /* Merge backend specific data from an object file to the output
503 object file when linking. */
506 arc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
508 unsigned short mach_ibfd;
509 static unsigned short mach_obfd = EM_NONE;
514 /* Check if we have the same endianess. */
515 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
517 _bfd_error_handler (_("ERROR: Endian Match failed. Attempting to link "
518 "%B with binary %s of opposite endian-ness"),
519 ibfd, bfd_get_filename (obfd));
523 /* Collect ELF flags. */
524 in_flags = elf_elfheader (ibfd)->e_flags & EF_ARC_MACH_MSK;
525 out_flags = elf_elfheader (obfd)->e_flags & EF_ARC_MACH_MSK;
527 if (!elf_flags_init (obfd)) /* First call, no flags set. */
529 elf_flags_init (obfd) = TRUE;
530 out_flags = in_flags;
533 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
534 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
537 /* Check to see if the input BFD actually contains any sections. Do
538 not short-circuit dynamic objects; their section list may be
539 emptied by elf_link_add_object_symbols. */
540 if (!(ibfd->flags & DYNAMIC))
542 bfd_boolean null_input_bfd = TRUE;
543 bfd_boolean only_data_sections = TRUE;
545 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
547 if ((bfd_get_section_flags (ibfd, sec)
548 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
549 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
550 only_data_sections = FALSE;
552 null_input_bfd = FALSE;
555 if (null_input_bfd || only_data_sections)
559 /* Complain about various flag/architecture mismatches. */
560 mach_ibfd = elf_elfheader (ibfd)->e_machine;
561 if (mach_obfd == EM_NONE)
563 mach_obfd = mach_ibfd;
567 if (mach_ibfd != mach_obfd)
569 _bfd_error_handler (_("ERROR: Attempting to link %B "
570 "with a binary %s of different architecture"),
571 ibfd, bfd_get_filename (obfd));
574 else if (in_flags != out_flags)
576 /* Warn if different flags. */
577 (*_bfd_error_handler)
578 (_("%s: uses different e_flags (0x%lx) fields than "
579 "previous modules (0x%lx)"),
580 bfd_get_filename (ibfd), (long)in_flags, (long)out_flags);
581 if (in_flags && out_flags)
583 /* MWDT doesnt set the eflags hence make sure we choose the
584 eflags set by gcc. */
585 in_flags = in_flags > out_flags ? in_flags : out_flags;
589 /* Update the flags. */
590 elf_elfheader (obfd)->e_flags = in_flags;
592 if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
594 return bfd_set_arch_mach (obfd, bfd_arch_arc, bfd_get_mach (ibfd));
600 /* Set the right machine number for an ARC ELF file. */
602 arc_elf_object_p (bfd * abfd)
604 /* Make sure this is initialised, or you'll have the potential of passing
605 garbage---or misleading values---into the call to
606 bfd_default_set_arch_mach (). */
607 int mach = bfd_mach_arc_arc700;
608 unsigned long arch = elf_elfheader (abfd)->e_flags & EF_ARC_MACH_MSK;
609 unsigned e_machine = elf_elfheader (abfd)->e_machine;
611 if (e_machine == EM_ARC_COMPACT || e_machine == EM_ARC_COMPACT2)
615 case E_ARC_MACH_ARC600:
616 mach = bfd_mach_arc_arc600;
618 case E_ARC_MACH_ARC601:
619 mach = bfd_mach_arc_arc601;
621 case E_ARC_MACH_ARC700:
622 mach = bfd_mach_arc_arc700;
624 case E_ARC_MACH_NPS400:
625 mach = bfd_mach_arc_nps400;
627 case EF_ARC_CPU_ARCV2HS:
628 case EF_ARC_CPU_ARCV2EM:
629 mach = bfd_mach_arc_arcv2;
632 mach = (e_machine == EM_ARC_COMPACT) ?
633 bfd_mach_arc_arc700 : bfd_mach_arc_arcv2;
639 if (e_machine == EM_ARC)
641 (*_bfd_error_handler)
642 (_("Error: The ARC4 architecture is no longer supported.\n"));
647 (*_bfd_error_handler)
648 (_("Warning: unset or old architecture flags. \n"
649 " Use default machine.\n"));
653 return bfd_default_set_arch_mach (abfd, bfd_arch_arc, mach);
656 /* The final processing done just before writing out an ARC ELF object file.
657 This gets the ARC architecture right based on the machine number. */
660 arc_elf_final_write_processing (bfd * abfd,
661 bfd_boolean linker ATTRIBUTE_UNUSED)
665 switch (bfd_get_mach (abfd))
667 case bfd_mach_arc_arc600:
668 emf = EM_ARC_COMPACT;
670 case bfd_mach_arc_arc601:
671 emf = EM_ARC_COMPACT;
673 case bfd_mach_arc_arc700:
674 emf = EM_ARC_COMPACT;
676 case bfd_mach_arc_nps400:
677 emf = EM_ARC_COMPACT;
679 case bfd_mach_arc_arcv2:
680 emf = EM_ARC_COMPACT2;
686 elf_elfheader (abfd)->e_machine = emf;
688 /* Record whatever is the current syscall ABI version. */
689 elf_elfheader (abfd)->e_flags |= E_ARC_OSABI_CURRENT;
695 #define BFD_DEBUG_PIC(...)
697 struct arc_relocation_data
699 bfd_signed_vma reloc_offset;
700 bfd_signed_vma reloc_addend;
701 bfd_signed_vma got_offset_value;
703 bfd_signed_vma sym_value;
704 asection * sym_section;
706 reloc_howto_type *howto;
708 asection * input_section;
710 bfd_signed_vma sdata_begin_symbol_vma;
711 bfd_boolean sdata_begin_symbol_vma_set;
712 bfd_signed_vma got_symbol_vma;
714 bfd_boolean should_relocate;
716 const char * symbol_name;
720 debug_arc_reloc (struct arc_relocation_data reloc_data)
722 PR_DEBUG ("Reloc type=%s, should_relocate = %s\n",
723 reloc_data.howto->name,
724 reloc_data.should_relocate ? "true" : "false");
725 PR_DEBUG (" offset = 0x%x, addend = 0x%x\n",
726 (unsigned int) reloc_data.reloc_offset,
727 (unsigned int) reloc_data.reloc_addend);
728 PR_DEBUG (" Symbol:\n");
729 PR_DEBUG (" value = 0x%08x\n",
730 (unsigned int) reloc_data.sym_value);
731 if (reloc_data.sym_section != NULL)
733 PR_DEBUG (" Symbol Section:\n");
735 " section name = %s, output_offset 0x%08x",
736 reloc_data.sym_section->name,
737 (unsigned int) reloc_data.sym_section->output_offset);
738 if (reloc_data.sym_section->output_section != NULL)
741 ", output_section->vma = 0x%08x",
742 ((unsigned int) reloc_data.sym_section->output_section->vma));
745 PR_DEBUG (" file: %s\n", reloc_data.sym_section->owner->filename);
749 PR_DEBUG ( " symbol section is NULL\n");
752 PR_DEBUG ( " Input_section:\n");
753 if (reloc_data.input_section != NULL)
756 " section name = %s, output_offset 0x%08x, output_section->vma = 0x%08x\n",
757 reloc_data.input_section->name,
758 (unsigned int) reloc_data.input_section->output_offset,
759 (unsigned int) reloc_data.input_section->output_section->vma);
760 PR_DEBUG ( " changed_address = 0x%08x\n",
761 (unsigned int) (reloc_data.input_section->output_section->vma +
762 reloc_data.input_section->output_offset +
763 reloc_data.reloc_offset));
764 PR_DEBUG (" file: %s\n", reloc_data.input_section->owner->filename);
768 PR_DEBUG ( " input section is NULL\n");
773 middle_endian_convert (bfd_vma insn, bfd_boolean do_it)
778 ((insn & 0xffff0000) >> 16) |
779 ((insn & 0xffff) << 16);
784 /* This function is called for relocations that are otherwise marked as NOT
785 requiring overflow checks. In here we perform non-standard checks of
786 the relocation value. */
788 static inline bfd_reloc_status_type
789 arc_special_overflow_checks (const struct arc_relocation_data reloc_data,
790 bfd_signed_vma relocation,
791 struct bfd_link_info *info ATTRIBUTE_UNUSED)
793 switch (reloc_data.howto->type)
795 case R_ARC_NPS_CMEM16:
796 if (((relocation >> 16) & 0xffff) != NPS_CMEM_HIGH_VALUE)
798 if (reloc_data.reloc_addend == 0)
799 (*_bfd_error_handler)
800 (_("%B(%A+0x%lx): CMEM relocation to `%s' is invalid, "
801 "16 MSB should be 0x%04x (value is 0x%lx)"),
802 reloc_data.input_section->owner,
803 reloc_data.input_section,
804 reloc_data.reloc_offset,
805 reloc_data.symbol_name,
809 (*_bfd_error_handler)
810 (_("%B(%A+0x%lx): CMEM relocation to `%s+0x%lx' is invalid, "
811 "16 MSB should be 0x%04x (value is 0x%lx)"),
812 reloc_data.input_section->owner,
813 reloc_data.input_section,
814 reloc_data.reloc_offset,
815 reloc_data.symbol_name,
816 reloc_data.reloc_addend,
819 return bfd_reloc_overflow;
830 #define ME(reloc) (reloc)
832 #define IS_ME(FORMULA,BFD) ((strstr (FORMULA, "ME") != NULL) \
833 && (!bfd_big_endian (BFD)))
835 #define S ((bfd_signed_vma) (reloc_data.sym_value \
836 + (reloc_data.sym_section->output_section != NULL ? \
837 (reloc_data.sym_section->output_offset \
838 + reloc_data.sym_section->output_section->vma) : 0)))
839 #define L ((bfd_signed_vma) (reloc_data.sym_value \
840 + (reloc_data.sym_section->output_section != NULL ? \
841 (reloc_data.sym_section->output_offset \
842 + reloc_data.sym_section->output_section->vma) : 0)))
843 #define A (reloc_data.reloc_addend)
845 #define G (reloc_data.got_offset_value)
846 #define GOT (reloc_data.got_symbol_vma)
847 #define GOT_BEGIN (htab->sgot->output_section->vma)
850 /* P: relative offset to PCL The offset should be to the
851 current location aligned to 32 bits. */
852 #define P ((bfd_signed_vma) ( \
854 (reloc_data.input_section->output_section != NULL ? \
855 reloc_data.input_section->output_section->vma : 0) \
856 + reloc_data.input_section->output_offset \
857 + (reloc_data.reloc_offset - (bitsize >= 32 ? 4 : 0))) \
859 #define PDATA ((bfd_signed_vma) ( \
860 (reloc_data.input_section->output_section->vma \
861 + reloc_data.input_section->output_offset \
862 + (reloc_data.reloc_offset))))
863 #define SECTSTART (bfd_signed_vma) (reloc_data.sym_section->output_section->vma \
864 + reloc_data.sym_section->output_offset)
866 #define _SDA_BASE_ (bfd_signed_vma) (reloc_data.sdata_begin_symbol_vma)
867 #define TLS_REL (bfd_signed_vma) \
868 ((elf_hash_table (info))->tls_sec->output_section->vma)
874 #define PRINT_DEBUG_RELOC_INFO_BEFORE(FORMULA, TYPE) \
876 asection *sym_section = reloc_data.sym_section; \
877 asection *input_section = reloc_data.input_section; \
878 ARC_DEBUG ("RELOC_TYPE = " TYPE "\n"); \
879 ARC_DEBUG ("FORMULA = " FORMULA "\n"); \
880 ARC_DEBUG ("S = 0x%x\n", S); \
881 ARC_DEBUG ("A = 0x%x\n", A); \
882 ARC_DEBUG ("L = 0x%x\n", L); \
883 if (sym_section->output_section != NULL) \
885 ARC_DEBUG ("symbol_section->vma = 0x%x\n", \
886 sym_section->output_section->vma + sym_section->output_offset); \
890 ARC_DEBUG ("symbol_section->vma = NULL\n"); \
892 if (input_section->output_section != NULL) \
894 ARC_DEBUG ("symbol_section->vma = 0x%x\n", \
895 input_section->output_section->vma + input_section->output_offset); \
899 ARC_DEBUG ("symbol_section->vma = NULL\n"); \
901 ARC_DEBUG ("PCL = 0x%x\n", P); \
902 ARC_DEBUG ("P = 0x%x\n", P); \
903 ARC_DEBUG ("G = 0x%x\n", G); \
904 ARC_DEBUG ("SDA_OFFSET = 0x%x\n", _SDA_BASE_); \
905 ARC_DEBUG ("SDA_SET = %d\n", reloc_data.sdata_begin_symbol_vma_set); \
906 ARC_DEBUG ("GOT_OFFSET = 0x%x\n", GOT); \
907 ARC_DEBUG ("relocation = 0x%08x\n", relocation); \
908 ARC_DEBUG ("before = 0x%08x\n", (unsigned int) insn); \
909 ARC_DEBUG ("data = 0x%08x (%u) (%d)\n", (unsigned int) relocation, (unsigned int) relocation, (int) relocation); \
912 #define PRINT_DEBUG_RELOC_INFO_AFTER \
914 ARC_DEBUG ("after = 0x%08x\n", (unsigned int) insn); \
917 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
920 bfd_signed_vma bitsize ATTRIBUTE_UNUSED = BITSIZE; \
921 relocation = FORMULA ; \
922 PRINT_DEBUG_RELOC_INFO_BEFORE (#FORMULA, #TYPE); \
923 insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
924 insn = (* get_replace_function (abfd, TYPE)) (insn, relocation); \
925 insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
926 PRINT_DEBUG_RELOC_INFO_AFTER \
930 static bfd_reloc_status_type
931 arc_do_relocation (bfd_byte * contents,
932 struct arc_relocation_data reloc_data,
933 struct bfd_link_info *info)
935 bfd_signed_vma relocation = 0;
937 bfd_vma orig_insn ATTRIBUTE_UNUSED;
938 bfd * abfd = reloc_data.input_section->owner;
939 struct elf_link_hash_table *htab ATTRIBUTE_UNUSED = elf_hash_table (info);
940 bfd_reloc_status_type flag;
942 if (reloc_data.should_relocate == FALSE)
945 switch (reloc_data.howto->size)
948 insn = arc_bfd_get_32 (abfd,
949 contents + reloc_data.reloc_offset,
950 reloc_data.input_section);
953 insn = arc_bfd_get_16 (abfd,
954 contents + reloc_data.reloc_offset,
955 reloc_data.input_section);
958 insn = arc_bfd_get_8 (abfd,
959 contents + reloc_data.reloc_offset,
960 reloc_data.input_section);
970 switch (reloc_data.howto->type)
972 #include "elf/arc-reloc.def"
979 /* Check for relocation overflow. */
980 if (reloc_data.howto->complain_on_overflow != complain_overflow_dont)
981 flag = bfd_check_overflow (reloc_data.howto->complain_on_overflow,
982 reloc_data.howto->bitsize,
983 reloc_data.howto->rightshift,
984 bfd_arch_bits_per_address (abfd),
987 flag = arc_special_overflow_checks (reloc_data, relocation, info);
989 #undef DEBUG_ARC_RELOC
990 #define DEBUG_ARC_RELOC(A) debug_arc_reloc (A)
991 if (flag != bfd_reloc_ok)
993 PR_DEBUG ( "Relocation overflows !!!!\n");
995 DEBUG_ARC_RELOC (reloc_data);
998 "Relocation value = signed -> %d, unsigned -> %u"
999 ", hex -> (0x%08x)\n",
1001 (unsigned int) relocation,
1002 (unsigned int) relocation);
1005 #undef DEBUG_ARC_RELOC
1006 #define DEBUG_ARC_RELOC(A)
1008 /* Write updated instruction back to memory. */
1009 switch (reloc_data.howto->size)
1012 arc_bfd_put_32 (abfd, insn,
1013 contents + reloc_data.reloc_offset,
1014 reloc_data.input_section);
1017 arc_bfd_put_16 (abfd, insn,
1018 contents + reloc_data.reloc_offset,
1019 reloc_data.input_section);
1022 arc_bfd_put_8 (abfd, insn,
1023 contents + reloc_data.reloc_offset,
1024 reloc_data.input_section);
1027 ARC_DEBUG ("size = %d\n", reloc_data.howto->size);
1032 return bfd_reloc_ok;
1047 #undef ARC_RELOC_HOWTO
1049 static struct got_entry **
1050 arc_get_local_got_ents (bfd * abfd)
1052 static struct got_entry **local_got_ents = NULL;
1054 if (local_got_ents == NULL)
1057 Elf_Internal_Shdr *symtab_hdr = &((elf_tdata (abfd))->symtab_hdr);
1059 size = symtab_hdr->sh_info * sizeof (bfd_vma);
1060 local_got_ents = (struct got_entry **)
1061 bfd_alloc (abfd, sizeof(struct got_entry *) * size);
1062 if (local_got_ents == NULL)
1065 memset (local_got_ents, 0, sizeof(struct got_entry *) * size);
1066 elf_local_got_ents (abfd) = local_got_ents;
1069 return local_got_ents;
1072 /* Relocate an arc ELF section.
1073 Function : elf_arc_relocate_section
1074 Brief : Relocate an arc section, by handling all the relocations
1075 appearing in that section.
1076 Args : output_bfd : The bfd being written to.
1077 info : Link information.
1078 input_bfd : The input bfd.
1079 input_section : The section being relocated.
1080 contents : contents of the section being relocated.
1081 relocs : List of relocations in the section.
1082 local_syms : is a pointer to the swapped in local symbols.
1083 local_section : is an array giving the section in the input file
1084 corresponding to the st_shndx field of each
1087 elf_arc_relocate_section (bfd * output_bfd,
1088 struct bfd_link_info * info,
1090 asection * input_section,
1091 bfd_byte * contents,
1092 Elf_Internal_Rela * relocs,
1093 Elf_Internal_Sym * local_syms,
1094 asection ** local_sections)
1096 Elf_Internal_Shdr * symtab_hdr;
1097 struct elf_link_hash_entry ** sym_hashes;
1098 struct got_entry ** local_got_ents;
1099 Elf_Internal_Rela * rel;
1100 Elf_Internal_Rela * wrel;
1101 Elf_Internal_Rela * relend;
1102 struct elf_link_hash_table *htab = elf_hash_table (info);
1104 symtab_hdr = &((elf_tdata (input_bfd))->symtab_hdr);
1105 sym_hashes = elf_sym_hashes (input_bfd);
1107 rel = wrel = relocs;
1108 relend = relocs + input_section->reloc_count;
1109 for (; rel < relend; wrel++, rel++)
1111 enum elf_arc_reloc_type r_type;
1112 reloc_howto_type * howto;
1113 unsigned long r_symndx;
1114 struct elf_link_hash_entry * h;
1115 Elf_Internal_Sym * sym;
1117 struct elf_link_hash_entry *h2;
1119 struct arc_relocation_data reloc_data =
1123 .got_offset_value = 0,
1125 .sym_section = NULL,
1127 .input_section = NULL,
1128 .sdata_begin_symbol_vma = 0,
1129 .sdata_begin_symbol_vma_set = FALSE,
1130 .got_symbol_vma = 0,
1131 .should_relocate = FALSE
1134 r_type = ELF32_R_TYPE (rel->r_info);
1136 if (r_type >= (int) R_ARC_max)
1138 bfd_set_error (bfd_error_bad_value);
1141 howto = arc_elf_howto (r_type);
1143 r_symndx = ELF32_R_SYM (rel->r_info);
1145 /* If we are generating another .o file and the symbol in not
1146 local, skip this relocation. */
1147 if (bfd_link_relocatable (info))
1149 /* This is a relocateable link. We don't have to change
1150 anything, unless the reloc is against a section symbol,
1151 in which case we have to adjust according to where the
1152 section symbol winds up in the output section. */
1154 /* Checks if this is a local symbol and thus the reloc
1155 might (will??) be against a section symbol. */
1156 if (r_symndx < symtab_hdr->sh_info)
1158 sym = local_syms + r_symndx;
1159 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1161 sec = local_sections[r_symndx];
1163 /* for RELA relocs.Just adjust the addend
1164 value in the relocation entry. */
1165 rel->r_addend += sec->output_offset + sym->st_value;
1168 PR_DEBUG ("local symbols reloc "
1169 "(section=%d %s) seen in %s\n",
1171 local_sections[r_symndx]->name,
1172 __PRETTY_FUNCTION__)
1178 h2 = elf_link_hash_lookup (elf_hash_table (info), "__SDATA_BEGIN__",
1179 FALSE, FALSE, TRUE);
1181 if (reloc_data.sdata_begin_symbol_vma_set == FALSE
1182 && h2 != NULL && h2->root.type != bfd_link_hash_undefined
1183 && h2->root.u.def.section->output_section != NULL)
1184 /* TODO: Verify this condition. */
1186 reloc_data.sdata_begin_symbol_vma =
1187 (h2->root.u.def.value +
1188 h2->root.u.def.section->output_section->vma);
1189 reloc_data.sdata_begin_symbol_vma_set = TRUE;
1192 reloc_data.input_section = input_section;
1193 reloc_data.howto = howto;
1194 reloc_data.reloc_offset = rel->r_offset;
1195 reloc_data.reloc_addend = rel->r_addend;
1197 /* This is a final link. */
1202 if (r_symndx < symtab_hdr->sh_info) /* A local symbol. */
1204 sym = local_syms + r_symndx;
1205 sec = local_sections[r_symndx];
1209 /* TODO: This code is repeated from below. We should
1210 clean it and remove duplications.
1211 Sec is used check for discarded sections.
1212 Need to redesign code below. */
1214 /* Get the symbol's entry in the symtab. */
1215 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1217 while (h->root.type == bfd_link_hash_indirect
1218 || h->root.type == bfd_link_hash_warning)
1219 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1221 /* If we have encountered a definition for this symbol. */
1222 if (h->root.type == bfd_link_hash_defined
1223 || h->root.type == bfd_link_hash_defweak)
1225 reloc_data.sym_value = h->root.u.def.value;
1226 sec = h->root.u.def.section;
1230 /* Clean relocs for symbols in discarded sections. */
1231 if (sec != NULL && discarded_section (sec))
1233 _bfd_clear_contents (howto, input_bfd, input_section,
1234 contents + rel->r_offset);
1235 rel->r_offset = rel->r_offset;
1239 /* For ld -r, remove relocations in debug sections against
1240 sections defined in discarded sections. Not done for
1241 eh_frame editing code expects to be present. */
1242 if (bfd_link_relocatable (info)
1243 && (input_section->flags & SEC_DEBUGGING))
1249 if (bfd_link_relocatable (info))
1256 if (r_symndx < symtab_hdr->sh_info) /* A local symbol. */
1258 struct got_entry *entry;
1260 local_got_ents = arc_get_local_got_ents (output_bfd);
1261 entry = local_got_ents[r_symndx];
1263 reloc_data.sym_value = sym->st_value;
1264 reloc_data.sym_section = sec;
1265 reloc_data.symbol_name =
1266 bfd_elf_string_from_elf_section (input_bfd,
1267 symtab_hdr->sh_link,
1270 /* Mergeable section handling. */
1271 if ((sec->flags & SEC_MERGE)
1272 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1276 rel->r_addend = _bfd_elf_rel_local_sym (output_bfd, sym,
1277 &msec, rel->r_addend);
1278 rel->r_addend -= (sec->output_section->vma
1279 + sec->output_offset
1281 rel->r_addend += msec->output_section->vma + msec->output_offset;
1283 reloc_data.reloc_addend = rel->r_addend;
1286 if ((is_reloc_for_GOT (howto)
1287 || is_reloc_for_TLS (howto)) && entry != NULL)
1289 if (is_reloc_for_TLS (howto))
1290 while (entry->type == GOT_NORMAL && entry->next != NULL)
1291 entry = entry->next;
1293 if (is_reloc_for_GOT (howto))
1294 while (entry->type != GOT_NORMAL && entry->next != NULL)
1295 entry = entry->next;
1297 if (entry->type == GOT_TLS_GD && entry->processed == FALSE)
1299 bfd_vma sym_vma = sym->st_value
1300 + sec->output_section->vma
1301 + sec->output_offset;
1303 /* Create dynamic relocation for local sym. */
1304 ADD_RELA (output_bfd, got, entry->offset, 0,
1305 R_ARC_TLS_DTPMOD, 0);
1306 ADD_RELA (output_bfd, got, entry->offset+4, 0,
1307 R_ARC_TLS_DTPOFF, 0);
1309 bfd_vma sec_vma = sec->output_section->vma
1310 + sec->output_offset;
1311 bfd_put_32 (output_bfd, sym_vma - sec_vma,
1312 htab->sgot->contents + entry->offset + 4);
1314 ARC_DEBUG ("arc_info: FIXED -> GOT_TLS_GD value "
1315 "= 0x%x @ 0x%x, for symbol %s\n",
1317 htab->sgot->contents + entry->offset + 4,
1320 entry->processed = TRUE;
1322 if (entry->type == GOT_TLS_IE && entry->processed == FALSE)
1324 bfd_vma sym_vma = sym->st_value
1325 + sec->output_section->vma
1326 + sec->output_offset;
1327 bfd_vma sec_vma = htab->tls_sec->output_section->vma;
1328 bfd_put_32 (output_bfd, sym_vma - sec_vma,
1329 htab->sgot->contents + entry->offset);
1330 /* TODO: Check if this type of relocs is the cause
1331 for all the ARC_NONE dynamic relocs. */
1333 ARC_DEBUG ("arc_info: FIXED -> GOT_TLS_IE value = "
1334 "0x%x @ 0x%x, for symbol %s\n",
1336 htab->sgot->contents + entry->offset,
1339 entry->processed = TRUE;
1341 if (entry->type == GOT_NORMAL && entry->processed == FALSE)
1343 bfd_vma sec_vma = reloc_data.sym_section->output_section->vma
1344 + reloc_data.sym_section->output_offset;
1346 bfd_put_32 (output_bfd, reloc_data.sym_value + sec_vma,
1347 htab->sgot->contents + entry->offset);
1349 ARC_DEBUG ("arc_info: PATCHED: 0x%08x @ 0x%08x for "
1350 "sym %s in got offset 0x%x\n",
1351 reloc_data.sym_value + sec_vma,
1352 htab->sgot->output_section->vma
1353 + htab->sgot->output_offset + entry->offset,
1356 entry->processed = TRUE;
1359 reloc_data.got_offset_value = entry->offset;
1360 ARC_DEBUG ("arc_info: GOT_ENTRY = %d, offset = 0x%x, "
1361 "vma = 0x%x for symbol %s\n",
1362 entry->type, entry->offset,
1363 htab->sgot->output_section->vma
1364 + htab->sgot->output_offset + entry->offset,
1368 BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto));
1369 if (htab->sgot != NULL)
1370 reloc_data.got_symbol_vma = htab->sgot->output_section->vma
1371 + htab->sgot->output_offset;
1373 reloc_data.should_relocate = TRUE;
1375 else /* Global symbol. */
1377 /* Get the symbol's entry in the symtab. */
1378 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1380 while (h->root.type == bfd_link_hash_indirect
1381 || h->root.type == bfd_link_hash_warning)
1382 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1384 /* TODO: Need to validate what was the intention. */
1385 /* BFD_ASSERT ((h->dynindx == -1) || (h->forced_local != 0)); */
1386 reloc_data.symbol_name = h->root.root.string;
1388 /* If we have encountered a definition for this symbol. */
1389 if (h->root.type == bfd_link_hash_defined
1390 || h->root.type == bfd_link_hash_defweak)
1392 reloc_data.sym_value = h->root.u.def.value;
1393 reloc_data.sym_section = h->root.u.def.section;
1395 reloc_data.should_relocate = TRUE;
1397 if (is_reloc_for_GOT (howto) && !bfd_link_pic (info))
1399 /* TODO: Change it to use arc_do_relocation with
1400 ARC_32 reloc. Try to use ADD_RELA macro. */
1401 bfd_vma relocation =
1402 reloc_data.sym_value + reloc_data.reloc_addend
1403 + (reloc_data.sym_section->output_section != NULL ?
1404 (reloc_data.sym_section->output_offset
1405 + reloc_data.sym_section->output_section->vma)
1408 BFD_ASSERT (h->got.glist);
1409 bfd_vma got_offset = h->got.glist->offset;
1410 bfd_put_32 (output_bfd, relocation,
1411 htab->sgot->contents + got_offset);
1413 if (is_reloc_for_PLT (howto) && h->plt.offset != (bfd_vma) -1)
1415 /* TODO: This is repeated up here. */
1416 reloc_data.sym_value = h->plt.offset;
1417 reloc_data.sym_section = htab->splt;
1420 else if (h->root.type == bfd_link_hash_undefweak)
1422 /* Is weak symbol and has no definition. */
1423 if (is_reloc_for_GOT (howto))
1425 reloc_data.sym_value = h->root.u.def.value;
1426 reloc_data.sym_section = htab->sgot;
1427 reloc_data.should_relocate = TRUE;
1429 else if (is_reloc_for_PLT (howto)
1430 && h->plt.offset != (bfd_vma) -1)
1432 /* TODO: This is repeated up here. */
1433 reloc_data.sym_value = h->plt.offset;
1434 reloc_data.sym_section = htab->splt;
1435 reloc_data.should_relocate = TRUE;
1442 if (is_reloc_for_GOT (howto))
1444 reloc_data.sym_value = h->root.u.def.value;
1445 reloc_data.sym_section = htab->sgot;
1447 reloc_data.should_relocate = TRUE;
1449 else if (is_reloc_for_PLT (howto))
1451 /* Fail if it is linking for PIE and the symbol is
1453 if (bfd_link_executable (info))
1454 (*info->callbacks->undefined_symbol)
1455 (info, h->root.root.string, input_bfd, input_section,
1456 rel->r_offset, TRUE);
1457 reloc_data.sym_value = h->plt.offset;
1458 reloc_data.sym_section = htab->splt;
1460 reloc_data.should_relocate = TRUE;
1462 else if (!bfd_link_pic (info))
1463 (*info->callbacks->undefined_symbol)
1464 (info, h->root.root.string, input_bfd, input_section,
1465 rel->r_offset, TRUE);
1468 if (h->got.glist != NULL)
1470 struct got_entry *entry = h->got.glist;
1472 if (is_reloc_for_GOT (howto) || is_reloc_for_TLS (howto))
1474 if (! elf_hash_table (info)->dynamic_sections_created
1475 || (bfd_link_pic (info)
1476 && SYMBOL_REFERENCES_LOCAL (info, h)))
1478 reloc_data.sym_value = h->root.u.def.value;
1479 reloc_data.sym_section = h->root.u.def.section;
1481 if (is_reloc_for_TLS (howto))
1482 while (entry->type == GOT_NORMAL && entry->next != NULL)
1483 entry = entry->next;
1485 if (entry->processed == FALSE
1486 && (entry->type == GOT_TLS_GD
1487 || entry->type == GOT_TLS_IE))
1489 bfd_vma sym_value = h->root.u.def.value
1490 + h->root.u.def.section->output_section->vma
1491 + h->root.u.def.section->output_offset;
1494 elf_hash_table (info)->tls_sec->output_section->vma;
1496 bfd_put_32 (output_bfd,
1497 sym_value - sec_vma,
1498 htab->sgot->contents + entry->offset
1499 + (entry->existing_entries == TLS_GOT_MOD_AND_OFF ? 4 : 0));
1501 ARC_DEBUG ("arc_info: FIXED -> %s value = 0x%x "
1502 "@ 0x%x, for symbol %s\n",
1503 (entry->type == GOT_TLS_GD ? "GOT_TLS_GD" :
1505 sym_value - sec_vma,
1506 htab->sgot->contents + entry->offset
1507 + (entry->existing_entries == TLS_GOT_MOD_AND_OFF ? 4 : 0),
1508 h->root.root.string);
1510 entry->processed = TRUE;
1513 if (entry->type == GOT_TLS_IE && entry->processed == FALSE)
1515 bfd_vma sec_vma = htab->tls_sec->output_section->vma;
1516 bfd_put_32 (output_bfd,
1517 reloc_data.sym_value - sec_vma,
1518 htab->sgot->contents + entry->offset);
1521 if (entry->type == GOT_NORMAL && entry->processed == FALSE)
1524 reloc_data.sym_section->output_section->vma
1525 + reloc_data.sym_section->output_offset;
1527 if (h->root.type != bfd_link_hash_undefweak)
1529 bfd_put_32 (output_bfd,
1530 reloc_data.sym_value + sec_vma,
1531 htab->sgot->contents + entry->offset);
1533 ARC_DEBUG ("arc_info: PATCHED: 0x%08x "
1534 "@ 0x%08x for sym %s in got offset 0x%x\n",
1535 reloc_data.sym_value + sec_vma,
1536 htab->sgot->output_section->vma
1537 + htab->sgot->output_offset + entry->offset,
1538 h->root.root.string,
1543 ARC_DEBUG ("arc_info: PATCHED: NOT_PATCHED "
1544 "@ 0x%08x for sym %s in got offset 0x%x "
1546 htab->sgot->output_section->vma
1547 + htab->sgot->output_offset + entry->offset,
1548 h->root.root.string,
1552 entry->processed = TRUE;
1557 reloc_data.got_offset_value = entry->offset;
1559 ARC_DEBUG ("arc_info: GOT_ENTRY = %d, offset = 0x%x, "
1560 "vma = 0x%x for symbol %s\n",
1561 entry->type, entry->offset,
1562 htab->sgot->output_section->vma
1563 + htab->sgot->output_offset + entry->offset,
1564 h->root.root.string);
1567 BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto));
1568 if (htab->sgot != NULL)
1569 reloc_data.got_symbol_vma = htab->sgot->output_section->vma
1570 + htab->sgot->output_offset;
1578 case R_ARC_32_PCREL:
1579 if ((bfd_link_pic (info) || bfd_link_pie (info))
1580 && ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL)
1583 && (!info->symbolic || !h->def_regular))))
1585 Elf_Internal_Rela outrel;
1587 bfd_boolean skip = FALSE;
1588 bfd_boolean relocate = FALSE;
1589 asection *sreloc = _bfd_elf_get_dynamic_reloc_section
1590 (input_bfd, input_section,
1593 BFD_ASSERT (sreloc != NULL);
1595 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
1599 if (outrel.r_offset == (bfd_vma) -1)
1602 outrel.r_addend = rel->r_addend;
1603 outrel.r_offset += (input_section->output_section->vma
1604 + input_section->output_offset);
1606 #define IS_ARC_PCREL_TYPE(TYPE) \
1607 ( (TYPE == R_ARC_PC32) \
1608 || (TYPE == R_ARC_32_PCREL))
1611 memset (&outrel, 0, sizeof outrel);
1616 && ((IS_ARC_PCREL_TYPE (r_type))
1617 || !(bfd_link_executable (info)
1618 || SYMBOLIC_BIND (info, h))
1619 || ! h->def_regular))
1621 BFD_ASSERT (h != NULL);
1622 if ((input_section->flags & SEC_ALLOC) != 0)
1627 BFD_ASSERT (h->dynindx != -1);
1628 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1632 /* Handle local symbols, they either do not have a
1633 global hash table entry (h == NULL), or are
1634 forced local due to a version script
1635 (h->forced_local), or the third condition is
1636 legacy, it appears to say something like, for
1637 links where we are pre-binding the symbols, or
1638 there's not an entry for this symbol in the
1639 dynamic symbol table, and it's a regular symbol
1640 not defined in a shared object, then treat the
1641 symbol as local, resolve it now. */
1643 /* outrel.r_addend = 0; */
1644 outrel.r_info = ELF32_R_INFO (0, R_ARC_RELATIVE);
1647 BFD_ASSERT (sreloc->contents != 0);
1649 loc = sreloc->contents;
1650 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
1651 sreloc->reloc_count += 1;
1653 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1655 if (relocate == FALSE)
1663 if (is_reloc_SDA_relative (howto)
1664 && (reloc_data.sdata_begin_symbol_vma_set == FALSE))
1666 (*_bfd_error_handler)
1667 ("Error: Linker symbol __SDATA_BEGIN__ not found");
1668 bfd_set_error (bfd_error_bad_value);
1672 DEBUG_ARC_RELOC (reloc_data);
1674 /* Make sure we have with a dynamic linker. In case of GOT and PLT
1675 the sym_section should point to .got or .plt respectively. */
1676 if ((is_reloc_for_GOT (howto) || is_reloc_for_PLT (howto))
1677 && reloc_data.sym_section == NULL)
1679 (*_bfd_error_handler)
1680 (_("GOT and PLT relocations cannot be fixed with a non dynamic linker."));
1681 bfd_set_error (bfd_error_bad_value);
1685 if (arc_do_relocation (contents, reloc_data, info) != bfd_reloc_ok)
1692 static struct dynamic_sections
1693 arc_create_dynamic_sections (bfd * abfd, struct bfd_link_info *info)
1695 struct elf_link_hash_table *htab;
1697 struct dynamic_sections ds =
1699 .initialized = FALSE,
1709 htab = elf_hash_table (info);
1712 /* Create dynamic sections for relocatable executables so that we
1713 can copy relocations. */
1714 if (! htab->dynamic_sections_created && bfd_link_pic (info))
1716 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
1720 dynobj = (elf_hash_table (info))->dynobj;
1724 ds.sgot = htab->sgot;
1725 ds.srelgot = htab->srelgot;
1727 ds.sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1728 ds.srelgotplt = ds.srelplt;
1730 ds.splt = bfd_get_section_by_name (dynobj, ".plt");
1731 ds.srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
1734 if (htab->dynamic_sections_created)
1736 ds.sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1739 ds.initialized = TRUE;
1744 #define ADD_SYMBOL_REF_SEC_AND_RELOC(SECNAME, COND_FOR_RELOC, H) \
1745 htab->s##SECNAME->size; \
1747 if (COND_FOR_RELOC) \
1749 htab->srel##SECNAME->size += sizeof (Elf32_External_Rela); \
1750 ARC_DEBUG ("arc_info: Added reloc space in " \
1751 #SECNAME " section at " __FILE__ \
1752 ":%d for symbol\n", \
1753 __LINE__, name_for_global_symbol (H)); \
1756 if (h->dynindx == -1 && !h->forced_local) \
1757 if (! bfd_elf_link_record_dynamic_symbol (info, H)) \
1759 htab->s##SECNAME->size += 4; \
1763 elf_arc_check_relocs (bfd * abfd,
1764 struct bfd_link_info * info,
1766 const Elf_Internal_Rela * relocs)
1768 Elf_Internal_Shdr * symtab_hdr;
1769 struct elf_link_hash_entry ** sym_hashes;
1770 struct got_entry ** local_got_ents;
1771 const Elf_Internal_Rela * rel;
1772 const Elf_Internal_Rela * rel_end;
1774 asection * sreloc = NULL;
1775 struct elf_link_hash_table * htab = elf_hash_table (info);
1777 if (bfd_link_relocatable (info))
1780 dynobj = (elf_hash_table (info))->dynobj;
1781 symtab_hdr = &((elf_tdata (abfd))->symtab_hdr);
1782 sym_hashes = elf_sym_hashes (abfd);
1783 local_got_ents = arc_get_local_got_ents (abfd);
1785 rel_end = relocs + sec->reloc_count;
1786 for (rel = relocs; rel < rel_end; rel++)
1788 enum elf_arc_reloc_type r_type;
1789 reloc_howto_type *howto;
1790 unsigned long r_symndx;
1791 struct elf_link_hash_entry *h;
1793 r_type = ELF32_R_TYPE (rel->r_info);
1795 if (r_type >= (int) R_ARC_max)
1797 bfd_set_error (bfd_error_bad_value);
1800 howto = arc_elf_howto (r_type);
1803 && (is_reloc_for_GOT (howto) == TRUE
1804 || is_reloc_for_TLS (howto) == TRUE))
1806 dynobj = elf_hash_table (info)->dynobj = abfd;
1807 if (! _bfd_elf_create_got_section (abfd, info))
1811 /* Load symbol information. */
1812 r_symndx = ELF32_R_SYM (rel->r_info);
1813 if (r_symndx < symtab_hdr->sh_info) /* Is a local symbol. */
1815 else /* Global one. */
1816 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1822 /* During shared library creation, these relocs should not
1823 appear in a shared library (as memory will be read only
1824 and the dynamic linker can not resolve these. However
1825 the error should not occur for e.g. debugging or
1826 non-readonly sections. */
1827 if ((bfd_link_dll (info) && !bfd_link_pie (info))
1828 && (sec->flags & SEC_ALLOC) != 0
1829 && (sec->flags & SEC_READONLY) != 0
1830 && ((sec->flags & SEC_CODE) != 0
1831 || (sec->flags & SEC_DEBUGGING) != 0))
1835 name = h->root.root.string;
1837 /* bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL); */
1839 (*_bfd_error_handler)
1841 %B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1843 arc_elf_howto (r_type)->name,
1845 bfd_set_error (bfd_error_bad_value);
1849 /* In some cases we are not setting the 'non_got_ref'
1850 flag, even though the relocations don't require a GOT
1851 access. We should extend the testing in this area to
1852 ensure that no significant cases are being missed. */
1857 case R_ARC_32_PCREL:
1858 if ((bfd_link_pic (info) || bfd_link_pie (info))
1859 && ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL)
1862 && (!info->symbolic || !h->def_regular))))
1866 sreloc = _bfd_elf_make_dynamic_reloc_section (sec, dynobj,
1874 sreloc->size += sizeof (Elf32_External_Rela);
1881 if (is_reloc_for_PLT (howto) == TRUE)
1889 if (is_reloc_for_GOT (howto) == TRUE)
1894 if (local_got_ents[r_symndx] == NULL)
1897 ADD_SYMBOL_REF_SEC_AND_RELOC (got,
1898 bfd_link_pic (info),
1900 new_got_entry_to_list (&(local_got_ents[r_symndx]),
1901 GOT_NORMAL, offset, TLS_GOT_NONE);
1906 /* Global symbol. */
1907 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1908 if (h->got.glist == NULL)
1911 ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
1912 new_got_entry_to_list (&h->got.glist,
1913 GOT_NORMAL, offset, TLS_GOT_NONE);
1918 if (is_reloc_for_TLS (howto) == TRUE)
1920 enum tls_type_e type = GOT_UNKNOWN;
1924 case R_ARC_TLS_GD_GOT:
1927 case R_ARC_TLS_IE_GOT:
1934 struct got_entry **list = NULL;
1936 list = &(h->got.glist);
1938 list = &(local_got_ents[r_symndx]);
1940 if (type != GOT_UNKNOWN && !symbol_has_entry_of_type (*list, type))
1942 enum tls_got_entries entries = TLS_GOT_NONE;
1944 ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
1946 if (type == GOT_TLS_GD)
1948 bfd_vma ATTRIBUTE_UNUSED notneeded =
1949 ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
1950 entries = TLS_GOT_MOD_AND_OFF;
1953 if (entries == TLS_GOT_NONE)
1954 entries = TLS_GOT_OFF;
1956 new_got_entry_to_list (list, type, offset, entries);
1964 #define ELF_DYNAMIC_INTERPRETER "/sbin/ld-uClibc.so"
1966 static struct plt_version_t *
1967 arc_get_plt_version (struct bfd_link_info *info)
1971 for (i = 0; i < 1; i++)
1973 ARC_DEBUG ("%d: size1 = %d, size2 = %d\n", i,
1974 plt_versions[i].entry_size,
1975 plt_versions[i].elem_size);
1978 if (bfd_get_mach (info->output_bfd) == bfd_mach_arc_arcv2)
1980 if (bfd_link_pic (info))
1981 return &(plt_versions[ELF_ARCV2_PIC]);
1983 return &(plt_versions[ELF_ARCV2_ABS]);
1987 if (bfd_link_pic (info))
1988 return &(plt_versions[ELF_ARC_PIC]);
1990 return &(plt_versions[ELF_ARC_ABS]);
1995 add_symbol_to_plt (struct bfd_link_info *info)
1997 struct elf_link_hash_table *htab = elf_hash_table (info);
2000 struct plt_version_t *plt_data = arc_get_plt_version (info);
2002 /* If this is the first .plt entry, make room for the special first
2004 if (htab->splt->size == 0)
2005 htab->splt->size += plt_data->entry_size;
2007 ret = htab->splt->size;
2009 htab->splt->size += plt_data->elem_size;
2010 ARC_DEBUG ("PLT_SIZE = %d\n", htab->splt->size);
2012 htab->sgotplt->size += 4;
2013 htab->srelplt->size += sizeof (Elf32_External_Rela);
2018 #define PLT_DO_RELOCS_FOR_ENTRY(ABFD, DS, RELOCS) \
2019 plt_do_relocs_for_symbol (ABFD, DS, RELOCS, 0, 0)
2022 plt_do_relocs_for_symbol (bfd *abfd,
2023 struct elf_link_hash_table *htab,
2024 const struct plt_reloc *reloc,
2026 bfd_vma symbol_got_offset)
2028 while (SYM_ONLY (reloc->symbol) != LAST_RELOC)
2030 bfd_vma relocation = 0;
2032 switch (SYM_ONLY (reloc->symbol))
2036 htab->sgotplt->output_section->vma +
2037 htab->sgotplt->output_offset + symbol_got_offset;
2040 relocation += reloc->addend;
2042 if (IS_RELATIVE (reloc->symbol))
2044 bfd_vma reloc_offset = reloc->offset;
2045 reloc_offset -= (IS_INSN_32 (reloc->symbol)) ? 4 : 0;
2046 reloc_offset -= (IS_INSN_24 (reloc->symbol)) ? 2 : 0;
2048 relocation -= htab->splt->output_section->vma
2049 + htab->splt->output_offset
2050 + plt_offset + reloc_offset;
2053 /* TODO: being ME is not a property of the relocation but of the
2054 section of which is applying the relocation. */
2055 if (IS_MIDDLE_ENDIAN (reloc->symbol) && !bfd_big_endian (abfd))
2058 ((relocation & 0xffff0000) >> 16) |
2059 ((relocation & 0xffff) << 16);
2062 switch (reloc->size)
2065 bfd_put_32 (htab->splt->output_section->owner,
2067 htab->splt->contents + plt_offset + reloc->offset);
2071 reloc = &(reloc[1]); /* Jump to next relocation. */
2076 relocate_plt_for_symbol (bfd *output_bfd,
2077 struct bfd_link_info *info,
2078 struct elf_link_hash_entry *h)
2080 struct plt_version_t *plt_data = arc_get_plt_version (info);
2081 struct elf_link_hash_table *htab = elf_hash_table (info);
2083 bfd_vma plt_index = (h->plt.offset - plt_data->entry_size)
2084 / plt_data->elem_size;
2085 bfd_vma got_offset = (plt_index + 3) * 4;
2087 ARC_DEBUG ("arc_info: PLT_OFFSET = 0x%x, PLT_ENTRY_VMA = 0x%x, \
2088 GOT_ENTRY_OFFSET = 0x%x, GOT_ENTRY_VMA = 0x%x, for symbol %s\n",
2090 htab->splt->output_section->vma
2091 + htab->splt->output_offset
2094 htab->sgotplt->output_section->vma
2095 + htab->sgotplt->output_offset
2097 h->root.root.string);
2102 uint16_t *ptr = (uint16_t *) plt_data->elem;
2103 for (i = 0; i < plt_data->elem_size/2; i++)
2105 uint16_t data = ptr[i];
2106 bfd_put_16 (output_bfd,
2108 htab->splt->contents + h->plt.offset + (i*2));
2112 plt_do_relocs_for_symbol (output_bfd, htab,
2113 plt_data->elem_relocs,
2117 /* Fill in the entry in the global offset table. */
2118 bfd_put_32 (output_bfd,
2119 (bfd_vma) (htab->splt->output_section->vma
2120 + htab->splt->output_offset),
2121 htab->sgotplt->contents + got_offset);
2123 /* TODO: Fill in the entry in the .rela.plt section. */
2125 Elf_Internal_Rela rel;
2128 rel.r_offset = (htab->sgotplt->output_section->vma
2129 + htab->sgotplt->output_offset
2133 BFD_ASSERT (h->dynindx != -1);
2134 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_JMP_SLOT);
2136 loc = htab->srelplt->contents;
2137 loc += plt_index * sizeof (Elf32_External_Rela); /* relA */
2138 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2143 relocate_plt_for_entry (bfd *abfd,
2144 struct bfd_link_info *info)
2146 struct plt_version_t *plt_data = arc_get_plt_version (info);
2147 struct elf_link_hash_table *htab = elf_hash_table (info);
2151 uint16_t *ptr = (uint16_t *) plt_data->entry;
2152 for (i = 0; i < plt_data->entry_size/2; i++)
2154 uint16_t data = ptr[i];
2157 htab->splt->contents + (i*2));
2160 PLT_DO_RELOCS_FOR_ENTRY (abfd, htab, plt_data->entry_relocs);
2163 /* Desc : Adjust a symbol defined by a dynamic object and referenced
2164 by a regular object. The current definition is in some section of
2165 the dynamic object, but we're not including those sections. We
2166 have to change the definition to something the rest of the link can
2170 elf_arc_adjust_dynamic_symbol (struct bfd_link_info *info,
2171 struct elf_link_hash_entry *h)
2174 bfd *dynobj = (elf_hash_table (info))->dynobj;
2175 struct elf_link_hash_table *htab = elf_hash_table (info);
2177 if (h->type == STT_FUNC
2178 || h->type == STT_GNU_IFUNC
2179 || h->needs_plt == 1)
2181 if (!bfd_link_pic (info) && !h->def_dynamic && !h->ref_dynamic)
2183 /* This case can occur if we saw a PLT32 reloc in an input
2184 file, but the symbol was never referred to by a dynamic
2185 object. In such a case, we don't actually need to build
2186 a procedure linkage table, and we can just do a PC32
2188 BFD_ASSERT (h->needs_plt);
2192 /* Make sure this symbol is output as a dynamic symbol. */
2193 if (h->dynindx == -1 && !h->forced_local
2194 && !bfd_elf_link_record_dynamic_symbol (info, h))
2197 if (bfd_link_pic (info)
2198 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2200 bfd_vma loc = add_symbol_to_plt (info);
2202 if (!bfd_link_pic (info) && !h->def_regular)
2204 h->root.u.def.section = htab->splt;
2205 h->root.u.def.value = loc;
2207 h->plt.offset = loc;
2211 h->plt.offset = (bfd_vma) -1;
2217 /* If this is a weak symbol, and there is a real definition, the
2218 processor independent code will have arranged for us to see the
2219 real definition first, and we can just use the same value. */
2220 if (h->u.weakdef != NULL)
2222 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2223 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2224 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2225 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2229 /* This is a reference to a symbol defined by a dynamic object which
2230 is not a function. */
2232 /* If we are creating a shared library, we must presume that the
2233 only references to the symbol are via the global offset table.
2234 For such cases we need not do anything here; the relocations will
2235 be handled correctly by relocate_section. */
2236 if (!bfd_link_executable (info))
2239 /* If there are no non-GOT references, we do not need a copy
2241 if (!h->non_got_ref)
2244 /* If -z nocopyreloc was given, we won't generate them either. */
2245 if (info->nocopyreloc)
2251 /* We must allocate the symbol in our .dynbss section, which will
2252 become part of the .bss section of the executable. There will be
2253 an entry for this symbol in the .dynsym section. The dynamic
2254 object will contain position independent code, so all references
2255 from the dynamic object to this symbol will go through the global
2256 offset table. The dynamic linker will use the .dynsym entry to
2257 determine the address it must put in the global offset table, so
2258 both the dynamic object and the regular object will refer to the
2259 same memory location for the variable. */
2264 /* We must generate a R_ARC_COPY reloc to tell the dynamic linker to
2265 copy the initial value out of the dynamic object and into the
2266 runtime process image. We need to remember the offset into the
2267 .rela.bss section we are going to use. */
2268 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2272 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2273 BFD_ASSERT (srel != NULL);
2274 srel->size += sizeof (Elf32_External_Rela);
2278 s = bfd_get_section_by_name (dynobj, ".dynbss");
2279 BFD_ASSERT (s != NULL);
2281 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2284 /* Function : elf_arc_finish_dynamic_symbol
2285 Brief : Finish up dynamic symbol handling. We set the
2286 contents of various dynamic sections here.
2291 Returns : True/False as the return status. */
2294 elf_arc_finish_dynamic_symbol (bfd * output_bfd,
2295 struct bfd_link_info *info,
2296 struct elf_link_hash_entry *h,
2297 Elf_Internal_Sym * sym)
2299 if (h->plt.offset != (bfd_vma) -1)
2301 relocate_plt_for_symbol (output_bfd, info, h);
2303 if (!h->def_regular)
2305 /* Mark the symbol as undefined, rather than as defined in
2306 the .plt section. Leave the value alone. */
2307 sym->st_shndx = SHN_UNDEF;
2311 if (h->got.glist != NULL)
2313 struct got_entry *list = h->got.glist;
2315 /* Traverse the list of got entries for this symbol. */
2318 bfd_vma got_offset = h->got.glist->offset;
2320 if (list->type == GOT_NORMAL
2321 && list->created_dyn_relocation == FALSE)
2323 if (bfd_link_pic (info)
2324 && (info->symbolic || h->dynindx == -1)
2327 ADD_RELA (output_bfd, got, got_offset, 0, R_ARC_RELATIVE, 0);
2329 /* Do not fully understand the side effects of this condition.
2330 The relocation space might still being reserved. Perhaps
2331 I should clear its value. */
2332 else if (h->dynindx != -1)
2334 ADD_RELA (output_bfd, got, got_offset, h->dynindx,
2337 list->created_dyn_relocation = TRUE;
2339 else if (list->existing_entries != TLS_GOT_NONE)
2341 struct elf_link_hash_table *htab = elf_hash_table (info);
2342 enum tls_got_entries e = list->existing_entries;
2344 BFD_ASSERT (list->type != GOT_TLS_GD
2345 || list->existing_entries == TLS_GOT_MOD_AND_OFF);
2347 bfd_vma dynindx = h->dynindx == -1 ? 0 : h->dynindx;
2348 if (e == TLS_GOT_MOD_AND_OFF || e == TLS_GOT_MOD)
2350 ADD_RELA (output_bfd, got, got_offset, dynindx,
2351 R_ARC_TLS_DTPMOD, 0);
2352 ARC_DEBUG ("arc_info: TLS_DYNRELOC: type = %d, \
2353 GOT_OFFSET = 0x%x, GOT_VMA = 0x%x, INDEX = %d, ADDEND = 0x%x\n",
2356 htab->sgot->output_section->vma
2357 + htab->sgot->output_offset + got_offset,
2360 if (e == TLS_GOT_MOD_AND_OFF || e == TLS_GOT_OFF)
2363 if (list->type == GOT_TLS_IE)
2364 addend = bfd_get_32 (output_bfd,
2365 htab->sgot->contents + got_offset);
2367 ADD_RELA (output_bfd, got,
2368 got_offset + (e == TLS_GOT_MOD_AND_OFF ? 4 : 0),
2370 (list->type == GOT_TLS_IE ?
2371 R_ARC_TLS_TPOFF : R_ARC_TLS_DTPOFF),
2374 ARC_DEBUG ("arc_info: TLS_DYNRELOC: type = %d, \
2375 GOT_OFFSET = 0x%x, GOT_VMA = 0x%x, INDEX = %d, ADDEND = 0x%x\n",
2378 htab->sgot->output_section->vma
2379 + htab->sgot->output_offset + got_offset,
2387 h->got.glist = NULL;
2392 bfd_vma rel_offset = (h->root.u.def.value
2393 + h->root.u.def.section->output_section->vma
2394 + h->root.u.def.section->output_offset);
2397 bfd_get_section_by_name (h->root.u.def.section->owner,
2400 bfd_byte * loc = srelbss->contents
2401 + (srelbss->reloc_count * sizeof (Elf32_External_Rela));
2402 srelbss->reloc_count++;
2404 Elf_Internal_Rela rel;
2406 rel.r_offset = rel_offset;
2408 BFD_ASSERT (h->dynindx != -1);
2409 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_COPY);
2411 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2414 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2415 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2416 || strcmp (h->root.root.string, "__DYNAMIC") == 0
2417 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2418 sym->st_shndx = SHN_ABS;
2423 #define GET_SYMBOL_OR_SECTION(TAG, SYMBOL, SECTION) \
2425 if (SYMBOL != NULL) \
2426 h = elf_link_hash_lookup (elf_hash_table (info), \
2427 SYMBOL, FALSE, FALSE, TRUE); \
2428 else if (SECTION != NULL) \
2429 s = bfd_get_linker_section (dynobj, SECTION); \
2432 /* Function : elf_arc_finish_dynamic_sections
2433 Brief : Finish up the dynamic sections handling.
2438 Returns : True/False as the return status. */
2441 elf_arc_finish_dynamic_sections (bfd * output_bfd,
2442 struct bfd_link_info *info)
2444 struct dynamic_sections ds = arc_create_dynamic_sections (output_bfd, info);
2445 struct elf_link_hash_table *htab = elf_hash_table (info);
2446 bfd *dynobj = (elf_hash_table (info))->dynobj;
2450 Elf32_External_Dyn *dyncon, *dynconend;
2452 dyncon = (Elf32_External_Dyn *) ds.sdyn->contents;
2454 (Elf32_External_Dyn *) (ds.sdyn->contents + ds.sdyn->size);
2455 for (; dyncon < dynconend; dyncon++)
2457 Elf_Internal_Dyn internal_dyn;
2458 bfd_boolean do_it = FALSE;
2460 struct elf_link_hash_entry *h = NULL;
2463 bfd_elf32_swap_dyn_in (dynobj, dyncon, &internal_dyn);
2465 switch (internal_dyn.d_tag)
2467 GET_SYMBOL_OR_SECTION (DT_INIT, "_init", NULL)
2468 GET_SYMBOL_OR_SECTION (DT_FINI, "_fini", NULL)
2469 GET_SYMBOL_OR_SECTION (DT_PLTGOT, NULL, ".plt")
2470 GET_SYMBOL_OR_SECTION (DT_JMPREL, NULL, ".rela.plt")
2471 GET_SYMBOL_OR_SECTION (DT_PLTRELSZ, NULL, ".rela.plt")
2472 GET_SYMBOL_OR_SECTION (DT_RELASZ, NULL, ".rela.plt")
2473 GET_SYMBOL_OR_SECTION (DT_VERSYM, NULL, ".gnu.version")
2474 GET_SYMBOL_OR_SECTION (DT_VERDEF, NULL, ".gnu.version_d")
2475 GET_SYMBOL_OR_SECTION (DT_VERNEED, NULL, ".gnu.version_r")
2480 /* In case the dynamic symbols should be updated with a symbol. */
2482 && (h->root.type == bfd_link_hash_defined
2483 || h->root.type == bfd_link_hash_defweak))
2487 internal_dyn.d_un.d_val = h->root.u.def.value;
2488 asec_ptr = h->root.u.def.section;
2489 if (asec_ptr->output_section != NULL)
2491 internal_dyn.d_un.d_val +=
2492 (asec_ptr->output_section->vma +
2493 asec_ptr->output_offset);
2497 /* The symbol is imported from another shared
2498 library and does not apply to this one. */
2499 internal_dyn.d_un.d_val = 0;
2503 else if (s != NULL) /* With a section information. */
2505 switch (internal_dyn.d_tag)
2512 internal_dyn.d_un.d_ptr = (s->output_section->vma
2513 + s->output_offset);
2518 internal_dyn.d_un.d_val = s->size;
2524 internal_dyn.d_un.d_val -= s->size;
2534 bfd_elf32_swap_dyn_out (output_bfd, &internal_dyn, dyncon);
2537 if (htab->splt->size > 0)
2539 relocate_plt_for_entry (output_bfd, info);
2542 /* TODO: Validate this. */
2543 elf_section_data (htab->srelplt->output_section)->this_hdr.sh_entsize
2547 /* Fill in the first three entries in the global offset table. */
2550 struct elf_link_hash_entry *h;
2551 h = elf_link_hash_lookup (elf_hash_table (info), "_GLOBAL_OFFSET_TABLE_",
2552 FALSE, FALSE, TRUE);
2554 if (h != NULL && h->root.type != bfd_link_hash_undefined
2555 && h->root.u.def.section != NULL)
2557 asection *sec = h->root.u.def.section;
2559 if (ds.sdyn == NULL)
2560 bfd_put_32 (output_bfd, (bfd_vma) 0,
2563 bfd_put_32 (output_bfd,
2564 ds.sdyn->output_section->vma + ds.sdyn->output_offset,
2566 bfd_put_32 (output_bfd, (bfd_vma) 0, sec->contents + 4);
2567 bfd_put_32 (output_bfd, (bfd_vma) 0, sec->contents + 8);
2574 #define ADD_DYNAMIC_SYMBOL(NAME, TAG) \
2575 h = elf_link_hash_lookup (elf_hash_table (info), \
2576 NAME, FALSE, FALSE, FALSE); \
2577 if ((h != NULL && (h->ref_regular || h->def_regular))) \
2578 if (! _bfd_elf_add_dynamic_entry (info, TAG, 0)) \
2581 /* Set the sizes of the dynamic sections. */
2583 elf_arc_size_dynamic_sections (bfd * output_bfd,
2584 struct bfd_link_info *info)
2588 bfd_boolean relocs_exist = FALSE;
2589 bfd_boolean reltext_exist = FALSE;
2590 struct dynamic_sections ds = arc_create_dynamic_sections (output_bfd, info);
2591 struct elf_link_hash_table *htab = elf_hash_table (info);
2593 dynobj = (elf_hash_table (info))->dynobj;
2594 BFD_ASSERT (dynobj != NULL);
2596 if ((elf_hash_table (info))->dynamic_sections_created)
2598 struct elf_link_hash_entry *h;
2600 /* Set the contents of the .interp section to the
2602 if (!bfd_link_pic (info))
2604 s = bfd_get_section_by_name (dynobj, ".interp");
2605 BFD_ASSERT (s != NULL);
2606 s->size = sizeof (ELF_DYNAMIC_INTERPRETER);
2607 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2610 /* Add some entries to the .dynamic section. We fill in some of
2611 the values later, in elf_bfd_final_link, but we must add the
2612 entries now so that we know the final size of the .dynamic
2613 section. Checking if the .init section is present. We also
2614 create DT_INIT and DT_FINI entries if the init_str has been
2615 changed by the user. */
2616 ADD_DYNAMIC_SYMBOL ("init", DT_INIT);
2617 ADD_DYNAMIC_SYMBOL ("fini", DT_FINI);
2621 /* We may have created entries in the .rela.got section.
2622 However, if we are not creating the dynamic sections, we will
2623 not actually use these entries. Reset the size of .rela.got,
2624 which will cause it to get stripped from the output file
2626 if (htab->srelgot != NULL)
2627 htab->srelgot->size = 0;
2630 if (htab->splt != NULL && htab->splt->size == 0)
2631 htab->splt->flags |= SEC_EXCLUDE;
2632 for (s = dynobj->sections; s != NULL; s = s->next)
2634 if ((s->flags & SEC_LINKER_CREATED) == 0)
2637 if (strncmp (s->name, ".rela", 5) == 0)
2641 s->flags |= SEC_EXCLUDE;
2645 if (strcmp (s->name, ".rela.plt") != 0)
2647 const char *outname =
2648 bfd_get_section_name (output_bfd,
2649 htab->srelplt->output_section);
2651 asection *target = bfd_get_section_by_name (output_bfd,
2654 relocs_exist = TRUE;
2655 if (target != NULL && target->size != 0
2656 && (target->flags & SEC_READONLY) != 0
2657 && (target->flags & SEC_ALLOC) != 0)
2658 reltext_exist = TRUE;
2662 /* We use the reloc_count field as a counter if we need to
2663 copy relocs into the output file. */
2667 if (strcmp (s->name, ".dynamic") == 0)
2671 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2673 if (s->contents == NULL && s->size != 0)
2679 /* TODO: Check if this is needed. */
2680 if (!bfd_link_pic (info))
2681 if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
2684 if (htab->splt && (htab->splt->flags & SEC_EXCLUDE) == 0)
2685 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
2686 || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2687 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2688 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0)
2692 if (relocs_exist == TRUE)
2693 if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
2694 || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
2695 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
2696 sizeof (Elf32_External_Rela))
2700 if (reltext_exist == TRUE)
2701 if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
2709 /* Classify dynamic relocs such that -z combreloc can reorder and combine
2711 static enum elf_reloc_type_class
2712 elf32_arc_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2713 const asection *rel_sec ATTRIBUTE_UNUSED,
2714 const Elf_Internal_Rela *rela)
2716 switch ((int) ELF32_R_TYPE (rela->r_info))
2718 case R_ARC_RELATIVE:
2719 return reloc_class_relative;
2720 case R_ARC_JMP_SLOT:
2721 return reloc_class_plt;
2723 return reloc_class_copy;
2724 /* TODO: Needed in future to support ifunc. */
2726 case R_ARC_IRELATIVE:
2727 return reloc_class_ifunc;
2730 return reloc_class_normal;
2734 const struct elf_size_info arc_elf32_size_info =
2736 sizeof (Elf32_External_Ehdr),
2737 sizeof (Elf32_External_Phdr),
2738 sizeof (Elf32_External_Shdr),
2739 sizeof (Elf32_External_Rel),
2740 sizeof (Elf32_External_Rela),
2741 sizeof (Elf32_External_Sym),
2742 sizeof (Elf32_External_Dyn),
2743 sizeof (Elf_External_Note),
2747 ELFCLASS32, EV_CURRENT,
2748 bfd_elf32_write_out_phdrs,
2749 bfd_elf32_write_shdrs_and_ehdr,
2750 bfd_elf32_checksum_contents,
2751 bfd_elf32_write_relocs,
2752 bfd_elf32_swap_symbol_in,
2753 bfd_elf32_swap_symbol_out,
2754 bfd_elf32_slurp_reloc_table,
2755 bfd_elf32_slurp_symbol_table,
2756 bfd_elf32_swap_dyn_in,
2757 bfd_elf32_swap_dyn_out,
2758 bfd_elf32_swap_reloc_in,
2759 bfd_elf32_swap_reloc_out,
2760 bfd_elf32_swap_reloca_in,
2761 bfd_elf32_swap_reloca_out
2764 #define elf_backend_size_info arc_elf32_size_info
2766 static struct bfd_link_hash_table *
2767 arc_elf_link_hash_table_create (bfd *abfd)
2769 struct elf_link_hash_table *htab;
2771 htab = bfd_zmalloc (sizeof (*htab));
2775 if (!_bfd_elf_link_hash_table_init (htab, abfd,
2776 _bfd_elf_link_hash_newfunc,
2777 sizeof (struct elf_link_hash_entry),
2784 htab->init_got_refcount.refcount = 0;
2785 htab->init_got_refcount.glist = NULL;
2786 htab->init_got_offset.offset = 0;
2787 htab->init_got_offset.glist = NULL;
2788 return (struct bfd_link_hash_table *) htab;
2791 /* Hook called by the linker routine which adds symbols from an object
2795 elf_arc_add_symbol_hook (bfd * abfd,
2796 struct bfd_link_info * info,
2797 Elf_Internal_Sym * sym,
2798 const char ** namep ATTRIBUTE_UNUSED,
2799 flagword * flagsp ATTRIBUTE_UNUSED,
2800 asection ** secp ATTRIBUTE_UNUSED,
2801 bfd_vma * valp ATTRIBUTE_UNUSED)
2803 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
2804 && (abfd->flags & DYNAMIC) == 0
2805 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
2806 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
2811 #define TARGET_LITTLE_SYM arc_elf32_le_vec
2812 #define TARGET_LITTLE_NAME "elf32-littlearc"
2813 #define TARGET_BIG_SYM arc_elf32_be_vec
2814 #define TARGET_BIG_NAME "elf32-bigarc"
2815 #define ELF_ARCH bfd_arch_arc
2816 #define ELF_MACHINE_CODE EM_ARC_COMPACT
2817 #define ELF_MACHINE_ALT1 EM_ARC_COMPACT2
2818 #define ELF_MAXPAGESIZE 0x2000
2820 #define bfd_elf32_bfd_link_hash_table_create arc_elf_link_hash_table_create
2822 #define bfd_elf32_bfd_merge_private_bfd_data arc_elf_merge_private_bfd_data
2823 #define bfd_elf32_bfd_reloc_type_lookup arc_elf32_bfd_reloc_type_lookup
2824 #define bfd_elf32_bfd_set_private_flags arc_elf_set_private_flags
2825 #define bfd_elf32_bfd_print_private_bfd_data arc_elf_print_private_bfd_data
2826 #define bfd_elf32_bfd_copy_private_bfd_data arc_elf_copy_private_bfd_data
2828 #define elf_info_to_howto_rel arc_info_to_howto_rel
2829 #define elf_backend_object_p arc_elf_object_p
2830 #define elf_backend_final_write_processing arc_elf_final_write_processing
2832 #define elf_backend_relocate_section elf_arc_relocate_section
2833 #define elf_backend_check_relocs elf_arc_check_relocs
2834 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
2836 #define elf_backend_reloc_type_class elf32_arc_reloc_type_class
2838 #define elf_backend_adjust_dynamic_symbol elf_arc_adjust_dynamic_symbol
2839 #define elf_backend_finish_dynamic_symbol elf_arc_finish_dynamic_symbol
2841 #define elf_backend_finish_dynamic_sections elf_arc_finish_dynamic_sections
2842 #define elf_backend_size_dynamic_sections elf_arc_size_dynamic_sections
2843 #define elf_backend_add_symbol_hook elf_arc_add_symbol_hook
2845 #define elf_backend_can_gc_sections 1
2846 #define elf_backend_want_got_plt 1
2847 #define elf_backend_plt_readonly 1
2848 #define elf_backend_rela_plts_and_copies_p 1
2849 #define elf_backend_want_plt_sym 0
2850 #define elf_backend_got_header_size 12
2852 #define elf_backend_may_use_rel_p 0
2853 #define elf_backend_may_use_rela_p 1
2854 #define elf_backend_default_use_rela_p 1
2856 #define elf_backend_default_execstack 0
2858 #include "elf32-target.h"