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 fprintf(stderr
, _("%s: %s: relocation section %u has bad info %u\n"),
191 program_name
, this->name().c_str(), i
, shndx
);
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_
)
209 _("%s: %s: relocation section %u uses unexpected "
210 "symbol table %u\n"),
211 program_name
, this->name().c_str(), i
, shdr
.get_sh_link());
215 off_t sh_size
= shdr
.get_sh_size();
217 unsigned int reloc_size
;
218 if (sh_type
== elfcpp::SHT_REL
)
219 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
221 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
222 if (reloc_size
!= shdr
.get_sh_entsize())
225 _("%s: %s: unexpected entsize for reloc section %u: "
227 program_name
, this->name().c_str(), i
,
228 static_cast<unsigned long>(shdr
.get_sh_entsize()),
233 size_t reloc_count
= sh_size
/ reloc_size
;
234 if (reloc_count
* reloc_size
!= sh_size
)
236 fprintf(stderr
, _("%s: %s: reloc section %u size %lu uneven"),
237 program_name
, this->name().c_str(), i
,
238 static_cast<unsigned long>(sh_size
));
242 rd
->relocs
.push_back(Section_relocs());
243 Section_relocs
& sr(rd
->relocs
.back());
245 sr
.data_shndx
= shndx
;
246 sr
.contents
= this->get_lasting_view(shdr
.get_sh_offset(), sh_size
,
248 sr
.sh_type
= sh_type
;
249 sr
.reloc_count
= reloc_count
;
252 // Read the local symbols.
253 gold_assert(this->symtab_shndx_
!= -1U);
254 if (this->symtab_shndx_
== 0 || this->local_symbol_count_
== 0)
255 rd
->local_symbols
= NULL
;
258 typename
This::Shdr
symtabshdr(pshdrs
259 + this->symtab_shndx_
* This::shdr_size
);
260 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
261 const int sym_size
= This::sym_size
;
262 const unsigned int loccount
= this->local_symbol_count_
;
263 gold_assert(loccount
== symtabshdr
.get_sh_info());
264 off_t locsize
= loccount
* sym_size
;
265 rd
->local_symbols
= this->get_lasting_view(symtabshdr
.get_sh_offset(),
270 // Scan the relocs and adjust the symbol table. This looks for
271 // relocations which require GOT/PLT/COPY relocations.
273 template<int size
, bool big_endian
>
275 Sized_relobj
<size
, big_endian
>::do_scan_relocs(const General_options
& options
,
276 Symbol_table
* symtab
,
278 Read_relocs_data
* rd
)
280 Sized_target
<size
, big_endian
>* target
= this->sized_target();
282 const unsigned char* local_symbols
;
283 if (rd
->local_symbols
== NULL
)
284 local_symbols
= NULL
;
286 local_symbols
= rd
->local_symbols
->data();
288 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
289 p
!= rd
->relocs
.end();
292 target
->scan_relocs(options
, symtab
, layout
, this, p
->data_shndx
,
293 p
->sh_type
, p
->contents
->data(), p
->reloc_count
,
294 this->local_symbol_count_
,
301 if (rd
->local_symbols
!= NULL
)
303 delete rd
->local_symbols
;
304 rd
->local_symbols
= NULL
;
308 // Relocate the input sections and write out the local symbols.
310 template<int size
, bool big_endian
>
312 Sized_relobj
<size
, big_endian
>::do_relocate(const General_options
& options
,
313 const Symbol_table
* symtab
,
314 const Layout
* layout
,
317 unsigned int shnum
= this->shnum();
319 // Read the section headers.
320 const unsigned char* pshdrs
= this->get_view(this->elf_file_
.shoff(),
321 shnum
* This::shdr_size
,
327 // Make two passes over the sections. The first one copies the
328 // section data to the output file. The second one applies
331 this->write_sections(pshdrs
, of
, &views
);
333 // Apply relocations.
335 this->relocate_sections(options
, symtab
, layout
, pshdrs
, &views
);
337 // Write out the accumulated views.
338 for (unsigned int i
= 1; i
< shnum
; ++i
)
340 if (views
[i
].view
!= NULL
)
341 of
->write_output_view(views
[i
].offset
, views
[i
].view_size
,
345 // Write out the local symbols.
346 this->write_local_symbols(of
, layout
->sympool());
349 // Write section data to the output file. PSHDRS points to the
350 // section headers. Record the views in *PVIEWS for use when
353 template<int size
, bool big_endian
>
355 Sized_relobj
<size
, big_endian
>::write_sections(const unsigned char* pshdrs
,
359 unsigned int shnum
= this->shnum();
360 std::vector
<Map_to_output
>& map_sections(this->map_to_output());
362 const unsigned char* p
= pshdrs
+ This::shdr_size
;
363 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
365 View_size
* pvs
= &(*pviews
)[i
];
369 if (map_sections
[i
].offset
== -1)
372 const Output_section
* os
= map_sections
[i
].output_section
;
376 typename
This::Shdr
shdr(p
);
378 if (shdr
.get_sh_type() == elfcpp::SHT_NOBITS
)
381 off_t start
= os
->offset() + map_sections
[i
].offset
;
382 off_t sh_size
= shdr
.get_sh_size();
387 gold_assert(map_sections
[i
].offset
>= 0
388 && map_sections
[i
].offset
+ sh_size
<= os
->data_size());
390 unsigned char* view
= of
->get_output_view(start
, sh_size
);
391 this->read(shdr
.get_sh_offset(), sh_size
, view
);
394 pvs
->address
= os
->address() + map_sections
[i
].offset
;
396 pvs
->view_size
= sh_size
;
400 // Relocate section data. VIEWS points to the section data as views
401 // in the output file.
403 template<int size
, bool big_endian
>
405 Sized_relobj
<size
, big_endian
>::relocate_sections(
406 const General_options
& options
,
407 const Symbol_table
* symtab
,
408 const Layout
* layout
,
409 const unsigned char* pshdrs
,
412 unsigned int shnum
= this->shnum();
413 Sized_target
<size
, big_endian
>* target
= this->sized_target();
415 Relocate_info
<size
, big_endian
> relinfo
;
416 relinfo
.options
= &options
;
417 relinfo
.symtab
= symtab
;
418 relinfo
.layout
= layout
;
419 relinfo
.object
= this;
420 relinfo
.local_symbol_count
= this->local_symbol_count_
;
421 relinfo
.local_values
= &this->local_values_
;
422 relinfo
.symbols
= this->symbols_
;
424 const unsigned char* p
= pshdrs
+ This::shdr_size
;
425 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
427 typename
This::Shdr
shdr(p
);
429 unsigned int sh_type
= shdr
.get_sh_type();
430 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
433 unsigned int index
= shdr
.get_sh_info();
434 if (index
>= this->shnum())
436 fprintf(stderr
, _("%s: %s: relocation section %u has bad info %u\n"),
437 program_name
, this->name().c_str(), i
, index
);
441 if (!this->is_section_included(index
))
443 // This relocation section is against a section which we
448 gold_assert((*pviews
)[index
].view
!= NULL
);
450 if (shdr
.get_sh_link() != this->symtab_shndx_
)
453 _("%s: %s: relocation section %u uses unexpected "
454 "symbol table %u\n"),
455 program_name
, this->name().c_str(), i
, shdr
.get_sh_link());
459 off_t sh_size
= shdr
.get_sh_size();
460 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
463 unsigned int reloc_size
;
464 if (sh_type
== elfcpp::SHT_REL
)
465 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
467 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
469 if (reloc_size
!= shdr
.get_sh_entsize())
472 _("%s: %s: unexpected entsize for reloc section %u: "
474 program_name
, this->name().c_str(), i
,
475 static_cast<unsigned long>(shdr
.get_sh_entsize()),
480 size_t reloc_count
= sh_size
/ reloc_size
;
481 if (reloc_count
* reloc_size
!= sh_size
)
483 fprintf(stderr
, _("%s: %s: reloc section %u size %lu uneven"),
484 program_name
, this->name().c_str(), i
,
485 static_cast<unsigned long>(sh_size
));
489 relinfo
.reloc_shndx
= i
;
490 relinfo
.data_shndx
= index
;
491 target
->relocate_section(&relinfo
,
495 (*pviews
)[index
].view
,
496 (*pviews
)[index
].address
,
497 (*pviews
)[index
].view_size
);
501 // Copy_relocs::Copy_reloc_entry methods.
503 // Return whether we should emit this reloc. We should emit it if the
504 // symbol is still defined in a dynamic object. If we should not emit
505 // it, we clear it, to save ourselves the test next time.
507 template<int size
, bool big_endian
>
509 Copy_relocs
<size
, big_endian
>::Copy_reloc_entry::should_emit()
511 if (this->sym_
== NULL
)
513 if (this->sym_
->is_from_dynobj())
519 // Emit a reloc into a SHT_REL section.
521 template<int size
, bool big_endian
>
523 Copy_relocs
<size
, big_endian
>::Copy_reloc_entry::emit(
524 Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>* reloc_data
)
526 this->sym_
->set_needs_dynsym_entry();
527 reloc_data
->add_global(this->sym_
, this->reloc_type_
, this->relobj_
,
528 this->shndx_
, this->address_
);
531 // Emit a reloc into a SHT_RELA section.
533 template<int size
, bool big_endian
>
535 Copy_relocs
<size
, big_endian
>::Copy_reloc_entry::emit(
536 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>* reloc_data
)
538 this->sym_
->set_needs_dynsym_entry();
539 reloc_data
->add_global(this->sym_
, this->reloc_type_
, this->relobj_
,
540 this->shndx_
, this->address_
, this->addend_
);
543 // Copy_relocs methods.
545 // Return whether we need a COPY reloc for a relocation against GSYM.
546 // The relocation is being applied to section SHNDX in OBJECT.
548 template<int size
, bool big_endian
>
550 Copy_relocs
<size
, big_endian
>::need_copy_reloc(
551 const General_options
*,
554 Sized_symbol
<size
>* sym
)
556 // FIXME: Handle -z nocopyrelocs.
558 if (sym
->symsize() == 0)
561 // If this is a readonly section, then we need a COPY reloc.
562 // Otherwise we can use a dynamic reloc.
563 if ((object
->section_flags(shndx
) & elfcpp::SHF_WRITE
) == 0)
571 template<int size
, bool big_endian
>
573 Copy_relocs
<size
, big_endian
>::save(
577 const elfcpp::Rel
<size
, big_endian
>& rel
)
579 unsigned int reloc_type
= elfcpp::elf_r_type
<size
>(rel
.get_r_info());
580 this->entries_
.push_back(Copy_reloc_entry(sym
, reloc_type
, relobj
, shndx
,
581 rel
.get_r_offset(), 0));
584 // Save a Rela reloc.
586 template<int size
, bool big_endian
>
588 Copy_relocs
<size
, big_endian
>::save(
592 const elfcpp::Rela
<size
, big_endian
>& rela
)
594 unsigned int reloc_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
595 this->entries_
.push_back(Copy_reloc_entry(sym
, reloc_type
, relobj
, shndx
,
597 rela
.get_r_addend()));
600 // Return whether there are any relocs to emit. We don't want to emit
601 // a reloc if the symbol is no longer defined in a dynamic object.
603 template<int size
, bool big_endian
>
605 Copy_relocs
<size
, big_endian
>::any_to_emit()
607 for (typename
Copy_reloc_entries::iterator p
= this->entries_
.begin();
608 p
!= this->entries_
.end();
611 if (p
->should_emit())
619 template<int size
, bool big_endian
>
620 template<int sh_type
>
622 Copy_relocs
<size
, big_endian
>::emit(
623 Output_data_reloc
<sh_type
, true, size
, big_endian
>* reloc_data
)
625 for (typename
Copy_reloc_entries::iterator p
= this->entries_
.begin();
626 p
!= this->entries_
.end();
629 if (p
->should_emit())
634 // Instantiate the templates we need. We could use the configure
635 // script to restrict this to only the ones for implemented targets.
637 #ifdef HAVE_TARGET_32_LITTLE
640 Sized_relobj
<32, false>::do_read_relocs(Read_relocs_data
* rd
);
643 #ifdef HAVE_TARGET_32_BIG
646 Sized_relobj
<32, true>::do_read_relocs(Read_relocs_data
* rd
);
649 #ifdef HAVE_TARGET_64_LITTLE
652 Sized_relobj
<64, false>::do_read_relocs(Read_relocs_data
* rd
);
655 #ifdef HAVE_TARGET_64_BIG
658 Sized_relobj
<64, true>::do_read_relocs(Read_relocs_data
* rd
);
661 #ifdef HAVE_TARGET_32_LITTLE
664 Sized_relobj
<32, false>::do_scan_relocs(const General_options
& options
,
665 Symbol_table
* symtab
,
667 Read_relocs_data
* rd
);
670 #ifdef HAVE_TARGET_32_BIG
673 Sized_relobj
<32, true>::do_scan_relocs(const General_options
& options
,
674 Symbol_table
* symtab
,
676 Read_relocs_data
* rd
);
679 #ifdef HAVE_TARGET_64_LITTLE
682 Sized_relobj
<64, false>::do_scan_relocs(const General_options
& options
,
683 Symbol_table
* symtab
,
685 Read_relocs_data
* rd
);
688 #ifdef HAVE_TARGET_64_BIG
691 Sized_relobj
<64, true>::do_scan_relocs(const General_options
& options
,
692 Symbol_table
* symtab
,
694 Read_relocs_data
* rd
);
697 #ifdef HAVE_TARGET_32_LITTLE
700 Sized_relobj
<32, false>::do_relocate(const General_options
& options
,
701 const Symbol_table
* symtab
,
702 const Layout
* layout
,
706 #ifdef HAVE_TARGET_32_BIG
709 Sized_relobj
<32, true>::do_relocate(const General_options
& options
,
710 const Symbol_table
* symtab
,
711 const Layout
* layout
,
715 #ifdef HAVE_TARGET_64_LITTLE
718 Sized_relobj
<64, false>::do_relocate(const General_options
& options
,
719 const Symbol_table
* symtab
,
720 const Layout
* layout
,
724 #ifdef HAVE_TARGET_64_BIG
727 Sized_relobj
<64, true>::do_relocate(const General_options
& options
,
728 const Symbol_table
* symtab
,
729 const Layout
* layout
,
733 #ifdef HAVE_TARGET_32_LITTLE
735 class Copy_relocs
<32, false>;
738 #ifdef HAVE_TARGET_32_BIG
740 class Copy_relocs
<32, true>;
743 #ifdef HAVE_TARGET_64_LITTLE
745 class Copy_relocs
<64, false>;
748 #ifdef HAVE_TARGET_64_BIG
750 class Copy_relocs
<64, true>;
753 #ifdef HAVE_TARGET_32_LITTLE
756 Copy_relocs
<32, false>::emit
<elfcpp::SHT_REL
>(
757 Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>*);
760 #ifdef HAVE_TARGET_32_BIG
763 Copy_relocs
<32, true>::emit
<elfcpp::SHT_REL
>(
764 Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>*);
767 #ifdef HAVE_TARGET_64_LITTLE
770 Copy_relocs
<64, false>::emit
<elfcpp::SHT_REL
>(
771 Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>*);
774 #ifdef HAVE_TARGET_64_BIG
777 Copy_relocs
<64, true>::emit
<elfcpp::SHT_REL
>(
778 Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>*);
781 #ifdef HAVE_TARGET_32_LITTLE
784 Copy_relocs
<32, false>::emit
<elfcpp::SHT_RELA
>(
785 Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>*);
788 #ifdef HAVE_TARGET_32_BIG
791 Copy_relocs
<32, true>::emit
<elfcpp::SHT_RELA
>(
792 Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>*);
795 #ifdef HAVE_TARGET_64_LITTLE
798 Copy_relocs
<64, false>::emit
<elfcpp::SHT_RELA
>(
799 Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>*);
802 #ifdef HAVE_TARGET_64_BIG
805 Copy_relocs
<64, true>::emit
<elfcpp::SHT_RELA
>(
806 Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>*);
809 } // End namespace gold.