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