OSDN Git Service

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