673153ff9368deb2bd6998ed3d778af4fe7ae4ca
[external/binutils.git] / gold / target.h
1 // target.h -- target support for gold   -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009 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 class Relocatable_relocs;
49 template<int size, bool big_endian>
50 class Relocate_info;
51 class Reloc_symbol_changes;
52 class Symbol;
53 template<int size>
54 class Sized_symbol;
55 class Symbol_table;
56 class Output_section;
57 class Input_objects;
58
59 // The abstract class for target specific handling.
60
61 class Target
62 {
63  public:
64   virtual ~Target()
65   { }
66
67   // Virtual function which is set to return true by a target if
68   // it can use relocation types to determine if a function's
69   // pointer is taken.
70   virtual bool
71   can_check_for_function_pointers() const
72   { return false; }
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   // If we see some object files with .note.GNU-stack sections, and
148   // some objects files without them, this returns whether we should
149   // consider the object files without them to imply that the stack
150   // should be executable.
151   bool
152   is_default_stack_executable() const
153   { return this->pti_->is_default_stack_executable; }
154
155   // Return a character which may appear as a prefix for a wrap
156   // symbol.  If this character appears, we strip it when checking for
157   // wrapping and add it back when forming the final symbol name.
158   // This should be '\0' if not special prefix is required, which is
159   // the normal case.
160   char
161   wrap_char() const
162   { return this->pti_->wrap_char; }
163
164   // Return the special section index which indicates a small common
165   // symbol.  This will return SHN_UNDEF if there are no small common
166   // symbols.
167   elfcpp::Elf_Half
168   small_common_shndx() const
169   { return this->pti_->small_common_shndx; }
170
171   // Return values to add to the section flags for the section holding
172   // small common symbols.
173   elfcpp::Elf_Xword
174   small_common_section_flags() const
175   {
176     gold_assert(this->pti_->small_common_shndx != elfcpp::SHN_UNDEF);
177     return this->pti_->small_common_section_flags;
178   }
179
180   // Return the special section index which indicates a large common
181   // symbol.  This will return SHN_UNDEF if there are no large common
182   // symbols.
183   elfcpp::Elf_Half
184   large_common_shndx() const
185   { return this->pti_->large_common_shndx; }
186
187   // Return values to add to the section flags for the section holding
188   // large common symbols.
189   elfcpp::Elf_Xword
190   large_common_section_flags() const
191   {
192     gold_assert(this->pti_->large_common_shndx != elfcpp::SHN_UNDEF);
193     return this->pti_->large_common_section_flags;
194   }
195
196   // This hook is called when an output section is created.
197   void
198   new_output_section(Output_section* os) const
199   { this->do_new_output_section(os); }
200
201   // This is called to tell the target to complete any sections it is
202   // handling.  After this all sections must have their final size.
203   void
204   finalize_sections(Layout* layout, const Input_objects* input_objects,
205                     Symbol_table* symtab)
206   { return this->do_finalize_sections(layout, input_objects, symtab); }
207
208   // Return the value to use for a global symbol which needs a special
209   // value in the dynamic symbol table.  This will only be called if
210   // the backend first calls symbol->set_needs_dynsym_value().
211   uint64_t
212   dynsym_value(const Symbol* sym) const
213   { return this->do_dynsym_value(sym); }
214
215   // Return a string to use to fill out a code section.  This is
216   // basically one or more NOPS which must fill out the specified
217   // length in bytes.
218   std::string
219   code_fill(section_size_type length) const
220   { return this->do_code_fill(length); }
221
222   // Return whether SYM is known to be defined by the ABI.  This is
223   // used to avoid inappropriate warnings about undefined symbols.
224   bool
225   is_defined_by_abi(const Symbol* sym) const
226   { return this->do_is_defined_by_abi(sym); }
227
228   // Adjust the output file header before it is written out.  VIEW
229   // points to the header in external form.  LEN is the length.
230   void
231   adjust_elf_header(unsigned char* view, int len) const
232   { return this->do_adjust_elf_header(view, len); }
233
234   // Return whether NAME is a local label name.  This is used to implement the
235   // --discard-locals options.
236   bool
237   is_local_label_name(const char* name) const
238   { return this->do_is_local_label_name(name); }
239
240   // Get the symbol index to use for a target specific reloc.
241   unsigned int
242   reloc_symbol_index(void* arg, unsigned int type) const
243   { return this->do_reloc_symbol_index(arg, type); }
244
245   // Get the addend to use for a target specific reloc.
246   uint64_t
247   reloc_addend(void* arg, unsigned int type, uint64_t addend) const
248   { return this->do_reloc_addend(arg, type, addend); }
249
250   // A function starts at OFFSET in section SHNDX in OBJECT.  That
251   // function was compiled with -fsplit-stack, but it refers to a
252   // function which was compiled without -fsplit-stack.  VIEW is a
253   // modifiable view of the section; VIEW_SIZE is the size of the
254   // view.  The target has to adjust the function so that it allocates
255   // enough stack.
256   void
257   calls_non_split(Relobj* object, unsigned int shndx,
258                   section_offset_type fnoffset, section_size_type fnsize,
259                   unsigned char* view, section_size_type view_size,
260                   std::string* from, std::string* to) const
261   {
262     this->do_calls_non_split(object, shndx, fnoffset, fnsize, view, view_size,
263                              from, to);
264   }
265
266   // Make an ELF object.
267   template<int size, bool big_endian>
268   Object*
269   make_elf_object(const std::string& name, Input_file* input_file,
270                   off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
271   { return this->do_make_elf_object(name, input_file, offset, ehdr); }
272
273   // Make an output section.
274   Output_section*
275   make_output_section(const char* name, elfcpp::Elf_Word type,
276                       elfcpp::Elf_Xword flags)
277   { return this->do_make_output_section(name, type, flags); }
278
279   // Return true if target wants to perform relaxation.
280   bool
281   may_relax() const
282   {
283     // Run the dummy relaxation pass twice if relaxation debugging is enabled.
284     if (is_debugging_enabled(DEBUG_RELAXATION))
285       return true;
286
287      return this->do_may_relax();
288   }
289
290   // Perform a relaxation pass.  Return true if layout may be changed.
291   bool
292   relax(int pass, const Input_objects* input_objects, Symbol_table* symtab,
293         Layout* layout)
294   {
295     // Run the dummy relaxation pass twice if relaxation debugging is enabled.
296     if (is_debugging_enabled(DEBUG_RELAXATION))
297       return pass < 2;
298
299     return this->do_relax(pass, input_objects, symtab, layout);
300   } 
301
302   // Return the target-specific name of attributes section.  This is
303   // NULL if a target does not use attributes section or if it uses
304   // the default section name ".gnu.attributes".
305   const char*
306   attributes_section() const
307   { return this->pti_->attributes_section; }
308
309   // Return the vendor name of vendor attributes.
310   const char*
311   attributes_vendor() const
312   { return this->pti_->attributes_vendor; }
313
314   // Whether a section called NAME is an attribute section.
315   bool
316   is_attributes_section(const char* name) const
317   {
318     return ((this->pti_->attributes_section != NULL
319              && strcmp(name, this->pti_->attributes_section) == 0)
320             || strcmp(name, ".gnu.attributes") == 0); 
321   }
322
323   // Return a bit mask of argument types for attribute with TAG.
324   int
325   attribute_arg_type(int tag) const
326   { return this->do_attribute_arg_type(tag); }
327
328   // Return the attribute tag of the position NUM in the list of fixed
329   // attributes.  Normally there is no reordering and
330   // attributes_order(NUM) == NUM.
331   int
332   attributes_order(int num) const
333   { return this->do_attributes_order(num); }
334
335   // When a target is selected as the default target, we call this method,
336   // which may be used for expensive, target-specific initialization.
337   void
338   select_as_default_target()
339   { this->do_select_as_default_target(); } 
340
341  protected:
342   // This struct holds the constant information for a child class.  We
343   // use a struct to avoid the overhead of virtual function calls for
344   // simple information.
345   struct Target_info
346   {
347     // Address size (32 or 64).
348     int size;
349     // Whether the target is big endian.
350     bool is_big_endian;
351     // The code to store in the e_machine field of the ELF header.
352     elfcpp::EM machine_code;
353     // Whether this target has a specific make_symbol function.
354     bool has_make_symbol;
355     // Whether this target has a specific resolve function.
356     bool has_resolve;
357     // Whether this target has a specific code fill function.
358     bool has_code_fill;
359     // Whether an object file with no .note.GNU-stack sections implies
360     // that the stack should be executable.
361     bool is_default_stack_executable;
362     // Prefix character to strip when checking for wrapping.
363     char wrap_char;
364     // The default dynamic linker name.
365     const char* dynamic_linker;
366     // The default text segment address.
367     uint64_t default_text_segment_address;
368     // The ABI specified page size.
369     uint64_t abi_pagesize;
370     // The common page size used by actual implementations.
371     uint64_t common_pagesize;
372     // The special section index for small common symbols; SHN_UNDEF
373     // if none.
374     elfcpp::Elf_Half small_common_shndx;
375     // The special section index for large common symbols; SHN_UNDEF
376     // if none.
377     elfcpp::Elf_Half large_common_shndx;
378     // Section flags for small common section.
379     elfcpp::Elf_Xword small_common_section_flags;
380     // Section flags for large common section.
381     elfcpp::Elf_Xword large_common_section_flags;
382     // Name of attributes section if it is not ".gnu.attributes".
383     const char* attributes_section;
384     // Vendor name of vendor attributes.
385     const char* attributes_vendor;
386   };
387
388   Target(const Target_info* pti)
389     : pti_(pti), processor_specific_flags_(0),
390       are_processor_specific_flags_set_(false)
391   { }
392
393   // Virtual function which may be implemented by the child class.
394   virtual void
395   do_new_output_section(Output_section*) const
396   { }
397
398   // Virtual function which may be implemented by the child class.
399   virtual void
400   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*)
401   { }
402
403   // Virtual function which may be implemented by the child class.
404   virtual uint64_t
405   do_dynsym_value(const Symbol*) const
406   { gold_unreachable(); }
407
408   // Virtual function which must be implemented by the child class if
409   // needed.
410   virtual std::string
411   do_code_fill(section_size_type) const
412   { gold_unreachable(); }
413
414   // Virtual function which may be implemented by the child class.
415   virtual bool
416   do_is_defined_by_abi(const Symbol*) const
417   { return false; }
418
419   // Adjust the output file header before it is written out.  VIEW
420   // points to the header in external form.  LEN is the length, and
421   // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
422   // By default, we do nothing.
423   virtual void
424   do_adjust_elf_header(unsigned char*, int) const
425   { }
426
427   // Virtual function which may be overriden by the child class.
428   virtual bool
429   do_is_local_label_name(const char*) const;
430
431   // Virtual function that must be overridden by a target which uses
432   // target specific relocations.
433   virtual unsigned int
434   do_reloc_symbol_index(void*, unsigned int) const
435   { gold_unreachable(); }
436
437   // Virtual function that must be overidden by a target which uses
438   // target specific relocations.
439   virtual uint64_t
440   do_reloc_addend(void*, unsigned int, uint64_t) const
441   { gold_unreachable(); }
442
443   // Virtual function which may be overridden by the child class.
444   virtual void
445   do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
446                      section_size_type, unsigned char*, section_size_type,
447                      std::string*, std::string*) const;
448
449   // make_elf_object hooks.  There are four versions of these for
450   // different address sizes and endianities.
451
452   // Set processor specific flags.
453   void
454   set_processor_specific_flags(elfcpp::Elf_Word flags)
455   {
456     this->processor_specific_flags_ = flags;
457     this->are_processor_specific_flags_set_ = true;
458   }
459   
460 #ifdef HAVE_TARGET_32_LITTLE
461   // Virtual functions which may be overriden by the child class.
462   virtual Object*
463   do_make_elf_object(const std::string&, Input_file*, off_t,
464                      const elfcpp::Ehdr<32, false>&);
465 #endif
466
467 #ifdef HAVE_TARGET_32_BIG
468   // Virtual functions which may be overriden by the child class.
469   virtual Object*
470   do_make_elf_object(const std::string&, Input_file*, off_t,
471                      const elfcpp::Ehdr<32, true>&);
472 #endif
473
474 #ifdef HAVE_TARGET_64_LITTLE
475   // Virtual functions which may be overriden by the child class.
476   virtual Object*
477   do_make_elf_object(const std::string&, Input_file*, off_t,
478                      const elfcpp::Ehdr<64, false>& ehdr);
479 #endif
480
481 #ifdef HAVE_TARGET_64_BIG
482   // Virtual functions which may be overriden by the child class.
483   virtual Object*
484   do_make_elf_object(const std::string& name, Input_file* input_file,
485                      off_t offset, const elfcpp::Ehdr<64, true>& ehdr);
486 #endif
487
488   // Virtual functions which may be overriden by the child class.
489   virtual Output_section*
490   do_make_output_section(const char* name, elfcpp::Elf_Word type,
491                          elfcpp::Elf_Xword flags);
492
493   // Virtual function which may be overriden by the child class.
494   virtual bool
495   do_may_relax() const
496   { return parameters->options().relax(); }
497
498   // Virtual function which may be overriden by the child class.
499   virtual bool
500   do_relax(int, const Input_objects*, Symbol_table*, Layout*)
501   { return false; }
502
503   // A function for targets to call.  Return whether BYTES/LEN matches
504   // VIEW/VIEW_SIZE at OFFSET.
505   bool
506   match_view(const unsigned char* view, section_size_type view_size,
507              section_offset_type offset, const char* bytes, size_t len) const;
508
509   // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
510   // for LEN bytes.
511   void
512   set_view_to_nop(unsigned char* view, section_size_type view_size,
513                   section_offset_type offset, size_t len) const;
514
515   // This must be overriden by the child class if it has target-specific
516   // attributes subsection in the attribute section. 
517   virtual int
518   do_attribute_arg_type(int) const
519   { gold_unreachable(); }
520
521   // This may be overridden by the child class.
522   virtual int
523   do_attributes_order(int num) const
524   { return num; }
525
526   // This may be overridden by the child class.
527   virtual void
528   do_select_as_default_target()
529   { }
530
531  private:
532   // The implementations of the four do_make_elf_object virtual functions are
533   // almost identical except for their sizes and endianity.  We use a template.
534   // for their implementations.
535   template<int size, bool big_endian>
536   inline Object*
537   do_make_elf_object_implementation(const std::string&, Input_file*, off_t,
538                                     const elfcpp::Ehdr<size, big_endian>&);
539
540   Target(const Target&);
541   Target& operator=(const Target&);
542
543   // The target information.
544   const Target_info* pti_;
545   // Processor-specific flags.
546   elfcpp::Elf_Word processor_specific_flags_;
547   // Whether the processor-specific flags are set at least once.
548   bool are_processor_specific_flags_set_;
549 };
550
551 // The abstract class for a specific size and endianness of target.
552 // Each actual target implementation class should derive from an
553 // instantiation of Sized_target.
554
555 template<int size, bool big_endian>
556 class Sized_target : public Target
557 {
558  public:
559   // Make a new symbol table entry for the target.  This should be
560   // overridden by a target which needs additional information in the
561   // symbol table.  This will only be called if has_make_symbol()
562   // returns true.
563   virtual Sized_symbol<size>*
564   make_symbol() const
565   { gold_unreachable(); }
566
567   // Resolve a symbol for the target.  This should be overridden by a
568   // target which needs to take special action.  TO is the
569   // pre-existing symbol.  SYM is the new symbol, seen in OBJECT.
570   // VERSION is the version of SYM.  This will only be called if
571   // has_resolve() returns true.
572   virtual void
573   resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
574           const char*)
575   { gold_unreachable(); }
576
577   // Process the relocs for a section, and record information of the
578   // mapping from source to destination sections. This mapping is later
579   // used to determine unreferenced garbage sections. This procedure is
580   // only called during garbage collection.
581   virtual void
582   gc_process_relocs(Symbol_table* symtab,
583                     Layout* layout,
584                     Sized_relobj<size, big_endian>* object,
585                     unsigned int data_shndx,
586                     unsigned int sh_type,
587                     const unsigned char* prelocs,
588                     size_t reloc_count,
589                     Output_section* output_section,
590                     bool needs_special_offset_handling,
591                     size_t local_symbol_count,
592                     const unsigned char* plocal_symbols) = 0;
593
594   // Scan the relocs for a section, and record any information
595   // required for the symbol.  SYMTAB is the symbol table.  OBJECT is
596   // the object in which the section appears.  DATA_SHNDX is the
597   // section index that these relocs apply to.  SH_TYPE is the type of
598   // the relocation section, SHT_REL or SHT_RELA.  PRELOCS points to
599   // the relocation data.  RELOC_COUNT is the number of relocs.
600   // LOCAL_SYMBOL_COUNT is the number of local symbols.
601   // OUTPUT_SECTION is the output section.
602   // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
603   // sections are not mapped as usual.  PLOCAL_SYMBOLS points to the
604   // local symbol data from OBJECT.  GLOBAL_SYMBOLS is the array of
605   // pointers to the global symbol table from OBJECT.
606   virtual void
607   scan_relocs(Symbol_table* symtab,
608               Layout* layout,
609               Sized_relobj<size, big_endian>* object,
610               unsigned int data_shndx,
611               unsigned int sh_type,
612               const unsigned char* prelocs,
613               size_t reloc_count,
614               Output_section* output_section,
615               bool needs_special_offset_handling,
616               size_t local_symbol_count,
617               const unsigned char* plocal_symbols) = 0;
618
619   // Relocate section data.  SH_TYPE is the type of the relocation
620   // section, SHT_REL or SHT_RELA.  PRELOCS points to the relocation
621   // information.  RELOC_COUNT is the number of relocs.
622   // OUTPUT_SECTION is the output section.
623   // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
624   // to correspond to the output section.  VIEW is a view into the
625   // output file holding the section contents, VIEW_ADDRESS is the
626   // virtual address of the view, and VIEW_SIZE is the size of the
627   // view.  If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
628   // parameters refer to the complete output section data, not just
629   // the input section data.
630   virtual void
631   relocate_section(const Relocate_info<size, big_endian>*,
632                    unsigned int sh_type,
633                    const unsigned char* prelocs,
634                    size_t reloc_count,
635                    Output_section* output_section,
636                    bool needs_special_offset_handling,
637                    unsigned char* view,
638                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
639                    section_size_type view_size,
640                    const Reloc_symbol_changes*) = 0;
641
642   // Scan the relocs during a relocatable link.  The parameters are
643   // like scan_relocs, with an additional Relocatable_relocs
644   // parameter, used to record the disposition of the relocs.
645   virtual void
646   scan_relocatable_relocs(Symbol_table* symtab,
647                           Layout* layout,
648                           Sized_relobj<size, big_endian>* object,
649                           unsigned int data_shndx,
650                           unsigned int sh_type,
651                           const unsigned char* prelocs,
652                           size_t reloc_count,
653                           Output_section* output_section,
654                           bool needs_special_offset_handling,
655                           size_t local_symbol_count,
656                           const unsigned char* plocal_symbols,
657                           Relocatable_relocs*) = 0;
658
659   // Relocate a section during a relocatable link.  The parameters are
660   // like relocate_section, with additional parameters for the view of
661   // the output reloc section.
662   virtual void
663   relocate_for_relocatable(const Relocate_info<size, big_endian>*,
664                            unsigned int sh_type,
665                            const unsigned char* prelocs,
666                            size_t reloc_count,
667                            Output_section* output_section,
668                            off_t offset_in_output_section,
669                            const Relocatable_relocs*,
670                            unsigned char* view,
671                            typename elfcpp::Elf_types<size>::Elf_Addr
672                              view_address,
673                            section_size_type view_size,
674                            unsigned char* reloc_view,
675                            section_size_type reloc_view_size) = 0;
676
677  protected:
678   Sized_target(const Target::Target_info* pti)
679     : Target(pti)
680   {
681     gold_assert(pti->size == size);
682     gold_assert(pti->is_big_endian ? big_endian : !big_endian);
683   }
684 };
685
686 } // End namespace gold.
687
688 #endif // !defined(GOLD_TARGET_H)