Support special always-defined symbols for targets.
[external/binutils.git] / gold / target.h
1 // target.h -- target support for gold   -*- C++ -*-
2
3 // Copyright 2006, 2007 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
38 namespace gold
39 {
40
41 class General_options;
42 class Object;
43 template<int size, bool big_endian>
44 class Sized_relobj;
45 template<int size, bool big_endian>
46 class Relocate_info;
47 class Symbol;
48 template<int size>
49 class Sized_symbol;
50 class Symbol_table;
51 class Output_section;
52
53 // The abstract class for target specific handling.
54
55 class Target
56 {
57  public:
58   virtual ~Target()
59   { }
60
61   // Return the bit size that this target implements.  This should
62   // return 32 or 64.
63   int
64   get_size() const
65   { return this->pti_->size; }
66
67   // Return whether this target is big-endian.
68   bool
69   is_big_endian() const
70   { return this->pti_->is_big_endian; }
71
72   // Machine code to store in e_machine field of ELF header.
73   elfcpp::EM
74   machine_code() const
75   { return this->pti_->machine_code; }
76
77   // Whether this target has a specific make_symbol function.
78   bool
79   has_make_symbol() const
80   { return this->pti_->has_make_symbol; }
81
82   // Whether this target has a specific resolve function.
83   bool
84   has_resolve() const
85   { return this->pti_->has_resolve; }
86
87   // Whether this target has a specific code fill function.
88   bool
89   has_code_fill() const
90   { return this->pti_->has_code_fill; }
91
92   // Return the default name of the dynamic linker.
93   const char*
94   dynamic_linker() const
95   { return this->pti_->dynamic_linker; }
96
97   // Return the default address to use for the text segment.
98   uint64_t
99   default_text_segment_address() const
100   { return this->pti_->default_text_segment_address; }
101
102   // Return the ABI specified page size.
103   uint64_t
104   abi_pagesize() const
105   { return this->pti_->abi_pagesize; }
106
107   // Return the common page size used on actual systems.
108   uint64_t
109   common_pagesize() const
110   { return this->pti_->common_pagesize; }
111
112   // If we see some object files with .note.GNU-stack sections, and
113   // some objects files without them, this returns whether we should
114   // consider the object files without them to imply that the stack
115   // should be executable.
116   bool
117   is_default_stack_executable() const
118   { return this->pti_->is_default_stack_executable; }
119
120   // This is called to tell the target to complete any sections it is
121   // handling.  After this all sections must have their final size.
122   void
123   finalize_sections(Layout* layout)
124   { return this->do_finalize_sections(layout); }
125
126   // Return the value to use for a global symbol which needs a special
127   // value in the dynamic symbol table.  This will only be called if
128   // the backend first calls symbol->set_needs_dynsym_value().
129   uint64_t
130   dynsym_value(const Symbol* sym) const
131   { return this->do_dynsym_value(sym); }
132
133   // Return a string to use to fill out a code section.  This is
134   // basically one or more NOPS which must fill out the specified
135   // length in bytes.
136   std::string
137   code_fill(off_t length)
138   { return this->do_code_fill(length); }
139
140   // Return whether SYM is a special symbol which is known to be
141   // defined.  This is used to avoid inappropriate warnings about
142   // undefined symbols.
143   bool
144   is_always_defined(Symbol* sym) const
145   { return this->do_is_always_defined(sym); }
146
147  protected:
148   // This struct holds the constant information for a child class.  We
149   // use a struct to avoid the overhead of virtual function calls for
150   // simple information.
151   struct Target_info
152   {
153     // Address size (32 or 64).
154     int size;
155     // Whether the target is big endian.
156     bool is_big_endian;
157     // The code to store in the e_machine field of the ELF header.
158     elfcpp::EM machine_code;
159     // Whether this target has a specific make_symbol function.
160     bool has_make_symbol;
161     // Whether this target has a specific resolve function.
162     bool has_resolve;
163     // Whether this target has a specific code fill function.
164     bool has_code_fill;
165     // Whether an object file with no .note.GNU-stack sections implies
166     // that the stack should be executable.
167     bool is_default_stack_executable;
168     // The default dynamic linker name.
169     const char* dynamic_linker;
170     // The default text segment address.
171     uint64_t default_text_segment_address;
172     // The ABI specified page size.
173     uint64_t abi_pagesize;
174     // The common page size used by actual implementations.
175     uint64_t common_pagesize;
176   };
177
178   Target(const Target_info* pti)
179     : pti_(pti)
180   { }
181
182   // Virtual function which may be implemented by the child class.
183   virtual void
184   do_finalize_sections(Layout*)
185   { }
186
187   // Virtual function which may be implemented by the child class.
188   virtual uint64_t
189   do_dynsym_value(const Symbol*) const
190   { gold_unreachable(); }
191
192   // Virtual function which must be implemented by the child class if
193   // needed.
194   virtual std::string
195   do_code_fill(off_t)
196   { gold_unreachable(); }
197
198   // Virtual function which may be implemented by the child class if
199   // needed.
200   virtual bool
201   do_is_always_defined(Symbol*) const
202   { return false; }
203
204  private:
205   Target(const Target&);
206   Target& operator=(const Target&);
207
208   // The target information.
209   const Target_info* pti_;
210 };
211
212 // The abstract class for a specific size and endianness of target.
213 // Each actual target implementation class should derive from an
214 // instantiation of Sized_target.
215
216 template<int size, bool big_endian>
217 class Sized_target : public Target
218 {
219  public:
220   // Make a new symbol table entry for the target.  This should be
221   // overridden by a target which needs additional information in the
222   // symbol table.  This will only be called if has_make_symbol()
223   // returns true.
224   virtual Sized_symbol<size>*
225   make_symbol() const
226   { gold_unreachable(); }
227
228   // Resolve a symbol for the target.  This should be overridden by a
229   // target which needs to take special action.  TO is the
230   // pre-existing symbol.  SYM is the new symbol, seen in OBJECT.
231   // VERSION is the version of SYM.  This will only be called if
232   // has_resolve() returns true.
233   virtual void
234   resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
235           const char*)
236   { gold_unreachable(); }
237
238   // Scan the relocs for a section, and record any information
239   // required for the symbol.  OPTIONS is the command line options.
240   // SYMTAB is the symbol table.  OBJECT is the object in which the
241   // section appears.  DATA_SHNDX is the section index that these
242   // relocs apply to.  SH_TYPE is the type of the relocation section,
243   // SHT_REL or SHT_RELA.  PRELOCS points to the relocation data.
244   // RELOC_COUNT is the number of relocs.  LOCAL_SYMBOL_COUNT is the
245   // number of local symbols.  OUTPUT_SECTION is the output section.
246   // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
247   // sections are not mapped as usual.  PLOCAL_SYMBOLS points to the
248   // local symbol data from OBJECT.  GLOBAL_SYMBOLS is the array of
249   // pointers to the global symbol table from OBJECT.
250   virtual void
251   scan_relocs(const General_options& options,
252               Symbol_table* symtab,
253               Layout* layout,
254               Sized_relobj<size, big_endian>* object,
255               unsigned int data_shndx,
256               unsigned int sh_type,
257               const unsigned char* prelocs,
258               size_t reloc_count,
259               Output_section* output_section,
260               bool needs_special_offset_handling,
261               size_t local_symbol_count,
262               const unsigned char* plocal_symbols) = 0;
263
264   // Relocate section data.  SH_TYPE is the type of the relocation
265   // section, SHT_REL or SHT_RELA.  PRELOCS points to the relocation
266   // information.  RELOC_COUNT is the number of relocs.
267   // OUTPUT_SECTION is the output section.
268   // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
269   // to correspond to the output section.  VIEW is a view into the
270   // output file holding the section contents, VIEW_ADDRESS is the
271   // virtual address of the view, and VIEW_SIZE is the size of the
272   // view.  If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
273   // parameters refer to the complete output section data, not just
274   // the input section data.
275   virtual void
276   relocate_section(const Relocate_info<size, big_endian>*,
277                    unsigned int sh_type,
278                    const unsigned char* prelocs,
279                    size_t reloc_count,
280                    Output_section* output_section,
281                    bool needs_special_offset_handling,
282                    unsigned char* view,
283                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
284                    off_t view_size) = 0;
285
286  protected:
287   Sized_target(const Target::Target_info* pti)
288     : Target(pti)
289   {
290     gold_assert(pti->size == size);
291     gold_assert(pti->is_big_endian ? big_endian : !big_endian);
292   }
293 };
294
295 } // End namespace gold.
296
297 #endif // !defined(GOLD_TARGET_H)