1 // mips.cc -- mips target support for gold. 2 3 // Copyright (C) 2011-2014 Free Software Foundation, Inc. 4 // Written by Sasa Stankovic <sasa.stankovic (at) imgtec.com> 5 // and Aleksandar Simeonov <aleksandar.simeonov (at) rt-rk.com>. 6 // This file contains borrowed and adapted code from bfd/elfxx-mips.c. 7 8 // This file is part of gold. 9 10 // This program is free software; you can redistribute it and/or modify 11 // it under the terms of the GNU General Public License as published by 12 // the Free Software Foundation; either version 3 of the License, or 13 // (at your option) any later version. 14 15 // This program is distributed in the hope that it will be useful, 16 // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 // GNU General Public License for more details. 19 20 // You should have received a copy of the GNU General Public License 21 // along with this program; if not, write to the Free Software 22 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 23 // MA 02110-1301, USA. 24 25 #include "gold.h" 26 27 #include <algorithm> 28 #include <set> 29 #include <sstream> 30 #include "demangle.h" 31 32 #include "elfcpp.h" 33 #include "parameters.h" 34 #include "reloc.h" 35 #include "mips.h" 36 #include "object.h" 37 #include "symtab.h" 38 #include "layout.h" 39 #include "output.h" 40 #include "copy-relocs.h" 41 #include "target.h" 42 #include "target-reloc.h" 43 #include "target-select.h" 44 #include "tls.h" 45 #include "errors.h" 46 #include "gc.h" 47 #include "nacl.h" 48 49 namespace 50 { 51 using namespace gold; 52 53 template<int size, bool big_endian> 54 class Mips_output_data_plt; 55 56 template<int size, bool big_endian> 57 class Mips_output_data_got; 58 59 template<int size, bool big_endian> 60 class Target_mips; 61 62 template<int size, bool big_endian> 63 class Mips_output_section_reginfo; 64 65 template<int size, bool big_endian> 66 class Mips_output_data_la25_stub; 67 68 template<int size, bool big_endian> 69 class Mips_output_data_mips_stubs; 70 71 template<int size> 72 class Mips_symbol; 73 74 template<int size, bool big_endian> 75 class Mips_got_info; 76 77 template<int size, bool big_endian> 78 class Mips_relobj; 79 80 class Mips16_stub_section_base; 81 82 template<int size, bool big_endian> 83 class Mips16_stub_section; 84 85 // The ABI says that every symbol used by dynamic relocations must have 86 // a global GOT entry. Among other things, this provides the dynamic 87 // linker with a free, directly-indexed cache. The GOT can therefore 88 // contain symbols that are not referenced by GOT relocations themselves 89 // (in other words, it may have symbols that are not referenced by things 90 // like R_MIPS_GOT16 and R_MIPS_GOT_PAGE). 91 92 // GOT relocations are less likely to overflow if we put the associated 93 // GOT entries towards the beginning. We therefore divide the global 94 // GOT entries into two areas: "normal" and "reloc-only". Entries in 95 // the first area can be used for both dynamic relocations and GP-relative 96 // accesses, while those in the "reloc-only" area are for dynamic 97 // relocations only. 98 99 // These GGA_* ("Global GOT Area") values are organised so that lower 100 // values are more general than higher values. Also, non-GGA_NONE 101 // values are ordered by the position of the area in the GOT. 102 103 enum Global_got_area 104 { 105 GGA_NORMAL = 0, 106 GGA_RELOC_ONLY = 1, 107 GGA_NONE = 2 108 }; 109 110 // The types of GOT entries needed for this platform. 111 // These values are exposed to the ABI in an incremental link. 112 // Do not renumber existing values without changing the version 113 // number of the .gnu_incremental_inputs section. 114 enum Got_type 115 { 116 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol 117 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset 118 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair 119 120 // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links. 121 GOT_TYPE_STANDARD_MULTIGOT = 3, 122 GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024, 123 GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024 124 }; 125 126 // TLS type of GOT entry. 127 enum Got_tls_type 128 { 129 GOT_TLS_NONE = 0, 130 GOT_TLS_GD = 1, 131 GOT_TLS_LDM = 2, 132 GOT_TLS_IE = 4 133 }; 134 135 // Return TRUE if a relocation of type R_TYPE from OBJECT might 136 // require an la25 stub. See also local_pic_function, which determines 137 // whether the destination function ever requires a stub. 138 template<int size, bool big_endian> 139 static inline bool 140 relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object, 141 unsigned int r_type, bool target_is_16_bit_code) 142 { 143 // We specifically ignore branches and jumps from EF_PIC objects, 144 // where the onus is on the compiler or programmer to perform any 145 // necessary initialization of $25. Sometimes such initialization 146 // is unnecessary; for example, -mno-shared functions do not use 147 // the incoming value of $25, and may therefore be called directly. 148 if (object->is_pic()) 149 return false; 150 151 switch (r_type) 152 { 153 case elfcpp::R_MIPS_26: 154 case elfcpp::R_MIPS_PC16: 155 case elfcpp::R_MICROMIPS_26_S1: 156 case elfcpp::R_MICROMIPS_PC7_S1: 157 case elfcpp::R_MICROMIPS_PC10_S1: 158 case elfcpp::R_MICROMIPS_PC16_S1: 159 case elfcpp::R_MICROMIPS_PC23_S2: 160 return true; 161 162 case elfcpp::R_MIPS16_26: 163 return !target_is_16_bit_code; 164 165 default: 166 return false; 167 } 168 } 169 170 // Return true if SYM is a locally-defined PIC function, in the sense 171 // that it or its fn_stub might need $25 to be valid on entry. 172 // Note that MIPS16 functions set up $gp using PC-relative instructions, 173 // so they themselves never need $25 to be valid. Only non-MIPS16 174 // entry points are of interest here. 175 template<int size, bool big_endian> 176 static inline bool 177 local_pic_function(Mips_symbol<size>* sym) 178 { 179 bool def_regular = (sym->source() == Symbol::FROM_OBJECT 180 && !sym->object()->is_dynamic() 181 && !sym->is_undefined()); 182 183 if (sym->is_defined() && def_regular) 184 { 185 Mips_relobj<size, big_endian>* object = 186 static_cast<Mips_relobj<size, big_endian>*>(sym->object()); 187 188 if ((object->is_pic() || sym->is_pic()) 189 && (!sym->is_mips16() 190 || (sym->has_mips16_fn_stub() && sym->need_fn_stub()))) 191 return true; 192 } 193 return false; 194 } 195 196 static inline bool 197 hi16_reloc(int r_type) 198 { 199 return (r_type == elfcpp::R_MIPS_HI16 200 || r_type == elfcpp::R_MIPS16_HI16 201 || r_type == elfcpp::R_MICROMIPS_HI16); 202 } 203 204 static inline bool 205 lo16_reloc(int r_type) 206 { 207 return (r_type == elfcpp::R_MIPS_LO16 208 || r_type == elfcpp::R_MIPS16_LO16 209 || r_type == elfcpp::R_MICROMIPS_LO16); 210 } 211 212 static inline bool 213 got16_reloc(unsigned int r_type) 214 { 215 return (r_type == elfcpp::R_MIPS_GOT16 216 || r_type == elfcpp::R_MIPS16_GOT16 217 || r_type == elfcpp::R_MICROMIPS_GOT16); 218 } 219 220 static inline bool 221 call_lo16_reloc(unsigned int r_type) 222 { 223 return (r_type == elfcpp::R_MIPS_CALL_LO16 224 || r_type == elfcpp::R_MICROMIPS_CALL_LO16); 225 } 226 227 static inline bool 228 got_lo16_reloc(unsigned int r_type) 229 { 230 return (r_type == elfcpp::R_MIPS_GOT_LO16 231 || r_type == elfcpp::R_MICROMIPS_GOT_LO16); 232 } 233 234 static inline bool 235 got_disp_reloc(unsigned int r_type) 236 { 237 return (r_type == elfcpp::R_MIPS_GOT_DISP 238 || r_type == elfcpp::R_MICROMIPS_GOT_DISP); 239 } 240 241 static inline bool 242 got_page_reloc(unsigned int r_type) 243 { 244 return (r_type == elfcpp::R_MIPS_GOT_PAGE 245 || r_type == elfcpp::R_MICROMIPS_GOT_PAGE); 246 } 247 248 static inline bool 249 tls_gd_reloc(unsigned int r_type) 250 { 251 return (r_type == elfcpp::R_MIPS_TLS_GD 252 || r_type == elfcpp::R_MIPS16_TLS_GD 253 || r_type == elfcpp::R_MICROMIPS_TLS_GD); 254 } 255 256 static inline bool 257 tls_gottprel_reloc(unsigned int r_type) 258 { 259 return (r_type == elfcpp::R_MIPS_TLS_GOTTPREL 260 || r_type == elfcpp::R_MIPS16_TLS_GOTTPREL 261 || r_type == elfcpp::R_MICROMIPS_TLS_GOTTPREL); 262 } 263 264 static inline bool 265 tls_ldm_reloc(unsigned int r_type) 266 { 267 return (r_type == elfcpp::R_MIPS_TLS_LDM 268 || r_type == elfcpp::R_MIPS16_TLS_LDM 269 || r_type == elfcpp::R_MICROMIPS_TLS_LDM); 270 } 271 272 static inline bool 273 mips16_call_reloc(unsigned int r_type) 274 { 275 return (r_type == elfcpp::R_MIPS16_26 276 || r_type == elfcpp::R_MIPS16_CALL16); 277 } 278 279 static inline bool 280 jal_reloc(unsigned int r_type) 281 { 282 return (r_type == elfcpp::R_MIPS_26 283 || r_type == elfcpp::R_MIPS16_26 284 || r_type == elfcpp::R_MICROMIPS_26_S1); 285 } 286 287 static inline bool 288 micromips_branch_reloc(unsigned int r_type) 289 { 290 return (r_type == elfcpp::R_MICROMIPS_26_S1 291 || r_type == elfcpp::R_MICROMIPS_PC16_S1 292 || r_type == elfcpp::R_MICROMIPS_PC10_S1 293 || r_type == elfcpp::R_MICROMIPS_PC7_S1); 294 } 295 296 // Check if R_TYPE is a MIPS16 reloc. 297 static inline bool 298 mips16_reloc(unsigned int r_type) 299 { 300 switch (r_type) 301 { 302 case elfcpp::R_MIPS16_26: 303 case elfcpp::R_MIPS16_GPREL: 304 case elfcpp::R_MIPS16_GOT16: 305 case elfcpp::R_MIPS16_CALL16: 306 case elfcpp::R_MIPS16_HI16: 307 case elfcpp::R_MIPS16_LO16: 308 case elfcpp::R_MIPS16_TLS_GD: 309 case elfcpp::R_MIPS16_TLS_LDM: 310 case elfcpp::R_MIPS16_TLS_DTPREL_HI16: 311 case elfcpp::R_MIPS16_TLS_DTPREL_LO16: 312 case elfcpp::R_MIPS16_TLS_GOTTPREL: 313 case elfcpp::R_MIPS16_TLS_TPREL_HI16: 314 case elfcpp::R_MIPS16_TLS_TPREL_LO16: 315 return true; 316 317 default: 318 return false; 319 } 320 } 321 322 // Check if R_TYPE is a microMIPS reloc. 323 static inline bool 324 micromips_reloc(unsigned int r_type) 325 { 326 switch (r_type) 327 { 328 case elfcpp::R_MICROMIPS_26_S1: 329 case elfcpp::R_MICROMIPS_HI16: 330 case elfcpp::R_MICROMIPS_LO16: 331 case elfcpp::R_MICROMIPS_GPREL16: 332 case elfcpp::R_MICROMIPS_LITERAL: 333 case elfcpp::R_MICROMIPS_GOT16: 334 case elfcpp::R_MICROMIPS_PC7_S1: 335 case elfcpp::R_MICROMIPS_PC10_S1: 336 case elfcpp::R_MICROMIPS_PC16_S1: 337 case elfcpp::R_MICROMIPS_CALL16: 338 case elfcpp::R_MICROMIPS_GOT_DISP: 339 case elfcpp::R_MICROMIPS_GOT_PAGE: 340 case elfcpp::R_MICROMIPS_GOT_OFST: 341 case elfcpp::R_MICROMIPS_GOT_HI16: 342 case elfcpp::R_MICROMIPS_GOT_LO16: 343 case elfcpp::R_MICROMIPS_SUB: 344 case elfcpp::R_MICROMIPS_HIGHER: 345 case elfcpp::R_MICROMIPS_HIGHEST: 346 case elfcpp::R_MICROMIPS_CALL_HI16: 347 case elfcpp::R_MICROMIPS_CALL_LO16: 348 case elfcpp::R_MICROMIPS_SCN_DISP: 349 case elfcpp::R_MICROMIPS_JALR: 350 case elfcpp::R_MICROMIPS_HI0_LO16: 351 case elfcpp::R_MICROMIPS_TLS_GD: 352 case elfcpp::R_MICROMIPS_TLS_LDM: 353 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16: 354 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16: 355 case elfcpp::R_MICROMIPS_TLS_GOTTPREL: 356 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16: 357 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16: 358 case elfcpp::R_MICROMIPS_GPREL7_S2: 359 case elfcpp::R_MICROMIPS_PC23_S2: 360 return true; 361 362 default: 363 return false; 364 } 365 } 366 367 static inline bool 368 is_matching_lo16_reloc(unsigned int high_reloc, unsigned int lo16_reloc) 369 { 370 switch (high_reloc) 371 { 372 case elfcpp::R_MIPS_HI16: 373 case elfcpp::R_MIPS_GOT16: 374 return lo16_reloc == elfcpp::R_MIPS_LO16; 375 case elfcpp::R_MIPS16_HI16: 376 case elfcpp::R_MIPS16_GOT16: 377 return lo16_reloc == elfcpp::R_MIPS16_LO16; 378 case elfcpp::R_MICROMIPS_HI16: 379 case elfcpp::R_MICROMIPS_GOT16: 380 return lo16_reloc == elfcpp::R_MICROMIPS_LO16; 381 default: 382 return false; 383 } 384 } 385 386 // This class is used to hold information about one GOT entry. 387 // There are three types of entry: 388 // 389 // (1) a SYMBOL + OFFSET address, where SYMBOL is local to an input object 390 // (object != NULL, symndx >= 0, tls_type != GOT_TLS_LDM) 391 // (2) a SYMBOL address, where SYMBOL is not local to an input object 392 // (object != NULL, symndx == -1) 393 // (3) a TLS LDM slot 394 // (object != NULL, symndx == 0, tls_type == GOT_TLS_LDM) 395 396 template<int size, bool big_endian> 397 class Mips_got_entry 398 { 399 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; 400 401 public: 402 Mips_got_entry(Mips_relobj<size, big_endian>* object, unsigned int symndx, 403 Mips_address addend, unsigned char tls_type, 404 unsigned int shndx) 405 : object_(object), symndx_(symndx), tls_type_(tls_type), shndx_(shndx) 406 { this->d.addend = addend; } 407 408 Mips_got_entry(Mips_relobj<size, big_endian>* object, Mips_symbol<size>* sym, 409 unsigned char tls_type) 410 : object_(object), symndx_(-1U), tls_type_(tls_type), shndx_(-1U) 411 { this->d.sym = sym; } 412 413 // Return whether this entry is for a local symbol. 414 bool 415 is_for_local_symbol() const 416 { return this->symndx_ != -1U; } 417 418 // Return whether this entry is for a global symbol. 419 bool 420 is_for_global_symbol() const 421 { return this->symndx_ == -1U; } 422 423 // Return the hash of this entry. 424 size_t 425 hash() const 426 { 427 if (this->tls_type_ == GOT_TLS_LDM) 428 return this->symndx_ + (1 << 18); 429 if (this->symndx_ != -1U) 430 { 431 uintptr_t object_id = reinterpret_cast<uintptr_t>(this->object()); 432 return this->symndx_ + object_id + this->d.addend; 433 } 434 else 435 { 436 uintptr_t sym_id = reinterpret_cast<uintptr_t>(this->d.sym); 437 return this->symndx_ + sym_id; 438 } 439 } 440 441 // Return whether this entry is equal to OTHER. 442 bool 443 equals(Mips_got_entry<size, big_endian>* other) const 444 { 445 if (this->symndx_ != other->symndx_ 446 || this->tls_type_ != other->tls_type_) 447 return false; 448 if (this->tls_type_ == GOT_TLS_LDM) 449 return true; 450 if (this->symndx_ != -1U) 451 return (this->object() == other->object() 452 && this->d.addend == other->d.addend); 453 else 454 return this->d.sym == other->d.sym; 455 } 456 457 // Return input object that needs this GOT entry. 458 Mips_relobj<size, big_endian>* 459 object() const 460 { 461 gold_assert(this->object_ != NULL); 462 return this->object_; 463 } 464 465 // Return local symbol index for local GOT entries. 466 unsigned int 467 symndx() const 468 { 469 gold_assert(this->symndx_ != -1U); 470 return this->symndx_; 471 } 472 473 // Return the relocation addend for local GOT entries. 474 Mips_address 475 addend() const 476 { 477 gold_assert(this->symndx_ != -1U); 478 return this->d.addend; 479 } 480 481 // Return global symbol for global GOT entries. 482 Mips_symbol<size>* 483 sym() const 484 { 485 gold_assert(this->symndx_ == -1U); 486 return this->d.sym; 487 } 488 489 // Return whether this is a TLS GOT entry. 490 bool 491 is_tls_entry() const 492 { return this->tls_type_ != GOT_TLS_NONE; } 493 494 // Return TLS type of this GOT entry. 495 unsigned char 496 tls_type() const 497 { return this->tls_type_; } 498 499 // Return section index of the local symbol for local GOT entries. 500 unsigned int 501 shndx() const 502 { return this->shndx_; } 503 504 private: 505 // The input object that needs the GOT entry. 506 Mips_relobj<size, big_endian>* object_; 507 // The index of the symbol if we have a local symbol; -1 otherwise. 508 unsigned int symndx_; 509 510 union 511 { 512 // If symndx != -1, the addend of the relocation that should be added to the 513 // symbol value. 514 Mips_address addend; 515 // If symndx == -1, the global symbol corresponding to this GOT entry. The 516 // symbol's entry is in the local area if mips_sym->global_got_area is 517 // GGA_NONE, otherwise it is in the global area. 518 Mips_symbol<size>* sym; 519 } d; 520 521 // The TLS type of this GOT entry. An LDM GOT entry will be a local 522 // symbol entry with r_symndx == 0. 523 unsigned char tls_type_; 524 525 // For local GOT entries, section index of the local symbol. 526 unsigned int shndx_; 527 }; 528 529 // Hash for Mips_got_entry. 530 531 template<int size, bool big_endian> 532 class Mips_got_entry_hash 533 { 534 public: 535 size_t 536 operator()(Mips_got_entry<size, big_endian>* entry) const 537 { return entry->hash(); } 538 }; 539 540 // Equality for Mips_got_entry. 541 542 template<int size, bool big_endian> 543 class Mips_got_entry_eq 544 { 545 public: 546 bool 547 operator()(Mips_got_entry<size, big_endian>* e1, 548 Mips_got_entry<size, big_endian>* e2) const 549 { return e1->equals(e2); } 550 }; 551 552 // Got_page_range. This class describes a range of addends: [MIN_ADDEND, 553 // MAX_ADDEND]. The instances form a non-overlapping list that is sorted by 554 // increasing MIN_ADDEND. 555 556 struct Got_page_range 557 { 558 Got_page_range() 559 : next(NULL), min_addend(0), max_addend(0) 560 { } 561 562 Got_page_range* next; 563 int min_addend; 564 int max_addend; 565 566 // Return the maximum number of GOT page entries required. 567 int 568 get_max_pages() 569 { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; } 570 }; 571 572 // Got_page_entry. This class describes the range of addends that are applied 573 // to page relocations against a given symbol. 574 575 struct Got_page_entry 576 { 577 Got_page_entry() 578 : object(NULL), symndx(-1U), ranges(NULL), num_pages(0) 579 { } 580 581 Got_page_entry(Object* object_, unsigned int symndx_) 582 : object(object_), symndx(symndx_), ranges(NULL), num_pages(0) 583 { } 584 585 // The input object that needs the GOT page entry. 586 Object* object; 587 // The index of the symbol, as stored in the relocation r_info. 588 unsigned int symndx; 589 // The ranges for this page entry. 590 Got_page_range* ranges; 591 // The maximum number of page entries needed for RANGES. 592 unsigned int num_pages; 593 }; 594 595 // Hash for Got_page_entry. 596 597 struct Got_page_entry_hash 598 { 599 size_t 600 operator()(Got_page_entry* entry) const 601 { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; } 602 }; 603 604 // Equality for Got_page_entry. 605 606 struct Got_page_entry_eq 607 { 608 bool 609 operator()(Got_page_entry* entry1, Got_page_entry* entry2) const 610 { 611 return entry1->object == entry2->object && entry1->symndx == entry2->symndx; 612 } 613 }; 614 615 // This class is used to hold .got information when linking. 616 617 template<int size, bool big_endian> 618 class Mips_got_info 619 { 620 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; 621 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> 622 Reloc_section; 623 typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets; 624 625 // Unordered set of GOT entries. 626 typedef Unordered_set<Mips_got_entry<size, big_endian>*, 627 Mips_got_entry_hash<size, big_endian>, 628 Mips_got_entry_eq<size, big_endian> > Got_entry_set; 629 630 // Unordered set of GOT page entries. 631 typedef Unordered_set<Got_page_entry*, 632 Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set; 633 634 public: 635 Mips_got_info() 636 : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0), 637 tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(), 638 got_entries_(), got_page_entries_(), got_page_offset_start_(0), 639 got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U), 640 offset_(0) 641 { } 642 643 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol 644 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT. 645 void 646 record_local_got_symbol(Mips_relobj<size, big_endian>* object, 647 unsigned int symndx, Mips_address addend, 648 unsigned int r_type, unsigned int shndx); 649 650 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM, 651 // in OBJECT. FOR_CALL is true if the caller is only interested in 652 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic 653 // relocation. 654 void 655 record_global_got_symbol(Mips_symbol<size>* mips_sym, 656 Mips_relobj<size, big_endian>* object, 657 unsigned int r_type, bool dyn_reloc, bool for_call); 658 659 // Add ENTRY to master GOT and to OBJECT's GOT. 660 void 661 record_got_entry(Mips_got_entry<size, big_endian>* entry, 662 Mips_relobj<size, big_endian>* object); 663 664 // Record that OBJECT has a page relocation against symbol SYMNDX and 665 // that ADDEND is the addend for that relocation. 666 void 667 record_got_page_entry(Mips_relobj<size, big_endian>* object, 668 unsigned int symndx, int addend); 669 670 // Create all entries that should be in the local part of the GOT. 671 void 672 add_local_entries(Target_mips<size, big_endian>* target, Layout* layout); 673 674 // Create GOT page entries. 675 void 676 add_page_entries(Target_mips<size, big_endian>* target, Layout* layout); 677 678 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY. 679 void 680 add_global_entries(Target_mips<size, big_endian>* target, Layout* layout, 681 unsigned int non_reloc_only_global_gotno); 682 683 // Create global GOT entries that should be in the GGA_RELOC_ONLY area. 684 void 685 add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got); 686 687 // Create TLS GOT entries. 688 void 689 add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout); 690 691 // Decide whether the symbol needs an entry in the global part of the primary 692 // GOT, setting global_got_area accordingly. Count the number of global 693 // symbols that are in the primary GOT only because they have dynamic 694 // relocations R_MIPS_REL32 against them (reloc_only_gotno). 695 void 696 count_got_symbols(Symbol_table* symtab); 697 698 // Return the offset of GOT page entry for VALUE. 699 unsigned int 700 get_got_page_offset(Mips_address value, 701 Mips_output_data_got<size, big_endian>* got); 702 703 // Count the number of GOT entries required. 704 void 705 count_got_entries(); 706 707 // Count the number of GOT entries required by ENTRY. Accumulate the result. 708 void 709 count_got_entry(Mips_got_entry<size, big_endian>* entry); 710 711 // Add FROM's GOT entries. 712 void 713 add_got_entries(Mips_got_info<size, big_endian>* from); 714 715 // Add FROM's GOT page entries. 716 void 717 add_got_page_entries(Mips_got_info<size, big_endian>* from); 718 719 // Return GOT size. 720 unsigned int 721 got_size() const 722 { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_ 723 + this->tls_gotno_) * size/8); 724 } 725 726 // Return the number of local GOT entries. 727 unsigned int 728 local_gotno() const 729 { return this->local_gotno_; } 730 731 // Return the maximum number of page GOT entries needed. 732 unsigned int 733 page_gotno() const 734 { return this->page_gotno_; } 735 736 // Return the number of global GOT entries. 737 unsigned int 738 global_gotno() const 739 { return this->global_gotno_; } 740 741 // Set the number of global GOT entries. 742 void 743 set_global_gotno(unsigned int global_gotno) 744 { this->global_gotno_ = global_gotno; } 745 746 // Return the number of GGA_RELOC_ONLY global GOT entries. 747 unsigned int 748 reloc_only_gotno() const 749 { return this->reloc_only_gotno_; } 750 751 // Return the number of TLS GOT entries. 752 unsigned int 753 tls_gotno() const 754 { return this->tls_gotno_; } 755 756 // Return the GOT type for this GOT. Used for multi-GOT links only. 757 unsigned int 758 multigot_got_type(unsigned int got_type) const 759 { 760 switch (got_type) 761 { 762 case GOT_TYPE_STANDARD: 763 return GOT_TYPE_STANDARD_MULTIGOT + this->index_; 764 case GOT_TYPE_TLS_OFFSET: 765 return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_; 766 case GOT_TYPE_TLS_PAIR: 767 return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_; 768 default: 769 gold_unreachable(); 770 } 771 } 772 773 // Remove lazy-binding stubs for global symbols in this GOT. 774 void 775 remove_lazy_stubs(Target_mips<size, big_endian>* target); 776 777 // Return offset of this GOT from the start of .got section. 778 unsigned int 779 offset() const 780 { return this->offset_; } 781 782 // Set offset of this GOT from the start of .got section. 783 void 784 set_offset(unsigned int offset) 785 { this->offset_ = offset; } 786 787 // Set index of this GOT in multi-GOT links. 788 void 789 set_index(unsigned int index) 790 { this->index_ = index; } 791 792 // Return next GOT in multi-GOT links. 793 Mips_got_info<size, big_endian>* 794 next() const 795 { return this->next_; } 796 797 // Set next GOT in multi-GOT links. 798 void 799 set_next(Mips_got_info<size, big_endian>* next) 800 { this->next_ = next; } 801 802 // Return the offset of TLS LDM entry for this GOT. 803 unsigned int 804 tls_ldm_offset() const 805 { return this->tls_ldm_offset_; } 806 807 // Set the offset of TLS LDM entry for this GOT. 808 void 809 set_tls_ldm_offset(unsigned int tls_ldm_offset) 810 { this->tls_ldm_offset_ = tls_ldm_offset; } 811 812 Unordered_set<Mips_symbol<size>*>& 813 global_got_symbols() 814 { return this->global_got_symbols_; } 815 816 // Return the GOT_TLS_* type required by relocation type R_TYPE. 817 static int 818 mips_elf_reloc_tls_type(unsigned int r_type) 819 { 820 if (tls_gd_reloc(r_type)) 821 return GOT_TLS_GD; 822 823 if (tls_ldm_reloc(r_type)) 824 return GOT_TLS_LDM; 825 826 if (tls_gottprel_reloc(r_type)) 827 return GOT_TLS_IE; 828 829 return GOT_TLS_NONE; 830 } 831 832 // Return the number of GOT slots needed for GOT TLS type TYPE. 833 static int 834 mips_tls_got_entries(unsigned int type) 835 { 836 switch (type) 837 { 838 case GOT_TLS_GD: 839 case GOT_TLS_LDM: 840 return 2; 841 842 case GOT_TLS_IE: 843 return 1; 844 845 case GOT_TLS_NONE: 846 return 0; 847 848 default: 849 gold_unreachable(); 850 } 851 } 852 853 private: 854 // The number of local GOT entries. 855 unsigned int local_gotno_; 856 // The maximum number of page GOT entries needed. 857 unsigned int page_gotno_; 858 // The number of global GOT entries. 859 unsigned int global_gotno_; 860 // The number of global GOT entries that are in the GGA_RELOC_ONLY area. 861 unsigned int reloc_only_gotno_; 862 // The number of TLS GOT entries. 863 unsigned int tls_gotno_; 864 // The offset of TLS LDM entry for this GOT. 865 unsigned int tls_ldm_offset_; 866 // All symbols that have global GOT entry. 867 Unordered_set<Mips_symbol<size>*> global_got_symbols_; 868 // A hash table holding GOT entries. 869 Got_entry_set got_entries_; 870 // A hash table of GOT page entries. 871 Got_page_entry_set got_page_entries_; 872 // The offset of first GOT page entry for this GOT. 873 unsigned int got_page_offset_start_; 874 // The offset of next available GOT page entry for this GOT. 875 unsigned int got_page_offset_next_; 876 // A hash table that maps GOT page entry value to the GOT offset where 877 // the entry is located. 878 Got_page_offsets got_page_offsets_; 879 // In multi-GOT links, a pointer to the next GOT. 880 Mips_got_info<size, big_endian>* next_; 881 // Index of this GOT in multi-GOT links. 882 unsigned int index_; 883 // The offset of this GOT in multi-GOT links. 884 unsigned int offset_; 885 }; 886 887 // This is a helper class used during relocation scan. It records GOT16 addend. 888 889 template<int size, bool big_endian> 890 struct got16_addend 891 { 892 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; 893 894 got16_addend(const Sized_relobj_file<size, big_endian>* _object, 895 unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym, 896 Mips_address _addend) 897 : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym), 898 addend(_addend) 899 { } 900 901 const Sized_relobj_file<size, big_endian>* object; 902 unsigned int shndx; 903 unsigned int r_type; 904 unsigned int r_sym; 905 Mips_address addend; 906 }; 907 908 // Mips_symbol class. Holds additional symbol information needed for Mips. 909 910 template<int size> 911 class Mips_symbol : public Sized_symbol<size> 912 { 913 public: 914 Mips_symbol() 915 : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U), 916 has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0), 917 pointer_equality_needed_(false), global_got_area_(GGA_NONE), 918 global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false), 919 needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U), 920 comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL), 921 mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false) 922 { } 923 924 // Return whether this is a MIPS16 symbol. 925 bool 926 is_mips16() const 927 { 928 // (st_other & STO_MIPS16) == STO_MIPS16 929 return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2)) 930 == elfcpp::STO_MIPS16 >> 2); 931 } 932 933 // Return whether this is a microMIPS symbol. 934 bool 935 is_micromips() const 936 { 937 // (st_other & STO_MIPS_ISA) == STO_MICROMIPS 938 return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2)) 939 == elfcpp::STO_MICROMIPS >> 2); 940 } 941 942 // Return whether the symbol needs MIPS16 fn_stub. 943 bool 944 need_fn_stub() const 945 { return this->need_fn_stub_; } 946 947 // Set that the symbol needs MIPS16 fn_stub. 948 void 949 set_need_fn_stub() 950 { this->need_fn_stub_ = true; } 951 952 // Return whether this symbol is referenced by branch relocations from 953 // any non-PIC input file. 954 bool 955 has_nonpic_branches() const 956 { return this->has_nonpic_branches_; } 957 958 // Set that this symbol is referenced by branch relocations from 959 // any non-PIC input file. 960 void 961 set_has_nonpic_branches() 962 { this->has_nonpic_branches_ = true; } 963 964 // Return the offset of the la25 stub for this symbol from the start of the 965 // la25 stub section. 966 unsigned int 967 la25_stub_offset() const 968 { return this->la25_stub_offset_; } 969 970 // Set the offset of the la25 stub for this symbol from the start of the 971 // la25 stub section. 972 void 973 set_la25_stub_offset(unsigned int offset) 974 { this->la25_stub_offset_ = offset; } 975 976 // Return whether the symbol has la25 stub. This is true if this symbol is 977 // for a PIC function, and there are non-PIC branches and jumps to it. 978 bool 979 has_la25_stub() const 980 { return this->la25_stub_offset_ != -1U; } 981 982 // Return whether there is a relocation against this symbol that must be 983 // resolved by the static linker (that is, the relocation cannot possibly 984 // be made dynamic). 985 bool 986 has_static_relocs() const 987 { return this->has_static_relocs_; } 988 989 // Set that there is a relocation against this symbol that must be resolved 990 // by the static linker (that is, the relocation cannot possibly be made 991 // dynamic). 992 void 993 set_has_static_relocs() 994 { this->has_static_relocs_ = true; } 995 996 // Return whether we must not create a lazy-binding stub for this symbol. 997 bool 998 no_lazy_stub() const 999 { return this->no_lazy_stub_; } 1000 1001 // Set that we must not create a lazy-binding stub for this symbol. 1002 void 1003 set_no_lazy_stub() 1004 { this->no_lazy_stub_ = true; } 1005 1006 // Return the offset of the lazy-binding stub for this symbol from the start 1007 // of .MIPS.stubs section. 1008 unsigned int 1009 lazy_stub_offset() const 1010 { return this->lazy_stub_offset_; } 1011 1012 // Set the offset of the lazy-binding stub for this symbol from the start 1013 // of .MIPS.stubs section. 1014 void 1015 set_lazy_stub_offset(unsigned int offset) 1016 { this->lazy_stub_offset_ = offset; } 1017 1018 // Return whether there are any relocations for this symbol where 1019 // pointer equality matters. 1020 bool 1021 pointer_equality_needed() const 1022 { return this->pointer_equality_needed_; } 1023 1024 // Set that there are relocations for this symbol where pointer equality 1025 // matters. 1026 void 1027 set_pointer_equality_needed() 1028 { this->pointer_equality_needed_ = true; } 1029 1030 // Return global GOT area where this symbol in located. 1031 Global_got_area 1032 global_got_area() const 1033 { return this->global_got_area_; } 1034 1035 // Set global GOT area where this symbol in located. 1036 void 1037 set_global_got_area(Global_got_area global_got_area) 1038 { this->global_got_area_ = global_got_area; } 1039 1040 // Return the global GOT offset for this symbol. For multi-GOT links, this 1041 // returns the offset from the start of .got section to the first GOT entry 1042 // for the symbol. Note that in multi-GOT links the symbol can have entry 1043 // in more than one GOT. 1044 unsigned int 1045 global_gotoffset() const 1046 { return this->global_gotoffset_; } 1047 1048 // Set the global GOT offset for this symbol. Note that in multi-GOT links 1049 // the symbol can have entry in more than one GOT. This method will set 1050 // the offset only if it is less than current offset. 1051 void 1052 set_global_gotoffset(unsigned int offset) 1053 { 1054 if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_) 1055 this->global_gotoffset_ = offset; 1056 } 1057 1058 // Return whether all GOT relocations for this symbol are for calls. 1059 bool 1060 got_only_for_calls() const 1061 { return this->got_only_for_calls_; } 1062 1063 // Set that there is a GOT relocation for this symbol that is not for call. 1064 void 1065 set_got_not_only_for_calls() 1066 { this->got_only_for_calls_ = false; } 1067 1068 // Return whether this is a PIC symbol. 1069 bool 1070 is_pic() const 1071 { 1072 // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC 1073 return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2)) 1074 == (elfcpp::STO_MIPS_PIC >> 2)); 1075 } 1076 1077 // Set the flag in st_other field that marks this symbol as PIC. 1078 void 1079 set_pic() 1080 { 1081 if (this->is_mips16()) 1082 // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC 1083 this->set_nonvis((this->nonvis() 1084 & ~((elfcpp::STO_MIPS16 >> 2) 1085 | (elfcpp::STO_MIPS_FLAGS >> 2))) 1086 | (elfcpp::STO_MIPS_PIC >> 2)); 1087 else 1088 // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC 1089 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2)) 1090 | (elfcpp::STO_MIPS_PIC >> 2)); 1091 } 1092 1093 // Set the flag in st_other field that marks this symbol as PLT. 1094 void 1095 set_mips_plt() 1096 { 1097 if (this->is_mips16()) 1098 // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT 1099 this->set_nonvis((this->nonvis() 1100 & ((elfcpp::STO_MIPS16 >> 2) 1101 | ~(elfcpp::STO_MIPS_FLAGS >> 2))) 1102 | (elfcpp::STO_MIPS_PLT >> 2)); 1103 1104 else 1105 // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT 1106 this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2)) 1107 | (elfcpp::STO_MIPS_PLT >> 2)); 1108 } 1109 1110 // Downcast a base pointer to a Mips_symbol pointer. 1111 static Mips_symbol<size>* 1112 as_mips_sym(Symbol* sym) 1113 { return static_cast<Mips_symbol<size>*>(sym); } 1114 1115 // Downcast a base pointer to a Mips_symbol pointer. 1116 static const Mips_symbol<size>* 1117 as_mips_sym(const Symbol* sym) 1118 { return static_cast<const Mips_symbol<size>*>(sym); } 1119 1120 // Return whether the symbol has lazy-binding stub. 1121 bool 1122 has_lazy_stub() const 1123 { return this->has_lazy_stub_; } 1124 1125 // Set whether the symbol has lazy-binding stub. 1126 void 1127 set_has_lazy_stub(bool has_lazy_stub) 1128 { this->has_lazy_stub_ = has_lazy_stub; } 1129 1130 // Return whether the symbol needs a standard PLT entry. 1131 bool 1132 needs_mips_plt() const 1133 { return this->needs_mips_plt_; } 1134 1135 // Set whether the symbol needs a standard PLT entry. 1136 void 1137 set_needs_mips_plt(bool needs_mips_plt) 1138 { this->needs_mips_plt_ = needs_mips_plt; } 1139 1140 // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT 1141 // entry. 1142 bool 1143 needs_comp_plt() const 1144 { return this->needs_comp_plt_; } 1145 1146 // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry. 1147 void 1148 set_needs_comp_plt(bool needs_comp_plt) 1149 { this->needs_comp_plt_ = needs_comp_plt; } 1150 1151 // Return standard PLT entry offset, or -1 if none. 1152 unsigned int 1153 mips_plt_offset() const 1154 { return this->mips_plt_offset_; } 1155 1156 // Set standard PLT entry offset. 1157 void 1158 set_mips_plt_offset(unsigned int mips_plt_offset) 1159 { this->mips_plt_offset_ = mips_plt_offset; } 1160 1161 // Return whether the symbol has standard PLT entry. 1162 bool 1163 has_mips_plt_offset() const 1164 { return this->mips_plt_offset_ != -1U; } 1165 1166 // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none. 1167 unsigned int 1168 comp_plt_offset() const 1169 { return this->comp_plt_offset_; } 1170 1171 // Set compressed (MIPS16 or microMIPS) PLT entry offset. 1172 void 1173 set_comp_plt_offset(unsigned int comp_plt_offset) 1174 { this->comp_plt_offset_ = comp_plt_offset; } 1175 1176 // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry. 1177 bool 1178 has_comp_plt_offset() const 1179 { return this->comp_plt_offset_ != -1U; } 1180 1181 // Return MIPS16 fn stub for a symbol. 1182 template<bool big_endian> 1183 Mips16_stub_section<size, big_endian>* 1184 get_mips16_fn_stub() const 1185 { 1186 return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_); 1187 } 1188 1189 // Set MIPS16 fn stub for a symbol. 1190 void 1191 set_mips16_fn_stub(Mips16_stub_section_base* stub) 1192 { this->mips16_fn_stub_ = stub; } 1193 1194 // Return whether symbol has MIPS16 fn stub. 1195 bool 1196 has_mips16_fn_stub() const 1197 { return this->mips16_fn_stub_ != NULL; } 1198 1199 // Return MIPS16 call stub for a symbol. 1200 template<bool big_endian> 1201 Mips16_stub_section<size, big_endian>* 1202 get_mips16_call_stub() const 1203 { 1204 return static_cast<Mips16_stub_section<size, big_endian>*>( 1205 mips16_call_stub_); 1206 } 1207 1208 // Set MIPS16 call stub for a symbol. 1209 void 1210 set_mips16_call_stub(Mips16_stub_section_base* stub) 1211 { this->mips16_call_stub_ = stub; } 1212 1213 // Return whether symbol has MIPS16 call stub. 1214 bool 1215 has_mips16_call_stub() const 1216 { return this->mips16_call_stub_ != NULL; } 1217 1218 // Return MIPS16 call_fp stub for a symbol. 1219 template<bool big_endian> 1220 Mips16_stub_section<size, big_endian>* 1221 get_mips16_call_fp_stub() const 1222 { 1223 return static_cast<Mips16_stub_section<size, big_endian>*>( 1224 mips16_call_fp_stub_); 1225 } 1226 1227 // Set MIPS16 call_fp stub for a symbol. 1228 void 1229 set_mips16_call_fp_stub(Mips16_stub_section_base* stub) 1230 { this->mips16_call_fp_stub_ = stub; } 1231 1232 // Return whether symbol has MIPS16 call_fp stub. 1233 bool 1234 has_mips16_call_fp_stub() const 1235 { return this->mips16_call_fp_stub_ != NULL; } 1236 1237 bool 1238 get_applied_secondary_got_fixup() const 1239 { return applied_secondary_got_fixup_; } 1240 1241 void 1242 set_applied_secondary_got_fixup() 1243 { this->applied_secondary_got_fixup_ = true; } 1244 1245 private: 1246 // Whether the symbol needs MIPS16 fn_stub. This is true if this symbol 1247 // appears in any relocs other than a 16 bit call. 1248 bool need_fn_stub_; 1249 1250 // True if this symbol is referenced by branch relocations from 1251 // any non-PIC input file. This is used to determine whether an 1252 // la25 stub is required. 1253 bool has_nonpic_branches_; 1254 1255 // The offset of the la25 stub for this symbol from the start of the 1256 // la25 stub section. 1257 unsigned int la25_stub_offset_; 1258 1259 // True if there is a relocation against this symbol that must be 1260 // resolved by the static linker (that is, the relocation cannot 1261 // possibly be made dynamic). 1262 bool has_static_relocs_; 1263 1264 // Whether we must not create a lazy-binding stub for this symbol. 1265 // This is true if the symbol has relocations related to taking the 1266 // function's address. 1267 bool no_lazy_stub_; 1268 1269 // The offset of the lazy-binding stub for this symbol from the start of 1270 // .MIPS.stubs section. 1271 unsigned int lazy_stub_offset_; 1272 1273 // True if there are any relocations for this symbol where pointer equality 1274 // matters. 1275 bool pointer_equality_needed_; 1276 1277 // Global GOT area where this symbol in located, or GGA_NONE if symbol is not 1278 // in the global part of the GOT. 1279 Global_got_area global_got_area_; 1280 1281 // The global GOT offset for this symbol. For multi-GOT links, this is offset 1282 // from the start of .got section to the first GOT entry for the symbol. 1283 // Note that in multi-GOT links the symbol can have entry in more than one GOT. 1284 unsigned int global_gotoffset_; 1285 1286 // Whether all GOT relocations for this symbol are for calls. 1287 bool got_only_for_calls_; 1288 // Whether the symbol has lazy-binding stub. 1289 bool has_lazy_stub_; 1290 // Whether the symbol needs a standard PLT entry. 1291 bool needs_mips_plt_; 1292 // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry. 1293 bool needs_comp_plt_; 1294 // Standard PLT entry offset, or -1 if none. 1295 unsigned int mips_plt_offset_; 1296 // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none. 1297 unsigned int comp_plt_offset_; 1298 // MIPS16 fn stub for a symbol. 1299 Mips16_stub_section_base* mips16_fn_stub_; 1300 // MIPS16 call stub for a symbol. 1301 Mips16_stub_section_base* mips16_call_stub_; 1302 // MIPS16 call_fp stub for a symbol. 1303 Mips16_stub_section_base* mips16_call_fp_stub_; 1304 1305 bool applied_secondary_got_fixup_; 1306 }; 1307 1308 // Mips16_stub_section class. 1309 1310 // The mips16 compiler uses a couple of special sections to handle 1311 // floating point arguments. 1312 1313 // Section names that look like .mips16.fn.FNNAME contain stubs that 1314 // copy floating point arguments from the fp regs to the gp regs and 1315 // then jump to FNNAME. If any 32 bit function calls FNNAME, the 1316 // call should be redirected to the stub instead. If no 32 bit 1317 // function calls FNNAME, the stub should be discarded. We need to 1318 // consider any reference to the function, not just a call, because 1319 // if the address of the function is taken we will need the stub, 1320 // since the address might be passed to a 32 bit function. 1321 1322 // Section names that look like .mips16.call.FNNAME contain stubs 1323 // that copy floating point arguments from the gp regs to the fp 1324 // regs and then jump to FNNAME. If FNNAME is a 32 bit function, 1325 // then any 16 bit function that calls FNNAME should be redirected 1326 // to the stub instead. If FNNAME is not a 32 bit function, the 1327 // stub should be discarded. 1328 1329 // .mips16.call.fp.FNNAME sections are similar, but contain stubs 1330 // which call FNNAME and then copy the return value from the fp regs 1331 // to the gp regs. These stubs store the return address in $18 while 1332 // calling FNNAME; any function which might call one of these stubs 1333 // must arrange to save $18 around the call. (This case is not 1334 // needed for 32 bit functions that call 16 bit functions, because 1335 // 16 bit functions always return floating point values in both 1336 // $f0/$f1 and $2/$3.) 1337 1338 // Note that in all cases FNNAME might be defined statically. 1339 // Therefore, FNNAME is not used literally. Instead, the relocation 1340 // information will indicate which symbol the section is for. 1341 1342 // We record any stubs that we find in the symbol table. 1343 1344 // TODO(sasa): All mips16 stub sections should be emitted in the .text section. 1345 1346 class Mips16_stub_section_base { }; 1347 1348 template<int size, bool big_endian> 1349 class Mips16_stub_section : public Mips16_stub_section_base 1350 { 1351 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; 1352 1353 public: 1354 Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx) 1355 : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL), 1356 found_r_mips_none_(false) 1357 { 1358 gold_assert(object->is_mips16_fn_stub_section(shndx) 1359 || object->is_mips16_call_stub_section(shndx) 1360 || object->is_mips16_call_fp_stub_section(shndx)); 1361 } 1362 1363 // Return the object of this stub section. 1364 Mips_relobj<size, big_endian>* 1365 object() const 1366 { return this->object_; } 1367 1368 // Return the size of a section. 1369 uint64_t 1370 section_size() const 1371 { return this->object_->section_size(this->shndx_); } 1372 1373 // Return section index of this stub section. 1374 unsigned int 1375 shndx() const 1376 { return this->shndx_; } 1377 1378 // Return symbol index, if stub is for a local function. 1379 unsigned int 1380 r_sym() const 1381 { return this->r_sym_; } 1382 1383 // Return symbol, if stub is for a global function. 1384 Mips_symbol<size>* 1385 gsym() const 1386 { return this->gsym_; } 1387 1388 // Return whether stub is for a local function. 1389 bool 1390 is_for_local_function() const 1391 { return this->gsym_ == NULL; } 1392 1393 // This method is called when a new relocation R_TYPE for local symbol R_SYM 1394 // is found in the stub section. Try to find stub target. 1395 void 1396 new_local_reloc_found(unsigned int r_type, unsigned int r_sym) 1397 { 1398 // To find target symbol for this stub, trust the first R_MIPS_NONE 1399 // relocation, if any. Otherwise trust the first relocation, whatever 1400 // its kind. 1401 if (this->found_r_mips_none_) 1402 return; 1403 if (r_type == elfcpp::R_MIPS_NONE) 1404 { 1405 this->r_sym_ = r_sym; 1406 this->gsym_ = NULL; 1407 this->found_r_mips_none_ = true; 1408 } 1409 else if (!is_target_found()) 1410 this->r_sym_ = r_sym; 1411 } 1412 1413 // This method is called when a new relocation R_TYPE for global symbol GSYM 1414 // is found in the stub section. Try to find stub target. 1415 void 1416 new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym) 1417 { 1418 // To find target symbol for this stub, trust the first R_MIPS_NONE 1419 // relocation, if any. Otherwise trust the first relocation, whatever 1420 // its kind. 1421 if (this->found_r_mips_none_) 1422 return; 1423 if (r_type == elfcpp::R_MIPS_NONE) 1424 { 1425 this->gsym_ = gsym; 1426 this->r_sym_ = 0; 1427 this->found_r_mips_none_ = true; 1428 } 1429 else if (!is_target_found()) 1430 this->gsym_ = gsym; 1431 } 1432 1433 // Return whether we found the stub target. 1434 bool 1435 is_target_found() const 1436 { return this->r_sym_ != 0 || this->gsym_ != NULL; } 1437 1438 // Return whether this is a fn stub. 1439 bool 1440 is_fn_stub() const 1441 { return this->object_->is_mips16_fn_stub_section(this->shndx_); } 1442 1443 // Return whether this is a call stub. 1444 bool 1445 is_call_stub() const 1446 { return this->object_->is_mips16_call_stub_section(this->shndx_); } 1447 1448 // Return whether this is a call_fp stub. 1449 bool 1450 is_call_fp_stub() const 1451 { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); } 1452 1453 // Return the output address. 1454 Mips_address 1455 output_address() const 1456 { 1457 return (this->object_->output_section(this->shndx_)->address() 1458 + this->object_->output_section_offset(this->shndx_)); 1459 } 1460 1461 private: 1462 // The object of this stub section. 1463 Mips_relobj<size, big_endian>* object_; 1464 // The section index of this stub section. 1465 unsigned int shndx_; 1466 // The symbol index, if stub is for a local function. 1467 unsigned int r_sym_; 1468 // The symbol, if stub is for a global function. 1469 Mips_symbol<size>* gsym_; 1470 // True if we found R_MIPS_NONE relocation in this stub. 1471 bool found_r_mips_none_; 1472 }; 1473 1474 // Mips_relobj class. 1475 1476 template<int size, bool big_endian> 1477 class Mips_relobj : public Sized_relobj_file<size, big_endian> 1478 { 1479 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; 1480 typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*> 1481 Mips16_stubs_int_map; 1482 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype; 1483 1484 public: 1485 Mips_relobj(const std::string& name, Input_file* input_file, off_t offset, 1486 const typename elfcpp::Ehdr<size, big_endian>& ehdr) 1487 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr), 1488 processor_specific_flags_(0), local_symbol_is_mips16_(), 1489 local_symbol_is_micromips_(), mips16_stub_sections_(), 1490 local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(), 1491 local_mips16_call_stubs_(), gp_(0), got_info_(NULL), 1492 section_is_mips16_fn_stub_(), section_is_mips16_call_stub_(), 1493 section_is_mips16_call_fp_stub_(), pdr_shndx_(-1U), gprmask_(0), 1494 cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0) 1495 { 1496 this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0; 1497 this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags()); 1498 this->is_n64_ = elfcpp::abi_64(ehdr.get_e_ident()[elfcpp::EI_CLASS]); 1499 } 1500 1501 ~Mips_relobj() 1502 { } 1503 1504 // Downcast a base pointer to a Mips_relobj pointer. This is 1505 // not type-safe but we only use Mips_relobj not the base class. 1506 static Mips_relobj<size, big_endian>* 1507 as_mips_relobj(Relobj* relobj) 1508 { return static_cast<Mips_relobj<size, big_endian>*>(relobj); } 1509 1510 // Downcast a base pointer to a Mips_relobj pointer. This is 1511 // not type-safe but we only use Mips_relobj not the base class. 1512 static const Mips_relobj<size, big_endian>* 1513 as_mips_relobj(const Relobj* relobj) 1514 { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); } 1515 1516 // Processor-specific flags in ELF file header. This is valid only after 1517 // reading symbols. 1518 elfcpp::Elf_Word 1519 processor_specific_flags() const 1520 { return this->processor_specific_flags_; } 1521 1522 // Whether a local symbol is MIPS16 symbol. R_SYM is the symbol table 1523 // index. This is only valid after do_count_local_symbol is called. 1524 bool 1525 local_symbol_is_mips16(unsigned int r_sym) const 1526 { 1527 gold_assert(r_sym < this->local_symbol_is_mips16_.size()); 1528 return this->local_symbol_is_mips16_[r_sym]; 1529 } 1530 1531 // Whether a local symbol is microMIPS symbol. R_SYM is the symbol table 1532 // index. This is only valid after do_count_local_symbol is called. 1533 bool 1534 local_symbol_is_micromips(unsigned int r_sym) const 1535 { 1536 gold_assert(r_sym < this->local_symbol_is_micromips_.size()); 1537 return this->local_symbol_is_micromips_[r_sym]; 1538 } 1539 1540 // Get or create MIPS16 stub section. 1541 Mips16_stub_section<size, big_endian>* 1542 get_mips16_stub_section(unsigned int shndx) 1543 { 1544 typename Mips16_stubs_int_map::const_iterator it = 1545 this->mips16_stub_sections_.find(shndx); 1546 if (it != this->mips16_stub_sections_.end()) 1547 return (*it).second; 1548 1549 Mips16_stub_section<size, big_endian>* stub_section = 1550 new Mips16_stub_section<size, big_endian>(this, shndx); 1551 this->mips16_stub_sections_.insert( 1552 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>( 1553 stub_section->shndx(), stub_section)); 1554 return stub_section; 1555 } 1556 1557 // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this 1558 // object doesn't have fn stub for R_SYM. 1559 Mips16_stub_section<size, big_endian>* 1560 get_local_mips16_fn_stub(unsigned int r_sym) const 1561 { 1562 typename Mips16_stubs_int_map::const_iterator it = 1563 this->local_mips16_fn_stubs_.find(r_sym); 1564 if (it != this->local_mips16_fn_stubs_.end()) 1565 return (*it).second; 1566 return NULL; 1567 } 1568 1569 // Record that this object has MIPS16 fn stub for local symbol. This method 1570 // is only called if we decided not to discard the stub. 1571 void 1572 add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub) 1573 { 1574 gold_assert(stub->is_for_local_function()); 1575 unsigned int r_sym = stub->r_sym(); 1576 this->local_mips16_fn_stubs_.insert( 1577 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>( 1578 r_sym, stub)); 1579 } 1580 1581 // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this 1582 // object doesn't have call stub for R_SYM. 1583 Mips16_stub_section<size, big_endian>* 1584 get_local_mips16_call_stub(unsigned int r_sym) const 1585 { 1586 typename Mips16_stubs_int_map::const_iterator it = 1587 this->local_mips16_call_stubs_.find(r_sym); 1588 if (it != this->local_mips16_call_stubs_.end()) 1589 return (*it).second; 1590 return NULL; 1591 } 1592 1593 // Record that this object has MIPS16 call stub for local symbol. This method 1594 // is only called if we decided not to discard the stub. 1595 void 1596 add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub) 1597 { 1598 gold_assert(stub->is_for_local_function()); 1599 unsigned int r_sym = stub->r_sym(); 1600 this->local_mips16_call_stubs_.insert( 1601 std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>( 1602 r_sym, stub)); 1603 } 1604 1605 // Record that we found "non 16-bit" call relocation against local symbol 1606 // SYMNDX. This reloc would need to refer to a MIPS16 fn stub, if there 1607 // is one. 1608 void 1609 add_local_non_16bit_call(unsigned int symndx) 1610 { this->local_non_16bit_calls_.insert(symndx); } 1611 1612 // Return true if there is any "non 16-bit" call relocation against local 1613 // symbol SYMNDX in this object. 1614 bool 1615 has_local_non_16bit_call_relocs(unsigned int symndx) 1616 { 1617 return (this->local_non_16bit_calls_.find(symndx) 1618 != this->local_non_16bit_calls_.end()); 1619 } 1620 1621 // Record that we found 16-bit call relocation R_MIPS16_26 against local 1622 // symbol SYMNDX. Local MIPS16 call or call_fp stubs will only be needed 1623 // if there is some R_MIPS16_26 relocation that refers to the stub symbol. 1624 void 1625 add_local_16bit_call(unsigned int symndx) 1626 { this->local_16bit_calls_.insert(symndx); } 1627 1628 // Return true if there is any 16-bit call relocation R_MIPS16_26 against local 1629 // symbol SYMNDX in this object. 1630 bool 1631 has_local_16bit_call_relocs(unsigned int symndx) 1632 { 1633 return (this->local_16bit_calls_.find(symndx) 1634 != this->local_16bit_calls_.end()); 1635 } 1636 1637 // Get gp value that was used to create this object. 1638 Mips_address 1639 gp_value() const 1640 { return this->gp_; } 1641 1642 // Return whether the object is a PIC object. 1643 bool 1644 is_pic() const 1645 { return this->is_pic_; } 1646 1647 // Return whether the object uses N32 ABI. 1648 bool 1649 is_n32() const 1650 { return this->is_n32_; } 1651 1652 // Return whether the object uses N64 ABI. 1653 bool 1654 is_n64() const 1655 { return this->is_n64_; } 1656 1657 // Return whether the object uses NewABI conventions. 1658 bool 1659 is_newabi() const 1660 { return this->is_n32_ || this->is_n64_; } 1661 1662 // Return Mips_got_info for this object. 1663 Mips_got_info<size, big_endian>* 1664 get_got_info() const 1665 { return this->got_info_; } 1666 1667 // Return Mips_got_info for this object. Create new info if it doesn't exist. 1668 Mips_got_info<size, big_endian>* 1669 get_or_create_got_info() 1670 { 1671 if (!this->got_info_) 1672 this->got_info_ = new Mips_got_info<size, big_endian>(); 1673 return this->got_info_; 1674 } 1675 1676 // Set Mips_got_info for this object. 1677 void 1678 set_got_info(Mips_got_info<size, big_endian>* got_info) 1679 { this->got_info_ = got_info; } 1680 1681 // Whether a section SHDNX is a MIPS16 stub section. This is only valid 1682 // after do_read_symbols is called. 1683 bool 1684 is_mips16_stub_section(unsigned int shndx) 1685 { 1686 return (is_mips16_fn_stub_section(shndx) 1687 || is_mips16_call_stub_section(shndx) 1688 || is_mips16_call_fp_stub_section(shndx)); 1689 } 1690 1691 // Return TRUE if relocations in section SHNDX can refer directly to a 1692 // MIPS16 function rather than to a hard-float stub. This is only valid 1693 // after do_read_symbols is called. 1694 bool 1695 section_allows_mips16_refs(unsigned int shndx) 1696 { 1697 return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_); 1698 } 1699 1700 // Whether a section SHDNX is a MIPS16 fn stub section. This is only valid 1701 // after do_read_symbols is called. 1702 bool 1703 is_mips16_fn_stub_section(unsigned int shndx) 1704 { 1705 gold_assert(shndx < this->section_is_mips16_fn_stub_.size()); 1706 return this->section_is_mips16_fn_stub_[shndx]; 1707 } 1708 1709 // Whether a section SHDNX is a MIPS16 call stub section. This is only valid 1710 // after do_read_symbols is called. 1711 bool 1712 is_mips16_call_stub_section(unsigned int shndx) 1713 { 1714 gold_assert(shndx < this->section_is_mips16_call_stub_.size()); 1715 return this->section_is_mips16_call_stub_[shndx]; 1716 } 1717 1718 // Whether a section SHDNX is a MIPS16 call_fp stub section. This is only 1719 // valid after do_read_symbols is called. 1720 bool 1721 is_mips16_call_fp_stub_section(unsigned int shndx) 1722 { 1723 gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size()); 1724 return this->section_is_mips16_call_fp_stub_[shndx]; 1725 } 1726 1727 // Discard MIPS16 stub secions that are not needed. 1728 void 1729 discard_mips16_stub_sections(Symbol_table* symtab); 1730 1731 // Return gprmask from the .reginfo section of this object. 1732 Valtype 1733 gprmask() const 1734 { return this->gprmask_; } 1735 1736 // Return cprmask1 from the .reginfo section of this object. 1737 Valtype 1738 cprmask1() const 1739 { return this->cprmask1_; } 1740 1741 // Return cprmask2 from the .reginfo section of this object. 1742 Valtype 1743 cprmask2() const 1744 { return this->cprmask2_; } 1745 1746 // Return cprmask3 from the .reginfo section of this object. 1747 Valtype 1748 cprmask3() const 1749 { return this->cprmask3_; } 1750 1751 // Return cprmask4 from the .reginfo section of this object. 1752 Valtype 1753 cprmask4() const 1754 { return this->cprmask4_; } 1755 1756 protected: 1757 // Count the local symbols. 1758 void 1759 do_count_local_symbols(Stringpool_template<char>*, 1760 Stringpool_template<char>*); 1761 1762 // Read the symbol information. 1763 void 1764 do_read_symbols(Read_symbols_data* sd); 1765 1766 private: 1767 // processor-specific flags in ELF file header. 1768 elfcpp::Elf_Word processor_specific_flags_; 1769 1770 // Bit vector to tell if a local symbol is a MIPS16 symbol or not. 1771 // This is only valid after do_count_local_symbol is called. 1772 std::vector<bool> local_symbol_is_mips16_; 1773 1774 // Bit vector to tell if a local symbol is a microMIPS symbol or not. 1775 // This is only valid after do_count_local_symbol is called. 1776 std::vector<bool> local_symbol_is_micromips_; 1777 1778 // Map from section index to the MIPS16 stub for that section. This contains 1779 // all stubs found in this object. 1780 Mips16_stubs_int_map mips16_stub_sections_; 1781 1782 // Local symbols that have "non 16-bit" call relocation. This relocation 1783 // would need to refer to a MIPS16 fn stub, if there is one. 1784 std::set<unsigned int> local_non_16bit_calls_; 1785 1786 // Local symbols that have 16-bit call relocation R_MIPS16_26. Local MIPS16 1787 // call or call_fp stubs will only be needed if there is some R_MIPS16_26 1788 // relocation that refers to the stub symbol. 1789 std::set<unsigned int> local_16bit_calls_; 1790 1791 // Map from local symbol index to the MIPS16 fn stub for that symbol. 1792 // This contains only the stubs that we decided not to discard. 1793 Mips16_stubs_int_map local_mips16_fn_stubs_; 1794 1795 // Map from local symbol index to the MIPS16 call stub for that symbol. 1796 // This contains only the stubs that we decided not to discard. 1797 Mips16_stubs_int_map local_mips16_call_stubs_; 1798 1799 // gp value that was used to create this object. 1800 Mips_address gp_; 1801 // Whether the object is a PIC object. 1802 bool is_pic_ : 1; 1803 // Whether the object uses N32 ABI. 1804 bool is_n32_ : 1; 1805 // Whether the object uses N64 ABI. 1806 bool is_n64_ : 1; 1807 // The Mips_got_info for this object. 1808 Mips_got_info<size, big_endian>* got_info_; 1809 1810 // Bit vector to tell if a section is a MIPS16 fn stub section or not. 1811 // This is only valid after do_read_symbols is called. 1812 std::vector<bool> section_is_mips16_fn_stub_; 1813 1814 // Bit vector to tell if a section is a MIPS16 call stub section or not. 1815 // This is only valid after do_read_symbols is called. 1816 std::vector<bool> section_is_mips16_call_stub_; 1817 1818 // Bit vector to tell if a section is a MIPS16 call_fp stub section or not. 1819 // This is only valid after do_read_symbols is called. 1820 std::vector<bool> section_is_mips16_call_fp_stub_; 1821 1822 // .pdr section index. 1823 unsigned int pdr_shndx_; 1824 1825 // gprmask from the .reginfo section of this object. 1826 Valtype gprmask_; 1827 // cprmask1 from the .reginfo section of this object. 1828 Valtype cprmask1_; 1829 // cprmask2 from the .reginfo section of this object. 1830 Valtype cprmask2_; 1831 // cprmask3 from the .reginfo section of this object. 1832 Valtype cprmask3_; 1833 // cprmask4 from the .reginfo section of this object. 1834 Valtype cprmask4_; 1835 }; 1836 1837 // Mips_output_data_got class. 1838 1839 template<int size, bool big_endian> 1840 class Mips_output_data_got : public Output_data_got<size, big_endian> 1841 { 1842 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; 1843 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> 1844 Reloc_section; 1845 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype; 1846 1847 public: 1848 Mips_output_data_got(Target_mips<size, big_endian>* target, 1849 Symbol_table* symtab, Layout* layout) 1850 : Output_data_got<size, big_endian>(), target_(target), 1851 symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL), 1852 first_global_got_dynsym_index_(-1U), primary_got_(NULL), 1853 secondary_got_relocs_() 1854 { 1855 this->master_got_info_ = new Mips_got_info<size, big_endian>(); 1856 this->set_addralign(16); 1857 } 1858 1859 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol 1860 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT. 1861 void 1862 record_local_got_symbol(Mips_relobj<size, big_endian>* object, 1863 unsigned int symndx, Mips_address addend, 1864 unsigned int r_type, unsigned int shndx) 1865 { 1866 this->master_got_info_->record_local_got_symbol(object, symndx, addend, 1867 r_type, shndx); 1868 } 1869 1870 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM, 1871 // in OBJECT. FOR_CALL is true if the caller is only interested in 1872 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic 1873 // relocation. 1874 void 1875 record_global_got_symbol(Mips_symbol<size>* mips_sym, 1876 Mips_relobj<size, big_endian>* object, 1877 unsigned int r_type, bool dyn_reloc, bool for_call) 1878 { 1879 this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type, 1880 dyn_reloc, for_call); 1881 } 1882 1883 // Record that OBJECT has a page relocation against symbol SYMNDX and 1884 // that ADDEND is the addend for that relocation. 1885 void 1886 record_got_page_entry(Mips_relobj<size, big_endian>* object, 1887 unsigned int symndx, int addend) 1888 { this->master_got_info_->record_got_page_entry(object, symndx, addend); } 1889 1890 // Add a static entry for the GOT entry at OFFSET. GSYM is a global 1891 // symbol and R_TYPE is the code of a dynamic relocation that needs to be 1892 // applied in a static link. 1893 void 1894 add_static_reloc(unsigned int got_offset, unsigned int r_type, 1895 Mips_symbol<size>* gsym) 1896 { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); } 1897 1898 // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object 1899 // defining a local symbol with INDEX. R_TYPE is the code of a dynamic 1900 // relocation that needs to be applied in a static link. 1901 void 1902 add_static_reloc(unsigned int got_offset, unsigned int r_type, 1903 Sized_relobj_file<size, big_endian>* relobj, 1904 unsigned int index) 1905 { 1906 this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj, 1907 index)); 1908 } 1909 1910 // Record that global symbol GSYM has R_TYPE dynamic relocation in the 1911 // secondary GOT at OFFSET. 1912 void 1913 add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type, 1914 Mips_symbol<size>* gsym) 1915 { 1916 this->secondary_got_relocs_.push_back(Static_reloc(got_offset, 1917 r_type, gsym)); 1918 } 1919 1920 // Update GOT entry at OFFSET with VALUE. 1921 void 1922 update_got_entry(unsigned int offset, Mips_address value) 1923 { 1924 elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value); 1925 } 1926 1927 // Return the number of entries in local part of the GOT. This includes 1928 // local entries, page entries and 2 reserved entries. 1929 unsigned int 1930 get_local_gotno() const 1931 { 1932 if (!this->multi_got()) 1933 { 1934 return (2 + this->master_got_info_->local_gotno() 1935 + this->master_got_info_->page_gotno()); 1936 } 1937 else 1938 return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno(); 1939 } 1940 1941 // Return dynamic symbol table index of the first symbol with global GOT 1942 // entry. 1943 unsigned int 1944 first_global_got_dynsym_index() const 1945 { return this->first_global_got_dynsym_index_; } 1946 1947 // Set dynamic symbol table index of the first symbol with global GOT entry. 1948 void 1949 set_first_global_got_dynsym_index(unsigned int index) 1950 { this->first_global_got_dynsym_index_ = index; } 1951 1952 // Lay out the GOT. Add local, global and TLS entries. If GOT is 1953 // larger than 64K, create multi-GOT. 1954 void 1955 lay_out_got(Layout* layout, Symbol_table* symtab, 1956 const Input_objects* input_objects); 1957 1958 // Create multi-GOT. For every GOT, add local, global and TLS entries. 1959 void 1960 lay_out_multi_got(Layout* layout, const Input_objects* input_objects); 1961 1962 // Attempt to merge GOTs of different input objects. 1963 void 1964 merge_gots(const Input_objects* input_objects); 1965 1966 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if 1967 // this would lead to overflow, true if they were merged successfully. 1968 bool 1969 merge_got_with(Mips_got_info<size, big_endian>* from, 1970 Mips_relobj<size, big_endian>* object, 1971 Mips_got_info<size, big_endian>* to); 1972 1973 // Return the offset of GOT page entry for VALUE. For multi-GOT links, 1974 // use OBJECT's GOT. 1975 unsigned int 1976 get_got_page_offset(Mips_address value, 1977 const Mips_relobj<size, big_endian>* object) 1978 { 1979 Mips_got_info<size, big_endian>* g = (!this->multi_got() 1980 ? this->master_got_info_ 1981 : object->get_got_info()); 1982 gold_assert(g != NULL); 1983 return g->get_got_page_offset(value, this); 1984 } 1985 1986 // Return the GOT offset of type GOT_TYPE of the global symbol 1987 // GSYM. For multi-GOT links, use OBJECT's GOT. 1988 unsigned int got_offset(const Symbol* gsym, unsigned int got_type, 1989 Mips_relobj<size, big_endian>* object) const 1990 { 1991 if (!this->multi_got()) 1992 return gsym->got_offset(got_type); 1993 else 1994 { 1995 Mips_got_info<size, big_endian>* g = object->get_got_info(); 1996 gold_assert(g != NULL); 1997 return gsym->got_offset(g->multigot_got_type(got_type)); 1998 } 1999 } 2000 2001 // Return the GOT offset of type GOT_TYPE of the local symbol 2002 // SYMNDX. 2003 unsigned int 2004 got_offset(unsigned int symndx, unsigned int got_type, 2005 Sized_relobj_file<size, big_endian>* object) const 2006 { return object->local_got_offset(symndx, got_type); } 2007 2008 // Return the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT. 2009 unsigned int 2010 tls_ldm_offset(Mips_relobj<size, big_endian>* object) const 2011 { 2012 Mips_got_info<size, big_endian>* g = (!this->multi_got() 2013 ? this->master_got_info_ 2014 : object->get_got_info()); 2015 gold_assert(g != NULL); 2016 return g->tls_ldm_offset(); 2017 } 2018 2019 // Set the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT. 2020 void 2021 set_tls_ldm_offset(unsigned int tls_ldm_offset, 2022 Mips_relobj<size, big_endian>* object) 2023 { 2024 Mips_got_info<size, big_endian>* g = (!this->multi_got() 2025 ? this->master_got_info_ 2026 : object->get_got_info()); 2027 gold_assert(g != NULL); 2028 g->set_tls_ldm_offset(tls_ldm_offset); 2029 } 2030 2031 // Return true for multi-GOT links. 2032 bool 2033 multi_got() const 2034 { return this->primary_got_ != NULL; } 2035 2036 // Return the offset of OBJECT's GOT from the start of .got section. 2037 unsigned int 2038 get_got_offset(const Mips_relobj<size, big_endian>* object) 2039 { 2040 if (!this->multi_got()) 2041 return 0; 2042 else 2043 { 2044 Mips_got_info<size, big_endian>* g = object->get_got_info(); 2045 return g != NULL ? g->offset() : 0; 2046 } 2047 } 2048 2049 // Create global GOT entries that should be in the GGA_RELOC_ONLY area. 2050 void 2051 add_reloc_only_entries() 2052 { this->master_got_info_->add_reloc_only_entries(this); } 2053 2054 // Return offset of the primary GOT's entry for global symbol. 2055 unsigned int 2056 get_primary_got_offset(const Mips_symbol<size>* sym) const 2057 { 2058 gold_assert(sym->global_got_area() != GGA_NONE); 2059 return (this->get_local_gotno() + sym->dynsym_index() 2060 - this->first_global_got_dynsym_index()) * size/8; 2061 } 2062 2063 // For the entry at offset GOT_OFFSET, return its offset from the gp. 2064 // Input argument GOT_OFFSET is always global offset from the start of 2065 // .got section, for both single and multi-GOT links. 2066 // For single GOT links, this returns GOT_OFFSET - 0x7FF0. For multi-GOT 2067 // links, the return value is object_got_offset - 0x7FF0, where 2068 // object_got_offset is offset in the OBJECT's GOT. 2069 int 2070 gp_offset(unsigned int got_offset, 2071 const Mips_relobj<size, big_endian>* object) const 2072 { 2073 return (this->address() + got_offset 2074 - this->target_->adjusted_gp_value(object)); 2075 } 2076 2077 protected: 2078 // Write out the GOT table. 2079 void 2080 do_write(Output_file*); 2081 2082 private: 2083 2084 // This class represent dynamic relocations that need to be applied by 2085 // gold because we are using TLS relocations in a static link. 2086 class Static_reloc 2087 { 2088 public: 2089 Static_reloc(unsigned int got_offset, unsigned int r_type, 2090 Mips_symbol<size>* gsym) 2091 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true) 2092 { this->u_.global.symbol = gsym; } 2093 2094 Static_reloc(unsigned int got_offset, unsigned int r_type, 2095 Sized_relobj_file<size, big_endian>* relobj, unsigned int index) 2096 : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false) 2097 { 2098 this->u_.local.relobj = relobj; 2099 this->u_.local.index = index; 2100 } 2101 2102 // Return the GOT offset. 2103 unsigned int 2104 got_offset() const 2105 { return this->got_offset_; } 2106 2107 // Relocation type. 2108 unsigned int 2109 r_type() const 2110 { return this->r_type_; } 2111 2112 // Whether the symbol is global or not. 2113 bool 2114 symbol_is_global() const 2115 { return this->symbol_is_global_; } 2116 2117 // For a relocation against a global symbol, the global symbol. 2118 Mips_symbol<size>* 2119 symbol() const 2120 { 2121 gold_assert(this->symbol_is_global_); 2122 return this->u_.global.symbol; 2123 } 2124 2125 // For a relocation against a local symbol, the defining object. 2126 Sized_relobj_file<size, big_endian>* 2127 relobj() const 2128 { 2129 gold_assert(!this->symbol_is_global_); 2130 return this->u_.local.relobj; 2131 } 2132 2133 // For a relocation against a local symbol, the local symbol index. 2134 unsigned int 2135 index() const 2136 { 2137 gold_assert(!this->symbol_is_global_); 2138 return this->u_.local.index; 2139 } 2140 2141 private: 2142 // GOT offset of the entry to which this relocation is applied. 2143 unsigned int got_offset_; 2144 // Type of relocation. 2145 unsigned int r_type_; 2146 // Whether this relocation is against a global symbol. 2147 bool symbol_is_global_; 2148 // A global or local symbol. 2149 union 2150 { 2151 struct 2152 { 2153 // For a global symbol, the symbol itself. 2154 Mips_symbol<size>* symbol; 2155 } global; 2156 struct 2157 { 2158 // For a local symbol, the object defining object. 2159 Sized_relobj_file<size, big_endian>* relobj; 2160 // For a local symbol, the symbol index. 2161 unsigned int index; 2162 } local; 2163 } u_; 2164 }; 2165 2166 // The target. 2167 Target_mips<size, big_endian>* target_; 2168 // The symbol table. 2169 Symbol_table* symbol_table_; 2170 // The layout. 2171 Layout* layout_; 2172 // Static relocs to be applied to the GOT. 2173 std::vector<Static_reloc> static_relocs_; 2174 // .got section view. 2175 unsigned char* got_view_; 2176 // The dynamic symbol table index of the first symbol with global GOT entry. 2177 unsigned int first_global_got_dynsym_index_; 2178 // The master GOT information. 2179 Mips_got_info<size, big_endian>* master_got_info_; 2180 // The primary GOT information. 2181 Mips_got_info<size, big_endian>* primary_got_; 2182 // Secondary GOT fixups. 2183 std::vector<Static_reloc> secondary_got_relocs_; 2184 }; 2185 2186 // A class to handle LA25 stubs - non-PIC interface to a PIC function. There are 2187 // two ways of creating these interfaces. The first is to add: 2188 // 2189 // lui $25,%hi(func) 2190 // j func 2191 // addiu $25,$25,%lo(func) 2192 // 2193 // to a separate trampoline section. The second is to add: 2194 // 2195 // lui $25,%hi(func) 2196 // addiu $25,$25,%lo(func) 2197 // 2198 // immediately before a PIC function "func", but only if a function is at the 2199 // beginning of the section, and the section is not too heavily aligned (i.e we 2200 // would need to add no more than 2 nops before the stub.) 2201 // 2202 // We only create stubs of the first type. 2203 2204 template<int size, bool big_endian> 2205 class Mips_output_data_la25_stub : public Output_section_data 2206 { 2207 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; 2208 2209 public: 2210 Mips_output_data_la25_stub() 2211 : Output_section_data(size == 32 ? 4 : 8), symbols_() 2212 { } 2213 2214 // Create LA25 stub for a symbol. 2215 void 2216 create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target, 2217 Mips_symbol<size>* gsym); 2218 2219 // Return output address of a stub. 2220 Mips_address 2221 stub_address(const Mips_symbol<size>* sym) const 2222 { 2223 gold_assert(sym->has_la25_stub()); 2224 return this->address() + sym->la25_stub_offset(); 2225 } 2226 2227 protected: 2228 void 2229 do_adjust_output_section(Output_section* os) 2230 { os->set_entsize(0); } 2231 2232 private: 2233 // Template for standard LA25 stub. 2234 static const uint32_t la25_stub_entry[]; 2235 // Template for microMIPS LA25 stub. 2236 static const uint32_t la25_stub_micromips_entry[]; 2237 2238 // Set the final size. 2239 void 2240 set_final_data_size() 2241 { this->set_data_size(this->symbols_.size() * 16); } 2242 2243 // Create a symbol for SYM stub's value and size, to help make the 2244 // disassembly easier to read. 2245 void 2246 create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab, 2247 Target_mips<size, big_endian>* target, uint64_t symsize); 2248 2249 // Write out the LA25 stub section. 2250 void 2251 do_write(Output_file*); 2252 2253 // Symbols that have LA25 stubs. 2254 Unordered_set<Mips_symbol<size>*> symbols_; 2255 }; 2256 2257 // A class to handle the PLT data. 2258 2259 template<int size, bool big_endian> 2260 class Mips_output_data_plt : public Output_section_data 2261 { 2262 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; 2263 typedef Output_data_reloc<elfcpp::SHT_REL, true, 2264 size, big_endian> Reloc_section; 2265 2266 public: 2267 // Create the PLT section. The ordinary .got section is an argument, 2268 // since we need to refer to the start. 2269 Mips_output_data_plt(Layout* layout, Output_data_space* got_plt, 2270 Target_mips<size, big_endian>* target) 2271 : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(), 2272 plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0), 2273 target_(target) 2274 { 2275 this->rel_ = new Reloc_section(false); 2276 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL, 2277 elfcpp::SHF_ALLOC, this->rel_, 2278 ORDER_DYNAMIC_PLT_RELOCS, false); 2279 } 2280 2281 // Add an entry to the PLT for a symbol referenced by r_type relocation. 2282 void 2283 add_entry(Mips_symbol<size>* gsym, unsigned int r_type); 2284 2285 // Return the .rel.plt section data. 2286 const Reloc_section* 2287 rel_plt() const 2288 { return this->rel_; } 2289 2290 // Return the number of PLT entries. 2291 unsigned int 2292 entry_count() const 2293 { return this->symbols_.size(); } 2294 2295 // Return the offset of the first non-reserved PLT entry. 2296 unsigned int 2297 first_plt_entry_offset() const 2298 { return sizeof(plt0_entry_o32); } 2299 2300 // Return the size of a PLT entry. 2301 unsigned int 2302 plt_entry_size() const 2303 { return sizeof(plt_entry); } 2304 2305 // Set final PLT offsets. For each symbol, determine whether standard or 2306 // compressed (MIPS16 or microMIPS) PLT entry is used. 2307 void 2308 set_plt_offsets(); 2309 2310 // Return the offset of the first standard PLT entry. 2311 unsigned int 2312 first_mips_plt_offset() const 2313 { return this->plt_header_size_; } 2314 2315 // Return the offset of the first compressed PLT entry. 2316 unsigned int 2317 first_comp_plt_offset() const 2318 { return this->plt_header_size_ + this->plt_mips_offset_; } 2319 2320 // Return whether there are any standard PLT entries. 2321 bool 2322 has_standard_entries() const 2323 { return this->plt_mips_offset_ > 0; } 2324 2325 // Return the output address of standard PLT entry. 2326 Mips_address 2327 mips_entry_address(const Mips_symbol<size>* sym) const 2328 { 2329 gold_assert (sym->has_mips_plt_offset()); 2330 return (this->address() + this->first_mips_plt_offset() 2331 + sym->mips_plt_offset()); 2332 } 2333 2334 // Return the output address of compressed (MIPS16 or microMIPS) PLT entry. 2335 Mips_address 2336 comp_entry_address(const Mips_symbol<size>* sym) const 2337 { 2338 gold_assert (sym->has_comp_plt_offset()); 2339 return (this->address() + this->first_comp_plt_offset() 2340 + sym->comp_plt_offset()); 2341 } 2342 2343 protected: 2344 void 2345 do_adjust_output_section(Output_section* os) 2346 { os->set_entsize(0); } 2347 2348 // Write to a map file. 2349 void 2350 do_print_to_mapfile(Mapfile* mapfile) const 2351 { mapfile->print_output_data(this, _(".plt")); } 2352 2353 private: 2354 // Template for the first PLT entry. 2355 static const uint32_t plt0_entry_o32[]; 2356 static const uint32_t plt0_entry_n32[]; 2357 static const uint32_t plt0_entry_n64[]; 2358 static const uint32_t plt0_entry_micromips_o32[]; 2359 static const uint32_t plt0_entry_micromips32_o32[]; 2360 2361 // Template for subsequent PLT entries. 2362 static const uint32_t plt_entry[]; 2363 static const uint32_t plt_entry_mips16_o32[]; 2364 static const uint32_t plt_entry_micromips_o32[]; 2365 static const uint32_t plt_entry_micromips32_o32[]; 2366 2367 // Set the final size. 2368 void 2369 set_final_data_size() 2370 { 2371 this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_ 2372 + this->plt_comp_offset_); 2373 } 2374 2375 // Write out the PLT data. 2376 void 2377 do_write(Output_file*); 2378 2379 // Return whether the plt header contains microMIPS code. For the sake of 2380 // cache alignment always use a standard header whenever any standard entries 2381 // are present even if microMIPS entries are present as well. This also lets 2382 // the microMIPS header rely on the value of $v0 only set by microMIPS 2383 // entries, for a small size reduction. 2384 bool 2385 is_plt_header_compressed() const 2386 { 2387 gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0); 2388 return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0; 2389 } 2390 2391 // Return the size of the PLT header. 2392 unsigned int 2393 get_plt_header_size() const 2394 { 2395 if (this->target_->is_output_n64()) 2396 return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]); 2397 else if (this->target_->is_output_n32()) 2398 return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]); 2399 else if (!this->is_plt_header_compressed()) 2400 return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]); 2401 else if (this->target_->use_32bit_micromips_instructions()) 2402 return (2 * sizeof(plt0_entry_micromips32_o32) 2403 / sizeof(plt0_entry_micromips32_o32[0])); 2404 else 2405 return (2 * sizeof(plt0_entry_micromips_o32) 2406 / sizeof(plt0_entry_micromips_o32[0])); 2407 } 2408 2409 // Return the PLT header entry. 2410 const uint32_t* 2411 get_plt_header_entry() const 2412 { 2413 if (this->target_->is_output_n64()) 2414 return plt0_entry_n64; 2415 else if (this->target_->is_output_n32()) 2416 return plt0_entry_n32; 2417 else if (!this->is_plt_header_compressed()) 2418 return plt0_entry_o32; 2419 else if (this->target_->use_32bit_micromips_instructions()) 2420 return plt0_entry_micromips32_o32; 2421 else 2422 return plt0_entry_micromips_o32; 2423 } 2424 2425 // Return the size of the standard PLT entry. 2426 unsigned int 2427 standard_plt_entry_size() const 2428 { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); } 2429 2430 // Return the size of the compressed PLT entry. 2431 unsigned int 2432 compressed_plt_entry_size() const 2433 { 2434 gold_assert(!this->target_->is_output_newabi()); 2435 2436 if (!this->target_->is_output_micromips()) 2437 return (2 * sizeof(plt_entry_mips16_o32) 2438 / sizeof(plt_entry_mips16_o32[0])); 2439 else if (this->target_->use_32bit_micromips_instructions()) 2440 return (2 * sizeof(plt_entry_micromips32_o32) 2441 / sizeof(plt_entry_micromips32_o32[0])); 2442 else 2443 return (2 * sizeof(plt_entry_micromips_o32) 2444 / sizeof(plt_entry_micromips_o32[0])); 2445 } 2446 2447 // The reloc section. 2448 Reloc_section* rel_; 2449 // The .got.plt section. 2450 Output_data_space* got_plt_; 2451 // Symbols that have PLT entry. 2452 std::vector<Mips_symbol<size>*> symbols_; 2453 // The offset of the next standard PLT entry to create. 2454 unsigned int plt_mips_offset_; 2455 // The offset of the next compressed PLT entry to create. 2456 unsigned int plt_comp_offset_; 2457 // The size of the PLT header in bytes. 2458 unsigned int plt_header_size_; 2459 // The target. 2460 Target_mips<size, big_endian>* target_; 2461 }; 2462 2463 // A class to handle the .MIPS.stubs data. 2464 2465 template<int size, bool big_endian> 2466 class Mips_output_data_mips_stubs : public Output_section_data 2467 { 2468 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; 2469 2470 public: 2471 Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target) 2472 : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U), 2473 stub_offsets_are_set_(false), target_(target) 2474 { } 2475 2476 // Create entry for a symbol. 2477 void 2478 make_entry(Mips_symbol<size>*); 2479 2480 // Remove entry for a symbol. 2481 void 2482 remove_entry(Mips_symbol<size>* gsym); 2483 2484 // Set stub offsets for symbols. This method expects that the number of 2485 // entries in dynamic symbol table is set. 2486 void 2487 set_lazy_stub_offsets(); 2488 2489 void 2490 set_needs_dynsym_value(); 2491 2492 // Set the number of entries in dynamic symbol table. 2493 void 2494 set_dynsym_count(unsigned int dynsym_count) 2495 { this->dynsym_count_ = dynsym_count; } 2496 2497 // Return maximum size of the stub, ie. the stub size if the dynamic symbol 2498 // count is greater than 0x10000. If the dynamic symbol count is less than 2499 // 0x10000, the stub will be 4 bytes smaller. 2500 // There's no disadvantage from using microMIPS code here, so for the sake of 2501 // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in 2502 // output produced at all. This has a benefit of stubs being shorter by 2503 // 4 bytes each too, unless in the insn32 mode. 2504 unsigned int 2505 stub_max_size() const 2506 { 2507 if (!this->target_->is_output_micromips() 2508 || this->target_->use_32bit_micromips_instructions()) 2509 return 20; 2510 else 2511 return 16; 2512 } 2513 2514 // Return the size of the stub. This method expects that the final dynsym 2515 // count is set. 2516 unsigned int 2517 stub_size() const 2518 { 2519 gold_assert(this->dynsym_count_ != -1U); 2520 if (this->dynsym_count_ > 0x10000) 2521 return this->stub_max_size(); 2522 else 2523 return this->stub_max_size() - 4; 2524 } 2525 2526 // Return output address of a stub. 2527 Mips_address 2528 stub_address(const Mips_symbol<size>* sym) const 2529 { 2530 gold_assert(sym->has_lazy_stub()); 2531 return this->address() + sym->lazy_stub_offset(); 2532 } 2533 2534 protected: 2535 void 2536 do_adjust_output_section(Output_section* os) 2537 { os->set_entsize(0); } 2538 2539 // Write to a map file. 2540 void 2541 do_print_to_mapfile(Mapfile* mapfile) const 2542 { mapfile->print_output_data(this, _(".MIPS.stubs")); } 2543 2544 private: 2545 static const uint32_t lazy_stub_normal_1[]; 2546 static const uint32_t lazy_stub_normal_1_n64[]; 2547 static const uint32_t lazy_stub_normal_2[]; 2548 static const uint32_t lazy_stub_normal_2_n64[]; 2549 static const uint32_t lazy_stub_big[]; 2550 static const uint32_t lazy_stub_big_n64[]; 2551 2552 static const uint32_t lazy_stub_micromips_normal_1[]; 2553 static const uint32_t lazy_stub_micromips_normal_1_n64[]; 2554 static const uint32_t lazy_stub_micromips_normal_2[]; 2555 static const uint32_t lazy_stub_micromips_normal_2_n64[]; 2556 static const uint32_t lazy_stub_micromips_big[]; 2557 static const uint32_t lazy_stub_micromips_big_n64[]; 2558 2559 static const uint32_t lazy_stub_micromips32_normal_1[]; 2560 static const uint32_t lazy_stub_micromips32_normal_1_n64[]; 2561 static const uint32_t lazy_stub_micromips32_normal_2[]; 2562 static const uint32_t lazy_stub_micromips32_normal_2_n64[]; 2563 static const uint32_t lazy_stub_micromips32_big[]; 2564 static const uint32_t lazy_stub_micromips32_big_n64[]; 2565 2566 // Set the final size. 2567 void 2568 set_final_data_size() 2569 { this->set_data_size(this->symbols_.size() * this->stub_max_size()); } 2570 2571 // Write out the .MIPS.stubs data. 2572 void 2573 do_write(Output_file*); 2574 2575 // .MIPS.stubs symbols 2576 Unordered_set<Mips_symbol<size>*> symbols_; 2577 // Number of entries in dynamic symbol table. 2578 unsigned int dynsym_count_; 2579 // Whether the stub offsets are set. 2580 bool stub_offsets_are_set_; 2581 // The target. 2582 Target_mips<size, big_endian>* target_; 2583 }; 2584 2585 // This class handles Mips .reginfo output section. 2586 2587 template<int size, bool big_endian> 2588 class Mips_output_section_reginfo : public Output_section 2589 { 2590 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype; 2591 2592 public: 2593 Mips_output_section_reginfo(const char* name, elfcpp::Elf_Word type, 2594 elfcpp::Elf_Xword flags, 2595 Target_mips<size, big_endian>* target) 2596 : Output_section(name, type, flags), target_(target), gprmask_(0), 2597 cprmask1_(0), cprmask2_(0), cprmask3_(0), cprmask4_(0) 2598 { } 2599 2600 // Downcast a base pointer to a Mips_output_section_reginfo pointer. 2601 static Mips_output_section_reginfo<size, big_endian>* 2602 as_mips_output_section_reginfo(Output_section* os) 2603 { return static_cast<Mips_output_section_reginfo<size, big_endian>*>(os); } 2604 2605 // Set masks of the output .reginfo section. 2606 void 2607 set_masks(Valtype gprmask, Valtype cprmask1, Valtype cprmask2, 2608 Valtype cprmask3, Valtype cprmask4) 2609 { 2610 this->gprmask_ = gprmask; 2611 this->cprmask1_ = cprmask1; 2612 this->cprmask2_ = cprmask2; 2613 this->cprmask3_ = cprmask3; 2614 this->cprmask4_ = cprmask4; 2615 } 2616 2617 protected: 2618 // Set the final data size. 2619 void 2620 set_final_data_size() 2621 { this->set_data_size(24); } 2622 2623 // Write out reginfo section. 2624 void 2625 do_write(Output_file* of); 2626 2627 private: 2628 Target_mips<size, big_endian>* target_; 2629 2630 // gprmask of the output .reginfo section. 2631 Valtype gprmask_; 2632 // cprmask1 of the output .reginfo section. 2633 Valtype cprmask1_; 2634 // cprmask2 of the output .reginfo section. 2635 Valtype cprmask2_; 2636 // cprmask3 of the output .reginfo section. 2637 Valtype cprmask3_; 2638 // cprmask4 of the output .reginfo section. 2639 Valtype cprmask4_; 2640 }; 2641 2642 // The MIPS target has relocation types which default handling of relocatable 2643 // relocation cannot process. So we have to extend the default code. 2644 2645 template<bool big_endian, int sh_type, typename Classify_reloc> 2646 class Mips_scan_relocatable_relocs : 2647 public Default_scan_relocatable_relocs<sh_type, Classify_reloc> 2648 { 2649 public: 2650 // Return the strategy to use for a local symbol which is a section 2651 // symbol, given the relocation type. 2652 inline Relocatable_relocs::Reloc_strategy 2653 local_section_strategy(unsigned int r_type, Relobj* object) 2654 { 2655 if (sh_type == elfcpp::SHT_RELA) 2656 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA; 2657 else 2658 { 2659 switch (r_type) 2660 { 2661 case elfcpp::R_MIPS_26: 2662 return Relocatable_relocs::RELOC_SPECIAL; 2663 2664 default: 2665 return Default_scan_relocatable_relocs<sh_type, Classify_reloc>:: 2666 local_section_strategy(r_type, object); 2667 } 2668 } 2669 } 2670 }; 2671 2672 // Mips_copy_relocs class. The only difference from the base class is the 2673 // method emit_mips, which should be called instead of Copy_reloc_entry::emit. 2674 // Mips cannot convert all relocation types to dynamic relocs. If a reloc 2675 // cannot be made dynamic, a COPY reloc is emitted. 2676 2677 template<int sh_type, int size, bool big_endian> 2678 class Mips_copy_relocs : public Copy_relocs<sh_type, size, big_endian> 2679 { 2680 public: 2681 Mips_copy_relocs() 2682 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_MIPS_COPY) 2683 { } 2684 2685 // Emit any saved relocations which turn out to be needed. This is 2686 // called after all the relocs have been scanned. 2687 void 2688 emit_mips(Output_data_reloc<sh_type, true, size, big_endian>*, 2689 Symbol_table*, Layout*, Target_mips<size, big_endian>*); 2690 2691 private: 2692 typedef typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry 2693 Copy_reloc_entry; 2694 2695 // Emit this reloc if appropriate. This is called after we have 2696 // scanned all the relocations, so we know whether we emitted a 2697 // COPY relocation for SYM_. 2698 void 2699 emit_entry(Copy_reloc_entry& entry, 2700 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section, 2701 Symbol_table* symtab, Layout* layout, 2702 Target_mips<size, big_endian>* target); 2703 }; 2704 2705 2706 // Return true if the symbol SYM should be considered to resolve local 2707 // to the current module, and false otherwise. The logic is taken from 2708 // GNU ld's method _bfd_elf_symbol_refs_local_p. 2709 static bool 2710 symbol_refs_local(const Symbol* sym, bool has_dynsym_entry, 2711 bool local_protected) 2712 { 2713 // If it's a local sym, of course we resolve locally. 2714 if (sym == NULL) 2715 return true; 2716 2717 // STV_HIDDEN or STV_INTERNAL ones must be local. 2718 if (sym->visibility() == elfcpp::STV_HIDDEN 2719 || sym->visibility() == elfcpp::STV_INTERNAL) 2720 return true; 2721 2722 // If we don't have a definition in a regular file, then we can't 2723 // resolve locally. The sym is either undefined or dynamic. 2724 if (sym->source() != Symbol::FROM_OBJECT || sym->object()->is_dynamic() 2725 || sym->is_undefined()) 2726 return false; 2727 2728 // Forced local symbols resolve locally. 2729 if (sym->is_forced_local()) 2730 return true; 2731 2732 // As do non-dynamic symbols. 2733 if (!has_dynsym_entry) 2734 return true; 2735 2736 // At this point, we know the symbol is defined and dynamic. In an 2737 // executable it must resolve locally, likewise when building symbolic 2738 // shared libraries. 2739 if (parameters->options().output_is_executable() 2740 || parameters->options().Bsymbolic()) 2741 return true; 2742 2743 // Now deal with defined dynamic symbols in shared libraries. Ones 2744 // with default visibility might not resolve locally. 2745 if (sym->visibility() == elfcpp::STV_DEFAULT) 2746 return false; 2747 2748 // STV_PROTECTED non-function symbols are local. 2749 if (sym->type() != elfcpp::STT_FUNC) 2750 return true; 2751 2752 // Function pointer equality tests may require that STV_PROTECTED 2753 // symbols be treated as dynamic symbols. If the address of a 2754 // function not defined in an executable is set to that function's 2755 // plt entry in the executable, then the address of the function in 2756 // a shared library must also be the plt entry in the executable. 2757 return local_protected; 2758 } 2759 2760 // Return TRUE if references to this symbol always reference the symbol in this 2761 // object. 2762 static bool 2763 symbol_references_local(const Symbol* sym, bool has_dynsym_entry) 2764 { 2765 return symbol_refs_local(sym, has_dynsym_entry, false); 2766 } 2767 2768 // Return TRUE if calls to this symbol always call the version in this object. 2769 static bool 2770 symbol_calls_local(const Symbol* sym, bool has_dynsym_entry) 2771 { 2772 return symbol_refs_local(sym, has_dynsym_entry, true); 2773 } 2774 2775 // Compare GOT offsets of two symbols. 2776 2777 template<int size, bool big_endian> 2778 static bool 2779 got_offset_compare(Symbol* sym1, Symbol* sym2) 2780 { 2781 Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1); 2782 Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2); 2783 unsigned int area1 = mips_sym1->global_got_area(); 2784 unsigned int area2 = mips_sym2->global_got_area(); 2785 gold_assert(area1 != GGA_NONE && area1 != GGA_NONE); 2786 2787 // GGA_NORMAL entries always come before GGA_RELOC_ONLY. 2788 if (area1 != area2) 2789 return area1 < area2; 2790 2791 return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset(); 2792 } 2793 2794 // This method divides dynamic symbols into symbols that have GOT entry, and 2795 // symbols that don't have GOT entry. It also sorts symbols with the GOT entry. 2796 // Mips ABI requires that symbols with the GOT entry must be at the end of 2797 // dynamic symbol table, and the order in dynamic symbol table must match the 2798 // order in GOT. 2799 2800 template<int size, bool big_endian> 2801 static void 2802 reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols, 2803 std::vector<Symbol*>* non_got_symbols, 2804 std::vector<Symbol*>* got_symbols) 2805 { 2806 for (std::vector<Symbol*>::iterator p = dyn_symbols->begin(); 2807 p != dyn_symbols->end(); 2808 ++p) 2809 { 2810 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p); 2811 if (mips_sym->global_got_area() == GGA_NORMAL 2812 || mips_sym->global_got_area() == GGA_RELOC_ONLY) 2813 got_symbols->push_back(mips_sym); 2814 else 2815 non_got_symbols->push_back(mips_sym); 2816 } 2817 2818 std::sort(got_symbols->begin(), got_symbols->end(), 2819 got_offset_compare<size, big_endian>); 2820 } 2821 2822 // Functor class for processing the global symbol table. 2823 2824 template<int size, bool big_endian> 2825 class Symbol_visitor_check_symbols 2826 { 2827 public: 2828 Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target, 2829 Layout* layout, Symbol_table* symtab) 2830 : target_(target), layout_(layout), symtab_(symtab) 2831 { } 2832 2833 void 2834 operator()(Sized_symbol<size>* sym) 2835 { 2836 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym); 2837 if (local_pic_function<size, big_endian>(mips_sym)) 2838 { 2839 // SYM is a function that might need $25 to be valid on entry. 2840 // If we're creating a non-PIC relocatable object, mark SYM as 2841 // being PIC. If we're creating a non-relocatable object with 2842 // non-PIC branches and jumps to SYM, make sure that SYM has an la25 2843 // stub. 2844 if (parameters->options().relocatable()) 2845 { 2846 if (!parameters->options().output_is_position_independent()) 2847 mips_sym->set_pic(); 2848 } 2849 else if (mips_sym->has_nonpic_branches()) 2850 { 2851 this->target_->la25_stub_section(layout_) 2852 ->create_la25_stub(this->symtab_, this->target_, mips_sym); 2853 } 2854 } 2855 } 2856 2857 private: 2858 Target_mips<size, big_endian>* target_; 2859 Layout* layout_; 2860 Symbol_table* symtab_; 2861 }; 2862 2863 template<int size, bool big_endian> 2864 class Target_mips : public Sized_target<size, big_endian> 2865 { 2866 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; 2867 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> 2868 Reloc_section; 2869 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> 2870 Reloca_section; 2871 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32; 2872 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype; 2873 2874 public: 2875 Target_mips(const Target::Target_info* info = &mips_info) 2876 : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL), 2877 got_plt_(NULL), rel_dyn_(NULL), copy_relocs_(), 2878 dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(), 2879 mips_stubs_(NULL), ei_class_(0), mach_(0), layout_(NULL), 2880 got16_addends_(), entry_symbol_is_compressed_(false), insn32_(false) 2881 { 2882 this->add_machine_extensions(); 2883 } 2884 2885 // The offset of $gp from the beginning of the .got section. 2886 static const unsigned int MIPS_GP_OFFSET = 0x7ff0; 2887 2888 // The maximum size of the GOT for it to be addressable using 16-bit 2889 // offsets from $gp. 2890 static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff; 2891 2892 // Make a new symbol table entry for the Mips target. 2893 Sized_symbol<size>* 2894 make_symbol() const 2895 { return new Mips_symbol<size>(); } 2896 2897 // Process the relocations to determine unreferenced sections for 2898 // garbage collection. 2899 void 2900 gc_process_relocs(Symbol_table* symtab, 2901 Layout* layout, 2902 Sized_relobj_file<size, big_endian>* object, 2903 unsigned int data_shndx, 2904 unsigned int sh_type, 2905 const unsigned char* prelocs, 2906 size_t reloc_count, 2907 Output_section* output_section, 2908 bool needs_special_offset_handling, 2909 size_t local_symbol_count, 2910 const unsigned char* plocal_symbols); 2911 2912 // Scan the relocations to look for symbol adjustments. 2913 void 2914 scan_relocs(Symbol_table* symtab, 2915 Layout* layout, 2916 Sized_relobj_file<size, big_endian>* object, 2917 unsigned int data_shndx, 2918 unsigned int sh_type, 2919 const unsigned char* prelocs, 2920 size_t reloc_count, 2921 Output_section* output_section, 2922 bool needs_special_offset_handling, 2923 size_t local_symbol_count, 2924 const unsigned char* plocal_symbols); 2925 2926 // Finalize the sections. 2927 void 2928 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*); 2929 2930 // Relocate a section. 2931 void 2932 relocate_section(const Relocate_info<size, big_endian>*, 2933 unsigned int sh_type, 2934 const unsigned char* prelocs, 2935 size_t reloc_count, 2936 Output_section* output_section, 2937 bool needs_special_offset_handling, 2938 unsigned char* view, 2939 Mips_address view_address, 2940 section_size_type view_size, 2941 const Reloc_symbol_changes*); 2942 2943 // Scan the relocs during a relocatable link. 2944 void 2945 scan_relocatable_relocs(Symbol_table* symtab, 2946 Layout* layout, 2947 Sized_relobj_file<size, big_endian>* object, 2948 unsigned int data_shndx, 2949 unsigned int sh_type, 2950 const unsigned char* prelocs, 2951 size_t reloc_count, 2952 Output_section* output_section, 2953 bool needs_special_offset_handling, 2954 size_t local_symbol_count, 2955 const unsigned char* plocal_symbols, 2956 Relocatable_relocs*); 2957 2958 // Emit relocations for a section. 2959 void 2960 relocate_relocs(const Relocate_info<size, big_endian>*, 2961 unsigned int sh_type, 2962 const unsigned char* prelocs, 2963 size_t reloc_count, 2964 Output_section* output_section, 2965 typename elfcpp::Elf_types<size>::Elf_Off 2966 offset_in_output_section, 2967 const Relocatable_relocs*, 2968 unsigned char* view, 2969 Mips_address view_address, 2970 section_size_type view_size, 2971 unsigned char* reloc_view, 2972 section_size_type reloc_view_size); 2973 2974 // Perform target-specific processing in a relocatable link. This is 2975 // only used if we use the relocation strategy RELOC_SPECIAL. 2976 void 2977 relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo, 2978 unsigned int sh_type, 2979 const unsigned char* preloc_in, 2980 size_t relnum, 2981 Output_section* output_section, 2982 typename elfcpp::Elf_types<size>::Elf_Off 2983 offset_in_output_section, 2984 unsigned char* view, 2985 Mips_address view_address, 2986 section_size_type view_size, 2987 unsigned char* preloc_out); 2988 2989 // Return whether SYM is defined by the ABI. 2990 bool 2991 do_is_defined_by_abi(const Symbol* sym) const 2992 { 2993 return ((strcmp(sym->name(), "__gnu_local_gp") == 0) 2994 || (strcmp(sym->name(), "_gp_disp") == 0) 2995 || (strcmp(sym->name(), "___tls_get_addr") == 0)); 2996 } 2997 2998 // Return the number of entries in the GOT. 2999 unsigned int 3000 got_entry_count() const 3001 { 3002 if (!this->has_got_section()) 3003 return 0; 3004 return this->got_size() / (size/8); 3005 } 3006 3007 // Return the number of entries in the PLT. 3008 unsigned int 3009 plt_entry_count() const 3010 { 3011 if (this->plt_ == NULL) 3012 return 0; 3013 return this->plt_->entry_count(); 3014 } 3015 3016 // Return the offset of the first non-reserved PLT entry. 3017 unsigned int 3018 first_plt_entry_offset() const 3019 { return this->plt_->first_plt_entry_offset(); } 3020 3021 // Return the size of each PLT entry. 3022 unsigned int 3023 plt_entry_size() const 3024 { return this->plt_->plt_entry_size(); } 3025 3026 // Get the GOT section, creating it if necessary. 3027 Mips_output_data_got<size, big_endian>* 3028 got_section(Symbol_table*, Layout*); 3029 3030 // Get the GOT section. 3031 Mips_output_data_got<size, big_endian>* 3032 got_section() const 3033 { 3034 gold_assert(this->got_ != NULL); 3035 return this->got_; 3036 } 3037 3038 // Get the .MIPS.stubs section, creating it if necessary. 3039 Mips_output_data_mips_stubs<size, big_endian>* 3040 mips_stubs_section(Layout* layout); 3041 3042 // Get the .MIPS.stubs section. 3043 Mips_output_data_mips_stubs<size, big_endian>* 3044 mips_stubs_section() const 3045 { 3046 gold_assert(this->mips_stubs_ != NULL); 3047 return this->mips_stubs_; 3048 } 3049 3050 // Get the LA25 stub section, creating it if necessary. 3051 Mips_output_data_la25_stub<size, big_endian>* 3052 la25_stub_section(Layout*); 3053 3054 // Get the LA25 stub section. 3055 Mips_output_data_la25_stub<size, big_endian>* 3056 la25_stub_section() 3057 { 3058 gold_assert(this->la25_stub_ != NULL); 3059 return this->la25_stub_; 3060 } 3061 3062 // Get gp value. It has the value of .got + 0x7FF0. 3063 Mips_address 3064 gp_value() const 3065 { 3066 if (this->gp_ != NULL) 3067 return this->gp_->value(); 3068 return 0; 3069 } 3070 3071 // Get gp value. It has the value of .got + 0x7FF0. Adjust it for 3072 // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned. 3073 Mips_address 3074 adjusted_gp_value(const Mips_relobj<size, big_endian>* object) 3075 { 3076 if (this->gp_ == NULL) 3077 return 0; 3078 3079 bool multi_got = false; 3080 if (this->has_got_section()) 3081 multi_got = this->got_section()->multi_got(); 3082 if (!multi_got) 3083 return this->gp_->value(); 3084 else 3085 return this->gp_->value() + this->got_section()->get_got_offset(object); 3086 } 3087 3088 // Get the dynamic reloc section, creating it if necessary. 3089 Reloc_section* 3090 rel_dyn_section(Layout*); 3091 3092 bool 3093 do_has_custom_set_dynsym_indexes() const 3094 { return true; } 3095 3096 // Don't emit input .reginfo sections to output .reginfo. 3097 bool 3098 do_should_include_section(elfcpp::Elf_Word sh_type) const 3099 { return sh_type != elfcpp::SHT_MIPS_REGINFO; } 3100 3101 // Set the dynamic symbol indexes. INDEX is the index of the first 3102 // global dynamic symbol. Pointers to the symbols are stored into the 3103 // vector SYMS. The names are added to DYNPOOL. This returns an 3104 // updated dynamic symbol index. 3105 unsigned int 3106 do_set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index, 3107 std::vector<Symbol*>* syms, Stringpool* dynpool, 3108 Versions* versions, Symbol_table* symtab) const; 3109 3110 // Remove .MIPS.stubs entry for a symbol. 3111 void 3112 remove_lazy_stub_entry(Mips_symbol<size>* sym) 3113 { 3114 if (this->mips_stubs_ != NULL) 3115 this->mips_stubs_->remove_entry(sym); 3116 } 3117 3118 // The value to write into got[1] for SVR4 targets, to identify it is 3119 // a GNU object. The dynamic linker can then use got[1] to store the 3120 // module pointer. 3121 uint64_t 3122 mips_elf_gnu_got1_mask() 3123 { 3124 if (this->is_output_n64()) 3125 return (uint64_t)1 << 63; 3126 else 3127 return 1 << 31; 3128 } 3129 3130 // Whether the output has microMIPS code. This is valid only after 3131 // merge_processor_specific_flags() is called. 3132 bool 3133 is_output_micromips() const 3134 { 3135 gold_assert(this->are_processor_specific_flags_set()); 3136 return elfcpp::is_micromips(this->processor_specific_flags()); 3137 } 3138 3139 // Whether the output uses N32 ABI. This is valid only after 3140 // merge_processor_specific_flags() is called. 3141 bool 3142 is_output_n32() const 3143 { 3144 gold_assert(this->are_processor_specific_flags_set()); 3145 return elfcpp::abi_n32(this->processor_specific_flags()); 3146 } 3147 3148 // Whether the output uses N64 ABI. This is valid only after 3149 // merge_processor_specific_flags() is called. 3150 bool 3151 is_output_n64() const 3152 { 3153 gold_assert(this->are_processor_specific_flags_set()); 3154 return elfcpp::abi_64(this->ei_class_); 3155 } 3156 3157 // Whether the output uses NEWABI. This is valid only after 3158 // merge_processor_specific_flags() is called. 3159 bool 3160 is_output_newabi() const 3161 { return this->is_output_n32() || this->is_output_n64(); } 3162 3163 // Whether we can only use 32-bit microMIPS instructions. 3164 bool 3165 use_32bit_micromips_instructions() const 3166 { return this->insn32_; } 3167 3168 protected: 3169 // Return the value to use for a dynamic symbol which requires special 3170 // treatment. This is how we support equality comparisons of function 3171 // pointers across shared library boundaries, as described in the 3172 // processor specific ABI supplement. 3173 uint64_t 3174 do_dynsym_value(const Symbol* gsym) const; 3175 3176 // Make an ELF object. 3177 Object* 3178 do_make_elf_object(const std::string&, Input_file*, off_t, 3179 const elfcpp::Ehdr<size, big_endian>& ehdr); 3180 3181 Object* 3182 do_make_elf_object(const std::string&, Input_file*, off_t, 3183 const elfcpp::Ehdr<size, !big_endian>&) 3184 { gold_unreachable(); } 3185 3186 // Make an output section. 3187 Output_section* 3188 do_make_output_section(const char* name, elfcpp::Elf_Word type, 3189 elfcpp::Elf_Xword flags) 3190 { 3191 if (type == elfcpp::SHT_MIPS_REGINFO) 3192 return new Mips_output_section_reginfo<size, big_endian>(name, type, 3193 flags, this); 3194 else 3195 return new Output_section(name, type, flags); 3196 } 3197 3198 // Adjust ELF file header. 3199 void 3200 do_adjust_elf_header(unsigned char* view, int len); 3201 3202 // Get the custom dynamic tag value. 3203 unsigned int 3204 do_dynamic_tag_custom_value(elfcpp::DT) const; 3205 3206 // Adjust the value written to the dynamic symbol table. 3207 virtual void 3208 do_adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const 3209 { 3210 elfcpp::Sym<size, big_endian> isym(view); 3211 elfcpp::Sym_write<size, big_endian> osym(view); 3212 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym); 3213 3214 // Keep dynamic compressed symbols odd. This allows the dynamic linker 3215 // to treat compressed symbols like any other. 3216 Mips_address value = isym.get_st_value(); 3217 if (mips_sym->is_mips16() && value != 0) 3218 { 3219 if (!mips_sym->has_mips16_fn_stub()) 3220 value |= 1; 3221 else 3222 { 3223 // If we have a MIPS16 function with a stub, the dynamic symbol 3224 // must refer to the stub, since only the stub uses the standard 3225 // calling conventions. Stub contains MIPS32 code, so don't add +1 3226 // in this case. 3227 3228 // There is a code which does this in the method 3229 // Target_mips::do_dynsym_value, but that code will only be 3230 // executed if the symbol is from dynobj. 3231 // TODO(sasa): GNU ld also changes the value in non-dynamic symbol 3232 // table. 3233 3234 Mips16_stub_section<size, big_endian>* fn_stub = 3235 mips_sym->template get_mips16_fn_stub<big_endian>(); 3236 value = fn_stub->output_address(); 3237 osym.put_st_size(fn_stub->section_size()); 3238 } 3239 3240 osym.put_st_value(value); 3241 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), 3242 mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2))); 3243 } 3244 else if ((mips_sym->is_micromips() 3245 // Stubs are always microMIPS if there is any microMIPS code in 3246 // the output. 3247 || (this->is_output_micromips() && mips_sym->has_lazy_stub())) 3248 && value != 0) 3249 { 3250 osym.put_st_value(value | 1); 3251 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), 3252 mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2))); 3253 } 3254 } 3255 3256 private: 3257 // The class which scans relocations. 3258 class Scan 3259 { 3260 public: 3261 Scan() 3262 { } 3263 3264 static inline int 3265 get_reference_flags(unsigned int r_type); 3266 3267 inline void 3268 local(Symbol_table* symtab, Layout* layout, Target_mips* target, 3269 Sized_relobj_file<size, big_endian>* object, 3270 unsigned int data_shndx, 3271 Output_section* output_section, 3272 const elfcpp::Rel<size, big_endian>& reloc, unsigned int r_type, 3273 const elfcpp::Sym<size, big_endian>& lsym, 3274 bool is_discarded); 3275 3276 inline void 3277 local(Symbol_table* symtab, Layout* layout, Target_mips* target, 3278 Sized_relobj_file<size, big_endian>* object, 3279 unsigned int data_shndx, 3280 Output_section* output_section, 3281 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type, 3282 const elfcpp::Sym<size, big_endian>& lsym, 3283 bool is_discarded); 3284 3285 inline void 3286 local(Symbol_table* symtab, Layout* layout, Target_mips* target, 3287 Sized_relobj_file<size, big_endian>* object, 3288 unsigned int data_shndx, 3289 Output_section* output_section, 3290 const elfcpp::Rela<size, big_endian>* rela, 3291 const elfcpp::Rel<size, big_endian>* rel, 3292 unsigned int rel_type, 3293 unsigned int r_type, 3294 const elfcpp::Sym<size, big_endian>& lsym, 3295 bool is_discarded); 3296 3297 inline void 3298 global(Symbol_table* symtab, Layout* layout, Target_mips* target, 3299 Sized_relobj_file<size, big_endian>* object, 3300 unsigned int data_shndx, 3301 Output_section* output_section, 3302 const elfcpp::Rel<size, big_endian>& reloc, unsigned int r_type, 3303 Symbol* gsym); 3304 3305 inline void 3306 global(Symbol_table* symtab, Layout* layout, Target_mips* target, 3307 Sized_relobj_file<size, big_endian>* object, 3308 unsigned int data_shndx, 3309 Output_section* output_section, 3310 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type, 3311 Symbol* gsym); 3312 3313 inline void 3314 global(Symbol_table* symtab, Layout* layout, Target_mips* target, 3315 Sized_relobj_file<size, big_endian>* object, 3316 unsigned int data_shndx, 3317 Output_section* output_section, 3318 const elfcpp::Rela<size, big_endian>* rela, 3319 const elfcpp::Rel<size, big_endian>* rel, 3320 unsigned int rel_type, 3321 unsigned int r_type, 3322 Symbol* gsym); 3323 3324 inline bool 3325 local_reloc_may_be_function_pointer(Symbol_table* , Layout*, 3326 Target_mips*, 3327 Sized_relobj_file<size, big_endian>*, 3328 unsigned int, 3329 Output_section*, 3330 const elfcpp::Rel<size, big_endian>&, 3331 unsigned int, 3332 const elfcpp::Sym<size, big_endian>&) 3333 { return false; } 3334 3335 inline bool 3336 global_reloc_may_be_function_pointer(Symbol_table*, Layout*, 3337 Target_mips*, 3338 Sized_relobj_file<size, big_endian>*, 3339 unsigned int, 3340 Output_section*, 3341 const elfcpp::Rel<size, big_endian>&, 3342 unsigned int, Symbol*) 3343 { return false; } 3344 3345 inline bool 3346 local_reloc_may_be_function_pointer(Symbol_table*, Layout*, 3347 Target_mips*, 3348 Sized_relobj_file<size, big_endian>*, 3349 unsigned int, 3350 Output_section*, 3351 const elfcpp::Rela<size, big_endian>&, 3352 unsigned int, 3353 const elfcpp::Sym<size, big_endian>&) 3354 { return false; } 3355 3356 inline bool 3357 global_reloc_may_be_function_pointer(Symbol_table*, Layout*, 3358 Target_mips*, 3359 Sized_relobj_file<size, big_endian>*, 3360 unsigned int, 3361 Output_section*, 3362 const elfcpp::Rela<size, big_endian>&, 3363 unsigned int, Symbol*) 3364 { return false; } 3365 private: 3366 static void 3367 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*, 3368 unsigned int r_type); 3369 3370 static void 3371 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*, 3372 unsigned int r_type, Symbol*); 3373 }; 3374 3375 // The class which implements relocation. 3376 class Relocate 3377 { 3378 public: 3379 Relocate() 3380 { } 3381 3382 ~Relocate() 3383 { } 3384 3385 // Return whether the R_MIPS_32 relocation needs to be applied. 3386 inline bool 3387 should_apply_r_mips_32_reloc(const Mips_symbol<size>* gsym, 3388 unsigned int r_type, 3389 Output_section* output_section, 3390 Target_mips* target); 3391 3392 // Do a relocation. Return false if the caller should not issue 3393 // any warnings about this relocation. 3394 inline bool 3395 relocate(const Relocate_info<size, big_endian>*, Target_mips*, 3396 Output_section*, size_t relnum, 3397 const elfcpp::Rela<size, big_endian>*, 3398 const elfcpp::Rel<size, big_endian>*, 3399 unsigned int, 3400 unsigned int, const Sized_symbol<size>*, 3401 const Symbol_value<size>*, 3402 unsigned char*, 3403 Mips_address, 3404 section_size_type); 3405 3406 inline bool 3407 relocate(const Relocate_info<size, big_endian>*, Target_mips*, 3408 Output_section*, size_t relnum, 3409 const elfcpp::Rel<size, big_endian>&, 3410 unsigned int, const Sized_symbol<size>*, 3411 const Symbol_value<size>*, 3412 unsigned char*, 3413 Mips_address, 3414 section_size_type); 3415 3416 inline bool 3417 relocate(const Relocate_info<size, big_endian>*, Target_mips*, 3418 Output_section*, size_t relnum, 3419 const elfcpp::Rela<size, big_endian>&, 3420 unsigned int, const Sized_symbol<size>*, 3421 const Symbol_value<size>*, 3422 unsigned char*, 3423 Mips_address, 3424 section_size_type); 3425 }; 3426 3427 // A class which returns the size required for a relocation type, 3428 // used while scanning relocs during a relocatable link. 3429 class Relocatable_size_for_reloc 3430 { 3431 public: 3432 unsigned int 3433 get_size_for_reloc(unsigned int, Relobj*); 3434 }; 3435 3436 // This POD class holds the dynamic relocations that should be emitted instead 3437 // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations. We will emit these 3438 // relocations if it turns out that the symbol does not have static 3439 // relocations. 3440 class Dyn_reloc 3441 { 3442 public: 3443 Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type, 3444 Mips_relobj<size, big_endian>* relobj, unsigned int shndx, 3445 Output_section* output_section, Mips_address r_offset) 3446 : sym_(sym), r_type_(r_type), relobj_(relobj), 3447 shndx_(shndx), output_section_(output_section), 3448 r_offset_(r_offset) 3449 { } 3450 3451 // Emit this reloc if appropriate. This is called after we have 3452 // scanned all the relocations, so we know whether the symbol has 3453 // static relocations. 3454 void 3455 emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got, 3456 Symbol_table* symtab) 3457 { 3458 if (!this->sym_->has_static_relocs()) 3459 { 3460 got->record_global_got_symbol(this->sym_, this->relobj_, 3461 this->r_type_, true, false); 3462 if (!symbol_references_local(this->sym_, 3463 this->sym_->should_add_dynsym_entry(symtab))) 3464 rel_dyn->add_global(this->sym_, this->r_type_, 3465 this->output_section_, this->relobj_, 3466 this->shndx_, this->r_offset_); 3467 else 3468 rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_, 3469 this->output_section_, this->relobj_, 3470 this->shndx_, this->r_offset_); 3471 } 3472 } 3473 3474 private: 3475 Mips_symbol<size>* sym_; 3476 unsigned int r_type_; 3477 Mips_relobj<size, big_endian>* relobj_; 3478 unsigned int shndx_; 3479 Output_section* output_section_; 3480 Mips_address r_offset_; 3481 }; 3482 3483 // Adjust TLS relocation type based on the options and whether this 3484 // is a local symbol. 3485 static tls::Tls_optimization 3486 optimize_tls_reloc(bool is_final, int r_type); 3487 3488 // Return whether there is a GOT section. 3489 bool 3490 has_got_section() const 3491 { return this->got_ != NULL; } 3492 3493 // Check whether the given ELF header flags describe a 32-bit binary. 3494 bool 3495 mips_32bit_flags(elfcpp::Elf_Word); 3496 3497 enum Mips_mach { 3498 mach_mips3000 = 3000, 3499 mach_mips3900 = 3900, 3500 mach_mips4000 = 4000, 3501 mach_mips4010 = 4010, 3502 mach_mips4100 = 4100, 3503 mach_mips4111 = 4111, 3504 mach_mips4120 = 4120, 3505 mach_mips4300 = 4300, 3506 mach_mips4400 = 4400, 3507 mach_mips4600 = 4600, 3508 mach_mips4650 = 4650, 3509 mach_mips5000 = 5000, 3510 mach_mips5400 = 5400, 3511 mach_mips5500 = 5500, 3512 mach_mips6000 = 6000, 3513 mach_mips7000 = 7000, 3514 mach_mips8000 = 8000, 3515 mach_mips9000 = 9000, 3516 mach_mips10000 = 10000, 3517 mach_mips12000 = 12000, 3518 mach_mips14000 = 14000, 3519 mach_mips16000 = 16000, 3520 mach_mips16 = 16, 3521 mach_mips5 = 5, 3522 mach_mips_loongson_2e = 3001, 3523 mach_mips_loongson_2f = 3002, 3524 mach_mips_loongson_3a = 3003, 3525 mach_mips_sb1 = 12310201, // octal 'SB', 01 3526 mach_mips_octeon = 6501, 3527 mach_mips_octeonp = 6601, 3528 mach_mips_octeon2 = 6502, 3529 mach_mips_xlr = 887682, // decimal 'XLR' 3530 mach_mipsisa32 = 32, 3531 mach_mipsisa32r2 = 33, 3532 mach_mipsisa64 = 64, 3533 mach_mipsisa64r2 = 65, 3534 mach_mips_micromips = 96 3535 }; 3536 3537 // Return the MACH for a MIPS e_flags value. 3538 unsigned int 3539 elf_mips_mach(elfcpp::Elf_Word); 3540 3541 // Check whether machine EXTENSION is an extension of machine BASE. 3542 bool 3543 mips_mach_extends(unsigned int, unsigned int); 3544 3545 // Merge processor specific flags. 3546 void 3547 merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word, 3548 unsigned char, bool); 3549 3550 // True if we are linking for CPUs that are faster if JAL is converted to BAL. 3551 static inline bool 3552 jal_to_bal() 3553 { return false; } 3554 3555 // True if we are linking for CPUs that are faster if JALR is converted to 3556 // BAL. This should be safe for all architectures. We enable this predicate 3557 // for all CPUs. 3558 static inline bool 3559 jalr_to_bal() 3560 { return true; } 3561 3562 // True if we are linking for CPUs that are faster if JR is converted to B. 3563 // This should be safe for all architectures. We enable this predicate for 3564 // all CPUs. 3565 static inline bool 3566 jr_to_b() 3567 { return true; } 3568 3569 // Return the size of the GOT section. 3570 section_size_type 3571 got_size() const 3572 { 3573 gold_assert(this->got_ != NULL); 3574 return this->got_->data_size(); 3575 } 3576 3577 // Create a PLT entry for a global symbol referenced by r_type relocation. 3578 void 3579 make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*, 3580 unsigned int r_type); 3581 3582 // Get the PLT section. 3583 Mips_output_data_plt<size, big_endian>* 3584 plt_section() const 3585 { 3586 gold_assert(this->plt_ != NULL); 3587 return this->plt_; 3588 } 3589 3590 // Get the GOT PLT section. 3591 const Mips_output_data_plt<size, big_endian>* 3592 got_plt_section() const 3593 { 3594 gold_assert(this->got_plt_ != NULL); 3595 return this->got_plt_; 3596 } 3597 3598 // Copy a relocation against a global symbol. 3599 void 3600 copy_reloc(Symbol_table* symtab, Layout* layout, 3601 Sized_relobj_file<size, big_endian>* object, 3602 unsigned int shndx, Output_section* output_section, 3603 Symbol* sym, const elfcpp::Rel<size, big_endian>& reloc) 3604 { 3605 this->copy_relocs_.copy_reloc(symtab, layout, 3606 symtab->get_sized_symbol<size>(sym), 3607 object, shndx, output_section, 3608 reloc, this->rel_dyn_section(layout)); 3609 } 3610 3611 void 3612 dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type, 3613 Mips_relobj<size, big_endian>* relobj, 3614 unsigned int shndx, Output_section* output_section, 3615 Mips_address r_offset) 3616 { 3617 this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx, 3618 output_section, r_offset)); 3619 } 3620 3621 // Calculate value of _gp symbol. 3622 void 3623 set_gp(Layout*, Symbol_table*); 3624 3625 const char* 3626 elf_mips_abi_name(elfcpp::Elf_Word e_flags, unsigned char ei_class); 3627 const char* 3628 elf_mips_mach_name(elfcpp::Elf_Word e_flags); 3629 3630 // Adds entries that describe how machines relate to one another. The entries 3631 // are ordered topologically with MIPS I extensions listed last. First 3632 // element is extension, second element is base. 3633 void 3634 add_machine_extensions() 3635 { 3636 // MIPS64r2 extensions. 3637 this->add_extension(mach_mips_octeon2, mach_mips_octeonp); 3638 this->add_extension(mach_mips_octeonp, mach_mips_octeon); 3639 this->add_extension(mach_mips_octeon, mach_mipsisa64r2); 3640 3641 // MIPS64 extensions. 3642 this->add_extension(mach_mipsisa64r2, mach_mipsisa64); 3643 this->add_extension(mach_mips_sb1, mach_mipsisa64); 3644 this->add_extension(mach_mips_xlr, mach_mipsisa64); 3645 this->add_extension(mach_mips_loongson_3a, mach_mipsisa64); 3646 3647 // MIPS V extensions. 3648 this->add_extension(mach_mipsisa64, mach_mips5); 3649 3650 // R10000 extensions. 3651 this->add_extension(mach_mips12000, mach_mips10000); 3652 this->add_extension(mach_mips14000, mach_mips10000); 3653 this->add_extension(mach_mips16000, mach_mips10000); 3654 3655 // R5000 extensions. Note: the vr5500 ISA is an extension of the core 3656 // vr5400 ISA, but doesn't include the multimedia stuff. It seems 3657 // better to allow vr5400 and vr5500 code to be merged anyway, since 3658 // many libraries will just use the core ISA. Perhaps we could add 3659 // some sort of ASE flag if this ever proves a problem. 3660 this->add_extension(mach_mips5500, mach_mips5400); 3661 this->add_extension(mach_mips5400, mach_mips5000); 3662 3663 // MIPS IV extensions. 3664 this->add_extension(mach_mips5, mach_mips8000); 3665 this->add_extension(mach_mips10000, mach_mips8000); 3666 this->add_extension(mach_mips5000, mach_mips8000); 3667 this->add_extension(mach_mips7000, mach_mips8000); 3668 this->add_extension(mach_mips9000, mach_mips8000); 3669 3670 // VR4100 extensions. 3671 this->add_extension(mach_mips4120, mach_mips4100); 3672 this->add_extension(mach_mips4111, mach_mips4100); 3673 3674 // MIPS III extensions. 3675 this->add_extension(mach_mips_loongson_2e, mach_mips4000); 3676 this->add_extension(mach_mips_loongson_2f, mach_mips4000); 3677 this->add_extension(mach_mips8000, mach_mips4000); 3678 this->add_extension(mach_mips4650, mach_mips4000); 3679 this->add_extension(mach_mips4600, mach_mips4000); 3680 this->add_extension(mach_mips4400, mach_mips4000); 3681 this->add_extension(mach_mips4300, mach_mips4000); 3682 this->add_extension(mach_mips4100, mach_mips4000); 3683 this->add_extension(mach_mips4010, mach_mips4000); 3684 3685 // MIPS32 extensions. 3686 this->add_extension(mach_mipsisa32r2, mach_mipsisa32); 3687 3688 // MIPS II extensions. 3689 this->add_extension(mach_mips4000, mach_mips6000); 3690 this->add_extension(mach_mipsisa32, mach_mips6000); 3691 3692 // MIPS I extensions. 3693 this->add_extension(mach_mips6000, mach_mips3000); 3694 this->add_extension(mach_mips3900, mach_mips3000); 3695 } 3696 3697 // Add value to MIPS extenstions. 3698 void 3699 add_extension(unsigned int base, unsigned int extension) 3700 { 3701 std::pair<unsigned int, unsigned int> ext(base, extension); 3702 this->mips_mach_extensions_.push_back(ext); 3703 } 3704 3705 // Return the number of entries in the .dynsym section. 3706 unsigned int get_dt_mips_symtabno() const 3707 { 3708 return ((unsigned int)(this->layout_->dynsym_section()->data_size() 3709 / elfcpp::Elf_sizes<size>::sym_size)); 3710 // TODO(sasa): Entry size is MIPS_ELF_SYM_SIZE. 3711 } 3712 3713 // Information about this specific target which we pass to the 3714 // general Target structure. 3715 static const Target::Target_info mips_info; 3716 // The GOT section. 3717 Mips_output_data_got<size, big_endian>* got_; 3718 // gp symbol. It has the value of .got + 0x7FF0. 3719 Sized_symbol<size>* gp_; 3720 // The PLT section. 3721 Mips_output_data_plt<size, big_endian>* plt_; 3722 // The GOT PLT section. 3723 Output_data_space* got_plt_; 3724 // The dynamic reloc section. 3725 Reloc_section* rel_dyn_; 3726 // Relocs saved to avoid a COPY reloc. 3727 Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_; 3728 3729 // A list of dyn relocs to be saved. 3730 std::vector<Dyn_reloc> dyn_relocs_; 3731 3732 // The LA25 stub section. 3733 Mips_output_data_la25_stub<size, big_endian>* la25_stub_; 3734 // Architecture extensions. 3735 std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_; 3736 // .MIPS.stubs 3737 Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_; 3738 3739 unsigned char ei_class_; 3740 unsigned int mach_; 3741 Layout* layout_; 3742 3743 typename std::list<got16_addend<size, big_endian> > got16_addends_; 3744 3745 // Whether the entry symbol is mips16 or micromips. 3746 bool entry_symbol_is_compressed_; 3747 3748 // Whether we can use only 32-bit microMIPS instructions. 3749 // TODO(sasa): This should be a linker option. 3750 bool insn32_; 3751 }; 3752 3753 3754 // Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations. 3755 // It records high part of the relocation pair. 3756 3757 template<int size, bool big_endian> 3758 struct reloc_high 3759 { 3760 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; 3761 3762 reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object, 3763 const Symbol_value<size>* _psymval, Mips_address _addend, 3764 unsigned int _r_type, unsigned int _r_sym, bool _extract_addend, 3765 Mips_address _address = 0, bool _gp_disp = false) 3766 : view(_view), object(_object), psymval(_psymval), addend(_addend), 3767 r_type(_r_type), r_sym(_r_sym), extract_addend(_extract_addend), 3768 address(_address), gp_disp(_gp_disp) 3769 { } 3770 3771 unsigned char* view; 3772 const Mips_relobj<size, big_endian>* object; 3773 const Symbol_value<size>* psymval; 3774 Mips_address addend; 3775 unsigned int r_type; 3776 unsigned int r_sym; 3777 bool extract_addend; 3778 Mips_address address; 3779 bool gp_disp; 3780 }; 3781 3782 template<int size, bool big_endian> 3783 class Mips_relocate_functions : public Relocate_functions<size, big_endian> 3784 { 3785 typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; 3786 typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16; 3787 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32; 3788 3789 public: 3790 typedef enum 3791 { 3792 STATUS_OKAY, // No error during relocation. 3793 STATUS_OVERFLOW, // Relocation overflow. 3794 STATUS_BAD_RELOC // Relocation cannot be applied. 3795 } Status; 3796 3797 private: 3798 typedef Relocate_functions<size, big_endian> Base; 3799 typedef Mips_relocate_functions<size, big_endian> This; 3800 3801 static typename std::list<reloc_high<size, big_endian> > hi16_relocs; 3802 static typename std::list<reloc_high<size, big_endian> > got16_relocs; 3803 3804 // R_MIPS16_26 is used for the mips16 jal and jalx instructions. 3805 // Most mips16 instructions are 16 bits, but these instructions 3806 // are 32 bits. 3807 // 3808 // The format of these instructions is: 3809 // 3810 // +--------------+--------------------------------+ 3811 // | JALX | X| Imm 20:16 | Imm 25:21 | 3812 // +--------------+--------------------------------+ 3813 // | Immediate 15:0 | 3814 // +-----------------------------------------------+ 3815 // 3816 // JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx. 3817 // Note that the immediate value in the first word is swapped. 3818 // 3819 // When producing a relocatable object file, R_MIPS16_26 is 3820 // handled mostly like R_MIPS_26. In particular, the addend is 3821 // stored as a straight 26-bit value in a 32-bit instruction. 3822 // (gas makes life simpler for itself by never adjusting a 3823 // R_MIPS16_26 reloc to be against a section, so the addend is 3824 // always zero). However, the 32 bit instruction is stored as 2 3825 // 16-bit values, rather than a single 32-bit value. In a 3826 // big-endian file, the result is the same; in a little-endian 3827 // file, the two 16-bit halves of the 32 bit value are swapped. 3828 // This is so that a disassembler can recognize the jal 3829 // instruction. 3830 // 3831 // When doing a final link, R_MIPS16_26 is treated as a 32 bit 3832 // instruction stored as two 16-bit values. The addend A is the 3833 // contents of the targ26 field. The calculation is the same as 3834 // R_MIPS_26. When storing the calculated value, reorder the 3835 // immediate value as shown above, and don't forget to store the 3836 // value as two 16-bit values. 3837 // 3838 // To put it in MIPS ABI terms, the relocation field is T-targ26-16, 3839 // defined as 3840 // 3841 // big-endian: 3842 // +--------+----------------------+ 3843 // | | | 3844 // | | targ26-16 | 3845 // |31 26|25 0| 3846 // +--------+----------------------+ 3847 // 3848 // little-endian: 3849 // +----------+------+-------------+ 3850 // | | | | 3851 // | sub1 | | sub2 | 3852 // |0 9|10 15|16 31| 3853 // +----------+--------------------+ 3854 // where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is 3855 // ((sub1 << 16) | sub2)). 3856 // 3857 // When producing a relocatable object file, the calculation is 3858 // (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2) 3859 // When producing a fully linked file, the calculation is 3860 // let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2) 3861 // ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff) 3862 // 3863 // The table below lists the other MIPS16 instruction relocations. 3864 // Each one is calculated in the same way as the non-MIPS16 relocation 3865 // given on the right, but using the extended MIPS16 layout of 16-bit 3866 // immediate fields: 3867 // 3868 // R_MIPS16_GPREL R_MIPS_GPREL16 3869 // R_MIPS16_GOT16 R_MIPS_GOT16 3870 // R_MIPS16_CALL16 R_MIPS_CALL16 3871 // R_MIPS16_HI16 R_MIPS_HI16 3872 // R_MIPS16_LO16 R_MIPS_LO16 3873 // 3874 // A typical instruction will have a format like this: 3875 // 3876 // +--------------+--------------------------------+ 3877 // | EXTEND | Imm 10:5 | Imm 15:11 | 3878 // +--------------+--------------------------------+ 3879 // | Major | rx | ry | Imm 4:0 | 3880 // +--------------+--------------------------------+ 3881 // 3882 // EXTEND is the five bit value 11110. Major is the instruction 3883 // opcode. 3884 // 3885 // All we need to do here is shuffle the bits appropriately. 3886 // As above, the two 16-bit halves must be swapped on a 3887 // little-endian system. 3888 3889 // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped 3890 // on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1 3891 // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. 3892 3893 static inline bool 3894 should_shuffle_micromips_reloc(unsigned int r_type) 3895 { 3896 return (micromips_reloc(r_type) 3897 && r_type != elfcpp::R_MICROMIPS_PC7_S1 3898 && r_type != elfcpp::R_MICROMIPS_PC10_S1); 3899 } 3900 3901 static void 3902 mips_reloc_unshuffle(unsigned char* view, unsigned int r_type, 3903 bool jal_shuffle) 3904 { 3905 if (!mips16_reloc(r_type) 3906 && !should_shuffle_micromips_reloc(r_type)) 3907 return; 3908 3909 // Pick up the first and second halfwords of the instruction. 3910 Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view); 3911 Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2); 3912 Valtype32 val; 3913 3914 if (micromips_reloc(r_type) 3915 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle)) 3916 val = first << 16 | second; 3917 else if (r_type != elfcpp::R_MIPS16_26) 3918 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11) 3919 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f)); 3920 else 3921 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11) 3922 | ((first & 0x1f) << 21) | second); 3923 3924 elfcpp::Swap<32, big_endian>::writeval(view, val); 3925 } 3926 3927 static void 3928 mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle) 3929 { 3930 if (!mips16_reloc(r_type) 3931 && !should_shuffle_micromips_reloc(r_type)) 3932 return; 3933 3934 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view); 3935 Valtype16 first, second; 3936 3937 if (micromips_reloc(r_type) 3938 || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle)) 3939 { 3940 second = val & 0xffff; 3941 first = val >> 16; 3942 } 3943 else if (r_type != elfcpp::R_MIPS16_26) 3944 { 3945 second = ((val >> 11) & 0xffe0) | (val & 0x1f); 3946 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0); 3947 } 3948 else 3949 { 3950 second = val & 0xffff; 3951 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0) 3952 | ((val >> 21) & 0x1f); 3953 } 3954 3955 elfcpp::Swap<16, big_endian>::writeval(view + 2, second); 3956 elfcpp::Swap<16, big_endian>::writeval(view, first); 3957 } 3958 3959 public: 3960 // R_MIPS_16: S + sign-extend(A) 3961 static inline typename This::Status 3962 rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object, 3963 const Symbol_value<size>* psymval, Mips_address addend_a, 3964 bool extract_addend, unsigned int r_type) 3965 { 3966 mips_reloc_unshuffle(view, r_type, false); 3967 Valtype16* wv = reinterpret_cast<Valtype16*>(view); 3968 Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv); 3969 3970 Valtype32 addend = (extract_addend ? Bits<16>::sign_extend32(val) 3971 : Bits<16>::sign_extend32(addend_a)); 3972 3973 Valtype32 x = psymval->value(object, addend); 3974 val = Bits<16>::bit_select32(val, x, 0xffffU); 3975 elfcpp::Swap<16, big_endian>::writeval(wv, val); 3976 mips_reloc_shuffle(view, r_type, false); 3977 return (Bits<16>::has_overflow32(x) 3978 ? This::STATUS_OVERFLOW 3979 : This::STATUS_OKAY); 3980 } 3981 3982 // R_MIPS_32: S + A 3983 static inline typename This::Status 3984 rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object, 3985 const Symbol_value<size>* psymval, Mips_address addend_a, 3986 bool extract_addend, unsigned int r_type) 3987 { 3988 mips_reloc_unshuffle(view, r_type, false); 3989 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 3990 Valtype32 addend = (extract_addend 3991 ? elfcpp::Swap<32, big_endian>::readval(wv) 3992 : Bits<32>::sign_extend32(addend_a)); 3993 Valtype32 x = psymval->value(object, addend); 3994 elfcpp::Swap<32, big_endian>::writeval(wv, x); 3995 mips_reloc_shuffle(view, r_type, false); 3996 return This::STATUS_OKAY; 3997 } 3998 3999 // R_MIPS_JALR, R_MICROMIPS_JALR 4000 static inline typename This::Status 4001 reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object, 4002 const Symbol_value<size>* psymval, Mips_address address, 4003 Mips_address addend_a, bool extract_addend, bool cross_mode_jump, 4004 unsigned int r_type, bool jalr_to_bal, bool jr_to_b) 4005 { 4006 mips_reloc_unshuffle(view, r_type, false); 4007 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4008 Valtype32 addend = extract_addend ? 0 : addend_a; 4009 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4010 4011 // Try converting J(AL)R to B(AL), if the target is in range. 4012 if (!parameters->options().relocatable() 4013 && r_type == elfcpp::R_MIPS_JALR 4014 && !cross_mode_jump 4015 && ((jalr_to_bal && val == 0x0320f809) // jalr t9 4016 || (jr_to_b && val == 0x03200008))) // jr t9 4017 { 4018 int offset = psymval->value(object, addend) - (address + 4); 4019 if (!Bits<18>::has_overflow32(offset)) 4020 { 4021 if (val == 0x03200008) // jr t9 4022 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr 4023 else 4024 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr 4025 } 4026 } 4027 4028 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4029 mips_reloc_shuffle(view, r_type, false); 4030 return This::STATUS_OKAY; 4031 } 4032 4033 // R_MIPS_PC32: S + A - P 4034 static inline typename This::Status 4035 relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object, 4036 const Symbol_value<size>* psymval, Mips_address address, 4037 Mips_address addend_a, bool extract_addend, unsigned int r_type) 4038 { 4039 mips_reloc_unshuffle(view, r_type, false); 4040 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4041 Valtype32 addend = (extract_addend 4042 ? elfcpp::Swap<32, big_endian>::readval(wv) 4043 : Bits<32>::sign_extend32(addend_a)); 4044 Valtype32 x = psymval->value(object, addend) - address; 4045 elfcpp::Swap<32, big_endian>::writeval(wv, x); 4046 mips_reloc_shuffle(view, r_type, false); 4047 return This::STATUS_OKAY; 4048 } 4049 4050 // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1 4051 static inline typename This::Status 4052 rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object, 4053 const Symbol_value<size>* psymval, Mips_address address, 4054 bool local, Mips_address addend_a, bool extract_addend, 4055 const Symbol* gsym, bool cross_mode_jump, unsigned int r_type, 4056 bool jal_to_bal) 4057 { 4058 mips_reloc_unshuffle(view, r_type, false); 4059 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4060 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4061 4062 Valtype32 addend; 4063 if (extract_addend) 4064 { 4065 if (r_type == elfcpp::R_MICROMIPS_26_S1) 4066 addend = (val & 0x03ffffff) << 1; 4067 else 4068 addend = (val & 0x03ffffff) << 2; 4069 } 4070 else 4071 addend = addend_a; 4072 4073 // Make sure the target of JALX is word-aligned. Bit 0 must be 4074 // the correct ISA mode selector and bit 1 must be 0. 4075 if (cross_mode_jump 4076 && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26)) 4077 { 4078 gold_warning(_("JALX to a non-word-aligned address")); 4079 mips_reloc_shuffle(view, r_type, !parameters->options().relocatable()); 4080 return This::STATUS_BAD_RELOC; 4081 } 4082 4083 // Shift is 2, unusually, for microMIPS JALX. 4084 unsigned int shift = 4085 (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2; 4086 4087 Valtype32 x; 4088 if (local) 4089 x = addend | ((address + 4) & (0xfc000000 << shift)); 4090 else 4091 { 4092 if (shift == 1) 4093 x = Bits<27>::sign_extend32(addend); 4094 else 4095 x = Bits<28>::sign_extend32(addend); 4096 } 4097 x = psymval->value(object, x) >> shift; 4098 4099 if (!local && !gsym->is_weak_undefined()) 4100 { 4101 if ((x >> 26) != ((address + 4) >> (26 + shift))) 4102 { 4103 gold_error(_("relocation truncated to fit: %u against '%s'"), 4104 r_type, gsym->name()); 4105 return This::STATUS_OVERFLOW; 4106 } 4107 } 4108 4109 val = Bits<32>::bit_select32(val, x, 0x03ffffff); 4110 4111 // If required, turn JAL into JALX. 4112 if (cross_mode_jump) 4113 { 4114 bool ok; 4115 Valtype32 opcode = val >> 26; 4116 Valtype32 jalx_opcode; 4117 4118 // Check to see if the opcode is already JAL or JALX. 4119 if (r_type == elfcpp::R_MIPS16_26) 4120 { 4121 ok = (opcode == 0x6) || (opcode == 0x7); 4122 jalx_opcode = 0x7; 4123 } 4124 else if (r_type == elfcpp::R_MICROMIPS_26_S1) 4125 { 4126 ok = (opcode == 0x3d) || (opcode == 0x3c); 4127 jalx_opcode = 0x3c; 4128 } 4129 else 4130 { 4131 ok = (opcode == 0x3) || (opcode == 0x1d); 4132 jalx_opcode = 0x1d; 4133 } 4134 4135 // If the opcode is not JAL or JALX, there's a problem. We cannot 4136 // convert J or JALS to JALX. 4137 if (!ok) 4138 { 4139 gold_error(_("Unsupported jump between ISA modes; consider " 4140 "recompiling with interlinking enabled.")); 4141 return This::STATUS_BAD_RELOC; 4142 } 4143 4144 // Make this the JALX opcode. 4145 val = (val & ~(0x3f << 26)) | (jalx_opcode << 26); 4146 } 4147 4148 // Try converting JAL to BAL, if the target is in range. 4149 if (!parameters->options().relocatable() 4150 && !cross_mode_jump 4151 && ((jal_to_bal 4152 && r_type == elfcpp::R_MIPS_26 4153 && (val >> 26) == 0x3))) // jal addr 4154 { 4155 Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28); 4156 int offset = dest - (address + 4); 4157 if (!Bits<18>::has_overflow32(offset)) 4158 { 4159 if (val == 0x03200008) // jr t9 4160 val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr 4161 else 4162 val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr 4163 } 4164 } 4165 4166 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4167 mips_reloc_shuffle(view, r_type, !parameters->options().relocatable()); 4168 return This::STATUS_OKAY; 4169 } 4170 4171 // R_MIPS_PC16 4172 static inline typename This::Status 4173 relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object, 4174 const Symbol_value<size>* psymval, Mips_address address, 4175 Mips_address addend_a, bool extract_addend, unsigned int r_type) 4176 { 4177 mips_reloc_unshuffle(view, r_type, false); 4178 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4179 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4180 4181 Valtype32 addend = extract_addend ? (val & 0xffff) << 2 : addend_a; 4182 addend = Bits<18>::sign_extend32(addend); 4183 4184 Valtype32 x = psymval->value(object, addend) - address; 4185 val = Bits<16>::bit_select32(val, x >> 2, 0xffff); 4186 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4187 mips_reloc_shuffle(view, r_type, false); 4188 return (Bits<18>::has_overflow32(x) 4189 ? This::STATUS_OVERFLOW 4190 : This::STATUS_OKAY); 4191 } 4192 4193 // R_MICROMIPS_PC7_S1 4194 static inline typename This::Status 4195 relmicromips_pc7_s1(unsigned char* view, 4196 const Mips_relobj<size, big_endian>* object, 4197 const Symbol_value<size>* psymval, Mips_address address, 4198 Mips_address addend_a, bool extract_addend, 4199 unsigned int r_type) 4200 { 4201 mips_reloc_unshuffle(view, r_type, false); 4202 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4203 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4204 4205 Valtype32 addend = extract_addend ? (val & 0x7f) << 1 : addend_a; 4206 addend = Bits<8>::sign_extend32(addend); 4207 4208 Valtype32 x = psymval->value(object, addend) - address; 4209 val = Bits<16>::bit_select32(val, x >> 1, 0x7f); 4210 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4211 mips_reloc_shuffle(view, r_type, false); 4212 return (Bits<8>::has_overflow32(x) 4213 ? This::STATUS_OVERFLOW 4214 : This::STATUS_OKAY); 4215 } 4216 4217 // R_MICROMIPS_PC10_S1 4218 static inline typename This::Status 4219 relmicromips_pc10_s1(unsigned char* view, 4220 const Mips_relobj<size, big_endian>* object, 4221 const Symbol_value<size>* psymval, Mips_address address, 4222 Mips_address addend_a, bool extract_addend, 4223 unsigned int r_type) 4224 { 4225 mips_reloc_unshuffle(view, r_type, false); 4226 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4227 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4228 4229 Valtype32 addend = extract_addend ? (val & 0x3ff) << 1 : addend_a; 4230 addend = Bits<11>::sign_extend32(addend); 4231 4232 Valtype32 x = psymval->value(object, addend) - address; 4233 val = Bits<16>::bit_select32(val, x >> 1, 0x3ff); 4234 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4235 mips_reloc_shuffle(view, r_type, false); 4236 return (Bits<11>::has_overflow32(x) 4237 ? This::STATUS_OVERFLOW 4238 : This::STATUS_OKAY); 4239 } 4240 4241 // R_MICROMIPS_PC16_S1 4242 static inline typename This::Status 4243 relmicromips_pc16_s1(unsigned char* view, 4244 const Mips_relobj<size, big_endian>* object, 4245 const Symbol_value<size>* psymval, Mips_address address, 4246 Mips_address addend_a, bool extract_addend, 4247 unsigned int r_type) 4248 { 4249 mips_reloc_unshuffle(view, r_type, false); 4250 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4251 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4252 4253 Valtype32 addend = extract_addend ? (val & 0xffff) << 1 : addend_a; 4254 addend = Bits<17>::sign_extend32(addend); 4255 4256 Valtype32 x = psymval->value(object, addend) - address; 4257 val = Bits<16>::bit_select32(val, x >> 1, 0xffff); 4258 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4259 mips_reloc_shuffle(view, r_type, false); 4260 return (Bits<17>::has_overflow32(x) 4261 ? This::STATUS_OVERFLOW 4262 : This::STATUS_OKAY); 4263 } 4264 4265 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16, 4266 static inline typename This::Status 4267 relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object, 4268 const Symbol_value<size>* psymval, Mips_address addend, 4269 Mips_address address, bool gp_disp, unsigned int r_type, 4270 unsigned int r_sym, bool extract_addend) 4271 { 4272 // Record the relocation. It will be resolved when we find lo16 part. 4273 hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval, 4274 addend, r_type, r_sym, extract_addend, address, 4275 gp_disp)); 4276 return This::STATUS_OKAY; 4277 } 4278 4279 // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16, 4280 static inline typename This::Status 4281 do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object, 4282 const Symbol_value<size>* psymval, Mips_address addend_hi, 4283 Mips_address address, bool is_gp_disp, unsigned int r_type, 4284 bool extract_addend, Valtype32 addend_lo, 4285 Target_mips<size, big_endian>* target) 4286 { 4287 mips_reloc_unshuffle(view, r_type, false); 4288 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4289 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4290 4291 Valtype32 addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo 4292 : addend_hi); 4293 4294 Valtype32 value; 4295 if (!is_gp_disp) 4296 value = psymval->value(object, addend); 4297 else 4298 { 4299 // For MIPS16 ABI code we generate this sequence 4300 // 0: li $v0,%hi(_gp_disp) 4301 // 4: addiupc $v1,%lo(_gp_disp) 4302 // 8: sll $v0,16 4303 // 12: addu $v0,$v1 4304 // 14: move $gp,$v0 4305 // So the offsets of hi and lo relocs are the same, but the 4306 // base $pc is that used by the ADDIUPC instruction at $t9 + 4. 4307 // ADDIUPC clears the low two bits of the instruction address, 4308 // so the base is ($t9 + 4) & ~3. 4309 Valtype32 gp_disp; 4310 if (r_type == elfcpp::R_MIPS16_HI16) 4311 gp_disp = (target->adjusted_gp_value(object) 4312 - ((address + 4) & ~0x3)); 4313 // The microMIPS .cpload sequence uses the same assembly 4314 // instructions as the traditional psABI version, but the 4315 // incoming $t9 has the low bit set. 4316 else if (r_type == elfcpp::R_MICROMIPS_HI16) 4317 gp_disp = target->adjusted_gp_value(object) - address - 1; 4318 else 4319 gp_disp = target->adjusted_gp_value(object) - address; 4320 value = gp_disp + addend; 4321 } 4322 Valtype32 x = ((value + 0x8000) >> 16) & 0xffff; 4323 val = Bits<32>::bit_select32(val, x, 0xffff); 4324 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4325 mips_reloc_shuffle(view, r_type, false); 4326 return (is_gp_disp && Bits<16>::has_overflow32(x) 4327 ? This::STATUS_OVERFLOW 4328 : This::STATUS_OKAY); 4329 } 4330 4331 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16 4332 static inline typename This::Status 4333 relgot16_local(unsigned char* view, 4334 const Mips_relobj<size, big_endian>* object, 4335 const Symbol_value<size>* psymval, Mips_address addend_a, 4336 bool extract_addend, unsigned int r_type, unsigned int r_sym) 4337 { 4338 // Record the relocation. It will be resolved when we find lo16 part. 4339 got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval, 4340 addend_a, r_type, r_sym, extract_addend)); 4341 return This::STATUS_OKAY; 4342 } 4343 4344 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16 4345 static inline typename This::Status 4346 do_relgot16_local(unsigned char* view, 4347 const Mips_relobj<size, big_endian>* object, 4348 const Symbol_value<size>* psymval, Mips_address addend_hi, 4349 unsigned int r_type, bool extract_addend, 4350 Valtype32 addend_lo, Target_mips<size, big_endian>* target) 4351 { 4352 mips_reloc_unshuffle(view, r_type, false); 4353 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4354 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4355 4356 Valtype32 addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo 4357 : addend_hi); 4358 4359 // Find GOT page entry. 4360 Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16) 4361 & 0xffff; 4362 value <<= 16; 4363 unsigned int got_offset = 4364 target->got_section()->get_got_page_offset(value, object); 4365 4366 // Resolve the relocation. 4367 Valtype32 x = target->got_section()->gp_offset(got_offset, object); 4368 val = Bits<32>::bit_select32(val, x, 0xffff); 4369 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4370 mips_reloc_shuffle(view, r_type, false); 4371 return (Bits<16>::has_overflow32(x) 4372 ? This::STATUS_OVERFLOW 4373 : This::STATUS_OKAY); 4374 } 4375 4376 // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16 4377 static inline typename This::Status 4378 rello16(Target_mips<size, big_endian>* target, unsigned char* view, 4379 const Mips_relobj<size, big_endian>* object, 4380 const Symbol_value<size>* psymval, Mips_address addend_a, 4381 bool extract_addend, Mips_address address, bool is_gp_disp, 4382 unsigned int r_type, unsigned int r_sym) 4383 { 4384 mips_reloc_unshuffle(view, r_type, false); 4385 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4386 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4387 4388 Valtype32 addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff) 4389 : addend_a); 4390 4391 // Resolve pending R_MIPS_HI16 relocations. 4392 typename std::list<reloc_high<size, big_endian> >::iterator it = 4393 hi16_relocs.begin(); 4394 while (it != hi16_relocs.end()) 4395 { 4396 reloc_high<size, big_endian> hi16 = *it; 4397 if (hi16.r_sym == r_sym 4398 && is_matching_lo16_reloc(hi16.r_type, r_type)) 4399 { 4400 if (do_relhi16(hi16.view, hi16.object, hi16.psymval, hi16.addend, 4401 hi16.address, hi16.gp_disp, hi16.r_type, 4402 hi16.extract_addend, addend, target) 4403 == This::STATUS_OVERFLOW) 4404 return This::STATUS_OVERFLOW; 4405 it = hi16_relocs.erase(it); 4406 } 4407 else 4408 ++it; 4409 } 4410 4411 // Resolve pending local R_MIPS_GOT16 relocations. 4412 typename std::list<reloc_high<size, big_endian> >::iterator it2 = 4413 got16_relocs.begin(); 4414 while (it2 != got16_relocs.end()) 4415 { 4416 reloc_high<size, big_endian> got16 = *it2; 4417 if (got16.r_sym == r_sym 4418 && is_matching_lo16_reloc(got16.r_type, r_type)) 4419 { 4420 if (do_relgot16_local(got16.view, got16.object, got16.psymval, 4421 got16.addend, got16.r_type, 4422 got16.extract_addend, addend, 4423 target) == This::STATUS_OVERFLOW) 4424 return This::STATUS_OVERFLOW; 4425 it2 = got16_relocs.erase(it2); 4426 } 4427 else 4428 ++it2; 4429 } 4430 4431 // Resolve R_MIPS_LO16 relocation. 4432 Valtype32 x; 4433 if (!is_gp_disp) 4434 x = psymval->value(object, addend); 4435 else 4436 { 4437 // See the comment for R_MIPS16_HI16 above for the reason 4438 // for this conditional. 4439 Valtype32 gp_disp; 4440 if (r_type == elfcpp::R_MIPS16_LO16) 4441 gp_disp = target->adjusted_gp_value(object) - (address & ~0x3); 4442 else if (r_type == elfcpp::R_MICROMIPS_LO16 4443 || r_type == elfcpp::R_MICROMIPS_HI0_LO16) 4444 gp_disp = target->adjusted_gp_value(object) - address + 3; 4445 else 4446 gp_disp = target->adjusted_gp_value(object) - address + 4; 4447 // The MIPS ABI requires checking the R_MIPS_LO16 relocation 4448 // for overflow. Relocations against _gp_disp are normally 4449 // generated from the .cpload pseudo-op. It generates code 4450 // that normally looks like this: 4451 4452 // lui $gp,%hi(_gp_disp) 4453 // addiu $gp,$gp,%lo(_gp_disp) 4454 // addu $gp,$gp,$t9 4455 4456 // Here $t9 holds the address of the function being called, 4457 // as required by the MIPS ELF ABI. The R_MIPS_LO16 4458 // relocation can easily overflow in this situation, but the 4459 // R_MIPS_HI16 relocation will handle the overflow. 4460 // Therefore, we consider this a bug in the MIPS ABI, and do 4461 // not check for overflow here. 4462 x = gp_disp + addend; 4463 } 4464 val = Bits<32>::bit_select32(val, x, 0xffff); 4465 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4466 mips_reloc_shuffle(view, r_type, false); 4467 return This::STATUS_OKAY; 4468 } 4469 4470 // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16 4471 // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16 4472 // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD 4473 // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL 4474 // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM 4475 // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP 4476 static inline typename This::Status 4477 relgot(unsigned char* view, int gp_offset, unsigned int r_type) 4478 { 4479 mips_reloc_unshuffle(view, r_type, false); 4480 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4481 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4482 Valtype32 x = gp_offset; 4483 val = Bits<32>::bit_select32(val, x, 0xffff); 4484 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4485 mips_reloc_shuffle(view, r_type, false); 4486 return (Bits<16>::has_overflow32(x) 4487 ? This::STATUS_OVERFLOW 4488 : This::STATUS_OKAY); 4489 } 4490 4491 // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE 4492 static inline typename This::Status 4493 relgotpage(Target_mips<size, big_endian>* target, unsigned char* view, 4494 const Mips_relobj<size, big_endian>* object, 4495 const Symbol_value<size>* psymval, Mips_address addend_a, 4496 bool extract_addend, unsigned int r_type) 4497 { 4498 mips_reloc_unshuffle(view, r_type, false); 4499 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4500 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view); 4501 Valtype32 addend = extract_addend ? val & 0xffff : addend_a; 4502 4503 // Find a GOT page entry that points to within 32KB of symbol + addend. 4504 Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff; 4505 unsigned int got_offset = 4506 target->got_section()->get_got_page_offset(value, object); 4507 4508 Valtype32 x = target->got_section()->gp_offset(got_offset, object); 4509 val = Bits<32>::bit_select32(val, x, 0xffff); 4510 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4511 mips_reloc_shuffle(view, r_type, false); 4512 return (Bits<16>::has_overflow32(x) 4513 ? This::STATUS_OVERFLOW 4514 : This::STATUS_OKAY); 4515 } 4516 4517 // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST 4518 static inline typename This::Status 4519 relgotofst(Target_mips<size, big_endian>* target, unsigned char* view, 4520 const Mips_relobj<size, big_endian>* object, 4521 const Symbol_value<size>* psymval, Mips_address addend_a, 4522 bool extract_addend, bool local, unsigned int r_type) 4523 { 4524 mips_reloc_unshuffle(view, r_type, false); 4525 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4526 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view); 4527 Valtype32 addend = extract_addend ? val & 0xffff : addend_a; 4528 4529 // For a local symbol, find a GOT page entry that points to within 32KB of 4530 // symbol + addend. Relocation value is the offset of the GOT page entry's 4531 // value from symbol + addend. 4532 // For a global symbol, relocation value is addend. 4533 Valtype32 x; 4534 if (local) 4535 { 4536 // Find GOT page entry. 4537 Mips_address value = ((psymval->value(object, addend) + 0x8000) 4538 & ~0xffff); 4539 target->got_section()->get_got_page_offset(value, object); 4540 4541 x = psymval->value(object, addend) - value; 4542 } 4543 else 4544 x = addend; 4545 val = Bits<32>::bit_select32(val, x, 0xffff); 4546 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4547 mips_reloc_shuffle(view, r_type, false); 4548 return (Bits<16>::has_overflow32(x) 4549 ? This::STATUS_OVERFLOW 4550 : This::STATUS_OKAY); 4551 } 4552 4553 // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16, 4554 // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16 4555 static inline typename This::Status 4556 relgot_hi16(unsigned char* view, int gp_offset, unsigned int r_type) 4557 { 4558 mips_reloc_unshuffle(view, r_type, false); 4559 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4560 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4561 Valtype32 x = gp_offset; 4562 x = ((x + 0x8000) >> 16) & 0xffff; 4563 val = Bits<32>::bit_select32(val, x, 0xffff); 4564 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4565 mips_reloc_shuffle(view, r_type, false); 4566 return This::STATUS_OKAY; 4567 } 4568 4569 // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16, 4570 // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16 4571 static inline typename This::Status 4572 relgot_lo16(unsigned char* view, int gp_offset, unsigned int r_type) 4573 { 4574 mips_reloc_unshuffle(view, r_type, false); 4575 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4576 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4577 Valtype32 x = gp_offset; 4578 val = Bits<32>::bit_select32(val, x, 0xffff); 4579 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4580 mips_reloc_shuffle(view, r_type, false); 4581 return This::STATUS_OKAY; 4582 } 4583 4584 // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL 4585 // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16 4586 static inline typename This::Status 4587 relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object, 4588 const Symbol_value<size>* psymval, Mips_address gp, 4589 Mips_address addend_a, bool extract_addend, bool local, 4590 unsigned int r_type) 4591 { 4592 mips_reloc_unshuffle(view, r_type, false); 4593 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4594 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4595 4596 Valtype32 addend; 4597 if (extract_addend) 4598 { 4599 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2) 4600 addend = (val & 0x7f) << 2; 4601 else 4602 addend = val & 0xffff; 4603 // Only sign-extend the addend if it was extracted from the 4604 // instruction. If the addend was separate, leave it alone, 4605 // otherwise we may lose significant bits. 4606 addend = Bits<16>::sign_extend32(addend); 4607 } 4608 else 4609 addend = addend_a; 4610 4611 Valtype32 x = psymval->value(object, addend) - gp; 4612 4613 // If the symbol was local, any earlier relocatable links will 4614 // have adjusted its addend with the gp offset, so compensate 4615 // for that now. Don't do it for symbols forced local in this 4616 // link, though, since they won't have had the gp offset applied 4617 // to them before. 4618 if (local) 4619 x += object->gp_value(); 4620 4621 if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2) 4622 val = Bits<32>::bit_select32(val, x, 0x7f); 4623 else 4624 val = Bits<32>::bit_select32(val, x, 0xffff); 4625 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4626 mips_reloc_shuffle(view, r_type, false); 4627 if (Bits<16>::has_overflow32(x)) 4628 { 4629 gold_error(_("small-data section exceeds 64KB; lower small-data size " 4630 "limit (see option -G)")); 4631 return This::STATUS_OVERFLOW; 4632 } 4633 return This::STATUS_OKAY; 4634 } 4635 4636 // R_MIPS_GPREL32 4637 static inline typename This::Status 4638 relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object, 4639 const Symbol_value<size>* psymval, Mips_address gp, 4640 Mips_address addend_a, bool extract_addend, unsigned int r_type) 4641 { 4642 mips_reloc_unshuffle(view, r_type, false); 4643 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4644 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4645 Valtype32 addend = extract_addend ? val : addend_a; 4646 4647 // R_MIPS_GPREL32 relocations are defined for local symbols only. 4648 Valtype32 x = psymval->value(object, addend) + object->gp_value() - gp; 4649 elfcpp::Swap<32, big_endian>::writeval(wv, x); 4650 mips_reloc_shuffle(view, r_type, false); 4651 return This::STATUS_OKAY; 4652 } 4653 4654 // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16 4655 // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16, 4656 // R_MICROMIPS_TLS_DTPREL_HI16 4657 static inline typename This::Status 4658 tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object, 4659 const Symbol_value<size>* psymval, Valtype32 tp_offset, 4660 Mips_address addend_a, bool extract_addend, unsigned int r_type) 4661 { 4662 mips_reloc_unshuffle(view, r_type, false); 4663 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4664 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4665 Valtype32 addend = extract_addend ? val & 0xffff : addend_a; 4666 4667 // tls symbol values are relative to tls_segment()->vaddr() 4668 Valtype32 x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16; 4669 val = Bits<32>::bit_select32(val, x, 0xffff); 4670 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4671 mips_reloc_shuffle(view, r_type, false); 4672 return This::STATUS_OKAY; 4673 } 4674 4675 // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16, 4676 // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16, 4677 // R_MICROMIPS_TLS_DTPREL_LO16, 4678 static inline typename This::Status 4679 tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object, 4680 const Symbol_value<size>* psymval, Valtype32 tp_offset, 4681 Mips_address addend_a, bool extract_addend, unsigned int r_type) 4682 { 4683 mips_reloc_unshuffle(view, r_type, false); 4684 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4685 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4686 Valtype32 addend = extract_addend ? val & 0xffff : addend_a; 4687 4688 // tls symbol values are relative to tls_segment()->vaddr() 4689 Valtype32 x = psymval->value(object, addend) - tp_offset; 4690 val = Bits<32>::bit_select32(val, x, 0xffff); 4691 elfcpp::Swap<32, big_endian>::writeval(wv, val); 4692 mips_reloc_shuffle(view, r_type, false); 4693 return This::STATUS_OKAY; 4694 } 4695 4696 // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64, 4697 // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64 4698 static inline typename This::Status 4699 tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object, 4700 const Symbol_value<size>* psymval, Valtype32 tp_offset, 4701 Mips_address addend_a, bool extract_addend, unsigned int r_type) 4702 { 4703 mips_reloc_unshuffle(view, r_type, false); 4704 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4705 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4706 Valtype32 addend = extract_addend ? val : addend_a; 4707 4708 // tls symbol values are relative to tls_segment()->vaddr() 4709 Valtype32 x = psymval->value(object, addend) - tp_offset; 4710 elfcpp::Swap<32, big_endian>::writeval(wv, x); 4711 mips_reloc_shuffle(view, r_type, false); 4712 return This::STATUS_OKAY; 4713 } 4714 4715 // R_MIPS_SUB, R_MICROMIPS_SUB 4716 static inline typename This::Status 4717 relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object, 4718 const Symbol_value<size>* psymval, Mips_address addend_a, 4719 bool extract_addend, unsigned int r_type) 4720 { 4721 mips_reloc_unshuffle(view, r_type, false); 4722 Valtype32* wv = reinterpret_cast<Valtype32*>(view); 4723 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv); 4724 Valtype32 addend = extract_addend ? val : addend_a; 4725 4726 Valtype32 x = psymval->value(object, -addend); 4727 elfcpp::Swap<32, big_endian>::writeval(wv, x); 4728 mips_reloc_shuffle(view, r_type, false); 4729 return This::STATUS_OKAY; 4730 } 4731 }; 4732 4733 template<int size, bool big_endian> 4734 typename std::list<reloc_high<size, big_endian> > 4735 Mips_relocate_functions<size, big_endian>::hi16_relocs; 4736 4737 template<int size, bool big_endian> 4738 typename std::list<reloc_high<size, big_endian> > 4739 Mips_relocate_functions<size, big_endian>::got16_relocs; 4740 4741 // Mips_got_info methods. 4742 4743 // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol 4744 // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT. 4745 4746 template<int size, bool big_endian> 4747 void 4748 Mips_got_info<size, big_endian>::record_local_got_symbol( 4749 Mips_relobj<size, big_endian>* object, unsigned int symndx, 4750 Mips_address addend, unsigned int r_type, unsigned int shndx) 4751 { 4752 Mips_got_entry<size, big_endian>* entry = 4753 new Mips_got_entry<size, big_endian>(object, symndx, addend, 4754 mips_elf_reloc_tls_type(r_type), 4755 shndx); 4756 this->record_got_entry(entry, object); 4757 } 4758 4759 // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM, 4760 // in OBJECT. FOR_CALL is true if the caller is only interested in 4761 // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic 4762 // relocation. 4763 4764 template<int size, bool big_endian> 4765 void 4766 Mips_got_info<size, big_endian>::record_global_got_symbol( 4767 Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object, 4768 unsigned int r_type, bool dyn_reloc, bool for_call) 4769 { 4770 if (!for_call) 4771 mips_sym->set_got_not_only_for_calls(); 4772 4773 // A global symbol in the GOT must also be in the dynamic symbol table. 4774 if (!mips_sym->needs_dynsym_entry()) 4775 { 4776 switch (mips_sym->visibility()) 4777 { 4778 case elfcpp::STV_INTERNAL: 4779 case elfcpp::STV_HIDDEN: 4780 mips_sym->set_is_forced_local(); 4781 break; 4782 default: 4783 mips_sym->set_needs_dynsym_entry(); 4784 break; 4785 } 4786 } 4787 4788 unsigned char tls_type = mips_elf_reloc_tls_type(r_type); 4789 if (tls_type == GOT_TLS_NONE) 4790 this->global_got_symbols_.insert(mips_sym); 4791 4792 if (dyn_reloc) 4793 { 4794 if (mips_sym->global_got_area() == GGA_NONE) 4795 mips_sym->set_global_got_area(GGA_RELOC_ONLY); 4796 return; 4797 } 4798 4799 Mips_got_entry<size, big_endian>* entry = 4800 new Mips_got_entry<size, big_endian>(object, mips_sym, tls_type); 4801 4802 this->record_got_entry(entry, object); 4803 } 4804 4805 // Add ENTRY to master GOT and to OBJECT's GOT. 4806 4807 template<int size, bool big_endian> 4808 void 4809 Mips_got_info<size, big_endian>::record_got_entry( 4810 Mips_got_entry<size, big_endian>* entry, 4811 Mips_relobj<size, big_endian>* object) 4812 { 4813 if (this->got_entries_.find(entry) == this->got_entries_.end()) 4814 this->got_entries_.insert(entry); 4815 4816 // Create the GOT entry for the OBJECT's GOT. 4817 Mips_got_info<size, big_endian>* g = object->get_or_create_got_info(); 4818 Mips_got_entry<size, big_endian>* entry2 = 4819 new Mips_got_entry<size, big_endian>(*entry); 4820 4821 if (g->got_entries_.find(entry2) == g->got_entries_.end()) 4822 g->got_entries_.insert(entry2); 4823 } 4824 4825 // Record that OBJECT has a page relocation against symbol SYMNDX and 4826 // that ADDEND is the addend for that relocation. 4827 // This function creates an upper bound on the number of GOT slots 4828 // required; no attempt is made to combine references to non-overridable 4829 // global symbols across multiple input files. 4830 4831 template<int size, bool big_endian> 4832 void 4833 Mips_got_info<size, big_endian>::record_got_page_entry( 4834 Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend) 4835 { 4836 struct Got_page_range **range_ptr, *range; 4837 int old_pages, new_pages; 4838 4839 // Find the Got_page_entry for this symbol. 4840 Got_page_entry* entry = new Got_page_entry(object, symndx); 4841 typename Got_page_entry_set::iterator it = 4842 this->got_page_entries_.find(entry); 4843 if (it != this->got_page_entries_.end()) 4844 entry = *it; 4845 else 4846 this->got_page_entries_.insert(entry); 4847 4848 // Add the same entry to the OBJECT's GOT. 4849 Got_page_entry* entry2 = NULL; 4850 Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info(); 4851 if (g2->got_page_entries_.find(entry) == g2->got_page_entries_.end()) 4852 { 4853 entry2 = new Got_page_entry(*entry); 4854 g2->got_page_entries_.insert(entry2); 4855 } 4856 4857 // Skip over ranges whose maximum extent cannot share a page entry 4858 // with ADDEND. 4859 range_ptr = &entry->ranges; 4860 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff) 4861 range_ptr = &(*range_ptr)->next; 4862 4863 // If we scanned to the end of the list, or found a range whose 4864 // minimum extent cannot share a page entry with ADDEND, create 4865 // a new singleton range. 4866 range = *range_ptr; 4867 if (!range || addend < range->min_addend - 0xffff) 4868 { 4869 range = new Got_page_range(); 4870 range->next = *range_ptr; 4871 range->min_addend = addend; 4872 range->max_addend = addend; 4873 4874 *range_ptr = range; 4875 ++entry->num_pages; 4876 if (entry2 != NULL) 4877 ++entry2->num_pages; 4878 ++this->page_gotno_; 4879 ++g2->page_gotno_; 4880 return; 4881 } 4882 4883 // Remember how many pages the old range contributed. 4884 old_pages = range->get_max_pages(); 4885 4886 // Update the ranges. 4887 if (addend < range->min_addend) 4888 range->min_addend = addend; 4889 else if (addend > range->max_addend) 4890 { 4891 if (range->next && addend >= range->next->min_addend - 0xffff) 4892 { 4893 old_pages += range->next->get_max_pages(); 4894 range->max_addend = range->next->max_addend; 4895 range->next = range->next->next; 4896 } 4897 else 4898 range->max_addend = addend; 4899 } 4900 4901 // Record any change in the total estimate. 4902 new_pages = range->get_max_pages(); 4903 if (old_pages != new_pages) 4904 { 4905 entry->num_pages += new_pages - old_pages; 4906 if (entry2 != NULL) 4907 entry2->num_pages += new_pages - old_pages; 4908 this->page_gotno_ += new_pages - old_pages; 4909 g2->page_gotno_ += new_pages - old_pages; 4910 } 4911 } 4912 4913 // Create all entries that should be in the local part of the GOT. 4914 4915 template<int size, bool big_endian> 4916 void 4917 Mips_got_info<size, big_endian>::add_local_entries( 4918 Target_mips<size, big_endian>* target, Layout* layout) 4919 { 4920 Mips_output_data_got<size, big_endian>* got = target->got_section(); 4921 // First two GOT entries are reserved. The first entry will be filled at 4922 // runtime. The second entry will be used by some runtime loaders. 4923 got->add_constant(0); 4924 got->add_constant(target->mips_elf_gnu_got1_mask()); 4925 4926 for (typename Got_entry_set::iterator 4927 p = this->got_entries_.begin(); 4928 p != this->got_entries_.end(); 4929 ++p) 4930 { 4931 Mips_got_entry<size, big_endian>* entry = *p; 4932 if (entry->is_for_local_symbol() && !entry->is_tls_entry()) 4933 { 4934 got->add_local(entry->object(), entry->symndx(), 4935 GOT_TYPE_STANDARD); 4936 unsigned int got_offset = entry->object()->local_got_offset( 4937 entry->symndx(), GOT_TYPE_STANDARD); 4938 if (got->multi_got() && this->index_ > 0 4939 && parameters->options().output_is_position_independent()) 4940 target->rel_dyn_section(layout)->add_local(entry->object(), 4941 entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset); 4942 } 4943 } 4944 4945 this->add_page_entries(target, layout); 4946 4947 // Add global entries that should be in the local area. 4948 for (typename Got_entry_set::iterator 4949 p = this->got_entries_.begin(); 4950 p != this->got_entries_.end(); 4951 ++p) 4952 { 4953 Mips_got_entry<size, big_endian>* entry = *p; 4954 if (!entry->is_for_global_symbol()) 4955 continue; 4956 4957 Mips_symbol<size>* mips_sym = entry->sym(); 4958 if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry()) 4959 { 4960 unsigned int got_type; 4961 if (!got->multi_got()) 4962 got_type = GOT_TYPE_STANDARD; 4963 else 4964 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_; 4965 if (got->add_global(mips_sym, got_type)) 4966 { 4967 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type)); 4968 if (got->multi_got() && this->index_ > 0 4969 && parameters->options().output_is_position_independent()) 4970 target->rel_dyn_section(layout)->add_symbolless_global_addend( 4971 mips_sym, elfcpp::R_MIPS_REL32, got, 4972 mips_sym->got_offset(got_type)); 4973 } 4974 } 4975 } 4976 } 4977 4978 // Create GOT page entries. 4979 4980 template<int size, bool big_endian> 4981 void 4982 Mips_got_info<size, big_endian>::add_page_entries( 4983 Target_mips<size, big_endian>* target, Layout* layout) 4984 { 4985 if (this->page_gotno_ == 0) 4986 return; 4987 4988 Mips_output_data_got<size, big_endian>* got = target->got_section(); 4989 this->got_page_offset_start_ = got->add_constant(0); 4990 if (got->multi_got() && this->index_ > 0 4991 && parameters->options().output_is_position_independent()) 4992 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got, 4993 this->got_page_offset_start_); 4994 int num_entries = this->page_gotno_; 4995 unsigned int prev_offset = this->got_page_offset_start_; 4996 while (--num_entries > 0) 4997 { 4998 unsigned int next_offset = got->add_constant(0); 4999 if (got->multi_got() && this->index_ > 0 5000 && parameters->options().output_is_position_independent()) 5001 target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got, 5002 next_offset); 5003 gold_assert(next_offset == prev_offset + size/8); 5004 prev_offset = next_offset; 5005 } 5006 this->got_page_offset_next_ = this->got_page_offset_start_; 5007 } 5008 5009 // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY. 5010 5011 template<int size, bool big_endian> 5012 void 5013 Mips_got_info<size, big_endian>::add_global_entries( 5014 Target_mips<size, big_endian>* target, Layout* layout, 5015 unsigned int non_reloc_only_global_gotno) 5016 { 5017 Mips_output_data_got<size, big_endian>* got = target->got_section(); 5018 // Add GGA_NORMAL entries. 5019 unsigned int count = 0; 5020 for (typename Got_entry_set::iterator 5021 p = this->got_entries_.begin(); 5022 p != this->got_entries_.end(); 5023 ++p) 5024 { 5025 Mips_got_entry<size, big_endian>* entry = *p; 5026 if (!entry->is_for_global_symbol()) 5027 continue; 5028 5029 Mips_symbol<size>* mips_sym = entry->sym(); 5030 if (mips_sym->global_got_area() != GGA_NORMAL) 5031 continue; 5032 5033 unsigned int got_type; 5034 if (!got->multi_got()) 5035 got_type = GOT_TYPE_STANDARD; 5036 else 5037 // In multi-GOT links, global symbol can be in both primary and 5038 // secondary GOT(s). By creating custom GOT type 5039 // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol 5040 // is added to secondary GOT(s). 5041 got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_; 5042 if (!got->add_global(mips_sym, got_type)) 5043 continue; 5044 5045 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type)); 5046 if (got->multi_got() && this->index_ == 0) 5047 count++; 5048 if (got->multi_got() && this->index_ > 0) 5049 { 5050 if (parameters->options().output_is_position_independent() 5051 || (!parameters->doing_static_link() 5052 && mips_sym->is_from_dynobj() && !mips_sym->is_undefined())) 5053 { 5054 target->rel_dyn_section(layout)->add_global( 5055 mips_sym, elfcpp::R_MIPS_REL32, got, 5056 mips_sym->got_offset(got_type)); 5057 got->add_secondary_got_reloc(mips_sym->got_offset(got_type), 5058 elfcpp::R_MIPS_REL32, mips_sym); 5059 } 5060 } 5061 } 5062 5063 if (!got->multi_got() || this->index_ == 0) 5064 { 5065 if (got->multi_got()) 5066 { 5067 // We need to allocate space in the primary GOT for GGA_NORMAL entries 5068 // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY 5069 // entries correspond to dynamic symbol indexes. 5070 while (count < non_reloc_only_global_gotno) 5071 { 5072 got->add_constant(0); 5073 ++count; 5074 } 5075 } 5076 5077 // Add GGA_RELOC_ONLY entries. 5078 got->add_reloc_only_entries(); 5079 } 5080 } 5081 5082 // Create global GOT entries that should be in the GGA_RELOC_ONLY area. 5083 5084 template<int size, bool big_endian> 5085 void 5086 Mips_got_info<size, big_endian>::add_reloc_only_entries( 5087 Mips_output_data_got<size, big_endian>* got) 5088 { 5089 for (typename Unordered_set<Mips_symbol<size>*>::iterator 5090 p = this->global_got_symbols_.begin(); 5091 p != this->global_got_symbols_.end(); 5092 ++p) 5093 { 5094 Mips_symbol<size>* mips_sym = *p; 5095 if (mips_sym->global_got_area() == GGA_RELOC_ONLY) 5096 { 5097 unsigned int got_type; 5098 if (!got->multi_got()) 5099 got_type = GOT_TYPE_STANDARD; 5100 else 5101 got_type = GOT_TYPE_STANDARD_MULTIGOT; 5102 if (got->add_global(mips_sym, got_type)) 5103 mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type)); 5104 } 5105 } 5106 } 5107 5108 // Create TLS GOT entries. 5109 5110 template<int size, bool big_endian> 5111 void 5112 Mips_got_info<size, big_endian>::add_tls_entries( 5113 Target_mips<size, big_endian>* target, Layout* layout) 5114 { 5115 Mips_output_data_got<size, big_endian>* got = target->got_section(); 5116 // Add local tls entries. 5117 for (typename Got_entry_set::iterator 5118 p = this->got_entries_.begin(); 5119 p != this->got_entries_.end(); 5120 ++p) 5121 { 5122 Mips_got_entry<size, big_endian>* entry = *p; 5123 if (!entry->is_tls_entry() || !entry->is_for_local_symbol()) 5124 continue; 5125 5126 if (entry->tls_type() == GOT_TLS_GD) 5127 { 5128 unsigned int got_type = GOT_TYPE_TLS_PAIR; 5129 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32 5130 : elfcpp::R_MIPS_TLS_DTPMOD64); 5131 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32 5132 : elfcpp::R_MIPS_TLS_DTPREL64); 5133 5134 if (!parameters->doing_static_link()) 5135 { 5136 got->add_local_pair_with_rel(entry->object(), entry->symndx(), 5137 entry->shndx(), got_type, 5138 target->rel_dyn_section(layout), 5139 r_type1); 5140 unsigned int got_offset = 5141 entry->object()->local_got_offset(entry->symndx(), got_type); 5142 got->add_static_reloc(got_offset + size/8, r_type2, 5143 entry->object(), entry->symndx()); 5144 } 5145 else 5146 { 5147 // We are doing a static link. Mark it as belong to module 1, 5148 // the executable. 5149 unsigned int got_offset = got->add_constant(1); 5150 entry->object()->set_local_got_offset(entry->symndx(), got_type, 5151 got_offset); 5152 got->add_constant(0); 5153 got->add_static_reloc(got_offset + size/8, r_type2, 5154 entry->object(), entry->symndx()); 5155 } 5156 } 5157 else if (entry->tls_type() == GOT_TLS_IE) 5158 { 5159 unsigned int got_type = GOT_TYPE_TLS_OFFSET; 5160 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32 5161 : elfcpp::R_MIPS_TLS_TPREL64); 5162 if (!parameters->doing_static_link()) 5163 got->add_local_with_rel(entry->object(), entry->symndx(), got_type, 5164 target->rel_dyn_section(layout), r_type); 5165 else 5166 { 5167 got->add_local(entry->object(), entry->symndx(), got_type); 5168 unsigned int got_offset = 5169 entry->object()->local_got_offset(entry->symndx(), got_type); 5170 got->add_static_reloc(got_offset, r_type, entry->object(), 5171 entry->symndx()); 5172 } 5173 } 5174 else if (entry->tls_type() == GOT_TLS_LDM) 5175 { 5176 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32 5177 : elfcpp::R_MIPS_TLS_DTPMOD64); 5178 unsigned int got_offset; 5179 if (!parameters->doing_static_link()) 5180 { 5181 got_offset = got->add_constant(0); 5182 target->rel_dyn_section(layout)->add_local( 5183 entry->object(), 0, r_type, got, got_offset); 5184 } 5185 else 5186 // We are doing a static link. Just mark it as belong to module 1, 5187 // the executable. 5188 got_offset = got->add_constant(1); 5189 5190 got->add_constant(0); 5191 got->set_tls_ldm_offset(got_offset, entry->object()); 5192 } 5193 else 5194 gold_unreachable(); 5195 } 5196 5197 // Add global tls entries. 5198 for (typename Got_entry_set::iterator 5199 p = this->got_entries_.begin(); 5200 p != this->got_entries_.end(); 5201 ++p) 5202 { 5203 Mips_got_entry<size, big_endian>* entry = *p; 5204 if (!entry->is_tls_entry() || !entry->is_for_global_symbol()) 5205 continue; 5206 5207 Mips_symbol<size>* mips_sym = entry->sym(); 5208 if (entry->tls_type() == GOT_TLS_GD) 5209 { 5210 unsigned int got_type; 5211 if (!got->multi_got()) 5212 got_type = GOT_TYPE_TLS_PAIR; 5213 else 5214 got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_; 5215 unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32 5216 : elfcpp::R_MIPS_TLS_DTPMOD64); 5217 unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32 5218 : elfcpp::R_MIPS_TLS_DTPREL64); 5219 if (!parameters->doing_static_link()) 5220 got->add_global_pair_with_rel(mips_sym, got_type, 5221 target->rel_dyn_section(layout), r_type1, r_type2); 5222 else 5223 { 5224 // Add a GOT pair for for R_MIPS_TLS_GD. The creates a pair of 5225 // GOT entries. The first one is initialized to be 1, which is the 5226 // module index for the main executable and the second one 0. A 5227 // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for 5228 // the second GOT entry and will be applied by gold. 5229 unsigned int got_offset = got->add_constant(1); 5230 mips_sym->set_got_offset(got_type, got_offset); 5231 got->add_constant(0); 5232 got->add_static_reloc(got_offset + size/8, r_type2, mips_sym); 5233 } 5234 } 5235 else if (entry->tls_type() == GOT_TLS_IE) 5236 { 5237 unsigned int got_type; 5238 if (!got->multi_got()) 5239 got_type = GOT_TYPE_TLS_OFFSET; 5240 else 5241 got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_; 5242 unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32 5243 : elfcpp::R_MIPS_TLS_TPREL64); 5244 if (!parameters->doing_static_link()) 5245 got->add_global_with_rel(mips_sym, got_type, 5246 target->rel_dyn_section(layout), r_type); 5247 else 5248 { 5249 got->add_global(mips_sym, got_type); 5250 unsigned int got_offset = mips_sym->got_offset(got_type); 5251 got->add_static_reloc(got_offset, r_type, mips_sym); 5252 } 5253 } 5254 else 5255 gold_unreachable(); 5256 } 5257 } 5258 5259 // Decide whether the symbol needs an entry in the global part of the primary 5260 // GOT, setting global_got_area accordingly. Count the number of global 5261 // symbols that are in the primary GOT only because they have dynamic 5262 // relocations R_MIPS_REL32 against them (reloc_only_gotno). 5263 5264 template<int size, bool big_endian> 5265 void 5266 Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab) 5267 { 5268 for (typename Unordered_set<Mips_symbol<size>*>::iterator 5269 p = this->global_got_symbols_.begin(); 5270 p != this->global_got_symbols_.end(); 5271 ++p) 5272 { 5273 Mips_symbol<size>* sym = *p; 5274 // Make a final decision about whether the symbol belongs in the 5275 // local or global GOT. Symbols that bind locally can (and in the 5276 // case of forced-local symbols, must) live in the local GOT. 5277 // Those that are aren't in the dynamic symbol table must also 5278 // live in the local GOT. 5279 5280 if (!sym->should_add_dynsym_entry(symtab) 5281 || (sym->got_only_for_calls() 5282 ? symbol_calls_local(sym, sym->should_add_dynsym_entry(symtab)) 5283 : symbol_references_local(sym, 5284 sym->should_add_dynsym_entry(symtab)))) 5285 // The symbol belongs in the local GOT. We no longer need this 5286 // entry if it was only used for relocations; those relocations 5287 // will be against the null or section symbol instead. 5288 sym->set_global_got_area(GGA_NONE); 5289 else if (sym->global_got_area() == GGA_RELOC_ONLY) 5290 { 5291 ++this->reloc_only_gotno_; 5292 ++this->global_gotno_ ; 5293 } 5294 } 5295 } 5296 5297 // Return the offset of GOT page entry for VALUE. Initialize the entry with 5298 // VALUE if it is not initialized. 5299 5300 template<int size, bool big_endian> 5301 unsigned int 5302 Mips_got_info<size, big_endian>::get_got_page_offset(Mips_address value, 5303 Mips_output_data_got<size, big_endian>* got) 5304 { 5305 typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value); 5306 if (it != this->got_page_offsets_.end()) 5307 return it->second; 5308 5309 gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_ 5310 + (size/8) * this->page_gotno_); 5311 5312 unsigned int got_offset = this->got_page_offset_next_; 5313 this->got_page_offsets_[value] = got_offset; 5314 this->got_page_offset_next_ += size/8; 5315 got->update_got_entry(got_offset, value); 5316 return got_offset; 5317 } 5318 5319 // Remove lazy-binding stubs for global symbols in this GOT. 5320 5321 template<int size, bool big_endian> 5322 void 5323 Mips_got_info<size, big_endian>::remove_lazy_stubs( 5324 Target_mips<size, big_endian>* target) 5325 { 5326 for (typename Got_entry_set::iterator 5327 p = this->got_entries_.begin(); 5328 p != this->got_entries_.end(); 5329 ++p) 5330 { 5331 Mips_got_entry<size, big_endian>* entry = *p; 5332 if (entry->is_for_global_symbol()) 5333 target->remove_lazy_stub_entry(entry->sym()); 5334 } 5335 } 5336 5337 // Count the number of GOT entries required. 5338 5339 template<int size, bool big_endian> 5340 void 5341 Mips_got_info<size, big_endian>::count_got_entries() 5342 { 5343 for (typename Got_entry_set::iterator 5344 p = this->got_entries_.begin(); 5345 p != this->got_entries_.end(); 5346 ++p) 5347 { 5348 this->count_got_entry(*p); 5349 } 5350 } 5351 5352 // Count the number of GOT entries required by ENTRY. Accumulate the result. 5353 5354 template<int size, bool big_endian> 5355 void 5356 Mips_got_info<size, big_endian>::count_got_entry( 5357 Mips_got_entry<size, big_endian>* entry) 5358 { 5359 if (entry->is_tls_entry()) 5360 this->tls_gotno_ += mips_tls_got_entries(entry->tls_type()); 5361 else if (entry->is_for_local_symbol() 5362 || entry->sym()->global_got_area() == GGA_NONE) 5363 ++this->local_gotno_; 5364 else 5365 ++this->global_gotno_; 5366 } 5367 5368 // Add FROM's GOT entries. 5369 5370 template<int size, bool big_endian> 5371 void 5372 Mips_got_info<size, big_endian>::add_got_entries( 5373 Mips_got_info<size, big_endian>* from) 5374 { 5375 for (typename Got_entry_set::iterator 5376 p = from->got_entries_.begin(); 5377 p != from->got_entries_.end(); 5378 ++p) 5379 { 5380 Mips_got_entry<size, big_endian>* entry = *p; 5381 if (this->got_entries_.find(entry) == this->got_entries_.end()) 5382 { 5383 Mips_got_entry<size, big_endian>* entry2 = 5384 new Mips_got_entry<size, big_endian>(*entry); 5385 this->got_entries_.insert(entry2); 5386 this->count_got_entry(entry); 5387 } 5388 } 5389 } 5390 5391 // Add FROM's GOT page entries. 5392 5393 template<int size, bool big_endian> 5394 void 5395 Mips_got_info<size, big_endian>::add_got_page_entries( 5396 Mips_got_info<size, big_endian>* from) 5397 { 5398 for (typename Got_page_entry_set::iterator 5399 p = from->got_page_entries_.begin(); 5400 p != from->got_page_entries_.end(); 5401 ++p) 5402 { 5403 Got_page_entry* entry = *p; 5404 if (this->got_page_entries_.find(entry) == this->got_page_entries_.end()) 5405 { 5406 Got_page_entry* entry2 = new Got_page_entry(*entry); 5407 this->got_page_entries_.insert(entry2); 5408 this->page_gotno_ += entry->num_pages; 5409 } 5410 } 5411 } 5412 5413 // Mips_output_data_got methods. 5414 5415 // Lay out the GOT. Add local, global and TLS entries. If GOT is 5416 // larger than 64K, create multi-GOT. 5417 5418 template<int size, bool big_endian> 5419 void 5420 Mips_output_data_got<size, big_endian>::lay_out_got(Layout* layout, 5421 Symbol_table* symtab, const Input_objects* input_objects) 5422 { 5423 // Decide which symbols need to go in the global part of the GOT and 5424 // count the number of reloc-only GOT symbols. 5425 this->master_got_info_->count_got_symbols(symtab); 5426 5427 // Count the number of GOT entries. 5428 this->master_got_info_->count_got_entries(); 5429 5430 unsigned int got_size = this->master_got_info_->got_size(); 5431 if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE) 5432 this->lay_out_multi_got(layout, input_objects); 5433 else 5434 { 5435 // Record that all objects use single GOT. 5436 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); 5437 p != input_objects->relobj_end(); 5438 ++p) 5439 { 5440 Mips_relobj<size, big_endian>* object = 5441 Mips_relobj<size, big_endian>::as_mips_relobj(*p); 5442 if (object->get_got_info() != NULL) 5443 object->set_got_info(this->master_got_info_); 5444 } 5445 5446 this->master_got_info_->add_local_entries(this->target_, layout); 5447 this->master_got_info_->add_global_entries(this->target_, layout, 5448 /*not used*/-1U); 5449 this->master_got_info_->add_tls_entries(this->target_, layout); 5450 } 5451 } 5452 5453 // Create multi-GOT. For every GOT, add local, global and TLS entries. 5454 5455 template<int size, bool big_endian> 5456 void 5457 Mips_output_data_got<size, big_endian>::lay_out_multi_got(Layout* layout, 5458 const Input_objects* input_objects) 5459 { 5460 // Try to merge the GOTs of input objects together, as long as they 5461 // don't seem to exceed the maximum GOT size, choosing one of them 5462 // to be the primary GOT. 5463 this->merge_gots(input_objects); 5464 5465 // Every symbol that is referenced in a dynamic relocation must be 5466 // present in the primary GOT. 5467 this->primary_got_->set_global_gotno(this->master_got_info_->global_gotno()); 5468 5469 // Add GOT entries. 5470 unsigned int i = 0; 5471 unsigned int offset = 0; 5472 Mips_got_info<size, big_endian>* g = this->primary_got_; 5473 do 5474 { 5475 g->set_index(i); 5476 g->set_offset(offset); 5477 5478 g->add_local_entries(this->target_, layout); 5479 if (i == 0) 5480 g->add_global_entries(this->target_, layout, 5481 (this->master_got_info_->global_gotno() 5482 - this->master_got_info_->reloc_only_gotno())); 5483 else 5484 g->add_global_entries(this->target_, layout, /*not used*/-1U); 5485 g->add_tls_entries(this->target_, layout); 5486 5487 // Forbid global symbols in every non-primary GOT from having 5488 // lazy-binding stubs. 5489 if (i > 0) 5490 g->remove_lazy_stubs(this->target_); 5491 5492 ++i; 5493 offset += g->got_size(); 5494 g = g->next(); 5495 } 5496 while (g); 5497 } 5498 5499 // Attempt to merge GOTs of different input objects. Try to use as much as 5500 // possible of the primary GOT, since it doesn't require explicit dynamic 5501 // relocations, but don't use objects that would reference global symbols 5502 // out of the addressable range. Failing the primary GOT, attempt to merge 5503 // with the current GOT, or finish the current GOT and then make make the new 5504 // GOT current. 5505 5506 template<int size, bool big_endian> 5507 void 5508 Mips_output_data_got<size, big_endian>::merge_gots( 5509 const Input_objects* input_objects) 5510 { 5511 gold_assert(this->primary_got_ == NULL); 5512 Mips_got_info<size, big_endian>* current = NULL; 5513 5514 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); 5515 p != input_objects->relobj_end(); 5516 ++p) 5517 { 5518 Mips_relobj<size, big_endian>* object = 5519 Mips_relobj<size, big_endian>::as_mips_relobj(*p); 5520 5521 Mips_got_info<size, big_endian>* g = object->get_got_info(); 5522 if (g == NULL) 5523 continue; 5524 5525 g->count_got_entries(); 5526 5527 // Work out the number of page, local and TLS entries. 5528 unsigned int estimate = this->master_got_info_->page_gotno(); 5529 if (estimate > g->page_gotno()) 5530 estimate = g->page_gotno(); 5531 estimate += g->local_gotno() + g->tls_gotno(); 5532 5533 // We place TLS GOT entries after both locals and globals. The globals 5534 // for the primary GOT may overflow the normal GOT size limit, so be 5535 // sure not to merge a GOT which requires TLS with the primary GOT in that 5536 // case. This doesn't affect non-primary GOTs. 5537 estimate += (g->tls_gotno() > 0 ? this->master_got_info_->global_gotno() 5538 : g->global_gotno()); 5539 5540 unsigned int max_count = 5541 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2; 5542 if (estimate <= max_count) 5543 { 5544 // If we don't have a primary GOT, use it as 5545 // a starting point for the primary GOT. 5546 if (!this->primary_got_) 5547 { 5548 this->primary_got_ = g; 5549 continue; 5550 } 5551 5552 // Try merging with the primary GOT. 5553 if (this->merge_got_with(g, object, this->primary_got_)) 5554 continue; 5555 } 5556 5557 // If we can merge with the last-created GOT, do it. 5558 if (current && this->merge_got_with(g, object, current)) 5559 continue; 5560 5561 // Well, we couldn't merge, so create a new GOT. Don't check if it 5562 // fits; if it turns out that it doesn't, we'll get relocation 5563 // overflows anyway. 5564 g->set_next(current); 5565 current = g; 5566 } 5567 5568 // If we do not find any suitable primary GOT, create an empty one. 5569 if (this->primary_got_ == NULL) 5570 this->primary_got_ = new Mips_got_info<size, big_endian>(); 5571 5572 // Link primary GOT with secondary GOTs. 5573 this->primary_got_->set_next(current); 5574 } 5575 5576 // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if 5577 // this would lead to overflow, true if they were merged successfully. 5578 5579 template<int size, bool big_endian> 5580 bool 5581 Mips_output_data_got<size, big_endian>::merge_got_with( 5582 Mips_got_info<size, big_endian>* from, 5583 Mips_relobj<size, big_endian>* object, 5584 Mips_got_info<size, big_endian>* to) 5585 { 5586 // Work out how many page entries we would need for the combined GOT. 5587 unsigned int estimate = this->master_got_info_->page_gotno(); 5588 if (estimate >= from->page_gotno() + to->page_gotno()) 5589 estimate = from->page_gotno() + to->page_gotno(); 5590 5591 // Conservatively estimate how many local and TLS entries would be needed. 5592 estimate += from->local_gotno() + to->local_gotno(); 5593 estimate += from->tls_gotno() + to->tls_gotno(); 5594 5595 // If we're merging with the primary got, any TLS relocations will 5596 // come after the full set of global entries. Otherwise estimate those 5597 // conservatively as well. 5598 if (to == this->primary_got_ && (from->tls_gotno() + to->tls_gotno()) > 0) 5599 estimate += this->master_got_info_->global_gotno(); 5600 else 5601 estimate += from->global_gotno() + to->global_gotno(); 5602 5603 // Bail out if the combined GOT might be too big. 5604 unsigned int max_count = 5605 Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2; 5606 if (estimate > max_count) 5607 return false; 5608 5609 // Transfer the object's GOT information from FROM to TO. 5610 to->add_got_entries(from); 5611 to->add_got_page_entries(from); 5612 5613 // Record that OBJECT should use output GOT TO. 5614 object->set_got_info(to); 5615 5616 return true; 5617 } 5618 5619 // Write out the GOT. 5620 5621 template<int size, bool big_endian> 5622 void 5623 Mips_output_data_got<size, big_endian>::do_write(Output_file* of) 5624 { 5625 // Call parent to write out GOT. 5626 Output_data_got<size, big_endian>::do_write(of); 5627 5628 const off_t offset = this->offset(); 5629 const section_size_type oview_size = 5630 convert_to_section_size_type(this->data_size()); 5631 unsigned char* const oview = of->get_output_view(offset, oview_size); 5632 5633 // Needed for fixing values of .got section. 5634 this->got_view_ = oview; 5635 5636 // Write lazy stub addresses. 5637 for (typename Unordered_set<Mips_symbol<size>*>::iterator 5638 p = this->master_got_info_->global_got_symbols().begin(); 5639 p != this->master_got_info_->global_got_symbols().end(); 5640 ++p) 5641 { 5642 Mips_symbol<size>* mips_sym = *p; 5643 if (mips_sym->has_lazy_stub()) 5644 { 5645 Valtype* wv = reinterpret_cast<Valtype*>( 5646 oview + this->get_primary_got_offset(mips_sym)); 5647 Valtype value = 5648 this->target_->mips_stubs_section()->stub_address(mips_sym); 5649 elfcpp::Swap<size, big_endian>::writeval(wv, value); 5650 } 5651 } 5652 5653 // Add +1 to GGA_NONE nonzero MIPS16 and microMIPS entries. 5654 for (typename Unordered_set<Mips_symbol<size>*>::iterator 5655 p = this->master_got_info_->global_got_symbols().begin(); 5656 p != this->master_got_info_->global_got_symbols().end(); 5657 ++p) 5658 { 5659 Mips_symbol<size>* mips_sym = *p; 5660 if (!this->multi_got() 5661 && (mips_sym->is_mips16() || mips_sym->is_micromips()) 5662 && mips_sym->global_got_area() == GGA_NONE 5663 && mips_sym->has_got_offset(GOT_TYPE_STANDARD)) 5664 { 5665 Valtype* wv = reinterpret_cast<Valtype*>( 5666 oview + mips_sym->got_offset(GOT_TYPE_STANDARD)); 5667 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv); 5668 if (value != 0) 5669 { 5670 value |= 1; 5671 elfcpp::Swap<size, big_endian>::writeval(wv, value); 5672 } 5673 } 5674 } 5675 5676 if (!this->secondary_got_relocs_.empty()) 5677 { 5678 // Fixup for the secondary GOT R_MIPS_REL32 relocs. For global 5679 // secondary GOT entries with non-zero initial value copy the value 5680 // to the corresponding primary GOT entry, and set the secondary GOT 5681 // entry to zero. 5682 // TODO(sasa): This is workaround. It needs to be investigated further. 5683 5684 for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i) 5685 { 5686 Static_reloc& reloc(this->secondary_got_relocs_[i]); 5687 if (reloc.symbol_is_global()) 5688 { 5689 Mips_symbol<size>* gsym = reloc.symbol(); 5690 gold_assert(gsym != NULL); 5691 5692 unsigned got_offset = reloc.got_offset(); 5693 gold_assert(got_offset < oview_size); 5694 5695 // Find primary GOT entry. 5696 Valtype* wv_prim = reinterpret_cast<Valtype*>( 5697 oview + this->get_primary_got_offset(gsym)); 5698 5699 // Find secondary GOT entry. 5700 Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset); 5701 5702 Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec); 5703 if (value != 0) 5704 { 5705 elfcpp::Swap<size, big_endian>::writeval(wv_prim, value); 5706 elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0); 5707 gsym->set_applied_secondary_got_fixup(); 5708 } 5709 } 5710 } 5711 5712 of->write_output_view(offset, oview_size, oview); 5713 } 5714 5715 // We are done if there is no fix up. 5716 if (this->static_relocs_.empty()) 5717 return; 5718 5719 Output_segment* tls_segment = this->layout_->tls_segment(); 5720 gold_assert(tls_segment != NULL); 5721 5722 for (size_t i = 0; i < this->static_relocs_.size(); ++i) 5723 { 5724 Static_reloc& reloc(this->static_relocs_[i]); 5725 5726 Mips_address value; 5727 if (!reloc.symbol_is_global()) 5728 { 5729 Sized_relobj_file<size, big_endian>* object = reloc.relobj(); 5730 const Symbol_value<size>* psymval = 5731 object->local_symbol(reloc.index()); 5732 5733 // We are doing static linking. Issue an error and skip this 5734 // relocation if the symbol is undefined or in a discarded_section. 5735 bool is_ordinary; 5736 unsigned int shndx = psymval->input_shndx(&is_ordinary); 5737 if ((shndx == elfcpp::SHN_UNDEF) 5738 || (is_ordinary 5739 && shndx != elfcpp::SHN_UNDEF 5740 && !object->is_section_included(shndx) 5741 && !this->symbol_table_->is_section_folded(object, shndx))) 5742 { 5743 gold_error(_("undefined or discarded local symbol %u from " 5744 " object %s in GOT"), 5745 reloc.index(), reloc.relobj()->name().c_str()); 5746 continue; 5747 } 5748 5749 value = psymval->value(object, 0); 5750 } 5751 else 5752 { 5753 const Mips_symbol<size>* gsym = reloc.symbol(); 5754 gold_assert(gsym != NULL); 5755 5756 // We are doing static linking. Issue an error and skip this 5757 // relocation if the symbol is undefined or in a discarded_section 5758 // unless it is a weakly_undefined symbol. 5759 if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined()) 5760 && !gsym->is_weak_undefined()) 5761 { 5762 gold_error(_("undefined or discarded symbol %s in GOT"), 5763 gsym->name()); 5764 continue; 5765 } 5766 5767 if (!gsym->is_weak_undefined()) 5768 value = gsym->value(); 5769 else 5770 value = 0; 5771 } 5772 5773 unsigned got_offset = reloc.got_offset(); 5774 gold_assert(got_offset < oview_size); 5775 5776 Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset); 5777 Valtype x; 5778 5779 switch (reloc.r_type()) 5780 { 5781 case elfcpp::R_MIPS_TLS_DTPMOD32: 5782 case elfcpp::R_MIPS_TLS_DTPMOD64: 5783 x = value; 5784 break; 5785 case elfcpp::R_MIPS_TLS_DTPREL32: 5786 case elfcpp::R_MIPS_TLS_DTPREL64: 5787 x = value - elfcpp::DTP_OFFSET; 5788 break; 5789 case elfcpp::R_MIPS_TLS_TPREL32: 5790 case elfcpp::R_MIPS_TLS_TPREL64: 5791 x = value - elfcpp::TP_OFFSET; 5792 break; 5793 default: 5794 gold_unreachable(); 5795 break; 5796 } 5797 5798 elfcpp::Swap<size, big_endian>::writeval(wv, x); 5799 } 5800 5801 of->write_output_view(offset, oview_size, oview); 5802 } 5803 5804 // Mips_relobj methods. 5805 5806 // Count the local symbols. The Mips backend needs to know if a symbol 5807 // is a MIPS16 or microMIPS function or not. For global symbols, it is easy 5808 // because the Symbol object keeps the ELF symbol type and st_other field. 5809 // For local symbol it is harder because we cannot access this information. 5810 // So we override the do_count_local_symbol in parent and scan local symbols to 5811 // mark MIPS16 and microMIPS functions. This is not the most efficient way but 5812 // I do not want to slow down other ports by calling a per symbol target hook 5813 // inside Sized_relobj_file<size, big_endian>::do_count_local_symbols. 5814 5815 template<int size, bool big_endian> 5816 void 5817 Mips_relobj<size, big_endian>::do_count_local_symbols( 5818 Stringpool_template<char>* pool, 5819 Stringpool_template<char>* dynpool) 5820 { 5821 // Ask parent to count the local symbols. 5822 Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool); 5823 const unsigned int loccount = this->local_symbol_count(); 5824 if (loccount == 0) 5825 return; 5826 5827 // Initialize the mips16 and micromips function bit-vector. 5828 this->local_symbol_is_mips16_.resize(loccount, false); 5829 this->local_symbol_is_micromips_.resize(loccount, false); 5830 5831 // Read the symbol table section header. 5832 const unsigned int symtab_shndx = this->symtab_shndx(); 5833 elfcpp::Shdr<size, big_endian> 5834 symtabshdr(this, this->elf_file()->section_header(symtab_shndx)); 5835 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); 5836 5837 // Read the local symbols. 5838 const int sym_size = elfcpp::Elf_sizes<size>::sym_size; 5839 gold_assert(loccount == symtabshdr.get_sh_info()); 5840 off_t locsize = loccount * sym_size; 5841 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), 5842 locsize, true, true); 5843 5844 // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols. 5845 5846 // Skip the first dummy symbol. 5847 psyms += sym_size; 5848 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size) 5849 { 5850 elfcpp::Sym<size, big_endian> sym(psyms); 5851 unsigned char st_other = sym.get_st_other(); 5852 this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other); 5853 this->local_symbol_is_micromips_[i] = 5854 elfcpp::elf_st_is_micromips(st_other); 5855 } 5856 } 5857 5858 // Read the symbol information. 5859 5860 template<int size, bool big_endian> 5861 void 5862 Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd) 5863 { 5864 // Call parent class to read symbol information. 5865 this->base_read_symbols(sd); 5866 5867 // Read processor-specific flags in ELF file header. 5868 const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset, 5869 elfcpp::Elf_sizes<size>::ehdr_size, 5870 true, false); 5871 elfcpp::Ehdr<size, big_endian> ehdr(pehdr); 5872 this->processor_specific_flags_ = ehdr.get_e_flags(); 5873 5874 // Get the section names. 5875 const unsigned char* pnamesu = sd->section_names->data(); 5876 const char* pnames = reinterpret_cast<const char*>(pnamesu); 5877 5878 // Initialize the mips16 stub section bit-vectors. 5879 this->section_is_mips16_fn_stub_.resize(this->shnum(), false); 5880 this->section_is_mips16_call_stub_.resize(this->shnum(), false); 5881 this->section_is_mips16_call_fp_stub_.resize(this->shnum(), false); 5882 5883 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size; 5884 const unsigned char* pshdrs = sd->section_headers->data(); 5885 const unsigned char* ps = pshdrs + shdr_size; 5886 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size) 5887 { 5888 elfcpp::Shdr<size, big_endian> shdr(ps); 5889 5890 if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO) 5891 { 5892 // Read the gp value that was used to create this object. We need the 5893 // gp value while processing relocs. The .reginfo section is not used 5894 // in the 64-bit MIPS ELF ABI. 5895 section_offset_type section_offset = shdr.get_sh_offset(); 5896 section_size_type section_size = 5897 convert_to_section_size_type(shdr.get_sh_size()); 5898 const unsigned char* view = 5899 this->get_view(section_offset, section_size, true, false); 5900 5901 this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20); 5902 5903 // Read the rest of .reginfo. 5904 this->gprmask_ = elfcpp::Swap<size, big_endian>::readval(view); 5905 this->cprmask1_ = elfcpp::Swap<size, big_endian>::readval(view + 4); 5906 this->cprmask2_ = elfcpp::Swap<size, big_endian>::readval(view + 8); 5907 this->cprmask3_ = elfcpp::Swap<size, big_endian>::readval(view + 12); 5908 this->cprmask4_ = elfcpp::Swap<size, big_endian>::readval(view + 16); 5909 } 5910 5911 const char* name = pnames + shdr.get_sh_name(); 5912 this->section_is_mips16_fn_stub_[i] = is_prefix_of(".mips16.fn", name); 5913 this->section_is_mips16_call_stub_[i] = 5914 is_prefix_of(".mips16.call.", name); 5915 this->section_is_mips16_call_fp_stub_[i] = 5916 is_prefix_of(".mips16.call.fp.", name); 5917 5918 if (strcmp(name, ".pdr") == 0) 5919 { 5920 gold_assert(this->pdr_shndx_ == -1U); 5921 this->pdr_shndx_ = i; 5922 } 5923 } 5924 } 5925 5926 // Discard MIPS16 stub secions that are not needed. 5927 5928 template<int size, bool big_endian> 5929 void 5930 Mips_relobj<size, big_endian>::discard_mips16_stub_sections(Symbol_table* symtab) 5931 { 5932 for (typename Mips16_stubs_int_map::const_iterator 5933 it = this->mips16_stub_sections_.begin(); 5934 it != this->mips16_stub_sections_.end(); ++it) 5935 { 5936 Mips16_stub_section<size, big_endian>* stub_section = it->second; 5937 if (!stub_section->is_target_found()) 5938 { 5939 gold_error(_("no relocation found in mips16 stub section '%s'"), 5940 stub_section->object() 5941 ->section_name(stub_section->shndx()).c_str()); 5942 } 5943 5944 bool discard = false; 5945 if (stub_section->is_for_local_function()) 5946 { 5947 if (stub_section->is_fn_stub()) 5948 { 5949 // This stub is for a local symbol. This stub will only 5950 // be needed if there is some relocation in this object, 5951 // other than a 16 bit function call, which refers to this 5952 // symbol. 5953 if (!this->has_local_non_16bit_call_relocs(stub_section->r_sym())) 5954 discard = true; 5955 else 5956 this->add_local_mips16_fn_stub(stub_section); 5957 } 5958 else 5959 { 5960 // This stub is for a local symbol. This stub will only 5961 // be needed if there is some relocation (R_MIPS16_26) in 5962 // this object that refers to this symbol. 5963 gold_assert(stub_section->is_call_stub() 5964 || stub_section->is_call_fp_stub()); 5965 if (!this->has_local_16bit_call_relocs(stub_section->r_sym())) 5966 discard = true; 5967 else 5968 this->add_local_mips16_call_stub(stub_section); 5969 } 5970 } 5971 else 5972 { 5973 Mips_symbol<size>* gsym = stub_section->gsym(); 5974 if (stub_section->is_fn_stub()) 5975 { 5976 if (gsym->has_mips16_fn_stub()) 5977 // We already have a stub for this function. 5978 discard = true; 5979 else 5980 { 5981 gsym->set_mips16_fn_stub(stub_section); 5982 if (gsym->should_add_dynsym_entry(symtab)) 5983 { 5984 // If we have a MIPS16 function with a stub, the 5985 // dynamic symbol must refer to the stub, since only 5986 // the stub uses the standard calling conventions. 5987 gsym->set_need_fn_stub(); 5988 if (gsym->is_from_dynobj()) 5989 gsym->set_needs_dynsym_value(); 5990 } 5991 } 5992 if (!gsym->need_fn_stub()) 5993 discard = true; 5994 } 5995 else if (stub_section->is_call_stub()) 5996 { 5997 if (gsym->is_mips16()) 5998 // We don't need the call_stub; this is a 16 bit 5999 // function, so calls from other 16 bit functions are 6000 // OK. 6001 discard = true; 6002 else if (gsym->has_mips16_call_stub()) 6003 // We already have a stub for this function. 6004 discard = true; 6005 else 6006 gsym->set_mips16_call_stub(stub_section); 6007 } 6008 else 6009 { 6010 gold_assert(stub_section->is_call_fp_stub()); 6011 if (gsym->is_mips16()) 6012 // We don't need the call_stub; this is a 16 bit 6013 // function, so calls from other 16 bit functions are 6014 // OK. 6015 discard = true; 6016 else if (gsym->has_mips16_call_fp_stub()) 6017 // We already have a stub for this function. 6018 discard = true; 6019 else 6020 gsym->set_mips16_call_fp_stub(stub_section); 6021 } 6022 } 6023 if (discard) 6024 this->set_output_section(stub_section->shndx(), NULL); 6025 } 6026 } 6027 6028 // Mips_output_data_la25_stub methods. 6029 6030 // Template for standard LA25 stub. 6031 template<int size, bool big_endian> 6032 const uint32_t 6033 Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[] = 6034 { 6035 0x3c190000, // lui $25,%hi(func) 6036 0x08000000, // j func 6037 0x27390000, // add $25,$25,%lo(func) 6038 0x00000000 // nop 6039 }; 6040 6041 // Template for microMIPS LA25 stub. 6042 template<int size, bool big_endian> 6043 const uint32_t 6044 Mips_output_data_la25_stub<size, big_endian>::la25_stub_micromips_entry[] = 6045 { 6046 0x41b9, 0x0000, // lui t9,%hi(func) 6047 0xd400, 0x0000, // j func 6048 0x3339, 0x0000, // addiu t9,t9,%lo(func) 6049 0x0000, 0x0000 // nop 6050 }; 6051 6052 // Create la25 stub for a symbol. 6053 6054 template<int size, bool big_endian> 6055 void 6056 Mips_output_data_la25_stub<size, big_endian>::create_la25_stub( 6057 Symbol_table* symtab, Target_mips<size, big_endian>* target, 6058 Mips_symbol<size>* gsym) 6059 { 6060 if (!gsym->has_la25_stub()) 6061 { 6062 gsym->set_la25_stub_offset(this->symbols_.size() * 16); 6063 this->symbols_.insert(gsym); 6064 this->create_stub_symbol(gsym, symtab, target, 16); 6065 } 6066 } 6067 6068 // Create a symbol for SYM stub's value and size, to help make the disassembly 6069 // easier to read. 6070 6071 template<int size, bool big_endian> 6072 void 6073 Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol( 6074 Mips_symbol<size>* sym, Symbol_table* symtab, 6075 Target_mips<size, big_endian>* target, uint64_t symsize) 6076 { 6077 std::string name(".pic."); 6078 name += sym->name(); 6079 6080 unsigned int offset = sym->la25_stub_offset(); 6081 if (sym->is_micromips()) 6082 offset |= 1; 6083 6084 // Make it a local function. 6085 Symbol* new_sym = symtab->define_in_output_data(name.c_str(), NULL, 6086 Symbol_table::PREDEFINED, 6087 target->la25_stub_section(), 6088 offset, symsize, elfcpp::STT_FUNC, 6089 elfcpp::STB_LOCAL, 6090 elfcpp::STV_DEFAULT, 0, 6091 false, false); 6092 new_sym->set_is_forced_local(); 6093 } 6094 6095 // Write out la25 stubs. This uses the hand-coded instructions above, 6096 // and adjusts them as needed. 6097 6098 template<int size, bool big_endian> 6099 void 6100 Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of) 6101 { 6102 const off_t offset = this->offset(); 6103 const section_size_type oview_size = 6104 convert_to_section_size_type(this->data_size()); 6105 unsigned char* const oview = of->get_output_view(offset, oview_size); 6106 6107 for (typename Unordered_set<Mips_symbol<size>*>::iterator 6108 p = this->symbols_.begin(); 6109 p != this->symbols_.end(); 6110 ++p) 6111 { 6112 Mips_symbol<size>* sym = *p; 6113 unsigned char* pov = oview + sym->la25_stub_offset(); 6114 6115 Mips_address target = sym->value(); 6116 if (!sym->is_micromips()) 6117 { 6118 elfcpp::Swap<32, big_endian>::writeval(pov, 6119 la25_stub_entry[0] | (((target + 0x8000) >> 16) & 0xffff)); 6120 elfcpp::Swap<32, big_endian>::writeval(pov + 4, 6121 la25_stub_entry[1] | ((target >> 2) & 0x3ffffff)); 6122 elfcpp::Swap<32, big_endian>::writeval(pov + 8, 6123 la25_stub_entry[2] | (target & 0xffff)); 6124 elfcpp::Swap<32, big_endian>::writeval(pov + 12, la25_stub_entry[3]); 6125 } 6126 else 6127 { 6128 target |= 1; 6129 // First stub instruction. Paste high 16-bits of the target. 6130 elfcpp::Swap<16, big_endian>::writeval(pov, 6131 la25_stub_micromips_entry[0]); 6132 elfcpp::Swap<16, big_endian>::writeval(pov + 2, 6133 ((target + 0x8000) >> 16) & 0xffff); 6134 // Second stub instruction. Paste low 26-bits of the target, shifted 6135 // right by 1. 6136 elfcpp::Swap<16, big_endian>::writeval(pov + 4, 6137 la25_stub_micromips_entry[2] | ((target >> 17) & 0x3ff)); 6138 elfcpp::Swap<16, big_endian>::writeval(pov + 6, 6139 la25_stub_micromips_entry[3] | ((target >> 1) & 0xffff)); 6140 // Third stub instruction. Paste low 16-bits of the target. 6141 elfcpp::Swap<16, big_endian>::writeval(pov + 8, 6142 la25_stub_micromips_entry[4]); 6143 elfcpp::Swap<16, big_endian>::writeval(pov + 10, target & 0xffff); 6144 // Fourth stub instruction. 6145 elfcpp::Swap<16, big_endian>::writeval(pov + 12, 6146 la25_stub_micromips_entry[6]); 6147 elfcpp::Swap<16, big_endian>::writeval(pov + 14, 6148 la25_stub_micromips_entry[7]); 6149 } 6150 } 6151 6152 of->write_output_view(offset, oview_size, oview); 6153 } 6154 6155 // Mips_output_data_plt methods. 6156 6157 // The format of the first PLT entry in an O32 executable. 6158 template<int size, bool big_endian> 6159 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[] = 6160 { 6161 0x3c1c0000, // lui $28, %hi(&GOTPLT[0]) 6162 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28) 6163 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0]) 6164 0x031cc023, // subu $24, $24, $28 6165 0x03e07821, // move $15, $31 # 32-bit move (addu) 6166 0x0018c082, // srl $24, $24, 2 6167 0x0320f809, // jalr $25 6168 0x2718fffe // subu $24, $24, 2 6169 }; 6170 6171 // The format of the first PLT entry in an N32 executable. Different 6172 // because gp ($28) is not available; we use t2 ($14) instead. 6173 template<int size, bool big_endian> 6174 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[] = 6175 { 6176 0x3c0e0000, // lui $14, %hi(&GOTPLT[0]) 6177 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14) 6178 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0]) 6179 0x030ec023, // subu $24, $24, $14 6180 0x03e07821, // move $15, $31 # 32-bit move (addu) 6181 0x0018c082, // srl $24, $24, 2 6182 0x0320f809, // jalr $25 6183 0x2718fffe // subu $24, $24, 2 6184 }; 6185 6186 // The format of the first PLT entry in an N64 executable. Different 6187 // from N32 because of the increased size of GOT entries. 6188 template<int size, bool big_endian> 6189 const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[] = 6190 { 6191 0x3c0e0000, // lui $14, %hi(&GOTPLT[0]) 6192 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14) 6193 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0]) 6194 0x030ec023, // subu $24, $24, $14 6195 0x03e07821, // move $15, $31 # 64-bit move (daddu) 6196 0x0018c0c2, // srl $24, $24, 3 6197 0x0320f809, // jalr $25 6198 0x2718fffe // subu $24, $24, 2 6199 }; 6200 6201 // The format of the microMIPS first PLT entry in an O32 executable. 6202 // We rely on v0 ($2) rather than t8 ($24) to contain the address 6203 // of the GOTPLT entry handled, so this stub may only be used when 6204 // all the subsequent PLT entries are microMIPS code too. 6205 // 6206 // The trailing NOP is for alignment and correct disassembly only. 6207 template<int size, bool big_endian> 6208 const uint32_t Mips_output_data_plt<size, big_endian>:: 6209 plt0_entry_micromips_o32[] = 6210 { 6211 0x7980, 0x0000, // addiupc $3, (&GOTPLT[0]) - . 6212 0xff23, 0x0000, // lw $25, 0($3) 6213 0x0535, // subu $2, $2, $3 6214 0x2525, // srl $2, $2, 2 6215 0x3302, 0xfffe, // subu $24, $2, 2 6216 0x0dff, // move $15, $31 6217 0x45f9, // jalrs $25 6218 0x0f83, // move $28, $3 6219 0x0c00 // nop 6220 }; 6221 6222 // The format of the microMIPS first PLT entry in an O32 executable 6223 // in the insn32 mode. 6224 template<int size, bool big_endian> 6225 const uint32_t Mips_output_data_plt<size, big_endian>:: 6226 plt0_entry_micromips32_o32[] = 6227 { 6228 0x41bc, 0x0000, // lui $28, %hi(&GOTPLT[0]) 6229 0xff3c, 0x0000, // lw $25, %lo(&GOTPLT[0])($28) 6230 0x339c, 0x0000, // addiu $28, $28, %lo(&GOTPLT[0]) 6231 0x0398, 0xc1d0, // subu $24, $24, $28 6232 0x001f, 0x7950, // move $15, $31 6233 0x0318, 0x1040, // srl $24, $24, 2 6234 0x03f9, 0x0f3c, // jalr $25 6235 0x3318, 0xfffe // subu $24, $24, 2 6236 }; 6237 6238 // The format of subsequent standard entries in the PLT. 6239 template<int size, bool big_endian> 6240 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[] = 6241 { 6242 0x3c0f0000, // lui $15, %hi(.got.plt entry) 6243 0x8df90000, // l[wd] $25, %lo(.got.plt entry)($15) 6244 0x03200008, // jr $25 6245 0x25f80000 // addiu $24, $15, %lo(.got.plt entry) 6246 }; 6247 6248 // The format of subsequent MIPS16 o32 PLT entries. We use v1 ($3) as a 6249 // temporary because t8 ($24) and t9 ($25) are not directly addressable. 6250 // Note that this differs from the GNU ld which uses both v0 ($2) and v1 ($3). 6251 // We cannot use v0 because MIPS16 call stubs from the CS toolchain expect 6252 // target function address in register v0. 6253 template<int size, bool big_endian> 6254 const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry_mips16_o32[] = 6255 { 6256 0xb303, // lw $3, 12($pc) 6257 0x651b, // move $24, $3 6258 0x9b60, // lw $3, 0($3) 6259 0xeb00, // jr $3 6260 0x653b, // move $25, $3 6261 0x6500, // nop 6262 0x0000, 0x0000 // .word (.got.plt entry) 6263 }; 6264 6265 // The format of subsequent microMIPS o32 PLT entries. We use v0 ($2) 6266 // as a temporary because t8 ($24) is not addressable with ADDIUPC. 6267 template<int size, bool big_endian> 6268 const uint32_t Mips_output_data_plt<size, big_endian>:: 6269 plt_entry_micromips_o32[] = 6270 { 6271 0x7900, 0x0000, // addiupc $2, (.got.plt entry) - . 6272 0xff22, 0x0000, // lw $25, 0($2) 6273 0x4599, // jr $25 6274 0x0f02 // move $24, $2 6275 }; 6276 6277 // The format of subsequent microMIPS o32 PLT entries in the insn32 mode. 6278 template<int size, bool big_endian> 6279 const uint32_t Mips_output_data_plt<size, big_endian>:: 6280 plt_entry_micromips32_o32[] = 6281 { 6282 0x41af, 0x0000, // lui $15, %hi(.got.plt entry) 6283 0xff2f, 0x0000, // lw $25, %lo(.got.plt entry)($15) 6284 0x0019, 0x0f3c, // jr $25 6285 0x330f, 0x0000 // addiu $24, $15, %lo(.got.plt entry) 6286 }; 6287 6288 // Add an entry to the PLT for a symbol referenced by r_type relocation. 6289 6290 template<int size, bool big_endian> 6291 void 6292 Mips_output_data_plt<size, big_endian>::add_entry(Mips_symbol<size>* gsym, 6293 unsigned int r_type) 6294 { 6295 gold_assert(!gsym->has_plt_offset()); 6296 6297 // Final PLT offset for a symbol will be set in method set_plt_offsets(). 6298 gsym->set_plt_offset(this->entry_count() * sizeof(plt_entry) 6299 + sizeof(plt0_entry_o32)); 6300 this->symbols_.push_back(gsym); 6301 6302 // Record whether the relocation requires a standard MIPS 6303 // or a compressed code entry. 6304 if (jal_reloc(r_type)) 6305 { 6306 if (r_type == elfcpp::R_MIPS_26) 6307 gsym->set_needs_mips_plt(true); 6308 else 6309 gsym->set_needs_comp_plt(true); 6310 } 6311 6312 section_offset_type got_offset = this->got_plt_->current_data_size(); 6313 6314 // Every PLT entry needs a GOT entry which points back to the PLT 6315 // entry (this will be changed by the dynamic linker, normally 6316 // lazily when the function is called). 6317 this->got_plt_->set_current_data_size(got_offset + size/8); 6318 6319 gsym->set_needs_dynsym_entry(); 6320 this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_, 6321 got_offset); 6322 } 6323 6324 // Set final PLT offsets. For each symbol, determine whether standard or 6325 // compressed (MIPS16 or microMIPS) PLT entry is used. 6326 6327 template<int size, bool big_endian> 6328 void 6329 Mips_output_data_plt<size, big_endian>::set_plt_offsets() 6330 { 6331 // The sizes of individual PLT entries. 6332 unsigned int plt_mips_entry_size = this->standard_plt_entry_size(); 6333 unsigned int plt_comp_entry_size = (!this->target_->is_output_newabi() 6334 ? this->compressed_plt_entry_size() : 0); 6335 6336 for (typename std::vector<Mips_symbol<size>*>::const_iterator 6337 p = this->symbols_.begin(); p != this->symbols_.end(); ++p) 6338 { 6339 Mips_symbol<size>* mips_sym = *p; 6340 6341 // There are no defined MIPS16 or microMIPS PLT entries for n32 or n64, 6342 // so always use a standard entry there. 6343 // 6344 // If the symbol has a MIPS16 call stub and gets a PLT entry, then 6345 // all MIPS16 calls will go via that stub, and there is no benefit 6346 // to having a MIPS16 entry. And in the case of call_stub a 6347 // standard entry actually has to be used as the stub ends with a J 6348 // instruction. 6349 if (this->target_->is_output_newabi() 6350 || mips_sym->has_mips16_call_stub() 6351 || mips_sym->has_mips16_call_fp_stub()) 6352 { 6353 mips_sym->set_needs_mips_plt(true); 6354 mips_sym->set_needs_comp_plt(false); 6355 } 6356 6357 // Otherwise, if there are no direct calls to the function, we 6358 // have a free choice of whether to use standard or compressed 6359 // entries. Prefer microMIPS entries if the object is known to 6360 // contain microMIPS code, so that it becomes possible to create 6361 // pure microMIPS binaries. Prefer standard entries otherwise, 6362 // because MIPS16 ones are no smaller and are usually slower. 6363 if (!mips_sym->needs_mips_plt() && !mips_sym->needs_comp_plt()) 6364 { 6365 if (this->target_->is_output_micromips()) 6366 mips_sym->set_needs_comp_plt(true); 6367 else 6368 mips_sym->set_needs_mips_plt(true); 6369 } 6370 6371 if (mips_sym->needs_mips_plt()) 6372 { 6373 mips_sym->set_mips_plt_offset(this->plt_mips_offset_); 6374 this->plt_mips_offset_ += plt_mips_entry_size; 6375 } 6376 if (mips_sym->needs_comp_plt()) 6377 { 6378 mips_sym->set_comp_plt_offset(this->plt_comp_offset_); 6379 this->plt_comp_offset_ += plt_comp_entry_size; 6380 } 6381 } 6382 6383 // Figure out the size of the PLT header if we know that we are using it. 6384 if (this->plt_mips_offset_ + this->plt_comp_offset_ != 0) 6385 this->plt_header_size_ = this->get_plt_header_size(); 6386 } 6387 6388 // Write out the PLT. This uses the hand-coded instructions above, 6389 // and adjusts them as needed. 6390 6391 template<int size, bool big_endian> 6392 void 6393 Mips_output_data_plt<size, big_endian>::do_write(Output_file* of) 6394 { 6395 const off_t offset = this->offset(); 6396 const section_size_type oview_size = 6397 convert_to_section_size_type(this->data_size()); 6398 unsigned char* const oview = of->get_output_view(offset, oview_size); 6399 6400 const off_t gotplt_file_offset = this->got_plt_->offset(); 6401 const section_size_type gotplt_size = 6402 convert_to_section_size_type(this->got_plt_->data_size()); 6403 unsigned char* const gotplt_view = of->get_output_view(gotplt_file_offset, 6404 gotplt_size); 6405 unsigned char* pov = oview; 6406 6407 Mips_address plt_address = this->address(); 6408 6409 // Calculate the address of .got.plt. 6410 Mips_address gotplt_addr = this->got_plt_->address(); 6411 Mips_address gotplt_addr_high = ((gotplt_addr + 0x8000) >> 16) & 0xffff; 6412 Mips_address gotplt_addr_low = gotplt_addr & 0xffff; 6413 6414 // The PLT sequence is not safe for N64 if .got.plt's address can 6415 // not be loaded in two instructions. 6416 gold_assert((gotplt_addr & ~(Mips_address) 0x7fffffff) == 0 6417 || ~(gotplt_addr | 0x7fffffff) == 0); 6418 6419 // Write the PLT header. 6420 const uint32_t* plt0_entry = this->get_plt_header_entry(); 6421 if (plt0_entry == plt0_entry_micromips_o32) 6422 { 6423 // Write microMIPS PLT header. 6424 gold_assert(gotplt_addr % 4 == 0); 6425 6426 Mips_address gotpc_offset = gotplt_addr - ((plt_address | 3) ^ 3); 6427 6428 // ADDIUPC has a span of +/-16MB, check we're in range. 6429 if (gotpc_offset + 0x1000000 >= 0x2000000) 6430 { 6431 gold_error(_(".got.plt offset of %ld from .plt beyond the range of " 6432 "ADDIUPC"), (long)gotpc_offset); 6433 return; 6434 } 6435 6436 elfcpp::Swap<16, big_endian>::writeval(pov, 6437 plt0_entry[0] | ((gotpc_offset >> 18) & 0x7f)); 6438 elfcpp::Swap<16, big_endian>::writeval(pov + 2, 6439 (gotpc_offset >> 2) & 0xffff); 6440 pov += 4; 6441 for (unsigned int i = 2; 6442 i < (sizeof(plt0_entry_micromips_o32) 6443 / sizeof(plt0_entry_micromips_o32[0])); 6444 i++) 6445 { 6446 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]); 6447 pov += 2; 6448 } 6449 } 6450 else if (plt0_entry == plt0_entry_micromips32_o32) 6451 { 6452 // Write microMIPS PLT header in insn32 mode. 6453 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[0]); 6454 elfcpp::Swap<16, big_endian>::writeval(pov + 2, gotplt_addr_high); 6455 elfcpp::Swap<16, big_endian>::writeval(pov + 4, plt0_entry[2]); 6456 elfcpp::Swap<16, big_endian>::writeval(pov + 6, gotplt_addr_low); 6457 elfcpp::Swap<16, big_endian>::writeval(pov + 8, plt0_entry[4]); 6458 elfcpp::Swap<16, big_endian>::writeval(pov + 10, gotplt_addr_low); 6459 pov += 12; 6460 for (unsigned int i = 6; 6461 i < (sizeof(plt0_entry_micromips32_o32) 6462 / sizeof(plt0_entry_micromips32_o32[0])); 6463 i++) 6464 { 6465 elfcpp::Swap<16, big_endian>::writeval(pov, plt0_entry[i]); 6466 pov += 2; 6467 } 6468 } 6469 else 6470 { 6471 // Write standard PLT header. 6472 elfcpp::Swap<32, big_endian>::writeval(pov, 6473 plt0_entry[0] | gotplt_addr_high); 6474 elfcpp::Swap<32, big_endian>::writeval(pov + 4, 6475 plt0_entry[1] | gotplt_addr_low); 6476 elfcpp::Swap<32, big_endian>::writeval(pov + 8, 6477 plt0_entry[2] | gotplt_addr_low); 6478 pov += 12; 6479 for (int i = 3; i < 8; i++) 6480 { 6481 elfcpp::Swap<32, big_endian>::writeval(pov, plt0_entry[i]); 6482 pov += 4; 6483 } 6484 } 6485 6486 6487 unsigned char* gotplt_pov = gotplt_view; 6488 unsigned int got_entry_size = size/8; // TODO(sasa): MIPS_ELF_GOT_SIZE 6489 6490 // The first two entries in .got.plt are reserved. 6491 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov, 0); 6492 elfcpp::Swap<size, big_endian>::writeval(gotplt_pov + got_entry_size, 0); 6493 6494 unsigned int gotplt_offset = 2 * got_entry_size; 6495 gotplt_pov += 2 * got_entry_size; 6496 6497 // Calculate the address of the PLT header. 6498 Mips_address header_address = (plt_address 6499 + (this->is_plt_header_compressed() ? 1 : 0)); 6500 6501 // Initialize compressed PLT area view. 6502 unsigned char* pov2 = pov + this->plt_mips_offset_; 6503 6504 // Write the PLT entries. 6505 for (typename std::vector<Mips_symbol<size>*>::const_iterator 6506 p = this->symbols_.begin(); 6507 p != this->symbols_.end(); 6508 ++p, gotplt_pov += got_entry_size, gotplt_offset += got_entry_size) 6509 { 6510 Mips_symbol<size>* mips_sym = *p; 6511 6512 // Calculate the address of the .got.plt entry. 6513 uint32_t gotplt_entry_addr = (gotplt_addr + gotplt_offset); 6514 uint32_t gotplt_entry_addr_hi = (((gotplt_entry_addr + 0x8000) >> 16) 6515 & 0xffff); 6516 uint32_t gotplt_entry_addr_lo = gotplt_entry_addr & 0xffff; 6517 6518 // Initially point the .got.plt entry at the PLT header. 6519 if (this->target_->is_output_n64()) 6520 elfcpp::Swap<64, big_endian>::writeval(gotplt_pov, header_address); 6521 else 6522 elfcpp::Swap<32, big_endian>::writeval(gotplt_pov, header_address); 6523 6524 // Now handle the PLT itself. First the standard entry. 6525 if (mips_sym->has_mips_plt_offset()) 6526 { 6527 // Pick the load opcode (LW or LD). 6528 uint64_t load = this->target_->is_output_n64() ? 0xdc000000 6529 : 0x8c000000; 6530 6531 // Fill in the PLT entry itself. 6532 elfcpp::Swap<32, big_endian>::writeval(pov, 6533 plt_entry[0] | gotplt_entry_addr_hi); 6534 elfcpp::Swap<32, big_endian>::writeval(pov + 4, 6535 plt_entry[1] | gotplt_entry_addr_lo | load); 6536 elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_entry[2]); 6537 elfcpp::Swap<32, big_endian>::writeval(pov + 12, 6538 plt_entry[3] | gotplt_entry_addr_lo); 6539 pov += 16; 6540 } 6541 6542 // Now the compressed entry. They come after any standard ones. 6543 if (mips_sym->has_comp_plt_offset()) 6544 { 6545 if (!this->target_->is_output_micromips()) 6546 { 6547 // Write MIPS16 PLT entry. 6548 const uint32_t* plt_entry = plt_entry_mips16_o32; 6549 6550 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]); 6551 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, plt_entry[1]); 6552 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]); 6553 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]); 6554 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]); 6555 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]); 6556 elfcpp::Swap<32, big_endian>::writeval(pov2 + 12, 6557 gotplt_entry_addr); 6558 pov2 += 16; 6559 } 6560 else if (this->target_->use_32bit_micromips_instructions()) 6561 { 6562 // Write microMIPS PLT entry in insn32 mode. 6563 const uint32_t* plt_entry = plt_entry_micromips32_o32; 6564 6565 elfcpp::Swap<16, big_endian>::writeval(pov2, plt_entry[0]); 6566 elfcpp::Swap<16, big_endian>::writeval(pov2 + 2, 6567 gotplt_entry_addr_hi); 6568 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]); 6569 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, 6570 gotplt_entry_addr_lo); 6571 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]); 6572 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]); 6573 elfcpp::Swap<16, big_endian>::writeval(pov2 + 12, plt_entry[6]); 6574 elfcpp::Swap<16, big_endian>::writeval(pov2 + 14, 6575 gotplt_entry_addr_lo); 6576 pov2 += 16; 6577 } 6578 else 6579 { 6580 // Write microMIPS PLT entry. 6581 const uint32_t* plt_entry = plt_entry_micromips_o32; 6582 6583 gold_assert(gotplt_entry_addr % 4 == 0); 6584 6585 Mips_address loc_address = plt_address + pov2 - oview; 6586 int gotpc_offset = gotplt_entry_addr - ((loc_address | 3) ^ 3); 6587 6588 // ADDIUPC has a span of +/-16MB, check we're in range. 6589 if (gotpc_offset + 0x1000000 >= 0x2000000) 6590 { 6591 gold_error(_(".got.plt offset of %ld from .plt beyond the " 6592 "range of ADDIUPC"), (long)gotpc_offset); 6593 return; 6594 } 6595 6596 elfcpp::Swap<16, big_endian>::writeval(pov2, 6597 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f)); 6598 elfcpp::Swap<16, big_endian>::writeval( 6599 pov2 + 2, (gotpc_offset >> 2) & 0xffff); 6600 elfcpp::Swap<16, big_endian>::writeval(pov2 + 4, plt_entry[2]); 6601 elfcpp::Swap<16, big_endian>::writeval(pov2 + 6, plt_entry[3]); 6602 elfcpp::Swap<16, big_endian>::writeval(pov2 + 8, plt_entry[4]); 6603 elfcpp::Swap<16, big_endian>::writeval(pov2 + 10, plt_entry[5]); 6604 pov2 += 12; 6605 } 6606 } 6607 } 6608 6609 // Check the number of bytes written for standard entries. 6610 gold_assert(static_cast<section_size_type>( 6611 pov - oview - this->plt_header_size_) == this->plt_mips_offset_); 6612 // Check the number of bytes written for compressed entries. 6613 gold_assert((static_cast<section_size_type>(pov2 - pov) 6614 == this->plt_comp_offset_)); 6615 // Check the total number of bytes written. 6616 gold_assert(static_cast<section_size_type>(pov2 - oview) == oview_size); 6617 6618 gold_assert(static_cast<section_size_type>(gotplt_pov - gotplt_view) 6619 == gotplt_size); 6620 6621 of->write_output_view(offset, oview_size, oview); 6622 of->write_output_view(gotplt_file_offset, gotplt_size, gotplt_view); 6623 } 6624 6625 // Mips_output_data_mips_stubs methods. 6626 6627 // The format of the lazy binding stub when dynamic symbol count is less than 6628 // 64K, dynamic symbol index is less than 32K, and ABI is not N64. 6629 template<int size, bool big_endian> 6630 const uint32_t 6631 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] = 6632 { 6633 0x8f998010, // lw t9,0x8010(gp) 6634 0x03e07821, // addu t7,ra,zero 6635 0x0320f809, // jalr t9,ra 6636 0x24180000 // addiu t8,zero,DYN_INDEX sign extended 6637 }; 6638 6639 // The format of the lazy binding stub when dynamic symbol count is less than 6640 // 64K, dynamic symbol index is less than 32K, and ABI is N64. 6641 template<int size, bool big_endian> 6642 const uint32_t 6643 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] = 6644 { 6645 0xdf998010, // ld t9,0x8010(gp) 6646 0x03e0782d, // daddu t7,ra,zero 6647 0x0320f809, // jalr t9,ra 6648 0x64180000 // daddiu t8,zero,DYN_INDEX sign extended 6649 }; 6650 6651 // The format of the lazy binding stub when dynamic symbol count is less than 6652 // 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64. 6653 template<int size, bool big_endian> 6654 const uint32_t 6655 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] = 6656 { 6657 0x8f998010, // lw t9,0x8010(gp) 6658 0x03e07821, // addu t7,ra,zero 6659 0x0320f809, // jalr t9,ra 6660 0x34180000 // ori t8,zero,DYN_INDEX unsigned 6661 }; 6662 6663 // The format of the lazy binding stub when dynamic symbol count is less than 6664 // 64K, dynamic symbol index is between 32K and 64K, and ABI is N64. 6665 template<int size, bool big_endian> 6666 const uint32_t 6667 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] = 6668 { 6669 0xdf998010, // ld t9,0x8010(gp) 6670 0x03e0782d, // daddu t7,ra,zero 6671 0x0320f809, // jalr t9,ra 6672 0x34180000 // ori t8,zero,DYN_INDEX unsigned 6673 }; 6674 6675 // The format of the lazy binding stub when dynamic symbol count is greater than 6676 // 64K, and ABI is not N64. 6677 template<int size, bool big_endian> 6678 const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] = 6679 { 6680 0x8f998010, // lw t9,0x8010(gp) 6681 0x03e07821, // addu t7,ra,zero 6682 0x3c180000, // lui t8,DYN_INDEX 6683 0x0320f809, // jalr t9,ra 6684 0x37180000 // ori t8,t8,DYN_INDEX 6685 }; 6686 6687 // The format of the lazy binding stub when dynamic symbol count is greater than 6688 // 64K, and ABI is N64. 6689 template<int size, bool big_endian> 6690 const uint32_t 6691 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] = 6692 { 6693 0xdf998010, // ld t9,0x8010(gp) 6694 0x03e0782d, // daddu t7,ra,zero 6695 0x3c180000, // lui t8,DYN_INDEX 6696 0x0320f809, // jalr t9,ra 6697 0x37180000 // ori t8,t8,DYN_INDEX 6698 }; 6699 6700 // microMIPS stubs. 6701 6702 // The format of the microMIPS lazy binding stub when dynamic symbol count is 6703 // less than 64K, dynamic symbol index is less than 32K, and ABI is not N64. 6704 template<int size, bool big_endian> 6705 const uint32_t 6706 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_1[] = 6707 { 6708 0xff3c, 0x8010, // lw t9,0x8010(gp) 6709 0x0dff, // move t7,ra 6710 0x45d9, // jalr t9 6711 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended 6712 }; 6713 6714 // The format of the microMIPS lazy binding stub when dynamic symbol count is 6715 // less than 64K, dynamic symbol index is less than 32K, and ABI is N64. 6716 template<int size, bool big_endian> 6717 const uint32_t 6718 Mips_output_data_mips_stubs<size, big_endian>:: 6719 lazy_stub_micromips_normal_1_n64[] = 6720 { 6721 0xdf3c, 0x8010, // ld t9,0x8010(gp) 6722 0x0dff, // move t7,ra 6723 0x45d9, // jalr t9 6724 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended 6725 }; 6726 6727 // The format of the microMIPS lazy binding stub when dynamic symbol 6728 // count is less than 64K, dynamic symbol index is between 32K and 64K, 6729 // and ABI is not N64. 6730 template<int size, bool big_endian> 6731 const uint32_t 6732 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_normal_2[] = 6733 { 6734 0xff3c, 0x8010, // lw t9,0x8010(gp) 6735 0x0dff, // move t7,ra 6736 0x45d9, // jalr t9 6737 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned 6738 }; 6739 6740 // The format of the microMIPS lazy binding stub when dynamic symbol 6741 // count is less than 64K, dynamic symbol index is between 32K and 64K, 6742 // and ABI is N64. 6743 template<int size, bool big_endian> 6744 const uint32_t 6745 Mips_output_data_mips_stubs<size, big_endian>:: 6746 lazy_stub_micromips_normal_2_n64[] = 6747 { 6748 0xdf3c, 0x8010, // ld t9,0x8010(gp) 6749 0x0dff, // move t7,ra 6750 0x45d9, // jalr t9 6751 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned 6752 }; 6753 6754 // The format of the microMIPS lazy binding stub when dynamic symbol count is 6755 // greater than 64K, and ABI is not N64. 6756 template<int size, bool big_endian> 6757 const uint32_t 6758 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big[] = 6759 { 6760 0xff3c, 0x8010, // lw t9,0x8010(gp) 6761 0x0dff, // move t7,ra 6762 0x41b8, 0x0000, // lui t8,DYN_INDEX 6763 0x45d9, // jalr t9 6764 0x5318, 0x0000 // ori t8,t8,DYN_INDEX 6765 }; 6766 6767 // The format of the microMIPS lazy binding stub when dynamic symbol count is 6768 // greater than 64K, and ABI is N64. 6769 template<int size, bool big_endian> 6770 const uint32_t 6771 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips_big_n64[] = 6772 { 6773 0xdf3c, 0x8010, // ld t9,0x8010(gp) 6774 0x0dff, // move t7,ra 6775 0x41b8, 0x0000, // lui t8,DYN_INDEX 6776 0x45d9, // jalr t9 6777 0x5318, 0x0000 // ori t8,t8,DYN_INDEX 6778 }; 6779 6780 // 32-bit microMIPS stubs. 6781 6782 // The format of the microMIPS lazy binding stub when dynamic symbol count is 6783 // less than 64K, dynamic symbol index is less than 32K, ABI is not N64, and we 6784 // can use only 32-bit instructions. 6785 template<int size, bool big_endian> 6786 const uint32_t 6787 Mips_output_data_mips_stubs<size, big_endian>:: 6788 lazy_stub_micromips32_normal_1[] = 6789 { 6790 0xff3c, 0x8010, // lw t9,0x8010(gp) 6791 0x001f, 0x7950, // addu t7,ra,zero 6792 0x03f9, 0x0f3c, // jalr ra,t9 6793 0x3300, 0x0000 // addiu t8,zero,DYN_INDEX sign extended 6794 }; 6795 6796 // The format of the microMIPS lazy binding stub when dynamic symbol count is 6797 // less than 64K, dynamic symbol index is less than 32K, ABI is N64, and we can 6798 // use only 32-bit instructions. 6799 template<int size, bool big_endian> 6800 const uint32_t 6801 Mips_output_data_mips_stubs<size, big_endian>:: 6802 lazy_stub_micromips32_normal_1_n64[] = 6803 { 6804 0xdf3c, 0x8010, // ld t9,0x8010(gp) 6805 0x581f, 0x7950, // daddu t7,ra,zero 6806 0x03f9, 0x0f3c, // jalr ra,t9 6807 0x5f00, 0x0000 // daddiu t8,zero,DYN_INDEX sign extended 6808 }; 6809 6810 // The format of the microMIPS lazy binding stub when dynamic symbol 6811 // count is less than 64K, dynamic symbol index is between 32K and 64K, 6812 // ABI is not N64, and we can use only 32-bit instructions. 6813 template<int size, bool big_endian> 6814 const uint32_t 6815 Mips_output_data_mips_stubs<size, big_endian>:: 6816 lazy_stub_micromips32_normal_2[] = 6817 { 6818 0xff3c, 0x8010, // lw t9,0x8010(gp) 6819 0x001f, 0x7950, // addu t7,ra,zero 6820 0x03f9, 0x0f3c, // jalr ra,t9 6821 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned 6822 }; 6823 6824 // The format of the microMIPS lazy binding stub when dynamic symbol 6825 // count is less than 64K, dynamic symbol index is between 32K and 64K, 6826 // ABI is N64, and we can use only 32-bit instructions. 6827 template<int size, bool big_endian> 6828 const uint32_t 6829 Mips_output_data_mips_stubs<size, big_endian>:: 6830 lazy_stub_micromips32_normal_2_n64[] = 6831 { 6832 0xdf3c, 0x8010, // ld t9,0x8010(gp) 6833 0x581f, 0x7950, // daddu t7,ra,zero 6834 0x03f9, 0x0f3c, // jalr ra,t9 6835 0x5300, 0x0000 // ori t8,zero,DYN_INDEX unsigned 6836 }; 6837 6838 // The format of the microMIPS lazy binding stub when dynamic symbol count is 6839 // greater than 64K, ABI is not N64, and we can use only 32-bit instructions. 6840 template<int size, bool big_endian> 6841 const uint32_t 6842 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big[] = 6843 { 6844 0xff3c, 0x8010, // lw t9,0x8010(gp) 6845 0x001f, 0x7950, // addu t7,ra,zero 6846 0x41b8, 0x0000, // lui t8,DYN_INDEX 6847 0x03f9, 0x0f3c, // jalr ra,t9 6848 0x5318, 0x0000 // ori t8,t8,DYN_INDEX 6849 }; 6850 6851 // The format of the microMIPS lazy binding stub when dynamic symbol count is 6852 // greater than 64K, ABI is N64, and we can use only 32-bit instructions. 6853 template<int size, bool big_endian> 6854 const uint32_t 6855 Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_micromips32_big_n64[] = 6856 { 6857 0xdf3c, 0x8010, // ld t9,0x8010(gp) 6858 0x581f, 0x7950, // daddu t7,ra,zero 6859 0x41b8, 0x0000, // lui t8,DYN_INDEX 6860 0x03f9, 0x0f3c, // jalr ra,t9 6861 0x5318, 0x0000 // ori t8,t8,DYN_INDEX 6862 }; 6863 6864 // Create entry for a symbol. 6865 6866 template<int size, bool big_endian> 6867 void 6868 Mips_output_data_mips_stubs<size, big_endian>::make_entry( 6869 Mips_symbol<size>* gsym) 6870 { 6871 if (!gsym->has_lazy_stub() && !gsym->has_plt_offset()) 6872 { 6873 this->symbols_.insert(gsym); 6874 gsym->set_has_lazy_stub(true); 6875 } 6876 } 6877 6878 // Remove entry for a symbol. 6879 6880 template<int size, bool big_endian> 6881 void 6882 Mips_output_data_mips_stubs<size, big_endian>::remove_entry( 6883 Mips_symbol<size>* gsym) 6884 { 6885 if (gsym->has_lazy_stub()) 6886 { 6887 this->symbols_.erase(gsym); 6888 gsym->set_has_lazy_stub(false); 6889 } 6890 } 6891 6892 // Set stub offsets for symbols. This method expects that the number of 6893 // entries in dynamic symbol table is set. 6894 6895 template<int size, bool big_endian> 6896 void 6897 Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets() 6898 { 6899 gold_assert(this->dynsym_count_ != -1U); 6900 6901 if (this->stub_offsets_are_set_) 6902 return; 6903 6904 unsigned int stub_size = this->stub_size(); 6905 unsigned int offset = 0; 6906 for (typename Unordered_set<Mips_symbol<size>*>::const_iterator 6907 p = this->symbols_.begin(); 6908 p != this->symbols_.end(); 6909 ++p, offset += stub_size) 6910 { 6911 Mips_symbol<size>* mips_sym = *p; 6912 mips_sym->set_lazy_stub_offset(offset); 6913 } 6914 this->stub_offsets_are_set_ = true; 6915 } 6916 6917 template<int size, bool big_endian> 6918 void 6919 Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value() 6920 { 6921 for (typename Unordered_set<Mips_symbol<size>*>::const_iterator 6922 p = this->symbols_.begin(); p != this->symbols_.end(); ++p) 6923 { 6924 Mips_symbol<size>* sym = *p; 6925 if (sym->is_from_dynobj()) 6926 sym->set_needs_dynsym_value(); 6927 } 6928 } 6929 6930 // Write out the .MIPS.stubs. This uses the hand-coded instructions and 6931 // adjusts them as needed. 6932 6933 template<int size, bool big_endian> 6934 void 6935 Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of) 6936 { 6937 const off_t offset = this->offset(); 6938 const section_size_type oview_size = 6939 convert_to_section_size_type(this->data_size()); 6940 unsigned char* const oview = of->get_output_view(offset, oview_size); 6941 6942 bool big_stub = this->dynsym_count_ > 0x10000; 6943 6944 unsigned char* pov = oview; 6945 for (typename Unordered_set<Mips_symbol<size>*>::const_iterator 6946 p = this->symbols_.begin(); p != this->symbols_.end(); ++p) 6947 { 6948 Mips_symbol<size>* sym = *p; 6949 const uint32_t* lazy_stub; 6950 bool n64 = this->target_->is_output_n64(); 6951 6952 if (!this->target_->is_output_micromips()) 6953 { 6954 // Write standard (non-microMIPS) stub. 6955 if (!big_stub) 6956 { 6957 if (sym->dynsym_index() & ~0x7fff) 6958 // Dynsym index is between 32K and 64K. 6959 lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2; 6960 else 6961 // Dynsym index is less than 32K. 6962 lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1; 6963 } 6964 else 6965 lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big; 6966 6967 unsigned int i = 0; 6968 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]); 6969 elfcpp::Swap<32, big_endian>::writeval(pov + 4, lazy_stub[i + 1]); 6970 pov += 8; 6971 6972 i += 2; 6973 if (big_stub) 6974 { 6975 // LUI instruction of the big stub. Paste high 16 bits of the 6976 // dynsym index. 6977 elfcpp::Swap<32, big_endian>::writeval(pov, 6978 lazy_stub[i] | ((sym->dynsym_index() >> 16) & 0x7fff)); 6979 pov += 4; 6980 i += 1; 6981 } 6982 elfcpp::Swap<32, big_endian>::writeval(pov, lazy_stub[i]); 6983 // Last stub instruction. Paste low 16 bits of the dynsym index. 6984 elfcpp::Swap<32, big_endian>::writeval(pov + 4, 6985 lazy_stub[i + 1] | (sym->dynsym_index() & 0xffff)); 6986 pov += 8; 6987 } 6988 else if (this->target_->use_32bit_micromips_instructions()) 6989 { 6990 // Write microMIPS stub in insn32 mode. 6991 if (!big_stub) 6992 { 6993 if (sym->dynsym_index() & ~0x7fff) 6994 // Dynsym index is between 32K and 64K. 6995 lazy_stub = n64 ? lazy_stub_micromips32_normal_2_n64 6996 : lazy_stub_micromips32_normal_2; 6997 else 6998 // Dynsym index is less than 32K. 6999 lazy_stub = n64 ? lazy_stub_micromips32_normal_1_n64 7000 : lazy_stub_micromips32_normal_1; 7001 } 7002 else 7003 lazy_stub = n64 ? lazy_stub_micromips32_big_n64 7004 : lazy_stub_micromips32_big; 7005 7006 unsigned int i = 0; 7007 // First stub instruction. We emit 32-bit microMIPS instructions by 7008 // emitting two 16-bit parts because on microMIPS the 16-bit part of 7009 // the instruction where the opcode is must always come first, for 7010 // both little and big endian. 7011 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]); 7012 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]); 7013 // Second stub instruction. 7014 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]); 7015 elfcpp::Swap<16, big_endian>::writeval(pov + 6, lazy_stub[i + 3]); 7016 pov += 8; 7017 i += 4; 7018 if (big_stub) 7019 { 7020 // LUI instruction of the big stub. Paste high 16 bits of the 7021 // dynsym index. 7022 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]); 7023 elfcpp::Swap<16, big_endian>::writeval(pov + 2, 7024 (sym->dynsym_index() >> 16) & 0x7fff); 7025 pov += 4; 7026 i += 2; 7027 } 7028 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]); 7029 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]); 7030 // Last stub instruction. Paste low 16 bits of the dynsym index. 7031 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]); 7032 elfcpp::Swap<16, big_endian>::writeval(pov + 6, 7033 sym->dynsym_index() & 0xffff); 7034 pov += 8; 7035 } 7036 else 7037 { 7038 // Write microMIPS stub. 7039 if (!big_stub) 7040 { 7041 if (sym->dynsym_index() & ~0x7fff) 7042 // Dynsym index is between 32K and 64K. 7043 lazy_stub = n64 ? lazy_stub_micromips_normal_2_n64 7044 : lazy_stub_micromips_normal_2; 7045 else 7046 // Dynsym index is less than 32K. 7047 lazy_stub = n64 ? lazy_stub_micromips_normal_1_n64 7048 : lazy_stub_micromips_normal_1; 7049 } 7050 else 7051 lazy_stub = n64 ? lazy_stub_micromips_big_n64 7052 : lazy_stub_micromips_big; 7053 7054 unsigned int i = 0; 7055 // First stub instruction. We emit 32-bit microMIPS instructions by 7056 // emitting two 16-bit parts because on microMIPS the 16-bit part of 7057 // the instruction where the opcode is must always come first, for 7058 // both little and big endian. 7059 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]); 7060 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]); 7061 // Second stub instruction. 7062 elfcpp::Swap<16, big_endian>::writeval(pov + 4, lazy_stub[i + 2]); 7063 pov += 6; 7064 i += 3; 7065 if (big_stub) 7066 { 7067 // LUI instruction of the big stub. Paste high 16 bits of the 7068 // dynsym index. 7069 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]); 7070 elfcpp::Swap<16, big_endian>::writeval(pov + 2, 7071 (sym->dynsym_index() >> 16) & 0x7fff); 7072 pov += 4; 7073 i += 2; 7074 } 7075 elfcpp::Swap<16, big_endian>::writeval(pov, lazy_stub[i]); 7076 // Last stub instruction. Paste low 16 bits of the dynsym index. 7077 elfcpp::Swap<16, big_endian>::writeval(pov + 2, lazy_stub[i + 1]); 7078 elfcpp::Swap<16, big_endian>::writeval(pov + 4, 7079 sym->dynsym_index() & 0xffff); 7080 pov += 6; 7081 } 7082 } 7083 7084 // We always allocate 20 bytes for every stub, because final dynsym count is 7085 // not known in method do_finalize_sections. There are 4 unused bytes per 7086 // stub if final dynsym count is less than 0x10000. 7087 unsigned int used = pov - oview; 7088 unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4; 7089 gold_assert(static_cast<section_size_type>(used + unused) == oview_size); 7090 7091 // Fill the unused space with zeroes. 7092 // TODO(sasa): Can we strip unused bytes during the relaxation? 7093 if (unused > 0) 7094 memset(pov, 0, unused); 7095 7096 of->write_output_view(offset, oview_size, oview); 7097 } 7098 7099 // Mips_output_section_reginfo methods. 7100 7101 template<int size, bool big_endian> 7102 void 7103 Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of) 7104 { 7105 off_t offset = this->offset(); 7106 off_t data_size = this->data_size(); 7107 7108 unsigned char* view = of->get_output_view(offset, data_size); 7109 elfcpp::Swap<size, big_endian>::writeval(view, this->gprmask_); 7110 elfcpp::Swap<size, big_endian>::writeval(view + 4, this->cprmask1_); 7111 elfcpp::Swap<size, big_endian>::writeval(view + 8, this->cprmask2_); 7112 elfcpp::Swap<size, big_endian>::writeval(view + 12, this->cprmask3_); 7113 elfcpp::Swap<size, big_endian>::writeval(view + 16, this->cprmask4_); 7114 // Write the gp value. 7115 elfcpp::Swap<size, big_endian>::writeval(view + 20, 7116 this->target_->gp_value()); 7117 7118 of->write_output_view(offset, data_size, view); 7119 } 7120 7121 // Mips_copy_relocs methods. 7122 7123 // Emit any saved relocs. 7124 7125 template<int sh_type, int size, bool big_endian> 7126 void 7127 Mips_copy_relocs<sh_type, size, big_endian>::emit_mips( 7128 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section, 7129 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target) 7130 { 7131 for (typename Copy_relocs<sh_type, size, big_endian>:: 7132 Copy_reloc_entries::iterator p = this->entries_.begin(); 7133 p != this->entries_.end(); 7134 ++p) 7135 emit_entry(*p, reloc_section, symtab, layout, target); 7136 7137 // We no longer need the saved information. 7138 this->entries_.clear(); 7139 } 7140 7141 // Emit the reloc if appropriate. 7142 7143 template<int sh_type, int size, bool big_endian> 7144 void 7145 Mips_copy_relocs<sh_type, size, big_endian>::emit_entry( 7146 Copy_reloc_entry& entry, 7147 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section, 7148 Symbol_table* symtab, Layout* layout, Target_mips<size, big_endian>* target) 7149 { 7150 // If the symbol is no longer defined in a dynamic object, then we 7151 // emitted a COPY relocation, and we do not want to emit this 7152 // dynamic relocation. 7153 if (!entry.sym_->is_from_dynobj()) 7154 return; 7155 7156 bool can_make_dynamic = (entry.reloc_type_ == elfcpp::R_MIPS_32 7157 || entry.reloc_type_ == elfcpp::R_MIPS_REL32 7158 || entry.reloc_type_ == elfcpp::R_MIPS_64); 7159 7160 Mips_symbol<size>* sym = Mips_symbol<size>::as_mips_sym(entry.sym_); 7161 if (can_make_dynamic && !sym->has_static_relocs()) 7162 { 7163 Mips_relobj<size, big_endian>* object = 7164 Mips_relobj<size, big_endian>::as_mips_relobj(entry.relobj_); 7165 target->got_section(symtab, layout)->record_global_got_symbol( 7166 sym, object, entry.reloc_type_, true, false); 7167 if (!symbol_references_local(sym, sym->should_add_dynsym_entry(symtab))) 7168 target->rel_dyn_section(layout)->add_global(sym, elfcpp::R_MIPS_REL32, 7169 entry.output_section_, entry.relobj_, entry.shndx_, entry.address_); 7170 else 7171 target->rel_dyn_section(layout)->add_symbolless_global_addend( 7172 sym, elfcpp::R_MIPS_REL32, entry.output_section_, entry.relobj_, 7173 entry.shndx_, entry.address_); 7174 } 7175 else 7176 this->make_copy_reloc(symtab, layout, 7177 static_cast<Sized_symbol<size>*>(entry.sym_), 7178 reloc_section); 7179 } 7180 7181 // Target_mips methods. 7182 7183 // Return the value to use for a dynamic symbol which requires special 7184 // treatment. This is how we support equality comparisons of function 7185 // pointers across shared library boundaries, as described in the 7186 // processor specific ABI supplement. 7187 7188 template<int size, bool big_endian> 7189 uint64_t 7190 Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const 7191 { 7192 uint64_t value = 0; 7193 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym); 7194 7195 if (!mips_sym->has_lazy_stub()) 7196 { 7197 if (mips_sym->has_plt_offset()) 7198 { 7199 // We distinguish between PLT entries and lazy-binding stubs by 7200 // giving the former an st_other value of STO_MIPS_PLT. Set the 7201 // value to the stub address if there are any relocations in the 7202 // binary where pointer equality matters. 7203 if (mips_sym->pointer_equality_needed()) 7204 { 7205 // Prefer a standard MIPS PLT entry. 7206 if (mips_sym->has_mips_plt_offset()) 7207 value = this->plt_section()->mips_entry_address(mips_sym); 7208 else 7209 value = this->plt_section()->comp_entry_address(mips_sym) + 1; 7210 } 7211 else 7212 value = 0; 7213 } 7214 } 7215 else 7216 { 7217 // First, set stub offsets for symbols. This method expects that the 7218 // number of entries in dynamic symbol table is set. 7219 this->mips_stubs_section()->set_lazy_stub_offsets(); 7220 7221 // The run-time linker uses the st_value field of the symbol 7222 // to reset the global offset table entry for this external 7223 // to its stub address when unlinking a shared object. 7224 value = this->mips_stubs_section()->stub_address(mips_sym); 7225 } 7226 7227 if (mips_sym->has_mips16_fn_stub()) 7228 { 7229 // If we have a MIPS16 function with a stub, the dynamic symbol must 7230 // refer to the stub, since only the stub uses the standard calling 7231 // conventions. 7232 value = mips_sym->template 7233 get_mips16_fn_stub<big_endian>()->output_address(); 7234 } 7235 7236 return value; 7237 } 7238 7239 // Get the dynamic reloc section, creating it if necessary. It's always 7240 // .rel.dyn, even for MIPS64. 7241 7242 template<int size, bool big_endian> 7243 typename Target_mips<size, big_endian>::Reloc_section* 7244 Target_mips<size, big_endian>::rel_dyn_section(Layout* layout) 7245 { 7246 if (this->rel_dyn_ == NULL) 7247 { 7248 gold_assert(layout != NULL); 7249 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc()); 7250 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL, 7251 elfcpp::SHF_ALLOC, this->rel_dyn_, 7252 ORDER_DYNAMIC_RELOCS, false); 7253 7254 // First entry in .rel.dyn has to be null. 7255 // This is hack - we define dummy output data and set its address to 0, 7256 // and define absolute R_MIPS_NONE relocation with offset 0 against it. 7257 // This ensures that the entry is null. 7258 Output_data* od = new Output_data_zero_fill(0, 0); 7259 od->set_address(0); 7260 this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0); 7261 } 7262 return this->rel_dyn_; 7263 } 7264 7265 // Get the GOT section, creating it if necessary. 7266 7267 template<int size, bool big_endian> 7268 Mips_output_data_got<size, big_endian>* 7269 Target_mips<size, big_endian>::got_section(Symbol_table* symtab, 7270 Layout* layout) 7271 { 7272 if (this->got_ == NULL) 7273 { 7274 gold_assert(symtab != NULL && layout != NULL); 7275 7276 this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab, 7277 layout); 7278 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, 7279 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE | 7280 elfcpp::SHF_MIPS_GPREL), 7281 this->got_, ORDER_DATA, false); 7282 7283 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section. 7284 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, 7285 Symbol_table::PREDEFINED, 7286 this->got_, 7287 0, 0, elfcpp::STT_OBJECT, 7288 elfcpp::STB_GLOBAL, 7289 elfcpp::STV_DEFAULT, 0, 7290 false, false); 7291 } 7292 7293 return this->got_; 7294 } 7295 7296 // Calculate value of _gp symbol. 7297 7298 template<int size, bool big_endian> 7299 void 7300 Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab) 7301 { 7302 if (this->gp_ != NULL) 7303 return; 7304 7305 Output_data* section = layout->find_output_section(".got"); 7306 if (section == NULL) 7307 { 7308 // If there is no .got section, gp should be based on .sdata. 7309 // TODO(sasa): This is probably not needed. This was needed for older 7310 // MIPS architectures which accessed both GOT and .sdata section using 7311 // gp-relative addressing. Modern Mips Linux ELF architectures don't 7312 // access .sdata using gp-relative addressing. 7313 for (Layout::Section_list::const_iterator 7314 p = layout->section_list().begin(); 7315 p != layout->section_list().end(); 7316 ++p) 7317 { 7318 if (strcmp((*p)->name(), ".sdata") == 0) 7319 { 7320 section = *p; 7321 break; 7322 } 7323 } 7324 } 7325 7326 Sized_symbol<size>* gp = 7327 static_cast<Sized_symbol<size>*>(symtab->lookup("_gp")); 7328 if (gp != NULL) 7329 { 7330 if (gp->source() != Symbol::IS_CONSTANT && section != NULL) 7331 gp->init_output_data(gp->name(), NULL, section, MIPS_GP_OFFSET, 0, 7332 elfcpp::STT_OBJECT, 7333 elfcpp::STB_GLOBAL, 7334 elfcpp::STV_DEFAULT, 0, 7335 false, false); 7336 this->gp_ = gp; 7337 } 7338 else if (section != NULL) 7339 { 7340 gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data( 7341 "_gp", NULL, Symbol_table::PREDEFINED, 7342 section, MIPS_GP_OFFSET, 0, 7343 elfcpp::STT_OBJECT, 7344 elfcpp::STB_GLOBAL, 7345 elfcpp::STV_DEFAULT, 7346 0, false, false)); 7347 this->gp_ = gp; 7348 } 7349 } 7350 7351 // Set the dynamic symbol indexes. INDEX is the index of the first 7352 // global dynamic symbol. Pointers to the symbols are stored into the 7353 // vector SYMS. The names are added to DYNPOOL. This returns an 7354 // updated dynamic symbol index. 7355 7356 template<int size, bool big_endian> 7357 unsigned int 7358 Target_mips<size, big_endian>::do_set_dynsym_indexes( 7359 std::vector<Symbol*>* dyn_symbols, unsigned int index, 7360 std::vector<Symbol*>* syms, Stringpool* dynpool, 7361 Versions* versions, Symbol_table* symtab) const 7362 { 7363 std::vector<Symbol*> non_got_symbols; 7364 std::vector<Symbol*> got_symbols; 7365 7366 reorder_dyn_symbols<size, big_endian>(dyn_symbols, &non_got_symbols, 7367 &got_symbols); 7368 7369 for (std::vector<Symbol*>::iterator p = non_got_symbols.begin(); 7370 p != non_got_symbols.end(); 7371 ++p) 7372 { 7373 Symbol* sym = *p; 7374 7375 // Note that SYM may already have a dynamic symbol index, since 7376 // some symbols appear more than once in the symbol table, with 7377 // and without a version. 7378 7379 if (!sym->has_dynsym_index()) 7380 { 7381 sym->set_dynsym_index(index); 7382 ++index; 7383 syms->push_back(sym); 7384 dynpool->add(sym->name(), false, NULL); 7385 7386 // Record any version information. 7387 if (sym->version() != NULL) 7388 versions->record_version(symtab, dynpool, sym); 7389 7390 // If the symbol is defined in a dynamic object and is 7391 // referenced in a regular object, then mark the dynamic 7392 // object as needed. This is used to implement --as-needed. 7393 if (sym->is_from_dynobj() && sym->in_reg()) 7394 sym->object()->set_is_needed(); 7395 } 7396 } 7397 7398 for (std::vector<Symbol*>::iterator p = got_symbols.begin(); 7399 p != got_symbols.end(); 7400 ++p) 7401 { 7402 Symbol* sym = *p; 7403 if (!sym->has_dynsym_index()) 7404 { 7405 // Record any version information. 7406 if (sym->version() != NULL) 7407 versions->record_version(symtab, dynpool, sym); 7408 } 7409 } 7410 7411 index = versions->finalize(symtab, index, syms); 7412 7413 int got_sym_count = 0; 7414 for (std::vector<Symbol*>::iterator p = got_symbols.begin(); 7415 p != got_symbols.end(); 7416 ++p) 7417 { 7418 Symbol* sym = *p; 7419 7420 if (!sym->has_dynsym_index()) 7421 { 7422 ++got_sym_count; 7423 sym->set_dynsym_index(index); 7424 ++index; 7425 syms->push_back(sym); 7426 dynpool->add(sym->name(), false, NULL); 7427 7428 // If the symbol is defined in a dynamic object and is 7429 // referenced in a regular object, then mark the dynamic 7430 // object as needed. This is used to implement --as-needed. 7431 if (sym->is_from_dynobj() && sym->in_reg()) 7432 sym->object()->set_is_needed(); 7433 } 7434 } 7435 7436 // Set index of the first symbol that has .got entry. 7437 this->got_->set_first_global_got_dynsym_index( 7438 got_sym_count > 0 ? index - got_sym_count : -1U); 7439 7440 if (this->mips_stubs_ != NULL) 7441 this->mips_stubs_->set_dynsym_count(index); 7442 7443 return index; 7444 } 7445 7446 // Create a PLT entry for a global symbol referenced by r_type relocation. 7447 7448 template<int size, bool big_endian> 7449 void 7450 Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab, 7451 Layout* layout, 7452 Mips_symbol<size>* gsym, 7453 unsigned int r_type) 7454 { 7455 if (gsym->has_lazy_stub() || gsym->has_plt_offset()) 7456 return; 7457 7458 if (this->plt_ == NULL) 7459 { 7460 // Create the GOT section first. 7461 this->got_section(symtab, layout); 7462 7463 this->got_plt_ = new Output_data_space(4, "** GOT PLT"); 7464 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, 7465 (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE), 7466 this->got_plt_, ORDER_DATA, false); 7467 7468 // The first two entries are reserved. 7469 this->got_plt_->set_current_data_size(2 * size/8); 7470 7471 this->plt_ = new Mips_output_data_plt<size, big_endian>(layout, 7472 this->got_plt_, 7473 this); 7474 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, 7475 (elfcpp::SHF_ALLOC 7476 | elfcpp::SHF_EXECINSTR), 7477 this->plt_, ORDER_PLT, false); 7478 } 7479 7480 this->plt_->add_entry(gsym, r_type); 7481 } 7482 7483 7484 // Get the .MIPS.stubs section, creating it if necessary. 7485 7486 template<int size, bool big_endian> 7487 Mips_output_data_mips_stubs<size, big_endian>* 7488 Target_mips<size, big_endian>::mips_stubs_section(Layout* layout) 7489 { 7490 if (this->mips_stubs_ == NULL) 7491 { 7492 this->mips_stubs_ = 7493 new Mips_output_data_mips_stubs<size, big_endian>(this); 7494 layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS, 7495 (elfcpp::SHF_ALLOC 7496 | elfcpp::SHF_EXECINSTR), 7497 this->mips_stubs_, ORDER_PLT, false); 7498 } 7499 return this->mips_stubs_; 7500 } 7501 7502 // Get the LA25 stub section, creating it if necessary. 7503 7504 template<int size, bool big_endian> 7505 Mips_output_data_la25_stub<size, big_endian>* 7506 Target_mips<size, big_endian>::la25_stub_section(Layout* layout) 7507 { 7508 if (this->la25_stub_ == NULL) 7509 { 7510 this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>(); 7511 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS, 7512 (elfcpp::SHF_ALLOC 7513 | elfcpp::SHF_EXECINSTR), 7514 this->la25_stub_, ORDER_TEXT, false); 7515 } 7516 return this->la25_stub_; 7517 } 7518 7519 // Process the relocations to determine unreferenced sections for 7520 // garbage collection. 7521 7522 template<int size, bool big_endian> 7523 void 7524 Target_mips<size, big_endian>::gc_process_relocs( 7525 Symbol_table* symtab, 7526 Layout* layout, 7527 Sized_relobj_file<size, big_endian>* object, 7528 unsigned int data_shndx, 7529 unsigned int, 7530 const unsigned char* prelocs, 7531 size_t reloc_count, 7532 Output_section* output_section, 7533 bool needs_special_offset_handling, 7534 size_t local_symbol_count, 7535 const unsigned char* plocal_symbols) 7536 { 7537 typedef Target_mips<size, big_endian> Mips; 7538 typedef typename Target_mips<size, big_endian>::Scan Scan; 7539 7540 gold::gc_process_relocs<size, big_endian, Mips, elfcpp::SHT_REL, Scan, 7541 typename Target_mips::Relocatable_size_for_reloc>( 7542 symtab, 7543 layout, 7544 this, 7545 object, 7546 data_shndx, 7547 prelocs, 7548 reloc_count, 7549 output_section, 7550 needs_special_offset_handling, 7551 local_symbol_count, 7552 plocal_symbols); 7553 } 7554 7555 // Scan relocations for a section. 7556 7557 template<int size, bool big_endian> 7558 void 7559 Target_mips<size, big_endian>::scan_relocs( 7560 Symbol_table* symtab, 7561 Layout* layout, 7562 Sized_relobj_file<size, big_endian>* object, 7563 unsigned int data_shndx, 7564 unsigned int sh_type, 7565 const unsigned char* prelocs, 7566 size_t reloc_count, 7567 Output_section* output_section, 7568 bool needs_special_offset_handling, 7569 size_t local_symbol_count, 7570 const unsigned char* plocal_symbols) 7571 { 7572 typedef Target_mips<size, big_endian> Mips; 7573 typedef typename Target_mips<size, big_endian>::Scan Scan; 7574 7575 if (sh_type == elfcpp::SHT_REL) 7576 gold::scan_relocs<size, big_endian, Mips, elfcpp::SHT_REL, Scan>( 7577 symtab, 7578 layout, 7579 this, 7580 object, 7581 data_shndx, 7582 prelocs, 7583 reloc_count, 7584 output_section, 7585 needs_special_offset_handling, 7586 local_symbol_count, 7587 plocal_symbols); 7588 else if (sh_type == elfcpp::SHT_RELA) 7589 gold::scan_relocs<size, big_endian, Mips, elfcpp::SHT_RELA, Scan>( 7590 symtab, 7591 layout, 7592 this, 7593 object, 7594 data_shndx, 7595 prelocs, 7596 reloc_count, 7597 output_section, 7598 needs_special_offset_handling, 7599 local_symbol_count, 7600 plocal_symbols); 7601 } 7602 7603 template<int size, bool big_endian> 7604 bool 7605 Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags) 7606 { 7607 return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0 7608 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32 7609 || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32 7610 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1 7611 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2 7612 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32 7613 || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2); 7614 } 7615 7616 // Return the MACH for a MIPS e_flags value. 7617 template<int size, bool big_endian> 7618 unsigned int 7619 Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags) 7620 { 7621 switch (flags & elfcpp::EF_MIPS_MACH) 7622 { 7623 case elfcpp::E_MIPS_MACH_3900: 7624 return mach_mips3900; 7625 7626 case elfcpp::E_MIPS_MACH_4010: 7627 return mach_mips4010; 7628 7629 case elfcpp::E_MIPS_MACH_4100: 7630 return mach_mips4100; 7631 7632 case elfcpp::E_MIPS_MACH_4111: 7633 return mach_mips4111; 7634 7635 case elfcpp::E_MIPS_MACH_4120: 7636 return mach_mips4120; 7637 7638 case elfcpp::E_MIPS_MACH_4650: 7639 return mach_mips4650; 7640 7641 case elfcpp::E_MIPS_MACH_5400: 7642 return mach_mips5400; 7643 7644 case elfcpp::E_MIPS_MACH_5500: 7645 return mach_mips5500; 7646 7647 case elfcpp::E_MIPS_MACH_9000: 7648 return mach_mips9000; 7649 7650 case elfcpp::E_MIPS_MACH_SB1: 7651 return mach_mips_sb1; 7652 7653 case elfcpp::E_MIPS_MACH_LS2E: 7654 return mach_mips_loongson_2e; 7655 7656 case elfcpp::E_MIPS_MACH_LS2F: 7657 return mach_mips_loongson_2f; 7658 7659 case elfcpp::E_MIPS_MACH_LS3A: 7660 return mach_mips_loongson_3a; 7661 7662 case elfcpp::E_MIPS_MACH_OCTEON2: 7663 return mach_mips_octeon2; 7664 7665 case elfcpp::E_MIPS_MACH_OCTEON: 7666 return mach_mips_octeon; 7667 7668 case elfcpp::E_MIPS_MACH_XLR: 7669 return mach_mips_xlr; 7670 7671 default: 7672 switch (flags & elfcpp::EF_MIPS_ARCH) 7673 { 7674 default: 7675 case elfcpp::E_MIPS_ARCH_1: 7676 return mach_mips3000; 7677 7678 case elfcpp::E_MIPS_ARCH_2: 7679 return mach_mips6000; 7680 7681 case elfcpp::E_MIPS_ARCH_3: 7682 return mach_mips4000; 7683 7684 case elfcpp::E_MIPS_ARCH_4: 7685 return mach_mips8000; 7686 7687 case elfcpp::E_MIPS_ARCH_5: 7688 return mach_mips5; 7689 7690 case elfcpp::E_MIPS_ARCH_32: 7691 return mach_mipsisa32; 7692 7693 case elfcpp::E_MIPS_ARCH_64: 7694 return mach_mipsisa64; 7695 7696 case elfcpp::E_MIPS_ARCH_32R2: 7697 return mach_mipsisa32r2; 7698 7699 case elfcpp::E_MIPS_ARCH_64R2: 7700 return mach_mipsisa64r2; 7701 } 7702 } 7703 7704 return 0; 7705 } 7706 7707 // Check whether machine EXTENSION is an extension of machine BASE. 7708 template<int size, bool big_endian> 7709 bool 7710 Target_mips<size, big_endian>::mips_mach_extends(unsigned int base, 7711 unsigned int extension) 7712 { 7713 if (extension == base) 7714 return true; 7715 7716 if ((base == mach_mipsisa32) 7717 && this->mips_mach_extends(mach_mipsisa64, extension)) 7718 return true; 7719 7720 if ((base == mach_mipsisa32r2) 7721 && this->mips_mach_extends(mach_mipsisa64r2, extension)) 7722 return true; 7723 7724 for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i) 7725 if (extension == this->mips_mach_extensions_[i].first) 7726 { 7727 extension = this->mips_mach_extensions_[i].second; 7728 if (extension == base) 7729 return true; 7730 } 7731 7732 return false; 7733 } 7734 7735 template<int size, bool big_endian> 7736 void 7737 Target_mips<size, big_endian>::merge_processor_specific_flags( 7738 const std::string& name, elfcpp::Elf_Word in_flags, 7739 unsigned char in_ei_class, bool dyn_obj) 7740 { 7741 // If flags are not set yet, just copy them. 7742 if (!this->are_processor_specific_flags_set()) 7743 { 7744 this->set_processor_specific_flags(in_flags); 7745 this->ei_class_ = in_ei_class; 7746 this->mach_ = this->elf_mips_mach(in_flags); 7747 return; 7748 } 7749 7750 elfcpp::Elf_Word new_flags = in_flags; 7751 elfcpp::Elf_Word old_flags = this->processor_specific_flags(); 7752 elfcpp::Elf_Word merged_flags = this->processor_specific_flags(); 7753 merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER; 7754 7755 // Check flag compatibility. 7756 new_flags &= ~elfcpp::EF_MIPS_NOREORDER; 7757 old_flags &= ~elfcpp::EF_MIPS_NOREORDER; 7758 7759 // Some IRIX 6 BSD-compatibility objects have this bit set. It 7760 // doesn't seem to matter. 7761 new_flags &= ~elfcpp::EF_MIPS_XGOT; 7762 old_flags &= ~elfcpp::EF_MIPS_XGOT; 7763 7764 // MIPSpro generates ucode info in n64 objects. Again, we should 7765 // just be able to ignore this. 7766 new_flags &= ~elfcpp::EF_MIPS_UCODE; 7767 old_flags &= ~elfcpp::EF_MIPS_UCODE; 7768 7769 // DSOs should only be linked with CPIC code. 7770 if (dyn_obj) 7771 new_flags |= elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC; 7772 7773 if (new_flags == old_flags) 7774 { 7775 this->set_processor_specific_flags(merged_flags); 7776 return; 7777 } 7778 7779 if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0) 7780 != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)) 7781 gold_warning(_("%s: linking abicalls files with non-abicalls files"), 7782 name.c_str()); 7783 7784 if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) 7785 merged_flags |= elfcpp::EF_MIPS_CPIC; 7786 if (!(new_flags & elfcpp::EF_MIPS_PIC)) 7787 merged_flags &= ~elfcpp::EF_MIPS_PIC; 7788 7789 new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC); 7790 old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC); 7791 7792 // Compare the ISAs. 7793 if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags)) 7794 gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str()); 7795 else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_)) 7796 { 7797 // Output ISA isn't the same as, or an extension of, input ISA. 7798 if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags))) 7799 { 7800 // Copy the architecture info from input object to output. Also copy 7801 // the 32-bit flag (if set) so that we continue to recognise 7802 // output as a 32-bit binary. 7803 this->mach_ = this->elf_mips_mach(in_flags); 7804 merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH); 7805 merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH 7806 | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE)); 7807 7808 // Copy across the ABI flags if output doesn't use them 7809 // and if that was what caused us to treat input object as 32-bit. 7810 if ((old_flags & elfcpp::EF_MIPS_ABI) == 0 7811 && this->mips_32bit_flags(new_flags) 7812 && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI)) 7813 merged_flags |= new_flags & elfcpp::EF_MIPS_ABI; 7814 } 7815 else 7816 // The ISAs aren't compatible. 7817 gold_error(_("%s: linking %s module with previous %s modules"), 7818 name.c_str(), this->elf_mips_mach_name(in_flags), 7819 this->elf_mips_mach_name(merged_flags)); 7820 } 7821 7822 new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH 7823 | elfcpp::EF_MIPS_32BITMODE)); 7824 old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH 7825 | elfcpp::EF_MIPS_32BITMODE)); 7826 7827 // Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it does set 7828 // EI_CLASS differently from any 32-bit ABI. 7829 if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI) 7830 || (in_ei_class != this->ei_class_)) 7831 { 7832 // Only error if both are set (to different values). 7833 if (((new_flags & elfcpp::EF_MIPS_ABI) 7834 && (old_flags & elfcpp::EF_MIPS_ABI)) 7835 || (in_ei_class != this->ei_class_)) 7836 gold_error(_("%s: ABI mismatch: linking %s module with " 7837 "previous %s modules"), name.c_str(), 7838 this->elf_mips_abi_name(in_flags, in_ei_class), 7839 this->elf_mips_abi_name(merged_flags, this->ei_class_)); 7840 7841 new_flags &= ~elfcpp::EF_MIPS_ABI; 7842 old_flags &= ~elfcpp::EF_MIPS_ABI; 7843 } 7844 7845 // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together 7846 // and allow arbitrary mixing of the remaining ASEs (retain the union). 7847 if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE) 7848 != (old_flags & elfcpp::EF_MIPS_ARCH_ASE)) 7849 { 7850 int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS; 7851 int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS; 7852 int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16; 7853 int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16; 7854 int micro_mis = old_m16 && new_micro; 7855 int m16_mis = old_micro && new_m16; 7856 7857 if (m16_mis || micro_mis) 7858 gold_error(_("%s: ASE mismatch: linking %s module with " 7859 "previous %s modules"), name.c_str(), 7860 m16_mis ? "MIPS16" : "microMIPS", 7861 m16_mis ? "microMIPS" : "MIPS16"); 7862 7863 merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE; 7864 7865 new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE; 7866 old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE; 7867 } 7868 7869 // Warn about any other mismatches. 7870 if (new_flags != old_flags) 7871 gold_error(_("%s: uses different e_flags (0x%x) fields than previous " 7872 "modules (0x%x)"), name.c_str(), new_flags, old_flags); 7873 7874 this->set_processor_specific_flags(merged_flags); 7875 } 7876 7877 // Adjust ELF file header. 7878 7879 template<int size, bool big_endian> 7880 void 7881 Target_mips<size, big_endian>::do_adjust_elf_header( 7882 unsigned char* view, 7883 int len) 7884 { 7885 gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size); 7886 7887 elfcpp::Ehdr<size, big_endian> ehdr(view); 7888 unsigned char e_ident[elfcpp::EI_NIDENT]; 7889 memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT); 7890 7891 e_ident[elfcpp::EI_CLASS] = this->ei_class_; 7892 7893 elfcpp::Ehdr_write<size, big_endian> oehdr(view); 7894 oehdr.put_e_ident(e_ident); 7895 if (this->entry_symbol_is_compressed_) 7896 oehdr.put_e_entry(ehdr.get_e_entry() + 1); 7897 } 7898 7899 // do_make_elf_object to override the same function in the base class. 7900 // We need to use a target-specific sub-class of 7901 // Sized_relobj_file<size, big_endian> to store Mips specific information. 7902 // Hence we need to have our own ELF object creation. 7903 7904 template<int size, bool big_endian> 7905 Object* 7906 Target_mips<size, big_endian>::do_make_elf_object( 7907 const std::string& name, 7908 Input_file* input_file, 7909 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr) 7910 { 7911 int et = ehdr.get_e_type(); 7912 // ET_EXEC files are valid input for --just-symbols/-R, 7913 // and we treat them as relocatable objects. 7914 if (et == elfcpp::ET_REL 7915 || (et == elfcpp::ET_EXEC && input_file->just_symbols())) 7916 { 7917 Mips_relobj<size, big_endian>* obj = 7918 new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr); 7919 obj->setup(); 7920 return obj; 7921 } 7922 else if (et == elfcpp::ET_DYN) 7923 { 7924 // TODO(sasa): Should we create Mips_dynobj? 7925 return Target::do_make_elf_object(name, input_file, offset, ehdr); 7926 } 7927 else 7928 { 7929 gold_error(_("%s: unsupported ELF file type %d"), 7930 name.c_str(), et); 7931 return NULL; 7932 } 7933 } 7934 7935 // Finalize the sections. 7936 7937 template <int size, bool big_endian> 7938 void 7939 Target_mips<size, big_endian>::do_finalize_sections(Layout* layout, 7940 const Input_objects* input_objects, 7941 Symbol_table* symtab) 7942 { 7943 // Add +1 to MIPS16 and microMIPS init_ and _fini symbols so that DT_INIT and 7944 // DT_FINI have correct values. 7945 Mips_symbol<size>* init = static_cast<Mips_symbol<size>*>( 7946 symtab->lookup(parameters->options().init())); 7947 if (init != NULL && (init->is_mips16() || init->is_micromips())) 7948 init->set_value(init->value() | 1); 7949 Mips_symbol<size>* fini = static_cast<Mips_symbol<size>*>( 7950 symtab->lookup(parameters->options().fini())); 7951 if (fini != NULL && (fini->is_mips16() || fini->is_micromips())) 7952 fini->set_value(fini->value() | 1); 7953 7954 // Check whether the entry symbol is mips16 or micromips. This is needed to 7955 // adjust entry address in ELF header. 7956 Mips_symbol<size>* entry = 7957 static_cast<Mips_symbol<size>*>(symtab->lookup(this->entry_symbol_name())); 7958 this->entry_symbol_is_compressed_ = (entry != NULL && (entry->is_mips16() 7959 || entry->is_micromips())); 7960 7961 if (!parameters->doing_static_link() 7962 && (strcmp(parameters->options().hash_style(), "gnu") == 0 7963 || strcmp(parameters->options().hash_style(), "both") == 0)) 7964 { 7965 // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different 7966 // ways. .gnu.hash needs symbols to be grouped by hash code whereas the 7967 // MIPS ABI requires a mapping between the GOT and the symbol table. 7968 gold_error(".gnu.hash is incompatible with the MIPS ABI"); 7969 } 7970 7971 // Check whether the final section that was scanned has HI16 or GOT16 7972 // relocations without the corresponding LO16 part. 7973 if (this->got16_addends_.size() > 0) 7974 gold_error("Can't find matching LO16 reloc"); 7975 7976 // Set _gp value. 7977 this->set_gp(layout, symtab); 7978 7979 // Check for any mips16 stub sections that we can discard. 7980 if (!parameters->options().relocatable()) 7981 { 7982 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); 7983 p != input_objects->relobj_end(); 7984 ++p) 7985 { 7986 Mips_relobj<size, big_endian>* object = 7987 Mips_relobj<size, big_endian>::as_mips_relobj(*p); 7988 object->discard_mips16_stub_sections(symtab); 7989 } 7990 } 7991 7992 // Merge processor-specific flags. 7993 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); 7994 p != input_objects->relobj_end(); 7995 ++p) 7996 { 7997 Mips_relobj<size, big_endian>* relobj = 7998 Mips_relobj<size, big_endian>::as_mips_relobj(*p); 7999 8000 Input_file::Format format = relobj->input_file()->format(); 8001 if (format == Input_file::FORMAT_ELF) 8002 { 8003 // Read processor-specific flags in ELF file header. 8004 const unsigned char* pehdr = relobj->get_view( 8005 elfcpp::file_header_offset, 8006 elfcpp::Elf_sizes<size>::ehdr_size, 8007 true, false); 8008 8009 elfcpp::Ehdr<size, big_endian> ehdr(pehdr); 8010 elfcpp::Elf_Word in_flags = ehdr.get_e_flags(); 8011 unsigned char ei_class = ehdr.get_e_ident()[elfcpp::EI_CLASS]; 8012 8013 this->merge_processor_specific_flags(relobj->name(), in_flags, 8014 ei_class, false); 8015 } 8016 } 8017 8018 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin(); 8019 p != input_objects->dynobj_end(); 8020 ++p) 8021 { 8022 Sized_dynobj<size, big_endian>* dynobj = 8023 static_cast<Sized_dynobj<size, big_endian>*>(*p); 8024 8025 // Read processor-specific flags. 8026 const unsigned char* pehdr = dynobj->get_view(elfcpp::file_header_offset, 8027 elfcpp::Elf_sizes<size>::ehdr_size, 8028 true, false); 8029 8030 elfcpp::Ehdr<size, big_endian> ehdr(pehdr); 8031 elfcpp::Elf_Word in_flags = ehdr.get_e_flags(); 8032 unsigned char ei_class = ehdr.get_e_ident()[elfcpp::EI_CLASS]; 8033 8034 this->merge_processor_specific_flags(dynobj->name(), in_flags, ei_class, 8035 true); 8036 } 8037 8038 // Merge .reginfo contents of input objects. 8039 Valtype gprmask = 0; 8040 Valtype cprmask1 = 0; 8041 Valtype cprmask2 = 0; 8042 Valtype cprmask3 = 0; 8043 Valtype cprmask4 = 0; 8044 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); 8045 p != input_objects->relobj_end(); 8046 ++p) 8047 { 8048 Mips_relobj<size, big_endian>* relobj = 8049 Mips_relobj<size, big_endian>::as_mips_relobj(*p); 8050 8051 gprmask |= relobj->gprmask(); 8052 cprmask1 |= relobj->cprmask1(); 8053 cprmask2 |= relobj->cprmask2(); 8054 cprmask3 |= relobj->cprmask3(); 8055 cprmask4 |= relobj->cprmask4(); 8056 } 8057 8058 if (this->plt_ != NULL) 8059 { 8060 // Set final PLT offsets for symbols. 8061 this->plt_section()->set_plt_offsets(); 8062 8063 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section. 8064 // Set STO_MICROMIPS flag if the output has microMIPS code, but only if 8065 // there are no standard PLT entries present. 8066 unsigned char nonvis = 0; 8067 if (this->is_output_micromips() 8068 && !this->plt_section()->has_standard_entries()) 8069 nonvis = elfcpp::STO_MICROMIPS >> 2; 8070 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL, 8071 Symbol_table::PREDEFINED, 8072 this->plt_, 8073 0, 0, elfcpp::STT_FUNC, 8074 elfcpp::STB_LOCAL, 8075 elfcpp::STV_DEFAULT, nonvis, 8076 false, false); 8077 } 8078 8079 if (this->mips_stubs_ != NULL) 8080 { 8081 // Define _MIPS_STUBS_ at the start of the .MIPS.stubs section. 8082 unsigned char nonvis = 0; 8083 if (this->is_output_micromips()) 8084 nonvis = elfcpp::STO_MICROMIPS >> 2; 8085 symtab->define_in_output_data("_MIPS_STUBS_", NULL, 8086 Symbol_table::PREDEFINED, 8087 this->mips_stubs_, 8088 0, 0, elfcpp::STT_FUNC, 8089 elfcpp::STB_LOCAL, 8090 elfcpp::STV_DEFAULT, nonvis, 8091 false, false); 8092 } 8093 8094 if (!parameters->options().relocatable() && !parameters->doing_static_link()) 8095 // In case there is no .got section, create one. 8096 this->got_section(symtab, layout); 8097 8098 // Emit any relocs we saved in an attempt to avoid generating COPY 8099 // relocs. 8100 if (this->copy_relocs_.any_saved_relocs()) 8101 this->copy_relocs_.emit_mips(this->rel_dyn_section(layout), symtab, layout, 8102 this); 8103 8104 // Emit dynamic relocs. 8105 for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin(); 8106 p != this->dyn_relocs_.end(); 8107 ++p) 8108 p->emit(this->rel_dyn_section(layout), this->got_section(), symtab); 8109 8110 if (this->has_got_section()) 8111 this->got_section()->lay_out_got(layout, symtab, input_objects); 8112 8113 if (this->mips_stubs_ != NULL) 8114 this->mips_stubs_->set_needs_dynsym_value(); 8115 8116 // Check for functions that might need $25 to be valid on entry. 8117 // TODO(sasa): Can we do this without iterating over all symbols? 8118 typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor; 8119 symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout, 8120 symtab)); 8121 8122 // Add NULL segment. 8123 if (!parameters->options().relocatable()) 8124 layout->make_output_segment(elfcpp::PT_NULL, 0); 8125 8126 for (Layout::Section_list::const_iterator p = layout->section_list().begin(); 8127 p != layout->section_list().end(); 8128 ++p) 8129 { 8130 if ((*p)->type() == elfcpp::SHT_MIPS_REGINFO) 8131 { 8132 Mips_output_section_reginfo<size, big_endian>* reginfo = 8133 Mips_output_section_reginfo<size, big_endian>:: 8134 as_mips_output_section_reginfo(*p); 8135 8136 reginfo->set_masks(gprmask, cprmask1, cprmask2, cprmask3, cprmask4); 8137 8138 if (!parameters->options().relocatable()) 8139 { 8140 Output_segment* reginfo_segment = 8141 layout->make_output_segment(elfcpp::PT_MIPS_REGINFO, 8142 elfcpp::PF_R); 8143 reginfo_segment->add_output_section_to_nonload(reginfo, 8144 elfcpp::PF_R); 8145 } 8146 } 8147 } 8148 8149 // Fill in some more dynamic tags. 8150 // TODO(sasa): Add more dynamic tags. 8151 const Reloc_section* rel_plt = (this->plt_ == NULL 8152 ? NULL : this->plt_->rel_plt()); 8153 layout->add_target_dynamic_tags(true, this->got_, rel_plt, 8154 this->rel_dyn_, true, false); 8155 8156 Output_data_dynamic* const odyn = layout->dynamic_data(); 8157 if (odyn != NULL 8158 && !parameters->options().relocatable() 8159 && !parameters->doing_static_link()) 8160 { 8161 unsigned int d_val; 8162 // This element holds a 32-bit version id for the Runtime 8163 // Linker Interface. This will start at integer value 1. 8164 d_val = 0x01; 8165 odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val); 8166 8167 // Dynamic flags 8168 d_val = elfcpp::RHF_NOTPOT; 8169 odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val); 8170 8171 // Save layout for using when emiting custom dynamic tags. 8172 this->layout_ = layout; 8173 8174 // This member holds the base address of the segment. 8175 odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS); 8176 8177 // This member holds the number of entries in the .dynsym section. 8178 odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO); 8179 8180 // This member holds the index of the first dynamic symbol 8181 // table entry that corresponds to an entry in the global offset table. 8182 odyn->add_custom(elfcpp::DT_MIPS_GOTSYM); 8183 8184 // This member holds the number of local GOT entries. 8185 odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO, 8186 this->got_->get_local_gotno()); 8187 8188 if (this->plt_ != NULL) 8189 // DT_MIPS_PLTGOT dynamic tag 8190 odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_); 8191 } 8192 } 8193 8194 // Get the custom dynamic tag value. 8195 template<int size, bool big_endian> 8196 unsigned int 8197 Target_mips<size, big_endian>::do_dynamic_tag_custom_value(elfcpp::DT tag) const 8198 { 8199 switch (tag) 8200 { 8201 case elfcpp::DT_MIPS_BASE_ADDRESS: 8202 { 8203 // The base address of the segment. 8204 // At this point, the segment list has been sorted into final order, 8205 // so just return vaddr of the first readable PT_LOAD segment. 8206 Output_segment* seg = 8207 this->layout_->find_output_segment(elfcpp::PT_LOAD, elfcpp::PF_R, 0); 8208 gold_assert(seg != NULL); 8209 return seg->vaddr(); 8210 } 8211 8212 case elfcpp::DT_MIPS_SYMTABNO: 8213 // The number of entries in the .dynsym section. 8214 return this->get_dt_mips_symtabno(); 8215 8216 case elfcpp::DT_MIPS_GOTSYM: 8217 { 8218 // The index of the first dynamic symbol table entry that corresponds 8219 // to an entry in the GOT. 8220 if (this->got_->first_global_got_dynsym_index() != -1U) 8221 return this->got_->first_global_got_dynsym_index(); 8222 else 8223 // In case if we don't have global GOT symbols we default to setting 8224 // DT_MIPS_GOTSYM to the same value as DT_MIPS_SYMTABNO. 8225 return this->get_dt_mips_symtabno(); 8226 } 8227 8228 default: 8229 gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag); 8230 } 8231 8232 return (unsigned int)-1; 8233 } 8234 8235 // Relocate section data. 8236 8237 template<int size, bool big_endian> 8238 void 8239 Target_mips<size, big_endian>::relocate_section( 8240 const Relocate_info<size, big_endian>* relinfo, 8241 unsigned int sh_type, 8242 const unsigned char* prelocs, 8243 size_t reloc_count, 8244 Output_section* output_section, 8245 bool needs_special_offset_handling, 8246 unsigned char* view, 8247 Mips_address address, 8248 section_size_type view_size, 8249 const Reloc_symbol_changes* reloc_symbol_changes) 8250 { 8251 typedef Target_mips<size, big_endian> Mips; 8252 typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate; 8253 8254 if (sh_type == elfcpp::SHT_REL) 8255 gold::relocate_section<size, big_endian, Mips, elfcpp::SHT_REL, 8256 Mips_relocate, gold::Default_comdat_behavior>( 8257 relinfo, 8258 this, 8259 prelocs, 8260 reloc_count, 8261 output_section, 8262 needs_special_offset_handling, 8263 view, 8264 address, 8265 view_size, 8266 reloc_symbol_changes); 8267 else if (sh_type == elfcpp::SHT_RELA) 8268 gold::relocate_section<size, big_endian, Mips, elfcpp::SHT_RELA, 8269 Mips_relocate, gold::Default_comdat_behavior>( 8270 relinfo, 8271 this, 8272 prelocs, 8273 reloc_count, 8274 output_section, 8275 needs_special_offset_handling, 8276 view, 8277 address, 8278 view_size, 8279 reloc_symbol_changes); 8280 } 8281 8282 // Return the size of a relocation while scanning during a relocatable 8283 // link. 8284 8285 template<int size, bool big_endian> 8286 unsigned int 8287 Target_mips<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc( 8288 unsigned int r_type, 8289 Relobj* object) 8290 { 8291 switch (r_type) 8292 { 8293 case elfcpp::R_MIPS_NONE: 8294 case elfcpp::R_MIPS_TLS_DTPMOD64: 8295 case elfcpp::R_MIPS_TLS_DTPREL64: 8296 case elfcpp::R_MIPS_TLS_TPREL64: 8297 return 0; 8298 8299 case elfcpp::R_MIPS_32: 8300 case elfcpp::R_MIPS_TLS_DTPMOD32: 8301 case elfcpp::R_MIPS_TLS_DTPREL32: 8302 case elfcpp::R_MIPS_TLS_TPREL32: 8303 case elfcpp::R_MIPS_REL32: 8304 case elfcpp::R_MIPS_PC32: 8305 case elfcpp::R_MIPS_GPREL32: 8306 case elfcpp::R_MIPS_JALR: 8307 return 4; 8308 8309 case elfcpp::R_MIPS_16: 8310 case elfcpp::R_MIPS_HI16: 8311 case elfcpp::R_MIPS_LO16: 8312 case elfcpp::R_MIPS_GPREL16: 8313 case elfcpp::R_MIPS16_HI16: 8314 case elfcpp::R_MIPS16_LO16: 8315 case elfcpp::R_MIPS_PC16: 8316 case elfcpp::R_MIPS_GOT16: 8317 case elfcpp::R_MIPS16_GOT16: 8318 case elfcpp::R_MIPS_CALL16: 8319 case elfcpp::R_MIPS16_CALL16: 8320 case elfcpp::R_MIPS_GOT_HI16: 8321 case elfcpp::R_MIPS_CALL_HI16: 8322 case elfcpp::R_MIPS_GOT_LO16: 8323 case elfcpp::R_MIPS_CALL_LO16: 8324 case elfcpp::R_MIPS_TLS_DTPREL_HI16: 8325 case elfcpp::R_MIPS_TLS_DTPREL_LO16: 8326 case elfcpp::R_MIPS_TLS_TPREL_HI16: 8327 case elfcpp::R_MIPS_TLS_TPREL_LO16: 8328 case elfcpp::R_MIPS16_GPREL: 8329 case elfcpp::R_MIPS_GOT_DISP: 8330 case elfcpp::R_MIPS_LITERAL: 8331 case elfcpp::R_MIPS_GOT_PAGE: 8332 case elfcpp::R_MIPS_GOT_OFST: 8333 case elfcpp::R_MIPS_TLS_GD: 8334 case elfcpp::R_MIPS_TLS_LDM: 8335 case elfcpp::R_MIPS_TLS_GOTTPREL: 8336 return 2; 8337 8338 // These relocations are not byte sized 8339 case elfcpp::R_MIPS_26: 8340 case elfcpp::R_MIPS16_26: 8341 return 4; 8342 8343 case elfcpp::R_MIPS_COPY: 8344 case elfcpp::R_MIPS_JUMP_SLOT: 8345 object->error(_("unexpected reloc %u in object file"), r_type); 8346 return 0; 8347 8348 default: 8349 object->error(_("unsupported reloc %u in object file"), r_type); 8350 return 0; 8351 } 8352 } 8353 8354 // Scan the relocs during a relocatable link. 8355 8356 template<int size, bool big_endian> 8357 void 8358 Target_mips<size, big_endian>::scan_relocatable_relocs( 8359 Symbol_table* symtab, 8360 Layout* layout, 8361 Sized_relobj_file<size, big_endian>* object, 8362 unsigned int data_shndx, 8363 unsigned int sh_type, 8364 const unsigned char* prelocs, 8365 size_t reloc_count, 8366 Output_section* output_section, 8367 bool needs_special_offset_handling, 8368 size_t local_symbol_count, 8369 const unsigned char* plocal_symbols, 8370 Relocatable_relocs* rr) 8371 { 8372 gold_assert(sh_type == elfcpp::SHT_REL); 8373 8374 typedef Mips_scan_relocatable_relocs<big_endian, elfcpp::SHT_REL, 8375 Relocatable_size_for_reloc> Scan_relocatable_relocs; 8376 8377 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_REL, 8378 Scan_relocatable_relocs>( 8379 symtab, 8380 layout, 8381 object, 8382 data_shndx, 8383 prelocs, 8384 reloc_count, 8385 output_section, 8386 needs_special_offset_handling, 8387 local_symbol_count, 8388 plocal_symbols, 8389 rr); 8390 } 8391 8392 // Emit relocations for a section. 8393 8394 template<int size, bool big_endian> 8395 void 8396 Target_mips<size, big_endian>::relocate_relocs( 8397 const Relocate_info<size, big_endian>* relinfo, 8398 unsigned int sh_type, 8399 const unsigned char* prelocs, 8400 size_t reloc_count, 8401 Output_section* output_section, 8402 typename elfcpp::Elf_types<size>::Elf_Off 8403 offset_in_output_section, 8404 const Relocatable_relocs* rr, 8405 unsigned char* view, 8406 Mips_address view_address, 8407 section_size_type view_size, 8408 unsigned char* reloc_view, 8409 section_size_type reloc_view_size) 8410 { 8411 gold_assert(sh_type == elfcpp::SHT_REL); 8412 8413 gold::relocate_relocs<size, big_endian, elfcpp::SHT_REL>( 8414 relinfo, 8415 prelocs, 8416 reloc_count, 8417 output_section, 8418 offset_in_output_section, 8419 rr, 8420 view, 8421 view_address, 8422 view_size, 8423 reloc_view, 8424 reloc_view_size); 8425 } 8426 8427 // Perform target-specific processing in a relocatable link. This is 8428 // only used if we use the relocation strategy RELOC_SPECIAL. 8429 8430 template<int size, bool big_endian> 8431 void 8432 Target_mips<size, big_endian>::relocate_special_relocatable( 8433 const Relocate_info<size, big_endian>* relinfo, 8434 unsigned int sh_type, 8435 const unsigned char* preloc_in, 8436 size_t relnum, 8437 Output_section* output_section, 8438 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section, 8439 unsigned char* view, 8440 Mips_address view_address, 8441 section_size_type, 8442 unsigned char* preloc_out) 8443 { 8444 // We can only handle REL type relocation sections. 8445 gold_assert(sh_type == elfcpp::SHT_REL); 8446 8447 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc 8448 Reltype; 8449 typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write 8450 Reltype_write; 8451 8452 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs; 8453 8454 const Mips_address invalid_address = static_cast<Mips_address>(0) - 1; 8455 8456 Mips_relobj<size, big_endian>* object = 8457 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object); 8458 const unsigned int local_count = object->local_symbol_count(); 8459 8460 Reltype reloc(preloc_in); 8461 Reltype_write reloc_write(preloc_out); 8462 8463 elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info(); 8464 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); 8465 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info); 8466 8467 // Get the new symbol index. 8468 // We only use RELOC_SPECIAL strategy in local relocations. 8469 gold_assert(r_sym < local_count); 8470 8471 // We are adjusting a section symbol. We need to find 8472 // the symbol table index of the section symbol for 8473 // the output section corresponding to input section 8474 // in which this symbol is defined. 8475 bool is_ordinary; 8476 unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary); 8477 gold_assert(is_ordinary); 8478 Output_section* os = object->output_section(shndx); 8479 gold_assert(os != NULL); 8480 gold_assert(os->needs_symtab_index()); 8481 unsigned int new_symndx = os->symtab_index(); 8482 8483 // Get the new offset--the location in the output section where 8484 // this relocation should be applied. 8485 8486 Mips_address offset = reloc.get_r_offset(); 8487 Mips_address new_offset; 8488 if (offset_in_output_section != invalid_address) 8489 new_offset = offset + offset_in_output_section; 8490 else 8491 { 8492 section_offset_type sot_offset = 8493 convert_types<section_offset_type, Mips_address>(offset); 8494 section_offset_type new_sot_offset = 8495 output_section->output_offset(object, relinfo->data_shndx, 8496 sot_offset); 8497 gold_assert(new_sot_offset != -1); 8498 new_offset = new_sot_offset; 8499 } 8500 8501 // In an object file, r_offset is an offset within the section. 8502 // In an executable or dynamic object, generated by 8503 // --emit-relocs, r_offset is an absolute address. 8504 if (!parameters->options().relocatable()) 8505 { 8506 new_offset += view_address; 8507 if (offset_in_output_section != invalid_address) 8508 new_offset -= offset_in_output_section; 8509 } 8510 8511 reloc_write.put_r_offset(new_offset); 8512 reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type)); 8513 8514 // Handle the reloc addend. 8515 // The relocation uses a section symbol in the input file. 8516 // We are adjusting it to use a section symbol in the output 8517 // file. The input section symbol refers to some address in 8518 // the input section. We need the relocation in the output 8519 // file to refer to that same address. This adjustment to 8520 // the addend is the same calculation we use for a simple 8521 // absolute relocation for the input section symbol. 8522 8523 const Symbol_value<size>* psymval = object->local_symbol(r_sym); 8524 8525 unsigned char* paddend = view + offset; 8526 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY; 8527 switch (r_type) 8528 { 8529 case elfcpp::R_MIPS_26: 8530 reloc_status = Reloc_funcs::rel26(paddend, object, psymval, 8531 offset_in_output_section, true, 0, sh_type == elfcpp::SHT_REL, NULL, 8532 false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal()); 8533 break; 8534 8535 default: 8536 gold_unreachable(); 8537 } 8538 8539 // Report any errors. 8540 switch (reloc_status) 8541 { 8542 case Reloc_funcs::STATUS_OKAY: 8543 break; 8544 case Reloc_funcs::STATUS_OVERFLOW: 8545 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(), 8546 _("relocation overflow")); 8547 break; 8548 case Reloc_funcs::STATUS_BAD_RELOC: 8549 gold_error_at_location(relinfo, relnum, reloc.get_r_offset(), 8550 _("unexpected opcode while processing relocation")); 8551 break; 8552 default: 8553 gold_unreachable(); 8554 } 8555 } 8556 8557 // Optimize the TLS relocation type based on what we know about the 8558 // symbol. IS_FINAL is true if the final address of this symbol is 8559 // known at link time. 8560 8561 template<int size, bool big_endian> 8562 tls::Tls_optimization 8563 Target_mips<size, big_endian>::optimize_tls_reloc(bool, int) 8564 { 8565 // FIXME: Currently we do not do any TLS optimization. 8566 return tls::TLSOPT_NONE; 8567 } 8568 8569 // Scan a relocation for a local symbol. 8570 8571 template<int size, bool big_endian> 8572 inline void 8573 Target_mips<size, big_endian>::Scan::local( 8574 Symbol_table* symtab, 8575 Layout* layout, 8576 Target_mips<size, big_endian>* target, 8577 Sized_relobj_file<size, big_endian>* object, 8578 unsigned int data_shndx, 8579 Output_section* output_section, 8580 const elfcpp::Rela<size, big_endian>* rela, 8581 const elfcpp::Rel<size, big_endian>* rel, 8582 unsigned int rel_type, 8583 unsigned int r_type, 8584 const elfcpp::Sym<size, big_endian>& lsym, 8585 bool is_discarded) 8586 { 8587 if (is_discarded) 8588 return; 8589 8590 Mips_address r_offset; 8591 typename elfcpp::Elf_types<size>::Elf_WXword r_info; 8592 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend; 8593 8594 if (rel_type == elfcpp::SHT_RELA) 8595 { 8596 r_offset = rela->get_r_offset(); 8597 r_info = rela->get_r_info(); 8598 r_addend = rela->get_r_addend(); 8599 } 8600 else 8601 { 8602 r_offset = rel->get_r_offset(); 8603 r_info = rel->get_r_info(); 8604 r_addend = 0; 8605 } 8606 8607 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); 8608 Mips_relobj<size, big_endian>* mips_obj = 8609 Mips_relobj<size, big_endian>::as_mips_relobj(object); 8610 8611 if (mips_obj->is_mips16_stub_section(data_shndx)) 8612 { 8613 mips_obj->get_mips16_stub_section(data_shndx) 8614 ->new_local_reloc_found(r_type, r_sym); 8615 } 8616 8617 if (r_type == elfcpp::R_MIPS_NONE) 8618 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the 8619 // mips16 stub. 8620 return; 8621 8622 if (!mips16_call_reloc(r_type) 8623 && !mips_obj->section_allows_mips16_refs(data_shndx)) 8624 // This reloc would need to refer to a MIPS16 hard-float stub, if 8625 // there is one. We ignore MIPS16 stub sections and .pdr section when 8626 // looking for relocs that would need to refer to MIPS16 stubs. 8627 mips_obj->add_local_non_16bit_call(r_sym); 8628 8629 if (r_type == elfcpp::R_MIPS16_26 8630 && !mips_obj->section_allows_mips16_refs(data_shndx)) 8631 mips_obj->add_local_16bit_call(r_sym); 8632 8633 switch (r_type) 8634 { 8635 case elfcpp::R_MIPS_GOT16: 8636 case elfcpp::R_MIPS_CALL16: 8637 case elfcpp::R_MIPS_CALL_HI16: 8638 case elfcpp::R_MIPS_CALL_LO16: 8639 case elfcpp::R_MIPS_GOT_HI16: 8640 case elfcpp::R_MIPS_GOT_LO16: 8641 case elfcpp::R_MIPS_GOT_PAGE: 8642 case elfcpp::R_MIPS_GOT_OFST: 8643 case elfcpp::R_MIPS_GOT_DISP: 8644 case elfcpp::R_MIPS_TLS_GOTTPREL: 8645 case elfcpp::R_MIPS_TLS_GD: 8646 case elfcpp::R_MIPS_TLS_LDM: 8647 case elfcpp::R_MIPS16_GOT16: 8648 case elfcpp::R_MIPS16_CALL16: 8649 case elfcpp::R_MIPS16_TLS_GOTTPREL: 8650 case elfcpp::R_MIPS16_TLS_GD: 8651 case elfcpp::R_MIPS16_TLS_LDM: 8652 case elfcpp::R_MICROMIPS_GOT16: 8653 case elfcpp::R_MICROMIPS_CALL16: 8654 case elfcpp::R_MICROMIPS_CALL_HI16: 8655 case elfcpp::R_MICROMIPS_CALL_LO16: 8656 case elfcpp::R_MICROMIPS_GOT_HI16: 8657 case elfcpp::R_MICROMIPS_GOT_LO16: 8658 case elfcpp::R_MICROMIPS_GOT_PAGE: 8659 case elfcpp::R_MICROMIPS_GOT_OFST: 8660 case elfcpp::R_MICROMIPS_GOT_DISP: 8661 case elfcpp::R_MICROMIPS_TLS_GOTTPREL: 8662 case elfcpp::R_MICROMIPS_TLS_GD: 8663 case elfcpp::R_MICROMIPS_TLS_LDM: 8664 // We need a GOT section. 8665 target->got_section(symtab, layout); 8666 break; 8667 8668 default: 8669 break; 8670 } 8671 8672 if (call_lo16_reloc(r_type) 8673 || got_lo16_reloc(r_type) 8674 || got_disp_reloc(r_type)) 8675 { 8676 // We may need a local GOT entry for this relocation. We 8677 // don't count R_MIPS_GOT_PAGE because we can estimate the 8678 // maximum number of pages needed by looking at the size of 8679 // the segment. Similar comments apply to R_MIPS*_GOT16 and 8680 // R_MIPS*_CALL16. We don't count R_MIPS_GOT_HI16, or 8681 // R_MIPS_CALL_HI16 because these are always followed by an 8682 // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. 8683 Mips_output_data_got<size, big_endian>* got = 8684 target->got_section(symtab, layout); 8685 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); 8686 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, -1U); 8687 } 8688 8689 switch (r_type) 8690 { 8691 case elfcpp::R_MIPS_CALL16: 8692 case elfcpp::R_MIPS16_CALL16: 8693 case elfcpp::R_MICROMIPS_CALL16: 8694 gold_error(_("CALL16 reloc at 0x%lx not against global symbol "), 8695 (unsigned long)r_offset); 8696 return; 8697 8698 case elfcpp::R_MIPS_GOT_PAGE: 8699 case elfcpp::R_MICROMIPS_GOT_PAGE: 8700 case elfcpp::R_MIPS16_GOT16: 8701 case elfcpp::R_MIPS_GOT16: 8702 case elfcpp::R_MIPS_GOT_HI16: 8703 case elfcpp::R_MIPS_GOT_LO16: 8704 case elfcpp::R_MICROMIPS_GOT16: 8705 case elfcpp::R_MICROMIPS_GOT_HI16: 8706 case elfcpp::R_MICROMIPS_GOT_LO16: 8707 { 8708 // This relocation needs a page entry in the GOT. 8709 // Get the section contents. 8710 section_size_type view_size = 0; 8711 const unsigned char* view = object->section_contents(data_shndx, 8712 &view_size, false); 8713 view += r_offset; 8714 8715 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view); 8716 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff 8717 : r_addend); 8718 8719 if (rel_type == elfcpp::SHT_REL && got16_reloc(r_type)) 8720 target->got16_addends_.push_back(got16_addend<size, big_endian>( 8721 object, data_shndx, r_type, r_sym, addend)); 8722 else 8723 target->got_section()->record_got_page_entry(mips_obj, r_sym, addend); 8724 break; 8725 } 8726 8727 case elfcpp::R_MIPS_HI16: 8728 case elfcpp::R_MIPS16_HI16: 8729 case elfcpp::R_MICROMIPS_HI16: 8730 // Record the reloc so that we can check whether the corresponding LO16 8731 // part exists. 8732 if (rel_type == elfcpp::SHT_REL) 8733 target->got16_addends_.push_back(got16_addend<size, big_endian>( 8734 object, data_shndx, r_type, r_sym, 0)); 8735 break; 8736 8737 case elfcpp::R_MIPS_LO16: 8738 case elfcpp::R_MIPS16_LO16: 8739 case elfcpp::R_MICROMIPS_LO16: 8740 { 8741 if (rel_type != elfcpp::SHT_REL) 8742 break; 8743 8744 // Find corresponding GOT16/HI16 relocation. 8745 8746 // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must 8747 // be immediately following. However, for the IRIX6 ABI, the next 8748 // relocation may be a composed relocation consisting of several 8749 // relocations for the same address. In that case, the R_MIPS_LO16 8750 // relocation may occur as one of these. We permit a similar 8751 // extension in general, as that is useful for GCC. 8752 8753 // In some cases GCC dead code elimination removes the LO16 but 8754 // keeps the corresponding HI16. This is strictly speaking a 8755 // violation of the ABI but not immediately harmful. 8756 8757 typename std::list<got16_addend<size, big_endian> >::iterator it = 8758 target->got16_addends_.begin(); 8759 while (it != target->got16_addends_.end()) 8760 { 8761 got16_addend<size, big_endian> _got16_addend = *it; 8762 8763 // TODO(sasa): Split got16_addends_ list into two lists - one for 8764 // GOT16 relocs and the other for HI16 relocs. 8765 8766 // Report an error if we find HI16 or GOT16 reloc from the 8767 // previous section without the matching LO16 part. 8768 if (_got16_addend.object != object 8769 || _got16_addend.shndx != data_shndx) 8770 { 8771 gold_error("Can't find matching LO16 reloc"); 8772 break; 8773 } 8774 8775 if (_got16_addend.r_sym != r_sym 8776 || !is_matching_lo16_reloc(_got16_addend.r_type, r_type)) 8777 { 8778 ++it; 8779 continue; 8780 } 8781 8782 // We found a matching HI16 or GOT16 reloc for this LO16 reloc. 8783 // For GOT16, we need to calculate combined addend and record GOT page 8784 // entry. 8785 if (got16_reloc(_got16_addend.r_type)) 8786 { 8787 8788 section_size_type view_size = 0; 8789 const unsigned char* view = object->section_contents(data_shndx, 8790 &view_size, 8791 false); 8792 view += r_offset; 8793 8794 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view); 8795 int32_t addend = Bits<16>::sign_extend32(val & 0xffff); 8796 8797 addend = (_got16_addend.addend << 16) + addend; 8798 target->got_section()->record_got_page_entry(mips_obj, r_sym, 8799 addend); 8800 } 8801 8802 it = target->got16_addends_.erase(it); 8803 } 8804 break; 8805 } 8806 } 8807 8808 switch (r_type) 8809 { 8810 case elfcpp::R_MIPS_32: 8811 case elfcpp::R_MIPS_REL32: 8812 case elfcpp::R_MIPS_64: 8813 { 8814 if (parameters->options().output_is_position_independent()) 8815 { 8816 // If building a shared library (or a position-independent 8817 // executable), we need to create a dynamic relocation for 8818 // this location. 8819 Reloc_section* rel_dyn = target->rel_dyn_section(layout); 8820 unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info); 8821 rel_dyn->add_symbolless_local_addend(object, r_sym, 8822 elfcpp::R_MIPS_REL32, 8823 output_section, data_shndx, 8824 r_offset); 8825 } 8826 break; 8827 } 8828 8829 case elfcpp::R_MIPS_TLS_GOTTPREL: 8830 case elfcpp::R_MIPS16_TLS_GOTTPREL: 8831 case elfcpp::R_MICROMIPS_TLS_GOTTPREL: 8832 case elfcpp::R_MIPS_TLS_LDM: 8833 case elfcpp::R_MIPS16_TLS_LDM: 8834 case elfcpp::R_MICROMIPS_TLS_LDM: 8835 case elfcpp::R_MIPS_TLS_GD: 8836 case elfcpp::R_MIPS16_TLS_GD: 8837 case elfcpp::R_MICROMIPS_TLS_GD: 8838 { 8839 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); 8840 bool output_is_shared = parameters->options().shared(); 8841 const tls::Tls_optimization optimized_type 8842 = Target_mips<size, big_endian>::optimize_tls_reloc( 8843 !output_is_shared, r_type); 8844 switch (r_type) 8845 { 8846 case elfcpp::R_MIPS_TLS_GD: 8847 case elfcpp::R_MIPS16_TLS_GD: 8848 case elfcpp::R_MICROMIPS_TLS_GD: 8849 if (optimized_type == tls::TLSOPT_NONE) 8850 { 8851 // Create a pair of GOT entries for the module index and 8852 // dtv-relative offset. 8853 Mips_output_data_got<size, big_endian>* got = 8854 target->got_section(symtab, layout); 8855 unsigned int shndx = lsym.get_st_shndx(); 8856 bool is_ordinary; 8857 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); 8858 if (!is_ordinary) 8859 { 8860 object->error(_("local symbol %u has bad shndx %u"), 8861 r_sym, shndx); 8862 break; 8863 } 8864 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, 8865 shndx); 8866 } 8867 else 8868 { 8869 // FIXME: TLS optimization not supported yet. 8870 gold_unreachable(); 8871 } 8872 break; 8873 8874 case elfcpp::R_MIPS_TLS_LDM: 8875 case elfcpp::R_MIPS16_TLS_LDM: 8876 case elfcpp::R_MICROMIPS_TLS_LDM: 8877 if (optimized_type == tls::TLSOPT_NONE) 8878 { 8879 // We always record LDM symbols as local with index 0. 8880 target->got_section()->record_local_got_symbol(mips_obj, 0, 8881 r_addend, r_type, 8882 -1U); 8883 } 8884 else 8885 { 8886 // FIXME: TLS optimization not supported yet. 8887 gold_unreachable(); 8888 } 8889 break; 8890 case elfcpp::R_MIPS_TLS_GOTTPREL: 8891 case elfcpp::R_MIPS16_TLS_GOTTPREL: 8892 case elfcpp::R_MICROMIPS_TLS_GOTTPREL: 8893 layout->set_has_static_tls(); 8894 if (optimized_type == tls::TLSOPT_NONE) 8895 { 8896 // Create a GOT entry for the tp-relative offset. 8897 Mips_output_data_got<size, big_endian>* got = 8898 target->got_section(symtab, layout); 8899 got->record_local_got_symbol(mips_obj, r_sym, r_addend, r_type, 8900 -1U); 8901 } 8902 else 8903 { 8904 // FIXME: TLS optimization not supported yet. 8905 gold_unreachable(); 8906 } 8907 break; 8908 8909 default: 8910 gold_unreachable(); 8911 } 8912 } 8913 break; 8914 8915 default: 8916 break; 8917 } 8918 8919 // Refuse some position-dependent relocations when creating a 8920 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're 8921 // not PIC, but we can create dynamic relocations and the result 8922 // will be fine. Also do not refuse R_MIPS_LO16, which can be 8923 // combined with R_MIPS_GOT16. 8924 if (parameters->options().shared()) 8925 { 8926 switch (r_type) 8927 { 8928 case elfcpp::R_MIPS16_HI16: 8929 case elfcpp::R_MIPS_HI16: 8930 case elfcpp::R_MICROMIPS_HI16: 8931 // Don't refuse a high part relocation if it's against 8932 // no symbol (e.g. part of a compound relocation). 8933 if (r_sym == 0) 8934 break; 8935 8936 // FALLTHROUGH 8937 8938 case elfcpp::R_MIPS16_26: 8939 case elfcpp::R_MIPS_26: 8940 case elfcpp::R_MICROMIPS_26_S1: 8941 gold_error(_("%s: relocation %u against `%s' can not be used when " 8942 "making a shared object; recompile with -fPIC"), 8943 object->name().c_str(), r_type, "a local symbol"); 8944 default: 8945 break; 8946 } 8947 } 8948 } 8949 8950 template<int size, bool big_endian> 8951 inline void 8952 Target_mips<size, big_endian>::Scan::local( 8953 Symbol_table* symtab, 8954 Layout* layout, 8955 Target_mips<size, big_endian>* target, 8956 Sized_relobj_file<size, big_endian>* object, 8957 unsigned int data_shndx, 8958 Output_section* output_section, 8959 const elfcpp::Rel<size, big_endian>& reloc, 8960 unsigned int r_type, 8961 const elfcpp::Sym<size, big_endian>& lsym, 8962 bool is_discarded) 8963 { 8964 if (is_discarded) 8965 return; 8966 8967 local( 8968 symtab, 8969 layout, 8970 target, 8971 object, 8972 data_shndx, 8973 output_section, 8974 (const elfcpp::Rela<size, big_endian>*) NULL, 8975 &reloc, 8976 elfcpp::SHT_REL, 8977 r_type, 8978 lsym, is_discarded); 8979 } 8980 8981 8982 template<int size, bool big_endian> 8983 inline void 8984 Target_mips<size, big_endian>::Scan::local( 8985 Symbol_table* symtab, 8986 Layout* layout, 8987 Target_mips<size, big_endian>* target, 8988 Sized_relobj_file<size, big_endian>* object, 8989 unsigned int data_shndx, 8990 Output_section* output_section, 8991 const elfcpp::Rela<size, big_endian>& reloc, 8992 unsigned int r_type, 8993 const elfcpp::Sym<size, big_endian>& lsym, 8994 bool is_discarded) 8995 { 8996 if (is_discarded) 8997 return; 8998 8999 local( 9000 symtab, 9001 layout, 9002 target, 9003 object, 9004 data_shndx, 9005 output_section, 9006 &reloc, 9007 (const elfcpp::Rel<size, big_endian>*) NULL, 9008 elfcpp::SHT_RELA, 9009 r_type, 9010 lsym, is_discarded); 9011 } 9012 9013 // Scan a relocation for a global symbol. 9014 9015 template<int size, bool big_endian> 9016 inline void 9017 Target_mips<size, big_endian>::Scan::global( 9018 Symbol_table* symtab, 9019 Layout* layout, 9020 Target_mips<size, big_endian>* target, 9021 Sized_relobj_file<size, big_endian>* object, 9022 unsigned int data_shndx, 9023 Output_section* output_section, 9024 const elfcpp::Rela<size, big_endian>* rela, 9025 const elfcpp::Rel<size, big_endian>* rel, 9026 unsigned int rel_type, 9027 unsigned int r_type, 9028 Symbol* gsym) 9029 { 9030 Mips_address r_offset; 9031 typename elfcpp::Elf_types<size>::Elf_WXword r_info; 9032 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend; 9033 9034 if (rel_type == elfcpp::SHT_RELA) 9035 { 9036 r_offset = rela->get_r_offset(); 9037 r_info = rela->get_r_info(); 9038 r_addend = rela->get_r_addend(); 9039 } 9040 else 9041 { 9042 r_offset = rel->get_r_offset(); 9043 r_info = rel->get_r_info(); 9044 r_addend = 0; 9045 } 9046 9047 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); 9048 Mips_relobj<size, big_endian>* mips_obj = 9049 Mips_relobj<size, big_endian>::as_mips_relobj(object); 9050 Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym); 9051 9052 if (mips_obj->is_mips16_stub_section(data_shndx)) 9053 { 9054 mips_obj->get_mips16_stub_section(data_shndx) 9055 ->new_global_reloc_found(r_type, mips_sym); 9056 } 9057 9058 if (r_type == elfcpp::R_MIPS_NONE) 9059 // R_MIPS_NONE is used in mips16 stub sections, to define the target of the 9060 // mips16 stub. 9061 return; 9062 9063 if (!mips16_call_reloc(r_type) 9064 && !mips_obj->section_allows_mips16_refs(data_shndx)) 9065 // This reloc would need to refer to a MIPS16 hard-float stub, if 9066 // there is one. We ignore MIPS16 stub sections and .pdr section when 9067 // looking for relocs that would need to refer to MIPS16 stubs. 9068 mips_sym->set_need_fn_stub(); 9069 9070 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got 9071 // section. We check here to avoid creating a dynamic reloc against 9072 // _GLOBAL_OFFSET_TABLE_. 9073 if (!target->has_got_section() 9074 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0) 9075 target->got_section(symtab, layout); 9076 9077 // We need PLT entries if there are static-only relocations against 9078 // an externally-defined function. This can technically occur for 9079 // shared libraries if there are branches to the symbol, although it 9080 // is unlikely that this will be used in practice due to the short 9081 // ranges involved. It can occur for any relative or absolute relocation 9082 // in executables; in that case, the PLT entry becomes the function's 9083 // canonical address. 9084 bool static_reloc = false; 9085 9086 // Set CAN_MAKE_DYNAMIC to true if we can convert this 9087 // relocation into a dynamic one. 9088 bool can_make_dynamic = false; 9089 switch (r_type) 9090 { 9091 case elfcpp::R_MIPS_GOT16: 9092 case elfcpp::R_MIPS_CALL16: 9093 case elfcpp::R_MIPS_CALL_HI16: 9094 case elfcpp::R_MIPS_CALL_LO16: 9095 case elfcpp::R_MIPS_GOT_HI16: 9096 case elfcpp::R_MIPS_GOT_LO16: 9097 case elfcpp::R_MIPS_GOT_PAGE: 9098 case elfcpp::R_MIPS_GOT_OFST: 9099 case elfcpp::R_MIPS_GOT_DISP: 9100 case elfcpp::R_MIPS_TLS_GOTTPREL: 9101 case elfcpp::R_MIPS_TLS_GD: 9102 case elfcpp::R_MIPS_TLS_LDM: 9103 case elfcpp::R_MIPS16_GOT16: 9104 case elfcpp::R_MIPS16_CALL16: 9105 case elfcpp::R_MIPS16_TLS_GOTTPREL: 9106 case elfcpp::R_MIPS16_TLS_GD: 9107 case elfcpp::R_MIPS16_TLS_LDM: 9108 case elfcpp::R_MICROMIPS_GOT16: 9109 case elfcpp::R_MICROMIPS_CALL16: 9110 case elfcpp::R_MICROMIPS_CALL_HI16: 9111 case elfcpp::R_MICROMIPS_CALL_LO16: 9112 case elfcpp::R_MICROMIPS_GOT_HI16: 9113 case elfcpp::R_MICROMIPS_GOT_LO16: 9114 case elfcpp::R_MICROMIPS_GOT_PAGE: 9115 case elfcpp::R_MICROMIPS_GOT_OFST: 9116 case elfcpp::R_MICROMIPS_GOT_DISP: 9117 case elfcpp::R_MICROMIPS_TLS_GOTTPREL: 9118 case elfcpp::R_MICROMIPS_TLS_GD: 9119 case elfcpp::R_MICROMIPS_TLS_LDM: 9120 // We need a GOT section. 9121 target->got_section(symtab, layout); 9122 break; 9123 9124 // This is just a hint; it can safely be ignored. Don't set 9125 // has_static_relocs for the corresponding symbol. 9126 case elfcpp::R_MIPS_JALR: 9127 case elfcpp::R_MICROMIPS_JALR: 9128 break; 9129 9130 case elfcpp::R_MIPS_GPREL16: 9131 case elfcpp::R_MIPS_GPREL32: 9132 case elfcpp::R_MIPS16_GPREL: 9133 case elfcpp::R_MICROMIPS_GPREL16: 9134 // TODO(sasa) 9135 // GP-relative relocations always resolve to a definition in a 9136 // regular input file, ignoring the one-definition rule. This is 9137 // important for the GP setup sequence in NewABI code, which 9138 // always resolves to a local function even if other relocations 9139 // against the symbol wouldn't. 9140 //constrain_symbol_p = FALSE; 9141 break; 9142 9143 case elfcpp::R_MIPS_32: 9144 case elfcpp::R_MIPS_REL32: 9145 case elfcpp::R_MIPS_64: 9146 if (parameters->options().shared() 9147 || strcmp(gsym->name(), "__gnu_local_gp") != 0) 9148 { 9149 if (r_type != elfcpp::R_MIPS_REL32) 9150 { 9151 static_reloc = true; 9152 mips_sym->set_pointer_equality_needed(); 9153 } 9154 can_make_dynamic = true; 9155 break; 9156 } 9157 // Fall through. 9158 9159 default: 9160 // Most static relocations require pointer equality, except 9161 // for branches. 9162 mips_sym->set_pointer_equality_needed(); 9163 9164 // Fall through. 9165 9166 case elfcpp::R_MIPS_26: 9167 case elfcpp::R_MIPS_PC16: 9168 case elfcpp::R_MIPS16_26: 9169 case elfcpp::R_MICROMIPS_26_S1: 9170 case elfcpp::R_MICROMIPS_PC7_S1: 9171 case elfcpp::R_MICROMIPS_PC10_S1: 9172 case elfcpp::R_MICROMIPS_PC16_S1: 9173 case elfcpp::R_MICROMIPS_PC23_S2: 9174 static_reloc = true; 9175 mips_sym->set_has_static_relocs(); 9176 break; 9177 } 9178 9179 // If there are call relocations against an externally-defined symbol, 9180 // see whether we can create a MIPS lazy-binding stub for it. We can 9181 // only do this if all references to the function are through call 9182 // relocations, and in that case, the traditional lazy-binding stubs 9183 // are much more efficient than PLT entries. 9184 switch (r_type) 9185 { 9186 case elfcpp::R_MIPS16_CALL16: 9187 case elfcpp::R_MIPS_CALL16: 9188 case elfcpp::R_MIPS_CALL_HI16: 9189 case elfcpp::R_MIPS_CALL_LO16: 9190 case elfcpp::R_MIPS_JALR: 9191 case elfcpp::R_MICROMIPS_CALL16: 9192 case elfcpp::R_MICROMIPS_CALL_HI16: 9193 case elfcpp::R_MICROMIPS_CALL_LO16: 9194 case elfcpp::R_MICROMIPS_JALR: 9195 if (!mips_sym->no_lazy_stub()) 9196 { 9197 if ((mips_sym->needs_plt_entry() && mips_sym->is_from_dynobj()) 9198 // Calls from shared objects to undefined symbols of type 9199 // STT_NOTYPE need lazy-binding stub. 9200 || (mips_sym->is_undefined() && parameters->options().shared())) 9201 target->mips_stubs_section(layout)->make_entry(mips_sym); 9202 } 9203 break; 9204 default: 9205 { 9206 // We must not create a stub for a symbol that has relocations 9207 // related to taking the function's address. 9208 mips_sym->set_no_lazy_stub(); 9209 target->remove_lazy_stub_entry(mips_sym); 9210 break; 9211 } 9212 } 9213 9214 if (relocation_needs_la25_stub<size, big_endian>(mips_obj, r_type, 9215 mips_sym->is_mips16())) 9216 mips_sym->set_has_nonpic_branches(); 9217 9218 // R_MIPS_HI16 against _gp_disp is used for $gp setup, 9219 // and has a special meaning. 9220 bool gp_disp_against_hi16 = (!mips_obj->is_newabi() 9221 && strcmp(gsym->name(), "_gp_disp") == 0 9222 && (hi16_reloc(r_type) || lo16_reloc(r_type))); 9223 if (static_reloc && gsym->needs_plt_entry()) 9224 { 9225 target->make_plt_entry(symtab, layout, mips_sym, r_type); 9226 9227 // Since this is not a PC-relative relocation, we may be 9228 // taking the address of a function. In that case we need to 9229 // set the entry in the dynamic symbol table to the address of 9230 // the PLT entry. 9231 if (gsym->is_from_dynobj() && !parameters->options().shared()) 9232 { 9233 gsym->set_needs_dynsym_value(); 9234 // We distinguish between PLT entries and lazy-binding stubs by 9235 // giving the former an st_other value of STO_MIPS_PLT. Set the 9236 // flag if there are any relocations in the binary where pointer 9237 // equality matters. 9238 if (mips_sym->pointer_equality_needed()) 9239 mips_sym->set_mips_plt(); 9240 } 9241 } 9242 if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16) 9243 { 9244 // Absolute addressing relocations. 9245 // Make a dynamic relocation if necessary. 9246 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))) 9247 { 9248 if (gsym->may_need_copy_reloc()) 9249 { 9250 target->copy_reloc(symtab, layout, object, 9251 data_shndx, output_section, gsym, *rel); 9252 } 9253 else if (can_make_dynamic) 9254 { 9255 // Create .rel.dyn section. 9256 target->rel_dyn_section(layout); 9257 target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32, mips_obj, 9258 data_shndx, output_section, r_offset); 9259 } 9260 else 9261 gold_error(_("non-dynamic relocations refer to dynamic symbol %s"), 9262 gsym->name()); 9263 } 9264 } 9265 9266 bool for_call = false; 9267 switch (r_type) 9268 { 9269 case elfcpp::R_MIPS_CALL16: 9270 case elfcpp::R_MIPS16_CALL16: 9271 case elfcpp::R_MICROMIPS_CALL16: 9272 case elfcpp::R_MIPS_CALL_HI16: 9273 case elfcpp::R_MIPS_CALL_LO16: 9274 case elfcpp::R_MICROMIPS_CALL_HI16: 9275 case elfcpp::R_MICROMIPS_CALL_LO16: 9276 for_call = true; 9277 // Fall through. 9278 9279 case elfcpp::R_MIPS16_GOT16: 9280 case elfcpp::R_MIPS_GOT16: 9281 case elfcpp::R_MIPS_GOT_HI16: 9282 case elfcpp::R_MIPS_GOT_LO16: 9283 case elfcpp::R_MICROMIPS_GOT16: 9284 case elfcpp::R_MICROMIPS_GOT_HI16: 9285 case elfcpp::R_MICROMIPS_GOT_LO16: 9286 case elfcpp::R_MIPS_GOT_DISP: 9287 case elfcpp::R_MICROMIPS_GOT_DISP: 9288 { 9289 // The symbol requires a GOT entry. 9290 Mips_output_data_got<size, big_endian>* got = 9291 target->got_section(symtab, layout); 9292 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false, 9293 for_call); 9294 mips_sym->set_global_got_area(GGA_NORMAL); 9295 } 9296 break; 9297 9298 case elfcpp::R_MIPS_GOT_PAGE: 9299 case elfcpp::R_MICROMIPS_GOT_PAGE: 9300 { 9301 // This relocation needs a page entry in the GOT. 9302 // Get the section contents. 9303 section_size_type view_size = 0; 9304 const unsigned char* view = 9305 object->section_contents(data_shndx, &view_size, false); 9306 view += r_offset; 9307 9308 Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view); 9309 Valtype32 addend = (rel_type == elfcpp::SHT_REL ? val & 0xffff 9310 : r_addend); 9311 Mips_output_data_got<size, big_endian>* got = 9312 target->got_section(symtab, layout); 9313 got->record_got_page_entry(mips_obj, r_sym, addend); 9314 9315 // If this is a global, overridable symbol, GOT_PAGE will 9316 // decay to GOT_DISP, so we'll need a GOT entry for it. 9317 bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT 9318 && !mips_sym->object()->is_dynamic() 9319 && !mips_sym->is_undefined()); 9320 if (!def_regular 9321 || (parameters->options().output_is_position_independent() 9322 && !parameters->options().Bsymbolic() 9323 && !mips_sym->is_forced_local())) 9324 { 9325 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false, 9326 for_call); 9327 mips_sym->set_global_got_area(GGA_NORMAL); 9328 } 9329 } 9330 break; 9331 9332 case elfcpp::R_MIPS_TLS_GOTTPREL: 9333 case elfcpp::R_MIPS16_TLS_GOTTPREL: 9334 case elfcpp::R_MICROMIPS_TLS_GOTTPREL: 9335 case elfcpp::R_MIPS_TLS_LDM: 9336 case elfcpp::R_MIPS16_TLS_LDM: 9337 case elfcpp::R_MICROMIPS_TLS_LDM: 9338 case elfcpp::R_MIPS_TLS_GD: 9339 case elfcpp::R_MIPS16_TLS_GD: 9340 case elfcpp::R_MICROMIPS_TLS_GD: 9341 { 9342 const bool is_final = gsym->final_value_is_known(); 9343 const tls::Tls_optimization optimized_type = 9344 Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type); 9345 9346 switch (r_type) 9347 { 9348 case elfcpp::R_MIPS_TLS_GD: 9349 case elfcpp::R_MIPS16_TLS_GD: 9350 case elfcpp::R_MICROMIPS_TLS_GD: 9351 if (optimized_type == tls::TLSOPT_NONE) 9352 { 9353 // Create a pair of GOT entries for the module index and 9354 // dtv-relative offset. 9355 Mips_output_data_got<size, big_endian>* got = 9356 target->got_section(symtab, layout); 9357 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false, 9358 false); 9359 } 9360 else 9361 { 9362 // FIXME: TLS optimization not supported yet. 9363 gold_unreachable(); 9364 } 9365 break; 9366 9367 case elfcpp::R_MIPS_TLS_LDM: 9368 case elfcpp::R_MIPS16_TLS_LDM: 9369 case elfcpp::R_MICROMIPS_TLS_LDM: 9370 if (optimized_type == tls::TLSOPT_NONE) 9371 { 9372 // We always record LDM symbols as local with index 0. 9373 target->got_section()->record_local_got_symbol(mips_obj, 0, 9374 r_addend, r_type, 9375 -1U); 9376 } 9377 else 9378 { 9379 // FIXME: TLS optimization not supported yet. 9380 gold_unreachable(); 9381 } 9382 break; 9383 case elfcpp::R_MIPS_TLS_GOTTPREL: 9384 case elfcpp::R_MIPS16_TLS_GOTTPREL: 9385 case elfcpp::R_MICROMIPS_TLS_GOTTPREL: 9386 layout->set_has_static_tls(); 9387 if (optimized_type == tls::TLSOPT_NONE) 9388 { 9389 // Create a GOT entry for the tp-relative offset. 9390 Mips_output_data_got<size, big_endian>* got = 9391 target->got_section(symtab, layout); 9392 got->record_global_got_symbol(mips_sym, mips_obj, r_type, false, 9393 false); 9394 } 9395 else 9396 { 9397 // FIXME: TLS optimization not supported yet. 9398 gold_unreachable(); 9399 } 9400 break; 9401 9402 default: 9403 gold_unreachable(); 9404 } 9405 } 9406 break; 9407 case elfcpp::R_MIPS_COPY: 9408 case elfcpp::R_MIPS_JUMP_SLOT: 9409 // These are relocations which should only be seen by the 9410 // dynamic linker, and should never be seen here. 9411 gold_error(_("%s: unexpected reloc %u in object file"), 9412 object->name().c_str(), r_type); 9413 break; 9414 9415 default: 9416 break; 9417 } 9418 9419 // Refuse some position-dependent relocations when creating a 9420 // shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're 9421 // not PIC, but we can create dynamic relocations and the result 9422 // will be fine. Also do not refuse R_MIPS_LO16, which can be 9423 // combined with R_MIPS_GOT16. 9424 if (parameters->options().shared()) 9425 { 9426 switch (r_type) 9427 { 9428 case elfcpp::R_MIPS16_HI16: 9429 case elfcpp::R_MIPS_HI16: 9430 case elfcpp::R_MICROMIPS_HI16: 9431 // Don't refuse a high part relocation if it's against 9432 // no symbol (e.g. part of a compound relocation). 9433 if (r_sym == 0) 9434 break; 9435 9436 // R_MIPS_HI16 against _gp_disp is used for $gp setup, 9437 // and has a special meaning. 9438 if (!mips_obj->is_newabi() && strcmp(gsym->name(), "_gp_disp") == 0) 9439 break; 9440 9441 // FALLTHROUGH 9442 9443 case elfcpp::R_MIPS16_26: 9444 case elfcpp::R_MIPS_26: 9445 case elfcpp::R_MICROMIPS_26_S1: 9446 gold_error(_("%s: relocation %u against `%s' can not be used when " 9447 "making a shared object; recompile with -fPIC"), 9448 object->name().c_str(), r_type, gsym->name()); 9449 default: 9450 break; 9451 } 9452 } 9453 } 9454 9455 template<int size, bool big_endian> 9456 inline void 9457 Target_mips<size, big_endian>::Scan::global( 9458 Symbol_table* symtab, 9459 Layout* layout, 9460 Target_mips<size, big_endian>* target, 9461 Sized_relobj_file<size, big_endian>* object, 9462 unsigned int data_shndx, 9463 Output_section* output_section, 9464 const elfcpp::Rela<size, big_endian>& reloc, 9465 unsigned int r_type, 9466 Symbol* gsym) 9467 { 9468 global( 9469 symtab, 9470 layout, 9471 target, 9472 object, 9473 data_shndx, 9474 output_section, 9475 &reloc, 9476 (const elfcpp::Rel<size, big_endian>*) NULL, 9477 elfcpp::SHT_RELA, 9478 r_type, 9479 gsym); 9480 } 9481 9482 template<int size, bool big_endian> 9483 inline void 9484 Target_mips<size, big_endian>::Scan::global( 9485 Symbol_table* symtab, 9486 Layout* layout, 9487 Target_mips<size, big_endian>* target, 9488 Sized_relobj_file<size, big_endian>* object, 9489 unsigned int data_shndx, 9490 Output_section* output_section, 9491 const elfcpp::Rel<size, big_endian>& reloc, 9492 unsigned int r_type, 9493 Symbol* gsym) 9494 { 9495 global( 9496 symtab, 9497 layout, 9498 target, 9499 object, 9500 data_shndx, 9501 output_section, 9502 (const elfcpp::Rela<size, big_endian>*) NULL, 9503 &reloc, 9504 elfcpp::SHT_REL, 9505 r_type, 9506 gsym); 9507 } 9508 9509 // Return whether a R_MIPS_32 relocation needs to be applied. 9510 9511 template<int size, bool big_endian> 9512 inline bool 9513 Target_mips<size, big_endian>::Relocate::should_apply_r_mips_32_reloc( 9514 const Mips_symbol<size>* gsym, 9515 unsigned int r_type, 9516 Output_section* output_section, 9517 Target_mips* target) 9518 { 9519 // If the output section is not allocated, then we didn't call 9520 // scan_relocs, we didn't create a dynamic reloc, and we must apply 9521 // the reloc here. 9522 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0) 9523 return true; 9524 9525 if (gsym == NULL) 9526 return true; 9527 else 9528 { 9529 // For global symbols, we use the same helper routines used in the 9530 // scan pass. 9531 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)) 9532 && !gsym->may_need_copy_reloc()) 9533 { 9534 // We have generated dynamic reloc (R_MIPS_REL32). 9535 9536 bool multi_got = false; 9537 if (target->has_got_section()) 9538 multi_got = target->got_section()->multi_got(); 9539 bool has_got_offset; 9540 if (!multi_got) 9541 has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD); 9542 else 9543 has_got_offset = gsym->global_gotoffset() != -1U; 9544 if (!has_got_offset) 9545 return true; 9546 else 9547 // Apply the relocation only if the symbol is in the local got. 9548 // Do not apply the relocation if the symbol is in the global 9549 // got. 9550 return symbol_references_local(gsym, gsym->has_dynsym_index()); 9551 } 9552 else 9553 // We have not generated dynamic reloc. 9554 return true; 9555 } 9556 } 9557 9558 // Perform a relocation. 9559 9560 template<int size, bool big_endian> 9561 inline bool 9562 Target_mips<size, big_endian>::Relocate::relocate( 9563 const Relocate_info<size, big_endian>* relinfo, 9564 Target_mips* target, 9565 Output_section* output_section, 9566 size_t relnum, 9567 const elfcpp::Rela<size, big_endian>* rela, 9568 const elfcpp::Rel<size, big_endian>* rel, 9569 unsigned int rel_type, 9570 unsigned int r_type, 9571 const Sized_symbol<size>* gsym, 9572 const Symbol_value<size>* psymval, 9573 unsigned char* view, 9574 Mips_address address, 9575 section_size_type) 9576 { 9577 Mips_address r_offset; 9578 typename elfcpp::Elf_types<size>::Elf_WXword r_info; 9579 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend; 9580 9581 if (rel_type == elfcpp::SHT_RELA) 9582 { 9583 r_offset = rela->get_r_offset(); 9584 r_info = rela->get_r_info(); 9585 r_addend = rela->get_r_addend(); 9586 } 9587 else 9588 { 9589 r_offset = rel->get_r_offset(); 9590 r_info = rel->get_r_info(); 9591 r_addend = 0; 9592 } 9593 9594 typedef Mips_relocate_functions<size, big_endian> Reloc_funcs; 9595 typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY; 9596 9597 Mips_relobj<size, big_endian>* object = 9598 Mips_relobj<size, big_endian>::as_mips_relobj(relinfo->object); 9599 9600 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); 9601 bool target_is_16_bit_code = false; 9602 bool target_is_micromips_code = false; 9603 bool cross_mode_jump; 9604 9605 Symbol_value<size> symval; 9606 9607 const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym); 9608 9609 bool changed_symbol_value = false; 9610 if (gsym == NULL) 9611 { 9612 target_is_16_bit_code = object->local_symbol_is_mips16(r_sym); 9613 target_is_micromips_code = object->local_symbol_is_micromips(r_sym); 9614 if (target_is_16_bit_code || target_is_micromips_code) 9615 { 9616 // MIPS16/microMIPS text labels should be treated as odd. 9617 symval.set_output_value(psymval->value(object, 1)); 9618 psymval = &symval; 9619 changed_symbol_value = true; 9620 } 9621 } 9622 else 9623 { 9624 target_is_16_bit_code = mips_sym->is_mips16(); 9625 target_is_micromips_code = mips_sym->is_micromips(); 9626 9627 // If this is a mips16/microMIPS text symbol, add 1 to the value to make 9628 // it odd. This will cause something like .word SYM to come up with 9629 // the right value when it is loaded into the PC. 9630 9631 if ((mips_sym->is_mips16() || mips_sym->is_micromips()) 9632 && psymval->value(object, 0) != 0) 9633 { 9634 symval.set_output_value(psymval->value(object, 0) | 1); 9635 psymval = &symval; 9636 changed_symbol_value = true; 9637 } 9638 9639 // Pick the value to use for symbols defined in shared objects. 9640 if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type)) 9641 || mips_sym->has_lazy_stub()) 9642 { 9643 Mips_address value; 9644 if (!mips_sym->has_lazy_stub()) 9645 { 9646 // Prefer a standard MIPS PLT entry. 9647 if (mips_sym->has_mips_plt_offset()) 9648 { 9649 value = target->plt_section()->mips_entry_address(mips_sym); 9650 target_is_micromips_code = false; 9651 target_is_16_bit_code = false; 9652 } 9653 else 9654 { 9655 value = (target->plt_section()->comp_entry_address(mips_sym) 9656 + 1); 9657 if (target->is_output_micromips()) 9658 target_is_micromips_code = true; 9659 else 9660 target_is_16_bit_code = true; 9661 } 9662 } 9663 else 9664 value = target->mips_stubs_section()->stub_address(mips_sym); 9665 9666 symval.set_output_value(value); 9667 psymval = &symval; 9668 } 9669 } 9670 9671 // TRUE if the symbol referred to by this relocation is "_gp_disp". 9672 // Note that such a symbol must always be a global symbol. 9673 bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0) 9674 && !object->is_newabi()); 9675 9676 // TRUE if the symbol referred to by this relocation is "__gnu_local_gp". 9677 // Note that such a symbol must always be a global symbol. 9678 bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0); 9679 9680 9681 if (gp_disp) 9682 { 9683 if (!hi16_reloc(r_type) && !lo16_reloc(r_type)) 9684 gold_error_at_location(relinfo, relnum, r_offset, 9685 _("relocations against _gp_disp are permitted only" 9686 " with R_MIPS_HI16 and R_MIPS_LO16 relocations.")); 9687 } 9688 else if (gnu_local_gp) 9689 { 9690 // __gnu_local_gp is _gp symbol. 9691 symval.set_output_value(target->adjusted_gp_value(object)); 9692 psymval = &symval; 9693 } 9694 9695 // If this is a reference to a 16-bit function with a stub, we need 9696 // to redirect the relocation to the stub unless: 9697 // 9698 // (a) the relocation is for a MIPS16 JAL; 9699 // 9700 // (b) the relocation is for a MIPS16 PIC call, and there are no 9701 // non-MIPS16 uses of the GOT slot; or 9702 // 9703 // (c) the section allows direct references to MIPS16 functions. 9704 if (r_type != elfcpp::R_MIPS16_26 9705 && !parameters->options().relocatable() 9706 && ((mips_sym != NULL 9707 && mips_sym->has_mips16_fn_stub() 9708 && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub())) 9709 || (mips_sym == NULL 9710 && object->get_local_mips16_fn_stub(r_sym) != NULL)) 9711 && !object->section_allows_mips16_refs(relinfo->data_shndx)) 9712 { 9713 // This is a 32- or 64-bit call to a 16-bit function. We should 9714 // have already noticed that we were going to need the 9715 // stub. 9716 Mips_address value; 9717 if (mips_sym == NULL) 9718 value = object->get_local_mips16_fn_stub(r_sym)->output_address(); 9719 else 9720 { 9721 gold_assert(mips_sym->need_fn_stub()); 9722 if (mips_sym->has_la25_stub()) 9723 value = target->la25_stub_section()->stub_address(mips_sym); 9724 else 9725 { 9726 value = mips_sym->template 9727 get_mips16_fn_stub<big_endian>()->output_address(); 9728 } 9729 } 9730 symval.set_output_value(value); 9731 psymval = &symval; 9732 changed_symbol_value = true; 9733 9734 // The target is 16-bit, but the stub isn't. 9735 target_is_16_bit_code = false; 9736 } 9737 // If this is a MIPS16 call with a stub, that is made through the PLT or 9738 // to a standard MIPS function, we need to redirect the call to the stub. 9739 // Note that we specifically exclude R_MIPS16_CALL16 from this behavior; 9740 // indirect calls should use an indirect stub instead. 9741 else if (r_type == elfcpp::R_MIPS16_26 && !parameters->options().relocatable() 9742 && ((mips_sym != NULL 9743 && (mips_sym->has_mips16_call_stub() 9744 || mips_sym->has_mips16_call_fp_stub())) 9745 || (mips_sym == NULL 9746 && object->get_local_mips16_call_stub(r_sym) != NULL)) 9747 && ((mips_sym != NULL && mips_sym->has_plt_offset()) 9748 || !target_is_16_bit_code)) 9749 { 9750 Mips16_stub_section<size, big_endian>* call_stub; 9751 if (mips_sym == NULL) 9752 call_stub = object->get_local_mips16_call_stub(r_sym); 9753 else 9754 { 9755 // If both call_stub and call_fp_stub are defined, we can figure 9756 // out which one to use by checking which one appears in the input 9757 // file. 9758 if (mips_sym->has_mips16_call_stub() 9759 && mips_sym->has_mips16_call_fp_stub()) 9760 { 9761 call_stub = NULL; 9762 for (unsigned int i = 1; i < object->shnum(); ++i) 9763 { 9764 if (object->is_mips16_call_fp_stub_section(i)) 9765 { 9766 call_stub = mips_sym->template 9767 get_mips16_call_fp_stub<big_endian>(); 9768 break; 9769 } 9770 9771 } 9772 if (call_stub == NULL) 9773 call_stub = 9774 mips_sym->template get_mips16_call_stub<big_endian>(); 9775 } 9776 else if (mips_sym->has_mips16_call_stub()) 9777 call_stub = mips_sym->template get_mips16_call_stub<big_endian>(); 9778 else 9779 call_stub = mips_sym->template get_mips16_call_fp_stub<big_endian>(); 9780 } 9781 9782 symval.set_output_value(call_stub->output_address()); 9783 psymval = &symval; 9784 changed_symbol_value = true; 9785 } 9786 // If this is a direct call to a PIC function, redirect to the 9787 // non-PIC stub. 9788 else if (mips_sym != NULL 9789 && mips_sym->has_la25_stub() 9790 && relocation_needs_la25_stub<size, big_endian>( 9791 object, r_type, target_is_16_bit_code)) 9792 { 9793 Mips_address value = target->la25_stub_section()->stub_address(mips_sym); 9794 if (mips_sym->is_micromips()) 9795 value += 1; 9796 symval.set_output_value(value); 9797 psymval = &symval; 9798 } 9799 // For direct MIPS16 and microMIPS calls make sure the compressed PLT 9800 // entry is used if a standard PLT entry has also been made. 9801 else if ((r_type == elfcpp::R_MIPS16_26 9802 || r_type == elfcpp::R_MICROMIPS_26_S1) 9803 && !parameters->options().relocatable() 9804 && mips_sym != NULL 9805 && mips_sym->has_plt_offset() 9806 && mips_sym->has_comp_plt_offset() 9807 && mips_sym->has_mips_plt_offset()) 9808 { 9809 Mips_address value = (target->plt_section()->comp_entry_address(mips_sym) 9810 + 1); 9811 symval.set_output_value(value); 9812 psymval = &symval; 9813 9814 target_is_16_bit_code = !target->is_output_micromips(); 9815 target_is_micromips_code = target->is_output_micromips(); 9816 } 9817 9818 // Make sure MIPS16 and microMIPS are not used together. 9819 if ((r_type == elfcpp::R_MIPS16_26 && target_is_micromips_code) 9820 || (micromips_branch_reloc(r_type) && target_is_16_bit_code)) 9821 { 9822 gold_error(_("MIPS16 and microMIPS functions cannot call each other")); 9823 } 9824 9825 // Calls from 16-bit code to 32-bit code and vice versa require the 9826 // mode change. However, we can ignore calls to undefined weak symbols, 9827 // which should never be executed at runtime. This exception is important 9828 // because the assembly writer may have "known" that any definition of the 9829 // symbol would be 16-bit code, and that direct jumps were therefore 9830 // acceptable. 9831 cross_mode_jump = 9832 (!parameters->options().relocatable() 9833 && !(gsym != NULL && gsym->is_weak_undefined()) 9834 && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code) 9835 || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code) 9836 || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR) 9837 && (target_is_16_bit_code || target_is_micromips_code)))); 9838 9839 bool local = (mips_sym == NULL 9840 || (mips_sym->got_only_for_calls() 9841 ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index()) 9842 : symbol_references_local(mips_sym, 9843 mips_sym->has_dynsym_index()))); 9844 9845 // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent 9846 // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the 9847 // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. 9848 if (got_page_reloc(r_type) && !local) 9849 r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP 9850 : elfcpp::R_MIPS_GOT_DISP); 9851 9852 unsigned int got_offset = 0; 9853 int gp_offset = 0; 9854 9855 bool update_got_entry = false; 9856 bool extract_addend = rel_type == elfcpp::SHT_REL; 9857 switch (r_type) 9858 { 9859 case elfcpp::R_MIPS_NONE: 9860 break; 9861 case elfcpp::R_MIPS_16: 9862 reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend, 9863 extract_addend, r_type); 9864 break; 9865 9866 case elfcpp::R_MIPS_32: 9867 if (should_apply_r_mips_32_reloc(mips_sym, r_type, output_section, 9868 target)) 9869 reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend, 9870 extract_addend, r_type); 9871 if (mips_sym != NULL 9872 && (mips_sym->is_mips16() || mips_sym->is_micromips()) 9873 && mips_sym->global_got_area() == GGA_RELOC_ONLY) 9874 { 9875 // If mips_sym->has_mips16_fn_stub() is false, symbol value is 9876 // already updated by adding +1. 9877 if (mips_sym->has_mips16_fn_stub()) 9878 { 9879 gold_assert(mips_sym->need_fn_stub()); 9880 Mips16_stub_section<size, big_endian>* fn_stub = 9881 mips_sym->template get_mips16_fn_stub<big_endian>(); 9882 9883 symval.set_output_value(fn_stub->output_address()); 9884 psymval = &symval; 9885 } 9886 got_offset = mips_sym->global_gotoffset(); 9887 update_got_entry = true; 9888 } 9889 break; 9890 9891 case elfcpp::R_MIPS_REL32: 9892 gold_unreachable(); 9893 9894 case elfcpp::R_MIPS_PC32: 9895 reloc_status = Reloc_funcs::relpc32(view, object, psymval, address, 9896 r_addend, extract_addend, r_type); 9897 break; 9898 9899 case elfcpp::R_MIPS16_26: 9900 // The calculation for R_MIPS16_26 is just the same as for an 9901 // R_MIPS_26. It's only the storage of the relocated field into 9902 // the output file that's different. So, we just fall through to the 9903 // R_MIPS_26 case here. 9904 case elfcpp::R_MIPS_26: 9905 case elfcpp::R_MICROMIPS_26_S1: 9906 reloc_status = Reloc_funcs::rel26(view, object, psymval, address, 9907 gsym == NULL, r_addend, extract_addend, gsym, cross_mode_jump, r_type, 9908 target->jal_to_bal()); 9909 break; 9910 9911 case elfcpp::R_MIPS_HI16: 9912 case elfcpp::R_MIPS16_HI16: 9913 case elfcpp::R_MICROMIPS_HI16: 9914 reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend, 9915 address, gp_disp, r_type, r_sym, 9916 extract_addend); 9917 break; 9918 9919 case elfcpp::R_MIPS_LO16: 9920 case elfcpp::R_MIPS16_LO16: 9921 case elfcpp::R_MICROMIPS_LO16: 9922 case elfcpp::R_MICROMIPS_HI0_LO16: 9923 reloc_status = Reloc_funcs::rello16(target, view, object, psymval, 9924 r_addend, extract_addend, address, 9925 gp_disp, r_type, r_sym); 9926 break; 9927 9928 case elfcpp::R_MIPS_LITERAL: 9929 case elfcpp::R_MICROMIPS_LITERAL: 9930 // Because we don't merge literal sections, we can handle this 9931 // just like R_MIPS_GPREL16. In the long run, we should merge 9932 // shared literals, and then we will need to additional work 9933 // here. 9934 9935 // Fall through. 9936 9937 case elfcpp::R_MIPS_GPREL16: 9938 case elfcpp::R_MIPS16_GPREL: 9939 case elfcpp::R_MICROMIPS_GPREL7_S2: 9940 case elfcpp::R_MICROMIPS_GPREL16: 9941 reloc_status = Reloc_funcs::relgprel(view, object, psymval, 9942 target->adjusted_gp_value(object), 9943 r_addend, extract_addend, 9944 gsym == NULL, r_type); 9945 break; 9946 9947 case elfcpp::R_MIPS_PC16: 9948 reloc_status = Reloc_funcs::relpc16(view, object, psymval, address, 9949 r_addend, extract_addend, r_type); 9950 break; 9951 case elfcpp::R_MICROMIPS_PC7_S1: 9952 reloc_status = Reloc_funcs::relmicromips_pc7_s1(view, object, psymval, 9953 address, r_addend, 9954 extract_addend, r_type); 9955 break; 9956 case elfcpp::R_MICROMIPS_PC10_S1: 9957 reloc_status = Reloc_funcs::relmicromips_pc10_s1(view, object, psymval, 9958 address, r_addend, 9959 extract_addend, r_type); 9960 break; 9961 case elfcpp::R_MICROMIPS_PC16_S1: 9962 reloc_status = Reloc_funcs::relmicromips_pc16_s1(view, object, psymval, 9963 address, r_addend, 9964 extract_addend, r_type); 9965 break; 9966 case elfcpp::R_MIPS_GPREL32: 9967 reloc_status = Reloc_funcs::relgprel32(view, object, psymval, 9968 target->adjusted_gp_value(object), 9969 r_addend, extract_addend, r_type); 9970 break; 9971 case elfcpp::R_MIPS_GOT_HI16: 9972 case elfcpp::R_MIPS_CALL_HI16: 9973 case elfcpp::R_MICROMIPS_GOT_HI16: 9974 case elfcpp::R_MICROMIPS_CALL_HI16: 9975 if (gsym != NULL) 9976 got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD, 9977 object); 9978 else 9979 got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_STANDARD, 9980 object); 9981 gp_offset = target->got_section()->gp_offset(got_offset, object); 9982 reloc_status = Reloc_funcs::relgot_hi16(view, gp_offset, r_type); 9983 update_got_entry = changed_symbol_value; 9984 break; 9985 9986 case elfcpp::R_MIPS_GOT_LO16: 9987 case elfcpp::R_MIPS_CALL_LO16: 9988 case elfcpp::R_MICROMIPS_GOT_LO16: 9989 case elfcpp::R_MICROMIPS_CALL_LO16: 9990 if (gsym != NULL) 9991 got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD, 9992 object); 9993 else 9994 got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_STANDARD, 9995 object); 9996 gp_offset = target->got_section()->gp_offset(got_offset, object); 9997 reloc_status = Reloc_funcs::relgot_lo16(view, gp_offset, r_type); 9998 update_got_entry = changed_symbol_value; 9999 break; 10000 10001 case elfcpp::R_MIPS_GOT_DISP: 10002 case elfcpp::R_MICROMIPS_GOT_DISP: 10003 if (gsym != NULL) 10004 got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD, 10005 object); 10006 else 10007 got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_STANDARD, 10008 object); 10009 gp_offset = target->got_section()->gp_offset(got_offset, object); 10010 reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type); 10011 break; 10012 10013 case elfcpp::R_MIPS_CALL16: 10014 case elfcpp::R_MIPS16_CALL16: 10015 case elfcpp::R_MICROMIPS_CALL16: 10016 gold_assert(gsym != NULL); 10017 got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD, 10018 object); 10019 gp_offset = target->got_section()->gp_offset(got_offset, object); 10020 reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type); 10021 // TODO(sasa): We should also initialize update_got_entry in other places 10022 // where relgot is called. 10023 update_got_entry = changed_symbol_value; 10024 break; 10025 10026 case elfcpp::R_MIPS_GOT16: 10027 case elfcpp::R_MIPS16_GOT16: 10028 case elfcpp::R_MICROMIPS_GOT16: 10029 if (gsym != NULL) 10030 { 10031 got_offset = target->got_section()->got_offset(gsym, 10032 GOT_TYPE_STANDARD, 10033 object); 10034 gp_offset = target->got_section()->gp_offset(got_offset, object); 10035 reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type); 10036 } 10037 else 10038 reloc_status = Reloc_funcs::relgot16_local(view, object, psymval, 10039 r_addend, extract_addend, 10040 r_type, r_sym); 10041 update_got_entry = changed_symbol_value; 10042 break; 10043 10044 case elfcpp::R_MIPS_TLS_GD: 10045 case elfcpp::R_MIPS16_TLS_GD: 10046 case elfcpp::R_MICROMIPS_TLS_GD: 10047 if (gsym != NULL) 10048 got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_TLS_PAIR, 10049 object); 10050 else 10051 got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_TLS_PAIR, 10052 object); 10053 gp_offset = target->got_section()->gp_offset(got_offset, object); 10054 reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type); 10055 break; 10056 10057 case elfcpp::R_MIPS_TLS_GOTTPREL: 10058 case elfcpp::R_MIPS16_TLS_GOTTPREL: 10059 case elfcpp::R_MICROMIPS_TLS_GOTTPREL: 10060 if (gsym != NULL) 10061 got_offset = target->got_section()->got_offset(gsym, 10062 GOT_TYPE_TLS_OFFSET, 10063 object); 10064 else 10065 got_offset = target->got_section()->got_offset(r_sym, 10066 GOT_TYPE_TLS_OFFSET, 10067 object); 10068 gp_offset = target->got_section()->gp_offset(got_offset, object); 10069 reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type); 10070 break; 10071 10072 case elfcpp::R_MIPS_TLS_LDM: 10073 case elfcpp::R_MIPS16_TLS_LDM: 10074 case elfcpp::R_MICROMIPS_TLS_LDM: 10075 // Relocate the field with the offset of the GOT entry for 10076 // the module index. 10077 got_offset = target->got_section()->tls_ldm_offset(object); 10078 gp_offset = target->got_section()->gp_offset(got_offset, object); 10079 reloc_status = Reloc_funcs::relgot(view, gp_offset, r_type); 10080 break; 10081 10082 case elfcpp::R_MIPS_GOT_PAGE: 10083 case elfcpp::R_MICROMIPS_GOT_PAGE: 10084 reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval, 10085 r_addend, extract_addend, r_type); 10086 break; 10087 10088 case elfcpp::R_MIPS_GOT_OFST: 10089 case elfcpp::R_MICROMIPS_GOT_OFST: 10090 reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval, 10091 r_addend, extract_addend, local, 10092 r_type); 10093 break; 10094 10095 case elfcpp::R_MIPS_JALR: 10096 case elfcpp::R_MICROMIPS_JALR: 10097 // This relocation is only a hint. In some cases, we optimize 10098 // it into a bal instruction. But we don't try to optimize 10099 // when the symbol does not resolve locally. 10100 if (gsym == NULL || symbol_calls_local(gsym, gsym->has_dynsym_index())) 10101 reloc_status = Reloc_funcs::reljalr(view, object, psymval, address, 10102 r_addend, extract_addend, 10103 cross_mode_jump, r_type, 10104 target->jalr_to_bal(), 10105 target->jr_to_b()); 10106 break; 10107 10108 case elfcpp::R_MIPS_TLS_DTPREL_HI16: 10109 case elfcpp::R_MIPS16_TLS_DTPREL_HI16: 10110 case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16: 10111 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval, 10112 elfcpp::DTP_OFFSET, r_addend, 10113 extract_addend, r_type); 10114 break; 10115 case elfcpp::R_MIPS_TLS_DTPREL_LO16: 10116 case elfcpp::R_MIPS16_TLS_DTPREL_LO16: 10117 case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16: 10118 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval, 10119 elfcpp::DTP_OFFSET, r_addend, 10120 extract_addend, r_type); 10121 break; 10122 case elfcpp::R_MIPS_TLS_DTPREL32: 10123 case elfcpp::R_MIPS_TLS_DTPREL64: 10124 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval, 10125 elfcpp::DTP_OFFSET, r_addend, 10126 extract_addend, r_type); 10127 break; 10128 case elfcpp::R_MIPS_TLS_TPREL_HI16: 10129 case elfcpp::R_MIPS16_TLS_TPREL_HI16: 10130 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16: 10131 reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval, 10132 elfcpp::TP_OFFSET, r_addend, 10133 extract_addend, r_type); 10134 break; 10135 case elfcpp::R_MIPS_TLS_TPREL_LO16: 10136 case elfcpp::R_MIPS16_TLS_TPREL_LO16: 10137 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16: 10138 reloc_status = Reloc_funcs::tlsrello16(view, object, psymval, 10139 elfcpp::TP_OFFSET, r_addend, 10140 extract_addend, r_type); 10141 break; 10142 case elfcpp::R_MIPS_TLS_TPREL32: 10143 case elfcpp::R_MIPS_TLS_TPREL64: 10144 reloc_status = Reloc_funcs::tlsrel32(view, object, psymval, 10145 elfcpp::TP_OFFSET, r_addend, 10146 extract_addend, r_type); 10147 break; 10148 case elfcpp::R_MIPS_SUB: 10149 case elfcpp::R_MICROMIPS_SUB: 10150 reloc_status = Reloc_funcs::relsub(view, object, psymval, r_addend, 10151 extract_addend, r_type); 10152 break; 10153 default: 10154 gold_error_at_location(relinfo, relnum, r_offset, 10155 _("unsupported reloc %u"), r_type); 10156 break; 10157 } 10158 10159 if (update_got_entry) 10160 { 10161 Mips_output_data_got<size, big_endian>* got = target->got_section(); 10162 if (mips_sym != NULL && mips_sym->get_applied_secondary_got_fixup()) 10163 got->update_got_entry(got->get_primary_got_offset(mips_sym), 10164 psymval->value(object, 0)); 10165 else 10166 got->update_got_entry(got_offset, psymval->value(object, 0)); 10167 } 10168 10169 // Report any errors. 10170 switch (reloc_status) 10171 { 10172 case Reloc_funcs::STATUS_OKAY: 10173 break; 10174 case Reloc_funcs::STATUS_OVERFLOW: 10175 gold_error_at_location(relinfo, relnum, r_offset, 10176 _("relocation overflow")); 10177 break; 10178 case Reloc_funcs::STATUS_BAD_RELOC: 10179 gold_error_at_location(relinfo, relnum, r_offset, 10180 _("unexpected opcode while processing relocation")); 10181 break; 10182 default: 10183 gold_unreachable(); 10184 } 10185 10186 return true; 10187 } 10188 10189 template<int size, bool big_endian> 10190 inline bool 10191 Target_mips<size, big_endian>::Relocate::relocate( 10192 const Relocate_info<size, big_endian>* relinfo, 10193 Target_mips* target, 10194 Output_section* output_section, 10195 size_t relnum, 10196 const elfcpp::Rela<size, big_endian>& reloc, 10197 unsigned int r_type, 10198 const Sized_symbol<size>* gsym, 10199 const Symbol_value<size>* psymval, 10200 unsigned char* view, 10201 Mips_address address, 10202 section_size_type view_size) 10203 { 10204 return relocate( 10205 relinfo, 10206 target, 10207 output_section, 10208 relnum, 10209 &reloc, 10210 (const elfcpp::Rel<size, big_endian>*) NULL, 10211 elfcpp::SHT_RELA, 10212 r_type, 10213 gsym, 10214 psymval, 10215 view, 10216 address, 10217 view_size); 10218 } 10219 10220 template<int size, bool big_endian> 10221 inline bool 10222 Target_mips<size, big_endian>::Relocate::relocate( 10223 const Relocate_info<size, big_endian>* relinfo, 10224 Target_mips* target, 10225 Output_section* output_section, 10226 size_t relnum, 10227 const elfcpp::Rel<size, big_endian>& reloc, 10228 unsigned int r_type, 10229 const Sized_symbol<size>* gsym, 10230 const Symbol_value<size>* psymval, 10231 unsigned char* view, 10232 Mips_address address, 10233 section_size_type view_size) 10234 { 10235 return relocate( 10236 relinfo, 10237 target, 10238 output_section, 10239 relnum, 10240 (const elfcpp::Rela<size, big_endian>*) NULL, 10241 &reloc, 10242 elfcpp::SHT_REL, 10243 r_type, 10244 gsym, 10245 psymval, 10246 view, 10247 address, 10248 view_size); 10249 } 10250 10251 // Get the Reference_flags for a particular relocation. 10252 10253 template<int size, bool big_endian> 10254 int 10255 Target_mips<size, big_endian>::Scan::get_reference_flags( 10256 unsigned int r_type) 10257 { 10258 switch (r_type) 10259 { 10260 case elfcpp::R_MIPS_NONE: 10261 // No symbol reference. 10262 return 0; 10263 10264 case elfcpp::R_MIPS_16: 10265 case elfcpp::R_MIPS_32: 10266 case elfcpp::R_MIPS_64: 10267 case elfcpp::R_MIPS_HI16: 10268 case elfcpp::R_MIPS_LO16: 10269 case elfcpp::R_MIPS16_HI16: 10270 case elfcpp::R_MIPS16_LO16: 10271 case elfcpp::R_MICROMIPS_HI16: 10272 case elfcpp::R_MICROMIPS_LO16: 10273 return Symbol::ABSOLUTE_REF; 10274 10275 case elfcpp::R_MIPS_26: 10276 case elfcpp::R_MIPS16_26: 10277 case elfcpp::R_MICROMIPS_26_S1: 10278 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF; 10279 10280 case elfcpp::R_MIPS_GPREL32: 10281 case elfcpp::R_MIPS_GPREL16: 10282 case elfcpp::R_MIPS_REL32: 10283 case elfcpp::R_MIPS16_GPREL: 10284 return Symbol::RELATIVE_REF; 10285 10286 case elfcpp::R_MIPS_PC16: 10287 case elfcpp::R_MIPS_PC32: 10288 case elfcpp::R_MIPS_JALR: 10289 case elfcpp::R_MICROMIPS_JALR: 10290 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF; 10291 10292 case elfcpp::R_MIPS_GOT16: 10293 case elfcpp::R_MIPS_CALL16: 10294 case elfcpp::R_MIPS_GOT_DISP: 10295 case elfcpp::R_MIPS_GOT_HI16: 10296 case elfcpp::R_MIPS_GOT_LO16: 10297 case elfcpp::R_MIPS_CALL_HI16: 10298 case elfcpp::R_MIPS_CALL_LO16: 10299 case elfcpp::R_MIPS_LITERAL: 10300 case elfcpp::R_MIPS_GOT_PAGE: 10301 case elfcpp::R_MIPS_GOT_OFST: 10302 case elfcpp::R_MIPS16_GOT16: 10303 case elfcpp::R_MIPS16_CALL16: 10304 case elfcpp::R_MICROMIPS_GOT16: 10305 case elfcpp::R_MICROMIPS_CALL16: 10306 case elfcpp::R_MICROMIPS_GOT_HI16: 10307 case elfcpp::R_MICROMIPS_GOT_LO16: 10308 case elfcpp::R_MICROMIPS_CALL_HI16: 10309 case elfcpp::R_MICROMIPS_CALL_LO16: 10310 // Absolute in GOT. 10311 return Symbol::RELATIVE_REF; 10312 10313 case elfcpp::R_MIPS_TLS_DTPMOD32: 10314 case elfcpp::R_MIPS_TLS_DTPREL32: 10315 case elfcpp::R_MIPS_TLS_DTPMOD64: 10316 case elfcpp::R_MIPS_TLS_DTPREL64: 10317 case elfcpp::R_MIPS_TLS_GD: 10318 case elfcpp::R_MIPS_TLS_LDM: 10319 case elfcpp::R_MIPS_TLS_DTPREL_HI16: 10320 case elfcpp::R_MIPS_TLS_DTPREL_LO16: 10321 case elfcpp::R_MIPS_TLS_GOTTPREL: 10322 case elfcpp::R_MIPS_TLS_TPREL32: 10323 case elfcpp::R_MIPS_TLS_TPREL64: 10324 case elfcpp::R_MIPS_TLS_TPREL_HI16: 10325 case elfcpp::R_MIPS_TLS_TPREL_LO16: 10326 case elfcpp::R_MIPS16_TLS_GD: 10327 case elfcpp::R_MIPS16_TLS_GOTTPREL: 10328 case elfcpp::R_MICROMIPS_TLS_GD: 10329 case elfcpp::R_MICROMIPS_TLS_GOTTPREL: 10330 case elfcpp::R_MICROMIPS_TLS_TPREL_HI16: 10331 case elfcpp::R_MICROMIPS_TLS_TPREL_LO16: 10332 return Symbol::TLS_REF; 10333 10334 case elfcpp::R_MIPS_COPY: 10335 case elfcpp::R_MIPS_JUMP_SLOT: 10336 default: 10337 gold_unreachable(); 10338 // Not expected. We will give an error later. 10339 return 0; 10340 } 10341 } 10342 10343 // Report an unsupported relocation against a local symbol. 10344 10345 template<int size, bool big_endian> 10346 void 10347 Target_mips<size, big_endian>::Scan::unsupported_reloc_local( 10348 Sized_relobj_file<size, big_endian>* object, 10349 unsigned int r_type) 10350 { 10351 gold_error(_("%s: unsupported reloc %u against local symbol"), 10352 object->name().c_str(), r_type); 10353 } 10354 10355 // Report an unsupported relocation against a global symbol. 10356 10357 template<int size, bool big_endian> 10358 void 10359 Target_mips<size, big_endian>::Scan::unsupported_reloc_global( 10360 Sized_relobj_file<size, big_endian>* object, 10361 unsigned int r_type, 10362 Symbol* gsym) 10363 { 10364 gold_error(_("%s: unsupported reloc %u against global symbol %s"), 10365 object->name().c_str(), r_type, gsym->demangled_name().c_str()); 10366 } 10367 10368 // Return printable name for ABI. 10369 template<int size, bool big_endian> 10370 const char* 10371 Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags, 10372 unsigned char ei_class) 10373 { 10374 switch (e_flags & elfcpp::EF_MIPS_ABI) 10375 { 10376 case 0: 10377 if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0) 10378 return "N32"; 10379 else if (elfcpp::abi_64(ei_class)) 10380 return "64"; 10381 else 10382 return "none"; 10383 case elfcpp::E_MIPS_ABI_O32: 10384 return "O32"; 10385 case elfcpp::E_MIPS_ABI_O64: 10386 return "O64"; 10387 case elfcpp::E_MIPS_ABI_EABI32: 10388 return "EABI32"; 10389 case elfcpp::E_MIPS_ABI_EABI64: 10390 return "EABI64"; 10391 default: 10392 return "unknown abi"; 10393 } 10394 } 10395 10396 template<int size, bool big_endian> 10397 const char* 10398 Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags) 10399 { 10400 switch (e_flags & elfcpp::EF_MIPS_MACH) 10401 { 10402 case elfcpp::E_MIPS_MACH_3900: 10403 return "mips:3900"; 10404 case elfcpp::E_MIPS_MACH_4010: 10405 return "mips:4010"; 10406 case elfcpp::E_MIPS_MACH_4100: 10407 return "mips:4100"; 10408 case elfcpp::E_MIPS_MACH_4111: 10409 return "mips:4111"; 10410 case elfcpp::E_MIPS_MACH_4120: 10411 return "mips:4120"; 10412 case elfcpp::E_MIPS_MACH_4650: 10413 return "mips:4650"; 10414 case elfcpp::E_MIPS_MACH_5400: 10415 return "mips:5400"; 10416 case elfcpp::E_MIPS_MACH_5500: 10417 return "mips:5500"; 10418 case elfcpp::E_MIPS_MACH_SB1: 10419 return "mips:sb1"; 10420 case elfcpp::E_MIPS_MACH_9000: 10421 return "mips:9000"; 10422 case elfcpp::E_MIPS_MACH_LS2E: 10423 return "mips:loongson-2e"; 10424 case elfcpp::E_MIPS_MACH_LS2F: 10425 return "mips:loongson-2f"; 10426 case elfcpp::E_MIPS_MACH_LS3A: 10427 return "mips:loongson-3a"; 10428 case elfcpp::E_MIPS_MACH_OCTEON: 10429 return "mips:octeon"; 10430 case elfcpp::E_MIPS_MACH_OCTEON2: 10431 return "mips:octeon2"; 10432 case elfcpp::E_MIPS_MACH_XLR: 10433 return "mips:xlr"; 10434 default: 10435 switch (e_flags & elfcpp::EF_MIPS_ARCH) 10436 { 10437 default: 10438 case elfcpp::E_MIPS_ARCH_1: 10439 return "mips:3000"; 10440 10441 case elfcpp::E_MIPS_ARCH_2: 10442 return "mips:6000"; 10443 10444 case elfcpp::E_MIPS_ARCH_3: 10445 return "mips:4000"; 10446 10447 case elfcpp::E_MIPS_ARCH_4: 10448 return "mips:8000"; 10449 10450 case elfcpp::E_MIPS_ARCH_5: 10451 return "mips:mips5"; 10452 10453 case elfcpp::E_MIPS_ARCH_32: 10454 return "mips:isa32"; 10455 10456 case elfcpp::E_MIPS_ARCH_64: 10457 return "mips:isa64"; 10458 10459 case elfcpp::E_MIPS_ARCH_32R2: 10460 return "mips:isa32r2"; 10461 10462 case elfcpp::E_MIPS_ARCH_64R2: 10463 return "mips:isa64r2"; 10464 } 10465 } 10466 return "unknown CPU"; 10467 } 10468 10469 template<int size, bool big_endian> 10470 const Target::Target_info Target_mips<size, big_endian>::mips_info = 10471 { 10472 size, // size 10473 big_endian, // is_big_endian 10474 elfcpp::EM_MIPS, // machine_code 10475 true, // has_make_symbol 10476 false, // has_resolve 10477 false, // has_code_fill 10478 true, // is_default_stack_executable 10479 false, // can_icf_inline_merge_sections 10480 '\0', // wrap_char 10481 "/lib/ld.so.1", // dynamic_linker 10482 0x400000, // default_text_segment_address 10483 64 * 1024, // abi_pagesize (overridable by -z max-page-size) 10484 4 * 1024, // common_pagesize (overridable by -z common-page-size) 10485 false, // isolate_execinstr 10486 0, // rosegment_gap 10487 elfcpp::SHN_UNDEF, // small_common_shndx 10488 elfcpp::SHN_UNDEF, // large_common_shndx 10489 0, // small_common_section_flags 10490 0, // large_common_section_flags 10491 NULL, // attributes_section 10492 NULL, // attributes_vendor 10493 "__start" // entry_symbol_name 10494 }; 10495 10496 template<int size, bool big_endian> 10497 class Target_mips_nacl : public Target_mips<size, big_endian> 10498 { 10499 public: 10500 Target_mips_nacl() 10501 : Target_mips<size, big_endian>(&mips_nacl_info) 10502 { } 10503 10504 private: 10505 static const Target::Target_info mips_nacl_info; 10506 }; 10507 10508 template<int size, bool big_endian> 10509 const Target::Target_info Target_mips_nacl<size, big_endian>::mips_nacl_info = 10510 { 10511 size, // size 10512 big_endian, // is_big_endian 10513 elfcpp::EM_MIPS, // machine_code 10514 true, // has_make_symbol 10515 false, // has_resolve 10516 false, // has_code_fill 10517 true, // is_default_stack_executable 10518 false, // can_icf_inline_merge_sections 10519 '\0', // wrap_char 10520 "/lib/ld.so.1", // dynamic_linker 10521 0x20000, // default_text_segment_address 10522 0x10000, // abi_pagesize (overridable by -z max-page-size) 10523 0x10000, // common_pagesize (overridable by -z common-page-size) 10524 true, // isolate_execinstr 10525 0x10000000, // rosegment_gap 10526 elfcpp::SHN_UNDEF, // small_common_shndx 10527 elfcpp::SHN_UNDEF, // large_common_shndx 10528 0, // small_common_section_flags 10529 0, // large_common_section_flags 10530 NULL, // attributes_section 10531 NULL, // attributes_vendor 10532 "_start" // entry_symbol_name 10533 }; 10534 10535 // Target selector for Mips. Note this is never instantiated directly. 10536 // It's only used in Target_selector_mips_nacl, below. 10537 10538 template<int size, bool big_endian> 10539 class Target_selector_mips : public Target_selector 10540 { 10541 public: 10542 Target_selector_mips() 10543 : Target_selector(elfcpp::EM_MIPS, size, big_endian, 10544 (size == 64 ? 10545 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") : 10546 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")), 10547 (size == 64 ? 10548 (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") : 10549 (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips"))) 10550 { } 10551 10552 Target* do_instantiate_target() 10553 { return new Target_mips<size, big_endian>(); } 10554 }; 10555 10556 template<int size, bool big_endian> 10557 class Target_selector_mips_nacl 10558 : public Target_selector_nacl<Target_selector_mips<size, big_endian>, 10559 Target_mips_nacl<size, big_endian> > 10560 { 10561 public: 10562 Target_selector_mips_nacl() 10563 : Target_selector_nacl<Target_selector_mips<size, big_endian>, 10564 Target_mips_nacl<size, big_endian> >( 10565 // NaCl currently supports only MIPS32 little-endian. 10566 "mipsel", "elf32-tradlittlemips-nacl", "elf32-tradlittlemips-nacl") 10567 { } 10568 }; 10569 10570 Target_selector_mips_nacl<32, true> target_selector_mips32; 10571 Target_selector_mips_nacl<32, false> target_selector_mips32el; 10572 Target_selector_mips_nacl<64, true> target_selector_mips64; 10573 Target_selector_mips_nacl<64, false> target_selector_mips64el; 10574 10575 } // End anonymous namespace. 10576