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