* mapfile.cc: New file.
[deliverable/binutils-gdb.git] / gold / common.cc
index 7ba8adc2697d68987e146b50aeb1df5f303b681f..632a20b6c9906469f516cc1d595bf795a3e38759 100644 (file)
@@ -1,10 +1,31 @@
 // common.cc -- handle common symbols for gold
 
+// Copyright 2006, 2007, 2008 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 <algorithm>
 
 #include "workqueue.h"
+#include "mapfile.h"
 #include "layout.h"
 #include "output.h"
 #include "symtab.h"
@@ -18,35 +39,21 @@ namespace gold
 // This task allocates the common symbols.  We need a lock on the
 // symbol table.
 
-Task::Is_runnable_type
-Allocate_commons_task::is_runnable(Workqueue*)
+Task_token*
+Allocate_commons_task::is_runnable()
 {
   if (!this->symtab_lock_->is_writable())
-    return IS_LOCKED;
-  return IS_RUNNABLE;
+    return this->symtab_lock_;
+  return NULL;
 }
 
 // Return the locks we hold: one on the symbol table, and one blocker.
 
-class Allocate_commons_task::Allocate_commons_locker : public Task_locker
-{
- public:
-  Allocate_commons_locker(Task_token& symtab_lock, Task* task,
-                         Task_token& blocker, Workqueue* workqueue)
-    : symtab_locker_(symtab_lock, task),
-      blocker_(blocker, workqueue)
-  { }
-
- private:
-  Task_locker_write symtab_locker_;
-  Task_locker_block blocker_;
-};
-
-Task_locker*
-Allocate_commons_task::locks(Workqueue* workqueue)
+void
+Allocate_commons_task::locks(Task_locker* tl)
 {
-  return new Allocate_commons_locker(*this->symtab_lock_, this,
-                                    *this->blocker_, workqueue);
+  tl->add(this, this->blocker_);
+  tl->add(this, this->symtab_lock_);
 }
 
 // Allocate the common symbols.
@@ -54,7 +61,7 @@ Allocate_commons_task::locks(Workqueue* workqueue)
 void
 Allocate_commons_task::run(Workqueue*)
 {
-  this->symtab_->allocate_commons(this->options_, this->layout_);
+  this->symtab_->allocate_commons(this->layout_, this->mapfile_);
 }
 
 // This class is used to sort the common symbol by size.  We put the
@@ -84,26 +91,24 @@ Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
     return true;
 
   const Symbol_table* symtab = this->symtab_;
-  const Sized_symbol<size>* psa;
-  psa = symtab->get_sized_symbol SELECT_SIZE_NAME(size) (pa
-                                                         SELECT_SIZE(size));
-  const Sized_symbol<size>* psb;
-  psb = symtab->get_sized_symbol SELECT_SIZE_NAME(size) (pb
-                                                         SELECT_SIZE(size));
+  const Sized_symbol<size>* psa = symtab->get_sized_symbol<size>(pa);
+  const Sized_symbol<size>* psb = symtab->get_sized_symbol<size>(pb);
 
+  // Sort by largest size first.
   typename Sized_symbol<size>::Size_type sa = psa->symsize();
   typename Sized_symbol<size>::Size_type sb = psb->symsize();
   if (sa < sb)
     return false;
-  else if (sb > sa)
+  else if (sb < sa)
     return true;
 
-  // When the symbols are the same size, we sort them by alignment.
+  // When the symbols are the same size, we sort them by alignment,
+  // largest alignment first.
   typename Sized_symbol<size>::Value_type va = psa->value();
   typename Sized_symbol<size>::Value_type vb = psb->value();
   if (va < vb)
     return false;
-  else if (vb > va)
+  else if (vb < va)
     return true;
 
   // Otherwise we stabilize the sort by sorting by name.
@@ -113,22 +118,48 @@ Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
 // Allocate the common symbols.
 
 void
-Symbol_table::allocate_commons(const General_options& options, Layout* layout)
+Symbol_table::allocate_commons(Layout* layout, Mapfile* mapfile)
 {
-  if (this->get_size() == 32)
-    this->do_allocate_commons<32>(options, layout);
-  else if (this->get_size() == 64)
-    this->do_allocate_commons<64>(options, layout);
+  if (parameters->target().get_size() == 32)
+    {
+#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
+      this->do_allocate_commons<32>(layout, mapfile);
+#else
+      gold_unreachable();
+#endif
+    }
+  else if (parameters->target().get_size() == 64)
+    {
+#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
+      this->do_allocate_commons<64>(layout, mapfile);
+#else
+      gold_unreachable();
+#endif
+    }
   else
-    abort();
+    gold_unreachable();
 }
 
 // Allocated the common symbols, sized version.
 
 template<int size>
 void
-Symbol_table::do_allocate_commons(const General_options&,
-                                 Layout* layout)
+Symbol_table::do_allocate_commons(Layout* layout, Mapfile* mapfile)
+{
+  this->do_allocate_commons_list<size>(layout, false, &this->commons_,
+                                      mapfile);
+  this->do_allocate_commons_list<size>(layout, true, &this->tls_commons_,
+                                      mapfile);
+}
+
+// Allocate the common symbols in a list.  IS_TLS indicates whether
+// these are TLS common symbols.
+
+template<int size>
+void
+Symbol_table::do_allocate_commons_list(Layout* layout, bool is_tls,
+                                      Commons_type* commons,
+                                      Mapfile* mapfile)
 {
   typedef typename Sized_symbol<size>::Value_type Value_type;
   typedef typename Sized_symbol<size>::Size_type Size_type;
@@ -138,8 +169,8 @@ Symbol_table::do_allocate_commons(const General_options&,
   // forwarder.  First remove all non-common symbols.
   bool any = false;
   uint64_t addralign = 0;
-  for (Commons_type::iterator p = this->commons_.begin();
-       p != this->commons_.end();
+  for (Commons_type::iterator p = commons->begin();
+       p != commons->end();
        ++p)
     {
       Symbol* sym = *p;
@@ -153,10 +184,7 @@ Symbol_table::do_allocate_commons(const General_options&,
       else
        {
          any = true;
-         Sized_symbol<size>* ssym;
-         ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) (
-              sym
-              SELECT_SIZE(size));
+         Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
          if (ssym->value() > addralign)
            addralign = ssym->value();
        }
@@ -166,45 +194,51 @@ Symbol_table::do_allocate_commons(const General_options&,
 
   // Sort the common symbols by size, so that they pack better into
   // memory.
-  std::sort(this->commons_.begin(), this->commons_.end(),
+  std::sort(commons->begin(), commons->end(),
            Sort_commons<size>(this));
 
-  // Place them in a newly allocated .bss section.
+  // Place them in a newly allocated BSS section.
 
-  Output_data_common *poc = new Output_data_common(addralign);
+  Output_data_space *poc = new Output_data_space(addralign,
+                                                (is_tls
+                                                 ? "** tls common"
+                                                 : "** common"));
 
-  layout->add_output_section_data(".bss", elfcpp::SHT_NOBITS,
-                                 elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC,
-                                 poc);
+  const char* name = ".bss";
+  elfcpp::Elf_Xword flags = elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC;
+  if (is_tls)
+    {
+      name = ".tbss";
+      flags |= elfcpp::SHF_TLS;
+    }
+  layout->add_output_section_data(name, elfcpp::SHT_NOBITS, flags, poc);
 
   // Allocate them all.
 
   off_t off = 0;
-  for (Commons_type::iterator p = this->commons_.begin();
-       p != this->commons_.end();
+  for (Commons_type::iterator p = commons->begin();
+       p != commons->end();
        ++p)
     {
       Symbol* sym = *p;
       if (sym == NULL)
        break;
+      Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
 
-      Sized_symbol<size>* ssym;
-      ssym = this->get_sized_symbol SELECT_SIZE_NAME(size) (sym
-                                                            SELECT_SIZE(size));
+      // Record the symbol in the map file now, before we change its
+      // value.  Pass the size in separately so that we don't have to
+      // templatize the map code, which is not performance sensitive.
+      if (mapfile != NULL)
+       mapfile->report_allocate_common(sym, ssym->symsize());
 
       off = align_address(off, ssym->value());
-
-      Size_type symsize = ssym->symsize();
-      ssym->init(ssym->name(), poc, off, symsize, ssym->type(),
-                ssym->binding(), ssym->visibility(), ssym->nonvis(),
-                false);
-
-      off += symsize;
+      ssym->allocate_common(poc, off);
+      off += ssym->symsize();
     }
 
-  poc->set_common_size(off);
+  poc->set_current_data_size(off);
 
-  this->commons_.clear();
+  commons->clear();
 }
 
 } // End namespace gold.
This page took 0.02792 seconds and 4 git commands to generate.