diff options
author | Andrew Hsieh <andrewhsieh@google.com> | 2013-01-27 14:38:41 -0800 |
---|---|---|
committer | Andrew Hsieh <andrewhsieh@google.com> | 2013-01-27 14:38:41 -0800 |
commit | dbb331ef919ae846a3983b7be1e5ffbb1ee1a839 (patch) | |
tree | bbda32952d67e2a1a9d8a12ab781bd93132cd88d | |
parent | 43cc3f59831134ed0b2447bb21767371417ac54a (diff) | |
download | toolchain_binutils-dbb331ef919ae846a3983b7be1e5ffbb1ee1a839.tar.gz toolchain_binutils-dbb331ef919ae846a3983b7be1e5ffbb1ee1a839.tar.bz2 toolchain_binutils-dbb331ef919ae846a3983b7be1e5ffbb1ee1a839.zip |
[2.22.90] Backport of three patches to fix text section reordering
2013-01-24 Sriraman Tallam <tmsriram@google.com>
* layout.cc (Layout::layout): Check for option text_reorder.
(Layout::make_output_section): Ditto.
* options.h (text_reorder): New option.
* output.cc (Input_section_sort_compare): Remove special ordering
of section names.
(Output_section::
Input_section_sort_section_name_special_ordering_compare::
operator()): New function.
(Output_section::sort_attached_input_sections): Use new sort function
for .text.
* output.h (Input_section_sort_section_name_special_ordering_compare):
New struct.
* testsuite/Makefile.am (text_section_grouping): Test option
--no-text-reorder
* testsuite/Makefile.in: Regenerate.
* testsuite/text_section_grouping.sh: Check order of functions without
default text reordering.
2013-01-16 Sriraman Tallam <tmsriram@google.com>
* testsuite/plugin_final_layout.cc: Fix comment.
2013-01-16 Sriraman Tallam <tmsriram@google.com>
* layout.cc (Layout::layout): Do not do default sorting for
text sections when section ordering is specified.
(make_output_section): Ditto.
* testsuite/plugin_final_layout.cc: Name the function sections
to catch reordering issues.
Change-Id: I3413636486d578d3482f9a8ec1d93a0ec0a8f7d8
-rw-r--r-- | binutils-2.22/gold/layout.cc | 8 | ||||
-rw-r--r-- | binutils-2.22/gold/options.h | 5 | ||||
-rw-r--r-- | binutils-2.22/gold/output.cc | 52 | ||||
-rw-r--r-- | binutils-2.22/gold/output.h | 9 | ||||
-rw-r--r-- | binutils-2.22/gold/testsuite/Makefile.am | 14 | ||||
-rw-r--r-- | binutils-2.22/gold/testsuite/Makefile.in | 17 | ||||
-rw-r--r-- | binutils-2.22/gold/testsuite/plugin_final_layout.cc | 8 | ||||
-rw-r--r-- | binutils-2.22/gold/testsuite/text_section_grouping.cc | 72 | ||||
-rwxr-xr-x | binutils-2.22/gold/testsuite/text_section_grouping.sh | 73 |
9 files changed, 242 insertions, 16 deletions
diff --git a/binutils-2.22/gold/layout.cc b/binutils-2.22/gold/layout.cc index cc800cdb..994249b1 100644 --- a/binutils-2.22/gold/layout.cc +++ b/binutils-2.22/gold/layout.cc @@ -1081,7 +1081,9 @@ Layout::layout(Sized_relobj_file<size, big_endian>* object, unsigned int shndx, // By default the GNU linker sorts some special text sections ahead // of others. We are compatible. - if (!this->script_options_->saw_sections_clause() + if (parameters->options().text_reorder() + && !this->script_options_->saw_sections_clause() + && !this->is_section_ordering_specified() && !parameters->options().relocatable() && Layout::special_ordering_of_input_section(name) >= 0) os->set_must_sort_attached_input_sections(); @@ -1568,7 +1570,9 @@ Layout::make_output_section(const char* name, elfcpp::Elf_Word type, // sections before other .text sections. We are compatible. We // need to know that this might happen before we attach any input // sections. - if (!this->script_options_->saw_sections_clause() + if (parameters->options().text_reorder() + && !this->script_options_->saw_sections_clause() + && !this->is_section_ordering_specified() && !parameters->options().relocatable() && strcmp(name, ".text") == 0) os->set_may_sort_attached_input_sections(); diff --git a/binutils-2.22/gold/options.h b/binutils-2.22/gold/options.h index 50762a54..5def1d58 100644 --- a/binutils-2.22/gold/options.h +++ b/binutils-2.22/gold/options.h @@ -878,6 +878,11 @@ class General_options DEFINE_dirlist(library_path, options::TWO_DASHES, 'L', N_("Add directory to search path"), N_("DIR")); + DEFINE_bool(text_reorder, options::TWO_DASHES, '\0', true, + N_("Enable text section reordering for GCC section names " + "(default)"), + N_("Disable text section reordering for GCC section names")); + DEFINE_bool(nostdlib, options::ONE_DASH, '\0', false, N_(" Only search directories specified on the command line."), NULL); diff --git a/binutils-2.22/gold/output.cc b/binutils-2.22/gold/output.cc index 82e2ea7b..ab9e5435 100644 --- a/binutils-2.22/gold/output.cc +++ b/binutils-2.22/gold/output.cc @@ -3335,19 +3335,6 @@ Output_section::Input_section_sort_compare::operator()( return s1.index() < s2.index(); } - // Some input section names have special ordering requirements. - int o1 = Layout::special_ordering_of_input_section(s1.section_name().c_str()); - int o2 = Layout::special_ordering_of_input_section(s2.section_name().c_str()); - if (o1 != o2) - { - if (o1 < 0) - return false; - else if (o2 < 0) - return true; - else - return o1 < o2; - } - // A section with a priority follows a section without a priority. bool s1_has_priority = s1.has_priority(); bool s2_has_priority = s2.has_priority(); @@ -3454,6 +3441,42 @@ Output_section::Input_section_sort_section_order_index_compare::operator()( return s1_secn_index < s2_secn_index; } +// Return true if S1 should come before S2. This is the sort comparison +// function for .text to sort sections with prefixes +// .text.{unlikely,exit,startup,hot} before other sections. +bool +Output_section::Input_section_sort_section_name_special_ordering_compare + ::operator()( + const Output_section::Input_section_sort_entry& s1, + const Output_section::Input_section_sort_entry& s2) const +{ + // We sort all the sections with no names to the end. + if (!s1.section_has_name() || !s2.section_has_name()) + { + if (s1.section_has_name()) + return true; + if (s2.section_has_name()) + return false; + return s1.index() < s2.index(); + } + + // Some input section names have special ordering requirements. + int o1 = Layout::special_ordering_of_input_section(s1.section_name().c_str()); + int o2 = Layout::special_ordering_of_input_section(s2.section_name().c_str()); + if (o1 != o2) + { + if (o1 < 0) + return false; + else if (o2 < 0) + return true; + else + return o1 < o2; + } + + // Keep input order otherwise. + return s1.index() < s2.index(); +} + // This updates the section order index of input sections according to the // the order specified in the mapping from Section id to order index. @@ -3522,6 +3545,9 @@ Output_section::sort_attached_input_sections() || this->type() == elfcpp::SHT_FINI_ARRAY) std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_init_fini_compare()); + else if (strcmp(this->name(), ".text") == 0) + std::sort(sort_list.begin(), sort_list.end(), + Input_section_sort_section_name_special_ordering_compare()); else std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_compare()); diff --git a/binutils-2.22/gold/output.h b/binutils-2.22/gold/output.h index 170f0ff8..553d76aa 100644 --- a/binutils-2.22/gold/output.h +++ b/binutils-2.22/gold/output.h @@ -3999,6 +3999,15 @@ class Output_section : public Output_data const Input_section_sort_entry&) const; }; + // This is the sort comparison function for .text to sort sections with + // prefixes .text.{unlikely,exit,startup,hot} before other sections. + struct Input_section_sort_section_name_special_ordering_compare + { + bool + operator()(const Input_section_sort_entry&, + const Input_section_sort_entry&) const; + }; + // Fill data. This is used to fill in data between input sections. // It is also used for data statements (BYTE, WORD, etc.) in linker // scripts. When we have to keep track of the input sections, we diff --git a/binutils-2.22/gold/testsuite/Makefile.am b/binutils-2.22/gold/testsuite/Makefile.am index b8b88e86..dd0e91ff 100644 --- a/binutils-2.22/gold/testsuite/Makefile.am +++ b/binutils-2.22/gold/testsuite/Makefile.am @@ -220,6 +220,20 @@ final_layout: final_layout.o final_layout_sequence.txt gcctestdir/ld final_layout.stdout: final_layout $(TEST_NM) -n final_layout > final_layout.stdout +check_SCRIPTS += text_section_grouping.sh +check_DATA += text_section_grouping.stdout text_section_no_grouping.stdout +MOSTLYCLEANFILES += text_section_grouping text_section_no_grouping +text_section_grouping.o: text_section_grouping.cc + $(CXXCOMPILE) -O0 -c -ffunction-sections -g -o $@ $< +text_section_grouping: text_section_grouping.o gcctestdir/ld + $(CXXLINK) -Bgcctestdir/ text_section_grouping.o +text_section_no_grouping: text_section_grouping.o gcctestdir/ld + $(CXXLINK) -Bgcctestdir/ -Wl,--no-text-reorder text_section_grouping.o +text_section_grouping.stdout: text_section_grouping + $(TEST_NM) -n --synthetic text_section_grouping > text_section_grouping.stdout +text_section_no_grouping.stdout: text_section_no_grouping + $(TEST_NM) -n --synthetic text_section_no_grouping > text_section_no_grouping.stdout + check_PROGRAMS += icf_virtual_function_folding_test MOSTLYCLEANFILES += icf_virtual_function_folding_test icf_virtual_function_folding_test.o: icf_virtual_function_folding_test.cc diff --git a/binutils-2.22/gold/testsuite/Makefile.in b/binutils-2.22/gold/testsuite/Makefile.in index 3660aa9e..4e4f971c 100644 --- a/binutils-2.22/gold/testsuite/Makefile.in +++ b/binutils-2.22/gold/testsuite/Makefile.in @@ -81,6 +81,7 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_safe_test.sh \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_safe_so_test.sh \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ final_layout.sh \ +@GCC_TRUE@@NATIVE_LINKER_TRUE@ text_section_grouping.sh \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_preemptible_functions_test.sh \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_string_merge_test.sh \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_sht_rel_addend_test.sh \ @@ -111,6 +112,8 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_safe_so_test_1.stdout \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_safe_so_test_2.stdout \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ final_layout.stdout \ +@GCC_TRUE@@NATIVE_LINKER_TRUE@ text_section_grouping.stdout \ +@GCC_TRUE@@NATIVE_LINKER_TRUE@ text_section_no_grouping.stdout \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_preemptible_functions_test.stdout \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_string_merge_test.stdout \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_sht_rel_addend_test.stdout \ @@ -122,6 +125,8 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_keep_unique_test \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_safe_test icf_safe_so_test \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ final_layout \ +@GCC_TRUE@@NATIVE_LINKER_TRUE@ text_section_grouping \ +@GCC_TRUE@@NATIVE_LINKER_TRUE@ text_section_no_grouping \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_virtual_function_folding_test \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_preemptible_functions_test \ @GCC_TRUE@@NATIVE_LINKER_TRUE@ icf_string_merge_test \ @@ -3820,6 +3825,8 @@ icf_safe_so_test.sh.log: icf_safe_so_test.sh @p='icf_safe_so_test.sh'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post) final_layout.sh.log: final_layout.sh @p='final_layout.sh'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post) +text_section_grouping.sh.log: text_section_grouping.sh + @p='text_section_grouping.sh'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post) icf_preemptible_functions_test.sh.log: icf_preemptible_functions_test.sh @p='icf_preemptible_functions_test.sh'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post) icf_string_merge_test.sh.log: icf_string_merge_test.sh @@ -4459,6 +4466,16 @@ uninstall-am: @GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXLINK) -Bgcctestdir/ -Wl,--section-ordering-file,final_layout_sequence.txt final_layout.o @GCC_TRUE@@NATIVE_LINKER_TRUE@final_layout.stdout: final_layout @GCC_TRUE@@NATIVE_LINKER_TRUE@ $(TEST_NM) -n final_layout > final_layout.stdout +@GCC_TRUE@@NATIVE_LINKER_TRUE@text_section_grouping.o: text_section_grouping.cc +@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXCOMPILE) -O0 -c -ffunction-sections -g -o $@ $< +@GCC_TRUE@@NATIVE_LINKER_TRUE@text_section_grouping: text_section_grouping.o gcctestdir/ld +@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXLINK) -Bgcctestdir/ text_section_grouping.o +@GCC_TRUE@@NATIVE_LINKER_TRUE@text_section_no_grouping: text_section_grouping.o gcctestdir/ld +@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXLINK) -Bgcctestdir/ -Wl,--no-text-reorder text_section_grouping.o +@GCC_TRUE@@NATIVE_LINKER_TRUE@text_section_grouping.stdout: text_section_grouping +@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(TEST_NM) -n --synthetic text_section_grouping > text_section_grouping.stdout +@GCC_TRUE@@NATIVE_LINKER_TRUE@text_section_no_grouping.stdout: text_section_no_grouping +@GCC_TRUE@@NATIVE_LINKER_TRUE@ $(TEST_NM) -n --synthetic text_section_no_grouping > text_section_no_grouping.stdout @GCC_TRUE@@NATIVE_LINKER_TRUE@icf_virtual_function_folding_test.o: icf_virtual_function_folding_test.cc @GCC_TRUE@@NATIVE_LINKER_TRUE@ $(CXXCOMPILE) -O0 -c -ffunction-sections -fPIE -g -o $@ $< @GCC_TRUE@@NATIVE_LINKER_TRUE@icf_virtual_function_folding_test: icf_virtual_function_folding_test.o gcctestdir/ld diff --git a/binutils-2.22/gold/testsuite/plugin_final_layout.cc b/binutils-2.22/gold/testsuite/plugin_final_layout.cc index 3e264f62..169eeefd 100644 --- a/binutils-2.22/gold/testsuite/plugin_final_layout.cc +++ b/binutils-2.22/gold/testsuite/plugin_final_layout.cc @@ -21,16 +21,22 @@ // MA 02110-1301, USA. // The goal of this program is to verify if section ordering -// via plugins happens correctly. +// via plugins happens correctly. Also, test if plugin based ordering +// overrides default text section ordering where ".text.hot" sections +// are grouped. The plugin does not want foo and baz next to each other. +// Plugin section order is foo() followed by bar() and then baz(). +__attribute__ ((section(".text._Z3barv"))) void bar () { } +__attribute__ ((section(".text.hot._Z3bazv"))) void baz () { } +__attribute__ ((section(".text.hot._Z3foov"))) void foo () { } diff --git a/binutils-2.22/gold/testsuite/text_section_grouping.cc b/binutils-2.22/gold/testsuite/text_section_grouping.cc new file mode 100644 index 00000000..5a3a809f --- /dev/null +++ b/binutils-2.22/gold/testsuite/text_section_grouping.cc @@ -0,0 +1,72 @@ +// text_section_grouping.cc -- a test case for gold + +// Copyright 2012 Free Software Foundation, Inc. +// Written by Sriraman Tallam <tmsriram@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. + +// The goal of this program is to verify if .text sections are grouped +// according to prefix. .text.unlikely, .text.startup and .text.hot should +// be grouped and placed together. + +extern "C" +__attribute__ ((section(".text.hot.foo"))) +int hot_foo() +{ + return 1; +} + +extern "C" +__attribute__ ((section(".text.startup.foo"))) +int startup_foo() +{ + return 1; +} + +extern "C" +__attribute__ ((section(".text.unlikely.foo"))) +int unlikely_foo() +{ + return 1; +} + +extern "C" +__attribute__ ((section(".text.hot.bar"))) +int hot_bar() +{ + return 1; +} + +extern "C" +__attribute__ ((section(".text.startup.bar"))) +int startup_bar() +{ + return 1; +} + +extern "C" +__attribute__ ((section(".text.unlikely.bar"))) +int unlikely_bar() +{ + return 1; +} + +int main() +{ + return 1; +} diff --git a/binutils-2.22/gold/testsuite/text_section_grouping.sh b/binutils-2.22/gold/testsuite/text_section_grouping.sh new file mode 100755 index 00000000..84ebe4c9 --- /dev/null +++ b/binutils-2.22/gold/testsuite/text_section_grouping.sh @@ -0,0 +1,73 @@ +#!/bin/sh + +# text_section_grouping.sh -- test + +# Copyright 2012 Free Software Foundation, Inc. +# Written by Sriraman Tallam <tmsriram@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. + +# The goal of this program is to verify if .text sections are grouped +# according to prefix. .text.unlikely, .text.startup and .text.hot should +# be grouped and placed together. + +# Also check if the functions do not get grouped with option --no-text-reorder. + +set -e + +check() +{ + awk " +BEGIN { saw1 = 0; saw2 = 0; err = 0; } +/.*$2\$/ { saw1 = 1; } +/.*$3\$/ { + saw2 = 1; + if (!saw1) + { + printf \"layout of $2 and $3 is not right\\n\"; + err = 1; + exit 1; + } + } +END { + if (!saw1 && !err) + { + printf \"did not see $2\\n\"; + exit 1; + } + if (!saw2 && !err) + { + printf \"did not see $3\\n\"; + exit 1; + } + }" $1 +} + +# addr (unlikely_*) < addr (startup_*) < addr (hot_*) +check text_section_grouping.stdout "unlikely_foo" "startup_foo" +check text_section_grouping.stdout "startup_foo" "hot_foo" +check text_section_grouping.stdout "unlikely_bar" "startup_bar" +check text_section_grouping.stdout "startup_bar" "hot_bar" +check text_section_grouping.stdout "unlikely_foo" "startup_bar" +check text_section_grouping.stdout "startup_foo" "hot_bar" + +check text_section_no_grouping.stdout "hot_foo" "startup_foo" +check text_section_no_grouping.stdout "startup_foo" "unlikely_foo" +check text_section_no_grouping.stdout "unlikely_foo" "hot_bar" +check text_section_no_grouping.stdout "hot_bar" "startup_bar" +check text_section_no_grouping.stdout "startup_bar" "unlikely_bar" |