2008-02-20 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / gold / reloc.cc
blob1920032e1a62aa0973e26748680d0bdc88e0c82c
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 unless this is a
226 // relocatable link.
227 if (!parameters->output_is_object())
229 typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
230 if ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
231 continue;
234 if (shdr.get_sh_link() != this->symtab_shndx_)
236 this->error(_("relocation section %u uses unexpected "
237 "symbol table %u"),
238 i, shdr.get_sh_link());
239 continue;
242 off_t sh_size = shdr.get_sh_size();
244 unsigned int reloc_size;
245 if (sh_type == elfcpp::SHT_REL)
246 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
247 else
248 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
249 if (reloc_size != shdr.get_sh_entsize())
251 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
252 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
253 reloc_size);
254 continue;
257 size_t reloc_count = sh_size / reloc_size;
258 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
260 this->error(_("reloc section %u size %lu uneven"),
261 i, static_cast<unsigned long>(sh_size));
262 continue;
265 rd->relocs.push_back(Section_relocs());
266 Section_relocs& sr(rd->relocs.back());
267 sr.reloc_shndx = i;
268 sr.data_shndx = shndx;
269 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
270 true);
271 sr.sh_type = sh_type;
272 sr.reloc_count = reloc_count;
273 sr.output_section = os;
274 sr.needs_special_offset_handling = map_sections[shndx].offset == -1;
277 // Read the local symbols.
278 gold_assert(this->symtab_shndx_ != -1U);
279 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
280 rd->local_symbols = NULL;
281 else
283 typename This::Shdr symtabshdr(pshdrs
284 + this->symtab_shndx_ * This::shdr_size);
285 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
286 const int sym_size = This::sym_size;
287 const unsigned int loccount = this->local_symbol_count_;
288 gold_assert(loccount == symtabshdr.get_sh_info());
289 off_t locsize = loccount * sym_size;
290 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
291 locsize, true);
295 // Scan the relocs and adjust the symbol table. This looks for
296 // relocations which require GOT/PLT/COPY relocations.
298 template<int size, bool big_endian>
299 void
300 Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
301 Symbol_table* symtab,
302 Layout* layout,
303 Read_relocs_data* rd)
305 Sized_target<size, big_endian>* target = this->sized_target();
307 const unsigned char* local_symbols;
308 if (rd->local_symbols == NULL)
309 local_symbols = NULL;
310 else
311 local_symbols = rd->local_symbols->data();
313 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
314 p != rd->relocs.end();
315 ++p)
317 if (!parameters->output_is_object())
318 target->scan_relocs(options, symtab, layout, this, p->data_shndx,
319 p->sh_type, p->contents->data(), p->reloc_count,
320 p->output_section,
321 p->needs_special_offset_handling,
322 this->local_symbol_count_,
323 local_symbols);
324 else
326 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
327 gold_assert(rr != NULL);
328 rr->set_reloc_count(p->reloc_count);
329 target->scan_relocatable_relocs(options, symtab, layout, this,
330 p->data_shndx, p->sh_type,
331 p->contents->data(),
332 p->reloc_count,
333 p->output_section,
334 p->needs_special_offset_handling,
335 this->local_symbol_count_,
336 local_symbols,
337 rr);
340 delete p->contents;
341 p->contents = NULL;
344 if (rd->local_symbols != NULL)
346 delete rd->local_symbols;
347 rd->local_symbols = NULL;
351 // Relocate the input sections and write out the local symbols.
353 template<int size, bool big_endian>
354 void
355 Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
356 const Symbol_table* symtab,
357 const Layout* layout,
358 Output_file* of)
360 unsigned int shnum = this->shnum();
362 // Read the section headers.
363 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
364 shnum * This::shdr_size,
365 true);
367 Views views;
368 views.resize(shnum);
370 // Make two passes over the sections. The first one copies the
371 // section data to the output file. The second one applies
372 // relocations.
374 this->write_sections(pshdrs, of, &views);
376 // To speed up relocations, we set up hash tables for fast lookup of
377 // input offsets to output addresses.
378 this->initialize_input_to_output_maps();
380 // Apply relocations.
382 this->relocate_sections(options, symtab, layout, pshdrs, &views);
384 // After we've done the relocations, we release the hash tables,
385 // since we no longer need them.
386 this->free_input_to_output_maps();
388 // Write out the accumulated views.
389 for (unsigned int i = 1; i < shnum; ++i)
391 if (views[i].view != NULL)
393 if (!views[i].is_postprocessing_view)
395 if (views[i].is_input_output_view)
396 of->write_input_output_view(views[i].offset,
397 views[i].view_size,
398 views[i].view);
399 else
400 of->write_output_view(views[i].offset, views[i].view_size,
401 views[i].view);
406 // Write out the local symbols.
407 this->write_local_symbols(of, layout->sympool(), layout->dynpool());
409 // We should no longer need the local symbol values.
410 this->clear_local_symbols();
413 // Sort a Read_multiple vector by file offset.
414 struct Read_multiple_compare
416 inline bool
417 operator()(const File_read::Read_multiple_entry& rme1,
418 const File_read::Read_multiple_entry& rme2) const
419 { return rme1.file_offset < rme2.file_offset; }
422 // Write section data to the output file. PSHDRS points to the
423 // section headers. Record the views in *PVIEWS for use when
424 // relocating.
426 template<int size, bool big_endian>
427 void
428 Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
429 Output_file* of,
430 Views* pviews)
432 unsigned int shnum = this->shnum();
433 const std::vector<Map_to_output>& map_sections(this->map_to_output());
435 File_read::Read_multiple rm;
436 bool is_sorted = true;
438 const unsigned char* p = pshdrs + This::shdr_size;
439 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
441 View_size* pvs = &(*pviews)[i];
443 pvs->view = NULL;
445 const Output_section* os = map_sections[i].output_section;
446 if (os == NULL)
447 continue;
448 off_t output_offset = map_sections[i].offset;
450 typename This::Shdr shdr(p);
452 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
453 continue;
455 if (parameters->output_is_object()
456 && (shdr.get_sh_type() == elfcpp::SHT_REL
457 || shdr.get_sh_type() == elfcpp::SHT_RELA)
458 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
460 // This is a reloc section in a relocatable link. We don't
461 // need to read the input file. The size and file offset
462 // are stored in the Relocatable_relocs structure.
463 Relocatable_relocs* rr = this->relocatable_relocs(i);
464 gold_assert(rr != NULL);
465 Output_data* posd = rr->output_data();
466 gold_assert(posd != NULL);
468 pvs->offset = posd->offset();
469 pvs->view_size = posd->data_size();
470 pvs->view = of->get_output_view(pvs->offset, pvs->view_size);
471 pvs->address = posd->address();
472 pvs->is_input_output_view = false;
473 pvs->is_postprocessing_view = false;
475 continue;
478 // In the normal case, this input section is simply mapped to
479 // the output section at offset OUTPUT_OFFSET.
481 // However, if OUTPUT_OFFSET == -1, then input data is handled
482 // specially--e.g., a .eh_frame section. The relocation
483 // routines need to check for each reloc where it should be
484 // applied. For this case, we need an input/output view for the
485 // entire contents of the section in the output file. We don't
486 // want to copy the contents of the input section to the output
487 // section; the output section contents were already written,
488 // and we waited for them in Relocate_task::is_runnable because
489 // relocs_must_follow_section_writes is set for the object.
491 // Regardless of which of the above cases is true, we have to
492 // check requires_postprocessing of the output section. If that
493 // is false, then we work with views of the output file
494 // directly. If it is true, then we work with a separate
495 // buffer, and the output section is responsible for writing the
496 // final data to the output file.
498 off_t output_section_offset;
499 off_t output_section_size;
500 if (!os->requires_postprocessing())
502 output_section_offset = os->offset();
503 output_section_size = os->data_size();
505 else
507 output_section_offset = 0;
508 output_section_size = os->postprocessing_buffer_size();
511 off_t view_start;
512 section_size_type view_size;
513 if (output_offset != -1)
515 view_start = output_section_offset + output_offset;
516 view_size = convert_to_section_size_type(shdr.get_sh_size());
518 else
520 view_start = output_section_offset;
521 view_size = convert_to_section_size_type(output_section_size);
524 if (view_size == 0)
525 continue;
527 gold_assert(output_offset == -1
528 || (output_offset >= 0
529 && (output_offset + static_cast<off_t>(view_size)
530 <= output_section_size)));
532 unsigned char* view;
533 if (os->requires_postprocessing())
535 unsigned char* buffer = os->postprocessing_buffer();
536 view = buffer + view_start;
537 if (output_offset != -1)
539 off_t sh_offset = shdr.get_sh_offset();
540 if (!rm.empty() && rm.back().file_offset > sh_offset)
541 is_sorted = false;
542 rm.push_back(File_read::Read_multiple_entry(sh_offset,
543 view_size, view));
546 else
548 if (output_offset == -1)
549 view = of->get_input_output_view(view_start, view_size);
550 else
552 view = of->get_output_view(view_start, view_size);
553 off_t sh_offset = shdr.get_sh_offset();
554 if (!rm.empty() && rm.back().file_offset > sh_offset)
555 is_sorted = false;
556 rm.push_back(File_read::Read_multiple_entry(sh_offset,
557 view_size, view));
561 pvs->view = view;
562 pvs->address = os->address();
563 if (output_offset != -1)
564 pvs->address += output_offset;
565 pvs->offset = view_start;
566 pvs->view_size = view_size;
567 pvs->is_input_output_view = output_offset == -1;
568 pvs->is_postprocessing_view = os->requires_postprocessing();
571 // Actually read the data.
572 if (!rm.empty())
574 if (!is_sorted)
575 std::sort(rm.begin(), rm.end(), Read_multiple_compare());
576 this->read_multiple(rm);
580 // Relocate section data. VIEWS points to the section data as views
581 // in the output file.
583 template<int size, bool big_endian>
584 void
585 Sized_relobj<size, big_endian>::relocate_sections(
586 const General_options& options,
587 const Symbol_table* symtab,
588 const Layout* layout,
589 const unsigned char* pshdrs,
590 Views* pviews)
592 unsigned int shnum = this->shnum();
593 Sized_target<size, big_endian>* target = this->sized_target();
595 const std::vector<Map_to_output>& map_sections(this->map_to_output());
597 Relocate_info<size, big_endian> relinfo;
598 relinfo.options = &options;
599 relinfo.symtab = symtab;
600 relinfo.layout = layout;
601 relinfo.object = this;
603 const unsigned char* p = pshdrs + This::shdr_size;
604 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
606 typename This::Shdr shdr(p);
608 unsigned int sh_type = shdr.get_sh_type();
609 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
610 continue;
612 unsigned int index = shdr.get_sh_info();
613 if (index >= this->shnum())
615 this->error(_("relocation section %u has bad info %u"),
616 i, index);
617 continue;
620 Output_section* os = map_sections[index].output_section;
621 if (os == NULL)
623 // This relocation section is against a section which we
624 // discarded.
625 continue;
627 off_t output_offset = map_sections[index].offset;
629 gold_assert((*pviews)[index].view != NULL);
630 if (parameters->output_is_object())
631 gold_assert((*pviews)[i].view != NULL);
633 if (shdr.get_sh_link() != this->symtab_shndx_)
635 gold_error(_("relocation section %u uses unexpected "
636 "symbol table %u"),
637 i, shdr.get_sh_link());
638 continue;
641 off_t sh_size = shdr.get_sh_size();
642 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
643 sh_size, false);
645 unsigned int reloc_size;
646 if (sh_type == elfcpp::SHT_REL)
647 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
648 else
649 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
651 if (reloc_size != shdr.get_sh_entsize())
653 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
654 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
655 reloc_size);
656 continue;
659 size_t reloc_count = sh_size / reloc_size;
660 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
662 gold_error(_("reloc section %u size %lu uneven"),
663 i, static_cast<unsigned long>(sh_size));
664 continue;
667 gold_assert(output_offset != -1
668 || this->relocs_must_follow_section_writes());
670 relinfo.reloc_shndx = i;
671 relinfo.data_shndx = index;
672 if (!parameters->output_is_object())
673 target->relocate_section(&relinfo,
674 sh_type,
675 prelocs,
676 reloc_count,
678 output_offset == -1,
679 (*pviews)[index].view,
680 (*pviews)[index].address,
681 (*pviews)[index].view_size);
682 else
684 Relocatable_relocs* rr = this->relocatable_relocs(i);
685 target->relocate_for_relocatable(&relinfo,
686 sh_type,
687 prelocs,
688 reloc_count,
690 output_offset,
692 (*pviews)[index].view,
693 (*pviews)[index].address,
694 (*pviews)[index].view_size,
695 (*pviews)[i].view,
696 (*pviews)[i].view_size);
701 // Create merge hash tables for the local symbols. These are used to
702 // speed up relocations.
704 template<int size, bool big_endian>
705 void
706 Sized_relobj<size, big_endian>::initialize_input_to_output_maps()
708 const unsigned int loccount = this->local_symbol_count_;
709 for (unsigned int i = 1; i < loccount; ++i)
711 Symbol_value<size>& lv(this->local_values_[i]);
712 lv.initialize_input_to_output_map(this);
716 // Free merge hash tables for the local symbols.
718 template<int size, bool big_endian>
719 void
720 Sized_relobj<size, big_endian>::free_input_to_output_maps()
722 const unsigned int loccount = this->local_symbol_count_;
723 for (unsigned int i = 1; i < loccount; ++i)
725 Symbol_value<size>& lv(this->local_values_[i]);
726 lv.free_input_to_output_map();
730 // Class Merged_symbol_value.
732 template<int size>
733 void
734 Merged_symbol_value<size>::initialize_input_to_output_map(
735 const Relobj* object,
736 unsigned int input_shndx)
738 Object_merge_map* map = object->merge_map();
739 map->initialize_input_to_output_map<size>(input_shndx,
740 this->output_start_address_,
741 &this->output_addresses_);
744 // Get the output value corresponding to an input offset if we
745 // couldn't find it in the hash table.
747 template<int size>
748 typename elfcpp::Elf_types<size>::Elf_Addr
749 Merged_symbol_value<size>::value_from_output_section(
750 const Relobj* object,
751 unsigned int input_shndx,
752 typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const
754 section_offset_type output_offset;
755 bool found = object->merge_map()->get_output_offset(NULL, input_shndx,
756 input_offset,
757 &output_offset);
759 // If this assertion fails, it means that some relocation was
760 // against a portion of an input merge section which we didn't map
761 // to the output file and we didn't explicitly discard. We should
762 // always map all portions of input merge sections.
763 gold_assert(found);
765 if (output_offset == -1)
766 return 0;
767 else
768 return this->output_start_address_ + output_offset;
771 // Copy_relocs::Copy_reloc_entry methods.
773 // Return whether we should emit this reloc. We should emit it if the
774 // symbol is still defined in a dynamic object. If we should not emit
775 // it, we clear it, to save ourselves the test next time.
777 template<int size, bool big_endian>
778 bool
779 Copy_relocs<size, big_endian>::Copy_reloc_entry::should_emit()
781 if (this->sym_ == NULL)
782 return false;
783 if (this->sym_->is_from_dynobj())
784 return true;
785 this->sym_ = NULL;
786 return false;
789 // Emit a reloc into a SHT_REL section.
791 template<int size, bool big_endian>
792 void
793 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
794 Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* reloc_data)
796 this->sym_->set_needs_dynsym_entry();
797 reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
798 this->relobj_, this->shndx_, this->address_);
801 // Emit a reloc into a SHT_RELA section.
803 template<int size, bool big_endian>
804 void
805 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
806 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* reloc_data)
808 this->sym_->set_needs_dynsym_entry();
809 reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
810 this->relobj_, this->shndx_, this->address_,
811 this->addend_);
814 // Copy_relocs methods.
816 // Return whether we need a COPY reloc for a relocation against GSYM.
817 // The relocation is being applied to section SHNDX in OBJECT.
819 template<int size, bool big_endian>
820 bool
821 Copy_relocs<size, big_endian>::need_copy_reloc(
822 const General_options*,
823 Relobj* object,
824 unsigned int shndx,
825 Sized_symbol<size>* sym)
827 // FIXME: Handle -z nocopyrelocs.
829 if (sym->symsize() == 0)
830 return false;
832 // If this is a readonly section, then we need a COPY reloc.
833 // Otherwise we can use a dynamic reloc.
834 if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
835 return true;
837 return false;
840 // Save a Rel reloc.
842 template<int size, bool big_endian>
843 void
844 Copy_relocs<size, big_endian>::save(
845 Symbol* sym,
846 Relobj* relobj,
847 unsigned int shndx,
848 Output_section* output_section,
849 const elfcpp::Rel<size, big_endian>& rel)
851 unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info());
852 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
853 output_section,
854 rel.get_r_offset(), 0));
857 // Save a Rela reloc.
859 template<int size, bool big_endian>
860 void
861 Copy_relocs<size, big_endian>::save(
862 Symbol* sym,
863 Relobj* relobj,
864 unsigned int shndx,
865 Output_section* output_section,
866 const elfcpp::Rela<size, big_endian>& rela)
868 unsigned int reloc_type = elfcpp::elf_r_type<size>(rela.get_r_info());
869 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
870 output_section,
871 rela.get_r_offset(),
872 rela.get_r_addend()));
875 // Return whether there are any relocs to emit. We don't want to emit
876 // a reloc if the symbol is no longer defined in a dynamic object.
878 template<int size, bool big_endian>
879 bool
880 Copy_relocs<size, big_endian>::any_to_emit()
882 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
883 p != this->entries_.end();
884 ++p)
886 if (p->should_emit())
887 return true;
889 return false;
892 // Emit relocs.
894 template<int size, bool big_endian>
895 template<int sh_type>
896 void
897 Copy_relocs<size, big_endian>::emit(
898 Output_data_reloc<sh_type, true, size, big_endian>* reloc_data)
900 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
901 p != this->entries_.end();
902 ++p)
904 if (p->should_emit())
905 p->emit(reloc_data);
909 // Track_relocs methods.
911 // Initialize the class to track the relocs. This gets the object,
912 // the reloc section index, and the type of the relocs. This returns
913 // false if something goes wrong.
915 template<int size, bool big_endian>
916 bool
917 Track_relocs<size, big_endian>::initialize(
918 Object* object,
919 unsigned int reloc_shndx,
920 unsigned int reloc_type)
922 // If RELOC_SHNDX is -1U, it means there is more than one reloc
923 // section for the .eh_frame section. We can't handle that case.
924 if (reloc_shndx == -1U)
925 return false;
927 // If RELOC_SHNDX is 0, there is no reloc section.
928 if (reloc_shndx == 0)
929 return true;
931 // Get the contents of the reloc section.
932 this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
934 if (reloc_type == elfcpp::SHT_REL)
935 this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
936 else if (reloc_type == elfcpp::SHT_RELA)
937 this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
938 else
939 gold_unreachable();
941 if (this->len_ % this->reloc_size_ != 0)
943 object->error(_("reloc section size %zu is not a multiple of "
944 "reloc size %d\n"),
945 static_cast<size_t>(this->len_),
946 this->reloc_size_);
947 return false;
950 return true;
953 // Return the offset of the next reloc, or -1 if there isn't one.
955 template<int size, bool big_endian>
956 off_t
957 Track_relocs<size, big_endian>::next_offset() const
959 if (this->pos_ >= this->len_)
960 return -1;
962 // Rel and Rela start out the same, so we can always use Rel to find
963 // the r_offset value.
964 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
965 return rel.get_r_offset();
968 // Return the index of the symbol referenced by the next reloc, or -1U
969 // if there aren't any more relocs.
971 template<int size, bool big_endian>
972 unsigned int
973 Track_relocs<size, big_endian>::next_symndx() const
975 if (this->pos_ >= this->len_)
976 return -1U;
978 // Rel and Rela start out the same, so we can use Rel to find the
979 // symbol index.
980 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
981 return elfcpp::elf_r_sym<size>(rel.get_r_info());
984 // Advance to the next reloc whose r_offset is greater than or equal
985 // to OFFSET. Return the number of relocs we skip.
987 template<int size, bool big_endian>
989 Track_relocs<size, big_endian>::advance(off_t offset)
991 int ret = 0;
992 while (this->pos_ < this->len_)
994 // Rel and Rela start out the same, so we can always use Rel to
995 // find the r_offset value.
996 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
997 if (static_cast<off_t>(rel.get_r_offset()) >= offset)
998 break;
999 ++ret;
1000 this->pos_ += this->reloc_size_;
1002 return ret;
1005 // Instantiate the templates we need. We could use the configure
1006 // script to restrict this to only the ones for implemented targets.
1008 #ifdef HAVE_TARGET_32_LITTLE
1009 template
1010 void
1011 Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
1012 #endif
1014 #ifdef HAVE_TARGET_32_BIG
1015 template
1016 void
1017 Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
1018 #endif
1020 #ifdef HAVE_TARGET_64_LITTLE
1021 template
1022 void
1023 Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
1024 #endif
1026 #ifdef HAVE_TARGET_64_BIG
1027 template
1028 void
1029 Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
1030 #endif
1032 #ifdef HAVE_TARGET_32_LITTLE
1033 template
1034 void
1035 Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
1036 Symbol_table* symtab,
1037 Layout* layout,
1038 Read_relocs_data* rd);
1039 #endif
1041 #ifdef HAVE_TARGET_32_BIG
1042 template
1043 void
1044 Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
1045 Symbol_table* symtab,
1046 Layout* layout,
1047 Read_relocs_data* rd);
1048 #endif
1050 #ifdef HAVE_TARGET_64_LITTLE
1051 template
1052 void
1053 Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
1054 Symbol_table* symtab,
1055 Layout* layout,
1056 Read_relocs_data* rd);
1057 #endif
1059 #ifdef HAVE_TARGET_64_BIG
1060 template
1061 void
1062 Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
1063 Symbol_table* symtab,
1064 Layout* layout,
1065 Read_relocs_data* rd);
1066 #endif
1068 #ifdef HAVE_TARGET_32_LITTLE
1069 template
1070 void
1071 Sized_relobj<32, false>::do_relocate(const General_options& options,
1072 const Symbol_table* symtab,
1073 const Layout* layout,
1074 Output_file* of);
1075 #endif
1077 #ifdef HAVE_TARGET_32_BIG
1078 template
1079 void
1080 Sized_relobj<32, true>::do_relocate(const General_options& options,
1081 const Symbol_table* symtab,
1082 const Layout* layout,
1083 Output_file* of);
1084 #endif
1086 #ifdef HAVE_TARGET_64_LITTLE
1087 template
1088 void
1089 Sized_relobj<64, false>::do_relocate(const General_options& options,
1090 const Symbol_table* symtab,
1091 const Layout* layout,
1092 Output_file* of);
1093 #endif
1095 #ifdef HAVE_TARGET_64_BIG
1096 template
1097 void
1098 Sized_relobj<64, true>::do_relocate(const General_options& options,
1099 const Symbol_table* symtab,
1100 const Layout* layout,
1101 Output_file* of);
1102 #endif
1104 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1105 template
1106 class Merged_symbol_value<32>;
1107 #endif
1109 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1110 template
1111 class Merged_symbol_value<64>;
1112 #endif
1114 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1115 template
1116 class Symbol_value<32>;
1117 #endif
1119 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1120 template
1121 class Symbol_value<64>;
1122 #endif
1124 #ifdef HAVE_TARGET_32_LITTLE
1125 template
1126 class Copy_relocs<32, false>;
1127 #endif
1129 #ifdef HAVE_TARGET_32_BIG
1130 template
1131 class Copy_relocs<32, true>;
1132 #endif
1134 #ifdef HAVE_TARGET_64_LITTLE
1135 template
1136 class Copy_relocs<64, false>;
1137 #endif
1139 #ifdef HAVE_TARGET_64_BIG
1140 template
1141 class Copy_relocs<64, true>;
1142 #endif
1144 #ifdef HAVE_TARGET_32_LITTLE
1145 template
1146 void
1147 Copy_relocs<32, false>::emit<elfcpp::SHT_REL>(
1148 Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*);
1149 #endif
1151 #ifdef HAVE_TARGET_32_BIG
1152 template
1153 void
1154 Copy_relocs<32, true>::emit<elfcpp::SHT_REL>(
1155 Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*);
1156 #endif
1158 #ifdef HAVE_TARGET_64_LITTLE
1159 template
1160 void
1161 Copy_relocs<64, false>::emit<elfcpp::SHT_REL>(
1162 Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*);
1163 #endif
1165 #ifdef HAVE_TARGET_64_BIG
1166 template
1167 void
1168 Copy_relocs<64, true>::emit<elfcpp::SHT_REL>(
1169 Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*);
1170 #endif
1172 #ifdef HAVE_TARGET_32_LITTLE
1173 template
1174 void
1175 Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>(
1176 Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*);
1177 #endif
1179 #ifdef HAVE_TARGET_32_BIG
1180 template
1181 void
1182 Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>(
1183 Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*);
1184 #endif
1186 #ifdef HAVE_TARGET_64_LITTLE
1187 template
1188 void
1189 Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>(
1190 Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*);
1191 #endif
1193 #ifdef HAVE_TARGET_64_BIG
1194 template
1195 void
1196 Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>(
1197 Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*);
1198 #endif
1200 #ifdef HAVE_TARGET_32_LITTLE
1201 template
1202 class Track_relocs<32, false>;
1203 #endif
1205 #ifdef HAVE_TARGET_32_BIG
1206 template
1207 class Track_relocs<32, true>;
1208 #endif
1210 #ifdef HAVE_TARGET_64_LITTLE
1211 template
1212 class Track_relocs<64, false>;
1213 #endif
1215 #ifdef HAVE_TARGET_64_BIG
1216 template
1217 class Track_relocs<64, true>;
1218 #endif
1220 } // End namespace gold.