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