1// target-select.h -- select a target for an object file  -*- C++ -*-
2
3// Copyright 2006, 2007, 2008, 2009, 2010 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
30namespace gold
31{
32
33class Target;
34class Target_selector;
35
36// Used to set the target only once.
37
38class Set_target_once : public Once
39{
40 public:
41  Set_target_once(Target_selector* target_selector)
42    : target_selector_(target_selector)
43  { }
44
45 protected:
46  void
47  do_run_once(void*);
48
49 private:
50  Target_selector* target_selector_;
51};
52
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.
60
61class Target_selector
62{
63 public:
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.
69  Target_selector(int machine, int size, bool is_big_endian,
70		  const char* bfd_name);
71
72  virtual ~Target_selector()
73  { }
74
75  // If we can handle this target, return a pointer to a target
76  // structure.  The size and endianness are known.
77  Target*
78  recognize(int machine, int osabi, int abiversion)
79  { return this->do_recognize(machine, osabi, abiversion); }
80
81  // If NAME matches the target, return a pointer to a target
82  // structure.
83  Target*
84  recognize_by_name(const char* name)
85  { return this->do_recognize_by_name(name); }
86
87  // Push all supported names onto the vector.  This is only used for
88  // help output.
89  void
90  supported_names(std::vector<const char*>* names)
91  { this->do_supported_names(names); }
92
93  // Return the next Target_selector in the linked list.
94  Target_selector*
95  next() const
96  { return this->next_; }
97
98  // Return the machine number this selector is looking for.  This can
99  // be EM_NONE to match any machine number, in which case the
100  // do_recognize hook will be responsible for matching the machine
101  // number.
102  int
103  machine() const
104  { return this->machine_; }
105
106  // Return the size this is looking for (32 or 64).
107  int
108  get_size() const
109  { return this->size_; }
110
111  // Return the endianness this is looking for.
112  bool
113  is_big_endian() const
114  { return this->is_big_endian_; }
115
116  // Return the BFD name.  This may return NULL, in which case the
117  // do_recognize_by_name hook will be responsible for matching the
118  // BFD name.
119  const char*
120  bfd_name() const
121  { return this->bfd_name_; }
122
123 protected:
124  // Return an instance of the real target.  This must be implemented
125  // by the child class.
126  virtual Target*
127  do_instantiate_target() = 0;
128
129  // Recognize an object file given a machine code, OSABI code, and
130  // ELF version value.  When this is called we already know that they
131  // match the machine_, size_, and is_big_endian_ fields.  The child
132  // class may implement a different version of this to do additional
133  // checks, or to check for multiple machine codes if the machine_
134  // field is EM_NONE.
135  virtual Target*
136  do_recognize(int, int, int)
137  { return this->instantiate_target(); }
138
139  // Recognize a target by name.  When this is called we already know
140  // that the name matches (or that the bfd_name_ field is NULL).  The
141  // child class may implement a different version of this to
142  // recognize more than one name.
143  virtual Target*
144  do_recognize_by_name(const char*)
145  { return this->instantiate_target(); }
146
147  // Return a list of supported BFD names.  The child class may
148  // implement a different version of this to handle more than one
149  // name.
150  virtual void
151  do_supported_names(std::vector<const char*>* names)
152  {
153    gold_assert(this->bfd_name_ != NULL);
154    names->push_back(this->bfd_name_);
155  }
156
157  // Instantiate the target and return it.
158  Target*
159  instantiate_target();
160
161 private:
162  // Set the target.
163  void
164  set_target();
165
166  friend class Set_target_once;
167
168  // ELF machine code.
169  const int machine_;
170  // Target size--32 or 64.
171  const int size_;
172  // Whether the target is big endian.
173  const bool is_big_endian_;
174  // BFD name of target, for compatibility.
175  const char* const bfd_name_;
176  // Next entry in list built at global constructor time.
177  Target_selector* next_;
178  // The singleton Target structure--this points to an instance of the
179  // real implementation.
180  Target* instantiated_target_;
181  // Used to set the target only once.
182  Set_target_once set_target_once_;
183};
184
185// Select the target for an ELF file.
186
187extern Target*
188select_target(int machine, int size, bool big_endian, int osabi,
189	      int abiversion);
190
191// Select a target using a BFD name.
192
193extern Target*
194select_target_by_name(const char* name);
195
196// Fill in a vector with the list of supported targets.  This returns
197// a list of BFD names.
198
199extern void
200supported_target_names(std::vector<const char*>*);
201
202} // End namespace gold.
203
204#endif // !defined(GOLD_TARGET_SELECT_H)
205