// dynobj.cc -- dynamic object support for gold
-// Copyright 2006, 2007 Free Software Foundation, Inc.
+// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <iant@google.com>.
// This file is part of gold.
#include "elfcpp.h"
#include "parameters.h"
+#include "script.h"
#include "symtab.h"
#include "dynobj.h"
// see a DT_SONAME entry.
Dynobj::Dynobj(const std::string& name, Input_file* input_file, off_t offset)
- : Object(name, input_file, true, offset)
+ : Object(name, input_file, true, offset),
+ needed_(),
+ unknown_needed_(UNKNOWN_NEEDED_UNSET)
{
// This will be overridden by a DT_SONAME entry, hopefully. But if
// we never see a DT_SONAME entry, our rule is to use the dynamic
}
}
-// Return the string to use in a DT_NEEDED entry.
-
-const char*
-Dynobj::soname() const
-{
- return this->soname_.c_str();
-}
-
// Class Sized_dynobj.
template<int size, bool big_endian>
elfcpp::SHT type,
unsigned int link,
File_view** view,
- off_t* view_size,
+ section_size_type* view_size,
unsigned int* view_info)
{
if (shndx == -1U)
*view = this->get_lasting_view(shdr.get_sh_offset(), shdr.get_sh_size(),
false);
- *view_size = shdr.get_sh_size();
+ *view_size = convert_to_section_size_type(shdr.get_sh_size());
*view_info = shdr.get_sh_info();
}
-// Set the soname field if this shared object has a DT_SONAME tag.
-// PSHDRS points to the section headers. DYNAMIC_SHNDX is the section
-// index of the SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and
-// STRTAB_SIZE are the section index and contents of a string table
-// which may be the one associated with the SHT_DYNAMIC section.
+// Read the dynamic tags. Set the soname field if this shared object
+// has a DT_SONAME tag. Record the DT_NEEDED tags. PSHDRS points to
+// the section headers. DYNAMIC_SHNDX is the section index of the
+// SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and STRTAB_SIZE are the
+// section index and contents of a string table which may be the one
+// associated with the SHT_DYNAMIC section.
template<int size, bool big_endian>
void
-Sized_dynobj<size, big_endian>::set_soname(const unsigned char* pshdrs,
- unsigned int dynamic_shndx,
- unsigned int strtab_shndx,
- const unsigned char* strtabu,
- off_t strtab_size)
+Sized_dynobj<size, big_endian>::read_dynamic(const unsigned char* pshdrs,
+ unsigned int dynamic_shndx,
+ unsigned int strtab_shndx,
+ const unsigned char* strtabu,
+ off_t strtab_size)
{
typename This::Shdr dynamicshdr(pshdrs + dynamic_shndx * This::shdr_size);
gold_assert(dynamicshdr.get_sh_type() == elfcpp::SHT_DYNAMIC);
strtabu = this->get_view(strtabshdr.get_sh_offset(), strtab_size, false);
}
+ const char* const strtab = reinterpret_cast<const char*>(strtabu);
+
for (const unsigned char* p = pdynamic;
p < pdynamic + dynamic_size;
p += This::dyn_size)
{
typename This::Dyn dyn(p);
- if (dyn.get_d_tag() == elfcpp::DT_SONAME)
+ switch (dyn.get_d_tag())
{
- off_t val = dyn.get_d_val();
- if (val >= strtab_size)
- {
+ case elfcpp::DT_NULL:
+ // We should always see DT_NULL at the end of the dynamic
+ // tags.
+ return;
+
+ case elfcpp::DT_SONAME:
+ {
+ off_t val = dyn.get_d_val();
+ if (val >= strtab_size)
this->error(_("DT_SONAME value out of range: %lld >= %lld"),
- static_cast<long long>(val),
- static_cast<long long>(strtab_size));
- return;
- }
+ static_cast<long long>(val),
+ static_cast<long long>(strtab_size));
+ else
+ this->set_soname_string(strtab + val);
+ }
+ break;
- const char* strtab = reinterpret_cast<const char*>(strtabu);
- this->set_soname_string(strtab + val);
- return;
- }
+ case elfcpp::DT_NEEDED:
+ {
+ off_t val = dyn.get_d_val();
+ if (val >= strtab_size)
+ this->error(_("DT_NEEDED value out of range: %lld >= %lld"),
+ static_cast<long long>(val),
+ static_cast<long long>(strtab_size));
+ else
+ this->add_needed(strtab + val);
+ }
+ break;
- if (dyn.get_d_tag() == elfcpp::DT_NULL)
- return;
+ default:
+ break;
+ }
}
this->error(_("missing DT_NULL in dynamic segment"));
sd->symbols = this->get_lasting_view(dynsymshdr.get_sh_offset(),
dynsymshdr.get_sh_size(), false);
- sd->symbols_size = dynsymshdr.get_sh_size();
+ sd->symbols_size =
+ convert_to_section_size_type(dynsymshdr.get_sh_size());
// Get the symbol names.
strtab_shndx = dynsymshdr.get_sh_link();
sd->symbol_names = this->get_lasting_view(strtabshdr.get_sh_offset(),
strtabshdr.get_sh_size(),
- true);
- sd->symbol_names_size = strtabshdr.get_sh_size();
+ false);
+ sd->symbol_names_size =
+ convert_to_section_size_type(strtabshdr.get_sh_size());
// Get the version information.
}
// Read the SHT_DYNAMIC section to find whether this shared object
- // has a DT_SONAME tag. This doesn't really have anything to do
- // with reading the symbols, but this is a convenient place to do
- // it.
+ // has a DT_SONAME tag and to record any DT_NEEDED tags. This
+ // doesn't really have anything to do with reading the symbols, but
+ // this is a convenient place to do it.
if (dynamic_shndx != -1U)
- this->set_soname(pshdrs, dynamic_shndx, strtab_shndx,
- (sd->symbol_names == NULL
- ? NULL
- : sd->symbol_names->data()),
- sd->symbol_names_size);
+ this->read_dynamic(pshdrs, dynamic_shndx, strtab_shndx,
+ (sd->symbol_names == NULL
+ ? NULL
+ : sd->symbol_names->data()),
+ sd->symbol_names_size);
}
// Lay out the input sections for a dynamic object. We don't want to
return;
const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
- off_t names_size = sd->symbol_names_size;
+ section_size_type names_size = sd->symbol_names_size;
const unsigned char* pverdef = sd->verdef->data();
- off_t verdef_size = sd->verdef_size;
+ section_size_type verdef_size = sd->verdef_size;
const unsigned int count = sd->verdef_info;
const unsigned char* p = pverdef;
return;
}
- const unsigned int vd_ndx = verdef.get_vd_ndx();
+ const section_size_type vd_ndx = verdef.get_vd_ndx();
// The GNU linker clears the VERSYM_HIDDEN bit. I'm not
// sure why.
// The first Verdaux holds the name of this version. Subsequent
// ones are versions that this one depends upon, which we don't
// care about here.
- const unsigned int vd_cnt = verdef.get_vd_cnt();
+ const section_size_type vd_cnt = verdef.get_vd_cnt();
if (vd_cnt < 1)
{
- this->error(_("verdef vd_cnt field too small: %u"), vd_cnt);
+ this->error(_("verdef vd_cnt field too small: %u"),
+ static_cast<unsigned int>(vd_cnt));
return;
}
- const unsigned int vd_aux = verdef.get_vd_aux();
+ const section_size_type vd_aux = verdef.get_vd_aux();
if ((p - pverdef) + vd_aux >= verdef_size)
{
- this->error(_("verdef vd_aux field out of range: %u"), vd_aux);
+ this->error(_("verdef vd_aux field out of range: %u"),
+ static_cast<unsigned int>(vd_aux));
return;
}
const unsigned char* pvda = p + vd_aux;
elfcpp::Verdaux<size, big_endian> verdaux(pvda);
- const unsigned int vda_name = verdaux.get_vda_name();
+ const section_size_type vda_name = verdaux.get_vda_name();
if (vda_name >= names_size)
{
- this->error(_("verdaux vda_name field out of range: %u"), vda_name);
+ this->error(_("verdaux vda_name field out of range: %u"),
+ static_cast<unsigned int>(vda_name));
return;
}
this->set_version_map(version_map, vd_ndx, names + vda_name);
- const unsigned int vd_next = verdef.get_vd_next();
+ const section_size_type vd_next = verdef.get_vd_next();
if ((p - pverdef) + vd_next >= verdef_size)
{
- this->error(_("verdef vd_next field out of range: %u"), vd_next);
+ this->error(_("verdef vd_next field out of range: %u"),
+ static_cast<unsigned int>(vd_next));
return;
}
return;
const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
- off_t names_size = sd->symbol_names_size;
+ section_size_type names_size = sd->symbol_names_size;
const unsigned char* pverneed = sd->verneed->data();
- const off_t verneed_size = sd->verneed_size;
+ const section_size_type verneed_size = sd->verneed_size;
const unsigned int count = sd->verneed_info;
const unsigned char* p = pverneed;
return;
}
- const unsigned int vn_aux = verneed.get_vn_aux();
+ const section_size_type vn_aux = verneed.get_vn_aux();
if ((p - pverneed) + vn_aux >= verneed_size)
{
- this->error(_("verneed vn_aux field out of range: %u"), vn_aux);
+ this->error(_("verneed vn_aux field out of range: %u"),
+ static_cast<unsigned int>(vn_aux));
return;
}
if (vna_name >= names_size)
{
this->error(_("vernaux vna_name field out of range: %u"),
- vna_name);
+ static_cast<unsigned int>(vna_name));
return;
}
this->set_version_map(version_map, vernaux.get_vna_other(),
names + vna_name);
- const unsigned int vna_next = vernaux.get_vna_next();
+ const section_size_type vna_next = vernaux.get_vna_next();
if ((pvna - pverneed) + vna_next >= verneed_size)
{
this->error(_("verneed vna_next field out of range: %u"),
- vna_next);
+ static_cast<unsigned int>(vna_next));
return;
}
pvna += vna_next;
}
- const unsigned int vn_next = verneed.get_vn_next();
+ const section_size_type vn_next = verneed.get_vn_next();
if ((p - pverneed) + vn_next >= verneed_size)
{
- this->error(_("verneed vn_next field out of range: %u"), vn_next);
+ this->error(_("verneed vn_next field out of range: %u"),
+ static_cast<unsigned int>(vn_next));
return;
}
const int sym_size = This::sym_size;
const size_t symcount = sd->symbols_size / sym_size;
gold_assert(sd->external_symbols_offset == 0);
- if (static_cast<off_t>(symcount * sym_size) != sd->symbols_size)
+ if (symcount * sym_size != sd->symbols_size)
{
this->error(_("size of dynamic symbols is not multiple of symbol size"));
return;
delete sd->verneed;
sd->verneed = NULL;
}
+
+ // 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
// 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->output_is_shared())
+ {
+ const char* name = dynpool->add(parameters->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();
{
gold_assert(!this->is_finalized_);
gold_assert(sym->version() != NULL);
-
+
Stringpool::Key version_key;
const char* version = dynpool->add(sym->version(), false, &version_key);
// We have now seen a symbol in this version, so it is not
// weak.
+ gold_assert(vb != NULL);
vb->clear_weak();
-
- // FIXME: When we support version scripts, we will need to
- // check whether this symbol should be forced local.
}
else
{
if (parameters->output_is_shared())
{
gold_error(_("symbol %s has undefined version %s"),
- sym->name(), version);
+ sym->demangled_name().c_str(), version);
return;
}
- // If this is the first version we are defining, first define
- // the base version. FIXME: Should use soname here when
- // creating a shared object.
- Verdef* vdbase = new Verdef(parameters->output_file_name(), true, false,
- true);
- this->defs_.push_back(vdbase);
-
// When creating a regular executable, automatically define
// a new version.
- Verdef* vd = new Verdef(version, false, false, false);
+ Verdef* vd = new Verdef(version, std::vector<std::string>(),
+ false, false, false);
this->defs_.push_back(vd);
ins.first->second = vd;
}
// each new version definition.
unsigned int
-Versions::finalize(const Target* target, Symbol_table* symtab,
- unsigned int dynsym_index, std::vector<Symbol*>* syms)
+Versions::finalize(Symbol_table* symtab, unsigned int dynsym_index,
+ std::vector<Symbol*>* syms)
{
gold_assert(!this->is_finalized_);
// Create a version symbol if necessary.
if (!(*p)->is_symbol_created())
{
- Symbol* vsym = symtab->define_as_constant(target, (*p)->name(),
+ Symbol* vsym = symtab->define_as_constant((*p)->name(),
(*p)->name(), 0, 0,
elfcpp::STT_OBJECT,
elfcpp::STB_GLOBAL,
elfcpp::STV_DEFAULT, 0,
- false);
+ false, false);
vsym->set_needs_dynsym_entry();
vsym->set_dynsym_index(dynsym_index);
++dynsym_index;
const char* version = (*p)->version();
if (version == NULL)
version_index = elfcpp::VER_NDX_GLOBAL;
- else
+ 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);
+ version_index);
}
*pp = pbuf;