target-select.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // target-select.h -- select a target for an object file -*- C++ -*-
  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. #ifndef GOLD_TARGET_SELECT_H
  18. #define GOLD_TARGET_SELECT_H
  19. #include <vector>
  20. #include "gold-threads.h"
  21. namespace gold
  22. {
  23. class Input_file;
  24. class Target;
  25. class Target_selector;
  26. // Used to set the target only once.
  27. class Set_target_once : public Once
  28. {
  29. public:
  30. Set_target_once(Target_selector* target_selector)
  31. : target_selector_(target_selector)
  32. { }
  33. protected:
  34. void
  35. do_run_once(void*);
  36. private:
  37. Target_selector* target_selector_;
  38. };
  39. // We want to avoid a master list of targets, which implies using a
  40. // global constructor. And we also want the program to start up as
  41. // quickly as possible, which implies avoiding global constructors.
  42. // We compromise on a very simple global constructor. We use a target
  43. // selector, which specifies an ELF machine number and a recognition
  44. // function. We use global constructors to build a linked list of
  45. // target selectors--a simple pointer list, not a std::list.
  46. class Target_selector
  47. {
  48. public:
  49. // Create a target selector for a specific machine number, size (32
  50. // or 64), and endianness. The machine number can be EM_NONE to
  51. // test for any machine number. BFD_NAME is the name of the target
  52. // used by the GNU linker, for backward compatibility; it may be
  53. // NULL. EMULATION is the name of the emulation used by the GNU
  54. // linker; it is similar to BFD_NAME.
  55. Target_selector(int machine, int size, bool is_big_endian,
  56. const char* bfd_name, const char* emulation);
  57. virtual ~Target_selector()
  58. { }
  59. // If we can handle this target, return a pointer to a target
  60. // structure. The size and endianness are known.
  61. Target*
  62. recognize(Input_file* input_file, off_t offset,
  63. int machine, int osabi, int abiversion)
  64. { return this->do_recognize(input_file, offset, machine, osabi, abiversion); }
  65. // If NAME matches the target, return a pointer to a target
  66. // structure.
  67. Target*
  68. recognize_by_bfd_name(const char* name)
  69. { return this->do_recognize_by_bfd_name(name); }
  70. // Push all supported BFD names onto the vector. This is only used
  71. // for help output.
  72. void
  73. supported_bfd_names(std::vector<const char*>* names)
  74. { this->do_supported_bfd_names(names); }
  75. // If NAME matches the target emulation, return a pointer to a
  76. // target structure.
  77. Target*
  78. recognize_by_emulation(const char* name)
  79. { return this->do_recognize_by_emulation(name); }
  80. // Push all supported emulations onto the vector. This is only used
  81. // for help output.
  82. void
  83. supported_emulations(std::vector<const char*>* names)
  84. { this->do_supported_emulations(names); }
  85. // Return the next Target_selector in the linked list.
  86. Target_selector*
  87. next() const
  88. { return this->next_; }
  89. // Return the machine number this selector is looking for. This can
  90. // be EM_NONE to match any machine number, in which case the
  91. // do_recognize hook will be responsible for matching the machine
  92. // number.
  93. int
  94. machine() const
  95. { return this->machine_; }
  96. // Return the size this is looking for (32 or 64).
  97. int
  98. get_size() const
  99. { return this->size_; }
  100. // Return the endianness this is looking for.
  101. bool
  102. is_big_endian() const
  103. { return this->is_big_endian_; }
  104. // Return the BFD name. This may return NULL, in which case the
  105. // do_recognize_by_bfd_name hook will be responsible for matching
  106. // the BFD name.
  107. const char*
  108. bfd_name() const
  109. { return this->bfd_name_; }
  110. // Return the emulation. This may return NULL, in which case the
  111. // do_recognize_by_emulation hook will be responsible for matching
  112. // the emulation.
  113. const char*
  114. emulation() const
  115. { return this->emulation_; }
  116. // The reverse mapping, for --print-output-format: if we
  117. // instantiated TARGET, return our BFD_NAME. If we did not
  118. // instantiate it, return NULL.
  119. const char*
  120. target_bfd_name(const Target* target)
  121. { return this->do_target_bfd_name(target); }
  122. protected:
  123. // Return an instance of the real target. This must be implemented
  124. // by the child class.
  125. virtual Target*
  126. do_instantiate_target() = 0;
  127. // Recognize an object file given a machine code, OSABI code, and
  128. // ELF version value. When this is called we already know that they
  129. // match the machine_, size_, and is_big_endian_ fields. The child
  130. // class may implement a different version of this to do additional
  131. // checks, or to check for multiple machine codes if the machine_
  132. // field is EM_NONE.
  133. virtual Target*
  134. do_recognize(Input_file*, off_t, int, int, int)
  135. { return this->instantiate_target(); }
  136. // Recognize a target by name. When this is called we already know
  137. // that the name matches (or that the bfd_name_ field is NULL). The
  138. // child class may implement a different version of this to
  139. // recognize more than one name.
  140. virtual Target*
  141. do_recognize_by_bfd_name(const char*)
  142. { return this->instantiate_target(); }
  143. // Return a list of supported BFD names. The child class may
  144. // implement a different version of this to handle more than one
  145. // name.
  146. virtual void
  147. do_supported_bfd_names(std::vector<const char*>* names)
  148. {
  149. gold_assert(this->bfd_name_ != NULL);
  150. names->push_back(this->bfd_name_);
  151. }
  152. // Recognize a target by emulation. When this is called we already
  153. // know that the name matches (or that the emulation_ field is
  154. // NULL). The child class may implement a different version of this
  155. // to recognize more than one emulation.
  156. virtual Target*
  157. do_recognize_by_emulation(const char*)
  158. { return this->instantiate_target(); }
  159. // Return a list of supported emulations. The child class may
  160. // implement a different version of this to handle more than one
  161. // emulation.
  162. virtual void
  163. do_supported_emulations(std::vector<const char*>* emulations)
  164. {
  165. gold_assert(this->emulation_ != NULL);
  166. emulations->push_back(this->emulation_);
  167. }
  168. // Map from target to BFD name.
  169. virtual const char*
  170. do_target_bfd_name(const Target*);
  171. // Instantiate the target and return it.
  172. Target*
  173. instantiate_target();
  174. // Return whether TARGET is the target we instantiated.
  175. bool
  176. is_our_target(const Target* target)
  177. { return target == this->instantiated_target_; }
  178. private:
  179. // Set the target.
  180. void
  181. set_target();
  182. friend class Set_target_once;
  183. // ELF machine code.
  184. const int machine_;
  185. // Target size--32 or 64.
  186. const int size_;
  187. // Whether the target is big endian.
  188. const bool is_big_endian_;
  189. // BFD name of target, for compatibility.
  190. const char* const bfd_name_;
  191. // GNU linker emulation for this target, for compatibility.
  192. const char* const emulation_;
  193. // Next entry in list built at global constructor time.
  194. Target_selector* next_;
  195. // The singleton Target structure--this points to an instance of the
  196. // real implementation.
  197. Target* instantiated_target_;
  198. // Used to set the target only once.
  199. Set_target_once set_target_once_;
  200. };
  201. // Select the target for an ELF file.
  202. extern Target*
  203. select_target(Input_file*, off_t,
  204. int machine, int size, bool big_endian, int osabi,
  205. int abiversion);
  206. // Select a target using a BFD name.
  207. extern Target*
  208. select_target_by_bfd_name(const char* name);
  209. // Select a target using a GNU linker emulation.
  210. extern Target*
  211. select_target_by_emulation(const char* name);
  212. // Fill in a vector with the list of supported targets. This returns
  213. // a list of BFD names.
  214. extern void
  215. supported_target_names(std::vector<const char*>*);
  216. // Fill in a vector with the list of supported emulations.
  217. extern void
  218. supported_emulation_names(std::vector<const char*>*);
  219. // Print the output format, for the --print-output-format option.
  220. extern void
  221. print_output_format();
  222. } // End namespace gold.
  223. #endif // !defined(GOLD_TARGET_SELECT_H)