common.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // common.cc -- handle common symbols 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 <algorithm>
  19. #include "workqueue.h"
  20. #include "mapfile.h"
  21. #include "layout.h"
  22. #include "output.h"
  23. #include "symtab.h"
  24. #include "common.h"
  25. namespace gold
  26. {
  27. // Allocate_commons_task methods.
  28. // This task allocates the common symbols. We arrange to run it
  29. // before anything else which needs to access the symbol table.
  30. Task_token*
  31. Allocate_commons_task::is_runnable()
  32. {
  33. return NULL;
  34. }
  35. // Release a blocker.
  36. void
  37. Allocate_commons_task::locks(Task_locker* tl)
  38. {
  39. tl->add(this, this->blocker_);
  40. }
  41. // Allocate the common symbols.
  42. void
  43. Allocate_commons_task::run(Workqueue*)
  44. {
  45. this->symtab_->allocate_commons(this->layout_, this->mapfile_);
  46. }
  47. // This class is used to sort the common symbol. We normally put the
  48. // larger common symbols first. This can be changed by using
  49. // --sort-commons, which tells the linker to sort by alignment.
  50. template<int size>
  51. class Sort_commons
  52. {
  53. public:
  54. Sort_commons(const Symbol_table* symtab,
  55. Symbol_table::Sort_commons_order sort_order)
  56. : symtab_(symtab), sort_order_(sort_order)
  57. { }
  58. bool operator()(const Symbol* a, const Symbol* b) const;
  59. private:
  60. // The symbol table.
  61. const Symbol_table* symtab_;
  62. // How to sort.
  63. Symbol_table::Sort_commons_order sort_order_;
  64. };
  65. template<int size>
  66. bool
  67. Sort_commons<size>::operator()(const Symbol* pa, const Symbol* pb) const
  68. {
  69. if (pa == NULL)
  70. return false;
  71. if (pb == NULL)
  72. return true;
  73. const Symbol_table* symtab = this->symtab_;
  74. const Sized_symbol<size>* psa = symtab->get_sized_symbol<size>(pa);
  75. const Sized_symbol<size>* psb = symtab->get_sized_symbol<size>(pb);
  76. // The size.
  77. typename Sized_symbol<size>::Size_type sa = psa->symsize();
  78. typename Sized_symbol<size>::Size_type sb = psb->symsize();
  79. // The alignment.
  80. typename Sized_symbol<size>::Value_type aa = psa->value();
  81. typename Sized_symbol<size>::Value_type ab = psb->value();
  82. if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_DESCENDING)
  83. {
  84. if (aa < ab)
  85. return false;
  86. else if (ab < aa)
  87. return true;
  88. }
  89. else if (this->sort_order_
  90. == Symbol_table::SORT_COMMONS_BY_ALIGNMENT_ASCENDING)
  91. {
  92. if (aa < ab)
  93. return true;
  94. else if (ab < aa)
  95. return false;
  96. }
  97. else
  98. gold_assert(this->sort_order_
  99. == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING);
  100. // Sort by descending size.
  101. if (sa < sb)
  102. return false;
  103. else if (sb < sa)
  104. return true;
  105. if (this->sort_order_ == Symbol_table::SORT_COMMONS_BY_SIZE_DESCENDING)
  106. {
  107. // When the symbols are the same size, we sort them by
  108. // alignment, largest alignment first.
  109. if (aa < ab)
  110. return false;
  111. else if (ab < aa)
  112. return true;
  113. }
  114. // Otherwise we stabilize the sort by sorting by name.
  115. return strcmp(psa->name(), psb->name()) < 0;
  116. }
  117. // Allocate the common symbols.
  118. void
  119. Symbol_table::allocate_commons(Layout* layout, Mapfile* mapfile)
  120. {
  121. Sort_commons_order sort_order;
  122. if (!parameters->options().user_set_sort_common())
  123. sort_order = SORT_COMMONS_BY_SIZE_DESCENDING;
  124. else
  125. {
  126. const char* order = parameters->options().sort_common();
  127. if (*order == '\0' || strcmp(order, "descending") == 0)
  128. sort_order = SORT_COMMONS_BY_ALIGNMENT_DESCENDING;
  129. else if (strcmp(order, "ascending") == 0)
  130. sort_order = SORT_COMMONS_BY_ALIGNMENT_ASCENDING;
  131. else
  132. {
  133. gold_error("invalid --sort-common argument: %s", order);
  134. sort_order = SORT_COMMONS_BY_SIZE_DESCENDING;
  135. }
  136. }
  137. if (parameters->target().get_size() == 32)
  138. {
  139. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  140. this->do_allocate_commons<32>(layout, mapfile, sort_order);
  141. #else
  142. gold_unreachable();
  143. #endif
  144. }
  145. else if (parameters->target().get_size() == 64)
  146. {
  147. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  148. this->do_allocate_commons<64>(layout, mapfile, sort_order);
  149. #else
  150. gold_unreachable();
  151. #endif
  152. }
  153. else
  154. gold_unreachable();
  155. }
  156. // Allocated the common symbols, sized version.
  157. template<int size>
  158. void
  159. Symbol_table::do_allocate_commons(Layout* layout, Mapfile* mapfile,
  160. Sort_commons_order sort_order)
  161. {
  162. if (!this->commons_.empty())
  163. this->do_allocate_commons_list<size>(layout, COMMONS_NORMAL,
  164. &this->commons_, mapfile,
  165. sort_order);
  166. if (!this->tls_commons_.empty())
  167. this->do_allocate_commons_list<size>(layout, COMMONS_TLS,
  168. &this->tls_commons_, mapfile,
  169. sort_order);
  170. if (!this->small_commons_.empty())
  171. this->do_allocate_commons_list<size>(layout, COMMONS_SMALL,
  172. &this->small_commons_, mapfile,
  173. sort_order);
  174. if (!this->large_commons_.empty())
  175. this->do_allocate_commons_list<size>(layout, COMMONS_LARGE,
  176. &this->large_commons_, mapfile,
  177. sort_order);
  178. }
  179. // Allocate the common symbols in a list. IS_TLS indicates whether
  180. // these are TLS common symbols.
  181. template<int size>
  182. void
  183. Symbol_table::do_allocate_commons_list(
  184. Layout* layout,
  185. Commons_section_type commons_section_type,
  186. Commons_type* commons,
  187. Mapfile* mapfile,
  188. Sort_commons_order sort_order)
  189. {
  190. // We've kept a list of all the common symbols. But the symbol may
  191. // have been resolved to a defined symbol by now. And it may be a
  192. // forwarder. First remove all non-common symbols.
  193. bool any = false;
  194. uint64_t addralign = 0;
  195. for (Commons_type::iterator p = commons->begin();
  196. p != commons->end();
  197. ++p)
  198. {
  199. Symbol* sym = *p;
  200. if (sym->is_forwarder())
  201. {
  202. sym = this->resolve_forwards(sym);
  203. *p = sym;
  204. }
  205. if (!sym->is_common())
  206. *p = NULL;
  207. else
  208. {
  209. any = true;
  210. Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
  211. if (ssym->value() > addralign)
  212. addralign = ssym->value();
  213. }
  214. }
  215. if (!any)
  216. return;
  217. // Sort the common symbols.
  218. std::sort(commons->begin(), commons->end(),
  219. Sort_commons<size>(this, sort_order));
  220. // Place them in a newly allocated BSS section.
  221. elfcpp::Elf_Xword flags = elfcpp::SHF_WRITE | elfcpp::SHF_ALLOC;
  222. const char* name;
  223. const char* ds_name;
  224. switch (commons_section_type)
  225. {
  226. case COMMONS_NORMAL:
  227. name = ".bss";
  228. ds_name = "** common";
  229. break;
  230. case COMMONS_TLS:
  231. flags |= elfcpp::SHF_TLS;
  232. name = ".tbss";
  233. ds_name = "** tls common";
  234. break;
  235. case COMMONS_SMALL:
  236. flags |= parameters->target().small_common_section_flags();
  237. name = ".sbss";
  238. ds_name = "** small common";
  239. break;
  240. case COMMONS_LARGE:
  241. flags |= parameters->target().large_common_section_flags();
  242. name = ".lbss";
  243. ds_name = "** large common";
  244. break;
  245. default:
  246. gold_unreachable();
  247. }
  248. Output_data_space* poc;
  249. Output_section* os;
  250. if (!parameters->incremental_update())
  251. {
  252. poc = new Output_data_space(addralign, ds_name);
  253. os = layout->add_output_section_data(name, elfcpp::SHT_NOBITS, flags,
  254. poc, ORDER_INVALID, false);
  255. }
  256. else
  257. {
  258. // When doing an incremental update, we need to allocate each common
  259. // directly from the output section's free list.
  260. poc = NULL;
  261. os = layout->find_output_section(name);
  262. }
  263. if (os != NULL)
  264. {
  265. if (commons_section_type == COMMONS_SMALL)
  266. os->set_is_small_section();
  267. else if (commons_section_type == COMMONS_LARGE)
  268. os->set_is_large_section();
  269. }
  270. // Allocate them all.
  271. off_t off = 0;
  272. for (Commons_type::iterator p = commons->begin();
  273. p != commons->end();
  274. ++p)
  275. {
  276. Symbol* sym = *p;
  277. if (sym == NULL)
  278. break;
  279. // Because we followed forwarding symbols above, but we didn't
  280. // do it reliably before adding symbols to the list, it is
  281. // possible for us to have the same symbol on the list twice.
  282. // This can happen in the horrible case where a program defines
  283. // a common symbol with the same name as a versioned libc
  284. // symbol. That will show up here as a symbol which has already
  285. // been allocated and is therefore no longer a common symbol.
  286. if (!sym->is_common())
  287. continue;
  288. Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
  289. // Record the symbol in the map file now, before we change its
  290. // value. Pass the size in separately so that we don't have to
  291. // templatize the map code, which is not performance sensitive.
  292. if (mapfile != NULL)
  293. mapfile->report_allocate_common(sym, ssym->symsize());
  294. if (poc != NULL)
  295. {
  296. off = align_address(off, ssym->value());
  297. ssym->allocate_common(poc, off);
  298. off += ssym->symsize();
  299. }
  300. else
  301. {
  302. // For an incremental update, allocate from the free list.
  303. off = os->allocate(ssym->symsize(), ssym->value());
  304. if (off == -1)
  305. gold_fallback(_("out of patch space in section %s; "
  306. "relink with --incremental-full"),
  307. os->name());
  308. ssym->allocate_common(os, off);
  309. }
  310. }
  311. if (poc != NULL)
  312. poc->set_current_data_size(off);
  313. commons->clear();
  314. }
  315. } // End namespace gold.