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.
25 #include "workqueue.h"
34 // Read_relocs methods.
36 // These tasks just read the relocation information from the file.
37 // After reading it, the start another task to process the
38 // information. These tasks requires access to the file.
40 Task::Is_runnable_type
41 Read_relocs::is_runnable(Workqueue
*)
43 return this->object_
->is_locked() ? IS_LOCKED
: IS_RUNNABLE
;
49 Read_relocs::locks(Workqueue
*)
51 return new Task_locker_obj
<Object
>(*this->object_
);
54 // Read the relocations and then start a Scan_relocs_task.
57 Read_relocs::run(Workqueue
* workqueue
)
59 Read_relocs_data
*rd
= new Read_relocs_data
;
60 this->object_
->read_relocs(rd
);
61 workqueue
->queue_front(new Scan_relocs(this->options_
, this->symtab_
,
62 this->layout_
, this->object_
, rd
,
63 this->symtab_lock_
, this->blocker_
));
66 // Scan_relocs methods.
68 // These tasks scan the relocations read by Read_relocs and mark up
69 // the symbol table to indicate which relocations are required. We
70 // use a lock on the symbol table to keep them from interfering with
73 Task::Is_runnable_type
74 Scan_relocs::is_runnable(Workqueue
*)
76 if (!this->symtab_lock_
->is_writable() || this->object_
->is_locked())
81 // Return the locks we hold: one on the file, one on the symbol table
84 class Scan_relocs::Scan_relocs_locker
: public Task_locker
87 Scan_relocs_locker(Object
* object
, Task_token
& symtab_lock
, Task
* task
,
88 Task_token
& blocker
, Workqueue
* workqueue
)
89 : objlock_(*object
), symtab_locker_(symtab_lock
, task
),
90 blocker_(blocker
, workqueue
)
94 Task_locker_obj
<Object
> objlock_
;
95 Task_locker_write symtab_locker_
;
96 Task_locker_block blocker_
;
100 Scan_relocs::locks(Workqueue
* workqueue
)
102 return new Scan_relocs_locker(this->object_
, *this->symtab_lock_
, this,
103 *this->blocker_
, workqueue
);
109 Scan_relocs::run(Workqueue
*)
111 this->object_
->scan_relocs(this->options_
, this->symtab_
, this->layout_
,
117 // Relocate_task methods.
119 // These tasks are always runnable.
121 Task::Is_runnable_type
122 Relocate_task::is_runnable(Workqueue
*)
127 // We want to lock the file while we run. We want to unblock
128 // FINAL_BLOCKER when we are done.
130 class Relocate_task::Relocate_locker
: public Task_locker
133 Relocate_locker(Task_token
& token
, Workqueue
* workqueue
,
135 : blocker_(token
, workqueue
), objlock_(*object
)
139 Task_locker_block blocker_
;
140 Task_locker_obj
<Object
> objlock_
;
144 Relocate_task::locks(Workqueue
* workqueue
)
146 return new Relocate_locker(*this->final_blocker_
, workqueue
,
153 Relocate_task::run(Workqueue
*)
155 this->object_
->relocate(this->options_
, this->symtab_
, this->layout_
,
159 // Read the relocs and local symbols from the object file and store
160 // the information in RD.
162 template<int size
, bool big_endian
>
164 Sized_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
168 unsigned int shnum
= this->shnum();
172 rd
->relocs
.reserve(shnum
/ 2);
174 const unsigned char *pshdrs
= this->get_view(this->elf_file_
.shoff(),
175 shnum
* This::shdr_size
,
177 // Skip the first, dummy, section.
178 const unsigned char *ps
= pshdrs
+ This::shdr_size
;
179 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= This::shdr_size
)
181 typename
This::Shdr
shdr(ps
);
183 unsigned int sh_type
= shdr
.get_sh_type();
184 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
187 unsigned int shndx
= shdr
.get_sh_info();
190 this->error(_("relocation section %u has bad info %u"),
195 if (!this->is_section_included(shndx
))
198 // We are scanning relocations in order to fill out the GOT and
199 // PLT sections. Relocations for sections which are not
200 // allocated (typically debugging sections) should not add new
201 // GOT and PLT entries. So we skip them.
202 typename
This::Shdr
secshdr(pshdrs
+ shndx
* This::shdr_size
);
203 if ((secshdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
206 if (shdr
.get_sh_link() != this->symtab_shndx_
)
208 this->error(_("relocation section %u uses unexpected "
210 i
, shdr
.get_sh_link());
214 off_t sh_size
= shdr
.get_sh_size();
216 unsigned int reloc_size
;
217 if (sh_type
== elfcpp::SHT_REL
)
218 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
220 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
221 if (reloc_size
!= shdr
.get_sh_entsize())
223 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
224 i
, static_cast<unsigned long>(shdr
.get_sh_entsize()),
229 size_t reloc_count
= sh_size
/ reloc_size
;
230 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
232 this->error(_("reloc section %u size %lu uneven"),
233 i
, static_cast<unsigned long>(sh_size
));
237 rd
->relocs
.push_back(Section_relocs());
238 Section_relocs
& sr(rd
->relocs
.back());
240 sr
.data_shndx
= shndx
;
241 sr
.contents
= this->get_lasting_view(shdr
.get_sh_offset(), sh_size
,
243 sr
.sh_type
= sh_type
;
244 sr
.reloc_count
= reloc_count
;
247 // Read the local symbols.
248 gold_assert(this->symtab_shndx_
!= -1U);
249 if (this->symtab_shndx_
== 0 || this->local_symbol_count_
== 0)
250 rd
->local_symbols
= NULL
;
253 typename
This::Shdr
symtabshdr(pshdrs
254 + this->symtab_shndx_
* This::shdr_size
);
255 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
256 const int sym_size
= This::sym_size
;
257 const unsigned int loccount
= this->local_symbol_count_
;
258 gold_assert(loccount
== symtabshdr
.get_sh_info());
259 off_t locsize
= loccount
* sym_size
;
260 rd
->local_symbols
= this->get_lasting_view(symtabshdr
.get_sh_offset(),
265 // Scan the relocs and adjust the symbol table. This looks for
266 // relocations which require GOT/PLT/COPY relocations.
268 template<int size
, bool big_endian
>
270 Sized_relobj
<size
, big_endian
>::do_scan_relocs(const General_options
& options
,
271 Symbol_table
* symtab
,
273 Read_relocs_data
* rd
)
275 Sized_target
<size
, big_endian
>* target
= this->sized_target();
277 const unsigned char* local_symbols
;
278 if (rd
->local_symbols
== NULL
)
279 local_symbols
= NULL
;
281 local_symbols
= rd
->local_symbols
->data();
283 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
284 p
!= rd
->relocs
.end();
287 target
->scan_relocs(options
, symtab
, layout
, this, p
->data_shndx
,
288 p
->sh_type
, p
->contents
->data(), p
->reloc_count
,
289 this->local_symbol_count_
,
296 if (rd
->local_symbols
!= NULL
)
298 delete rd
->local_symbols
;
299 rd
->local_symbols
= NULL
;
303 // Relocate the input sections and write out the local symbols.
305 template<int size
, bool big_endian
>
307 Sized_relobj
<size
, big_endian
>::do_relocate(const General_options
& options
,
308 const Symbol_table
* symtab
,
309 const Layout
* layout
,
312 unsigned int shnum
= this->shnum();
314 // Read the section headers.
315 const unsigned char* pshdrs
= this->get_view(this->elf_file_
.shoff(),
316 shnum
* This::shdr_size
,
322 // Make two passes over the sections. The first one copies the
323 // section data to the output file. The second one applies
326 this->write_sections(pshdrs
, of
, &views
);
328 // Apply relocations.
330 this->relocate_sections(options
, symtab
, layout
, pshdrs
, &views
);
332 // Write out the accumulated views.
333 for (unsigned int i
= 1; i
< shnum
; ++i
)
335 if (views
[i
].view
!= NULL
)
336 of
->write_output_view(views
[i
].offset
, views
[i
].view_size
,
340 // Write out the local symbols.
341 this->write_local_symbols(of
, layout
->sympool());
344 // Write section data to the output file. PSHDRS points to the
345 // section headers. Record the views in *PVIEWS for use when
348 template<int size
, bool big_endian
>
350 Sized_relobj
<size
, big_endian
>::write_sections(const unsigned char* pshdrs
,
354 unsigned int shnum
= this->shnum();
355 std::vector
<Map_to_output
>& map_sections(this->map_to_output());
357 const unsigned char* p
= pshdrs
+ This::shdr_size
;
358 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
360 View_size
* pvs
= &(*pviews
)[i
];
364 if (map_sections
[i
].offset
== -1)
367 const Output_section
* os
= map_sections
[i
].output_section
;
371 typename
This::Shdr
shdr(p
);
373 if (shdr
.get_sh_type() == elfcpp::SHT_NOBITS
)
376 off_t start
= os
->offset() + map_sections
[i
].offset
;
377 off_t sh_size
= shdr
.get_sh_size();
382 gold_assert(map_sections
[i
].offset
>= 0
383 && map_sections
[i
].offset
+ sh_size
<= os
->data_size());
385 unsigned char* view
= of
->get_output_view(start
, sh_size
);
386 this->read(shdr
.get_sh_offset(), sh_size
, view
);
389 pvs
->address
= os
->address() + map_sections
[i
].offset
;
391 pvs
->view_size
= sh_size
;
395 // Relocate section data. VIEWS points to the section data as views
396 // in the output file.
398 template<int size
, bool big_endian
>
400 Sized_relobj
<size
, big_endian
>::relocate_sections(
401 const General_options
& options
,
402 const Symbol_table
* symtab
,
403 const Layout
* layout
,
404 const unsigned char* pshdrs
,
407 unsigned int shnum
= this->shnum();
408 Sized_target
<size
, big_endian
>* target
= this->sized_target();
410 Relocate_info
<size
, big_endian
> relinfo
;
411 relinfo
.options
= &options
;
412 relinfo
.symtab
= symtab
;
413 relinfo
.layout
= layout
;
414 relinfo
.object
= this;
415 relinfo
.local_symbol_count
= this->local_symbol_count_
;
416 relinfo
.local_values
= &this->local_values_
;
417 relinfo
.symbols
= this->symbols_
;
419 const unsigned char* p
= pshdrs
+ This::shdr_size
;
420 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
422 typename
This::Shdr
shdr(p
);
424 unsigned int sh_type
= shdr
.get_sh_type();
425 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
428 unsigned int index
= shdr
.get_sh_info();
429 if (index
>= this->shnum())
431 this->error(_("relocation section %u has bad info %u"),
436 if (!this->is_section_included(index
))
438 // This relocation section is against a section which we
443 gold_assert((*pviews
)[index
].view
!= NULL
);
445 if (shdr
.get_sh_link() != this->symtab_shndx_
)
447 gold_error(_("relocation section %u uses unexpected "
449 i
, shdr
.get_sh_link());
453 off_t sh_size
= shdr
.get_sh_size();
454 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
457 unsigned int reloc_size
;
458 if (sh_type
== elfcpp::SHT_REL
)
459 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
461 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
463 if (reloc_size
!= shdr
.get_sh_entsize())
465 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
466 i
, static_cast<unsigned long>(shdr
.get_sh_entsize()),
471 size_t reloc_count
= sh_size
/ reloc_size
;
472 if (static_cast<off_t
>(reloc_count
* reloc_size
) != sh_size
)
474 gold_error(_("reloc section %u size %lu uneven"),
475 i
, static_cast<unsigned long>(sh_size
));
479 relinfo
.reloc_shndx
= i
;
480 relinfo
.data_shndx
= index
;
481 target
->relocate_section(&relinfo
,
485 (*pviews
)[index
].view
,
486 (*pviews
)[index
].address
,
487 (*pviews
)[index
].view_size
);
491 // Copy_relocs::Copy_reloc_entry methods.
493 // Return whether we should emit this reloc. We should emit it if the
494 // symbol is still defined in a dynamic object. If we should not emit
495 // it, we clear it, to save ourselves the test next time.
497 template<int size
, bool big_endian
>
499 Copy_relocs
<size
, big_endian
>::Copy_reloc_entry::should_emit()
501 if (this->sym_
== NULL
)
503 if (this->sym_
->is_from_dynobj())
509 // Emit a reloc into a SHT_REL section.
511 template<int size
, bool big_endian
>
513 Copy_relocs
<size
, big_endian
>::Copy_reloc_entry::emit(
514 Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>* reloc_data
)
516 this->sym_
->set_needs_dynsym_entry();
517 reloc_data
->add_global(this->sym_
, this->reloc_type_
, this->relobj_
,
518 this->shndx_
, this->address_
);
521 // Emit a reloc into a SHT_RELA section.
523 template<int size
, bool big_endian
>
525 Copy_relocs
<size
, big_endian
>::Copy_reloc_entry::emit(
526 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>* reloc_data
)
528 this->sym_
->set_needs_dynsym_entry();
529 reloc_data
->add_global(this->sym_
, this->reloc_type_
, this->relobj_
,
530 this->shndx_
, this->address_
, this->addend_
);
533 // Copy_relocs methods.
535 // Return whether we need a COPY reloc for a relocation against GSYM.
536 // The relocation is being applied to section SHNDX in OBJECT.
538 template<int size
, bool big_endian
>
540 Copy_relocs
<size
, big_endian
>::need_copy_reloc(
541 const General_options
*,
544 Sized_symbol
<size
>* sym
)
546 // FIXME: Handle -z nocopyrelocs.
548 if (sym
->symsize() == 0)
551 // If this is a readonly section, then we need a COPY reloc.
552 // Otherwise we can use a dynamic reloc.
553 if ((object
->section_flags(shndx
) & elfcpp::SHF_WRITE
) == 0)
561 template<int size
, bool big_endian
>
563 Copy_relocs
<size
, big_endian
>::save(
567 const elfcpp::Rel
<size
, big_endian
>& rel
)
569 unsigned int reloc_type
= elfcpp::elf_r_type
<size
>(rel
.get_r_info());
570 this->entries_
.push_back(Copy_reloc_entry(sym
, reloc_type
, relobj
, shndx
,
571 rel
.get_r_offset(), 0));
574 // Save a Rela reloc.
576 template<int size
, bool big_endian
>
578 Copy_relocs
<size
, big_endian
>::save(
582 const elfcpp::Rela
<size
, big_endian
>& rela
)
584 unsigned int reloc_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
585 this->entries_
.push_back(Copy_reloc_entry(sym
, reloc_type
, relobj
, shndx
,
587 rela
.get_r_addend()));
590 // Return whether there are any relocs to emit. We don't want to emit
591 // a reloc if the symbol is no longer defined in a dynamic object.
593 template<int size
, bool big_endian
>
595 Copy_relocs
<size
, big_endian
>::any_to_emit()
597 for (typename
Copy_reloc_entries::iterator p
= this->entries_
.begin();
598 p
!= this->entries_
.end();
601 if (p
->should_emit())
609 template<int size
, bool big_endian
>
610 template<int sh_type
>
612 Copy_relocs
<size
, big_endian
>::emit(
613 Output_data_reloc
<sh_type
, true, size
, big_endian
>* reloc_data
)
615 for (typename
Copy_reloc_entries::iterator p
= this->entries_
.begin();
616 p
!= this->entries_
.end();
619 if (p
->should_emit())
624 // Instantiate the templates we need. We could use the configure
625 // script to restrict this to only the ones for implemented targets.
627 #ifdef HAVE_TARGET_32_LITTLE
630 Sized_relobj
<32, false>::do_read_relocs(Read_relocs_data
* rd
);
633 #ifdef HAVE_TARGET_32_BIG
636 Sized_relobj
<32, true>::do_read_relocs(Read_relocs_data
* rd
);
639 #ifdef HAVE_TARGET_64_LITTLE
642 Sized_relobj
<64, false>::do_read_relocs(Read_relocs_data
* rd
);
645 #ifdef HAVE_TARGET_64_BIG
648 Sized_relobj
<64, true>::do_read_relocs(Read_relocs_data
* rd
);
651 #ifdef HAVE_TARGET_32_LITTLE
654 Sized_relobj
<32, false>::do_scan_relocs(const General_options
& options
,
655 Symbol_table
* symtab
,
657 Read_relocs_data
* rd
);
660 #ifdef HAVE_TARGET_32_BIG
663 Sized_relobj
<32, true>::do_scan_relocs(const General_options
& options
,
664 Symbol_table
* symtab
,
666 Read_relocs_data
* rd
);
669 #ifdef HAVE_TARGET_64_LITTLE
672 Sized_relobj
<64, false>::do_scan_relocs(const General_options
& options
,
673 Symbol_table
* symtab
,
675 Read_relocs_data
* rd
);
678 #ifdef HAVE_TARGET_64_BIG
681 Sized_relobj
<64, true>::do_scan_relocs(const General_options
& options
,
682 Symbol_table
* symtab
,
684 Read_relocs_data
* rd
);
687 #ifdef HAVE_TARGET_32_LITTLE
690 Sized_relobj
<32, false>::do_relocate(const General_options
& options
,
691 const Symbol_table
* symtab
,
692 const Layout
* layout
,
696 #ifdef HAVE_TARGET_32_BIG
699 Sized_relobj
<32, true>::do_relocate(const General_options
& options
,
700 const Symbol_table
* symtab
,
701 const Layout
* layout
,
705 #ifdef HAVE_TARGET_64_LITTLE
708 Sized_relobj
<64, false>::do_relocate(const General_options
& options
,
709 const Symbol_table
* symtab
,
710 const Layout
* layout
,
714 #ifdef HAVE_TARGET_64_BIG
717 Sized_relobj
<64, true>::do_relocate(const General_options
& options
,
718 const Symbol_table
* symtab
,
719 const Layout
* layout
,
723 #ifdef HAVE_TARGET_32_LITTLE
725 class Copy_relocs
<32, false>;
728 #ifdef HAVE_TARGET_32_BIG
730 class Copy_relocs
<32, true>;
733 #ifdef HAVE_TARGET_64_LITTLE
735 class Copy_relocs
<64, false>;
738 #ifdef HAVE_TARGET_64_BIG
740 class Copy_relocs
<64, true>;
743 #ifdef HAVE_TARGET_32_LITTLE
746 Copy_relocs
<32, false>::emit
<elfcpp::SHT_REL
>(
747 Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>*);
750 #ifdef HAVE_TARGET_32_BIG
753 Copy_relocs
<32, true>::emit
<elfcpp::SHT_REL
>(
754 Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>*);
757 #ifdef HAVE_TARGET_64_LITTLE
760 Copy_relocs
<64, false>::emit
<elfcpp::SHT_REL
>(
761 Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>*);
764 #ifdef HAVE_TARGET_64_BIG
767 Copy_relocs
<64, true>::emit
<elfcpp::SHT_REL
>(
768 Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>*);
771 #ifdef HAVE_TARGET_32_LITTLE
774 Copy_relocs
<32, false>::emit
<elfcpp::SHT_RELA
>(
775 Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>*);
778 #ifdef HAVE_TARGET_32_BIG
781 Copy_relocs
<32, true>::emit
<elfcpp::SHT_RELA
>(
782 Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>*);
785 #ifdef HAVE_TARGET_64_LITTLE
788 Copy_relocs
<64, false>::emit
<elfcpp::SHT_RELA
>(
789 Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>*);
792 #ifdef HAVE_TARGET_64_BIG
795 Copy_relocs
<64, true>::emit
<elfcpp::SHT_RELA
>(
796 Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>*);
799 } // End namespace gold.