public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [AArch64] Generate DT_TEXTREL when there is relocation against read-only section
@ 2015-06-19  9:06 Jiong Wang
  2015-06-23 11:03 ` Marcus Shawcroft
  0 siblings, 1 reply; 3+ messages in thread
From: Jiong Wang @ 2015-06-19  9:06 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 808 bytes --]


This patch fix the SEGSEGV noticed on some scenarios when there is
relocation against read-only segment in the program.

We need to generate DT_TEXTREL entry in .dynamic section to
notify dynamic linker that there are relocations against read-only
segment, that dynamic linker can invoke mprotect to change the segment
attribute into writable, and after relocation finished, change it back
to read-only.

OK for trunk?

bfd/
  * elfnn-aarch64.c (aarch64_readonly_dynrelocs): New function.
  (elfNN_aarch64_size_dynamic_sections): Traverse hash table to check
  relocations against read-only sections.

ld/testsuite/
  * ld-aarch64/dt_textrel.s: New testcase.
  * ld-aarch64/dt_textrel.d: New expectation file.
  * ld-aarch64/aarch64-elf.exp: Run new testcase.
  
-- 
Regards,
Jiong

[-- Attachment #2: fix_dt_rel.patch --]
[-- Type: text/x-diff, Size: 2656 bytes --]

diff --git a/bfd/elfnn-aarch64.c b/bfd/elfnn-aarch64.c
index 9d8d70a..270e436 100644
--- a/bfd/elfnn-aarch64.c
+++ b/bfd/elfnn-aarch64.c
@@ -7909,6 +7909,32 @@ elfNN_aarch64_allocate_local_ifunc_dynrelocs (void **slot, void *inf)
   return elfNN_aarch64_allocate_ifunc_dynrelocs (h, inf);
 }
 
+/* Find any dynamic relocs that apply to read-only sections.  */
+
+static bfd_boolean
+aarch64_readonly_dynrelocs (struct elf_link_hash_entry * h, void * inf)
+{
+  struct elf_aarch64_link_hash_entry * eh;
+  struct elf_dyn_relocs * p;
+
+  eh = (struct elf_aarch64_link_hash_entry *) h;
+  for (p = eh->dyn_relocs; p != NULL; p = p->next)
+    {
+      asection *s = p->sec;
+
+      if (s != NULL && (s->flags & SEC_READONLY) != 0)
+	{
+	  struct bfd_link_info *info = (struct bfd_link_info *) inf;
+
+	  info->flags |= DF_TEXTREL;
+
+	  /* Not an error, just cut short the traversal.  */
+	  return FALSE;
+	}
+    }
+  return TRUE;
+}
+
 /* This is the most important function of all . Innocuosly named
    though !  */
 static bfd_boolean
@@ -8196,6 +8222,10 @@ elfNN_aarch64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 
 	  /* If any dynamic relocs apply to a read-only section,
 	     then we need a DT_TEXTREL entry.  */
+	  if ((info->flags & DF_TEXTREL) == 0)
+	    elf_link_hash_traverse (& htab->root, aarch64_readonly_dynrelocs,
+				    info);
+
 	  if ((info->flags & DF_TEXTREL) != 0)
 	    {
 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
diff --git a/ld/testsuite/ld-aarch64/aarch64-elf.exp b/ld/testsuite/ld-aarch64/aarch64-elf.exp
index 1eb5c92..81f69ab 100644
--- a/ld/testsuite/ld-aarch64/aarch64-elf.exp
+++ b/ld/testsuite/ld-aarch64/aarch64-elf.exp
@@ -227,6 +227,8 @@ run_dump_test "ifunc-22"
 run_dump_test "relasz"
 run_dump_test "relocs-257-symbolic-func"
 
+run_dump_test "dt_textrel"
+
 set aarch64elflinktests {
   {"ld-aarch64/so with global symbol" "-shared" "" "" {copy-reloc-so.s}
   {} "copy-reloc-so.so"}
diff --git a/ld/testsuite/ld-aarch64/dt_textrel.d b/ld/testsuite/ld-aarch64/dt_textrel.d
new file mode 100644
index 0000000..2dbbd2f
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/dt_textrel.d
@@ -0,0 +1,7 @@
+#source: dt_textrel.s
+#ld: -shared
+#readelf: -d
+#...
+.*TEXTREL.*
+.*
+
diff --git a/ld/testsuite/ld-aarch64/dt_textrel.s b/ld/testsuite/ld-aarch64/dt_textrel.s
new file mode 100644
index 0000000..f37f456
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/dt_textrel.s
@@ -0,0 +1,9 @@
+	.cpu generic+fp+simd
+	.global	p
+	.comm	x,4,4
+	.section	.rodata
+	.align	3
+	.type	p, %object
+	.size	p, 8
+p:
+	.xword	x

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [AArch64] Generate DT_TEXTREL when there is relocation against read-only section
  2015-06-19  9:06 [AArch64] Generate DT_TEXTREL when there is relocation against read-only section Jiong Wang
@ 2015-06-23 11:03 ` Marcus Shawcroft
  2015-06-23 11:29   ` Jiong Wang
  0 siblings, 1 reply; 3+ messages in thread
From: Marcus Shawcroft @ 2015-06-23 11:03 UTC (permalink / raw)
  To: Jiong Wang; +Cc: binutils

On 19 June 2015 at 10:05, Jiong Wang <jiong.wang@arm.com> wrote:
>
> This patch fix the SEGSEGV noticed on some scenarios when there is
> relocation against read-only segment in the program.
>
> We need to generate DT_TEXTREL entry in .dynamic section to
> notify dynamic linker that there are relocations against read-only
> segment, that dynamic linker can invoke mprotect to change the segment
> attribute into writable, and after relocation finished, change it back
> to read-only.
>
> OK for trunk?

OK, can you back port to 2.25 aswell please? /Marcus

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [AArch64] Generate DT_TEXTREL when there is relocation against read-only section
  2015-06-23 11:03 ` Marcus Shawcroft
@ 2015-06-23 11:29   ` Jiong Wang
  0 siblings, 0 replies; 3+ messages in thread
From: Jiong Wang @ 2015-06-23 11:29 UTC (permalink / raw)
  To: Marcus Shawcroft; +Cc: binutils


Marcus Shawcroft writes:

> On 19 June 2015 at 10:05, Jiong Wang <jiong.wang@arm.com> wrote:
>>
>> This patch fix the SEGSEGV noticed on some scenarios when there is
>> relocation against read-only segment in the program.
>>
>> We need to generate DT_TEXTREL entry in .dynamic section to
>> notify dynamic linker that there are relocations against read-only
>> segment, that dynamic linker can invoke mprotect to change the segment
>> attribute into writable, and after relocation finished, change it back
>> to read-only.
>>
>> OK for trunk?
>
> OK, can you back port to 2.25 aswell please? /Marcus

Thanks. This atch applies cleanly on 2.25 branch, pushed to both.

trunk:  https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commit;h=c217058957a45a93481da35e1531ed120750d739
2.25 :  https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commit;h=372832d02571c8a6e470cc601bd904c18f7bc374

--
Regards,
Jiong

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-06-23 11:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-19  9:06 [AArch64] Generate DT_TEXTREL when there is relocation against read-only section Jiong Wang
2015-06-23 11:03 ` Marcus Shawcroft
2015-06-23 11:29   ` Jiong Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).