gdb/
[binutils-gdb.git] / gold / symtab.cc
index d8461a742dc3d8f736392d156fd7f8f7ee420ba6..c2a811f19c0be84cb5b45b56ee6e176551a1dd9d 100644 (file)
@@ -1,6 +1,6 @@
 // symtab.cc -- the gold symbol table
 
-// Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
+// Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
 // Written by Ian Lance Taylor <iant@google.com>.
 
 // This file is part of gold.
@@ -59,24 +59,23 @@ Symbol::init_fields(const char* name, const char* version,
   this->symtab_index_ = 0;
   this->dynsym_index_ = 0;
   this->got_offsets_.init();
-  this->plt_offset_ = 0;
+  this->plt_offset_ = -1U;
   this->type_ = type;
   this->binding_ = binding;
   this->visibility_ = visibility;
   this->nonvis_ = nonvis;
-  this->is_target_special_ = false;
   this->is_def_ = false;
   this->is_forwarder_ = false;
   this->has_alias_ = false;
   this->needs_dynsym_entry_ = false;
   this->in_reg_ = false;
   this->in_dyn_ = false;
-  this->has_plt_offset_ = false;
   this->has_warning_ = false;
   this->is_copied_from_dynobj_ = false;
   this->is_forced_local_ = false;
   this->is_ordinary_shndx_ = false;
   this->in_real_elf_ = false;
+  this->is_defined_in_discarded_section_ = false;
 }
 
 // Return the demangled version of the symbol's name, but only
@@ -813,13 +812,13 @@ Symbol_table::define_default_version(Sized_symbol<size>* sym,
 // section index; IS_ORDINARY is whether this is a normal section
 // rather than a special code.
 
-// If DEF is true, then this is the definition of a default version of
-// a symbol.  That means that any lookup of NAME/NULL and any lookup
-// of NAME/VERSION should always return the same symbol.  This is
-// obvious for references, but in particular we want to do this for
-// definitions: overriding NAME/NULL should also override
-// NAME/VERSION.  If we don't do that, it would be very hard to
-// override functions in a shared library which uses versioning.
+// If IS_DEFAULT_VERSION is true, then this is the definition of a
+// default version of a symbol.  That means that any lookup of
+// NAME/NULL and any lookup of NAME/VERSION should always return the
+// same symbol.  This is obvious for references, but in particular we
+// want to do this for definitions: overriding NAME/NULL should also
+// override NAME/VERSION.  If we don't do that, it would be very hard
+// to override functions in a shared library which uses versioning.
 
 // We implement this by simply making both entries in the hash table
 // point to the same Symbol structure.  That is easy enough if this is
@@ -844,7 +843,7 @@ Symbol_table::add_from_object(Object* object,
                              Stringpool::Key name_key,
                              const char *version,
                              Stringpool::Key version_key,
-                             bool def,
+                             bool is_default_version,
                              const elfcpp::Sym<size, big_endian>& sym,
                              unsigned int st_shndx,
                              bool is_ordinary,
@@ -883,14 +882,14 @@ Symbol_table::add_from_object(Object* object,
     this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
                                       snull));
 
-  std::pair<typename Symbol_table_type::iterator, bool> insdef =
+  std::pair<typename Symbol_table_type::iterator, bool> insdefault =
     std::make_pair(this->table_.end(), false);
-  if (def)
+  if (is_default_version)
     {
       const Stringpool::Key vnull_key = 0;
-      insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
-                                                                vnull_key),
-                                                 snull));
+      insdefault = this->table_.insert(std::make_pair(std::make_pair(name_key,
+                                                                    vnull_key),
+                                                     snull));
     }
 
   // ins.first: an iterator, which is a pointer to a pair.
@@ -915,20 +914,20 @@ Symbol_table::add_from_object(Object* object,
       if (parameters->options().gc_sections())
         this->gc_mark_dyn_syms(ret);
 
-      if (def)
-       this->define_default_version<size, big_endian>(ret, insdef.second,
-                                                      insdef.first);
+      if (is_default_version)
+       this->define_default_version<size, big_endian>(ret, insdefault.second,
+                                                      insdefault.first);
     }
   else
     {
       // This is the first time we have seen NAME/VERSION.
       gold_assert(ins.first->second == NULL);
 
-      if (def && !insdef.second)
+      if (is_default_version && !insdefault.second)
        {
          // We already have an entry for NAME/NULL.  If we override
          // it, then change it to NAME/VERSION.
-         ret = this->get_sized_symbol<size>(insdef.first->second);
+         ret = this->get_sized_symbol<size>(insdefault.first->second);
 
          was_undefined = ret->is_undefined();
          was_common = ret->is_common();
@@ -955,12 +954,12 @@ Symbol_table::add_from_object(Object* object,
                {
                  // This means that we don't want a symbol table
                  // entry after all.
-                 if (!def)
+                 if (!is_default_version)
                    this->table_.erase(ins.first);
                  else
                    {
-                     this->table_.erase(insdef.first);
-                     // Inserting insdef invalidated ins.
+                     this->table_.erase(insdefault.first);
+                     // Inserting INSDEFAULT invalidated INS.
                      this->table_.erase(std::make_pair(name_key,
                                                        version_key));
                    }
@@ -971,16 +970,16 @@ Symbol_table::add_from_object(Object* object,
          ret->init_object(name, version, object, sym, st_shndx, is_ordinary);
 
          ins.first->second = ret;
-         if (def)
+         if (is_default_version)
            {
              // This is the first time we have seen NAME/NULL.  Point
              // it at the new entry for NAME/VERSION.
-             gold_assert(insdef.second);
-             insdef.first->second = ret;
+             gold_assert(insdefault.second);
+             insdefault.first->second = ret;
            }
        }
 
-      if (def)
+      if (is_default_version)
        ret->set_is_default();
     }
 
@@ -1070,10 +1069,14 @@ Symbol_table::add_from_relobj(
 
       // A symbol defined in a section which we are not including must
       // be treated as an undefined symbol.
+      bool is_defined_in_discarded_section = false;
       if (st_shndx != elfcpp::SHN_UNDEF
          && is_ordinary
          && !relobj->is_section_included(st_shndx))
-       st_shndx = elfcpp::SHN_UNDEF;
+       {
+         st_shndx = elfcpp::SHN_UNDEF;
+         is_defined_in_discarded_section = true;
+       }
 
       // In an object file, an '@' in the name separates the symbol
       // name from the version name.  If there are two '@' characters,
@@ -1081,9 +1084,10 @@ Symbol_table::add_from_relobj(
       const char* ver = strchr(name, '@');
       Stringpool::Key ver_key = 0;
       int namelen = 0;
-      // DEF: is the version default?  LOCAL: is the symbol forced local?
-      bool def = false;
-      bool local = false;
+      // IS_DEFAULT_VERSION: is the version default?
+      // IS_FORCED_LOCAL: is the symbol forced local?
+      bool is_default_version = false;
+      bool is_forced_local = false;
 
       if (ver != NULL)
         {
@@ -1092,7 +1096,7 @@ Symbol_table::add_from_relobj(
           ++ver;
          if (*ver == '@')
            {
-             def = true;
+             is_default_version = true;
              ++ver;
            }
          ver = this->namepool_.add(ver, true, &ver_key);
@@ -1109,21 +1113,21 @@ Symbol_table::add_from_relobj(
              // The symbol name did not have a version, but the
              // version script may assign a version anyway.
              std::string version;
-             if (this->version_script_.get_symbol_version(name, &version))
+             bool is_global;
+             if (this->version_script_.get_symbol_version(name, &version,
+                                                          &is_global))
                {
-                 // The version can be empty if the version script is
-                 // only used to force some symbols to be local.
-                 if (!version.empty())
+                 if (!is_global)
+                   is_forced_local = true;
+                 else if (!version.empty())
                    {
                      ver = this->namepool_.add_with_length(version.c_str(),
                                                            version.length(),
                                                            true,
                                                            &ver_key);
-                     def = true;
+                     is_default_version = true;
                    }
                }
-             else if (this->version_script_.symbol_is_local(name))
-               local = true;
            }
        }
 
@@ -1151,7 +1155,8 @@ Symbol_table::add_from_relobj(
        }
 
       // Fix up visibility if object has no-export set.
-      if (relobj->no_export())
+      if (relobj->no_export()
+         && (orig_st_shndx != elfcpp::SHN_UNDEF || !is_ordinary))
         {
          // We may have copied symbol already above.
          if (psym != &sym2)
@@ -1176,8 +1181,8 @@ Symbol_table::add_from_relobj(
 
       Sized_symbol<size>* res;
       res = this->add_from_object(relobj, name, name_key, ver, ver_key,
-                                 def, *psym, st_shndx, is_ordinary,
-                                 orig_st_shndx);
+                                 is_default_version, *psym, st_shndx,
+                                 is_ordinary, orig_st_shndx);
       
       // If building a shared library using garbage collection, do not 
       // treat externally visible symbols as garbage.
@@ -1185,9 +1190,12 @@ Symbol_table::add_from_relobj(
           && parameters->options().shared())
         this->gc_mark_symbol_for_shlib(res);
 
-      if (local)
+      if (is_forced_local)
        this->force_local(res);
 
+      if (is_defined_in_discarded_section)
+       res->set_is_defined_in_discarded_section();
+
       (*sympointers)[i] = res;
     }
 }
@@ -1206,8 +1214,8 @@ Symbol_table::add_from_pluginobj(
   bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
 
   Stringpool::Key ver_key = 0;
-  bool def = false;
-  bool local = false;
+  bool is_default_version = false;
+  bool is_forced_local = false;
 
   if (ver != NULL)
     {
@@ -1224,21 +1232,21 @@ Symbol_table::add_from_pluginobj(
           // The symbol name did not have a version, but the
           // version script may assign a version anyway.
           std::string version;
-          if (this->version_script_.get_symbol_version(name, &version))
+         bool is_global;
+          if (this->version_script_.get_symbol_version(name, &version,
+                                                      &is_global))
             {
-              // The version can be empty if the version script is
-              // only used to force some symbols to be local.
-              if (!version.empty())
+             if (!is_global)
+               is_forced_local = true;
+             else if (!version.empty())
                 {
                   ver = this->namepool_.add_with_length(version.c_str(),
                                                         version.length(),
                                                         true,
                                                         &ver_key);
-                  def = true;
+                  is_default_version = true;
                 }
             }
-          else if (this->version_script_.symbol_is_local(name))
-            local = true;
         }
     }
 
@@ -1247,9 +1255,10 @@ Symbol_table::add_from_pluginobj(
 
   Sized_symbol<size>* res;
   res = this->add_from_object(obj, name, name_key, ver, ver_key,
-                             def, *sym, st_shndx, is_ordinary, st_shndx);
+                             is_default_version, *sym, st_shndx,
+                             is_ordinary, st_shndx);
 
-  if (local)
+  if (is_forced_local)
     this->force_local(res);
 
   return res;
@@ -1424,10 +1433,11 @@ Symbol_table::add_from_dynobj(
                                            st_shndx);
              else
                {
-                 const bool def = (!hidden
-                                   && st_shndx != elfcpp::SHN_UNDEF);
+                 const bool is_default_version =
+                   !hidden && st_shndx != elfcpp::SHN_UNDEF;
                  res = this->add_from_object(dynobj, name, name_key, version,
-                                             version_key, def, *psym, st_shndx,
+                                             version_key, is_default_version,
+                                             *psym, st_shndx,
                                              is_ordinary, st_shndx);
                }
            }
@@ -1556,14 +1566,16 @@ Symbol_table::define_special_symbol(const char** pname, const char** pversion,
   bool is_default_version = false;
   if (*pversion == NULL)
     {
-      if (this->version_script_.get_symbol_version(*pname, &v))
+      bool is_global;
+      if (this->version_script_.get_symbol_version(*pname, &v, &is_global))
        {
-         if (!v.empty())
-           *pversion = v.c_str();
-
-         // If we get the version from a version script, then we are
-         // also the default version.
-         is_default_version = true;
+         if (is_global && !v.empty())
+           {
+             *pversion = v.c_str();
+             // If we get the version from a version script, then we
+             // are also the default version.
+             is_default_version = true;
+           }
        }
     }
 
@@ -1603,14 +1615,15 @@ Symbol_table::define_special_symbol(const char** pname, const char** pversion,
                                                          version_key),
                                           snull));
 
-      std::pair<typename Symbol_table_type::iterator, bool> insdef =
+      std::pair<typename Symbol_table_type::iterator, bool> insdefault =
        std::make_pair(this->table_.end(), false);
       if (is_default_version)
        {
          const Stringpool::Key vnull = 0;
-         insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
-                                                                    vnull),
-                                                     snull));
+         insdefault =
+           this->table_.insert(std::make_pair(std::make_pair(name_key,
+                                                             vnull),
+                                              snull));
        }
 
       if (!ins.second)
@@ -1624,8 +1637,8 @@ Symbol_table::define_special_symbol(const char** pname, const char** pversion,
              Sized_symbol<size>* soldsym =
                this->get_sized_symbol<size>(oldsym);
              this->define_default_version<size, big_endian>(soldsym,
-                                                            insdef.second,
-                                                            insdef.first);
+                                                            insdefault.second,
+                                                            insdefault.first);
            }
        }
       else
@@ -1636,11 +1649,11 @@ Symbol_table::define_special_symbol(const char** pname, const char** pversion,
          add_to_table = true;
          add_loc = ins.first;
 
-         if (is_default_version && !insdef.second)
+         if (is_default_version && !insdefault.second)
            {
              // We are adding NAME/VERSION, and it is the default
              // version.  We already have an entry for NAME/NULL.
-             oldsym = insdef.first->second;
+             oldsym = insdefault.first->second;
              *resolve_oldsym = true;
            }
          else
@@ -1650,7 +1663,7 @@ Symbol_table::define_special_symbol(const char** pname, const char** pversion,
              if (is_default_version)
                {
                  add_def_to_table = true;
-                 add_def_loc = insdef.first;
+                 add_def_loc = insdefault.first;
                }
            }
        }