diff options
author | H.J. Lu <hjl.tools@gmail.com> | 2016-04-20 11:10:21 -0700 |
---|---|---|
committer | H.J. Lu <hjl.tools@gmail.com> | 2016-04-20 11:10:38 -0700 |
commit | 338c190a92871c063847caef51bdc066372d4550 (patch) | |
tree | de16361f5e3786665f3e4d55473860224f784656 /bfd/elf64-x86-64.c | |
parent | 6c739336e5aadd6670909059fba889f50caa9cea (diff) | |
download | gdb-338c190a92871c063847caef51bdc066372d4550.zip gdb-338c190a92871c063847caef51bdc066372d4550.tar.gz gdb-338c190a92871c063847caef51bdc066372d4550.tar.bz2 |
Check run-time R_X86_64_32 relocation overflow
Since elf_x86_64_check_relocs is called after opening all input files,
we can detect dynamic R_X86_64_32 relocation overflow there.
bfd/
PR ld/19969
* elf64-x86-64.c (check_relocs_failed): New.
(elf_x86_64_need_pic): Moved before elf_x86_64_check_relocs.
Support relocation agaist local symbol. Set check_relocs_failed.
(elf_x86_64_check_relocs): Use elf_x86_64_need_pic. Check
R_X86_64_32 relocation overflow.
(elf_x86_64_relocate_section): Skip if check_relocs failed.
Update one elf_x86_64_need_pic and remove one elf_x86_64_need_pic.
ld/
PR ld/19969
* testsuite/ld-x86-64/pr19969.d: New file.
* testsuite/ld-x86-64/pr19969a.S: Likewise.
* testsuite/ld-x86-64/pr19969b.S: Likewise.
* testsuite/ld-x86-64/x86-64.exp: Run pr19969 tests.
Diffstat (limited to 'bfd/elf64-x86-64.c')
-rw-r--r-- | bfd/elf64-x86-64.c | 145 |
1 files changed, 71 insertions, 74 deletions
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c index 197a1b8..54fc1c8 100644 --- a/bfd/elf64-x86-64.c +++ b/bfd/elf64-x86-64.c @@ -1587,7 +1587,57 @@ 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 need_convert_load sec_flg0 +#define check_relocs_failed sec_flg1 + +static bfd_boolean +elf_x86_64_need_pic (bfd *input_bfd, asection *sec, + struct elf_link_hash_entry *h, + Elf_Internal_Shdr *symtab_hdr, + Elf_Internal_Sym *isym, + reloc_howto_type *howto) +{ + const char *v = ""; + const char *und = ""; + const char *pic = ""; + + const char *name; + if (h) + { + name = h->root.root.string; + switch (ELF_ST_VISIBILITY (h->other)) + { + case STV_HIDDEN: + v = _("hidden symbol "); + break; + case STV_INTERNAL: + v = _("internal symbol "); + break; + case STV_PROTECTED: + v = _("protected symbol "); + break; + default: + v = _("symbol "); + pic = _("; recompile with -fPIC"); + break; + } + + if (!h->def_regular && !h->def_dynamic) + und = _("undefined "); + } + else + { + name = bfd_elf_sym_name (input_bfd, symtab_hdr, isym, NULL); + pic = _("; recompile with -fPIC"); + } + + (*_bfd_error_handler) (_("%B: relocation %s against %s%s`%s' can " + "not be used when making a shared object%s"), + input_bfd, howto->name, und, v, name, pic); + bfd_set_error (bfd_error_bad_value); + sec->check_relocs_failed = 1; + return FALSE; +} /* Look through the relocs for a section during the first phase, and calculate needed space in the global offset table, procedure @@ -1802,19 +1852,8 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, case R_X86_64_TPOFF32: if (!bfd_link_executable (info) && ABI_64_P (abfd)) - { - if (h) - name = h->root.root.string; - else - name = bfd_elf_sym_name (abfd, symtab_hdr, isym, - NULL); - (*_bfd_error_handler) - (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"), - abfd, - x86_64_elf_howto_table[r_type].name, name); - bfd_set_error (bfd_error_bad_value); - return FALSE; - } + return elf_x86_64_need_pic (abfd, sec, h, symtab_hdr, isym, + &x86_64_elf_howto_table[r_type]); if (eh != NULL) eh->has_got_reloc = 1; break; @@ -1973,26 +2012,20 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, case R_X86_64_8: case R_X86_64_16: case R_X86_64_32S: - /* Let's help debug shared library creation. These relocs - cannot be used in shared libs. Don't error out for + /* Check relocation overflow as these relocs may lead to + run-time relocation overflow. Don't error out for sections we don't care about, such as debug sections or - non-constant sections, or when relocation overflow check - is disabled. */ + when relocation overflow check is disabled. */ if (!info->no_reloc_overflow_check - && bfd_link_pic (info) - && (sec->flags & SEC_ALLOC) != 0 - && (sec->flags & SEC_READONLY) != 0) - { - if (h) - name = h->root.root.string; - else - name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL); - (*_bfd_error_handler) - (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"), - abfd, x86_64_elf_howto_table[r_type].name, name); - bfd_set_error (bfd_error_bad_value); - return FALSE; - } + && (bfd_link_pic (info) + || (bfd_link_executable (info) + && h != NULL + && !h->def_regular + && h->def_dynamic + && (sec->flags & SEC_READONLY) == 0)) + && (sec->flags & SEC_ALLOC) != 0) + return elf_x86_64_need_pic (abfd, sec, h, symtab_hdr, isym, + &x86_64_elf_howto_table[r_type]); /* Fall through. */ case R_X86_64_PC8: @@ -4040,42 +4073,6 @@ is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset) && (contents [offset - 1] & 0xf0) == 0x80)); } -static bfd_boolean -elf_x86_64_need_pic (bfd *input_bfd, struct elf_link_hash_entry *h, - reloc_howto_type *howto) -{ - const char *fmt; - const char *v; - const char *pic = ""; - - switch (ELF_ST_VISIBILITY (h->other)) - { - case STV_HIDDEN: - v = _("hidden symbol"); - break; - case STV_INTERNAL: - v = _("internal symbol"); - break; - case STV_PROTECTED: - v = _("protected symbol"); - break; - default: - v = _("symbol"); - pic = _("; recompile with -fPIC"); - break; - } - - if (h->def_regular) - fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s"); - else - fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s"); - - (*_bfd_error_handler) (fmt, input_bfd, howto->name, - v, h->root.root.string, pic); - bfd_set_error (bfd_error_bad_value); - return FALSE; -} - /* Relocate an x86_64 ELF section. */ static bfd_boolean @@ -4100,6 +4097,10 @@ elf_x86_64_relocate_section (bfd *output_bfd, BFD_ASSERT (is_x86_64_elf (input_bfd)); + /* Skip if check_relocs failed. */ + if (input_section->check_relocs_failed) + return FALSE; + htab = elf_x86_64_hash_table (info); if (htab == NULL) return FALSE; @@ -4756,7 +4757,8 @@ elf_x86_64_relocate_section (bfd *output_bfd, } if (fail) - return elf_x86_64_need_pic (input_bfd, h, howto); + return elf_x86_64_need_pic (input_bfd, input_section, + h, NULL, NULL, howto); } /* Fall through. */ @@ -4838,11 +4840,6 @@ direct: || SYMBOLIC_BIND (info, h)) || ! h->def_regular)) { - if ((r_type != R_X86_64_PC64 && r_type != R_X86_64_64) - && bfd_link_executable (info) - && h->root.type == bfd_link_hash_undefweak - && !resolved_to_zero) - return elf_x86_64_need_pic (input_bfd, h, howto); outrel.r_info = htab->r_info (h->dynindx, r_type); outrel.r_addend = rel->r_addend; } |