OSDN Git Service

2009-06-05 Doug Kwan <dougkwan@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
40 namespace gold
41 {
42
43 class General_options;
44 class Object;
45 template<int size, bool big_endian>
46 class Sized_relobj;
47 class Relocatable_relocs;
48 template<int size, bool big_endian>
49 class Relocate_info;
50 class Symbol;
51 template<int size>
52 class Sized_symbol;
53 class Symbol_table;
54 class Output_section;
55
56 // The abstract class for target specific handling.
57
58 class Target
59 {
60  public:
61   virtual ~Target()
62   { }
63
64   // Return the bit size that this target implements.  This should
65   // return 32 or 64.
66   int
67   get_size() const
68   { return this->pti_->size; }
69
70   // Return whether this target is big-endian.
71   bool
72   is_big_endian() const
73   { return this->pti_->is_big_endian; }
74
75   // Machine code to store in e_machine field of ELF header.
76   elfcpp::EM
77   machine_code() const
78   { return this->pti_->machine_code; }
79
80   // Whether this target has a specific make_symbol function.
81   bool
82   has_make_symbol() const
83   { return this->pti_->has_make_symbol; }
84
85   // Whether this target has a specific resolve function.
86   bool
87   has_resolve() const
88   { return this->pti_->has_resolve; }
89
90   // Whether this target has a specific code fill function.
91   bool
92   has_code_fill() const
93   { return this->pti_->has_code_fill; }
94
95   // Return the default name of the dynamic linker.
96   const char*
97   dynamic_linker() const
98   { return this->pti_->dynamic_linker; }
99
100   // Return the default address to use for the text segment.
101   uint64_t
102   default_text_segment_address() const
103   { return this->pti_->default_text_segment_address; }
104
105   // Return the ABI specified page size.
106   uint64_t
107   abi_pagesize() const
108   {
109     if (parameters->options().max_page_size() > 0)
110       return parameters->options().max_page_size();
111     else
112       return this->pti_->abi_pagesize;
113   }
114
115   // Return the common page size used on actual systems.
116   uint64_t
117   common_pagesize() const
118   {
119     if (parameters->options().common_page_size() > 0)
120       return std::min(parameters->options().common_page_size(),
121                       this->abi_pagesize());
122     else
123       return std::min(this->pti_->common_pagesize,
124                       this->abi_pagesize());
125   }
126
127   // If we see some object files with .note.GNU-stack sections, and
128   // some objects files without them, this returns whether we should
129   // consider the object files without them to imply that the stack
130   // should be executable.
131   bool
132   is_default_stack_executable() const
133   { return this->pti_->is_default_stack_executable; }
134
135   // Return a character which may appear as a prefix for a wrap
136   // symbol.  If this character appears, we strip it when checking for
137   // wrapping and add it back when forming the final symbol name.
138   // This should be '\0' if not special prefix is required, which is
139   // the normal case.
140   char
141   wrap_char() const
142   { return this->pti_->wrap_char; }
143
144   // This is called to tell the target to complete any sections it is
145   // handling.  After this all sections must have their final size.
146   void
147   finalize_sections(Layout* layout)
148   { return this->do_finalize_sections(layout); }
149
150   // Return the value to use for a global symbol which needs a special
151   // value in the dynamic symbol table.  This will only be called if
152   // the backend first calls symbol->set_needs_dynsym_value().
153   uint64_t
154   dynsym_value(const Symbol* sym) const
155   { return this->do_dynsym_value(sym); }
156
157   // Return a string to use to fill out a code section.  This is
158   // basically one or more NOPS which must fill out the specified
159   // length in bytes.
160   std::string
161   code_fill(section_size_type length) const
162   { return this->do_code_fill(length); }
163
164   // Return whether SYM is known to be defined by the ABI.  This is
165   // used to avoid inappropriate warnings about undefined symbols.
166   bool
167   is_defined_by_abi(const Symbol* sym) const
168   { return this->do_is_defined_by_abi(sym); }
169
170   // Adjust the output file header before it is written out.  VIEW
171   // points to the header in external form.  LEN is the length.
172   void
173   adjust_elf_header(unsigned char* view, int len) const
174   { return this->do_adjust_elf_header(view, len); }
175
176   // Return whether NAME is a local label name.  This is used to implement the
177   // --discard-locals options.
178   bool
179   is_local_label_name(const char* name) const
180   { return this->do_is_local_label_name(name); }
181
182  protected:
183   // This struct holds the constant information for a child class.  We
184   // use a struct to avoid the overhead of virtual function calls for
185   // simple information.
186   struct Target_info
187   {
188     // Address size (32 or 64).
189     int size;
190     // Whether the target is big endian.
191     bool is_big_endian;
192     // The code to store in the e_machine field of the ELF header.
193     elfcpp::EM machine_code;
194     // Whether this target has a specific make_symbol function.
195     bool has_make_symbol;
196     // Whether this target has a specific resolve function.
197     bool has_resolve;
198     // Whether this target has a specific code fill function.
199     bool has_code_fill;
200     // Whether an object file with no .note.GNU-stack sections implies
201     // that the stack should be executable.
202     bool is_default_stack_executable;
203     // Prefix character to strip when checking for wrapping.
204     char wrap_char;
205     // The default dynamic linker name.
206     const char* dynamic_linker;
207     // The default text segment address.
208     uint64_t default_text_segment_address;
209     // The ABI specified page size.
210     uint64_t abi_pagesize;
211     // The common page size used by actual implementations.
212     uint64_t common_pagesize;
213   };
214
215   Target(const Target_info* pti)
216     : pti_(pti)
217   { }
218
219   // Virtual function which may be implemented by the child class.
220   virtual void
221   do_finalize_sections(Layout*)
222   { }
223
224   // Virtual function which may be implemented by the child class.
225   virtual uint64_t
226   do_dynsym_value(const Symbol*) const
227   { gold_unreachable(); }
228
229   // Virtual function which must be implemented by the child class if
230   // needed.
231   virtual std::string
232   do_code_fill(section_size_type) const
233   { gold_unreachable(); }
234
235   // Virtual function which may be implemented by the child class.
236   virtual bool
237   do_is_defined_by_abi(const Symbol*) const
238   { return false; }
239
240   // Adjust the output file header before it is written out.  VIEW
241   // points to the header in external form.  LEN is the length, and
242   // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
243   // By default, we do nothing.
244   virtual void
245   do_adjust_elf_header(unsigned char*, int) const
246   { }
247
248   // Virtual function which may be overriden by the child class.
249   virtual bool
250   do_is_local_label_name(const char*) const;
251
252  private:
253   Target(const Target&);
254   Target& operator=(const Target&);
255
256   // The target information.
257   const Target_info* pti_;
258 };
259
260 // The abstract class for a specific size and endianness of target.
261 // Each actual target implementation class should derive from an
262 // instantiation of Sized_target.
263
264 template<int size, bool big_endian>
265 class Sized_target : public Target
266 {
267  public:
268   // Make a new symbol table entry for the target.  This should be
269   // overridden by a target which needs additional information in the
270   // symbol table.  This will only be called if has_make_symbol()
271   // returns true.
272   virtual Sized_symbol<size>*
273   make_symbol() const
274   { gold_unreachable(); }
275
276   // Resolve a symbol for the target.  This should be overridden by a
277   // target which needs to take special action.  TO is the
278   // pre-existing symbol.  SYM is the new symbol, seen in OBJECT.
279   // VERSION is the version of SYM.  This will only be called if
280   // has_resolve() returns true.
281   virtual void
282   resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
283           const char*)
284   { gold_unreachable(); }
285
286   // Process the relocs for a section, and record information of the
287   // mapping from source to destination sections. This mapping is later
288   // used to determine unreferenced garbage sections. This procedure is
289   // only called during garbage collection.
290   virtual void
291   gc_process_relocs(const General_options& options,
292               Symbol_table* symtab,
293               Layout* layout,
294               Sized_relobj<size, big_endian>* object,
295               unsigned int data_shndx,
296               unsigned int sh_type,
297               const unsigned char* prelocs,
298               size_t reloc_count,
299               Output_section* output_section,
300               bool needs_special_offset_handling,
301               size_t local_symbol_count,
302               const unsigned char* plocal_symbols) = 0;
303
304   // Scan the relocs for a section, and record any information
305   // required for the symbol.  OPTIONS is the command line options.
306   // SYMTAB is the symbol table.  OBJECT is the object in which the
307   // section appears.  DATA_SHNDX is the section index that these
308   // relocs apply to.  SH_TYPE is the type of the relocation section,
309   // SHT_REL or SHT_RELA.  PRELOCS points to the relocation data.
310   // RELOC_COUNT is the number of relocs.  LOCAL_SYMBOL_COUNT is the
311   // number of local symbols.  OUTPUT_SECTION is the output section.
312   // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
313   // sections are not mapped as usual.  PLOCAL_SYMBOLS points to the
314   // local symbol data from OBJECT.  GLOBAL_SYMBOLS is the array of
315   // pointers to the global symbol table from OBJECT.
316   virtual void
317   scan_relocs(const General_options& options,
318               Symbol_table* symtab,
319               Layout* layout,
320               Sized_relobj<size, big_endian>* object,
321               unsigned int data_shndx,
322               unsigned int sh_type,
323               const unsigned char* prelocs,
324               size_t reloc_count,
325               Output_section* output_section,
326               bool needs_special_offset_handling,
327               size_t local_symbol_count,
328               const unsigned char* plocal_symbols) = 0;
329
330   // Relocate section data.  SH_TYPE is the type of the relocation
331   // section, SHT_REL or SHT_RELA.  PRELOCS points to the relocation
332   // information.  RELOC_COUNT is the number of relocs.
333   // OUTPUT_SECTION is the output section.
334   // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
335   // to correspond to the output section.  VIEW is a view into the
336   // output file holding the section contents, VIEW_ADDRESS is the
337   // virtual address of the view, and VIEW_SIZE is the size of the
338   // view.  If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
339   // parameters refer to the complete output section data, not just
340   // the input section data.
341   virtual void
342   relocate_section(const Relocate_info<size, big_endian>*,
343                    unsigned int sh_type,
344                    const unsigned char* prelocs,
345                    size_t reloc_count,
346                    Output_section* output_section,
347                    bool needs_special_offset_handling,
348                    unsigned char* view,
349                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
350                    section_size_type view_size) = 0;
351
352   // Scan the relocs during a relocatable link.  The parameters are
353   // like scan_relocs, with an additional Relocatable_relocs
354   // parameter, used to record the disposition of the relocs.
355   virtual void
356   scan_relocatable_relocs(const General_options& options,
357                           Symbol_table* symtab,
358                           Layout* layout,
359                           Sized_relobj<size, big_endian>* object,
360                           unsigned int data_shndx,
361                           unsigned int sh_type,
362                           const unsigned char* prelocs,
363                           size_t reloc_count,
364                           Output_section* output_section,
365                           bool needs_special_offset_handling,
366                           size_t local_symbol_count,
367                           const unsigned char* plocal_symbols,
368                           Relocatable_relocs*) = 0;
369
370   // Relocate a section during a relocatable link.  The parameters are
371   // like relocate_section, with additional parameters for the view of
372   // the output reloc section.
373   virtual void
374   relocate_for_relocatable(const Relocate_info<size, big_endian>*,
375                            unsigned int sh_type,
376                            const unsigned char* prelocs,
377                            size_t reloc_count,
378                            Output_section* output_section,
379                            off_t offset_in_output_section,
380                            const Relocatable_relocs*,
381                            unsigned char* view,
382                            typename elfcpp::Elf_types<size>::Elf_Addr
383                              view_address,
384                            section_size_type view_size,
385                            unsigned char* reloc_view,
386                            section_size_type reloc_view_size) = 0;
387
388  protected:
389   Sized_target(const Target::Target_info* pti)
390     : Target(pti)
391   {
392     gold_assert(pti->size == size);
393     gold_assert(pti->is_big_endian ? big_endian : !big_endian);
394   }
395 };
396
397 } // End namespace gold.
398
399 #endif // !defined(GOLD_TARGET_H)