+ /* See above re bit 1 and bit 0 usage. */
+ if ((off & 2) == 0)
+ {
+ Elf_Internal_Rela outrel;
+ bfd_byte *loc;
+
+ off &= ~3;
+
+ if (h != NULL)
+ h->got.offset |= 2;
+ else
+ local_got_offsets[r_symndx] |= 2;
+
+ /* Clear the target contents of the GOT (just as a
+ gesture; it's already cleared on allocation): this
+ relocation is not like the other dynrelocs. */
+ bfd_put_32 (output_bfd, 0, sgot->contents + off);
+ bfd_put_32 (output_bfd, 0, sgot->contents + off + 4);
+
+ if (srelgot == NULL)
+ srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
+ BFD_ASSERT (srelgot != NULL);
+
+ if (h != NULL && h->dynindx != -1)
+ {
+ outrel.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_DTP);
+ relocation = 0;
+ }
+ else
+ {
+ outrel.r_info = ELF32_R_INFO (0, R_CRIS_DTP);
+
+ /* NULL if we had an error. */
+ relocation -= elf_hash_table (info)->tls_sec == NULL
+ ? 0 : elf_hash_table (info)->tls_sec->vma;
+ }
+
+ outrel.r_offset = (sgot->output_section->vma
+ + sgot->output_offset
+ + off);
+ outrel.r_addend = relocation;
+ loc = srelgot->contents;
+ loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
+
+ /* NULL if we had an error. */
+ if (srelgot->contents != NULL)
+ bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
+ }
+ else
+ off &= ~3;
+
+ relocation = sgot->output_offset + off
+ + (r_type == R_CRIS_32_GD ? sgot->output_section->vma : 0);
+ }
+
+ /* The GOT-relative offset to the GOT entry is the
+ relocation, or for R_CRIS_32_GD, the actual address of
+ the GOT entry. */
+ break;
+
+ case R_CRIS_32_GOT_TPREL:
+ case R_CRIS_16_GOT_TPREL:
+ if (rel->r_addend != 0)
+ {
+ /* We can't do anything for a relocation which is
+ against a symbol *plus offset*. GOT holds
+ relocations for symbols. Make this an error; the
+ compiler isn't allowed to pass us these kinds of
+ things. */
+ (*_bfd_error_handler)
+ (_("%B, section %A: relocation %s with non-zero addend %d"
+ " against symbol `%s'"),
+ input_bfd,
+ input_section,
+ cris_elf_howto_table[r_type].name,
+ rel->r_addend,
+ symname[0] != '\0' ? symname : _("[whose name is lost]"));
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
+ }
+
+ if (!info->shared && (h == NULL || h->def_regular))
+ {
+ /* Known contents of the GOT. */
+ bfd_vma off;
+
+ /* The symbol is defined in the program, so just write
+ the known_tpoffset into the GOT. */
+ relocation -= elf_hash_table (info)->tls_sec->vma;
+
+ if (h != NULL)
+ off = h->got.offset;
+ else
+ off = local_got_offsets[r_symndx];
+
+ /* Bit 0 is used to mark whether we've emitted the required
+ entry (and if needed R_CRIS_32_TPREL reloc). Bit 1
+ is used similarly for R_CRIS_DTP, see above. */
+ if ((off & 1) == 0)
+ {
+ off &= ~3;
+
+ if (h != NULL)
+ h->got.offset |= 1;
+ else
+ local_got_offsets[r_symndx] |= 1;
+
+ bfd_put_32 (output_bfd, relocation, sgot->contents + off);
+ }
+ else
+ off &= ~3;
+
+ relocation = sgot->output_offset + off;
+ }
+ else
+ {
+ /* Emit a real relocation. */
+ bfd_vma off;
+
+ if (h != NULL)
+ off = h->got.offset;
+ else
+ off = local_got_offsets[r_symndx];
+
+ /* See above re usage of bit 0 and 1. */
+ if ((off & 1) == 0)
+ {
+ Elf_Internal_Rela outrel;
+ bfd_byte *loc;
+
+ off &= ~3;
+
+ if (h != NULL)
+ h->got.offset |= 1;
+ else
+ local_got_offsets[r_symndx] |= 1;
+
+ if (srelgot == NULL)
+ srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
+ BFD_ASSERT (srelgot != NULL);
+
+ if (h != NULL && h->dynindx != -1)
+ {
+ outrel.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_32_TPREL);
+ relocation = 0;
+ }
+ else
+ {
+ outrel.r_info = ELF32_R_INFO (0, R_CRIS_32_TPREL);
+
+ /* NULL if we had an error. */
+ relocation -= elf_hash_table (info)->tls_sec == NULL
+ ? 0 : elf_hash_table (info)->tls_sec->vma;
+ }
+
+ /* Just "define" the initial contents in some
+ semi-logical way. */
+ bfd_put_32 (output_bfd, relocation, sgot->contents + off);
+
+ outrel.r_offset = (sgot->output_section->vma
+ + sgot->output_offset
+ + off);
+ outrel.r_addend = relocation;
+ loc = srelgot->contents;
+ loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
+ /* NULL if we had an error. */
+ if (srelgot->contents != NULL)
+ bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
+ }
+ else
+ off &= ~3;
+
+ relocation = sgot->output_offset + off;
+ }
+
+ /* The GOT-relative offset to the GOT entry is the relocation. */
+ break;
+
+ case R_CRIS_16_TPREL:
+ case R_CRIS_32_TPREL:
+ /* This relocation must only be performed against symbols
+ defined in an ordinary (non-DSO) object. */
+ if (info->shared)
+ {
+ bfd_set_error (bfd_error_invalid_operation);
+
+ /* We've already informed in cris_elf_check_relocs that
+ this is an error. */
+ return FALSE;
+ }
+
+ if (h != NULL
+ && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
+ && !h->def_regular
+ /* If it's undefined, then an error message has already
+ been emitted. */
+ && h->root.type != bfd_link_hash_undefined)
+ {
+ (*_bfd_error_handler)
+ (_("%B, section %A: relocation %s is"
+ " not allowed for symbol: `%s'"
+ " which is defined outside the program,"
+ " perhaps a declaration mixup?"),
+ input_bfd,
+ input_section,
+ cris_elf_howto_table[r_type].name,
+ symname);
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
+ }
+
+ /* NULL if we had an error. */
+ relocation -= elf_hash_table (info)->tls_sec == NULL
+ ? 0 : elf_hash_table (info)->tls_sec->vma;
+
+ /* The TLS-relative offset is the relocation. */
+ break;
+
+ default:
+ BFD_FAIL ();
+ return FALSE;
+ }
+
+ r = cris_final_link_relocate (howto, input_bfd, input_section,
+ contents, rel, relocation);
+
+ if (r != bfd_reloc_ok)
+ {
+ const char * msg = (const char *) NULL;
+
+ switch (r)
+ {
+ case bfd_reloc_overflow:
+ r = info->callbacks->reloc_overflow
+ (info, (h ? &h->root : NULL), symname, howto->name,
+ (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
+ if (additional_relocation_error_msg_count > 0)
+ {
+ additional_relocation_error_msg_count--;
+ switch (r_type)
+ {
+ case R_CRIS_16_GOTPLT:
+ case R_CRIS_16_GOT:
+
+ /* Not just TLS is involved here, so we make
+ generation and message depend on -fPIC/-fpic
+ only. */
+ case R_CRIS_16_GOT_TPREL:
+ case R_CRIS_16_GOT_GD:
+ (*_bfd_error_handler)
+ (_("(too many global variables for -fpic:"
+ " recompile with -fPIC)"));
+ break;
+
+ case R_CRIS_16_TPREL:
+ case R_CRIS_16_DTPREL:
+ (*_bfd_error_handler)
+ (_("(thread-local data too big for -fpic or"
+ " -msmall-tls: recompile with -fPIC or"
+ " -mno-small-tls)"));
+ break;
+
+ /* No known cause for overflow for other relocs. */
+ default:
+ break;
+ }
+ }
+ break;
+
+ case bfd_reloc_undefined:
+ r = info->callbacks->undefined_symbol
+ (info, symname, input_bfd, input_section, rel->r_offset,
+ TRUE);
+ break;
+
+ case bfd_reloc_outofrange:
+ msg = _("internal error: out of range error");
+ break;
+
+ case bfd_reloc_notsupported:
+ msg = _("internal error: unsupported relocation error");
+ break;
+
+ case bfd_reloc_dangerous:
+ msg = _("internal error: dangerous relocation");
+ break;
+
+ default:
+ msg = _("internal error: unknown error");
+ break;
+ }
+
+ if (msg)
+ r = info->callbacks->warning
+ (info, msg, symname, input_bfd, input_section, rel->r_offset);
+
+ if (! r)
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+\f
+/* Finish up dynamic symbol handling. We set the contents of various
+ dynamic sections here. */
+
+static bfd_boolean
+elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym)
+ bfd *output_bfd;
+ struct bfd_link_info *info;
+ struct elf_link_hash_entry *h;
+ Elf_Internal_Sym *sym;
+{
+ bfd *dynobj;
+
+ /* Where in the plt entry to put values. */
+ int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
+
+ /* What offset to add to the distance to the first PLT entry for the
+ value at plt_off3. */
+ int plt_off3_value_bias = 4;
+
+ /* Where in the PLT entry the call-dynlink-stub is (happens to be same
+ for PIC and non-PIC for v32 and pre-v32). */
+ int plt_stub_offset = 8;
+ int plt_entry_size = PLT_ENTRY_SIZE;
+ const bfd_byte *plt_entry = elf_cris_plt_entry;
+ const bfd_byte *plt_pic_entry = elf_cris_pic_plt_entry;
+
+ /* Adjust the various PLT entry offsets. */
+ if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
+ {
+ plt_off2 = 14;
+ plt_off3 = 20;
+ plt_off3_value_bias = -2;
+ plt_stub_offset = 12;
+ plt_entry_size = PLT_ENTRY_SIZE_V32;
+ plt_entry = elf_cris_plt_entry_v32;
+ plt_pic_entry = elf_cris_pic_plt_entry_v32;
+ }
+
+ dynobj = elf_hash_table (info)->dynobj;
+
+ if (h->plt.offset != (bfd_vma) -1)
+ {
+ asection *splt;
+ asection *sgotplt;
+ asection *sgot;
+ asection *srela;
+ bfd_vma got_base;
+
+ bfd_vma gotplt_offset
+ = elf_cris_hash_entry (h)->gotplt_offset;
+ Elf_Internal_Rela rela;
+ bfd_byte *loc;
+ bfd_boolean has_gotplt = gotplt_offset != 0;
+
+ /* Get the index in the procedure linkage table which
+ corresponds to this symbol. This is the index of this symbol
+ in all the symbols for which we are making plt entries. The
+ first entry in the procedure linkage table is reserved. */
+ /* We have to count backwards here, and the result is only valid as
+ an index into .got.plt and its relocations. FIXME: Constants... */
+ bfd_vma gotplt_index = gotplt_offset/4 - 3;
+
+ /* Get the offset into the .got table of the entry that corresponds
+ to this function. Note that we embed knowledge that "incoming"
+ .got goes after .got.plt in the output without padding (pointer
+ aligned). However, that knowledge is present in several other
+ places too. */
+ bfd_vma got_offset
+ = (has_gotplt
+ ? gotplt_offset
+ : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry);
+
+ /* This symbol has an entry in the procedure linkage table. Set it
+ up. */
+
+ BFD_ASSERT (h->dynindx != -1);