1 // reloc.cc -- relocate input files for gold.
14 // Read_relocs methods.
16 // These tasks just read the relocation information from the file.
17 // After reading it, the start another task to process the
18 // information. These tasks requires access to the file.
20 Task::Is_runnable_type
21 Read_relocs::is_runnable(Workqueue
*)
23 return this->object_
->is_locked() ? IS_LOCKED
: IS_RUNNABLE
;
29 Read_relocs::locks(Workqueue
*)
31 return new Task_locker_obj
<Object
>(*this->object_
);
34 // Read the relocations and then start a Scan_relocs_task.
37 Read_relocs::run(Workqueue
* workqueue
)
39 Read_relocs_data
*rd
= new Read_relocs_data
;
40 this->object_
->read_relocs(rd
);
41 workqueue
->queue_front(new Scan_relocs(this->options_
, this->symtab_
,
42 this->layout_
, this->object_
, rd
,
43 this->symtab_lock_
, this->blocker_
));
46 // Scan_relocs methods.
48 // These tasks scan the relocations read by Read_relocs and mark up
49 // the symbol table to indicate which relocations are required. We
50 // use a lock on the symbol table to keep them from interfering with
53 Task::Is_runnable_type
54 Scan_relocs::is_runnable(Workqueue
*)
56 if (!this->symtab_lock_
->is_writable() || this->object_
->is_locked())
61 // Return the locks we hold: one on the file, one on the symbol table
64 class Scan_relocs::Scan_relocs_locker
: public Task_locker
67 Scan_relocs_locker(Object
* object
, Task_token
& symtab_lock
, Task
* task
,
68 Task_token
& blocker
, Workqueue
* workqueue
)
69 : objlock_(*object
), symtab_locker_(symtab_lock
, task
),
70 blocker_(blocker
, workqueue
)
74 Task_locker_obj
<Object
> objlock_
;
75 Task_locker_write symtab_locker_
;
76 Task_locker_block blocker_
;
80 Scan_relocs::locks(Workqueue
* workqueue
)
82 return new Scan_relocs_locker(this->object_
, *this->symtab_lock_
, this,
83 *this->blocker_
, workqueue
);
89 Scan_relocs::run(Workqueue
*)
91 this->object_
->scan_relocs(this->options_
, this->symtab_
, this->layout_
,
97 // Relocate_task methods.
99 // These tasks are always runnable.
101 Task::Is_runnable_type
102 Relocate_task::is_runnable(Workqueue
*)
107 // We want to lock the file while we run. We want to unblock
108 // FINAL_BLOCKER when we are done.
110 class Relocate_task::Relocate_locker
: public Task_locker
113 Relocate_locker(Task_token
& token
, Workqueue
* workqueue
,
115 : blocker_(token
, workqueue
), objlock_(*object
)
119 Task_locker_block blocker_
;
120 Task_locker_obj
<Object
> objlock_
;
124 Relocate_task::locks(Workqueue
* workqueue
)
126 return new Relocate_locker(*this->final_blocker_
, workqueue
,
133 Relocate_task::run(Workqueue
*)
135 this->object_
->relocate(this->options_
, this->symtab_
, this->layout_
,
139 // Read the relocs and local symbols from the object file and store
140 // the information in RD.
142 template<int size
, bool big_endian
>
144 Sized_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
148 unsigned int shnum
= this->shnum();
152 rd
->relocs
.reserve(shnum
/ 2);
154 const unsigned char *pshdrs
= this->get_view(this->elf_file_
.shoff(),
155 shnum
* This::shdr_size
);
156 // Skip the first, dummy, section.
157 const unsigned char *ps
= pshdrs
+ This::shdr_size
;
158 for (unsigned int i
= 1; i
< shnum
; ++i
, ps
+= This::shdr_size
)
160 typename
This::Shdr
shdr(ps
);
162 unsigned int sh_type
= shdr
.get_sh_type();
163 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
166 unsigned int shndx
= shdr
.get_sh_info();
169 fprintf(stderr
, _("%s: %s: relocation section %u has bad info %u\n"),
170 program_name
, this->name().c_str(), i
, shndx
);
174 if (!this->is_section_included(shndx
))
177 // We are scanning relocations in order to fill out the GOT and
178 // PLT sections. Relocations for sections which are not
179 // allocated (typically debugging sections) should not add new
180 // GOT and PLT entries. So we skip them.
181 typename
This::Shdr
secshdr(pshdrs
+ shndx
* This::shdr_size
);
182 if ((secshdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
185 if (shdr
.get_sh_link() != this->symtab_shndx_
)
188 _("%s: %s: relocation section %u uses unexpected "
189 "symbol table %u\n"),
190 program_name
, this->name().c_str(), i
, shdr
.get_sh_link());
194 off_t sh_size
= shdr
.get_sh_size();
196 unsigned int reloc_size
;
197 if (sh_type
== elfcpp::SHT_REL
)
198 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
200 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
201 if (reloc_size
!= shdr
.get_sh_entsize())
204 _("%s: %s: unexpected entsize for reloc section %u: "
206 program_name
, this->name().c_str(), i
,
207 static_cast<unsigned long>(shdr
.get_sh_entsize()),
212 size_t reloc_count
= sh_size
/ reloc_size
;
213 if (reloc_count
* reloc_size
!= sh_size
)
215 fprintf(stderr
, _("%s: %s: reloc section %u size %lu uneven"),
216 program_name
, this->name().c_str(), i
,
217 static_cast<unsigned long>(sh_size
));
221 rd
->relocs
.push_back(Section_relocs());
222 Section_relocs
& sr(rd
->relocs
.back());
224 sr
.data_shndx
= shndx
;
225 sr
.contents
= this->get_lasting_view(shdr
.get_sh_offset(), sh_size
);
226 sr
.sh_type
= sh_type
;
227 sr
.reloc_count
= reloc_count
;
230 // Read the local symbols.
231 gold_assert(this->symtab_shndx_
!= -1U);
232 if (this->symtab_shndx_
== 0 || this->local_symbol_count_
== 0)
233 rd
->local_symbols
= NULL
;
236 typename
This::Shdr
symtabshdr(pshdrs
237 + this->symtab_shndx_
* This::shdr_size
);
238 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
239 const int sym_size
= This::sym_size
;
240 const unsigned int loccount
= this->local_symbol_count_
;
241 gold_assert(loccount
== symtabshdr
.get_sh_info());
242 off_t locsize
= loccount
* sym_size
;
243 rd
->local_symbols
= this->get_lasting_view(symtabshdr
.get_sh_offset(),
248 // Scan the relocs and adjust the symbol table. This looks for
249 // relocations which require GOT/PLT/COPY relocations.
251 template<int size
, bool big_endian
>
253 Sized_relobj
<size
, big_endian
>::do_scan_relocs(const General_options
& options
,
254 Symbol_table
* symtab
,
256 Read_relocs_data
* rd
)
258 Sized_target
<size
, big_endian
>* target
= this->sized_target();
260 const unsigned char* local_symbols
;
261 if (rd
->local_symbols
== NULL
)
262 local_symbols
= NULL
;
264 local_symbols
= rd
->local_symbols
->data();
266 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
267 p
!= rd
->relocs
.end();
270 target
->scan_relocs(options
, symtab
, layout
, this, p
->data_shndx
,
271 p
->sh_type
, p
->contents
->data(), p
->reloc_count
,
272 this->local_symbol_count_
,
279 if (rd
->local_symbols
!= NULL
)
281 delete rd
->local_symbols
;
282 rd
->local_symbols
= NULL
;
286 // Relocate the input sections and write out the local symbols.
288 template<int size
, bool big_endian
>
290 Sized_relobj
<size
, big_endian
>::do_relocate(const General_options
& options
,
291 const Symbol_table
* symtab
,
292 const Layout
* layout
,
295 unsigned int shnum
= this->shnum();
297 // Read the section headers.
298 const unsigned char* pshdrs
= this->get_view(this->elf_file_
.shoff(),
299 shnum
* This::shdr_size
);
304 // Make two passes over the sections. The first one copies the
305 // section data to the output file. The second one applies
308 this->write_sections(pshdrs
, of
, &views
);
310 // Apply relocations.
312 this->relocate_sections(options
, symtab
, layout
, pshdrs
, &views
);
314 // Write out the accumulated views.
315 for (unsigned int i
= 1; i
< shnum
; ++i
)
317 if (views
[i
].view
!= NULL
)
318 of
->write_output_view(views
[i
].offset
, views
[i
].view_size
,
322 // Write out the local symbols.
323 this->write_local_symbols(of
, layout
->sympool());
326 // Write section data to the output file. PSHDRS points to the
327 // section headers. Record the views in *PVIEWS for use when
330 template<int size
, bool big_endian
>
332 Sized_relobj
<size
, big_endian
>::write_sections(const unsigned char* pshdrs
,
336 unsigned int shnum
= this->shnum();
337 std::vector
<Map_to_output
>& map_sections(this->map_to_output());
339 const unsigned char* p
= pshdrs
+ This::shdr_size
;
340 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
342 View_size
* pvs
= &(*pviews
)[i
];
346 if (map_sections
[i
].offset
== -1)
349 const Output_section
* os
= map_sections
[i
].output_section
;
353 typename
This::Shdr
shdr(p
);
355 if (shdr
.get_sh_type() == elfcpp::SHT_NOBITS
)
358 off_t start
= os
->offset() + map_sections
[i
].offset
;
359 off_t sh_size
= shdr
.get_sh_size();
364 gold_assert(map_sections
[i
].offset
>= 0
365 && map_sections
[i
].offset
+ sh_size
<= os
->data_size());
367 unsigned char* view
= of
->get_output_view(start
, sh_size
);
368 this->read(shdr
.get_sh_offset(), sh_size
, view
);
371 pvs
->address
= os
->address() + map_sections
[i
].offset
;
373 pvs
->view_size
= sh_size
;
377 // Relocate section data. VIEWS points to the section data as views
378 // in the output file.
380 template<int size
, bool big_endian
>
382 Sized_relobj
<size
, big_endian
>::relocate_sections(
383 const General_options
& options
,
384 const Symbol_table
* symtab
,
385 const Layout
* layout
,
386 const unsigned char* pshdrs
,
389 unsigned int shnum
= this->shnum();
390 Sized_target
<size
, big_endian
>* target
= this->sized_target();
392 Relocate_info
<size
, big_endian
> relinfo
;
393 relinfo
.options
= &options
;
394 relinfo
.symtab
= symtab
;
395 relinfo
.layout
= layout
;
396 relinfo
.object
= this;
397 relinfo
.local_symbol_count
= this->local_symbol_count_
;
398 relinfo
.local_values
= &this->local_values_
;
399 relinfo
.symbols
= this->symbols_
;
401 const unsigned char* p
= pshdrs
+ This::shdr_size
;
402 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
404 typename
This::Shdr
shdr(p
);
406 unsigned int sh_type
= shdr
.get_sh_type();
407 if (sh_type
!= elfcpp::SHT_REL
&& sh_type
!= elfcpp::SHT_RELA
)
410 unsigned int index
= shdr
.get_sh_info();
411 if (index
>= this->shnum())
413 fprintf(stderr
, _("%s: %s: relocation section %u has bad info %u\n"),
414 program_name
, this->name().c_str(), i
, index
);
418 if (!this->is_section_included(index
))
420 // This relocation section is against a section which we
425 gold_assert((*pviews
)[index
].view
!= NULL
);
427 if (shdr
.get_sh_link() != this->symtab_shndx_
)
430 _("%s: %s: relocation section %u uses unexpected "
431 "symbol table %u\n"),
432 program_name
, this->name().c_str(), i
, shdr
.get_sh_link());
436 off_t sh_size
= shdr
.get_sh_size();
437 const unsigned char* prelocs
= this->get_view(shdr
.get_sh_offset(),
440 unsigned int reloc_size
;
441 if (sh_type
== elfcpp::SHT_REL
)
442 reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
444 reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
446 if (reloc_size
!= shdr
.get_sh_entsize())
449 _("%s: %s: unexpected entsize for reloc section %u: "
451 program_name
, this->name().c_str(), i
,
452 static_cast<unsigned long>(shdr
.get_sh_entsize()),
457 size_t reloc_count
= sh_size
/ reloc_size
;
458 if (reloc_count
* reloc_size
!= sh_size
)
460 fprintf(stderr
, _("%s: %s: reloc section %u size %lu uneven"),
461 program_name
, this->name().c_str(), i
,
462 static_cast<unsigned long>(sh_size
));
466 relinfo
.reloc_shndx
= i
;
467 relinfo
.data_shndx
= index
;
468 target
->relocate_section(&relinfo
,
472 (*pviews
)[index
].view
,
473 (*pviews
)[index
].address
,
474 (*pviews
)[index
].view_size
);
478 // Copy_relocs::Copy_reloc_entry methods.
480 // Return whether we should emit this reloc. We should emit it if the
481 // symbol is still defined in a dynamic object. If we should not emit
482 // it, we clear it, to save ourselves the test next time.
484 template<int size
, bool big_endian
>
486 Copy_relocs
<size
, big_endian
>::Copy_reloc_entry::should_emit()
488 if (this->sym_
== NULL
)
490 if (this->sym_
->is_from_dynobj())
496 // Emit a reloc into a SHT_REL section.
498 template<int size
, bool big_endian
>
500 Copy_relocs
<size
, big_endian
>::Copy_reloc_entry::emit(
501 Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>* reloc_data
)
503 this->sym_
->set_needs_dynsym_entry();
504 reloc_data
->add_global(this->sym_
, this->reloc_type_
, this->relobj_
,
505 this->shndx_
, this->address_
);
508 // Emit a reloc into a SHT_RELA section.
510 template<int size
, bool big_endian
>
512 Copy_relocs
<size
, big_endian
>::Copy_reloc_entry::emit(
513 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>* reloc_data
)
515 this->sym_
->set_needs_dynsym_entry();
516 reloc_data
->add_global(this->sym_
, this->reloc_type_
, this->relobj_
,
517 this->shndx_
, this->address_
, this->addend_
);
520 // Copy_relocs methods.
522 // Return whether we need a COPY reloc for a relocation against GSYM.
523 // The relocation is being applied to section SHNDX in OBJECT.
525 template<int size
, bool big_endian
>
527 Copy_relocs
<size
, big_endian
>::need_copy_reloc(
528 const General_options
*,
531 Sized_symbol
<size
>* sym
)
533 // FIXME: Handle -z nocopyrelocs.
535 if (sym
->symsize() == 0)
538 // If this is a readonly section, then we need a COPY reloc.
539 // Otherwise we can use a dynamic reloc.
540 if ((object
->section_flags(shndx
) & elfcpp::SHF_WRITE
) == 0)
548 template<int size
, bool big_endian
>
550 Copy_relocs
<size
, big_endian
>::save(
554 const elfcpp::Rel
<size
, big_endian
>& rel
)
556 unsigned int reloc_type
= elfcpp::elf_r_type
<size
>(rel
.get_r_info());
557 this->entries_
.push_back(Copy_reloc_entry(sym
, reloc_type
, relobj
, shndx
,
558 rel
.get_r_offset(), 0));
561 // Save a Rela reloc.
563 template<int size
, bool big_endian
>
565 Copy_relocs
<size
, big_endian
>::save(
569 const elfcpp::Rela
<size
, big_endian
>& rela
)
571 unsigned int reloc_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
572 this->entries_
.push_back(Copy_reloc_entry(sym
, reloc_type
, relobj
, shndx
,
574 rela
.get_r_addend()));
577 // Return whether there are any relocs to emit. We don't want to emit
578 // a reloc if the symbol is no longer defined in a dynamic object.
580 template<int size
, bool big_endian
>
582 Copy_relocs
<size
, big_endian
>::any_to_emit()
584 for (typename
Copy_reloc_entries::iterator p
= this->entries_
.begin();
585 p
!= this->entries_
.end();
588 if (p
->should_emit())
596 template<int size
, bool big_endian
>
597 template<int sh_type
>
599 Copy_relocs
<size
, big_endian
>::emit(
600 Output_data_reloc
<sh_type
, true, size
, big_endian
>* reloc_data
)
602 for (typename
Copy_reloc_entries::iterator p
= this->entries_
.begin();
603 p
!= this->entries_
.end();
606 if (p
->should_emit())
611 // Instantiate the templates we need. We could use the configure
612 // script to restrict this to only the ones for implemented targets.
616 Sized_relobj
<32, false>::do_read_relocs(Read_relocs_data
* rd
);
620 Sized_relobj
<32, true>::do_read_relocs(Read_relocs_data
* rd
);
624 Sized_relobj
<64, false>::do_read_relocs(Read_relocs_data
* rd
);
628 Sized_relobj
<64, true>::do_read_relocs(Read_relocs_data
* rd
);
632 Sized_relobj
<32, false>::do_scan_relocs(const General_options
& options
,
633 Symbol_table
* symtab
,
635 Read_relocs_data
* rd
);
639 Sized_relobj
<32, true>::do_scan_relocs(const General_options
& options
,
640 Symbol_table
* symtab
,
642 Read_relocs_data
* rd
);
646 Sized_relobj
<64, false>::do_scan_relocs(const General_options
& options
,
647 Symbol_table
* symtab
,
649 Read_relocs_data
* rd
);
653 Sized_relobj
<64, true>::do_scan_relocs(const General_options
& options
,
654 Symbol_table
* symtab
,
656 Read_relocs_data
* rd
);
660 Sized_relobj
<32, false>::do_relocate(const General_options
& options
,
661 const Symbol_table
* symtab
,
662 const Layout
* layout
,
667 Sized_relobj
<32, true>::do_relocate(const General_options
& options
,
668 const Symbol_table
* symtab
,
669 const Layout
* layout
,
674 Sized_relobj
<64, false>::do_relocate(const General_options
& options
,
675 const Symbol_table
* symtab
,
676 const Layout
* layout
,
681 Sized_relobj
<64, true>::do_relocate(const General_options
& options
,
682 const Symbol_table
* symtab
,
683 const Layout
* layout
,
687 class Copy_relocs
<32, false>;
690 class Copy_relocs
<32, true>;
693 class Copy_relocs
<64, false>;
696 class Copy_relocs
<64, true>;
700 Copy_relocs
<32, false>::emit
<elfcpp::SHT_REL
>(
701 Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false>*);
705 Copy_relocs
<32, true>::emit
<elfcpp::SHT_REL
>(
706 Output_data_reloc
<elfcpp::SHT_REL
, true, 32, true>*);
710 Copy_relocs
<64, false>::emit
<elfcpp::SHT_REL
>(
711 Output_data_reloc
<elfcpp::SHT_REL
, true, 64, false>*);
715 Copy_relocs
<64, true>::emit
<elfcpp::SHT_REL
>(
716 Output_data_reloc
<elfcpp::SHT_REL
, true, 64, true>*);
720 Copy_relocs
<32, false>::emit
<elfcpp::SHT_RELA
>(
721 Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, false>*);
725 Copy_relocs
<32, true>::emit
<elfcpp::SHT_RELA
>(
726 Output_data_reloc
<elfcpp::SHT_RELA
, true, 32, true>*);
730 Copy_relocs
<64, false>::emit
<elfcpp::SHT_RELA
>(
731 Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false>*);
735 Copy_relocs
<64, true>::emit
<elfcpp::SHT_RELA
>(
736 Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, true>*);
738 } // End namespace gold.