Clean up HAVE_MEMBER_TEMPLATE_SPECIFICATIONS somewhat.
[binutils-gdb.git] / gold / symtab.cc
index 8cf7789b82fd7feb19548865e0d5059dcfb062c0..2251ea76c7d1f3dc64d1aad428033d712f1064d5 100644 (file)
@@ -8,6 +8,8 @@
 #include <utility>
 
 #include "object.h"
+#include "output.h"
+#include "target.h"
 #include "symtab.h"
 
 namespace gold
@@ -52,7 +54,7 @@ Sized_symbol<size>::init(const char* name, const char* version, Object* object,
 // Class Symbol_table.
 
 Symbol_table::Symbol_table()
-  : size_(0), table_(), namepool_(), forwarders_()
+  : size_(0), offset_(0), table_(), namepool_(), forwarders_()
 {
 }
 
@@ -91,6 +93,8 @@ Symbol_table::make_forwarder(Symbol* from, Symbol* to)
   from->set_forwarder();
 }
 
+// Resolve the forwards from FROM, returning the real symbol.
+
 Symbol*
 Symbol_table::resolve_forwards(Symbol* from) const
 {
@@ -101,6 +105,28 @@ Symbol_table::resolve_forwards(Symbol* from) const
   return p->second;
 }
 
+// Look up a symbol by name.
+
+Symbol*
+Symbol_table::lookup(const char* name, const char* version) const
+{
+  name = this->namepool_.find(name);
+  if (name == NULL)
+    return NULL;
+  if (version != NULL)
+    {
+      version = this->namepool_.find(version);
+      if (version == NULL)
+       return NULL;
+    }
+
+  Symbol_table_key key(name, version);
+  Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
+  if (p == this->table_.end())
+    return NULL;
+  return p->second;
+}
+
 // Resolve a Symbol with another Symbol.  This is only used in the
 // unusual case where there are references to both an unversioned
 // symbol and a symbol with a version, and we then discover that that
@@ -109,7 +135,8 @@ Symbol_table::resolve_forwards(Symbol* from) const
 
 template<int size, bool big_endian>
 void
-Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from)
+Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from
+                      ACCEPT_SIZE_ENDIAN)
 {
   unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
   elfcpp::Sym_write<size, big_endian> esym(buf);
@@ -174,7 +201,8 @@ Symbol_table::add_from_object(Sized_object<size, big_endian>* object,
   if (!ins.second)
     {
       // We already have an entry for NAME/VERSION.
-      ret = this->get_sized_symbol<size>(ins.first->second);
+      ret = this->get_sized_symbol SELECT_SIZE_NAME (ins.first->second
+                                                     SELECT_SIZE(size));
       assert(ret != NULL);
       Symbol_table::resolve(ret, sym, object);
 
@@ -190,9 +218,12 @@ Symbol_table::add_from_object(Sized_object<size, big_endian>* object,
            {
              // This is the unfortunate case where we already have
              // entries for both NAME/VERSION and NAME/NULL.
-             const Sized_symbol<size>* sym2 =
-               this->get_sized_symbol<size>(insdef.first->second);
-             Symbol_table::resolve<size, big_endian>(ret, sym2);
+             const Sized_symbol<size>* sym2;
+             sym2 = this->get_sized_symbol SELECT_SIZE_NAME (
+               insdef.first->second
+                SELECT_SIZE(size));
+             Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME (
+               ret, sym2 SELECT_SIZE_ENDIAN(size, big_endian));
              this->make_forwarder(insdef.first->second, ret);
              insdef.first->second = ret;
            }
@@ -206,7 +237,8 @@ Symbol_table::add_from_object(Sized_object<size, big_endian>* object,
        {
          // We already have an entry for NAME/NULL.  Make
          // NAME/VERSION point to it.
-         ret = this->get_sized_symbol<size>(insdef.first->second);
+         ret = this->get_sized_symbol SELECT_SIZE_NAME (insdef.first->second
+                                                         SELECT_SIZE(size));
          Symbol_table::resolve(ret, sym, object);
          ins.first->second = ret;
        }
@@ -281,7 +313,8 @@ Symbol_table::add_from_object(
       unsigned int st_name = sym.get_st_name();
       if (st_name >= sym_name_size)
        {
-         fprintf(stderr, _("%s: %s: bad symbol name offset %u at %lu\n"),
+         fprintf(stderr,
+                 _("%s: %s: bad global symbol name offset %u at %lu\n"),
                  program_name, object->name().c_str(), st_name,
                  static_cast<unsigned long>(i));
          gold_exit(false);
@@ -320,6 +353,143 @@ Symbol_table::add_from_object(
     }
 }
 
+// Set the final values for all the symbols.  Record the file offset
+// OFF.  Add their names to POOL.  Return the new file offset.
+
+off_t
+Symbol_table::finalize(off_t off, Stringpool* pool)
+{
+  if (this->size_ == 32)
+    return this->sized_finalize<32>(off, pool);
+  else if (this->size_ == 64)
+    return this->sized_finalize<64>(off, pool);
+  else
+    abort();
+}
+
+// Set the final value for all the symbols.
+
+template<int size>
+off_t
+Symbol_table::sized_finalize(off_t off, Stringpool* pool)
+{
+  off = (off + (size >> 3) - 1) & ~ ((size >> 3) - 1);
+  this->offset_ = off;
+
+  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
+  Symbol_table_type::iterator p = this->table_.begin();
+  size_t count = 0;
+  while (p != this->table_.end())
+    {
+      Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
+
+      // FIXME: Here we need to decide which symbols should go into
+      // the output file.
+
+      // FIXME: This is wrong.
+      if (sym->shnum() >= elfcpp::SHN_LORESERVE)
+       {
+         ++p;
+         continue;
+       }
+
+      const Object::Map_to_output* mo =
+       sym->object()->section_output_info(sym->shnum());
+
+      if (mo->output_section == NULL)
+       {
+         // We should be able to erase this symbol from the symbol
+         // table, but at least with gcc 4.0.2
+         // std::unordered_map::erase doesn't appear to return the
+         // new iterator.
+         // p = this->table_.erase(p);
+         ++p;
+       }
+      else
+       {
+         sym->set_value(sym->value()
+                        + mo->output_section->address()
+                        + mo->offset);
+         pool->add(sym->name());
+         ++p;
+         ++count;
+         off += sym_size;
+       }
+    }
+
+  this->output_count_ = count;
+
+  return off;
+}
+
+// Write out the global symbols.
+
+void
+Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
+                           Output_file* of) const
+{
+  if (this->size_ == 32)
+    {
+      if (target->is_big_endian())
+       this->sized_write_globals<32, true>(target, sympool, of);
+      else
+       this->sized_write_globals<32, false>(target, sympool, of);
+    }
+  else if (this->size_ == 64)
+    {
+      if (target->is_big_endian())
+       this->sized_write_globals<64, true>(target, sympool, of);
+      else
+       this->sized_write_globals<64, false>(target, sympool, of);
+    }
+  else
+    abort();
+}
+
+// Write out the global symbols.
+
+template<int size, bool big_endian>
+void
+Symbol_table::sized_write_globals(const Target*,
+                                 const Stringpool* sympool,
+                                 Output_file* of) const
+{
+  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
+  unsigned char* psyms = of->get_output_view(this->offset_,
+                                            this->output_count_ * sym_size);
+  unsigned char* ps = psyms;
+  for (Symbol_table_type::const_iterator p = this->table_.begin();
+       p != this->table_.end();
+       ++p)
+    {
+      Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
+
+      // FIXME: This repeats sized_finalize().
+
+      // FIXME: This is wrong.
+      if (sym->shnum() >= elfcpp::SHN_LORESERVE)
+       continue;
+
+      const Object::Map_to_output* mo =
+       sym->object()->section_output_info(sym->shnum());
+
+      if (mo->output_section == NULL)
+       continue;
+
+      elfcpp::Sym_write<size, big_endian> osym(ps);
+      osym.put_st_name(sympool->get_offset(sym->name()));
+      osym.put_st_value(sym->value());
+      osym.put_st_size(sym->symsize());
+      osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
+      osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->other()));
+      osym.put_st_shndx(mo->output_section->shndx());
+
+      ps += sym_size;
+    }
+
+  of->write_output_view(this->offset_, this->output_count_ * sym_size, psyms);
+}
+
 // Instantiate the templates we need.  We could use the configure
 // script to restrict this to only the ones needed for implemented
 // targets.