summaryrefslogtreecommitdiff
path: root/bfd/elf64-x86-64.c
diff options
context:
space:
mode:
authorH.J. Lu <hjl.tools@gmail.com>2017-09-07 04:03:15 -0700
committerH.J. Lu <hjl.tools@gmail.com>2017-09-07 04:03:30 -0700
commit6999821f8b1e898f02d0c1196c4c83337c3b565e (patch)
treeafd4c30d7e1fc646fe5c27996bb4ce58db0ec6b8 /bfd/elf64-x86-64.c
parent639257e981e61472e5fc215eb60c5d545ac5e2ab (diff)
x86: Remove _bfd_{i386,x86_64}_elf_convert_load
Instead of converting GOT relocations when sizing dynamic sections, we convert GOT relocations during relocation check. Add a field, local_ref, to elf_x86_link_hash_entry to indicate if symbol references are always local with a new function to check if symbol references are always local, which works in check_relocs. * elf32-i386.c (elf_i386_convert_load_reloc): Add an argument, r_type_p. Remove the converted argument. Replace SYMBOL_REFERENCES_LOCAL with SYMBOL_REFERENCES_LOCAL_P. Return the new relocation type via r_type_p. (elf_i386_relocate_section): Likewise. (elf_i386_finish_dynamic_symbol): Likewise. (need_convert_load): Removed. (check_relocs_failed): Updated. (elf_i386_check_relocs): Call elf_i386_convert_load_reloc, instead of setting need_convert_load. (_bfd_i386_elf_convert_load): Removed. * elf64-x86-64.c (need_convert_load): Removed. (check_relocs_failed): Updated. (elf_x86_64_convert_load_reloc): Add an argument, r_type_p. Replace SYMBOL_REFERENCES_LOCAL with SYMBOL_REFERENCES_LOCAL_P. Return the new relocation type via r_type_p. (elf_x86_64_check_relocs): Call elf_x86_64_convert_load_reloc, instead of setting need_convert_load. (elf_x86_64_check_relocs): Don't check PIC if relocation has been converted. (_bfd_x86_64_elf_convert_load): Removed. (elf_x86_64_relocate_section): Replace SYMBOL_REFERENCES_LOCAL with SYMBOL_REFERENCES_LOCAL_P. (elf_x86_64_finish_dynamic_symbol): Likewise. * elfxx-x86.c (_bfd_x86_elf_link_hash_table_create): Don't set convert_load. (_bfd_x86_elf_size_dynamic_sections): Don't call convert_load. (_bfd_x86_elf_link_symbol_references_local): New function. * elfxx-x86.h (SYMBOL_REFERENCES_LOCAL_P): New. (UNDEFINED_WEAK_RESOLVED_TO_ZERO): Replace elf.forced_local with SYMBOL_REFERENCES_LOCAL_P. (elf_x86_link_hash_entry): Add local_ref. (elf_x86_link_hash_table): Remove convert_load. (_bfd_i386_elf_convert_load): Removed. (_bfd_x86_64_elf_convert_load): Likewise. (_bfd_x86_elf_link_symbol_references_local): New.
Diffstat (limited to 'bfd/elf64-x86-64.c')
-rw-r--r--bfd/elf64-x86-64.c170
1 files changed, 25 insertions, 145 deletions
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index 8f1ac1a358..a2a4be84d6 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -1362,8 +1362,7 @@ elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
/* Rename some of the generic section flags to better document how they
are used here. */
-#define need_convert_load sec_flg0
-#define check_relocs_failed sec_flg1
+#define check_relocs_failed sec_flg0
static bfd_boolean
elf_x86_64_need_pic (struct bfd_link_info *info,
@@ -1450,6 +1449,7 @@ elf_x86_64_need_pic (struct bfd_link_info *info,
static bfd_boolean
elf_x86_64_convert_load_reloc (bfd *abfd,
bfd_byte *contents,
+ unsigned int *r_type_p,
Elf_Internal_Rela *irel,
struct elf_link_hash_entry *h,
bfd_boolean *converted,
@@ -1464,7 +1464,7 @@ elf_x86_64_convert_load_reloc (bfd *abfd,
bfd_signed_vma raddend;
unsigned int opcode;
unsigned int modrm;
- unsigned int r_type = ELF32_R_TYPE (irel->r_info);
+ unsigned int r_type = *r_type_p;
unsigned int r_symndx;
bfd_vma roff = irel->r_offset;
@@ -1572,7 +1572,7 @@ elf_x86_64_convert_load_reloc (bfd *abfd,
|| h->root.type == bfd_link_hash_defined
|| h->root.type == bfd_link_hash_defweak)
&& h != htab->elf.hdynamic
- && SYMBOL_REFERENCES_LOCAL (link_info, h)))
+ && SYMBOL_REFERENCES_LOCAL_P (link_info, h)))
{
/* bfd_link_hash_new or bfd_link_hash_undefined is
set by an assignment in a linker script in
@@ -1749,6 +1749,7 @@ rewrite_modrm_rex:
bfd_put_8 (abfd, opcode, contents + roff - 2);
}
+ *r_type_p = r_type;
irel->r_info = htab->r_info (r_symndx,
r_type | R_X86_64_converted_reloc_bit);
@@ -1819,6 +1820,7 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
Elf_Internal_Sym *isym;
const char *name;
bfd_boolean size_reloc;
+ bfd_boolean converted_reloc;
r_symndx = htab->r_sym (rel->r_info);
r_type = ELF32_R_TYPE (rel->r_info);
@@ -1912,6 +1914,19 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
|= elf_gnu_symbol_ifunc;
}
+ converted_reloc = FALSE;
+ if ((r_type == R_X86_64_GOTPCREL
+ || r_type == R_X86_64_GOTPCRELX
+ || r_type == R_X86_64_REX_GOTPCRELX)
+ && (h == NULL || h->type != STT_GNU_IFUNC))
+ {
+ Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
+ if (!elf_x86_64_convert_load_reloc (abfd, contents, &r_type,
+ irel, h, &converted_reloc,
+ info))
+ goto error_return;
+ }
+
if (! elf_x86_64_tls_transition (info, abfd, sec, contents,
symtab_hdr, sym_hashes,
&r_type, GOT_UNKNOWN,
@@ -2087,6 +2102,7 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
sections we don't care about, such as debug sections or
when relocation overflow check is disabled. */
if (!info->no_reloc_overflow_check
+ && !converted_reloc
&& (bfd_link_pic (info)
|| (bfd_link_executable (info)
&& h != NULL
@@ -2280,12 +2296,6 @@ do_size:
default:
break;
}
-
- if ((r_type == R_X86_64_GOTPCREL
- || r_type == R_X86_64_GOTPCRELX
- || r_type == R_X86_64_REX_GOTPCRELX)
- && (h == NULL || h->type != STT_GNU_IFUNC))
- sec->need_convert_load = 1;
}
if (elf_section_data (sec)->this_hdr.contents != contents)
@@ -2308,136 +2318,6 @@ error_return:
return FALSE;
}
-/* Convert load via the GOT slot to load immediate. */
-
-bfd_boolean
-_bfd_x86_64_elf_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_link_hash_table *htab;
- bfd_boolean changed;
- bfd_signed_vma *local_got_refcounts;
-
- /* 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;
-
- changed = FALSE;
- htab = elf_x86_hash_table (link_info, X86_64_ELF_DATA);
- local_got_refcounts = elf_local_got_refcounts (abfd);
-
- /* 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;
- struct elf_link_hash_entry *h;
- bfd_boolean converted;
-
- if (r_type != R_X86_64_GOTPCRELX
- && r_type != R_X86_64_REX_GOTPCRELX
- && r_type != R_X86_64_GOTPCREL)
- continue;
-
- r_symndx = htab->r_sym (irel->r_info);
- if (r_symndx < symtab_hdr->sh_info)
- h = _bfd_elf_x86_get_local_sym_hash (htab, sec->owner,
- (const Elf_Internal_Rela *) irel,
- FALSE);
- else
- {
- h = elf_sym_hashes (abfd)[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;
- }
-
- /* STT_GNU_IFUNC must keep GOTPCREL relocations. */
- if (h != NULL && h->type == STT_GNU_IFUNC)
- continue;
-
- converted = FALSE;
- if (!elf_x86_64_convert_load_reloc (abfd, contents, irel, h,
- &converted, link_info))
- goto error_return;
-
- if (converted)
- {
- changed = converted;
- 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 && !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)
- 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;
-}
-
/* Return the relocation value for @tpoff relocation
if STT_TLS virtual address is ADDRESS. */
@@ -2929,7 +2809,7 @@ do_ifunc_pointer:
if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
|| (bfd_link_pic (info)
- && SYMBOL_REFERENCES_LOCAL (info, h))
+ && SYMBOL_REFERENCES_LOCAL_P (info, h))
|| (ELF_ST_VISIBILITY (h->other)
&& h->root.type == bfd_link_hash_undefweak))
{
@@ -3064,7 +2944,7 @@ do_ifunc_pointer:
return FALSE;
}
else if (!bfd_link_executable (info)
- && !SYMBOL_REFERENCES_LOCAL (info, h)
+ && !SYMBOL_REFERENCES_LOCAL_P (info, h)
&& (h->type == STT_FUNC
|| h->type == STT_OBJECT)
&& ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
@@ -3212,7 +3092,7 @@ do_ifunc_pointer:
|| r_type == R_X86_64_PC32_BND)
&& is_32bit_relative_branch (contents, rel->r_offset));
- if (SYMBOL_REFERENCES_LOCAL (info, h))
+ if (SYMBOL_REFERENCES_LOCAL_P (info, h))
{
/* Symbol is referenced locally. Make sure it is
defined locally or for a branch. */
@@ -4379,7 +4259,7 @@ elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
in static executable. */
relgot = htab->elf.irelplt;
}
- if (SYMBOL_REFERENCES_LOCAL (info, h))
+ if (SYMBOL_REFERENCES_LOCAL_P (info, h))
{
info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
output_bfd,
@@ -4429,7 +4309,7 @@ elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
}
}
else if (bfd_link_pic (info)
- && SYMBOL_REFERENCES_LOCAL (info, h))
+ && SYMBOL_REFERENCES_LOCAL_P (info, h))
{
if (!(h->def_regular || ELF_COMMON_DEF_P (h)))
return FALSE;