* object.h (class Object): Remove target_ field, and target,
[deliverable/binutils-gdb.git] / gold / reloc.cc
1 // reloc.cc -- relocate input files for gold.
2
3 // Copyright 2006, 2007, 2008, 2009 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 <algorithm>
26
27 #include "workqueue.h"
28 #include "symtab.h"
29 #include "output.h"
30 #include "merge.h"
31 #include "object.h"
32 #include "target-reloc.h"
33 #include "reloc.h"
34
35 namespace gold
36 {
37
38 // Read_relocs methods.
39
40 // These tasks just read the relocation information from the file.
41 // After reading it, the start another task to process the
42 // information. These tasks requires access to the file.
43
44 Task_token*
45 Read_relocs::is_runnable()
46 {
47 return this->object_->is_locked() ? this->object_->token() : NULL;
48 }
49
50 // Lock the file.
51
52 void
53 Read_relocs::locks(Task_locker* tl)
54 {
55 tl->add(this, this->object_->token());
56 }
57
58 // Read the relocations and then start a Scan_relocs_task.
59
60 void
61 Read_relocs::run(Workqueue* workqueue)
62 {
63 Read_relocs_data *rd = new Read_relocs_data;
64 this->object_->read_relocs(rd);
65 this->object_->set_relocs_data(rd);
66 this->object_->release();
67
68 // If garbage collection or identical comdat folding is desired, we
69 // process the relocs first before scanning them. Scanning of relocs is
70 // done only after garbage or identical sections is identified.
71 if (parameters->options().gc_sections() || parameters->options().icf())
72 {
73 workqueue->queue_next(new Gc_process_relocs(this->options_,
74 this->symtab_,
75 this->layout_,
76 this->object_, rd,
77 this->symtab_lock_,
78 this->blocker_));
79 }
80 else
81 {
82 workqueue->queue_next(new Scan_relocs(this->options_, this->symtab_,
83 this->layout_, this->object_, rd,
84 this->symtab_lock_,
85 this->blocker_));
86 }
87 }
88
89 // Return a debugging name for the task.
90
91 std::string
92 Read_relocs::get_name() const
93 {
94 return "Read_relocs " + this->object_->name();
95 }
96
97 // Gc_process_relocs methods.
98
99 // These tasks process the relocations read by Read_relocs and
100 // determine which sections are referenced and which are garbage.
101 // This task is done only when --gc-sections is used.
102
103 Task_token*
104 Gc_process_relocs::is_runnable()
105 {
106 if (this->object_->is_locked())
107 return this->object_->token();
108 return NULL;
109 }
110
111 void
112 Gc_process_relocs::locks(Task_locker* tl)
113 {
114 tl->add(this, this->object_->token());
115 tl->add(this, this->blocker_);
116 }
117
118 void
119 Gc_process_relocs::run(Workqueue*)
120 {
121 this->object_->gc_process_relocs(this->options_, this->symtab_, this->layout_,
122 this->rd_);
123 this->object_->release();
124 }
125
126 // Return a debugging name for the task.
127
128 std::string
129 Gc_process_relocs::get_name() const
130 {
131 return "Gc_process_relocs " + this->object_->name();
132 }
133
134 // Scan_relocs methods.
135
136 // These tasks scan the relocations read by Read_relocs and mark up
137 // the symbol table to indicate which relocations are required. We
138 // use a lock on the symbol table to keep them from interfering with
139 // each other.
140
141 Task_token*
142 Scan_relocs::is_runnable()
143 {
144 if (!this->symtab_lock_->is_writable())
145 return this->symtab_lock_;
146 if (this->object_->is_locked())
147 return this->object_->token();
148 return NULL;
149 }
150
151 // Return the locks we hold: one on the file, one on the symbol table
152 // and one blocker.
153
154 void
155 Scan_relocs::locks(Task_locker* tl)
156 {
157 tl->add(this, this->object_->token());
158 tl->add(this, this->symtab_lock_);
159 tl->add(this, this->blocker_);
160 }
161
162 // Scan the relocs.
163
164 void
165 Scan_relocs::run(Workqueue*)
166 {
167 this->object_->scan_relocs(this->options_, this->symtab_, this->layout_,
168 this->rd_);
169 this->object_->release();
170 delete this->rd_;
171 this->rd_ = NULL;
172 }
173
174 // Return a debugging name for the task.
175
176 std::string
177 Scan_relocs::get_name() const
178 {
179 return "Scan_relocs " + this->object_->name();
180 }
181
182 // Relocate_task methods.
183
184 // We may have to wait for the output sections to be written.
185
186 Task_token*
187 Relocate_task::is_runnable()
188 {
189 if (this->object_->relocs_must_follow_section_writes()
190 && this->output_sections_blocker_->is_blocked())
191 return this->output_sections_blocker_;
192
193 if (this->object_->is_locked())
194 return this->object_->token();
195
196 return NULL;
197 }
198
199 // We want to lock the file while we run. We want to unblock
200 // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
201 // INPUT_SECTIONS_BLOCKER may be NULL.
202
203 void
204 Relocate_task::locks(Task_locker* tl)
205 {
206 if (this->input_sections_blocker_ != NULL)
207 tl->add(this, this->input_sections_blocker_);
208 tl->add(this, this->final_blocker_);
209 tl->add(this, this->object_->token());
210 }
211
212 // Run the task.
213
214 void
215 Relocate_task::run(Workqueue*)
216 {
217 this->object_->relocate(this->options_, this->symtab_, this->layout_,
218 this->of_);
219
220 // This is normally the last thing we will do with an object, so
221 // uncache all views.
222 this->object_->clear_view_cache_marks();
223
224 this->object_->release();
225 }
226
227 // Return a debugging name for the task.
228
229 std::string
230 Relocate_task::get_name() const
231 {
232 return "Relocate_task " + this->object_->name();
233 }
234
235 // Read the relocs and local symbols from the object file and store
236 // the information in RD.
237
238 template<int size, bool big_endian>
239 void
240 Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
241 {
242 rd->relocs.clear();
243
244 unsigned int shnum = this->shnum();
245 if (shnum == 0)
246 return;
247
248 rd->relocs.reserve(shnum / 2);
249
250 const Output_sections& out_sections(this->output_sections());
251 const std::vector<Address>& out_offsets(this->section_offsets_);
252
253 const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(),
254 shnum * This::shdr_size,
255 true, true);
256 // Skip the first, dummy, section.
257 const unsigned char *ps = pshdrs + This::shdr_size;
258 for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size)
259 {
260 typename This::Shdr shdr(ps);
261
262 unsigned int sh_type = shdr.get_sh_type();
263 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
264 continue;
265
266 unsigned int shndx = this->adjust_shndx(shdr.get_sh_info());
267 if (shndx >= shnum)
268 {
269 this->error(_("relocation section %u has bad info %u"),
270 i, shndx);
271 continue;
272 }
273
274 Output_section* os = out_sections[shndx];
275 if (os == NULL)
276 continue;
277
278 // We are scanning relocations in order to fill out the GOT and
279 // PLT sections. Relocations for sections which are not
280 // allocated (typically debugging sections) should not add new
281 // GOT and PLT entries. So we skip them unless this is a
282 // relocatable link or we need to emit relocations. FIXME: What
283 // should we do if a linker script maps a section with SHF_ALLOC
284 // clear to a section with SHF_ALLOC set?
285 typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
286 bool is_section_allocated = ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC)
287 != 0);
288 if (!is_section_allocated
289 && !parameters->options().relocatable()
290 && !parameters->options().emit_relocs())
291 continue;
292
293 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
294 {
295 this->error(_("relocation section %u uses unexpected "
296 "symbol table %u"),
297 i, this->adjust_shndx(shdr.get_sh_link()));
298 continue;
299 }
300
301 off_t sh_size = shdr.get_sh_size();
302
303 unsigned int reloc_size;
304 if (sh_type == elfcpp::SHT_REL)
305 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
306 else
307 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
308 if (reloc_size != shdr.get_sh_entsize())
309 {
310 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
311 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
312 reloc_size);
313 continue;
314 }
315
316 size_t reloc_count = sh_size / reloc_size;
317 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
318 {
319 this->error(_("reloc section %u size %lu uneven"),
320 i, static_cast<unsigned long>(sh_size));
321 continue;
322 }
323
324 rd->relocs.push_back(Section_relocs());
325 Section_relocs& sr(rd->relocs.back());
326 sr.reloc_shndx = i;
327 sr.data_shndx = shndx;
328 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
329 true, true);
330 sr.sh_type = sh_type;
331 sr.reloc_count = reloc_count;
332 sr.output_section = os;
333 sr.needs_special_offset_handling = out_offsets[shndx] == invalid_address;
334 sr.is_data_section_allocated = is_section_allocated;
335 }
336
337 // Read the local symbols.
338 gold_assert(this->symtab_shndx_ != -1U);
339 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
340 rd->local_symbols = NULL;
341 else
342 {
343 typename This::Shdr symtabshdr(pshdrs
344 + this->symtab_shndx_ * This::shdr_size);
345 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
346 const int sym_size = This::sym_size;
347 const unsigned int loccount = this->local_symbol_count_;
348 gold_assert(loccount == symtabshdr.get_sh_info());
349 off_t locsize = loccount * sym_size;
350 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
351 locsize, true, true);
352 }
353 }
354
355 // Process the relocs to generate mappings from source sections to referenced
356 // sections. This is used during garbage colletion to determine garbage
357 // sections.
358
359 template<int size, bool big_endian>
360 void
361 Sized_relobj<size, big_endian>::do_gc_process_relocs(const General_options& options,
362 Symbol_table* symtab,
363 Layout* layout,
364 Read_relocs_data* rd)
365 {
366 Sized_target<size, big_endian>* target =
367 parameters->sized_target<size, big_endian>();
368
369 const unsigned char* local_symbols;
370 if (rd->local_symbols == NULL)
371 local_symbols = NULL;
372 else
373 local_symbols = rd->local_symbols->data();
374
375 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
376 p != rd->relocs.end();
377 ++p)
378 {
379 if (!parameters->options().relocatable())
380 {
381 // As noted above, when not generating an object file, we
382 // only scan allocated sections. We may see a non-allocated
383 // section here if we are emitting relocs.
384 if (p->is_data_section_allocated)
385 target->gc_process_relocs(options, symtab, layout, this,
386 p->data_shndx, p->sh_type,
387 p->contents->data(), p->reloc_count,
388 p->output_section,
389 p->needs_special_offset_handling,
390 this->local_symbol_count_,
391 local_symbols);
392 }
393 }
394 }
395
396
397 // Scan the relocs and adjust the symbol table. This looks for
398 // relocations which require GOT/PLT/COPY relocations.
399
400 template<int size, bool big_endian>
401 void
402 Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
403 Symbol_table* symtab,
404 Layout* layout,
405 Read_relocs_data* rd)
406 {
407 Sized_target<size, big_endian>* target =
408 parameters->sized_target<size, big_endian>();
409
410 const unsigned char* local_symbols;
411 if (rd->local_symbols == NULL)
412 local_symbols = NULL;
413 else
414 local_symbols = rd->local_symbols->data();
415
416 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
417 p != rd->relocs.end();
418 ++p)
419 {
420 // When garbage collection is on, unreferenced sections are not included
421 // in the link that would have been included normally. This is known only
422 // after Read_relocs hence this check has to be done again.
423 if (parameters->options().gc_sections() || parameters->options().icf())
424 {
425 if (p->output_section == NULL)
426 continue;
427 }
428 if (!parameters->options().relocatable())
429 {
430 // As noted above, when not generating an object file, we
431 // only scan allocated sections. We may see a non-allocated
432 // section here if we are emitting relocs.
433 if (p->is_data_section_allocated)
434 target->scan_relocs(options, symtab, layout, this, p->data_shndx,
435 p->sh_type, p->contents->data(),
436 p->reloc_count, p->output_section,
437 p->needs_special_offset_handling,
438 this->local_symbol_count_,
439 local_symbols);
440 if (parameters->options().emit_relocs())
441 this->emit_relocs_scan(options, symtab, layout, local_symbols, p);
442 }
443 else
444 {
445 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
446 gold_assert(rr != NULL);
447 rr->set_reloc_count(p->reloc_count);
448 target->scan_relocatable_relocs(options, symtab, layout, this,
449 p->data_shndx, p->sh_type,
450 p->contents->data(),
451 p->reloc_count,
452 p->output_section,
453 p->needs_special_offset_handling,
454 this->local_symbol_count_,
455 local_symbols,
456 rr);
457 }
458
459 delete p->contents;
460 p->contents = NULL;
461 }
462
463 if (rd->local_symbols != NULL)
464 {
465 delete rd->local_symbols;
466 rd->local_symbols = NULL;
467 }
468 }
469
470 // This is a strategy class we use when scanning for --emit-relocs.
471
472 template<int sh_type>
473 class Emit_relocs_strategy
474 {
475 public:
476 // A local non-section symbol.
477 inline Relocatable_relocs::Reloc_strategy
478 local_non_section_strategy(unsigned int, Relobj*, unsigned int)
479 { return Relocatable_relocs::RELOC_COPY; }
480
481 // A local section symbol.
482 inline Relocatable_relocs::Reloc_strategy
483 local_section_strategy(unsigned int, Relobj*)
484 {
485 if (sh_type == elfcpp::SHT_RELA)
486 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
487 else
488 {
489 // The addend is stored in the section contents. Since this
490 // is not a relocatable link, we are going to apply the
491 // relocation contents to the section as usual. This means
492 // that we have no way to record the original addend. If the
493 // original addend is not zero, there is basically no way for
494 // the user to handle this correctly. Caveat emptor.
495 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
496 }
497 }
498
499 // A global symbol.
500 inline Relocatable_relocs::Reloc_strategy
501 global_strategy(unsigned int, Relobj*, unsigned int)
502 { return Relocatable_relocs::RELOC_COPY; }
503 };
504
505 // Scan the input relocations for --emit-relocs.
506
507 template<int size, bool big_endian>
508 void
509 Sized_relobj<size, big_endian>::emit_relocs_scan(
510 const General_options& options,
511 Symbol_table* symtab,
512 Layout* layout,
513 const unsigned char* plocal_syms,
514 const Read_relocs_data::Relocs_list::iterator& p)
515 {
516 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
517 gold_assert(rr != NULL);
518 rr->set_reloc_count(p->reloc_count);
519
520 if (p->sh_type == elfcpp::SHT_REL)
521 this->emit_relocs_scan_reltype<elfcpp::SHT_REL>(options, symtab, layout,
522 plocal_syms, p, rr);
523 else
524 {
525 gold_assert(p->sh_type == elfcpp::SHT_RELA);
526 this->emit_relocs_scan_reltype<elfcpp::SHT_RELA>(options, symtab,
527 layout, plocal_syms, p,
528 rr);
529 }
530 }
531
532 // Scan the input relocation for --emit-relocs, templatized on the
533 // type of the relocation section.
534
535 template<int size, bool big_endian>
536 template<int sh_type>
537 void
538 Sized_relobj<size, big_endian>::emit_relocs_scan_reltype(
539 const General_options& options,
540 Symbol_table* symtab,
541 Layout* layout,
542 const unsigned char* plocal_syms,
543 const Read_relocs_data::Relocs_list::iterator& p,
544 Relocatable_relocs* rr)
545 {
546 scan_relocatable_relocs<size, big_endian, sh_type,
547 Emit_relocs_strategy<sh_type> >(
548 options,
549 symtab,
550 layout,
551 this,
552 p->data_shndx,
553 p->contents->data(),
554 p->reloc_count,
555 p->output_section,
556 p->needs_special_offset_handling,
557 this->local_symbol_count_,
558 plocal_syms,
559 rr);
560 }
561
562 // Relocate the input sections and write out the local symbols.
563
564 template<int size, bool big_endian>
565 void
566 Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
567 const Symbol_table* symtab,
568 const Layout* layout,
569 Output_file* of)
570 {
571 unsigned int shnum = this->shnum();
572
573 // Read the section headers.
574 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
575 shnum * This::shdr_size,
576 true, true);
577
578 Views views;
579 views.resize(shnum);
580
581 // Make two passes over the sections. The first one copies the
582 // section data to the output file. The second one applies
583 // relocations.
584
585 this->write_sections(pshdrs, of, &views);
586
587 // To speed up relocations, we set up hash tables for fast lookup of
588 // input offsets to output addresses.
589 this->initialize_input_to_output_maps();
590
591 // Apply relocations.
592
593 this->relocate_sections(options, symtab, layout, pshdrs, &views);
594
595 // After we've done the relocations, we release the hash tables,
596 // since we no longer need them.
597 this->free_input_to_output_maps();
598
599 // Write out the accumulated views.
600 for (unsigned int i = 1; i < shnum; ++i)
601 {
602 if (views[i].view != NULL)
603 {
604 if (!views[i].is_postprocessing_view)
605 {
606 if (views[i].is_input_output_view)
607 of->write_input_output_view(views[i].offset,
608 views[i].view_size,
609 views[i].view);
610 else
611 of->write_output_view(views[i].offset, views[i].view_size,
612 views[i].view);
613 }
614 }
615 }
616
617 // Write out the local symbols.
618 this->write_local_symbols(of, layout->sympool(), layout->dynpool(),
619 layout->symtab_xindex(), layout->dynsym_xindex());
620
621 // We should no longer need the local symbol values.
622 this->clear_local_symbols();
623 }
624
625 // Sort a Read_multiple vector by file offset.
626 struct Read_multiple_compare
627 {
628 inline bool
629 operator()(const File_read::Read_multiple_entry& rme1,
630 const File_read::Read_multiple_entry& rme2) const
631 { return rme1.file_offset < rme2.file_offset; }
632 };
633
634 // Write section data to the output file. PSHDRS points to the
635 // section headers. Record the views in *PVIEWS for use when
636 // relocating.
637
638 template<int size, bool big_endian>
639 void
640 Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
641 Output_file* of,
642 Views* pviews)
643 {
644 unsigned int shnum = this->shnum();
645 const Output_sections& out_sections(this->output_sections());
646 const std::vector<Address>& out_offsets(this->section_offsets_);
647
648 File_read::Read_multiple rm;
649 bool is_sorted = true;
650
651 const unsigned char* p = pshdrs + This::shdr_size;
652 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
653 {
654 View_size* pvs = &(*pviews)[i];
655
656 pvs->view = NULL;
657
658 const Output_section* os = out_sections[i];
659 if (os == NULL)
660 continue;
661 Address output_offset = out_offsets[i];
662
663 typename This::Shdr shdr(p);
664
665 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
666 continue;
667
668 if ((parameters->options().relocatable()
669 || parameters->options().emit_relocs())
670 && (shdr.get_sh_type() == elfcpp::SHT_REL
671 || shdr.get_sh_type() == elfcpp::SHT_RELA)
672 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
673 {
674 // This is a reloc section in a relocatable link or when
675 // emitting relocs. We don't need to read the input file.
676 // The size and file offset are stored in the
677 // Relocatable_relocs structure.
678 Relocatable_relocs* rr = this->relocatable_relocs(i);
679 gold_assert(rr != NULL);
680 Output_data* posd = rr->output_data();
681 gold_assert(posd != NULL);
682
683 pvs->offset = posd->offset();
684 pvs->view_size = posd->data_size();
685 pvs->view = of->get_output_view(pvs->offset, pvs->view_size);
686 pvs->address = posd->address();
687 pvs->is_input_output_view = false;
688 pvs->is_postprocessing_view = false;
689
690 continue;
691 }
692
693 // In the normal case, this input section is simply mapped to
694 // the output section at offset OUTPUT_OFFSET.
695
696 // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is
697 // handled specially--e.g., a .eh_frame section. The relocation
698 // routines need to check for each reloc where it should be
699 // applied. For this case, we need an input/output view for the
700 // entire contents of the section in the output file. We don't
701 // want to copy the contents of the input section to the output
702 // section; the output section contents were already written,
703 // and we waited for them in Relocate_task::is_runnable because
704 // relocs_must_follow_section_writes is set for the object.
705
706 // Regardless of which of the above cases is true, we have to
707 // check requires_postprocessing of the output section. If that
708 // is false, then we work with views of the output file
709 // directly. If it is true, then we work with a separate
710 // buffer, and the output section is responsible for writing the
711 // final data to the output file.
712
713 off_t output_section_offset;
714 Address output_section_size;
715 if (!os->requires_postprocessing())
716 {
717 output_section_offset = os->offset();
718 output_section_size = convert_types<Address, off_t>(os->data_size());
719 }
720 else
721 {
722 output_section_offset = 0;
723 output_section_size =
724 convert_types<Address, off_t>(os->postprocessing_buffer_size());
725 }
726
727 off_t view_start;
728 section_size_type view_size;
729 if (output_offset != invalid_address)
730 {
731 view_start = output_section_offset + output_offset;
732 view_size = convert_to_section_size_type(shdr.get_sh_size());
733 }
734 else
735 {
736 view_start = output_section_offset;
737 view_size = convert_to_section_size_type(output_section_size);
738 }
739
740 if (view_size == 0)
741 continue;
742
743 gold_assert(output_offset == invalid_address
744 || output_offset + view_size <= output_section_size);
745
746 unsigned char* view;
747 if (os->requires_postprocessing())
748 {
749 unsigned char* buffer = os->postprocessing_buffer();
750 view = buffer + view_start;
751 if (output_offset != invalid_address)
752 {
753 off_t sh_offset = shdr.get_sh_offset();
754 if (!rm.empty() && rm.back().file_offset > sh_offset)
755 is_sorted = false;
756 rm.push_back(File_read::Read_multiple_entry(sh_offset,
757 view_size, view));
758 }
759 }
760 else
761 {
762 if (output_offset == invalid_address)
763 view = of->get_input_output_view(view_start, view_size);
764 else
765 {
766 view = of->get_output_view(view_start, view_size);
767 off_t sh_offset = shdr.get_sh_offset();
768 if (!rm.empty() && rm.back().file_offset > sh_offset)
769 is_sorted = false;
770 rm.push_back(File_read::Read_multiple_entry(sh_offset,
771 view_size, view));
772 }
773 }
774
775 pvs->view = view;
776 pvs->address = os->address();
777 if (output_offset != invalid_address)
778 pvs->address += output_offset;
779 pvs->offset = view_start;
780 pvs->view_size = view_size;
781 pvs->is_input_output_view = output_offset == invalid_address;
782 pvs->is_postprocessing_view = os->requires_postprocessing();
783 }
784
785 // Actually read the data.
786 if (!rm.empty())
787 {
788 if (!is_sorted)
789 std::sort(rm.begin(), rm.end(), Read_multiple_compare());
790 this->read_multiple(rm);
791 }
792 }
793
794 // Relocate section data. VIEWS points to the section data as views
795 // in the output file.
796
797 template<int size, bool big_endian>
798 void
799 Sized_relobj<size, big_endian>::relocate_sections(
800 const General_options& options,
801 const Symbol_table* symtab,
802 const Layout* layout,
803 const unsigned char* pshdrs,
804 Views* pviews)
805 {
806 unsigned int shnum = this->shnum();
807 Sized_target<size, big_endian>* target =
808 parameters->sized_target<size, big_endian>();
809
810 const Output_sections& out_sections(this->output_sections());
811 const std::vector<Address>& out_offsets(this->section_offsets_);
812
813 Relocate_info<size, big_endian> relinfo;
814 relinfo.options = &options;
815 relinfo.symtab = symtab;
816 relinfo.layout = layout;
817 relinfo.object = this;
818
819 const unsigned char* p = pshdrs + This::shdr_size;
820 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
821 {
822 typename This::Shdr shdr(p);
823
824 unsigned int sh_type = shdr.get_sh_type();
825 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
826 continue;
827
828 off_t sh_size = shdr.get_sh_size();
829 if (sh_size == 0)
830 continue;
831
832 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
833 if (index >= this->shnum())
834 {
835 this->error(_("relocation section %u has bad info %u"),
836 i, index);
837 continue;
838 }
839
840 Output_section* os = out_sections[index];
841 if (os == NULL)
842 {
843 // This relocation section is against a section which we
844 // discarded.
845 continue;
846 }
847 Address output_offset = out_offsets[index];
848
849 gold_assert((*pviews)[index].view != NULL);
850 if (parameters->options().relocatable())
851 gold_assert((*pviews)[i].view != NULL);
852
853 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
854 {
855 gold_error(_("relocation section %u uses unexpected "
856 "symbol table %u"),
857 i, this->adjust_shndx(shdr.get_sh_link()));
858 continue;
859 }
860
861 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
862 sh_size, true, false);
863
864 unsigned int reloc_size;
865 if (sh_type == elfcpp::SHT_REL)
866 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
867 else
868 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
869
870 if (reloc_size != shdr.get_sh_entsize())
871 {
872 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
873 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
874 reloc_size);
875 continue;
876 }
877
878 size_t reloc_count = sh_size / reloc_size;
879 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
880 {
881 gold_error(_("reloc section %u size %lu uneven"),
882 i, static_cast<unsigned long>(sh_size));
883 continue;
884 }
885
886 gold_assert(output_offset != invalid_address
887 || this->relocs_must_follow_section_writes());
888
889 relinfo.reloc_shndx = i;
890 relinfo.data_shndx = index;
891 if (!parameters->options().relocatable())
892 {
893 target->relocate_section(&relinfo,
894 sh_type,
895 prelocs,
896 reloc_count,
897 os,
898 output_offset == invalid_address,
899 (*pviews)[index].view,
900 (*pviews)[index].address,
901 (*pviews)[index].view_size);
902 if (parameters->options().emit_relocs())
903 this->emit_relocs(&relinfo, i, sh_type, prelocs, reloc_count,
904 os, output_offset,
905 (*pviews)[index].view,
906 (*pviews)[index].address,
907 (*pviews)[index].view_size,
908 (*pviews)[i].view,
909 (*pviews)[i].view_size);
910 }
911 else
912 {
913 Relocatable_relocs* rr = this->relocatable_relocs(i);
914 target->relocate_for_relocatable(&relinfo,
915 sh_type,
916 prelocs,
917 reloc_count,
918 os,
919 output_offset,
920 rr,
921 (*pviews)[index].view,
922 (*pviews)[index].address,
923 (*pviews)[index].view_size,
924 (*pviews)[i].view,
925 (*pviews)[i].view_size);
926 }
927 }
928 }
929
930 // Emit the relocs for --emit-relocs.
931
932 template<int size, bool big_endian>
933 void
934 Sized_relobj<size, big_endian>::emit_relocs(
935 const Relocate_info<size, big_endian>* relinfo,
936 unsigned int i,
937 unsigned int sh_type,
938 const unsigned char* prelocs,
939 size_t reloc_count,
940 Output_section* output_section,
941 typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section,
942 unsigned char* view,
943 typename elfcpp::Elf_types<size>::Elf_Addr address,
944 section_size_type view_size,
945 unsigned char* reloc_view,
946 section_size_type reloc_view_size)
947 {
948 if (sh_type == elfcpp::SHT_REL)
949 this->emit_relocs_reltype<elfcpp::SHT_REL>(relinfo, i, prelocs,
950 reloc_count, output_section,
951 offset_in_output_section,
952 view, address, view_size,
953 reloc_view, reloc_view_size);
954 else
955 {
956 gold_assert(sh_type == elfcpp::SHT_RELA);
957 this->emit_relocs_reltype<elfcpp::SHT_RELA>(relinfo, i, prelocs,
958 reloc_count, output_section,
959 offset_in_output_section,
960 view, address, view_size,
961 reloc_view, reloc_view_size);
962 }
963 }
964
965 // Emit the relocs for --emit-relocs, templatized on the type of the
966 // relocation section.
967
968 template<int size, bool big_endian>
969 template<int sh_type>
970 void
971 Sized_relobj<size, big_endian>::emit_relocs_reltype(
972 const Relocate_info<size, big_endian>* relinfo,
973 unsigned int i,
974 const unsigned char* prelocs,
975 size_t reloc_count,
976 Output_section* output_section,
977 typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section,
978 unsigned char* view,
979 typename elfcpp::Elf_types<size>::Elf_Addr address,
980 section_size_type view_size,
981 unsigned char* reloc_view,
982 section_size_type reloc_view_size)
983 {
984 const Relocatable_relocs* rr = this->relocatable_relocs(i);
985 relocate_for_relocatable<size, big_endian, sh_type>(
986 relinfo,
987 prelocs,
988 reloc_count,
989 output_section,
990 offset_in_output_section,
991 rr,
992 view,
993 address,
994 view_size,
995 reloc_view,
996 reloc_view_size);
997 }
998
999 // Create merge hash tables for the local symbols. These are used to
1000 // speed up relocations.
1001
1002 template<int size, bool big_endian>
1003 void
1004 Sized_relobj<size, big_endian>::initialize_input_to_output_maps()
1005 {
1006 const unsigned int loccount = this->local_symbol_count_;
1007 for (unsigned int i = 1; i < loccount; ++i)
1008 {
1009 Symbol_value<size>& lv(this->local_values_[i]);
1010 lv.initialize_input_to_output_map(this);
1011 }
1012 }
1013
1014 // Free merge hash tables for the local symbols.
1015
1016 template<int size, bool big_endian>
1017 void
1018 Sized_relobj<size, big_endian>::free_input_to_output_maps()
1019 {
1020 const unsigned int loccount = this->local_symbol_count_;
1021 for (unsigned int i = 1; i < loccount; ++i)
1022 {
1023 Symbol_value<size>& lv(this->local_values_[i]);
1024 lv.free_input_to_output_map();
1025 }
1026 }
1027
1028 // Class Merged_symbol_value.
1029
1030 template<int size>
1031 void
1032 Merged_symbol_value<size>::initialize_input_to_output_map(
1033 const Relobj* object,
1034 unsigned int input_shndx)
1035 {
1036 Object_merge_map* map = object->merge_map();
1037 map->initialize_input_to_output_map<size>(input_shndx,
1038 this->output_start_address_,
1039 &this->output_addresses_);
1040 }
1041
1042 // Get the output value corresponding to an input offset if we
1043 // couldn't find it in the hash table.
1044
1045 template<int size>
1046 typename elfcpp::Elf_types<size>::Elf_Addr
1047 Merged_symbol_value<size>::value_from_output_section(
1048 const Relobj* object,
1049 unsigned int input_shndx,
1050 typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const
1051 {
1052 section_offset_type output_offset;
1053 bool found = object->merge_map()->get_output_offset(NULL, input_shndx,
1054 input_offset,
1055 &output_offset);
1056
1057 // If this assertion fails, it means that some relocation was
1058 // against a portion of an input merge section which we didn't map
1059 // to the output file and we didn't explicitly discard. We should
1060 // always map all portions of input merge sections.
1061 gold_assert(found);
1062
1063 if (output_offset == -1)
1064 return 0;
1065 else
1066 return this->output_start_address_ + output_offset;
1067 }
1068
1069 // Track_relocs methods.
1070
1071 // Initialize the class to track the relocs. This gets the object,
1072 // the reloc section index, and the type of the relocs. This returns
1073 // false if something goes wrong.
1074
1075 template<int size, bool big_endian>
1076 bool
1077 Track_relocs<size, big_endian>::initialize(
1078 Object* object,
1079 unsigned int reloc_shndx,
1080 unsigned int reloc_type)
1081 {
1082 // If RELOC_SHNDX is -1U, it means there is more than one reloc
1083 // section for the .eh_frame section. We can't handle that case.
1084 if (reloc_shndx == -1U)
1085 return false;
1086
1087 // If RELOC_SHNDX is 0, there is no reloc section.
1088 if (reloc_shndx == 0)
1089 return true;
1090
1091 // Get the contents of the reloc section.
1092 this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
1093
1094 if (reloc_type == elfcpp::SHT_REL)
1095 this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
1096 else if (reloc_type == elfcpp::SHT_RELA)
1097 this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
1098 else
1099 gold_unreachable();
1100
1101 if (this->len_ % this->reloc_size_ != 0)
1102 {
1103 object->error(_("reloc section size %zu is not a multiple of "
1104 "reloc size %d\n"),
1105 static_cast<size_t>(this->len_),
1106 this->reloc_size_);
1107 return false;
1108 }
1109
1110 return true;
1111 }
1112
1113 // Return the offset of the next reloc, or -1 if there isn't one.
1114
1115 template<int size, bool big_endian>
1116 off_t
1117 Track_relocs<size, big_endian>::next_offset() const
1118 {
1119 if (this->pos_ >= this->len_)
1120 return -1;
1121
1122 // Rel and Rela start out the same, so we can always use Rel to find
1123 // the r_offset value.
1124 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1125 return rel.get_r_offset();
1126 }
1127
1128 // Return the index of the symbol referenced by the next reloc, or -1U
1129 // if there aren't any more relocs.
1130
1131 template<int size, bool big_endian>
1132 unsigned int
1133 Track_relocs<size, big_endian>::next_symndx() const
1134 {
1135 if (this->pos_ >= this->len_)
1136 return -1U;
1137
1138 // Rel and Rela start out the same, so we can use Rel to find the
1139 // symbol index.
1140 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1141 return elfcpp::elf_r_sym<size>(rel.get_r_info());
1142 }
1143
1144 // Advance to the next reloc whose r_offset is greater than or equal
1145 // to OFFSET. Return the number of relocs we skip.
1146
1147 template<int size, bool big_endian>
1148 int
1149 Track_relocs<size, big_endian>::advance(off_t offset)
1150 {
1151 int ret = 0;
1152 while (this->pos_ < this->len_)
1153 {
1154 // Rel and Rela start out the same, so we can always use Rel to
1155 // find the r_offset value.
1156 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1157 if (static_cast<off_t>(rel.get_r_offset()) >= offset)
1158 break;
1159 ++ret;
1160 this->pos_ += this->reloc_size_;
1161 }
1162 return ret;
1163 }
1164
1165 // Instantiate the templates we need.
1166
1167 #ifdef HAVE_TARGET_32_LITTLE
1168 template
1169 void
1170 Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
1171 #endif
1172
1173 #ifdef HAVE_TARGET_32_BIG
1174 template
1175 void
1176 Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
1177 #endif
1178
1179 #ifdef HAVE_TARGET_64_LITTLE
1180 template
1181 void
1182 Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
1183 #endif
1184
1185 #ifdef HAVE_TARGET_64_BIG
1186 template
1187 void
1188 Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
1189 #endif
1190
1191 #ifdef HAVE_TARGET_32_LITTLE
1192 template
1193 void
1194 Sized_relobj<32, false>::do_gc_process_relocs(const General_options& options,
1195 Symbol_table* symtab,
1196 Layout* layout,
1197 Read_relocs_data* rd);
1198 #endif
1199
1200 #ifdef HAVE_TARGET_32_BIG
1201 template
1202 void
1203 Sized_relobj<32, true>::do_gc_process_relocs(const General_options& options,
1204 Symbol_table* symtab,
1205 Layout* layout,
1206 Read_relocs_data* rd);
1207 #endif
1208
1209 #ifdef HAVE_TARGET_64_LITTLE
1210 template
1211 void
1212 Sized_relobj<64, false>::do_gc_process_relocs(const General_options& options,
1213 Symbol_table* symtab,
1214 Layout* layout,
1215 Read_relocs_data* rd);
1216 #endif
1217
1218 #ifdef HAVE_TARGET_64_BIG
1219 template
1220 void
1221 Sized_relobj<64, true>::do_gc_process_relocs(const General_options& options,
1222 Symbol_table* symtab,
1223 Layout* layout,
1224 Read_relocs_data* rd);
1225 #endif
1226
1227 #ifdef HAVE_TARGET_32_LITTLE
1228 template
1229 void
1230 Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
1231 Symbol_table* symtab,
1232 Layout* layout,
1233 Read_relocs_data* rd);
1234 #endif
1235
1236 #ifdef HAVE_TARGET_32_BIG
1237 template
1238 void
1239 Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
1240 Symbol_table* symtab,
1241 Layout* layout,
1242 Read_relocs_data* rd);
1243 #endif
1244
1245 #ifdef HAVE_TARGET_64_LITTLE
1246 template
1247 void
1248 Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
1249 Symbol_table* symtab,
1250 Layout* layout,
1251 Read_relocs_data* rd);
1252 #endif
1253
1254 #ifdef HAVE_TARGET_64_BIG
1255 template
1256 void
1257 Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
1258 Symbol_table* symtab,
1259 Layout* layout,
1260 Read_relocs_data* rd);
1261 #endif
1262
1263 #ifdef HAVE_TARGET_32_LITTLE
1264 template
1265 void
1266 Sized_relobj<32, false>::do_relocate(const General_options& options,
1267 const Symbol_table* symtab,
1268 const Layout* layout,
1269 Output_file* of);
1270 #endif
1271
1272 #ifdef HAVE_TARGET_32_BIG
1273 template
1274 void
1275 Sized_relobj<32, true>::do_relocate(const General_options& options,
1276 const Symbol_table* symtab,
1277 const Layout* layout,
1278 Output_file* of);
1279 #endif
1280
1281 #ifdef HAVE_TARGET_64_LITTLE
1282 template
1283 void
1284 Sized_relobj<64, false>::do_relocate(const General_options& options,
1285 const Symbol_table* symtab,
1286 const Layout* layout,
1287 Output_file* of);
1288 #endif
1289
1290 #ifdef HAVE_TARGET_64_BIG
1291 template
1292 void
1293 Sized_relobj<64, true>::do_relocate(const General_options& options,
1294 const Symbol_table* symtab,
1295 const Layout* layout,
1296 Output_file* of);
1297 #endif
1298
1299 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1300 template
1301 class Merged_symbol_value<32>;
1302 #endif
1303
1304 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1305 template
1306 class Merged_symbol_value<64>;
1307 #endif
1308
1309 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1310 template
1311 class Symbol_value<32>;
1312 #endif
1313
1314 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1315 template
1316 class Symbol_value<64>;
1317 #endif
1318
1319 #ifdef HAVE_TARGET_32_LITTLE
1320 template
1321 class Track_relocs<32, false>;
1322 #endif
1323
1324 #ifdef HAVE_TARGET_32_BIG
1325 template
1326 class Track_relocs<32, true>;
1327 #endif
1328
1329 #ifdef HAVE_TARGET_64_LITTLE
1330 template
1331 class Track_relocs<64, false>;
1332 #endif
1333
1334 #ifdef HAVE_TARGET_64_BIG
1335 template
1336 class Track_relocs<64, true>;
1337 #endif
1338
1339 } // End namespace gold.
This page took 0.061424 seconds and 5 git commands to generate.