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