opcodes/
[binutils.git] / gold / reloc.cc
blob7647edf511038ebd842830ebcbcb45f0c3cacf16
1 // reloc.cc -- relocate input files for gold.
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include "workqueue.h"
26 #include "object.h"
27 #include "symtab.h"
28 #include "output.h"
29 #include "reloc.h"
31 namespace gold
34 // Read_relocs methods.
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.
40 Task::Is_runnable_type
41 Read_relocs::is_runnable(Workqueue*)
43 return this->object_->is_locked() ? IS_LOCKED : IS_RUNNABLE;
46 // Lock the file.
48 Task_locker*
49 Read_relocs::locks(Workqueue*)
51 return new Task_locker_obj<Object>(*this->object_);
54 // Read the relocations and then start a Scan_relocs_task.
56 void
57 Read_relocs::run(Workqueue* workqueue)
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_));
66 // Scan_relocs methods.
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.
73 Task::Is_runnable_type
74 Scan_relocs::is_runnable(Workqueue*)
76 if (!this->symtab_lock_->is_writable() || this->object_->is_locked())
77 return IS_LOCKED;
78 return IS_RUNNABLE;
81 // Return the locks we hold: one on the file, one on the symbol table
82 // and one blocker.
84 class Scan_relocs::Scan_relocs_locker : public Task_locker
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 { }
93 private:
94 Task_locker_obj<Object> objlock_;
95 Task_locker_write symtab_locker_;
96 Task_locker_block blocker_;
99 Task_locker*
100 Scan_relocs::locks(Workqueue* workqueue)
102 return new Scan_relocs_locker(this->object_, *this->symtab_lock_, this,
103 *this->blocker_, workqueue);
106 // Scan the relocs.
108 void
109 Scan_relocs::run(Workqueue*)
111 this->object_->scan_relocs(this->options_, this->symtab_, this->layout_,
112 this->rd_);
113 delete this->rd_;
114 this->rd_ = NULL;
117 // Relocate_task methods.
119 // These tasks are always runnable.
121 Task::Is_runnable_type
122 Relocate_task::is_runnable(Workqueue*)
124 return IS_RUNNABLE;
127 // We want to lock the file while we run. We want to unblock
128 // FINAL_BLOCKER when we are done.
130 class Relocate_task::Relocate_locker : public Task_locker
132 public:
133 Relocate_locker(Task_token& token, Workqueue* workqueue,
134 Object* object)
135 : blocker_(token, workqueue), objlock_(*object)
138 private:
139 Task_locker_block blocker_;
140 Task_locker_obj<Object> objlock_;
143 Task_locker*
144 Relocate_task::locks(Workqueue* workqueue)
146 return new Relocate_locker(*this->final_blocker_, workqueue,
147 this->object_);
150 // Run the task.
152 void
153 Relocate_task::run(Workqueue*)
155 this->object_->relocate(this->options_, this->symtab_, this->layout_,
156 this->of_);
159 // Read the relocs and local symbols from the object file and store
160 // the information in RD.
162 template<int size, bool big_endian>
163 void
164 Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
166 rd->relocs.clear();
168 unsigned int shnum = this->shnum();
169 if (shnum == 0)
170 return;
172 rd->relocs.reserve(shnum / 2);
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)
181 typename This::Shdr shdr(ps);
183 unsigned int sh_type = shdr.get_sh_type();
184 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
185 continue;
187 unsigned int shndx = shdr.get_sh_info();
188 if (shndx >= shnum)
190 this->error(_("relocation section %u has bad info %u"),
191 i, shndx);
192 continue;
195 if (!this->is_section_included(shndx))
196 continue;
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;
206 if (shdr.get_sh_link() != this->symtab_shndx_)
208 this->error(_("relocation section %u uses unexpected "
209 "symbol table %u"),
210 i, shdr.get_sh_link());
211 continue;
214 off_t sh_size = shdr.get_sh_size();
216 unsigned int reloc_size;
217 if (sh_type == elfcpp::SHT_REL)
218 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
219 else
220 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
221 if (reloc_size != shdr.get_sh_entsize())
223 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
224 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
225 reloc_size);
226 continue;
229 size_t reloc_count = sh_size / reloc_size;
230 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
232 this->error(_("reloc section %u size %lu uneven"),
233 i, static_cast<unsigned long>(sh_size));
234 continue;
237 rd->relocs.push_back(Section_relocs());
238 Section_relocs& sr(rd->relocs.back());
239 sr.reloc_shndx = i;
240 sr.data_shndx = shndx;
241 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
242 true);
243 sr.sh_type = sh_type;
244 sr.reloc_count = reloc_count;
247 // Read the local symbols.
248 gold_assert(this->symtab_shndx_ != -1U);
249 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
250 rd->local_symbols = NULL;
251 else
253 typename This::Shdr symtabshdr(pshdrs
254 + this->symtab_shndx_ * This::shdr_size);
255 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
256 const int sym_size = This::sym_size;
257 const unsigned int loccount = this->local_symbol_count_;
258 gold_assert(loccount == symtabshdr.get_sh_info());
259 off_t locsize = loccount * sym_size;
260 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
261 locsize, true);
265 // Scan the relocs and adjust the symbol table. This looks for
266 // relocations which require GOT/PLT/COPY relocations.
268 template<int size, bool big_endian>
269 void
270 Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
271 Symbol_table* symtab,
272 Layout* layout,
273 Read_relocs_data* rd)
275 Sized_target<size, big_endian>* target = this->sized_target();
277 const unsigned char* local_symbols;
278 if (rd->local_symbols == NULL)
279 local_symbols = NULL;
280 else
281 local_symbols = rd->local_symbols->data();
283 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
284 p != rd->relocs.end();
285 ++p)
287 target->scan_relocs(options, symtab, layout, this, p->data_shndx,
288 p->sh_type, p->contents->data(), p->reloc_count,
289 this->local_symbol_count_,
290 local_symbols,
291 this->symbols_);
292 delete p->contents;
293 p->contents = NULL;
296 if (rd->local_symbols != NULL)
298 delete rd->local_symbols;
299 rd->local_symbols = NULL;
303 // Relocate the input sections and write out the local symbols.
305 template<int size, bool big_endian>
306 void
307 Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
308 const Symbol_table* symtab,
309 const Layout* layout,
310 Output_file* of)
312 unsigned int shnum = this->shnum();
314 // Read the section headers.
315 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
316 shnum * This::shdr_size,
317 true);
319 Views views;
320 views.resize(shnum);
322 // Make two passes over the sections. The first one copies the
323 // section data to the output file. The second one applies
324 // relocations.
326 this->write_sections(pshdrs, of, &views);
328 // Apply relocations.
330 this->relocate_sections(options, symtab, layout, pshdrs, &views);
332 // Write out the accumulated views.
333 for (unsigned int i = 1; i < shnum; ++i)
335 if (views[i].view != NULL)
336 of->write_output_view(views[i].offset, views[i].view_size,
337 views[i].view);
340 // Write out the local symbols.
341 this->write_local_symbols(of, layout->sympool());
344 // Write section data to the output file. PSHDRS points to the
345 // section headers. Record the views in *PVIEWS for use when
346 // relocating.
348 template<int size, bool big_endian>
349 void
350 Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
351 Output_file* of,
352 Views* pviews)
354 unsigned int shnum = this->shnum();
355 std::vector<Map_to_output>& map_sections(this->map_to_output());
357 const unsigned char* p = pshdrs + This::shdr_size;
358 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
360 View_size* pvs = &(*pviews)[i];
362 pvs->view = NULL;
364 if (map_sections[i].offset == -1)
365 continue;
367 const Output_section* os = map_sections[i].output_section;
368 if (os == NULL)
369 continue;
371 typename This::Shdr shdr(p);
373 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
374 continue;
376 off_t start = os->offset() + map_sections[i].offset;
377 off_t sh_size = shdr.get_sh_size();
379 if (sh_size == 0)
380 continue;
382 gold_assert(map_sections[i].offset >= 0
383 && map_sections[i].offset + sh_size <= os->data_size());
385 unsigned char* view = of->get_output_view(start, sh_size);
386 this->read(shdr.get_sh_offset(), sh_size, view);
388 pvs->view = view;
389 pvs->address = os->address() + map_sections[i].offset;
390 pvs->offset = start;
391 pvs->view_size = sh_size;
395 // Relocate section data. VIEWS points to the section data as views
396 // in the output file.
398 template<int size, bool big_endian>
399 void
400 Sized_relobj<size, big_endian>::relocate_sections(
401 const General_options& options,
402 const Symbol_table* symtab,
403 const Layout* layout,
404 const unsigned char* pshdrs,
405 Views* pviews)
407 unsigned int shnum = this->shnum();
408 Sized_target<size, big_endian>* target = this->sized_target();
410 Relocate_info<size, big_endian> relinfo;
411 relinfo.options = &options;
412 relinfo.symtab = symtab;
413 relinfo.layout = layout;
414 relinfo.object = this;
415 relinfo.local_symbol_count = this->local_symbol_count_;
416 relinfo.local_values = &this->local_values_;
417 relinfo.symbols = this->symbols_;
419 const unsigned char* p = pshdrs + This::shdr_size;
420 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
422 typename This::Shdr shdr(p);
424 unsigned int sh_type = shdr.get_sh_type();
425 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
426 continue;
428 unsigned int index = shdr.get_sh_info();
429 if (index >= this->shnum())
431 this->error(_("relocation section %u has bad info %u"),
432 i, index);
433 continue;
436 if (!this->is_section_included(index))
438 // This relocation section is against a section which we
439 // discarded.
440 continue;
443 gold_assert((*pviews)[index].view != NULL);
445 if (shdr.get_sh_link() != this->symtab_shndx_)
447 gold_error(_("relocation section %u uses unexpected "
448 "symbol table %u"),
449 i, shdr.get_sh_link());
450 continue;
453 off_t sh_size = shdr.get_sh_size();
454 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
455 sh_size, false);
457 unsigned int reloc_size;
458 if (sh_type == elfcpp::SHT_REL)
459 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
460 else
461 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
463 if (reloc_size != shdr.get_sh_entsize())
465 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
466 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
467 reloc_size);
468 continue;
471 size_t reloc_count = sh_size / reloc_size;
472 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
474 gold_error(_("reloc section %u size %lu uneven"),
475 i, static_cast<unsigned long>(sh_size));
476 continue;
479 relinfo.reloc_shndx = i;
480 relinfo.data_shndx = index;
481 target->relocate_section(&relinfo,
482 sh_type,
483 prelocs,
484 reloc_count,
485 (*pviews)[index].view,
486 (*pviews)[index].address,
487 (*pviews)[index].view_size);
491 // Copy_relocs::Copy_reloc_entry methods.
493 // Return whether we should emit this reloc. We should emit it if the
494 // symbol is still defined in a dynamic object. If we should not emit
495 // it, we clear it, to save ourselves the test next time.
497 template<int size, bool big_endian>
498 bool
499 Copy_relocs<size, big_endian>::Copy_reloc_entry::should_emit()
501 if (this->sym_ == NULL)
502 return false;
503 if (this->sym_->is_from_dynobj())
504 return true;
505 this->sym_ = NULL;
506 return false;
509 // Emit a reloc into a SHT_REL section.
511 template<int size, bool big_endian>
512 void
513 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
514 Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* reloc_data)
516 this->sym_->set_needs_dynsym_entry();
517 reloc_data->add_global(this->sym_, this->reloc_type_, this->relobj_,
518 this->shndx_, this->address_);
521 // Emit a reloc into a SHT_RELA section.
523 template<int size, bool big_endian>
524 void
525 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
526 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* reloc_data)
528 this->sym_->set_needs_dynsym_entry();
529 reloc_data->add_global(this->sym_, this->reloc_type_, this->relobj_,
530 this->shndx_, this->address_, this->addend_);
533 // Copy_relocs methods.
535 // Return whether we need a COPY reloc for a relocation against GSYM.
536 // The relocation is being applied to section SHNDX in OBJECT.
538 template<int size, bool big_endian>
539 bool
540 Copy_relocs<size, big_endian>::need_copy_reloc(
541 const General_options*,
542 Relobj* object,
543 unsigned int shndx,
544 Sized_symbol<size>* sym)
546 // FIXME: Handle -z nocopyrelocs.
548 if (sym->symsize() == 0)
549 return false;
551 // If this is a readonly section, then we need a COPY reloc.
552 // Otherwise we can use a dynamic reloc.
553 if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
554 return true;
556 return false;
559 // Save a Rel reloc.
561 template<int size, bool big_endian>
562 void
563 Copy_relocs<size, big_endian>::save(
564 Symbol* sym,
565 Relobj* relobj,
566 unsigned int shndx,
567 const elfcpp::Rel<size, big_endian>& rel)
569 unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info());
570 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
571 rel.get_r_offset(), 0));
574 // Save a Rela reloc.
576 template<int size, bool big_endian>
577 void
578 Copy_relocs<size, big_endian>::save(
579 Symbol* sym,
580 Relobj* relobj,
581 unsigned int shndx,
582 const elfcpp::Rela<size, big_endian>& rela)
584 unsigned int reloc_type = elfcpp::elf_r_type<size>(rela.get_r_info());
585 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
586 rela.get_r_offset(),
587 rela.get_r_addend()));
590 // Return whether there are any relocs to emit. We don't want to emit
591 // a reloc if the symbol is no longer defined in a dynamic object.
593 template<int size, bool big_endian>
594 bool
595 Copy_relocs<size, big_endian>::any_to_emit()
597 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
598 p != this->entries_.end();
599 ++p)
601 if (p->should_emit())
602 return true;
604 return false;
607 // Emit relocs.
609 template<int size, bool big_endian>
610 template<int sh_type>
611 void
612 Copy_relocs<size, big_endian>::emit(
613 Output_data_reloc<sh_type, true, size, big_endian>* reloc_data)
615 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
616 p != this->entries_.end();
617 ++p)
619 if (p->should_emit())
620 p->emit(reloc_data);
624 // Instantiate the templates we need. We could use the configure
625 // script to restrict this to only the ones for implemented targets.
627 #ifdef HAVE_TARGET_32_LITTLE
628 template
629 void
630 Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
631 #endif
633 #ifdef HAVE_TARGET_32_BIG
634 template
635 void
636 Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
637 #endif
639 #ifdef HAVE_TARGET_64_LITTLE
640 template
641 void
642 Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
643 #endif
645 #ifdef HAVE_TARGET_64_BIG
646 template
647 void
648 Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
649 #endif
651 #ifdef HAVE_TARGET_32_LITTLE
652 template
653 void
654 Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
655 Symbol_table* symtab,
656 Layout* layout,
657 Read_relocs_data* rd);
658 #endif
660 #ifdef HAVE_TARGET_32_BIG
661 template
662 void
663 Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
664 Symbol_table* symtab,
665 Layout* layout,
666 Read_relocs_data* rd);
667 #endif
669 #ifdef HAVE_TARGET_64_LITTLE
670 template
671 void
672 Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
673 Symbol_table* symtab,
674 Layout* layout,
675 Read_relocs_data* rd);
676 #endif
678 #ifdef HAVE_TARGET_64_BIG
679 template
680 void
681 Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
682 Symbol_table* symtab,
683 Layout* layout,
684 Read_relocs_data* rd);
685 #endif
687 #ifdef HAVE_TARGET_32_LITTLE
688 template
689 void
690 Sized_relobj<32, false>::do_relocate(const General_options& options,
691 const Symbol_table* symtab,
692 const Layout* layout,
693 Output_file* of);
694 #endif
696 #ifdef HAVE_TARGET_32_BIG
697 template
698 void
699 Sized_relobj<32, true>::do_relocate(const General_options& options,
700 const Symbol_table* symtab,
701 const Layout* layout,
702 Output_file* of);
703 #endif
705 #ifdef HAVE_TARGET_64_LITTLE
706 template
707 void
708 Sized_relobj<64, false>::do_relocate(const General_options& options,
709 const Symbol_table* symtab,
710 const Layout* layout,
711 Output_file* of);
712 #endif
714 #ifdef HAVE_TARGET_64_BIG
715 template
716 void
717 Sized_relobj<64, true>::do_relocate(const General_options& options,
718 const Symbol_table* symtab,
719 const Layout* layout,
720 Output_file* of);
721 #endif
723 #ifdef HAVE_TARGET_32_LITTLE
724 template
725 class Copy_relocs<32, false>;
726 #endif
728 #ifdef HAVE_TARGET_32_BIG
729 template
730 class Copy_relocs<32, true>;
731 #endif
733 #ifdef HAVE_TARGET_64_LITTLE
734 template
735 class Copy_relocs<64, false>;
736 #endif
738 #ifdef HAVE_TARGET_64_BIG
739 template
740 class Copy_relocs<64, true>;
741 #endif
743 #ifdef HAVE_TARGET_32_LITTLE
744 template
745 void
746 Copy_relocs<32, false>::emit<elfcpp::SHT_REL>(
747 Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*);
748 #endif
750 #ifdef HAVE_TARGET_32_BIG
751 template
752 void
753 Copy_relocs<32, true>::emit<elfcpp::SHT_REL>(
754 Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*);
755 #endif
757 #ifdef HAVE_TARGET_64_LITTLE
758 template
759 void
760 Copy_relocs<64, false>::emit<elfcpp::SHT_REL>(
761 Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*);
762 #endif
764 #ifdef HAVE_TARGET_64_BIG
765 template
766 void
767 Copy_relocs<64, true>::emit<elfcpp::SHT_REL>(
768 Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*);
769 #endif
771 #ifdef HAVE_TARGET_32_LITTLE
772 template
773 void
774 Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>(
775 Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*);
776 #endif
778 #ifdef HAVE_TARGET_32_BIG
779 template
780 void
781 Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>(
782 Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*);
783 #endif
785 #ifdef HAVE_TARGET_64_LITTLE
786 template
787 void
788 Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>(
789 Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*);
790 #endif
792 #ifdef HAVE_TARGET_64_BIG
793 template
794 void
795 Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>(
796 Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*);
797 #endif
799 } // End namespace gold.