bfd/
[binutils.git] / gold / target.h
blobac59fb25ec2067a0099cd81c554a5e9ac909f874
1 // target.h -- target support for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 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"
39 #include "debug.h"
41 namespace gold
44 class General_options;
45 class Object;
46 template<int size, bool big_endian>
47 class Sized_relobj;
48 class Relocatable_relocs;
49 template<int size, bool big_endian>
50 class Relocate_info;
51 class Symbol;
52 template<int size>
53 class Sized_symbol;
54 class Symbol_table;
55 class Output_section;
57 // The abstract class for target specific handling.
59 class Target
61 public:
62 virtual ~Target()
63 { }
65 // Return the bit size that this target implements. This should
66 // return 32 or 64.
67 int
68 get_size() const
69 { return this->pti_->size; }
71 // Return whether this target is big-endian.
72 bool
73 is_big_endian() const
74 { return this->pti_->is_big_endian; }
76 // Machine code to store in e_machine field of ELF header.
77 elfcpp::EM
78 machine_code() const
79 { return this->pti_->machine_code; }
81 // Whether this target has a specific make_symbol function.
82 bool
83 has_make_symbol() const
84 { return this->pti_->has_make_symbol; }
86 // Whether this target has a specific resolve function.
87 bool
88 has_resolve() const
89 { return this->pti_->has_resolve; }
91 // Whether this target has a specific code fill function.
92 bool
93 has_code_fill() const
94 { return this->pti_->has_code_fill; }
96 // Return the default name of the dynamic linker.
97 const char*
98 dynamic_linker() const
99 { return this->pti_->dynamic_linker; }
101 // Return the default address to use for the text segment.
102 uint64_t
103 default_text_segment_address() const
104 { return this->pti_->default_text_segment_address; }
106 // Return the ABI specified page size.
107 uint64_t
108 abi_pagesize() const
110 if (parameters->options().max_page_size() > 0)
111 return parameters->options().max_page_size();
112 else
113 return this->pti_->abi_pagesize;
116 // Return the common page size used on actual systems.
117 uint64_t
118 common_pagesize() const
120 if (parameters->options().common_page_size() > 0)
121 return std::min(parameters->options().common_page_size(),
122 this->abi_pagesize());
123 else
124 return std::min(this->pti_->common_pagesize,
125 this->abi_pagesize());
128 // If we see some object files with .note.GNU-stack sections, and
129 // some objects files without them, this returns whether we should
130 // consider the object files without them to imply that the stack
131 // should be executable.
132 bool
133 is_default_stack_executable() const
134 { return this->pti_->is_default_stack_executable; }
136 // Return a character which may appear as a prefix for a wrap
137 // symbol. If this character appears, we strip it when checking for
138 // wrapping and add it back when forming the final symbol name.
139 // This should be '\0' if not special prefix is required, which is
140 // the normal case.
141 char
142 wrap_char() const
143 { return this->pti_->wrap_char; }
145 // Return the special section index which indicates a small common
146 // symbol. This will return SHN_UNDEF if there are no small common
147 // symbols.
148 elfcpp::Elf_Half
149 small_common_shndx() const
150 { return this->pti_->small_common_shndx; }
152 // Return values to add to the section flags for the section holding
153 // small common symbols.
154 elfcpp::Elf_Xword
155 small_common_section_flags() const
157 gold_assert(this->pti_->small_common_shndx != elfcpp::SHN_UNDEF);
158 return this->pti_->small_common_section_flags;
161 // Return the special section index which indicates a large common
162 // symbol. This will return SHN_UNDEF if there are no large common
163 // symbols.
164 elfcpp::Elf_Half
165 large_common_shndx() const
166 { return this->pti_->large_common_shndx; }
168 // Return values to add to the section flags for the section holding
169 // large common symbols.
170 elfcpp::Elf_Xword
171 large_common_section_flags() const
173 gold_assert(this->pti_->large_common_shndx != elfcpp::SHN_UNDEF);
174 return this->pti_->large_common_section_flags;
177 // This hook is called when an output section is created.
178 void
179 new_output_section(Output_section* os) const
180 { this->do_new_output_section(os); }
182 // This is called to tell the target to complete any sections it is
183 // handling. After this all sections must have their final size.
184 void
185 finalize_sections(Layout* layout)
186 { return this->do_finalize_sections(layout); }
188 // Return the value to use for a global symbol which needs a special
189 // value in the dynamic symbol table. This will only be called if
190 // the backend first calls symbol->set_needs_dynsym_value().
191 uint64_t
192 dynsym_value(const Symbol* sym) const
193 { return this->do_dynsym_value(sym); }
195 // Return a string to use to fill out a code section. This is
196 // basically one or more NOPS which must fill out the specified
197 // length in bytes.
198 std::string
199 code_fill(section_size_type length) const
200 { return this->do_code_fill(length); }
202 // Return whether SYM is known to be defined by the ABI. This is
203 // used to avoid inappropriate warnings about undefined symbols.
204 bool
205 is_defined_by_abi(const Symbol* sym) const
206 { return this->do_is_defined_by_abi(sym); }
208 // Adjust the output file header before it is written out. VIEW
209 // points to the header in external form. LEN is the length.
210 void
211 adjust_elf_header(unsigned char* view, int len) const
212 { return this->do_adjust_elf_header(view, len); }
214 // Return whether NAME is a local label name. This is used to implement the
215 // --discard-locals options.
216 bool
217 is_local_label_name(const char* name) const
218 { return this->do_is_local_label_name(name); }
220 // Make an ELF object.
221 template<int size, bool big_endian>
222 Object*
223 make_elf_object(const std::string& name, Input_file* input_file,
224 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
225 { return this->do_make_elf_object(name, input_file, offset, ehdr); }
227 // Return true if target wants to perform relaxation.
228 bool
229 may_relax() const
231 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
232 if (is_debugging_enabled(DEBUG_RELAXATION))
233 return true;
235 return this->do_may_relax();
238 // Perform a relaxation pass. Return true if layout may be changed.
239 bool
240 relax(int pass)
242 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
243 if (is_debugging_enabled(DEBUG_RELAXATION))
244 return pass < 2;
246 return this->do_relax(pass);
249 protected:
250 // This struct holds the constant information for a child class. We
251 // use a struct to avoid the overhead of virtual function calls for
252 // simple information.
253 struct Target_info
255 // Address size (32 or 64).
256 int size;
257 // Whether the target is big endian.
258 bool is_big_endian;
259 // The code to store in the e_machine field of the ELF header.
260 elfcpp::EM machine_code;
261 // Whether this target has a specific make_symbol function.
262 bool has_make_symbol;
263 // Whether this target has a specific resolve function.
264 bool has_resolve;
265 // Whether this target has a specific code fill function.
266 bool has_code_fill;
267 // Whether an object file with no .note.GNU-stack sections implies
268 // that the stack should be executable.
269 bool is_default_stack_executable;
270 // Prefix character to strip when checking for wrapping.
271 char wrap_char;
272 // The default dynamic linker name.
273 const char* dynamic_linker;
274 // The default text segment address.
275 uint64_t default_text_segment_address;
276 // The ABI specified page size.
277 uint64_t abi_pagesize;
278 // The common page size used by actual implementations.
279 uint64_t common_pagesize;
280 // The special section index for small common symbols; SHN_UNDEF
281 // if none.
282 elfcpp::Elf_Half small_common_shndx;
283 // The special section index for large common symbols; SHN_UNDEF
284 // if none.
285 elfcpp::Elf_Half large_common_shndx;
286 // Section flags for small common section.
287 elfcpp::Elf_Xword small_common_section_flags;
288 // Section flags for large common section.
289 elfcpp::Elf_Xword large_common_section_flags;
292 Target(const Target_info* pti)
293 : pti_(pti)
296 // Virtual function which may be implemented by the child class.
297 virtual void
298 do_new_output_section(Output_section*) const
301 // Virtual function which may be implemented by the child class.
302 virtual void
303 do_finalize_sections(Layout*)
306 // Virtual function which may be implemented by the child class.
307 virtual uint64_t
308 do_dynsym_value(const Symbol*) const
309 { gold_unreachable(); }
311 // Virtual function which must be implemented by the child class if
312 // needed.
313 virtual std::string
314 do_code_fill(section_size_type) const
315 { gold_unreachable(); }
317 // Virtual function which may be implemented by the child class.
318 virtual bool
319 do_is_defined_by_abi(const Symbol*) const
320 { return false; }
322 // Adjust the output file header before it is written out. VIEW
323 // points to the header in external form. LEN is the length, and
324 // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
325 // By default, we do nothing.
326 virtual void
327 do_adjust_elf_header(unsigned char*, int) const
330 // Virtual function which may be overriden by the child class.
331 virtual bool
332 do_is_local_label_name(const char*) const;
334 // make_elf_object hooks. There are four versions of these for
335 // different address sizes and endianities.
337 #ifdef HAVE_TARGET_32_LITTLE
338 // Virtual functions which may be overriden by the child class.
339 virtual Object*
340 do_make_elf_object(const std::string&, Input_file*, off_t,
341 const elfcpp::Ehdr<32, false>&);
342 #endif
344 #ifdef HAVE_TARGET_32_BIG
345 // Virtual functions which may be overriden by the child class.
346 virtual Object*
347 do_make_elf_object(const std::string&, Input_file*, off_t,
348 const elfcpp::Ehdr<32, true>&);
349 #endif
351 #ifdef HAVE_TARGET_64_LITTLE
352 // Virtual functions which may be overriden by the child class.
353 virtual Object*
354 do_make_elf_object(const std::string&, Input_file*, off_t,
355 const elfcpp::Ehdr<64, false>& ehdr);
356 #endif
358 #ifdef HAVE_TARGET_64_BIG
359 // Virtual functions which may be overriden by the child class.
360 virtual Object*
361 do_make_elf_object(const std::string& name, Input_file* input_file,
362 off_t offset, const elfcpp::Ehdr<64, true>& ehdr);
363 #endif
365 // Virtual function which may be overriden by the child class.
366 virtual bool
367 do_may_relax() const
368 { return parameters->options().relax(); }
370 // Virtual function which may be overriden by the child class.
371 virtual bool
372 do_relax(int)
373 { return false; }
375 private:
376 // The implementations of the four do_make_elf_object virtual functions are
377 // almost identical except for their sizes and endianity. We use a template.
378 // for their implementations.
379 template<int size, bool big_endian>
380 inline Object*
381 do_make_elf_object_implementation(const std::string&, Input_file*, off_t,
382 const elfcpp::Ehdr<size, big_endian>&);
384 Target(const Target&);
385 Target& operator=(const Target&);
387 // The target information.
388 const Target_info* pti_;
391 // The abstract class for a specific size and endianness of target.
392 // Each actual target implementation class should derive from an
393 // instantiation of Sized_target.
395 template<int size, bool big_endian>
396 class Sized_target : public Target
398 public:
399 // Make a new symbol table entry for the target. This should be
400 // overridden by a target which needs additional information in the
401 // symbol table. This will only be called if has_make_symbol()
402 // returns true.
403 virtual Sized_symbol<size>*
404 make_symbol() const
405 { gold_unreachable(); }
407 // Resolve a symbol for the target. This should be overridden by a
408 // target which needs to take special action. TO is the
409 // pre-existing symbol. SYM is the new symbol, seen in OBJECT.
410 // VERSION is the version of SYM. This will only be called if
411 // has_resolve() returns true.
412 virtual void
413 resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
414 const char*)
415 { gold_unreachable(); }
417 // Process the relocs for a section, and record information of the
418 // mapping from source to destination sections. This mapping is later
419 // used to determine unreferenced garbage sections. This procedure is
420 // only called during garbage collection.
421 virtual void
422 gc_process_relocs(const General_options& options,
423 Symbol_table* symtab,
424 Layout* layout,
425 Sized_relobj<size, big_endian>* object,
426 unsigned int data_shndx,
427 unsigned int sh_type,
428 const unsigned char* prelocs,
429 size_t reloc_count,
430 Output_section* output_section,
431 bool needs_special_offset_handling,
432 size_t local_symbol_count,
433 const unsigned char* plocal_symbols) = 0;
435 // Scan the relocs for a section, and record any information
436 // required for the symbol. OPTIONS is the command line options.
437 // SYMTAB is the symbol table. OBJECT is the object in which the
438 // section appears. DATA_SHNDX is the section index that these
439 // relocs apply to. SH_TYPE is the type of the relocation section,
440 // SHT_REL or SHT_RELA. PRELOCS points to the relocation data.
441 // RELOC_COUNT is the number of relocs. LOCAL_SYMBOL_COUNT is the
442 // number of local symbols. OUTPUT_SECTION is the output section.
443 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
444 // sections are not mapped as usual. PLOCAL_SYMBOLS points to the
445 // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of
446 // pointers to the global symbol table from OBJECT.
447 virtual void
448 scan_relocs(const General_options& options,
449 Symbol_table* symtab,
450 Layout* layout,
451 Sized_relobj<size, big_endian>* object,
452 unsigned int data_shndx,
453 unsigned int sh_type,
454 const unsigned char* prelocs,
455 size_t reloc_count,
456 Output_section* output_section,
457 bool needs_special_offset_handling,
458 size_t local_symbol_count,
459 const unsigned char* plocal_symbols) = 0;
461 // Relocate section data. SH_TYPE is the type of the relocation
462 // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation
463 // information. RELOC_COUNT is the number of relocs.
464 // OUTPUT_SECTION is the output section.
465 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
466 // to correspond to the output section. VIEW is a view into the
467 // output file holding the section contents, VIEW_ADDRESS is the
468 // virtual address of the view, and VIEW_SIZE is the size of the
469 // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
470 // parameters refer to the complete output section data, not just
471 // the input section data.
472 virtual void
473 relocate_section(const Relocate_info<size, big_endian>*,
474 unsigned int sh_type,
475 const unsigned char* prelocs,
476 size_t reloc_count,
477 Output_section* output_section,
478 bool needs_special_offset_handling,
479 unsigned char* view,
480 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
481 section_size_type view_size) = 0;
483 // Scan the relocs during a relocatable link. The parameters are
484 // like scan_relocs, with an additional Relocatable_relocs
485 // parameter, used to record the disposition of the relocs.
486 virtual void
487 scan_relocatable_relocs(const General_options& options,
488 Symbol_table* symtab,
489 Layout* layout,
490 Sized_relobj<size, big_endian>* object,
491 unsigned int data_shndx,
492 unsigned int sh_type,
493 const unsigned char* prelocs,
494 size_t reloc_count,
495 Output_section* output_section,
496 bool needs_special_offset_handling,
497 size_t local_symbol_count,
498 const unsigned char* plocal_symbols,
499 Relocatable_relocs*) = 0;
501 // Relocate a section during a relocatable link. The parameters are
502 // like relocate_section, with additional parameters for the view of
503 // the output reloc section.
504 virtual void
505 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
506 unsigned int sh_type,
507 const unsigned char* prelocs,
508 size_t reloc_count,
509 Output_section* output_section,
510 off_t offset_in_output_section,
511 const Relocatable_relocs*,
512 unsigned char* view,
513 typename elfcpp::Elf_types<size>::Elf_Addr
514 view_address,
515 section_size_type view_size,
516 unsigned char* reloc_view,
517 section_size_type reloc_view_size) = 0;
519 protected:
520 Sized_target(const Target::Target_info* pti)
521 : Target(pti)
523 gold_assert(pti->size == size);
524 gold_assert(pti->is_big_endian ? big_endian : !big_endian);
528 } // End namespace gold.
530 #endif // !defined(GOLD_TARGET_H)