| 1 | // reloc.cc -- relocate input files for gold. |
| 2 | |
| 3 | // Copyright 2006, 2007 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 "workqueue.h" |
| 26 | #include "object.h" |
| 27 | #include "symtab.h" |
| 28 | #include "output.h" |
| 29 | #include "reloc.h" |
| 30 | |
| 31 | namespace gold |
| 32 | { |
| 33 | |
| 34 | // Read_relocs methods. |
| 35 | |
| 36 | // These tasks just read the relocation information from the file. |
| 37 | // After reading it, the start another task to process the |
| 38 | // information. These tasks requires access to the file. |
| 39 | |
| 40 | Task::Is_runnable_type |
| 41 | Read_relocs::is_runnable(Workqueue*) |
| 42 | { |
| 43 | return this->object_->is_locked() ? IS_LOCKED : IS_RUNNABLE; |
| 44 | } |
| 45 | |
| 46 | // Lock the file. |
| 47 | |
| 48 | Task_locker* |
| 49 | Read_relocs::locks(Workqueue*) |
| 50 | { |
| 51 | return new Task_locker_obj<Object>(*this->object_); |
| 52 | } |
| 53 | |
| 54 | // Read the relocations and then start a Scan_relocs_task. |
| 55 | |
| 56 | void |
| 57 | Read_relocs::run(Workqueue* workqueue) |
| 58 | { |
| 59 | Read_relocs_data *rd = new Read_relocs_data; |
| 60 | this->object_->read_relocs(rd); |
| 61 | workqueue->queue_front(new Scan_relocs(this->options_, this->symtab_, |
| 62 | this->layout_, this->object_, rd, |
| 63 | this->symtab_lock_, this->blocker_)); |
| 64 | } |
| 65 | |
| 66 | // Scan_relocs methods. |
| 67 | |
| 68 | // These tasks scan the relocations read by Read_relocs and mark up |
| 69 | // the symbol table to indicate which relocations are required. We |
| 70 | // use a lock on the symbol table to keep them from interfering with |
| 71 | // each other. |
| 72 | |
| 73 | Task::Is_runnable_type |
| 74 | Scan_relocs::is_runnable(Workqueue*) |
| 75 | { |
| 76 | if (!this->symtab_lock_->is_writable() || this->object_->is_locked()) |
| 77 | return IS_LOCKED; |
| 78 | return IS_RUNNABLE; |
| 79 | } |
| 80 | |
| 81 | // Return the locks we hold: one on the file, one on the symbol table |
| 82 | // and one blocker. |
| 83 | |
| 84 | class Scan_relocs::Scan_relocs_locker : public Task_locker |
| 85 | { |
| 86 | public: |
| 87 | Scan_relocs_locker(Object* object, Task_token& symtab_lock, Task* task, |
| 88 | Task_token& blocker, Workqueue* workqueue) |
| 89 | : objlock_(*object), symtab_locker_(symtab_lock, task), |
| 90 | blocker_(blocker, workqueue) |
| 91 | { } |
| 92 | |
| 93 | private: |
| 94 | Task_locker_obj<Object> objlock_; |
| 95 | Task_locker_write symtab_locker_; |
| 96 | Task_locker_block blocker_; |
| 97 | }; |
| 98 | |
| 99 | Task_locker* |
| 100 | Scan_relocs::locks(Workqueue* workqueue) |
| 101 | { |
| 102 | return new Scan_relocs_locker(this->object_, *this->symtab_lock_, this, |
| 103 | *this->blocker_, workqueue); |
| 104 | } |
| 105 | |
| 106 | // Scan the relocs. |
| 107 | |
| 108 | void |
| 109 | Scan_relocs::run(Workqueue*) |
| 110 | { |
| 111 | this->object_->scan_relocs(this->options_, this->symtab_, this->layout_, |
| 112 | this->rd_); |
| 113 | delete this->rd_; |
| 114 | this->rd_ = NULL; |
| 115 | } |
| 116 | |
| 117 | // Relocate_task methods. |
| 118 | |
| 119 | // These tasks are always runnable. |
| 120 | |
| 121 | Task::Is_runnable_type |
| 122 | Relocate_task::is_runnable(Workqueue*) |
| 123 | { |
| 124 | return IS_RUNNABLE; |
| 125 | } |
| 126 | |
| 127 | // We want to lock the file while we run. We want to unblock |
| 128 | // FINAL_BLOCKER when we are done. |
| 129 | |
| 130 | class Relocate_task::Relocate_locker : public Task_locker |
| 131 | { |
| 132 | public: |
| 133 | Relocate_locker(Task_token& token, Workqueue* workqueue, |
| 134 | Object* object) |
| 135 | : blocker_(token, workqueue), objlock_(*object) |
| 136 | { } |
| 137 | |
| 138 | private: |
| 139 | Task_locker_block blocker_; |
| 140 | Task_locker_obj<Object> objlock_; |
| 141 | }; |
| 142 | |
| 143 | Task_locker* |
| 144 | Relocate_task::locks(Workqueue* workqueue) |
| 145 | { |
| 146 | return new Relocate_locker(*this->final_blocker_, workqueue, |
| 147 | this->object_); |
| 148 | } |
| 149 | |
| 150 | // Run the task. |
| 151 | |
| 152 | void |
| 153 | Relocate_task::run(Workqueue*) |
| 154 | { |
| 155 | this->object_->relocate(this->options_, this->symtab_, this->layout_, |
| 156 | this->of_); |
| 157 | } |
| 158 | |
| 159 | // Read the relocs and local symbols from the object file and store |
| 160 | // the information in RD. |
| 161 | |
| 162 | template<int size, bool big_endian> |
| 163 | void |
| 164 | Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd) |
| 165 | { |
| 166 | rd->relocs.clear(); |
| 167 | |
| 168 | unsigned int shnum = this->shnum(); |
| 169 | if (shnum == 0) |
| 170 | return; |
| 171 | |
| 172 | rd->relocs.reserve(shnum / 2); |
| 173 | |
| 174 | const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(), |
| 175 | shnum * This::shdr_size, |
| 176 | true); |
| 177 | // Skip the first, dummy, section. |
| 178 | const unsigned char *ps = pshdrs + This::shdr_size; |
| 179 | for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size) |
| 180 | { |
| 181 | typename This::Shdr shdr(ps); |
| 182 | |
| 183 | unsigned int sh_type = shdr.get_sh_type(); |
| 184 | if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA) |
| 185 | continue; |
| 186 | |
| 187 | unsigned int shndx = shdr.get_sh_info(); |
| 188 | if (shndx >= shnum) |
| 189 | { |
| 190 | fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"), |
| 191 | program_name, this->name().c_str(), i, shndx); |
| 192 | gold_exit(false); |
| 193 | } |
| 194 | |
| 195 | if (!this->is_section_included(shndx)) |
| 196 | continue; |
| 197 | |
| 198 | // We are scanning relocations in order to fill out the GOT and |
| 199 | // PLT sections. Relocations for sections which are not |
| 200 | // allocated (typically debugging sections) should not add new |
| 201 | // GOT and PLT entries. So we skip them. |
| 202 | typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size); |
| 203 | if ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) |
| 204 | continue; |
| 205 | |
| 206 | if (shdr.get_sh_link() != this->symtab_shndx_) |
| 207 | { |
| 208 | fprintf(stderr, |
| 209 | _("%s: %s: relocation section %u uses unexpected " |
| 210 | "symbol table %u\n"), |
| 211 | program_name, this->name().c_str(), i, shdr.get_sh_link()); |
| 212 | gold_exit(false); |
| 213 | } |
| 214 | |
| 215 | off_t sh_size = shdr.get_sh_size(); |
| 216 | |
| 217 | unsigned int reloc_size; |
| 218 | if (sh_type == elfcpp::SHT_REL) |
| 219 | reloc_size = elfcpp::Elf_sizes<size>::rel_size; |
| 220 | else |
| 221 | reloc_size = elfcpp::Elf_sizes<size>::rela_size; |
| 222 | if (reloc_size != shdr.get_sh_entsize()) |
| 223 | { |
| 224 | fprintf(stderr, |
| 225 | _("%s: %s: unexpected entsize for reloc section %u: " |
| 226 | "%lu != %u"), |
| 227 | program_name, this->name().c_str(), i, |
| 228 | static_cast<unsigned long>(shdr.get_sh_entsize()), |
| 229 | reloc_size); |
| 230 | gold_exit(false); |
| 231 | } |
| 232 | |
| 233 | size_t reloc_count = sh_size / reloc_size; |
| 234 | if (reloc_count * reloc_size != sh_size) |
| 235 | { |
| 236 | fprintf(stderr, _("%s: %s: reloc section %u size %lu uneven"), |
| 237 | program_name, this->name().c_str(), i, |
| 238 | static_cast<unsigned long>(sh_size)); |
| 239 | gold_exit(false); |
| 240 | } |
| 241 | |
| 242 | rd->relocs.push_back(Section_relocs()); |
| 243 | Section_relocs& sr(rd->relocs.back()); |
| 244 | sr.reloc_shndx = i; |
| 245 | sr.data_shndx = shndx; |
| 246 | sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size, |
| 247 | true); |
| 248 | sr.sh_type = sh_type; |
| 249 | sr.reloc_count = reloc_count; |
| 250 | } |
| 251 | |
| 252 | // Read the local symbols. |
| 253 | gold_assert(this->symtab_shndx_ != -1U); |
| 254 | if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0) |
| 255 | rd->local_symbols = NULL; |
| 256 | else |
| 257 | { |
| 258 | typename This::Shdr symtabshdr(pshdrs |
| 259 | + this->symtab_shndx_ * This::shdr_size); |
| 260 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
| 261 | const int sym_size = This::sym_size; |
| 262 | const unsigned int loccount = this->local_symbol_count_; |
| 263 | gold_assert(loccount == symtabshdr.get_sh_info()); |
| 264 | off_t locsize = loccount * sym_size; |
| 265 | rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(), |
| 266 | locsize, true); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // Scan the relocs and adjust the symbol table. This looks for |
| 271 | // relocations which require GOT/PLT/COPY relocations. |
| 272 | |
| 273 | template<int size, bool big_endian> |
| 274 | void |
| 275 | Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options, |
| 276 | Symbol_table* symtab, |
| 277 | Layout* layout, |
| 278 | Read_relocs_data* rd) |
| 279 | { |
| 280 | Sized_target<size, big_endian>* target = this->sized_target(); |
| 281 | |
| 282 | const unsigned char* local_symbols; |
| 283 | if (rd->local_symbols == NULL) |
| 284 | local_symbols = NULL; |
| 285 | else |
| 286 | local_symbols = rd->local_symbols->data(); |
| 287 | |
| 288 | for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin(); |
| 289 | p != rd->relocs.end(); |
| 290 | ++p) |
| 291 | { |
| 292 | target->scan_relocs(options, symtab, layout, this, p->data_shndx, |
| 293 | p->sh_type, p->contents->data(), p->reloc_count, |
| 294 | this->local_symbol_count_, |
| 295 | local_symbols, |
| 296 | this->symbols_); |
| 297 | delete p->contents; |
| 298 | p->contents = NULL; |
| 299 | } |
| 300 | |
| 301 | if (rd->local_symbols != NULL) |
| 302 | { |
| 303 | delete rd->local_symbols; |
| 304 | rd->local_symbols = NULL; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // Relocate the input sections and write out the local symbols. |
| 309 | |
| 310 | template<int size, bool big_endian> |
| 311 | void |
| 312 | Sized_relobj<size, big_endian>::do_relocate(const General_options& options, |
| 313 | const Symbol_table* symtab, |
| 314 | const Layout* layout, |
| 315 | Output_file* of) |
| 316 | { |
| 317 | unsigned int shnum = this->shnum(); |
| 318 | |
| 319 | // Read the section headers. |
| 320 | const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(), |
| 321 | shnum * This::shdr_size, |
| 322 | true); |
| 323 | |
| 324 | Views views; |
| 325 | views.resize(shnum); |
| 326 | |
| 327 | // Make two passes over the sections. The first one copies the |
| 328 | // section data to the output file. The second one applies |
| 329 | // relocations. |
| 330 | |
| 331 | this->write_sections(pshdrs, of, &views); |
| 332 | |
| 333 | // Apply relocations. |
| 334 | |
| 335 | this->relocate_sections(options, symtab, layout, pshdrs, &views); |
| 336 | |
| 337 | // Write out the accumulated views. |
| 338 | for (unsigned int i = 1; i < shnum; ++i) |
| 339 | { |
| 340 | if (views[i].view != NULL) |
| 341 | of->write_output_view(views[i].offset, views[i].view_size, |
| 342 | views[i].view); |
| 343 | } |
| 344 | |
| 345 | // Write out the local symbols. |
| 346 | this->write_local_symbols(of, layout->sympool()); |
| 347 | } |
| 348 | |
| 349 | // Write section data to the output file. PSHDRS points to the |
| 350 | // section headers. Record the views in *PVIEWS for use when |
| 351 | // relocating. |
| 352 | |
| 353 | template<int size, bool big_endian> |
| 354 | void |
| 355 | Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs, |
| 356 | Output_file* of, |
| 357 | Views* pviews) |
| 358 | { |
| 359 | unsigned int shnum = this->shnum(); |
| 360 | std::vector<Map_to_output>& map_sections(this->map_to_output()); |
| 361 | |
| 362 | const unsigned char* p = pshdrs + This::shdr_size; |
| 363 | for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) |
| 364 | { |
| 365 | View_size* pvs = &(*pviews)[i]; |
| 366 | |
| 367 | pvs->view = NULL; |
| 368 | |
| 369 | if (map_sections[i].offset == -1) |
| 370 | continue; |
| 371 | |
| 372 | const Output_section* os = map_sections[i].output_section; |
| 373 | if (os == NULL) |
| 374 | continue; |
| 375 | |
| 376 | typename This::Shdr shdr(p); |
| 377 | |
| 378 | if (shdr.get_sh_type() == elfcpp::SHT_NOBITS) |
| 379 | continue; |
| 380 | |
| 381 | off_t start = os->offset() + map_sections[i].offset; |
| 382 | off_t sh_size = shdr.get_sh_size(); |
| 383 | |
| 384 | if (sh_size == 0) |
| 385 | continue; |
| 386 | |
| 387 | gold_assert(map_sections[i].offset >= 0 |
| 388 | && map_sections[i].offset + sh_size <= os->data_size()); |
| 389 | |
| 390 | unsigned char* view = of->get_output_view(start, sh_size); |
| 391 | this->read(shdr.get_sh_offset(), sh_size, view); |
| 392 | |
| 393 | pvs->view = view; |
| 394 | pvs->address = os->address() + map_sections[i].offset; |
| 395 | pvs->offset = start; |
| 396 | pvs->view_size = sh_size; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // Relocate section data. VIEWS points to the section data as views |
| 401 | // in the output file. |
| 402 | |
| 403 | template<int size, bool big_endian> |
| 404 | void |
| 405 | Sized_relobj<size, big_endian>::relocate_sections( |
| 406 | const General_options& options, |
| 407 | const Symbol_table* symtab, |
| 408 | const Layout* layout, |
| 409 | const unsigned char* pshdrs, |
| 410 | Views* pviews) |
| 411 | { |
| 412 | unsigned int shnum = this->shnum(); |
| 413 | Sized_target<size, big_endian>* target = this->sized_target(); |
| 414 | |
| 415 | Relocate_info<size, big_endian> relinfo; |
| 416 | relinfo.options = &options; |
| 417 | relinfo.symtab = symtab; |
| 418 | relinfo.layout = layout; |
| 419 | relinfo.object = this; |
| 420 | relinfo.local_symbol_count = this->local_symbol_count_; |
| 421 | relinfo.local_values = &this->local_values_; |
| 422 | relinfo.symbols = this->symbols_; |
| 423 | |
| 424 | const unsigned char* p = pshdrs + This::shdr_size; |
| 425 | for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) |
| 426 | { |
| 427 | typename This::Shdr shdr(p); |
| 428 | |
| 429 | unsigned int sh_type = shdr.get_sh_type(); |
| 430 | if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA) |
| 431 | continue; |
| 432 | |
| 433 | unsigned int index = shdr.get_sh_info(); |
| 434 | if (index >= this->shnum()) |
| 435 | { |
| 436 | fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"), |
| 437 | program_name, this->name().c_str(), i, index); |
| 438 | gold_exit(false); |
| 439 | } |
| 440 | |
| 441 | if (!this->is_section_included(index)) |
| 442 | { |
| 443 | // This relocation section is against a section which we |
| 444 | // discarded. |
| 445 | continue; |
| 446 | } |
| 447 | |
| 448 | gold_assert((*pviews)[index].view != NULL); |
| 449 | |
| 450 | if (shdr.get_sh_link() != this->symtab_shndx_) |
| 451 | { |
| 452 | fprintf(stderr, |
| 453 | _("%s: %s: relocation section %u uses unexpected " |
| 454 | "symbol table %u\n"), |
| 455 | program_name, this->name().c_str(), i, shdr.get_sh_link()); |
| 456 | gold_exit(false); |
| 457 | } |
| 458 | |
| 459 | off_t sh_size = shdr.get_sh_size(); |
| 460 | const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(), |
| 461 | sh_size, false); |
| 462 | |
| 463 | unsigned int reloc_size; |
| 464 | if (sh_type == elfcpp::SHT_REL) |
| 465 | reloc_size = elfcpp::Elf_sizes<size>::rel_size; |
| 466 | else |
| 467 | reloc_size = elfcpp::Elf_sizes<size>::rela_size; |
| 468 | |
| 469 | if (reloc_size != shdr.get_sh_entsize()) |
| 470 | { |
| 471 | fprintf(stderr, |
| 472 | _("%s: %s: unexpected entsize for reloc section %u: " |
| 473 | "%lu != %u"), |
| 474 | program_name, this->name().c_str(), i, |
| 475 | static_cast<unsigned long>(shdr.get_sh_entsize()), |
| 476 | reloc_size); |
| 477 | gold_exit(false); |
| 478 | } |
| 479 | |
| 480 | size_t reloc_count = sh_size / reloc_size; |
| 481 | if (reloc_count * reloc_size != sh_size) |
| 482 | { |
| 483 | fprintf(stderr, _("%s: %s: reloc section %u size %lu uneven"), |
| 484 | program_name, this->name().c_str(), i, |
| 485 | static_cast<unsigned long>(sh_size)); |
| 486 | gold_exit(false); |
| 487 | } |
| 488 | |
| 489 | relinfo.reloc_shndx = i; |
| 490 | relinfo.data_shndx = index; |
| 491 | target->relocate_section(&relinfo, |
| 492 | sh_type, |
| 493 | prelocs, |
| 494 | reloc_count, |
| 495 | (*pviews)[index].view, |
| 496 | (*pviews)[index].address, |
| 497 | (*pviews)[index].view_size); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | // Copy_relocs::Copy_reloc_entry methods. |
| 502 | |
| 503 | // Return whether we should emit this reloc. We should emit it if the |
| 504 | // symbol is still defined in a dynamic object. If we should not emit |
| 505 | // it, we clear it, to save ourselves the test next time. |
| 506 | |
| 507 | template<int size, bool big_endian> |
| 508 | bool |
| 509 | Copy_relocs<size, big_endian>::Copy_reloc_entry::should_emit() |
| 510 | { |
| 511 | if (this->sym_ == NULL) |
| 512 | return false; |
| 513 | if (this->sym_->is_from_dynobj()) |
| 514 | return true; |
| 515 | this->sym_ = NULL; |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | // Emit a reloc into a SHT_REL section. |
| 520 | |
| 521 | template<int size, bool big_endian> |
| 522 | void |
| 523 | Copy_relocs<size, big_endian>::Copy_reloc_entry::emit( |
| 524 | Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* reloc_data) |
| 525 | { |
| 526 | this->sym_->set_needs_dynsym_entry(); |
| 527 | reloc_data->add_global(this->sym_, this->reloc_type_, this->relobj_, |
| 528 | this->shndx_, this->address_); |
| 529 | } |
| 530 | |
| 531 | // Emit a reloc into a SHT_RELA section. |
| 532 | |
| 533 | template<int size, bool big_endian> |
| 534 | void |
| 535 | Copy_relocs<size, big_endian>::Copy_reloc_entry::emit( |
| 536 | Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* reloc_data) |
| 537 | { |
| 538 | this->sym_->set_needs_dynsym_entry(); |
| 539 | reloc_data->add_global(this->sym_, this->reloc_type_, this->relobj_, |
| 540 | this->shndx_, this->address_, this->addend_); |
| 541 | } |
| 542 | |
| 543 | // Copy_relocs methods. |
| 544 | |
| 545 | // Return whether we need a COPY reloc for a relocation against GSYM. |
| 546 | // The relocation is being applied to section SHNDX in OBJECT. |
| 547 | |
| 548 | template<int size, bool big_endian> |
| 549 | bool |
| 550 | Copy_relocs<size, big_endian>::need_copy_reloc( |
| 551 | const General_options*, |
| 552 | Relobj* object, |
| 553 | unsigned int shndx, |
| 554 | Sized_symbol<size>* sym) |
| 555 | { |
| 556 | // FIXME: Handle -z nocopyrelocs. |
| 557 | |
| 558 | if (sym->symsize() == 0) |
| 559 | return false; |
| 560 | |
| 561 | // If this is a readonly section, then we need a COPY reloc. |
| 562 | // Otherwise we can use a dynamic reloc. |
| 563 | if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0) |
| 564 | return true; |
| 565 | |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | // Save a Rel reloc. |
| 570 | |
| 571 | template<int size, bool big_endian> |
| 572 | void |
| 573 | Copy_relocs<size, big_endian>::save( |
| 574 | Symbol* sym, |
| 575 | Relobj* relobj, |
| 576 | unsigned int shndx, |
| 577 | const elfcpp::Rel<size, big_endian>& rel) |
| 578 | { |
| 579 | unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info()); |
| 580 | this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx, |
| 581 | rel.get_r_offset(), 0)); |
| 582 | } |
| 583 | |
| 584 | // Save a Rela reloc. |
| 585 | |
| 586 | template<int size, bool big_endian> |
| 587 | void |
| 588 | Copy_relocs<size, big_endian>::save( |
| 589 | Symbol* sym, |
| 590 | Relobj* relobj, |
| 591 | unsigned int shndx, |
| 592 | const elfcpp::Rela<size, big_endian>& rela) |
| 593 | { |
| 594 | unsigned int reloc_type = elfcpp::elf_r_type<size>(rela.get_r_info()); |
| 595 | this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx, |
| 596 | rela.get_r_offset(), |
| 597 | rela.get_r_addend())); |
| 598 | } |
| 599 | |
| 600 | // Return whether there are any relocs to emit. We don't want to emit |
| 601 | // a reloc if the symbol is no longer defined in a dynamic object. |
| 602 | |
| 603 | template<int size, bool big_endian> |
| 604 | bool |
| 605 | Copy_relocs<size, big_endian>::any_to_emit() |
| 606 | { |
| 607 | for (typename Copy_reloc_entries::iterator p = this->entries_.begin(); |
| 608 | p != this->entries_.end(); |
| 609 | ++p) |
| 610 | { |
| 611 | if (p->should_emit()) |
| 612 | return true; |
| 613 | } |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | // Emit relocs. |
| 618 | |
| 619 | template<int size, bool big_endian> |
| 620 | template<int sh_type> |
| 621 | void |
| 622 | Copy_relocs<size, big_endian>::emit( |
| 623 | Output_data_reloc<sh_type, true, size, big_endian>* reloc_data) |
| 624 | { |
| 625 | for (typename Copy_reloc_entries::iterator p = this->entries_.begin(); |
| 626 | p != this->entries_.end(); |
| 627 | ++p) |
| 628 | { |
| 629 | if (p->should_emit()) |
| 630 | p->emit(reloc_data); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | // Instantiate the templates we need. We could use the configure |
| 635 | // script to restrict this to only the ones for implemented targets. |
| 636 | |
| 637 | #ifdef HAVE_TARGET_32_LITTLE |
| 638 | template |
| 639 | void |
| 640 | Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd); |
| 641 | #endif |
| 642 | |
| 643 | #ifdef HAVE_TARGET_32_BIG |
| 644 | template |
| 645 | void |
| 646 | Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd); |
| 647 | #endif |
| 648 | |
| 649 | #ifdef HAVE_TARGET_64_LITTLE |
| 650 | template |
| 651 | void |
| 652 | Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd); |
| 653 | #endif |
| 654 | |
| 655 | #ifdef HAVE_TARGET_64_BIG |
| 656 | template |
| 657 | void |
| 658 | Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd); |
| 659 | #endif |
| 660 | |
| 661 | #ifdef HAVE_TARGET_32_LITTLE |
| 662 | template |
| 663 | void |
| 664 | Sized_relobj<32, false>::do_scan_relocs(const General_options& options, |
| 665 | Symbol_table* symtab, |
| 666 | Layout* layout, |
| 667 | Read_relocs_data* rd); |
| 668 | #endif |
| 669 | |
| 670 | #ifdef HAVE_TARGET_32_BIG |
| 671 | template |
| 672 | void |
| 673 | Sized_relobj<32, true>::do_scan_relocs(const General_options& options, |
| 674 | Symbol_table* symtab, |
| 675 | Layout* layout, |
| 676 | Read_relocs_data* rd); |
| 677 | #endif |
| 678 | |
| 679 | #ifdef HAVE_TARGET_64_LITTLE |
| 680 | template |
| 681 | void |
| 682 | Sized_relobj<64, false>::do_scan_relocs(const General_options& options, |
| 683 | Symbol_table* symtab, |
| 684 | Layout* layout, |
| 685 | Read_relocs_data* rd); |
| 686 | #endif |
| 687 | |
| 688 | #ifdef HAVE_TARGET_64_BIG |
| 689 | template |
| 690 | void |
| 691 | Sized_relobj<64, true>::do_scan_relocs(const General_options& options, |
| 692 | Symbol_table* symtab, |
| 693 | Layout* layout, |
| 694 | Read_relocs_data* rd); |
| 695 | #endif |
| 696 | |
| 697 | #ifdef HAVE_TARGET_32_LITTLE |
| 698 | template |
| 699 | void |
| 700 | Sized_relobj<32, false>::do_relocate(const General_options& options, |
| 701 | const Symbol_table* symtab, |
| 702 | const Layout* layout, |
| 703 | Output_file* of); |
| 704 | #endif |
| 705 | |
| 706 | #ifdef HAVE_TARGET_32_BIG |
| 707 | template |
| 708 | void |
| 709 | Sized_relobj<32, true>::do_relocate(const General_options& options, |
| 710 | const Symbol_table* symtab, |
| 711 | const Layout* layout, |
| 712 | Output_file* of); |
| 713 | #endif |
| 714 | |
| 715 | #ifdef HAVE_TARGET_64_LITTLE |
| 716 | template |
| 717 | void |
| 718 | Sized_relobj<64, false>::do_relocate(const General_options& options, |
| 719 | const Symbol_table* symtab, |
| 720 | const Layout* layout, |
| 721 | Output_file* of); |
| 722 | #endif |
| 723 | |
| 724 | #ifdef HAVE_TARGET_64_BIG |
| 725 | template |
| 726 | void |
| 727 | Sized_relobj<64, true>::do_relocate(const General_options& options, |
| 728 | const Symbol_table* symtab, |
| 729 | const Layout* layout, |
| 730 | Output_file* of); |
| 731 | #endif |
| 732 | |
| 733 | #ifdef HAVE_TARGET_32_LITTLE |
| 734 | template |
| 735 | class Copy_relocs<32, false>; |
| 736 | #endif |
| 737 | |
| 738 | #ifdef HAVE_TARGET_32_BIG |
| 739 | template |
| 740 | class Copy_relocs<32, true>; |
| 741 | #endif |
| 742 | |
| 743 | #ifdef HAVE_TARGET_64_LITTLE |
| 744 | template |
| 745 | class Copy_relocs<64, false>; |
| 746 | #endif |
| 747 | |
| 748 | #ifdef HAVE_TARGET_64_BIG |
| 749 | template |
| 750 | class Copy_relocs<64, true>; |
| 751 | #endif |
| 752 | |
| 753 | #ifdef HAVE_TARGET_32_LITTLE |
| 754 | template |
| 755 | void |
| 756 | Copy_relocs<32, false>::emit<elfcpp::SHT_REL>( |
| 757 | Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*); |
| 758 | #endif |
| 759 | |
| 760 | #ifdef HAVE_TARGET_32_BIG |
| 761 | template |
| 762 | void |
| 763 | Copy_relocs<32, true>::emit<elfcpp::SHT_REL>( |
| 764 | Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*); |
| 765 | #endif |
| 766 | |
| 767 | #ifdef HAVE_TARGET_64_LITTLE |
| 768 | template |
| 769 | void |
| 770 | Copy_relocs<64, false>::emit<elfcpp::SHT_REL>( |
| 771 | Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*); |
| 772 | #endif |
| 773 | |
| 774 | #ifdef HAVE_TARGET_64_BIG |
| 775 | template |
| 776 | void |
| 777 | Copy_relocs<64, true>::emit<elfcpp::SHT_REL>( |
| 778 | Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*); |
| 779 | #endif |
| 780 | |
| 781 | #ifdef HAVE_TARGET_32_LITTLE |
| 782 | template |
| 783 | void |
| 784 | Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>( |
| 785 | Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*); |
| 786 | #endif |
| 787 | |
| 788 | #ifdef HAVE_TARGET_32_BIG |
| 789 | template |
| 790 | void |
| 791 | Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>( |
| 792 | Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*); |
| 793 | #endif |
| 794 | |
| 795 | #ifdef HAVE_TARGET_64_LITTLE |
| 796 | template |
| 797 | void |
| 798 | Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>( |
| 799 | Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*); |
| 800 | #endif |
| 801 | |
| 802 | #ifdef HAVE_TARGET_64_BIG |
| 803 | template |
| 804 | void |
| 805 | Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>( |
| 806 | Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*); |
| 807 | #endif |
| 808 | |
| 809 | } // End namespace gold. |