1 // target-select.cc -- select a target for an object file
3 // Copyright (C) 2006-2017 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.
30 #include "parameters.h"
31 #include "target-select.h"
36 // The start of the list of target selectors.
38 gold::Target_selector
* target_selectors
;
40 } // End anonymous namespace.
45 // Class Set_target_once.
48 Set_target_once::do_run_once(void*)
50 this->target_selector_
->set_target();
53 // Construct a Target_selector, which means adding it to the linked
54 // list. This runs at global constructor time, so we want it to be
57 Target_selector::Target_selector(int machine
, int size
, bool is_big_endian
,
58 const char* bfd_name
, const char* emulation
)
59 : machine_(machine
), size_(size
), is_big_endian_(is_big_endian
),
60 bfd_name_(bfd_name
), emulation_(emulation
), instantiated_target_(NULL
),
61 set_target_once_(this)
63 this->next_
= target_selectors
;
64 target_selectors
= this;
67 // Instantiate the target and return it. Use SET_TARGET_ONCE_ to
68 // avoid instantiating two instances of the same target.
71 Target_selector::instantiate_target()
73 this->set_target_once_
.run_once(NULL
);
74 return this->instantiated_target_
;
77 // Instantiate the target. This is called at most once.
80 Target_selector::set_target()
82 gold_assert(this->instantiated_target_
== NULL
);
83 this->instantiated_target_
= this->do_instantiate_target();
86 // If we instantiated TARGET, return the corresponding BFD name.
89 Target_selector::do_target_bfd_name(const Target
* target
)
91 if (!this->is_our_target(target
))
93 const char* my_bfd_name
= this->bfd_name();
94 gold_assert(my_bfd_name
!= NULL
);
98 // Find the target for an ELF file.
101 select_target(Input_file
* input_file
, off_t offset
,
102 int machine
, int size
, bool is_big_endian
,
103 int osabi
, int abiversion
)
105 for (Target_selector
* p
= target_selectors
; p
!= NULL
; p
= p
->next())
107 int pmach
= p
->machine();
108 if ((pmach
== machine
|| pmach
== elfcpp::EM_NONE
)
109 && p
->get_size() == size
110 && (p
->is_big_endian() ? is_big_endian
: !is_big_endian
))
112 Target
* ret
= p
->recognize(input_file
, offset
,
113 machine
, osabi
, abiversion
);
121 // Find a target using a BFD name. This is used to support the
125 select_target_by_bfd_name(const char* name
)
127 for (Target_selector
* p
= target_selectors
; p
!= NULL
; p
= p
->next())
129 const char* pname
= p
->bfd_name();
130 if (pname
== NULL
|| strcmp(pname
, name
) == 0)
132 Target
* ret
= p
->recognize_by_bfd_name(name
);
140 // Find a target using a GNU linker emulation. This is used to
141 // support the -m option.
144 select_target_by_emulation(const char* name
)
146 for (Target_selector
* p
= target_selectors
; p
!= NULL
; p
= p
->next())
148 const char* pname
= p
->emulation();
149 if (pname
== NULL
|| strcmp(pname
, name
) == 0)
151 Target
* ret
= p
->recognize_by_emulation(name
);
159 // Push all the supported BFD names onto a vector.
162 supported_target_names(std::vector
<const char*>* names
)
164 for (Target_selector
* p
= target_selectors
; p
!= NULL
; p
= p
->next())
165 p
->supported_bfd_names(names
);
168 // Push all the supported emulations onto a vector.
171 supported_emulation_names(std::vector
<const char*>* names
)
173 for (Target_selector
* p
= target_selectors
; p
!= NULL
; p
= p
->next())
174 p
->supported_emulations(names
);
177 // Implement the --print-output-format option.
180 print_output_format()
182 if (!parameters
->target_valid())
184 // This case arises when --print-output-format is used with no
185 // input files. We need to come up with the right string to
186 // print based on the other options. If the user specified the
187 // format using a --oformat option, use that. That saves each
188 // target from having to remember the name that was used to
189 // select it. In other cases, we will just have to ask the
191 if (parameters
->options().user_set_oformat())
193 const char* bfd_name
= parameters
->options().oformat();
194 Target
* target
= select_target_by_bfd_name(bfd_name
);
196 printf("%s\n", bfd_name
);
198 gold_error(_("unrecognized output format %s"), bfd_name
);
202 parameters_force_valid_target();
205 const Target
* target
= ¶meters
->target();
206 for (Target_selector
* p
= target_selectors
; p
!= NULL
; p
= p
->next())
208 const char* bfd_name
= p
->target_bfd_name(target
);
209 if (bfd_name
!= NULL
)
211 printf("%s\n", bfd_name
);
219 } // End namespace gold.