Automatic date update in version.in
[binutils-gdb.git] / gold / target.h
blobcf6d902310f9f5e22d2826fda37c87b0996ddb28
1 // target.h -- target support for gold -*- C++ -*-
3 // Copyright (C) 2006-2017 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 "stringpool.h"
40 #include "debug.h"
42 namespace gold
45 class Object;
46 class Relobj;
47 template<int size, bool big_endian>
48 class Sized_relobj;
49 template<int size, bool big_endian>
50 class Sized_relobj_file;
51 class Relocatable_relocs;
52 template<int size, bool big_endian>
53 struct Relocate_info;
54 class Reloc_symbol_changes;
55 class Symbol;
56 template<int size>
57 class Sized_symbol;
58 class Symbol_table;
59 class Output_data;
60 class Output_data_got_base;
61 class Output_section;
62 class Input_objects;
63 class Task;
64 struct Symbol_location;
65 class Versions;
67 // The abstract class for target specific handling.
69 class Target
71 public:
72 virtual ~Target()
73 { }
75 // Return the bit size that this target implements. This should
76 // return 32 or 64.
77 int
78 get_size() const
79 { return this->pti_->size; }
81 // Return whether this target is big-endian.
82 bool
83 is_big_endian() const
84 { return this->pti_->is_big_endian; }
86 // Machine code to store in e_machine field of ELF header.
87 elfcpp::EM
88 machine_code() const
89 { return this->pti_->machine_code; }
91 // Processor specific flags to store in e_flags field of ELF header.
92 elfcpp::Elf_Word
93 processor_specific_flags() const
94 { return this->processor_specific_flags_; }
96 // Whether processor specific flags are set at least once.
97 bool
98 are_processor_specific_flags_set() const
99 { return this->are_processor_specific_flags_set_; }
101 // Whether this target has a specific make_symbol function.
102 bool
103 has_make_symbol() const
104 { return this->pti_->has_make_symbol; }
106 // Whether this target has a specific resolve function.
107 bool
108 has_resolve() const
109 { return this->pti_->has_resolve; }
111 // Whether this target has a specific code fill function.
112 bool
113 has_code_fill() const
114 { return this->pti_->has_code_fill; }
116 // Return the default name of the dynamic linker.
117 const char*
118 dynamic_linker() const
119 { return this->pti_->dynamic_linker; }
121 // Return the default address to use for the text segment.
122 // If a -z max-page-size argument has set the ABI page size
123 // to a value larger than the default starting address,
124 // bump the starting address up to the page size, to avoid
125 // misaligning the text segment in the file.
126 uint64_t
127 default_text_segment_address() const
129 uint64_t addr = this->pti_->default_text_segment_address;
130 uint64_t pagesize = this->abi_pagesize();
131 if (addr < pagesize)
132 addr = pagesize;
133 return addr;
136 // Return the ABI specified page size.
137 uint64_t
138 abi_pagesize() const
140 if (parameters->options().max_page_size() > 0)
141 return parameters->options().max_page_size();
142 else
143 return this->pti_->abi_pagesize;
146 // Return the common page size used on actual systems.
147 uint64_t
148 common_pagesize() const
150 if (parameters->options().common_page_size() > 0)
151 return std::min(parameters->options().common_page_size(),
152 this->abi_pagesize());
153 else
154 return std::min(this->pti_->common_pagesize,
155 this->abi_pagesize());
158 // Return whether PF_X segments must contain nothing but the contents of
159 // SHF_EXECINSTR sections (no non-executable data, no headers).
160 bool
161 isolate_execinstr() const
162 { return this->pti_->isolate_execinstr; }
164 uint64_t
165 rosegment_gap() const
166 { return this->pti_->rosegment_gap; }
168 // If we see some object files with .note.GNU-stack sections, and
169 // some objects files without them, this returns whether we should
170 // consider the object files without them to imply that the stack
171 // should be executable.
172 bool
173 is_default_stack_executable() const
174 { return this->pti_->is_default_stack_executable; }
176 // Return a character which may appear as a prefix for a wrap
177 // symbol. If this character appears, we strip it when checking for
178 // wrapping and add it back when forming the final symbol name.
179 // This should be '\0' if not special prefix is required, which is
180 // the normal case.
181 char
182 wrap_char() const
183 { return this->pti_->wrap_char; }
185 // Return the special section index which indicates a small common
186 // symbol. This will return SHN_UNDEF if there are no small common
187 // symbols.
188 elfcpp::Elf_Half
189 small_common_shndx() const
190 { return this->pti_->small_common_shndx; }
192 // Return values to add to the section flags for the section holding
193 // small common symbols.
194 elfcpp::Elf_Xword
195 small_common_section_flags() const
197 gold_assert(this->pti_->small_common_shndx != elfcpp::SHN_UNDEF);
198 return this->pti_->small_common_section_flags;
201 // Return the special section index which indicates a large common
202 // symbol. This will return SHN_UNDEF if there are no large common
203 // symbols.
204 elfcpp::Elf_Half
205 large_common_shndx() const
206 { return this->pti_->large_common_shndx; }
208 // Return values to add to the section flags for the section holding
209 // large common symbols.
210 elfcpp::Elf_Xword
211 large_common_section_flags() const
213 gold_assert(this->pti_->large_common_shndx != elfcpp::SHN_UNDEF);
214 return this->pti_->large_common_section_flags;
217 // This hook is called when an output section is created.
218 void
219 new_output_section(Output_section* os) const
220 { this->do_new_output_section(os); }
222 // This is called to tell the target to complete any sections it is
223 // handling. After this all sections must have their final size.
224 void
225 finalize_sections(Layout* layout, const Input_objects* input_objects,
226 Symbol_table* symtab)
227 { return this->do_finalize_sections(layout, input_objects, symtab); }
229 // Return the value to use for a global symbol which needs a special
230 // value in the dynamic symbol table. This will only be called if
231 // the backend first calls symbol->set_needs_dynsym_value().
232 uint64_t
233 dynsym_value(const Symbol* sym) const
234 { return this->do_dynsym_value(sym); }
236 // Return a string to use to fill out a code section. This is
237 // basically one or more NOPS which must fill out the specified
238 // length in bytes.
239 std::string
240 code_fill(section_size_type length) const
241 { return this->do_code_fill(length); }
243 // Return whether SYM is known to be defined by the ABI. This is
244 // used to avoid inappropriate warnings about undefined symbols.
245 bool
246 is_defined_by_abi(const Symbol* sym) const
247 { return this->do_is_defined_by_abi(sym); }
249 // Adjust the output file header before it is written out. VIEW
250 // points to the header in external form. LEN is the length.
251 void
252 adjust_elf_header(unsigned char* view, int len)
253 { return this->do_adjust_elf_header(view, len); }
255 // Return address and size to plug into eh_frame FDEs associated with a PLT.
256 void
257 plt_fde_location(const Output_data* plt, unsigned char* oview,
258 uint64_t* address, off_t* len) const
259 { return this->do_plt_fde_location(plt, oview, address, len); }
261 // Return whether NAME is a local label name. This is used to implement the
262 // --discard-locals options.
263 bool
264 is_local_label_name(const char* name) const
265 { return this->do_is_local_label_name(name); }
267 // Get the symbol index to use for a target specific reloc.
268 unsigned int
269 reloc_symbol_index(void* arg, unsigned int type) const
270 { return this->do_reloc_symbol_index(arg, type); }
272 // Get the addend to use for a target specific reloc.
273 uint64_t
274 reloc_addend(void* arg, unsigned int type, uint64_t addend) const
275 { return this->do_reloc_addend(arg, type, addend); }
277 // Return the PLT address to use for a global symbol.
278 uint64_t
279 plt_address_for_global(const Symbol* sym) const
280 { return this->do_plt_address_for_global(sym); }
282 // Return the PLT address to use for a local symbol.
283 uint64_t
284 plt_address_for_local(const Relobj* object, unsigned int symndx) const
285 { return this->do_plt_address_for_local(object, symndx); }
287 // Return the offset to use for the GOT_INDX'th got entry which is
288 // for a local tls symbol specified by OBJECT, SYMNDX.
289 int64_t
290 tls_offset_for_local(const Relobj* object,
291 unsigned int symndx,
292 unsigned int got_indx) const
293 { return do_tls_offset_for_local(object, symndx, got_indx); }
295 // Return the offset to use for the GOT_INDX'th got entry which is
296 // for global tls symbol GSYM.
297 int64_t
298 tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const
299 { return do_tls_offset_for_global(gsym, got_indx); }
301 // For targets that use function descriptors, if LOC is the location
302 // of a function, modify it to point at the function entry location.
303 void
304 function_location(Symbol_location* loc) const
305 { return do_function_location(loc); }
307 // Return whether this target can use relocation types to determine
308 // if a function's address is taken.
309 bool
310 can_check_for_function_pointers() const
311 { return this->do_can_check_for_function_pointers(); }
313 // Return whether a relocation to a merged section can be processed
314 // to retrieve the contents.
315 bool
316 can_icf_inline_merge_sections () const
317 { return this->pti_->can_icf_inline_merge_sections; }
319 // Whether a section called SECTION_NAME may have function pointers to
320 // sections not eligible for safe ICF folding.
321 virtual bool
322 section_may_have_icf_unsafe_pointers(const char* section_name) const
323 { return this->do_section_may_have_icf_unsafe_pointers(section_name); }
325 // Return the base to use for the PC value in an FDE when it is
326 // encoded using DW_EH_PE_datarel. This does not appear to be
327 // documented anywhere, but it is target specific. Any use of
328 // DW_EH_PE_datarel in gcc requires defining a special macro
329 // (ASM_MAYBE_OUTPUT_ENCODED_ADDR_RTX) to output the value.
330 uint64_t
331 ehframe_datarel_base() const
332 { return this->do_ehframe_datarel_base(); }
334 // Return true if a reference to SYM from a reloc at *PRELOC
335 // means that the current function may call an object compiled
336 // without -fsplit-stack. SYM is known to be defined in an object
337 // compiled without -fsplit-stack.
338 bool
339 is_call_to_non_split(const Symbol* sym, const unsigned char* preloc,
340 const unsigned char* view,
341 section_size_type view_size) const
342 { return this->do_is_call_to_non_split(sym, preloc, view, view_size); }
344 // A function starts at OFFSET in section SHNDX in OBJECT. That
345 // function was compiled with -fsplit-stack, but it refers to a
346 // function which was compiled without -fsplit-stack. VIEW is a
347 // modifiable view of the section; VIEW_SIZE is the size of the
348 // view. The target has to adjust the function so that it allocates
349 // enough stack.
350 void
351 calls_non_split(Relobj* object, unsigned int shndx,
352 section_offset_type fnoffset, section_size_type fnsize,
353 const unsigned char* prelocs, size_t reloc_count,
354 unsigned char* view, section_size_type view_size,
355 std::string* from, std::string* to) const
357 this->do_calls_non_split(object, shndx, fnoffset, fnsize,
358 prelocs, reloc_count, view, view_size,
359 from, to);
362 // Make an ELF object.
363 template<int size, bool big_endian>
364 Object*
365 make_elf_object(const std::string& name, Input_file* input_file,
366 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
367 { return this->do_make_elf_object(name, input_file, offset, ehdr); }
369 // Make an output section.
370 Output_section*
371 make_output_section(const char* name, elfcpp::Elf_Word type,
372 elfcpp::Elf_Xword flags)
373 { return this->do_make_output_section(name, type, flags); }
375 // Return true if target wants to perform relaxation.
376 bool
377 may_relax() const
379 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
380 if (is_debugging_enabled(DEBUG_RELAXATION))
381 return true;
383 return this->do_may_relax();
386 // Perform a relaxation pass. Return true if layout may be changed.
387 bool
388 relax(int pass, const Input_objects* input_objects, Symbol_table* symtab,
389 Layout* layout, const Task* task)
391 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
392 if (is_debugging_enabled(DEBUG_RELAXATION))
393 return pass < 2;
395 return this->do_relax(pass, input_objects, symtab, layout, task);
398 // Return the target-specific name of attributes section. This is
399 // NULL if a target does not use attributes section or if it uses
400 // the default section name ".gnu.attributes".
401 const char*
402 attributes_section() const
403 { return this->pti_->attributes_section; }
405 // Return the vendor name of vendor attributes.
406 const char*
407 attributes_vendor() const
408 { return this->pti_->attributes_vendor; }
410 // Whether a section called NAME is an attribute section.
411 bool
412 is_attributes_section(const char* name) const
414 return ((this->pti_->attributes_section != NULL
415 && strcmp(name, this->pti_->attributes_section) == 0)
416 || strcmp(name, ".gnu.attributes") == 0);
419 // Return a bit mask of argument types for attribute with TAG.
421 attribute_arg_type(int tag) const
422 { return this->do_attribute_arg_type(tag); }
424 // Return the attribute tag of the position NUM in the list of fixed
425 // attributes. Normally there is no reordering and
426 // attributes_order(NUM) == NUM.
428 attributes_order(int num) const
429 { return this->do_attributes_order(num); }
431 // When a target is selected as the default target, we call this method,
432 // which may be used for expensive, target-specific initialization.
433 void
434 select_as_default_target()
435 { this->do_select_as_default_target(); }
437 // Return the value to store in the EI_OSABI field in the ELF
438 // header.
439 elfcpp::ELFOSABI
440 osabi() const
441 { return this->osabi_; }
443 // Set the value to store in the EI_OSABI field in the ELF header.
444 void
445 set_osabi(elfcpp::ELFOSABI osabi)
446 { this->osabi_ = osabi; }
448 // Define target-specific standard symbols.
449 void
450 define_standard_symbols(Symbol_table* symtab, Layout* layout)
451 { this->do_define_standard_symbols(symtab, layout); }
453 // Return the output section name to use given an input section
454 // name, or NULL if no target specific name mapping is required.
455 // Set *PLEN to the length of the name if returning non-NULL.
456 const char*
457 output_section_name(const Relobj* relobj,
458 const char* name,
459 size_t* plen) const
460 { return this->do_output_section_name(relobj, name, plen); }
462 // Add any special sections for this symbol to the gc work list.
463 void
464 gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const
465 { this->do_gc_mark_symbol(symtab, sym); }
467 // Return the name of the entry point symbol.
468 const char*
469 entry_symbol_name() const
470 { return this->pti_->entry_symbol_name; }
472 // Return the size in bits of SHT_HASH entry.
474 hash_entry_size() const
475 { return this->pti_->hash_entry_size; }
477 // Whether the target has a custom set_dynsym_indexes method.
478 bool
479 has_custom_set_dynsym_indexes() const
480 { return this->do_has_custom_set_dynsym_indexes(); }
482 // Custom set_dynsym_indexes method for a target.
483 unsigned int
484 set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
485 std::vector<Symbol*>* syms, Stringpool* dynpool,
486 Versions* versions, Symbol_table* symtab) const
488 return this->do_set_dynsym_indexes(dyn_symbols, index, syms, dynpool,
489 versions, symtab);
492 // Get the custom dynamic tag value.
493 unsigned int
494 dynamic_tag_custom_value(elfcpp::DT tag) const
495 { return this->do_dynamic_tag_custom_value(tag); }
497 // Adjust the value written to the dynamic symbol table.
498 void
499 adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
500 { this->do_adjust_dyn_symbol(sym, view); }
502 // Return whether to include the section in the link.
503 bool
504 should_include_section(elfcpp::Elf_Word sh_type) const
505 { return this->do_should_include_section(sh_type); }
507 protected:
508 // This struct holds the constant information for a child class. We
509 // use a struct to avoid the overhead of virtual function calls for
510 // simple information.
511 struct Target_info
513 // Address size (32 or 64).
514 int size;
515 // Whether the target is big endian.
516 bool is_big_endian;
517 // The code to store in the e_machine field of the ELF header.
518 elfcpp::EM machine_code;
519 // Whether this target has a specific make_symbol function.
520 bool has_make_symbol;
521 // Whether this target has a specific resolve function.
522 bool has_resolve;
523 // Whether this target has a specific code fill function.
524 bool has_code_fill;
525 // Whether an object file with no .note.GNU-stack sections implies
526 // that the stack should be executable.
527 bool is_default_stack_executable;
528 // Whether a relocation to a merged section can be processed to
529 // retrieve the contents.
530 bool can_icf_inline_merge_sections;
531 // Prefix character to strip when checking for wrapping.
532 char wrap_char;
533 // The default dynamic linker name.
534 const char* dynamic_linker;
535 // The default text segment address.
536 uint64_t default_text_segment_address;
537 // The ABI specified page size.
538 uint64_t abi_pagesize;
539 // The common page size used by actual implementations.
540 uint64_t common_pagesize;
541 // Whether PF_X segments must contain nothing but the contents of
542 // SHF_EXECINSTR sections (no non-executable data, no headers).
543 bool isolate_execinstr;
544 // If nonzero, distance from the text segment to the read-only segment.
545 uint64_t rosegment_gap;
546 // The special section index for small common symbols; SHN_UNDEF
547 // if none.
548 elfcpp::Elf_Half small_common_shndx;
549 // The special section index for large common symbols; SHN_UNDEF
550 // if none.
551 elfcpp::Elf_Half large_common_shndx;
552 // Section flags for small common section.
553 elfcpp::Elf_Xword small_common_section_flags;
554 // Section flags for large common section.
555 elfcpp::Elf_Xword large_common_section_flags;
556 // Name of attributes section if it is not ".gnu.attributes".
557 const char* attributes_section;
558 // Vendor name of vendor attributes.
559 const char* attributes_vendor;
560 // Name of the main entry point to the program.
561 const char* entry_symbol_name;
562 // Size (in bits) of SHT_HASH entry. Always equal to 32, except for
563 // 64-bit S/390.
564 const int hash_entry_size;
567 Target(const Target_info* pti)
568 : pti_(pti), processor_specific_flags_(0),
569 are_processor_specific_flags_set_(false), osabi_(elfcpp::ELFOSABI_NONE)
572 // Virtual function which may be implemented by the child class.
573 virtual void
574 do_new_output_section(Output_section*) const
577 // Virtual function which may be implemented by the child class.
578 virtual void
579 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*)
582 // Virtual function which may be implemented by the child class.
583 virtual uint64_t
584 do_dynsym_value(const Symbol*) const
585 { gold_unreachable(); }
587 // Virtual function which must be implemented by the child class if
588 // needed.
589 virtual std::string
590 do_code_fill(section_size_type) const
591 { gold_unreachable(); }
593 // Virtual function which may be implemented by the child class.
594 virtual bool
595 do_is_defined_by_abi(const Symbol*) const
596 { return false; }
598 // Adjust the output file header before it is written out. VIEW
599 // points to the header in external form. LEN is the length, and
600 // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
601 // By default, we set the EI_OSABI field if requested (in
602 // Sized_target).
603 virtual void
604 do_adjust_elf_header(unsigned char*, int) = 0;
606 // Return address and size to plug into eh_frame FDEs associated with a PLT.
607 virtual void
608 do_plt_fde_location(const Output_data* plt, unsigned char* oview,
609 uint64_t* address, off_t* len) const;
611 // Virtual function which may be overridden by the child class.
612 virtual bool
613 do_is_local_label_name(const char*) const;
615 // Virtual function that must be overridden by a target which uses
616 // target specific relocations.
617 virtual unsigned int
618 do_reloc_symbol_index(void*, unsigned int) const
619 { gold_unreachable(); }
621 // Virtual function that must be overridden by a target which uses
622 // target specific relocations.
623 virtual uint64_t
624 do_reloc_addend(void*, unsigned int, uint64_t) const
625 { gold_unreachable(); }
627 // Virtual functions that must be overridden by a target that uses
628 // STT_GNU_IFUNC symbols.
629 virtual uint64_t
630 do_plt_address_for_global(const Symbol*) const
631 { gold_unreachable(); }
633 virtual uint64_t
634 do_plt_address_for_local(const Relobj*, unsigned int) const
635 { gold_unreachable(); }
637 virtual int64_t
638 do_tls_offset_for_local(const Relobj*, unsigned int, unsigned int) const
639 { gold_unreachable(); }
641 virtual int64_t
642 do_tls_offset_for_global(Symbol*, unsigned int) const
643 { gold_unreachable(); }
645 virtual void
646 do_function_location(Symbol_location*) const = 0;
648 // Virtual function which may be overriden by the child class.
649 virtual bool
650 do_can_check_for_function_pointers() const
651 { return false; }
653 // Virtual function which may be overridden by the child class. We
654 // recognize some default sections for which we don't care whether
655 // they have function pointers.
656 virtual bool
657 do_section_may_have_icf_unsafe_pointers(const char* section_name) const
659 // We recognize sections for normal vtables, construction vtables and
660 // EH frames.
661 return (!is_prefix_of(".rodata._ZTV", section_name)
662 && !is_prefix_of(".data.rel.ro._ZTV", section_name)
663 && !is_prefix_of(".rodata._ZTC", section_name)
664 && !is_prefix_of(".data.rel.ro._ZTC", section_name)
665 && !is_prefix_of(".eh_frame", section_name));
668 virtual uint64_t
669 do_ehframe_datarel_base() const
670 { gold_unreachable(); }
672 // Virtual function which may be overridden by the child class. The
673 // default implementation is that any function not defined by the
674 // ABI is a call to a non-split function.
675 virtual bool
676 do_is_call_to_non_split(const Symbol* sym, const unsigned char*,
677 const unsigned char*, section_size_type) const;
679 // Virtual function which may be overridden by the child class.
680 virtual void
681 do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
682 section_size_type, const unsigned char*, size_t,
683 unsigned char*, section_size_type,
684 std::string*, std::string*) const;
686 // make_elf_object hooks. There are four versions of these for
687 // different address sizes and endianness.
689 // Set processor specific flags.
690 void
691 set_processor_specific_flags(elfcpp::Elf_Word flags)
693 this->processor_specific_flags_ = flags;
694 this->are_processor_specific_flags_set_ = true;
697 #ifdef HAVE_TARGET_32_LITTLE
698 // Virtual functions which may be overridden by the child class.
699 virtual Object*
700 do_make_elf_object(const std::string&, Input_file*, off_t,
701 const elfcpp::Ehdr<32, false>&);
702 #endif
704 #ifdef HAVE_TARGET_32_BIG
705 // Virtual functions which may be overridden by the child class.
706 virtual Object*
707 do_make_elf_object(const std::string&, Input_file*, off_t,
708 const elfcpp::Ehdr<32, true>&);
709 #endif
711 #ifdef HAVE_TARGET_64_LITTLE
712 // Virtual functions which may be overridden by the child class.
713 virtual Object*
714 do_make_elf_object(const std::string&, Input_file*, off_t,
715 const elfcpp::Ehdr<64, false>& ehdr);
716 #endif
718 #ifdef HAVE_TARGET_64_BIG
719 // Virtual functions which may be overridden by the child class.
720 virtual Object*
721 do_make_elf_object(const std::string& name, Input_file* input_file,
722 off_t offset, const elfcpp::Ehdr<64, true>& ehdr);
723 #endif
725 // Virtual functions which may be overridden by the child class.
726 virtual Output_section*
727 do_make_output_section(const char* name, elfcpp::Elf_Word type,
728 elfcpp::Elf_Xword flags);
730 // Virtual function which may be overridden by the child class.
731 virtual bool
732 do_may_relax() const
733 { return parameters->options().relax(); }
735 // Virtual function which may be overridden by the child class.
736 virtual bool
737 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*)
738 { return false; }
740 // A function for targets to call. Return whether BYTES/LEN matches
741 // VIEW/VIEW_SIZE at OFFSET.
742 bool
743 match_view(const unsigned char* view, section_size_type view_size,
744 section_offset_type offset, const char* bytes, size_t len) const;
746 // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
747 // for LEN bytes.
748 void
749 set_view_to_nop(unsigned char* view, section_size_type view_size,
750 section_offset_type offset, size_t len) const;
752 // This must be overridden by the child class if it has target-specific
753 // attributes subsection in the attribute section.
754 virtual int
755 do_attribute_arg_type(int) const
756 { gold_unreachable(); }
758 // This may be overridden by the child class.
759 virtual int
760 do_attributes_order(int num) const
761 { return num; }
763 // This may be overridden by the child class.
764 virtual void
765 do_select_as_default_target()
768 // This may be overridden by the child class.
769 virtual void
770 do_define_standard_symbols(Symbol_table*, Layout*)
773 // This may be overridden by the child class.
774 virtual const char*
775 do_output_section_name(const Relobj*, const char*, size_t*) const
776 { return NULL; }
778 // This may be overridden by the child class.
779 virtual void
780 do_gc_mark_symbol(Symbol_table*, Symbol*) const
783 // This may be overridden by the child class.
784 virtual bool
785 do_has_custom_set_dynsym_indexes() const
786 { return false; }
788 // This may be overridden by the child class.
789 virtual unsigned int
790 do_set_dynsym_indexes(std::vector<Symbol*>*, unsigned int,
791 std::vector<Symbol*>*, Stringpool*, Versions*,
792 Symbol_table*) const
793 { gold_unreachable(); }
795 // This may be overridden by the child class.
796 virtual unsigned int
797 do_dynamic_tag_custom_value(elfcpp::DT) const
798 { gold_unreachable(); }
800 // This may be overridden by the child class.
801 virtual void
802 do_adjust_dyn_symbol(const Symbol*, unsigned char*) const
805 // This may be overridden by the child class.
806 virtual bool
807 do_should_include_section(elfcpp::Elf_Word) const
808 { return true; }
810 private:
811 // The implementations of the four do_make_elf_object virtual functions are
812 // almost identical except for their sizes and endianness. We use a template.
813 // for their implementations.
814 template<int size, bool big_endian>
815 inline Object*
816 do_make_elf_object_implementation(const std::string&, Input_file*, off_t,
817 const elfcpp::Ehdr<size, big_endian>&);
819 Target(const Target&);
820 Target& operator=(const Target&);
822 // The target information.
823 const Target_info* pti_;
824 // Processor-specific flags.
825 elfcpp::Elf_Word processor_specific_flags_;
826 // Whether the processor-specific flags are set at least once.
827 bool are_processor_specific_flags_set_;
828 // If not ELFOSABI_NONE, the value to put in the EI_OSABI field of
829 // the ELF header. This is handled at this level because it is
830 // OS-specific rather than processor-specific.
831 elfcpp::ELFOSABI osabi_;
834 // The abstract class for a specific size and endianness of target.
835 // Each actual target implementation class should derive from an
836 // instantiation of Sized_target.
838 template<int size, bool big_endian>
839 class Sized_target : public Target
841 public:
842 // Make a new symbol table entry for the target. This should be
843 // overridden by a target which needs additional information in the
844 // symbol table. This will only be called if has_make_symbol()
845 // returns true.
846 virtual Sized_symbol<size>*
847 make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
848 { gold_unreachable(); }
850 // Resolve a symbol for the target. This should be overridden by a
851 // target which needs to take special action. TO is the
852 // pre-existing symbol. SYM is the new symbol, seen in OBJECT.
853 // VERSION is the version of SYM. This will only be called if
854 // has_resolve() returns true.
855 virtual bool
856 resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
857 const char*)
858 { gold_unreachable(); }
860 // Process the relocs for a section, and record information of the
861 // mapping from source to destination sections. This mapping is later
862 // used to determine unreferenced garbage sections. This procedure is
863 // only called during garbage collection.
864 virtual void
865 gc_process_relocs(Symbol_table* symtab,
866 Layout* layout,
867 Sized_relobj_file<size, big_endian>* object,
868 unsigned int data_shndx,
869 unsigned int sh_type,
870 const unsigned char* prelocs,
871 size_t reloc_count,
872 Output_section* output_section,
873 bool needs_special_offset_handling,
874 size_t local_symbol_count,
875 const unsigned char* plocal_symbols) = 0;
877 // Scan the relocs for a section, and record any information
878 // required for the symbol. SYMTAB is the symbol table. OBJECT is
879 // the object in which the section appears. DATA_SHNDX is the
880 // section index that these relocs apply to. SH_TYPE is the type of
881 // the relocation section, SHT_REL or SHT_RELA. PRELOCS points to
882 // the relocation data. RELOC_COUNT is the number of relocs.
883 // LOCAL_SYMBOL_COUNT is the number of local symbols.
884 // OUTPUT_SECTION is the output section.
885 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
886 // sections are not mapped as usual. PLOCAL_SYMBOLS points to the
887 // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of
888 // pointers to the global symbol table from OBJECT.
889 virtual void
890 scan_relocs(Symbol_table* symtab,
891 Layout* layout,
892 Sized_relobj_file<size, big_endian>* object,
893 unsigned int data_shndx,
894 unsigned int sh_type,
895 const unsigned char* prelocs,
896 size_t reloc_count,
897 Output_section* output_section,
898 bool needs_special_offset_handling,
899 size_t local_symbol_count,
900 const unsigned char* plocal_symbols) = 0;
902 // Relocate section data. SH_TYPE is the type of the relocation
903 // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation
904 // information. RELOC_COUNT is the number of relocs.
905 // OUTPUT_SECTION is the output section.
906 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
907 // to correspond to the output section. VIEW is a view into the
908 // output file holding the section contents, VIEW_ADDRESS is the
909 // virtual address of the view, and VIEW_SIZE is the size of the
910 // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
911 // parameters refer to the complete output section data, not just
912 // the input section data.
913 virtual void
914 relocate_section(const Relocate_info<size, big_endian>*,
915 unsigned int sh_type,
916 const unsigned char* prelocs,
917 size_t reloc_count,
918 Output_section* output_section,
919 bool needs_special_offset_handling,
920 unsigned char* view,
921 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
922 section_size_type view_size,
923 const Reloc_symbol_changes*) = 0;
925 // Scan the relocs during a relocatable link. The parameters are
926 // like scan_relocs, with an additional Relocatable_relocs
927 // parameter, used to record the disposition of the relocs.
928 virtual void
929 scan_relocatable_relocs(Symbol_table* symtab,
930 Layout* layout,
931 Sized_relobj_file<size, big_endian>* object,
932 unsigned int data_shndx,
933 unsigned int sh_type,
934 const unsigned char* prelocs,
935 size_t reloc_count,
936 Output_section* output_section,
937 bool needs_special_offset_handling,
938 size_t local_symbol_count,
939 const unsigned char* plocal_symbols,
940 Relocatable_relocs*) = 0;
942 // Scan the relocs for --emit-relocs. The parameters are
943 // like scan_relocatable_relocs.
944 virtual void
945 emit_relocs_scan(Symbol_table* symtab,
946 Layout* layout,
947 Sized_relobj_file<size, big_endian>* object,
948 unsigned int data_shndx,
949 unsigned int sh_type,
950 const unsigned char* prelocs,
951 size_t reloc_count,
952 Output_section* output_section,
953 bool needs_special_offset_handling,
954 size_t local_symbol_count,
955 const unsigned char* plocal_syms,
956 Relocatable_relocs* rr) = 0;
958 // Emit relocations for a section during a relocatable link, and for
959 // --emit-relocs. The parameters are like relocate_section, with
960 // additional parameters for the view of the output reloc section.
961 virtual void
962 relocate_relocs(const Relocate_info<size, big_endian>*,
963 unsigned int sh_type,
964 const unsigned char* prelocs,
965 size_t reloc_count,
966 Output_section* output_section,
967 typename elfcpp::Elf_types<size>::Elf_Off
968 offset_in_output_section,
969 unsigned char* view,
970 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
971 section_size_type view_size,
972 unsigned char* reloc_view,
973 section_size_type reloc_view_size) = 0;
975 // Perform target-specific processing in a relocatable link. This is
976 // only used if we use the relocation strategy RELOC_SPECIAL.
977 // RELINFO points to a Relocation_info structure. SH_TYPE is the relocation
978 // section type. PRELOC_IN points to the original relocation. RELNUM is
979 // the index number of the relocation in the relocation section.
980 // OUTPUT_SECTION is the output section to which the relocation is applied.
981 // OFFSET_IN_OUTPUT_SECTION is the offset of the relocation input section
982 // within the output section. VIEW points to the output view of the
983 // output section. VIEW_ADDRESS is output address of the view. VIEW_SIZE
984 // is the size of the output view and PRELOC_OUT points to the new
985 // relocation in the output object.
987 // A target only needs to override this if the generic code in
988 // target-reloc.h cannot handle some relocation types.
990 virtual void
991 relocate_special_relocatable(const Relocate_info<size, big_endian>*
992 /*relinfo */,
993 unsigned int /* sh_type */,
994 const unsigned char* /* preloc_in */,
995 size_t /* relnum */,
996 Output_section* /* output_section */,
997 typename elfcpp::Elf_types<size>::Elf_Off
998 /* offset_in_output_section */,
999 unsigned char* /* view */,
1000 typename elfcpp::Elf_types<size>::Elf_Addr
1001 /* view_address */,
1002 section_size_type /* view_size */,
1003 unsigned char* /* preloc_out*/)
1004 { gold_unreachable(); }
1006 // Return the number of entries in the GOT. This is only used for
1007 // laying out the incremental link info sections. A target needs
1008 // to implement this to support incremental linking.
1010 virtual unsigned int
1011 got_entry_count() const
1012 { gold_unreachable(); }
1014 // Return the number of entries in the PLT. This is only used for
1015 // laying out the incremental link info sections. A target needs
1016 // to implement this to support incremental linking.
1018 virtual unsigned int
1019 plt_entry_count() const
1020 { gold_unreachable(); }
1022 // Return the offset of the first non-reserved PLT entry. This is
1023 // only used for laying out the incremental link info sections.
1024 // A target needs to implement this to support incremental linking.
1026 virtual unsigned int
1027 first_plt_entry_offset() const
1028 { gold_unreachable(); }
1030 // Return the size of each PLT entry. This is only used for
1031 // laying out the incremental link info sections. A target needs
1032 // to implement this to support incremental linking.
1034 virtual unsigned int
1035 plt_entry_size() const
1036 { gold_unreachable(); }
1038 // Return the size of each GOT entry. This is only used for
1039 // laying out the incremental link info sections. A target needs
1040 // to implement this if its GOT size is different.
1042 virtual unsigned int
1043 got_entry_size() const
1044 { return size / 8; }
1046 // Create the GOT and PLT sections for an incremental update.
1047 // A target needs to implement this to support incremental linking.
1049 virtual Output_data_got_base*
1050 init_got_plt_for_update(Symbol_table*,
1051 Layout*,
1052 unsigned int /* got_count */,
1053 unsigned int /* plt_count */)
1054 { gold_unreachable(); }
1056 // Reserve a GOT entry for a local symbol, and regenerate any
1057 // necessary dynamic relocations.
1058 virtual void
1059 reserve_local_got_entry(unsigned int /* got_index */,
1060 Sized_relobj<size, big_endian>* /* obj */,
1061 unsigned int /* r_sym */,
1062 unsigned int /* got_type */)
1063 { gold_unreachable(); }
1065 // Reserve a GOT entry for a global symbol, and regenerate any
1066 // necessary dynamic relocations.
1067 virtual void
1068 reserve_global_got_entry(unsigned int /* got_index */, Symbol* /* gsym */,
1069 unsigned int /* got_type */)
1070 { gold_unreachable(); }
1072 // Register an existing PLT entry for a global symbol.
1073 // A target needs to implement this to support incremental linking.
1075 virtual void
1076 register_global_plt_entry(Symbol_table*, Layout*,
1077 unsigned int /* plt_index */,
1078 Symbol*)
1079 { gold_unreachable(); }
1081 // Force a COPY relocation for a given symbol.
1082 // A target needs to implement this to support incremental linking.
1084 virtual void
1085 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t)
1086 { gold_unreachable(); }
1088 // Apply an incremental relocation.
1090 virtual void
1091 apply_relocation(const Relocate_info<size, big_endian>* /* relinfo */,
1092 typename elfcpp::Elf_types<size>::Elf_Addr /* r_offset */,
1093 unsigned int /* r_type */,
1094 typename elfcpp::Elf_types<size>::Elf_Swxword /* r_addend */,
1095 const Symbol* /* gsym */,
1096 unsigned char* /* view */,
1097 typename elfcpp::Elf_types<size>::Elf_Addr /* address */,
1098 section_size_type /* view_size */)
1099 { gold_unreachable(); }
1101 // Handle target specific gc actions when adding a gc reference from
1102 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1103 // and DST_OFF.
1104 void
1105 gc_add_reference(Symbol_table* symtab,
1106 Relobj* src_obj,
1107 unsigned int src_shndx,
1108 Relobj* dst_obj,
1109 unsigned int dst_shndx,
1110 typename elfcpp::Elf_types<size>::Elf_Addr dst_off) const
1112 this->do_gc_add_reference(symtab, src_obj, src_shndx,
1113 dst_obj, dst_shndx, dst_off);
1116 // Return the r_sym field from a relocation.
1117 // Most targets can use the default version of this routine,
1118 // but some targets have a non-standard r_info field, and will
1119 // need to provide a target-specific version.
1120 virtual unsigned int
1121 get_r_sym(const unsigned char* preloc) const
1123 // Since REL and RELA relocs share the same structure through
1124 // the r_info field, we can just use REL here.
1125 elfcpp::Rel<size, big_endian> rel(preloc);
1126 return elfcpp::elf_r_sym<size>(rel.get_r_info());
1129 protected:
1130 Sized_target(const Target::Target_info* pti)
1131 : Target(pti)
1133 gold_assert(pti->size == size);
1134 gold_assert(pti->is_big_endian ? big_endian : !big_endian);
1137 // Set the EI_OSABI field if requested.
1138 virtual void
1139 do_adjust_elf_header(unsigned char*, int);
1141 // Handle target specific gc actions when adding a gc reference.
1142 virtual void
1143 do_gc_add_reference(Symbol_table*, Relobj*, unsigned int,
1144 Relobj*, unsigned int,
1145 typename elfcpp::Elf_types<size>::Elf_Addr) const
1148 virtual void
1149 do_function_location(Symbol_location*) const
1153 } // End namespace gold.
1155 #endif // !defined(GOLD_TARGET_H)