xref: /dflybsd-src/contrib/binutils-2.27/gold/target.cc (revision e656dc90e3d65d744d534af2f5ea88cf8101ebcf)
1*a9fa9459Szrj // target.cc -- target support for gold.
2*a9fa9459Szrj 
3*a9fa9459Szrj // Copyright (C) 2009-2016 Free Software Foundation, Inc.
4*a9fa9459Szrj // Written by Doug Kwan <dougkwan@google.com>.
5*a9fa9459Szrj 
6*a9fa9459Szrj // This file is part of gold.
7*a9fa9459Szrj 
8*a9fa9459Szrj // This program is free software; you can redistribute it and/or modify
9*a9fa9459Szrj // it under the terms of the GNU General Public License as published by
10*a9fa9459Szrj // the Free Software Foundation; either version 3 of the License, or
11*a9fa9459Szrj // (at your option) any later version.
12*a9fa9459Szrj 
13*a9fa9459Szrj // This program is distributed in the hope that it will be useful,
14*a9fa9459Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*a9fa9459Szrj // GNU General Public License for more details.
17*a9fa9459Szrj 
18*a9fa9459Szrj // You should have received a copy of the GNU General Public License
19*a9fa9459Szrj // along with this program; if not, write to the Free Software
20*a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*a9fa9459Szrj // MA 02110-1301, USA.
22*a9fa9459Szrj 
23*a9fa9459Szrj #include "gold.h"
24*a9fa9459Szrj #include "elfcpp.h"
25*a9fa9459Szrj #include "dynobj.h"
26*a9fa9459Szrj #include "symtab.h"
27*a9fa9459Szrj #include "output.h"
28*a9fa9459Szrj #include "target.h"
29*a9fa9459Szrj 
30*a9fa9459Szrj namespace gold
31*a9fa9459Szrj {
32*a9fa9459Szrj 
33*a9fa9459Szrj // Return whether NAME is a local label name.  This is used to implement the
34*a9fa9459Szrj // --discard-locals options and can be overridden by child classes to
35*a9fa9459Szrj // implement system-specific behaviour.  The logic here is the same as that
36*a9fa9459Szrj // in _bfd_elf_is_local_label_name().
37*a9fa9459Szrj 
38*a9fa9459Szrj bool
do_is_local_label_name(const char * name) const39*a9fa9459Szrj Target::do_is_local_label_name(const char* name) const
40*a9fa9459Szrj {
41*a9fa9459Szrj   // Normal local symbols start with ``.L''.
42*a9fa9459Szrj   if (name[0] == '.' && name[1] == 'L')
43*a9fa9459Szrj     return true;
44*a9fa9459Szrj 
45*a9fa9459Szrj   // At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
46*a9fa9459Szrj   // DWARF debugging symbols starting with ``..''.
47*a9fa9459Szrj   if (name[0] == '.' && name[1] == '.')
48*a9fa9459Szrj     return true;
49*a9fa9459Szrj 
50*a9fa9459Szrj   // gcc will sometimes generate symbols beginning with ``_.L_'' when
51*a9fa9459Szrj   // emitting DWARF debugging output.  I suspect this is actually a
52*a9fa9459Szrj   // small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
53*a9fa9459Szrj   // ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
54*a9fa9459Szrj   // underscore to be emitted on some ELF targets).  For ease of use,
55*a9fa9459Szrj   // we treat such symbols as local.
56*a9fa9459Szrj   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
57*a9fa9459Szrj     return true;
58*a9fa9459Szrj 
59*a9fa9459Szrj   return false;
60*a9fa9459Szrj }
61*a9fa9459Szrj 
62*a9fa9459Szrj // Implementations of methods Target::do_make_elf_object are almost identical
63*a9fa9459Szrj // except for the address sizes and endianities.  So we extract this
64*a9fa9459Szrj // into a template.
65*a9fa9459Szrj 
66*a9fa9459Szrj template<int size, bool big_endian>
67*a9fa9459Szrj inline Object*
do_make_elf_object_implementation(const std::string & name,Input_file * input_file,off_t offset,const elfcpp::Ehdr<size,big_endian> & ehdr)68*a9fa9459Szrj Target::do_make_elf_object_implementation(
69*a9fa9459Szrj     const std::string& name,
70*a9fa9459Szrj     Input_file* input_file,
71*a9fa9459Szrj     off_t offset,
72*a9fa9459Szrj     const elfcpp::Ehdr<size, big_endian>& ehdr)
73*a9fa9459Szrj {
74*a9fa9459Szrj   int et = ehdr.get_e_type();
75*a9fa9459Szrj   // ET_EXEC files are valid input for --just-symbols/-R,
76*a9fa9459Szrj   // and we treat them as relocatable objects.
77*a9fa9459Szrj   if (et == elfcpp::ET_REL
78*a9fa9459Szrj       || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
79*a9fa9459Szrj     {
80*a9fa9459Szrj       Sized_relobj_file<size, big_endian>* obj =
81*a9fa9459Szrj 	new Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr);
82*a9fa9459Szrj       obj->setup();
83*a9fa9459Szrj       return obj;
84*a9fa9459Szrj     }
85*a9fa9459Szrj   else if (et == elfcpp::ET_DYN)
86*a9fa9459Szrj     {
87*a9fa9459Szrj       Sized_dynobj<size, big_endian>* obj =
88*a9fa9459Szrj 	new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
89*a9fa9459Szrj       obj->setup();
90*a9fa9459Szrj       return obj;
91*a9fa9459Szrj     }
92*a9fa9459Szrj   else
93*a9fa9459Szrj     {
94*a9fa9459Szrj       gold_error(_("%s: unsupported ELF file type %d"),
95*a9fa9459Szrj 		 name.c_str(), et);
96*a9fa9459Szrj       return NULL;
97*a9fa9459Szrj     }
98*a9fa9459Szrj }
99*a9fa9459Szrj 
100*a9fa9459Szrj // Make an ELF object called NAME by reading INPUT_FILE at OFFSET.  EHDR
101*a9fa9459Szrj // is the ELF header of the object.  There are four versions of this
102*a9fa9459Szrj // for different address sizes and endianities.
103*a9fa9459Szrj 
104*a9fa9459Szrj #ifdef HAVE_TARGET_32_LITTLE
105*a9fa9459Szrj Object*
do_make_elf_object(const std::string & name,Input_file * input_file,off_t offset,const elfcpp::Ehdr<32,false> & ehdr)106*a9fa9459Szrj Target::do_make_elf_object(const std::string& name, Input_file* input_file,
107*a9fa9459Szrj 			   off_t offset, const elfcpp::Ehdr<32, false>& ehdr)
108*a9fa9459Szrj {
109*a9fa9459Szrj   return this->do_make_elf_object_implementation<32, false>(name, input_file,
110*a9fa9459Szrj 							    offset, ehdr);
111*a9fa9459Szrj }
112*a9fa9459Szrj #endif
113*a9fa9459Szrj 
114*a9fa9459Szrj #ifdef HAVE_TARGET_32_BIG
115*a9fa9459Szrj Object*
do_make_elf_object(const std::string & name,Input_file * input_file,off_t offset,const elfcpp::Ehdr<32,true> & ehdr)116*a9fa9459Szrj Target::do_make_elf_object(const std::string& name, Input_file* input_file,
117*a9fa9459Szrj 			   off_t offset, const elfcpp::Ehdr<32, true>& ehdr)
118*a9fa9459Szrj {
119*a9fa9459Szrj   return this->do_make_elf_object_implementation<32, true>(name, input_file,
120*a9fa9459Szrj 							   offset, ehdr);
121*a9fa9459Szrj }
122*a9fa9459Szrj #endif
123*a9fa9459Szrj 
124*a9fa9459Szrj #ifdef HAVE_TARGET_64_LITTLE
125*a9fa9459Szrj Object*
do_make_elf_object(const std::string & name,Input_file * input_file,off_t offset,const elfcpp::Ehdr<64,false> & ehdr)126*a9fa9459Szrj Target::do_make_elf_object(const std::string& name, Input_file* input_file,
127*a9fa9459Szrj 			   off_t offset, const elfcpp::Ehdr<64, false>& ehdr)
128*a9fa9459Szrj {
129*a9fa9459Szrj   return this->do_make_elf_object_implementation<64, false>(name, input_file,
130*a9fa9459Szrj 							    offset, ehdr);
131*a9fa9459Szrj }
132*a9fa9459Szrj #endif
133*a9fa9459Szrj 
134*a9fa9459Szrj #ifdef HAVE_TARGET_64_BIG
135*a9fa9459Szrj Object*
do_make_elf_object(const std::string & name,Input_file * input_file,off_t offset,const elfcpp::Ehdr<64,true> & ehdr)136*a9fa9459Szrj Target::do_make_elf_object(const std::string& name, Input_file* input_file,
137*a9fa9459Szrj 			   off_t offset, const elfcpp::Ehdr<64, true>& ehdr)
138*a9fa9459Szrj {
139*a9fa9459Szrj   return this->do_make_elf_object_implementation<64, true>(name, input_file,
140*a9fa9459Szrj 							   offset, ehdr);
141*a9fa9459Szrj }
142*a9fa9459Szrj #endif
143*a9fa9459Szrj 
144*a9fa9459Szrj Output_section*
do_make_output_section(const char * name,elfcpp::Elf_Word type,elfcpp::Elf_Xword flags)145*a9fa9459Szrj Target::do_make_output_section(const char* name, elfcpp::Elf_Word type,
146*a9fa9459Szrj 			       elfcpp::Elf_Xword flags)
147*a9fa9459Szrj {
148*a9fa9459Szrj   return new Output_section(name, type, flags);
149*a9fa9459Szrj }
150*a9fa9459Szrj 
151*a9fa9459Szrj // Default for whether a reloc is a call to a non-split function is
152*a9fa9459Szrj // whether the symbol is a function.
153*a9fa9459Szrj 
154*a9fa9459Szrj bool
do_is_call_to_non_split(const Symbol * sym,const unsigned char *,const unsigned char *,section_size_type) const155*a9fa9459Szrj Target::do_is_call_to_non_split(const Symbol* sym, const unsigned char*,
156*a9fa9459Szrj 				const unsigned char*, section_size_type) const
157*a9fa9459Szrj {
158*a9fa9459Szrj   return sym->type() == elfcpp::STT_FUNC;
159*a9fa9459Szrj }
160*a9fa9459Szrj 
161*a9fa9459Szrj // Default conversion for -fsplit-stack is to give an error.
162*a9fa9459Szrj 
163*a9fa9459Szrj void
do_calls_non_split(Relobj * object,unsigned int,section_offset_type,section_size_type,const unsigned char *,size_t,unsigned char *,section_size_type,std::string *,std::string *) const164*a9fa9459Szrj Target::do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
165*a9fa9459Szrj 			   section_size_type, const unsigned char*, size_t,
166*a9fa9459Szrj 			   unsigned char*, section_size_type,
167*a9fa9459Szrj 			   std::string*, std::string*) const
168*a9fa9459Szrj {
169*a9fa9459Szrj   static bool warned;
170*a9fa9459Szrj   if (!warned)
171*a9fa9459Szrj     {
172*a9fa9459Szrj       gold_error(_("linker does not include stack split support "
173*a9fa9459Szrj 		   "required by %s"),
174*a9fa9459Szrj 		 object->name().c_str());
175*a9fa9459Szrj       warned = true;
176*a9fa9459Szrj     }
177*a9fa9459Szrj }
178*a9fa9459Szrj 
179*a9fa9459Szrj //  Return whether BYTES/LEN matches VIEW/VIEW_SIZE at OFFSET.
180*a9fa9459Szrj 
181*a9fa9459Szrj bool
match_view(const unsigned char * view,section_size_type view_size,section_offset_type offset,const char * bytes,size_t len) const182*a9fa9459Szrj Target::match_view(const unsigned char* view, section_size_type view_size,
183*a9fa9459Szrj 		   section_offset_type offset, const char* bytes,
184*a9fa9459Szrj 		   size_t len) const
185*a9fa9459Szrj {
186*a9fa9459Szrj   if (offset + len > view_size)
187*a9fa9459Szrj     return false;
188*a9fa9459Szrj   return memcmp(view + offset, bytes, len) == 0;
189*a9fa9459Szrj }
190*a9fa9459Szrj 
191*a9fa9459Szrj // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
192*a9fa9459Szrj // for LEN bytes.
193*a9fa9459Szrj 
194*a9fa9459Szrj void
set_view_to_nop(unsigned char * view,section_size_type view_size,section_offset_type offset,size_t len) const195*a9fa9459Szrj Target::set_view_to_nop(unsigned char* view, section_size_type view_size,
196*a9fa9459Szrj 			section_offset_type offset, size_t len) const
197*a9fa9459Szrj {
198*a9fa9459Szrj   gold_assert(offset >= 0 && offset + len <= view_size);
199*a9fa9459Szrj   if (!this->has_code_fill())
200*a9fa9459Szrj     memset(view + offset, 0, len);
201*a9fa9459Szrj   else
202*a9fa9459Szrj     {
203*a9fa9459Szrj       std::string fill = this->code_fill(len);
204*a9fa9459Szrj       memcpy(view + offset, fill.data(), len);
205*a9fa9459Szrj     }
206*a9fa9459Szrj }
207*a9fa9459Szrj 
208*a9fa9459Szrj // Return address and size to plug into eh_frame FDEs associated with a PLT.
209*a9fa9459Szrj void
do_plt_fde_location(const Output_data * plt,unsigned char *,uint64_t * address,off_t * len) const210*a9fa9459Szrj Target::do_plt_fde_location(const Output_data* plt, unsigned char*,
211*a9fa9459Szrj 			    uint64_t* address, off_t* len) const
212*a9fa9459Szrj {
213*a9fa9459Szrj   *address = plt->address();
214*a9fa9459Szrj   *len = plt->data_size();
215*a9fa9459Szrj }
216*a9fa9459Szrj 
217*a9fa9459Szrj // Class Sized_target.
218*a9fa9459Szrj 
219*a9fa9459Szrj // Set the EI_OSABI field of the ELF header if requested.
220*a9fa9459Szrj 
221*a9fa9459Szrj template<int size, bool big_endian>
222*a9fa9459Szrj void
do_adjust_elf_header(unsigned char * view,int len)223*a9fa9459Szrj Sized_target<size, big_endian>::do_adjust_elf_header(unsigned char* view,
224*a9fa9459Szrj 						     int len)
225*a9fa9459Szrj {
226*a9fa9459Szrj   elfcpp::ELFOSABI osabi = this->osabi();
227*a9fa9459Szrj   if (osabi != elfcpp::ELFOSABI_NONE)
228*a9fa9459Szrj     {
229*a9fa9459Szrj       gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
230*a9fa9459Szrj 
231*a9fa9459Szrj       elfcpp::Ehdr<size, big_endian> ehdr(view);
232*a9fa9459Szrj       unsigned char e_ident[elfcpp::EI_NIDENT];
233*a9fa9459Szrj       memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
234*a9fa9459Szrj 
235*a9fa9459Szrj       e_ident[elfcpp::EI_OSABI] = osabi;
236*a9fa9459Szrj 
237*a9fa9459Szrj       elfcpp::Ehdr_write<size, big_endian> oehdr(view);
238*a9fa9459Szrj       oehdr.put_e_ident(e_ident);
239*a9fa9459Szrj     }
240*a9fa9459Szrj }
241*a9fa9459Szrj 
242*a9fa9459Szrj #ifdef HAVE_TARGET_32_LITTLE
243*a9fa9459Szrj template
244*a9fa9459Szrj class Sized_target<32, false>;
245*a9fa9459Szrj #endif
246*a9fa9459Szrj 
247*a9fa9459Szrj #ifdef HAVE_TARGET_32_BIG
248*a9fa9459Szrj template
249*a9fa9459Szrj class Sized_target<32, true>;
250*a9fa9459Szrj #endif
251*a9fa9459Szrj 
252*a9fa9459Szrj #ifdef HAVE_TARGET_64_LITTLE
253*a9fa9459Szrj template
254*a9fa9459Szrj class Sized_target<64, false>;
255*a9fa9459Szrj #endif
256*a9fa9459Szrj 
257*a9fa9459Szrj #ifdef HAVE_TARGET_64_BIG
258*a9fa9459Szrj template
259*a9fa9459Szrj class Sized_target<64, true>;
260*a9fa9459Szrj #endif
261*a9fa9459Szrj 
262*a9fa9459Szrj } // End namespace gold.
263