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