1 // target-select.h -- select a target for an object file -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #ifndef GOLD_TARGET_SELECT_H
24 #define GOLD_TARGET_SELECT_H
28 #include "gold-threads.h"
34 class Target_selector
;
36 // Used to set the target only once.
38 class Set_target_once
: public Once
41 Set_target_once(Target_selector
* target_selector
)
42 : target_selector_(target_selector
)
50 Target_selector
* target_selector_
;
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.
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
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
68 // NULL. EMULATION is the name of the emulation used by the GNU
69 // linker; it is similar to BFD_NAME.
70 Target_selector(int machine
, int size
, bool is_big_endian
,
71 const char* bfd_name
, const char* emulation
);
73 virtual ~Target_selector()
76 // If we can handle this target, return a pointer to a target
77 // structure. The size and endianness are known.
79 recognize(int machine
, int osabi
, int abiversion
)
80 { return this->do_recognize(machine
, osabi
, abiversion
); }
82 // If NAME matches the target, return a pointer to a target
85 recognize_by_bfd_name(const char* name
)
86 { return this->do_recognize_by_bfd_name(name
); }
88 // Push all supported BFD names onto the vector. This is only used
91 supported_bfd_names(std::vector
<const char*>* names
)
92 { this->do_supported_bfd_names(names
); }
94 // If NAME matches the target emulation, return a pointer to a
97 recognize_by_emulation(const char* name
)
98 { return this->do_recognize_by_emulation(name
); }
100 // Push all supported emulations onto the vector. This is only used
103 supported_emulations(std::vector
<const char*>* names
)
104 { this->do_supported_emulations(names
); }
106 // Return the next Target_selector in the linked list.
109 { return this->next_
; }
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
117 { return this->machine_
; }
119 // Return the size this is looking for (32 or 64).
122 { return this->size_
; }
124 // Return the endianness this is looking for.
126 is_big_endian() const
127 { return this->is_big_endian_
; }
129 // Return the BFD name. This may return NULL, in which case the
130 // do_recognize_by_bfd_name hook will be responsible for matching
134 { return this->bfd_name_
; }
136 // Return the emulation. This may return NULL, in which case the
137 // do_recognize_by_emulation hook will be responsible for matching
141 { return this->emulation_
; }
144 // Return an instance of the real target. This must be implemented
145 // by the child class.
147 do_instantiate_target() = 0;
149 // Recognize an object file given a machine code, OSABI code, and
150 // ELF version value. When this is called we already know that they
151 // match the machine_, size_, and is_big_endian_ fields. The child
152 // class may implement a different version of this to do additional
153 // checks, or to check for multiple machine codes if the machine_
156 do_recognize(int, int, int)
157 { return this->instantiate_target(); }
159 // Recognize a target by name. When this is called we already know
160 // that the name matches (or that the bfd_name_ field is NULL). The
161 // child class may implement a different version of this to
162 // recognize more than one name.
164 do_recognize_by_bfd_name(const char*)
165 { return this->instantiate_target(); }
167 // Return a list of supported BFD names. The child class may
168 // implement a different version of this to handle more than one
171 do_supported_bfd_names(std::vector
<const char*>* names
)
173 gold_assert(this->bfd_name_
!= NULL
);
174 names
->push_back(this->bfd_name_
);
177 // Recognize a target by emulation. When this is called we already
178 // know that the name matches (or that the emulation_ field is
179 // NULL). The child class may implement a different version of this
180 // to recognize more than one emulation.
182 do_recognize_by_emulation(const char*)
183 { return this->instantiate_target(); }
185 // Return a list of supported emulations. The child class may
186 // implement a different version of this to handle more than one
189 do_supported_emulations(std::vector
<const char*>* emulations
)
191 gold_assert(this->emulation_
!= NULL
);
192 emulations
->push_back(this->emulation_
);
195 // Instantiate the target and return it.
197 instantiate_target();
204 friend class Set_target_once
;
208 // Target size--32 or 64.
210 // Whether the target is big endian.
211 const bool is_big_endian_
;
212 // BFD name of target, for compatibility.
213 const char* const bfd_name_
;
214 // GNU linker emulation for this target, for compatibility.
215 const char* const emulation_
;
216 // Next entry in list built at global constructor time.
217 Target_selector
* next_
;
218 // The singleton Target structure--this points to an instance of the
219 // real implementation.
220 Target
* instantiated_target_
;
221 // Used to set the target only once.
222 Set_target_once set_target_once_
;
225 // Select the target for an ELF file.
228 select_target(int machine
, int size
, bool big_endian
, int osabi
,
231 // Select a target using a BFD name.
234 select_target_by_bfd_name(const char* name
);
236 // Select a target using a GNU linker emulation.
239 select_target_by_emulation(const char* name
);
241 // Fill in a vector with the list of supported targets. This returns
242 // a list of BFD names.
245 supported_target_names(std::vector
<const char*>*);
247 // Fill in a vector with the list of supported emulations.
250 supported_emulation_names(std::vector
<const char*>*);
252 } // End namespace gold.
254 #endif // !defined(GOLD_TARGET_SELECT_H)