| 1 | // x86_64.cc -- x86_64 target support for gold. |
| 2 | |
| 3 | // Copyright 2006, 2007, 2008 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 "parameters.h" |
| 29 | #include "reloc.h" |
| 30 | #include "x86_64.h" |
| 31 | #include "object.h" |
| 32 | #include "symtab.h" |
| 33 | #include "layout.h" |
| 34 | #include "output.h" |
| 35 | #include "copy-relocs.h" |
| 36 | #include "target.h" |
| 37 | #include "target-reloc.h" |
| 38 | #include "target-select.h" |
| 39 | #include "tls.h" |
| 40 | |
| 41 | namespace |
| 42 | { |
| 43 | |
| 44 | using namespace gold; |
| 45 | |
| 46 | class Output_data_plt_x86_64; |
| 47 | |
| 48 | // The x86_64 target class. |
| 49 | // See the ABI at |
| 50 | // http://www.x86-64.org/documentation/abi.pdf |
| 51 | // TLS info comes from |
| 52 | // http://people.redhat.com/drepper/tls.pdf |
| 53 | // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt |
| 54 | |
| 55 | class Target_x86_64 : public Sized_target<64, false> |
| 56 | { |
| 57 | public: |
| 58 | // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures |
| 59 | // uses only Elf64_Rela relocation entries with explicit addends." |
| 60 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section; |
| 61 | |
| 62 | Target_x86_64() |
| 63 | : Sized_target<64, false>(&x86_64_info), |
| 64 | got_(NULL), plt_(NULL), got_plt_(NULL), rela_dyn_(NULL), |
| 65 | copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL), |
| 66 | got_mod_index_offset_(-1U), tls_base_symbol_defined_(false) |
| 67 | { } |
| 68 | |
| 69 | // Scan the relocations to look for symbol adjustments. |
| 70 | void |
| 71 | scan_relocs(const General_options& options, |
| 72 | Symbol_table* symtab, |
| 73 | Layout* layout, |
| 74 | Sized_relobj<64, false>* object, |
| 75 | unsigned int data_shndx, |
| 76 | unsigned int sh_type, |
| 77 | const unsigned char* prelocs, |
| 78 | size_t reloc_count, |
| 79 | Output_section* output_section, |
| 80 | bool needs_special_offset_handling, |
| 81 | size_t local_symbol_count, |
| 82 | const unsigned char* plocal_symbols); |
| 83 | |
| 84 | // Finalize the sections. |
| 85 | void |
| 86 | do_finalize_sections(Layout*); |
| 87 | |
| 88 | // Return the value to use for a dynamic which requires special |
| 89 | // treatment. |
| 90 | uint64_t |
| 91 | do_dynsym_value(const Symbol*) const; |
| 92 | |
| 93 | // Relocate a section. |
| 94 | void |
| 95 | relocate_section(const Relocate_info<64, false>*, |
| 96 | unsigned int sh_type, |
| 97 | const unsigned char* prelocs, |
| 98 | size_t reloc_count, |
| 99 | Output_section* output_section, |
| 100 | bool needs_special_offset_handling, |
| 101 | unsigned char* view, |
| 102 | elfcpp::Elf_types<64>::Elf_Addr view_address, |
| 103 | section_size_type view_size); |
| 104 | |
| 105 | // Scan the relocs during a relocatable link. |
| 106 | void |
| 107 | scan_relocatable_relocs(const General_options& options, |
| 108 | Symbol_table* symtab, |
| 109 | Layout* layout, |
| 110 | Sized_relobj<64, false>* object, |
| 111 | unsigned int data_shndx, |
| 112 | unsigned int sh_type, |
| 113 | const unsigned char* prelocs, |
| 114 | size_t reloc_count, |
| 115 | Output_section* output_section, |
| 116 | bool needs_special_offset_handling, |
| 117 | size_t local_symbol_count, |
| 118 | const unsigned char* plocal_symbols, |
| 119 | Relocatable_relocs*); |
| 120 | |
| 121 | // Relocate a section during a relocatable link. |
| 122 | void |
| 123 | relocate_for_relocatable(const Relocate_info<64, false>*, |
| 124 | unsigned int sh_type, |
| 125 | const unsigned char* prelocs, |
| 126 | size_t reloc_count, |
| 127 | Output_section* output_section, |
| 128 | off_t offset_in_output_section, |
| 129 | const Relocatable_relocs*, |
| 130 | unsigned char* view, |
| 131 | elfcpp::Elf_types<64>::Elf_Addr view_address, |
| 132 | section_size_type view_size, |
| 133 | unsigned char* reloc_view, |
| 134 | section_size_type reloc_view_size); |
| 135 | |
| 136 | // Return a string used to fill a code section with nops. |
| 137 | std::string |
| 138 | do_code_fill(section_size_type length) const; |
| 139 | |
| 140 | // Return whether SYM is defined by the ABI. |
| 141 | bool |
| 142 | do_is_defined_by_abi(Symbol* sym) const |
| 143 | { return strcmp(sym->name(), "__tls_get_addr") == 0; } |
| 144 | |
| 145 | // Return the size of the GOT section. |
| 146 | section_size_type |
| 147 | got_size() |
| 148 | { |
| 149 | gold_assert(this->got_ != NULL); |
| 150 | return this->got_->data_size(); |
| 151 | } |
| 152 | |
| 153 | private: |
| 154 | // The class which scans relocations. |
| 155 | class Scan |
| 156 | { |
| 157 | public: |
| 158 | Scan() |
| 159 | : issued_non_pic_error_(false) |
| 160 | { } |
| 161 | |
| 162 | inline void |
| 163 | local(const General_options& options, Symbol_table* symtab, |
| 164 | Layout* layout, Target_x86_64* target, |
| 165 | Sized_relobj<64, false>* object, |
| 166 | unsigned int data_shndx, |
| 167 | Output_section* output_section, |
| 168 | const elfcpp::Rela<64, false>& reloc, unsigned int r_type, |
| 169 | const elfcpp::Sym<64, false>& lsym); |
| 170 | |
| 171 | inline void |
| 172 | global(const General_options& options, Symbol_table* symtab, |
| 173 | Layout* layout, Target_x86_64* target, |
| 174 | Sized_relobj<64, false>* object, |
| 175 | unsigned int data_shndx, |
| 176 | Output_section* output_section, |
| 177 | const elfcpp::Rela<64, false>& reloc, unsigned int r_type, |
| 178 | Symbol* gsym); |
| 179 | |
| 180 | private: |
| 181 | static void |
| 182 | unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type); |
| 183 | |
| 184 | static void |
| 185 | unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type, |
| 186 | Symbol*); |
| 187 | |
| 188 | void |
| 189 | check_non_pic(Relobj*, unsigned int r_type); |
| 190 | |
| 191 | // Whether we have issued an error about a non-PIC compilation. |
| 192 | bool issued_non_pic_error_; |
| 193 | }; |
| 194 | |
| 195 | // The class which implements relocation. |
| 196 | class Relocate |
| 197 | { |
| 198 | public: |
| 199 | Relocate() |
| 200 | : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false) |
| 201 | { } |
| 202 | |
| 203 | ~Relocate() |
| 204 | { |
| 205 | if (this->skip_call_tls_get_addr_) |
| 206 | { |
| 207 | // FIXME: This needs to specify the location somehow. |
| 208 | gold_error(_("missing expected TLS relocation")); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Do a relocation. Return false if the caller should not issue |
| 213 | // any warnings about this relocation. |
| 214 | inline bool |
| 215 | relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum, |
| 216 | const elfcpp::Rela<64, false>&, |
| 217 | unsigned int r_type, const Sized_symbol<64>*, |
| 218 | const Symbol_value<64>*, |
| 219 | unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, |
| 220 | section_size_type); |
| 221 | |
| 222 | private: |
| 223 | // Do a TLS relocation. |
| 224 | inline void |
| 225 | relocate_tls(const Relocate_info<64, false>*, Target_x86_64*, |
| 226 | size_t relnum, const elfcpp::Rela<64, false>&, |
| 227 | unsigned int r_type, const Sized_symbol<64>*, |
| 228 | const Symbol_value<64>*, |
| 229 | unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, |
| 230 | section_size_type); |
| 231 | |
| 232 | // Do a TLS General-Dynamic to Initial-Exec transition. |
| 233 | inline void |
| 234 | tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum, |
| 235 | Output_segment* tls_segment, |
| 236 | const elfcpp::Rela<64, false>&, unsigned int r_type, |
| 237 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 238 | unsigned char* view, |
| 239 | elfcpp::Elf_types<64>::Elf_Addr, |
| 240 | section_size_type view_size); |
| 241 | |
| 242 | // Do a TLS General-Dynamic to Local-Exec transition. |
| 243 | inline void |
| 244 | tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum, |
| 245 | Output_segment* tls_segment, |
| 246 | const elfcpp::Rela<64, false>&, unsigned int r_type, |
| 247 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 248 | unsigned char* view, |
| 249 | section_size_type view_size); |
| 250 | |
| 251 | // Do a TLSDESC-style General-Dynamic to Initial-Exec transition. |
| 252 | inline void |
| 253 | tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum, |
| 254 | Output_segment* tls_segment, |
| 255 | const elfcpp::Rela<64, false>&, unsigned int r_type, |
| 256 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 257 | unsigned char* view, |
| 258 | elfcpp::Elf_types<64>::Elf_Addr, |
| 259 | section_size_type view_size); |
| 260 | |
| 261 | // Do a TLSDESC-style General-Dynamic to Local-Exec transition. |
| 262 | inline void |
| 263 | tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum, |
| 264 | Output_segment* tls_segment, |
| 265 | const elfcpp::Rela<64, false>&, unsigned int r_type, |
| 266 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 267 | unsigned char* view, |
| 268 | section_size_type view_size); |
| 269 | |
| 270 | // Do a TLS Local-Dynamic to Local-Exec transition. |
| 271 | inline void |
| 272 | tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum, |
| 273 | Output_segment* tls_segment, |
| 274 | const elfcpp::Rela<64, false>&, unsigned int r_type, |
| 275 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 276 | unsigned char* view, |
| 277 | section_size_type view_size); |
| 278 | |
| 279 | // Do a TLS Initial-Exec to Local-Exec transition. |
| 280 | static inline void |
| 281 | tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum, |
| 282 | Output_segment* tls_segment, |
| 283 | const elfcpp::Rela<64, false>&, unsigned int r_type, |
| 284 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 285 | unsigned char* view, |
| 286 | section_size_type view_size); |
| 287 | |
| 288 | // This is set if we should skip the next reloc, which should be a |
| 289 | // PLT32 reloc against ___tls_get_addr. |
| 290 | bool skip_call_tls_get_addr_; |
| 291 | |
| 292 | // This is set if we see a relocation which could load the address |
| 293 | // of the TLS block. Whether we see such a relocation determines |
| 294 | // how we handle the R_X86_64_DTPOFF32 relocation, which is used |
| 295 | // in debugging sections. |
| 296 | bool saw_tls_block_reloc_; |
| 297 | }; |
| 298 | |
| 299 | // A class which returns the size required for a relocation type, |
| 300 | // used while scanning relocs during a relocatable link. |
| 301 | class Relocatable_size_for_reloc |
| 302 | { |
| 303 | public: |
| 304 | unsigned int |
| 305 | get_size_for_reloc(unsigned int, Relobj*); |
| 306 | }; |
| 307 | |
| 308 | // Adjust TLS relocation type based on the options and whether this |
| 309 | // is a local symbol. |
| 310 | static tls::Tls_optimization |
| 311 | optimize_tls_reloc(bool is_final, int r_type); |
| 312 | |
| 313 | // Get the GOT section, creating it if necessary. |
| 314 | Output_data_got<64, false>* |
| 315 | got_section(Symbol_table*, Layout*); |
| 316 | |
| 317 | // Get the GOT PLT section. |
| 318 | Output_data_space* |
| 319 | got_plt_section() const |
| 320 | { |
| 321 | gold_assert(this->got_plt_ != NULL); |
| 322 | return this->got_plt_; |
| 323 | } |
| 324 | |
| 325 | // Create the PLT section. |
| 326 | void |
| 327 | make_plt_section(Symbol_table* symtab, Layout* layout); |
| 328 | |
| 329 | // Create a PLT entry for a global symbol. |
| 330 | void |
| 331 | make_plt_entry(Symbol_table*, Layout*, Symbol*); |
| 332 | |
| 333 | // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment. |
| 334 | void |
| 335 | define_tls_base_symbol(Symbol_table*, Layout*); |
| 336 | |
| 337 | // Create the reserved PLT and GOT entries for the TLS descriptor resolver. |
| 338 | void |
| 339 | reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout); |
| 340 | |
| 341 | // Create a GOT entry for the TLS module index. |
| 342 | unsigned int |
| 343 | got_mod_index_entry(Symbol_table* symtab, Layout* layout, |
| 344 | Sized_relobj<64, false>* object); |
| 345 | |
| 346 | // Get the PLT section. |
| 347 | Output_data_plt_x86_64* |
| 348 | plt_section() const |
| 349 | { |
| 350 | gold_assert(this->plt_ != NULL); |
| 351 | return this->plt_; |
| 352 | } |
| 353 | |
| 354 | // Get the dynamic reloc section, creating it if necessary. |
| 355 | Reloc_section* |
| 356 | rela_dyn_section(Layout*); |
| 357 | |
| 358 | // Return true if the symbol may need a COPY relocation. |
| 359 | // References from an executable object to non-function symbols |
| 360 | // defined in a dynamic object may need a COPY relocation. |
| 361 | bool |
| 362 | may_need_copy_reloc(Symbol* gsym) |
| 363 | { |
| 364 | return (!parameters->options().shared() |
| 365 | && gsym->is_from_dynobj() |
| 366 | && gsym->type() != elfcpp::STT_FUNC); |
| 367 | } |
| 368 | |
| 369 | // Add a potential copy relocation. |
| 370 | void |
| 371 | copy_reloc(Symbol_table* symtab, Layout* layout, Relobj* object, |
| 372 | unsigned int shndx, Output_section* output_section, |
| 373 | Symbol* sym, const elfcpp::Rela<64, false>& reloc) |
| 374 | { |
| 375 | this->copy_relocs_.copy_reloc(symtab, layout, |
| 376 | symtab->get_sized_symbol<64>(sym), |
| 377 | object, shndx, output_section, |
| 378 | reloc, this->rela_dyn_section(layout)); |
| 379 | } |
| 380 | |
| 381 | // Information about this specific target which we pass to the |
| 382 | // general Target structure. |
| 383 | static const Target::Target_info x86_64_info; |
| 384 | |
| 385 | enum Got_type |
| 386 | { |
| 387 | GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol |
| 388 | GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset |
| 389 | GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair |
| 390 | GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair |
| 391 | }; |
| 392 | |
| 393 | // The GOT section. |
| 394 | Output_data_got<64, false>* got_; |
| 395 | // The PLT section. |
| 396 | Output_data_plt_x86_64* plt_; |
| 397 | // The GOT PLT section. |
| 398 | Output_data_space* got_plt_; |
| 399 | // The dynamic reloc section. |
| 400 | Reloc_section* rela_dyn_; |
| 401 | // Relocs saved to avoid a COPY reloc. |
| 402 | Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_; |
| 403 | // Space for variables copied with a COPY reloc. |
| 404 | Output_data_space* dynbss_; |
| 405 | // Offset of the GOT entry for the TLS module index. |
| 406 | unsigned int got_mod_index_offset_; |
| 407 | // True if the _TLS_MODULE_BASE_ symbol has been defined. |
| 408 | bool tls_base_symbol_defined_; |
| 409 | }; |
| 410 | |
| 411 | const Target::Target_info Target_x86_64::x86_64_info = |
| 412 | { |
| 413 | 64, // size |
| 414 | false, // is_big_endian |
| 415 | elfcpp::EM_X86_64, // machine_code |
| 416 | false, // has_make_symbol |
| 417 | false, // has_resolve |
| 418 | true, // has_code_fill |
| 419 | true, // is_default_stack_executable |
| 420 | '\0', // wrap_char |
| 421 | "/lib/ld64.so.1", // program interpreter |
| 422 | 0x400000, // default_text_segment_address |
| 423 | 0x1000, // abi_pagesize (overridable by -z max-page-size) |
| 424 | 0x1000 // common_pagesize (overridable by -z common-page-size) |
| 425 | }; |
| 426 | |
| 427 | // Get the GOT section, creating it if necessary. |
| 428 | |
| 429 | Output_data_got<64, false>* |
| 430 | Target_x86_64::got_section(Symbol_table* symtab, Layout* layout) |
| 431 | { |
| 432 | if (this->got_ == NULL) |
| 433 | { |
| 434 | gold_assert(symtab != NULL && layout != NULL); |
| 435 | |
| 436 | this->got_ = new Output_data_got<64, false>(); |
| 437 | |
| 438 | layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, |
| 439 | elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, |
| 440 | this->got_); |
| 441 | |
| 442 | // The old GNU linker creates a .got.plt section. We just |
| 443 | // create another set of data in the .got section. Note that we |
| 444 | // always create a PLT if we create a GOT, although the PLT |
| 445 | // might be empty. |
| 446 | this->got_plt_ = new Output_data_space(8); |
| 447 | layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, |
| 448 | elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, |
| 449 | this->got_plt_); |
| 450 | |
| 451 | // The first three entries are reserved. |
| 452 | this->got_plt_->set_current_data_size(3 * 8); |
| 453 | |
| 454 | // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT. |
| 455 | symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, |
| 456 | this->got_plt_, |
| 457 | 0, 0, elfcpp::STT_OBJECT, |
| 458 | elfcpp::STB_LOCAL, |
| 459 | elfcpp::STV_HIDDEN, 0, |
| 460 | false, false); |
| 461 | } |
| 462 | |
| 463 | return this->got_; |
| 464 | } |
| 465 | |
| 466 | // Get the dynamic reloc section, creating it if necessary. |
| 467 | |
| 468 | Target_x86_64::Reloc_section* |
| 469 | Target_x86_64::rela_dyn_section(Layout* layout) |
| 470 | { |
| 471 | if (this->rela_dyn_ == NULL) |
| 472 | { |
| 473 | gold_assert(layout != NULL); |
| 474 | this->rela_dyn_ = new Reloc_section(parameters->options().combreloc()); |
| 475 | layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA, |
| 476 | elfcpp::SHF_ALLOC, this->rela_dyn_); |
| 477 | } |
| 478 | return this->rela_dyn_; |
| 479 | } |
| 480 | |
| 481 | // A class to handle the PLT data. |
| 482 | |
| 483 | class Output_data_plt_x86_64 : public Output_section_data |
| 484 | { |
| 485 | public: |
| 486 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section; |
| 487 | |
| 488 | Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*, |
| 489 | Output_data_space*); |
| 490 | |
| 491 | // Add an entry to the PLT. |
| 492 | void |
| 493 | add_entry(Symbol* gsym); |
| 494 | |
| 495 | // Add the reserved TLSDESC_PLT entry to the PLT. |
| 496 | void |
| 497 | reserve_tlsdesc_entry(unsigned int got_offset) |
| 498 | { this->tlsdesc_got_offset_ = got_offset; } |
| 499 | |
| 500 | // Return true if a TLSDESC_PLT entry has been reserved. |
| 501 | bool |
| 502 | has_tlsdesc_entry() const |
| 503 | { return this->tlsdesc_got_offset_ != -1U; } |
| 504 | |
| 505 | // Return the GOT offset for the reserved TLSDESC_PLT entry. |
| 506 | unsigned int |
| 507 | get_tlsdesc_got_offset() const |
| 508 | { return this->tlsdesc_got_offset_; } |
| 509 | |
| 510 | // Return the offset of the reserved TLSDESC_PLT entry. |
| 511 | unsigned int |
| 512 | get_tlsdesc_plt_offset() const |
| 513 | { return (this->count_ + 1) * plt_entry_size; } |
| 514 | |
| 515 | // Return the .rel.plt section data. |
| 516 | const Reloc_section* |
| 517 | rel_plt() const |
| 518 | { return this->rel_; } |
| 519 | |
| 520 | protected: |
| 521 | void |
| 522 | do_adjust_output_section(Output_section* os); |
| 523 | |
| 524 | private: |
| 525 | // The size of an entry in the PLT. |
| 526 | static const int plt_entry_size = 16; |
| 527 | |
| 528 | // The first entry in the PLT. |
| 529 | // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same |
| 530 | // procedure linkage table for both programs and shared objects." |
| 531 | static unsigned char first_plt_entry[plt_entry_size]; |
| 532 | |
| 533 | // Other entries in the PLT for an executable. |
| 534 | static unsigned char plt_entry[plt_entry_size]; |
| 535 | |
| 536 | // The reserved TLSDESC entry in the PLT for an executable. |
| 537 | static unsigned char tlsdesc_plt_entry[plt_entry_size]; |
| 538 | |
| 539 | // Set the final size. |
| 540 | void |
| 541 | set_final_data_size(); |
| 542 | |
| 543 | // Write out the PLT data. |
| 544 | void |
| 545 | do_write(Output_file*); |
| 546 | |
| 547 | // The reloc section. |
| 548 | Reloc_section* rel_; |
| 549 | // The .got section. |
| 550 | Output_data_got<64, false>* got_; |
| 551 | // The .got.plt section. |
| 552 | Output_data_space* got_plt_; |
| 553 | // The number of PLT entries. |
| 554 | unsigned int count_; |
| 555 | // Offset of the reserved TLSDESC_GOT entry when needed. |
| 556 | unsigned int tlsdesc_got_offset_; |
| 557 | }; |
| 558 | |
| 559 | // Create the PLT section. The ordinary .got section is an argument, |
| 560 | // since we need to refer to the start. We also create our own .got |
| 561 | // section just for PLT entries. |
| 562 | |
| 563 | Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout, |
| 564 | Output_data_got<64, false>* got, |
| 565 | Output_data_space* got_plt) |
| 566 | : Output_section_data(8), got_(got), got_plt_(got_plt), count_(0), |
| 567 | tlsdesc_got_offset_(-1U) |
| 568 | { |
| 569 | this->rel_ = new Reloc_section(false); |
| 570 | layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, |
| 571 | elfcpp::SHF_ALLOC, this->rel_); |
| 572 | } |
| 573 | |
| 574 | void |
| 575 | Output_data_plt_x86_64::do_adjust_output_section(Output_section* os) |
| 576 | { |
| 577 | // UnixWare sets the entsize of .plt to 4, and so does the old GNU |
| 578 | // linker, and so do we. |
| 579 | os->set_entsize(4); |
| 580 | } |
| 581 | |
| 582 | // Add an entry to the PLT. |
| 583 | |
| 584 | void |
| 585 | Output_data_plt_x86_64::add_entry(Symbol* gsym) |
| 586 | { |
| 587 | gold_assert(!gsym->has_plt_offset()); |
| 588 | |
| 589 | // Note that when setting the PLT offset we skip the initial |
| 590 | // reserved PLT entry. |
| 591 | gsym->set_plt_offset((this->count_ + 1) * plt_entry_size); |
| 592 | |
| 593 | ++this->count_; |
| 594 | |
| 595 | section_offset_type got_offset = this->got_plt_->current_data_size(); |
| 596 | |
| 597 | // Every PLT entry needs a GOT entry which points back to the PLT |
| 598 | // entry (this will be changed by the dynamic linker, normally |
| 599 | // lazily when the function is called). |
| 600 | this->got_plt_->set_current_data_size(got_offset + 8); |
| 601 | |
| 602 | // Every PLT entry needs a reloc. |
| 603 | gsym->set_needs_dynsym_entry(); |
| 604 | this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_, |
| 605 | got_offset, 0); |
| 606 | |
| 607 | // Note that we don't need to save the symbol. The contents of the |
| 608 | // PLT are independent of which symbols are used. The symbols only |
| 609 | // appear in the relocations. |
| 610 | } |
| 611 | |
| 612 | // Set the final size. |
| 613 | void |
| 614 | Output_data_plt_x86_64::set_final_data_size() |
| 615 | { |
| 616 | unsigned int count = this->count_; |
| 617 | if (this->has_tlsdesc_entry()) |
| 618 | ++count; |
| 619 | this->set_data_size((count + 1) * plt_entry_size); |
| 620 | } |
| 621 | |
| 622 | // The first entry in the PLT for an executable. |
| 623 | |
| 624 | unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] = |
| 625 | { |
| 626 | // From AMD64 ABI Draft 0.98, page 76 |
| 627 | 0xff, 0x35, // pushq contents of memory address |
| 628 | 0, 0, 0, 0, // replaced with address of .got + 8 |
| 629 | 0xff, 0x25, // jmp indirect |
| 630 | 0, 0, 0, 0, // replaced with address of .got + 16 |
| 631 | 0x90, 0x90, 0x90, 0x90 // noop (x4) |
| 632 | }; |
| 633 | |
| 634 | // Subsequent entries in the PLT for an executable. |
| 635 | |
| 636 | unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] = |
| 637 | { |
| 638 | // From AMD64 ABI Draft 0.98, page 76 |
| 639 | 0xff, 0x25, // jmpq indirect |
| 640 | 0, 0, 0, 0, // replaced with address of symbol in .got |
| 641 | 0x68, // pushq immediate |
| 642 | 0, 0, 0, 0, // replaced with offset into relocation table |
| 643 | 0xe9, // jmpq relative |
| 644 | 0, 0, 0, 0 // replaced with offset to start of .plt |
| 645 | }; |
| 646 | |
| 647 | // The reserved TLSDESC entry in the PLT for an executable. |
| 648 | |
| 649 | unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] = |
| 650 | { |
| 651 | // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32 |
| 652 | // and AMD64/EM64T", Version 0.9.4 (2005-10-10). |
| 653 | 0xff, 0x35, // pushq x(%rip) |
| 654 | 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8) |
| 655 | 0xff, 0x25, // jmpq *y(%rip) |
| 656 | 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry |
| 657 | 0x0f, 0x1f, // nop |
| 658 | 0x40, 0 |
| 659 | }; |
| 660 | |
| 661 | // Write out the PLT. This uses the hand-coded instructions above, |
| 662 | // and adjusts them as needed. This is specified by the AMD64 ABI. |
| 663 | |
| 664 | void |
| 665 | Output_data_plt_x86_64::do_write(Output_file* of) |
| 666 | { |
| 667 | const off_t offset = this->offset(); |
| 668 | const section_size_type oview_size = |
| 669 | convert_to_section_size_type(this->data_size()); |
| 670 | unsigned char* const oview = of->get_output_view(offset, oview_size); |
| 671 | |
| 672 | const off_t got_file_offset = this->got_plt_->offset(); |
| 673 | const section_size_type got_size = |
| 674 | convert_to_section_size_type(this->got_plt_->data_size()); |
| 675 | unsigned char* const got_view = of->get_output_view(got_file_offset, |
| 676 | got_size); |
| 677 | |
| 678 | unsigned char* pov = oview; |
| 679 | |
| 680 | // The base address of the .plt section. |
| 681 | elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address(); |
| 682 | // The base address of the .got section. |
| 683 | elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address(); |
| 684 | // The base address of the PLT portion of the .got section, |
| 685 | // which is where the GOT pointer will point, and where the |
| 686 | // three reserved GOT entries are located. |
| 687 | elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address(); |
| 688 | |
| 689 | memcpy(pov, first_plt_entry, plt_entry_size); |
| 690 | // We do a jmp relative to the PC at the end of this instruction. |
| 691 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, |
| 692 | (got_address + 8 |
| 693 | - (plt_address + 6))); |
| 694 | elfcpp::Swap<32, false>::writeval(pov + 8, |
| 695 | (got_address + 16 |
| 696 | - (plt_address + 12))); |
| 697 | pov += plt_entry_size; |
| 698 | |
| 699 | unsigned char* got_pov = got_view; |
| 700 | |
| 701 | memset(got_pov, 0, 24); |
| 702 | got_pov += 24; |
| 703 | |
| 704 | unsigned int plt_offset = plt_entry_size; |
| 705 | unsigned int got_offset = 24; |
| 706 | const unsigned int count = this->count_; |
| 707 | for (unsigned int plt_index = 0; |
| 708 | plt_index < count; |
| 709 | ++plt_index, |
| 710 | pov += plt_entry_size, |
| 711 | got_pov += 8, |
| 712 | plt_offset += plt_entry_size, |
| 713 | got_offset += 8) |
| 714 | { |
| 715 | // Set and adjust the PLT entry itself. |
| 716 | memcpy(pov, plt_entry, plt_entry_size); |
| 717 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, |
| 718 | (got_address + got_offset |
| 719 | - (plt_address + plt_offset |
| 720 | + 6))); |
| 721 | |
| 722 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index); |
| 723 | elfcpp::Swap<32, false>::writeval(pov + 12, |
| 724 | - (plt_offset + plt_entry_size)); |
| 725 | |
| 726 | // Set the entry in the GOT. |
| 727 | elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6); |
| 728 | } |
| 729 | |
| 730 | if (this->has_tlsdesc_entry()) |
| 731 | { |
| 732 | // Set and adjust the reserved TLSDESC PLT entry. |
| 733 | unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset(); |
| 734 | memcpy(pov, tlsdesc_plt_entry, plt_entry_size); |
| 735 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, |
| 736 | (got_address + 8 |
| 737 | - (plt_address + plt_offset |
| 738 | + 6))); |
| 739 | elfcpp::Swap_unaligned<32, false>::writeval(pov + 8, |
| 740 | (got_base |
| 741 | + tlsdesc_got_offset |
| 742 | - (plt_address + plt_offset |
| 743 | + 12))); |
| 744 | pov += plt_entry_size; |
| 745 | } |
| 746 | |
| 747 | gold_assert(static_cast<section_size_type>(pov - oview) == oview_size); |
| 748 | gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size); |
| 749 | |
| 750 | of->write_output_view(offset, oview_size, oview); |
| 751 | of->write_output_view(got_file_offset, got_size, got_view); |
| 752 | } |
| 753 | |
| 754 | // Create the PLT section. |
| 755 | |
| 756 | void |
| 757 | Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout) |
| 758 | { |
| 759 | if (this->plt_ == NULL) |
| 760 | { |
| 761 | // Create the GOT sections first. |
| 762 | this->got_section(symtab, layout); |
| 763 | |
| 764 | this->plt_ = new Output_data_plt_x86_64(layout, this->got_, |
| 765 | this->got_plt_); |
| 766 | layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, |
| 767 | (elfcpp::SHF_ALLOC |
| 768 | | elfcpp::SHF_EXECINSTR), |
| 769 | this->plt_); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | // Create a PLT entry for a global symbol. |
| 774 | |
| 775 | void |
| 776 | Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout, |
| 777 | Symbol* gsym) |
| 778 | { |
| 779 | if (gsym->has_plt_offset()) |
| 780 | return; |
| 781 | |
| 782 | if (this->plt_ == NULL) |
| 783 | this->make_plt_section(symtab, layout); |
| 784 | |
| 785 | this->plt_->add_entry(gsym); |
| 786 | } |
| 787 | |
| 788 | // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment. |
| 789 | |
| 790 | void |
| 791 | Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout) |
| 792 | { |
| 793 | if (this->tls_base_symbol_defined_) |
| 794 | return; |
| 795 | |
| 796 | Output_segment* tls_segment = layout->tls_segment(); |
| 797 | if (tls_segment != NULL) |
| 798 | { |
| 799 | symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL, |
| 800 | tls_segment, 0, 0, |
| 801 | elfcpp::STT_TLS, |
| 802 | elfcpp::STB_LOCAL, |
| 803 | elfcpp::STV_HIDDEN, 0, |
| 804 | Symbol::SEGMENT_END, true); |
| 805 | } |
| 806 | this->tls_base_symbol_defined_ = true; |
| 807 | } |
| 808 | |
| 809 | // Create the reserved PLT and GOT entries for the TLS descriptor resolver. |
| 810 | |
| 811 | void |
| 812 | Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab, |
| 813 | Layout* layout) |
| 814 | { |
| 815 | if (this->plt_ == NULL) |
| 816 | this->make_plt_section(symtab, layout); |
| 817 | |
| 818 | if (!this->plt_->has_tlsdesc_entry()) |
| 819 | { |
| 820 | // Allocate the TLSDESC_GOT entry. |
| 821 | Output_data_got<64, false>* got = this->got_section(symtab, layout); |
| 822 | unsigned int got_offset = got->add_constant(0); |
| 823 | |
| 824 | // Allocate the TLSDESC_PLT entry. |
| 825 | this->plt_->reserve_tlsdesc_entry(got_offset); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | // Create a GOT entry for the TLS module index. |
| 830 | |
| 831 | unsigned int |
| 832 | Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout, |
| 833 | Sized_relobj<64, false>* object) |
| 834 | { |
| 835 | if (this->got_mod_index_offset_ == -1U) |
| 836 | { |
| 837 | gold_assert(symtab != NULL && layout != NULL && object != NULL); |
| 838 | Reloc_section* rela_dyn = this->rela_dyn_section(layout); |
| 839 | Output_data_got<64, false>* got = this->got_section(symtab, layout); |
| 840 | unsigned int got_offset = got->add_constant(0); |
| 841 | rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got, |
| 842 | got_offset, 0); |
| 843 | got->add_constant(0); |
| 844 | this->got_mod_index_offset_ = got_offset; |
| 845 | } |
| 846 | return this->got_mod_index_offset_; |
| 847 | } |
| 848 | |
| 849 | // Optimize the TLS relocation type based on what we know about the |
| 850 | // symbol. IS_FINAL is true if the final address of this symbol is |
| 851 | // known at link time. |
| 852 | |
| 853 | tls::Tls_optimization |
| 854 | Target_x86_64::optimize_tls_reloc(bool is_final, int r_type) |
| 855 | { |
| 856 | // If we are generating a shared library, then we can't do anything |
| 857 | // in the linker. |
| 858 | if (parameters->options().shared()) |
| 859 | return tls::TLSOPT_NONE; |
| 860 | |
| 861 | switch (r_type) |
| 862 | { |
| 863 | case elfcpp::R_X86_64_TLSGD: |
| 864 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: |
| 865 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 866 | // These are General-Dynamic which permits fully general TLS |
| 867 | // access. Since we know that we are generating an executable, |
| 868 | // we can convert this to Initial-Exec. If we also know that |
| 869 | // this is a local symbol, we can further switch to Local-Exec. |
| 870 | if (is_final) |
| 871 | return tls::TLSOPT_TO_LE; |
| 872 | return tls::TLSOPT_TO_IE; |
| 873 | |
| 874 | case elfcpp::R_X86_64_TLSLD: |
| 875 | // This is Local-Dynamic, which refers to a local symbol in the |
| 876 | // dynamic TLS block. Since we know that we generating an |
| 877 | // executable, we can switch to Local-Exec. |
| 878 | return tls::TLSOPT_TO_LE; |
| 879 | |
| 880 | case elfcpp::R_X86_64_DTPOFF32: |
| 881 | case elfcpp::R_X86_64_DTPOFF64: |
| 882 | // Another Local-Dynamic reloc. |
| 883 | return tls::TLSOPT_TO_LE; |
| 884 | |
| 885 | case elfcpp::R_X86_64_GOTTPOFF: |
| 886 | // These are Initial-Exec relocs which get the thread offset |
| 887 | // from the GOT. If we know that we are linking against the |
| 888 | // local symbol, we can switch to Local-Exec, which links the |
| 889 | // thread offset into the instruction. |
| 890 | if (is_final) |
| 891 | return tls::TLSOPT_TO_LE; |
| 892 | return tls::TLSOPT_NONE; |
| 893 | |
| 894 | case elfcpp::R_X86_64_TPOFF32: |
| 895 | // When we already have Local-Exec, there is nothing further we |
| 896 | // can do. |
| 897 | return tls::TLSOPT_NONE; |
| 898 | |
| 899 | default: |
| 900 | gold_unreachable(); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | // Report an unsupported relocation against a local symbol. |
| 905 | |
| 906 | void |
| 907 | Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object, |
| 908 | unsigned int r_type) |
| 909 | { |
| 910 | gold_error(_("%s: unsupported reloc %u against local symbol"), |
| 911 | object->name().c_str(), r_type); |
| 912 | } |
| 913 | |
| 914 | // We are about to emit a dynamic relocation of type R_TYPE. If the |
| 915 | // dynamic linker does not support it, issue an error. The GNU linker |
| 916 | // only issues a non-PIC error for an allocated read-only section. |
| 917 | // Here we know the section is allocated, but we don't know that it is |
| 918 | // read-only. But we check for all the relocation types which the |
| 919 | // glibc dynamic linker supports, so it seems appropriate to issue an |
| 920 | // error even if the section is not read-only. |
| 921 | |
| 922 | void |
| 923 | Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type) |
| 924 | { |
| 925 | switch (r_type) |
| 926 | { |
| 927 | // These are the relocation types supported by glibc for x86_64. |
| 928 | case elfcpp::R_X86_64_RELATIVE: |
| 929 | case elfcpp::R_X86_64_GLOB_DAT: |
| 930 | case elfcpp::R_X86_64_JUMP_SLOT: |
| 931 | case elfcpp::R_X86_64_DTPMOD64: |
| 932 | case elfcpp::R_X86_64_DTPOFF64: |
| 933 | case elfcpp::R_X86_64_TPOFF64: |
| 934 | case elfcpp::R_X86_64_64: |
| 935 | case elfcpp::R_X86_64_32: |
| 936 | case elfcpp::R_X86_64_PC32: |
| 937 | case elfcpp::R_X86_64_COPY: |
| 938 | return; |
| 939 | |
| 940 | default: |
| 941 | // This prevents us from issuing more than one error per reloc |
| 942 | // section. But we can still wind up issuing more than one |
| 943 | // error per object file. |
| 944 | if (this->issued_non_pic_error_) |
| 945 | return; |
| 946 | object->error(_("requires unsupported dynamic reloc; " |
| 947 | "recompile with -fPIC")); |
| 948 | this->issued_non_pic_error_ = true; |
| 949 | return; |
| 950 | |
| 951 | case elfcpp::R_X86_64_NONE: |
| 952 | gold_unreachable(); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | // Scan a relocation for a local symbol. |
| 957 | |
| 958 | inline void |
| 959 | Target_x86_64::Scan::local(const General_options&, |
| 960 | Symbol_table* symtab, |
| 961 | Layout* layout, |
| 962 | Target_x86_64* target, |
| 963 | Sized_relobj<64, false>* object, |
| 964 | unsigned int data_shndx, |
| 965 | Output_section* output_section, |
| 966 | const elfcpp::Rela<64, false>& reloc, |
| 967 | unsigned int r_type, |
| 968 | const elfcpp::Sym<64, false>& lsym) |
| 969 | { |
| 970 | switch (r_type) |
| 971 | { |
| 972 | case elfcpp::R_X86_64_NONE: |
| 973 | case elfcpp::R_386_GNU_VTINHERIT: |
| 974 | case elfcpp::R_386_GNU_VTENTRY: |
| 975 | break; |
| 976 | |
| 977 | case elfcpp::R_X86_64_64: |
| 978 | // If building a shared library (or a position-independent |
| 979 | // executable), we need to create a dynamic relocation for this |
| 980 | // location. The relocation applied at link time will apply the |
| 981 | // link-time value, so we flag the location with an |
| 982 | // R_X86_64_RELATIVE relocation so the dynamic loader can |
| 983 | // relocate it easily. |
| 984 | if (parameters->options().output_is_position_independent()) |
| 985 | { |
| 986 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
| 987 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
| 988 | rela_dyn->add_local_relative(object, r_sym, |
| 989 | elfcpp::R_X86_64_RELATIVE, |
| 990 | output_section, data_shndx, |
| 991 | reloc.get_r_offset(), |
| 992 | reloc.get_r_addend()); |
| 993 | } |
| 994 | break; |
| 995 | |
| 996 | case elfcpp::R_X86_64_32: |
| 997 | case elfcpp::R_X86_64_32S: |
| 998 | case elfcpp::R_X86_64_16: |
| 999 | case elfcpp::R_X86_64_8: |
| 1000 | // If building a shared library (or a position-independent |
| 1001 | // executable), we need to create a dynamic relocation for this |
| 1002 | // location. We can't use an R_X86_64_RELATIVE relocation |
| 1003 | // because that is always a 64-bit relocation. |
| 1004 | if (parameters->options().output_is_position_independent()) |
| 1005 | { |
| 1006 | this->check_non_pic(object, r_type); |
| 1007 | |
| 1008 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
| 1009 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
| 1010 | if (lsym.get_st_type() != elfcpp::STT_SECTION) |
| 1011 | rela_dyn->add_local(object, r_sym, r_type, output_section, |
| 1012 | data_shndx, reloc.get_r_offset(), |
| 1013 | reloc.get_r_addend()); |
| 1014 | else |
| 1015 | { |
| 1016 | gold_assert(lsym.get_st_value() == 0); |
| 1017 | unsigned int shndx = lsym.get_st_shndx(); |
| 1018 | bool is_ordinary; |
| 1019 | shndx = object->adjust_sym_shndx(r_sym, shndx, |
| 1020 | &is_ordinary); |
| 1021 | if (!is_ordinary) |
| 1022 | object->error(_("section symbol %u has bad shndx %u"), |
| 1023 | r_sym, shndx); |
| 1024 | else |
| 1025 | rela_dyn->add_local_section(object, shndx, |
| 1026 | r_type, output_section, |
| 1027 | data_shndx, reloc.get_r_offset(), |
| 1028 | reloc.get_r_addend()); |
| 1029 | } |
| 1030 | } |
| 1031 | break; |
| 1032 | |
| 1033 | case elfcpp::R_X86_64_PC64: |
| 1034 | case elfcpp::R_X86_64_PC32: |
| 1035 | case elfcpp::R_X86_64_PC16: |
| 1036 | case elfcpp::R_X86_64_PC8: |
| 1037 | break; |
| 1038 | |
| 1039 | case elfcpp::R_X86_64_PLT32: |
| 1040 | // Since we know this is a local symbol, we can handle this as a |
| 1041 | // PC32 reloc. |
| 1042 | break; |
| 1043 | |
| 1044 | case elfcpp::R_X86_64_GOTPC32: |
| 1045 | case elfcpp::R_X86_64_GOTOFF64: |
| 1046 | case elfcpp::R_X86_64_GOTPC64: |
| 1047 | case elfcpp::R_X86_64_PLTOFF64: |
| 1048 | // We need a GOT section. |
| 1049 | target->got_section(symtab, layout); |
| 1050 | // For PLTOFF64, we'd normally want a PLT section, but since we |
| 1051 | // know this is a local symbol, no PLT is needed. |
| 1052 | break; |
| 1053 | |
| 1054 | case elfcpp::R_X86_64_GOT64: |
| 1055 | case elfcpp::R_X86_64_GOT32: |
| 1056 | case elfcpp::R_X86_64_GOTPCREL64: |
| 1057 | case elfcpp::R_X86_64_GOTPCREL: |
| 1058 | case elfcpp::R_X86_64_GOTPLT64: |
| 1059 | { |
| 1060 | // The symbol requires a GOT entry. |
| 1061 | Output_data_got<64, false>* got = target->got_section(symtab, layout); |
| 1062 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
| 1063 | if (got->add_local(object, r_sym, GOT_TYPE_STANDARD)) |
| 1064 | { |
| 1065 | // If we are generating a shared object, we need to add a |
| 1066 | // dynamic relocation for this symbol's GOT entry. |
| 1067 | if (parameters->options().output_is_position_independent()) |
| 1068 | { |
| 1069 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
| 1070 | // R_X86_64_RELATIVE assumes a 64-bit relocation. |
| 1071 | if (r_type != elfcpp::R_X86_64_GOT32) |
| 1072 | rela_dyn->add_local_relative( |
| 1073 | object, r_sym, elfcpp::R_X86_64_RELATIVE, got, |
| 1074 | object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0); |
| 1075 | else |
| 1076 | { |
| 1077 | this->check_non_pic(object, r_type); |
| 1078 | |
| 1079 | gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION); |
| 1080 | rela_dyn->add_local( |
| 1081 | object, r_sym, r_type, got, |
| 1082 | object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0); |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | // For GOTPLT64, we'd normally want a PLT section, but since |
| 1087 | // we know this is a local symbol, no PLT is needed. |
| 1088 | } |
| 1089 | break; |
| 1090 | |
| 1091 | case elfcpp::R_X86_64_COPY: |
| 1092 | case elfcpp::R_X86_64_GLOB_DAT: |
| 1093 | case elfcpp::R_X86_64_JUMP_SLOT: |
| 1094 | case elfcpp::R_X86_64_RELATIVE: |
| 1095 | // These are outstanding tls relocs, which are unexpected when linking |
| 1096 | case elfcpp::R_X86_64_TPOFF64: |
| 1097 | case elfcpp::R_X86_64_DTPMOD64: |
| 1098 | case elfcpp::R_X86_64_TLSDESC: |
| 1099 | gold_error(_("%s: unexpected reloc %u in object file"), |
| 1100 | object->name().c_str(), r_type); |
| 1101 | break; |
| 1102 | |
| 1103 | // These are initial tls relocs, which are expected when linking |
| 1104 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
| 1105 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) |
| 1106 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 1107 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 1108 | case elfcpp::R_X86_64_DTPOFF32: |
| 1109 | case elfcpp::R_X86_64_DTPOFF64: |
| 1110 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 1111 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 1112 | { |
| 1113 | bool output_is_shared = parameters->options().shared(); |
| 1114 | const tls::Tls_optimization optimized_type |
| 1115 | = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type); |
| 1116 | switch (r_type) |
| 1117 | { |
| 1118 | case elfcpp::R_X86_64_TLSGD: // General-dynamic |
| 1119 | if (optimized_type == tls::TLSOPT_NONE) |
| 1120 | { |
| 1121 | // Create a pair of GOT entries for the module index and |
| 1122 | // dtv-relative offset. |
| 1123 | Output_data_got<64, false>* got |
| 1124 | = target->got_section(symtab, layout); |
| 1125 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
| 1126 | unsigned int shndx = lsym.get_st_shndx(); |
| 1127 | bool is_ordinary; |
| 1128 | shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); |
| 1129 | if (!is_ordinary) |
| 1130 | object->error(_("local symbol %u has bad shndx %u"), |
| 1131 | r_sym, shndx); |
| 1132 | else |
| 1133 | got->add_local_pair_with_rela(object, r_sym, |
| 1134 | shndx, |
| 1135 | GOT_TYPE_TLS_PAIR, |
| 1136 | target->rela_dyn_section(layout), |
| 1137 | elfcpp::R_X86_64_DTPMOD64, 0); |
| 1138 | } |
| 1139 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 1140 | unsupported_reloc_local(object, r_type); |
| 1141 | break; |
| 1142 | |
| 1143 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: |
| 1144 | target->define_tls_base_symbol(symtab, layout); |
| 1145 | if (optimized_type == tls::TLSOPT_NONE) |
| 1146 | { |
| 1147 | // Create reserved PLT and GOT entries for the resolver. |
| 1148 | target->reserve_tlsdesc_entries(symtab, layout); |
| 1149 | |
| 1150 | // Generate a double GOT entry with an R_X86_64_TLSDESC reloc. |
| 1151 | Output_data_got<64, false>* got |
| 1152 | = target->got_section(symtab, layout); |
| 1153 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
| 1154 | unsigned int shndx = lsym.get_st_shndx(); |
| 1155 | bool is_ordinary; |
| 1156 | shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); |
| 1157 | if (!is_ordinary) |
| 1158 | object->error(_("local symbol %u has bad shndx %u"), |
| 1159 | r_sym, shndx); |
| 1160 | else |
| 1161 | got->add_local_pair_with_rela(object, r_sym, |
| 1162 | shndx, |
| 1163 | GOT_TYPE_TLS_DESC, |
| 1164 | target->rela_dyn_section(layout), |
| 1165 | elfcpp::R_X86_64_TLSDESC, 0); |
| 1166 | } |
| 1167 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 1168 | unsupported_reloc_local(object, r_type); |
| 1169 | break; |
| 1170 | |
| 1171 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 1172 | break; |
| 1173 | |
| 1174 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 1175 | if (optimized_type == tls::TLSOPT_NONE) |
| 1176 | { |
| 1177 | // Create a GOT entry for the module index. |
| 1178 | target->got_mod_index_entry(symtab, layout, object); |
| 1179 | } |
| 1180 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 1181 | unsupported_reloc_local(object, r_type); |
| 1182 | break; |
| 1183 | |
| 1184 | case elfcpp::R_X86_64_DTPOFF32: |
| 1185 | case elfcpp::R_X86_64_DTPOFF64: |
| 1186 | break; |
| 1187 | |
| 1188 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 1189 | layout->set_has_static_tls(); |
| 1190 | if (optimized_type == tls::TLSOPT_NONE) |
| 1191 | { |
| 1192 | // Create a GOT entry for the tp-relative offset. |
| 1193 | Output_data_got<64, false>* got |
| 1194 | = target->got_section(symtab, layout); |
| 1195 | unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info()); |
| 1196 | got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET, |
| 1197 | target->rela_dyn_section(layout), |
| 1198 | elfcpp::R_X86_64_TPOFF64); |
| 1199 | } |
| 1200 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 1201 | unsupported_reloc_local(object, r_type); |
| 1202 | break; |
| 1203 | |
| 1204 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 1205 | layout->set_has_static_tls(); |
| 1206 | if (output_is_shared) |
| 1207 | unsupported_reloc_local(object, r_type); |
| 1208 | break; |
| 1209 | |
| 1210 | default: |
| 1211 | gold_unreachable(); |
| 1212 | } |
| 1213 | } |
| 1214 | break; |
| 1215 | |
| 1216 | case elfcpp::R_X86_64_SIZE32: |
| 1217 | case elfcpp::R_X86_64_SIZE64: |
| 1218 | default: |
| 1219 | gold_error(_("%s: unsupported reloc %u against local symbol"), |
| 1220 | object->name().c_str(), r_type); |
| 1221 | break; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | |
| 1226 | // Report an unsupported relocation against a global symbol. |
| 1227 | |
| 1228 | void |
| 1229 | Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object, |
| 1230 | unsigned int r_type, |
| 1231 | Symbol* gsym) |
| 1232 | { |
| 1233 | gold_error(_("%s: unsupported reloc %u against global symbol %s"), |
| 1234 | object->name().c_str(), r_type, gsym->demangled_name().c_str()); |
| 1235 | } |
| 1236 | |
| 1237 | // Scan a relocation for a global symbol. |
| 1238 | |
| 1239 | inline void |
| 1240 | Target_x86_64::Scan::global(const General_options&, |
| 1241 | Symbol_table* symtab, |
| 1242 | Layout* layout, |
| 1243 | Target_x86_64* target, |
| 1244 | Sized_relobj<64, false>* object, |
| 1245 | unsigned int data_shndx, |
| 1246 | Output_section* output_section, |
| 1247 | const elfcpp::Rela<64, false>& reloc, |
| 1248 | unsigned int r_type, |
| 1249 | Symbol* gsym) |
| 1250 | { |
| 1251 | switch (r_type) |
| 1252 | { |
| 1253 | case elfcpp::R_X86_64_NONE: |
| 1254 | case elfcpp::R_386_GNU_VTINHERIT: |
| 1255 | case elfcpp::R_386_GNU_VTENTRY: |
| 1256 | break; |
| 1257 | |
| 1258 | case elfcpp::R_X86_64_64: |
| 1259 | case elfcpp::R_X86_64_32: |
| 1260 | case elfcpp::R_X86_64_32S: |
| 1261 | case elfcpp::R_X86_64_16: |
| 1262 | case elfcpp::R_X86_64_8: |
| 1263 | { |
| 1264 | // Make a PLT entry if necessary. |
| 1265 | if (gsym->needs_plt_entry()) |
| 1266 | { |
| 1267 | target->make_plt_entry(symtab, layout, gsym); |
| 1268 | // Since this is not a PC-relative relocation, we may be |
| 1269 | // taking the address of a function. In that case we need to |
| 1270 | // set the entry in the dynamic symbol table to the address of |
| 1271 | // the PLT entry. |
| 1272 | if (gsym->is_from_dynobj() && !parameters->options().shared()) |
| 1273 | gsym->set_needs_dynsym_value(); |
| 1274 | } |
| 1275 | // Make a dynamic relocation if necessary. |
| 1276 | if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF)) |
| 1277 | { |
| 1278 | if (target->may_need_copy_reloc(gsym)) |
| 1279 | { |
| 1280 | target->copy_reloc(symtab, layout, object, |
| 1281 | data_shndx, output_section, gsym, reloc); |
| 1282 | } |
| 1283 | else if (r_type == elfcpp::R_X86_64_64 |
| 1284 | && gsym->can_use_relative_reloc(false)) |
| 1285 | { |
| 1286 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
| 1287 | rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE, |
| 1288 | output_section, object, |
| 1289 | data_shndx, reloc.get_r_offset(), |
| 1290 | reloc.get_r_addend()); |
| 1291 | } |
| 1292 | else |
| 1293 | { |
| 1294 | this->check_non_pic(object, r_type); |
| 1295 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
| 1296 | rela_dyn->add_global(gsym, r_type, output_section, object, |
| 1297 | data_shndx, reloc.get_r_offset(), |
| 1298 | reloc.get_r_addend()); |
| 1299 | } |
| 1300 | } |
| 1301 | } |
| 1302 | break; |
| 1303 | |
| 1304 | case elfcpp::R_X86_64_PC64: |
| 1305 | case elfcpp::R_X86_64_PC32: |
| 1306 | case elfcpp::R_X86_64_PC16: |
| 1307 | case elfcpp::R_X86_64_PC8: |
| 1308 | { |
| 1309 | // Make a PLT entry if necessary. |
| 1310 | if (gsym->needs_plt_entry()) |
| 1311 | target->make_plt_entry(symtab, layout, gsym); |
| 1312 | // Make a dynamic relocation if necessary. |
| 1313 | int flags = Symbol::NON_PIC_REF; |
| 1314 | if (gsym->type() == elfcpp::STT_FUNC) |
| 1315 | flags |= Symbol::FUNCTION_CALL; |
| 1316 | if (gsym->needs_dynamic_reloc(flags)) |
| 1317 | { |
| 1318 | if (target->may_need_copy_reloc(gsym)) |
| 1319 | { |
| 1320 | target->copy_reloc(symtab, layout, object, |
| 1321 | data_shndx, output_section, gsym, reloc); |
| 1322 | } |
| 1323 | else |
| 1324 | { |
| 1325 | this->check_non_pic(object, r_type); |
| 1326 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
| 1327 | rela_dyn->add_global(gsym, r_type, output_section, object, |
| 1328 | data_shndx, reloc.get_r_offset(), |
| 1329 | reloc.get_r_addend()); |
| 1330 | } |
| 1331 | } |
| 1332 | } |
| 1333 | break; |
| 1334 | |
| 1335 | case elfcpp::R_X86_64_GOT64: |
| 1336 | case elfcpp::R_X86_64_GOT32: |
| 1337 | case elfcpp::R_X86_64_GOTPCREL64: |
| 1338 | case elfcpp::R_X86_64_GOTPCREL: |
| 1339 | case elfcpp::R_X86_64_GOTPLT64: |
| 1340 | { |
| 1341 | // The symbol requires a GOT entry. |
| 1342 | Output_data_got<64, false>* got = target->got_section(symtab, layout); |
| 1343 | if (gsym->final_value_is_known()) |
| 1344 | got->add_global(gsym, GOT_TYPE_STANDARD); |
| 1345 | else |
| 1346 | { |
| 1347 | // If this symbol is not fully resolved, we need to add a |
| 1348 | // dynamic relocation for it. |
| 1349 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); |
| 1350 | if (gsym->is_from_dynobj() |
| 1351 | || gsym->is_undefined() |
| 1352 | || gsym->is_preemptible()) |
| 1353 | got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn, |
| 1354 | elfcpp::R_X86_64_GLOB_DAT); |
| 1355 | else |
| 1356 | { |
| 1357 | if (got->add_global(gsym, GOT_TYPE_STANDARD)) |
| 1358 | rela_dyn->add_global_relative( |
| 1359 | gsym, elfcpp::R_X86_64_RELATIVE, got, |
| 1360 | gsym->got_offset(GOT_TYPE_STANDARD), 0); |
| 1361 | } |
| 1362 | } |
| 1363 | // For GOTPLT64, we also need a PLT entry (but only if the |
| 1364 | // symbol is not fully resolved). |
| 1365 | if (r_type == elfcpp::R_X86_64_GOTPLT64 |
| 1366 | && !gsym->final_value_is_known()) |
| 1367 | target->make_plt_entry(symtab, layout, gsym); |
| 1368 | } |
| 1369 | break; |
| 1370 | |
| 1371 | case elfcpp::R_X86_64_PLT32: |
| 1372 | // If the symbol is fully resolved, this is just a PC32 reloc. |
| 1373 | // Otherwise we need a PLT entry. |
| 1374 | if (gsym->final_value_is_known()) |
| 1375 | break; |
| 1376 | // If building a shared library, we can also skip the PLT entry |
| 1377 | // if the symbol is defined in the output file and is protected |
| 1378 | // or hidden. |
| 1379 | if (gsym->is_defined() |
| 1380 | && !gsym->is_from_dynobj() |
| 1381 | && !gsym->is_preemptible()) |
| 1382 | break; |
| 1383 | target->make_plt_entry(symtab, layout, gsym); |
| 1384 | break; |
| 1385 | |
| 1386 | case elfcpp::R_X86_64_GOTPC32: |
| 1387 | case elfcpp::R_X86_64_GOTOFF64: |
| 1388 | case elfcpp::R_X86_64_GOTPC64: |
| 1389 | case elfcpp::R_X86_64_PLTOFF64: |
| 1390 | // We need a GOT section. |
| 1391 | target->got_section(symtab, layout); |
| 1392 | // For PLTOFF64, we also need a PLT entry (but only if the |
| 1393 | // symbol is not fully resolved). |
| 1394 | if (r_type == elfcpp::R_X86_64_PLTOFF64 |
| 1395 | && !gsym->final_value_is_known()) |
| 1396 | target->make_plt_entry(symtab, layout, gsym); |
| 1397 | break; |
| 1398 | |
| 1399 | case elfcpp::R_X86_64_COPY: |
| 1400 | case elfcpp::R_X86_64_GLOB_DAT: |
| 1401 | case elfcpp::R_X86_64_JUMP_SLOT: |
| 1402 | case elfcpp::R_X86_64_RELATIVE: |
| 1403 | // These are outstanding tls relocs, which are unexpected when linking |
| 1404 | case elfcpp::R_X86_64_TPOFF64: |
| 1405 | case elfcpp::R_X86_64_DTPMOD64: |
| 1406 | case elfcpp::R_X86_64_TLSDESC: |
| 1407 | gold_error(_("%s: unexpected reloc %u in object file"), |
| 1408 | object->name().c_str(), r_type); |
| 1409 | break; |
| 1410 | |
| 1411 | // These are initial tls relocs, which are expected for global() |
| 1412 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
| 1413 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) |
| 1414 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 1415 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 1416 | case elfcpp::R_X86_64_DTPOFF32: |
| 1417 | case elfcpp::R_X86_64_DTPOFF64: |
| 1418 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 1419 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 1420 | { |
| 1421 | const bool is_final = gsym->final_value_is_known(); |
| 1422 | const tls::Tls_optimization optimized_type |
| 1423 | = Target_x86_64::optimize_tls_reloc(is_final, r_type); |
| 1424 | switch (r_type) |
| 1425 | { |
| 1426 | case elfcpp::R_X86_64_TLSGD: // General-dynamic |
| 1427 | if (optimized_type == tls::TLSOPT_NONE) |
| 1428 | { |
| 1429 | // Create a pair of GOT entries for the module index and |
| 1430 | // dtv-relative offset. |
| 1431 | Output_data_got<64, false>* got |
| 1432 | = target->got_section(symtab, layout); |
| 1433 | got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR, |
| 1434 | target->rela_dyn_section(layout), |
| 1435 | elfcpp::R_X86_64_DTPMOD64, |
| 1436 | elfcpp::R_X86_64_DTPOFF64); |
| 1437 | } |
| 1438 | else if (optimized_type == tls::TLSOPT_TO_IE) |
| 1439 | { |
| 1440 | // Create a GOT entry for the tp-relative offset. |
| 1441 | Output_data_got<64, false>* got |
| 1442 | = target->got_section(symtab, layout); |
| 1443 | got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET, |
| 1444 | target->rela_dyn_section(layout), |
| 1445 | elfcpp::R_X86_64_TPOFF64); |
| 1446 | } |
| 1447 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 1448 | unsupported_reloc_global(object, r_type, gsym); |
| 1449 | break; |
| 1450 | |
| 1451 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: |
| 1452 | target->define_tls_base_symbol(symtab, layout); |
| 1453 | if (optimized_type == tls::TLSOPT_NONE) |
| 1454 | { |
| 1455 | // Create reserved PLT and GOT entries for the resolver. |
| 1456 | target->reserve_tlsdesc_entries(symtab, layout); |
| 1457 | |
| 1458 | // Create a double GOT entry with an R_X86_64_TLSDESC reloc. |
| 1459 | Output_data_got<64, false>* got |
| 1460 | = target->got_section(symtab, layout); |
| 1461 | got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, |
| 1462 | target->rela_dyn_section(layout), |
| 1463 | elfcpp::R_X86_64_TLSDESC, 0); |
| 1464 | } |
| 1465 | else if (optimized_type == tls::TLSOPT_TO_IE) |
| 1466 | { |
| 1467 | // Create a GOT entry for the tp-relative offset. |
| 1468 | Output_data_got<64, false>* got |
| 1469 | = target->got_section(symtab, layout); |
| 1470 | got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET, |
| 1471 | target->rela_dyn_section(layout), |
| 1472 | elfcpp::R_X86_64_TPOFF64); |
| 1473 | } |
| 1474 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 1475 | unsupported_reloc_global(object, r_type, gsym); |
| 1476 | break; |
| 1477 | |
| 1478 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 1479 | break; |
| 1480 | |
| 1481 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 1482 | if (optimized_type == tls::TLSOPT_NONE) |
| 1483 | { |
| 1484 | // Create a GOT entry for the module index. |
| 1485 | target->got_mod_index_entry(symtab, layout, object); |
| 1486 | } |
| 1487 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 1488 | unsupported_reloc_global(object, r_type, gsym); |
| 1489 | break; |
| 1490 | |
| 1491 | case elfcpp::R_X86_64_DTPOFF32: |
| 1492 | case elfcpp::R_X86_64_DTPOFF64: |
| 1493 | break; |
| 1494 | |
| 1495 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 1496 | layout->set_has_static_tls(); |
| 1497 | if (optimized_type == tls::TLSOPT_NONE) |
| 1498 | { |
| 1499 | // Create a GOT entry for the tp-relative offset. |
| 1500 | Output_data_got<64, false>* got |
| 1501 | = target->got_section(symtab, layout); |
| 1502 | got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET, |
| 1503 | target->rela_dyn_section(layout), |
| 1504 | elfcpp::R_X86_64_TPOFF64); |
| 1505 | } |
| 1506 | else if (optimized_type != tls::TLSOPT_TO_LE) |
| 1507 | unsupported_reloc_global(object, r_type, gsym); |
| 1508 | break; |
| 1509 | |
| 1510 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 1511 | layout->set_has_static_tls(); |
| 1512 | if (parameters->options().shared()) |
| 1513 | unsupported_reloc_local(object, r_type); |
| 1514 | break; |
| 1515 | |
| 1516 | default: |
| 1517 | gold_unreachable(); |
| 1518 | } |
| 1519 | } |
| 1520 | break; |
| 1521 | |
| 1522 | case elfcpp::R_X86_64_SIZE32: |
| 1523 | case elfcpp::R_X86_64_SIZE64: |
| 1524 | default: |
| 1525 | gold_error(_("%s: unsupported reloc %u against global symbol %s"), |
| 1526 | object->name().c_str(), r_type, |
| 1527 | gsym->demangled_name().c_str()); |
| 1528 | break; |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | // Scan relocations for a section. |
| 1533 | |
| 1534 | void |
| 1535 | Target_x86_64::scan_relocs(const General_options& options, |
| 1536 | Symbol_table* symtab, |
| 1537 | Layout* layout, |
| 1538 | Sized_relobj<64, false>* object, |
| 1539 | unsigned int data_shndx, |
| 1540 | unsigned int sh_type, |
| 1541 | const unsigned char* prelocs, |
| 1542 | size_t reloc_count, |
| 1543 | Output_section* output_section, |
| 1544 | bool needs_special_offset_handling, |
| 1545 | size_t local_symbol_count, |
| 1546 | const unsigned char* plocal_symbols) |
| 1547 | { |
| 1548 | if (sh_type == elfcpp::SHT_REL) |
| 1549 | { |
| 1550 | gold_error(_("%s: unsupported REL reloc section"), |
| 1551 | object->name().c_str()); |
| 1552 | return; |
| 1553 | } |
| 1554 | |
| 1555 | gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA, |
| 1556 | Target_x86_64::Scan>( |
| 1557 | options, |
| 1558 | symtab, |
| 1559 | layout, |
| 1560 | this, |
| 1561 | object, |
| 1562 | data_shndx, |
| 1563 | prelocs, |
| 1564 | reloc_count, |
| 1565 | output_section, |
| 1566 | needs_special_offset_handling, |
| 1567 | local_symbol_count, |
| 1568 | plocal_symbols); |
| 1569 | } |
| 1570 | |
| 1571 | // Finalize the sections. |
| 1572 | |
| 1573 | void |
| 1574 | Target_x86_64::do_finalize_sections(Layout* layout) |
| 1575 | { |
| 1576 | // Fill in some more dynamic tags. |
| 1577 | Output_data_dynamic* const odyn = layout->dynamic_data(); |
| 1578 | if (odyn != NULL) |
| 1579 | { |
| 1580 | if (this->got_plt_ != NULL) |
| 1581 | odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_); |
| 1582 | |
| 1583 | if (this->plt_ != NULL) |
| 1584 | { |
| 1585 | const Output_data* od = this->plt_->rel_plt(); |
| 1586 | odyn->add_section_size(elfcpp::DT_PLTRELSZ, od); |
| 1587 | odyn->add_section_address(elfcpp::DT_JMPREL, od); |
| 1588 | odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA); |
| 1589 | if (this->plt_->has_tlsdesc_entry()) |
| 1590 | { |
| 1591 | unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset(); |
| 1592 | unsigned int got_offset = this->plt_->get_tlsdesc_got_offset(); |
| 1593 | this->got_->finalize_data_size(); |
| 1594 | odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT, |
| 1595 | this->plt_, plt_offset); |
| 1596 | odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT, |
| 1597 | this->got_, got_offset); |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | if (this->rela_dyn_ != NULL) |
| 1602 | { |
| 1603 | const Output_data* od = this->rela_dyn_; |
| 1604 | odyn->add_section_address(elfcpp::DT_RELA, od); |
| 1605 | odyn->add_section_size(elfcpp::DT_RELASZ, od); |
| 1606 | odyn->add_constant(elfcpp::DT_RELAENT, |
| 1607 | elfcpp::Elf_sizes<64>::rela_size); |
| 1608 | } |
| 1609 | |
| 1610 | if (!parameters->options().shared()) |
| 1611 | { |
| 1612 | // The value of the DT_DEBUG tag is filled in by the dynamic |
| 1613 | // linker at run time, and used by the debugger. |
| 1614 | odyn->add_constant(elfcpp::DT_DEBUG, 0); |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | // Emit any relocs we saved in an attempt to avoid generating COPY |
| 1619 | // relocs. |
| 1620 | if (this->copy_relocs_.any_saved_relocs()) |
| 1621 | this->copy_relocs_.emit(this->rela_dyn_section(layout)); |
| 1622 | } |
| 1623 | |
| 1624 | // Perform a relocation. |
| 1625 | |
| 1626 | inline bool |
| 1627 | Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo, |
| 1628 | Target_x86_64* target, |
| 1629 | size_t relnum, |
| 1630 | const elfcpp::Rela<64, false>& rela, |
| 1631 | unsigned int r_type, |
| 1632 | const Sized_symbol<64>* gsym, |
| 1633 | const Symbol_value<64>* psymval, |
| 1634 | unsigned char* view, |
| 1635 | elfcpp::Elf_types<64>::Elf_Addr address, |
| 1636 | section_size_type view_size) |
| 1637 | { |
| 1638 | if (this->skip_call_tls_get_addr_) |
| 1639 | { |
| 1640 | if (r_type != elfcpp::R_X86_64_PLT32 |
| 1641 | || gsym == NULL |
| 1642 | || strcmp(gsym->name(), "__tls_get_addr") != 0) |
| 1643 | { |
| 1644 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
| 1645 | _("missing expected TLS relocation")); |
| 1646 | } |
| 1647 | else |
| 1648 | { |
| 1649 | this->skip_call_tls_get_addr_ = false; |
| 1650 | return false; |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | // Pick the value to use for symbols defined in shared objects. |
| 1655 | Symbol_value<64> symval; |
| 1656 | if (gsym != NULL |
| 1657 | && (gsym->is_from_dynobj() |
| 1658 | || (parameters->options().shared() |
| 1659 | && (gsym->is_undefined() || gsym->is_preemptible()))) |
| 1660 | && gsym->has_plt_offset()) |
| 1661 | { |
| 1662 | symval.set_output_value(target->plt_section()->address() |
| 1663 | + gsym->plt_offset()); |
| 1664 | psymval = &symval; |
| 1665 | } |
| 1666 | |
| 1667 | const Sized_relobj<64, false>* object = relinfo->object; |
| 1668 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); |
| 1669 | |
| 1670 | // Get the GOT offset if needed. |
| 1671 | // The GOT pointer points to the end of the GOT section. |
| 1672 | // We need to subtract the size of the GOT section to get |
| 1673 | // the actual offset to use in the relocation. |
| 1674 | bool have_got_offset = false; |
| 1675 | unsigned int got_offset = 0; |
| 1676 | switch (r_type) |
| 1677 | { |
| 1678 | case elfcpp::R_X86_64_GOT32: |
| 1679 | case elfcpp::R_X86_64_GOT64: |
| 1680 | case elfcpp::R_X86_64_GOTPLT64: |
| 1681 | case elfcpp::R_X86_64_GOTPCREL: |
| 1682 | case elfcpp::R_X86_64_GOTPCREL64: |
| 1683 | if (gsym != NULL) |
| 1684 | { |
| 1685 | gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD)); |
| 1686 | got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size(); |
| 1687 | } |
| 1688 | else |
| 1689 | { |
| 1690 | unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info()); |
| 1691 | gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD)); |
| 1692 | got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD) |
| 1693 | - target->got_size()); |
| 1694 | } |
| 1695 | have_got_offset = true; |
| 1696 | break; |
| 1697 | |
| 1698 | default: |
| 1699 | break; |
| 1700 | } |
| 1701 | |
| 1702 | switch (r_type) |
| 1703 | { |
| 1704 | case elfcpp::R_X86_64_NONE: |
| 1705 | case elfcpp::R_386_GNU_VTINHERIT: |
| 1706 | case elfcpp::R_386_GNU_VTENTRY: |
| 1707 | break; |
| 1708 | |
| 1709 | case elfcpp::R_X86_64_64: |
| 1710 | Relocate_functions<64, false>::rela64(view, object, psymval, addend); |
| 1711 | break; |
| 1712 | |
| 1713 | case elfcpp::R_X86_64_PC64: |
| 1714 | Relocate_functions<64, false>::pcrela64(view, object, psymval, addend, |
| 1715 | address); |
| 1716 | break; |
| 1717 | |
| 1718 | case elfcpp::R_X86_64_32: |
| 1719 | // FIXME: we need to verify that value + addend fits into 32 bits: |
| 1720 | // uint64_t x = value + addend; |
| 1721 | // x == static_cast<uint64_t>(static_cast<uint32_t>(x)) |
| 1722 | // Likewise for other <=32-bit relocations (but see R_X86_64_32S). |
| 1723 | Relocate_functions<64, false>::rela32(view, object, psymval, addend); |
| 1724 | break; |
| 1725 | |
| 1726 | case elfcpp::R_X86_64_32S: |
| 1727 | // FIXME: we need to verify that value + addend fits into 32 bits: |
| 1728 | // int64_t x = value + addend; // note this quantity is signed! |
| 1729 | // x == static_cast<int64_t>(static_cast<int32_t>(x)) |
| 1730 | Relocate_functions<64, false>::rela32(view, object, psymval, addend); |
| 1731 | break; |
| 1732 | |
| 1733 | case elfcpp::R_X86_64_PC32: |
| 1734 | Relocate_functions<64, false>::pcrela32(view, object, psymval, addend, |
| 1735 | address); |
| 1736 | break; |
| 1737 | |
| 1738 | case elfcpp::R_X86_64_16: |
| 1739 | Relocate_functions<64, false>::rela16(view, object, psymval, addend); |
| 1740 | break; |
| 1741 | |
| 1742 | case elfcpp::R_X86_64_PC16: |
| 1743 | Relocate_functions<64, false>::pcrela16(view, object, psymval, addend, |
| 1744 | address); |
| 1745 | break; |
| 1746 | |
| 1747 | case elfcpp::R_X86_64_8: |
| 1748 | Relocate_functions<64, false>::rela8(view, object, psymval, addend); |
| 1749 | break; |
| 1750 | |
| 1751 | case elfcpp::R_X86_64_PC8: |
| 1752 | Relocate_functions<64, false>::pcrela8(view, object, psymval, addend, |
| 1753 | address); |
| 1754 | break; |
| 1755 | |
| 1756 | case elfcpp::R_X86_64_PLT32: |
| 1757 | gold_assert(gsym == NULL |
| 1758 | || gsym->has_plt_offset() |
| 1759 | || gsym->final_value_is_known() |
| 1760 | || (gsym->is_defined() |
| 1761 | && !gsym->is_from_dynobj() |
| 1762 | && !gsym->is_preemptible())); |
| 1763 | // Note: while this code looks the same as for R_X86_64_PC32, it |
| 1764 | // behaves differently because psymval was set to point to |
| 1765 | // the PLT entry, rather than the symbol, in Scan::global(). |
| 1766 | Relocate_functions<64, false>::pcrela32(view, object, psymval, addend, |
| 1767 | address); |
| 1768 | break; |
| 1769 | |
| 1770 | case elfcpp::R_X86_64_PLTOFF64: |
| 1771 | { |
| 1772 | gold_assert(gsym); |
| 1773 | gold_assert(gsym->has_plt_offset() |
| 1774 | || gsym->final_value_is_known()); |
| 1775 | elfcpp::Elf_types<64>::Elf_Addr got_address; |
| 1776 | got_address = target->got_section(NULL, NULL)->address(); |
| 1777 | Relocate_functions<64, false>::rela64(view, object, psymval, |
| 1778 | addend - got_address); |
| 1779 | } |
| 1780 | |
| 1781 | case elfcpp::R_X86_64_GOT32: |
| 1782 | gold_assert(have_got_offset); |
| 1783 | Relocate_functions<64, false>::rela32(view, got_offset, addend); |
| 1784 | break; |
| 1785 | |
| 1786 | case elfcpp::R_X86_64_GOTPC32: |
| 1787 | { |
| 1788 | gold_assert(gsym); |
| 1789 | elfcpp::Elf_types<64>::Elf_Addr value; |
| 1790 | value = target->got_plt_section()->address(); |
| 1791 | Relocate_functions<64, false>::pcrela32(view, value, addend, address); |
| 1792 | } |
| 1793 | break; |
| 1794 | |
| 1795 | case elfcpp::R_X86_64_GOT64: |
| 1796 | // The ABI doc says "Like GOT64, but indicates a PLT entry is needed." |
| 1797 | // Since we always add a PLT entry, this is equivalent. |
| 1798 | case elfcpp::R_X86_64_GOTPLT64: |
| 1799 | gold_assert(have_got_offset); |
| 1800 | Relocate_functions<64, false>::rela64(view, got_offset, addend); |
| 1801 | break; |
| 1802 | |
| 1803 | case elfcpp::R_X86_64_GOTPC64: |
| 1804 | { |
| 1805 | gold_assert(gsym); |
| 1806 | elfcpp::Elf_types<64>::Elf_Addr value; |
| 1807 | value = target->got_plt_section()->address(); |
| 1808 | Relocate_functions<64, false>::pcrela64(view, value, addend, address); |
| 1809 | } |
| 1810 | break; |
| 1811 | |
| 1812 | case elfcpp::R_X86_64_GOTOFF64: |
| 1813 | { |
| 1814 | elfcpp::Elf_types<64>::Elf_Addr value; |
| 1815 | value = (psymval->value(object, 0) |
| 1816 | - target->got_plt_section()->address()); |
| 1817 | Relocate_functions<64, false>::rela64(view, value, addend); |
| 1818 | } |
| 1819 | break; |
| 1820 | |
| 1821 | case elfcpp::R_X86_64_GOTPCREL: |
| 1822 | { |
| 1823 | gold_assert(have_got_offset); |
| 1824 | elfcpp::Elf_types<64>::Elf_Addr value; |
| 1825 | value = target->got_plt_section()->address() + got_offset; |
| 1826 | Relocate_functions<64, false>::pcrela32(view, value, addend, address); |
| 1827 | } |
| 1828 | break; |
| 1829 | |
| 1830 | case elfcpp::R_X86_64_GOTPCREL64: |
| 1831 | { |
| 1832 | gold_assert(have_got_offset); |
| 1833 | elfcpp::Elf_types<64>::Elf_Addr value; |
| 1834 | value = target->got_plt_section()->address() + got_offset; |
| 1835 | Relocate_functions<64, false>::pcrela64(view, value, addend, address); |
| 1836 | } |
| 1837 | break; |
| 1838 | |
| 1839 | case elfcpp::R_X86_64_COPY: |
| 1840 | case elfcpp::R_X86_64_GLOB_DAT: |
| 1841 | case elfcpp::R_X86_64_JUMP_SLOT: |
| 1842 | case elfcpp::R_X86_64_RELATIVE: |
| 1843 | // These are outstanding tls relocs, which are unexpected when linking |
| 1844 | case elfcpp::R_X86_64_TPOFF64: |
| 1845 | case elfcpp::R_X86_64_DTPMOD64: |
| 1846 | case elfcpp::R_X86_64_TLSDESC: |
| 1847 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
| 1848 | _("unexpected reloc %u in object file"), |
| 1849 | r_type); |
| 1850 | break; |
| 1851 | |
| 1852 | // These are initial tls relocs, which are expected when linking |
| 1853 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
| 1854 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) |
| 1855 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 1856 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 1857 | case elfcpp::R_X86_64_DTPOFF32: |
| 1858 | case elfcpp::R_X86_64_DTPOFF64: |
| 1859 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 1860 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 1861 | this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval, |
| 1862 | view, address, view_size); |
| 1863 | break; |
| 1864 | |
| 1865 | case elfcpp::R_X86_64_SIZE32: |
| 1866 | case elfcpp::R_X86_64_SIZE64: |
| 1867 | default: |
| 1868 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
| 1869 | _("unsupported reloc %u"), |
| 1870 | r_type); |
| 1871 | break; |
| 1872 | } |
| 1873 | |
| 1874 | return true; |
| 1875 | } |
| 1876 | |
| 1877 | // Perform a TLS relocation. |
| 1878 | |
| 1879 | inline void |
| 1880 | Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo, |
| 1881 | Target_x86_64* target, |
| 1882 | size_t relnum, |
| 1883 | const elfcpp::Rela<64, false>& rela, |
| 1884 | unsigned int r_type, |
| 1885 | const Sized_symbol<64>* gsym, |
| 1886 | const Symbol_value<64>* psymval, |
| 1887 | unsigned char* view, |
| 1888 | elfcpp::Elf_types<64>::Elf_Addr address, |
| 1889 | section_size_type view_size) |
| 1890 | { |
| 1891 | Output_segment* tls_segment = relinfo->layout->tls_segment(); |
| 1892 | |
| 1893 | const Sized_relobj<64, false>* object = relinfo->object; |
| 1894 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); |
| 1895 | |
| 1896 | elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0); |
| 1897 | |
| 1898 | const bool is_final = (gsym == NULL |
| 1899 | ? !parameters->options().output_is_position_independent() |
| 1900 | : gsym->final_value_is_known()); |
| 1901 | const tls::Tls_optimization optimized_type |
| 1902 | = Target_x86_64::optimize_tls_reloc(is_final, r_type); |
| 1903 | switch (r_type) |
| 1904 | { |
| 1905 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
| 1906 | this->saw_tls_block_reloc_ = true; |
| 1907 | if (optimized_type == tls::TLSOPT_TO_LE) |
| 1908 | { |
| 1909 | gold_assert(tls_segment != NULL); |
| 1910 | this->tls_gd_to_le(relinfo, relnum, tls_segment, |
| 1911 | rela, r_type, value, view, |
| 1912 | view_size); |
| 1913 | break; |
| 1914 | } |
| 1915 | else |
| 1916 | { |
| 1917 | unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE |
| 1918 | ? GOT_TYPE_TLS_OFFSET |
| 1919 | : GOT_TYPE_TLS_PAIR); |
| 1920 | unsigned int got_offset; |
| 1921 | if (gsym != NULL) |
| 1922 | { |
| 1923 | gold_assert(gsym->has_got_offset(got_type)); |
| 1924 | got_offset = gsym->got_offset(got_type) - target->got_size(); |
| 1925 | } |
| 1926 | else |
| 1927 | { |
| 1928 | unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info()); |
| 1929 | gold_assert(object->local_has_got_offset(r_sym, got_type)); |
| 1930 | got_offset = (object->local_got_offset(r_sym, got_type) |
| 1931 | - target->got_size()); |
| 1932 | } |
| 1933 | if (optimized_type == tls::TLSOPT_TO_IE) |
| 1934 | { |
| 1935 | gold_assert(tls_segment != NULL); |
| 1936 | value = target->got_plt_section()->address() + got_offset; |
| 1937 | this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type, |
| 1938 | value, view, address, view_size); |
| 1939 | break; |
| 1940 | } |
| 1941 | else if (optimized_type == tls::TLSOPT_NONE) |
| 1942 | { |
| 1943 | // Relocate the field with the offset of the pair of GOT |
| 1944 | // entries. |
| 1945 | value = target->got_plt_section()->address() + got_offset; |
| 1946 | Relocate_functions<64, false>::pcrela32(view, value, addend, |
| 1947 | address); |
| 1948 | break; |
| 1949 | } |
| 1950 | } |
| 1951 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
| 1952 | _("unsupported reloc %u"), r_type); |
| 1953 | break; |
| 1954 | |
| 1955 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) |
| 1956 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 1957 | this->saw_tls_block_reloc_ = true; |
| 1958 | if (optimized_type == tls::TLSOPT_TO_LE) |
| 1959 | { |
| 1960 | gold_assert(tls_segment != NULL); |
| 1961 | this->tls_desc_gd_to_le(relinfo, relnum, tls_segment, |
| 1962 | rela, r_type, value, view, |
| 1963 | view_size); |
| 1964 | break; |
| 1965 | } |
| 1966 | else |
| 1967 | { |
| 1968 | unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE |
| 1969 | ? GOT_TYPE_TLS_OFFSET |
| 1970 | : GOT_TYPE_TLS_DESC); |
| 1971 | unsigned int got_offset; |
| 1972 | if (gsym != NULL) |
| 1973 | { |
| 1974 | gold_assert(gsym->has_got_offset(got_type)); |
| 1975 | got_offset = gsym->got_offset(got_type) - target->got_size(); |
| 1976 | } |
| 1977 | else |
| 1978 | { |
| 1979 | unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info()); |
| 1980 | gold_assert(object->local_has_got_offset(r_sym, got_type)); |
| 1981 | got_offset = (object->local_got_offset(r_sym, got_type) |
| 1982 | - target->got_size()); |
| 1983 | } |
| 1984 | if (optimized_type == tls::TLSOPT_TO_IE) |
| 1985 | { |
| 1986 | gold_assert(tls_segment != NULL); |
| 1987 | value = target->got_plt_section()->address() + got_offset; |
| 1988 | this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, |
| 1989 | rela, r_type, value, view, address, |
| 1990 | view_size); |
| 1991 | break; |
| 1992 | } |
| 1993 | else if (optimized_type == tls::TLSOPT_NONE) |
| 1994 | { |
| 1995 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) |
| 1996 | { |
| 1997 | // Relocate the field with the offset of the pair of GOT |
| 1998 | // entries. |
| 1999 | value = target->got_plt_section()->address() + got_offset; |
| 2000 | Relocate_functions<64, false>::pcrela32(view, value, addend, |
| 2001 | address); |
| 2002 | } |
| 2003 | break; |
| 2004 | } |
| 2005 | } |
| 2006 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
| 2007 | _("unsupported reloc %u"), r_type); |
| 2008 | break; |
| 2009 | |
| 2010 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 2011 | this->saw_tls_block_reloc_ = true; |
| 2012 | if (optimized_type == tls::TLSOPT_TO_LE) |
| 2013 | { |
| 2014 | gold_assert(tls_segment != NULL); |
| 2015 | this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type, |
| 2016 | value, view, view_size); |
| 2017 | break; |
| 2018 | } |
| 2019 | else if (optimized_type == tls::TLSOPT_NONE) |
| 2020 | { |
| 2021 | // Relocate the field with the offset of the GOT entry for |
| 2022 | // the module index. |
| 2023 | unsigned int got_offset; |
| 2024 | got_offset = (target->got_mod_index_entry(NULL, NULL, NULL) |
| 2025 | - target->got_size()); |
| 2026 | value = target->got_plt_section()->address() + got_offset; |
| 2027 | Relocate_functions<64, false>::pcrela32(view, value, addend, |
| 2028 | address); |
| 2029 | break; |
| 2030 | } |
| 2031 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
| 2032 | _("unsupported reloc %u"), r_type); |
| 2033 | break; |
| 2034 | |
| 2035 | case elfcpp::R_X86_64_DTPOFF32: |
| 2036 | gold_assert(tls_segment != NULL); |
| 2037 | if (optimized_type == tls::TLSOPT_TO_LE) |
| 2038 | { |
| 2039 | // This relocation type is used in debugging information. |
| 2040 | // In that case we need to not optimize the value. If we |
| 2041 | // haven't seen a TLSLD reloc, then we assume we should not |
| 2042 | // optimize this reloc. |
| 2043 | if (this->saw_tls_block_reloc_) |
| 2044 | value -= tls_segment->memsz(); |
| 2045 | } |
| 2046 | Relocate_functions<64, false>::rela32(view, value, 0); |
| 2047 | break; |
| 2048 | |
| 2049 | case elfcpp::R_X86_64_DTPOFF64: |
| 2050 | gold_assert(tls_segment != NULL); |
| 2051 | if (optimized_type == tls::TLSOPT_TO_LE) |
| 2052 | { |
| 2053 | // See R_X86_64_DTPOFF32, just above, for why we test this. |
| 2054 | if (this->saw_tls_block_reloc_) |
| 2055 | value -= tls_segment->memsz(); |
| 2056 | } |
| 2057 | Relocate_functions<64, false>::rela64(view, value, 0); |
| 2058 | break; |
| 2059 | |
| 2060 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 2061 | if (optimized_type == tls::TLSOPT_TO_LE) |
| 2062 | { |
| 2063 | gold_assert(tls_segment != NULL); |
| 2064 | Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment, |
| 2065 | rela, r_type, value, view, |
| 2066 | view_size); |
| 2067 | break; |
| 2068 | } |
| 2069 | else if (optimized_type == tls::TLSOPT_NONE) |
| 2070 | { |
| 2071 | // Relocate the field with the offset of the GOT entry for |
| 2072 | // the tp-relative offset of the symbol. |
| 2073 | unsigned int got_offset; |
| 2074 | if (gsym != NULL) |
| 2075 | { |
| 2076 | gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET)); |
| 2077 | got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET) |
| 2078 | - target->got_size()); |
| 2079 | } |
| 2080 | else |
| 2081 | { |
| 2082 | unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info()); |
| 2083 | gold_assert(object->local_has_got_offset(r_sym, |
| 2084 | GOT_TYPE_TLS_OFFSET)); |
| 2085 | got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET) |
| 2086 | - target->got_size()); |
| 2087 | } |
| 2088 | value = target->got_plt_section()->address() + got_offset; |
| 2089 | Relocate_functions<64, false>::pcrela32(view, value, addend, address); |
| 2090 | break; |
| 2091 | } |
| 2092 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), |
| 2093 | _("unsupported reloc type %u"), |
| 2094 | r_type); |
| 2095 | break; |
| 2096 | |
| 2097 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 2098 | value -= tls_segment->memsz(); |
| 2099 | Relocate_functions<64, false>::rela32(view, value, 0); |
| 2100 | break; |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | // Do a relocation in which we convert a TLS General-Dynamic to an |
| 2105 | // Initial-Exec. |
| 2106 | |
| 2107 | inline void |
| 2108 | Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo, |
| 2109 | size_t relnum, |
| 2110 | Output_segment*, |
| 2111 | const elfcpp::Rela<64, false>& rela, |
| 2112 | unsigned int, |
| 2113 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 2114 | unsigned char* view, |
| 2115 | elfcpp::Elf_types<64>::Elf_Addr address, |
| 2116 | section_size_type view_size) |
| 2117 | { |
| 2118 | // .byte 0x66; leaq foo@tlsgd(%rip),%rdi; |
| 2119 | // .word 0x6666; rex64; call __tls_get_addr |
| 2120 | // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax |
| 2121 | |
| 2122 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4); |
| 2123 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12); |
| 2124 | |
| 2125 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 2126 | (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0)); |
| 2127 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 2128 | (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0)); |
| 2129 | |
| 2130 | memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16); |
| 2131 | |
| 2132 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); |
| 2133 | Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address); |
| 2134 | |
| 2135 | // The next reloc should be a PLT32 reloc against __tls_get_addr. |
| 2136 | // We can skip it. |
| 2137 | this->skip_call_tls_get_addr_ = true; |
| 2138 | } |
| 2139 | |
| 2140 | // Do a relocation in which we convert a TLS General-Dynamic to a |
| 2141 | // Local-Exec. |
| 2142 | |
| 2143 | inline void |
| 2144 | Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo, |
| 2145 | size_t relnum, |
| 2146 | Output_segment* tls_segment, |
| 2147 | const elfcpp::Rela<64, false>& rela, |
| 2148 | unsigned int, |
| 2149 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 2150 | unsigned char* view, |
| 2151 | section_size_type view_size) |
| 2152 | { |
| 2153 | // .byte 0x66; leaq foo@tlsgd(%rip),%rdi; |
| 2154 | // .word 0x6666; rex64; call __tls_get_addr |
| 2155 | // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax |
| 2156 | |
| 2157 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4); |
| 2158 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12); |
| 2159 | |
| 2160 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 2161 | (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0)); |
| 2162 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 2163 | (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0)); |
| 2164 | |
| 2165 | memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16); |
| 2166 | |
| 2167 | value -= tls_segment->memsz(); |
| 2168 | Relocate_functions<64, false>::rela32(view + 8, value, 0); |
| 2169 | |
| 2170 | // The next reloc should be a PLT32 reloc against __tls_get_addr. |
| 2171 | // We can skip it. |
| 2172 | this->skip_call_tls_get_addr_ = true; |
| 2173 | } |
| 2174 | |
| 2175 | // Do a TLSDESC-style General-Dynamic to Initial-Exec transition. |
| 2176 | |
| 2177 | inline void |
| 2178 | Target_x86_64::Relocate::tls_desc_gd_to_ie( |
| 2179 | const Relocate_info<64, false>* relinfo, |
| 2180 | size_t relnum, |
| 2181 | Output_segment*, |
| 2182 | const elfcpp::Rela<64, false>& rela, |
| 2183 | unsigned int r_type, |
| 2184 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 2185 | unsigned char* view, |
| 2186 | elfcpp::Elf_types<64>::Elf_Addr address, |
| 2187 | section_size_type view_size) |
| 2188 | { |
| 2189 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) |
| 2190 | { |
| 2191 | // leaq foo@tlsdesc(%rip), %rax |
| 2192 | // ==> movq foo@gottpoff(%rip), %rax |
| 2193 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); |
| 2194 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); |
| 2195 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 2196 | view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05); |
| 2197 | view[-2] = 0x8b; |
| 2198 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); |
| 2199 | Relocate_functions<64, false>::pcrela32(view, value, addend, address); |
| 2200 | } |
| 2201 | else |
| 2202 | { |
| 2203 | // call *foo@tlscall(%rax) |
| 2204 | // ==> nop; nop |
| 2205 | gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL); |
| 2206 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2); |
| 2207 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 2208 | view[0] == 0xff && view[1] == 0x10); |
| 2209 | view[0] = 0x66; |
| 2210 | view[1] = 0x90; |
| 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | // Do a TLSDESC-style General-Dynamic to Local-Exec transition. |
| 2215 | |
| 2216 | inline void |
| 2217 | Target_x86_64::Relocate::tls_desc_gd_to_le( |
| 2218 | const Relocate_info<64, false>* relinfo, |
| 2219 | size_t relnum, |
| 2220 | Output_segment* tls_segment, |
| 2221 | const elfcpp::Rela<64, false>& rela, |
| 2222 | unsigned int r_type, |
| 2223 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 2224 | unsigned char* view, |
| 2225 | section_size_type view_size) |
| 2226 | { |
| 2227 | if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) |
| 2228 | { |
| 2229 | // leaq foo@tlsdesc(%rip), %rax |
| 2230 | // ==> movq foo@tpoff, %rax |
| 2231 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); |
| 2232 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); |
| 2233 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 2234 | view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05); |
| 2235 | view[-2] = 0xc7; |
| 2236 | view[-1] = 0xc0; |
| 2237 | value -= tls_segment->memsz(); |
| 2238 | Relocate_functions<64, false>::rela32(view, value, 0); |
| 2239 | } |
| 2240 | else |
| 2241 | { |
| 2242 | // call *foo@tlscall(%rax) |
| 2243 | // ==> nop; nop |
| 2244 | gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL); |
| 2245 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2); |
| 2246 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 2247 | view[0] == 0xff && view[1] == 0x10); |
| 2248 | view[0] = 0x66; |
| 2249 | view[1] = 0x90; |
| 2250 | } |
| 2251 | } |
| 2252 | |
| 2253 | inline void |
| 2254 | Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo, |
| 2255 | size_t relnum, |
| 2256 | Output_segment*, |
| 2257 | const elfcpp::Rela<64, false>& rela, |
| 2258 | unsigned int, |
| 2259 | elfcpp::Elf_types<64>::Elf_Addr, |
| 2260 | unsigned char* view, |
| 2261 | section_size_type view_size) |
| 2262 | { |
| 2263 | // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt; |
| 2264 | // ... leq foo@dtpoff(%rax),%reg |
| 2265 | // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx |
| 2266 | |
| 2267 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); |
| 2268 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9); |
| 2269 | |
| 2270 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), |
| 2271 | view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d); |
| 2272 | |
| 2273 | tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8); |
| 2274 | |
| 2275 | memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12); |
| 2276 | |
| 2277 | // The next reloc should be a PLT32 reloc against __tls_get_addr. |
| 2278 | // We can skip it. |
| 2279 | this->skip_call_tls_get_addr_ = true; |
| 2280 | } |
| 2281 | |
| 2282 | // Do a relocation in which we convert a TLS Initial-Exec to a |
| 2283 | // Local-Exec. |
| 2284 | |
| 2285 | inline void |
| 2286 | Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo, |
| 2287 | size_t relnum, |
| 2288 | Output_segment* tls_segment, |
| 2289 | const elfcpp::Rela<64, false>& rela, |
| 2290 | unsigned int, |
| 2291 | elfcpp::Elf_types<64>::Elf_Addr value, |
| 2292 | unsigned char* view, |
| 2293 | section_size_type view_size) |
| 2294 | { |
| 2295 | // We need to examine the opcodes to figure out which instruction we |
| 2296 | // are looking at. |
| 2297 | |
| 2298 | // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg |
| 2299 | // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg |
| 2300 | |
| 2301 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); |
| 2302 | tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); |
| 2303 | |
| 2304 | unsigned char op1 = view[-3]; |
| 2305 | unsigned char op2 = view[-2]; |
| 2306 | unsigned char op3 = view[-1]; |
| 2307 | unsigned char reg = op3 >> 3; |
| 2308 | |
| 2309 | if (op2 == 0x8b) |
| 2310 | { |
| 2311 | // movq |
| 2312 | if (op1 == 0x4c) |
| 2313 | view[-3] = 0x49; |
| 2314 | view[-2] = 0xc7; |
| 2315 | view[-1] = 0xc0 | reg; |
| 2316 | } |
| 2317 | else if (reg == 4) |
| 2318 | { |
| 2319 | // Special handling for %rsp. |
| 2320 | if (op1 == 0x4c) |
| 2321 | view[-3] = 0x49; |
| 2322 | view[-2] = 0x81; |
| 2323 | view[-1] = 0xc0 | reg; |
| 2324 | } |
| 2325 | else |
| 2326 | { |
| 2327 | // addq |
| 2328 | if (op1 == 0x4c) |
| 2329 | view[-3] = 0x4d; |
| 2330 | view[-2] = 0x8d; |
| 2331 | view[-1] = 0x80 | reg | (reg << 3); |
| 2332 | } |
| 2333 | |
| 2334 | value -= tls_segment->memsz(); |
| 2335 | Relocate_functions<64, false>::rela32(view, value, 0); |
| 2336 | } |
| 2337 | |
| 2338 | // Relocate section data. |
| 2339 | |
| 2340 | void |
| 2341 | Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo, |
| 2342 | unsigned int sh_type, |
| 2343 | const unsigned char* prelocs, |
| 2344 | size_t reloc_count, |
| 2345 | Output_section* output_section, |
| 2346 | bool needs_special_offset_handling, |
| 2347 | unsigned char* view, |
| 2348 | elfcpp::Elf_types<64>::Elf_Addr address, |
| 2349 | section_size_type view_size) |
| 2350 | { |
| 2351 | gold_assert(sh_type == elfcpp::SHT_RELA); |
| 2352 | |
| 2353 | gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA, |
| 2354 | Target_x86_64::Relocate>( |
| 2355 | relinfo, |
| 2356 | this, |
| 2357 | prelocs, |
| 2358 | reloc_count, |
| 2359 | output_section, |
| 2360 | needs_special_offset_handling, |
| 2361 | view, |
| 2362 | address, |
| 2363 | view_size); |
| 2364 | } |
| 2365 | |
| 2366 | // Return the size of a relocation while scanning during a relocatable |
| 2367 | // link. |
| 2368 | |
| 2369 | unsigned int |
| 2370 | Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc( |
| 2371 | unsigned int r_type, |
| 2372 | Relobj* object) |
| 2373 | { |
| 2374 | switch (r_type) |
| 2375 | { |
| 2376 | case elfcpp::R_X86_64_NONE: |
| 2377 | case elfcpp::R_386_GNU_VTINHERIT: |
| 2378 | case elfcpp::R_386_GNU_VTENTRY: |
| 2379 | case elfcpp::R_X86_64_TLSGD: // Global-dynamic |
| 2380 | case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) |
| 2381 | case elfcpp::R_X86_64_TLSDESC_CALL: |
| 2382 | case elfcpp::R_X86_64_TLSLD: // Local-dynamic |
| 2383 | case elfcpp::R_X86_64_DTPOFF32: |
| 2384 | case elfcpp::R_X86_64_DTPOFF64: |
| 2385 | case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec |
| 2386 | case elfcpp::R_X86_64_TPOFF32: // Local-exec |
| 2387 | return 0; |
| 2388 | |
| 2389 | case elfcpp::R_X86_64_64: |
| 2390 | case elfcpp::R_X86_64_PC64: |
| 2391 | case elfcpp::R_X86_64_GOTOFF64: |
| 2392 | case elfcpp::R_X86_64_GOTPC64: |
| 2393 | case elfcpp::R_X86_64_PLTOFF64: |
| 2394 | case elfcpp::R_X86_64_GOT64: |
| 2395 | case elfcpp::R_X86_64_GOTPCREL64: |
| 2396 | case elfcpp::R_X86_64_GOTPCREL: |
| 2397 | case elfcpp::R_X86_64_GOTPLT64: |
| 2398 | return 8; |
| 2399 | |
| 2400 | case elfcpp::R_X86_64_32: |
| 2401 | case elfcpp::R_X86_64_32S: |
| 2402 | case elfcpp::R_X86_64_PC32: |
| 2403 | case elfcpp::R_X86_64_PLT32: |
| 2404 | case elfcpp::R_X86_64_GOTPC32: |
| 2405 | case elfcpp::R_X86_64_GOT32: |
| 2406 | return 4; |
| 2407 | |
| 2408 | case elfcpp::R_X86_64_16: |
| 2409 | case elfcpp::R_X86_64_PC16: |
| 2410 | return 2; |
| 2411 | |
| 2412 | case elfcpp::R_X86_64_8: |
| 2413 | case elfcpp::R_X86_64_PC8: |
| 2414 | return 1; |
| 2415 | |
| 2416 | case elfcpp::R_X86_64_COPY: |
| 2417 | case elfcpp::R_X86_64_GLOB_DAT: |
| 2418 | case elfcpp::R_X86_64_JUMP_SLOT: |
| 2419 | case elfcpp::R_X86_64_RELATIVE: |
| 2420 | // These are outstanding tls relocs, which are unexpected when linking |
| 2421 | case elfcpp::R_X86_64_TPOFF64: |
| 2422 | case elfcpp::R_X86_64_DTPMOD64: |
| 2423 | case elfcpp::R_X86_64_TLSDESC: |
| 2424 | object->error(_("unexpected reloc %u in object file"), r_type); |
| 2425 | return 0; |
| 2426 | |
| 2427 | case elfcpp::R_X86_64_SIZE32: |
| 2428 | case elfcpp::R_X86_64_SIZE64: |
| 2429 | default: |
| 2430 | object->error(_("unsupported reloc %u against local symbol"), r_type); |
| 2431 | return 0; |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | // Scan the relocs during a relocatable link. |
| 2436 | |
| 2437 | void |
| 2438 | Target_x86_64::scan_relocatable_relocs(const General_options& options, |
| 2439 | Symbol_table* symtab, |
| 2440 | Layout* layout, |
| 2441 | Sized_relobj<64, false>* object, |
| 2442 | unsigned int data_shndx, |
| 2443 | unsigned int sh_type, |
| 2444 | const unsigned char* prelocs, |
| 2445 | size_t reloc_count, |
| 2446 | Output_section* output_section, |
| 2447 | bool needs_special_offset_handling, |
| 2448 | size_t local_symbol_count, |
| 2449 | const unsigned char* plocal_symbols, |
| 2450 | Relocatable_relocs* rr) |
| 2451 | { |
| 2452 | gold_assert(sh_type == elfcpp::SHT_RELA); |
| 2453 | |
| 2454 | typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA, |
| 2455 | Relocatable_size_for_reloc> Scan_relocatable_relocs; |
| 2456 | |
| 2457 | gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA, |
| 2458 | Scan_relocatable_relocs>( |
| 2459 | options, |
| 2460 | symtab, |
| 2461 | layout, |
| 2462 | object, |
| 2463 | data_shndx, |
| 2464 | prelocs, |
| 2465 | reloc_count, |
| 2466 | output_section, |
| 2467 | needs_special_offset_handling, |
| 2468 | local_symbol_count, |
| 2469 | plocal_symbols, |
| 2470 | rr); |
| 2471 | } |
| 2472 | |
| 2473 | // Relocate a section during a relocatable link. |
| 2474 | |
| 2475 | void |
| 2476 | Target_x86_64::relocate_for_relocatable( |
| 2477 | const Relocate_info<64, false>* relinfo, |
| 2478 | unsigned int sh_type, |
| 2479 | const unsigned char* prelocs, |
| 2480 | size_t reloc_count, |
| 2481 | Output_section* output_section, |
| 2482 | off_t offset_in_output_section, |
| 2483 | const Relocatable_relocs* rr, |
| 2484 | unsigned char* view, |
| 2485 | elfcpp::Elf_types<64>::Elf_Addr view_address, |
| 2486 | section_size_type view_size, |
| 2487 | unsigned char* reloc_view, |
| 2488 | section_size_type reloc_view_size) |
| 2489 | { |
| 2490 | gold_assert(sh_type == elfcpp::SHT_RELA); |
| 2491 | |
| 2492 | gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>( |
| 2493 | relinfo, |
| 2494 | prelocs, |
| 2495 | reloc_count, |
| 2496 | output_section, |
| 2497 | offset_in_output_section, |
| 2498 | rr, |
| 2499 | view, |
| 2500 | view_address, |
| 2501 | view_size, |
| 2502 | reloc_view, |
| 2503 | reloc_view_size); |
| 2504 | } |
| 2505 | |
| 2506 | // Return the value to use for a dynamic which requires special |
| 2507 | // treatment. This is how we support equality comparisons of function |
| 2508 | // pointers across shared library boundaries, as described in the |
| 2509 | // processor specific ABI supplement. |
| 2510 | |
| 2511 | uint64_t |
| 2512 | Target_x86_64::do_dynsym_value(const Symbol* gsym) const |
| 2513 | { |
| 2514 | gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset()); |
| 2515 | return this->plt_section()->address() + gsym->plt_offset(); |
| 2516 | } |
| 2517 | |
| 2518 | // Return a string used to fill a code section with nops to take up |
| 2519 | // the specified length. |
| 2520 | |
| 2521 | std::string |
| 2522 | Target_x86_64::do_code_fill(section_size_type length) const |
| 2523 | { |
| 2524 | if (length >= 16) |
| 2525 | { |
| 2526 | // Build a jmpq instruction to skip over the bytes. |
| 2527 | unsigned char jmp[5]; |
| 2528 | jmp[0] = 0xe9; |
| 2529 | elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5); |
| 2530 | return (std::string(reinterpret_cast<char*>(&jmp[0]), 5) |
| 2531 | + std::string(length - 5, '\0')); |
| 2532 | } |
| 2533 | |
| 2534 | // Nop sequences of various lengths. |
| 2535 | const char nop1[1] = { 0x90 }; // nop |
| 2536 | const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax |
| 2537 | const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi |
| 2538 | const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi |
| 2539 | const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop |
| 2540 | 0x00 }; // leal 0(%esi,1),%esi |
| 2541 | const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi |
| 2542 | 0x00, 0x00 }; |
| 2543 | const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi |
| 2544 | 0x00, 0x00, 0x00 }; |
| 2545 | const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop |
| 2546 | 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi |
| 2547 | const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi |
| 2548 | 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi |
| 2549 | 0x00 }; |
| 2550 | const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi |
| 2551 | 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi |
| 2552 | 0x00, 0x00 }; |
| 2553 | const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi |
| 2554 | 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi |
| 2555 | 0x00, 0x00, 0x00 }; |
| 2556 | const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi |
| 2557 | 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi |
| 2558 | 0x00, 0x00, 0x00, 0x00 }; |
| 2559 | const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi |
| 2560 | 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi |
| 2561 | 0x27, 0x00, 0x00, 0x00, |
| 2562 | 0x00 }; |
| 2563 | const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi |
| 2564 | 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi |
| 2565 | 0xbc, 0x27, 0x00, 0x00, |
| 2566 | 0x00, 0x00 }; |
| 2567 | const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15 |
| 2568 | 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,... |
| 2569 | 0x90, 0x90, 0x90, 0x90, |
| 2570 | 0x90, 0x90, 0x90 }; |
| 2571 | |
| 2572 | const char* nops[16] = { |
| 2573 | NULL, |
| 2574 | nop1, nop2, nop3, nop4, nop5, nop6, nop7, |
| 2575 | nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15 |
| 2576 | }; |
| 2577 | |
| 2578 | return std::string(nops[length], length); |
| 2579 | } |
| 2580 | |
| 2581 | // The selector for x86_64 object files. |
| 2582 | |
| 2583 | class Target_selector_x86_64 : public Target_selector |
| 2584 | { |
| 2585 | public: |
| 2586 | Target_selector_x86_64() |
| 2587 | : Target_selector(elfcpp::EM_X86_64, 64, false, "elf64-x86-64") |
| 2588 | { } |
| 2589 | |
| 2590 | Target* |
| 2591 | do_instantiate_target() |
| 2592 | { return new Target_x86_64(); } |
| 2593 | }; |
| 2594 | |
| 2595 | Target_selector_x86_64 target_selector_x86_64; |
| 2596 | |
| 2597 | } // End anonymous namespace. |