1 // reloc.cc -- relocate input files for gold.
3 // Copyright 2006, 2007, 2008 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.
27 #include "workqueue.h"
32 #include "target-reloc.h"
38 // Read_relocs methods.
40 // These tasks just read the relocation information from the file.
41 // After reading it, the start another task to process the
42 // information. These tasks requires access to the file.
45 Read_relocs::is_runnable()
47 return this->object_
->is_locked() ? this->object_
->token() : NULL
;
53 Read_relocs::locks(Task_locker
* tl
)
55 tl
->add(this, this->object_
->token());
58 // Read the relocations and then start a Scan_relocs_task.
61 Read_relocs::run(Workqueue
* workqueue
)
63 Read_relocs_data
*rd
= new Read_relocs_data
;
64 this->object_
->read_relocs(rd
);
65 this->object_
->release();
67 workqueue
->queue_next(new Scan_relocs(this->options_
, this->symtab_
,
68 this->layout_
, this->object_
, rd
,
69 this->symtab_lock_
, this->blocker_
));
72 // Return a debugging name for the task.
75 Read_relocs::get_name() const
77 return "Read_relocs " + this->object_
->name();
80 // Scan_relocs methods.
82 // These tasks scan the relocations read by Read_relocs and mark up
83 // the symbol table to indicate which relocations are required. We
84 // use a lock on the symbol table to keep them from interfering with
88 Scan_relocs::is_runnable()
90 if (!this->symtab_lock_
->is_writable())
91 return this->symtab_lock_
;
92 if (this->object_
->is_locked())
93 return this->object_
->token();
97 // Return the locks we hold: one on the file, one on the symbol table
101 Scan_relocs::locks(Task_locker
* tl
)
103 tl
->add(this, this->object_
->token());
104 tl
->add(this, this->symtab_lock_
);
105 tl
->add(this, this->blocker_
);
111 Scan_relocs::run(Workqueue
*)
113 this->object_
->scan_relocs(this->options_
, this->symtab_
, this->layout_
,
115 this->object_
->release();
120 // Return a debugging name for the task.
123 Scan_relocs::get_name() const
125 return "Scan_relocs " + this->object_
->name();
128 // Relocate_task methods.
130 // We may have to wait for the output sections to be written.
133 Relocate_task::is_runnable()
135 if (this->object_
->relocs_must_follow_section_writes()
136 && this->output_sections_blocker_
->is_blocked())
137 return this->output_sections_blocker_
;
139 if (this->object_
->is_locked())
140 return this->object_
->token();
145 // We want to lock the file while we run. We want to unblock
146 // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
147 // INPUT_SECTIONS_BLOCKER may be NULL.
150 Relocate_task::locks(Task_locker
* tl
)
152 if (this->input_sections_blocker_
!= NULL
)
153 tl
->add(this, this->input_sections_blocker_
);
154 tl
->add(this, this->final_blocker_
);
155 tl
->add(this, this->object_
->token());
161 Relocate_task::run(Workqueue
*)
163 this->object_
->relocate(this->options_
, this->symtab_
, this->layout_
,
166 // This is normally the last thing we will do with an object, so
167 // uncache all views.
168 this->object_
->clear_view_cache_marks();
170 this->object_
->release();
173 // Return a debugging name for the task.
176 Relocate_task::get_name() const
178 return "Relocate_task " + this->object_
->name();
181 // Read the relocs and local symbols from the object file and store
182 // the information in RD.
184 template<int size
, bool big_endian
>
186 Sized_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
190 unsigned int shnum
= this->shnum();
194 rd
->relocs
.reserve(shnum
/ 2);
196 std::vector
<Map_to_output
>& map_sections(this->map_to_output());
198 const unsigned char *pshdrs
= this->get_view(this->elf_file_
.shoff(),
199 shnum
* This::shdr_size
,
201 // Skip the first, dummy, section.
202 const unsigned char *ps
= pshdrs
+ This::shdr_size
;
203 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= This::shdr_size
)
205 typename
This::Shdr
shdr(ps
);
207 unsigned int sh_type
= shdr
.get_sh_type();
208 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
211 unsigned int shndx
= this->adjust_shndx(shdr
.get_sh_info());
214 this->error(_("relocation section %u has bad info %u"),
219 Output_section
* os
= map_sections
[shndx
].output_section
;
223 // We are scanning relocations in order to fill out the GOT and
224 // PLT sections. Relocations for sections which are not
225 // allocated (typically debugging sections) should not add new
226 // GOT and PLT entries. So we skip them unless this is a
227 // relocatable link or we need to emit relocations.
228 typename
This::Shdr
secshdr(pshdrs
+ shndx
* This::shdr_size
);
229 bool is_section_allocated
= ((secshdr
.get_sh_flags() & elfcpp::SHF_ALLOC
)
231 if (!is_section_allocated
232 && !parameters
->options().relocatable()
233 && !parameters
->options().emit_relocs())
236 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx_
)
238 this->error(_("relocation section %u uses unexpected "
240 i
, this->adjust_shndx(shdr
.get_sh_link()));
244 off_t sh_size
= shdr
.get_sh_size();
246 unsigned int reloc_size
;
247 if (sh_type
== elfcpp::SHT_REL
)
248 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
250 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
251 if (reloc_size
!= shdr
.get_sh_entsize())
253 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
254 i
, static_cast<unsigned long>(shdr
.get_sh_entsize()),
259 size_t reloc_count
= sh_size
/ reloc_size
;
260 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
262 this->error(_("reloc section %u size %lu uneven"),
263 i
, static_cast<unsigned long>(sh_size
));
267 rd
->relocs
.push_back(Section_relocs());
268 Section_relocs
& sr(rd
->relocs
.back());
270 sr
.data_shndx
= shndx
;
271 sr
.contents
= this->get_lasting_view(shdr
.get_sh_offset(), sh_size
,
273 sr
.sh_type
= sh_type
;
274 sr
.reloc_count
= reloc_count
;
275 sr
.output_section
= os
;
276 sr
.needs_special_offset_handling
= map_sections
[shndx
].offset
== -1;
277 sr
.is_data_section_allocated
= is_section_allocated
;
280 // Read the local symbols.
281 gold_assert(this->symtab_shndx_
!= -1U);
282 if (this->symtab_shndx_
== 0 || this->local_symbol_count_
== 0)
283 rd
->local_symbols
= NULL
;
286 typename
This::Shdr
symtabshdr(pshdrs
287 + this->symtab_shndx_
* This::shdr_size
);
288 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
289 const int sym_size
= This::sym_size
;
290 const unsigned int loccount
= this->local_symbol_count_
;
291 gold_assert(loccount
== symtabshdr
.get_sh_info());
292 off_t locsize
= loccount
* sym_size
;
293 rd
->local_symbols
= this->get_lasting_view(symtabshdr
.get_sh_offset(),
294 locsize
, true, true);
298 // Scan the relocs and adjust the symbol table. This looks for
299 // relocations which require GOT/PLT/COPY relocations.
301 template<int size
, bool big_endian
>
303 Sized_relobj
<size
, big_endian
>::do_scan_relocs(const General_options
& options
,
304 Symbol_table
* symtab
,
306 Read_relocs_data
* rd
)
308 Sized_target
<size
, big_endian
>* target
= this->sized_target();
310 const unsigned char* local_symbols
;
311 if (rd
->local_symbols
== NULL
)
312 local_symbols
= NULL
;
314 local_symbols
= rd
->local_symbols
->data();
316 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
317 p
!= rd
->relocs
.end();
320 if (!parameters
->options().relocatable())
322 // As noted above, when not generating an object file, we
323 // only scan allocated sections. We may see a non-allocated
324 // section here if we are emitting relocs.
325 if (p
->is_data_section_allocated
)
326 target
->scan_relocs(options
, symtab
, layout
, this, p
->data_shndx
,
327 p
->sh_type
, p
->contents
->data(),
328 p
->reloc_count
, p
->output_section
,
329 p
->needs_special_offset_handling
,
330 this->local_symbol_count_
,
332 if (parameters
->options().emit_relocs())
333 this->emit_relocs_scan(options
, symtab
, layout
, local_symbols
, p
);
337 Relocatable_relocs
* rr
= this->relocatable_relocs(p
->reloc_shndx
);
338 gold_assert(rr
!= NULL
);
339 rr
->set_reloc_count(p
->reloc_count
);
340 target
->scan_relocatable_relocs(options
, symtab
, layout
, this,
341 p
->data_shndx
, p
->sh_type
,
345 p
->needs_special_offset_handling
,
346 this->local_symbol_count_
,
355 if (rd
->local_symbols
!= NULL
)
357 delete rd
->local_symbols
;
358 rd
->local_symbols
= NULL
;
362 // This is a strategy class we use when scanning for --emit-relocs.
364 template<int sh_type
>
365 class Emit_relocs_strategy
368 // A local non-section symbol.
369 inline Relocatable_relocs::Reloc_strategy
370 local_non_section_strategy(unsigned int, Relobj
*)
371 { return Relocatable_relocs::RELOC_COPY
; }
373 // A local section symbol.
374 inline Relocatable_relocs::Reloc_strategy
375 local_section_strategy(unsigned int, Relobj
*)
377 if (sh_type
== elfcpp::SHT_RELA
)
378 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
381 // The addend is stored in the section contents. Since this
382 // is not a relocatable link, we are going to apply the
383 // relocation contents to the section as usual. This means
384 // that we have no way to record the original addend. If the
385 // original addend is not zero, there is basically no way for
386 // the user to handle this correctly. Caveat emptor.
387 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
;
392 inline Relocatable_relocs::Reloc_strategy
393 global_strategy(unsigned int, Relobj
*, unsigned int)
394 { return Relocatable_relocs::RELOC_COPY
; }
397 // Scan the input relocations for --emit-relocs.
399 template<int size
, bool big_endian
>
401 Sized_relobj
<size
, big_endian
>::emit_relocs_scan(
402 const General_options
& options
,
403 Symbol_table
* symtab
,
405 const unsigned char* plocal_syms
,
406 const Read_relocs_data::Relocs_list::iterator
& p
)
408 Relocatable_relocs
* rr
= this->relocatable_relocs(p
->reloc_shndx
);
409 gold_assert(rr
!= NULL
);
410 rr
->set_reloc_count(p
->reloc_count
);
412 if (p
->sh_type
== elfcpp::SHT_REL
)
413 this->emit_relocs_scan_reltype
<elfcpp::SHT_REL
>(options
, symtab
, layout
,
417 gold_assert(p
->sh_type
== elfcpp::SHT_RELA
);
418 this->emit_relocs_scan_reltype
<elfcpp::SHT_RELA
>(options
, symtab
,
419 layout
, plocal_syms
, p
,
424 // Scan the input relocation for --emit-relocs, templatized on the
425 // type of the relocation section.
427 template<int size
, bool big_endian
>
428 template<int sh_type
>
430 Sized_relobj
<size
, big_endian
>::emit_relocs_scan_reltype(
431 const General_options
& options
,
432 Symbol_table
* symtab
,
434 const unsigned char* plocal_syms
,
435 const Read_relocs_data::Relocs_list::iterator
& p
,
436 Relocatable_relocs
* rr
)
438 scan_relocatable_relocs
<size
, big_endian
, sh_type
,
439 Emit_relocs_strategy
<sh_type
> >(
448 p
->needs_special_offset_handling
,
449 this->local_symbol_count_
,
454 // Relocate the input sections and write out the local symbols.
456 template<int size
, bool big_endian
>
458 Sized_relobj
<size
, big_endian
>::do_relocate(const General_options
& options
,
459 const Symbol_table
* symtab
,
460 const Layout
* layout
,
463 unsigned int shnum
= this->shnum();
465 // Read the section headers.
466 const unsigned char* pshdrs
= this->get_view(this->elf_file_
.shoff(),
467 shnum
* This::shdr_size
,
473 // Make two passes over the sections. The first one copies the
474 // section data to the output file. The second one applies
477 this->write_sections(pshdrs
, of
, &views
);
479 // To speed up relocations, we set up hash tables for fast lookup of
480 // input offsets to output addresses.
481 this->initialize_input_to_output_maps();
483 // Apply relocations.
485 this->relocate_sections(options
, symtab
, layout
, pshdrs
, &views
);
487 // After we've done the relocations, we release the hash tables,
488 // since we no longer need them.
489 this->free_input_to_output_maps();
491 // Write out the accumulated views.
492 for (unsigned int i
= 1; i
< shnum
; ++i
)
494 if (views
[i
].view
!= NULL
)
496 if (!views
[i
].is_postprocessing_view
)
498 if (views
[i
].is_input_output_view
)
499 of
->write_input_output_view(views
[i
].offset
,
503 of
->write_output_view(views
[i
].offset
, views
[i
].view_size
,
509 // Write out the local symbols.
510 this->write_local_symbols(of
, layout
->sympool(), layout
->dynpool(),
511 layout
->symtab_xindex(), layout
->dynsym_xindex());
513 // We should no longer need the local symbol values.
514 this->clear_local_symbols();
517 // Sort a Read_multiple vector by file offset.
518 struct Read_multiple_compare
521 operator()(const File_read::Read_multiple_entry
& rme1
,
522 const File_read::Read_multiple_entry
& rme2
) const
523 { return rme1
.file_offset
< rme2
.file_offset
; }
526 // Write section data to the output file. PSHDRS points to the
527 // section headers. Record the views in *PVIEWS for use when
530 template<int size
, bool big_endian
>
532 Sized_relobj
<size
, big_endian
>::write_sections(const unsigned char* pshdrs
,
536 unsigned int shnum
= this->shnum();
537 const std::vector
<Map_to_output
>& map_sections(this->map_to_output());
539 File_read::Read_multiple rm
;
540 bool is_sorted
= true;
542 const unsigned char* p
= pshdrs
+ This::shdr_size
;
543 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
545 View_size
* pvs
= &(*pviews
)[i
];
549 const Output_section
* os
= map_sections
[i
].output_section
;
552 off_t output_offset
= map_sections
[i
].offset
;
554 typename
This::Shdr
shdr(p
);
556 if (shdr
.get_sh_type() == elfcpp::SHT_NOBITS
)
559 if ((parameters
->options().relocatable()
560 || parameters
->options().emit_relocs())
561 && (shdr
.get_sh_type() == elfcpp::SHT_REL
562 || shdr
.get_sh_type() == elfcpp::SHT_RELA
)
563 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
565 // This is a reloc section in a relocatable link or when
566 // emitting relocs. We don't need to read the input file.
567 // The size and file offset are stored in the
568 // Relocatable_relocs structure.
569 Relocatable_relocs
* rr
= this->relocatable_relocs(i
);
570 gold_assert(rr
!= NULL
);
571 Output_data
* posd
= rr
->output_data();
572 gold_assert(posd
!= NULL
);
574 pvs
->offset
= posd
->offset();
575 pvs
->view_size
= posd
->data_size();
576 pvs
->view
= of
->get_output_view(pvs
->offset
, pvs
->view_size
);
577 pvs
->address
= posd
->address();
578 pvs
->is_input_output_view
= false;
579 pvs
->is_postprocessing_view
= false;
584 // In the normal case, this input section is simply mapped to
585 // the output section at offset OUTPUT_OFFSET.
587 // However, if OUTPUT_OFFSET == -1, then input data is handled
588 // specially--e.g., a .eh_frame section. The relocation
589 // routines need to check for each reloc where it should be
590 // applied. For this case, we need an input/output view for the
591 // entire contents of the section in the output file. We don't
592 // want to copy the contents of the input section to the output
593 // section; the output section contents were already written,
594 // and we waited for them in Relocate_task::is_runnable because
595 // relocs_must_follow_section_writes is set for the object.
597 // Regardless of which of the above cases is true, we have to
598 // check requires_postprocessing of the output section. If that
599 // is false, then we work with views of the output file
600 // directly. If it is true, then we work with a separate
601 // buffer, and the output section is responsible for writing the
602 // final data to the output file.
604 off_t output_section_offset
;
605 off_t output_section_size
;
606 if (!os
->requires_postprocessing())
608 output_section_offset
= os
->offset();
609 output_section_size
= os
->data_size();
613 output_section_offset
= 0;
614 output_section_size
= os
->postprocessing_buffer_size();
618 section_size_type view_size
;
619 if (output_offset
!= -1)
621 view_start
= output_section_offset
+ output_offset
;
622 view_size
= convert_to_section_size_type(shdr
.get_sh_size());
626 view_start
= output_section_offset
;
627 view_size
= convert_to_section_size_type(output_section_size
);
633 gold_assert(output_offset
== -1
634 || (output_offset
>= 0
635 && (output_offset
+ static_cast<off_t
>(view_size
)
636 <= output_section_size
)));
639 if (os
->requires_postprocessing())
641 unsigned char* buffer
= os
->postprocessing_buffer();
642 view
= buffer
+ view_start
;
643 if (output_offset
!= -1)
645 off_t sh_offset
= shdr
.get_sh_offset();
646 if (!rm
.empty() && rm
.back().file_offset
> sh_offset
)
648 rm
.push_back(File_read::Read_multiple_entry(sh_offset
,
654 if (output_offset
== -1)
655 view
= of
->get_input_output_view(view_start
, view_size
);
658 view
= of
->get_output_view(view_start
, view_size
);
659 off_t sh_offset
= shdr
.get_sh_offset();
660 if (!rm
.empty() && rm
.back().file_offset
> sh_offset
)
662 rm
.push_back(File_read::Read_multiple_entry(sh_offset
,
668 pvs
->address
= os
->address();
669 if (output_offset
!= -1)
670 pvs
->address
+= output_offset
;
671 pvs
->offset
= view_start
;
672 pvs
->view_size
= view_size
;
673 pvs
->is_input_output_view
= output_offset
== -1;
674 pvs
->is_postprocessing_view
= os
->requires_postprocessing();
677 // Actually read the data.
681 std::sort(rm
.begin(), rm
.end(), Read_multiple_compare());
682 this->read_multiple(rm
);
686 // Relocate section data. VIEWS points to the section data as views
687 // in the output file.
689 template<int size
, bool big_endian
>
691 Sized_relobj
<size
, big_endian
>::relocate_sections(
692 const General_options
& options
,
693 const Symbol_table
* symtab
,
694 const Layout
* layout
,
695 const unsigned char* pshdrs
,
698 unsigned int shnum
= this->shnum();
699 Sized_target
<size
, big_endian
>* target
= this->sized_target();
701 const std::vector
<Map_to_output
>& map_sections(this->map_to_output());
703 Relocate_info
<size
, big_endian
> relinfo
;
704 relinfo
.options
= &options
;
705 relinfo
.symtab
= symtab
;
706 relinfo
.layout
= layout
;
707 relinfo
.object
= this;
709 const unsigned char* p
= pshdrs
+ This::shdr_size
;
710 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
712 typename
This::Shdr
shdr(p
);
714 unsigned int sh_type
= shdr
.get_sh_type();
715 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
718 unsigned int index
= this->adjust_shndx(shdr
.get_sh_info());
719 if (index
>= this->shnum())
721 this->error(_("relocation section %u has bad info %u"),
726 Output_section
* os
= map_sections
[index
].output_section
;
729 // This relocation section is against a section which we
733 off_t output_offset
= map_sections
[index
].offset
;
735 gold_assert((*pviews
)[index
].view
!= NULL
);
736 if (parameters
->options().relocatable())
737 gold_assert((*pviews
)[i
].view
!= NULL
);
739 if (this->adjust_shndx(shdr
.get_sh_link()) != this->symtab_shndx_
)
741 gold_error(_("relocation section %u uses unexpected "
743 i
, this->adjust_shndx(shdr
.get_sh_link()));
747 off_t sh_size
= shdr
.get_sh_size();
748 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
749 sh_size
, true, false);
751 unsigned int reloc_size
;
752 if (sh_type
== elfcpp::SHT_REL
)
753 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
755 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
757 if (reloc_size
!= shdr
.get_sh_entsize())
759 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
760 i
, static_cast<unsigned long>(shdr
.get_sh_entsize()),
765 size_t reloc_count
= sh_size
/ reloc_size
;
766 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
768 gold_error(_("reloc section %u size %lu uneven"),
769 i
, static_cast<unsigned long>(sh_size
));
773 gold_assert(output_offset
!= -1
774 || this->relocs_must_follow_section_writes());
776 relinfo
.reloc_shndx
= i
;
777 relinfo
.data_shndx
= index
;
778 if (!parameters
->options().relocatable())
780 target
->relocate_section(&relinfo
,
786 (*pviews
)[index
].view
,
787 (*pviews
)[index
].address
,
788 (*pviews
)[index
].view_size
);
789 if (parameters
->options().emit_relocs())
790 this->emit_relocs(&relinfo
, i
, sh_type
, prelocs
, reloc_count
,
792 (*pviews
)[index
].view
,
793 (*pviews
)[index
].address
,
794 (*pviews
)[index
].view_size
,
796 (*pviews
)[i
].view_size
);
800 Relocatable_relocs
* rr
= this->relocatable_relocs(i
);
801 target
->relocate_for_relocatable(&relinfo
,
808 (*pviews
)[index
].view
,
809 (*pviews
)[index
].address
,
810 (*pviews
)[index
].view_size
,
812 (*pviews
)[i
].view_size
);
817 // Emit the relocs for --emit-relocs.
819 template<int size
, bool big_endian
>
821 Sized_relobj
<size
, big_endian
>::emit_relocs(
822 const Relocate_info
<size
, big_endian
>* relinfo
,
824 unsigned int sh_type
,
825 const unsigned char* prelocs
,
827 Output_section
* output_section
,
828 off_t offset_in_output_section
,
830 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
831 section_size_type view_size
,
832 unsigned char* reloc_view
,
833 section_size_type reloc_view_size
)
835 if (sh_type
== elfcpp::SHT_REL
)
836 this->emit_relocs_reltype
<elfcpp::SHT_REL
>(relinfo
, i
, prelocs
,
837 reloc_count
, output_section
,
838 offset_in_output_section
,
839 view
, address
, view_size
,
840 reloc_view
, reloc_view_size
);
843 gold_assert(sh_type
== elfcpp::SHT_RELA
);
844 this->emit_relocs_reltype
<elfcpp::SHT_RELA
>(relinfo
, i
, prelocs
,
845 reloc_count
, output_section
,
846 offset_in_output_section
,
847 view
, address
, view_size
,
848 reloc_view
, reloc_view_size
);
852 // Emit the relocs for --emit-relocs, templatized on the type of the
853 // relocation section.
855 template<int size
, bool big_endian
>
856 template<int sh_type
>
858 Sized_relobj
<size
, big_endian
>::emit_relocs_reltype(
859 const Relocate_info
<size
, big_endian
>* relinfo
,
861 const unsigned char* prelocs
,
863 Output_section
* output_section
,
864 off_t offset_in_output_section
,
866 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
867 section_size_type view_size
,
868 unsigned char* reloc_view
,
869 section_size_type reloc_view_size
)
871 const Relocatable_relocs
* rr
= this->relocatable_relocs(i
);
872 relocate_for_relocatable
<size
, big_endian
, sh_type
>(
877 offset_in_output_section
,
886 // Create merge hash tables for the local symbols. These are used to
887 // speed up relocations.
889 template<int size
, bool big_endian
>
891 Sized_relobj
<size
, big_endian
>::initialize_input_to_output_maps()
893 const unsigned int loccount
= this->local_symbol_count_
;
894 for (unsigned int i
= 1; i
< loccount
; ++i
)
896 Symbol_value
<size
>& lv(this->local_values_
[i
]);
897 lv
.initialize_input_to_output_map(this);
901 // Free merge hash tables for the local symbols.
903 template<int size
, bool big_endian
>
905 Sized_relobj
<size
, big_endian
>::free_input_to_output_maps()
907 const unsigned int loccount
= this->local_symbol_count_
;
908 for (unsigned int i
= 1; i
< loccount
; ++i
)
910 Symbol_value
<size
>& lv(this->local_values_
[i
]);
911 lv
.free_input_to_output_map();
915 // Class Merged_symbol_value.
919 Merged_symbol_value
<size
>::initialize_input_to_output_map(
920 const Relobj
* object
,
921 unsigned int input_shndx
)
923 Object_merge_map
* map
= object
->merge_map();
924 map
->initialize_input_to_output_map
<size
>(input_shndx
,
925 this->output_start_address_
,
926 &this->output_addresses_
);
929 // Get the output value corresponding to an input offset if we
930 // couldn't find it in the hash table.
933 typename
elfcpp::Elf_types
<size
>::Elf_Addr
934 Merged_symbol_value
<size
>::value_from_output_section(
935 const Relobj
* object
,
936 unsigned int input_shndx
,
937 typename
elfcpp::Elf_types
<size
>::Elf_Addr input_offset
) const
939 section_offset_type output_offset
;
940 bool found
= object
->merge_map()->get_output_offset(NULL
, input_shndx
,
944 // If this assertion fails, it means that some relocation was
945 // against a portion of an input merge section which we didn't map
946 // to the output file and we didn't explicitly discard. We should
947 // always map all portions of input merge sections.
950 if (output_offset
== -1)
953 return this->output_start_address_
+ output_offset
;
956 // Track_relocs methods.
958 // Initialize the class to track the relocs. This gets the object,
959 // the reloc section index, and the type of the relocs. This returns
960 // false if something goes wrong.
962 template<int size
, bool big_endian
>
964 Track_relocs
<size
, big_endian
>::initialize(
966 unsigned int reloc_shndx
,
967 unsigned int reloc_type
)
969 // If RELOC_SHNDX is -1U, it means there is more than one reloc
970 // section for the .eh_frame section. We can't handle that case.
971 if (reloc_shndx
== -1U)
974 // If RELOC_SHNDX is 0, there is no reloc section.
975 if (reloc_shndx
== 0)
978 // Get the contents of the reloc section.
979 this->prelocs_
= object
->section_contents(reloc_shndx
, &this->len_
, false);
981 if (reloc_type
== elfcpp::SHT_REL
)
982 this->reloc_size_
= elfcpp::Elf_sizes
<size
>::rel_size
;
983 else if (reloc_type
== elfcpp::SHT_RELA
)
984 this->reloc_size_
= elfcpp::Elf_sizes
<size
>::rela_size
;
988 if (this->len_
% this->reloc_size_
!= 0)
990 object
->error(_("reloc section size %zu is not a multiple of "
992 static_cast<size_t>(this->len_
),
1000 // Return the offset of the next reloc, or -1 if there isn't one.
1002 template<int size
, bool big_endian
>
1004 Track_relocs
<size
, big_endian
>::next_offset() const
1006 if (this->pos_
>= this->len_
)
1009 // Rel and Rela start out the same, so we can always use Rel to find
1010 // the r_offset value.
1011 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
1012 return rel
.get_r_offset();
1015 // Return the index of the symbol referenced by the next reloc, or -1U
1016 // if there aren't any more relocs.
1018 template<int size
, bool big_endian
>
1020 Track_relocs
<size
, big_endian
>::next_symndx() const
1022 if (this->pos_
>= this->len_
)
1025 // Rel and Rela start out the same, so we can use Rel to find the
1027 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
1028 return elfcpp::elf_r_sym
<size
>(rel
.get_r_info());
1031 // Advance to the next reloc whose r_offset is greater than or equal
1032 // to OFFSET. Return the number of relocs we skip.
1034 template<int size
, bool big_endian
>
1036 Track_relocs
<size
, big_endian
>::advance(off_t offset
)
1039 while (this->pos_
< this->len_
)
1041 // Rel and Rela start out the same, so we can always use Rel to
1042 // find the r_offset value.
1043 elfcpp::Rel
<size
, big_endian
> rel(this->prelocs_
+ this->pos_
);
1044 if (static_cast<off_t
>(rel
.get_r_offset()) >= offset
)
1047 this->pos_
+= this->reloc_size_
;
1052 // Instantiate the templates we need.
1054 #ifdef HAVE_TARGET_32_LITTLE
1057 Sized_relobj
<32, false>::do_read_relocs(Read_relocs_data
* rd
);
1060 #ifdef HAVE_TARGET_32_BIG
1063 Sized_relobj
<32, true>::do_read_relocs(Read_relocs_data
* rd
);
1066 #ifdef HAVE_TARGET_64_LITTLE
1069 Sized_relobj
<64, false>::do_read_relocs(Read_relocs_data
* rd
);
1072 #ifdef HAVE_TARGET_64_BIG
1075 Sized_relobj
<64, true>::do_read_relocs(Read_relocs_data
* rd
);
1078 #ifdef HAVE_TARGET_32_LITTLE
1081 Sized_relobj
<32, false>::do_scan_relocs(const General_options
& options
,
1082 Symbol_table
* symtab
,
1084 Read_relocs_data
* rd
);
1087 #ifdef HAVE_TARGET_32_BIG
1090 Sized_relobj
<32, true>::do_scan_relocs(const General_options
& options
,
1091 Symbol_table
* symtab
,
1093 Read_relocs_data
* rd
);
1096 #ifdef HAVE_TARGET_64_LITTLE
1099 Sized_relobj
<64, false>::do_scan_relocs(const General_options
& options
,
1100 Symbol_table
* symtab
,
1102 Read_relocs_data
* rd
);
1105 #ifdef HAVE_TARGET_64_BIG
1108 Sized_relobj
<64, true>::do_scan_relocs(const General_options
& options
,
1109 Symbol_table
* symtab
,
1111 Read_relocs_data
* rd
);
1114 #ifdef HAVE_TARGET_32_LITTLE
1117 Sized_relobj
<32, false>::do_relocate(const General_options
& options
,
1118 const Symbol_table
* symtab
,
1119 const Layout
* layout
,
1123 #ifdef HAVE_TARGET_32_BIG
1126 Sized_relobj
<32, true>::do_relocate(const General_options
& options
,
1127 const Symbol_table
* symtab
,
1128 const Layout
* layout
,
1132 #ifdef HAVE_TARGET_64_LITTLE
1135 Sized_relobj
<64, false>::do_relocate(const General_options
& options
,
1136 const Symbol_table
* symtab
,
1137 const Layout
* layout
,
1141 #ifdef HAVE_TARGET_64_BIG
1144 Sized_relobj
<64, true>::do_relocate(const General_options
& options
,
1145 const Symbol_table
* symtab
,
1146 const Layout
* layout
,
1150 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1152 class Merged_symbol_value
<32>;
1155 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1157 class Merged_symbol_value
<64>;
1160 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1162 class Symbol_value
<32>;
1165 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1167 class Symbol_value
<64>;
1170 #ifdef HAVE_TARGET_32_LITTLE
1172 class Track_relocs
<32, false>;
1175 #ifdef HAVE_TARGET_32_BIG
1177 class Track_relocs
<32, true>;
1180 #ifdef HAVE_TARGET_64_LITTLE
1182 class Track_relocs
<64, false>;
1185 #ifdef HAVE_TARGET_64_BIG
1187 class Track_relocs
<64, true>;
1190 } // End namespace gold.