PR gold/5986
[platform/upstream/binutils.git] / gold / target-select.cc
1 // target-select.cc -- select a target for an object file
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 #include "gold.h"
24
25 #include <cstring>
26
27 #include "elfcpp.h"
28 #include "target-select.h"
29
30 namespace
31 {
32
33 // The start of the list of target selectors.
34
35 gold::Target_selector* target_selectors;
36
37 } // End anonymous namespace.
38
39 namespace gold
40 {
41
42 // Construct a Target_selector, which means adding it to the linked
43 // list.  This runs at global constructor time, so we want it to be
44 // fast.
45
46 Target_selector::Target_selector(int machine, int size, bool is_big_endian,
47                                  const char* bfd_name)
48   : machine_(machine), size_(size), is_big_endian_(is_big_endian),
49     bfd_name_(bfd_name), instantiated_target_(NULL)
50     
51 {
52   this->next_ = target_selectors;
53   target_selectors = this;
54 }
55
56 // Find the target for an ELF file.
57
58 Target*
59 select_target(int machine, int size, bool is_big_endian, int osabi,
60               int abiversion)
61 {
62   for (Target_selector* p = target_selectors; p != NULL; p = p->next())
63     {
64       int pmach = p->machine();
65       if ((pmach == machine || pmach == elfcpp::EM_NONE)
66           && p->get_size() == size
67           && (p->is_big_endian() ? is_big_endian : !is_big_endian))
68         {
69           Target* ret = p->recognize(machine, osabi, abiversion);
70           if (ret != NULL)
71             return ret;
72         }
73     }
74   return NULL;
75 }
76
77 // Find a target using a BFD name.  This is used to support the
78 // --oformat option.
79
80 Target*
81 select_target_by_name(const char* name)
82 {
83   for (Target_selector* p = target_selectors; p != NULL; p = p->next())
84     {
85       const char* pname = p->bfd_name();
86       if (pname == NULL || strcmp(pname, name) == 0)
87         {
88           Target* ret = p->recognize_by_name(name);
89           if (ret != NULL)
90             return ret;
91         }
92     }
93   return NULL;
94 }
95
96 // Push all the supported BFD names onto a vector.
97
98 void
99 supported_target_names(std::vector<const char*>* names)
100 {
101   for (Target_selector* p = target_selectors; p != NULL; p = p->next())
102     p->supported_names(names);
103 }
104
105 } // End namespace gold.