target-select.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // target-select.cc -- select a target for an object file
  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 <cstdio>
  19. #include <cstring>
  20. #include "elfcpp.h"
  21. #include "options.h"
  22. #include "parameters.h"
  23. #include "target-select.h"
  24. namespace
  25. {
  26. // The start of the list of target selectors.
  27. gold::Target_selector* target_selectors;
  28. } // End anonymous namespace.
  29. namespace gold
  30. {
  31. // Class Set_target_once.
  32. void
  33. Set_target_once::do_run_once(void*)
  34. {
  35. this->target_selector_->set_target();
  36. }
  37. // Construct a Target_selector, which means adding it to the linked
  38. // list. This runs at global constructor time, so we want it to be
  39. // fast.
  40. Target_selector::Target_selector(int machine, int size, bool is_big_endian,
  41. const char* bfd_name, const char* emulation)
  42. : machine_(machine), size_(size), is_big_endian_(is_big_endian),
  43. bfd_name_(bfd_name), emulation_(emulation), instantiated_target_(NULL),
  44. set_target_once_(this)
  45. {
  46. this->next_ = target_selectors;
  47. target_selectors = this;
  48. }
  49. // Instantiate the target and return it. Use SET_TARGET_ONCE_ to
  50. // avoid instantiating two instances of the same target.
  51. Target*
  52. Target_selector::instantiate_target()
  53. {
  54. this->set_target_once_.run_once(NULL);
  55. return this->instantiated_target_;
  56. }
  57. // Instantiate the target. This is called at most once.
  58. void
  59. Target_selector::set_target()
  60. {
  61. gold_assert(this->instantiated_target_ == NULL);
  62. this->instantiated_target_ = this->do_instantiate_target();
  63. }
  64. // If we instantiated TARGET, return the corresponding BFD name.
  65. const char*
  66. Target_selector::do_target_bfd_name(const Target* target)
  67. {
  68. if (!this->is_our_target(target))
  69. return NULL;
  70. const char* my_bfd_name = this->bfd_name();
  71. gold_assert(my_bfd_name != NULL);
  72. return my_bfd_name;
  73. }
  74. // Find the target for an ELF file.
  75. Target*
  76. select_target(Input_file* input_file, off_t offset,
  77. int machine, int size, bool is_big_endian,
  78. int osabi, int abiversion)
  79. {
  80. for (Target_selector* p = target_selectors; p != NULL; p = p->next())
  81. {
  82. int pmach = p->machine();
  83. if ((pmach == machine || pmach == elfcpp::EM_NONE)
  84. && p->get_size() == size
  85. && (p->is_big_endian() ? is_big_endian : !is_big_endian))
  86. {
  87. Target* ret = p->recognize(input_file, offset,
  88. machine, osabi, abiversion);
  89. if (ret != NULL)
  90. return ret;
  91. }
  92. }
  93. return NULL;
  94. }
  95. // Find a target using a BFD name. This is used to support the
  96. // --oformat option.
  97. Target*
  98. select_target_by_bfd_name(const char* name)
  99. {
  100. for (Target_selector* p = target_selectors; p != NULL; p = p->next())
  101. {
  102. const char* pname = p->bfd_name();
  103. if (pname == NULL || strcmp(pname, name) == 0)
  104. {
  105. Target* ret = p->recognize_by_bfd_name(name);
  106. if (ret != NULL)
  107. return ret;
  108. }
  109. }
  110. return NULL;
  111. }
  112. // Find a target using a GNU linker emulation. This is used to
  113. // support the -m option.
  114. Target*
  115. select_target_by_emulation(const char* name)
  116. {
  117. for (Target_selector* p = target_selectors; p != NULL; p = p->next())
  118. {
  119. const char* pname = p->emulation();
  120. if (pname == NULL || strcmp(pname, name) == 0)
  121. {
  122. Target* ret = p->recognize_by_emulation(name);
  123. if (ret != NULL)
  124. return ret;
  125. }
  126. }
  127. return NULL;
  128. }
  129. // Push all the supported BFD names onto a vector.
  130. void
  131. supported_target_names(std::vector<const char*>* names)
  132. {
  133. for (Target_selector* p = target_selectors; p != NULL; p = p->next())
  134. p->supported_bfd_names(names);
  135. }
  136. // Push all the supported emulations onto a vector.
  137. void
  138. supported_emulation_names(std::vector<const char*>* names)
  139. {
  140. for (Target_selector* p = target_selectors; p != NULL; p = p->next())
  141. p->supported_emulations(names);
  142. }
  143. // Implement the --print-output-format option.
  144. void
  145. print_output_format()
  146. {
  147. if (!parameters->target_valid())
  148. {
  149. // This case arises when --print-output-format is used with no
  150. // input files. We need to come up with the right string to
  151. // print based on the other options. If the user specified the
  152. // format using a --oformat option, use that. That saves each
  153. // target from having to remember the name that was used to
  154. // select it. In other cases, we will just have to ask the
  155. // target.
  156. if (parameters->options().user_set_oformat())
  157. {
  158. const char* bfd_name = parameters->options().oformat();
  159. Target* target = select_target_by_bfd_name(bfd_name);
  160. if (target != NULL)
  161. printf("%s\n", bfd_name);
  162. else
  163. gold_error(_("unrecognized output format %s"), bfd_name);
  164. return;
  165. }
  166. parameters_force_valid_target();
  167. }
  168. const Target* target = &parameters->target();
  169. for (Target_selector* p = target_selectors; p != NULL; p = p->next())
  170. {
  171. const char* bfd_name = p->target_bfd_name(target);
  172. if (bfd_name != NULL)
  173. {
  174. printf("%s\n", bfd_name);
  175. return;
  176. }
  177. }
  178. gold_unreachable();
  179. }
  180. } // End namespace gold.