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);
176 // Skip the first, dummy, section.
177 const unsigned char *ps = pshdrs + This::shdr_size;
178 for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size)
180 typename This::Shdr shdr(ps);
182 unsigned int sh_type = shdr.get_sh_type();
183 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
186 unsigned int shndx = shdr.get_sh_info();
189 fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"),
190 program_name, this->name().c_str(), i, shndx);
194 if (!this->is_section_included(shndx))
197 // We are scanning relocations in order to fill out the GOT and
198 // PLT sections. Relocations for sections which are not
199 // allocated (typically debugging sections) should not add new
200 // GOT and PLT entries. So we skip them.
201 typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
202 if ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
205 if (shdr.get_sh_link() != this->symtab_shndx_)
208 _("%s: %s: relocation section %u uses unexpected "
209 "symbol table %u\n"),
210 program_name, this->name().c_str(), 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())
224 _("%s: %s: unexpected entsize for reloc section %u: "
226 program_name, this->name().c_str(), i,
227 static_cast<unsigned long>(shdr.get_sh_entsize()),
232 size_t reloc_count = sh_size / reloc_size;
233 if (reloc_count * reloc_size != sh_size)
235 fprintf(stderr, _("%s: %s: reloc section %u size %lu uneven"),
236 program_name, this->name().c_str(), i,
237 static_cast<unsigned long>(sh_size));
241 rd->relocs.push_back(Section_relocs());
242 Section_relocs& sr(rd->relocs.back());
244 sr.data_shndx = shndx;
245 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size);
246 sr.sh_type = sh_type;
247 sr.reloc_count = reloc_count;
250 // Read the local symbols.
251 gold_assert(this->symtab_shndx_ != -1U);
252 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
253 rd->local_symbols = NULL;
256 typename This::Shdr symtabshdr(pshdrs
257 + this->symtab_shndx_ * This::shdr_size);
258 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
259 const int sym_size = This::sym_size;
260 const unsigned int loccount = this->local_symbol_count_;
261 gold_assert(loccount == symtabshdr.get_sh_info());
262 off_t locsize = loccount * sym_size;
263 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
268 // Scan the relocs and adjust the symbol table. This looks for
269 // relocations which require GOT/PLT/COPY relocations.
271 template<int size, bool big_endian>
273 Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
274 Symbol_table* symtab,
276 Read_relocs_data* rd)
278 Sized_target<size, big_endian>* target = this->sized_target();
280 const unsigned char* local_symbols;
281 if (rd->local_symbols == NULL)
282 local_symbols = NULL;
284 local_symbols = rd->local_symbols->data();
286 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
287 p != rd->relocs.end();
290 target->scan_relocs(options, symtab, layout, this, p->data_shndx,
291 p->sh_type, p->contents->data(), p->reloc_count,
292 this->local_symbol_count_,
299 if (rd->local_symbols != NULL)
301 delete rd->local_symbols;
302 rd->local_symbols = NULL;
306 // Relocate the input sections and write out the local symbols.
308 template<int size, bool big_endian>
310 Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
311 const Symbol_table* symtab,
312 const Layout* layout,
315 unsigned int shnum = this->shnum();
317 // Read the section headers.
318 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
319 shnum * This::shdr_size);
324 // Make two passes over the sections. The first one copies the
325 // section data to the output file. The second one applies
328 this->write_sections(pshdrs, of, &views);
330 // Apply relocations.
332 this->relocate_sections(options, symtab, layout, pshdrs, &views);
334 // Write out the accumulated views.
335 for (unsigned int i = 1; i < shnum; ++i)
337 if (views[i].view != NULL)
338 of->write_output_view(views[i].offset, views[i].view_size,
342 // Write out the local symbols.
343 this->write_local_symbols(of, layout->sympool());
346 // Write section data to the output file. PSHDRS points to the
347 // section headers. Record the views in *PVIEWS for use when
350 template<int size, bool big_endian>
352 Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
356 unsigned int shnum = this->shnum();
357 std::vector<Map_to_output>& map_sections(this->map_to_output());
359 const unsigned char* p = pshdrs + This::shdr_size;
360 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
362 View_size* pvs = &(*pviews)[i];
366 if (map_sections[i].offset == -1)
369 const Output_section* os = map_sections[i].output_section;
373 typename This::Shdr shdr(p);
375 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
378 off_t start = os->offset() + map_sections[i].offset;
379 off_t sh_size = shdr.get_sh_size();
384 gold_assert(map_sections[i].offset >= 0
385 && map_sections[i].offset + sh_size <= os->data_size());
387 unsigned char* view = of->get_output_view(start, sh_size);
388 this->read(shdr.get_sh_offset(), sh_size, view);
391 pvs->address = os->address() + map_sections[i].offset;
393 pvs->view_size = sh_size;
397 // Relocate section data. VIEWS points to the section data as views
398 // in the output file.
400 template<int size, bool big_endian>
402 Sized_relobj<size, big_endian>::relocate_sections(
403 const General_options& options,
404 const Symbol_table* symtab,
405 const Layout* layout,
406 const unsigned char* pshdrs,
409 unsigned int shnum = this->shnum();
410 Sized_target<size, big_endian>* target = this->sized_target();
412 Relocate_info<size, big_endian> relinfo;
413 relinfo.options = &options;
414 relinfo.symtab = symtab;
415 relinfo.layout = layout;
416 relinfo.object = this;
417 relinfo.local_symbol_count = this->local_symbol_count_;
418 relinfo.local_values = &this->local_values_;
419 relinfo.symbols = this->symbols_;
421 const unsigned char* p = pshdrs + This::shdr_size;
422 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
424 typename This::Shdr shdr(p);
426 unsigned int sh_type = shdr.get_sh_type();
427 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
430 unsigned int index = shdr.get_sh_info();
431 if (index >= this->shnum())
433 fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"),
434 program_name, this->name().c_str(), i, index);
438 if (!this->is_section_included(index))
440 // This relocation section is against a section which we
445 gold_assert((*pviews)[index].view != NULL);
447 if (shdr.get_sh_link() != this->symtab_shndx_)
450 _("%s: %s: relocation section %u uses unexpected "
451 "symbol table %u\n"),
452 program_name, this->name().c_str(), i, shdr.get_sh_link());
456 off_t sh_size = shdr.get_sh_size();
457 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
460 unsigned int reloc_size;
461 if (sh_type == elfcpp::SHT_REL)
462 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
464 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
466 if (reloc_size != shdr.get_sh_entsize())
469 _("%s: %s: unexpected entsize for reloc section %u: "
471 program_name, this->name().c_str(), i,
472 static_cast<unsigned long>(shdr.get_sh_entsize()),
477 size_t reloc_count = sh_size / reloc_size;
478 if (reloc_count * reloc_size != sh_size)
480 fprintf(stderr, _("%s: %s: reloc section %u size %lu uneven"),
481 program_name, this->name().c_str(), i,
482 static_cast<unsigned long>(sh_size));
486 relinfo.reloc_shndx = i;
487 relinfo.data_shndx = index;
488 target->relocate_section(&relinfo,
492 (*pviews)[index].view,
493 (*pviews)[index].address,
494 (*pviews)[index].view_size);
498 // Copy_relocs::Copy_reloc_entry methods.
500 // Return whether we should emit this reloc. We should emit it if the
501 // symbol is still defined in a dynamic object. If we should not emit
502 // it, we clear it, to save ourselves the test next time.
504 template<int size, bool big_endian>
506 Copy_relocs<size, big_endian>::Copy_reloc_entry::should_emit()
508 if (this->sym_ == NULL)
510 if (this->sym_->is_from_dynobj())
516 // Emit a reloc into a SHT_REL section.
518 template<int size, bool big_endian>
520 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
521 Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* reloc_data)
523 this->sym_->set_needs_dynsym_entry();
524 reloc_data->add_global(this->sym_, this->reloc_type_, this->relobj_,
525 this->shndx_, this->address_);
528 // Emit a reloc into a SHT_RELA section.
530 template<int size, bool big_endian>
532 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
533 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* reloc_data)
535 this->sym_->set_needs_dynsym_entry();
536 reloc_data->add_global(this->sym_, this->reloc_type_, this->relobj_,
537 this->shndx_, this->address_, this->addend_);
540 // Copy_relocs methods.
542 // Return whether we need a COPY reloc for a relocation against GSYM.
543 // The relocation is being applied to section SHNDX in OBJECT.
545 template<int size, bool big_endian>
547 Copy_relocs<size, big_endian>::need_copy_reloc(
548 const General_options*,
551 Sized_symbol<size>* sym)
553 // FIXME: Handle -z nocopyrelocs.
555 if (sym->symsize() == 0)
558 // If this is a readonly section, then we need a COPY reloc.
559 // Otherwise we can use a dynamic reloc.
560 if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
568 template<int size, bool big_endian>
570 Copy_relocs<size, big_endian>::save(
574 const elfcpp::Rel<size, big_endian>& rel)
576 unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info());
577 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
578 rel.get_r_offset(), 0));
581 // Save a Rela reloc.
583 template<int size, bool big_endian>
585 Copy_relocs<size, big_endian>::save(
589 const elfcpp::Rela<size, big_endian>& rela)
591 unsigned int reloc_type = elfcpp::elf_r_type<size>(rela.get_r_info());
592 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
594 rela.get_r_addend()));
597 // Return whether there are any relocs to emit. We don't want to emit
598 // a reloc if the symbol is no longer defined in a dynamic object.
600 template<int size, bool big_endian>
602 Copy_relocs<size, big_endian>::any_to_emit()
604 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
605 p != this->entries_.end();
608 if (p->should_emit())
616 template<int size, bool big_endian>
617 template<int sh_type>
619 Copy_relocs<size, big_endian>::emit(
620 Output_data_reloc<sh_type, true, size, big_endian>* reloc_data)
622 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
623 p != this->entries_.end();
626 if (p->should_emit())
631 // Instantiate the templates we need. We could use the configure
632 // script to restrict this to only the ones for implemented targets.
634 #ifdef HAVE_TARGET_32_LITTLE
637 Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
640 #ifdef HAVE_TARGET_32_BIG
643 Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
646 #ifdef HAVE_TARGET_64_LITTLE
649 Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
652 #ifdef HAVE_TARGET_64_BIG
655 Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
658 #ifdef HAVE_TARGET_32_LITTLE
661 Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
662 Symbol_table* symtab,
664 Read_relocs_data* rd);
667 #ifdef HAVE_TARGET_32_BIG
670 Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
671 Symbol_table* symtab,
673 Read_relocs_data* rd);
676 #ifdef HAVE_TARGET_64_LITTLE
679 Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
680 Symbol_table* symtab,
682 Read_relocs_data* rd);
685 #ifdef HAVE_TARGET_64_BIG
688 Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
689 Symbol_table* symtab,
691 Read_relocs_data* rd);
694 #ifdef HAVE_TARGET_32_LITTLE
697 Sized_relobj<32, false>::do_relocate(const General_options& options,
698 const Symbol_table* symtab,
699 const Layout* layout,
703 #ifdef HAVE_TARGET_32_BIG
706 Sized_relobj<32, true>::do_relocate(const General_options& options,
707 const Symbol_table* symtab,
708 const Layout* layout,
712 #ifdef HAVE_TARGET_64_LITTLE
715 Sized_relobj<64, false>::do_relocate(const General_options& options,
716 const Symbol_table* symtab,
717 const Layout* layout,
721 #ifdef HAVE_TARGET_64_BIG
724 Sized_relobj<64, true>::do_relocate(const General_options& options,
725 const Symbol_table* symtab,
726 const Layout* layout,
730 #ifdef HAVE_TARGET_32_LITTLE
732 class Copy_relocs<32, false>;
735 #ifdef HAVE_TARGET_32_BIG
737 class Copy_relocs<32, true>;
740 #ifdef HAVE_TARGET_64_LITTLE
742 class Copy_relocs<64, false>;
745 #ifdef HAVE_TARGET_64_BIG
747 class Copy_relocs<64, true>;
750 #ifdef HAVE_TARGET_32_LITTLE
753 Copy_relocs<32, false>::emit<elfcpp::SHT_REL>(
754 Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*);
757 #ifdef HAVE_TARGET_32_BIG
760 Copy_relocs<32, true>::emit<elfcpp::SHT_REL>(
761 Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*);
764 #ifdef HAVE_TARGET_64_LITTLE
767 Copy_relocs<64, false>::emit<elfcpp::SHT_REL>(
768 Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*);
771 #ifdef HAVE_TARGET_64_BIG
774 Copy_relocs<64, true>::emit<elfcpp::SHT_REL>(
775 Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*);
778 #ifdef HAVE_TARGET_32_LITTLE
781 Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>(
782 Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*);
785 #ifdef HAVE_TARGET_32_BIG
788 Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>(
789 Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*);
792 #ifdef HAVE_TARGET_64_LITTLE
795 Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>(
796 Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*);
799 #ifdef HAVE_TARGET_64_BIG
802 Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>(
803 Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*);
806 } // End namespace gold.