Robustify mi-simplerun.
[deliverable/binutils-gdb.git] / gold / symtab.cc
index a2ae22f92674776fea2c740a7fad9aa68f7e6b04..d5718b87ea894eefe947511e32fe3949247089d5 100644 (file)
@@ -72,6 +72,7 @@ Symbol::init_fields(const char* name, const char* version,
   this->has_warning_ = false;
   this->is_copied_from_dynobj_ = false;
   this->is_forced_local_ = false;
+  this->is_ordinary_shndx_ = false;
 }
 
 // Return the demangled version of the symbol's name, but only
@@ -104,14 +105,15 @@ Symbol::demangled_name() const
 
 template<int size, bool big_endian>
 void
-Symbol::init_base(const char* name, const char* version, Object* object,
-                 const elfcpp::Sym<size, big_endian>& sym)
+Symbol::init_base_object(const char* name, const char* version, Object* object,
+                        const elfcpp::Sym<size, big_endian>& sym,
+                        unsigned int st_shndx, bool is_ordinary)
 {
   this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
                    sym.get_st_visibility(), sym.get_st_nonvis());
   this->u_.from_object.object = object;
-  // FIXME: Handle SHN_XINDEX.
-  this->u_.from_object.shndx = sym.get_st_shndx();
+  this->u_.from_object.shndx = st_shndx;
+  this->is_ordinary_shndx_ = is_ordinary;
   this->source_ = FROM_OBJECT;
   this->in_reg_ = !object->is_dynamic();
   this->in_dyn_ = object->is_dynamic();
@@ -121,11 +123,12 @@ Symbol::init_base(const char* name, const char* version, Object* object,
 // in an Output_data.
 
 void
-Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
-                 elfcpp::STB binding, elfcpp::STV visibility,
-                 unsigned char nonvis, bool offset_is_from_end)
+Symbol::init_base_output_data(const char* name, const char* version,
+                             Output_data* od, elfcpp::STT type,
+                             elfcpp::STB binding, elfcpp::STV visibility,
+                             unsigned char nonvis, bool offset_is_from_end)
 {
-  this->init_fields(name, NULL, type, binding, visibility, nonvis);
+  this->init_fields(name, version, type, binding, visibility, nonvis);
   this->u_.in_output_data.output_data = od;
   this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
   this->source_ = IN_OUTPUT_DATA;
@@ -136,11 +139,13 @@ Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
 // in an Output_segment.
 
 void
-Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
-                 elfcpp::STB binding, elfcpp::STV visibility,
-                 unsigned char nonvis, Segment_offset_base offset_base)
+Symbol::init_base_output_segment(const char* name, const char* version,
+                                Output_segment* os, elfcpp::STT type,
+                                elfcpp::STB binding, elfcpp::STV visibility,
+                                unsigned char nonvis,
+                                Segment_offset_base offset_base)
 {
-  this->init_fields(name, NULL, type, binding, visibility, nonvis);
+  this->init_fields(name, version, type, binding, visibility, nonvis);
   this->u_.in_output_segment.output_segment = os;
   this->u_.in_output_segment.offset_base = offset_base;
   this->source_ = IN_OUTPUT_SEGMENT;
@@ -151,12 +156,25 @@ Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
 // as a constant.
 
 void
-Symbol::init_base(const char* name, elfcpp::STT type,
-                 elfcpp::STB binding, elfcpp::STV visibility,
-                 unsigned char nonvis)
+Symbol::init_base_constant(const char* name, const char* version,
+                          elfcpp::STT type, elfcpp::STB binding,
+                          elfcpp::STV visibility, unsigned char nonvis)
 {
-  this->init_fields(name, NULL, type, binding, visibility, nonvis);
-  this->source_ = CONSTANT;
+  this->init_fields(name, version, type, binding, visibility, nonvis);
+  this->source_ = IS_CONSTANT;
+  this->in_reg_ = true;
+}
+
+// Initialize the fields in the base class Symbol for an undefined
+// symbol.
+
+void
+Symbol::init_base_undefined(const char* name, const char* version,
+                           elfcpp::STT type, elfcpp::STB binding,
+                           elfcpp::STV visibility, unsigned char nonvis)
+{
+  this->init_fields(name, version, type, binding, visibility, nonvis);
+  this->source_ = IS_UNDEFINED;
   this->in_reg_ = true;
 }
 
@@ -176,10 +194,12 @@ Symbol::allocate_base_common(Output_data* od)
 template<int size>
 template<bool big_endian>
 void
-Sized_symbol<size>::init(const char* name, const char* version, Object* object,
-                        const elfcpp::Sym<size, big_endian>& sym)
+Sized_symbol<size>::init_object(const char* name, const char* version,
+                               Object* object,
+                               const elfcpp::Sym<size, big_endian>& sym,
+                               unsigned int st_shndx, bool is_ordinary)
 {
-  this->init_base(name, version, object, sym);
+  this->init_base_object(name, version, object, sym, st_shndx, is_ordinary);
   this->value_ = sym.get_st_value();
   this->symsize_ = sym.get_st_size();
 }
@@ -189,14 +209,16 @@ Sized_symbol<size>::init(const char* name, const char* version, Object* object,
 
 template<int size>
 void
-Sized_symbol<size>::init(const char* name, Output_data* od,
-                        Value_type value, Size_type symsize,
-                        elfcpp::STT type, elfcpp::STB binding,
-                        elfcpp::STV visibility, unsigned char nonvis,
-                        bool offset_is_from_end)
-{
-  this->init_base(name, od, type, binding, visibility, nonvis,
-                 offset_is_from_end);
+Sized_symbol<size>::init_output_data(const char* name, const char* version,
+                                    Output_data* od, Value_type value,
+                                    Size_type symsize, elfcpp::STT type,
+                                    elfcpp::STB binding,
+                                    elfcpp::STV visibility,
+                                    unsigned char nonvis,
+                                    bool offset_is_from_end)
+{
+  this->init_base_output_data(name, version, od, type, binding, visibility,
+                             nonvis, offset_is_from_end);
   this->value_ = value;
   this->symsize_ = symsize;
 }
@@ -206,13 +228,16 @@ Sized_symbol<size>::init(const char* name, Output_data* od,
 
 template<int size>
 void
-Sized_symbol<size>::init(const char* name, Output_segment* os,
-                        Value_type value, Size_type symsize,
-                        elfcpp::STT type, elfcpp::STB binding,
-                        elfcpp::STV visibility, unsigned char nonvis,
-                        Segment_offset_base offset_base)
+Sized_symbol<size>::init_output_segment(const char* name, const char* version,
+                                       Output_segment* os, Value_type value,
+                                       Size_type symsize, elfcpp::STT type,
+                                       elfcpp::STB binding,
+                                       elfcpp::STV visibility,
+                                       unsigned char nonvis,
+                                       Segment_offset_base offset_base)
 {
-  this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
+  this->init_base_output_segment(name, version, os, type, binding, visibility,
+                                nonvis, offset_base);
   this->value_ = value;
   this->symsize_ = symsize;
 }
@@ -222,15 +247,29 @@ Sized_symbol<size>::init(const char* name, Output_segment* os,
 
 template<int size>
 void
-Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize,
-                        elfcpp::STT type, elfcpp::STB binding,
-                        elfcpp::STV visibility, unsigned char nonvis)
+Sized_symbol<size>::init_constant(const char* name, const char* version,
+                                 Value_type value, Size_type symsize,
+                                 elfcpp::STT type, elfcpp::STB binding,
+                                 elfcpp::STV visibility, unsigned char nonvis)
 {
-  this->init_base(name, type, binding, visibility, nonvis);
+  this->init_base_constant(name, version, type, binding, visibility, nonvis);
   this->value_ = value;
   this->symsize_ = symsize;
 }
 
+// Initialize the fields in Sized_symbol for an undefined symbol.
+
+template<int size>
+void
+Sized_symbol<size>::init_undefined(const char* name, const char* version,
+                                  elfcpp::STT type, elfcpp::STB binding,
+                                  elfcpp::STV visibility, unsigned char nonvis)
+{
+  this->init_base_undefined(name, version, type, binding, visibility, nonvis);
+  this->value_ = 0;
+  this->symsize_ = 0;
+}
+
 // Allocate a common symbol.
 
 template<int size>
@@ -277,20 +316,25 @@ Symbol::final_value_is_known() const
   if (parameters->options().shared() || parameters->options().relocatable())
     return false;
 
-  // If the symbol is not from an object file, then it is defined, and
-  // known.
+  // If the symbol is not from an object file, and is not undefined,
+  // then it is defined, and known.
   if (this->source_ != FROM_OBJECT)
-    return true;
-
-  // If the symbol is from a dynamic object, then the final value is
-  // not known.
-  if (this->object()->is_dynamic())
-    return false;
+    {
+      if (this->source_ != IS_UNDEFINED)
+       return true;
+    }
+  else
+    {
+      // If the symbol is from a dynamic object, then the final value
+      // is not known.
+      if (this->object()->is_dynamic())
+       return false;
 
-  // If the symbol is not undefined (it is defined or common), then
-  // the final value is known.
-  if (!this->is_undefined())
-    return true;
+      // If the symbol is not undefined (it is defined or common),
+      // then the final value is known.
+      if (!this->is_undefined())
+       return true;
+    }
 
   // If the symbol is undefined, then whether the final value is known
   // depends on whether we are doing a static link.  If we are doing a
@@ -309,7 +353,7 @@ Symbol::output_section() const
     case FROM_OBJECT:
       {
        unsigned int shndx = this->u_.from_object.shndx;
-       if (shndx != elfcpp::SHN_UNDEF && shndx < elfcpp::SHN_LORESERVE)
+       if (shndx != elfcpp::SHN_UNDEF && this->is_ordinary_shndx_)
          {
            gold_assert(!this->u_.from_object.object->is_dynamic());
            Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object);
@@ -323,7 +367,8 @@ Symbol::output_section() const
       return this->u_.in_output_data.output_data->output_section();
 
     case IN_OUTPUT_SEGMENT:
-    case CONSTANT:
+    case IS_CONSTANT:
+    case IS_UNDEFINED:
       return NULL;
 
     default:
@@ -344,12 +389,13 @@ Symbol::set_output_section(Output_section* os)
     case IN_OUTPUT_DATA:
       gold_assert(this->output_section() == os);
       break;
-    case CONSTANT:
+    case IS_CONSTANT:
       this->source_ = IN_OUTPUT_DATA;
       this->u_.in_output_data.output_data = os;
       this->u_.in_output_data.offset_is_from_end = false;
       break;
     case IN_OUTPUT_SEGMENT:
+    case IS_UNDEFINED:
     default:
       gold_unreachable();
     }
@@ -360,7 +406,7 @@ Symbol::set_output_section(Output_section* os)
 Symbol_table::Symbol_table(unsigned int count,
                            const Version_script_info& version_script)
   : saw_undefined_(0), offset_(0), table_(count), namepool_(),
-    forwarders_(), commons_(), forced_locals_(), warnings_(),
+    forwarders_(), commons_(), tls_commons_(), forced_locals_(), warnings_(),
     version_script_(version_script)
 {
   namepool_.reserve(count);
@@ -449,13 +495,15 @@ Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
 {
   unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
   elfcpp::Sym_write<size, big_endian> esym(buf);
-  // We don't bother to set the st_name field.
+  // We don't bother to set the st_name or the st_shndx field.
   esym.put_st_value(from->value());
   esym.put_st_size(from->symsize());
   esym.put_st_info(from->binding(), from->type());
   esym.put_st_other(from->visibility(), from->nonvis());
-  esym.put_st_shndx(from->shndx());
-  this->resolve(to, esym.sym(), esym.sym(), from->object(), version);
+  bool is_ordinary;
+  unsigned int shndx = from->shndx(&is_ordinary);
+  this->resolve(to, esym.sym(), shndx, is_ordinary, shndx, from->object(),
+               version);
   if (from->in_reg())
     to->set_in_reg();
   if (from->in_dyn())
@@ -478,9 +526,59 @@ Symbol_table::force_local(Symbol* sym)
   this->forced_locals_.push_back(sym);
 }
 
+// Adjust NAME for wrapping, and update *NAME_KEY if necessary.  This
+// is only called for undefined symbols, when at least one --wrap
+// option was used.
+
+const char*
+Symbol_table::wrap_symbol(Object* object, const char* name,
+                         Stringpool::Key* name_key)
+{
+  // For some targets, we need to ignore a specific character when
+  // wrapping, and add it back later.
+  char prefix = '\0';
+  if (name[0] == object->target()->wrap_char())
+    {
+      prefix = name[0];
+      ++name;
+    }
+
+  if (parameters->options().is_wrap(name))
+    {
+      // Turn NAME into __wrap_NAME.
+      std::string s;
+      if (prefix != '\0')
+       s += prefix;
+      s += "__wrap_";
+      s += name;
+
+      // This will give us both the old and new name in NAMEPOOL_, but
+      // that is OK.  Only the versions we need will wind up in the
+      // real string table in the output file.
+      return this->namepool_.add(s.c_str(), true, name_key);
+    }
+
+  const char* const real_prefix = "__real_";
+  const size_t real_prefix_length = strlen(real_prefix);
+  if (strncmp(name, real_prefix, real_prefix_length) == 0
+      && parameters->options().is_wrap(name + real_prefix_length))
+    {
+      // Turn __real_NAME into NAME.
+      std::string s;
+      if (prefix != '\0')
+       s += prefix;
+      s += name + real_prefix_length;
+      return this->namepool_.add(s.c_str(), true, name_key);
+    }
+
+  return name;
+}
+
 // Add one symbol from OBJECT to the symbol table.  NAME is symbol
 // name and VERSION is the version; both are canonicalized.  DEF is
-// whether this is the default version.
+// whether this is the default version.  ST_SHNDX is the symbol's
+// 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
@@ -501,10 +599,10 @@ Symbol_table::force_local(Symbol* sym)
 // Note that entries in the hash table will never be marked as
 // forwarders.
 //
-// SYM and ORIG_SYM are almost always the same.  ORIG_SYM is the
-// symbol exactly as it existed in the input file.  SYM is usually
-// that as well, but can be modified, for instance if we determine
-// it's in a to-be-discarded section.
+// ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
+// ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
+// for a special section code.  ST_SHNDX may be modified if the symbol
+// is defined in a section being discarded.
 
 template<int size, bool big_endian>
 Sized_symbol<size>*
@@ -515,8 +613,38 @@ Symbol_table::add_from_object(Object* object,
                              Stringpool::Key version_key,
                              bool def,
                              const elfcpp::Sym<size, big_endian>& sym,
-                             const elfcpp::Sym<size, big_endian>& orig_sym)
+                             unsigned int st_shndx,
+                             bool is_ordinary,
+                             unsigned int orig_st_shndx)
 {
+  // Print a message if this symbol is being traced.
+  if (parameters->options().is_trace_symbol(name))
+    {
+      if (orig_st_shndx == elfcpp::SHN_UNDEF)
+        gold_info(_("%s: reference to %s"), object->name().c_str(), name);
+      else
+        gold_info(_("%s: definition of %s"), object->name().c_str(), name);
+    }
+
+  // For an undefined symbol, we may need to adjust the name using
+  // --wrap.
+  if (orig_st_shndx == elfcpp::SHN_UNDEF
+      && parameters->options().any_wrap())
+    {
+      const char* wrap_name = this->wrap_symbol(object, name, &name_key);
+      if (wrap_name != name)
+       {
+         // If we see a reference to malloc with version GLIBC_2.0,
+         // and we turn it into a reference to __wrap_malloc, then we
+         // discard the version number.  Otherwise the user would be
+         // required to specify the correct version for
+         // __wrap_malloc.
+         version = NULL;
+         version_key = 0;
+         name = wrap_name;
+       }
+    }
+
   Symbol* const snull = NULL;
   std::pair<typename Symbol_table_type::iterator, bool> ins =
     this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
@@ -549,7 +677,8 @@ Symbol_table::add_from_object(Object* object,
       was_undefined = ret->is_undefined();
       was_common = ret->is_common();
 
-      this->resolve(ret, sym, orig_sym, object, version);
+      this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
+                   version);
 
       if (def)
        {
@@ -577,6 +706,8 @@ Symbol_table::add_from_object(Object* object,
              this->make_forwarder(insdef.first->second, ret);
              insdef.first->second = ret;
            }
+         else
+           def = false;
        }
     }
   else
@@ -593,7 +724,8 @@ Symbol_table::add_from_object(Object* object,
          was_undefined = ret->is_undefined();
          was_common = ret->is_common();
 
-         this->resolve(ret, sym, orig_sym, object, version);
+         this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
+                       version);
          ins.first->second = ret;
        }
       else
@@ -625,7 +757,7 @@ Symbol_table::add_from_object(Object* object,
                }
            }
 
-         ret->init(name, version, object, sym);
+         ret->init_object(name, version, object, sym, st_shndx, is_ordinary);
 
          ins.first->second = ret;
          if (def)
@@ -646,7 +778,12 @@ Symbol_table::add_from_object(Object* object,
   // Keep track of common symbols, to speed up common symbol
   // allocation.
   if (!was_common && ret->is_common())
-    this->commons_.push_back(ret);
+    {
+      if (ret->type() != elfcpp::STT_TLS)
+       this->commons_.push_back(ret);
+      else
+       this->tls_commons_.push_back(ret);
+    }
 
   if (def)
     ret->set_is_default();
@@ -661,6 +798,7 @@ Symbol_table::add_from_relobj(
     Sized_relobj<size, big_endian>* relobj,
     const unsigned char* syms,
     size_t count,
+    size_t symndx_offset,
     const char* sym_names,
     size_t sym_name_size,
     typename Sized_relobj<size, big_endian>::Symbols* sympointers)
@@ -676,9 +814,8 @@ Symbol_table::add_from_relobj(
   for (size_t i = 0; i < count; ++i, p += sym_size)
     {
       elfcpp::Sym<size, big_endian> sym(p);
-      elfcpp::Sym<size, big_endian>* psym = &sym;
 
-      unsigned int st_name = psym->get_st_name();
+      unsigned int st_name = sym.get_st_name();
       if (st_name >= sym_name_size)
        {
          relobj->error(_("bad global symbol name offset %u at %zu"),
@@ -688,20 +825,20 @@ Symbol_table::add_from_relobj(
 
       const char* name = sym_names + st_name;
 
+      bool is_ordinary;
+      unsigned int st_shndx = relobj->adjust_sym_shndx(i + symndx_offset,
+                                                      sym.get_st_shndx(),
+                                                      &is_ordinary);
+      unsigned int orig_st_shndx = st_shndx;
+      if (!is_ordinary)
+       orig_st_shndx = elfcpp::SHN_UNDEF;
+
       // A symbol defined in a section which we are not including must
       // be treated as an undefined symbol.
-      unsigned char symbuf[sym_size];
-      elfcpp::Sym<size, big_endian> sym2(symbuf);
-      unsigned int st_shndx = psym->get_st_shndx();
       if (st_shndx != elfcpp::SHN_UNDEF
-         && st_shndx < elfcpp::SHN_LORESERVE
+         && is_ordinary
          && !relobj->is_section_included(st_shndx))
-       {
-         memcpy(symbuf, p, sym_size);
-         elfcpp::Sym_write<size, big_endian> sw(symbuf);
-         sw.put_st_shndx(elfcpp::SHN_UNDEF);
-         psym = &sym2;
-       }
+       st_shndx = elfcpp::SHN_UNDEF;
 
       // In an object file, an '@' in the name separates the symbol
       // name from the version name.  If there are two '@' characters,
@@ -727,7 +864,7 @@ Symbol_table::add_from_relobj(
       // even if it is listed in the version script.  FIXME: What
       // about a common symbol?
       else if (!version_script_.empty()
-              && psym->get_st_shndx() != elfcpp::SHN_UNDEF)
+              && st_shndx != elfcpp::SHN_UNDEF)
         {
           // The symbol name did not have a version, but
           // the version script may assign a version anyway.
@@ -743,14 +880,14 @@ Symbol_table::add_from_relobj(
             local = true;
         }
 
+      elfcpp::Sym<size, big_endian>* psym = &sym;
+      unsigned char symbuf[sym_size];
+      elfcpp::Sym<size, big_endian> sym2(symbuf);
       if (just_symbols)
        {
-         if (psym != &sym2)
-           memcpy(symbuf, p, sym_size);
+         memcpy(symbuf, p, sym_size);
          elfcpp::Sym_write<size, big_endian> sw(symbuf);
-         sw.put_st_shndx(elfcpp::SHN_ABS);
-         if (st_shndx != elfcpp::SHN_UNDEF
-             && st_shndx < elfcpp::SHN_LORESERVE)
+         if (orig_st_shndx != elfcpp::SHN_UNDEF && is_ordinary)
            {
              // Symbol values in object files are section relative.
              // This is normally what we want, but since here we are
@@ -758,9 +895,11 @@ Symbol_table::add_from_relobj(
              // section address.  The section address in an object
              // file is normally zero, but people can use a linker
              // script to change it.
-             sw.put_st_value(sym2.get_st_value()
-                             + relobj->section_address(st_shndx));
+             sw.put_st_value(sym.get_st_value()
+                             + relobj->section_address(orig_st_shndx));
            }
+         st_shndx = elfcpp::SHN_ABS;
+         is_ordinary = false;
          psym = &sym2;
        }
 
@@ -770,7 +909,8 @@ Symbol_table::add_from_relobj(
          Stringpool::Key name_key;
          name = this->namepool_.add(name, true, &name_key);
          res = this->add_from_object(relobj, name, name_key, NULL, 0,
-                                     false, *psym, sym);
+                                     false, *psym, st_shndx, is_ordinary,
+                                     orig_st_shndx);
           if (local)
            this->force_local(res);
        }
@@ -783,7 +923,8 @@ Symbol_table::add_from_relobj(
          ver = this->namepool_.add(ver, true, &ver_key);
 
          res = this->add_from_object(relobj, name, name_key, ver, ver_key,
-                                     def, *psym, sym);
+                                     def, *psym, st_shndx, is_ordinary,
+                                     orig_st_shndx);
        }
 
       (*sympointers)[i] = res;
@@ -844,7 +985,21 @@ Symbol_table::add_from_dynobj(
           || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
        continue;
 
-      unsigned int st_name = sym.get_st_name();
+      // A protected symbol in a shared library must be treated as a
+      // normal symbol when viewed from outside the shared library.
+      // Implement this by overriding the visibility here.
+      elfcpp::Sym<size, big_endian>* psym = &sym;
+      unsigned char symbuf[sym_size];
+      elfcpp::Sym<size, big_endian> sym2(symbuf);
+      if (sym.get_st_visibility() == elfcpp::STV_PROTECTED)
+       {
+         memcpy(symbuf, p, sym_size);
+         elfcpp::Sym_write<size, big_endian> sw(symbuf);
+         sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis());
+         psym = &sym2;
+       }
+
+      unsigned int st_name = psym->get_st_name();
       if (st_name >= sym_name_size)
        {
          dynobj->error(_("bad symbol name offset %u at %zu"),
@@ -854,6 +1009,10 @@ Symbol_table::add_from_dynobj(
 
       const char* name = sym_names + st_name;
 
+      bool is_ordinary;
+      unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(),
+                                                      &is_ordinary);
+
       Sized_symbol<size>* res;
 
       if (versym == NULL)
@@ -861,7 +1020,8 @@ Symbol_table::add_from_dynobj(
          Stringpool::Key name_key;
          name = this->namepool_.add(name, true, &name_key);
          res = this->add_from_object(dynobj, name, name_key, NULL, 0,
-                                     false, sym, sym);
+                                     false, *psym, st_shndx, is_ordinary,
+                                     st_shndx);
        }
       else
        {
@@ -880,7 +1040,7 @@ Symbol_table::add_from_dynobj(
          // linker will generate.
 
          if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
-             && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
+             && st_shndx != elfcpp::SHN_UNDEF)
            {
              // This symbol should not be visible outside the object.
              continue;
@@ -895,7 +1055,8 @@ Symbol_table::add_from_dynobj(
            {
              // This symbol does not have a version.
              res = this->add_from_object(dynobj, name, name_key, NULL, 0,
-                                         false, sym, sym);
+                                         false, *psym, st_shndx, is_ordinary,
+                                         st_shndx);
            }
          else
            {
@@ -922,23 +1083,30 @@ Symbol_table::add_from_dynobj(
              // version definition symbol.  These symbols exist to
              // support using -u to pull in particular versions.  We
              // do not want to record a version for them.
-             if (sym.get_st_shndx() == elfcpp::SHN_ABS
+             if (st_shndx == elfcpp::SHN_ABS
+                 && !is_ordinary
                  && name_key == version_key)
                res = this->add_from_object(dynobj, name, name_key, NULL, 0,
-                                           false, sym, sym);
+                                           false, *psym, st_shndx, is_ordinary,
+                                           st_shndx);
              else
                {
                  const bool def = (!hidden
-                                   && (sym.get_st_shndx()
-                                       != elfcpp::SHN_UNDEF));
+                                   && st_shndx != elfcpp::SHN_UNDEF);
                  res = this->add_from_object(dynobj, name, name_key, version,
-                                             version_key, def, sym, sym);
+                                             version_key, def, *psym, st_shndx,
+                                             is_ordinary, st_shndx);
                }
            }
        }
 
-      if (sym.get_st_shndx() != elfcpp::SHN_UNDEF
-         && sym.get_st_type() == elfcpp::STT_OBJECT)
+      // Note that it is possible that RES was overridden by an
+      // earlier object, in which case it can't be aliased here.
+      if (st_shndx != elfcpp::SHN_UNDEF
+         && is_ordinary
+         && psym->get_st_type() == elfcpp::STT_OBJECT
+         && res->source() == Symbol::FROM_OBJECT
+         && res->object() == dynobj)
        object_symbols.push_back(res);
     }
 
@@ -960,8 +1128,14 @@ bool
 Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
                                    const Sized_symbol<size>* s2) const
 {
-  if (s1->shndx() != s2->shndx())
-    return s1->shndx() < s2->shndx();
+  bool is_ordinary;
+  unsigned int s1_shndx = s1->shndx(&is_ordinary);
+  gold_assert(is_ordinary);
+  unsigned int s2_shndx = s2->shndx(&is_ordinary);
+  gold_assert(is_ordinary);
+  if (s1_shndx != s2_shndx)
+    return s1_shndx < s2_shndx;
+
   if (s1->value() != s2->value())
     return s1->value() < s2->value();
   if (s1->binding() != s2->binding())
@@ -1004,7 +1178,8 @@ Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
       typename std::vector<Sized_symbol<size>*>::const_iterator q;
       for (q = p + 1; q != symbols->end(); ++q)
        {
-         if ((*q)->shndx() != from_sym->shndx()
+         bool dummy;
+         if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy)
              || (*q)->value() != from_sym->value())
            break;
 
@@ -1199,15 +1374,16 @@ Symbol_table::do_define_in_output_data(
   if (sym == NULL)
     return NULL;
 
-  gold_assert(version == NULL || oldsym != NULL);
-  sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
-           offset_is_from_end);
+  sym->init_output_data(name, version, od, value, symsize, type, binding,
+                       visibility, nonvis, offset_is_from_end);
 
   if (oldsym == NULL)
     {
       if (binding == elfcpp::STB_LOCAL
          || this->version_script_.symbol_is_local(name))
        this->force_local(sym);
+      else if (version != NULL)
+       sym->set_is_default();
       return sym;
     }
 
@@ -1299,15 +1475,16 @@ Symbol_table::do_define_in_output_segment(
   if (sym == NULL)
     return NULL;
 
-  gold_assert(version == NULL || oldsym != NULL);
-  sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
-           offset_base);
+  sym->init_output_segment(name, version, os, value, symsize, type, binding,
+                          visibility, nonvis, offset_base);
 
   if (oldsym == NULL)
     {
       if (binding == elfcpp::STB_LOCAL
          || this->version_script_.symbol_is_local(name))
        this->force_local(sym);
+      else if (version != NULL)
+       sym->set_is_default();
       return sym;
     }
 
@@ -1399,14 +1576,22 @@ Symbol_table::do_define_as_constant(
   if (sym == NULL)
     return NULL;
 
-  gold_assert(version == NULL || version == name || oldsym != NULL);
-  sym->init(name, value, symsize, type, binding, visibility, nonvis);
+  sym->init_constant(name, version, value, symsize, type, binding, visibility,
+                    nonvis);
 
   if (oldsym == NULL)
     {
-      if (binding == elfcpp::STB_LOCAL
-         || this->version_script_.symbol_is_local(name))
+      // Version symbols are absolute symbols with name == version.
+      // We don't want to force them to be local.
+      if ((version == NULL
+          || name != version
+          || value != 0)
+         && (binding == elfcpp::STB_LOCAL
+             || this->version_script_.symbol_is_local(name)))
        this->force_local(sym);
+      else if (version != NULL
+              && (name != version || value != 0))
+       sym->set_is_default();
       return sym;
     }
 
@@ -1531,6 +1716,79 @@ Symbol_table::get_copy_source(const Symbol* sym) const
   return p->second;
 }
 
+// Add any undefined symbols named on the command line.
+
+void
+Symbol_table::add_undefined_symbols_from_command_line()
+{
+  if (parameters->options().any_undefined())
+    {
+      if (parameters->target().get_size() == 32)
+       {
+#if defined(HAVE_TARGET_32_LITTL) || defined(HAVE_TARGET_32_BIG)
+         this->do_add_undefined_symbols_from_command_line<32>();
+#else
+         gold_unreachable();
+#endif
+       }
+      else if (parameters->target().get_size() == 64)
+       {
+#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
+         this->do_add_undefined_symbols_from_command_line<64>();
+#else
+         gold_unreachable();
+#endif
+       }
+      else
+       gold_unreachable();
+    }
+}
+
+template<int size>
+void
+Symbol_table::do_add_undefined_symbols_from_command_line()
+{
+  for (options::String_set::const_iterator p =
+        parameters->options().undefined_begin();
+       p != parameters->options().undefined_end();
+       ++p)
+    {
+      const char* name = p->c_str();
+
+      if (this->lookup(name) != NULL)
+       continue;
+
+      const char* version = NULL;
+
+      Sized_symbol<size>* sym;
+      Sized_symbol<size>* oldsym;
+      if (parameters->target().is_big_endian())
+       {
+#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
+         sym = this->define_special_symbol<size, true>(&name, &version,
+                                                       false, &oldsym);
+#else
+         gold_unreachable();
+#endif
+       }
+      else
+       {
+#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
+         sym = this->define_special_symbol<size, false>(&name, &version,
+                                                        false, &oldsym);
+#else
+         gold_unreachable();
+#endif
+       }
+
+      gold_assert(oldsym == NULL);
+
+      sym->init_undefined(name, version, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
+                         elfcpp::STV_DEFAULT, 0);
+      ++this->saw_undefined_;
+    }
+}
+
 // Set the dynamic symbol indexes.  INDEX is the index of the first
 // global dynamic symbol.  Pointers to the symbols are stored into the
 // vector SYMS.  The names are added to DYNPOOL.  This returns an
@@ -1706,10 +1964,11 @@ Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
     {
     case Symbol::FROM_OBJECT:
       {
-       unsigned int shndx = sym->shndx();
+       bool is_ordinary;
+       unsigned int shndx = sym->shndx(&is_ordinary);
 
        // FIXME: We need some target specific support here.
-       if (shndx >= elfcpp::SHN_LORESERVE
+       if (!is_ordinary
            && shndx != elfcpp::SHN_ABS
            && shndx != elfcpp::SHN_COMMON)
          {
@@ -1726,7 +1985,8 @@ Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
          }
        else if (shndx == elfcpp::SHN_UNDEF)
          value = 0;
-       else if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON)
+       else if (!is_ordinary
+                && (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON))
          value = sym->value();
        else
          {
@@ -1752,7 +2012,15 @@ Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
     case Symbol::IN_OUTPUT_DATA:
       {
        Output_data* od = sym->output_data();
-       value = sym->value() + od->address();
+       value = sym->value();
+       if (sym->type() != elfcpp::STT_TLS)
+         value += od->address();
+       else
+         {
+           Output_section* os = od->output_section();
+           gold_assert(os != NULL);
+           value += os->tls_offset() + (od->address() - os->address());
+         }
        if (sym->offset_is_from_end())
          value += od->data_size();
       }
@@ -1761,7 +2029,9 @@ Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
     case Symbol::IN_OUTPUT_SEGMENT:
       {
        Output_segment* os = sym->output_segment();
-       value = sym->value() + os->vaddr();
+       value = sym->value();
+        if (sym->type() != elfcpp::STT_TLS)
+         value += os->vaddr();
        switch (sym->offset_base())
          {
          case Symbol::SEGMENT_START:
@@ -1778,10 +2048,14 @@ Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
       }
       break;
 
-    case Symbol::CONSTANT:
+    case Symbol::IS_CONSTANT:
       value = sym->value();
       break;
 
+    case Symbol::IS_UNDEFINED:
+      value = 0;
+      break;
+
     default:
       gold_unreachable();
     }
@@ -1802,32 +2076,39 @@ Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
 void
 Symbol_table::write_globals(const Input_objects* input_objects,
                            const Stringpool* sympool,
-                           const Stringpool* dynpool, Output_file* of) const
+                           const Stringpool* dynpool,
+                           Output_symtab_xindex* symtab_xindex,
+                           Output_symtab_xindex* dynsym_xindex,
+                           Output_file* of) const
 {
   switch (parameters->size_and_endianness())
     {
 #ifdef HAVE_TARGET_32_LITTLE
     case Parameters::TARGET_32_LITTLE:
       this->sized_write_globals<32, false>(input_objects, sympool,
-                                           dynpool, of);
+                                           dynpool, symtab_xindex,
+                                          dynsym_xindex, of);
       break;
 #endif
 #ifdef HAVE_TARGET_32_BIG
     case Parameters::TARGET_32_BIG:
       this->sized_write_globals<32, true>(input_objects, sympool,
-                                          dynpool, of);
+                                          dynpool, symtab_xindex,
+                                         dynsym_xindex, of);
       break;
 #endif
 #ifdef HAVE_TARGET_64_LITTLE
     case Parameters::TARGET_64_LITTLE:
       this->sized_write_globals<64, false>(input_objects, sympool,
-                                           dynpool, of);
+                                           dynpool, symtab_xindex,
+                                          dynsym_xindex, of);
       break;
 #endif
 #ifdef HAVE_TARGET_64_BIG
     case Parameters::TARGET_64_BIG:
       this->sized_write_globals<64, true>(input_objects, sympool,
-                                          dynpool, of);
+                                          dynpool, symtab_xindex,
+                                         dynsym_xindex, of);
       break;
 #endif
     default:
@@ -1842,6 +2123,8 @@ void
 Symbol_table::sized_write_globals(const Input_objects* input_objects,
                                  const Stringpool* sympool,
                                  const Stringpool* dynpool,
+                                 Output_symtab_xindex* symtab_xindex,
+                                 Output_symtab_xindex* dynsym_xindex,
                                  Output_file* of) const
 {
   const Target& target = parameters->target();
@@ -1896,10 +2179,11 @@ Symbol_table::sized_write_globals(const Input_objects* input_objects,
        {
        case Symbol::FROM_OBJECT:
          {
-           unsigned int in_shndx = sym->shndx();
+           bool is_ordinary;
+           unsigned int in_shndx = sym->shndx(&is_ordinary);
 
            // FIXME: We need some target specific support here.
-           if (in_shndx >= elfcpp::SHN_LORESERVE
+           if (!is_ordinary
                && in_shndx != elfcpp::SHN_ABS
                && in_shndx != elfcpp::SHN_COMMON)
              {
@@ -1917,8 +2201,9 @@ Symbol_table::sized_write_globals(const Input_objects* input_objects,
                    shndx = elfcpp::SHN_UNDEF;
                  }
                else if (in_shndx == elfcpp::SHN_UNDEF
-                        || in_shndx == elfcpp::SHN_ABS
-                        || in_shndx == elfcpp::SHN_COMMON)
+                        || (!is_ordinary
+                            && (in_shndx == elfcpp::SHN_ABS
+                                || in_shndx == elfcpp::SHN_COMMON)))
                  shndx = in_shndx;
                else
                  {
@@ -1929,6 +2214,15 @@ Symbol_table::sized_write_globals(const Input_objects* input_objects,
                    gold_assert(os != NULL);
                    shndx = os->out_shndx();
 
+                   if (shndx >= elfcpp::SHN_LORESERVE)
+                     {
+                       if (sym_index != -1U)
+                         symtab_xindex->add(sym_index, shndx);
+                       if (dynsym_index != -1U)
+                         dynsym_xindex->add(dynsym_index, shndx);
+                       shndx = elfcpp::SHN_XINDEX;
+                     }
+
                    // In object files symbol values are section
                    // relative.
                    if (parameters->options().relocatable())
@@ -1940,16 +2234,28 @@ Symbol_table::sized_write_globals(const Input_objects* input_objects,
 
        case Symbol::IN_OUTPUT_DATA:
          shndx = sym->output_data()->out_shndx();
+         if (shndx >= elfcpp::SHN_LORESERVE)
+           {
+             if (sym_index != -1U)
+               symtab_xindex->add(sym_index, shndx);
+             if (dynsym_index != -1U)
+               dynsym_xindex->add(dynsym_index, shndx);
+             shndx = elfcpp::SHN_XINDEX;
+           }
          break;
 
        case Symbol::IN_OUTPUT_SEGMENT:
          shndx = elfcpp::SHN_ABS;
          break;
 
-       case Symbol::CONSTANT:
+       case Symbol::IS_CONSTANT:
          shndx = elfcpp::SHN_ABS;
          break;
 
+       case Symbol::IS_UNDEFINED:
+         shndx = elfcpp::SHN_UNDEF;
+         break;
+
        default:
          gold_unreachable();
        }
@@ -2023,9 +2329,10 @@ Symbol_table::warn_about_undefined_dynobj_symbol(
     const Input_objects* input_objects,
     Symbol* sym) const
 {
+  bool dummy;
   if (sym->source() == Symbol::FROM_OBJECT
       && sym->object()->is_dynamic()
-      && sym->shndx() == elfcpp::SHN_UNDEF
+      && sym->shndx(&dummy) == elfcpp::SHN_UNDEF
       && sym->binding() != elfcpp::STB_WEAK
       && !parameters->options().allow_shlib_undefined()
       && !parameters->target().is_defined_by_abi(sym)
@@ -2044,6 +2351,7 @@ Symbol_table::warn_about_undefined_dynobj_symbol(
 
 void
 Symbol_table::write_section_symbol(const Output_section *os,
+                                  Output_symtab_xindex* symtab_xindex,
                                   Output_file* of,
                                   off_t offset) const
 {
@@ -2051,22 +2359,26 @@ Symbol_table::write_section_symbol(const Output_section *os,
     {
 #ifdef HAVE_TARGET_32_LITTLE
     case Parameters::TARGET_32_LITTLE:
-      this->sized_write_section_symbol<32, false>(os, of, offset);
+      this->sized_write_section_symbol<32, false>(os, symtab_xindex, of,
+                                                 offset);
       break;
 #endif
 #ifdef HAVE_TARGET_32_BIG
     case Parameters::TARGET_32_BIG:
-      this->sized_write_section_symbol<32, true>(os, of, offset);
+      this->sized_write_section_symbol<32, true>(os, symtab_xindex, of,
+                                                offset);
       break;
 #endif
 #ifdef HAVE_TARGET_64_LITTLE
     case Parameters::TARGET_64_LITTLE:
-      this->sized_write_section_symbol<64, false>(os, of, offset);
+      this->sized_write_section_symbol<64, false>(os, symtab_xindex, of,
+                                                 offset);
       break;
 #endif
 #ifdef HAVE_TARGET_64_BIG
     case Parameters::TARGET_64_BIG:
-      this->sized_write_section_symbol<64, true>(os, of, offset);
+      this->sized_write_section_symbol<64, true>(os, symtab_xindex, of,
+                                                offset);
       break;
 #endif
     default:
@@ -2079,6 +2391,7 @@ Symbol_table::write_section_symbol(const Output_section *os,
 template<int size, bool big_endian>
 void
 Symbol_table::sized_write_section_symbol(const Output_section* os,
+                                        Output_symtab_xindex* symtab_xindex,
                                         Output_file* of,
                                         off_t offset) const
 {
@@ -2093,7 +2406,14 @@ Symbol_table::sized_write_section_symbol(const Output_section* os,
   osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
                                       elfcpp::STT_SECTION));
   osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
-  osym.put_st_shndx(os->out_shndx());
+
+  unsigned int shndx = os->out_shndx();
+  if (shndx >= elfcpp::SHN_LORESERVE)
+    {
+      symtab_xindex->add(os->symtab_index(), shndx);
+      shndx = elfcpp::SHN_XINDEX;
+    }
+  osym.put_st_shndx(shndx);
 
   of->write_output_view(offset, sym_size, pov);
 }
@@ -2166,10 +2486,12 @@ Symbol_table::detect_odr_violations(const Task* task,
          // want to run this in a general Task for better
          // performance, we will need one Task for object, plus
          // appropriate locking to ensure that we don't conflict with
-         // other uses of the object.
+         // other uses of the object.  Also note, one_addr2line is not
+          // currently thread-safe.
          Task_lock_obj<Object> tl(task, locs->object);
+          // 16 is the size of the object-cache that one_addr2line should use.
           std::string lineno = Dwarf_line_info::one_addr2line(
-              locs->object, locs->shndx, locs->offset);
+              locs->object, locs->shndx, locs->offset, 16);
           if (!lineno.empty())
             line_nums.insert(lineno);
         }
@@ -2185,6 +2507,8 @@ Symbol_table::detect_odr_violations(const Task* task,
             fprintf(stderr, "  %s\n", it2->c_str());
         }
     }
+  // We only call one_addr2line() in this function, so we can clear its cache.
+  Dwarf_line_info::clear_addr2line_cache();
 }
 
 // Warnings functions.
@@ -2257,6 +2581,7 @@ Symbol_table::add_from_relobj<32, false>(
     Sized_relobj<32, false>* relobj,
     const unsigned char* syms,
     size_t count,
+    size_t symndx_offset,
     const char* sym_names,
     size_t sym_name_size,
     Sized_relobj<32, true>::Symbols* sympointers);
@@ -2269,6 +2594,7 @@ Symbol_table::add_from_relobj<32, true>(
     Sized_relobj<32, true>* relobj,
     const unsigned char* syms,
     size_t count,
+    size_t symndx_offset,
     const char* sym_names,
     size_t sym_name_size,
     Sized_relobj<32, false>::Symbols* sympointers);
@@ -2281,6 +2607,7 @@ Symbol_table::add_from_relobj<64, false>(
     Sized_relobj<64, false>* relobj,
     const unsigned char* syms,
     size_t count,
+    size_t symndx_offset,
     const char* sym_names,
     size_t sym_name_size,
     Sized_relobj<64, true>::Symbols* sympointers);
@@ -2293,6 +2620,7 @@ Symbol_table::add_from_relobj<64, true>(
     Sized_relobj<64, true>* relobj,
     const unsigned char* syms,
     size_t count,
+    size_t symndx_offset,
     const char* sym_names,
     size_t sym_name_size,
     Sized_relobj<64, false>::Symbols* sympointers);
This page took 0.037228 seconds and 4 git commands to generate.