| 1 | // target-select.h -- select a target for an object file -*- C++ -*- |
| 2 | |
| 3 | // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
| 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 | |
| 23 | #ifndef GOLD_TARGET_SELECT_H |
| 24 | #define GOLD_TARGET_SELECT_H |
| 25 | |
| 26 | #include <vector> |
| 27 | |
| 28 | #include "gold-threads.h" |
| 29 | |
| 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 |
| 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); |
| 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. |
| 59 | Target* |
| 60 | recognize(int mach, int osabi, int abiversion) |
| 61 | { return this->do_recognize(mach, osabi, abiversion); } |
| 62 | |
| 63 | // If NAME matches the target, return a pointer to a target |
| 64 | // structure. |
| 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); } |
| 74 | |
| 75 | // Return the next Target_selector in the linked list. |
| 76 | Target_selector* |
| 77 | next() const |
| 78 | { return this->next_; } |
| 79 | |
| 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. |
| 84 | int |
| 85 | machine() const |
| 86 | { return this->machine_; } |
| 87 | |
| 88 | // Return the size this is looking for (32 or 64). |
| 89 | int |
| 90 | get_size() const |
| 91 | { return this->size_; } |
| 92 | |
| 93 | // Return the endianness this is looking for. |
| 94 | bool |
| 95 | is_big_endian() const |
| 96 | { return this->is_big_endian_; } |
| 97 | |
| 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 | |
| 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 |
| 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 | |
| 139 | // Instantiate the target and return it. |
| 140 | Target* |
| 141 | instantiate_target(); |
| 142 | |
| 143 | private: |
| 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. |
| 153 | Target_selector* next_; |
| 154 | // The singleton Target structure--this points to an instance of the |
| 155 | // real implementation. |
| 156 | Target* instantiated_target_; |
| 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_; |
| 162 | }; |
| 163 | |
| 164 | // Select the target for an ELF file. |
| 165 | |
| 166 | extern Target* |
| 167 | select_target(int machine, int size, bool big_endian, int osabi, |
| 168 | int abiversion); |
| 169 | |
| 170 | // Select a target using a BFD name. |
| 171 | |
| 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*>*); |
| 180 | |
| 181 | } // End namespace gold. |
| 182 | |
| 183 | #endif // !defined(GOLD_TARGET_SELECT_H) |