2008-01-23 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / gold / reloc.cc
blobd361f165d9fc92b6cab74ed947ce756492f11447
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 <algorithm>
27 #include "workqueue.h"
28 #include "symtab.h"
29 #include "output.h"
30 #include "merge.h"
31 #include "object.h"
32 #include "reloc.h"
34 namespace gold
37 // Read_relocs methods.
39 // These tasks just read the relocation information from the file.
40 // After reading it, the start another task to process the
41 // information. These tasks requires access to the file.
43 Task_token*
44 Read_relocs::is_runnable()
46 return this->object_->is_locked() ? this->object_->token() : NULL;
49 // Lock the file.
51 void
52 Read_relocs::locks(Task_locker* tl)
54 tl->add(this, this->object_->token());
57 // Read the relocations and then start a Scan_relocs_task.
59 void
60 Read_relocs::run(Workqueue* workqueue)
62 Read_relocs_data *rd = new Read_relocs_data;
63 this->object_->read_relocs(rd);
64 this->object_->release();
66 workqueue->queue_front(new Scan_relocs(this->options_, this->symtab_,
67 this->layout_, this->object_, rd,
68 this->symtab_lock_, this->blocker_));
71 // Return a debugging name for the task.
73 std::string
74 Read_relocs::get_name() const
76 return "Read_relocs " + this->object_->name();
79 // Scan_relocs methods.
81 // These tasks scan the relocations read by Read_relocs and mark up
82 // the symbol table to indicate which relocations are required. We
83 // use a lock on the symbol table to keep them from interfering with
84 // each other.
86 Task_token*
87 Scan_relocs::is_runnable()
89 if (!this->symtab_lock_->is_writable())
90 return this->symtab_lock_;
91 if (this->object_->is_locked())
92 return this->object_->token();
93 return NULL;
96 // Return the locks we hold: one on the file, one on the symbol table
97 // and one blocker.
99 void
100 Scan_relocs::locks(Task_locker* tl)
102 tl->add(this, this->object_->token());
103 tl->add(this, this->symtab_lock_);
104 tl->add(this, this->blocker_);
107 // Scan the relocs.
109 void
110 Scan_relocs::run(Workqueue*)
112 this->object_->scan_relocs(this->options_, this->symtab_, this->layout_,
113 this->rd_);
114 this->object_->release();
115 delete this->rd_;
116 this->rd_ = NULL;
119 // Return a debugging name for the task.
121 std::string
122 Scan_relocs::get_name() const
124 return "Scan_relocs " + this->object_->name();
127 // Relocate_task methods.
129 // We may have to wait for the output sections to be written.
131 Task_token*
132 Relocate_task::is_runnable()
134 if (this->object_->relocs_must_follow_section_writes()
135 && this->output_sections_blocker_->is_blocked())
136 return this->output_sections_blocker_;
138 if (this->object_->is_locked())
139 return this->object_->token();
141 return NULL;
144 // We want to lock the file while we run. We want to unblock
145 // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
146 // INPUT_SECTIONS_BLOCKER may be NULL.
148 void
149 Relocate_task::locks(Task_locker* tl)
151 if (this->input_sections_blocker_ != NULL)
152 tl->add(this, this->input_sections_blocker_);
153 tl->add(this, this->final_blocker_);
154 tl->add(this, this->object_->token());
157 // Run the task.
159 void
160 Relocate_task::run(Workqueue*)
162 this->object_->relocate(this->options_, this->symtab_, this->layout_,
163 this->of_);
165 // This is normally the last thing we will do with an object, so
166 // uncache all views.
167 this->object_->clear_view_cache_marks();
169 this->object_->release();
172 // Return a debugging name for the task.
174 std::string
175 Relocate_task::get_name() const
177 return "Relocate_task " + this->object_->name();
180 // Read the relocs and local symbols from the object file and store
181 // the information in RD.
183 template<int size, bool big_endian>
184 void
185 Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
187 rd->relocs.clear();
189 unsigned int shnum = this->shnum();
190 if (shnum == 0)
191 return;
193 rd->relocs.reserve(shnum / 2);
195 std::vector<Map_to_output>& map_sections(this->map_to_output());
197 const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(),
198 shnum * This::shdr_size,
199 true);
200 // Skip the first, dummy, section.
201 const unsigned char *ps = pshdrs + This::shdr_size;
202 for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size)
204 typename This::Shdr shdr(ps);
206 unsigned int sh_type = shdr.get_sh_type();
207 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
208 continue;
210 unsigned int shndx = shdr.get_sh_info();
211 if (shndx >= shnum)
213 this->error(_("relocation section %u has bad info %u"),
214 i, shndx);
215 continue;
218 Output_section* os = map_sections[shndx].output_section;
219 if (os == NULL)
220 continue;
222 // We are scanning relocations in order to fill out the GOT and
223 // PLT sections. Relocations for sections which are not
224 // allocated (typically debugging sections) should not add new
225 // GOT and PLT entries. So we skip them.
226 typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
227 if ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
228 continue;
230 if (shdr.get_sh_link() != this->symtab_shndx_)
232 this->error(_("relocation section %u uses unexpected "
233 "symbol table %u"),
234 i, shdr.get_sh_link());
235 continue;
238 off_t sh_size = shdr.get_sh_size();
240 unsigned int reloc_size;
241 if (sh_type == elfcpp::SHT_REL)
242 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
243 else
244 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
245 if (reloc_size != shdr.get_sh_entsize())
247 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
248 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
249 reloc_size);
250 continue;
253 size_t reloc_count = sh_size / reloc_size;
254 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
256 this->error(_("reloc section %u size %lu uneven"),
257 i, static_cast<unsigned long>(sh_size));
258 continue;
261 rd->relocs.push_back(Section_relocs());
262 Section_relocs& sr(rd->relocs.back());
263 sr.reloc_shndx = i;
264 sr.data_shndx = shndx;
265 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
266 true);
267 sr.sh_type = sh_type;
268 sr.reloc_count = reloc_count;
269 sr.output_section = os;
270 sr.needs_special_offset_handling = map_sections[shndx].offset == -1;
273 // Read the local symbols.
274 gold_assert(this->symtab_shndx_ != -1U);
275 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
276 rd->local_symbols = NULL;
277 else
279 typename This::Shdr symtabshdr(pshdrs
280 + this->symtab_shndx_ * This::shdr_size);
281 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
282 const int sym_size = This::sym_size;
283 const unsigned int loccount = this->local_symbol_count_;
284 gold_assert(loccount == symtabshdr.get_sh_info());
285 off_t locsize = loccount * sym_size;
286 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
287 locsize, true);
291 // Scan the relocs and adjust the symbol table. This looks for
292 // relocations which require GOT/PLT/COPY relocations.
294 template<int size, bool big_endian>
295 void
296 Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
297 Symbol_table* symtab,
298 Layout* layout,
299 Read_relocs_data* rd)
301 Sized_target<size, big_endian>* target = this->sized_target();
303 const unsigned char* local_symbols;
304 if (rd->local_symbols == NULL)
305 local_symbols = NULL;
306 else
307 local_symbols = rd->local_symbols->data();
309 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
310 p != rd->relocs.end();
311 ++p)
313 target->scan_relocs(options, symtab, layout, this, p->data_shndx,
314 p->sh_type, p->contents->data(), p->reloc_count,
315 p->output_section, p->needs_special_offset_handling,
316 this->local_symbol_count_,
317 local_symbols);
318 delete p->contents;
319 p->contents = NULL;
322 if (rd->local_symbols != NULL)
324 delete rd->local_symbols;
325 rd->local_symbols = NULL;
329 // Relocate the input sections and write out the local symbols.
331 template<int size, bool big_endian>
332 void
333 Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
334 const Symbol_table* symtab,
335 const Layout* layout,
336 Output_file* of)
338 unsigned int shnum = this->shnum();
340 // Read the section headers.
341 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
342 shnum * This::shdr_size,
343 true);
345 Views views;
346 views.resize(shnum);
348 // Make two passes over the sections. The first one copies the
349 // section data to the output file. The second one applies
350 // relocations.
352 this->write_sections(pshdrs, of, &views);
354 // To speed up relocations, we set up hash tables for fast lookup of
355 // input offsets to output addresses.
356 this->initialize_input_to_output_maps();
358 // Apply relocations.
360 this->relocate_sections(options, symtab, layout, pshdrs, &views);
362 // After we've done the relocations, we release the hash tables,
363 // since we no longer need them.
364 this->free_input_to_output_maps();
366 // Write out the accumulated views.
367 for (unsigned int i = 1; i < shnum; ++i)
369 if (views[i].view != NULL)
371 if (!views[i].is_postprocessing_view)
373 if (views[i].is_input_output_view)
374 of->write_input_output_view(views[i].offset,
375 views[i].view_size,
376 views[i].view);
377 else
378 of->write_output_view(views[i].offset, views[i].view_size,
379 views[i].view);
384 // Write out the local symbols.
385 this->write_local_symbols(of, layout->sympool(), layout->dynpool());
387 // We should no longer need the local symbol values.
388 this->clear_local_symbols();
391 // Sort a Read_multiple vector by file offset.
392 struct Read_multiple_compare
394 inline bool
395 operator()(const File_read::Read_multiple_entry& rme1,
396 const File_read::Read_multiple_entry& rme2) const
397 { return rme1.file_offset < rme2.file_offset; }
400 // Write section data to the output file. PSHDRS points to the
401 // section headers. Record the views in *PVIEWS for use when
402 // relocating.
404 template<int size, bool big_endian>
405 void
406 Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
407 Output_file* of,
408 Views* pviews)
410 unsigned int shnum = this->shnum();
411 const std::vector<Map_to_output>& map_sections(this->map_to_output());
413 File_read::Read_multiple rm;
414 bool is_sorted = true;
416 const unsigned char* p = pshdrs + This::shdr_size;
417 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
419 View_size* pvs = &(*pviews)[i];
421 pvs->view = NULL;
423 const Output_section* os = map_sections[i].output_section;
424 if (os == NULL)
425 continue;
426 off_t output_offset = map_sections[i].offset;
428 typename This::Shdr shdr(p);
430 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
431 continue;
433 // In the normal case, this input section is simply mapped to
434 // the output section at offset OUTPUT_OFFSET.
436 // However, if OUTPUT_OFFSET == -1, then input data is handled
437 // specially--e.g., a .eh_frame section. The relocation
438 // routines need to check for each reloc where it should be
439 // applied. For this case, we need an input/output view for the
440 // entire contents of the section in the output file. We don't
441 // want to copy the contents of the input section to the output
442 // section; the output section contents were already written,
443 // and we waited for them in Relocate_task::is_runnable because
444 // relocs_must_follow_section_writes is set for the object.
446 // Regardless of which of the above cases is true, we have to
447 // check requires_postprocessing of the output section. If that
448 // is false, then we work with views of the output file
449 // directly. If it is true, then we work with a separate
450 // buffer, and the output section is responsible for writing the
451 // final data to the output file.
453 off_t output_section_offset;
454 off_t output_section_size;
455 if (!os->requires_postprocessing())
457 output_section_offset = os->offset();
458 output_section_size = os->data_size();
460 else
462 output_section_offset = 0;
463 output_section_size = os->postprocessing_buffer_size();
466 off_t view_start;
467 section_size_type view_size;
468 if (output_offset != -1)
470 view_start = output_section_offset + output_offset;
471 view_size = convert_to_section_size_type(shdr.get_sh_size());
473 else
475 view_start = output_section_offset;
476 view_size = convert_to_section_size_type(output_section_size);
479 if (view_size == 0)
480 continue;
482 gold_assert(output_offset == -1
483 || (output_offset >= 0
484 && (output_offset + static_cast<off_t>(view_size)
485 <= output_section_size)));
487 unsigned char* view;
488 if (os->requires_postprocessing())
490 unsigned char* buffer = os->postprocessing_buffer();
491 view = buffer + view_start;
492 if (output_offset != -1)
494 off_t sh_offset = shdr.get_sh_offset();
495 if (!rm.empty() && rm.back().file_offset > sh_offset)
496 is_sorted = false;
497 rm.push_back(File_read::Read_multiple_entry(sh_offset,
498 view_size, view));
501 else
503 if (output_offset == -1)
504 view = of->get_input_output_view(view_start, view_size);
505 else
507 view = of->get_output_view(view_start, view_size);
508 off_t sh_offset = shdr.get_sh_offset();
509 if (!rm.empty() && rm.back().file_offset > sh_offset)
510 is_sorted = false;
511 rm.push_back(File_read::Read_multiple_entry(sh_offset,
512 view_size, view));
516 pvs->view = view;
517 pvs->address = os->address();
518 if (output_offset != -1)
519 pvs->address += output_offset;
520 pvs->offset = view_start;
521 pvs->view_size = view_size;
522 pvs->is_input_output_view = output_offset == -1;
523 pvs->is_postprocessing_view = os->requires_postprocessing();
526 // Actually read the data.
527 if (!rm.empty())
529 if (!is_sorted)
530 std::sort(rm.begin(), rm.end(), Read_multiple_compare());
531 this->read_multiple(rm);
535 // Relocate section data. VIEWS points to the section data as views
536 // in the output file.
538 template<int size, bool big_endian>
539 void
540 Sized_relobj<size, big_endian>::relocate_sections(
541 const General_options& options,
542 const Symbol_table* symtab,
543 const Layout* layout,
544 const unsigned char* pshdrs,
545 Views* pviews)
547 unsigned int shnum = this->shnum();
548 Sized_target<size, big_endian>* target = this->sized_target();
550 const std::vector<Map_to_output>& map_sections(this->map_to_output());
552 Relocate_info<size, big_endian> relinfo;
553 relinfo.options = &options;
554 relinfo.symtab = symtab;
555 relinfo.layout = layout;
556 relinfo.object = this;
558 const unsigned char* p = pshdrs + This::shdr_size;
559 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
561 typename This::Shdr shdr(p);
563 unsigned int sh_type = shdr.get_sh_type();
564 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
565 continue;
567 unsigned int index = shdr.get_sh_info();
568 if (index >= this->shnum())
570 this->error(_("relocation section %u has bad info %u"),
571 i, index);
572 continue;
575 Output_section* os = map_sections[index].output_section;
576 if (os == NULL)
578 // This relocation section is against a section which we
579 // discarded.
580 continue;
582 off_t output_offset = map_sections[index].offset;
584 gold_assert((*pviews)[index].view != NULL);
586 if (shdr.get_sh_link() != this->symtab_shndx_)
588 gold_error(_("relocation section %u uses unexpected "
589 "symbol table %u"),
590 i, shdr.get_sh_link());
591 continue;
594 off_t sh_size = shdr.get_sh_size();
595 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
596 sh_size, false);
598 unsigned int reloc_size;
599 if (sh_type == elfcpp::SHT_REL)
600 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
601 else
602 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
604 if (reloc_size != shdr.get_sh_entsize())
606 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
607 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
608 reloc_size);
609 continue;
612 size_t reloc_count = sh_size / reloc_size;
613 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
615 gold_error(_("reloc section %u size %lu uneven"),
616 i, static_cast<unsigned long>(sh_size));
617 continue;
620 gold_assert(output_offset != -1
621 || this->relocs_must_follow_section_writes());
623 relinfo.reloc_shndx = i;
624 relinfo.data_shndx = index;
625 target->relocate_section(&relinfo,
626 sh_type,
627 prelocs,
628 reloc_count,
630 output_offset == -1,
631 (*pviews)[index].view,
632 (*pviews)[index].address,
633 (*pviews)[index].view_size);
637 // Create merge hash tables for the local symbols. These are used to
638 // speed up relocations.
640 template<int size, bool big_endian>
641 void
642 Sized_relobj<size, big_endian>::initialize_input_to_output_maps()
644 const unsigned int loccount = this->local_symbol_count_;
645 for (unsigned int i = 1; i < loccount; ++i)
647 Symbol_value<size>& lv(this->local_values_[i]);
648 lv.initialize_input_to_output_map(this);
652 // Free merge hash tables for the local symbols.
654 template<int size, bool big_endian>
655 void
656 Sized_relobj<size, big_endian>::free_input_to_output_maps()
658 const unsigned int loccount = this->local_symbol_count_;
659 for (unsigned int i = 1; i < loccount; ++i)
661 Symbol_value<size>& lv(this->local_values_[i]);
662 lv.free_input_to_output_map();
666 // Class Merged_symbol_value.
668 template<int size>
669 void
670 Merged_symbol_value<size>::initialize_input_to_output_map(
671 const Relobj* object,
672 unsigned int input_shndx)
674 Object_merge_map* map = object->merge_map();
675 map->initialize_input_to_output_map<size>(input_shndx,
676 this->output_start_address_,
677 &this->output_addresses_);
680 // Get the output value corresponding to an input offset if we
681 // couldn't find it in the hash table.
683 template<int size>
684 typename elfcpp::Elf_types<size>::Elf_Addr
685 Merged_symbol_value<size>::value_from_output_section(
686 const Relobj* object,
687 unsigned int input_shndx,
688 typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const
690 section_offset_type output_offset;
691 bool found = object->merge_map()->get_output_offset(NULL, input_shndx,
692 input_offset,
693 &output_offset);
695 // If this assertion fails, it means that some relocation was
696 // against a portion of an input merge section which we didn't map
697 // to the output file and we didn't explicitly discard. We should
698 // always map all portions of input merge sections.
699 gold_assert(found);
701 if (output_offset == -1)
702 return 0;
703 else
704 return this->output_start_address_ + output_offset;
707 // Copy_relocs::Copy_reloc_entry methods.
709 // Return whether we should emit this reloc. We should emit it if the
710 // symbol is still defined in a dynamic object. If we should not emit
711 // it, we clear it, to save ourselves the test next time.
713 template<int size, bool big_endian>
714 bool
715 Copy_relocs<size, big_endian>::Copy_reloc_entry::should_emit()
717 if (this->sym_ == NULL)
718 return false;
719 if (this->sym_->is_from_dynobj())
720 return true;
721 this->sym_ = NULL;
722 return false;
725 // Emit a reloc into a SHT_REL section.
727 template<int size, bool big_endian>
728 void
729 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
730 Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* reloc_data)
732 this->sym_->set_needs_dynsym_entry();
733 reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
734 this->relobj_, this->shndx_, this->address_);
737 // Emit a reloc into a SHT_RELA section.
739 template<int size, bool big_endian>
740 void
741 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
742 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* reloc_data)
744 this->sym_->set_needs_dynsym_entry();
745 reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
746 this->relobj_, this->shndx_, this->address_,
747 this->addend_);
750 // Copy_relocs methods.
752 // Return whether we need a COPY reloc for a relocation against GSYM.
753 // The relocation is being applied to section SHNDX in OBJECT.
755 template<int size, bool big_endian>
756 bool
757 Copy_relocs<size, big_endian>::need_copy_reloc(
758 const General_options*,
759 Relobj* object,
760 unsigned int shndx,
761 Sized_symbol<size>* sym)
763 // FIXME: Handle -z nocopyrelocs.
765 if (sym->symsize() == 0)
766 return false;
768 // If this is a readonly section, then we need a COPY reloc.
769 // Otherwise we can use a dynamic reloc.
770 if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
771 return true;
773 return false;
776 // Save a Rel reloc.
778 template<int size, bool big_endian>
779 void
780 Copy_relocs<size, big_endian>::save(
781 Symbol* sym,
782 Relobj* relobj,
783 unsigned int shndx,
784 Output_section* output_section,
785 const elfcpp::Rel<size, big_endian>& rel)
787 unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info());
788 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
789 output_section,
790 rel.get_r_offset(), 0));
793 // Save a Rela reloc.
795 template<int size, bool big_endian>
796 void
797 Copy_relocs<size, big_endian>::save(
798 Symbol* sym,
799 Relobj* relobj,
800 unsigned int shndx,
801 Output_section* output_section,
802 const elfcpp::Rela<size, big_endian>& rela)
804 unsigned int reloc_type = elfcpp::elf_r_type<size>(rela.get_r_info());
805 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
806 output_section,
807 rela.get_r_offset(),
808 rela.get_r_addend()));
811 // Return whether there are any relocs to emit. We don't want to emit
812 // a reloc if the symbol is no longer defined in a dynamic object.
814 template<int size, bool big_endian>
815 bool
816 Copy_relocs<size, big_endian>::any_to_emit()
818 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
819 p != this->entries_.end();
820 ++p)
822 if (p->should_emit())
823 return true;
825 return false;
828 // Emit relocs.
830 template<int size, bool big_endian>
831 template<int sh_type>
832 void
833 Copy_relocs<size, big_endian>::emit(
834 Output_data_reloc<sh_type, true, size, big_endian>* reloc_data)
836 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
837 p != this->entries_.end();
838 ++p)
840 if (p->should_emit())
841 p->emit(reloc_data);
845 // Track_relocs methods.
847 // Initialize the class to track the relocs. This gets the object,
848 // the reloc section index, and the type of the relocs. This returns
849 // false if something goes wrong.
851 template<int size, bool big_endian>
852 bool
853 Track_relocs<size, big_endian>::initialize(
854 Object* object,
855 unsigned int reloc_shndx,
856 unsigned int reloc_type)
858 // If RELOC_SHNDX is -1U, it means there is more than one reloc
859 // section for the .eh_frame section. We can't handle that case.
860 if (reloc_shndx == -1U)
861 return false;
863 // If RELOC_SHNDX is 0, there is no reloc section.
864 if (reloc_shndx == 0)
865 return true;
867 // Get the contents of the reloc section.
868 this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
870 if (reloc_type == elfcpp::SHT_REL)
871 this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
872 else if (reloc_type == elfcpp::SHT_RELA)
873 this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
874 else
875 gold_unreachable();
877 if (this->len_ % this->reloc_size_ != 0)
879 object->error(_("reloc section size %zu is not a multiple of "
880 "reloc size %d\n"),
881 static_cast<size_t>(this->len_),
882 this->reloc_size_);
883 return false;
886 return true;
889 // Return the offset of the next reloc, or -1 if there isn't one.
891 template<int size, bool big_endian>
892 off_t
893 Track_relocs<size, big_endian>::next_offset() const
895 if (this->pos_ >= this->len_)
896 return -1;
898 // Rel and Rela start out the same, so we can always use Rel to find
899 // the r_offset value.
900 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
901 return rel.get_r_offset();
904 // Return the index of the symbol referenced by the next reloc, or -1U
905 // if there aren't any more relocs.
907 template<int size, bool big_endian>
908 unsigned int
909 Track_relocs<size, big_endian>::next_symndx() const
911 if (this->pos_ >= this->len_)
912 return -1U;
914 // Rel and Rela start out the same, so we can use Rel to find the
915 // symbol index.
916 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
917 return elfcpp::elf_r_sym<size>(rel.get_r_info());
920 // Advance to the next reloc whose r_offset is greater than or equal
921 // to OFFSET. Return the number of relocs we skip.
923 template<int size, bool big_endian>
925 Track_relocs<size, big_endian>::advance(off_t offset)
927 int ret = 0;
928 while (this->pos_ < this->len_)
930 // Rel and Rela start out the same, so we can always use Rel to
931 // find the r_offset value.
932 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
933 if (static_cast<off_t>(rel.get_r_offset()) >= offset)
934 break;
935 ++ret;
936 this->pos_ += this->reloc_size_;
938 return ret;
941 // Instantiate the templates we need. We could use the configure
942 // script to restrict this to only the ones for implemented targets.
944 #ifdef HAVE_TARGET_32_LITTLE
945 template
946 void
947 Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
948 #endif
950 #ifdef HAVE_TARGET_32_BIG
951 template
952 void
953 Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
954 #endif
956 #ifdef HAVE_TARGET_64_LITTLE
957 template
958 void
959 Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
960 #endif
962 #ifdef HAVE_TARGET_64_BIG
963 template
964 void
965 Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
966 #endif
968 #ifdef HAVE_TARGET_32_LITTLE
969 template
970 void
971 Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
972 Symbol_table* symtab,
973 Layout* layout,
974 Read_relocs_data* rd);
975 #endif
977 #ifdef HAVE_TARGET_32_BIG
978 template
979 void
980 Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
981 Symbol_table* symtab,
982 Layout* layout,
983 Read_relocs_data* rd);
984 #endif
986 #ifdef HAVE_TARGET_64_LITTLE
987 template
988 void
989 Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
990 Symbol_table* symtab,
991 Layout* layout,
992 Read_relocs_data* rd);
993 #endif
995 #ifdef HAVE_TARGET_64_BIG
996 template
997 void
998 Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
999 Symbol_table* symtab,
1000 Layout* layout,
1001 Read_relocs_data* rd);
1002 #endif
1004 #ifdef HAVE_TARGET_32_LITTLE
1005 template
1006 void
1007 Sized_relobj<32, false>::do_relocate(const General_options& options,
1008 const Symbol_table* symtab,
1009 const Layout* layout,
1010 Output_file* of);
1011 #endif
1013 #ifdef HAVE_TARGET_32_BIG
1014 template
1015 void
1016 Sized_relobj<32, true>::do_relocate(const General_options& options,
1017 const Symbol_table* symtab,
1018 const Layout* layout,
1019 Output_file* of);
1020 #endif
1022 #ifdef HAVE_TARGET_64_LITTLE
1023 template
1024 void
1025 Sized_relobj<64, false>::do_relocate(const General_options& options,
1026 const Symbol_table* symtab,
1027 const Layout* layout,
1028 Output_file* of);
1029 #endif
1031 #ifdef HAVE_TARGET_64_BIG
1032 template
1033 void
1034 Sized_relobj<64, true>::do_relocate(const General_options& options,
1035 const Symbol_table* symtab,
1036 const Layout* layout,
1037 Output_file* of);
1038 #endif
1040 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1041 template
1042 class Merged_symbol_value<32>;
1043 #endif
1045 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1046 template
1047 class Merged_symbol_value<64>;
1048 #endif
1050 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1051 template
1052 class Symbol_value<32>;
1053 #endif
1055 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1056 template
1057 class Symbol_value<64>;
1058 #endif
1060 #ifdef HAVE_TARGET_32_LITTLE
1061 template
1062 class Copy_relocs<32, false>;
1063 #endif
1065 #ifdef HAVE_TARGET_32_BIG
1066 template
1067 class Copy_relocs<32, true>;
1068 #endif
1070 #ifdef HAVE_TARGET_64_LITTLE
1071 template
1072 class Copy_relocs<64, false>;
1073 #endif
1075 #ifdef HAVE_TARGET_64_BIG
1076 template
1077 class Copy_relocs<64, true>;
1078 #endif
1080 #ifdef HAVE_TARGET_32_LITTLE
1081 template
1082 void
1083 Copy_relocs<32, false>::emit<elfcpp::SHT_REL>(
1084 Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*);
1085 #endif
1087 #ifdef HAVE_TARGET_32_BIG
1088 template
1089 void
1090 Copy_relocs<32, true>::emit<elfcpp::SHT_REL>(
1091 Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*);
1092 #endif
1094 #ifdef HAVE_TARGET_64_LITTLE
1095 template
1096 void
1097 Copy_relocs<64, false>::emit<elfcpp::SHT_REL>(
1098 Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*);
1099 #endif
1101 #ifdef HAVE_TARGET_64_BIG
1102 template
1103 void
1104 Copy_relocs<64, true>::emit<elfcpp::SHT_REL>(
1105 Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*);
1106 #endif
1108 #ifdef HAVE_TARGET_32_LITTLE
1109 template
1110 void
1111 Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>(
1112 Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*);
1113 #endif
1115 #ifdef HAVE_TARGET_32_BIG
1116 template
1117 void
1118 Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>(
1119 Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*);
1120 #endif
1122 #ifdef HAVE_TARGET_64_LITTLE
1123 template
1124 void
1125 Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>(
1126 Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*);
1127 #endif
1129 #ifdef HAVE_TARGET_64_BIG
1130 template
1131 void
1132 Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>(
1133 Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*);
1134 #endif
1136 #ifdef HAVE_TARGET_32_LITTLE
1137 template
1138 class Track_relocs<32, false>;
1139 #endif
1141 #ifdef HAVE_TARGET_32_BIG
1142 template
1143 class Track_relocs<32, true>;
1144 #endif
1146 #ifdef HAVE_TARGET_64_LITTLE
1147 template
1148 class Track_relocs<64, false>;
1149 #endif
1151 #ifdef HAVE_TARGET_64_BIG
1152 template
1153 class Track_relocs<64, true>;
1154 #endif
1156 } // End namespace gold.