#ifndef GOLD_DYNOBJ_H
#define GOLD_DYNOBJ_H
#include <vector>
#include "stringpool.h"
#include "object.h"
namespace gold
{
class Version_script_info;
class Dynobj : public Object
{
public:
typedef std::vector<std::string> Needed;
Dynobj(const std::string& name, Input_file* input_file, off_t offset = 0);
const char*
soname() const
{ return this->soname_.c_str(); }
const Needed&
needed() const
{ return this->needed_; }
bool
has_unknown_needed_entries() const
{
gold_assert(this->unknown_needed_ != UNKNOWN_NEEDED_UNSET);
return this->unknown_needed_ == UNKNOWN_NEEDED_TRUE;
}
void
set_has_unknown_needed_entries(bool set)
{
gold_assert(this->unknown_needed_ == UNKNOWN_NEEDED_UNSET);
this->unknown_needed_ = set ? UNKNOWN_NEEDED_TRUE : UNKNOWN_NEEDED_FALSE;
}
static uint32_t
elf_hash(const char*);
static void
create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
unsigned int local_dynsym_count,
unsigned char** pphash,
unsigned int* phashlen);
static void
create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
unsigned int local_dynsym_count,
unsigned char** pphash, unsigned int* phashlen);
protected:
void
set_soname_string(const char* s)
{ this->soname_.assign(s); }
void
add_needed(const char* s)
{ this->needed_.push_back(std::string(s)); }
private:
static uint32_t
gnu_hash(const char*);
static unsigned int
compute_bucket_count(const std::vector<uint32_t>& hashcodes,
bool for_gnu_hash_table);
template<bool big_endian>
static void
sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
const std::vector<uint32_t>& chain,
unsigned char* phash,
unsigned int hashlen);
template<int size, bool big_endian>
static void
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);
enum Unknown_needed
{
UNKNOWN_NEEDED_UNSET,
UNKNOWN_NEEDED_TRUE,
UNKNOWN_NEEDED_FALSE
};
std::string soname_;
Needed needed_;
Unknown_needed unknown_needed_;
};
template<int size, bool big_endian>
class Sized_dynobj : public Dynobj
{
public:
typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
Sized_dynobj(const std::string& name, Input_file* input_file, off_t offset,
const typename elfcpp::Ehdr<size, big_endian>&);
void
setup(const typename elfcpp::Ehdr<size, big_endian>&);
void
do_read_symbols(Read_symbols_data*);
void
do_layout(Symbol_table*, Layout*, Read_symbols_data*);
void
do_add_symbols(Symbol_table*, Read_symbols_data*);
uint64_t
do_section_size(unsigned int shndx)
{ return this->elf_file_.section_size(shndx); }
std::string
do_section_name(unsigned int shndx)
{ return this->elf_file_.section_name(shndx); }
Object::Location
do_section_contents(unsigned int shndx)
{ return this->elf_file_.section_contents(shndx); }
uint64_t
do_section_flags(unsigned int shndx)
{ return this->elf_file_.section_flags(shndx); }
uint64_t
do_section_address(unsigned int shndx)
{ return this->elf_file_.section_addr(shndx); }
unsigned int
do_section_type(unsigned int shndx)
{ return this->elf_file_.section_type(shndx); }
unsigned int
do_section_link(unsigned int shndx)
{ return this->elf_file_.section_link(shndx); }
unsigned int
do_section_info(unsigned int shndx)
{ return this->elf_file_.section_info(shndx); }
uint64_t
do_section_addralign(unsigned int shndx)
{ return this->elf_file_.section_addralign(shndx); }
Xindex*
do_initialize_xindex();
void
do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
private:
typedef Sized_dynobj<size, big_endian> This;
static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
typedef elfcpp::Shdr<size, big_endian> Shdr;
typedef elfcpp::Dyn<size, big_endian> Dyn;
unsigned int
adjust_shndx(unsigned int shndx)
{
if (shndx >= elfcpp::SHN_LORESERVE)
shndx += this->elf_file_.large_shndx_offset();
return shndx;
}
void
find_dynsym_sections(const unsigned char* pshdrs,
unsigned int* pversym_shndx,
unsigned int* pverdef_shndx,
unsigned int* pverneed_shndx,
unsigned int* pdynamic_shndx);
void
read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx,
elfcpp::SHT type, unsigned int link,
File_view** view, section_size_type* view_size,
unsigned int* view_info);
void
read_dynamic(const unsigned char* pshdrs, unsigned int dynamic_shndx,
unsigned int strtab_shndx, const unsigned char* strtabu,
off_t strtab_size);
typedef std::vector<const char*> Version_map;
void
make_version_map(Read_symbols_data* sd, Version_map*) const;
void
make_verdef_map(Read_symbols_data* sd, Version_map*) const;
void
make_verneed_map(Read_symbols_data* sd, Version_map*) const;
void
set_version_map(Version_map*, unsigned int ndx, const char* name) const;
elfcpp::Elf_file<size, big_endian, Object> elf_file_;
unsigned int dynsym_shndx_;
Symbols* symbols_;
size_t defined_count_;
};
class Version_base
{
public:
Version_base()
: index_(-1U)
{ }
virtual
~Version_base()
{ }
unsigned int
index() const
{
gold_assert(this->index_ != -1U);
return this->index_;
}
void
set_index(unsigned int index)
{
gold_assert(this->index_ == -1U);
this->index_ = index;
}
virtual void
clear_weak() = 0;
private:
Version_base(const Version_base&);
Version_base& operator=(const Version_base&);
unsigned int index_;
};
class Verdef : public Version_base
{
public:
Verdef(const char* name, const std::vector<std::string>& deps,
bool is_base, bool is_weak, bool is_symbol_created)
: name_(name), deps_(deps), is_base_(is_base), is_weak_(is_weak),
is_symbol_created_(is_symbol_created)
{ }
const char*
name() const
{ return this->name_; }
unsigned int
count_dependencies() const
{ return this->deps_.size(); }
void
add_dependency(const char* name)
{ this->deps_.push_back(name); }
bool
is_weak() const
{ return this->is_weak_; }
void
clear_weak()
{ this->is_weak_ = false; }
bool
is_symbol_created() const
{ return this->is_symbol_created_; }
template<int size, bool big_endian>
unsigned char*
write(const Stringpool*, bool is_last, unsigned char*) const;
private:
Verdef(const Verdef&);
Verdef& operator=(const Verdef&);
typedef std::vector<std::string> Deps;
const char* name_;
Deps deps_;
bool is_base_;
bool is_weak_;
bool is_symbol_created_;
};
class Verneed_version : public Version_base
{
public:
Verneed_version(const char* version)
: version_(version)
{ }
const char*
version() const
{ return this->version_; }
void
clear_weak()
{ gold_unreachable(); }
private:
Verneed_version(const Verneed_version&);
Verneed_version& operator=(const Verneed_version&);
const char* version_;
};
class Verneed
{
public:
Verneed(const char* filename)
: filename_(filename), need_versions_()
{ }
~Verneed();
const char*
filename() const
{ return this->filename_; }
unsigned int
count_versions() const
{ return this->need_versions_.size(); }
Verneed_version*
add_name(const char* name);
unsigned int
finalize(unsigned int index);
template<int size, bool big_endian>
unsigned char*
write(const Stringpool*, bool is_last, unsigned char*) const;
private:
Verneed(const Verneed&);
Verneed& operator=(const Verneed&);
typedef std::vector<Verneed_version*> Need_versions;
const char* filename_;
Need_versions need_versions_;
};
class Versions
{
public:
Versions(const Version_script_info&, Stringpool*);
~Versions();
void
record_version(const Symbol_table* symtab, Stringpool*, const Symbol* sym);
unsigned int
finalize(Symbol_table* symtab, unsigned int dynsym_index,
std::vector<Symbol*>* syms);
bool
any_defs() const
{ return !this->defs_.empty(); }
bool
any_needs() const
{ return !this->needs_.empty(); }
template<int size, bool big_endian>
void
symbol_section_contents(const Symbol_table*, const Stringpool*,
unsigned int local_symcount,
const std::vector<Symbol*>& syms,
unsigned char**, unsigned int*) const;
template<int size, bool big_endian>
void
def_section_contents(const Stringpool*, unsigned char**,
unsigned int* psize, unsigned int* pentries) const;
template<int size, bool big_endian>
void
need_section_contents(const Stringpool*, unsigned char**,
unsigned int* psize, unsigned int* pentries) const;
const Version_script_info&
version_script() const
{ return this->version_script_; }
private:
Versions(const Versions&);
Versions& operator=(const Versions&);
typedef std::vector<Verdef*> Defs;
typedef std::vector<Verneed*> Needs;
void
add_def(const Symbol* sym, const char* version, Stringpool::Key);
void
add_need(Stringpool*, const char* filename, const char* name,
Stringpool::Key);
Dynobj*
get_dynobj_for_sym(const Symbol_table*, const Symbol* sym) const;
unsigned int
version_index(const Symbol_table*, const Stringpool*,
const Symbol* sym) const;
typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
struct Version_table_hash
{
size_t
operator()(const Key& k) const
{ return k.first + k.second; }
};
struct Version_table_eq
{
bool
operator()(const Key& k1, const Key& k2) const
{ return k1.first == k2.first && k1.second == k2.second; }
};
typedef Unordered_map<Key, Version_base*, Version_table_hash,
Version_table_eq> Version_table;
Defs defs_;
Needs needs_;
Version_table version_table_;
bool is_finalized_;
const Version_script_info& version_script_;
};
}
#endif