#include "gold.h"
#include "gdb-index.h"
#include "dwarf_reader.h"
#include "dwarf.h"
#include "object.h"
#include "output.h"
#include "demangle.h"
namespace gold
{
const int gdb_index_version = 7;
const int gdb_index_offset_size = 4;
const int gdb_index_hdr_size = 6 * gdb_index_offset_size;
const int gdb_index_cu_size = 16;
const int gdb_index_tu_size = 24;
const int gdb_index_addr_size = 16 + gdb_index_offset_size;
const int gdb_index_sym_size = 2 * gdb_index_offset_size;
template <typename T>
class Gdb_hashtab
{
public:
Gdb_hashtab()
: size_(0), capacity_(0), hashtab_(NULL)
{ }
~Gdb_hashtab()
{
for (size_t i = 0; i < this->capacity_; ++i)
if (this->hashtab_[i] != NULL)
delete this->hashtab_[i];
delete[] this->hashtab_;
}
T*
add(T* symbol)
{
if (4 * this->size_ / 3 >= this->capacity_)
this->expand();
T** slot = this->find_slot(symbol);
if (*slot == NULL)
{
++this->size_;
*slot = symbol;
}
return *slot;
}
size_t
size() const
{ return this->size_; }
size_t
capacity() const
{ return this->capacity_; }
T*
operator[](size_t n)
{ return this->hashtab_[n]; }
private:
T**
find_slot(T* symbol)
{
unsigned int index = symbol->hash() & (this->capacity_ - 1);
unsigned int step = ((symbol->hash() * 17) & (this->capacity_ - 1)) | 1;
for (;;)
{
if (this->hashtab_[index] == NULL
|| this->hashtab_[index]->equal(symbol))
return &this->hashtab_[index];
index = (index + step) & (this->capacity_ - 1);
}
}
void
expand()
{
if (this->capacity_ == 0)
{
this->capacity_ = Gdb_hashtab::initial_size;
this->hashtab_ = new T*[this->capacity_];
memset(this->hashtab_, 0, this->capacity_ * sizeof(T*));
}
else
{
unsigned int old_cap = this->capacity_;
T** old_hashtab = this->hashtab_;
this->capacity_ *= 2;
this->hashtab_ = new T*[this->capacity_];
memset(this->hashtab_, 0, this->capacity_ * sizeof(T*));
for (size_t i = 0; i < old_cap; ++i)
{
if (old_hashtab[i] != NULL)
{
T** slot = this->find_slot(old_hashtab[i]);
*slot = old_hashtab[i];
}
}
delete[] old_hashtab;
}
}
static const int initial_size = 1024;
size_t size_;
size_t capacity_;
T** hashtab_;
};
static unsigned int
mapped_index_string_hash(const unsigned char* str)
{
unsigned int r = 0;
unsigned char c;
while ((c = *str++) != 0)
{
if (gdb_index_version >= 5)
c = tolower (c);
r = r * 67 + c - 113;
}
return r;
}
class Gdb_index_info_reader : public Dwarf_info_reader
{
public:
Gdb_index_info_reader(bool is_type_unit,
Relobj* object,
const unsigned char* symbols,
off_t symbols_size,
unsigned int shndx,
unsigned int reloc_shndx,
unsigned int reloc_type,
Gdb_index* gdb_index)
: Dwarf_info_reader(is_type_unit, object, symbols, symbols_size, shndx,
reloc_shndx, reloc_type),
gdb_index_(gdb_index), cu_index_(0), cu_language_(0)
{ }
~Gdb_index_info_reader()
{ this->clear_declarations(); }
static void
print_stats();
protected:
virtual void
visit_compilation_unit(off_t cu_offset, off_t cu_length, Dwarf_die*);
virtual void
visit_type_unit(off_t tu_offset, off_t tu_length, off_t type_offset,
uint64_t signature, Dwarf_die*);
private:
struct Declaration_pair
{
Declaration_pair(off_t parent_offset, const char* name)
: parent_offset_(parent_offset), name_(name)
{ }
off_t parent_offset_;
const char* name_;
};
typedef Unordered_map<off_t, Declaration_pair> Declaration_map;
void
visit_top_die(Dwarf_die* die);
void
visit_children(Dwarf_die* die, Dwarf_die* context);
void
visit_die(Dwarf_die* die, Dwarf_die* context);
void
visit_children_for_decls(Dwarf_die* die);
void
visit_die_for_decls(Dwarf_die* die, Dwarf_die* context);
std::string
guess_full_class_name(Dwarf_die* die);
void
add_declaration(Dwarf_die* die, Dwarf_die* context);
void
add_declaration_with_full_name(Dwarf_die* die, const char* full_name);
std::string
get_context(off_t die_offset);
std::string
get_qualified_name(Dwarf_die* die, Dwarf_die* context);
void
record_cu_ranges(Dwarf_die* die);
bool
read_pubnames_and_pubtypes(Dwarf_die* die);
bool
read_pubtable(Dwarf_pubnames_table* table, off_t offset);
void
clear_declarations();
Gdb_index* gdb_index_;
int cu_index_;
unsigned int cu_language_;
Declaration_map declarations_;
static unsigned int dwarf_cu_count;
static unsigned int dwarf_cu_nopubnames_count;
static unsigned int dwarf_tu_count;
static unsigned int dwarf_tu_nopubnames_count;
};
unsigned int Gdb_index_info_reader::dwarf_cu_count = 0;
unsigned int Gdb_index_info_reader::dwarf_cu_nopubnames_count = 0;
unsigned int Gdb_index_info_reader::dwarf_tu_count = 0;
unsigned int Gdb_index_info_reader::dwarf_tu_nopubnames_count = 0;
void
Gdb_index_info_reader::visit_compilation_unit(off_t cu_offset, off_t cu_length,
Dwarf_die* root_die)
{
++Gdb_index_info_reader::dwarf_cu_count;
this->cu_index_ = this->gdb_index_->add_comp_unit(cu_offset, cu_length);
this->visit_top_die(root_die);
}
void
Gdb_index_info_reader::visit_type_unit(off_t tu_offset, off_t,
off_t type_offset, uint64_t signature,
Dwarf_die* root_die)
{
++Gdb_index_info_reader::dwarf_tu_count;
this->cu_index_ = -1 - this->gdb_index_->add_type_unit(tu_offset, type_offset,
signature);
this->visit_top_die(root_die);
}
void
Gdb_index_info_reader::visit_top_die(Dwarf_die* die)
{
this->clear_declarations();
switch (die->tag())
{
case elfcpp::DW_TAG_compile_unit:
case elfcpp::DW_TAG_type_unit:
this->cu_language_ = die->int_attribute(elfcpp::DW_AT_language);
if (die->tag() == elfcpp::DW_TAG_compile_unit)
this->record_cu_ranges(die);
if (!this->read_pubnames_and_pubtypes(die))
{
if (this->cu_language_ == elfcpp::DW_LANG_Ada83
|| this->cu_language_ == elfcpp::DW_LANG_Fortran77
|| this->cu_language_ == elfcpp::DW_LANG_Fortran90
|| this->cu_language_ == elfcpp::DW_LANG_Java
|| this->cu_language_ == elfcpp::DW_LANG_Ada95
|| this->cu_language_ == elfcpp::DW_LANG_Fortran95
|| this->cu_language_ == elfcpp::DW_LANG_Fortran03
|| this->cu_language_ == elfcpp::DW_LANG_Fortran08)
{
gold_warning(_("%s: --gdb-index currently supports "
"only C and C++ languages"),
this->object()->name().c_str());
return;
}
if (die->tag() == elfcpp::DW_TAG_compile_unit)
++Gdb_index_info_reader::dwarf_cu_nopubnames_count;
else
++Gdb_index_info_reader::dwarf_tu_nopubnames_count;
this->visit_children(die, NULL);
}
break;
default:
gold_warning(_("%s: top level DIE is not DW_TAG_compile_unit "
"or DW_TAG_type_unit"),
this->object()->name().c_str());
return;
}
}
void
Gdb_index_info_reader::visit_children(Dwarf_die* parent, Dwarf_die* context)
{
off_t next_offset = 0;
for (off_t die_offset = parent->child_offset();
die_offset != 0;
die_offset = next_offset)
{
Dwarf_die die(this, die_offset, parent);
if (die.tag() == 0)
break;
this->visit_die(&die, context);
next_offset = die.sibling_offset();
}
}
void
Gdb_index_info_reader::visit_die(Dwarf_die* die, Dwarf_die* context)
{
switch (die->tag())
{
case elfcpp::DW_TAG_subprogram:
case elfcpp::DW_TAG_constant:
case elfcpp::DW_TAG_variable:
case elfcpp::DW_TAG_enumerator:
case elfcpp::DW_TAG_base_type:
if (die->is_declaration())
this->add_declaration(die, context);
else
{
std::string full_name = this->get_qualified_name(die, context);
if (!full_name.empty())
this->gdb_index_->add_symbol(this->cu_index_,
full_name.c_str(), 0);
}
break;
case elfcpp::DW_TAG_typedef:
case elfcpp::DW_TAG_union_type:
case elfcpp::DW_TAG_class_type:
case elfcpp::DW_TAG_interface_type:
case elfcpp::DW_TAG_structure_type:
case elfcpp::DW_TAG_enumeration_type:
case elfcpp::DW_TAG_subrange_type:
case elfcpp::DW_TAG_namespace:
{
std::string full_name;
if (context == NULL
&& (die->tag() == elfcpp::DW_TAG_class_type
|| die->tag() == elfcpp::DW_TAG_structure_type
|| die->tag() == elfcpp::DW_TAG_union_type))
full_name.assign(this->guess_full_class_name(die));
if (full_name.empty())
this->add_declaration(die, context);
else
this->add_declaration_with_full_name(die, full_name.c_str());
if (die->tag() == elfcpp::DW_TAG_namespace
|| !die->is_declaration())
{
if (full_name.empty())
full_name = this->get_qualified_name(die, context);
if (!full_name.empty())
this->gdb_index_->add_symbol(this->cu_index_,
full_name.c_str(), 0);
}
if (die->tag() == elfcpp::DW_TAG_namespace
|| die->tag() == elfcpp::DW_TAG_enumeration_type)
this->visit_children(die, die);
else
this->visit_children_for_decls(die);
}
break;
default:
break;
}
}
void
Gdb_index_info_reader::visit_children_for_decls(Dwarf_die* parent)
{
off_t next_offset = 0;
for (off_t die_offset = parent->child_offset();
die_offset != 0;
die_offset = next_offset)
{
Dwarf_die die(this, die_offset, parent);
if (die.tag() == 0)
break;
this->visit_die_for_decls(&die, parent);
next_offset = die.sibling_offset();
}
}
void
Gdb_index_info_reader::visit_die_for_decls(Dwarf_die* die, Dwarf_die* context)
{
switch (die->tag())
{
case elfcpp::DW_TAG_subprogram:
case elfcpp::DW_TAG_constant:
case elfcpp::DW_TAG_variable:
case elfcpp::DW_TAG_enumerator:
case elfcpp::DW_TAG_base_type:
{
if (die->is_declaration())
this->add_declaration(die, context);
}
break;
case elfcpp::DW_TAG_typedef:
case elfcpp::DW_TAG_union_type:
case elfcpp::DW_TAG_class_type:
case elfcpp::DW_TAG_interface_type:
case elfcpp::DW_TAG_structure_type:
case elfcpp::DW_TAG_enumeration_type:
case elfcpp::DW_TAG_subrange_type:
case elfcpp::DW_TAG_namespace:
{
if (die->is_declaration())
this->add_declaration(die, context);
this->visit_children_for_decls(die);
}
break;
default:
break;
}
}
#define d_left(dc) (dc)->u.s_binary.left
#define d_right(dc) (dc)->u.s_binary.right
static char*
class_name_from_linkage_name(const char* linkage_name)
{
void* storage;
struct demangle_component* tree =
cplus_demangle_v3_components(linkage_name, DMGL_NO_OPTS, &storage);
if (tree == NULL)
return NULL;
int done = 0;
while (!done)
switch (tree->type)
{
case DEMANGLE_COMPONENT_CONST:
case DEMANGLE_COMPONENT_RESTRICT:
case DEMANGLE_COMPONENT_VOLATILE:
case DEMANGLE_COMPONENT_CONST_THIS:
case DEMANGLE_COMPONENT_RESTRICT_THIS:
case DEMANGLE_COMPONENT_VOLATILE_THIS:
case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
tree = d_left(tree);
break;
default:
done = 1;
break;
}
if (tree->type == DEMANGLE_COMPONENT_TYPED_NAME)
tree = d_left(tree);
if (tree->type == DEMANGLE_COMPONENT_TEMPLATE)
tree = d_left(tree);
done = 0;
struct demangle_component* prev_comp = NULL;
struct demangle_component* cur_comp = tree;
while (!done)
switch (cur_comp->type)
{
case DEMANGLE_COMPONENT_QUAL_NAME:
case DEMANGLE_COMPONENT_LOCAL_NAME:
prev_comp = cur_comp;
cur_comp = d_right(cur_comp);
break;
case DEMANGLE_COMPONENT_TEMPLATE:
case DEMANGLE_COMPONENT_NAME:
case DEMANGLE_COMPONENT_CTOR:
case DEMANGLE_COMPONENT_DTOR:
case DEMANGLE_COMPONENT_OPERATOR:
case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
done = 1;
break;
default:
done = 1;
cur_comp = NULL;
break;
}
char* ret = NULL;
if (cur_comp != NULL && prev_comp != NULL)
{
*prev_comp = *d_left(prev_comp);
size_t allocated_size;
ret = cplus_demangle_print(DMGL_NO_OPTS, tree, 30, &allocated_size);
}
free(storage);
return ret;
}
std::string
Gdb_index_info_reader::guess_full_class_name(Dwarf_die* die)
{
std::string full_name;
off_t next_offset = 0;
uint64_t checkpoint = this->get_reloc_checkpoint();
for (off_t child_offset = die->child_offset();
child_offset != 0;
child_offset = next_offset)
{
Dwarf_die child(this, child_offset, die);
if (child.tag() == 0)
break;
if (child.tag() == elfcpp::DW_TAG_subprogram)
{
const char* linkage_name = child.linkage_name();
if (linkage_name != NULL)
{
char* guess = class_name_from_linkage_name(linkage_name);
if (guess != NULL)
{
full_name.assign(guess);
free(guess);
break;
}
}
}
next_offset = child.sibling_offset();
}
this->reset_relocs(checkpoint);
return full_name;
}
void
Gdb_index_info_reader::add_declaration(Dwarf_die* die, Dwarf_die* context)
{
const char* name = die->name();
off_t parent_offset = context != NULL ? context->offset() : 0;
off_t spec = die->specification();
if (spec == 0)
spec = die->abstract_origin();
if (spec > 0)
{
Declaration_map::iterator it = this->declarations_.find(spec);
if (it != this->declarations_.end())
{
parent_offset = it->second.parent_offset_;
name = it->second.name_;
}
}
if (name == NULL)
{
if (die->tag() == elfcpp::DW_TAG_namespace)
name = "(anonymous namespace)";
else if (die->tag() == elfcpp::DW_TAG_union_type)
name = "(anonymous union)";
else
name = "(unknown)";
}
Declaration_pair decl(parent_offset, name);
this->declarations_.insert(std::make_pair(die->offset(), decl));
}
void
Gdb_index_info_reader::add_declaration_with_full_name(
Dwarf_die* die,
const char* full_name)
{
int len = strlen(full_name);
char* copy = new char[len + 1];
memcpy(copy, full_name, len + 1);
Declaration_pair decl(-1, copy);
this->declarations_.insert(std::make_pair(die->offset(), decl));
}
std::string
Gdb_index_info_reader::get_context(off_t die_offset)
{
std::string context;
Declaration_map::iterator it = this->declarations_.find(die_offset);
if (it != this->declarations_.end())
{
off_t parent_offset = it->second.parent_offset_;
if (parent_offset > 0)
{
context = get_context(parent_offset);
context.append("::");
}
if (it->second.name_ != NULL)
context.append(it->second.name_);
}
return context;
}
std::string
Gdb_index_info_reader::get_qualified_name(Dwarf_die* die, Dwarf_die* context)
{
std::string full_name;
const char* name = die->name();
off_t parent_offset = context != NULL ? context->offset() : 0;
off_t spec = die->specification();
if (spec == 0)
spec = die->abstract_origin();
if (spec > 0)
{
Declaration_map::iterator it = this->declarations_.find(spec);
if (it != this->declarations_.end())
{
parent_offset = it->second.parent_offset_;
name = it->second.name_;
}
}
if (name == NULL && die->tag() == elfcpp::DW_TAG_namespace)
name = "(anonymous namespace)";
else if (name == NULL)
return full_name;
if (die->tag() == elfcpp::DW_TAG_enumerator)
{
Declaration_map::iterator it = this->declarations_.find(parent_offset);
if (it != this->declarations_.end())
parent_offset = it->second.parent_offset_;
}
if (parent_offset > 0)
{
full_name.assign(this->get_context(parent_offset));
full_name.append("::");
}
full_name.append(name);
return full_name;
}
void
Gdb_index_info_reader::record_cu_ranges(Dwarf_die* die)
{
unsigned int shndx;
unsigned int shndx2;
off_t ranges_offset = die->ref_attribute(elfcpp::DW_AT_ranges, &shndx);
if (ranges_offset != -1)
{
Dwarf_range_list* ranges = this->read_range_list(shndx, ranges_offset);
if (ranges != NULL)
this->gdb_index_->add_address_range_list(this->object(),
this->cu_index_, ranges);
return;
}
off_t low_pc = die->address_attribute(elfcpp::DW_AT_low_pc, &shndx);
off_t high_pc = die->address_attribute(elfcpp::DW_AT_high_pc, &shndx2);
if (high_pc == -1)
{
high_pc = die->uint_attribute(elfcpp::DW_AT_high_pc);
high_pc += low_pc;
shndx2 = shndx;
}
if ((low_pc != 0 || high_pc != 0) && low_pc != -1)
{
if (shndx != shndx2)
{
gold_warning(_("%s: DWARF info may be corrupt; low_pc and high_pc "
"are in different sections"),
this->object()->name().c_str());
return;
}
if (shndx == 0 || this->object()->is_section_included(shndx))
{
Dwarf_range_list* ranges = new Dwarf_range_list();
ranges->add(shndx, low_pc, high_pc);
this->gdb_index_->add_address_range_list(this->object(),
this->cu_index_, ranges);
}
}
}
bool
Gdb_index_info_reader::read_pubtable(Dwarf_pubnames_table* table, off_t offset)
{
if (table == NULL)
return false;
if (!table->read_header(offset))
return false;
while (true)
{
uint8_t flag_byte;
const char* name = table->next_name(&flag_byte);
if (name == NULL)
break;
this->gdb_index_->add_symbol(this->cu_index_, name, flag_byte);
}
return true;
}
bool
Gdb_index_info_reader::read_pubnames_and_pubtypes(Dwarf_die* die)
{
if (die->tag() == elfcpp::DW_TAG_type_unit
&& die->string_attribute(elfcpp::DW_AT_GNU_dwo_name))
return true;
unsigned int shndx;
off_t stmt_list_off = die->ref_attribute (elfcpp::DW_AT_stmt_list,
&shndx);
off_t offset = die->ref_attribute(elfcpp::DW_AT_GNU_pubnames, &shndx);
unsigned int flag = die->uint_attribute(elfcpp::DW_AT_GNU_pubnames);
if (offset == -1 && flag == 0)
{
if (die->tag() == elfcpp::DW_TAG_type_unit)
{
return this->gdb_index_->pubnames_read(this->object(),
stmt_list_off);
}
else
{
return false;
}
}
if (this->gdb_index_->pubnames_read(this->object(), stmt_list_off))
return true;
this->gdb_index_->set_pubnames_read(this->object(), stmt_list_off);
bool names = false;
offset = this->gdb_index_->find_pubname_offset(this->cu_offset());
names = this->read_pubtable(this->gdb_index_->pubnames_table(), offset);
bool types = false;
offset = this->gdb_index_->find_pubtype_offset(this->cu_offset());
types = this->read_pubtable(this->gdb_index_->pubtypes_table(), offset);
return names || types;
}
void
Gdb_index_info_reader::clear_declarations()
{
for (Declaration_map::iterator it = this->declarations_.begin();
it != this->declarations_.end();
++it)
{
if (it->second.parent_offset_ == -1)
delete[] it->second.name_;
}
this->declarations_.clear();
}
void
Gdb_index_info_reader::print_stats()
{
fprintf(stderr, _("%s: DWARF CUs: %u\n"),
program_name, Gdb_index_info_reader::dwarf_cu_count);
fprintf(stderr, _("%s: DWARF CUs without pubnames/pubtypes: %u\n"),
program_name, Gdb_index_info_reader::dwarf_cu_nopubnames_count);
fprintf(stderr, _("%s: DWARF TUs: %u\n"),
program_name, Gdb_index_info_reader::dwarf_tu_count);
fprintf(stderr, _("%s: DWARF TUs without pubnames/pubtypes: %u\n"),
program_name, Gdb_index_info_reader::dwarf_tu_nopubnames_count);
}
Gdb_index::Gdb_index(Output_section* gdb_index_section)
: Output_section_data(4),
pubnames_table_(NULL),
pubtypes_table_(NULL),
gdb_index_section_(gdb_index_section),
comp_units_(),
type_units_(),
ranges_(),
cu_vector_list_(),
cu_vector_offsets_(NULL),
stringpool_(),
tu_offset_(0),
addr_offset_(0),
symtab_offset_(0),
cu_pool_offset_(0),
stringpool_offset_(0),
pubnames_object_(NULL),
stmt_list_offset_(-1)
{
this->gdb_symtab_ = new Gdb_hashtab<Gdb_symbol>();
}
Gdb_index::~Gdb_index()
{
delete this->gdb_symtab_;
for (unsigned int i = 0; i < this->cu_vector_list_.size(); ++i)
delete this->cu_vector_list_[i];
}
Dwarf_pubnames_table*
Gdb_index::map_pubtable_to_dies(unsigned int attr,
Gdb_index_info_reader* dwinfo,
Relobj* object,
const unsigned char* symbols,
off_t symbols_size)
{
uint64_t section_offset = 0;
Dwarf_pubnames_table* table;
Pubname_offset_map* map;
if (attr == elfcpp::DW_AT_GNU_pubnames)
{
table = new Dwarf_pubnames_table(dwinfo, false);
map = &this->cu_pubname_map_;
}
else
{
table = new Dwarf_pubnames_table(dwinfo, true);
map = &this->cu_pubtype_map_;
}
map->clear();
if (!table->read_section(object, symbols, symbols_size))
return NULL;
while (table->read_header(section_offset))
{
map->insert(std::make_pair(table->cu_offset(), section_offset));
section_offset += table->subsection_size();
}
return table;
}
void
Gdb_index::map_pubnames_and_types_to_dies(Gdb_index_info_reader* dwinfo,
Relobj* object,
const unsigned char* symbols,
off_t symbols_size)
{
this->pubnames_object_ = object;
this->stmt_list_offset_ = -1;
delete this->pubnames_table_;
this->pubnames_table_
= this->map_pubtable_to_dies(elfcpp::DW_AT_GNU_pubnames, dwinfo,
object, symbols, symbols_size);
delete this->pubtypes_table_;
this->pubtypes_table_
= this->map_pubtable_to_dies(elfcpp::DW_AT_GNU_pubtypes, dwinfo,
object, symbols, symbols_size);
}
off_t
Gdb_index::find_pubname_offset(off_t cu_offset)
{
Pubname_offset_map::iterator it = this->cu_pubname_map_.find(cu_offset);
if (it != this->cu_pubname_map_.end())
return it->second;
return -1;
}
off_t
Gdb_index::find_pubtype_offset(off_t cu_offset)
{
Pubname_offset_map::iterator it = this->cu_pubtype_map_.find(cu_offset);
if (it != this->cu_pubtype_map_.end())
return it->second;
return -1;
}
void
Gdb_index::scan_debug_info(bool is_type_unit,
Relobj* object,
const unsigned char* symbols,
off_t symbols_size,
unsigned int shndx,
unsigned int reloc_shndx,
unsigned int reloc_type)
{
Gdb_index_info_reader dwinfo(is_type_unit, object,
symbols, symbols_size,
shndx, reloc_shndx,
reloc_type, this);
if (object != this->pubnames_object_)
map_pubnames_and_types_to_dies(&dwinfo, object, symbols, symbols_size);
dwinfo.parse();
}
void
Gdb_index::add_symbol(int cu_index, const char* sym_name, uint8_t flags)
{
unsigned int hash = mapped_index_string_hash(
reinterpret_cast<const unsigned char*>(sym_name));
Gdb_symbol* sym = new Gdb_symbol();
this->stringpool_.add(sym_name, true, &sym->name_key);
sym->hashval = hash;
sym->cu_vector_index = 0;
Gdb_symbol* found = this->gdb_symtab_->add(sym);
if (found == sym)
{
found->cu_vector_index = this->cu_vector_list_.size();
this->cu_vector_list_.push_back(new Cu_vector());
}
else
{
delete sym;
}
Cu_vector* cu_vec = this->cu_vector_list_[found->cu_vector_index];
if (cu_vec->size() == 0
|| cu_vec->back().first != cu_index
|| cu_vec->back().second != flags)
cu_vec->push_back(std::make_pair(cu_index, flags));
}
bool
Gdb_index::pubnames_read(const Relobj* object, off_t offset)
{
bool ret = (this->pubnames_object_ == object
&& this->stmt_list_offset_ == offset);
return ret;
}
void
Gdb_index::set_pubnames_read(const Relobj* object, off_t offset)
{
this->pubnames_object_ = object;
this->stmt_list_offset_ = offset;
}
void
Gdb_index::set_final_data_size()
{
this->stringpool_.set_string_offsets();
unsigned int cu_vector_count = this->cu_vector_list_.size();
unsigned int cu_vector_size = 0;
this->cu_vector_offsets_ = new off_t[cu_vector_count];
for (unsigned int i = 0; i < cu_vector_count; ++i)
{
Cu_vector* cu_vec = this->cu_vector_list_[i];
cu_vector_offsets_[i] = cu_vector_size;
cu_vector_size += gdb_index_offset_size * (cu_vec->size() + 1);
}
section_size_type data_size = gdb_index_hdr_size;
data_size += this->comp_units_.size() * gdb_index_cu_size;
this->tu_offset_ = data_size;
data_size += this->type_units_.size() * gdb_index_tu_size;
this->addr_offset_ = data_size;
for (unsigned int i = 0; i < this->ranges_.size(); ++i)
data_size += this->ranges_[i].ranges->size() * gdb_index_addr_size;
this->symtab_offset_ = data_size;
data_size += this->gdb_symtab_->capacity() * gdb_index_sym_size;
this->cu_pool_offset_ = data_size;
data_size += cu_vector_size;
this->stringpool_offset_ = data_size;
data_size += this->stringpool_.get_strtab_size();
this->set_data_size(data_size);
}
void
Gdb_index::do_write(Output_file* of)
{
const off_t off = this->offset();
const off_t oview_size = this->data_size();
unsigned char* const oview = of->get_output_view(off, oview_size);
unsigned char* pov = oview;
elfcpp::Swap<32, false>::writeval(pov, gdb_index_version);
pov += 4;
elfcpp::Swap<32, false>::writeval(pov, gdb_index_hdr_size);
pov += 4;
elfcpp::Swap<32, false>::writeval(pov, this->tu_offset_);
pov += 4;
elfcpp::Swap<32, false>::writeval(pov, this->addr_offset_);
pov += 4;
elfcpp::Swap<32, false>::writeval(pov, this->symtab_offset_);
pov += 4;
elfcpp::Swap<32, false>::writeval(pov, this->cu_pool_offset_);
pov += 4;
gold_assert(pov - oview == gdb_index_hdr_size);
unsigned int comp_units_count = this->comp_units_.size();
for (unsigned int i = 0; i < comp_units_count; ++i)
{
const Comp_unit& cu = this->comp_units_[i];
elfcpp::Swap<64, false>::writeval(pov, cu.cu_offset);
elfcpp::Swap<64, false>::writeval(pov + 8, cu.cu_length);
pov += 16;
}
gold_assert(pov - oview == this->tu_offset_);
for (unsigned int i = 0; i < this->type_units_.size(); ++i)
{
const Type_unit& tu = this->type_units_[i];
elfcpp::Swap<64, false>::writeval(pov, tu.tu_offset);
elfcpp::Swap<64, false>::writeval(pov + 8, tu.type_offset);
elfcpp::Swap<64, false>::writeval(pov + 16, tu.type_signature);
pov += 24;
}
gold_assert(pov - oview == this->addr_offset_);
for (unsigned int i = 0; i < this->ranges_.size(); ++i)
{
int cu_index = this->ranges_[i].cu_index;
if (cu_index < 0)
cu_index = comp_units_count + (-1 - cu_index);
Relobj* object = this->ranges_[i].object;
const Dwarf_range_list& ranges = *this->ranges_[i].ranges;
for (unsigned int j = 0; j < ranges.size(); ++j)
{
const Dwarf_range_list::Range& range = ranges[j];
uint64_t base = 0;
if (range.shndx > 0)
{
const Output_section* os = object->output_section(range.shndx);
base = (os->address()
+ object->output_section_offset(range.shndx));
}
elfcpp::Swap_aligned32<64, false>::writeval(pov, base + range.start);
elfcpp::Swap_aligned32<64, false>::writeval(pov + 8,
base + range.end);
elfcpp::Swap<32, false>::writeval(pov + 16, cu_index);
pov += 20;
}
}
gold_assert(pov - oview == this->symtab_offset_);
for (unsigned int i = 0; i < this->gdb_symtab_->capacity(); ++i)
{
const Gdb_symbol* sym = (*this->gdb_symtab_)[i];
section_offset_type name_offset = 0;
unsigned int cu_vector_offset = 0;
if (sym != NULL)
{
name_offset = (this->stringpool_.get_offset_from_key(sym->name_key)
+ this->stringpool_offset_ - this->cu_pool_offset_);
cu_vector_offset = this->cu_vector_offsets_[sym->cu_vector_index];
}
elfcpp::Swap<32, false>::writeval(pov, name_offset);
elfcpp::Swap<32, false>::writeval(pov + 4, cu_vector_offset);
pov += 8;
}
gold_assert(pov - oview == this->cu_pool_offset_);
for (unsigned int i = 0; i < this->cu_vector_list_.size(); ++i)
{
Cu_vector* cu_vec = this->cu_vector_list_[i];
elfcpp::Swap<32, false>::writeval(pov, cu_vec->size());
pov += 4;
for (unsigned int j = 0; j < cu_vec->size(); ++j)
{
int cu_index = (*cu_vec)[j].first;
uint8_t flags = (*cu_vec)[j].second;
if (cu_index < 0)
cu_index = comp_units_count + (-1 - cu_index);
cu_index |= flags << 24;
elfcpp::Swap<32, false>::writeval(pov, cu_index);
pov += 4;
}
}
gold_assert(pov - oview == this->stringpool_offset_);
this->stringpool_.write_to_buffer(pov, oview_size - this->stringpool_offset_);
of->write_output_view(off, oview_size, oview);
}
void
Gdb_index::print_stats()
{
if (parameters->options().gdb_index())
Gdb_index_info_reader::print_stats();
}
}