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