#include "gold.h"
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include "object.h"
#include "archive.h"
#include "cref.h"
namespace gold
{
class Cref_inputs
{
public:
Cref_inputs()
: objects_(), archives_(), current_(&this->objects_)
{ }
void
add_object(Object* object);
void
add_archive_start(Archive*);
void
add_archive_stop(Archive*);
void
print_symbol_counts(const Symbol_table*, FILE*) const;
private:
typedef std::vector<Object*> Objects;
struct Archive_info
{
std::string name;
Objects* objects;
size_t member_count;
};
typedef std::map<std::string, Archive_info> Archives;
void
print_objects_symbol_counts(const Symbol_table*, FILE*, const Objects*) const;
void
print_object_symbol_counts(const Symbol_table*, FILE*, const Object*) const;
Objects objects_;
Archives archives_;
Objects* current_;
};
void
Cref_inputs::add_object(Object* object)
{
this->current_->push_back(object);
}
void
Cref_inputs::add_archive_start(Archive* archive)
{
gold_assert(this->current_ == &this->objects_);
if (this->archives_.find(archive->name()) == this->archives_.end())
{
Archive_info* pai = &this->archives_[archive->name()];
pai->name = archive->filename();
pai->objects = new Objects();
pai->member_count = archive->count_members();
}
this->current_ = this->archives_[archive->name()].objects;
}
void
Cref_inputs::add_archive_stop(Archive*)
{
gold_assert(this->current_ != &this->objects_);
this->current_ = &this->objects_;
}
void
Cref_inputs::print_object_symbol_counts(const Symbol_table* symtab,
FILE* f,
const Object* object) const
{
size_t defined, used;
object->get_global_symbol_counts(symtab, &defined, &used);
fprintf(f, "symbols %s %zu %zu\n", object->name().c_str(), defined, used);
}
void
Cref_inputs::print_objects_symbol_counts(const Symbol_table* symtab,
FILE* f,
const Objects* objects) const
{
for (Objects::const_iterator p = objects->begin();
p != objects->end();
++p)
this->print_object_symbol_counts(symtab, f, *p);
}
void
Cref_inputs::print_symbol_counts(const Symbol_table* symtab, FILE* f) const
{
this->print_objects_symbol_counts(symtab, f, &this->objects_);
for (Archives::const_iterator p = this->archives_.begin();
p != this->archives_.end();
++p)
{
fprintf(f, "archive %s %zu %zu\n", p->second.name.c_str(),
p->second.member_count, p->second.objects->size());
this->print_objects_symbol_counts(symtab, f, p->second.objects);
}
}
void
Cref::need_inputs()
{
if (this->inputs_ == NULL)
this->inputs_ = new Cref_inputs();
}
void
Cref::add_object(Object* object)
{
this->need_inputs();
this->inputs_->add_object(object);
}
void
Cref::add_archive_start(Archive* archive)
{
this->need_inputs();
this->inputs_->add_archive_start(archive);
}
void
Cref::add_archive_stop(Archive* archive)
{
this->inputs_->add_archive_stop(archive);
}
void
Cref::print_symbol_counts(const Symbol_table* symtab) const
{
if (parameters->options().user_set_print_symbol_counts()
&& this->inputs_ != NULL)
{
FILE* f;
if (strcmp(parameters->options().print_symbol_counts(), "-") == 0)
f = stdout;
else
{
f = fopen(parameters->options().print_symbol_counts(), "w");
if (f == NULL)
gold_error(_("cannot open symbol count file %s: %s"),
parameters->options().print_symbol_counts(),
strerror(errno));
}
if (f != NULL)
this->inputs_->print_symbol_counts(symtab, f);
}
}
}