Automatic date update in version.in
[binutils-gdb.git] / gold / target.h
blobfb4981192734b74a3737726ddd0991f3e910cdbd
1 // target.h -- target support for gold -*- C++ -*-
3 // Copyright (C) 2006-2022 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 Output_data_got_base* got,
293 unsigned int got_indx,
294 uint64_t addend) const
295 { return do_tls_offset_for_local(object, symndx, got, got_indx, addend); }
297 // Return the offset to use for the GOT_INDX'th got entry which is
298 // for global tls symbol GSYM.
299 int64_t
300 tls_offset_for_global(Symbol* gsym,
301 Output_data_got_base* got,
302 unsigned int got_indx,
303 uint64_t addend) const
304 { return do_tls_offset_for_global(gsym, got, got_indx, addend); }
306 // For targets that use function descriptors, if LOC is the location
307 // of a function, modify it to point at the function entry location.
308 void
309 function_location(Symbol_location* loc) const
310 { return do_function_location(loc); }
312 // Return whether this target can use relocation types to determine
313 // if a function's address is taken.
314 bool
315 can_check_for_function_pointers() const
316 { return this->do_can_check_for_function_pointers(); }
318 // Return whether a relocation to a merged section can be processed
319 // to retrieve the contents.
320 bool
321 can_icf_inline_merge_sections () const
322 { return this->pti_->can_icf_inline_merge_sections; }
324 // Whether a section called SECTION_NAME may have function pointers to
325 // sections not eligible for safe ICF folding.
326 virtual bool
327 section_may_have_icf_unsafe_pointers(const char* section_name) const
328 { return this->do_section_may_have_icf_unsafe_pointers(section_name); }
330 // Return the base to use for the PC value in an FDE when it is
331 // encoded using DW_EH_PE_datarel. This does not appear to be
332 // documented anywhere, but it is target specific. Any use of
333 // DW_EH_PE_datarel in gcc requires defining a special macro
334 // (ASM_MAYBE_OUTPUT_ENCODED_ADDR_RTX) to output the value.
335 uint64_t
336 ehframe_datarel_base() const
337 { return this->do_ehframe_datarel_base(); }
339 // Return true if a reference to SYM from a reloc at *PRELOC
340 // means that the current function may call an object compiled
341 // without -fsplit-stack. SYM is known to be defined in an object
342 // compiled without -fsplit-stack.
343 bool
344 is_call_to_non_split(const Symbol* sym, const unsigned char* preloc,
345 const unsigned char* view,
346 section_size_type view_size) const
347 { return this->do_is_call_to_non_split(sym, preloc, view, view_size); }
349 // A function starts at OFFSET in section SHNDX in OBJECT. That
350 // function was compiled with -fsplit-stack, but it refers to a
351 // function which was compiled without -fsplit-stack. VIEW is a
352 // modifiable view of the section; VIEW_SIZE is the size of the
353 // view. The target has to adjust the function so that it allocates
354 // enough stack.
355 void
356 calls_non_split(Relobj* object, unsigned int shndx,
357 section_offset_type fnoffset, section_size_type fnsize,
358 const unsigned char* prelocs, size_t reloc_count,
359 unsigned char* view, section_size_type view_size,
360 std::string* from, std::string* to) const
362 this->do_calls_non_split(object, shndx, fnoffset, fnsize,
363 prelocs, reloc_count, view, view_size,
364 from, to);
367 // Make an ELF object.
368 template<int size, bool big_endian>
369 Object*
370 make_elf_object(const std::string& name, Input_file* input_file,
371 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
372 { return this->do_make_elf_object(name, input_file, offset, ehdr); }
374 // Make an output section.
375 Output_section*
376 make_output_section(const char* name, elfcpp::Elf_Word type,
377 elfcpp::Elf_Xword flags)
378 { return this->do_make_output_section(name, type, flags); }
380 // Return true if target wants to perform relaxation.
381 bool
382 may_relax() const
384 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
385 if (is_debugging_enabled(DEBUG_RELAXATION))
386 return true;
388 return this->do_may_relax();
391 // Perform a relaxation pass. Return true if layout may be changed.
392 bool
393 relax(int pass, const Input_objects* input_objects, Symbol_table* symtab,
394 Layout* layout, const Task* task)
396 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
397 if (is_debugging_enabled(DEBUG_RELAXATION))
398 return pass < 2;
400 return this->do_relax(pass, input_objects, symtab, layout, task);
403 // Return the target-specific name of attributes section. This is
404 // NULL if a target does not use attributes section or if it uses
405 // the default section name ".gnu.attributes".
406 const char*
407 attributes_section() const
408 { return this->pti_->attributes_section; }
410 // Return the vendor name of vendor attributes.
411 const char*
412 attributes_vendor() const
413 { return this->pti_->attributes_vendor; }
415 // Whether a section called NAME is an attribute section.
416 bool
417 is_attributes_section(const char* name) const
419 return ((this->pti_->attributes_section != NULL
420 && strcmp(name, this->pti_->attributes_section) == 0)
421 || strcmp(name, ".gnu.attributes") == 0);
424 // Return a bit mask of argument types for attribute with TAG.
426 attribute_arg_type(int tag) const
427 { return this->do_attribute_arg_type(tag); }
429 // Return the attribute tag of the position NUM in the list of fixed
430 // attributes. Normally there is no reordering and
431 // attributes_order(NUM) == NUM.
433 attributes_order(int num) const
434 { return this->do_attributes_order(num); }
436 // When a target is selected as the default target, we call this method,
437 // which may be used for expensive, target-specific initialization.
438 void
439 select_as_default_target()
440 { this->do_select_as_default_target(); }
442 // Return the value to store in the EI_OSABI field in the ELF
443 // header.
444 elfcpp::ELFOSABI
445 osabi() const
446 { return this->osabi_; }
448 // Set the value to store in the EI_OSABI field in the ELF header.
449 void
450 set_osabi(elfcpp::ELFOSABI osabi)
451 { this->osabi_ = osabi; }
453 // Define target-specific standard symbols.
454 void
455 define_standard_symbols(Symbol_table* symtab, Layout* layout)
456 { this->do_define_standard_symbols(symtab, layout); }
458 // Return the output section name to use given an input section
459 // name, or NULL if no target specific name mapping is required.
460 // Set *PLEN to the length of the name if returning non-NULL.
461 const char*
462 output_section_name(const Relobj* relobj,
463 const char* name,
464 size_t* plen) const
465 { return this->do_output_section_name(relobj, name, plen); }
467 // Add any special sections for this symbol to the gc work list.
468 void
469 gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const
470 { this->do_gc_mark_symbol(symtab, sym); }
472 // Return the name of the entry point symbol.
473 const char*
474 entry_symbol_name() const
475 { return this->pti_->entry_symbol_name; }
477 // Return the size in bits of SHT_HASH entry.
479 hash_entry_size() const
480 { return this->pti_->hash_entry_size; }
482 // Return the section type to use for unwind sections.
483 unsigned int
484 unwind_section_type() const
485 { return this->pti_->unwind_section_type; }
487 // Whether the target has a custom set_dynsym_indexes method.
488 bool
489 has_custom_set_dynsym_indexes() const
490 { return this->do_has_custom_set_dynsym_indexes(); }
492 // Custom set_dynsym_indexes method for a target.
493 unsigned int
494 set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
495 std::vector<Symbol*>* syms, Stringpool* dynpool,
496 Versions* versions, Symbol_table* symtab) const
498 return this->do_set_dynsym_indexes(dyn_symbols, index, syms, dynpool,
499 versions, symtab);
502 // Get the custom dynamic tag value.
503 unsigned int
504 dynamic_tag_custom_value(elfcpp::DT tag) const
505 { return this->do_dynamic_tag_custom_value(tag); }
507 // Adjust the value written to the dynamic symbol table.
508 void
509 adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
510 { this->do_adjust_dyn_symbol(sym, view); }
512 // Return whether to include the section in the link.
513 bool
514 should_include_section(elfcpp::Elf_Word sh_type) const
515 { return this->do_should_include_section(sh_type); }
517 // Finalize the target-specific properties in the .note.gnu.property section.
518 void
519 finalize_gnu_properties(Layout* layout) const
520 { this->do_finalize_gnu_properties(layout); }
522 protected:
523 // This struct holds the constant information for a child class. We
524 // use a struct to avoid the overhead of virtual function calls for
525 // simple information.
526 struct Target_info
528 // Address size (32 or 64).
529 int size;
530 // Whether the target is big endian.
531 bool is_big_endian;
532 // The code to store in the e_machine field of the ELF header.
533 elfcpp::EM machine_code;
534 // Whether this target has a specific make_symbol function.
535 bool has_make_symbol;
536 // Whether this target has a specific resolve function.
537 bool has_resolve;
538 // Whether this target has a specific code fill function.
539 bool has_code_fill;
540 // Whether an object file with no .note.GNU-stack sections implies
541 // that the stack should be executable.
542 bool is_default_stack_executable;
543 // Whether a relocation to a merged section can be processed to
544 // retrieve the contents.
545 bool can_icf_inline_merge_sections;
546 // Prefix character to strip when checking for wrapping.
547 char wrap_char;
548 // The default dynamic linker name.
549 const char* dynamic_linker;
550 // The default text segment address.
551 uint64_t default_text_segment_address;
552 // The ABI specified page size.
553 uint64_t abi_pagesize;
554 // The common page size used by actual implementations.
555 uint64_t common_pagesize;
556 // Whether PF_X segments must contain nothing but the contents of
557 // SHF_EXECINSTR sections (no non-executable data, no headers).
558 bool isolate_execinstr;
559 // If nonzero, distance from the text segment to the read-only segment.
560 uint64_t rosegment_gap;
561 // The special section index for small common symbols; SHN_UNDEF
562 // if none.
563 elfcpp::Elf_Half small_common_shndx;
564 // The special section index for large common symbols; SHN_UNDEF
565 // if none.
566 elfcpp::Elf_Half large_common_shndx;
567 // Section flags for small common section.
568 elfcpp::Elf_Xword small_common_section_flags;
569 // Section flags for large common section.
570 elfcpp::Elf_Xword large_common_section_flags;
571 // Name of attributes section if it is not ".gnu.attributes".
572 const char* attributes_section;
573 // Vendor name of vendor attributes.
574 const char* attributes_vendor;
575 // Name of the main entry point to the program.
576 const char* entry_symbol_name;
577 // Size (in bits) of SHT_HASH entry. Always equal to 32, except for
578 // 64-bit S/390.
579 const int hash_entry_size;
580 // Processor-specific section type for ".eh_frame" (unwind) sections.
581 // SHT_PROGBITS if there is no special section type.
582 const unsigned int unwind_section_type;
585 Target(const Target_info* pti)
586 : pti_(pti), processor_specific_flags_(0),
587 are_processor_specific_flags_set_(false), osabi_(elfcpp::ELFOSABI_NONE)
590 // Virtual function which may be implemented by the child class.
591 virtual void
592 do_new_output_section(Output_section*) const
595 // Virtual function which may be implemented by the child class.
596 virtual void
597 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*)
600 // Virtual function which may be implemented by the child class.
601 virtual uint64_t
602 do_dynsym_value(const Symbol*) const
603 { gold_unreachable(); }
605 // Virtual function which must be implemented by the child class if
606 // needed.
607 virtual std::string
608 do_code_fill(section_size_type) const
609 { gold_unreachable(); }
611 // Virtual function which may be implemented by the child class.
612 virtual bool
613 do_is_defined_by_abi(const Symbol*) const
614 { return false; }
616 // Adjust the output file header before it is written out. VIEW
617 // points to the header in external form. LEN is the length, and
618 // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
619 // By default, we set the EI_OSABI field if requested (in
620 // Sized_target).
621 virtual void
622 do_adjust_elf_header(unsigned char*, int) = 0;
624 // Return address and size to plug into eh_frame FDEs associated with a PLT.
625 virtual void
626 do_plt_fde_location(const Output_data* plt, unsigned char* oview,
627 uint64_t* address, off_t* len) const;
629 // Virtual function which may be overridden by the child class.
630 virtual bool
631 do_is_local_label_name(const char*) const;
633 // Virtual function that must be overridden by a target which uses
634 // target specific relocations.
635 virtual unsigned int
636 do_reloc_symbol_index(void*, unsigned int) const
637 { gold_unreachable(); }
639 // Virtual function that must be overridden by a target which uses
640 // target specific relocations.
641 virtual uint64_t
642 do_reloc_addend(void*, unsigned int, uint64_t) const
643 { gold_unreachable(); }
645 // Virtual functions that must be overridden by a target that uses
646 // STT_GNU_IFUNC symbols.
647 virtual uint64_t
648 do_plt_address_for_global(const Symbol*) const
649 { gold_unreachable(); }
651 virtual uint64_t
652 do_plt_address_for_local(const Relobj*, unsigned int) const
653 { gold_unreachable(); }
655 virtual int64_t
656 do_tls_offset_for_local(const Relobj*, unsigned int,
657 Output_data_got_base*, unsigned int,
658 uint64_t) const
659 { gold_unreachable(); }
661 virtual int64_t
662 do_tls_offset_for_global(Symbol*, Output_data_got_base*, unsigned int,
663 uint64_t) const
664 { gold_unreachable(); }
666 virtual void
667 do_function_location(Symbol_location*) const = 0;
669 // Virtual function which may be overriden by the child class.
670 virtual bool
671 do_can_check_for_function_pointers() const
672 { return false; }
674 // Virtual function which may be overridden by the child class. We
675 // recognize some default sections for which we don't care whether
676 // they have function pointers.
677 virtual bool
678 do_section_may_have_icf_unsafe_pointers(const char* section_name) const
680 // We recognize sections for normal vtables, construction vtables and
681 // EH frames.
682 return (!is_prefix_of(".rodata._ZTV", section_name)
683 && !is_prefix_of(".data.rel.ro._ZTV", section_name)
684 && !is_prefix_of(".rodata._ZTC", section_name)
685 && !is_prefix_of(".data.rel.ro._ZTC", section_name)
686 && !is_prefix_of(".eh_frame", section_name));
689 virtual uint64_t
690 do_ehframe_datarel_base() const
691 { gold_unreachable(); }
693 // Virtual function which may be overridden by the child class. The
694 // default implementation is that any function not defined by the
695 // ABI is a call to a non-split function.
696 virtual bool
697 do_is_call_to_non_split(const Symbol* sym, const unsigned char*,
698 const unsigned char*, section_size_type) const;
700 // Virtual function which may be overridden by the child class.
701 virtual void
702 do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
703 section_size_type, const unsigned char*, size_t,
704 unsigned char*, section_size_type,
705 std::string*, std::string*) const;
707 // make_elf_object hooks. There are four versions of these for
708 // different address sizes and endianness.
710 // Set processor specific flags.
711 void
712 set_processor_specific_flags(elfcpp::Elf_Word flags)
714 this->processor_specific_flags_ = flags;
715 this->are_processor_specific_flags_set_ = true;
718 #ifdef HAVE_TARGET_32_LITTLE
719 // Virtual functions which may be overridden by the child class.
720 virtual Object*
721 do_make_elf_object(const std::string&, Input_file*, off_t,
722 const elfcpp::Ehdr<32, false>&);
723 #endif
725 #ifdef HAVE_TARGET_32_BIG
726 // Virtual functions which may be overridden by the child class.
727 virtual Object*
728 do_make_elf_object(const std::string&, Input_file*, off_t,
729 const elfcpp::Ehdr<32, true>&);
730 #endif
732 #ifdef HAVE_TARGET_64_LITTLE
733 // Virtual functions which may be overridden by the child class.
734 virtual Object*
735 do_make_elf_object(const std::string&, Input_file*, off_t,
736 const elfcpp::Ehdr<64, false>& ehdr);
737 #endif
739 #ifdef HAVE_TARGET_64_BIG
740 // Virtual functions which may be overridden by the child class.
741 virtual Object*
742 do_make_elf_object(const std::string& name, Input_file* input_file,
743 off_t offset, const elfcpp::Ehdr<64, true>& ehdr);
744 #endif
746 // Virtual functions which may be overridden by the child class.
747 virtual Output_section*
748 do_make_output_section(const char* name, elfcpp::Elf_Word type,
749 elfcpp::Elf_Xword flags);
751 // Virtual function which may be overridden by the child class.
752 virtual bool
753 do_may_relax() const
754 { return parameters->options().relax(); }
756 // Virtual function which may be overridden by the child class.
757 virtual bool
758 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*)
759 { return false; }
761 // A function for targets to call. Return whether BYTES/LEN matches
762 // VIEW/VIEW_SIZE at OFFSET.
763 bool
764 match_view(const unsigned char* view, section_size_type view_size,
765 section_offset_type offset, const char* bytes, size_t len) const;
767 // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
768 // for LEN bytes.
769 void
770 set_view_to_nop(unsigned char* view, section_size_type view_size,
771 section_offset_type offset, size_t len) const;
773 // This must be overridden by the child class if it has target-specific
774 // attributes subsection in the attribute section.
775 virtual int
776 do_attribute_arg_type(int) const
777 { gold_unreachable(); }
779 // This may be overridden by the child class.
780 virtual int
781 do_attributes_order(int num) const
782 { return num; }
784 // This may be overridden by the child class.
785 virtual void
786 do_select_as_default_target()
789 // This may be overridden by the child class.
790 virtual void
791 do_define_standard_symbols(Symbol_table*, Layout*)
794 // This may be overridden by the child class.
795 virtual const char*
796 do_output_section_name(const Relobj*, const char*, size_t*) const
797 { return NULL; }
799 // This may be overridden by the child class.
800 virtual void
801 do_gc_mark_symbol(Symbol_table*, Symbol*) const
804 // This may be overridden by the child class.
805 virtual bool
806 do_has_custom_set_dynsym_indexes() const
807 { return false; }
809 // This may be overridden by the child class.
810 virtual unsigned int
811 do_set_dynsym_indexes(std::vector<Symbol*>*, unsigned int,
812 std::vector<Symbol*>*, Stringpool*, Versions*,
813 Symbol_table*) const
814 { gold_unreachable(); }
816 // This may be overridden by the child class.
817 virtual unsigned int
818 do_dynamic_tag_custom_value(elfcpp::DT) const
819 { gold_unreachable(); }
821 // This may be overridden by the child class.
822 virtual void
823 do_adjust_dyn_symbol(const Symbol*, unsigned char*) const
826 // This may be overridden by the child class.
827 virtual bool
828 do_should_include_section(elfcpp::Elf_Word) const
829 { return true; }
831 // Finalize the target-specific properties in the .note.gnu.property section.
832 virtual void
833 do_finalize_gnu_properties(Layout*) const
836 private:
837 // The implementations of the four do_make_elf_object virtual functions are
838 // almost identical except for their sizes and endianness. We use a template.
839 // for their implementations.
840 template<int size, bool big_endian>
841 inline Object*
842 do_make_elf_object_implementation(const std::string&, Input_file*, off_t,
843 const elfcpp::Ehdr<size, big_endian>&);
845 Target(const Target&);
846 Target& operator=(const Target&);
848 // The target information.
849 const Target_info* pti_;
850 // Processor-specific flags.
851 elfcpp::Elf_Word processor_specific_flags_;
852 // Whether the processor-specific flags are set at least once.
853 bool are_processor_specific_flags_set_;
854 // If not ELFOSABI_NONE, the value to put in the EI_OSABI field of
855 // the ELF header. This is handled at this level because it is
856 // OS-specific rather than processor-specific.
857 elfcpp::ELFOSABI osabi_;
860 // The abstract class for a specific size and endianness of target.
861 // Each actual target implementation class should derive from an
862 // instantiation of Sized_target.
864 template<int size, bool big_endian>
865 class Sized_target : public Target
867 public:
868 // Make a new symbol table entry for the target. This should be
869 // overridden by a target which needs additional information in the
870 // symbol table. This will only be called if has_make_symbol()
871 // returns true.
872 virtual Sized_symbol<size>*
873 make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t)
874 { gold_unreachable(); }
876 // Resolve a symbol for the target. This should be overridden by a
877 // target which needs to take special action. TO is the
878 // pre-existing symbol. SYM is the new symbol, seen in OBJECT.
879 // VERSION is the version of SYM. This will only be called if
880 // has_resolve() returns true.
881 virtual bool
882 resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
883 const char*)
884 { gold_unreachable(); }
886 // Process the relocs for a section, and record information of the
887 // mapping from source to destination sections. This mapping is later
888 // used to determine unreferenced garbage sections. This procedure is
889 // only called during garbage collection.
890 virtual void
891 gc_process_relocs(Symbol_table* symtab,
892 Layout* layout,
893 Sized_relobj_file<size, big_endian>* object,
894 unsigned int data_shndx,
895 unsigned int sh_type,
896 const unsigned char* prelocs,
897 size_t reloc_count,
898 Output_section* output_section,
899 bool needs_special_offset_handling,
900 size_t local_symbol_count,
901 const unsigned char* plocal_symbols) = 0;
903 // Scan the relocs for a section, and record any information
904 // required for the symbol. SYMTAB is the symbol table. OBJECT is
905 // the object in which the section appears. DATA_SHNDX is the
906 // section index that these relocs apply to. SH_TYPE is the type of
907 // the relocation section, SHT_REL or SHT_RELA. PRELOCS points to
908 // the relocation data. RELOC_COUNT is the number of relocs.
909 // LOCAL_SYMBOL_COUNT is the number of local symbols.
910 // OUTPUT_SECTION is the output section.
911 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
912 // sections are not mapped as usual. PLOCAL_SYMBOLS points to the
913 // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of
914 // pointers to the global symbol table from OBJECT.
915 virtual void
916 scan_relocs(Symbol_table* symtab,
917 Layout* layout,
918 Sized_relobj_file<size, big_endian>* object,
919 unsigned int data_shndx,
920 unsigned int sh_type,
921 const unsigned char* prelocs,
922 size_t reloc_count,
923 Output_section* output_section,
924 bool needs_special_offset_handling,
925 size_t local_symbol_count,
926 const unsigned char* plocal_symbols) = 0;
928 // Relocate section data. SH_TYPE is the type of the relocation
929 // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation
930 // information. RELOC_COUNT is the number of relocs.
931 // OUTPUT_SECTION is the output section.
932 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
933 // to correspond to the output section. VIEW is a view into the
934 // output file holding the section contents, VIEW_ADDRESS is the
935 // virtual address of the view, and VIEW_SIZE is the size of the
936 // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
937 // parameters refer to the complete output section data, not just
938 // the input section data.
939 virtual void
940 relocate_section(const Relocate_info<size, big_endian>*,
941 unsigned int sh_type,
942 const unsigned char* prelocs,
943 size_t reloc_count,
944 Output_section* output_section,
945 bool needs_special_offset_handling,
946 unsigned char* view,
947 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
948 section_size_type view_size,
949 const Reloc_symbol_changes*) = 0;
951 // Scan the relocs during a relocatable link. The parameters are
952 // like scan_relocs, with an additional Relocatable_relocs
953 // parameter, used to record the disposition of the relocs.
954 virtual void
955 scan_relocatable_relocs(Symbol_table* symtab,
956 Layout* layout,
957 Sized_relobj_file<size, big_endian>* object,
958 unsigned int data_shndx,
959 unsigned int sh_type,
960 const unsigned char* prelocs,
961 size_t reloc_count,
962 Output_section* output_section,
963 bool needs_special_offset_handling,
964 size_t local_symbol_count,
965 const unsigned char* plocal_symbols,
966 Relocatable_relocs*) = 0;
968 // Scan the relocs for --emit-relocs. The parameters are
969 // like scan_relocatable_relocs.
970 virtual void
971 emit_relocs_scan(Symbol_table* symtab,
972 Layout* layout,
973 Sized_relobj_file<size, big_endian>* object,
974 unsigned int data_shndx,
975 unsigned int sh_type,
976 const unsigned char* prelocs,
977 size_t reloc_count,
978 Output_section* output_section,
979 bool needs_special_offset_handling,
980 size_t local_symbol_count,
981 const unsigned char* plocal_syms,
982 Relocatable_relocs* rr) = 0;
984 // Emit relocations for a section during a relocatable link, and for
985 // --emit-relocs. The parameters are like relocate_section, with
986 // additional parameters for the view of the output reloc section.
987 virtual void
988 relocate_relocs(const Relocate_info<size, big_endian>*,
989 unsigned int sh_type,
990 const unsigned char* prelocs,
991 size_t reloc_count,
992 Output_section* output_section,
993 typename elfcpp::Elf_types<size>::Elf_Off
994 offset_in_output_section,
995 unsigned char* view,
996 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
997 section_size_type view_size,
998 unsigned char* reloc_view,
999 section_size_type reloc_view_size) = 0;
1001 // Perform target-specific processing in a relocatable link. This is
1002 // only used if we use the relocation strategy RELOC_SPECIAL.
1003 // RELINFO points to a Relocation_info structure. SH_TYPE is the relocation
1004 // section type. PRELOC_IN points to the original relocation. RELNUM is
1005 // the index number of the relocation in the relocation section.
1006 // OUTPUT_SECTION is the output section to which the relocation is applied.
1007 // OFFSET_IN_OUTPUT_SECTION is the offset of the relocation input section
1008 // within the output section. VIEW points to the output view of the
1009 // output section. VIEW_ADDRESS is output address of the view. VIEW_SIZE
1010 // is the size of the output view and PRELOC_OUT points to the new
1011 // relocation in the output object.
1013 // A target only needs to override this if the generic code in
1014 // target-reloc.h cannot handle some relocation types.
1016 virtual void
1017 relocate_special_relocatable(const Relocate_info<size, big_endian>*
1018 /*relinfo */,
1019 unsigned int /* sh_type */,
1020 const unsigned char* /* preloc_in */,
1021 size_t /* relnum */,
1022 Output_section* /* output_section */,
1023 typename elfcpp::Elf_types<size>::Elf_Off
1024 /* offset_in_output_section */,
1025 unsigned char* /* view */,
1026 typename elfcpp::Elf_types<size>::Elf_Addr
1027 /* view_address */,
1028 section_size_type /* view_size */,
1029 unsigned char* /* preloc_out*/)
1030 { gold_unreachable(); }
1032 // Return the number of entries in the GOT. This is only used for
1033 // laying out the incremental link info sections. A target needs
1034 // to implement this to support incremental linking.
1036 virtual unsigned int
1037 got_entry_count() const
1038 { gold_unreachable(); }
1040 // Return the number of entries in the PLT. This is only used for
1041 // laying out the incremental link info sections. A target needs
1042 // to implement this to support incremental linking.
1044 virtual unsigned int
1045 plt_entry_count() const
1046 { gold_unreachable(); }
1048 // Return the offset of the first non-reserved PLT entry. This is
1049 // only used for laying out the incremental link info sections.
1050 // A target needs to implement this to support incremental linking.
1052 virtual unsigned int
1053 first_plt_entry_offset() const
1054 { gold_unreachable(); }
1056 // Return the size of each PLT entry. This is only used for
1057 // laying out the incremental link info sections. A target needs
1058 // to implement this to support incremental linking.
1060 virtual unsigned int
1061 plt_entry_size() const
1062 { gold_unreachable(); }
1064 // Return the size of each GOT entry. This is only used for
1065 // laying out the incremental link info sections. A target needs
1066 // to implement this if its GOT size is different.
1068 virtual unsigned int
1069 got_entry_size() const
1070 { return size / 8; }
1072 // Create the GOT and PLT sections for an incremental update.
1073 // A target needs to implement this to support incremental linking.
1075 virtual Output_data_got_base*
1076 init_got_plt_for_update(Symbol_table*,
1077 Layout*,
1078 unsigned int /* got_count */,
1079 unsigned int /* plt_count */)
1080 { gold_unreachable(); }
1082 // Reserve a GOT entry for a local symbol, and regenerate any
1083 // necessary dynamic relocations.
1084 virtual void
1085 reserve_local_got_entry(unsigned int /* got_index */,
1086 Sized_relobj<size, big_endian>* /* obj */,
1087 unsigned int /* r_sym */,
1088 unsigned int /* got_type */)
1089 { gold_unreachable(); }
1091 // Reserve a GOT entry for a global symbol, and regenerate any
1092 // necessary dynamic relocations.
1093 virtual void
1094 reserve_global_got_entry(unsigned int /* got_index */, Symbol* /* gsym */,
1095 unsigned int /* got_type */)
1096 { gold_unreachable(); }
1098 // Register an existing PLT entry for a global symbol.
1099 // A target needs to implement this to support incremental linking.
1101 virtual void
1102 register_global_plt_entry(Symbol_table*, Layout*,
1103 unsigned int /* plt_index */,
1104 Symbol*)
1105 { gold_unreachable(); }
1107 // Force a COPY relocation for a given symbol.
1108 // A target needs to implement this to support incremental linking.
1110 virtual void
1111 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t)
1112 { gold_unreachable(); }
1114 // Apply an incremental relocation.
1116 virtual void
1117 apply_relocation(const Relocate_info<size, big_endian>* /* relinfo */,
1118 typename elfcpp::Elf_types<size>::Elf_Addr /* r_offset */,
1119 unsigned int /* r_type */,
1120 typename elfcpp::Elf_types<size>::Elf_Swxword /* r_addend */,
1121 const Symbol* /* gsym */,
1122 unsigned char* /* view */,
1123 typename elfcpp::Elf_types<size>::Elf_Addr /* address */,
1124 section_size_type /* view_size */)
1125 { gold_unreachable(); }
1127 // Handle target specific gc actions when adding a gc reference from
1128 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1129 // and DST_OFF.
1130 void
1131 gc_add_reference(Symbol_table* symtab,
1132 Relobj* src_obj,
1133 unsigned int src_shndx,
1134 Relobj* dst_obj,
1135 unsigned int dst_shndx,
1136 typename elfcpp::Elf_types<size>::Elf_Addr dst_off) const
1138 this->do_gc_add_reference(symtab, src_obj, src_shndx,
1139 dst_obj, dst_shndx, dst_off);
1142 // Return the r_sym field from a relocation.
1143 // Most targets can use the default version of this routine,
1144 // but some targets have a non-standard r_info field, and will
1145 // need to provide a target-specific version.
1146 virtual unsigned int
1147 get_r_sym(const unsigned char* preloc) const
1149 // Since REL and RELA relocs share the same structure through
1150 // the r_info field, we can just use REL here.
1151 elfcpp::Rel<size, big_endian> rel(preloc);
1152 return elfcpp::elf_r_sym<size>(rel.get_r_info());
1155 // Record a target-specific program property in the .note.gnu.property
1156 // section.
1157 virtual void
1158 record_gnu_property(unsigned int, unsigned int, size_t,
1159 const unsigned char*, const Object*)
1162 // Merge the target-specific program properties from the current object.
1163 virtual void
1164 merge_gnu_properties(const Object*)
1167 protected:
1168 Sized_target(const Target::Target_info* pti)
1169 : Target(pti)
1171 gold_assert(pti->size == size);
1172 gold_assert(pti->is_big_endian ? big_endian : !big_endian);
1175 // Set the EI_OSABI field if requested.
1176 virtual void
1177 do_adjust_elf_header(unsigned char*, int);
1179 // Handle target specific gc actions when adding a gc reference.
1180 virtual void
1181 do_gc_add_reference(Symbol_table*, Relobj*, unsigned int,
1182 Relobj*, unsigned int,
1183 typename elfcpp::Elf_types<size>::Elf_Addr) const
1186 virtual void
1187 do_function_location(Symbol_location*) const
1191 } // End namespace gold.
1193 #endif // !defined(GOLD_TARGET_H)