+// Sort symbols for the cross reference table.
+
+bool
+Cref_inputs::Cref_table_compare::operator()(const Symbol* s1,
+ const Symbol* s2) const
+{
+ int i = strcmp(s1->name(), s2->name());
+ if (i != 0)
+ return i < 0;
+
+ if (s1->version() == NULL)
+ {
+ if (s2->version() != NULL)
+ return true;
+ }
+ else if (s2->version() == NULL)
+ return false;
+ else
+ {
+ i = strcmp(s1->version(), s2->version());
+ if (i != 0)
+ return i < 0;
+ }
+
+ // We should never have two different symbols with the same name and
+ // version, where one doesn't forward to the other.
+ if (s1 == s2)
+ return false;
+ if (s1->is_forwarder() && !s2->is_forwarder())
+ return true;
+ if (!s1->is_forwarder() && s2->is_forwarder())
+ return false;
+ gold_unreachable();
+}
+
+// Gather cross reference information from a list of inputs.
+
+void
+Cref_inputs::gather_cref(const Objects* objects, Cref_table* table) const
+{
+ for (Objects::const_iterator po = objects->begin();
+ po != objects->end();
+ ++po)
+ {
+ const Object::Symbols* symbols = (*po)->get_global_symbols();
+ if (symbols == NULL)
+ continue;
+ for (Object::Symbols::const_iterator ps = symbols->begin();
+ ps != symbols->end();
+ ++ps)
+ {
+ const Symbol* sym = *ps;
+ if (sym == NULL)
+ continue;
+ Objects* const onull = NULL;
+ std::pair<Cref_table::iterator, bool> ins =
+ table->insert(std::make_pair(sym, onull));
+ Cref_table::iterator pc = ins.first;
+ if (ins.second)
+ pc->second = new Objects();
+ if (sym->source() == Symbol::FROM_OBJECT
+ && sym->object() == *po
+ && sym->is_defined())
+ pc->second->insert(pc->second->begin(), *po);
+ else
+ pc->second->push_back(*po);
+ }
+ }
+}
+
+// The column where the file name starts in a cross reference table.
+
+static const size_t filecol = 50;
+
+// Print a cross reference table.
+
+void
+Cref_inputs::print_cref(const Symbol_table*, FILE* f) const
+{
+ Cref_table table;
+ this->gather_cref(&this->objects_, &table);
+ for (Archives::const_iterator p = this->archives_.begin();
+ p != this->archives_.end();
+ ++p)
+ this->gather_cref(p->second.objects, &table);
+
+ for (Cref_table::const_iterator pc = table.begin();
+ pc != table.end();
+ ++pc)
+ {
+ // If all the objects are dynamic, skip this symbol.
+ const Symbol* sym = pc->first;
+ const Objects* objects = pc->second;
+ Objects::const_iterator po;
+ for (po = objects->begin(); po != objects->end(); ++po)
+ if (!(*po)->is_dynamic())
+ break;
+ if (po == objects->end())
+ continue;
+
+ std::string s = sym->demangled_name();
+ if (sym->version() != NULL)
+ {
+ s += '@';
+ if (sym->is_default())
+ s += '@';
+ s += sym->version();
+ }
+
+ fputs(s.c_str(), f);
+
+ size_t len = s.length();
+
+ for (po = objects->begin(); po != objects->end(); ++po)
+ {
+ int n = len < filecol ? filecol - len : 1;
+ fprintf(f, "%*c%s\n", n, ' ', (*po)->name().c_str());
+ len = 0;
+ }
+ }
+}
+