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