This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
gold patch committed: Reverse .ctors in .init_array
- From: Ian Lance Taylor <iant at google dot com>
- To: binutils at sourceware dot org
- Date: Fri, 24 Jun 2011 17:42:30 -0700
- Subject: gold patch committed: Reverse .ctors in .init_array
Alan pointed out that when a .ctors sections which has multiple words is
mapped to a .init_array section, we need to reverse the order of the
entries in the .ctors section. This is because .ctors and .init_array
are executed in reverse order, and we don't want the linker to change
the order in which the constructors are executed. The same applies to
.dtors mapped to .fini_array. This patch implements that in gold.
Committed to mainline.
Ian
2011-06-24 Ian Lance Taylor <iant@google.com>
* layout.cc: Include "object.h".
(ctors_sections_in_init_array): New static variable.
(Layout::is_ctors_in_init_array): New function.
(Layout::layout): Add entry to ctors_sections_in_init_array if
appropriate.
* layout.h (class Layout): Declare is_ctors_in_init_array.
* reloc.cc (Sized_relobj_file::do_relocate): Call reverse_words if
is_ctors_reverse_view is set.
(Sized_relobj_file::write_sections): Add layout parameter. Change
all callers. Set is_ctors_reverse_view field of View_size.
(Sized_relobj_file::reverse_words): New function.
* object.h (Sized_relobj_file::View_size): Add
is_ctors_reverse_view field.
(class Sized_relobj_file): Update declarations.
* testsuite/initpri3.c: New test.
* testsuite/Makefile.am: (check_PROGRAMS): Add initpri3a and
initpri3b.
(initpri3a_SOURCES, initpri3a_DEPENDENCIES): New variables.
(initpri3a_LDFLAGS, initpri3a_LDADD): New variables.
(initpri3b_SOURCES, initpri3b_DEPENDENCIES): New variables.
(initpri3b_LDFLAGS, initpri3b_LDADD): New variables.
* testsuite/Makefile.in: Rebuild.
Index: layout.cc
===================================================================
RCS file: /cvs/src/src/gold/layout.cc,v
retrieving revision 1.205
diff -p -u -r1.205 layout.cc
--- layout.cc 23 Jun 2011 00:46:13 -0000 1.205
+++ layout.cc 25 Jun 2011 00:35:35 -0000
@@ -46,6 +46,7 @@
#include "ehframe.h"
#include "compressed_output.h"
#include "reduced_debug_output.h"
+#include "object.h"
#include "reloc.h"
#include "descriptors.h"
#include "plugin.h"
@@ -617,6 +618,30 @@ Layout::find_output_segment(elfcpp::PT t
return NULL;
}
+// When we put a .ctors or .dtors section with more than one word into
+// a .init_array or .fini_array section, we need to reverse the words
+// in the .ctors/.dtors section. This is because .init_array executes
+// constructors front to back, where .ctors executes them back to
+// front, and vice-versa for .fini_array/.dtors. Although we do want
+// to remap .ctors/.dtors into .init_array/.fini_array because it can
+// be more efficient, we don't want to change the order in which
+// constructors/destructors are run. This set just keeps track of
+// these sections which need to be reversed. It is only changed by
+// Layout::layout. It should be a private member of Layout, but that
+// would require layout.h to #include object.h to get the definition
+// of Section_id.
+static Unordered_set<Section_id, Section_id_hash> ctors_sections_in_init_array;
+
+// Return whether OBJECT/SHNDX is a .ctors/.dtors section mapped to a
+// .init_array/.fini_array section.
+
+bool
+Layout::is_ctors_in_init_array(Relobj* relobj, unsigned int shndx) const
+{
+ return (ctors_sections_in_init_array.find(Section_id(relobj, shndx))
+ != ctors_sections_in_init_array.end());
+}
+
// Return the output section to use for section NAME with type TYPE
// and section flags FLAGS. NAME must be canonicalized in the string
// pool, and NAME_KEY is the key. ORDER is where this should appear
@@ -922,11 +947,11 @@ Layout::layout(Sized_relobj_file<size, b
}
// By default the GNU linker sorts input sections whose names match
- // .ctor.*, .dtor.*, .init_array.*, or .fini_array.*. The sections
- // are sorted by name. This is used to implement constructor
- // priority ordering. We are compatible. When we put .ctor
- // sections in .init_array and .dtor sections in .fini_array, we
- // must also sort plain .ctor and .dtor sections.
+ // .ctors.*, .dtors.*, .init_array.*, or .fini_array.*. The
+ // sections are sorted by name. This is used to implement
+ // constructor priority ordering. We are compatible. When we put
+ // .ctor sections in .init_array and .dtor sections in .fini_array,
+ // we must also sort plain .ctor and .dtor sections.
if (!this->script_options_->saw_sections_clause()
&& !parameters->options().relocatable()
&& (is_prefix_of(".ctors.", name)
@@ -938,6 +963,22 @@ Layout::layout(Sized_relobj_file<size, b
|| strcmp(name, ".dtors") == 0))))
os->set_must_sort_attached_input_sections();
+ // If this is a .ctors or .ctors.* section being mapped to a
+ // .init_array section, or a .dtors or .dtors.* section being mapped
+ // to a .fini_array section, we will need to reverse the words if
+ // there is more than one. Record this section for later. See
+ // ctors_sections_in_init_array above.
+ if (!this->script_options_->saw_sections_clause()
+ && !parameters->options().relocatable()
+ && shdr.get_sh_size() > size / 8
+ && (((strcmp(name, ".ctors") == 0
+ || is_prefix_of(".ctors.", name))
+ && strcmp(os->name(), ".init_array") == 0)
+ || ((strcmp(name, ".dtors") == 0
+ || is_prefix_of(".dtors.", name))
+ && strcmp(os->name(), ".fini_array") == 0)))
+ ctors_sections_in_init_array.insert(Section_id(object, shndx));
+
// FIXME: Handle SHF_LINK_ORDER somewhere.
elfcpp::Elf_Xword orig_flags = os->flags();
Index: layout.h
===================================================================
RCS file: /cvs/src/src/gold/layout.h,v
retrieving revision 1.93
diff -p -u -r1.93 layout.h
--- layout.h 23 Jun 2011 00:46:13 -0000 1.93
+++ layout.h 25 Jun 2011 00:35:35 -0000
@@ -651,6 +651,12 @@ class Layout
static bool
match_file_name(const Relobj* relobj, const char* file_name);
+ // Return whether section SHNDX in RELOBJ is a .ctors/.dtors section
+ // with more than one word being mapped to a .init_array/.fini_array
+ // section.
+ bool
+ is_ctors_in_init_array(Relobj* relobj, unsigned int shndx) const;
+
// Check if a comdat group or .gnu.linkonce section with the given
// NAME is selected for the link. If there is already a section,
// *KEPT_SECTION is set to point to the signature and the function
Index: reloc.cc
===================================================================
RCS file: /cvs/src/src/gold/reloc.cc,v
retrieving revision 1.67
diff -p -u -r1.67 reloc.cc
--- reloc.cc 31 May 2011 04:26:49 -0000 1.67
+++ reloc.cc 25 Jun 2011 00:35:35 -0000
@@ -659,7 +659,7 @@ Sized_relobj_file<size, big_endian>::do_
// section data to the output file. The second one applies
// relocations.
- this->write_sections(pshdrs, of, &views);
+ this->write_sections(layout, pshdrs, of, &views);
// To speed up relocations, we set up hash tables for fast lookup of
// input offsets to output addresses.
@@ -678,6 +678,8 @@ Sized_relobj_file<size, big_endian>::do_
{
if (views[i].view != NULL)
{
+ if (views[i].is_ctors_reverse_view)
+ this->reverse_words(views[i].view, views[i].view_size);
if (!views[i].is_postprocessing_view)
{
if (views[i].is_input_output_view)
@@ -712,7 +714,8 @@ struct Read_multiple_compare
template<int size, bool big_endian>
void
-Sized_relobj_file<size, big_endian>::write_sections(const unsigned char* pshdrs,
+Sized_relobj_file<size, big_endian>::write_sections(const Layout* layout,
+ const unsigned char* pshdrs,
Output_file* of,
Views* pviews)
{
@@ -761,6 +764,7 @@ Sized_relobj_file<size, big_endian>::wri
pvs->address = posd->address();
pvs->is_input_output_view = false;
pvs->is_postprocessing_view = false;
+ pvs->is_ctors_reverse_view = false;
continue;
}
@@ -875,6 +879,12 @@ Sized_relobj_file<size, big_endian>::wri
pvs->view_size = view_size;
pvs->is_input_output_view = output_offset == invalid_address;
pvs->is_postprocessing_view = os->requires_postprocessing();
+ pvs->is_ctors_reverse_view =
+ (!parameters->options().relocatable()
+ && view_size > size / 8
+ && (strcmp(os->name(), ".init_array") == 0
+ || strcmp(os->name(), ".fini_array") == 0)
+ && layout->is_ctors_in_init_array(this, i));
}
// Actually read the data.
@@ -1483,6 +1493,26 @@ Sized_relobj_file<size, big_endian>::fin
}
}
+// Reverse the words in a section. Used for .ctors sections mapped to
+// .init_array sections. See ctors_sections_in_init_array in
+// layout.cc.
+
+template<int size, bool big_endian>
+void
+Sized_relobj_file<size, big_endian>::reverse_words(unsigned char* view,
+ section_size_type view_size)
+{
+ typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
+ Valtype* vview = reinterpret_cast<Valtype*>(view);
+ section_size_type vview_size = view_size / (size / 8);
+ for (section_size_type i = 0; i < vview_size / 2; ++i)
+ {
+ Valtype tmp = vview[i];
+ vview[i] = vview[vview_size - 1 - i];
+ vview[vview_size - 1 - i] = tmp;
+ }
+}
+
// Class Merged_symbol_value.
template<int size>
Index: object.h
===================================================================
RCS file: /cvs/src/src/gold/object.h,v
retrieving revision 1.110
diff -p -u -r1.110 object.h
--- object.h 24 May 2011 21:57:28 -0000 1.110
+++ object.h 25 Jun 2011 00:35:35 -0000
@@ -2220,6 +2220,7 @@ class Sized_relobj_file : public Sized_r
section_size_type view_size;
bool is_input_output_view;
bool is_postprocessing_view;
+ bool is_ctors_reverse_view;
};
typedef std::vector<View_size> Views;
@@ -2311,7 +2312,8 @@ class Sized_relobj_file : public Sized_r
// Write section data to the output file. Record the views and
// sizes in VIEWS for use when relocating.
void
- write_sections(const unsigned char* pshdrs, Output_file*, Views*);
+ write_sections(const Layout*, const unsigned char* pshdrs, Output_file*,
+ Views*);
// Relocate the sections in the output file.
void
@@ -2320,6 +2322,11 @@ class Sized_relobj_file : public Sized_r
Views* pviews)
{ this->do_relocate_sections(symtab, layout, pshdrs, of, pviews); }
+ // Reverse the words in a section. Used for .ctors sections mapped
+ // to .init_array sections.
+ void
+ reverse_words(unsigned char*, section_size_type);
+
// Scan the input relocations for --emit-relocs.
void
emit_relocs_scan(Symbol_table*, Layout*, const unsigned char* plocal_syms,
Index: testsuite/initpri3.c
===================================================================
RCS file: testsuite/initpri3.c
diff -N testsuite/initpri3.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/initpri3.c 25 Jun 2011 00:35:35 -0000
@@ -0,0 +1,80 @@
+/* initpri3.c -- test ctor odering when using init_array.
+
+ Copyright 2011 Free Software Foundation, Inc.
+ Written by Ian Lance Taylor <iant@google.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. */
+
+/* This tests that the linker correctly orders .ctor entries when
+ putting them into .init_array, as is the default. */
+
+#include <assert.h>
+
+int i = 1;
+
+static void
+ctor1 (void)
+{
+ assert (i == 1);
+ i = 2;
+}
+
+static void
+ctor2 (void)
+{
+ assert (i == 2);
+ i = 3;
+}
+
+static void
+dtor1 (void)
+{
+ assert (i == 3);
+ i = 2;
+}
+
+static void
+dtor2 (void)
+{
+ assert (i == 2);
+ i = 1;
+}
+
+/* The .ctors section is run in reverse order, the .dtors section in
+ run in forward order. We give these arrays the "aligned" attribute
+ because the x86_64 ABI would otherwise give them a 16-byte
+ alignment, which may leave a hole in the section. */
+
+void (*ctors[]) (void)
+ __attribute__ ((aligned (4), section (".ctors"))) = {
+ ctor2,
+ ctor1
+};
+
+void (*dtors[]) (void)
+ __attribute__ ((aligned (4), section (".dtors"))) = {
+ dtor1,
+ dtor2
+};
+
+int
+main (void)
+{
+ assert (i == 3);
+ return 0;
+}
Index: testsuite/Makefile.am
===================================================================
RCS file: /cvs/src/src/gold/testsuite/Makefile.am,v
retrieving revision 1.165
diff -p -u -r1.165 Makefile.am
--- testsuite/Makefile.am 24 Jun 2011 18:08:49 -0000 1.165
+++ testsuite/Makefile.am 25 Jun 2011 00:35:36 -0000
@@ -846,6 +846,18 @@ initpri2_DEPENDENCIES = gcctestdir/ld
initpri2_LDFLAGS = -Bgcctestdir/
initpri2_LDADD =
+check_PROGRAMS += initpri3a
+initpri3a_SOURCES = initpri3.c
+initpri3a_DEPENDENCIES = gcctestdir/ld
+initpri3a_LDFLAGS = -Bgcctestdir/
+initpri3a_LDADD =
+
+check_PROGRAMS += initpri3b
+initpri3b_SOURCES = initpri3.c
+initpri3b_DEPENDENCIES = gcctestdir/ld
+initpri3b_LDFLAGS = -Bgcctestdir/ -Wl,--no-ctors-in-init-array
+initpri3b_LDADD =
+
# Test --detect-odr-violations
check_SCRIPTS += debug_msg.sh