Commit | Line | Data |
---|---|---|
14bfc3f5 ILT |
1 | // target-select.h -- select a target for an object file -*- C++ -*- |
2 | ||
03ef7571 | 3 | // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
14bfc3f5 ILT |
23 | #ifndef GOLD_TARGET_SELECT_H |
24 | #define GOLD_TARGET_SELECT_H | |
25 | ||
e96caa79 ILT |
26 | #include <vector> |
27 | ||
7f055c20 ILT |
28 | #include "gold-threads.h" |
29 | ||
14bfc3f5 ILT |
30 | namespace gold |
31 | { | |
32 | ||
33 | class Target; | |
114dfbe1 ILT |
34 | class Target_selector; |
35 | ||
36 | // Used to set the target only once. | |
37 | ||
38 | class Set_target_once : public Once | |
39 | { | |
40 | public: | |
41 | Set_target_once(Target_selector* target_selector) | |
42 | : target_selector_(target_selector) | |
43 | { } | |
44 | ||
45 | protected: | |
46 | void | |
47 | do_run_once(void*); | |
48 | ||
49 | private: | |
50 | Target_selector* target_selector_; | |
51 | }; | |
14bfc3f5 ILT |
52 | |
53 | // We want to avoid a master list of targets, which implies using a | |
54 | // global constructor. And we also want the program to start up as | |
55 | // quickly as possible, which implies avoiding global constructors. | |
56 | // We compromise on a very simple global constructor. We use a target | |
57 | // selector, which specifies an ELF machine number and a recognition | |
58 | // function. We use global constructors to build a linked list of | |
59 | // target selectors--a simple pointer list, not a std::list. | |
60 | ||
61 | class Target_selector | |
62 | { | |
63 | public: | |
64 | // Create a target selector for a specific machine number, size (32 | |
65 | // or 64), and endianness. The machine number can be EM_NONE to | |
e96caa79 ILT |
66 | // test for any machine number. BFD_NAME is the name of the target |
67 | // used by the GNU linker, for backward compatibility; it may be | |
03ef7571 ILT |
68 | // NULL. EMULATION is the name of the emulation used by the GNU |
69 | // linker; it is similar to BFD_NAME. | |
e96caa79 | 70 | Target_selector(int machine, int size, bool is_big_endian, |
03ef7571 | 71 | const char* bfd_name, const char* emulation); |
14bfc3f5 ILT |
72 | |
73 | virtual ~Target_selector() | |
74 | { } | |
75 | ||
76 | // If we can handle this target, return a pointer to a target | |
77 | // structure. The size and endianness are known. | |
e96caa79 | 78 | Target* |
2ea97941 ILT |
79 | recognize(int machine, int osabi, int abiversion) |
80 | { return this->do_recognize(machine, osabi, abiversion); } | |
0daa6f62 ILT |
81 | |
82 | // If NAME matches the target, return a pointer to a target | |
83 | // structure. | |
e96caa79 | 84 | Target* |
03ef7571 ILT |
85 | recognize_by_bfd_name(const char* name) |
86 | { return this->do_recognize_by_bfd_name(name); } | |
e96caa79 | 87 | |
03ef7571 ILT |
88 | // Push all supported BFD names onto the vector. This is only used |
89 | // for help output. | |
e96caa79 | 90 | void |
03ef7571 ILT |
91 | supported_bfd_names(std::vector<const char*>* names) |
92 | { this->do_supported_bfd_names(names); } | |
93 | ||
94 | // If NAME matches the target emulation, return a pointer to a | |
95 | // target structure. | |
96 | Target* | |
97 | recognize_by_emulation(const char* name) | |
98 | { return this->do_recognize_by_emulation(name); } | |
99 | ||
100 | // Push all supported emulations onto the vector. This is only used | |
101 | // for help output. | |
102 | void | |
103 | supported_emulations(std::vector<const char*>* names) | |
104 | { this->do_supported_emulations(names); } | |
14bfc3f5 ILT |
105 | |
106 | // Return the next Target_selector in the linked list. | |
107 | Target_selector* | |
108 | next() const | |
109 | { return this->next_; } | |
110 | ||
e96caa79 ILT |
111 | // Return the machine number this selector is looking for. This can |
112 | // be EM_NONE to match any machine number, in which case the | |
113 | // do_recognize hook will be responsible for matching the machine | |
114 | // number. | |
14bfc3f5 ILT |
115 | int |
116 | machine() const | |
117 | { return this->machine_; } | |
118 | ||
119 | // Return the size this is looking for (32 or 64). | |
120 | int | |
6340166c | 121 | get_size() const |
14bfc3f5 ILT |
122 | { return this->size_; } |
123 | ||
124 | // Return the endianness this is looking for. | |
125 | bool | |
6340166c ILT |
126 | is_big_endian() const |
127 | { return this->is_big_endian_; } | |
14bfc3f5 | 128 | |
e96caa79 | 129 | // Return the BFD name. This may return NULL, in which case the |
03ef7571 ILT |
130 | // do_recognize_by_bfd_name hook will be responsible for matching |
131 | // the BFD name. | |
e96caa79 ILT |
132 | const char* |
133 | bfd_name() const | |
134 | { return this->bfd_name_; } | |
135 | ||
03ef7571 ILT |
136 | // Return the emulation. This may return NULL, in which case the |
137 | // do_recognize_by_emulation hook will be responsible for matching | |
138 | // the emulation. | |
139 | const char* | |
140 | emulation() const | |
141 | { return this->emulation_; } | |
142 | ||
f1ddb600 ILT |
143 | // The reverse mapping, for --print-output-format: if we |
144 | // instantiated TARGET, return our BFD_NAME. If we did not | |
145 | // instantiate it, return NULL. | |
146 | const char* | |
147 | target_bfd_name(const Target* target) | |
148 | { return this->do_target_bfd_name(target); } | |
149 | ||
e96caa79 ILT |
150 | protected: |
151 | // Return an instance of the real target. This must be implemented | |
152 | // by the child class. | |
153 | virtual Target* | |
154 | do_instantiate_target() = 0; | |
155 | ||
36959681 ILT |
156 | // Recognize an object file given a machine code, OSABI code, and |
157 | // ELF version value. When this is called we already know that they | |
158 | // match the machine_, size_, and is_big_endian_ fields. The child | |
159 | // class may implement a different version of this to do additional | |
e96caa79 ILT |
160 | // checks, or to check for multiple machine codes if the machine_ |
161 | // field is EM_NONE. | |
162 | virtual Target* | |
163 | do_recognize(int, int, int) | |
164 | { return this->instantiate_target(); } | |
165 | ||
166 | // Recognize a target by name. When this is called we already know | |
167 | // that the name matches (or that the bfd_name_ field is NULL). The | |
168 | // child class may implement a different version of this to | |
169 | // recognize more than one name. | |
170 | virtual Target* | |
03ef7571 | 171 | do_recognize_by_bfd_name(const char*) |
e96caa79 ILT |
172 | { return this->instantiate_target(); } |
173 | ||
174 | // Return a list of supported BFD names. The child class may | |
175 | // implement a different version of this to handle more than one | |
176 | // name. | |
177 | virtual void | |
03ef7571 | 178 | do_supported_bfd_names(std::vector<const char*>* names) |
e96caa79 ILT |
179 | { |
180 | gold_assert(this->bfd_name_ != NULL); | |
181 | names->push_back(this->bfd_name_); | |
182 | } | |
183 | ||
03ef7571 ILT |
184 | // Recognize a target by emulation. When this is called we already |
185 | // know that the name matches (or that the emulation_ field is | |
186 | // NULL). The child class may implement a different version of this | |
187 | // to recognize more than one emulation. | |
188 | virtual Target* | |
189 | do_recognize_by_emulation(const char*) | |
190 | { return this->instantiate_target(); } | |
191 | ||
192 | // Return a list of supported emulations. The child class may | |
193 | // implement a different version of this to handle more than one | |
194 | // emulation. | |
195 | virtual void | |
196 | do_supported_emulations(std::vector<const char*>* emulations) | |
197 | { | |
198 | gold_assert(this->emulation_ != NULL); | |
199 | emulations->push_back(this->emulation_); | |
200 | } | |
201 | ||
f1ddb600 ILT |
202 | // Map from target to BFD name. |
203 | virtual const char* | |
204 | do_target_bfd_name(const Target*); | |
205 | ||
e96caa79 ILT |
206 | // Instantiate the target and return it. |
207 | Target* | |
7f055c20 | 208 | instantiate_target(); |
e96caa79 | 209 | |
f1ddb600 ILT |
210 | // Return whether TARGET is the target we instantiated. |
211 | bool | |
212 | is_our_target(const Target* target) | |
213 | { return target == this->instantiated_target_; } | |
214 | ||
36959681 | 215 | private: |
114dfbe1 ILT |
216 | // Set the target. |
217 | void | |
218 | set_target(); | |
219 | ||
220 | friend class Set_target_once; | |
221 | ||
e96caa79 ILT |
222 | // ELF machine code. |
223 | const int machine_; | |
224 | // Target size--32 or 64. | |
225 | const int size_; | |
226 | // Whether the target is big endian. | |
227 | const bool is_big_endian_; | |
228 | // BFD name of target, for compatibility. | |
229 | const char* const bfd_name_; | |
03ef7571 ILT |
230 | // GNU linker emulation for this target, for compatibility. |
231 | const char* const emulation_; | |
e96caa79 | 232 | // Next entry in list built at global constructor time. |
14bfc3f5 | 233 | Target_selector* next_; |
e96caa79 ILT |
234 | // The singleton Target structure--this points to an instance of the |
235 | // real implementation. | |
236 | Target* instantiated_target_; | |
114dfbe1 ILT |
237 | // Used to set the target only once. |
238 | Set_target_once set_target_once_; | |
14bfc3f5 ILT |
239 | }; |
240 | ||
241 | // Select the target for an ELF file. | |
242 | ||
e96caa79 ILT |
243 | extern Target* |
244 | select_target(int machine, int size, bool big_endian, int osabi, | |
245 | int abiversion); | |
14bfc3f5 | 246 | |
0daa6f62 ILT |
247 | // Select a target using a BFD name. |
248 | ||
e96caa79 | 249 | extern Target* |
03ef7571 ILT |
250 | select_target_by_bfd_name(const char* name); |
251 | ||
252 | // Select a target using a GNU linker emulation. | |
253 | ||
254 | extern Target* | |
255 | select_target_by_emulation(const char* name); | |
e96caa79 ILT |
256 | |
257 | // Fill in a vector with the list of supported targets. This returns | |
258 | // a list of BFD names. | |
259 | ||
260 | extern void | |
261 | supported_target_names(std::vector<const char*>*); | |
0daa6f62 | 262 | |
03ef7571 ILT |
263 | // Fill in a vector with the list of supported emulations. |
264 | ||
265 | extern void | |
266 | supported_emulation_names(std::vector<const char*>*); | |
267 | ||
f1ddb600 ILT |
268 | // Print the output format, for the --print-output-format option. |
269 | ||
270 | extern void | |
271 | print_output_format(); | |
272 | ||
14bfc3f5 ILT |
273 | } // End namespace gold. |
274 | ||
275 | #endif // !defined(GOLD_TARGET_SELECT_H) |