#ifndef GOLD_OBJECT_H
#define GOLD_OBJECT_H
#include <string>
#include <vector>
#include "elfcpp.h"
#include "elfcpp_file.h"
#include "fileread.h"
#include "target.h"
namespace gold
{
class General_options;
class Task;
class Cref;
class Archive;
class Layout;
class Output_section;
class Output_file;
class Output_symtab_xindex;
class Dynobj;
class Object_merge_map;
class Relocatable_relocs;
template<typename Stringpool_char>
class Stringpool_template;
struct Read_symbols_data
{
File_view* section_headers;
File_view* section_names;
section_size_type section_names_size;
File_view* symbols;
section_size_type symbols_size;
section_offset_type external_symbols_offset;
File_view* symbol_names;
section_size_type symbol_names_size;
File_view* versym;
section_size_type versym_size;
File_view* verdef;
section_size_type verdef_size;
unsigned int verdef_info;
File_view* verneed;
section_size_type verneed_size;
unsigned int verneed_info;
};
struct Symbol_location_info
{
std::string source_file;
std::string enclosing_symbol_name;
int line_number;
};
struct Section_relocs
{
unsigned int reloc_shndx;
unsigned int data_shndx;
File_view* contents;
unsigned int sh_type;
size_t reloc_count;
Output_section* output_section;
bool needs_special_offset_handling;
bool is_data_section_allocated;
};
struct Read_relocs_data
{
typedef std::vector<Section_relocs> Relocs_list;
Relocs_list relocs;
File_view* local_symbols;
};
class Xindex
{
public:
Xindex(int large_shndx_offset)
: large_shndx_offset_(large_shndx_offset), symtab_xindex_()
{ }
template<int size, bool big_endian>
void
initialize_symtab_xindex(Object*, unsigned int symtab_shndx);
template<int size, bool big_endian>
void
read_symtab_xindex(Object*, unsigned int xindex_shndx,
const unsigned char* pshdrs);
unsigned int
sym_xindex_to_shndx(Object* object, unsigned int symndx);
private:
typedef std::vector<unsigned int> Symtab_xindex;
unsigned int
adjust_shndx(unsigned int shndx)
{
if (shndx >= elfcpp::SHN_LORESERVE)
shndx += this->large_shndx_offset_;
return shndx;
}
int large_shndx_offset_;
Symtab_xindex symtab_xindex_;
};
class Object
{
public:
Object(const std::string& name, Input_file* input_file, bool is_dynamic,
off_t offset = 0)
: name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
is_dynamic_(is_dynamic), target_(NULL), xindex_(NULL)
{ input_file->file().add_object(); }
virtual ~Object()
{ this->input_file_->file().remove_object(); }
const std::string&
name() const
{ return this->name_; }
off_t
offset() const
{ return this->offset_; }
bool
is_dynamic() const
{ return this->is_dynamic_; }
Target*
target() const
{ return this->target_; }
void
lock(const Task* t)
{ this->input_file()->file().lock(t); }
void
unlock(const Task* t)
{ this->input_file()->file().unlock(t); }
bool
is_locked() const
{ return this->input_file()->file().is_locked(); }
Task_token*
token()
{ return this->input_file()->file().token(); }
void
release()
{ this->input_file_->file().release(); }
bool
just_symbols() const
{ return this->input_file()->just_symbols(); }
template<int size, bool big_endian>
Sized_target<size, big_endian>*
sized_target() const;
unsigned int
shnum() const
{ return this->shnum_; }
const unsigned char*
section_contents(unsigned int shndx, section_size_type* plen, bool cache);
unsigned int
adjust_sym_shndx(unsigned int symndx, unsigned int shndx, bool* is_ordinary)
{
if (shndx < elfcpp::SHN_LORESERVE)
*is_ordinary = true;
else if (shndx == elfcpp::SHN_XINDEX)
{
if (this->xindex_ == NULL)
this->xindex_ = this->do_initialize_xindex();
shndx = this->xindex_->sym_xindex_to_shndx(this, symndx);
*is_ordinary = true;
}
else
*is_ordinary = false;
return shndx;
}
uint64_t
section_size(unsigned int shndx)
{ return this->do_section_size(shndx); }
std::string
section_name(unsigned int shndx)
{ return this->do_section_name(shndx); }
uint64_t
section_flags(unsigned int shndx)
{ return this->do_section_flags(shndx); }
uint64_t
section_address(unsigned int shndx)
{ return this->do_section_address(shndx); }
unsigned int
section_type(unsigned int shndx)
{ return this->do_section_type(shndx); }
unsigned int
section_link(unsigned int shndx)
{ return this->do_section_link(shndx); }
unsigned int
section_info(unsigned int shndx)
{ return this->do_section_info(shndx); }
uint64_t
section_addralign(unsigned int shndx)
{ return this->do_section_addralign(shndx); }
void
read_symbols(Read_symbols_data* sd)
{ return this->do_read_symbols(sd); }
void
layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
{ this->do_layout(symtab, layout, sd); }
void
add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
{ this->do_add_symbols(symtab, sd); }
class View
{
public:
View(const unsigned char* p)
: p_(p)
{ }
const unsigned char*
data() const
{ return this->p_; }
private:
const unsigned char* p_;
};
View
view(off_t file_offset, section_size_type data_size)
{ return View(this->get_view(file_offset, data_size, true, true)); }
void
error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
struct Location
{
off_t file_offset;
off_t data_size;
Location(off_t fo, section_size_type ds)
: file_offset(fo), data_size(ds)
{ }
};
View view(Location loc)
{ return View(this->get_view(loc.file_offset, loc.data_size, true, true)); }
const unsigned char*
get_view(off_t start, section_size_type size, bool aligned, bool cache)
{
return this->input_file()->file().get_view(this->offset_, start, size,
aligned, cache);
}
File_view*
get_lasting_view(off_t start, section_size_type size, bool aligned,
bool cache)
{
return this->input_file()->file().get_lasting_view(this->offset_, start,
size, aligned, cache);
}
void
read(off_t start, section_size_type size, void* p)
{ this->input_file()->file().read(start + this->offset_, size, p); }
void
read_multiple(const File_read::Read_multiple& rm)
{ this->input_file()->file().read_multiple(this->offset_, rm); }
void
clear_view_cache_marks()
{ this->input_file()->file().clear_view_cache_marks(); }
void
get_global_symbol_counts(const Symbol_table* symtab, size_t* defined,
size_t* used) const
{ this->do_get_global_symbol_counts(symtab, defined, used); }
protected:
virtual void
do_read_symbols(Read_symbols_data*) = 0;
virtual void
do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
virtual void
do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
virtual Location
do_section_contents(unsigned int shndx) = 0;
virtual uint64_t
do_section_size(unsigned int shndx) = 0;
virtual std::string
do_section_name(unsigned int shndx) = 0;
virtual uint64_t
do_section_flags(unsigned int shndx) = 0;
virtual uint64_t
do_section_address(unsigned int shndx) = 0;
virtual unsigned int
do_section_type(unsigned int shndx) = 0;
virtual unsigned int
do_section_link(unsigned int shndx) = 0;
virtual unsigned int
do_section_info(unsigned int shndx) = 0;
virtual uint64_t
do_section_addralign(unsigned int shndx) = 0;
virtual Xindex*
do_initialize_xindex() = 0;
virtual void
do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const = 0;
Input_file*
input_file()
{ return this->input_file_; }
const Input_file*
input_file() const
{ return this->input_file_; }
void
set_target(int machine, int size, bool big_endian, int osabi,
int abiversion);
void
set_shnum(int shnum)
{ this->shnum_ = shnum; }
template<int size, bool big_endian>
void
read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
Read_symbols_data*);
void
set_xindex(Xindex* xindex)
{
gold_assert(this->xindex_ == NULL);
this->xindex_ = xindex;
}
bool
handle_gnu_warning_section(const char* name, unsigned int shndx,
Symbol_table*);
private:
Object(const Object&);
Object& operator=(const Object&);
std::string name_;
Input_file* input_file_;
off_t offset_;
unsigned int shnum_;
bool is_dynamic_;
Target* target_;
Xindex* xindex_;
};
template<int size, bool big_endian>
inline Sized_target<size, big_endian>*
Object::sized_target() const
{
gold_assert(this->target_->get_size() == size);
gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
return static_cast<Sized_target<size, big_endian>*>(this->target_);
}
class Relobj : public Object
{
public:
Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
: Object(name, input_file, false, offset),
output_sections_(),
map_to_relocatable_relocs_(NULL),
object_merge_map_(NULL),
relocs_must_follow_section_writes_(false)
{ }
void
read_relocs(Read_relocs_data* rd)
{ return this->do_read_relocs(rd); }
void
scan_relocs(const General_options& options, Symbol_table* symtab,
Layout* layout, Read_relocs_data* rd)
{ return this->do_scan_relocs(options, symtab, layout, rd); }
virtual unsigned int
local_symbol_count() const
{ return this->do_local_symbol_count(); }
void
count_local_symbols(Stringpool_template<char>* pool,
Stringpool_template<char>* dynpool)
{ return this->do_count_local_symbols(pool, dynpool); }
unsigned int
finalize_local_symbols(unsigned int index, off_t off)
{ return this->do_finalize_local_symbols(index, off); }
unsigned int
set_local_dynsym_indexes(unsigned int index)
{ return this->do_set_local_dynsym_indexes(index); }
unsigned int
set_local_dynsym_offset(off_t off)
{ return this->do_set_local_dynsym_offset(off); }
void
relocate(const General_options& options, const Symbol_table* symtab,
const Layout* layout, Output_file* of)
{ return this->do_relocate(options, symtab, layout, of); }
bool
is_section_included(unsigned int shndx) const
{
gold_assert(shndx < this->output_sections_.size());
return this->output_sections_[shndx] != NULL;
}
Output_section*
output_section(unsigned int shndx) const
{
gold_assert(shndx < this->output_sections_.size());
return this->output_sections_[shndx];
}
uint64_t
output_section_offset(unsigned int shndx) const
{ return this->do_output_section_offset(shndx); }
virtual void
set_section_offset(unsigned int shndx, uint64_t off)
{ this->do_set_section_offset(shndx, off); }
bool
relocs_must_follow_section_writes() const
{ return this->relocs_must_follow_section_writes_; }
Object_merge_map*
merge_map() const
{ return this->object_merge_map_; }
void
set_merge_map(Object_merge_map* object_merge_map)
{
gold_assert(this->object_merge_map_ == NULL);
this->object_merge_map_ = object_merge_map;
}
void
set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr)
{
gold_assert(reloc_shndx < this->shnum());
(*this->map_to_relocatable_relocs_)[reloc_shndx] = rr;
}
Relocatable_relocs*
relocatable_relocs(unsigned int reloc_shndx)
{
gold_assert(reloc_shndx < this->shnum());
return (*this->map_to_relocatable_relocs_)[reloc_shndx];
}
protected:
typedef std::vector<Output_section*> Output_sections;
virtual void
do_read_relocs(Read_relocs_data*) = 0;
virtual void
do_scan_relocs(const General_options&, Symbol_table*, Layout*,
Read_relocs_data*) = 0;
virtual unsigned int
do_local_symbol_count() const = 0;
virtual void
do_count_local_symbols(Stringpool_template<char>*,
Stringpool_template<char>*) = 0;
virtual unsigned int
do_finalize_local_symbols(unsigned int, off_t) = 0;
virtual unsigned int
do_set_local_dynsym_indexes(unsigned int) = 0;
virtual unsigned int
do_set_local_dynsym_offset(off_t) = 0;
virtual void
do_relocate(const General_options& options, const Symbol_table* symtab,
const Layout*, Output_file* of) = 0;
virtual uint64_t
do_output_section_offset(unsigned int shndx) const = 0;
virtual void
do_set_section_offset(unsigned int shndx, uint64_t off) = 0;
Output_sections&
output_sections()
{ return this->output_sections_; }
const Output_sections&
output_sections() const
{ return this->output_sections_; }
void
size_relocatable_relocs()
{
this->map_to_relocatable_relocs_ =
new std::vector<Relocatable_relocs*>(this->shnum());
}
void
set_relocs_must_follow_section_writes()
{ this->relocs_must_follow_section_writes_ = true; }
private:
Output_sections output_sections_;
std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_;
Object_merge_map* object_merge_map_;
bool relocs_must_follow_section_writes_;
};
template<int size>
class Merged_symbol_value
{
public:
typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
typedef Unordered_map<section_offset_type, Value> Output_addresses;
Merged_symbol_value(Value input_value, Value output_start_address)
: input_value_(input_value), output_start_address_(output_start_address),
output_addresses_()
{ }
void
initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
void
free_input_to_output_map()
{ this->output_addresses_.clear(); }
Value
value(const Relobj* object, unsigned int input_shndx, Value addend) const
{
Value input_offset = this->input_value_;
if (addend < 0xffffff00)
{
input_offset += addend;
addend = 0;
}
typename Output_addresses::const_iterator p =
this->output_addresses_.find(input_offset);
if (p != this->output_addresses_.end())
return p->second + addend;
return (this->value_from_output_section(object, input_shndx, input_offset)
+ addend);
}
private:
Value
value_from_output_section(const Relobj*, unsigned int input_shndx,
Value input_offset) const;
Value input_value_;
Value output_start_address_;
Output_addresses output_addresses_;
};
template<int size>
class Symbol_value
{
public:
typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
Symbol_value()
: output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
is_ordinary_shndx_(false), is_section_symbol_(false),
is_tls_symbol_(false), has_output_value_(true)
{ this->u_.value = 0; }
template<bool big_endian>
Value
value(const Sized_relobj<size, big_endian>* object, Value addend) const
{
if (this->has_output_value_)
return this->u_.value + addend;
else
{
gold_assert(this->is_ordinary_shndx_);
return this->u_.merged_symbol_value->value(object, this->input_shndx_,
addend);
}
}
void
set_output_value(Value value)
{ this->u_.value = value; }
void
set_merged_symbol_value(Merged_symbol_value<size>* msv)
{
gold_assert(this->is_section_symbol_);
this->has_output_value_ = false;
this->u_.merged_symbol_value = msv;
}
void
initialize_input_to_output_map(const Relobj* object)
{
if (!this->has_output_value_)
{
gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_);
Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
msv->initialize_input_to_output_map(object, this->input_shndx_);
}
}
void
free_input_to_output_map()
{
if (!this->has_output_value_)
this->u_.merged_symbol_value->free_input_to_output_map();
}
void
set_input_value(Value value)
{ this->u_.value = value; }
Value
input_value() const
{ return this->u_.value; }
bool
needs_output_symtab_entry() const
{ return this->output_symtab_index_ != -1U; }
unsigned int
output_symtab_index() const
{
gold_assert(this->output_symtab_index_ != 0);
return this->output_symtab_index_;
}
void
set_output_symtab_index(unsigned int i)
{
gold_assert(this->output_symtab_index_ == 0);
this->output_symtab_index_ = i;
}
void
set_no_output_symtab_entry()
{
gold_assert(this->output_symtab_index_ == 0);
this->output_symtab_index_ = -1U;
}
void
set_needs_output_dynsym_entry()
{
gold_assert(!this->is_section_symbol());
this->output_dynsym_index_ = 0;
}
bool
needs_output_dynsym_entry() const
{
return this->output_dynsym_index_ != -1U;
}
void
set_output_dynsym_index(unsigned int i)
{
gold_assert(this->output_dynsym_index_ == 0);
this->output_dynsym_index_ = i;
}
unsigned int
output_dynsym_index() const
{
gold_assert(this->output_dynsym_index_ != 0
&& this->output_dynsym_index_ != -1U);
return this->output_dynsym_index_;
}
void
set_input_shndx(unsigned int i, bool is_ordinary)
{
this->input_shndx_ = i;
gold_assert(this->input_shndx_ == i);
this->is_ordinary_shndx_ = is_ordinary;
}
unsigned int
input_shndx(bool* is_ordinary) const
{
*is_ordinary = this->is_ordinary_shndx_;
return this->input_shndx_;
}
bool
is_section_symbol() const
{ return this->is_section_symbol_; }
void
set_is_section_symbol()
{
gold_assert(!this->needs_output_dynsym_entry());
this->is_section_symbol_ = true;
}
void
set_is_tls_symbol()
{ this->is_tls_symbol_ = true; }
bool
is_tls_symbol() const
{ return this->is_tls_symbol_; }
private:
unsigned int output_symtab_index_;
unsigned int output_dynsym_index_;
unsigned int input_shndx_ : 28;
bool is_ordinary_shndx_ : 1;
bool is_section_symbol_ : 1;
bool is_tls_symbol_ : 1;
bool has_output_value_ : 1;
union
{
Value value;
Merged_symbol_value<size>* merged_symbol_value;
} u_;
};
class Got_offset_list
{
public:
Got_offset_list()
: got_type_(-1U), got_offset_(0), got_next_(NULL)
{ }
Got_offset_list(unsigned int got_type, unsigned int got_offset)
: got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
{ }
~Got_offset_list()
{
if (this->got_next_ != NULL)
{
delete this->got_next_;
this->got_next_ = NULL;
}
}
void
init()
{
this->got_type_ = -1U;
this->got_offset_ = 0;
this->got_next_ = NULL;
}
void
set_offset(unsigned int got_type, unsigned int got_offset)
{
if (this->got_type_ == -1U)
{
this->got_type_ = got_type;
this->got_offset_ = got_offset;
}
else
{
for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
{
if (g->got_type_ == got_type)
{
g->got_offset_ = got_offset;
return;
}
}
Got_offset_list* g = new Got_offset_list(got_type, got_offset);
g->got_next_ = this->got_next_;
this->got_next_ = g;
}
}
unsigned int
get_offset(unsigned int got_type) const
{
for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
{
if (g->got_type_ == got_type)
return g->got_offset_;
}
return -1U;
}
private:
unsigned int got_type_;
unsigned int got_offset_;
Got_offset_list* got_next_;
};
template<int size, bool big_endian>
class Sized_relobj : public Relobj
{
public:
typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
typedef std::vector<Symbol*> Symbols;
typedef std::vector<Symbol_value<size> > Local_values;
Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
const typename elfcpp::Ehdr<size, big_endian>&);
~Sized_relobj();
void
setup(const typename elfcpp::Ehdr<size, big_endian>&);
unsigned int
symbol_count() const
{ return this->local_symbol_count_ + this->symbols_.size(); }
Symbol*
global_symbol(unsigned int sym) const
{
if (sym >= this->local_symbol_count_)
{
gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
return this->symbols_[sym - this->local_symbol_count_];
}
return NULL;
}
unsigned int
symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary);
const Symbol_value<size>*
local_symbol(unsigned int sym) const
{
gold_assert(sym < this->local_values_.size());
return &this->local_values_[sym];
}
unsigned int
symtab_index(unsigned int sym) const
{
gold_assert(sym < this->local_values_.size());
return this->local_values_[sym].output_symtab_index();
}
unsigned int
dynsym_index(unsigned int sym) const
{
gold_assert(sym < this->local_values_.size());
return this->local_values_[sym].output_dynsym_index();
}
unsigned int
local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const
{
gold_assert(sym < this->local_values_.size());
return this->local_values_[sym].input_shndx(is_ordinary);
}
Sized_target<size, big_endian>*
sized_target()
{ return this->Object::sized_target<size, big_endian>(); }
void
set_needs_output_dynsym_entry(unsigned int sym)
{
gold_assert(sym < this->local_values_.size());
this->local_values_[sym].set_needs_output_dynsym_entry();
}
bool
local_has_got_offset(unsigned int symndx, unsigned int got_type) const
{
Local_got_offsets::const_iterator p =
this->local_got_offsets_.find(symndx);
return (p != this->local_got_offsets_.end()
&& p->second->get_offset(got_type) != -1U);
}
unsigned int
local_got_offset(unsigned int symndx, unsigned int got_type) const
{
Local_got_offsets::const_iterator p =
this->local_got_offsets_.find(symndx);
gold_assert(p != this->local_got_offsets_.end());
unsigned int off = p->second->get_offset(got_type);
gold_assert(off != -1U);
return off;
}
void
set_local_got_offset(unsigned int symndx, unsigned int got_type,
unsigned int got_offset)
{
Local_got_offsets::const_iterator p =
this->local_got_offsets_.find(symndx);
if (p != this->local_got_offsets_.end())
p->second->set_offset(got_type, got_offset);
else
{
Got_offset_list* g = new Got_offset_list(got_type, got_offset);
std::pair<Local_got_offsets::iterator, bool> ins =
this->local_got_offsets_.insert(std::make_pair(symndx, g));
gold_assert(ins.second);
}
}
Address
get_output_section_offset(unsigned int shndx) const
{
gold_assert(shndx < this->section_offsets_.size());
return this->section_offsets_[shndx];
}
bool
get_symbol_location_info(unsigned int shndx, off_t offset,
Symbol_location_info* info);
Address
map_to_kept_section(unsigned int shndx, bool* found) const;
protected:
void
do_read_symbols(Read_symbols_data*);
unsigned int
do_local_symbol_count() const
{ return this->local_symbol_count_; }
void
do_layout(Symbol_table*, Layout*, Read_symbols_data*);
void
do_add_symbols(Symbol_table*, Read_symbols_data*);
void
do_read_relocs(Read_relocs_data*);
void
do_scan_relocs(const General_options&, Symbol_table*, Layout*,
Read_relocs_data*);
void
do_count_local_symbols(Stringpool_template<char>*,
Stringpool_template<char>*);
unsigned int
do_finalize_local_symbols(unsigned int, off_t);
unsigned int
do_set_local_dynsym_indexes(unsigned int);
unsigned int
do_set_local_dynsym_offset(off_t);
void
do_relocate(const General_options& options, const Symbol_table* symtab,
const Layout*, Output_file* of);
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;
uint64_t
do_output_section_offset(unsigned int shndx) const
{ return this->get_output_section_offset(shndx); }
void
do_set_section_offset(unsigned int shndx, uint64_t off)
{
gold_assert(shndx < this->section_offsets_.size());
this->section_offsets_[shndx] = convert_types<Address, uint64_t>(off);
}
private:
typedef Sized_relobj<size, big_endian> This;
static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
typedef elfcpp::Shdr<size, big_endian> Shdr;
struct Kept_comdat_section
{
Kept_comdat_section(Sized_relobj<size, big_endian>* object,
unsigned int shndx)
: object_(object), shndx_(shndx)
{ }
Sized_relobj<size, big_endian>* object_;
unsigned int shndx_;
};
typedef std::map<unsigned int, Kept_comdat_section*>
Kept_comdat_section_table;
typedef Unordered_map<std::string, unsigned int> Comdat_group;
typedef std::map<unsigned int, Comdat_group*> Comdat_group_table;
Comdat_group*
find_comdat_group(unsigned int shndx) const
{
Comdat_group_table::const_iterator p =
this->comdat_groups_.find(shndx);
if (p != this->comdat_groups_.end())
return p->second;
return NULL;
}
void
add_comdat_group(unsigned int shndx, Comdat_group* group)
{ this->comdat_groups_[shndx] = group; }
unsigned int
adjust_shndx(unsigned int shndx)
{
if (shndx >= elfcpp::SHN_LORESERVE)
shndx += this->elf_file_.large_shndx_offset();
return shndx;
}
void
find_symtab(const unsigned char* pshdrs);
bool
check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
bool
find_eh_frame(const unsigned char* pshdrs, const char* names,
section_size_type names_size) const;
bool
include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
const unsigned char*, const char *, section_size_type,
std::vector<bool>*);
bool
include_linkonce_section(Layout*, unsigned int, const char*,
const elfcpp::Shdr<size, big_endian>&);
struct View_size
{
unsigned char* view;
typename elfcpp::Elf_types<size>::Elf_Addr address;
off_t offset;
section_size_type view_size;
bool is_input_output_view;
bool is_postprocessing_view;
};
typedef std::vector<View_size> Views;
void
write_sections(const unsigned char* pshdrs, Output_file*, Views*);
void
relocate_sections(const General_options& options, const Symbol_table*,
const Layout*, const unsigned char* pshdrs, Views*);
void
emit_relocs_scan(const General_options&, Symbol_table*, Layout*,
const unsigned char* plocal_syms,
const Read_relocs_data::Relocs_list::iterator&);
template<int sh_type>
void
emit_relocs_scan_reltype(const General_options&, Symbol_table*, Layout*,
const unsigned char* plocal_syms,
const Read_relocs_data::Relocs_list::iterator&,
Relocatable_relocs*);
void
emit_relocs(const Relocate_info<size, big_endian>*, unsigned int,
unsigned int sh_type, const unsigned char* prelocs,
size_t reloc_count, Output_section*, Address output_offset,
unsigned char* view, Address address,
section_size_type view_size,
unsigned char* reloc_view, section_size_type reloc_view_size);
template<int sh_type>
void
emit_relocs_reltype(const Relocate_info<size, big_endian>*, unsigned int,
const unsigned char* prelocs, size_t reloc_count,
Output_section*, Address output_offset,
unsigned char* view, Address address,
section_size_type view_size,
unsigned char* reloc_view,
section_size_type reloc_view_size);
void
initialize_input_to_output_maps();
void
free_input_to_output_maps();
void
write_local_symbols(Output_file*,
const Stringpool_template<char>*,
const Stringpool_template<char>*,
Output_symtab_xindex*,
Output_symtab_xindex*);
void
clear_local_symbols()
{
this->local_values_.clear();
this->local_got_offsets_.clear();
}
void
set_kept_comdat_section(unsigned int shndx, Kept_comdat_section* kept)
{
this->kept_comdat_sections_[shndx] = kept;
}
Kept_comdat_section*
get_kept_comdat_section(unsigned int shndx) const
{
typename Kept_comdat_section_table::const_iterator p =
this->kept_comdat_sections_.find(shndx);
if (p == this->kept_comdat_sections_.end())
return NULL;
return p->second;
}
typedef Unordered_map<unsigned int, Got_offset_list*> Local_got_offsets;
struct Tls_got_entry
{
Tls_got_entry(int got_offset, bool have_pair)
: got_offset_(got_offset),
have_pair_(have_pair)
{ }
int got_offset_;
bool have_pair_;
};
typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
elfcpp::Elf_file<size, big_endian, Object> elf_file_;
unsigned int symtab_shndx_;
unsigned int local_symbol_count_;
unsigned int output_local_symbol_count_;
unsigned int output_local_dynsym_count_;
Symbols symbols_;
size_t defined_count_;
off_t local_symbol_offset_;
off_t local_dynsym_offset_;
Local_values local_values_;
Local_got_offsets local_got_offsets_;
std::vector<Address> section_offsets_;
Kept_comdat_section_table kept_comdat_sections_;
Comdat_group_table comdat_groups_;
bool has_eh_frame_;
};
class Input_objects
{
public:
Input_objects()
: relobj_list_(), dynobj_list_(), sonames_(), system_library_directory_(),
cref_(NULL)
{ }
typedef std::vector<Relobj*> Relobj_list;
typedef Relobj_list::const_iterator Relobj_iterator;
typedef std::vector<Dynobj*> Dynobj_list;
typedef Dynobj_list::const_iterator Dynobj_iterator;
bool
add_object(Object*);
void
archive_start(Archive*);
void
archive_stop(Archive*);
void
check_dynamic_dependencies() const;
bool
found_in_system_library_directory(const Object*) const;
void
print_symbol_counts(const Symbol_table*) const;
Relobj_iterator
relobj_begin() const
{ return this->relobj_list_.begin(); }
Relobj_iterator
relobj_end() const
{ return this->relobj_list_.end(); }
Dynobj_iterator
dynobj_begin() const
{ return this->dynobj_list_.begin(); }
Dynobj_iterator
dynobj_end() const
{ return this->dynobj_list_.end(); }
bool
any_dynamic() const
{ return !this->dynobj_list_.empty(); }
int
number_of_input_objects() const
{ return this->relobj_list_.size() + this->dynobj_list_.size(); }
private:
Input_objects(const Input_objects&);
Input_objects& operator=(const Input_objects&);
Relobj_list relobj_list_;
Dynobj_list dynobj_list_;
Unordered_set<std::string> sonames_;
std::string system_library_directory_;
Cref* cref_;
};
template<int size, bool big_endian>
struct Relocate_info
{
const General_options* options;
const Symbol_table* symtab;
const Layout* layout;
Sized_relobj<size, big_endian>* object;
unsigned int reloc_shndx;
unsigned int data_shndx;
std::string
location(size_t relnum, off_t reloffset) const;
};
extern Object*
make_elf_object(const std::string& name, Input_file*,
off_t offset, const unsigned char* p,
section_offset_type bytes);
}
#endif