1 // reloc.cc -- relocate input files for gold. 2 3 // Copyright (C) 2006-2016 Free Software Foundation, Inc. 4 // Written by Ian Lance Taylor <iant@google.com>. 5 6 // This file is part of gold. 7 8 // This program is free software; you can redistribute it and/or modify 9 // it under the terms of the GNU General Public License as published by 10 // the Free Software Foundation; either version 3 of the License, or 11 // (at your option) any later version. 12 13 // This program is distributed in the hope that it will be useful, 14 // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 // GNU General Public License for more details. 17 18 // You should have received a copy of the GNU General Public License 19 // along with this program; if not, write to the Free Software 20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 21 // MA 02110-1301, USA. 22 23 #include "gold.h" 24 25 #include <algorithm> 26 27 #include "workqueue.h" 28 #include "layout.h" 29 #include "symtab.h" 30 #include "output.h" 31 #include "merge.h" 32 #include "object.h" 33 #include "target-reloc.h" 34 #include "reloc.h" 35 #include "icf.h" 36 #include "compressed_output.h" 37 #include "incremental.h" 38 39 namespace gold 40 { 41 42 // Read_relocs methods. 43 44 // These tasks just read the relocation information from the file. 45 // After reading it, the start another task to process the 46 // information. These tasks requires access to the file. 47 48 Task_token* 49 Read_relocs::is_runnable() 50 { 51 return this->object_->is_locked() ? this->object_->token() : NULL; 52 } 53 54 // Lock the file. 55 56 void 57 Read_relocs::locks(Task_locker* tl) 58 { 59 Task_token* token = this->object_->token(); 60 if (token != NULL) 61 tl->add(this, token); 62 } 63 64 // Read the relocations and then start a Scan_relocs_task. 65 66 void 67 Read_relocs::run(Workqueue* workqueue) 68 { 69 Read_relocs_data* rd = new Read_relocs_data; 70 this->object_->read_relocs(rd); 71 this->object_->set_relocs_data(rd); 72 this->object_->release(); 73 74 // If garbage collection or identical comdat folding is desired, we 75 // process the relocs first before scanning them. Scanning of relocs is 76 // done only after garbage or identical sections is identified. 77 if (parameters->options().gc_sections() 78 || parameters->options().icf_enabled()) 79 { 80 workqueue->queue_next(new Gc_process_relocs(this->symtab_, 81 this->layout_, 82 this->object_, rd, 83 this->this_blocker_, 84 this->next_blocker_)); 85 } 86 else 87 { 88 workqueue->queue_next(new Scan_relocs(this->symtab_, this->layout_, 89 this->object_, rd, 90 this->this_blocker_, 91 this->next_blocker_)); 92 } 93 } 94 95 // Return a debugging name for the task. 96 97 std::string 98 Read_relocs::get_name() const 99 { 100 return "Read_relocs " + this->object_->name(); 101 } 102 103 // Gc_process_relocs methods. 104 105 Gc_process_relocs::~Gc_process_relocs() 106 { 107 if (this->this_blocker_ != NULL) 108 delete this->this_blocker_; 109 } 110 111 // These tasks process the relocations read by Read_relocs and 112 // determine which sections are referenced and which are garbage. 113 // This task is done only when --gc-sections is used. This is blocked 114 // by THIS_BLOCKER_. It unblocks NEXT_BLOCKER_. 115 116 Task_token* 117 Gc_process_relocs::is_runnable() 118 { 119 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) 120 return this->this_blocker_; 121 if (this->object_->is_locked()) 122 return this->object_->token(); 123 return NULL; 124 } 125 126 void 127 Gc_process_relocs::locks(Task_locker* tl) 128 { 129 tl->add(this, this->object_->token()); 130 tl->add(this, this->next_blocker_); 131 } 132 133 void 134 Gc_process_relocs::run(Workqueue*) 135 { 136 this->object_->gc_process_relocs(this->symtab_, this->layout_, this->rd_); 137 this->object_->release(); 138 } 139 140 // Return a debugging name for the task. 141 142 std::string 143 Gc_process_relocs::get_name() const 144 { 145 return "Gc_process_relocs " + this->object_->name(); 146 } 147 148 // Scan_relocs methods. 149 150 Scan_relocs::~Scan_relocs() 151 { 152 if (this->this_blocker_ != NULL) 153 delete this->this_blocker_; 154 } 155 156 // These tasks scan the relocations read by Read_relocs and mark up 157 // the symbol table to indicate which relocations are required. We 158 // use a lock on the symbol table to keep them from interfering with 159 // each other. 160 161 Task_token* 162 Scan_relocs::is_runnable() 163 { 164 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) 165 return this->this_blocker_; 166 if (this->object_->is_locked()) 167 return this->object_->token(); 168 return NULL; 169 } 170 171 // Return the locks we hold: one on the file, one on the symbol table 172 // and one blocker. 173 174 void 175 Scan_relocs::locks(Task_locker* tl) 176 { 177 Task_token* token = this->object_->token(); 178 if (token != NULL) 179 tl->add(this, token); 180 tl->add(this, this->next_blocker_); 181 } 182 183 // Scan the relocs. 184 185 void 186 Scan_relocs::run(Workqueue*) 187 { 188 this->object_->scan_relocs(this->symtab_, this->layout_, this->rd_); 189 delete this->rd_; 190 this->rd_ = NULL; 191 this->object_->release(); 192 } 193 194 // Return a debugging name for the task. 195 196 std::string 197 Scan_relocs::get_name() const 198 { 199 return "Scan_relocs " + this->object_->name(); 200 } 201 202 // Relocate_task methods. 203 204 // We may have to wait for the output sections to be written. 205 206 Task_token* 207 Relocate_task::is_runnable() 208 { 209 if (this->object_->relocs_must_follow_section_writes() 210 && this->output_sections_blocker_->is_blocked()) 211 return this->output_sections_blocker_; 212 213 if (this->object_->is_locked()) 214 return this->object_->token(); 215 216 return NULL; 217 } 218 219 // We want to lock the file while we run. We want to unblock 220 // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done. 221 // INPUT_SECTIONS_BLOCKER may be NULL. 222 223 void 224 Relocate_task::locks(Task_locker* tl) 225 { 226 if (this->input_sections_blocker_ != NULL) 227 tl->add(this, this->input_sections_blocker_); 228 tl->add(this, this->final_blocker_); 229 Task_token* token = this->object_->token(); 230 if (token != NULL) 231 tl->add(this, token); 232 } 233 234 // Run the task. 235 236 void 237 Relocate_task::run(Workqueue*) 238 { 239 this->object_->relocate(this->symtab_, this->layout_, this->of_); 240 241 // This is normally the last thing we will do with an object, so 242 // uncache all views. 243 this->object_->clear_view_cache_marks(); 244 245 this->object_->release(); 246 } 247 248 // Return a debugging name for the task. 249 250 std::string 251 Relocate_task::get_name() const 252 { 253 return "Relocate_task " + this->object_->name(); 254 } 255 256 // Read the relocs and local symbols from the object file and store 257 // the information in RD. 258 259 template<int size, bool big_endian> 260 void 261 Sized_relobj_file<size, big_endian>::do_read_relocs(Read_relocs_data* rd) 262 { 263 rd->relocs.clear(); 264 265 unsigned int shnum = this->shnum(); 266 if (shnum == 0) 267 return; 268 269 rd->relocs.reserve(shnum / 2); 270 271 const Output_sections& out_sections(this->output_sections()); 272 const std::vector<Address>& out_offsets(this->section_offsets()); 273 274 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(), 275 shnum * This::shdr_size, 276 true, true); 277 // Skip the first, dummy, section. 278 const unsigned char* ps = pshdrs + This::shdr_size; 279 for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size) 280 { 281 typename This::Shdr shdr(ps); 282 283 unsigned int sh_type = shdr.get_sh_type(); 284 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA) 285 continue; 286 287 unsigned int shndx = this->adjust_shndx(shdr.get_sh_info()); 288 if (shndx >= shnum) 289 { 290 this->error(_("relocation section %u has bad info %u"), 291 i, shndx); 292 continue; 293 } 294 295 Output_section* os = out_sections[shndx]; 296 if (os == NULL) 297 continue; 298 299 // We are scanning relocations in order to fill out the GOT and 300 // PLT sections. Relocations for sections which are not 301 // allocated (typically debugging sections) should not add new 302 // GOT and PLT entries. So we skip them unless this is a 303 // relocatable link or we need to emit relocations. FIXME: What 304 // should we do if a linker script maps a section with SHF_ALLOC 305 // clear to a section with SHF_ALLOC set? 306 typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size); 307 bool is_section_allocated = ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) 308 != 0); 309 if (!is_section_allocated 310 && !parameters->options().relocatable() 311 && !parameters->options().emit_relocs() 312 && !parameters->incremental()) 313 continue; 314 315 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_) 316 { 317 this->error(_("relocation section %u uses unexpected " 318 "symbol table %u"), 319 i, this->adjust_shndx(shdr.get_sh_link())); 320 continue; 321 } 322 323 off_t sh_size = shdr.get_sh_size(); 324 325 if (sh_size == 0) 326 continue; 327 328 unsigned int reloc_size; 329 if (sh_type == elfcpp::SHT_REL) 330 reloc_size = elfcpp::Elf_sizes<size>::rel_size; 331 else 332 reloc_size = elfcpp::Elf_sizes<size>::rela_size; 333 if (reloc_size != shdr.get_sh_entsize()) 334 { 335 this->error(_("unexpected entsize for reloc section %u: %lu != %u"), 336 i, static_cast<unsigned long>(shdr.get_sh_entsize()), 337 reloc_size); 338 continue; 339 } 340 341 size_t reloc_count = sh_size / reloc_size; 342 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size) 343 { 344 this->error(_("reloc section %u size %lu uneven"), 345 i, static_cast<unsigned long>(sh_size)); 346 continue; 347 } 348 349 rd->relocs.push_back(Section_relocs()); 350 Section_relocs& sr(rd->relocs.back()); 351 sr.reloc_shndx = i; 352 sr.data_shndx = shndx; 353 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size, 354 true, true); 355 sr.sh_type = sh_type; 356 sr.reloc_count = reloc_count; 357 sr.output_section = os; 358 sr.needs_special_offset_handling = out_offsets[shndx] == invalid_address; 359 sr.is_data_section_allocated = is_section_allocated; 360 } 361 362 // Read the local symbols. 363 gold_assert(this->symtab_shndx_ != -1U); 364 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0) 365 rd->local_symbols = NULL; 366 else 367 { 368 typename This::Shdr symtabshdr(pshdrs 369 + this->symtab_shndx_ * This::shdr_size); 370 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); 371 const int sym_size = This::sym_size; 372 const unsigned int loccount = this->local_symbol_count_; 373 gold_assert(loccount == symtabshdr.get_sh_info()); 374 off_t locsize = loccount * sym_size; 375 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(), 376 locsize, true, true); 377 } 378 } 379 380 // Process the relocs to generate mappings from source sections to referenced 381 // sections. This is used during garbage collection to determine garbage 382 // sections. 383 384 template<int size, bool big_endian> 385 void 386 Sized_relobj_file<size, big_endian>::do_gc_process_relocs(Symbol_table* symtab, 387 Layout* layout, 388 Read_relocs_data* rd) 389 { 390 Sized_target<size, big_endian>* target = 391 parameters->sized_target<size, big_endian>(); 392 393 const unsigned char* local_symbols; 394 if (rd->local_symbols == NULL) 395 local_symbols = NULL; 396 else 397 local_symbols = rd->local_symbols->data(); 398 399 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin(); 400 p != rd->relocs.end(); 401 ++p) 402 { 403 if (!parameters->options().relocatable()) 404 { 405 // As noted above, when not generating an object file, we 406 // only scan allocated sections. We may see a non-allocated 407 // section here if we are emitting relocs. 408 if (p->is_data_section_allocated) 409 target->gc_process_relocs(symtab, layout, this, 410 p->data_shndx, p->sh_type, 411 p->contents->data(), p->reloc_count, 412 p->output_section, 413 p->needs_special_offset_handling, 414 this->local_symbol_count_, 415 local_symbols); 416 } 417 } 418 } 419 420 421 // Scan the relocs and adjust the symbol table. This looks for 422 // relocations which require GOT/PLT/COPY relocations. 423 424 template<int size, bool big_endian> 425 void 426 Sized_relobj_file<size, big_endian>::do_scan_relocs(Symbol_table* symtab, 427 Layout* layout, 428 Read_relocs_data* rd) 429 { 430 Sized_target<size, big_endian>* target = 431 parameters->sized_target<size, big_endian>(); 432 433 const unsigned char* local_symbols; 434 if (rd->local_symbols == NULL) 435 local_symbols = NULL; 436 else 437 local_symbols = rd->local_symbols->data(); 438 439 // For incremental links, allocate the counters for incremental relocations. 440 if (layout->incremental_inputs() != NULL) 441 this->allocate_incremental_reloc_counts(); 442 443 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin(); 444 p != rd->relocs.end(); 445 ++p) 446 { 447 // When garbage collection is on, unreferenced sections are not included 448 // in the link that would have been included normally. This is known only 449 // after Read_relocs hence this check has to be done again. 450 if (parameters->options().gc_sections() 451 || parameters->options().icf_enabled()) 452 { 453 if (p->output_section == NULL) 454 continue; 455 } 456 if (!parameters->options().relocatable()) 457 { 458 // As noted above, when not generating an object file, we 459 // only scan allocated sections. We may see a non-allocated 460 // section here if we are emitting relocs. 461 if (p->is_data_section_allocated) 462 target->scan_relocs(symtab, layout, this, p->data_shndx, 463 p->sh_type, p->contents->data(), 464 p->reloc_count, p->output_section, 465 p->needs_special_offset_handling, 466 this->local_symbol_count_, 467 local_symbols); 468 if (parameters->options().emit_relocs()) 469 this->emit_relocs_scan(symtab, layout, local_symbols, p); 470 if (layout->incremental_inputs() != NULL) 471 this->incremental_relocs_scan(p); 472 } 473 else 474 { 475 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx); 476 gold_assert(rr != NULL); 477 rr->set_reloc_count(p->reloc_count); 478 target->scan_relocatable_relocs(symtab, layout, this, 479 p->data_shndx, p->sh_type, 480 p->contents->data(), 481 p->reloc_count, 482 p->output_section, 483 p->needs_special_offset_handling, 484 this->local_symbol_count_, 485 local_symbols, 486 rr); 487 } 488 489 delete p->contents; 490 p->contents = NULL; 491 } 492 493 // For incremental links, finalize the allocation of relocations. 494 if (layout->incremental_inputs() != NULL) 495 this->finalize_incremental_relocs(layout, true); 496 497 if (rd->local_symbols != NULL) 498 { 499 delete rd->local_symbols; 500 rd->local_symbols = NULL; 501 } 502 } 503 504 // Scan the input relocations for --emit-relocs. 505 506 template<int size, bool big_endian> 507 void 508 Sized_relobj_file<size, big_endian>::emit_relocs_scan( 509 Symbol_table* symtab, 510 Layout* layout, 511 const unsigned char* plocal_syms, 512 const Read_relocs_data::Relocs_list::iterator& p) 513 { 514 Sized_target<size, big_endian>* target = 515 parameters->sized_target<size, big_endian>(); 516 517 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx); 518 gold_assert(rr != NULL); 519 rr->set_reloc_count(p->reloc_count); 520 target->emit_relocs_scan( 521 symtab, 522 layout, 523 this, 524 p->data_shndx, 525 p->sh_type, 526 p->contents->data(), 527 p->reloc_count, 528 p->output_section, 529 p->needs_special_offset_handling, 530 this->local_symbol_count_, 531 plocal_syms, 532 rr); 533 } 534 535 // Scan the input relocations for --incremental. 536 537 template<int size, bool big_endian> 538 void 539 Sized_relobj_file<size, big_endian>::incremental_relocs_scan( 540 const Read_relocs_data::Relocs_list::iterator& p) 541 { 542 if (p->sh_type == elfcpp::SHT_REL) 543 this->incremental_relocs_scan_reltype<elfcpp::SHT_REL>(p); 544 else 545 { 546 gold_assert(p->sh_type == elfcpp::SHT_RELA); 547 this->incremental_relocs_scan_reltype<elfcpp::SHT_RELA>(p); 548 } 549 } 550 551 // Scan the input relocation for --incremental, templatized on the 552 // type of the relocation section. 553 554 template<int size, bool big_endian> 555 template<int sh_type> 556 void 557 Sized_relobj_file<size, big_endian>::incremental_relocs_scan_reltype( 558 const Read_relocs_data::Relocs_list::iterator& p) 559 { 560 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; 561 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; 562 const unsigned char* prelocs = p->contents->data(); 563 size_t reloc_count = p->reloc_count; 564 565 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) 566 { 567 Reltype reloc(prelocs); 568 569 if (p->needs_special_offset_handling 570 && !p->output_section->is_input_address_mapped(this, p->data_shndx, 571 reloc.get_r_offset())) 572 continue; 573 574 // FIXME: Some targets have a non-standard r_info field. 575 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); 576 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); 577 578 if (r_sym >= this->local_symbol_count_) 579 this->count_incremental_reloc(r_sym - this->local_symbol_count_); 580 } 581 } 582 583 // Relocate the input sections and write out the local symbols. 584 585 template<int size, bool big_endian> 586 void 587 Sized_relobj_file<size, big_endian>::do_relocate(const Symbol_table* symtab, 588 const Layout* layout, 589 Output_file* of) 590 { 591 unsigned int shnum = this->shnum(); 592 593 // Read the section headers. 594 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(), 595 shnum * This::shdr_size, 596 true, true); 597 598 Views views; 599 views.resize(shnum); 600 601 // Make two passes over the sections. The first one copies the 602 // section data to the output file. The second one applies 603 // relocations. 604 605 this->write_sections(layout, pshdrs, of, &views); 606 607 // To speed up relocations, we set up hash tables for fast lookup of 608 // input offsets to output addresses. 609 this->initialize_input_to_output_maps(); 610 611 // Make the views available through get_output_view() for the duration 612 // of this routine. This RAII class will reset output_views_ to NULL 613 // when the views go out of scope. 614 struct Set_output_views 615 { 616 Set_output_views(const Views** ppviews, const Views* pviews) 617 { 618 ppviews_ = ppviews; 619 *ppviews = pviews; 620 } 621 622 ~Set_output_views() 623 { *ppviews_ = NULL; } 624 625 const Views** ppviews_; 626 }; 627 Set_output_views set_output_views(&this->output_views_, &views); 628 629 // Apply relocations. 630 631 this->relocate_sections(symtab, layout, pshdrs, of, &views); 632 633 // After we've done the relocations, we release the hash tables, 634 // since we no longer need them. 635 this->free_input_to_output_maps(); 636 637 // Write out the accumulated views. 638 for (unsigned int i = 1; i < shnum; ++i) 639 { 640 if (views[i].view != NULL) 641 { 642 if (views[i].is_ctors_reverse_view) 643 this->reverse_words(views[i].view, views[i].view_size); 644 if (!views[i].is_postprocessing_view) 645 { 646 if (views[i].is_input_output_view) 647 of->write_input_output_view(views[i].offset, 648 views[i].view_size, 649 views[i].view); 650 else 651 of->write_output_view(views[i].offset, views[i].view_size, 652 views[i].view); 653 } 654 } 655 } 656 657 // Write out the local symbols. 658 this->write_local_symbols(of, layout->sympool(), layout->dynpool(), 659 layout->symtab_xindex(), layout->dynsym_xindex(), 660 layout->symtab_section_offset()); 661 } 662 663 // Sort a Read_multiple vector by file offset. 664 struct Read_multiple_compare 665 { 666 inline bool 667 operator()(const File_read::Read_multiple_entry& rme1, 668 const File_read::Read_multiple_entry& rme2) const 669 { return rme1.file_offset < rme2.file_offset; } 670 }; 671 672 // Write section data to the output file. PSHDRS points to the 673 // section headers. Record the views in *PVIEWS for use when 674 // relocating. 675 676 template<int size, bool big_endian> 677 void 678 Sized_relobj_file<size, big_endian>::write_sections(const Layout* layout, 679 const unsigned char* pshdrs, 680 Output_file* of, 681 Views* pviews) 682 { 683 unsigned int shnum = this->shnum(); 684 const Output_sections& out_sections(this->output_sections()); 685 const std::vector<Address>& out_offsets(this->section_offsets()); 686 687 File_read::Read_multiple rm; 688 bool is_sorted = true; 689 690 const unsigned char* p = pshdrs + This::shdr_size; 691 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) 692 { 693 View_size* pvs = &(*pviews)[i]; 694 695 pvs->view = NULL; 696 697 const Output_section* os = out_sections[i]; 698 if (os == NULL) 699 continue; 700 Address output_offset = out_offsets[i]; 701 702 typename This::Shdr shdr(p); 703 704 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS) 705 continue; 706 707 if ((parameters->options().relocatable() 708 || parameters->options().emit_relocs()) 709 && (shdr.get_sh_type() == elfcpp::SHT_REL 710 || shdr.get_sh_type() == elfcpp::SHT_RELA) 711 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) 712 { 713 // This is a reloc section in a relocatable link or when 714 // emitting relocs. We don't need to read the input file. 715 // The size and file offset are stored in the 716 // Relocatable_relocs structure. 717 Relocatable_relocs* rr = this->relocatable_relocs(i); 718 gold_assert(rr != NULL); 719 Output_data* posd = rr->output_data(); 720 gold_assert(posd != NULL); 721 722 pvs->offset = posd->offset(); 723 pvs->view_size = posd->data_size(); 724 pvs->view = of->get_output_view(pvs->offset, pvs->view_size); 725 pvs->address = posd->address(); 726 pvs->is_input_output_view = false; 727 pvs->is_postprocessing_view = false; 728 pvs->is_ctors_reverse_view = false; 729 730 continue; 731 } 732 733 // In the normal case, this input section is simply mapped to 734 // the output section at offset OUTPUT_OFFSET. 735 736 // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is 737 // handled specially--e.g., a .eh_frame section. The relocation 738 // routines need to check for each reloc where it should be 739 // applied. For this case, we need an input/output view for the 740 // entire contents of the section in the output file. We don't 741 // want to copy the contents of the input section to the output 742 // section; the output section contents were already written, 743 // and we waited for them in Relocate_task::is_runnable because 744 // relocs_must_follow_section_writes is set for the object. 745 746 // Regardless of which of the above cases is true, we have to 747 // check requires_postprocessing of the output section. If that 748 // is false, then we work with views of the output file 749 // directly. If it is true, then we work with a separate 750 // buffer, and the output section is responsible for writing the 751 // final data to the output file. 752 753 off_t output_section_offset; 754 Address output_section_size; 755 if (!os->requires_postprocessing()) 756 { 757 output_section_offset = os->offset(); 758 output_section_size = convert_types<Address, off_t>(os->data_size()); 759 } 760 else 761 { 762 output_section_offset = 0; 763 output_section_size = 764 convert_types<Address, off_t>(os->postprocessing_buffer_size()); 765 } 766 767 off_t view_start; 768 section_size_type view_size; 769 bool must_decompress = false; 770 if (output_offset != invalid_address) 771 { 772 view_start = output_section_offset + output_offset; 773 view_size = convert_to_section_size_type(shdr.get_sh_size()); 774 section_size_type uncompressed_size; 775 if (this->section_is_compressed(i, &uncompressed_size)) 776 { 777 view_size = uncompressed_size; 778 must_decompress = true; 779 } 780 } 781 else 782 { 783 view_start = output_section_offset; 784 view_size = convert_to_section_size_type(output_section_size); 785 } 786 787 if (view_size == 0) 788 continue; 789 790 gold_assert(output_offset == invalid_address 791 || output_offset + view_size <= output_section_size); 792 793 unsigned char* view; 794 if (os->requires_postprocessing()) 795 { 796 unsigned char* buffer = os->postprocessing_buffer(); 797 view = buffer + view_start; 798 if (output_offset != invalid_address && !must_decompress) 799 { 800 off_t sh_offset = shdr.get_sh_offset(); 801 if (!rm.empty() && rm.back().file_offset > sh_offset) 802 is_sorted = false; 803 rm.push_back(File_read::Read_multiple_entry(sh_offset, 804 view_size, view)); 805 } 806 } 807 else 808 { 809 if (output_offset == invalid_address) 810 view = of->get_input_output_view(view_start, view_size); 811 else 812 { 813 view = of->get_output_view(view_start, view_size); 814 if (!must_decompress) 815 { 816 off_t sh_offset = shdr.get_sh_offset(); 817 if (!rm.empty() && rm.back().file_offset > sh_offset) 818 is_sorted = false; 819 rm.push_back(File_read::Read_multiple_entry(sh_offset, 820 view_size, view)); 821 } 822 } 823 } 824 825 if (must_decompress) 826 { 827 // Read and decompress the section. 828 section_size_type len; 829 const unsigned char* p = this->section_contents(i, &len, false); 830 if (!decompress_input_section(p, len, view, view_size, 831 size, big_endian, 832 shdr.get_sh_flags())) 833 this->error(_("could not decompress section %s"), 834 this->section_name(i).c_str()); 835 } 836 837 pvs->view = view; 838 pvs->address = os->address(); 839 if (output_offset != invalid_address) 840 pvs->address += output_offset; 841 pvs->offset = view_start; 842 pvs->view_size = view_size; 843 pvs->is_input_output_view = output_offset == invalid_address; 844 pvs->is_postprocessing_view = os->requires_postprocessing(); 845 pvs->is_ctors_reverse_view = 846 (!parameters->options().relocatable() 847 && view_size > size / 8 848 && (strcmp(os->name(), ".init_array") == 0 849 || strcmp(os->name(), ".fini_array") == 0) 850 && layout->is_ctors_in_init_array(this, i)); 851 } 852 853 // Actually read the data. 854 if (!rm.empty()) 855 { 856 if (!is_sorted) 857 std::sort(rm.begin(), rm.end(), Read_multiple_compare()); 858 this->read_multiple(rm); 859 } 860 } 861 862 // Relocate section data. VIEWS points to the section data as views 863 // in the output file. 864 865 template<int size, bool big_endian> 866 void 867 Sized_relobj_file<size, big_endian>::do_relocate_sections( 868 const Symbol_table* symtab, 869 const Layout* layout, 870 const unsigned char* pshdrs, 871 Output_file* of, 872 Views* pviews) 873 { 874 unsigned int shnum = this->shnum(); 875 Sized_target<size, big_endian>* target = 876 parameters->sized_target<size, big_endian>(); 877 878 const Output_sections& out_sections(this->output_sections()); 879 const std::vector<Address>& out_offsets(this->section_offsets()); 880 881 Relocate_info<size, big_endian> relinfo; 882 relinfo.symtab = symtab; 883 relinfo.layout = layout; 884 relinfo.object = this; 885 886 const unsigned char* p = pshdrs + This::shdr_size; 887 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) 888 { 889 typename This::Shdr shdr(p); 890 891 unsigned int sh_type = shdr.get_sh_type(); 892 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA) 893 continue; 894 895 off_t sh_size = shdr.get_sh_size(); 896 if (sh_size == 0) 897 continue; 898 899 unsigned int index = this->adjust_shndx(shdr.get_sh_info()); 900 if (index >= this->shnum()) 901 { 902 this->error(_("relocation section %u has bad info %u"), 903 i, index); 904 continue; 905 } 906 907 Output_section* os = out_sections[index]; 908 if (os == NULL) 909 { 910 // This relocation section is against a section which we 911 // discarded. 912 continue; 913 } 914 Address output_offset = out_offsets[index]; 915 916 gold_assert((*pviews)[index].view != NULL); 917 if (parameters->options().relocatable()) 918 gold_assert((*pviews)[i].view != NULL); 919 920 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_) 921 { 922 gold_error(_("relocation section %u uses unexpected " 923 "symbol table %u"), 924 i, this->adjust_shndx(shdr.get_sh_link())); 925 continue; 926 } 927 928 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(), 929 sh_size, true, false); 930 931 unsigned int reloc_size; 932 if (sh_type == elfcpp::SHT_REL) 933 reloc_size = elfcpp::Elf_sizes<size>::rel_size; 934 else 935 reloc_size = elfcpp::Elf_sizes<size>::rela_size; 936 937 if (reloc_size != shdr.get_sh_entsize()) 938 { 939 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"), 940 i, static_cast<unsigned long>(shdr.get_sh_entsize()), 941 reloc_size); 942 continue; 943 } 944 945 size_t reloc_count = sh_size / reloc_size; 946 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size) 947 { 948 gold_error(_("reloc section %u size %lu uneven"), 949 i, static_cast<unsigned long>(sh_size)); 950 continue; 951 } 952 953 gold_assert(output_offset != invalid_address 954 || this->relocs_must_follow_section_writes()); 955 956 relinfo.reloc_shndx = i; 957 relinfo.reloc_shdr = p; 958 relinfo.data_shndx = index; 959 relinfo.data_shdr = pshdrs + index * This::shdr_size; 960 unsigned char* view = (*pviews)[index].view; 961 Address address = (*pviews)[index].address; 962 section_size_type view_size = (*pviews)[index].view_size; 963 964 Reloc_symbol_changes* reloc_map = NULL; 965 if (this->uses_split_stack() && output_offset != invalid_address) 966 { 967 typename This::Shdr data_shdr(pshdrs + index * This::shdr_size); 968 if ((data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0) 969 this->split_stack_adjust(symtab, pshdrs, sh_type, index, 970 prelocs, reloc_count, view, view_size, 971 &reloc_map, target); 972 } 973 974 Relocatable_relocs* rr = NULL; 975 if (parameters->options().emit_relocs() 976 || parameters->options().relocatable()) 977 rr = this->relocatable_relocs(i); 978 relinfo.rr = rr; 979 980 if (!parameters->options().relocatable()) 981 { 982 target->relocate_section(&relinfo, sh_type, prelocs, reloc_count, os, 983 output_offset == invalid_address, 984 view, address, view_size, reloc_map); 985 if (parameters->options().emit_relocs()) 986 target->relocate_relocs(&relinfo, sh_type, prelocs, reloc_count, 987 os, output_offset, 988 view, address, view_size, 989 (*pviews)[i].view, 990 (*pviews)[i].view_size); 991 if (parameters->incremental()) 992 this->incremental_relocs_write(&relinfo, sh_type, prelocs, 993 reloc_count, os, output_offset, of); 994 } 995 else 996 target->relocate_relocs(&relinfo, sh_type, prelocs, reloc_count, 997 os, output_offset, 998 view, address, view_size, 999 (*pviews)[i].view, 1000 (*pviews)[i].view_size); 1001 } 1002 } 1003 1004 // Return the output view for section SHNDX. 1005 1006 template<int size, bool big_endian> 1007 unsigned char* 1008 Sized_relobj_file<size, big_endian>::do_get_output_view( 1009 unsigned int shndx, 1010 section_size_type* plen) const 1011 { 1012 gold_assert(this->output_views_ != NULL); 1013 gold_assert(shndx < this->output_views_->size()); 1014 const View_size& v = (*this->output_views_)[shndx]; 1015 *plen = v.view_size; 1016 return v.view; 1017 } 1018 1019 // Write the incremental relocs. 1020 1021 template<int size, bool big_endian> 1022 void 1023 Sized_relobj_file<size, big_endian>::incremental_relocs_write( 1024 const Relocate_info<size, big_endian>* relinfo, 1025 unsigned int sh_type, 1026 const unsigned char* prelocs, 1027 size_t reloc_count, 1028 Output_section* output_section, 1029 Address output_offset, 1030 Output_file* of) 1031 { 1032 if (sh_type == elfcpp::SHT_REL) 1033 this->incremental_relocs_write_reltype<elfcpp::SHT_REL>( 1034 relinfo, 1035 prelocs, 1036 reloc_count, 1037 output_section, 1038 output_offset, 1039 of); 1040 else 1041 { 1042 gold_assert(sh_type == elfcpp::SHT_RELA); 1043 this->incremental_relocs_write_reltype<elfcpp::SHT_RELA>( 1044 relinfo, 1045 prelocs, 1046 reloc_count, 1047 output_section, 1048 output_offset, 1049 of); 1050 } 1051 } 1052 1053 // Write the incremental relocs, templatized on the type of the 1054 // relocation section. 1055 1056 template<int size, bool big_endian> 1057 template<int sh_type> 1058 void 1059 Sized_relobj_file<size, big_endian>::incremental_relocs_write_reltype( 1060 const Relocate_info<size, big_endian>* relinfo, 1061 const unsigned char* prelocs, 1062 size_t reloc_count, 1063 Output_section* output_section, 1064 Address output_offset, 1065 Output_file* of) 1066 { 1067 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reloc; 1068 const unsigned int reloc_size = 1069 Reloc_types<sh_type, size, big_endian>::reloc_size; 1070 const unsigned int sizeof_addr = size / 8; 1071 const unsigned int incr_reloc_size = 1072 Incremental_relocs_reader<size, big_endian>::reloc_size; 1073 1074 unsigned int out_shndx = output_section->out_shndx(); 1075 1076 // Get a view for the .gnu_incremental_relocs section. 1077 1078 Incremental_inputs* inputs = relinfo->layout->incremental_inputs(); 1079 gold_assert(inputs != NULL); 1080 const off_t relocs_off = inputs->relocs_section()->offset(); 1081 const off_t relocs_size = inputs->relocs_section()->data_size(); 1082 unsigned char* const view = of->get_output_view(relocs_off, relocs_size); 1083 1084 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) 1085 { 1086 Reloc reloc(prelocs); 1087 1088 // FIXME: Some targets have a non-standard r_info field. 1089 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); 1090 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); 1091 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info); 1092 1093 if (r_sym < this->local_symbol_count_) 1094 continue; 1095 1096 // Get the new offset--the location in the output section where 1097 // this relocation should be applied. 1098 1099 Address offset = reloc.get_r_offset(); 1100 if (output_offset != invalid_address) 1101 offset += output_offset; 1102 else 1103 { 1104 section_offset_type sot_offset = 1105 convert_types<section_offset_type, Address>(offset); 1106 section_offset_type new_sot_offset = 1107 output_section->output_offset(relinfo->object, 1108 relinfo->data_shndx, 1109 sot_offset); 1110 gold_assert(new_sot_offset != -1); 1111 offset += new_sot_offset; 1112 } 1113 1114 // Get the addend. 1115 typename elfcpp::Elf_types<size>::Elf_Swxword addend; 1116 if (sh_type == elfcpp::SHT_RELA) 1117 addend = 1118 Reloc_types<sh_type, size, big_endian>::get_reloc_addend(&reloc); 1119 else 1120 { 1121 // FIXME: Get the addend for SHT_REL. 1122 addend = 0; 1123 } 1124 1125 // Get the index of the output relocation. 1126 1127 unsigned int reloc_index = 1128 this->next_incremental_reloc_index(r_sym - this->local_symbol_count_); 1129 1130 // Write the relocation. 1131 1132 unsigned char* pov = view + reloc_index * incr_reloc_size; 1133 elfcpp::Swap<32, big_endian>::writeval(pov, r_type); 1134 elfcpp::Swap<32, big_endian>::writeval(pov + 4, out_shndx); 1135 elfcpp::Swap<size, big_endian>::writeval(pov + 8, offset); 1136 elfcpp::Swap<size, big_endian>::writeval(pov + 8 + sizeof_addr, addend); 1137 of->write_output_view(pov - view, incr_reloc_size, view); 1138 } 1139 } 1140 1141 // Create merge hash tables for the local symbols. These are used to 1142 // speed up relocations. 1143 1144 template<int size, bool big_endian> 1145 void 1146 Sized_relobj_file<size, big_endian>::initialize_input_to_output_maps() 1147 { 1148 const unsigned int loccount = this->local_symbol_count_; 1149 for (unsigned int i = 1; i < loccount; ++i) 1150 { 1151 Symbol_value<size>& lv(this->local_values_[i]); 1152 lv.initialize_input_to_output_map(this); 1153 } 1154 } 1155 1156 // Free merge hash tables for the local symbols. 1157 1158 template<int size, bool big_endian> 1159 void 1160 Sized_relobj_file<size, big_endian>::free_input_to_output_maps() 1161 { 1162 const unsigned int loccount = this->local_symbol_count_; 1163 for (unsigned int i = 1; i < loccount; ++i) 1164 { 1165 Symbol_value<size>& lv(this->local_values_[i]); 1166 lv.free_input_to_output_map(); 1167 } 1168 } 1169 1170 // If an object was compiled with -fsplit-stack, this is called to 1171 // check whether any relocations refer to functions defined in objects 1172 // which were not compiled with -fsplit-stack. If they were, then we 1173 // need to apply some target-specific adjustments to request 1174 // additional stack space. 1175 1176 template<int size, bool big_endian> 1177 void 1178 Sized_relobj_file<size, big_endian>::split_stack_adjust( 1179 const Symbol_table* symtab, 1180 const unsigned char* pshdrs, 1181 unsigned int sh_type, 1182 unsigned int shndx, 1183 const unsigned char* prelocs, 1184 size_t reloc_count, 1185 unsigned char* view, 1186 section_size_type view_size, 1187 Reloc_symbol_changes** reloc_map, 1188 const Sized_target<size, big_endian>* target) 1189 { 1190 if (sh_type == elfcpp::SHT_REL) 1191 this->split_stack_adjust_reltype<elfcpp::SHT_REL>(symtab, pshdrs, shndx, 1192 prelocs, reloc_count, 1193 view, view_size, 1194 reloc_map, target); 1195 else 1196 { 1197 gold_assert(sh_type == elfcpp::SHT_RELA); 1198 this->split_stack_adjust_reltype<elfcpp::SHT_RELA>(symtab, pshdrs, shndx, 1199 prelocs, reloc_count, 1200 view, view_size, 1201 reloc_map, target); 1202 } 1203 } 1204 1205 // Adjust for -fsplit-stack, templatized on the type of the relocation 1206 // section. 1207 1208 template<int size, bool big_endian> 1209 template<int sh_type> 1210 void 1211 Sized_relobj_file<size, big_endian>::split_stack_adjust_reltype( 1212 const Symbol_table* symtab, 1213 const unsigned char* pshdrs, 1214 unsigned int shndx, 1215 const unsigned char* prelocs, 1216 size_t reloc_count, 1217 unsigned char* view, 1218 section_size_type view_size, 1219 Reloc_symbol_changes** reloc_map, 1220 const Sized_target<size, big_endian>* target) 1221 { 1222 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; 1223 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; 1224 1225 size_t local_count = this->local_symbol_count(); 1226 1227 std::vector<section_offset_type> non_split_refs; 1228 1229 const unsigned char* pr = prelocs; 1230 for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size) 1231 { 1232 // Some supported targets have a non-standard r_info field. 1233 // If this call is too slow, we can move this routine to 1234 // target-reloc.h and templatize it on Classify_reloc. 1235 unsigned int r_sym = target->get_r_sym(pr); 1236 if (r_sym < local_count) 1237 continue; 1238 1239 const Symbol* gsym = this->global_symbol(r_sym); 1240 gold_assert(gsym != NULL); 1241 if (gsym->is_forwarder()) 1242 gsym = symtab->resolve_forwards(gsym); 1243 1244 // See if this relocation refers to a function defined in an 1245 // object compiled without -fsplit-stack. Note that we don't 1246 // care about the type of relocation--this means that in some 1247 // cases we will ask for a large stack unnecessarily, but this 1248 // is not fatal. FIXME: Some targets have symbols which are 1249 // functions but are not type STT_FUNC, e.g., STT_ARM_TFUNC. 1250 if (!gsym->is_undefined() 1251 && gsym->source() == Symbol::FROM_OBJECT 1252 && !gsym->object()->uses_split_stack()) 1253 { 1254 if (parameters->target().is_call_to_non_split(gsym, pr, view, 1255 view_size)) 1256 { 1257 Reltype reloc(pr); 1258 section_offset_type offset = 1259 convert_to_section_size_type(reloc.get_r_offset()); 1260 non_split_refs.push_back(offset); 1261 } 1262 } 1263 } 1264 1265 if (non_split_refs.empty()) 1266 return; 1267 1268 // At this point, every entry in NON_SPLIT_REFS indicates a 1269 // relocation which refers to a function in an object compiled 1270 // without -fsplit-stack. We now have to convert that list into a 1271 // set of offsets to functions. First, we find all the functions. 1272 1273 Function_offsets function_offsets; 1274 this->find_functions(pshdrs, shndx, &function_offsets); 1275 if (function_offsets.empty()) 1276 return; 1277 1278 // Now get a list of the function with references to non split-stack 1279 // code. 1280 1281 Function_offsets calls_non_split; 1282 for (std::vector<section_offset_type>::const_iterator p 1283 = non_split_refs.begin(); 1284 p != non_split_refs.end(); 1285 ++p) 1286 { 1287 Function_offsets::const_iterator low = function_offsets.lower_bound(*p); 1288 if (low == function_offsets.end()) 1289 --low; 1290 else if (low->first == *p) 1291 ; 1292 else if (low == function_offsets.begin()) 1293 continue; 1294 else 1295 --low; 1296 1297 calls_non_split.insert(*low); 1298 } 1299 if (calls_non_split.empty()) 1300 return; 1301 1302 // Now we have a set of functions to adjust. The adjustments are 1303 // target specific. Besides changing the output section view 1304 // however, it likes, the target may request a relocation change 1305 // from one global symbol name to another. 1306 1307 for (Function_offsets::const_iterator p = calls_non_split.begin(); 1308 p != calls_non_split.end(); 1309 ++p) 1310 { 1311 std::string from; 1312 std::string to; 1313 parameters->target().calls_non_split(this, shndx, p->first, p->second, 1314 prelocs, reloc_count, 1315 view, view_size, &from, &to); 1316 if (!from.empty()) 1317 { 1318 gold_assert(!to.empty()); 1319 Symbol* tosym = NULL; 1320 1321 // Find relocations in the relevant function which are for 1322 // FROM. 1323 pr = prelocs; 1324 for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size) 1325 { 1326 Reltype reloc(pr); 1327 1328 unsigned int r_sym = target->get_r_sym(pr); 1329 if (r_sym < local_count) 1330 continue; 1331 1332 section_offset_type offset = 1333 convert_to_section_size_type(reloc.get_r_offset()); 1334 if (offset < p->first 1335 || (offset 1336 >= (p->first 1337 + static_cast<section_offset_type>(p->second)))) 1338 continue; 1339 1340 const Symbol* gsym = this->global_symbol(r_sym); 1341 if (from == gsym->name()) 1342 { 1343 if (tosym == NULL) 1344 { 1345 tosym = symtab->lookup(to.c_str()); 1346 if (tosym == NULL) 1347 { 1348 this->error(_("could not convert call " 1349 "to '%s' to '%s'"), 1350 from.c_str(), to.c_str()); 1351 break; 1352 } 1353 } 1354 1355 if (*reloc_map == NULL) 1356 *reloc_map = new Reloc_symbol_changes(reloc_count); 1357 (*reloc_map)->set(i, tosym); 1358 } 1359 } 1360 } 1361 } 1362 } 1363 1364 // Find all the function in this object defined in section SHNDX. 1365 // Store their offsets in the section in FUNCTION_OFFSETS. 1366 1367 template<int size, bool big_endian> 1368 void 1369 Sized_relobj_file<size, big_endian>::find_functions( 1370 const unsigned char* pshdrs, 1371 unsigned int shndx, 1372 Sized_relobj_file<size, big_endian>::Function_offsets* function_offsets) 1373 { 1374 // We need to read the symbols to find the functions. If we wanted 1375 // to, we could cache reading the symbols across all sections in the 1376 // object. 1377 const unsigned int symtab_shndx = this->symtab_shndx_; 1378 typename This::Shdr symtabshdr(pshdrs + symtab_shndx * This::shdr_size); 1379 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); 1380 1381 typename elfcpp::Elf_types<size>::Elf_WXword sh_size = 1382 symtabshdr.get_sh_size(); 1383 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(), 1384 sh_size, true, true); 1385 1386 const int sym_size = This::sym_size; 1387 const unsigned int symcount = sh_size / sym_size; 1388 for (unsigned int i = 0; i < symcount; ++i, psyms += sym_size) 1389 { 1390 typename elfcpp::Sym<size, big_endian> isym(psyms); 1391 1392 // FIXME: Some targets can have functions which do not have type 1393 // STT_FUNC, e.g., STT_ARM_TFUNC. 1394 if (isym.get_st_type() != elfcpp::STT_FUNC 1395 || isym.get_st_size() == 0) 1396 continue; 1397 1398 bool is_ordinary; 1399 Symbol_location loc; 1400 loc.shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(), 1401 &is_ordinary); 1402 if (!is_ordinary) 1403 continue; 1404 1405 loc.object = this; 1406 loc.offset = isym.get_st_value(); 1407 parameters->target().function_location(&loc); 1408 1409 if (loc.shndx != shndx) 1410 continue; 1411 1412 section_offset_type value = 1413 convert_to_section_size_type(loc.offset); 1414 section_size_type fnsize = 1415 convert_to_section_size_type(isym.get_st_size()); 1416 1417 (*function_offsets)[value] = fnsize; 1418 } 1419 } 1420 1421 // Reverse the words in a section. Used for .ctors sections mapped to 1422 // .init_array sections. See ctors_sections_in_init_array in 1423 // layout.cc. 1424 1425 template<int size, bool big_endian> 1426 void 1427 Sized_relobj_file<size, big_endian>::reverse_words(unsigned char* view, 1428 section_size_type view_size) 1429 { 1430 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype; 1431 Valtype* vview = reinterpret_cast<Valtype*>(view); 1432 section_size_type vview_size = view_size / (size / 8); 1433 for (section_size_type i = 0; i < vview_size / 2; ++i) 1434 { 1435 Valtype tmp = vview[i]; 1436 vview[i] = vview[vview_size - 1 - i]; 1437 vview[vview_size - 1 - i] = tmp; 1438 } 1439 } 1440 1441 // Class Merged_symbol_value. 1442 1443 template<int size> 1444 void 1445 Merged_symbol_value<size>::initialize_input_to_output_map( 1446 const Relobj* object, 1447 unsigned int input_shndx) 1448 { 1449 object->initialize_input_to_output_map<size>(input_shndx, 1450 this->output_start_address_, 1451 &this->output_addresses_); 1452 } 1453 1454 // Get the output value corresponding to an input offset if we 1455 // couldn't find it in the hash table. 1456 1457 template<int size> 1458 typename elfcpp::Elf_types<size>::Elf_Addr 1459 Merged_symbol_value<size>::value_from_output_section( 1460 const Relobj* object, 1461 unsigned int input_shndx, 1462 typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const 1463 { 1464 section_offset_type output_offset; 1465 bool found = object->merge_output_offset(input_shndx, input_offset, 1466 &output_offset); 1467 1468 // If this assertion fails, it means that some relocation was 1469 // against a portion of an input merge section which we didn't map 1470 // to the output file and we didn't explicitly discard. We should 1471 // always map all portions of input merge sections. 1472 gold_assert(found); 1473 1474 if (output_offset == -1) 1475 return 0; 1476 else 1477 return this->output_start_address_ + output_offset; 1478 } 1479 1480 // Track_relocs methods. 1481 1482 // Initialize the class to track the relocs. This gets the object, 1483 // the reloc section index, and the type of the relocs. This returns 1484 // false if something goes wrong. 1485 1486 template<int size, bool big_endian> 1487 bool 1488 Track_relocs<size, big_endian>::initialize( 1489 Object* object, 1490 unsigned int reloc_shndx, 1491 unsigned int reloc_type) 1492 { 1493 // If RELOC_SHNDX is -1U, it means there is more than one reloc 1494 // section for the .eh_frame section. We can't handle that case. 1495 if (reloc_shndx == -1U) 1496 return false; 1497 1498 // If RELOC_SHNDX is 0, there is no reloc section. 1499 if (reloc_shndx == 0) 1500 return true; 1501 1502 // Get the contents of the reloc section. 1503 this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false); 1504 1505 if (reloc_type == elfcpp::SHT_REL) 1506 this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size; 1507 else if (reloc_type == elfcpp::SHT_RELA) 1508 this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size; 1509 else 1510 gold_unreachable(); 1511 1512 if (this->len_ % this->reloc_size_ != 0) 1513 { 1514 object->error(_("reloc section size %zu is not a multiple of " 1515 "reloc size %d\n"), 1516 static_cast<size_t>(this->len_), 1517 this->reloc_size_); 1518 return false; 1519 } 1520 1521 return true; 1522 } 1523 1524 // Return the offset of the next reloc, or -1 if there isn't one. 1525 1526 template<int size, bool big_endian> 1527 off_t 1528 Track_relocs<size, big_endian>::next_offset() const 1529 { 1530 if (this->pos_ >= this->len_) 1531 return -1; 1532 1533 // Rel and Rela start out the same, so we can always use Rel to find 1534 // the r_offset value. 1535 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_); 1536 return rel.get_r_offset(); 1537 } 1538 1539 // Return the index of the symbol referenced by the next reloc, or -1U 1540 // if there aren't any more relocs. 1541 1542 template<int size, bool big_endian> 1543 unsigned int 1544 Track_relocs<size, big_endian>::next_symndx() const 1545 { 1546 if (this->pos_ >= this->len_) 1547 return -1U; 1548 Sized_target<size, big_endian>* target 1549 = parameters->sized_target<size, big_endian>(); 1550 return target->get_r_sym(this->prelocs_ + this->pos_); 1551 } 1552 1553 // Return the addend of the next reloc, or 0 if there isn't one. 1554 1555 template<int size, bool big_endian> 1556 uint64_t 1557 Track_relocs<size, big_endian>::next_addend() const 1558 { 1559 if (this->pos_ >= this->len_) 1560 return 0; 1561 if (this->reloc_size_ == elfcpp::Elf_sizes<size>::rel_size) 1562 return 0; 1563 elfcpp::Rela<size, big_endian> rela(this->prelocs_ + this->pos_); 1564 return rela.get_r_addend(); 1565 } 1566 1567 // Advance to the next reloc whose r_offset is greater than or equal 1568 // to OFFSET. Return the number of relocs we skip. 1569 1570 template<int size, bool big_endian> 1571 int 1572 Track_relocs<size, big_endian>::advance(off_t offset) 1573 { 1574 int ret = 0; 1575 while (this->pos_ < this->len_) 1576 { 1577 // Rel and Rela start out the same, so we can always use Rel to 1578 // find the r_offset value. 1579 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_); 1580 if (static_cast<off_t>(rel.get_r_offset()) >= offset) 1581 break; 1582 ++ret; 1583 this->pos_ += this->reloc_size_; 1584 } 1585 return ret; 1586 } 1587 1588 // Instantiate the templates we need. 1589 1590 #ifdef HAVE_TARGET_32_LITTLE 1591 template 1592 void 1593 Sized_relobj_file<32, false>::do_read_relocs(Read_relocs_data* rd); 1594 #endif 1595 1596 #ifdef HAVE_TARGET_32_BIG 1597 template 1598 void 1599 Sized_relobj_file<32, true>::do_read_relocs(Read_relocs_data* rd); 1600 #endif 1601 1602 #ifdef HAVE_TARGET_64_LITTLE 1603 template 1604 void 1605 Sized_relobj_file<64, false>::do_read_relocs(Read_relocs_data* rd); 1606 #endif 1607 1608 #ifdef HAVE_TARGET_64_BIG 1609 template 1610 void 1611 Sized_relobj_file<64, true>::do_read_relocs(Read_relocs_data* rd); 1612 #endif 1613 1614 #ifdef HAVE_TARGET_32_LITTLE 1615 template 1616 void 1617 Sized_relobj_file<32, false>::do_gc_process_relocs(Symbol_table* symtab, 1618 Layout* layout, 1619 Read_relocs_data* rd); 1620 #endif 1621 1622 #ifdef HAVE_TARGET_32_BIG 1623 template 1624 void 1625 Sized_relobj_file<32, true>::do_gc_process_relocs(Symbol_table* symtab, 1626 Layout* layout, 1627 Read_relocs_data* rd); 1628 #endif 1629 1630 #ifdef HAVE_TARGET_64_LITTLE 1631 template 1632 void 1633 Sized_relobj_file<64, false>::do_gc_process_relocs(Symbol_table* symtab, 1634 Layout* layout, 1635 Read_relocs_data* rd); 1636 #endif 1637 1638 #ifdef HAVE_TARGET_64_BIG 1639 template 1640 void 1641 Sized_relobj_file<64, true>::do_gc_process_relocs(Symbol_table* symtab, 1642 Layout* layout, 1643 Read_relocs_data* rd); 1644 #endif 1645 1646 #ifdef HAVE_TARGET_32_LITTLE 1647 template 1648 void 1649 Sized_relobj_file<32, false>::do_scan_relocs(Symbol_table* symtab, 1650 Layout* layout, 1651 Read_relocs_data* rd); 1652 #endif 1653 1654 #ifdef HAVE_TARGET_32_BIG 1655 template 1656 void 1657 Sized_relobj_file<32, true>::do_scan_relocs(Symbol_table* symtab, 1658 Layout* layout, 1659 Read_relocs_data* rd); 1660 #endif 1661 1662 #ifdef HAVE_TARGET_64_LITTLE 1663 template 1664 void 1665 Sized_relobj_file<64, false>::do_scan_relocs(Symbol_table* symtab, 1666 Layout* layout, 1667 Read_relocs_data* rd); 1668 #endif 1669 1670 #ifdef HAVE_TARGET_64_BIG 1671 template 1672 void 1673 Sized_relobj_file<64, true>::do_scan_relocs(Symbol_table* symtab, 1674 Layout* layout, 1675 Read_relocs_data* rd); 1676 #endif 1677 1678 #ifdef HAVE_TARGET_32_LITTLE 1679 template 1680 void 1681 Sized_relobj_file<32, false>::do_relocate(const Symbol_table* symtab, 1682 const Layout* layout, 1683 Output_file* of); 1684 #endif 1685 1686 #ifdef HAVE_TARGET_32_BIG 1687 template 1688 void 1689 Sized_relobj_file<32, true>::do_relocate(const Symbol_table* symtab, 1690 const Layout* layout, 1691 Output_file* of); 1692 #endif 1693 1694 #ifdef HAVE_TARGET_64_LITTLE 1695 template 1696 void 1697 Sized_relobj_file<64, false>::do_relocate(const Symbol_table* symtab, 1698 const Layout* layout, 1699 Output_file* of); 1700 #endif 1701 1702 #ifdef HAVE_TARGET_64_BIG 1703 template 1704 void 1705 Sized_relobj_file<64, true>::do_relocate(const Symbol_table* symtab, 1706 const Layout* layout, 1707 Output_file* of); 1708 #endif 1709 1710 #ifdef HAVE_TARGET_32_LITTLE 1711 template 1712 void 1713 Sized_relobj_file<32, false>::do_relocate_sections( 1714 const Symbol_table* symtab, 1715 const Layout* layout, 1716 const unsigned char* pshdrs, 1717 Output_file* of, 1718 Views* pviews); 1719 1720 template 1721 unsigned char* 1722 Sized_relobj_file<32, false>::do_get_output_view( 1723 unsigned int shndx, 1724 section_size_type* plen) const; 1725 #endif 1726 1727 #ifdef HAVE_TARGET_32_BIG 1728 template 1729 void 1730 Sized_relobj_file<32, true>::do_relocate_sections( 1731 const Symbol_table* symtab, 1732 const Layout* layout, 1733 const unsigned char* pshdrs, 1734 Output_file* of, 1735 Views* pviews); 1736 1737 template 1738 unsigned char* 1739 Sized_relobj_file<32, true>::do_get_output_view( 1740 unsigned int shndx, 1741 section_size_type* plen) const; 1742 #endif 1743 1744 #ifdef HAVE_TARGET_64_LITTLE 1745 template 1746 void 1747 Sized_relobj_file<64, false>::do_relocate_sections( 1748 const Symbol_table* symtab, 1749 const Layout* layout, 1750 const unsigned char* pshdrs, 1751 Output_file* of, 1752 Views* pviews); 1753 1754 template 1755 unsigned char* 1756 Sized_relobj_file<64, false>::do_get_output_view( 1757 unsigned int shndx, 1758 section_size_type* plen) const; 1759 #endif 1760 1761 #ifdef HAVE_TARGET_64_BIG 1762 template 1763 void 1764 Sized_relobj_file<64, true>::do_relocate_sections( 1765 const Symbol_table* symtab, 1766 const Layout* layout, 1767 const unsigned char* pshdrs, 1768 Output_file* of, 1769 Views* pviews); 1770 1771 template 1772 unsigned char* 1773 Sized_relobj_file<64, true>::do_get_output_view( 1774 unsigned int shndx, 1775 section_size_type* plen) const; 1776 #endif 1777 1778 #ifdef HAVE_TARGET_32_LITTLE 1779 template 1780 void 1781 Sized_relobj_file<32, false>::initialize_input_to_output_maps(); 1782 1783 template 1784 void 1785 Sized_relobj_file<32, false>::free_input_to_output_maps(); 1786 #endif 1787 1788 #ifdef HAVE_TARGET_32_BIG 1789 template 1790 void 1791 Sized_relobj_file<32, true>::initialize_input_to_output_maps(); 1792 1793 template 1794 void 1795 Sized_relobj_file<32, true>::free_input_to_output_maps(); 1796 #endif 1797 1798 #ifdef HAVE_TARGET_64_LITTLE 1799 template 1800 void 1801 Sized_relobj_file<64, false>::initialize_input_to_output_maps(); 1802 1803 template 1804 void 1805 Sized_relobj_file<64, false>::free_input_to_output_maps(); 1806 #endif 1807 1808 #ifdef HAVE_TARGET_64_BIG 1809 template 1810 void 1811 Sized_relobj_file<64, true>::initialize_input_to_output_maps(); 1812 1813 template 1814 void 1815 Sized_relobj_file<64, true>::free_input_to_output_maps(); 1816 #endif 1817 1818 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) 1819 template 1820 class Merged_symbol_value<32>; 1821 #endif 1822 1823 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) 1824 template 1825 class Merged_symbol_value<64>; 1826 #endif 1827 1828 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) 1829 template 1830 class Symbol_value<32>; 1831 #endif 1832 1833 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) 1834 template 1835 class Symbol_value<64>; 1836 #endif 1837 1838 #ifdef HAVE_TARGET_32_LITTLE 1839 template 1840 class Track_relocs<32, false>; 1841 #endif 1842 1843 #ifdef HAVE_TARGET_32_BIG 1844 template 1845 class Track_relocs<32, true>; 1846 #endif 1847 1848 #ifdef HAVE_TARGET_64_LITTLE 1849 template 1850 class Track_relocs<64, false>; 1851 #endif 1852 1853 #ifdef HAVE_TARGET_64_BIG 1854 template 1855 class Track_relocs<64, true>; 1856 #endif 1857 1858 } // End namespace gold. 1859