Patch to do reorder text and data sections according to a user specified sequence.

Sriraman Tallam tmsriram@google.com
Thu Jun 3 06:51:00 GMT 2010


Hi,

    I have attached the patch to fix the bug in section ordering.
Sections which do not match any pattern in the file will be laid out
ahead of sections that match some pattern to remove the ambiguity that
was causing the problem. Please let me know if this is ok.

	* output.cc
	(Output_section::Input_section_sort_entry::compare_section_ordering):
	Change to return non-zero correctly.
	(Output_section::Input_section_sort_section_order_index_compare::operator()):
        Change to fix ambiguity in comparisons.

Thanks,
-Sri.

On Wed, Jun 2, 2010 at 2:04 PM, Sriraman Tallam <tmsriram@google.com> wrote:
> The bug seems to be with the sort compare function. It tries to order
> input sections based on the section ordering specified. If there are
> some input sections with no ordering, then it tries to preserve the
> original order. The way it is designed currently is causing conflicts.
> I will prepare a patch to fix this.
>
> Thanks,
> -Sri.
>
> On Wed, Jun 2, 2010 at 10:16 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
>> On Tue, Jun 1, 2010 at 4:40 PM, Sriraman Tallam <tmsriram@google.com> wrote:
>>> Hi Ian,
>>>
>>>     I committed the patch after making the changes you mentioned. I
>>> did not add a warning when using the SORT clause and
>>> --section-ordering file. Like you said, I will do this as a separate
>>> patch after I figure out how to do this effectively.
>>>
>>> 2010-06-01  Sriraman Tallam  <tmsriram@google.com>
>>>
>>>        * gold.h (is_wildcard_string): New function.
>>>        * layout.cc (Layout::layout): Pass this pointer to add_input_section.
>>>        (Layout::layout_eh_frame): Ditto.
>>>        (Layout::find_section_order_index): New method.
>>>        (Layout::read_layout_from_file): New method.
>>>        * layout.h (Layout::find_section_order_index): New method.
>>>        (Layout::read_layout_from_file): New method.
>>>        (Layout::input_section_position_): New private member.
>>>        (Layout::input_section_glob_): New private member.
>>>        * main.cc (main): Call read_layout_from_file here.
>>>        * options.h (--section-ordering-file): New option.
>>>        * output.cc (Output_section::input_section_order_specified_): New
>>>        member.
>>>        (Output_section::Output_section): Initialize new member.
>>>        (Output_section::add_input_section): Add new parameter.
>>>        Keep input sections when --section-ordering-file is used.
>>>        (Output_section::set_final_data_size): Sort input sections when
>>>        section ordering file is specified.
>>>        (Output_section::Input_section_sort_entry): Add new parameter.
>>>        Check sorting type.
>>>        (Output_section::Input_section_sort_entry::compare_section_ordering):
>>>        New method.
>>>        (Output_section::Input_section_sort_compare::operator()): Change to
>>>        consider section_order_index.
>>>        (Output_section::Input_section_sort_init_fini_compare::operator()):
>>>        Change to consider section_order_index.
>>>        (Output_section::Input_section_sort_section_order_index_compare
>>>        ::operator()): New method.
>>>        (Output_section::sort_attached_input_sections): Change to sort
>>>        according to section order when specified.
>>>        (Output_section::add_input_section<32, true>): Add new parameter.
>>>        (Output_section::add_input_section<64, true>): Add new parameter.
>>>        (Output_section::add_input_section<32, false>): Add new parameter.
>>>        (Output_section::add_input_section<64, false>): Add new parameter.
>>>        * output.h (Output_section::add_input_section): Add new parameter.
>>>        (Output_section::input_section_order_specified): New
>>>        method.
>>>        (Output_section::set_input_section_order_specified): New method.
>>>        (Input_section::Input_section): Initialize section_order_index_.
>>>        (Input_section::section_order_index): New method.
>>>        (Input_section::set_section_order_index): New method.
>>>        (Input_section::section_order_index_): New member.
>>>        (Input_section::Input_section_sort_section_order_index_compare): New
>>>        struct.
>>>        (Output_section::input_section_order_specified_): New member.
>>>        * script-sections.cc (is_wildcard_string): Delete and move modified
>>>        method to gold.h.
>>>        (Output_section_element_input::Output_section_element_input): Modify
>>>        call to is_wildcard_string.
>>>        (Output_section_element_input::Input_section_pattern
>>>        ::Input_section_pattern): Ditto.
>>>        (Output_section_element_input::Output_section_element_input): Ditto.
>>>        * testsuite/Makefile.am (final_layout): New test case.
>>>        * testsuite/Makefile.in: Regenerate.
>>>        * testsuite/final_layout.cc: New file.
>>>        * testsuite/final_layout.sh: New file.
>>>
>>>
>>
>> testsuite/final_layout.sh failed on Linux/x86-64:
>>
>> http://www.sourceware.org/bugzilla/show_bug.cgi?id=11658
>>
>> --
>> H.J.
>>
>
-------------- next part --------------
Index: output.cc
===================================================================
RCS file: /cvs/src/src/gold/output.cc,v
retrieving revision 1.127
diff -u -u -p -r1.127 output.cc
--- output.cc	1 Jun 2010 23:37:57 -0000	1.127
+++ output.cc	3 Jun 2010 06:17:16 -0000
@@ -2801,20 +2801,21 @@ class Output_section::Input_section_sort
     return memcmp(base_name + base_len - 2, ".o", 2) == 0;
   }
 
-  // Returns 0 if sections are not comparable. Returns 1 if THIS is the
-  // first section in order, returns -1 for S.
+  // Returns 1 if THIS should appear before S in section order, -1 if S
+  // appears before THIS and 0 if they are not comparable.
   int
   compare_section_ordering(const Input_section_sort_entry& s) const
   {
-    gold_assert(this->index_ != -1U);
-    if (this->input_section_.section_order_index() == 0
-        || s.input_section().section_order_index() == 0)
-      return 0;
-    if (this->input_section_.section_order_index()
-        < s.input_section().section_order_index())
-      return 1;
-    else
-      return -1;
+    unsigned int this_secn_index = this->input_section_.section_order_index();
+    unsigned int s_secn_index = s.input_section().section_order_index();
+    if (this_secn_index > 0 && s_secn_index > 0)
+      {
+        if (this_secn_index < s_secn_index)
+          return 1;
+        else if (this_secn_index > s_secn_index)
+          return -1;
+      }
+    return 0;
   }
 
  private:
@@ -2935,20 +2936,23 @@ Output_section::Input_section_sort_init_
   return s1.index() < s2.index();
 }
 
-// Return true if S1 should come before S2.
+// Return true if S1 should come before S2.  Sections that do not match
+// any pattern in the section ordering file are placed ahead of the sections
+// that match some pattern.
+
 bool
 Output_section::Input_section_sort_section_order_index_compare::operator()(
     const Output_section::Input_section_sort_entry& s1,
     const Output_section::Input_section_sort_entry& s2) const
 {
-  // Check if a section order exists for these sections through a section
-  // ordering file.  If sequence_num is 0, an order does not exist.
-  int sequence_num = s1.compare_section_ordering(s2);
-  if (sequence_num != 0)
-    return sequence_num == 1;
+  unsigned int s1_secn_index = s1.input_section().section_order_index();
+  unsigned int s2_secn_index = s2.input_section().section_order_index();
 
-  // Otherwise we keep the input order.
-  return s1.index() < s2.index();
+  // Keep input order if section ordering cannot determine order.
+  if (s1_secn_index == s2_secn_index)
+    return s1.index() < s2.index();
+  
+  return s1_secn_index < s2_secn_index;
 }


More information about the Binutils mailing list