+ elf_elfheader (abfd)->e_machine = emf;
+
+ /* Record whatever is the current syscall ABI version. */
+ if (osver)
+ e_flags |= ((osver & 0x0f) << 8);
+ else
+ e_flags |= E_ARC_OSABI_V3;
+
+ elf_elfheader (abfd)->e_flags |= e_flags;
+}
+
+#ifdef ARC_ENABLE_DEBUG
+#define DEBUG_ARC_RELOC(A) debug_arc_reloc (A)
+
+static void
+debug_arc_reloc (struct arc_relocation_data reloc_data)
+{
+ ARC_DEBUG ("Reloc type=%s, should_relocate = %s\n",
+ reloc_data.howto->name,
+ reloc_data.should_relocate ? "true" : "false");
+ ARC_DEBUG (" offset = 0x%x, addend = 0x%x\n",
+ (unsigned int) reloc_data.reloc_offset,
+ (unsigned int) reloc_data.reloc_addend);
+ ARC_DEBUG (" Symbol:\n");
+ ARC_DEBUG (" value = 0x%08x\n",
+ (unsigned int) reloc_data.sym_value);
+ if (reloc_data.sym_section != NULL)
+ {
+ ARC_DEBUG (" Symbol Section:\n");
+ ARC_DEBUG (" section name = %s, output_offset 0x%08x",
+ reloc_data.sym_section->name,
+ (unsigned int) reloc_data.sym_section->output_offset);
+ if (reloc_data.sym_section->output_section != NULL)
+ ARC_DEBUG (", output_section->vma = 0x%08x",
+ ((unsigned int) reloc_data.sym_section->output_section->vma));
+ ARC_DEBUG ("\n");
+ if (reloc_data.sym_section->owner && reloc_data.sym_section->owner->filename)
+ ARC_DEBUG (" file: %s\n", reloc_data.sym_section->owner->filename);
+ }
+ else
+ {
+ ARC_DEBUG (" symbol section is NULL\n");
+ }
+
+ ARC_DEBUG (" Input_section:\n");
+ if (reloc_data.input_section != NULL)
+ {
+ ARC_DEBUG (" section name = %s, output_offset 0x%08x, output_section->vma = 0x%08x\n",
+ reloc_data.input_section->name,
+ (unsigned int) reloc_data.input_section->output_offset,
+ (unsigned int) reloc_data.input_section->output_section->vma);
+ ARC_DEBUG (" changed_address = 0x%08x\n",
+ (unsigned int) (reloc_data.input_section->output_section->vma
+ + reloc_data.input_section->output_offset
+ + reloc_data.reloc_offset));
+ ARC_DEBUG (" file: %s\n", reloc_data.input_section->owner->filename);
+ }
+ else
+ {
+ ARC_DEBUG (" input section is NULL\n");
+ }
+}
+#else
+#define DEBUG_ARC_RELOC(A)
+#endif /* ARC_ENABLE_DEBUG */
+
+static bfd_vma
+middle_endian_convert (bfd_vma insn, bfd_boolean do_it)
+{
+ if (do_it)
+ {
+ insn
+ = ((insn & 0xffff0000) >> 16)
+ | ((insn & 0xffff) << 16);
+ }
+ return insn;
+}
+
+/* This function is called for relocations that are otherwise marked as NOT
+ requiring overflow checks. In here we perform non-standard checks of
+ the relocation value. */
+
+static inline bfd_reloc_status_type
+arc_special_overflow_checks (const struct arc_relocation_data reloc_data,
+ bfd_signed_vma relocation,
+ struct bfd_link_info *info ATTRIBUTE_UNUSED)
+{
+ switch (reloc_data.howto->type)
+ {
+ case R_ARC_NPS_CMEM16:
+ if (((relocation >> 16) & 0xffff) != NPS_CMEM_HIGH_VALUE)
+ {
+ if (reloc_data.reloc_addend == 0)
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("%pB(%pA+%#" PRIx64 "): CMEM relocation to `%s' is invalid, "
+ "16 MSB should be %#x (value is %#" PRIx64 ")"),
+ reloc_data.input_section->owner,
+ reloc_data.input_section,
+ (uint64_t) reloc_data.reloc_offset,
+ reloc_data.symbol_name,
+ NPS_CMEM_HIGH_VALUE,
+ (uint64_t) relocation);
+ else
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("%pB(%pA+%#" PRIx64 "): CMEM relocation to `%s+%#" PRIx64
+ "' is invalid, 16 MSB should be %#x (value is %#" PRIx64 ")"),
+ reloc_data.input_section->owner,
+ reloc_data.input_section,
+ (uint64_t) reloc_data.reloc_offset,
+ reloc_data.symbol_name,
+ (uint64_t) reloc_data.reloc_addend,
+ NPS_CMEM_HIGH_VALUE,
+ (uint64_t) relocation);
+ return bfd_reloc_overflow;
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ return bfd_reloc_ok;
+}
+
+#define ME(reloc) (reloc)
+
+#define IS_ME(FORMULA,BFD) ((strstr (FORMULA, "ME") != NULL) \
+ && (!bfd_big_endian (BFD)))
+
+#define S ((bfd_signed_vma) (reloc_data.sym_value \
+ + (reloc_data.sym_section->output_section != NULL ? \
+ (reloc_data.sym_section->output_offset \
+ + reloc_data.sym_section->output_section->vma) : 0)))
+#define L ((bfd_signed_vma) (reloc_data.sym_value \
+ + (reloc_data.sym_section->output_section != NULL ? \
+ (reloc_data.sym_section->output_offset \
+ + reloc_data.sym_section->output_section->vma) : 0)))
+#define A (reloc_data.reloc_addend)
+#define B (0)
+#define G (reloc_data.got_offset_value)
+#define GOT (reloc_data.got_symbol_vma)
+#define GOT_BEGIN (htab->sgot->output_section->vma)
+
+#define MES (0)
+ /* P: relative offset to PCL The offset should be to the
+ current location aligned to 32 bits. */
+#define P ((bfd_signed_vma) ( \
+ ( \
+ (reloc_data.input_section->output_section != NULL ? \
+ reloc_data.input_section->output_section->vma : 0) \
+ + reloc_data.input_section->output_offset \
+ + (reloc_data.reloc_offset - (bitsize >= 32 ? 4 : 0))) \
+ & ~0x3))
+#define PDATA ((bfd_signed_vma) ( \
+ (reloc_data.input_section->output_section->vma \
+ + reloc_data.input_section->output_offset \
+ + (reloc_data.reloc_offset))))
+#define SECTSTART (bfd_signed_vma) (reloc_data.sym_section->output_section->vma \
+ + reloc_data.sym_section->output_offset)
+#define JLI (bfd_signed_vma) (reloc_data.sym_section->output_section->vma)
+#define _SDA_BASE_ (bfd_signed_vma) (reloc_data.sdata_begin_symbol_vma)
+#define TLS_REL (bfd_signed_vma) \
+ ((elf_hash_table (info))->tls_sec->output_section->vma)
+#define TLS_TBSS (8)
+
+#define none (0)
+
+#ifdef ARC_ENABLE_DEBUG
+#define PRINT_DEBUG_RELOC_INFO_BEFORE(FORMULA, TYPE) \
+ do \
+ { \
+ asection *sym_section = reloc_data.sym_section; \
+ asection *input_section = reloc_data.input_section; \
+ ARC_DEBUG ("RELOC_TYPE = " TYPE "\n"); \
+ ARC_DEBUG ("FORMULA = " FORMULA "\n"); \
+ ARC_DEBUG ("S = %#lx\n", S); \
+ ARC_DEBUG ("A = %#lx\n", A); \
+ ARC_DEBUG ("L = %lx\n", L); \
+ if (sym_section->output_section != NULL) \
+ ARC_DEBUG ("symbol_section->vma = %#lx\n", \
+ sym_section->output_section->vma \
+ + sym_section->output_offset); \
+ else \
+ ARC_DEBUG ("symbol_section->vma = NULL\n"); \
+ if (input_section->output_section != NULL) \
+ ARC_DEBUG ("symbol_section->vma = %#lx\n", \
+ input_section->output_section->vma \
+ + input_section->output_offset); \
+ else \
+ ARC_DEBUG ("symbol_section->vma = NULL\n"); \
+ ARC_DEBUG ("PCL = %#lx\n", P); \
+ ARC_DEBUG ("P = %#lx\n", P); \
+ ARC_DEBUG ("G = %#lx\n", G); \
+ ARC_DEBUG ("SDA_OFFSET = %#lx\n", _SDA_BASE_); \
+ ARC_DEBUG ("SDA_SET = %d\n", reloc_data.sdata_begin_symbol_vma_set); \
+ ARC_DEBUG ("GOT_OFFSET = %#lx\n", GOT); \
+ ARC_DEBUG ("relocation = %#08lx\n", relocation); \
+ ARC_DEBUG ("before = %#08x\n", (unsigned) insn); \
+ ARC_DEBUG ("data = %08x (%u) (%d)\n", (unsigned) relocation, \
+ (unsigned) relocation, (int) relocation); \
+ } \
+ while (0)
+
+#define PRINT_DEBUG_RELOC_INFO_AFTER \
+ do \
+ { \
+ ARC_DEBUG ("after = 0x%08x\n", (unsigned int) insn); \
+ } \
+ while (0)
+
+#else
+
+#define PRINT_DEBUG_RELOC_INFO_BEFORE(...)
+#define PRINT_DEBUG_RELOC_INFO_AFTER
+
+#endif /* ARC_ENABLE_DEBUG */
+
+#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
+ case R_##TYPE: \
+ { \
+ bfd_signed_vma bitsize ATTRIBUTE_UNUSED = BITSIZE; \
+ relocation = FORMULA ; \
+ PRINT_DEBUG_RELOC_INFO_BEFORE (#FORMULA, #TYPE); \
+ insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
+ insn = (* get_replace_function (abfd, TYPE)) (insn, relocation); \
+ insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
+ PRINT_DEBUG_RELOC_INFO_AFTER; \
+ } \
+ break;
+
+static bfd_reloc_status_type
+arc_do_relocation (bfd_byte * contents,
+ struct arc_relocation_data reloc_data,
+ struct bfd_link_info *info)
+{
+ bfd_signed_vma relocation = 0;
+ bfd_vma insn;
+ bfd_vma orig_insn ATTRIBUTE_UNUSED;
+ bfd * abfd = reloc_data.input_section->owner;
+ struct elf_link_hash_table *htab ATTRIBUTE_UNUSED = elf_hash_table (info);
+ bfd_reloc_status_type flag;
+
+ if (!reloc_data.should_relocate)
+ return bfd_reloc_ok;
+
+ switch (reloc_data.howto->size)
+ {
+ case 2:
+ insn = arc_bfd_get_32 (abfd,
+ contents + reloc_data.reloc_offset,
+ reloc_data.input_section);
+ break;
+ case 1:
+ insn = arc_bfd_get_16 (abfd,
+ contents + reloc_data.reloc_offset,
+ reloc_data.input_section);
+ break;
+ case 0:
+ insn = arc_bfd_get_8 (abfd,
+ contents + reloc_data.reloc_offset,
+ reloc_data.input_section);
+ break;
+ default:
+ insn = 0;
+ BFD_ASSERT (0);
+ break;
+ }
+
+ orig_insn = insn;
+
+ switch (reloc_data.howto->type)
+ {
+#include "elf/arc-reloc.def"
+
+ default:
+ BFD_ASSERT (0);
+ break;
+ }
+
+ /* Check for relocation overflow. */
+ if (reloc_data.howto->complain_on_overflow != complain_overflow_dont)
+ flag = bfd_check_overflow (reloc_data.howto->complain_on_overflow,
+ reloc_data.howto->bitsize,
+ reloc_data.howto->rightshift,
+ bfd_arch_bits_per_address (abfd),
+ relocation);
+ else
+ flag = arc_special_overflow_checks (reloc_data, relocation, info);
+
+ if (flag != bfd_reloc_ok)
+ {
+ ARC_DEBUG ("Relocation overflows !\n");
+ DEBUG_ARC_RELOC (reloc_data);
+ ARC_DEBUG ("Relocation value = signed -> %d, unsigned -> %u"
+ ", hex -> (0x%08x)\n",
+ (int) relocation, (unsigned) relocation, (int) relocation);
+
+ return flag;
+ }
+
+ /* Write updated instruction back to memory. */
+ switch (reloc_data.howto->size)
+ {
+ case 2:
+ arc_bfd_put_32 (abfd, insn,
+ contents + reloc_data.reloc_offset,
+ reloc_data.input_section);
+ break;
+ case 1:
+ arc_bfd_put_16 (abfd, insn,
+ contents + reloc_data.reloc_offset,
+ reloc_data.input_section);
+ break;
+ case 0:
+ arc_bfd_put_8 (abfd, insn,
+ contents + reloc_data.reloc_offset,
+ reloc_data.input_section);
+ break;
+ default:
+ ARC_DEBUG ("size = %d\n", reloc_data.howto->size);
+ BFD_ASSERT (0);
+ break;
+ }
+
+ return bfd_reloc_ok;
+}
+#undef S
+#undef A
+#undef B
+#undef G
+#undef GOT
+#undef L
+#undef MES
+#undef P
+#undef SECTSTAR
+#undef SECTSTART
+#undef JLI
+#undef _SDA_BASE_
+#undef none
+
+#undef ARC_RELOC_HOWTO
+
+
+/* Relocate an arc ELF section.
+ Function : elf_arc_relocate_section
+ Brief : Relocate an arc section, by handling all the relocations
+ appearing in that section.
+ Args : output_bfd : The bfd being written to.
+ info : Link information.
+ input_bfd : The input bfd.
+ input_section : The section being relocated.
+ contents : contents of the section being relocated.
+ relocs : List of relocations in the section.
+ local_syms : is a pointer to the swapped in local symbols.
+ local_section : is an array giving the section in the input file
+ corresponding to the st_shndx field of each
+ local symbol. */
+static bfd_boolean
+elf_arc_relocate_section (bfd * output_bfd,
+ struct bfd_link_info * info,
+ bfd * input_bfd,
+ asection * input_section,
+ bfd_byte * contents,
+ Elf_Internal_Rela * relocs,
+ Elf_Internal_Sym * local_syms,
+ asection ** local_sections)
+{
+ Elf_Internal_Shdr * symtab_hdr;
+ struct elf_link_hash_entry ** sym_hashes;
+ Elf_Internal_Rela * rel;
+ Elf_Internal_Rela * wrel;
+ Elf_Internal_Rela * relend;
+ struct elf_link_hash_table * htab = elf_hash_table (info);
+
+ symtab_hdr = &((elf_tdata (input_bfd))->symtab_hdr);
+ sym_hashes = elf_sym_hashes (input_bfd);
+
+ rel = wrel = relocs;
+ relend = relocs + input_section->reloc_count;
+ for (; rel < relend; wrel++, rel++)
+ {
+ enum elf_arc_reloc_type r_type;
+ reloc_howto_type * howto;
+ unsigned long r_symndx;
+ struct elf_link_hash_entry * h;
+ Elf_Internal_Sym * sym;
+ asection * sec;
+ struct elf_link_hash_entry * h2;
+ const char * msg;
+ bfd_boolean unresolved_reloc = FALSE;
+
+ struct arc_relocation_data reloc_data =
+ {
+ .reloc_offset = 0,
+ .reloc_addend = 0,
+ .got_offset_value = 0,
+ .sym_value = 0,
+ .sym_section = NULL,
+ .howto = NULL,
+ .input_section = NULL,
+ .sdata_begin_symbol_vma = 0,
+ .sdata_begin_symbol_vma_set = FALSE,
+ .got_symbol_vma = 0,
+ .should_relocate = FALSE
+ };
+
+ r_type = ELF32_R_TYPE (rel->r_info);
+
+ if (r_type >= (int) R_ARC_max)
+ {
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
+ }
+ howto = arc_elf_howto (r_type);
+
+ r_symndx = ELF32_R_SYM (rel->r_info);
+
+ /* If we are generating another .o file and the symbol in not
+ local, skip this relocation. */
+ if (bfd_link_relocatable (info))
+ {
+ /* This is a relocateable link. We don't have to change
+ anything, unless the reloc is against a section symbol,
+ in which case we have to adjust according to where the
+ section symbol winds up in the output section. */
+
+ /* Checks if this is a local symbol and thus the reloc
+ might (will??) be against a section symbol. */
+ if (r_symndx < symtab_hdr->sh_info)
+ {
+ sym = local_syms + r_symndx;
+ if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
+ {
+ sec = local_sections[r_symndx];
+
+ /* For RELA relocs. Just adjust the addend
+ value in the relocation entry. */
+ rel->r_addend += sec->output_offset + sym->st_value;
+
+ ARC_DEBUG ("local symbols reloc (section=%d %s) seen in %s\n",
+ (int) r_symndx, local_sections[r_symndx]->name,
+ __PRETTY_FUNCTION__);
+ }
+ }
+ }
+
+ h2 = elf_link_hash_lookup (elf_hash_table (info), "__SDATA_BEGIN__",
+ FALSE, FALSE, TRUE);
+
+ if (!reloc_data.sdata_begin_symbol_vma_set
+ && h2 != NULL && h2->root.type != bfd_link_hash_undefined
+ && h2->root.u.def.section->output_section != NULL)
+ /* TODO: Verify this condition. */
+ {
+ reloc_data.sdata_begin_symbol_vma =
+ (h2->root.u.def.value
+ + h2->root.u.def.section->output_section->vma);
+ reloc_data.sdata_begin_symbol_vma_set = TRUE;
+ }
+
+ reloc_data.input_section = input_section;
+ reloc_data.howto = howto;
+ reloc_data.reloc_offset = rel->r_offset;
+ reloc_data.reloc_addend = rel->r_addend;
+
+ /* This is a final link. */
+ h = NULL;
+ sym = NULL;
+ sec = NULL;
+
+ if (r_symndx < symtab_hdr->sh_info) /* A local symbol. */
+ {
+ sym = local_syms + r_symndx;
+ sec = local_sections[r_symndx];
+ }
+ else
+ {
+ bfd_boolean warned, ignored;
+ bfd_vma relocation ATTRIBUTE_UNUSED;
+
+ RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
+ r_symndx, symtab_hdr, sym_hashes,
+ h, sec, relocation,
+ unresolved_reloc, warned, ignored);
+
+ /* TODO: This code is repeated from below. We should
+ clean it and remove duplications.
+ Sec is used check for discarded sections.
+ Need to redesign code below. */
+
+ /* Get the symbol's entry in the symtab. */
+ h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+
+ while (h->root.type == bfd_link_hash_indirect
+ || h->root.type == bfd_link_hash_warning)
+ h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
+ /* If we have encountered a definition for this symbol. */
+ if (h->root.type == bfd_link_hash_defined
+ || h->root.type == bfd_link_hash_defweak)
+ {
+ reloc_data.sym_value = h->root.u.def.value;
+ sec = h->root.u.def.section;
+ }
+ }
+
+ /* Clean relocs for symbols in discarded sections. */
+ if (sec != NULL && discarded_section (sec))
+ {
+ _bfd_clear_contents (howto, input_bfd, input_section,
+ contents + rel->r_offset);
+ rel->r_info = 0;
+ rel->r_addend = 0;
+
+ /* For ld -r, remove relocations in debug sections against
+ sections defined in discarded sections. Not done for
+ eh_frame editing code expects to be present. */
+ if (bfd_link_relocatable (info)
+ && (input_section->flags & SEC_DEBUGGING))
+ wrel--;
+
+ continue;
+ }
+
+ if (bfd_link_relocatable (info))
+ {
+ if (wrel != rel)
+ *wrel = *rel;
+ continue;
+ }
+
+ if (r_symndx < symtab_hdr->sh_info) /* A local symbol. */
+ {
+ reloc_data.sym_value = sym->st_value;
+ reloc_data.sym_section = sec;
+ reloc_data.symbol_name =
+ bfd_elf_string_from_elf_section (input_bfd,
+ symtab_hdr->sh_link,
+ sym->st_name);
+
+ /* Mergeable section handling. */
+ if ((sec->flags & SEC_MERGE)
+ && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
+ {
+ asection *msec;
+ msec = sec;
+ rel->r_addend = _bfd_elf_rel_local_sym (output_bfd, sym,
+ &msec, rel->r_addend);
+ rel->r_addend -= (sec->output_section->vma
+ + sec->output_offset
+ + sym->st_value);
+ rel->r_addend += msec->output_section->vma + msec->output_offset;
+
+ reloc_data.reloc_addend = rel->r_addend;
+ }
+
+ BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto));
+ if (htab->sgot != NULL)
+ reloc_data.got_symbol_vma = htab->sgot->output_section->vma
+ + htab->sgot->output_offset;
+
+ reloc_data.should_relocate = TRUE;
+ }
+ else /* Global symbol. */
+ {
+ /* FIXME: We should use the RELOC_FOR_GLOBAL_SYMBOL macro
+ (defined in elf-bfd.h) here. */
+
+ /* Get the symbol's entry in the symtab. */
+ h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+
+ while (h->root.type == bfd_link_hash_indirect
+ || h->root.type == bfd_link_hash_warning)
+ {
+ struct elf_link_hash_entry *h_old = h;
+ h = (struct elf_link_hash_entry *) h->root.u.i.link;
+ if (h->got.glist == 0 && h_old->got.glist != h->got.glist)
+ h->got.glist = h_old->got.glist;
+ }
+
+ /* TODO: Need to validate what was the intention. */
+ /* BFD_ASSERT ((h->dynindx == -1) || (h->forced_local != 0)); */
+ reloc_data.symbol_name = h->root.root.string;
+
+ /* If we have encountered a definition for this symbol. */
+ if (h->root.type == bfd_link_hash_defined
+ || h->root.type == bfd_link_hash_defweak)
+ {
+ reloc_data.sym_value = h->root.u.def.value;
+ reloc_data.sym_section = h->root.u.def.section;
+
+ reloc_data.should_relocate = TRUE;
+
+ if (is_reloc_for_GOT (howto) && !bfd_link_pic (info))
+ {
+ /* TODO: Change it to use arc_do_relocation with
+ ARC_32 reloc. Try to use ADD_RELA macro. */
+ bfd_vma relocation =
+ reloc_data.sym_value + reloc_data.reloc_addend
+ + (reloc_data.sym_section->output_section != NULL ?
+ (reloc_data.sym_section->output_offset
+ + reloc_data.sym_section->output_section->vma)
+ : 0);
+
+ BFD_ASSERT (h->got.glist);
+ bfd_vma got_offset = h->got.glist->offset;
+ bfd_put_32 (output_bfd, relocation,
+ htab->sgot->contents + got_offset);
+ }
+ if (is_reloc_for_PLT (howto) && h->plt.offset != (bfd_vma) -1)
+ {
+ /* TODO: This is repeated up here. */
+ reloc_data.sym_value = h->plt.offset;
+ reloc_data.sym_section = htab->splt;
+ }
+ }
+ else if (h->root.type == bfd_link_hash_undefweak)
+ {
+ /* Is weak symbol and has no definition. */
+ if (is_reloc_for_GOT (howto))
+ {
+ reloc_data.sym_value = h->root.u.def.value;
+ reloc_data.sym_section = htab->sgot;
+ reloc_data.should_relocate = TRUE;
+ }
+ else if (is_reloc_for_PLT (howto)
+ && h->plt.offset != (bfd_vma) -1)
+ {
+ /* TODO: This is repeated up here. */
+ reloc_data.sym_value = h->plt.offset;
+ reloc_data.sym_section = htab->splt;
+ reloc_data.should_relocate = TRUE;
+ }
+ else
+ continue;
+ }
+ else
+ {
+ if (is_reloc_for_GOT (howto))
+ {
+ reloc_data.sym_value = h->root.u.def.value;
+ reloc_data.sym_section = htab->sgot;
+
+ reloc_data.should_relocate = TRUE;
+ }
+ else if (is_reloc_for_PLT (howto))
+ {
+ /* Fail if it is linking for PIE and the symbol is
+ undefined. */
+ if (bfd_link_executable (info))
+ (*info->callbacks->undefined_symbol)
+ (info, h->root.root.string, input_bfd, input_section,
+ rel->r_offset, TRUE);
+ reloc_data.sym_value = h->plt.offset;
+ reloc_data.sym_section = htab->splt;
+
+ reloc_data.should_relocate = TRUE;
+ }
+ else if (!bfd_link_pic (info) || bfd_link_executable (info))
+ (*info->callbacks->undefined_symbol)
+ (info, h->root.root.string, input_bfd, input_section,
+ rel->r_offset, TRUE);
+ }
+
+ BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto));
+ if (htab->sgot != NULL)
+ reloc_data.got_symbol_vma = htab->sgot->output_section->vma
+ + htab->sgot->output_offset;
+ }
+
+ if ((is_reloc_for_GOT (howto)
+ || is_reloc_for_TLS (howto)))
+ {
+ reloc_data.should_relocate = TRUE;
+
+ struct got_entry **list
+ = get_got_entry_list_for_symbol (output_bfd, r_symndx, h);
+
+ reloc_data.got_offset_value
+ = relocate_fix_got_relocs_for_got_info (list,
+ tls_type_for_reloc (howto),
+ info,
+ output_bfd,
+ r_symndx,
+ local_syms,
+ local_sections,
+ h,
+ &reloc_data);
+
+ if (h == NULL)
+ {
+ create_got_dynrelocs_for_single_entry (
+ got_entry_for_type (list,
+ arc_got_entry_type_for_reloc (howto)),
+ output_bfd, info, NULL);
+ }
+ }
+
+
+#define IS_ARC_PCREL_TYPE(TYPE) \
+ ( (TYPE == R_ARC_PC32) \
+ || (TYPE == R_ARC_32_PCREL))
+
+ switch (r_type)
+ {
+ case R_ARC_32:
+ case R_ARC_32_ME:
+ case R_ARC_PC32:
+ case R_ARC_32_PCREL:
+ if (bfd_link_pic (info)
+ && (!IS_ARC_PCREL_TYPE (r_type)
+ || (h != NULL
+ && h->dynindx != -1
+ && !h->def_regular
+ && (!info->symbolic || !h->def_regular))))
+ {
+ Elf_Internal_Rela outrel;
+ bfd_byte *loc;
+ bfd_boolean skip = FALSE;
+ bfd_boolean relocate = FALSE;
+ asection *sreloc = _bfd_elf_get_dynamic_reloc_section
+ (input_bfd, input_section,
+ /*RELA*/ TRUE);
+
+ BFD_ASSERT (sreloc != NULL);
+
+ outrel.r_offset = _bfd_elf_section_offset (output_bfd,
+ info,
+ input_section,
+ rel->r_offset);
+
+ if (outrel.r_offset == (bfd_vma) -1)
+ skip = TRUE;
+
+ outrel.r_addend = rel->r_addend;
+ outrel.r_offset += (input_section->output_section->vma
+ + input_section->output_offset);
+
+ if (skip)
+ {
+ memset (&outrel, 0, sizeof outrel);
+ relocate = FALSE;
+ }
+ else if (h != NULL
+ && h->dynindx != -1
+ && (IS_ARC_PCREL_TYPE (r_type)
+ || !(bfd_link_executable (info)
+ || SYMBOLIC_BIND (info, h))
+ || ! h->def_regular))
+ {
+ BFD_ASSERT (h != NULL);
+ if ((input_section->flags & SEC_ALLOC) != 0)
+ relocate = FALSE;
+ else
+ relocate = TRUE;
+
+ BFD_ASSERT (h->dynindx != -1);
+ outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
+ }
+ else
+ {
+ /* Handle local symbols, they either do not have a
+ global hash table entry (h == NULL), or are
+ forced local due to a version script
+ (h->forced_local), or the third condition is
+ legacy, it appears to say something like, for
+ links where we are pre-binding the symbols, or
+ there's not an entry for this symbol in the
+ dynamic symbol table, and it's a regular symbol
+ not defined in a shared object, then treat the
+ symbol as local, resolve it now. */
+ relocate = TRUE;
+ /* outrel.r_addend = 0; */
+ outrel.r_info = ELF32_R_INFO (0, R_ARC_RELATIVE);
+ }
+
+ BFD_ASSERT (sreloc->contents != 0);
+
+ loc = sreloc->contents;
+ loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
+ sreloc->reloc_count += 1;
+
+ bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
+
+ if (!relocate)
+ continue;
+ }
+ break;
+ default:
+ break;
+ }
+
+ if (is_reloc_SDA_relative (howto)
+ && !reloc_data.sdata_begin_symbol_vma_set)
+ {
+ _bfd_error_handler
+ ("error: linker symbol __SDATA_BEGIN__ not found");
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
+ }
+
+ DEBUG_ARC_RELOC (reloc_data);
+
+ /* Make sure we have with a dynamic linker. In case of GOT and PLT
+ the sym_section should point to .got or .plt respectively. */
+ if ((is_reloc_for_GOT (howto) || is_reloc_for_PLT (howto))
+ && reloc_data.sym_section == NULL)
+ {
+ _bfd_error_handler
+ (_("GOT and PLT relocations cannot be fixed with a non dynamic linker"));
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
+ }
+
+ msg = NULL;
+ switch (arc_do_relocation (contents, reloc_data, info))
+ {
+ case bfd_reloc_ok:
+ continue; /* The reloc processing loop. */
+
+ case bfd_reloc_overflow:
+ (*info->callbacks->reloc_overflow)
+ (info, (h ? &h->root : NULL), reloc_data.symbol_name, howto->name, (bfd_vma) 0,
+ input_bfd, input_section, rel->r_offset);
+ break;
+
+ case bfd_reloc_undefined:
+ (*info->callbacks->undefined_symbol)
+ (info, reloc_data.symbol_name, input_bfd, input_section, rel->r_offset, TRUE);
+ break;
+
+ case bfd_reloc_other:
+ /* xgettext:c-format */
+ msg = _("%pB(%pA): warning: unaligned access to symbol '%s' in the small data area");
+ break;
+
+ case bfd_reloc_outofrange:
+ /* xgettext:c-format */
+ msg = _("%pB(%pA): internal error: out of range error");
+ break;
+
+ case bfd_reloc_notsupported:
+ /* xgettext:c-format */
+ msg = _("%pB(%pA): internal error: unsupported relocation error");
+ break;
+
+ case bfd_reloc_dangerous:
+ /* xgettext:c-format */
+ msg = _("%pB(%pA): internal error: dangerous relocation");
+ break;
+
+ default:
+ /* xgettext:c-format */
+ msg = _("%pB(%pA): internal error: unknown error");
+ break;
+ }
+
+ if (msg)
+ _bfd_error_handler (msg, input_bfd, input_section, reloc_data.symbol_name);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+#define elf_arc_hash_table(p) \
+ (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
+ == ARC_ELF_DATA ? ((struct elf_arc_link_hash_table *) ((p)->hash)) : NULL)
+
+static bfd_boolean
+elf_arc_check_relocs (bfd * abfd,
+ struct bfd_link_info * info,
+ asection * sec,
+ const Elf_Internal_Rela * relocs)
+{
+ Elf_Internal_Shdr * symtab_hdr;
+ struct elf_link_hash_entry ** sym_hashes;
+ const Elf_Internal_Rela * rel;
+ const Elf_Internal_Rela * rel_end;
+ bfd * dynobj;
+ asection * sreloc = NULL;
+ struct elf_link_hash_table * htab = elf_hash_table (info);
+
+ if (bfd_link_relocatable (info))
+ return TRUE;
+
+ if (htab->dynobj == NULL)
+ htab->dynobj = abfd;
+
+ dynobj = (elf_hash_table (info))->dynobj;
+ symtab_hdr = &((elf_tdata (abfd))->symtab_hdr);
+ sym_hashes = elf_sym_hashes (abfd);
+
+ rel_end = relocs + sec->reloc_count;
+ for (rel = relocs; rel < rel_end; rel++)
+ {
+ enum elf_arc_reloc_type r_type;
+ reloc_howto_type *howto;
+ unsigned long r_symndx;
+ struct elf_link_hash_entry *h;
+
+ r_type = ELF32_R_TYPE (rel->r_info);
+
+ if (r_type >= (int) R_ARC_max)
+ {
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
+ }
+ howto = arc_elf_howto (r_type);
+
+ /* Load symbol information. */
+ r_symndx = ELF32_R_SYM (rel->r_info);
+ if (r_symndx < symtab_hdr->sh_info) /* Is a local symbol. */
+ h = NULL;
+ else /* Global one. */
+ h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+
+ switch (r_type)
+ {
+ case R_ARC_32:
+ case R_ARC_32_ME:
+ /* During shared library creation, these relocs should not
+ appear in a shared library (as memory will be read only
+ and the dynamic linker can not resolve these. However
+ the error should not occur for e.g. debugging or
+ non-readonly sections. */
+ if (h != NULL
+ && (bfd_link_dll (info) && !bfd_link_pie (info))
+ && (sec->flags & SEC_ALLOC) != 0
+ && (sec->flags & SEC_READONLY) != 0
+ && ((sec->flags & SEC_CODE) != 0
+ || (sec->flags & SEC_DEBUGGING) != 0))
+ {
+ const char *name;
+ if (h)
+ name = h->root.root.string;
+ else
+ /* bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL); */
+ name = "UNKNOWN";
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("%pB: relocation %s against `%s' can not be used"
+ " when making a shared object; recompile with -fPIC"),
+ abfd,
+ arc_elf_howto (r_type)->name,
+ name);
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
+ }
+
+ /* In some cases we are not setting the 'non_got_ref'
+ flag, even though the relocations don't require a GOT
+ access. We should extend the testing in this area to
+ ensure that no significant cases are being missed. */
+ if (h)
+ h->non_got_ref = 1;
+ /* FALLTHROUGH */
+ case R_ARC_PC32:
+ case R_ARC_32_PCREL:
+ if ((bfd_link_pic (info))
+ && ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL)
+ || (h != NULL
+ && (!info->symbolic || !h->def_regular))))
+ {
+ if (sreloc == NULL)
+ {
+ if (info->dynamic
+ && ! htab->dynamic_sections_created
+ && ! _bfd_elf_link_create_dynamic_sections (abfd, info))
+ return FALSE;
+ sreloc = _bfd_elf_make_dynamic_reloc_section (sec, dynobj,
+ 2, abfd,
+ /*rela*/
+ TRUE);
+
+ if (sreloc == NULL)
+ return FALSE;
+ }
+ sreloc->size += sizeof (Elf32_External_Rela);
+
+ }
+ default:
+ break;
+ }
+
+ if (is_reloc_for_PLT (howto))
+ {
+ if (h == NULL)
+ continue;
+ else
+ h->needs_plt = 1;
+ }
+
+ /* Add info to the symbol got_entry_list. */
+ if (is_reloc_for_GOT (howto)
+ || is_reloc_for_TLS (howto))
+ {
+ if (! _bfd_elf_create_got_section (dynobj, info))
+ return FALSE;
+
+ arc_fill_got_info_for_reloc (
+ arc_got_entry_type_for_reloc (howto),
+ get_got_entry_list_for_symbol (abfd, r_symndx, h),
+ info,
+ h);
+ }
+ }
+
+ return TRUE;
+}
+
+#define ELF_DYNAMIC_INTERPRETER "/sbin/ld-uClibc.so"
+
+static struct plt_version_t *
+arc_get_plt_version (struct bfd_link_info *info)
+{
+ int i;
+
+ for (i = 0; i < 1; i++)
+ {
+ ARC_DEBUG ("%d: size1 = %d, size2 = %d\n", i,
+ (int) plt_versions[i].entry_size,
+ (int) plt_versions[i].elem_size);
+ }
+
+ if (bfd_get_mach (info->output_bfd) == bfd_mach_arc_arcv2)
+ {
+ if (bfd_link_pic (info))
+ return &(plt_versions[ELF_ARCV2_PIC]);
+ else
+ return &(plt_versions[ELF_ARCV2_ABS]);
+ }
+ else
+ {
+ if (bfd_link_pic (info))
+ return &(plt_versions[ELF_ARC_PIC]);
+ else
+ return &(plt_versions[ELF_ARC_ABS]);
+ }
+}
+
+static bfd_vma
+add_symbol_to_plt (struct bfd_link_info *info)
+{
+ struct elf_link_hash_table *htab = elf_hash_table (info);
+ bfd_vma ret;
+
+ struct plt_version_t *plt_data = arc_get_plt_version (info);
+
+ /* If this is the first .plt entry, make room for the special first
+ entry. */
+ if (htab->splt->size == 0)
+ htab->splt->size += plt_data->entry_size;
+
+ ret = htab->splt->size;
+
+ htab->splt->size += plt_data->elem_size;
+ ARC_DEBUG ("PLT_SIZE = %d\n", (int) htab->splt->size);
+
+ htab->sgotplt->size += 4;
+ htab->srelplt->size += sizeof (Elf32_External_Rela);
+
+ return ret;
+}
+
+#define PLT_DO_RELOCS_FOR_ENTRY(ABFD, DS, RELOCS) \
+ plt_do_relocs_for_symbol (ABFD, DS, RELOCS, 0, 0)
+
+static void
+plt_do_relocs_for_symbol (bfd *abfd,
+ struct elf_link_hash_table *htab,
+ const struct plt_reloc *reloc,
+ bfd_vma plt_offset,
+ bfd_vma symbol_got_offset)
+{
+ while (SYM_ONLY (reloc->symbol) != LAST_RELOC)
+ {
+ bfd_vma relocation = 0;
+
+ switch (SYM_ONLY (reloc->symbol))
+ {
+ case SGOT:
+ relocation
+ = htab->sgotplt->output_section->vma
+ + htab->sgotplt->output_offset + symbol_got_offset;
+ break;
+ }
+ relocation += reloc->addend;
+
+ if (IS_RELATIVE (reloc->symbol))
+ {
+ bfd_vma reloc_offset = reloc->offset;
+ reloc_offset -= (IS_INSN_32 (reloc->symbol)) ? 4 : 0;
+ reloc_offset -= (IS_INSN_24 (reloc->symbol)) ? 2 : 0;
+
+ relocation -= htab->splt->output_section->vma
+ + htab->splt->output_offset
+ + plt_offset + reloc_offset;
+ }
+
+ /* TODO: being ME is not a property of the relocation but of the
+ section of which is applying the relocation. */
+ if (IS_MIDDLE_ENDIAN (reloc->symbol) && !bfd_big_endian (abfd))
+ {
+ relocation
+ = ((relocation & 0xffff0000) >> 16)
+ | ((relocation & 0xffff) << 16);
+ }
+
+ switch (reloc->size)
+ {
+ case 32:
+ bfd_put_32 (htab->splt->output_section->owner,
+ relocation,
+ htab->splt->contents + plt_offset + reloc->offset);
+ break;
+ }
+
+ reloc = &(reloc[1]); /* Jump to next relocation. */
+ }
+}
+
+static void
+relocate_plt_for_symbol (bfd *output_bfd,
+ struct bfd_link_info *info,
+ struct elf_link_hash_entry *h)
+{
+ struct plt_version_t *plt_data = arc_get_plt_version (info);
+ struct elf_link_hash_table *htab = elf_hash_table (info);
+
+ bfd_vma plt_index = (h->plt.offset - plt_data->entry_size)
+ / plt_data->elem_size;
+ bfd_vma got_offset = (plt_index + 3) * 4;
+
+ ARC_DEBUG ("arc_info: PLT_OFFSET = %#lx, PLT_ENTRY_VMA = %#lx, \
+GOT_ENTRY_OFFSET = %#lx, GOT_ENTRY_VMA = %#lx, for symbol %s\n",
+ (long) h->plt.offset,
+ (long) (htab->splt->output_section->vma
+ + htab->splt->output_offset
+ + h->plt.offset),
+ (long) got_offset,
+ (long) (htab->sgotplt->output_section->vma
+ + htab->sgotplt->output_offset
+ + got_offset),
+ h->root.root.string);
+
+ {
+ bfd_vma i = 0;
+ uint16_t *ptr = (uint16_t *) plt_data->elem;
+
+ for (i = 0; i < plt_data->elem_size/2; i++)
+ {
+ uint16_t data = ptr[i];
+ bfd_put_16 (output_bfd,
+ (bfd_vma) data,
+ htab->splt->contents + h->plt.offset + (i*2));
+ }
+ }
+
+ plt_do_relocs_for_symbol (output_bfd, htab,
+ plt_data->elem_relocs,
+ h->plt.offset,
+ got_offset);
+
+ /* Fill in the entry in the global offset table. */
+ bfd_put_32 (output_bfd,
+ (bfd_vma) (htab->splt->output_section->vma
+ + htab->splt->output_offset),
+ htab->sgotplt->contents + got_offset);
+
+ /* TODO: Fill in the entry in the .rela.plt section. */
+ {
+ Elf_Internal_Rela rel;
+ bfd_byte *loc;
+
+ rel.r_offset = (htab->sgotplt->output_section->vma
+ + htab->sgotplt->output_offset
+ + got_offset);
+ rel.r_addend = 0;
+
+ BFD_ASSERT (h->dynindx != -1);
+ rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_JMP_SLOT);
+
+ loc = htab->srelplt->contents;
+ loc += plt_index * sizeof (Elf32_External_Rela); /* relA */
+ bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
+ }
+}
+
+static void
+relocate_plt_for_entry (bfd *abfd,
+ struct bfd_link_info *info)
+{
+ struct plt_version_t *plt_data = arc_get_plt_version (info);
+ struct elf_link_hash_table *htab = elf_hash_table (info);
+
+ {
+ bfd_vma i = 0;
+ uint16_t *ptr = (uint16_t *) plt_data->entry;
+ for (i = 0; i < plt_data->entry_size/2; i++)
+ {
+ uint16_t data = ptr[i];
+ bfd_put_16 (abfd,
+ (bfd_vma) data,
+ htab->splt->contents + (i*2));
+ }
+ }
+ PLT_DO_RELOCS_FOR_ENTRY (abfd, htab, plt_data->entry_relocs);
+}
+
+/* Desc : Adjust a symbol defined by a dynamic object and referenced
+ by a regular object. The current definition is in some section of
+ the dynamic object, but we're not including those sections. We
+ have to change the definition to something the rest of the link can
+ understand. */
+
+static bfd_boolean
+elf_arc_adjust_dynamic_symbol (struct bfd_link_info *info,
+ struct elf_link_hash_entry *h)
+{
+ asection *s;
+ bfd *dynobj = (elf_hash_table (info))->dynobj;
+ struct elf_link_hash_table *htab = elf_hash_table (info);
+
+ if (h->type == STT_FUNC
+ || h->type == STT_GNU_IFUNC
+ || h->needs_plt == 1)
+ {
+ if (!bfd_link_pic (info) && !h->def_dynamic && !h->ref_dynamic)
+ {
+ /* This case can occur if we saw a PLT32 reloc in an input
+ file, but the symbol was never referred to by a dynamic
+ object. In such a case, we don't actually need to build
+ a procedure linkage table, and we can just do a PC32
+ reloc instead. */
+ BFD_ASSERT (h->needs_plt);
+ return TRUE;
+ }
+
+ /* Make sure this symbol is output as a dynamic symbol. */
+ if (h->dynindx == -1 && !h->forced_local
+ && !bfd_elf_link_record_dynamic_symbol (info, h))
+ return FALSE;
+
+ if (bfd_link_pic (info)
+ || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
+ {
+ bfd_vma loc = add_symbol_to_plt (info);
+
+ if (bfd_link_executable (info) && !h->def_regular)
+ {
+ h->root.u.def.section = htab->splt;
+ h->root.u.def.value = loc;
+ }
+ h->plt.offset = loc;
+ }
+ else
+ {
+ h->plt.offset = (bfd_vma) -1;
+ h->needs_plt = 0;
+ }
+ return TRUE;
+ }
+
+ /* If this is a weak symbol, and there is a real definition, the
+ processor independent code will have arranged for us to see the
+ real definition first, and we can just use the same value. */
+ if (h->is_weakalias)
+ {
+ struct elf_link_hash_entry *def = weakdef (h);
+ BFD_ASSERT (def->root.type == bfd_link_hash_defined);
+ h->root.u.def.section = def->root.u.def.section;
+ h->root.u.def.value = def->root.u.def.value;
+ return TRUE;
+ }
+
+ /* This is a reference to a symbol defined by a dynamic object which
+ is not a function. */
+
+ /* If we are creating a shared library, we must presume that the
+ only references to the symbol are via the global offset table.
+ For such cases we need not do anything here; the relocations will
+ be handled correctly by relocate_section. */
+ if (!bfd_link_executable (info))
+ return TRUE;
+
+ /* If there are no non-GOT references, we do not need a copy
+ relocation. */
+ if (!h->non_got_ref)
+ return TRUE;
+
+ /* If -z nocopyreloc was given, we won't generate them either. */
+ if (info->nocopyreloc)
+ {
+ h->non_got_ref = 0;
+ return TRUE;
+ }
+
+ /* We must allocate the symbol in our .dynbss section, which will
+ become part of the .bss section of the executable. There will be
+ an entry for this symbol in the .dynsym section. The dynamic
+ object will contain position independent code, so all references
+ from the dynamic object to this symbol will go through the global
+ offset table. The dynamic linker will use the .dynsym entry to
+ determine the address it must put in the global offset table, so
+ both the dynamic object and the regular object will refer to the
+ same memory location for the variable. */
+
+ if (htab == NULL)
+ return FALSE;
+
+ /* We must generate a R_ARC_COPY reloc to tell the dynamic linker to
+ copy the initial value out of the dynamic object and into the
+ runtime process image. We need to remember the offset into the
+ .rela.bss section we are going to use. */
+ if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
+ {
+ struct elf_arc_link_hash_table *arc_htab = elf_arc_hash_table (info);
+
+ BFD_ASSERT (arc_htab->elf.srelbss != NULL);
+ arc_htab->elf.srelbss->size += sizeof (Elf32_External_Rela);
+ h->needs_copy = 1;
+ }
+
+ /* TODO: Move this also to arc_hash_table. */
+ s = bfd_get_section_by_name (dynobj, ".dynbss");
+ BFD_ASSERT (s != NULL);
+
+ return _bfd_elf_adjust_dynamic_copy (info, h, s);