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