parameters.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // parameters.cc -- general parameters for a link using 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 "debug.h"
  19. #include "options.h"
  20. #include "target.h"
  21. #include "target-select.h"
  22. namespace gold
  23. {
  24. // Our local version of the variable, which is not const.
  25. static Parameters static_parameters;
  26. // The global variable.
  27. const Parameters* parameters = &static_parameters;
  28. // A helper class to set the target once.
  29. class Set_parameters_target_once : public Once
  30. {
  31. public:
  32. Set_parameters_target_once(Parameters* parameters)
  33. : parameters_(parameters)
  34. { }
  35. protected:
  36. void
  37. do_run_once(void* arg)
  38. { this->parameters_->set_target_once(static_cast<Target*>(arg)); }
  39. private:
  40. Parameters* parameters_;
  41. };
  42. // We only need one Set_parameters_target_once.
  43. static
  44. Set_parameters_target_once set_parameters_target_once(&static_parameters);
  45. // Class Parameters.
  46. Parameters::Parameters()
  47. : errors_(NULL), timer_(NULL), options_(NULL), target_(NULL),
  48. doing_static_link_valid_(false), doing_static_link_(false),
  49. debug_(0), incremental_mode_(General_options::INCREMENTAL_OFF),
  50. set_parameters_target_once_(&set_parameters_target_once)
  51. {
  52. }
  53. void
  54. Parameters::set_errors(Errors* errors)
  55. {
  56. gold_assert(this->errors_ == NULL);
  57. this->errors_ = errors;
  58. }
  59. void
  60. Parameters::set_timer(Timer* timer)
  61. {
  62. gold_assert(this->timer_ == NULL);
  63. this->timer_ = timer;
  64. }
  65. void
  66. Parameters::set_options(const General_options* options)
  67. {
  68. gold_assert(!this->options_valid());
  69. this->options_ = options;
  70. // For speed, we convert the options() debug var from a string to an
  71. // enum (from debug.h).
  72. this->debug_ = debug_string_to_enum(this->options().debug());
  73. // Set incremental_mode_ based on the value of the --incremental option.
  74. // We copy the mode into parameters because it can change based on inputs.
  75. this->incremental_mode_ = this->options().incremental_mode();
  76. // If --verbose is set, it acts as "--debug=files".
  77. if (options->verbose())
  78. this->debug_ |= DEBUG_FILES;
  79. if (this->target_valid())
  80. this->check_target_endianness();
  81. }
  82. void
  83. Parameters::set_doing_static_link(bool doing_static_link)
  84. {
  85. gold_assert(!this->doing_static_link_valid_);
  86. this->doing_static_link_ = doing_static_link;
  87. this->doing_static_link_valid_ = true;
  88. }
  89. void
  90. Parameters::set_target(Target* target)
  91. {
  92. this->set_parameters_target_once_->run_once(static_cast<void*>(target));
  93. gold_assert(target == this->target_);
  94. }
  95. // This is called at most once.
  96. void
  97. Parameters::set_target_once(Target* target)
  98. {
  99. gold_assert(this->target_ == NULL);
  100. this->target_ = target;
  101. target->select_as_default_target();
  102. if (this->options_valid())
  103. {
  104. this->check_target_endianness();
  105. this->check_rodata_segment();
  106. }
  107. }
  108. // Clear the target, for testing.
  109. void
  110. Parameters::clear_target()
  111. {
  112. this->target_ = NULL;
  113. // We need a new Set_parameters_target_once so that we can set the
  114. // target again.
  115. this->set_parameters_target_once_ = new Set_parameters_target_once(this);
  116. }
  117. // Return whether TARGET is compatible with the target we are using.
  118. bool
  119. Parameters::is_compatible_target(const Target* target) const
  120. {
  121. if (this->target_ == NULL)
  122. return true;
  123. return target == this->target_;
  124. }
  125. Parameters::Target_size_endianness
  126. Parameters::size_and_endianness() const
  127. {
  128. if (this->target().get_size() == 32)
  129. {
  130. if (!this->target().is_big_endian())
  131. {
  132. #ifdef HAVE_TARGET_32_LITTLE
  133. return TARGET_32_LITTLE;
  134. #else
  135. gold_unreachable();
  136. #endif
  137. }
  138. else
  139. {
  140. #ifdef HAVE_TARGET_32_BIG
  141. return TARGET_32_BIG;
  142. #else
  143. gold_unreachable();
  144. #endif
  145. }
  146. }
  147. else if (parameters->target().get_size() == 64)
  148. {
  149. if (!parameters->target().is_big_endian())
  150. {
  151. #ifdef HAVE_TARGET_64_LITTLE
  152. return TARGET_64_LITTLE;
  153. #else
  154. gold_unreachable();
  155. #endif
  156. }
  157. else
  158. {
  159. #ifdef HAVE_TARGET_64_BIG
  160. return TARGET_64_BIG;
  161. #else
  162. gold_unreachable();
  163. #endif
  164. }
  165. }
  166. else
  167. gold_unreachable();
  168. }
  169. // If output endianness is specified in command line, check that it does
  170. // not conflict with the target.
  171. void
  172. Parameters::check_target_endianness()
  173. {
  174. General_options::Endianness endianness = this->options().endianness();
  175. if (endianness != General_options::ENDIANNESS_NOT_SET)
  176. {
  177. bool big_endian;
  178. if (endianness == General_options::ENDIANNESS_BIG)
  179. big_endian = true;
  180. else
  181. {
  182. gold_assert(endianness == General_options::ENDIANNESS_LITTLE);
  183. big_endian = false;;
  184. }
  185. if (this->target().is_big_endian() != big_endian)
  186. gold_error(_("input file does not match -EB/EL option"));
  187. }
  188. }
  189. void
  190. Parameters::check_rodata_segment()
  191. {
  192. if (this->options().user_set_Trodata_segment()
  193. && !this->options().rosegment()
  194. && !this->target().isolate_execinstr())
  195. gold_error(_("-Trodata-segment is meaningless without --rosegment"));
  196. }
  197. // Return the name of the entry symbol.
  198. const char*
  199. Parameters::entry() const
  200. {
  201. const char* ret = this->options().entry();
  202. if (ret == NULL && parameters->target_valid())
  203. ret = parameters->target().entry_symbol_name();
  204. return ret;
  205. }
  206. // Set the incremental linking mode to INCREMENTAL_FULL. Used when
  207. // the linker determines that an incremental update is not possible.
  208. // Returns false if the incremental mode was INCREMENTAL_UPDATE,
  209. // indicating that the linker should exit if an update is not possible.
  210. bool
  211. Parameters::set_incremental_full()
  212. {
  213. gold_assert(this->incremental_mode_ != General_options::INCREMENTAL_OFF);
  214. if (this->incremental_mode_ == General_options::INCREMENTAL_UPDATE)
  215. return false;
  216. this->incremental_mode_ = General_options::INCREMENTAL_FULL;
  217. return true;
  218. }
  219. // Return true if we need to prepare incremental linking information.
  220. bool
  221. Parameters::incremental() const
  222. {
  223. return this->incremental_mode_ != General_options::INCREMENTAL_OFF;
  224. }
  225. // Return true if we are doing a full incremental link.
  226. bool
  227. Parameters::incremental_full() const
  228. {
  229. return this->incremental_mode_ == General_options::INCREMENTAL_FULL;
  230. }
  231. // Return true if we are doing an incremental update.
  232. bool
  233. Parameters::incremental_update() const
  234. {
  235. return (this->incremental_mode_ == General_options::INCREMENTAL_UPDATE
  236. || this->incremental_mode_ == General_options::INCREMENTAL_AUTO);
  237. }
  238. void
  239. set_parameters_errors(Errors* errors)
  240. { static_parameters.set_errors(errors); }
  241. void
  242. set_parameters_timer(Timer* timer)
  243. { static_parameters.set_timer(timer); }
  244. void
  245. set_parameters_options(const General_options* options)
  246. { static_parameters.set_options(options); }
  247. void
  248. set_parameters_target(Target* target)
  249. {
  250. static_parameters.set_target(target);
  251. }
  252. void
  253. set_parameters_doing_static_link(bool doing_static_link)
  254. { static_parameters.set_doing_static_link(doing_static_link); }
  255. // Set the incremental linking mode to INCREMENTAL_FULL. Used when
  256. // the linker determines that an incremental update is not possible.
  257. // Returns false if the incremental mode was INCREMENTAL_UPDATE,
  258. // indicating that the linker should exit if an update is not possible.
  259. bool
  260. set_parameters_incremental_full()
  261. { return static_parameters.set_incremental_full(); }
  262. // Force the target to be valid by using the default. Use the
  263. // --oformat option is set; this supports the x86_64 kernel build,
  264. // which converts a binary file to an object file using -r --format
  265. // binary --oformat elf32-i386 foo.o. Otherwise use the configured
  266. // default.
  267. void
  268. parameters_force_valid_target()
  269. {
  270. if (parameters->target_valid())
  271. return;
  272. gold_assert(parameters->options_valid());
  273. if (parameters->options().user_set_oformat())
  274. {
  275. const char* bfd_name = parameters->options().oformat();
  276. Target* target = select_target_by_bfd_name(bfd_name);
  277. if (target != NULL)
  278. {
  279. set_parameters_target(target);
  280. return;
  281. }
  282. gold_error(_("unrecognized output format %s"), bfd_name);
  283. }
  284. if (parameters->options().user_set_m())
  285. {
  286. const char* emulation = parameters->options().m();
  287. Target* target = select_target_by_emulation(emulation);
  288. if (target != NULL)
  289. {
  290. set_parameters_target(target);
  291. return;
  292. }
  293. gold_error(_("unrecognized emulation %s"), emulation);
  294. }
  295. // The GOLD_DEFAULT_xx macros are defined by the configure script.
  296. bool is_big_endian;
  297. General_options::Endianness endianness = parameters->options().endianness();
  298. if (endianness == General_options::ENDIANNESS_BIG)
  299. is_big_endian = true;
  300. else if (endianness == General_options::ENDIANNESS_LITTLE)
  301. is_big_endian = false;
  302. else
  303. is_big_endian = GOLD_DEFAULT_BIG_ENDIAN;
  304. Target* target = select_target(NULL, 0,
  305. elfcpp::GOLD_DEFAULT_MACHINE,
  306. GOLD_DEFAULT_SIZE,
  307. is_big_endian,
  308. elfcpp::GOLD_DEFAULT_OSABI,
  309. 0);
  310. if (target == NULL)
  311. {
  312. gold_assert(is_big_endian != GOLD_DEFAULT_BIG_ENDIAN);
  313. gold_fatal(_("no supported target for -EB/-EL option"));
  314. }
  315. set_parameters_target(target);
  316. }
  317. // Clear the current target, for testing.
  318. void
  319. parameters_clear_target()
  320. {
  321. static_parameters.clear_target();
  322. }
  323. } // End namespace gold.