public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [gold commit] Place copy relocations in .dynbss when target is read-only
@ 2016-12-28  4:45 Cary Coutant
  2016-12-28  4:58 ` Cary Coutant
  2016-12-28  8:28 ` Andreas Schwab
  0 siblings, 2 replies; 6+ messages in thread
From: Cary Coutant @ 2016-12-28  4:45 UTC (permalink / raw)
  To: Binutils

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

Piggy-backing on PR 20995...

2016-12-27  Cary Coutant  <ccoutant@gmail.com>

gold/
        PR ld/20995
        * copy-relocs.cc (Copy_relocs::make_copy_reloc): Use .dynbss for
        read-only data when linking with -z relro.
        * copy-relocs.h (Copy_relocs::dynrelro_): New data member.
        * testsuite/Makefile.am (copy_test_relro): New test case.
        * testsuite/Makefile.in: Regenerate.
        * testsuite/copy_test_relro.cc: New source file.
        * testsuite/copy_test_relro_1.cc: New source file.

[-- Attachment #2: dynrelro.patch --]
[-- Type: application/octet-stream, Size: 7732 bytes --]

Place copy relocations in .dynbss when target is read-only.

2016-12-27  Cary Coutant  <ccoutant@gmail.com>

gold/
	PR ld/20995
	* copy-relocs.cc (Copy_relocs::make_copy_reloc): Use .dynbss for
	read-only data when linking with -z relro.
	* copy-relocs.h (Copy_relocs::dynrelro_): New data member.
	* testsuite/Makefile.am (copy_test_relro): New test case.
	* testsuite/Makefile.in: Regenerate.
	* testsuite/copy_test_relro.cc: New source file.
	* testsuite/copy_test_relro_1.cc: New source file.


diff --git a/gold/copy-relocs.cc b/gold/copy-relocs.cc
index ce019c4..4fe17e9 100644
--- a/gold/copy-relocs.cc
+++ b/gold/copy-relocs.cc
@@ -141,6 +141,7 @@ Copy_relocs<sh_type, size, big_endian>::make_copy_reloc(
   unsigned int shndx = sym->shndx(&is_ordinary);
   gold_assert(is_ordinary);
   typename elfcpp::Elf_types<size>::Elf_WXword addralign;
+  bool is_readonly = false;
 
   {
     // Lock the object so we can read from it.  This is only called
@@ -150,6 +151,17 @@ Copy_relocs<sh_type, size, big_endian>::make_copy_reloc(
     Object* obj = sym->object();
     Task_lock_obj<Object> tl(dummy_task, obj);
     addralign = obj->section_addralign(shndx);
+    if (parameters->options().relro())
+      {
+	if ((obj->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
+	  is_readonly = true;
+	else
+	  {
+	    // Symbols in .data.rel.ro should also be treated as read-only.
+	    if (obj->section_name(shndx) == ".data.rel.ro")
+	      is_readonly = true;
+	  }
+      }
   }
 
   typename Sized_symbol<size>::Value_type value = sym->value();
@@ -159,16 +171,32 @@ Copy_relocs<sh_type, size, big_endian>::make_copy_reloc(
   // Mark the dynamic object as needed for the --as-needed option.
   sym->object()->set_is_needed();
 
-  if (this->dynbss_ == NULL)
+  Output_data_space* dynbss;
+
+  if (is_readonly)
     {
-      this->dynbss_ = new Output_data_space(addralign, "** dynbss");
-      layout->add_output_section_data(".bss",
-				      elfcpp::SHT_NOBITS,
-				      elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
-				      this->dynbss_, ORDER_BSS, false);
+      if (this->dynrelro_ == NULL)
+	{
+	  this->dynrelro_ = new Output_data_space(addralign, "** dynrelro");
+	  layout->add_output_section_data(".data.rel.ro",
+					  elfcpp::SHT_PROGBITS,
+					  elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
+					  this->dynrelro_, ORDER_RELRO, false);
+	}
+      dynbss = this->dynrelro_;
+    }
+  else
+    {
+      if (this->dynbss_ == NULL)
+	{
+	  this->dynbss_ = new Output_data_space(addralign, "** dynbss");
+	  layout->add_output_section_data(".bss",
+					  elfcpp::SHT_NOBITS,
+					  elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
+					  this->dynbss_, ORDER_BSS, false);
+	}
+      dynbss = this->dynbss_;
     }
-
-  Output_data_space* dynbss = this->dynbss_;
 
   if (addralign > dynbss->addralign())
     dynbss->set_space_alignment(addralign);
diff --git a/gold/copy-relocs.h b/gold/copy-relocs.h
index aa42c7c..92b2ceb 100644
--- a/gold/copy-relocs.h
+++ b/gold/copy-relocs.h
@@ -54,7 +54,8 @@ class Copy_relocs
 
  public:
   Copy_relocs(unsigned int copy_reloc_type)
-    : entries_(), copy_reloc_type_(copy_reloc_type), dynbss_(NULL)
+    : entries_(), copy_reloc_type_(copy_reloc_type), dynbss_(NULL),
+      dynrelro_(NULL)
   { }
 
   // This is called while scanning relocs if we see a relocation
@@ -152,8 +153,11 @@ class Copy_relocs
   // The target specific relocation type of the COPY relocation.
   const unsigned int copy_reloc_type_;
   // The dynamic BSS data which goes into the .bss section.  This is
-  // where variables which require COPY relocations are placed.
+  // where writable variables which require COPY relocations are placed.
   Output_data_space* dynbss_;
+  // The dynamic read-only data, which goes into the .dynbss section.  This
+  // is where read-only variables which require COPY relocations are placed.
+  Output_data_space* dynrelro_;
 };
 
 } // End namespace gold.
diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am
index 6a882ea..d9480ab 100644
--- a/gold/testsuite/Makefile.am
+++ b/gold/testsuite/Makefile.am
@@ -858,6 +858,16 @@ copy_test_2_pic.o: copy_test_2.cc
 copy_test_2.so: gcctestdir/ld copy_test_2_pic.o
 	$(CXXLINK) -Bgcctestdir/ -shared copy_test_2_pic.o
 
+check_PROGRAMS += copy_test_relro
+copy_test_relro_SOURCES = copy_test_relro.cc
+copy_test_relro_DEPENDENCIES = gcctestdir/ld copy_test_relro_1.so
+copy_test_relro_LDFLAGS = -Bgcctestdir/ -Wl,-R,. -Wl,-z,relro
+copy_test_relro_LDADD = copy_test_relro_1.so
+copy_test_relro_1_pic.o: copy_test_relro_1.cc
+	$(CXXCOMPILE) -c -fpic -o $@ $<
+copy_test_relro_1.so: gcctestdir/ld copy_test_relro_1_pic.o
+	$(CXXLINK) -Bgcctestdir/ -shared -Wl,-z,relro copy_test_relro_1_pic.o
+
 if !DEFAULT_TARGET_POWERPC
 check_SCRIPTS += copy_test_protected.sh
 check_DATA += copy_test_protected.err
diff --git a/gold/testsuite/copy_test_relro.cc b/gold/testsuite/copy_test_relro.cc
new file mode 100644
index 0000000..297b578
--- /dev/null
+++ b/gold/testsuite/copy_test_relro.cc
@@ -0,0 +1,45 @@
+// copy_test_relro.cc -- test copy relocs against read-only and relro symbols.
+
+// Copyright (C) 2016 Free Software Foundation, Inc.
+// Written by Cary Coutant <ccoutant@gmail.com>.
+
+// This file is part of gold.
+
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+// MA 02110-1301, USA.
+
+#include <cassert>
+#include <stdint.h>
+
+extern char* _etext;
+extern char* __data_start;
+extern char* _edata;
+extern char* _end;
+
+extern int* const p;
+extern const int b[];
+int a = 123;
+
+int main()
+{
+  assert(*p == 123);
+  assert(b[0] == 100);
+  assert(b[1] == 200);
+  assert(b[2] == 300);
+  assert(b[3] == 400);
+  assert(reinterpret_cast<const void*>(&p) < reinterpret_cast<void*>(&__data_start));
+  assert(reinterpret_cast<const void*>(b) < reinterpret_cast<void*>(&__data_start));
+  return 0;
+}
diff --git a/gold/testsuite/copy_test_relro_1.cc b/gold/testsuite/copy_test_relro_1.cc
new file mode 100644
index 0000000..d154534
--- /dev/null
+++ b/gold/testsuite/copy_test_relro_1.cc
@@ -0,0 +1,26 @@
+// copy_test_relro_1.cc -- test copy relocs variables for gold
+
+// Copyright (C) 2016 Free Software Foundation, Inc.
+// Written by Cary Coutant <ccoutant@gmail.com>.
+
+// This file is part of gold.
+
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+// MA 02110-1301, USA.
+
+extern int a;
+extern int* const p = &a;
+
+extern const int b[] = { 100, 200, 300, 400 };

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

* Re: [gold commit] Place copy relocations in .dynbss when target is read-only
  2016-12-28  4:45 [gold commit] Place copy relocations in .dynbss when target is read-only Cary Coutant
@ 2016-12-28  4:58 ` Cary Coutant
  2016-12-28  8:28 ` Andreas Schwab
  1 sibling, 0 replies; 6+ messages in thread
From: Cary Coutant @ 2016-12-28  4:58 UTC (permalink / raw)
  To: Binutils

> 2016-12-27  Cary Coutant  <ccoutant@gmail.com>
>
> gold/
>         PR ld/20995
>         * copy-relocs.cc (Copy_relocs::make_copy_reloc): Use .dynbss for
>         read-only data when linking with -z relro.
>         * copy-relocs.h (Copy_relocs::dynrelro_): New data member.
>         * testsuite/Makefile.am (copy_test_relro): New test case.
>         * testsuite/Makefile.in: Regenerate.
>         * testsuite/copy_test_relro.cc: New source file.
>         * testsuite/copy_test_relro_1.cc: New source file.

Also backported to binutils-2_28-branch.

-cary

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

* Re: [gold commit] Place copy relocations in .dynbss when target is read-only
  2016-12-28  4:45 [gold commit] Place copy relocations in .dynbss when target is read-only Cary Coutant
  2016-12-28  4:58 ` Cary Coutant
@ 2016-12-28  8:28 ` Andreas Schwab
  2016-12-28 16:40   ` Cary Coutant
  1 sibling, 1 reply; 6+ messages in thread
From: Andreas Schwab @ 2016-12-28  8:28 UTC (permalink / raw)
  To: Cary Coutant; +Cc: Binutils

On Dez 27 2016, Cary Coutant <ccoutant@gmail.com> wrote:

> Piggy-backing on PR 20995...
>
> 2016-12-27  Cary Coutant  <ccoutant@gmail.com>
>
> gold/
>         PR ld/20995
>         * copy-relocs.cc (Copy_relocs::make_copy_reloc): Use .dynbss for

s/dynbss/dynrelro/

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: [gold commit] Place copy relocations in .dynbss when target is read-only
  2016-12-28  8:28 ` Andreas Schwab
@ 2016-12-28 16:40   ` Cary Coutant
  2016-12-28 17:35     ` Andreas Schwab
  0 siblings, 1 reply; 6+ messages in thread
From: Cary Coutant @ 2016-12-28 16:40 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Binutils

>> 2016-12-27  Cary Coutant  <ccoutant@gmail.com>
>>
>> gold/
>>         PR ld/20995
>>         * copy-relocs.cc (Copy_relocs::make_copy_reloc): Use .dynbss for
>
> s/dynbss/dynrelro/

Fixed, thanks!

-cary

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

* Re: [gold commit] Place copy relocations in .dynbss when target is read-only
  2016-12-28 16:40   ` Cary Coutant
@ 2016-12-28 17:35     ` Andreas Schwab
  2016-12-28 19:23       ` Cary Coutant
  0 siblings, 1 reply; 6+ messages in thread
From: Andreas Schwab @ 2016-12-28 17:35 UTC (permalink / raw)
  To: Cary Coutant; +Cc: Binutils

On Dez 28 2016, Cary Coutant <ccoutant@gmail.com> wrote:

>>> 2016-12-27  Cary Coutant  <ccoutant@gmail.com>
>>>
>>> gold/
>>>         PR ld/20995
>>>         * copy-relocs.cc (Copy_relocs::make_copy_reloc): Use .dynbss for
>>
>> s/dynbss/dynrelro/
>
> Fixed, thanks!

There was a reference in a comment left.

Andreas.

diff --git a/gold/copy-relocs.h b/gold/copy-relocs.h
index 92b2ceb1e8..f5cd0a5529 100644
--- a/gold/copy-relocs.h
+++ b/gold/copy-relocs.h
@@ -155,8 +155,9 @@ class Copy_relocs
   // The dynamic BSS data which goes into the .bss section.  This is
   // where writable variables which require COPY relocations are placed.
   Output_data_space* dynbss_;
-  // The dynamic read-only data, which goes into the .dynbss section.  This
-  // is where read-only variables which require COPY relocations are placed.
+  // The dynamic read-only data, which goes into the .data.rel.ro section.
+  // This is where read-only variables which require COPY relocations are
+  // placed.
   Output_data_space* dynrelro_;
 };
 
-- 
2.11.0


-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: [gold commit] Place copy relocations in .dynbss when target is read-only
  2016-12-28 17:35     ` Andreas Schwab
@ 2016-12-28 19:23       ` Cary Coutant
  0 siblings, 0 replies; 6+ messages in thread
From: Cary Coutant @ 2016-12-28 19:23 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Binutils

> There was a reference in a comment left.

Oops, thanks!

-cary

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

end of thread, other threads:[~2016-12-28 19:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-28  4:45 [gold commit] Place copy relocations in .dynbss when target is read-only Cary Coutant
2016-12-28  4:58 ` Cary Coutant
2016-12-28  8:28 ` Andreas Schwab
2016-12-28 16:40   ` Cary Coutant
2016-12-28 17:35     ` Andreas Schwab
2016-12-28 19:23       ` Cary Coutant

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).