+
+ // This is normally the last time we will read any data from this
+ // file.
+ this->clear_view_cache_marks();
+}
+
+// Given a vector of hash codes, compute the number of hash buckets to
+// use.
+
+unsigned int
+Dynobj::compute_bucket_count(const std::vector<uint32_t>& hashcodes,
+ bool for_gnu_hash_table)
+{
+ // FIXME: Implement optional hash table optimization.
+
+ // Array used to determine the number of hash table buckets to use
+ // based on the number of symbols there are. If there are fewer
+ // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
+ // buckets, fewer than 37 we use 17 buckets, and so forth. We never
+ // use more than 262147 buckets. This is straight from the old GNU
+ // linker.
+ static const unsigned int buckets[] =
+ {
+ 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
+ 16411, 32771, 65537, 131101, 262147
+ };
+ const int buckets_count = sizeof buckets / sizeof buckets[0];
+
+ unsigned int symcount = hashcodes.size();
+ unsigned int ret = 1;
+ const double full_fraction
+ = 1.0 - parameters->options().hash_bucket_empty_fraction();
+ for (int i = 0; i < buckets_count; ++i)
+ {
+ if (symcount < buckets[i] * full_fraction)
+ break;
+ ret = buckets[i];
+ }
+
+ if (for_gnu_hash_table && ret < 2)
+ ret = 2;
+
+ return ret;
+}
+
+// The standard ELF hash function. This hash function must not
+// change, as the dynamic linker uses it also.
+
+uint32_t
+Dynobj::elf_hash(const char* name)
+{
+ const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
+ uint32_t h = 0;
+ unsigned char c;
+ while ((c = *nameu++) != '\0')
+ {
+ h = (h << 4) + c;
+ uint32_t g = h & 0xf0000000;
+ if (g != 0)
+ {
+ h ^= g >> 24;
+ // The ELF ABI says h &= ~g, but using xor is equivalent in
+ // this case (since g was set from h) and may save one
+ // instruction.
+ h ^= g;
+ }
+ }
+ return h;
+}
+
+// Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
+// DYNSYMS is a vector with all the global dynamic symbols.
+// LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
+// symbol table.
+
+void
+Dynobj::create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
+ unsigned int local_dynsym_count,
+ unsigned char** pphash,
+ unsigned int* phashlen)
+{
+ unsigned int dynsym_count = dynsyms.size();
+
+ // Get the hash values for all the symbols.
+ std::vector<uint32_t> dynsym_hashvals(dynsym_count);
+ for (unsigned int i = 0; i < dynsym_count; ++i)
+ dynsym_hashvals[i] = Dynobj::elf_hash(dynsyms[i]->name());
+
+ const unsigned int bucketcount =
+ Dynobj::compute_bucket_count(dynsym_hashvals, false);
+
+ std::vector<uint32_t> bucket(bucketcount);
+ std::vector<uint32_t> chain(local_dynsym_count + dynsym_count);
+
+ for (unsigned int i = 0; i < dynsym_count; ++i)
+ {
+ unsigned int dynsym_index = dynsyms[i]->dynsym_index();
+ unsigned int bucketpos = dynsym_hashvals[i] % bucketcount;
+ chain[dynsym_index] = bucket[bucketpos];
+ bucket[bucketpos] = dynsym_index;
+ }
+
+ unsigned int hashlen = ((2
+ + bucketcount
+ + local_dynsym_count
+ + dynsym_count)
+ * 4);
+ unsigned char* phash = new unsigned char[hashlen];
+
+ if (parameters->target().is_big_endian())
+ {
+#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
+ Dynobj::sized_create_elf_hash_table<true>(bucket, chain, phash,
+ hashlen);
+#else
+ gold_unreachable();
+#endif
+ }
+ else
+ {
+#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
+ Dynobj::sized_create_elf_hash_table<false>(bucket, chain, phash,
+ hashlen);
+#else
+ gold_unreachable();
+#endif
+ }
+
+ *pphash = phash;
+ *phashlen = hashlen;
+}
+
+// Fill in an ELF hash table.
+
+template<bool big_endian>
+void
+Dynobj::sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
+ const std::vector<uint32_t>& chain,
+ unsigned char* phash,
+ unsigned int hashlen)
+{
+ unsigned char* p = phash;
+
+ const unsigned int bucketcount = bucket.size();
+ const unsigned int chaincount = chain.size();
+
+ elfcpp::Swap<32, big_endian>::writeval(p, bucketcount);
+ p += 4;
+ elfcpp::Swap<32, big_endian>::writeval(p, chaincount);
+ p += 4;
+
+ for (unsigned int i = 0; i < bucketcount; ++i)
+ {
+ elfcpp::Swap<32, big_endian>::writeval(p, bucket[i]);
+ p += 4;
+ }
+
+ for (unsigned int i = 0; i < chaincount; ++i)
+ {
+ elfcpp::Swap<32, big_endian>::writeval(p, chain[i]);
+ p += 4;
+ }
+
+ gold_assert(static_cast<unsigned int>(p - phash) == hashlen);
+}
+
+// The hash function used for the GNU hash table. This hash function
+// must not change, as the dynamic linker uses it also.
+
+uint32_t
+Dynobj::gnu_hash(const char* name)
+{
+ const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
+ uint32_t h = 5381;
+ unsigned char c;
+ while ((c = *nameu++) != '\0')
+ h = (h << 5) + h + c;
+ return h;
+}
+
+// Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
+// tables are an extension to ELF which are recognized by the GNU
+// dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
+// TARGET is the target. DYNSYMS is a vector with all the global
+// symbols which will be going into the dynamic symbol table.
+// LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
+// symbol table.
+
+void
+Dynobj::create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
+ unsigned int local_dynsym_count,
+ unsigned char** pphash,
+ unsigned int* phashlen)
+{
+ const unsigned int count = dynsyms.size();
+
+ // Sort the dynamic symbols into two vectors. Symbols which we do
+ // not want to put into the hash table we store into
+ // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
+ // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
+ // and records the hash codes.
+
+ std::vector<Symbol*> unhashed_dynsyms;
+ unhashed_dynsyms.reserve(count);
+
+ std::vector<Symbol*> hashed_dynsyms;
+ hashed_dynsyms.reserve(count);
+
+ std::vector<uint32_t> dynsym_hashvals;
+ dynsym_hashvals.reserve(count);
+
+ for (unsigned int i = 0; i < count; ++i)
+ {
+ Symbol* sym = dynsyms[i];
+
+ // FIXME: Should put on unhashed_dynsyms if the symbol is
+ // hidden.
+ if (sym->is_undefined())
+ unhashed_dynsyms.push_back(sym);
+ else
+ {
+ hashed_dynsyms.push_back(sym);
+ dynsym_hashvals.push_back(Dynobj::gnu_hash(sym->name()));
+ }
+ }
+
+ // Put the unhashed symbols at the start of the global portion of
+ // the dynamic symbol table.
+ const unsigned int unhashed_count = unhashed_dynsyms.size();
+ unsigned int unhashed_dynsym_index = local_dynsym_count;
+ for (unsigned int i = 0; i < unhashed_count; ++i)
+ {
+ unhashed_dynsyms[i]->set_dynsym_index(unhashed_dynsym_index);
+ ++unhashed_dynsym_index;
+ }
+
+ // For the actual data generation we call out to a templatized
+ // function.
+ int size = parameters->target().get_size();
+ bool big_endian = parameters->target().is_big_endian();
+ if (size == 32)
+ {
+ if (big_endian)
+ {
+#ifdef HAVE_TARGET_32_BIG
+ Dynobj::sized_create_gnu_hash_table<32, true>(hashed_dynsyms,
+ dynsym_hashvals,
+ unhashed_dynsym_index,
+ pphash,
+ phashlen);
+#else
+ gold_unreachable();
+#endif
+ }
+ else
+ {
+#ifdef HAVE_TARGET_32_LITTLE
+ Dynobj::sized_create_gnu_hash_table<32, false>(hashed_dynsyms,
+ dynsym_hashvals,
+ unhashed_dynsym_index,
+ pphash,
+ phashlen);
+#else
+ gold_unreachable();
+#endif
+ }
+ }
+ else if (size == 64)
+ {
+ if (big_endian)
+ {
+#ifdef HAVE_TARGET_64_BIG
+ Dynobj::sized_create_gnu_hash_table<64, true>(hashed_dynsyms,
+ dynsym_hashvals,
+ unhashed_dynsym_index,
+ pphash,
+ phashlen);
+#else
+ gold_unreachable();
+#endif
+ }
+ else
+ {
+#ifdef HAVE_TARGET_64_LITTLE
+ Dynobj::sized_create_gnu_hash_table<64, false>(hashed_dynsyms,
+ dynsym_hashvals,
+ unhashed_dynsym_index,
+ pphash,
+ phashlen);
+#else
+ gold_unreachable();
+#endif
+ }
+ }
+ else
+ gold_unreachable();
+}
+
+// Create the actual data for a GNU hash table. This is just a copy
+// of the code from the old GNU linker.
+
+template<int size, bool big_endian>
+void
+Dynobj::sized_create_gnu_hash_table(
+ const std::vector<Symbol*>& hashed_dynsyms,
+ const std::vector<uint32_t>& dynsym_hashvals,
+ unsigned int unhashed_dynsym_count,
+ unsigned char** pphash,
+ unsigned int* phashlen)
+{
+ if (hashed_dynsyms.empty())
+ {
+ // Special case for the empty hash table.
+ unsigned int hashlen = 5 * 4 + size / 8;
+ unsigned char* phash = new unsigned char[hashlen];
+ // One empty bucket.
+ elfcpp::Swap<32, big_endian>::writeval(phash, 1);
+ // Symbol index above unhashed symbols.
+ elfcpp::Swap<32, big_endian>::writeval(phash + 4, unhashed_dynsym_count);
+ // One word for bitmask.
+ elfcpp::Swap<32, big_endian>::writeval(phash + 8, 1);
+ // Only bloom filter.
+ elfcpp::Swap<32, big_endian>::writeval(phash + 12, 0);
+ // No valid hashes.
+ elfcpp::Swap<size, big_endian>::writeval(phash + 16, 0);
+ // No hashes in only bucket.
+ elfcpp::Swap<32, big_endian>::writeval(phash + 16 + size / 8, 0);
+
+ *phashlen = hashlen;
+ *pphash = phash;
+
+ return;
+ }
+
+ const unsigned int bucketcount =
+ Dynobj::compute_bucket_count(dynsym_hashvals, true);
+
+ const unsigned int nsyms = hashed_dynsyms.size();
+
+ uint32_t maskbitslog2 = 1;
+ uint32_t x = nsyms >> 1;
+ while (x != 0)
+ {
+ ++maskbitslog2;
+ x >>= 1;
+ }
+ if (maskbitslog2 < 3)
+ maskbitslog2 = 5;
+ else if (((1U << (maskbitslog2 - 2)) & nsyms) != 0)
+ maskbitslog2 += 3;
+ else
+ maskbitslog2 += 2;
+
+ uint32_t shift1;
+ if (size == 32)
+ shift1 = 5;
+ else
+ {
+ if (maskbitslog2 == 5)
+ maskbitslog2 = 6;
+ shift1 = 6;
+ }
+ uint32_t mask = (1U << shift1) - 1U;
+ uint32_t shift2 = maskbitslog2;
+ uint32_t maskbits = 1U << maskbitslog2;
+ uint32_t maskwords = 1U << (maskbitslog2 - shift1);
+
+ typedef typename elfcpp::Elf_types<size>::Elf_WXword Word;
+ std::vector<Word> bitmask(maskwords);
+ std::vector<uint32_t> counts(bucketcount);
+ std::vector<uint32_t> indx(bucketcount);
+ uint32_t symindx = unhashed_dynsym_count;
+
+ // Count the number of times each hash bucket is used.
+ for (unsigned int i = 0; i < nsyms; ++i)
+ ++counts[dynsym_hashvals[i] % bucketcount];
+
+ unsigned int cnt = symindx;
+ for (unsigned int i = 0; i < bucketcount; ++i)
+ {
+ indx[i] = cnt;
+ cnt += counts[i];
+ }
+
+ unsigned int hashlen = (4 + bucketcount + nsyms) * 4;
+ hashlen += maskbits / 8;
+ unsigned char* phash = new unsigned char[hashlen];
+
+ elfcpp::Swap<32, big_endian>::writeval(phash, bucketcount);
+ elfcpp::Swap<32, big_endian>::writeval(phash + 4, symindx);
+ elfcpp::Swap<32, big_endian>::writeval(phash + 8, maskwords);
+ elfcpp::Swap<32, big_endian>::writeval(phash + 12, shift2);
+
+ unsigned char* p = phash + 16 + maskbits / 8;
+ for (unsigned int i = 0; i < bucketcount; ++i)
+ {
+ if (counts[i] == 0)
+ elfcpp::Swap<32, big_endian>::writeval(p, 0);
+ else
+ elfcpp::Swap<32, big_endian>::writeval(p, indx[i]);
+ p += 4;
+ }
+
+ for (unsigned int i = 0; i < nsyms; ++i)
+ {
+ Symbol* sym = hashed_dynsyms[i];
+ uint32_t hashval = dynsym_hashvals[i];
+
+ unsigned int bucket = hashval % bucketcount;
+ unsigned int val = ((hashval >> shift1)
+ & ((maskbits >> shift1) - 1));
+ bitmask[val] |= (static_cast<Word>(1U)) << (hashval & mask);
+ bitmask[val] |= (static_cast<Word>(1U)) << ((hashval >> shift2) & mask);
+ val = hashval & ~ 1U;
+ if (counts[bucket] == 1)
+ {
+ // Last element terminates the chain.
+ val |= 1;
+ }
+ elfcpp::Swap<32, big_endian>::writeval(p + (indx[bucket] - symindx) * 4,
+ val);
+ --counts[bucket];
+
+ sym->set_dynsym_index(indx[bucket]);
+ ++indx[bucket];
+ }
+
+ p = phash + 16;
+ for (unsigned int i = 0; i < maskwords; ++i)
+ {
+ elfcpp::Swap<size, big_endian>::writeval(p, bitmask[i]);
+ p += size / 8;
+ }
+
+ *phashlen = hashlen;
+ *pphash = phash;
+}
+
+// Verdef methods.
+
+// Write this definition to a buffer for the output section.
+
+template<int size, bool big_endian>
+unsigned char*
+Verdef::write(const Stringpool* dynpool, bool is_last, unsigned char* pb) const
+{
+ const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
+ const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
+
+ elfcpp::Verdef_write<size, big_endian> vd(pb);
+ vd.set_vd_version(elfcpp::VER_DEF_CURRENT);
+ vd.set_vd_flags((this->is_base_ ? elfcpp::VER_FLG_BASE : 0)
+ | (this->is_weak_ ? elfcpp::VER_FLG_WEAK : 0));
+ vd.set_vd_ndx(this->index());
+ vd.set_vd_cnt(1 + this->deps_.size());
+ vd.set_vd_hash(Dynobj::elf_hash(this->name()));
+ vd.set_vd_aux(verdef_size);
+ vd.set_vd_next(is_last
+ ? 0
+ : verdef_size + (1 + this->deps_.size()) * verdaux_size);
+ pb += verdef_size;
+
+ elfcpp::Verdaux_write<size, big_endian> vda(pb);
+ vda.set_vda_name(dynpool->get_offset(this->name()));
+ vda.set_vda_next(this->deps_.empty() ? 0 : verdaux_size);
+ pb += verdaux_size;
+
+ Deps::const_iterator p;
+ unsigned int i;
+ for (p = this->deps_.begin(), i = 0;
+ p != this->deps_.end();
+ ++p, ++i)
+ {
+ elfcpp::Verdaux_write<size, big_endian> vda(pb);
+ vda.set_vda_name(dynpool->get_offset(*p));
+ vda.set_vda_next(i + 1 >= this->deps_.size() ? 0 : verdaux_size);
+ pb += verdaux_size;
+ }
+
+ return pb;
+}
+
+// Verneed methods.
+
+Verneed::~Verneed()
+{
+ for (Need_versions::iterator p = this->need_versions_.begin();
+ p != this->need_versions_.end();
+ ++p)
+ delete *p;
+}
+
+// Add a new version to this file reference.
+
+Verneed_version*
+Verneed::add_name(const char* name)
+{
+ Verneed_version* vv = new Verneed_version(name);
+ this->need_versions_.push_back(vv);
+ return vv;
+}
+
+// Set the version indexes starting at INDEX.
+
+unsigned int
+Verneed::finalize(unsigned int index)
+{
+ for (Need_versions::iterator p = this->need_versions_.begin();
+ p != this->need_versions_.end();
+ ++p)
+ {
+ (*p)->set_index(index);
+ ++index;
+ }
+ return index;
+}
+
+// Write this list of referenced versions to a buffer for the output
+// section.
+
+template<int size, bool big_endian>
+unsigned char*
+Verneed::write(const Stringpool* dynpool, bool is_last,
+ unsigned char* pb) const
+{
+ const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
+ const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
+
+ elfcpp::Verneed_write<size, big_endian> vn(pb);
+ vn.set_vn_version(elfcpp::VER_NEED_CURRENT);
+ vn.set_vn_cnt(this->need_versions_.size());
+ vn.set_vn_file(dynpool->get_offset(this->filename()));
+ vn.set_vn_aux(verneed_size);
+ vn.set_vn_next(is_last
+ ? 0
+ : verneed_size + this->need_versions_.size() * vernaux_size);
+ pb += verneed_size;
+
+ Need_versions::const_iterator p;
+ unsigned int i;
+ for (p = this->need_versions_.begin(), i = 0;
+ p != this->need_versions_.end();
+ ++p, ++i)
+ {
+ elfcpp::Vernaux_write<size, big_endian> vna(pb);
+ vna.set_vna_hash(Dynobj::elf_hash((*p)->version()));
+ // FIXME: We need to sometimes set VER_FLG_WEAK here.
+ vna.set_vna_flags(0);
+ vna.set_vna_other((*p)->index());
+ vna.set_vna_name(dynpool->get_offset((*p)->version()));
+ vna.set_vna_next(i + 1 >= this->need_versions_.size()
+ ? 0
+ : vernaux_size);
+ pb += vernaux_size;
+ }
+
+ return pb;
+}
+
+// Versions methods.
+
+Versions::Versions(const Version_script_info& version_script,
+ Stringpool* dynpool)
+ : defs_(), needs_(), version_table_(),
+ is_finalized_(false), version_script_(version_script)
+{
+ // We always need a base version, so define that first. Nothing
+ // explicitly declares itself as part of base, so it doesn't need to
+ // be in version_table_.
+ // FIXME: Should use soname here when creating a shared object. Is
+ // this fixme still valid? It looks like it's doing the right thing
+ // to me.
+ if (parameters->options().shared())
+ {
+ const char* name = dynpool->add(parameters->options().output_file_name(),
+ false, NULL);
+ Verdef* vdbase = new Verdef(name, std::vector<std::string>(),
+ true, false, true);
+ this->defs_.push_back(vdbase);
+ }
+
+ if (!this->version_script_.empty())
+ {
+ // Parse the version script, and insert each declared version into
+ // defs_ and version_table_.
+ std::vector<std::string> versions = this->version_script_.get_versions();
+ for (size_t k = 0; k < versions.size(); ++k)
+ {
+ Stringpool::Key version_key;
+ const char* version = dynpool->add(versions[k].c_str(),
+ true, &version_key);
+ Verdef* const vd = new Verdef(
+ version,
+ this->version_script_.get_dependencies(version),
+ false, false, false);
+ this->defs_.push_back(vd);
+ Key key(version_key, 0);
+ this->version_table_.insert(std::make_pair(key, vd));
+ }
+ }
+}
+
+Versions::~Versions()
+{
+ for (Defs::iterator p = this->defs_.begin();
+ p != this->defs_.end();
+ ++p)
+ delete *p;
+
+ for (Needs::iterator p = this->needs_.begin();
+ p != this->needs_.end();
+ ++p)
+ delete *p;
+}
+
+// Return the dynamic object which a symbol refers to.
+
+Dynobj*
+Versions::get_dynobj_for_sym(const Symbol_table* symtab,
+ const Symbol* sym) const
+{
+ if (sym->is_copied_from_dynobj())
+ return symtab->get_copy_source(sym);
+ else
+ {
+ Object* object = sym->object();
+ gold_assert(object->is_dynamic());
+ return static_cast<Dynobj*>(object);
+ }
+}
+
+// Record version information for a symbol going into the dynamic
+// symbol table.
+
+void
+Versions::record_version(const Symbol_table* symtab,
+ Stringpool* dynpool, const Symbol* sym)
+{
+ gold_assert(!this->is_finalized_);
+ gold_assert(sym->version() != NULL);
+
+ Stringpool::Key version_key;
+ const char* version = dynpool->add(sym->version(), false, &version_key);
+
+ if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
+ {
+ if (parameters->options().shared())
+ this->add_def(sym, version, version_key);
+ }
+ else
+ {
+ // This is a version reference.
+ Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
+ this->add_need(dynpool, dynobj->soname(), version, version_key);
+ }
+}
+
+// We've found a symbol SYM defined in version VERSION.
+
+void
+Versions::add_def(const Symbol* sym, const char* version,
+ Stringpool::Key version_key)
+{
+ Key k(version_key, 0);
+ Version_base* const vbnull = NULL;
+ std::pair<Version_table::iterator, bool> ins =
+ this->version_table_.insert(std::make_pair(k, vbnull));
+
+ if (!ins.second)
+ {
+ // We already have an entry for this version.
+ Version_base* vb = ins.first->second;
+
+ // We have now seen a symbol in this version, so it is not
+ // weak.
+ gold_assert(vb != NULL);
+ vb->clear_weak();
+ }
+ else
+ {
+ // If we are creating a shared object, it is an error to
+ // find a definition of a symbol with a version which is not
+ // in the version script.
+ if (parameters->options().shared())
+ {
+ gold_error(_("symbol %s has undefined version %s"),
+ sym->demangled_name().c_str(), version);
+ return;
+ }
+
+ // When creating a regular executable, automatically define
+ // a new version.
+ Verdef* vd = new Verdef(version, std::vector<std::string>(),
+ false, false, false);
+ this->defs_.push_back(vd);
+ ins.first->second = vd;
+ }
+}
+
+// Add a reference to version NAME in file FILENAME.
+
+void
+Versions::add_need(Stringpool* dynpool, const char* filename, const char* name,
+ Stringpool::Key name_key)
+{
+ Stringpool::Key filename_key;
+ filename = dynpool->add(filename, true, &filename_key);
+
+ Key k(name_key, filename_key);
+ Version_base* const vbnull = NULL;
+ std::pair<Version_table::iterator, bool> ins =
+ this->version_table_.insert(std::make_pair(k, vbnull));
+
+ if (!ins.second)
+ {
+ // We already have an entry for this filename/version.
+ return;
+ }
+
+ // See whether we already have this filename. We don't expect many
+ // version references, so we just do a linear search. This could be
+ // replaced by a hash table.
+ Verneed* vn = NULL;
+ for (Needs::iterator p = this->needs_.begin();
+ p != this->needs_.end();
+ ++p)
+ {
+ if ((*p)->filename() == filename)
+ {
+ vn = *p;
+ break;
+ }
+ }
+
+ if (vn == NULL)
+ {
+ // We have a new filename.
+ vn = new Verneed(filename);
+ this->needs_.push_back(vn);
+ }
+
+ ins.first->second = vn->add_name(name);
+}
+
+// Set the version indexes. Create a new dynamic version symbol for
+// each new version definition.
+
+unsigned int
+Versions::finalize(Symbol_table* symtab, unsigned int dynsym_index,
+ std::vector<Symbol*>* syms)
+{
+ gold_assert(!this->is_finalized_);
+
+ unsigned int vi = 1;
+
+ for (Defs::iterator p = this->defs_.begin();
+ p != this->defs_.end();
+ ++p)
+ {
+ (*p)->set_index(vi);
+ ++vi;
+
+ // Create a version symbol if necessary.
+ if (!(*p)->is_symbol_created())
+ {
+ Symbol* vsym = symtab->define_as_constant((*p)->name(),
+ (*p)->name(), 0, 0,
+ elfcpp::STT_OBJECT,
+ elfcpp::STB_GLOBAL,
+ elfcpp::STV_DEFAULT, 0,
+ false, false);
+ vsym->set_needs_dynsym_entry();
+ vsym->set_dynsym_index(dynsym_index);
+ ++dynsym_index;
+ syms->push_back(vsym);
+ // The name is already in the dynamic pool.
+ }
+ }
+
+ // Index 1 is used for global symbols.
+ if (vi == 1)
+ {
+ gold_assert(this->defs_.empty());
+ vi = 2;
+ }
+
+ for (Needs::iterator p = this->needs_.begin();
+ p != this->needs_.end();
+ ++p)
+ vi = (*p)->finalize(vi);
+
+ this->is_finalized_ = true;
+
+ return dynsym_index;
+}
+
+// Return the version index to use for a symbol. This does two hash
+// table lookups: one in DYNPOOL and one in this->version_table_.
+// Another approach alternative would be store a pointer in SYM, which
+// would increase the size of the symbol table. Or perhaps we could
+// use a hash table from dynamic symbol pointer values to Version_base
+// pointers.
+
+unsigned int
+Versions::version_index(const Symbol_table* symtab, const Stringpool* dynpool,
+ const Symbol* sym) const
+{
+ Stringpool::Key version_key;
+ const char* version = dynpool->find(sym->version(), &version_key);
+ gold_assert(version != NULL);
+
+ Key k;
+ if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
+ {
+ if (!parameters->options().shared())
+ return elfcpp::VER_NDX_GLOBAL;
+ k = Key(version_key, 0);
+ }
+ else
+ {
+ Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
+
+ Stringpool::Key filename_key;
+ const char* filename = dynpool->find(dynobj->soname(), &filename_key);
+ gold_assert(filename != NULL);
+
+ k = Key(version_key, filename_key);
+ }
+
+ Version_table::const_iterator p = this->version_table_.find(k);
+ gold_assert(p != this->version_table_.end());
+
+ return p->second->index();
+}
+
+// Return an allocated buffer holding the contents of the symbol
+// version section.
+
+template<int size, bool big_endian>
+void
+Versions::symbol_section_contents(const Symbol_table* symtab,
+ const Stringpool* dynpool,
+ unsigned int local_symcount,
+ const std::vector<Symbol*>& syms,
+ unsigned char** pp,
+ unsigned int* psize) const
+{
+ gold_assert(this->is_finalized_);
+
+ unsigned int sz = (local_symcount + syms.size()) * 2;
+ unsigned char* pbuf = new unsigned char[sz];
+
+ for (unsigned int i = 0; i < local_symcount; ++i)
+ elfcpp::Swap<16, big_endian>::writeval(pbuf + i * 2,
+ elfcpp::VER_NDX_LOCAL);
+
+ for (std::vector<Symbol*>::const_iterator p = syms.begin();
+ p != syms.end();
+ ++p)
+ {
+ unsigned int version_index;
+ const char* version = (*p)->version();
+ if (version == NULL)
+ version_index = elfcpp::VER_NDX_GLOBAL;
+ else
+ version_index = this->version_index(symtab, dynpool, *p);
+ // If the symbol was defined as foo@V1 instead of foo@@V1, add
+ // the hidden bit.
+ if ((*p)->version() != NULL && !(*p)->is_default())
+ version_index |= elfcpp::VERSYM_HIDDEN;
+ elfcpp::Swap<16, big_endian>::writeval(pbuf + (*p)->dynsym_index() * 2,
+ version_index);
+ }
+
+ *pp = pbuf;
+ *psize = sz;
+}
+
+// Return an allocated buffer holding the contents of the version
+// definition section.
+
+template<int size, bool big_endian>
+void
+Versions::def_section_contents(const Stringpool* dynpool,
+ unsigned char** pp, unsigned int* psize,
+ unsigned int* pentries) const
+{
+ gold_assert(this->is_finalized_);
+ gold_assert(!this->defs_.empty());
+
+ const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
+ const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
+
+ unsigned int sz = 0;
+ for (Defs::const_iterator p = this->defs_.begin();
+ p != this->defs_.end();
+ ++p)
+ {
+ sz += verdef_size + verdaux_size;
+ sz += (*p)->count_dependencies() * verdaux_size;
+ }
+
+ unsigned char* pbuf = new unsigned char[sz];
+
+ unsigned char* pb = pbuf;
+ Defs::const_iterator p;
+ unsigned int i;
+ for (p = this->defs_.begin(), i = 0;
+ p != this->defs_.end();
+ ++p, ++i)
+ pb = (*p)->write<size, big_endian>(dynpool,
+ i + 1 >= this->defs_.size(),
+ pb);
+
+ gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
+
+ *pp = pbuf;
+ *psize = sz;
+ *pentries = this->defs_.size();
+}
+
+// Return an allocated buffer holding the contents of the version
+// reference section.
+
+template<int size, bool big_endian>
+void
+Versions::need_section_contents(const Stringpool* dynpool,
+ unsigned char** pp, unsigned int *psize,
+ unsigned int *pentries) const
+{
+ gold_assert(this->is_finalized_);
+ gold_assert(!this->needs_.empty());
+
+ const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
+ const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
+
+ unsigned int sz = 0;
+ for (Needs::const_iterator p = this->needs_.begin();
+ p != this->needs_.end();
+ ++p)
+ {
+ sz += verneed_size;
+ sz += (*p)->count_versions() * vernaux_size;
+ }
+
+ unsigned char* pbuf = new unsigned char[sz];
+
+ unsigned char* pb = pbuf;
+ Needs::const_iterator p;
+ unsigned int i;
+ for (p = this->needs_.begin(), i = 0;
+ p != this->needs_.end();
+ ++p, ++i)
+ pb = (*p)->write<size, big_endian>(dynpool,
+ i + 1 >= this->needs_.size(),
+ pb);
+
+ gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
+
+ *pp = pbuf;
+ *psize = sz;
+ *pentries = this->needs_.size();