| 1 | // x86_64.cc -- x86_64 target support for gold. |
| 2 | |
| 3 | // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
| 4 | // Written by Ian Lance Taylor <iant@google.com>. |
| 5 | |
| 6 | // This file is part of gold. |
| 7 | |
| 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. |
| 12 | |
| 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. |
| 17 | |
| 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. |
| 22 | |
| 23 | #include "gold.h" |
| 24 | |
| 25 | #include <cstring> |
| 26 | |
| 27 | #include "elfcpp.h" |
| 28 | #include "dwarf.h" |
| 29 | #include "parameters.h" |
| 30 | #include "reloc.h" |
| 31 | #include "x86_64.h" |
| 32 | #include "object.h" |
| 33 | #include "symtab.h" |
| 34 | #include "layout.h" |
| 35 | #include "output.h" |
| 36 | #include "copy-relocs.h" |
| 37 | #include "target.h" |
| 38 | #include "target-reloc.h" |
| 39 | #include "target-select.h" |
| 40 | #include "tls.h" |
| 41 | #include "freebsd.h" |
| 42 | #include "gc.h" |
| 43 | #include "icf.h" |
| 44 | |
| 45 | namespace |
| 46 | { |
| 47 | |
| 48 | using namespace gold; |
| 49 | |
| 50 | // A class to handle the PLT data. |
| 51 | |
| 52 | class Output_data_plt_x86_64 : public Output_section_data |
| 53 | { |
| 54 | public: |
| 55 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section; |
| 56 | |
| 57 | Output_data_plt_x86_64(Layout* layout, Output_data_got<64, false>* got, |
| 58 | Output_data_space* got_plt, |
| 59 | Output_data_space* got_irelative) |
| 60 | : Output_section_data(16), layout_(layout), tlsdesc_rel_(NULL), |
| 61 | irelative_rel_(NULL), got_(got), got_plt_(got_plt), |
| 62 | got_irelative_(got_irelative), count_(0), irelative_count_(0), |
| 63 | tlsdesc_got_offset_(-1U), free_list_() |
| 64 | { this->init(layout); } |
| 65 | |
| 66 | Output_data_plt_x86_64(Layout* layout, Output_data_got<64, false>* got, |
| 67 | Output_data_space* got_plt, |
| 68 | Output_data_space* got_irelative, |
| 69 | unsigned int plt_count) |
| 70 | : Output_section_data((plt_count + 1) * plt_entry_size, 16, false), |
| 71 | layout_(layout), tlsdesc_rel_(NULL), irelative_rel_(NULL), got_(got), |
| 72 | got_plt_(got_plt), got_irelative_(got_irelative), count_(plt_count), |
| 73 | irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_() |
| 74 | { |
| 75 | this->init(layout); |
| 76 | |
| 77 | // Initialize the free list and reserve the first entry. |
| 78 | this->free_list_.init((plt_count + 1) * plt_entry_size, false); |
| 79 | this->free_list_.remove(0, plt_entry_size); |
| 80 | } |
| 81 | |
| 82 | // Initialize the PLT section. |
| 83 | void |
| 84 | init(Layout* layout); |
| 85 | |
| 86 | // Add an entry to the PLT. |
| 87 | void |
| 88 | add_entry(Symbol_table*, Layout*, Symbol* gsym); |
| 89 | |
| 90 | // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. |
| 91 | unsigned int |
| 92 | add_local_ifunc_entry(Symbol_table* symtab, Layout*, |
| 93 | Sized_relobj_file<64, false>* relobj, |
| 94 | unsigned int local_sym_index); |
| 95 | |
| 96 | // Add the relocation for a PLT entry. |
| 97 | void |
| 98 | add_relocation(Symbol_table*, Layout*, Symbol* gsym, |
| 99 | unsigned int got_offset); |
| 100 | |
| 101 | // Add the reserved TLSDESC_PLT entry to the PLT. |
| 102 | void |
| 103 | reserve_tlsdesc_entry(unsigned int got_offset) |
| 104 | { this->tlsdesc_got_offset_ = got_offset; } |
| 105 | |
| 106 | // Return true if a TLSDESC_PLT entry has been reserved. |
| 107 | bool |
| 108 | has_tlsdesc_entry() const |
| 109 | { return this->tlsdesc_got_offset_ != -1U; } |
| 110 | |
| 111 | // Return the GOT offset for the reserved TLSDESC_PLT entry. |
| 112 | unsigned int |
| 113 | get_tlsdesc_got_offset() const |
| 114 | { return this->tlsdesc_got_offset_; } |
| 115 | |
| 116 | // Return the offset of the reserved TLSDESC_PLT entry. |
| 117 | unsigned int |
| 118 | get_tlsdesc_plt_offset() const |
| 119 | { return (this->count_ + this->irelative_count_ + 1) * plt_entry_size; } |
| 120 | |
| 121 | // Return the .rela.plt section data. |
| 122 | Reloc_section* |
| 123 | rela_plt() |
| 124 | { return this->rel_; } |
| 125 | |
| 126 | // Return where the TLSDESC relocations should go. |
| 127 | Reloc_section* |
| 128 | rela_tlsdesc(Layout*); |
| 129 | |
| 130 | // Return where the IRELATIVE relocations should go in the PLT |
| 131 | // relocations. |
| 132 | Reloc_section* |
| 133 | rela_irelative(Symbol_table*, Layout*); |
| 134 | |
| 135 | // Return whether we created a section for IRELATIVE relocations. |
| 136 | bool |
| 137 | has_irelative_section() const |
| 138 | { return this->irelative_rel_ != NULL; } |
| 139 | |
| 140 | // Return the number of PLT entries. |
| 141 | unsigned int |
| 142 | entry_count() const |
| 143 | { return this->count_ + this->irelative_count_; } |
| 144 | |
| 145 | // Return the offset of the first non-reserved PLT entry. |
| 146 | static unsigned int |
| 147 | first_plt_entry_offset() |
| 148 | { return plt_entry_size; } |
| 149 | |
| 150 | // Return the size of a PLT entry. |
| 151 | static unsigned int |
| 152 | get_plt_entry_size() |
| 153 | { return plt_entry_size; } |
| 154 | |
| 155 | // Reserve a slot in the PLT for an existing symbol in an incremental update. |
| 156 | void |
| 157 | reserve_slot(unsigned int plt_index) |
| 158 | { |
| 159 | this->free_list_.remove((plt_index + 1) * plt_entry_size, |
| 160 | (plt_index + 2) * plt_entry_size); |
| 161 | } |
| 162 | |
| 163 | // Return the PLT address to use for a global symbol. |
| 164 | uint64_t |
| 165 | address_for_global(const Symbol*); |
| 166 | |
| 167 | // Return the PLT address to use for a local symbol. |
| 168 | uint64_t |
| 169 | address_for_local(const Relobj*, unsigned int symndx); |
| 170 | |
| 171 | protected: |
| 172 | void |
| 173 | do_adjust_output_section(Output_section* os); |
| 174 | |
| 175 | // Write to a map file. |
| 176 | void |
| 177 | do_print_to_mapfile(Mapfile* mapfile) const |
| 178 | { mapfile->print_output_data(this, _("** PLT")); } |
| 179 | |
| 180 | private: |
| 181 | // The size of an entry in the PLT. |
| 182 | static const int plt_entry_size = 16; |
| 183 | |
| 184 | // The first entry in the PLT. |
| 185 | // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same |
| 186 | // procedure linkage table for both programs and shared objects." |
| 187 | static const unsigned char first_plt_entry[plt_entry_size]; |
| 188 | |
| 189 | // Other entries in the PLT for an executable. |
| 190 | static const unsigned char plt_entry[plt_entry_size]; |
| 191 | |
| 192 | // The reserved TLSDESC entry in the PLT for an executable. |
| 193 | static const unsigned char tlsdesc_plt_entry[plt_entry_size]; |
| 194 | |
| 195 | // The .eh_frame unwind information for the PLT. |
| 196 | static const int plt_eh_frame_cie_size = 16; |
| 197 | static const int plt_eh_frame_fde_size = 32; |
| 198 | static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size]; |
| 199 | static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size]; |
| 200 | |
| 201 | // Set the final size. |
| 202 | void |
| 203 | set_final_data_size(); |
| 204 | |
| 205 | // Write out the PLT data. |
| 206 | void |
| 207 | do_write(Output_file*); |
| 208 | |
| 209 | // A pointer to the Layout class, so that we can find the .dynamic |
| 210 | // section when we write out the GOT PLT section. |
| 211 | Layout* layout_; |
| 212 | // The reloc section. |
| 213 | Reloc_section* rel_; |
| 214 | // The TLSDESC relocs, if necessary. These must follow the regular |
| 215 | // PLT relocs. |
| 216 | Reloc_section* tlsdesc_rel_; |
| 217 | // The IRELATIVE relocs, if necessary. These must follow the |
| 218 | // regular PLT relocations and the TLSDESC relocations. |
| 219 | Reloc_section* irelative_rel_; |
| 220 | // The .got section. |
| 221 | Output_data_got<64, false>* got_; |
| 222 | // The .got.plt section. |
| 223 | Output_data_space* got_plt_; |
| 224 | // The part of the .got.plt section used for IRELATIVE relocs. |
| 225 | Output_data_space* got_irelative_; |
| 226 | // The number of PLT entries. |
| 227 | unsigned int count_; |
| 228 | // Number of PLT entries with R_X86_64_IRELATIVE relocs. These |
| 229 | // follow the regular PLT entries. |
| 230 | unsigned int irelative_count_; |
| 231 | // Offset of the reserved TLSDESC_GOT entry when needed. |
| 232 | unsigned int tlsdesc_got_offset_; |
| 233 | // List of available regions within the section, for incremental |
| 234 | // update links. |
| 235 | Free_list free_list_; |
| 236 | }; |
| 237 | |
| 238 | // The x86_64 target class. |
| 239 | // See the ABI at |
| 240 | // http://www.x86-64.org/documentation/abi.pdf |
| 241 | // TLS info comes from |
| 242 | // http://people.redhat.com/drepper/tls.pdf |
| 243 | // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt |
| 244 | |
| 245 | class Target_x86_64 : public Sized_target<64, false> |
| 246 | { |
| 247 | public: |
| 248 | // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures |
| 249 | // uses only Elf64_Rela relocation entries with explicit addends." |
| 250 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section; |
| 251 | |
| 252 | Target_x86_64() |
| 253 | : Sized_target<64, false>(&x86_64_info), |
| 254 | got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL), |
| 255 | got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL), |
| 256 | rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY), |
| 257 | dynbss_(NULL), got_mod_index_offset_(-1U), tlsdesc_reloc_info_(), |
| 258 | tls_base_symbol_defined_(false) |
| 259 | { } |
| 260 | |
| 261 | // Hook for a new output section. |
| 262 | void |
| 263 | do_new_output_section(Output_section*) const; |
| 264 | |
| 265 | // Scan the relocations to look for symbol adjustments. |
| 266 | void |
| 267 | gc_process_relocs(Symbol_table* symtab, |
| 268 | Layout* layout, |
| 269 | Sized_relobj_file<64, false>* object, |
| 270 | unsigned int data_shndx, |
| 271 | unsigned int sh_type, |
| 272 | const unsigned char* prelocs, |
| 273 | size_t reloc_count, |
| 274 | Output_section* output_section, |
| 275 | bool needs_special_offset_handling, |
| 276 | size_t local_symbol_count, |
| 277 | const unsigned char* plocal_symbols); |
| 278 | |
| 279 | // Scan the relocations to look for symbol adjustments. |
| 280 | void |
| 281 | scan_relocs(Symbol_table* symtab, |
| 282 | Layout* layout, |
| 283 | Sized_relobj_file<64, false>* object, |
| 284 | unsigned int data_shndx, |
| 285 | unsigned int sh_type, |
| 286 | const unsigned char* prelocs, |
| 287 | size_t reloc_count, |
| 288 | Output_section* output_section, |
| 289 | bool needs_special_offset_handling, |
| 290 | size_t local_symbol_count, |
| 291 | const unsigned char* plocal_symbols); |
| 292 | |
| 293 | // Finalize the sections. |
| 294 | void |
| 295 | do_finalize_sections(Layout*, const Input_objects*, Symbol_table*); |
| 296 | |
| 297 | // Return the value to use for a dynamic which requires special |
| 298 | // treatment. |
| 299 | uint64_t |
| 300 | do_dynsym_value(const Symbol*) const; |
| 301 | |
| 302 | // Relocate a section. |
| 303 | void |
| 304 | relocate_section(const Relocate_info<64, false>*, |
| 305 | unsigned int sh_type, |
| 306 | const unsigned char* prelocs, |
| 307 | size_t reloc_count, |
| 308 | Output_section* output_section, |
| 309 | bool needs_special_offset_handling, |
| 310 | unsigned char* view, |
| 311 | elfcpp::Elf_types<64>::Elf_Addr view_address, |
| 312 | section_size_type view_size, |
| 313 | const Reloc_symbol_changes*); |
| 314 | |
| 315 | // Scan the relocs during a relocatable link. |
| 316 | void |
| 317 | scan_relocatable_relocs(Symbol_table* symtab, |
| 318 | Layout* layout, |
| 319 | Sized_relobj_file<64, false>* object, |
| 320 | unsigned int data_shndx, |
| 321 | unsigned int sh_type, |
| 322 | const unsigned char* prelocs, |
| 323 | size_t reloc_count, |
| 324 | Output_section* output_section, |
| 325 | bool needs_special_offset_handling, |
| 326 | size_t local_symbol_count, |
| 327 | const unsigned char* plocal_symbols, |
| 328 | Relocatable_relocs*); |
| 329 | |
| 330 | // Relocate a section during a relocatable link. |
| 331 | void |
| 332 | relocate_for_relocatable(const Relocate_info<64, false>*, |
| 333 | unsigned int sh_type, |
| 334 | const unsigned char* prelocs, |
| 335 | size_t reloc_count, |
| 336 | Output_section* output_section, |
| 337 | off_t offset_in_output_section, |
| 338 | const Relocatable_relocs*, |
| 339 | unsigned char* view, |
| 340 | elfcpp::Elf_types<64>::Elf_Addr view_address, |
| 341 | section_size_type view_size, |
| 342 | unsigned char* reloc_view, |
| 343 | section_size_type reloc_view_size); |
| 344 | |
| 345 | // Return a string used to fill a code section with nops. |
| 346 | std::string |
| 347 | do_code_fill(section_size_type length) const; |
| 348 | |
| 349 | // Return whether SYM is defined by the ABI. |
| 350 | bool |
| 351 | do_is_defined_by_abi(const Symbol* sym) const |
| 352 | { return strcmp(sym->name(), "__tls_get_addr") == 0; } |
| 353 | |
| 354 | // Return the symbol index to use for a target specific relocation. |
| 355 | // The only target specific relocation is R_X86_64_TLSDESC for a |
| 356 | // local symbol, which is an absolute reloc. |
| 357 | unsigned int |
| 358 | do_reloc_symbol_index(void*, unsigned int r_type) const |
| 359 | { |
| 360 | gold_assert(r_type == elfcpp::R_X86_64_TLSDESC); |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | // Return the addend to use for a target specific relocation. |
| 365 | uint64_t |
| 366 | do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const; |
| 367 | |
| 368 | // Return the PLT section. |
| 369 | uint64_t |
| 370 | do_plt_address_for_global(const Symbol* gsym) const |
| 371 | { return this->plt_section()->address_for_global(gsym); } |
| 372 | |
| 373 | uint64_t |
| 374 | do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const |
| 375 | { return this->plt_section()->address_for_local(relobj, symndx); } |
| 376 | |
| 377 | // This function should be defined in targets that can use relocation |
| 378 | // types to determine (implemented in local_reloc_may_be_function_pointer |
| 379 | // and global_reloc_may_be_function_pointer) |
| 380 | // if a function's pointer is taken. ICF uses this in safe mode to only |
| 381 | // fold those functions whose pointer is defintely not taken. For x86_64 |
| 382 | // pie binaries, safe ICF cannot be done by looking at relocation types. |
| 383 | bool |
| 384 | do_can_check_for_function_pointers() const |
| 385 | { return !parameters->options().pie(); } |
| 386 | |
| 387 | // Return the base for a DW_EH_PE_datarel encoding. |
| 388 | uint64_t |
| 389 | do_ehframe_datarel_base() const; |
| 390 | |
| 391 | // Adjust -fsplit-stack code which calls non-split-stack code. |
| 392 | void |
| 393 | do_calls_non_split(Relobj* object, unsigned int shndx, |
| 394 | section_offset_type fnoffset, section_size_type fnsize, |
| 395 | unsigned char* view, section_size_type view_size, |
| 396 | std::string* from, std::string* to) const; |
| 397 | |
| 398 | // Return the size of the GOT section. |
| 399 | section_size_type |
| 400 | got_size() const |
| 401 | { |
| 402 | gold_assert(this->got_ != NULL); |
| 403 | return this->got_->data_size(); |
| 404 | } |
| 405 | |
| 406 | // Return the number of entries in the GOT. |
| 407 | unsigned int |
| 408 | got_entry_count() const |
| 409 | { |
| 410 | if (this->got_ == NULL) |
| 411 | return 0; |
| 412 | return this->got_size() / 8; |
| 413 | } |
| 414 | |
| 415 | // Return the number of entries in the PLT. |
| 416 | unsigned int |
| 417 | plt_entry_count() const; |
| 418 | |
| 419 | // Return the offset of the first non-reserved PLT entry. |
| 420 | unsigned int |
| 421 | first_plt_entry_offset() const; |
| 422 | |
| 423 | // Return the size of each PLT entry. |
| 424 | unsigned int |
| 425 | plt_entry_size() const; |
| 426 | |
| 427 | // Create the GOT section for an incremental update. |
| 428 | Output_data_got<64, false>* |
| 429 | init_got_plt_for_update(Symbol_table* symtab, |
| 430 | Layout* layout, |
| 431 | unsigned int got_count, |
| 432 | unsigned int plt_count); |
| 433 | |
| 434 | // Reserve a GOT entry for a local symbol, and regenerate any |
| 435 | // necessary dynamic relocations. |
| 436 | void |
| 437 | reserve_local_got_entry(unsigned int got_index, |
| 438 | Sized_relobj<64, false>* obj, |
| 439 | unsigned int r_sym, |
| 440 | unsigned int got_type); |
| 441 | |
| 442 | // Reserve a GOT entry for a global symbol, and regenerate any |
| 443 | // necessary dynamic relocations. |
| 444 | void |
| 445 | reserve_global_got_entry(unsigned int got_index, Symbol* gsym, |
| 446 | unsigned int got_type); |
| 447 | |
| 448 | // Register an existing PLT entry for a global symbol. |
| 449 | void |
| 450 | register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index, |
| 451 | Symbol* gsym); |
| 452 | |
| 453 | // Force a COPY relocation for a given symbol. |
| 454 | void |
| 455 | emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t); |
| 456 | |
| 457 | // Apply an incremental relocation. |
| 458 | void |
| 459 | apply_relocation(const Relocate_info<64, false>* relinfo, |
| 460 | elfcpp::Elf_types<64>::Elf_Addr r_offset, |
| 461 | unsigned int r_type, |
| 462 | elfcpp::Elf_types<64>::Elf_Swxword r_addend, |
| 463 | const Symbol* gsym, |
| 464 | unsigned char* view, |
| 465 | elfcpp::Elf_types<64>::Elf_Addr address, |
| 466 | section_size_type view_size); |
| 467 | |
| 468 | // Add a new reloc argument, returning the index in the vector. |
| 469 | size_t |
| 470 | add_tlsdesc_info(Sized_relobj_file<64, false>* object, unsigned int r_sym) |
| 471 | { |
| 472 | this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym)); |
| 473 | return this->tlsdesc_reloc_info_.size() - 1; |
| 474 | } |
| 475 | |
| 476 | private: |
| 477 | // The class which scans relocations. |
| 478 | class Scan |
| 479 | { |
| 480 | public: |
| 481 | Scan() |
| 482 | : issued_non_pic_error_(false) |
| 483 | { } |
| 484 | |
| 485 | static inline int |
| 486 | get_reference_flags(unsigned int r_type); |
| 487 | |
| 488 | inline void |
| 489 | local(Symbol_table* symtab, Layout* layout, Target_x86_64* target, |
| 490 | Sized_relobj_file<64, false>* object, |
| 491 | unsigned int data_shndx, |
| 492 | Output_section* output_section, |
| 493 | const elfcpp::Rela<64, false>& reloc, unsigned int r_type, |
| 494 | const elfcpp::Sym<64, false>& lsym); |
| 495 | |
| 496 | inline void |
| 497 | global(Symbol_table* symtab, Layout* layout, Target_x86_64* target, |
| 498 | Sized_relobj_file<64, false>* object, |
| 499 | unsigned int data_shndx, |
| 500 | Output_section* output_section, |
| 501 | const elfcpp::Rela<64, false>& reloc, unsigned int r_type, |
| 502 | Symbol* gsym); |
| 503 | |
| 504 | inline bool |
| 505 | local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout, |
| 506 | Target_x86_64* target, |
| 507 | Sized_relobj_file<64, false>* object, |
| 508 | unsigned int data_shndx, |
| 509 | Output_section* output_section, |
| 510 | const elfcpp::Rela<64, false>& reloc, |
| 511 | unsigned int r_type, |
| 512 | const elfcpp::Sym<64, false>& lsym); |
| 513 | |
| 514 | inline bool |
| 515 | global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout, |
| 516 | Target_x86_64* target, |
| 517 | Sized_relobj_file<64, false>* object, |
| 518 | unsigned int data_shndx, |
| 519 | Output_section* output_section, |
| 520 | const elfcpp::Rela<64, false>& reloc, |
| 521 | unsigned int r_type, |
| 522 | Symbol* gsym); |
| 523 | |
| 524 | private: |
| 525 | static void |
| 526 | unsupported_reloc_local(Sized_relobj_file<64, false>*, unsigned int r_type); |
| 527 | |
| 528 | static void |
| 529 | unsupported_reloc_global(Sized_relobj_file<64, false>*, unsigned int r_type, |
| 530 | Symbol*); |
| 531 | |
| 532 | void |
| 533 | check_non_pic(Relobj*, unsigned int r_type, Symbol*); |
| 534 | |
| 535 | inline bool |
| 536 | possible_function_pointer_reloc(unsigned int r_type); |
| 537 | |
| 538 | bool |
| 539 | reloc_needs_plt_for_ifunc(Sized_relobj_file<64, false>*, |
| 540 | unsigned int r_type); |
| 541 | |
| 542 | // Whether we have issued an error about a non-PIC compilation. |
| 543 | bool issued_non_pic_error_; |
| 544 | }; |
| 545 | |
| 546 | // The class which implements relocation. |
| 547 | class Relocate |
| 548 | { |
| 549 | public: |
| 550 | Relocate() |
| 551 | : skip_call_tls_get_addr_(false) |
| 552 | { } |
| 553 | |
| 554 | ~Relocate() |
| 555 | { |
| 556 | if (this->skip_call_tls_get_addr_) |
| 557 | { |
| 558 | // FIXME: This needs to specify the location somehow. |
| 559 | gold_error(_("missing expected TLS relocation")); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | // Do a relocation. Return false if the caller should not issue |
| 564 | // any warnings about this relocation. |
| 565 | inline bool |
| 566 | relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*, |
| 567 | size_t relnum, const elfcpp::Rela<64, false>&, |
| 568 | unsigned int r_type, const Sized_symbol<64>*, |
| 569 | const Symbol_value<64>*, |
| 570 | unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, |
| 571 | section_size_type); |
| 572 | |
| 573 | private: |
| 574 | // Do a TLS relocation. |
| 575 | inline void |
| 576 | relocate_tls(const Relocate_info<64, false>*, Target_x86_64*, |
| 577 | size_t relnum, const elfcpp::Rela<64, false>&, |
| 578 | unsigned int r_type, const Sized_symbol<64>*, |
| 579 | const Symbol_value<64>*, |
| 580 | unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, |
| 581 | section_size_type); |
| 582 | |
| 583 | // Do a TLS General-Dynamic to Initial-Exec transition. |
| 584 | inline void |
| 585 | tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum, |
| 586 | Output_segment* tls_segment, |
| 587 | const elfcpp::Rela<64, false>&, unsigned int r_type, |
| 588 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 589 | unsigned char* view, |
| 590 | elfcpp::Elf_types<64>::Elf_Addr, |
| 591 | section_size_type view_size); |
| 592 | |
| 593 | // Do a TLS General-Dynamic to Local-Exec transition. |
| 594 | inline void |
| 595 | tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum, |
| 596 | Output_segment* tls_segment, |
| 597 | const elfcpp::Rela<64, false>&, unsigned int r_type, |
| 598 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 599 | unsigned char* view, |
| 600 | section_size_type view_size); |
| 601 | |
| 602 | // Do a TLSDESC-style General-Dynamic to Initial-Exec transition. |
| 603 | inline void |
| 604 | tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum, |
| 605 | Output_segment* tls_segment, |
| 606 | const elfcpp::Rela<64, false>&, unsigned int r_type, |
| 607 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 608 | unsigned char* view, |
| 609 | elfcpp::Elf_types<64>::Elf_Addr, |
| 610 | section_size_type view_size); |
| 611 | |
| 612 | // Do a TLSDESC-style General-Dynamic to Local-Exec transition. |
| 613 | inline void |
| 614 | tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum, |
| 615 | Output_segment* tls_segment, |
| 616 | const elfcpp::Rela<64, false>&, unsigned int r_type, |
| 617 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 618 | unsigned char* view, |
| 619 | section_size_type view_size); |
| 620 | |
| 621 | // Do a TLS Local-Dynamic to Local-Exec transition. |
| 622 | inline void |
| 623 | tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum, |
| 624 | Output_segment* tls_segment, |
| 625 | const elfcpp::Rela<64, false>&, unsigned int r_type, |
| 626 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 627 | unsigned char* view, |
| 628 | section_size_type view_size); |
| 629 | |
| 630 | // Do a TLS Initial-Exec to Local-Exec transition. |
| 631 | static inline void |
| 632 | tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum, |
| 633 | Output_segment* tls_segment, |
| 634 | const elfcpp::Rela<64, false>&, unsigned int r_type, |
| 635 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 636 | unsigned char* view, |
| 637 | section_size_type view_size); |
| 638 | |
| 639 | // This is set if we should skip the next reloc, which should be a |
| 640 | // PLT32 reloc against ___tls_get_addr. |
| 641 | bool skip_call_tls_get_addr_; |
| 642 | }; |
| 643 | |
| 644 | // A class which returns the size required for a relocation type, |
| 645 | // used while scanning relocs during a relocatable link. |
| 646 | class Relocatable_size_for_reloc |
| 647 | { |
| 648 | public: |
| 649 | unsigned int |
| 650 | get_size_for_reloc(unsigned int, Relobj*); |
| 651 | }; |
| 652 | |
| 653 | // Adjust TLS relocation type based on the options and whether this |
| 654 | // is a local symbol. |
| 655 | static tls::Tls_optimization |
| 656 | optimize_tls_reloc(bool is_final, int r_type); |
| 657 | |
| 658 | // Get the GOT section, creating it if necessary. |
| 659 | Output_data_got<64, false>* |
| 660 | got_section(Symbol_table*, Layout*); |
| 661 | |
| 662 | // Get the GOT PLT section. |
| 663 | Output_data_space* |
| 664 | got_plt_section() const |
| 665 | { |
| 666 | gold_assert(this->got_plt_ != NULL); |
| 667 | return this->got_plt_; |
| 668 | } |
| 669 | |
| 670 | // Get the GOT section for TLSDESC entries. |
| 671 | Output_data_got<64, false>* |
| 672 | got_tlsdesc_section() const |
| 673 | { |
| 674 | gold_assert(this->got_tlsdesc_ != NULL); |
| 675 | return this->got_tlsdesc_; |
| 676 | } |
| 677 | |
| 678 | // Create the PLT section. |
| 679 | void |
| 680 | make_plt_section(Symbol_table* symtab, Layout* layout); |
| 681 | |
| 682 | // Create a PLT entry for a global symbol. |
| 683 | void |
| 684 | make_plt_entry(Symbol_table*, Layout*, Symbol*); |
| 685 | |
| 686 | // Create a PLT entry for a local STT_GNU_IFUNC symbol. |
| 687 | void |
| 688 | make_local_ifunc_plt_entry(Symbol_table*, Layout*, |
| 689 | Sized_relobj_file<64, false>* relobj, |
| 690 | unsigned int local_sym_index); |
| 691 | |
| 692 | // Define the _TLS_MODULE_BASE_ symbol in the TLS segment. |
| 693 | void |
| 694 | define_tls_base_symbol(Symbol_table*, Layout*); |
| 695 | |
| 696 | // Create the reserved PLT and GOT entries for the TLS descriptor resolver. |
| 697 | void |
| 698 | reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout); |
| 699 | |
| 700 | // Create a GOT entry for the TLS module index. |
| 701 | unsigned int |
| 702 | got_mod_index_entry(Symbol_table* symtab, Layout* layout, |
| 703 | Sized_relobj_file<64, false>* object); |
| 704 | |
| 705 | // Get the PLT section. |
| 706 | Output_data_plt_x86_64* |
| 707 | plt_section() const |
| 708 | { |
| 709 | gold_assert(this->plt_ != NULL); |
| 710 | return this->plt_; |
| 711 | } |
| 712 | |
| 713 | // Get the dynamic reloc section, creating it if necessary. |
| 714 | Reloc_section* |
| 715 | rela_dyn_section(Layout*); |
| 716 | |
| 717 | // Get the section to use for TLSDESC relocations. |
| 718 | Reloc_section* |
| 719 | rela_tlsdesc_section(Layout*) const; |
| 720 | |
| 721 | // Get the section to use for IRELATIVE relocations. |
| 722 | Reloc_section* |
| 723 | rela_irelative_section(Layout*); |
| 724 | |
| 725 | // Add a potential copy relocation. |
| 726 | void |
| 727 | copy_reloc(Symbol_table* symtab, Layout* layout, |
| 728 | Sized_relobj_file<64, false>* object, |
| 729 | unsigned int shndx, Output_section* output_section, |
| 730 | Symbol* sym, const elfcpp::Rela<64, false>& reloc) |
| 731 | { |
| 732 | this->copy_relocs_.copy_reloc(symtab, layout, |
| 733 | symtab->get_sized_symbol<64>(sym), |
| 734 | object, shndx, output_section, |
| 735 | reloc, this->rela_dyn_section(layout)); |
| 736 | } |
| 737 | |
| 738 | // Information about this specific target which we pass to the |
| 739 | // general Target structure. |
| 740 | static const Target::Target_info x86_64_info; |
| 741 | |
| 742 | // The types of GOT entries needed for this platform. |
| 743 | // These values are exposed to the ABI in an incremental link. |
| 744 | // Do not renumber existing values without changing the version |
| 745 | // number of the .gnu_incremental_inputs section. |
| 746 | enum Got_type |
| 747 | { |
| 748 | GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol |
| 749 | GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset |
| 750 | GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair |
| 751 | GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair |
| 752 | }; |
| 753 | |
| 754 | // This type is used as the argument to the target specific |
| 755 | // relocation routines. The only target specific reloc is |
| 756 | // R_X86_64_TLSDESC against a local symbol. |
| 757 | struct Tlsdesc_info |
| 758 | { |
| 759 | Tlsdesc_info(Sized_relobj_file<64, false>* a_object, unsigned int a_r_sym) |
| 760 | : object(a_object), r_sym(a_r_sym) |
| 761 | { } |
| 762 | |
| 763 | // The object in which the local symbol is defined. |
| 764 | Sized_relobj_file<64, false>* object; |
| 765 | // The local symbol index in the object. |
| 766 | unsigned int r_sym; |
| 767 | }; |
| 768 | |
| 769 | // The GOT section. |
| 770 | Output_data_got<64, false>* got_; |
| 771 | // The PLT section. |
| 772 | Output_data_plt_x86_64* plt_; |
| 773 | // The GOT PLT section. |
| 774 | Output_data_space* got_plt_; |
| 775 | // The GOT section for IRELATIVE relocations. |
| 776 | Output_data_space* got_irelative_; |
| 777 | // The GOT section for TLSDESC relocations. |
| 778 | Output_data_got<64, false>* got_tlsdesc_; |
| 779 | // The _GLOBAL_OFFSET_TABLE_ symbol. |
| 780 | Symbol* global_offset_table_; |
| 781 | // The dynamic reloc section. |
| 782 | Reloc_section* rela_dyn_; |
| 783 | // The section to use for IRELATIVE relocs. |
| 784 | Reloc_section* rela_irelative_; |
| 785 | // Relocs saved to avoid a COPY reloc. |
| 786 | Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_; |
| 787 | // Space for variables copied with a COPY reloc. |
| 788 | Output_data_space* dynbss_; |
| 789 | // Offset of the GOT entry for the TLS module index. |
| 790 | unsigned int got_mod_index_offset_; |
| 791 | // We handle R_X86_64_TLSDESC against a local symbol as a target |
| 792 | // specific relocation. Here we store the object and local symbol |
| 793 | // index for the relocation. |
| 794 | std::vector<Tlsdesc_info> tlsdesc_reloc_info_; |
| 795 | // True if the _TLS_MODULE_BASE_ symbol has been defined. |
| 796 | bool tls_base_symbol_defined_; |
| 797 | }; |
| 798 | |
| 799 | const Target::Target_info Target_x86_64::x86_64_info = |
| 800 | { |
| 801 | 64, // size |
| 802 | false, // is_big_endian |
| 803 | elfcpp::EM_X86_64, // machine_code |
| 804 | false, // has_make_symbol |
| 805 | false, // has_resolve |
| 806 | true, // has_code_fill |
| 807 | true, // is_default_stack_executable |
| 808 | true, // can_icf_inline_merge_sections |
| 809 | '\0', // wrap_char |
| 810 | "/lib/ld64.so.1", // program interpreter |
| 811 | 0x400000, // default_text_segment_address |
| 812 | 0x1000, // abi_pagesize (overridable by -z max-page-size) |
| 813 | 0x1000, // common_pagesize (overridable by -z common-page-size) |
| 814 | elfcpp::SHN_UNDEF, // small_common_shndx |
| 815 | elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx |
| 816 | 0, // small_common_section_flags |
| 817 | elfcpp::SHF_X86_64_LARGE, // large_common_section_flags |
| 818 | NULL, // attributes_section |
| 819 | NULL // attributes_vendor |
| 820 | }; |
| 821 | |
| 822 | // This is called when a new output section is created. This is where |
| 823 | // we handle the SHF_X86_64_LARGE. |
| 824 | |
| 825 | void |
| 826 | Target_x86_64::do_new_output_section(Output_section* os) const |
| 827 | { |
| 828 | if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0) |
| 829 | os->set_is_large_section(); |
| 830 | } |
| 831 | |
| 832 | // Get the GOT section, creating it if necessary. |
| 833 | |
| 834 | Output_data_got<64, false>* |
| 835 | Target_x86_64::got_section(Symbol_table* symtab, Layout* layout) |
| 836 | { |
| 837 | if (this->got_ == NULL) |
| 838 | { |
| 839 | gold_assert(symtab != NULL && layout != NULL); |
| 840 | |
| 841 | // When using -z now, we can treat .got.plt as a relro section. |
| 842 | // Without -z now, it is modified after program startup by lazy |
| 843 | // PLT relocations. |
| 844 | bool is_got_plt_relro = parameters->options().now(); |
| 845 | Output_section_order got_order = (is_got_plt_relro |
| 846 | ? ORDER_RELRO |
| 847 | : ORDER_RELRO_LAST); |
| 848 | Output_section_order got_plt_order = (is_got_plt_relro |
| 849 | ? ORDER_RELRO |
| 850 | : ORDER_NON_RELRO_FIRST); |
| 851 | |
| 852 | this->got_ = new Output_data_got<64, false>(); |
| 853 | |
| 854 | layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, |
| 855 | (elfcpp::SHF_ALLOC |
| 856 | | elfcpp::SHF_WRITE), |
| 857 | this->got_, got_order, true); |
| 858 | |
| 859 | this->got_plt_ = new Output_data_space(8, "** GOT PLT"); |
| 860 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, |
| 861 | (elfcpp::SHF_ALLOC |
| 862 | | elfcpp::SHF_WRITE), |
| 863 | this->got_plt_, got_plt_order, |
| 864 | is_got_plt_relro); |
| 865 | |
| 866 | // The first three entries are reserved. |
| 867 | this->got_plt_->set_current_data_size(3 * 8); |
| 868 | |
| 869 | if (!is_got_plt_relro) |
| 870 | { |
| 871 | // Those bytes can go into the relro segment. |
| 872 | layout->increase_relro(3 * 8); |
| 873 | } |
| 874 | |
| 875 | // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT. |
| 876 | this->global_offset_table_ = |
| 877 | symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, |
| 878 | Symbol_table::PREDEFINED, |
| 879 | this->got_plt_, |
| 880 | 0, 0, elfcpp::STT_OBJECT, |
| 881 | elfcpp::STB_LOCAL, |
| 882 | elfcpp::STV_HIDDEN, 0, |
| 883 | false, false); |
| 884 | |
| 885 | // If there are any IRELATIVE relocations, they get GOT entries |
| 886 | // in .got.plt after the jump slot entries. |
| 887 | this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT"); |
| 888 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, |
| 889 | (elfcpp::SHF_ALLOC |
| 890 | | elfcpp::SHF_WRITE), |
| 891 | this->got_irelative_, |
| 892 | got_plt_order, is_got_plt_relro); |
| 893 | |
| 894 | // If there are any TLSDESC relocations, they get GOT entries in |
| 895 | // .got.plt after the jump slot and IRELATIVE entries. |
| 896 | this->got_tlsdesc_ = new Output_data_got<64, false>(); |
| 897 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, |
| 898 | (elfcpp::SHF_ALLOC |
| 899 | | elfcpp::SHF_WRITE), |
| 900 | this->got_tlsdesc_, |
| 901 | got_plt_order, is_got_plt_relro); |
| 902 | } |
| 903 | |
| 904 | return this->got_; |
| 905 | } |
| 906 | |
| 907 | // Get the dynamic reloc section, creating it if necessary. |
| 908 | |
| 909 | Target_x86_64::Reloc_section* |
| 910 | Target_x86_64::rela_dyn_section(Layout* layout) |
| 911 | { |
| 912 | if (this->rela_dyn_ == NULL) |
| 913 | { |
| 914 | gold_assert(layout != NULL); |
| 915 | this->rela_dyn_ = new Reloc_section(parameters->options().combreloc()); |
| 916 | layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA, |
| 917 | elfcpp::SHF_ALLOC, this->rela_dyn_, |
| 918 | ORDER_DYNAMIC_RELOCS, false); |
| 919 | } |
| 920 | return this->rela_dyn_; |
| 921 | } |
| 922 | |
| 923 | // Get the section to use for IRELATIVE relocs, creating it if |
| 924 | // necessary. These go in .rela.dyn, but only after all other dynamic |
| 925 | // relocations. They need to follow the other dynamic relocations so |
| 926 | // that they can refer to global variables initialized by those |
| 927 | // relocs. |
| 928 | |
| 929 | Target_x86_64::Reloc_section* |
| 930 | Target_x86_64::rela_irelative_section(Layout* layout) |
| 931 | { |
| 932 | if (this->rela_irelative_ == NULL) |
| 933 | { |
| 934 | // Make sure we have already created the dynamic reloc section. |
| 935 | this->rela_dyn_section(layout); |
| 936 | this->rela_irelative_ = new Reloc_section(false); |
| 937 | layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA, |
| 938 | elfcpp::SHF_ALLOC, this->rela_irelative_, |
| 939 | ORDER_DYNAMIC_RELOCS, false); |
| 940 | gold_assert(this->rela_dyn_->output_section() |
| 941 | == this->rela_irelative_->output_section()); |
| 942 | } |
| 943 | return this->rela_irelative_; |
| 944 | } |
| 945 | |
| 946 | // Initialize the PLT section. |
| 947 | |
| 948 | void |
| 949 | Output_data_plt_x86_64::init(Layout* layout) |
| 950 | { |
| 951 | this->rel_ = new Reloc_section(false); |
| 952 | layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, |
| 953 | elfcpp::SHF_ALLOC, this->rel_, |
| 954 | ORDER_DYNAMIC_PLT_RELOCS, false); |
| 955 | |
| 956 | // Add unwind information if requested. |
| 957 | if (parameters->options().ld_generated_unwind_info()) |
| 958 | layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size, |
| 959 | plt_eh_frame_fde, plt_eh_frame_fde_size); |
| 960 | } |
| 961 | |
| 962 | void |
| 963 | Output_data_plt_x86_64::do_adjust_output_section(Output_section* os) |
| 964 | { |
| 965 | os->set_entsize(plt_entry_size); |
| 966 | } |
| 967 | |
| 968 | // Add an entry to the PLT. |
| 969 | |
| 970 | void |
| 971 | Output_data_plt_x86_64::add_entry(Symbol_table* symtab, Layout* layout, |
| 972 | Symbol* gsym) |
| 973 | { |
| 974 | gold_assert(!gsym->has_plt_offset()); |
| 975 | |
| 976 | unsigned int plt_index; |
| 977 | off_t plt_offset; |
| 978 | section_offset_type got_offset; |
| 979 | |
| 980 | unsigned int* pcount; |
| 981 | unsigned int offset; |
| 982 | unsigned int reserved; |
| 983 | Output_data_space* got; |
| 984 | if (gsym->type() == elfcpp::STT_GNU_IFUNC |
| 985 | && gsym->can_use_relative_reloc(false)) |
| 986 | { |
| 987 | pcount = &this->irelative_count_; |
| 988 | offset = 0; |
| 989 | reserved = 0; |
| 990 | got = this->got_irelative_; |
| 991 | } |
| 992 | else |
| 993 | { |
| 994 | pcount = &this->count_; |
| 995 | offset = 1; |
| 996 | reserved = 3; |
| 997 | got = this->got_plt_; |
| 998 | } |
| 999 | |
| 1000 | if (!this->is_data_size_valid()) |
| 1001 | { |
| 1002 | // Note that when setting the PLT offset for a non-IRELATIVE |
| 1003 | // entry we skip the initial reserved PLT entry. |
| 1004 | plt_index = *pcount + offset; |
| 1005 | plt_offset = plt_index * plt_entry_size; |
| 1006 | |
| 1007 | ++*pcount; |
| 1008 | |
| 1009 | got_offset = (plt_index - offset + reserved) * 8; |
| 1010 | gold_assert(got_offset == got->current_data_size()); |
| 1011 | |
| 1012 | // Every PLT entry needs a GOT entry which points back to the PLT |
| 1013 | // entry (this will be changed by the dynamic linker, normally |
| 1014 | // lazily when the function is called). |
| 1015 | got->set_current_data_size(got_offset + 8); |
| 1016 | } |
| 1017 | else |
| 1018 | { |
| 1019 | // FIXME: This is probably not correct for IRELATIVE relocs. |
| 1020 | |
| 1021 | // For incremental updates, find an available slot. |
| 1022 | plt_offset = this->free_list_.allocate(plt_entry_size, plt_entry_size, 0); |
| 1023 | if (plt_offset == -1) |
| 1024 | gold_fallback(_("out of patch space (PLT);" |
| 1025 | " relink with --incremental-full")); |
| 1026 | |
| 1027 | // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset |
| 1028 | // can be calculated from the PLT index, adjusting for the three |
| 1029 | // reserved entries at the beginning of the GOT. |
| 1030 | plt_index = plt_offset / plt_entry_size - 1; |
| 1031 | got_offset = (plt_index - offset + reserved) * 8; |
| 1032 | } |
| 1033 | |
| 1034 | gsym->set_plt_offset(plt_offset); |
| 1035 | |
| 1036 | // Every PLT entry needs a reloc. |
| 1037 | this->add_relocation(symtab, layout, gsym, got_offset); |
| 1038 | |
| 1039 | // Note that we don't need to save the symbol. The contents of the |
| 1040 | // PLT are independent of which symbols are used. The symbols only |
| 1041 | // appear in the relocations. |
| 1042 | } |
| 1043 | |
| 1044 | // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return |
| 1045 | // the PLT offset. |
| 1046 | |
| 1047 | unsigned int |
| 1048 | Output_data_plt_x86_64::add_local_ifunc_entry( |
| 1049 | Symbol_table* symtab, |
| 1050 | Layout* layout, |
| 1051 | Sized_relobj_file<64, false>* relobj, |
| 1052 | unsigned int local_sym_index) |
| 1053 | { |
| 1054 | unsigned int plt_offset = this->irelative_count_ * plt_entry_size; |
| 1055 | ++this->irelative_count_; |
| 1056 | |
| 1057 | section_offset_type got_offset = this->got_irelative_->current_data_size(); |
| 1058 | |
| 1059 | // Every PLT entry needs a GOT entry which points back to the PLT |
| 1060 | // entry. |
| 1061 | this->got_irelative_->set_current_data_size(got_offset + 8); |
| 1062 | |
| 1063 | // Every PLT entry needs a reloc. |
| 1064 | Reloc_section* rela = this->rela_irelative(symtab, layout); |
| 1065 | rela->add_symbolless_local_addend(relobj, local_sym_index, |
| 1066 | elfcpp::R_X86_64_IRELATIVE, |
| 1067 | this->got_irelative_, got_offset, 0); |
| 1068 | |
| 1069 | return plt_offset; |
| 1070 | } |
| 1071 | |
| 1072 | // Add the relocation for a PLT entry. |
| 1073 | |
| 1074 | void |
| 1075 | Output_data_plt_x86_64::add_relocation(Symbol_table* symtab, Layout* layout, |
| 1076 | Symbol* gsym, unsigned int got_offset) |
| 1077 | { |
| 1078 | if (gsym->type() == elfcpp::STT_GNU_IFUNC |
| 1079 | && gsym->can_use_relative_reloc(false)) |
| 1080 | { |
| 1081 | Reloc_section* rela = this->rela_irelative(symtab, layout); |
| 1082 | rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE, |
| 1083 | this->got_irelative_, got_offset, 0); |
| 1084 | } |
| 1085 | else |
| 1086 | { |
| 1087 | gsym->set_needs_dynsym_entry(); |
| 1088 | this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_, |
| 1089 | got_offset, 0); |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | // Return where the TLSDESC relocations should go, creating it if |
| 1094 | // necessary. These follow the JUMP_SLOT relocations. |
| 1095 | |
| 1096 | Output_data_plt_x86_64::Reloc_section* |
| 1097 | Output_data_plt_x86_64::rela_tlsdesc(Layout* layout) |
| 1098 | { |
| 1099 | if (this->tlsdesc_rel_ == NULL) |
| 1100 | { |
| 1101 | this->tlsdesc_rel_ = new Reloc_section(false); |
| 1102 | layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, |
| 1103 | elfcpp::SHF_ALLOC, this->tlsdesc_rel_, |
| 1104 | ORDER_DYNAMIC_PLT_RELOCS, false); |
| 1105 | gold_assert(this->tlsdesc_rel_->output_section() |
| 1106 | == this->rel_->output_section()); |
| 1107 | } |
| 1108 | return this->tlsdesc_rel_; |
| 1109 | } |
| 1110 | |
| 1111 | // Return where the IRELATIVE relocations should go in the PLT. These |
| 1112 | // follow the JUMP_SLOT and the TLSDESC relocations. |
| 1113 | |
| 1114 | Output_data_plt_x86_64::Reloc_section* |
| 1115 | Output_data_plt_x86_64::rela_irelative(Symbol_table* symtab, Layout* layout) |
| 1116 | { |
| 1117 | if (this->irelative_rel_ == NULL) |
| 1118 | { |
| 1119 | // Make sure we have a place for the TLSDESC relocations, in |
| 1120 | // case we see any later on. |
| 1121 | this->rela_tlsdesc(layout); |
| 1122 | this->irelative_rel_ = new Reloc_section(false); |
| 1123 | layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, |
| 1124 | elfcpp::SHF_ALLOC, this->irelative_rel_, |
| 1125 | ORDER_DYNAMIC_PLT_RELOCS, false); |
| 1126 | gold_assert(this->irelative_rel_->output_section() |
| 1127 | == this->rel_->output_section()); |
| 1128 | |
| 1129 | if (parameters->doing_static_link()) |
| 1130 | { |
| 1131 | // A statically linked executable will only have a .rela.plt |
| 1132 | // section to hold R_X86_64_IRELATIVE relocs for |
| 1133 | // STT_GNU_IFUNC symbols. The library will use these |
| 1134 | // symbols to locate the IRELATIVE relocs at program startup |
| 1135 | // time. |
| 1136 | symtab->define_in_output_data("__rela_iplt_start", NULL, |
| 1137 | Symbol_table::PREDEFINED, |
| 1138 | this->irelative_rel_, 0, 0, |
| 1139 | elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, |
| 1140 | elfcpp::STV_HIDDEN, 0, false, true); |
| 1141 | symtab->define_in_output_data("__rela_iplt_end", NULL, |
| 1142 | Symbol_table::PREDEFINED, |
| 1143 | this->irelative_rel_, 0, 0, |
| 1144 | elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, |
| 1145 | elfcpp::STV_HIDDEN, 0, true, true); |
| 1146 | } |
| 1147 | } |
| 1148 | return this->irelative_rel_; |
| 1149 | } |
| 1150 | |
| 1151 | // Return the PLT address to use for a global symbol. |
| 1152 | |
| 1153 | uint64_t |
| 1154 | Output_data_plt_x86_64::address_for_global(const Symbol* gsym) |
| 1155 | { |
| 1156 | uint64_t offset = 0; |
| 1157 | if (gsym->type() == elfcpp::STT_GNU_IFUNC |
| 1158 | && gsym->can_use_relative_reloc(false)) |
| 1159 | offset = (this->count_ + 1) * plt_entry_size; |
| 1160 | return this->address() + offset; |
| 1161 | } |
| 1162 | |
| 1163 | // Return the PLT address to use for a local symbol. These are always |
| 1164 | // IRELATIVE relocs. |
| 1165 | |
| 1166 | uint64_t |
| 1167 | Output_data_plt_x86_64::address_for_local(const Relobj*, unsigned int) |
| 1168 | { |
| 1169 | return this->address() + (this->count_ + 1) * plt_entry_size; |
| 1170 | } |
| 1171 | |
| 1172 | // Set the final size. |
| 1173 | void |
| 1174 | Output_data_plt_x86_64::set_final_data_size() |
| 1175 | { |
| 1176 | unsigned int count = this->count_ + this->irelative_count_; |
| 1177 | if (this->has_tlsdesc_entry()) |
| 1178 | ++count; |
| 1179 | this->set_data_size((count + 1) * plt_entry_size); |
| 1180 | } |
| 1181 | |
| 1182 | // The first entry in the PLT for an executable. |
| 1183 | |
| 1184 | const unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] = |
| 1185 | { |
| 1186 | // From AMD64 ABI Draft 0.98, page 76 |
| 1187 | 0xff, 0x35, // pushq contents of memory address |
| 1188 | 0, 0, 0, 0, // replaced with address of .got + 8 |
| 1189 | 0xff, 0x25, // jmp indirect |
| 1190 | 0, 0, 0, 0, // replaced with address of .got + 16 |
| 1191 | 0x90, 0x90, 0x90, 0x90 // noop (x4) |
| 1192 | }; |
| 1193 | |
| 1194 | // Subsequent entries in the PLT for an executable. |
| 1195 | |
| 1196 | const unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] = |
| 1197 | { |
| 1198 | // From AMD64 ABI Draft 0.98, page 76 |
| 1199 | 0xff, 0x25, // jmpq indirect |
| 1200 | 0, 0, 0, 0, // replaced with address of symbol in .got |
| 1201 | 0x68, // pushq immediate |
| 1202 | 0, 0, 0, 0, // replaced with offset into relocation table |
| 1203 | 0xe9, // jmpq relative |
| 1204 | 0, 0, 0, 0 // replaced with offset to start of .plt |
| 1205 | }; |
| 1206 | |
| 1207 | // The reserved TLSDESC entry in the PLT for an executable. |
| 1208 | |
| 1209 | const unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] = |
| 1210 | { |
| 1211 | // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32 |
| 1212 | // and AMD64/EM64T", Version 0.9.4 (2005-10-10). |
| 1213 | 0xff, 0x35, // pushq x(%rip) |
| 1214 | 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8) |
| 1215 | 0xff, 0x25, // jmpq *y(%rip) |
| 1216 | 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry |
| 1217 | 0x0f, 0x1f, // nop |
| 1218 | 0x40, 0 |
| 1219 | }; |
| 1220 | |
| 1221 | // The .eh_frame unwind information for the PLT. |
| 1222 | |
| 1223 | const unsigned char |
| 1224 | Output_data_plt_x86_64::plt_eh_frame_cie[plt_eh_frame_cie_size] = |
| 1225 | { |
| 1226 | 1, // CIE version. |
| 1227 | 'z', // Augmentation: augmentation size included. |
| 1228 | 'R', // Augmentation: FDE encoding included. |
| 1229 | '\0', // End of augmentation string. |
| 1230 | 1, // Code alignment factor. |
| 1231 | 0x78, // Data alignment factor. |
| 1232 | 16, // Return address column. |
| 1233 | 1, // Augmentation size. |
| 1234 | (elfcpp::DW_EH_PE_pcrel // FDE encoding. |
| 1235 | | elfcpp::DW_EH_PE_sdata4), |
| 1236 | elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8. |
| 1237 | elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8. |
| 1238 | elfcpp::DW_CFA_nop, // Align to 16 bytes. |
| 1239 | elfcpp::DW_CFA_nop |
| 1240 | }; |
| 1241 | |
| 1242 | const unsigned char |
| 1243 | Output_data_plt_x86_64::plt_eh_frame_fde[plt_eh_frame_fde_size] = |
| 1244 | { |
| 1245 | 0, 0, 0, 0, // Replaced with offset to .plt. |
| 1246 | 0, 0, 0, 0, // Replaced with size of .plt. |
| 1247 | 0, // Augmentation size. |
| 1248 | elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16. |
| 1249 | elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6. |
| 1250 | elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24. |
| 1251 | elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16. |
| 1252 | elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression. |
| 1253 | 11, // Block length. |
| 1254 | elfcpp::DW_OP_breg7, 8, // Push %rsp + 8. |
| 1255 | elfcpp::DW_OP_breg16, 0, // Push %rip. |
| 1256 | elfcpp::DW_OP_lit15, // Push 0xf. |
| 1257 | elfcpp::DW_OP_and, // & (%rip & 0xf). |
| 1258 | elfcpp::DW_OP_lit11, // Push 0xb. |
| 1259 | elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb) |
| 1260 | elfcpp::DW_OP_lit3, // Push 3. |
| 1261 | elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3) |
| 1262 | elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8 |
| 1263 | elfcpp::DW_CFA_nop, // Align to 32 bytes. |
| 1264 | elfcpp::DW_CFA_nop, |
| 1265 | elfcpp::DW_CFA_nop, |
| 1266 | elfcpp::DW_CFA_nop |
| 1267 | }; |
| 1268 | |
| 1269 | // Write out the PLT. This uses the hand-coded instructions above, |
| 1270 | // and adjusts them as needed. This is specified by the AMD64 ABI. |
| 1271 | |
| 1272 | void |
| 1273 | Output_data_plt_x86_64::do_write(Output_file* of) |
| 1274 | { |
| 1275 | const off_t offset = this->offset(); |
| 1276 | const section_size_type oview_size = |
| 1277 | convert_to_section_size_type(this->data_size()); |
| 1278 | unsigned char* const oview = of->get_output_view(offset, oview_size); |
| 1279 | |
| 1280 | const off_t got_file_offset = this->got_plt_->offset(); |
| 1281 | gold_assert(parameters->incremental_update() |
| 1282 | || (got_file_offset + this->got_plt_->data_size() |
| 1283 | == this->got_irelative_->offset())); |
| 1284 | const section_size_type got_size = |
| 1285 | convert_to_section_size_type(this->got_plt_->data_size() |
| 1286 | + this->got_irelative_->data_size()); |
| 1287 | unsigned char* const got_view = of->get_output_view(got_file_offset, |
| 1288 | got_size); |
| 1289 | |
| 1290 | unsigned char* pov = oview; |
| 1291 | |
| 1292 | // The base address of the .plt section. |
| 1293 | elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address(); |
| 1294 | // The base address of the .got section. |
| 1295 | elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address(); |
| 1296 | // The base address of the PLT portion of the .got section, |
| 1297 | // which is where the GOT pointer will point, and where the |
| 1298 | // three reserved GOT entries are located. |
| 1299 | elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address(); |
| 1300 | |
| 1301 | memcpy(pov, first_plt_entry, plt_entry_size); |
| 1302 | // We do a jmp relative to the PC at the end of this instruction. |
| 1303 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, |
| 1304 | (got_address + 8 |
| 1305 | - (plt_address + 6))); |
| 1306 | elfcpp::Swap<32, false>::writeval(pov + 8, |
| 1307 | (got_address + 16 |
| 1308 | - (plt_address + 12))); |
| 1309 | pov += plt_entry_size; |
| 1310 | |
| 1311 | unsigned char* got_pov = got_view; |
| 1312 | |
| 1313 | // The first entry in the GOT is the address of the .dynamic section |
| 1314 | // aka the PT_DYNAMIC segment. The next two entries are reserved. |
| 1315 | // We saved space for them when we created the section in |
| 1316 | // Target_x86_64::got_section. |
| 1317 | Output_section* dynamic = this->layout_->dynamic_section(); |
| 1318 | uint32_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address(); |
| 1319 | elfcpp::Swap<64, false>::writeval(got_pov, dynamic_addr); |
| 1320 | got_pov += 8; |
| 1321 | memset(got_pov, 0, 16); |
| 1322 | got_pov += 16; |
| 1323 | |
| 1324 | unsigned int plt_offset = plt_entry_size; |
| 1325 | unsigned int got_offset = 24; |
| 1326 | const unsigned int count = this->count_ + this->irelative_count_; |
| 1327 | for (unsigned int plt_index = 0; |
| 1328 | plt_index < count; |
| 1329 | ++plt_index, |
| 1330 | pov += plt_entry_size, |
| 1331 | got_pov += 8, |
| 1332 | plt_offset += plt_entry_size, |
| 1333 | got_offset += 8) |
| 1334 | { |
| 1335 | // Set and adjust the PLT entry itself. |
| 1336 | memcpy(pov, plt_entry, plt_entry_size); |
| 1337 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, |
| 1338 | (got_address + got_offset |
| 1339 | - (plt_address + plt_offset |
| 1340 | + 6))); |
| 1341 | |
| 1342 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index); |
| 1343 | elfcpp::Swap<32, false>::writeval(pov + 12, |
| 1344 | - (plt_offset + plt_entry_size)); |
| 1345 | |
| 1346 | // Set the entry in the GOT. |
| 1347 | elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6); |
| 1348 | } |
| 1349 | |
| 1350 | if (this->has_tlsdesc_entry()) |
| 1351 | { |
| 1352 | // Set and adjust the reserved TLSDESC PLT entry. |
| 1353 | unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset(); |
| 1354 | memcpy(pov, tlsdesc_plt_entry, plt_entry_size); |
| 1355 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, |
| 1356 | (got_address + 8 |
| 1357 | - (plt_address + plt_offset |
| 1358 | + 6))); |
| 1359 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 8, |
| 1360 | (got_base |
| 1361 | + tlsdesc_got_offset |
| 1362 | - (plt_address + plt_offset |
| 1363 | + 12))); |
| 1364 | pov += plt_entry_size; |
| 1365 | } |
| 1366 | |
| 1367 | gold_assert(static_cast<section_size_type>(pov - oview) == oview_size); |
| 1368 | gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size); |
| 1369 | |
| 1370 | of->write_output_view(offset, oview_size, oview); |
| 1371 | of->write_output_view(got_file_offset, got_size, got_view); |
| 1372 | } |
| 1373 | |
| 1374 | // Create the PLT section. |
| 1375 | |
| 1376 | void |
| 1377 | Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout) |
| 1378 | { |
| 1379 | if (this->plt_ == NULL) |
| 1380 | { |
| 1381 | // Create the GOT sections first. |
| 1382 | this->got_section(symtab, layout); |
| 1383 | |
| 1384 | this->plt_ = new Output_data_plt_x86_64(layout, this->got_, |
| 1385 | this->got_plt_, |
| 1386 | this->got_irelative_); |
| 1387 | layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, |
| 1388 | (elfcpp::SHF_ALLOC |
| 1389 | | elfcpp::SHF_EXECINSTR), |
| 1390 | this->plt_, ORDER_PLT, false); |
| 1391 | |
| 1392 | // Make the sh_info field of .rela.plt point to .plt. |
| 1393 | Output_section* rela_plt_os = this->plt_->rela_plt()->output_section(); |
| 1394 | rela_plt_os->set_info_section(this->plt_->output_section()); |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | // Return the section for TLSDESC relocations. |
| 1399 | |
| 1400 | Target_x86_64::Reloc_section* |
| 1401 | Target_x86_64::rela_tlsdesc_section(Layout* layout) const |
| 1402 | { |
| 1403 | return this->plt_section()->rela_tlsdesc(layout); |
| 1404 | } |
| 1405 | |
| 1406 | // Create a PLT entry for a global symbol. |
| 1407 | |
| 1408 | void |
| 1409 | Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout, |
| 1410 | Symbol* gsym) |
| 1411 | { |
| 1412 | if (gsym->has_plt_offset()) |
| 1413 | return; |
| 1414 | |
| 1415 | if (this->plt_ == NULL) |
| 1416 | this->make_plt_section(symtab, layout); |
| 1417 | |
| 1418 | this->plt_->add_entry(symtab, layout, gsym); |
| 1419 | } |
| 1420 | |
| 1421 | // Make a PLT entry for a local STT_GNU_IFUNC symbol. |
| 1422 | |
| 1423 | void |
| 1424 | Target_x86_64::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout, |
| 1425 | Sized_relobj_file<64, false>* relobj, |
| 1426 | unsigned int local_sym_index) |
| 1427 | { |
| 1428 | if (relobj->local_has_plt_offset(local_sym_index)) |
| 1429 | return; |
| 1430 | if (this->plt_ == NULL) |
| 1431 | this->make_plt_section(symtab, layout); |
| 1432 | unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout, |
| 1433 | relobj, |
| 1434 | local_sym_index); |
| 1435 | relobj->set_local_plt_offset(local_sym_index, plt_offset); |
| 1436 | } |
| 1437 | |
| 1438 | // Return the number of entries in the PLT. |
| 1439 | |
| 1440 | unsigned int |
| 1441 | Target_x86_64::plt_entry_count() const |
| 1442 | { |
| 1443 | if (this->plt_ == NULL) |
| 1444 | return 0; |
| 1445 | return this->plt_->entry_count(); |
| 1446 | } |
| 1447 | |
| 1448 | // Return the offset of the first non-reserved PLT entry. |
| 1449 | |
| 1450 | unsigned int |
| 1451 | Target_x86_64::first_plt_entry_offset() const |
| 1452 | { |
| 1453 | return Output_data_plt_x86_64::first_plt_entry_offset(); |
| 1454 | } |
| 1455 | |
| 1456 | // Return the size of each PLT entry. |
| 1457 | |
| 1458 | unsigned int |
| 1459 | Target_x86_64::plt_entry_size() const |
| 1460 | { |
| 1461 | return Output_data_plt_x86_64::get_plt_entry_size(); |
| 1462 | } |
| 1463 | |
| 1464 | // Create the GOT and PLT sections for an incremental update. |
| 1465 | |
| 1466 | Output_data_got<64, false>* |
| 1467 | Target_x86_64::init_got_plt_for_update(Symbol_table* symtab, |
| 1468 | Layout* layout, |
| 1469 | unsigned int got_count, |
| 1470 | unsigned int plt_count) |
| 1471 | { |
| 1472 | gold_assert(this->got_ == NULL); |
| 1473 | |
| 1474 | this->got_ = new Output_data_got<64, false>(got_count * 8); |
| 1475 | layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, |
| 1476 | (elfcpp::SHF_ALLOC |
| 1477 | | elfcpp::SHF_WRITE), |
| 1478 | this->got_, ORDER_RELRO_LAST, |
| 1479 | true); |
| 1480 | |
| 1481 | // Add the three reserved entries. |
| 1482 | this->got_plt_ = new Output_data_space((plt_count + 3) * 8, 8, "** GOT PLT"); |
| 1483 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, |
| 1484 | (elfcpp::SHF_ALLOC |
| 1485 | | elfcpp::SHF_WRITE), |
| 1486 | this->got_plt_, ORDER_NON_RELRO_FIRST, |
| 1487 | false); |
| 1488 | |
| 1489 | // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT. |
| 1490 | this->global_offset_table_ = |
| 1491 | symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, |
| 1492 | Symbol_table::PREDEFINED, |
| 1493 | this->got_plt_, |
| 1494 | 0, 0, elfcpp::STT_OBJECT, |
| 1495 | elfcpp::STB_LOCAL, |
| 1496 | elfcpp::STV_HIDDEN, 0, |
| 1497 | false, false); |
| 1498 | |
| 1499 | // If there are any TLSDESC relocations, they get GOT entries in |
| 1500 | // .got.plt after the jump slot entries. |
| 1501 | // FIXME: Get the count for TLSDESC entries. |
| 1502 | this->got_tlsdesc_ = new Output_data_got<64, false>(0); |
| 1503 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, |
| 1504 | elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, |
| 1505 | this->got_tlsdesc_, |
| 1506 | ORDER_NON_RELRO_FIRST, false); |
| 1507 | |
| 1508 | // If there are any IRELATIVE relocations, they get GOT entries in |
| 1509 | // .got.plt after the jump slot and TLSDESC entries. |
| 1510 | this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT"); |
| 1511 | layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, |
| 1512 | elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, |
| 1513 | this->got_irelative_, |
| 1514 | ORDER_NON_RELRO_FIRST, false); |
| 1515 | |
| 1516 | // Create the PLT section. |
| 1517 | this->plt_ = new Output_data_plt_x86_64(layout, this->got_, this->got_plt_, |
| 1518 | this->got_irelative_, plt_count); |
| 1519 | layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, |
| 1520 | elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR, |
| 1521 | this->plt_, ORDER_PLT, false); |
| 1522 | |
| 1523 | // Make the sh_info field of .rela.plt point to .plt. |
| 1524 | Output_section* rela_plt_os = this->plt_->rela_plt()->output_section(); |
| 1525 | rela_plt_os->set_info_section(this->plt_->output_section()); |
| 1526 | |
| 1527 | // Create the rela_dyn section. |
| 1528 | this->rela_dyn_section(layout); |
| 1529 | |
| 1530 | return this->got_; |
| 1531 | } |
| 1532 | |
| 1533 | // Reserve a GOT entry for a local symbol, and regenerate any |
| 1534 | // necessary dynamic relocations. |
| 1535 | |
| 1536 | void |
| 1537 | Target_x86_64::reserve_local_got_entry( |
| 1538 | unsigned int got_index, |
| 1539 | Sized_relobj<64, false>* obj, |
| 1540 | unsigned int r_sym, |
| 1541 | unsigned int got_type) |
| 1542 | { |
| 1543 | unsigned int got_offset = got_index * 8; |
| 1544 | Reloc_section* rela_dyn = this->rela_dyn_section(NULL); |
| 1545 | |
| 1546 | this->got_->reserve_local(got_index, obj, r_sym, got_type); |
| 1547 | switch (got_type) |
| 1548 | { |
| 1549 | case GOT_TYPE_STANDARD: |
| 1550 | if (parameters->options().output_is_position_independent()) |
| 1551 | rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE, |
| 1552 | this->got_, got_offset, 0, false); |
| 1553 | break; |
| 1554 | case GOT_TYPE_TLS_OFFSET: |
| 1555 | rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64, |
| 1556 | this->got_, got_offset, 0); |
| 1557 | break; |
| 1558 | case GOT_TYPE_TLS_PAIR: |
| 1559 | this->got_->reserve_slot(got_index + 1); |
| 1560 | rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64, |
| 1561 | this->got_, got_offset, 0); |
| 1562 | break; |
| 1563 | case GOT_TYPE_TLS_DESC: |
| 1564 | gold_fatal(_("TLS_DESC not yet supported for incremental linking")); |
| 1565 | // this->got_->reserve_slot(got_index + 1); |
| 1566 | // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg, |
| 1567 | // this->got_, got_offset, 0); |
| 1568 | break; |
| 1569 | default: |
| 1570 | gold_unreachable(); |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | // Reserve a GOT entry for a global symbol, and regenerate any |
| 1575 | // necessary dynamic relocations. |
| 1576 | |
| 1577 | void |
| 1578 | Target_x86_64::reserve_global_got_entry(unsigned int got_index, Symbol* gsym, |
| 1579 | unsigned int got_type) |
| 1580 | { |
| 1581 | unsigned int got_offset = got_index * 8; |
| 1582 | Reloc_section* rela_dyn = this->rela_dyn_section(NULL); |
| 1583 | |
| 1584 | this->got_->reserve_global(got_index, gsym, got_type); |
| 1585 | switch (got_type) |
| 1586 | { |
| 1587 | case GOT_TYPE_STANDARD: |
| 1588 | if (!gsym->final_value_is_known()) |
| 1589 | { |
| 1590 | if (gsym->is_from_dynobj() |
| 1591 | || gsym->is_undefined() |
| 1592 | || gsym->is_preemptible() |
| 1593 | || gsym->type() == elfcpp::STT_GNU_IFUNC) |
| 1594 | rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT, |
| 1595 | this->got_, got_offset, 0); |
| 1596 | else |
| 1597 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE, |
| 1598 | this->got_, got_offset, 0); |
| 1599 | } |
| 1600 | break; |
| 1601 | case GOT_TYPE_TLS_OFFSET: |
| 1602 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64, |
| 1603 | this->got_, got_offset, 0); |
| 1604 | break; |
| 1605 | case GOT_TYPE_TLS_PAIR: |
| 1606 | this->got_->reserve_slot(got_index + 1); |
| 1607 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64, |
| 1608 | this->got_, got_offset, 0); |
| 1609 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64, |
| 1610 | this->got_, got_offset + 8, 0); |
| 1611 | break; |
| 1612 | case GOT_TYPE_TLS_DESC: |
| 1613 | this->got_->reserve_slot(got_index + 1); |
| 1614 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC, |
| 1615 | this->got_, got_offset, 0); |
| 1616 | break; |
| 1617 | default: |
| 1618 | gold_unreachable(); |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | // Register an existing PLT entry for a global symbol. |
| 1623 | |
| 1624 | void |
| 1625 | Target_x86_64::register_global_plt_entry(Symbol_table* symtab, |
| 1626 | Layout* layout, |
| 1627 | unsigned int plt_index, |
| 1628 | Symbol* gsym) |
| 1629 | { |
| 1630 | gold_assert(this->plt_ != NULL); |
| 1631 | gold_assert(!gsym->has_plt_offset()); |
| 1632 | |
| 1633 | this->plt_->reserve_slot(plt_index); |
| 1634 | |
| 1635 | gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size()); |
| 1636 | |
| 1637 | unsigned int got_offset = (plt_index + 3) * 8; |
| 1638 | this->plt_->add_relocation(symtab, layout, gsym, got_offset); |
| 1639 | } |
| 1640 | |
| 1641 | // Force a COPY relocation for a given symbol. |
| 1642 | |
| 1643 | void |
| 1644 | Target_x86_64::emit_copy_reloc( |
| 1645 | Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset) |
| 1646 | { |
| 1647 | this->copy_relocs_.emit_copy_reloc(symtab, |
| 1648 | symtab->get_sized_symbol<64>(sym), |
| 1649 | os, |
| 1650 | offset, |
| 1651 | this->rela_dyn_section(NULL)); |
| 1652 | } |
| 1653 | |
| 1654 | // Define the _TLS_MODULE_BASE_ symbol in the TLS segment. |
| 1655 | |
| 1656 | void |
| 1657 | Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout) |
| 1658 | { |
| 1659 | if (this->tls_base_symbol_defined_) |
| 1660 | return; |
| 1661 | |
| 1662 | Output_segment* tls_segment = layout->tls_segment(); |
| 1663 | if (tls_segment != NULL) |
| 1664 | { |
| 1665 | bool is_exec = parameters->options().output_is_executable(); |
| 1666 | symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL, |
| 1667 | Symbol_table::PREDEFINED, |
| 1668 | tls_segment, 0, 0, |
| 1669 | elfcpp::STT_TLS, |
| 1670 | elfcpp::STB_LOCAL, |
| 1671 | elfcpp::STV_HIDDEN, 0, |
| 1672 | (is_exec |
| 1673 | ? Symbol::SEGMENT_END |
| 1674 | : Symbol::SEGMENT_START), |
| 1675 | true); |
| 1676 | } |
| 1677 | this->tls_base_symbol_defined_ = true; |
| 1678 | } |
| 1679 | |
| 1680 | // Create the reserved PLT and GOT entries for the TLS descriptor resolver. |
| 1681 | |
| 1682 | void |
| 1683 | Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab, |
| 1684 | Layout* layout) |
| 1685 | { |
| 1686 | if (this->plt_ == NULL) |
| 1687 | this->make_plt_section(symtab, layout); |
| 1688 | |
| 1689 | if (!this->plt_->has_tlsdesc_entry()) |
| 1690 | { |
| 1691 | // Allocate the TLSDESC_GOT entry. |
| 1692 | Output_data_got<64, false>* got = this->got_section(symtab, layout); |
| 1693 | unsigned int got_offset = got->add_constant(0); |
| 1694 | |
| 1695 | // Allocate the TLSDESC_PLT entry. |
| 1696 | this->plt_->reserve_tlsdesc_entry(got_offset); |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | // Create a GOT entry for the TLS module index. |
| 1701 | |
| 1702 | unsigned int |
| 1703 | Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout, |
| 1704 | Sized_relobj_file<64, false>* object) |
| 1705 | { |
| 1706 | if (this->got_mod_index_offset_ == -1U) |
| 1707 | { |
| 1708 | gold_assert(symtab != NULL && layout != NULL && object != NULL); |
| 1709 | Reloc_section* rela_dyn = this->rela_dyn_section(layout); |
| 1710 | Output_data_got<64, false>* got = this->got_section(symtab, layout); |
| 1711 | unsigned int got_offset = got->add_constant(0); |
| 1712 | rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got, |
| 1713 | got_offset, 0); |
| 1714 | got->add_constant(0); |
| 1715 | this->got_mod_index_offset_ = got_offset; |
| 1716 | } |
| 1717 | return this->got_mod_index_offset_; |
| 1718 | } |
| 1719 | |
| 1720 | // Optimize the TLS relocation type based on what we know about the |
| 1721 | // symbol. IS_FINAL is true if the final address of this symbol is |
| 1722 | // known at link time. |
| 1723 | |
| 1724 | tls::Tls_optimization |
| 1725 | Target_x86_64::optimize_tls_reloc(bool is_final, int r_type) |
| 1726 | { |
| 1727 | // If we are generating a shared library, then we can't do anything |
| 1728 | // in the linker. |
| 1729 | if (parameters->options().shared()) |
| 1730 | return tls::TLSOPT_NONE; |
| 1731 | |
| 1732 | switch (r_type) |
| 1733 | { |
| 1734 | case elfcpp::R_X86_64_TLSGD: |
| 1735 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: |
| 1736 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 1737 | // These are General-Dynamic which permits fully general TLS |
| 1738 | // access. Since we know that we are generating an executable, |
| 1739 | // we can convert this to Initial-Exec. If we also know that |
| 1740 | // this is a local symbol, we can further switch to Local-Exec. |
| 1741 | if (is_final) |
| 1742 | return tls::TLSOPT_TO_LE; |
| 1743 | return tls::TLSOPT_TO_IE; |
| 1744 | |
| 1745 | case elfcpp::R_X86_64_TLSLD: |
| 1746 | // This is Local-Dynamic, which refers to a local symbol in the |
| 1747 | // dynamic TLS block. Since we know that we generating an |
| 1748 | // executable, we can switch to Local-Exec. |
| 1749 | return tls::TLSOPT_TO_LE; |
| 1750 | |
| 1751 | case elfcpp::R_X86_64_DTPOFF32: |
| 1752 | case elfcpp::R_X86_64_DTPOFF64: |
| 1753 | // Another Local-Dynamic reloc. |
| 1754 | return tls::TLSOPT_TO_LE; |
| 1755 | |
| 1756 | case elfcpp::R_X86_64_GOTTPOFF: |
| 1757 | // These are Initial-Exec relocs which get the thread offset |
| 1758 | // from the GOT. If we know that we are linking against the |
| 1759 | // local symbol, we can switch to Local-Exec, which links the |
| 1760 | // thread offset into the instruction. |
| 1761 | if (is_final) |
| 1762 | return tls::TLSOPT_TO_LE; |
| 1763 | return tls::TLSOPT_NONE; |
| 1764 | |
| 1765 | case elfcpp::R_X86_64_TPOFF32: |
| 1766 | // When we already have Local-Exec, there is nothing further we |
| 1767 | // can do. |
| 1768 | return tls::TLSOPT_NONE; |
| 1769 | |
| 1770 | default: |
| 1771 | gold_unreachable(); |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | // Get the Reference_flags for a particular relocation. |
| 1776 | |
| 1777 | int |
| 1778 | Target_x86_64::Scan::get_reference_flags(unsigned int r_type) |
| 1779 | { |
| 1780 | switch (r_type) |
| 1781 | { |
| 1782 | case elfcpp::R_X86_64_NONE: |
| 1783 | case elfcpp::R_X86_64_GNU_VTINHERIT: |
| 1784 | case elfcpp::R_X86_64_GNU_VTENTRY: |
| 1785 | case elfcpp::R_X86_64_GOTPC32: |
| 1786 | case elfcpp::R_X86_64_GOTPC64: |
| 1787 | // No symbol reference. |
| 1788 | return 0; |
| 1789 | |
| 1790 | case elfcpp::R_X86_64_64: |
| 1791 | case elfcpp::R_X86_64_32: |
| 1792 | case elfcpp::R_X86_64_32S: |
| 1793 | case elfcpp::R_X86_64_16: |
| 1794 | case elfcpp::R_X86_64_8: |
| 1795 | return Symbol::ABSOLUTE_REF; |
| 1796 | |
| 1797 | case elfcpp::R_X86_64_PC64: |
| 1798 | case elfcpp::R_X86_64_PC32: |
| 1799 | case elfcpp::R_X86_64_PC16: |
| 1800 | case elfcpp::R_X86_64_PC8: |
| 1801 | case elfcpp::R_X86_64_GOTOFF64: |
| 1802 | return Symbol::RELATIVE_REF; |
| 1803 | |
| 1804 | case elfcpp::R_X86_64_PLT32: |
| 1805 | case elfcpp::R_X86_64_PLTOFF64: |
| 1806 | return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF; |
| 1807 | |
| 1808 | case elfcpp::R_X86_64_GOT64: |
| 1809 | case elfcpp::R_X86_64_GOT32: |
| 1810 | case elfcpp::R_X86_64_GOTPCREL64: |
| 1811 | case elfcpp::R_X86_64_GOTPCREL: |
| 1812 | case elfcpp::R_X86_64_GOTPLT64: |
| 1813 | // Absolute in GOT. |
| 1814 | return Symbol::ABSOLUTE_REF; |
| 1815 | |
| 1816 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
| 1817 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) |
| 1818 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 1819 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 1820 | case elfcpp::R_X86_64_DTPOFF32: |
| 1821 | case elfcpp::R_X86_64_DTPOFF64: |
| 1822 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 1823 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 1824 | return Symbol::TLS_REF; |
| 1825 | |
| 1826 | case elfcpp::R_X86_64_COPY: |
| 1827 | case elfcpp::R_X86_64_GLOB_DAT: |
| 1828 | case elfcpp::R_X86_64_JUMP_SLOT: |
| 1829 | case elfcpp::R_X86_64_RELATIVE: |
| 1830 | case elfcpp::R_X86_64_IRELATIVE: |
| 1831 | case elfcpp::R_X86_64_TPOFF64: |
| 1832 | case elfcpp::R_X86_64_DTPMOD64: |
| 1833 | case elfcpp::R_X86_64_TLSDESC: |
| 1834 | case elfcpp::R_X86_64_SIZE32: |
| 1835 | case elfcpp::R_X86_64_SIZE64: |
| 1836 | default: |
| 1837 | // Not expected. We will give an error later. |
| 1838 | return 0; |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | // Report an unsupported relocation against a local symbol. |
| 1843 | |
| 1844 | void |
| 1845 | Target_x86_64::Scan::unsupported_reloc_local( |
| 1846 | Sized_relobj_file<64, false>* object, |
| 1847 | unsigned int r_type) |
| 1848 | { |
| 1849 | gold_error(_("%s: unsupported reloc %u against local symbol"), |
| 1850 | object->name().c_str(), r_type); |
| 1851 | } |
| 1852 | |
| 1853 | // We are about to emit a dynamic relocation of type R_TYPE. If the |
| 1854 | // dynamic linker does not support it, issue an error. The GNU linker |
| 1855 | // only issues a non-PIC error for an allocated read-only section. |
| 1856 | // Here we know the section is allocated, but we don't know that it is |
| 1857 | // read-only. But we check for all the relocation types which the |
| 1858 | // glibc dynamic linker supports, so it seems appropriate to issue an |
| 1859 | // error even if the section is not read-only. If GSYM is not NULL, |
| 1860 | // it is the symbol the relocation is against; if it is NULL, the |
| 1861 | // relocation is against a local symbol. |
| 1862 | |
| 1863 | void |
| 1864 | Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type, |
| 1865 | Symbol* gsym) |
| 1866 | { |
| 1867 | switch (r_type) |
| 1868 | { |
| 1869 | // These are the relocation types supported by glibc for x86_64 |
| 1870 | // which should always work. |
| 1871 | case elfcpp::R_X86_64_RELATIVE: |
| 1872 | case elfcpp::R_X86_64_IRELATIVE: |
| 1873 | case elfcpp::R_X86_64_GLOB_DAT: |
| 1874 | case elfcpp::R_X86_64_JUMP_SLOT: |
| 1875 | case elfcpp::R_X86_64_DTPMOD64: |
| 1876 | case elfcpp::R_X86_64_DTPOFF64: |
| 1877 | case elfcpp::R_X86_64_TPOFF64: |
| 1878 | case elfcpp::R_X86_64_64: |
| 1879 | case elfcpp::R_X86_64_COPY: |
| 1880 | return; |
| 1881 | |
| 1882 | // glibc supports these reloc types, but they can overflow. |
| 1883 | case elfcpp::R_X86_64_PC32: |
| 1884 | // A PC relative reference is OK against a local symbol or if |
| 1885 | // the symbol is defined locally. |
| 1886 | if (gsym == NULL |
| 1887 | || (!gsym->is_from_dynobj() |
| 1888 | && !gsym->is_undefined() |
| 1889 | && !gsym->is_preemptible())) |
| 1890 | return; |
| 1891 | /* Fall through. */ |
| 1892 | case elfcpp::R_X86_64_32: |
| 1893 | if (this->issued_non_pic_error_) |
| 1894 | return; |
| 1895 | gold_assert(parameters->options().output_is_position_independent()); |
| 1896 | if (gsym == NULL) |
| 1897 | object->error(_("requires dynamic R_X86_64_32 reloc which may " |
| 1898 | "overflow at runtime; recompile with -fPIC")); |
| 1899 | else |
| 1900 | object->error(_("requires dynamic %s reloc against '%s' which may " |
| 1901 | "overflow at runtime; recompile with -fPIC"), |
| 1902 | (r_type == elfcpp::R_X86_64_32 |
| 1903 | ? "R_X86_64_32" |
| 1904 | : "R_X86_64_PC32"), |
| 1905 | gsym->name()); |
| 1906 | this->issued_non_pic_error_ = true; |
| 1907 | return; |
| 1908 | |
| 1909 | default: |
| 1910 | // This prevents us from issuing more than one error per reloc |
| 1911 | // section. But we can still wind up issuing more than one |
| 1912 | // error per object file. |
| 1913 | if (this->issued_non_pic_error_) |
| 1914 | return; |
| 1915 | gold_assert(parameters->options().output_is_position_independent()); |
| 1916 | object->error(_("requires unsupported dynamic reloc %u; " |
| 1917 | "recompile with -fPIC"), |
| 1918 | r_type); |
| 1919 | this->issued_non_pic_error_ = true; |
| 1920 | return; |
| 1921 | |
| 1922 | case elfcpp::R_X86_64_NONE: |
| 1923 | gold_unreachable(); |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | // Return whether we need to make a PLT entry for a relocation of the |
| 1928 | // given type against a STT_GNU_IFUNC symbol. |
| 1929 | |
| 1930 | bool |
| 1931 | Target_x86_64::Scan::reloc_needs_plt_for_ifunc( |
| 1932 | Sized_relobj_file<64, false>* object, |
| 1933 | unsigned int r_type) |
| 1934 | { |
| 1935 | int flags = Scan::get_reference_flags(r_type); |
| 1936 | if (flags & Symbol::TLS_REF) |
| 1937 | gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"), |
| 1938 | object->name().c_str(), r_type); |
| 1939 | return flags != 0; |
| 1940 | } |
| 1941 | |
| 1942 | // Scan a relocation for a local symbol. |
| 1943 | |
| 1944 | inline void |
| 1945 | Target_x86_64::Scan::local(Symbol_table* symtab, |
| 1946 | Layout* layout, |
| 1947 | Target_x86_64* target, |
| 1948 | Sized_relobj_file<64, false>* object, |
| 1949 | unsigned int data_shndx, |
| 1950 | Output_section* output_section, |
| 1951 | const elfcpp::Rela<64, false>& reloc, |
| 1952 | unsigned int r_type, |
| 1953 | const elfcpp::Sym<64, false>& lsym) |
| 1954 | { |
| 1955 | // A local STT_GNU_IFUNC symbol may require a PLT entry. |
| 1956 | bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC; |
| 1957 | if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type)) |
| 1958 | { |
| 1959 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
| 1960 | target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym); |
| 1961 | } |
| 1962 | |
| 1963 | switch (r_type) |
| 1964 | { |
| 1965 | case elfcpp::R_X86_64_NONE: |
| 1966 | case elfcpp::R_X86_64_GNU_VTINHERIT: |
| 1967 | case elfcpp::R_X86_64_GNU_VTENTRY: |
| 1968 | break; |
| 1969 | |
| 1970 | case elfcpp::R_X86_64_64: |
| 1971 | // If building a shared library (or a position-independent |
| 1972 | // executable), we need to create a dynamic relocation for this |
| 1973 | // location. The relocation applied at link time will apply the |
| 1974 | // link-time value, so we flag the location with an |
| 1975 | // R_X86_64_RELATIVE relocation so the dynamic loader can |
| 1976 | // relocate it easily. |
| 1977 | if (parameters->options().output_is_position_independent()) |
| 1978 | { |
| 1979 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
| 1980 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
| 1981 | rela_dyn->add_local_relative(object, r_sym, |
| 1982 | elfcpp::R_X86_64_RELATIVE, |
| 1983 | output_section, data_shndx, |
| 1984 | reloc.get_r_offset(), |
| 1985 | reloc.get_r_addend(), is_ifunc); |
| 1986 | } |
| 1987 | break; |
| 1988 | |
| 1989 | case elfcpp::R_X86_64_32: |
| 1990 | case elfcpp::R_X86_64_32S: |
| 1991 | case elfcpp::R_X86_64_16: |
| 1992 | case elfcpp::R_X86_64_8: |
| 1993 | // If building a shared library (or a position-independent |
| 1994 | // executable), we need to create a dynamic relocation for this |
| 1995 | // location. We can't use an R_X86_64_RELATIVE relocation |
| 1996 | // because that is always a 64-bit relocation. |
| 1997 | if (parameters->options().output_is_position_independent()) |
| 1998 | { |
| 1999 | this->check_non_pic(object, r_type, NULL); |
| 2000 | |
| 2001 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
| 2002 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
| 2003 | if (lsym.get_st_type() != elfcpp::STT_SECTION) |
| 2004 | rela_dyn->add_local(object, r_sym, r_type, output_section, |
| 2005 | data_shndx, reloc.get_r_offset(), |
| 2006 | reloc.get_r_addend()); |
| 2007 | else |
| 2008 | { |
| 2009 | gold_assert(lsym.get_st_value() == 0); |
| 2010 | unsigned int shndx = lsym.get_st_shndx(); |
| 2011 | bool is_ordinary; |
| 2012 | shndx = object->adjust_sym_shndx(r_sym, shndx, |
| 2013 | &is_ordinary); |
| 2014 | if (!is_ordinary) |
| 2015 | object->error(_("section symbol %u has bad shndx %u"), |
| 2016 | r_sym, shndx); |
| 2017 | else |
| 2018 | rela_dyn->add_local_section(object, shndx, |
| 2019 | r_type, output_section, |
| 2020 | data_shndx, reloc.get_r_offset(), |
| 2021 | reloc.get_r_addend()); |
| 2022 | } |
| 2023 | } |
| 2024 | break; |
| 2025 | |
| 2026 | case elfcpp::R_X86_64_PC64: |
| 2027 | case elfcpp::R_X86_64_PC32: |
| 2028 | case elfcpp::R_X86_64_PC16: |
| 2029 | case elfcpp::R_X86_64_PC8: |
| 2030 | break; |
| 2031 | |
| 2032 | case elfcpp::R_X86_64_PLT32: |
| 2033 | // Since we know this is a local symbol, we can handle this as a |
| 2034 | // PC32 reloc. |
| 2035 | break; |
| 2036 | |
| 2037 | case elfcpp::R_X86_64_GOTPC32: |
| 2038 | case elfcpp::R_X86_64_GOTOFF64: |
| 2039 | case elfcpp::R_X86_64_GOTPC64: |
| 2040 | case elfcpp::R_X86_64_PLTOFF64: |
| 2041 | // We need a GOT section. |
| 2042 | target->got_section(symtab, layout); |
| 2043 | // For PLTOFF64, we'd normally want a PLT section, but since we |
| 2044 | // know this is a local symbol, no PLT is needed. |
| 2045 | break; |
| 2046 | |
| 2047 | case elfcpp::R_X86_64_GOT64: |
| 2048 | case elfcpp::R_X86_64_GOT32: |
| 2049 | case elfcpp::R_X86_64_GOTPCREL64: |
| 2050 | case elfcpp::R_X86_64_GOTPCREL: |
| 2051 | case elfcpp::R_X86_64_GOTPLT64: |
| 2052 | { |
| 2053 | // The symbol requires a GOT entry. |
| 2054 | Output_data_got<64, false>* got = target->got_section(symtab, layout); |
| 2055 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
| 2056 | |
| 2057 | // For a STT_GNU_IFUNC symbol we want the PLT offset. That |
| 2058 | // lets function pointers compare correctly with shared |
| 2059 | // libraries. Otherwise we would need an IRELATIVE reloc. |
| 2060 | bool is_new; |
| 2061 | if (is_ifunc) |
| 2062 | is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD); |
| 2063 | else |
| 2064 | is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD); |
| 2065 | if (is_new) |
| 2066 | { |
| 2067 | // If we are generating a shared object, we need to add a |
| 2068 | // dynamic relocation for this symbol's GOT entry. |
| 2069 | if (parameters->options().output_is_position_independent()) |
| 2070 | { |
| 2071 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
| 2072 | // R_X86_64_RELATIVE assumes a 64-bit relocation. |
| 2073 | if (r_type != elfcpp::R_X86_64_GOT32) |
| 2074 | { |
| 2075 | unsigned int got_offset = |
| 2076 | object->local_got_offset(r_sym, GOT_TYPE_STANDARD); |
| 2077 | rela_dyn->add_local_relative(object, r_sym, |
| 2078 | elfcpp::R_X86_64_RELATIVE, |
| 2079 | got, got_offset, 0, is_ifunc); |
| 2080 | } |
| 2081 | else |
| 2082 | { |
| 2083 | this->check_non_pic(object, r_type, NULL); |
| 2084 | |
| 2085 | gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION); |
| 2086 | rela_dyn->add_local( |
| 2087 | object, r_sym, r_type, got, |
| 2088 | object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0); |
| 2089 | } |
| 2090 | } |
| 2091 | } |
| 2092 | // For GOTPLT64, we'd normally want a PLT section, but since |
| 2093 | // we know this is a local symbol, no PLT is needed. |
| 2094 | } |
| 2095 | break; |
| 2096 | |
| 2097 | case elfcpp::R_X86_64_COPY: |
| 2098 | case elfcpp::R_X86_64_GLOB_DAT: |
| 2099 | case elfcpp::R_X86_64_JUMP_SLOT: |
| 2100 | case elfcpp::R_X86_64_RELATIVE: |
| 2101 | case elfcpp::R_X86_64_IRELATIVE: |
| 2102 | // These are outstanding tls relocs, which are unexpected when linking |
| 2103 | case elfcpp::R_X86_64_TPOFF64: |
| 2104 | case elfcpp::R_X86_64_DTPMOD64: |
| 2105 | case elfcpp::R_X86_64_TLSDESC: |
| 2106 | gold_error(_("%s: unexpected reloc %u in object file"), |
| 2107 | object->name().c_str(), r_type); |
| 2108 | break; |
| 2109 | |
| 2110 | // These are initial tls relocs, which are expected when linking |
| 2111 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
| 2112 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) |
| 2113 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 2114 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 2115 | case elfcpp::R_X86_64_DTPOFF32: |
| 2116 | case elfcpp::R_X86_64_DTPOFF64: |
| 2117 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 2118 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 2119 | { |
| 2120 | bool output_is_shared = parameters->options().shared(); |
| 2121 | const tls::Tls_optimization optimized_type |
| 2122 | = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type); |
| 2123 | switch (r_type) |
| 2124 | { |
| 2125 | case elfcpp::R_X86_64_TLSGD: // General-dynamic |
| 2126 | if (optimized_type == tls::TLSOPT_NONE) |
| 2127 | { |
| 2128 | // Create a pair of GOT entries for the module index and |
| 2129 | // dtv-relative offset. |
| 2130 | Output_data_got<64, false>* got |
| 2131 | = target->got_section(symtab, layout); |
| 2132 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
| 2133 | unsigned int shndx = lsym.get_st_shndx(); |
| 2134 | bool is_ordinary; |
| 2135 | shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); |
| 2136 | if (!is_ordinary) |
| 2137 | object->error(_("local symbol %u has bad shndx %u"), |
| 2138 | r_sym, shndx); |
| 2139 | else |
| 2140 | got->add_local_pair_with_rela(object, r_sym, |
| 2141 | shndx, |
| 2142 | GOT_TYPE_TLS_PAIR, |
| 2143 | target->rela_dyn_section(layout), |
| 2144 | elfcpp::R_X86_64_DTPMOD64, 0); |
| 2145 | } |
| 2146 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 2147 | unsupported_reloc_local(object, r_type); |
| 2148 | break; |
| 2149 | |
| 2150 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: |
| 2151 | target->define_tls_base_symbol(symtab, layout); |
| 2152 | if (optimized_type == tls::TLSOPT_NONE) |
| 2153 | { |
| 2154 | // Create reserved PLT and GOT entries for the resolver. |
| 2155 | target->reserve_tlsdesc_entries(symtab, layout); |
| 2156 | |
| 2157 | // Generate a double GOT entry with an |
| 2158 | // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc |
| 2159 | // is resolved lazily, so the GOT entry needs to be in |
| 2160 | // an area in .got.plt, not .got. Call got_section to |
| 2161 | // make sure the section has been created. |
| 2162 | target->got_section(symtab, layout); |
| 2163 | Output_data_got<64, false>* got = target->got_tlsdesc_section(); |
| 2164 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
| 2165 | if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC)) |
| 2166 | { |
| 2167 | unsigned int got_offset = got->add_constant(0); |
| 2168 | got->add_constant(0); |
| 2169 | object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC, |
| 2170 | got_offset); |
| 2171 | Reloc_section* rt = target->rela_tlsdesc_section(layout); |
| 2172 | // We store the arguments we need in a vector, and |
| 2173 | // use the index into the vector as the parameter |
| 2174 | // to pass to the target specific routines. |
| 2175 | uintptr_t intarg = target->add_tlsdesc_info(object, r_sym); |
| 2176 | void* arg = reinterpret_cast<void*>(intarg); |
| 2177 | rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg, |
| 2178 | got, got_offset, 0); |
| 2179 | } |
| 2180 | } |
| 2181 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 2182 | unsupported_reloc_local(object, r_type); |
| 2183 | break; |
| 2184 | |
| 2185 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 2186 | break; |
| 2187 | |
| 2188 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 2189 | if (optimized_type == tls::TLSOPT_NONE) |
| 2190 | { |
| 2191 | // Create a GOT entry for the module index. |
| 2192 | target->got_mod_index_entry(symtab, layout, object); |
| 2193 | } |
| 2194 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 2195 | unsupported_reloc_local(object, r_type); |
| 2196 | break; |
| 2197 | |
| 2198 | case elfcpp::R_X86_64_DTPOFF32: |
| 2199 | case elfcpp::R_X86_64_DTPOFF64: |
| 2200 | break; |
| 2201 | |
| 2202 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 2203 | layout->set_has_static_tls(); |
| 2204 | if (optimized_type == tls::TLSOPT_NONE) |
| 2205 | { |
| 2206 | // Create a GOT entry for the tp-relative offset. |
| 2207 | Output_data_got<64, false>* got |
| 2208 | = target->got_section(symtab, layout); |
| 2209 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
| 2210 | got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET, |
| 2211 | target->rela_dyn_section(layout), |
| 2212 | elfcpp::R_X86_64_TPOFF64); |
| 2213 | } |
| 2214 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 2215 | unsupported_reloc_local(object, r_type); |
| 2216 | break; |
| 2217 | |
| 2218 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 2219 | layout->set_has_static_tls(); |
| 2220 | if (output_is_shared) |
| 2221 | unsupported_reloc_local(object, r_type); |
| 2222 | break; |
| 2223 | |
| 2224 | default: |
| 2225 | gold_unreachable(); |
| 2226 | } |
| 2227 | } |
| 2228 | break; |
| 2229 | |
| 2230 | case elfcpp::R_X86_64_SIZE32: |
| 2231 | case elfcpp::R_X86_64_SIZE64: |
| 2232 | default: |
| 2233 | gold_error(_("%s: unsupported reloc %u against local symbol"), |
| 2234 | object->name().c_str(), r_type); |
| 2235 | break; |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | |
| 2240 | // Report an unsupported relocation against a global symbol. |
| 2241 | |
| 2242 | void |
| 2243 | Target_x86_64::Scan::unsupported_reloc_global( |
| 2244 | Sized_relobj_file<64, false>* object, |
| 2245 | unsigned int r_type, |
| 2246 | Symbol* gsym) |
| 2247 | { |
| 2248 | gold_error(_("%s: unsupported reloc %u against global symbol %s"), |
| 2249 | object->name().c_str(), r_type, gsym->demangled_name().c_str()); |
| 2250 | } |
| 2251 | |
| 2252 | // Returns true if this relocation type could be that of a function pointer. |
| 2253 | inline bool |
| 2254 | Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type) |
| 2255 | { |
| 2256 | switch (r_type) |
| 2257 | { |
| 2258 | case elfcpp::R_X86_64_64: |
| 2259 | case elfcpp::R_X86_64_32: |
| 2260 | case elfcpp::R_X86_64_32S: |
| 2261 | case elfcpp::R_X86_64_16: |
| 2262 | case elfcpp::R_X86_64_8: |
| 2263 | case elfcpp::R_X86_64_GOT64: |
| 2264 | case elfcpp::R_X86_64_GOT32: |
| 2265 | case elfcpp::R_X86_64_GOTPCREL64: |
| 2266 | case elfcpp::R_X86_64_GOTPCREL: |
| 2267 | case elfcpp::R_X86_64_GOTPLT64: |
| 2268 | { |
| 2269 | return true; |
| 2270 | } |
| 2271 | } |
| 2272 | return false; |
| 2273 | } |
| 2274 | |
| 2275 | // For safe ICF, scan a relocation for a local symbol to check if it |
| 2276 | // corresponds to a function pointer being taken. In that case mark |
| 2277 | // the function whose pointer was taken as not foldable. |
| 2278 | |
| 2279 | inline bool |
| 2280 | Target_x86_64::Scan::local_reloc_may_be_function_pointer( |
| 2281 | Symbol_table* , |
| 2282 | Layout* , |
| 2283 | Target_x86_64* , |
| 2284 | Sized_relobj_file<64, false>* , |
| 2285 | unsigned int , |
| 2286 | Output_section* , |
| 2287 | const elfcpp::Rela<64, false>& , |
| 2288 | unsigned int r_type, |
| 2289 | const elfcpp::Sym<64, false>&) |
| 2290 | { |
| 2291 | // When building a shared library, do not fold any local symbols as it is |
| 2292 | // not possible to distinguish pointer taken versus a call by looking at |
| 2293 | // the relocation types. |
| 2294 | return (parameters->options().shared() |
| 2295 | || possible_function_pointer_reloc(r_type)); |
| 2296 | } |
| 2297 | |
| 2298 | // For safe ICF, scan a relocation for a global symbol to check if it |
| 2299 | // corresponds to a function pointer being taken. In that case mark |
| 2300 | // the function whose pointer was taken as not foldable. |
| 2301 | |
| 2302 | inline bool |
| 2303 | Target_x86_64::Scan::global_reloc_may_be_function_pointer( |
| 2304 | Symbol_table*, |
| 2305 | Layout* , |
| 2306 | Target_x86_64* , |
| 2307 | Sized_relobj_file<64, false>* , |
| 2308 | unsigned int , |
| 2309 | Output_section* , |
| 2310 | const elfcpp::Rela<64, false>& , |
| 2311 | unsigned int r_type, |
| 2312 | Symbol* gsym) |
| 2313 | { |
| 2314 | // When building a shared library, do not fold symbols whose visibility |
| 2315 | // is hidden, internal or protected. |
| 2316 | return ((parameters->options().shared() |
| 2317 | && (gsym->visibility() == elfcpp::STV_INTERNAL |
| 2318 | || gsym->visibility() == elfcpp::STV_PROTECTED |
| 2319 | || gsym->visibility() == elfcpp::STV_HIDDEN)) |
| 2320 | || possible_function_pointer_reloc(r_type)); |
| 2321 | } |
| 2322 | |
| 2323 | // Scan a relocation for a global symbol. |
| 2324 | |
| 2325 | inline void |
| 2326 | Target_x86_64::Scan::global(Symbol_table* symtab, |
| 2327 | Layout* layout, |
| 2328 | Target_x86_64* target, |
| 2329 | Sized_relobj_file<64, false>* object, |
| 2330 | unsigned int data_shndx, |
| 2331 | Output_section* output_section, |
| 2332 | const elfcpp::Rela<64, false>& reloc, |
| 2333 | unsigned int r_type, |
| 2334 | Symbol* gsym) |
| 2335 | { |
| 2336 | // A STT_GNU_IFUNC symbol may require a PLT entry. |
| 2337 | if (gsym->type() == elfcpp::STT_GNU_IFUNC |
| 2338 | && this->reloc_needs_plt_for_ifunc(object, r_type)) |
| 2339 | target->make_plt_entry(symtab, layout, gsym); |
| 2340 | |
| 2341 | switch (r_type) |
| 2342 | { |
| 2343 | case elfcpp::R_X86_64_NONE: |
| 2344 | case elfcpp::R_X86_64_GNU_VTINHERIT: |
| 2345 | case elfcpp::R_X86_64_GNU_VTENTRY: |
| 2346 | break; |
| 2347 | |
| 2348 | case elfcpp::R_X86_64_64: |
| 2349 | case elfcpp::R_X86_64_32: |
| 2350 | case elfcpp::R_X86_64_32S: |
| 2351 | case elfcpp::R_X86_64_16: |
| 2352 | case elfcpp::R_X86_64_8: |
| 2353 | { |
| 2354 | // Make a PLT entry if necessary. |
| 2355 | if (gsym->needs_plt_entry()) |
| 2356 | { |
| 2357 | target->make_plt_entry(symtab, layout, gsym); |
| 2358 | // Since this is not a PC-relative relocation, we may be |
| 2359 | // taking the address of a function. In that case we need to |
| 2360 | // set the entry in the dynamic symbol table to the address of |
| 2361 | // the PLT entry. |
| 2362 | if (gsym->is_from_dynobj() && !parameters->options().shared()) |
| 2363 | gsym->set_needs_dynsym_value(); |
| 2364 | } |
| 2365 | // Make a dynamic relocation if necessary. |
| 2366 | if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))) |
| 2367 | { |
| 2368 | if (gsym->may_need_copy_reloc()) |
| 2369 | { |
| 2370 | target->copy_reloc(symtab, layout, object, |
| 2371 | data_shndx, output_section, gsym, reloc); |
| 2372 | } |
| 2373 | else if (r_type == elfcpp::R_X86_64_64 |
| 2374 | && gsym->type() == elfcpp::STT_GNU_IFUNC |
| 2375 | && gsym->can_use_relative_reloc(false) |
| 2376 | && !gsym->is_from_dynobj() |
| 2377 | && !gsym->is_undefined() |
| 2378 | && !gsym->is_preemptible()) |
| 2379 | { |
| 2380 | // Use an IRELATIVE reloc for a locally defined |
| 2381 | // STT_GNU_IFUNC symbol. This makes a function |
| 2382 | // address in a PIE executable match the address in a |
| 2383 | // shared library that it links against. |
| 2384 | Reloc_section* rela_dyn = |
| 2385 | target->rela_irelative_section(layout); |
| 2386 | unsigned int r_type = elfcpp::R_X86_64_IRELATIVE; |
| 2387 | rela_dyn->add_symbolless_global_addend(gsym, r_type, |
| 2388 | output_section, object, |
| 2389 | data_shndx, |
| 2390 | reloc.get_r_offset(), |
| 2391 | reloc.get_r_addend()); |
| 2392 | } |
| 2393 | else if (r_type == elfcpp::R_X86_64_64 |
| 2394 | && gsym->can_use_relative_reloc(false)) |
| 2395 | { |
| 2396 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
| 2397 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE, |
| 2398 | output_section, object, |
| 2399 | data_shndx, |
| 2400 | reloc.get_r_offset(), |
| 2401 | reloc.get_r_addend()); |
| 2402 | } |
| 2403 | else |
| 2404 | { |
| 2405 | this->check_non_pic(object, r_type, gsym); |
| 2406 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
| 2407 | rela_dyn->add_global(gsym, r_type, output_section, object, |
| 2408 | data_shndx, reloc.get_r_offset(), |
| 2409 | reloc.get_r_addend()); |
| 2410 | } |
| 2411 | } |
| 2412 | } |
| 2413 | break; |
| 2414 | |
| 2415 | case elfcpp::R_X86_64_PC64: |
| 2416 | case elfcpp::R_X86_64_PC32: |
| 2417 | case elfcpp::R_X86_64_PC16: |
| 2418 | case elfcpp::R_X86_64_PC8: |
| 2419 | { |
| 2420 | // Make a PLT entry if necessary. |
| 2421 | if (gsym->needs_plt_entry()) |
| 2422 | target->make_plt_entry(symtab, layout, gsym); |
| 2423 | // Make a dynamic relocation if necessary. |
| 2424 | if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))) |
| 2425 | { |
| 2426 | if (gsym->may_need_copy_reloc()) |
| 2427 | { |
| 2428 | target->copy_reloc(symtab, layout, object, |
| 2429 | data_shndx, output_section, gsym, reloc); |
| 2430 | } |
| 2431 | else |
| 2432 | { |
| 2433 | this->check_non_pic(object, r_type, gsym); |
| 2434 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
| 2435 | rela_dyn->add_global(gsym, r_type, output_section, object, |
| 2436 | data_shndx, reloc.get_r_offset(), |
| 2437 | reloc.get_r_addend()); |
| 2438 | } |
| 2439 | } |
| 2440 | } |
| 2441 | break; |
| 2442 | |
| 2443 | case elfcpp::R_X86_64_GOT64: |
| 2444 | case elfcpp::R_X86_64_GOT32: |
| 2445 | case elfcpp::R_X86_64_GOTPCREL64: |
| 2446 | case elfcpp::R_X86_64_GOTPCREL: |
| 2447 | case elfcpp::R_X86_64_GOTPLT64: |
| 2448 | { |
| 2449 | // The symbol requires a GOT entry. |
| 2450 | Output_data_got<64, false>* got = target->got_section(symtab, layout); |
| 2451 | if (gsym->final_value_is_known()) |
| 2452 | { |
| 2453 | // For a STT_GNU_IFUNC symbol we want the PLT address. |
| 2454 | if (gsym->type() == elfcpp::STT_GNU_IFUNC) |
| 2455 | got->add_global_plt(gsym, GOT_TYPE_STANDARD); |
| 2456 | else |
| 2457 | got->add_global(gsym, GOT_TYPE_STANDARD); |
| 2458 | } |
| 2459 | else |
| 2460 | { |
| 2461 | // If this symbol is not fully resolved, we need to add a |
| 2462 | // dynamic relocation for it. |
| 2463 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
| 2464 | |
| 2465 | // Use a GLOB_DAT rather than a RELATIVE reloc if: |
| 2466 | // |
| 2467 | // 1) The symbol may be defined in some other module. |
| 2468 | // |
| 2469 | // 2) We are building a shared library and this is a |
| 2470 | // protected symbol; using GLOB_DAT means that the dynamic |
| 2471 | // linker can use the address of the PLT in the main |
| 2472 | // executable when appropriate so that function address |
| 2473 | // comparisons work. |
| 2474 | // |
| 2475 | // 3) This is a STT_GNU_IFUNC symbol in position dependent |
| 2476 | // code, again so that function address comparisons work. |
| 2477 | if (gsym->is_from_dynobj() |
| 2478 | || gsym->is_undefined() |
| 2479 | || gsym->is_preemptible() |
| 2480 | || (gsym->visibility() == elfcpp::STV_PROTECTED |
| 2481 | && parameters->options().shared()) |
| 2482 | || (gsym->type() == elfcpp::STT_GNU_IFUNC |
| 2483 | && parameters->options().output_is_position_independent())) |
| 2484 | got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn, |
| 2485 | elfcpp::R_X86_64_GLOB_DAT); |
| 2486 | else |
| 2487 | { |
| 2488 | // For a STT_GNU_IFUNC symbol we want to write the PLT |
| 2489 | // offset into the GOT, so that function pointer |
| 2490 | // comparisons work correctly. |
| 2491 | bool is_new; |
| 2492 | if (gsym->type() != elfcpp::STT_GNU_IFUNC) |
| 2493 | is_new = got->add_global(gsym, GOT_TYPE_STANDARD); |
| 2494 | else |
| 2495 | { |
| 2496 | is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD); |
| 2497 | // Tell the dynamic linker to use the PLT address |
| 2498 | // when resolving relocations. |
| 2499 | if (gsym->is_from_dynobj() |
| 2500 | && !parameters->options().shared()) |
| 2501 | gsym->set_needs_dynsym_value(); |
| 2502 | } |
| 2503 | if (is_new) |
| 2504 | { |
| 2505 | unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD); |
| 2506 | rela_dyn->add_global_relative(gsym, |
| 2507 | elfcpp::R_X86_64_RELATIVE, |
| 2508 | got, got_off, 0); |
| 2509 | } |
| 2510 | } |
| 2511 | } |
| 2512 | // For GOTPLT64, we also need a PLT entry (but only if the |
| 2513 | // symbol is not fully resolved). |
| 2514 | if (r_type == elfcpp::R_X86_64_GOTPLT64 |
| 2515 | && !gsym->final_value_is_known()) |
| 2516 | target->make_plt_entry(symtab, layout, gsym); |
| 2517 | } |
| 2518 | break; |
| 2519 | |
| 2520 | case elfcpp::R_X86_64_PLT32: |
| 2521 | // If the symbol is fully resolved, this is just a PC32 reloc. |
| 2522 | // Otherwise we need a PLT entry. |
| 2523 | if (gsym->final_value_is_known()) |
| 2524 | break; |
| 2525 | // If building a shared library, we can also skip the PLT entry |
| 2526 | // if the symbol is defined in the output file and is protected |
| 2527 | // or hidden. |
| 2528 | if (gsym->is_defined() |
| 2529 | && !gsym->is_from_dynobj() |
| 2530 | && !gsym->is_preemptible()) |
| 2531 | break; |
| 2532 | target->make_plt_entry(symtab, layout, gsym); |
| 2533 | break; |
| 2534 | |
| 2535 | case elfcpp::R_X86_64_GOTPC32: |
| 2536 | case elfcpp::R_X86_64_GOTOFF64: |
| 2537 | case elfcpp::R_X86_64_GOTPC64: |
| 2538 | case elfcpp::R_X86_64_PLTOFF64: |
| 2539 | // We need a GOT section. |
| 2540 | target->got_section(symtab, layout); |
| 2541 | // For PLTOFF64, we also need a PLT entry (but only if the |
| 2542 | // symbol is not fully resolved). |
| 2543 | if (r_type == elfcpp::R_X86_64_PLTOFF64 |
| 2544 | && !gsym->final_value_is_known()) |
| 2545 | target->make_plt_entry(symtab, layout, gsym); |
| 2546 | break; |
| 2547 | |
| 2548 | case elfcpp::R_X86_64_COPY: |
| 2549 | case elfcpp::R_X86_64_GLOB_DAT: |
| 2550 | case elfcpp::R_X86_64_JUMP_SLOT: |
| 2551 | case elfcpp::R_X86_64_RELATIVE: |
| 2552 | case elfcpp::R_X86_64_IRELATIVE: |
| 2553 | // These are outstanding tls relocs, which are unexpected when linking |
| 2554 | case elfcpp::R_X86_64_TPOFF64: |
| 2555 | case elfcpp::R_X86_64_DTPMOD64: |
| 2556 | case elfcpp::R_X86_64_TLSDESC: |
| 2557 | gold_error(_("%s: unexpected reloc %u in object file"), |
| 2558 | object->name().c_str(), r_type); |
| 2559 | break; |
| 2560 | |
| 2561 | // These are initial tls relocs, which are expected for global() |
| 2562 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
| 2563 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) |
| 2564 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 2565 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 2566 | case elfcpp::R_X86_64_DTPOFF32: |
| 2567 | case elfcpp::R_X86_64_DTPOFF64: |
| 2568 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 2569 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 2570 | { |
| 2571 | const bool is_final = gsym->final_value_is_known(); |
| 2572 | const tls::Tls_optimization optimized_type |
| 2573 | = Target_x86_64::optimize_tls_reloc(is_final, r_type); |
| 2574 | switch (r_type) |
| 2575 | { |
| 2576 | case elfcpp::R_X86_64_TLSGD: // General-dynamic |
| 2577 | if (optimized_type == tls::TLSOPT_NONE) |
| 2578 | { |
| 2579 | // Create a pair of GOT entries for the module index and |
| 2580 | // dtv-relative offset. |
| 2581 | Output_data_got<64, false>* got |
| 2582 | = target->got_section(symtab, layout); |
| 2583 | got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR, |
| 2584 | target->rela_dyn_section(layout), |
| 2585 | elfcpp::R_X86_64_DTPMOD64, |
| 2586 | elfcpp::R_X86_64_DTPOFF64); |
| 2587 | } |
| 2588 | else if (optimized_type == tls::TLSOPT_TO_IE) |
| 2589 | { |
| 2590 | // Create a GOT entry for the tp-relative offset. |
| 2591 | Output_data_got<64, false>* got |
| 2592 | = target->got_section(symtab, layout); |
| 2593 | got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET, |
| 2594 | target->rela_dyn_section(layout), |
| 2595 | elfcpp::R_X86_64_TPOFF64); |
| 2596 | } |
| 2597 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 2598 | unsupported_reloc_global(object, r_type, gsym); |
| 2599 | break; |
| 2600 | |
| 2601 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: |
| 2602 | target->define_tls_base_symbol(symtab, layout); |
| 2603 | if (optimized_type == tls::TLSOPT_NONE) |
| 2604 | { |
| 2605 | // Create reserved PLT and GOT entries for the resolver. |
| 2606 | target->reserve_tlsdesc_entries(symtab, layout); |
| 2607 | |
| 2608 | // Create a double GOT entry with an R_X86_64_TLSDESC |
| 2609 | // reloc. The R_X86_64_TLSDESC reloc is resolved |
| 2610 | // lazily, so the GOT entry needs to be in an area in |
| 2611 | // .got.plt, not .got. Call got_section to make sure |
| 2612 | // the section has been created. |
| 2613 | target->got_section(symtab, layout); |
| 2614 | Output_data_got<64, false>* got = target->got_tlsdesc_section(); |
| 2615 | Reloc_section* rt = target->rela_tlsdesc_section(layout); |
| 2616 | got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt, |
| 2617 | elfcpp::R_X86_64_TLSDESC, 0); |
| 2618 | } |
| 2619 | else if (optimized_type == tls::TLSOPT_TO_IE) |
| 2620 | { |
| 2621 | // Create a GOT entry for the tp-relative offset. |
| 2622 | Output_data_got<64, false>* got |
| 2623 | = target->got_section(symtab, layout); |
| 2624 | got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET, |
| 2625 | target->rela_dyn_section(layout), |
| 2626 | elfcpp::R_X86_64_TPOFF64); |
| 2627 | } |
| 2628 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 2629 | unsupported_reloc_global(object, r_type, gsym); |
| 2630 | break; |
| 2631 | |
| 2632 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 2633 | break; |
| 2634 | |
| 2635 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 2636 | if (optimized_type == tls::TLSOPT_NONE) |
| 2637 | { |
| 2638 | // Create a GOT entry for the module index. |
| 2639 | target->got_mod_index_entry(symtab, layout, object); |
| 2640 | } |
| 2641 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 2642 | unsupported_reloc_global(object, r_type, gsym); |
| 2643 | break; |
| 2644 | |
| 2645 | case elfcpp::R_X86_64_DTPOFF32: |
| 2646 | case elfcpp::R_X86_64_DTPOFF64: |
| 2647 | break; |
| 2648 | |
| 2649 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 2650 | layout->set_has_static_tls(); |
| 2651 | if (optimized_type == tls::TLSOPT_NONE) |
| 2652 | { |
| 2653 | // Create a GOT entry for the tp-relative offset. |
| 2654 | Output_data_got<64, false>* got |
| 2655 | = target->got_section(symtab, layout); |
| 2656 | got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET, |
| 2657 | target->rela_dyn_section(layout), |
| 2658 | elfcpp::R_X86_64_TPOFF64); |
| 2659 | } |
| 2660 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 2661 | unsupported_reloc_global(object, r_type, gsym); |
| 2662 | break; |
| 2663 | |
| 2664 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 2665 | layout->set_has_static_tls(); |
| 2666 | if (parameters->options().shared()) |
| 2667 | unsupported_reloc_local(object, r_type); |
| 2668 | break; |
| 2669 | |
| 2670 | default: |
| 2671 | gold_unreachable(); |
| 2672 | } |
| 2673 | } |
| 2674 | break; |
| 2675 | |
| 2676 | case elfcpp::R_X86_64_SIZE32: |
| 2677 | case elfcpp::R_X86_64_SIZE64: |
| 2678 | default: |
| 2679 | gold_error(_("%s: unsupported reloc %u against global symbol %s"), |
| 2680 | object->name().c_str(), r_type, |
| 2681 | gsym->demangled_name().c_str()); |
| 2682 | break; |
| 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | void |
| 2687 | Target_x86_64::gc_process_relocs(Symbol_table* symtab, |
| 2688 | Layout* layout, |
| 2689 | Sized_relobj_file<64, false>* object, |
| 2690 | unsigned int data_shndx, |
| 2691 | unsigned int sh_type, |
| 2692 | const unsigned char* prelocs, |
| 2693 | size_t reloc_count, |
| 2694 | Output_section* output_section, |
| 2695 | bool needs_special_offset_handling, |
| 2696 | size_t local_symbol_count, |
| 2697 | const unsigned char* plocal_symbols) |
| 2698 | { |
| 2699 | |
| 2700 | if (sh_type == elfcpp::SHT_REL) |
| 2701 | { |
| 2702 | return; |
| 2703 | } |
| 2704 | |
| 2705 | gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA, |
| 2706 | Target_x86_64::Scan, |
| 2707 | Target_x86_64::Relocatable_size_for_reloc>( |
| 2708 | symtab, |
| 2709 | layout, |
| 2710 | this, |
| 2711 | object, |
| 2712 | data_shndx, |
| 2713 | prelocs, |
| 2714 | reloc_count, |
| 2715 | output_section, |
| 2716 | needs_special_offset_handling, |
| 2717 | local_symbol_count, |
| 2718 | plocal_symbols); |
| 2719 | |
| 2720 | } |
| 2721 | // Scan relocations for a section. |
| 2722 | |
| 2723 | void |
| 2724 | Target_x86_64::scan_relocs(Symbol_table* symtab, |
| 2725 | Layout* layout, |
| 2726 | Sized_relobj_file<64, false>* object, |
| 2727 | unsigned int data_shndx, |
| 2728 | unsigned int sh_type, |
| 2729 | const unsigned char* prelocs, |
| 2730 | size_t reloc_count, |
| 2731 | Output_section* output_section, |
| 2732 | bool needs_special_offset_handling, |
| 2733 | size_t local_symbol_count, |
| 2734 | const unsigned char* plocal_symbols) |
| 2735 | { |
| 2736 | if (sh_type == elfcpp::SHT_REL) |
| 2737 | { |
| 2738 | gold_error(_("%s: unsupported REL reloc section"), |
| 2739 | object->name().c_str()); |
| 2740 | return; |
| 2741 | } |
| 2742 | |
| 2743 | gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA, |
| 2744 | Target_x86_64::Scan>( |
| 2745 | symtab, |
| 2746 | layout, |
| 2747 | this, |
| 2748 | object, |
| 2749 | data_shndx, |
| 2750 | prelocs, |
| 2751 | reloc_count, |
| 2752 | output_section, |
| 2753 | needs_special_offset_handling, |
| 2754 | local_symbol_count, |
| 2755 | plocal_symbols); |
| 2756 | } |
| 2757 | |
| 2758 | // Finalize the sections. |
| 2759 | |
| 2760 | void |
| 2761 | Target_x86_64::do_finalize_sections( |
| 2762 | Layout* layout, |
| 2763 | const Input_objects*, |
| 2764 | Symbol_table* symtab) |
| 2765 | { |
| 2766 | const Reloc_section* rel_plt = (this->plt_ == NULL |
| 2767 | ? NULL |
| 2768 | : this->plt_->rela_plt()); |
| 2769 | layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt, |
| 2770 | this->rela_dyn_, true, false); |
| 2771 | |
| 2772 | // Fill in some more dynamic tags. |
| 2773 | Output_data_dynamic* const odyn = layout->dynamic_data(); |
| 2774 | if (odyn != NULL) |
| 2775 | { |
| 2776 | if (this->plt_ != NULL |
| 2777 | && this->plt_->output_section() != NULL |
| 2778 | && this->plt_->has_tlsdesc_entry()) |
| 2779 | { |
| 2780 | unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset(); |
| 2781 | unsigned int got_offset = this->plt_->get_tlsdesc_got_offset(); |
| 2782 | this->got_->finalize_data_size(); |
| 2783 | odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT, |
| 2784 | this->plt_, plt_offset); |
| 2785 | odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT, |
| 2786 | this->got_, got_offset); |
| 2787 | } |
| 2788 | } |
| 2789 | |
| 2790 | // Emit any relocs we saved in an attempt to avoid generating COPY |
| 2791 | // relocs. |
| 2792 | if (this->copy_relocs_.any_saved_relocs()) |
| 2793 | this->copy_relocs_.emit(this->rela_dyn_section(layout)); |
| 2794 | |
| 2795 | // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of |
| 2796 | // the .got.plt section. |
| 2797 | Symbol* sym = this->global_offset_table_; |
| 2798 | if (sym != NULL) |
| 2799 | { |
| 2800 | uint64_t data_size = this->got_plt_->current_data_size(); |
| 2801 | symtab->get_sized_symbol<64>(sym)->set_symsize(data_size); |
| 2802 | } |
| 2803 | |
| 2804 | if (parameters->doing_static_link() |
| 2805 | && (this->plt_ == NULL || !this->plt_->has_irelative_section())) |
| 2806 | { |
| 2807 | // If linking statically, make sure that the __rela_iplt symbols |
| 2808 | // were defined if necessary, even if we didn't create a PLT. |
| 2809 | static const Define_symbol_in_segment syms[] = |
| 2810 | { |
| 2811 | { |
| 2812 | "__rela_iplt_start", // name |
| 2813 | elfcpp::PT_LOAD, // segment_type |
| 2814 | elfcpp::PF_W, // segment_flags_set |
| 2815 | elfcpp::PF(0), // segment_flags_clear |
| 2816 | 0, // value |
| 2817 | 0, // size |
| 2818 | elfcpp::STT_NOTYPE, // type |
| 2819 | elfcpp::STB_GLOBAL, // binding |
| 2820 | elfcpp::STV_HIDDEN, // visibility |
| 2821 | 0, // nonvis |
| 2822 | Symbol::SEGMENT_START, // offset_from_base |
| 2823 | true // only_if_ref |
| 2824 | }, |
| 2825 | { |
| 2826 | "__rela_iplt_end", // name |
| 2827 | elfcpp::PT_LOAD, // segment_type |
| 2828 | elfcpp::PF_W, // segment_flags_set |
| 2829 | elfcpp::PF(0), // segment_flags_clear |
| 2830 | 0, // value |
| 2831 | 0, // size |
| 2832 | elfcpp::STT_NOTYPE, // type |
| 2833 | elfcpp::STB_GLOBAL, // binding |
| 2834 | elfcpp::STV_HIDDEN, // visibility |
| 2835 | 0, // nonvis |
| 2836 | Symbol::SEGMENT_START, // offset_from_base |
| 2837 | true // only_if_ref |
| 2838 | } |
| 2839 | }; |
| 2840 | |
| 2841 | symtab->define_symbols(layout, 2, syms, |
| 2842 | layout->script_options()->saw_sections_clause()); |
| 2843 | } |
| 2844 | } |
| 2845 | |
| 2846 | // Perform a relocation. |
| 2847 | |
| 2848 | inline bool |
| 2849 | Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo, |
| 2850 | Target_x86_64* target, |
| 2851 | Output_section*, |
| 2852 | size_t relnum, |
| 2853 | const elfcpp::Rela<64, false>& rela, |
| 2854 | unsigned int r_type, |
| 2855 | const Sized_symbol<64>* gsym, |
| 2856 | const Symbol_value<64>* psymval, |
| 2857 | unsigned char* view, |
| 2858 | elfcpp::Elf_types<64>::Elf_Addr address, |
| 2859 | section_size_type view_size) |
| 2860 | { |
| 2861 | if (this->skip_call_tls_get_addr_) |
| 2862 | { |
| 2863 | if ((r_type != elfcpp::R_X86_64_PLT32 |
| 2864 | && r_type != elfcpp::R_X86_64_PC32) |
| 2865 | || gsym == NULL |
| 2866 | || strcmp(gsym->name(), "__tls_get_addr") != 0) |
| 2867 | { |
| 2868 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
| 2869 | _("missing expected TLS relocation")); |
| 2870 | } |
| 2871 | else |
| 2872 | { |
| 2873 | this->skip_call_tls_get_addr_ = false; |
| 2874 | return false; |
| 2875 | } |
| 2876 | } |
| 2877 | |
| 2878 | const Sized_relobj_file<64, false>* object = relinfo->object; |
| 2879 | |
| 2880 | // Pick the value to use for symbols defined in the PLT. |
| 2881 | Symbol_value<64> symval; |
| 2882 | if (gsym != NULL |
| 2883 | && gsym->use_plt_offset(Scan::get_reference_flags(r_type))) |
| 2884 | { |
| 2885 | symval.set_output_value(target->plt_address_for_global(gsym) |
| 2886 | + gsym->plt_offset()); |
| 2887 | psymval = &symval; |
| 2888 | } |
| 2889 | else if (gsym == NULL && psymval->is_ifunc_symbol()) |
| 2890 | { |
| 2891 | unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info()); |
| 2892 | if (object->local_has_plt_offset(r_sym)) |
| 2893 | { |
| 2894 | symval.set_output_value(target->plt_address_for_local(object, r_sym) |
| 2895 | + object->local_plt_offset(r_sym)); |
| 2896 | psymval = &symval; |
| 2897 | } |
| 2898 | } |
| 2899 | |
| 2900 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); |
| 2901 | |
| 2902 | // Get the GOT offset if needed. |
| 2903 | // The GOT pointer points to the end of the GOT section. |
| 2904 | // We need to subtract the size of the GOT section to get |
| 2905 | // the actual offset to use in the relocation. |
| 2906 | bool have_got_offset = false; |
| 2907 | unsigned int got_offset = 0; |
| 2908 | switch (r_type) |
| 2909 | { |
| 2910 | case elfcpp::R_X86_64_GOT32: |
| 2911 | case elfcpp::R_X86_64_GOT64: |
| 2912 | case elfcpp::R_X86_64_GOTPLT64: |
| 2913 | case elfcpp::R_X86_64_GOTPCREL: |
| 2914 | case elfcpp::R_X86_64_GOTPCREL64: |
| 2915 | if (gsym != NULL) |
| 2916 | { |
| 2917 | gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD)); |
| 2918 | got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size(); |
| 2919 | } |
| 2920 | else |
| 2921 | { |
| 2922 | unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info()); |
| 2923 | gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD)); |
| 2924 | got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD) |
| 2925 | - target->got_size()); |
| 2926 | } |
| 2927 | have_got_offset = true; |
| 2928 | break; |
| 2929 | |
| 2930 | default: |
| 2931 | break; |
| 2932 | } |
| 2933 | |
| 2934 | switch (r_type) |
| 2935 | { |
| 2936 | case elfcpp::R_X86_64_NONE: |
| 2937 | case elfcpp::R_X86_64_GNU_VTINHERIT: |
| 2938 | case elfcpp::R_X86_64_GNU_VTENTRY: |
| 2939 | break; |
| 2940 | |
| 2941 | case elfcpp::R_X86_64_64: |
| 2942 | Relocate_functions<64, false>::rela64(view, object, psymval, addend); |
| 2943 | break; |
| 2944 | |
| 2945 | case elfcpp::R_X86_64_PC64: |
| 2946 | Relocate_functions<64, false>::pcrela64(view, object, psymval, addend, |
| 2947 | address); |
| 2948 | break; |
| 2949 | |
| 2950 | case elfcpp::R_X86_64_32: |
| 2951 | // FIXME: we need to verify that value + addend fits into 32 bits: |
| 2952 | // uint64_t x = value + addend; |
| 2953 | // x == static_cast<uint64_t>(static_cast<uint32_t>(x)) |
| 2954 | // Likewise for other <=32-bit relocations (but see R_X86_64_32S). |
| 2955 | Relocate_functions<64, false>::rela32(view, object, psymval, addend); |
| 2956 | break; |
| 2957 | |
| 2958 | case elfcpp::R_X86_64_32S: |
| 2959 | // FIXME: we need to verify that value + addend fits into 32 bits: |
| 2960 | // int64_t x = value + addend; // note this quantity is signed! |
| 2961 | // x == static_cast<int64_t>(static_cast<int32_t>(x)) |
| 2962 | Relocate_functions<64, false>::rela32(view, object, psymval, addend); |
| 2963 | break; |
| 2964 | |
| 2965 | case elfcpp::R_X86_64_PC32: |
| 2966 | Relocate_functions<64, false>::pcrela32(view, object, psymval, addend, |
| 2967 | address); |
| 2968 | break; |
| 2969 | |
| 2970 | case elfcpp::R_X86_64_16: |
| 2971 | Relocate_functions<64, false>::rela16(view, object, psymval, addend); |
| 2972 | break; |
| 2973 | |
| 2974 | case elfcpp::R_X86_64_PC16: |
| 2975 | Relocate_functions<64, false>::pcrela16(view, object, psymval, addend, |
| 2976 | address); |
| 2977 | break; |
| 2978 | |
| 2979 | case elfcpp::R_X86_64_8: |
| 2980 | Relocate_functions<64, false>::rela8(view, object, psymval, addend); |
| 2981 | break; |
| 2982 | |
| 2983 | case elfcpp::R_X86_64_PC8: |
| 2984 | Relocate_functions<64, false>::pcrela8(view, object, psymval, addend, |
| 2985 | address); |
| 2986 | break; |
| 2987 | |
| 2988 | case elfcpp::R_X86_64_PLT32: |
| 2989 | gold_assert(gsym == NULL |
| 2990 | || gsym->has_plt_offset() |
| 2991 | || gsym->final_value_is_known() |
| 2992 | || (gsym->is_defined() |
| 2993 | && !gsym->is_from_dynobj() |
| 2994 | && !gsym->is_preemptible())); |
| 2995 | // Note: while this code looks the same as for R_X86_64_PC32, it |
| 2996 | // behaves differently because psymval was set to point to |
| 2997 | // the PLT entry, rather than the symbol, in Scan::global(). |
| 2998 | Relocate_functions<64, false>::pcrela32(view, object, psymval, addend, |
| 2999 | address); |
| 3000 | break; |
| 3001 | |
| 3002 | case elfcpp::R_X86_64_PLTOFF64: |
| 3003 | { |
| 3004 | gold_assert(gsym); |
| 3005 | gold_assert(gsym->has_plt_offset() |
| 3006 | || gsym->final_value_is_known()); |
| 3007 | elfcpp::Elf_types<64>::Elf_Addr got_address; |
| 3008 | got_address = target->got_section(NULL, NULL)->address(); |
| 3009 | Relocate_functions<64, false>::rela64(view, object, psymval, |
| 3010 | addend - got_address); |
| 3011 | } |
| 3012 | |
| 3013 | case elfcpp::R_X86_64_GOT32: |
| 3014 | gold_assert(have_got_offset); |
| 3015 | Relocate_functions<64, false>::rela32(view, got_offset, addend); |
| 3016 | break; |
| 3017 | |
| 3018 | case elfcpp::R_X86_64_GOTPC32: |
| 3019 | { |
| 3020 | gold_assert(gsym); |
| 3021 | elfcpp::Elf_types<64>::Elf_Addr value; |
| 3022 | value = target->got_plt_section()->address(); |
| 3023 | Relocate_functions<64, false>::pcrela32(view, value, addend, address); |
| 3024 | } |
| 3025 | break; |
| 3026 | |
| 3027 | case elfcpp::R_X86_64_GOT64: |
| 3028 | // The ABI doc says "Like GOT64, but indicates a PLT entry is needed." |
| 3029 | // Since we always add a PLT entry, this is equivalent. |
| 3030 | case elfcpp::R_X86_64_GOTPLT64: |
| 3031 | gold_assert(have_got_offset); |
| 3032 | Relocate_functions<64, false>::rela64(view, got_offset, addend); |
| 3033 | break; |
| 3034 | |
| 3035 | case elfcpp::R_X86_64_GOTPC64: |
| 3036 | { |
| 3037 | gold_assert(gsym); |
| 3038 | elfcpp::Elf_types<64>::Elf_Addr value; |
| 3039 | value = target->got_plt_section()->address(); |
| 3040 | Relocate_functions<64, false>::pcrela64(view, value, addend, address); |
| 3041 | } |
| 3042 | break; |
| 3043 | |
| 3044 | case elfcpp::R_X86_64_GOTOFF64: |
| 3045 | { |
| 3046 | elfcpp::Elf_types<64>::Elf_Addr value; |
| 3047 | value = (psymval->value(object, 0) |
| 3048 | - target->got_plt_section()->address()); |
| 3049 | Relocate_functions<64, false>::rela64(view, value, addend); |
| 3050 | } |
| 3051 | break; |
| 3052 | |
| 3053 | case elfcpp::R_X86_64_GOTPCREL: |
| 3054 | { |
| 3055 | gold_assert(have_got_offset); |
| 3056 | elfcpp::Elf_types<64>::Elf_Addr value; |
| 3057 | value = target->got_plt_section()->address() + got_offset; |
| 3058 | Relocate_functions<64, false>::pcrela32(view, value, addend, address); |
| 3059 | } |
| 3060 | break; |
| 3061 | |
| 3062 | case elfcpp::R_X86_64_GOTPCREL64: |
| 3063 | { |
| 3064 | gold_assert(have_got_offset); |
| 3065 | elfcpp::Elf_types<64>::Elf_Addr value; |
| 3066 | value = target->got_plt_section()->address() + got_offset; |
| 3067 | Relocate_functions<64, false>::pcrela64(view, value, addend, address); |
| 3068 | } |
| 3069 | break; |
| 3070 | |
| 3071 | case elfcpp::R_X86_64_COPY: |
| 3072 | case elfcpp::R_X86_64_GLOB_DAT: |
| 3073 | case elfcpp::R_X86_64_JUMP_SLOT: |
| 3074 | case elfcpp::R_X86_64_RELATIVE: |
| 3075 | case elfcpp::R_X86_64_IRELATIVE: |
| 3076 | // These are outstanding tls relocs, which are unexpected when linking |
| 3077 | case elfcpp::R_X86_64_TPOFF64: |
| 3078 | case elfcpp::R_X86_64_DTPMOD64: |
| 3079 | case elfcpp::R_X86_64_TLSDESC: |
| 3080 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
| 3081 | _("unexpected reloc %u in object file"), |
| 3082 | r_type); |
| 3083 | break; |
| 3084 | |
| 3085 | // These are initial tls relocs, which are expected when linking |
| 3086 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
| 3087 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) |
| 3088 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 3089 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 3090 | case elfcpp::R_X86_64_DTPOFF32: |
| 3091 | case elfcpp::R_X86_64_DTPOFF64: |
| 3092 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 3093 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 3094 | this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval, |
| 3095 | view, address, view_size); |
| 3096 | break; |
| 3097 | |
| 3098 | case elfcpp::R_X86_64_SIZE32: |
| 3099 | case elfcpp::R_X86_64_SIZE64: |
| 3100 | default: |
| 3101 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
| 3102 | _("unsupported reloc %u"), |
| 3103 | r_type); |
| 3104 | break; |
| 3105 | } |
| 3106 | |
| 3107 | return true; |
| 3108 | } |
| 3109 | |
| 3110 | // Perform a TLS relocation. |
| 3111 | |
| 3112 | inline void |
| 3113 | Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo, |
| 3114 | Target_x86_64* target, |
| 3115 | size_t relnum, |
| 3116 | const elfcpp::Rela<64, false>& rela, |
| 3117 | unsigned int r_type, |
| 3118 | const Sized_symbol<64>* gsym, |
| 3119 | const Symbol_value<64>* psymval, |
| 3120 | unsigned char* view, |
| 3121 | elfcpp::Elf_types<64>::Elf_Addr address, |
| 3122 | section_size_type view_size) |
| 3123 | { |
| 3124 | Output_segment* tls_segment = relinfo->layout->tls_segment(); |
| 3125 | |
| 3126 | const Sized_relobj_file<64, false>* object = relinfo->object; |
| 3127 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); |
| 3128 | elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr); |
| 3129 | bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0; |
| 3130 | |
| 3131 | elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0); |
| 3132 | |
| 3133 | const bool is_final = (gsym == NULL |
| 3134 | ? !parameters->options().shared() |
| 3135 | : gsym->final_value_is_known()); |
| 3136 | tls::Tls_optimization optimized_type |
| 3137 | = Target_x86_64::optimize_tls_reloc(is_final, r_type); |
| 3138 | switch (r_type) |
| 3139 | { |
| 3140 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
| 3141 | if (!is_executable && optimized_type == tls::TLSOPT_TO_LE) |
| 3142 | { |
| 3143 | // If this code sequence is used in a non-executable section, |
| 3144 | // we will not optimize the R_X86_64_DTPOFF32/64 relocation, |
| 3145 | // on the assumption that it's being used by itself in a debug |
| 3146 | // section. Therefore, in the unlikely event that the code |
| 3147 | // sequence appears in a non-executable section, we simply |
| 3148 | // leave it unoptimized. |
| 3149 | optimized_type = tls::TLSOPT_NONE; |
| 3150 | } |
| 3151 | if (optimized_type == tls::TLSOPT_TO_LE) |
| 3152 | { |
| 3153 | if (tls_segment == NULL) |
| 3154 | { |
| 3155 | gold_assert(parameters->errors()->error_count() > 0 |
| 3156 | || issue_undefined_symbol_error(gsym)); |
| 3157 | return; |
| 3158 | } |
| 3159 | this->tls_gd_to_le(relinfo, relnum, tls_segment, |
| 3160 | rela, r_type, value, view, |
| 3161 | view_size); |
| 3162 | break; |
| 3163 | } |
| 3164 | else |
| 3165 | { |
| 3166 | unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE |
| 3167 | ? GOT_TYPE_TLS_OFFSET |
| 3168 | : GOT_TYPE_TLS_PAIR); |
| 3169 | unsigned int got_offset; |
| 3170 | if (gsym != NULL) |
| 3171 | { |
| 3172 | gold_assert(gsym->has_got_offset(got_type)); |
| 3173 | got_offset = gsym->got_offset(got_type) - target->got_size(); |
| 3174 | } |
| 3175 | else |
| 3176 | { |
| 3177 | unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info()); |
| 3178 | gold_assert(object->local_has_got_offset(r_sym, got_type)); |
| 3179 | got_offset = (object->local_got_offset(r_sym, got_type) |
| 3180 | - target->got_size()); |
| 3181 | } |
| 3182 | if (optimized_type == tls::TLSOPT_TO_IE) |
| 3183 | { |
| 3184 | value = target->got_plt_section()->address() + got_offset; |
| 3185 | this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type, |
| 3186 | value, view, address, view_size); |
| 3187 | break; |
| 3188 | } |
| 3189 | else if (optimized_type == tls::TLSOPT_NONE) |
| 3190 | { |
| 3191 | // Relocate the field with the offset of the pair of GOT |
| 3192 | // entries. |
| 3193 | value = target->got_plt_section()->address() + got_offset; |
| 3194 | Relocate_functions<64, false>::pcrela32(view, value, addend, |
| 3195 | address); |
| 3196 | break; |
| 3197 | } |
| 3198 | } |
| 3199 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
| 3200 | _("unsupported reloc %u"), r_type); |
| 3201 | break; |
| 3202 | |
| 3203 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) |
| 3204 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 3205 | if (!is_executable && optimized_type == tls::TLSOPT_TO_LE) |
| 3206 | { |
| 3207 | // See above comment for R_X86_64_TLSGD. |
| 3208 | optimized_type = tls::TLSOPT_NONE; |
| 3209 | } |
| 3210 | if (optimized_type == tls::TLSOPT_TO_LE) |
| 3211 | { |
| 3212 | if (tls_segment == NULL) |
| 3213 | { |
| 3214 | gold_assert(parameters->errors()->error_count() > 0 |
| 3215 | || issue_undefined_symbol_error(gsym)); |
| 3216 | return; |
| 3217 | } |
| 3218 | this->tls_desc_gd_to_le(relinfo, relnum, tls_segment, |
| 3219 | rela, r_type, value, view, |
| 3220 | view_size); |
| 3221 | break; |
| 3222 | } |
| 3223 | else |
| 3224 | { |
| 3225 | unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE |
| 3226 | ? GOT_TYPE_TLS_OFFSET |
| 3227 | : GOT_TYPE_TLS_DESC); |
| 3228 | unsigned int got_offset = 0; |
| 3229 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC |
| 3230 | && optimized_type == tls::TLSOPT_NONE) |
| 3231 | { |
| 3232 | // We created GOT entries in the .got.tlsdesc portion of |
| 3233 | // the .got.plt section, but the offset stored in the |
| 3234 | // symbol is the offset within .got.tlsdesc. |
| 3235 | got_offset = (target->got_size() |
| 3236 | + target->got_plt_section()->data_size()); |
| 3237 | } |
| 3238 | if (gsym != NULL) |
| 3239 | { |
| 3240 | gold_assert(gsym->has_got_offset(got_type)); |
| 3241 | got_offset += gsym->got_offset(got_type) - target->got_size(); |
| 3242 | } |
| 3243 | else |
| 3244 | { |
| 3245 | unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info()); |
| 3246 | gold_assert(object->local_has_got_offset(r_sym, got_type)); |
| 3247 | got_offset += (object->local_got_offset(r_sym, got_type) |
| 3248 | - target->got_size()); |
| 3249 | } |
| 3250 | if (optimized_type == tls::TLSOPT_TO_IE) |
| 3251 | { |
| 3252 | if (tls_segment == NULL) |
| 3253 | { |
| 3254 | gold_assert(parameters->errors()->error_count() > 0 |
| 3255 | || issue_undefined_symbol_error(gsym)); |
| 3256 | return; |
| 3257 | } |
| 3258 | value = target->got_plt_section()->address() + got_offset; |
| 3259 | this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, |
| 3260 | rela, r_type, value, view, address, |
| 3261 | view_size); |
| 3262 | break; |
| 3263 | } |
| 3264 | else if (optimized_type == tls::TLSOPT_NONE) |
| 3265 | { |
| 3266 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) |
| 3267 | { |
| 3268 | // Relocate the field with the offset of the pair of GOT |
| 3269 | // entries. |
| 3270 | value = target->got_plt_section()->address() + got_offset; |
| 3271 | Relocate_functions<64, false>::pcrela32(view, value, addend, |
| 3272 | address); |
| 3273 | } |
| 3274 | break; |
| 3275 | } |
| 3276 | } |
| 3277 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
| 3278 | _("unsupported reloc %u"), r_type); |
| 3279 | break; |
| 3280 | |
| 3281 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 3282 | if (!is_executable && optimized_type == tls::TLSOPT_TO_LE) |
| 3283 | { |
| 3284 | // See above comment for R_X86_64_TLSGD. |
| 3285 | optimized_type = tls::TLSOPT_NONE; |
| 3286 | } |
| 3287 | if (optimized_type == tls::TLSOPT_TO_LE) |
| 3288 | { |
| 3289 | if (tls_segment == NULL) |
| 3290 | { |
| 3291 | gold_assert(parameters->errors()->error_count() > 0 |
| 3292 | || issue_undefined_symbol_error(gsym)); |
| 3293 | return; |
| 3294 | } |
| 3295 | this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type, |
| 3296 | value, view, view_size); |
| 3297 | break; |
| 3298 | } |
| 3299 | else if (optimized_type == tls::TLSOPT_NONE) |
| 3300 | { |
| 3301 | // Relocate the field with the offset of the GOT entry for |
| 3302 | // the module index. |
| 3303 | unsigned int got_offset; |
| 3304 | got_offset = (target->got_mod_index_entry(NULL, NULL, NULL) |
| 3305 | - target->got_size()); |
| 3306 | value = target->got_plt_section()->address() + got_offset; |
| 3307 | Relocate_functions<64, false>::pcrela32(view, value, addend, |
| 3308 | address); |
| 3309 | break; |
| 3310 | } |
| 3311 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
| 3312 | _("unsupported reloc %u"), r_type); |
| 3313 | break; |
| 3314 | |
| 3315 | case elfcpp::R_X86_64_DTPOFF32: |
| 3316 | // This relocation type is used in debugging information. |
| 3317 | // In that case we need to not optimize the value. If the |
| 3318 | // section is not executable, then we assume we should not |
| 3319 | // optimize this reloc. See comments above for R_X86_64_TLSGD, |
| 3320 | // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and |
| 3321 | // R_X86_64_TLSLD. |
| 3322 | if (optimized_type == tls::TLSOPT_TO_LE && is_executable) |
| 3323 | { |
| 3324 | if (tls_segment == NULL) |
| 3325 | { |
| 3326 | gold_assert(parameters->errors()->error_count() > 0 |
| 3327 | || issue_undefined_symbol_error(gsym)); |
| 3328 | return; |
| 3329 | } |
| 3330 | value -= tls_segment->memsz(); |
| 3331 | } |
| 3332 | Relocate_functions<64, false>::rela32(view, value, addend); |
| 3333 | break; |
| 3334 | |
| 3335 | case elfcpp::R_X86_64_DTPOFF64: |
| 3336 | // See R_X86_64_DTPOFF32, just above, for why we check for is_executable. |
| 3337 | if (optimized_type == tls::TLSOPT_TO_LE && is_executable) |
| 3338 | { |
| 3339 | if (tls_segment == NULL) |
| 3340 | { |
| 3341 | gold_assert(parameters->errors()->error_count() > 0 |
| 3342 | || issue_undefined_symbol_error(gsym)); |
| 3343 | return; |
| 3344 | } |
| 3345 | value -= tls_segment->memsz(); |
| 3346 | } |
| 3347 | Relocate_functions<64, false>::rela64(view, value, addend); |
| 3348 | break; |
| 3349 | |
| 3350 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 3351 | if (optimized_type == tls::TLSOPT_TO_LE) |
| 3352 | { |
| 3353 | if (tls_segment == NULL) |
| 3354 | { |
| 3355 | gold_assert(parameters->errors()->error_count() > 0 |
| 3356 | || issue_undefined_symbol_error(gsym)); |
| 3357 | return; |
| 3358 | } |
| 3359 | Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment, |
| 3360 | rela, r_type, value, view, |
| 3361 | view_size); |
| 3362 | break; |
| 3363 | } |
| 3364 | else if (optimized_type == tls::TLSOPT_NONE) |
| 3365 | { |
| 3366 | // Relocate the field with the offset of the GOT entry for |
| 3367 | // the tp-relative offset of the symbol. |
| 3368 | unsigned int got_offset; |
| 3369 | if (gsym != NULL) |
| 3370 | { |
| 3371 | gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET)); |
| 3372 | got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET) |
| 3373 | - target->got_size()); |
| 3374 | } |
| 3375 | else |
| 3376 | { |
| 3377 | unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info()); |
| 3378 | gold_assert(object->local_has_got_offset(r_sym, |
| 3379 | GOT_TYPE_TLS_OFFSET)); |
| 3380 | got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET) |
| 3381 | - target->got_size()); |
| 3382 | } |
| 3383 | value = target->got_plt_section()->address() + got_offset; |
| 3384 | Relocate_functions<64, false>::pcrela32(view, value, addend, address); |
| 3385 | break; |
| 3386 | } |
| 3387 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
| 3388 | _("unsupported reloc type %u"), |
| 3389 | r_type); |
| 3390 | break; |
| 3391 | |
| 3392 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 3393 | if (tls_segment == NULL) |
| 3394 | { |
| 3395 | gold_assert(parameters->errors()->error_count() > 0 |
| 3396 | || issue_undefined_symbol_error(gsym)); |
| 3397 | return; |
| 3398 | } |
| 3399 | value -= tls_segment->memsz(); |
| 3400 | Relocate_functions<64, false>::rela32(view, value, addend); |
| 3401 | break; |
| 3402 | } |
| 3403 | } |
| 3404 | |
| 3405 | // Do a relocation in which we convert a TLS General-Dynamic to an |
| 3406 | // Initial-Exec. |
| 3407 | |
| 3408 | inline void |
| 3409 | Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo, |
| 3410 | size_t relnum, |
| 3411 | Output_segment*, |
| 3412 | const elfcpp::Rela<64, false>& rela, |
| 3413 | unsigned int, |
| 3414 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 3415 | unsigned char* view, |
| 3416 | elfcpp::Elf_types<64>::Elf_Addr address, |
| 3417 | section_size_type view_size) |
| 3418 | { |
| 3419 | // .byte 0x66; leaq foo@tlsgd(%rip),%rdi; |
| 3420 | // .word 0x6666; rex64; call __tls_get_addr |
| 3421 | // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax |
| 3422 | |
| 3423 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4); |
| 3424 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12); |
| 3425 | |
| 3426 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 3427 | (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0)); |
| 3428 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 3429 | (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0)); |
| 3430 | |
| 3431 | memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16); |
| 3432 | |
| 3433 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); |
| 3434 | Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address); |
| 3435 | |
| 3436 | // The next reloc should be a PLT32 reloc against __tls_get_addr. |
| 3437 | // We can skip it. |
| 3438 | this->skip_call_tls_get_addr_ = true; |
| 3439 | } |
| 3440 | |
| 3441 | // Do a relocation in which we convert a TLS General-Dynamic to a |
| 3442 | // Local-Exec. |
| 3443 | |
| 3444 | inline void |
| 3445 | Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo, |
| 3446 | size_t relnum, |
| 3447 | Output_segment* tls_segment, |
| 3448 | const elfcpp::Rela<64, false>& rela, |
| 3449 | unsigned int, |
| 3450 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 3451 | unsigned char* view, |
| 3452 | section_size_type view_size) |
| 3453 | { |
| 3454 | // .byte 0x66; leaq foo@tlsgd(%rip),%rdi; |
| 3455 | // .word 0x6666; rex64; call __tls_get_addr |
| 3456 | // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax |
| 3457 | |
| 3458 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4); |
| 3459 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12); |
| 3460 | |
| 3461 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 3462 | (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0)); |
| 3463 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 3464 | (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0)); |
| 3465 | |
| 3466 | memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16); |
| 3467 | |
| 3468 | value -= tls_segment->memsz(); |
| 3469 | Relocate_functions<64, false>::rela32(view + 8, value, 0); |
| 3470 | |
| 3471 | // The next reloc should be a PLT32 reloc against __tls_get_addr. |
| 3472 | // We can skip it. |
| 3473 | this->skip_call_tls_get_addr_ = true; |
| 3474 | } |
| 3475 | |
| 3476 | // Do a TLSDESC-style General-Dynamic to Initial-Exec transition. |
| 3477 | |
| 3478 | inline void |
| 3479 | Target_x86_64::Relocate::tls_desc_gd_to_ie( |
| 3480 | const Relocate_info<64, false>* relinfo, |
| 3481 | size_t relnum, |
| 3482 | Output_segment*, |
| 3483 | const elfcpp::Rela<64, false>& rela, |
| 3484 | unsigned int r_type, |
| 3485 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 3486 | unsigned char* view, |
| 3487 | elfcpp::Elf_types<64>::Elf_Addr address, |
| 3488 | section_size_type view_size) |
| 3489 | { |
| 3490 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) |
| 3491 | { |
| 3492 | // leaq foo@tlsdesc(%rip), %rax |
| 3493 | // ==> movq foo@gottpoff(%rip), %rax |
| 3494 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); |
| 3495 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); |
| 3496 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 3497 | view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05); |
| 3498 | view[-2] = 0x8b; |
| 3499 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); |
| 3500 | Relocate_functions<64, false>::pcrela32(view, value, addend, address); |
| 3501 | } |
| 3502 | else |
| 3503 | { |
| 3504 | // call *foo@tlscall(%rax) |
| 3505 | // ==> nop; nop |
| 3506 | gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL); |
| 3507 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2); |
| 3508 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 3509 | view[0] == 0xff && view[1] == 0x10); |
| 3510 | view[0] = 0x66; |
| 3511 | view[1] = 0x90; |
| 3512 | } |
| 3513 | } |
| 3514 | |
| 3515 | // Do a TLSDESC-style General-Dynamic to Local-Exec transition. |
| 3516 | |
| 3517 | inline void |
| 3518 | Target_x86_64::Relocate::tls_desc_gd_to_le( |
| 3519 | const Relocate_info<64, false>* relinfo, |
| 3520 | size_t relnum, |
| 3521 | Output_segment* tls_segment, |
| 3522 | const elfcpp::Rela<64, false>& rela, |
| 3523 | unsigned int r_type, |
| 3524 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 3525 | unsigned char* view, |
| 3526 | section_size_type view_size) |
| 3527 | { |
| 3528 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) |
| 3529 | { |
| 3530 | // leaq foo@tlsdesc(%rip), %rax |
| 3531 | // ==> movq foo@tpoff, %rax |
| 3532 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); |
| 3533 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); |
| 3534 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 3535 | view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05); |
| 3536 | view[-2] = 0xc7; |
| 3537 | view[-1] = 0xc0; |
| 3538 | value -= tls_segment->memsz(); |
| 3539 | Relocate_functions<64, false>::rela32(view, value, 0); |
| 3540 | } |
| 3541 | else |
| 3542 | { |
| 3543 | // call *foo@tlscall(%rax) |
| 3544 | // ==> nop; nop |
| 3545 | gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL); |
| 3546 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2); |
| 3547 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 3548 | view[0] == 0xff && view[1] == 0x10); |
| 3549 | view[0] = 0x66; |
| 3550 | view[1] = 0x90; |
| 3551 | } |
| 3552 | } |
| 3553 | |
| 3554 | inline void |
| 3555 | Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo, |
| 3556 | size_t relnum, |
| 3557 | Output_segment*, |
| 3558 | const elfcpp::Rela<64, false>& rela, |
| 3559 | unsigned int, |
| 3560 | elfcpp::Elf_types<64>::Elf_Addr, |
| 3561 | unsigned char* view, |
| 3562 | section_size_type view_size) |
| 3563 | { |
| 3564 | // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt; |
| 3565 | // ... leq foo@dtpoff(%rax),%reg |
| 3566 | // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx |
| 3567 | |
| 3568 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); |
| 3569 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9); |
| 3570 | |
| 3571 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 3572 | view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d); |
| 3573 | |
| 3574 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8); |
| 3575 | |
| 3576 | memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12); |
| 3577 | |
| 3578 | // The next reloc should be a PLT32 reloc against __tls_get_addr. |
| 3579 | // We can skip it. |
| 3580 | this->skip_call_tls_get_addr_ = true; |
| 3581 | } |
| 3582 | |
| 3583 | // Do a relocation in which we convert a TLS Initial-Exec to a |
| 3584 | // Local-Exec. |
| 3585 | |
| 3586 | inline void |
| 3587 | Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo, |
| 3588 | size_t relnum, |
| 3589 | Output_segment* tls_segment, |
| 3590 | const elfcpp::Rela<64, false>& rela, |
| 3591 | unsigned int, |
| 3592 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 3593 | unsigned char* view, |
| 3594 | section_size_type view_size) |
| 3595 | { |
| 3596 | // We need to examine the opcodes to figure out which instruction we |
| 3597 | // are looking at. |
| 3598 | |
| 3599 | // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg |
| 3600 | // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg |
| 3601 | |
| 3602 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); |
| 3603 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); |
| 3604 | |
| 3605 | unsigned char op1 = view[-3]; |
| 3606 | unsigned char op2 = view[-2]; |
| 3607 | unsigned char op3 = view[-1]; |
| 3608 | unsigned char reg = op3 >> 3; |
| 3609 | |
| 3610 | if (op2 == 0x8b) |
| 3611 | { |
| 3612 | // movq |
| 3613 | if (op1 == 0x4c) |
| 3614 | view[-3] = 0x49; |
| 3615 | view[-2] = 0xc7; |
| 3616 | view[-1] = 0xc0 | reg; |
| 3617 | } |
| 3618 | else if (reg == 4) |
| 3619 | { |
| 3620 | // Special handling for %rsp. |
| 3621 | if (op1 == 0x4c) |
| 3622 | view[-3] = 0x49; |
| 3623 | view[-2] = 0x81; |
| 3624 | view[-1] = 0xc0 | reg; |
| 3625 | } |
| 3626 | else |
| 3627 | { |
| 3628 | // addq |
| 3629 | if (op1 == 0x4c) |
| 3630 | view[-3] = 0x4d; |
| 3631 | view[-2] = 0x8d; |
| 3632 | view[-1] = 0x80 | reg | (reg << 3); |
| 3633 | } |
| 3634 | |
| 3635 | value -= tls_segment->memsz(); |
| 3636 | Relocate_functions<64, false>::rela32(view, value, 0); |
| 3637 | } |
| 3638 | |
| 3639 | // Relocate section data. |
| 3640 | |
| 3641 | void |
| 3642 | Target_x86_64::relocate_section( |
| 3643 | const Relocate_info<64, false>* relinfo, |
| 3644 | unsigned int sh_type, |
| 3645 | const unsigned char* prelocs, |
| 3646 | size_t reloc_count, |
| 3647 | Output_section* output_section, |
| 3648 | bool needs_special_offset_handling, |
| 3649 | unsigned char* view, |
| 3650 | elfcpp::Elf_types<64>::Elf_Addr address, |
| 3651 | section_size_type view_size, |
| 3652 | const Reloc_symbol_changes* reloc_symbol_changes) |
| 3653 | { |
| 3654 | gold_assert(sh_type == elfcpp::SHT_RELA); |
| 3655 | |
| 3656 | gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA, |
| 3657 | Target_x86_64::Relocate>( |
| 3658 | relinfo, |
| 3659 | this, |
| 3660 | prelocs, |
| 3661 | reloc_count, |
| 3662 | output_section, |
| 3663 | needs_special_offset_handling, |
| 3664 | view, |
| 3665 | address, |
| 3666 | view_size, |
| 3667 | reloc_symbol_changes); |
| 3668 | } |
| 3669 | |
| 3670 | // Apply an incremental relocation. Incremental relocations always refer |
| 3671 | // to global symbols. |
| 3672 | |
| 3673 | void |
| 3674 | Target_x86_64::apply_relocation( |
| 3675 | const Relocate_info<64, false>* relinfo, |
| 3676 | elfcpp::Elf_types<64>::Elf_Addr r_offset, |
| 3677 | unsigned int r_type, |
| 3678 | elfcpp::Elf_types<64>::Elf_Swxword r_addend, |
| 3679 | const Symbol* gsym, |
| 3680 | unsigned char* view, |
| 3681 | elfcpp::Elf_types<64>::Elf_Addr address, |
| 3682 | section_size_type view_size) |
| 3683 | { |
| 3684 | gold::apply_relocation<64, false, Target_x86_64, Target_x86_64::Relocate>( |
| 3685 | relinfo, |
| 3686 | this, |
| 3687 | r_offset, |
| 3688 | r_type, |
| 3689 | r_addend, |
| 3690 | gsym, |
| 3691 | view, |
| 3692 | address, |
| 3693 | view_size); |
| 3694 | } |
| 3695 | |
| 3696 | // Return the size of a relocation while scanning during a relocatable |
| 3697 | // link. |
| 3698 | |
| 3699 | unsigned int |
| 3700 | Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc( |
| 3701 | unsigned int r_type, |
| 3702 | Relobj* object) |
| 3703 | { |
| 3704 | switch (r_type) |
| 3705 | { |
| 3706 | case elfcpp::R_X86_64_NONE: |
| 3707 | case elfcpp::R_X86_64_GNU_VTINHERIT: |
| 3708 | case elfcpp::R_X86_64_GNU_VTENTRY: |
| 3709 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
| 3710 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) |
| 3711 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 3712 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 3713 | case elfcpp::R_X86_64_DTPOFF32: |
| 3714 | case elfcpp::R_X86_64_DTPOFF64: |
| 3715 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 3716 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 3717 | return 0; |
| 3718 | |
| 3719 | case elfcpp::R_X86_64_64: |
| 3720 | case elfcpp::R_X86_64_PC64: |
| 3721 | case elfcpp::R_X86_64_GOTOFF64: |
| 3722 | case elfcpp::R_X86_64_GOTPC64: |
| 3723 | case elfcpp::R_X86_64_PLTOFF64: |
| 3724 | case elfcpp::R_X86_64_GOT64: |
| 3725 | case elfcpp::R_X86_64_GOTPCREL64: |
| 3726 | case elfcpp::R_X86_64_GOTPCREL: |
| 3727 | case elfcpp::R_X86_64_GOTPLT64: |
| 3728 | return 8; |
| 3729 | |
| 3730 | case elfcpp::R_X86_64_32: |
| 3731 | case elfcpp::R_X86_64_32S: |
| 3732 | case elfcpp::R_X86_64_PC32: |
| 3733 | case elfcpp::R_X86_64_PLT32: |
| 3734 | case elfcpp::R_X86_64_GOTPC32: |
| 3735 | case elfcpp::R_X86_64_GOT32: |
| 3736 | return 4; |
| 3737 | |
| 3738 | case elfcpp::R_X86_64_16: |
| 3739 | case elfcpp::R_X86_64_PC16: |
| 3740 | return 2; |
| 3741 | |
| 3742 | case elfcpp::R_X86_64_8: |
| 3743 | case elfcpp::R_X86_64_PC8: |
| 3744 | return 1; |
| 3745 | |
| 3746 | case elfcpp::R_X86_64_COPY: |
| 3747 | case elfcpp::R_X86_64_GLOB_DAT: |
| 3748 | case elfcpp::R_X86_64_JUMP_SLOT: |
| 3749 | case elfcpp::R_X86_64_RELATIVE: |
| 3750 | case elfcpp::R_X86_64_IRELATIVE: |
| 3751 | // These are outstanding tls relocs, which are unexpected when linking |
| 3752 | case elfcpp::R_X86_64_TPOFF64: |
| 3753 | case elfcpp::R_X86_64_DTPMOD64: |
| 3754 | case elfcpp::R_X86_64_TLSDESC: |
| 3755 | object->error(_("unexpected reloc %u in object file"), r_type); |
| 3756 | return 0; |
| 3757 | |
| 3758 | case elfcpp::R_X86_64_SIZE32: |
| 3759 | case elfcpp::R_X86_64_SIZE64: |
| 3760 | default: |
| 3761 | object->error(_("unsupported reloc %u against local symbol"), r_type); |
| 3762 | return 0; |
| 3763 | } |
| 3764 | } |
| 3765 | |
| 3766 | // Scan the relocs during a relocatable link. |
| 3767 | |
| 3768 | void |
| 3769 | Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab, |
| 3770 | Layout* layout, |
| 3771 | Sized_relobj_file<64, false>* object, |
| 3772 | unsigned int data_shndx, |
| 3773 | unsigned int sh_type, |
| 3774 | const unsigned char* prelocs, |
| 3775 | size_t reloc_count, |
| 3776 | Output_section* output_section, |
| 3777 | bool needs_special_offset_handling, |
| 3778 | size_t local_symbol_count, |
| 3779 | const unsigned char* plocal_symbols, |
| 3780 | Relocatable_relocs* rr) |
| 3781 | { |
| 3782 | gold_assert(sh_type == elfcpp::SHT_RELA); |
| 3783 | |
| 3784 | typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA, |
| 3785 | Relocatable_size_for_reloc> Scan_relocatable_relocs; |
| 3786 | |
| 3787 | gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA, |
| 3788 | Scan_relocatable_relocs>( |
| 3789 | symtab, |
| 3790 | layout, |
| 3791 | object, |
| 3792 | data_shndx, |
| 3793 | prelocs, |
| 3794 | reloc_count, |
| 3795 | output_section, |
| 3796 | needs_special_offset_handling, |
| 3797 | local_symbol_count, |
| 3798 | plocal_symbols, |
| 3799 | rr); |
| 3800 | } |
| 3801 | |
| 3802 | // Relocate a section during a relocatable link. |
| 3803 | |
| 3804 | void |
| 3805 | Target_x86_64::relocate_for_relocatable( |
| 3806 | const Relocate_info<64, false>* relinfo, |
| 3807 | unsigned int sh_type, |
| 3808 | const unsigned char* prelocs, |
| 3809 | size_t reloc_count, |
| 3810 | Output_section* output_section, |
| 3811 | off_t offset_in_output_section, |
| 3812 | const Relocatable_relocs* rr, |
| 3813 | unsigned char* view, |
| 3814 | elfcpp::Elf_types<64>::Elf_Addr view_address, |
| 3815 | section_size_type view_size, |
| 3816 | unsigned char* reloc_view, |
| 3817 | section_size_type reloc_view_size) |
| 3818 | { |
| 3819 | gold_assert(sh_type == elfcpp::SHT_RELA); |
| 3820 | |
| 3821 | gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>( |
| 3822 | relinfo, |
| 3823 | prelocs, |
| 3824 | reloc_count, |
| 3825 | output_section, |
| 3826 | offset_in_output_section, |
| 3827 | rr, |
| 3828 | view, |
| 3829 | view_address, |
| 3830 | view_size, |
| 3831 | reloc_view, |
| 3832 | reloc_view_size); |
| 3833 | } |
| 3834 | |
| 3835 | // Return the value to use for a dynamic which requires special |
| 3836 | // treatment. This is how we support equality comparisons of function |
| 3837 | // pointers across shared library boundaries, as described in the |
| 3838 | // processor specific ABI supplement. |
| 3839 | |
| 3840 | uint64_t |
| 3841 | Target_x86_64::do_dynsym_value(const Symbol* gsym) const |
| 3842 | { |
| 3843 | gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset()); |
| 3844 | return this->plt_address_for_global(gsym) + gsym->plt_offset(); |
| 3845 | } |
| 3846 | |
| 3847 | // Return a string used to fill a code section with nops to take up |
| 3848 | // the specified length. |
| 3849 | |
| 3850 | std::string |
| 3851 | Target_x86_64::do_code_fill(section_size_type length) const |
| 3852 | { |
| 3853 | if (length >= 16) |
| 3854 | { |
| 3855 | // Build a jmpq instruction to skip over the bytes. |
| 3856 | unsigned char jmp[5]; |
| 3857 | jmp[0] = 0xe9; |
| 3858 | elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5); |
| 3859 | return (std::string(reinterpret_cast<char*>(&jmp[0]), 5) |
| 3860 | + std::string(length - 5, '\0')); |
| 3861 | } |
| 3862 | |
| 3863 | // Nop sequences of various lengths. |
| 3864 | const char nop1[1] = { 0x90 }; // nop |
| 3865 | const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax |
| 3866 | const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax) |
| 3867 | const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax) |
| 3868 | const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1) |
| 3869 | 0x00 }; |
| 3870 | const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1) |
| 3871 | 0x00, 0x00 }; |
| 3872 | const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax) |
| 3873 | 0x00, 0x00, 0x00 }; |
| 3874 | const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1) |
| 3875 | 0x00, 0x00, 0x00, 0x00 }; |
| 3876 | const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1) |
| 3877 | 0x00, 0x00, 0x00, 0x00, |
| 3878 | 0x00 }; |
| 3879 | const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1) |
| 3880 | 0x84, 0x00, 0x00, 0x00, |
| 3881 | 0x00, 0x00 }; |
| 3882 | const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16 |
| 3883 | 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1) |
| 3884 | 0x00, 0x00, 0x00 }; |
| 3885 | const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16 |
| 3886 | 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1) |
| 3887 | 0x00, 0x00, 0x00, 0x00 }; |
| 3888 | const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16 |
| 3889 | 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1) |
| 3890 | 0x00, 0x00, 0x00, 0x00, |
| 3891 | 0x00 }; |
| 3892 | const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16 |
| 3893 | 0x66, 0x2e, 0x0f, 0x1f, // data16 |
| 3894 | 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1) |
| 3895 | 0x00, 0x00 }; |
| 3896 | const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16 |
| 3897 | 0x66, 0x66, 0x2e, 0x0f, // data16; data16 |
| 3898 | 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1) |
| 3899 | 0x00, 0x00, 0x00 }; |
| 3900 | |
| 3901 | const char* nops[16] = { |
| 3902 | NULL, |
| 3903 | nop1, nop2, nop3, nop4, nop5, nop6, nop7, |
| 3904 | nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15 |
| 3905 | }; |
| 3906 | |
| 3907 | return std::string(nops[length], length); |
| 3908 | } |
| 3909 | |
| 3910 | // Return the addend to use for a target specific relocation. The |
| 3911 | // only target specific relocation is R_X86_64_TLSDESC for a local |
| 3912 | // symbol. We want to set the addend is the offset of the local |
| 3913 | // symbol in the TLS segment. |
| 3914 | |
| 3915 | uint64_t |
| 3916 | Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type, |
| 3917 | uint64_t) const |
| 3918 | { |
| 3919 | gold_assert(r_type == elfcpp::R_X86_64_TLSDESC); |
| 3920 | uintptr_t intarg = reinterpret_cast<uintptr_t>(arg); |
| 3921 | gold_assert(intarg < this->tlsdesc_reloc_info_.size()); |
| 3922 | const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]); |
| 3923 | const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym); |
| 3924 | gold_assert(psymval->is_tls_symbol()); |
| 3925 | // The value of a TLS symbol is the offset in the TLS segment. |
| 3926 | return psymval->value(ti.object, 0); |
| 3927 | } |
| 3928 | |
| 3929 | // Return the value to use for the base of a DW_EH_PE_datarel offset |
| 3930 | // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their |
| 3931 | // assembler can not write out the difference between two labels in |
| 3932 | // different sections, so instead of using a pc-relative value they |
| 3933 | // use an offset from the GOT. |
| 3934 | |
| 3935 | uint64_t |
| 3936 | Target_x86_64::do_ehframe_datarel_base() const |
| 3937 | { |
| 3938 | gold_assert(this->global_offset_table_ != NULL); |
| 3939 | Symbol* sym = this->global_offset_table_; |
| 3940 | Sized_symbol<64>* ssym = static_cast<Sized_symbol<64>*>(sym); |
| 3941 | return ssym->value(); |
| 3942 | } |
| 3943 | |
| 3944 | // FNOFFSET in section SHNDX in OBJECT is the start of a function |
| 3945 | // compiled with -fsplit-stack. The function calls non-split-stack |
| 3946 | // code. We have to change the function so that it always ensures |
| 3947 | // that it has enough stack space to run some random function. |
| 3948 | |
| 3949 | void |
| 3950 | Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx, |
| 3951 | section_offset_type fnoffset, |
| 3952 | section_size_type fnsize, |
| 3953 | unsigned char* view, |
| 3954 | section_size_type view_size, |
| 3955 | std::string* from, |
| 3956 | std::string* to) const |
| 3957 | { |
| 3958 | // The function starts with a comparison of the stack pointer and a |
| 3959 | // field in the TCB. This is followed by a jump. |
| 3960 | |
| 3961 | // cmp %fs:NN,%rsp |
| 3962 | if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5) |
| 3963 | && fnsize > 9) |
| 3964 | { |
| 3965 | // We will call __morestack if the carry flag is set after this |
| 3966 | // comparison. We turn the comparison into an stc instruction |
| 3967 | // and some nops. |
| 3968 | view[fnoffset] = '\xf9'; |
| 3969 | this->set_view_to_nop(view, view_size, fnoffset + 1, 8); |
| 3970 | } |
| 3971 | // lea NN(%rsp),%r10 |
| 3972 | // lea NN(%rsp),%r11 |
| 3973 | else if ((this->match_view(view, view_size, fnoffset, |
| 3974 | "\x4c\x8d\x94\x24", 4) |
| 3975 | || this->match_view(view, view_size, fnoffset, |
| 3976 | "\x4c\x8d\x9c\x24", 4)) |
| 3977 | && fnsize > 8) |
| 3978 | { |
| 3979 | // This is loading an offset from the stack pointer for a |
| 3980 | // comparison. The offset is negative, so we decrease the |
| 3981 | // offset by the amount of space we need for the stack. This |
| 3982 | // means we will avoid calling __morestack if there happens to |
| 3983 | // be plenty of space on the stack already. |
| 3984 | unsigned char* pval = view + fnoffset + 4; |
| 3985 | uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval); |
| 3986 | val -= parameters->options().split_stack_adjust_size(); |
| 3987 | elfcpp::Swap_unaligned<32, false>::writeval(pval, val); |
| 3988 | } |
| 3989 | else |
| 3990 | { |
| 3991 | if (!object->has_no_split_stack()) |
| 3992 | object->error(_("failed to match split-stack sequence at " |
| 3993 | "section %u offset %0zx"), |
| 3994 | shndx, static_cast<size_t>(fnoffset)); |
| 3995 | return; |
| 3996 | } |
| 3997 | |
| 3998 | // We have to change the function so that it calls |
| 3999 | // __morestack_non_split instead of __morestack. The former will |
| 4000 | // allocate additional stack space. |
| 4001 | *from = "__morestack"; |
| 4002 | *to = "__morestack_non_split"; |
| 4003 | } |
| 4004 | |
| 4005 | // The selector for x86_64 object files. |
| 4006 | |
| 4007 | class Target_selector_x86_64 : public Target_selector_freebsd |
| 4008 | { |
| 4009 | public: |
| 4010 | Target_selector_x86_64() |
| 4011 | : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64", |
| 4012 | "elf64-x86-64-freebsd", "elf_x86_64") |
| 4013 | { } |
| 4014 | |
| 4015 | Target* |
| 4016 | do_instantiate_target() |
| 4017 | { return new Target_x86_64(); } |
| 4018 | |
| 4019 | }; |
| 4020 | |
| 4021 | Target_selector_x86_64 target_selector_x86_64; |
| 4022 | |
| 4023 | } // End anonymous namespace. |