More dynamic object support, initial scripting support.
[external/binutils.git] / gold / target.h
1 // target.h -- target support for gold   -*- C++ -*-
2
3 // The abstract class Target is the interface for target specific
4 // support.  It defines abstract methods which each target must
5 // implement.  Typically there will be one target per processor, but
6 // in some cases it may be necessary to have subclasses.
7
8 // For speed and consistency we want to use inline functions to handle
9 // relocation processing.  So besides implementations of the abstract
10 // methods, each target is expected to define a template
11 // specialization of the relocation functions.
12
13 #ifndef GOLD_TARGET_H
14 #define GOLD_TARGET_H
15
16 #include <cassert>
17
18 #include "elfcpp.h"
19
20 namespace gold
21 {
22
23 class General_options;
24 class Object;
25 template<int size, bool big_endian>
26 class Sized_relobj;
27 template<int size, bool big_endian>
28 struct Relocate_info;
29 class Symbol;
30 template<int size>
31 class Sized_symbol;
32 class Symbol_table;
33
34 // The abstract class for target specific handling.
35
36 class Target
37 {
38  public:
39   virtual ~Target()
40   { }
41
42   // Return the bit size that this target implements.  This should
43   // return 32 or 64.
44   int
45   get_size() const
46   { return this->pti_->size; }
47
48   // Return whether this target is big-endian.
49   bool
50   is_big_endian() const
51   { return this->pti_->is_big_endian; }
52
53   // Machine code to store in e_machine field of ELF header.
54   elfcpp::EM
55   machine_code() const
56   { return this->pti_->machine_code; }
57
58   // Whether this target has a specific make_symbol function.
59   bool
60   has_make_symbol() const
61   { return this->pti_->has_make_symbol; }
62
63   // Whether this target has a specific resolve function.
64   bool
65   has_resolve() const
66   { return this->pti_->has_resolve; }
67
68   // Return the default name of the dynamic linker.
69   const char*
70   dynamic_linker() const
71   { return this->pti_->dynamic_linker; }
72
73   // Return the default address to use for the text segment.
74   uint64_t
75   text_segment_address() const
76   { return this->pti_->text_segment_address; }
77
78   // Return the ABI specified page size.
79   uint64_t
80   abi_pagesize() const
81   { return this->pti_->abi_pagesize; }
82
83   // Return the common page size used on actual systems.
84   uint64_t
85   common_pagesize() const
86   { return this->pti_->common_pagesize; }
87
88  protected:
89   // This struct holds the constant information for a child class.  We
90   // use a struct to avoid the overhead of virtual function calls for
91   // simple information.
92   struct Target_info
93   {
94     // Address size (32 or 64).
95     int size;
96     // Whether the target is big endian.
97     bool is_big_endian;
98     // The code to store in the e_machine field of the ELF header.
99     elfcpp::EM machine_code;
100     // Whether this target has a specific make_symbol function.
101     bool has_make_symbol;
102     // Whether this target has a specific resolve function.
103     bool has_resolve;
104     // The default dynamic linker name.
105     const char* dynamic_linker;
106     // The default text segment address.
107     uint64_t text_segment_address;
108     // The ABI specified page size.
109     uint64_t abi_pagesize;
110     // The common page size used by actual implementations.
111     uint64_t common_pagesize;
112   };
113
114   Target(const Target_info* pti)
115     : pti_(pti)
116   { }
117
118  private:
119   Target(const Target&);
120   Target& operator=(const Target&);
121
122   // The target information.
123   const Target_info* pti_;
124 };
125
126 // The abstract class for a specific size and endianness of target.
127 // Each actual target implementation class should derive from an
128 // instantiation of Sized_target.
129
130 template<int size, bool big_endian>
131 class Sized_target : public Target
132 {
133  public:
134   // Make a new symbol table entry for the target.  This should be
135   // overridden by a target which needs additional information in the
136   // symbol table.  This will only be called if has_make_symbol()
137   // returns true.
138   virtual Sized_symbol<size>*
139   make_symbol()
140   { abort(); }
141
142   // Resolve a symbol for the target.  This should be overridden by a
143   // target which needs to take special action.  TO is the
144   // pre-existing symbol.  SYM is the new symbol, seen in OBJECT.
145   // This will only be called if has_resolve() returns true.
146   virtual void
147   resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*)
148   { abort(); }
149
150   // Scan the relocs for a section, and record any information
151   // required for the symbol.  OPTIONS is the command line options.
152   // SYMTAB is the symbol table.  OBJECT is the object in which the
153   // section appears.  SH_TYPE is the type of the relocation section,
154   // SHT_REL or SHT_RELA.  PRELOCS points to the relocation data.
155   // RELOC_COUNT is the number of relocs.  LOCAL_SYMBOL_COUNT is the
156   // number of local symbols.  PLOCAL_SYMBOLS points to the local
157   // symbol data from OBJECT.  GLOBAL_SYMBOLS is the array of pointers
158   // to the global symbol table from OBJECT.
159   virtual void
160   scan_relocs(const General_options& options,
161               Symbol_table* symtab,
162               Layout* layout,
163               Sized_relobj<size, big_endian>* object,
164               unsigned int sh_type,
165               const unsigned char* prelocs,
166               size_t reloc_count,
167               size_t local_symbol_count,
168               const unsigned char* plocal_symbols,
169               Symbol** global_symbols) = 0;
170
171   // Relocate section data.  SH_TYPE is the type of the relocation
172   // section, SHT_REL or SHT_RELA.  PRELOCS points to the relocation
173   // information.  RELOC_COUNT is the number of relocs.  VIEW is a
174   // view into the output file holding the section contents,
175   // VIEW_ADDRESS is the virtual address of the view, and VIEW_SIZE is
176   // the size of the view.
177   virtual void
178   relocate_section(const Relocate_info<size, big_endian>*,
179                    unsigned int sh_type,
180                    const unsigned char* prelocs,
181                    size_t reloc_count,
182                    unsigned char* view,
183                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
184                    off_t view_size) = 0;
185
186  protected:
187   Sized_target(const Target::Target_info* pti)
188     : Target(pti)
189   {
190     assert(pti->size == size);
191     assert(pti->is_big_endian ? big_endian : !big_endian);
192   }
193 };
194
195 } // End namespace gold.
196
197 #endif // !defined(GOLD_TARGET_H)