Commit | Line | Data |
---|---|---|
14bfc3f5 ILT |
1 | // target-select.h -- select a target for an object file -*- C++ -*- |
2 | ||
ebdbb458 | 3 | // Copyright 2006, 2007, 2008 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; | |
34 | ||
35 | // We want to avoid a master list of targets, which implies using a | |
36 | // global constructor. And we also want the program to start up as | |
37 | // quickly as possible, which implies avoiding global constructors. | |
38 | // We compromise on a very simple global constructor. We use a target | |
39 | // selector, which specifies an ELF machine number and a recognition | |
40 | // function. We use global constructors to build a linked list of | |
41 | // target selectors--a simple pointer list, not a std::list. | |
42 | ||
43 | class Target_selector | |
44 | { | |
45 | public: | |
46 | // Create a target selector for a specific machine number, size (32 | |
47 | // or 64), and endianness. The machine number can be EM_NONE to | |
e96caa79 ILT |
48 | // test for any machine number. BFD_NAME is the name of the target |
49 | // used by the GNU linker, for backward compatibility; it may be | |
50 | // NULL. | |
51 | Target_selector(int machine, int size, bool is_big_endian, | |
52 | const char* bfd_name); | |
14bfc3f5 ILT |
53 | |
54 | virtual ~Target_selector() | |
55 | { } | |
56 | ||
57 | // If we can handle this target, return a pointer to a target | |
58 | // structure. The size and endianness are known. | |
e96caa79 ILT |
59 | Target* |
60 | recognize(int machine, int osabi, int abiversion) | |
61 | { return this->do_recognize(machine, osabi, abiversion); } | |
0daa6f62 ILT |
62 | |
63 | // If NAME matches the target, return a pointer to a target | |
64 | // structure. | |
e96caa79 ILT |
65 | Target* |
66 | recognize_by_name(const char* name) | |
67 | { return this->do_recognize_by_name(name); } | |
68 | ||
69 | // Push all supported names onto the vector. This is only used for | |
70 | // help output. | |
71 | void | |
72 | supported_names(std::vector<const char*>* names) | |
73 | { this->do_supported_names(names); } | |
14bfc3f5 ILT |
74 | |
75 | // Return the next Target_selector in the linked list. | |
76 | Target_selector* | |
77 | next() const | |
78 | { return this->next_; } | |
79 | ||
e96caa79 ILT |
80 | // Return the machine number this selector is looking for. This can |
81 | // be EM_NONE to match any machine number, in which case the | |
82 | // do_recognize hook will be responsible for matching the machine | |
83 | // number. | |
14bfc3f5 ILT |
84 | int |
85 | machine() const | |
86 | { return this->machine_; } | |
87 | ||
88 | // Return the size this is looking for (32 or 64). | |
89 | int | |
6340166c | 90 | get_size() const |
14bfc3f5 ILT |
91 | { return this->size_; } |
92 | ||
93 | // Return the endianness this is looking for. | |
94 | bool | |
6340166c ILT |
95 | is_big_endian() const |
96 | { return this->is_big_endian_; } | |
14bfc3f5 | 97 | |
e96caa79 ILT |
98 | // Return the BFD name. This may return NULL, in which case the |
99 | // do_recognize_by_name hook will be responsible for matching the | |
100 | // BFD name. | |
101 | const char* | |
102 | bfd_name() const | |
103 | { return this->bfd_name_; } | |
104 | ||
105 | protected: | |
106 | // Return an instance of the real target. This must be implemented | |
107 | // by the child class. | |
108 | virtual Target* | |
109 | do_instantiate_target() = 0; | |
110 | ||
36959681 ILT |
111 | // Recognize an object file given a machine code, OSABI code, and |
112 | // ELF version value. When this is called we already know that they | |
113 | // match the machine_, size_, and is_big_endian_ fields. The child | |
114 | // class may implement a different version of this to do additional | |
e96caa79 ILT |
115 | // checks, or to check for multiple machine codes if the machine_ |
116 | // field is EM_NONE. | |
117 | virtual Target* | |
118 | do_recognize(int, int, int) | |
119 | { return this->instantiate_target(); } | |
120 | ||
121 | // Recognize a target by name. When this is called we already know | |
122 | // that the name matches (or that the bfd_name_ field is NULL). The | |
123 | // child class may implement a different version of this to | |
124 | // recognize more than one name. | |
125 | virtual Target* | |
126 | do_recognize_by_name(const char*) | |
127 | { return this->instantiate_target(); } | |
128 | ||
129 | // Return a list of supported BFD names. The child class may | |
130 | // implement a different version of this to handle more than one | |
131 | // name. | |
132 | virtual void | |
133 | do_supported_names(std::vector<const char*>* names) | |
134 | { | |
135 | gold_assert(this->bfd_name_ != NULL); | |
136 | names->push_back(this->bfd_name_); | |
137 | } | |
138 | ||
e96caa79 ILT |
139 | // Instantiate the target and return it. |
140 | Target* | |
7f055c20 | 141 | instantiate_target(); |
e96caa79 | 142 | |
36959681 | 143 | private: |
e96caa79 ILT |
144 | // ELF machine code. |
145 | const int machine_; | |
146 | // Target size--32 or 64. | |
147 | const int size_; | |
148 | // Whether the target is big endian. | |
149 | const bool is_big_endian_; | |
150 | // BFD name of target, for compatibility. | |
151 | const char* const bfd_name_; | |
152 | // Next entry in list built at global constructor time. | |
14bfc3f5 | 153 | Target_selector* next_; |
e96caa79 ILT |
154 | // The singleton Target structure--this points to an instance of the |
155 | // real implementation. | |
156 | Target* instantiated_target_; | |
7f055c20 ILT |
157 | // Lock to make sure that we don't instantiate the target more than |
158 | // once. | |
159 | Lock* lock_; | |
160 | // We only want to initialize the lock_ pointer once. | |
161 | Initialize_lock initialize_lock_; | |
14bfc3f5 ILT |
162 | }; |
163 | ||
164 | // Select the target for an ELF file. | |
165 | ||
e96caa79 ILT |
166 | extern Target* |
167 | select_target(int machine, int size, bool big_endian, int osabi, | |
168 | int abiversion); | |
14bfc3f5 | 169 | |
0daa6f62 ILT |
170 | // Select a target using a BFD name. |
171 | ||
e96caa79 ILT |
172 | extern Target* |
173 | select_target_by_name(const char* name); | |
174 | ||
175 | // Fill in a vector with the list of supported targets. This returns | |
176 | // a list of BFD names. | |
177 | ||
178 | extern void | |
179 | supported_target_names(std::vector<const char*>*); | |
0daa6f62 | 180 | |
14bfc3f5 ILT |
181 | } // End namespace gold. |
182 | ||
183 | #endif // !defined(GOLD_TARGET_SELECT_H) |