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.
38 #include "parameters.h"
44 class General_options;
47 template<int size, bool big_endian>
49 class Relocatable_relocs;
50 template<int size, bool big_endian>
52 class Reloc_symbol_changes;
59 // The abstract class for target specific handling.
67 // Return the bit size that this target implements. This should
71 { return this->pti_->size; }
73 // Return whether this target is big-endian.
76 { return this->pti_->is_big_endian; }
78 // Machine code to store in e_machine field of ELF header.
81 { return this->pti_->machine_code; }
83 // Whether this target has a specific make_symbol function.
85 has_make_symbol() const
86 { return this->pti_->has_make_symbol; }
88 // Whether this target has a specific resolve function.
91 { return this->pti_->has_resolve; }
93 // Whether this target has a specific code fill function.
96 { return this->pti_->has_code_fill; }
98 // Return the default name of the dynamic linker.
100 dynamic_linker() const
101 { return this->pti_->dynamic_linker; }
103 // Return the default address to use for the text segment.
105 default_text_segment_address() const
106 { return this->pti_->default_text_segment_address; }
108 // Return the ABI specified page size.
112 if (parameters->options().max_page_size() > 0)
113 return parameters->options().max_page_size();
115 return this->pti_->abi_pagesize;
118 // Return the common page size used on actual systems.
120 common_pagesize() const
122 if (parameters->options().common_page_size() > 0)
123 return std::min(parameters->options().common_page_size(),
124 this->abi_pagesize());
126 return std::min(this->pti_->common_pagesize,
127 this->abi_pagesize());
130 // If we see some object files with .note.GNU-stack sections, and
131 // some objects files without them, this returns whether we should
132 // consider the object files without them to imply that the stack
133 // should be executable.
135 is_default_stack_executable() const
136 { return this->pti_->is_default_stack_executable; }
138 // Return a character which may appear as a prefix for a wrap
139 // symbol. If this character appears, we strip it when checking for
140 // wrapping and add it back when forming the final symbol name.
141 // This should be '\0' if not special prefix is required, which is
145 { return this->pti_->wrap_char; }
147 // Return the special section index which indicates a small common
148 // symbol. This will return SHN_UNDEF if there are no small common
151 small_common_shndx() const
152 { return this->pti_->small_common_shndx; }
154 // Return values to add to the section flags for the section holding
155 // small common symbols.
157 small_common_section_flags() const
159 gold_assert(this->pti_->small_common_shndx != elfcpp::SHN_UNDEF);
160 return this->pti_->small_common_section_flags;
163 // Return the special section index which indicates a large common
164 // symbol. This will return SHN_UNDEF if there are no large common
167 large_common_shndx() const
168 { return this->pti_->large_common_shndx; }
170 // Return values to add to the section flags for the section holding
171 // large common symbols.
173 large_common_section_flags() const
175 gold_assert(this->pti_->large_common_shndx != elfcpp::SHN_UNDEF);
176 return this->pti_->large_common_section_flags;
179 // This hook is called when an output section is created.
181 new_output_section(Output_section* os) const
182 { this->do_new_output_section(os); }
184 // This is called to tell the target to complete any sections it is
185 // handling. After this all sections must have their final size.
187 finalize_sections(Layout* layout)
188 { return this->do_finalize_sections(layout); }
190 // Return the value to use for a global symbol which needs a special
191 // value in the dynamic symbol table. This will only be called if
192 // the backend first calls symbol->set_needs_dynsym_value().
194 dynsym_value(const Symbol* sym) const
195 { return this->do_dynsym_value(sym); }
197 // Return a string to use to fill out a code section. This is
198 // basically one or more NOPS which must fill out the specified
201 code_fill(section_size_type length) const
202 { return this->do_code_fill(length); }
204 // Return whether SYM is known to be defined by the ABI. This is
205 // used to avoid inappropriate warnings about undefined symbols.
207 is_defined_by_abi(const Symbol* sym) const
208 { return this->do_is_defined_by_abi(sym); }
210 // Adjust the output file header before it is written out. VIEW
211 // points to the header in external form. LEN is the length.
213 adjust_elf_header(unsigned char* view, int len) const
214 { return this->do_adjust_elf_header(view, len); }
216 // Return whether NAME is a local label name. This is used to implement the
217 // --discard-locals options.
219 is_local_label_name(const char* name) const
220 { return this->do_is_local_label_name(name); }
222 // A function starts at OFFSET in section SHNDX in OBJECT. That
223 // function was compiled with -fsplit-stack, but it refers to a
224 // function which was compiled without -fsplit-stack. VIEW is a
225 // modifiable view of the section; VIEW_SIZE is the size of the
226 // view. The target has to adjust the function so that it allocates
229 calls_non_split(Relobj* object, unsigned int shndx,
230 section_offset_type fnoffset, section_size_type fnsize,
231 unsigned char* view, section_size_type view_size,
232 std::string* from, std::string* to) const
234 this->do_calls_non_split(object, shndx, fnoffset, fnsize, view, view_size,
238 // Make an ELF object.
239 template<int size, bool big_endian>
241 make_elf_object(const std::string& name, Input_file* input_file,
242 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
243 { return this->do_make_elf_object(name, input_file, offset, ehdr); }
245 // Return true if target wants to perform relaxation.
249 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
250 if (is_debugging_enabled(DEBUG_RELAXATION))
253 return this->do_may_relax();
256 // Perform a relaxation pass. Return true if layout may be changed.
260 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
261 if (is_debugging_enabled(DEBUG_RELAXATION))
264 return this->do_relax(pass);
268 // This struct holds the constant information for a child class. We
269 // use a struct to avoid the overhead of virtual function calls for
270 // simple information.
273 // Address size (32 or 64).
275 // Whether the target is big endian.
277 // The code to store in the e_machine field of the ELF header.
278 elfcpp::EM machine_code;
279 // Whether this target has a specific make_symbol function.
280 bool has_make_symbol;
281 // Whether this target has a specific resolve function.
283 // Whether this target has a specific code fill function.
285 // Whether an object file with no .note.GNU-stack sections implies
286 // that the stack should be executable.
287 bool is_default_stack_executable;
288 // Prefix character to strip when checking for wrapping.
290 // The default dynamic linker name.
291 const char* dynamic_linker;
292 // The default text segment address.
293 uint64_t default_text_segment_address;
294 // The ABI specified page size.
295 uint64_t abi_pagesize;
296 // The common page size used by actual implementations.
297 uint64_t common_pagesize;
298 // The special section index for small common symbols; SHN_UNDEF
300 elfcpp::Elf_Half small_common_shndx;
301 // The special section index for large common symbols; SHN_UNDEF
303 elfcpp::Elf_Half large_common_shndx;
304 // Section flags for small common section.
305 elfcpp::Elf_Xword small_common_section_flags;
306 // Section flags for large common section.
307 elfcpp::Elf_Xword large_common_section_flags;
310 Target(const Target_info* pti)
314 // Virtual function which may be implemented by the child class.
316 do_new_output_section(Output_section*) const
319 // Virtual function which may be implemented by the child class.
321 do_finalize_sections(Layout*)
324 // Virtual function which may be implemented by the child class.
326 do_dynsym_value(const Symbol*) const
327 { gold_unreachable(); }
329 // Virtual function which must be implemented by the child class if
332 do_code_fill(section_size_type) const
333 { gold_unreachable(); }
335 // Virtual function which may be implemented by the child class.
337 do_is_defined_by_abi(const Symbol*) const
340 // Adjust the output file header before it is written out. VIEW
341 // points to the header in external form. LEN is the length, and
342 // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
343 // By default, we do nothing.
345 do_adjust_elf_header(unsigned char*, int) const
348 // Virtual function which may be overriden by the child class.
350 do_is_local_label_name(const char*) const;
352 // Virtual function which may be overridden by the child class.
354 do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
355 section_size_type, unsigned char*, section_size_type,
356 std::string*, std::string*) const;
358 // make_elf_object hooks. There are four versions of these for
359 // different address sizes and endianities.
361 #ifdef HAVE_TARGET_32_LITTLE
362 // Virtual functions which may be overriden by the child class.
364 do_make_elf_object(const std::string&, Input_file*, off_t,
365 const elfcpp::Ehdr<32, false>&);
368 #ifdef HAVE_TARGET_32_BIG
369 // Virtual functions which may be overriden by the child class.
371 do_make_elf_object(const std::string&, Input_file*, off_t,
372 const elfcpp::Ehdr<32, true>&);
375 #ifdef HAVE_TARGET_64_LITTLE
376 // Virtual functions which may be overriden by the child class.
378 do_make_elf_object(const std::string&, Input_file*, off_t,
379 const elfcpp::Ehdr<64, false>& ehdr);
382 #ifdef HAVE_TARGET_64_BIG
383 // Virtual functions which may be overriden by the child class.
385 do_make_elf_object(const std::string& name, Input_file* input_file,
386 off_t offset, const elfcpp::Ehdr<64, true>& ehdr);
389 // Virtual function which may be overriden by the child class.
392 { return parameters->options().relax(); }
394 // Virtual function which may be overriden by the child class.
399 // A function for targets to call. Return whether BYTES/LEN matches
400 // VIEW/VIEW_SIZE at OFFSET.
402 match_view(const unsigned char* view, section_size_type view_size,
403 section_offset_type offset, const char* bytes, size_t len) const;
405 // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
408 set_view_to_nop(unsigned char* view, section_size_type view_size,
409 section_offset_type offset, size_t len) const;
412 // The implementations of the four do_make_elf_object virtual functions are
413 // almost identical except for their sizes and endianity. We use a template.
414 // for their implementations.
415 template<int size, bool big_endian>
417 do_make_elf_object_implementation(const std::string&, Input_file*, off_t,
418 const elfcpp::Ehdr<size, big_endian>&);
420 Target(const Target&);
421 Target& operator=(const Target&);
423 // The target information.
424 const Target_info* pti_;
427 // The abstract class for a specific size and endianness of target.
428 // Each actual target implementation class should derive from an
429 // instantiation of Sized_target.
431 template<int size, bool big_endian>
432 class Sized_target : public Target
435 // Make a new symbol table entry for the target. This should be
436 // overridden by a target which needs additional information in the
437 // symbol table. This will only be called if has_make_symbol()
439 virtual Sized_symbol<size>*
441 { gold_unreachable(); }
443 // Resolve a symbol for the target. This should be overridden by a
444 // target which needs to take special action. TO is the
445 // pre-existing symbol. SYM is the new symbol, seen in OBJECT.
446 // VERSION is the version of SYM. This will only be called if
447 // has_resolve() returns true.
449 resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
451 { gold_unreachable(); }
453 // Process the relocs for a section, and record information of the
454 // mapping from source to destination sections. This mapping is later
455 // used to determine unreferenced garbage sections. This procedure is
456 // only called during garbage collection.
458 gc_process_relocs(const General_options& options,
459 Symbol_table* symtab,
461 Sized_relobj<size, big_endian>* object,
462 unsigned int data_shndx,
463 unsigned int sh_type,
464 const unsigned char* prelocs,
466 Output_section* output_section,
467 bool needs_special_offset_handling,
468 size_t local_symbol_count,
469 const unsigned char* plocal_symbols) = 0;
471 // Scan the relocs for a section, and record any information
472 // required for the symbol. OPTIONS is the command line options.
473 // SYMTAB is the symbol table. OBJECT is the object in which the
474 // section appears. DATA_SHNDX is the section index that these
475 // relocs apply to. SH_TYPE is the type of the relocation section,
476 // SHT_REL or SHT_RELA. PRELOCS points to the relocation data.
477 // RELOC_COUNT is the number of relocs. LOCAL_SYMBOL_COUNT is the
478 // number of local symbols. OUTPUT_SECTION is the output section.
479 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
480 // sections are not mapped as usual. PLOCAL_SYMBOLS points to the
481 // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of
482 // pointers to the global symbol table from OBJECT.
484 scan_relocs(const General_options& options,
485 Symbol_table* symtab,
487 Sized_relobj<size, big_endian>* object,
488 unsigned int data_shndx,
489 unsigned int sh_type,
490 const unsigned char* prelocs,
492 Output_section* output_section,
493 bool needs_special_offset_handling,
494 size_t local_symbol_count,
495 const unsigned char* plocal_symbols) = 0;
497 // Relocate section data. SH_TYPE is the type of the relocation
498 // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation
499 // information. RELOC_COUNT is the number of relocs.
500 // OUTPUT_SECTION is the output section.
501 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
502 // to correspond to the output section. VIEW is a view into the
503 // output file holding the section contents, VIEW_ADDRESS is the
504 // virtual address of the view, and VIEW_SIZE is the size of the
505 // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
506 // parameters refer to the complete output section data, not just
507 // the input section data.
509 relocate_section(const Relocate_info<size, big_endian>*,
510 unsigned int sh_type,
511 const unsigned char* prelocs,
513 Output_section* output_section,
514 bool needs_special_offset_handling,
516 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
517 section_size_type view_size,
518 const Reloc_symbol_changes*) = 0;
520 // Scan the relocs during a relocatable link. The parameters are
521 // like scan_relocs, with an additional Relocatable_relocs
522 // parameter, used to record the disposition of the relocs.
524 scan_relocatable_relocs(const General_options& options,
525 Symbol_table* symtab,
527 Sized_relobj<size, big_endian>* object,
528 unsigned int data_shndx,
529 unsigned int sh_type,
530 const unsigned char* prelocs,
532 Output_section* output_section,
533 bool needs_special_offset_handling,
534 size_t local_symbol_count,
535 const unsigned char* plocal_symbols,
536 Relocatable_relocs*) = 0;
538 // Relocate a section during a relocatable link. The parameters are
539 // like relocate_section, with additional parameters for the view of
540 // the output reloc section.
542 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
543 unsigned int sh_type,
544 const unsigned char* prelocs,
546 Output_section* output_section,
547 off_t offset_in_output_section,
548 const Relocatable_relocs*,
550 typename elfcpp::Elf_types<size>::Elf_Addr
552 section_size_type view_size,
553 unsigned char* reloc_view,
554 section_size_type reloc_view_size) = 0;
557 Sized_target(const Target::Target_info* pti)
560 gold_assert(pti->size == size);
561 gold_assert(pti->is_big_endian ? big_endian : !big_endian);
565 } // End namespace gold.
567 #endif // !defined(GOLD_TARGET_H)