1 // target-select.h -- select a target for an object file -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
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.
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.
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.
24 #ifndef GOLD_TARGET_SELECT_H
25 #define GOLD_TARGET_SELECT_H
29 #include "gold-threads.h"
36 class Target_selector
;
38 // Used to set the target only once.
40 class Set_target_once
: public Once
43 Set_target_once(Target_selector
* target_selector
)
44 : target_selector_(target_selector
)
52 Target_selector
* target_selector_
;
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.
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
);
75 virtual ~Target_selector()
78 // If we can handle this target, return a pointer to a target
79 // structure. The size and endianness are known.
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
); }
85 // If NAME matches the target, return a pointer to a target
88 recognize_by_bfd_name(const char* name
)
89 { return this->do_recognize_by_bfd_name(name
); }
91 // Push all supported BFD names onto the vector. This is only used
94 supported_bfd_names(std::vector
<const char*>* names
)
95 { this->do_supported_bfd_names(names
); }
97 // If NAME matches the target emulation, return a pointer to a
100 recognize_by_emulation(const char* name
)
101 { return this->do_recognize_by_emulation(name
); }
103 // Push all supported emulations onto the vector. This is only used
106 supported_emulations(std::vector
<const char*>* names
)
107 { this->do_supported_emulations(names
); }
109 // Return the next Target_selector in the linked list.
112 { return this->next_
; }
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
120 { return this->machine_
; }
122 // Return the size this is looking for (32 or 64).
125 { return this->size_
; }
127 // Return the endianness this is looking for.
129 is_big_endian() const
130 { return this->is_big_endian_
; }
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
137 { return this->bfd_name_
; }
139 // Return the emulation. This may return NULL, in which case the
140 // do_recognize_by_emulation hook will be responsible for matching
144 { return this->emulation_
; }
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.
150 target_bfd_name(const Target
* target
)
151 { return this->do_target_bfd_name(target
); }
154 // Return an instance of the real target. This must be implemented
155 // by the child class.
157 do_instantiate_target() = 0;
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_
166 do_recognize(Input_file
*, off_t
, int, int, int)
167 { return this->instantiate_target(); }
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.
174 do_recognize_by_bfd_name(const char*)
175 { return this->instantiate_target(); }
177 // Return a list of supported BFD names. The child class may
178 // implement a different version of this to handle more than one
181 do_supported_bfd_names(std::vector
<const char*>* names
)
183 gold_assert(this->bfd_name_
!= NULL
);
184 names
->push_back(this->bfd_name_
);
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.
192 do_recognize_by_emulation(const char*)
193 { return this->instantiate_target(); }
195 // Return a list of supported emulations. The child class may
196 // implement a different version of this to handle more than one
199 do_supported_emulations(std::vector
<const char*>* emulations
)
201 gold_assert(this->emulation_
!= NULL
);
202 emulations
->push_back(this->emulation_
);
205 // Map from target to BFD name.
207 do_target_bfd_name(const Target
*);
209 // Instantiate the target and return it.
211 instantiate_target();
213 // Return whether TARGET is the target we instantiated.
215 is_our_target(const Target
* target
)
216 { return target
== this->instantiated_target_
; }
223 friend class Set_target_once
;
227 // Target size--32 or 64.
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_
;
244 // Select the target for an ELF file.
247 select_target(Input_file
*, off_t
,
248 int machine
, int size
, bool big_endian
, int osabi
,
251 // Select a target using a BFD name.
254 select_target_by_bfd_name(const char* name
);
256 // Select a target using a GNU linker emulation.
259 select_target_by_emulation(const char* name
);
261 // Fill in a vector with the list of supported targets. This returns
262 // a list of BFD names.
265 supported_target_names(std::vector
<const char*>*);
267 // Fill in a vector with the list of supported emulations.
270 supported_emulation_names(std::vector
<const char*>*);
272 // Print the output format, for the --print-output-format option.
275 print_output_format();
277 } // End namespace gold.
279 #endif // !defined(GOLD_TARGET_SELECT_H)