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