+ // 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();
+ }
+
+ // A section without a priority follows a section with a priority.
+ // This is the reverse of .ctors and .dtors sections.
+ bool s1_has_priority = s1.has_priority();
+ bool s2_has_priority = s2.has_priority();
+ if (s1_has_priority && !s2_has_priority)
+ return true;
+ if (!s1_has_priority && s2_has_priority)
+ return false;
+
+ // .ctors and .dtors sections without priority come after
+ // .init_array and .fini_array sections without priority.
+ if (!s1_has_priority
+ && (s1.section_name() == ".ctors" || s1.section_name() == ".dtors")
+ && s1.section_name() != s2.section_name())
+ return false;
+ if (!s2_has_priority
+ && (s2.section_name() == ".ctors" || s2.section_name() == ".dtors")
+ && s2.section_name() != s1.section_name())
+ return true;
+
+ // Sort by priority if we can.
+ if (s1_has_priority)
+ {
+ unsigned int s1_prio = s1.get_priority();
+ unsigned int s2_prio = s2.get_priority();
+ if (s1_prio < s2_prio)
+ return true;
+ else if (s1_prio > s2_prio)
+ return false;
+ }
+
+ // 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;
+
+ // Otherwise we sort by name.
+ int compare = s1.section_name().compare(s2.section_name());
+ if (compare != 0)
+ return compare < 0;
+
+ // Otherwise we keep the input order.
+ return s1.index() < s2.index();
+}
+
+// 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
+{
+ unsigned int s1_secn_index = s1.input_section().section_order_index();
+ unsigned int s2_secn_index = s2.input_section().section_order_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;
+}
+
+// This updates the section order index of input sections according to the
+// the order specified in the mapping from Section id to order index.
+
+void
+Output_section::update_section_layout(
+ const Section_layout_order& order_map)
+{
+ for (Input_section_list::iterator p = this->input_sections_.begin();
+ p != this->input_sections_.end();
+ ++p)
+ {
+ if (p->is_input_section()
+ || p->is_relaxed_input_section())
+ {
+ Object* obj = (p->is_input_section()
+ ? p->relobj()
+ : p->relaxed_input_section()->relobj());
+ unsigned int shndx = p->shndx();
+ Section_layout_order::const_iterator it
+ = order_map.find(Section_id(obj, shndx));
+ if (it == order_map.end())
+ continue;
+ unsigned int section_order_index = it->second;
+ if (section_order_index != 0)
+ {
+ p->set_section_order_index(section_order_index);
+ this->set_input_section_order_specified();
+ }
+ }
+ }
+}
+
+// Sort the input sections attached to an output section.
+
+void
+Output_section::sort_attached_input_sections()
+{
+ if (this->attached_input_sections_are_sorted_)
+ return;
+