Added a testsuite. More support for COPY relocations.
[deliverable/binutils-gdb.git] / gold / reloc.cc
1 // reloc.cc -- relocate input files for gold.
2
3 #include "gold.h"
4
5 #include "workqueue.h"
6 #include "object.h"
7 #include "symtab.h"
8 #include "output.h"
9 #include "reloc.h"
10
11 namespace gold
12 {
13
14 // Read_relocs methods.
15
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.
19
20 Task::Is_runnable_type
21 Read_relocs::is_runnable(Workqueue*)
22 {
23 return this->object_->is_locked() ? IS_LOCKED : IS_RUNNABLE;
24 }
25
26 // Lock the file.
27
28 Task_locker*
29 Read_relocs::locks(Workqueue*)
30 {
31 return new Task_locker_obj<Object>(*this->object_);
32 }
33
34 // Read the relocations and then start a Scan_relocs_task.
35
36 void
37 Read_relocs::run(Workqueue* workqueue)
38 {
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_));
44 }
45
46 // Scan_relocs methods.
47
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
51 // each other.
52
53 Task::Is_runnable_type
54 Scan_relocs::is_runnable(Workqueue*)
55 {
56 if (!this->symtab_lock_->is_writable() || this->object_->is_locked())
57 return IS_LOCKED;
58 return IS_RUNNABLE;
59 }
60
61 // Return the locks we hold: one on the file, one on the symbol table
62 // and one blocker.
63
64 class Scan_relocs::Scan_relocs_locker : public Task_locker
65 {
66 public:
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)
71 { }
72
73 private:
74 Task_locker_obj<Object> objlock_;
75 Task_locker_write symtab_locker_;
76 Task_locker_block blocker_;
77 };
78
79 Task_locker*
80 Scan_relocs::locks(Workqueue* workqueue)
81 {
82 return new Scan_relocs_locker(this->object_, *this->symtab_lock_, this,
83 *this->blocker_, workqueue);
84 }
85
86 // Scan the relocs.
87
88 void
89 Scan_relocs::run(Workqueue*)
90 {
91 this->object_->scan_relocs(this->options_, this->symtab_, this->layout_,
92 this->rd_);
93 delete this->rd_;
94 this->rd_ = NULL;
95 }
96
97 // Relocate_task methods.
98
99 // These tasks are always runnable.
100
101 Task::Is_runnable_type
102 Relocate_task::is_runnable(Workqueue*)
103 {
104 return IS_RUNNABLE;
105 }
106
107 // We want to lock the file while we run. We want to unblock
108 // FINAL_BLOCKER when we are done.
109
110 class Relocate_task::Relocate_locker : public Task_locker
111 {
112 public:
113 Relocate_locker(Task_token& token, Workqueue* workqueue,
114 Object* object)
115 : blocker_(token, workqueue), objlock_(*object)
116 { }
117
118 private:
119 Task_locker_block blocker_;
120 Task_locker_obj<Object> objlock_;
121 };
122
123 Task_locker*
124 Relocate_task::locks(Workqueue* workqueue)
125 {
126 return new Relocate_locker(*this->final_blocker_, workqueue,
127 this->object_);
128 }
129
130 // Run the task.
131
132 void
133 Relocate_task::run(Workqueue*)
134 {
135 this->object_->relocate(this->options_, this->symtab_, this->layout_,
136 this->of_);
137 }
138
139 // Read the relocs and local symbols from the object file and store
140 // the information in RD.
141
142 template<int size, bool big_endian>
143 void
144 Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
145 {
146 rd->relocs.clear();
147
148 unsigned int shnum = this->shnum();
149 if (shnum == 0)
150 return;
151
152 rd->relocs.reserve(shnum / 2);
153
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)
159 {
160 typename This::Shdr shdr(ps);
161
162 unsigned int sh_type = shdr.get_sh_type();
163 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
164 continue;
165
166 unsigned int shndx = shdr.get_sh_info();
167 if (shndx >= shnum)
168 {
169 fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"),
170 program_name, this->name().c_str(), i, shndx);
171 gold_exit(false);
172 }
173
174 if (!this->is_section_included(shndx))
175 continue;
176
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)
183 continue;
184
185 if (shdr.get_sh_link() != this->symtab_shndx_)
186 {
187 fprintf(stderr,
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());
191 gold_exit(false);
192 }
193
194 off_t sh_size = shdr.get_sh_size();
195
196 unsigned int reloc_size;
197 if (sh_type == elfcpp::SHT_REL)
198 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
199 else
200 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
201 if (reloc_size != shdr.get_sh_entsize())
202 {
203 fprintf(stderr,
204 _("%s: %s: unexpected entsize for reloc section %u: "
205 "%lu != %u"),
206 program_name, this->name().c_str(), i,
207 static_cast<unsigned long>(shdr.get_sh_entsize()),
208 reloc_size);
209 gold_exit(false);
210 }
211
212 size_t reloc_count = sh_size / reloc_size;
213 if (reloc_count * reloc_size != sh_size)
214 {
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));
218 gold_exit(false);
219 }
220
221 rd->relocs.push_back(Section_relocs());
222 Section_relocs& sr(rd->relocs.back());
223 sr.reloc_shndx = i;
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;
228 }
229
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;
234 else
235 {
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(),
244 locsize);
245 }
246 }
247
248 // Scan the relocs and adjust the symbol table. This looks for
249 // relocations which require GOT/PLT/COPY relocations.
250
251 template<int size, bool big_endian>
252 void
253 Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
254 Symbol_table* symtab,
255 Layout* layout,
256 Read_relocs_data* rd)
257 {
258 Sized_target<size, big_endian>* target = this->sized_target();
259
260 const unsigned char* local_symbols;
261 if (rd->local_symbols == NULL)
262 local_symbols = NULL;
263 else
264 local_symbols = rd->local_symbols->data();
265
266 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
267 p != rd->relocs.end();
268 ++p)
269 {
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_,
273 local_symbols,
274 this->symbols_);
275 delete p->contents;
276 p->contents = NULL;
277 }
278
279 if (rd->local_symbols != NULL)
280 {
281 delete rd->local_symbols;
282 rd->local_symbols = NULL;
283 }
284 }
285
286 // Relocate the input sections and write out the local symbols.
287
288 template<int size, bool big_endian>
289 void
290 Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
291 const Symbol_table* symtab,
292 const Layout* layout,
293 Output_file* of)
294 {
295 unsigned int shnum = this->shnum();
296
297 // Read the section headers.
298 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
299 shnum * This::shdr_size);
300
301 Views views;
302 views.resize(shnum);
303
304 // Make two passes over the sections. The first one copies the
305 // section data to the output file. The second one applies
306 // relocations.
307
308 this->write_sections(pshdrs, of, &views);
309
310 // Apply relocations.
311
312 this->relocate_sections(options, symtab, layout, pshdrs, &views);
313
314 // Write out the accumulated views.
315 for (unsigned int i = 1; i < shnum; ++i)
316 {
317 if (views[i].view != NULL)
318 of->write_output_view(views[i].offset, views[i].view_size,
319 views[i].view);
320 }
321
322 // Write out the local symbols.
323 this->write_local_symbols(of, layout->sympool());
324 }
325
326 // Write section data to the output file. PSHDRS points to the
327 // section headers. Record the views in *PVIEWS for use when
328 // relocating.
329
330 template<int size, bool big_endian>
331 void
332 Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
333 Output_file* of,
334 Views* pviews)
335 {
336 unsigned int shnum = this->shnum();
337 std::vector<Map_to_output>& map_sections(this->map_to_output());
338
339 const unsigned char* p = pshdrs + This::shdr_size;
340 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
341 {
342 View_size* pvs = &(*pviews)[i];
343
344 pvs->view = NULL;
345
346 const Output_section* os = map_sections[i].output_section;
347 if (os == NULL)
348 continue;
349
350 typename This::Shdr shdr(p);
351
352 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
353 continue;
354
355 off_t start = os->offset() + map_sections[i].offset;
356 off_t sh_size = shdr.get_sh_size();
357
358 if (sh_size == 0)
359 continue;
360
361 gold_assert(map_sections[i].offset >= 0
362 && map_sections[i].offset + sh_size <= os->data_size());
363
364 unsigned char* view = of->get_output_view(start, sh_size);
365 this->read(shdr.get_sh_offset(), sh_size, view);
366
367 pvs->view = view;
368 pvs->address = os->address() + map_sections[i].offset;
369 pvs->offset = start;
370 pvs->view_size = sh_size;
371 }
372 }
373
374 // Relocate section data. VIEWS points to the section data as views
375 // in the output file.
376
377 template<int size, bool big_endian>
378 void
379 Sized_relobj<size, big_endian>::relocate_sections(
380 const General_options& options,
381 const Symbol_table* symtab,
382 const Layout* layout,
383 const unsigned char* pshdrs,
384 Views* pviews)
385 {
386 unsigned int shnum = this->shnum();
387 Sized_target<size, big_endian>* target = this->sized_target();
388
389 Relocate_info<size, big_endian> relinfo;
390 relinfo.options = &options;
391 relinfo.symtab = symtab;
392 relinfo.layout = layout;
393 relinfo.object = this;
394 relinfo.local_symbol_count = this->local_symbol_count_;
395 relinfo.local_values = &this->local_values_;
396 relinfo.symbols = this->symbols_;
397
398 const unsigned char* p = pshdrs + This::shdr_size;
399 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
400 {
401 typename This::Shdr shdr(p);
402
403 unsigned int sh_type = shdr.get_sh_type();
404 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
405 continue;
406
407 unsigned int index = shdr.get_sh_info();
408 if (index >= this->shnum())
409 {
410 fprintf(stderr, _("%s: %s: relocation section %u has bad info %u\n"),
411 program_name, this->name().c_str(), i, index);
412 gold_exit(false);
413 }
414
415 if (!this->is_section_included(index))
416 {
417 // This relocation section is against a section which we
418 // discarded.
419 continue;
420 }
421
422 gold_assert((*pviews)[index].view != NULL);
423
424 if (shdr.get_sh_link() != this->symtab_shndx_)
425 {
426 fprintf(stderr,
427 _("%s: %s: relocation section %u uses unexpected "
428 "symbol table %u\n"),
429 program_name, this->name().c_str(), i, shdr.get_sh_link());
430 gold_exit(false);
431 }
432
433 off_t sh_size = shdr.get_sh_size();
434 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
435 sh_size);
436
437 unsigned int reloc_size;
438 if (sh_type == elfcpp::SHT_REL)
439 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
440 else
441 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
442
443 if (reloc_size != shdr.get_sh_entsize())
444 {
445 fprintf(stderr,
446 _("%s: %s: unexpected entsize for reloc section %u: "
447 "%lu != %u"),
448 program_name, this->name().c_str(), i,
449 static_cast<unsigned long>(shdr.get_sh_entsize()),
450 reloc_size);
451 gold_exit(false);
452 }
453
454 size_t reloc_count = sh_size / reloc_size;
455 if (reloc_count * reloc_size != sh_size)
456 {
457 fprintf(stderr, _("%s: %s: reloc section %u size %lu uneven"),
458 program_name, this->name().c_str(), i,
459 static_cast<unsigned long>(sh_size));
460 gold_exit(false);
461 }
462
463 relinfo.reloc_shndx = i;
464 relinfo.data_shndx = index;
465 target->relocate_section(&relinfo,
466 sh_type,
467 prelocs,
468 reloc_count,
469 (*pviews)[index].view,
470 (*pviews)[index].address,
471 (*pviews)[index].view_size);
472 }
473 }
474
475 // Copy_relocs::Copy_reloc_entry methods.
476
477 // Return whether we should emit this reloc. We should emit it if the
478 // symbol is still defined in a dynamic object. If we should not emit
479 // it, we clear it, to save ourselves the test next time.
480
481 template<int size, bool big_endian>
482 bool
483 Copy_relocs<size, big_endian>::Copy_reloc_entry::should_emit()
484 {
485 if (this->sym_ == NULL)
486 return false;
487 if (this->sym_->is_defined_in_dynobj())
488 return true;
489 this->sym_ = NULL;
490 return false;
491 }
492
493 // Emit a reloc into a SHT_REL section.
494
495 template<int size, bool big_endian>
496 void
497 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
498 Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* reloc_data)
499 {
500 reloc_data->add_global(this->sym_, this->reloc_type_, this->relobj_,
501 this->shndx_, this->address_);
502 }
503
504 // Emit a reloc into a SHT_RELA section.
505
506 template<int size, bool big_endian>
507 void
508 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
509 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* reloc_data)
510 {
511 reloc_data->add_global(this->sym_, this->reloc_type_, this->relobj_,
512 this->shndx_, this->address_, this->addend_);
513 }
514
515 // Copy_relocs methods.
516
517 // Return whether we need a COPY reloc for a relocation against GSYM.
518 // The relocation is being applied to section SHNDX in OBJECT.
519
520 template<int size, bool big_endian>
521 bool
522 Copy_relocs<size, big_endian>::need_copy_reloc(
523 const General_options*,
524 Relobj* object,
525 unsigned int shndx,
526 Sized_symbol<size>* sym)
527 {
528 // FIXME: Handle -z nocopyrelocs.
529
530 if (sym->symsize() == 0)
531 return false;
532
533 // If this is a readonly section, then we need a COPY reloc.
534 // Otherwise we can use a dynamic reloc.
535 if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
536 return true;
537
538 return false;
539 }
540
541 // Save a Rel reloc.
542
543 template<int size, bool big_endian>
544 void
545 Copy_relocs<size, big_endian>::save(
546 Symbol* sym,
547 Relobj* relobj,
548 unsigned int shndx,
549 const elfcpp::Rel<size, big_endian>& rel)
550 {
551 unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info());
552 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
553 rel.get_r_offset(), 0));
554 }
555
556 // Save a Rela reloc.
557
558 template<int size, bool big_endian>
559 void
560 Copy_relocs<size, big_endian>::save(
561 Symbol* sym,
562 Relobj* relobj,
563 unsigned int shndx,
564 const elfcpp::Rela<size, big_endian>& rela)
565 {
566 unsigned int reloc_type = elfcpp::elf_r_type<size>(rela.get_r_info());
567 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
568 rela.get_r_offset(),
569 rela.get_r_addend()));
570 }
571
572 // Return whether there are any relocs to emit. We don't want to emit
573 // a reloc if the symbol is no longer defined in a dynamic object.
574
575 template<int size, bool big_endian>
576 bool
577 Copy_relocs<size, big_endian>::any_to_emit()
578 {
579 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
580 p != this->entries_.end();
581 ++p)
582 {
583 if (p->should_emit())
584 return true;
585 }
586 return false;
587 }
588
589 // Emit relocs.
590
591 template<int size, bool big_endian>
592 template<int sh_type>
593 void
594 Copy_relocs<size, big_endian>::emit(
595 Output_data_reloc<sh_type, true, size, big_endian>* reloc_data)
596 {
597 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
598 p != this->entries_.end();
599 ++p)
600 {
601 if (p->should_emit())
602 p->emit(reloc_data);
603 }
604 }
605
606 // Instantiate the templates we need. We could use the configure
607 // script to restrict this to only the ones for implemented targets.
608
609 template
610 void
611 Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
612
613 template
614 void
615 Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
616
617 template
618 void
619 Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
620
621 template
622 void
623 Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
624
625 template
626 void
627 Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
628 Symbol_table* symtab,
629 Layout* layout,
630 Read_relocs_data* rd);
631
632 template
633 void
634 Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
635 Symbol_table* symtab,
636 Layout* layout,
637 Read_relocs_data* rd);
638
639 template
640 void
641 Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
642 Symbol_table* symtab,
643 Layout* layout,
644 Read_relocs_data* rd);
645
646 template
647 void
648 Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
649 Symbol_table* symtab,
650 Layout* layout,
651 Read_relocs_data* rd);
652
653 template
654 void
655 Sized_relobj<32, false>::do_relocate(const General_options& options,
656 const Symbol_table* symtab,
657 const Layout* layout,
658 Output_file* of);
659
660 template
661 void
662 Sized_relobj<32, true>::do_relocate(const General_options& options,
663 const Symbol_table* symtab,
664 const Layout* layout,
665 Output_file* of);
666
667 template
668 void
669 Sized_relobj<64, false>::do_relocate(const General_options& options,
670 const Symbol_table* symtab,
671 const Layout* layout,
672 Output_file* of);
673
674 template
675 void
676 Sized_relobj<64, true>::do_relocate(const General_options& options,
677 const Symbol_table* symtab,
678 const Layout* layout,
679 Output_file* of);
680
681 template
682 class Copy_relocs<32, false>;
683
684 template
685 class Copy_relocs<32, true>;
686
687 template
688 class Copy_relocs<64, false>;
689
690 template
691 class Copy_relocs<64, true>;
692
693 template
694 void
695 Copy_relocs<32, false>::emit<elfcpp::SHT_REL>(
696 Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*);
697
698 template
699 void
700 Copy_relocs<32, true>::emit<elfcpp::SHT_REL>(
701 Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*);
702
703 template
704 void
705 Copy_relocs<64, false>::emit<elfcpp::SHT_REL>(
706 Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*);
707
708 template
709 void
710 Copy_relocs<64, true>::emit<elfcpp::SHT_REL>(
711 Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*);
712
713 template
714 void
715 Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>(
716 Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*);
717
718 template
719 void
720 Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>(
721 Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*);
722
723 template
724 void
725 Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>(
726 Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*);
727
728 template
729 void
730 Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>(
731 Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*);
732
733 } // End namespace gold.
This page took 0.059231 seconds and 5 git commands to generate.