xref: /netbsd-src/external/gpl3/binutils/dist/gold/target.cc (revision 567219e1d7461bff1b180e494a9674a287b057a7)
1 // target.cc
2 
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@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 #include "elfcpp.h"
25 #include "dynobj.h"
26 #include "symtab.h"
27 #include "output.h"
28 #include "target.h"
29 
30 namespace gold
31 {
32 
33 // Return whether NAME is a local label name.  This is used to implement the
34 // --discard-locals options and can be overriden by children classes to
35 // implement system-specific behaviour.  The logic here is the same as that
36 // in _bfd_elf_is_local_label_name().
37 
38 bool
39 Target::do_is_local_label_name(const char* name) const
40 {
41   // Normal local symbols start with ``.L''.
42   if (name[0] == '.' && name[1] == 'L')
43     return true;
44 
45   // At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
46   // DWARF debugging symbols starting with ``..''.
47   if (name[0] == '.' && name[1] == '.')
48     return true;
49 
50   // gcc will sometimes generate symbols beginning with ``_.L_'' when
51   // emitting DWARF debugging output.  I suspect this is actually a
52   // small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
53   // ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
54   // underscore to be emitted on some ELF targets).  For ease of use,
55   // we treat such symbols as local.
56   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
57     return true;
58 
59   return false;
60 }
61 
62 // Implementations of methods Target::do_make_elf_object are almost identical
63 // except for the address sizes and endianities.  So we extract this
64 // into a template.
65 
66 template<int size, bool big_endian>
67 inline Object*
68 Target::do_make_elf_object_implementation(
69     const std::string& name,
70     Input_file* input_file,
71     off_t offset,
72     const elfcpp::Ehdr<size, big_endian>& ehdr)
73 {
74   int et = ehdr.get_e_type();
75   if (et == elfcpp::ET_REL)
76     {
77       Sized_relobj<size, big_endian>* obj =
78 	new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
79       obj->setup();
80       return obj;
81     }
82   else if (et == elfcpp::ET_DYN)
83     {
84       Sized_dynobj<size, big_endian>* obj =
85 	new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
86       obj->setup();
87       return obj;
88     }
89   else
90     {
91       gold_error(_("%s: unsupported ELF file type %d"),
92 		 name.c_str(), et);
93       return NULL;
94     }
95 }
96 
97 // Make an ELF object called NAME by reading INPUT_FILE at OFFSET.  EHDR
98 // is the ELF header of the object.  There are four versions of this
99 // for different address sizes and endianities.
100 
101 #ifdef HAVE_TARGET_32_LITTLE
102 Object*
103 Target::do_make_elf_object(const std::string& name, Input_file* input_file,
104 			   off_t offset, const elfcpp::Ehdr<32, false>& ehdr)
105 {
106   return this->do_make_elf_object_implementation<32, false>(name, input_file,
107 							    offset, ehdr);
108 }
109 #endif
110 
111 #ifdef HAVE_TARGET_32_BIG
112 Object*
113 Target::do_make_elf_object(const std::string& name, Input_file* input_file,
114 			   off_t offset, const elfcpp::Ehdr<32, true>& ehdr)
115 {
116   return this->do_make_elf_object_implementation<32, true>(name, input_file,
117 							   offset, ehdr);
118 }
119 #endif
120 
121 #ifdef HAVE_TARGET_64_LITTLE
122 Object*
123 Target::do_make_elf_object(const std::string& name, Input_file* input_file,
124 			   off_t offset, const elfcpp::Ehdr<64, false>& ehdr)
125 {
126   return this->do_make_elf_object_implementation<64, false>(name, input_file,
127 							    offset, ehdr);
128 }
129 #endif
130 
131 #ifdef HAVE_TARGET_64_BIG
132 Object*
133 Target::do_make_elf_object(const std::string& name, Input_file* input_file,
134 			   off_t offset, const elfcpp::Ehdr<64, true>& ehdr)
135 {
136   return this->do_make_elf_object_implementation<64, true>(name, input_file,
137 							   offset, ehdr);
138 }
139 #endif
140 
141 Output_section*
142 Target::do_make_output_section(const char* name, elfcpp::Elf_Word type,
143 			       elfcpp::Elf_Xword flags)
144 {
145   return new Output_section(name, type, flags);
146 }
147 
148 // Default for whether a reloc is a call to a non-split function is
149 // whether the symbol is a function.
150 
151 bool
152 Target::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
153 {
154   return sym->type() == elfcpp::STT_FUNC;
155 }
156 
157 // Default conversion for -fsplit-stack is to give an error.
158 
159 void
160 Target::do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
161 			   section_size_type, unsigned char*, section_size_type,
162 			   std::string*, std::string*) const
163 {
164   static bool warned;
165   if (!warned)
166     {
167       gold_error(_("linker does not include stack split support "
168 		   "required by %s"),
169 		 object->name().c_str());
170       warned = true;
171     }
172 }
173 
174 //  Return whether BYTES/LEN matches VIEW/VIEW_SIZE at OFFSET.
175 
176 bool
177 Target::match_view(const unsigned char* view, section_size_type view_size,
178 		   section_offset_type offset, const char* bytes,
179 		   size_t len) const
180 {
181   if (offset + len > view_size)
182     return false;
183   return memcmp(view + offset, bytes, len) == 0;
184 }
185 
186 // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
187 // for LEN bytes.
188 
189 void
190 Target::set_view_to_nop(unsigned char* view, section_size_type view_size,
191 			section_offset_type offset, size_t len) const
192 {
193   gold_assert(offset >= 0 && offset + len <= view_size);
194   if (!this->has_code_fill())
195     memset(view + offset, 0, len);
196   else
197     {
198       std::string fill = this->code_fill(len);
199       memcpy(view + offset, fill.data(), len);
200     }
201 }
202 
203 } // End namespace gold.
204