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