Replace VEX.DNS with VEX.NDS in comments.
[binutils.git] / gold / target.h
blob88cc973e6b7c81f21730858440a9c491625776e1
1 // target.h -- target support for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 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.
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.
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.
33 #ifndef GOLD_TARGET_H
34 #define GOLD_TARGET_H
36 #include "elfcpp.h"
37 #include "options.h"
38 #include "parameters.h"
39 #include "debug.h"
41 namespace gold
44 class Object;
45 class Relobj;
46 template<int size, bool big_endian>
47 class Sized_relobj;
48 class Relocatable_relocs;
49 template<int size, bool big_endian>
50 class Relocate_info;
51 class Reloc_symbol_changes;
52 class Symbol;
53 template<int size>
54 class Sized_symbol;
55 class Symbol_table;
56 class Output_section;
57 class Input_objects;
59 // The abstract class for target specific handling.
61 class Target
63 public:
64 virtual ~Target()
65 { }
67 // Return the bit size that this target implements. This should
68 // return 32 or 64.
69 int
70 get_size() const
71 { return this->pti_->size; }
73 // Return whether this target is big-endian.
74 bool
75 is_big_endian() const
76 { return this->pti_->is_big_endian; }
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; }
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_; }
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_; }
93 // Whether this target has a specific make_symbol function.
94 bool
95 has_make_symbol() const
96 { return this->pti_->has_make_symbol; }
98 // Whether this target has a specific resolve function.
99 bool
100 has_resolve() const
101 { return this->pti_->has_resolve; }
103 // Whether this target has a specific code fill function.
104 bool
105 has_code_fill() const
106 { return this->pti_->has_code_fill; }
108 // Return the default name of the dynamic linker.
109 const char*
110 dynamic_linker() const
111 { return this->pti_->dynamic_linker; }
113 // Return the default address to use for the text segment.
114 uint64_t
115 default_text_segment_address() const
116 { return this->pti_->default_text_segment_address; }
118 // Return the ABI specified page size.
119 uint64_t
120 abi_pagesize() const
122 if (parameters->options().max_page_size() > 0)
123 return parameters->options().max_page_size();
124 else
125 return this->pti_->abi_pagesize;
128 // Return the common page size used on actual systems.
129 uint64_t
130 common_pagesize() const
132 if (parameters->options().common_page_size() > 0)
133 return std::min(parameters->options().common_page_size(),
134 this->abi_pagesize());
135 else
136 return std::min(this->pti_->common_pagesize,
137 this->abi_pagesize());
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; }
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; }
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; }
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
169 gold_assert(this->pti_->small_common_shndx != elfcpp::SHN_UNDEF);
170 return this->pti_->small_common_section_flags;
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; }
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
185 gold_assert(this->pti_->large_common_shndx != elfcpp::SHN_UNDEF);
186 return this->pti_->large_common_section_flags;
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); }
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
197 finalize_sections(Layout* layout, const Input_objects* input_objects,
198 Symbol_table* symtab)
199 { return this->do_finalize_sections(layout, input_objects, symtab); }
201 // Return the value to use for a global symbol which needs a special
202 // value in the dynamic symbol table. This will only be called if
203 // the backend first calls symbol->set_needs_dynsym_value().
204 uint64_t
205 dynsym_value(const Symbol* sym) const
206 { return this->do_dynsym_value(sym); }
208 // Return a string to use to fill out a code section. This is
209 // basically one or more NOPS which must fill out the specified
210 // length in bytes.
211 std::string
212 code_fill(section_size_type length) const
213 { return this->do_code_fill(length); }
215 // Return whether SYM is known to be defined by the ABI. This is
216 // used to avoid inappropriate warnings about undefined symbols.
217 bool
218 is_defined_by_abi(const Symbol* sym) const
219 { return this->do_is_defined_by_abi(sym); }
221 // Adjust the output file header before it is written out. VIEW
222 // points to the header in external form. LEN is the length.
223 void
224 adjust_elf_header(unsigned char* view, int len) const
225 { return this->do_adjust_elf_header(view, len); }
227 // Return whether NAME is a local label name. This is used to implement the
228 // --discard-locals options.
229 bool
230 is_local_label_name(const char* name) const
231 { return this->do_is_local_label_name(name); }
233 // Get the symbol index to use for a target specific reloc.
234 unsigned int
235 reloc_symbol_index(void* arg, unsigned int type) const
236 { return this->do_reloc_symbol_index(arg, type); }
238 // Get the addend to use for a target specific reloc.
239 uint64_t
240 reloc_addend(void* arg, unsigned int type, uint64_t addend) const
241 { return this->do_reloc_addend(arg, type, addend); }
243 // A function starts at OFFSET in section SHNDX in OBJECT. That
244 // function was compiled with -fsplit-stack, but it refers to a
245 // function which was compiled without -fsplit-stack. VIEW is a
246 // modifiable view of the section; VIEW_SIZE is the size of the
247 // view. The target has to adjust the function so that it allocates
248 // enough stack.
249 void
250 calls_non_split(Relobj* object, unsigned int shndx,
251 section_offset_type fnoffset, section_size_type fnsize,
252 unsigned char* view, section_size_type view_size,
253 std::string* from, std::string* to) const
255 this->do_calls_non_split(object, shndx, fnoffset, fnsize, view, view_size,
256 from, to);
259 // Make an ELF object.
260 template<int size, bool big_endian>
261 Object*
262 make_elf_object(const std::string& name, Input_file* input_file,
263 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
264 { return this->do_make_elf_object(name, input_file, offset, ehdr); }
266 // Make an output section.
267 Output_section*
268 make_output_section(const char* name, elfcpp::Elf_Word type,
269 elfcpp::Elf_Xword flags)
270 { return this->do_make_output_section(name, type, flags); }
272 // Return true if target wants to perform relaxation.
273 bool
274 may_relax() const
276 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
277 if (is_debugging_enabled(DEBUG_RELAXATION))
278 return true;
280 return this->do_may_relax();
283 // Perform a relaxation pass. Return true if layout may be changed.
284 bool
285 relax(int pass, const Input_objects* input_objects, Symbol_table* symtab,
286 Layout* layout)
288 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
289 if (is_debugging_enabled(DEBUG_RELAXATION))
290 return pass < 2;
292 return this->do_relax(pass, input_objects, symtab, layout);
295 // Return the target-specific name of attributes section. This is
296 // NULL if a target does not use attributes section or if it uses
297 // the default section name ".gnu.attributes".
298 const char*
299 attributes_section() const
300 { return this->pti_->attributes_section; }
302 // Return the vendor name of vendor attributes.
303 const char*
304 attributes_vendor() const
305 { return this->pti_->attributes_vendor; }
307 // Whether a section called NAME is an attribute section.
308 bool
309 is_attributes_section(const char* name) const
311 return ((this->pti_->attributes_section != NULL
312 && strcmp(name, this->pti_->attributes_section) == 0)
313 || strcmp(name, ".gnu.attributes") == 0);
316 // Return a bit mask of argument types for attribute with TAG.
318 attribute_arg_type(int tag) const
319 { return this->do_attribute_arg_type(tag); }
321 // Return the attribute tag of the position NUM in the list of fixed
322 // attributes. Normally there is no reordering and
323 // attributes_order(NUM) == NUM.
325 attributes_order(int num) const
326 { return this->do_attributes_order(num); }
328 protected:
329 // This struct holds the constant information for a child class. We
330 // use a struct to avoid the overhead of virtual function calls for
331 // simple information.
332 struct Target_info
334 // Address size (32 or 64).
335 int size;
336 // Whether the target is big endian.
337 bool is_big_endian;
338 // The code to store in the e_machine field of the ELF header.
339 elfcpp::EM machine_code;
340 // Whether this target has a specific make_symbol function.
341 bool has_make_symbol;
342 // Whether this target has a specific resolve function.
343 bool has_resolve;
344 // Whether this target has a specific code fill function.
345 bool has_code_fill;
346 // Whether an object file with no .note.GNU-stack sections implies
347 // that the stack should be executable.
348 bool is_default_stack_executable;
349 // Prefix character to strip when checking for wrapping.
350 char wrap_char;
351 // The default dynamic linker name.
352 const char* dynamic_linker;
353 // The default text segment address.
354 uint64_t default_text_segment_address;
355 // The ABI specified page size.
356 uint64_t abi_pagesize;
357 // The common page size used by actual implementations.
358 uint64_t common_pagesize;
359 // The special section index for small common symbols; SHN_UNDEF
360 // if none.
361 elfcpp::Elf_Half small_common_shndx;
362 // The special section index for large common symbols; SHN_UNDEF
363 // if none.
364 elfcpp::Elf_Half large_common_shndx;
365 // Section flags for small common section.
366 elfcpp::Elf_Xword small_common_section_flags;
367 // Section flags for large common section.
368 elfcpp::Elf_Xword large_common_section_flags;
369 // Name of attributes section if it is not ".gnu.attributes".
370 const char* attributes_section;
371 // Vendor name of vendor attributes.
372 const char* attributes_vendor;
375 Target(const Target_info* pti)
376 : pti_(pti), processor_specific_flags_(0),
377 are_processor_specific_flags_set_(false)
380 // Virtual function which may be implemented by the child class.
381 virtual void
382 do_new_output_section(Output_section*) const
385 // Virtual function which may be implemented by the child class.
386 virtual void
387 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*)
390 // Virtual function which may be implemented by the child class.
391 virtual uint64_t
392 do_dynsym_value(const Symbol*) const
393 { gold_unreachable(); }
395 // Virtual function which must be implemented by the child class if
396 // needed.
397 virtual std::string
398 do_code_fill(section_size_type) const
399 { gold_unreachable(); }
401 // Virtual function which may be implemented by the child class.
402 virtual bool
403 do_is_defined_by_abi(const Symbol*) const
404 { return false; }
406 // Adjust the output file header before it is written out. VIEW
407 // points to the header in external form. LEN is the length, and
408 // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
409 // By default, we do nothing.
410 virtual void
411 do_adjust_elf_header(unsigned char*, int) const
414 // Virtual function which may be overriden by the child class.
415 virtual bool
416 do_is_local_label_name(const char*) const;
418 // Virtual function that must be overridden by a target which uses
419 // target specific relocations.
420 virtual unsigned int
421 do_reloc_symbol_index(void*, unsigned int) const
422 { gold_unreachable(); }
424 // Virtual function that must be overidden by a target which uses
425 // target specific relocations.
426 virtual uint64_t
427 do_reloc_addend(void*, unsigned int, uint64_t) const
428 { gold_unreachable(); }
430 // Virtual function which may be overridden by the child class.
431 virtual void
432 do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
433 section_size_type, unsigned char*, section_size_type,
434 std::string*, std::string*) const;
436 // make_elf_object hooks. There are four versions of these for
437 // different address sizes and endianities.
439 // Set processor specific flags.
440 void
441 set_processor_specific_flags(elfcpp::Elf_Word flags)
443 this->processor_specific_flags_ = flags;
444 this->are_processor_specific_flags_set_ = true;
447 #ifdef HAVE_TARGET_32_LITTLE
448 // Virtual functions which may be overriden by the child class.
449 virtual Object*
450 do_make_elf_object(const std::string&, Input_file*, off_t,
451 const elfcpp::Ehdr<32, false>&);
452 #endif
454 #ifdef HAVE_TARGET_32_BIG
455 // Virtual functions which may be overriden by the child class.
456 virtual Object*
457 do_make_elf_object(const std::string&, Input_file*, off_t,
458 const elfcpp::Ehdr<32, true>&);
459 #endif
461 #ifdef HAVE_TARGET_64_LITTLE
462 // Virtual functions which may be overriden by the child class.
463 virtual Object*
464 do_make_elf_object(const std::string&, Input_file*, off_t,
465 const elfcpp::Ehdr<64, false>& ehdr);
466 #endif
468 #ifdef HAVE_TARGET_64_BIG
469 // Virtual functions which may be overriden by the child class.
470 virtual Object*
471 do_make_elf_object(const std::string& name, Input_file* input_file,
472 off_t offset, const elfcpp::Ehdr<64, true>& ehdr);
473 #endif
475 // Virtual functions which may be overriden by the child class.
476 virtual Output_section*
477 do_make_output_section(const char* name, elfcpp::Elf_Word type,
478 elfcpp::Elf_Xword flags);
480 // Virtual function which may be overriden by the child class.
481 virtual bool
482 do_may_relax() const
483 { return parameters->options().relax(); }
485 // Virtual function which may be overriden by the child class.
486 virtual bool
487 do_relax(int, const Input_objects*, Symbol_table*, Layout*)
488 { return false; }
490 // A function for targets to call. Return whether BYTES/LEN matches
491 // VIEW/VIEW_SIZE at OFFSET.
492 bool
493 match_view(const unsigned char* view, section_size_type view_size,
494 section_offset_type offset, const char* bytes, size_t len) const;
496 // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
497 // for LEN bytes.
498 void
499 set_view_to_nop(unsigned char* view, section_size_type view_size,
500 section_offset_type offset, size_t len) const;
502 // This must be overriden by the child class if it has target-specific
503 // attributes subsection in the attribute section.
504 virtual int
505 do_attribute_arg_type(int) const
506 { gold_unreachable(); }
508 // This may be overridden by the child class.
509 virtual int
510 do_attributes_order(int num) const
511 { return num; }
513 private:
514 // The implementations of the four do_make_elf_object virtual functions are
515 // almost identical except for their sizes and endianity. We use a template.
516 // for their implementations.
517 template<int size, bool big_endian>
518 inline Object*
519 do_make_elf_object_implementation(const std::string&, Input_file*, off_t,
520 const elfcpp::Ehdr<size, big_endian>&);
522 Target(const Target&);
523 Target& operator=(const Target&);
525 // The target information.
526 const Target_info* pti_;
527 // Processor-specific flags.
528 elfcpp::Elf_Word processor_specific_flags_;
529 // Whether the processor-specific flags are set at least once.
530 bool are_processor_specific_flags_set_;
533 // The abstract class for a specific size and endianness of target.
534 // Each actual target implementation class should derive from an
535 // instantiation of Sized_target.
537 template<int size, bool big_endian>
538 class Sized_target : public Target
540 public:
541 // Make a new symbol table entry for the target. This should be
542 // overridden by a target which needs additional information in the
543 // symbol table. This will only be called if has_make_symbol()
544 // returns true.
545 virtual Sized_symbol<size>*
546 make_symbol() const
547 { gold_unreachable(); }
549 // Resolve a symbol for the target. This should be overridden by a
550 // target which needs to take special action. TO is the
551 // pre-existing symbol. SYM is the new symbol, seen in OBJECT.
552 // VERSION is the version of SYM. This will only be called if
553 // has_resolve() returns true.
554 virtual void
555 resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
556 const char*)
557 { gold_unreachable(); }
559 // Process the relocs for a section, and record information of the
560 // mapping from source to destination sections. This mapping is later
561 // used to determine unreferenced garbage sections. This procedure is
562 // only called during garbage collection.
563 virtual void
564 gc_process_relocs(Symbol_table* symtab,
565 Layout* layout,
566 Sized_relobj<size, big_endian>* object,
567 unsigned int data_shndx,
568 unsigned int sh_type,
569 const unsigned char* prelocs,
570 size_t reloc_count,
571 Output_section* output_section,
572 bool needs_special_offset_handling,
573 size_t local_symbol_count,
574 const unsigned char* plocal_symbols) = 0;
576 // Scan the relocs for a section, and record any information
577 // required for the symbol. SYMTAB is the symbol table. OBJECT is
578 // the object in which the section appears. DATA_SHNDX is the
579 // section index that these relocs apply to. SH_TYPE is the type of
580 // the relocation section, SHT_REL or SHT_RELA. PRELOCS points to
581 // the relocation data. RELOC_COUNT is the number of relocs.
582 // LOCAL_SYMBOL_COUNT is the number of local symbols.
583 // OUTPUT_SECTION is the output section.
584 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
585 // sections are not mapped as usual. PLOCAL_SYMBOLS points to the
586 // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of
587 // pointers to the global symbol table from OBJECT.
588 virtual void
589 scan_relocs(Symbol_table* symtab,
590 Layout* layout,
591 Sized_relobj<size, big_endian>* object,
592 unsigned int data_shndx,
593 unsigned int sh_type,
594 const unsigned char* prelocs,
595 size_t reloc_count,
596 Output_section* output_section,
597 bool needs_special_offset_handling,
598 size_t local_symbol_count,
599 const unsigned char* plocal_symbols) = 0;
601 // Relocate section data. SH_TYPE is the type of the relocation
602 // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation
603 // information. RELOC_COUNT is the number of relocs.
604 // OUTPUT_SECTION is the output section.
605 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
606 // to correspond to the output section. VIEW is a view into the
607 // output file holding the section contents, VIEW_ADDRESS is the
608 // virtual address of the view, and VIEW_SIZE is the size of the
609 // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
610 // parameters refer to the complete output section data, not just
611 // the input section data.
612 virtual void
613 relocate_section(const Relocate_info<size, big_endian>*,
614 unsigned int sh_type,
615 const unsigned char* prelocs,
616 size_t reloc_count,
617 Output_section* output_section,
618 bool needs_special_offset_handling,
619 unsigned char* view,
620 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
621 section_size_type view_size,
622 const Reloc_symbol_changes*) = 0;
624 // Scan the relocs during a relocatable link. The parameters are
625 // like scan_relocs, with an additional Relocatable_relocs
626 // parameter, used to record the disposition of the relocs.
627 virtual void
628 scan_relocatable_relocs(Symbol_table* symtab,
629 Layout* layout,
630 Sized_relobj<size, big_endian>* object,
631 unsigned int data_shndx,
632 unsigned int sh_type,
633 const unsigned char* prelocs,
634 size_t reloc_count,
635 Output_section* output_section,
636 bool needs_special_offset_handling,
637 size_t local_symbol_count,
638 const unsigned char* plocal_symbols,
639 Relocatable_relocs*) = 0;
641 // Relocate a section during a relocatable link. The parameters are
642 // like relocate_section, with additional parameters for the view of
643 // the output reloc section.
644 virtual void
645 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
646 unsigned int sh_type,
647 const unsigned char* prelocs,
648 size_t reloc_count,
649 Output_section* output_section,
650 off_t offset_in_output_section,
651 const Relocatable_relocs*,
652 unsigned char* view,
653 typename elfcpp::Elf_types<size>::Elf_Addr
654 view_address,
655 section_size_type view_size,
656 unsigned char* reloc_view,
657 section_size_type reloc_view_size) = 0;
659 protected:
660 Sized_target(const Target::Target_info* pti)
661 : Target(pti)
663 gold_assert(pti->size == size);
664 gold_assert(pti->is_big_endian ? big_endian : !big_endian);
668 } // End namespace gold.
670 #endif // !defined(GOLD_TARGET_H)