1 // reloc.h -- relocate input files for gold -*- C++ -*- 2 3 // Copyright (C) 2006-2014 Free Software Foundation, Inc. 4 // Written by Ian Lance Taylor <iant (at) google.com>. 5 6 // This file is part of gold. 7 8 // This program is free software; you can redistribute it and/or modify 9 // it under the terms of the GNU General Public License as published by 10 // the Free Software Foundation; either version 3 of the License, or 11 // (at your option) any later version. 12 13 // This program is distributed in the hope that it will be useful, 14 // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 // GNU General Public License for more details. 17 18 // You should have received a copy of the GNU General Public License 19 // along with this program; if not, write to the Free Software 20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 21 // MA 02110-1301, USA. 22 23 #ifndef GOLD_RELOC_H 24 #define GOLD_RELOC_H 25 26 #include <vector> 27 #ifdef HAVE_BYTESWAP_H 28 #include <byteswap.h> 29 #endif 30 31 #include "elfcpp.h" 32 #include "workqueue.h" 33 34 namespace gold 35 { 36 37 class General_options; 38 class Object; 39 class Relobj; 40 struct Read_relocs_data; 41 class Symbol; 42 class Layout; 43 class Output_data; 44 class Output_section; 45 46 template<int size> 47 class Sized_symbol; 48 49 template<int size, bool big_endian> 50 class Sized_relobj_file; 51 52 template<int size> 53 class Symbol_value; 54 55 template<int sh_type, bool dynamic, int size, bool big_endian> 56 class Output_data_reloc; 57 58 // A class to read the relocations for an object file, and then queue 59 // up a task to see if they require any GOT/PLT/COPY relocations in 60 // the symbol table. 61 62 class Read_relocs : public Task 63 { 64 public: 65 // THIS_BLOCKER and NEXT_BLOCKER are passed along to a Scan_relocs 66 // or Gc_process_relocs task, so that they run in a deterministic 67 // order. 68 Read_relocs(Symbol_table* symtab, Layout* layout, Relobj* object, 69 Task_token* this_blocker, Task_token* next_blocker) 70 : symtab_(symtab), layout_(layout), object_(object), 71 this_blocker_(this_blocker), next_blocker_(next_blocker) 72 { } 73 74 // The standard Task methods. 75 76 Task_token* 77 is_runnable(); 78 79 void 80 locks(Task_locker*); 81 82 void 83 run(Workqueue*); 84 85 std::string 86 get_name() const; 87 88 private: 89 Symbol_table* symtab_; 90 Layout* layout_; 91 Relobj* object_; 92 Task_token* this_blocker_; 93 Task_token* next_blocker_; 94 }; 95 96 // Process the relocs to figure out which sections are garbage. 97 // Very similar to scan relocs. 98 99 class Gc_process_relocs : public Task 100 { 101 public: 102 // THIS_BLOCKER prevents this task from running until the previous 103 // one is finished. NEXT_BLOCKER prevents the next task from 104 // running. 105 Gc_process_relocs(Symbol_table* symtab, Layout* layout, Relobj* object, 106 Read_relocs_data* rd, Task_token* this_blocker, 107 Task_token* next_blocker) 108 : symtab_(symtab), layout_(layout), object_(object), rd_(rd), 109 this_blocker_(this_blocker), next_blocker_(next_blocker) 110 { } 111 112 ~Gc_process_relocs(); 113 114 // The standard Task methods. 115 116 Task_token* 117 is_runnable(); 118 119 void 120 locks(Task_locker*); 121 122 void 123 run(Workqueue*); 124 125 std::string 126 get_name() const; 127 128 private: 129 Symbol_table* symtab_; 130 Layout* layout_; 131 Relobj* object_; 132 Read_relocs_data* rd_; 133 Task_token* this_blocker_; 134 Task_token* next_blocker_; 135 }; 136 137 // Scan the relocations for an object to see if they require any 138 // GOT/PLT/COPY relocations. 139 140 class Scan_relocs : public Task 141 { 142 public: 143 // THIS_BLOCKER prevents this task from running until the previous 144 // one is finished. NEXT_BLOCKER prevents the next task from 145 // running. 146 Scan_relocs(Symbol_table* symtab, Layout* layout, Relobj* object, 147 Read_relocs_data* rd, Task_token* this_blocker, 148 Task_token* next_blocker) 149 : symtab_(symtab), layout_(layout), object_(object), rd_(rd), 150 this_blocker_(this_blocker), next_blocker_(next_blocker) 151 { } 152 153 ~Scan_relocs(); 154 155 // The standard Task methods. 156 157 Task_token* 158 is_runnable(); 159 160 void 161 locks(Task_locker*); 162 163 void 164 run(Workqueue*); 165 166 std::string 167 get_name() const; 168 169 private: 170 Symbol_table* symtab_; 171 Layout* layout_; 172 Relobj* object_; 173 Read_relocs_data* rd_; 174 Task_token* this_blocker_; 175 Task_token* next_blocker_; 176 }; 177 178 // A class to perform all the relocations for an object file. 179 180 class Relocate_task : public Task 181 { 182 public: 183 Relocate_task(const Symbol_table* symtab, const Layout* layout, 184 Relobj* object, Output_file* of, 185 Task_token* input_sections_blocker, 186 Task_token* output_sections_blocker, Task_token* final_blocker, 187 bool defer_object_cleanup) 188 : symtab_(symtab), layout_(layout), object_(object), of_(of), 189 input_sections_blocker_(input_sections_blocker), 190 output_sections_blocker_(output_sections_blocker), 191 final_blocker_(final_blocker), 192 defer_object_cleanup_(defer_object_cleanup) 193 { } 194 195 // The standard Task methods. 196 197 virtual Task_token* 198 is_runnable(); 199 200 virtual void 201 locks(Task_locker*); 202 203 virtual void 204 run(Workqueue*); 205 206 virtual std::string 207 get_name() const; 208 209 protected: 210 const Symbol_table* symtab_; 211 const Layout* layout_; 212 Relobj* object_; 213 Output_file* of_; 214 Task_token* input_sections_blocker_; 215 Task_token* output_sections_blocker_; 216 Task_token* final_blocker_; 217 218 private: 219 // When this is true, do not do object cleanup, because later, Relocstub_task 220 // will do the chores. Only use this in Relocate_task. Do not use it in 221 // subclasses. 222 bool defer_object_cleanup_; 223 }; 224 225 // This task relocates stub_tables. It does similar things as a 226 // Relocate_task. The reason why we have this is that we have to wait for all 227 // instructions to be relocated before we can copy the instruction into 228 // stub_tables. More details here - pr/21491. 229 230 class Relocstub_task : public Relocate_task 231 { 232 public: 233 234 Relocstub_task(const Symbol_table* symtab, 235 const Layout* layout, 236 Relobj* object, 237 Output_file* of, 238 Task_token* input_sections_blocker, 239 Task_token* output_sections_blocker, 240 Task_token* final_blocker) 241 : Relocate_task(symtab, layout, object, of, 242 input_sections_blocker, 243 output_sections_blocker, 244 final_blocker, true) 245 {} 246 247 virtual void 248 run(Workqueue*); 249 250 virtual std::string 251 get_name() const; 252 }; 253 254 // During a relocatable link, this class records how relocations 255 // should be handled for a single input reloc section. An instance of 256 // this class is created while scanning relocs, and it is used while 257 // processing relocs. 258 259 class Relocatable_relocs 260 { 261 public: 262 // We use a vector of unsigned char to indicate how the input relocs 263 // should be handled. Each element is one of the following values. 264 // We create this vector when we initially scan the relocations. 265 enum Reloc_strategy 266 { 267 // Copy the input reloc. Don't modify it other than updating the 268 // r_offset field and the r_sym part of the r_info field. 269 RELOC_COPY, 270 // Copy the input reloc which is against an STT_SECTION symbol. 271 // Update the r_offset and r_sym part of the r_info field. Adjust 272 // the addend by subtracting the value of the old local symbol and 273 // adding the value of the new local symbol. The addend is in the 274 // SHT_RELA reloc and the contents of the data section do not need 275 // to be changed. 276 RELOC_ADJUST_FOR_SECTION_RELA, 277 // Like RELOC_ADJUST_FOR_SECTION_RELA but the addend should not be 278 // adjusted. 279 RELOC_ADJUST_FOR_SECTION_0, 280 // Like RELOC_ADJUST_FOR_SECTION_RELA but the contents of the 281 // section need to be changed. The number indicates the number of 282 // bytes in the addend in the section contents. 283 RELOC_ADJUST_FOR_SECTION_1, 284 RELOC_ADJUST_FOR_SECTION_2, 285 RELOC_ADJUST_FOR_SECTION_4, 286 RELOC_ADJUST_FOR_SECTION_8, 287 // Like RELOC_ADJUST_FOR_SECTION_4 but for unaligned relocs. 288 RELOC_ADJUST_FOR_SECTION_4_UNALIGNED, 289 // Discard the input reloc--process it completely when relocating 290 // the data section contents. 291 RELOC_DISCARD, 292 // An input reloc which is not discarded, but which requires 293 // target specific processing in order to update it. 294 RELOC_SPECIAL 295 }; 296 297 Relocatable_relocs() 298 : reloc_strategies_(), output_reloc_count_(0), posd_(NULL) 299 { } 300 301 // Record the number of relocs. 302 void 303 set_reloc_count(size_t reloc_count) 304 { this->reloc_strategies_.reserve(reloc_count); } 305 306 // Record what to do for the next reloc. 307 void 308 set_next_reloc_strategy(Reloc_strategy strategy) 309 { 310 this->reloc_strategies_.push_back(static_cast<unsigned char>(strategy)); 311 if (strategy != RELOC_DISCARD) 312 ++this->output_reloc_count_; 313 } 314 315 // Record the Output_data associated with this reloc section. 316 void 317 set_output_data(Output_data* posd) 318 { 319 gold_assert(this->posd_ == NULL); 320 this->posd_ = posd; 321 } 322 323 // Return the Output_data associated with this reloc section. 324 Output_data* 325 output_data() const 326 { return this->posd_; } 327 328 // Return what to do for reloc I. 329 Reloc_strategy 330 strategy(unsigned int i) const 331 { 332 gold_assert(i < this->reloc_strategies_.size()); 333 return static_cast<Reloc_strategy>(this->reloc_strategies_[i]); 334 } 335 336 // Return the number of relocations to create in the output file. 337 size_t 338 output_reloc_count() const 339 { return this->output_reloc_count_; } 340 341 private: 342 typedef std::vector<unsigned char> Reloc_strategies; 343 344 // The strategies for the input reloc. There is one entry in this 345 // vector for each relocation in the input section. 346 Reloc_strategies reloc_strategies_; 347 // The number of relocations to be created in the output file. 348 size_t output_reloc_count_; 349 // The output data structure associated with this relocation. 350 Output_data* posd_; 351 }; 352 353 // Standard relocation routines which are used on many targets. Here 354 // SIZE and BIG_ENDIAN refer to the target, not the relocation type. 355 356 template<int size, bool big_endian> 357 class Relocate_functions 358 { 359 private: 360 // Do a simple relocation with the addend in the section contents. 361 // VALSIZE is the size of the value. 362 template<int valsize> 363 static inline void 364 rel(unsigned char* view, 365 typename elfcpp::Swap<valsize, big_endian>::Valtype value) 366 { 367 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; 368 Valtype* wv = reinterpret_cast<Valtype*>(view); 369 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv); 370 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value); 371 } 372 373 // Like the above but for relocs at unaligned addresses. 374 template<int valsize> 375 static inline void 376 rel_unaligned(unsigned char* view, 377 typename elfcpp::Swap<valsize, big_endian>::Valtype value) 378 { 379 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype 380 Valtype; 381 Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view); 382 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, x + value); 383 } 384 385 // Do a simple relocation using a Symbol_value with the addend in 386 // the section contents. VALSIZE is the size of the value to 387 // relocate. 388 template<int valsize> 389 static inline void 390 rel(unsigned char* view, 391 const Sized_relobj_file<size, big_endian>* object, 392 const Symbol_value<size>* psymval) 393 { 394 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; 395 Valtype* wv = reinterpret_cast<Valtype*>(view); 396 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv); 397 x = psymval->value(object, x); 398 elfcpp::Swap<valsize, big_endian>::writeval(wv, x); 399 } 400 401 // Like the above but for relocs at unaligned addresses. 402 template<int valsize> 403 static inline void 404 rel_unaligned(unsigned char* view, 405 const Sized_relobj_file<size, big_endian>* object, 406 const Symbol_value<size>* psymval) 407 { 408 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype 409 Valtype; 410 Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view); 411 x = psymval->value(object, x); 412 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, x); 413 } 414 415 // Do a simple relocation with the addend in the relocation. 416 // VALSIZE is the size of the value. 417 template<int valsize> 418 static inline void 419 rela(unsigned char* view, 420 typename elfcpp::Swap<valsize, big_endian>::Valtype value, 421 typename elfcpp::Swap<valsize, big_endian>::Valtype addend) 422 { 423 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; 424 Valtype* wv = reinterpret_cast<Valtype*>(view); 425 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend); 426 } 427 428 // Do a simple relocation using a symbol value with the addend in 429 // the relocation. VALSIZE is the size of the value. 430 template<int valsize> 431 static inline void 432 rela(unsigned char* view, 433 const Sized_relobj_file<size, big_endian>* object, 434 const Symbol_value<size>* psymval, 435 typename elfcpp::Swap<valsize, big_endian>::Valtype addend) 436 { 437 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; 438 Valtype* wv = reinterpret_cast<Valtype*>(view); 439 Valtype x = psymval->value(object, addend); 440 elfcpp::Swap<valsize, big_endian>::writeval(wv, x); 441 } 442 443 // Do a simple PC relative relocation with the addend in the section 444 // contents. VALSIZE is the size of the value. 445 template<int valsize> 446 static inline void 447 pcrel(unsigned char* view, 448 typename elfcpp::Swap<valsize, big_endian>::Valtype value, 449 typename elfcpp::Elf_types<size>::Elf_Addr address) 450 { 451 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; 452 Valtype* wv = reinterpret_cast<Valtype*>(view); 453 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv); 454 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address); 455 } 456 457 // Like the above but for relocs at unaligned addresses. 458 template<int valsize> 459 static inline void 460 pcrel_unaligned(unsigned char* view, 461 typename elfcpp::Swap<valsize, big_endian>::Valtype value, 462 typename elfcpp::Elf_types<size>::Elf_Addr address) 463 { 464 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; 465 Valtype x = elfcpp::Swap_unaligned<valsize, big_endian>::readval(view); 466 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, 467 x + value - address); 468 } 469 470 // Do a simple PC relative relocation with a Symbol_value with the 471 // addend in the section contents. VALSIZE is the size of the 472 // value. 473 template<int valsize> 474 static inline void 475 pcrel(unsigned char* view, 476 const Sized_relobj_file<size, big_endian>* object, 477 const Symbol_value<size>* psymval, 478 typename elfcpp::Elf_types<size>::Elf_Addr address) 479 { 480 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; 481 Valtype* wv = reinterpret_cast<Valtype*>(view); 482 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv); 483 x = psymval->value(object, x); 484 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address); 485 } 486 487 // Do a simple PC relative relocation with the addend in the 488 // relocation. VALSIZE is the size of the value. 489 template<int valsize> 490 static inline void 491 pcrela(unsigned char* view, 492 typename elfcpp::Swap<valsize, big_endian>::Valtype value, 493 typename elfcpp::Swap<valsize, big_endian>::Valtype addend, 494 typename elfcpp::Elf_types<size>::Elf_Addr address) 495 { 496 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; 497 Valtype* wv = reinterpret_cast<Valtype*>(view); 498 elfcpp::Swap<valsize, big_endian>::writeval(wv, value + addend - address); 499 } 500 501 // Do a simple PC relative relocation with a Symbol_value with the 502 // addend in the relocation. VALSIZE is the size of the value. 503 template<int valsize> 504 static inline void 505 pcrela(unsigned char* view, 506 const Sized_relobj_file<size, big_endian>* object, 507 const Symbol_value<size>* psymval, 508 typename elfcpp::Swap<valsize, big_endian>::Valtype addend, 509 typename elfcpp::Elf_types<size>::Elf_Addr address) 510 { 511 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; 512 Valtype* wv = reinterpret_cast<Valtype*>(view); 513 Valtype x = psymval->value(object, addend); 514 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address); 515 } 516 517 typedef Relocate_functions<size, big_endian> This; 518 519 public: 520 // Do a simple 8-bit REL relocation with the addend in the section 521 // contents. 522 static inline void 523 rel8(unsigned char* view, unsigned char value) 524 { This::template rel<8>(view, value); } 525 526 static inline void 527 rel8(unsigned char* view, 528 const Sized_relobj_file<size, big_endian>* object, 529 const Symbol_value<size>* psymval) 530 { This::template rel<8>(view, object, psymval); } 531 532 // Do an 8-bit RELA relocation with the addend in the relocation. 533 static inline void 534 rela8(unsigned char* view, unsigned char value, unsigned char addend) 535 { This::template rela<8>(view, value, addend); } 536 537 static inline void 538 rela8(unsigned char* view, 539 const Sized_relobj_file<size, big_endian>* object, 540 const Symbol_value<size>* psymval, 541 unsigned char addend) 542 { This::template rela<8>(view, object, psymval, addend); } 543 544 // Do a simple 8-bit PC relative relocation with the addend in the 545 // section contents. 546 static inline void 547 pcrel8(unsigned char* view, unsigned char value, 548 typename elfcpp::Elf_types<size>::Elf_Addr address) 549 { This::template pcrel<8>(view, value, address); } 550 551 static inline void 552 pcrel8(unsigned char* view, 553 const Sized_relobj_file<size, big_endian>* object, 554 const Symbol_value<size>* psymval, 555 typename elfcpp::Elf_types<size>::Elf_Addr address) 556 { This::template pcrel<8>(view, object, psymval, address); } 557 558 // Do a simple 8-bit PC relative RELA relocation with the addend in 559 // the reloc. 560 static inline void 561 pcrela8(unsigned char* view, unsigned char value, unsigned char addend, 562 typename elfcpp::Elf_types<size>::Elf_Addr address) 563 { This::template pcrela<8>(view, value, addend, address); } 564 565 static inline void 566 pcrela8(unsigned char* view, 567 const Sized_relobj_file<size, big_endian>* object, 568 const Symbol_value<size>* psymval, 569 unsigned char addend, 570 typename elfcpp::Elf_types<size>::Elf_Addr address) 571 { This::template pcrela<8>(view, object, psymval, addend, address); } 572 573 // Do a simple 16-bit REL relocation with the addend in the section 574 // contents. 575 static inline void 576 rel16(unsigned char* view, elfcpp::Elf_Half value) 577 { This::template rel<16>(view, value); } 578 579 static inline void 580 rel16(unsigned char* view, 581 const Sized_relobj_file<size, big_endian>* object, 582 const Symbol_value<size>* psymval) 583 { This::template rel<16>(view, object, psymval); } 584 585 // Do an 16-bit RELA relocation with the addend in the relocation. 586 static inline void 587 rela16(unsigned char* view, elfcpp::Elf_Half value, elfcpp::Elf_Half addend) 588 { This::template rela<16>(view, value, addend); } 589 590 static inline void 591 rela16(unsigned char* view, 592 const Sized_relobj_file<size, big_endian>* object, 593 const Symbol_value<size>* psymval, 594 elfcpp::Elf_Half addend) 595 { This::template rela<16>(view, object, psymval, addend); } 596 597 // Do a simple 16-bit PC relative REL relocation with the addend in 598 // the section contents. 599 static inline void 600 pcrel16(unsigned char* view, elfcpp::Elf_Half value, 601 typename elfcpp::Elf_types<size>::Elf_Addr address) 602 { This::template pcrel<16>(view, value, address); } 603 604 static inline void 605 pcrel16(unsigned char* view, 606 const Sized_relobj_file<size, big_endian>* object, 607 const Symbol_value<size>* psymval, 608 typename elfcpp::Elf_types<size>::Elf_Addr address) 609 { This::template pcrel<16>(view, object, psymval, address); } 610 611 // Do a simple 16-bit PC relative RELA relocation with the addend in 612 // the reloc. 613 static inline void 614 pcrela16(unsigned char* view, elfcpp::Elf_Half value, 615 elfcpp::Elf_Half addend, 616 typename elfcpp::Elf_types<size>::Elf_Addr address) 617 { This::template pcrela<16>(view, value, addend, address); } 618 619 static inline void 620 pcrela16(unsigned char* view, 621 const Sized_relobj_file<size, big_endian>* object, 622 const Symbol_value<size>* psymval, 623 elfcpp::Elf_Half addend, 624 typename elfcpp::Elf_types<size>::Elf_Addr address) 625 { This::template pcrela<16>(view, object, psymval, addend, address); } 626 627 // Do a simple 32-bit REL relocation with the addend in the section 628 // contents. 629 static inline void 630 rel32(unsigned char* view, elfcpp::Elf_Word value) 631 { This::template rel<32>(view, value); } 632 633 // Like above but for relocs at unaligned addresses. 634 static inline void 635 rel32_unaligned(unsigned char* view, elfcpp::Elf_Word value) 636 { This::template rel_unaligned<32>(view, value); } 637 638 static inline void 639 rel32(unsigned char* view, 640 const Sized_relobj_file<size, big_endian>* object, 641 const Symbol_value<size>* psymval) 642 { This::template rel<32>(view, object, psymval); } 643 644 // Like above but for relocs at unaligned addresses. 645 static inline void 646 rel32_unaligned(unsigned char* view, 647 const Sized_relobj_file<size, big_endian>* object, 648 const Symbol_value<size>* psymval) 649 { This::template rel_unaligned<32>(view, object, psymval); } 650 651 // Do an 32-bit RELA relocation with the addend in the relocation. 652 static inline void 653 rela32(unsigned char* view, elfcpp::Elf_Word value, elfcpp::Elf_Word addend) 654 { This::template rela<32>(view, value, addend); } 655 656 static inline void 657 rela32(unsigned char* view, 658 const Sized_relobj_file<size, big_endian>* object, 659 const Symbol_value<size>* psymval, 660 elfcpp::Elf_Word addend) 661 { This::template rela<32>(view, object, psymval, addend); } 662 663 // Do a simple 32-bit PC relative REL relocation with the addend in 664 // the section contents. 665 static inline void 666 pcrel32(unsigned char* view, elfcpp::Elf_Word value, 667 typename elfcpp::Elf_types<size>::Elf_Addr address) 668 { This::template pcrel<32>(view, value, address); } 669 670 // Unaligned version of the above. 671 static inline void 672 pcrel32_unaligned(unsigned char* view, elfcpp::Elf_Word value, 673 typename elfcpp::Elf_types<size>::Elf_Addr address) 674 { This::template pcrel_unaligned<32>(view, value, address); } 675 676 static inline void 677 pcrel32(unsigned char* view, 678 const Sized_relobj_file<size, big_endian>* object, 679 const Symbol_value<size>* psymval, 680 typename elfcpp::Elf_types<size>::Elf_Addr address) 681 { This::template pcrel<32>(view, object, psymval, address); } 682 683 // Do a simple 32-bit PC relative RELA relocation with the addend in 684 // the relocation. 685 static inline void 686 pcrela32(unsigned char* view, elfcpp::Elf_Word value, 687 elfcpp::Elf_Word addend, 688 typename elfcpp::Elf_types<size>::Elf_Addr address) 689 { This::template pcrela<32>(view, value, addend, address); } 690 691 static inline void 692 pcrela32(unsigned char* view, 693 const Sized_relobj_file<size, big_endian>* object, 694 const Symbol_value<size>* psymval, 695 elfcpp::Elf_Word addend, 696 typename elfcpp::Elf_types<size>::Elf_Addr address) 697 { This::template pcrela<32>(view, object, psymval, addend, address); } 698 699 // Do a simple 64-bit REL relocation with the addend in the section 700 // contents. 701 static inline void 702 rel64(unsigned char* view, elfcpp::Elf_Xword value) 703 { This::template rel<64>(view, value); } 704 705 static inline void 706 rel64(unsigned char* view, 707 const Sized_relobj_file<size, big_endian>* object, 708 const Symbol_value<size>* psymval) 709 { This::template rel<64>(view, object, psymval); } 710 711 // Do a 64-bit RELA relocation with the addend in the relocation. 712 static inline void 713 rela64(unsigned char* view, elfcpp::Elf_Xword value, 714 elfcpp::Elf_Xword addend) 715 { This::template rela<64>(view, value, addend); } 716 717 static inline void 718 rela64(unsigned char* view, 719 const Sized_relobj_file<size, big_endian>* object, 720 const Symbol_value<size>* psymval, 721 elfcpp::Elf_Xword addend) 722 { This::template rela<64>(view, object, psymval, addend); } 723 724 // Do a simple 64-bit PC relative REL relocation with the addend in 725 // the section contents. 726 static inline void 727 pcrel64(unsigned char* view, elfcpp::Elf_Xword value, 728 typename elfcpp::Elf_types<size>::Elf_Addr address) 729 { This::template pcrel<64>(view, value, address); } 730 731 static inline void 732 pcrel64(unsigned char* view, 733 const Sized_relobj_file<size, big_endian>* object, 734 const Symbol_value<size>* psymval, 735 typename elfcpp::Elf_types<size>::Elf_Addr address) 736 { This::template pcrel<64>(view, object, psymval, address); } 737 738 // Do a simple 64-bit PC relative RELA relocation with the addend in 739 // the relocation. 740 static inline void 741 pcrela64(unsigned char* view, elfcpp::Elf_Xword value, 742 elfcpp::Elf_Xword addend, 743 typename elfcpp::Elf_types<size>::Elf_Addr address) 744 { This::template pcrela<64>(view, value, addend, address); } 745 746 static inline void 747 pcrela64(unsigned char* view, 748 const Sized_relobj_file<size, big_endian>* object, 749 const Symbol_value<size>* psymval, 750 elfcpp::Elf_Xword addend, 751 typename elfcpp::Elf_types<size>::Elf_Addr address) 752 { This::template pcrela<64>(view, object, psymval, addend, address); } 753 }; 754 755 // Integer manipulation functions used by various targets when 756 // performing relocations. 757 758 template<int bits> 759 class Bits 760 { 761 public: 762 // Sign extend an n-bit unsigned integer stored in a uint32_t into 763 // an int32_t. BITS must be between 1 and 32. 764 static inline int32_t 765 sign_extend32(uint32_t val) 766 { 767 gold_assert(bits > 0 && bits <= 32); 768 if (bits == 32) 769 return static_cast<int32_t>(val); 770 uint32_t mask = (~static_cast<uint32_t>(0)) >> (32 - bits); 771 val &= mask; 772 uint32_t top_bit = 1U << (bits - 1); 773 int32_t as_signed = static_cast<int32_t>(val); 774 if ((val & top_bit) != 0) 775 as_signed -= static_cast<int32_t>(top_bit * 2); 776 return as_signed; 777 } 778 779 // Return true if VAL (stored in a uint32_t) has overflowed a signed 780 // value with BITS bits. 781 static inline bool 782 has_overflow32(uint32_t val) 783 { 784 gold_assert(bits > 0 && bits <= 32); 785 if (bits == 32) 786 return false; 787 int32_t max = (1 << (bits - 1)) - 1; 788 int32_t min = -(1 << (bits - 1)); 789 int32_t as_signed = static_cast<int32_t>(val); 790 return as_signed > max || as_signed < min; 791 } 792 793 // Return true if VAL (stored in a uint32_t) has overflowed both a 794 // signed and an unsigned value. E.g., 795 // Bits<8>::has_signed_unsigned_overflow32 would check -128 <= VAL < 796 // 255. 797 static inline bool 798 has_signed_unsigned_overflow32(uint32_t val) 799 { 800 gold_assert(bits > 0 && bits <= 32); 801 if (bits == 32) 802 return false; 803 int32_t max = static_cast<int32_t>((1U << bits) - 1); 804 int32_t min = -(1 << (bits - 1)); 805 int32_t as_signed = static_cast<int32_t>(val); 806 return as_signed > max || as_signed < min; 807 } 808 809 // Select bits from A and B using bits in MASK. For each n in 810 // [0..31], the n-th bit in the result is chosen from the n-th bits 811 // of A and B. A zero selects A and a one selects B. 812 static inline uint32_t 813 bit_select32(uint32_t a, uint32_t b, uint32_t mask) 814 { return (a & ~mask) | (b & mask); } 815 816 // Sign extend an n-bit unsigned integer stored in a uint64_t into 817 // an int64_t. BITS must be between 1 and 64. 818 static inline int64_t 819 sign_extend(uint64_t val) 820 { 821 gold_assert(bits > 0 && bits <= 64); 822 if (bits == 64) 823 return static_cast<int64_t>(val); 824 uint64_t mask = (~static_cast<uint64_t>(0)) >> (64 - bits); 825 val &= mask; 826 uint64_t top_bit = static_cast<uint64_t>(1) << (bits - 1); 827 int64_t as_signed = static_cast<int64_t>(val); 828 if ((val & top_bit) != 0) 829 as_signed -= static_cast<int64_t>(top_bit * 2); 830 return as_signed; 831 } 832 833 // Return true if VAL (stored in a uint64_t) has overflowed a signed 834 // value with BITS bits. 835 static inline bool 836 has_overflow(uint64_t val) 837 { 838 gold_assert(bits > 0 && bits <= 64); 839 if (bits == 64) 840 return false; 841 int64_t max = (static_cast<int64_t>(1) << (bits - 1)) - 1; 842 int64_t min = -(static_cast<int64_t>(1) << (bits - 1)); 843 int64_t as_signed = static_cast<int64_t>(val); 844 return as_signed > max || as_signed < min; 845 } 846 847 // Return true if VAL (stored in a uint64_t) has overflowed both a 848 // signed and an unsigned value. E.g., 849 // Bits<8>::has_signed_unsigned_overflow would check -128 <= VAL < 850 // 255. 851 static inline bool 852 has_signed_unsigned_overflow64(uint64_t val) 853 { 854 gold_assert(bits > 0 && bits <= 64); 855 if (bits == 64) 856 return false; 857 int64_t max = static_cast<int64_t>((static_cast<uint64_t>(1) << bits) - 1); 858 int64_t min = -(static_cast<int64_t>(1) << (bits - 1)); 859 int64_t as_signed = static_cast<int64_t>(val); 860 return as_signed > max || as_signed < min; 861 } 862 863 // Select bits from A and B using bits in MASK. For each n in 864 // [0..31], the n-th bit in the result is chosen from the n-th bits 865 // of A and B. A zero selects A and a one selects B. 866 static inline uint64_t 867 bit_select64(uint64_t a, uint64_t b, uint64_t mask) 868 { return (a & ~mask) | (b & mask); } 869 }; 870 871 // Track relocations while reading a section. This lets you ask for 872 // the relocation at a certain offset, and see how relocs occur 873 // between points of interest. 874 875 template<int size, bool big_endian> 876 class Track_relocs 877 { 878 public: 879 Track_relocs() 880 : prelocs_(NULL), len_(0), pos_(0), reloc_size_(0) 881 { } 882 883 // Initialize the Track_relocs object. OBJECT is the object holding 884 // the reloc section, RELOC_SHNDX is the section index of the reloc 885 // section, and RELOC_TYPE is the type of the reloc section 886 // (elfcpp::SHT_REL or elfcpp::SHT_RELA). This returns false if 887 // something went wrong. 888 bool 889 initialize(Object* object, unsigned int reloc_shndx, 890 unsigned int reloc_type); 891 892 // Return the offset in the data section to which the next reloc 893 // applies. This returns -1 if there is no next reloc. 894 off_t 895 next_offset() const; 896 897 // Return the symbol index of the next reloc. This returns -1U if 898 // there is no next reloc. 899 unsigned int 900 next_symndx() const; 901 902 // Return the addend of the next reloc. This returns 0 if there is 903 // no next reloc. 904 uint64_t 905 next_addend() const; 906 907 // Advance to OFFSET within the data section, and return the number 908 // of relocs which would be skipped. 909 int 910 advance(off_t offset); 911 912 // Checkpoint the current position in the reloc section. 913 section_size_type 914 checkpoint() const 915 { return this->pos_; } 916 917 // Reset the position to CHECKPOINT. 918 void 919 reset(section_size_type checkpoint) 920 { this->pos_ = checkpoint; } 921 922 private: 923 // The contents of the input object's reloc section. 924 const unsigned char* prelocs_; 925 // The length of the reloc section. 926 section_size_type len_; 927 // Our current position in the reloc section. 928 section_size_type pos_; 929 // The size of the relocs in the section. 930 int reloc_size_; 931 }; 932 933 } // End namespace gold. 934 935 #endif // !defined(GOLD_RELOC_H) 936