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