* elflink.c (bfd_elf_final_link): Move code reading relocs to..
[binutils.git] / gold / target.h
blob460ac96ecff65a4892760a429a83a87e42805666
1 // target.h -- target support for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
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.
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.
33 #ifndef GOLD_TARGET_H
34 #define GOLD_TARGET_H
36 #include "elfcpp.h"
37 #include "options.h"
38 #include "parameters.h"
40 namespace gold
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;
56 // The abstract class for target specific handling.
58 class Target
60 public:
61 virtual ~Target()
62 { }
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; }
70 // Return whether this target is big-endian.
71 bool
72 is_big_endian() const
73 { return this->pti_->is_big_endian; }
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; }
80 // Whether this target has a specific make_symbol function.
81 bool
82 has_make_symbol() const
83 { return this->pti_->has_make_symbol; }
85 // Whether this target has a specific resolve function.
86 bool
87 has_resolve() const
88 { return this->pti_->has_resolve; }
90 // Whether this target has a specific code fill function.
91 bool
92 has_code_fill() const
93 { return this->pti_->has_code_fill; }
95 // Return the default name of the dynamic linker.
96 const char*
97 dynamic_linker() const
98 { return this->pti_->dynamic_linker; }
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; }
105 // Return the ABI specified page size.
106 uint64_t
107 abi_pagesize() const
109 if (parameters->options().max_page_size() > 0)
110 return parameters->options().max_page_size();
111 else
112 return this->pti_->abi_pagesize;
115 // Return the common page size used on actual systems.
116 uint64_t
117 common_pagesize() const
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());
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; }
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; }
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); }
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); }
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); }
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); }
170 protected:
171 // This struct holds the constant information for a child class. We
172 // use a struct to avoid the overhead of virtual function calls for
173 // simple information.
174 struct Target_info
176 // Address size (32 or 64).
177 int size;
178 // Whether the target is big endian.
179 bool is_big_endian;
180 // The code to store in the e_machine field of the ELF header.
181 elfcpp::EM machine_code;
182 // Whether this target has a specific make_symbol function.
183 bool has_make_symbol;
184 // Whether this target has a specific resolve function.
185 bool has_resolve;
186 // Whether this target has a specific code fill function.
187 bool has_code_fill;
188 // Whether an object file with no .note.GNU-stack sections implies
189 // that the stack should be executable.
190 bool is_default_stack_executable;
191 // Prefix character to strip when checking for wrapping.
192 char wrap_char;
193 // The default dynamic linker name.
194 const char* dynamic_linker;
195 // The default text segment address.
196 uint64_t default_text_segment_address;
197 // The ABI specified page size.
198 uint64_t abi_pagesize;
199 // The common page size used by actual implementations.
200 uint64_t common_pagesize;
203 Target(const Target_info* pti)
204 : pti_(pti)
207 // Virtual function which may be implemented by the child class.
208 virtual void
209 do_finalize_sections(Layout*)
212 // Virtual function which may be implemented by the child class.
213 virtual uint64_t
214 do_dynsym_value(const Symbol*) const
215 { gold_unreachable(); }
217 // Virtual function which must be implemented by the child class if
218 // needed.
219 virtual std::string
220 do_code_fill(section_size_type) const
221 { gold_unreachable(); }
223 // Virtual function which may be implemented by the child class.
224 virtual bool
225 do_is_defined_by_abi(const Symbol*) const
226 { return false; }
228 private:
229 Target(const Target&);
230 Target& operator=(const Target&);
232 // The target information.
233 const Target_info* pti_;
236 // The abstract class for a specific size and endianness of target.
237 // Each actual target implementation class should derive from an
238 // instantiation of Sized_target.
240 template<int size, bool big_endian>
241 class Sized_target : public Target
243 public:
244 // Make a new symbol table entry for the target. This should be
245 // overridden by a target which needs additional information in the
246 // symbol table. This will only be called if has_make_symbol()
247 // returns true.
248 virtual Sized_symbol<size>*
249 make_symbol() const
250 { gold_unreachable(); }
252 // Resolve a symbol for the target. This should be overridden by a
253 // target which needs to take special action. TO is the
254 // pre-existing symbol. SYM is the new symbol, seen in OBJECT.
255 // VERSION is the version of SYM. This will only be called if
256 // has_resolve() returns true.
257 virtual void
258 resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
259 const char*)
260 { gold_unreachable(); }
262 // Scan the relocs for a section, and record any information
263 // required for the symbol. OPTIONS is the command line options.
264 // SYMTAB is the symbol table. OBJECT is the object in which the
265 // section appears. DATA_SHNDX is the section index that these
266 // relocs apply to. SH_TYPE is the type of the relocation section,
267 // SHT_REL or SHT_RELA. PRELOCS points to the relocation data.
268 // RELOC_COUNT is the number of relocs. LOCAL_SYMBOL_COUNT is the
269 // number of local symbols. OUTPUT_SECTION is the output section.
270 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
271 // sections are not mapped as usual. PLOCAL_SYMBOLS points to the
272 // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of
273 // pointers to the global symbol table from OBJECT.
274 virtual void
275 scan_relocs(const General_options& options,
276 Symbol_table* symtab,
277 Layout* layout,
278 Sized_relobj<size, big_endian>* object,
279 unsigned int data_shndx,
280 unsigned int sh_type,
281 const unsigned char* prelocs,
282 size_t reloc_count,
283 Output_section* output_section,
284 bool needs_special_offset_handling,
285 size_t local_symbol_count,
286 const unsigned char* plocal_symbols) = 0;
288 // Relocate section data. SH_TYPE is the type of the relocation
289 // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation
290 // information. RELOC_COUNT is the number of relocs.
291 // OUTPUT_SECTION is the output section.
292 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
293 // to correspond to the output section. VIEW is a view into the
294 // output file holding the section contents, VIEW_ADDRESS is the
295 // virtual address of the view, and VIEW_SIZE is the size of the
296 // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
297 // parameters refer to the complete output section data, not just
298 // the input section data.
299 virtual void
300 relocate_section(const Relocate_info<size, big_endian>*,
301 unsigned int sh_type,
302 const unsigned char* prelocs,
303 size_t reloc_count,
304 Output_section* output_section,
305 bool needs_special_offset_handling,
306 unsigned char* view,
307 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
308 section_size_type view_size) = 0;
310 // Scan the relocs during a relocatable link. The parameters are
311 // like scan_relocs, with an additional Relocatable_relocs
312 // parameter, used to record the disposition of the relocs.
313 virtual void
314 scan_relocatable_relocs(const General_options& options,
315 Symbol_table* symtab,
316 Layout* layout,
317 Sized_relobj<size, big_endian>* object,
318 unsigned int data_shndx,
319 unsigned int sh_type,
320 const unsigned char* prelocs,
321 size_t reloc_count,
322 Output_section* output_section,
323 bool needs_special_offset_handling,
324 size_t local_symbol_count,
325 const unsigned char* plocal_symbols,
326 Relocatable_relocs*) = 0;
328 // Relocate a section during a relocatable link. The parameters are
329 // like relocate_section, with additional parameters for the view of
330 // the output reloc section.
331 virtual void
332 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
333 unsigned int sh_type,
334 const unsigned char* prelocs,
335 size_t reloc_count,
336 Output_section* output_section,
337 off_t offset_in_output_section,
338 const Relocatable_relocs*,
339 unsigned char* view,
340 typename elfcpp::Elf_types<size>::Elf_Addr
341 view_address,
342 section_size_type view_size,
343 unsigned char* reloc_view,
344 section_size_type reloc_view_size) = 0;
346 protected:
347 Sized_target(const Target::Target_info* pti)
348 : Target(pti)
350 gold_assert(pti->size == size);
351 gold_assert(pti->is_big_endian ? big_endian : !big_endian);
355 } // End namespace gold.
357 #endif // !defined(GOLD_TARGET_H)