merge.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. // merge.cc -- handle section merging 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 <cstdlib>
  19. #include <algorithm>
  20. #include "merge.h"
  21. #include "compressed_output.h"
  22. namespace gold
  23. {
  24. // Class Object_merge_map.
  25. // Destructor.
  26. Object_merge_map::~Object_merge_map()
  27. {
  28. for (Section_merge_maps::iterator p = this->section_merge_maps_.begin();
  29. p != this->section_merge_maps_.end();
  30. ++p)
  31. delete p->second;
  32. }
  33. // Get the Input_merge_map to use for an input section, or NULL.
  34. const Object_merge_map::Input_merge_map*
  35. Object_merge_map::get_input_merge_map(unsigned int shndx) const
  36. {
  37. gold_assert(shndx != -1U);
  38. const Section_merge_maps &maps = this->section_merge_maps_;
  39. for (Section_merge_maps::const_iterator i = maps.begin(), e = maps.end();
  40. i != e; ++i)
  41. {
  42. if (i->first == shndx)
  43. return i->second;
  44. }
  45. return NULL;
  46. }
  47. // Get or create the Input_merge_map to use for an input section.
  48. Object_merge_map::Input_merge_map*
  49. Object_merge_map::get_or_make_input_merge_map(
  50. const Output_section_data* output_data, unsigned int shndx) {
  51. Input_merge_map* map = this->get_input_merge_map(shndx);
  52. if (map != NULL)
  53. {
  54. // For a given input section in a given object, every mapping
  55. // must be done with the same Merge_map.
  56. gold_assert(map->output_data == output_data);
  57. return map;
  58. }
  59. Input_merge_map* new_map = new Input_merge_map;
  60. new_map->output_data = output_data;
  61. Section_merge_maps &maps = this->section_merge_maps_;
  62. maps.push_back(std::make_pair(shndx, new_map));
  63. return new_map;
  64. }
  65. // Add a mapping.
  66. void
  67. Object_merge_map::add_mapping(const Output_section_data* output_data,
  68. unsigned int shndx,
  69. section_offset_type input_offset,
  70. section_size_type length,
  71. section_offset_type output_offset)
  72. {
  73. Input_merge_map* map = this->get_or_make_input_merge_map(output_data, shndx);
  74. map->add_mapping(input_offset, length, output_offset);
  75. }
  76. void
  77. Object_merge_map::Input_merge_map::add_mapping(
  78. section_offset_type input_offset, section_size_type length,
  79. section_offset_type output_offset) {
  80. // Try to merge the new entry in the last one we saw.
  81. if (!this->entries.empty())
  82. {
  83. Input_merge_entry& entry(this->entries.back());
  84. // Use section_size_type to avoid signed/unsigned warnings.
  85. section_size_type input_offset_u = input_offset;
  86. section_size_type output_offset_u = output_offset;
  87. // If this entry is not in order, we need to sort the vector
  88. // before looking anything up.
  89. if (input_offset_u < entry.input_offset + entry.length)
  90. {
  91. gold_assert(input_offset < entry.input_offset);
  92. gold_assert(input_offset_u + length
  93. <= static_cast<section_size_type>(entry.input_offset));
  94. this->sorted = false;
  95. }
  96. else if (entry.input_offset + entry.length == input_offset_u
  97. && (output_offset == -1
  98. ? entry.output_offset == -1
  99. : entry.output_offset + entry.length == output_offset_u))
  100. {
  101. entry.length += length;
  102. return;
  103. }
  104. }
  105. Input_merge_entry entry;
  106. entry.input_offset = input_offset;
  107. entry.length = length;
  108. entry.output_offset = output_offset;
  109. this->entries.push_back(entry);
  110. }
  111. // Get the output offset for an input address.
  112. bool
  113. Object_merge_map::get_output_offset(unsigned int shndx,
  114. section_offset_type input_offset,
  115. section_offset_type* output_offset)
  116. {
  117. Input_merge_map* map = this->get_input_merge_map(shndx);
  118. if (map == NULL)
  119. return false;
  120. if (!map->sorted)
  121. {
  122. std::sort(map->entries.begin(), map->entries.end(),
  123. Input_merge_compare());
  124. map->sorted = true;
  125. }
  126. Input_merge_entry entry;
  127. entry.input_offset = input_offset;
  128. std::vector<Input_merge_entry>::const_iterator p =
  129. std::upper_bound(map->entries.begin(), map->entries.end(),
  130. entry, Input_merge_compare());
  131. if (p == map->entries.begin())
  132. return false;
  133. --p;
  134. gold_assert(p->input_offset <= input_offset);
  135. if (input_offset - p->input_offset
  136. >= static_cast<section_offset_type>(p->length))
  137. return false;
  138. *output_offset = p->output_offset;
  139. if (*output_offset != -1)
  140. *output_offset += (input_offset - p->input_offset);
  141. return true;
  142. }
  143. // Return whether this is the merge map for section SHNDX.
  144. const Output_section_data*
  145. Object_merge_map::find_merge_section(unsigned int shndx) const {
  146. const Object_merge_map::Input_merge_map* map =
  147. this->get_input_merge_map(shndx);
  148. if (map == NULL)
  149. return NULL;
  150. return map->output_data;
  151. }
  152. // Initialize a mapping from input offsets to output addresses.
  153. template<int size>
  154. void
  155. Object_merge_map::initialize_input_to_output_map(
  156. unsigned int shndx,
  157. typename elfcpp::Elf_types<size>::Elf_Addr starting_address,
  158. Unordered_map<section_offset_type,
  159. typename elfcpp::Elf_types<size>::Elf_Addr>* initialize_map)
  160. {
  161. Input_merge_map* map = this->get_input_merge_map(shndx);
  162. gold_assert(map != NULL);
  163. gold_assert(initialize_map->empty());
  164. // We know how many entries we are going to add.
  165. // reserve_unordered_map takes an expected count of buckets, not a
  166. // count of elements, so double it to try to reduce collisions.
  167. reserve_unordered_map(initialize_map, map->entries.size() * 2);
  168. for (Input_merge_map::Entries::const_iterator p = map->entries.begin();
  169. p != map->entries.end();
  170. ++p)
  171. {
  172. section_offset_type output_offset = p->output_offset;
  173. if (output_offset != -1)
  174. output_offset += starting_address;
  175. else
  176. {
  177. // If we see a relocation against an address we have chosen
  178. // to discard, we relocate to zero. FIXME: We could also
  179. // issue a warning in this case; that would require
  180. // reporting this somehow and checking it in the routines in
  181. // reloc.h.
  182. output_offset = 0;
  183. }
  184. initialize_map->insert(std::make_pair(p->input_offset, output_offset));
  185. }
  186. }
  187. // Class Output_merge_base.
  188. // Return the output offset for an input offset. The input address is
  189. // at offset OFFSET in section SHNDX in OBJECT. If we know the
  190. // offset, set *POUTPUT and return true. Otherwise return false.
  191. bool
  192. Output_merge_base::do_output_offset(const Relobj* object,
  193. unsigned int shndx,
  194. section_offset_type offset,
  195. section_offset_type* poutput) const
  196. {
  197. return object->merge_output_offset(shndx, offset, poutput);
  198. }
  199. // Record a merged input section for script processing.
  200. void
  201. Output_merge_base::record_input_section(Relobj* relobj, unsigned int shndx)
  202. {
  203. gold_assert(this->keeps_input_sections_ && relobj != NULL);
  204. // If this is the first input section, record it. We need do this because
  205. // this->input_sections_ is unordered.
  206. if (this->first_relobj_ == NULL)
  207. {
  208. this->first_relobj_ = relobj;
  209. this->first_shndx_ = shndx;
  210. }
  211. std::pair<Input_sections::iterator, bool> result =
  212. this->input_sections_.insert(Section_id(relobj, shndx));
  213. // We should insert a merge section once only.
  214. gold_assert(result.second);
  215. }
  216. // Class Output_merge_data.
  217. // Compute the hash code for a fixed-size constant.
  218. size_t
  219. Output_merge_data::Merge_data_hash::operator()(Merge_data_key k) const
  220. {
  221. const unsigned char* p = this->pomd_->constant(k);
  222. section_size_type entsize =
  223. convert_to_section_size_type(this->pomd_->entsize());
  224. // Fowler/Noll/Vo (FNV) hash (type FNV-1a).
  225. if (sizeof(size_t) == 8)
  226. {
  227. size_t result = static_cast<size_t>(14695981039346656037ULL);
  228. for (section_size_type i = 0; i < entsize; ++i)
  229. {
  230. result &= (size_t) *p++;
  231. result *= 1099511628211ULL;
  232. }
  233. return result;
  234. }
  235. else
  236. {
  237. size_t result = 2166136261UL;
  238. for (section_size_type i = 0; i < entsize; ++i)
  239. {
  240. result ^= (size_t) *p++;
  241. result *= 16777619UL;
  242. }
  243. return result;
  244. }
  245. }
  246. // Return whether one hash table key equals another.
  247. bool
  248. Output_merge_data::Merge_data_eq::operator()(Merge_data_key k1,
  249. Merge_data_key k2) const
  250. {
  251. const unsigned char* p1 = this->pomd_->constant(k1);
  252. const unsigned char* p2 = this->pomd_->constant(k2);
  253. return memcmp(p1, p2, this->pomd_->entsize()) == 0;
  254. }
  255. // Add a constant to the end of the section contents.
  256. void
  257. Output_merge_data::add_constant(const unsigned char* p)
  258. {
  259. section_size_type entsize = convert_to_section_size_type(this->entsize());
  260. section_size_type addralign =
  261. convert_to_section_size_type(this->addralign());
  262. section_size_type addsize = std::max(entsize, addralign);
  263. if (this->len_ + addsize > this->alc_)
  264. {
  265. if (this->alc_ == 0)
  266. this->alc_ = 128 * addsize;
  267. else
  268. this->alc_ *= 2;
  269. this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->alc_));
  270. if (this->p_ == NULL)
  271. gold_nomem();
  272. }
  273. memcpy(this->p_ + this->len_, p, entsize);
  274. if (addsize > entsize)
  275. memset(this->p_ + this->len_ + entsize, 0, addsize - entsize);
  276. this->len_ += addsize;
  277. }
  278. // Add the input section SHNDX in OBJECT to a merged output section
  279. // which holds fixed length constants. Return whether we were able to
  280. // handle the section; if not, it will be linked as usual without
  281. // constant merging.
  282. bool
  283. Output_merge_data::do_add_input_section(Relobj* object, unsigned int shndx)
  284. {
  285. section_size_type len;
  286. bool is_new;
  287. const unsigned char* p = object->decompressed_section_contents(shndx, &len,
  288. &is_new);
  289. section_size_type entsize = convert_to_section_size_type(this->entsize());
  290. if (len % entsize != 0)
  291. {
  292. if (is_new)
  293. delete[] p;
  294. return false;
  295. }
  296. this->input_count_ += len / entsize;
  297. Object_merge_map* merge_map = object->get_or_create_merge_map();
  298. Object_merge_map::Input_merge_map* input_merge_map =
  299. merge_map->get_or_make_input_merge_map(this, shndx);
  300. for (section_size_type i = 0; i < len; i += entsize, p += entsize)
  301. {
  302. // Add the constant to the section contents. If we find that it
  303. // is already in the hash table, we will remove it again.
  304. Merge_data_key k = this->len_;
  305. this->add_constant(p);
  306. std::pair<Merge_data_hashtable::iterator, bool> ins =
  307. this->hashtable_.insert(k);
  308. if (!ins.second)
  309. {
  310. // Key was already present. Remove the copy we just added.
  311. this->len_ -= entsize;
  312. k = *ins.first;
  313. }
  314. // Record the offset of this constant in the output section.
  315. input_merge_map->add_mapping(i, entsize, k);
  316. }
  317. // For script processing, we keep the input sections.
  318. if (this->keeps_input_sections())
  319. record_input_section(object, shndx);
  320. if (is_new)
  321. delete[] p;
  322. return true;
  323. }
  324. // Set the final data size in a merged output section with fixed size
  325. // constants.
  326. void
  327. Output_merge_data::set_final_data_size()
  328. {
  329. // Release the memory we don't need.
  330. this->p_ = static_cast<unsigned char*>(realloc(this->p_, this->len_));
  331. // An Output_merge_data object may be empty and realloc is allowed
  332. // to return a NULL pointer in this case. An Output_merge_data is empty
  333. // if all its input sections have sizes that are not multiples of entsize.
  334. gold_assert(this->p_ != NULL || this->len_ == 0);
  335. this->set_data_size(this->len_);
  336. }
  337. // Write the data of a merged output section with fixed size constants
  338. // to the file.
  339. void
  340. Output_merge_data::do_write(Output_file* of)
  341. {
  342. of->write(this->offset(), this->p_, this->len_);
  343. }
  344. // Write the data to a buffer.
  345. void
  346. Output_merge_data::do_write_to_buffer(unsigned char* buffer)
  347. {
  348. memcpy(buffer, this->p_, this->len_);
  349. }
  350. // Print merge stats to stderr.
  351. void
  352. Output_merge_data::do_print_merge_stats(const char* section_name)
  353. {
  354. fprintf(stderr,
  355. _("%s: %s merged constants size: %lu; input: %zu; output: %zu\n"),
  356. program_name, section_name,
  357. static_cast<unsigned long>(this->entsize()),
  358. this->input_count_, this->hashtable_.size());
  359. }
  360. // Class Output_merge_string.
  361. // Add an input section to a merged string section.
  362. template<typename Char_type>
  363. bool
  364. Output_merge_string<Char_type>::do_add_input_section(Relobj* object,
  365. unsigned int shndx)
  366. {
  367. section_size_type sec_len;
  368. bool is_new;
  369. uint64_t addralign = this->addralign();
  370. const unsigned char* pdata = object->decompressed_section_contents(shndx,
  371. &sec_len,
  372. &is_new,
  373. &addralign);
  374. const Char_type* p = reinterpret_cast<const Char_type*>(pdata);
  375. const Char_type* pend = p + sec_len / sizeof(Char_type);
  376. const Char_type* pend0 = pend;
  377. if (sec_len % sizeof(Char_type) != 0)
  378. {
  379. object->error(_("mergeable string section length not multiple of "
  380. "character size"));
  381. if (is_new)
  382. delete[] pdata;
  383. return false;
  384. }
  385. if (pend[-1] != 0)
  386. {
  387. gold_warning(_("%s: last entry in mergeable string section '%s' "
  388. "not null terminated"),
  389. object->name().c_str(),
  390. object->section_name(shndx).c_str());
  391. // Find the end of the last NULL-terminated string in the buffer.
  392. while (pend0 > p && pend0[-1] != 0)
  393. --pend0;
  394. }
  395. Merged_strings_list* merged_strings_list =
  396. new Merged_strings_list(object, shndx);
  397. this->merged_strings_lists_.push_back(merged_strings_list);
  398. Merged_strings& merged_strings = merged_strings_list->merged_strings;
  399. // Count the number of non-null strings in the section and size the list.
  400. size_t count = 0;
  401. const Char_type* pt = p;
  402. while (pt < pend0)
  403. {
  404. size_t len = string_length(pt);
  405. if (len != 0)
  406. ++count;
  407. pt += len + 1;
  408. }
  409. if (pend0 < pend)
  410. ++count;
  411. merged_strings.reserve(count + 1);
  412. // The index I is in bytes, not characters.
  413. section_size_type i = 0;
  414. // We assume here that the beginning of the section is correctly
  415. // aligned, so each string within the section must retain the same
  416. // modulo.
  417. uintptr_t init_align_modulo = (reinterpret_cast<uintptr_t>(pdata)
  418. & (addralign - 1));
  419. bool has_misaligned_strings = false;
  420. while (p < pend)
  421. {
  422. size_t len = p < pend0 ? string_length(p) : pend - p;
  423. // Within merge input section each string must be aligned.
  424. if (len != 0
  425. && ((reinterpret_cast<uintptr_t>(p) & (addralign - 1))
  426. != init_align_modulo))
  427. has_misaligned_strings = true;
  428. Stringpool::Key key;
  429. this->stringpool_.add_with_length(p, len, true, &key);
  430. merged_strings.push_back(Merged_string(i, key));
  431. p += len + 1;
  432. i += (len + 1) * sizeof(Char_type);
  433. }
  434. // Record the last offset in the input section so that we can
  435. // compute the length of the last string.
  436. merged_strings.push_back(Merged_string(i, 0));
  437. this->input_count_ += count;
  438. this->input_size_ += i;
  439. if (has_misaligned_strings)
  440. gold_warning(_("%s: section %s contains incorrectly aligned strings;"
  441. " the alignment of those strings won't be preserved"),
  442. object->name().c_str(),
  443. object->section_name(shndx).c_str());
  444. // For script processing, we keep the input sections.
  445. if (this->keeps_input_sections())
  446. record_input_section(object, shndx);
  447. if (is_new)
  448. delete[] pdata;
  449. return true;
  450. }
  451. // Finalize the mappings from the input sections to the output
  452. // section, and return the final data size.
  453. template<typename Char_type>
  454. section_size_type
  455. Output_merge_string<Char_type>::finalize_merged_data()
  456. {
  457. this->stringpool_.set_string_offsets();
  458. for (typename Merged_strings_lists::const_iterator l =
  459. this->merged_strings_lists_.begin();
  460. l != this->merged_strings_lists_.end();
  461. ++l)
  462. {
  463. section_offset_type last_input_offset = 0;
  464. section_offset_type last_output_offset = 0;
  465. Relobj *object = (*l)->object;
  466. Object_merge_map* merge_map = object->get_or_create_merge_map();
  467. Object_merge_map::Input_merge_map* input_merge_map =
  468. merge_map->get_or_make_input_merge_map(this, (*l)->shndx);
  469. for (typename Merged_strings::const_iterator p =
  470. (*l)->merged_strings.begin();
  471. p != (*l)->merged_strings.end();
  472. ++p)
  473. {
  474. section_size_type length = p->offset - last_input_offset;
  475. if (length > 0)
  476. input_merge_map->add_mapping(last_input_offset, length,
  477. last_output_offset);
  478. last_input_offset = p->offset;
  479. if (p->stringpool_key != 0)
  480. last_output_offset =
  481. this->stringpool_.get_offset_from_key(p->stringpool_key);
  482. }
  483. delete *l;
  484. }
  485. // Save some memory. This also ensures that this function will work
  486. // if called twice, as may happen if Layout::set_segment_offsets
  487. // finds a better alignment.
  488. this->merged_strings_lists_.clear();
  489. return this->stringpool_.get_strtab_size();
  490. }
  491. template<typename Char_type>
  492. void
  493. Output_merge_string<Char_type>::set_final_data_size()
  494. {
  495. const off_t final_data_size = this->finalize_merged_data();
  496. this->set_data_size(final_data_size);
  497. }
  498. // Write out a merged string section.
  499. template<typename Char_type>
  500. void
  501. Output_merge_string<Char_type>::do_write(Output_file* of)
  502. {
  503. this->stringpool_.write(of, this->offset());
  504. }
  505. // Write a merged string section to a buffer.
  506. template<typename Char_type>
  507. void
  508. Output_merge_string<Char_type>::do_write_to_buffer(unsigned char* buffer)
  509. {
  510. this->stringpool_.write_to_buffer(buffer, this->data_size());
  511. }
  512. // Return the name of the types of string to use with
  513. // do_print_merge_stats.
  514. template<typename Char_type>
  515. const char*
  516. Output_merge_string<Char_type>::string_name()
  517. {
  518. gold_unreachable();
  519. return NULL;
  520. }
  521. template<>
  522. const char*
  523. Output_merge_string<char>::string_name()
  524. {
  525. return "strings";
  526. }
  527. template<>
  528. const char*
  529. Output_merge_string<uint16_t>::string_name()
  530. {
  531. return "16-bit strings";
  532. }
  533. template<>
  534. const char*
  535. Output_merge_string<uint32_t>::string_name()
  536. {
  537. return "32-bit strings";
  538. }
  539. // Print merge stats to stderr.
  540. template<typename Char_type>
  541. void
  542. Output_merge_string<Char_type>::do_print_merge_stats(const char* section_name)
  543. {
  544. char buf[200];
  545. snprintf(buf, sizeof buf, "%s merged %s", section_name, this->string_name());
  546. fprintf(stderr, _("%s: %s input bytes: %zu\n"),
  547. program_name, buf, this->input_size_);
  548. fprintf(stderr, _("%s: %s input strings: %zu\n"),
  549. program_name, buf, this->input_count_);
  550. this->stringpool_.print_stats(buf);
  551. }
  552. // Instantiate the templates we need.
  553. template
  554. class Output_merge_string<char>;
  555. template
  556. class Output_merge_string<uint16_t>;
  557. template
  558. class Output_merge_string<uint32_t>;
  559. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  560. template
  561. void
  562. Object_merge_map::initialize_input_to_output_map<32>(
  563. unsigned int shndx,
  564. elfcpp::Elf_types<32>::Elf_Addr starting_address,
  565. Unordered_map<section_offset_type, elfcpp::Elf_types<32>::Elf_Addr>*);
  566. #endif
  567. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  568. template
  569. void
  570. Object_merge_map::initialize_input_to_output_map<64>(
  571. unsigned int shndx,
  572. elfcpp::Elf_types<64>::Elf_Addr starting_address,
  573. Unordered_map<section_offset_type, elfcpp::Elf_types<64>::Elf_Addr>*);
  574. #endif
  575. } // End namespace gold.