Add licensing text to every source file.
[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 struct Relocate_info;
47 class Symbol;
48 template<int size>
49 class Sized_symbol;
50 class Symbol_table;
51
52 // The abstract class for target specific handling.
53
54 class Target
55 {
56  public:
57   virtual ~Target()
58   { }
59
60   // Return the bit size that this target implements.  This should
61   // return 32 or 64.
62   int
63   get_size() const
64   { return this->pti_->size; }
65
66   // Return whether this target is big-endian.
67   bool
68   is_big_endian() const
69   { return this->pti_->is_big_endian; }
70
71   // Machine code to store in e_machine field of ELF header.
72   elfcpp::EM
73   machine_code() const
74   { return this->pti_->machine_code; }
75
76   // Whether this target has a specific make_symbol function.
77   bool
78   has_make_symbol() const
79   { return this->pti_->has_make_symbol; }
80
81   // Whether this target has a specific resolve function.
82   bool
83   has_resolve() const
84   { return this->pti_->has_resolve; }
85
86   // Whether this target has a specific code fill function.
87   bool
88   has_code_fill() const
89   { return this->pti_->has_code_fill; }
90
91   // Return the default name of the dynamic linker.
92   const char*
93   dynamic_linker() const
94   { return this->pti_->dynamic_linker; }
95
96   // Return the default address to use for the text segment.
97   uint64_t
98   text_segment_address() const
99   { return this->pti_->text_segment_address; }
100
101   // Return the ABI specified page size.
102   uint64_t
103   abi_pagesize() const
104   { return this->pti_->abi_pagesize; }
105
106   // Return the common page size used on actual systems.
107   uint64_t
108   common_pagesize() const
109   { return this->pti_->common_pagesize; }
110
111   // This is called to tell the target to complete any sections it is
112   // handling.  After this all sections must have their final size.
113   void
114   finalize_sections(Layout* layout)
115   { return this->do_finalize_sections(layout); }
116
117   // Return a string to use to fill out a code section.  This is
118   // basically one or more NOPS which must fill out the specified
119   // length in bytes.
120   std::string
121   code_fill(off_t length)
122   { return this->do_code_fill(length); }
123
124  protected:
125   // This struct holds the constant information for a child class.  We
126   // use a struct to avoid the overhead of virtual function calls for
127   // simple information.
128   struct Target_info
129   {
130     // Address size (32 or 64).
131     int size;
132     // Whether the target is big endian.
133     bool is_big_endian;
134     // The code to store in the e_machine field of the ELF header.
135     elfcpp::EM machine_code;
136     // Whether this target has a specific make_symbol function.
137     bool has_make_symbol;
138     // Whether this target has a specific resolve function.
139     bool has_resolve;
140     // Whether this target has a specific code fill function.
141     bool has_code_fill;
142     // The default dynamic linker name.
143     const char* dynamic_linker;
144     // The default text segment address.
145     uint64_t text_segment_address;
146     // The ABI specified page size.
147     uint64_t abi_pagesize;
148     // The common page size used by actual implementations.
149     uint64_t common_pagesize;
150   };
151
152   Target(const Target_info* pti)
153     : pti_(pti)
154   { }
155
156   // Virtual function which may be implemented by the child class.
157   virtual void
158   do_finalize_sections(Layout*)
159   { }
160
161   // Virtual function which must be implemented by the child class if
162   // needed.
163   virtual std::string
164   do_code_fill(off_t)
165   { gold_unreachable(); }
166
167  private:
168   Target(const Target&);
169   Target& operator=(const Target&);
170
171   // The target information.
172   const Target_info* pti_;
173 };
174
175 // The abstract class for a specific size and endianness of target.
176 // Each actual target implementation class should derive from an
177 // instantiation of Sized_target.
178
179 template<int size, bool big_endian>
180 class Sized_target : public Target
181 {
182  public:
183   // Make a new symbol table entry for the target.  This should be
184   // overridden by a target which needs additional information in the
185   // symbol table.  This will only be called if has_make_symbol()
186   // returns true.
187   virtual Sized_symbol<size>*
188   make_symbol() const
189   { gold_unreachable(); }
190
191   // Resolve a symbol for the target.  This should be overridden by a
192   // target which needs to take special action.  TO is the
193   // pre-existing symbol.  SYM is the new symbol, seen in OBJECT.
194   // VERSION is the version of SYM.  This will only be called if
195   // has_resolve() returns true.
196   virtual void
197   resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
198           const char*)
199   { gold_unreachable(); }
200
201   // Scan the relocs for a section, and record any information
202   // required for the symbol.  OPTIONS is the command line options.
203   // SYMTAB is the symbol table.  OBJECT is the object in which the
204   // section appears.  DATA_SHNDX is the section index that these
205   // relocs apply to.  SH_TYPE is the type of the relocation section,
206   // SHT_REL or SHT_RELA.  PRELOCS points to the relocation data.
207   // RELOC_COUNT is the number of relocs.  LOCAL_SYMBOL_COUNT is the
208   // number of local symbols.  PLOCAL_SYMBOLS points to the local
209   // symbol data from OBJECT.  GLOBAL_SYMBOLS is the array of pointers
210   // to the global symbol table from OBJECT.
211   virtual void
212   scan_relocs(const General_options& options,
213               Symbol_table* symtab,
214               Layout* layout,
215               Sized_relobj<size, big_endian>* object,
216               unsigned int data_shndx,
217               unsigned int sh_type,
218               const unsigned char* prelocs,
219               size_t reloc_count,
220               size_t local_symbol_count,
221               const unsigned char* plocal_symbols,
222               Symbol** global_symbols) = 0;
223
224   // Relocate section data.  SH_TYPE is the type of the relocation
225   // section, SHT_REL or SHT_RELA.  PRELOCS points to the relocation
226   // information.  RELOC_COUNT is the number of relocs.  VIEW is a
227   // view into the output file holding the section contents,
228   // VIEW_ADDRESS is the virtual address of the view, and VIEW_SIZE is
229   // the size of the view.
230   virtual void
231   relocate_section(const Relocate_info<size, big_endian>*,
232                    unsigned int sh_type,
233                    const unsigned char* prelocs,
234                    size_t reloc_count,
235                    unsigned char* view,
236                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
237                    off_t view_size) = 0;
238
239  protected:
240   Sized_target(const Target::Target_info* pti)
241     : Target(pti)
242   {
243     gold_assert(pti->size == size);
244     gold_assert(pti->is_big_endian ? big_endian : !big_endian);
245   }
246 };
247
248 } // End namespace gold.
249
250 #endif // !defined(GOLD_TARGET_H)