cref.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // cref.cc -- cross reference for gold
  2. // Copyright (C) 2008-2022 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, where one doesn't forward to the other.
  196. if (s1 == s2)
  197. return false;
  198. if (s1->is_forwarder() && !s2->is_forwarder())
  199. return true;
  200. if (!s1->is_forwarder() && s2->is_forwarder())
  201. return false;
  202. gold_unreachable();
  203. }
  204. // Gather cross reference information from a list of inputs.
  205. void
  206. Cref_inputs::gather_cref(const Objects* objects, Cref_table* table) const
  207. {
  208. for (Objects::const_iterator po = objects->begin();
  209. po != objects->end();
  210. ++po)
  211. {
  212. const Object::Symbols* symbols = (*po)->get_global_symbols();
  213. if (symbols == NULL)
  214. continue;
  215. for (Object::Symbols::const_iterator ps = symbols->begin();
  216. ps != symbols->end();
  217. ++ps)
  218. {
  219. const Symbol* sym = *ps;
  220. if (sym == NULL)
  221. continue;
  222. Objects* const onull = NULL;
  223. std::pair<Cref_table::iterator, bool> ins =
  224. table->insert(std::make_pair(sym, onull));
  225. Cref_table::iterator pc = ins.first;
  226. if (ins.second)
  227. pc->second = new Objects();
  228. if (sym->source() == Symbol::FROM_OBJECT
  229. && sym->object() == *po
  230. && sym->is_defined())
  231. pc->second->insert(pc->second->begin(), *po);
  232. else
  233. pc->second->push_back(*po);
  234. }
  235. }
  236. }
  237. // The column where the file name starts in a cross reference table.
  238. static const size_t filecol = 50;
  239. // Print a cross reference table.
  240. void
  241. Cref_inputs::print_cref(const Symbol_table*, FILE* f) const
  242. {
  243. Cref_table table;
  244. this->gather_cref(&this->objects_, &table);
  245. for (Archives::const_iterator p = this->archives_.begin();
  246. p != this->archives_.end();
  247. ++p)
  248. this->gather_cref(p->second.objects, &table);
  249. for (Cref_table::const_iterator pc = table.begin();
  250. pc != table.end();
  251. ++pc)
  252. {
  253. // If all the objects are dynamic, skip this symbol.
  254. const Symbol* sym = pc->first;
  255. const Objects* objects = pc->second;
  256. Objects::const_iterator po;
  257. for (po = objects->begin(); po != objects->end(); ++po)
  258. if (!(*po)->is_dynamic())
  259. break;
  260. if (po == objects->end())
  261. continue;
  262. std::string s = sym->demangled_name();
  263. if (sym->version() != NULL)
  264. {
  265. s += '@';
  266. if (sym->is_default())
  267. s += '@';
  268. s += sym->version();
  269. }
  270. fputs(s.c_str(), f);
  271. size_t len = s.length();
  272. for (po = objects->begin(); po != objects->end(); ++po)
  273. {
  274. int n = len < filecol ? filecol - len : 1;
  275. fprintf(f, "%*c%s\n", n, ' ', (*po)->name().c_str());
  276. len = 0;
  277. }
  278. }
  279. }
  280. // Class Cref.
  281. // Make sure the Cref_inputs object has been created.
  282. void
  283. Cref::need_inputs()
  284. {
  285. if (this->inputs_ == NULL)
  286. this->inputs_ = new Cref_inputs();
  287. }
  288. // Add an input object file.
  289. void
  290. Cref::add_object(Object* object)
  291. {
  292. this->need_inputs();
  293. this->inputs_->add_object(object);
  294. }
  295. // Start adding an archive.
  296. void
  297. Cref::add_archive_start(Archive* archive)
  298. {
  299. this->need_inputs();
  300. this->inputs_->add_archive_start(archive);
  301. }
  302. // Stop adding an archive.
  303. void
  304. Cref::add_archive_stop(Archive* archive)
  305. {
  306. this->inputs_->add_archive_stop(archive);
  307. }
  308. // Print symbol counts.
  309. void
  310. Cref::print_symbol_counts(const Symbol_table* symtab) const
  311. {
  312. if (parameters->options().user_set_print_symbol_counts()
  313. && this->inputs_ != NULL)
  314. {
  315. FILE* f;
  316. if (strcmp(parameters->options().print_symbol_counts(), "-") == 0)
  317. f = stdout;
  318. else
  319. {
  320. f = fopen(parameters->options().print_symbol_counts(), "w");
  321. if (f == NULL)
  322. gold_error(_("cannot open symbol count file %s: %s"),
  323. parameters->options().print_symbol_counts(),
  324. strerror(errno));
  325. }
  326. if (f != NULL)
  327. this->inputs_->print_symbol_counts(symtab, f);
  328. }
  329. }
  330. // Print a cross reference table.
  331. void
  332. Cref::print_cref(const Symbol_table* symtab, FILE* f) const
  333. {
  334. fprintf(f, _("\nCross Reference Table\n\n"));
  335. const char* msg = _("Symbol");
  336. int len = filecol - strlen(msg);
  337. fprintf(f, "%s%*c%s\n", msg, len, ' ', _("File"));
  338. if (parameters->options().cref() && this->inputs_ != NULL)
  339. this->inputs_->print_cref(symtab, f);
  340. }
  341. } // End namespace gold.