ld: change --enable-new-dtags to only generate new dtags
[deliverable/binutils-gdb.git] / gold / layout.cc
index 86e39ebcf688949c43e1f1ba7642260eda2fabcf..250782a92bead199a193ca48ed9c6d4b7e41c70c 100644 (file)
@@ -582,7 +582,8 @@ bool
 Layout::include_section(Sized_relobj_file<size, big_endian>*, const char* name,
                        const elfcpp::Shdr<size, big_endian>& shdr)
 {
-  if (shdr.get_sh_flags() & elfcpp::SHF_EXCLUDE)
+  if (!parameters->options().relocatable()
+      && (shdr.get_sh_flags() & elfcpp::SHF_EXCLUDE))
     return false;
 
   switch (shdr.get_sh_type())
@@ -1032,6 +1033,33 @@ Layout::init_fixed_output_section(const char* name,
   return os;
 }
 
+// Return the index by which an input section should be ordered.  This
+// is used to sort some .text sections, for compatibility with GNU ld.
+
+int
+Layout::special_ordering_of_input_section(const char* name)
+{
+  // The GNU linker has some special handling for some sections that
+  // wind up in the .text section.  Sections that start with these
+  // prefixes must appear first, and must appear in the order listed
+  // here.
+  static const char* const text_section_sort[] = 
+  {
+    ".text.unlikely",
+    ".text.exit",
+    ".text.startup",
+    ".text.hot"
+  };
+
+  for (size_t i = 0;
+       i < sizeof(text_section_sort) / sizeof(text_section_sort[0]);
+       i++)
+    if (is_prefix_of(text_section_sort[i], name))
+      return i;
+
+  return -1;
+}
+
 // Return the output section to use for input section SHNDX, with name
 // NAME, with header HEADER, from object OBJECT.  RELOC_SHNDX is the
 // index of a relocation section which applies to this section, or 0
@@ -1119,6 +1147,14 @@ Layout::layout(Sized_relobj_file<size, big_endian>* object, unsigned int shndx,
                  || strcmp(name, ".dtors") == 0))))
     os->set_must_sort_attached_input_sections();
 
+  // By default the GNU linker sorts some special text sections ahead
+  // of others.  We are compatible.
+  if (!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();
+
   // 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
@@ -1606,6 +1642,16 @@ Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
                  || strcmp(name, ".dtors") == 0))))
     os->set_may_sort_attached_input_sections();
 
+  // The GNU linker by default sorts .text.{unlikely,exit,startup,hot}
+  // 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()
+      && !this->is_section_ordering_specified()
+      && !parameters->options().relocatable()
+      && strcmp(name, ".text") == 0)
+    os->set_may_sort_attached_input_sections();
+
   // Check for .stab*str sections, as .stab* sections need to link to
   // them.
   if (type == elfcpp::SHT_STRTAB
@@ -4601,8 +4647,9 @@ Layout::finish_dynamic_section(const Input_objects* input_objects,
            }
        }
 
-      odyn->add_string(elfcpp::DT_RPATH, rpath_val);
-      if (parameters->options().enable_new_dtags())
+      if (!parameters->options().enable_new_dtags())
+       odyn->add_string(elfcpp::DT_RPATH, rpath_val);
+      else
        odyn->add_string(elfcpp::DT_RUNPATH, rpath_val);
     }
 
@@ -5003,6 +5050,19 @@ Layout::get_allocated_sections(Section_list* section_list) const
       section_list->push_back(*p);
 }
 
+// Store the executable sections into the section list.
+
+void
+Layout::get_executable_sections(Section_list* section_list) const
+{
+  for (Section_list::const_iterator p = this->section_list_.begin();
+       p != this->section_list_.end();
+       ++p)
+    if (((*p)->flags() & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
+       == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
+      section_list->push_back(*p);
+}
+
 // Create an output segment.
 
 Output_segment*
This page took 0.024808 seconds and 4 git commands to generate.