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"
46 template<int size, bool big_endian>
48 class Relocatable_relocs;
49 template<int size, bool big_endian>
51 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 // Processor specific flags to store in e_flags field of ELF header.
85 processor_specific_flags() const
86 { return this->processor_specific_flags_; }
88 // Whether processor specific flags are set at least once.
90 are_processor_specific_flags_set() const
91 { return this->are_processor_specific_flags_set_; }
93 // Whether this target has a specific make_symbol function.
95 has_make_symbol() const
96 { return this->pti_->has_make_symbol; }
98 // Whether this target has a specific resolve function.
101 { return this->pti_->has_resolve; }
103 // Whether this target has a specific code fill function.
105 has_code_fill() const
106 { return this->pti_->has_code_fill; }
108 // Return the default name of the dynamic linker.
110 dynamic_linker() const
111 { return this->pti_->dynamic_linker; }
113 // Return the default address to use for the text segment.
115 default_text_segment_address() const
116 { return this->pti_->default_text_segment_address; }
118 // Return the ABI specified page size.
122 if (parameters->options().max_page_size() > 0)
123 return parameters->options().max_page_size();
125 return this->pti_->abi_pagesize;
128 // Return the common page size used on actual systems.
130 common_pagesize() const
132 if (parameters->options().common_page_size() > 0)
133 return std::min(parameters->options().common_page_size(),
134 this->abi_pagesize());
136 return std::min(this->pti_->common_pagesize,
137 this->abi_pagesize());
140 // If we see some object files with .note.GNU-stack sections, and
141 // some objects files without them, this returns whether we should
142 // consider the object files without them to imply that the stack
143 // should be executable.
145 is_default_stack_executable() const
146 { return this->pti_->is_default_stack_executable; }
148 // Return a character which may appear as a prefix for a wrap
149 // symbol. If this character appears, we strip it when checking for
150 // wrapping and add it back when forming the final symbol name.
151 // This should be '\0' if not special prefix is required, which is
155 { return this->pti_->wrap_char; }
157 // Return the special section index which indicates a small common
158 // symbol. This will return SHN_UNDEF if there are no small common
161 small_common_shndx() const
162 { return this->pti_->small_common_shndx; }
164 // Return values to add to the section flags for the section holding
165 // small common symbols.
167 small_common_section_flags() const
169 gold_assert(this->pti_->small_common_shndx != elfcpp::SHN_UNDEF);
170 return this->pti_->small_common_section_flags;
173 // Return the special section index which indicates a large common
174 // symbol. This will return SHN_UNDEF if there are no large common
177 large_common_shndx() const
178 { return this->pti_->large_common_shndx; }
180 // Return values to add to the section flags for the section holding
181 // large common symbols.
183 large_common_section_flags() const
185 gold_assert(this->pti_->large_common_shndx != elfcpp::SHN_UNDEF);
186 return this->pti_->large_common_section_flags;
189 // This hook is called when an output section is created.
191 new_output_section(Output_section* os) const
192 { this->do_new_output_section(os); }
194 // This is called to tell the target to complete any sections it is
195 // handling. After this all sections must have their final size.
197 finalize_sections(Layout* layout, const Input_objects* input_objects,
198 Symbol_table* symtab)
199 { return this->do_finalize_sections(layout, input_objects, symtab); }
201 // Return the value to use for a global symbol which needs a special
202 // value in the dynamic symbol table. This will only be called if
203 // the backend first calls symbol->set_needs_dynsym_value().
205 dynsym_value(const Symbol* sym) const
206 { return this->do_dynsym_value(sym); }
208 // Return a string to use to fill out a code section. This is
209 // basically one or more NOPS which must fill out the specified
212 code_fill(section_size_type length) const
213 { return this->do_code_fill(length); }
215 // Return whether SYM is known to be defined by the ABI. This is
216 // used to avoid inappropriate warnings about undefined symbols.
218 is_defined_by_abi(const Symbol* sym) const
219 { return this->do_is_defined_by_abi(sym); }
221 // Adjust the output file header before it is written out. VIEW
222 // points to the header in external form. LEN is the length.
224 adjust_elf_header(unsigned char* view, int len) const
225 { return this->do_adjust_elf_header(view, len); }
227 // Return whether NAME is a local label name. This is used to implement the
228 // --discard-locals options.
230 is_local_label_name(const char* name) const
231 { return this->do_is_local_label_name(name); }
233 // A function starts at OFFSET in section SHNDX in OBJECT. That
234 // function was compiled with -fsplit-stack, but it refers to a
235 // function which was compiled without -fsplit-stack. VIEW is a
236 // modifiable view of the section; VIEW_SIZE is the size of the
237 // view. The target has to adjust the function so that it allocates
240 calls_non_split(Relobj* object, unsigned int shndx,
241 section_offset_type fnoffset, section_size_type fnsize,
242 unsigned char* view, section_size_type view_size,
243 std::string* from, std::string* to) const
245 this->do_calls_non_split(object, shndx, fnoffset, fnsize, view, view_size,
249 // Make an ELF object.
250 template<int size, bool big_endian>
252 make_elf_object(const std::string& name, Input_file* input_file,
253 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
254 { return this->do_make_elf_object(name, input_file, offset, ehdr); }
256 // Make an output section.
258 make_output_section(const char* name, elfcpp::Elf_Word type,
259 elfcpp::Elf_Xword flags)
260 { return this->do_make_output_section(name, type, flags); }
262 // Return true if target wants to perform relaxation.
266 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
267 if (is_debugging_enabled(DEBUG_RELAXATION))
270 return this->do_may_relax();
273 // Perform a relaxation pass. Return true if layout may be changed.
275 relax(int pass, const Input_objects* input_objects, Symbol_table* symtab,
278 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
279 if (is_debugging_enabled(DEBUG_RELAXATION))
282 return this->do_relax(pass, input_objects, symtab, layout);
286 // This struct holds the constant information for a child class. We
287 // use a struct to avoid the overhead of virtual function calls for
288 // simple information.
291 // Address size (32 or 64).
293 // Whether the target is big endian.
295 // The code to store in the e_machine field of the ELF header.
296 elfcpp::EM machine_code;
297 // Whether this target has a specific make_symbol function.
298 bool has_make_symbol;
299 // Whether this target has a specific resolve function.
301 // Whether this target has a specific code fill function.
303 // Whether an object file with no .note.GNU-stack sections implies
304 // that the stack should be executable.
305 bool is_default_stack_executable;
306 // Prefix character to strip when checking for wrapping.
308 // The default dynamic linker name.
309 const char* dynamic_linker;
310 // The default text segment address.
311 uint64_t default_text_segment_address;
312 // The ABI specified page size.
313 uint64_t abi_pagesize;
314 // The common page size used by actual implementations.
315 uint64_t common_pagesize;
316 // The special section index for small common symbols; SHN_UNDEF
318 elfcpp::Elf_Half small_common_shndx;
319 // The special section index for large common symbols; SHN_UNDEF
321 elfcpp::Elf_Half large_common_shndx;
322 // Section flags for small common section.
323 elfcpp::Elf_Xword small_common_section_flags;
324 // Section flags for large common section.
325 elfcpp::Elf_Xword large_common_section_flags;
328 Target(const Target_info* pti)
329 : pti_(pti), processor_specific_flags_(0),
330 are_processor_specific_flags_set_(false)
333 // Virtual function which may be implemented by the child class.
335 do_new_output_section(Output_section*) const
338 // Virtual function which may be implemented by the child class.
340 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*)
343 // Virtual function which may be implemented by the child class.
345 do_dynsym_value(const Symbol*) const
346 { gold_unreachable(); }
348 // Virtual function which must be implemented by the child class if
351 do_code_fill(section_size_type) const
352 { gold_unreachable(); }
354 // Virtual function which may be implemented by the child class.
356 do_is_defined_by_abi(const Symbol*) const
359 // Adjust the output file header before it is written out. VIEW
360 // points to the header in external form. LEN is the length, and
361 // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
362 // By default, we do nothing.
364 do_adjust_elf_header(unsigned char*, int) const
367 // Virtual function which may be overriden by the child class.
369 do_is_local_label_name(const char*) const;
371 // Virtual function which may be overridden by the child class.
373 do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
374 section_size_type, unsigned char*, section_size_type,
375 std::string*, std::string*) const;
377 // make_elf_object hooks. There are four versions of these for
378 // different address sizes and endianities.
380 // Set processor specific flags.
382 set_processor_specific_flags(elfcpp::Elf_Word flags)
384 this->processor_specific_flags_ = flags;
385 this->are_processor_specific_flags_set_ = true;
388 #ifdef HAVE_TARGET_32_LITTLE
389 // Virtual functions which may be overriden by the child class.
391 do_make_elf_object(const std::string&, Input_file*, off_t,
392 const elfcpp::Ehdr<32, false>&);
395 #ifdef HAVE_TARGET_32_BIG
396 // Virtual functions which may be overriden by the child class.
398 do_make_elf_object(const std::string&, Input_file*, off_t,
399 const elfcpp::Ehdr<32, true>&);
402 #ifdef HAVE_TARGET_64_LITTLE
403 // Virtual functions which may be overriden by the child class.
405 do_make_elf_object(const std::string&, Input_file*, off_t,
406 const elfcpp::Ehdr<64, false>& ehdr);
409 #ifdef HAVE_TARGET_64_BIG
410 // Virtual functions which may be overriden by the child class.
412 do_make_elf_object(const std::string& name, Input_file* input_file,
413 off_t offset, const elfcpp::Ehdr<64, true>& ehdr);
416 // Virtual functions which may be overriden by the child class.
417 virtual Output_section*
418 do_make_output_section(const char* name, elfcpp::Elf_Word type,
419 elfcpp::Elf_Xword flags);
421 // Virtual function which may be overriden by the child class.
424 { return parameters->options().relax(); }
426 // Virtual function which may be overriden by the child class.
428 do_relax(int, const Input_objects*, Symbol_table*, Layout*)
431 // A function for targets to call. Return whether BYTES/LEN matches
432 // VIEW/VIEW_SIZE at OFFSET.
434 match_view(const unsigned char* view, section_size_type view_size,
435 section_offset_type offset, const char* bytes, size_t len) const;
437 // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
440 set_view_to_nop(unsigned char* view, section_size_type view_size,
441 section_offset_type offset, size_t len) const;
444 // The implementations of the four do_make_elf_object virtual functions are
445 // almost identical except for their sizes and endianity. We use a template.
446 // for their implementations.
447 template<int size, bool big_endian>
449 do_make_elf_object_implementation(const std::string&, Input_file*, off_t,
450 const elfcpp::Ehdr<size, big_endian>&);
452 Target(const Target&);
453 Target& operator=(const Target&);
455 // The target information.
456 const Target_info* pti_;
457 // Processor-specific flags.
458 elfcpp::Elf_Word processor_specific_flags_;
459 // Whether the processor-specific flags are set at least once.
460 bool are_processor_specific_flags_set_;
463 // The abstract class for a specific size and endianness of target.
464 // Each actual target implementation class should derive from an
465 // instantiation of Sized_target.
467 template<int size, bool big_endian>
468 class Sized_target : public Target
471 // Make a new symbol table entry for the target. This should be
472 // overridden by a target which needs additional information in the
473 // symbol table. This will only be called if has_make_symbol()
475 virtual Sized_symbol<size>*
477 { gold_unreachable(); }
479 // Resolve a symbol for the target. This should be overridden by a
480 // target which needs to take special action. TO is the
481 // pre-existing symbol. SYM is the new symbol, seen in OBJECT.
482 // VERSION is the version of SYM. This will only be called if
483 // has_resolve() returns true.
485 resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
487 { gold_unreachable(); }
489 // Process the relocs for a section, and record information of the
490 // mapping from source to destination sections. This mapping is later
491 // used to determine unreferenced garbage sections. This procedure is
492 // only called during garbage collection.
494 gc_process_relocs(Symbol_table* symtab,
496 Sized_relobj<size, big_endian>* object,
497 unsigned int data_shndx,
498 unsigned int sh_type,
499 const unsigned char* prelocs,
501 Output_section* output_section,
502 bool needs_special_offset_handling,
503 size_t local_symbol_count,
504 const unsigned char* plocal_symbols) = 0;
506 // Scan the relocs for a section, and record any information
507 // required for the symbol. SYMTAB is the symbol table. OBJECT is
508 // the object in which the section appears. DATA_SHNDX is the
509 // section index that these relocs apply to. SH_TYPE is the type of
510 // the relocation section, SHT_REL or SHT_RELA. PRELOCS points to
511 // the relocation data. RELOC_COUNT is the number of relocs.
512 // LOCAL_SYMBOL_COUNT is the number of local symbols.
513 // OUTPUT_SECTION is the output section.
514 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
515 // sections are not mapped as usual. PLOCAL_SYMBOLS points to the
516 // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of
517 // pointers to the global symbol table from OBJECT.
519 scan_relocs(Symbol_table* symtab,
521 Sized_relobj<size, big_endian>* object,
522 unsigned int data_shndx,
523 unsigned int sh_type,
524 const unsigned char* prelocs,
526 Output_section* output_section,
527 bool needs_special_offset_handling,
528 size_t local_symbol_count,
529 const unsigned char* plocal_symbols) = 0;
531 // Relocate section data. SH_TYPE is the type of the relocation
532 // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation
533 // information. RELOC_COUNT is the number of relocs.
534 // OUTPUT_SECTION is the output section.
535 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
536 // to correspond to the output section. VIEW is a view into the
537 // output file holding the section contents, VIEW_ADDRESS is the
538 // virtual address of the view, and VIEW_SIZE is the size of the
539 // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
540 // parameters refer to the complete output section data, not just
541 // the input section data.
543 relocate_section(const Relocate_info<size, big_endian>*,
544 unsigned int sh_type,
545 const unsigned char* prelocs,
547 Output_section* output_section,
548 bool needs_special_offset_handling,
550 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
551 section_size_type view_size,
552 const Reloc_symbol_changes*) = 0;
554 // Scan the relocs during a relocatable link. The parameters are
555 // like scan_relocs, with an additional Relocatable_relocs
556 // parameter, used to record the disposition of the relocs.
558 scan_relocatable_relocs(Symbol_table* symtab,
560 Sized_relobj<size, big_endian>* object,
561 unsigned int data_shndx,
562 unsigned int sh_type,
563 const unsigned char* prelocs,
565 Output_section* output_section,
566 bool needs_special_offset_handling,
567 size_t local_symbol_count,
568 const unsigned char* plocal_symbols,
569 Relocatable_relocs*) = 0;
571 // Relocate a section during a relocatable link. The parameters are
572 // like relocate_section, with additional parameters for the view of
573 // the output reloc section.
575 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
576 unsigned int sh_type,
577 const unsigned char* prelocs,
579 Output_section* output_section,
580 off_t offset_in_output_section,
581 const Relocatable_relocs*,
583 typename elfcpp::Elf_types<size>::Elf_Addr
585 section_size_type view_size,
586 unsigned char* reloc_view,
587 section_size_type reloc_view_size) = 0;
590 Sized_target(const Target::Target_info* pti)
593 gold_assert(pti->size == size);
594 gold_assert(pti->is_big_endian ? big_endian : !big_endian);
598 } // End namespace gold.
600 #endif // !defined(GOLD_TARGET_H)