gold/
[deliverable/binutils-gdb.git] / gold / target.h
1 // target.h -- target support for gold -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
6
7 // This file is part of gold.
8
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
13
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
23
24 // The abstract class Target is the interface for target specific
25 // support. It defines abstract methods which each target must
26 // implement. Typically there will be one target per processor, but
27 // in some cases it may be necessary to have subclasses.
28
29 // For speed and consistency we want to use inline functions to handle
30 // relocation processing. So besides implementations of the abstract
31 // methods, each target is expected to define a template
32 // specialization of the relocation functions.
33
34 #ifndef GOLD_TARGET_H
35 #define GOLD_TARGET_H
36
37 #include "elfcpp.h"
38 #include "options.h"
39 #include "parameters.h"
40 #include "debug.h"
41
42 namespace gold
43 {
44
45 class Object;
46 class Relobj;
47 template<int size, bool big_endian>
48 class Sized_relobj;
49 template<int size, bool big_endian>
50 class Sized_relobj_file;
51 class Relocatable_relocs;
52 template<int size, bool big_endian>
53 struct Relocate_info;
54 class Reloc_symbol_changes;
55 class Symbol;
56 template<int size>
57 class Sized_symbol;
58 class Symbol_table;
59 class Output_data;
60 class Output_data_got_base;
61 class Output_section;
62 class Input_objects;
63 class Task;
64
65 // The abstract class for target specific handling.
66
67 class Target
68 {
69 public:
70 virtual ~Target()
71 { }
72
73 // Return the bit size that this target implements. This should
74 // return 32 or 64.
75 int
76 get_size() const
77 { return this->pti_->size; }
78
79 // Return whether this target is big-endian.
80 bool
81 is_big_endian() const
82 { return this->pti_->is_big_endian; }
83
84 // Machine code to store in e_machine field of ELF header.
85 elfcpp::EM
86 machine_code() const
87 { return this->pti_->machine_code; }
88
89 // Processor specific flags to store in e_flags field of ELF header.
90 elfcpp::Elf_Word
91 processor_specific_flags() const
92 { return this->processor_specific_flags_; }
93
94 // Whether processor specific flags are set at least once.
95 bool
96 are_processor_specific_flags_set() const
97 { return this->are_processor_specific_flags_set_; }
98
99 // Whether this target has a specific make_symbol function.
100 bool
101 has_make_symbol() const
102 { return this->pti_->has_make_symbol; }
103
104 // Whether this target has a specific resolve function.
105 bool
106 has_resolve() const
107 { return this->pti_->has_resolve; }
108
109 // Whether this target has a specific code fill function.
110 bool
111 has_code_fill() const
112 { return this->pti_->has_code_fill; }
113
114 // Return the default name of the dynamic linker.
115 const char*
116 dynamic_linker() const
117 { return this->pti_->dynamic_linker; }
118
119 // Return the default address to use for the text segment.
120 uint64_t
121 default_text_segment_address() const
122 { return this->pti_->default_text_segment_address; }
123
124 // Return the ABI specified page size.
125 uint64_t
126 abi_pagesize() const
127 {
128 if (parameters->options().max_page_size() > 0)
129 return parameters->options().max_page_size();
130 else
131 return this->pti_->abi_pagesize;
132 }
133
134 // Return the common page size used on actual systems.
135 uint64_t
136 common_pagesize() const
137 {
138 if (parameters->options().common_page_size() > 0)
139 return std::min(parameters->options().common_page_size(),
140 this->abi_pagesize());
141 else
142 return std::min(this->pti_->common_pagesize,
143 this->abi_pagesize());
144 }
145
146 // Return whether PF_X segments must contain nothing but the contents of
147 // SHF_EXECINSTR sections (no non-executable data, no headers).
148 bool
149 isolate_execinstr() const
150 { return this->pti_->isolate_execinstr; }
151
152 uint64_t
153 rosegment_gap() const
154 { return this->pti_->rosegment_gap; }
155
156 // If we see some object files with .note.GNU-stack sections, and
157 // some objects files without them, this returns whether we should
158 // consider the object files without them to imply that the stack
159 // should be executable.
160 bool
161 is_default_stack_executable() const
162 { return this->pti_->is_default_stack_executable; }
163
164 // Return a character which may appear as a prefix for a wrap
165 // symbol. If this character appears, we strip it when checking for
166 // wrapping and add it back when forming the final symbol name.
167 // This should be '\0' if not special prefix is required, which is
168 // the normal case.
169 char
170 wrap_char() const
171 { return this->pti_->wrap_char; }
172
173 // Return the special section index which indicates a small common
174 // symbol. This will return SHN_UNDEF if there are no small common
175 // symbols.
176 elfcpp::Elf_Half
177 small_common_shndx() const
178 { return this->pti_->small_common_shndx; }
179
180 // Return values to add to the section flags for the section holding
181 // small common symbols.
182 elfcpp::Elf_Xword
183 small_common_section_flags() const
184 {
185 gold_assert(this->pti_->small_common_shndx != elfcpp::SHN_UNDEF);
186 return this->pti_->small_common_section_flags;
187 }
188
189 // Return the special section index which indicates a large common
190 // symbol. This will return SHN_UNDEF if there are no large common
191 // symbols.
192 elfcpp::Elf_Half
193 large_common_shndx() const
194 { return this->pti_->large_common_shndx; }
195
196 // Return values to add to the section flags for the section holding
197 // large common symbols.
198 elfcpp::Elf_Xword
199 large_common_section_flags() const
200 {
201 gold_assert(this->pti_->large_common_shndx != elfcpp::SHN_UNDEF);
202 return this->pti_->large_common_section_flags;
203 }
204
205 // This hook is called when an output section is created.
206 void
207 new_output_section(Output_section* os) const
208 { this->do_new_output_section(os); }
209
210 // This is called to tell the target to complete any sections it is
211 // handling. After this all sections must have their final size.
212 void
213 finalize_sections(Layout* layout, const Input_objects* input_objects,
214 Symbol_table* symtab)
215 { return this->do_finalize_sections(layout, input_objects, symtab); }
216
217 // Return the value to use for a global symbol which needs a special
218 // value in the dynamic symbol table. This will only be called if
219 // the backend first calls symbol->set_needs_dynsym_value().
220 uint64_t
221 dynsym_value(const Symbol* sym) const
222 { return this->do_dynsym_value(sym); }
223
224 // Return a string to use to fill out a code section. This is
225 // basically one or more NOPS which must fill out the specified
226 // length in bytes.
227 std::string
228 code_fill(section_size_type length) const
229 { return this->do_code_fill(length); }
230
231 // Return whether SYM is known to be defined by the ABI. This is
232 // used to avoid inappropriate warnings about undefined symbols.
233 bool
234 is_defined_by_abi(const Symbol* sym) const
235 { return this->do_is_defined_by_abi(sym); }
236
237 // Adjust the output file header before it is written out. VIEW
238 // points to the header in external form. LEN is the length.
239 void
240 adjust_elf_header(unsigned char* view, int len) const
241 { return this->do_adjust_elf_header(view, len); }
242
243 // Return whether NAME is a local label name. This is used to implement the
244 // --discard-locals options.
245 bool
246 is_local_label_name(const char* name) const
247 { return this->do_is_local_label_name(name); }
248
249 // Get the symbol index to use for a target specific reloc.
250 unsigned int
251 reloc_symbol_index(void* arg, unsigned int type) const
252 { return this->do_reloc_symbol_index(arg, type); }
253
254 // Get the addend to use for a target specific reloc.
255 uint64_t
256 reloc_addend(void* arg, unsigned int type, uint64_t addend) const
257 { return this->do_reloc_addend(arg, type, addend); }
258
259 // Return the PLT address to use for a global symbol.
260 uint64_t
261 plt_address_for_global(const Symbol* sym) const
262 { return this->do_plt_address_for_global(sym); }
263
264 // Return the PLT address to use for a local symbol.
265 uint64_t
266 plt_address_for_local(const Relobj* object, unsigned int symndx) const
267 { return this->do_plt_address_for_local(object, symndx); }
268
269 // Return the offset to use for the GOT_INDX'th got entry which is
270 // for a local tls symbol specified by OBJECT, SYMNDX.
271 int64_t
272 tls_offset_for_local(const Relobj* object,
273 unsigned int symndx,
274 unsigned int got_indx) const
275 { return do_tls_offset_for_local(object, symndx, got_indx); }
276
277 // Return the offset to use for the GOT_INDX'th got entry which is
278 // for global tls symbol GSYM.
279 int64_t
280 tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const
281 { return do_tls_offset_for_global(gsym, got_indx); }
282
283 // Return whether this target can use relocation types to determine
284 // if a function's address is taken.
285 bool
286 can_check_for_function_pointers() const
287 { return this->do_can_check_for_function_pointers(); }
288
289 // Return whether a relocation to a merged section can be processed
290 // to retrieve the contents.
291 bool
292 can_icf_inline_merge_sections () const
293 { return this->pti_->can_icf_inline_merge_sections; }
294
295 // Whether a section called SECTION_NAME may have function pointers to
296 // sections not eligible for safe ICF folding.
297 virtual bool
298 section_may_have_icf_unsafe_pointers(const char* section_name) const
299 { return this->do_section_may_have_icf_unsafe_pointers(section_name); }
300
301 // Return the base to use for the PC value in an FDE when it is
302 // encoded using DW_EH_PE_datarel. This does not appear to be
303 // documented anywhere, but it is target specific. Any use of
304 // DW_EH_PE_datarel in gcc requires defining a special macro
305 // (ASM_MAYBE_OUTPUT_ENCODED_ADDR_RTX) to output the value.
306 uint64_t
307 ehframe_datarel_base() const
308 { return this->do_ehframe_datarel_base(); }
309
310 // Return true if a reference to SYM from a reloc of type R_TYPE
311 // means that the current function may call an object compiled
312 // without -fsplit-stack. SYM is known to be defined in an object
313 // compiled without -fsplit-stack.
314 bool
315 is_call_to_non_split(const Symbol* sym, unsigned int r_type) const
316 { return this->do_is_call_to_non_split(sym, r_type); }
317
318 // A function starts at OFFSET in section SHNDX in OBJECT. That
319 // function was compiled with -fsplit-stack, but it refers to a
320 // function which was compiled without -fsplit-stack. VIEW is a
321 // modifiable view of the section; VIEW_SIZE is the size of the
322 // view. The target has to adjust the function so that it allocates
323 // enough stack.
324 void
325 calls_non_split(Relobj* object, unsigned int shndx,
326 section_offset_type fnoffset, section_size_type fnsize,
327 unsigned char* view, section_size_type view_size,
328 std::string* from, std::string* to) const
329 {
330 this->do_calls_non_split(object, shndx, fnoffset, fnsize, view, view_size,
331 from, to);
332 }
333
334 // Make an ELF object.
335 template<int size, bool big_endian>
336 Object*
337 make_elf_object(const std::string& name, Input_file* input_file,
338 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
339 { return this->do_make_elf_object(name, input_file, offset, ehdr); }
340
341 // Make an output section.
342 Output_section*
343 make_output_section(const char* name, elfcpp::Elf_Word type,
344 elfcpp::Elf_Xword flags)
345 { return this->do_make_output_section(name, type, flags); }
346
347 // Return true if target wants to perform relaxation.
348 bool
349 may_relax() const
350 {
351 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
352 if (is_debugging_enabled(DEBUG_RELAXATION))
353 return true;
354
355 return this->do_may_relax();
356 }
357
358 // Perform a relaxation pass. Return true if layout may be changed.
359 bool
360 relax(int pass, const Input_objects* input_objects, Symbol_table* symtab,
361 Layout* layout, const Task* task)
362 {
363 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
364 if (is_debugging_enabled(DEBUG_RELAXATION))
365 return pass < 2;
366
367 return this->do_relax(pass, input_objects, symtab, layout, task);
368 }
369
370 // Return the target-specific name of attributes section. This is
371 // NULL if a target does not use attributes section or if it uses
372 // the default section name ".gnu.attributes".
373 const char*
374 attributes_section() const
375 { return this->pti_->attributes_section; }
376
377 // Return the vendor name of vendor attributes.
378 const char*
379 attributes_vendor() const
380 { return this->pti_->attributes_vendor; }
381
382 // Whether a section called NAME is an attribute section.
383 bool
384 is_attributes_section(const char* name) const
385 {
386 return ((this->pti_->attributes_section != NULL
387 && strcmp(name, this->pti_->attributes_section) == 0)
388 || strcmp(name, ".gnu.attributes") == 0);
389 }
390
391 // Return a bit mask of argument types for attribute with TAG.
392 int
393 attribute_arg_type(int tag) const
394 { return this->do_attribute_arg_type(tag); }
395
396 // Return the attribute tag of the position NUM in the list of fixed
397 // attributes. Normally there is no reordering and
398 // attributes_order(NUM) == NUM.
399 int
400 attributes_order(int num) const
401 { return this->do_attributes_order(num); }
402
403 // When a target is selected as the default target, we call this method,
404 // which may be used for expensive, target-specific initialization.
405 void
406 select_as_default_target()
407 { this->do_select_as_default_target(); }
408
409 // Return the value to store in the EI_OSABI field in the ELF
410 // header.
411 elfcpp::ELFOSABI
412 osabi() const
413 { return this->osabi_; }
414
415 // Set the value to store in the EI_OSABI field in the ELF header.
416 void
417 set_osabi(elfcpp::ELFOSABI osabi)
418 { this->osabi_ = osabi; }
419
420 // Define target-specific standard symbols.
421 void
422 define_standard_symbols(Symbol_table* symtab, Layout* layout)
423 { this->do_define_standard_symbols(symtab, layout); }
424
425 // Return the output section name to use given an input section
426 // name, or NULL if no target specific name mapping is required.
427 // Set *PLEN to the length of the name if returning non-NULL.
428 const char*
429 output_section_name(const Relobj* relobj,
430 const char* name,
431 size_t* plen) const
432 { return this->do_output_section_name(relobj, name, plen); }
433
434 // Add any special sections for this symbol to the gc work list.
435 void
436 gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const
437 { this->do_gc_mark_symbol(symtab, sym); }
438
439 protected:
440 // This struct holds the constant information for a child class. We
441 // use a struct to avoid the overhead of virtual function calls for
442 // simple information.
443 struct Target_info
444 {
445 // Address size (32 or 64).
446 int size;
447 // Whether the target is big endian.
448 bool is_big_endian;
449 // The code to store in the e_machine field of the ELF header.
450 elfcpp::EM machine_code;
451 // Whether this target has a specific make_symbol function.
452 bool has_make_symbol;
453 // Whether this target has a specific resolve function.
454 bool has_resolve;
455 // Whether this target has a specific code fill function.
456 bool has_code_fill;
457 // Whether an object file with no .note.GNU-stack sections implies
458 // that the stack should be executable.
459 bool is_default_stack_executable;
460 // Whether a relocation to a merged section can be processed to
461 // retrieve the contents.
462 bool can_icf_inline_merge_sections;
463 // Prefix character to strip when checking for wrapping.
464 char wrap_char;
465 // The default dynamic linker name.
466 const char* dynamic_linker;
467 // The default text segment address.
468 uint64_t default_text_segment_address;
469 // The ABI specified page size.
470 uint64_t abi_pagesize;
471 // The common page size used by actual implementations.
472 uint64_t common_pagesize;
473 // Whether PF_X segments must contain nothing but the contents of
474 // SHF_EXECINSTR sections (no non-executable data, no headers).
475 bool isolate_execinstr;
476 // If nonzero, distance from the text segment to the read-only segment.
477 uint64_t rosegment_gap;
478 // The special section index for small common symbols; SHN_UNDEF
479 // if none.
480 elfcpp::Elf_Half small_common_shndx;
481 // The special section index for large common symbols; SHN_UNDEF
482 // if none.
483 elfcpp::Elf_Half large_common_shndx;
484 // Section flags for small common section.
485 elfcpp::Elf_Xword small_common_section_flags;
486 // Section flags for large common section.
487 elfcpp::Elf_Xword large_common_section_flags;
488 // Name of attributes section if it is not ".gnu.attributes".
489 const char* attributes_section;
490 // Vendor name of vendor attributes.
491 const char* attributes_vendor;
492 };
493
494 Target(const Target_info* pti)
495 : pti_(pti), processor_specific_flags_(0),
496 are_processor_specific_flags_set_(false), osabi_(elfcpp::ELFOSABI_NONE)
497 { }
498
499 // Virtual function which may be implemented by the child class.
500 virtual void
501 do_new_output_section(Output_section*) const
502 { }
503
504 // Virtual function which may be implemented by the child class.
505 virtual void
506 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*)
507 { }
508
509 // Virtual function which may be implemented by the child class.
510 virtual uint64_t
511 do_dynsym_value(const Symbol*) const
512 { gold_unreachable(); }
513
514 // Virtual function which must be implemented by the child class if
515 // needed.
516 virtual std::string
517 do_code_fill(section_size_type) const
518 { gold_unreachable(); }
519
520 // Virtual function which may be implemented by the child class.
521 virtual bool
522 do_is_defined_by_abi(const Symbol*) const
523 { return false; }
524
525 // Adjust the output file header before it is written out. VIEW
526 // points to the header in external form. LEN is the length, and
527 // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
528 // By default, we set the EI_OSABI field if requested (in
529 // Sized_target).
530 virtual void
531 do_adjust_elf_header(unsigned char*, int) const = 0;
532
533 // Virtual function which may be overridden by the child class.
534 virtual bool
535 do_is_local_label_name(const char*) const;
536
537 // Virtual function that must be overridden by a target which uses
538 // target specific relocations.
539 virtual unsigned int
540 do_reloc_symbol_index(void*, unsigned int) const
541 { gold_unreachable(); }
542
543 // Virtual function that must be overridden by a target which uses
544 // target specific relocations.
545 virtual uint64_t
546 do_reloc_addend(void*, unsigned int, uint64_t) const
547 { gold_unreachable(); }
548
549 // Virtual functions that must be overridden by a target that uses
550 // STT_GNU_IFUNC symbols.
551 virtual uint64_t
552 do_plt_address_for_global(const Symbol*) const
553 { gold_unreachable(); }
554
555 virtual uint64_t
556 do_plt_address_for_local(const Relobj*, unsigned int) const
557 { gold_unreachable(); }
558
559 virtual int64_t
560 do_tls_offset_for_local(const Relobj*, unsigned int, unsigned int) const
561 { gold_unreachable(); }
562
563 virtual int64_t
564 do_tls_offset_for_global(Symbol*, unsigned int) const
565 { gold_unreachable(); }
566
567 // Virtual function which may be overriden by the child class.
568 virtual bool
569 do_can_check_for_function_pointers() const
570 { return false; }
571
572 // Virtual function which may be overridden by the child class. We
573 // recognize some default sections for which we don't care whether
574 // they have function pointers.
575 virtual bool
576 do_section_may_have_icf_unsafe_pointers(const char* section_name) const
577 {
578 // We recognize sections for normal vtables, construction vtables and
579 // EH frames.
580 return (!is_prefix_of(".rodata._ZTV", section_name)
581 && !is_prefix_of(".data.rel.ro._ZTV", section_name)
582 && !is_prefix_of(".rodata._ZTC", section_name)
583 && !is_prefix_of(".data.rel.ro._ZTC", section_name)
584 && !is_prefix_of(".eh_frame", section_name));
585 }
586
587 virtual uint64_t
588 do_ehframe_datarel_base() const
589 { gold_unreachable(); }
590
591 // Virtual function which may be overridden by the child class. The
592 // default implementation is that any function not defined by the
593 // ABI is a call to a non-split function.
594 virtual bool
595 do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
596
597 // Virtual function which may be overridden by the child class.
598 virtual void
599 do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
600 section_size_type, unsigned char*, section_size_type,
601 std::string*, std::string*) const;
602
603 // make_elf_object hooks. There are four versions of these for
604 // different address sizes and endianness.
605
606 // Set processor specific flags.
607 void
608 set_processor_specific_flags(elfcpp::Elf_Word flags)
609 {
610 this->processor_specific_flags_ = flags;
611 this->are_processor_specific_flags_set_ = true;
612 }
613
614 #ifdef HAVE_TARGET_32_LITTLE
615 // Virtual functions which may be overridden by the child class.
616 virtual Object*
617 do_make_elf_object(const std::string&, Input_file*, off_t,
618 const elfcpp::Ehdr<32, false>&);
619 #endif
620
621 #ifdef HAVE_TARGET_32_BIG
622 // Virtual functions which may be overridden by the child class.
623 virtual Object*
624 do_make_elf_object(const std::string&, Input_file*, off_t,
625 const elfcpp::Ehdr<32, true>&);
626 #endif
627
628 #ifdef HAVE_TARGET_64_LITTLE
629 // Virtual functions which may be overridden by the child class.
630 virtual Object*
631 do_make_elf_object(const std::string&, Input_file*, off_t,
632 const elfcpp::Ehdr<64, false>& ehdr);
633 #endif
634
635 #ifdef HAVE_TARGET_64_BIG
636 // Virtual functions which may be overridden by the child class.
637 virtual Object*
638 do_make_elf_object(const std::string& name, Input_file* input_file,
639 off_t offset, const elfcpp::Ehdr<64, true>& ehdr);
640 #endif
641
642 // Virtual functions which may be overridden by the child class.
643 virtual Output_section*
644 do_make_output_section(const char* name, elfcpp::Elf_Word type,
645 elfcpp::Elf_Xword flags);
646
647 // Virtual function which may be overridden by the child class.
648 virtual bool
649 do_may_relax() const
650 { return parameters->options().relax(); }
651
652 // Virtual function which may be overridden by the child class.
653 virtual bool
654 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*)
655 { return false; }
656
657 // A function for targets to call. Return whether BYTES/LEN matches
658 // VIEW/VIEW_SIZE at OFFSET.
659 bool
660 match_view(const unsigned char* view, section_size_type view_size,
661 section_offset_type offset, const char* bytes, size_t len) const;
662
663 // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
664 // for LEN bytes.
665 void
666 set_view_to_nop(unsigned char* view, section_size_type view_size,
667 section_offset_type offset, size_t len) const;
668
669 // This must be overridden by the child class if it has target-specific
670 // attributes subsection in the attribute section.
671 virtual int
672 do_attribute_arg_type(int) const
673 { gold_unreachable(); }
674
675 // This may be overridden by the child class.
676 virtual int
677 do_attributes_order(int num) const
678 { return num; }
679
680 // This may be overridden by the child class.
681 virtual void
682 do_select_as_default_target()
683 { }
684
685 // This may be overridden by the child class.
686 virtual void
687 do_define_standard_symbols(Symbol_table*, Layout*)
688 { }
689
690 // This may be overridden by the child class.
691 virtual const char*
692 do_output_section_name(const Relobj*, const char*, size_t*) const
693 { return NULL; }
694
695 // This may be overridden by the child class.
696 virtual void
697 do_gc_mark_symbol(Symbol_table*, Symbol*) const
698 { }
699
700 private:
701 // The implementations of the four do_make_elf_object virtual functions are
702 // almost identical except for their sizes and endianness. We use a template.
703 // for their implementations.
704 template<int size, bool big_endian>
705 inline Object*
706 do_make_elf_object_implementation(const std::string&, Input_file*, off_t,
707 const elfcpp::Ehdr<size, big_endian>&);
708
709 Target(const Target&);
710 Target& operator=(const Target&);
711
712 // The target information.
713 const Target_info* pti_;
714 // Processor-specific flags.
715 elfcpp::Elf_Word processor_specific_flags_;
716 // Whether the processor-specific flags are set at least once.
717 bool are_processor_specific_flags_set_;
718 // If not ELFOSABI_NONE, the value to put in the EI_OSABI field of
719 // the ELF header. This is handled at this level because it is
720 // OS-specific rather than processor-specific.
721 elfcpp::ELFOSABI osabi_;
722 };
723
724 // The abstract class for a specific size and endianness of target.
725 // Each actual target implementation class should derive from an
726 // instantiation of Sized_target.
727
728 template<int size, bool big_endian>
729 class Sized_target : public Target
730 {
731 public:
732 // Make a new symbol table entry for the target. This should be
733 // overridden by a target which needs additional information in the
734 // symbol table. This will only be called if has_make_symbol()
735 // returns true.
736 virtual Sized_symbol<size>*
737 make_symbol() const
738 { gold_unreachable(); }
739
740 // Resolve a symbol for the target. This should be overridden by a
741 // target which needs to take special action. TO is the
742 // pre-existing symbol. SYM is the new symbol, seen in OBJECT.
743 // VERSION is the version of SYM. This will only be called if
744 // has_resolve() returns true.
745 virtual void
746 resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
747 const char*)
748 { gold_unreachable(); }
749
750 // Process the relocs for a section, and record information of the
751 // mapping from source to destination sections. This mapping is later
752 // used to determine unreferenced garbage sections. This procedure is
753 // only called during garbage collection.
754 virtual void
755 gc_process_relocs(Symbol_table* symtab,
756 Layout* layout,
757 Sized_relobj_file<size, big_endian>* object,
758 unsigned int data_shndx,
759 unsigned int sh_type,
760 const unsigned char* prelocs,
761 size_t reloc_count,
762 Output_section* output_section,
763 bool needs_special_offset_handling,
764 size_t local_symbol_count,
765 const unsigned char* plocal_symbols) = 0;
766
767 // Scan the relocs for a section, and record any information
768 // required for the symbol. SYMTAB is the symbol table. OBJECT is
769 // the object in which the section appears. DATA_SHNDX is the
770 // section index that these relocs apply to. SH_TYPE is the type of
771 // the relocation section, SHT_REL or SHT_RELA. PRELOCS points to
772 // the relocation data. RELOC_COUNT is the number of relocs.
773 // LOCAL_SYMBOL_COUNT is the number of local symbols.
774 // OUTPUT_SECTION is the output section.
775 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
776 // sections are not mapped as usual. PLOCAL_SYMBOLS points to the
777 // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of
778 // pointers to the global symbol table from OBJECT.
779 virtual void
780 scan_relocs(Symbol_table* symtab,
781 Layout* layout,
782 Sized_relobj_file<size, big_endian>* object,
783 unsigned int data_shndx,
784 unsigned int sh_type,
785 const unsigned char* prelocs,
786 size_t reloc_count,
787 Output_section* output_section,
788 bool needs_special_offset_handling,
789 size_t local_symbol_count,
790 const unsigned char* plocal_symbols) = 0;
791
792 // Relocate section data. SH_TYPE is the type of the relocation
793 // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation
794 // information. RELOC_COUNT is the number of relocs.
795 // OUTPUT_SECTION is the output section.
796 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
797 // to correspond to the output section. VIEW is a view into the
798 // output file holding the section contents, VIEW_ADDRESS is the
799 // virtual address of the view, and VIEW_SIZE is the size of the
800 // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
801 // parameters refer to the complete output section data, not just
802 // the input section data.
803 virtual void
804 relocate_section(const Relocate_info<size, big_endian>*,
805 unsigned int sh_type,
806 const unsigned char* prelocs,
807 size_t reloc_count,
808 Output_section* output_section,
809 bool needs_special_offset_handling,
810 unsigned char* view,
811 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
812 section_size_type view_size,
813 const Reloc_symbol_changes*) = 0;
814
815 // Scan the relocs during a relocatable link. The parameters are
816 // like scan_relocs, with an additional Relocatable_relocs
817 // parameter, used to record the disposition of the relocs.
818 virtual void
819 scan_relocatable_relocs(Symbol_table* symtab,
820 Layout* layout,
821 Sized_relobj_file<size, big_endian>* object,
822 unsigned int data_shndx,
823 unsigned int sh_type,
824 const unsigned char* prelocs,
825 size_t reloc_count,
826 Output_section* output_section,
827 bool needs_special_offset_handling,
828 size_t local_symbol_count,
829 const unsigned char* plocal_symbols,
830 Relocatable_relocs*) = 0;
831
832 // Emit relocations for a section during a relocatable link, and for
833 // --emit-relocs. The parameters are like relocate_section, with
834 // additional parameters for the view of the output reloc section.
835 virtual void
836 relocate_relocs(const Relocate_info<size, big_endian>*,
837 unsigned int sh_type,
838 const unsigned char* prelocs,
839 size_t reloc_count,
840 Output_section* output_section,
841 typename elfcpp::Elf_types<size>::Elf_Off
842 offset_in_output_section,
843 const Relocatable_relocs*,
844 unsigned char* view,
845 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
846 section_size_type view_size,
847 unsigned char* reloc_view,
848 section_size_type reloc_view_size) = 0;
849
850 // Perform target-specific processing in a relocatable link. This is
851 // only used if we use the relocation strategy RELOC_SPECIAL.
852 // RELINFO points to a Relocation_info structure. SH_TYPE is the relocation
853 // section type. PRELOC_IN points to the original relocation. RELNUM is
854 // the index number of the relocation in the relocation section.
855 // OUTPUT_SECTION is the output section to which the relocation is applied.
856 // OFFSET_IN_OUTPUT_SECTION is the offset of the relocation input section
857 // within the output section. VIEW points to the output view of the
858 // output section. VIEW_ADDRESS is output address of the view. VIEW_SIZE
859 // is the size of the output view and PRELOC_OUT points to the new
860 // relocation in the output object.
861 //
862 // A target only needs to override this if the generic code in
863 // target-reloc.h cannot handle some relocation types.
864
865 virtual void
866 relocate_special_relocatable(const Relocate_info<size, big_endian>*
867 /*relinfo */,
868 unsigned int /* sh_type */,
869 const unsigned char* /* preloc_in */,
870 size_t /* relnum */,
871 Output_section* /* output_section */,
872 typename elfcpp::Elf_types<size>::Elf_Off
873 /* offset_in_output_section */,
874 unsigned char* /* view */,
875 typename elfcpp::Elf_types<size>::Elf_Addr
876 /* view_address */,
877 section_size_type /* view_size */,
878 unsigned char* /* preloc_out*/)
879 { gold_unreachable(); }
880
881 // Return the number of entries in the GOT. This is only used for
882 // laying out the incremental link info sections. A target needs
883 // to implement this to support incremental linking.
884
885 virtual unsigned int
886 got_entry_count() const
887 { gold_unreachable(); }
888
889 // Return the number of entries in the PLT. This is only used for
890 // laying out the incremental link info sections. A target needs
891 // to implement this to support incremental linking.
892
893 virtual unsigned int
894 plt_entry_count() const
895 { gold_unreachable(); }
896
897 // Return the offset of the first non-reserved PLT entry. This is
898 // only used for laying out the incremental link info sections.
899 // A target needs to implement this to support incremental linking.
900
901 virtual unsigned int
902 first_plt_entry_offset() const
903 { gold_unreachable(); }
904
905 // Return the size of each PLT entry. This is only used for
906 // laying out the incremental link info sections. A target needs
907 // to implement this to support incremental linking.
908
909 virtual unsigned int
910 plt_entry_size() const
911 { gold_unreachable(); }
912
913 // Create the GOT and PLT sections for an incremental update.
914 // A target needs to implement this to support incremental linking.
915
916 virtual Output_data_got_base*
917 init_got_plt_for_update(Symbol_table*,
918 Layout*,
919 unsigned int /* got_count */,
920 unsigned int /* plt_count */)
921 { gold_unreachable(); }
922
923 // Reserve a GOT entry for a local symbol, and regenerate any
924 // necessary dynamic relocations.
925 virtual void
926 reserve_local_got_entry(unsigned int /* got_index */,
927 Sized_relobj<size, big_endian>* /* obj */,
928 unsigned int /* r_sym */,
929 unsigned int /* got_type */)
930 { gold_unreachable(); }
931
932 // Reserve a GOT entry for a global symbol, and regenerate any
933 // necessary dynamic relocations.
934 virtual void
935 reserve_global_got_entry(unsigned int /* got_index */, Symbol* /* gsym */,
936 unsigned int /* got_type */)
937 { gold_unreachable(); }
938
939 // Register an existing PLT entry for a global symbol.
940 // A target needs to implement this to support incremental linking.
941
942 virtual void
943 register_global_plt_entry(Symbol_table*, Layout*,
944 unsigned int /* plt_index */,
945 Symbol*)
946 { gold_unreachable(); }
947
948 // Force a COPY relocation for a given symbol.
949 // A target needs to implement this to support incremental linking.
950
951 virtual void
952 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t)
953 { gold_unreachable(); }
954
955 // Apply an incremental relocation.
956
957 virtual void
958 apply_relocation(const Relocate_info<size, big_endian>* /* relinfo */,
959 typename elfcpp::Elf_types<size>::Elf_Addr /* r_offset */,
960 unsigned int /* r_type */,
961 typename elfcpp::Elf_types<size>::Elf_Swxword /* r_addend */,
962 const Symbol* /* gsym */,
963 unsigned char* /* view */,
964 typename elfcpp::Elf_types<size>::Elf_Addr /* address */,
965 section_size_type /* view_size */)
966 { gold_unreachable(); }
967
968 // Handle target specific gc actions when adding a gc reference from
969 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
970 // and DST_OFF.
971 void
972 gc_add_reference(Symbol_table* symtab,
973 Object* src_obj,
974 unsigned int src_shndx,
975 Object* dst_obj,
976 unsigned int dst_shndx,
977 typename elfcpp::Elf_types<size>::Elf_Addr dst_off) const
978 {
979 this->do_gc_add_reference(symtab, src_obj, src_shndx,
980 dst_obj, dst_shndx, dst_off);
981 }
982
983 protected:
984 Sized_target(const Target::Target_info* pti)
985 : Target(pti)
986 {
987 gold_assert(pti->size == size);
988 gold_assert(pti->is_big_endian ? big_endian : !big_endian);
989 }
990
991 // Set the EI_OSABI field if requested.
992 virtual void
993 do_adjust_elf_header(unsigned char*, int) const;
994
995 // Handle target specific gc actions when adding a gc reference.
996 virtual void
997 do_gc_add_reference(Symbol_table*, Object*, unsigned int,
998 Object*, unsigned int,
999 typename elfcpp::Elf_types<size>::Elf_Addr) const
1000 { }
1001 };
1002
1003 } // End namespace gold.
1004
1005 #endif // !defined(GOLD_TARGET_H)
This page took 0.063254 seconds and 5 git commands to generate.