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 // Make an output section.
247 make_output_section(const char* name
, elfcpp::Elf_Word type
,
248 elfcpp::Elf_Xword flags
)
249 { return this->do_make_output_section(name
, type
, flags
); }
251 // Return true if target wants to perform relaxation.
255 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
256 if (is_debugging_enabled(DEBUG_RELAXATION
))
259 return this->do_may_relax();
262 // Perform a relaxation pass. Return true if layout may be changed.
264 relax(int pass
, const Input_objects
* input_objects
, Symbol_table
* symtab
,
267 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
268 if (is_debugging_enabled(DEBUG_RELAXATION
))
271 return this->do_relax(pass
, input_objects
, symtab
, layout
);
275 // This struct holds the constant information for a child class. We
276 // use a struct to avoid the overhead of virtual function calls for
277 // simple information.
280 // Address size (32 or 64).
282 // Whether the target is big endian.
284 // The code to store in the e_machine field of the ELF header.
285 elfcpp::EM machine_code
;
286 // Whether this target has a specific make_symbol function.
287 bool has_make_symbol
;
288 // Whether this target has a specific resolve function.
290 // Whether this target has a specific code fill function.
292 // Whether an object file with no .note.GNU-stack sections implies
293 // that the stack should be executable.
294 bool is_default_stack_executable
;
295 // Prefix character to strip when checking for wrapping.
297 // The default dynamic linker name.
298 const char* dynamic_linker
;
299 // The default text segment address.
300 uint64_t default_text_segment_address
;
301 // The ABI specified page size.
302 uint64_t abi_pagesize
;
303 // The common page size used by actual implementations.
304 uint64_t common_pagesize
;
305 // The special section index for small common symbols; SHN_UNDEF
307 elfcpp::Elf_Half small_common_shndx
;
308 // The special section index for large common symbols; SHN_UNDEF
310 elfcpp::Elf_Half large_common_shndx
;
311 // Section flags for small common section.
312 elfcpp::Elf_Xword small_common_section_flags
;
313 // Section flags for large common section.
314 elfcpp::Elf_Xword large_common_section_flags
;
317 Target(const Target_info
* pti
)
321 // Virtual function which may be implemented by the child class.
323 do_new_output_section(Output_section
*) const
326 // Virtual function which may be implemented by the child class.
328 do_finalize_sections(Layout
*)
331 // Virtual function which may be implemented by the child class.
333 do_dynsym_value(const Symbol
*) const
334 { gold_unreachable(); }
336 // Virtual function which must be implemented by the child class if
339 do_code_fill(section_size_type
) const
340 { gold_unreachable(); }
342 // Virtual function which may be implemented by the child class.
344 do_is_defined_by_abi(const Symbol
*) const
347 // Adjust the output file header before it is written out. VIEW
348 // points to the header in external form. LEN is the length, and
349 // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
350 // By default, we do nothing.
352 do_adjust_elf_header(unsigned char*, int) const
355 // Virtual function which may be overriden by the child class.
357 do_is_local_label_name(const char*) const;
359 // Virtual function which may be overridden by the child class.
361 do_calls_non_split(Relobj
* object
, unsigned int, section_offset_type
,
362 section_size_type
, unsigned char*, section_size_type
,
363 std::string
*, std::string
*) const;
365 // make_elf_object hooks. There are four versions of these for
366 // different address sizes and endianities.
368 #ifdef HAVE_TARGET_32_LITTLE
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, false>&);
375 #ifdef HAVE_TARGET_32_BIG
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
<32, true>&);
382 #ifdef HAVE_TARGET_64_LITTLE
383 // Virtual functions which may be overriden by the child class.
385 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
386 const elfcpp::Ehdr
<64, false>& ehdr
);
389 #ifdef HAVE_TARGET_64_BIG
390 // Virtual functions which may be overriden by the child class.
392 do_make_elf_object(const std::string
& name
, Input_file
* input_file
,
393 off_t offset
, const elfcpp::Ehdr
<64, true>& ehdr
);
396 // Virtual functions which may be overriden by the child class.
397 virtual Output_section
*
398 do_make_output_section(const char* name
, elfcpp::Elf_Word type
,
399 elfcpp::Elf_Xword flags
);
401 // Virtual function which may be overriden by the child class.
404 { return parameters
->options().relax(); }
406 // Virtual function which may be overriden by the child class.
408 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*)
411 // A function for targets to call. Return whether BYTES/LEN matches
412 // VIEW/VIEW_SIZE at OFFSET.
414 match_view(const unsigned char* view
, section_size_type view_size
,
415 section_offset_type offset
, const char* bytes
, size_t len
) const;
417 // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
420 set_view_to_nop(unsigned char* view
, section_size_type view_size
,
421 section_offset_type offset
, size_t len
) const;
424 // The implementations of the four do_make_elf_object virtual functions are
425 // almost identical except for their sizes and endianity. We use a template.
426 // for their implementations.
427 template<int size
, bool big_endian
>
429 do_make_elf_object_implementation(const std::string
&, Input_file
*, off_t
,
430 const elfcpp::Ehdr
<size
, big_endian
>&);
432 Target(const Target
&);
433 Target
& operator=(const Target
&);
435 // The target information.
436 const Target_info
* pti_
;
439 // The abstract class for a specific size and endianness of target.
440 // Each actual target implementation class should derive from an
441 // instantiation of Sized_target.
443 template<int size
, bool big_endian
>
444 class Sized_target
: public Target
447 // Make a new symbol table entry for the target. This should be
448 // overridden by a target which needs additional information in the
449 // symbol table. This will only be called if has_make_symbol()
451 virtual Sized_symbol
<size
>*
453 { gold_unreachable(); }
455 // Resolve a symbol for the target. This should be overridden by a
456 // target which needs to take special action. TO is the
457 // pre-existing symbol. SYM is the new symbol, seen in OBJECT.
458 // VERSION is the version of SYM. This will only be called if
459 // has_resolve() returns true.
461 resolve(Symbol
*, const elfcpp::Sym
<size
, big_endian
>&, Object
*,
463 { gold_unreachable(); }
465 // Process the relocs for a section, and record information of the
466 // mapping from source to destination sections. This mapping is later
467 // used to determine unreferenced garbage sections. This procedure is
468 // only called during garbage collection.
470 gc_process_relocs(const General_options
& options
,
471 Symbol_table
* symtab
,
473 Sized_relobj
<size
, big_endian
>* object
,
474 unsigned int data_shndx
,
475 unsigned int sh_type
,
476 const unsigned char* prelocs
,
478 Output_section
* output_section
,
479 bool needs_special_offset_handling
,
480 size_t local_symbol_count
,
481 const unsigned char* plocal_symbols
) = 0;
483 // Scan the relocs for a section, and record any information
484 // required for the symbol. OPTIONS is the command line options.
485 // SYMTAB is the symbol table. OBJECT is the object in which the
486 // section appears. DATA_SHNDX is the section index that these
487 // relocs apply to. SH_TYPE is the type of the relocation section,
488 // SHT_REL or SHT_RELA. PRELOCS points to the relocation data.
489 // RELOC_COUNT is the number of relocs. LOCAL_SYMBOL_COUNT is the
490 // number of local symbols. OUTPUT_SECTION is the output section.
491 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
492 // sections are not mapped as usual. PLOCAL_SYMBOLS points to the
493 // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of
494 // pointers to the global symbol table from OBJECT.
496 scan_relocs(const General_options
& options
,
497 Symbol_table
* symtab
,
499 Sized_relobj
<size
, big_endian
>* object
,
500 unsigned int data_shndx
,
501 unsigned int sh_type
,
502 const unsigned char* prelocs
,
504 Output_section
* output_section
,
505 bool needs_special_offset_handling
,
506 size_t local_symbol_count
,
507 const unsigned char* plocal_symbols
) = 0;
509 // Relocate section data. SH_TYPE is the type of the relocation
510 // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation
511 // information. RELOC_COUNT is the number of relocs.
512 // OUTPUT_SECTION is the output section.
513 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
514 // to correspond to the output section. VIEW is a view into the
515 // output file holding the section contents, VIEW_ADDRESS is the
516 // virtual address of the view, and VIEW_SIZE is the size of the
517 // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
518 // parameters refer to the complete output section data, not just
519 // the input section data.
521 relocate_section(const Relocate_info
<size
, big_endian
>*,
522 unsigned int sh_type
,
523 const unsigned char* prelocs
,
525 Output_section
* output_section
,
526 bool needs_special_offset_handling
,
528 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
529 section_size_type view_size
,
530 const Reloc_symbol_changes
*) = 0;
532 // Scan the relocs during a relocatable link. The parameters are
533 // like scan_relocs, with an additional Relocatable_relocs
534 // parameter, used to record the disposition of the relocs.
536 scan_relocatable_relocs(const General_options
& options
,
537 Symbol_table
* symtab
,
539 Sized_relobj
<size
, big_endian
>* object
,
540 unsigned int data_shndx
,
541 unsigned int sh_type
,
542 const unsigned char* prelocs
,
544 Output_section
* output_section
,
545 bool needs_special_offset_handling
,
546 size_t local_symbol_count
,
547 const unsigned char* plocal_symbols
,
548 Relocatable_relocs
*) = 0;
550 // Relocate a section during a relocatable link. The parameters are
551 // like relocate_section, with additional parameters for the view of
552 // the output reloc section.
554 relocate_for_relocatable(const Relocate_info
<size
, big_endian
>*,
555 unsigned int sh_type
,
556 const unsigned char* prelocs
,
558 Output_section
* output_section
,
559 off_t offset_in_output_section
,
560 const Relocatable_relocs
*,
562 typename
elfcpp::Elf_types
<size
>::Elf_Addr
564 section_size_type view_size
,
565 unsigned char* reloc_view
,
566 section_size_type reloc_view_size
) = 0;
569 Sized_target(const Target::Target_info
* pti
)
572 gold_assert(pti
->size
== size
);
573 gold_assert(pti
->is_big_endian
? big_endian
: !big_endian
);
577 } // End namespace gold.
579 #endif // !defined(GOLD_TARGET_H)