* options.cc (parse_short_option): Keep dash_z from registering itself.
[deliverable/binutils-gdb.git] / gold / layout.cc
index 62ccaae2b1ea450b7c3f0cdeabc3e8948d85cd3f..df74df36c5039deb05bf327b11bf2678ea231edc 100644 (file)
@@ -1,6 +1,6 @@
 // layout.cc -- lay out output file sections for gold
 
-// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
+// Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
 // Written by Ian Lance Taylor <iant@google.com>.
 
 // This file is part of gold.
@@ -45,7 +45,9 @@
 #include "compressed_output.h"
 #include "reduced_debug_output.h"
 #include "reloc.h"
+#include "descriptors.h"
 #include "layout.h"
+#include "plugin.h"
 
 namespace gold
 {
@@ -138,14 +140,6 @@ Layout::Hash_key::operator()(const Layout::Key& k) const
  return k.first + k.second.first + k.second.second;
 }
 
-// Return whether PREFIX is a prefix of STR.
-
-static inline bool
-is_prefix_of(const char* prefix, const char* str)
-{
-  return strncmp(prefix, str, strlen(prefix)) == 0;
-}
-
 // Returns whether the given section is in the list of
 // debug-sections-used-by-some-version-of-gdb.  Currently,
 // we've checked versions of gdb up to and including 6.7.1.
@@ -205,6 +199,9 @@ bool
 Layout::include_section(Sized_relobj<size, big_endian>*, const char* name,
                        const elfcpp::Shdr<size, big_endian>& shdr)
 {
+  if (shdr.get_sh_flags() & elfcpp::SHF_EXCLUDE)
+    return false;
+
   switch (shdr.get_sh_type())
     {
     case elfcpp::SHT_NULL:
@@ -255,6 +252,14 @@ Layout::include_section(Sized_relobj<size, big_endian>*, const char* name,
               && !is_gdb_debug_section(name))
            return false;
        }
+      if (parameters->options().strip_lto_sections()
+          && !parameters->options().relocatable()
+          && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
+        {
+          // Ignore LTO sections containing intermediate code.
+          if (is_prefix_of(".gnu.lto_", name))
+            return false;
+        }
       return true;
 
     default:
@@ -421,7 +426,9 @@ Layout::choose_output_section(const Relobj* relobj, const char* name,
   // output section.
 
   size_t len = strlen(name);
-  if (is_input_section && !parameters->options().relocatable())
+  if (is_input_section
+      && !this->script_options_->saw_sections_clause()
+      && !parameters->options().relocatable())
     name = Layout::output_section_name(name, &len);
 
   Stringpool::Key name_key;
@@ -448,6 +455,8 @@ Layout::layout(Sized_relobj<size, big_endian>* object, unsigned int shndx,
               const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
               unsigned int reloc_shndx, unsigned int, off_t* off)
 {
+  *off = 0;
+
   if (!this->include_section(object, name, shdr))
     return NULL;
 
@@ -863,7 +872,8 @@ Layout::attach_allocated_section_to_segment(Output_section* os)
        ++p)
     {
       if ((*p)->type() == elfcpp::PT_LOAD
-          && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
+         && (parameters->options().omagic()
+             || ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W)))
         {
           // If -Tbss was specified, we need to separate the data
           // and BSS segments.
@@ -917,8 +927,7 @@ Layout::attach_allocated_section_to_segment(Output_section* os)
   if ((flags & elfcpp::SHF_TLS) != 0)
     {
       if (this->tls_segment_ == NULL)
-        this->tls_segment_ = this->make_output_segment(elfcpp::PT_TLS,
-                                                       seg_flags);
+       this->make_output_segment(elfcpp::PT_TLS, seg_flags);
       this->tls_segment_->add_output_section(os, seg_flags);
     }
 
@@ -928,8 +937,7 @@ Layout::attach_allocated_section_to_segment(Output_section* os)
     {
       gold_assert(seg_flags == (elfcpp::PF_R | elfcpp::PF_W));
       if (this->relro_segment_ == NULL)
-       this->relro_segment_ = this->make_output_segment(elfcpp::PT_GNU_RELRO,
-                                                        seg_flags);
+       this->make_output_segment(elfcpp::PT_GNU_RELRO, seg_flags);
       this->relro_segment_->add_output_section(os, seg_flags);
     }
 }
@@ -1106,7 +1114,8 @@ Layout::find_first_load_seg()
     {
       if ((*p)->type() == elfcpp::PT_LOAD
          && ((*p)->flags() & elfcpp::PF_R) != 0
-         && ((*p)->flags() & elfcpp::PF_W) == 0)
+         && (parameters->options().omagic()
+             || ((*p)->flags() & elfcpp::PF_W) == 0))
        return *p;
     }
 
@@ -1503,14 +1512,14 @@ Layout::create_build_id()
       char buffer[uuidsz];
       memset(buffer, 0, uuidsz);
 
-      int descriptor = ::open("/dev/urandom", O_RDONLY);
+      int descriptor = open_descriptor(-1, "/dev/urandom", O_RDONLY);
       if (descriptor < 0)
        gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
                   strerror(errno));
       else
        {
          ssize_t got = ::read(descriptor, buffer, uuidsz);
-         ::close(descriptor);
+         release_descriptor(descriptor, true);
          if (got < 0)
            gold_error(_("/dev/urandom: read failed: %s"), strerror(errno));
          else if (static_cast<size_t>(got) != uuidsz)
@@ -1728,6 +1737,9 @@ Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
        }
     }
 
+  const bool check_sections = parameters->options().check_sections();
+  Output_segment* last_load_segment = NULL;
+
   bool was_readonly = false;
   for (Segment_list::iterator p = this->segment_list_.begin();
        p != this->segment_list_.end();
@@ -1767,20 +1779,26 @@ Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
 
          uint64_t aligned_addr = 0;
          uint64_t abi_pagesize = target->abi_pagesize();
+         uint64_t common_pagesize = target->common_pagesize();
 
-         // FIXME: This should depend on the -n and -N options.
-         (*p)->set_minimum_p_align(target->common_pagesize());
+         if (!parameters->options().nmagic()
+             && !parameters->options().omagic())
+           (*p)->set_minimum_p_align(common_pagesize);
 
          if (are_addresses_set)
            {
-             // Adjust the file offset to the same address modulo the
-             // page size.
-             uint64_t unsigned_off = off;
-             uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1))
-                                     | (addr & (abi_pagesize - 1)));
-             if (aligned_off < unsigned_off)
-               aligned_off += abi_pagesize;
-             off = aligned_off;
+             if (!parameters->options().nmagic()
+                 && !parameters->options().omagic())
+               {
+                 // Adjust the file offset to the same address modulo
+                 // the page size.
+                 uint64_t unsigned_off = off;
+                 uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1))
+                                         | (addr & (abi_pagesize - 1)));
+                 if (aligned_off < unsigned_off)
+                   aligned_off += abi_pagesize;
+                 off = aligned_off;
+               }
            }
          else
            {
@@ -1815,7 +1833,6 @@ Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
 
          if (!are_addresses_set && aligned_addr != addr)
            {
-             uint64_t common_pagesize = target->common_pagesize();
              uint64_t first_off = (common_pagesize
                                    - (aligned_addr
                                       & (common_pagesize - 1)));
@@ -1839,6 +1856,25 @@ Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
 
          if (((*p)->flags() & elfcpp::PF_W) == 0)
            was_readonly = true;
+
+         // Implement --check-sections.  We know that the segments
+         // are sorted by LMA.
+         if (check_sections && last_load_segment != NULL)
+           {
+             gold_assert(last_load_segment->paddr() <= (*p)->paddr());
+             if (last_load_segment->paddr() + last_load_segment->memsz()
+                 > (*p)->paddr())
+               {
+                 unsigned long long lb1 = last_load_segment->paddr();
+                 unsigned long long le1 = lb1 + last_load_segment->memsz();
+                 unsigned long long lb2 = (*p)->paddr();
+                 unsigned long long le2 = lb2 + (*p)->memsz();
+                 gold_error(_("load segment overlap [0x%llx -> 0x%llx] and "
+                              "[0x%llx -> 0x%llx]"),
+                            lb1, le1, lb2, le2);
+               }
+           }
+         last_load_segment = *p;
        }
     }
 
@@ -2723,6 +2759,8 @@ Layout::finish_dynamic_section(const Input_objects* input_objects,
     }
   if (parameters->options().shared() && this->has_static_tls())
     flags |= elfcpp::DF_STATIC_TLS;
+  if (parameters->options().origin())
+    flags |= elfcpp::DF_ORIGIN;
   odyn->add_constant(elfcpp::DT_FLAGS, flags);
 
   flags = 0;
@@ -2744,6 +2782,8 @@ Layout::finish_dynamic_section(const Input_objects* input_objects,
     flags &= ~(elfcpp::DF_1_INITFIRST
               | elfcpp::DF_1_NODELETE
               | elfcpp::DF_1_NOOPEN);
+  if (parameters->options().origin())
+    flags |= elfcpp::DF_1_ORIGIN;
   if (flags)
     odyn->add_constant(elfcpp::DT_FLAGS_1, flags);
 }
@@ -2890,38 +2930,50 @@ Layout::output_section_name(const char* name, size_t* plen)
   return name;
 }
 
-// Record the signature of a comdat section, and return whether to
-// include it in the link.  If GROUP is true, this is a regular
-// section group.  If GROUP is false, this is a group signature
-// derived from the name of a linkonce section.  We want linkonce
-// signatures and group signatures to block each other, but we don't
-// want a linkonce signature to block another linkonce signature.
+// 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
+// returns false.  Otherwise, the CANDIDATE signature is recorded for
+// this NAME in the layout object, *KEPT_SECTION is set to the
+// internal copy and the function return false.  In some cases, with
+// CANDIDATE->GROUP_ being false, KEPT_SECTION can point back to
+// CANDIDATE.
 
 bool
-Layout::add_comdat(Relobj* object, unsigned int shndx,
-                   const std::string& signature, bool group)
+Layout::find_or_add_kept_section(const std::string name,
+                                 Kept_section* candidate,
+                                 Kept_section** kept_section)
 {
-  Kept_section kept(object, shndx, group);
   std::pair<Signatures::iterator, bool> ins(
-    this->signatures_.insert(std::make_pair(signature, kept)));
+    this->signatures_.insert(std::make_pair(name, *candidate)));
 
+  if (kept_section)
+    *kept_section = &ins.first->second;
   if (ins.second)
     {
       // This is the first time we've seen this signature.
       return true;
     }
 
-  if (ins.first->second.group_)
+  if (ins.first->second.is_group)
     {
       // We've already seen a real section group with this signature.
+      // If the kept group is from a plugin object, and we're in
+      // the replacement phase, accept the new one as a replacement.
+      if (ins.first->second.object == NULL
+          && parameters->options().plugins()->in_replacement_phase())
+        {
+          ins.first->second = *candidate;
+          return true;
+        }
       return false;
     }
-  else if (group)
+  else if (candidate->is_group)
     {
       // This is a real section group, and we've already seen a
       // linkonce section with this signature.  Record that we've seen
       // a section group, and don't include this section group.
-      ins.first->second.group_ = true;
+      ins.first->second.is_group = true;
       return false;
     }
   else
@@ -2929,6 +2981,7 @@ Layout::add_comdat(Relobj* object, unsigned int shndx,
       // We've already seen a linkonce section and this is a linkonce
       // section.  These don't block each other--this may be the same
       // symbol name with different section types.
+      *kept_section = candidate;
       return true;
     }
 }
@@ -2943,8 +2996,8 @@ Layout::find_kept_object(const std::string& signature,
   if (p == this->signatures_.end())
     return NULL;
   if (pshndx != NULL)
-    *pshndx = p->second.shndx_;
-  return p->second.object_;
+    *pshndx = p->second.shndx;
+  return p->second.object;
 }
 
 // Store the allocated sections into the section list.
@@ -2967,6 +3020,12 @@ Layout::make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
   gold_assert(!parameters->options().relocatable());
   Output_segment* oseg = new Output_segment(type, flags);
   this->segment_list_.push_back(oseg);
+
+  if (type == elfcpp::PT_TLS)
+    this->tls_segment_ = oseg;
+  else if (type == elfcpp::PT_GNU_RELRO)
+    this->relro_segment_ = oseg;
+
   return oseg;
 }
 
@@ -3047,7 +3106,7 @@ Layout::write_sections_after_input_sections(Output_file* of)
     {
       off_t off = this->output_file_size_;
       off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS);
-      
+
       // Now that we've finalized the names, we can finalize the shstrab.
       off =
        this->set_section_offsets(off,
@@ -3263,8 +3322,8 @@ Write_symbols_task::locks(Task_locker* tl)
 void
 Write_symbols_task::run(Workqueue*)
 {
-  this->symtab_->write_globals(this->input_objects_, this->sympool_,
-                              this->dynpool_, this->layout_->symtab_xindex(),
+  this->symtab_->write_globals(this->sympool_, this->dynpool_,
+                              this->layout_->symtab_xindex(),
                               this->layout_->dynsym_xindex(), this->of_);
 }
 
This page took 0.028568 seconds and 4 git commands to generate.