stringpool.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. // stringpool.cc -- a string pool for gold
  2. // Copyright (C) 2006-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 <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 strings.
  194. offset = align_address(offset, this->addralign_);
  195. this->offset_ = offset + (length + 1) * sizeof(Stringpool_char);
  196. }
  197. this->key_to_offset_.push_back(offset);
  198. }
  199. template<typename Stringpool_char>
  200. const Stringpool_char*
  201. Stringpool_template<Stringpool_char>::add_with_length(const Stringpool_char* s,
  202. size_t length,
  203. bool copy,
  204. Key* pkey)
  205. {
  206. typedef std::pair<typename String_set_type::iterator, bool> Insert_type;
  207. // We add 1 so that 0 is always invalid.
  208. const Key k = this->key_to_offset_.size() + 1;
  209. if (!copy)
  210. {
  211. // When we don't need to copy the string, we can call insert
  212. // directly.
  213. std::pair<Hashkey, Hashval> element(Hashkey(s, length), k);
  214. Insert_type ins = this->string_set_.insert(element);
  215. typename String_set_type::const_iterator p = ins.first;
  216. if (ins.second)
  217. {
  218. // We just added the string. The key value has now been
  219. // used.
  220. this->new_key_offset(length);
  221. }
  222. else
  223. {
  224. gold_assert(k != p->second);
  225. }
  226. if (pkey != NULL)
  227. *pkey = p->second;
  228. return p->first.string;
  229. }
  230. // When we have to copy the string, we look it up twice in the hash
  231. // table. The problem is that we can't insert S before we
  232. // canonicalize it by copying it into the canonical list. The hash
  233. // code will only be computed once.
  234. Hashkey hk(s, length);
  235. typename String_set_type::const_iterator p = this->string_set_.find(hk);
  236. if (p != this->string_set_.end())
  237. {
  238. if (pkey != NULL)
  239. *pkey = p->second;
  240. return p->first.string;
  241. }
  242. this->new_key_offset(length);
  243. hk.string = this->add_string(s, length);
  244. // The contents of the string stay the same, so we don't need to
  245. // adjust hk.hash_code or hk.length.
  246. std::pair<Hashkey, Hashval> element(hk, k);
  247. Insert_type ins = this->string_set_.insert(element);
  248. gold_assert(ins.second);
  249. if (pkey != NULL)
  250. *pkey = k;
  251. return hk.string;
  252. }
  253. template<typename Stringpool_char>
  254. const Stringpool_char*
  255. Stringpool_template<Stringpool_char>::find(const Stringpool_char* s,
  256. Key* pkey) const
  257. {
  258. Hashkey hk(s);
  259. typename String_set_type::const_iterator p = this->string_set_.find(hk);
  260. if (p == this->string_set_.end())
  261. return NULL;
  262. if (pkey != NULL)
  263. *pkey = p->second;
  264. return p->first.string;
  265. }
  266. // Comparison routine used when sorting into an ELF strtab. We want
  267. // to sort this so that when one string is a suffix of another, we
  268. // always see the shorter string immediately after the longer string.
  269. // For example, we want to see these strings in this order:
  270. // abcd
  271. // cd
  272. // d
  273. // When strings are not suffixes, we don't care what order they are
  274. // in, but we need to ensure that suffixes wind up next to each other.
  275. // So we do a reversed lexicographic sort on the reversed string.
  276. template<typename Stringpool_char>
  277. bool
  278. Stringpool_template<Stringpool_char>::Stringpool_sort_comparison::operator()(
  279. const Stringpool_sort_info& sort_info1,
  280. const Stringpool_sort_info& sort_info2) const
  281. {
  282. const Hashkey& h1(sort_info1->first);
  283. const Hashkey& h2(sort_info2->first);
  284. const Stringpool_char* s1 = h1.string;
  285. const Stringpool_char* s2 = h2.string;
  286. const size_t len1 = h1.length;
  287. const size_t len2 = h2.length;
  288. const size_t minlen = len1 < len2 ? len1 : len2;
  289. const Stringpool_char* p1 = s1 + len1 - 1;
  290. const Stringpool_char* p2 = s2 + len2 - 1;
  291. for (size_t i = minlen; i > 0; --i, --p1, --p2)
  292. {
  293. if (*p1 != *p2)
  294. return *p1 > *p2;
  295. }
  296. return len1 > len2;
  297. }
  298. // Return whether s1 is a suffix of s2.
  299. template<typename Stringpool_char>
  300. bool
  301. Stringpool_template<Stringpool_char>::is_suffix(const Stringpool_char* s1,
  302. size_t len1,
  303. const Stringpool_char* s2,
  304. size_t len2)
  305. {
  306. if (len1 > len2)
  307. return false;
  308. return memcmp(s1, s2 + len2 - len1, len1 * sizeof(Stringpool_char)) == 0;
  309. }
  310. // Turn the stringpool into an ELF strtab: determine the offsets of
  311. // each string in the table.
  312. template<typename Stringpool_char>
  313. void
  314. Stringpool_template<Stringpool_char>::set_string_offsets()
  315. {
  316. if (this->strtab_size_ != 0)
  317. {
  318. // We've already computed the offsets.
  319. return;
  320. }
  321. const size_t charsize = sizeof(Stringpool_char);
  322. // Offset 0 may be reserved for the empty string.
  323. section_offset_type offset = this->zero_null_ ? charsize : 0;
  324. // Sorting to find suffixes can take over 25% of the total CPU time
  325. // used by the linker. Since it's merely an optimization to reduce
  326. // the strtab size, and gives a relatively small benefit (it's
  327. // typically rare for a symbol to be a suffix of another), we only
  328. // take the time to sort when the user asks for heavy optimization.
  329. if (!this->optimize_)
  330. {
  331. // If we are not optimizing, the offsets are already assigned.
  332. offset = this->offset_;
  333. }
  334. else
  335. {
  336. size_t count = this->string_set_.size();
  337. std::vector<Stringpool_sort_info> v;
  338. v.reserve(count);
  339. for (typename String_set_type::iterator p = this->string_set_.begin();
  340. p != this->string_set_.end();
  341. ++p)
  342. v.push_back(Stringpool_sort_info(p));
  343. std::sort(v.begin(), v.end(), Stringpool_sort_comparison());
  344. section_offset_type last_offset = -1;
  345. for (typename std::vector<Stringpool_sort_info>::iterator last = v.end(),
  346. curr = v.begin();
  347. curr != v.end();
  348. last = curr++)
  349. {
  350. section_offset_type this_offset;
  351. if (this->zero_null_ && (*curr)->first.string[0] == 0)
  352. this_offset = 0;
  353. else if (last != v.end()
  354. && ((((*curr)->first.length - (*last)->first.length)
  355. % this->addralign_) == 0)
  356. && is_suffix((*curr)->first.string,
  357. (*curr)->first.length,
  358. (*last)->first.string,
  359. (*last)->first.length))
  360. this_offset = (last_offset
  361. + (((*last)->first.length - (*curr)->first.length)
  362. * charsize));
  363. else
  364. {
  365. this_offset = align_address(offset, this->addralign_);
  366. offset = this_offset + ((*curr)->first.length + 1) * charsize;
  367. }
  368. this->key_to_offset_[(*curr)->second - 1] = this_offset;
  369. last_offset = this_offset;
  370. }
  371. }
  372. this->strtab_size_ = offset;
  373. }
  374. // Get the offset of a string in the ELF strtab. The string must
  375. // exist.
  376. template<typename Stringpool_char>
  377. section_offset_type
  378. Stringpool_template<Stringpool_char>::get_offset(const Stringpool_char* s)
  379. const
  380. {
  381. return this->get_offset_with_length(s, string_length(s));
  382. }
  383. template<typename Stringpool_char>
  384. section_offset_type
  385. Stringpool_template<Stringpool_char>::get_offset_with_length(
  386. const Stringpool_char* s,
  387. size_t length) const
  388. {
  389. gold_assert(this->strtab_size_ != 0);
  390. Hashkey hk(s, length);
  391. typename String_set_type::const_iterator p = this->string_set_.find(hk);
  392. if (p != this->string_set_.end())
  393. return this->key_to_offset_[p->second - 1];
  394. gold_unreachable();
  395. }
  396. // Write the ELF strtab into the buffer.
  397. template<typename Stringpool_char>
  398. void
  399. Stringpool_template<Stringpool_char>::write_to_buffer(
  400. unsigned char* buffer,
  401. section_size_type bufsize)
  402. {
  403. gold_assert(this->strtab_size_ != 0);
  404. gold_assert(bufsize >= this->strtab_size_);
  405. if (this->zero_null_)
  406. buffer[0] = '\0';
  407. for (typename String_set_type::const_iterator p = this->string_set_.begin();
  408. p != this->string_set_.end();
  409. ++p)
  410. {
  411. const int len = (p->first.length + 1) * sizeof(Stringpool_char);
  412. const section_offset_type offset = this->key_to_offset_[p->second - 1];
  413. gold_assert(static_cast<section_size_type>(offset) + len
  414. <= this->strtab_size_);
  415. memcpy(buffer + offset, p->first.string, len);
  416. }
  417. }
  418. // Write the ELF strtab into the output file at the specified offset.
  419. template<typename Stringpool_char>
  420. void
  421. Stringpool_template<Stringpool_char>::write(Output_file* of, off_t offset)
  422. {
  423. gold_assert(this->strtab_size_ != 0);
  424. unsigned char* view = of->get_output_view(offset, this->strtab_size_);
  425. this->write_to_buffer(view, this->strtab_size_);
  426. of->write_output_view(offset, this->strtab_size_, view);
  427. }
  428. // Print statistical information to stderr. This is used for --stats.
  429. template<typename Stringpool_char>
  430. void
  431. Stringpool_template<Stringpool_char>::print_stats(const char* name) const
  432. {
  433. #if defined(HAVE_UNORDERED_MAP) || defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
  434. fprintf(stderr, _("%s: %s entries: %zu; buckets: %zu\n"),
  435. program_name, name, this->string_set_.size(),
  436. this->string_set_.bucket_count());
  437. #else
  438. fprintf(stderr, _("%s: %s entries: %zu\n"),
  439. program_name, name, this->table_.size());
  440. #endif
  441. fprintf(stderr, _("%s: %s Stringdata structures: %zu\n"),
  442. program_name, name, this->strings_.size());
  443. }
  444. // Instantiate the templates we need.
  445. template
  446. class Stringpool_template<char>;
  447. template
  448. class Stringpool_template<uint16_t>;
  449. template
  450. class Stringpool_template<uint32_t>;
  451. } // End namespace gold.