- h->needs_plt = 1;
- h->non_got_ref = 1;
- if (h->plt.refcount <= 0)
- h->plt.refcount = 1;
- else
- h->plt.refcount += 1;
- }
- }
-
- if (h->plt.refcount <= 0)
- {
- h->plt.offset = (bfd_vma) -1;
- h->needs_plt = 0;
- }
- return TRUE;
- }
-
- /* If this is a function, put it in the procedure linkage table. We
- will fill in the contents of the procedure linkage table later,
- when we know the address of the .got section. */
- if (h->type == STT_FUNC
- || h->needs_plt)
- {
- if (h->plt.refcount <= 0
- || SYMBOL_CALLS_LOCAL (info, h)
- || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
- && h->root.type == bfd_link_hash_undefweak))
- {
- /* 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, or if all references were garbage collected. In
- such a case, we don't actually need to build a procedure
- linkage table, and we can just do a PC32 reloc instead. */
- h->plt.offset = (bfd_vma) -1;
- h->needs_plt = 0;
- }
-
- return TRUE;
- }
- else
- /* It's possible that we incorrectly decided a .plt reloc was
- needed for an R_X86_64_PC32 reloc to a non-function sym in
- check_relocs. We can't decide accurately between function and
- non-function syms in check-relocs; Objects loaded later in
- the link may change h->type. So fix it now. */
- h->plt.offset = (bfd_vma) -1;
-
- /* 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->u.weakdef != NULL)
- {
- BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
- || h->u.weakdef->root.type == bfd_link_hash_defweak);
- h->root.u.def.section = h->u.weakdef->root.u.def.section;
- h->root.u.def.value = h->u.weakdef->root.u.def.value;
- if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
- {
- eh = (struct elf_x86_64_link_hash_entry *) h;
- h->non_got_ref = h->u.weakdef->non_got_ref;
- eh->needs_copy = h->u.weakdef->needs_copy;
- }
- 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 references to this symbol that do not use the
- GOT, we don't need to generate a copy reloc. */
- 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;
- }
-
- if (ELIMINATE_COPY_RELOCS)
- {
- eh = (struct elf_x86_64_link_hash_entry *) h;
- for (p = eh->dyn_relocs; p != NULL; p = p->next)
- {
- s = p->sec->output_section;
- if (s != NULL && (s->flags & SEC_READONLY) != 0)
- break;
- }
-
- /* If we didn't find any dynamic relocs in read-only sections, then
- we'll be keeping the dynamic relocs and avoiding the copy reloc. */
- if (p == NULL)
- {
- 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. */
-
- htab = elf_x86_64_hash_table (info);
- if (htab == NULL)
- return FALSE;
-
- /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
- to copy the initial value out of the dynamic object and into the
- runtime process image. */
- if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
- {
- const struct elf_backend_data *bed;
- bed = get_elf_backend_data (info->output_bfd);
- htab->srelbss->size += bed->s->sizeof_rela;
- h->needs_copy = 1;
- }
-
- s = htab->sdynbss;
-
- return _bfd_elf_adjust_dynamic_copy (info, h, s);
-}
-
-/* Allocate space in .plt, .got and associated reloc sections for
- dynamic relocs. */
-
-static bfd_boolean
-elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
-{
- struct bfd_link_info *info;
- struct elf_x86_64_link_hash_table *htab;
- struct elf_x86_64_link_hash_entry *eh;
- struct elf_dyn_relocs *p;
- const struct elf_backend_data *bed;
- unsigned int plt_entry_size;
-
- if (h->root.type == bfd_link_hash_indirect)
- return TRUE;
-
- eh = (struct elf_x86_64_link_hash_entry *) h;
-
- info = (struct bfd_link_info *) inf;
- htab = elf_x86_64_hash_table (info);
- if (htab == NULL)
- return FALSE;
- bed = get_elf_backend_data (info->output_bfd);
- plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
-
- /* We can't use the GOT PLT if pointer equality is needed since
- finish_dynamic_symbol won't clear symbol value and the dynamic
- linker won't update the GOT slot. We will get into an infinite
- loop at run-time. */
- if (htab->plt_got != NULL
- && h->type != STT_GNU_IFUNC
- && !h->pointer_equality_needed
- && h->plt.refcount > 0
- && h->got.refcount > 0)
- {
- /* Don't use the regular PLT if there are both GOT and GOTPLT
- reloctions. */
- h->plt.offset = (bfd_vma) -1;
-
- /* Use the GOT PLT. */
- eh->plt_got.refcount = 1;
- }
-
- /* Clear the reference count of function pointer relocations if
- symbol isn't a normal function. */
- if (h->type != STT_FUNC)
- eh->func_pointer_refcount = 0;
-
- /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
- here if it is defined and referenced in a non-shared object. */
- if (h->type == STT_GNU_IFUNC
- && h->def_regular)
- {
- if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h,
- &eh->dyn_relocs,
- plt_entry_size,
- plt_entry_size,
- GOT_ENTRY_SIZE))
- {
- asection *s = htab->plt_bnd;
- if (h->plt.offset != (bfd_vma) -1 && s != NULL)
- {
- /* Use the .plt.bnd section if it is created. */
- eh->plt_bnd.offset = s->size;
-
- /* Make room for this entry in the .plt.bnd section. */
- s->size += sizeof (elf_x86_64_legacy_plt2_entry);
- }
-
- return TRUE;
- }
- else
- return FALSE;
- }
- /* Don't create the PLT entry if there are only function pointer
- relocations which can be resolved at run-time. */
- else if (htab->elf.dynamic_sections_created
- && (h->plt.refcount > eh->func_pointer_refcount
- || eh->plt_got.refcount > 0))
- {
- bfd_boolean use_plt_got;
-
- /* Clear the reference count of function pointer relocations
- if PLT is used. */
- eh->func_pointer_refcount = 0;
-
- if ((info->flags & DF_BIND_NOW) && !h->pointer_equality_needed)
- {
- /* Don't use the regular PLT for DF_BIND_NOW. */
- h->plt.offset = (bfd_vma) -1;
-
- /* Use the GOT PLT. */
- h->got.refcount = 1;
- eh->plt_got.refcount = 1;
- }
-
- use_plt_got = eh->plt_got.refcount > 0;
-
- /* Make sure this symbol is output as a dynamic symbol.
- Undefined weak syms won't yet be marked as dynamic. */
- if (h->dynindx == -1
- && !h->forced_local)
- {
- if (! bfd_elf_link_record_dynamic_symbol (info, h))
- return FALSE;
- }
-
- if (bfd_link_pic (info)
- || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
- {
- asection *s = htab->elf.splt;
- asection *bnd_s = htab->plt_bnd;
- asection *got_s = htab->plt_got;
-
- /* If this is the first .plt entry, make room for the special
- first entry. The .plt section is used by prelink to undo
- prelinking for dynamic relocations. */
- if (s->size == 0)
- s->size = plt_entry_size;
-
- if (use_plt_got)
- eh->plt_got.offset = got_s->size;
- else
- {
- h->plt.offset = s->size;
- if (bnd_s)
- eh->plt_bnd.offset = bnd_s->size;
- }
-
- /* If this symbol is not defined in a regular file, and we are
- not generating a shared library, then set the symbol to this
- location in the .plt. This is required to make function
- pointers compare as equal between the normal executable and
- the shared library. */
- if (! bfd_link_pic (info)
- && !h->def_regular)
- {
- if (use_plt_got)
- {
- /* We need to make a call to the entry of the GOT PLT
- instead of regular PLT entry. */
- h->root.u.def.section = got_s;
- h->root.u.def.value = eh->plt_got.offset;
- }
- else
- {
- if (bnd_s)
- {
- /* We need to make a call to the entry of the second
- PLT instead of regular PLT entry. */
- h->root.u.def.section = bnd_s;
- h->root.u.def.value = eh->plt_bnd.offset;
- }
- else
- {
- h->root.u.def.section = s;
- h->root.u.def.value = h->plt.offset;
- }
- }
- }
-
- /* Make room for this entry. */
- if (use_plt_got)
- got_s->size += sizeof (elf_x86_64_legacy_plt2_entry);
- else
- {
- s->size += plt_entry_size;
- if (bnd_s)
- bnd_s->size += sizeof (elf_x86_64_legacy_plt2_entry);
-
- /* We also need to make an entry in the .got.plt section,
- which will be placed in the .got section by the linker
- script. */
- htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
-
- /* We also need to make an entry in the .rela.plt
- section. */
- htab->elf.srelplt->size += bed->s->sizeof_rela;
- htab->elf.srelplt->reloc_count++;
- }
- }
- else
- {
- h->plt.offset = (bfd_vma) -1;
- h->needs_plt = 0;
- }
- }
- else
- {
- h->plt.offset = (bfd_vma) -1;
- h->needs_plt = 0;
- }
-
- eh->tlsdesc_got = (bfd_vma) -1;
-
- /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
- make it a R_X86_64_TPOFF32 requiring no GOT entry. */
- if (h->got.refcount > 0
- && bfd_link_executable (info)
- && h->dynindx == -1
- && elf_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
- {
- h->got.offset = (bfd_vma) -1;
- }
- else if (h->got.refcount > 0)
- {
- asection *s;
- bfd_boolean dyn;
- int tls_type = elf_x86_64_hash_entry (h)->tls_type;
-
- /* Make sure this symbol is output as a dynamic symbol.
- Undefined weak syms won't yet be marked as dynamic. */
- if (h->dynindx == -1
- && !h->forced_local)
- {
- if (! bfd_elf_link_record_dynamic_symbol (info, h))
- return FALSE;
- }
-
- if (GOT_TLS_GDESC_P (tls_type))
- {
- eh->tlsdesc_got = htab->elf.sgotplt->size
- - elf_x86_64_compute_jump_table_size (htab);
- htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
- h->got.offset = (bfd_vma) -2;
- }
- if (! GOT_TLS_GDESC_P (tls_type)
- || GOT_TLS_GD_P (tls_type))
- {
- s = htab->elf.sgot;
- h->got.offset = s->size;
- s->size += GOT_ENTRY_SIZE;
- if (GOT_TLS_GD_P (tls_type))
- s->size += GOT_ENTRY_SIZE;
- }
- dyn = htab->elf.dynamic_sections_created;
- /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
- and two if global.
- R_X86_64_GOTTPOFF needs one dynamic relocation. */
- if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
- || tls_type == GOT_TLS_IE)
- htab->elf.srelgot->size += bed->s->sizeof_rela;
- else if (GOT_TLS_GD_P (tls_type))
- htab->elf.srelgot->size += 2 * bed->s->sizeof_rela;
- else if (! GOT_TLS_GDESC_P (tls_type)
- && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
- || h->root.type != bfd_link_hash_undefweak)
- && (bfd_link_pic (info)
- || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
- htab->elf.srelgot->size += bed->s->sizeof_rela;
- if (GOT_TLS_GDESC_P (tls_type))
- {
- htab->elf.srelplt->size += bed->s->sizeof_rela;
- htab->tlsdesc_plt = (bfd_vma) -1;
- }
- }
- else
- h->got.offset = (bfd_vma) -1;
-
- if (eh->dyn_relocs == NULL)
- return TRUE;
-
- /* In the shared -Bsymbolic case, discard space allocated for
- dynamic pc-relative relocs against symbols which turn out to be
- defined in regular objects. For the normal shared case, discard
- space for pc-relative relocs that have become local due to symbol
- visibility changes. */
-
- if (bfd_link_pic (info))
- {
- /* Relocs that use pc_count are those that appear on a call
- insn, or certain REL relocs that can generated via assembly.
- We want calls to protected symbols to resolve directly to the
- function rather than going via the plt. If people want
- function pointer comparisons to work as expected then they
- should avoid writing weird assembly. */
- if (SYMBOL_CALLS_LOCAL (info, h))
- {
- struct elf_dyn_relocs **pp;
-
- for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
- {
- p->count -= p->pc_count;
- p->pc_count = 0;
- if (p->count == 0)
- *pp = p->next;
- else
- pp = &p->next;
- }
- }
-
- /* Also discard relocs on undefined weak syms with non-default
- visibility. */
- if (eh->dyn_relocs != NULL)
- {
- if (h->root.type == bfd_link_hash_undefweak)
- {
- if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
- eh->dyn_relocs = NULL;
-
- /* Make sure undefined weak symbols are output as a dynamic
- symbol in PIEs. */
- else if (h->dynindx == -1
- && ! h->forced_local
- && ! bfd_elf_link_record_dynamic_symbol (info, h))
- return FALSE;
- }
- /* For PIE, discard space for pc-relative relocs against
- symbols which turn out to need copy relocs. */
- else if (bfd_link_executable (info)
- && (h->needs_copy || eh->needs_copy)
- && h->def_dynamic
- && !h->def_regular)
- {
- struct elf_dyn_relocs **pp;
-
- for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
- {
- if (p->pc_count != 0)
- *pp = p->next;
- else
- pp = &p->next;
- }
- }
- }
- }
- else if (ELIMINATE_COPY_RELOCS)
- {
- /* For the non-shared case, discard space for relocs against
- symbols which turn out to need copy relocs or are not
- dynamic. Keep dynamic relocations for run-time function
- pointer initialization. */
-
- if ((!h->non_got_ref || eh->func_pointer_refcount > 0)
- && ((h->def_dynamic
- && !h->def_regular)
- || (htab->elf.dynamic_sections_created
- && (h->root.type == bfd_link_hash_undefweak
- || h->root.type == bfd_link_hash_undefined))))
- {
- /* Make sure this symbol is output as a dynamic symbol.
- Undefined weak syms won't yet be marked as dynamic. */
- if (h->dynindx == -1
- && ! h->forced_local
- && ! bfd_elf_link_record_dynamic_symbol (info, h))
- return FALSE;
-
- /* If that succeeded, we know we'll be keeping all the
- relocs. */
- if (h->dynindx != -1)
- goto keep;
- }
-
- eh->dyn_relocs = NULL;
- eh->func_pointer_refcount = 0;
-
- keep: ;
- }
-
- /* Finally, allocate space. */
- for (p = eh->dyn_relocs; p != NULL; p = p->next)
- {
- asection * sreloc;
-
- sreloc = elf_section_data (p->sec)->sreloc;
-
- BFD_ASSERT (sreloc != NULL);
-
- sreloc->size += p->count * bed->s->sizeof_rela;
- }
-
- return TRUE;
-}
-
-/* Allocate space in .plt, .got and associated reloc sections for
- local dynamic relocs. */
-
-static bfd_boolean
-elf_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
-{
- struct elf_link_hash_entry *h
- = (struct elf_link_hash_entry *) *slot;
-
- if (h->type != STT_GNU_IFUNC
- || !h->def_regular
- || !h->ref_regular
- || !h->forced_local
- || h->root.type != bfd_link_hash_defined)
- abort ();
-
- return elf_x86_64_allocate_dynrelocs (h, inf);
-}
-
-/* Find any dynamic relocs that apply to read-only sections. */
-
-static bfd_boolean
-elf_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h,
- void * inf)
-{
- struct elf_x86_64_link_hash_entry *eh;
- struct elf_dyn_relocs *p;
-
- /* Skip local IFUNC symbols. */
- if (h->forced_local && h->type == STT_GNU_IFUNC)
- return TRUE;
-
- eh = (struct elf_x86_64_link_hash_entry *) h;
- for (p = eh->dyn_relocs; p != NULL; p = p->next)
- {
- asection *s = p->sec->output_section;
-
- if (s != NULL && (s->flags & SEC_READONLY) != 0)
- {
- struct bfd_link_info *info = (struct bfd_link_info *) inf;
-
- info->flags |= DF_TEXTREL;
-
- if ((info->warn_shared_textrel && bfd_link_pic (info))
- || info->error_textrel)
- info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
- p->sec->owner, h->root.root.string,
- p->sec);
-
- /* Not an error, just cut short the traversal. */
- return FALSE;
- }
- }
- return TRUE;
-}
-
-/* With the local symbol, foo, we convert
- mov foo@GOTPCREL(%rip), %reg
- to
- lea foo(%rip), %reg
- and convert
- call/jmp *foo@GOTPCREL(%rip)
- to
- nop call foo/jmp foo nop
- When PIC is false, convert
- test %reg, foo@GOTPCREL(%rip)
- to
- test $foo, %reg
- and convert
- binop foo@GOTPCREL(%rip), %reg
- to
- binop $foo, %reg
- where binop is one of adc, add, and, cmp, or, sbb, sub, xor
- instructions. */
-
-static bfd_boolean
-elf_x86_64_convert_load (bfd *abfd, asection *sec,
- struct bfd_link_info *link_info)
-{
- Elf_Internal_Shdr *symtab_hdr;
- Elf_Internal_Rela *internal_relocs;
- Elf_Internal_Rela *irel, *irelend;
- bfd_byte *contents;
- struct elf_x86_64_link_hash_table *htab;
- bfd_boolean changed_contents;
- bfd_boolean changed_relocs;
- bfd_signed_vma *local_got_refcounts;
- bfd_vma maxpagesize;
-
- /* Don't even try to convert non-ELF outputs. */
- if (!is_elf_hash_table (link_info->hash))
- return FALSE;
-
- /* Nothing to do if there is no need or no output. */
- if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
- || sec->need_convert_load == 0
- || bfd_is_abs_section (sec->output_section))
- return TRUE;
-
- symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
-
- /* Load the relocations for this section. */
- internal_relocs = (_bfd_elf_link_read_relocs
- (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
- link_info->keep_memory));
- if (internal_relocs == NULL)
- return FALSE;
-
- htab = elf_x86_64_hash_table (link_info);
- changed_contents = FALSE;
- changed_relocs = FALSE;
- local_got_refcounts = elf_local_got_refcounts (abfd);
- maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
-
- /* Get the section contents. */
- if (elf_section_data (sec)->this_hdr.contents != NULL)
- contents = elf_section_data (sec)->this_hdr.contents;
- else
- {
- if (!bfd_malloc_and_get_section (abfd, sec, &contents))
- goto error_return;
- }
-
- irelend = internal_relocs + sec->reloc_count;
- for (irel = internal_relocs; irel < irelend; irel++)
- {
- unsigned int r_type = ELF32_R_TYPE (irel->r_info);
- unsigned int r_symndx = htab->r_sym (irel->r_info);
- unsigned int indx;
- struct elf_link_hash_entry *h;
- asection *tsec;
- char symtype;
- bfd_vma toff, roff;
- bfd_signed_vma raddend;
- unsigned int opcode;
- unsigned int modrm;
-
- if (r_type != R_X86_64_GOTPCREL
- && r_type != R_X86_64_GOTPCRELX
- && r_type != R_X86_64_REX_GOTPCRELX)
- continue;
-
- roff = irel->r_offset;
- if (roff < (r_type == R_X86_64_REX_GOTPCRELX ? 3 : 2))
- continue;
-
- raddend = irel->r_addend;
- /* Addend for 32-bit PC-relative relocation must be -4. */
- if (raddend != -4)
- continue;
-
- opcode = bfd_get_8 (abfd, contents + roff - 2);
-
- /* It is OK to convert mov to lea. */
- if (opcode != 0x8b)
- {
- /* Only convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX
- for mov call, jmp or one of adc, add, and, cmp, or, sbb,
- sub, test, xor instructions. */
- if (r_type != R_X86_64_GOTPCRELX
- && r_type != R_X86_64_REX_GOTPCRELX)
- continue;
-
- /* It is OK to convert indirect branch to direct branch. */
- if (opcode != 0xff)
- {
- /* It is OK to convert adc, add, and, cmp, or, sbb, sub,
- test, xor only when PIC is false. */
- if (bfd_link_pic (link_info))
- continue;
- }
- }
-
- /* Get the symbol referred to by the reloc. */
- if (r_symndx < symtab_hdr->sh_info)
- {
- Elf_Internal_Sym *isym;
-
- isym = bfd_sym_from_r_symndx (&htab->sym_cache,
- abfd, r_symndx);
-
- symtype = ELF_ST_TYPE (isym->st_info);
-
- /* STT_GNU_IFUNC must keep GOTPCREL relocations and skip
- relocation against undefined symbols. */
- if (symtype == STT_GNU_IFUNC || isym->st_shndx == SHN_UNDEF)
- continue;
-
- if (isym->st_shndx == SHN_ABS)
- tsec = bfd_abs_section_ptr;
- else if (isym->st_shndx == SHN_COMMON)
- tsec = bfd_com_section_ptr;
- else if (isym->st_shndx == SHN_X86_64_LCOMMON)
- tsec = &_bfd_elf_large_com_section;
- else
- tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
-
- h = NULL;
- toff = isym->st_value;
- }
- else
- {
- indx = r_symndx - symtab_hdr->sh_info;
- h = elf_sym_hashes (abfd)[indx];
- BFD_ASSERT (h != NULL);
-
- 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;
-
- /* STT_GNU_IFUNC must keep GOTPCREL relocations. We also
- avoid optimizing GOTPCREL relocations againt _DYNAMIC
- since ld.so may use its link-time address. */
- if ((h->def_regular
- || h->root.type == bfd_link_hash_defined
- || h->root.type == bfd_link_hash_defweak)
- && h->type != STT_GNU_IFUNC
- && h != htab->elf.hdynamic
- && SYMBOL_REFERENCES_LOCAL (link_info, h))
- {
- /* bfd_link_hash_new or bfd_link_hash_undefined is
- set by an assignment in a linker script in
- bfd_elf_record_link_assignment. FIXME: If we
- ever get a linker error due relocation overflow,
- we will skip this optimization. */
- if (h->def_regular
- && (h->root.type == bfd_link_hash_new
- || h->root.type == bfd_link_hash_undefined))
- goto convert;
- tsec = h->root.u.def.section;
- toff = h->root.u.def.value;
- symtype = h->type;
- }
- else
- continue;
- }
-
- if (tsec->sec_info_type == SEC_INFO_TYPE_MERGE)
- {
- /* At this stage in linking, no SEC_MERGE symbol has been
- adjusted, so all references to such symbols need to be
- passed through _bfd_merged_section_offset. (Later, in
- relocate_section, all SEC_MERGE symbols *except* for
- section symbols have been adjusted.)
-
- gas may reduce relocations against symbols in SEC_MERGE
- sections to a relocation against the section symbol when
- the original addend was zero. When the reloc is against
- a section symbol we should include the addend in the
- offset passed to _bfd_merged_section_offset, since the
- location of interest is the original symbol. On the
- other hand, an access to "sym+addend" where "sym" is not
- a section symbol should not include the addend; Such an
- access is presumed to be an offset from "sym"; The
- location of interest is just "sym". */
- if (symtype == STT_SECTION)
- toff += raddend;
-
- toff = _bfd_merged_section_offset (abfd, &tsec,
- elf_section_data (tsec)->sec_info,
- toff);
-
- if (symtype != STT_SECTION)
- toff += raddend;
- }
- else
- toff += raddend;
-
- /* Don't convert if R_X86_64_PC32 relocation overflows. */
- if (tsec->output_section == sec->output_section)
- {
- if ((toff - roff + 0x80000000) > 0xffffffff)
- continue;
- }
- else
- {
- asection *asect;
- bfd_size_type size;
-
- /* At this point, we don't know the load addresses of TSEC
- section nor SEC section. We estimate the distrance between
- SEC and TSEC. */
- size = 0;
- for (asect = sec->output_section;
- asect != NULL && asect != tsec->output_section;
- asect = asect->next)
- {
- asection *i;
- for (i = asect->output_section->map_head.s;
- i != NULL;
- i = i->map_head.s)
- {
- size = align_power (size, i->alignment_power);
- size += i->size;
- }
- }
-
- /* Don't convert GOTPCREL relocations if TSEC isn't placed
- after SEC. */
- if (asect == NULL)
- continue;
-
- /* Take PT_GNU_RELRO segment into account by adding
- maxpagesize. */
- if ((toff + size + maxpagesize - roff + 0x80000000)
- > 0xffffffff)
- continue;
- }
-
-convert:
- if (opcode == 0xff)
- {
- /* We have "call/jmp *foo@GOTPCREL(%rip)". */
- unsigned int nop;
- unsigned int disp;
- bfd_vma nop_offset;
-
- /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
- R_X86_64_PC32. */
- modrm = bfd_get_8 (abfd, contents + roff - 1);
- if (modrm == 0x25)
- {
- /* Convert to "jmp foo nop". */
- modrm = 0xe9;
- nop = NOP_OPCODE;
- nop_offset = irel->r_offset + 3;
- disp = bfd_get_32 (abfd, contents + irel->r_offset);
- irel->r_offset -= 1;
- bfd_put_32 (abfd, disp, contents + irel->r_offset);
- }
- else
- {
- /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
- is a nop prefix. */
- modrm = 0xe8;
- nop = link_info->call_nop_byte;
- if (link_info->call_nop_as_suffix)
- {
- nop_offset = irel->r_offset + 3;
- disp = bfd_get_32 (abfd, contents + irel->r_offset);
- irel->r_offset -= 1;
- bfd_put_32 (abfd, disp, contents + irel->r_offset);
- }
- else
- nop_offset = irel->r_offset - 2;
- }
- bfd_put_8 (abfd, nop, contents + nop_offset);
- bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
- r_type = R_X86_64_PC32;
- }
- else
- {
- if (opcode == 0x8b)
- {
- /* Convert "mov foo@GOTPCREL(%rip), %reg" to
- "lea foo(%rip), %reg". */
- opcode = 0x8d;
- r_type = R_X86_64_PC32;
- }
- else
- {
- modrm = bfd_get_8 (abfd, contents + roff - 1);
- if (opcode == 0x85)
- {
- /* Convert "test %reg, foo@GOTPCREL(%rip)" to
- "test $foo, %reg". */
- modrm = 0xc0 | (modrm & 0x38) >> 3;
- opcode = 0xf7;
- }
- else
- {
- /* Convert "binop foo@GOTPCREL(%rip), %reg" to
- "binop $foo, %reg". */
- modrm = 0xc0 | (modrm & 0x38) >> 3 | (opcode & 0x3c);
- opcode = 0x81;
- }
- bfd_put_8 (abfd, modrm, contents + roff - 1);
-
- if (r_type == R_X86_64_REX_GOTPCRELX)
- {
- /* Move the R bit to the B bit in REX byte. */
- unsigned int rex = bfd_get_8 (abfd, contents + roff - 3);
- rex = (rex & ~REX_R) | (rex & REX_R) >> 2;
- bfd_put_8 (abfd, rex, contents + roff - 3);
- }
- /* No addend for R_X86_64_32S relocation. */
- irel->r_addend = 0;
- r_type = R_X86_64_32S;
- }
-
- bfd_put_8 (abfd, opcode, contents + roff - 2);
- }
-
- irel->r_info = htab->r_info (r_symndx, r_type);
- changed_contents = TRUE;
- changed_relocs = TRUE;
-
- if (h)
- {
- if (h->got.refcount > 0)
- h->got.refcount -= 1;
- }
- else
- {
- if (local_got_refcounts != NULL
- && local_got_refcounts[r_symndx] > 0)
- local_got_refcounts[r_symndx] -= 1;
- }
- }
-
- if (contents != NULL
- && elf_section_data (sec)->this_hdr.contents != contents)
- {
- if (!changed_contents && !link_info->keep_memory)
- free (contents);
- else
- {
- /* Cache the section contents for elf_link_input_bfd. */
- elf_section_data (sec)->this_hdr.contents = contents;
- }
- }
-
- if (elf_section_data (sec)->relocs != internal_relocs)
- {
- if (!changed_relocs)
- free (internal_relocs);
- else
- elf_section_data (sec)->relocs = internal_relocs;
- }
-
- return TRUE;
-
- error_return:
- if (contents != NULL
- && elf_section_data (sec)->this_hdr.contents != contents)
- free (contents);
- if (internal_relocs != NULL
- && elf_section_data (sec)->relocs != internal_relocs)
- free (internal_relocs);
- return FALSE;
-}
-
-/* Set the sizes of the dynamic sections. */
-
-static bfd_boolean
-elf_x86_64_size_dynamic_sections (bfd *output_bfd,
- struct bfd_link_info *info)
-{
- struct elf_x86_64_link_hash_table *htab;
- bfd *dynobj;
- asection *s;
- bfd_boolean relocs;
- bfd *ibfd;
- const struct elf_backend_data *bed;
-
- htab = elf_x86_64_hash_table (info);
- if (htab == NULL)
- return FALSE;
- bed = get_elf_backend_data (output_bfd);
-
- dynobj = htab->elf.dynobj;
- if (dynobj == NULL)
- abort ();
-
- if (htab->elf.dynamic_sections_created)
- {
- /* Set the contents of the .interp section to the interpreter. */
- if (bfd_link_executable (info) && !info->nointerp)
- {
- s = bfd_get_linker_section (dynobj, ".interp");
- if (s == NULL)
- abort ();
- s->size = htab->dynamic_interpreter_size;
- s->contents = (unsigned char *) htab->dynamic_interpreter;
- }
- }
-
- /* Set up .got offsets for local syms, and space for local dynamic
- relocs. */
- for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
- {
- bfd_signed_vma *local_got;
- bfd_signed_vma *end_local_got;
- char *local_tls_type;
- bfd_vma *local_tlsdesc_gotent;
- bfd_size_type locsymcount;
- Elf_Internal_Shdr *symtab_hdr;
- asection *srel;
-
- if (! is_x86_64_elf (ibfd))
- continue;
-
- for (s = ibfd->sections; s != NULL; s = s->next)
- {
- struct elf_dyn_relocs *p;
-
- if (!elf_x86_64_convert_load (ibfd, s, info))
- return FALSE;
-
- for (p = (struct elf_dyn_relocs *)
- (elf_section_data (s)->local_dynrel);
- p != NULL;
- p = p->next)
- {
- if (!bfd_is_abs_section (p->sec)
- && bfd_is_abs_section (p->sec->output_section))
- {
- /* Input section has been discarded, either because
- it is a copy of a linkonce section or due to
- linker script /DISCARD/, so we'll be discarding
- the relocs too. */
- }
- else if (p->count != 0)
- {
- srel = elf_section_data (p->sec)->sreloc;
- srel->size += p->count * bed->s->sizeof_rela;
- if ((p->sec->output_section->flags & SEC_READONLY) != 0
- && (info->flags & DF_TEXTREL) == 0)
- {
- info->flags |= DF_TEXTREL;
- if ((info->warn_shared_textrel && bfd_link_pic (info))
- || info->error_textrel)
- info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
- p->sec->owner, p->sec);
- }
- }
- }
- }
-
- local_got = elf_local_got_refcounts (ibfd);
- if (!local_got)
- continue;
-
- symtab_hdr = &elf_symtab_hdr (ibfd);
- locsymcount = symtab_hdr->sh_info;
- end_local_got = local_got + locsymcount;
- local_tls_type = elf_x86_64_local_got_tls_type (ibfd);
- local_tlsdesc_gotent = elf_x86_64_local_tlsdesc_gotent (ibfd);
- s = htab->elf.sgot;
- srel = htab->elf.srelgot;
- for (; local_got < end_local_got;
- ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
- {
- *local_tlsdesc_gotent = (bfd_vma) -1;
- if (*local_got > 0)
- {
- if (GOT_TLS_GDESC_P (*local_tls_type))
- {
- *local_tlsdesc_gotent = htab->elf.sgotplt->size
- - elf_x86_64_compute_jump_table_size (htab);
- htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
- *local_got = (bfd_vma) -2;
- }
- if (! GOT_TLS_GDESC_P (*local_tls_type)
- || GOT_TLS_GD_P (*local_tls_type))
- {
- *local_got = s->size;
- s->size += GOT_ENTRY_SIZE;
- if (GOT_TLS_GD_P (*local_tls_type))
- s->size += GOT_ENTRY_SIZE;
- }
- if (bfd_link_pic (info)
- || GOT_TLS_GD_ANY_P (*local_tls_type)
- || *local_tls_type == GOT_TLS_IE)
- {
- if (GOT_TLS_GDESC_P (*local_tls_type))
- {
- htab->elf.srelplt->size
- += bed->s->sizeof_rela;
- htab->tlsdesc_plt = (bfd_vma) -1;
- }
- if (! GOT_TLS_GDESC_P (*local_tls_type)
- || GOT_TLS_GD_P (*local_tls_type))
- srel->size += bed->s->sizeof_rela;
- }
- }
- else
- *local_got = (bfd_vma) -1;
- }
- }
-
- if (htab->tls_ld_got.refcount > 0)
- {
- /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
- relocs. */
- htab->tls_ld_got.offset = htab->elf.sgot->size;
- htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
- htab->elf.srelgot->size += bed->s->sizeof_rela;
- }
- else
- htab->tls_ld_got.offset = -1;
-
- /* Allocate global sym .plt and .got entries, and space for global
- sym dynamic relocs. */
- elf_link_hash_traverse (&htab->elf, elf_x86_64_allocate_dynrelocs,
- info);
-
- /* Allocate .plt and .got entries, and space for local symbols. */
- htab_traverse (htab->loc_hash_table,
- elf_x86_64_allocate_local_dynrelocs,
- info);
-
- /* For every jump slot reserved in the sgotplt, reloc_count is
- incremented. However, when we reserve space for TLS descriptors,
- it's not incremented, so in order to compute the space reserved
- for them, it suffices to multiply the reloc count by the jump
- slot size.
-
- PR ld/13302: We start next_irelative_index at the end of .rela.plt
- so that R_X86_64_IRELATIVE entries come last. */
- if (htab->elf.srelplt)
- {
- htab->sgotplt_jump_table_size
- = elf_x86_64_compute_jump_table_size (htab);
- htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
- }
- else if (htab->elf.irelplt)
- htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
-
- if (htab->tlsdesc_plt)
- {
- /* If we're not using lazy TLS relocations, don't generate the
- PLT and GOT entries they require. */
- if ((info->flags & DF_BIND_NOW))
- htab->tlsdesc_plt = 0;
- else
- {
- htab->tlsdesc_got = htab->elf.sgot->size;
- htab->elf.sgot->size += GOT_ENTRY_SIZE;
- /* Reserve room for the initial entry.
- FIXME: we could probably do away with it in this case. */
- if (htab->elf.splt->size == 0)
- htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
- htab->tlsdesc_plt = htab->elf.splt->size;
- htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
- }
- }
-
- if (htab->elf.sgotplt)
- {
- /* Don't allocate .got.plt section if there are no GOT nor PLT
- entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
- if ((htab->elf.hgot == NULL
- || !htab->elf.hgot->ref_regular_nonweak)
- && (htab->elf.sgotplt->size
- == get_elf_backend_data (output_bfd)->got_header_size)
- && (htab->elf.splt == NULL
- || htab->elf.splt->size == 0)
- && (htab->elf.sgot == NULL
- || htab->elf.sgot->size == 0)
- && (htab->elf.iplt == NULL
- || htab->elf.iplt->size == 0)
- && (htab->elf.igotplt == NULL
- || htab->elf.igotplt->size == 0))
- htab->elf.sgotplt->size = 0;
- }
-
- if (htab->plt_eh_frame != NULL
- && htab->elf.splt != NULL
- && htab->elf.splt->size != 0
- && !bfd_is_abs_section (htab->elf.splt->output_section)
- && _bfd_elf_eh_frame_present (info))
- {
- const struct elf_x86_64_backend_data *arch_data
- = get_elf_x86_64_arch_data (bed);
- htab->plt_eh_frame->size = arch_data->eh_frame_plt_size;
- }