gas/
[deliverable/binutils-gdb.git] / gold / layout.cc
index 0de42c2f758ab34d5f35e0b73f99ef1718de76aa..83c8c6a025587200936bb66e321463b006ba194c 100644 (file)
@@ -1,5 +1,25 @@
 // layout.cc -- lay out output file sections for gold
 
+// Copyright 2006, 2007 Free Software Foundation, Inc.
+// Written by Ian Lance Taylor <iant@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.
+
 #include "gold.h"
 
 #include <cstring>
@@ -7,9 +27,11 @@
 #include <iostream>
 #include <utility>
 
+#include "parameters.h"
 #include "output.h"
 #include "symtab.h"
 #include "dynobj.h"
+#include "ehframe.h"
 #include "layout.h"
 
 namespace gold
@@ -28,7 +50,8 @@ Layout_task_runner::run(Workqueue* workqueue)
 
   // Now we know the final size of the output file and we know where
   // each piece of information goes.
-  Output_file* of = new Output_file(this->options_);
+  Output_file* of = new Output_file(this->options_,
+                                    this->input_objects_->target());
   of->open(file_size);
 
   // Queue up the final set of tasks.
@@ -42,7 +65,9 @@ Layout::Layout(const General_options& options)
   : options_(options), namepool_(), sympool_(), dynpool_(), signatures_(),
     section_name_map_(), segment_list_(), section_list_(),
     unattached_section_list_(), special_output_list_(),
-    tls_segment_(NULL), symtab_section_(NULL), dynsym_section_(NULL)
+    tls_segment_(NULL), symtab_section_(NULL),
+    dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL),
+    eh_frame_section_(NULL)
 {
   // Make space for more than enough segments for a typical file.
   // This is just for efficiency--it's OK if we wind up needing more.
@@ -84,7 +109,7 @@ Layout::include_section(Object*, const char*,
     case elfcpp::SHT_RELA:
     case elfcpp::SHT_REL:
     case elfcpp::SHT_GROUP:
-      return this->options_.is_relocatable();
+      return parameters->output_is_object();
 
     default:
       // FIXME: Handle stripping debug sections here.
@@ -132,7 +157,9 @@ Layout::get_output_section(const char* name, Stringpool::Key name_key,
   // We should ignore some flags.
   flags &= ~ (elfcpp::SHF_INFO_LINK
              | elfcpp::SHF_LINK_ORDER
-             | elfcpp::SHF_GROUP);
+             | elfcpp::SHF_GROUP
+             | elfcpp::SHF_MERGE
+             | elfcpp::SHF_STRINGS);
 
   const Key key(name_key, std::make_pair(type, flags));
   const std::pair<Key, Output_section*> v(key, NULL);
@@ -166,7 +193,7 @@ Layout::layout(Relobj* object, unsigned int shndx, const char* name,
   // If we are not doing a relocateable link, choose the name to use
   // for the output section.
   size_t len = strlen(name);
-  if (!this->options_.is_relocatable())
+  if (!parameters->output_is_object())
     name = Layout::output_section_name(name, &len);
 
   // FIXME: Handle SHF_OS_NONCONFORMING here.
@@ -181,6 +208,17 @@ Layout::layout(Relobj* object, unsigned int shndx, const char* name,
                                                shdr.get_sh_type(),
                                                shdr.get_sh_flags());
 
+  // Special GNU handling of sections named .eh_frame.
+  if (!parameters->output_is_object()
+      && strcmp(name, ".eh_frame") == 0
+      && shdr.get_sh_size() > 0
+      && shdr.get_sh_type() == elfcpp::SHT_PROGBITS
+      && shdr.get_sh_flags() == elfcpp::SHF_ALLOC)
+    {
+      this->layout_eh_frame(object, shndx, name, shdr, os, off);
+      return os;
+    }
+
   // FIXME: Handle SHF_LINK_ORDER somewhere.
 
   *off = os->add_input_section(object, shndx, name, shdr);
@@ -188,6 +226,46 @@ Layout::layout(Relobj* object, unsigned int shndx, const char* name,
   return os;
 }
 
+// Special GNU handling of sections named .eh_frame.  They will
+// normally hold exception frame data.
+
+template<int size, bool big_endian>
+void
+Layout::layout_eh_frame(Relobj* object,
+                       unsigned int shndx,
+                       const char* name,
+                       const elfcpp::Shdr<size, big_endian>& shdr,
+                       Output_section* os, off_t* off)
+{
+  if (this->eh_frame_section_ == NULL)
+    {
+      this->eh_frame_section_ = os;
+
+      if (this->options_.create_eh_frame_hdr())
+       {
+         Stringpool::Key hdr_name_key;
+         const char* hdr_name = this->namepool_.add(".eh_frame_hdr",
+                                                    &hdr_name_key);
+         Output_section* hdr_os =
+           this->get_output_section(hdr_name, hdr_name_key,
+                                    elfcpp::SHT_PROGBITS,
+                                    elfcpp::SHF_ALLOC);
+
+         Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os);
+         hdr_os->add_output_section_data(hdr_posd);
+
+         Output_segment* hdr_oseg =
+           new Output_segment(elfcpp::PT_GNU_EH_FRAME, elfcpp::PF_R);
+         this->segment_list_.push_back(hdr_oseg);
+         hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R);
+       }
+    }
+
+  gold_assert(this->eh_frame_section_ == os);
+
+  *off = os->add_input_section(object, shndx, name, shdr);
+}
+
 // Add POSD to an output section using NAME, TYPE, and FLAGS.
 
 void
@@ -223,7 +301,7 @@ Output_section*
 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
                            elfcpp::Elf_Xword flags)
 {
-  Output_section* os = new Output_section(name, type, flags, true);
+  Output_section* os = new Output_section(name, type, flags);
   this->section_list_.push_back(os);
 
   if ((flags & elfcpp::SHF_ALLOC) == 0)
@@ -321,10 +399,66 @@ Layout::create_initial_dynamic_sections(const Input_objects* input_objects,
                                                     (elfcpp::SHF_ALLOC
                                                      | elfcpp::SHF_WRITE));
 
-  symtab->define_in_output_data(input_objects->target(), "_DYNAMIC",
+  symtab->define_in_output_data(input_objects->target(), "_DYNAMIC", NULL,
                                this->dynamic_section_, 0, 0,
                                elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
                                elfcpp::STV_HIDDEN, 0, false, false);
+
+  this->dynamic_data_ =  new Output_data_dynamic(&this->dynpool_);
+
+  this->dynamic_section_->add_output_section_data(this->dynamic_data_);
+}
+
+// For each output section whose name can be represented as C symbol,
+// define __start and __stop symbols for the section.  This is a GNU
+// extension.
+
+void
+Layout::define_section_symbols(Symbol_table* symtab, const Target* target)
+{
+  for (Section_list::const_iterator p = this->section_list_.begin();
+       p != this->section_list_.end();
+       ++p)
+    {
+      const char* const name = (*p)->name();
+      if (name[strspn(name,
+                     ("0123456789"
+                      "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
+                      "abcdefghijklmnopqrstuvwxyz"
+                      "_"))]
+         == '\0')
+       {
+         const std::string name_string(name);
+         const std::string start_name("__start_" + name_string);
+         const std::string stop_name("__stop_" + name_string);
+
+         symtab->define_in_output_data(target,
+                                       start_name.c_str(),
+                                       NULL, // version
+                                       *p,
+                                       0, // value
+                                       0, // symsize
+                                       elfcpp::STT_NOTYPE,
+                                       elfcpp::STB_GLOBAL,
+                                       elfcpp::STV_DEFAULT,
+                                       0, // nonvis
+                                       false, // offset_is_from_end
+                                       false); // only_if_ref
+
+         symtab->define_in_output_data(target,
+                                       stop_name.c_str(),
+                                       NULL, // version
+                                       *p,
+                                       0, // value
+                                       0, // symsize
+                                       elfcpp::STT_NOTYPE,
+                                       elfcpp::STB_GLOBAL,
+                                       elfcpp::STV_DEFAULT,
+                                       0, // nonvis
+                                       true, // offset_is_from_end
+                                       false); // only_if_ref
+       }
+    }
 }
 
 // Find the first read-only PT_LOAD segment, creating one if
@@ -384,7 +518,6 @@ off_t
 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
 {
   Target* const target = input_objects->target();
-  const int size = target->get_size();
 
   target->finalize_sections(this);
 
@@ -399,14 +532,14 @@ Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
       phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
       this->segment_list_.push_back(phdr_seg);
 
-      // This holds the dynamic tags.
-      Output_data_dynamic* odyn;
-      odyn = new Output_data_dynamic(input_objects->target(),
-                                    &this->dynpool_);
-
-      // Create the dynamic symbol table, including the hash table,
-      // the dynamic relocations, and the version sections.
-      this->create_dynamic_symtab(target, odyn, symtab);
+      // Create the dynamic symbol table, including the hash table.
+      Output_section* dynstr;
+      std::vector<Symbol*> dynamic_symbols;
+      unsigned int local_dynamic_count;
+      Versions versions;
+      this->create_dynamic_symtab(target, symtab, &dynstr,
+                                 &local_dynamic_count, &dynamic_symbols,
+                                 &versions);
 
       // Create the .interp section to hold the name of the
       // interpreter, and put it in a PT_INTERP segment.
@@ -414,7 +547,16 @@ Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
 
       // Finish the .dynamic section to hold the dynamic data, and put
       // it in a PT_DYNAMIC segment.
-      this->finish_dynamic_section(input_objects, symtab, odyn);
+      this->finish_dynamic_section(input_objects, symtab);
+
+      // We should have added everything we need to the dynamic string
+      // table.
+      this->dynpool_.set_string_offsets();
+
+      // Create the version sections.  We can't do this until the
+      // dynamic string table is complete.
+      this->create_version_sections(&versions, local_dynamic_count,
+                                   dynamic_symbols, dynstr);
     }
 
   // FIXME: Handle PT_GNU_STACK.
@@ -422,10 +564,8 @@ Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
   Output_segment* load_seg = this->find_first_load_seg();
 
   // Lay out the segment headers.
-  bool big_endian = target->is_big_endian();
   Output_segment_headers* segment_headers;
-  segment_headers = new Output_segment_headers(size, big_endian,
-                                              this->segment_list_);
+  segment_headers = new Output_segment_headers(this->segment_list_);
   load_seg->add_initial_output_data(segment_headers);
   this->special_output_list_.push_back(segment_headers);
   if (phdr_seg != NULL)
@@ -433,12 +573,7 @@ Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
 
   // Lay out the file header.
   Output_file_header* file_header;
-  file_header = new Output_file_header(size,
-                                      big_endian,
-                                      this->options_,
-                                      target,
-                                      symtab,
-                                      segment_headers);
+  file_header = new Output_file_header(target, symtab, segment_headers);
   load_seg->add_initial_output_data(file_header);
   this->special_output_list_.push_back(file_header);
 
@@ -451,10 +586,7 @@ Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
   off_t off = this->set_segment_offsets(target, load_seg, &shndx);
 
   // Create the symbol table sections.
-  // FIXME: We don't need to do this if we are stripping symbols.
-  Output_section* ostrtab;
-  this->create_symtab_sections(size, input_objects, symtab, &off,
-                              &ostrtab);
+  this->create_symtab_sections(input_objects, symtab, &off);
 
   // Create the .shstrtab section.
   Output_section* shstrtab_section = this->create_shstrtab();
@@ -463,11 +595,8 @@ Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
   // segments.
   off = this->set_section_offsets(off, &shndx);
 
-  // Now the section index of OSTRTAB is set.
-  this->symtab_section_->set_link(ostrtab->out_shndx());
-
   // Create the section table header.
-  Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
+  Output_section_headers* oshdrs = this->create_shdrs(&off);
 
   file_header->set_section_info(oshdrs, shstrtab_section);
 
@@ -599,6 +728,10 @@ Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
 
          uint64_t aligned_addr = addr;
          uint64_t abi_pagesize = target->abi_pagesize();
+
+          // FIXME: This should depend on the -n and -N options.
+          (*p)->set_minimum_addralign(target->common_pagesize());
+
          if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
            {
              uint64_t align = (*p)->addralign();
@@ -680,22 +813,23 @@ Layout::set_section_offsets(off_t off, unsigned int* pshndx)
   return off;
 }
 
-// Create the symbol table sections.
+// Create the symbol table sections.  Here we also set the final
+// values of the symbols.  At this point all the loadable sections are
+// fully laid out.
 
 void
-Layout::create_symtab_sections(int size, const Input_objects* input_objects,
+Layout::create_symtab_sections(const Input_objects* input_objects,
                               Symbol_table* symtab,
-                              off_t* poff,
-                              Output_section** postrtab)
+                              off_t* poff)
 {
   int symsize;
   unsigned int align;
-  if (size == 32)
+  if (parameters->get_size() == 32)
     {
       symsize = elfcpp::Elf_sizes<32>::sym_size;
       align = 4;
     }
-  else if (size == 64)
+  else if (parameters->get_size() == 64)
     {
       symsize = elfcpp::Elf_sizes<64>::sym_size;
       align = 8;
@@ -742,7 +876,27 @@ Layout::create_symtab_sections(int size, const Input_objects* input_objects,
   unsigned int local_symcount = local_symbol_index;
   gold_assert(local_symcount * symsize == off - startoff);
 
-  off = symtab->finalize(local_symcount, off, &this->sympool_);
+  off_t dynoff;
+  size_t dyn_global_index;
+  size_t dyncount;
+  if (this->dynsym_section_ == NULL)
+    {
+      dynoff = 0;
+      dyn_global_index = 0;
+      dyncount = 0;
+    }
+  else
+    {
+      dyn_global_index = this->dynsym_section_->info();
+      off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
+      dynoff = this->dynsym_section_->offset() + locsize;
+      dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
+      gold_assert(dyncount * symsize
+                 == this->dynsym_section_->data_size() - locsize);
+    }
+
+  off = symtab->finalize(local_symcount, off, dynoff, dyn_global_index,
+                        dyncount, &this->sympool_);
 
   this->sympool_.set_string_offsets();
 
@@ -765,11 +919,11 @@ Layout::create_symtab_sections(int size, const Input_objects* input_objects,
   ostrtab->add_output_section_data(pstr);
 
   osymtab->set_address(0, startoff);
+  osymtab->set_link_section(ostrtab);
   osymtab->set_info(local_symcount);
   osymtab->set_entsize(symsize);
 
   *poff = off;
-  *postrtab = ostrtab;
 }
 
 // Create the .shstrtab section, which holds the names of the
@@ -798,11 +952,12 @@ Layout::create_shstrtab()
 // offset.
 
 Output_section_headers*
-Layout::create_shdrs(int size, bool big_endian, off_t* poff)
+Layout::create_shdrs(off_t* poff)
 {
   Output_section_headers* oshdrs;
-  oshdrs = new Output_section_headers(size, big_endian, this->segment_list_,
-                                     this->unattached_section_list_,
+  oshdrs = new Output_section_headers(this,
+                                     &this->segment_list_,
+                                     &this->unattached_section_list_,
                                      &this->namepool_);
   off_t off = align_address(*poff, oshdrs->addralign());
   oshdrs->set_address(0, off);
@@ -815,8 +970,11 @@ Layout::create_shdrs(int size, bool big_endian, off_t* poff)
 // Create the dynamic symbol table.
 
 void
-Layout::create_dynamic_symtab(const Target* target, Output_data_dynamic* odyn,
-                             Symbol_table* symtab)
+Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab,
+                             Output_section **pdynstr,
+                             unsigned int* plocal_dynamic_count,
+                             std::vector<Symbol*>* pdynamic_symbols,
+                             Versions* pversions)
 {
   // Count all the symbols in the dynamic symbol table, and set the
   // dynamic symbol indexes.
@@ -844,17 +1002,17 @@ Layout::create_dynamic_symtab(const Target* target, Output_data_dynamic* odyn,
   // this->dynpool_.
 
   unsigned int local_symcount = index;
-
-  std::vector<Symbol*> dynamic_symbols;
+  *plocal_dynamic_count = local_symcount;
 
   // FIXME: We have to tell set_dynsym_indexes whether the
   // -E/--export-dynamic option was used.
-  index = symtab->set_dynsym_indexes(index, &dynamic_symbols,
-                                    &this->dynpool_);
+  index = symtab->set_dynsym_indexes(&this->options_, target, index,
+                                    pdynamic_symbols, &this->dynpool_,
+                                    pversions);
 
   int symsize;
   unsigned int align;
-  const int size = target->get_size();
+  const int size = parameters->get_size();
   if (size == 32)
     {
       symsize = elfcpp::Elf_sizes<32>::sym_size;
@@ -868,6 +1026,8 @@ Layout::create_dynamic_symtab(const Target* target, Output_data_dynamic* odyn,
   else
     gold_unreachable();
 
+  // Create the dynamic symbol table section.
+
   const char* dynsym_name = this->namepool_.add(".dynsym", NULL);
   Output_section* dynsym = this->make_output_section(dynsym_name,
                                                     elfcpp::SHT_DYNSYM,
@@ -883,9 +1043,12 @@ Layout::create_dynamic_symtab(const Target* target, Output_data_dynamic* odyn,
 
   this->dynsym_section_ = dynsym;
 
+  Output_data_dynamic* const odyn = this->dynamic_data_;
   odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
   odyn->add_constant(elfcpp::DT_SYMENT, symsize);
 
+  // Create the dynamic string table section.
+
   const char* dynstr_name = this->namepool_.add(".dynstr", NULL);
   Output_section* dynstr = this->make_output_section(dynstr_name,
                                                     elfcpp::SHT_STRTAB,
@@ -894,14 +1057,21 @@ Layout::create_dynamic_symtab(const Target* target, Output_data_dynamic* odyn,
   Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
   dynstr->add_output_section_data(strdata);
 
+  dynsym->set_link_section(dynstr);
+  this->dynamic_section_->set_link_section(dynstr);
+
   odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
   odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
 
+  *pdynstr = dynstr;
+
+  // Create the hash tables.
+
   // FIXME: We need an option to create a GNU hash table.
 
   unsigned char* phash;
   unsigned int hashlen;
-  Dynobj::create_elf_hash_table(target, dynamic_symbols, local_symcount,
+  Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
                                &phash, &hashlen);
 
   const char* hash_name = this->namepool_.add(".hash", NULL);
@@ -914,12 +1084,161 @@ Layout::create_dynamic_symtab(const Target* target, Output_data_dynamic* odyn,
                                                               align);
   hashsec->add_output_section_data(hashdata);
 
+  hashsec->set_link_section(dynsym);
   hashsec->set_entsize(4);
-  // FIXME: .hash should link to .dynsym.
 
   odyn->add_section_address(elfcpp::DT_HASH, hashsec);
 }
 
+// Create the version sections.
+
+void
+Layout::create_version_sections(const Versions* versions,
+                               unsigned int local_symcount,
+                               const std::vector<Symbol*>& dynamic_symbols,
+                               const Output_section* dynstr)
+{
+  if (!versions->any_defs() && !versions->any_needs())
+    return;
+
+  if (parameters->get_size() == 32)
+    {
+      if (parameters->is_big_endian())
+        {
+#ifdef HAVE_TARGET_32_BIG
+          this->sized_create_version_sections
+              SELECT_SIZE_ENDIAN_NAME(32, true)(
+                  versions, local_symcount, dynamic_symbols, dynstr
+                  SELECT_SIZE_ENDIAN(32, true));
+#else
+          gold_unreachable();
+#endif
+        }
+      else
+        {
+#ifdef HAVE_TARGET_32_LITTLE
+          this->sized_create_version_sections
+              SELECT_SIZE_ENDIAN_NAME(32, false)(
+                  versions, local_symcount, dynamic_symbols, dynstr
+                  SELECT_SIZE_ENDIAN(32, false));
+#else
+          gold_unreachable();
+#endif
+        }
+    }
+  else if (parameters->get_size() == 64)
+    {
+      if (parameters->is_big_endian())
+        {
+#ifdef HAVE_TARGET_64_BIG
+          this->sized_create_version_sections
+              SELECT_SIZE_ENDIAN_NAME(64, true)(
+                  versions, local_symcount, dynamic_symbols, dynstr
+                  SELECT_SIZE_ENDIAN(64, true));
+#else
+          gold_unreachable();
+#endif
+        }
+      else
+        {
+#ifdef HAVE_TARGET_64_LITTLE
+          this->sized_create_version_sections
+              SELECT_SIZE_ENDIAN_NAME(64, false)(
+                  versions, local_symcount, dynamic_symbols, dynstr
+                  SELECT_SIZE_ENDIAN(64, false));
+#else
+          gold_unreachable();
+#endif
+        }
+    }
+  else
+    gold_unreachable();
+}
+
+// Create the version sections, sized version.
+
+template<int size, bool big_endian>
+void
+Layout::sized_create_version_sections(
+    const Versions* versions,
+    unsigned int local_symcount,
+    const std::vector<Symbol*>& dynamic_symbols,
+    const Output_section* dynstr
+    ACCEPT_SIZE_ENDIAN)
+{
+  const char* vname = this->namepool_.add(".gnu.version", NULL);
+  Output_section* vsec = this->make_output_section(vname,
+                                                  elfcpp::SHT_GNU_versym,
+                                                  elfcpp::SHF_ALLOC);
+
+  unsigned char* vbuf;
+  unsigned int vsize;
+  versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
+      &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize
+      SELECT_SIZE_ENDIAN(size, big_endian));
+
+  Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2);
+
+  vsec->add_output_section_data(vdata);
+  vsec->set_entsize(2);
+  vsec->set_link_section(this->dynsym_section_);
+
+  Output_data_dynamic* const odyn = this->dynamic_data_;
+  odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
+
+  if (versions->any_defs())
+    {
+      const char* vdname = this->namepool_.add(".gnu.version_d", NULL);
+      Output_section *vdsec;
+      vdsec = this->make_output_section(vdname, elfcpp::SHT_GNU_verdef,
+                                       elfcpp::SHF_ALLOC);
+
+      unsigned char* vdbuf;
+      unsigned int vdsize;
+      unsigned int vdentries;
+      versions->def_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
+          &this->dynpool_, &vdbuf, &vdsize, &vdentries
+          SELECT_SIZE_ENDIAN(size, big_endian));
+
+      Output_section_data* vddata = new Output_data_const_buffer(vdbuf,
+                                                                vdsize,
+                                                                4);
+
+      vdsec->add_output_section_data(vddata);
+      vdsec->set_link_section(dynstr);
+      vdsec->set_info(vdentries);
+
+      odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
+      odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
+    }
+
+  if (versions->any_needs())
+    {
+      const char* vnname = this->namepool_.add(".gnu.version_r", NULL);
+      Output_section* vnsec;
+      vnsec = this->make_output_section(vnname, elfcpp::SHT_GNU_verneed,
+                                       elfcpp::SHF_ALLOC);
+
+      unsigned char* vnbuf;
+      unsigned int vnsize;
+      unsigned int vnentries;
+      versions->need_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)
+        (&this->dynpool_, &vnbuf, &vnsize, &vnentries
+         SELECT_SIZE_ENDIAN(size, big_endian));
+
+      Output_section_data* vndata = new Output_data_const_buffer(vnbuf,
+                                                                vnsize,
+                                                                4);
+
+      vnsec->add_output_section_data(vndata);
+      vnsec->set_link_section(dynstr);
+      vnsec->set_info(vnentries);
+
+      odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
+      odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
+    }
+}
+
 // Create the .interp section and PT_INTERP segment.
 
 void
@@ -951,17 +1270,16 @@ Layout::create_interp(const Target* target)
 
 void
 Layout::finish_dynamic_section(const Input_objects* input_objects,
-                              const Symbol_table* symtab,
-                              Output_data_dynamic* odyn)
+                              const Symbol_table* symtab)
 {
-  this->dynamic_section_->add_output_section_data(odyn);
-
   Output_segment* oseg = new Output_segment(elfcpp::PT_DYNAMIC,
                                            elfcpp::PF_R | elfcpp::PF_W);
   this->segment_list_.push_back(oseg);
   oseg->add_initial_output_section(this->dynamic_section_,
                                   elfcpp::PF_R | elfcpp::PF_W);
 
+  Output_data_dynamic* const odyn = this->dynamic_data_;
+
   for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
        p != input_objects->dynobj_end();
        ++p)
@@ -972,14 +1290,43 @@ Layout::finish_dynamic_section(const Input_objects* input_objects,
 
   // FIXME: Support --init and --fini.
   Symbol* sym = symtab->lookup("_init");
-  if (sym != NULL && sym->is_defined() && !sym->is_defined_in_dynobj())
+  if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
     odyn->add_symbol(elfcpp::DT_INIT, sym);
 
   sym = symtab->lookup("_fini");
-  if (sym != NULL && sym->is_defined() && !sym->is_defined_in_dynobj())
+  if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
     odyn->add_symbol(elfcpp::DT_FINI, sym);
 
   // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
+
+  // Add a DT_RPATH entry if needed.
+  const General_options::Dir_list& rpath(this->options_.rpath());
+  if (!rpath.empty())
+    {
+      std::string rpath_val;
+      for (General_options::Dir_list::const_iterator p = rpath.begin();
+           p != rpath.end();
+           ++p)
+        {
+          if (rpath_val.empty())
+            rpath_val = *p;
+          else
+            {
+              // Eliminate duplicates.
+              General_options::Dir_list::const_iterator q;
+              for (q = rpath.begin(); q != p; ++q)
+                if (strcmp(*q, *p) == 0)
+                  break;
+              if (q == p)
+                {
+                  rpath_val += ':';
+                  rpath_val += *p;
+                }
+            }
+        }
+
+      odyn->add_string(elfcpp::DT_RPATH, rpath_val);
+    }
 }
 
 // The mapping of .gnu.linkonce section names to real section names.
@@ -1114,7 +1461,7 @@ Layout::add_comdat(const char* signature, bool group)
   else if (group)
     {
       // This is a real section group, and we've already seen a
-      // linkonce section with tihs signature.  Record that we've seen
+      // linkonce section with this signature.  Record that we've seen
       // a section group, and don't include this section group.
       ins.first->second = true;
       return false;
@@ -1131,8 +1478,7 @@ Layout::add_comdat(const char* signature, bool group)
 // Write out data not associated with a section or the symbol table.
 
 void
-Layout::write_data(const Symbol_table* symtab, const Target* target,
-                  Output_file* of) const
+Layout::write_data(const Symbol_table* symtab, Output_file* of) const
 {
   const Output_section* symtab_section = this->symtab_section_;
   for (Section_list::const_iterator p = this->section_list_.begin();
@@ -1146,7 +1492,7 @@ Layout::write_data(const Symbol_table* symtab, const Target* target,
          gold_assert(index > 0 && index != -1U);
          off_t off = (symtab_section->offset()
                       + index * symtab_section->entsize());
-         symtab->write_section_symbol(target, *p, of, off);
+         symtab->write_section_symbol(*p, of, off);
        }
     }
 
@@ -1162,7 +1508,7 @@ Layout::write_data(const Symbol_table* symtab, const Target* target,
          gold_assert(index > 0 && index != -1U);
          off_t off = (dynsym_section->offset()
                       + index * dynsym_section->entsize());
-         symtab->write_section_symbol(target, *p, of, off);
+         symtab->write_section_symbol(*p, of, off);
        }
     }
 
@@ -1205,7 +1551,7 @@ Write_data_task::locks(Workqueue* workqueue)
 void
 Write_data_task::run(Workqueue*)
 {
-  this->layout_->write_data(this->symtab_, this->target_, this->of_);
+  this->layout_->write_data(this->symtab_, this->of_);
 }
 
 // Write_symbols_task methods.
@@ -1231,7 +1577,8 @@ Write_symbols_task::locks(Workqueue* workqueue)
 void
 Write_symbols_task::run(Workqueue*)
 {
-  this->symtab_->write_globals(this->target_, this->sympool_, this->of_);
+  this->symtab_->write_globals(this->target_, this->sympool_, this->dynpool_,
+                              this->of_);
 }
 
 // Close_task_runner methods.
@@ -1247,25 +1594,33 @@ Close_task_runner::run(Workqueue*)
 // Instantiate the templates we need.  We could use the configure
 // script to restrict this to only the ones for implemented targets.
 
+#ifdef HAVE_TARGET_32_LITTLE
 template
 Output_section*
 Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name,
                          const elfcpp::Shdr<32, false>& shdr, off_t*);
+#endif
 
+#ifdef HAVE_TARGET_32_BIG
 template
 Output_section*
 Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name,
                         const elfcpp::Shdr<32, true>& shdr, off_t*);
+#endif
 
+#ifdef HAVE_TARGET_64_LITTLE
 template
 Output_section*
 Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name,
                          const elfcpp::Shdr<64, false>& shdr, off_t*);
+#endif
 
+#ifdef HAVE_TARGET_64_BIG
 template
 Output_section*
 Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name,
                         const elfcpp::Shdr<64, true>& shdr, off_t*);
+#endif
 
 
 } // End namespace gold.
This page took 0.033322 seconds and 4 git commands to generate.