Properly handle dynamic reloc against normal symbol

We shouldn't issue an error for read-only segment with dynamic IFUNC
relocations when dynamic relocations are against normal symbols.

bfd/

	PR ld/19939
	* elf-bfd.h (_bfd_elf_allocate_ifunc_dyn_relocs): Add a pointer
	to bfd_boolean.
	* elf-ifunc.c (_bfd_elf_allocate_ifunc_dyn_relocs): Updated.
	Set *readonly_dynrelocs_against_ifunc_p to TRUE if dynamic reloc
	applies to read-only section.
	* elf32-i386.c (elf_i386_link_hash_table): Add
	readonly_dynrelocs_against_ifunc.
	(elf_i386_allocate_dynrelocs): Updated.
	(elf_i386_size_dynamic_sections): Issue an error for read-only
	segment with dynamic IFUNC relocations only if
	readonly_dynrelocs_against_ifunc is TRUE.
	* elf64-x86-64.c (elf_x86_64_link_hash_table): Add
	readonly_dynrelocs_against_ifunc.
	(elf_x86_64_allocate_dynrelocs): Updated.
	(elf_x86_64_size_dynamic_sections): Issue an error for read-only
	segment with dynamic IFUNC relocations only if
	readonly_dynrelocs_against_ifunc is TRUE.
	* elfnn-aarch64.c (elfNN_aarch64_allocate_ifunc_dynrelocs):
	Updated.

ld/

	PR ld/19939
	* testsuite/ld-i386/i386.exp: Run PR ld/19939 tests.
	* testsuite/ld-x86-64/x86-64.exp: Likewise.
	* testsuite/ld-i386/pr19939.s: New file.
	* testsuite/ld-i386/pr19939a.d: Likewise.
	* testsuite/ld-i386/pr19939b.d: Likewise.
	* testsuite/ld-x86-64/pr19939.s: Likewise.
	* testsuite/ld-x86-64/pr19939a.d: Likewise.
	* testsuite/ld-x86-64/pr19939b.d: Likewise.
This commit is contained in:
H.J. Lu 2016-04-11 19:41:37 -07:00
parent b545ef977b
commit 2df3368d85
15 changed files with 110 additions and 6 deletions

View file

@ -1,3 +1,26 @@
2016-04-11 H.J. Lu <hongjiu.lu@intel.com>
PR ld/19939
* elf-bfd.h (_bfd_elf_allocate_ifunc_dyn_relocs): Add a pointer
to bfd_boolean.
* elf-ifunc.c (_bfd_elf_allocate_ifunc_dyn_relocs): Updated.
Set *readonly_dynrelocs_against_ifunc_p to TRUE if dynamic reloc
applies to read-only section.
* elf32-i386.c (elf_i386_link_hash_table): Add
readonly_dynrelocs_against_ifunc.
(elf_i386_allocate_dynrelocs): Updated.
(elf_i386_size_dynamic_sections): Issue an error for read-only
segment with dynamic IFUNC relocations only if
readonly_dynrelocs_against_ifunc is TRUE.
* elf64-x86-64.c (elf_x86_64_link_hash_table): Add
readonly_dynrelocs_against_ifunc.
(elf_x86_64_allocate_dynrelocs): Updated.
(elf_x86_64_size_dynamic_sections): Issue an error for read-only
segment with dynamic IFUNC relocations only if
readonly_dynrelocs_against_ifunc is TRUE.
* elfnn-aarch64.c (elfNN_aarch64_allocate_ifunc_dynrelocs):
Updated.
2016-04-06 Thomas Preud'homme <thomas.preudhomme@arm.com>
* elf32-arm.c (elf32_arm_size_stubs): Move error_ret_free_local to be

View file

@ -2490,7 +2490,8 @@ extern bfd_boolean _bfd_elf_create_ifunc_sections
(bfd *, struct bfd_link_info *);
extern bfd_boolean _bfd_elf_allocate_ifunc_dyn_relocs
(struct bfd_link_info *, struct elf_link_hash_entry *,
struct elf_dyn_relocs **, unsigned int, unsigned int, unsigned int);
struct elf_dyn_relocs **, bfd_boolean *, unsigned int,
unsigned int, unsigned int);
extern long _bfd_elf_ifunc_get_synthetic_symtab
(bfd *, long, asymbol **, long, asymbol **, asymbol **, asection *,
bfd_vma *(*) (bfd *, asymbol **, asection *, asection *));

View file

@ -110,6 +110,7 @@ bfd_boolean
_bfd_elf_allocate_ifunc_dyn_relocs (struct bfd_link_info *info,
struct elf_link_hash_entry *h,
struct elf_dyn_relocs **head,
bfd_boolean *readonly_dynrelocs_against_ifunc_p,
unsigned int plt_entry_size,
unsigned int plt_header_size,
unsigned int got_entry_size)
@ -119,6 +120,7 @@ _bfd_elf_allocate_ifunc_dyn_relocs (struct bfd_link_info *info,
unsigned int sizeof_reloc;
const struct elf_backend_data *bed;
struct elf_link_hash_table *htab;
bfd_boolean readonly_dynrelocs_against_ifunc;
/* When a shared library references a STT_GNU_IFUNC symbol defined
in executable, the address of the resolved function may be used.
@ -224,6 +226,8 @@ keep:
|| !h->non_got_ref)
*head = NULL;
readonly_dynrelocs_against_ifunc = FALSE;
/* Finally, allocate space. */
p = *head;
if (p != NULL)
@ -231,6 +235,12 @@ keep:
bfd_size_type count = 0;
do
{
if (!readonly_dynrelocs_against_ifunc)
{
asection *s = p->sec->output_section;
if (s != NULL && (s->flags & SEC_READONLY) != 0)
readonly_dynrelocs_against_ifunc = TRUE;
}
count += p->count;
p = p->next;
}
@ -238,6 +248,9 @@ keep:
htab->irelifunc->size += count * sizeof_reloc;
}
if (readonly_dynrelocs_against_ifunc_p)
*readonly_dynrelocs_against_ifunc_p = readonly_dynrelocs_against_ifunc;
/* For STT_GNU_IFUNC symbol, .got.plt has the real function address
and .got has the PLT entry adddress. We will load the GOT entry
with the PLT entry in finish_dynamic_symbol if it is used. For

View file

@ -878,6 +878,10 @@ struct elf_i386_link_hash_table
/* The index of the next unused R_386_IRELATIVE slot in .rel.plt. */
bfd_vma next_irelative_index;
/* TRUE if there are dynamic relocs against IFUNC symbols that apply
to read-only sections. */
bfd_boolean readonly_dynrelocs_against_ifunc;
};
/* Get the i386 ELF linker hash table from a link_info structure. */
@ -2442,7 +2446,8 @@ elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
if (h->type == STT_GNU_IFUNC
&& h->def_regular)
return _bfd_elf_allocate_ifunc_dyn_relocs (info, h, &eh->dyn_relocs,
plt_entry_size,
&htab->readonly_dynrelocs_against_ifunc,
plt_entry_size,
plt_entry_size, 4);
/* Don't create the PLT entry if there are only function pointer
relocations which can be resolved at run-time. */
@ -3553,8 +3558,7 @@ elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
if ((info->flags & DF_TEXTREL) != 0)
{
if ((elf_tdata (output_bfd)->has_gnu_symbols
& elf_gnu_symbol_ifunc) == elf_gnu_symbol_ifunc)
if (htab->readonly_dynrelocs_against_ifunc)
{
info->callbacks->einfo
(_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));

View file

@ -900,6 +900,10 @@ struct elf_x86_64_link_hash_table
bfd_vma next_jump_slot_index;
/* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
bfd_vma next_irelative_index;
/* TRUE if there are dynamic relocs against IFUNC symbols that apply
to read-only sections. */
bfd_boolean readonly_dynrelocs_against_ifunc;
};
/* Get the x86-64 ELF linker hash table from a link_info structure. */
@ -2659,6 +2663,7 @@ elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
{
if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h,
&eh->dyn_relocs,
&htab->readonly_dynrelocs_against_ifunc,
plt_entry_size,
plt_entry_size,
GOT_ENTRY_SIZE))
@ -3899,8 +3904,7 @@ elf_x86_64_size_dynamic_sections (bfd *output_bfd,
if ((info->flags & DF_TEXTREL) != 0)
{
if ((elf_tdata (output_bfd)->has_gnu_symbols
& elf_gnu_symbol_ifunc) == elf_gnu_symbol_ifunc)
if (htab->readonly_dynrelocs_against_ifunc)
{
info->callbacks->einfo
(_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));

View file

@ -8249,6 +8249,7 @@ elfNN_aarch64_allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h,
&& h->def_regular)
return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
&eh->dyn_relocs,
NULL,
htab->plt_entry_size,
htab->plt_header_size,
GOT_ENTRY_SIZE);

View file

@ -1,3 +1,15 @@
2016-04-11 H.J. Lu <hongjiu.lu@intel.com>
PR ld/19939
* testsuite/ld-i386/i386.exp: Run PR ld/19939 tests.
* testsuite/ld-x86-64/x86-64.exp: Likewise.
* testsuite/ld-i386/pr19939.s: New file.
* testsuite/ld-i386/pr19939a.d: Likewise.
* testsuite/ld-i386/pr19939b.d: Likewise.
* testsuite/ld-x86-64/pr19939.s: Likewise.
* testsuite/ld-x86-64/pr19939a.d: Likewise.
* testsuite/ld-x86-64/pr19939b.d: Likewise.
2016-04-09 Oleg Endo <olegendo@gcc.gnu.org>
* emulparams/shelf.sh: Set stack area to 0x3FFFFF00.

View file

@ -390,6 +390,8 @@ run_dump_test "pr17935-1"
run_dump_test "pr17935-2"
run_dump_test "pr18801"
run_dump_test "pr18815"
run_dump_test "pr19939a"
run_dump_test "pr19939b"
proc undefined_weak {cflags ldflags} {
set testname "Undefined weak symbol"

View file

@ -0,0 +1,9 @@
.text
selector:
movl foo@GOT(%eax), %eax
mov $bar, %ebx
ret
.type selector, %gnu_indirect_function
.globl bar
bar:
jmp selector@PLT

View file

@ -0,0 +1,4 @@
#source: pr19939.s
#as: --32
#ld: -melf_i386 -shared -z defs
#error: undefined reference to `foo'

View file

@ -0,0 +1,8 @@
#source: pr19939.s
#as: --32
#ld: -melf_i386 -shared
#readelf: -d --wide
#...
.*\(TEXTREL\).*
#pass

View file

@ -0,0 +1,9 @@
.text
selector:
movq foo@GOTPCREL(%rip), %rax
movabs $bar, %rbx
ret
.type selector, %gnu_indirect_function
.globl bar
bar:
jmp selector@PLT

View file

@ -0,0 +1,4 @@
#source: pr19939.s
#as: --64
#ld: -melf_x86_64 -shared -z defs
#error: undefined reference to `foo'

View file

@ -0,0 +1,8 @@
#source: pr19939.s
#as: --64
#ld: -melf_x86_64 -shared
#readelf: -d --wide
#...
.*\(TEXTREL\).*
#pass

View file

@ -438,6 +438,8 @@ run_dump_test "pr19609-7a"
run_dump_test "pr19609-7b"
run_dump_test "pr19609-7c"
run_dump_test "pr19609-7d"
run_dump_test "pr19939a"
run_dump_test "pr19939b"
proc undefined_weak {cflags ldflags} {
set testname "Undefined weak symbol"