stringpool.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. // stringpool.cc -- a string pool for gold
  2. // Copyright (C) 2006-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 <cstring>
  19. #include <algorithm>
  20. #include <vector>
  21. #include "output.h"
  22. #include "parameters.h"
  23. #include "stringpool.h"
  24. namespace gold
  25. {
  26. template<typename Stringpool_char>
  27. Stringpool_template<Stringpool_char>::Stringpool_template(uint64_t addralign)
  28. : string_set_(), key_to_offset_(), strings_(), strtab_size_(0),
  29. zero_null_(true), optimize_(false), offset_(sizeof(Stringpool_char)),
  30. addralign_(addralign)
  31. {
  32. if (parameters->options_valid()
  33. && parameters->options().optimize() >= 2
  34. && addralign <= sizeof(Stringpool_char))
  35. this->optimize_ = true;
  36. }
  37. template<typename Stringpool_char>
  38. void
  39. Stringpool_template<Stringpool_char>::clear()
  40. {
  41. for (typename std::list<Stringdata*>::iterator p = this->strings_.begin();
  42. p != this->strings_.end();
  43. ++p)
  44. delete[] reinterpret_cast<char*>(*p);
  45. this->strings_.clear();
  46. this->key_to_offset_.clear();
  47. this->string_set_.clear();
  48. }
  49. template<typename Stringpool_char>
  50. Stringpool_template<Stringpool_char>::~Stringpool_template()
  51. {
  52. this->clear();
  53. }
  54. // Resize the internal hashtable with the expectation we'll get n new
  55. // elements. Note that the hashtable constructor takes a "number of
  56. // buckets you'd like," rather than "number of elements you'd like,"
  57. // but that's the best we can do.
  58. template<typename Stringpool_char>
  59. void
  60. Stringpool_template<Stringpool_char>::reserve(unsigned int n)
  61. {
  62. this->key_to_offset_.reserve(n);
  63. #if defined(HAVE_UNORDERED_MAP)
  64. this->string_set_.rehash(this->string_set_.size() + n);
  65. return;
  66. #elif defined(HAVE_TR1_UNORDERED_MAP)
  67. // rehash() implementation is broken in gcc 4.0.3's stl
  68. //this->string_set_.rehash(this->string_set_.size() + n);
  69. //return;
  70. #elif defined(HAVE_EXT_HASH_MAP)
  71. this->string_set_.resize(this->string_set_.size() + n);
  72. return;
  73. #endif
  74. // This is the generic "reserve" code, if no #ifdef above triggers.
  75. String_set_type new_string_set(this->string_set_.size() + n);
  76. new_string_set.insert(this->string_set_.begin(), this->string_set_.end());
  77. this->string_set_.swap(new_string_set);
  78. }
  79. // Compare two strings of arbitrary character type for equality.
  80. template<typename Stringpool_char>
  81. bool
  82. Stringpool_template<Stringpool_char>::string_equal(const Stringpool_char* s1,
  83. const Stringpool_char* s2)
  84. {
  85. while (*s1 != 0)
  86. if (*s1++ != *s2++)
  87. return false;
  88. return *s2 == 0;
  89. }
  90. // Specialize string_equal for char.
  91. template<>
  92. inline bool
  93. Stringpool_template<char>::string_equal(const char* s1, const char* s2)
  94. {
  95. return strcmp(s1, s2) == 0;
  96. }
  97. // Equality comparison function for the hash table.
  98. template<typename Stringpool_char>
  99. inline bool
  100. Stringpool_template<Stringpool_char>::Stringpool_eq::operator()(
  101. const Hashkey& h1,
  102. const Hashkey& h2) const
  103. {
  104. return (h1.hash_code == h2.hash_code
  105. && h1.length == h2.length
  106. && (h1.string == h2.string
  107. || memcmp(h1.string, h2.string,
  108. h1.length * sizeof(Stringpool_char)) == 0));
  109. }
  110. // Hash function. The length is in characters, not bytes.
  111. template<typename Stringpool_char>
  112. size_t
  113. Stringpool_template<Stringpool_char>::string_hash(const Stringpool_char* s,
  114. size_t length)
  115. {
  116. return gold::string_hash<Stringpool_char>(s, length);
  117. }
  118. // Add the string S to the list of canonical strings. Return a
  119. // pointer to the canonical string. If PKEY is not NULL, set *PKEY to
  120. // the key. LENGTH is the length of S in characters. Note that S may
  121. // not be NUL terminated.
  122. template<typename Stringpool_char>
  123. const Stringpool_char*
  124. Stringpool_template<Stringpool_char>::add_string(const Stringpool_char* s,
  125. size_t len)
  126. {
  127. // We are in trouble if we've already computed the string offsets.
  128. gold_assert(this->strtab_size_ == 0);
  129. // The size we allocate for a new Stringdata.
  130. const size_t buffer_size = 1000;
  131. // The amount we multiply the Stringdata index when calculating the
  132. // key.
  133. const size_t key_mult = 1024;
  134. gold_assert(key_mult >= buffer_size);
  135. // Convert len to the number of bytes we need to allocate, including
  136. // the null character.
  137. len = (len + 1) * sizeof(Stringpool_char);
  138. size_t alc;
  139. bool front = true;
  140. if (len > buffer_size)
  141. {
  142. alc = sizeof(Stringdata) + len;
  143. front = false;
  144. }
  145. else if (this->strings_.empty())
  146. alc = sizeof(Stringdata) + buffer_size;
  147. else
  148. {
  149. Stringdata* psd = this->strings_.front();
  150. if (len > psd->alc - psd->len)
  151. alc = sizeof(Stringdata) + buffer_size;
  152. else
  153. {
  154. char* ret = psd->data + psd->len;
  155. memcpy(ret, s, len - sizeof(Stringpool_char));
  156. memset(ret + len - sizeof(Stringpool_char), 0,
  157. sizeof(Stringpool_char));
  158. psd->len += len;
  159. return reinterpret_cast<const Stringpool_char*>(ret);
  160. }
  161. }
  162. Stringdata* psd = reinterpret_cast<Stringdata*>(new char[alc]);
  163. psd->alc = alc - sizeof(Stringdata);
  164. memcpy(psd->data, s, len - sizeof(Stringpool_char));
  165. memset(psd->data + len - sizeof(Stringpool_char), 0,
  166. sizeof(Stringpool_char));
  167. psd->len = len;
  168. if (front)
  169. this->strings_.push_front(psd);
  170. else
  171. this->strings_.push_back(psd);
  172. return reinterpret_cast<const Stringpool_char*>(psd->data);
  173. }
  174. // Add a string to a string pool.
  175. template<typename Stringpool_char>
  176. const Stringpool_char*
  177. Stringpool_template<Stringpool_char>::add(const Stringpool_char* s, bool copy,
  178. Key* pkey)
  179. {
  180. return this->add_with_length(s, string_length(s), copy, pkey);
  181. }
  182. // Add a new key offset entry.
  183. template<typename Stringpool_char>
  184. void
  185. Stringpool_template<Stringpool_char>::new_key_offset(size_t length)
  186. {
  187. section_offset_type offset;
  188. if (this->zero_null_ && length == 0)
  189. offset = 0;
  190. else
  191. {
  192. offset = this->offset_;
  193. // Align non-zero length strings.
  194. if (length != 0)
  195. offset = align_address(offset, this->addralign_);
  196. this->offset_ = offset + (length + 1) * sizeof(Stringpool_char);
  197. }
  198. this->key_to_offset_.push_back(offset);
  199. }
  200. template<typename Stringpool_char>
  201. const Stringpool_char*
  202. Stringpool_template<Stringpool_char>::add_with_length(const Stringpool_char* s,
  203. size_t length,
  204. bool copy,
  205. Key* pkey)
  206. {
  207. typedef std::pair<typename String_set_type::iterator, bool> Insert_type;
  208. // We add 1 so that 0 is always invalid.
  209. const Key k = this->key_to_offset_.size() + 1;
  210. if (!copy)
  211. {
  212. // When we don't need to copy the string, we can call insert
  213. // directly.
  214. std::pair<Hashkey, Hashval> element(Hashkey(s, length), k);
  215. Insert_type ins = this->string_set_.insert(element);
  216. typename String_set_type::const_iterator p = ins.first;
  217. if (ins.second)
  218. {
  219. // We just added the string. The key value has now been
  220. // used.
  221. this->new_key_offset(length);
  222. }
  223. else
  224. {
  225. gold_assert(k != p->second);
  226. }
  227. if (pkey != NULL)
  228. *pkey = p->second;
  229. return p->first.string;
  230. }
  231. // When we have to copy the string, we look it up twice in the hash
  232. // table. The problem is that we can't insert S before we
  233. // canonicalize it by copying it into the canonical list. The hash
  234. // code will only be computed once.
  235. Hashkey hk(s, length);
  236. typename String_set_type::const_iterator p = this->string_set_.find(hk);
  237. if (p != this->string_set_.end())
  238. {
  239. if (pkey != NULL)
  240. *pkey = p->second;
  241. return p->first.string;
  242. }
  243. this->new_key_offset(length);
  244. hk.string = this->add_string(s, length);
  245. // The contents of the string stay the same, so we don't need to
  246. // adjust hk.hash_code or hk.length.
  247. std::pair<Hashkey, Hashval> element(hk, k);
  248. Insert_type ins = this->string_set_.insert(element);
  249. gold_assert(ins.second);
  250. if (pkey != NULL)
  251. *pkey = k;
  252. return hk.string;
  253. }
  254. template<typename Stringpool_char>
  255. const Stringpool_char*
  256. Stringpool_template<Stringpool_char>::find(const Stringpool_char* s,
  257. Key* pkey) const
  258. {
  259. Hashkey hk(s);
  260. typename String_set_type::const_iterator p = this->string_set_.find(hk);
  261. if (p == this->string_set_.end())
  262. return NULL;
  263. if (pkey != NULL)
  264. *pkey = p->second;
  265. return p->first.string;
  266. }
  267. // Comparison routine used when sorting into an ELF strtab. We want
  268. // to sort this so that when one string is a suffix of another, we
  269. // always see the shorter string immediately after the longer string.
  270. // For example, we want to see these strings in this order:
  271. // abcd
  272. // cd
  273. // d
  274. // When strings are not suffixes, we don't care what order they are
  275. // in, but we need to ensure that suffixes wind up next to each other.
  276. // So we do a reversed lexicographic sort on the reversed string.
  277. template<typename Stringpool_char>
  278. bool
  279. Stringpool_template<Stringpool_char>::Stringpool_sort_comparison::operator()(
  280. const Stringpool_sort_info& sort_info1,
  281. const Stringpool_sort_info& sort_info2) const
  282. {
  283. const Hashkey& h1(sort_info1->first);
  284. const Hashkey& h2(sort_info2->first);
  285. const Stringpool_char* s1 = h1.string;
  286. const Stringpool_char* s2 = h2.string;
  287. const size_t len1 = h1.length;
  288. const size_t len2 = h2.length;
  289. const size_t minlen = len1 < len2 ? len1 : len2;
  290. const Stringpool_char* p1 = s1 + len1 - 1;
  291. const Stringpool_char* p2 = s2 + len2 - 1;
  292. for (size_t i = minlen; i > 0; --i, --p1, --p2)
  293. {
  294. if (*p1 != *p2)
  295. return *p1 > *p2;
  296. }
  297. return len1 > len2;
  298. }
  299. // Return whether s1 is a suffix of s2.
  300. template<typename Stringpool_char>
  301. bool
  302. Stringpool_template<Stringpool_char>::is_suffix(const Stringpool_char* s1,
  303. size_t len1,
  304. const Stringpool_char* s2,
  305. size_t len2)
  306. {
  307. if (len1 > len2)
  308. return false;
  309. return memcmp(s1, s2 + len2 - len1, len1 * sizeof(Stringpool_char)) == 0;
  310. }
  311. // Turn the stringpool into an ELF strtab: determine the offsets of
  312. // each string in the table.
  313. template<typename Stringpool_char>
  314. void
  315. Stringpool_template<Stringpool_char>::set_string_offsets()
  316. {
  317. if (this->strtab_size_ != 0)
  318. {
  319. // We've already computed the offsets.
  320. return;
  321. }
  322. const size_t charsize = sizeof(Stringpool_char);
  323. // Offset 0 may be reserved for the empty string.
  324. section_offset_type offset = this->zero_null_ ? charsize : 0;
  325. // Sorting to find suffixes can take over 25% of the total CPU time
  326. // used by the linker. Since it's merely an optimization to reduce
  327. // the strtab size, and gives a relatively small benefit (it's
  328. // typically rare for a symbol to be a suffix of another), we only
  329. // take the time to sort when the user asks for heavy optimization.
  330. if (!this->optimize_)
  331. {
  332. // If we are not optimizing, the offsets are already assigned.
  333. offset = this->offset_;
  334. }
  335. else
  336. {
  337. size_t count = this->string_set_.size();
  338. std::vector<Stringpool_sort_info> v;
  339. v.reserve(count);
  340. for (typename String_set_type::iterator p = this->string_set_.begin();
  341. p != this->string_set_.end();
  342. ++p)
  343. v.push_back(Stringpool_sort_info(p));
  344. std::sort(v.begin(), v.end(), Stringpool_sort_comparison());
  345. section_offset_type last_offset = -1;
  346. for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(),
  347. curr = v.begin();
  348. curr != v.end();
  349. last = curr++)
  350. {
  351. section_offset_type this_offset;
  352. if (this->zero_null_ && (*curr)->first.string[0] == 0)
  353. this_offset = 0;
  354. else if (last != v.end()
  355. && is_suffix((*curr)->first.string,
  356. (*curr)->first.length,
  357. (*last)->first.string,
  358. (*last)->first.length))
  359. this_offset = (last_offset
  360. + (((*last)->first.length - (*curr)->first.length)
  361. * charsize));
  362. else
  363. {
  364. this_offset = align_address(offset, this->addralign_);
  365. offset = this_offset + ((*curr)->first.length + 1) * charsize;
  366. }
  367. this->key_to_offset_[(*curr)->second - 1] = this_offset;
  368. last_offset = this_offset;
  369. }
  370. }
  371. this->strtab_size_ = offset;
  372. }
  373. // Get the offset of a string in the ELF strtab. The string must
  374. // exist.
  375. template<typename Stringpool_char>
  376. section_offset_type
  377. Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s)
  378. const
  379. {
  380. return this->get_offset_with_length(s, string_length(s));
  381. }
  382. template<typename Stringpool_char>
  383. section_offset_type
  384. Stringpool_template<Stringpool_char>::get_offset_with_length(
  385. const Stringpool_char* s,
  386. size_t length) const
  387. {
  388. gold_assert(this->strtab_size_ != 0);
  389. Hashkey hk(s, length);
  390. typename String_set_type::const_iterator p = this->string_set_.find(hk);
  391. if (p != this->string_set_.end())
  392. return this->key_to_offset_[p->second - 1];
  393. gold_unreachable();
  394. }
  395. // Write the ELF strtab into the buffer.
  396. template<typename Stringpool_char>
  397. void
  398. Stringpool_template<Stringpool_char>::write_to_buffer(
  399. unsigned char* buffer,
  400. section_size_type bufsize)
  401. {
  402. gold_assert(this->strtab_size_ != 0);
  403. gold_assert(bufsize >= this->strtab_size_);
  404. if (this->zero_null_)
  405. buffer[0] = '\0';
  406. for (typename String_set_type::const_iterator p = this->string_set_.begin();
  407. p != this->string_set_.end();
  408. ++p)
  409. {
  410. const int len = (p->first.length + 1) * sizeof(Stringpool_char);
  411. const section_offset_type offset = this->key_to_offset_[p->second - 1];
  412. gold_assert(static_cast<section_size_type>(offset) + len
  413. <= this->strtab_size_);
  414. memcpy(buffer + offset, p->first.string, len);
  415. }
  416. }
  417. // Write the ELF strtab into the output file at the specified offset.
  418. template<typename Stringpool_char>
  419. void
  420. Stringpool_template<Stringpool_char>::write(Output_file* of, off_t offset)
  421. {
  422. gold_assert(this->strtab_size_ != 0);
  423. unsigned char* view = of->get_output_view(offset, this->strtab_size_);
  424. this->write_to_buffer(view, this->strtab_size_);
  425. of->write_output_view(offset, this->strtab_size_, view);
  426. }
  427. // Print statistical information to stderr. This is used for --stats.
  428. template<typename Stringpool_char>
  429. void
  430. Stringpool_template<Stringpool_char>::print_stats(const char* name) const
  431. {
  432. #if defined(HAVE_UNORDERED_MAP) || defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
  433. fprintf(stderr, _("%s: %s entries: %zu; buckets: %zu\n"),
  434. program_name, name, this->string_set_.size(),
  435. this->string_set_.bucket_count());
  436. #else
  437. fprintf(stderr, _("%s: %s entries: %zu\n"),
  438. program_name, name, this->table_.size());
  439. #endif
  440. fprintf(stderr, _("%s: %s Stringdata structures: %zu\n"),
  441. program_name, name, this->strings_.size());
  442. }
  443. // Instantiate the templates we need.
  444. template
  445. class Stringpool_template<char>;
  446. template
  447. class Stringpool_template<uint16_t>;
  448. template
  449. class Stringpool_template<uint32_t>;
  450. } // End namespace gold.