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