+2016-12-27 Cary Coutant <ccoutant@gmail.com>
+
+ 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.
+
2016-12-23 Cary Coutant <ccoutant@gmail.com>
* NEWS: Add new features in 1.14.
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
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();
// 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);
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
// 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.
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
@FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_16 = weak_undef_nonpic_test
@FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_17 = alt/weak_undef_lib_nonpic.so
@GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_18 = weak_alias_test weak_plt \
-@GCC_TRUE@@NATIVE_LINKER_TRUE@ copy_test
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ copy_test copy_test_relro
@DEFAULT_TARGET_POWERPC_FALSE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_19 = copy_test_protected.sh
@DEFAULT_TARGET_POWERPC_FALSE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_20 = copy_test_protected.err
@DEFAULT_TARGET_POWERPC_FALSE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_21 = copy_test_protected.err
@GCC_TRUE@@NATIVE_LINKER_TRUE@am__EXEEXT_14 = \
@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_alias_test$(EXEEXT) \
@GCC_TRUE@@NATIVE_LINKER_TRUE@ weak_plt$(EXEEXT) \
-@GCC_TRUE@@NATIVE_LINKER_TRUE@ copy_test$(EXEEXT)
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ copy_test$(EXEEXT) \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ copy_test_relro$(EXEEXT)
@GCC_TRUE@@NATIVE_LINKER_TRUE@@TLS_TRUE@am__EXEEXT_15 = \
@GCC_TRUE@@NATIVE_LINKER_TRUE@@TLS_TRUE@ tls_test$(EXEEXT) \
@GCC_TRUE@@NATIVE_LINKER_TRUE@@TLS_TRUE@ tls_pic_test$(EXEEXT) \
copy_test_OBJECTS = $(am_copy_test_OBJECTS)
copy_test_LINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) \
$(copy_test_LDFLAGS) $(LDFLAGS) -o $@
+@GCC_TRUE@@NATIVE_LINKER_TRUE@am_copy_test_relro_OBJECTS = \
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ copy_test_relro.$(OBJEXT)
+copy_test_relro_OBJECTS = $(am_copy_test_relro_OBJECTS)
+copy_test_relro_LINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) \
+ $(copy_test_relro_LDFLAGS) $(LDFLAGS) -o $@
@GCC_TRUE@@NATIVE_LINKER_TRUE@am_discard_locals_test_OBJECTS = \
@GCC_TRUE@@NATIVE_LINKER_TRUE@ discard_locals_test.$(OBJEXT)
discard_locals_test_OBJECTS = $(am_discard_locals_test_OBJECTS)
$(binary_test_SOURCES) $(binary_unittest_SOURCES) \
$(common_test_1_SOURCES) $(common_test_2_SOURCES) \
$(constructor_static_test_SOURCES) $(constructor_test_SOURCES) \
- $(copy_test_SOURCES) $(discard_locals_test_SOURCES) \
- $(dynamic_list_2_SOURCES) eh_test.c \
- $(ehdr_start_test_1_SOURCES) $(ehdr_start_test_2_SOURCES) \
- $(ehdr_start_test_3_SOURCES) $(ehdr_start_test_5_SOURCES) \
+ $(copy_test_SOURCES) $(copy_test_relro_SOURCES) \
+ $(discard_locals_test_SOURCES) $(dynamic_list_2_SOURCES) \
+ eh_test.c $(ehdr_start_test_1_SOURCES) \
+ $(ehdr_start_test_2_SOURCES) $(ehdr_start_test_3_SOURCES) \
+ $(ehdr_start_test_5_SOURCES) \
$(exception_same_shared_test_SOURCES) \
$(exception_separate_shared_12_test_SOURCES) \
$(exception_separate_shared_21_test_SOURCES) \
@GCC_TRUE@@NATIVE_LINKER_TRUE@copy_test_DEPENDENCIES = gcctestdir/ld copy_test_1.so copy_test_2.so
@GCC_TRUE@@NATIVE_LINKER_TRUE@copy_test_LDFLAGS = -Bgcctestdir/ -Wl,-R,.
@GCC_TRUE@@NATIVE_LINKER_TRUE@copy_test_LDADD = copy_test_1.so copy_test_2.so
+@GCC_TRUE@@NATIVE_LINKER_TRUE@copy_test_relro_SOURCES = copy_test_relro.cc
+@GCC_TRUE@@NATIVE_LINKER_TRUE@copy_test_relro_DEPENDENCIES = gcctestdir/ld copy_test_relro_1.so
+@GCC_TRUE@@NATIVE_LINKER_TRUE@copy_test_relro_LDFLAGS = -Bgcctestdir/ -Wl,-R,. -Wl,-z,relro
+@GCC_TRUE@@NATIVE_LINKER_TRUE@copy_test_relro_LDADD = copy_test_relro_1.so
@GCC_TRUE@@NATIVE_LINKER_TRUE@@TLS_TRUE@tls_test_SOURCES = tls_test.cc tls_test_file2.cc tls_test_main.cc tls_test.h
@GCC_TRUE@@NATIVE_LINKER_TRUE@@TLS_TRUE@tls_test_DEPENDENCIES = gcctestdir/ld tls_test_c.o
@GCC_TRUE@@NATIVE_LINKER_TRUE@@TLS_TRUE@tls_test_LDFLAGS = -Bgcctestdir/
copy_test$(EXEEXT): $(copy_test_OBJECTS) $(copy_test_DEPENDENCIES) $(EXTRA_copy_test_DEPENDENCIES)
@rm -f copy_test$(EXEEXT)
$(copy_test_LINK) $(copy_test_OBJECTS) $(copy_test_LDADD) $(LIBS)
+copy_test_relro$(EXEEXT): $(copy_test_relro_OBJECTS) $(copy_test_relro_DEPENDENCIES) $(EXTRA_copy_test_relro_DEPENDENCIES)
+ @rm -f copy_test_relro$(EXEEXT)
+ $(copy_test_relro_LINK) $(copy_test_relro_OBJECTS) $(copy_test_relro_LDADD) $(LIBS)
discard_locals_test$(EXEEXT): $(discard_locals_test_OBJECTS) $(discard_locals_test_DEPENDENCIES) $(EXTRA_discard_locals_test_DEPENDENCIES)
@rm -f discard_locals_test$(EXEEXT)
$(discard_locals_test_LINK) $(discard_locals_test_OBJECTS) $(discard_locals_test_LDADD) $(LIBS)
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/common_test_1.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/constructor_test.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/copy_test.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/copy_test_relro.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/discard_locals_test.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dynamic_list_2.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/eh_test.Po@am__quote@
@p='weak_plt$(EXEEXT)'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
copy_test.log: copy_test$(EXEEXT)
@p='copy_test$(EXEEXT)'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
+copy_test_relro.log: copy_test_relro$(EXEEXT)
+ @p='copy_test_relro$(EXEEXT)'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
tls_test.log: tls_test$(EXEEXT)
@p='tls_test$(EXEEXT)'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
tls_pic_test.log: tls_pic_test$(EXEEXT)
@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXCOMPILE) -c -fpic -o $@ $<
@GCC_TRUE@@NATIVE_LINKER_TRUE@copy_test_2.so: gcctestdir/ld copy_test_2_pic.o
@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXLINK) -Bgcctestdir/ -shared copy_test_2_pic.o
+@GCC_TRUE@@NATIVE_LINKER_TRUE@copy_test_relro_1_pic.o: copy_test_relro_1.cc
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXCOMPILE) -c -fpic -o $@ $<
+@GCC_TRUE@@NATIVE_LINKER_TRUE@copy_test_relro_1.so: gcctestdir/ld copy_test_relro_1_pic.o
+@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXLINK) -Bgcctestdir/ -shared -Wl,-z,relro copy_test_relro_1_pic.o
@DEFAULT_TARGET_POWERPC_FALSE@@GCC_TRUE@@NATIVE_LINKER_TRUE@copy_test_protected.err: copy_test_protected.o copy_test_2.so gcctestdir/ld
@DEFAULT_TARGET_POWERPC_FALSE@@GCC_TRUE@@NATIVE_LINKER_TRUE@ @echo $(CXXLINK) -Bgcctestdir/ -o copy_test_protected copy_test_protected.o copy_test_2.so -Wl,-R,. "2>$@"
@DEFAULT_TARGET_POWERPC_FALSE@@GCC_TRUE@@NATIVE_LINKER_TRUE@ @if $(CXXLINK) -Bgcctestdir/ -o copy_test_protected copy_test_protected.o copy_test_2.so -Wl,-R,. 2>$@; \
--- /dev/null
+// 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;
+}
--- /dev/null
+// 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 };