daily update
[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"
20e6d0d6 39#include "debug.h"
14bfc3f5 40
bae7f79e
ILT
41namespace gold
42{
43
14bfc3f5 44class Object;
364c7fa5 45class Relobj;
61ba1cf9 46template<int size, bool big_endian>
f6ce93d6 47class Sized_relobj;
6a74a719 48class Relocatable_relocs;
92e059d8 49template<int size, bool big_endian>
730cdc88 50class Relocate_info;
364c7fa5 51class Reloc_symbol_changes;
f6ce93d6
ILT
52class Symbol;
53template<int size>
54class Sized_symbol;
55class Symbol_table;
730cdc88 56class Output_section;
d5b40221 57class Input_objects;
14bfc3f5
ILT
58
59// The abstract class for target specific handling.
60
bae7f79e
ILT
61class Target
62{
63 public:
14bfc3f5
ILT
64 virtual ~Target()
65 { }
66
67 // Return the bit size that this target implements. This should
68 // return 32 or 64.
69 int
70 get_size() const
75f65a3e 71 { return this->pti_->size; }
14bfc3f5
ILT
72
73 // Return whether this target is big-endian.
74 bool
75 is_big_endian() const
75f65a3e 76 { return this->pti_->is_big_endian; }
14bfc3f5 77
61ba1cf9
ILT
78 // Machine code to store in e_machine field of ELF header.
79 elfcpp::EM
80 machine_code() const
81 { return this->pti_->machine_code; }
82
d5b40221
DK
83 // Processor specific flags to store in e_flags field of ELF header.
84 elfcpp::Elf_Word
85 processor_specific_flags() const
86 { return this->processor_specific_flags_; }
87
88 // Whether processor specific flags are set at least once.
89 bool
90 are_processor_specific_flags_set() const
91 { return this->are_processor_specific_flags_set_; }
92
14bfc3f5
ILT
93 // Whether this target has a specific make_symbol function.
94 bool
95 has_make_symbol() const
75f65a3e 96 { return this->pti_->has_make_symbol; }
14bfc3f5
ILT
97
98 // Whether this target has a specific resolve function.
99 bool
100 has_resolve() const
75f65a3e
ILT
101 { return this->pti_->has_resolve; }
102
c51e6221
ILT
103 // Whether this target has a specific code fill function.
104 bool
105 has_code_fill() const
106 { return this->pti_->has_code_fill; }
107
dbe717ef
ILT
108 // Return the default name of the dynamic linker.
109 const char*
110 dynamic_linker() const
111 { return this->pti_->dynamic_linker; }
112
75f65a3e
ILT
113 // Return the default address to use for the text segment.
114 uint64_t
0c5e9c22
ILT
115 default_text_segment_address() const
116 { return this->pti_->default_text_segment_address; }
75f65a3e
ILT
117
118 // Return the ABI specified page size.
119 uint64_t
120 abi_pagesize() const
cd72c291 121 {
8851ecca
ILT
122 if (parameters->options().max_page_size() > 0)
123 return parameters->options().max_page_size();
cd72c291
ILT
124 else
125 return this->pti_->abi_pagesize;
126 }
75f65a3e
ILT
127
128 // Return the common page size used on actual systems.
129 uint64_t
130 common_pagesize() const
cd72c291 131 {
8851ecca
ILT
132 if (parameters->options().common_page_size() > 0)
133 return std::min(parameters->options().common_page_size(),
cd72c291
ILT
134 this->abi_pagesize());
135 else
136 return std::min(this->pti_->common_pagesize,
137 this->abi_pagesize());
138 }
14bfc3f5 139
35cdfc9a
ILT
140 // If we see some object files with .note.GNU-stack sections, and
141 // some objects files without them, this returns whether we should
142 // consider the object files without them to imply that the stack
143 // should be executable.
144 bool
145 is_default_stack_executable() const
146 { return this->pti_->is_default_stack_executable; }
147
0864d551
ILT
148 // Return a character which may appear as a prefix for a wrap
149 // symbol. If this character appears, we strip it when checking for
150 // wrapping and add it back when forming the final symbol name.
151 // This should be '\0' if not special prefix is required, which is
152 // the normal case.
153 char
154 wrap_char() const
155 { return this->pti_->wrap_char; }
156
8a5e3e08
ILT
157 // Return the special section index which indicates a small common
158 // symbol. This will return SHN_UNDEF if there are no small common
159 // symbols.
160 elfcpp::Elf_Half
161 small_common_shndx() const
162 { return this->pti_->small_common_shndx; }
163
164 // Return values to add to the section flags for the section holding
165 // small common symbols.
166 elfcpp::Elf_Xword
167 small_common_section_flags() const
168 {
169 gold_assert(this->pti_->small_common_shndx != elfcpp::SHN_UNDEF);
170 return this->pti_->small_common_section_flags;
171 }
172
173 // Return the special section index which indicates a large common
174 // symbol. This will return SHN_UNDEF if there are no large common
175 // symbols.
176 elfcpp::Elf_Half
177 large_common_shndx() const
178 { return this->pti_->large_common_shndx; }
179
180 // Return values to add to the section flags for the section holding
181 // large common symbols.
182 elfcpp::Elf_Xword
183 large_common_section_flags() const
184 {
185 gold_assert(this->pti_->large_common_shndx != elfcpp::SHN_UNDEF);
186 return this->pti_->large_common_section_flags;
187 }
188
189 // This hook is called when an output section is created.
190 void
191 new_output_section(Output_section* os) const
192 { this->do_new_output_section(os); }
193
5a6f7e2d
ILT
194 // This is called to tell the target to complete any sections it is
195 // handling. After this all sections must have their final size.
196 void
d5b40221
DK
197 finalize_sections(Layout* layout, const Input_objects* input_objects)
198 { return this->do_finalize_sections(layout, input_objects); }
5a6f7e2d 199
ab5c9e90
ILT
200 // Return the value to use for a global symbol which needs a special
201 // value in the dynamic symbol table. This will only be called if
202 // the backend first calls symbol->set_needs_dynsym_value().
203 uint64_t
204 dynsym_value(const Symbol* sym) const
205 { return this->do_dynsym_value(sym); }
206
c51e6221
ILT
207 // Return a string to use to fill out a code section. This is
208 // basically one or more NOPS which must fill out the specified
209 // length in bytes.
210 std::string
8851ecca 211 code_fill(section_size_type length) const
c51e6221
ILT
212 { return this->do_code_fill(length); }
213
9a2d6984
ILT
214 // Return whether SYM is known to be defined by the ABI. This is
215 // used to avoid inappropriate warnings about undefined symbols.
216 bool
9c2d0ef9 217 is_defined_by_abi(const Symbol* sym) const
9a2d6984
ILT
218 { return this->do_is_defined_by_abi(sym); }
219
36959681
ILT
220 // Adjust the output file header before it is written out. VIEW
221 // points to the header in external form. LEN is the length.
222 void
223 adjust_elf_header(unsigned char* view, int len) const
224 { return this->do_adjust_elf_header(view, len); }
225
bb04269c
DK
226 // Return whether NAME is a local label name. This is used to implement the
227 // --discard-locals options.
228 bool
229 is_local_label_name(const char* name) const
230 { return this->do_is_local_label_name(name); }
231
364c7fa5
ILT
232 // A function starts at OFFSET in section SHNDX in OBJECT. That
233 // function was compiled with -fsplit-stack, but it refers to a
234 // function which was compiled without -fsplit-stack. VIEW is a
235 // modifiable view of the section; VIEW_SIZE is the size of the
236 // view. The target has to adjust the function so that it allocates
237 // enough stack.
238 void
239 calls_non_split(Relobj* object, unsigned int shndx,
240 section_offset_type fnoffset, section_size_type fnsize,
241 unsigned char* view, section_size_type view_size,
242 std::string* from, std::string* to) const
243 {
244 this->do_calls_non_split(object, shndx, fnoffset, fnsize, view, view_size,
245 from, to);
246 }
247
f733487b
DK
248 // Make an ELF object.
249 template<int size, bool big_endian>
250 Object*
251 make_elf_object(const std::string& name, Input_file* input_file,
252 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
253 { return this->do_make_elf_object(name, input_file, offset, ehdr); }
254
c0a62865
DK
255 // Make an output section.
256 Output_section*
257 make_output_section(const char* name, elfcpp::Elf_Word type,
258 elfcpp::Elf_Xword flags)
259 { return this->do_make_output_section(name, type, flags); }
260
20e6d0d6
DK
261 // Return true if target wants to perform relaxation.
262 bool
263 may_relax() const
264 {
265 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
266 if (is_debugging_enabled(DEBUG_RELAXATION))
267 return true;
268
269 return this->do_may_relax();
270 }
271
272 // Perform a relaxation pass. Return true if layout may be changed.
273 bool
c0a62865
DK
274 relax(int pass, const Input_objects* input_objects, Symbol_table* symtab,
275 Layout* layout)
20e6d0d6
DK
276 {
277 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
278 if (is_debugging_enabled(DEBUG_RELAXATION))
279 return pass < 2;
280
c0a62865
DK
281 return this->do_relax(pass, input_objects, symtab, layout);
282 }
20e6d0d6 283
14bfc3f5 284 protected:
75f65a3e
ILT
285 // This struct holds the constant information for a child class. We
286 // use a struct to avoid the overhead of virtual function calls for
287 // simple information.
288 struct Target_info
289 {
290 // Address size (32 or 64).
291 int size;
292 // Whether the target is big endian.
293 bool is_big_endian;
61ba1cf9
ILT
294 // The code to store in the e_machine field of the ELF header.
295 elfcpp::EM machine_code;
75f65a3e
ILT
296 // Whether this target has a specific make_symbol function.
297 bool has_make_symbol;
298 // Whether this target has a specific resolve function.
299 bool has_resolve;
c51e6221
ILT
300 // Whether this target has a specific code fill function.
301 bool has_code_fill;
35cdfc9a
ILT
302 // Whether an object file with no .note.GNU-stack sections implies
303 // that the stack should be executable.
304 bool is_default_stack_executable;
0864d551
ILT
305 // Prefix character to strip when checking for wrapping.
306 char wrap_char;
dbe717ef
ILT
307 // The default dynamic linker name.
308 const char* dynamic_linker;
75f65a3e 309 // The default text segment address.
0c5e9c22 310 uint64_t default_text_segment_address;
75f65a3e
ILT
311 // The ABI specified page size.
312 uint64_t abi_pagesize;
313 // The common page size used by actual implementations.
314 uint64_t common_pagesize;
8a5e3e08
ILT
315 // The special section index for small common symbols; SHN_UNDEF
316 // if none.
317 elfcpp::Elf_Half small_common_shndx;
318 // The special section index for large common symbols; SHN_UNDEF
319 // if none.
320 elfcpp::Elf_Half large_common_shndx;
321 // Section flags for small common section.
322 elfcpp::Elf_Xword small_common_section_flags;
323 // Section flags for large common section.
324 elfcpp::Elf_Xword large_common_section_flags;
75f65a3e
ILT
325 };
326
327 Target(const Target_info* pti)
d5b40221
DK
328 : pti_(pti), processor_specific_flags_(0),
329 are_processor_specific_flags_set_(false)
14bfc3f5
ILT
330 { }
331
8a5e3e08
ILT
332 // Virtual function which may be implemented by the child class.
333 virtual void
334 do_new_output_section(Output_section*) const
335 { }
336
5a6f7e2d
ILT
337 // Virtual function which may be implemented by the child class.
338 virtual void
d5b40221 339 do_finalize_sections(Layout*, const Input_objects*)
5a6f7e2d
ILT
340 { }
341
ab5c9e90
ILT
342 // Virtual function which may be implemented by the child class.
343 virtual uint64_t
344 do_dynsym_value(const Symbol*) const
345 { gold_unreachable(); }
346
c51e6221
ILT
347 // Virtual function which must be implemented by the child class if
348 // needed.
349 virtual std::string
8851ecca 350 do_code_fill(section_size_type) const
c51e6221
ILT
351 { gold_unreachable(); }
352
9a2d6984
ILT
353 // Virtual function which may be implemented by the child class.
354 virtual bool
9c2d0ef9 355 do_is_defined_by_abi(const Symbol*) const
9a2d6984
ILT
356 { return false; }
357
36959681
ILT
358 // Adjust the output file header before it is written out. VIEW
359 // points to the header in external form. LEN is the length, and
360 // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
361 // By default, we do nothing.
362 virtual void
363 do_adjust_elf_header(unsigned char*, int) const
364 { }
365
bb04269c
DK
366 // Virtual function which may be overriden by the child class.
367 virtual bool
368 do_is_local_label_name(const char*) const;
369
364c7fa5
ILT
370 // Virtual function which may be overridden by the child class.
371 virtual void
372 do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
373 section_size_type, unsigned char*, section_size_type,
374 std::string*, std::string*) const;
375
f733487b
DK
376 // make_elf_object hooks. There are four versions of these for
377 // different address sizes and endianities.
364c7fa5 378
d5b40221
DK
379 // Set processor specific flags.
380 void
381 set_processor_specific_flags(elfcpp::Elf_Word flags)
382 {
383 this->processor_specific_flags_ = flags;
384 this->are_processor_specific_flags_set_ = true;
385 }
386
f733487b
DK
387#ifdef HAVE_TARGET_32_LITTLE
388 // Virtual functions which may be overriden by the child class.
389 virtual Object*
390 do_make_elf_object(const std::string&, Input_file*, off_t,
391 const elfcpp::Ehdr<32, false>&);
392#endif
393
394#ifdef HAVE_TARGET_32_BIG
395 // Virtual functions which may be overriden by the child class.
396 virtual Object*
397 do_make_elf_object(const std::string&, Input_file*, off_t,
398 const elfcpp::Ehdr<32, true>&);
399#endif
400
401#ifdef HAVE_TARGET_64_LITTLE
402 // Virtual functions which may be overriden by the child class.
403 virtual Object*
404 do_make_elf_object(const std::string&, Input_file*, off_t,
405 const elfcpp::Ehdr<64, false>& ehdr);
406#endif
407
408#ifdef HAVE_TARGET_64_BIG
409 // Virtual functions which may be overriden by the child class.
410 virtual Object*
411 do_make_elf_object(const std::string& name, Input_file* input_file,
412 off_t offset, const elfcpp::Ehdr<64, true>& ehdr);
413#endif
414
c0a62865
DK
415 // Virtual functions which may be overriden by the child class.
416 virtual Output_section*
417 do_make_output_section(const char* name, elfcpp::Elf_Word type,
418 elfcpp::Elf_Xword flags);
419
20e6d0d6
DK
420 // Virtual function which may be overriden by the child class.
421 virtual bool
422 do_may_relax() const
423 { return parameters->options().relax(); }
424
425 // Virtual function which may be overriden by the child class.
426 virtual bool
c0a62865 427 do_relax(int, const Input_objects*, Symbol_table*, Layout*)
20e6d0d6
DK
428 { return false; }
429
364c7fa5
ILT
430 // A function for targets to call. Return whether BYTES/LEN matches
431 // VIEW/VIEW_SIZE at OFFSET.
432 bool
433 match_view(const unsigned char* view, section_size_type view_size,
434 section_offset_type offset, const char* bytes, size_t len) const;
435
436 // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
437 // for LEN bytes.
438 void
439 set_view_to_nop(unsigned char* view, section_size_type view_size,
440 section_offset_type offset, size_t len) const;
441
14bfc3f5 442 private:
f733487b
DK
443 // The implementations of the four do_make_elf_object virtual functions are
444 // almost identical except for their sizes and endianity. We use a template.
445 // for their implementations.
446 template<int size, bool big_endian>
447 inline Object*
448 do_make_elf_object_implementation(const std::string&, Input_file*, off_t,
449 const elfcpp::Ehdr<size, big_endian>&);
450
14bfc3f5
ILT
451 Target(const Target&);
452 Target& operator=(const Target&);
453
75f65a3e
ILT
454 // The target information.
455 const Target_info* pti_;
d5b40221
DK
456 // Processor-specific flags.
457 elfcpp::Elf_Word processor_specific_flags_;
458 // Whether the processor-specific flags are set at least once.
459 bool are_processor_specific_flags_set_;
bae7f79e
ILT
460};
461
14bfc3f5
ILT
462// The abstract class for a specific size and endianness of target.
463// Each actual target implementation class should derive from an
464// instantiation of Sized_target.
465
466template<int size, bool big_endian>
467class Sized_target : public Target
468{
469 public:
470 // Make a new symbol table entry for the target. This should be
471 // overridden by a target which needs additional information in the
472 // symbol table. This will only be called if has_make_symbol()
473 // returns true.
474 virtual Sized_symbol<size>*
14b31740 475 make_symbol() const
a3ad94ed 476 { gold_unreachable(); }
14bfc3f5
ILT
477
478 // Resolve a symbol for the target. This should be overridden by a
479 // target which needs to take special action. TO is the
480 // pre-existing symbol. SYM is the new symbol, seen in OBJECT.
14b31740
ILT
481 // VERSION is the version of SYM. This will only be called if
482 // has_resolve() returns true.
14bfc3f5 483 virtual void
14b31740
ILT
484 resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
485 const char*)
a3ad94ed 486 { gold_unreachable(); }
14bfc3f5 487
6d03d481
ST
488 // Process the relocs for a section, and record information of the
489 // mapping from source to destination sections. This mapping is later
490 // used to determine unreferenced garbage sections. This procedure is
491 // only called during garbage collection.
492 virtual void
ad0f2072
ILT
493 gc_process_relocs(Symbol_table* symtab,
494 Layout* layout,
495 Sized_relobj<size, big_endian>* object,
496 unsigned int data_shndx,
497 unsigned int sh_type,
498 const unsigned char* prelocs,
499 size_t reloc_count,
500 Output_section* output_section,
501 bool needs_special_offset_handling,
502 size_t local_symbol_count,
503 const unsigned char* plocal_symbols) = 0;
6d03d481 504
92e059d8 505 // Scan the relocs for a section, and record any information
ad0f2072
ILT
506 // required for the symbol. SYMTAB is the symbol table. OBJECT is
507 // the object in which the section appears. DATA_SHNDX is the
508 // section index that these relocs apply to. SH_TYPE is the type of
509 // the relocation section, SHT_REL or SHT_RELA. PRELOCS points to
510 // the relocation data. RELOC_COUNT is the number of relocs.
511 // LOCAL_SYMBOL_COUNT is the number of local symbols.
512 // OUTPUT_SECTION is the output section.
730cdc88
ILT
513 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
514 // sections are not mapped as usual. PLOCAL_SYMBOLS points to the
515 // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of
516 // pointers to the global symbol table from OBJECT.
61ba1cf9 517 virtual void
ad0f2072 518 scan_relocs(Symbol_table* symtab,
ead1e424 519 Layout* layout,
f6ce93d6 520 Sized_relobj<size, big_endian>* object,
a3ad94ed 521 unsigned int data_shndx,
92e059d8
ILT
522 unsigned int sh_type,
523 const unsigned char* prelocs,
524 size_t reloc_count,
730cdc88
ILT
525 Output_section* output_section,
526 bool needs_special_offset_handling,
92e059d8 527 size_t local_symbol_count,
730cdc88 528 const unsigned char* plocal_symbols) = 0;
92e059d8
ILT
529
530 // Relocate section data. SH_TYPE is the type of the relocation
531 // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation
730cdc88
ILT
532 // information. RELOC_COUNT is the number of relocs.
533 // OUTPUT_SECTION is the output section.
534 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
535 // to correspond to the output section. VIEW is a view into the
536 // output file holding the section contents, VIEW_ADDRESS is the
537 // virtual address of the view, and VIEW_SIZE is the size of the
538 // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
539 // parameters refer to the complete output section data, not just
540 // the input section data.
92e059d8
ILT
541 virtual void
542 relocate_section(const Relocate_info<size, big_endian>*,
543 unsigned int sh_type,
544 const unsigned char* prelocs,
545 size_t reloc_count,
730cdc88
ILT
546 Output_section* output_section,
547 bool needs_special_offset_handling,
92e059d8
ILT
548 unsigned char* view,
549 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
364c7fa5
ILT
550 section_size_type view_size,
551 const Reloc_symbol_changes*) = 0;
61ba1cf9 552
6a74a719
ILT
553 // Scan the relocs during a relocatable link. The parameters are
554 // like scan_relocs, with an additional Relocatable_relocs
555 // parameter, used to record the disposition of the relocs.
556 virtual void
ad0f2072 557 scan_relocatable_relocs(Symbol_table* symtab,
6a74a719
ILT
558 Layout* layout,
559 Sized_relobj<size, big_endian>* object,
560 unsigned int data_shndx,
561 unsigned int sh_type,
562 const unsigned char* prelocs,
563 size_t reloc_count,
564 Output_section* output_section,
565 bool needs_special_offset_handling,
566 size_t local_symbol_count,
567 const unsigned char* plocal_symbols,
568 Relocatable_relocs*) = 0;
569
570 // Relocate a section during a relocatable link. The parameters are
571 // like relocate_section, with additional parameters for the view of
572 // the output reloc section.
573 virtual void
574 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
575 unsigned int sh_type,
576 const unsigned char* prelocs,
577 size_t reloc_count,
578 Output_section* output_section,
579 off_t offset_in_output_section,
580 const Relocatable_relocs*,
581 unsigned char* view,
582 typename elfcpp::Elf_types<size>::Elf_Addr
583 view_address,
584 section_size_type view_size,
585 unsigned char* reloc_view,
586 section_size_type reloc_view_size) = 0;
587
14bfc3f5 588 protected:
75f65a3e
ILT
589 Sized_target(const Target::Target_info* pti)
590 : Target(pti)
591 {
a3ad94ed
ILT
592 gold_assert(pti->size == size);
593 gold_assert(pti->is_big_endian ? big_endian : !big_endian);
75f65a3e 594 }
14bfc3f5 595};
bae7f79e
ILT
596
597} // End namespace gold.
598
599#endif // !defined(GOLD_TARGET_H)
This page took 0.186346 seconds and 4 git commands to generate.