* arm.cc (has_signed_unsigned_overflow): New function.
[deliverable/binutils-gdb.git] / gold / target.h
CommitLineData
14bfc3f5 1// target.h -- target support for gold -*- C++ -*-
bae7f79e 2
6d03d481 3// Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6cb15b7f
ILT
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
bae7f79e
ILT
23// The abstract class Target is the interface for target specific
24// support. It defines abstract methods which each target must
25// implement. Typically there will be one target per processor, but
26// in some cases it may be necessary to have subclasses.
27
28// For speed and consistency we want to use inline functions to handle
29// relocation processing. So besides implementations of the abstract
30// methods, each target is expected to define a template
31// specialization of the relocation functions.
32
33#ifndef GOLD_TARGET_H
34#define GOLD_TARGET_H
35
14bfc3f5 36#include "elfcpp.h"
8851ecca 37#include "options.h"
cd72c291 38#include "parameters.h"
14bfc3f5 39
bae7f79e
ILT
40namespace gold
41{
42
92e059d8 43class General_options;
14bfc3f5 44class Object;
61ba1cf9 45template<int size, bool big_endian>
f6ce93d6 46class Sized_relobj;
6a74a719 47class Relocatable_relocs;
92e059d8 48template<int size, bool big_endian>
730cdc88 49class Relocate_info;
f6ce93d6
ILT
50class Symbol;
51template<int size>
52class Sized_symbol;
53class Symbol_table;
730cdc88 54class Output_section;
14bfc3f5
ILT
55
56// The abstract class for target specific handling.
57
bae7f79e
ILT
58class Target
59{
60 public:
14bfc3f5
ILT
61 virtual ~Target()
62 { }
63
64 // Return the bit size that this target implements. This should
65 // return 32 or 64.
66 int
67 get_size() const
75f65a3e 68 { return this->pti_->size; }
14bfc3f5
ILT
69
70 // Return whether this target is big-endian.
71 bool
72 is_big_endian() const
75f65a3e 73 { return this->pti_->is_big_endian; }
14bfc3f5 74
61ba1cf9
ILT
75 // Machine code to store in e_machine field of ELF header.
76 elfcpp::EM
77 machine_code() const
78 { return this->pti_->machine_code; }
79
14bfc3f5
ILT
80 // Whether this target has a specific make_symbol function.
81 bool
82 has_make_symbol() const
75f65a3e 83 { return this->pti_->has_make_symbol; }
14bfc3f5
ILT
84
85 // Whether this target has a specific resolve function.
86 bool
87 has_resolve() const
75f65a3e
ILT
88 { return this->pti_->has_resolve; }
89
c51e6221
ILT
90 // Whether this target has a specific code fill function.
91 bool
92 has_code_fill() const
93 { return this->pti_->has_code_fill; }
94
dbe717ef
ILT
95 // Return the default name of the dynamic linker.
96 const char*
97 dynamic_linker() const
98 { return this->pti_->dynamic_linker; }
99
75f65a3e
ILT
100 // Return the default address to use for the text segment.
101 uint64_t
0c5e9c22
ILT
102 default_text_segment_address() const
103 { return this->pti_->default_text_segment_address; }
75f65a3e
ILT
104
105 // Return the ABI specified page size.
106 uint64_t
107 abi_pagesize() const
cd72c291 108 {
8851ecca
ILT
109 if (parameters->options().max_page_size() > 0)
110 return parameters->options().max_page_size();
cd72c291
ILT
111 else
112 return this->pti_->abi_pagesize;
113 }
75f65a3e
ILT
114
115 // Return the common page size used on actual systems.
116 uint64_t
117 common_pagesize() const
cd72c291 118 {
8851ecca
ILT
119 if (parameters->options().common_page_size() > 0)
120 return std::min(parameters->options().common_page_size(),
cd72c291
ILT
121 this->abi_pagesize());
122 else
123 return std::min(this->pti_->common_pagesize,
124 this->abi_pagesize());
125 }
14bfc3f5 126
35cdfc9a
ILT
127 // If we see some object files with .note.GNU-stack sections, and
128 // some objects files without them, this returns whether we should
129 // consider the object files without them to imply that the stack
130 // should be executable.
131 bool
132 is_default_stack_executable() const
133 { return this->pti_->is_default_stack_executable; }
134
0864d551
ILT
135 // Return a character which may appear as a prefix for a wrap
136 // symbol. If this character appears, we strip it when checking for
137 // wrapping and add it back when forming the final symbol name.
138 // This should be '\0' if not special prefix is required, which is
139 // the normal case.
140 char
141 wrap_char() const
142 { return this->pti_->wrap_char; }
143
8a5e3e08
ILT
144 // Return the special section index which indicates a small common
145 // symbol. This will return SHN_UNDEF if there are no small common
146 // symbols.
147 elfcpp::Elf_Half
148 small_common_shndx() const
149 { return this->pti_->small_common_shndx; }
150
151 // Return values to add to the section flags for the section holding
152 // small common symbols.
153 elfcpp::Elf_Xword
154 small_common_section_flags() const
155 {
156 gold_assert(this->pti_->small_common_shndx != elfcpp::SHN_UNDEF);
157 return this->pti_->small_common_section_flags;
158 }
159
160 // Return the special section index which indicates a large common
161 // symbol. This will return SHN_UNDEF if there are no large common
162 // symbols.
163 elfcpp::Elf_Half
164 large_common_shndx() const
165 { return this->pti_->large_common_shndx; }
166
167 // Return values to add to the section flags for the section holding
168 // large common symbols.
169 elfcpp::Elf_Xword
170 large_common_section_flags() const
171 {
172 gold_assert(this->pti_->large_common_shndx != elfcpp::SHN_UNDEF);
173 return this->pti_->large_common_section_flags;
174 }
175
176 // This hook is called when an output section is created.
177 void
178 new_output_section(Output_section* os) const
179 { this->do_new_output_section(os); }
180
5a6f7e2d
ILT
181 // This is called to tell the target to complete any sections it is
182 // handling. After this all sections must have their final size.
183 void
7e1edb90
ILT
184 finalize_sections(Layout* layout)
185 { return this->do_finalize_sections(layout); }
5a6f7e2d 186
ab5c9e90
ILT
187 // Return the value to use for a global symbol which needs a special
188 // value in the dynamic symbol table. This will only be called if
189 // the backend first calls symbol->set_needs_dynsym_value().
190 uint64_t
191 dynsym_value(const Symbol* sym) const
192 { return this->do_dynsym_value(sym); }
193
c51e6221
ILT
194 // Return a string to use to fill out a code section. This is
195 // basically one or more NOPS which must fill out the specified
196 // length in bytes.
197 std::string
8851ecca 198 code_fill(section_size_type length) const
c51e6221
ILT
199 { return this->do_code_fill(length); }
200
9a2d6984
ILT
201 // Return whether SYM is known to be defined by the ABI. This is
202 // used to avoid inappropriate warnings about undefined symbols.
203 bool
9c2d0ef9 204 is_defined_by_abi(const Symbol* sym) const
9a2d6984
ILT
205 { return this->do_is_defined_by_abi(sym); }
206
36959681
ILT
207 // Adjust the output file header before it is written out. VIEW
208 // points to the header in external form. LEN is the length.
209 void
210 adjust_elf_header(unsigned char* view, int len) const
211 { return this->do_adjust_elf_header(view, len); }
212
bb04269c
DK
213 // Return whether NAME is a local label name. This is used to implement the
214 // --discard-locals options.
215 bool
216 is_local_label_name(const char* name) const
217 { return this->do_is_local_label_name(name); }
218
f733487b
DK
219 // Make an ELF object.
220 template<int size, bool big_endian>
221 Object*
222 make_elf_object(const std::string& name, Input_file* input_file,
223 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
224 { return this->do_make_elf_object(name, input_file, offset, ehdr); }
225
14bfc3f5 226 protected:
75f65a3e
ILT
227 // This struct holds the constant information for a child class. We
228 // use a struct to avoid the overhead of virtual function calls for
229 // simple information.
230 struct Target_info
231 {
232 // Address size (32 or 64).
233 int size;
234 // Whether the target is big endian.
235 bool is_big_endian;
61ba1cf9
ILT
236 // The code to store in the e_machine field of the ELF header.
237 elfcpp::EM machine_code;
75f65a3e
ILT
238 // Whether this target has a specific make_symbol function.
239 bool has_make_symbol;
240 // Whether this target has a specific resolve function.
241 bool has_resolve;
c51e6221
ILT
242 // Whether this target has a specific code fill function.
243 bool has_code_fill;
35cdfc9a
ILT
244 // Whether an object file with no .note.GNU-stack sections implies
245 // that the stack should be executable.
246 bool is_default_stack_executable;
0864d551
ILT
247 // Prefix character to strip when checking for wrapping.
248 char wrap_char;
dbe717ef
ILT
249 // The default dynamic linker name.
250 const char* dynamic_linker;
75f65a3e 251 // The default text segment address.
0c5e9c22 252 uint64_t default_text_segment_address;
75f65a3e
ILT
253 // The ABI specified page size.
254 uint64_t abi_pagesize;
255 // The common page size used by actual implementations.
256 uint64_t common_pagesize;
8a5e3e08
ILT
257 // The special section index for small common symbols; SHN_UNDEF
258 // if none.
259 elfcpp::Elf_Half small_common_shndx;
260 // The special section index for large common symbols; SHN_UNDEF
261 // if none.
262 elfcpp::Elf_Half large_common_shndx;
263 // Section flags for small common section.
264 elfcpp::Elf_Xword small_common_section_flags;
265 // Section flags for large common section.
266 elfcpp::Elf_Xword large_common_section_flags;
75f65a3e
ILT
267 };
268
269 Target(const Target_info* pti)
270 : pti_(pti)
14bfc3f5
ILT
271 { }
272
8a5e3e08
ILT
273 // Virtual function which may be implemented by the child class.
274 virtual void
275 do_new_output_section(Output_section*) const
276 { }
277
5a6f7e2d
ILT
278 // Virtual function which may be implemented by the child class.
279 virtual void
7e1edb90 280 do_finalize_sections(Layout*)
5a6f7e2d
ILT
281 { }
282
ab5c9e90
ILT
283 // Virtual function which may be implemented by the child class.
284 virtual uint64_t
285 do_dynsym_value(const Symbol*) const
286 { gold_unreachable(); }
287
c51e6221
ILT
288 // Virtual function which must be implemented by the child class if
289 // needed.
290 virtual std::string
8851ecca 291 do_code_fill(section_size_type) const
c51e6221
ILT
292 { gold_unreachable(); }
293
9a2d6984
ILT
294 // Virtual function which may be implemented by the child class.
295 virtual bool
9c2d0ef9 296 do_is_defined_by_abi(const Symbol*) const
9a2d6984
ILT
297 { return false; }
298
36959681
ILT
299 // Adjust the output file header before it is written out. VIEW
300 // points to the header in external form. LEN is the length, and
301 // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
302 // By default, we do nothing.
303 virtual void
304 do_adjust_elf_header(unsigned char*, int) const
305 { }
306
bb04269c
DK
307 // Virtual function which may be overriden by the child class.
308 virtual bool
309 do_is_local_label_name(const char*) const;
310
f733487b
DK
311 // make_elf_object hooks. There are four versions of these for
312 // different address sizes and endianities.
313
314#ifdef HAVE_TARGET_32_LITTLE
315 // Virtual functions which may be overriden by the child class.
316 virtual Object*
317 do_make_elf_object(const std::string&, Input_file*, off_t,
318 const elfcpp::Ehdr<32, false>&);
319#endif
320
321#ifdef HAVE_TARGET_32_BIG
322 // Virtual functions which may be overriden by the child class.
323 virtual Object*
324 do_make_elf_object(const std::string&, Input_file*, off_t,
325 const elfcpp::Ehdr<32, true>&);
326#endif
327
328#ifdef HAVE_TARGET_64_LITTLE
329 // Virtual functions which may be overriden by the child class.
330 virtual Object*
331 do_make_elf_object(const std::string&, Input_file*, off_t,
332 const elfcpp::Ehdr<64, false>& ehdr);
333#endif
334
335#ifdef HAVE_TARGET_64_BIG
336 // Virtual functions which may be overriden by the child class.
337 virtual Object*
338 do_make_elf_object(const std::string& name, Input_file* input_file,
339 off_t offset, const elfcpp::Ehdr<64, true>& ehdr);
340#endif
341
14bfc3f5 342 private:
f733487b
DK
343 // The implementations of the four do_make_elf_object virtual functions are
344 // almost identical except for their sizes and endianity. We use a template.
345 // for their implementations.
346 template<int size, bool big_endian>
347 inline Object*
348 do_make_elf_object_implementation(const std::string&, Input_file*, off_t,
349 const elfcpp::Ehdr<size, big_endian>&);
350
14bfc3f5
ILT
351 Target(const Target&);
352 Target& operator=(const Target&);
353
75f65a3e
ILT
354 // The target information.
355 const Target_info* pti_;
bae7f79e
ILT
356};
357
14bfc3f5
ILT
358// The abstract class for a specific size and endianness of target.
359// Each actual target implementation class should derive from an
360// instantiation of Sized_target.
361
362template<int size, bool big_endian>
363class Sized_target : public Target
364{
365 public:
366 // Make a new symbol table entry for the target. This should be
367 // overridden by a target which needs additional information in the
368 // symbol table. This will only be called if has_make_symbol()
369 // returns true.
370 virtual Sized_symbol<size>*
14b31740 371 make_symbol() const
a3ad94ed 372 { gold_unreachable(); }
14bfc3f5
ILT
373
374 // Resolve a symbol for the target. This should be overridden by a
375 // target which needs to take special action. TO is the
376 // pre-existing symbol. SYM is the new symbol, seen in OBJECT.
14b31740
ILT
377 // VERSION is the version of SYM. This will only be called if
378 // has_resolve() returns true.
14bfc3f5 379 virtual void
14b31740
ILT
380 resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
381 const char*)
a3ad94ed 382 { gold_unreachable(); }
14bfc3f5 383
6d03d481
ST
384 // Process the relocs for a section, and record information of the
385 // mapping from source to destination sections. This mapping is later
386 // used to determine unreferenced garbage sections. This procedure is
387 // only called during garbage collection.
388 virtual void
389 gc_process_relocs(const General_options& options,
390 Symbol_table* symtab,
391 Layout* layout,
392 Sized_relobj<size, big_endian>* object,
393 unsigned int data_shndx,
394 unsigned int sh_type,
395 const unsigned char* prelocs,
396 size_t reloc_count,
397 Output_section* output_section,
398 bool needs_special_offset_handling,
399 size_t local_symbol_count,
400 const unsigned char* plocal_symbols) = 0;
401
92e059d8
ILT
402 // Scan the relocs for a section, and record any information
403 // required for the symbol. OPTIONS is the command line options.
404 // SYMTAB is the symbol table. OBJECT is the object in which the
a3ad94ed
ILT
405 // section appears. DATA_SHNDX is the section index that these
406 // relocs apply to. SH_TYPE is the type of the relocation section,
92e059d8
ILT
407 // SHT_REL or SHT_RELA. PRELOCS points to the relocation data.
408 // RELOC_COUNT is the number of relocs. LOCAL_SYMBOL_COUNT is the
730cdc88
ILT
409 // number of local symbols. OUTPUT_SECTION is the output section.
410 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
411 // sections are not mapped as usual. PLOCAL_SYMBOLS points to the
412 // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of
413 // pointers to the global symbol table from OBJECT.
61ba1cf9 414 virtual void
92e059d8
ILT
415 scan_relocs(const General_options& options,
416 Symbol_table* symtab,
ead1e424 417 Layout* layout,
f6ce93d6 418 Sized_relobj<size, big_endian>* object,
a3ad94ed 419 unsigned int data_shndx,
92e059d8
ILT
420 unsigned int sh_type,
421 const unsigned char* prelocs,
422 size_t reloc_count,
730cdc88
ILT
423 Output_section* output_section,
424 bool needs_special_offset_handling,
92e059d8 425 size_t local_symbol_count,
730cdc88 426 const unsigned char* plocal_symbols) = 0;
92e059d8
ILT
427
428 // Relocate section data. SH_TYPE is the type of the relocation
429 // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation
730cdc88
ILT
430 // information. RELOC_COUNT is the number of relocs.
431 // OUTPUT_SECTION is the output section.
432 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
433 // to correspond to the output section. VIEW is a view into the
434 // output file holding the section contents, VIEW_ADDRESS is the
435 // virtual address of the view, and VIEW_SIZE is the size of the
436 // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
437 // parameters refer to the complete output section data, not just
438 // the input section data.
92e059d8
ILT
439 virtual void
440 relocate_section(const Relocate_info<size, big_endian>*,
441 unsigned int sh_type,
442 const unsigned char* prelocs,
443 size_t reloc_count,
730cdc88
ILT
444 Output_section* output_section,
445 bool needs_special_offset_handling,
92e059d8
ILT
446 unsigned char* view,
447 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
fe8718a4 448 section_size_type view_size) = 0;
61ba1cf9 449
6a74a719
ILT
450 // Scan the relocs during a relocatable link. The parameters are
451 // like scan_relocs, with an additional Relocatable_relocs
452 // parameter, used to record the disposition of the relocs.
453 virtual void
454 scan_relocatable_relocs(const General_options& options,
455 Symbol_table* symtab,
456 Layout* layout,
457 Sized_relobj<size, big_endian>* object,
458 unsigned int data_shndx,
459 unsigned int sh_type,
460 const unsigned char* prelocs,
461 size_t reloc_count,
462 Output_section* output_section,
463 bool needs_special_offset_handling,
464 size_t local_symbol_count,
465 const unsigned char* plocal_symbols,
466 Relocatable_relocs*) = 0;
467
468 // Relocate a section during a relocatable link. The parameters are
469 // like relocate_section, with additional parameters for the view of
470 // the output reloc section.
471 virtual void
472 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
473 unsigned int sh_type,
474 const unsigned char* prelocs,
475 size_t reloc_count,
476 Output_section* output_section,
477 off_t offset_in_output_section,
478 const Relocatable_relocs*,
479 unsigned char* view,
480 typename elfcpp::Elf_types<size>::Elf_Addr
481 view_address,
482 section_size_type view_size,
483 unsigned char* reloc_view,
484 section_size_type reloc_view_size) = 0;
485
14bfc3f5 486 protected:
75f65a3e
ILT
487 Sized_target(const Target::Target_info* pti)
488 : Target(pti)
489 {
a3ad94ed
ILT
490 gold_assert(pti->size == size);
491 gold_assert(pti->is_big_endian ? big_endian : !big_endian);
75f65a3e 492 }
14bfc3f5 493};
bae7f79e
ILT
494
495} // End namespace gold.
496
497#endif // !defined(GOLD_TARGET_H)
This page took 0.156614 seconds and 4 git commands to generate.