cref.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // cref.cc -- cross reference for gold
  2. // Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <cerrno>
  19. #include <cstdio>
  20. #include <cstring>
  21. #include <map>
  22. #include <string>
  23. #include <vector>
  24. #include "object.h"
  25. #include "archive.h"
  26. #include "symtab.h"
  27. #include "cref.h"
  28. namespace gold
  29. {
  30. // Class Cref_inputs. This is used to hold the list of input files
  31. // for cross referencing.
  32. class Cref_inputs
  33. {
  34. public:
  35. Cref_inputs()
  36. : objects_(), archives_(), current_(&this->objects_)
  37. { }
  38. // Add an input object file.
  39. void
  40. add_object(Object* object);
  41. // Start adding an archive. We support nested archives for future
  42. // flexibility.
  43. void
  44. add_archive_start(Archive*);
  45. // Finish adding an archive.
  46. void
  47. add_archive_stop(Archive*);
  48. // Report symbol counts.
  49. void
  50. print_symbol_counts(const Symbol_table*, FILE*) const;
  51. // Print a cross reference table.
  52. void
  53. print_cref(const Symbol_table*, FILE*) const;
  54. private:
  55. // A list of input objects.
  56. typedef std::vector<Object*> Objects;
  57. // Information we record for an archive.
  58. struct Archive_info
  59. {
  60. // Archive name.
  61. std::string name;
  62. // List of objects included from the archive.
  63. Objects* objects;
  64. // Number of archive members.
  65. size_t member_count;
  66. };
  67. // A mapping from the name of an archive to the list of objects in
  68. // that archive.
  69. typedef std::map<std::string, Archive_info> Archives;
  70. // For --cref, we build a cross reference table which maps from
  71. // symbols to lists of objects. The symbols are sorted
  72. // alphabetically.
  73. class Cref_table_compare
  74. {
  75. public:
  76. bool
  77. operator()(const Symbol*, const Symbol*) const;
  78. };
  79. typedef std::map<const Symbol*, Objects*, Cref_table_compare> Cref_table;
  80. // Report symbol counts for a list of Objects.
  81. void
  82. print_objects_symbol_counts(const Symbol_table*, FILE*, const Objects*) const;
  83. // Report symbol counts for an object.
  84. void
  85. print_object_symbol_counts(const Symbol_table*, FILE*, const Object*) const;
  86. // Gather cross reference information.
  87. void
  88. gather_cref(const Objects*, Cref_table*) const;
  89. // List of input objects.
  90. Objects objects_;
  91. // List of input archives. This is a mapping from the archive file
  92. // name to the list of objects.
  93. Archives archives_;
  94. // The list to which we are currently adding objects.
  95. Objects* current_;
  96. };
  97. // Add an object.
  98. void
  99. Cref_inputs::add_object(Object* object)
  100. {
  101. this->current_->push_back(object);
  102. }
  103. // Start adding an archive.
  104. void
  105. Cref_inputs::add_archive_start(Archive* archive)
  106. {
  107. gold_assert(this->current_ == &this->objects_);
  108. if (this->archives_.find(archive->name()) == this->archives_.end())
  109. {
  110. Archive_info* pai = &this->archives_[archive->name()];
  111. pai->name = archive->filename();
  112. pai->objects = new Objects();
  113. pai->member_count = archive->count_members();
  114. }
  115. this->current_ = this->archives_[archive->name()].objects;
  116. }
  117. // Stop adding an archive.
  118. void
  119. Cref_inputs::add_archive_stop(Archive*)
  120. {
  121. gold_assert(this->current_ != &this->objects_);
  122. this->current_ = &this->objects_;
  123. }
  124. // Report symbol counts for an object.
  125. void
  126. Cref_inputs::print_object_symbol_counts(const Symbol_table* symtab,
  127. FILE* f,
  128. const Object* object) const
  129. {
  130. size_t defined, used;
  131. object->get_global_symbol_counts(symtab, &defined, &used);
  132. fprintf(f, "symbols %s %zu %zu\n", object->name().c_str(), defined, used);
  133. }
  134. // Report symbol counts for a list of inputs.
  135. void
  136. Cref_inputs::print_objects_symbol_counts(const Symbol_table* symtab,
  137. FILE* f,
  138. const Objects* objects) const
  139. {
  140. for (Objects::const_iterator p = objects->begin();
  141. p != objects->end();
  142. ++p)
  143. this->print_object_symbol_counts(symtab, f, *p);
  144. }
  145. // Print symbol counts. This implements --print-symbol-counts. This
  146. // is intended to be easily read by a program. This outputs a series
  147. // of lines. There are two different types of lines.
  148. // The first is "symbols FILENAME DEFINED USED". FILENAME is the name
  149. // of an object file included in the link; for an archive, this will
  150. // be ARCHIVEFILENAME(MEMBERNAME). DEFINED is the number of symbols
  151. // which the object file defines. USED is the number of symbols which
  152. // are used in the final output; this is the number of symbols which
  153. // appear in the final output table as having been defined by this
  154. // object. These numbers will be different when weak symbols are
  155. // used, and they will be different for dynamic objects.
  156. // The second is "archives FILENAME MEMBERS USED". FILENAME is the
  157. // name of an archive file included in the link. MEMBERS is the
  158. // number of members of the archive. USED is the number of archive
  159. // members included in the link.
  160. void
  161. Cref_inputs::print_symbol_counts(const Symbol_table* symtab, FILE* f) const
  162. {
  163. this->print_objects_symbol_counts(symtab, f, &this->objects_);
  164. for (Archives::const_iterator p = this->archives_.begin();
  165. p != this->archives_.end();
  166. ++p)
  167. {
  168. fprintf(f, "archive %s %zu %zu\n", p->second.name.c_str(),
  169. p->second.member_count, p->second.objects->size());
  170. this->print_objects_symbol_counts(symtab, f, p->second.objects);
  171. }
  172. }
  173. // Sort symbols for the cross reference table.
  174. bool
  175. Cref_inputs::Cref_table_compare::operator()(const Symbol* s1,
  176. const Symbol* s2) const
  177. {
  178. int i = strcmp(s1->name(), s2->name());
  179. if (i != 0)
  180. return i < 0;
  181. if (s1->version() == NULL)
  182. {
  183. if (s2->version() != NULL)
  184. return true;
  185. }
  186. else if (s2->version() == NULL)
  187. return false;
  188. else
  189. {
  190. i = strcmp(s1->version(), s2->version());
  191. if (i != 0)
  192. return i < 0;
  193. }
  194. // We should never have two different symbols with the same name and
  195. // version.
  196. if (s1 == s2)
  197. return false;
  198. gold_unreachable();
  199. }
  200. // Gather cross reference information from a list of inputs.
  201. void
  202. Cref_inputs::gather_cref(const Objects* objects, Cref_table* table) const
  203. {
  204. for (Objects::const_iterator po = objects->begin();
  205. po != objects->end();
  206. ++po)
  207. {
  208. const Object::Symbols* symbols = (*po)->get_global_symbols();
  209. if (symbols == NULL)
  210. continue;
  211. for (Object::Symbols::const_iterator ps = symbols->begin();
  212. ps != symbols->end();
  213. ++ps)
  214. {
  215. const Symbol* sym = *ps;
  216. if (sym == NULL)
  217. continue;
  218. Objects* const onull = NULL;
  219. std::pair<Cref_table::iterator, bool> ins =
  220. table->insert(std::make_pair(sym, onull));
  221. Cref_table::iterator pc = ins.first;
  222. if (ins.second)
  223. pc->second = new Objects();
  224. if (sym->source() == Symbol::FROM_OBJECT
  225. && sym->object() == *po
  226. && sym->is_defined())
  227. pc->second->insert(pc->second->begin(), *po);
  228. else
  229. pc->second->push_back(*po);
  230. }
  231. }
  232. }
  233. // The column where the file name starts in a cross reference table.
  234. static const size_t filecol = 50;
  235. // Print a cross reference table.
  236. void
  237. Cref_inputs::print_cref(const Symbol_table*, FILE* f) const
  238. {
  239. Cref_table table;
  240. this->gather_cref(&this->objects_, &table);
  241. for (Archives::const_iterator p = this->archives_.begin();
  242. p != this->archives_.end();
  243. ++p)
  244. this->gather_cref(p->second.objects, &table);
  245. for (Cref_table::const_iterator pc = table.begin();
  246. pc != table.end();
  247. ++pc)
  248. {
  249. // If all the objects are dynamic, skip this symbol.
  250. const Symbol* sym = pc->first;
  251. const Objects* objects = pc->second;
  252. Objects::const_iterator po;
  253. for (po = objects->begin(); po != objects->end(); ++po)
  254. if (!(*po)->is_dynamic())
  255. break;
  256. if (po == objects->end())
  257. continue;
  258. std::string s = sym->demangled_name();
  259. if (sym->version() != NULL)
  260. {
  261. s += '@';
  262. if (sym->is_default())
  263. s += '@';
  264. s += sym->version();
  265. }
  266. fputs(s.c_str(), f);
  267. size_t len = s.length();
  268. for (po = objects->begin(); po != objects->end(); ++po)
  269. {
  270. int n = len < filecol ? filecol - len : 1;
  271. fprintf(f, "%*c%s\n", n, ' ', (*po)->name().c_str());
  272. len = 0;
  273. }
  274. }
  275. }
  276. // Class Cref.
  277. // Make sure the Cref_inputs object has been created.
  278. void
  279. Cref::need_inputs()
  280. {
  281. if (this->inputs_ == NULL)
  282. this->inputs_ = new Cref_inputs();
  283. }
  284. // Add an input object file.
  285. void
  286. Cref::add_object(Object* object)
  287. {
  288. this->need_inputs();
  289. this->inputs_->add_object(object);
  290. }
  291. // Start adding an archive.
  292. void
  293. Cref::add_archive_start(Archive* archive)
  294. {
  295. this->need_inputs();
  296. this->inputs_->add_archive_start(archive);
  297. }
  298. // Stop adding an archive.
  299. void
  300. Cref::add_archive_stop(Archive* archive)
  301. {
  302. this->inputs_->add_archive_stop(archive);
  303. }
  304. // Print symbol counts.
  305. void
  306. Cref::print_symbol_counts(const Symbol_table* symtab) const
  307. {
  308. if (parameters->options().user_set_print_symbol_counts()
  309. && this->inputs_ != NULL)
  310. {
  311. FILE* f;
  312. if (strcmp(parameters->options().print_symbol_counts(), "-") == 0)
  313. f = stdout;
  314. else
  315. {
  316. f = fopen(parameters->options().print_symbol_counts(), "w");
  317. if (f == NULL)
  318. gold_error(_("cannot open symbol count file %s: %s"),
  319. parameters->options().print_symbol_counts(),
  320. strerror(errno));
  321. }
  322. if (f != NULL)
  323. this->inputs_->print_symbol_counts(symtab, f);
  324. }
  325. }
  326. // Print a cross reference table.
  327. void
  328. Cref::print_cref(const Symbol_table* symtab, FILE* f) const
  329. {
  330. fprintf(f, _("\nCross Reference Table\n\n"));
  331. const char* msg = _("Symbol");
  332. int len = filecol - strlen(msg);
  333. fprintf(f, "%s%*c%s\n", msg, len, ' ', _("File"));
  334. if (parameters->options().cref() && this->inputs_ != NULL)
  335. this->inputs_->print_cref(symtab, f);
  336. }
  337. } // End namespace gold.