Correct handling of non-section symbol in merged section. Avoid some
[deliverable/binutils-gdb.git] / gold / reloc.cc
1 // reloc.cc -- relocate input files for gold.
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include "gold.h"
24
25 #include "workqueue.h"
26 #include "symtab.h"
27 #include "output.h"
28 #include "merge.h"
29 #include "object.h"
30 #include "reloc.h"
31
32 namespace gold
33 {
34
35 // Read_relocs methods.
36
37 // These tasks just read the relocation information from the file.
38 // After reading it, the start another task to process the
39 // information. These tasks requires access to the file.
40
41 Task_token*
42 Read_relocs::is_runnable()
43 {
44 return this->object_->is_locked() ? this->object_->token() : NULL;
45 }
46
47 // Lock the file.
48
49 void
50 Read_relocs::locks(Task_locker* tl)
51 {
52 tl->add(this, this->object_->token());
53 }
54
55 // Read the relocations and then start a Scan_relocs_task.
56
57 void
58 Read_relocs::run(Workqueue* workqueue)
59 {
60 Read_relocs_data *rd = new Read_relocs_data;
61 this->object_->read_relocs(rd);
62 this->object_->release();
63
64 workqueue->queue_front(new Scan_relocs(this->options_, this->symtab_,
65 this->layout_, this->object_, rd,
66 this->symtab_lock_, this->blocker_));
67 }
68
69 // Return a debugging name for the task.
70
71 std::string
72 Read_relocs::get_name() const
73 {
74 return "Read_relocs " + this->object_->name();
75 }
76
77 // Scan_relocs methods.
78
79 // These tasks scan the relocations read by Read_relocs and mark up
80 // the symbol table to indicate which relocations are required. We
81 // use a lock on the symbol table to keep them from interfering with
82 // each other.
83
84 Task_token*
85 Scan_relocs::is_runnable()
86 {
87 if (!this->symtab_lock_->is_writable())
88 return this->symtab_lock_;
89 if (this->object_->is_locked())
90 return this->object_->token();
91 return NULL;
92 }
93
94 // Return the locks we hold: one on the file, one on the symbol table
95 // and one blocker.
96
97 void
98 Scan_relocs::locks(Task_locker* tl)
99 {
100 tl->add(this, this->object_->token());
101 tl->add(this, this->symtab_lock_);
102 tl->add(this, this->blocker_);
103 }
104
105 // Scan the relocs.
106
107 void
108 Scan_relocs::run(Workqueue*)
109 {
110 this->object_->scan_relocs(this->options_, this->symtab_, this->layout_,
111 this->rd_);
112 this->object_->release();
113 delete this->rd_;
114 this->rd_ = NULL;
115 }
116
117 // Return a debugging name for the task.
118
119 std::string
120 Scan_relocs::get_name() const
121 {
122 return "Scan_relocs " + this->object_->name();
123 }
124
125 // Relocate_task methods.
126
127 // We may have to wait for the output sections to be written.
128
129 Task_token*
130 Relocate_task::is_runnable()
131 {
132 if (this->object_->relocs_must_follow_section_writes()
133 && this->output_sections_blocker_->is_blocked())
134 return this->output_sections_blocker_;
135
136 if (this->object_->is_locked())
137 return this->object_->token();
138
139 return NULL;
140 }
141
142 // We want to lock the file while we run. We want to unblock
143 // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
144 // INPUT_SECTIONS_BLOCKER may be NULL.
145
146 void
147 Relocate_task::locks(Task_locker* tl)
148 {
149 if (this->input_sections_blocker_ != NULL)
150 tl->add(this, this->input_sections_blocker_);
151 tl->add(this, this->final_blocker_);
152 tl->add(this, this->object_->token());
153 }
154
155 // Run the task.
156
157 void
158 Relocate_task::run(Workqueue*)
159 {
160 this->object_->relocate(this->options_, this->symtab_, this->layout_,
161 this->of_);
162 this->object_->release();
163 }
164
165 // Return a debugging name for the task.
166
167 std::string
168 Relocate_task::get_name() const
169 {
170 return "Relocate_task " + this->object_->name();
171 }
172
173 // Read the relocs and local symbols from the object file and store
174 // the information in RD.
175
176 template<int size, bool big_endian>
177 void
178 Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
179 {
180 rd->relocs.clear();
181
182 unsigned int shnum = this->shnum();
183 if (shnum == 0)
184 return;
185
186 rd->relocs.reserve(shnum / 2);
187
188 std::vector<Map_to_output>& map_sections(this->map_to_output());
189
190 const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(),
191 shnum * This::shdr_size,
192 true);
193 // Skip the first, dummy, section.
194 const unsigned char *ps = pshdrs + This::shdr_size;
195 for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size)
196 {
197 typename This::Shdr shdr(ps);
198
199 unsigned int sh_type = shdr.get_sh_type();
200 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
201 continue;
202
203 unsigned int shndx = shdr.get_sh_info();
204 if (shndx >= shnum)
205 {
206 this->error(_("relocation section %u has bad info %u"),
207 i, shndx);
208 continue;
209 }
210
211 Output_section* os = map_sections[shndx].output_section;
212 if (os == NULL)
213 continue;
214
215 // We are scanning relocations in order to fill out the GOT and
216 // PLT sections. Relocations for sections which are not
217 // allocated (typically debugging sections) should not add new
218 // GOT and PLT entries. So we skip them.
219 typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
220 if ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
221 continue;
222
223 if (shdr.get_sh_link() != this->symtab_shndx_)
224 {
225 this->error(_("relocation section %u uses unexpected "
226 "symbol table %u"),
227 i, shdr.get_sh_link());
228 continue;
229 }
230
231 off_t sh_size = shdr.get_sh_size();
232
233 unsigned int reloc_size;
234 if (sh_type == elfcpp::SHT_REL)
235 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
236 else
237 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
238 if (reloc_size != shdr.get_sh_entsize())
239 {
240 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
241 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
242 reloc_size);
243 continue;
244 }
245
246 size_t reloc_count = sh_size / reloc_size;
247 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
248 {
249 this->error(_("reloc section %u size %lu uneven"),
250 i, static_cast<unsigned long>(sh_size));
251 continue;
252 }
253
254 rd->relocs.push_back(Section_relocs());
255 Section_relocs& sr(rd->relocs.back());
256 sr.reloc_shndx = i;
257 sr.data_shndx = shndx;
258 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
259 true);
260 sr.sh_type = sh_type;
261 sr.reloc_count = reloc_count;
262 sr.output_section = os;
263 sr.needs_special_offset_handling = map_sections[shndx].offset == -1;
264 }
265
266 // Read the local symbols.
267 gold_assert(this->symtab_shndx_ != -1U);
268 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
269 rd->local_symbols = NULL;
270 else
271 {
272 typename This::Shdr symtabshdr(pshdrs
273 + this->symtab_shndx_ * This::shdr_size);
274 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
275 const int sym_size = This::sym_size;
276 const unsigned int loccount = this->local_symbol_count_;
277 gold_assert(loccount == symtabshdr.get_sh_info());
278 off_t locsize = loccount * sym_size;
279 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
280 locsize, true);
281 }
282 }
283
284 // Scan the relocs and adjust the symbol table. This looks for
285 // relocations which require GOT/PLT/COPY relocations.
286
287 template<int size, bool big_endian>
288 void
289 Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
290 Symbol_table* symtab,
291 Layout* layout,
292 Read_relocs_data* rd)
293 {
294 Sized_target<size, big_endian>* target = this->sized_target();
295
296 const unsigned char* local_symbols;
297 if (rd->local_symbols == NULL)
298 local_symbols = NULL;
299 else
300 local_symbols = rd->local_symbols->data();
301
302 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
303 p != rd->relocs.end();
304 ++p)
305 {
306 target->scan_relocs(options, symtab, layout, this, p->data_shndx,
307 p->sh_type, p->contents->data(), p->reloc_count,
308 p->output_section, p->needs_special_offset_handling,
309 this->local_symbol_count_,
310 local_symbols);
311 delete p->contents;
312 p->contents = NULL;
313 }
314
315 if (rd->local_symbols != NULL)
316 {
317 delete rd->local_symbols;
318 rd->local_symbols = NULL;
319 }
320 }
321
322 // Relocate the input sections and write out the local symbols.
323
324 template<int size, bool big_endian>
325 void
326 Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
327 const Symbol_table* symtab,
328 const Layout* layout,
329 Output_file* of)
330 {
331 unsigned int shnum = this->shnum();
332
333 // Read the section headers.
334 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
335 shnum * This::shdr_size,
336 true);
337
338 Views views;
339 views.resize(shnum);
340
341 // Make two passes over the sections. The first one copies the
342 // section data to the output file. The second one applies
343 // relocations.
344
345 this->write_sections(pshdrs, of, &views);
346
347 // To speed up relocations, we set up hash tables for fast lookup of
348 // input offsets to output addresses.
349 this->initialize_input_to_output_maps();
350
351 // Apply relocations.
352
353 this->relocate_sections(options, symtab, layout, pshdrs, &views);
354
355 // After we've done the relocations, we release the hash tables,
356 // since we no longer need them.
357 this->free_input_to_output_maps();
358
359 // Write out the accumulated views.
360 for (unsigned int i = 1; i < shnum; ++i)
361 {
362 if (views[i].view != NULL)
363 {
364 if (!views[i].is_postprocessing_view)
365 {
366 if (views[i].is_input_output_view)
367 of->write_input_output_view(views[i].offset,
368 views[i].view_size,
369 views[i].view);
370 else
371 of->write_output_view(views[i].offset, views[i].view_size,
372 views[i].view);
373 }
374 }
375 }
376
377 // Write out the local symbols.
378 this->write_local_symbols(of, layout->sympool(), layout->dynpool());
379 }
380
381 // Write section data to the output file. PSHDRS points to the
382 // section headers. Record the views in *PVIEWS for use when
383 // relocating.
384
385 template<int size, bool big_endian>
386 void
387 Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
388 Output_file* of,
389 Views* pviews) const
390 {
391 unsigned int shnum = this->shnum();
392 const std::vector<Map_to_output>& map_sections(this->map_to_output());
393
394 const unsigned char* p = pshdrs + This::shdr_size;
395 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
396 {
397 View_size* pvs = &(*pviews)[i];
398
399 pvs->view = NULL;
400
401 const Output_section* os = map_sections[i].output_section;
402 if (os == NULL)
403 continue;
404 off_t output_offset = map_sections[i].offset;
405
406 typename This::Shdr shdr(p);
407
408 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
409 continue;
410
411 // In the normal case, this input section is simply mapped to
412 // the output section at offset OUTPUT_OFFSET.
413
414 // However, if OUTPUT_OFFSET == -1, then input data is handled
415 // specially--e.g., a .eh_frame section. The relocation
416 // routines need to check for each reloc where it should be
417 // applied. For this case, we need an input/output view for the
418 // entire contents of the section in the output file. We don't
419 // want to copy the contents of the input section to the output
420 // section; the output section contents were already written,
421 // and we waited for them in Relocate_task::is_runnable because
422 // relocs_must_follow_section_writes is set for the object.
423
424 // Regardless of which of the above cases is true, we have to
425 // check requires_postprocessing of the output section. If that
426 // is false, then we work with views of the output file
427 // directly. If it is true, then we work with a separate
428 // buffer, and the output section is responsible for writing the
429 // final data to the output file.
430
431 off_t output_section_offset;
432 off_t output_section_size;
433 if (!os->requires_postprocessing())
434 {
435 output_section_offset = os->offset();
436 output_section_size = os->data_size();
437 }
438 else
439 {
440 output_section_offset = 0;
441 output_section_size = os->postprocessing_buffer_size();
442 }
443
444 off_t view_start;
445 section_size_type view_size;
446 if (output_offset != -1)
447 {
448 view_start = output_section_offset + output_offset;
449 view_size = convert_to_section_size_type(shdr.get_sh_size());
450 }
451 else
452 {
453 view_start = output_section_offset;
454 view_size = convert_to_section_size_type(output_section_size);
455 }
456
457 if (view_size == 0)
458 continue;
459
460 gold_assert(output_offset == -1
461 || (output_offset >= 0
462 && (output_offset + static_cast<off_t>(view_size)
463 <= output_section_size)));
464
465 unsigned char* view;
466 if (os->requires_postprocessing())
467 {
468 unsigned char* buffer = os->postprocessing_buffer();
469 view = buffer + view_start;
470 if (output_offset != -1)
471 this->read(shdr.get_sh_offset(), view_size, view);
472 }
473 else
474 {
475 if (output_offset == -1)
476 view = of->get_input_output_view(view_start, view_size);
477 else
478 {
479 view = of->get_output_view(view_start, view_size);
480 this->read(shdr.get_sh_offset(), view_size, view);
481 }
482 }
483
484 pvs->view = view;
485 pvs->address = os->address();
486 if (output_offset != -1)
487 pvs->address += output_offset;
488 pvs->offset = view_start;
489 pvs->view_size = view_size;
490 pvs->is_input_output_view = output_offset == -1;
491 pvs->is_postprocessing_view = os->requires_postprocessing();
492 }
493 }
494
495 // Relocate section data. VIEWS points to the section data as views
496 // in the output file.
497
498 template<int size, bool big_endian>
499 void
500 Sized_relobj<size, big_endian>::relocate_sections(
501 const General_options& options,
502 const Symbol_table* symtab,
503 const Layout* layout,
504 const unsigned char* pshdrs,
505 Views* pviews)
506 {
507 unsigned int shnum = this->shnum();
508 Sized_target<size, big_endian>* target = this->sized_target();
509
510 const std::vector<Map_to_output>& map_sections(this->map_to_output());
511
512 Relocate_info<size, big_endian> relinfo;
513 relinfo.options = &options;
514 relinfo.symtab = symtab;
515 relinfo.layout = layout;
516 relinfo.object = this;
517
518 const unsigned char* p = pshdrs + This::shdr_size;
519 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
520 {
521 typename This::Shdr shdr(p);
522
523 unsigned int sh_type = shdr.get_sh_type();
524 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
525 continue;
526
527 unsigned int index = shdr.get_sh_info();
528 if (index >= this->shnum())
529 {
530 this->error(_("relocation section %u has bad info %u"),
531 i, index);
532 continue;
533 }
534
535 Output_section* os = map_sections[index].output_section;
536 if (os == NULL)
537 {
538 // This relocation section is against a section which we
539 // discarded.
540 continue;
541 }
542 off_t output_offset = map_sections[index].offset;
543
544 gold_assert((*pviews)[index].view != NULL);
545
546 if (shdr.get_sh_link() != this->symtab_shndx_)
547 {
548 gold_error(_("relocation section %u uses unexpected "
549 "symbol table %u"),
550 i, shdr.get_sh_link());
551 continue;
552 }
553
554 off_t sh_size = shdr.get_sh_size();
555 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
556 sh_size, false);
557
558 unsigned int reloc_size;
559 if (sh_type == elfcpp::SHT_REL)
560 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
561 else
562 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
563
564 if (reloc_size != shdr.get_sh_entsize())
565 {
566 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
567 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
568 reloc_size);
569 continue;
570 }
571
572 size_t reloc_count = sh_size / reloc_size;
573 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
574 {
575 gold_error(_("reloc section %u size %lu uneven"),
576 i, static_cast<unsigned long>(sh_size));
577 continue;
578 }
579
580 gold_assert(output_offset != -1
581 || this->relocs_must_follow_section_writes());
582
583 relinfo.reloc_shndx = i;
584 relinfo.data_shndx = index;
585 target->relocate_section(&relinfo,
586 sh_type,
587 prelocs,
588 reloc_count,
589 os,
590 output_offset == -1,
591 (*pviews)[index].view,
592 (*pviews)[index].address,
593 (*pviews)[index].view_size);
594 }
595 }
596
597 // Create merge hash tables for the local symbols. These are used to
598 // speed up relocations.
599
600 template<int size, bool big_endian>
601 void
602 Sized_relobj<size, big_endian>::initialize_input_to_output_maps()
603 {
604 const unsigned int loccount = this->local_symbol_count_;
605 for (unsigned int i = 1; i < loccount; ++i)
606 {
607 Symbol_value<size>& lv(this->local_values_[i]);
608 lv.initialize_input_to_output_map(this);
609 }
610 }
611
612 // Free merge hash tables for the local symbols.
613
614 template<int size, bool big_endian>
615 void
616 Sized_relobj<size, big_endian>::free_input_to_output_maps()
617 {
618 const unsigned int loccount = this->local_symbol_count_;
619 for (unsigned int i = 1; i < loccount; ++i)
620 {
621 Symbol_value<size>& lv(this->local_values_[i]);
622 lv.free_input_to_output_map();
623 }
624 }
625
626 // Class Merged_symbol_value.
627
628 template<int size>
629 void
630 Merged_symbol_value<size>::initialize_input_to_output_map(
631 const Relobj* object,
632 unsigned int input_shndx)
633 {
634 Object_merge_map* map = object->merge_map();
635 map->initialize_input_to_output_map<size>(input_shndx,
636 this->output_start_address_,
637 &this->output_addresses_);
638 }
639
640 // Get the output value corresponding to an input offset if we
641 // couldn't find it in the hash table.
642
643 template<int size>
644 typename elfcpp::Elf_types<size>::Elf_Addr
645 Merged_symbol_value<size>::value_from_output_section(
646 const Relobj* object,
647 unsigned int input_shndx,
648 typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const
649 {
650 section_offset_type output_offset;
651 bool found = object->merge_map()->get_output_offset(NULL, input_shndx,
652 input_offset,
653 &output_offset);
654
655 // If this assertion fails, it means that some relocation was
656 // against a portion of an input merge section which we didn't map
657 // to the output file and we didn't explicitly discard. We should
658 // always map all portions of input merge sections.
659 gold_assert(found);
660
661 if (output_offset == -1)
662 return 0;
663 else
664 return this->output_start_address_ + output_offset;
665 }
666
667 // Copy_relocs::Copy_reloc_entry methods.
668
669 // Return whether we should emit this reloc. We should emit it if the
670 // symbol is still defined in a dynamic object. If we should not emit
671 // it, we clear it, to save ourselves the test next time.
672
673 template<int size, bool big_endian>
674 bool
675 Copy_relocs<size, big_endian>::Copy_reloc_entry::should_emit()
676 {
677 if (this->sym_ == NULL)
678 return false;
679 if (this->sym_->is_from_dynobj())
680 return true;
681 this->sym_ = NULL;
682 return false;
683 }
684
685 // Emit a reloc into a SHT_REL section.
686
687 template<int size, bool big_endian>
688 void
689 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
690 Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* reloc_data)
691 {
692 this->sym_->set_needs_dynsym_entry();
693 reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
694 this->relobj_, this->shndx_, this->address_);
695 }
696
697 // Emit a reloc into a SHT_RELA section.
698
699 template<int size, bool big_endian>
700 void
701 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
702 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* reloc_data)
703 {
704 this->sym_->set_needs_dynsym_entry();
705 reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
706 this->relobj_, this->shndx_, this->address_,
707 this->addend_);
708 }
709
710 // Copy_relocs methods.
711
712 // Return whether we need a COPY reloc for a relocation against GSYM.
713 // The relocation is being applied to section SHNDX in OBJECT.
714
715 template<int size, bool big_endian>
716 bool
717 Copy_relocs<size, big_endian>::need_copy_reloc(
718 const General_options*,
719 Relobj* object,
720 unsigned int shndx,
721 Sized_symbol<size>* sym)
722 {
723 // FIXME: Handle -z nocopyrelocs.
724
725 if (sym->symsize() == 0)
726 return false;
727
728 // If this is a readonly section, then we need a COPY reloc.
729 // Otherwise we can use a dynamic reloc.
730 if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
731 return true;
732
733 return false;
734 }
735
736 // Save a Rel reloc.
737
738 template<int size, bool big_endian>
739 void
740 Copy_relocs<size, big_endian>::save(
741 Symbol* sym,
742 Relobj* relobj,
743 unsigned int shndx,
744 Output_section* output_section,
745 const elfcpp::Rel<size, big_endian>& rel)
746 {
747 unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info());
748 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
749 output_section,
750 rel.get_r_offset(), 0));
751 }
752
753 // Save a Rela reloc.
754
755 template<int size, bool big_endian>
756 void
757 Copy_relocs<size, big_endian>::save(
758 Symbol* sym,
759 Relobj* relobj,
760 unsigned int shndx,
761 Output_section* output_section,
762 const elfcpp::Rela<size, big_endian>& rela)
763 {
764 unsigned int reloc_type = elfcpp::elf_r_type<size>(rela.get_r_info());
765 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
766 output_section,
767 rela.get_r_offset(),
768 rela.get_r_addend()));
769 }
770
771 // Return whether there are any relocs to emit. We don't want to emit
772 // a reloc if the symbol is no longer defined in a dynamic object.
773
774 template<int size, bool big_endian>
775 bool
776 Copy_relocs<size, big_endian>::any_to_emit()
777 {
778 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
779 p != this->entries_.end();
780 ++p)
781 {
782 if (p->should_emit())
783 return true;
784 }
785 return false;
786 }
787
788 // Emit relocs.
789
790 template<int size, bool big_endian>
791 template<int sh_type>
792 void
793 Copy_relocs<size, big_endian>::emit(
794 Output_data_reloc<sh_type, true, size, big_endian>* reloc_data)
795 {
796 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
797 p != this->entries_.end();
798 ++p)
799 {
800 if (p->should_emit())
801 p->emit(reloc_data);
802 }
803 }
804
805 // Track_relocs methods.
806
807 // Initialize the class to track the relocs. This gets the object,
808 // the reloc section index, and the type of the relocs. This returns
809 // false if something goes wrong.
810
811 template<int size, bool big_endian>
812 bool
813 Track_relocs<size, big_endian>::initialize(
814 Object* object,
815 unsigned int reloc_shndx,
816 unsigned int reloc_type)
817 {
818 // If RELOC_SHNDX is -1U, it means there is more than one reloc
819 // section for the .eh_frame section. We can't handle that case.
820 if (reloc_shndx == -1U)
821 return false;
822
823 // If RELOC_SHNDX is 0, there is no reloc section.
824 if (reloc_shndx == 0)
825 return true;
826
827 // Get the contents of the reloc section.
828 this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
829
830 if (reloc_type == elfcpp::SHT_REL)
831 this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
832 else if (reloc_type == elfcpp::SHT_RELA)
833 this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
834 else
835 gold_unreachable();
836
837 if (this->len_ % this->reloc_size_ != 0)
838 {
839 object->error(_("reloc section size %zu is not a multiple of "
840 "reloc size %d\n"),
841 static_cast<size_t>(this->len_),
842 this->reloc_size_);
843 return false;
844 }
845
846 return true;
847 }
848
849 // Return the offset of the next reloc, or -1 if there isn't one.
850
851 template<int size, bool big_endian>
852 off_t
853 Track_relocs<size, big_endian>::next_offset() const
854 {
855 if (this->pos_ >= this->len_)
856 return -1;
857
858 // Rel and Rela start out the same, so we can always use Rel to find
859 // the r_offset value.
860 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
861 return rel.get_r_offset();
862 }
863
864 // Return the index of the symbol referenced by the next reloc, or -1U
865 // if there aren't any more relocs.
866
867 template<int size, bool big_endian>
868 unsigned int
869 Track_relocs<size, big_endian>::next_symndx() const
870 {
871 if (this->pos_ >= this->len_)
872 return -1U;
873
874 // Rel and Rela start out the same, so we can use Rel to find the
875 // symbol index.
876 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
877 return elfcpp::elf_r_sym<size>(rel.get_r_info());
878 }
879
880 // Advance to the next reloc whose r_offset is greater than or equal
881 // to OFFSET. Return the number of relocs we skip.
882
883 template<int size, bool big_endian>
884 int
885 Track_relocs<size, big_endian>::advance(off_t offset)
886 {
887 int ret = 0;
888 while (this->pos_ < this->len_)
889 {
890 // Rel and Rela start out the same, so we can always use Rel to
891 // find the r_offset value.
892 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
893 if (static_cast<off_t>(rel.get_r_offset()) >= offset)
894 break;
895 ++ret;
896 this->pos_ += this->reloc_size_;
897 }
898 return ret;
899 }
900
901 // Instantiate the templates we need. We could use the configure
902 // script to restrict this to only the ones for implemented targets.
903
904 #ifdef HAVE_TARGET_32_LITTLE
905 template
906 void
907 Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
908 #endif
909
910 #ifdef HAVE_TARGET_32_BIG
911 template
912 void
913 Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
914 #endif
915
916 #ifdef HAVE_TARGET_64_LITTLE
917 template
918 void
919 Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
920 #endif
921
922 #ifdef HAVE_TARGET_64_BIG
923 template
924 void
925 Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
926 #endif
927
928 #ifdef HAVE_TARGET_32_LITTLE
929 template
930 void
931 Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
932 Symbol_table* symtab,
933 Layout* layout,
934 Read_relocs_data* rd);
935 #endif
936
937 #ifdef HAVE_TARGET_32_BIG
938 template
939 void
940 Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
941 Symbol_table* symtab,
942 Layout* layout,
943 Read_relocs_data* rd);
944 #endif
945
946 #ifdef HAVE_TARGET_64_LITTLE
947 template
948 void
949 Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
950 Symbol_table* symtab,
951 Layout* layout,
952 Read_relocs_data* rd);
953 #endif
954
955 #ifdef HAVE_TARGET_64_BIG
956 template
957 void
958 Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
959 Symbol_table* symtab,
960 Layout* layout,
961 Read_relocs_data* rd);
962 #endif
963
964 #ifdef HAVE_TARGET_32_LITTLE
965 template
966 void
967 Sized_relobj<32, false>::do_relocate(const General_options& options,
968 const Symbol_table* symtab,
969 const Layout* layout,
970 Output_file* of);
971 #endif
972
973 #ifdef HAVE_TARGET_32_BIG
974 template
975 void
976 Sized_relobj<32, true>::do_relocate(const General_options& options,
977 const Symbol_table* symtab,
978 const Layout* layout,
979 Output_file* of);
980 #endif
981
982 #ifdef HAVE_TARGET_64_LITTLE
983 template
984 void
985 Sized_relobj<64, false>::do_relocate(const General_options& options,
986 const Symbol_table* symtab,
987 const Layout* layout,
988 Output_file* of);
989 #endif
990
991 #ifdef HAVE_TARGET_64_BIG
992 template
993 void
994 Sized_relobj<64, true>::do_relocate(const General_options& options,
995 const Symbol_table* symtab,
996 const Layout* layout,
997 Output_file* of);
998 #endif
999
1000 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1001 template
1002 class Merged_symbol_value<32>;
1003 #endif
1004
1005 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1006 template
1007 class Merged_symbol_value<64>;
1008 #endif
1009
1010 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1011 template
1012 class Symbol_value<32>;
1013 #endif
1014
1015 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1016 template
1017 class Symbol_value<64>;
1018 #endif
1019
1020 #ifdef HAVE_TARGET_32_LITTLE
1021 template
1022 class Copy_relocs<32, false>;
1023 #endif
1024
1025 #ifdef HAVE_TARGET_32_BIG
1026 template
1027 class Copy_relocs<32, true>;
1028 #endif
1029
1030 #ifdef HAVE_TARGET_64_LITTLE
1031 template
1032 class Copy_relocs<64, false>;
1033 #endif
1034
1035 #ifdef HAVE_TARGET_64_BIG
1036 template
1037 class Copy_relocs<64, true>;
1038 #endif
1039
1040 #ifdef HAVE_TARGET_32_LITTLE
1041 template
1042 void
1043 Copy_relocs<32, false>::emit<elfcpp::SHT_REL>(
1044 Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*);
1045 #endif
1046
1047 #ifdef HAVE_TARGET_32_BIG
1048 template
1049 void
1050 Copy_relocs<32, true>::emit<elfcpp::SHT_REL>(
1051 Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*);
1052 #endif
1053
1054 #ifdef HAVE_TARGET_64_LITTLE
1055 template
1056 void
1057 Copy_relocs<64, false>::emit<elfcpp::SHT_REL>(
1058 Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*);
1059 #endif
1060
1061 #ifdef HAVE_TARGET_64_BIG
1062 template
1063 void
1064 Copy_relocs<64, true>::emit<elfcpp::SHT_REL>(
1065 Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*);
1066 #endif
1067
1068 #ifdef HAVE_TARGET_32_LITTLE
1069 template
1070 void
1071 Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>(
1072 Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*);
1073 #endif
1074
1075 #ifdef HAVE_TARGET_32_BIG
1076 template
1077 void
1078 Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>(
1079 Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*);
1080 #endif
1081
1082 #ifdef HAVE_TARGET_64_LITTLE
1083 template
1084 void
1085 Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>(
1086 Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*);
1087 #endif
1088
1089 #ifdef HAVE_TARGET_64_BIG
1090 template
1091 void
1092 Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>(
1093 Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*);
1094 #endif
1095
1096 #ifdef HAVE_TARGET_32_LITTLE
1097 template
1098 class Track_relocs<32, false>;
1099 #endif
1100
1101 #ifdef HAVE_TARGET_32_BIG
1102 template
1103 class Track_relocs<32, true>;
1104 #endif
1105
1106 #ifdef HAVE_TARGET_64_LITTLE
1107 template
1108 class Track_relocs<64, false>;
1109 #endif
1110
1111 #ifdef HAVE_TARGET_64_BIG
1112 template
1113 class Track_relocs<64, true>;
1114 #endif
1115
1116 } // End namespace gold.
This page took 0.055531 seconds and 5 git commands to generate.